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