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