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 
55 #include "capabilities.h"
56 #include "visualConfigs.h"
57 #include "darwinfb.h"
58 
59 /* Based originally on code from indirect.c which was based on code from i830_dri.c. */
__glXAquaCreateVisualConfigs(int * numConfigsPtr,int screenNumber)60 __GLXconfig *__glXAquaCreateVisualConfigs(int *numConfigsPtr, int screenNumber) {
61     int numConfigs = 0;
62     __GLXconfig *visualConfigs, *c, *l;
63     struct glCapabilities caps;
64     struct glCapabilitiesConfig *conf;
65     int stereo, depth, aux, buffers, stencil, accum, color, msample;
66 
67     if(getGlCapabilities(&caps)) {
68         ErrorF("error from getGlCapabilities()!\n");
69         return NULL;
70     }
71 
72     /*
73      conf->stereo is 0 or 1, but we need at least 1 iteration of the loop,
74      so we treat a true conf->stereo as 2.
75 
76      The depth size is 0 or 24.  Thus we do 2 iterations for that.
77 
78      conf->aux_buffers (when available/non-zero) result in 2 iterations instead of 1.
79 
80      conf->buffers indicates whether we have single or double buffering.
81 
82      conf->total_stencil_bit_depths
83 
84      conf->total_color_buffers indicates the RGB/RGBA color depths.
85 
86      conf->total_accum_buffers iterations for accum (with at least 1 if equal to 0)
87 
88      conf->total_depth_buffer_depths
89 
90      conf->multisample_buffers iterations (with at least 1 if equal to 0).  We add 1
91      for the 0 multisampling config.
92 
93      */
94 
95     assert(NULL != caps.configurations);
96 
97     numConfigs = 0;
98 
99     for(conf = caps.configurations; conf; conf = conf->next) {
100         if(conf->total_color_buffers <= 0)
101             continue;
102 
103         numConfigs += (conf->stereo ? 2 : 1)
104 	    * (conf->aux_buffers ? 2 : 1)
105 	    * conf->buffers
106 	    * ((conf->total_stencil_bit_depths > 0) ? conf->total_stencil_bit_depths : 1)
107 	    * conf->total_color_buffers
108 	    * ((conf->total_accum_buffers > 0) ? conf->total_accum_buffers : 1)
109 	    * conf->total_depth_buffer_depths
110 	    * (conf->multisample_buffers + 1);
111     }
112 
113     if(numConfigsPtr)
114         *numConfigsPtr = numConfigs;
115 
116     /* Note that as of 1.20.0, we cannot allocate all the configs at once.
117      * __glXScreenDestroy now walks all the fbconfigs and frees them one at a time.
118      * See 4b0a3cbab131eb453e2b3fc0337121969258a7be.
119      */
120     visualConfigs = calloc(sizeof(*visualConfigs), 1);
121 
122     l = NULL;
123     c = visualConfigs; /* current buffer */
124     for(conf = caps.configurations; conf; conf = conf->next) {
125         for(stereo = 0; stereo < (conf->stereo ? 2 : 1); ++stereo) {
126             for(aux = 0; aux < (conf->aux_buffers ? 2 : 1); ++aux) {
127                 for(buffers = 0; buffers < conf->buffers; ++buffers) {
128                     for(stencil = 0; stencil < ((conf->total_stencil_bit_depths > 0) ?
129                                                 conf->total_stencil_bit_depths : 1); ++stencil) {
130                         for(color = 0; color < conf->total_color_buffers; ++color) {
131                             for(accum = 0; accum < ((conf->total_accum_buffers > 0) ?
132                                                     conf->total_accum_buffers : 1); ++accum) {
133                                 for(depth = 0; depth < conf->total_depth_buffer_depths; ++depth) {
134                                     for(msample = 0; msample < (conf->multisample_buffers + 1); ++msample) {
135 
136                                         // Global
137                                         c->visualID = -1;
138                                         c->visualType = GLX_TRUE_COLOR;
139                                         c->next = calloc(sizeof(*visualConfigs), 1);
140                                         assert(c->next);
141 
142                                         c->level = 0;
143                                         c->indexBits = 0;
144 
145                                         if(conf->accelerated) {
146                                             c->visualRating = GLX_NONE;
147                                         } else {
148                                             c->visualRating = GLX_SLOW_VISUAL_EXT;
149                                         }
150 
151                                         c->transparentPixel = GLX_NONE;
152                                         c->transparentRed = GLX_NONE;
153                                         c->transparentGreen = GLX_NONE;
154                                         c->transparentBlue = GLX_NONE;
155                                         c->transparentAlpha = GLX_NONE;
156                                         c->transparentIndex = GLX_NONE;
157 
158                                         c->visualSelectGroup = 0;
159 
160                                         c->swapMethod = GLX_SWAP_UNDEFINED_OML;
161 
162                                         // Stereo
163                                         c->stereoMode = stereo ? TRUE : FALSE;
164 
165                                         // Aux buffers
166                                         c->numAuxBuffers = aux ? conf->aux_buffers : 0;
167 
168                                         // Double Buffered
169                                         c->doubleBufferMode = buffers ? TRUE : FALSE;
170 
171                                         // Stencil Buffer
172                                         if(conf->total_stencil_bit_depths > 0) {
173                                             c->stencilBits = conf->stencil_bit_depths[stencil];
174                                         } else {
175                                             c->stencilBits = 0;
176                                         }
177 
178                                         // Color
179                                         if(GLCAPS_COLOR_BUF_INVALID_VALUE != conf->color_buffers[color].a) {
180                                             c->alphaBits = conf->color_buffers[color].a;
181                                         } else {
182                                             c->alphaBits = 0;
183                                         }
184                                         c->redBits   = conf->color_buffers[color].r;
185                                         c->greenBits = conf->color_buffers[color].g;
186                                         c->blueBits  = conf->color_buffers[color].b;
187 
188                                         c->rgbBits = c->alphaBits + c->redBits + c->greenBits + c->blueBits;
189 
190                                         c->alphaMask = AM_ARGB(c->alphaBits, c->redBits, c->greenBits, c->blueBits);
191                                         c->redMask   = RM_ARGB(c->alphaBits, c->redBits, c->greenBits, c->blueBits);
192                                         c->greenMask = GM_ARGB(c->alphaBits, c->redBits, c->greenBits, c->blueBits);
193                                         c->blueMask  = BM_ARGB(c->alphaBits, c->redBits, c->greenBits, c->blueBits);
194 
195                                         // Accumulation Buffers
196                                         if(conf->total_accum_buffers > 0) {
197                                             c->accumRedBits = conf->accum_buffers[accum].r;
198                                             c->accumGreenBits = conf->accum_buffers[accum].g;
199                                             c->accumBlueBits = conf->accum_buffers[accum].b;
200                                             if(GLCAPS_COLOR_BUF_INVALID_VALUE != conf->accum_buffers[accum].a) {
201                                                 c->accumAlphaBits = conf->accum_buffers[accum].a;
202                                             } else {
203                                                 c->accumAlphaBits = 0;
204                                             }
205                                         } else {
206                                             c->accumRedBits = 0;
207                                             c->accumGreenBits = 0;
208                                             c->accumBlueBits = 0;
209                                             c->accumAlphaBits = 0;
210                                         }
211 
212                                         // Depth
213                                         c->depthBits = conf->depth_buffers[depth];
214 
215                                         // MultiSample
216                                         if(msample > 0) {
217                                             c->samples = conf->multisample_samples;
218                                             c->sampleBuffers = conf->multisample_buffers;
219                                         } else {
220                                             c->samples = 0;
221                                             c->sampleBuffers = 0;
222                                         }
223 
224                                         /*
225                                          * The Apple libGL supports GLXPixmaps and
226                                          * GLXPbuffers in direct mode.
227                                          */
228                                         /* SGIX_fbconfig / GLX 1.3 */
229                                         c->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
230                                         c->renderType = GLX_RGBA_BIT;
231                                         c->fbconfigID = -1;
232 
233                                         /* SGIX_pbuffer / GLX 1.3 */
234 
235                                         /*
236                                          * The CGL layer provides a way of retrieving
237                                          * the maximum pbuffer width/height, but only
238                                          * if we create a context and call glGetIntegerv.
239                                          *
240                                          * The following values are from a test program
241                                          * that does so.
242                                          */
243                                         c->maxPbufferWidth = 8192;
244                                         c->maxPbufferHeight = 8192;
245                                         c->maxPbufferPixels = /*Do we need this?*/ 0;
246                                         /*
247                                          * There is no introspection for this sort of thing
248                                          * with CGL.  What should we do realistically?
249                                          */
250                                         c->optimalPbufferWidth = 0;
251                                         c->optimalPbufferHeight = 0;
252 
253                                         /* EXT_texture_from_pixmap */
254                                         c->bindToTextureRgb = 0;
255                                         c->bindToTextureRgba = 0;
256                                         c->bindToMipmapTexture = 0;
257                                         c->bindToTextureTargets = 0;
258                                         c->yInverted = 0;
259 
260                                         /* EXT_framebuffer_sRGB */
261                                         c->sRGBCapable = GL_FALSE;
262 
263                                         l = c;
264                                         c = c->next;
265                                     }
266                                 }
267                             }
268                         }
269                     }
270                 }
271             }
272         }
273     }
274 
275     free(c);
276     l->next = NULL;
277 
278     freeGlCapabilities(&caps);
279     return visualConfigs;
280 }
281