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