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