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