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