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