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