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