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