st/dri: replace hard-coded array size with ARRAY_SIZE()
[mesa.git] / src / gallium / state_trackers / dri / dri_screen.c
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 "utils.h"
33
34 #include "dri_screen.h"
35 #include "dri_context.h"
36
37 #include "util/u_inlines.h"
38 #include "pipe/p_screen.h"
39 #include "pipe/p_format.h"
40 #include "pipe-loader/pipe_loader.h"
41 #include "state_tracker/st_gl_api.h" /* for st_gl_api_create */
42 #include "state_tracker/drm_driver.h"
43
44 #include "util/u_debug.h"
45 #include "util/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 .xml = gallium_driinfo_xml,
54 .getXml = pipe_loader_get_driinfo_xml
55 };
56
57 #define false 0
58
59 static void
60 dri_fill_st_options(struct dri_screen *screen)
61 {
62 struct st_config_options *options = &screen->options;
63 const struct driOptionCache *optionCache = &screen->dev->option_cache;
64
65 options->disable_blend_func_extended =
66 driQueryOptionb(optionCache, "disable_blend_func_extended");
67 options->disable_glsl_line_continuations =
68 driQueryOptionb(optionCache, "disable_glsl_line_continuations");
69 options->disable_shader_bit_encoding =
70 driQueryOptionb(optionCache, "disable_shader_bit_encoding");
71 options->force_glsl_extensions_warn =
72 driQueryOptionb(optionCache, "force_glsl_extensions_warn");
73 options->force_glsl_version =
74 driQueryOptioni(optionCache, "force_glsl_version");
75 options->allow_glsl_extension_directive_midshader =
76 driQueryOptionb(optionCache, "allow_glsl_extension_directive_midshader");
77 options->allow_glsl_builtin_variable_redeclaration =
78 driQueryOptionb(optionCache, "allow_glsl_builtin_variable_redeclaration");
79 options->allow_higher_compat_version =
80 driQueryOptionb(optionCache, "allow_higher_compat_version");
81 options->glsl_zero_init = driQueryOptionb(optionCache, "glsl_zero_init");
82 options->force_glsl_abs_sqrt =
83 driQueryOptionb(optionCache, "force_glsl_abs_sqrt");
84
85 driComputeOptionsSha1(optionCache, options->config_options_sha1);
86 }
87
88 static unsigned
89 dri_loader_get_cap(struct dri_screen *screen, enum dri_loader_cap cap)
90 {
91 const __DRIdri2LoaderExtension *dri2_loader = screen->sPriv->dri2.loader;
92 const __DRIimageLoaderExtension *image_loader = screen->sPriv->image.loader;
93
94 if (dri2_loader && dri2_loader->base.version >= 4 &&
95 dri2_loader->getCapability)
96 return dri2_loader->getCapability(screen->sPriv->loaderPrivate, cap);
97
98 if (image_loader && image_loader->base.version >= 2 &&
99 image_loader->getCapability)
100 return image_loader->getCapability(screen->sPriv->loaderPrivate, cap);
101
102 return 0;
103 }
104
105 static const __DRIconfig **
106 dri_fill_in_modes(struct dri_screen *screen)
107 {
108 static const mesa_format mesa_formats[] = {
109 MESA_FORMAT_B8G8R8A8_UNORM,
110 MESA_FORMAT_B8G8R8X8_UNORM,
111 MESA_FORMAT_B8G8R8A8_SRGB,
112 MESA_FORMAT_B8G8R8X8_SRGB,
113 MESA_FORMAT_B5G6R5_UNORM,
114
115 /* The 32-bit RGBA format must not precede the 32-bit BGRA format.
116 * Likewise for RGBX and BGRX. Otherwise, the GLX client and the GLX
117 * server may disagree on which format the GLXFBConfig represents,
118 * resulting in swapped color channels.
119 *
120 * The problem, as of 2017-05-30:
121 * When matching a GLXFBConfig to a __DRIconfig, GLX ignores the channel
122 * order and chooses the first __DRIconfig with the expected channel
123 * sizes. Specifically, GLX compares the GLXFBConfig's and __DRIconfig's
124 * __DRI_ATTRIB_{CHANNEL}_SIZE but ignores __DRI_ATTRIB_{CHANNEL}_MASK.
125 *
126 * EGL does not suffer from this problem. It correctly compares the
127 * channel masks when matching EGLConfig to __DRIconfig.
128 */
129
130 /* Required by Android, for HAL_PIXEL_FORMAT_RGBA_8888. */
131 MESA_FORMAT_R8G8B8A8_UNORM,
132
133 /* Required by Android, for HAL_PIXEL_FORMAT_RGBX_8888. */
134 MESA_FORMAT_R8G8B8X8_UNORM,
135 };
136 static const enum pipe_format pipe_formats[] = {
137 PIPE_FORMAT_BGRA8888_UNORM,
138 PIPE_FORMAT_BGRX8888_UNORM,
139 PIPE_FORMAT_BGRA8888_SRGB,
140 PIPE_FORMAT_BGRX8888_SRGB,
141 PIPE_FORMAT_B5G6R5_UNORM,
142 PIPE_FORMAT_RGBA8888_UNORM,
143 PIPE_FORMAT_RGBX8888_UNORM,
144 };
145 mesa_format format;
146 __DRIconfig **configs = NULL;
147 uint8_t depth_bits_array[5];
148 uint8_t stencil_bits_array[5];
149 unsigned depth_buffer_factor;
150 unsigned msaa_samples_max;
151 unsigned i;
152 struct pipe_screen *p_screen = screen->base.screen;
153 boolean pf_z16, pf_x8z24, pf_z24x8, pf_s8z24, pf_z24s8, pf_z32;
154 boolean mixed_color_depth;
155
156 static const GLenum back_buffer_modes[] = {
157 __DRI_ATTRIB_SWAP_NONE, __DRI_ATTRIB_SWAP_UNDEFINED,
158 __DRI_ATTRIB_SWAP_COPY
159 };
160
161 if (driQueryOptionb(&screen->dev->option_cache, "always_have_depth_buffer")) {
162 /* all visuals will have a depth buffer */
163 depth_buffer_factor = 0;
164 }
165 else {
166 depth_bits_array[0] = 0;
167 stencil_bits_array[0] = 0;
168 depth_buffer_factor = 1;
169 }
170
171 msaa_samples_max = (screen->st_api->feature_mask & ST_API_FEATURE_MS_VISUALS_MASK)
172 ? MSAA_VISUAL_MAX_SAMPLES : 1;
173
174 pf_x8z24 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24X8_UNORM,
175 PIPE_TEXTURE_2D, 0,
176 PIPE_BIND_DEPTH_STENCIL);
177 pf_z24x8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_X8Z24_UNORM,
178 PIPE_TEXTURE_2D, 0,
179 PIPE_BIND_DEPTH_STENCIL);
180 pf_s8z24 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z24_UNORM_S8_UINT,
181 PIPE_TEXTURE_2D, 0,
182 PIPE_BIND_DEPTH_STENCIL);
183 pf_z24s8 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_S8_UINT_Z24_UNORM,
184 PIPE_TEXTURE_2D, 0,
185 PIPE_BIND_DEPTH_STENCIL);
186 pf_z16 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z16_UNORM,
187 PIPE_TEXTURE_2D, 0,
188 PIPE_BIND_DEPTH_STENCIL);
189 pf_z32 = p_screen->is_format_supported(p_screen, PIPE_FORMAT_Z32_UNORM,
190 PIPE_TEXTURE_2D, 0,
191 PIPE_BIND_DEPTH_STENCIL);
192
193 if (pf_z16) {
194 depth_bits_array[depth_buffer_factor] = 16;
195 stencil_bits_array[depth_buffer_factor++] = 0;
196 }
197 if (pf_x8z24 || pf_z24x8) {
198 depth_bits_array[depth_buffer_factor] = 24;
199 stencil_bits_array[depth_buffer_factor++] = 0;
200 screen->d_depth_bits_last = pf_x8z24;
201 }
202 if (pf_s8z24 || pf_z24s8) {
203 depth_bits_array[depth_buffer_factor] = 24;
204 stencil_bits_array[depth_buffer_factor++] = 8;
205 screen->sd_depth_bits_last = pf_s8z24;
206 }
207 if (pf_z32) {
208 depth_bits_array[depth_buffer_factor] = 32;
209 stencil_bits_array[depth_buffer_factor++] = 0;
210 }
211
212 mixed_color_depth =
213 p_screen->get_param(p_screen, PIPE_CAP_MIXED_COLOR_DEPTH_BITS);
214
215 assert(ARRAY_SIZE(mesa_formats) == ARRAY_SIZE(pipe_formats));
216
217 /* Expose only BGRA ordering if the loader doesn't support RGBA ordering. */
218 unsigned num_formats;
219 if (dri_loader_get_cap(screen, DRI_LOADER_CAP_RGBA_ORDERING))
220 num_formats = ARRAY_SIZE(mesa_formats);
221 else
222 num_formats = ARRAY_SIZE(mesa_formats) - 2;
223
224 /* Add configs. */
225 for (format = 0; format < num_formats; format++) {
226 __DRIconfig **new_configs = NULL;
227 unsigned num_msaa_modes = 0; /* includes a single-sample mode */
228 uint8_t msaa_modes[MSAA_VISUAL_MAX_SAMPLES];
229
230 if (!p_screen->is_format_supported(p_screen, pipe_formats[format],
231 PIPE_TEXTURE_2D, 0,
232 PIPE_BIND_RENDER_TARGET))
233 continue;
234
235 for (i = 1; i <= msaa_samples_max; i++) {
236 int samples = i > 1 ? i : 0;
237
238 if (p_screen->is_format_supported(p_screen, pipe_formats[format],
239 PIPE_TEXTURE_2D, samples,
240 PIPE_BIND_RENDER_TARGET)) {
241 msaa_modes[num_msaa_modes++] = samples;
242 }
243 }
244
245 if (num_msaa_modes) {
246 /* Single-sample configs with an accumulation buffer. */
247 new_configs = driCreateConfigs(mesa_formats[format],
248 depth_bits_array, stencil_bits_array,
249 depth_buffer_factor, back_buffer_modes,
250 ARRAY_SIZE(back_buffer_modes),
251 msaa_modes, 1,
252 GL_TRUE, !mixed_color_depth);
253 configs = driConcatConfigs(configs, new_configs);
254
255 /* Multi-sample configs without an accumulation buffer. */
256 if (num_msaa_modes > 1) {
257 new_configs = driCreateConfigs(mesa_formats[format],
258 depth_bits_array, stencil_bits_array,
259 depth_buffer_factor, back_buffer_modes,
260 ARRAY_SIZE(back_buffer_modes),
261 msaa_modes+1, num_msaa_modes-1,
262 GL_FALSE, !mixed_color_depth);
263 configs = driConcatConfigs(configs, new_configs);
264 }
265 }
266 }
267
268 if (configs == NULL) {
269 debug_printf("%s: driCreateConfigs failed\n", __FUNCTION__);
270 return NULL;
271 }
272
273 return (const __DRIconfig **)configs;
274 }
275
276 /**
277 * Roughly the converse of dri_fill_in_modes.
278 */
279 void
280 dri_fill_st_visual(struct st_visual *stvis, struct dri_screen *screen,
281 const struct gl_config *mode)
282 {
283 memset(stvis, 0, sizeof(*stvis));
284
285 if (!mode)
286 return;
287
288 /* Deduce the color format. */
289 switch (mode->redMask) {
290 case 0x00FF0000:
291 if (mode->alphaMask) {
292 assert(mode->alphaMask == 0xFF000000);
293 stvis->color_format = mode->sRGBCapable ?
294 PIPE_FORMAT_BGRA8888_SRGB :
295 PIPE_FORMAT_BGRA8888_UNORM;
296 } else {
297 stvis->color_format = mode->sRGBCapable ?
298 PIPE_FORMAT_BGRX8888_SRGB :
299 PIPE_FORMAT_BGRX8888_UNORM;
300 }
301 break;
302
303 case 0x000000FF:
304 if (mode->alphaMask) {
305 assert(mode->alphaMask == 0xFF000000);
306 stvis->color_format = mode->sRGBCapable ?
307 PIPE_FORMAT_RGBA8888_SRGB :
308 PIPE_FORMAT_RGBA8888_UNORM;
309 } else {
310 stvis->color_format = mode->sRGBCapable ?
311 PIPE_FORMAT_RGBX8888_SRGB :
312 PIPE_FORMAT_RGBX8888_UNORM;
313 }
314 break;
315
316 case 0x0000F800:
317 stvis->color_format = PIPE_FORMAT_B5G6R5_UNORM;
318 break;
319
320 default:
321 assert(!"unsupported visual: invalid red mask");
322 return;
323 }
324
325 if (mode->sampleBuffers) {
326 stvis->samples = mode->samples;
327 }
328
329 switch (mode->depthBits) {
330 default:
331 case 0:
332 stvis->depth_stencil_format = PIPE_FORMAT_NONE;
333 break;
334 case 16:
335 stvis->depth_stencil_format = PIPE_FORMAT_Z16_UNORM;
336 break;
337 case 24:
338 if (mode->stencilBits == 0) {
339 stvis->depth_stencil_format = (screen->d_depth_bits_last) ?
340 PIPE_FORMAT_Z24X8_UNORM:
341 PIPE_FORMAT_X8Z24_UNORM;
342 } else {
343 stvis->depth_stencil_format = (screen->sd_depth_bits_last) ?
344 PIPE_FORMAT_Z24_UNORM_S8_UINT:
345 PIPE_FORMAT_S8_UINT_Z24_UNORM;
346 }
347 break;
348 case 32:
349 stvis->depth_stencil_format = PIPE_FORMAT_Z32_UNORM;
350 break;
351 }
352
353 stvis->accum_format = (mode->haveAccumBuffer) ?
354 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
355
356 stvis->buffer_mask |= ST_ATTACHMENT_FRONT_LEFT_MASK;
357 stvis->render_buffer = ST_ATTACHMENT_FRONT_LEFT;
358 if (mode->doubleBufferMode) {
359 stvis->buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
360 stvis->render_buffer = ST_ATTACHMENT_BACK_LEFT;
361 }
362 if (mode->stereoMode) {
363 stvis->buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
364 if (mode->doubleBufferMode)
365 stvis->buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
366 }
367
368 if (mode->haveDepthBuffer || mode->haveStencilBuffer)
369 stvis->buffer_mask |= ST_ATTACHMENT_DEPTH_STENCIL_MASK;
370 /* let the state tracker allocate the accum buffer */
371 }
372
373 static boolean
374 dri_get_egl_image(struct st_manager *smapi,
375 void *egl_image,
376 struct st_egl_image *stimg)
377 {
378 struct dri_screen *screen = (struct dri_screen *)smapi;
379 __DRIimage *img = NULL;
380
381 if (screen->lookup_egl_image) {
382 img = screen->lookup_egl_image(screen, egl_image);
383 }
384
385 if (!img)
386 return FALSE;
387
388 stimg->texture = NULL;
389 pipe_resource_reference(&stimg->texture, img->texture);
390 switch (img->dri_components) {
391 case __DRI_IMAGE_COMPONENTS_Y_U_V:
392 stimg->format = PIPE_FORMAT_IYUV;
393 break;
394 case __DRI_IMAGE_COMPONENTS_Y_UV:
395 stimg->format = PIPE_FORMAT_NV12;
396 break;
397 default:
398 stimg->format = img->texture->format;
399 break;
400 }
401 stimg->level = img->level;
402 stimg->layer = img->layer;
403
404 return TRUE;
405 }
406
407 static int
408 dri_get_param(struct st_manager *smapi,
409 enum st_manager_param param)
410 {
411 struct dri_screen *screen = (struct dri_screen *)smapi;
412
413 switch(param) {
414 case ST_MANAGER_BROKEN_INVALIDATE:
415 return screen->broken_invalidate;
416 default:
417 return 0;
418 }
419 }
420
421 void
422 dri_destroy_screen_helper(struct dri_screen * screen)
423 {
424 if (screen->base.destroy)
425 screen->base.destroy(&screen->base);
426
427 if (screen->st_api && screen->st_api->destroy)
428 screen->st_api->destroy(screen->st_api);
429
430 if (screen->base.screen)
431 screen->base.screen->destroy(screen->base.screen);
432
433 mtx_destroy(&screen->opencl_func_mutex);
434 }
435
436 void
437 dri_destroy_screen(__DRIscreen * sPriv)
438 {
439 struct dri_screen *screen = dri_screen(sPriv);
440
441 dri_destroy_screen_helper(screen);
442
443 pipe_loader_release(&screen->dev, 1);
444
445 free(screen);
446 sPriv->driverPrivate = NULL;
447 sPriv->extensions = NULL;
448 }
449
450 static void
451 dri_postprocessing_init(struct dri_screen *screen)
452 {
453 unsigned i;
454
455 for (i = 0; i < PP_FILTERS; i++) {
456 screen->pp_enabled[i] = driQueryOptioni(&screen->dev->option_cache,
457 pp_filters[i].name);
458 }
459 }
460
461 static void
462 dri_set_background_context(struct st_context_iface *st,
463 struct util_queue_monitoring *queue_info)
464 {
465 struct dri_context *ctx = (struct dri_context *)st->st_manager_private;
466 const __DRIbackgroundCallableExtension *backgroundCallable =
467 ctx->sPriv->dri2.backgroundCallable;
468
469 /* Note: Mesa will only call this function if GL multithreading is enabled
470 * We only do that if the loader exposed the __DRI_BACKGROUND_CALLABLE
471 * extension. So we know that backgroundCallable is not NULL.
472 */
473 assert(backgroundCallable);
474 backgroundCallable->setBackgroundContext(ctx->cPriv->loaderPrivate);
475
476 if (ctx->hud)
477 hud_add_queue_for_monitoring(ctx->hud, queue_info);
478 }
479
480 void
481 dri_init_options(struct dri_screen *screen)
482 {
483 pipe_loader_load_options(screen->dev);
484
485 dri_fill_st_options(screen);
486 }
487
488 const __DRIconfig **
489 dri_init_screen_helper(struct dri_screen *screen,
490 struct pipe_screen *pscreen)
491 {
492 screen->base.screen = pscreen;
493 screen->base.get_egl_image = dri_get_egl_image;
494 screen->base.get_param = dri_get_param;
495 screen->base.set_background_context = dri_set_background_context;
496
497 screen->st_api = st_gl_api_create();
498 if (!screen->st_api)
499 return NULL;
500
501 if(pscreen->get_param(pscreen, PIPE_CAP_NPOT_TEXTURES))
502 screen->target = PIPE_TEXTURE_2D;
503 else
504 screen->target = PIPE_TEXTURE_RECT;
505
506 dri_postprocessing_init(screen);
507
508 screen->st_api->query_versions(screen->st_api, &screen->base,
509 &screen->options,
510 &screen->sPriv->max_gl_core_version,
511 &screen->sPriv->max_gl_compat_version,
512 &screen->sPriv->max_gl_es1_version,
513 &screen->sPriv->max_gl_es2_version);
514
515 return dri_fill_in_modes(screen);
516 }
517
518 /* vim: set sw=3 ts=8 sts=3 expandtab: */