448c33d187409a4cfe350d065adb37238d511dd7
[mesa.git] / src / mesa / drivers / dri / common / 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 #include <X11/X.h>
35 #include <GL/glx.h>
36 #include "GL/glxint.h"
37
38 #ifdef XFree86Server
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
44 #else
45 # include <X11/Xlibint.h>
46 # define __glXMemset memset
47 #endif /* XFree86Server */
48
49 #include "glcontextmodes.h"
50
51 #ifndef DRI_NEW_INTERFACE_ONLY
52 #define NUM_VISUAL_TYPES 6
53
54 /**
55 * Convert an X visual type to a GLX visual type.
56 *
57 * \param visualType X visual type (i.e., \c TrueColor, \c StaticGray, etc.)
58 * to be converted.
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.
61 */
62 GLint
63 _gl_convert_from_x_visual_type( int visualType )
64 {
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
69 };
70
71 return ( (unsigned) visualType < NUM_VISUAL_TYPES )
72 ? glx_visual_types[ visualType ] : GLX_NONE;
73 }
74
75
76 /**
77 * Convert a GLX visual type to an X visual type.
78 *
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.
83 */
84 GLint
85 _gl_convert_to_x_visual_type( int visualType )
86 {
87 static const int x_visual_types[ NUM_VISUAL_TYPES ] = {
88 TrueColor, DirectColor,
89 PseudoColor, StaticColor,
90 GrayScale, StaticGray
91 };
92
93 return ( (unsigned) (visualType - GLX_TRUE_COLOR) <= NUM_VISUAL_TYPES )
94 ? x_visual_types[ visualType - GLX_TRUE_COLOR ] : -1;
95 }
96
97
98 /**
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.
104 *
105 * \param mode Destination GL context mode.
106 * \param config Source GLX visual config.
107 *
108 * \note
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.
111 */
112 void
113 _gl_copy_visual_to_context_mode( __GLcontextModes * mode,
114 const __GLXvisualConfig * config )
115 {
116 __GLcontextModes * const next = mode->next;
117
118 (void) __glXMemset( mode, 0, sizeof( __GLcontextModes ) );
119 mode->next = next;
120
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;
126
127 mode->rgbMode = (config->rgba != 0);
128 mode->renderType = (mode->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
129
130 mode->colorIndexMode = !(mode->rgbMode);
131 mode->doubleBufferMode = (config->doubleBuffer != 0);
132 mode->stereoMode = (config->stereo != 0);
133
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);
140
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;
151
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;
158
159 mode->numAuxBuffers = config->auxBuffers;
160 mode->level = config->level;
161
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;
169
170 mode->swapMethod = GLX_SWAP_UNDEFINED_OML;
171 }
172 #endif /* DRI_NEW_INTERFACE_ONLY */
173
174
175 /**
176 * Get data from a GL context mode.
177 *
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.
183 */
184 int
185 _gl_get_context_mode_data(const __GLcontextModes *mode, int attribute,
186 int *value_return)
187 {
188 switch (attribute) {
189 case GLX_USE_GL:
190 *value_return = GL_TRUE;
191 return 0;
192 case GLX_BUFFER_SIZE:
193 *value_return = mode->rgbBits;
194 return 0;
195 case GLX_RGBA:
196 *value_return = mode->rgbMode;
197 return 0;
198 case GLX_RED_SIZE:
199 *value_return = mode->redBits;
200 return 0;
201 case GLX_GREEN_SIZE:
202 *value_return = mode->greenBits;
203 return 0;
204 case GLX_BLUE_SIZE:
205 *value_return = mode->blueBits;
206 return 0;
207 case GLX_ALPHA_SIZE:
208 *value_return = mode->alphaBits;
209 return 0;
210 case GLX_DOUBLEBUFFER:
211 *value_return = mode->doubleBufferMode;
212 return 0;
213 case GLX_STEREO:
214 *value_return = mode->stereoMode;
215 return 0;
216 case GLX_AUX_BUFFERS:
217 *value_return = mode->numAuxBuffers;
218 return 0;
219 case GLX_DEPTH_SIZE:
220 *value_return = mode->depthBits;
221 return 0;
222 case GLX_STENCIL_SIZE:
223 *value_return = mode->stencilBits;
224 return 0;
225 case GLX_ACCUM_RED_SIZE:
226 *value_return = mode->accumRedBits;
227 return 0;
228 case GLX_ACCUM_GREEN_SIZE:
229 *value_return = mode->accumGreenBits;
230 return 0;
231 case GLX_ACCUM_BLUE_SIZE:
232 *value_return = mode->accumBlueBits;
233 return 0;
234 case GLX_ACCUM_ALPHA_SIZE:
235 *value_return = mode->accumAlphaBits;
236 return 0;
237 case GLX_LEVEL:
238 *value_return = mode->level;
239 return 0;
240 case GLX_TRANSPARENT_TYPE_EXT:
241 *value_return = mode->transparentPixel;
242 return 0;
243 case GLX_TRANSPARENT_RED_VALUE:
244 *value_return = mode->transparentRed;
245 return 0;
246 case GLX_TRANSPARENT_GREEN_VALUE:
247 *value_return = mode->transparentGreen;
248 return 0;
249 case GLX_TRANSPARENT_BLUE_VALUE:
250 *value_return = mode->transparentBlue;
251 return 0;
252 case GLX_TRANSPARENT_ALPHA_VALUE:
253 *value_return = mode->transparentAlpha;
254 return 0;
255 case GLX_TRANSPARENT_INDEX_VALUE:
256 *value_return = mode->transparentIndex;
257 return 0;
258 case GLX_X_VISUAL_TYPE:
259 *value_return = mode->visualType;
260 return 0;
261 case GLX_CONFIG_CAVEAT:
262 *value_return = mode->visualRating;
263 return 0;
264 case GLX_VISUAL_ID:
265 *value_return = mode->visualID;
266 return 0;
267 case GLX_DRAWABLE_TYPE:
268 *value_return = mode->drawableType;
269 return 0;
270 case GLX_RENDER_TYPE:
271 *value_return = mode->renderType;
272 return 0;
273 case GLX_X_RENDERABLE:
274 *value_return = mode->xRenderable;
275 return 0;
276 case GLX_FBCONFIG_ID:
277 *value_return = mode->fbconfigID;
278 return 0;
279 case GLX_MAX_PBUFFER_WIDTH:
280 *value_return = mode->maxPbufferWidth;
281 return 0;
282 case GLX_MAX_PBUFFER_HEIGHT:
283 *value_return = mode->maxPbufferHeight;
284 return 0;
285 case GLX_MAX_PBUFFER_PIXELS:
286 *value_return = mode->maxPbufferPixels;
287 return 0;
288 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
289 *value_return = mode->optimalPbufferWidth;
290 return 0;
291 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
292 *value_return = mode->optimalPbufferHeight;
293 return 0;
294 case GLX_SWAP_METHOD_OML:
295 *value_return = mode->swapMethod;
296 return 0;
297 case GLX_SAMPLE_BUFFERS_SGIS:
298 *value_return = mode->sampleBuffers;
299 return 0;
300 case GLX_SAMPLES_SGIS:
301 *value_return = mode->samples;
302 return 0;
303
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
306 * server.
307 */
308 case GLX_VISUAL_SELECT_GROUP_SGIX:
309 default:
310 return GLX_BAD_ATTRIBUTE;
311 }
312 }
313
314
315 /**
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.
323 *
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
328 * DRI-based driver.
329 * \returns A pointer to the first element in a linked list of \c count
330 * stuctures on success, or \c NULL on failure.
331 *
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.
340 */
341 __GLcontextModes *
342 _gl_context_modes_create( unsigned count, size_t minimum_size )
343 {
344 const size_t size = (minimum_size > sizeof( __GLcontextModes ))
345 ? minimum_size : sizeof( __GLcontextModes );
346 __GLcontextModes * base = NULL;
347 __GLcontextModes ** next;
348 unsigned i;
349
350 next = & base;
351 for ( i = 0 ; i < count ; i++ ) {
352 *next = (__GLcontextModes *) Xmalloc( size );
353 if ( *next == NULL ) {
354 _gl_context_modes_destroy( base );
355 base = NULL;
356 break;
357 }
358
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;
372
373 next = & ((*next)->next);
374 }
375
376 return base;
377 }
378
379
380 /**
381 * Destroy a linked list of \c __GLcontextModes structures created by
382 * \c _gl_context_modes_create.
383 *
384 * \param modes Linked list of structures to be destroyed. All structres
385 * in the list will be freed.
386 */
387 void
388 _gl_context_modes_destroy( __GLcontextModes * modes )
389 {
390 while ( modes != NULL ) {
391 __GLcontextModes * const next = modes->next;
392
393 Xfree( modes );
394 modes = next;
395 }
396 }
397
398
399 /**
400 * Find a context mode matching a Visual ID.
401 *
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.
406 */
407
408 __GLcontextModes *
409 _gl_context_modes_find_visual( __GLcontextModes * modes, int vid )
410 {
411 while ( modes != NULL ) {
412 if ( modes->visualID == vid ) {
413 break;
414 }
415
416 modes = modes->next;
417 }
418
419 return modes;
420 }
421
422
423 /**
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.
426 *
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.
431 */
432 GLboolean
433 _gl_context_modes_are_same( const __GLcontextModes * a,
434 const __GLcontextModes * b )
435 {
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) &&
450 #endif
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) &&
463
464 (a->transparentPixel == b->transparentPixel) &&
465
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))) &&
471
472 ((a->transparentPixel != GLX_TRANSPARENT_INDEX) ||
473 (a->transparentIndex == b->transparentIndex)) &&
474
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) );
485 }