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