• 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 "util/format/u_formats.h"
39 #include "pipe-loader/pipe_loader.h"
40 #include "frontend/drm_driver.h"
41 
42 #include "util/u_debug.h"
43 #include "util/u_driconf.h"
44 #include "util/format/u_format_s3tc.h"
45 
46 #include "state_tracker/st_context.h"
47 
48 #define MSAA_VISUAL_MAX_SAMPLES 32
49 
50 #undef false
51 
52 const __DRIconfigOptionsExtension gallium_config_options = {
53    .base = { __DRI_CONFIG_OPTIONS, 2 },
54    .getXml = pipe_loader_get_driinfo_xml
55 };
56 
57 #define false 0
58 
59 void
dri_init_options(struct dri_screen * screen)60 dri_init_options(struct dri_screen *screen)
61 {
62    pipe_loader_config_options(screen->dev);
63 
64    struct st_config_options *options = &screen->options;
65    const struct driOptionCache *optionCache = &screen->dev->option_cache;
66 
67    u_driconf_fill_st_options(options, optionCache);
68 }
69 
70 static unsigned
dri_loader_get_cap(struct dri_screen * screen,enum dri_loader_cap cap)71 dri_loader_get_cap(struct dri_screen *screen, enum dri_loader_cap cap)
72 {
73    const __DRIdri2LoaderExtension *dri2_loader = screen->dri2.loader;
74    const __DRIimageLoaderExtension *image_loader = screen->image.loader;
75 
76    if (dri2_loader && dri2_loader->base.version >= 4 &&
77        dri2_loader->getCapability)
78       return dri2_loader->getCapability(screen->loaderPrivate, cap);
79 
80    if (image_loader && image_loader->base.version >= 2 &&
81        image_loader->getCapability)
82       return image_loader->getCapability(screen->loaderPrivate, cap);
83 
84    return 0;
85 }
86 
87 /**
88  * Creates a set of \c struct gl_config that a driver will expose.
89  *
90  * A set of \c struct gl_config will be created based on the supplied
91  * parameters.  The number of modes processed will be 2 *
92  * \c num_depth_stencil_bits * \c num_db_modes.
93  *
94  * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
95  * \c db_modes, and \c visType into each \c struct gl_config element.
96  * However, the meanings of \c fb_format and \c fb_type require further
97  * explanation.  The \c fb_format specifies which color components are in
98  * each pixel and what the default order is.  For example, \c GL_RGB specifies
99  * that red, green, blue are available and red is in the "most significant"
100  * position and blue is in the "least significant".  The \c fb_type specifies
101  * the bit sizes of each component and the actual ordering.  For example, if
102  * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
103  * are the blue value, bits [10:5] are the green value, and bits [4:0] are
104  * the red value.
105  *
106  * One sublte issue is the combination of \c GL_RGB  or \c GL_BGR and either
107  * of the \c GL_UNSIGNED_INT_8_8_8_8 modes.  The resulting mask values in the
108  * \c struct gl_config structure is \b identical to the \c GL_RGBA or
109  * \c GL_BGRA case, except the \c alphaMask is zero.  This means that, as
110  * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
111  * still uses 32-bits.
112  *
113  * If in doubt, look at the tables used in the function.
114  *
115  * \param ptr_to_modes  Pointer to a pointer to a linked list of
116  *                      \c struct gl_config.  Upon completion, a pointer to
117  *                      the next element to be process will be stored here.
118  *                      If the function fails and returns \c GL_FALSE, this
119  *                      value will be unmodified, but some elements in the
120  *                      linked list may be modified.
121  * \param format        Mesa mesa_format enum describing the pixel format
122  * \param zs_formats    Array of depth/stencil formats to expose
123  * \param num_zs_formats Number of entries in \c depth_stencil_formats.
124  * \param db_modes      Array of buffer swap modes.
125  * \param num_db_modes  Number of entries in \c db_modes.
126  * \param msaa_samples  Array of msaa sample count. 0 represents a visual
127  *                      without a multisample buffer.
128  * \param num_msaa_modes Number of entries in \c msaa_samples.
129  * \param enable_accum  Add an accum buffer to the configs
130  * \param color_depth_match Whether the color depth must match the zs depth
131  *                          This forces 32-bit color to have 24-bit depth, and
132  *                          16-bit color to have 16-bit depth.
133  *
134  * \returns
135  * Pointer to any array of pointers to the \c __DRIconfig structures created
136  * for the specified formats.  If there is an error, \c NULL is returned.
137  * Currently the only cause of failure is a bad parameter (i.e., unsupported
138  * \c format).
139  */
140 static __DRIconfig **
driCreateConfigs(enum pipe_format format,enum pipe_format * zs_formats,unsigned num_zs_formats,const bool * db_modes,unsigned num_db_modes,const uint8_t * msaa_samples,unsigned num_msaa_modes,GLboolean enable_accum,GLboolean color_depth_match)141 driCreateConfigs(enum pipe_format format,
142                  enum pipe_format *zs_formats, unsigned num_zs_formats,
143                  const bool *db_modes, unsigned num_db_modes,
144                  const uint8_t * msaa_samples, unsigned num_msaa_modes,
145                  GLboolean enable_accum, GLboolean color_depth_match)
146 {
147    uint32_t masks[4];
148    int shifts[4];
149    int color_bits[4];
150    __DRIconfig **configs, **c;
151    struct gl_config *modes;
152    unsigned i, j, k, h;
153    unsigned num_modes;
154    unsigned num_accum_bits = (enable_accum) ? 2 : 1;
155    bool is_srgb;
156    bool is_float;
157 
158    is_srgb = util_format_is_srgb(format);
159    is_float = util_format_is_float(format);
160 
161    for (i = 0; i < 4; i++) {
162       color_bits[i] =
163          util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, i);
164 
165       if (color_bits[i] > 0) {
166          shifts[i] =
167             util_format_get_component_shift(format, UTIL_FORMAT_COLORSPACE_RGB, i);
168       } else {
169          shifts[i] = -1;
170       }
171 
172       if (is_float || color_bits[i] == 0)
173          masks[i] = 0;
174       else
175          masks[i] = ((1 << color_bits[i]) - 1) << shifts[i];
176    }
177 
178    num_modes = num_zs_formats * num_db_modes * num_accum_bits * num_msaa_modes;
179    configs = calloc(num_modes + 1, sizeof *configs);
180    if (configs == NULL)
181        return NULL;
182 
183     c = configs;
184     for ( k = 0 ; k < num_zs_formats ; k++ ) {
185         unsigned depth_bits, stencil_bits;
186 
187         if (zs_formats[k] != PIPE_FORMAT_NONE) {
188            depth_bits =
189               util_format_get_component_bits(zs_formats[k],
190                                           UTIL_FORMAT_COLORSPACE_ZS, 0);
191            stencil_bits =
192               util_format_get_component_bits(zs_formats[k],
193                                           UTIL_FORMAT_COLORSPACE_ZS, 1);
194         } else {
195            depth_bits = 0;
196            stencil_bits = 0;
197         }
198 
199         for ( i = 0 ; i < num_db_modes ; i++ ) {
200             for ( h = 0 ; h < num_msaa_modes; h++ ) {
201                 for ( j = 0 ; j < num_accum_bits ; j++ ) {
202                     if (color_depth_match &&
203                         (depth_bits || stencil_bits)) {
204                         /* Depth can really only be 0, 16, 24, or 32. A 32-bit
205                          * color format still matches 24-bit depth, as there
206                          * is an implicit 8-bit stencil. So really we just
207                          * need to make sure that color/depth are both 16 or
208                          * both non-16.
209                          */
210                         if ((depth_bits + stencil_bits == 16) !=
211                             (color_bits[0] + color_bits[1] +
212                              color_bits[2] + color_bits[3] == 16))
213                             continue;
214                     }
215 
216                     *c = malloc (sizeof **c);
217                     modes = &(*c)->modes;
218                     c++;
219 
220                     memset(modes, 0, sizeof *modes);
221                     modes->color_format = format;
222                     modes->zs_format = zs_formats[k];
223                     if (j > 0)
224                        modes->accum_format = PIPE_FORMAT_R16G16B16A16_SNORM;
225                     else
226                        modes->accum_format = PIPE_FORMAT_NONE;
227 
228                     modes->floatMode  = is_float;
229                     modes->redBits    = color_bits[0];
230                     modes->redShift   = shifts[0];
231                     modes->redMask    = masks[0];
232                     modes->greenBits  = color_bits[1];
233                     modes->greenShift = shifts[1];
234                     modes->greenMask  = masks[1];
235                     modes->blueBits   = color_bits[2];
236                     modes->blueShift  = shifts[2];
237                     modes->blueMask   = masks[2];
238                     modes->alphaBits  = color_bits[3];
239                     modes->alphaMask  = masks[3];
240                     modes->alphaShift = shifts[3];
241                     modes->rgbBits   = modes->redBits + modes->greenBits
242                             + modes->blueBits + modes->alphaBits;
243 
244                     modes->accumRedBits   = 16 * j;
245                     modes->accumGreenBits = 16 * j;
246                     modes->accumBlueBits  = 16 * j;
247                     modes->accumAlphaBits = 16 * j;
248 
249                     modes->stencilBits = stencil_bits;
250                     modes->depthBits = depth_bits;
251 
252                     modes->doubleBufferMode = db_modes[i];
253 
254                     modes->samples = msaa_samples[h];
255 
256                     modes->sRGBCapable = is_srgb;
257                 }
258             }
259         }
260     }
261     *c = NULL;
262 
263     return configs;
264 }
265 
266 static __DRIconfig **
driConcatConfigs(__DRIconfig ** a,__DRIconfig ** b)267 driConcatConfigs(__DRIconfig **a, __DRIconfig **b)
268 {
269     __DRIconfig **all;
270     int i, j, index;
271 
272     if (a == NULL || a[0] == NULL)
273        return b;
274     else if (b == NULL || b[0] == NULL)
275        return a;
276 
277     i = 0;
278     while (a[i] != NULL)
279         i++;
280     j = 0;
281     while (b[j] != NULL)
282         j++;
283 
284     all = malloc((i + j + 1) * sizeof *all);
285     index = 0;
286     for (i = 0; a[i] != NULL; i++)
287         all[index++] = a[i];
288     for (j = 0; b[j] != NULL; j++)
289         all[index++] = b[j];
290     all[index++] = NULL;
291 
292     free(a);
293     free(b);
294 
295     return all;
296 }
297 
298 
299 static const __DRIconfig **
dri_fill_in_modes(struct dri_screen * screen)300 dri_fill_in_modes(struct dri_screen *screen)
301 {
302    /* The 32-bit RGBA format must not precede the 32-bit BGRA format.
303     * Likewise for RGBX and BGRX.  Otherwise, the GLX client and the GLX
304     * server may disagree on which format the GLXFBConfig represents,
305     * resulting in swapped color channels.
306     *
307     * The problem, as of 2017-05-30:
308     * When matching a GLXFBConfig to a __DRIconfig, GLX ignores the channel
309     * order and chooses the first __DRIconfig with the expected channel
310     * sizes. Specifically, GLX compares the GLXFBConfig's and __DRIconfig's
311     * __DRI_ATTRIB_{CHANNEL}_SIZE but ignores __DRI_ATTRIB_{CHANNEL}_MASK.
312     *
313     * EGL does not suffer from this problem. It correctly compares the
314     * channel masks when matching EGLConfig to __DRIconfig.
315     */
316    static const enum pipe_format pipe_formats[] = {
317       PIPE_FORMAT_B10G10R10A2_UNORM,
318       PIPE_FORMAT_B10G10R10X2_UNORM,
319       PIPE_FORMAT_R10G10B10A2_UNORM,
320       PIPE_FORMAT_R10G10B10X2_UNORM,
321       PIPE_FORMAT_BGRA8888_UNORM,
322       PIPE_FORMAT_BGRX8888_UNORM,
323       PIPE_FORMAT_BGRA8888_SRGB,
324       PIPE_FORMAT_BGRX8888_SRGB,
325       PIPE_FORMAT_B5G6R5_UNORM,
326       PIPE_FORMAT_R16G16B16A16_FLOAT,
327       PIPE_FORMAT_R16G16B16X16_FLOAT,
328       PIPE_FORMAT_RGBA8888_UNORM,
329       PIPE_FORMAT_RGBX8888_UNORM,
330       PIPE_FORMAT_RGBA8888_SRGB,
331       PIPE_FORMAT_RGBX8888_SRGB,
332       PIPE_FORMAT_B5G5R5A1_UNORM,
333       PIPE_FORMAT_R5G5B5A1_UNORM,
334       PIPE_FORMAT_B4G4R4A4_UNORM,
335       PIPE_FORMAT_R4G4B4A4_UNORM,
336    };
337    __DRIconfig **configs = NULL;
338    enum pipe_format zs_formats[5];
339    unsigned num_zs_formats = 0;
340    unsigned i;
341    struct pipe_screen *p_screen = screen->base.screen;
342    bool mixed_color_depth;
343    bool allow_rgba_ordering;
344    bool allow_rgb10;
345    bool allow_fp16;
346 
347    static const bool db_modes[] = { false, true };
348 
349    if (!driQueryOptionb(&screen->dev->option_cache, "always_have_depth_buffer"))
350       zs_formats[num_zs_formats++] = PIPE_FORMAT_NONE;
351 
352    allow_rgba_ordering = dri_loader_get_cap(screen, DRI_LOADER_CAP_RGBA_ORDERING);
353    allow_rgb10 = driQueryOptionb(&screen->dev->option_cache, "allow_rgb10_configs");
354    allow_fp16 = dri_loader_get_cap(screen, DRI_LOADER_CAP_FP16);
355 
356 #define HAS_ZS(fmt) \
357    p_screen->is_format_supported(p_screen, PIPE_FORMAT_##fmt, \
358                                  PIPE_TEXTURE_2D, 0, 0, \
359                                  PIPE_BIND_DEPTH_STENCIL)
360 
361    if (HAS_ZS(Z16_UNORM))
362       zs_formats[num_zs_formats++] = PIPE_FORMAT_Z16_UNORM;
363 
364    if (HAS_ZS(Z24X8_UNORM))
365       zs_formats[num_zs_formats++] = PIPE_FORMAT_Z24X8_UNORM;
366    else if (HAS_ZS(X8Z24_UNORM))
367       zs_formats[num_zs_formats++] = PIPE_FORMAT_X8Z24_UNORM;
368 
369    if (HAS_ZS(Z24_UNORM_S8_UINT))
370       zs_formats[num_zs_formats++] = PIPE_FORMAT_Z24_UNORM_S8_UINT;
371    else if (HAS_ZS(S8_UINT_Z24_UNORM))
372       zs_formats[num_zs_formats++] = PIPE_FORMAT_S8_UINT_Z24_UNORM;
373 
374    if (HAS_ZS(Z32_UNORM))
375       zs_formats[num_zs_formats++] = PIPE_FORMAT_Z32_UNORM;
376 
377 #undef HAS_ZS
378 
379    mixed_color_depth =
380       p_screen->get_param(p_screen, PIPE_CAP_MIXED_COLOR_DEPTH_BITS);
381 
382    /* Add configs. */
383    for (unsigned f = 0; f < ARRAY_SIZE(pipe_formats); f++) {
384       __DRIconfig **new_configs = NULL;
385       unsigned num_msaa_modes = 0; /* includes a single-sample mode */
386       uint8_t msaa_modes[MSAA_VISUAL_MAX_SAMPLES];
387 
388       /* Expose only BGRA ordering if the loader doesn't support RGBA ordering. */
389       if (!allow_rgba_ordering &&
390           util_format_get_component_shift(pipe_formats[f],
391                                           UTIL_FORMAT_COLORSPACE_RGB, 0)
392 #if UTIL_ARCH_BIG_ENDIAN
393          >
394 #else
395          <
396 #endif
397           util_format_get_component_shift(pipe_formats[f],
398                                           UTIL_FORMAT_COLORSPACE_RGB, 2))
399          continue;
400 
401       if (!allow_rgb10 &&
402           util_format_get_component_bits(pipe_formats[f],
403                                          UTIL_FORMAT_COLORSPACE_RGB, 0) == 10 &&
404           util_format_get_component_bits(pipe_formats[f],
405                                          UTIL_FORMAT_COLORSPACE_RGB, 1) == 10 &&
406           util_format_get_component_bits(pipe_formats[f],
407                                          UTIL_FORMAT_COLORSPACE_RGB, 2) == 10)
408          continue;
409 
410       if (!allow_fp16 && util_format_is_float(pipe_formats[f]))
411          continue;
412 
413       if (!p_screen->is_format_supported(p_screen, pipe_formats[f],
414                                          PIPE_TEXTURE_2D, 0, 0,
415                                          PIPE_BIND_RENDER_TARGET |
416                                          PIPE_BIND_DISPLAY_TARGET))
417          continue;
418 
419       for (i = 1; i <= MSAA_VISUAL_MAX_SAMPLES; i++) {
420          int samples = i > 1 ? i : 0;
421 
422          if (p_screen->is_format_supported(p_screen, pipe_formats[f],
423                                            PIPE_TEXTURE_2D, samples, samples,
424                                            PIPE_BIND_RENDER_TARGET)) {
425             msaa_modes[num_msaa_modes++] = samples;
426          }
427       }
428 
429       if (num_msaa_modes) {
430          /* Single-sample configs with an accumulation buffer. */
431          new_configs = driCreateConfigs(pipe_formats[f],
432                                         zs_formats, num_zs_formats,
433                                         db_modes, ARRAY_SIZE(db_modes),
434                                         msaa_modes, 1,
435                                         GL_TRUE, !mixed_color_depth);
436          configs = driConcatConfigs(configs, new_configs);
437 
438          /* Multi-sample configs without an accumulation buffer. */
439          if (num_msaa_modes > 1) {
440             new_configs = driCreateConfigs(pipe_formats[f],
441                                            zs_formats, num_zs_formats,
442                                            db_modes, ARRAY_SIZE(db_modes),
443                                            msaa_modes+1, num_msaa_modes-1,
444                                            GL_FALSE, !mixed_color_depth);
445             configs = driConcatConfigs(configs, new_configs);
446          }
447       }
448    }
449 
450    if (configs == NULL) {
451       debug_printf("%s: driCreateConfigs failed\n", __func__);
452       return NULL;
453    }
454 
455    return (const __DRIconfig **)configs;
456 }
457 
458 /**
459  * Roughly the converse of dri_fill_in_modes.
460  */
461 void
dri_fill_st_visual(struct st_visual * stvis,const struct dri_screen * screen,const struct gl_config * mode)462 dri_fill_st_visual(struct st_visual *stvis,
463                    const struct dri_screen *screen,
464                    const struct gl_config *mode)
465 {
466    memset(stvis, 0, sizeof(*stvis));
467 
468    if (!mode)
469       return;
470 
471    assert(mode->color_format != PIPE_FORMAT_NONE);
472    stvis->color_format = mode->color_format;
473    stvis->accum_format = mode->accum_format;
474    stvis->depth_stencil_format = mode->zs_format;
475 
476    if (mode->samples > 0) {
477       if (debug_get_bool_option("DRI_NO_MSAA", false))
478          stvis->samples = 0;
479       else
480          stvis->samples = mode->samples;
481    }
482 
483    stvis->buffer_mask |= ST_ATTACHMENT_FRONT_LEFT_MASK;
484    if (mode->doubleBufferMode) {
485       stvis->buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
486    }
487    if (mode->stereoMode) {
488       stvis->buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
489       if (mode->doubleBufferMode)
490          stvis->buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
491    }
492 
493    if (mode->depthBits > 0 || mode->stencilBits > 0)
494       stvis->buffer_mask |= ST_ATTACHMENT_DEPTH_STENCIL_MASK;
495    /* let the gallium frontend allocate the accum buffer */
496 }
497 
498 static bool
dri_get_egl_image(struct pipe_frontend_screen * fscreen,void * egl_image,struct st_egl_image * stimg)499 dri_get_egl_image(struct pipe_frontend_screen *fscreen,
500                   void *egl_image,
501                   struct st_egl_image *stimg)
502 {
503    struct dri_screen *screen = (struct dri_screen *)fscreen;
504    __DRIimage *img = NULL;
505    const struct dri2_format_mapping *map;
506 
507    if (screen->lookup_egl_image_validated) {
508       img = screen->lookup_egl_image_validated(screen, egl_image);
509    } else if (screen->lookup_egl_image) {
510       img = screen->lookup_egl_image(screen, egl_image);
511    }
512 
513    if (!img)
514       return false;
515 
516    stimg->texture = NULL;
517    pipe_resource_reference(&stimg->texture, img->texture);
518    map = dri2_get_mapping_by_fourcc(img->dri_fourcc);
519    stimg->format = map ? map->pipe_format : img->texture->format;
520    stimg->level = img->level;
521    stimg->layer = img->layer;
522    stimg->imported_dmabuf = img->imported_dmabuf;
523 
524    if (img->imported_dmabuf && map) {
525       /* Guess sized internal format for dma-bufs. Could be used
526        * by EXT_EGL_image_storage.
527        */
528       mesa_format mesa_format = driImageFormatToGLFormat(map->dri_format);
529       stimg->internalformat = driGLFormatToSizedInternalGLFormat(mesa_format);
530    } else {
531       stimg->internalformat = img->internal_format;
532    }
533 
534    stimg->yuv_color_space = img->yuv_color_space;
535    stimg->yuv_range = img->sample_range;
536 
537    return true;
538 }
539 
540 static bool
dri_validate_egl_image(struct pipe_frontend_screen * fscreen,void * egl_image)541 dri_validate_egl_image(struct pipe_frontend_screen *fscreen,
542                        void *egl_image)
543 {
544    struct dri_screen *screen = (struct dri_screen *)fscreen;
545 
546    return screen->validate_egl_image(screen, egl_image);
547 }
548 
549 static int
dri_get_param(struct pipe_frontend_screen * fscreen,enum st_manager_param param)550 dri_get_param(struct pipe_frontend_screen *fscreen,
551               enum st_manager_param param)
552 {
553    return 0;
554 }
555 
556 void
dri_release_screen(struct dri_screen * screen)557 dri_release_screen(struct dri_screen * screen)
558 {
559    st_screen_destroy(&screen->base);
560 
561    if (screen->base.screen) {
562       screen->base.screen->destroy(screen->base.screen);
563       screen->base.screen = NULL;
564    }
565 
566    if (screen->dev) {
567       pipe_loader_release(&screen->dev, 1);
568       screen->dev = NULL;
569    }
570 
571    mtx_destroy(&screen->opencl_func_mutex);
572 }
573 
574 void
dri_destroy_screen(struct dri_screen * screen)575 dri_destroy_screen(struct dri_screen *screen)
576 {
577    dri_release_screen(screen);
578 
579    free(screen->options.force_gl_vendor);
580    free(screen->options.force_gl_renderer);
581    free(screen->options.mesa_extension_override);
582 
583    driDestroyOptionCache(&screen->optionCache);
584    driDestroyOptionInfo(&screen->optionInfo);
585 
586    /* The caller in dri_util preserves the fd ownership */
587    free(screen);
588 }
589 
590 static void
dri_postprocessing_init(struct dri_screen * screen)591 dri_postprocessing_init(struct dri_screen *screen)
592 {
593    unsigned i;
594 
595    for (i = 0; i < PP_FILTERS; i++) {
596       screen->pp_enabled[i] = driQueryOptioni(&screen->dev->option_cache,
597                                               pp_filters[i].name);
598    }
599 }
600 
601 static void
dri_set_background_context(struct st_context * st,struct util_queue_monitoring * queue_info)602 dri_set_background_context(struct st_context *st,
603                            struct util_queue_monitoring *queue_info)
604 {
605    struct dri_context *ctx = (struct dri_context *)st->frontend_context;
606    const __DRIbackgroundCallableExtension *backgroundCallable =
607       ctx->screen->dri2.backgroundCallable;
608 
609    if (backgroundCallable)
610       backgroundCallable->setBackgroundContext(ctx->loaderPrivate);
611 
612    if (ctx->hud)
613       hud_add_queue_for_monitoring(ctx->hud, queue_info);
614 }
615 
616 const __DRIconfig **
dri_init_screen(struct dri_screen * screen,struct pipe_screen * pscreen)617 dri_init_screen(struct dri_screen *screen,
618                 struct pipe_screen *pscreen)
619 {
620    screen->base.screen = pscreen;
621    screen->base.get_egl_image = dri_get_egl_image;
622    screen->base.get_param = dri_get_param;
623    screen->base.set_background_context = dri_set_background_context;
624 
625    if (screen->validate_egl_image)
626       screen->base.validate_egl_image = dri_validate_egl_image;
627 
628    if (pscreen->get_param(pscreen, PIPE_CAP_NPOT_TEXTURES))
629       screen->target = PIPE_TEXTURE_2D;
630    else
631       screen->target = PIPE_TEXTURE_RECT;
632 
633    dri_postprocessing_init(screen);
634 
635    st_api_query_versions(&screen->base,
636                          &screen->options,
637                          &screen->max_gl_core_version,
638                          &screen->max_gl_compat_version,
639                          &screen->max_gl_es1_version,
640                          &screen->max_gl_es2_version);
641 
642    return dri_fill_in_modes(screen);
643 }
644 
645 /* vim: set sw=3 ts=8 sts=3 expandtab: */
646