libGL: Consolidate DRI initialization in dri_glx.c
[mesa.git] / src / glx / x11 / glcontextmodes.c
1 /*
2 * (C) Copyright IBM Corporation 2003
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file glcontextmodes.c
27 * Utility routines for working with \c __GLcontextModes structures. At
28 * some point most or all of these functions will be moved to the Mesa
29 * code base.
30 *
31 * \author Ian Romanick <idr@us.ibm.com>
32 */
33
34 #if defined(IN_MINI_GLX)
35 #include <GL/gl.h>
36 #else
37 #if defined(HAVE_DIX_CONFIG_H)
38 # include <dix-config.h>
39 #endif
40 #include <X11/X.h>
41 #include <GL/glx.h>
42 #include "GL/glxint.h"
43 #endif
44
45 /* Memory macros */
46 #if defined(IN_MINI_GLX)
47 # include <stdlib.h>
48 # include <string.h>
49 # define _mesa_malloc(b) malloc(b)
50 # define _mesa_free(m) free(m)
51 # define _mesa_memset memset
52 #else
53 # ifdef XFree86Server
54 # include <os.h>
55 # include <string.h>
56 # define _mesa_malloc(b) xalloc(b)
57 # define _mesa_free(m) xfree(m)
58 # define _mesa_memset memset
59 # else
60 # include <X11/Xlibint.h>
61 # define _mesa_memset memset
62 # define _mesa_malloc(b) Xmalloc(b)
63 # define _mesa_free(m) Xfree(m)
64 # endif /* XFree86Server */
65 #endif /* !defined(IN_MINI_GLX) */
66
67 #include "glcontextmodes.h"
68
69 #if !defined(IN_MINI_GLX)
70 #define NUM_VISUAL_TYPES 6
71
72 /**
73 * Convert an X visual type to a GLX visual type.
74 *
75 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
76 * to be converted.
77 * \return If \c visualType is a valid X visual type, a GLX visual type will
78 * be returned. Otherwise \c GLX_NONE will be returned.
79 */
80 GLint
81 _gl_convert_from_x_visual_type( int visualType )
82 {
83 static const int glx_visual_types[ NUM_VISUAL_TYPES ] = {
84 GLX_STATIC_GRAY, GLX_GRAY_SCALE,
85 GLX_STATIC_COLOR, GLX_PSEUDO_COLOR,
86 GLX_TRUE_COLOR, GLX_DIRECT_COLOR
87 };
88
89 return ( (unsigned) visualType < NUM_VISUAL_TYPES )
90 ? glx_visual_types[ visualType ] : GLX_NONE;
91 }
92
93
94 /**
95 * Convert a GLX visual type to an X visual type.
96 *
97 * \param visualType GLX visual type (i.e., \c GLX_TRUE_COLOR,
98 * \c GLX_STATIC_GRAY, etc.) to be converted.
99 * \return If \c visualType is a valid GLX visual type, an X visual type will
100 * be returned. Otherwise -1 will be returned.
101 */
102 GLint
103 _gl_convert_to_x_visual_type( int visualType )
104 {
105 static const int x_visual_types[ NUM_VISUAL_TYPES ] = {
106 TrueColor, DirectColor,
107 PseudoColor, StaticColor,
108 GrayScale, StaticGray
109 };
110
111 return ( (unsigned) (visualType - GLX_TRUE_COLOR) < NUM_VISUAL_TYPES )
112 ? x_visual_types[ visualType - GLX_TRUE_COLOR ] : -1;
113 }
114
115
116 /**
117 * Copy a GLX visual config structure to a GL context mode structure. All
118 * of the fields in \c config are copied to \c mode. Additional fields in
119 * \c mode that can be derrived from the fields of \c config (i.e.,
120 * \c haveDepthBuffer) are also filled in. The remaining fields in \c mode
121 * that cannot be derrived are set to default values.
122 *
123 * \param mode Destination GL context mode.
124 * \param config Source GLX visual config.
125 *
126 * \note
127 * The \c fbconfigID and \c visualID fields of the \c __GLcontextModes
128 * structure will be set to the \c vid of the \c __GLXvisualConfig structure.
129 */
130 void
131 _gl_copy_visual_to_context_mode( __GLcontextModes * mode,
132 const __GLXvisualConfig * config )
133 {
134 __GLcontextModes * const next = mode->next;
135
136 (void) _mesa_memset( mode, 0, sizeof( __GLcontextModes ) );
137 mode->next = next;
138
139 mode->visualID = config->vid;
140 mode->visualType = _gl_convert_from_x_visual_type( config->class );
141 mode->xRenderable = GL_TRUE;
142 mode->fbconfigID = config->vid;
143 mode->drawableType = GLX_WINDOW_BIT | GLX_PIXMAP_BIT;
144
145 mode->rgbMode = (config->rgba != 0);
146 mode->renderType = (mode->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
147
148 mode->colorIndexMode = !(mode->rgbMode);
149 mode->doubleBufferMode = (config->doubleBuffer != 0);
150 mode->stereoMode = (config->stereo != 0);
151
152 mode->haveAccumBuffer = ((config->accumRedSize +
153 config->accumGreenSize +
154 config->accumBlueSize +
155 config->accumAlphaSize) > 0);
156 mode->haveDepthBuffer = (config->depthSize > 0);
157 mode->haveStencilBuffer = (config->stencilSize > 0);
158
159 mode->redBits = config->redSize;
160 mode->greenBits = config->greenSize;
161 mode->blueBits = config->blueSize;
162 mode->alphaBits = config->alphaSize;
163 mode->redMask = config->redMask;
164 mode->greenMask = config->greenMask;
165 mode->blueMask = config->blueMask;
166 mode->alphaMask = config->alphaMask;
167 mode->rgbBits = mode->rgbMode ? config->bufferSize : 0;
168 mode->indexBits = mode->colorIndexMode ? config->bufferSize : 0;
169
170 mode->accumRedBits = config->accumRedSize;
171 mode->accumGreenBits = config->accumGreenSize;
172 mode->accumBlueBits = config->accumBlueSize;
173 mode->accumAlphaBits = config->accumAlphaSize;
174 mode->depthBits = config->depthSize;
175 mode->stencilBits = config->stencilSize;
176
177 mode->numAuxBuffers = config->auxBuffers;
178 mode->level = config->level;
179
180 mode->visualRating = config->visualRating;
181 mode->transparentPixel = config->transparentPixel;
182 mode->transparentRed = config->transparentRed;
183 mode->transparentGreen = config->transparentGreen;
184 mode->transparentBlue = config->transparentBlue;
185 mode->transparentAlpha = config->transparentAlpha;
186 mode->transparentIndex = config->transparentIndex;
187
188 mode->swapMethod = GLX_SWAP_UNDEFINED_OML;
189
190 mode->bindToTextureRgb = (mode->rgbMode) ? GL_TRUE : GL_FALSE;
191 mode->bindToTextureRgba = (mode->rgbMode && mode->alphaBits) ?
192 GL_TRUE : GL_FALSE;
193 mode->bindToMipmapTexture = mode->rgbMode ? GL_TRUE : GL_FALSE;
194 mode->bindToTextureTargets = mode->rgbMode ?
195 GLX_TEXTURE_1D_BIT_EXT | GLX_TEXTURE_2D_BIT_EXT |
196 GLX_TEXTURE_RECTANGLE_BIT_EXT : 0;
197 mode->yInverted = GL_FALSE;
198 }
199
200
201 /**
202 * Get data from a GL context mode.
203 *
204 * \param mode GL context mode whose data is to be returned.
205 * \param attribute Attribute of \c mode that is to be returned.
206 * \param value_return Location to store the data member of \c mode.
207 * \return If \c attribute is a valid attribute of \c mode, zero is
208 * returned. Otherwise \c GLX_BAD_ATTRIBUTE is returned.
209 */
210 int
211 _gl_get_context_mode_data(const __GLcontextModes *mode, int attribute,
212 int *value_return)
213 {
214 switch (attribute) {
215 case GLX_USE_GL:
216 *value_return = GL_TRUE;
217 return 0;
218 case GLX_BUFFER_SIZE:
219 *value_return = mode->rgbBits;
220 return 0;
221 case GLX_RGBA:
222 *value_return = mode->rgbMode;
223 return 0;
224 case GLX_RED_SIZE:
225 *value_return = mode->redBits;
226 return 0;
227 case GLX_GREEN_SIZE:
228 *value_return = mode->greenBits;
229 return 0;
230 case GLX_BLUE_SIZE:
231 *value_return = mode->blueBits;
232 return 0;
233 case GLX_ALPHA_SIZE:
234 *value_return = mode->alphaBits;
235 return 0;
236 case GLX_DOUBLEBUFFER:
237 *value_return = mode->doubleBufferMode;
238 return 0;
239 case GLX_STEREO:
240 *value_return = mode->stereoMode;
241 return 0;
242 case GLX_AUX_BUFFERS:
243 *value_return = mode->numAuxBuffers;
244 return 0;
245 case GLX_DEPTH_SIZE:
246 *value_return = mode->depthBits;
247 return 0;
248 case GLX_STENCIL_SIZE:
249 *value_return = mode->stencilBits;
250 return 0;
251 case GLX_ACCUM_RED_SIZE:
252 *value_return = mode->accumRedBits;
253 return 0;
254 case GLX_ACCUM_GREEN_SIZE:
255 *value_return = mode->accumGreenBits;
256 return 0;
257 case GLX_ACCUM_BLUE_SIZE:
258 *value_return = mode->accumBlueBits;
259 return 0;
260 case GLX_ACCUM_ALPHA_SIZE:
261 *value_return = mode->accumAlphaBits;
262 return 0;
263 case GLX_LEVEL:
264 *value_return = mode->level;
265 return 0;
266 case GLX_TRANSPARENT_TYPE_EXT:
267 *value_return = mode->transparentPixel;
268 return 0;
269 case GLX_TRANSPARENT_RED_VALUE:
270 *value_return = mode->transparentRed;
271 return 0;
272 case GLX_TRANSPARENT_GREEN_VALUE:
273 *value_return = mode->transparentGreen;
274 return 0;
275 case GLX_TRANSPARENT_BLUE_VALUE:
276 *value_return = mode->transparentBlue;
277 return 0;
278 case GLX_TRANSPARENT_ALPHA_VALUE:
279 *value_return = mode->transparentAlpha;
280 return 0;
281 case GLX_TRANSPARENT_INDEX_VALUE:
282 *value_return = mode->transparentIndex;
283 return 0;
284 case GLX_X_VISUAL_TYPE:
285 *value_return = mode->visualType;
286 return 0;
287 case GLX_CONFIG_CAVEAT:
288 *value_return = mode->visualRating;
289 return 0;
290 case GLX_VISUAL_ID:
291 *value_return = mode->visualID;
292 return 0;
293 case GLX_DRAWABLE_TYPE:
294 *value_return = mode->drawableType;
295 return 0;
296 case GLX_RENDER_TYPE:
297 *value_return = mode->renderType;
298 return 0;
299 case GLX_X_RENDERABLE:
300 *value_return = mode->xRenderable;
301 return 0;
302 case GLX_FBCONFIG_ID:
303 *value_return = mode->fbconfigID;
304 return 0;
305 case GLX_MAX_PBUFFER_WIDTH:
306 *value_return = mode->maxPbufferWidth;
307 return 0;
308 case GLX_MAX_PBUFFER_HEIGHT:
309 *value_return = mode->maxPbufferHeight;
310 return 0;
311 case GLX_MAX_PBUFFER_PIXELS:
312 *value_return = mode->maxPbufferPixels;
313 return 0;
314 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
315 *value_return = mode->optimalPbufferWidth;
316 return 0;
317 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
318 *value_return = mode->optimalPbufferHeight;
319 return 0;
320 case GLX_SWAP_METHOD_OML:
321 *value_return = mode->swapMethod;
322 return 0;
323 case GLX_SAMPLE_BUFFERS_SGIS:
324 *value_return = mode->sampleBuffers;
325 return 0;
326 case GLX_SAMPLES_SGIS:
327 *value_return = mode->samples;
328 return 0;
329 case GLX_BIND_TO_TEXTURE_RGB_EXT:
330 *value_return = mode->bindToTextureRgb;
331 return 0;
332 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
333 *value_return = mode->bindToTextureRgba;
334 return 0;
335 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
336 *value_return = mode->bindToMipmapTexture == GL_TRUE ? GL_TRUE :
337 GL_FALSE;
338 return 0;
339 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
340 *value_return = mode->bindToTextureTargets;
341 return 0;
342 case GLX_Y_INVERTED_EXT:
343 *value_return = mode->yInverted;
344 return 0;
345
346 /* Applications are NOT allowed to query GLX_VISUAL_SELECT_GROUP_SGIX.
347 * It is ONLY for communication between the GLX client and the GLX
348 * server.
349 */
350 case GLX_VISUAL_SELECT_GROUP_SGIX:
351 default:
352 return GLX_BAD_ATTRIBUTE;
353 }
354 }
355 #endif /* !defined(IN_MINI_GLX) */
356
357
358 /**
359 * Allocate a linked list of \c __GLcontextModes structures. The fields of
360 * each structure will be initialized to "reasonable" default values. In
361 * most cases this is the default value defined by table 3.4 of the GLX
362 * 1.3 specification. This means that most values are either initialized to
363 * zero or \c GLX_DONT_CARE (which is -1). As support for additional
364 * extensions is added, the new values will be initialized to appropriate
365 * values from the extension specification.
366 *
367 * \param count Number of structures to allocate.
368 * \param minimum_size Minimum size of a structure to allocate. This allows
369 * for differences in the version of the
370 * \c __GLcontextModes stucture used in libGL and in a
371 * DRI-based driver.
372 * \returns A pointer to the first element in a linked list of \c count
373 * stuctures on success, or \c NULL on failure.
374 *
375 * \warning Use of \c minimum_size does \b not guarantee binary compatibility.
376 * The fundamental assumption is that if the \c minimum_size
377 * specified by the driver and the size of the \c __GLcontextModes
378 * structure in libGL is the same, then the meaning of each byte in
379 * the structure is the same in both places. \b Be \b careful!
380 * Basically this means that fields have to be added in libGL and
381 * then propagated to drivers. Drivers should \b never arbitrarilly
382 * extend the \c __GLcontextModes data-structure.
383 */
384 __GLcontextModes *
385 _gl_context_modes_create( unsigned count, size_t minimum_size )
386 {
387 const size_t size = (minimum_size > sizeof( __GLcontextModes ))
388 ? minimum_size : sizeof( __GLcontextModes );
389 __GLcontextModes * base = NULL;
390 __GLcontextModes ** next;
391 unsigned i;
392
393 next = & base;
394 for ( i = 0 ; i < count ; i++ ) {
395 *next = (__GLcontextModes *) _mesa_malloc( size );
396 if ( *next == NULL ) {
397 _gl_context_modes_destroy( base );
398 base = NULL;
399 break;
400 }
401
402 (void) _mesa_memset( *next, 0, size );
403 (*next)->visualID = GLX_DONT_CARE;
404 (*next)->visualType = GLX_DONT_CARE;
405 (*next)->visualRating = GLX_NONE;
406 (*next)->transparentPixel = GLX_NONE;
407 (*next)->transparentRed = GLX_DONT_CARE;
408 (*next)->transparentGreen = GLX_DONT_CARE;
409 (*next)->transparentBlue = GLX_DONT_CARE;
410 (*next)->transparentAlpha = GLX_DONT_CARE;
411 (*next)->transparentIndex = GLX_DONT_CARE;
412 (*next)->xRenderable = GLX_DONT_CARE;
413 (*next)->fbconfigID = GLX_DONT_CARE;
414 (*next)->swapMethod = GLX_SWAP_UNDEFINED_OML;
415 (*next)->bindToTextureRgb = GLX_DONT_CARE;
416 (*next)->bindToTextureRgba = GLX_DONT_CARE;
417 (*next)->bindToMipmapTexture = GLX_DONT_CARE;
418 (*next)->bindToTextureTargets = 0;
419 (*next)->yInverted = GLX_DONT_CARE;
420
421 next = & ((*next)->next);
422 }
423
424 return base;
425 }
426
427
428 /**
429 * Destroy a linked list of \c __GLcontextModes structures created by
430 * \c _gl_context_modes_create.
431 *
432 * \param modes Linked list of structures to be destroyed. All structres
433 * in the list will be freed.
434 */
435 void
436 _gl_context_modes_destroy( __GLcontextModes * modes )
437 {
438 while ( modes != NULL ) {
439 __GLcontextModes * const next = modes->next;
440
441 _mesa_free( modes );
442 modes = next;
443 }
444 }
445
446
447 /**
448 * Find a context mode matching a Visual ID.
449 *
450 * \param modes List list of context-mode structures to be searched.
451 * \param vid Visual ID to be found.
452 * \returns A pointer to a context-mode in \c modes if \c vid was found in
453 * the list, or \c NULL if it was not.
454 */
455
456 __GLcontextModes *
457 _gl_context_modes_find_visual(__GLcontextModes *modes, int vid)
458 {
459 __GLcontextModes *m;
460
461 for (m = modes; m != NULL; m = m->next)
462 if (m->visualID == vid)
463 return m;
464
465 return NULL;
466 }
467
468 __GLcontextModes *
469 _gl_context_modes_find_fbconfig(__GLcontextModes *modes, int fbid)
470 {
471 __GLcontextModes *m;
472
473 for (m = modes; m != NULL; m = m->next)
474 if (m->fbconfigID == fbid)
475 return m;
476
477 return NULL;
478 }
479
480 /**
481 * Determine if two context-modes are the same. This is intended to be used
482 * by libGL implementations to compare to sets of driver generated FBconfigs.
483 *
484 * \param a Context-mode to be compared.
485 * \param b Context-mode to be compared.
486 * \returns \c GL_TRUE if the two context-modes are the same. \c GL_FALSE is
487 * returned otherwise.
488 */
489 GLboolean
490 _gl_context_modes_are_same( const __GLcontextModes * a,
491 const __GLcontextModes * b )
492 {
493 return( (a->rgbMode == b->rgbMode) &&
494 (a->floatMode == b->floatMode) &&
495 (a->colorIndexMode == b->colorIndexMode) &&
496 (a->doubleBufferMode == b->doubleBufferMode) &&
497 (a->stereoMode == b->stereoMode) &&
498 (a->redBits == b->redBits) &&
499 (a->greenBits == b->greenBits) &&
500 (a->blueBits == b->blueBits) &&
501 (a->alphaBits == b->alphaBits) &&
502 #if 0 /* For some reason these don't get set on the client-side in libGL. */
503 (a->redMask == b->redMask) &&
504 (a->greenMask == b->greenMask) &&
505 (a->blueMask == b->blueMask) &&
506 (a->alphaMask == b->alphaMask) &&
507 #endif
508 (a->rgbBits == b->rgbBits) &&
509 (a->indexBits == b->indexBits) &&
510 (a->accumRedBits == b->accumRedBits) &&
511 (a->accumGreenBits == b->accumGreenBits) &&
512 (a->accumBlueBits == b->accumBlueBits) &&
513 (a->accumAlphaBits == b->accumAlphaBits) &&
514 (a->depthBits == b->depthBits) &&
515 (a->stencilBits == b->stencilBits) &&
516 (a->numAuxBuffers == b->numAuxBuffers) &&
517 (a->level == b->level) &&
518 (a->pixmapMode == b->pixmapMode) &&
519 (a->visualRating == b->visualRating) &&
520
521 (a->transparentPixel == b->transparentPixel) &&
522
523 ((a->transparentPixel != GLX_TRANSPARENT_RGB) ||
524 ((a->transparentRed == b->transparentRed) &&
525 (a->transparentGreen == b->transparentGreen) &&
526 (a->transparentBlue == b->transparentBlue) &&
527 (a->transparentAlpha == b->transparentAlpha))) &&
528
529 ((a->transparentPixel != GLX_TRANSPARENT_INDEX) ||
530 (a->transparentIndex == b->transparentIndex)) &&
531
532 (a->sampleBuffers == b->sampleBuffers) &&
533 (a->samples == b->samples) &&
534 ((a->drawableType & b->drawableType) != 0) &&
535 (a->renderType == b->renderType) &&
536 (a->maxPbufferWidth == b->maxPbufferWidth) &&
537 (a->maxPbufferHeight == b->maxPbufferHeight) &&
538 (a->maxPbufferPixels == b->maxPbufferPixels) &&
539 (a->optimalPbufferWidth == b->optimalPbufferWidth) &&
540 (a->optimalPbufferHeight == b->optimalPbufferHeight) &&
541 (a->swapMethod == b->swapMethod) &&
542 (a->bindToTextureRgb == b->bindToTextureRgb) &&
543 (a->bindToTextureRgba == b->bindToTextureRgba) &&
544 (a->bindToMipmapTexture == b->bindToMipmapTexture) &&
545 (a->bindToTextureTargets == b->bindToTextureTargets) &&
546 (a->yInverted == b->yInverted) );
547 }