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