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