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