1/*
2 * Copyright (c) 2007, 2008 Apple Inc.
3 * Copyright (c) 2004 Torrey T. Lyons. All Rights Reserved.
4 * Copyright (c) 2002 Greg Parker. All Rights Reserved.
5 *
6 * Portions of this file are copied from Mesa's xf86glx.c,
7 * which contains the following copyright:
8 *
9 * Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
10 * All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 * DEALINGS IN THE SOFTWARE.
29 */
30
31#ifdef HAVE_DIX_CONFIG_H
32#include <dix-config.h>
33#endif
34
35#include "dri.h"
36
37#include <OpenGL/OpenGL.h>
38#include <OpenGL/gl.h>
39#include <OpenGL/glext.h>
40#include <OpenGL/CGLContext.h>
41
42#include <GL/glxproto.h>
43#include <windowstr.h>
44#include <resource.h>
45#include <GL/glxint.h>
46#include <GL/glxtokens.h>
47#include <scrnintstr.h>
48#include <glxserver.h>
49#include <glxscreens.h>
50#include <glxdrawable.h>
51#include <glxcontext.h>
52#include <glxext.h>
53#include <glxutil.h>
54#include <glxscreens.h>
55#include <GL/internal/glcore.h>
56
57#include "capabilities.h"
58#include "visualConfigs.h"
59#include "darwinfb.h"
60
61/* Based originally on code from indirect.c which was based on code from i830_dri.c. */
62__GLXconfig *__glXAquaCreateVisualConfigs(int *numConfigsPtr, int screenNumber) {
63    int numConfigs = 0;
64    __GLXconfig *visualConfigs, *c;
65    struct glCapabilities caps;
66    struct glCapabilitiesConfig *conf;
67    int stereo, depth, aux, buffers, stencil, accum, color, msample;
68
69    if(getGlCapabilities(&caps)) {
70        ErrorF("error from getGlCapabilities()!\n");
71        return NULL;
72    }
73
74    /*
75     conf->stereo is 0 or 1, but we need at least 1 iteration of the loop,
76     so we treat a true conf->stereo as 2.
77
78     The depth size is 0 or 24.  Thus we do 2 iterations for that.
79
80     conf->aux_buffers (when available/non-zero) result in 2 iterations instead of 1.
81
82     conf->buffers indicates whether we have single or double buffering.
83
84     conf->total_stencil_bit_depths
85
86     conf->total_color_buffers indicates the RGB/RGBA color depths.
87
88     conf->total_accum_buffers iterations for accum (with at least 1 if equal to 0)
89
90     conf->total_depth_buffer_depths
91
92     conf->multisample_buffers iterations (with at least 1 if equal to 0).  We add 1
93     for the 0 multisampling config.
94
95     */
96
97    assert(NULL != caps.configurations);
98
99    numConfigs = 0;
100
101    for(conf = caps.configurations; conf; conf = conf->next) {
102        if(conf->total_color_buffers <= 0)
103            continue;
104
105        numConfigs += (conf->stereo ? 2 : 1)
106	    * (conf->aux_buffers ? 2 : 1)
107	    * conf->buffers
108	    * ((conf->total_stencil_bit_depths > 0) ? conf->total_stencil_bit_depths : 1)
109	    * conf->total_color_buffers
110	    * ((conf->total_accum_buffers > 0) ? conf->total_accum_buffers : 1)
111	    * conf->total_depth_buffer_depths
112	    * (conf->multisample_buffers + 1);
113    }
114
115    if(numConfigsPtr)
116        *numConfigsPtr = numConfigs;
117
118    visualConfigs = calloc(sizeof(*visualConfigs), numConfigs);
119
120    if(NULL == visualConfigs) {
121        ErrorF("xcalloc failure when allocating visualConfigs\n");
122        freeGlCapabilities(&caps);
123        return NULL;
124    }
125
126    c = visualConfigs; /* current buffer */
127    for(conf = caps.configurations; conf; conf = conf->next) {
128        for(stereo = 0; stereo < (conf->stereo ? 2 : 1); ++stereo) {
129            for(aux = 0; aux < (conf->aux_buffers ? 2 : 1); ++aux) {
130                for(buffers = 0; buffers < conf->buffers; ++buffers) {
131                    for(stencil = 0; stencil < ((conf->total_stencil_bit_depths > 0) ?
132                                                conf->total_stencil_bit_depths : 1); ++stencil) {
133                        for(color = 0; color < conf->total_color_buffers; ++color) {
134                            for(accum = 0; accum < ((conf->total_accum_buffers > 0) ?
135                                                    conf->total_accum_buffers : 1); ++accum) {
136                                for(depth = 0; depth < conf->total_depth_buffer_depths; ++depth) {
137                                    for(msample = 0; msample < (conf->multisample_buffers + 1); ++msample) {
138
139                                        // Global
140                                        c->visualID = -1;
141                                        c->visualType = GLX_TRUE_COLOR;
142                                        c->next = c + 1;
143
144                                        c->screen = screenNumber;
145
146                                        c->level = 0;
147                                        c->indexBits = 0;
148                                        c->pixmapMode = 0; // TODO: What should this be?
149
150                                        if(conf->accelerated) {
151                                            c->visualRating = GLX_NONE;
152                                        } else {
153                                            c->visualRating = GLX_SLOW_VISUAL_EXT;
154                                        }
155
156                                        c->transparentPixel = GLX_NONE;
157                                        c->transparentRed = GLX_NONE;
158                                        c->transparentGreen = GLX_NONE;
159                                        c->transparentBlue = GLX_NONE;
160                                        c->transparentAlpha = GLX_NONE;
161                                        c->transparentIndex = GLX_NONE;
162
163                                        c->visualSelectGroup = 0;
164
165                                        c->swapMethod = GLX_SWAP_UNDEFINED_OML;
166
167                                        // Stereo
168                                        c->stereoMode = stereo ? TRUE : FALSE;
169
170                                        // Aux buffers
171                                        c->numAuxBuffers = aux ? conf->aux_buffers : 0;
172
173                                        // Double Buffered
174                                        c->doubleBufferMode = buffers ? TRUE : FALSE;
175
176                                        // Stencil Buffer
177                                        if(conf->total_stencil_bit_depths > 0) {
178                                            c->stencilBits = conf->stencil_bit_depths[stencil];
179                                        } else {
180                                            c->stencilBits = 0;
181                                        }
182
183                                        // Color
184                                        if(GLCAPS_COLOR_BUF_INVALID_VALUE != conf->color_buffers[color].a) {
185                                            c->alphaBits = conf->color_buffers[color].a;
186                                        } else {
187                                            c->alphaBits = 0;
188                                        }
189                                        c->redBits   = conf->color_buffers[color].r;
190                                        c->greenBits = conf->color_buffers[color].g;
191                                        c->blueBits  = conf->color_buffers[color].b;
192
193                                        c->rgbBits = c->alphaBits + c->redBits + c->greenBits + c->blueBits;
194
195                                        c->alphaMask = AM_ARGB(c->alphaBits, c->redBits, c->greenBits, c->blueBits);
196                                        c->redMask   = RM_ARGB(c->alphaBits, c->redBits, c->greenBits, c->blueBits);
197                                        c->greenMask = GM_ARGB(c->alphaBits, c->redBits, c->greenBits, c->blueBits);
198                                        c->blueMask  = BM_ARGB(c->alphaBits, c->redBits, c->greenBits, c->blueBits);
199
200                                        // Accumulation Buffers
201                                        if(conf->total_accum_buffers > 0) {
202                                            c->accumRedBits = conf->accum_buffers[accum].r;
203                                            c->accumGreenBits = conf->accum_buffers[accum].g;
204                                            c->accumBlueBits = conf->accum_buffers[accum].b;
205                                            if(GLCAPS_COLOR_BUF_INVALID_VALUE != conf->accum_buffers[accum].a) {
206                                                c->accumAlphaBits = conf->accum_buffers[accum].a;
207                                            } else {
208                                                c->accumAlphaBits = 0;
209                                            }
210                                        } else {
211                                            c->accumRedBits = 0;
212                                            c->accumGreenBits = 0;
213                                            c->accumBlueBits = 0;
214                                            c->accumAlphaBits = 0;
215                                        }
216
217                                        // Depth
218                                        c->depthBits = conf->depth_buffers[depth];
219
220                                        // MultiSample
221                                        if(msample > 0) {
222                                            c->samples = conf->multisample_samples;
223                                            c->sampleBuffers = conf->multisample_buffers;
224                                        } else {
225                                            c->samples = 0;
226                                            c->sampleBuffers = 0;
227                                        }
228
229                                        /*
230                                         * The Apple libGL supports GLXPixmaps and
231                                         * GLXPbuffers in direct mode.
232                                         */
233                                        /* SGIX_fbconfig / GLX 1.3 */
234                                        c->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
235                                        c->renderType = GLX_RGBA_BIT;
236                                        c->xRenderable = GL_TRUE;
237                                        c->fbconfigID = -1;
238
239                                        /* SGIX_pbuffer / GLX 1.3 */
240
241                                        /*
242                                         * The CGL layer provides a way of retrieving
243                                         * the maximum pbuffer width/height, but only
244                                         * if we create a context and call glGetIntegerv.
245                                         *
246                                         * The following values are from a test program
247                                         * that does so.
248                                         */
249                                        c->maxPbufferWidth = 8192;
250                                        c->maxPbufferHeight = 8192;
251                                        c->maxPbufferPixels = /*Do we need this?*/ 0;
252                                        /*
253                                         * There is no introspection for this sort of thing
254                                         * with CGL.  What should we do realistically?
255                                         */
256                                        c->optimalPbufferWidth = 0;
257                                        c->optimalPbufferHeight = 0;
258
259                                        /* EXT_texture_from_pixmap */
260                                        c->bindToTextureRgb = 0;
261                                        c->bindToTextureRgba = 0;
262                                        c->bindToMipmapTexture = 0;
263                                        c->bindToTextureTargets = 0;
264                                        c->yInverted = 0;
265
266                                        c = c->next;
267                                    }
268                                }
269                            }
270                        }
271                    }
272                }
273            }
274        }
275    }
276
277    (c-1)->next = NULL;
278
279    if (c - visualConfigs != numConfigs) {
280        FatalError("numConfigs calculation error in setVisualConfigs!  numConfigs is %d  i is %d\n", numConfigs, (int)(c - visualConfigs));
281    }
282
283    freeGlCapabilities(&caps);
284    return visualConfigs;
285}
286