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