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