xref: /aosp_15_r20/external/mesa3d/src/gallium/frontends/dri/dri_util.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 __DRIscreen *
driCreateNewScreen3(int scrn,int fd,const __DRIextension ** loader_extensions,enum dri_screen_type type,const __DRIconfig *** 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 __DRIconfig ***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 opaque_dri_screen(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(__DRIscreen * psp)199 void driDestroyScreen(__DRIscreen *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(dri_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 __DRIconfig * config,unsigned int index,unsigned int * value)225 driGetConfigAttribIndex(const __DRIconfig *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 __DRIconfig * config,unsigned int attrib,unsigned int * value)346 driGetConfigAttrib(const __DRIconfig *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 __DRIconfig 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 __DRIconfig * config,int index,unsigned int * attrib,unsigned int * value)360 driIndexConfigAttrib(const __DRIconfig *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 __DRIcontext *
driCreateContextAttribs(__DRIscreen * psp,int api,const __DRIconfig * config,__DRIcontext * shared,unsigned num_attribs,const uint32_t * attribs,unsigned * error,void * data)426 driCreateContextAttribs(__DRIscreen *psp, int api,
427                         const __DRIconfig *config,
428                         __DRIcontext *shared,
429                         unsigned num_attribs,
430                         const uint32_t *attribs,
431                         unsigned *error,
432                         void *data)
433 {
434     struct dri_screen *screen = dri_screen(psp);
435     const struct gl_config *modes = (config != NULL) ? &config->modes : NULL;
436     gl_api mesa_api;
437     struct __DriverContextConfig ctx_config;
438 
439     ctx_config.major_version = 1;
440     ctx_config.minor_version = 0;
441     ctx_config.flags = 0;
442     ctx_config.attribute_mask = 0;
443     ctx_config.priority = __DRI_CTX_PRIORITY_MEDIUM;
444 
445     assert((num_attribs == 0) || (attribs != NULL));
446 
447     switch (api) {
448     case __DRI_API_OPENGL:
449         mesa_api = API_OPENGL_COMPAT;
450         break;
451     case __DRI_API_GLES:
452         mesa_api = API_OPENGLES;
453         break;
454     case __DRI_API_GLES2:
455     case __DRI_API_GLES3:
456         mesa_api = API_OPENGLES2;
457         break;
458     case __DRI_API_OPENGL_CORE:
459         mesa_api = API_OPENGL_CORE;
460         break;
461     default:
462         *error = __DRI_CTX_ERROR_BAD_API;
463         return NULL;
464     }
465 
466     for (unsigned i = 0; i < num_attribs; i++) {
467         switch (attribs[i * 2]) {
468         case __DRI_CTX_ATTRIB_MAJOR_VERSION:
469             ctx_config.major_version = attribs[i * 2 + 1];
470             break;
471         case __DRI_CTX_ATTRIB_MINOR_VERSION:
472             ctx_config.minor_version = attribs[i * 2 + 1];
473             break;
474         case __DRI_CTX_ATTRIB_FLAGS:
475             ctx_config.flags = attribs[i * 2 + 1];
476             break;
477         case __DRI_CTX_ATTRIB_RESET_STRATEGY:
478             if (attribs[i * 2 + 1] != __DRI_CTX_RESET_NO_NOTIFICATION) {
479                 ctx_config.attribute_mask |=
480                     __DRIVER_CONTEXT_ATTRIB_RESET_STRATEGY;
481                 ctx_config.reset_strategy = attribs[i * 2 + 1];
482             } else {
483                 ctx_config.attribute_mask &=
484                     ~__DRIVER_CONTEXT_ATTRIB_RESET_STRATEGY;
485             }
486             break;
487         case __DRI_CTX_ATTRIB_PRIORITY:
488             ctx_config.attribute_mask |= __DRIVER_CONTEXT_ATTRIB_PRIORITY;
489             ctx_config.priority = attribs[i * 2 + 1];
490             break;
491         case __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR:
492             if (attribs[i * 2 + 1] != __DRI_CTX_RELEASE_BEHAVIOR_FLUSH) {
493                 ctx_config.attribute_mask |=
494                     __DRIVER_CONTEXT_ATTRIB_RELEASE_BEHAVIOR;
495                 ctx_config.release_behavior = attribs[i * 2 + 1];
496             } else {
497                 ctx_config.attribute_mask &=
498                     ~__DRIVER_CONTEXT_ATTRIB_RELEASE_BEHAVIOR;
499             }
500             break;
501         case __DRI_CTX_ATTRIB_NO_ERROR:
502             if (attribs[i * 2 + 1] != 0) {
503                ctx_config.attribute_mask |=
504                   __DRIVER_CONTEXT_ATTRIB_NO_ERROR;
505                ctx_config.no_error = attribs[i * 2 + 1];
506             } else {
507                ctx_config.attribute_mask &=
508                   ~__DRIVER_CONTEXT_ATTRIB_NO_ERROR;
509             }
510             break;
511         case __DRI_CTX_ATTRIB_PROTECTED:
512            if (attribs[i * 2 + 1]) {
513               ctx_config.attribute_mask |= __DRIVER_CONTEXT_ATTRIB_PROTECTED;
514            } else {
515               ctx_config.attribute_mask &= ~__DRIVER_CONTEXT_ATTRIB_PROTECTED;
516            }
517            break;
518         default:
519             /* We can't create a context that satisfies the requirements of an
520              * attribute that we don't understand.  Return failure.
521              */
522             assert(!"Should not get here.");
523             *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
524             return NULL;
525         }
526     }
527 
528     /* The specific Mesa driver may not support the GL_ARB_compatibilty
529      * extension or the compatibility profile.  In that case, we treat an
530      * API_OPENGL_COMPAT 3.1 as API_OPENGL_CORE. We reject API_OPENGL_COMPAT
531      * 3.2+ in any case.
532      */
533     if (mesa_api == API_OPENGL_COMPAT &&
534         ctx_config.major_version == 3 && ctx_config.minor_version == 1 &&
535         screen->max_gl_compat_version < 31)
536        mesa_api = API_OPENGL_CORE;
537 
538     /* The latest version of EGL_KHR_create_context spec says:
539      *
540      *     "If the EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR flag bit is set in
541      *     EGL_CONTEXT_FLAGS_KHR, then a <debug context> will be created.
542      *     [...] This bit is supported for OpenGL and OpenGL ES contexts.
543      *
544      * No other EGL_CONTEXT_OPENGL_*_BIT is legal for an ES context.
545      *
546      * However, Mesa's EGL layer translates the context attribute
547      * EGL_CONTEXT_OPENGL_ROBUST_ACCESS into the context flag
548      * __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS.  That attribute is legal for ES
549      * (with EGL 1.5 or EGL_EXT_create_context_robustness) and GL (only with
550      * EGL 1.5).
551      *
552      * From the EGL_EXT_create_context_robustness spec:
553      *
554      *     This extension is written against the OpenGL ES 2.0 Specification
555      *     but can apply to OpenGL ES 1.1 and up.
556      *
557      * From the EGL 1.5 (2014.08.27) spec, p55:
558      *
559      *     If the EGL_CONTEXT_OPENGL_ROBUST_ACCESS attribute is set to
560      *     EGL_TRUE, a context supporting robust buffer access will be created.
561      *     OpenGL contexts must support the GL_ARB_robustness extension, or
562      *     equivalent core API functional- ity. OpenGL ES contexts must support
563      *     the GL_EXT_robustness extension, or equivalent core API
564      *     functionality.
565      */
566     if (mesa_api != API_OPENGL_COMPAT
567         && mesa_api != API_OPENGL_CORE
568         && (ctx_config.flags & ~(__DRI_CTX_FLAG_DEBUG |
569                                  __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS))) {
570         *error = __DRI_CTX_ERROR_BAD_FLAG;
571         return NULL;
572     }
573 
574     /* There are no forward-compatible contexts before OpenGL 3.0.  The
575      * GLX_ARB_create_context spec says:
576      *
577      *     "Forward-compatible contexts are defined only for OpenGL versions
578      *     3.0 and later."
579      *
580      * Forward-looking contexts are supported by silently converting the
581      * requested API to API_OPENGL_CORE.
582      *
583      * In Mesa, a debug context is the same as a regular context.
584      */
585     if ((ctx_config.flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0) {
586        mesa_api = API_OPENGL_CORE;
587     }
588 
589     const uint32_t allowed_flags = (__DRI_CTX_FLAG_DEBUG
590                                     | __DRI_CTX_FLAG_FORWARD_COMPATIBLE
591                                     | __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
592                                     | __DRI_CTX_FLAG_RESET_ISOLATION);
593     if (ctx_config.flags & ~allowed_flags) {
594         *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
595         return NULL;
596     }
597 
598     *error = validate_context_version(screen, mesa_api,
599                                       ctx_config.major_version,
600                                       ctx_config.minor_version);
601     if (*error != __DRI_CTX_ERROR_SUCCESS)
602        return NULL;
603 
604     struct dri_context *ctx = dri_create_context(screen, mesa_api,
605                                                  modes, &ctx_config, error,
606                                                  dri_context(shared),
607                                                  data);
608     return opaque_dri_context(ctx);
609 }
610 
611 static __DRIcontext *
driCreateNewContextForAPI(__DRIscreen * screen,int api,const __DRIconfig * config,__DRIcontext * shared,void * data)612 driCreateNewContextForAPI(__DRIscreen *screen, int api,
613                           const __DRIconfig *config,
614                           __DRIcontext *shared, void *data)
615 {
616     unsigned error;
617 
618     return driCreateContextAttribs(screen, api, config, shared, 0, NULL,
619                                    &error, data);
620 }
621 
622 __DRIcontext *
driCreateNewContext(__DRIscreen * screen,const __DRIconfig * config,__DRIcontext * shared,void * data)623 driCreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
624                     __DRIcontext *shared, void *data)
625 {
626     return driCreateNewContextForAPI(screen, __DRI_API_OPENGL,
627                                      config, shared, data);
628 }
629 
630 /**
631  * Destroy the per-context private information.
632  *
633  * \internal
634  * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
635  * drmDestroyContext(), and finally frees \p contextPrivate.
636  */
637 void
driDestroyContext(__DRIcontext * pcp)638 driDestroyContext(__DRIcontext *pcp)
639 {
640     if (pcp)
641         dri_destroy_context(dri_context(pcp));
642 }
643 
644 int
driCopyContext(__DRIcontext * dest,__DRIcontext * src,unsigned long mask)645 driCopyContext(__DRIcontext *dest, __DRIcontext *src, unsigned long mask)
646 {
647     (void) dest;
648     (void) src;
649     (void) mask;
650     return GL_FALSE;
651 }
652 
653 /*@}*/
654 
655 
656 /*****************************************************************/
657 /** \name Context (un)binding functions                          */
658 /*****************************************************************/
659 /*@{*/
660 
661 /**
662  * This function takes both a read buffer and a draw buffer.  This is needed
663  * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
664  * function.
665  */
driBindContext(__DRIcontext * pcp,__DRIdrawable * pdp,__DRIdrawable * prp)666 int driBindContext(__DRIcontext *pcp,
667                    __DRIdrawable *pdp,
668                    __DRIdrawable *prp)
669 {
670    /*
671     ** Assume error checking is done properly in glXMakeCurrent before
672     ** calling driBindContext.
673     */
674 
675     if (!pcp)
676         return GL_FALSE;
677 
678     return dri_make_current(dri_context(pcp), dri_drawable(pdp),
679                             dri_drawable(prp));
680 }
681 
682 /**
683  * Unbind context.
684  *
685  * \param scrn the screen.
686  * \param gc context.
687  *
688  * \return \c GL_TRUE on success, or \c GL_FALSE on failure.
689  *
690  * \internal
691  * This function calls __DriverAPIRec::UnbindContext, and then decrements
692  * dri_drawable::refcount which must be non-zero for a successful
693  * return.
694  *
695  * While casting the opaque private pointers associated with the parameters
696  * into their respective real types it also assures they are not \c NULL.
697  */
driUnbindContext(__DRIcontext * pcp)698 int driUnbindContext(__DRIcontext *pcp)
699 {
700     /*
701     ** Assume error checking is done properly in glXMakeCurrent before
702     ** calling driUnbindContext.
703     */
704 
705     if (pcp == NULL)
706         return GL_FALSE;
707 
708     /*
709     ** Call dri_unbind_context before checking for valid drawables
710     ** to handle surfaceless contexts properly.
711     */
712     return dri_unbind_context(dri_context(pcp));
713 }
714 
715 /*@}*/
716 
717 void
driDestroyDrawable(__DRIdrawable * pdp)718 driDestroyDrawable(__DRIdrawable *pdp)
719 {
720     dri_put_drawable(dri_drawable(pdp));
721 }
722 
723 static int
dri2ConfigQueryb(__DRIscreen * psp,const char * var,unsigned char * val)724 dri2ConfigQueryb(__DRIscreen *psp, const char *var, unsigned char *val)
725 {
726    struct dri_screen *screen = dri_screen(psp);
727 
728    if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
729       return -1;
730 
731    *val = driQueryOptionb(&screen->optionCache, var);
732 
733    return 0;
734 }
735 
736 static int
dri2ConfigQueryi(__DRIscreen * psp,const char * var,int * val)737 dri2ConfigQueryi(__DRIscreen *psp, const char *var, int *val)
738 {
739    struct dri_screen *screen = dri_screen(psp);
740 
741    if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
742        !driCheckOption(&screen->optionCache, var, DRI_ENUM))
743       return -1;
744 
745     *val = driQueryOptioni(&screen->optionCache, var);
746 
747     return 0;
748 }
749 
750 static int
dri2ConfigQueryf(__DRIscreen * psp,const char * var,float * val)751 dri2ConfigQueryf(__DRIscreen *psp, const char *var, float *val)
752 {
753    struct dri_screen *screen = dri_screen(psp);
754 
755    if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
756       return -1;
757 
758     *val = driQueryOptionf(&screen->optionCache, var);
759 
760     return 0;
761 }
762 
763 static int
dri2ConfigQuerys(__DRIscreen * psp,const char * var,char ** val)764 dri2ConfigQuerys(__DRIscreen *psp, const char *var, char **val)
765 {
766    struct dri_screen *screen = dri_screen(psp);
767 
768    if (!driCheckOption(&screen->optionCache, var, DRI_STRING))
769       return -1;
770 
771     *val = driQueryOptionstr(&screen->optionCache, var);
772 
773     return 0;
774 }
775 
776 
777 /**
778  * \brief the DRI2ConfigQueryExtension configQueryb method
779  */
780 int
dri2GalliumConfigQueryb(__DRIscreen * sPriv,const char * var,unsigned char * val)781 dri2GalliumConfigQueryb(__DRIscreen *sPriv, const char *var,
782                         unsigned char *val)
783 {
784    struct dri_screen *screen = dri_screen(sPriv);
785 
786    if (!driCheckOption(&screen->dev->option_cache, var, DRI_BOOL))
787       return dri2ConfigQueryb(sPriv, var, val);
788 
789    *val = driQueryOptionb(&screen->dev->option_cache, var);
790 
791    return 0;
792 }
793 
794 /**
795  * \brief the DRI2ConfigQueryExtension configQueryi method
796  */
797 int
dri2GalliumConfigQueryi(__DRIscreen * sPriv,const char * var,int * val)798 dri2GalliumConfigQueryi(__DRIscreen *sPriv, const char *var, int *val)
799 {
800    struct dri_screen *screen = dri_screen(sPriv);
801 
802    if (!driCheckOption(&screen->dev->option_cache, var, DRI_INT) &&
803        !driCheckOption(&screen->dev->option_cache, var, DRI_ENUM))
804       return dri2ConfigQueryi(sPriv, var, val);
805 
806     *val = driQueryOptioni(&screen->dev->option_cache, var);
807 
808     return 0;
809 }
810 
811 /**
812  * \brief the DRI2ConfigQueryExtension configQueryf method
813  */
814 int
dri2GalliumConfigQueryf(__DRIscreen * sPriv,const char * var,float * val)815 dri2GalliumConfigQueryf(__DRIscreen *sPriv, const char *var, float *val)
816 {
817    struct dri_screen *screen = dri_screen(sPriv);
818 
819    if (!driCheckOption(&screen->dev->option_cache, var, DRI_FLOAT))
820       return dri2ConfigQueryf(sPriv, var, val);
821 
822     *val = driQueryOptionf(&screen->dev->option_cache, var);
823 
824     return 0;
825 }
826 
827 /**
828  * \brief the DRI2ConfigQueryExtension configQuerys method
829  */
830 int
dri2GalliumConfigQuerys(__DRIscreen * sPriv,const char * var,char ** val)831 dri2GalliumConfigQuerys(__DRIscreen *sPriv, const char *var, char **val)
832 {
833    struct dri_screen *screen = dri_screen(sPriv);
834 
835    if (!driCheckOption(&screen->dev->option_cache, var, DRI_STRING))
836       return dri2ConfigQuerys(sPriv, var, val);
837 
838     *val = driQueryOptionstr(&screen->dev->option_cache, var);
839 
840     return 0;
841 }
842 
843 /**
844  * \brief the DRI2ConfigQueryExtension struct.
845  *
846  * We first query the driver option cache. Then the dri2 option cache.
847  */
848 const __DRI2configQueryExtension dri2GalliumConfigQueryExtension = {
849    .base = { __DRI2_CONFIG_QUERY, 2 },
850 
851    .configQueryb        = dri2GalliumConfigQueryb,
852    .configQueryi        = dri2GalliumConfigQueryi,
853    .configQueryf        = dri2GalliumConfigQueryf,
854    .configQuerys        = dri2GalliumConfigQuerys,
855 };
856 
857 
858 unsigned int
driGetAPIMask(__DRIscreen * screen)859 driGetAPIMask(__DRIscreen *screen)
860 {
861     return dri_screen(screen)->api_mask;
862 }
863 
864 /**
865  * swrast swapbuffers entrypoint.
866  *
867  * DRI2 implements this inside the loader with only flushes handled by the
868  * driver.
869  */
870 void
driSwapBuffersWithDamage(__DRIdrawable * pdp,int nrects,const int * rects)871 driSwapBuffersWithDamage(__DRIdrawable *pdp, int nrects, const int *rects)
872 {
873    struct dri_drawable *drawable = dri_drawable(pdp);
874 
875    assert(drawable->screen->swrast_loader);
876 
877    drawable->swap_buffers_with_damage(drawable, nrects, rects);
878 }
879 
880 void
driSwapBuffers(__DRIdrawable * pdp)881 driSwapBuffers(__DRIdrawable *pdp)
882 {
883    struct dri_drawable *drawable = dri_drawable(pdp);
884 
885    assert(drawable->screen->swrast_loader);
886 
887    drawable->swap_buffers(drawable);
888 }
889 
890 int
driSWRastQueryBufferAge(__DRIdrawable * pdp)891 driSWRastQueryBufferAge(__DRIdrawable *pdp)
892 {
893    struct dri_drawable *drawable = dri_drawable(pdp);
894    return drawable->buffer_age;
895 }
896 
897 /*
898  * Note: the first match is returned, which is important for formats like
899  * __DRI_IMAGE_FORMAT_R8 which maps to both MESA_FORMAT_{R,L}_UNORM8
900  */
901 static const struct {
902    uint32_t    image_format;
903    GLenum internal_format;
904 } format_mapping[] = {
905    {
906       .image_format    = __DRI_IMAGE_FORMAT_RGB565,
907       .internal_format =        GL_RGB565,
908    },
909    {
910       .image_format    = __DRI_IMAGE_FORMAT_ARGB1555,
911       .internal_format =        GL_RGB5_A1,
912    },
913    {
914       .image_format    = __DRI_IMAGE_FORMAT_ABGR1555,
915       .internal_format =        GL_RGB5_A1,
916    },
917    {
918       .image_format    = __DRI_IMAGE_FORMAT_XRGB8888,
919       .internal_format =        GL_RGB8,
920    },
921    {
922       .image_format    = __DRI_IMAGE_FORMAT_ABGR16161616F,
923       .internal_format =        GL_RGBA16F,
924    },
925    {
926       .image_format    = __DRI_IMAGE_FORMAT_XBGR16161616F,
927       .internal_format =        GL_RGB16F,
928    },
929    {
930       .image_format    = __DRI_IMAGE_FORMAT_ABGR16161616,
931       .internal_format =        GL_RGBA16,
932    },
933    {
934       .image_format    = __DRI_IMAGE_FORMAT_XBGR16161616,
935       .internal_format =        GL_RGB16,
936    },
937    {
938       .image_format    = __DRI_IMAGE_FORMAT_ARGB2101010,
939       .internal_format =        GL_RGB10_A2,
940    },
941    {
942       .image_format    = __DRI_IMAGE_FORMAT_XRGB2101010,
943       .internal_format =        GL_RGB10,
944    },
945    {
946       .image_format    = __DRI_IMAGE_FORMAT_ABGR2101010,
947       .internal_format =        GL_RGB10_A2,
948    },
949    {
950       .image_format    = __DRI_IMAGE_FORMAT_XBGR2101010,
951       .internal_format =        GL_RGB10,
952    },
953    {
954       .image_format    = __DRI_IMAGE_FORMAT_ARGB8888,
955       .internal_format =        GL_RGBA8,
956    },
957    {
958       .image_format    = __DRI_IMAGE_FORMAT_ABGR8888,
959       .internal_format =        GL_RGBA8,
960    },
961    {
962       .image_format    = __DRI_IMAGE_FORMAT_XBGR8888,
963       .internal_format =        GL_RGB8,
964    },
965    {
966       .image_format    = __DRI_IMAGE_FORMAT_R8,
967       .internal_format =        GL_R8,
968    },
969    {
970       .image_format    = __DRI_IMAGE_FORMAT_R8,
971       .internal_format =        GL_R8,
972    },
973 #if UTIL_ARCH_LITTLE_ENDIAN
974    {
975       .image_format    = __DRI_IMAGE_FORMAT_GR88,
976       .internal_format =        GL_RG8,
977    },
978    {
979       .image_format    = __DRI_IMAGE_FORMAT_GR88,
980       .internal_format =        GL_RG8,
981    },
982 #endif
983    {
984       .image_format    = __DRI_IMAGE_FORMAT_SABGR8,
985       .internal_format =        GL_SRGB8_ALPHA8,
986    },
987    {
988       .image_format    = __DRI_IMAGE_FORMAT_SARGB8,
989       .internal_format =        GL_SRGB8_ALPHA8,
990    },
991    {
992       .image_format = __DRI_IMAGE_FORMAT_SXRGB8,
993       .internal_format =        GL_SRGB8,
994    },
995    {
996       .image_format    = __DRI_IMAGE_FORMAT_R16,
997       .internal_format =        GL_R16,
998    },
999    {
1000       .image_format    = __DRI_IMAGE_FORMAT_R16,
1001       .internal_format =        GL_R16,
1002    },
1003 #if UTIL_ARCH_LITTLE_ENDIAN
1004    {
1005       .image_format    = __DRI_IMAGE_FORMAT_GR1616,
1006       .internal_format =        GL_RG16,
1007    },
1008    {
1009       .image_format    = __DRI_IMAGE_FORMAT_GR1616,
1010       .internal_format =        GL_RG16,
1011    },
1012 #endif
1013    {
1014       .image_format    = __DRI_IMAGE_FORMAT_ARGB4444,
1015       .internal_format =        GL_RGBA4,
1016    },
1017    {
1018       .image_format    = __DRI_IMAGE_FORMAT_ABGR4444,
1019       .internal_format =        GL_RGBA4,
1020    },
1021 };
1022 
1023 uint32_t
driImageFormatToSizedInternalGLFormat(uint32_t image_format)1024 driImageFormatToSizedInternalGLFormat(uint32_t image_format)
1025 {
1026    for (size_t i = 0; i < ARRAY_SIZE(format_mapping); i++)
1027       if (format_mapping[i].image_format == image_format)
1028          return format_mapping[i].internal_format;
1029 
1030    return GL_NONE;
1031 }
1032 
dri_vblank_mode(__DRIscreen * driScreen)1033 static int dri_vblank_mode(__DRIscreen *driScreen)
1034 {
1035    GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1036 
1037    dri2GalliumConfigQueryi(driScreen, "vblank_mode", &vblank_mode);
1038 
1039    return vblank_mode;
1040 }
1041 
dri_get_initial_swap_interval(__DRIscreen * driScreen)1042 int dri_get_initial_swap_interval(__DRIscreen *driScreen)
1043 {
1044    int vblank_mode = dri_vblank_mode(driScreen);
1045 
1046    switch (vblank_mode) {
1047    case DRI_CONF_VBLANK_NEVER:
1048    case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1049       return 0;
1050    case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1051    case DRI_CONF_VBLANK_ALWAYS_SYNC:
1052    default:
1053       return 1;
1054    }
1055 }
1056 
dri_valid_swap_interval(__DRIscreen * driScreen,int interval)1057 bool dri_valid_swap_interval(__DRIscreen *driScreen, int interval)
1058 {
1059    int vblank_mode = dri_vblank_mode(driScreen);
1060 
1061    switch (vblank_mode) {
1062    case DRI_CONF_VBLANK_NEVER:
1063       if (interval != 0)
1064          return false;
1065       break;
1066    case DRI_CONF_VBLANK_ALWAYS_SYNC:
1067       if (interval <= 0)
1068          return false;
1069       break;
1070    default:
1071       break;
1072    }
1073 
1074    return true;
1075 }
1076 
1077 struct pipe_screen *
dri_get_pipe_screen(__DRIscreen * driScreen)1078 dri_get_pipe_screen(__DRIscreen *driScreen)
1079 {
1080    struct dri_screen *screen = dri_screen(driScreen);
1081    return screen->base.screen;
1082 }
1083 
1084 int
dri_get_screen_param(__DRIscreen * driScreen,enum pipe_cap param)1085 dri_get_screen_param(__DRIscreen *driScreen, enum pipe_cap param)
1086 {
1087    struct pipe_screen *screen = dri_get_pipe_screen(driScreen);
1088    return screen->get_param(screen, param);
1089 }
1090