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