Bring in DRI2 changes
[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 derived 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 mode->samples = config->multiSampleSize;
188 mode->sampleBuffers = config->nMultiSampleBuffers;
189 /* mode->visualSelectGroup = config->visualSelectGroup; ? */
190
191 mode->swapMethod = GLX_SWAP_UNDEFINED_OML;
192
193 mode->bindToTextureRgb = (mode->rgbMode) ? GL_TRUE : GL_FALSE;
194 mode->bindToTextureRgba = (mode->rgbMode && mode->alphaBits) ?
195 GL_TRUE : GL_FALSE;
196 mode->bindToMipmapTexture = mode->rgbMode ? GL_TRUE : GL_FALSE;
197 mode->bindToTextureTargets = mode->rgbMode ?
198 GLX_TEXTURE_1D_BIT_EXT | GLX_TEXTURE_2D_BIT_EXT |
199 GLX_TEXTURE_RECTANGLE_BIT_EXT : 0;
200 mode->yInverted = GL_FALSE;
201 }
202
203
204 /**
205 * Get data from a GL context mode.
206 *
207 * \param mode GL context mode whose data is to be returned.
208 * \param attribute Attribute of \c mode that is to be returned.
209 * \param value_return Location to store the data member of \c mode.
210 * \return If \c attribute is a valid attribute of \c mode, zero is
211 * returned. Otherwise \c GLX_BAD_ATTRIBUTE is returned.
212 */
213 int
214 _gl_get_context_mode_data(const __GLcontextModes *mode, int attribute,
215 int *value_return)
216 {
217 switch (attribute) {
218 case GLX_USE_GL:
219 *value_return = GL_TRUE;
220 return 0;
221 case GLX_BUFFER_SIZE:
222 *value_return = mode->rgbBits;
223 return 0;
224 case GLX_RGBA:
225 *value_return = mode->rgbMode;
226 return 0;
227 case GLX_RED_SIZE:
228 *value_return = mode->redBits;
229 return 0;
230 case GLX_GREEN_SIZE:
231 *value_return = mode->greenBits;
232 return 0;
233 case GLX_BLUE_SIZE:
234 *value_return = mode->blueBits;
235 return 0;
236 case GLX_ALPHA_SIZE:
237 *value_return = mode->alphaBits;
238 return 0;
239 case GLX_DOUBLEBUFFER:
240 *value_return = mode->doubleBufferMode;
241 return 0;
242 case GLX_STEREO:
243 *value_return = mode->stereoMode;
244 return 0;
245 case GLX_AUX_BUFFERS:
246 *value_return = mode->numAuxBuffers;
247 return 0;
248 case GLX_DEPTH_SIZE:
249 *value_return = mode->depthBits;
250 return 0;
251 case GLX_STENCIL_SIZE:
252 *value_return = mode->stencilBits;
253 return 0;
254 case GLX_ACCUM_RED_SIZE:
255 *value_return = mode->accumRedBits;
256 return 0;
257 case GLX_ACCUM_GREEN_SIZE:
258 *value_return = mode->accumGreenBits;
259 return 0;
260 case GLX_ACCUM_BLUE_SIZE:
261 *value_return = mode->accumBlueBits;
262 return 0;
263 case GLX_ACCUM_ALPHA_SIZE:
264 *value_return = mode->accumAlphaBits;
265 return 0;
266 case GLX_LEVEL:
267 *value_return = mode->level;
268 return 0;
269 case GLX_TRANSPARENT_TYPE_EXT:
270 *value_return = mode->transparentPixel;
271 return 0;
272 case GLX_TRANSPARENT_RED_VALUE:
273 *value_return = mode->transparentRed;
274 return 0;
275 case GLX_TRANSPARENT_GREEN_VALUE:
276 *value_return = mode->transparentGreen;
277 return 0;
278 case GLX_TRANSPARENT_BLUE_VALUE:
279 *value_return = mode->transparentBlue;
280 return 0;
281 case GLX_TRANSPARENT_ALPHA_VALUE:
282 *value_return = mode->transparentAlpha;
283 return 0;
284 case GLX_TRANSPARENT_INDEX_VALUE:
285 *value_return = mode->transparentIndex;
286 return 0;
287 case GLX_X_VISUAL_TYPE:
288 *value_return = mode->visualType;
289 return 0;
290 case GLX_CONFIG_CAVEAT:
291 *value_return = mode->visualRating;
292 return 0;
293 case GLX_VISUAL_ID:
294 *value_return = mode->visualID;
295 return 0;
296 case GLX_DRAWABLE_TYPE:
297 *value_return = mode->drawableType;
298 return 0;
299 case GLX_RENDER_TYPE:
300 *value_return = mode->renderType;
301 return 0;
302 case GLX_X_RENDERABLE:
303 *value_return = mode->xRenderable;
304 return 0;
305 case GLX_FBCONFIG_ID:
306 *value_return = mode->fbconfigID;
307 return 0;
308 case GLX_MAX_PBUFFER_WIDTH:
309 *value_return = mode->maxPbufferWidth;
310 return 0;
311 case GLX_MAX_PBUFFER_HEIGHT:
312 *value_return = mode->maxPbufferHeight;
313 return 0;
314 case GLX_MAX_PBUFFER_PIXELS:
315 *value_return = mode->maxPbufferPixels;
316 return 0;
317 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
318 *value_return = mode->optimalPbufferWidth;
319 return 0;
320 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
321 *value_return = mode->optimalPbufferHeight;
322 return 0;
323 case GLX_SWAP_METHOD_OML:
324 *value_return = mode->swapMethod;
325 return 0;
326 case GLX_SAMPLE_BUFFERS_SGIS:
327 *value_return = mode->sampleBuffers;
328 return 0;
329 case GLX_SAMPLES_SGIS:
330 *value_return = mode->samples;
331 return 0;
332 case GLX_BIND_TO_TEXTURE_RGB_EXT:
333 *value_return = mode->bindToTextureRgb;
334 return 0;
335 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
336 *value_return = mode->bindToTextureRgba;
337 return 0;
338 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
339 *value_return = mode->bindToMipmapTexture == GL_TRUE ? GL_TRUE :
340 GL_FALSE;
341 return 0;
342 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
343 *value_return = mode->bindToTextureTargets;
344 return 0;
345 case GLX_Y_INVERTED_EXT:
346 *value_return = mode->yInverted;
347 return 0;
348
349 /* Applications are NOT allowed to query GLX_VISUAL_SELECT_GROUP_SGIX.
350 * It is ONLY for communication between the GLX client and the GLX
351 * server.
352 */
353 case GLX_VISUAL_SELECT_GROUP_SGIX:
354 default:
355 return GLX_BAD_ATTRIBUTE;
356 }
357 }
358 #endif /* !defined(IN_MINI_GLX) */
359
360
361 /**
362 * Allocate a linked list of \c __GLcontextModes structures. The fields of
363 * each structure will be initialized to "reasonable" default values. In
364 * most cases this is the default value defined by table 3.4 of the GLX
365 * 1.3 specification. This means that most values are either initialized to
366 * zero or \c GLX_DONT_CARE (which is -1). As support for additional
367 * extensions is added, the new values will be initialized to appropriate
368 * values from the extension specification.
369 *
370 * \param count Number of structures to allocate.
371 * \param minimum_size Minimum size of a structure to allocate. This allows
372 * for differences in the version of the
373 * \c __GLcontextModes stucture used in libGL and in a
374 * DRI-based driver.
375 * \returns A pointer to the first element in a linked list of \c count
376 * stuctures on success, or \c NULL on failure.
377 *
378 * \warning Use of \c minimum_size does \b not guarantee binary compatibility.
379 * The fundamental assumption is that if the \c minimum_size
380 * specified by the driver and the size of the \c __GLcontextModes
381 * structure in libGL is the same, then the meaning of each byte in
382 * the structure is the same in both places. \b Be \b careful!
383 * Basically this means that fields have to be added in libGL and
384 * then propagated to drivers. Drivers should \b never arbitrarilly
385 * extend the \c __GLcontextModes data-structure.
386 */
387 __GLcontextModes *
388 _gl_context_modes_create( unsigned count, size_t minimum_size )
389 {
390 const size_t size = (minimum_size > sizeof( __GLcontextModes ))
391 ? minimum_size : sizeof( __GLcontextModes );
392 __GLcontextModes * base = NULL;
393 __GLcontextModes ** next;
394 unsigned i;
395
396 next = & base;
397 for ( i = 0 ; i < count ; i++ ) {
398 *next = (__GLcontextModes *) _mesa_malloc( size );
399 if ( *next == NULL ) {
400 _gl_context_modes_destroy( base );
401 base = NULL;
402 break;
403 }
404
405 (void) _mesa_memset( *next, 0, size );
406 (*next)->visualID = GLX_DONT_CARE;
407 (*next)->visualType = GLX_DONT_CARE;
408 (*next)->visualRating = GLX_NONE;
409 (*next)->transparentPixel = GLX_NONE;
410 (*next)->transparentRed = GLX_DONT_CARE;
411 (*next)->transparentGreen = GLX_DONT_CARE;
412 (*next)->transparentBlue = GLX_DONT_CARE;
413 (*next)->transparentAlpha = GLX_DONT_CARE;
414 (*next)->transparentIndex = GLX_DONT_CARE;
415 (*next)->xRenderable = GLX_DONT_CARE;
416 (*next)->fbconfigID = GLX_DONT_CARE;
417 (*next)->swapMethod = GLX_SWAP_UNDEFINED_OML;
418 (*next)->bindToTextureRgb = GLX_DONT_CARE;
419 (*next)->bindToTextureRgba = GLX_DONT_CARE;
420 (*next)->bindToMipmapTexture = GLX_DONT_CARE;
421 (*next)->bindToTextureTargets = GLX_DONT_CARE;
422 (*next)->yInverted = GLX_DONT_CARE;
423
424 next = & ((*next)->next);
425 }
426
427 return base;
428 }
429
430
431 /**
432 * Destroy a linked list of \c __GLcontextModes structures created by
433 * \c _gl_context_modes_create.
434 *
435 * \param modes Linked list of structures to be destroyed. All structres
436 * in the list will be freed.
437 */
438 void
439 _gl_context_modes_destroy( __GLcontextModes * modes )
440 {
441 while ( modes != NULL ) {
442 __GLcontextModes * const next = modes->next;
443
444 _mesa_free( modes );
445 modes = next;
446 }
447 }
448
449
450 /**
451 * Find a context mode matching a Visual ID.
452 *
453 * \param modes List list of context-mode structures to be searched.
454 * \param vid Visual ID to be found.
455 * \returns A pointer to a context-mode in \c modes if \c vid was found in
456 * the list, or \c NULL if it was not.
457 */
458
459 __GLcontextModes *
460 _gl_context_modes_find_visual(__GLcontextModes *modes, int vid)
461 {
462 __GLcontextModes *m;
463
464 for (m = modes; m != NULL; m = m->next)
465 if (m->visualID == vid)
466 return m;
467
468 return NULL;
469 }
470
471 __GLcontextModes *
472 _gl_context_modes_find_fbconfig(__GLcontextModes *modes, int fbid)
473 {
474 __GLcontextModes *m;
475
476 for (m = modes; m != NULL; m = m->next)
477 if (m->fbconfigID == fbid)
478 return m;
479
480 return NULL;
481 }
482
483 /**
484 * Determine if two context-modes are the same. This is intended to be used
485 * by libGL implementations to compare to sets of driver generated FBconfigs.
486 *
487 * \param a Context-mode to be compared.
488 * \param b Context-mode to be compared.
489 * \returns \c GL_TRUE if the two context-modes are the same. \c GL_FALSE is
490 * returned otherwise.
491 */
492 GLboolean
493 _gl_context_modes_are_same( const __GLcontextModes * a,
494 const __GLcontextModes * b )
495 {
496 return( (a->rgbMode == b->rgbMode) &&
497 (a->floatMode == b->floatMode) &&
498 (a->colorIndexMode == b->colorIndexMode) &&
499 (a->doubleBufferMode == b->doubleBufferMode) &&
500 (a->stereoMode == b->stereoMode) &&
501 (a->redBits == b->redBits) &&
502 (a->greenBits == b->greenBits) &&
503 (a->blueBits == b->blueBits) &&
504 (a->alphaBits == b->alphaBits) &&
505 #if 0 /* For some reason these don't get set on the client-side in libGL. */
506 (a->redMask == b->redMask) &&
507 (a->greenMask == b->greenMask) &&
508 (a->blueMask == b->blueMask) &&
509 (a->alphaMask == b->alphaMask) &&
510 #endif
511 (a->rgbBits == b->rgbBits) &&
512 (a->indexBits == b->indexBits) &&
513 (a->accumRedBits == b->accumRedBits) &&
514 (a->accumGreenBits == b->accumGreenBits) &&
515 (a->accumBlueBits == b->accumBlueBits) &&
516 (a->accumAlphaBits == b->accumAlphaBits) &&
517 (a->depthBits == b->depthBits) &&
518 (a->stencilBits == b->stencilBits) &&
519 (a->numAuxBuffers == b->numAuxBuffers) &&
520 (a->level == b->level) &&
521 (a->pixmapMode == b->pixmapMode) &&
522 (a->visualRating == b->visualRating) &&
523
524 (a->transparentPixel == b->transparentPixel) &&
525
526 ((a->transparentPixel != GLX_TRANSPARENT_RGB) ||
527 ((a->transparentRed == b->transparentRed) &&
528 (a->transparentGreen == b->transparentGreen) &&
529 (a->transparentBlue == b->transparentBlue) &&
530 (a->transparentAlpha == b->transparentAlpha))) &&
531
532 ((a->transparentPixel != GLX_TRANSPARENT_INDEX) ||
533 (a->transparentIndex == b->transparentIndex)) &&
534
535 (a->sampleBuffers == b->sampleBuffers) &&
536 (a->samples == b->samples) &&
537 ((a->drawableType & b->drawableType) != 0) &&
538 (a->renderType == b->renderType) &&
539 (a->maxPbufferWidth == b->maxPbufferWidth) &&
540 (a->maxPbufferHeight == b->maxPbufferHeight) &&
541 (a->maxPbufferPixels == b->maxPbufferPixels) &&
542 (a->optimalPbufferWidth == b->optimalPbufferWidth) &&
543 (a->optimalPbufferHeight == b->optimalPbufferHeight) &&
544 (a->swapMethod == b->swapMethod) &&
545 (a->bindToTextureRgb == b->bindToTextureRgb) &&
546 (a->bindToTextureRgba == b->bindToTextureRgba) &&
547 (a->bindToMipmapTexture == b->bindToMipmapTexture) &&
548 (a->bindToTextureTargets == b->bindToTextureTargets) &&
549 (a->yInverted == b->yInverted) );
550 }