Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[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;
340 return 0;
341 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
342 *value_return = mode->bindToTextureTargets;
343 return 0;
344 case GLX_Y_INVERTED_EXT:
345 *value_return = mode->yInverted;
346 return 0;
347
348 /* Applications are NOT allowed to query GLX_VISUAL_SELECT_GROUP_SGIX.
349 * It is ONLY for communication between the GLX client and the GLX
350 * server.
351 */
352 case GLX_VISUAL_SELECT_GROUP_SGIX:
353 default:
354 return GLX_BAD_ATTRIBUTE;
355 }
356 }
357 #endif /* !defined(IN_MINI_GLX) */
358
359
360 /**
361 * Allocate a linked list of \c __GLcontextModes structures. The fields of
362 * each structure will be initialized to "reasonable" default values. In
363 * most cases this is the default value defined by table 3.4 of the GLX
364 * 1.3 specification. This means that most values are either initialized to
365 * zero or \c GLX_DONT_CARE (which is -1). As support for additional
366 * extensions is added, the new values will be initialized to appropriate
367 * values from the extension specification.
368 *
369 * \param count Number of structures to allocate.
370 * \param minimum_size Minimum size of a structure to allocate. This allows
371 * for differences in the version of the
372 * \c __GLcontextModes stucture used in libGL and in a
373 * DRI-based driver.
374 * \returns A pointer to the first element in a linked list of \c count
375 * stuctures on success, or \c NULL on failure.
376 *
377 * \warning Use of \c minimum_size does \b not guarantee binary compatibility.
378 * The fundamental assumption is that if the \c minimum_size
379 * specified by the driver and the size of the \c __GLcontextModes
380 * structure in libGL is the same, then the meaning of each byte in
381 * the structure is the same in both places. \b Be \b careful!
382 * Basically this means that fields have to be added in libGL and
383 * then propagated to drivers. Drivers should \b never arbitrarilly
384 * extend the \c __GLcontextModes data-structure.
385 */
386 __GLcontextModes *
387 _gl_context_modes_create( unsigned count, size_t minimum_size )
388 {
389 const size_t size = (minimum_size > sizeof( __GLcontextModes ))
390 ? minimum_size : sizeof( __GLcontextModes );
391 __GLcontextModes * base = NULL;
392 __GLcontextModes ** next;
393 unsigned i;
394
395 next = & base;
396 for ( i = 0 ; i < count ; i++ ) {
397 *next = (__GLcontextModes *) _mesa_malloc( size );
398 if ( *next == NULL ) {
399 _gl_context_modes_destroy( base );
400 base = NULL;
401 break;
402 }
403
404 (void) _mesa_memset( *next, 0, size );
405 (*next)->visualID = GLX_DONT_CARE;
406 (*next)->visualType = GLX_DONT_CARE;
407 (*next)->visualRating = GLX_NONE;
408 (*next)->transparentPixel = GLX_NONE;
409 (*next)->transparentRed = GLX_DONT_CARE;
410 (*next)->transparentGreen = GLX_DONT_CARE;
411 (*next)->transparentBlue = GLX_DONT_CARE;
412 (*next)->transparentAlpha = GLX_DONT_CARE;
413 (*next)->transparentIndex = GLX_DONT_CARE;
414 (*next)->xRenderable = GLX_DONT_CARE;
415 (*next)->fbconfigID = GLX_DONT_CARE;
416 (*next)->swapMethod = GLX_SWAP_UNDEFINED_OML;
417 (*next)->bindToTextureRgb = GLX_DONT_CARE;
418 (*next)->bindToTextureRgba = GLX_DONT_CARE;
419 (*next)->bindToMipmapTexture = GLX_DONT_CARE;
420 (*next)->bindToTextureTargets = 0;
421 (*next)->yInverted = GLX_DONT_CARE;
422
423 next = & ((*next)->next);
424 }
425
426 return base;
427 }
428
429
430 /**
431 * Destroy a linked list of \c __GLcontextModes structures created by
432 * \c _gl_context_modes_create.
433 *
434 * \param modes Linked list of structures to be destroyed. All structres
435 * in the list will be freed.
436 */
437 void
438 _gl_context_modes_destroy( __GLcontextModes * modes )
439 {
440 while ( modes != NULL ) {
441 __GLcontextModes * const next = modes->next;
442
443 _mesa_free( modes );
444 modes = next;
445 }
446 }
447
448
449 /**
450 * Find a context mode matching a Visual ID.
451 *
452 * \param modes List list of context-mode structures to be searched.
453 * \param vid Visual ID to be found.
454 * \returns A pointer to a context-mode in \c modes if \c vid was found in
455 * the list, or \c NULL if it was not.
456 */
457
458 __GLcontextModes *
459 _gl_context_modes_find_visual( __GLcontextModes * modes, int vid )
460 {
461 while ( modes != NULL ) {
462 if ( modes->visualID == vid ) {
463 break;
464 }
465
466 modes = modes->next;
467 }
468
469 return modes;
470 }
471
472
473 /**
474 * Determine if two context-modes are the same. This is intended to be used
475 * by libGL implementations to compare to sets of driver generated FBconfigs.
476 *
477 * \param a Context-mode to be compared.
478 * \param b Context-mode to be compared.
479 * \returns \c GL_TRUE if the two context-modes are the same. \c GL_FALSE is
480 * returned otherwise.
481 */
482 GLboolean
483 _gl_context_modes_are_same( const __GLcontextModes * a,
484 const __GLcontextModes * b )
485 {
486 return( (a->rgbMode == b->rgbMode) &&
487 (a->floatMode == b->floatMode) &&
488 (a->colorIndexMode == b->colorIndexMode) &&
489 (a->doubleBufferMode == b->doubleBufferMode) &&
490 (a->stereoMode == b->stereoMode) &&
491 (a->redBits == b->redBits) &&
492 (a->greenBits == b->greenBits) &&
493 (a->blueBits == b->blueBits) &&
494 (a->alphaBits == b->alphaBits) &&
495 #if 0 /* For some reason these don't get set on the client-side in libGL. */
496 (a->redMask == b->redMask) &&
497 (a->greenMask == b->greenMask) &&
498 (a->blueMask == b->blueMask) &&
499 (a->alphaMask == b->alphaMask) &&
500 #endif
501 (a->rgbBits == b->rgbBits) &&
502 (a->indexBits == b->indexBits) &&
503 (a->accumRedBits == b->accumRedBits) &&
504 (a->accumGreenBits == b->accumGreenBits) &&
505 (a->accumBlueBits == b->accumBlueBits) &&
506 (a->accumAlphaBits == b->accumAlphaBits) &&
507 (a->depthBits == b->depthBits) &&
508 (a->stencilBits == b->stencilBits) &&
509 (a->numAuxBuffers == b->numAuxBuffers) &&
510 (a->level == b->level) &&
511 (a->pixmapMode == b->pixmapMode) &&
512 (a->visualRating == b->visualRating) &&
513
514 (a->transparentPixel == b->transparentPixel) &&
515
516 ((a->transparentPixel != GLX_TRANSPARENT_RGB) ||
517 ((a->transparentRed == b->transparentRed) &&
518 (a->transparentGreen == b->transparentGreen) &&
519 (a->transparentBlue == b->transparentBlue) &&
520 (a->transparentAlpha == b->transparentAlpha))) &&
521
522 ((a->transparentPixel != GLX_TRANSPARENT_INDEX) ||
523 (a->transparentIndex == b->transparentIndex)) &&
524
525 (a->sampleBuffers == b->sampleBuffers) &&
526 (a->samples == b->samples) &&
527 ((a->drawableType & b->drawableType) != 0) &&
528 (a->renderType == b->renderType) &&
529 (a->maxPbufferWidth == b->maxPbufferWidth) &&
530 (a->maxPbufferHeight == b->maxPbufferHeight) &&
531 (a->maxPbufferPixels == b->maxPbufferPixels) &&
532 (a->optimalPbufferWidth == b->optimalPbufferWidth) &&
533 (a->optimalPbufferHeight == b->optimalPbufferHeight) &&
534 (a->swapMethod == b->swapMethod) &&
535 (a->bindToTextureRgb == b->bindToTextureRgb) &&
536 (a->bindToTextureRgba == b->bindToTextureRgba) &&
537 (a->bindToMipmapTexture == b->bindToMipmapTexture) &&
538 (a->bindToTextureTargets == b->bindToTextureTargets) &&
539 (a->yInverted == b->yInverted) );
540 }