• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2009, VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 /*
28  * Author: Keith Whitwell <keithw@vmware.com>
29  * Author: Jakob Bornecrantz <wallbraker@gmail.com>
30  */
31 
32 #include "dri_screen.h"
33 #include "dri_context.h"
34 #include "dri_helpers.h"
35 
36 #include "util/u_inlines.h"
37 #include "pipe/p_screen.h"
38 #include "pipe/p_format.h"
39 #include "pipe-loader/pipe_loader.h"
40 #include "state_tracker/st_gl_api.h" /* for st_gl_api_create */
41 #include "frontend/drm_driver.h"
42 
43 #include "util/u_debug.h"
44 #include "util/u_driconf.h"
45 #include "util/format/u_format_s3tc.h"
46 
47 #define MSAA_VISUAL_MAX_SAMPLES 32
48 
49 #undef false
50 
51 const __DRIconfigOptionsExtension gallium_config_options = {
52    .base = { __DRI_CONFIG_OPTIONS, 2 },
53    .getXml = pipe_loader_get_driinfo_xml
54 };
55 
56 #define false 0
57 
58 void
dri_init_options(struct dri_screen * screen)59 dri_init_options(struct dri_screen *screen)
60 {
61    pipe_loader_config_options(screen->dev);
62 
63    struct st_config_options *options = &screen->options;
64    const struct driOptionCache *optionCache = &screen->dev->option_cache;
65 
66    u_driconf_fill_st_options(options, optionCache);
67 }
68 
69 static unsigned
dri_loader_get_cap(struct dri_screen * screen,enum dri_loader_cap cap)70 dri_loader_get_cap(struct dri_screen *screen, enum dri_loader_cap cap)
71 {
72    const __DRIdri2LoaderExtension *dri2_loader = screen->sPriv->dri2.loader;
73    const __DRIimageLoaderExtension *image_loader = screen->sPriv->image.loader;
74 
75    if (dri2_loader && dri2_loader->base.version >= 4 &&
76        dri2_loader->getCapability)
77       return dri2_loader->getCapability(screen->sPriv->loaderPrivate, cap);
78 
79    if (image_loader && image_loader->base.version >= 2 &&
80        image_loader->getCapability)
81       return image_loader->getCapability(screen->sPriv->loaderPrivate, cap);
82 
83    return 0;
84 }
85 
86 /**
87  * Creates a set of \c struct gl_config that a driver will expose.
88  *
89  * A set of \c struct gl_config will be created based on the supplied
90  * parameters.  The number of modes processed will be 2 *
91  * \c num_depth_stencil_bits * \c num_db_modes.
92  *
93  * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
94  * \c db_modes, and \c visType into each \c struct gl_config element.
95  * However, the meanings of \c fb_format and \c fb_type require further
96  * explanation.  The \c fb_format specifies which color components are in
97  * each pixel and what the default order is.  For example, \c GL_RGB specifies
98  * that red, green, blue are available and red is in the "most significant"
99  * position and blue is in the "least significant".  The \c fb_type specifies
100  * the bit sizes of each component and the actual ordering.  For example, if
101  * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
102  * are the blue value, bits [10:5] are the green value, and bits [4:0] are
103  * the red value.
104  *
105  * One sublte issue is the combination of \c GL_RGB  or \c GL_BGR and either
106  * of the \c GL_UNSIGNED_INT_8_8_8_8 modes.  The resulting mask values in the
107  * \c struct gl_config structure is \b identical to the \c GL_RGBA or
108  * \c GL_BGRA case, except the \c alphaMask is zero.  This means that, as
109  * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
110  * still uses 32-bits.
111  *
112  * If in doubt, look at the tables used in the function.
113  *
114  * \param ptr_to_modes  Pointer to a pointer to a linked list of
115  *                      \c struct gl_config.  Upon completion, a pointer to
116  *                      the next element to be process will be stored here.
117  *                      If the function fails and returns \c GL_FALSE, this
118  *                      value will be unmodified, but some elements in the
119  *                      linked list may be modified.
120  * \param format        Mesa mesa_format enum describing the pixel format
121  * \param depth_bits    Array of depth buffer sizes to be exposed.
122  * \param stencil_bits  Array of stencil buffer sizes to be exposed.
123  * \param num_depth_stencil_bits  Number of entries in both \c depth_bits and
124  *                      \c stencil_bits.
125  * \param db_modes      Array of buffer swap modes.  If an element has a
126  *                      value of \c __DRI_ATTRIB_SWAP_NONE, then it
127  *                      represents a single-buffered mode.  Other valid
128  *                      values are \c __DRI_ATTRIB_SWAP_EXCHANGE,
129  *                      \c __DRI_ATTRIB_SWAP_COPY, and \c __DRI_ATTRIB_SWAP_UNDEFINED.
130  *                      They represent the respective GLX values as in
131  *                      the GLX_OML_swap_method extension spec.
132  * \param num_db_modes  Number of entries in \c db_modes.
133  * \param msaa_samples  Array of msaa sample count. 0 represents a visual
134  *                      without a multisample buffer.
135  * \param num_msaa_modes Number of entries in \c msaa_samples.
136  * \param enable_accum  Add an accum buffer to the configs
137  * \param color_depth_match Whether the color depth must match the zs depth
138  *                          This forces 32-bit color to have 24-bit depth, and
139  *                          16-bit color to have 16-bit depth.
140  *
141  * \returns
142  * Pointer to any array of pointers to the \c __DRIconfig structures created
143  * for the specified formats.  If there is an error, \c NULL is returned.
144  * Currently the only cause of failure is a bad parameter (i.e., unsupported
145  * \c format).
146  */
147 static __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)148 driCreateConfigs(mesa_format format,
149 		 const uint8_t * depth_bits, const uint8_t * stencil_bits,
150 		 unsigned num_depth_stencil_bits,
151 		 const GLenum * db_modes, unsigned num_db_modes,
152 		 const uint8_t * msaa_samples, unsigned num_msaa_modes,
153 		 GLboolean enable_accum, GLboolean color_depth_match)
154 {
155    static const struct {
156       uint32_t masks[4];
157       int shifts[4];
158    } format_table[] = {
159       /* MESA_FORMAT_B5G6R5_UNORM */
160       {{ 0x0000F800, 0x000007E0, 0x0000001F, 0x00000000 },
161        { 11, 5, 0, -1 }},
162       /* MESA_FORMAT_B8G8R8X8_UNORM */
163       {{ 0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000 },
164        { 16, 8, 0, -1 }},
165       /* MESA_FORMAT_B8G8R8A8_UNORM */
166       {{ 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 },
167        { 16, 8, 0, 24 }},
168       /* MESA_FORMAT_B10G10R10X2_UNORM */
169       {{ 0x3FF00000, 0x000FFC00, 0x000003FF, 0x00000000 },
170        { 20, 10, 0, -1 }},
171       /* MESA_FORMAT_B10G10R10A2_UNORM */
172       {{ 0x3FF00000, 0x000FFC00, 0x000003FF, 0xC0000000 },
173        { 20, 10, 0, 30 }},
174       /* MESA_FORMAT_R8G8B8A8_UNORM */
175       {{ 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 },
176        { 0, 8, 16, 24 }},
177       /* MESA_FORMAT_R8G8B8X8_UNORM */
178       {{ 0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000 },
179        { 0, 8, 16, -1 }},
180       /* MESA_FORMAT_R10G10B10X2_UNORM */
181       {{ 0x000003FF, 0x000FFC00, 0x3FF00000, 0x00000000 },
182        { 0, 10, 20, -1 }},
183       /* MESA_FORMAT_R10G10B10A2_UNORM */
184       {{ 0x000003FF, 0x000FFC00, 0x3FF00000, 0xC0000000 },
185        { 0, 10, 20, 30 }},
186       /* MESA_FORMAT_RGBX_FLOAT16 */
187       {{ 0, 0, 0, 0},
188        { 0, 16, 32, -1 }},
189       /* MESA_FORMAT_RGBA_FLOAT16 */
190       {{ 0, 0, 0, 0},
191        { 0, 16, 32, 48 }},
192    };
193 
194    const uint32_t * masks;
195    const int * shifts;
196    __DRIconfig **configs, **c;
197    struct gl_config *modes;
198    unsigned i, j, k, h;
199    unsigned num_modes;
200    unsigned num_accum_bits = (enable_accum) ? 2 : 1;
201    int red_bits;
202    int green_bits;
203    int blue_bits;
204    int alpha_bits;
205    bool is_srgb;
206    bool is_float;
207 
208    switch (format) {
209    case MESA_FORMAT_B5G6R5_UNORM:
210       masks = format_table[0].masks;
211       shifts = format_table[0].shifts;
212       break;
213    case MESA_FORMAT_B8G8R8X8_UNORM:
214    case MESA_FORMAT_B8G8R8X8_SRGB:
215       masks = format_table[1].masks;
216       shifts = format_table[1].shifts;
217       break;
218    case MESA_FORMAT_B8G8R8A8_UNORM:
219    case MESA_FORMAT_B8G8R8A8_SRGB:
220       masks = format_table[2].masks;
221       shifts = format_table[2].shifts;
222       break;
223    case MESA_FORMAT_R8G8B8A8_UNORM:
224    case MESA_FORMAT_R8G8B8A8_SRGB:
225       masks = format_table[5].masks;
226       shifts = format_table[5].shifts;
227       break;
228    case MESA_FORMAT_R8G8B8X8_UNORM:
229    case MESA_FORMAT_R8G8B8X8_SRGB:
230       masks = format_table[6].masks;
231       shifts = format_table[6].shifts;
232       break;
233    case MESA_FORMAT_B10G10R10X2_UNORM:
234       masks = format_table[3].masks;
235       shifts = format_table[3].shifts;
236       break;
237    case MESA_FORMAT_B10G10R10A2_UNORM:
238       masks = format_table[4].masks;
239       shifts = format_table[4].shifts;
240       break;
241    case MESA_FORMAT_RGBX_FLOAT16:
242       masks = format_table[9].masks;
243       shifts = format_table[9].shifts;
244       break;
245    case MESA_FORMAT_RGBA_FLOAT16:
246       masks = format_table[10].masks;
247       shifts = format_table[10].shifts;
248       break;
249    case MESA_FORMAT_R10G10B10X2_UNORM:
250       masks = format_table[7].masks;
251       shifts = format_table[7].shifts;
252       break;
253    case MESA_FORMAT_R10G10B10A2_UNORM:
254       masks = format_table[8].masks;
255       shifts = format_table[8].shifts;
256       break;
257    default:
258       fprintf(stderr, "[%s:%u] Unknown framebuffer type %s (%d).\n",
259               __func__, __LINE__,
260               _mesa_get_format_name(format), format);
261       return NULL;
262    }
263 
264    red_bits = _mesa_get_format_bits(format, GL_RED_BITS);
265    green_bits = _mesa_get_format_bits(format, GL_GREEN_BITS);
266    blue_bits = _mesa_get_format_bits(format, GL_BLUE_BITS);
267    alpha_bits = _mesa_get_format_bits(format, GL_ALPHA_BITS);
268    is_srgb = _mesa_is_format_srgb(format);
269    is_float = _mesa_get_format_datatype(format) == GL_FLOAT;
270 
271    num_modes = num_depth_stencil_bits * num_db_modes * num_accum_bits * num_msaa_modes;
272    configs = calloc(num_modes + 1, sizeof *configs);
273    if (configs == NULL)
274        return NULL;
275 
276     c = configs;
277     for ( k = 0 ; k < num_depth_stencil_bits ; k++ ) {
278 	for ( i = 0 ; i < num_db_modes ; i++ ) {
279 	    for ( h = 0 ; h < num_msaa_modes; h++ ) {
280 	    	for ( j = 0 ; j < num_accum_bits ; j++ ) {
281 		    if (color_depth_match &&
282 			(depth_bits[k] || stencil_bits[k])) {
283 			/* Depth can really only be 0, 16, 24, or 32. A 32-bit
284 			 * color format still matches 24-bit depth, as there
285 			 * is an implicit 8-bit stencil. So really we just
286 			 * need to make sure that color/depth are both 16 or
287 			 * both non-16.
288 			 */
289 			if ((depth_bits[k] + stencil_bits[k] == 16) !=
290 			    (red_bits + green_bits + blue_bits + alpha_bits == 16))
291 			    continue;
292 		    }
293 
294 		    *c = malloc (sizeof **c);
295 		    modes = &(*c)->modes;
296 		    c++;
297 
298 		    memset(modes, 0, sizeof *modes);
299 		    modes->floatMode = is_float;
300 		    modes->redBits   = red_bits;
301 		    modes->greenBits = green_bits;
302 		    modes->blueBits  = blue_bits;
303 		    modes->alphaBits = alpha_bits;
304 		    modes->redMask   = masks[0];
305 		    modes->greenMask = masks[1];
306 		    modes->blueMask  = masks[2];
307 		    modes->alphaMask = masks[3];
308 		    modes->redShift   = shifts[0];
309 		    modes->greenShift = shifts[1];
310 		    modes->blueShift  = shifts[2];
311 		    modes->alphaShift = shifts[3];
312 		    modes->rgbBits   = modes->redBits + modes->greenBits
313 		    	+ modes->blueBits + modes->alphaBits;
314 
315 		    modes->accumRedBits   = 16 * j;
316 		    modes->accumGreenBits = 16 * j;
317 		    modes->accumBlueBits  = 16 * j;
318 		    modes->accumAlphaBits = 16 * j;
319 
320 		    modes->stencilBits = stencil_bits[k];
321 		    modes->depthBits = depth_bits[k];
322 
323 		    if (db_modes[i] == __DRI_ATTRIB_SWAP_NONE) {
324 		    	modes->doubleBufferMode = GL_FALSE;
325 		        modes->swapMethod = __DRI_ATTRIB_SWAP_UNDEFINED;
326 		    }
327 		    else {
328 		    	modes->doubleBufferMode = GL_TRUE;
329 		    	modes->swapMethod = db_modes[i];
330 		    }
331 
332 		    modes->samples = msaa_samples[h];
333 
334 		    modes->sRGBCapable = is_srgb;
335 		}
336 	    }
337 	}
338     }
339     *c = NULL;
340 
341     return configs;
342 }
343 
344 static __DRIconfig **
driConcatConfigs(__DRIconfig ** a,__DRIconfig ** b)345 driConcatConfigs(__DRIconfig **a, __DRIconfig **b)
346 {
347     __DRIconfig **all;
348     int i, j, index;
349 
350     if (a == NULL || a[0] == NULL)
351        return b;
352     else if (b == NULL || b[0] == NULL)
353        return a;
354 
355     i = 0;
356     while (a[i] != NULL)
357 	i++;
358     j = 0;
359     while (b[j] != NULL)
360 	j++;
361 
362     all = malloc((i + j + 1) * sizeof *all);
363     index = 0;
364     for (i = 0; a[i] != NULL; i++)
365 	all[index++] = a[i];
366     for (j = 0; b[j] != NULL; j++)
367 	all[index++] = b[j];
368     all[index++] = NULL;
369 
370     free(a);
371     free(b);
372 
373     return all;
374 }
375 
376 
377 static const __DRIconfig **
dri_fill_in_modes(struct dri_screen * screen)378 dri_fill_in_modes(struct dri_screen *screen)
379 {
380    static const mesa_format mesa_formats[] = {
381       MESA_FORMAT_B10G10R10A2_UNORM,
382       MESA_FORMAT_B10G10R10X2_UNORM,
383       MESA_FORMAT_R10G10B10A2_UNORM,
384       MESA_FORMAT_R10G10B10X2_UNORM,
385       MESA_FORMAT_B8G8R8A8_UNORM,
386       MESA_FORMAT_B8G8R8X8_UNORM,
387       MESA_FORMAT_B8G8R8A8_SRGB,
388       MESA_FORMAT_B8G8R8X8_SRGB,
389       MESA_FORMAT_B5G6R5_UNORM,
390       MESA_FORMAT_RGBA_FLOAT16,
391       MESA_FORMAT_RGBX_FLOAT16,
392 
393       /* The 32-bit RGBA format must not precede the 32-bit BGRA format.
394        * Likewise for RGBX and BGRX.  Otherwise, the GLX client and the GLX
395        * server may disagree on which format the GLXFBConfig represents,
396        * resulting in swapped color channels.
397        *
398        * The problem, as of 2017-05-30:
399        * When matching a GLXFBConfig to a __DRIconfig, GLX ignores the channel
400        * order and chooses the first __DRIconfig with the expected channel
401        * sizes. Specifically, GLX compares the GLXFBConfig's and __DRIconfig's
402        * __DRI_ATTRIB_{CHANNEL}_SIZE but ignores __DRI_ATTRIB_{CHANNEL}_MASK.
403        *
404        * EGL does not suffer from this problem. It correctly compares the
405        * channel masks when matching EGLConfig to __DRIconfig.
406        */
407 
408       /* Required by Android, for HAL_PIXEL_FORMAT_RGBA_8888. */
409       MESA_FORMAT_R8G8B8A8_UNORM,
410 
411       /* Required by Android, for HAL_PIXEL_FORMAT_RGBX_8888. */
412       MESA_FORMAT_R8G8B8X8_UNORM,
413 
414       /* Required by Android, for HAL_PIXEL_FORMAT_RGBA_8888. */
415       MESA_FORMAT_R8G8B8A8_SRGB,
416 
417       /* Required by Android, for HAL_PIXEL_FORMAT_RGBX_8888. */
418       MESA_FORMAT_R8G8B8X8_SRGB,
419    };
420    static const enum pipe_format pipe_formats[] = {
421       PIPE_FORMAT_B10G10R10A2_UNORM,
422       PIPE_FORMAT_B10G10R10X2_UNORM,
423       PIPE_FORMAT_R10G10B10A2_UNORM,
424       PIPE_FORMAT_R10G10B10X2_UNORM,
425       PIPE_FORMAT_BGRA8888_UNORM,
426       PIPE_FORMAT_BGRX8888_UNORM,
427       PIPE_FORMAT_BGRA8888_SRGB,
428       PIPE_FORMAT_BGRX8888_SRGB,
429       PIPE_FORMAT_B5G6R5_UNORM,
430       PIPE_FORMAT_R16G16B16A16_FLOAT,
431       PIPE_FORMAT_R16G16B16X16_FLOAT,
432       PIPE_FORMAT_RGBA8888_UNORM,
433       PIPE_FORMAT_RGBX8888_UNORM,
434       PIPE_FORMAT_RGBA8888_SRGB,
435       PIPE_FORMAT_RGBX8888_SRGB,
436    };
437    mesa_format format;
438    __DRIconfig **configs = NULL;
439    uint8_t depth_bits_array[5];
440    uint8_t stencil_bits_array[5];
441    unsigned depth_buffer_factor;
442    unsigned msaa_samples_max;
443    unsigned i;
444    struct pipe_screen *p_screen = screen->base.screen;
445    bool pf_z16, pf_x8z24, pf_z24x8, pf_s8z24, pf_z24s8, pf_z32;
446    bool mixed_color_depth;
447    bool allow_rgba_ordering;
448    bool allow_rgb10;
449    bool allow_fp16;
450 
451    static const GLenum back_buffer_modes[] = {
452       __DRI_ATTRIB_SWAP_NONE, __DRI_ATTRIB_SWAP_UNDEFINED,
453       __DRI_ATTRIB_SWAP_COPY
454    };
455 
456    if (driQueryOptionb(&screen->dev->option_cache, "always_have_depth_buffer")) {
457       /* all visuals will have a depth buffer */
458       depth_buffer_factor = 0;
459    }
460    else {
461       depth_bits_array[0] = 0;
462       stencil_bits_array[0] = 0;
463       depth_buffer_factor = 1;
464    }
465 
466    allow_rgba_ordering = dri_loader_get_cap(screen, DRI_LOADER_CAP_RGBA_ORDERING);
467    allow_rgb10 = driQueryOptionb(&screen->dev->option_cache, "allow_rgb10_configs");
468    allow_fp16 = dri_loader_get_cap(screen, DRI_LOADER_CAP_FP16);
469 
470    msaa_samples_max = (screen->st_api->feature_mask & ST_API_FEATURE_MS_VISUALS_MASK)
471       ? MSAA_VISUAL_MAX_SAMPLES : 1;
472 
473    pf_x8z24 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24X8_UNORM,
474 					    PIPE_TEXTURE_2D, 0, 0,
475                                             PIPE_BIND_DEPTH_STENCIL);
476    pf_z24x8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_X8Z24_UNORM,
477 					    PIPE_TEXTURE_2D, 0, 0,
478                                             PIPE_BIND_DEPTH_STENCIL);
479    pf_s8z24 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24_UNORM_S8_UINT,
480 					    PIPE_TEXTURE_2D, 0, 0,
481                                             PIPE_BIND_DEPTH_STENCIL);
482    pf_z24s8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_S8_UINT_Z24_UNORM,
483 					    PIPE_TEXTURE_2D, 0, 0,
484                                             PIPE_BIND_DEPTH_STENCIL);
485    pf_z16 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z16_UNORM,
486                                           PIPE_TEXTURE_2D, 0, 0,
487                                           PIPE_BIND_DEPTH_STENCIL);
488    pf_z32 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z32_UNORM,
489                                           PIPE_TEXTURE_2D, 0, 0,
490                                           PIPE_BIND_DEPTH_STENCIL);
491 
492    if (pf_z16) {
493       depth_bits_array[depth_buffer_factor] = 16;
494       stencil_bits_array[depth_buffer_factor++] = 0;
495    }
496    if (pf_x8z24 || pf_z24x8) {
497       depth_bits_array[depth_buffer_factor] = 24;
498       stencil_bits_array[depth_buffer_factor++] = 0;
499       screen->d_depth_bits_last = pf_x8z24;
500    }
501    if (pf_s8z24 || pf_z24s8) {
502       depth_bits_array[depth_buffer_factor] = 24;
503       stencil_bits_array[depth_buffer_factor++] = 8;
504       screen->sd_depth_bits_last = pf_s8z24;
505    }
506    if (pf_z32) {
507       depth_bits_array[depth_buffer_factor] = 32;
508       stencil_bits_array[depth_buffer_factor++] = 0;
509    }
510 
511    mixed_color_depth =
512       p_screen->get_param(p_screen, PIPE_CAP_MIXED_COLOR_DEPTH_BITS);
513 
514    assert(ARRAY_SIZE(mesa_formats) == ARRAY_SIZE(pipe_formats));
515 
516    /* Add configs. */
517    for (format = 0; format < ARRAY_SIZE(mesa_formats); format++) {
518       __DRIconfig **new_configs = NULL;
519       unsigned num_msaa_modes = 0; /* includes a single-sample mode */
520       uint8_t msaa_modes[MSAA_VISUAL_MAX_SAMPLES];
521 
522       /* Expose only BGRA ordering if the loader doesn't support RGBA ordering. */
523       if (!allow_rgba_ordering &&
524           (mesa_formats[format] == MESA_FORMAT_R8G8B8A8_UNORM ||
525            mesa_formats[format] == MESA_FORMAT_R8G8B8X8_UNORM ||
526            mesa_formats[format] == MESA_FORMAT_R8G8B8A8_SRGB  ||
527            mesa_formats[format] == MESA_FORMAT_R8G8B8X8_SRGB))
528          continue;
529 
530       if (!allow_rgb10 &&
531           (mesa_formats[format] == MESA_FORMAT_B10G10R10A2_UNORM ||
532            mesa_formats[format] == MESA_FORMAT_B10G10R10X2_UNORM ||
533            mesa_formats[format] == MESA_FORMAT_R10G10B10A2_UNORM ||
534            mesa_formats[format] == MESA_FORMAT_R10G10B10X2_UNORM))
535          continue;
536 
537       if (!allow_fp16 &&
538           (mesa_formats[format] == MESA_FORMAT_RGBA_FLOAT16 ||
539            mesa_formats[format] == MESA_FORMAT_RGBX_FLOAT16))
540          continue;
541 
542       if (!p_screen->is_format_supported(p_screen, pipe_formats[format],
543                                          PIPE_TEXTURE_2D, 0, 0,
544                                          PIPE_BIND_RENDER_TARGET |
545                                          PIPE_BIND_DISPLAY_TARGET))
546          continue;
547 
548       for (i = 1; i <= msaa_samples_max; i++) {
549          int samples = i > 1 ? i : 0;
550 
551          if (p_screen->is_format_supported(p_screen, pipe_formats[format],
552                                            PIPE_TEXTURE_2D, samples, samples,
553                                            PIPE_BIND_RENDER_TARGET)) {
554             msaa_modes[num_msaa_modes++] = samples;
555          }
556       }
557 
558       if (num_msaa_modes) {
559          /* Single-sample configs with an accumulation buffer. */
560          new_configs = driCreateConfigs(mesa_formats[format],
561                                         depth_bits_array, stencil_bits_array,
562                                         depth_buffer_factor, back_buffer_modes,
563                                         ARRAY_SIZE(back_buffer_modes),
564                                         msaa_modes, 1,
565                                         GL_TRUE, !mixed_color_depth);
566          configs = driConcatConfigs(configs, new_configs);
567 
568          /* Multi-sample configs without an accumulation buffer. */
569          if (num_msaa_modes > 1) {
570             new_configs = driCreateConfigs(mesa_formats[format],
571                                            depth_bits_array, stencil_bits_array,
572                                            depth_buffer_factor, back_buffer_modes,
573                                            ARRAY_SIZE(back_buffer_modes),
574                                            msaa_modes+1, num_msaa_modes-1,
575                                            GL_FALSE, !mixed_color_depth);
576             configs = driConcatConfigs(configs, new_configs);
577          }
578       }
579    }
580 
581    if (configs == NULL) {
582       debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__);
583       return NULL;
584    }
585 
586    return (const __DRIconfig **)configs;
587 }
588 
589 /**
590  * Roughly the converse of dri_fill_in_modes.
591  */
592 void
dri_fill_st_visual(struct st_visual * stvis,const struct dri_screen * screen,const struct gl_config * mode)593 dri_fill_st_visual(struct st_visual *stvis,
594                    const struct dri_screen *screen,
595                    const struct gl_config *mode)
596 {
597    memset(stvis, 0, sizeof(*stvis));
598 
599    if (!mode)
600       return;
601 
602    /* Deduce the color format. */
603    switch (mode->redMask) {
604    case 0:
605       /* Formats > 32 bpp */
606       assert(mode->floatMode);
607       if (mode->alphaShift > -1) {
608          assert(mode->alphaShift == 48);
609          stvis->color_format = PIPE_FORMAT_R16G16B16A16_FLOAT;
610       } else {
611          stvis->color_format = PIPE_FORMAT_R16G16B16X16_FLOAT;
612       }
613       break;
614 
615    case 0x3FF00000:
616       if (mode->alphaMask) {
617          assert(mode->alphaMask == 0xC0000000);
618          stvis->color_format = PIPE_FORMAT_B10G10R10A2_UNORM;
619       } else {
620          stvis->color_format = PIPE_FORMAT_B10G10R10X2_UNORM;
621       }
622       break;
623 
624    case 0x000003FF:
625       if (mode->alphaMask) {
626          assert(mode->alphaMask == 0xC0000000);
627          stvis->color_format = PIPE_FORMAT_R10G10B10A2_UNORM;
628       } else {
629          stvis->color_format = PIPE_FORMAT_R10G10B10X2_UNORM;
630       }
631       break;
632 
633    case 0x00FF0000:
634       if (mode->alphaMask) {
635          assert(mode->alphaMask == 0xFF000000);
636          stvis->color_format = mode->sRGBCapable ?
637                                   PIPE_FORMAT_BGRA8888_SRGB :
638                                   PIPE_FORMAT_BGRA8888_UNORM;
639       } else {
640          stvis->color_format = mode->sRGBCapable ?
641                                   PIPE_FORMAT_BGRX8888_SRGB :
642                                   PIPE_FORMAT_BGRX8888_UNORM;
643       }
644       break;
645 
646    case 0x000000FF:
647       if (mode->alphaMask) {
648          assert(mode->alphaMask == 0xFF000000);
649          stvis->color_format = mode->sRGBCapable ?
650                                   PIPE_FORMAT_RGBA8888_SRGB :
651                                   PIPE_FORMAT_RGBA8888_UNORM;
652       } else {
653          stvis->color_format = mode->sRGBCapable ?
654                                   PIPE_FORMAT_RGBX8888_SRGB :
655                                   PIPE_FORMAT_RGBX8888_UNORM;
656       }
657       break;
658 
659    case 0x0000F800:
660       stvis->color_format = PIPE_FORMAT_B5G6R5_UNORM;
661       break;
662 
663    default:
664       assert(!"unsupported visual: invalid red mask");
665       return;
666    }
667 
668    if (mode->samples > 0) {
669       if (debug_get_bool_option("DRI_NO_MSAA", false))
670          stvis->samples = 0;
671       else
672          stvis->samples = mode->samples;
673    }
674 
675    switch (mode->depthBits) {
676    default:
677    case 0:
678       stvis->depth_stencil_format = PIPE_FORMAT_NONE;
679       break;
680    case 16:
681       stvis->depth_stencil_format = PIPE_FORMAT_Z16_UNORM;
682       break;
683    case 24:
684       if (mode->stencilBits == 0) {
685 	 stvis->depth_stencil_format = (screen->d_depth_bits_last) ?
686                                           PIPE_FORMAT_Z24X8_UNORM:
687                                           PIPE_FORMAT_X8Z24_UNORM;
688       } else {
689 	 stvis->depth_stencil_format = (screen->sd_depth_bits_last) ?
690                                           PIPE_FORMAT_Z24_UNORM_S8_UINT:
691                                           PIPE_FORMAT_S8_UINT_Z24_UNORM;
692       }
693       break;
694    case 32:
695       stvis->depth_stencil_format = PIPE_FORMAT_Z32_UNORM;
696       break;
697    }
698 
699    stvis->accum_format = (mode->accumRedBits > 0) ?
700       PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
701 
702    stvis->buffer_mask |= ST_ATTACHMENT_FRONT_LEFT_MASK;
703    if (mode->doubleBufferMode) {
704       stvis->buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
705    }
706    if (mode->stereoMode) {
707       stvis->buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
708       if (mode->doubleBufferMode)
709          stvis->buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
710    }
711 
712    if (mode->depthBits > 0 || mode->stencilBits > 0)
713       stvis->buffer_mask |= ST_ATTACHMENT_DEPTH_STENCIL_MASK;
714    /* let the gallium frontend allocate the accum buffer */
715 }
716 
717 static bool
dri_get_egl_image(struct st_manager * smapi,void * egl_image,struct st_egl_image * stimg)718 dri_get_egl_image(struct st_manager *smapi,
719                   void *egl_image,
720                   struct st_egl_image *stimg)
721 {
722    struct dri_screen *screen = (struct dri_screen *)smapi;
723    __DRIimage *img = NULL;
724    const struct dri2_format_mapping *map;
725 
726    if (screen->lookup_egl_image_validated) {
727       img = screen->lookup_egl_image_validated(screen, egl_image);
728    } else if (screen->lookup_egl_image) {
729       img = screen->lookup_egl_image(screen, egl_image);
730    }
731 
732    if (!img)
733       return FALSE;
734 
735    stimg->texture = NULL;
736    pipe_resource_reference(&stimg->texture, img->texture);
737    map = dri2_get_mapping_by_fourcc(img->dri_fourcc);
738    stimg->format = map ? map->pipe_format : img->texture->format;
739    stimg->level = img->level;
740    stimg->layer = img->layer;
741 
742    if (img->imported_dmabuf && map) {
743       /* Guess sized internal format for dma-bufs. Could be used
744        * by EXT_EGL_image_storage.
745        */
746       mesa_format mesa_format = driImageFormatToGLFormat(map->dri_format);
747       stimg->internalformat = driGLFormatToSizedInternalGLFormat(mesa_format);
748    } else {
749       stimg->internalformat = img->internal_format;
750    }
751 
752    stimg->yuv_color_space = img->yuv_color_space;
753    stimg->yuv_range = img->sample_range;
754 
755    return TRUE;
756 }
757 
758 static bool
dri_validate_egl_image(struct st_manager * smapi,void * egl_image)759 dri_validate_egl_image(struct st_manager *smapi,
760                        void *egl_image)
761 {
762    struct dri_screen *screen = (struct dri_screen *)smapi;
763 
764    return screen->validate_egl_image(screen, egl_image);
765 }
766 
767 static int
dri_get_param(struct st_manager * smapi,enum st_manager_param param)768 dri_get_param(struct st_manager *smapi,
769               enum st_manager_param param)
770 {
771    return 0;
772 }
773 
774 void
dri_destroy_screen_helper(struct dri_screen * screen)775 dri_destroy_screen_helper(struct dri_screen * screen)
776 {
777    if (screen->base.destroy)
778       screen->base.destroy(&screen->base);
779 
780    if (screen->st_api && screen->st_api->destroy)
781       screen->st_api->destroy(screen->st_api);
782 
783    if (screen->base.screen)
784       screen->base.screen->destroy(screen->base.screen);
785 
786    mtx_destroy(&screen->opencl_func_mutex);
787 }
788 
789 void
dri_destroy_screen(__DRIscreen * sPriv)790 dri_destroy_screen(__DRIscreen * sPriv)
791 {
792    struct dri_screen *screen = dri_screen(sPriv);
793 
794    dri_destroy_screen_helper(screen);
795 
796    pipe_loader_release(&screen->dev, 1);
797 
798    free(screen->options.force_gl_vendor);
799    free(screen->options.force_gl_renderer);
800    free(screen->options.mesa_extension_override);
801 
802    /* The caller in dri_util preserves the fd ownership */
803    free(screen);
804    sPriv->driverPrivate = NULL;
805    sPriv->extensions = NULL;
806 }
807 
808 static void
dri_postprocessing_init(struct dri_screen * screen)809 dri_postprocessing_init(struct dri_screen *screen)
810 {
811    unsigned i;
812 
813    for (i = 0; i < PP_FILTERS; i++) {
814       screen->pp_enabled[i] = driQueryOptioni(&screen->dev->option_cache,
815                                               pp_filters[i].name);
816    }
817 }
818 
819 static void
dri_set_background_context(struct st_context_iface * st,struct util_queue_monitoring * queue_info)820 dri_set_background_context(struct st_context_iface *st,
821                            struct util_queue_monitoring *queue_info)
822 {
823    struct dri_context *ctx = (struct dri_context *)st->st_manager_private;
824    const __DRIbackgroundCallableExtension *backgroundCallable =
825       ctx->sPriv->dri2.backgroundCallable;
826 
827    /* Note: Mesa will only call this function if GL multithreading is enabled
828     * We only do that if the loader exposed the __DRI_BACKGROUND_CALLABLE
829     * extension. So we know that backgroundCallable is not NULL.
830     */
831    assert(backgroundCallable);
832    backgroundCallable->setBackgroundContext(ctx->cPriv->loaderPrivate);
833 
834    if (ctx->hud)
835       hud_add_queue_for_monitoring(ctx->hud, queue_info);
836 }
837 
838 const __DRIconfig **
dri_init_screen_helper(struct dri_screen * screen,struct pipe_screen * pscreen)839 dri_init_screen_helper(struct dri_screen *screen,
840                        struct pipe_screen *pscreen)
841 {
842    screen->base.screen = pscreen;
843    screen->base.get_egl_image = dri_get_egl_image;
844    screen->base.get_param = dri_get_param;
845    screen->base.set_background_context = dri_set_background_context;
846 
847    if (screen->validate_egl_image)
848       screen->base.validate_egl_image = dri_validate_egl_image;
849 
850    screen->st_api = st_gl_api_create();
851    if (!screen->st_api)
852       return NULL;
853 
854    if(pscreen->get_param(pscreen, PIPE_CAP_NPOT_TEXTURES))
855       screen->target = PIPE_TEXTURE_2D;
856    else
857       screen->target = PIPE_TEXTURE_RECT;
858 
859    dri_postprocessing_init(screen);
860 
861    screen->st_api->query_versions(screen->st_api, &screen->base,
862                                   &screen->options,
863                                   &screen->sPriv->max_gl_core_version,
864                                   &screen->sPriv->max_gl_compat_version,
865                                   &screen->sPriv->max_gl_es1_version,
866                                   &screen->sPriv->max_gl_es2_version);
867 
868    return dri_fill_in_modes(screen);
869 }
870 
871 /* vim: set sw=3 ts=8 sts=3 expandtab: */
872