2 * (C) Copyright IBM Corporation 2003
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:
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
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.
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
31 * \author Ian Romanick <idr@us.ibm.com>
36 #include "GL/glxint.h"
39 # include "GL/glx_ansic.h"
40 extern void * __glXMalloc( size_t size
);
41 extern void __glXFree( void * ptr
);
42 # define Xmalloc __glXMalloc
43 # define Xfree __glXFree
45 # include <X11/Xlibint.h>
46 # define __glXMemset memset
47 #endif /* XFree86Server */
49 #include "glcontextmodes.h"
51 #ifndef DRI_NEW_INTERFACE_ONLY
52 #define NUM_VISUAL_TYPES 6
55 * Convert an X visual type to a GLX visual type.
57 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
59 * \return If \c visualType is a valid X visual type, a GLX visual type will
60 * be returned. Otherwise \c GLX_NONE will be returned.
63 _gl_convert_from_x_visual_type( int visualType
)
65 static const int glx_visual_types
[ NUM_VISUAL_TYPES
] = {
66 GLX_STATIC_GRAY
, GLX_GRAY_SCALE
,
67 GLX_STATIC_COLOR
, GLX_PSEUDO_COLOR
,
68 GLX_TRUE_COLOR
, GLX_DIRECT_COLOR
71 return ( (unsigned) visualType
< NUM_VISUAL_TYPES
)
72 ? glx_visual_types
[ visualType
] : GLX_NONE
;
77 * Convert a GLX visual type to an X visual type.
79 * \param visualType GLX visual type (i.e., \c GLX_TRUE_COLOR,
80 * \c GLX_STATIC_GRAY, etc.) to be converted.
81 * \return If \c visualType is a valid GLX visual type, an X visual type will
82 * be returned. Otherwise -1 will be returned.
85 _gl_convert_to_x_visual_type( int visualType
)
87 static const int x_visual_types
[ NUM_VISUAL_TYPES
] = {
88 TrueColor
, DirectColor
,
89 PseudoColor
, StaticColor
,
93 return ( (unsigned) (visualType
- GLX_TRUE_COLOR
) <= NUM_VISUAL_TYPES
)
94 ? x_visual_types
[ visualType
- GLX_TRUE_COLOR
] : -1;
99 * Copy a GLX visual config structure to a GL context mode structure. All
100 * of the fields in \c config are copied to \c mode. Additional fields in
101 * \c mode that can be derrived from the fields of \c config (i.e.,
102 * \c haveDepthBuffer) are also filled in. The remaining fields in \c mode
103 * that cannot be derrived are set to default values.
105 * \param mode Destination GL context mode.
106 * \param config Source GLX visual config.
109 * The \c fbconfigID and \c visualID fields of the \c __GLcontextModes
110 * structure will be set to the \c vid of the \c __GLXvisualConfig structure.
113 _gl_copy_visual_to_context_mode( __GLcontextModes
* mode
,
114 const __GLXvisualConfig
* config
)
116 __GLcontextModes
* const next
= mode
->next
;
118 (void) __glXMemset( mode
, 0, sizeof( __GLcontextModes
) );
121 mode
->visualID
= config
->vid
;
122 mode
->visualType
= _gl_convert_from_x_visual_type( config
->class );
123 mode
->xRenderable
= GL_TRUE
;
124 mode
->fbconfigID
= config
->vid
;
125 mode
->drawableType
= GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
;
127 mode
->rgbMode
= (config
->rgba
!= 0);
128 mode
->renderType
= (mode
->rgbMode
) ? GLX_RGBA_BIT
: GLX_COLOR_INDEX_BIT
;
130 mode
->colorIndexMode
= !(mode
->rgbMode
);
131 mode
->doubleBufferMode
= (config
->doubleBuffer
!= 0);
132 mode
->stereoMode
= (config
->stereo
!= 0);
134 mode
->haveAccumBuffer
= ((config
->accumRedSize
+
135 config
->accumGreenSize
+
136 config
->accumBlueSize
+
137 config
->accumAlphaSize
) > 0);
138 mode
->haveDepthBuffer
= (config
->depthSize
> 0);
139 mode
->haveStencilBuffer
= (config
->stencilSize
> 0);
141 mode
->redBits
= config
->redSize
;
142 mode
->greenBits
= config
->greenSize
;
143 mode
->blueBits
= config
->blueSize
;
144 mode
->alphaBits
= config
->alphaSize
;
145 mode
->redMask
= config
->redMask
;
146 mode
->greenMask
= config
->greenMask
;
147 mode
->blueMask
= config
->blueMask
;
148 mode
->alphaMask
= config
->alphaMask
;
149 mode
->rgbBits
= config
->bufferSize
;
150 mode
->indexBits
= config
->bufferSize
;
152 mode
->accumRedBits
= config
->accumRedSize
;
153 mode
->accumGreenBits
= config
->accumGreenSize
;
154 mode
->accumBlueBits
= config
->accumBlueSize
;
155 mode
->accumAlphaBits
= config
->accumAlphaSize
;
156 mode
->depthBits
= config
->depthSize
;
157 mode
->stencilBits
= config
->stencilSize
;
159 mode
->numAuxBuffers
= config
->auxBuffers
;
160 mode
->level
= config
->level
;
162 mode
->visualRating
= config
->visualRating
;
163 mode
->transparentPixel
= config
->transparentPixel
;
164 mode
->transparentRed
= config
->transparentRed
;
165 mode
->transparentGreen
= config
->transparentGreen
;
166 mode
->transparentBlue
= config
->transparentBlue
;
167 mode
->transparentAlpha
= config
->transparentAlpha
;
168 mode
->transparentIndex
= config
->transparentIndex
;
170 mode
->swapMethod
= GLX_SWAP_UNDEFINED_OML
;
172 #endif /* DRI_NEW_INTERFACE_ONLY */
176 * Get data from a GL context mode.
178 * \param mode GL context mode whose data is to be returned.
179 * \param attribute Attribute of \c mode that is to be returned.
180 * \param value_return Location to store the data member of \c mode.
181 * \return If \c attribute is a valid attribute of \c mode, zero is
182 * returned. Otherwise \c GLX_BAD_ATTRIBUTE is returned.
185 _gl_get_context_mode_data(const __GLcontextModes
*mode
, int attribute
,
190 *value_return
= GL_TRUE
;
192 case GLX_BUFFER_SIZE
:
193 *value_return
= mode
->rgbBits
;
196 *value_return
= mode
->rgbMode
;
199 *value_return
= mode
->redBits
;
202 *value_return
= mode
->greenBits
;
205 *value_return
= mode
->blueBits
;
208 *value_return
= mode
->alphaBits
;
210 case GLX_DOUBLEBUFFER
:
211 *value_return
= mode
->doubleBufferMode
;
214 *value_return
= mode
->stereoMode
;
216 case GLX_AUX_BUFFERS
:
217 *value_return
= mode
->numAuxBuffers
;
220 *value_return
= mode
->depthBits
;
222 case GLX_STENCIL_SIZE
:
223 *value_return
= mode
->stencilBits
;
225 case GLX_ACCUM_RED_SIZE
:
226 *value_return
= mode
->accumRedBits
;
228 case GLX_ACCUM_GREEN_SIZE
:
229 *value_return
= mode
->accumGreenBits
;
231 case GLX_ACCUM_BLUE_SIZE
:
232 *value_return
= mode
->accumBlueBits
;
234 case GLX_ACCUM_ALPHA_SIZE
:
235 *value_return
= mode
->accumAlphaBits
;
238 *value_return
= mode
->level
;
240 case GLX_TRANSPARENT_TYPE_EXT
:
241 *value_return
= mode
->transparentPixel
;
243 case GLX_TRANSPARENT_RED_VALUE
:
244 *value_return
= mode
->transparentRed
;
246 case GLX_TRANSPARENT_GREEN_VALUE
:
247 *value_return
= mode
->transparentGreen
;
249 case GLX_TRANSPARENT_BLUE_VALUE
:
250 *value_return
= mode
->transparentBlue
;
252 case GLX_TRANSPARENT_ALPHA_VALUE
:
253 *value_return
= mode
->transparentAlpha
;
255 case GLX_TRANSPARENT_INDEX_VALUE
:
256 *value_return
= mode
->transparentIndex
;
258 case GLX_X_VISUAL_TYPE
:
259 *value_return
= mode
->visualType
;
261 case GLX_CONFIG_CAVEAT
:
262 *value_return
= mode
->visualRating
;
265 *value_return
= mode
->visualID
;
267 case GLX_DRAWABLE_TYPE
:
268 *value_return
= mode
->drawableType
;
270 case GLX_RENDER_TYPE
:
271 *value_return
= mode
->renderType
;
273 case GLX_X_RENDERABLE
:
274 *value_return
= mode
->xRenderable
;
276 case GLX_FBCONFIG_ID
:
277 *value_return
= mode
->fbconfigID
;
279 case GLX_MAX_PBUFFER_WIDTH
:
280 *value_return
= mode
->maxPbufferWidth
;
282 case GLX_MAX_PBUFFER_HEIGHT
:
283 *value_return
= mode
->maxPbufferHeight
;
285 case GLX_MAX_PBUFFER_PIXELS
:
286 *value_return
= mode
->maxPbufferPixels
;
288 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX
:
289 *value_return
= mode
->optimalPbufferWidth
;
291 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX
:
292 *value_return
= mode
->optimalPbufferHeight
;
294 case GLX_SWAP_METHOD_OML
:
295 *value_return
= mode
->swapMethod
;
297 case GLX_SAMPLE_BUFFERS_SGIS
:
298 *value_return
= mode
->sampleBuffers
;
300 case GLX_SAMPLES_SGIS
:
301 *value_return
= mode
->samples
;
304 /* Applications are NOT allowed to query GLX_VISUAL_SELECT_GROUP_SGIX.
305 * It is ONLY for communication between the GLX client and the GLX
308 case GLX_VISUAL_SELECT_GROUP_SGIX
:
310 return GLX_BAD_ATTRIBUTE
;
316 * Allocate a linked list of \c __GLcontextModes structures. The fields of
317 * each structure will be initialized to "reasonable" default values. In
318 * most cases this is the default value defined by table 3.4 of the GLX
319 * 1.3 specification. This means that most values are either initialized to
320 * zero or \c GLX_DONT_CARE (which is -1). As support for additional
321 * extensions is added, the new values will be initialized to appropriate
322 * values from the extension specification.
324 * \param count Number of structures to allocate.
325 * \param minimum_size Minimum size of a structure to allocate. This allows
326 * for differences in the version of the
327 * \c __GLcontextModes stucture used in libGL and in a
329 * \returns A pointer to the first element in a linked list of \c count
330 * stuctures on success, or \c NULL on failure.
332 * \warning Use of \c minimum_size does \b not guarantee binary compatibility.
333 * The fundamental assumption is that if the \c minimum_size
334 * specified by the driver and the size of the \c __GLcontextModes
335 * structure in libGL is the same, then the meaning of each byte in
336 * the structure is the same in both places. \b Be \b careful!
337 * Basically this means that fields have to be added in libGL and
338 * then propagated to drivers. Drivers should \b never arbitrarilly
339 * extend the \c __GLcontextModes data-structure.
342 _gl_context_modes_create( unsigned count
, size_t minimum_size
)
344 const size_t size
= (minimum_size
> sizeof( __GLcontextModes
))
345 ? minimum_size
: sizeof( __GLcontextModes
);
346 __GLcontextModes
* base
= NULL
;
347 __GLcontextModes
** next
;
351 for ( i
= 0 ; i
< count
; i
++ ) {
352 *next
= (__GLcontextModes
*) Xmalloc( size
);
353 if ( *next
== NULL
) {
354 _gl_context_modes_destroy( base
);
359 (void) __glXMemset( *next
, 0, size
);
360 (*next
)->visualID
= GLX_DONT_CARE
;
361 (*next
)->visualType
= GLX_DONT_CARE
;
362 (*next
)->visualRating
= GLX_NONE
;
363 (*next
)->transparentPixel
= GLX_NONE
;
364 (*next
)->transparentRed
= GLX_DONT_CARE
;
365 (*next
)->transparentGreen
= GLX_DONT_CARE
;
366 (*next
)->transparentBlue
= GLX_DONT_CARE
;
367 (*next
)->transparentAlpha
= GLX_DONT_CARE
;
368 (*next
)->transparentIndex
= GLX_DONT_CARE
;
369 (*next
)->xRenderable
= GLX_DONT_CARE
;
370 (*next
)->fbconfigID
= GLX_DONT_CARE
;
371 (*next
)->swapMethod
= GLX_SWAP_UNDEFINED_OML
;
373 next
= & ((*next
)->next
);
381 * Destroy a linked list of \c __GLcontextModes structures created by
382 * \c _gl_context_modes_create.
384 * \param modes Linked list of structures to be destroyed. All structres
385 * in the list will be freed.
388 _gl_context_modes_destroy( __GLcontextModes
* modes
)
390 while ( modes
!= NULL
) {
391 __GLcontextModes
* const next
= modes
->next
;
400 * Find a context mode matching a Visual ID.
402 * \param modes List list of context-mode structures to be searched.
403 * \param vid Visual ID to be found.
404 * \returns A pointer to a context-mode in \c modes if \c vid was found in
405 * the list, or \c NULL if it was not.
409 _gl_context_modes_find_visual( __GLcontextModes
* modes
, int vid
)
411 while ( modes
!= NULL
) {
412 if ( modes
->visualID
== vid
) {
424 * Determine if two context-modes are the same. This is intended to be used
425 * by libGL implementations to compare to sets of driver generated FBconfigs.
427 * \param a Context-mode to be compared.
428 * \param b Context-mode to be compared.
429 * \returns \c GL_TRUE if the two context-modes are the same. \c GL_FALSE is
430 * returned otherwise.
433 _gl_context_modes_are_same( const __GLcontextModes
* a
,
434 const __GLcontextModes
* b
)
436 return( (a
->rgbMode
== b
->rgbMode
) &&
437 (a
->floatMode
== b
->floatMode
) &&
438 (a
->colorIndexMode
== b
->colorIndexMode
) &&
439 (a
->doubleBufferMode
== b
->doubleBufferMode
) &&
440 (a
->stereoMode
== b
->stereoMode
) &&
441 (a
->redBits
== b
->redBits
) &&
442 (a
->greenBits
== b
->greenBits
) &&
443 (a
->blueBits
== b
->blueBits
) &&
444 (a
->alphaBits
== b
->alphaBits
) &&
445 #if 0 /* For some reason these don't get set on the client-side in libGL. */
446 (a
->redMask
== b
->redMask
) &&
447 (a
->greenMask
== b
->greenMask
) &&
448 (a
->blueMask
== b
->blueMask
) &&
449 (a
->alphaMask
== b
->alphaMask
) &&
451 (a
->rgbBits
== b
->rgbBits
) &&
452 (a
->indexBits
== b
->indexBits
) &&
453 (a
->accumRedBits
== b
->accumRedBits
) &&
454 (a
->accumGreenBits
== b
->accumGreenBits
) &&
455 (a
->accumBlueBits
== b
->accumBlueBits
) &&
456 (a
->accumAlphaBits
== b
->accumAlphaBits
) &&
457 (a
->depthBits
== b
->depthBits
) &&
458 (a
->stencilBits
== b
->stencilBits
) &&
459 (a
->numAuxBuffers
== b
->numAuxBuffers
) &&
460 (a
->level
== b
->level
) &&
461 (a
->pixmapMode
== b
->pixmapMode
) &&
462 (a
->visualRating
== b
->visualRating
) &&
464 (a
->transparentPixel
== b
->transparentPixel
) &&
466 ((a
->transparentPixel
!= GLX_TRANSPARENT_RGB
) ||
467 ((a
->transparentRed
== b
->transparentRed
) &&
468 (a
->transparentGreen
== b
->transparentGreen
) &&
469 (a
->transparentBlue
== b
->transparentBlue
) &&
470 (a
->transparentAlpha
== b
->transparentAlpha
))) &&
472 ((a
->transparentPixel
!= GLX_TRANSPARENT_INDEX
) ||
473 (a
->transparentIndex
== b
->transparentIndex
)) &&
475 (a
->sampleBuffers
== b
->sampleBuffers
) &&
476 (a
->samples
== b
->samples
) &&
477 ((a
->drawableType
& b
->drawableType
) != 0) &&
478 (a
->renderType
== b
->renderType
) &&
479 (a
->maxPbufferWidth
== b
->maxPbufferWidth
) &&
480 (a
->maxPbufferHeight
== b
->maxPbufferHeight
) &&
481 (a
->maxPbufferPixels
== b
->maxPbufferPixels
) &&
482 (a
->optimalPbufferWidth
== b
->optimalPbufferWidth
) &&
483 (a
->optimalPbufferHeight
== b
->optimalPbufferHeight
) &&
484 (a
->swapMethod
== b
->swapMethod
) );