b51b263fe46e2e6fdc926d4460b1fe6fb20c7fa9
[mesa.git] / src / mesa / drivers / dri / common / utils.c
1 /*
2 * (C) Copyright IBM Corporation 2002, 2004
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 utils.c
27 * Utility functions for DRI drivers.
28 *
29 * \author Ian Romanick <idr@us.ibm.com>
30 */
31
32 #include <string.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdbool.h>
36 #include <stdint.h>
37 #include "main/macros.h"
38 #include "main/mtypes.h"
39 #include "main/cpuinfo.h"
40 #include "main/extensions.h"
41 #include "utils.h"
42 #include "dri_util.h"
43
44
45 uint64_t
46 driParseDebugString( const char * debug,
47 const struct dri_debug_control * control )
48 {
49 uint64_t flag = 0;
50
51 if ( debug != NULL ) {
52 while( control->string != NULL ) {
53 if ( !strcmp( debug, "all" ) ||
54 strstr( debug, control->string ) != NULL ) {
55 flag |= control->flag;
56 }
57
58 control++;
59 }
60 }
61
62 return flag;
63 }
64
65
66
67 /**
68 * Create the \c GL_RENDERER string for DRI drivers.
69 *
70 * Almost all DRI drivers use a \c GL_RENDERER string of the form:
71 *
72 * "Mesa DRI <chip> <driver date> <AGP speed) <CPU information>"
73 *
74 * Using the supplied chip name, driver data, and AGP speed, this function
75 * creates the string.
76 *
77 * \param buffer Buffer to hold the \c GL_RENDERER string.
78 * \param hardware_name Name of the hardware.
79 * \param agp_mode AGP mode (speed).
80 *
81 * \returns
82 * The length of the string stored in \c buffer. This does \b not include
83 * the terminating \c NUL character.
84 */
85 unsigned
86 driGetRendererString( char * buffer, const char * hardware_name,
87 GLuint agp_mode )
88 {
89 unsigned offset;
90 char *cpu;
91
92 offset = sprintf( buffer, "Mesa DRI %s", hardware_name );
93
94 /* Append any AGP-specific information.
95 */
96 switch ( agp_mode ) {
97 case 1:
98 case 2:
99 case 4:
100 case 8:
101 offset += sprintf( & buffer[ offset ], " AGP %ux", agp_mode );
102 break;
103
104 default:
105 break;
106 }
107
108 /* Append any CPU-specific information.
109 */
110 cpu = _mesa_get_cpu_string();
111 if (cpu) {
112 offset += sprintf(buffer + offset, " %s", cpu);
113 free(cpu);
114 }
115
116 return offset;
117 }
118
119
120 /**
121 * Creates a set of \c struct gl_config that a driver will expose.
122 *
123 * A set of \c struct gl_config will be created based on the supplied
124 * parameters. The number of modes processed will be 2 *
125 * \c num_depth_stencil_bits * \c num_db_modes.
126 *
127 * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
128 * \c db_modes, and \c visType into each \c struct gl_config element.
129 * However, the meanings of \c fb_format and \c fb_type require further
130 * explanation. The \c fb_format specifies which color components are in
131 * each pixel and what the default order is. For example, \c GL_RGB specifies
132 * that red, green, blue are available and red is in the "most significant"
133 * position and blue is in the "least significant". The \c fb_type specifies
134 * the bit sizes of each component and the actual ordering. For example, if
135 * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
136 * are the blue value, bits [10:5] are the green value, and bits [4:0] are
137 * the red value.
138 *
139 * One sublte issue is the combination of \c GL_RGB or \c GL_BGR and either
140 * of the \c GL_UNSIGNED_INT_8_8_8_8 modes. The resulting mask values in the
141 * \c struct gl_config structure is \b identical to the \c GL_RGBA or
142 * \c GL_BGRA case, except the \c alphaMask is zero. This means that, as
143 * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
144 * still uses 32-bits.
145 *
146 * If in doubt, look at the tables used in the function.
147 *
148 * \param ptr_to_modes Pointer to a pointer to a linked list of
149 * \c struct gl_config. Upon completion, a pointer to
150 * the next element to be process will be stored here.
151 * If the function fails and returns \c GL_FALSE, this
152 * value will be unmodified, but some elements in the
153 * linked list may be modified.
154 * \param format Mesa mesa_format enum describing the pixel format
155 * \param depth_bits Array of depth buffer sizes to be exposed.
156 * \param stencil_bits Array of stencil buffer sizes to be exposed.
157 * \param num_depth_stencil_bits Number of entries in both \c depth_bits and
158 * \c stencil_bits.
159 * \param db_modes Array of buffer swap modes. If an element has a
160 * value of \c GLX_NONE, then it represents a
161 * single-buffered mode. Other valid values are
162 * \c GLX_SWAP_EXCHANGE_OML, \c GLX_SWAP_COPY_OML, and
163 * \c GLX_SWAP_UNDEFINED_OML. See the
164 * GLX_OML_swap_method extension spec for more details.
165 * \param num_db_modes Number of entries in \c db_modes.
166 * \param msaa_samples Array of msaa sample count. 0 represents a visual
167 * without a multisample buffer.
168 * \param num_msaa_modes Number of entries in \c msaa_samples.
169 * \param visType GLX visual type. Usually either \c GLX_TRUE_COLOR or
170 * \c GLX_DIRECT_COLOR.
171 *
172 * \returns
173 * Pointer to any array of pointers to the \c __DRIconfig structures created
174 * for the specified formats. If there is an error, \c NULL is returned.
175 * Currently the only cause of failure is a bad parameter (i.e., unsupported
176 * \c format).
177 */
178 __DRIconfig **
179 driCreateConfigs(mesa_format format,
180 const uint8_t * depth_bits, const uint8_t * stencil_bits,
181 unsigned num_depth_stencil_bits,
182 const GLenum * db_modes, unsigned num_db_modes,
183 const uint8_t * msaa_samples, unsigned num_msaa_modes,
184 GLboolean enable_accum)
185 {
186 static const uint32_t masks_table[][4] = {
187 /* MESA_FORMAT_B5G6R5_UNORM */
188 { 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 },
189 /* MESA_FORMAT_B8G8R8X8_UNORM */
190 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 },
191 /* MESA_FORMAT_B8G8R8A8_UNORM */
192 { 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 },
193 /* MESA_FORMAT_B10G10R10X2_UNORM */
194 { 0x3FF00000, 0x000FFC00, 0x000003FF, 0x00000000 },
195 /* MESA_FORMAT_B10G10R10A2_UNORM */
196 { 0x3FF00000, 0x000FFC00, 0x000003FF, 0xC0000000 },
197 };
198
199 const uint32_t * masks;
200 __DRIconfig **configs, **c;
201 struct gl_config *modes;
202 unsigned i, j, k, h;
203 unsigned num_modes;
204 unsigned num_accum_bits = (enable_accum) ? 2 : 1;
205 int red_bits;
206 int green_bits;
207 int blue_bits;
208 int alpha_bits;
209 bool is_srgb;
210
211 switch (format) {
212 case MESA_FORMAT_B5G6R5_UNORM:
213 masks = masks_table[0];
214 break;
215 case MESA_FORMAT_B8G8R8X8_UNORM:
216 case MESA_FORMAT_B8G8R8X8_SRGB:
217 masks = masks_table[1];
218 break;
219 case MESA_FORMAT_B8G8R8A8_UNORM:
220 case MESA_FORMAT_B8G8R8A8_SRGB:
221 masks = masks_table[2];
222 break;
223 case MESA_FORMAT_B10G10R10X2_UNORM:
224 masks = masks_table[3];
225 break;
226 case MESA_FORMAT_B10G10R10A2_UNORM:
227 masks = masks_table[4];
228 break;
229 default:
230 fprintf(stderr, "[%s:%u] Unknown framebuffer type %s (%d).\n",
231 __func__, __LINE__,
232 _mesa_get_format_name(format), format);
233 return NULL;
234 }
235
236 red_bits = _mesa_get_format_bits(format, GL_RED_BITS);
237 green_bits = _mesa_get_format_bits(format, GL_GREEN_BITS);
238 blue_bits = _mesa_get_format_bits(format, GL_BLUE_BITS);
239 alpha_bits = _mesa_get_format_bits(format, GL_ALPHA_BITS);
240 is_srgb = _mesa_get_format_color_encoding(format) == GL_SRGB;
241
242 num_modes = num_depth_stencil_bits * num_db_modes * num_accum_bits * num_msaa_modes;
243 configs = calloc(num_modes + 1, sizeof *configs);
244 if (configs == NULL)
245 return NULL;
246
247 c = configs;
248 for ( k = 0 ; k < num_depth_stencil_bits ; k++ ) {
249 for ( i = 0 ; i < num_db_modes ; i++ ) {
250 for ( h = 0 ; h < num_msaa_modes; h++ ) {
251 for ( j = 0 ; j < num_accum_bits ; j++ ) {
252 *c = malloc (sizeof **c);
253 modes = &(*c)->modes;
254 c++;
255
256 memset(modes, 0, sizeof *modes);
257 modes->redBits = red_bits;
258 modes->greenBits = green_bits;
259 modes->blueBits = blue_bits;
260 modes->alphaBits = alpha_bits;
261 modes->redMask = masks[0];
262 modes->greenMask = masks[1];
263 modes->blueMask = masks[2];
264 modes->alphaMask = masks[3];
265 modes->rgbBits = modes->redBits + modes->greenBits
266 + modes->blueBits + modes->alphaBits;
267
268 modes->accumRedBits = 16 * j;
269 modes->accumGreenBits = 16 * j;
270 modes->accumBlueBits = 16 * j;
271 modes->accumAlphaBits = (masks[3] != 0) ? 16 * j : 0;
272 modes->visualRating = (j == 0) ? GLX_NONE : GLX_SLOW_CONFIG;
273
274 modes->stencilBits = stencil_bits[k];
275 modes->depthBits = depth_bits[k];
276
277 modes->transparentPixel = GLX_NONE;
278 modes->transparentRed = GLX_DONT_CARE;
279 modes->transparentGreen = GLX_DONT_CARE;
280 modes->transparentBlue = GLX_DONT_CARE;
281 modes->transparentAlpha = GLX_DONT_CARE;
282 modes->transparentIndex = GLX_DONT_CARE;
283 modes->rgbMode = GL_TRUE;
284
285 if ( db_modes[i] == GLX_NONE ) {
286 modes->doubleBufferMode = GL_FALSE;
287 }
288 else {
289 modes->doubleBufferMode = GL_TRUE;
290 modes->swapMethod = db_modes[i];
291 }
292
293 modes->samples = msaa_samples[h];
294 modes->sampleBuffers = modes->samples ? 1 : 0;
295
296
297 modes->haveAccumBuffer = ((modes->accumRedBits +
298 modes->accumGreenBits +
299 modes->accumBlueBits +
300 modes->accumAlphaBits) > 0);
301 modes->haveDepthBuffer = (modes->depthBits > 0);
302 modes->haveStencilBuffer = (modes->stencilBits > 0);
303
304 modes->bindToTextureRgb = GL_TRUE;
305 modes->bindToTextureRgba = GL_TRUE;
306 modes->bindToMipmapTexture = GL_FALSE;
307 modes->bindToTextureTargets =
308 __DRI_ATTRIB_TEXTURE_1D_BIT |
309 __DRI_ATTRIB_TEXTURE_2D_BIT |
310 __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT;
311
312 modes->yInverted = GL_TRUE;
313 modes->sRGBCapable = is_srgb;
314 }
315 }
316 }
317 }
318 *c = NULL;
319
320 return configs;
321 }
322
323 __DRIconfig **driConcatConfigs(__DRIconfig **a,
324 __DRIconfig **b)
325 {
326 __DRIconfig **all;
327 int i, j, index;
328
329 if (a == NULL || a[0] == NULL)
330 return b;
331 else if (b == NULL || b[0] == NULL)
332 return a;
333
334 i = 0;
335 while (a[i] != NULL)
336 i++;
337 j = 0;
338 while (b[j] != NULL)
339 j++;
340
341 all = malloc((i + j + 1) * sizeof *all);
342 index = 0;
343 for (i = 0; a[i] != NULL; i++)
344 all[index++] = a[i];
345 for (j = 0; b[j] != NULL; j++)
346 all[index++] = b[j];
347 all[index++] = NULL;
348
349 free(a);
350 free(b);
351
352 return all;
353 }
354
355 #define __ATTRIB(attrib, field) \
356 { attrib, offsetof(struct gl_config, field) }
357
358 static const struct { unsigned int attrib, offset; } attribMap[] = {
359 __ATTRIB(__DRI_ATTRIB_BUFFER_SIZE, rgbBits),
360 __ATTRIB(__DRI_ATTRIB_LEVEL, level),
361 __ATTRIB(__DRI_ATTRIB_RED_SIZE, redBits),
362 __ATTRIB(__DRI_ATTRIB_GREEN_SIZE, greenBits),
363 __ATTRIB(__DRI_ATTRIB_BLUE_SIZE, blueBits),
364 __ATTRIB(__DRI_ATTRIB_ALPHA_SIZE, alphaBits),
365 __ATTRIB(__DRI_ATTRIB_DEPTH_SIZE, depthBits),
366 __ATTRIB(__DRI_ATTRIB_STENCIL_SIZE, stencilBits),
367 __ATTRIB(__DRI_ATTRIB_ACCUM_RED_SIZE, accumRedBits),
368 __ATTRIB(__DRI_ATTRIB_ACCUM_GREEN_SIZE, accumGreenBits),
369 __ATTRIB(__DRI_ATTRIB_ACCUM_BLUE_SIZE, accumBlueBits),
370 __ATTRIB(__DRI_ATTRIB_ACCUM_ALPHA_SIZE, accumAlphaBits),
371 __ATTRIB(__DRI_ATTRIB_SAMPLE_BUFFERS, sampleBuffers),
372 __ATTRIB(__DRI_ATTRIB_SAMPLES, samples),
373 __ATTRIB(__DRI_ATTRIB_DOUBLE_BUFFER, doubleBufferMode),
374 __ATTRIB(__DRI_ATTRIB_STEREO, stereoMode),
375 __ATTRIB(__DRI_ATTRIB_AUX_BUFFERS, numAuxBuffers),
376 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_TYPE, transparentPixel),
377 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_INDEX_VALUE, transparentPixel),
378 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_RED_VALUE, transparentRed),
379 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_GREEN_VALUE, transparentGreen),
380 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_BLUE_VALUE, transparentBlue),
381 __ATTRIB(__DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE, transparentAlpha),
382 __ATTRIB(__DRI_ATTRIB_RED_MASK, redMask),
383 __ATTRIB(__DRI_ATTRIB_GREEN_MASK, greenMask),
384 __ATTRIB(__DRI_ATTRIB_BLUE_MASK, blueMask),
385 __ATTRIB(__DRI_ATTRIB_ALPHA_MASK, alphaMask),
386 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_WIDTH, maxPbufferWidth),
387 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_HEIGHT, maxPbufferHeight),
388 __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_PIXELS, maxPbufferPixels),
389 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH, optimalPbufferWidth),
390 __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT, optimalPbufferHeight),
391 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD, swapMethod),
392 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGB, bindToTextureRgb),
393 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGBA, bindToTextureRgba),
394 __ATTRIB(__DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE, bindToMipmapTexture),
395 __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS, bindToTextureTargets),
396 __ATTRIB(__DRI_ATTRIB_YINVERTED, yInverted),
397 __ATTRIB(__DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE, sRGBCapable),
398
399 /* The struct field doesn't matter here, these are handled by the
400 * switch in driGetConfigAttribIndex. We need them in the array
401 * so the iterator includes them though.*/
402 __ATTRIB(__DRI_ATTRIB_RENDER_TYPE, level),
403 __ATTRIB(__DRI_ATTRIB_CONFIG_CAVEAT, level),
404 __ATTRIB(__DRI_ATTRIB_SWAP_METHOD, level)
405 };
406
407
408 /**
409 * Return the value of a configuration attribute. The attribute is
410 * indicated by the index.
411 */
412 static int
413 driGetConfigAttribIndex(const __DRIconfig *config,
414 unsigned int index, unsigned int *value)
415 {
416 switch (attribMap[index].attrib) {
417 case __DRI_ATTRIB_RENDER_TYPE:
418 /* no support for color index mode */
419 *value = __DRI_ATTRIB_RGBA_BIT;
420 break;
421 case __DRI_ATTRIB_CONFIG_CAVEAT:
422 if (config->modes.visualRating == GLX_NON_CONFORMANT_CONFIG)
423 *value = __DRI_ATTRIB_NON_CONFORMANT_CONFIG;
424 else if (config->modes.visualRating == GLX_SLOW_CONFIG)
425 *value = __DRI_ATTRIB_SLOW_BIT;
426 else
427 *value = 0;
428 break;
429 case __DRI_ATTRIB_SWAP_METHOD:
430 /* XXX no return value??? */
431 break;
432
433 default:
434 /* any other int-sized field */
435 *value = *(unsigned int *)
436 ((char *) &config->modes + attribMap[index].offset);
437
438 break;
439 }
440
441 return GL_TRUE;
442 }
443
444
445 /**
446 * Get the value of a configuration attribute.
447 * \param attrib the attribute (one of the _DRI_ATTRIB_x tokens)
448 * \param value returns the attribute's value
449 * \return 1 for success, 0 for failure
450 */
451 int
452 driGetConfigAttrib(const __DRIconfig *config,
453 unsigned int attrib, unsigned int *value)
454 {
455 int i;
456
457 for (i = 0; i < ARRAY_SIZE(attribMap); i++)
458 if (attribMap[i].attrib == attrib)
459 return driGetConfigAttribIndex(config, i, value);
460
461 return GL_FALSE;
462 }
463
464
465 /**
466 * Get a configuration attribute name and value, given an index.
467 * \param index which field of the __DRIconfig to query
468 * \param attrib returns the attribute name (one of the _DRI_ATTRIB_x tokens)
469 * \param value returns the attribute's value
470 * \return 1 for success, 0 for failure
471 */
472 int
473 driIndexConfigAttrib(const __DRIconfig *config, int index,
474 unsigned int *attrib, unsigned int *value)
475 {
476 if (index >= 0 && index < ARRAY_SIZE(attribMap)) {
477 *attrib = attribMap[index].attrib;
478 return driGetConfigAttribIndex(config, index, value);
479 }
480
481 return GL_FALSE;
482 }
483
484 /**
485 * Implement queries for values that are common across all Mesa drivers
486 *
487 * Currently only the following queries are supported by this function:
488 *
489 * - \c __DRI2_RENDERER_VERSION
490 * - \c __DRI2_RENDERER_PREFERRED_PROFILE
491 * - \c __DRI2_RENDERER_OPENGL_CORE_PROFILE_VERSION
492 * - \c __DRI2_RENDERER_OPENGL_COMPATIBLITY_PROFILE_VERSION
493 * - \c __DRI2_RENDERER_ES_PROFILE_VERSION
494 * - \c __DRI2_RENDERER_ES2_PROFILE_VERSION
495 *
496 * \returns
497 * Zero if a recognized value of \c param is supplied, -1 otherwise.
498 */
499 int
500 driQueryRendererIntegerCommon(__DRIscreen *psp, int param, unsigned int *value)
501 {
502 switch (param) {
503 case __DRI2_RENDERER_VERSION: {
504 static const char *const ver = PACKAGE_VERSION;
505 char *endptr;
506 int v[3];
507
508 v[0] = strtol(ver, &endptr, 10);
509 assert(endptr[0] == '.');
510 if (endptr[0] != '.')
511 return -1;
512
513 v[1] = strtol(endptr + 1, &endptr, 10);
514 assert(endptr[0] == '.');
515 if (endptr[0] != '.')
516 return -1;
517
518 v[2] = strtol(endptr + 1, &endptr, 10);
519
520 value[0] = v[0];
521 value[1] = v[1];
522 value[2] = v[2];
523 return 0;
524 }
525 case __DRI2_RENDERER_PREFERRED_PROFILE:
526 value[0] = (psp->max_gl_core_version != 0)
527 ? (1U << __DRI_API_OPENGL_CORE) : (1U << __DRI_API_OPENGL);
528 return 0;
529 case __DRI2_RENDERER_OPENGL_CORE_PROFILE_VERSION:
530 value[0] = psp->max_gl_core_version / 10;
531 value[1] = psp->max_gl_core_version % 10;
532 return 0;
533 case __DRI2_RENDERER_OPENGL_COMPATIBILITY_PROFILE_VERSION:
534 value[0] = psp->max_gl_compat_version / 10;
535 value[1] = psp->max_gl_compat_version % 10;
536 return 0;
537 case __DRI2_RENDERER_OPENGL_ES_PROFILE_VERSION:
538 value[0] = psp->max_gl_es1_version / 10;
539 value[1] = psp->max_gl_es1_version % 10;
540 return 0;
541 case __DRI2_RENDERER_OPENGL_ES2_PROFILE_VERSION:
542 value[0] = psp->max_gl_es2_version / 10;
543 value[1] = psp->max_gl_es2_version % 10;
544 return 0;
545 default:
546 break;
547 }
548
549 return -1;
550 }