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