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