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