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