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