17117f1b4Smrg/*
27117f1b4Smrg * (C) Copyright IBM Corporation 2002, 2004
37117f1b4Smrg * All Rights Reserved.
47117f1b4Smrg *
57117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a
67117f1b4Smrg * copy of this software and associated documentation files (the "Software"),
77117f1b4Smrg * to deal in the Software without restriction, including without limitation
87117f1b4Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
97117f1b4Smrg * license, and/or sell copies of the Software, and to permit persons to whom
107117f1b4Smrg * the Software is furnished to do so, subject to the following conditions:
117117f1b4Smrg *
127117f1b4Smrg * The above copyright notice and this permission notice (including the next
137117f1b4Smrg * paragraph) shall be included in all copies or substantial portions of the
147117f1b4Smrg * Software.
157117f1b4Smrg *
167117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
177117f1b4Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
197117f1b4Smrg * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
207117f1b4Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
217117f1b4Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
227117f1b4Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE.
237117f1b4Smrg */
247117f1b4Smrg
257117f1b4Smrg/**
267117f1b4Smrg * \file utils.c
277117f1b4Smrg * Utility functions for DRI drivers.
287117f1b4Smrg *
297117f1b4Smrg * \author Ian Romanick <idr@us.ibm.com>
307117f1b4Smrg */
317117f1b4Smrg
327117f1b4Smrg#include <string.h>
3301e04c3fSmrg#include <stdio.h>
347117f1b4Smrg#include <stdlib.h>
35af69d88dSmrg#include <stdbool.h>
36af69d88dSmrg#include <stdint.h>
37af69d88dSmrg#include "main/macros.h"
38c1f859d4Smrg#include "main/mtypes.h"
394a49301eSmrg#include "main/cpuinfo.h"
40c1f859d4Smrg#include "main/extensions.h"
417117f1b4Smrg#include "utils.h"
42af69d88dSmrg#include "dri_util.h"
437117f1b4Smrg
4401e04c3fSmrg/* WARNING: HACK: Local defines to avoid pulling glx.h.
4501e04c3fSmrg *
4601e04c3fSmrg * Any parts of this file that use the following defines are either partial or
4701e04c3fSmrg * entirely broken wrt EGL.
4801e04c3fSmrg *
4901e04c3fSmrg * For example any getConfigAttrib() or indexConfigAttrib() query from EGL for
5001e04c3fSmrg * SLOW or NON_CONFORMANT_CONFIG will not work as expected since the EGL tokens
5101e04c3fSmrg * are different from the GLX ones.
5201e04c3fSmrg */
5301e04c3fSmrg#define GLX_NONE                                                0x8000
5401e04c3fSmrg#define GLX_SLOW_CONFIG                                         0x8001
5501e04c3fSmrg#define GLX_NON_CONFORMANT_CONFIG                               0x800D
5601e04c3fSmrg#define GLX_DONT_CARE                                           0xFFFFFFFF
577117f1b4Smrg
587117f1b4Smrg/**
597117f1b4Smrg * Create the \c GL_RENDERER string for DRI drivers.
607117f1b4Smrg *
617117f1b4Smrg * Almost all DRI drivers use a \c GL_RENDERER string of the form:
627117f1b4Smrg *
637117f1b4Smrg *    "Mesa DRI <chip> <driver date> <AGP speed) <CPU information>"
647117f1b4Smrg *
657117f1b4Smrg * Using the supplied chip name, driver data, and AGP speed, this function
667117f1b4Smrg * creates the string.
677117f1b4Smrg *
687117f1b4Smrg * \param buffer         Buffer to hold the \c GL_RENDERER string.
697117f1b4Smrg * \param hardware_name  Name of the hardware.
707117f1b4Smrg * \param agp_mode       AGP mode (speed).
717117f1b4Smrg *
727117f1b4Smrg * \returns
737117f1b4Smrg * The length of the string stored in \c buffer.  This does \b not include
747117f1b4Smrg * the terminating \c NUL character.
757117f1b4Smrg */
767117f1b4Smrgunsigned
777117f1b4SmrgdriGetRendererString( char * buffer, const char * hardware_name,
783464ebd5Sriastradh		      GLuint agp_mode )
797117f1b4Smrg{
804a49301eSmrg   unsigned offset;
814a49301eSmrg   char *cpu;
827117f1b4Smrg
833464ebd5Sriastradh   offset = sprintf( buffer, "Mesa DRI %s", hardware_name );
847117f1b4Smrg
857117f1b4Smrg   /* Append any AGP-specific information.
867117f1b4Smrg    */
877117f1b4Smrg   switch ( agp_mode ) {
887117f1b4Smrg   case 1:
897117f1b4Smrg   case 2:
907117f1b4Smrg   case 4:
917117f1b4Smrg   case 8:
927117f1b4Smrg      offset += sprintf( & buffer[ offset ], " AGP %ux", agp_mode );
937117f1b4Smrg      break;
947117f1b4Smrg
957117f1b4Smrg   default:
967117f1b4Smrg      break;
977117f1b4Smrg   }
987117f1b4Smrg
997117f1b4Smrg   /* Append any CPU-specific information.
1007117f1b4Smrg    */
1014a49301eSmrg   cpu = _mesa_get_cpu_string();
1024a49301eSmrg   if (cpu) {
1034a49301eSmrg      offset += sprintf(buffer + offset, " %s", cpu);
104cdc920a0Smrg      free(cpu);
1057117f1b4Smrg   }
1067117f1b4Smrg
1077117f1b4Smrg   return offset;
1087117f1b4Smrg}
1097117f1b4Smrg
1107117f1b4Smrg
1117117f1b4Smrg/**
1123464ebd5Sriastradh * Creates a set of \c struct gl_config that a driver will expose.
1137117f1b4Smrg *
1143464ebd5Sriastradh * A set of \c struct gl_config will be created based on the supplied
1157117f1b4Smrg * parameters.  The number of modes processed will be 2 *
1167117f1b4Smrg * \c num_depth_stencil_bits * \c num_db_modes.
1177117f1b4Smrg *
1187117f1b4Smrg * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
1193464ebd5Sriastradh * \c db_modes, and \c visType into each \c struct gl_config element.
1207117f1b4Smrg * However, the meanings of \c fb_format and \c fb_type require further
1217117f1b4Smrg * explanation.  The \c fb_format specifies which color components are in
1227117f1b4Smrg * each pixel and what the default order is.  For example, \c GL_RGB specifies
1237117f1b4Smrg * that red, green, blue are available and red is in the "most significant"
1247117f1b4Smrg * position and blue is in the "least significant".  The \c fb_type specifies
1257117f1b4Smrg * the bit sizes of each component and the actual ordering.  For example, if
1267117f1b4Smrg * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
1277117f1b4Smrg * are the blue value, bits [10:5] are the green value, and bits [4:0] are
1287117f1b4Smrg * the red value.
1297117f1b4Smrg *
1307117f1b4Smrg * One sublte issue is the combination of \c GL_RGB  or \c GL_BGR and either
1317117f1b4Smrg * of the \c GL_UNSIGNED_INT_8_8_8_8 modes.  The resulting mask values in the
1323464ebd5Sriastradh * \c struct gl_config structure is \b identical to the \c GL_RGBA or
1337117f1b4Smrg * \c GL_BGRA case, except the \c alphaMask is zero.  This means that, as
1347117f1b4Smrg * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
1357117f1b4Smrg * still uses 32-bits.
1367117f1b4Smrg *
1377117f1b4Smrg * If in doubt, look at the tables used in the function.
1387117f1b4Smrg *
1397117f1b4Smrg * \param ptr_to_modes  Pointer to a pointer to a linked list of
1403464ebd5Sriastradh *                      \c struct gl_config.  Upon completion, a pointer to
1417117f1b4Smrg *                      the next element to be process will be stored here.
1427117f1b4Smrg *                      If the function fails and returns \c GL_FALSE, this
1437117f1b4Smrg *                      value will be unmodified, but some elements in the
1447117f1b4Smrg *                      linked list may be modified.
145af69d88dSmrg * \param format        Mesa mesa_format enum describing the pixel format
1467117f1b4Smrg * \param depth_bits    Array of depth buffer sizes to be exposed.
1477117f1b4Smrg * \param stencil_bits  Array of stencil buffer sizes to be exposed.
1487117f1b4Smrg * \param num_depth_stencil_bits  Number of entries in both \c depth_bits and
1497117f1b4Smrg *                      \c stencil_bits.
1507117f1b4Smrg * \param db_modes      Array of buffer swap modes.  If an element has a
15101e04c3fSmrg *                      value of \c __DRI_ATTRIB_SWAP_NONE, then it
15201e04c3fSmrg *                      represents a single-buffered mode.  Other valid
15301e04c3fSmrg *                      values are \c __DRI_ATTRIB_SWAP_EXCHANGE,
15401e04c3fSmrg *                      \c __DRI_ATTRIB_SWAP_COPY, and \c __DRI_ATTRIB_SWAP_UNDEFINED.
15501e04c3fSmrg *                      They represent the respective GLX values as in
15601e04c3fSmrg *                      the GLX_OML_swap_method extension spec.
1577117f1b4Smrg * \param num_db_modes  Number of entries in \c db_modes.
1584a49301eSmrg * \param msaa_samples  Array of msaa sample count. 0 represents a visual
1594a49301eSmrg *                      without a multisample buffer.
1604a49301eSmrg * \param num_msaa_modes Number of entries in \c msaa_samples.
16101e04c3fSmrg * \param enable_accum  Add an accum buffer to the configs
16201e04c3fSmrg * \param color_depth_match Whether the color depth must match the zs depth
16301e04c3fSmrg *                          This forces 32-bit color to have 24-bit depth, and
16401e04c3fSmrg *                          16-bit color to have 16-bit depth.
16501e04c3fSmrg *
1667117f1b4Smrg * \returns
167af69d88dSmrg * Pointer to any array of pointers to the \c __DRIconfig structures created
168af69d88dSmrg * for the specified formats.  If there is an error, \c NULL is returned.
169af69d88dSmrg * Currently the only cause of failure is a bad parameter (i.e., unsupported
170af69d88dSmrg * \c format).
1717117f1b4Smrg */
172c1f859d4Smrg__DRIconfig **
173af69d88dSmrgdriCreateConfigs(mesa_format format,
174c1f859d4Smrg		 const uint8_t * depth_bits, const uint8_t * stencil_bits,
175c1f859d4Smrg		 unsigned num_depth_stencil_bits,
1764a49301eSmrg		 const GLenum * db_modes, unsigned num_db_modes,
177cdc920a0Smrg		 const uint8_t * msaa_samples, unsigned num_msaa_modes,
1787ec681f3Smrg		 GLboolean enable_accum, GLboolean color_depth_match)
1797117f1b4Smrg{
1807ec681f3Smrg   static const struct {
1817ec681f3Smrg      uint32_t masks[4];
1827ec681f3Smrg      int shifts[4];
1837ec681f3Smrg   } format_table[] = {
184af69d88dSmrg      /* MESA_FORMAT_B5G6R5_UNORM */
1857ec681f3Smrg      {{ 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 },
1867ec681f3Smrg       { 11, 5, 0, -1 }},
187af69d88dSmrg      /* MESA_FORMAT_B8G8R8X8_UNORM */
1887ec681f3Smrg      {{ 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 },
1897ec681f3Smrg       { 16, 8, 0, -1 }},
190af69d88dSmrg      /* MESA_FORMAT_B8G8R8A8_UNORM */
1917ec681f3Smrg      {{ 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 },
1927ec681f3Smrg       { 16, 8, 0, 24 }},
193af69d88dSmrg      /* MESA_FORMAT_B10G10R10X2_UNORM */
1947ec681f3Smrg      {{ 0x3FF00000, 0x000FFC00, 0x000003FF, 0x00000000 },
1957ec681f3Smrg       { 20, 10, 0, -1 }},
196af69d88dSmrg      /* MESA_FORMAT_B10G10R10A2_UNORM */
1977ec681f3Smrg      {{ 0x3FF00000, 0x000FFC00, 0x000003FF, 0xC0000000 },
1987ec681f3Smrg       { 20, 10, 0, 30 }},
19901e04c3fSmrg      /* MESA_FORMAT_R8G8B8A8_UNORM */
2007ec681f3Smrg      {{ 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 },
2017ec681f3Smrg       { 0, 8, 16, 24 }},
20201e04c3fSmrg      /* MESA_FORMAT_R8G8B8X8_UNORM */
2037ec681f3Smrg      {{ 0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000 },
2047ec681f3Smrg       { 0, 8, 16, -1 }},
20501e04c3fSmrg      /* MESA_FORMAT_R10G10B10X2_UNORM */
2067ec681f3Smrg      {{ 0x000003FF, 0x000FFC00, 0x3FF00000, 0x00000000 },
2077ec681f3Smrg       { 0, 10, 20, -1 }},
20801e04c3fSmrg      /* MESA_FORMAT_R10G10B10A2_UNORM */
2097ec681f3Smrg      {{ 0x000003FF, 0x000FFC00, 0x3FF00000, 0xC0000000 },
2107ec681f3Smrg       { 0, 10, 20, 30 }},
2117ec681f3Smrg      /* MESA_FORMAT_RGBX_FLOAT16 */
2127ec681f3Smrg      {{ 0, 0, 0, 0},
2137ec681f3Smrg       { 0, 16, 32, -1 }},
2147ec681f3Smrg      /* MESA_FORMAT_RGBA_FLOAT16 */
2157ec681f3Smrg      {{ 0, 0, 0, 0},
2167ec681f3Smrg       { 0, 16, 32, 48 }},
2177117f1b4Smrg   };
2187117f1b4Smrg
219c1f859d4Smrg   const uint32_t * masks;
2207ec681f3Smrg   const int * shifts;
221c1f859d4Smrg   __DRIconfig **configs, **c;
2223464ebd5Sriastradh   struct gl_config *modes;
2234a49301eSmrg   unsigned i, j, k, h;
224c1f859d4Smrg   unsigned num_modes;
225cdc920a0Smrg   unsigned num_accum_bits = (enable_accum) ? 2 : 1;
226af69d88dSmrg   int red_bits;
227af69d88dSmrg   int green_bits;
228af69d88dSmrg   int blue_bits;
229af69d88dSmrg   int alpha_bits;
230af69d88dSmrg   bool is_srgb;
2317ec681f3Smrg   bool is_float;
232af69d88dSmrg
233af69d88dSmrg   switch (format) {
234af69d88dSmrg   case MESA_FORMAT_B5G6R5_UNORM:
2357ec681f3Smrg      masks = format_table[0].masks;
2367ec681f3Smrg      shifts = format_table[0].shifts;
237af69d88dSmrg      break;
238af69d88dSmrg   case MESA_FORMAT_B8G8R8X8_UNORM:
23901e04c3fSmrg   case MESA_FORMAT_B8G8R8X8_SRGB:
2407ec681f3Smrg      masks = format_table[1].masks;
2417ec681f3Smrg      shifts = format_table[1].shifts;
242af69d88dSmrg      break;
243af69d88dSmrg   case MESA_FORMAT_B8G8R8A8_UNORM:
244af69d88dSmrg   case MESA_FORMAT_B8G8R8A8_SRGB:
2457ec681f3Smrg      masks = format_table[2].masks;
2467ec681f3Smrg      shifts = format_table[2].shifts;
247af69d88dSmrg      break;
24801e04c3fSmrg   case MESA_FORMAT_R8G8B8A8_UNORM:
24901e04c3fSmrg   case MESA_FORMAT_R8G8B8A8_SRGB:
2507ec681f3Smrg      masks = format_table[5].masks;
2517ec681f3Smrg      shifts = format_table[5].shifts;
25201e04c3fSmrg      break;
25301e04c3fSmrg   case MESA_FORMAT_R8G8B8X8_UNORM:
2547ec681f3Smrg   case MESA_FORMAT_R8G8B8X8_SRGB:
2557ec681f3Smrg      masks = format_table[6].masks;
2567ec681f3Smrg      shifts = format_table[6].shifts;
25701e04c3fSmrg      break;
258af69d88dSmrg   case MESA_FORMAT_B10G10R10X2_UNORM:
2597ec681f3Smrg      masks = format_table[3].masks;
2607ec681f3Smrg      shifts = format_table[3].shifts;
261af69d88dSmrg      break;
262af69d88dSmrg   case MESA_FORMAT_B10G10R10A2_UNORM:
2637ec681f3Smrg      masks = format_table[4].masks;
2647ec681f3Smrg      shifts = format_table[4].shifts;
2657ec681f3Smrg      break;
2667ec681f3Smrg   case MESA_FORMAT_RGBX_FLOAT16:
2677ec681f3Smrg      masks = format_table[9].masks;
2687ec681f3Smrg      shifts = format_table[9].shifts;
2697ec681f3Smrg      break;
2707ec681f3Smrg   case MESA_FORMAT_RGBA_FLOAT16:
2717ec681f3Smrg      masks = format_table[10].masks;
2727ec681f3Smrg      shifts = format_table[10].shifts;
273af69d88dSmrg      break;
27401e04c3fSmrg   case MESA_FORMAT_R10G10B10X2_UNORM:
2757ec681f3Smrg      masks = format_table[7].masks;
2767ec681f3Smrg      shifts = format_table[7].shifts;
27701e04c3fSmrg      break;
27801e04c3fSmrg   case MESA_FORMAT_R10G10B10A2_UNORM:
2797ec681f3Smrg      masks = format_table[8].masks;
2807ec681f3Smrg      shifts = format_table[8].shifts;
28101e04c3fSmrg      break;
282af69d88dSmrg   default:
283af69d88dSmrg      fprintf(stderr, "[%s:%u] Unknown framebuffer type %s (%d).\n",
28401e04c3fSmrg              __func__, __LINE__,
285af69d88dSmrg              _mesa_get_format_name(format), format);
286af69d88dSmrg      return NULL;
287c1f859d4Smrg   }
288c1f859d4Smrg
289af69d88dSmrg   red_bits = _mesa_get_format_bits(format, GL_RED_BITS);
290af69d88dSmrg   green_bits = _mesa_get_format_bits(format, GL_GREEN_BITS);
291af69d88dSmrg   blue_bits = _mesa_get_format_bits(format, GL_BLUE_BITS);
292af69d88dSmrg   alpha_bits = _mesa_get_format_bits(format, GL_ALPHA_BITS);
2937ec681f3Smrg   is_srgb = _mesa_is_format_srgb(format);
2947ec681f3Smrg   is_float = _mesa_get_format_datatype(format) == GL_FLOAT;
2957117f1b4Smrg
2964a49301eSmrg   num_modes = num_depth_stencil_bits * num_db_modes * num_accum_bits * num_msaa_modes;
29701e04c3fSmrg   configs = calloc(num_modes + 1, sizeof *configs);
298c1f859d4Smrg   if (configs == NULL)
299c1f859d4Smrg       return NULL;
3007117f1b4Smrg
301c1f859d4Smrg    c = configs;
3027117f1b4Smrg    for ( k = 0 ; k < num_depth_stencil_bits ; k++ ) {
3037117f1b4Smrg	for ( i = 0 ; i < num_db_modes ; i++ ) {
3044a49301eSmrg	    for ( h = 0 ; h < num_msaa_modes; h++ ) {
3054a49301eSmrg	    	for ( j = 0 ; j < num_accum_bits ; j++ ) {
30601e04c3fSmrg		    if (color_depth_match &&
30701e04c3fSmrg			(depth_bits[k] || stencil_bits[k])) {
30801e04c3fSmrg			/* Depth can really only be 0, 16, 24, or 32. A 32-bit
30901e04c3fSmrg			 * color format still matches 24-bit depth, as there
31001e04c3fSmrg			 * is an implicit 8-bit stencil. So really we just
31101e04c3fSmrg			 * need to make sure that color/depth are both 16 or
31201e04c3fSmrg			 * both non-16.
31301e04c3fSmrg			 */
31401e04c3fSmrg			if ((depth_bits[k] + stencil_bits[k] == 16) !=
31501e04c3fSmrg			    (red_bits + green_bits + blue_bits + alpha_bits == 16))
31601e04c3fSmrg			    continue;
31701e04c3fSmrg		    }
31801e04c3fSmrg
319cdc920a0Smrg		    *c = malloc (sizeof **c);
3204a49301eSmrg		    modes = &(*c)->modes;
3214a49301eSmrg		    c++;
3224a49301eSmrg
3234a49301eSmrg		    memset(modes, 0, sizeof *modes);
3247ec681f3Smrg		    modes->floatMode = is_float;
325af69d88dSmrg		    modes->redBits   = red_bits;
326af69d88dSmrg		    modes->greenBits = green_bits;
327af69d88dSmrg		    modes->blueBits  = blue_bits;
328af69d88dSmrg		    modes->alphaBits = alpha_bits;
3294a49301eSmrg		    modes->redMask   = masks[0];
3304a49301eSmrg		    modes->greenMask = masks[1];
3314a49301eSmrg		    modes->blueMask  = masks[2];
3324a49301eSmrg		    modes->alphaMask = masks[3];
3337ec681f3Smrg		    modes->redShift   = shifts[0];
3347ec681f3Smrg		    modes->greenShift = shifts[1];
3357ec681f3Smrg		    modes->blueShift  = shifts[2];
3367ec681f3Smrg		    modes->alphaShift = shifts[3];
3374a49301eSmrg		    modes->rgbBits   = modes->redBits + modes->greenBits
3384a49301eSmrg		    	+ modes->blueBits + modes->alphaBits;
3394a49301eSmrg
3404a49301eSmrg		    modes->accumRedBits   = 16 * j;
3414a49301eSmrg		    modes->accumGreenBits = 16 * j;
3424a49301eSmrg		    modes->accumBlueBits  = 16 * j;
3437ec681f3Smrg		    modes->accumAlphaBits = 16 * j;
3444a49301eSmrg
3454a49301eSmrg		    modes->stencilBits = stencil_bits[k];
3464a49301eSmrg		    modes->depthBits = depth_bits[k];
3474a49301eSmrg
34801e04c3fSmrg		    if (db_modes[i] == __DRI_ATTRIB_SWAP_NONE) {
3494a49301eSmrg		    	modes->doubleBufferMode = GL_FALSE;
35001e04c3fSmrg		        modes->swapMethod = __DRI_ATTRIB_SWAP_UNDEFINED;
3514a49301eSmrg		    }
3524a49301eSmrg		    else {
3534a49301eSmrg		    	modes->doubleBufferMode = GL_TRUE;
3544a49301eSmrg		    	modes->swapMethod = db_modes[i];
3554a49301eSmrg		    }
3564a49301eSmrg
3574a49301eSmrg		    modes->samples = msaa_samples[h];
3584a49301eSmrg
359af69d88dSmrg		    modes->sRGBCapable = is_srgb;
3604a49301eSmrg		}
3617117f1b4Smrg	    }
3627117f1b4Smrg	}
3637117f1b4Smrg    }
364c1f859d4Smrg    *c = NULL;
365c1f859d4Smrg
366c1f859d4Smrg    return configs;
367c1f859d4Smrg}
368c1f859d4Smrg
3694a49301eSmrg__DRIconfig **driConcatConfigs(__DRIconfig **a,
3704a49301eSmrg			       __DRIconfig **b)
371c1f859d4Smrg{
3724a49301eSmrg    __DRIconfig **all;
373c1f859d4Smrg    int i, j, index;
374c1f859d4Smrg
375af69d88dSmrg    if (a == NULL || a[0] == NULL)
376af69d88dSmrg       return b;
377af69d88dSmrg    else if (b == NULL || b[0] == NULL)
378af69d88dSmrg       return a;
379af69d88dSmrg
380c1f859d4Smrg    i = 0;
381c1f859d4Smrg    while (a[i] != NULL)
382c1f859d4Smrg	i++;
383c1f859d4Smrg    j = 0;
384c1f859d4Smrg    while (b[j] != NULL)
385c1f859d4Smrg	j++;
386c1f859d4Smrg
387cdc920a0Smrg    all = malloc((i + j + 1) * sizeof *all);
388c1f859d4Smrg    index = 0;
389c1f859d4Smrg    for (i = 0; a[i] != NULL; i++)
390c1f859d4Smrg	all[index++] = a[i];
391c1f859d4Smrg    for (j = 0; b[j] != NULL; j++)
392c1f859d4Smrg	all[index++] = b[j];
393c1f859d4Smrg    all[index++] = NULL;
394c1f859d4Smrg
395cdc920a0Smrg    free(a);
396cdc920a0Smrg    free(b);
397c1f859d4Smrg
398c1f859d4Smrg    return all;
399c1f859d4Smrg}
400c1f859d4Smrg
4017ec681f3Smrg#define __ATTRIB(attrib, field) case attrib: *value = config->modes.field; break
4023464ebd5Sriastradh
4033464ebd5Sriastradh/**
4043464ebd5Sriastradh * Return the value of a configuration attribute.  The attribute is
4053464ebd5Sriastradh * indicated by the index.
4063464ebd5Sriastradh */
407c1f859d4Smrgstatic int
408c1f859d4SmrgdriGetConfigAttribIndex(const __DRIconfig *config,
409c1f859d4Smrg			unsigned int index, unsigned int *value)
410c1f859d4Smrg{
4117ec681f3Smrg    switch (index + 1) {
4127ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_BUFFER_SIZE,			rgbBits);
4137ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_RED_SIZE,			redBits);
4147ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_GREEN_SIZE,			greenBits);
4157ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_BLUE_SIZE,			blueBits);
4167ec681f3Smrg    case __DRI_ATTRIB_LEVEL:
4177ec681f3Smrg    case __DRI_ATTRIB_LUMINANCE_SIZE:
4187ec681f3Smrg    case __DRI_ATTRIB_AUX_BUFFERS:
4197ec681f3Smrg        *value = 0;
4207ec681f3Smrg        break;
4217ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_ALPHA_SIZE,			alphaBits);
4227ec681f3Smrg    case __DRI_ATTRIB_ALPHA_MASK_SIZE:
4237ec681f3Smrg        /* I have no idea what this value was ever meant to mean, it's
4247ec681f3Smrg         * never been set to anything, just say 0.
4257ec681f3Smrg         */
4267ec681f3Smrg        *value = 0;
4277ec681f3Smrg        break;
4287ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_DEPTH_SIZE,			depthBits);
4297ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_STENCIL_SIZE,			stencilBits);
4307ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_ACCUM_RED_SIZE,		accumRedBits);
4317ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_ACCUM_GREEN_SIZE,		accumGreenBits);
4327ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_ACCUM_BLUE_SIZE,		accumBlueBits);
4337ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_ACCUM_ALPHA_SIZE,		accumAlphaBits);
4347ec681f3Smrg    case __DRI_ATTRIB_SAMPLE_BUFFERS:
4357ec681f3Smrg        *value = !!config->modes.samples;
4367ec681f3Smrg        break;
4377ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_SAMPLES,			samples);
438c1f859d4Smrg    case __DRI_ATTRIB_RENDER_TYPE:
4393464ebd5Sriastradh        /* no support for color index mode */
440cdc920a0Smrg	*value = __DRI_ATTRIB_RGBA_BIT;
4417ec681f3Smrg        if (config->modes.floatMode)
4427ec681f3Smrg            *value |= __DRI_ATTRIB_FLOAT_BIT;
443c1f859d4Smrg	break;
444c1f859d4Smrg    case __DRI_ATTRIB_CONFIG_CAVEAT:
4457ec681f3Smrg	if (config->modes.accumRedBits != 0)
446c1f859d4Smrg	    *value = __DRI_ATTRIB_SLOW_BIT;
447c1f859d4Smrg	else
448c1f859d4Smrg	    *value = 0;
449c1f859d4Smrg	break;
4507ec681f3Smrg    case __DRI_ATTRIB_CONFORMANT:
4517ec681f3Smrg        *value = GL_TRUE;
4527ec681f3Smrg        break;
4537ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_DOUBLE_BUFFER,		doubleBufferMode);
4547ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_STEREO,			stereoMode);
4557ec681f3Smrg    case __DRI_ATTRIB_TRANSPARENT_TYPE:
4567ec681f3Smrg    case __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE: /* horrible bc hack */
4577ec681f3Smrg        *value = GLX_NONE;
4587ec681f3Smrg        break;
4597ec681f3Smrg    case __DRI_ATTRIB_TRANSPARENT_RED_VALUE:
4607ec681f3Smrg    case __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE:
4617ec681f3Smrg    case __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE:
4627ec681f3Smrg    case __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE:
4637ec681f3Smrg        *value = GLX_DONT_CARE;
4647ec681f3Smrg        break;
4657ec681f3Smrg    case __DRI_ATTRIB_FLOAT_MODE:
4667ec681f3Smrg        *value = config->modes.floatMode;
4677ec681f3Smrg        break;
4687ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_RED_MASK,			redMask);
4697ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_GREEN_MASK,			greenMask);
4707ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_BLUE_MASK,			blueMask);
4717ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_ALPHA_MASK,			alphaMask);
4727ec681f3Smrg    case __DRI_ATTRIB_MAX_PBUFFER_WIDTH:
4737ec681f3Smrg    case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT:
4747ec681f3Smrg    case __DRI_ATTRIB_MAX_PBUFFER_PIXELS:
4757ec681f3Smrg    case __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH:
4767ec681f3Smrg    case __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT:
4777ec681f3Smrg    case __DRI_ATTRIB_VISUAL_SELECT_GROUP:
4787ec681f3Smrg        *value = 0;
4797ec681f3Smrg        break;
4807ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_SWAP_METHOD,			swapMethod);
4817ec681f3Smrg    case __DRI_ATTRIB_MAX_SWAP_INTERVAL:
4827ec681f3Smrg        *value = INT_MAX;
4837ec681f3Smrg        break;
4847ec681f3Smrg    case __DRI_ATTRIB_MIN_SWAP_INTERVAL:
4857ec681f3Smrg        *value = 0;
4867ec681f3Smrg        break;
4877ec681f3Smrg    case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
4887ec681f3Smrg    case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
4897ec681f3Smrg    case __DRI_ATTRIB_YINVERTED:
4907ec681f3Smrg        *value = GL_TRUE;
4917ec681f3Smrg        break;
4927ec681f3Smrg    case __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE:
4937ec681f3Smrg        *value = GL_FALSE;
4947ec681f3Smrg        break;
4957ec681f3Smrg    case __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS:
4967ec681f3Smrg        *value = __DRI_ATTRIB_TEXTURE_1D_BIT |
4977ec681f3Smrg                 __DRI_ATTRIB_TEXTURE_2D_BIT |
4987ec681f3Smrg                 __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT;
4997ec681f3Smrg        break;
5007ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE,	sRGBCapable);
5017ec681f3Smrg    case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER:
5027ec681f3Smrg        *value = GL_FALSE;
5037ec681f3Smrg        break;
5047ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_RED_SHIFT,			redShift);
5057ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_GREEN_SHIFT,			greenShift);
5067ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_BLUE_SHIFT,			blueShift);
5077ec681f3Smrg    __ATTRIB(__DRI_ATTRIB_ALPHA_SHIFT,			alphaShift);
508c1f859d4Smrg    default:
5097ec681f3Smrg        /* XXX log an error or smth */
5107ec681f3Smrg        return GL_FALSE;
511c1f859d4Smrg    }
5127117f1b4Smrg
5137117f1b4Smrg    return GL_TRUE;
5147117f1b4Smrg}
515c1f859d4Smrg
5163464ebd5Sriastradh/**
5173464ebd5Sriastradh * Get the value of a configuration attribute.
5183464ebd5Sriastradh * \param attrib  the attribute (one of the _DRI_ATTRIB_x tokens)
5193464ebd5Sriastradh * \param value  returns the attribute's value
5203464ebd5Sriastradh * \return 1 for success, 0 for failure
5213464ebd5Sriastradh */
522c1f859d4Smrgint
523c1f859d4SmrgdriGetConfigAttrib(const __DRIconfig *config,
524c1f859d4Smrg		   unsigned int attrib, unsigned int *value)
525c1f859d4Smrg{
5267ec681f3Smrg    return driGetConfigAttribIndex(config, attrib - 1, value);
527c1f859d4Smrg}
528c1f859d4Smrg
5293464ebd5Sriastradh
5303464ebd5Sriastradh/**
5313464ebd5Sriastradh * Get a configuration attribute name and value, given an index.
5323464ebd5Sriastradh * \param index  which field of the __DRIconfig to query
5333464ebd5Sriastradh * \param attrib  returns the attribute name (one of the _DRI_ATTRIB_x tokens)
5343464ebd5Sriastradh * \param value  returns the attribute's value
5353464ebd5Sriastradh * \return 1 for success, 0 for failure
5363464ebd5Sriastradh */
537c1f859d4Smrgint
538c1f859d4SmrgdriIndexConfigAttrib(const __DRIconfig *config, int index,
539c1f859d4Smrg		     unsigned int *attrib, unsigned int *value)
540c1f859d4Smrg{
5417ec681f3Smrg    if (driGetConfigAttribIndex(config, index, value)) {
5427ec681f3Smrg        *attrib = index + 1;
5437ec681f3Smrg        return GL_TRUE;
544c1f859d4Smrg    }
545c1f859d4Smrg
546c1f859d4Smrg    return GL_FALSE;
547c1f859d4Smrg}
548af69d88dSmrg
549af69d88dSmrg/**
550af69d88dSmrg * Implement queries for values that are common across all Mesa drivers
551af69d88dSmrg *
552af69d88dSmrg * Currently only the following queries are supported by this function:
553af69d88dSmrg *
554af69d88dSmrg *     - \c __DRI2_RENDERER_VERSION
55501e04c3fSmrg *     - \c __DRI2_RENDERER_PREFERRED_PROFILE
556af69d88dSmrg *     - \c __DRI2_RENDERER_OPENGL_CORE_PROFILE_VERSION
557af69d88dSmrg *     - \c __DRI2_RENDERER_OPENGL_COMPATIBLITY_PROFILE_VERSION
558af69d88dSmrg *     - \c __DRI2_RENDERER_ES_PROFILE_VERSION
559af69d88dSmrg *     - \c __DRI2_RENDERER_ES2_PROFILE_VERSION
560af69d88dSmrg *
561af69d88dSmrg * \returns
562af69d88dSmrg * Zero if a recognized value of \c param is supplied, -1 otherwise.
563af69d88dSmrg */
564af69d88dSmrgint
565af69d88dSmrgdriQueryRendererIntegerCommon(__DRIscreen *psp, int param, unsigned int *value)
566af69d88dSmrg{
567af69d88dSmrg   switch (param) {
568af69d88dSmrg   case __DRI2_RENDERER_VERSION: {
569af69d88dSmrg      static const char *const ver = PACKAGE_VERSION;
570af69d88dSmrg      char *endptr;
571af69d88dSmrg      int v[3];
572af69d88dSmrg
573af69d88dSmrg      v[0] = strtol(ver, &endptr, 10);
574af69d88dSmrg      assert(endptr[0] == '.');
575af69d88dSmrg      if (endptr[0] != '.')
576af69d88dSmrg         return -1;
577af69d88dSmrg
578af69d88dSmrg      v[1] = strtol(endptr + 1, &endptr, 10);
579af69d88dSmrg      assert(endptr[0] == '.');
580af69d88dSmrg      if (endptr[0] != '.')
581af69d88dSmrg         return -1;
582af69d88dSmrg
583af69d88dSmrg      v[2] = strtol(endptr + 1, &endptr, 10);
584af69d88dSmrg
585af69d88dSmrg      value[0] = v[0];
586af69d88dSmrg      value[1] = v[1];
587af69d88dSmrg      value[2] = v[2];
588af69d88dSmrg      return 0;
589af69d88dSmrg   }
590af69d88dSmrg   case __DRI2_RENDERER_PREFERRED_PROFILE:
591af69d88dSmrg      value[0] = (psp->max_gl_core_version != 0)
592af69d88dSmrg         ? (1U << __DRI_API_OPENGL_CORE) : (1U << __DRI_API_OPENGL);
593af69d88dSmrg      return 0;
594af69d88dSmrg   case __DRI2_RENDERER_OPENGL_CORE_PROFILE_VERSION:
595af69d88dSmrg      value[0] = psp->max_gl_core_version / 10;
596af69d88dSmrg      value[1] = psp->max_gl_core_version % 10;
597af69d88dSmrg      return 0;
598af69d88dSmrg   case __DRI2_RENDERER_OPENGL_COMPATIBILITY_PROFILE_VERSION:
599af69d88dSmrg      value[0] = psp->max_gl_compat_version / 10;
600af69d88dSmrg      value[1] = psp->max_gl_compat_version % 10;
601af69d88dSmrg      return 0;
602af69d88dSmrg   case __DRI2_RENDERER_OPENGL_ES_PROFILE_VERSION:
603af69d88dSmrg      value[0] = psp->max_gl_es1_version / 10;
604af69d88dSmrg      value[1] = psp->max_gl_es1_version % 10;
605af69d88dSmrg      return 0;
606af69d88dSmrg   case __DRI2_RENDERER_OPENGL_ES2_PROFILE_VERSION:
607af69d88dSmrg      value[0] = psp->max_gl_es2_version / 10;
608af69d88dSmrg      value[1] = psp->max_gl_es2_version % 10;
609af69d88dSmrg      return 0;
610af69d88dSmrg   default:
611af69d88dSmrg      break;
612af69d88dSmrg   }
613af69d88dSmrg
614af69d88dSmrg   return -1;
615af69d88dSmrg}
616