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