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