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