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