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