1706f2543Smrg/*
2706f2543Smrg * Copyright (c) 2007, 2008 Apple Inc.
3706f2543Smrg * Copyright (c) 2004 Torrey T. Lyons. All Rights Reserved.
4706f2543Smrg * Copyright (c) 2002 Greg Parker. All Rights Reserved.
5706f2543Smrg *
6706f2543Smrg * Portions of this file are copied from Mesa's xf86glx.c,
7706f2543Smrg * which contains the following copyright:
8706f2543Smrg *
9706f2543Smrg * Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
10706f2543Smrg * All Rights Reserved.
11706f2543Smrg *
12706f2543Smrg * Permission is hereby granted, free of charge, to any person obtaining a
13706f2543Smrg * copy of this software and associated documentation files (the "Software"),
14706f2543Smrg * to deal in the Software without restriction, including without limitation
15706f2543Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16706f2543Smrg * and/or sell copies of the Software, and to permit persons to whom the
17706f2543Smrg * Software is furnished to do so, subject to the following conditions:
18706f2543Smrg *
19706f2543Smrg * The above copyright notice and this permission notice shall be included in
20706f2543Smrg * all copies or substantial portions of the Software.
21706f2543Smrg *
22706f2543Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23706f2543Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24706f2543Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25706f2543Smrg * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
26706f2543Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27706f2543Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28706f2543Smrg * DEALINGS IN THE SOFTWARE.
29706f2543Smrg */
30706f2543Smrg
31706f2543Smrg#ifdef HAVE_DIX_CONFIG_H
32706f2543Smrg#include <dix-config.h>
33706f2543Smrg#endif
34706f2543Smrg
35706f2543Smrg#include "dri.h"
36706f2543Smrg
37706f2543Smrg#include <OpenGL/OpenGL.h>
38706f2543Smrg#include <OpenGL/gl.h>
39706f2543Smrg#include <OpenGL/glext.h>
40706f2543Smrg#include <OpenGL/CGLContext.h>
41706f2543Smrg
42706f2543Smrg#include <GL/glxproto.h>
43706f2543Smrg#include <windowstr.h>
44706f2543Smrg#include <resource.h>
45706f2543Smrg#include <GL/glxint.h>
46706f2543Smrg#include <GL/glxtokens.h>
47706f2543Smrg#include <scrnintstr.h>
48706f2543Smrg#include <glxserver.h>
49706f2543Smrg#include <glxscreens.h>
50706f2543Smrg#include <glxdrawable.h>
51706f2543Smrg#include <glxcontext.h>
52706f2543Smrg#include <glxext.h>
53706f2543Smrg#include <glxutil.h>
54706f2543Smrg#include <glxscreens.h>
55706f2543Smrg#include <GL/internal/glcore.h>
56706f2543Smrg
57706f2543Smrg#include "capabilities.h"
58706f2543Smrg#include "visualConfigs.h"
59706f2543Smrg#include "darwinfb.h"
60706f2543Smrg
61706f2543Smrg/* Based originally on code from indirect.c which was based on code from i830_dri.c. */
62706f2543Smrg__GLXconfig *__glXAquaCreateVisualConfigs(int *numConfigsPtr, int screenNumber) {
63706f2543Smrg    int numConfigs = 0;
64706f2543Smrg    __GLXconfig *visualConfigs, *c;
65706f2543Smrg    struct glCapabilities caps;
66706f2543Smrg    struct glCapabilitiesConfig *conf;
67706f2543Smrg    int stereo, depth, aux, buffers, stencil, accum, color, msample;
68706f2543Smrg
69706f2543Smrg    if(getGlCapabilities(&caps)) {
70706f2543Smrg        ErrorF("error from getGlCapabilities()!\n");
71706f2543Smrg        return NULL;
72706f2543Smrg    }
73706f2543Smrg
74706f2543Smrg    /*
75706f2543Smrg     conf->stereo is 0 or 1, but we need at least 1 iteration of the loop,
76706f2543Smrg     so we treat a true conf->stereo as 2.
77706f2543Smrg
78706f2543Smrg     The depth size is 0 or 24.  Thus we do 2 iterations for that.
79706f2543Smrg
80706f2543Smrg     conf->aux_buffers (when available/non-zero) result in 2 iterations instead of 1.
81706f2543Smrg
82706f2543Smrg     conf->buffers indicates whether we have single or double buffering.
83706f2543Smrg
84706f2543Smrg     conf->total_stencil_bit_depths
85706f2543Smrg
86706f2543Smrg     conf->total_color_buffers indicates the RGB/RGBA color depths.
87706f2543Smrg
88706f2543Smrg     conf->total_accum_buffers iterations for accum (with at least 1 if equal to 0)
89706f2543Smrg
90706f2543Smrg     conf->total_depth_buffer_depths
91706f2543Smrg
92706f2543Smrg     conf->multisample_buffers iterations (with at least 1 if equal to 0).  We add 1
93706f2543Smrg     for the 0 multisampling config.
94706f2543Smrg
95706f2543Smrg     */
96706f2543Smrg
97706f2543Smrg    assert(NULL != caps.configurations);
98706f2543Smrg
99706f2543Smrg    numConfigs = 0;
100706f2543Smrg
101706f2543Smrg    for(conf = caps.configurations; conf; conf = conf->next) {
102706f2543Smrg        if(conf->total_color_buffers <= 0)
103706f2543Smrg            continue;
104706f2543Smrg
105706f2543Smrg        numConfigs += (conf->stereo ? 2 : 1)
106706f2543Smrg	    * (conf->aux_buffers ? 2 : 1)
107706f2543Smrg	    * conf->buffers
108706f2543Smrg	    * ((conf->total_stencil_bit_depths > 0) ? conf->total_stencil_bit_depths : 1)
109706f2543Smrg	    * conf->total_color_buffers
110706f2543Smrg	    * ((conf->total_accum_buffers > 0) ? conf->total_accum_buffers : 1)
111706f2543Smrg	    * conf->total_depth_buffer_depths
112706f2543Smrg	    * (conf->multisample_buffers + 1);
113706f2543Smrg    }
114706f2543Smrg
115706f2543Smrg    if(numConfigsPtr)
116706f2543Smrg        *numConfigsPtr = numConfigs;
117706f2543Smrg
118706f2543Smrg    visualConfigs = calloc(sizeof(*visualConfigs), numConfigs);
119706f2543Smrg
120706f2543Smrg    if(NULL == visualConfigs) {
121706f2543Smrg        ErrorF("xcalloc failure when allocating visualConfigs\n");
122706f2543Smrg        freeGlCapabilities(&caps);
123706f2543Smrg        return NULL;
124706f2543Smrg    }
125706f2543Smrg
126706f2543Smrg    c = visualConfigs; /* current buffer */
127706f2543Smrg    for(conf = caps.configurations; conf; conf = conf->next) {
128706f2543Smrg        for(stereo = 0; stereo < (conf->stereo ? 2 : 1); ++stereo) {
129706f2543Smrg            for(aux = 0; aux < (conf->aux_buffers ? 2 : 1); ++aux) {
130706f2543Smrg                for(buffers = 0; buffers < conf->buffers; ++buffers) {
131706f2543Smrg                    for(stencil = 0; stencil < ((conf->total_stencil_bit_depths > 0) ?
132706f2543Smrg                                                conf->total_stencil_bit_depths : 1); ++stencil) {
133706f2543Smrg                        for(color = 0; color < conf->total_color_buffers; ++color) {
134706f2543Smrg                            for(accum = 0; accum < ((conf->total_accum_buffers > 0) ?
135706f2543Smrg                                                    conf->total_accum_buffers : 1); ++accum) {
136706f2543Smrg                                for(depth = 0; depth < conf->total_depth_buffer_depths; ++depth) {
137706f2543Smrg                                    for(msample = 0; msample < (conf->multisample_buffers + 1); ++msample) {
138706f2543Smrg
139706f2543Smrg                                        // Global
140706f2543Smrg                                        c->visualID = -1;
141706f2543Smrg                                        c->visualType = GLX_TRUE_COLOR;
142706f2543Smrg                                        c->next = c + 1;
143706f2543Smrg
144706f2543Smrg                                        c->screen = screenNumber;
145706f2543Smrg
146706f2543Smrg                                        c->level = 0;
147706f2543Smrg                                        c->indexBits = 0;
148706f2543Smrg                                        c->pixmapMode = 0; // TODO: What should this be?
149706f2543Smrg
150706f2543Smrg                                        if(conf->accelerated) {
151706f2543Smrg                                            c->visualRating = GLX_NONE;
152706f2543Smrg                                        } else {
153706f2543Smrg                                            c->visualRating = GLX_SLOW_VISUAL_EXT;
154706f2543Smrg                                        }
155706f2543Smrg
156706f2543Smrg                                        c->transparentPixel = GLX_NONE;
157706f2543Smrg                                        c->transparentRed = GLX_NONE;
158706f2543Smrg                                        c->transparentGreen = GLX_NONE;
159706f2543Smrg                                        c->transparentBlue = GLX_NONE;
160706f2543Smrg                                        c->transparentAlpha = GLX_NONE;
161706f2543Smrg                                        c->transparentIndex = GLX_NONE;
162706f2543Smrg
163706f2543Smrg                                        c->visualSelectGroup = 0;
164706f2543Smrg
165706f2543Smrg                                        c->swapMethod = GLX_SWAP_UNDEFINED_OML;
166706f2543Smrg
167706f2543Smrg                                        // Stereo
168706f2543Smrg                                        c->stereoMode = stereo ? TRUE : FALSE;
169706f2543Smrg
170706f2543Smrg                                        // Aux buffers
171706f2543Smrg                                        c->numAuxBuffers = aux ? conf->aux_buffers : 0;
172706f2543Smrg
173706f2543Smrg                                        // Double Buffered
174706f2543Smrg                                        c->doubleBufferMode = buffers ? TRUE : FALSE;
175706f2543Smrg
176706f2543Smrg                                        // Stencil Buffer
177706f2543Smrg                                        if(conf->total_stencil_bit_depths > 0) {
178706f2543Smrg                                            c->stencilBits = conf->stencil_bit_depths[stencil];
179706f2543Smrg                                        } else {
180706f2543Smrg                                            c->stencilBits = 0;
181706f2543Smrg                                        }
182706f2543Smrg
183706f2543Smrg                                        // Color
184706f2543Smrg                                        if(GLCAPS_COLOR_BUF_INVALID_VALUE != conf->color_buffers[color].a) {
185706f2543Smrg                                            c->alphaBits = conf->color_buffers[color].a;
186706f2543Smrg                                        } else {
187706f2543Smrg                                            c->alphaBits = 0;
188706f2543Smrg                                        }
189706f2543Smrg                                        c->redBits   = conf->color_buffers[color].r;
190706f2543Smrg                                        c->greenBits = conf->color_buffers[color].g;
191706f2543Smrg                                        c->blueBits  = conf->color_buffers[color].b;
192706f2543Smrg
193706f2543Smrg                                        c->rgbBits = c->alphaBits + c->redBits + c->greenBits + c->blueBits;
194706f2543Smrg
195706f2543Smrg                                        c->alphaMask = AM_ARGB(c->alphaBits, c->redBits, c->greenBits, c->blueBits);
196706f2543Smrg                                        c->redMask   = RM_ARGB(c->alphaBits, c->redBits, c->greenBits, c->blueBits);
197706f2543Smrg                                        c->greenMask = GM_ARGB(c->alphaBits, c->redBits, c->greenBits, c->blueBits);
198706f2543Smrg                                        c->blueMask  = BM_ARGB(c->alphaBits, c->redBits, c->greenBits, c->blueBits);
199706f2543Smrg
200706f2543Smrg                                        // Accumulation Buffers
201706f2543Smrg                                        if(conf->total_accum_buffers > 0) {
202706f2543Smrg                                            c->accumRedBits = conf->accum_buffers[accum].r;
203706f2543Smrg                                            c->accumGreenBits = conf->accum_buffers[accum].g;
204706f2543Smrg                                            c->accumBlueBits = conf->accum_buffers[accum].b;
205706f2543Smrg                                            if(GLCAPS_COLOR_BUF_INVALID_VALUE != conf->accum_buffers[accum].a) {
206706f2543Smrg                                                c->accumAlphaBits = conf->accum_buffers[accum].a;
207706f2543Smrg                                            } else {
208706f2543Smrg                                                c->accumAlphaBits = 0;
209706f2543Smrg                                            }
210706f2543Smrg                                        } else {
211706f2543Smrg                                            c->accumRedBits = 0;
212706f2543Smrg                                            c->accumGreenBits = 0;
213706f2543Smrg                                            c->accumBlueBits = 0;
214706f2543Smrg                                            c->accumAlphaBits = 0;
215706f2543Smrg                                        }
216706f2543Smrg
217706f2543Smrg                                        // Depth
218706f2543Smrg                                        c->depthBits = conf->depth_buffers[depth];
219706f2543Smrg
220706f2543Smrg                                        // MultiSample
221706f2543Smrg                                        if(msample > 0) {
222706f2543Smrg                                            c->samples = conf->multisample_samples;
223706f2543Smrg                                            c->sampleBuffers = conf->multisample_buffers;
224706f2543Smrg                                        } else {
225706f2543Smrg                                            c->samples = 0;
226706f2543Smrg                                            c->sampleBuffers = 0;
227706f2543Smrg                                        }
228706f2543Smrg
229706f2543Smrg                                        /*
230706f2543Smrg                                         * The Apple libGL supports GLXPixmaps and
231706f2543Smrg                                         * GLXPbuffers in direct mode.
232706f2543Smrg                                         */
233706f2543Smrg                                        /* SGIX_fbconfig / GLX 1.3 */
234706f2543Smrg                                        c->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
235706f2543Smrg                                        c->renderType = GLX_RGBA_BIT;
236706f2543Smrg                                        c->xRenderable = GL_TRUE;
237706f2543Smrg                                        c->fbconfigID = -1;
238706f2543Smrg
239706f2543Smrg                                        /* SGIX_pbuffer / GLX 1.3 */
240706f2543Smrg
241706f2543Smrg                                        /*
242706f2543Smrg                                         * The CGL layer provides a way of retrieving
243706f2543Smrg                                         * the maximum pbuffer width/height, but only
244706f2543Smrg                                         * if we create a context and call glGetIntegerv.
245706f2543Smrg                                         *
246706f2543Smrg                                         * The following values are from a test program
247706f2543Smrg                                         * that does so.
248706f2543Smrg                                         */
249706f2543Smrg                                        c->maxPbufferWidth = 8192;
250706f2543Smrg                                        c->maxPbufferHeight = 8192;
251706f2543Smrg                                        c->maxPbufferPixels = /*Do we need this?*/ 0;
252706f2543Smrg                                        /*
253706f2543Smrg                                         * There is no introspection for this sort of thing
254706f2543Smrg                                         * with CGL.  What should we do realistically?
255706f2543Smrg                                         */
256706f2543Smrg                                        c->optimalPbufferWidth = 0;
257706f2543Smrg                                        c->optimalPbufferHeight = 0;
258706f2543Smrg
259706f2543Smrg                                        /* EXT_texture_from_pixmap */
260706f2543Smrg                                        c->bindToTextureRgb = 0;
261706f2543Smrg                                        c->bindToTextureRgba = 0;
262706f2543Smrg                                        c->bindToMipmapTexture = 0;
263706f2543Smrg                                        c->bindToTextureTargets = 0;
264706f2543Smrg                                        c->yInverted = 0;
265706f2543Smrg
266706f2543Smrg                                        c = c->next;
267706f2543Smrg                                    }
268706f2543Smrg                                }
269706f2543Smrg                            }
270706f2543Smrg                        }
271706f2543Smrg                    }
272706f2543Smrg                }
273706f2543Smrg            }
274706f2543Smrg        }
275706f2543Smrg    }
276706f2543Smrg
277706f2543Smrg    (c-1)->next = NULL;
278706f2543Smrg
279706f2543Smrg    if (c - visualConfigs != numConfigs) {
280706f2543Smrg        FatalError("numConfigs calculation error in setVisualConfigs!  numConfigs is %d  i is %d\n", numConfigs, (int)(c - visualConfigs));
281706f2543Smrg    }
282706f2543Smrg
283706f2543Smrg    freeGlCapabilities(&caps);
284706f2543Smrg    return visualConfigs;
285706f2543Smrg}
286