2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Kristian Høgsberg <krh@bitplanet.net>
39 #include <c11/threads.h>
43 #include "drm-uapi/drm_fourcc.h"
46 #include <GL/internal/dri_interface.h>
47 #include <sys/types.h>
50 #ifdef HAVE_WAYLAND_PLATFORM
51 #include <wayland-client.h>
52 #include "wayland-drm.h"
53 #include "wayland-drm-client-protocol.h"
54 #include "linux-dmabuf-unstable-v1-client-protocol.h"
57 #ifdef HAVE_X11_PLATFORM
58 #include "X11/Xlibint.h"
61 #include "egldefines.h"
63 #include "GL/mesa_glinterop.h"
64 #include "loader/loader.h"
65 #include "util/os_file.h"
66 #include "util/u_atomic.h"
67 #include "util/u_vector.h"
68 #include "mapi/glapi/glapi.h"
69 #include "util/bitscan.h"
70 #include "util/u_math.h"
72 /* Additional definitions not yet in the drm_fourcc.h.
74 #ifndef DRM_FORMAT_P010
75 #define DRM_FORMAT_P010 fourcc_code('P', '0', '1', '0') /* 2x2 subsampled Cb:Cr plane 10 bits per channel */
78 #ifndef DRM_FORMAT_P012
79 #define DRM_FORMAT_P012 fourcc_code('P', '0', '1', '2') /* 2x2 subsampled Cb:Cr plane 12 bits per channel */
82 #ifndef DRM_FORMAT_P016
83 #define DRM_FORMAT_P016 fourcc_code('P', '0', '1', '6') /* 2x2 subsampled Cb:Cr plane 16 bits per channel */
86 #define NUM_ATTRIBS 12
88 static const struct dri2_pbuffer_visual {
89 const char *format_name;
90 unsigned int dri_image_format;
92 unsigned int rgba_sizes[4];
93 } dri2_pbuffer_visuals[] = {
96 __DRI_IMAGE_FORMAT_ABGR16161616F,
102 __DRI_IMAGE_FORMAT_XBGR16161616F,
108 __DRI_IMAGE_FORMAT_ARGB2101010,
114 __DRI_IMAGE_FORMAT_XRGB2101010,
120 __DRI_IMAGE_FORMAT_ARGB8888,
126 __DRI_IMAGE_FORMAT_XRGB8888,
132 __DRI_IMAGE_FORMAT_RGB565,
139 dri_set_background_context(void *loaderPrivate)
141 _EGLContext *ctx = _eglGetCurrentContext();
142 _EGLThreadInfo *t = _eglGetCurrentThread();
144 _eglBindContextToThread(ctx, t);
150 static void (*glFlush)(void);
151 static mtx_t glFlushMutex = _MTX_INITIALIZER_NP;
153 mtx_lock(&glFlushMutex);
155 glFlush = _glapi_get_proc_address("glFlush");
156 mtx_unlock(&glFlushMutex);
158 /* if glFlush is not available things are horribly broken */
160 _eglLog(_EGL_WARNING, "DRI2: failed to find glFlush entry point");
168 dri_is_thread_safe(void *loaderPrivate)
170 struct dri2_egl_surface *dri2_surf = loaderPrivate;
171 UNUSED _EGLDisplay *display = dri2_surf->base.Resource.Display;
173 #ifdef HAVE_X11_PLATFORM
174 Display *xdpy = (Display*)display->PlatformDisplay;
176 /* Check Xlib is running in thread safe mode when running on EGL/X11-xlib
179 * 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
180 * It wll be NULL if XInitThreads wasn't called.
182 if (display->Platform == _EGL_PLATFORM_X11 && xdpy && !xdpy->lock_fns)
189 const __DRIbackgroundCallableExtension background_callable_extension = {
190 .base = { __DRI_BACKGROUND_CALLABLE, 2 },
192 .setBackgroundContext = dri_set_background_context,
193 .isThreadSafe = dri_is_thread_safe,
196 const __DRIuseInvalidateExtension use_invalidate = {
197 .base = { __DRI_USE_INVALIDATE, 1 }
201 dri2_get_pbuffer_drawable_info(__DRIdrawable * draw,
202 int *x, int *y, int *w, int *h,
205 struct dri2_egl_surface *dri2_surf = loaderPrivate;
208 *w = dri2_surf->base.Width;
209 *h = dri2_surf->base.Height;
213 dri2_get_bytes_per_pixel(struct dri2_egl_surface *dri2_surf)
215 const int depth = dri2_surf->base.Config->BufferSize;
216 return depth ? util_next_power_of_two(depth / 8) : 0;
220 dri2_put_image(__DRIdrawable * draw, int op,
221 int x, int y, int w, int h,
222 char *data, void *loaderPrivate)
224 struct dri2_egl_surface *dri2_surf = loaderPrivate;
225 const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
226 const int width = dri2_surf->base.Width;
227 const int height = dri2_surf->base.Height;
228 const int dst_stride = width*bpp;
229 const int src_stride = w*bpp;
230 const int x_offset = x*bpp;
231 int copy_width = src_stride;
233 if (!dri2_surf->swrast_device_buffer)
234 dri2_surf->swrast_device_buffer = malloc(height*dst_stride);
236 if (dri2_surf->swrast_device_buffer) {
237 const char *src = data;
238 char *dst = dri2_surf->swrast_device_buffer;
243 /* Drivers are allowed to submit OOB PutImage requests, so clip here. */
244 if (copy_width > dst_stride - x_offset)
245 copy_width = dst_stride - x_offset;
250 memcpy(dst, src, copy_width);
258 dri2_get_image(__DRIdrawable * read,
259 int x, int y, int w, int h,
260 char *data, void *loaderPrivate)
262 struct dri2_egl_surface *dri2_surf = loaderPrivate;
263 const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
264 const int width = dri2_surf->base.Width;
265 const int height = dri2_surf->base.Height;
266 const int src_stride = width*bpp;
267 const int dst_stride = w*bpp;
268 const int x_offset = x*bpp;
269 int copy_width = dst_stride;
270 const char *src = dri2_surf->swrast_device_buffer;
274 memset(data, 0, copy_width * h);
281 /* Drivers are allowed to submit OOB GetImage requests, so clip here. */
282 if (copy_width > src_stride - x_offset)
283 copy_width = src_stride - x_offset;
288 memcpy(dst, src, copy_width);
295 /* HACK: technically we should have swrast_null, instead of these.
297 const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension = {
298 .base = { __DRI_SWRAST_LOADER, 1 },
299 .getDrawableInfo = dri2_get_pbuffer_drawable_info,
300 .putImage = dri2_put_image,
301 .getImage = dri2_get_image,
304 static const EGLint dri2_to_egl_attribute_map[__DRI_ATTRIB_MAX] = {
305 [__DRI_ATTRIB_BUFFER_SIZE ] = EGL_BUFFER_SIZE,
306 [__DRI_ATTRIB_LEVEL] = EGL_LEVEL,
307 [__DRI_ATTRIB_LUMINANCE_SIZE] = EGL_LUMINANCE_SIZE,
308 [__DRI_ATTRIB_DEPTH_SIZE] = EGL_DEPTH_SIZE,
309 [__DRI_ATTRIB_STENCIL_SIZE] = EGL_STENCIL_SIZE,
310 [__DRI_ATTRIB_SAMPLE_BUFFERS] = EGL_SAMPLE_BUFFERS,
311 [__DRI_ATTRIB_SAMPLES] = EGL_SAMPLES,
312 [__DRI_ATTRIB_MAX_PBUFFER_WIDTH] = EGL_MAX_PBUFFER_WIDTH,
313 [__DRI_ATTRIB_MAX_PBUFFER_HEIGHT] = EGL_MAX_PBUFFER_HEIGHT,
314 [__DRI_ATTRIB_MAX_PBUFFER_PIXELS] = EGL_MAX_PBUFFER_PIXELS,
315 [__DRI_ATTRIB_MAX_SWAP_INTERVAL] = EGL_MAX_SWAP_INTERVAL,
316 [__DRI_ATTRIB_MIN_SWAP_INTERVAL] = EGL_MIN_SWAP_INTERVAL,
317 [__DRI_ATTRIB_YINVERTED] = EGL_Y_INVERTED_NOK,
321 dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,
324 const bool double_buffer = surface_type == EGL_WINDOW_BIT;
325 const bool srgb = colorspace == EGL_GL_COLORSPACE_SRGB_KHR;
327 return conf->dri_config[double_buffer][srgb];
331 dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria)
333 if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0)
336 if (!_eglMatchConfig(conf, criteria))
343 dri2_get_shifts_and_sizes(const __DRIcoreExtension *core,
344 const __DRIconfig *config, int *shifts,
349 if (core->getConfigAttrib(config, __DRI_ATTRIB_RED_SHIFT, (unsigned int *)&shifts[0])) {
350 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SHIFT, (unsigned int *)&shifts[1]);
351 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SHIFT, (unsigned int *)&shifts[2]);
352 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SHIFT, (unsigned int *)&shifts[3]);
354 /* Driver isn't exposing shifts, so convert masks to shifts */
355 core->getConfigAttrib(config, __DRI_ATTRIB_RED_MASK, &mask);
356 shifts[0] = ffs(mask) - 1;
357 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_MASK, &mask);
358 shifts[1] = ffs(mask) - 1;
359 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_MASK, &mask);
360 shifts[2] = ffs(mask) - 1;
361 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_MASK, &mask);
362 shifts[3] = ffs(mask) - 1;
365 core->getConfigAttrib(config, __DRI_ATTRIB_RED_SIZE, &sizes[0]);
366 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SIZE, &sizes[1]);
367 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SIZE, &sizes[2]);
368 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SIZE, &sizes[3]);
372 dri2_get_render_type_float(const __DRIcoreExtension *core,
373 const __DRIconfig *config,
376 unsigned int render_type;
378 core->getConfigAttrib(config, __DRI_ATTRIB_RENDER_TYPE, &render_type);
379 *is_float = (render_type & __DRI_ATTRIB_FLOAT_BIT) ? true : false;
383 dri2_image_format_for_pbuffer_config(struct dri2_egl_display *dri2_dpy,
384 const __DRIconfig *config)
387 unsigned int sizes[4];
389 dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
391 for (unsigned i = 0; i < ARRAY_SIZE(dri2_pbuffer_visuals); ++i) {
392 const struct dri2_pbuffer_visual *visual = &dri2_pbuffer_visuals[i];
394 if (shifts[0] == visual->rgba_shifts[0] &&
395 shifts[1] == visual->rgba_shifts[1] &&
396 shifts[2] == visual->rgba_shifts[2] &&
397 shifts[3] == visual->rgba_shifts[3] &&
398 sizes[0] == visual->rgba_sizes[0] &&
399 sizes[1] == visual->rgba_sizes[1] &&
400 sizes[2] == visual->rgba_sizes[2] &&
401 sizes[3] == visual->rgba_sizes[3]) {
402 return visual->dri_image_format;
406 return __DRI_IMAGE_FORMAT_NONE;
409 struct dri2_egl_config *
410 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
411 EGLint surface_type, const EGLint *attr_list,
412 const int *rgba_shifts, const unsigned int *rgba_sizes)
414 struct dri2_egl_config *conf;
415 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
417 unsigned int attrib, value, double_buffer;
419 EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
420 int dri_shifts[4] = { -1, -1, -1, -1 };
421 unsigned int dri_sizes[4] = { 0, 0, 0, 0 };
422 _EGLConfig *matching_config;
423 EGLint num_configs = 0;
426 _eglInitConfig(&base, disp, id);
429 bind_to_texture_rgb = 0;
430 bind_to_texture_rgba = 0;
432 for (int i = 0; i < __DRI_ATTRIB_MAX; ++i) {
433 if (!dri2_dpy->core->indexConfigAttrib(dri_config, i, &attrib, &value))
437 case __DRI_ATTRIB_RENDER_TYPE:
438 if (value & __DRI_ATTRIB_FLOAT_BIT)
439 base.ComponentType = EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT;
440 if (value & __DRI_ATTRIB_RGBA_BIT)
441 value = EGL_RGB_BUFFER;
442 else if (value & __DRI_ATTRIB_LUMINANCE_BIT)
443 value = EGL_LUMINANCE_BUFFER;
446 base.ColorBufferType = value;
449 case __DRI_ATTRIB_CONFIG_CAVEAT:
450 if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
451 value = EGL_NON_CONFORMANT_CONFIG;
452 else if (value & __DRI_ATTRIB_SLOW_BIT)
453 value = EGL_SLOW_CONFIG;
456 base.ConfigCaveat = value;
459 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
460 bind_to_texture_rgb = value;
463 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
464 bind_to_texture_rgba = value;
467 case __DRI_ATTRIB_DOUBLE_BUFFER:
468 double_buffer = value;
471 case __DRI_ATTRIB_RED_SIZE:
472 dri_sizes[0] = value;
473 base.RedSize = value;
476 case __DRI_ATTRIB_RED_MASK:
477 dri_shifts[0] = ffs(value) - 1;
480 case __DRI_ATTRIB_RED_SHIFT:
481 dri_shifts[0] = value;
484 case __DRI_ATTRIB_GREEN_SIZE:
485 dri_sizes[1] = value;
486 base.GreenSize = value;
489 case __DRI_ATTRIB_GREEN_MASK:
490 dri_shifts[1] = ffs(value) - 1;
493 case __DRI_ATTRIB_GREEN_SHIFT:
494 dri_shifts[1] = value;
497 case __DRI_ATTRIB_BLUE_SIZE:
498 dri_sizes[2] = value;
499 base.BlueSize = value;
502 case __DRI_ATTRIB_BLUE_MASK:
503 dri_shifts[2] = ffs(value) - 1;
506 case __DRI_ATTRIB_BLUE_SHIFT:
507 dri_shifts[2] = value;
510 case __DRI_ATTRIB_ALPHA_SIZE:
511 dri_sizes[3] = value;
512 base.AlphaSize = value;
515 case __DRI_ATTRIB_ALPHA_MASK:
516 dri_shifts[3] = ffs(value) - 1;
519 case __DRI_ATTRIB_ALPHA_SHIFT:
520 dri_shifts[3] = value;
523 case __DRI_ATTRIB_ACCUM_RED_SIZE:
524 case __DRI_ATTRIB_ACCUM_GREEN_SIZE:
525 case __DRI_ATTRIB_ACCUM_BLUE_SIZE:
526 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE:
527 /* Don't expose visuals with the accumulation buffer. */
532 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE:
534 if (!disp->Extensions.KHR_gl_colorspace && srgb)
538 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH:
539 base.MaxPbufferWidth = _EGL_MAX_PBUFFER_WIDTH;
541 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT:
542 base.MaxPbufferHeight = _EGL_MAX_PBUFFER_HEIGHT;
544 case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER:
545 if (disp->Extensions.KHR_mutable_render_buffer)
546 surface_type |= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR;
549 key = dri2_to_egl_attribute_map[attrib];
551 _eglSetConfigKey(&base, key, value);
557 for (int i = 0; attr_list[i] != EGL_NONE; i += 2)
558 _eglSetConfigKey(&base, attr_list[i], attr_list[i+1]);
560 if (rgba_shifts && memcmp(rgba_shifts, dri_shifts, sizeof(dri_shifts)))
563 if (rgba_sizes && memcmp(rgba_sizes, dri_sizes, sizeof(dri_sizes)))
566 base.NativeRenderable = EGL_TRUE;
568 base.SurfaceType = surface_type;
569 if (surface_type & (EGL_PBUFFER_BIT |
570 (disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) {
571 base.BindToTextureRGB = bind_to_texture_rgb;
572 if (base.AlphaSize > 0)
573 base.BindToTextureRGBA = bind_to_texture_rgba;
577 surface_type &= ~EGL_PIXMAP_BIT;
580 /* No support for pbuffer + MSAA for now.
582 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
583 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
586 surface_type &= ~EGL_PBUFFER_BIT;
592 base.RenderableType = disp->ClientAPIs;
593 base.Conformant = disp->ClientAPIs;
595 base.MinSwapInterval = dri2_dpy->min_swap_interval;
596 base.MaxSwapInterval = dri2_dpy->max_swap_interval;
598 if (!_eglValidateConfig(&base, EGL_FALSE)) {
599 _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);
603 config_id = base.ConfigID;
604 base.ConfigID = EGL_DONT_CARE;
605 base.SurfaceType = EGL_DONT_CARE;
606 num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1,
607 (_EGLArrayForEach) dri2_match_config, &base);
609 if (num_configs == 1) {
610 conf = (struct dri2_egl_config *) matching_config;
612 if (!conf->dri_config[double_buffer][srgb])
613 conf->dri_config[double_buffer][srgb] = dri_config;
615 /* a similar config type is already added (unlikely) => discard */
618 else if (num_configs == 0) {
619 conf = calloc(1, sizeof *conf);
623 conf->dri_config[double_buffer][srgb] = dri_config;
625 memcpy(&conf->base, &base, sizeof base);
626 conf->base.SurfaceType = 0;
627 conf->base.ConfigID = config_id;
629 _eglLinkConfig(&conf->base);
632 unreachable("duplicates should not be possible");
636 conf->base.SurfaceType |= surface_type;
642 dri2_add_pbuffer_configs_for_visuals(_EGLDisplay *disp)
644 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
645 unsigned int format_count[ARRAY_SIZE(dri2_pbuffer_visuals)] = { 0 };
646 unsigned int config_count = 0;
648 for (unsigned i = 0; dri2_dpy->driver_configs[i] != NULL; i++) {
649 for (unsigned j = 0; j < ARRAY_SIZE(dri2_pbuffer_visuals); j++) {
650 struct dri2_egl_config *dri2_conf;
652 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
653 config_count + 1, EGL_PBUFFER_BIT, NULL,
654 dri2_pbuffer_visuals[j].rgba_shifts, dri2_pbuffer_visuals[j].rgba_sizes);
657 if (dri2_conf->base.ConfigID == config_count + 1)
664 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
665 if (!format_count[i]) {
666 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
667 dri2_pbuffer_visuals[i].format_name);
671 return (config_count != 0);
675 dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
677 _EGLDisplay *disp = data;
678 struct dri2_egl_image *dri2_img;
683 mtx_lock(&disp->Mutex);
684 img = _eglLookupImage(image, disp);
685 mtx_unlock(&disp->Mutex);
688 _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image");
692 dri2_img = dri2_egl_image(image);
694 return dri2_img->dri_image;
697 const __DRIimageLookupExtension image_lookup_extension = {
698 .base = { __DRI_IMAGE_LOOKUP, 1 },
700 .lookupEGLImage = dri2_lookup_egl_image
703 struct dri2_extension_match {
709 static const struct dri2_extension_match dri3_driver_extensions[] = {
710 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
711 { __DRI_IMAGE_DRIVER, 1, offsetof(struct dri2_egl_display, image_driver) },
715 static const struct dri2_extension_match dri2_driver_extensions[] = {
716 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
717 { __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) },
721 static const struct dri2_extension_match dri2_core_extensions[] = {
722 { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) },
723 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
724 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
728 static const struct dri2_extension_match swrast_driver_extensions[] = {
729 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
730 { __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) },
734 static const struct dri2_extension_match swrast_core_extensions[] = {
735 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
739 static const struct dri2_extension_match optional_driver_extensions[] = {
740 { __DRI_CONFIG_OPTIONS, 1, offsetof(struct dri2_egl_display, configOptions) },
744 static const struct dri2_extension_match optional_core_extensions[] = {
745 { __DRI2_ROBUSTNESS, 1, offsetof(struct dri2_egl_display, robustness) },
746 { __DRI2_NO_ERROR, 1, offsetof(struct dri2_egl_display, no_error) },
747 { __DRI2_CONFIG_QUERY, 1, offsetof(struct dri2_egl_display, config) },
748 { __DRI2_FENCE, 1, offsetof(struct dri2_egl_display, fence) },
749 { __DRI2_BUFFER_DAMAGE, 1, offsetof(struct dri2_egl_display, buffer_damage) },
750 { __DRI2_RENDERER_QUERY, 1, offsetof(struct dri2_egl_display, rendererQuery) },
751 { __DRI2_INTEROP, 1, offsetof(struct dri2_egl_display, interop) },
752 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
753 { __DRI2_FLUSH_CONTROL, 1, offsetof(struct dri2_egl_display, flush_control) },
754 { __DRI2_BLOB, 1, offsetof(struct dri2_egl_display, blob) },
755 { __DRI_MUTABLE_RENDER_BUFFER_DRIVER, 1, offsetof(struct dri2_egl_display, mutable_render_buffer) },
760 dri2_bind_extensions(struct dri2_egl_display *dri2_dpy,
761 const struct dri2_extension_match *matches,
762 const __DRIextension **extensions,
768 for (int i = 0; extensions[i]; i++) {
769 _eglLog(_EGL_DEBUG, "found extension `%s'", extensions[i]->name);
770 for (int j = 0; matches[j].name; j++) {
771 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
772 extensions[i]->version >= matches[j].version) {
773 field = ((char *) dri2_dpy + matches[j].offset);
774 *(const __DRIextension **) field = extensions[i];
775 _eglLog(_EGL_INFO, "found extension %s version %d",
776 extensions[i]->name, extensions[i]->version);
782 for (int j = 0; matches[j].name; j++) {
783 field = ((char *) dri2_dpy + matches[j].offset);
784 if (*(const __DRIextension **) field == NULL) {
786 _eglLog(_EGL_DEBUG, "did not find optional extension %s version %d",
787 matches[j].name, matches[j].version);
789 _eglLog(_EGL_WARNING, "did not find extension %s version %d",
790 matches[j].name, matches[j].version);
799 static const __DRIextension **
800 dri2_open_driver(_EGLDisplay *disp)
802 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
803 static const char *search_path_vars[] = {
804 "LIBGL_DRIVERS_PATH",
808 return loader_open_driver(dri2_dpy->driver_name,
814 dri2_load_driver_common(_EGLDisplay *disp,
815 const struct dri2_extension_match *driver_extensions)
817 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
818 const __DRIextension **extensions;
820 extensions = dri2_open_driver(disp);
824 if (!dri2_bind_extensions(dri2_dpy, driver_extensions, extensions, false)) {
825 dlclose(dri2_dpy->driver);
826 dri2_dpy->driver = NULL;
829 dri2_dpy->driver_extensions = extensions;
831 dri2_bind_extensions(dri2_dpy, optional_driver_extensions, extensions, true);
837 dri2_load_driver(_EGLDisplay *disp)
839 return dri2_load_driver_common(disp, dri2_driver_extensions);
843 dri2_load_driver_dri3(_EGLDisplay *disp)
845 return dri2_load_driver_common(disp, dri3_driver_extensions);
849 dri2_load_driver_swrast(_EGLDisplay *disp)
851 return dri2_load_driver_common(disp, swrast_driver_extensions);
855 dri2_renderer_query_integer(struct dri2_egl_display *dri2_dpy, int param)
857 const __DRI2rendererQueryExtension *rendererQuery = dri2_dpy->rendererQuery;
858 unsigned int value = 0;
860 if (!rendererQuery ||
861 rendererQuery->queryInteger(dri2_dpy->dri_screen, param, &value) == -1)
868 dri2_query_driver_name(_EGLDisplay *disp)
870 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
871 return dri2_dpy->driver_name;
875 dri2_query_driver_config(_EGLDisplay *disp)
877 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
878 const __DRIconfigOptionsExtension *ext = dri2_dpy->configOptions;
880 if (ext->base.version >= 2)
881 return ext->getXml(dri2_dpy->driver_name);
883 return strdup(ext->xml);
888 dri2_setup_screen(_EGLDisplay *disp)
890 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
891 unsigned int api_mask;
894 * EGL 1.5 specification defines the default value to 1. Moreover,
895 * eglSwapInterval() is required to clamp requested value to the supported
896 * range. Since the default value is implicitly assumed to be supported,
897 * use it as both minimum and maximum for the platforms that do not allow
898 * changing the interval. Platforms, which allow it (e.g. x11, wayland)
899 * override these values already.
901 dri2_dpy->min_swap_interval = 1;
902 dri2_dpy->max_swap_interval = 1;
903 dri2_dpy->default_swap_interval = 1;
905 if (dri2_dpy->image_driver) {
906 api_mask = dri2_dpy->image_driver->getAPIMask(dri2_dpy->dri_screen);
907 } else if (dri2_dpy->dri2) {
908 api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);
910 assert(dri2_dpy->swrast);
911 api_mask = 1 << __DRI_API_OPENGL |
912 1 << __DRI_API_GLES |
913 1 << __DRI_API_GLES2 |
914 1 << __DRI_API_GLES3;
917 disp->ClientAPIs = 0;
918 if ((api_mask & (1 <<__DRI_API_OPENGL)) && _eglIsApiValid(EGL_OPENGL_API))
919 disp->ClientAPIs |= EGL_OPENGL_BIT;
920 if ((api_mask & (1 << __DRI_API_GLES)) && _eglIsApiValid(EGL_OPENGL_ES_API))
921 disp->ClientAPIs |= EGL_OPENGL_ES_BIT;
922 if ((api_mask & (1 << __DRI_API_GLES2)) && _eglIsApiValid(EGL_OPENGL_ES_API))
923 disp->ClientAPIs |= EGL_OPENGL_ES2_BIT;
924 if ((api_mask & (1 << __DRI_API_GLES3)) && _eglIsApiValid(EGL_OPENGL_ES_API))
925 disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR;
927 assert(dri2_dpy->image_driver || dri2_dpy->dri2 || dri2_dpy->swrast);
928 disp->Extensions.KHR_no_config_context = EGL_TRUE;
929 disp->Extensions.KHR_surfaceless_context = EGL_TRUE;
931 if (dri2_dpy->configOptions) {
932 disp->Extensions.MESA_query_driver = EGL_TRUE;
935 /* Report back to EGL the bitmask of priorities supported */
936 disp->Extensions.IMG_context_priority =
937 dri2_renderer_query_integer(dri2_dpy,
938 __DRI2_RENDERER_HAS_CONTEXT_PRIORITY);
940 disp->Extensions.EXT_pixel_format_float = EGL_TRUE;
942 if (dri2_renderer_query_integer(dri2_dpy,
943 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB))
944 disp->Extensions.KHR_gl_colorspace = EGL_TRUE;
946 if (dri2_dpy->image_driver ||
947 (dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) ||
948 (dri2_dpy->swrast && dri2_dpy->swrast->base.version >= 3)) {
949 disp->Extensions.KHR_create_context = EGL_TRUE;
951 if (dri2_dpy->robustness)
952 disp->Extensions.EXT_create_context_robustness = EGL_TRUE;
955 if (dri2_dpy->no_error)
956 disp->Extensions.KHR_create_context_no_error = EGL_TRUE;
958 if (dri2_dpy->fence) {
959 disp->Extensions.KHR_fence_sync = EGL_TRUE;
960 disp->Extensions.KHR_wait_sync = EGL_TRUE;
961 if (dri2_dpy->fence->get_fence_from_cl_event)
962 disp->Extensions.KHR_cl_event2 = EGL_TRUE;
963 if (dri2_dpy->fence->base.version >= 2 &&
964 dri2_dpy->fence->get_capabilities) {
965 unsigned capabilities =
966 dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen);
967 disp->Extensions.ANDROID_native_fence_sync =
968 (capabilities & __DRI_FENCE_CAP_NATIVE_FD) != 0;
973 disp->Extensions.ANDROID_blob_cache = EGL_TRUE;
975 disp->Extensions.KHR_reusable_sync = EGL_TRUE;
977 if (dri2_dpy->image) {
978 if (dri2_dpy->image->base.version >= 10 &&
979 dri2_dpy->image->getCapabilities != NULL) {
982 capabilities = dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
983 disp->Extensions.MESA_drm_image = (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
985 if (dri2_dpy->image->base.version >= 11)
986 disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
988 disp->Extensions.MESA_drm_image = EGL_TRUE;
989 if (dri2_dpy->image->base.version >= 11)
990 disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
993 disp->Extensions.KHR_image_base = EGL_TRUE;
994 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
995 if (dri2_dpy->image->base.version >= 5 &&
996 dri2_dpy->image->createImageFromTexture) {
997 disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
998 disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
1000 if (dri2_renderer_query_integer(dri2_dpy,
1001 __DRI2_RENDERER_HAS_TEXTURE_3D))
1002 disp->Extensions.KHR_gl_texture_3D_image = EGL_TRUE;
1005 if (dri2_dpy->image->base.version >= 8 &&
1006 dri2_dpy->image->createImageFromDmaBufs) {
1007 disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE;
1008 disp->Extensions.EXT_image_dma_buf_import_modifiers = EGL_TRUE;
1013 if (dri2_dpy->flush_control)
1014 disp->Extensions.KHR_context_flush_control = EGL_TRUE;
1016 if (dri2_dpy->buffer_damage && dri2_dpy->buffer_damage->set_damage_region)
1017 disp->Extensions.KHR_partial_update = EGL_TRUE;
1019 disp->Extensions.EXT_protected_content =
1020 dri2_renderer_query_integer(dri2_dpy,
1021 __DRI2_RENDERER_HAS_PROTECTED_CONTENT);
1025 dri2_setup_swap_interval(_EGLDisplay *disp, int max_swap_interval)
1027 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1028 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1030 /* Allow driconf to override applications.*/
1031 if (dri2_dpy->config)
1032 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
1033 "vblank_mode", &vblank_mode);
1034 switch (vblank_mode) {
1035 case DRI_CONF_VBLANK_NEVER:
1036 dri2_dpy->min_swap_interval = 0;
1037 dri2_dpy->max_swap_interval = 0;
1038 dri2_dpy->default_swap_interval = 0;
1040 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1041 dri2_dpy->min_swap_interval = 1;
1042 dri2_dpy->max_swap_interval = max_swap_interval;
1043 dri2_dpy->default_swap_interval = 1;
1045 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1046 dri2_dpy->min_swap_interval = 0;
1047 dri2_dpy->max_swap_interval = max_swap_interval;
1048 dri2_dpy->default_swap_interval = 0;
1051 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1052 dri2_dpy->min_swap_interval = 0;
1053 dri2_dpy->max_swap_interval = max_swap_interval;
1054 dri2_dpy->default_swap_interval = 1;
1059 /* All platforms but DRM call this function to create the screen and populate
1060 * the driver_configs. DRM inherits that information from its display - GBM.
1063 dri2_create_screen(_EGLDisplay *disp)
1065 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1067 if (dri2_dpy->image_driver) {
1068 dri2_dpy->dri_screen =
1069 dri2_dpy->image_driver->createNewScreen2(0, dri2_dpy->fd,
1070 dri2_dpy->loader_extensions,
1071 dri2_dpy->driver_extensions,
1072 &dri2_dpy->driver_configs,
1074 } else if (dri2_dpy->dri2) {
1075 if (dri2_dpy->dri2->base.version >= 4) {
1076 dri2_dpy->dri_screen =
1077 dri2_dpy->dri2->createNewScreen2(0, dri2_dpy->fd,
1078 dri2_dpy->loader_extensions,
1079 dri2_dpy->driver_extensions,
1080 &dri2_dpy->driver_configs, disp);
1082 dri2_dpy->dri_screen =
1083 dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd,
1084 dri2_dpy->loader_extensions,
1085 &dri2_dpy->driver_configs, disp);
1088 assert(dri2_dpy->swrast);
1089 if (dri2_dpy->swrast->base.version >= 4) {
1090 dri2_dpy->dri_screen =
1091 dri2_dpy->swrast->createNewScreen2(0, dri2_dpy->loader_extensions,
1092 dri2_dpy->driver_extensions,
1093 &dri2_dpy->driver_configs, disp);
1095 dri2_dpy->dri_screen =
1096 dri2_dpy->swrast->createNewScreen(0, dri2_dpy->loader_extensions,
1097 &dri2_dpy->driver_configs, disp);
1101 if (dri2_dpy->dri_screen == NULL) {
1102 _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen");
1106 dri2_dpy->own_dri_screen = true;
1111 dri2_setup_extensions(_EGLDisplay *disp)
1113 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1114 const struct dri2_extension_match *mandatory_core_extensions;
1115 const __DRIextension **extensions;
1117 extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
1119 if (dri2_dpy->image_driver || dri2_dpy->dri2)
1120 mandatory_core_extensions = dri2_core_extensions;
1122 mandatory_core_extensions = swrast_core_extensions;
1124 if (!dri2_bind_extensions(dri2_dpy, mandatory_core_extensions, extensions, false))
1127 #ifdef HAVE_DRI3_MODIFIERS
1128 dri2_dpy->multibuffers_available =
1129 (dri2_dpy->dri3_major_version > 1 || (dri2_dpy->dri3_major_version == 1 &&
1130 dri2_dpy->dri3_minor_version >= 2)) &&
1131 (dri2_dpy->present_major_version > 1 || (dri2_dpy->present_major_version == 1 &&
1132 dri2_dpy->present_minor_version >= 2)) &&
1133 (dri2_dpy->image && dri2_dpy->image->base.version >= 15);
1136 dri2_bind_extensions(dri2_dpy, optional_core_extensions, extensions, true);
1141 * Called via eglInitialize(), drv->Initialize().
1143 * This must be guaranteed to be called exactly once, even if eglInitialize is
1144 * called many times (without a eglTerminate in between).
1147 dri2_initialize(_EGLDisplay *disp)
1149 EGLBoolean ret = EGL_FALSE;
1150 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1152 /* In the case where the application calls eglMakeCurrent(context1),
1153 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
1154 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
1155 * initialized, as we need it to be able to free context1 correctly.
1157 * It would probably be safest to forcibly release the display with
1158 * dri2_display_release, to make sure the display is reinitialized correctly.
1159 * However, the EGL spec states that we need to keep a reference to the
1160 * current context (so we cannot call dri2_make_current(NULL)), and therefore
1161 * we would leak context1 as we would be missing the old display connection
1162 * to free it up correctly.
1165 dri2_dpy->ref_count++;
1169 loader_set_logger(_eglLog);
1171 switch (disp->Platform) {
1172 case _EGL_PLATFORM_SURFACELESS:
1173 ret = dri2_initialize_surfaceless(disp);
1175 case _EGL_PLATFORM_DEVICE:
1176 ret = dri2_initialize_device(disp);
1178 case _EGL_PLATFORM_X11:
1179 case _EGL_PLATFORM_XCB:
1180 ret = dri2_initialize_x11(disp);
1182 case _EGL_PLATFORM_DRM:
1183 ret = dri2_initialize_drm(disp);
1185 case _EGL_PLATFORM_WAYLAND:
1186 ret = dri2_initialize_wayland(disp);
1188 case _EGL_PLATFORM_ANDROID:
1189 ret = dri2_initialize_android(disp);
1192 unreachable("Callers ensure we cannot get here.");
1199 dri2_dpy = dri2_egl_display(disp);
1200 dri2_dpy->ref_count++;
1206 * Decrement display reference count, and free up display if necessary.
1209 dri2_display_release(_EGLDisplay *disp)
1211 struct dri2_egl_display *dri2_dpy;
1216 dri2_dpy = dri2_egl_display(disp);
1218 assert(dri2_dpy->ref_count > 0);
1219 dri2_dpy->ref_count--;
1221 if (dri2_dpy->ref_count > 0)
1224 _eglCleanupDisplay(disp);
1225 dri2_display_destroy(disp);
1229 dri2_display_destroy(_EGLDisplay *disp)
1231 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1233 if (dri2_dpy->own_dri_screen) {
1234 if (dri2_dpy->vtbl && dri2_dpy->vtbl->close_screen_notify)
1235 dri2_dpy->vtbl->close_screen_notify(disp);
1236 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1238 if (dri2_dpy->fd >= 0)
1239 close(dri2_dpy->fd);
1240 if (dri2_dpy->driver)
1241 dlclose(dri2_dpy->driver);
1242 free(dri2_dpy->driver_name);
1244 #ifdef HAVE_WAYLAND_PLATFORM
1245 free(dri2_dpy->device_name);
1248 switch (disp->Platform) {
1249 case _EGL_PLATFORM_X11:
1250 dri2_teardown_x11(dri2_dpy);
1252 case _EGL_PLATFORM_DRM:
1253 dri2_teardown_drm(dri2_dpy);
1255 case _EGL_PLATFORM_WAYLAND:
1256 dri2_teardown_wayland(dri2_dpy);
1259 /* TODO: add teardown for other platforms */
1263 /* The drm platform does not create the screen/driver_configs but reuses
1264 * the ones from the gbm device. As such the gbm itself is responsible
1267 if (disp->Platform != _EGL_PLATFORM_DRM && dri2_dpy->driver_configs) {
1268 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++)
1269 free((__DRIconfig *) dri2_dpy->driver_configs[i]);
1270 free(dri2_dpy->driver_configs);
1273 disp->DriverData = NULL;
1277 dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
1278 unsigned int att, unsigned int format)
1280 struct dri2_egl_display *dri2_dpy =
1281 dri2_egl_display(dri2_surf->base.Resource.Display);
1283 if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
1286 if (!dri2_surf->local_buffers[att]) {
1287 dri2_surf->local_buffers[att] =
1288 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
1289 dri2_surf->base.Width, dri2_surf->base.Height);
1292 return dri2_surf->local_buffers[att];
1296 dri2_egl_surface_free_local_buffers(struct dri2_egl_surface *dri2_surf)
1298 struct dri2_egl_display *dri2_dpy =
1299 dri2_egl_display(dri2_surf->base.Resource.Display);
1301 for (int i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
1302 if (dri2_surf->local_buffers[i]) {
1303 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
1304 dri2_surf->local_buffers[i]);
1305 dri2_surf->local_buffers[i] = NULL;
1311 * Called via eglTerminate(), drv->Terminate().
1313 * This must be guaranteed to be called exactly once, even if eglTerminate is
1314 * called many times (without a eglInitialize in between).
1317 dri2_terminate(_EGLDisplay *disp)
1319 /* Release all non-current Context/Surfaces. */
1320 _eglReleaseDisplayResources(disp);
1322 dri2_display_release(disp);
1328 * Set the error code after a call to
1329 * dri2_egl_display::dri2::createContextAttribs.
1332 dri2_create_context_attribs_error(int dri_error)
1336 switch (dri_error) {
1337 case __DRI_CTX_ERROR_SUCCESS:
1340 case __DRI_CTX_ERROR_NO_MEMORY:
1341 egl_error = EGL_BAD_ALLOC;
1344 /* From the EGL_KHR_create_context spec, section "Errors":
1346 * * If <config> does not support a client API context compatible
1347 * with the requested API major and minor version, [...] context flags,
1348 * and context reset notification behavior (for client API types where
1349 * these attributes are supported), then an EGL_BAD_MATCH error is
1352 * * If an OpenGL ES context is requested and the values for
1353 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1354 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1355 * is not defined, than an EGL_BAD_MATCH error is generated.
1357 * * If an OpenGL context is requested, the requested version is
1358 * greater than 3.2, and the value for attribute
1359 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1360 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1361 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1362 * one of these bits set; or if the implementation does not support
1363 * the requested profile, then an EGL_BAD_MATCH error is generated.
1365 case __DRI_CTX_ERROR_BAD_API:
1366 case __DRI_CTX_ERROR_BAD_VERSION:
1367 case __DRI_CTX_ERROR_BAD_FLAG:
1368 egl_error = EGL_BAD_MATCH;
1371 /* From the EGL_KHR_create_context spec, section "Errors":
1373 * * If an attribute name or attribute value in <attrib_list> is not
1374 * recognized (including unrecognized bits in bitmask attributes),
1375 * then an EGL_BAD_ATTRIBUTE error is generated."
1377 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE:
1378 case __DRI_CTX_ERROR_UNKNOWN_FLAG:
1379 egl_error = EGL_BAD_ATTRIBUTE;
1383 assert(!"unknown dri_error code");
1384 egl_error = EGL_BAD_MATCH;
1388 _eglError(egl_error, "dri2_create_context");
1392 dri2_fill_context_attribs(struct dri2_egl_context *dri2_ctx,
1393 struct dri2_egl_display *dri2_dpy,
1394 uint32_t *ctx_attribs,
1395 unsigned *num_attribs)
1399 assert(*num_attribs >= NUM_ATTRIBS);
1401 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
1402 ctx_attribs[pos++] = dri2_ctx->base.ClientMajorVersion;
1403 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
1404 ctx_attribs[pos++] = dri2_ctx->base.ClientMinorVersion;
1406 if (dri2_ctx->base.Flags != 0 || dri2_ctx->base.NoError) {
1407 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1408 * extension, don't even try to send it the robust-access flag.
1409 * It may explode. Instead, generate the required EGL error here.
1411 if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0
1412 && !dri2_dpy->robustness) {
1413 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1417 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_FLAGS;
1418 ctx_attribs[pos++] = dri2_ctx->base.Flags |
1419 (dri2_ctx->base.NoError ? __DRI_CTX_FLAG_NO_ERROR : 0);
1422 if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) {
1423 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1424 * extension, don't even try to send it a reset strategy. It may
1425 * explode. Instead, generate the required EGL error here.
1427 if (!dri2_dpy->robustness) {
1428 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1432 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
1433 ctx_attribs[pos++] = __DRI_CTX_RESET_LOSE_CONTEXT;
1436 if (dri2_ctx->base.ContextPriority != EGL_CONTEXT_PRIORITY_MEDIUM_IMG) {
1439 switch (dri2_ctx->base.ContextPriority) {
1440 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
1441 val = __DRI_CTX_PRIORITY_HIGH;
1443 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
1444 val = __DRI_CTX_PRIORITY_MEDIUM;
1446 case EGL_CONTEXT_PRIORITY_LOW_IMG:
1447 val = __DRI_CTX_PRIORITY_LOW;
1450 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1454 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_PRIORITY;
1455 ctx_attribs[pos++] = val;
1458 if (dri2_ctx->base.ReleaseBehavior == EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR) {
1459 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
1460 ctx_attribs[pos++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE;
1469 * Called via eglCreateContext(), drv->CreateContext().
1471 static _EGLContext *
1472 dri2_create_context(_EGLDisplay *disp, _EGLConfig *conf,
1473 _EGLContext *share_list, const EGLint *attrib_list)
1475 struct dri2_egl_context *dri2_ctx;
1476 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1477 struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
1478 __DRIcontext *shared =
1479 dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL;
1480 struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
1481 const __DRIconfig *dri_config;
1484 unsigned num_attribs = NUM_ATTRIBS;
1485 uint32_t ctx_attribs[NUM_ATTRIBS];
1487 dri2_ctx = malloc(sizeof *dri2_ctx);
1489 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
1493 if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
1496 /* The EGL_EXT_create_context_robustness spec says:
1498 * "Add to the eglCreateContext context creation errors: [...]
1500 * * If the reset notification behavior of <share_context> and the
1501 * newly created context are different then an EGL_BAD_MATCH error is
1504 if (share_list && share_list->ResetNotificationStrategy !=
1505 dri2_ctx->base.ResetNotificationStrategy) {
1506 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1510 /* The EGL_KHR_create_context_no_error spec says:
1512 * "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
1513 * used to create <share_context> does not match the value of
1514 * EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
1516 if (share_list && share_list->NoError != dri2_ctx->base.NoError) {
1517 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1521 switch (dri2_ctx->base.ClientAPI) {
1522 case EGL_OPENGL_ES_API:
1523 switch (dri2_ctx->base.ClientMajorVersion) {
1525 api = __DRI_API_GLES;
1528 api = __DRI_API_GLES2;
1531 api = __DRI_API_GLES3;
1534 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1539 case EGL_OPENGL_API:
1540 if ((dri2_ctx->base.ClientMajorVersion >= 4
1541 || (dri2_ctx->base.ClientMajorVersion == 3
1542 && dri2_ctx->base.ClientMinorVersion >= 2))
1543 && dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)
1544 api = __DRI_API_OPENGL_CORE;
1545 else if (dri2_ctx->base.ClientMajorVersion == 3 &&
1546 dri2_ctx->base.ClientMinorVersion == 1)
1547 api = __DRI_API_OPENGL_CORE;
1549 api = __DRI_API_OPENGL;
1552 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1558 /* The config chosen here isn't necessarily
1559 * used for surfaces later.
1560 * A pixmap surface will use the single config.
1561 * This opportunity depends on disabling the
1562 * doubleBufferMode check in
1563 * src/mesa/main/context.c:check_compatible()
1565 if (dri2_config->dri_config[1][0])
1566 dri_config = dri2_config->dri_config[1][0];
1568 dri_config = dri2_config->dri_config[0][0];
1573 if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs,
1577 if (dri2_dpy->image_driver) {
1578 dri2_ctx->dri_context =
1579 dri2_dpy->image_driver->createContextAttribs(dri2_dpy->dri_screen,
1587 dri2_create_context_attribs_error(error);
1588 } else if (dri2_dpy->dri2) {
1589 if (dri2_dpy->dri2->base.version >= 3) {
1590 dri2_ctx->dri_context =
1591 dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen,
1599 dri2_create_context_attribs_error(error);
1601 dri2_ctx->dri_context =
1602 dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
1609 assert(dri2_dpy->swrast);
1610 if (dri2_dpy->swrast->base.version >= 3) {
1611 dri2_ctx->dri_context =
1612 dri2_dpy->swrast->createContextAttribs(dri2_dpy->dri_screen,
1620 dri2_create_context_attribs_error(error);
1622 dri2_ctx->dri_context =
1623 dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,
1631 if (!dri2_ctx->dri_context)
1634 return &dri2_ctx->base;
1642 * Called via eglDestroyContext(), drv->DestroyContext().
1645 dri2_destroy_context(_EGLDisplay *disp, _EGLContext *ctx)
1647 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1648 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1650 if (_eglPutContext(ctx)) {
1651 dri2_dpy->core->destroyContext(dri2_ctx->dri_context);
1659 dri2_init_surface(_EGLSurface *surf, _EGLDisplay *disp, EGLint type,
1660 _EGLConfig *conf, const EGLint *attrib_list,
1661 EGLBoolean enable_out_fence, void *native_surface)
1663 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1664 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1666 dri2_surf->out_fence_fd = -1;
1667 dri2_surf->enable_out_fence = false;
1668 if (dri2_dpy->fence && dri2_dpy->fence->base.version >= 2 &&
1669 dri2_dpy->fence->get_capabilities &&
1670 (dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen) &
1671 __DRI_FENCE_CAP_NATIVE_FD)) {
1672 dri2_surf->enable_out_fence = enable_out_fence;
1675 return _eglInitSurface(surf, disp, type, conf, attrib_list, native_surface);
1679 dri2_surface_set_out_fence_fd( _EGLSurface *surf, int fence_fd)
1681 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1683 if (dri2_surf->out_fence_fd >= 0)
1684 close(dri2_surf->out_fence_fd);
1686 dri2_surf->out_fence_fd = fence_fd;
1690 dri2_fini_surface(_EGLSurface *surf)
1692 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1694 dri2_surface_set_out_fence_fd(surf, -1);
1695 dri2_surf->enable_out_fence = false;
1699 dri2_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
1701 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1703 if (!_eglPutSurface(surf))
1706 return dri2_dpy->vtbl->destroy_surface(disp, surf);
1710 dri2_surf_update_fence_fd(_EGLContext *ctx,
1711 _EGLDisplay *disp, _EGLSurface *surf)
1713 __DRIcontext *dri_ctx = dri2_egl_context(ctx)->dri_context;
1714 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1715 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1719 if (!dri2_surf->enable_out_fence)
1722 fence = dri2_dpy->fence->create_fence_fd(dri_ctx, -1);
1724 fence_fd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
1726 dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, fence);
1728 dri2_surface_set_out_fence_fd(surf, fence_fd);
1732 dri2_create_drawable(struct dri2_egl_display *dri2_dpy,
1733 const __DRIconfig *config,
1734 struct dri2_egl_surface *dri2_surf,
1735 void *loaderPrivate)
1737 __DRIcreateNewDrawableFunc createNewDrawable;
1739 if (dri2_dpy->image_driver)
1740 createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
1741 else if (dri2_dpy->dri2)
1742 createNewDrawable = dri2_dpy->dri2->createNewDrawable;
1743 else if (dri2_dpy->swrast)
1744 createNewDrawable = dri2_dpy->swrast->createNewDrawable;
1746 return _eglError(EGL_BAD_ALLOC, "no createNewDrawable");
1748 dri2_surf->dri_drawable = createNewDrawable(dri2_dpy->dri_screen,
1749 config, loaderPrivate);
1750 if (dri2_surf->dri_drawable == NULL)
1751 return _eglError(EGL_BAD_ALLOC, "createNewDrawable");
1757 * Called via eglMakeCurrent(), drv->MakeCurrent().
1760 dri2_make_current(_EGLDisplay *disp, _EGLSurface *dsurf,
1761 _EGLSurface *rsurf, _EGLContext *ctx)
1763 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1764 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1765 _EGLDisplay *old_disp = NULL;
1766 struct dri2_egl_display *old_dri2_dpy = NULL;
1767 _EGLContext *old_ctx;
1768 _EGLSurface *old_dsurf, *old_rsurf;
1769 _EGLSurface *tmp_dsurf, *tmp_rsurf;
1770 __DRIdrawable *ddraw, *rdraw;
1772 EGLint egl_error = EGL_SUCCESS;
1775 return _eglError(EGL_NOT_INITIALIZED, "eglMakeCurrent");
1777 /* make new bindings, set the EGL error otherwise */
1778 if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))
1782 __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context;
1783 old_disp = old_ctx->Resource.Display;
1784 old_dri2_dpy = dri2_egl_display(old_disp);
1786 /* flush before context switch */
1790 dri2_surf_update_fence_fd(old_ctx, disp, old_dsurf);
1792 /* Disable shared buffer mode */
1793 if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1794 old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1795 old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, false);
1798 dri2_dpy->core->unbindContext(old_cctx);
1801 ddraw = (dsurf) ? dri2_dpy->vtbl->get_dri_drawable(dsurf) : NULL;
1802 rdraw = (rsurf) ? dri2_dpy->vtbl->get_dri_drawable(rsurf) : NULL;
1803 cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
1805 if (cctx || ddraw || rdraw) {
1806 if (!dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1807 _EGLContext *tmp_ctx;
1809 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1810 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1813 egl_error = EGL_BAD_MATCH;
1815 /* undo the previous _eglBindContext */
1816 _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &tmp_dsurf, &tmp_rsurf);
1817 assert(&dri2_ctx->base == ctx &&
1818 tmp_dsurf == dsurf &&
1819 tmp_rsurf == rsurf);
1821 _eglPutSurface(dsurf);
1822 _eglPutSurface(rsurf);
1823 _eglPutContext(ctx);
1825 _eglPutSurface(old_dsurf);
1826 _eglPutSurface(old_rsurf);
1827 _eglPutContext(old_ctx);
1829 ddraw = (old_dsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_dsurf) : NULL;
1830 rdraw = (old_rsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_rsurf) : NULL;
1831 cctx = (old_ctx) ? dri2_egl_context(old_ctx)->dri_context : NULL;
1833 /* undo the previous dri2_dpy->core->unbindContext */
1834 if (dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1835 if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1836 old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1837 old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, true);
1840 return _eglError(egl_error, "eglMakeCurrent");
1843 /* We cannot restore the same state as it was before calling
1844 * eglMakeCurrent() and the spec isn't clear about what to do. We
1845 * can prevent EGL from calling into the DRI driver with no DRI
1848 dsurf = rsurf = NULL;
1851 _eglBindContext(ctx, dsurf, rsurf, &tmp_ctx, &tmp_dsurf, &tmp_rsurf);
1852 assert(tmp_ctx == old_ctx && tmp_dsurf == old_dsurf &&
1853 tmp_rsurf == old_rsurf);
1855 _eglLog(_EGL_WARNING, "DRI2: failed to rebind the previous context");
1857 /* dri2_dpy->core->bindContext succeeded, so take a reference on the
1858 * dri2_dpy. This prevents dri2_dpy from being reinitialized when a
1859 * EGLDisplay is terminated and then initialized again while a
1860 * context is still bound. See dri2_intitialize() for a more in depth
1862 dri2_dpy->ref_count++;
1866 dri2_destroy_surface(disp, old_dsurf);
1867 dri2_destroy_surface(disp, old_rsurf);
1870 dri2_destroy_context(disp, old_ctx);
1871 dri2_display_release(old_disp);
1874 if (egl_error != EGL_SUCCESS)
1875 return _eglError(egl_error, "eglMakeCurrent");
1877 if (dsurf && _eglSurfaceHasMutableRenderBuffer(dsurf) &&
1878 dri2_dpy->vtbl->set_shared_buffer_mode) {
1879 /* Always update the shared buffer mode. This is obviously needed when
1880 * the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When
1881 * EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the
1882 * case where external non-EGL API may have changed window's shared
1883 * buffer mode since we last saw it.
1885 bool mode = (dsurf->ActiveRenderBuffer == EGL_SINGLE_BUFFER);
1886 dri2_dpy->vtbl->set_shared_buffer_mode(disp, dsurf, mode);
1893 dri2_surface_get_dri_drawable(_EGLSurface *surf)
1895 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1897 return dri2_surf->dri_drawable;
1901 * Called from eglGetProcAddress() via drv->GetProcAddress().
1904 dri2_get_proc_address(const char *procname)
1906 return _glapi_get_proc_address(procname);
1910 dri2_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
1911 void *native_window, const EGLint *attrib_list)
1913 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1914 return dri2_dpy->vtbl->create_window_surface(disp, conf, native_window,
1919 dri2_create_pixmap_surface(_EGLDisplay *disp, _EGLConfig *conf,
1920 void *native_pixmap, const EGLint *attrib_list)
1922 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1923 if (!dri2_dpy->vtbl->create_pixmap_surface)
1925 return dri2_dpy->vtbl->create_pixmap_surface(disp, conf, native_pixmap,
1930 dri2_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
1931 const EGLint *attrib_list)
1933 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1934 if (!dri2_dpy->vtbl->create_pbuffer_surface)
1936 return dri2_dpy->vtbl->create_pbuffer_surface(disp, conf, attrib_list);
1940 dri2_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
1942 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1943 if (!dri2_dpy->vtbl->swap_interval)
1945 return dri2_dpy->vtbl->swap_interval(disp, surf, interval);
1949 * Asks the client API to flush any rendering to the drawable so that we can
1950 * do our swapbuffers.
1953 dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw)
1955 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1956 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(draw);
1958 if (dri2_dpy->flush) {
1959 if (dri2_dpy->flush->base.version >= 4) {
1960 /* We know there's a current context because:
1962 * "If surface is not bound to the calling thread’s current
1963 * context, an EGL_BAD_SURFACE error is generated."
1965 _EGLContext *ctx = _eglGetCurrentContext();
1966 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1968 /* From the EGL 1.4 spec (page 52):
1970 * "The contents of ancillary buffers are always undefined
1971 * after calling eglSwapBuffers."
1973 dri2_dpy->flush->flush_with_flags(dri2_ctx->dri_context,
1975 __DRI2_FLUSH_DRAWABLE |
1976 __DRI2_FLUSH_INVALIDATE_ANCILLARY,
1977 __DRI2_THROTTLE_SWAPBUFFER);
1979 dri2_dpy->flush->flush(dri_drawable);
1985 dri2_swap_buffers(_EGLDisplay *disp, _EGLSurface *surf)
1987 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1988 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
1989 _EGLContext *ctx = _eglGetCurrentContext();
1993 dri2_surf_update_fence_fd(ctx, disp, surf);
1994 ret = dri2_dpy->vtbl->swap_buffers(disp, surf);
1996 /* SwapBuffers marks the end of the frame; reset the damage region for
1997 * use again next time.
1999 if (ret && dri2_dpy->buffer_damage &&
2000 dri2_dpy->buffer_damage->set_damage_region)
2001 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2007 dri2_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *surf,
2008 const EGLint *rects, EGLint n_rects)
2010 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2011 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2012 _EGLContext *ctx = _eglGetCurrentContext();
2016 dri2_surf_update_fence_fd(ctx, disp, surf);
2017 if (dri2_dpy->vtbl->swap_buffers_with_damage)
2018 ret = dri2_dpy->vtbl->swap_buffers_with_damage(disp, surf,
2021 ret = dri2_dpy->vtbl->swap_buffers(disp, surf);
2023 /* SwapBuffers marks the end of the frame; reset the damage region for
2024 * use again next time.
2026 if (ret && dri2_dpy->buffer_damage &&
2027 dri2_dpy->buffer_damage->set_damage_region)
2028 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2034 dri2_swap_buffers_region(_EGLDisplay *disp, _EGLSurface *surf,
2035 EGLint numRects, const EGLint *rects)
2037 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2038 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2041 if (!dri2_dpy->vtbl->swap_buffers_region)
2043 ret = dri2_dpy->vtbl->swap_buffers_region(disp, surf, numRects, rects);
2045 /* SwapBuffers marks the end of the frame; reset the damage region for
2046 * use again next time.
2048 if (ret && dri2_dpy->buffer_damage &&
2049 dri2_dpy->buffer_damage->set_damage_region)
2050 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2056 dri2_set_damage_region(_EGLDisplay *disp, _EGLSurface *surf,
2057 EGLint *rects, EGLint n_rects)
2059 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2060 __DRIdrawable *drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2062 if (!dri2_dpy->buffer_damage || !dri2_dpy->buffer_damage->set_damage_region)
2065 dri2_dpy->buffer_damage->set_damage_region(drawable, n_rects, rects);
2070 dri2_post_sub_buffer(_EGLDisplay *disp, _EGLSurface *surf,
2071 EGLint x, EGLint y, EGLint width, EGLint height)
2073 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2074 if (!dri2_dpy->vtbl->post_sub_buffer)
2076 return dri2_dpy->vtbl->post_sub_buffer(disp, surf, x, y, width, height);
2080 dri2_copy_buffers(_EGLDisplay *disp, _EGLSurface *surf, void *native_pixmap_target)
2082 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2083 if (!dri2_dpy->vtbl->copy_buffers)
2084 return _eglError(EGL_BAD_NATIVE_PIXMAP, "no support for native pixmaps");
2085 return dri2_dpy->vtbl->copy_buffers(disp, surf, native_pixmap_target);
2089 dri2_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surf)
2091 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2092 if (!dri2_dpy->vtbl->query_buffer_age)
2094 return dri2_dpy->vtbl->query_buffer_age(disp, surf);
2098 dri2_wait_client(_EGLDisplay *disp, _EGLContext *ctx)
2100 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2101 _EGLSurface *surf = ctx->DrawSurface;
2102 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2104 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
2105 * we need to copy fake to real here.*/
2107 if (dri2_dpy->flush != NULL)
2108 dri2_dpy->flush->flush(dri_drawable);
2114 dri2_wait_native(EGLint engine)
2116 if (engine != EGL_CORE_NATIVE_ENGINE)
2117 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
2124 dri2_bind_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
2126 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2127 struct dri2_egl_context *dri2_ctx;
2129 GLint format, target;
2130 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2132 ctx = _eglGetCurrentContext();
2133 dri2_ctx = dri2_egl_context(ctx);
2135 if (!_eglBindTexImage(disp, surf, buffer))
2138 switch (surf->TextureFormat) {
2139 case EGL_TEXTURE_RGB:
2140 format = __DRI_TEXTURE_FORMAT_RGB;
2142 case EGL_TEXTURE_RGBA:
2143 format = __DRI_TEXTURE_FORMAT_RGBA;
2146 assert(!"Unexpected texture format in dri2_bind_tex_image()");
2147 format = __DRI_TEXTURE_FORMAT_RGBA;
2150 switch (surf->TextureTarget) {
2151 case EGL_TEXTURE_2D:
2152 target = GL_TEXTURE_2D;
2155 target = GL_TEXTURE_2D;
2156 assert(!"Unexpected texture target in dri2_bind_tex_image()");
2159 dri2_dpy->tex_buffer->setTexBuffer2(dri2_ctx->dri_context,
2167 dri2_release_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
2169 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2170 struct dri2_egl_context *dri2_ctx;
2173 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2175 ctx = _eglGetCurrentContext();
2176 dri2_ctx = dri2_egl_context(ctx);
2178 if (!_eglReleaseTexImage(disp, surf, buffer))
2181 switch (surf->TextureTarget) {
2182 case EGL_TEXTURE_2D:
2183 target = GL_TEXTURE_2D;
2186 assert(!"missing texture target");
2189 if (dri2_dpy->tex_buffer->base.version >= 3 &&
2190 dri2_dpy->tex_buffer->releaseTexBuffer != NULL) {
2191 dri2_dpy->tex_buffer->releaseTexBuffer(dri2_ctx->dri_context,
2192 target, dri_drawable);
2199 dri2_create_image(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
2200 EGLClientBuffer buffer, const EGLint *attr_list)
2202 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2203 return dri2_dpy->vtbl->create_image(disp, ctx, target, buffer,
2208 dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image)
2210 struct dri2_egl_image *dri2_img;
2212 if (dri_image == NULL) {
2213 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
2217 dri2_img = malloc(sizeof *dri2_img);
2219 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
2223 _eglInitImage(&dri2_img->base, disp);
2225 dri2_img->dri_image = dri_image;
2227 return &dri2_img->base;
2231 * Translate a DRI Image extension error code into an EGL error code.
2234 egl_error_from_dri_image_error(int dri_error)
2236 switch (dri_error) {
2237 case __DRI_IMAGE_ERROR_SUCCESS:
2239 case __DRI_IMAGE_ERROR_BAD_ALLOC:
2240 return EGL_BAD_ALLOC;
2241 case __DRI_IMAGE_ERROR_BAD_MATCH:
2242 return EGL_BAD_MATCH;
2243 case __DRI_IMAGE_ERROR_BAD_PARAMETER:
2244 return EGL_BAD_PARAMETER;
2245 case __DRI_IMAGE_ERROR_BAD_ACCESS:
2246 return EGL_BAD_ACCESS;
2248 assert(!"unknown dri_error code");
2249 return EGL_BAD_ALLOC;
2254 dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
2255 EGLClientBuffer buffer,
2256 const EGLint *attr_list)
2258 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2259 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2260 GLuint renderbuffer = (GLuint) (uintptr_t) buffer;
2261 __DRIimage *dri_image;
2263 if (renderbuffer == 0) {
2264 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2265 return EGL_NO_IMAGE_KHR;
2268 if (!disp->Extensions.KHR_gl_renderbuffer_image) {
2269 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2270 return EGL_NO_IMAGE_KHR;
2273 if (dri2_dpy->image->base.version >= 17 &&
2274 dri2_dpy->image->createImageFromRenderbuffer2) {
2275 unsigned error = ~0;
2277 dri_image = dri2_dpy->image->createImageFromRenderbuffer2(
2278 dri2_ctx->dri_context, renderbuffer, NULL, &error);
2280 assert(!!dri_image == (error == __DRI_IMAGE_ERROR_SUCCESS));
2283 _eglError(egl_error_from_dri_image_error(error), "dri2_create_image_khr");
2284 return EGL_NO_IMAGE_KHR;
2287 dri_image = dri2_dpy->image->createImageFromRenderbuffer(
2288 dri2_ctx->dri_context, renderbuffer, NULL);
2290 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2291 return EGL_NO_IMAGE_KHR;
2295 return dri2_create_image_from_dri(disp, dri_image);
2298 #ifdef HAVE_WAYLAND_PLATFORM
2300 /* This structure describes how a wl_buffer maps to one or more
2301 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
2302 * offsets and strides of the planes in the buffer. This table maps a
2303 * wl_drm format code to a description of the planes in the buffer
2304 * that lets us create a __DRIimage for each of the planes. */
2306 static const struct wl_drm_components_descriptor {
2307 uint32_t dri_components;
2310 } wl_drm_components[] = {
2311 { __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 },
2312 { __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 },
2313 { __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 },
2314 { __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 },
2315 { __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 },
2319 dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx,
2320 EGLClientBuffer _buffer,
2321 const EGLint *attr_list)
2323 struct wl_drm_buffer *buffer;
2324 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2325 const struct wl_drm_components_descriptor *f;
2326 __DRIimage *dri_image;
2327 _EGLImageAttribs attrs;
2330 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm,
2331 (struct wl_resource *) _buffer);
2335 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2338 plane = attrs.PlaneWL;
2339 f = buffer->driver_format;
2340 if (plane < 0 || plane >= f->nplanes) {
2341 _eglError(EGL_BAD_PARAMETER,
2342 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
2346 dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL);
2347 if (dri_image == NULL && plane == 0)
2348 dri_image = dri2_dpy->image->dupImage(buffer->driver_buffer, NULL);
2349 if (dri_image == NULL) {
2350 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
2354 return dri2_create_image_from_dri(disp, dri_image);
2359 dri2_get_sync_values_chromium(_EGLDisplay *disp, _EGLSurface *surf,
2360 EGLuint64KHR *ust, EGLuint64KHR *msc,
2363 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2364 if (!dri2_dpy->vtbl->get_sync_values)
2366 return dri2_dpy->vtbl->get_sync_values(disp, surf, ust, msc, sbc);
2370 * Set the error code after a call to
2371 * dri2_egl_image::dri_image::createImageFromTexture.
2374 dri2_create_image_khr_texture_error(int dri_error)
2376 EGLint egl_error = egl_error_from_dri_image_error(dri_error);
2378 if (egl_error != EGL_SUCCESS)
2379 _eglError(egl_error, "dri2_create_image_khr_texture");
2383 dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx,
2385 EGLClientBuffer buffer,
2386 const EGLint *attr_list)
2388 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2389 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2390 struct dri2_egl_image *dri2_img;
2391 GLuint texture = (GLuint) (uintptr_t) buffer;
2392 _EGLImageAttribs attrs;
2398 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2399 return EGL_NO_IMAGE_KHR;
2402 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2403 return EGL_NO_IMAGE_KHR;
2406 case EGL_GL_TEXTURE_2D_KHR:
2407 if (!disp->Extensions.KHR_gl_texture_2D_image) {
2408 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2409 return EGL_NO_IMAGE_KHR;
2412 gl_target = GL_TEXTURE_2D;
2414 case EGL_GL_TEXTURE_3D_KHR:
2415 if (!disp->Extensions.KHR_gl_texture_3D_image) {
2416 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2417 return EGL_NO_IMAGE_KHR;
2420 depth = attrs.GLTextureZOffset;
2421 gl_target = GL_TEXTURE_3D;
2423 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
2424 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
2425 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
2426 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
2427 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
2428 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
2429 if (!disp->Extensions.KHR_gl_texture_cubemap_image) {
2430 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2431 return EGL_NO_IMAGE_KHR;
2434 depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
2435 gl_target = GL_TEXTURE_CUBE_MAP;
2438 unreachable("Unexpected target in dri2_create_image_khr_texture()");
2439 return EGL_NO_IMAGE_KHR;
2442 dri2_img = malloc(sizeof *dri2_img);
2444 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2445 return EGL_NO_IMAGE_KHR;
2448 _eglInitImage(&dri2_img->base, disp);
2450 dri2_img->dri_image =
2451 dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context,
2455 attrs.GLTextureLevel,
2458 dri2_create_image_khr_texture_error(error);
2460 if (!dri2_img->dri_image) {
2462 return EGL_NO_IMAGE_KHR;
2464 return &dri2_img->base;
2468 dri2_query_surface(_EGLDisplay *disp, _EGLSurface *surf,
2469 EGLint attribute, EGLint *value)
2471 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2472 if (!dri2_dpy->vtbl->query_surface)
2473 return _eglQuerySurface(disp, surf, attribute, value);
2474 return dri2_dpy->vtbl->query_surface(disp, surf, attribute, value);
2477 static struct wl_buffer*
2478 dri2_create_wayland_buffer_from_image(_EGLDisplay *disp, _EGLImage *img)
2480 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2481 if (!dri2_dpy->vtbl->create_wayland_buffer_from_image)
2483 return dri2_dpy->vtbl->create_wayland_buffer_from_image(disp, img);
2488 dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
2489 EGLClientBuffer buffer, const EGLint *attr_list)
2491 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2492 EGLint format, name, pitch;
2493 _EGLImageAttribs attrs;
2494 __DRIimage *dri_image;
2496 name = (EGLint) (uintptr_t) buffer;
2498 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2501 if (attrs.Width <= 0 || attrs.Height <= 0 ||
2502 attrs.DRMBufferStrideMESA <= 0) {
2503 _eglError(EGL_BAD_PARAMETER,
2504 "bad width, height or stride");
2508 switch (attrs.DRMBufferFormatMESA) {
2509 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2510 format = __DRI_IMAGE_FORMAT_ARGB8888;
2511 pitch = attrs.DRMBufferStrideMESA;
2514 _eglError(EGL_BAD_PARAMETER,
2515 "dri2_create_image_khr: unsupported pixmap depth");
2520 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
2528 return dri2_create_image_from_dri(disp, dri_image);
2532 dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)
2537 * "Required attributes and their values are as follows:
2539 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
2541 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
2542 * by drm_fourcc.h and used as the pixel_format parameter of the
2543 * drm_mode_fb_cmd2 ioctl."
2547 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2548 * incomplete, EGL_BAD_PARAMETER is generated."
2550 if (attrs->Width <= 0 || attrs->Height <= 0 ||
2551 !attrs->DMABufFourCC.IsPresent)
2552 return _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");
2557 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
2558 * specified for a plane's pitch or offset isn't supported by EGL,
2559 * EGL_BAD_ACCESS is generated."
2561 for (unsigned i = 0; i < ARRAY_SIZE(attrs->DMABufPlanePitches); ++i) {
2562 if (attrs->DMABufPlanePitches[i].IsPresent &&
2563 attrs->DMABufPlanePitches[i].Value <= 0)
2564 return _eglError(EGL_BAD_ACCESS, "invalid pitch");
2568 * If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2569 * attribute values may be given.
2571 * This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2572 * EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2574 for (unsigned i = 0; i < DMA_BUF_MAX_PLANES; ++i) {
2575 if (attrs->DMABufPlaneModifiersLo[i].IsPresent !=
2576 attrs->DMABufPlaneModifiersHi[i].IsPresent)
2577 return _eglError(EGL_BAD_PARAMETER, "modifier attribute lo or hi missing");
2580 /* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2581 * mandate it, we only accept the same modifier across all planes. */
2582 for (unsigned i = 1; i < DMA_BUF_MAX_PLANES; ++i) {
2583 if (attrs->DMABufPlaneFds[i].IsPresent) {
2584 if ((attrs->DMABufPlaneModifiersLo[0].IsPresent !=
2585 attrs->DMABufPlaneModifiersLo[i].IsPresent) ||
2586 (attrs->DMABufPlaneModifiersLo[0].Value !=
2587 attrs->DMABufPlaneModifiersLo[i].Value) ||
2588 (attrs->DMABufPlaneModifiersHi[0].Value !=
2589 attrs->DMABufPlaneModifiersHi[i].Value))
2590 return _eglError(EGL_BAD_PARAMETER, "modifier attributes not equal");
2597 /* Returns the total number of planes for the format or zero if it isn't a
2598 * valid fourcc format.
2601 dri2_num_fourcc_format_planes(EGLint format)
2605 case DRM_FORMAT_RG88:
2606 case DRM_FORMAT_GR88:
2607 case DRM_FORMAT_R16:
2608 case DRM_FORMAT_GR1616:
2609 case DRM_FORMAT_RGB332:
2610 case DRM_FORMAT_BGR233:
2611 case DRM_FORMAT_XRGB4444:
2612 case DRM_FORMAT_XBGR4444:
2613 case DRM_FORMAT_RGBX4444:
2614 case DRM_FORMAT_BGRX4444:
2615 case DRM_FORMAT_ARGB4444:
2616 case DRM_FORMAT_ABGR4444:
2617 case DRM_FORMAT_RGBA4444:
2618 case DRM_FORMAT_BGRA4444:
2619 case DRM_FORMAT_XRGB1555:
2620 case DRM_FORMAT_XBGR1555:
2621 case DRM_FORMAT_RGBX5551:
2622 case DRM_FORMAT_BGRX5551:
2623 case DRM_FORMAT_ARGB1555:
2624 case DRM_FORMAT_ABGR1555:
2625 case DRM_FORMAT_RGBA5551:
2626 case DRM_FORMAT_BGRA5551:
2627 case DRM_FORMAT_RGB565:
2628 case DRM_FORMAT_BGR565:
2629 case DRM_FORMAT_RGB888:
2630 case DRM_FORMAT_BGR888:
2631 case DRM_FORMAT_XRGB8888:
2632 case DRM_FORMAT_XBGR8888:
2633 case DRM_FORMAT_RGBX8888:
2634 case DRM_FORMAT_BGRX8888:
2635 case DRM_FORMAT_ARGB8888:
2636 case DRM_FORMAT_ABGR8888:
2637 case DRM_FORMAT_RGBA8888:
2638 case DRM_FORMAT_BGRA8888:
2639 case DRM_FORMAT_XRGB2101010:
2640 case DRM_FORMAT_XBGR2101010:
2641 case DRM_FORMAT_RGBX1010102:
2642 case DRM_FORMAT_BGRX1010102:
2643 case DRM_FORMAT_ARGB2101010:
2644 case DRM_FORMAT_ABGR2101010:
2645 case DRM_FORMAT_RGBA1010102:
2646 case DRM_FORMAT_BGRA1010102:
2647 case DRM_FORMAT_XBGR16161616F:
2648 case DRM_FORMAT_ABGR16161616F:
2649 case DRM_FORMAT_YUYV:
2650 case DRM_FORMAT_YVYU:
2651 case DRM_FORMAT_UYVY:
2652 case DRM_FORMAT_VYUY:
2653 case DRM_FORMAT_AYUV:
2654 case DRM_FORMAT_XYUV8888:
2657 case DRM_FORMAT_NV12:
2658 case DRM_FORMAT_NV21:
2659 case DRM_FORMAT_NV16:
2660 case DRM_FORMAT_NV61:
2661 case DRM_FORMAT_P010:
2662 case DRM_FORMAT_P012:
2663 case DRM_FORMAT_P016:
2666 case DRM_FORMAT_YUV410:
2667 case DRM_FORMAT_YVU410:
2668 case DRM_FORMAT_YUV411:
2669 case DRM_FORMAT_YVU411:
2670 case DRM_FORMAT_YUV420:
2671 case DRM_FORMAT_YVU420:
2672 case DRM_FORMAT_YUV422:
2673 case DRM_FORMAT_YVU422:
2674 case DRM_FORMAT_YUV444:
2675 case DRM_FORMAT_YVU444:
2683 /* Returns the total number of file descriptors. Zero indicates an error. */
2685 dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)
2687 unsigned plane_n = dri2_num_fourcc_format_planes(attrs->DMABufFourCC.Value);
2689 _eglError(EGL_BAD_MATCH, "unknown drm fourcc format");
2693 for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; i++) {
2695 * The modifiers extension spec says:
2697 * "Modifiers may modify any attribute of a buffer import, including
2698 * but not limited to adding extra planes to a format which
2699 * otherwise does not have those planes. As an example, a modifier
2700 * may add a plane for an external compression buffer to a
2701 * single-plane format. The exact meaning and effect of any
2702 * modifier is canonically defined by drm_fourcc.h, not as part of
2705 if (attrs->DMABufPlaneModifiersLo[i].IsPresent &&
2706 attrs->DMABufPlaneModifiersHi[i].IsPresent) {
2714 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2715 * incomplete, EGL_BAD_PARAMETER is generated."
2717 for (unsigned i = 0; i < plane_n; ++i) {
2718 if (!attrs->DMABufPlaneFds[i].IsPresent ||
2719 !attrs->DMABufPlaneOffsets[i].IsPresent ||
2720 !attrs->DMABufPlanePitches[i].IsPresent) {
2721 _eglError(EGL_BAD_PARAMETER, "plane attribute(s) missing");
2727 * The spec also says:
2729 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2730 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2731 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2732 * or EGL_DMA_BUF_PLANE3_* attributes are specified."
2734 for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; ++i) {
2735 if (attrs->DMABufPlaneFds[i].IsPresent ||
2736 attrs->DMABufPlaneOffsets[i].IsPresent ||
2737 attrs->DMABufPlanePitches[i].IsPresent) {
2738 _eglError(EGL_BAD_ATTRIBUTE, "too many plane attributes");
2747 dri2_query_dma_buf_formats(_EGLDisplay *disp, EGLint max,
2748 EGLint *formats, EGLint *count)
2750 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2751 if (max < 0 || (max > 0 && formats == NULL))
2752 return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
2754 if (dri2_dpy->image->base.version < 15 ||
2755 dri2_dpy->image->queryDmaBufFormats == NULL)
2758 if (!dri2_dpy->image->queryDmaBufFormats(dri2_dpy->dri_screen, max,
2763 /* Assert that all of the formats returned are actually fourcc formats.
2764 * Some day, if we want the internal interface function to be able to
2765 * return the fake fourcc formats defined in dri_interface.h, we'll have
2766 * to do something more clever here to pair the list down to just real
2767 * fourcc formats so that we don't leak the fake internal ones.
2769 for (int i = 0; i < *count; i++) {
2770 assert(dri2_num_fourcc_format_planes(formats[i]) > 0);
2778 dri2_query_dma_buf_modifiers(_EGLDisplay *disp, EGLint format,
2779 EGLint max, EGLuint64KHR *modifiers,
2780 EGLBoolean *external_only, EGLint *count)
2782 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2784 if (dri2_num_fourcc_format_planes(format) == 0)
2785 return _eglError(EGL_BAD_PARAMETER, "invalid fourcc format");
2788 return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
2790 if (max > 0 && modifiers == NULL)
2791 return _eglError(EGL_BAD_PARAMETER, "invalid modifiers array");
2793 if (dri2_dpy->image->base.version < 15 ||
2794 dri2_dpy->image->queryDmaBufModifiers == NULL)
2797 if (dri2_dpy->image->queryDmaBufModifiers(dri2_dpy->dri_screen, format,
2799 (unsigned int *) external_only,
2801 return _eglError(EGL_BAD_PARAMETER, "invalid format");
2809 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2810 * EGL will take a reference to the dma_buf(s) which it will release at any
2811 * time while the EGLDisplay is initialized. It is the responsibility of the
2812 * application to close the dma_buf file descriptors."
2814 * Therefore we must never close or otherwise modify the file descriptors.
2817 dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
2818 EGLClientBuffer buffer, const EGLint *attr_list)
2820 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2822 _EGLImageAttribs attrs;
2823 __DRIimage *dri_image;
2825 int fds[DMA_BUF_MAX_PLANES];
2826 int pitches[DMA_BUF_MAX_PLANES];
2827 int offsets[DMA_BUF_MAX_PLANES];
2829 bool has_modifier = false;
2835 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2836 * error EGL_BAD_PARAMETER is generated."
2838 if (buffer != NULL) {
2839 _eglError(EGL_BAD_PARAMETER, "buffer not NULL");
2843 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2846 if (!dri2_check_dma_buf_attribs(&attrs))
2849 num_fds = dri2_check_dma_buf_format(&attrs);
2853 for (unsigned i = 0; i < num_fds; ++i) {
2854 fds[i] = attrs.DMABufPlaneFds[i].Value;
2855 pitches[i] = attrs.DMABufPlanePitches[i].Value;
2856 offsets[i] = attrs.DMABufPlaneOffsets[i].Value;
2859 /* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2860 * will be present in attrs.DMABufPlaneModifiersLo[0] and
2861 * attrs.DMABufPlaneModifiersHi[0] */
2862 if (attrs.DMABufPlaneModifiersLo[0].IsPresent) {
2863 modifier = combine_u32_into_u64(attrs.DMABufPlaneModifiersHi[0].Value,
2864 attrs.DMABufPlaneModifiersLo[0].Value);
2865 has_modifier = true;
2868 if (attrs.ProtectedContent) {
2869 if (dri2_dpy->image->base.version < 18 ||
2870 dri2_dpy->image->createImageFromDmaBufs3 == NULL) {
2871 _eglError(EGL_BAD_MATCH, "unsupported protected_content attribute");
2872 return EGL_NO_IMAGE_KHR;
2875 modifier = DRM_FORMAT_MOD_INVALID;
2878 dri2_dpy->image->createImageFromDmaBufs3(dri2_dpy->dri_screen,
2879 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2880 modifier, fds, num_fds, pitches, offsets,
2881 attrs.DMABufYuvColorSpaceHint.Value,
2882 attrs.DMABufSampleRangeHint.Value,
2883 attrs.DMABufChromaHorizontalSiting.Value,
2884 attrs.DMABufChromaVerticalSiting.Value,
2885 attrs.ProtectedContent ? __DRI_IMAGE_PROTECTED_CONTENT_FLAG : 0,
2889 else if (has_modifier) {
2890 if (dri2_dpy->image->base.version < 15 ||
2891 dri2_dpy->image->createImageFromDmaBufs2 == NULL) {
2892 _eglError(EGL_BAD_MATCH, "unsupported dma_buf format modifier");
2893 return EGL_NO_IMAGE_KHR;
2896 dri2_dpy->image->createImageFromDmaBufs2(dri2_dpy->dri_screen,
2897 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2898 modifier, fds, num_fds, pitches, offsets,
2899 attrs.DMABufYuvColorSpaceHint.Value,
2900 attrs.DMABufSampleRangeHint.Value,
2901 attrs.DMABufChromaHorizontalSiting.Value,
2902 attrs.DMABufChromaVerticalSiting.Value,
2908 dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen,
2909 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2910 fds, num_fds, pitches, offsets,
2911 attrs.DMABufYuvColorSpaceHint.Value,
2912 attrs.DMABufSampleRangeHint.Value,
2913 attrs.DMABufChromaHorizontalSiting.Value,
2914 attrs.DMABufChromaVerticalSiting.Value,
2918 dri2_create_image_khr_texture_error(error);
2921 return EGL_NO_IMAGE_KHR;
2923 res = dri2_create_image_from_dri(disp, dri_image);
2928 dri2_create_drm_image_mesa(_EGLDisplay *disp, const EGLint *attr_list)
2930 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2931 struct dri2_egl_image *dri2_img;
2932 _EGLImageAttribs attrs;
2933 unsigned int dri_use, valid_mask;
2937 _eglError(EGL_BAD_PARAMETER, __func__);
2938 return EGL_NO_IMAGE_KHR;
2941 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2942 return EGL_NO_IMAGE_KHR;
2944 if (attrs.Width <= 0 || attrs.Height <= 0) {
2945 _eglError(EGL_BAD_PARAMETER, __func__);
2946 return EGL_NO_IMAGE_KHR;
2949 switch (attrs.DRMBufferFormatMESA) {
2950 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2951 format = __DRI_IMAGE_FORMAT_ARGB8888;
2954 _eglError(EGL_BAD_PARAMETER, __func__);
2955 return EGL_NO_IMAGE_KHR;
2959 EGL_DRM_BUFFER_USE_SCANOUT_MESA |
2960 EGL_DRM_BUFFER_USE_SHARE_MESA |
2961 EGL_DRM_BUFFER_USE_CURSOR_MESA;
2962 if (attrs.DRMBufferUseMESA & ~valid_mask) {
2963 _eglError(EGL_BAD_PARAMETER, __func__);
2964 return EGL_NO_IMAGE_KHR;
2968 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
2969 dri_use |= __DRI_IMAGE_USE_SHARE;
2970 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
2971 dri_use |= __DRI_IMAGE_USE_SCANOUT;
2972 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA)
2973 dri_use |= __DRI_IMAGE_USE_CURSOR;
2975 dri2_img = malloc(sizeof *dri2_img);
2977 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2978 return EGL_NO_IMAGE_KHR;
2981 _eglInitImage(&dri2_img->base, disp);
2983 dri2_img->dri_image =
2984 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
2985 attrs.Width, attrs.Height,
2986 format, dri_use, dri2_img);
2987 if (dri2_img->dri_image == NULL) {
2989 _eglError(EGL_BAD_ALLOC, "dri2_create_drm_image_mesa");
2990 return EGL_NO_IMAGE_KHR;
2993 return &dri2_img->base;
2997 dri2_export_drm_image_mesa(_EGLDisplay *disp, _EGLImage *img,
2998 EGLint *name, EGLint *handle, EGLint *stride)
3000 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3001 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3003 if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image,
3004 __DRI_IMAGE_ATTRIB_NAME, name))
3005 return _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa");
3008 dri2_dpy->image->queryImage(dri2_img->dri_image,
3009 __DRI_IMAGE_ATTRIB_HANDLE, handle);
3012 dri2_dpy->image->queryImage(dri2_img->dri_image,
3013 __DRI_IMAGE_ATTRIB_STRIDE, stride);
3019 * Checks if we can support EGL_MESA_image_dma_buf_export on this image.
3021 * The spec provides a boolean return for the driver to reject exporting for
3022 * basically any reason, but doesn't specify any particular error cases. For
3023 * now, we just fail if we don't have a DRM fourcc for the format.
3026 dri2_can_export_dma_buf_image(_EGLDisplay *disp, _EGLImage *img)
3028 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3029 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3032 if (!dri2_dpy->image->queryImage(dri2_img->dri_image,
3033 __DRI_IMAGE_ATTRIB_FOURCC, &fourcc)) {
3041 dri2_export_dma_buf_image_query_mesa(_EGLDisplay *disp, _EGLImage *img,
3042 EGLint *fourcc, EGLint *nplanes,
3043 EGLuint64KHR *modifiers)
3045 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3046 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3049 if (!dri2_can_export_dma_buf_image(disp, img))
3052 dri2_dpy->image->queryImage(dri2_img->dri_image,
3053 __DRI_IMAGE_ATTRIB_NUM_PLANES, &num_planes);
3055 *nplanes = num_planes;
3058 dri2_dpy->image->queryImage(dri2_img->dri_image,
3059 __DRI_IMAGE_ATTRIB_FOURCC, fourcc);
3063 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
3066 query = dri2_dpy->image->queryImage(dri2_img->dri_image,
3067 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
3069 query &= dri2_dpy->image->queryImage(dri2_img->dri_image,
3070 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
3073 modifier = combine_u32_into_u64 (mod_hi, mod_lo);
3075 for (int i = 0; i < num_planes; i++)
3076 modifiers[i] = modifier;
3083 dri2_export_dma_buf_image_mesa(_EGLDisplay *disp, _EGLImage *img,
3084 int *fds, EGLint *strides, EGLint *offsets)
3086 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3087 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3090 if (!dri2_can_export_dma_buf_image(disp, img))
3093 /* EGL_MESA_image_dma_buf_export spec says:
3094 * "If the number of fds is less than the number of planes, then
3095 * subsequent fd slots should contain -1."
3098 /* Query nplanes so that we know how big the given array is. */
3099 dri2_dpy->image->queryImage(dri2_img->dri_image,
3100 __DRI_IMAGE_ATTRIB_NUM_PLANES, &nplanes);
3101 memset(fds, -1, nplanes * sizeof(int));
3104 /* rework later to provide multiple fds/strides/offsets */
3106 dri2_dpy->image->queryImage(dri2_img->dri_image,
3107 __DRI_IMAGE_ATTRIB_FD, fds);
3110 dri2_dpy->image->queryImage(dri2_img->dri_image,
3111 __DRI_IMAGE_ATTRIB_STRIDE, strides);
3115 bool ret = dri2_dpy->image->queryImage(dri2_img->dri_image,
3116 __DRI_IMAGE_ATTRIB_OFFSET, &img_offset);
3118 offsets[0] = img_offset;
3129 dri2_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
3130 EGLClientBuffer buffer, const EGLint *attr_list)
3133 case EGL_GL_TEXTURE_2D_KHR:
3134 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
3135 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
3136 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
3137 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
3138 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
3139 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
3140 case EGL_GL_TEXTURE_3D_KHR:
3141 return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list);
3142 case EGL_GL_RENDERBUFFER_KHR:
3143 return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
3145 case EGL_DRM_BUFFER_MESA:
3146 return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list);
3147 case EGL_LINUX_DMA_BUF_EXT:
3148 return dri2_create_image_dma_buf(disp, ctx, buffer, attr_list);
3150 #ifdef HAVE_WAYLAND_PLATFORM
3151 case EGL_WAYLAND_BUFFER_WL:
3152 return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list);
3155 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
3156 return EGL_NO_IMAGE_KHR;
3161 dri2_destroy_image_khr(_EGLDisplay *disp, _EGLImage *image)
3163 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3164 struct dri2_egl_image *dri2_img = dri2_egl_image(image);
3166 dri2_dpy->image->destroyImage(dri2_img->dri_image);
3172 #ifdef HAVE_WAYLAND_PLATFORM
3175 dri2_wl_reference_buffer(void *user_data, uint32_t name, int fd,
3176 struct wl_drm_buffer *buffer)
3178 _EGLDisplay *disp = user_data;
3179 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3181 int dri_components = 0;
3184 img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen,
3193 img = dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
3205 dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components);
3207 buffer->driver_format = NULL;
3208 for (int i = 0; i < ARRAY_SIZE(wl_drm_components); i++)
3209 if (wl_drm_components[i].dri_components == dri_components)
3210 buffer->driver_format = &wl_drm_components[i];
3212 if (buffer->driver_format == NULL)
3213 dri2_dpy->image->destroyImage(img);
3215 buffer->driver_buffer = img;
3219 dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer)
3221 _EGLDisplay *disp = user_data;
3222 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3224 dri2_dpy->image->destroyImage(buffer->driver_buffer);
3228 dri2_bind_wayland_display_wl(_EGLDisplay *disp, struct wl_display *wl_dpy)
3230 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3231 const struct wayland_drm_callbacks wl_drm_callbacks = {
3232 .authenticate = (int(*)(void *, uint32_t)) dri2_dpy->vtbl->authenticate,
3233 .reference_buffer = dri2_wl_reference_buffer,
3234 .release_buffer = dri2_wl_release_buffer,
3235 .is_format_supported = dri2_wl_is_format_supported
3240 if (dri2_dpy->wl_server_drm)
3243 if (drmGetCap(dri2_dpy->fd, DRM_CAP_PRIME, &cap) == 0 &&
3244 cap == (DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT) &&
3245 dri2_dpy->image->base.version >= 7 &&
3246 dri2_dpy->image->createImageFromFds != NULL)
3247 flags |= WAYLAND_DRM_PRIME;
3249 dri2_dpy->wl_server_drm =
3250 wayland_drm_init(wl_dpy, dri2_dpy->device_name,
3251 &wl_drm_callbacks, disp, flags);
3253 if (!dri2_dpy->wl_server_drm)
3256 #ifdef HAVE_DRM_PLATFORM
3257 /* We have to share the wl_drm instance with gbm, so gbm can convert
3258 * wl_buffers to gbm bos. */
3259 if (dri2_dpy->gbm_dri)
3260 dri2_dpy->gbm_dri->wl_drm = dri2_dpy->wl_server_drm;
3267 dri2_unbind_wayland_display_wl(_EGLDisplay *disp, struct wl_display *wl_dpy)
3269 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3271 if (!dri2_dpy->wl_server_drm)
3274 wayland_drm_uninit(dri2_dpy->wl_server_drm);
3275 dri2_dpy->wl_server_drm = NULL;
3281 dri2_query_wayland_buffer_wl(_EGLDisplay *disp, struct wl_resource *buffer_resource,
3282 EGLint attribute, EGLint *value)
3284 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3285 struct wl_drm_buffer *buffer;
3286 const struct wl_drm_components_descriptor *format;
3288 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm, buffer_resource);
3292 format = buffer->driver_format;
3293 switch (attribute) {
3294 case EGL_TEXTURE_FORMAT:
3295 *value = format->components;
3298 *value = buffer->width;
3301 *value = buffer->height;
3310 dri2_egl_ref_sync(struct dri2_egl_sync *sync)
3312 p_atomic_inc(&sync->refcount);
3316 dri2_egl_unref_sync(struct dri2_egl_display *dri2_dpy,
3317 struct dri2_egl_sync *dri2_sync)
3319 if (p_atomic_dec_zero(&dri2_sync->refcount)) {
3320 switch (dri2_sync->base.Type) {
3321 case EGL_SYNC_REUSABLE_KHR:
3322 cnd_destroy(&dri2_sync->cond);
3324 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3325 if (dri2_sync->base.SyncFd != EGL_NO_NATIVE_FENCE_FD_ANDROID)
3326 close(dri2_sync->base.SyncFd);
3332 if (dri2_sync->fence)
3333 dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, dri2_sync->fence);
3340 dri2_create_sync(_EGLDisplay *disp, EGLenum type, const EGLAttrib *attrib_list)
3342 _EGLContext *ctx = _eglGetCurrentContext();
3343 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3344 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3345 struct dri2_egl_sync *dri2_sync;
3347 pthread_condattr_t attr;
3349 dri2_sync = calloc(1, sizeof(struct dri2_egl_sync));
3351 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3355 if (!_eglInitSync(&dri2_sync->base, disp, type, attrib_list)) {
3361 case EGL_SYNC_FENCE_KHR:
3362 dri2_sync->fence = dri2_dpy->fence->create_fence(dri2_ctx->dri_context);
3363 if (!dri2_sync->fence) {
3364 /* Why did it fail? DRI doesn't return an error code, so we emit
3365 * a generic EGL error that doesn't communicate user error.
3367 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3373 case EGL_SYNC_CL_EVENT_KHR:
3374 dri2_sync->fence = dri2_dpy->fence->get_fence_from_cl_event(
3375 dri2_dpy->dri_screen,
3376 dri2_sync->base.CLEvent);
3377 /* this can only happen if the cl_event passed in is invalid. */
3378 if (!dri2_sync->fence) {
3379 _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3384 /* the initial status must be "signaled" if the cl_event is signaled */
3385 if (dri2_dpy->fence->client_wait_sync(dri2_ctx->dri_context,
3386 dri2_sync->fence, 0, 0))
3387 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3390 case EGL_SYNC_REUSABLE_KHR:
3391 /* intialize attr */
3392 ret = pthread_condattr_init(&attr);
3395 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3400 /* change clock attribute to CLOCK_MONOTONIC */
3401 ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
3404 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3409 ret = pthread_cond_init(&dri2_sync->cond, &attr);
3412 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3417 /* initial status of reusable sync must be "unsignaled" */
3418 dri2_sync->base.SyncStatus = EGL_UNSIGNALED_KHR;
3421 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3422 if (dri2_dpy->fence->create_fence_fd) {
3423 dri2_sync->fence = dri2_dpy->fence->create_fence_fd(
3424 dri2_ctx->dri_context,
3425 dri2_sync->base.SyncFd);
3427 if (!dri2_sync->fence) {
3428 _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3435 p_atomic_set(&dri2_sync->refcount, 1);
3436 return &dri2_sync->base;
3440 dri2_destroy_sync(_EGLDisplay *disp, _EGLSync *sync)
3442 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3443 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3444 EGLint ret = EGL_TRUE;
3447 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
3448 * then unlock all threads possibly blocked by the reusable sync before
3451 if (dri2_sync->base.Type == EGL_SYNC_REUSABLE_KHR &&
3452 dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3453 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3454 /* unblock all threads currently blocked by sync */
3455 err = cnd_broadcast(&dri2_sync->cond);
3458 _eglError(EGL_BAD_ACCESS, "eglDestroySyncKHR");
3463 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
3469 dri2_dup_native_fence_fd(_EGLDisplay *disp, _EGLSync *sync)
3471 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3472 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3474 assert(sync->Type == EGL_SYNC_NATIVE_FENCE_ANDROID);
3476 if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3477 /* try to retrieve the actual native fence fd.. if rendering is
3478 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
3480 sync->SyncFd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
3484 if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3485 /* if native fence fd still not created, return an error: */
3486 _eglError(EGL_BAD_PARAMETER, "eglDupNativeFenceFDANDROID");
3487 return EGL_NO_NATIVE_FENCE_FD_ANDROID;
3490 return os_dupfd_cloexec(sync->SyncFd);
3494 dri2_set_blob_cache_funcs(_EGLDisplay *disp,
3495 EGLSetBlobFuncANDROID set,
3496 EGLGetBlobFuncANDROID get)
3498 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3499 dri2_dpy->blob->set_cache_funcs(dri2_dpy->dri_screen,
3501 disp->BlobCacheGet);
3505 dri2_client_wait_sync(_EGLDisplay *disp, _EGLSync *sync,
3506 EGLint flags, EGLTime timeout)
3508 _EGLContext *ctx = _eglGetCurrentContext();
3509 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3510 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3511 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3512 unsigned wait_flags = 0;
3514 EGLint ret = EGL_CONDITION_SATISFIED_KHR;
3516 /* The EGL_KHR_fence_sync spec states:
3518 * "If no context is current for the bound API,
3519 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
3521 if (dri2_ctx && flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)
3522 wait_flags |= __DRI2_FENCE_FLAG_FLUSH_COMMANDS;
3524 /* the sync object should take a reference while waiting */
3525 dri2_egl_ref_sync(dri2_sync);
3527 switch (sync->Type) {
3528 case EGL_SYNC_FENCE_KHR:
3529 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3530 case EGL_SYNC_CL_EVENT_KHR:
3531 if (dri2_dpy->fence->client_wait_sync(dri2_ctx ? dri2_ctx->dri_context : NULL,
3532 dri2_sync->fence, wait_flags,
3534 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3536 ret = EGL_TIMEOUT_EXPIRED_KHR;
3539 case EGL_SYNC_REUSABLE_KHR:
3540 if (dri2_ctx && dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR &&
3541 (flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)) {
3542 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
3546 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
3547 if (timeout == EGL_FOREVER_KHR) {
3548 mtx_lock(&dri2_sync->mutex);
3549 cnd_wait(&dri2_sync->cond, &dri2_sync->mutex);
3550 mtx_unlock(&dri2_sync->mutex);
3552 /* if reusable sync has not been yet signaled */
3553 if (dri2_sync->base.SyncStatus != EGL_SIGNALED_KHR) {
3554 /* timespecs for cnd_timedwait */
3555 struct timespec current;
3556 struct timespec expire;
3558 /* We override the clock to monotonic when creating the condition
3560 clock_gettime(CLOCK_MONOTONIC, ¤t);
3562 /* calculating when to expire */
3563 expire.tv_nsec = timeout % 1000000000L;
3564 expire.tv_sec = timeout / 1000000000L;
3566 expire.tv_nsec += current.tv_nsec;
3567 expire.tv_sec += current.tv_sec;
3569 /* expire.nsec now is a number between 0 and 1999999998 */
3570 if (expire.tv_nsec > 999999999L) {
3572 expire.tv_nsec -= 1000000000L;
3575 mtx_lock(&dri2_sync->mutex);
3576 ret = cnd_timedwait(&dri2_sync->cond, &dri2_sync->mutex, &expire);
3577 mtx_unlock(&dri2_sync->mutex);
3579 if (ret == thrd_busy) {
3580 if (dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3581 ret = EGL_TIMEOUT_EXPIRED_KHR;
3583 _eglError(EGL_BAD_ACCESS, "eglClientWaitSyncKHR");
3591 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
3597 dri2_signal_sync(_EGLDisplay *disp, _EGLSync *sync, EGLenum mode)
3599 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3602 if (sync->Type != EGL_SYNC_REUSABLE_KHR)
3603 return _eglError(EGL_BAD_MATCH, "eglSignalSyncKHR");
3605 if (mode != EGL_SIGNALED_KHR && mode != EGL_UNSIGNALED_KHR)
3606 return _eglError(EGL_BAD_ATTRIBUTE, "eglSignalSyncKHR");
3608 dri2_sync->base.SyncStatus = mode;
3610 if (mode == EGL_SIGNALED_KHR) {
3611 ret = cnd_broadcast(&dri2_sync->cond);
3613 /* fail to broadcast */
3615 return _eglError(EGL_BAD_ACCESS, "eglSignalSyncKHR");
3622 dri2_server_wait_sync(_EGLDisplay *disp, _EGLSync *sync)
3624 _EGLContext *ctx = _eglGetCurrentContext();
3625 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3626 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3627 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3629 dri2_dpy->fence->server_wait_sync(dri2_ctx->dri_context,
3630 dri2_sync->fence, 0);
3635 dri2_interop_query_device_info(_EGLDisplay *disp, _EGLContext *ctx,
3636 struct mesa_glinterop_device_info *out)
3638 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3639 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3641 if (!dri2_dpy->interop)
3642 return MESA_GLINTEROP_UNSUPPORTED;
3644 return dri2_dpy->interop->query_device_info(dri2_ctx->dri_context, out);
3648 dri2_interop_export_object(_EGLDisplay *disp, _EGLContext *ctx,
3649 struct mesa_glinterop_export_in *in,
3650 struct mesa_glinterop_export_out *out)
3652 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3653 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3655 if (!dri2_dpy->interop)
3656 return MESA_GLINTEROP_UNSUPPORTED;
3658 return dri2_dpy->interop->export_object(dri2_ctx->dri_context, in, out);
3661 const _EGLDriver _eglDriver = {
3662 .Initialize = dri2_initialize,
3663 .Terminate = dri2_terminate,
3664 .CreateContext = dri2_create_context,
3665 .DestroyContext = dri2_destroy_context,
3666 .MakeCurrent = dri2_make_current,
3667 .CreateWindowSurface = dri2_create_window_surface,
3668 .CreatePixmapSurface = dri2_create_pixmap_surface,
3669 .CreatePbufferSurface = dri2_create_pbuffer_surface,
3670 .DestroySurface = dri2_destroy_surface,
3671 .GetProcAddress = dri2_get_proc_address,
3672 .WaitClient = dri2_wait_client,
3673 .WaitNative = dri2_wait_native,
3674 .BindTexImage = dri2_bind_tex_image,
3675 .ReleaseTexImage = dri2_release_tex_image,
3676 .SwapInterval = dri2_swap_interval,
3677 .SwapBuffers = dri2_swap_buffers,
3678 .SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage,
3679 .SwapBuffersRegionNOK = dri2_swap_buffers_region,
3680 .SetDamageRegion = dri2_set_damage_region,
3681 .PostSubBufferNV = dri2_post_sub_buffer,
3682 .CopyBuffers = dri2_copy_buffers,
3683 .QueryBufferAge = dri2_query_buffer_age,
3684 .CreateImageKHR = dri2_create_image,
3685 .DestroyImageKHR = dri2_destroy_image_khr,
3686 .CreateWaylandBufferFromImageWL = dri2_create_wayland_buffer_from_image,
3687 .QuerySurface = dri2_query_surface,
3688 .QueryDriverName = dri2_query_driver_name,
3689 .QueryDriverConfig = dri2_query_driver_config,
3691 .CreateDRMImageMESA = dri2_create_drm_image_mesa,
3692 .ExportDRMImageMESA = dri2_export_drm_image_mesa,
3693 .ExportDMABUFImageQueryMESA = dri2_export_dma_buf_image_query_mesa,
3694 .ExportDMABUFImageMESA = dri2_export_dma_buf_image_mesa,
3695 .QueryDmaBufFormatsEXT = dri2_query_dma_buf_formats,
3696 .QueryDmaBufModifiersEXT = dri2_query_dma_buf_modifiers,
3698 #ifdef HAVE_WAYLAND_PLATFORM
3699 .BindWaylandDisplayWL = dri2_bind_wayland_display_wl,
3700 .UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl,
3701 .QueryWaylandBufferWL = dri2_query_wayland_buffer_wl,
3703 .GetSyncValuesCHROMIUM = dri2_get_sync_values_chromium,
3704 .CreateSyncKHR = dri2_create_sync,
3705 .ClientWaitSyncKHR = dri2_client_wait_sync,
3706 .SignalSyncKHR = dri2_signal_sync,
3707 .WaitSyncKHR = dri2_server_wait_sync,
3708 .DestroySyncKHR = dri2_destroy_sync,
3709 .GLInteropQueryDeviceInfo = dri2_interop_query_device_info,
3710 .GLInteropExportObject = dri2_interop_export_object,
3711 .DupNativeFenceFDANDROID = dri2_dup_native_fence_fd,
3712 .SetBlobCacheFuncsANDROID = dri2_set_blob_cache_funcs,