2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 * Kristian Høgsberg <krh@bitplanet.net>
39 #include <c11/threads.h>
43 #include "drm-uapi/drm_fourcc.h"
46 #include <GL/internal/dri_interface.h>
47 #include <sys/types.h>
50 #ifdef HAVE_WAYLAND_PLATFORM
51 #include <wayland-client.h>
52 #include "wayland-drm.h"
53 #include "wayland-drm-client-protocol.h"
54 #include "linux-dmabuf-unstable-v1-client-protocol.h"
57 #ifdef HAVE_X11_PLATFORM
58 #include "X11/Xlibint.h"
61 #include "egldefines.h"
63 #include "GL/mesa_glinterop.h"
64 #include "loader/loader.h"
65 #include "util/os_file.h"
66 #include "util/u_atomic.h"
67 #include "util/u_vector.h"
68 #include "mapi/glapi/glapi.h"
69 #include "util/bitscan.h"
70 #include "util/u_math.h"
72 /* Additional definitions not yet in the drm_fourcc.h.
74 #ifndef DRM_FORMAT_P010
75 #define DRM_FORMAT_P010 fourcc_code('P', '0', '1', '0') /* 2x2 subsampled Cb:Cr plane 10 bits per channel */
78 #ifndef DRM_FORMAT_P012
79 #define DRM_FORMAT_P012 fourcc_code('P', '0', '1', '2') /* 2x2 subsampled Cb:Cr plane 12 bits per channel */
82 #ifndef DRM_FORMAT_P016
83 #define DRM_FORMAT_P016 fourcc_code('P', '0', '1', '6') /* 2x2 subsampled Cb:Cr plane 16 bits per channel */
86 #define NUM_ATTRIBS 12
88 static const struct dri2_pbuffer_visual {
89 const char *format_name;
90 unsigned int dri_image_format;
92 unsigned int rgba_sizes[4];
93 } dri2_pbuffer_visuals[] = {
96 __DRI_IMAGE_FORMAT_ABGR16161616F,
102 __DRI_IMAGE_FORMAT_XBGR16161616F,
108 __DRI_IMAGE_FORMAT_ARGB2101010,
114 __DRI_IMAGE_FORMAT_XRGB2101010,
120 __DRI_IMAGE_FORMAT_ARGB8888,
126 __DRI_IMAGE_FORMAT_XRGB8888,
132 __DRI_IMAGE_FORMAT_RGB565,
139 dri_set_background_context(void *loaderPrivate)
141 _EGLContext *ctx = _eglGetCurrentContext();
142 _EGLThreadInfo *t = _eglGetCurrentThread();
144 _eglBindContextToThread(ctx, t);
150 static void (*glFlush)(void);
151 static mtx_t glFlushMutex = _MTX_INITIALIZER_NP;
153 mtx_lock(&glFlushMutex);
155 glFlush = _glapi_get_proc_address("glFlush");
156 mtx_unlock(&glFlushMutex);
158 /* if glFlush is not available things are horribly broken */
160 _eglLog(_EGL_WARNING, "DRI2: failed to find glFlush entry point");
168 dri_is_thread_safe(void *loaderPrivate)
170 struct dri2_egl_surface *dri2_surf = loaderPrivate;
171 UNUSED _EGLDisplay *display = dri2_surf->base.Resource.Display;
173 #ifdef HAVE_X11_PLATFORM
174 Display *xdpy = (Display*)display->PlatformDisplay;
176 /* Check Xlib is running in thread safe mode when running on EGL/X11-xlib
179 * 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
180 * It wll be NULL if XInitThreads wasn't called.
182 if (display->Platform == _EGL_PLATFORM_X11 && xdpy && !xdpy->lock_fns)
186 #ifdef HAVE_XCB_PLATFORM
187 if (display->Platform == _EGL_PLATFORM_XCB)
191 #ifdef HAVE_WAYLAND_PLATFORM
192 if (display->Platform == _EGL_PLATFORM_WAYLAND)
199 const __DRIbackgroundCallableExtension background_callable_extension = {
200 .base = { __DRI_BACKGROUND_CALLABLE, 2 },
202 .setBackgroundContext = dri_set_background_context,
203 .isThreadSafe = dri_is_thread_safe,
206 const __DRIuseInvalidateExtension use_invalidate = {
207 .base = { __DRI_USE_INVALIDATE, 1 }
211 dri2_get_pbuffer_drawable_info(__DRIdrawable * draw,
212 int *x, int *y, int *w, int *h,
215 struct dri2_egl_surface *dri2_surf = loaderPrivate;
218 *w = dri2_surf->base.Width;
219 *h = dri2_surf->base.Height;
223 dri2_get_bytes_per_pixel(struct dri2_egl_surface *dri2_surf)
225 const int depth = dri2_surf->base.Config->BufferSize;
226 return depth ? util_next_power_of_two(depth / 8) : 0;
230 dri2_put_image(__DRIdrawable * draw, int op,
231 int x, int y, int w, int h,
232 char *data, void *loaderPrivate)
234 struct dri2_egl_surface *dri2_surf = loaderPrivate;
235 const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
236 const int width = dri2_surf->base.Width;
237 const int height = dri2_surf->base.Height;
238 const int dst_stride = width*bpp;
239 const int src_stride = w*bpp;
240 const int x_offset = x*bpp;
241 int copy_width = src_stride;
243 if (!dri2_surf->swrast_device_buffer)
244 dri2_surf->swrast_device_buffer = malloc(height*dst_stride);
246 if (dri2_surf->swrast_device_buffer) {
247 const char *src = data;
248 char *dst = dri2_surf->swrast_device_buffer;
253 /* Drivers are allowed to submit OOB PutImage requests, so clip here. */
254 if (copy_width > dst_stride - x_offset)
255 copy_width = dst_stride - x_offset;
260 memcpy(dst, src, copy_width);
268 dri2_get_image(__DRIdrawable * read,
269 int x, int y, int w, int h,
270 char *data, void *loaderPrivate)
272 struct dri2_egl_surface *dri2_surf = loaderPrivate;
273 const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
274 const int width = dri2_surf->base.Width;
275 const int height = dri2_surf->base.Height;
276 const int src_stride = width*bpp;
277 const int dst_stride = w*bpp;
278 const int x_offset = x*bpp;
279 int copy_width = dst_stride;
280 const char *src = dri2_surf->swrast_device_buffer;
284 memset(data, 0, copy_width * h);
291 /* Drivers are allowed to submit OOB GetImage requests, so clip here. */
292 if (copy_width > src_stride - x_offset)
293 copy_width = src_stride - x_offset;
298 memcpy(dst, src, copy_width);
305 /* HACK: technically we should have swrast_null, instead of these.
307 const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension = {
308 .base = { __DRI_SWRAST_LOADER, 1 },
309 .getDrawableInfo = dri2_get_pbuffer_drawable_info,
310 .putImage = dri2_put_image,
311 .getImage = dri2_get_image,
314 static const EGLint dri2_to_egl_attribute_map[__DRI_ATTRIB_MAX] = {
315 [__DRI_ATTRIB_BUFFER_SIZE ] = EGL_BUFFER_SIZE,
316 [__DRI_ATTRIB_LEVEL] = EGL_LEVEL,
317 [__DRI_ATTRIB_LUMINANCE_SIZE] = EGL_LUMINANCE_SIZE,
318 [__DRI_ATTRIB_DEPTH_SIZE] = EGL_DEPTH_SIZE,
319 [__DRI_ATTRIB_STENCIL_SIZE] = EGL_STENCIL_SIZE,
320 [__DRI_ATTRIB_SAMPLE_BUFFERS] = EGL_SAMPLE_BUFFERS,
321 [__DRI_ATTRIB_SAMPLES] = EGL_SAMPLES,
322 [__DRI_ATTRIB_MAX_PBUFFER_WIDTH] = EGL_MAX_PBUFFER_WIDTH,
323 [__DRI_ATTRIB_MAX_PBUFFER_HEIGHT] = EGL_MAX_PBUFFER_HEIGHT,
324 [__DRI_ATTRIB_MAX_PBUFFER_PIXELS] = EGL_MAX_PBUFFER_PIXELS,
325 [__DRI_ATTRIB_MAX_SWAP_INTERVAL] = EGL_MAX_SWAP_INTERVAL,
326 [__DRI_ATTRIB_MIN_SWAP_INTERVAL] = EGL_MIN_SWAP_INTERVAL,
327 [__DRI_ATTRIB_YINVERTED] = EGL_Y_INVERTED_NOK,
331 dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,
334 const bool double_buffer = surface_type == EGL_WINDOW_BIT;
335 const bool srgb = colorspace == EGL_GL_COLORSPACE_SRGB_KHR;
337 return conf->dri_config[double_buffer][srgb];
341 dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria)
343 if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0)
346 if (!_eglMatchConfig(conf, criteria))
353 dri2_get_shifts_and_sizes(const __DRIcoreExtension *core,
354 const __DRIconfig *config, int *shifts,
359 if (core->getConfigAttrib(config, __DRI_ATTRIB_RED_SHIFT, (unsigned int *)&shifts[0])) {
360 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SHIFT, (unsigned int *)&shifts[1]);
361 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SHIFT, (unsigned int *)&shifts[2]);
362 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SHIFT, (unsigned int *)&shifts[3]);
364 /* Driver isn't exposing shifts, so convert masks to shifts */
365 core->getConfigAttrib(config, __DRI_ATTRIB_RED_MASK, &mask);
366 shifts[0] = ffs(mask) - 1;
367 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_MASK, &mask);
368 shifts[1] = ffs(mask) - 1;
369 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_MASK, &mask);
370 shifts[2] = ffs(mask) - 1;
371 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_MASK, &mask);
372 shifts[3] = ffs(mask) - 1;
375 core->getConfigAttrib(config, __DRI_ATTRIB_RED_SIZE, &sizes[0]);
376 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SIZE, &sizes[1]);
377 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SIZE, &sizes[2]);
378 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SIZE, &sizes[3]);
382 dri2_get_render_type_float(const __DRIcoreExtension *core,
383 const __DRIconfig *config,
386 unsigned int render_type;
388 core->getConfigAttrib(config, __DRI_ATTRIB_RENDER_TYPE, &render_type);
389 *is_float = (render_type & __DRI_ATTRIB_FLOAT_BIT) ? true : false;
393 dri2_image_format_for_pbuffer_config(struct dri2_egl_display *dri2_dpy,
394 const __DRIconfig *config)
397 unsigned int sizes[4];
399 dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
401 for (unsigned i = 0; i < ARRAY_SIZE(dri2_pbuffer_visuals); ++i) {
402 const struct dri2_pbuffer_visual *visual = &dri2_pbuffer_visuals[i];
404 if (shifts[0] == visual->rgba_shifts[0] &&
405 shifts[1] == visual->rgba_shifts[1] &&
406 shifts[2] == visual->rgba_shifts[2] &&
407 shifts[3] == visual->rgba_shifts[3] &&
408 sizes[0] == visual->rgba_sizes[0] &&
409 sizes[1] == visual->rgba_sizes[1] &&
410 sizes[2] == visual->rgba_sizes[2] &&
411 sizes[3] == visual->rgba_sizes[3]) {
412 return visual->dri_image_format;
416 return __DRI_IMAGE_FORMAT_NONE;
419 struct dri2_egl_config *
420 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
421 EGLint surface_type, const EGLint *attr_list,
422 const int *rgba_shifts, const unsigned int *rgba_sizes)
424 struct dri2_egl_config *conf;
425 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
427 unsigned int attrib, value, double_buffer;
429 EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
430 int dri_shifts[4] = { -1, -1, -1, -1 };
431 unsigned int dri_sizes[4] = { 0, 0, 0, 0 };
432 _EGLConfig *matching_config;
433 EGLint num_configs = 0;
436 _eglInitConfig(&base, disp, id);
439 bind_to_texture_rgb = 0;
440 bind_to_texture_rgba = 0;
442 for (int i = 0; i < __DRI_ATTRIB_MAX; ++i) {
443 if (!dri2_dpy->core->indexConfigAttrib(dri_config, i, &attrib, &value))
447 case __DRI_ATTRIB_RENDER_TYPE:
448 if (value & __DRI_ATTRIB_FLOAT_BIT)
449 base.ComponentType = EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT;
450 if (value & __DRI_ATTRIB_RGBA_BIT)
451 value = EGL_RGB_BUFFER;
452 else if (value & __DRI_ATTRIB_LUMINANCE_BIT)
453 value = EGL_LUMINANCE_BUFFER;
456 base.ColorBufferType = value;
459 case __DRI_ATTRIB_CONFIG_CAVEAT:
460 if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
461 value = EGL_NON_CONFORMANT_CONFIG;
462 else if (value & __DRI_ATTRIB_SLOW_BIT)
463 value = EGL_SLOW_CONFIG;
466 base.ConfigCaveat = value;
469 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
470 bind_to_texture_rgb = value;
473 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
474 bind_to_texture_rgba = value;
477 case __DRI_ATTRIB_DOUBLE_BUFFER:
478 double_buffer = value;
481 case __DRI_ATTRIB_RED_SIZE:
482 dri_sizes[0] = value;
483 base.RedSize = value;
486 case __DRI_ATTRIB_RED_MASK:
487 dri_shifts[0] = ffs(value) - 1;
490 case __DRI_ATTRIB_RED_SHIFT:
491 dri_shifts[0] = value;
494 case __DRI_ATTRIB_GREEN_SIZE:
495 dri_sizes[1] = value;
496 base.GreenSize = value;
499 case __DRI_ATTRIB_GREEN_MASK:
500 dri_shifts[1] = ffs(value) - 1;
503 case __DRI_ATTRIB_GREEN_SHIFT:
504 dri_shifts[1] = value;
507 case __DRI_ATTRIB_BLUE_SIZE:
508 dri_sizes[2] = value;
509 base.BlueSize = value;
512 case __DRI_ATTRIB_BLUE_MASK:
513 dri_shifts[2] = ffs(value) - 1;
516 case __DRI_ATTRIB_BLUE_SHIFT:
517 dri_shifts[2] = value;
520 case __DRI_ATTRIB_ALPHA_SIZE:
521 dri_sizes[3] = value;
522 base.AlphaSize = value;
525 case __DRI_ATTRIB_ALPHA_MASK:
526 dri_shifts[3] = ffs(value) - 1;
529 case __DRI_ATTRIB_ALPHA_SHIFT:
530 dri_shifts[3] = value;
533 case __DRI_ATTRIB_ACCUM_RED_SIZE:
534 case __DRI_ATTRIB_ACCUM_GREEN_SIZE:
535 case __DRI_ATTRIB_ACCUM_BLUE_SIZE:
536 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE:
537 /* Don't expose visuals with the accumulation buffer. */
542 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE:
544 if (!disp->Extensions.KHR_gl_colorspace && srgb)
548 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH:
549 base.MaxPbufferWidth = _EGL_MAX_PBUFFER_WIDTH;
551 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT:
552 base.MaxPbufferHeight = _EGL_MAX_PBUFFER_HEIGHT;
554 case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER:
555 if (disp->Extensions.KHR_mutable_render_buffer)
556 surface_type |= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR;
559 key = dri2_to_egl_attribute_map[attrib];
561 _eglSetConfigKey(&base, key, value);
567 for (int i = 0; attr_list[i] != EGL_NONE; i += 2)
568 _eglSetConfigKey(&base, attr_list[i], attr_list[i+1]);
570 if (rgba_shifts && memcmp(rgba_shifts, dri_shifts, sizeof(dri_shifts)))
573 if (rgba_sizes && memcmp(rgba_sizes, dri_sizes, sizeof(dri_sizes)))
576 base.NativeRenderable = EGL_TRUE;
578 base.SurfaceType = surface_type;
579 if (surface_type & (EGL_PBUFFER_BIT |
580 (disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) {
581 base.BindToTextureRGB = bind_to_texture_rgb;
582 if (base.AlphaSize > 0)
583 base.BindToTextureRGBA = bind_to_texture_rgba;
587 surface_type &= ~EGL_PIXMAP_BIT;
590 /* No support for pbuffer + MSAA for now.
592 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
593 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
596 surface_type &= ~EGL_PBUFFER_BIT;
602 base.RenderableType = disp->ClientAPIs;
603 base.Conformant = disp->ClientAPIs;
605 base.MinSwapInterval = dri2_dpy->min_swap_interval;
606 base.MaxSwapInterval = dri2_dpy->max_swap_interval;
608 if (!_eglValidateConfig(&base, EGL_FALSE)) {
609 _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);
613 config_id = base.ConfigID;
614 base.ConfigID = EGL_DONT_CARE;
615 base.SurfaceType = EGL_DONT_CARE;
616 num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1,
617 (_EGLArrayForEach) dri2_match_config, &base);
619 if (num_configs == 1) {
620 conf = (struct dri2_egl_config *) matching_config;
622 if (!conf->dri_config[double_buffer][srgb])
623 conf->dri_config[double_buffer][srgb] = dri_config;
625 /* a similar config type is already added (unlikely) => discard */
628 else if (num_configs == 0) {
629 conf = calloc(1, sizeof *conf);
633 conf->dri_config[double_buffer][srgb] = dri_config;
635 memcpy(&conf->base, &base, sizeof base);
636 conf->base.SurfaceType = 0;
637 conf->base.ConfigID = config_id;
639 _eglLinkConfig(&conf->base);
642 unreachable("duplicates should not be possible");
646 conf->base.SurfaceType |= surface_type;
652 dri2_add_pbuffer_configs_for_visuals(_EGLDisplay *disp)
654 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
655 unsigned int format_count[ARRAY_SIZE(dri2_pbuffer_visuals)] = { 0 };
656 unsigned int config_count = 0;
658 for (unsigned i = 0; dri2_dpy->driver_configs[i] != NULL; i++) {
659 for (unsigned j = 0; j < ARRAY_SIZE(dri2_pbuffer_visuals); j++) {
660 struct dri2_egl_config *dri2_conf;
662 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
663 config_count + 1, EGL_PBUFFER_BIT, NULL,
664 dri2_pbuffer_visuals[j].rgba_shifts, dri2_pbuffer_visuals[j].rgba_sizes);
667 if (dri2_conf->base.ConfigID == config_count + 1)
674 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
675 if (!format_count[i]) {
676 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
677 dri2_pbuffer_visuals[i].format_name);
681 return (config_count != 0);
685 dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
687 _EGLDisplay *disp = data;
688 struct dri2_egl_image *dri2_img;
693 img = _eglLookupImage(image, disp);
695 _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image");
699 dri2_img = dri2_egl_image(image);
701 return dri2_img->dri_image;
704 const __DRIimageLookupExtension image_lookup_extension = {
705 .base = { __DRI_IMAGE_LOOKUP, 1 },
707 .lookupEGLImage = dri2_lookup_egl_image
710 struct dri2_extension_match {
716 static const struct dri2_extension_match dri3_driver_extensions[] = {
717 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
718 { __DRI_IMAGE_DRIVER, 1, offsetof(struct dri2_egl_display, image_driver) },
722 static const struct dri2_extension_match dri2_driver_extensions[] = {
723 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
724 { __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) },
728 static const struct dri2_extension_match dri2_core_extensions[] = {
729 { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) },
730 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
731 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
735 static const struct dri2_extension_match swrast_driver_extensions[] = {
736 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
737 { __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) },
741 static const struct dri2_extension_match swrast_core_extensions[] = {
742 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
746 static const struct dri2_extension_match optional_driver_extensions[] = {
747 { __DRI_CONFIG_OPTIONS, 1, offsetof(struct dri2_egl_display, configOptions) },
751 static const struct dri2_extension_match optional_core_extensions[] = {
752 { __DRI2_ROBUSTNESS, 1, offsetof(struct dri2_egl_display, robustness) },
753 { __DRI2_NO_ERROR, 1, offsetof(struct dri2_egl_display, no_error) },
754 { __DRI2_CONFIG_QUERY, 1, offsetof(struct dri2_egl_display, config) },
755 { __DRI2_FENCE, 1, offsetof(struct dri2_egl_display, fence) },
756 { __DRI2_BUFFER_DAMAGE, 1, offsetof(struct dri2_egl_display, buffer_damage) },
757 { __DRI2_RENDERER_QUERY, 1, offsetof(struct dri2_egl_display, rendererQuery) },
758 { __DRI2_INTEROP, 1, offsetof(struct dri2_egl_display, interop) },
759 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
760 { __DRI2_FLUSH_CONTROL, 1, offsetof(struct dri2_egl_display, flush_control) },
761 { __DRI2_BLOB, 1, offsetof(struct dri2_egl_display, blob) },
762 { __DRI_MUTABLE_RENDER_BUFFER_DRIVER, 1, offsetof(struct dri2_egl_display, mutable_render_buffer) },
767 dri2_bind_extensions(struct dri2_egl_display *dri2_dpy,
768 const struct dri2_extension_match *matches,
769 const __DRIextension **extensions,
775 for (int i = 0; extensions[i]; i++) {
776 _eglLog(_EGL_DEBUG, "found extension `%s'", extensions[i]->name);
777 for (int j = 0; matches[j].name; j++) {
778 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
779 extensions[i]->version >= matches[j].version) {
780 field = ((char *) dri2_dpy + matches[j].offset);
781 *(const __DRIextension **) field = extensions[i];
782 _eglLog(_EGL_INFO, "found extension %s version %d",
783 extensions[i]->name, extensions[i]->version);
789 for (int j = 0; matches[j].name; j++) {
790 field = ((char *) dri2_dpy + matches[j].offset);
791 if (*(const __DRIextension **) field == NULL) {
793 _eglLog(_EGL_DEBUG, "did not find optional extension %s version %d",
794 matches[j].name, matches[j].version);
796 _eglLog(_EGL_WARNING, "did not find extension %s version %d",
797 matches[j].name, matches[j].version);
806 static const __DRIextension **
807 dri2_open_driver(_EGLDisplay *disp)
809 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
810 static const char *search_path_vars[] = {
811 "LIBGL_DRIVERS_PATH",
815 return loader_open_driver(dri2_dpy->driver_name,
821 dri2_load_driver_common(_EGLDisplay *disp,
822 const struct dri2_extension_match *driver_extensions)
824 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
825 const __DRIextension **extensions;
827 extensions = dri2_open_driver(disp);
831 if (!dri2_bind_extensions(dri2_dpy, driver_extensions, extensions, false)) {
832 dlclose(dri2_dpy->driver);
833 dri2_dpy->driver = NULL;
836 dri2_dpy->driver_extensions = extensions;
838 dri2_bind_extensions(dri2_dpy, optional_driver_extensions, extensions, true);
844 dri2_load_driver(_EGLDisplay *disp)
846 return dri2_load_driver_common(disp, dri2_driver_extensions);
850 dri2_load_driver_dri3(_EGLDisplay *disp)
852 return dri2_load_driver_common(disp, dri3_driver_extensions);
856 dri2_load_driver_swrast(_EGLDisplay *disp)
858 return dri2_load_driver_common(disp, swrast_driver_extensions);
862 dri2_renderer_query_integer(struct dri2_egl_display *dri2_dpy, int param)
864 const __DRI2rendererQueryExtension *rendererQuery = dri2_dpy->rendererQuery;
865 unsigned int value = 0;
867 if (!rendererQuery ||
868 rendererQuery->queryInteger(dri2_dpy->dri_screen, param, &value) == -1)
875 dri2_query_driver_name(_EGLDisplay *disp)
877 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
878 return dri2_dpy->driver_name;
882 dri2_query_driver_config(_EGLDisplay *disp)
884 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
885 const __DRIconfigOptionsExtension *ext = dri2_dpy->configOptions;
887 if (ext->base.version >= 2)
888 return ext->getXml(dri2_dpy->driver_name);
890 return strdup(ext->xml);
895 dri2_setup_screen(_EGLDisplay *disp)
897 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
898 unsigned int api_mask;
901 * EGL 1.5 specification defines the default value to 1. Moreover,
902 * eglSwapInterval() is required to clamp requested value to the supported
903 * range. Since the default value is implicitly assumed to be supported,
904 * use it as both minimum and maximum for the platforms that do not allow
905 * changing the interval. Platforms, which allow it (e.g. x11, wayland)
906 * override these values already.
908 dri2_dpy->min_swap_interval = 1;
909 dri2_dpy->max_swap_interval = 1;
910 dri2_dpy->default_swap_interval = 1;
912 if (dri2_dpy->image_driver) {
913 api_mask = dri2_dpy->image_driver->getAPIMask(dri2_dpy->dri_screen);
914 } else if (dri2_dpy->dri2) {
915 api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);
917 assert(dri2_dpy->swrast);
918 api_mask = 1 << __DRI_API_OPENGL |
919 1 << __DRI_API_GLES |
920 1 << __DRI_API_GLES2 |
921 1 << __DRI_API_GLES3;
924 disp->ClientAPIs = 0;
925 if ((api_mask & (1 <<__DRI_API_OPENGL)) && _eglIsApiValid(EGL_OPENGL_API))
926 disp->ClientAPIs |= EGL_OPENGL_BIT;
927 if ((api_mask & (1 << __DRI_API_GLES)) && _eglIsApiValid(EGL_OPENGL_ES_API))
928 disp->ClientAPIs |= EGL_OPENGL_ES_BIT;
929 if ((api_mask & (1 << __DRI_API_GLES2)) && _eglIsApiValid(EGL_OPENGL_ES_API))
930 disp->ClientAPIs |= EGL_OPENGL_ES2_BIT;
931 if ((api_mask & (1 << __DRI_API_GLES3)) && _eglIsApiValid(EGL_OPENGL_ES_API))
932 disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR;
934 assert(dri2_dpy->image_driver || dri2_dpy->dri2 || dri2_dpy->swrast);
935 disp->Extensions.KHR_no_config_context = EGL_TRUE;
936 disp->Extensions.KHR_surfaceless_context = EGL_TRUE;
938 if (dri2_dpy->configOptions) {
939 disp->Extensions.MESA_query_driver = EGL_TRUE;
942 /* Report back to EGL the bitmask of priorities supported */
943 disp->Extensions.IMG_context_priority =
944 dri2_renderer_query_integer(dri2_dpy,
945 __DRI2_RENDERER_HAS_CONTEXT_PRIORITY);
947 disp->Extensions.EXT_pixel_format_float = EGL_TRUE;
949 if (dri2_renderer_query_integer(dri2_dpy,
950 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB))
951 disp->Extensions.KHR_gl_colorspace = EGL_TRUE;
953 if (dri2_dpy->image_driver ||
954 (dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) ||
955 (dri2_dpy->swrast && dri2_dpy->swrast->base.version >= 3)) {
956 disp->Extensions.KHR_create_context = EGL_TRUE;
958 if (dri2_dpy->robustness)
959 disp->Extensions.EXT_create_context_robustness = EGL_TRUE;
962 if (dri2_dpy->no_error)
963 disp->Extensions.KHR_create_context_no_error = EGL_TRUE;
965 if (dri2_dpy->fence) {
966 disp->Extensions.KHR_fence_sync = EGL_TRUE;
967 disp->Extensions.KHR_wait_sync = EGL_TRUE;
968 if (dri2_dpy->fence->get_fence_from_cl_event)
969 disp->Extensions.KHR_cl_event2 = EGL_TRUE;
970 if (dri2_dpy->fence->base.version >= 2 &&
971 dri2_dpy->fence->get_capabilities) {
972 unsigned capabilities =
973 dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen);
974 disp->Extensions.ANDROID_native_fence_sync =
975 (capabilities & __DRI_FENCE_CAP_NATIVE_FD) != 0;
980 disp->Extensions.ANDROID_blob_cache = EGL_TRUE;
982 disp->Extensions.KHR_reusable_sync = EGL_TRUE;
984 if (dri2_dpy->image) {
985 if (dri2_dpy->image->base.version >= 10 &&
986 dri2_dpy->image->getCapabilities != NULL) {
989 capabilities = dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
990 disp->Extensions.MESA_drm_image = (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
992 if (dri2_dpy->image->base.version >= 11)
993 disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
995 disp->Extensions.MESA_drm_image = EGL_TRUE;
996 if (dri2_dpy->image->base.version >= 11)
997 disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
1000 disp->Extensions.KHR_image_base = EGL_TRUE;
1001 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
1002 if (dri2_dpy->image->base.version >= 5 &&
1003 dri2_dpy->image->createImageFromTexture) {
1004 disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
1005 disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
1007 if (dri2_renderer_query_integer(dri2_dpy,
1008 __DRI2_RENDERER_HAS_TEXTURE_3D))
1009 disp->Extensions.KHR_gl_texture_3D_image = EGL_TRUE;
1012 if (dri2_dpy->image->base.version >= 8 &&
1013 dri2_dpy->image->createImageFromDmaBufs) {
1014 disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE;
1015 disp->Extensions.EXT_image_dma_buf_import_modifiers = EGL_TRUE;
1020 if (dri2_dpy->flush_control)
1021 disp->Extensions.KHR_context_flush_control = EGL_TRUE;
1023 if (dri2_dpy->buffer_damage && dri2_dpy->buffer_damage->set_damage_region)
1024 disp->Extensions.KHR_partial_update = EGL_TRUE;
1026 disp->Extensions.EXT_protected_content =
1027 dri2_renderer_query_integer(dri2_dpy,
1028 __DRI2_RENDERER_HAS_PROTECTED_CONTENT);
1032 dri2_setup_swap_interval(_EGLDisplay *disp, int max_swap_interval)
1034 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1035 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1037 /* Allow driconf to override applications.*/
1038 if (dri2_dpy->config)
1039 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
1040 "vblank_mode", &vblank_mode);
1041 switch (vblank_mode) {
1042 case DRI_CONF_VBLANK_NEVER:
1043 dri2_dpy->min_swap_interval = 0;
1044 dri2_dpy->max_swap_interval = 0;
1045 dri2_dpy->default_swap_interval = 0;
1047 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1048 dri2_dpy->min_swap_interval = 1;
1049 dri2_dpy->max_swap_interval = max_swap_interval;
1050 dri2_dpy->default_swap_interval = 1;
1052 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1053 dri2_dpy->min_swap_interval = 0;
1054 dri2_dpy->max_swap_interval = max_swap_interval;
1055 dri2_dpy->default_swap_interval = 0;
1058 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1059 dri2_dpy->min_swap_interval = 0;
1060 dri2_dpy->max_swap_interval = max_swap_interval;
1061 dri2_dpy->default_swap_interval = 1;
1066 /* All platforms but DRM call this function to create the screen and populate
1067 * the driver_configs. DRM inherits that information from its display - GBM.
1070 dri2_create_screen(_EGLDisplay *disp)
1072 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1074 if (dri2_dpy->image_driver) {
1075 dri2_dpy->dri_screen =
1076 dri2_dpy->image_driver->createNewScreen2(0, dri2_dpy->fd,
1077 dri2_dpy->loader_extensions,
1078 dri2_dpy->driver_extensions,
1079 &dri2_dpy->driver_configs,
1081 } else if (dri2_dpy->dri2) {
1082 if (dri2_dpy->dri2->base.version >= 4) {
1083 dri2_dpy->dri_screen =
1084 dri2_dpy->dri2->createNewScreen2(0, dri2_dpy->fd,
1085 dri2_dpy->loader_extensions,
1086 dri2_dpy->driver_extensions,
1087 &dri2_dpy->driver_configs, disp);
1089 dri2_dpy->dri_screen =
1090 dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd,
1091 dri2_dpy->loader_extensions,
1092 &dri2_dpy->driver_configs, disp);
1095 assert(dri2_dpy->swrast);
1096 if (dri2_dpy->swrast->base.version >= 4) {
1097 dri2_dpy->dri_screen =
1098 dri2_dpy->swrast->createNewScreen2(0, dri2_dpy->loader_extensions,
1099 dri2_dpy->driver_extensions,
1100 &dri2_dpy->driver_configs, disp);
1102 dri2_dpy->dri_screen =
1103 dri2_dpy->swrast->createNewScreen(0, dri2_dpy->loader_extensions,
1104 &dri2_dpy->driver_configs, disp);
1108 if (dri2_dpy->dri_screen == NULL) {
1109 _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen");
1113 dri2_dpy->own_dri_screen = true;
1118 dri2_setup_extensions(_EGLDisplay *disp)
1120 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1121 const struct dri2_extension_match *mandatory_core_extensions;
1122 const __DRIextension **extensions;
1124 extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
1126 if (dri2_dpy->image_driver || dri2_dpy->dri2)
1127 mandatory_core_extensions = dri2_core_extensions;
1129 mandatory_core_extensions = swrast_core_extensions;
1131 if (!dri2_bind_extensions(dri2_dpy, mandatory_core_extensions, extensions, false))
1134 #ifdef HAVE_DRI3_MODIFIERS
1135 dri2_dpy->multibuffers_available =
1136 (dri2_dpy->dri3_major_version > 1 || (dri2_dpy->dri3_major_version == 1 &&
1137 dri2_dpy->dri3_minor_version >= 2)) &&
1138 (dri2_dpy->present_major_version > 1 || (dri2_dpy->present_major_version == 1 &&
1139 dri2_dpy->present_minor_version >= 2)) &&
1140 (dri2_dpy->image && dri2_dpy->image->base.version >= 15);
1143 dri2_bind_extensions(dri2_dpy, optional_core_extensions, extensions, true);
1148 * Called via eglInitialize(), drv->Initialize().
1150 * This must be guaranteed to be called exactly once, even if eglInitialize is
1151 * called many times (without a eglTerminate in between).
1154 dri2_initialize(_EGLDisplay *disp)
1156 EGLBoolean ret = EGL_FALSE;
1157 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1159 /* In the case where the application calls eglMakeCurrent(context1),
1160 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
1161 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
1162 * initialized, as we need it to be able to free context1 correctly.
1164 * It would probably be safest to forcibly release the display with
1165 * dri2_display_release, to make sure the display is reinitialized correctly.
1166 * However, the EGL spec states that we need to keep a reference to the
1167 * current context (so we cannot call dri2_make_current(NULL)), and therefore
1168 * we would leak context1 as we would be missing the old display connection
1169 * to free it up correctly.
1172 dri2_dpy->ref_count++;
1176 loader_set_logger(_eglLog);
1178 switch (disp->Platform) {
1179 case _EGL_PLATFORM_SURFACELESS:
1180 ret = dri2_initialize_surfaceless(disp);
1182 case _EGL_PLATFORM_DEVICE:
1183 ret = dri2_initialize_device(disp);
1185 case _EGL_PLATFORM_X11:
1186 case _EGL_PLATFORM_XCB:
1187 ret = dri2_initialize_x11(disp);
1189 case _EGL_PLATFORM_DRM:
1190 ret = dri2_initialize_drm(disp);
1192 case _EGL_PLATFORM_WAYLAND:
1193 ret = dri2_initialize_wayland(disp);
1195 case _EGL_PLATFORM_ANDROID:
1196 ret = dri2_initialize_android(disp);
1199 unreachable("Callers ensure we cannot get here.");
1206 dri2_dpy = dri2_egl_display(disp);
1207 dri2_dpy->ref_count++;
1213 * Decrement display reference count, and free up display if necessary.
1216 dri2_display_release(_EGLDisplay *disp)
1218 struct dri2_egl_display *dri2_dpy;
1223 dri2_dpy = dri2_egl_display(disp);
1225 assert(dri2_dpy->ref_count > 0);
1226 dri2_dpy->ref_count--;
1228 if (dri2_dpy->ref_count > 0)
1231 _eglCleanupDisplay(disp);
1232 dri2_display_destroy(disp);
1236 dri2_display_destroy(_EGLDisplay *disp)
1238 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1240 if (dri2_dpy->own_dri_screen) {
1241 if (dri2_dpy->vtbl && dri2_dpy->vtbl->close_screen_notify)
1242 dri2_dpy->vtbl->close_screen_notify(disp);
1243 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1245 if (dri2_dpy->fd >= 0)
1246 close(dri2_dpy->fd);
1247 if (dri2_dpy->driver)
1248 dlclose(dri2_dpy->driver);
1249 free(dri2_dpy->driver_name);
1251 #ifdef HAVE_WAYLAND_PLATFORM
1252 free(dri2_dpy->device_name);
1255 switch (disp->Platform) {
1256 case _EGL_PLATFORM_X11:
1257 dri2_teardown_x11(dri2_dpy);
1259 case _EGL_PLATFORM_DRM:
1260 dri2_teardown_drm(dri2_dpy);
1262 case _EGL_PLATFORM_WAYLAND:
1263 dri2_teardown_wayland(dri2_dpy);
1266 /* TODO: add teardown for other platforms */
1270 /* The drm platform does not create the screen/driver_configs but reuses
1271 * the ones from the gbm device. As such the gbm itself is responsible
1274 if (disp->Platform != _EGL_PLATFORM_DRM && dri2_dpy->driver_configs) {
1275 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++)
1276 free((__DRIconfig *) dri2_dpy->driver_configs[i]);
1277 free(dri2_dpy->driver_configs);
1280 disp->DriverData = NULL;
1284 dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
1285 unsigned int att, unsigned int format)
1287 struct dri2_egl_display *dri2_dpy =
1288 dri2_egl_display(dri2_surf->base.Resource.Display);
1290 if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
1293 if (!dri2_surf->local_buffers[att]) {
1294 dri2_surf->local_buffers[att] =
1295 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
1296 dri2_surf->base.Width, dri2_surf->base.Height);
1299 return dri2_surf->local_buffers[att];
1303 dri2_egl_surface_free_local_buffers(struct dri2_egl_surface *dri2_surf)
1305 struct dri2_egl_display *dri2_dpy =
1306 dri2_egl_display(dri2_surf->base.Resource.Display);
1308 for (int i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
1309 if (dri2_surf->local_buffers[i]) {
1310 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
1311 dri2_surf->local_buffers[i]);
1312 dri2_surf->local_buffers[i] = NULL;
1318 * Called via eglTerminate(), drv->Terminate().
1320 * This must be guaranteed to be called exactly once, even if eglTerminate is
1321 * called many times (without a eglInitialize in between).
1324 dri2_terminate(_EGLDisplay *disp)
1326 /* Release all non-current Context/Surfaces. */
1327 _eglReleaseDisplayResources(disp);
1329 dri2_display_release(disp);
1335 * Set the error code after a call to
1336 * dri2_egl_display::dri2::createContextAttribs.
1339 dri2_create_context_attribs_error(int dri_error)
1343 switch (dri_error) {
1344 case __DRI_CTX_ERROR_SUCCESS:
1347 case __DRI_CTX_ERROR_NO_MEMORY:
1348 egl_error = EGL_BAD_ALLOC;
1351 /* From the EGL_KHR_create_context spec, section "Errors":
1353 * * If <config> does not support a client API context compatible
1354 * with the requested API major and minor version, [...] context flags,
1355 * and context reset notification behavior (for client API types where
1356 * these attributes are supported), then an EGL_BAD_MATCH error is
1359 * * If an OpenGL ES context is requested and the values for
1360 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1361 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1362 * is not defined, than an EGL_BAD_MATCH error is generated.
1364 * * If an OpenGL context is requested, the requested version is
1365 * greater than 3.2, and the value for attribute
1366 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1367 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1368 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1369 * one of these bits set; or if the implementation does not support
1370 * the requested profile, then an EGL_BAD_MATCH error is generated.
1372 case __DRI_CTX_ERROR_BAD_API:
1373 case __DRI_CTX_ERROR_BAD_VERSION:
1374 case __DRI_CTX_ERROR_BAD_FLAG:
1375 egl_error = EGL_BAD_MATCH;
1378 /* From the EGL_KHR_create_context spec, section "Errors":
1380 * * If an attribute name or attribute value in <attrib_list> is not
1381 * recognized (including unrecognized bits in bitmask attributes),
1382 * then an EGL_BAD_ATTRIBUTE error is generated."
1384 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE:
1385 case __DRI_CTX_ERROR_UNKNOWN_FLAG:
1386 egl_error = EGL_BAD_ATTRIBUTE;
1390 assert(!"unknown dri_error code");
1391 egl_error = EGL_BAD_MATCH;
1395 _eglError(egl_error, "dri2_create_context");
1399 dri2_fill_context_attribs(struct dri2_egl_context *dri2_ctx,
1400 struct dri2_egl_display *dri2_dpy,
1401 uint32_t *ctx_attribs,
1402 unsigned *num_attribs)
1406 assert(*num_attribs >= NUM_ATTRIBS);
1408 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
1409 ctx_attribs[pos++] = dri2_ctx->base.ClientMajorVersion;
1410 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
1411 ctx_attribs[pos++] = dri2_ctx->base.ClientMinorVersion;
1413 if (dri2_ctx->base.Flags != 0 || dri2_ctx->base.NoError) {
1414 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1415 * extension, don't even try to send it the robust-access flag.
1416 * It may explode. Instead, generate the required EGL error here.
1418 if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0
1419 && !dri2_dpy->robustness) {
1420 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1424 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_FLAGS;
1425 ctx_attribs[pos++] = dri2_ctx->base.Flags |
1426 (dri2_ctx->base.NoError ? __DRI_CTX_FLAG_NO_ERROR : 0);
1429 if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) {
1430 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1431 * extension, don't even try to send it a reset strategy. It may
1432 * explode. Instead, generate the required EGL error here.
1434 if (!dri2_dpy->robustness) {
1435 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1439 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
1440 ctx_attribs[pos++] = __DRI_CTX_RESET_LOSE_CONTEXT;
1443 if (dri2_ctx->base.ContextPriority != EGL_CONTEXT_PRIORITY_MEDIUM_IMG) {
1446 switch (dri2_ctx->base.ContextPriority) {
1447 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
1448 val = __DRI_CTX_PRIORITY_HIGH;
1450 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
1451 val = __DRI_CTX_PRIORITY_MEDIUM;
1453 case EGL_CONTEXT_PRIORITY_LOW_IMG:
1454 val = __DRI_CTX_PRIORITY_LOW;
1457 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1461 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_PRIORITY;
1462 ctx_attribs[pos++] = val;
1465 if (dri2_ctx->base.ReleaseBehavior == EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR) {
1466 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
1467 ctx_attribs[pos++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE;
1476 * Called via eglCreateContext(), drv->CreateContext().
1478 static _EGLContext *
1479 dri2_create_context(_EGLDisplay *disp, _EGLConfig *conf,
1480 _EGLContext *share_list, const EGLint *attrib_list)
1482 struct dri2_egl_context *dri2_ctx;
1483 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1484 struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
1485 __DRIcontext *shared =
1486 dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL;
1487 struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
1488 const __DRIconfig *dri_config;
1491 unsigned num_attribs = NUM_ATTRIBS;
1492 uint32_t ctx_attribs[NUM_ATTRIBS];
1494 dri2_ctx = malloc(sizeof *dri2_ctx);
1496 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
1500 if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
1503 /* The EGL_EXT_create_context_robustness spec says:
1505 * "Add to the eglCreateContext context creation errors: [...]
1507 * * If the reset notification behavior of <share_context> and the
1508 * newly created context are different then an EGL_BAD_MATCH error is
1511 if (share_list && share_list->ResetNotificationStrategy !=
1512 dri2_ctx->base.ResetNotificationStrategy) {
1513 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1517 /* The EGL_KHR_create_context_no_error spec says:
1519 * "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
1520 * used to create <share_context> does not match the value of
1521 * EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
1523 if (share_list && share_list->NoError != dri2_ctx->base.NoError) {
1524 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1528 switch (dri2_ctx->base.ClientAPI) {
1529 case EGL_OPENGL_ES_API:
1530 switch (dri2_ctx->base.ClientMajorVersion) {
1532 api = __DRI_API_GLES;
1535 api = __DRI_API_GLES2;
1538 api = __DRI_API_GLES3;
1541 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1546 case EGL_OPENGL_API:
1547 if ((dri2_ctx->base.ClientMajorVersion >= 4
1548 || (dri2_ctx->base.ClientMajorVersion == 3
1549 && dri2_ctx->base.ClientMinorVersion >= 2))
1550 && dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)
1551 api = __DRI_API_OPENGL_CORE;
1552 else if (dri2_ctx->base.ClientMajorVersion == 3 &&
1553 dri2_ctx->base.ClientMinorVersion == 1)
1554 api = __DRI_API_OPENGL_CORE;
1556 api = __DRI_API_OPENGL;
1559 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1565 /* The config chosen here isn't necessarily
1566 * used for surfaces later.
1567 * A pixmap surface will use the single config.
1568 * This opportunity depends on disabling the
1569 * doubleBufferMode check in
1570 * src/mesa/main/context.c:check_compatible()
1572 if (dri2_config->dri_config[1][0])
1573 dri_config = dri2_config->dri_config[1][0];
1575 dri_config = dri2_config->dri_config[0][0];
1580 if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs,
1584 if (dri2_dpy->image_driver) {
1585 dri2_ctx->dri_context =
1586 dri2_dpy->image_driver->createContextAttribs(dri2_dpy->dri_screen,
1594 dri2_create_context_attribs_error(error);
1595 } else if (dri2_dpy->dri2) {
1596 if (dri2_dpy->dri2->base.version >= 3) {
1597 dri2_ctx->dri_context =
1598 dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen,
1606 dri2_create_context_attribs_error(error);
1608 dri2_ctx->dri_context =
1609 dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
1616 assert(dri2_dpy->swrast);
1617 if (dri2_dpy->swrast->base.version >= 3) {
1618 dri2_ctx->dri_context =
1619 dri2_dpy->swrast->createContextAttribs(dri2_dpy->dri_screen,
1627 dri2_create_context_attribs_error(error);
1629 dri2_ctx->dri_context =
1630 dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,
1638 if (!dri2_ctx->dri_context)
1641 return &dri2_ctx->base;
1649 * Called via eglDestroyContext(), drv->DestroyContext().
1652 dri2_destroy_context(_EGLDisplay *disp, _EGLContext *ctx)
1654 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1655 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1657 if (_eglPutContext(ctx)) {
1658 dri2_dpy->core->destroyContext(dri2_ctx->dri_context);
1666 dri2_init_surface(_EGLSurface *surf, _EGLDisplay *disp, EGLint type,
1667 _EGLConfig *conf, const EGLint *attrib_list,
1668 EGLBoolean enable_out_fence, void *native_surface)
1670 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1671 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1673 dri2_surf->out_fence_fd = -1;
1674 dri2_surf->enable_out_fence = false;
1675 if (dri2_dpy->fence && dri2_dpy->fence->base.version >= 2 &&
1676 dri2_dpy->fence->get_capabilities &&
1677 (dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen) &
1678 __DRI_FENCE_CAP_NATIVE_FD)) {
1679 dri2_surf->enable_out_fence = enable_out_fence;
1682 return _eglInitSurface(surf, disp, type, conf, attrib_list, native_surface);
1686 dri2_surface_set_out_fence_fd( _EGLSurface *surf, int fence_fd)
1688 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1690 if (dri2_surf->out_fence_fd >= 0)
1691 close(dri2_surf->out_fence_fd);
1693 dri2_surf->out_fence_fd = fence_fd;
1697 dri2_fini_surface(_EGLSurface *surf)
1699 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1701 dri2_surface_set_out_fence_fd(surf, -1);
1702 dri2_surf->enable_out_fence = false;
1706 dri2_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
1708 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1710 if (!_eglPutSurface(surf))
1713 return dri2_dpy->vtbl->destroy_surface(disp, surf);
1717 dri2_surf_update_fence_fd(_EGLContext *ctx,
1718 _EGLDisplay *disp, _EGLSurface *surf)
1720 __DRIcontext *dri_ctx = dri2_egl_context(ctx)->dri_context;
1721 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1722 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1726 if (!dri2_surf->enable_out_fence)
1729 fence = dri2_dpy->fence->create_fence_fd(dri_ctx, -1);
1731 fence_fd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
1733 dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, fence);
1735 dri2_surface_set_out_fence_fd(surf, fence_fd);
1739 dri2_create_drawable(struct dri2_egl_display *dri2_dpy,
1740 const __DRIconfig *config,
1741 struct dri2_egl_surface *dri2_surf,
1742 void *loaderPrivate)
1744 __DRIcreateNewDrawableFunc createNewDrawable;
1746 if (dri2_dpy->image_driver)
1747 createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
1748 else if (dri2_dpy->dri2)
1749 createNewDrawable = dri2_dpy->dri2->createNewDrawable;
1750 else if (dri2_dpy->swrast)
1751 createNewDrawable = dri2_dpy->swrast->createNewDrawable;
1753 return _eglError(EGL_BAD_ALLOC, "no createNewDrawable");
1755 dri2_surf->dri_drawable = createNewDrawable(dri2_dpy->dri_screen,
1756 config, loaderPrivate);
1757 if (dri2_surf->dri_drawable == NULL)
1758 return _eglError(EGL_BAD_ALLOC, "createNewDrawable");
1764 * Called via eglMakeCurrent(), drv->MakeCurrent().
1767 dri2_make_current(_EGLDisplay *disp, _EGLSurface *dsurf,
1768 _EGLSurface *rsurf, _EGLContext *ctx)
1770 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1771 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1772 _EGLDisplay *old_disp = NULL;
1773 struct dri2_egl_display *old_dri2_dpy = NULL;
1774 _EGLContext *old_ctx;
1775 _EGLSurface *old_dsurf, *old_rsurf;
1776 _EGLSurface *tmp_dsurf, *tmp_rsurf;
1777 __DRIdrawable *ddraw, *rdraw;
1779 EGLint egl_error = EGL_SUCCESS;
1782 return _eglError(EGL_NOT_INITIALIZED, "eglMakeCurrent");
1784 /* make new bindings, set the EGL error otherwise */
1785 if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))
1789 __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context;
1790 old_disp = old_ctx->Resource.Display;
1791 old_dri2_dpy = dri2_egl_display(old_disp);
1793 /* flush before context switch */
1797 dri2_surf_update_fence_fd(old_ctx, disp, old_dsurf);
1799 /* Disable shared buffer mode */
1800 if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1801 old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1802 old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, false);
1805 dri2_dpy->core->unbindContext(old_cctx);
1808 ddraw = (dsurf) ? dri2_dpy->vtbl->get_dri_drawable(dsurf) : NULL;
1809 rdraw = (rsurf) ? dri2_dpy->vtbl->get_dri_drawable(rsurf) : NULL;
1810 cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
1812 if (cctx || ddraw || rdraw) {
1813 if (!dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1814 _EGLContext *tmp_ctx;
1816 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1817 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1820 egl_error = EGL_BAD_MATCH;
1822 /* undo the previous _eglBindContext */
1823 _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &tmp_dsurf, &tmp_rsurf);
1824 assert(&dri2_ctx->base == ctx &&
1825 tmp_dsurf == dsurf &&
1826 tmp_rsurf == rsurf);
1828 _eglPutSurface(dsurf);
1829 _eglPutSurface(rsurf);
1830 _eglPutContext(ctx);
1832 _eglPutSurface(old_dsurf);
1833 _eglPutSurface(old_rsurf);
1834 _eglPutContext(old_ctx);
1836 ddraw = (old_dsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_dsurf) : NULL;
1837 rdraw = (old_rsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_rsurf) : NULL;
1838 cctx = (old_ctx) ? dri2_egl_context(old_ctx)->dri_context : NULL;
1840 /* undo the previous dri2_dpy->core->unbindContext */
1841 if (dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1842 if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1843 old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1844 old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, true);
1847 return _eglError(egl_error, "eglMakeCurrent");
1850 /* We cannot restore the same state as it was before calling
1851 * eglMakeCurrent() and the spec isn't clear about what to do. We
1852 * can prevent EGL from calling into the DRI driver with no DRI
1855 dsurf = rsurf = NULL;
1858 _eglBindContext(ctx, dsurf, rsurf, &tmp_ctx, &tmp_dsurf, &tmp_rsurf);
1859 assert(tmp_ctx == old_ctx && tmp_dsurf == old_dsurf &&
1860 tmp_rsurf == old_rsurf);
1862 _eglLog(_EGL_WARNING, "DRI2: failed to rebind the previous context");
1864 /* dri2_dpy->core->bindContext succeeded, so take a reference on the
1865 * dri2_dpy. This prevents dri2_dpy from being reinitialized when a
1866 * EGLDisplay is terminated and then initialized again while a
1867 * context is still bound. See dri2_intitialize() for a more in depth
1869 dri2_dpy->ref_count++;
1873 dri2_destroy_surface(disp, old_dsurf);
1874 dri2_destroy_surface(disp, old_rsurf);
1877 dri2_destroy_context(disp, old_ctx);
1878 dri2_display_release(old_disp);
1881 if (egl_error != EGL_SUCCESS)
1882 return _eglError(egl_error, "eglMakeCurrent");
1884 if (dsurf && _eglSurfaceHasMutableRenderBuffer(dsurf) &&
1885 dri2_dpy->vtbl->set_shared_buffer_mode) {
1886 /* Always update the shared buffer mode. This is obviously needed when
1887 * the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When
1888 * EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the
1889 * case where external non-EGL API may have changed window's shared
1890 * buffer mode since we last saw it.
1892 bool mode = (dsurf->ActiveRenderBuffer == EGL_SINGLE_BUFFER);
1893 dri2_dpy->vtbl->set_shared_buffer_mode(disp, dsurf, mode);
1900 dri2_surface_get_dri_drawable(_EGLSurface *surf)
1902 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1904 return dri2_surf->dri_drawable;
1908 * Called from eglGetProcAddress() via drv->GetProcAddress().
1911 dri2_get_proc_address(const char *procname)
1913 return _glapi_get_proc_address(procname);
1917 dri2_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
1918 void *native_window, const EGLint *attrib_list)
1920 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1921 return dri2_dpy->vtbl->create_window_surface(disp, conf, native_window,
1926 dri2_create_pixmap_surface(_EGLDisplay *disp, _EGLConfig *conf,
1927 void *native_pixmap, const EGLint *attrib_list)
1929 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1930 if (!dri2_dpy->vtbl->create_pixmap_surface)
1932 return dri2_dpy->vtbl->create_pixmap_surface(disp, conf, native_pixmap,
1937 dri2_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
1938 const EGLint *attrib_list)
1940 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1941 if (!dri2_dpy->vtbl->create_pbuffer_surface)
1943 return dri2_dpy->vtbl->create_pbuffer_surface(disp, conf, attrib_list);
1947 dri2_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
1949 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1950 if (!dri2_dpy->vtbl->swap_interval)
1952 return dri2_dpy->vtbl->swap_interval(disp, surf, interval);
1956 * Asks the client API to flush any rendering to the drawable so that we can
1957 * do our swapbuffers.
1960 dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw)
1962 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1963 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(draw);
1965 if (dri2_dpy->flush) {
1966 if (dri2_dpy->flush->base.version >= 4) {
1967 /* We know there's a current context because:
1969 * "If surface is not bound to the calling thread’s current
1970 * context, an EGL_BAD_SURFACE error is generated."
1972 _EGLContext *ctx = _eglGetCurrentContext();
1973 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1975 /* From the EGL 1.4 spec (page 52):
1977 * "The contents of ancillary buffers are always undefined
1978 * after calling eglSwapBuffers."
1980 dri2_dpy->flush->flush_with_flags(dri2_ctx->dri_context,
1982 __DRI2_FLUSH_DRAWABLE |
1983 __DRI2_FLUSH_INVALIDATE_ANCILLARY,
1984 __DRI2_THROTTLE_SWAPBUFFER);
1986 dri2_dpy->flush->flush(dri_drawable);
1992 dri2_swap_buffers(_EGLDisplay *disp, _EGLSurface *surf)
1994 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1995 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
1996 _EGLContext *ctx = _eglGetCurrentContext();
2000 dri2_surf_update_fence_fd(ctx, disp, surf);
2001 ret = dri2_dpy->vtbl->swap_buffers(disp, surf);
2003 /* SwapBuffers marks the end of the frame; reset the damage region for
2004 * use again next time.
2006 if (ret && dri2_dpy->buffer_damage &&
2007 dri2_dpy->buffer_damage->set_damage_region)
2008 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2014 dri2_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *surf,
2015 const EGLint *rects, EGLint n_rects)
2017 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2018 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2019 _EGLContext *ctx = _eglGetCurrentContext();
2023 dri2_surf_update_fence_fd(ctx, disp, surf);
2024 if (dri2_dpy->vtbl->swap_buffers_with_damage)
2025 ret = dri2_dpy->vtbl->swap_buffers_with_damage(disp, surf,
2028 ret = dri2_dpy->vtbl->swap_buffers(disp, surf);
2030 /* SwapBuffers marks the end of the frame; reset the damage region for
2031 * use again next time.
2033 if (ret && dri2_dpy->buffer_damage &&
2034 dri2_dpy->buffer_damage->set_damage_region)
2035 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2041 dri2_swap_buffers_region(_EGLDisplay *disp, _EGLSurface *surf,
2042 EGLint numRects, const EGLint *rects)
2044 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2045 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2048 if (!dri2_dpy->vtbl->swap_buffers_region)
2050 ret = dri2_dpy->vtbl->swap_buffers_region(disp, surf, numRects, rects);
2052 /* SwapBuffers marks the end of the frame; reset the damage region for
2053 * use again next time.
2055 if (ret && dri2_dpy->buffer_damage &&
2056 dri2_dpy->buffer_damage->set_damage_region)
2057 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2063 dri2_set_damage_region(_EGLDisplay *disp, _EGLSurface *surf,
2064 EGLint *rects, EGLint n_rects)
2066 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2067 __DRIdrawable *drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2069 if (!dri2_dpy->buffer_damage || !dri2_dpy->buffer_damage->set_damage_region)
2072 dri2_dpy->buffer_damage->set_damage_region(drawable, n_rects, rects);
2077 dri2_post_sub_buffer(_EGLDisplay *disp, _EGLSurface *surf,
2078 EGLint x, EGLint y, EGLint width, EGLint height)
2080 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2081 if (!dri2_dpy->vtbl->post_sub_buffer)
2083 return dri2_dpy->vtbl->post_sub_buffer(disp, surf, x, y, width, height);
2087 dri2_copy_buffers(_EGLDisplay *disp, _EGLSurface *surf, void *native_pixmap_target)
2089 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2090 if (!dri2_dpy->vtbl->copy_buffers)
2091 return _eglError(EGL_BAD_NATIVE_PIXMAP, "no support for native pixmaps");
2092 return dri2_dpy->vtbl->copy_buffers(disp, surf, native_pixmap_target);
2096 dri2_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surf)
2098 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2099 if (!dri2_dpy->vtbl->query_buffer_age)
2101 return dri2_dpy->vtbl->query_buffer_age(disp, surf);
2105 dri2_wait_client(_EGLDisplay *disp, _EGLContext *ctx)
2107 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2108 _EGLSurface *surf = ctx->DrawSurface;
2109 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2111 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
2112 * we need to copy fake to real here.*/
2114 if (dri2_dpy->flush != NULL)
2115 dri2_dpy->flush->flush(dri_drawable);
2121 dri2_wait_native(EGLint engine)
2123 if (engine != EGL_CORE_NATIVE_ENGINE)
2124 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
2131 dri2_bind_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
2133 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2134 struct dri2_egl_context *dri2_ctx;
2136 GLint format, target;
2137 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2139 ctx = _eglGetCurrentContext();
2140 dri2_ctx = dri2_egl_context(ctx);
2142 if (!_eglBindTexImage(disp, surf, buffer))
2145 switch (surf->TextureFormat) {
2146 case EGL_TEXTURE_RGB:
2147 format = __DRI_TEXTURE_FORMAT_RGB;
2149 case EGL_TEXTURE_RGBA:
2150 format = __DRI_TEXTURE_FORMAT_RGBA;
2153 assert(!"Unexpected texture format in dri2_bind_tex_image()");
2154 format = __DRI_TEXTURE_FORMAT_RGBA;
2157 switch (surf->TextureTarget) {
2158 case EGL_TEXTURE_2D:
2159 target = GL_TEXTURE_2D;
2162 target = GL_TEXTURE_2D;
2163 assert(!"Unexpected texture target in dri2_bind_tex_image()");
2166 dri2_dpy->tex_buffer->setTexBuffer2(dri2_ctx->dri_context,
2174 dri2_release_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
2176 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2177 struct dri2_egl_context *dri2_ctx;
2180 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2182 ctx = _eglGetCurrentContext();
2183 dri2_ctx = dri2_egl_context(ctx);
2185 if (!_eglReleaseTexImage(disp, surf, buffer))
2188 switch (surf->TextureTarget) {
2189 case EGL_TEXTURE_2D:
2190 target = GL_TEXTURE_2D;
2193 assert(!"missing texture target");
2196 if (dri2_dpy->tex_buffer->base.version >= 3 &&
2197 dri2_dpy->tex_buffer->releaseTexBuffer != NULL) {
2198 dri2_dpy->tex_buffer->releaseTexBuffer(dri2_ctx->dri_context,
2199 target, dri_drawable);
2206 dri2_create_image(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
2207 EGLClientBuffer buffer, const EGLint *attr_list)
2209 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2210 return dri2_dpy->vtbl->create_image(disp, ctx, target, buffer,
2215 dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image)
2217 struct dri2_egl_image *dri2_img;
2219 if (dri_image == NULL) {
2220 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
2224 dri2_img = malloc(sizeof *dri2_img);
2226 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
2230 _eglInitImage(&dri2_img->base, disp);
2232 dri2_img->dri_image = dri_image;
2234 return &dri2_img->base;
2238 * Translate a DRI Image extension error code into an EGL error code.
2241 egl_error_from_dri_image_error(int dri_error)
2243 switch (dri_error) {
2244 case __DRI_IMAGE_ERROR_SUCCESS:
2246 case __DRI_IMAGE_ERROR_BAD_ALLOC:
2247 return EGL_BAD_ALLOC;
2248 case __DRI_IMAGE_ERROR_BAD_MATCH:
2249 return EGL_BAD_MATCH;
2250 case __DRI_IMAGE_ERROR_BAD_PARAMETER:
2251 return EGL_BAD_PARAMETER;
2252 case __DRI_IMAGE_ERROR_BAD_ACCESS:
2253 return EGL_BAD_ACCESS;
2255 assert(!"unknown dri_error code");
2256 return EGL_BAD_ALLOC;
2261 dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
2262 EGLClientBuffer buffer,
2263 const EGLint *attr_list)
2265 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2266 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2267 GLuint renderbuffer = (GLuint) (uintptr_t) buffer;
2268 __DRIimage *dri_image;
2270 if (renderbuffer == 0) {
2271 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2272 return EGL_NO_IMAGE_KHR;
2275 if (!disp->Extensions.KHR_gl_renderbuffer_image) {
2276 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2277 return EGL_NO_IMAGE_KHR;
2280 if (dri2_dpy->image->base.version >= 17 &&
2281 dri2_dpy->image->createImageFromRenderbuffer2) {
2282 unsigned error = ~0;
2284 dri_image = dri2_dpy->image->createImageFromRenderbuffer2(
2285 dri2_ctx->dri_context, renderbuffer, NULL, &error);
2287 assert(!!dri_image == (error == __DRI_IMAGE_ERROR_SUCCESS));
2290 _eglError(egl_error_from_dri_image_error(error), "dri2_create_image_khr");
2291 return EGL_NO_IMAGE_KHR;
2294 dri_image = dri2_dpy->image->createImageFromRenderbuffer(
2295 dri2_ctx->dri_context, renderbuffer, NULL);
2297 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2298 return EGL_NO_IMAGE_KHR;
2302 return dri2_create_image_from_dri(disp, dri_image);
2305 #ifdef HAVE_WAYLAND_PLATFORM
2307 /* This structure describes how a wl_buffer maps to one or more
2308 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
2309 * offsets and strides of the planes in the buffer. This table maps a
2310 * wl_drm format code to a description of the planes in the buffer
2311 * that lets us create a __DRIimage for each of the planes. */
2313 static const struct wl_drm_components_descriptor {
2314 uint32_t dri_components;
2317 } wl_drm_components[] = {
2318 { __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 },
2319 { __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 },
2320 { __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 },
2321 { __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 },
2322 { __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 },
2326 dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx,
2327 EGLClientBuffer _buffer,
2328 const EGLint *attr_list)
2330 struct wl_drm_buffer *buffer;
2331 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2332 const struct wl_drm_components_descriptor *f;
2333 __DRIimage *dri_image;
2334 _EGLImageAttribs attrs;
2337 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm,
2338 (struct wl_resource *) _buffer);
2342 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2345 plane = attrs.PlaneWL;
2346 f = buffer->driver_format;
2347 if (plane < 0 || plane >= f->nplanes) {
2348 _eglError(EGL_BAD_PARAMETER,
2349 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
2353 dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL);
2354 if (dri_image == NULL && plane == 0)
2355 dri_image = dri2_dpy->image->dupImage(buffer->driver_buffer, NULL);
2356 if (dri_image == NULL) {
2357 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
2361 return dri2_create_image_from_dri(disp, dri_image);
2366 dri2_get_sync_values_chromium(_EGLDisplay *disp, _EGLSurface *surf,
2367 EGLuint64KHR *ust, EGLuint64KHR *msc,
2370 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2371 if (!dri2_dpy->vtbl->get_sync_values)
2373 return dri2_dpy->vtbl->get_sync_values(disp, surf, ust, msc, sbc);
2377 * Set the error code after a call to
2378 * dri2_egl_image::dri_image::createImageFromTexture.
2381 dri2_create_image_khr_texture_error(int dri_error)
2383 EGLint egl_error = egl_error_from_dri_image_error(dri_error);
2385 if (egl_error != EGL_SUCCESS)
2386 _eglError(egl_error, "dri2_create_image_khr_texture");
2390 dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx,
2392 EGLClientBuffer buffer,
2393 const EGLint *attr_list)
2395 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2396 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2397 struct dri2_egl_image *dri2_img;
2398 GLuint texture = (GLuint) (uintptr_t) buffer;
2399 _EGLImageAttribs attrs;
2405 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2406 return EGL_NO_IMAGE_KHR;
2409 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2410 return EGL_NO_IMAGE_KHR;
2413 case EGL_GL_TEXTURE_2D_KHR:
2414 if (!disp->Extensions.KHR_gl_texture_2D_image) {
2415 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2416 return EGL_NO_IMAGE_KHR;
2419 gl_target = GL_TEXTURE_2D;
2421 case EGL_GL_TEXTURE_3D_KHR:
2422 if (!disp->Extensions.KHR_gl_texture_3D_image) {
2423 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2424 return EGL_NO_IMAGE_KHR;
2427 depth = attrs.GLTextureZOffset;
2428 gl_target = GL_TEXTURE_3D;
2430 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
2431 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
2432 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
2433 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
2434 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
2435 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
2436 if (!disp->Extensions.KHR_gl_texture_cubemap_image) {
2437 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2438 return EGL_NO_IMAGE_KHR;
2441 depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
2442 gl_target = GL_TEXTURE_CUBE_MAP;
2445 unreachable("Unexpected target in dri2_create_image_khr_texture()");
2446 return EGL_NO_IMAGE_KHR;
2449 dri2_img = malloc(sizeof *dri2_img);
2451 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2452 return EGL_NO_IMAGE_KHR;
2455 _eglInitImage(&dri2_img->base, disp);
2457 dri2_img->dri_image =
2458 dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context,
2462 attrs.GLTextureLevel,
2465 dri2_create_image_khr_texture_error(error);
2467 if (!dri2_img->dri_image) {
2469 return EGL_NO_IMAGE_KHR;
2471 return &dri2_img->base;
2475 dri2_query_surface(_EGLDisplay *disp, _EGLSurface *surf,
2476 EGLint attribute, EGLint *value)
2478 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2479 if (!dri2_dpy->vtbl->query_surface)
2480 return _eglQuerySurface(disp, surf, attribute, value);
2481 return dri2_dpy->vtbl->query_surface(disp, surf, attribute, value);
2484 static struct wl_buffer*
2485 dri2_create_wayland_buffer_from_image(_EGLDisplay *disp, _EGLImage *img)
2487 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2488 if (!dri2_dpy->vtbl->create_wayland_buffer_from_image)
2490 return dri2_dpy->vtbl->create_wayland_buffer_from_image(disp, img);
2495 dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
2496 EGLClientBuffer buffer, const EGLint *attr_list)
2498 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2499 EGLint format, name, pitch;
2500 _EGLImageAttribs attrs;
2501 __DRIimage *dri_image;
2503 name = (EGLint) (uintptr_t) buffer;
2505 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2508 if (attrs.Width <= 0 || attrs.Height <= 0 ||
2509 attrs.DRMBufferStrideMESA <= 0) {
2510 _eglError(EGL_BAD_PARAMETER,
2511 "bad width, height or stride");
2515 switch (attrs.DRMBufferFormatMESA) {
2516 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2517 format = __DRI_IMAGE_FORMAT_ARGB8888;
2518 pitch = attrs.DRMBufferStrideMESA;
2521 _eglError(EGL_BAD_PARAMETER,
2522 "dri2_create_image_khr: unsupported pixmap depth");
2527 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
2535 return dri2_create_image_from_dri(disp, dri_image);
2539 dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)
2544 * "Required attributes and their values are as follows:
2546 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
2548 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
2549 * by drm_fourcc.h and used as the pixel_format parameter of the
2550 * drm_mode_fb_cmd2 ioctl."
2554 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2555 * incomplete, EGL_BAD_PARAMETER is generated."
2557 if (attrs->Width <= 0 || attrs->Height <= 0 ||
2558 !attrs->DMABufFourCC.IsPresent)
2559 return _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");
2564 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
2565 * specified for a plane's pitch or offset isn't supported by EGL,
2566 * EGL_BAD_ACCESS is generated."
2568 for (unsigned i = 0; i < ARRAY_SIZE(attrs->DMABufPlanePitches); ++i) {
2569 if (attrs->DMABufPlanePitches[i].IsPresent &&
2570 attrs->DMABufPlanePitches[i].Value <= 0)
2571 return _eglError(EGL_BAD_ACCESS, "invalid pitch");
2575 * If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2576 * attribute values may be given.
2578 * This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2579 * EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2581 for (unsigned i = 0; i < DMA_BUF_MAX_PLANES; ++i) {
2582 if (attrs->DMABufPlaneModifiersLo[i].IsPresent !=
2583 attrs->DMABufPlaneModifiersHi[i].IsPresent)
2584 return _eglError(EGL_BAD_PARAMETER, "modifier attribute lo or hi missing");
2587 /* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2588 * mandate it, we only accept the same modifier across all planes. */
2589 for (unsigned i = 1; i < DMA_BUF_MAX_PLANES; ++i) {
2590 if (attrs->DMABufPlaneFds[i].IsPresent) {
2591 if ((attrs->DMABufPlaneModifiersLo[0].IsPresent !=
2592 attrs->DMABufPlaneModifiersLo[i].IsPresent) ||
2593 (attrs->DMABufPlaneModifiersLo[0].Value !=
2594 attrs->DMABufPlaneModifiersLo[i].Value) ||
2595 (attrs->DMABufPlaneModifiersHi[0].Value !=
2596 attrs->DMABufPlaneModifiersHi[i].Value))
2597 return _eglError(EGL_BAD_PARAMETER, "modifier attributes not equal");
2604 /* Returns the total number of planes for the format or zero if it isn't a
2605 * valid fourcc format.
2608 dri2_num_fourcc_format_planes(EGLint format)
2612 case DRM_FORMAT_RG88:
2613 case DRM_FORMAT_GR88:
2614 case DRM_FORMAT_R16:
2615 case DRM_FORMAT_GR1616:
2616 case DRM_FORMAT_RGB332:
2617 case DRM_FORMAT_BGR233:
2618 case DRM_FORMAT_XRGB4444:
2619 case DRM_FORMAT_XBGR4444:
2620 case DRM_FORMAT_RGBX4444:
2621 case DRM_FORMAT_BGRX4444:
2622 case DRM_FORMAT_ARGB4444:
2623 case DRM_FORMAT_ABGR4444:
2624 case DRM_FORMAT_RGBA4444:
2625 case DRM_FORMAT_BGRA4444:
2626 case DRM_FORMAT_XRGB1555:
2627 case DRM_FORMAT_XBGR1555:
2628 case DRM_FORMAT_RGBX5551:
2629 case DRM_FORMAT_BGRX5551:
2630 case DRM_FORMAT_ARGB1555:
2631 case DRM_FORMAT_ABGR1555:
2632 case DRM_FORMAT_RGBA5551:
2633 case DRM_FORMAT_BGRA5551:
2634 case DRM_FORMAT_RGB565:
2635 case DRM_FORMAT_BGR565:
2636 case DRM_FORMAT_RGB888:
2637 case DRM_FORMAT_BGR888:
2638 case DRM_FORMAT_XRGB8888:
2639 case DRM_FORMAT_XBGR8888:
2640 case DRM_FORMAT_RGBX8888:
2641 case DRM_FORMAT_BGRX8888:
2642 case DRM_FORMAT_ARGB8888:
2643 case DRM_FORMAT_ABGR8888:
2644 case DRM_FORMAT_RGBA8888:
2645 case DRM_FORMAT_BGRA8888:
2646 case DRM_FORMAT_XRGB2101010:
2647 case DRM_FORMAT_XBGR2101010:
2648 case DRM_FORMAT_RGBX1010102:
2649 case DRM_FORMAT_BGRX1010102:
2650 case DRM_FORMAT_ARGB2101010:
2651 case DRM_FORMAT_ABGR2101010:
2652 case DRM_FORMAT_RGBA1010102:
2653 case DRM_FORMAT_BGRA1010102:
2654 case DRM_FORMAT_XBGR16161616F:
2655 case DRM_FORMAT_ABGR16161616F:
2656 case DRM_FORMAT_YUYV:
2657 case DRM_FORMAT_YVYU:
2658 case DRM_FORMAT_UYVY:
2659 case DRM_FORMAT_VYUY:
2660 case DRM_FORMAT_AYUV:
2661 case DRM_FORMAT_XYUV8888:
2664 case DRM_FORMAT_NV12:
2665 case DRM_FORMAT_NV21:
2666 case DRM_FORMAT_NV16:
2667 case DRM_FORMAT_NV61:
2668 case DRM_FORMAT_P010:
2669 case DRM_FORMAT_P012:
2670 case DRM_FORMAT_P016:
2673 case DRM_FORMAT_YUV410:
2674 case DRM_FORMAT_YVU410:
2675 case DRM_FORMAT_YUV411:
2676 case DRM_FORMAT_YVU411:
2677 case DRM_FORMAT_YUV420:
2678 case DRM_FORMAT_YVU420:
2679 case DRM_FORMAT_YUV422:
2680 case DRM_FORMAT_YVU422:
2681 case DRM_FORMAT_YUV444:
2682 case DRM_FORMAT_YVU444:
2690 /* Returns the total number of file descriptors. Zero indicates an error. */
2692 dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)
2694 unsigned plane_n = dri2_num_fourcc_format_planes(attrs->DMABufFourCC.Value);
2696 _eglError(EGL_BAD_MATCH, "unknown drm fourcc format");
2700 for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; i++) {
2702 * The modifiers extension spec says:
2704 * "Modifiers may modify any attribute of a buffer import, including
2705 * but not limited to adding extra planes to a format which
2706 * otherwise does not have those planes. As an example, a modifier
2707 * may add a plane for an external compression buffer to a
2708 * single-plane format. The exact meaning and effect of any
2709 * modifier is canonically defined by drm_fourcc.h, not as part of
2712 if (attrs->DMABufPlaneModifiersLo[i].IsPresent &&
2713 attrs->DMABufPlaneModifiersHi[i].IsPresent) {
2721 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2722 * incomplete, EGL_BAD_PARAMETER is generated."
2724 for (unsigned i = 0; i < plane_n; ++i) {
2725 if (!attrs->DMABufPlaneFds[i].IsPresent ||
2726 !attrs->DMABufPlaneOffsets[i].IsPresent ||
2727 !attrs->DMABufPlanePitches[i].IsPresent) {
2728 _eglError(EGL_BAD_PARAMETER, "plane attribute(s) missing");
2734 * The spec also says:
2736 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2737 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2738 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2739 * or EGL_DMA_BUF_PLANE3_* attributes are specified."
2741 for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; ++i) {
2742 if (attrs->DMABufPlaneFds[i].IsPresent ||
2743 attrs->DMABufPlaneOffsets[i].IsPresent ||
2744 attrs->DMABufPlanePitches[i].IsPresent) {
2745 _eglError(EGL_BAD_ATTRIBUTE, "too many plane attributes");
2754 dri2_query_dma_buf_formats(_EGLDisplay *disp, EGLint max,
2755 EGLint *formats, EGLint *count)
2757 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2758 if (max < 0 || (max > 0 && formats == NULL))
2759 return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
2761 if (dri2_dpy->image->base.version < 15 ||
2762 dri2_dpy->image->queryDmaBufFormats == NULL)
2765 if (!dri2_dpy->image->queryDmaBufFormats(dri2_dpy->dri_screen, max,
2770 /* Assert that all of the formats returned are actually fourcc formats.
2771 * Some day, if we want the internal interface function to be able to
2772 * return the fake fourcc formats defined in dri_interface.h, we'll have
2773 * to do something more clever here to pair the list down to just real
2774 * fourcc formats so that we don't leak the fake internal ones.
2776 for (int i = 0; i < *count; i++) {
2777 assert(dri2_num_fourcc_format_planes(formats[i]) > 0);
2785 dri2_query_dma_buf_modifiers(_EGLDisplay *disp, EGLint format,
2786 EGLint max, EGLuint64KHR *modifiers,
2787 EGLBoolean *external_only, EGLint *count)
2789 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2791 if (dri2_num_fourcc_format_planes(format) == 0)
2792 return _eglError(EGL_BAD_PARAMETER, "invalid fourcc format");
2795 return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
2797 if (max > 0 && modifiers == NULL)
2798 return _eglError(EGL_BAD_PARAMETER, "invalid modifiers array");
2800 if (dri2_dpy->image->base.version < 15 ||
2801 dri2_dpy->image->queryDmaBufModifiers == NULL)
2804 if (dri2_dpy->image->queryDmaBufModifiers(dri2_dpy->dri_screen, format,
2806 (unsigned int *) external_only,
2808 return _eglError(EGL_BAD_PARAMETER, "invalid format");
2816 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2817 * EGL will take a reference to the dma_buf(s) which it will release at any
2818 * time while the EGLDisplay is initialized. It is the responsibility of the
2819 * application to close the dma_buf file descriptors."
2821 * Therefore we must never close or otherwise modify the file descriptors.
2824 dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
2825 EGLClientBuffer buffer, const EGLint *attr_list)
2827 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2829 _EGLImageAttribs attrs;
2830 __DRIimage *dri_image;
2832 int fds[DMA_BUF_MAX_PLANES];
2833 int pitches[DMA_BUF_MAX_PLANES];
2834 int offsets[DMA_BUF_MAX_PLANES];
2836 bool has_modifier = false;
2842 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2843 * error EGL_BAD_PARAMETER is generated."
2845 if (buffer != NULL) {
2846 _eglError(EGL_BAD_PARAMETER, "buffer not NULL");
2850 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2853 if (!dri2_check_dma_buf_attribs(&attrs))
2856 num_fds = dri2_check_dma_buf_format(&attrs);
2860 for (unsigned i = 0; i < num_fds; ++i) {
2861 fds[i] = attrs.DMABufPlaneFds[i].Value;
2862 pitches[i] = attrs.DMABufPlanePitches[i].Value;
2863 offsets[i] = attrs.DMABufPlaneOffsets[i].Value;
2866 /* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2867 * will be present in attrs.DMABufPlaneModifiersLo[0] and
2868 * attrs.DMABufPlaneModifiersHi[0] */
2869 if (attrs.DMABufPlaneModifiersLo[0].IsPresent) {
2870 modifier = combine_u32_into_u64(attrs.DMABufPlaneModifiersHi[0].Value,
2871 attrs.DMABufPlaneModifiersLo[0].Value);
2872 has_modifier = true;
2875 if (attrs.ProtectedContent) {
2876 if (dri2_dpy->image->base.version < 18 ||
2877 dri2_dpy->image->createImageFromDmaBufs3 == NULL) {
2878 _eglError(EGL_BAD_MATCH, "unsupported protected_content attribute");
2879 return EGL_NO_IMAGE_KHR;
2882 modifier = DRM_FORMAT_MOD_INVALID;
2885 dri2_dpy->image->createImageFromDmaBufs3(dri2_dpy->dri_screen,
2886 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2887 modifier, fds, num_fds, pitches, offsets,
2888 attrs.DMABufYuvColorSpaceHint.Value,
2889 attrs.DMABufSampleRangeHint.Value,
2890 attrs.DMABufChromaHorizontalSiting.Value,
2891 attrs.DMABufChromaVerticalSiting.Value,
2892 attrs.ProtectedContent ? __DRI_IMAGE_PROTECTED_CONTENT_FLAG : 0,
2896 else if (has_modifier) {
2897 if (dri2_dpy->image->base.version < 15 ||
2898 dri2_dpy->image->createImageFromDmaBufs2 == NULL) {
2899 _eglError(EGL_BAD_MATCH, "unsupported dma_buf format modifier");
2900 return EGL_NO_IMAGE_KHR;
2903 dri2_dpy->image->createImageFromDmaBufs2(dri2_dpy->dri_screen,
2904 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2905 modifier, fds, num_fds, pitches, offsets,
2906 attrs.DMABufYuvColorSpaceHint.Value,
2907 attrs.DMABufSampleRangeHint.Value,
2908 attrs.DMABufChromaHorizontalSiting.Value,
2909 attrs.DMABufChromaVerticalSiting.Value,
2915 dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen,
2916 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2917 fds, num_fds, pitches, offsets,
2918 attrs.DMABufYuvColorSpaceHint.Value,
2919 attrs.DMABufSampleRangeHint.Value,
2920 attrs.DMABufChromaHorizontalSiting.Value,
2921 attrs.DMABufChromaVerticalSiting.Value,
2925 dri2_create_image_khr_texture_error(error);
2928 return EGL_NO_IMAGE_KHR;
2930 res = dri2_create_image_from_dri(disp, dri_image);
2935 dri2_create_drm_image_mesa(_EGLDisplay *disp, const EGLint *attr_list)
2937 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2938 struct dri2_egl_image *dri2_img;
2939 _EGLImageAttribs attrs;
2940 unsigned int dri_use, valid_mask;
2944 _eglError(EGL_BAD_PARAMETER, __func__);
2945 return EGL_NO_IMAGE_KHR;
2948 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2949 return EGL_NO_IMAGE_KHR;
2951 if (attrs.Width <= 0 || attrs.Height <= 0) {
2952 _eglError(EGL_BAD_PARAMETER, __func__);
2953 return EGL_NO_IMAGE_KHR;
2956 switch (attrs.DRMBufferFormatMESA) {
2957 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2958 format = __DRI_IMAGE_FORMAT_ARGB8888;
2961 _eglError(EGL_BAD_PARAMETER, __func__);
2962 return EGL_NO_IMAGE_KHR;
2966 EGL_DRM_BUFFER_USE_SCANOUT_MESA |
2967 EGL_DRM_BUFFER_USE_SHARE_MESA |
2968 EGL_DRM_BUFFER_USE_CURSOR_MESA;
2969 if (attrs.DRMBufferUseMESA & ~valid_mask) {
2970 _eglError(EGL_BAD_PARAMETER, __func__);
2971 return EGL_NO_IMAGE_KHR;
2975 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
2976 dri_use |= __DRI_IMAGE_USE_SHARE;
2977 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
2978 dri_use |= __DRI_IMAGE_USE_SCANOUT;
2979 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA)
2980 dri_use |= __DRI_IMAGE_USE_CURSOR;
2982 dri2_img = malloc(sizeof *dri2_img);
2984 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2985 return EGL_NO_IMAGE_KHR;
2988 _eglInitImage(&dri2_img->base, disp);
2990 dri2_img->dri_image =
2991 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
2992 attrs.Width, attrs.Height,
2993 format, dri_use, dri2_img);
2994 if (dri2_img->dri_image == NULL) {
2996 _eglError(EGL_BAD_ALLOC, "dri2_create_drm_image_mesa");
2997 return EGL_NO_IMAGE_KHR;
3000 return &dri2_img->base;
3004 dri2_export_drm_image_mesa(_EGLDisplay *disp, _EGLImage *img,
3005 EGLint *name, EGLint *handle, EGLint *stride)
3007 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3008 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3010 if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image,
3011 __DRI_IMAGE_ATTRIB_NAME, name))
3012 return _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa");
3015 dri2_dpy->image->queryImage(dri2_img->dri_image,
3016 __DRI_IMAGE_ATTRIB_HANDLE, handle);
3019 dri2_dpy->image->queryImage(dri2_img->dri_image,
3020 __DRI_IMAGE_ATTRIB_STRIDE, stride);
3026 * Checks if we can support EGL_MESA_image_dma_buf_export on this image.
3028 * The spec provides a boolean return for the driver to reject exporting for
3029 * basically any reason, but doesn't specify any particular error cases. For
3030 * now, we just fail if we don't have a DRM fourcc for the format.
3033 dri2_can_export_dma_buf_image(_EGLDisplay *disp, _EGLImage *img)
3035 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3036 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3039 if (!dri2_dpy->image->queryImage(dri2_img->dri_image,
3040 __DRI_IMAGE_ATTRIB_FOURCC, &fourcc)) {
3048 dri2_export_dma_buf_image_query_mesa(_EGLDisplay *disp, _EGLImage *img,
3049 EGLint *fourcc, EGLint *nplanes,
3050 EGLuint64KHR *modifiers)
3052 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3053 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3056 if (!dri2_can_export_dma_buf_image(disp, img))
3059 dri2_dpy->image->queryImage(dri2_img->dri_image,
3060 __DRI_IMAGE_ATTRIB_NUM_PLANES, &num_planes);
3062 *nplanes = num_planes;
3065 dri2_dpy->image->queryImage(dri2_img->dri_image,
3066 __DRI_IMAGE_ATTRIB_FOURCC, fourcc);
3070 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
3073 query = dri2_dpy->image->queryImage(dri2_img->dri_image,
3074 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
3076 query &= dri2_dpy->image->queryImage(dri2_img->dri_image,
3077 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
3080 modifier = combine_u32_into_u64 (mod_hi, mod_lo);
3082 for (int i = 0; i < num_planes; i++)
3083 modifiers[i] = modifier;
3090 dri2_export_dma_buf_image_mesa(_EGLDisplay *disp, _EGLImage *img,
3091 int *fds, EGLint *strides, EGLint *offsets)
3093 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3094 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3097 if (!dri2_can_export_dma_buf_image(disp, img))
3100 /* EGL_MESA_image_dma_buf_export spec says:
3101 * "If the number of fds is less than the number of planes, then
3102 * subsequent fd slots should contain -1."
3105 /* Query nplanes so that we know how big the given array is. */
3106 dri2_dpy->image->queryImage(dri2_img->dri_image,
3107 __DRI_IMAGE_ATTRIB_NUM_PLANES, &nplanes);
3108 memset(fds, -1, nplanes * sizeof(int));
3111 /* rework later to provide multiple fds/strides/offsets */
3113 dri2_dpy->image->queryImage(dri2_img->dri_image,
3114 __DRI_IMAGE_ATTRIB_FD, fds);
3117 dri2_dpy->image->queryImage(dri2_img->dri_image,
3118 __DRI_IMAGE_ATTRIB_STRIDE, strides);
3122 bool ret = dri2_dpy->image->queryImage(dri2_img->dri_image,
3123 __DRI_IMAGE_ATTRIB_OFFSET, &img_offset);
3125 offsets[0] = img_offset;
3136 dri2_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
3137 EGLClientBuffer buffer, const EGLint *attr_list)
3140 case EGL_GL_TEXTURE_2D_KHR:
3141 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
3142 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
3143 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
3144 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
3145 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
3146 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
3147 case EGL_GL_TEXTURE_3D_KHR:
3148 return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list);
3149 case EGL_GL_RENDERBUFFER_KHR:
3150 return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
3152 case EGL_DRM_BUFFER_MESA:
3153 return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list);
3154 case EGL_LINUX_DMA_BUF_EXT:
3155 return dri2_create_image_dma_buf(disp, ctx, buffer, attr_list);
3157 #ifdef HAVE_WAYLAND_PLATFORM
3158 case EGL_WAYLAND_BUFFER_WL:
3159 return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list);
3162 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
3163 return EGL_NO_IMAGE_KHR;
3168 dri2_destroy_image_khr(_EGLDisplay *disp, _EGLImage *image)
3170 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3171 struct dri2_egl_image *dri2_img = dri2_egl_image(image);
3173 dri2_dpy->image->destroyImage(dri2_img->dri_image);
3179 #ifdef HAVE_WAYLAND_PLATFORM
3182 dri2_wl_reference_buffer(void *user_data, uint32_t name, int fd,
3183 struct wl_drm_buffer *buffer)
3185 _EGLDisplay *disp = user_data;
3186 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3188 int dri_components = 0;
3191 img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen,
3200 img = dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
3212 dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components);
3214 buffer->driver_format = NULL;
3215 for (int i = 0; i < ARRAY_SIZE(wl_drm_components); i++)
3216 if (wl_drm_components[i].dri_components == dri_components)
3217 buffer->driver_format = &wl_drm_components[i];
3219 if (buffer->driver_format == NULL)
3220 dri2_dpy->image->destroyImage(img);
3222 buffer->driver_buffer = img;
3226 dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer)
3228 _EGLDisplay *disp = user_data;
3229 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3231 dri2_dpy->image->destroyImage(buffer->driver_buffer);
3235 dri2_bind_wayland_display_wl(_EGLDisplay *disp, struct wl_display *wl_dpy)
3237 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3238 const struct wayland_drm_callbacks wl_drm_callbacks = {
3239 .authenticate = (int(*)(void *, uint32_t)) dri2_dpy->vtbl->authenticate,
3240 .reference_buffer = dri2_wl_reference_buffer,
3241 .release_buffer = dri2_wl_release_buffer,
3242 .is_format_supported = dri2_wl_is_format_supported
3247 if (dri2_dpy->wl_server_drm)
3250 if (drmGetCap(dri2_dpy->fd, DRM_CAP_PRIME, &cap) == 0 &&
3251 cap == (DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT) &&
3252 dri2_dpy->image->base.version >= 7 &&
3253 dri2_dpy->image->createImageFromFds != NULL)
3254 flags |= WAYLAND_DRM_PRIME;
3256 dri2_dpy->wl_server_drm =
3257 wayland_drm_init(wl_dpy, dri2_dpy->device_name,
3258 &wl_drm_callbacks, disp, flags);
3260 if (!dri2_dpy->wl_server_drm)
3263 #ifdef HAVE_DRM_PLATFORM
3264 /* We have to share the wl_drm instance with gbm, so gbm can convert
3265 * wl_buffers to gbm bos. */
3266 if (dri2_dpy->gbm_dri)
3267 dri2_dpy->gbm_dri->wl_drm = dri2_dpy->wl_server_drm;
3274 dri2_unbind_wayland_display_wl(_EGLDisplay *disp, struct wl_display *wl_dpy)
3276 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3278 if (!dri2_dpy->wl_server_drm)
3281 wayland_drm_uninit(dri2_dpy->wl_server_drm);
3282 dri2_dpy->wl_server_drm = NULL;
3288 dri2_query_wayland_buffer_wl(_EGLDisplay *disp, struct wl_resource *buffer_resource,
3289 EGLint attribute, EGLint *value)
3291 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3292 struct wl_drm_buffer *buffer;
3293 const struct wl_drm_components_descriptor *format;
3295 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm, buffer_resource);
3299 format = buffer->driver_format;
3300 switch (attribute) {
3301 case EGL_TEXTURE_FORMAT:
3302 *value = format->components;
3305 *value = buffer->width;
3308 *value = buffer->height;
3317 dri2_egl_ref_sync(struct dri2_egl_sync *sync)
3319 p_atomic_inc(&sync->refcount);
3323 dri2_egl_unref_sync(struct dri2_egl_display *dri2_dpy,
3324 struct dri2_egl_sync *dri2_sync)
3326 if (p_atomic_dec_zero(&dri2_sync->refcount)) {
3327 switch (dri2_sync->base.Type) {
3328 case EGL_SYNC_REUSABLE_KHR:
3329 cnd_destroy(&dri2_sync->cond);
3331 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3332 if (dri2_sync->base.SyncFd != EGL_NO_NATIVE_FENCE_FD_ANDROID)
3333 close(dri2_sync->base.SyncFd);
3339 if (dri2_sync->fence)
3340 dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, dri2_sync->fence);
3347 dri2_create_sync(_EGLDisplay *disp, EGLenum type, const EGLAttrib *attrib_list)
3349 _EGLContext *ctx = _eglGetCurrentContext();
3350 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3351 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3352 struct dri2_egl_sync *dri2_sync;
3354 pthread_condattr_t attr;
3356 dri2_sync = calloc(1, sizeof(struct dri2_egl_sync));
3358 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3362 if (!_eglInitSync(&dri2_sync->base, disp, type, attrib_list)) {
3368 case EGL_SYNC_FENCE_KHR:
3369 dri2_sync->fence = dri2_dpy->fence->create_fence(dri2_ctx->dri_context);
3370 if (!dri2_sync->fence) {
3371 /* Why did it fail? DRI doesn't return an error code, so we emit
3372 * a generic EGL error that doesn't communicate user error.
3374 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3380 case EGL_SYNC_CL_EVENT_KHR:
3381 dri2_sync->fence = dri2_dpy->fence->get_fence_from_cl_event(
3382 dri2_dpy->dri_screen,
3383 dri2_sync->base.CLEvent);
3384 /* this can only happen if the cl_event passed in is invalid. */
3385 if (!dri2_sync->fence) {
3386 _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3391 /* the initial status must be "signaled" if the cl_event is signaled */
3392 if (dri2_dpy->fence->client_wait_sync(dri2_ctx->dri_context,
3393 dri2_sync->fence, 0, 0))
3394 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3397 case EGL_SYNC_REUSABLE_KHR:
3398 /* intialize attr */
3399 ret = pthread_condattr_init(&attr);
3402 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3407 /* change clock attribute to CLOCK_MONOTONIC */
3408 ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
3411 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3416 ret = pthread_cond_init(&dri2_sync->cond, &attr);
3419 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3424 /* initial status of reusable sync must be "unsignaled" */
3425 dri2_sync->base.SyncStatus = EGL_UNSIGNALED_KHR;
3428 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3429 if (dri2_dpy->fence->create_fence_fd) {
3430 dri2_sync->fence = dri2_dpy->fence->create_fence_fd(
3431 dri2_ctx->dri_context,
3432 dri2_sync->base.SyncFd);
3434 if (!dri2_sync->fence) {
3435 _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3442 p_atomic_set(&dri2_sync->refcount, 1);
3443 return &dri2_sync->base;
3447 dri2_destroy_sync(_EGLDisplay *disp, _EGLSync *sync)
3449 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3450 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3451 EGLint ret = EGL_TRUE;
3454 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
3455 * then unlock all threads possibly blocked by the reusable sync before
3458 if (dri2_sync->base.Type == EGL_SYNC_REUSABLE_KHR &&
3459 dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3460 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3461 /* unblock all threads currently blocked by sync */
3462 err = cnd_broadcast(&dri2_sync->cond);
3465 _eglError(EGL_BAD_ACCESS, "eglDestroySyncKHR");
3470 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
3476 dri2_dup_native_fence_fd(_EGLDisplay *disp, _EGLSync *sync)
3478 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3479 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3481 assert(sync->Type == EGL_SYNC_NATIVE_FENCE_ANDROID);
3483 if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3484 /* try to retrieve the actual native fence fd.. if rendering is
3485 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
3487 sync->SyncFd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
3491 if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3492 /* if native fence fd still not created, return an error: */
3493 _eglError(EGL_BAD_PARAMETER, "eglDupNativeFenceFDANDROID");
3494 return EGL_NO_NATIVE_FENCE_FD_ANDROID;
3497 return os_dupfd_cloexec(sync->SyncFd);
3501 dri2_set_blob_cache_funcs(_EGLDisplay *disp,
3502 EGLSetBlobFuncANDROID set,
3503 EGLGetBlobFuncANDROID get)
3505 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3506 dri2_dpy->blob->set_cache_funcs(dri2_dpy->dri_screen,
3508 disp->BlobCacheGet);
3512 dri2_client_wait_sync(_EGLDisplay *disp, _EGLSync *sync,
3513 EGLint flags, EGLTime timeout)
3515 _EGLContext *ctx = _eglGetCurrentContext();
3516 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3517 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3518 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3519 unsigned wait_flags = 0;
3521 EGLint ret = EGL_CONDITION_SATISFIED_KHR;
3523 /* The EGL_KHR_fence_sync spec states:
3525 * "If no context is current for the bound API,
3526 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
3528 if (dri2_ctx && flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)
3529 wait_flags |= __DRI2_FENCE_FLAG_FLUSH_COMMANDS;
3531 /* the sync object should take a reference while waiting */
3532 dri2_egl_ref_sync(dri2_sync);
3534 switch (sync->Type) {
3535 case EGL_SYNC_FENCE_KHR:
3536 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3537 case EGL_SYNC_CL_EVENT_KHR:
3538 if (dri2_dpy->fence->client_wait_sync(dri2_ctx ? dri2_ctx->dri_context : NULL,
3539 dri2_sync->fence, wait_flags,
3541 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3543 ret = EGL_TIMEOUT_EXPIRED_KHR;
3546 case EGL_SYNC_REUSABLE_KHR:
3547 if (dri2_ctx && dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR &&
3548 (flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)) {
3549 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
3553 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
3554 if (timeout == EGL_FOREVER_KHR) {
3555 mtx_lock(&dri2_sync->mutex);
3556 cnd_wait(&dri2_sync->cond, &dri2_sync->mutex);
3557 mtx_unlock(&dri2_sync->mutex);
3559 /* if reusable sync has not been yet signaled */
3560 if (dri2_sync->base.SyncStatus != EGL_SIGNALED_KHR) {
3561 /* timespecs for cnd_timedwait */
3562 struct timespec current;
3563 struct timespec expire;
3565 /* We override the clock to monotonic when creating the condition
3567 clock_gettime(CLOCK_MONOTONIC, ¤t);
3569 /* calculating when to expire */
3570 expire.tv_nsec = timeout % 1000000000L;
3571 expire.tv_sec = timeout / 1000000000L;
3573 expire.tv_nsec += current.tv_nsec;
3574 expire.tv_sec += current.tv_sec;
3576 /* expire.nsec now is a number between 0 and 1999999998 */
3577 if (expire.tv_nsec > 999999999L) {
3579 expire.tv_nsec -= 1000000000L;
3582 mtx_lock(&dri2_sync->mutex);
3583 ret = cnd_timedwait(&dri2_sync->cond, &dri2_sync->mutex, &expire);
3584 mtx_unlock(&dri2_sync->mutex);
3586 if (ret == thrd_busy) {
3587 if (dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3588 ret = EGL_TIMEOUT_EXPIRED_KHR;
3590 _eglError(EGL_BAD_ACCESS, "eglClientWaitSyncKHR");
3598 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
3604 dri2_signal_sync(_EGLDisplay *disp, _EGLSync *sync, EGLenum mode)
3606 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3609 if (sync->Type != EGL_SYNC_REUSABLE_KHR)
3610 return _eglError(EGL_BAD_MATCH, "eglSignalSyncKHR");
3612 if (mode != EGL_SIGNALED_KHR && mode != EGL_UNSIGNALED_KHR)
3613 return _eglError(EGL_BAD_ATTRIBUTE, "eglSignalSyncKHR");
3615 dri2_sync->base.SyncStatus = mode;
3617 if (mode == EGL_SIGNALED_KHR) {
3618 ret = cnd_broadcast(&dri2_sync->cond);
3620 /* fail to broadcast */
3622 return _eglError(EGL_BAD_ACCESS, "eglSignalSyncKHR");
3629 dri2_server_wait_sync(_EGLDisplay *disp, _EGLSync *sync)
3631 _EGLContext *ctx = _eglGetCurrentContext();
3632 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3633 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3634 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3636 dri2_dpy->fence->server_wait_sync(dri2_ctx->dri_context,
3637 dri2_sync->fence, 0);
3642 dri2_interop_query_device_info(_EGLDisplay *disp, _EGLContext *ctx,
3643 struct mesa_glinterop_device_info *out)
3645 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3646 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3648 if (!dri2_dpy->interop)
3649 return MESA_GLINTEROP_UNSUPPORTED;
3651 return dri2_dpy->interop->query_device_info(dri2_ctx->dri_context, out);
3655 dri2_interop_export_object(_EGLDisplay *disp, _EGLContext *ctx,
3656 struct mesa_glinterop_export_in *in,
3657 struct mesa_glinterop_export_out *out)
3659 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3660 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3662 if (!dri2_dpy->interop)
3663 return MESA_GLINTEROP_UNSUPPORTED;
3665 return dri2_dpy->interop->export_object(dri2_ctx->dri_context, in, out);
3668 const _EGLDriver _eglDriver = {
3669 .Initialize = dri2_initialize,
3670 .Terminate = dri2_terminate,
3671 .CreateContext = dri2_create_context,
3672 .DestroyContext = dri2_destroy_context,
3673 .MakeCurrent = dri2_make_current,
3674 .CreateWindowSurface = dri2_create_window_surface,
3675 .CreatePixmapSurface = dri2_create_pixmap_surface,
3676 .CreatePbufferSurface = dri2_create_pbuffer_surface,
3677 .DestroySurface = dri2_destroy_surface,
3678 .GetProcAddress = dri2_get_proc_address,
3679 .WaitClient = dri2_wait_client,
3680 .WaitNative = dri2_wait_native,
3681 .BindTexImage = dri2_bind_tex_image,
3682 .ReleaseTexImage = dri2_release_tex_image,
3683 .SwapInterval = dri2_swap_interval,
3684 .SwapBuffers = dri2_swap_buffers,
3685 .SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage,
3686 .SwapBuffersRegionNOK = dri2_swap_buffers_region,
3687 .SetDamageRegion = dri2_set_damage_region,
3688 .PostSubBufferNV = dri2_post_sub_buffer,
3689 .CopyBuffers = dri2_copy_buffers,
3690 .QueryBufferAge = dri2_query_buffer_age,
3691 .CreateImageKHR = dri2_create_image,
3692 .DestroyImageKHR = dri2_destroy_image_khr,
3693 .CreateWaylandBufferFromImageWL = dri2_create_wayland_buffer_from_image,
3694 .QuerySurface = dri2_query_surface,
3695 .QueryDriverName = dri2_query_driver_name,
3696 .QueryDriverConfig = dri2_query_driver_config,
3698 .CreateDRMImageMESA = dri2_create_drm_image_mesa,
3699 .ExportDRMImageMESA = dri2_export_drm_image_mesa,
3700 .ExportDMABUFImageQueryMESA = dri2_export_dma_buf_image_query_mesa,
3701 .ExportDMABUFImageMESA = dri2_export_dma_buf_image_mesa,
3702 .QueryDmaBufFormatsEXT = dri2_query_dma_buf_formats,
3703 .QueryDmaBufModifiersEXT = dri2_query_dma_buf_modifiers,
3705 #ifdef HAVE_WAYLAND_PLATFORM
3706 .BindWaylandDisplayWL = dri2_bind_wayland_display_wl,
3707 .UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl,
3708 .QueryWaylandBufferWL = dri2_query_wayland_buffer_wl,
3710 .GetSyncValuesCHROMIUM = dri2_get_sync_values_chromium,
3711 .CreateSyncKHR = dri2_create_sync,
3712 .ClientWaitSyncKHR = dri2_client_wait_sync,
3713 .SignalSyncKHR = dri2_signal_sync,
3714 .WaitSyncKHR = dri2_server_wait_sync,
3715 .DestroySyncKHR = dri2_destroy_sync,
3716 .GLInteropQueryDeviceInfo = dri2_interop_query_device_info,
3717 .GLInteropExportObject = dri2_interop_export_object,
3718 .DupNativeFenceFDANDROID = dri2_dup_native_fence_fd,
3719 .SetBlobCacheFuncsANDROID = dri2_set_blob_cache_funcs,