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/u_atomic.h"
66 #include "util/u_vector.h"
67 #include "mapi/glapi/glapi.h"
68 #include "util/bitscan.h"
69 #include "util/u_math.h"
71 /* Additional definitions not yet in the drm_fourcc.h.
73 #ifndef DRM_FORMAT_P010
74 #define DRM_FORMAT_P010 fourcc_code('P', '0', '1', '0') /* 2x2 subsampled Cb:Cr plane 10 bits per channel */
77 #ifndef DRM_FORMAT_P012
78 #define DRM_FORMAT_P012 fourcc_code('P', '0', '1', '2') /* 2x2 subsampled Cb:Cr plane 12 bits per channel */
81 #ifndef DRM_FORMAT_P016
82 #define DRM_FORMAT_P016 fourcc_code('P', '0', '1', '6') /* 2x2 subsampled Cb:Cr plane 16 bits per channel */
85 #define NUM_ATTRIBS 12
87 static const struct dri2_pbuffer_visual {
88 unsigned int dri_image_format;
90 unsigned int rgba_sizes[4];
91 } dri2_pbuffer_visuals[] = {
93 __DRI_IMAGE_FORMAT_ABGR16161616F,
98 __DRI_IMAGE_FORMAT_XBGR16161616F,
103 __DRI_IMAGE_FORMAT_ARGB2101010,
108 __DRI_IMAGE_FORMAT_XRGB2101010,
113 __DRI_IMAGE_FORMAT_ARGB8888,
118 __DRI_IMAGE_FORMAT_XRGB8888,
123 __DRI_IMAGE_FORMAT_RGB565,
130 dri_set_background_context(void *loaderPrivate)
132 _EGLContext *ctx = _eglGetCurrentContext();
133 _EGLThreadInfo *t = _eglGetCurrentThread();
135 _eglBindContextToThread(ctx, t);
141 static void (*glFlush)(void);
142 static mtx_t glFlushMutex = _MTX_INITIALIZER_NP;
144 mtx_lock(&glFlushMutex);
146 glFlush = _glapi_get_proc_address("glFlush");
147 mtx_unlock(&glFlushMutex);
149 /* if glFlush is not available things are horribly broken */
151 _eglLog(_EGL_WARNING, "DRI2: failed to find glFlush entry point");
159 dri_is_thread_safe(void *loaderPrivate)
161 struct dri2_egl_surface *dri2_surf = loaderPrivate;
162 UNUSED _EGLDisplay *display = dri2_surf->base.Resource.Display;
164 #ifdef HAVE_X11_PLATFORM
165 Display *xdpy = (Display*)display->PlatformDisplay;
167 /* Check Xlib is running in thread safe mode when running on EGL/X11-xlib
170 * 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
171 * It wll be NULL if XInitThreads wasn't called.
173 if (display->Platform == _EGL_PLATFORM_X11 && xdpy && !xdpy->lock_fns)
177 #ifdef HAVE_WAYLAND_PLATFORM
178 if (display->Platform == _EGL_PLATFORM_WAYLAND)
185 const __DRIbackgroundCallableExtension background_callable_extension = {
186 .base = { __DRI_BACKGROUND_CALLABLE, 2 },
188 .setBackgroundContext = dri_set_background_context,
189 .isThreadSafe = dri_is_thread_safe,
192 const __DRIuseInvalidateExtension use_invalidate = {
193 .base = { __DRI_USE_INVALIDATE, 1 }
197 dri2_get_pbuffer_drawable_info(__DRIdrawable * draw,
198 int *x, int *y, int *w, int *h,
201 struct dri2_egl_surface *dri2_surf = loaderPrivate;
204 *w = dri2_surf->base.Width;
205 *h = dri2_surf->base.Height;
209 dri2_get_bytes_per_pixel(struct dri2_egl_surface *dri2_surf)
211 const int depth = dri2_surf->base.Config->BufferSize;
212 return depth ? util_next_power_of_two(depth / 8) : 0;
216 dri2_put_image(__DRIdrawable * draw, int op,
217 int x, int y, int w, int h,
218 char *data, void *loaderPrivate)
220 struct dri2_egl_surface *dri2_surf = loaderPrivate;
221 const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
222 const int width = dri2_surf->base.Width;
223 const int height = dri2_surf->base.Height;
224 const int dst_stride = width*bpp;
225 const int src_stride = w*bpp;
226 const int x_offset = x*bpp;
227 int copy_width = src_stride;
229 if (!dri2_surf->swrast_device_buffer)
230 dri2_surf->swrast_device_buffer = malloc(height*dst_stride);
232 if (dri2_surf->swrast_device_buffer) {
233 const char *src = data;
234 char *dst = dri2_surf->swrast_device_buffer;
239 /* Drivers are allowed to submit OOB PutImage requests, so clip here. */
240 if (copy_width > dst_stride - x_offset)
241 copy_width = dst_stride - x_offset;
246 memcpy(dst, src, copy_width);
254 dri2_get_image(__DRIdrawable * read,
255 int x, int y, int w, int h,
256 char *data, void *loaderPrivate)
258 struct dri2_egl_surface *dri2_surf = loaderPrivate;
259 const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
260 const int width = dri2_surf->base.Width;
261 const int height = dri2_surf->base.Height;
262 const int src_stride = width*bpp;
263 const int dst_stride = w*bpp;
264 const int x_offset = x*bpp;
265 int copy_width = dst_stride;
266 const char *src = dri2_surf->swrast_device_buffer;
270 memset(data, 0, copy_width * h);
277 /* Drivers are allowed to submit OOB GetImage requests, so clip here. */
278 if (copy_width > src_stride - x_offset)
279 copy_width = src_stride - x_offset;
284 memcpy(dst, src, copy_width);
291 /* HACK: technically we should have swrast_null, instead of these.
293 const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension = {
294 .base = { __DRI_SWRAST_LOADER, 1 },
295 .getDrawableInfo = dri2_get_pbuffer_drawable_info,
296 .putImage = dri2_put_image,
297 .getImage = dri2_get_image,
300 static const EGLint dri2_to_egl_attribute_map[__DRI_ATTRIB_MAX] = {
301 [__DRI_ATTRIB_BUFFER_SIZE ] = EGL_BUFFER_SIZE,
302 [__DRI_ATTRIB_LEVEL] = EGL_LEVEL,
303 [__DRI_ATTRIB_LUMINANCE_SIZE] = EGL_LUMINANCE_SIZE,
304 [__DRI_ATTRIB_DEPTH_SIZE] = EGL_DEPTH_SIZE,
305 [__DRI_ATTRIB_STENCIL_SIZE] = EGL_STENCIL_SIZE,
306 [__DRI_ATTRIB_SAMPLE_BUFFERS] = EGL_SAMPLE_BUFFERS,
307 [__DRI_ATTRIB_SAMPLES] = EGL_SAMPLES,
308 [__DRI_ATTRIB_MAX_PBUFFER_WIDTH] = EGL_MAX_PBUFFER_WIDTH,
309 [__DRI_ATTRIB_MAX_PBUFFER_HEIGHT] = EGL_MAX_PBUFFER_HEIGHT,
310 [__DRI_ATTRIB_MAX_PBUFFER_PIXELS] = EGL_MAX_PBUFFER_PIXELS,
311 [__DRI_ATTRIB_MAX_SWAP_INTERVAL] = EGL_MAX_SWAP_INTERVAL,
312 [__DRI_ATTRIB_MIN_SWAP_INTERVAL] = EGL_MIN_SWAP_INTERVAL,
313 [__DRI_ATTRIB_YINVERTED] = EGL_Y_INVERTED_NOK,
317 dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,
320 const bool double_buffer = surface_type == EGL_WINDOW_BIT;
321 const bool srgb = colorspace == EGL_GL_COLORSPACE_SRGB_KHR;
323 return conf->dri_config[double_buffer][srgb];
327 dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria)
329 if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0)
332 if (!_eglMatchConfig(conf, criteria))
339 dri2_get_shifts_and_sizes(const __DRIcoreExtension *core,
340 const __DRIconfig *config, int *shifts,
345 if (core->getConfigAttrib(config, __DRI_ATTRIB_RED_SHIFT, (unsigned int *)&shifts[0])) {
346 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SHIFT, (unsigned int *)&shifts[1]);
347 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SHIFT, (unsigned int *)&shifts[2]);
348 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SHIFT, (unsigned int *)&shifts[3]);
350 /* Driver isn't exposing shifts, so convert masks to shifts */
351 core->getConfigAttrib(config, __DRI_ATTRIB_RED_MASK, &mask);
352 shifts[0] = ffs(mask) - 1;
353 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_MASK, &mask);
354 shifts[1] = ffs(mask) - 1;
355 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_MASK, &mask);
356 shifts[2] = ffs(mask) - 1;
357 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_MASK, &mask);
358 shifts[3] = ffs(mask) - 1;
361 core->getConfigAttrib(config, __DRI_ATTRIB_RED_SIZE, &sizes[0]);
362 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SIZE, &sizes[1]);
363 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SIZE, &sizes[2]);
364 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SIZE, &sizes[3]);
368 dri2_get_render_type_float(const __DRIcoreExtension *core,
369 const __DRIconfig *config,
372 unsigned int render_type;
374 core->getConfigAttrib(config, __DRI_ATTRIB_RENDER_TYPE, &render_type);
375 *is_float = (render_type & __DRI_ATTRIB_FLOAT_BIT) ? true : false;
379 dri2_image_format_for_pbuffer_config(struct dri2_egl_display *dri2_dpy,
380 const __DRIconfig *config)
383 unsigned int sizes[4];
385 dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
387 for (unsigned i = 0; i < ARRAY_SIZE(dri2_pbuffer_visuals); ++i) {
388 const struct dri2_pbuffer_visual *visual = &dri2_pbuffer_visuals[i];
390 if (shifts[0] == visual->rgba_shifts[0] &&
391 shifts[1] == visual->rgba_shifts[1] &&
392 shifts[2] == visual->rgba_shifts[2] &&
393 shifts[3] == visual->rgba_shifts[3] &&
394 sizes[0] == visual->rgba_sizes[0] &&
395 sizes[1] == visual->rgba_sizes[1] &&
396 sizes[2] == visual->rgba_sizes[2] &&
397 sizes[3] == visual->rgba_sizes[3]) {
398 return visual->dri_image_format;
402 return __DRI_IMAGE_FORMAT_NONE;
405 struct dri2_egl_config *
406 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
407 EGLint surface_type, const EGLint *attr_list,
408 const int *rgba_shifts, const unsigned int *rgba_sizes)
410 struct dri2_egl_config *conf;
411 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
413 unsigned int attrib, value, double_buffer;
415 EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
416 int dri_shifts[4] = { -1, -1, -1, -1 };
417 unsigned int dri_sizes[4] = { 0, 0, 0, 0 };
418 _EGLConfig *matching_config;
419 EGLint num_configs = 0;
422 _eglInitConfig(&base, disp, id);
425 bind_to_texture_rgb = 0;
426 bind_to_texture_rgba = 0;
428 for (int i = 0; i < __DRI_ATTRIB_MAX; ++i) {
429 if (!dri2_dpy->core->indexConfigAttrib(dri_config, i, &attrib, &value))
433 case __DRI_ATTRIB_RENDER_TYPE:
434 if (value & __DRI_ATTRIB_FLOAT_BIT)
435 base.ComponentType = EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT;
436 if (value & __DRI_ATTRIB_RGBA_BIT)
437 value = EGL_RGB_BUFFER;
438 else if (value & __DRI_ATTRIB_LUMINANCE_BIT)
439 value = EGL_LUMINANCE_BUFFER;
442 base.ColorBufferType = value;
445 case __DRI_ATTRIB_CONFIG_CAVEAT:
446 if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
447 value = EGL_NON_CONFORMANT_CONFIG;
448 else if (value & __DRI_ATTRIB_SLOW_BIT)
449 value = EGL_SLOW_CONFIG;
452 base.ConfigCaveat = value;
455 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
456 bind_to_texture_rgb = value;
459 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
460 bind_to_texture_rgba = value;
463 case __DRI_ATTRIB_DOUBLE_BUFFER:
464 double_buffer = value;
467 case __DRI_ATTRIB_RED_SIZE:
468 dri_sizes[0] = value;
469 base.RedSize = value;
472 case __DRI_ATTRIB_RED_MASK:
473 dri_shifts[0] = ffs(value) - 1;
476 case __DRI_ATTRIB_RED_SHIFT:
477 dri_shifts[0] = value;
480 case __DRI_ATTRIB_GREEN_SIZE:
481 dri_sizes[1] = value;
482 base.GreenSize = value;
485 case __DRI_ATTRIB_GREEN_MASK:
486 dri_shifts[1] = ffs(value) - 1;
489 case __DRI_ATTRIB_GREEN_SHIFT:
490 dri_shifts[1] = value;
493 case __DRI_ATTRIB_BLUE_SIZE:
494 dri_sizes[2] = value;
495 base.BlueSize = value;
498 case __DRI_ATTRIB_BLUE_MASK:
499 dri_shifts[2] = ffs(value) - 1;
502 case __DRI_ATTRIB_BLUE_SHIFT:
503 dri_shifts[2] = value;
506 case __DRI_ATTRIB_ALPHA_SIZE:
507 dri_sizes[3] = value;
508 base.AlphaSize = value;
511 case __DRI_ATTRIB_ALPHA_MASK:
512 dri_shifts[3] = ffs(value) - 1;
515 case __DRI_ATTRIB_ALPHA_SHIFT:
516 dri_shifts[3] = value;
519 case __DRI_ATTRIB_ACCUM_RED_SIZE:
520 case __DRI_ATTRIB_ACCUM_GREEN_SIZE:
521 case __DRI_ATTRIB_ACCUM_BLUE_SIZE:
522 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE:
523 /* Don't expose visuals with the accumulation buffer. */
528 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE:
530 if (!disp->Extensions.KHR_gl_colorspace && srgb)
534 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH:
535 base.MaxPbufferWidth = _EGL_MAX_PBUFFER_WIDTH;
537 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT:
538 base.MaxPbufferHeight = _EGL_MAX_PBUFFER_HEIGHT;
540 case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER:
541 if (disp->Extensions.KHR_mutable_render_buffer)
542 surface_type |= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR;
545 key = dri2_to_egl_attribute_map[attrib];
547 _eglSetConfigKey(&base, key, value);
553 for (int i = 0; attr_list[i] != EGL_NONE; i += 2)
554 _eglSetConfigKey(&base, attr_list[i], attr_list[i+1]);
556 if (rgba_shifts && memcmp(rgba_shifts, dri_shifts, sizeof(dri_shifts)))
559 if (rgba_sizes && memcmp(rgba_sizes, dri_sizes, sizeof(dri_sizes)))
562 base.NativeRenderable = EGL_TRUE;
564 base.SurfaceType = surface_type;
565 if (surface_type & (EGL_PBUFFER_BIT |
566 (disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) {
567 base.BindToTextureRGB = bind_to_texture_rgb;
568 if (base.AlphaSize > 0)
569 base.BindToTextureRGBA = bind_to_texture_rgba;
573 surface_type &= ~EGL_PIXMAP_BIT;
576 /* No support for pbuffer + MSAA for now.
578 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
579 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
582 surface_type &= ~EGL_PBUFFER_BIT;
588 base.RenderableType = disp->ClientAPIs;
589 base.Conformant = disp->ClientAPIs;
591 base.MinSwapInterval = dri2_dpy->min_swap_interval;
592 base.MaxSwapInterval = dri2_dpy->max_swap_interval;
594 if (!_eglValidateConfig(&base, EGL_FALSE)) {
595 _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);
599 config_id = base.ConfigID;
600 base.ConfigID = EGL_DONT_CARE;
601 base.SurfaceType = EGL_DONT_CARE;
602 num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1,
603 (_EGLArrayForEach) dri2_match_config, &base);
605 if (num_configs == 1) {
606 conf = (struct dri2_egl_config *) matching_config;
608 if (!conf->dri_config[double_buffer][srgb])
609 conf->dri_config[double_buffer][srgb] = dri_config;
611 /* a similar config type is already added (unlikely) => discard */
614 else if (num_configs == 0) {
615 conf = calloc(1, sizeof *conf);
619 conf->dri_config[double_buffer][srgb] = dri_config;
621 memcpy(&conf->base, &base, sizeof base);
622 conf->base.SurfaceType = 0;
623 conf->base.ConfigID = config_id;
625 _eglLinkConfig(&conf->base);
628 unreachable("duplicates should not be possible");
632 conf->base.SurfaceType |= surface_type;
638 dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
640 _EGLDisplay *disp = data;
641 struct dri2_egl_image *dri2_img;
646 img = _eglLookupImage(image, disp);
648 _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image");
652 dri2_img = dri2_egl_image(image);
654 return dri2_img->dri_image;
657 const __DRIimageLookupExtension image_lookup_extension = {
658 .base = { __DRI_IMAGE_LOOKUP, 1 },
660 .lookupEGLImage = dri2_lookup_egl_image
663 struct dri2_extension_match {
669 static const struct dri2_extension_match dri3_driver_extensions[] = {
670 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
671 { __DRI_IMAGE_DRIVER, 1, offsetof(struct dri2_egl_display, image_driver) },
675 static const struct dri2_extension_match dri2_driver_extensions[] = {
676 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
677 { __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) },
681 static const struct dri2_extension_match dri2_core_extensions[] = {
682 { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) },
683 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
684 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
688 static const struct dri2_extension_match swrast_driver_extensions[] = {
689 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
690 { __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) },
694 static const struct dri2_extension_match swrast_core_extensions[] = {
695 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
699 static const struct dri2_extension_match optional_driver_extensions[] = {
700 { __DRI_CONFIG_OPTIONS, 1, offsetof(struct dri2_egl_display, configOptions) },
704 static const struct dri2_extension_match optional_core_extensions[] = {
705 { __DRI2_ROBUSTNESS, 1, offsetof(struct dri2_egl_display, robustness) },
706 { __DRI2_NO_ERROR, 1, offsetof(struct dri2_egl_display, no_error) },
707 { __DRI2_CONFIG_QUERY, 1, offsetof(struct dri2_egl_display, config) },
708 { __DRI2_FENCE, 1, offsetof(struct dri2_egl_display, fence) },
709 { __DRI2_BUFFER_DAMAGE, 1, offsetof(struct dri2_egl_display, buffer_damage) },
710 { __DRI2_RENDERER_QUERY, 1, offsetof(struct dri2_egl_display, rendererQuery) },
711 { __DRI2_INTEROP, 1, offsetof(struct dri2_egl_display, interop) },
712 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
713 { __DRI2_FLUSH_CONTROL, 1, offsetof(struct dri2_egl_display, flush_control) },
714 { __DRI2_BLOB, 1, offsetof(struct dri2_egl_display, blob) },
715 { __DRI_MUTABLE_RENDER_BUFFER_DRIVER, 1, offsetof(struct dri2_egl_display, mutable_render_buffer) },
720 dri2_bind_extensions(struct dri2_egl_display *dri2_dpy,
721 const struct dri2_extension_match *matches,
722 const __DRIextension **extensions,
728 for (int i = 0; extensions[i]; i++) {
729 _eglLog(_EGL_DEBUG, "found extension `%s'", extensions[i]->name);
730 for (int j = 0; matches[j].name; j++) {
731 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
732 extensions[i]->version >= matches[j].version) {
733 field = ((char *) dri2_dpy + matches[j].offset);
734 *(const __DRIextension **) field = extensions[i];
735 _eglLog(_EGL_INFO, "found extension %s version %d",
736 extensions[i]->name, extensions[i]->version);
742 for (int j = 0; matches[j].name; j++) {
743 field = ((char *) dri2_dpy + matches[j].offset);
744 if (*(const __DRIextension **) field == NULL) {
746 _eglLog(_EGL_DEBUG, "did not find optional extension %s version %d",
747 matches[j].name, matches[j].version);
749 _eglLog(_EGL_WARNING, "did not find extension %s version %d",
750 matches[j].name, matches[j].version);
759 static const __DRIextension **
760 dri2_open_driver(_EGLDisplay *disp)
762 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
763 static const char *search_path_vars[] = {
764 "LIBGL_DRIVERS_PATH",
768 return loader_open_driver(dri2_dpy->driver_name,
774 dri2_load_driver_common(_EGLDisplay *disp,
775 const struct dri2_extension_match *driver_extensions)
777 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
778 const __DRIextension **extensions;
780 extensions = dri2_open_driver(disp);
784 if (!dri2_bind_extensions(dri2_dpy, driver_extensions, extensions, false))
787 dri2_dpy->driver_extensions = extensions;
789 dri2_bind_extensions(dri2_dpy, optional_driver_extensions, extensions, true);
795 dri2_load_driver(_EGLDisplay *disp)
797 return dri2_load_driver_common(disp, dri2_driver_extensions);
801 dri2_load_driver_dri3(_EGLDisplay *disp)
803 return dri2_load_driver_common(disp, dri3_driver_extensions);
807 dri2_load_driver_swrast(_EGLDisplay *disp)
809 return dri2_load_driver_common(disp, swrast_driver_extensions);
813 dri2_renderer_query_integer(struct dri2_egl_display *dri2_dpy, int param)
815 const __DRI2rendererQueryExtension *rendererQuery = dri2_dpy->rendererQuery;
816 unsigned int value = 0;
818 if (!rendererQuery ||
819 rendererQuery->queryInteger(dri2_dpy->dri_screen, param, &value) == -1)
826 dri2_query_driver_name(_EGLDisplay *disp)
828 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
829 return dri2_dpy->driver_name;
833 dri2_query_driver_config(_EGLDisplay *disp)
835 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
836 const __DRIconfigOptionsExtension *ext = dri2_dpy->configOptions;
838 if (ext->base.version >= 2)
839 return ext->getXml(dri2_dpy->driver_name);
841 return strdup(ext->xml);
846 dri2_setup_screen(_EGLDisplay *disp)
848 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
849 unsigned int api_mask;
852 * EGL 1.5 specification defines the default value to 1. Moreover,
853 * eglSwapInterval() is required to clamp requested value to the supported
854 * range. Since the default value is implicitly assumed to be supported,
855 * use it as both minimum and maximum for the platforms that do not allow
856 * changing the interval. Platforms, which allow it (e.g. x11, wayland)
857 * override these values already.
859 dri2_dpy->min_swap_interval = 1;
860 dri2_dpy->max_swap_interval = 1;
861 dri2_dpy->default_swap_interval = 1;
863 if (dri2_dpy->image_driver) {
864 api_mask = dri2_dpy->image_driver->getAPIMask(dri2_dpy->dri_screen);
865 } else if (dri2_dpy->dri2) {
866 api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);
868 assert(dri2_dpy->swrast);
869 api_mask = 1 << __DRI_API_OPENGL |
870 1 << __DRI_API_GLES |
871 1 << __DRI_API_GLES2 |
872 1 << __DRI_API_GLES3;
875 disp->ClientAPIs = 0;
876 if ((api_mask & (1 <<__DRI_API_OPENGL)) && _eglIsApiValid(EGL_OPENGL_API))
877 disp->ClientAPIs |= EGL_OPENGL_BIT;
878 if ((api_mask & (1 << __DRI_API_GLES)) && _eglIsApiValid(EGL_OPENGL_ES_API))
879 disp->ClientAPIs |= EGL_OPENGL_ES_BIT;
880 if ((api_mask & (1 << __DRI_API_GLES2)) && _eglIsApiValid(EGL_OPENGL_ES_API))
881 disp->ClientAPIs |= EGL_OPENGL_ES2_BIT;
882 if ((api_mask & (1 << __DRI_API_GLES3)) && _eglIsApiValid(EGL_OPENGL_ES_API))
883 disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR;
885 assert(dri2_dpy->image_driver || dri2_dpy->dri2 || dri2_dpy->swrast);
886 disp->Extensions.KHR_no_config_context = EGL_TRUE;
887 disp->Extensions.KHR_surfaceless_context = EGL_TRUE;
889 if (dri2_dpy->configOptions) {
890 disp->Extensions.MESA_query_driver = EGL_TRUE;
893 /* Report back to EGL the bitmask of priorities supported */
894 disp->Extensions.IMG_context_priority =
895 dri2_renderer_query_integer(dri2_dpy,
896 __DRI2_RENDERER_HAS_CONTEXT_PRIORITY);
898 disp->Extensions.EXT_pixel_format_float = EGL_TRUE;
900 if (dri2_renderer_query_integer(dri2_dpy,
901 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB))
902 disp->Extensions.KHR_gl_colorspace = EGL_TRUE;
904 if (dri2_dpy->image_driver ||
905 (dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) ||
906 (dri2_dpy->swrast && dri2_dpy->swrast->base.version >= 3)) {
907 disp->Extensions.KHR_create_context = EGL_TRUE;
909 if (dri2_dpy->robustness)
910 disp->Extensions.EXT_create_context_robustness = EGL_TRUE;
913 if (dri2_dpy->no_error)
914 disp->Extensions.KHR_create_context_no_error = EGL_TRUE;
916 if (dri2_dpy->fence) {
917 disp->Extensions.KHR_fence_sync = EGL_TRUE;
918 disp->Extensions.KHR_wait_sync = EGL_TRUE;
919 if (dri2_dpy->fence->get_fence_from_cl_event)
920 disp->Extensions.KHR_cl_event2 = EGL_TRUE;
921 if (dri2_dpy->fence->base.version >= 2 &&
922 dri2_dpy->fence->get_capabilities) {
923 unsigned capabilities =
924 dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen);
925 disp->Extensions.ANDROID_native_fence_sync =
926 (capabilities & __DRI_FENCE_CAP_NATIVE_FD) != 0;
931 disp->Extensions.ANDROID_blob_cache = EGL_TRUE;
933 disp->Extensions.KHR_reusable_sync = EGL_TRUE;
935 if (dri2_dpy->image) {
936 if (dri2_dpy->image->base.version >= 10 &&
937 dri2_dpy->image->getCapabilities != NULL) {
940 capabilities = dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
941 disp->Extensions.MESA_drm_image = (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
943 if (dri2_dpy->image->base.version >= 11)
944 disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
946 disp->Extensions.MESA_drm_image = EGL_TRUE;
947 if (dri2_dpy->image->base.version >= 11)
948 disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
951 disp->Extensions.KHR_image_base = EGL_TRUE;
952 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
953 if (dri2_dpy->image->base.version >= 5 &&
954 dri2_dpy->image->createImageFromTexture) {
955 disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
956 disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
958 if (dri2_renderer_query_integer(dri2_dpy,
959 __DRI2_RENDERER_HAS_TEXTURE_3D))
960 disp->Extensions.KHR_gl_texture_3D_image = EGL_TRUE;
963 if (dri2_dpy->image->base.version >= 8 &&
964 dri2_dpy->image->createImageFromDmaBufs) {
965 disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE;
967 if (dri2_dpy->image->base.version >= 15 &&
968 dri2_dpy->image->createImageFromDmaBufs2 &&
969 dri2_dpy->image->queryDmaBufFormats &&
970 dri2_dpy->image->queryDmaBufModifiers) {
971 disp->Extensions.EXT_image_dma_buf_import_modifiers = EGL_TRUE;
976 if (dri2_dpy->flush_control)
977 disp->Extensions.KHR_context_flush_control = EGL_TRUE;
979 if (dri2_dpy->buffer_damage && dri2_dpy->buffer_damage->set_damage_region)
980 disp->Extensions.KHR_partial_update = EGL_TRUE;
984 dri2_setup_swap_interval(_EGLDisplay *disp, int max_swap_interval)
986 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
987 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
989 /* Allow driconf to override applications.*/
990 if (dri2_dpy->config)
991 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
992 "vblank_mode", &vblank_mode);
993 switch (vblank_mode) {
994 case DRI_CONF_VBLANK_NEVER:
995 dri2_dpy->min_swap_interval = 0;
996 dri2_dpy->max_swap_interval = 0;
997 dri2_dpy->default_swap_interval = 0;
999 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1000 dri2_dpy->min_swap_interval = 1;
1001 dri2_dpy->max_swap_interval = max_swap_interval;
1002 dri2_dpy->default_swap_interval = 1;
1004 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1005 dri2_dpy->min_swap_interval = 0;
1006 dri2_dpy->max_swap_interval = max_swap_interval;
1007 dri2_dpy->default_swap_interval = 0;
1010 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1011 dri2_dpy->min_swap_interval = 0;
1012 dri2_dpy->max_swap_interval = max_swap_interval;
1013 dri2_dpy->default_swap_interval = 1;
1018 /* All platforms but DRM call this function to create the screen and populate
1019 * the driver_configs. DRM inherits that information from its display - GBM.
1022 dri2_create_screen(_EGLDisplay *disp)
1024 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1026 if (dri2_dpy->image_driver) {
1027 dri2_dpy->dri_screen =
1028 dri2_dpy->image_driver->createNewScreen2(0, dri2_dpy->fd,
1029 dri2_dpy->loader_extensions,
1030 dri2_dpy->driver_extensions,
1031 &dri2_dpy->driver_configs,
1033 } else if (dri2_dpy->dri2) {
1034 if (dri2_dpy->dri2->base.version >= 4) {
1035 dri2_dpy->dri_screen =
1036 dri2_dpy->dri2->createNewScreen2(0, dri2_dpy->fd,
1037 dri2_dpy->loader_extensions,
1038 dri2_dpy->driver_extensions,
1039 &dri2_dpy->driver_configs, disp);
1041 dri2_dpy->dri_screen =
1042 dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd,
1043 dri2_dpy->loader_extensions,
1044 &dri2_dpy->driver_configs, disp);
1047 assert(dri2_dpy->swrast);
1048 if (dri2_dpy->swrast->base.version >= 4) {
1049 dri2_dpy->dri_screen =
1050 dri2_dpy->swrast->createNewScreen2(0, dri2_dpy->loader_extensions,
1051 dri2_dpy->driver_extensions,
1052 &dri2_dpy->driver_configs, disp);
1054 dri2_dpy->dri_screen =
1055 dri2_dpy->swrast->createNewScreen(0, dri2_dpy->loader_extensions,
1056 &dri2_dpy->driver_configs, disp);
1060 if (dri2_dpy->dri_screen == NULL) {
1061 _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen");
1065 dri2_dpy->own_dri_screen = true;
1070 dri2_setup_extensions(_EGLDisplay *disp)
1072 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1073 const struct dri2_extension_match *mandatory_core_extensions;
1074 const __DRIextension **extensions;
1076 extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
1078 if (dri2_dpy->image_driver || dri2_dpy->dri2)
1079 mandatory_core_extensions = dri2_core_extensions;
1081 mandatory_core_extensions = swrast_core_extensions;
1083 if (!dri2_bind_extensions(dri2_dpy, mandatory_core_extensions, extensions, false))
1086 #ifdef HAVE_DRI3_MODIFIERS
1087 dri2_dpy->multibuffers_available =
1088 (dri2_dpy->dri3_major_version > 1 || (dri2_dpy->dri3_major_version == 1 &&
1089 dri2_dpy->dri3_minor_version >= 2)) &&
1090 (dri2_dpy->present_major_version > 1 || (dri2_dpy->present_major_version == 1 &&
1091 dri2_dpy->present_minor_version >= 2)) &&
1092 (dri2_dpy->image && dri2_dpy->image->base.version >= 15);
1095 dri2_bind_extensions(dri2_dpy, optional_core_extensions, extensions, true);
1100 * Called via eglInitialize(), GLX_drv->API.Initialize().
1102 * This must be guaranteed to be called exactly once, even if eglInitialize is
1103 * called many times (without a eglTerminate in between).
1106 dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp)
1108 EGLBoolean ret = EGL_FALSE;
1109 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1111 /* In the case where the application calls eglMakeCurrent(context1),
1112 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
1113 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
1114 * initialized, as we need it to be able to free context1 correctly.
1116 * It would probably be safest to forcibly release the display with
1117 * dri2_display_release, to make sure the display is reinitialized correctly.
1118 * However, the EGL spec states that we need to keep a reference to the
1119 * current context (so we cannot call dri2_make_current(NULL)), and therefore
1120 * we would leak context1 as we would be missing the old display connection
1121 * to free it up correctly.
1124 dri2_dpy->ref_count++;
1128 loader_set_logger(_eglLog);
1130 switch (disp->Platform) {
1131 case _EGL_PLATFORM_SURFACELESS:
1132 ret = dri2_initialize_surfaceless(drv, disp);
1134 case _EGL_PLATFORM_DEVICE:
1135 ret = dri2_initialize_device(drv, disp);
1137 case _EGL_PLATFORM_X11:
1138 ret = dri2_initialize_x11(drv, disp);
1140 case _EGL_PLATFORM_DRM:
1141 ret = dri2_initialize_drm(drv, disp);
1143 case _EGL_PLATFORM_WAYLAND:
1144 ret = dri2_initialize_wayland(drv, disp);
1146 case _EGL_PLATFORM_ANDROID:
1147 ret = dri2_initialize_android(drv, disp);
1150 unreachable("Callers ensure we cannot get here.");
1157 dri2_dpy = dri2_egl_display(disp);
1158 dri2_dpy->ref_count++;
1164 * Decrement display reference count, and free up display if necessary.
1167 dri2_display_release(_EGLDisplay *disp)
1169 struct dri2_egl_display *dri2_dpy;
1174 dri2_dpy = dri2_egl_display(disp);
1176 assert(dri2_dpy->ref_count > 0);
1177 dri2_dpy->ref_count--;
1179 if (dri2_dpy->ref_count > 0)
1182 _eglCleanupDisplay(disp);
1183 dri2_display_destroy(disp);
1187 dri2_display_destroy(_EGLDisplay *disp)
1189 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1191 if (dri2_dpy->own_dri_screen) {
1192 if (dri2_dpy->vtbl && dri2_dpy->vtbl->close_screen_notify)
1193 dri2_dpy->vtbl->close_screen_notify(disp);
1194 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1196 if (dri2_dpy->fd >= 0)
1197 close(dri2_dpy->fd);
1198 if (dri2_dpy->driver)
1199 dlclose(dri2_dpy->driver);
1200 free(dri2_dpy->driver_name);
1202 #ifdef HAVE_WAYLAND_PLATFORM
1203 free(dri2_dpy->device_name);
1206 switch (disp->Platform) {
1207 case _EGL_PLATFORM_X11:
1208 dri2_teardown_x11(dri2_dpy);
1210 case _EGL_PLATFORM_DRM:
1211 dri2_teardown_drm(dri2_dpy);
1213 case _EGL_PLATFORM_WAYLAND:
1214 dri2_teardown_wayland(dri2_dpy);
1217 /* TODO: add teardown for other platforms */
1221 /* The drm platform does not create the screen/driver_configs but reuses
1222 * the ones from the gbm device. As such the gbm itself is responsible
1225 if (disp->Platform != _EGL_PLATFORM_DRM && dri2_dpy->driver_configs) {
1226 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++)
1227 free((__DRIconfig *) dri2_dpy->driver_configs[i]);
1228 free(dri2_dpy->driver_configs);
1231 disp->DriverData = NULL;
1235 dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
1236 unsigned int att, unsigned int format)
1238 struct dri2_egl_display *dri2_dpy =
1239 dri2_egl_display(dri2_surf->base.Resource.Display);
1241 if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
1244 if (!dri2_surf->local_buffers[att]) {
1245 dri2_surf->local_buffers[att] =
1246 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
1247 dri2_surf->base.Width, dri2_surf->base.Height);
1250 return dri2_surf->local_buffers[att];
1254 dri2_egl_surface_free_local_buffers(struct dri2_egl_surface *dri2_surf)
1256 struct dri2_egl_display *dri2_dpy =
1257 dri2_egl_display(dri2_surf->base.Resource.Display);
1259 for (int i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
1260 if (dri2_surf->local_buffers[i]) {
1261 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
1262 dri2_surf->local_buffers[i]);
1263 dri2_surf->local_buffers[i] = NULL;
1269 * Called via eglTerminate(), drv->API.Terminate().
1271 * This must be guaranteed to be called exactly once, even if eglTerminate is
1272 * called many times (without a eglInitialize in between).
1275 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
1277 /* Release all non-current Context/Surfaces. */
1278 _eglReleaseDisplayResources(drv, disp);
1280 dri2_display_release(disp);
1286 * Set the error code after a call to
1287 * dri2_egl_display::dri2::createContextAttribs.
1290 dri2_create_context_attribs_error(int dri_error)
1294 switch (dri_error) {
1295 case __DRI_CTX_ERROR_SUCCESS:
1298 case __DRI_CTX_ERROR_NO_MEMORY:
1299 egl_error = EGL_BAD_ALLOC;
1302 /* From the EGL_KHR_create_context spec, section "Errors":
1304 * * If <config> does not support a client API context compatible
1305 * with the requested API major and minor version, [...] context flags,
1306 * and context reset notification behavior (for client API types where
1307 * these attributes are supported), then an EGL_BAD_MATCH error is
1310 * * If an OpenGL ES context is requested and the values for
1311 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1312 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1313 * is not defined, than an EGL_BAD_MATCH error is generated.
1315 * * If an OpenGL context is requested, the requested version is
1316 * greater than 3.2, and the value for attribute
1317 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1318 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1319 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1320 * one of these bits set; or if the implementation does not support
1321 * the requested profile, then an EGL_BAD_MATCH error is generated.
1323 case __DRI_CTX_ERROR_BAD_API:
1324 case __DRI_CTX_ERROR_BAD_VERSION:
1325 case __DRI_CTX_ERROR_BAD_FLAG:
1326 egl_error = EGL_BAD_MATCH;
1329 /* From the EGL_KHR_create_context spec, section "Errors":
1331 * * If an attribute name or attribute value in <attrib_list> is not
1332 * recognized (including unrecognized bits in bitmask attributes),
1333 * then an EGL_BAD_ATTRIBUTE error is generated."
1335 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE:
1336 case __DRI_CTX_ERROR_UNKNOWN_FLAG:
1337 egl_error = EGL_BAD_ATTRIBUTE;
1341 assert(!"unknown dri_error code");
1342 egl_error = EGL_BAD_MATCH;
1346 _eglError(egl_error, "dri2_create_context");
1350 dri2_fill_context_attribs(struct dri2_egl_context *dri2_ctx,
1351 struct dri2_egl_display *dri2_dpy,
1352 uint32_t *ctx_attribs,
1353 unsigned *num_attribs)
1357 assert(*num_attribs >= NUM_ATTRIBS);
1359 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
1360 ctx_attribs[pos++] = dri2_ctx->base.ClientMajorVersion;
1361 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
1362 ctx_attribs[pos++] = dri2_ctx->base.ClientMinorVersion;
1364 if (dri2_ctx->base.Flags != 0 || dri2_ctx->base.NoError) {
1365 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1366 * extension, don't even try to send it the robust-access flag.
1367 * It may explode. Instead, generate the required EGL error here.
1369 if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0
1370 && !dri2_dpy->robustness) {
1371 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1375 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_FLAGS;
1376 ctx_attribs[pos++] = dri2_ctx->base.Flags |
1377 (dri2_ctx->base.NoError ? __DRI_CTX_FLAG_NO_ERROR : 0);
1380 if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) {
1381 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1382 * extension, don't even try to send it a reset strategy. It may
1383 * explode. Instead, generate the required EGL error here.
1385 if (!dri2_dpy->robustness) {
1386 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1390 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
1391 ctx_attribs[pos++] = __DRI_CTX_RESET_LOSE_CONTEXT;
1394 if (dri2_ctx->base.ContextPriority != EGL_CONTEXT_PRIORITY_MEDIUM_IMG) {
1397 switch (dri2_ctx->base.ContextPriority) {
1398 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
1399 val = __DRI_CTX_PRIORITY_HIGH;
1401 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
1402 val = __DRI_CTX_PRIORITY_MEDIUM;
1404 case EGL_CONTEXT_PRIORITY_LOW_IMG:
1405 val = __DRI_CTX_PRIORITY_LOW;
1408 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1412 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_PRIORITY;
1413 ctx_attribs[pos++] = val;
1416 if (dri2_ctx->base.ReleaseBehavior == EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR) {
1417 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
1418 ctx_attribs[pos++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE;
1427 * Called via eglCreateContext(), drv->API.CreateContext().
1429 static _EGLContext *
1430 dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
1431 _EGLContext *share_list, const EGLint *attrib_list)
1433 struct dri2_egl_context *dri2_ctx;
1434 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1435 struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
1436 __DRIcontext *shared =
1437 dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL;
1438 struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
1439 const __DRIconfig *dri_config;
1442 unsigned num_attribs = NUM_ATTRIBS;
1443 uint32_t ctx_attribs[NUM_ATTRIBS];
1447 dri2_ctx = malloc(sizeof *dri2_ctx);
1449 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
1453 if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
1456 /* The EGL_EXT_create_context_robustness spec says:
1458 * "Add to the eglCreateContext context creation errors: [...]
1460 * * If the reset notification behavior of <share_context> and the
1461 * newly created context are different then an EGL_BAD_MATCH error is
1464 if (share_list && share_list->ResetNotificationStrategy !=
1465 dri2_ctx->base.ResetNotificationStrategy) {
1466 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1470 /* The EGL_KHR_create_context_no_error spec says:
1472 * "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
1473 * used to create <share_context> does not match the value of
1474 * EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
1476 if (share_list && share_list->NoError != dri2_ctx->base.NoError) {
1477 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1481 switch (dri2_ctx->base.ClientAPI) {
1482 case EGL_OPENGL_ES_API:
1483 switch (dri2_ctx->base.ClientMajorVersion) {
1485 api = __DRI_API_GLES;
1488 api = __DRI_API_GLES2;
1491 api = __DRI_API_GLES3;
1494 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1499 case EGL_OPENGL_API:
1500 if ((dri2_ctx->base.ClientMajorVersion >= 4
1501 || (dri2_ctx->base.ClientMajorVersion == 3
1502 && dri2_ctx->base.ClientMinorVersion >= 2))
1503 && dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)
1504 api = __DRI_API_OPENGL_CORE;
1505 else if (dri2_ctx->base.ClientMajorVersion == 3 &&
1506 dri2_ctx->base.ClientMinorVersion == 1)
1507 api = __DRI_API_OPENGL_CORE;
1509 api = __DRI_API_OPENGL;
1512 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1518 /* The config chosen here isn't necessarily
1519 * used for surfaces later.
1520 * A pixmap surface will use the single config.
1521 * This opportunity depends on disabling the
1522 * doubleBufferMode check in
1523 * src/mesa/main/context.c:check_compatible()
1525 if (dri2_config->dri_config[1][0])
1526 dri_config = dri2_config->dri_config[1][0];
1528 dri_config = dri2_config->dri_config[0][0];
1533 if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs,
1537 if (dri2_dpy->image_driver) {
1538 dri2_ctx->dri_context =
1539 dri2_dpy->image_driver->createContextAttribs(dri2_dpy->dri_screen,
1547 dri2_create_context_attribs_error(error);
1548 } else if (dri2_dpy->dri2) {
1549 if (dri2_dpy->dri2->base.version >= 3) {
1550 dri2_ctx->dri_context =
1551 dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen,
1559 dri2_create_context_attribs_error(error);
1561 dri2_ctx->dri_context =
1562 dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
1569 assert(dri2_dpy->swrast);
1570 if (dri2_dpy->swrast->base.version >= 3) {
1571 dri2_ctx->dri_context =
1572 dri2_dpy->swrast->createContextAttribs(dri2_dpy->dri_screen,
1580 dri2_create_context_attribs_error(error);
1582 dri2_ctx->dri_context =
1583 dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,
1591 if (!dri2_ctx->dri_context)
1594 return &dri2_ctx->base;
1602 * Called via eglDestroyContext(), drv->API.DestroyContext().
1605 dri2_destroy_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
1607 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1608 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1610 if (_eglPutContext(ctx)) {
1611 dri2_dpy->core->destroyContext(dri2_ctx->dri_context);
1619 dri2_init_surface(_EGLSurface *surf, _EGLDisplay *disp, EGLint type,
1620 _EGLConfig *conf, const EGLint *attrib_list,
1621 EGLBoolean enable_out_fence, void *native_surface)
1623 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1624 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1626 dri2_surf->out_fence_fd = -1;
1627 dri2_surf->enable_out_fence = false;
1628 if (dri2_dpy->fence && dri2_dpy->fence->base.version >= 2 &&
1629 dri2_dpy->fence->get_capabilities &&
1630 (dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen) &
1631 __DRI_FENCE_CAP_NATIVE_FD)) {
1632 dri2_surf->enable_out_fence = enable_out_fence;
1635 return _eglInitSurface(surf, disp, type, conf, attrib_list, native_surface);
1639 dri2_surface_set_out_fence_fd( _EGLSurface *surf, int fence_fd)
1641 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1643 if (dri2_surf->out_fence_fd >= 0)
1644 close(dri2_surf->out_fence_fd);
1646 dri2_surf->out_fence_fd = fence_fd;
1650 dri2_fini_surface(_EGLSurface *surf)
1652 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1654 dri2_surface_set_out_fence_fd(surf, -1);
1655 dri2_surf->enable_out_fence = false;
1659 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
1661 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1663 if (!_eglPutSurface(surf))
1666 return dri2_dpy->vtbl->destroy_surface(drv, disp, surf);
1670 dri2_surf_update_fence_fd(_EGLContext *ctx,
1671 _EGLDisplay *disp, _EGLSurface *surf)
1673 __DRIcontext *dri_ctx = dri2_egl_context(ctx)->dri_context;
1674 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1675 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1679 if (!dri2_surf->enable_out_fence)
1682 fence = dri2_dpy->fence->create_fence_fd(dri_ctx, -1);
1684 fence_fd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
1686 dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, fence);
1688 dri2_surface_set_out_fence_fd(surf, fence_fd);
1692 dri2_create_drawable(struct dri2_egl_display *dri2_dpy,
1693 const __DRIconfig *config,
1694 struct dri2_egl_surface *dri2_surf,
1695 void *loaderPrivate)
1697 __DRIcreateNewDrawableFunc createNewDrawable;
1699 if (dri2_dpy->image_driver)
1700 createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
1701 else if (dri2_dpy->dri2)
1702 createNewDrawable = dri2_dpy->dri2->createNewDrawable;
1703 else if (dri2_dpy->swrast)
1704 createNewDrawable = dri2_dpy->swrast->createNewDrawable;
1706 return _eglError(EGL_BAD_ALLOC, "no createNewDrawable");
1708 dri2_surf->dri_drawable = createNewDrawable(dri2_dpy->dri_screen,
1709 config, loaderPrivate);
1710 if (dri2_surf->dri_drawable == NULL)
1711 return _eglError(EGL_BAD_ALLOC, "createNewDrawable");
1717 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1720 dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf,
1721 _EGLSurface *rsurf, _EGLContext *ctx)
1723 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1724 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1725 _EGLDisplay *old_disp = NULL;
1726 struct dri2_egl_display *old_dri2_dpy = NULL;
1727 _EGLContext *old_ctx;
1728 _EGLSurface *old_dsurf, *old_rsurf;
1729 _EGLSurface *tmp_dsurf, *tmp_rsurf;
1730 __DRIdrawable *ddraw, *rdraw;
1735 return _eglError(EGL_NOT_INITIALIZED, "eglMakeCurrent");
1737 /* make new bindings */
1738 if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf)) {
1739 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1744 old_disp = old_ctx->Resource.Display;
1745 old_dri2_dpy = dri2_egl_display(old_disp);
1748 /* flush before context switch */
1752 ddraw = (dsurf) ? dri2_dpy->vtbl->get_dri_drawable(dsurf) : NULL;
1753 rdraw = (rsurf) ? dri2_dpy->vtbl->get_dri_drawable(rsurf) : NULL;
1754 cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
1757 __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context;
1760 dri2_surf_update_fence_fd(old_ctx, disp, old_dsurf);
1762 /* Disable shared buffer mode */
1763 if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1764 old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1765 old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, false);
1768 dri2_dpy->core->unbindContext(old_cctx);
1771 unbind = (cctx == NULL && ddraw == NULL && rdraw == NULL);
1773 if (!unbind && !dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1774 /* undo the previous _eglBindContext */
1775 _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &tmp_dsurf, &tmp_rsurf);
1776 assert(&dri2_ctx->base == ctx &&
1777 tmp_dsurf == dsurf &&
1778 tmp_rsurf == rsurf);
1780 if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1781 old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1782 old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, true);
1785 _eglPutSurface(dsurf);
1786 _eglPutSurface(rsurf);
1787 _eglPutContext(ctx);
1789 _eglPutSurface(old_dsurf);
1790 _eglPutSurface(old_rsurf);
1791 _eglPutContext(old_ctx);
1793 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1794 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1797 return _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
1800 dri2_destroy_surface(drv, disp, old_dsurf);
1801 dri2_destroy_surface(drv, disp, old_rsurf);
1804 dri2_dpy->ref_count++;
1807 dri2_destroy_context(drv, disp, old_ctx);
1808 dri2_display_release(old_disp);
1811 if (dsurf && _eglSurfaceHasMutableRenderBuffer(dsurf) &&
1812 dri2_dpy->vtbl->set_shared_buffer_mode) {
1813 /* Always update the shared buffer mode. This is obviously needed when
1814 * the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When
1815 * EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the
1816 * case where external non-EGL API may have changed window's shared
1817 * buffer mode since we last saw it.
1819 bool mode = (dsurf->ActiveRenderBuffer == EGL_SINGLE_BUFFER);
1820 dri2_dpy->vtbl->set_shared_buffer_mode(disp, dsurf, mode);
1827 dri2_surface_get_dri_drawable(_EGLSurface *surf)
1829 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1831 return dri2_surf->dri_drawable;
1835 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1838 dri2_get_proc_address(_EGLDriver *drv, const char *procname)
1840 return _glapi_get_proc_address(procname);
1844 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
1845 _EGLConfig *conf, void *native_window,
1846 const EGLint *attrib_list)
1848 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1849 return dri2_dpy->vtbl->create_window_surface(drv, disp, conf, native_window,
1854 dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
1855 _EGLConfig *conf, void *native_pixmap,
1856 const EGLint *attrib_list)
1858 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1859 return dri2_dpy->vtbl->create_pixmap_surface(drv, disp, conf, native_pixmap,
1864 dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
1865 _EGLConfig *conf, const EGLint *attrib_list)
1867 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1868 return dri2_dpy->vtbl->create_pbuffer_surface(drv, disp, conf, attrib_list);
1872 dri2_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
1875 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1876 if (!dri2_dpy->vtbl->swap_interval)
1878 return dri2_dpy->vtbl->swap_interval(drv, disp, surf, interval);
1882 * Asks the client API to flush any rendering to the drawable so that we can
1883 * do our swapbuffers.
1886 dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw)
1888 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1889 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(draw);
1891 if (dri2_dpy->flush) {
1892 if (dri2_dpy->flush->base.version >= 4) {
1893 /* We know there's a current context because:
1895 * "If surface is not bound to the calling thread’s current
1896 * context, an EGL_BAD_SURFACE error is generated."
1898 _EGLContext *ctx = _eglGetCurrentContext();
1899 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1901 /* From the EGL 1.4 spec (page 52):
1903 * "The contents of ancillary buffers are always undefined
1904 * after calling eglSwapBuffers."
1906 dri2_dpy->flush->flush_with_flags(dri2_ctx->dri_context,
1908 __DRI2_FLUSH_DRAWABLE |
1909 __DRI2_FLUSH_INVALIDATE_ANCILLARY,
1910 __DRI2_THROTTLE_SWAPBUFFER);
1912 dri2_dpy->flush->flush(dri_drawable);
1918 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
1920 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1921 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
1922 _EGLContext *ctx = _eglGetCurrentContext();
1926 dri2_surf_update_fence_fd(ctx, disp, surf);
1927 ret = dri2_dpy->vtbl->swap_buffers(drv, disp, surf);
1929 /* SwapBuffers marks the end of the frame; reset the damage region for
1930 * use again next time.
1932 if (ret && dri2_dpy->buffer_damage &&
1933 dri2_dpy->buffer_damage->set_damage_region)
1934 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
1940 dri2_swap_buffers_with_damage(_EGLDriver *drv, _EGLDisplay *disp,
1942 const EGLint *rects, EGLint n_rects)
1944 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1945 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
1946 _EGLContext *ctx = _eglGetCurrentContext();
1950 dri2_surf_update_fence_fd(ctx, disp, surf);
1951 ret = dri2_dpy->vtbl->swap_buffers_with_damage(drv, disp, surf,
1954 /* SwapBuffers marks the end of the frame; reset the damage region for
1955 * use again next time.
1957 if (ret && dri2_dpy->buffer_damage &&
1958 dri2_dpy->buffer_damage->set_damage_region)
1959 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
1965 dri2_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
1966 EGLint numRects, const EGLint *rects)
1968 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1969 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
1972 ret = dri2_dpy->vtbl->swap_buffers_region(drv, disp, surf, numRects, rects);
1974 /* SwapBuffers marks the end of the frame; reset the damage region for
1975 * use again next time.
1977 if (ret && dri2_dpy->buffer_damage &&
1978 dri2_dpy->buffer_damage->set_damage_region)
1979 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
1985 dri2_set_damage_region(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
1986 EGLint *rects, EGLint n_rects)
1988 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1989 __DRIdrawable *drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
1991 if (!dri2_dpy->buffer_damage || !dri2_dpy->buffer_damage->set_damage_region)
1994 dri2_dpy->buffer_damage->set_damage_region(drawable, n_rects, rects);
1999 dri2_post_sub_buffer(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
2000 EGLint x, EGLint y, EGLint width, EGLint height)
2002 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2003 return dri2_dpy->vtbl->post_sub_buffer(drv, disp, surf, x, y, width, height);
2007 dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
2008 void *native_pixmap_target)
2010 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2011 return dri2_dpy->vtbl->copy_buffers(drv, disp, surf, native_pixmap_target);
2015 dri2_query_buffer_age(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
2017 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2018 return dri2_dpy->vtbl->query_buffer_age(drv, disp, surf);
2022 dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
2024 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2025 _EGLSurface *surf = ctx->DrawSurface;
2026 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2030 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
2031 * we need to copy fake to real here.*/
2033 if (dri2_dpy->flush != NULL)
2034 dri2_dpy->flush->flush(dri_drawable);
2040 dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine)
2045 if (engine != EGL_CORE_NATIVE_ENGINE)
2046 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
2053 dri2_bind_tex_image(_EGLDriver *drv,
2054 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
2056 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2057 struct dri2_egl_context *dri2_ctx;
2059 GLint format, target;
2060 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2062 ctx = _eglGetCurrentContext();
2063 dri2_ctx = dri2_egl_context(ctx);
2065 if (!_eglBindTexImage(drv, disp, surf, buffer))
2068 switch (surf->TextureFormat) {
2069 case EGL_TEXTURE_RGB:
2070 format = __DRI_TEXTURE_FORMAT_RGB;
2072 case EGL_TEXTURE_RGBA:
2073 format = __DRI_TEXTURE_FORMAT_RGBA;
2076 assert(!"Unexpected texture format in dri2_bind_tex_image()");
2077 format = __DRI_TEXTURE_FORMAT_RGBA;
2080 switch (surf->TextureTarget) {
2081 case EGL_TEXTURE_2D:
2082 target = GL_TEXTURE_2D;
2085 target = GL_TEXTURE_2D;
2086 assert(!"Unexpected texture target in dri2_bind_tex_image()");
2089 dri2_dpy->tex_buffer->setTexBuffer2(dri2_ctx->dri_context,
2097 dri2_release_tex_image(_EGLDriver *drv,
2098 _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
2100 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2101 struct dri2_egl_context *dri2_ctx;
2104 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2106 ctx = _eglGetCurrentContext();
2107 dri2_ctx = dri2_egl_context(ctx);
2109 if (!_eglReleaseTexImage(drv, disp, surf, buffer))
2112 switch (surf->TextureTarget) {
2113 case EGL_TEXTURE_2D:
2114 target = GL_TEXTURE_2D;
2117 assert(!"missing texture target");
2120 if (dri2_dpy->tex_buffer->base.version >= 3 &&
2121 dri2_dpy->tex_buffer->releaseTexBuffer != NULL) {
2122 dri2_dpy->tex_buffer->releaseTexBuffer(dri2_ctx->dri_context,
2123 target, dri_drawable);
2130 dri2_create_image(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx,
2131 EGLenum target, EGLClientBuffer buffer,
2132 const EGLint *attr_list)
2134 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2135 return dri2_dpy->vtbl->create_image(drv, disp, ctx, target, buffer,
2140 dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image)
2142 struct dri2_egl_image *dri2_img;
2144 if (dri_image == NULL) {
2145 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
2149 dri2_img = malloc(sizeof *dri2_img);
2151 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
2155 _eglInitImage(&dri2_img->base, disp);
2157 dri2_img->dri_image = dri_image;
2159 return &dri2_img->base;
2163 * Translate a DRI Image extension error code into an EGL error code.
2166 egl_error_from_dri_image_error(int dri_error)
2168 switch (dri_error) {
2169 case __DRI_IMAGE_ERROR_SUCCESS:
2171 case __DRI_IMAGE_ERROR_BAD_ALLOC:
2172 return EGL_BAD_ALLOC;
2173 case __DRI_IMAGE_ERROR_BAD_MATCH:
2174 return EGL_BAD_MATCH;
2175 case __DRI_IMAGE_ERROR_BAD_PARAMETER:
2176 return EGL_BAD_PARAMETER;
2177 case __DRI_IMAGE_ERROR_BAD_ACCESS:
2178 return EGL_BAD_ACCESS;
2180 assert(!"unknown dri_error code");
2181 return EGL_BAD_ALLOC;
2186 dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
2187 EGLClientBuffer buffer,
2188 const EGLint *attr_list)
2190 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2191 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2192 GLuint renderbuffer = (GLuint) (uintptr_t) buffer;
2193 __DRIimage *dri_image;
2195 if (renderbuffer == 0) {
2196 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2197 return EGL_NO_IMAGE_KHR;
2200 if (!disp->Extensions.KHR_gl_renderbuffer_image) {
2201 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2202 return EGL_NO_IMAGE_KHR;
2205 if (dri2_dpy->image->base.version >= 17 &&
2206 dri2_dpy->image->createImageFromRenderbuffer2) {
2207 unsigned error = ~0;
2209 dri_image = dri2_dpy->image->createImageFromRenderbuffer2(
2210 dri2_ctx->dri_context, renderbuffer, NULL, &error);
2212 assert(!!dri_image == (error == __DRI_IMAGE_ERROR_SUCCESS));
2215 _eglError(egl_error_from_dri_image_error(error), "dri2_create_image_khr");
2216 return EGL_NO_IMAGE_KHR;
2219 dri_image = dri2_dpy->image->createImageFromRenderbuffer(
2220 dri2_ctx->dri_context, renderbuffer, NULL);
2222 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2223 return EGL_NO_IMAGE_KHR;
2227 return dri2_create_image_from_dri(disp, dri_image);
2230 #ifdef HAVE_WAYLAND_PLATFORM
2232 /* This structure describes how a wl_buffer maps to one or more
2233 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
2234 * offsets and strides of the planes in the buffer. This table maps a
2235 * wl_drm format code to a description of the planes in the buffer
2236 * that lets us create a __DRIimage for each of the planes. */
2238 static const struct wl_drm_components_descriptor {
2239 uint32_t dri_components;
2242 } wl_drm_components[] = {
2243 { __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 },
2244 { __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 },
2245 { __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 },
2246 { __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 },
2247 { __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 },
2251 dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx,
2252 EGLClientBuffer _buffer,
2253 const EGLint *attr_list)
2255 struct wl_drm_buffer *buffer;
2256 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2257 const struct wl_drm_components_descriptor *f;
2258 __DRIimage *dri_image;
2259 _EGLImageAttribs attrs;
2262 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm,
2263 (struct wl_resource *) _buffer);
2267 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2270 plane = attrs.PlaneWL;
2271 f = buffer->driver_format;
2272 if (plane < 0 || plane >= f->nplanes) {
2273 _eglError(EGL_BAD_PARAMETER,
2274 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
2278 dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL);
2279 if (dri_image == NULL && plane == 0)
2280 dri_image = dri2_dpy->image->dupImage(buffer->driver_buffer, NULL);
2281 if (dri_image == NULL) {
2282 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
2286 return dri2_create_image_from_dri(disp, dri_image);
2291 dri2_get_sync_values_chromium(_EGLDisplay *disp, _EGLSurface *surf,
2292 EGLuint64KHR *ust, EGLuint64KHR *msc,
2295 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2296 return dri2_dpy->vtbl->get_sync_values(disp, surf, ust, msc, sbc);
2300 * Set the error code after a call to
2301 * dri2_egl_image::dri_image::createImageFromTexture.
2304 dri2_create_image_khr_texture_error(int dri_error)
2306 EGLint egl_error = egl_error_from_dri_image_error(dri_error);
2308 if (egl_error != EGL_SUCCESS)
2309 _eglError(egl_error, "dri2_create_image_khr_texture");
2313 dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx,
2315 EGLClientBuffer buffer,
2316 const EGLint *attr_list)
2318 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2319 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2320 struct dri2_egl_image *dri2_img;
2321 GLuint texture = (GLuint) (uintptr_t) buffer;
2322 _EGLImageAttribs attrs;
2328 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2329 return EGL_NO_IMAGE_KHR;
2332 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2333 return EGL_NO_IMAGE_KHR;
2336 case EGL_GL_TEXTURE_2D_KHR:
2337 if (!disp->Extensions.KHR_gl_texture_2D_image) {
2338 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2339 return EGL_NO_IMAGE_KHR;
2342 gl_target = GL_TEXTURE_2D;
2344 case EGL_GL_TEXTURE_3D_KHR:
2345 if (!disp->Extensions.KHR_gl_texture_3D_image) {
2346 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2347 return EGL_NO_IMAGE_KHR;
2350 depth = attrs.GLTextureZOffset;
2351 gl_target = GL_TEXTURE_3D;
2353 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
2354 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
2355 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
2356 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
2357 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
2358 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
2359 if (!disp->Extensions.KHR_gl_texture_cubemap_image) {
2360 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2361 return EGL_NO_IMAGE_KHR;
2364 depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
2365 gl_target = GL_TEXTURE_CUBE_MAP;
2368 unreachable("Unexpected target in dri2_create_image_khr_texture()");
2369 return EGL_NO_IMAGE_KHR;
2372 dri2_img = malloc(sizeof *dri2_img);
2374 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2375 return EGL_NO_IMAGE_KHR;
2378 _eglInitImage(&dri2_img->base, disp);
2380 dri2_img->dri_image =
2381 dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context,
2385 attrs.GLTextureLevel,
2388 dri2_create_image_khr_texture_error(error);
2390 if (!dri2_img->dri_image) {
2392 return EGL_NO_IMAGE_KHR;
2394 return &dri2_img->base;
2398 dri2_query_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
2399 EGLint attribute, EGLint *value)
2401 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2402 if (!dri2_dpy->vtbl->query_surface)
2403 return _eglQuerySurface(drv, disp, surf, attribute, value);
2404 return dri2_dpy->vtbl->query_surface(drv, disp, surf, attribute, value);
2407 static struct wl_buffer*
2408 dri2_create_wayland_buffer_from_image(_EGLDriver *drv, _EGLDisplay *disp,
2411 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2412 return dri2_dpy->vtbl->create_wayland_buffer_from_image(drv, disp, img);
2417 dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
2418 EGLClientBuffer buffer, const EGLint *attr_list)
2420 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2421 EGLint format, name, pitch;
2422 _EGLImageAttribs attrs;
2423 __DRIimage *dri_image;
2425 name = (EGLint) (uintptr_t) buffer;
2427 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2430 if (attrs.Width <= 0 || attrs.Height <= 0 ||
2431 attrs.DRMBufferStrideMESA <= 0) {
2432 _eglError(EGL_BAD_PARAMETER,
2433 "bad width, height or stride");
2437 switch (attrs.DRMBufferFormatMESA) {
2438 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2439 format = __DRI_IMAGE_FORMAT_ARGB8888;
2440 pitch = attrs.DRMBufferStrideMESA;
2443 _eglError(EGL_BAD_PARAMETER,
2444 "dri2_create_image_khr: unsupported pixmap depth");
2449 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
2457 return dri2_create_image_from_dri(disp, dri_image);
2461 dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)
2466 * "Required attributes and their values are as follows:
2468 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
2470 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
2471 * by drm_fourcc.h and used as the pixel_format parameter of the
2472 * drm_mode_fb_cmd2 ioctl."
2476 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2477 * incomplete, EGL_BAD_PARAMETER is generated."
2479 if (attrs->Width <= 0 || attrs->Height <= 0 ||
2480 !attrs->DMABufFourCC.IsPresent)
2481 return _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");
2486 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
2487 * specified for a plane's pitch or offset isn't supported by EGL,
2488 * EGL_BAD_ACCESS is generated."
2490 for (unsigned i = 0; i < ARRAY_SIZE(attrs->DMABufPlanePitches); ++i) {
2491 if (attrs->DMABufPlanePitches[i].IsPresent &&
2492 attrs->DMABufPlanePitches[i].Value <= 0)
2493 return _eglError(EGL_BAD_ACCESS, "invalid pitch");
2497 * If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2498 * attribute values may be given.
2500 * This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2501 * EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2503 for (unsigned i = 0; i < DMA_BUF_MAX_PLANES; ++i) {
2504 if (attrs->DMABufPlaneModifiersLo[i].IsPresent !=
2505 attrs->DMABufPlaneModifiersHi[i].IsPresent)
2506 return _eglError(EGL_BAD_PARAMETER, "modifier attribute lo or hi missing");
2509 /* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2510 * mandate it, we only accept the same modifier across all planes. */
2511 for (unsigned i = 1; i < DMA_BUF_MAX_PLANES; ++i) {
2512 if (attrs->DMABufPlaneFds[i].IsPresent) {
2513 if ((attrs->DMABufPlaneModifiersLo[0].IsPresent !=
2514 attrs->DMABufPlaneModifiersLo[i].IsPresent) ||
2515 (attrs->DMABufPlaneModifiersLo[0].Value !=
2516 attrs->DMABufPlaneModifiersLo[i].Value) ||
2517 (attrs->DMABufPlaneModifiersHi[0].Value !=
2518 attrs->DMABufPlaneModifiersHi[i].Value))
2519 return _eglError(EGL_BAD_PARAMETER, "modifier attributes not equal");
2526 /* Returns the total number of planes for the format or zero if it isn't a
2527 * valid fourcc format.
2530 dri2_num_fourcc_format_planes(EGLint format)
2534 case DRM_FORMAT_RG88:
2535 case DRM_FORMAT_GR88:
2536 case DRM_FORMAT_R16:
2537 case DRM_FORMAT_GR1616:
2538 case DRM_FORMAT_RGB332:
2539 case DRM_FORMAT_BGR233:
2540 case DRM_FORMAT_XRGB4444:
2541 case DRM_FORMAT_XBGR4444:
2542 case DRM_FORMAT_RGBX4444:
2543 case DRM_FORMAT_BGRX4444:
2544 case DRM_FORMAT_ARGB4444:
2545 case DRM_FORMAT_ABGR4444:
2546 case DRM_FORMAT_RGBA4444:
2547 case DRM_FORMAT_BGRA4444:
2548 case DRM_FORMAT_XRGB1555:
2549 case DRM_FORMAT_XBGR1555:
2550 case DRM_FORMAT_RGBX5551:
2551 case DRM_FORMAT_BGRX5551:
2552 case DRM_FORMAT_ARGB1555:
2553 case DRM_FORMAT_ABGR1555:
2554 case DRM_FORMAT_RGBA5551:
2555 case DRM_FORMAT_BGRA5551:
2556 case DRM_FORMAT_RGB565:
2557 case DRM_FORMAT_BGR565:
2558 case DRM_FORMAT_RGB888:
2559 case DRM_FORMAT_BGR888:
2560 case DRM_FORMAT_XRGB8888:
2561 case DRM_FORMAT_XBGR8888:
2562 case DRM_FORMAT_RGBX8888:
2563 case DRM_FORMAT_BGRX8888:
2564 case DRM_FORMAT_ARGB8888:
2565 case DRM_FORMAT_ABGR8888:
2566 case DRM_FORMAT_RGBA8888:
2567 case DRM_FORMAT_BGRA8888:
2568 case DRM_FORMAT_XRGB2101010:
2569 case DRM_FORMAT_XBGR2101010:
2570 case DRM_FORMAT_RGBX1010102:
2571 case DRM_FORMAT_BGRX1010102:
2572 case DRM_FORMAT_ARGB2101010:
2573 case DRM_FORMAT_ABGR2101010:
2574 case DRM_FORMAT_RGBA1010102:
2575 case DRM_FORMAT_BGRA1010102:
2576 case DRM_FORMAT_XBGR16161616F:
2577 case DRM_FORMAT_ABGR16161616F:
2578 case DRM_FORMAT_YUYV:
2579 case DRM_FORMAT_YVYU:
2580 case DRM_FORMAT_UYVY:
2581 case DRM_FORMAT_VYUY:
2582 case DRM_FORMAT_AYUV:
2583 case DRM_FORMAT_XYUV8888:
2586 case DRM_FORMAT_NV12:
2587 case DRM_FORMAT_NV21:
2588 case DRM_FORMAT_NV16:
2589 case DRM_FORMAT_NV61:
2590 case DRM_FORMAT_P010:
2591 case DRM_FORMAT_P012:
2592 case DRM_FORMAT_P016:
2595 case DRM_FORMAT_YUV410:
2596 case DRM_FORMAT_YVU410:
2597 case DRM_FORMAT_YUV411:
2598 case DRM_FORMAT_YVU411:
2599 case DRM_FORMAT_YUV420:
2600 case DRM_FORMAT_YVU420:
2601 case DRM_FORMAT_YUV422:
2602 case DRM_FORMAT_YVU422:
2603 case DRM_FORMAT_YUV444:
2604 case DRM_FORMAT_YVU444:
2612 /* Returns the total number of file descriptors. Zero indicates an error. */
2614 dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)
2616 unsigned plane_n = dri2_num_fourcc_format_planes(attrs->DMABufFourCC.Value);
2618 _eglError(EGL_BAD_MATCH, "unknown drm fourcc format");
2622 for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; i++) {
2624 * The modifiers extension spec says:
2626 * "Modifiers may modify any attribute of a buffer import, including
2627 * but not limited to adding extra planes to a format which
2628 * otherwise does not have those planes. As an example, a modifier
2629 * may add a plane for an external compression buffer to a
2630 * single-plane format. The exact meaning and effect of any
2631 * modifier is canonically defined by drm_fourcc.h, not as part of
2634 if (attrs->DMABufPlaneModifiersLo[i].IsPresent &&
2635 attrs->DMABufPlaneModifiersHi[i].IsPresent) {
2643 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2644 * incomplete, EGL_BAD_PARAMETER is generated."
2646 for (unsigned i = 0; i < plane_n; ++i) {
2647 if (!attrs->DMABufPlaneFds[i].IsPresent ||
2648 !attrs->DMABufPlaneOffsets[i].IsPresent ||
2649 !attrs->DMABufPlanePitches[i].IsPresent) {
2650 _eglError(EGL_BAD_PARAMETER, "plane attribute(s) missing");
2656 * The spec also says:
2658 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2659 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2660 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2661 * or EGL_DMA_BUF_PLANE3_* attributes are specified."
2663 for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; ++i) {
2664 if (attrs->DMABufPlaneFds[i].IsPresent ||
2665 attrs->DMABufPlaneOffsets[i].IsPresent ||
2666 attrs->DMABufPlanePitches[i].IsPresent) {
2667 _eglError(EGL_BAD_ATTRIBUTE, "too many plane attributes");
2676 dri2_query_dma_buf_formats(_EGLDriver *drv, _EGLDisplay *disp,
2677 EGLint max, EGLint *formats, EGLint *count)
2679 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2680 if (max < 0 || (max > 0 && formats == NULL))
2681 return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
2683 if (dri2_dpy->image->base.version < 15 ||
2684 dri2_dpy->image->queryDmaBufFormats == NULL)
2687 if (!dri2_dpy->image->queryDmaBufFormats(dri2_dpy->dri_screen, max,
2692 /* Assert that all of the formats returned are actually fourcc formats.
2693 * Some day, if we want the internal interface function to be able to
2694 * return the fake fourcc formats defined in dri_interface.h, we'll have
2695 * to do something more clever here to pair the list down to just real
2696 * fourcc formats so that we don't leak the fake internal ones.
2698 for (int i = 0; i < *count; i++) {
2699 assert(dri2_num_fourcc_format_planes(formats[i]) > 0);
2707 dri2_query_dma_buf_modifiers(_EGLDriver *drv, _EGLDisplay *disp, EGLint format,
2708 EGLint max, EGLuint64KHR *modifiers,
2709 EGLBoolean *external_only, EGLint *count)
2711 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2713 if (dri2_num_fourcc_format_planes(format) == 0)
2714 return _eglError(EGL_BAD_PARAMETER, "invalid fourcc format");
2717 return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
2719 if (max > 0 && modifiers == NULL)
2720 return _eglError(EGL_BAD_PARAMETER, "invalid modifiers array");
2722 if (dri2_dpy->image->base.version < 15 ||
2723 dri2_dpy->image->queryDmaBufModifiers == NULL)
2726 if (dri2_dpy->image->queryDmaBufModifiers(dri2_dpy->dri_screen, format,
2728 (unsigned int *) external_only,
2730 return _eglError(EGL_BAD_PARAMETER, "invalid format");
2738 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2739 * EGL will take a reference to the dma_buf(s) which it will release at any
2740 * time while the EGLDisplay is initialized. It is the responsibility of the
2741 * application to close the dma_buf file descriptors."
2743 * Therefore we must never close or otherwise modify the file descriptors.
2746 dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
2747 EGLClientBuffer buffer, const EGLint *attr_list)
2749 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2751 _EGLImageAttribs attrs;
2752 __DRIimage *dri_image;
2754 int fds[DMA_BUF_MAX_PLANES];
2755 int pitches[DMA_BUF_MAX_PLANES];
2756 int offsets[DMA_BUF_MAX_PLANES];
2758 bool has_modifier = false;
2764 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2765 * error EGL_BAD_PARAMETER is generated."
2767 if (buffer != NULL) {
2768 _eglError(EGL_BAD_PARAMETER, "buffer not NULL");
2772 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2775 if (!dri2_check_dma_buf_attribs(&attrs))
2778 num_fds = dri2_check_dma_buf_format(&attrs);
2782 for (unsigned i = 0; i < num_fds; ++i) {
2783 fds[i] = attrs.DMABufPlaneFds[i].Value;
2784 pitches[i] = attrs.DMABufPlanePitches[i].Value;
2785 offsets[i] = attrs.DMABufPlaneOffsets[i].Value;
2788 /* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2789 * will be present in attrs.DMABufPlaneModifiersLo[0] and
2790 * attrs.DMABufPlaneModifiersHi[0] */
2791 if (attrs.DMABufPlaneModifiersLo[0].IsPresent) {
2792 modifier = combine_u32_into_u64(attrs.DMABufPlaneModifiersHi[0].Value,
2793 attrs.DMABufPlaneModifiersLo[0].Value);
2794 has_modifier = true;
2798 if (dri2_dpy->image->base.version < 15 ||
2799 dri2_dpy->image->createImageFromDmaBufs2 == NULL) {
2800 _eglError(EGL_BAD_MATCH, "unsupported dma_buf format modifier");
2801 return EGL_NO_IMAGE_KHR;
2804 dri2_dpy->image->createImageFromDmaBufs2(dri2_dpy->dri_screen,
2805 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2806 modifier, fds, num_fds, pitches, offsets,
2807 attrs.DMABufYuvColorSpaceHint.Value,
2808 attrs.DMABufSampleRangeHint.Value,
2809 attrs.DMABufChromaHorizontalSiting.Value,
2810 attrs.DMABufChromaVerticalSiting.Value,
2816 dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen,
2817 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2818 fds, num_fds, pitches, offsets,
2819 attrs.DMABufYuvColorSpaceHint.Value,
2820 attrs.DMABufSampleRangeHint.Value,
2821 attrs.DMABufChromaHorizontalSiting.Value,
2822 attrs.DMABufChromaVerticalSiting.Value,
2826 dri2_create_image_khr_texture_error(error);
2829 return EGL_NO_IMAGE_KHR;
2831 res = dri2_create_image_from_dri(disp, dri_image);
2836 dri2_create_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp,
2837 const EGLint *attr_list)
2839 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2840 struct dri2_egl_image *dri2_img;
2841 _EGLImageAttribs attrs;
2842 unsigned int dri_use, valid_mask;
2848 _eglError(EGL_BAD_PARAMETER, __func__);
2849 return EGL_NO_IMAGE_KHR;
2852 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2853 return EGL_NO_IMAGE_KHR;
2855 if (attrs.Width <= 0 || attrs.Height <= 0) {
2856 _eglError(EGL_BAD_PARAMETER, __func__);
2857 return EGL_NO_IMAGE_KHR;
2860 switch (attrs.DRMBufferFormatMESA) {
2861 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2862 format = __DRI_IMAGE_FORMAT_ARGB8888;
2865 _eglError(EGL_BAD_PARAMETER, __func__);
2866 return EGL_NO_IMAGE_KHR;
2870 EGL_DRM_BUFFER_USE_SCANOUT_MESA |
2871 EGL_DRM_BUFFER_USE_SHARE_MESA |
2872 EGL_DRM_BUFFER_USE_CURSOR_MESA;
2873 if (attrs.DRMBufferUseMESA & ~valid_mask) {
2874 _eglError(EGL_BAD_PARAMETER, __func__);
2875 return EGL_NO_IMAGE_KHR;
2879 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
2880 dri_use |= __DRI_IMAGE_USE_SHARE;
2881 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
2882 dri_use |= __DRI_IMAGE_USE_SCANOUT;
2883 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA)
2884 dri_use |= __DRI_IMAGE_USE_CURSOR;
2886 dri2_img = malloc(sizeof *dri2_img);
2888 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2889 return EGL_NO_IMAGE_KHR;
2892 _eglInitImage(&dri2_img->base, disp);
2894 dri2_img->dri_image =
2895 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
2896 attrs.Width, attrs.Height,
2897 format, dri_use, dri2_img);
2898 if (dri2_img->dri_image == NULL) {
2900 _eglError(EGL_BAD_ALLOC, "dri2_create_drm_image_mesa");
2901 return EGL_NO_IMAGE_KHR;
2904 return &dri2_img->base;
2908 dri2_export_drm_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img,
2909 EGLint *name, EGLint *handle, EGLint *stride)
2911 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2912 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
2916 if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image,
2917 __DRI_IMAGE_ATTRIB_NAME, name))
2918 return _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa");
2921 dri2_dpy->image->queryImage(dri2_img->dri_image,
2922 __DRI_IMAGE_ATTRIB_HANDLE, handle);
2925 dri2_dpy->image->queryImage(dri2_img->dri_image,
2926 __DRI_IMAGE_ATTRIB_STRIDE, stride);
2932 * Checks if we can support EGL_MESA_image_dma_buf_export on this image.
2934 * The spec provides a boolean return for the driver to reject exporting for
2935 * basically any reason, but doesn't specify any particular error cases. For
2936 * now, we just fail if we don't have a DRM fourcc for the format.
2939 dri2_can_export_dma_buf_image(_EGLDisplay *disp, _EGLImage *img)
2941 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2942 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
2945 if (!dri2_dpy->image->queryImage(dri2_img->dri_image,
2946 __DRI_IMAGE_ATTRIB_FOURCC, &fourcc)) {
2954 dri2_export_dma_buf_image_query_mesa(_EGLDriver *drv, _EGLDisplay *disp,
2956 EGLint *fourcc, EGLint *nplanes,
2957 EGLuint64KHR *modifiers)
2959 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2960 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
2965 if (!dri2_can_export_dma_buf_image(disp, img))
2968 dri2_dpy->image->queryImage(dri2_img->dri_image,
2969 __DRI_IMAGE_ATTRIB_NUM_PLANES, &num_planes);
2971 *nplanes = num_planes;
2974 dri2_dpy->image->queryImage(dri2_img->dri_image,
2975 __DRI_IMAGE_ATTRIB_FOURCC, fourcc);
2979 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
2982 query = dri2_dpy->image->queryImage(dri2_img->dri_image,
2983 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
2985 query &= dri2_dpy->image->queryImage(dri2_img->dri_image,
2986 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
2989 modifier = combine_u32_into_u64 (mod_hi, mod_lo);
2991 for (int i = 0; i < num_planes; i++)
2992 modifiers[i] = modifier;
2999 dri2_export_dma_buf_image_mesa(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *img,
3000 int *fds, EGLint *strides, EGLint *offsets)
3002 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3003 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3008 if (!dri2_can_export_dma_buf_image(disp, img))
3011 /* EGL_MESA_image_dma_buf_export spec says:
3012 * "If the number of fds is less than the number of planes, then
3013 * subsequent fd slots should contain -1."
3016 /* Query nplanes so that we know how big the given array is. */
3017 dri2_dpy->image->queryImage(dri2_img->dri_image,
3018 __DRI_IMAGE_ATTRIB_NUM_PLANES, &nplanes);
3019 memset(fds, -1, nplanes * sizeof(int));
3022 /* rework later to provide multiple fds/strides/offsets */
3024 dri2_dpy->image->queryImage(dri2_img->dri_image,
3025 __DRI_IMAGE_ATTRIB_FD, fds);
3028 dri2_dpy->image->queryImage(dri2_img->dri_image,
3029 __DRI_IMAGE_ATTRIB_STRIDE, strides);
3033 bool ret = dri2_dpy->image->queryImage(dri2_img->dri_image,
3034 __DRI_IMAGE_ATTRIB_OFFSET, &img_offset);
3036 offsets[0] = img_offset;
3047 dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
3048 _EGLContext *ctx, EGLenum target,
3049 EGLClientBuffer buffer, const EGLint *attr_list)
3054 case EGL_GL_TEXTURE_2D_KHR:
3055 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
3056 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
3057 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
3058 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
3059 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
3060 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
3061 case EGL_GL_TEXTURE_3D_KHR:
3062 return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list);
3063 case EGL_GL_RENDERBUFFER_KHR:
3064 return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
3066 case EGL_DRM_BUFFER_MESA:
3067 return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list);
3068 case EGL_LINUX_DMA_BUF_EXT:
3069 return dri2_create_image_dma_buf(disp, ctx, buffer, attr_list);
3071 #ifdef HAVE_WAYLAND_PLATFORM
3072 case EGL_WAYLAND_BUFFER_WL:
3073 return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list);
3076 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
3077 return EGL_NO_IMAGE_KHR;
3082 dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image)
3084 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3085 struct dri2_egl_image *dri2_img = dri2_egl_image(image);
3089 dri2_dpy->image->destroyImage(dri2_img->dri_image);
3095 #ifdef HAVE_WAYLAND_PLATFORM
3098 dri2_wl_reference_buffer(void *user_data, uint32_t name, int fd,
3099 struct wl_drm_buffer *buffer)
3101 _EGLDisplay *disp = user_data;
3102 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3104 int dri_components = 0;
3107 img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen,
3116 img = dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
3128 dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components);
3130 buffer->driver_format = NULL;
3131 for (int i = 0; i < ARRAY_SIZE(wl_drm_components); i++)
3132 if (wl_drm_components[i].dri_components == dri_components)
3133 buffer->driver_format = &wl_drm_components[i];
3135 if (buffer->driver_format == NULL)
3136 dri2_dpy->image->destroyImage(img);
3138 buffer->driver_buffer = img;
3142 dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer)
3144 _EGLDisplay *disp = user_data;
3145 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3147 dri2_dpy->image->destroyImage(buffer->driver_buffer);
3151 dri2_bind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp,
3152 struct wl_display *wl_dpy)
3154 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3155 const struct wayland_drm_callbacks wl_drm_callbacks = {
3156 .authenticate = (int(*)(void *, uint32_t)) dri2_dpy->vtbl->authenticate,
3157 .reference_buffer = dri2_wl_reference_buffer,
3158 .release_buffer = dri2_wl_release_buffer,
3159 .is_format_supported = dri2_wl_is_format_supported
3166 if (dri2_dpy->wl_server_drm)
3169 if (drmGetCap(dri2_dpy->fd, DRM_CAP_PRIME, &cap) == 0 &&
3170 cap == (DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT) &&
3171 dri2_dpy->image->base.version >= 7 &&
3172 dri2_dpy->image->createImageFromFds != NULL)
3173 flags |= WAYLAND_DRM_PRIME;
3175 dri2_dpy->wl_server_drm =
3176 wayland_drm_init(wl_dpy, dri2_dpy->device_name,
3177 &wl_drm_callbacks, disp, flags);
3179 if (!dri2_dpy->wl_server_drm)
3182 #ifdef HAVE_DRM_PLATFORM
3183 /* We have to share the wl_drm instance with gbm, so gbm can convert
3184 * wl_buffers to gbm bos. */
3185 if (dri2_dpy->gbm_dri)
3186 dri2_dpy->gbm_dri->wl_drm = dri2_dpy->wl_server_drm;
3193 dri2_unbind_wayland_display_wl(_EGLDriver *drv, _EGLDisplay *disp,
3194 struct wl_display *wl_dpy)
3196 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3200 if (!dri2_dpy->wl_server_drm)
3203 wayland_drm_uninit(dri2_dpy->wl_server_drm);
3204 dri2_dpy->wl_server_drm = NULL;
3210 dri2_query_wayland_buffer_wl(_EGLDriver *drv, _EGLDisplay *disp,
3211 struct wl_resource *buffer_resource,
3212 EGLint attribute, EGLint *value)
3214 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3215 struct wl_drm_buffer *buffer;
3216 const struct wl_drm_components_descriptor *format;
3218 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm, buffer_resource);
3222 format = buffer->driver_format;
3223 switch (attribute) {
3224 case EGL_TEXTURE_FORMAT:
3225 *value = format->components;
3228 *value = buffer->width;
3231 *value = buffer->height;
3240 dri2_egl_ref_sync(struct dri2_egl_sync *sync)
3242 p_atomic_inc(&sync->refcount);
3246 dri2_egl_unref_sync(struct dri2_egl_display *dri2_dpy,
3247 struct dri2_egl_sync *dri2_sync)
3249 if (p_atomic_dec_zero(&dri2_sync->refcount)) {
3250 switch (dri2_sync->base.Type) {
3251 case EGL_SYNC_REUSABLE_KHR:
3252 cnd_destroy(&dri2_sync->cond);
3254 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3255 if (dri2_sync->base.SyncFd != EGL_NO_NATIVE_FENCE_FD_ANDROID)
3256 close(dri2_sync->base.SyncFd);
3262 if (dri2_sync->fence)
3263 dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, dri2_sync->fence);
3270 dri2_create_sync(_EGLDriver *drv, _EGLDisplay *disp,
3271 EGLenum type, const EGLAttrib *attrib_list)
3273 _EGLContext *ctx = _eglGetCurrentContext();
3274 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3275 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3276 struct dri2_egl_sync *dri2_sync;
3278 pthread_condattr_t attr;
3280 dri2_sync = calloc(1, sizeof(struct dri2_egl_sync));
3282 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3286 if (!_eglInitSync(&dri2_sync->base, disp, type, attrib_list)) {
3292 case EGL_SYNC_FENCE_KHR:
3293 dri2_sync->fence = dri2_dpy->fence->create_fence(dri2_ctx->dri_context);
3294 if (!dri2_sync->fence) {
3295 /* Why did it fail? DRI doesn't return an error code, so we emit
3296 * a generic EGL error that doesn't communicate user error.
3298 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3304 case EGL_SYNC_CL_EVENT_KHR:
3305 dri2_sync->fence = dri2_dpy->fence->get_fence_from_cl_event(
3306 dri2_dpy->dri_screen,
3307 dri2_sync->base.CLEvent);
3308 /* this can only happen if the cl_event passed in is invalid. */
3309 if (!dri2_sync->fence) {
3310 _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3315 /* the initial status must be "signaled" if the cl_event is signaled */
3316 if (dri2_dpy->fence->client_wait_sync(dri2_ctx->dri_context,
3317 dri2_sync->fence, 0, 0))
3318 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3321 case EGL_SYNC_REUSABLE_KHR:
3322 /* intialize attr */
3323 ret = pthread_condattr_init(&attr);
3326 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3331 /* change clock attribute to CLOCK_MONOTONIC */
3332 ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
3335 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3340 ret = pthread_cond_init(&dri2_sync->cond, &attr);
3343 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3348 /* initial status of reusable sync must be "unsignaled" */
3349 dri2_sync->base.SyncStatus = EGL_UNSIGNALED_KHR;
3352 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3353 if (dri2_dpy->fence->create_fence_fd) {
3354 dri2_sync->fence = dri2_dpy->fence->create_fence_fd(
3355 dri2_ctx->dri_context,
3356 dri2_sync->base.SyncFd);
3358 if (!dri2_sync->fence) {
3359 _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3366 p_atomic_set(&dri2_sync->refcount, 1);
3367 return &dri2_sync->base;
3371 dri2_destroy_sync(_EGLDriver *drv, _EGLDisplay *disp, _EGLSync *sync)
3373 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3374 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3375 EGLint ret = EGL_TRUE;
3378 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
3379 * then unlock all threads possibly blocked by the reusable sync before
3382 if (dri2_sync->base.Type == EGL_SYNC_REUSABLE_KHR &&
3383 dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3384 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3385 /* unblock all threads currently blocked by sync */
3386 err = cnd_broadcast(&dri2_sync->cond);
3389 _eglError(EGL_BAD_ACCESS, "eglDestroySyncKHR");
3394 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
3400 dri2_dup_native_fence_fd(_EGLDriver *drv, _EGLDisplay *disp, _EGLSync *sync)
3402 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3403 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3405 assert(sync->Type == EGL_SYNC_NATIVE_FENCE_ANDROID);
3407 if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3408 /* try to retrieve the actual native fence fd.. if rendering is
3409 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
3411 sync->SyncFd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
3415 if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3416 /* if native fence fd still not created, return an error: */
3417 _eglError(EGL_BAD_PARAMETER, "eglDupNativeFenceFDANDROID");
3418 return EGL_NO_NATIVE_FENCE_FD_ANDROID;
3421 return dup(sync->SyncFd);
3425 dri2_set_blob_cache_funcs(_EGLDriver *drv, _EGLDisplay *disp,
3426 EGLSetBlobFuncANDROID set,
3427 EGLGetBlobFuncANDROID get)
3429 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3430 dri2_dpy->blob->set_cache_funcs(dri2_dpy->dri_screen,
3432 disp->BlobCacheGet);
3436 dri2_client_wait_sync(_EGLDriver *drv, _EGLDisplay *disp, _EGLSync *sync,
3437 EGLint flags, EGLTime timeout)
3439 _EGLContext *ctx = _eglGetCurrentContext();
3440 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3441 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3442 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3443 unsigned wait_flags = 0;
3445 EGLint ret = EGL_CONDITION_SATISFIED_KHR;
3447 /* The EGL_KHR_fence_sync spec states:
3449 * "If no context is current for the bound API,
3450 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
3452 if (dri2_ctx && flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)
3453 wait_flags |= __DRI2_FENCE_FLAG_FLUSH_COMMANDS;
3455 /* the sync object should take a reference while waiting */
3456 dri2_egl_ref_sync(dri2_sync);
3458 switch (sync->Type) {
3459 case EGL_SYNC_FENCE_KHR:
3460 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3461 case EGL_SYNC_CL_EVENT_KHR:
3462 if (dri2_dpy->fence->client_wait_sync(dri2_ctx ? dri2_ctx->dri_context : NULL,
3463 dri2_sync->fence, wait_flags,
3465 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3467 ret = EGL_TIMEOUT_EXPIRED_KHR;
3470 case EGL_SYNC_REUSABLE_KHR:
3471 if (dri2_ctx && dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR &&
3472 (flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)) {
3473 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
3477 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
3478 if (timeout == EGL_FOREVER_KHR) {
3479 mtx_lock(&dri2_sync->mutex);
3480 cnd_wait(&dri2_sync->cond, &dri2_sync->mutex);
3481 mtx_unlock(&dri2_sync->mutex);
3483 /* if reusable sync has not been yet signaled */
3484 if (dri2_sync->base.SyncStatus != EGL_SIGNALED_KHR) {
3485 /* timespecs for cnd_timedwait */
3486 struct timespec current;
3487 struct timespec expire;
3489 /* We override the clock to monotonic when creating the condition
3491 clock_gettime(CLOCK_MONOTONIC, ¤t);
3493 /* calculating when to expire */
3494 expire.tv_nsec = timeout % 1000000000L;
3495 expire.tv_sec = timeout / 1000000000L;
3497 expire.tv_nsec += current.tv_nsec;
3498 expire.tv_sec += current.tv_sec;
3500 /* expire.nsec now is a number between 0 and 1999999998 */
3501 if (expire.tv_nsec > 999999999L) {
3503 expire.tv_nsec -= 1000000000L;
3506 mtx_lock(&dri2_sync->mutex);
3507 ret = cnd_timedwait(&dri2_sync->cond, &dri2_sync->mutex, &expire);
3508 mtx_unlock(&dri2_sync->mutex);
3510 if (ret == thrd_busy) {
3511 if (dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3512 ret = EGL_TIMEOUT_EXPIRED_KHR;
3514 _eglError(EGL_BAD_ACCESS, "eglClientWaitSyncKHR");
3522 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
3528 dri2_signal_sync(_EGLDriver *drv, _EGLDisplay *disp, _EGLSync *sync,
3531 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3534 if (sync->Type != EGL_SYNC_REUSABLE_KHR)
3535 return _eglError(EGL_BAD_MATCH, "eglSignalSyncKHR");
3537 if (mode != EGL_SIGNALED_KHR && mode != EGL_UNSIGNALED_KHR)
3538 return _eglError(EGL_BAD_ATTRIBUTE, "eglSignalSyncKHR");
3540 dri2_sync->base.SyncStatus = mode;
3542 if (mode == EGL_SIGNALED_KHR) {
3543 ret = cnd_broadcast(&dri2_sync->cond);
3545 /* fail to broadcast */
3547 return _eglError(EGL_BAD_ACCESS, "eglSignalSyncKHR");
3554 dri2_server_wait_sync(_EGLDriver *drv, _EGLDisplay *disp, _EGLSync *sync)
3556 _EGLContext *ctx = _eglGetCurrentContext();
3557 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3558 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3559 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3561 dri2_dpy->fence->server_wait_sync(dri2_ctx->dri_context,
3562 dri2_sync->fence, 0);
3567 dri2_interop_query_device_info(_EGLDisplay *disp, _EGLContext *ctx,
3568 struct mesa_glinterop_device_info *out)
3570 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3571 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3573 if (!dri2_dpy->interop)
3574 return MESA_GLINTEROP_UNSUPPORTED;
3576 return dri2_dpy->interop->query_device_info(dri2_ctx->dri_context, out);
3580 dri2_interop_export_object(_EGLDisplay *disp, _EGLContext *ctx,
3581 struct mesa_glinterop_export_in *in,
3582 struct mesa_glinterop_export_out *out)
3584 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3585 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3587 if (!dri2_dpy->interop)
3588 return MESA_GLINTEROP_UNSUPPORTED;
3590 return dri2_dpy->interop->export_object(dri2_ctx->dri_context, in, out);
3594 * This is the main entrypoint into the driver, called by libEGL.
3595 * Gets an _EGLDriver object and init its dispatch table.
3598 _eglInitDriver(_EGLDriver *dri2_drv)
3600 dri2_drv->API.Initialize = dri2_initialize;
3601 dri2_drv->API.Terminate = dri2_terminate;
3602 dri2_drv->API.CreateContext = dri2_create_context;
3603 dri2_drv->API.DestroyContext = dri2_destroy_context;
3604 dri2_drv->API.MakeCurrent = dri2_make_current;
3605 dri2_drv->API.CreateWindowSurface = dri2_create_window_surface;
3606 dri2_drv->API.CreatePixmapSurface = dri2_create_pixmap_surface;
3607 dri2_drv->API.CreatePbufferSurface = dri2_create_pbuffer_surface;
3608 dri2_drv->API.DestroySurface = dri2_destroy_surface;
3609 dri2_drv->API.GetProcAddress = dri2_get_proc_address;
3610 dri2_drv->API.WaitClient = dri2_wait_client;
3611 dri2_drv->API.WaitNative = dri2_wait_native;
3612 dri2_drv->API.BindTexImage = dri2_bind_tex_image;
3613 dri2_drv->API.ReleaseTexImage = dri2_release_tex_image;
3614 dri2_drv->API.SwapInterval = dri2_swap_interval;
3615 dri2_drv->API.SwapBuffers = dri2_swap_buffers;
3616 dri2_drv->API.SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage;
3617 dri2_drv->API.SwapBuffersRegionNOK = dri2_swap_buffers_region;
3618 dri2_drv->API.SetDamageRegion = dri2_set_damage_region;
3619 dri2_drv->API.PostSubBufferNV = dri2_post_sub_buffer;
3620 dri2_drv->API.CopyBuffers = dri2_copy_buffers,
3621 dri2_drv->API.QueryBufferAge = dri2_query_buffer_age;
3622 dri2_drv->API.CreateImageKHR = dri2_create_image;
3623 dri2_drv->API.DestroyImageKHR = dri2_destroy_image_khr;
3624 dri2_drv->API.CreateWaylandBufferFromImageWL = dri2_create_wayland_buffer_from_image;
3625 dri2_drv->API.QuerySurface = dri2_query_surface;
3626 dri2_drv->API.QueryDriverName = dri2_query_driver_name;
3627 dri2_drv->API.QueryDriverConfig = dri2_query_driver_config;
3629 dri2_drv->API.CreateDRMImageMESA = dri2_create_drm_image_mesa;
3630 dri2_drv->API.ExportDRMImageMESA = dri2_export_drm_image_mesa;
3631 dri2_drv->API.ExportDMABUFImageQueryMESA = dri2_export_dma_buf_image_query_mesa;
3632 dri2_drv->API.ExportDMABUFImageMESA = dri2_export_dma_buf_image_mesa;
3633 dri2_drv->API.QueryDmaBufFormatsEXT = dri2_query_dma_buf_formats;
3634 dri2_drv->API.QueryDmaBufModifiersEXT = dri2_query_dma_buf_modifiers;
3636 #ifdef HAVE_WAYLAND_PLATFORM
3637 dri2_drv->API.BindWaylandDisplayWL = dri2_bind_wayland_display_wl;
3638 dri2_drv->API.UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl;
3639 dri2_drv->API.QueryWaylandBufferWL = dri2_query_wayland_buffer_wl;
3641 dri2_drv->API.GetSyncValuesCHROMIUM = dri2_get_sync_values_chromium;
3642 dri2_drv->API.CreateSyncKHR = dri2_create_sync;
3643 dri2_drv->API.ClientWaitSyncKHR = dri2_client_wait_sync;
3644 dri2_drv->API.SignalSyncKHR = dri2_signal_sync;
3645 dri2_drv->API.WaitSyncKHR = dri2_server_wait_sync;
3646 dri2_drv->API.DestroySyncKHR = dri2_destroy_sync;
3647 dri2_drv->API.GLInteropQueryDeviceInfo = dri2_interop_query_device_info;
3648 dri2_drv->API.GLInteropExportObject = dri2_interop_export_object;
3649 dri2_drv->API.DupNativeFenceFDANDROID = dri2_dup_native_fence_fd;
3650 dri2_drv->API.SetBlobCacheFuncsANDROID = dri2_set_blob_cache_funcs;