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