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