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