1 /*
2 * (C) Copyright IBM Corporation 2002, 2004
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file dri_util.c
27 * DRI utility functions.
28 *
29 * This module acts as glue between GLX and the actual hardware driver. A DRI
30 * driver doesn't really \e have to use any of this - it's optional. But, some
31 * useful stuff is done here that otherwise would have to be duplicated in most
32 * drivers.
33 *
34 * Basically, these utility functions take care of some of the dirty details of
35 * screen initialization, context creation, context binding, DRM setup, etc.
36 *
37 * These functions are compiled into each DRI driver so libGL.so knows nothing
38 * about them.
39 */
40
41
42 #include <stdbool.h>
43 #include "dri_util.h"
44 #include "dri_context.h"
45 #include "dri_screen.h"
46 #include "dri_drawable.h"
47 #include "util/u_endian.h"
48 #include "util/u_memory.h"
49 #include "util/driconf.h"
50 #include "main/framebuffer.h"
51 #include "main/version.h"
52 #include "main/debug_output.h"
53 #include "main/errors.h"
54 #include "loader/loader.h"
55 #include "mesa_interface.h"
56 #include "loader_dri_helper.h"
57 #include "pipe-loader/pipe_loader.h"
58 #include "pipe/p_screen.h"
59
60 driOptionDescription __dri2ConfigOptions[] = {
61 DRI_CONF_SECTION_DEBUG
62 DRI_CONF_GLX_EXTENSION_OVERRIDE()
63 DRI_CONF_INDIRECT_GL_EXTENSION_OVERRIDE()
64 DRI_CONF_SECTION_END
65
66 DRI_CONF_SECTION_PERFORMANCE
67 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
68 DRI_CONF_BLOCK_ON_DEPLETED_BUFFERS(false)
69 DRI_CONF_SECTION_END
70 };
71
72 /*****************************************************************/
73 /** \name Screen handling functions */
74 /*****************************************************************/
75 /*@{*/
76
77 static void
setupLoaderExtensions(struct dri_screen * screen,const __DRIextension ** extensions)78 setupLoaderExtensions(struct dri_screen *screen,
79 const __DRIextension **extensions)
80 {
81 static const struct dri_extension_match matches[] = {
82 {__DRI_DRI2_LOADER, 1, offsetof(struct dri_screen, dri2.loader), true},
83 {__DRI_IMAGE_LOOKUP, 1, offsetof(struct dri_screen, dri2.image), true},
84 {__DRI_USE_INVALIDATE, 1, offsetof(struct dri_screen, dri2.useInvalidate), true},
85 {__DRI_BACKGROUND_CALLABLE, 1, offsetof(struct dri_screen, dri2.backgroundCallable), true},
86 {__DRI_SWRAST_LOADER, 1, offsetof(struct dri_screen, swrast_loader), true},
87 {__DRI_IMAGE_LOADER, 1, offsetof(struct dri_screen, image.loader), true},
88 {__DRI_MUTABLE_RENDER_BUFFER_LOADER, 1, offsetof(struct dri_screen, mutableRenderBuffer.loader), true},
89 {__DRI_KOPPER_LOADER, 1, offsetof(struct dri_screen, kopper_loader), true},
90 };
91 loader_bind_extensions(screen, matches, ARRAY_SIZE(matches), extensions);
92 }
93
94 /**
95 * This is the first entrypoint in the driver called by the DRI driver loader
96 * after dlopen()ing it.
97 *
98 * It's used to create global state for the driver across contexts on the same
99 * Display.
100 */
101 struct dri_screen *
driCreateNewScreen3(int scrn,int fd,const __DRIextension ** loader_extensions,enum dri_screen_type type,const struct dri_config *** driver_configs,bool driver_name_is_inferred,bool has_multibuffer,void * data)102 driCreateNewScreen3(int scrn, int fd,
103 const __DRIextension **loader_extensions,
104 enum dri_screen_type type,
105 const struct dri_config ***driver_configs, bool driver_name_is_inferred,
106 bool has_multibuffer, void *data)
107 {
108 struct dri_screen *screen;
109
110 screen = CALLOC_STRUCT(dri_screen);
111 if (!screen)
112 return NULL;
113
114 setupLoaderExtensions(screen, loader_extensions);
115 // dri2 drivers require working invalidate
116 if (fd != -1 && !screen->dri2.useInvalidate) {
117 free(screen);
118 return NULL;
119 }
120
121 screen->loaderPrivate = data;
122
123 screen->fd = fd;
124 screen->myNum = scrn;
125 screen->type = type;
126
127 /* Option parsing before ->InitScreen(), as some options apply there. */
128 driParseOptionInfo(&screen->optionInfo,
129 __dri2ConfigOptions, ARRAY_SIZE(__dri2ConfigOptions));
130 driParseConfigFiles(&screen->optionCache, &screen->optionInfo, screen->myNum,
131 "dri2", NULL, NULL, NULL, 0, NULL, 0);
132
133 (void) mtx_init(&screen->opencl_func_mutex, mtx_plain);
134
135 struct pipe_screen *pscreen = NULL;
136 switch (type) {
137 case DRI_SCREEN_DRI3:
138 pscreen = dri2_init_screen(screen, driver_name_is_inferred);
139 break;
140 case DRI_SCREEN_KOPPER:
141 pscreen = kopper_init_screen(screen, driver_name_is_inferred);
142 break;
143 case DRI_SCREEN_SWRAST:
144 pscreen = drisw_init_screen(screen, driver_name_is_inferred);
145 break;
146 case DRI_SCREEN_KMS_SWRAST:
147 pscreen = dri_swrast_kms_init_screen(screen, driver_name_is_inferred);
148 break;
149 default:
150 unreachable("unknown dri screen type");
151 }
152 if (pscreen == NULL) {
153 dri_destroy_screen(screen);
154 return NULL;
155 }
156 *driver_configs = dri_init_screen(screen, pscreen, has_multibuffer);
157 if (*driver_configs == NULL) {
158 dri_destroy_screen(screen);
159 return NULL;
160 }
161
162 struct gl_constants consts = { 0 };
163 gl_api api;
164 unsigned version;
165
166 api = API_OPENGLES2;
167 if (_mesa_override_gl_version_contextless(&consts, &api, &version))
168 screen->max_gl_es2_version = version;
169
170 api = API_OPENGL_COMPAT;
171 if (_mesa_override_gl_version_contextless(&consts, &api, &version)) {
172 screen->max_gl_core_version = version;
173 if (api == API_OPENGL_COMPAT)
174 screen->max_gl_compat_version = version;
175 }
176
177 screen->api_mask = 0;
178 if (screen->max_gl_compat_version > 0)
179 screen->api_mask |= (1 << __DRI_API_OPENGL);
180 if (screen->max_gl_core_version > 0)
181 screen->api_mask |= (1 << __DRI_API_OPENGL_CORE);
182 if (screen->max_gl_es1_version > 0)
183 screen->api_mask |= (1 << __DRI_API_GLES);
184 if (screen->max_gl_es2_version > 0)
185 screen->api_mask |= (1 << __DRI_API_GLES2);
186 if (screen->max_gl_es2_version >= 30)
187 screen->api_mask |= (1 << __DRI_API_GLES3);
188
189 return screen;
190 }
191
192 /**
193 * Destroy the per-screen private information.
194 *
195 * \internal
196 * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
197 * drmClose(), and finally frees \p screenPrivate.
198 */
driDestroyScreen(struct dri_screen * psp)199 void driDestroyScreen(struct dri_screen *psp)
200 {
201 if (psp) {
202 /* No interaction with the X-server is possible at this point. This
203 * routine is called after XCloseDisplay, so there is no protocol
204 * stream open to the X-server anymore.
205 */
206
207 dri_destroy_screen(psp);
208 }
209 }
210
211 /*@}*/
212
213 /* WARNING: HACK: Local defines to avoid pulling glx.h.
214 */
215 #define GLX_NONE 0x8000
216 #define GLX_DONT_CARE 0xFFFFFFFF
217
218 #define SIMPLE_CASE(attrib, field) case attrib: *value = config->modes.field; break
219
220 /**
221 * Return the value of a configuration attribute. The attribute is
222 * indicated by the index.
223 */
224 static int
driGetConfigAttribIndex(const struct dri_config * config,unsigned int index,unsigned int * value)225 driGetConfigAttribIndex(const struct dri_config *config,
226 unsigned int index, unsigned int *value)
227 {
228 switch (index + 1) {
229 SIMPLE_CASE(__DRI_ATTRIB_BUFFER_SIZE, rgbBits);
230 SIMPLE_CASE(__DRI_ATTRIB_RED_SIZE, redBits);
231 SIMPLE_CASE(__DRI_ATTRIB_GREEN_SIZE, greenBits);
232 SIMPLE_CASE(__DRI_ATTRIB_BLUE_SIZE, blueBits);
233 case __DRI_ATTRIB_LEVEL:
234 case __DRI_ATTRIB_LUMINANCE_SIZE:
235 case __DRI_ATTRIB_AUX_BUFFERS:
236 *value = 0;
237 break;
238 SIMPLE_CASE(__DRI_ATTRIB_ALPHA_SIZE, alphaBits);
239 case __DRI_ATTRIB_ALPHA_MASK_SIZE:
240 /* I have no idea what this value was ever meant to mean, it's
241 * never been set to anything, just say 0.
242 */
243 *value = 0;
244 break;
245 SIMPLE_CASE(__DRI_ATTRIB_DEPTH_SIZE, depthBits);
246 SIMPLE_CASE(__DRI_ATTRIB_STENCIL_SIZE, stencilBits);
247 SIMPLE_CASE(__DRI_ATTRIB_ACCUM_RED_SIZE, accumRedBits);
248 SIMPLE_CASE(__DRI_ATTRIB_ACCUM_GREEN_SIZE, accumGreenBits);
249 SIMPLE_CASE(__DRI_ATTRIB_ACCUM_BLUE_SIZE, accumBlueBits);
250 SIMPLE_CASE(__DRI_ATTRIB_ACCUM_ALPHA_SIZE, accumAlphaBits);
251 case __DRI_ATTRIB_SAMPLE_BUFFERS:
252 *value = !!config->modes.samples;
253 break;
254 SIMPLE_CASE(__DRI_ATTRIB_SAMPLES, samples);
255 case __DRI_ATTRIB_RENDER_TYPE:
256 /* no support for color index mode */
257 *value = __DRI_ATTRIB_RGBA_BIT;
258 if (config->modes.floatMode)
259 *value |= __DRI_ATTRIB_FLOAT_BIT;
260 break;
261 case __DRI_ATTRIB_CONFIG_CAVEAT:
262 if (config->modes.accumRedBits != 0)
263 *value = __DRI_ATTRIB_SLOW_BIT;
264 else
265 *value = 0;
266 break;
267 case __DRI_ATTRIB_CONFORMANT:
268 *value = GL_TRUE;
269 break;
270 SIMPLE_CASE(__DRI_ATTRIB_DOUBLE_BUFFER, doubleBufferMode);
271 SIMPLE_CASE(__DRI_ATTRIB_STEREO, stereoMode);
272 case __DRI_ATTRIB_TRANSPARENT_TYPE:
273 case __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE: /* horrible bc hack */
274 *value = GLX_NONE;
275 break;
276 case __DRI_ATTRIB_TRANSPARENT_RED_VALUE:
277 case __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE:
278 case __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE:
279 case __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE:
280 *value = GLX_DONT_CARE;
281 break;
282 case __DRI_ATTRIB_FLOAT_MODE:
283 *value = config->modes.floatMode;
284 break;
285 SIMPLE_CASE(__DRI_ATTRIB_RED_MASK, redMask);
286 SIMPLE_CASE(__DRI_ATTRIB_GREEN_MASK, greenMask);
287 SIMPLE_CASE(__DRI_ATTRIB_BLUE_MASK, blueMask);
288 SIMPLE_CASE(__DRI_ATTRIB_ALPHA_MASK, alphaMask);
289 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH:
290 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT:
291 case __DRI_ATTRIB_MAX_PBUFFER_PIXELS:
292 case __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH:
293 case __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT:
294 case __DRI_ATTRIB_VISUAL_SELECT_GROUP:
295 *value = 0;
296 break;
297 case __DRI_ATTRIB_SWAP_METHOD:
298 /* Not supported any more, but we have the __DRI_ATTRIB still defined
299 * for the X server's sake, and EGL will expect us to handle it because
300 * it iterates all __DRI_ATTRIBs.
301 */
302 *value = __DRI_ATTRIB_SWAP_UNDEFINED;
303 break;
304 case __DRI_ATTRIB_MAX_SWAP_INTERVAL:
305 *value = INT_MAX;
306 break;
307 case __DRI_ATTRIB_MIN_SWAP_INTERVAL:
308 *value = 0;
309 break;
310 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
311 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
312 case __DRI_ATTRIB_YINVERTED:
313 *value = GL_TRUE;
314 break;
315 case __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE:
316 *value = GL_FALSE;
317 break;
318 case __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS:
319 *value = __DRI_ATTRIB_TEXTURE_1D_BIT |
320 __DRI_ATTRIB_TEXTURE_2D_BIT |
321 __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT;
322 break;
323 SIMPLE_CASE(__DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE, sRGBCapable);
324 case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER:
325 *value = GL_FALSE;
326 break;
327 SIMPLE_CASE(__DRI_ATTRIB_RED_SHIFT, redShift);
328 SIMPLE_CASE(__DRI_ATTRIB_GREEN_SHIFT, greenShift);
329 SIMPLE_CASE(__DRI_ATTRIB_BLUE_SHIFT, blueShift);
330 SIMPLE_CASE(__DRI_ATTRIB_ALPHA_SHIFT, alphaShift);
331 default:
332 /* XXX log an error or smth */
333 return GL_FALSE;
334 }
335
336 return GL_TRUE;
337 }
338
339 /**
340 * Get the value of a configuration attribute.
341 * \param attrib the attribute (one of the _DRI_ATTRIB_x tokens)
342 * \param value returns the attribute's value
343 * \return 1 for success, 0 for failure
344 */
345 int
driGetConfigAttrib(const struct dri_config * config,unsigned int attrib,unsigned int * value)346 driGetConfigAttrib(const struct dri_config *config,
347 unsigned int attrib, unsigned int *value)
348 {
349 return driGetConfigAttribIndex(config, attrib - 1, value);
350 }
351
352 /**
353 * Get a configuration attribute name and value, given an index.
354 * \param index which field of the struct dri_config to query
355 * \param attrib returns the attribute name (one of the _DRI_ATTRIB_x tokens)
356 * \param value returns the attribute's value
357 * \return 1 for success, 0 for failure
358 */
359 int
driIndexConfigAttrib(const struct dri_config * config,int index,unsigned int * attrib,unsigned int * value)360 driIndexConfigAttrib(const struct dri_config *config, int index,
361 unsigned int *attrib, unsigned int *value)
362 {
363 if (driGetConfigAttribIndex(config, index, value)) {
364 *attrib = index + 1;
365 return GL_TRUE;
366 }
367
368 return GL_FALSE;
369 }
370
371 static int
validate_context_version(struct dri_screen * screen,int mesa_api,unsigned major_version,unsigned minor_version)372 validate_context_version(struct dri_screen *screen,
373 int mesa_api,
374 unsigned major_version,
375 unsigned minor_version)
376 {
377 unsigned req_version = 10 * major_version + minor_version;
378 unsigned max_version = 0;
379
380 if (major_version == 0 || major_version > 4)
381 return __DRI_CTX_ERROR_BAD_API;
382
383 if (mesa_api == API_OPENGL_COMPAT) {
384 if ((major_version == 4 && minor_version > 6) ||
385 (major_version == 3 && minor_version > 3) ||
386 (major_version == 2 && minor_version > 1) ||
387 (major_version == 1 && minor_version > 5))
388 return __DRI_CTX_ERROR_BAD_API;
389 max_version = screen->max_gl_compat_version;
390 } else if (mesa_api == API_OPENGLES) {
391 if (major_version > 1 || minor_version > 1)
392 return __DRI_CTX_ERROR_BAD_API;
393 max_version = screen->max_gl_es1_version;
394 } else if (mesa_api == API_OPENGLES2) {
395 if ((major_version > 3) ||
396 (major_version == 3 && minor_version > 2) ||
397 (major_version == 2 && minor_version > 0) ||
398 (major_version < 2))
399 return __DRI_CTX_ERROR_BAD_API;
400 max_version = screen->max_gl_es2_version;
401 } else if (mesa_api == API_OPENGL_CORE) {
402 if ((major_version == 4 && minor_version > 6) ||
403 (major_version == 3 && minor_version > 3) ||
404 (major_version < 3))
405 return __DRI_CTX_ERROR_BAD_API;
406 max_version = screen->max_gl_core_version;
407 } else {
408 return __DRI_CTX_ERROR_BAD_API;
409 }
410
411 if (max_version == 0)
412 return __DRI_CTX_ERROR_BAD_VERSION;
413
414 if (req_version > max_version)
415 return __DRI_CTX_ERROR_BAD_VERSION;
416
417 return __DRI_CTX_ERROR_SUCCESS;
418 }
419
420 /*****************************************************************/
421 /** \name Context handling functions */
422 /*****************************************************************/
423 /*@{*/
424
425 struct dri_context *
driCreateContextAttribs(struct dri_screen * screen,int api,const struct dri_config * config,struct dri_context * shared,unsigned num_attribs,const uint32_t * attribs,unsigned * error,void * data)426 driCreateContextAttribs(struct dri_screen *screen, int api,
427 const struct dri_config *config,
428 struct dri_context *shared,
429 unsigned num_attribs,
430 const uint32_t *attribs,
431 unsigned *error,
432 void *data)
433 {
434 const struct gl_config *modes = (config != NULL) ? &config->modes : NULL;
435 gl_api mesa_api;
436 struct __DriverContextConfig ctx_config;
437
438 ctx_config.major_version = 1;
439 ctx_config.minor_version = 0;
440 ctx_config.flags = 0;
441 ctx_config.attribute_mask = 0;
442 ctx_config.priority = __DRI_CTX_PRIORITY_MEDIUM;
443
444 assert((num_attribs == 0) || (attribs != NULL));
445
446 switch (api) {
447 case __DRI_API_OPENGL:
448 mesa_api = API_OPENGL_COMPAT;
449 break;
450 case __DRI_API_GLES:
451 mesa_api = API_OPENGLES;
452 break;
453 case __DRI_API_GLES2:
454 case __DRI_API_GLES3:
455 mesa_api = API_OPENGLES2;
456 break;
457 case __DRI_API_OPENGL_CORE:
458 mesa_api = API_OPENGL_CORE;
459 break;
460 default:
461 *error = __DRI_CTX_ERROR_BAD_API;
462 return NULL;
463 }
464
465 for (unsigned i = 0; i < num_attribs; i++) {
466 switch (attribs[i * 2]) {
467 case __DRI_CTX_ATTRIB_MAJOR_VERSION:
468 ctx_config.major_version = attribs[i * 2 + 1];
469 break;
470 case __DRI_CTX_ATTRIB_MINOR_VERSION:
471 ctx_config.minor_version = attribs[i * 2 + 1];
472 break;
473 case __DRI_CTX_ATTRIB_FLAGS:
474 ctx_config.flags = attribs[i * 2 + 1];
475 break;
476 case __DRI_CTX_ATTRIB_RESET_STRATEGY:
477 if (attribs[i * 2 + 1] != __DRI_CTX_RESET_NO_NOTIFICATION) {
478 ctx_config.attribute_mask |=
479 __DRIVER_CONTEXT_ATTRIB_RESET_STRATEGY;
480 ctx_config.reset_strategy = attribs[i * 2 + 1];
481 } else {
482 ctx_config.attribute_mask &=
483 ~__DRIVER_CONTEXT_ATTRIB_RESET_STRATEGY;
484 }
485 break;
486 case __DRI_CTX_ATTRIB_PRIORITY:
487 ctx_config.attribute_mask |= __DRIVER_CONTEXT_ATTRIB_PRIORITY;
488 ctx_config.priority = attribs[i * 2 + 1];
489 break;
490 case __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR:
491 if (attribs[i * 2 + 1] != __DRI_CTX_RELEASE_BEHAVIOR_FLUSH) {
492 ctx_config.attribute_mask |=
493 __DRIVER_CONTEXT_ATTRIB_RELEASE_BEHAVIOR;
494 ctx_config.release_behavior = attribs[i * 2 + 1];
495 } else {
496 ctx_config.attribute_mask &=
497 ~__DRIVER_CONTEXT_ATTRIB_RELEASE_BEHAVIOR;
498 }
499 break;
500 case __DRI_CTX_ATTRIB_NO_ERROR:
501 if (attribs[i * 2 + 1] != 0) {
502 ctx_config.attribute_mask |=
503 __DRIVER_CONTEXT_ATTRIB_NO_ERROR;
504 ctx_config.no_error = attribs[i * 2 + 1];
505 } else {
506 ctx_config.attribute_mask &=
507 ~__DRIVER_CONTEXT_ATTRIB_NO_ERROR;
508 }
509 break;
510 case __DRI_CTX_ATTRIB_PROTECTED:
511 if (attribs[i * 2 + 1]) {
512 ctx_config.attribute_mask |= __DRIVER_CONTEXT_ATTRIB_PROTECTED;
513 } else {
514 ctx_config.attribute_mask &= ~__DRIVER_CONTEXT_ATTRIB_PROTECTED;
515 }
516 break;
517 default:
518 /* We can't create a context that satisfies the requirements of an
519 * attribute that we don't understand. Return failure.
520 */
521 assert(!"Should not get here.");
522 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
523 return NULL;
524 }
525 }
526
527 /* The specific Mesa driver may not support the GL_ARB_compatibilty
528 * extension or the compatibility profile. In that case, we treat an
529 * API_OPENGL_COMPAT 3.1 as API_OPENGL_CORE. We reject API_OPENGL_COMPAT
530 * 3.2+ in any case.
531 */
532 if (mesa_api == API_OPENGL_COMPAT &&
533 ctx_config.major_version == 3 && ctx_config.minor_version == 1 &&
534 screen->max_gl_compat_version < 31)
535 mesa_api = API_OPENGL_CORE;
536
537 /* The latest version of EGL_KHR_create_context spec says:
538 *
539 * "If the EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR flag bit is set in
540 * EGL_CONTEXT_FLAGS_KHR, then a <debug context> will be created.
541 * [...] This bit is supported for OpenGL and OpenGL ES contexts.
542 *
543 * No other EGL_CONTEXT_OPENGL_*_BIT is legal for an ES context.
544 *
545 * However, Mesa's EGL layer translates the context attribute
546 * EGL_CONTEXT_OPENGL_ROBUST_ACCESS into the context flag
547 * __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS. That attribute is legal for ES
548 * (with EGL 1.5 or EGL_EXT_create_context_robustness) and GL (only with
549 * EGL 1.5).
550 *
551 * From the EGL_EXT_create_context_robustness spec:
552 *
553 * This extension is written against the OpenGL ES 2.0 Specification
554 * but can apply to OpenGL ES 1.1 and up.
555 *
556 * From the EGL 1.5 (2014.08.27) spec, p55:
557 *
558 * If the EGL_CONTEXT_OPENGL_ROBUST_ACCESS attribute is set to
559 * EGL_TRUE, a context supporting robust buffer access will be created.
560 * OpenGL contexts must support the GL_ARB_robustness extension, or
561 * equivalent core API functional- ity. OpenGL ES contexts must support
562 * the GL_EXT_robustness extension, or equivalent core API
563 * functionality.
564 */
565 if (mesa_api != API_OPENGL_COMPAT
566 && mesa_api != API_OPENGL_CORE
567 && (ctx_config.flags & ~(__DRI_CTX_FLAG_DEBUG |
568 __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS))) {
569 *error = __DRI_CTX_ERROR_BAD_FLAG;
570 return NULL;
571 }
572
573 /* There are no forward-compatible contexts before OpenGL 3.0. The
574 * GLX_ARB_create_context spec says:
575 *
576 * "Forward-compatible contexts are defined only for OpenGL versions
577 * 3.0 and later."
578 *
579 * Forward-looking contexts are supported by silently converting the
580 * requested API to API_OPENGL_CORE.
581 *
582 * In Mesa, a debug context is the same as a regular context.
583 */
584 if ((ctx_config.flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0) {
585 mesa_api = API_OPENGL_CORE;
586 }
587
588 const uint32_t allowed_flags = (__DRI_CTX_FLAG_DEBUG
589 | __DRI_CTX_FLAG_FORWARD_COMPATIBLE
590 | __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
591 | __DRI_CTX_FLAG_RESET_ISOLATION);
592 if (ctx_config.flags & ~allowed_flags) {
593 *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
594 return NULL;
595 }
596
597 *error = validate_context_version(screen, mesa_api,
598 ctx_config.major_version,
599 ctx_config.minor_version);
600 if (*error != __DRI_CTX_ERROR_SUCCESS)
601 return NULL;
602
603 struct dri_context *ctx = dri_create_context(screen, mesa_api,
604 modes, &ctx_config, error,
605 shared, data);
606 return ctx;
607 }
608
609 static struct dri_context *
driCreateNewContextForAPI(struct dri_screen * screen,int api,const struct dri_config * config,struct dri_context * shared,void * data)610 driCreateNewContextForAPI(struct dri_screen *screen, int api,
611 const struct dri_config *config,
612 struct dri_context *shared, void *data)
613 {
614 unsigned error;
615
616 return driCreateContextAttribs(screen, api, config, shared, 0, NULL,
617 &error, data);
618 }
619
620 struct dri_context *
driCreateNewContext(struct dri_screen * screen,const struct dri_config * config,struct dri_context * shared,void * data)621 driCreateNewContext(struct dri_screen *screen, const struct dri_config *config,
622 struct dri_context *shared, void *data)
623 {
624 return driCreateNewContextForAPI(screen, __DRI_API_OPENGL,
625 config, shared, data);
626 }
627
628 /**
629 * Destroy the per-context private information.
630 *
631 * \internal
632 * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
633 * drmDestroyContext(), and finally frees \p contextPrivate.
634 */
635 void
driDestroyContext(struct dri_context * ctx)636 driDestroyContext(struct dri_context *ctx)
637 {
638 if (ctx)
639 dri_destroy_context(ctx);
640 }
641
642 int
driCopyContext(struct dri_context * dest,struct dri_context * src,unsigned long mask)643 driCopyContext(struct dri_context *dest, struct dri_context *src, unsigned long mask)
644 {
645 (void) dest;
646 (void) src;
647 (void) mask;
648 return GL_FALSE;
649 }
650
651 /*@}*/
652
653
654 /*****************************************************************/
655 /** \name Context (un)binding functions */
656 /*****************************************************************/
657 /*@{*/
658
659 /**
660 * This function takes both a read buffer and a draw buffer. This is needed
661 * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
662 * function.
663 */
driBindContext(struct dri_context * ctx,struct dri_drawable * draw,struct dri_drawable * read)664 int driBindContext(struct dri_context *ctx,
665 struct dri_drawable *draw,
666 struct dri_drawable *read)
667 {
668 /*
669 ** Assume error checking is done properly in glXMakeCurrent before
670 ** calling driBindContext.
671 */
672
673 if (!ctx)
674 return GL_FALSE;
675
676 return dri_make_current(ctx, draw, read);
677 }
678
679 /**
680 * Unbind context.
681 *
682 * \param scrn the screen.
683 * \param gc context.
684 *
685 * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
686 *
687 * \internal
688 * This function calls __DriverAPIRec::UnbindContext, and then decrements
689 * dri_drawable::refcount which must be non-zero for a successful
690 * return.
691 *
692 * While casting the opaque private pointers associated with the parameters
693 * into their respective real types it also assures they are not \c NULL.
694 */
driUnbindContext(struct dri_context * ctx)695 int driUnbindContext(struct dri_context *ctx)
696 {
697 /*
698 ** Assume error checking is done properly in glXMakeCurrent before
699 ** calling driUnbindContext.
700 */
701
702 if (ctx == NULL)
703 return GL_FALSE;
704
705 /*
706 ** Call dri_unbind_context before checking for valid drawables
707 ** to handle surfaceless contexts properly.
708 */
709 return dri_unbind_context(ctx);
710 }
711
712 /*@}*/
713
714 void
driDestroyDrawable(struct dri_drawable * drawable)715 driDestroyDrawable(struct dri_drawable *drawable)
716 {
717 dri_put_drawable(drawable);
718 }
719
720 static int
dri2ConfigQueryb(struct dri_screen * screen,const char * var,unsigned char * val)721 dri2ConfigQueryb(struct dri_screen *screen, const char *var, unsigned char *val)
722 {
723 if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
724 return -1;
725
726 *val = driQueryOptionb(&screen->optionCache, var);
727
728 return 0;
729 }
730
731 static int
dri2ConfigQueryi(struct dri_screen * screen,const char * var,int * val)732 dri2ConfigQueryi(struct dri_screen *screen, const char *var, int *val)
733 {
734 if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
735 !driCheckOption(&screen->optionCache, var, DRI_ENUM))
736 return -1;
737
738 *val = driQueryOptioni(&screen->optionCache, var);
739
740 return 0;
741 }
742
743 static int
dri2ConfigQueryf(struct dri_screen * screen,const char * var,float * val)744 dri2ConfigQueryf(struct dri_screen *screen, const char *var, float *val)
745 {
746 if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
747 return -1;
748
749 *val = driQueryOptionf(&screen->optionCache, var);
750
751 return 0;
752 }
753
754 static int
dri2ConfigQuerys(struct dri_screen * screen,const char * var,char ** val)755 dri2ConfigQuerys(struct dri_screen *screen, const char *var, char **val)
756 {
757 if (!driCheckOption(&screen->optionCache, var, DRI_STRING))
758 return -1;
759
760 *val = driQueryOptionstr(&screen->optionCache, var);
761
762 return 0;
763 }
764
765
766 /**
767 * \brief the DRI2ConfigQueryExtension configQueryb method
768 */
769 int
dri2GalliumConfigQueryb(struct dri_screen * screen,const char * var,unsigned char * val)770 dri2GalliumConfigQueryb(struct dri_screen *screen, const char *var,
771 unsigned char *val)
772 {
773 if (!driCheckOption(&screen->dev->option_cache, var, DRI_BOOL))
774 return dri2ConfigQueryb(screen, var, val);
775
776 *val = driQueryOptionb(&screen->dev->option_cache, var);
777
778 return 0;
779 }
780
781 /**
782 * \brief the DRI2ConfigQueryExtension configQueryi method
783 */
784 int
dri2GalliumConfigQueryi(struct dri_screen * screen,const char * var,int * val)785 dri2GalliumConfigQueryi(struct dri_screen *screen, const char *var, int *val)
786 {
787 if (!driCheckOption(&screen->dev->option_cache, var, DRI_INT) &&
788 !driCheckOption(&screen->dev->option_cache, var, DRI_ENUM))
789 return dri2ConfigQueryi(screen, var, val);
790
791 *val = driQueryOptioni(&screen->dev->option_cache, var);
792
793 return 0;
794 }
795
796 /**
797 * \brief the DRI2ConfigQueryExtension configQueryf method
798 */
799 int
dri2GalliumConfigQueryf(struct dri_screen * screen,const char * var,float * val)800 dri2GalliumConfigQueryf(struct dri_screen *screen, const char *var, float *val)
801 {
802 if (!driCheckOption(&screen->dev->option_cache, var, DRI_FLOAT))
803 return dri2ConfigQueryf(screen, var, val);
804
805 *val = driQueryOptionf(&screen->dev->option_cache, var);
806
807 return 0;
808 }
809
810 /**
811 * \brief the DRI2ConfigQueryExtension configQuerys method
812 */
813 int
dri2GalliumConfigQuerys(struct dri_screen * screen,const char * var,char ** val)814 dri2GalliumConfigQuerys(struct dri_screen *screen, const char *var, char **val)
815 {
816 if (!driCheckOption(&screen->dev->option_cache, var, DRI_STRING))
817 return dri2ConfigQuerys(screen, var, val);
818
819 *val = driQueryOptionstr(&screen->dev->option_cache, var);
820
821 return 0;
822 }
823
824 /**
825 * \brief the DRI2ConfigQueryExtension struct.
826 *
827 * We first query the driver option cache. Then the dri2 option cache.
828 */
829 const __DRI2configQueryExtension dri2GalliumConfigQueryExtension = {
830 .base = { __DRI2_CONFIG_QUERY, 2 },
831
832 .configQueryb = dri2GalliumConfigQueryb,
833 .configQueryi = dri2GalliumConfigQueryi,
834 .configQueryf = dri2GalliumConfigQueryf,
835 .configQuerys = dri2GalliumConfigQuerys,
836 };
837
838
839 unsigned int
driGetAPIMask(struct dri_screen * screen)840 driGetAPIMask(struct dri_screen *screen)
841 {
842 return screen->api_mask;
843 }
844
845 /**
846 * swrast swapbuffers entrypoint.
847 *
848 * DRI2 implements this inside the loader with only flushes handled by the
849 * driver.
850 */
851 void
driSwapBuffersWithDamage(struct dri_drawable * drawable,int nrects,const int * rects)852 driSwapBuffersWithDamage(struct dri_drawable *drawable, int nrects, const int *rects)
853 {
854 assert(drawable->screen->swrast_loader);
855
856 drawable->swap_buffers_with_damage(drawable, nrects, rects);
857 }
858
859 void
driSwapBuffers(struct dri_drawable * drawable)860 driSwapBuffers(struct dri_drawable *drawable)
861 {
862 assert(drawable->screen->swrast_loader);
863
864 drawable->swap_buffers(drawable);
865 }
866
867 int
driSWRastQueryBufferAge(struct dri_drawable * drawable)868 driSWRastQueryBufferAge(struct dri_drawable *drawable)
869 {
870 return drawable->buffer_age;
871 }
872
873 /*
874 * Note: the first match is returned, which is important for formats like
875 * __DRI_IMAGE_FORMAT_R8 which maps to both MESA_FORMAT_{R,L}_UNORM8
876 */
877 static const struct {
878 uint32_t image_format;
879 GLenum internal_format;
880 } format_mapping[] = {
881 {
882 .image_format = __DRI_IMAGE_FORMAT_RGB565,
883 .internal_format = GL_RGB565,
884 },
885 {
886 .image_format = __DRI_IMAGE_FORMAT_ARGB1555,
887 .internal_format = GL_RGB5_A1,
888 },
889 {
890 .image_format = __DRI_IMAGE_FORMAT_ABGR1555,
891 .internal_format = GL_RGB5_A1,
892 },
893 {
894 .image_format = __DRI_IMAGE_FORMAT_XRGB8888,
895 .internal_format = GL_RGB8,
896 },
897 {
898 .image_format = __DRI_IMAGE_FORMAT_ABGR16161616F,
899 .internal_format = GL_RGBA16F,
900 },
901 {
902 .image_format = __DRI_IMAGE_FORMAT_XBGR16161616F,
903 .internal_format = GL_RGB16F,
904 },
905 {
906 .image_format = __DRI_IMAGE_FORMAT_ABGR16161616,
907 .internal_format = GL_RGBA16,
908 },
909 {
910 .image_format = __DRI_IMAGE_FORMAT_XBGR16161616,
911 .internal_format = GL_RGB16,
912 },
913 {
914 .image_format = __DRI_IMAGE_FORMAT_ARGB2101010,
915 .internal_format = GL_RGB10_A2,
916 },
917 {
918 .image_format = __DRI_IMAGE_FORMAT_XRGB2101010,
919 .internal_format = GL_RGB10,
920 },
921 {
922 .image_format = __DRI_IMAGE_FORMAT_ABGR2101010,
923 .internal_format = GL_RGB10_A2,
924 },
925 {
926 .image_format = __DRI_IMAGE_FORMAT_XBGR2101010,
927 .internal_format = GL_RGB10,
928 },
929 {
930 .image_format = __DRI_IMAGE_FORMAT_ARGB8888,
931 .internal_format = GL_RGBA8,
932 },
933 {
934 .image_format = __DRI_IMAGE_FORMAT_ABGR8888,
935 .internal_format = GL_RGBA8,
936 },
937 {
938 .image_format = __DRI_IMAGE_FORMAT_XBGR8888,
939 .internal_format = GL_RGB8,
940 },
941 {
942 .image_format = __DRI_IMAGE_FORMAT_R8,
943 .internal_format = GL_R8,
944 },
945 {
946 .image_format = __DRI_IMAGE_FORMAT_R8,
947 .internal_format = GL_R8,
948 },
949 #if UTIL_ARCH_LITTLE_ENDIAN
950 {
951 .image_format = __DRI_IMAGE_FORMAT_GR88,
952 .internal_format = GL_RG8,
953 },
954 {
955 .image_format = __DRI_IMAGE_FORMAT_GR88,
956 .internal_format = GL_RG8,
957 },
958 #endif
959 {
960 .image_format = __DRI_IMAGE_FORMAT_SABGR8,
961 .internal_format = GL_SRGB8_ALPHA8,
962 },
963 {
964 .image_format = __DRI_IMAGE_FORMAT_SARGB8,
965 .internal_format = GL_SRGB8_ALPHA8,
966 },
967 {
968 .image_format = __DRI_IMAGE_FORMAT_SXRGB8,
969 .internal_format = GL_SRGB8,
970 },
971 {
972 .image_format = __DRI_IMAGE_FORMAT_R16,
973 .internal_format = GL_R16,
974 },
975 {
976 .image_format = __DRI_IMAGE_FORMAT_R16,
977 .internal_format = GL_R16,
978 },
979 #if UTIL_ARCH_LITTLE_ENDIAN
980 {
981 .image_format = __DRI_IMAGE_FORMAT_GR1616,
982 .internal_format = GL_RG16,
983 },
984 {
985 .image_format = __DRI_IMAGE_FORMAT_GR1616,
986 .internal_format = GL_RG16,
987 },
988 #endif
989 {
990 .image_format = __DRI_IMAGE_FORMAT_ARGB4444,
991 .internal_format = GL_RGBA4,
992 },
993 {
994 .image_format = __DRI_IMAGE_FORMAT_ABGR4444,
995 .internal_format = GL_RGBA4,
996 },
997 };
998
999 uint32_t
driImageFormatToSizedInternalGLFormat(uint32_t image_format)1000 driImageFormatToSizedInternalGLFormat(uint32_t image_format)
1001 {
1002 for (size_t i = 0; i < ARRAY_SIZE(format_mapping); i++)
1003 if (format_mapping[i].image_format == image_format)
1004 return format_mapping[i].internal_format;
1005
1006 return GL_NONE;
1007 }
1008
dri_vblank_mode(struct dri_screen * driScreen)1009 static int dri_vblank_mode(struct dri_screen *driScreen)
1010 {
1011 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1012
1013 dri2GalliumConfigQueryi(driScreen, "vblank_mode", &vblank_mode);
1014
1015 return vblank_mode;
1016 }
1017
dri_get_initial_swap_interval(struct dri_screen * driScreen)1018 int dri_get_initial_swap_interval(struct dri_screen *driScreen)
1019 {
1020 int vblank_mode = dri_vblank_mode(driScreen);
1021
1022 switch (vblank_mode) {
1023 case DRI_CONF_VBLANK_NEVER:
1024 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1025 return 0;
1026 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1027 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1028 default:
1029 return 1;
1030 }
1031 }
1032
dri_valid_swap_interval(struct dri_screen * driScreen,int interval)1033 bool dri_valid_swap_interval(struct dri_screen *driScreen, int interval)
1034 {
1035 int vblank_mode = dri_vblank_mode(driScreen);
1036
1037 switch (vblank_mode) {
1038 case DRI_CONF_VBLANK_NEVER:
1039 if (interval != 0)
1040 return false;
1041 break;
1042 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1043 if (interval <= 0)
1044 return false;
1045 break;
1046 default:
1047 break;
1048 }
1049
1050 return true;
1051 }
1052
1053 struct pipe_screen *
dri_get_pipe_screen(struct dri_screen * screen)1054 dri_get_pipe_screen(struct dri_screen *screen)
1055 {
1056 return screen->base.screen;
1057 }
1058