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