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