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