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