• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2010 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Kristian Høgsberg <krh@bitplanet.net>
26  */
27 
28 #include <stdbool.h>
29 #include <stdint.h>
30 #include <stdbool.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <limits.h>
35 #include <dlfcn.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <c11/threads.h>
40 #include <time.h>
41 #ifdef HAVE_LIBDRM
42 #include <xf86drm.h>
43 #include "drm-uapi/drm_fourcc.h"
44 #endif
45 #include <GL/gl.h>
46 #include <GL/internal/dri_interface.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 
50 #ifdef HAVE_WAYLAND_PLATFORM
51 #include <wayland-client.h>
52 #include "wayland-drm.h"
53 #include "wayland-drm-client-protocol.h"
54 #include "linux-dmabuf-unstable-v1-client-protocol.h"
55 #endif
56 
57 #ifdef HAVE_X11_PLATFORM
58 #include "X11/Xlibint.h"
59 #endif
60 
61 #include "egldefines.h"
62 #include "egl_dri2.h"
63 #include "GL/mesa_glinterop.h"
64 #include "loader/loader.h"
65 #include "util/os_file.h"
66 #include "util/u_atomic.h"
67 #include "util/u_vector.h"
68 #include "mapi/glapi/glapi.h"
69 #include "util/bitscan.h"
70 #include "util/u_math.h"
71 
72 /* Additional definitions not yet in the drm_fourcc.h.
73  */
74 #ifndef DRM_FORMAT_P010
75 #define DRM_FORMAT_P010 	 fourcc_code('P', '0', '1', '0') /* 2x2 subsampled Cb:Cr plane 10 bits per channel */
76 #endif
77 
78 #ifndef DRM_FORMAT_P012
79 #define DRM_FORMAT_P012 	 fourcc_code('P', '0', '1', '2') /* 2x2 subsampled Cb:Cr plane 12 bits per channel */
80 #endif
81 
82 #ifndef DRM_FORMAT_P016
83 #define DRM_FORMAT_P016 	 fourcc_code('P', '0', '1', '6') /* 2x2 subsampled Cb:Cr plane 16 bits per channel */
84 #endif
85 
86 #define NUM_ATTRIBS 12
87 
88 static const struct dri2_pbuffer_visual {
89    const char *format_name;
90    unsigned int dri_image_format;
91    int rgba_shifts[4];
92    unsigned int rgba_sizes[4];
93 } dri2_pbuffer_visuals[] = {
94    {
95       "ABGR16F",
96       __DRI_IMAGE_FORMAT_ABGR16161616F,
97       { 0, 16, 32, 48 },
98       { 16, 16, 16, 16 }
99    },
100    {
101       "XBGR16F",
102       __DRI_IMAGE_FORMAT_XBGR16161616F,
103       { 0, 16, 32, -1 },
104       { 16, 16, 16, 0 }
105    },
106    {
107       "A2RGB10",
108       __DRI_IMAGE_FORMAT_ARGB2101010,
109       { 20, 10, 0, 30 },
110       { 10, 10, 10, 2 }
111    },
112    {
113       "X2RGB10",
114       __DRI_IMAGE_FORMAT_XRGB2101010,
115       { 20, 10, 0, -1 },
116       { 10, 10, 10, 0 }
117    },
118    {
119       "ARGB8888",
120       __DRI_IMAGE_FORMAT_ARGB8888,
121       { 16, 8, 0, 24 },
122       { 8, 8, 8, 8 }
123    },
124    {
125       "RGB888",
126       __DRI_IMAGE_FORMAT_XRGB8888,
127       { 16, 8, 0, -1 },
128       { 8, 8, 8, 0 }
129    },
130    {
131       "RGB565",
132       __DRI_IMAGE_FORMAT_RGB565,
133       { 11, 5, 0, -1 },
134       { 5, 6, 5, 0 }
135    },
136 };
137 
138 static void
dri_set_background_context(void * loaderPrivate)139 dri_set_background_context(void *loaderPrivate)
140 {
141    _EGLContext *ctx = _eglGetCurrentContext();
142    _EGLThreadInfo *t = _eglGetCurrentThread();
143 
144    _eglBindContextToThread(ctx, t);
145 }
146 
147 static void
dri2_gl_flush()148 dri2_gl_flush()
149 {
150    static void (*glFlush)(void);
151    static mtx_t glFlushMutex = _MTX_INITIALIZER_NP;
152 
153    mtx_lock(&glFlushMutex);
154    if (!glFlush)
155       glFlush = _glapi_get_proc_address("glFlush");
156    mtx_unlock(&glFlushMutex);
157 
158    /* if glFlush is not available things are horribly broken */
159    if (!glFlush) {
160       _eglLog(_EGL_WARNING, "DRI2: failed to find glFlush entry point");
161       return;
162    }
163 
164    glFlush();
165 }
166 
167 static GLboolean
dri_is_thread_safe(void * loaderPrivate)168 dri_is_thread_safe(void *loaderPrivate)
169 {
170    struct dri2_egl_surface *dri2_surf = loaderPrivate;
171    UNUSED _EGLDisplay *display =  dri2_surf->base.Resource.Display;
172 
173 #ifdef HAVE_X11_PLATFORM
174    Display *xdpy = (Display*)display->PlatformDisplay;
175 
176    /* Check Xlib is running in thread safe mode when running on EGL/X11-xlib
177     * platform
178     *
179     * 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
180     * It wll be NULL if XInitThreads wasn't called.
181     */
182    if (display->Platform == _EGL_PLATFORM_X11 && xdpy && !xdpy->lock_fns)
183       return false;
184 #endif
185 
186 #ifdef HAVE_WAYLAND_PLATFORM
187    if (display->Platform == _EGL_PLATFORM_WAYLAND)
188       return true;
189 #endif
190 
191    return true;
192 }
193 
194 const __DRIbackgroundCallableExtension background_callable_extension = {
195    .base = { __DRI_BACKGROUND_CALLABLE, 2 },
196 
197    .setBackgroundContext = dri_set_background_context,
198    .isThreadSafe         = dri_is_thread_safe,
199 };
200 
201 const __DRIuseInvalidateExtension use_invalidate = {
202    .base = { __DRI_USE_INVALIDATE, 1 }
203 };
204 
205 static void
dri2_get_pbuffer_drawable_info(__DRIdrawable * draw,int * x,int * y,int * w,int * h,void * loaderPrivate)206 dri2_get_pbuffer_drawable_info(__DRIdrawable * draw,
207                                int *x, int *y, int *w, int *h,
208                                void *loaderPrivate)
209 {
210    struct dri2_egl_surface *dri2_surf = loaderPrivate;
211 
212    *x = *y = 0;
213    *w = dri2_surf->base.Width;
214    *h = dri2_surf->base.Height;
215 }
216 
217 static int
dri2_get_bytes_per_pixel(struct dri2_egl_surface * dri2_surf)218 dri2_get_bytes_per_pixel(struct dri2_egl_surface *dri2_surf)
219 {
220    const int depth = dri2_surf->base.Config->BufferSize;
221    return depth ? util_next_power_of_two(depth / 8) : 0;
222 }
223 
224 static void
dri2_put_image(__DRIdrawable * draw,int op,int x,int y,int w,int h,char * data,void * loaderPrivate)225 dri2_put_image(__DRIdrawable * draw, int op,
226                int x, int y, int w, int h,
227                char *data, void *loaderPrivate)
228 {
229    struct dri2_egl_surface *dri2_surf = loaderPrivate;
230    const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
231    const int width = dri2_surf->base.Width;
232    const int height = dri2_surf->base.Height;
233    const int dst_stride = width*bpp;
234    const int src_stride = w*bpp;
235    const int x_offset = x*bpp;
236    int copy_width = src_stride;
237 
238    if (!dri2_surf->swrast_device_buffer)
239       dri2_surf->swrast_device_buffer = malloc(height*dst_stride);
240 
241    if (dri2_surf->swrast_device_buffer) {
242       const char *src = data;
243       char *dst = dri2_surf->swrast_device_buffer;
244 
245       dst += x_offset;
246       dst += y*dst_stride;
247 
248       /* Drivers are allowed to submit OOB PutImage requests, so clip here. */
249       if (copy_width > dst_stride - x_offset)
250          copy_width = dst_stride - x_offset;
251       if (h > height - y)
252          h = height - y;
253 
254       for (; 0 < h; --h) {
255          memcpy(dst, src, copy_width);
256          dst += dst_stride;
257          src += src_stride;
258       }
259    }
260 }
261 
262 static void
dri2_get_image(__DRIdrawable * read,int x,int y,int w,int h,char * data,void * loaderPrivate)263 dri2_get_image(__DRIdrawable * read,
264                int x, int y, int w, int h,
265                char *data, void *loaderPrivate)
266 {
267    struct dri2_egl_surface *dri2_surf = loaderPrivate;
268    const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
269    const int width = dri2_surf->base.Width;
270    const int height = dri2_surf->base.Height;
271    const int src_stride = width*bpp;
272    const int dst_stride = w*bpp;
273    const int x_offset = x*bpp;
274    int copy_width = dst_stride;
275    const char *src = dri2_surf->swrast_device_buffer;
276    char *dst = data;
277 
278    if (!src) {
279       memset(data, 0, copy_width * h);
280       return;
281    }
282 
283    src += x_offset;
284    src += y*src_stride;
285 
286    /* Drivers are allowed to submit OOB GetImage requests, so clip here. */
287    if (copy_width > src_stride - x_offset)
288       copy_width = src_stride - x_offset;
289    if (h > height - y)
290       h = height - y;
291 
292    for (; 0 < h; --h) {
293       memcpy(dst, src, copy_width);
294       src += src_stride;
295       dst += dst_stride;
296    }
297 
298 }
299 
300 /* HACK: technically we should have swrast_null, instead of these.
301  */
302 const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension = {
303    .base            = { __DRI_SWRAST_LOADER, 1 },
304    .getDrawableInfo = dri2_get_pbuffer_drawable_info,
305    .putImage        = dri2_put_image,
306    .getImage        = dri2_get_image,
307 };
308 
309 static const EGLint dri2_to_egl_attribute_map[__DRI_ATTRIB_MAX] = {
310    [__DRI_ATTRIB_BUFFER_SIZE ]          = EGL_BUFFER_SIZE,
311    [__DRI_ATTRIB_LEVEL]                 = EGL_LEVEL,
312    [__DRI_ATTRIB_LUMINANCE_SIZE]        = EGL_LUMINANCE_SIZE,
313    [__DRI_ATTRIB_DEPTH_SIZE]            = EGL_DEPTH_SIZE,
314    [__DRI_ATTRIB_STENCIL_SIZE]          = EGL_STENCIL_SIZE,
315    [__DRI_ATTRIB_SAMPLE_BUFFERS]        = EGL_SAMPLE_BUFFERS,
316    [__DRI_ATTRIB_SAMPLES]               = EGL_SAMPLES,
317    [__DRI_ATTRIB_MAX_PBUFFER_WIDTH]     = EGL_MAX_PBUFFER_WIDTH,
318    [__DRI_ATTRIB_MAX_PBUFFER_HEIGHT]    = EGL_MAX_PBUFFER_HEIGHT,
319    [__DRI_ATTRIB_MAX_PBUFFER_PIXELS]    = EGL_MAX_PBUFFER_PIXELS,
320    [__DRI_ATTRIB_MAX_SWAP_INTERVAL]     = EGL_MAX_SWAP_INTERVAL,
321    [__DRI_ATTRIB_MIN_SWAP_INTERVAL]     = EGL_MIN_SWAP_INTERVAL,
322    [__DRI_ATTRIB_YINVERTED]             = EGL_Y_INVERTED_NOK,
323 };
324 
325 const __DRIconfig *
dri2_get_dri_config(struct dri2_egl_config * conf,EGLint surface_type,EGLenum colorspace)326 dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,
327                     EGLenum colorspace)
328 {
329    const bool double_buffer = surface_type == EGL_WINDOW_BIT;
330    const bool srgb = colorspace == EGL_GL_COLORSPACE_SRGB_KHR;
331 
332    return conf->dri_config[double_buffer][srgb];
333 }
334 
335 static EGLBoolean
dri2_match_config(const _EGLConfig * conf,const _EGLConfig * criteria)336 dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria)
337 {
338    if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0)
339       return EGL_FALSE;
340 
341    if (!_eglMatchConfig(conf, criteria))
342       return EGL_FALSE;
343 
344    return EGL_TRUE;
345 }
346 
347 void
dri2_get_shifts_and_sizes(const __DRIcoreExtension * core,const __DRIconfig * config,int * shifts,unsigned int * sizes)348 dri2_get_shifts_and_sizes(const __DRIcoreExtension *core,
349                           const __DRIconfig *config, int *shifts,
350 		          unsigned int *sizes)
351 {
352    unsigned int mask;
353 
354    if (core->getConfigAttrib(config, __DRI_ATTRIB_RED_SHIFT, (unsigned int *)&shifts[0])) {
355       core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SHIFT, (unsigned int *)&shifts[1]);
356       core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SHIFT, (unsigned int *)&shifts[2]);
357       core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SHIFT, (unsigned int *)&shifts[3]);
358    } else {
359       /* Driver isn't exposing shifts, so convert masks to shifts */
360       core->getConfigAttrib(config, __DRI_ATTRIB_RED_MASK, &mask);
361       shifts[0] = ffs(mask) - 1;
362       core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_MASK, &mask);
363       shifts[1] = ffs(mask) - 1;
364       core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_MASK, &mask);
365       shifts[2] = ffs(mask) - 1;
366       core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_MASK, &mask);
367       shifts[3] = ffs(mask) - 1;
368    }
369 
370    core->getConfigAttrib(config, __DRI_ATTRIB_RED_SIZE, &sizes[0]);
371    core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SIZE, &sizes[1]);
372    core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SIZE, &sizes[2]);
373    core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SIZE, &sizes[3]);
374 }
375 
376 void
dri2_get_render_type_float(const __DRIcoreExtension * core,const __DRIconfig * config,bool * is_float)377 dri2_get_render_type_float(const __DRIcoreExtension *core,
378                            const __DRIconfig *config,
379                            bool *is_float)
380 {
381    unsigned int render_type;
382 
383    core->getConfigAttrib(config, __DRI_ATTRIB_RENDER_TYPE, &render_type);
384    *is_float = (render_type & __DRI_ATTRIB_FLOAT_BIT) ? true : false;
385 }
386 
387 unsigned int
dri2_image_format_for_pbuffer_config(struct dri2_egl_display * dri2_dpy,const __DRIconfig * config)388 dri2_image_format_for_pbuffer_config(struct dri2_egl_display *dri2_dpy,
389                                      const __DRIconfig *config)
390 {
391    int shifts[4];
392    unsigned int sizes[4];
393 
394    dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
395 
396    for (unsigned i = 0; i < ARRAY_SIZE(dri2_pbuffer_visuals); ++i) {
397       const struct dri2_pbuffer_visual *visual = &dri2_pbuffer_visuals[i];
398 
399       if (shifts[0] == visual->rgba_shifts[0] &&
400           shifts[1] == visual->rgba_shifts[1] &&
401           shifts[2] == visual->rgba_shifts[2] &&
402           shifts[3] == visual->rgba_shifts[3] &&
403           sizes[0] == visual->rgba_sizes[0] &&
404           sizes[1] == visual->rgba_sizes[1] &&
405           sizes[2] == visual->rgba_sizes[2] &&
406           sizes[3] == visual->rgba_sizes[3]) {
407          return visual->dri_image_format;
408       }
409    }
410 
411    return __DRI_IMAGE_FORMAT_NONE;
412 }
413 
414 struct dri2_egl_config *
dri2_add_config(_EGLDisplay * disp,const __DRIconfig * dri_config,int id,EGLint surface_type,const EGLint * attr_list,const int * rgba_shifts,const unsigned int * rgba_sizes)415 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
416                 EGLint surface_type, const EGLint *attr_list,
417                 const int *rgba_shifts, const unsigned int *rgba_sizes)
418 {
419    struct dri2_egl_config *conf;
420    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
421    _EGLConfig base;
422    unsigned int attrib, value, double_buffer;
423    bool srgb = false;
424    EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
425    int dri_shifts[4] = { -1, -1, -1, -1 };
426    unsigned int dri_sizes[4] = { 0, 0, 0, 0 };
427    _EGLConfig *matching_config;
428    EGLint num_configs = 0;
429    EGLint config_id;
430 
431    _eglInitConfig(&base, disp, id);
432 
433    double_buffer = 0;
434    bind_to_texture_rgb = 0;
435    bind_to_texture_rgba = 0;
436 
437    for (int i = 0; i < __DRI_ATTRIB_MAX; ++i) {
438       if (!dri2_dpy->core->indexConfigAttrib(dri_config, i, &attrib, &value))
439          break;
440 
441       switch (attrib) {
442       case __DRI_ATTRIB_RENDER_TYPE:
443          if (value & __DRI_ATTRIB_FLOAT_BIT)
444             base.ComponentType = EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT;
445          if (value & __DRI_ATTRIB_RGBA_BIT)
446             value = EGL_RGB_BUFFER;
447          else if (value & __DRI_ATTRIB_LUMINANCE_BIT)
448             value = EGL_LUMINANCE_BUFFER;
449          else
450             return NULL;
451          base.ColorBufferType = value;
452          break;
453 
454       case __DRI_ATTRIB_CONFIG_CAVEAT:
455          if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
456             value = EGL_NON_CONFORMANT_CONFIG;
457          else if (value & __DRI_ATTRIB_SLOW_BIT)
458             value = EGL_SLOW_CONFIG;
459          else
460             value = EGL_NONE;
461          base.ConfigCaveat = value;
462          break;
463 
464       case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
465          bind_to_texture_rgb = value;
466          break;
467 
468       case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
469          bind_to_texture_rgba = value;
470          break;
471 
472       case __DRI_ATTRIB_DOUBLE_BUFFER:
473          double_buffer = value;
474          break;
475 
476       case __DRI_ATTRIB_RED_SIZE:
477          dri_sizes[0] = value;
478          base.RedSize = value;
479          break;
480 
481       case __DRI_ATTRIB_RED_MASK:
482          dri_shifts[0] = ffs(value) - 1;
483          break;
484 
485       case __DRI_ATTRIB_RED_SHIFT:
486          dri_shifts[0] = value;
487          break;
488 
489       case __DRI_ATTRIB_GREEN_SIZE:
490          dri_sizes[1] = value;
491          base.GreenSize = value;
492          break;
493 
494       case __DRI_ATTRIB_GREEN_MASK:
495          dri_shifts[1] = ffs(value) - 1;
496          break;
497 
498       case __DRI_ATTRIB_GREEN_SHIFT:
499          dri_shifts[1] = value;
500          break;
501 
502       case __DRI_ATTRIB_BLUE_SIZE:
503          dri_sizes[2] = value;
504          base.BlueSize = value;
505          break;
506 
507       case __DRI_ATTRIB_BLUE_MASK:
508          dri_shifts[2] = ffs(value) - 1;
509          break;
510 
511       case __DRI_ATTRIB_BLUE_SHIFT:
512          dri_shifts[2] = value;
513          break;
514 
515      case __DRI_ATTRIB_ALPHA_SIZE:
516          dri_sizes[3] = value;
517          base.AlphaSize = value;
518          break;
519 
520       case __DRI_ATTRIB_ALPHA_MASK:
521          dri_shifts[3] = ffs(value) - 1;
522          break;
523 
524       case __DRI_ATTRIB_ALPHA_SHIFT:
525          dri_shifts[3] = value;
526          break;
527 
528       case __DRI_ATTRIB_ACCUM_RED_SIZE:
529       case __DRI_ATTRIB_ACCUM_GREEN_SIZE:
530       case __DRI_ATTRIB_ACCUM_BLUE_SIZE:
531       case __DRI_ATTRIB_ACCUM_ALPHA_SIZE:
532          /* Don't expose visuals with the accumulation buffer. */
533          if (value > 0)
534             return NULL;
535          break;
536 
537       case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE:
538          srgb = value != 0;
539          if (!disp->Extensions.KHR_gl_colorspace && srgb)
540             return NULL;
541          break;
542 
543       case __DRI_ATTRIB_MAX_PBUFFER_WIDTH:
544          base.MaxPbufferWidth = _EGL_MAX_PBUFFER_WIDTH;
545          break;
546       case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT:
547          base.MaxPbufferHeight = _EGL_MAX_PBUFFER_HEIGHT;
548          break;
549       case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER:
550          if (disp->Extensions.KHR_mutable_render_buffer)
551             surface_type |= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR;
552          break;
553       default:
554          key = dri2_to_egl_attribute_map[attrib];
555          if (key != 0)
556             _eglSetConfigKey(&base, key, value);
557          break;
558       }
559    }
560 
561    if (attr_list)
562       for (int i = 0; attr_list[i] != EGL_NONE; i += 2)
563          _eglSetConfigKey(&base, attr_list[i], attr_list[i+1]);
564 
565    if (rgba_shifts && memcmp(rgba_shifts, dri_shifts, sizeof(dri_shifts)))
566       return NULL;
567 
568    if (rgba_sizes && memcmp(rgba_sizes, dri_sizes, sizeof(dri_sizes)))
569       return NULL;
570 
571    base.NativeRenderable = EGL_TRUE;
572 
573    base.SurfaceType = surface_type;
574    if (surface_type & (EGL_PBUFFER_BIT |
575        (disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) {
576       base.BindToTextureRGB = bind_to_texture_rgb;
577       if (base.AlphaSize > 0)
578          base.BindToTextureRGBA = bind_to_texture_rgba;
579    }
580 
581    if (double_buffer) {
582       surface_type &= ~EGL_PIXMAP_BIT;
583    }
584 
585    /* No support for pbuffer + MSAA for now.
586     *
587     * XXX TODO: pbuffer + MSAA does not work and causes crashes.
588     * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
589     */
590    if (base.Samples) {
591       surface_type &= ~EGL_PBUFFER_BIT;
592    }
593 
594    if (!surface_type)
595       return NULL;
596 
597    base.RenderableType = disp->ClientAPIs;
598    base.Conformant = disp->ClientAPIs;
599 
600    base.MinSwapInterval = dri2_dpy->min_swap_interval;
601    base.MaxSwapInterval = dri2_dpy->max_swap_interval;
602 
603    if (!_eglValidateConfig(&base, EGL_FALSE)) {
604       _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);
605       return NULL;
606    }
607 
608    config_id = base.ConfigID;
609    base.ConfigID    = EGL_DONT_CARE;
610    base.SurfaceType = EGL_DONT_CARE;
611    num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1,
612                                  (_EGLArrayForEach) dri2_match_config, &base);
613 
614    if (num_configs == 1) {
615       conf = (struct dri2_egl_config *) matching_config;
616 
617       if (!conf->dri_config[double_buffer][srgb])
618          conf->dri_config[double_buffer][srgb] = dri_config;
619       else
620          /* a similar config type is already added (unlikely) => discard */
621          return NULL;
622    }
623    else if (num_configs == 0) {
624       conf = calloc(1, sizeof *conf);
625       if (conf == NULL)
626          return NULL;
627 
628       conf->dri_config[double_buffer][srgb] = dri_config;
629 
630       memcpy(&conf->base, &base, sizeof base);
631       conf->base.SurfaceType = 0;
632       conf->base.ConfigID = config_id;
633 
634       _eglLinkConfig(&conf->base);
635    }
636    else {
637       unreachable("duplicates should not be possible");
638       return NULL;
639    }
640 
641    conf->base.SurfaceType |= surface_type;
642 
643    return conf;
644 }
645 
646 EGLBoolean
dri2_add_pbuffer_configs_for_visuals(_EGLDisplay * disp)647 dri2_add_pbuffer_configs_for_visuals(_EGLDisplay *disp)
648 {
649    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
650    unsigned int format_count[ARRAY_SIZE(dri2_pbuffer_visuals)] = { 0 };
651    unsigned int config_count = 0;
652 
653    for (unsigned i = 0; dri2_dpy->driver_configs[i] != NULL; i++) {
654       for (unsigned j = 0; j < ARRAY_SIZE(dri2_pbuffer_visuals); j++) {
655          struct dri2_egl_config *dri2_conf;
656 
657          dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
658                config_count + 1, EGL_PBUFFER_BIT, NULL,
659                dri2_pbuffer_visuals[j].rgba_shifts, dri2_pbuffer_visuals[j].rgba_sizes);
660 
661          if (dri2_conf) {
662             if (dri2_conf->base.ConfigID == config_count + 1)
663                config_count++;
664             format_count[j]++;
665          }
666       }
667    }
668 
669    for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
670       if (!format_count[i]) {
671          _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
672                dri2_pbuffer_visuals[i].format_name);
673       }
674    }
675 
676    return (config_count != 0);
677 }
678 
679 __DRIimage *
dri2_lookup_egl_image(__DRIscreen * screen,void * image,void * data)680 dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
681 {
682    _EGLDisplay *disp = data;
683    struct dri2_egl_image *dri2_img;
684    _EGLImage *img;
685 
686    (void) screen;
687 
688    mtx_lock(&disp->Mutex);
689    img = _eglLookupImage(image, disp);
690    mtx_unlock(&disp->Mutex);
691 
692    if (img == NULL) {
693       _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image");
694       return NULL;
695    }
696 
697    dri2_img = dri2_egl_image(image);
698 
699    return dri2_img->dri_image;
700 }
701 
702 const __DRIimageLookupExtension image_lookup_extension = {
703    .base = { __DRI_IMAGE_LOOKUP, 1 },
704 
705    .lookupEGLImage       = dri2_lookup_egl_image
706 };
707 
708 struct dri2_extension_match {
709    const char *name;
710    int version;
711    int offset;
712 };
713 
714 static const struct dri2_extension_match dri3_driver_extensions[] = {
715    { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
716    { __DRI_IMAGE_DRIVER, 1, offsetof(struct dri2_egl_display, image_driver) },
717    { NULL, 0, 0 }
718 };
719 
720 static const struct dri2_extension_match dri2_driver_extensions[] = {
721    { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
722    { __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) },
723    { NULL, 0, 0 }
724 };
725 
726 static const struct dri2_extension_match dri2_core_extensions[] = {
727    { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) },
728    { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
729    { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
730    { NULL, 0, 0 }
731 };
732 
733 static const struct dri2_extension_match swrast_driver_extensions[] = {
734    { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
735    { __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) },
736    { NULL, 0, 0 }
737 };
738 
739 static const struct dri2_extension_match swrast_core_extensions[] = {
740    { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
741    { NULL, 0, 0 }
742 };
743 
744 static const struct dri2_extension_match optional_driver_extensions[] = {
745    { __DRI_CONFIG_OPTIONS, 1, offsetof(struct dri2_egl_display, configOptions) },
746    { NULL, 0, 0 }
747 };
748 
749 static const struct dri2_extension_match optional_core_extensions[] = {
750    { __DRI2_ROBUSTNESS, 1, offsetof(struct dri2_egl_display, robustness) },
751    { __DRI2_NO_ERROR, 1, offsetof(struct dri2_egl_display, no_error) },
752    { __DRI2_CONFIG_QUERY, 1, offsetof(struct dri2_egl_display, config) },
753    { __DRI2_FENCE, 1, offsetof(struct dri2_egl_display, fence) },
754    { __DRI2_BUFFER_DAMAGE, 1, offsetof(struct dri2_egl_display, buffer_damage) },
755    { __DRI2_RENDERER_QUERY, 1, offsetof(struct dri2_egl_display, rendererQuery) },
756    { __DRI2_INTEROP, 1, offsetof(struct dri2_egl_display, interop) },
757    { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
758    { __DRI2_FLUSH_CONTROL, 1, offsetof(struct dri2_egl_display, flush_control) },
759    { __DRI2_BLOB, 1, offsetof(struct dri2_egl_display, blob) },
760    { __DRI_MUTABLE_RENDER_BUFFER_DRIVER, 1, offsetof(struct dri2_egl_display, mutable_render_buffer) },
761    { NULL, 0, 0 }
762 };
763 
764 static EGLBoolean
dri2_bind_extensions(struct dri2_egl_display * dri2_dpy,const struct dri2_extension_match * matches,const __DRIextension ** extensions,bool optional)765 dri2_bind_extensions(struct dri2_egl_display *dri2_dpy,
766                      const struct dri2_extension_match *matches,
767                      const __DRIextension **extensions,
768                      bool optional)
769 {
770    int ret = EGL_TRUE;
771    void *field;
772 
773    for (int i = 0; extensions[i]; i++) {
774       _eglLog(_EGL_DEBUG, "found extension `%s'", extensions[i]->name);
775       for (int j = 0; matches[j].name; j++) {
776          if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
777              extensions[i]->version >= matches[j].version) {
778             field = ((char *) dri2_dpy + matches[j].offset);
779             *(const __DRIextension **) field = extensions[i];
780             _eglLog(_EGL_INFO, "found extension %s version %d",
781                     extensions[i]->name, extensions[i]->version);
782             break;
783          }
784       }
785    }
786 
787    for (int j = 0; matches[j].name; j++) {
788       field = ((char *) dri2_dpy + matches[j].offset);
789       if (*(const __DRIextension **) field == NULL) {
790          if (optional) {
791             _eglLog(_EGL_DEBUG, "did not find optional extension %s version %d",
792                     matches[j].name, matches[j].version);
793          } else {
794             _eglLog(_EGL_WARNING, "did not find extension %s version %d",
795                     matches[j].name, matches[j].version);
796             ret = EGL_FALSE;
797          }
798       }
799    }
800 
801    return ret;
802 }
803 
804 static const __DRIextension **
dri2_open_driver(_EGLDisplay * disp)805 dri2_open_driver(_EGLDisplay *disp)
806 {
807    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
808    static const char *search_path_vars[] = {
809       "LIBGL_DRIVERS_PATH",
810       NULL,
811    };
812 
813    return loader_open_driver(dri2_dpy->driver_name,
814                              &dri2_dpy->driver,
815                              search_path_vars);
816 }
817 
818 static EGLBoolean
dri2_load_driver_common(_EGLDisplay * disp,const struct dri2_extension_match * driver_extensions)819 dri2_load_driver_common(_EGLDisplay *disp,
820                         const struct dri2_extension_match *driver_extensions)
821 {
822    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
823    const __DRIextension **extensions;
824 
825    extensions = dri2_open_driver(disp);
826    if (!extensions)
827       return EGL_FALSE;
828 
829    if (!dri2_bind_extensions(dri2_dpy, driver_extensions, extensions, false)) {
830       dlclose(dri2_dpy->driver);
831       dri2_dpy->driver = NULL;
832       return EGL_FALSE;
833    }
834    dri2_dpy->driver_extensions = extensions;
835 
836    dri2_bind_extensions(dri2_dpy, optional_driver_extensions, extensions, true);
837 
838    return EGL_TRUE;
839 }
840 
841 EGLBoolean
dri2_load_driver(_EGLDisplay * disp)842 dri2_load_driver(_EGLDisplay *disp)
843 {
844    return dri2_load_driver_common(disp, dri2_driver_extensions);
845 }
846 
847 EGLBoolean
dri2_load_driver_dri3(_EGLDisplay * disp)848 dri2_load_driver_dri3(_EGLDisplay *disp)
849 {
850    return dri2_load_driver_common(disp, dri3_driver_extensions);
851 }
852 
853 EGLBoolean
dri2_load_driver_swrast(_EGLDisplay * disp)854 dri2_load_driver_swrast(_EGLDisplay *disp)
855 {
856    return dri2_load_driver_common(disp, swrast_driver_extensions);
857 }
858 
859 static unsigned
dri2_renderer_query_integer(struct dri2_egl_display * dri2_dpy,int param)860 dri2_renderer_query_integer(struct dri2_egl_display *dri2_dpy, int param)
861 {
862    const __DRI2rendererQueryExtension *rendererQuery = dri2_dpy->rendererQuery;
863    unsigned int value = 0;
864 
865    if (!rendererQuery ||
866        rendererQuery->queryInteger(dri2_dpy->dri_screen, param, &value) == -1)
867       return 0;
868 
869    return value;
870 }
871 
872 static const char *
dri2_query_driver_name(_EGLDisplay * disp)873 dri2_query_driver_name(_EGLDisplay *disp)
874 {
875     struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
876     return dri2_dpy->driver_name;
877 }
878 
879 static char *
dri2_query_driver_config(_EGLDisplay * disp)880 dri2_query_driver_config(_EGLDisplay *disp)
881 {
882     struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
883     const __DRIconfigOptionsExtension *ext = dri2_dpy->configOptions;
884 
885     if (ext->base.version >= 2)
886         return ext->getXml(dri2_dpy->driver_name);
887 
888     return strdup(ext->xml);
889 }
890 
891 
892 void
dri2_setup_screen(_EGLDisplay * disp)893 dri2_setup_screen(_EGLDisplay *disp)
894 {
895    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
896    unsigned int api_mask;
897 
898    /*
899     * EGL 1.5 specification defines the default value to 1. Moreover,
900     * eglSwapInterval() is required to clamp requested value to the supported
901     * range. Since the default value is implicitly assumed to be supported,
902     * use it as both minimum and maximum for the platforms that do not allow
903     * changing the interval. Platforms, which allow it (e.g. x11, wayland)
904     * override these values already.
905     */
906    dri2_dpy->min_swap_interval = 1;
907    dri2_dpy->max_swap_interval = 1;
908    dri2_dpy->default_swap_interval = 1;
909 
910    if (dri2_dpy->image_driver) {
911       api_mask = dri2_dpy->image_driver->getAPIMask(dri2_dpy->dri_screen);
912    } else if (dri2_dpy->dri2) {
913       api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);
914    } else {
915       assert(dri2_dpy->swrast);
916       api_mask = 1 << __DRI_API_OPENGL |
917                  1 << __DRI_API_GLES |
918                  1 << __DRI_API_GLES2 |
919                  1 << __DRI_API_GLES3;
920    }
921 
922    disp->ClientAPIs = 0;
923    if ((api_mask & (1 <<__DRI_API_OPENGL)) && _eglIsApiValid(EGL_OPENGL_API))
924       disp->ClientAPIs |= EGL_OPENGL_BIT;
925    if ((api_mask & (1 << __DRI_API_GLES)) && _eglIsApiValid(EGL_OPENGL_ES_API))
926       disp->ClientAPIs |= EGL_OPENGL_ES_BIT;
927    if ((api_mask & (1 << __DRI_API_GLES2)) && _eglIsApiValid(EGL_OPENGL_ES_API))
928       disp->ClientAPIs |= EGL_OPENGL_ES2_BIT;
929    if ((api_mask & (1 << __DRI_API_GLES3)) && _eglIsApiValid(EGL_OPENGL_ES_API))
930       disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR;
931 
932    assert(dri2_dpy->image_driver || dri2_dpy->dri2 || dri2_dpy->swrast);
933    disp->Extensions.KHR_no_config_context = EGL_TRUE;
934    disp->Extensions.KHR_surfaceless_context = EGL_TRUE;
935 
936    if (dri2_dpy->configOptions) {
937        disp->Extensions.MESA_query_driver = EGL_TRUE;
938    }
939 
940    /* Report back to EGL the bitmask of priorities supported */
941    disp->Extensions.IMG_context_priority =
942       dri2_renderer_query_integer(dri2_dpy,
943                                   __DRI2_RENDERER_HAS_CONTEXT_PRIORITY);
944 
945    disp->Extensions.EXT_pixel_format_float = EGL_TRUE;
946 
947    if (dri2_renderer_query_integer(dri2_dpy,
948                                    __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB))
949       disp->Extensions.KHR_gl_colorspace = EGL_TRUE;
950 
951    if (dri2_dpy->image_driver ||
952        (dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) ||
953        (dri2_dpy->swrast && dri2_dpy->swrast->base.version >= 3)) {
954       disp->Extensions.KHR_create_context = EGL_TRUE;
955 
956       if (dri2_dpy->robustness)
957          disp->Extensions.EXT_create_context_robustness = EGL_TRUE;
958    }
959 
960    if (dri2_dpy->no_error)
961       disp->Extensions.KHR_create_context_no_error = EGL_TRUE;
962 
963    if (dri2_dpy->fence) {
964       disp->Extensions.KHR_fence_sync = EGL_TRUE;
965       disp->Extensions.KHR_wait_sync = EGL_TRUE;
966       if (dri2_dpy->fence->get_fence_from_cl_event)
967          disp->Extensions.KHR_cl_event2 = EGL_TRUE;
968       if (dri2_dpy->fence->base.version >= 2 &&
969           dri2_dpy->fence->get_capabilities) {
970          unsigned capabilities =
971             dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen);
972          disp->Extensions.ANDROID_native_fence_sync =
973             (capabilities & __DRI_FENCE_CAP_NATIVE_FD) != 0;
974       }
975    }
976 
977    if (dri2_dpy->blob)
978       disp->Extensions.ANDROID_blob_cache = EGL_TRUE;
979 
980    disp->Extensions.KHR_reusable_sync = EGL_TRUE;
981 
982    if (dri2_dpy->image) {
983       if (dri2_dpy->image->base.version >= 10 &&
984           dri2_dpy->image->getCapabilities != NULL) {
985          int capabilities;
986 
987          capabilities = dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
988          disp->Extensions.MESA_drm_image = (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
989 
990          if (dri2_dpy->image->base.version >= 11)
991             disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
992       } else {
993          disp->Extensions.MESA_drm_image = EGL_TRUE;
994          if (dri2_dpy->image->base.version >= 11)
995             disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
996       }
997 
998       disp->Extensions.KHR_image_base = EGL_TRUE;
999       disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
1000       if (dri2_dpy->image->base.version >= 5 &&
1001           dri2_dpy->image->createImageFromTexture) {
1002          disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
1003          disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
1004 
1005          if (dri2_renderer_query_integer(dri2_dpy,
1006                                          __DRI2_RENDERER_HAS_TEXTURE_3D))
1007              disp->Extensions.KHR_gl_texture_3D_image = EGL_TRUE;
1008       }
1009 #ifdef HAVE_LIBDRM
1010       if (dri2_dpy->image->base.version >= 8 &&
1011           dri2_dpy->image->createImageFromDmaBufs) {
1012          disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE;
1013          disp->Extensions.EXT_image_dma_buf_import_modifiers = EGL_TRUE;
1014       }
1015 #endif
1016    }
1017 
1018    if (dri2_dpy->flush_control)
1019       disp->Extensions.KHR_context_flush_control = EGL_TRUE;
1020 
1021    if (dri2_dpy->buffer_damage && dri2_dpy->buffer_damage->set_damage_region)
1022       disp->Extensions.KHR_partial_update = EGL_TRUE;
1023 
1024    disp->Extensions.EXT_protected_surface =
1025       dri2_renderer_query_integer(dri2_dpy,
1026                                   __DRI2_RENDERER_HAS_PROTECTED_CONTENT);
1027 }
1028 
1029 void
dri2_setup_swap_interval(_EGLDisplay * disp,int max_swap_interval)1030 dri2_setup_swap_interval(_EGLDisplay *disp, int max_swap_interval)
1031 {
1032    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1033    GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1034 
1035    /* Allow driconf to override applications.*/
1036    if (dri2_dpy->config)
1037       dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
1038                                      "vblank_mode", &vblank_mode);
1039    switch (vblank_mode) {
1040    case DRI_CONF_VBLANK_NEVER:
1041       dri2_dpy->min_swap_interval = 0;
1042       dri2_dpy->max_swap_interval = 0;
1043       dri2_dpy->default_swap_interval = 0;
1044       break;
1045    case DRI_CONF_VBLANK_ALWAYS_SYNC:
1046       dri2_dpy->min_swap_interval = 1;
1047       dri2_dpy->max_swap_interval = max_swap_interval;
1048       dri2_dpy->default_swap_interval = 1;
1049       break;
1050    case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1051       dri2_dpy->min_swap_interval = 0;
1052       dri2_dpy->max_swap_interval = max_swap_interval;
1053       dri2_dpy->default_swap_interval = 0;
1054       break;
1055    default:
1056    case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1057       dri2_dpy->min_swap_interval = 0;
1058       dri2_dpy->max_swap_interval = max_swap_interval;
1059       dri2_dpy->default_swap_interval = 1;
1060       break;
1061    }
1062 }
1063 
1064 /* All platforms but DRM call this function to create the screen and populate
1065  * the driver_configs. DRM inherits that information from its display - GBM.
1066  */
1067 EGLBoolean
dri2_create_screen(_EGLDisplay * disp)1068 dri2_create_screen(_EGLDisplay *disp)
1069 {
1070    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1071 
1072    if (dri2_dpy->image_driver) {
1073       dri2_dpy->dri_screen =
1074          dri2_dpy->image_driver->createNewScreen2(0, dri2_dpy->fd,
1075                                                   dri2_dpy->loader_extensions,
1076                                                   dri2_dpy->driver_extensions,
1077                                                   &dri2_dpy->driver_configs,
1078                                                   disp);
1079    } else if (dri2_dpy->dri2) {
1080       if (dri2_dpy->dri2->base.version >= 4) {
1081          dri2_dpy->dri_screen =
1082             dri2_dpy->dri2->createNewScreen2(0, dri2_dpy->fd,
1083                                              dri2_dpy->loader_extensions,
1084                                              dri2_dpy->driver_extensions,
1085                                              &dri2_dpy->driver_configs, disp);
1086       } else {
1087          dri2_dpy->dri_screen =
1088             dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd,
1089                                             dri2_dpy->loader_extensions,
1090                                             &dri2_dpy->driver_configs, disp);
1091       }
1092    } else {
1093       assert(dri2_dpy->swrast);
1094       if (dri2_dpy->swrast->base.version >= 4) {
1095          dri2_dpy->dri_screen =
1096             dri2_dpy->swrast->createNewScreen2(0, dri2_dpy->loader_extensions,
1097                                                dri2_dpy->driver_extensions,
1098                                                &dri2_dpy->driver_configs, disp);
1099       } else {
1100          dri2_dpy->dri_screen =
1101             dri2_dpy->swrast->createNewScreen(0, dri2_dpy->loader_extensions,
1102                                               &dri2_dpy->driver_configs, disp);
1103       }
1104    }
1105 
1106    if (dri2_dpy->dri_screen == NULL) {
1107       _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen");
1108       return EGL_FALSE;
1109    }
1110 
1111    dri2_dpy->own_dri_screen = true;
1112    return EGL_TRUE;
1113 }
1114 
1115 EGLBoolean
dri2_setup_extensions(_EGLDisplay * disp)1116 dri2_setup_extensions(_EGLDisplay *disp)
1117 {
1118    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1119    const struct dri2_extension_match *mandatory_core_extensions;
1120    const __DRIextension **extensions;
1121 
1122    extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
1123 
1124    if (dri2_dpy->image_driver || dri2_dpy->dri2)
1125       mandatory_core_extensions = dri2_core_extensions;
1126    else
1127       mandatory_core_extensions = swrast_core_extensions;
1128 
1129    if (!dri2_bind_extensions(dri2_dpy, mandatory_core_extensions, extensions, false))
1130       return EGL_FALSE;
1131 
1132 #ifdef HAVE_DRI3_MODIFIERS
1133    dri2_dpy->multibuffers_available =
1134       (dri2_dpy->dri3_major_version > 1 || (dri2_dpy->dri3_major_version == 1 &&
1135                                             dri2_dpy->dri3_minor_version >= 2)) &&
1136       (dri2_dpy->present_major_version > 1 || (dri2_dpy->present_major_version == 1 &&
1137                                                dri2_dpy->present_minor_version >= 2)) &&
1138       (dri2_dpy->image && dri2_dpy->image->base.version >= 15);
1139 #endif
1140 
1141    dri2_bind_extensions(dri2_dpy, optional_core_extensions, extensions, true);
1142    return EGL_TRUE;
1143 }
1144 
1145 /**
1146  * Called via eglInitialize(), drv->Initialize().
1147  *
1148  * This must be guaranteed to be called exactly once, even if eglInitialize is
1149  * called many times (without a eglTerminate in between).
1150  */
1151 static EGLBoolean
dri2_initialize(_EGLDisplay * disp)1152 dri2_initialize(_EGLDisplay *disp)
1153 {
1154    EGLBoolean ret = EGL_FALSE;
1155    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1156 
1157    /* In the case where the application calls eglMakeCurrent(context1),
1158     * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
1159     * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
1160     * initialized, as we need it to be able to free context1 correctly.
1161     *
1162     * It would probably be safest to forcibly release the display with
1163     * dri2_display_release, to make sure the display is reinitialized correctly.
1164     * However, the EGL spec states that we need to keep a reference to the
1165     * current context (so we cannot call dri2_make_current(NULL)), and therefore
1166     * we would leak context1 as we would be missing the old display connection
1167     * to free it up correctly.
1168     */
1169    if (dri2_dpy) {
1170       dri2_dpy->ref_count++;
1171       return EGL_TRUE;
1172    }
1173 
1174    loader_set_logger(_eglLog);
1175 
1176    switch (disp->Platform) {
1177    case _EGL_PLATFORM_SURFACELESS:
1178       ret = dri2_initialize_surfaceless(disp);
1179       break;
1180    case _EGL_PLATFORM_DEVICE:
1181       ret = dri2_initialize_device(disp);
1182       break;
1183    case _EGL_PLATFORM_X11:
1184       ret = dri2_initialize_x11(disp);
1185       break;
1186    case _EGL_PLATFORM_DRM:
1187       ret = dri2_initialize_drm(disp);
1188       break;
1189    case _EGL_PLATFORM_WAYLAND:
1190       ret = dri2_initialize_wayland(disp);
1191       break;
1192    case _EGL_PLATFORM_ANDROID:
1193       ret = dri2_initialize_android(disp);
1194       break;
1195    default:
1196       unreachable("Callers ensure we cannot get here.");
1197       return EGL_FALSE;
1198    }
1199 
1200    if (!ret)
1201       return EGL_FALSE;
1202 
1203    dri2_dpy = dri2_egl_display(disp);
1204    dri2_dpy->ref_count++;
1205 
1206    return EGL_TRUE;
1207 }
1208 
1209 /**
1210  * Decrement display reference count, and free up display if necessary.
1211  */
1212 static void
dri2_display_release(_EGLDisplay * disp)1213 dri2_display_release(_EGLDisplay *disp)
1214 {
1215    struct dri2_egl_display *dri2_dpy;
1216 
1217    if (!disp)
1218       return;
1219 
1220    dri2_dpy = dri2_egl_display(disp);
1221 
1222    assert(dri2_dpy->ref_count > 0);
1223    dri2_dpy->ref_count--;
1224 
1225    if (dri2_dpy->ref_count > 0)
1226       return;
1227 
1228    _eglCleanupDisplay(disp);
1229    dri2_display_destroy(disp);
1230 }
1231 
1232 void
dri2_display_destroy(_EGLDisplay * disp)1233 dri2_display_destroy(_EGLDisplay *disp)
1234 {
1235    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1236 
1237    if (dri2_dpy->own_dri_screen) {
1238       if (dri2_dpy->vtbl && dri2_dpy->vtbl->close_screen_notify)
1239          dri2_dpy->vtbl->close_screen_notify(disp);
1240       dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1241    }
1242    if (dri2_dpy->fd >= 0)
1243       close(dri2_dpy->fd);
1244    if (dri2_dpy->driver)
1245       dlclose(dri2_dpy->driver);
1246    free(dri2_dpy->driver_name);
1247 
1248 #ifdef HAVE_WAYLAND_PLATFORM
1249    free(dri2_dpy->device_name);
1250 #endif
1251 
1252    switch (disp->Platform) {
1253    case _EGL_PLATFORM_X11:
1254       dri2_teardown_x11(dri2_dpy);
1255       break;
1256    case _EGL_PLATFORM_DRM:
1257       dri2_teardown_drm(dri2_dpy);
1258       break;
1259    case _EGL_PLATFORM_WAYLAND:
1260       dri2_teardown_wayland(dri2_dpy);
1261       break;
1262    default:
1263       /* TODO: add teardown for other platforms */
1264       break;
1265    }
1266 
1267    /* The drm platform does not create the screen/driver_configs but reuses
1268     * the ones from the gbm device. As such the gbm itself is responsible
1269     * for the cleanup.
1270     */
1271    if (disp->Platform != _EGL_PLATFORM_DRM && dri2_dpy->driver_configs) {
1272       for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++)
1273          free((__DRIconfig *) dri2_dpy->driver_configs[i]);
1274       free(dri2_dpy->driver_configs);
1275    }
1276    free(dri2_dpy);
1277    disp->DriverData = NULL;
1278 }
1279 
1280 __DRIbuffer *
dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface * dri2_surf,unsigned int att,unsigned int format)1281 dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
1282                                     unsigned int att, unsigned int format)
1283 {
1284    struct dri2_egl_display *dri2_dpy =
1285       dri2_egl_display(dri2_surf->base.Resource.Display);
1286 
1287    if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
1288       return NULL;
1289 
1290    if (!dri2_surf->local_buffers[att]) {
1291       dri2_surf->local_buffers[att] =
1292          dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
1293                                         dri2_surf->base.Width, dri2_surf->base.Height);
1294    }
1295 
1296    return dri2_surf->local_buffers[att];
1297 }
1298 
1299 void
dri2_egl_surface_free_local_buffers(struct dri2_egl_surface * dri2_surf)1300 dri2_egl_surface_free_local_buffers(struct dri2_egl_surface *dri2_surf)
1301 {
1302    struct dri2_egl_display *dri2_dpy =
1303       dri2_egl_display(dri2_surf->base.Resource.Display);
1304 
1305    for (int i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
1306       if (dri2_surf->local_buffers[i]) {
1307          dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
1308                                        dri2_surf->local_buffers[i]);
1309          dri2_surf->local_buffers[i] = NULL;
1310       }
1311    }
1312 }
1313 
1314 /**
1315  * Called via eglTerminate(), drv->Terminate().
1316  *
1317  * This must be guaranteed to be called exactly once, even if eglTerminate is
1318  * called many times (without a eglInitialize in between).
1319  */
1320 static EGLBoolean
dri2_terminate(_EGLDisplay * disp)1321 dri2_terminate(_EGLDisplay *disp)
1322 {
1323    /* Release all non-current Context/Surfaces. */
1324    _eglReleaseDisplayResources(disp);
1325 
1326    dri2_display_release(disp);
1327 
1328    return EGL_TRUE;
1329 }
1330 
1331 /**
1332  * Set the error code after a call to
1333  * dri2_egl_display::dri2::createContextAttribs.
1334  */
1335 static void
dri2_create_context_attribs_error(int dri_error)1336 dri2_create_context_attribs_error(int dri_error)
1337 {
1338    EGLint egl_error;
1339 
1340    switch (dri_error) {
1341    case __DRI_CTX_ERROR_SUCCESS:
1342       return;
1343 
1344    case __DRI_CTX_ERROR_NO_MEMORY:
1345       egl_error = EGL_BAD_ALLOC;
1346       break;
1347 
1348   /* From the EGL_KHR_create_context spec, section "Errors":
1349    *
1350    *   * If <config> does not support a client API context compatible
1351    *     with the requested API major and minor version, [...] context flags,
1352    *     and context reset notification behavior (for client API types where
1353    *     these attributes are supported), then an EGL_BAD_MATCH error is
1354    *     generated.
1355    *
1356    *   * If an OpenGL ES context is requested and the values for
1357    *     attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1358    *     EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1359    *     is not defined, than an EGL_BAD_MATCH error is generated.
1360    *
1361    *   * If an OpenGL context is requested, the requested version is
1362    *     greater than 3.2, and the value for attribute
1363    *     EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1364    *     bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1365    *     EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1366    *     one of these bits set; or if the implementation does not support
1367    *     the requested profile, then an EGL_BAD_MATCH error is generated.
1368    */
1369    case __DRI_CTX_ERROR_BAD_API:
1370    case __DRI_CTX_ERROR_BAD_VERSION:
1371    case __DRI_CTX_ERROR_BAD_FLAG:
1372       egl_error = EGL_BAD_MATCH;
1373       break;
1374 
1375   /* From the EGL_KHR_create_context spec, section "Errors":
1376    *
1377    *   * If an attribute name or attribute value in <attrib_list> is not
1378    *     recognized (including unrecognized bits in bitmask attributes),
1379    *     then an EGL_BAD_ATTRIBUTE error is generated."
1380    */
1381    case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE:
1382    case __DRI_CTX_ERROR_UNKNOWN_FLAG:
1383       egl_error = EGL_BAD_ATTRIBUTE;
1384       break;
1385 
1386    default:
1387       assert(!"unknown dri_error code");
1388       egl_error = EGL_BAD_MATCH;
1389       break;
1390    }
1391 
1392    _eglError(egl_error, "dri2_create_context");
1393 }
1394 
1395 static bool
dri2_fill_context_attribs(struct dri2_egl_context * dri2_ctx,struct dri2_egl_display * dri2_dpy,uint32_t * ctx_attribs,unsigned * num_attribs)1396 dri2_fill_context_attribs(struct dri2_egl_context *dri2_ctx,
1397                           struct dri2_egl_display *dri2_dpy,
1398                           uint32_t *ctx_attribs,
1399                           unsigned *num_attribs)
1400 {
1401    int pos = 0;
1402 
1403    assert(*num_attribs >= NUM_ATTRIBS);
1404 
1405    ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
1406    ctx_attribs[pos++] = dri2_ctx->base.ClientMajorVersion;
1407    ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
1408    ctx_attribs[pos++] = dri2_ctx->base.ClientMinorVersion;
1409 
1410    if (dri2_ctx->base.Flags != 0 || dri2_ctx->base.NoError) {
1411       /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1412        * extension, don't even try to send it the robust-access flag.
1413        * It may explode.  Instead, generate the required EGL error here.
1414        */
1415       if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0
1416             && !dri2_dpy->robustness) {
1417          _eglError(EGL_BAD_MATCH, "eglCreateContext");
1418          return false;
1419       }
1420 
1421       ctx_attribs[pos++] = __DRI_CTX_ATTRIB_FLAGS;
1422       ctx_attribs[pos++] = dri2_ctx->base.Flags |
1423          (dri2_ctx->base.NoError ? __DRI_CTX_FLAG_NO_ERROR : 0);
1424    }
1425 
1426    if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) {
1427       /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1428        * extension, don't even try to send it a reset strategy.  It may
1429        * explode.  Instead, generate the required EGL error here.
1430        */
1431       if (!dri2_dpy->robustness) {
1432          _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1433          return false;
1434       }
1435 
1436       ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
1437       ctx_attribs[pos++] = __DRI_CTX_RESET_LOSE_CONTEXT;
1438    }
1439 
1440    if (dri2_ctx->base.ContextPriority != EGL_CONTEXT_PRIORITY_MEDIUM_IMG) {
1441       unsigned val;
1442 
1443       switch (dri2_ctx->base.ContextPriority) {
1444       case EGL_CONTEXT_PRIORITY_HIGH_IMG:
1445          val = __DRI_CTX_PRIORITY_HIGH;
1446          break;
1447       case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
1448          val = __DRI_CTX_PRIORITY_MEDIUM;
1449          break;
1450       case EGL_CONTEXT_PRIORITY_LOW_IMG:
1451          val = __DRI_CTX_PRIORITY_LOW;
1452          break;
1453       default:
1454          _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1455          return false;
1456       }
1457 
1458       ctx_attribs[pos++] = __DRI_CTX_ATTRIB_PRIORITY;
1459       ctx_attribs[pos++] = val;
1460    }
1461 
1462    if (dri2_ctx->base.ReleaseBehavior == EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR) {
1463       ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
1464       ctx_attribs[pos++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE;
1465    }
1466 
1467    *num_attribs = pos;
1468 
1469    return true;
1470 }
1471 
1472 /**
1473  * Called via eglCreateContext(), drv->CreateContext().
1474  */
1475 static _EGLContext *
dri2_create_context(_EGLDisplay * disp,_EGLConfig * conf,_EGLContext * share_list,const EGLint * attrib_list)1476 dri2_create_context(_EGLDisplay *disp, _EGLConfig *conf,
1477                     _EGLContext *share_list, const EGLint *attrib_list)
1478 {
1479    struct dri2_egl_context *dri2_ctx;
1480    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1481    struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
1482    __DRIcontext *shared =
1483       dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL;
1484    struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
1485    const __DRIconfig *dri_config;
1486    int api;
1487    unsigned error;
1488    unsigned num_attribs = NUM_ATTRIBS;
1489    uint32_t ctx_attribs[NUM_ATTRIBS];
1490 
1491    dri2_ctx = malloc(sizeof *dri2_ctx);
1492    if (!dri2_ctx) {
1493       _eglError(EGL_BAD_ALLOC, "eglCreateContext");
1494       return NULL;
1495    }
1496 
1497    if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
1498       goto cleanup;
1499 
1500    /* The EGL_EXT_create_context_robustness spec says:
1501     *
1502     *    "Add to the eglCreateContext context creation errors: [...]
1503     *
1504     *     * If the reset notification behavior of <share_context> and the
1505     *       newly created context are different then an EGL_BAD_MATCH error is
1506     *       generated."
1507     */
1508    if (share_list && share_list->ResetNotificationStrategy !=
1509                      dri2_ctx->base.ResetNotificationStrategy) {
1510       _eglError(EGL_BAD_MATCH, "eglCreateContext");
1511       goto cleanup;
1512    }
1513 
1514    /* The EGL_KHR_create_context_no_error spec says:
1515     *
1516     *    "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
1517     *    used to create <share_context> does not match the value of
1518     *    EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
1519     */
1520    if (share_list && share_list->NoError != dri2_ctx->base.NoError) {
1521       _eglError(EGL_BAD_MATCH, "eglCreateContext");
1522       goto cleanup;
1523    }
1524 
1525    switch (dri2_ctx->base.ClientAPI) {
1526    case EGL_OPENGL_ES_API:
1527       switch (dri2_ctx->base.ClientMajorVersion) {
1528       case 1:
1529          api = __DRI_API_GLES;
1530          break;
1531       case 2:
1532          api = __DRI_API_GLES2;
1533          break;
1534       case 3:
1535          api = __DRI_API_GLES3;
1536          break;
1537       default:
1538          _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1539          free(dri2_ctx);
1540          return NULL;
1541       }
1542       break;
1543    case EGL_OPENGL_API:
1544       if ((dri2_ctx->base.ClientMajorVersion >= 4
1545            || (dri2_ctx->base.ClientMajorVersion == 3
1546                && dri2_ctx->base.ClientMinorVersion >= 2))
1547           && dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)
1548          api = __DRI_API_OPENGL_CORE;
1549       else if (dri2_ctx->base.ClientMajorVersion == 3 &&
1550                dri2_ctx->base.ClientMinorVersion == 1)
1551          api = __DRI_API_OPENGL_CORE;
1552       else
1553          api = __DRI_API_OPENGL;
1554       break;
1555    default:
1556       _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1557       free(dri2_ctx);
1558       return NULL;
1559    }
1560 
1561    if (conf != NULL) {
1562       /* The config chosen here isn't necessarily
1563        * used for surfaces later.
1564        * A pixmap surface will use the single config.
1565        * This opportunity depends on disabling the
1566        * doubleBufferMode check in
1567        * src/mesa/main/context.c:check_compatible()
1568        */
1569       if (dri2_config->dri_config[1][0])
1570          dri_config = dri2_config->dri_config[1][0];
1571       else
1572          dri_config = dri2_config->dri_config[0][0];
1573    }
1574    else
1575       dri_config = NULL;
1576 
1577    if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs,
1578                                   &num_attribs))
1579       goto cleanup;
1580 
1581    if (dri2_dpy->image_driver) {
1582       dri2_ctx->dri_context =
1583          dri2_dpy->image_driver->createContextAttribs(dri2_dpy->dri_screen,
1584                                                       api,
1585                                                       dri_config,
1586                                                       shared,
1587                                                       num_attribs / 2,
1588                                                       ctx_attribs,
1589                                                       & error,
1590                                                       dri2_ctx);
1591       dri2_create_context_attribs_error(error);
1592    } else if (dri2_dpy->dri2) {
1593       if (dri2_dpy->dri2->base.version >= 3) {
1594          dri2_ctx->dri_context =
1595             dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen,
1596                                                  api,
1597                                                  dri_config,
1598                                                  shared,
1599                                                  num_attribs / 2,
1600                                                  ctx_attribs,
1601                                                  & error,
1602                                                  dri2_ctx);
1603          dri2_create_context_attribs_error(error);
1604       } else {
1605          dri2_ctx->dri_context =
1606             dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
1607                                                    api,
1608                                                    dri_config,
1609                                                    shared,
1610                                                    dri2_ctx);
1611       }
1612    } else {
1613       assert(dri2_dpy->swrast);
1614       if (dri2_dpy->swrast->base.version >= 3) {
1615          dri2_ctx->dri_context =
1616             dri2_dpy->swrast->createContextAttribs(dri2_dpy->dri_screen,
1617                                                    api,
1618                                                    dri_config,
1619                                                    shared,
1620                                                    num_attribs / 2,
1621                                                    ctx_attribs,
1622                                                    & error,
1623                                                    dri2_ctx);
1624          dri2_create_context_attribs_error(error);
1625       } else {
1626          dri2_ctx->dri_context =
1627             dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,
1628                                                      api,
1629                                                      dri_config,
1630                                                      shared,
1631                                                      dri2_ctx);
1632       }
1633    }
1634 
1635    if (!dri2_ctx->dri_context)
1636       goto cleanup;
1637 
1638    return &dri2_ctx->base;
1639 
1640  cleanup:
1641    free(dri2_ctx);
1642    return NULL;
1643 }
1644 
1645 /**
1646  * Called via eglDestroyContext(), drv->DestroyContext().
1647  */
1648 static EGLBoolean
dri2_destroy_context(_EGLDisplay * disp,_EGLContext * ctx)1649 dri2_destroy_context(_EGLDisplay *disp, _EGLContext *ctx)
1650 {
1651    struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1652    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1653 
1654    if (_eglPutContext(ctx)) {
1655       dri2_dpy->core->destroyContext(dri2_ctx->dri_context);
1656       free(dri2_ctx);
1657    }
1658 
1659    return EGL_TRUE;
1660 }
1661 
1662 EGLBoolean
dri2_init_surface(_EGLSurface * surf,_EGLDisplay * disp,EGLint type,_EGLConfig * conf,const EGLint * attrib_list,EGLBoolean enable_out_fence,void * native_surface)1663 dri2_init_surface(_EGLSurface *surf, _EGLDisplay *disp, EGLint type,
1664         _EGLConfig *conf, const EGLint *attrib_list,
1665         EGLBoolean enable_out_fence, void *native_surface)
1666 {
1667    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1668    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1669 
1670    dri2_surf->out_fence_fd = -1;
1671    dri2_surf->enable_out_fence = false;
1672    if (dri2_dpy->fence && dri2_dpy->fence->base.version >= 2 &&
1673        dri2_dpy->fence->get_capabilities &&
1674        (dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen) &
1675         __DRI_FENCE_CAP_NATIVE_FD)) {
1676       dri2_surf->enable_out_fence = enable_out_fence;
1677    }
1678 
1679    return _eglInitSurface(surf, disp, type, conf, attrib_list, native_surface);
1680 }
1681 
1682 static void
dri2_surface_set_out_fence_fd(_EGLSurface * surf,int fence_fd)1683 dri2_surface_set_out_fence_fd( _EGLSurface *surf, int fence_fd)
1684 {
1685    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1686 
1687    if (dri2_surf->out_fence_fd >= 0)
1688       close(dri2_surf->out_fence_fd);
1689 
1690    dri2_surf->out_fence_fd = fence_fd;
1691 }
1692 
1693 void
dri2_fini_surface(_EGLSurface * surf)1694 dri2_fini_surface(_EGLSurface *surf)
1695 {
1696    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1697 
1698    dri2_surface_set_out_fence_fd(surf, -1);
1699    dri2_surf->enable_out_fence = false;
1700 }
1701 
1702 static EGLBoolean
dri2_destroy_surface(_EGLDisplay * disp,_EGLSurface * surf)1703 dri2_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
1704 {
1705    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1706 
1707    if (!_eglPutSurface(surf))
1708       return EGL_TRUE;
1709 
1710    return dri2_dpy->vtbl->destroy_surface(disp, surf);
1711 }
1712 
1713 static void
dri2_surf_update_fence_fd(_EGLContext * ctx,_EGLDisplay * disp,_EGLSurface * surf)1714 dri2_surf_update_fence_fd(_EGLContext *ctx,
1715                           _EGLDisplay *disp, _EGLSurface *surf)
1716 {
1717    __DRIcontext *dri_ctx = dri2_egl_context(ctx)->dri_context;
1718    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1719    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1720    int fence_fd = -1;
1721    void *fence;
1722 
1723    if (!dri2_surf->enable_out_fence)
1724       return;
1725 
1726    fence = dri2_dpy->fence->create_fence_fd(dri_ctx, -1);
1727    if (fence) {
1728       fence_fd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
1729                                                fence);
1730       dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, fence);
1731    }
1732    dri2_surface_set_out_fence_fd(surf, fence_fd);
1733 }
1734 
1735 EGLBoolean
dri2_create_drawable(struct dri2_egl_display * dri2_dpy,const __DRIconfig * config,struct dri2_egl_surface * dri2_surf,void * loaderPrivate)1736 dri2_create_drawable(struct dri2_egl_display *dri2_dpy,
1737                      const __DRIconfig *config,
1738                      struct dri2_egl_surface *dri2_surf,
1739                      void *loaderPrivate)
1740 {
1741    __DRIcreateNewDrawableFunc createNewDrawable;
1742 
1743    if (dri2_dpy->image_driver)
1744       createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
1745    else if (dri2_dpy->dri2)
1746       createNewDrawable = dri2_dpy->dri2->createNewDrawable;
1747    else if (dri2_dpy->swrast)
1748       createNewDrawable = dri2_dpy->swrast->createNewDrawable;
1749    else
1750       return _eglError(EGL_BAD_ALLOC, "no createNewDrawable");
1751 
1752    dri2_surf->dri_drawable = createNewDrawable(dri2_dpy->dri_screen,
1753                                                config, loaderPrivate);
1754    if (dri2_surf->dri_drawable == NULL)
1755       return _eglError(EGL_BAD_ALLOC, "createNewDrawable");
1756 
1757    return EGL_TRUE;
1758 }
1759 
1760 /**
1761  * Called via eglMakeCurrent(), drv->MakeCurrent().
1762  */
1763 static EGLBoolean
dri2_make_current(_EGLDisplay * disp,_EGLSurface * dsurf,_EGLSurface * rsurf,_EGLContext * ctx)1764 dri2_make_current(_EGLDisplay *disp, _EGLSurface *dsurf,
1765                   _EGLSurface *rsurf, _EGLContext *ctx)
1766 {
1767    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1768    struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1769    _EGLDisplay *old_disp = NULL;
1770    struct dri2_egl_display *old_dri2_dpy = NULL;
1771    _EGLContext *old_ctx;
1772    _EGLSurface *old_dsurf, *old_rsurf;
1773    _EGLSurface *tmp_dsurf, *tmp_rsurf;
1774    __DRIdrawable *ddraw, *rdraw;
1775    __DRIcontext *cctx;
1776    EGLint egl_error = EGL_SUCCESS;
1777 
1778    if (!dri2_dpy)
1779       return _eglError(EGL_NOT_INITIALIZED, "eglMakeCurrent");
1780 
1781    /* make new bindings, set the EGL error otherwise */
1782    if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))
1783       return EGL_FALSE;
1784 
1785    if (old_ctx) {
1786       __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context;
1787       old_disp = old_ctx->Resource.Display;
1788       old_dri2_dpy = dri2_egl_display(old_disp);
1789 
1790       /* flush before context switch */
1791       dri2_gl_flush();
1792 
1793       if (old_dsurf)
1794          dri2_surf_update_fence_fd(old_ctx, disp, old_dsurf);
1795 
1796       /* Disable shared buffer mode */
1797       if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1798           old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1799          old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, false);
1800       }
1801 
1802       dri2_dpy->core->unbindContext(old_cctx);
1803    }
1804 
1805    ddraw = (dsurf) ? dri2_dpy->vtbl->get_dri_drawable(dsurf) : NULL;
1806    rdraw = (rsurf) ? dri2_dpy->vtbl->get_dri_drawable(rsurf) : NULL;
1807    cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
1808 
1809    if (cctx || ddraw || rdraw) {
1810       if (!dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1811          _EGLContext *tmp_ctx;
1812 
1813          /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1814           * setting the error to EGL_BAD_MATCH is surely better than leaving it
1815           * as EGL_SUCCESS.
1816           */
1817          egl_error = EGL_BAD_MATCH;
1818 
1819          /* undo the previous _eglBindContext */
1820          _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &tmp_dsurf, &tmp_rsurf);
1821          assert(&dri2_ctx->base == ctx &&
1822                 tmp_dsurf == dsurf &&
1823                 tmp_rsurf == rsurf);
1824 
1825          _eglPutSurface(dsurf);
1826          _eglPutSurface(rsurf);
1827          _eglPutContext(ctx);
1828 
1829          _eglPutSurface(old_dsurf);
1830          _eglPutSurface(old_rsurf);
1831          _eglPutContext(old_ctx);
1832 
1833          ddraw = (old_dsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_dsurf) : NULL;
1834          rdraw = (old_rsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_rsurf) : NULL;
1835          cctx = (old_ctx) ? dri2_egl_context(old_ctx)->dri_context : NULL;
1836 
1837          /* undo the previous dri2_dpy->core->unbindContext */
1838          if (dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1839             if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1840                 old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1841                old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, true);
1842             }
1843 
1844             return _eglError(egl_error, "eglMakeCurrent");
1845          }
1846 
1847          /* We cannot restore the same state as it was before calling
1848           * eglMakeCurrent() and the spec isn't clear about what to do. We
1849           * can prevent EGL from calling into the DRI driver with no DRI
1850           * context bound.
1851           */
1852          dsurf = rsurf = NULL;
1853          ctx = NULL;
1854 
1855          _eglBindContext(ctx, dsurf, rsurf, &tmp_ctx, &tmp_dsurf, &tmp_rsurf);
1856          assert(tmp_ctx == old_ctx && tmp_dsurf == old_dsurf &&
1857                 tmp_rsurf == old_rsurf);
1858 
1859          _eglLog(_EGL_WARNING, "DRI2: failed to rebind the previous context");
1860       } else {
1861          /* dri2_dpy->core->bindContext succeeded, so take a reference on the
1862           * dri2_dpy. This prevents dri2_dpy from being reinitialized when a
1863           * EGLDisplay is terminated and then initialized again while a
1864           * context is still bound. See dri2_intitialize() for a more in depth
1865           * explanation. */
1866          dri2_dpy->ref_count++;
1867       }
1868    }
1869 
1870    dri2_destroy_surface(disp, old_dsurf);
1871    dri2_destroy_surface(disp, old_rsurf);
1872 
1873    if (old_ctx) {
1874       dri2_destroy_context(disp, old_ctx);
1875       dri2_display_release(old_disp);
1876    }
1877 
1878    if (egl_error != EGL_SUCCESS)
1879       return _eglError(egl_error, "eglMakeCurrent");
1880 
1881    if (dsurf && _eglSurfaceHasMutableRenderBuffer(dsurf) &&
1882        dri2_dpy->vtbl->set_shared_buffer_mode) {
1883       /* Always update the shared buffer mode. This is obviously needed when
1884        * the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When
1885        * EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the
1886        * case where external non-EGL API may have changed window's shared
1887        * buffer mode since we last saw it.
1888        */
1889       bool mode = (dsurf->ActiveRenderBuffer == EGL_SINGLE_BUFFER);
1890       dri2_dpy->vtbl->set_shared_buffer_mode(disp, dsurf, mode);
1891    }
1892 
1893    return EGL_TRUE;
1894 }
1895 
1896 __DRIdrawable *
dri2_surface_get_dri_drawable(_EGLSurface * surf)1897 dri2_surface_get_dri_drawable(_EGLSurface *surf)
1898 {
1899    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1900 
1901    return dri2_surf->dri_drawable;
1902 }
1903 
1904 /*
1905  * Called from eglGetProcAddress() via drv->GetProcAddress().
1906  */
1907 static _EGLProc
dri2_get_proc_address(const char * procname)1908 dri2_get_proc_address(const char *procname)
1909 {
1910    return _glapi_get_proc_address(procname);
1911 }
1912 
1913 static _EGLSurface*
dri2_create_window_surface(_EGLDisplay * disp,_EGLConfig * conf,void * native_window,const EGLint * attrib_list)1914 dri2_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
1915                            void *native_window, const EGLint *attrib_list)
1916 {
1917    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1918    return dri2_dpy->vtbl->create_window_surface(disp, conf, native_window,
1919                                                 attrib_list);
1920 }
1921 
1922 static _EGLSurface*
dri2_create_pixmap_surface(_EGLDisplay * disp,_EGLConfig * conf,void * native_pixmap,const EGLint * attrib_list)1923 dri2_create_pixmap_surface(_EGLDisplay *disp, _EGLConfig *conf,
1924                            void *native_pixmap, const EGLint *attrib_list)
1925 {
1926    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1927    if (!dri2_dpy->vtbl->create_pixmap_surface)
1928       return NULL;
1929    return dri2_dpy->vtbl->create_pixmap_surface(disp, conf, native_pixmap,
1930                                                 attrib_list);
1931 }
1932 
1933 static _EGLSurface*
dri2_create_pbuffer_surface(_EGLDisplay * disp,_EGLConfig * conf,const EGLint * attrib_list)1934 dri2_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
1935                             const EGLint *attrib_list)
1936 {
1937    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1938    if (!dri2_dpy->vtbl->create_pbuffer_surface)
1939       return NULL;
1940    return dri2_dpy->vtbl->create_pbuffer_surface(disp, conf, attrib_list);
1941 }
1942 
1943 static EGLBoolean
dri2_swap_interval(_EGLDisplay * disp,_EGLSurface * surf,EGLint interval)1944 dri2_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
1945 {
1946    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1947    if (!dri2_dpy->vtbl->swap_interval)
1948       return EGL_TRUE;
1949    return dri2_dpy->vtbl->swap_interval(disp, surf, interval);
1950 }
1951 
1952 /**
1953  * Asks the client API to flush any rendering to the drawable so that we can
1954  * do our swapbuffers.
1955  */
1956 void
dri2_flush_drawable_for_swapbuffers(_EGLDisplay * disp,_EGLSurface * draw)1957 dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw)
1958 {
1959    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1960    __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(draw);
1961 
1962    if (dri2_dpy->flush) {
1963       if (dri2_dpy->flush->base.version >= 4) {
1964          /* We know there's a current context because:
1965           *
1966           *     "If surface is not bound to the calling thread’s current
1967           *      context, an EGL_BAD_SURFACE error is generated."
1968          */
1969          _EGLContext *ctx = _eglGetCurrentContext();
1970          struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1971 
1972          /* From the EGL 1.4 spec (page 52):
1973           *
1974           *     "The contents of ancillary buffers are always undefined
1975           *      after calling eglSwapBuffers."
1976           */
1977          dri2_dpy->flush->flush_with_flags(dri2_ctx->dri_context,
1978                                            dri_drawable,
1979                                            __DRI2_FLUSH_DRAWABLE |
1980                                            __DRI2_FLUSH_INVALIDATE_ANCILLARY,
1981                                            __DRI2_THROTTLE_SWAPBUFFER);
1982       } else {
1983          dri2_dpy->flush->flush(dri_drawable);
1984       }
1985    }
1986 }
1987 
1988 static EGLBoolean
dri2_swap_buffers(_EGLDisplay * disp,_EGLSurface * surf)1989 dri2_swap_buffers(_EGLDisplay *disp, _EGLSurface *surf)
1990 {
1991    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1992    __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
1993    _EGLContext *ctx = _eglGetCurrentContext();
1994    EGLBoolean ret;
1995 
1996    if (ctx && surf)
1997       dri2_surf_update_fence_fd(ctx, disp, surf);
1998    ret = dri2_dpy->vtbl->swap_buffers(disp, surf);
1999 
2000    /* SwapBuffers marks the end of the frame; reset the damage region for
2001     * use again next time.
2002     */
2003    if (ret && dri2_dpy->buffer_damage &&
2004        dri2_dpy->buffer_damage->set_damage_region)
2005       dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2006 
2007    return ret;
2008 }
2009 
2010 static EGLBoolean
dri2_swap_buffers_with_damage(_EGLDisplay * disp,_EGLSurface * surf,const EGLint * rects,EGLint n_rects)2011 dri2_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *surf,
2012                               const EGLint *rects, EGLint n_rects)
2013 {
2014    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2015    __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2016    _EGLContext *ctx = _eglGetCurrentContext();
2017    EGLBoolean ret;
2018 
2019    if (ctx && surf)
2020       dri2_surf_update_fence_fd(ctx, disp, surf);
2021    if (dri2_dpy->vtbl->swap_buffers_with_damage)
2022       ret = dri2_dpy->vtbl->swap_buffers_with_damage(disp, surf,
2023                                                      rects, n_rects);
2024    else
2025       ret = dri2_dpy->vtbl->swap_buffers(disp, surf);
2026 
2027    /* SwapBuffers marks the end of the frame; reset the damage region for
2028     * use again next time.
2029     */
2030    if (ret && dri2_dpy->buffer_damage &&
2031        dri2_dpy->buffer_damage->set_damage_region)
2032       dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2033 
2034    return ret;
2035 }
2036 
2037 static EGLBoolean
dri2_swap_buffers_region(_EGLDisplay * disp,_EGLSurface * surf,EGLint numRects,const EGLint * rects)2038 dri2_swap_buffers_region(_EGLDisplay *disp, _EGLSurface *surf,
2039                          EGLint numRects, const EGLint *rects)
2040 {
2041    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2042    __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2043    EGLBoolean ret;
2044 
2045    if (!dri2_dpy->vtbl->swap_buffers_region)
2046       return EGL_FALSE;
2047    ret = dri2_dpy->vtbl->swap_buffers_region(disp, surf, numRects, rects);
2048 
2049    /* SwapBuffers marks the end of the frame; reset the damage region for
2050     * use again next time.
2051     */
2052    if (ret && dri2_dpy->buffer_damage &&
2053        dri2_dpy->buffer_damage->set_damage_region)
2054       dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2055 
2056    return ret;
2057 }
2058 
2059 static EGLBoolean
dri2_set_damage_region(_EGLDisplay * disp,_EGLSurface * surf,EGLint * rects,EGLint n_rects)2060 dri2_set_damage_region(_EGLDisplay *disp, _EGLSurface *surf,
2061                        EGLint *rects, EGLint n_rects)
2062 {
2063    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2064    __DRIdrawable *drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2065 
2066    if (!dri2_dpy->buffer_damage || !dri2_dpy->buffer_damage->set_damage_region)
2067       return EGL_FALSE;
2068 
2069    dri2_dpy->buffer_damage->set_damage_region(drawable, n_rects, rects);
2070    return EGL_TRUE;
2071 }
2072 
2073 static EGLBoolean
dri2_post_sub_buffer(_EGLDisplay * disp,_EGLSurface * surf,EGLint x,EGLint y,EGLint width,EGLint height)2074 dri2_post_sub_buffer(_EGLDisplay *disp, _EGLSurface *surf,
2075                      EGLint x, EGLint y, EGLint width, EGLint height)
2076 {
2077    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2078    if (!dri2_dpy->vtbl->post_sub_buffer)
2079       return EGL_FALSE;
2080    return dri2_dpy->vtbl->post_sub_buffer(disp, surf, x, y, width, height);
2081 }
2082 
2083 static EGLBoolean
dri2_copy_buffers(_EGLDisplay * disp,_EGLSurface * surf,void * native_pixmap_target)2084 dri2_copy_buffers(_EGLDisplay *disp, _EGLSurface *surf, void *native_pixmap_target)
2085 {
2086    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2087    if (!dri2_dpy->vtbl->copy_buffers)
2088       return _eglError(EGL_BAD_NATIVE_PIXMAP, "no support for native pixmaps");
2089    return dri2_dpy->vtbl->copy_buffers(disp, surf, native_pixmap_target);
2090 }
2091 
2092 static EGLint
dri2_query_buffer_age(_EGLDisplay * disp,_EGLSurface * surf)2093 dri2_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surf)
2094 {
2095    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2096    if (!dri2_dpy->vtbl->query_buffer_age)
2097       return 0;
2098    return dri2_dpy->vtbl->query_buffer_age(disp, surf);
2099 }
2100 
2101 static EGLBoolean
dri2_wait_client(_EGLDisplay * disp,_EGLContext * ctx)2102 dri2_wait_client(_EGLDisplay *disp, _EGLContext *ctx)
2103 {
2104    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2105    _EGLSurface *surf = ctx->DrawSurface;
2106    __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2107 
2108    /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
2109     * we need to copy fake to real here.*/
2110 
2111    if (dri2_dpy->flush != NULL)
2112       dri2_dpy->flush->flush(dri_drawable);
2113 
2114    return EGL_TRUE;
2115 }
2116 
2117 static EGLBoolean
dri2_wait_native(EGLint engine)2118 dri2_wait_native(EGLint engine)
2119 {
2120    if (engine != EGL_CORE_NATIVE_ENGINE)
2121       return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
2122    /* glXWaitX(); */
2123 
2124    return EGL_TRUE;
2125 }
2126 
2127 static EGLBoolean
dri2_bind_tex_image(_EGLDisplay * disp,_EGLSurface * surf,EGLint buffer)2128 dri2_bind_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
2129 {
2130    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2131    struct dri2_egl_context *dri2_ctx;
2132    _EGLContext *ctx;
2133    GLint format, target;
2134    __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2135 
2136    ctx = _eglGetCurrentContext();
2137    dri2_ctx = dri2_egl_context(ctx);
2138 
2139    if (!_eglBindTexImage(disp, surf, buffer))
2140       return EGL_FALSE;
2141 
2142    switch (surf->TextureFormat) {
2143    case EGL_TEXTURE_RGB:
2144       format = __DRI_TEXTURE_FORMAT_RGB;
2145       break;
2146    case EGL_TEXTURE_RGBA:
2147       format = __DRI_TEXTURE_FORMAT_RGBA;
2148       break;
2149    default:
2150       assert(!"Unexpected texture format in dri2_bind_tex_image()");
2151       format = __DRI_TEXTURE_FORMAT_RGBA;
2152    }
2153 
2154    switch (surf->TextureTarget) {
2155    case EGL_TEXTURE_2D:
2156       target = GL_TEXTURE_2D;
2157       break;
2158    default:
2159       target = GL_TEXTURE_2D;
2160       assert(!"Unexpected texture target in dri2_bind_tex_image()");
2161    }
2162 
2163    dri2_dpy->tex_buffer->setTexBuffer2(dri2_ctx->dri_context,
2164                                        target, format,
2165                                        dri_drawable);
2166 
2167    return EGL_TRUE;
2168 }
2169 
2170 static EGLBoolean
dri2_release_tex_image(_EGLDisplay * disp,_EGLSurface * surf,EGLint buffer)2171 dri2_release_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
2172 {
2173    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2174    struct dri2_egl_context *dri2_ctx;
2175    _EGLContext *ctx;
2176    GLint  target;
2177    __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2178 
2179    ctx = _eglGetCurrentContext();
2180    dri2_ctx = dri2_egl_context(ctx);
2181 
2182    if (!_eglReleaseTexImage(disp, surf, buffer))
2183       return EGL_FALSE;
2184 
2185    switch (surf->TextureTarget) {
2186    case EGL_TEXTURE_2D:
2187       target = GL_TEXTURE_2D;
2188       break;
2189    default:
2190       assert(!"missing texture target");
2191    }
2192 
2193    if (dri2_dpy->tex_buffer->base.version >= 3 &&
2194        dri2_dpy->tex_buffer->releaseTexBuffer != NULL) {
2195       dri2_dpy->tex_buffer->releaseTexBuffer(dri2_ctx->dri_context,
2196                                              target, dri_drawable);
2197    }
2198 
2199    return EGL_TRUE;
2200 }
2201 
2202 static _EGLImage*
dri2_create_image(_EGLDisplay * disp,_EGLContext * ctx,EGLenum target,EGLClientBuffer buffer,const EGLint * attr_list)2203 dri2_create_image(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
2204                   EGLClientBuffer buffer, const EGLint *attr_list)
2205 {
2206    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2207    return dri2_dpy->vtbl->create_image(disp, ctx, target, buffer,
2208                                        attr_list);
2209 }
2210 
2211 _EGLImage *
dri2_create_image_from_dri(_EGLDisplay * disp,__DRIimage * dri_image)2212 dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image)
2213 {
2214    struct dri2_egl_image *dri2_img;
2215 
2216    if (dri_image == NULL) {
2217       _eglError(EGL_BAD_ALLOC, "dri2_create_image");
2218       return NULL;
2219    }
2220 
2221    dri2_img = malloc(sizeof *dri2_img);
2222    if (!dri2_img) {
2223       _eglError(EGL_BAD_ALLOC, "dri2_create_image");
2224       return NULL;
2225    }
2226 
2227    _eglInitImage(&dri2_img->base, disp);
2228 
2229    dri2_img->dri_image = dri_image;
2230 
2231    return &dri2_img->base;
2232 }
2233 
2234 /**
2235  * Translate a DRI Image extension error code into an EGL error code.
2236  */
2237 static EGLint
egl_error_from_dri_image_error(int dri_error)2238 egl_error_from_dri_image_error(int dri_error)
2239 {
2240    switch (dri_error) {
2241    case __DRI_IMAGE_ERROR_SUCCESS:
2242       return EGL_SUCCESS;
2243    case __DRI_IMAGE_ERROR_BAD_ALLOC:
2244       return EGL_BAD_ALLOC;
2245    case __DRI_IMAGE_ERROR_BAD_MATCH:
2246       return EGL_BAD_MATCH;
2247    case __DRI_IMAGE_ERROR_BAD_PARAMETER:
2248       return EGL_BAD_PARAMETER;
2249    case __DRI_IMAGE_ERROR_BAD_ACCESS:
2250       return EGL_BAD_ACCESS;
2251    default:
2252       assert(!"unknown dri_error code");
2253       return EGL_BAD_ALLOC;
2254    }
2255 }
2256 
2257 static _EGLImage *
dri2_create_image_khr_renderbuffer(_EGLDisplay * disp,_EGLContext * ctx,EGLClientBuffer buffer,const EGLint * attr_list)2258 dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
2259                                    EGLClientBuffer buffer,
2260                                    const EGLint *attr_list)
2261 {
2262    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2263    struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2264    GLuint renderbuffer = (GLuint) (uintptr_t) buffer;
2265    __DRIimage *dri_image;
2266 
2267    if (renderbuffer == 0) {
2268       _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2269       return EGL_NO_IMAGE_KHR;
2270    }
2271 
2272    if (!disp->Extensions.KHR_gl_renderbuffer_image) {
2273       _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2274       return EGL_NO_IMAGE_KHR;
2275    }
2276 
2277    if (dri2_dpy->image->base.version >= 17 &&
2278        dri2_dpy->image->createImageFromRenderbuffer2) {
2279       unsigned error = ~0;
2280 
2281       dri_image = dri2_dpy->image->createImageFromRenderbuffer2(
2282                dri2_ctx->dri_context, renderbuffer, NULL, &error);
2283 
2284       assert(!!dri_image == (error == __DRI_IMAGE_ERROR_SUCCESS));
2285 
2286       if (!dri_image) {
2287          _eglError(egl_error_from_dri_image_error(error), "dri2_create_image_khr");
2288          return EGL_NO_IMAGE_KHR;
2289       }
2290    } else {
2291       dri_image = dri2_dpy->image->createImageFromRenderbuffer(
2292                dri2_ctx->dri_context, renderbuffer, NULL);
2293       if (!dri_image) {
2294          _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2295          return EGL_NO_IMAGE_KHR;
2296       }
2297    }
2298 
2299    return dri2_create_image_from_dri(disp, dri_image);
2300 }
2301 
2302 #ifdef HAVE_WAYLAND_PLATFORM
2303 
2304 /* This structure describes how a wl_buffer maps to one or more
2305  * __DRIimages.  A wl_drm_buffer stores the wl_drm format code and the
2306  * offsets and strides of the planes in the buffer.  This table maps a
2307  * wl_drm format code to a description of the planes in the buffer
2308  * that lets us create a __DRIimage for each of the planes. */
2309 
2310 static const struct wl_drm_components_descriptor {
2311    uint32_t dri_components;
2312    EGLint components;
2313    int nplanes;
2314 } wl_drm_components[] = {
2315    { __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 },
2316    { __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 },
2317    { __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 },
2318    { __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 },
2319    { __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 },
2320 };
2321 
2322 static _EGLImage *
dri2_create_image_wayland_wl_buffer(_EGLDisplay * disp,_EGLContext * ctx,EGLClientBuffer _buffer,const EGLint * attr_list)2323 dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx,
2324                                     EGLClientBuffer _buffer,
2325                                     const EGLint *attr_list)
2326 {
2327    struct wl_drm_buffer *buffer;
2328    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2329    const struct wl_drm_components_descriptor *f;
2330    __DRIimage *dri_image;
2331    _EGLImageAttribs attrs;
2332    int32_t plane;
2333 
2334    buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm,
2335                                    (struct wl_resource *) _buffer);
2336    if (!buffer)
2337        return NULL;
2338 
2339    if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2340       return NULL;
2341 
2342    plane = attrs.PlaneWL;
2343    f = buffer->driver_format;
2344    if (plane < 0 || plane >= f->nplanes) {
2345       _eglError(EGL_BAD_PARAMETER,
2346                 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
2347       return NULL;
2348    }
2349 
2350    dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL);
2351    if (dri_image == NULL && plane == 0)
2352       dri_image = dri2_dpy->image->dupImage(buffer->driver_buffer, NULL);
2353    if (dri_image == NULL) {
2354       _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
2355       return NULL;
2356    }
2357 
2358    return dri2_create_image_from_dri(disp, dri_image);
2359 }
2360 #endif
2361 
2362 static EGLBoolean
dri2_get_sync_values_chromium(_EGLDisplay * disp,_EGLSurface * surf,EGLuint64KHR * ust,EGLuint64KHR * msc,EGLuint64KHR * sbc)2363 dri2_get_sync_values_chromium(_EGLDisplay *disp, _EGLSurface *surf,
2364                               EGLuint64KHR *ust, EGLuint64KHR *msc,
2365                               EGLuint64KHR *sbc)
2366 {
2367    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2368    if (!dri2_dpy->vtbl->get_sync_values)
2369       return EGL_FALSE;
2370    return dri2_dpy->vtbl->get_sync_values(disp, surf, ust, msc, sbc);
2371 }
2372 
2373 /**
2374  * Set the error code after a call to
2375  * dri2_egl_image::dri_image::createImageFromTexture.
2376  */
2377 static void
dri2_create_image_khr_texture_error(int dri_error)2378 dri2_create_image_khr_texture_error(int dri_error)
2379 {
2380    EGLint egl_error = egl_error_from_dri_image_error(dri_error);
2381 
2382    if (egl_error != EGL_SUCCESS)
2383       _eglError(egl_error, "dri2_create_image_khr_texture");
2384 }
2385 
2386 static _EGLImage *
dri2_create_image_khr_texture(_EGLDisplay * disp,_EGLContext * ctx,EGLenum target,EGLClientBuffer buffer,const EGLint * attr_list)2387 dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx,
2388                                    EGLenum target,
2389                                    EGLClientBuffer buffer,
2390                                    const EGLint *attr_list)
2391 {
2392    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2393    struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2394    struct dri2_egl_image *dri2_img;
2395    GLuint texture = (GLuint) (uintptr_t) buffer;
2396    _EGLImageAttribs attrs;
2397    GLuint depth;
2398    GLenum gl_target;
2399    unsigned error;
2400 
2401    if (texture == 0) {
2402       _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2403       return EGL_NO_IMAGE_KHR;
2404    }
2405 
2406    if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2407       return EGL_NO_IMAGE_KHR;
2408 
2409    switch (target) {
2410    case EGL_GL_TEXTURE_2D_KHR:
2411       if (!disp->Extensions.KHR_gl_texture_2D_image) {
2412          _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2413          return EGL_NO_IMAGE_KHR;
2414       }
2415       depth = 0;
2416       gl_target = GL_TEXTURE_2D;
2417       break;
2418    case EGL_GL_TEXTURE_3D_KHR:
2419       if (!disp->Extensions.KHR_gl_texture_3D_image) {
2420          _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2421          return EGL_NO_IMAGE_KHR;
2422       }
2423 
2424       depth = attrs.GLTextureZOffset;
2425       gl_target = GL_TEXTURE_3D;
2426       break;
2427    case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
2428    case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
2429    case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
2430    case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
2431    case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
2432    case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
2433       if (!disp->Extensions.KHR_gl_texture_cubemap_image) {
2434          _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2435          return EGL_NO_IMAGE_KHR;
2436       }
2437 
2438       depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
2439       gl_target = GL_TEXTURE_CUBE_MAP;
2440       break;
2441    default:
2442       unreachable("Unexpected target in dri2_create_image_khr_texture()");
2443       return EGL_NO_IMAGE_KHR;
2444    }
2445 
2446    dri2_img = malloc(sizeof *dri2_img);
2447    if (!dri2_img) {
2448       _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2449       return EGL_NO_IMAGE_KHR;
2450    }
2451 
2452    _eglInitImage(&dri2_img->base, disp);
2453 
2454    dri2_img->dri_image =
2455       dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context,
2456                                               gl_target,
2457                                               texture,
2458                                               depth,
2459                                               attrs.GLTextureLevel,
2460                                               &error,
2461                                               dri2_img);
2462    dri2_create_image_khr_texture_error(error);
2463 
2464    if (!dri2_img->dri_image) {
2465       free(dri2_img);
2466       return EGL_NO_IMAGE_KHR;
2467    }
2468    return &dri2_img->base;
2469 }
2470 
2471 static EGLBoolean
dri2_query_surface(_EGLDisplay * disp,_EGLSurface * surf,EGLint attribute,EGLint * value)2472 dri2_query_surface(_EGLDisplay *disp, _EGLSurface *surf,
2473                    EGLint attribute, EGLint *value)
2474 {
2475    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2476    if (!dri2_dpy->vtbl->query_surface)
2477       return _eglQuerySurface(disp, surf, attribute, value);
2478    return dri2_dpy->vtbl->query_surface(disp, surf, attribute, value);
2479 }
2480 
2481 static struct wl_buffer*
dri2_create_wayland_buffer_from_image(_EGLDisplay * disp,_EGLImage * img)2482 dri2_create_wayland_buffer_from_image(_EGLDisplay *disp, _EGLImage *img)
2483 {
2484    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2485    if (!dri2_dpy->vtbl->create_wayland_buffer_from_image)
2486       return NULL;
2487    return dri2_dpy->vtbl->create_wayland_buffer_from_image(disp, img);
2488 }
2489 
2490 #ifdef HAVE_LIBDRM
2491 static _EGLImage *
dri2_create_image_mesa_drm_buffer(_EGLDisplay * disp,_EGLContext * ctx,EGLClientBuffer buffer,const EGLint * attr_list)2492 dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
2493                                   EGLClientBuffer buffer, const EGLint *attr_list)
2494 {
2495    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2496    EGLint format, name, pitch;
2497    _EGLImageAttribs attrs;
2498    __DRIimage *dri_image;
2499 
2500    name = (EGLint) (uintptr_t) buffer;
2501 
2502    if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2503       return NULL;
2504 
2505    if (attrs.Width <= 0 || attrs.Height <= 0 ||
2506        attrs.DRMBufferStrideMESA <= 0) {
2507       _eglError(EGL_BAD_PARAMETER,
2508                 "bad width, height or stride");
2509       return NULL;
2510    }
2511 
2512    switch (attrs.DRMBufferFormatMESA) {
2513    case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2514       format = __DRI_IMAGE_FORMAT_ARGB8888;
2515       pitch = attrs.DRMBufferStrideMESA;
2516       break;
2517    default:
2518       _eglError(EGL_BAD_PARAMETER,
2519                 "dri2_create_image_khr: unsupported pixmap depth");
2520       return NULL;
2521    }
2522 
2523    dri_image =
2524       dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
2525                                            attrs.Width,
2526                                            attrs.Height,
2527                                            format,
2528                                            name,
2529                                            pitch,
2530                                            NULL);
2531 
2532    return dri2_create_image_from_dri(disp, dri_image);
2533 }
2534 
2535 static EGLBoolean
dri2_check_dma_buf_attribs(const _EGLImageAttribs * attrs)2536 dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)
2537 {
2538    /**
2539      * The spec says:
2540      *
2541      * "Required attributes and their values are as follows:
2542      *
2543      *  * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
2544      *
2545      *  * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
2546      *    by drm_fourcc.h and used as the pixel_format parameter of the
2547      *    drm_mode_fb_cmd2 ioctl."
2548      *
2549      * and
2550      *
2551      * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2552      *    incomplete, EGL_BAD_PARAMETER is generated."
2553      */
2554    if (attrs->Width <= 0 || attrs->Height <= 0 ||
2555        !attrs->DMABufFourCC.IsPresent)
2556       return _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");
2557 
2558    /**
2559     * Also:
2560     *
2561     * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
2562     *  specified for a plane's pitch or offset isn't supported by EGL,
2563     *  EGL_BAD_ACCESS is generated."
2564     */
2565    for (unsigned i = 0; i < ARRAY_SIZE(attrs->DMABufPlanePitches); ++i) {
2566       if (attrs->DMABufPlanePitches[i].IsPresent &&
2567           attrs->DMABufPlanePitches[i].Value <= 0)
2568          return _eglError(EGL_BAD_ACCESS, "invalid pitch");
2569    }
2570 
2571    /**
2572     * If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2573     * attribute values may be given.
2574     *
2575     * This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2576     * EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2577     */
2578    for (unsigned i = 0; i < DMA_BUF_MAX_PLANES; ++i) {
2579       if (attrs->DMABufPlaneModifiersLo[i].IsPresent !=
2580           attrs->DMABufPlaneModifiersHi[i].IsPresent)
2581          return _eglError(EGL_BAD_PARAMETER, "modifier attribute lo or hi missing");
2582    }
2583 
2584    /* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2585     * mandate it, we only accept the same modifier across all planes. */
2586    for (unsigned i = 1; i < DMA_BUF_MAX_PLANES; ++i) {
2587       if (attrs->DMABufPlaneFds[i].IsPresent) {
2588          if ((attrs->DMABufPlaneModifiersLo[0].IsPresent !=
2589                attrs->DMABufPlaneModifiersLo[i].IsPresent) ||
2590              (attrs->DMABufPlaneModifiersLo[0].Value !=
2591                attrs->DMABufPlaneModifiersLo[i].Value) ||
2592              (attrs->DMABufPlaneModifiersHi[0].Value !=
2593                attrs->DMABufPlaneModifiersHi[i].Value))
2594             return _eglError(EGL_BAD_PARAMETER, "modifier attributes not equal");
2595       }
2596    }
2597 
2598    return EGL_TRUE;
2599 }
2600 
2601 /* Returns the total number of planes for the format or zero if it isn't a
2602  * valid fourcc format.
2603  */
2604 static unsigned
dri2_num_fourcc_format_planes(EGLint format)2605 dri2_num_fourcc_format_planes(EGLint format)
2606 {
2607    switch (format) {
2608    case DRM_FORMAT_R8:
2609    case DRM_FORMAT_RG88:
2610    case DRM_FORMAT_GR88:
2611    case DRM_FORMAT_R16:
2612    case DRM_FORMAT_GR1616:
2613    case DRM_FORMAT_RGB332:
2614    case DRM_FORMAT_BGR233:
2615    case DRM_FORMAT_XRGB4444:
2616    case DRM_FORMAT_XBGR4444:
2617    case DRM_FORMAT_RGBX4444:
2618    case DRM_FORMAT_BGRX4444:
2619    case DRM_FORMAT_ARGB4444:
2620    case DRM_FORMAT_ABGR4444:
2621    case DRM_FORMAT_RGBA4444:
2622    case DRM_FORMAT_BGRA4444:
2623    case DRM_FORMAT_XRGB1555:
2624    case DRM_FORMAT_XBGR1555:
2625    case DRM_FORMAT_RGBX5551:
2626    case DRM_FORMAT_BGRX5551:
2627    case DRM_FORMAT_ARGB1555:
2628    case DRM_FORMAT_ABGR1555:
2629    case DRM_FORMAT_RGBA5551:
2630    case DRM_FORMAT_BGRA5551:
2631    case DRM_FORMAT_RGB565:
2632    case DRM_FORMAT_BGR565:
2633    case DRM_FORMAT_RGB888:
2634    case DRM_FORMAT_BGR888:
2635    case DRM_FORMAT_XRGB8888:
2636    case DRM_FORMAT_XBGR8888:
2637    case DRM_FORMAT_RGBX8888:
2638    case DRM_FORMAT_BGRX8888:
2639    case DRM_FORMAT_ARGB8888:
2640    case DRM_FORMAT_ABGR8888:
2641    case DRM_FORMAT_RGBA8888:
2642    case DRM_FORMAT_BGRA8888:
2643    case DRM_FORMAT_XRGB2101010:
2644    case DRM_FORMAT_XBGR2101010:
2645    case DRM_FORMAT_RGBX1010102:
2646    case DRM_FORMAT_BGRX1010102:
2647    case DRM_FORMAT_ARGB2101010:
2648    case DRM_FORMAT_ABGR2101010:
2649    case DRM_FORMAT_RGBA1010102:
2650    case DRM_FORMAT_BGRA1010102:
2651    case DRM_FORMAT_XBGR16161616F:
2652    case DRM_FORMAT_ABGR16161616F:
2653    case DRM_FORMAT_YUYV:
2654    case DRM_FORMAT_YVYU:
2655    case DRM_FORMAT_UYVY:
2656    case DRM_FORMAT_VYUY:
2657    case DRM_FORMAT_AYUV:
2658    case DRM_FORMAT_XYUV8888:
2659       return 1;
2660 
2661    case DRM_FORMAT_NV12:
2662    case DRM_FORMAT_NV21:
2663    case DRM_FORMAT_NV16:
2664    case DRM_FORMAT_NV61:
2665    case DRM_FORMAT_P010:
2666    case DRM_FORMAT_P012:
2667    case DRM_FORMAT_P016:
2668       return 2;
2669 
2670    case DRM_FORMAT_YUV410:
2671    case DRM_FORMAT_YVU410:
2672    case DRM_FORMAT_YUV411:
2673    case DRM_FORMAT_YVU411:
2674    case DRM_FORMAT_YUV420:
2675    case DRM_FORMAT_YVU420:
2676    case DRM_FORMAT_YUV422:
2677    case DRM_FORMAT_YVU422:
2678    case DRM_FORMAT_YUV444:
2679    case DRM_FORMAT_YVU444:
2680       return 3;
2681 
2682    default:
2683       return 0;
2684    }
2685 }
2686 
2687 /* Returns the total number of file descriptors. Zero indicates an error. */
2688 static unsigned
dri2_check_dma_buf_format(const _EGLImageAttribs * attrs)2689 dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)
2690 {
2691    unsigned plane_n = dri2_num_fourcc_format_planes(attrs->DMABufFourCC.Value);
2692    if (plane_n == 0) {
2693       _eglError(EGL_BAD_MATCH, "unknown drm fourcc format");
2694       return 0;
2695    }
2696 
2697    for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; i++) {
2698       /**
2699        * The modifiers extension spec says:
2700        *
2701        * "Modifiers may modify any attribute of a buffer import, including
2702        *  but not limited to adding extra planes to a format which
2703        *  otherwise does not have those planes. As an example, a modifier
2704        *  may add a plane for an external compression buffer to a
2705        *  single-plane format. The exact meaning and effect of any
2706        *  modifier is canonically defined by drm_fourcc.h, not as part of
2707        *  this extension."
2708        */
2709       if (attrs->DMABufPlaneModifiersLo[i].IsPresent &&
2710           attrs->DMABufPlaneModifiersHi[i].IsPresent) {
2711          plane_n = i + 1;
2712       }
2713    }
2714 
2715    /**
2716      * The spec says:
2717      *
2718      * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2719      *    incomplete, EGL_BAD_PARAMETER is generated."
2720      */
2721    for (unsigned i = 0; i < plane_n; ++i) {
2722       if (!attrs->DMABufPlaneFds[i].IsPresent ||
2723           !attrs->DMABufPlaneOffsets[i].IsPresent ||
2724           !attrs->DMABufPlanePitches[i].IsPresent) {
2725          _eglError(EGL_BAD_PARAMETER, "plane attribute(s) missing");
2726          return 0;
2727       }
2728    }
2729 
2730    /**
2731     * The spec also says:
2732     *
2733     * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2734     *  attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2735     *  generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2736     *  or EGL_DMA_BUF_PLANE3_* attributes are specified."
2737     */
2738    for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; ++i) {
2739       if (attrs->DMABufPlaneFds[i].IsPresent ||
2740           attrs->DMABufPlaneOffsets[i].IsPresent ||
2741           attrs->DMABufPlanePitches[i].IsPresent) {
2742          _eglError(EGL_BAD_ATTRIBUTE, "too many plane attributes");
2743          return 0;
2744       }
2745    }
2746 
2747    return plane_n;
2748 }
2749 
2750 static EGLBoolean
dri2_query_dma_buf_formats(_EGLDisplay * disp,EGLint max,EGLint * formats,EGLint * count)2751 dri2_query_dma_buf_formats(_EGLDisplay *disp, EGLint max,
2752                            EGLint *formats, EGLint *count)
2753 {
2754    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2755    if (max < 0 || (max > 0 && formats == NULL))
2756       return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
2757 
2758    if (dri2_dpy->image->base.version < 15 ||
2759        dri2_dpy->image->queryDmaBufFormats == NULL)
2760       return EGL_FALSE;
2761 
2762    if (!dri2_dpy->image->queryDmaBufFormats(dri2_dpy->dri_screen, max,
2763                                             formats, count))
2764       return EGL_FALSE;
2765 
2766    if (max > 0) {
2767       /* Assert that all of the formats returned are actually fourcc formats.
2768        * Some day, if we want the internal interface function to be able to
2769        * return the fake fourcc formats defined in dri_interface.h, we'll have
2770        * to do something more clever here to pair the list down to just real
2771        * fourcc formats so that we don't leak the fake internal ones.
2772        */
2773       for (int i = 0; i < *count; i++) {
2774          assert(dri2_num_fourcc_format_planes(formats[i]) > 0);
2775       }
2776    }
2777 
2778    return EGL_TRUE;
2779 }
2780 
2781 static EGLBoolean
dri2_query_dma_buf_modifiers(_EGLDisplay * disp,EGLint format,EGLint max,EGLuint64KHR * modifiers,EGLBoolean * external_only,EGLint * count)2782 dri2_query_dma_buf_modifiers(_EGLDisplay *disp, EGLint format,
2783                              EGLint max, EGLuint64KHR *modifiers,
2784                              EGLBoolean *external_only, EGLint *count)
2785 {
2786    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2787 
2788    if (dri2_num_fourcc_format_planes(format) == 0)
2789       return _eglError(EGL_BAD_PARAMETER, "invalid fourcc format");
2790 
2791    if (max < 0)
2792       return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
2793 
2794    if (max > 0 && modifiers == NULL)
2795       return _eglError(EGL_BAD_PARAMETER, "invalid modifiers array");
2796 
2797    if (dri2_dpy->image->base.version < 15 ||
2798        dri2_dpy->image->queryDmaBufModifiers == NULL)
2799       return EGL_FALSE;
2800 
2801    if (dri2_dpy->image->queryDmaBufModifiers(dri2_dpy->dri_screen, format,
2802                                              max, modifiers,
2803                                              (unsigned int *) external_only,
2804                                              count) == false)
2805       return _eglError(EGL_BAD_PARAMETER, "invalid format");
2806 
2807    return EGL_TRUE;
2808 }
2809 
2810 /**
2811  * The spec says:
2812  *
2813  * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2814  *  EGL will take a reference to the dma_buf(s) which it will release at any
2815  *  time while the EGLDisplay is initialized. It is the responsibility of the
2816  *  application to close the dma_buf file descriptors."
2817  *
2818  * Therefore we must never close or otherwise modify the file descriptors.
2819  */
2820 _EGLImage *
dri2_create_image_dma_buf(_EGLDisplay * disp,_EGLContext * ctx,EGLClientBuffer buffer,const EGLint * attr_list)2821 dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
2822                           EGLClientBuffer buffer, const EGLint *attr_list)
2823 {
2824    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2825    _EGLImage *res;
2826    _EGLImageAttribs attrs;
2827    __DRIimage *dri_image;
2828    unsigned num_fds;
2829    int fds[DMA_BUF_MAX_PLANES];
2830    int pitches[DMA_BUF_MAX_PLANES];
2831    int offsets[DMA_BUF_MAX_PLANES];
2832    uint64_t modifier;
2833    bool has_modifier = false;
2834    unsigned error;
2835 
2836    /**
2837     * The spec says:
2838     *
2839     * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2840     *     error EGL_BAD_PARAMETER is generated."
2841     */
2842    if (buffer != NULL) {
2843       _eglError(EGL_BAD_PARAMETER, "buffer not NULL");
2844       return NULL;
2845    }
2846 
2847    if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2848       return NULL;
2849 
2850    if (!dri2_check_dma_buf_attribs(&attrs))
2851       return NULL;
2852 
2853    num_fds = dri2_check_dma_buf_format(&attrs);
2854    if (!num_fds)
2855       return NULL;
2856 
2857    for (unsigned i = 0; i < num_fds; ++i) {
2858       fds[i] = attrs.DMABufPlaneFds[i].Value;
2859       pitches[i] = attrs.DMABufPlanePitches[i].Value;
2860       offsets[i] = attrs.DMABufPlaneOffsets[i].Value;
2861    }
2862 
2863    /* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2864     * will be present in attrs.DMABufPlaneModifiersLo[0] and
2865     * attrs.DMABufPlaneModifiersHi[0] */
2866    if (attrs.DMABufPlaneModifiersLo[0].IsPresent) {
2867       modifier = combine_u32_into_u64(attrs.DMABufPlaneModifiersHi[0].Value,
2868                                       attrs.DMABufPlaneModifiersLo[0].Value);
2869       has_modifier = true;
2870    }
2871 
2872    if (attrs.ProtectedContent) {
2873       if (dri2_dpy->image->base.version < 18 ||
2874           dri2_dpy->image->createImageFromDmaBufs3 == NULL) {
2875          _eglError(EGL_BAD_MATCH, "unsupported protected_content attribute");
2876          return EGL_NO_IMAGE_KHR;
2877       }
2878       if (!has_modifier)
2879          modifier = DRM_FORMAT_MOD_INVALID;
2880 
2881       dri_image =
2882          dri2_dpy->image->createImageFromDmaBufs3(dri2_dpy->dri_screen,
2883             attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2884             modifier, fds, num_fds, pitches, offsets,
2885             attrs.DMABufYuvColorSpaceHint.Value,
2886             attrs.DMABufSampleRangeHint.Value,
2887             attrs.DMABufChromaHorizontalSiting.Value,
2888             attrs.DMABufChromaVerticalSiting.Value,
2889             attrs.ProtectedContent ? __DRI_IMAGE_PROTECTED_CONTENT_FLAG : 0,
2890             &error,
2891             NULL);
2892    }
2893    else if (has_modifier) {
2894       if (dri2_dpy->image->base.version < 15 ||
2895           dri2_dpy->image->createImageFromDmaBufs2 == NULL) {
2896          _eglError(EGL_BAD_MATCH, "unsupported dma_buf format modifier");
2897          return EGL_NO_IMAGE_KHR;
2898       }
2899       dri_image =
2900          dri2_dpy->image->createImageFromDmaBufs2(dri2_dpy->dri_screen,
2901             attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2902             modifier, fds, num_fds, pitches, offsets,
2903             attrs.DMABufYuvColorSpaceHint.Value,
2904             attrs.DMABufSampleRangeHint.Value,
2905             attrs.DMABufChromaHorizontalSiting.Value,
2906             attrs.DMABufChromaVerticalSiting.Value,
2907             &error,
2908             NULL);
2909    }
2910    else {
2911       dri_image =
2912          dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen,
2913             attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2914             fds, num_fds, pitches, offsets,
2915             attrs.DMABufYuvColorSpaceHint.Value,
2916             attrs.DMABufSampleRangeHint.Value,
2917             attrs.DMABufChromaHorizontalSiting.Value,
2918             attrs.DMABufChromaVerticalSiting.Value,
2919             &error,
2920             NULL);
2921    }
2922    dri2_create_image_khr_texture_error(error);
2923 
2924    if (!dri_image)
2925       return EGL_NO_IMAGE_KHR;
2926 
2927    res = dri2_create_image_from_dri(disp, dri_image);
2928 
2929    return res;
2930 }
2931 static _EGLImage *
dri2_create_drm_image_mesa(_EGLDisplay * disp,const EGLint * attr_list)2932 dri2_create_drm_image_mesa(_EGLDisplay *disp, const EGLint *attr_list)
2933 {
2934    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2935    struct dri2_egl_image *dri2_img;
2936    _EGLImageAttribs attrs;
2937    unsigned int dri_use, valid_mask;
2938    int format;
2939 
2940    if (!attr_list) {
2941       _eglError(EGL_BAD_PARAMETER, __func__);
2942       return EGL_NO_IMAGE_KHR;
2943    }
2944 
2945    if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2946       return EGL_NO_IMAGE_KHR;
2947 
2948    if (attrs.Width <= 0 || attrs.Height <= 0) {
2949       _eglError(EGL_BAD_PARAMETER, __func__);
2950       return EGL_NO_IMAGE_KHR;
2951    }
2952 
2953    switch (attrs.DRMBufferFormatMESA) {
2954    case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2955       format = __DRI_IMAGE_FORMAT_ARGB8888;
2956       break;
2957    default:
2958       _eglError(EGL_BAD_PARAMETER, __func__);
2959       return EGL_NO_IMAGE_KHR;
2960    }
2961 
2962    valid_mask =
2963       EGL_DRM_BUFFER_USE_SCANOUT_MESA |
2964       EGL_DRM_BUFFER_USE_SHARE_MESA |
2965       EGL_DRM_BUFFER_USE_CURSOR_MESA;
2966    if (attrs.DRMBufferUseMESA & ~valid_mask) {
2967       _eglError(EGL_BAD_PARAMETER, __func__);
2968       return EGL_NO_IMAGE_KHR;
2969    }
2970 
2971    dri_use = 0;
2972    if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
2973       dri_use |= __DRI_IMAGE_USE_SHARE;
2974    if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
2975       dri_use |= __DRI_IMAGE_USE_SCANOUT;
2976    if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA)
2977       dri_use |= __DRI_IMAGE_USE_CURSOR;
2978 
2979    dri2_img = malloc(sizeof *dri2_img);
2980    if (!dri2_img) {
2981       _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2982       return EGL_NO_IMAGE_KHR;
2983    }
2984 
2985    _eglInitImage(&dri2_img->base, disp);
2986 
2987    dri2_img->dri_image =
2988       dri2_dpy->image->createImage(dri2_dpy->dri_screen,
2989                                    attrs.Width, attrs.Height,
2990                                    format, dri_use, dri2_img);
2991    if (dri2_img->dri_image == NULL) {
2992       free(dri2_img);
2993        _eglError(EGL_BAD_ALLOC, "dri2_create_drm_image_mesa");
2994       return EGL_NO_IMAGE_KHR;
2995    }
2996 
2997    return &dri2_img->base;
2998 }
2999 
3000 static EGLBoolean
dri2_export_drm_image_mesa(_EGLDisplay * disp,_EGLImage * img,EGLint * name,EGLint * handle,EGLint * stride)3001 dri2_export_drm_image_mesa(_EGLDisplay *disp, _EGLImage *img,
3002                           EGLint *name, EGLint *handle, EGLint *stride)
3003 {
3004    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3005    struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3006 
3007    if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image,
3008                                             __DRI_IMAGE_ATTRIB_NAME, name))
3009       return _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa");
3010 
3011    if (handle)
3012       dri2_dpy->image->queryImage(dri2_img->dri_image,
3013                                   __DRI_IMAGE_ATTRIB_HANDLE, handle);
3014 
3015    if (stride)
3016       dri2_dpy->image->queryImage(dri2_img->dri_image,
3017                                   __DRI_IMAGE_ATTRIB_STRIDE, stride);
3018 
3019    return EGL_TRUE;
3020 }
3021 
3022 /**
3023  * Checks if we can support EGL_MESA_image_dma_buf_export on this image.
3024 
3025  * The spec provides a boolean return for the driver to reject exporting for
3026  * basically any reason, but doesn't specify any particular error cases.  For
3027  * now, we just fail if we don't have a DRM fourcc for the format.
3028  */
3029 static bool
dri2_can_export_dma_buf_image(_EGLDisplay * disp,_EGLImage * img)3030 dri2_can_export_dma_buf_image(_EGLDisplay *disp, _EGLImage *img)
3031 {
3032    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3033    struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3034    EGLint fourcc;
3035 
3036    if (!dri2_dpy->image->queryImage(dri2_img->dri_image,
3037                                     __DRI_IMAGE_ATTRIB_FOURCC, &fourcc)) {
3038       return false;
3039    }
3040 
3041    return true;
3042 }
3043 
3044 static EGLBoolean
dri2_export_dma_buf_image_query_mesa(_EGLDisplay * disp,_EGLImage * img,EGLint * fourcc,EGLint * nplanes,EGLuint64KHR * modifiers)3045 dri2_export_dma_buf_image_query_mesa(_EGLDisplay *disp, _EGLImage *img,
3046                                      EGLint *fourcc, EGLint *nplanes,
3047                                      EGLuint64KHR *modifiers)
3048 {
3049    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3050    struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3051    int num_planes;
3052 
3053    if (!dri2_can_export_dma_buf_image(disp, img))
3054       return EGL_FALSE;
3055 
3056    dri2_dpy->image->queryImage(dri2_img->dri_image,
3057                                __DRI_IMAGE_ATTRIB_NUM_PLANES, &num_planes);
3058    if (nplanes)
3059      *nplanes = num_planes;
3060 
3061    if (fourcc)
3062       dri2_dpy->image->queryImage(dri2_img->dri_image,
3063                                   __DRI_IMAGE_ATTRIB_FOURCC, fourcc);
3064 
3065    if (modifiers) {
3066       int mod_hi, mod_lo;
3067       uint64_t modifier = DRM_FORMAT_MOD_INVALID;
3068       bool query;
3069 
3070       query = dri2_dpy->image->queryImage(dri2_img->dri_image,
3071                                           __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
3072                                           &mod_hi);
3073       query &= dri2_dpy->image->queryImage(dri2_img->dri_image,
3074                                            __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
3075                                            &mod_lo);
3076       if (query)
3077          modifier = combine_u32_into_u64 (mod_hi, mod_lo);
3078 
3079       for (int i = 0; i < num_planes; i++)
3080         modifiers[i] = modifier;
3081    }
3082 
3083    return EGL_TRUE;
3084 }
3085 
3086 static EGLBoolean
dri2_export_dma_buf_image_mesa(_EGLDisplay * disp,_EGLImage * img,int * fds,EGLint * strides,EGLint * offsets)3087 dri2_export_dma_buf_image_mesa(_EGLDisplay *disp, _EGLImage *img,
3088                                int *fds, EGLint *strides, EGLint *offsets)
3089 {
3090    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3091    struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3092    EGLint nplanes;
3093 
3094    if (!dri2_can_export_dma_buf_image(disp, img))
3095       return EGL_FALSE;
3096 
3097    /* EGL_MESA_image_dma_buf_export spec says:
3098     *    "If the number of fds is less than the number of planes, then
3099     *    subsequent fd slots should contain -1."
3100     */
3101    if (fds) {
3102       /* Query nplanes so that we know how big the given array is. */
3103       dri2_dpy->image->queryImage(dri2_img->dri_image,
3104                                   __DRI_IMAGE_ATTRIB_NUM_PLANES, &nplanes);
3105       memset(fds, -1, nplanes * sizeof(int));
3106    }
3107 
3108    /* rework later to provide multiple fds/strides/offsets */
3109    if (fds)
3110       dri2_dpy->image->queryImage(dri2_img->dri_image,
3111                                   __DRI_IMAGE_ATTRIB_FD, fds);
3112 
3113    if (strides)
3114       dri2_dpy->image->queryImage(dri2_img->dri_image,
3115                                   __DRI_IMAGE_ATTRIB_STRIDE, strides);
3116 
3117    if (offsets) {
3118       int img_offset;
3119       bool ret = dri2_dpy->image->queryImage(dri2_img->dri_image,
3120                      __DRI_IMAGE_ATTRIB_OFFSET, &img_offset);
3121       if (ret)
3122          offsets[0] = img_offset;
3123       else
3124          offsets[0] = 0;
3125    }
3126 
3127    return EGL_TRUE;
3128 }
3129 
3130 #endif
3131 
3132 _EGLImage *
dri2_create_image_khr(_EGLDisplay * disp,_EGLContext * ctx,EGLenum target,EGLClientBuffer buffer,const EGLint * attr_list)3133 dri2_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
3134                       EGLClientBuffer buffer, const EGLint *attr_list)
3135 {
3136    switch (target) {
3137    case EGL_GL_TEXTURE_2D_KHR:
3138    case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
3139    case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
3140    case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
3141    case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
3142    case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
3143    case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
3144    case EGL_GL_TEXTURE_3D_KHR:
3145       return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list);
3146    case EGL_GL_RENDERBUFFER_KHR:
3147       return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
3148 #ifdef HAVE_LIBDRM
3149    case EGL_DRM_BUFFER_MESA:
3150       return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list);
3151    case EGL_LINUX_DMA_BUF_EXT:
3152       return dri2_create_image_dma_buf(disp, ctx, buffer, attr_list);
3153 #endif
3154 #ifdef HAVE_WAYLAND_PLATFORM
3155    case EGL_WAYLAND_BUFFER_WL:
3156       return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list);
3157 #endif
3158    default:
3159       _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
3160       return EGL_NO_IMAGE_KHR;
3161    }
3162 }
3163 
3164 static EGLBoolean
dri2_destroy_image_khr(_EGLDisplay * disp,_EGLImage * image)3165 dri2_destroy_image_khr(_EGLDisplay *disp, _EGLImage *image)
3166 {
3167    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3168    struct dri2_egl_image *dri2_img = dri2_egl_image(image);
3169 
3170    dri2_dpy->image->destroyImage(dri2_img->dri_image);
3171    free(dri2_img);
3172 
3173    return EGL_TRUE;
3174 }
3175 
3176 #ifdef HAVE_WAYLAND_PLATFORM
3177 
3178 static void
dri2_wl_reference_buffer(void * user_data,uint32_t name,int fd,struct wl_drm_buffer * buffer)3179 dri2_wl_reference_buffer(void *user_data, uint32_t name, int fd,
3180                          struct wl_drm_buffer *buffer)
3181 {
3182    _EGLDisplay *disp = user_data;
3183    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3184    __DRIimage *img;
3185    int dri_components = 0;
3186 
3187    if (fd == -1)
3188       img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen,
3189                                                   buffer->width,
3190                                                   buffer->height,
3191                                                   buffer->format,
3192                                                   (int*)&name, 1,
3193                                                   buffer->stride,
3194                                                   buffer->offset,
3195                                                   NULL);
3196    else
3197       img = dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
3198                                                 buffer->width,
3199                                                 buffer->height,
3200                                                 buffer->format,
3201                                                 &fd, 1,
3202                                                 buffer->stride,
3203                                                 buffer->offset,
3204                                                 NULL);
3205 
3206    if (img == NULL)
3207       return;
3208 
3209    dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components);
3210 
3211    buffer->driver_format = NULL;
3212    for (int i = 0; i < ARRAY_SIZE(wl_drm_components); i++)
3213       if (wl_drm_components[i].dri_components == dri_components)
3214          buffer->driver_format = &wl_drm_components[i];
3215 
3216    if (buffer->driver_format == NULL)
3217       dri2_dpy->image->destroyImage(img);
3218    else
3219       buffer->driver_buffer = img;
3220 }
3221 
3222 static void
dri2_wl_release_buffer(void * user_data,struct wl_drm_buffer * buffer)3223 dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer)
3224 {
3225    _EGLDisplay *disp = user_data;
3226    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3227 
3228    dri2_dpy->image->destroyImage(buffer->driver_buffer);
3229 }
3230 
3231 static EGLBoolean
dri2_bind_wayland_display_wl(_EGLDisplay * disp,struct wl_display * wl_dpy)3232 dri2_bind_wayland_display_wl(_EGLDisplay *disp, struct wl_display *wl_dpy)
3233 {
3234    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3235    const struct wayland_drm_callbacks wl_drm_callbacks = {
3236       .authenticate = (int(*)(void *, uint32_t)) dri2_dpy->vtbl->authenticate,
3237       .reference_buffer = dri2_wl_reference_buffer,
3238       .release_buffer = dri2_wl_release_buffer,
3239       .is_format_supported = dri2_wl_is_format_supported
3240    };
3241    int flags = 0;
3242    uint64_t cap;
3243 
3244    if (dri2_dpy->wl_server_drm)
3245            return EGL_FALSE;
3246 
3247    if (drmGetCap(dri2_dpy->fd, DRM_CAP_PRIME, &cap) == 0 &&
3248        cap == (DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT) &&
3249        dri2_dpy->image->base.version >= 7 &&
3250        dri2_dpy->image->createImageFromFds != NULL)
3251       flags |= WAYLAND_DRM_PRIME;
3252 
3253    dri2_dpy->wl_server_drm =
3254            wayland_drm_init(wl_dpy, dri2_dpy->device_name,
3255                             &wl_drm_callbacks, disp, flags);
3256 
3257    if (!dri2_dpy->wl_server_drm)
3258            return EGL_FALSE;
3259 
3260 #ifdef HAVE_DRM_PLATFORM
3261    /* We have to share the wl_drm instance with gbm, so gbm can convert
3262     * wl_buffers to gbm bos. */
3263    if (dri2_dpy->gbm_dri)
3264       dri2_dpy->gbm_dri->wl_drm = dri2_dpy->wl_server_drm;
3265 #endif
3266 
3267    return EGL_TRUE;
3268 }
3269 
3270 static EGLBoolean
dri2_unbind_wayland_display_wl(_EGLDisplay * disp,struct wl_display * wl_dpy)3271 dri2_unbind_wayland_display_wl(_EGLDisplay *disp, struct wl_display *wl_dpy)
3272 {
3273    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3274 
3275    if (!dri2_dpy->wl_server_drm)
3276            return EGL_FALSE;
3277 
3278    wayland_drm_uninit(dri2_dpy->wl_server_drm);
3279    dri2_dpy->wl_server_drm = NULL;
3280 
3281    return EGL_TRUE;
3282 }
3283 
3284 static EGLBoolean
dri2_query_wayland_buffer_wl(_EGLDisplay * disp,struct wl_resource * buffer_resource,EGLint attribute,EGLint * value)3285 dri2_query_wayland_buffer_wl(_EGLDisplay *disp, struct wl_resource *buffer_resource,
3286                              EGLint attribute, EGLint *value)
3287 {
3288    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3289    struct wl_drm_buffer *buffer;
3290    const struct wl_drm_components_descriptor *format;
3291 
3292    buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm, buffer_resource);
3293    if (!buffer)
3294       return EGL_FALSE;
3295 
3296    format = buffer->driver_format;
3297    switch (attribute) {
3298    case EGL_TEXTURE_FORMAT:
3299       *value = format->components;
3300       return EGL_TRUE;
3301    case EGL_WIDTH:
3302       *value = buffer->width;
3303       return EGL_TRUE;
3304    case EGL_HEIGHT:
3305       *value = buffer->height;
3306       return EGL_TRUE;
3307    }
3308 
3309    return EGL_FALSE;
3310 }
3311 #endif
3312 
3313 static void
dri2_egl_ref_sync(struct dri2_egl_sync * sync)3314 dri2_egl_ref_sync(struct dri2_egl_sync *sync)
3315 {
3316    p_atomic_inc(&sync->refcount);
3317 }
3318 
3319 static void
dri2_egl_unref_sync(struct dri2_egl_display * dri2_dpy,struct dri2_egl_sync * dri2_sync)3320 dri2_egl_unref_sync(struct dri2_egl_display *dri2_dpy,
3321                     struct dri2_egl_sync *dri2_sync)
3322 {
3323    if (p_atomic_dec_zero(&dri2_sync->refcount)) {
3324       switch (dri2_sync->base.Type) {
3325       case EGL_SYNC_REUSABLE_KHR:
3326          cnd_destroy(&dri2_sync->cond);
3327          break;
3328       case EGL_SYNC_NATIVE_FENCE_ANDROID:
3329          if (dri2_sync->base.SyncFd != EGL_NO_NATIVE_FENCE_FD_ANDROID)
3330             close(dri2_sync->base.SyncFd);
3331          break;
3332       default:
3333          break;
3334       }
3335 
3336       if (dri2_sync->fence)
3337          dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, dri2_sync->fence);
3338 
3339       free(dri2_sync);
3340    }
3341 }
3342 
3343 static _EGLSync *
dri2_create_sync(_EGLDisplay * disp,EGLenum type,const EGLAttrib * attrib_list)3344 dri2_create_sync(_EGLDisplay *disp, EGLenum type, const EGLAttrib *attrib_list)
3345 {
3346    _EGLContext *ctx = _eglGetCurrentContext();
3347    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3348    struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3349    struct dri2_egl_sync *dri2_sync;
3350    EGLint ret;
3351    pthread_condattr_t attr;
3352 
3353    dri2_sync = calloc(1, sizeof(struct dri2_egl_sync));
3354    if (!dri2_sync) {
3355       _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3356       return NULL;
3357    }
3358 
3359    if (!_eglInitSync(&dri2_sync->base, disp, type, attrib_list)) {
3360       free(dri2_sync);
3361       return NULL;
3362    }
3363 
3364    switch (type) {
3365    case EGL_SYNC_FENCE_KHR:
3366       dri2_sync->fence = dri2_dpy->fence->create_fence(dri2_ctx->dri_context);
3367       if (!dri2_sync->fence) {
3368          /* Why did it fail? DRI doesn't return an error code, so we emit
3369           * a generic EGL error that doesn't communicate user error.
3370           */
3371          _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3372          free(dri2_sync);
3373          return NULL;
3374       }
3375       break;
3376 
3377    case EGL_SYNC_CL_EVENT_KHR:
3378       dri2_sync->fence = dri2_dpy->fence->get_fence_from_cl_event(
3379                                  dri2_dpy->dri_screen,
3380                                  dri2_sync->base.CLEvent);
3381       /* this can only happen if the cl_event passed in is invalid. */
3382       if (!dri2_sync->fence) {
3383          _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3384          free(dri2_sync);
3385          return NULL;
3386       }
3387 
3388       /* the initial status must be "signaled" if the cl_event is signaled */
3389       if (dri2_dpy->fence->client_wait_sync(dri2_ctx->dri_context,
3390                                             dri2_sync->fence, 0, 0))
3391          dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3392       break;
3393 
3394    case EGL_SYNC_REUSABLE_KHR:
3395       /* intialize attr */
3396       ret = pthread_condattr_init(&attr);
3397 
3398       if (ret) {
3399          _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3400          free(dri2_sync);
3401          return NULL;
3402       }
3403 
3404       /* change clock attribute to CLOCK_MONOTONIC */
3405       ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
3406 
3407       if (ret) {
3408          _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3409          free(dri2_sync);
3410          return NULL;
3411       }
3412 
3413       ret = pthread_cond_init(&dri2_sync->cond, &attr);
3414 
3415       if (ret) {
3416          _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3417          free(dri2_sync);
3418          return NULL;
3419       }
3420 
3421       /* initial status of reusable sync must be "unsignaled" */
3422       dri2_sync->base.SyncStatus = EGL_UNSIGNALED_KHR;
3423       break;
3424 
3425    case EGL_SYNC_NATIVE_FENCE_ANDROID:
3426       if (dri2_dpy->fence->create_fence_fd) {
3427          dri2_sync->fence = dri2_dpy->fence->create_fence_fd(
3428                                     dri2_ctx->dri_context,
3429                                     dri2_sync->base.SyncFd);
3430       }
3431       if (!dri2_sync->fence) {
3432          _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3433          free(dri2_sync);
3434          return NULL;
3435       }
3436       break;
3437    }
3438 
3439    p_atomic_set(&dri2_sync->refcount, 1);
3440    return &dri2_sync->base;
3441 }
3442 
3443 static EGLBoolean
dri2_destroy_sync(_EGLDisplay * disp,_EGLSync * sync)3444 dri2_destroy_sync(_EGLDisplay *disp, _EGLSync *sync)
3445 {
3446    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3447    struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3448    EGLint ret = EGL_TRUE;
3449    EGLint err;
3450 
3451    /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
3452     * then unlock all threads possibly blocked by the reusable sync before
3453     * destroying it.
3454     */
3455    if (dri2_sync->base.Type == EGL_SYNC_REUSABLE_KHR &&
3456        dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3457       dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3458       /* unblock all threads currently blocked by sync */
3459       err = cnd_broadcast(&dri2_sync->cond);
3460 
3461       if (err) {
3462          _eglError(EGL_BAD_ACCESS, "eglDestroySyncKHR");
3463          ret = EGL_FALSE;
3464       }
3465    }
3466 
3467    dri2_egl_unref_sync(dri2_dpy, dri2_sync);
3468 
3469    return ret;
3470 }
3471 
3472 static EGLint
dri2_dup_native_fence_fd(_EGLDisplay * disp,_EGLSync * sync)3473 dri2_dup_native_fence_fd(_EGLDisplay *disp, _EGLSync *sync)
3474 {
3475    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3476    struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3477 
3478    assert(sync->Type == EGL_SYNC_NATIVE_FENCE_ANDROID);
3479 
3480    if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3481       /* try to retrieve the actual native fence fd.. if rendering is
3482        * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
3483        */
3484       sync->SyncFd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
3485                                                    dri2_sync->fence);
3486    }
3487 
3488    if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3489       /* if native fence fd still not created, return an error: */
3490       _eglError(EGL_BAD_PARAMETER, "eglDupNativeFenceFDANDROID");
3491       return EGL_NO_NATIVE_FENCE_FD_ANDROID;
3492    }
3493 
3494    return os_dupfd_cloexec(sync->SyncFd);
3495 }
3496 
3497 static void
dri2_set_blob_cache_funcs(_EGLDisplay * disp,EGLSetBlobFuncANDROID set,EGLGetBlobFuncANDROID get)3498 dri2_set_blob_cache_funcs(_EGLDisplay *disp,
3499                           EGLSetBlobFuncANDROID set,
3500                           EGLGetBlobFuncANDROID get)
3501 {
3502    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3503    dri2_dpy->blob->set_cache_funcs(dri2_dpy->dri_screen,
3504                                    disp->BlobCacheSet,
3505                                    disp->BlobCacheGet);
3506 }
3507 
3508 static EGLint
dri2_client_wait_sync(_EGLDisplay * disp,_EGLSync * sync,EGLint flags,EGLTime timeout)3509 dri2_client_wait_sync(_EGLDisplay *disp, _EGLSync *sync,
3510                       EGLint flags, EGLTime timeout)
3511 {
3512    _EGLContext *ctx = _eglGetCurrentContext();
3513    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3514    struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3515    struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3516    unsigned wait_flags = 0;
3517 
3518    EGLint ret = EGL_CONDITION_SATISFIED_KHR;
3519 
3520    /* The EGL_KHR_fence_sync spec states:
3521     *
3522     *    "If no context is current for the bound API,
3523     *     the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
3524     */
3525    if (dri2_ctx && flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)
3526       wait_flags |= __DRI2_FENCE_FLAG_FLUSH_COMMANDS;
3527 
3528    /* the sync object should take a reference while waiting */
3529    dri2_egl_ref_sync(dri2_sync);
3530 
3531    switch (sync->Type) {
3532    case EGL_SYNC_FENCE_KHR:
3533    case EGL_SYNC_NATIVE_FENCE_ANDROID:
3534    case EGL_SYNC_CL_EVENT_KHR:
3535       if (dri2_dpy->fence->client_wait_sync(dri2_ctx ? dri2_ctx->dri_context : NULL,
3536                                          dri2_sync->fence, wait_flags,
3537                                          timeout))
3538          dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3539       else
3540          ret = EGL_TIMEOUT_EXPIRED_KHR;
3541       break;
3542 
3543    case EGL_SYNC_REUSABLE_KHR:
3544       if (dri2_ctx && dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR &&
3545           (flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)) {
3546          /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
3547          dri2_gl_flush();
3548       }
3549 
3550       /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
3551       if (timeout == EGL_FOREVER_KHR) {
3552          mtx_lock(&dri2_sync->mutex);
3553          cnd_wait(&dri2_sync->cond, &dri2_sync->mutex);
3554          mtx_unlock(&dri2_sync->mutex);
3555       } else {
3556          /* if reusable sync has not been yet signaled */
3557          if (dri2_sync->base.SyncStatus != EGL_SIGNALED_KHR) {
3558             /* timespecs for cnd_timedwait */
3559             struct timespec current;
3560             struct timespec expire;
3561 
3562             /* We override the clock to monotonic when creating the condition
3563              * variable. */
3564             clock_gettime(CLOCK_MONOTONIC, &current);
3565 
3566             /* calculating when to expire */
3567             expire.tv_nsec = timeout % 1000000000L;
3568             expire.tv_sec = timeout / 1000000000L;
3569 
3570             expire.tv_nsec += current.tv_nsec;
3571             expire.tv_sec += current.tv_sec;
3572 
3573             /* expire.nsec now is a number between 0 and 1999999998 */
3574             if (expire.tv_nsec > 999999999L) {
3575                expire.tv_sec++;
3576                expire.tv_nsec -= 1000000000L;
3577             }
3578 
3579             mtx_lock(&dri2_sync->mutex);
3580             ret = cnd_timedwait(&dri2_sync->cond, &dri2_sync->mutex, &expire);
3581             mtx_unlock(&dri2_sync->mutex);
3582 
3583             if (ret == thrd_busy) {
3584                if (dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3585                   ret = EGL_TIMEOUT_EXPIRED_KHR;
3586                } else {
3587                   _eglError(EGL_BAD_ACCESS, "eglClientWaitSyncKHR");
3588                   ret = EGL_FALSE;
3589                }
3590             }
3591          }
3592       }
3593       break;
3594   }
3595   dri2_egl_unref_sync(dri2_dpy, dri2_sync);
3596 
3597   return ret;
3598 }
3599 
3600 static EGLBoolean
dri2_signal_sync(_EGLDisplay * disp,_EGLSync * sync,EGLenum mode)3601 dri2_signal_sync(_EGLDisplay *disp, _EGLSync *sync, EGLenum mode)
3602 {
3603    struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3604    EGLint ret;
3605 
3606    if (sync->Type != EGL_SYNC_REUSABLE_KHR)
3607       return _eglError(EGL_BAD_MATCH, "eglSignalSyncKHR");
3608 
3609    if (mode != EGL_SIGNALED_KHR && mode != EGL_UNSIGNALED_KHR)
3610       return _eglError(EGL_BAD_ATTRIBUTE, "eglSignalSyncKHR");
3611 
3612    dri2_sync->base.SyncStatus = mode;
3613 
3614    if (mode == EGL_SIGNALED_KHR) {
3615       ret = cnd_broadcast(&dri2_sync->cond);
3616 
3617       /* fail to broadcast */
3618       if (ret)
3619          return _eglError(EGL_BAD_ACCESS, "eglSignalSyncKHR");
3620    }
3621 
3622    return EGL_TRUE;
3623 }
3624 
3625 static EGLint
dri2_server_wait_sync(_EGLDisplay * disp,_EGLSync * sync)3626 dri2_server_wait_sync(_EGLDisplay *disp, _EGLSync *sync)
3627 {
3628    _EGLContext *ctx = _eglGetCurrentContext();
3629    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3630    struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3631    struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3632 
3633    dri2_dpy->fence->server_wait_sync(dri2_ctx->dri_context,
3634                                      dri2_sync->fence, 0);
3635    return EGL_TRUE;
3636 }
3637 
3638 static int
dri2_interop_query_device_info(_EGLDisplay * disp,_EGLContext * ctx,struct mesa_glinterop_device_info * out)3639 dri2_interop_query_device_info(_EGLDisplay *disp, _EGLContext *ctx,
3640                                struct mesa_glinterop_device_info *out)
3641 {
3642    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3643    struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3644 
3645    if (!dri2_dpy->interop)
3646       return MESA_GLINTEROP_UNSUPPORTED;
3647 
3648    return dri2_dpy->interop->query_device_info(dri2_ctx->dri_context, out);
3649 }
3650 
3651 static int
dri2_interop_export_object(_EGLDisplay * disp,_EGLContext * ctx,struct mesa_glinterop_export_in * in,struct mesa_glinterop_export_out * out)3652 dri2_interop_export_object(_EGLDisplay *disp, _EGLContext *ctx,
3653                            struct mesa_glinterop_export_in *in,
3654                            struct mesa_glinterop_export_out *out)
3655 {
3656    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3657    struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3658 
3659    if (!dri2_dpy->interop)
3660       return MESA_GLINTEROP_UNSUPPORTED;
3661 
3662    return dri2_dpy->interop->export_object(dri2_ctx->dri_context, in, out);
3663 }
3664 
3665 const _EGLDriver _eglDriver = {
3666    .Initialize = dri2_initialize,
3667    .Terminate = dri2_terminate,
3668    .CreateContext = dri2_create_context,
3669    .DestroyContext = dri2_destroy_context,
3670    .MakeCurrent = dri2_make_current,
3671    .CreateWindowSurface = dri2_create_window_surface,
3672    .CreatePixmapSurface = dri2_create_pixmap_surface,
3673    .CreatePbufferSurface = dri2_create_pbuffer_surface,
3674    .DestroySurface = dri2_destroy_surface,
3675    .GetProcAddress = dri2_get_proc_address,
3676    .WaitClient = dri2_wait_client,
3677    .WaitNative = dri2_wait_native,
3678    .BindTexImage = dri2_bind_tex_image,
3679    .ReleaseTexImage = dri2_release_tex_image,
3680    .SwapInterval = dri2_swap_interval,
3681    .SwapBuffers = dri2_swap_buffers,
3682    .SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage,
3683    .SwapBuffersRegionNOK = dri2_swap_buffers_region,
3684    .SetDamageRegion = dri2_set_damage_region,
3685    .PostSubBufferNV = dri2_post_sub_buffer,
3686    .CopyBuffers = dri2_copy_buffers,
3687    .QueryBufferAge = dri2_query_buffer_age,
3688    .CreateImageKHR = dri2_create_image,
3689    .DestroyImageKHR = dri2_destroy_image_khr,
3690    .CreateWaylandBufferFromImageWL = dri2_create_wayland_buffer_from_image,
3691    .QuerySurface = dri2_query_surface,
3692    .QueryDriverName = dri2_query_driver_name,
3693    .QueryDriverConfig = dri2_query_driver_config,
3694 #ifdef HAVE_LIBDRM
3695    .CreateDRMImageMESA = dri2_create_drm_image_mesa,
3696    .ExportDRMImageMESA = dri2_export_drm_image_mesa,
3697    .ExportDMABUFImageQueryMESA = dri2_export_dma_buf_image_query_mesa,
3698    .ExportDMABUFImageMESA = dri2_export_dma_buf_image_mesa,
3699    .QueryDmaBufFormatsEXT = dri2_query_dma_buf_formats,
3700    .QueryDmaBufModifiersEXT = dri2_query_dma_buf_modifiers,
3701 #endif
3702 #ifdef HAVE_WAYLAND_PLATFORM
3703    .BindWaylandDisplayWL = dri2_bind_wayland_display_wl,
3704    .UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl,
3705    .QueryWaylandBufferWL = dri2_query_wayland_buffer_wl,
3706 #endif
3707    .GetSyncValuesCHROMIUM = dri2_get_sync_values_chromium,
3708    .CreateSyncKHR = dri2_create_sync,
3709    .ClientWaitSyncKHR = dri2_client_wait_sync,
3710    .SignalSyncKHR = dri2_signal_sync,
3711    .WaitSyncKHR = dri2_server_wait_sync,
3712    .DestroySyncKHR = dri2_destroy_sync,
3713    .GLInteropQueryDeviceInfo = dri2_interop_query_device_info,
3714    .GLInteropExportObject = dri2_interop_export_object,
3715    .DupNativeFenceFDANDROID = dri2_dup_native_fence_fd,
3716    .SetBlobCacheFuncsANDROID = dri2_set_blob_cache_funcs,
3717 };
3718