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/libsync.h"
66 #include "util/os_file.h"
67 #include "util/u_atomic.h"
68 #include "util/u_call_once.h"
69 #include "util/u_vector.h"
70 #include "mapi/glapi/glapi.h"
71 #include "util/bitscan.h"
72 #include "util/driconf.h"
73 #include "util/u_math.h"
75 #define NUM_ATTRIBS 12
77 static const struct dri2_pbuffer_visual {
78 const char *format_name;
79 unsigned int dri_image_format;
81 unsigned int rgba_sizes[4];
82 } dri2_pbuffer_visuals[] = {
85 __DRI_IMAGE_FORMAT_ABGR16161616F,
91 __DRI_IMAGE_FORMAT_XBGR16161616F,
97 __DRI_IMAGE_FORMAT_ARGB2101010,
103 __DRI_IMAGE_FORMAT_XRGB2101010,
109 __DRI_IMAGE_FORMAT_ARGB8888,
115 __DRI_IMAGE_FORMAT_XRGB8888,
121 __DRI_IMAGE_FORMAT_RGB565,
128 dri_set_background_context(void *loaderPrivate)
130 _EGLContext *ctx = _eglGetCurrentContext();
131 _EGLThreadInfo *t = _eglGetCurrentThread();
133 _eglBindContextToThread(ctx, t);
137 dri2_gl_flush_get(_glapi_proc *glFlush)
139 *glFlush = _glapi_get_proc_address("glFlush");
145 static void (*glFlush)(void);
146 static util_once_flag once = UTIL_ONCE_FLAG_INIT;
148 util_call_once_data(&once,
149 (util_call_once_data_func)dri2_gl_flush_get, &glFlush);
151 /* if glFlush is not available things are horribly broken */
153 _eglLog(_EGL_WARNING, "DRI2: failed to find glFlush entry point");
161 dri_is_thread_safe(UNUSED void *loaderPrivate)
163 #ifdef HAVE_X11_PLATFORM
164 struct dri2_egl_surface *dri2_surf = loaderPrivate;
166 /* loader_dri3_blit_context_get creates a context with
167 * loaderPrivate being NULL. Enabling glthread for a blitting
168 * context isn't useful so return false.
173 _EGLDisplay *display = dri2_surf->base.Resource.Display;
175 Display *xdpy = (Display*)display->PlatformDisplay;
177 /* Check Xlib is running in thread safe mode when running on EGL/X11-xlib
180 * 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
181 * It wll be NULL if XInitThreads wasn't called.
183 if (display->Platform == _EGL_PLATFORM_X11 && xdpy && !xdpy->lock_fns)
190 const __DRIbackgroundCallableExtension background_callable_extension = {
191 .base = { __DRI_BACKGROUND_CALLABLE, 2 },
193 .setBackgroundContext = dri_set_background_context,
194 .isThreadSafe = dri_is_thread_safe,
197 const __DRIuseInvalidateExtension use_invalidate = {
198 .base = { __DRI_USE_INVALIDATE, 1 }
202 dri2_get_pbuffer_drawable_info(__DRIdrawable * draw,
203 int *x, int *y, int *w, int *h,
206 struct dri2_egl_surface *dri2_surf = loaderPrivate;
209 *w = dri2_surf->base.Width;
210 *h = dri2_surf->base.Height;
214 dri2_get_bytes_per_pixel(struct dri2_egl_surface *dri2_surf)
216 const int depth = dri2_surf->base.Config->BufferSize;
217 return depth ? util_next_power_of_two(depth / 8) : 0;
221 dri2_put_image(__DRIdrawable * draw, int op,
222 int x, int y, int w, int h,
223 char *data, void *loaderPrivate)
225 struct dri2_egl_surface *dri2_surf = loaderPrivate;
226 const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
227 const int width = dri2_surf->base.Width;
228 const int height = dri2_surf->base.Height;
229 const int dst_stride = width*bpp;
230 const int src_stride = w*bpp;
231 const int x_offset = x*bpp;
232 int copy_width = src_stride;
234 if (!dri2_surf->swrast_device_buffer)
235 dri2_surf->swrast_device_buffer = malloc(height*dst_stride);
237 if (dri2_surf->swrast_device_buffer) {
238 const char *src = data;
239 char *dst = dri2_surf->swrast_device_buffer;
244 /* Drivers are allowed to submit OOB PutImage requests, so clip here. */
245 if (copy_width > dst_stride - x_offset)
246 copy_width = dst_stride - x_offset;
251 memcpy(dst, src, copy_width);
259 dri2_get_image(__DRIdrawable * read,
260 int x, int y, int w, int h,
261 char *data, void *loaderPrivate)
263 struct dri2_egl_surface *dri2_surf = loaderPrivate;
264 const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
265 const int width = dri2_surf->base.Width;
266 const int height = dri2_surf->base.Height;
267 const int src_stride = width*bpp;
268 const int dst_stride = w*bpp;
269 const int x_offset = x*bpp;
270 int copy_width = dst_stride;
271 const char *src = dri2_surf->swrast_device_buffer;
275 memset(data, 0, copy_width * h);
282 /* Drivers are allowed to submit OOB GetImage requests, so clip here. */
283 if (copy_width > src_stride - x_offset)
284 copy_width = src_stride - x_offset;
289 memcpy(dst, src, copy_width);
296 /* HACK: technically we should have swrast_null, instead of these.
298 const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension = {
299 .base = { __DRI_SWRAST_LOADER, 1 },
300 .getDrawableInfo = dri2_get_pbuffer_drawable_info,
301 .putImage = dri2_put_image,
302 .getImage = dri2_get_image,
305 static const EGLint dri2_to_egl_attribute_map[__DRI_ATTRIB_MAX] = {
306 [__DRI_ATTRIB_BUFFER_SIZE ] = EGL_BUFFER_SIZE,
307 [__DRI_ATTRIB_LEVEL] = EGL_LEVEL,
308 [__DRI_ATTRIB_LUMINANCE_SIZE] = EGL_LUMINANCE_SIZE,
309 [__DRI_ATTRIB_DEPTH_SIZE] = EGL_DEPTH_SIZE,
310 [__DRI_ATTRIB_STENCIL_SIZE] = EGL_STENCIL_SIZE,
311 [__DRI_ATTRIB_SAMPLE_BUFFERS] = EGL_SAMPLE_BUFFERS,
312 [__DRI_ATTRIB_SAMPLES] = EGL_SAMPLES,
313 [__DRI_ATTRIB_MAX_PBUFFER_WIDTH] = EGL_MAX_PBUFFER_WIDTH,
314 [__DRI_ATTRIB_MAX_PBUFFER_HEIGHT] = EGL_MAX_PBUFFER_HEIGHT,
315 [__DRI_ATTRIB_MAX_PBUFFER_PIXELS] = EGL_MAX_PBUFFER_PIXELS,
316 [__DRI_ATTRIB_MAX_SWAP_INTERVAL] = EGL_MAX_SWAP_INTERVAL,
317 [__DRI_ATTRIB_MIN_SWAP_INTERVAL] = EGL_MIN_SWAP_INTERVAL,
318 [__DRI_ATTRIB_YINVERTED] = EGL_Y_INVERTED_NOK,
322 dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,
325 const bool double_buffer = surface_type == EGL_WINDOW_BIT;
326 const bool srgb = colorspace == EGL_GL_COLORSPACE_SRGB_KHR;
328 return conf->dri_config[double_buffer][srgb];
332 dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria)
334 if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0)
337 if (!_eglMatchConfig(conf, criteria))
344 dri2_get_shifts_and_sizes(const __DRIcoreExtension *core,
345 const __DRIconfig *config, int *shifts,
350 if (core->getConfigAttrib(config, __DRI_ATTRIB_RED_SHIFT, (unsigned int *)&shifts[0])) {
351 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SHIFT, (unsigned int *)&shifts[1]);
352 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SHIFT, (unsigned int *)&shifts[2]);
353 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SHIFT, (unsigned int *)&shifts[3]);
355 /* Driver isn't exposing shifts, so convert masks to shifts */
356 core->getConfigAttrib(config, __DRI_ATTRIB_RED_MASK, &mask);
357 shifts[0] = ffs(mask) - 1;
358 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_MASK, &mask);
359 shifts[1] = ffs(mask) - 1;
360 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_MASK, &mask);
361 shifts[2] = ffs(mask) - 1;
362 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_MASK, &mask);
363 shifts[3] = ffs(mask) - 1;
366 core->getConfigAttrib(config, __DRI_ATTRIB_RED_SIZE, &sizes[0]);
367 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SIZE, &sizes[1]);
368 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SIZE, &sizes[2]);
369 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SIZE, &sizes[3]);
373 dri2_get_render_type_float(const __DRIcoreExtension *core,
374 const __DRIconfig *config,
377 unsigned int render_type;
379 core->getConfigAttrib(config, __DRI_ATTRIB_RENDER_TYPE, &render_type);
380 *is_float = (render_type & __DRI_ATTRIB_FLOAT_BIT) ? true : false;
384 dri2_image_format_for_pbuffer_config(struct dri2_egl_display *dri2_dpy,
385 const __DRIconfig *config)
388 unsigned int sizes[4];
390 dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
392 for (unsigned i = 0; i < ARRAY_SIZE(dri2_pbuffer_visuals); ++i) {
393 const struct dri2_pbuffer_visual *visual = &dri2_pbuffer_visuals[i];
395 if (shifts[0] == visual->rgba_shifts[0] &&
396 shifts[1] == visual->rgba_shifts[1] &&
397 shifts[2] == visual->rgba_shifts[2] &&
398 shifts[3] == visual->rgba_shifts[3] &&
399 sizes[0] == visual->rgba_sizes[0] &&
400 sizes[1] == visual->rgba_sizes[1] &&
401 sizes[2] == visual->rgba_sizes[2] &&
402 sizes[3] == visual->rgba_sizes[3]) {
403 return visual->dri_image_format;
407 return __DRI_IMAGE_FORMAT_NONE;
410 struct dri2_egl_config *
411 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
412 EGLint surface_type, const EGLint *attr_list,
413 const int *rgba_shifts, const unsigned int *rgba_sizes)
415 struct dri2_egl_config *conf;
416 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
418 unsigned int attrib, value, double_buffer;
420 EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
421 int dri_shifts[4] = { -1, -1, -1, -1 };
422 unsigned int dri_sizes[4] = { 0, 0, 0, 0 };
423 _EGLConfig *matching_config;
424 EGLint num_configs = 0;
427 _eglInitConfig(&base, disp, id);
430 bind_to_texture_rgb = 0;
431 bind_to_texture_rgba = 0;
433 for (int i = 0; i < __DRI_ATTRIB_MAX; ++i) {
434 if (!dri2_dpy->core->indexConfigAttrib(dri_config, i, &attrib, &value))
438 case __DRI_ATTRIB_RENDER_TYPE:
439 if (value & __DRI_ATTRIB_FLOAT_BIT)
440 base.ComponentType = EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT;
441 if (value & __DRI_ATTRIB_RGBA_BIT)
442 value = EGL_RGB_BUFFER;
443 else if (value & __DRI_ATTRIB_LUMINANCE_BIT)
444 value = EGL_LUMINANCE_BUFFER;
447 base.ColorBufferType = value;
450 case __DRI_ATTRIB_CONFIG_CAVEAT:
451 if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
452 value = EGL_NON_CONFORMANT_CONFIG;
453 else if (value & __DRI_ATTRIB_SLOW_BIT)
454 value = EGL_SLOW_CONFIG;
457 base.ConfigCaveat = value;
460 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
461 bind_to_texture_rgb = value;
464 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
465 bind_to_texture_rgba = value;
468 case __DRI_ATTRIB_DOUBLE_BUFFER:
469 double_buffer = value;
472 case __DRI_ATTRIB_RED_SIZE:
473 dri_sizes[0] = value;
474 base.RedSize = value;
477 case __DRI_ATTRIB_RED_MASK:
478 dri_shifts[0] = ffs(value) - 1;
481 case __DRI_ATTRIB_RED_SHIFT:
482 dri_shifts[0] = value;
485 case __DRI_ATTRIB_GREEN_SIZE:
486 dri_sizes[1] = value;
487 base.GreenSize = value;
490 case __DRI_ATTRIB_GREEN_MASK:
491 dri_shifts[1] = ffs(value) - 1;
494 case __DRI_ATTRIB_GREEN_SHIFT:
495 dri_shifts[1] = value;
498 case __DRI_ATTRIB_BLUE_SIZE:
499 dri_sizes[2] = value;
500 base.BlueSize = value;
503 case __DRI_ATTRIB_BLUE_MASK:
504 dri_shifts[2] = ffs(value) - 1;
507 case __DRI_ATTRIB_BLUE_SHIFT:
508 dri_shifts[2] = value;
511 case __DRI_ATTRIB_ALPHA_SIZE:
512 dri_sizes[3] = value;
513 base.AlphaSize = value;
516 case __DRI_ATTRIB_ALPHA_MASK:
517 dri_shifts[3] = ffs(value) - 1;
520 case __DRI_ATTRIB_ALPHA_SHIFT:
521 dri_shifts[3] = value;
524 case __DRI_ATTRIB_ACCUM_RED_SIZE:
525 case __DRI_ATTRIB_ACCUM_GREEN_SIZE:
526 case __DRI_ATTRIB_ACCUM_BLUE_SIZE:
527 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE:
528 /* Don't expose visuals with the accumulation buffer. */
533 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE:
535 if (!disp->Extensions.KHR_gl_colorspace && srgb)
539 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH:
540 base.MaxPbufferWidth = _EGL_MAX_PBUFFER_WIDTH;
542 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT:
543 base.MaxPbufferHeight = _EGL_MAX_PBUFFER_HEIGHT;
545 case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER:
546 if (disp->Extensions.KHR_mutable_render_buffer)
547 surface_type |= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR;
550 key = dri2_to_egl_attribute_map[attrib];
552 _eglSetConfigKey(&base, key, value);
558 for (int i = 0; attr_list[i] != EGL_NONE; i += 2)
559 _eglSetConfigKey(&base, attr_list[i], attr_list[i+1]);
561 if (rgba_shifts && memcmp(rgba_shifts, dri_shifts, sizeof(dri_shifts)))
564 if (rgba_sizes && memcmp(rgba_sizes, dri_sizes, sizeof(dri_sizes)))
567 base.NativeRenderable = EGL_TRUE;
569 base.SurfaceType = surface_type;
570 if (surface_type & (EGL_PBUFFER_BIT |
571 (disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) {
572 base.BindToTextureRGB = bind_to_texture_rgb;
573 if (base.AlphaSize > 0)
574 base.BindToTextureRGBA = bind_to_texture_rgba;
578 surface_type &= ~EGL_PIXMAP_BIT;
584 base.RenderableType = disp->ClientAPIs;
585 base.Conformant = disp->ClientAPIs;
587 base.MinSwapInterval = dri2_dpy->min_swap_interval;
588 base.MaxSwapInterval = dri2_dpy->max_swap_interval;
590 if (!_eglValidateConfig(&base, EGL_FALSE)) {
591 _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);
595 config_id = base.ConfigID;
596 base.ConfigID = EGL_DONT_CARE;
597 base.SurfaceType = EGL_DONT_CARE;
598 num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1,
599 (_EGLArrayForEach) dri2_match_config, &base);
601 if (num_configs == 1) {
602 conf = (struct dri2_egl_config *) matching_config;
604 if (!conf->dri_config[double_buffer][srgb])
605 conf->dri_config[double_buffer][srgb] = dri_config;
607 /* a similar config type is already added (unlikely) => discard */
610 else if (num_configs == 0) {
611 conf = calloc(1, sizeof *conf);
615 conf->dri_config[double_buffer][srgb] = dri_config;
617 memcpy(&conf->base, &base, sizeof base);
618 conf->base.SurfaceType = 0;
619 conf->base.ConfigID = config_id;
621 _eglLinkConfig(&conf->base);
624 unreachable("duplicates should not be possible");
628 conf->base.SurfaceType |= surface_type;
634 dri2_add_pbuffer_configs_for_visuals(_EGLDisplay *disp)
636 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
637 unsigned int format_count[ARRAY_SIZE(dri2_pbuffer_visuals)] = { 0 };
638 unsigned int config_count = 0;
640 for (unsigned i = 0; dri2_dpy->driver_configs[i] != NULL; i++) {
641 for (unsigned j = 0; j < ARRAY_SIZE(dri2_pbuffer_visuals); j++) {
642 struct dri2_egl_config *dri2_conf;
644 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
645 config_count + 1, EGL_PBUFFER_BIT, NULL,
646 dri2_pbuffer_visuals[j].rgba_shifts, dri2_pbuffer_visuals[j].rgba_sizes);
649 if (dri2_conf->base.ConfigID == config_count + 1)
656 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
657 if (!format_count[i]) {
658 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
659 dri2_pbuffer_visuals[i].format_name);
663 return (config_count != 0);
667 dri2_validate_egl_image(void *image, void *data)
669 _EGLDisplay *disp = _eglLockDisplay(data);
670 _EGLImage *img = _eglLookupImage(image, disp);
671 _eglUnlockDisplay(disp);
674 _eglError(EGL_BAD_PARAMETER, "dri2_validate_egl_image");
682 dri2_lookup_egl_image_validated(void *image, void *data)
684 struct dri2_egl_image *dri2_img;
688 dri2_img = dri2_egl_image(image);
690 return dri2_img->dri_image;
694 dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
698 if (!dri2_validate_egl_image(image, data))
701 return dri2_lookup_egl_image_validated(image, data);
704 const __DRIimageLookupExtension image_lookup_extension = {
705 .base = { __DRI_IMAGE_LOOKUP, 2 },
707 .lookupEGLImage = dri2_lookup_egl_image,
708 .validateEGLImage = dri2_validate_egl_image,
709 .lookupEGLImageValidated = dri2_lookup_egl_image_validated,
712 struct dri2_extension_match {
718 static const struct dri2_extension_match dri3_driver_extensions[] = {
719 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
720 { __DRI_IMAGE_DRIVER, 1, offsetof(struct dri2_egl_display, image_driver) },
724 static const struct dri2_extension_match dri2_driver_extensions[] = {
725 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
726 { __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) },
730 static const struct dri2_extension_match dri2_core_extensions[] = {
731 { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) },
732 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
733 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
737 static const struct dri2_extension_match swrast_driver_extensions[] = {
738 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
739 { __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) },
743 static const struct dri2_extension_match swrast_core_extensions[] = {
744 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
748 static const struct dri2_extension_match optional_driver_extensions[] = {
749 { __DRI_CONFIG_OPTIONS, 1, offsetof(struct dri2_egl_display, configOptions) },
753 static const struct dri2_extension_match optional_core_extensions[] = {
754 { __DRI2_ROBUSTNESS, 1, offsetof(struct dri2_egl_display, robustness) },
755 { __DRI2_CONFIG_QUERY, 1, offsetof(struct dri2_egl_display, config) },
756 { __DRI2_FENCE, 1, offsetof(struct dri2_egl_display, fence) },
757 { __DRI2_BUFFER_DAMAGE, 1, offsetof(struct dri2_egl_display, buffer_damage) },
758 { __DRI2_RENDERER_QUERY, 1, offsetof(struct dri2_egl_display, rendererQuery) },
759 { __DRI2_INTEROP, 1, offsetof(struct dri2_egl_display, interop) },
760 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
761 { __DRI2_FLUSH_CONTROL, 1, offsetof(struct dri2_egl_display, flush_control) },
762 { __DRI2_BLOB, 1, offsetof(struct dri2_egl_display, blob) },
763 { __DRI_MUTABLE_RENDER_BUFFER_DRIVER, 1, offsetof(struct dri2_egl_display, mutable_render_buffer) },
764 { __DRI_KOPPER, 1, offsetof(struct dri2_egl_display, kopper) },
769 dri2_bind_extensions(struct dri2_egl_display *dri2_dpy,
770 const struct dri2_extension_match *matches,
771 const __DRIextension **extensions,
777 for (int i = 0; extensions[i]; i++) {
778 _eglLog(_EGL_DEBUG, "found extension `%s'", extensions[i]->name);
779 for (int j = 0; matches[j].name; j++) {
780 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
781 extensions[i]->version >= matches[j].version) {
782 field = ((char *) dri2_dpy + matches[j].offset);
783 *(const __DRIextension **) field = extensions[i];
784 _eglLog(_EGL_INFO, "found extension %s version %d",
785 extensions[i]->name, extensions[i]->version);
791 for (int j = 0; matches[j].name; j++) {
792 field = ((char *) dri2_dpy + matches[j].offset);
793 if (*(const __DRIextension **) field == NULL) {
795 _eglLog(_EGL_DEBUG, "did not find optional extension %s version %d",
796 matches[j].name, matches[j].version);
798 _eglLog(_EGL_WARNING, "did not find extension %s version %d",
799 matches[j].name, matches[j].version);
808 static const __DRIextension **
809 dri2_open_driver(_EGLDisplay *disp)
811 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
812 static const char *search_path_vars[] = {
813 "LIBGL_DRIVERS_PATH",
817 return loader_open_driver(dri2_dpy->driver_name,
823 dri2_load_driver_common(_EGLDisplay *disp,
824 const struct dri2_extension_match *driver_extensions)
826 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
827 const __DRIextension **extensions;
829 extensions = dri2_open_driver(disp);
833 if (!dri2_bind_extensions(dri2_dpy, driver_extensions, extensions, false)) {
834 dlclose(dri2_dpy->driver);
835 dri2_dpy->driver = NULL;
838 dri2_dpy->driver_extensions = extensions;
840 dri2_bind_extensions(dri2_dpy, optional_driver_extensions, extensions, true);
846 dri2_load_driver(_EGLDisplay *disp)
848 return dri2_load_driver_common(disp, dri2_driver_extensions);
852 dri2_load_driver_dri3(_EGLDisplay *disp)
854 return dri2_load_driver_common(disp, dri3_driver_extensions);
858 dri2_load_driver_swrast(_EGLDisplay *disp)
860 return dri2_load_driver_common(disp, swrast_driver_extensions);
864 dri2_renderer_query_integer(struct dri2_egl_display *dri2_dpy, int param)
866 const __DRI2rendererQueryExtension *rendererQuery = dri2_dpy->rendererQuery;
867 unsigned int value = 0;
869 if (!rendererQuery ||
870 rendererQuery->queryInteger(dri2_dpy->dri_screen, param, &value) == -1)
877 dri2_query_driver_name(_EGLDisplay *disp)
879 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
880 return dri2_dpy->driver_name;
884 dri2_query_driver_config(_EGLDisplay *disp)
886 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
887 const __DRIconfigOptionsExtension *ext = dri2_dpy->configOptions;
890 if (ext->base.version >= 2) {
891 ret = ext->getXml(dri2_dpy->driver_name);
893 ret = strdup(ext->xml);
896 mtx_unlock(&dri2_dpy->lock);
903 dri2_setup_screen(_EGLDisplay *disp)
905 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
906 unsigned int api_mask;
909 * EGL 1.5 specification defines the default value to 1. Moreover,
910 * eglSwapInterval() is required to clamp requested value to the supported
911 * range. Since the default value is implicitly assumed to be supported,
912 * use it as both minimum and maximum for the platforms that do not allow
913 * changing the interval. Platforms, which allow it (e.g. x11, wayland)
914 * override these values already.
916 dri2_dpy->min_swap_interval = 1;
917 dri2_dpy->max_swap_interval = 1;
918 dri2_dpy->default_swap_interval = 1;
920 if (dri2_dpy->image_driver) {
921 api_mask = dri2_dpy->image_driver->getAPIMask(dri2_dpy->dri_screen);
922 } else if (dri2_dpy->dri2) {
923 api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);
925 assert(dri2_dpy->swrast);
926 api_mask = 1 << __DRI_API_OPENGL |
927 1 << __DRI_API_GLES |
928 1 << __DRI_API_GLES2 |
929 1 << __DRI_API_GLES3;
932 disp->ClientAPIs = 0;
933 if ((api_mask & (1 <<__DRI_API_OPENGL)) && _eglIsApiValid(EGL_OPENGL_API))
934 disp->ClientAPIs |= EGL_OPENGL_BIT;
935 if ((api_mask & (1 << __DRI_API_GLES)) && _eglIsApiValid(EGL_OPENGL_ES_API))
936 disp->ClientAPIs |= EGL_OPENGL_ES_BIT;
937 if ((api_mask & (1 << __DRI_API_GLES2)) && _eglIsApiValid(EGL_OPENGL_ES_API))
938 disp->ClientAPIs |= EGL_OPENGL_ES2_BIT;
939 if ((api_mask & (1 << __DRI_API_GLES3)) && _eglIsApiValid(EGL_OPENGL_ES_API))
940 disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR;
942 assert(dri2_dpy->image_driver || dri2_dpy->dri2 || dri2_dpy->swrast);
943 disp->Extensions.KHR_no_config_context = EGL_TRUE;
944 disp->Extensions.KHR_surfaceless_context = EGL_TRUE;
946 if (dri2_dpy->configOptions) {
947 disp->Extensions.MESA_query_driver = EGL_TRUE;
950 /* Report back to EGL the bitmask of priorities supported */
951 disp->Extensions.IMG_context_priority =
952 dri2_renderer_query_integer(dri2_dpy,
953 __DRI2_RENDERER_HAS_CONTEXT_PRIORITY);
955 disp->Extensions.EXT_pixel_format_float = EGL_TRUE;
957 if (dri2_renderer_query_integer(dri2_dpy,
958 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB))
959 disp->Extensions.KHR_gl_colorspace = EGL_TRUE;
961 if (dri2_dpy->image_driver ||
962 (dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) ||
963 (dri2_dpy->swrast && dri2_dpy->swrast->base.version >= 3)) {
964 disp->Extensions.KHR_create_context = EGL_TRUE;
966 if (dri2_dpy->robustness)
967 disp->Extensions.EXT_create_context_robustness = EGL_TRUE;
970 if (dri2_renderer_query_integer(dri2_dpy,
971 __DRI2_RENDERER_HAS_NO_ERROR_CONTEXT))
972 disp->Extensions.KHR_create_context_no_error = EGL_TRUE;
974 if (dri2_dpy->fence) {
975 disp->Extensions.KHR_fence_sync = EGL_TRUE;
976 disp->Extensions.KHR_wait_sync = EGL_TRUE;
977 if (dri2_dpy->fence->get_fence_from_cl_event)
978 disp->Extensions.KHR_cl_event2 = EGL_TRUE;
979 if (dri2_dpy->fence->base.version >= 2 &&
980 dri2_dpy->fence->get_capabilities) {
981 unsigned capabilities =
982 dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen);
983 disp->Extensions.ANDROID_native_fence_sync =
984 (capabilities & __DRI_FENCE_CAP_NATIVE_FD) != 0;
989 disp->Extensions.ANDROID_blob_cache = EGL_TRUE;
991 disp->Extensions.KHR_reusable_sync = EGL_TRUE;
993 if (dri2_dpy->image) {
994 if (dri2_dpy->image->base.version >= 10 &&
995 dri2_dpy->image->getCapabilities != NULL) {
998 capabilities = dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
999 disp->Extensions.MESA_drm_image = (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
1001 if (dri2_dpy->image->base.version >= 11)
1002 disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
1004 disp->Extensions.MESA_drm_image = EGL_TRUE;
1005 if (dri2_dpy->image->base.version >= 11)
1006 disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
1009 disp->Extensions.KHR_image_base = EGL_TRUE;
1010 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
1011 if (dri2_dpy->image->base.version >= 5 &&
1012 dri2_dpy->image->createImageFromTexture) {
1013 disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
1014 disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
1016 if (dri2_renderer_query_integer(dri2_dpy,
1017 __DRI2_RENDERER_HAS_TEXTURE_3D))
1018 disp->Extensions.KHR_gl_texture_3D_image = EGL_TRUE;
1021 if (dri2_dpy->image->base.version >= 8 &&
1022 dri2_dpy->image->createImageFromDmaBufs) {
1023 disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE;
1024 disp->Extensions.EXT_image_dma_buf_import_modifiers = EGL_TRUE;
1029 if (dri2_dpy->flush_control)
1030 disp->Extensions.KHR_context_flush_control = EGL_TRUE;
1032 if (dri2_dpy->buffer_damage && dri2_dpy->buffer_damage->set_damage_region)
1033 disp->Extensions.KHR_partial_update = EGL_TRUE;
1035 disp->Extensions.EXT_protected_surface =
1036 dri2_renderer_query_integer(dri2_dpy,
1037 __DRI2_RENDERER_HAS_PROTECTED_SURFACE);
1038 disp->Extensions.EXT_protected_content =
1039 dri2_renderer_query_integer(dri2_dpy,
1040 __DRI2_RENDERER_HAS_PROTECTED_CONTEXT);
1044 dri2_setup_swap_interval(_EGLDisplay *disp, int max_swap_interval)
1046 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1047 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1049 /* Allow driconf to override applications.*/
1050 if (dri2_dpy->config)
1051 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
1052 "vblank_mode", &vblank_mode);
1053 switch (vblank_mode) {
1054 case DRI_CONF_VBLANK_NEVER:
1055 dri2_dpy->min_swap_interval = 0;
1056 dri2_dpy->max_swap_interval = 0;
1057 dri2_dpy->default_swap_interval = 0;
1059 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1060 dri2_dpy->min_swap_interval = 1;
1061 dri2_dpy->max_swap_interval = max_swap_interval;
1062 dri2_dpy->default_swap_interval = 1;
1064 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1065 dri2_dpy->min_swap_interval = 0;
1066 dri2_dpy->max_swap_interval = max_swap_interval;
1067 dri2_dpy->default_swap_interval = 0;
1070 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1071 dri2_dpy->min_swap_interval = 0;
1072 dri2_dpy->max_swap_interval = max_swap_interval;
1073 dri2_dpy->default_swap_interval = 1;
1078 /* All platforms but DRM call this function to create the screen and populate
1079 * the driver_configs. DRM inherits that information from its display - GBM.
1082 dri2_create_screen(_EGLDisplay *disp)
1084 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1086 if (dri2_dpy->image_driver) {
1087 dri2_dpy->dri_screen =
1088 dri2_dpy->image_driver->createNewScreen2(0, dri2_dpy->fd,
1089 dri2_dpy->loader_extensions,
1090 dri2_dpy->driver_extensions,
1091 &dri2_dpy->driver_configs,
1093 } else if (dri2_dpy->dri2) {
1094 if (dri2_dpy->dri2->base.version >= 4) {
1095 dri2_dpy->dri_screen =
1096 dri2_dpy->dri2->createNewScreen2(0, dri2_dpy->fd,
1097 dri2_dpy->loader_extensions,
1098 dri2_dpy->driver_extensions,
1099 &dri2_dpy->driver_configs, disp);
1101 dri2_dpy->dri_screen =
1102 dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd,
1103 dri2_dpy->loader_extensions,
1104 &dri2_dpy->driver_configs, disp);
1107 assert(dri2_dpy->swrast);
1108 if (dri2_dpy->swrast->base.version >= 4) {
1109 dri2_dpy->dri_screen =
1110 dri2_dpy->swrast->createNewScreen2(0, dri2_dpy->loader_extensions,
1111 dri2_dpy->driver_extensions,
1112 &dri2_dpy->driver_configs, disp);
1114 dri2_dpy->dri_screen =
1115 dri2_dpy->swrast->createNewScreen(0, dri2_dpy->loader_extensions,
1116 &dri2_dpy->driver_configs, disp);
1120 if (dri2_dpy->dri_screen == NULL) {
1121 _eglLog(_EGL_WARNING, "egl: failed to create dri2 screen");
1125 dri2_dpy->own_dri_screen = true;
1130 dri2_setup_extensions(_EGLDisplay *disp)
1132 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1133 const struct dri2_extension_match *mandatory_core_extensions;
1134 const __DRIextension **extensions;
1136 extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
1138 if (dri2_dpy->image_driver || dri2_dpy->dri2)
1139 mandatory_core_extensions = dri2_core_extensions;
1141 mandatory_core_extensions = swrast_core_extensions;
1143 if (!dri2_bind_extensions(dri2_dpy, mandatory_core_extensions, extensions, false))
1146 #ifdef HAVE_DRI3_MODIFIERS
1147 dri2_dpy->multibuffers_available =
1148 (dri2_dpy->dri3_major_version > 1 || (dri2_dpy->dri3_major_version == 1 &&
1149 dri2_dpy->dri3_minor_version >= 2)) &&
1150 (dri2_dpy->present_major_version > 1 || (dri2_dpy->present_major_version == 1 &&
1151 dri2_dpy->present_minor_version >= 2)) &&
1152 (dri2_dpy->image && dri2_dpy->image->base.version >= 15);
1155 dri2_bind_extensions(dri2_dpy, optional_core_extensions, extensions, true);
1160 * Called via eglInitialize(), drv->Initialize().
1162 * This must be guaranteed to be called exactly once, even if eglInitialize is
1163 * called many times (without a eglTerminate in between).
1166 dri2_initialize(_EGLDisplay *disp)
1168 EGLBoolean ret = EGL_FALSE;
1169 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1171 /* In the case where the application calls eglMakeCurrent(context1),
1172 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
1173 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
1174 * initialized, as we need it to be able to free context1 correctly.
1176 * It would probably be safest to forcibly release the display with
1177 * dri2_display_release, to make sure the display is reinitialized correctly.
1178 * However, the EGL spec states that we need to keep a reference to the
1179 * current context (so we cannot call dri2_make_current(NULL)), and therefore
1180 * we would leak context1 as we would be missing the old display connection
1181 * to free it up correctly.
1184 p_atomic_inc(&dri2_dpy->ref_count);
1188 loader_set_logger(_eglLog);
1190 switch (disp->Platform) {
1191 case _EGL_PLATFORM_SURFACELESS:
1192 ret = dri2_initialize_surfaceless(disp);
1194 case _EGL_PLATFORM_DEVICE:
1195 ret = dri2_initialize_device(disp);
1197 case _EGL_PLATFORM_X11:
1198 case _EGL_PLATFORM_XCB:
1199 ret = dri2_initialize_x11(disp);
1201 case _EGL_PLATFORM_DRM:
1202 ret = dri2_initialize_drm(disp);
1204 case _EGL_PLATFORM_WAYLAND:
1205 ret = dri2_initialize_wayland(disp);
1207 case _EGL_PLATFORM_ANDROID:
1208 ret = dri2_initialize_android(disp);
1211 unreachable("Callers ensure we cannot get here.");
1218 dri2_dpy = dri2_egl_display(disp);
1219 p_atomic_inc(&dri2_dpy->ref_count);
1221 mtx_init(&dri2_dpy->lock, mtx_plain);
1227 * Decrement display reference count, and free up display if necessary.
1230 dri2_display_release(_EGLDisplay *disp)
1232 struct dri2_egl_display *dri2_dpy;
1237 dri2_dpy = dri2_egl_display(disp);
1239 assert(dri2_dpy->ref_count > 0);
1241 if (!p_atomic_dec_zero(&dri2_dpy->ref_count))
1244 _eglCleanupDisplay(disp);
1245 dri2_display_destroy(disp);
1249 dri2_display_destroy(_EGLDisplay *disp)
1251 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1253 if (dri2_dpy->own_dri_screen) {
1254 if (dri2_dpy->vtbl && dri2_dpy->vtbl->close_screen_notify)
1255 dri2_dpy->vtbl->close_screen_notify(disp);
1256 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1258 if (dri2_dpy->fd >= 0)
1259 close(dri2_dpy->fd);
1261 /* Don't dlclose the driver when building with the address sanitizer, so you
1262 * get good symbols from the leak reports.
1264 #if !BUILT_WITH_ASAN || defined(NDEBUG)
1265 if (dri2_dpy->driver)
1266 dlclose(dri2_dpy->driver);
1269 free(dri2_dpy->driver_name);
1271 #ifdef HAVE_WAYLAND_PLATFORM
1272 free(dri2_dpy->device_name);
1275 switch (disp->Platform) {
1276 case _EGL_PLATFORM_X11:
1277 dri2_teardown_x11(dri2_dpy);
1279 case _EGL_PLATFORM_DRM:
1280 dri2_teardown_drm(dri2_dpy);
1282 case _EGL_PLATFORM_WAYLAND:
1283 dri2_teardown_wayland(dri2_dpy);
1286 /* TODO: add teardown for other platforms */
1290 /* The drm platform does not create the screen/driver_configs but reuses
1291 * the ones from the gbm device. As such the gbm itself is responsible
1294 if (disp->Platform != _EGL_PLATFORM_DRM && dri2_dpy->driver_configs) {
1295 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++)
1296 free((__DRIconfig *) dri2_dpy->driver_configs[i]);
1297 free(dri2_dpy->driver_configs);
1300 disp->DriverData = NULL;
1304 dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
1305 unsigned int att, unsigned int format)
1307 struct dri2_egl_display *dri2_dpy =
1308 dri2_egl_display(dri2_surf->base.Resource.Display);
1310 if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
1313 if (!dri2_surf->local_buffers[att]) {
1314 dri2_surf->local_buffers[att] =
1315 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
1316 dri2_surf->base.Width, dri2_surf->base.Height);
1319 return dri2_surf->local_buffers[att];
1323 dri2_egl_surface_free_local_buffers(struct dri2_egl_surface *dri2_surf)
1325 struct dri2_egl_display *dri2_dpy =
1326 dri2_egl_display(dri2_surf->base.Resource.Display);
1328 for (int i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
1329 if (dri2_surf->local_buffers[i]) {
1330 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
1331 dri2_surf->local_buffers[i]);
1332 dri2_surf->local_buffers[i] = NULL;
1338 * Called via eglTerminate(), drv->Terminate().
1340 * This must be guaranteed to be called exactly once, even if eglTerminate is
1341 * called many times (without a eglInitialize in between).
1344 dri2_terminate(_EGLDisplay *disp)
1346 /* Release all non-current Context/Surfaces. */
1347 _eglReleaseDisplayResources(disp);
1349 dri2_display_release(disp);
1355 * Set the error code after a call to
1356 * dri2_egl_display::dri2::createContextAttribs.
1359 dri2_create_context_attribs_error(int dri_error)
1363 switch (dri_error) {
1364 case __DRI_CTX_ERROR_SUCCESS:
1367 case __DRI_CTX_ERROR_NO_MEMORY:
1368 egl_error = EGL_BAD_ALLOC;
1371 /* From the EGL_KHR_create_context spec, section "Errors":
1373 * * If <config> does not support a client API context compatible
1374 * with the requested API major and minor version, [...] context flags,
1375 * and context reset notification behavior (for client API types where
1376 * these attributes are supported), then an EGL_BAD_MATCH error is
1379 * * If an OpenGL ES context is requested and the values for
1380 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1381 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1382 * is not defined, than an EGL_BAD_MATCH error is generated.
1384 * * If an OpenGL context is requested, the requested version is
1385 * greater than 3.2, and the value for attribute
1386 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1387 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1388 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1389 * one of these bits set; or if the implementation does not support
1390 * the requested profile, then an EGL_BAD_MATCH error is generated.
1392 case __DRI_CTX_ERROR_BAD_API:
1393 case __DRI_CTX_ERROR_BAD_VERSION:
1394 case __DRI_CTX_ERROR_BAD_FLAG:
1395 egl_error = EGL_BAD_MATCH;
1398 /* From the EGL_KHR_create_context spec, section "Errors":
1400 * * If an attribute name or attribute value in <attrib_list> is not
1401 * recognized (including unrecognized bits in bitmask attributes),
1402 * then an EGL_BAD_ATTRIBUTE error is generated."
1404 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE:
1405 case __DRI_CTX_ERROR_UNKNOWN_FLAG:
1406 egl_error = EGL_BAD_ATTRIBUTE;
1410 assert(!"unknown dri_error code");
1411 egl_error = EGL_BAD_MATCH;
1415 _eglError(egl_error, "dri2_create_context");
1419 dri2_fill_context_attribs(struct dri2_egl_context *dri2_ctx,
1420 struct dri2_egl_display *dri2_dpy,
1421 uint32_t *ctx_attribs,
1422 unsigned *num_attribs)
1426 assert(*num_attribs >= NUM_ATTRIBS);
1428 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
1429 ctx_attribs[pos++] = dri2_ctx->base.ClientMajorVersion;
1430 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
1431 ctx_attribs[pos++] = dri2_ctx->base.ClientMinorVersion;
1433 if (dri2_ctx->base.Flags != 0) {
1434 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1435 * extension, don't even try to send it the robust-access flag.
1436 * It may explode. Instead, generate the required EGL error here.
1438 if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0
1439 && !dri2_dpy->robustness) {
1440 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1444 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_FLAGS;
1445 ctx_attribs[pos++] = dri2_ctx->base.Flags;
1448 if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) {
1449 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1450 * extension, don't even try to send it a reset strategy. It may
1451 * explode. Instead, generate the required EGL error here.
1453 if (!dri2_dpy->robustness) {
1454 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1458 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
1459 ctx_attribs[pos++] = __DRI_CTX_RESET_LOSE_CONTEXT;
1462 if (dri2_ctx->base.ContextPriority != EGL_CONTEXT_PRIORITY_MEDIUM_IMG) {
1465 switch (dri2_ctx->base.ContextPriority) {
1466 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
1467 val = __DRI_CTX_PRIORITY_HIGH;
1469 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
1470 val = __DRI_CTX_PRIORITY_MEDIUM;
1472 case EGL_CONTEXT_PRIORITY_LOW_IMG:
1473 val = __DRI_CTX_PRIORITY_LOW;
1476 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1480 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_PRIORITY;
1481 ctx_attribs[pos++] = val;
1484 if (dri2_ctx->base.ReleaseBehavior == EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR) {
1485 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
1486 ctx_attribs[pos++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE;
1489 if (dri2_ctx->base.NoError) {
1490 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_NO_ERROR;
1491 ctx_attribs[pos++] = true;
1494 if (dri2_ctx->base.Protected) {
1495 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_PROTECTED;
1496 ctx_attribs[pos++] = true;
1505 * Called via eglCreateContext(), drv->CreateContext().
1507 static _EGLContext *
1508 dri2_create_context(_EGLDisplay *disp, _EGLConfig *conf,
1509 _EGLContext *share_list, const EGLint *attrib_list)
1511 struct dri2_egl_context *dri2_ctx;
1512 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
1513 struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
1514 __DRIcontext *shared =
1515 dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL;
1516 struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
1517 const __DRIconfig *dri_config;
1520 unsigned num_attribs = NUM_ATTRIBS;
1521 uint32_t ctx_attribs[NUM_ATTRIBS];
1523 dri2_ctx = malloc(sizeof *dri2_ctx);
1525 dri2_egl_error_unlock(dri2_dpy, EGL_BAD_ALLOC, "eglCreateContext");
1529 if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
1532 /* The EGL_EXT_create_context_robustness spec says:
1534 * "Add to the eglCreateContext context creation errors: [...]
1536 * * If the reset notification behavior of <share_context> and the
1537 * newly created context are different then an EGL_BAD_MATCH error is
1540 if (share_list && share_list->ResetNotificationStrategy !=
1541 dri2_ctx->base.ResetNotificationStrategy) {
1542 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1546 /* The EGL_KHR_create_context_no_error spec says:
1548 * "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
1549 * used to create <share_context> does not match the value of
1550 * EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
1552 if (share_list && share_list->NoError != dri2_ctx->base.NoError) {
1553 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1557 switch (dri2_ctx->base.ClientAPI) {
1558 case EGL_OPENGL_ES_API:
1559 switch (dri2_ctx->base.ClientMajorVersion) {
1561 api = __DRI_API_GLES;
1564 api = __DRI_API_GLES2;
1567 api = __DRI_API_GLES3;
1570 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1574 case EGL_OPENGL_API:
1575 if ((dri2_ctx->base.ClientMajorVersion >= 4
1576 || (dri2_ctx->base.ClientMajorVersion == 3
1577 && dri2_ctx->base.ClientMinorVersion >= 2))
1578 && dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)
1579 api = __DRI_API_OPENGL_CORE;
1580 else if (dri2_ctx->base.ClientMajorVersion == 3 &&
1581 dri2_ctx->base.ClientMinorVersion == 1)
1582 api = __DRI_API_OPENGL_CORE;
1584 api = __DRI_API_OPENGL;
1587 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1592 /* The config chosen here isn't necessarily
1593 * used for surfaces later.
1594 * A pixmap surface will use the single config.
1595 * This opportunity depends on disabling the
1596 * doubleBufferMode check in
1597 * src/mesa/main/context.c:check_compatible()
1599 if (dri2_config->dri_config[1][0])
1600 dri_config = dri2_config->dri_config[1][0];
1602 dri_config = dri2_config->dri_config[0][0];
1607 if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs,
1611 if (dri2_dpy->image_driver) {
1612 dri2_ctx->dri_context =
1613 dri2_dpy->image_driver->createContextAttribs(dri2_dpy->dri_screen,
1621 dri2_create_context_attribs_error(error);
1622 } else if (dri2_dpy->dri2) {
1623 if (dri2_dpy->dri2->base.version >= 3) {
1624 dri2_ctx->dri_context =
1625 dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen,
1633 dri2_create_context_attribs_error(error);
1635 dri2_ctx->dri_context =
1636 dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
1643 assert(dri2_dpy->swrast);
1644 if (dri2_dpy->swrast->base.version >= 3) {
1645 dri2_ctx->dri_context =
1646 dri2_dpy->swrast->createContextAttribs(dri2_dpy->dri_screen,
1654 dri2_create_context_attribs_error(error);
1656 dri2_ctx->dri_context =
1657 dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,
1665 if (!dri2_ctx->dri_context)
1668 mtx_unlock(&dri2_dpy->lock);
1670 return &dri2_ctx->base;
1673 mtx_unlock(&dri2_dpy->lock);
1679 * Called via eglDestroyContext(), drv->DestroyContext().
1682 dri2_destroy_context(_EGLDisplay *disp, _EGLContext *ctx)
1684 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1685 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1687 if (_eglPutContext(ctx)) {
1688 dri2_dpy->core->destroyContext(dri2_ctx->dri_context);
1696 dri2_init_surface(_EGLSurface *surf, _EGLDisplay *disp, EGLint type,
1697 _EGLConfig *conf, const EGLint *attrib_list,
1698 EGLBoolean enable_out_fence, void *native_surface)
1700 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1701 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1703 dri2_surf->out_fence_fd = -1;
1704 dri2_surf->enable_out_fence = false;
1705 if (dri2_dpy->fence && dri2_dpy->fence->base.version >= 2 &&
1706 dri2_dpy->fence->get_capabilities &&
1707 (dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen) &
1708 __DRI_FENCE_CAP_NATIVE_FD)) {
1709 dri2_surf->enable_out_fence = enable_out_fence;
1712 return _eglInitSurface(surf, disp, type, conf, attrib_list, native_surface);
1716 dri2_surface_set_out_fence_fd( _EGLSurface *surf, int fence_fd)
1718 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1720 if (dri2_surf->out_fence_fd >= 0)
1721 close(dri2_surf->out_fence_fd);
1723 dri2_surf->out_fence_fd = fence_fd;
1727 dri2_fini_surface(_EGLSurface *surf)
1729 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1731 dri2_surface_set_out_fence_fd(surf, -1);
1732 dri2_surf->enable_out_fence = false;
1736 dri2_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
1738 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1739 EGLBoolean ret = EGL_TRUE;
1741 if (_eglPutSurface(surf))
1742 ret = dri2_dpy->vtbl->destroy_surface(disp, surf);
1748 dri2_surf_update_fence_fd(_EGLContext *ctx,
1749 _EGLDisplay *disp, _EGLSurface *surf)
1751 __DRIcontext *dri_ctx = dri2_egl_context(ctx)->dri_context;
1752 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1753 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1757 if (!dri2_surf->enable_out_fence)
1760 fence = dri2_dpy->fence->create_fence_fd(dri_ctx, -1);
1762 fence_fd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
1764 dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, fence);
1766 dri2_surface_set_out_fence_fd(surf, fence_fd);
1770 dri2_create_drawable(struct dri2_egl_display *dri2_dpy,
1771 const __DRIconfig *config,
1772 struct dri2_egl_surface *dri2_surf,
1773 void *loaderPrivate)
1775 if (dri2_dpy->kopper) {
1776 dri2_surf->dri_drawable =
1777 dri2_dpy->kopper->createNewDrawable(dri2_dpy->dri_screen, config, loaderPrivate,
1778 dri2_surf->base.Type == EGL_PBUFFER_BIT ||
1779 dri2_surf->base.Type == EGL_PIXMAP_BIT);
1781 __DRIcreateNewDrawableFunc createNewDrawable;
1782 if (dri2_dpy->image_driver)
1783 createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
1784 else if (dri2_dpy->dri2)
1785 createNewDrawable = dri2_dpy->dri2->createNewDrawable;
1786 else if (dri2_dpy->swrast)
1787 createNewDrawable = dri2_dpy->swrast->createNewDrawable;
1789 return _eglError(EGL_BAD_ALLOC, "no createNewDrawable");
1791 dri2_surf->dri_drawable = createNewDrawable(dri2_dpy->dri_screen,
1792 config, loaderPrivate);
1794 if (dri2_surf->dri_drawable == NULL)
1795 return _eglError(EGL_BAD_ALLOC, "createNewDrawable");
1801 * Called via eglMakeCurrent(), drv->MakeCurrent().
1804 dri2_make_current(_EGLDisplay *disp, _EGLSurface *dsurf,
1805 _EGLSurface *rsurf, _EGLContext *ctx)
1807 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1808 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1809 _EGLDisplay *old_disp = NULL;
1810 struct dri2_egl_display *old_dri2_dpy = NULL;
1811 _EGLContext *old_ctx;
1812 _EGLSurface *old_dsurf, *old_rsurf;
1813 _EGLSurface *tmp_dsurf, *tmp_rsurf;
1814 __DRIdrawable *ddraw, *rdraw;
1816 EGLint egl_error = EGL_SUCCESS;
1819 return _eglError(EGL_NOT_INITIALIZED, "eglMakeCurrent");
1821 /* make new bindings, set the EGL error otherwise */
1822 if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))
1825 if (old_ctx == ctx && old_dsurf == dsurf && old_rsurf == rsurf) {
1826 _eglPutSurface(old_dsurf);
1827 _eglPutSurface(old_rsurf);
1828 _eglPutContext(old_ctx);
1833 __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context;
1834 old_disp = old_ctx->Resource.Display;
1835 old_dri2_dpy = dri2_egl_display(old_disp);
1837 /* Disable shared buffer mode */
1838 if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1839 old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1840 old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, false);
1843 old_dri2_dpy->core->unbindContext(old_cctx);
1846 dri2_surf_update_fence_fd(old_ctx, old_disp, old_dsurf);
1849 ddraw = (dsurf) ? dri2_dpy->vtbl->get_dri_drawable(dsurf) : NULL;
1850 rdraw = (rsurf) ? dri2_dpy->vtbl->get_dri_drawable(rsurf) : NULL;
1851 cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
1854 if (!dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1855 _EGLContext *tmp_ctx;
1857 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1858 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1861 egl_error = EGL_BAD_MATCH;
1863 /* undo the previous _eglBindContext */
1864 _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &tmp_dsurf, &tmp_rsurf);
1865 assert(&dri2_ctx->base == ctx &&
1866 tmp_dsurf == dsurf &&
1867 tmp_rsurf == rsurf);
1869 _eglPutSurface(dsurf);
1870 _eglPutSurface(rsurf);
1871 _eglPutContext(ctx);
1873 _eglPutSurface(old_dsurf);
1874 _eglPutSurface(old_rsurf);
1875 _eglPutContext(old_ctx);
1877 ddraw = (old_dsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_dsurf) : NULL;
1878 rdraw = (old_rsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_rsurf) : NULL;
1879 cctx = (old_ctx) ? dri2_egl_context(old_ctx)->dri_context : NULL;
1881 /* undo the previous dri2_dpy->core->unbindContext */
1882 if (dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1883 if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1884 old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1885 old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, true);
1888 return _eglError(egl_error, "eglMakeCurrent");
1891 /* We cannot restore the same state as it was before calling
1892 * eglMakeCurrent() and the spec isn't clear about what to do. We
1893 * can prevent EGL from calling into the DRI driver with no DRI
1896 dsurf = rsurf = NULL;
1899 _eglBindContext(ctx, dsurf, rsurf, &tmp_ctx, &tmp_dsurf, &tmp_rsurf);
1900 assert(tmp_ctx == old_ctx && tmp_dsurf == old_dsurf &&
1901 tmp_rsurf == old_rsurf);
1903 _eglLog(_EGL_WARNING, "DRI2: failed to rebind the previous context");
1905 /* dri2_dpy->core->bindContext succeeded, so take a reference on the
1906 * dri2_dpy. This prevents dri2_dpy from being reinitialized when a
1907 * EGLDisplay is terminated and then initialized again while a
1908 * context is still bound. See dri2_initialize() for a more in depth
1910 p_atomic_inc(&dri2_dpy->ref_count);
1914 dri2_destroy_surface(disp, old_dsurf);
1915 dri2_destroy_surface(disp, old_rsurf);
1918 dri2_destroy_context(disp, old_ctx);
1919 dri2_display_release(old_disp);
1922 if (egl_error != EGL_SUCCESS)
1923 return _eglError(egl_error, "eglMakeCurrent");
1925 if (dsurf && _eglSurfaceHasMutableRenderBuffer(dsurf) &&
1926 dri2_dpy->vtbl->set_shared_buffer_mode) {
1927 /* Always update the shared buffer mode. This is obviously needed when
1928 * the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When
1929 * EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the
1930 * case where external non-EGL API may have changed window's shared
1931 * buffer mode since we last saw it.
1933 bool mode = (dsurf->ActiveRenderBuffer == EGL_SINGLE_BUFFER);
1934 dri2_dpy->vtbl->set_shared_buffer_mode(disp, dsurf, mode);
1941 dri2_surface_get_dri_drawable(_EGLSurface *surf)
1943 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1945 return dri2_surf->dri_drawable;
1949 * Called from eglGetProcAddress() via drv->GetProcAddress().
1952 dri2_get_proc_address(const char *procname)
1954 return _glapi_get_proc_address(procname);
1958 dri2_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
1959 void *native_window, const EGLint *attrib_list)
1961 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
1963 dri2_dpy->vtbl->create_window_surface(disp, conf, native_window,
1965 mtx_unlock(&dri2_dpy->lock);
1970 dri2_create_pixmap_surface(_EGLDisplay *disp, _EGLConfig *conf,
1971 void *native_pixmap, const EGLint *attrib_list)
1973 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
1974 _EGLSurface *ret = NULL;
1976 if (dri2_dpy->vtbl->create_pixmap_surface)
1977 ret = dri2_dpy->vtbl->create_pixmap_surface(disp, conf, native_pixmap,
1980 mtx_unlock(&dri2_dpy->lock);
1986 dri2_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
1987 const EGLint *attrib_list)
1989 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
1990 _EGLSurface *ret = NULL;
1992 if (dri2_dpy->vtbl->create_pbuffer_surface)
1993 ret = dri2_dpy->vtbl->create_pbuffer_surface(disp, conf, attrib_list);
1995 mtx_unlock(&dri2_dpy->lock);
2001 dri2_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
2003 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
2004 EGLBoolean ret = EGL_TRUE;
2006 if (dri2_dpy->vtbl->swap_interval)
2007 ret = dri2_dpy->vtbl->swap_interval(disp, surf, interval);
2009 mtx_unlock(&dri2_dpy->lock);
2015 * Asks the client API to flush any rendering to the drawable so that we can
2016 * do our swapbuffers.
2019 dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw)
2021 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2022 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(draw);
2024 if (dri2_dpy->flush) {
2025 if (dri2_dpy->flush->base.version >= 4) {
2026 /* We know there's a current context because:
2028 * "If surface is not bound to the calling thread’s current
2029 * context, an EGL_BAD_SURFACE error is generated."
2031 _EGLContext *ctx = _eglGetCurrentContext();
2032 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2034 /* From the EGL 1.4 spec (page 52):
2036 * "The contents of ancillary buffers are always undefined
2037 * after calling eglSwapBuffers."
2039 dri2_dpy->flush->flush_with_flags(dri2_ctx->dri_context,
2041 __DRI2_FLUSH_DRAWABLE |
2042 __DRI2_FLUSH_INVALIDATE_ANCILLARY,
2043 __DRI2_THROTTLE_SWAPBUFFER);
2045 dri2_dpy->flush->flush(dri_drawable);
2051 dri2_swap_buffers(_EGLDisplay *disp, _EGLSurface *surf)
2053 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2054 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2055 _EGLContext *ctx = _eglGetCurrentContext();
2059 dri2_surf_update_fence_fd(ctx, disp, surf);
2060 ret = dri2_dpy->vtbl->swap_buffers(disp, surf);
2062 /* SwapBuffers marks the end of the frame; reset the damage region for
2063 * use again next time.
2065 if (ret && dri2_dpy->buffer_damage &&
2066 dri2_dpy->buffer_damage->set_damage_region)
2067 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2073 dri2_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *surf,
2074 const EGLint *rects, EGLint n_rects)
2076 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2077 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2078 _EGLContext *ctx = _eglGetCurrentContext();
2082 dri2_surf_update_fence_fd(ctx, disp, surf);
2083 if (dri2_dpy->vtbl->swap_buffers_with_damage)
2084 ret = dri2_dpy->vtbl->swap_buffers_with_damage(disp, surf,
2087 ret = dri2_dpy->vtbl->swap_buffers(disp, surf);
2089 /* SwapBuffers marks the end of the frame; reset the damage region for
2090 * use again next time.
2092 if (ret && dri2_dpy->buffer_damage &&
2093 dri2_dpy->buffer_damage->set_damage_region)
2094 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2100 dri2_swap_buffers_region(_EGLDisplay *disp, _EGLSurface *surf,
2101 EGLint numRects, const EGLint *rects)
2103 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2104 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2107 if (!dri2_dpy->vtbl->swap_buffers_region)
2109 ret = dri2_dpy->vtbl->swap_buffers_region(disp, surf, numRects, rects);
2111 /* SwapBuffers marks the end of the frame; reset the damage region for
2112 * use again next time.
2114 if (ret && dri2_dpy->buffer_damage &&
2115 dri2_dpy->buffer_damage->set_damage_region)
2116 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2122 dri2_set_damage_region(_EGLDisplay *disp, _EGLSurface *surf,
2123 EGLint *rects, EGLint n_rects)
2125 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
2126 __DRIdrawable *drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2128 if (!dri2_dpy->buffer_damage || !dri2_dpy->buffer_damage->set_damage_region) {
2129 mtx_unlock(&dri2_dpy->lock);
2133 dri2_dpy->buffer_damage->set_damage_region(drawable, n_rects, rects);
2134 mtx_unlock(&dri2_dpy->lock);
2139 dri2_post_sub_buffer(_EGLDisplay *disp, _EGLSurface *surf,
2140 EGLint x, EGLint y, EGLint width, EGLint height)
2142 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
2143 EGLBoolean ret = EGL_FALSE;
2145 if (dri2_dpy->vtbl->post_sub_buffer)
2146 ret = dri2_dpy->vtbl->post_sub_buffer(disp, surf, x, y, width, height);
2148 mtx_unlock(&dri2_dpy->lock);
2154 dri2_copy_buffers(_EGLDisplay *disp, _EGLSurface *surf, void *native_pixmap_target)
2156 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
2157 if (!dri2_dpy->vtbl->copy_buffers)
2158 return dri2_egl_error_unlock(dri2_dpy, EGL_BAD_NATIVE_PIXMAP, "no support for native pixmaps");
2159 EGLBoolean ret = dri2_dpy->vtbl->copy_buffers(disp, surf, native_pixmap_target);
2160 mtx_unlock(&dri2_dpy->lock);
2165 dri2_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surf)
2167 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2168 if (!dri2_dpy->vtbl->query_buffer_age)
2170 return dri2_dpy->vtbl->query_buffer_age(disp, surf);
2174 dri2_wait_client(_EGLDisplay *disp, _EGLContext *ctx)
2176 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2177 _EGLSurface *surf = ctx->DrawSurface;
2178 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2180 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
2181 * we need to copy fake to real here.*/
2183 if (dri2_dpy->flush != NULL)
2184 dri2_dpy->flush->flush(dri_drawable);
2190 dri2_wait_native(EGLint engine)
2192 if (engine != EGL_CORE_NATIVE_ENGINE)
2193 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
2200 dri2_bind_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
2202 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
2203 struct dri2_egl_context *dri2_ctx;
2205 GLint format, target;
2206 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2208 ctx = _eglGetCurrentContext();
2209 dri2_ctx = dri2_egl_context(ctx);
2211 if (!_eglBindTexImage(disp, surf, buffer)) {
2212 mtx_unlock(&dri2_dpy->lock);
2216 switch (surf->TextureFormat) {
2217 case EGL_TEXTURE_RGB:
2218 format = __DRI_TEXTURE_FORMAT_RGB;
2220 case EGL_TEXTURE_RGBA:
2221 format = __DRI_TEXTURE_FORMAT_RGBA;
2224 assert(!"Unexpected texture format in dri2_bind_tex_image()");
2225 format = __DRI_TEXTURE_FORMAT_RGBA;
2228 switch (surf->TextureTarget) {
2229 case EGL_TEXTURE_2D:
2230 target = GL_TEXTURE_2D;
2233 target = GL_TEXTURE_2D;
2234 assert(!"Unexpected texture target in dri2_bind_tex_image()");
2237 dri2_dpy->tex_buffer->setTexBuffer2(dri2_ctx->dri_context,
2241 mtx_unlock(&dri2_dpy->lock);
2247 dri2_release_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
2249 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
2250 struct dri2_egl_context *dri2_ctx;
2253 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2255 ctx = _eglGetCurrentContext();
2256 dri2_ctx = dri2_egl_context(ctx);
2258 if (!_eglReleaseTexImage(disp, surf, buffer)) {
2259 mtx_unlock(&dri2_dpy->lock);
2263 switch (surf->TextureTarget) {
2264 case EGL_TEXTURE_2D:
2265 target = GL_TEXTURE_2D;
2268 assert(!"missing texture target");
2271 if (dri2_dpy->tex_buffer->base.version >= 3 &&
2272 dri2_dpy->tex_buffer->releaseTexBuffer != NULL) {
2273 dri2_dpy->tex_buffer->releaseTexBuffer(dri2_ctx->dri_context,
2274 target, dri_drawable);
2277 mtx_unlock(&dri2_dpy->lock);
2283 dri2_create_image(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
2284 EGLClientBuffer buffer, const EGLint *attr_list)
2286 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
2287 _EGLImage *ret = dri2_dpy->vtbl->create_image(disp, ctx, target, buffer,
2289 mtx_unlock(&dri2_dpy->lock);
2294 dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image)
2296 struct dri2_egl_image *dri2_img;
2298 if (dri_image == NULL) {
2299 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
2303 dri2_img = malloc(sizeof *dri2_img);
2305 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
2309 _eglInitImage(&dri2_img->base, disp);
2311 dri2_img->dri_image = dri_image;
2313 return &dri2_img->base;
2317 * Translate a DRI Image extension error code into an EGL error code.
2320 egl_error_from_dri_image_error(int dri_error)
2322 switch (dri_error) {
2323 case __DRI_IMAGE_ERROR_SUCCESS:
2325 case __DRI_IMAGE_ERROR_BAD_ALLOC:
2326 return EGL_BAD_ALLOC;
2327 case __DRI_IMAGE_ERROR_BAD_MATCH:
2328 return EGL_BAD_MATCH;
2329 case __DRI_IMAGE_ERROR_BAD_PARAMETER:
2330 return EGL_BAD_PARAMETER;
2331 case __DRI_IMAGE_ERROR_BAD_ACCESS:
2332 return EGL_BAD_ACCESS;
2334 assert(!"unknown dri_error code");
2335 return EGL_BAD_ALLOC;
2340 dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
2341 EGLClientBuffer buffer,
2342 const EGLint *attr_list)
2344 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2345 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2346 GLuint renderbuffer = (GLuint) (uintptr_t) buffer;
2347 __DRIimage *dri_image;
2349 if (renderbuffer == 0) {
2350 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2351 return EGL_NO_IMAGE_KHR;
2354 if (!disp->Extensions.KHR_gl_renderbuffer_image) {
2355 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2356 return EGL_NO_IMAGE_KHR;
2359 if (dri2_dpy->image->base.version >= 17 &&
2360 dri2_dpy->image->createImageFromRenderbuffer2) {
2361 unsigned error = ~0;
2363 dri_image = dri2_dpy->image->createImageFromRenderbuffer2(
2364 dri2_ctx->dri_context, renderbuffer, NULL, &error);
2366 assert(!!dri_image == (error == __DRI_IMAGE_ERROR_SUCCESS));
2369 _eglError(egl_error_from_dri_image_error(error), "dri2_create_image_khr");
2370 return EGL_NO_IMAGE_KHR;
2373 dri_image = dri2_dpy->image->createImageFromRenderbuffer(
2374 dri2_ctx->dri_context, renderbuffer, NULL);
2376 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2377 return EGL_NO_IMAGE_KHR;
2381 return dri2_create_image_from_dri(disp, dri_image);
2384 #ifdef HAVE_WAYLAND_PLATFORM
2386 /* This structure describes how a wl_buffer maps to one or more
2387 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
2388 * offsets and strides of the planes in the buffer. This table maps a
2389 * wl_drm format code to a description of the planes in the buffer
2390 * that lets us create a __DRIimage for each of the planes. */
2392 static const struct wl_drm_components_descriptor {
2393 uint32_t dri_components;
2396 } wl_drm_components[] = {
2397 { __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 },
2398 { __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 },
2399 { __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 },
2400 { __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 },
2401 { __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 },
2405 dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx,
2406 EGLClientBuffer _buffer,
2407 const EGLint *attr_list)
2409 struct wl_drm_buffer *buffer;
2410 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2411 const struct wl_drm_components_descriptor *f;
2412 __DRIimage *dri_image;
2413 _EGLImageAttribs attrs;
2416 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm,
2417 (struct wl_resource *) _buffer);
2421 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2424 plane = attrs.PlaneWL;
2425 f = buffer->driver_format;
2426 if (plane < 0 || plane >= f->nplanes) {
2427 _eglError(EGL_BAD_PARAMETER,
2428 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
2432 dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL);
2433 if (dri_image == NULL && plane == 0)
2434 dri_image = dri2_dpy->image->dupImage(buffer->driver_buffer, NULL);
2435 if (dri_image == NULL) {
2436 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
2440 return dri2_create_image_from_dri(disp, dri_image);
2445 dri2_get_sync_values_chromium(_EGLDisplay *disp, _EGLSurface *surf,
2446 EGLuint64KHR *ust, EGLuint64KHR *msc,
2449 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2450 EGLBoolean ret = EGL_FALSE;
2452 if (dri2_dpy->vtbl->get_sync_values)
2453 ret = dri2_dpy->vtbl->get_sync_values(disp, surf, ust, msc, sbc);
2455 mtx_unlock(&dri2_dpy->lock);
2461 dri2_get_msc_rate_angle(_EGLDisplay *disp, _EGLSurface *surf,
2462 EGLint *numerator, EGLint *denominator)
2464 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2465 if (!dri2_dpy->vtbl->get_msc_rate)
2467 return dri2_dpy->vtbl->get_msc_rate(disp, surf, numerator, denominator);
2471 * Set the error code after a call to
2472 * dri2_egl_image::dri_image::createImageFromTexture.
2475 dri2_create_image_khr_texture_error(int dri_error)
2477 EGLint egl_error = egl_error_from_dri_image_error(dri_error);
2479 if (egl_error != EGL_SUCCESS)
2480 _eglError(egl_error, "dri2_create_image_khr_texture");
2484 dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx,
2486 EGLClientBuffer buffer,
2487 const EGLint *attr_list)
2489 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2490 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2491 struct dri2_egl_image *dri2_img;
2492 GLuint texture = (GLuint) (uintptr_t) buffer;
2493 _EGLImageAttribs attrs;
2499 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2500 return EGL_NO_IMAGE_KHR;
2503 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2504 return EGL_NO_IMAGE_KHR;
2507 case EGL_GL_TEXTURE_2D_KHR:
2508 if (!disp->Extensions.KHR_gl_texture_2D_image) {
2509 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2510 return EGL_NO_IMAGE_KHR;
2513 gl_target = GL_TEXTURE_2D;
2515 case EGL_GL_TEXTURE_3D_KHR:
2516 if (!disp->Extensions.KHR_gl_texture_3D_image) {
2517 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2518 return EGL_NO_IMAGE_KHR;
2521 depth = attrs.GLTextureZOffset;
2522 gl_target = GL_TEXTURE_3D;
2524 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
2525 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
2526 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
2527 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
2528 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
2529 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
2530 if (!disp->Extensions.KHR_gl_texture_cubemap_image) {
2531 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2532 return EGL_NO_IMAGE_KHR;
2535 depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
2536 gl_target = GL_TEXTURE_CUBE_MAP;
2539 unreachable("Unexpected target in dri2_create_image_khr_texture()");
2540 return EGL_NO_IMAGE_KHR;
2543 dri2_img = malloc(sizeof *dri2_img);
2545 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2546 return EGL_NO_IMAGE_KHR;
2549 _eglInitImage(&dri2_img->base, disp);
2551 dri2_img->dri_image =
2552 dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context,
2556 attrs.GLTextureLevel,
2559 dri2_create_image_khr_texture_error(error);
2561 if (!dri2_img->dri_image) {
2563 return EGL_NO_IMAGE_KHR;
2565 return &dri2_img->base;
2569 dri2_query_surface(_EGLDisplay *disp, _EGLSurface *surf,
2570 EGLint attribute, EGLint *value)
2572 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2575 if (!dri2_dpy->vtbl->query_surface) {
2576 ret = _eglQuerySurface(disp, surf, attribute, value);
2578 ret = dri2_dpy->vtbl->query_surface(disp, surf, attribute, value);
2581 mtx_unlock(&dri2_dpy->lock);
2586 static struct wl_buffer*
2587 dri2_create_wayland_buffer_from_image(_EGLDisplay *disp, _EGLImage *img)
2589 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
2590 struct wl_buffer *ret = NULL;
2592 if (dri2_dpy->vtbl->create_wayland_buffer_from_image)
2593 ret = dri2_dpy->vtbl->create_wayland_buffer_from_image(disp, img);
2595 mtx_unlock(&dri2_dpy->lock);
2602 dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
2603 EGLClientBuffer buffer, const EGLint *attr_list)
2605 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2606 EGLint format, name, pitch;
2607 _EGLImageAttribs attrs;
2608 __DRIimage *dri_image;
2610 name = (EGLint) (uintptr_t) buffer;
2612 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2615 if (attrs.Width <= 0 || attrs.Height <= 0 ||
2616 attrs.DRMBufferStrideMESA <= 0) {
2617 _eglError(EGL_BAD_PARAMETER,
2618 "bad width, height or stride");
2622 switch (attrs.DRMBufferFormatMESA) {
2623 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2624 format = __DRI_IMAGE_FORMAT_ARGB8888;
2625 pitch = attrs.DRMBufferStrideMESA;
2628 _eglError(EGL_BAD_PARAMETER,
2629 "dri2_create_image_khr: unsupported pixmap depth");
2634 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
2642 return dri2_create_image_from_dri(disp, dri_image);
2646 dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)
2651 * "Required attributes and their values are as follows:
2653 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
2655 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
2656 * by drm_fourcc.h and used as the pixel_format parameter of the
2657 * drm_mode_fb_cmd2 ioctl."
2661 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2662 * incomplete, EGL_BAD_PARAMETER is generated."
2664 if (attrs->Width <= 0 || attrs->Height <= 0 ||
2665 !attrs->DMABufFourCC.IsPresent)
2666 return _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");
2671 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
2672 * specified for a plane's pitch or offset isn't supported by EGL,
2673 * EGL_BAD_ACCESS is generated."
2675 for (unsigned i = 0; i < ARRAY_SIZE(attrs->DMABufPlanePitches); ++i) {
2676 if (attrs->DMABufPlanePitches[i].IsPresent &&
2677 attrs->DMABufPlanePitches[i].Value <= 0)
2678 return _eglError(EGL_BAD_ACCESS, "invalid pitch");
2682 * If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2683 * attribute values may be given.
2685 * This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2686 * EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2688 for (unsigned i = 0; i < DMA_BUF_MAX_PLANES; ++i) {
2689 if (attrs->DMABufPlaneModifiersLo[i].IsPresent !=
2690 attrs->DMABufPlaneModifiersHi[i].IsPresent)
2691 return _eglError(EGL_BAD_PARAMETER, "modifier attribute lo or hi missing");
2694 /* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2695 * mandate it, we only accept the same modifier across all planes. */
2696 for (unsigned i = 1; i < DMA_BUF_MAX_PLANES; ++i) {
2697 if (attrs->DMABufPlaneFds[i].IsPresent) {
2698 if ((attrs->DMABufPlaneModifiersLo[0].IsPresent !=
2699 attrs->DMABufPlaneModifiersLo[i].IsPresent) ||
2700 (attrs->DMABufPlaneModifiersLo[0].Value !=
2701 attrs->DMABufPlaneModifiersLo[i].Value) ||
2702 (attrs->DMABufPlaneModifiersHi[0].Value !=
2703 attrs->DMABufPlaneModifiersHi[i].Value))
2704 return _eglError(EGL_BAD_PARAMETER, "modifier attributes not equal");
2711 /* Returns the total number of planes for the format or zero if it isn't a
2712 * valid fourcc format.
2715 dri2_num_fourcc_format_planes(EGLint format)
2719 case DRM_FORMAT_RG88:
2720 case DRM_FORMAT_GR88:
2721 case DRM_FORMAT_R16:
2722 case DRM_FORMAT_GR1616:
2723 case DRM_FORMAT_RGB332:
2724 case DRM_FORMAT_BGR233:
2725 case DRM_FORMAT_XRGB4444:
2726 case DRM_FORMAT_XBGR4444:
2727 case DRM_FORMAT_RGBX4444:
2728 case DRM_FORMAT_BGRX4444:
2729 case DRM_FORMAT_ARGB4444:
2730 case DRM_FORMAT_ABGR4444:
2731 case DRM_FORMAT_RGBA4444:
2732 case DRM_FORMAT_BGRA4444:
2733 case DRM_FORMAT_XRGB1555:
2734 case DRM_FORMAT_XBGR1555:
2735 case DRM_FORMAT_RGBX5551:
2736 case DRM_FORMAT_BGRX5551:
2737 case DRM_FORMAT_ARGB1555:
2738 case DRM_FORMAT_ABGR1555:
2739 case DRM_FORMAT_RGBA5551:
2740 case DRM_FORMAT_BGRA5551:
2741 case DRM_FORMAT_RGB565:
2742 case DRM_FORMAT_BGR565:
2743 case DRM_FORMAT_RGB888:
2744 case DRM_FORMAT_BGR888:
2745 case DRM_FORMAT_XRGB8888:
2746 case DRM_FORMAT_XBGR8888:
2747 case DRM_FORMAT_RGBX8888:
2748 case DRM_FORMAT_BGRX8888:
2749 case DRM_FORMAT_ARGB8888:
2750 case DRM_FORMAT_ABGR8888:
2751 case DRM_FORMAT_RGBA8888:
2752 case DRM_FORMAT_BGRA8888:
2753 case DRM_FORMAT_XRGB2101010:
2754 case DRM_FORMAT_XBGR2101010:
2755 case DRM_FORMAT_RGBX1010102:
2756 case DRM_FORMAT_BGRX1010102:
2757 case DRM_FORMAT_ARGB2101010:
2758 case DRM_FORMAT_ABGR2101010:
2759 case DRM_FORMAT_RGBA1010102:
2760 case DRM_FORMAT_BGRA1010102:
2761 case DRM_FORMAT_ABGR16161616:
2762 case DRM_FORMAT_XBGR16161616:
2763 case DRM_FORMAT_XBGR16161616F:
2764 case DRM_FORMAT_ABGR16161616F:
2765 case DRM_FORMAT_YUYV:
2766 case DRM_FORMAT_YVYU:
2767 case DRM_FORMAT_UYVY:
2768 case DRM_FORMAT_VYUY:
2769 case DRM_FORMAT_AYUV:
2770 case DRM_FORMAT_XYUV8888:
2771 case DRM_FORMAT_Y210:
2772 case DRM_FORMAT_Y212:
2773 case DRM_FORMAT_Y216:
2774 case DRM_FORMAT_Y410:
2775 case DRM_FORMAT_Y412:
2776 case DRM_FORMAT_Y416:
2779 case DRM_FORMAT_NV12:
2780 case DRM_FORMAT_NV21:
2781 case DRM_FORMAT_NV16:
2782 case DRM_FORMAT_NV61:
2783 case DRM_FORMAT_P010:
2784 case DRM_FORMAT_P012:
2785 case DRM_FORMAT_P016:
2788 case DRM_FORMAT_YUV410:
2789 case DRM_FORMAT_YVU410:
2790 case DRM_FORMAT_YUV411:
2791 case DRM_FORMAT_YVU411:
2792 case DRM_FORMAT_YUV420:
2793 case DRM_FORMAT_YVU420:
2794 case DRM_FORMAT_YUV422:
2795 case DRM_FORMAT_YVU422:
2796 case DRM_FORMAT_YUV444:
2797 case DRM_FORMAT_YVU444:
2805 /* Returns the total number of file descriptors. Zero indicates an error. */
2807 dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)
2809 unsigned plane_n = dri2_num_fourcc_format_planes(attrs->DMABufFourCC.Value);
2811 _eglError(EGL_BAD_MATCH, "unknown drm fourcc format");
2815 for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; i++) {
2817 * The modifiers extension spec says:
2819 * "Modifiers may modify any attribute of a buffer import, including
2820 * but not limited to adding extra planes to a format which
2821 * otherwise does not have those planes. As an example, a modifier
2822 * may add a plane for an external compression buffer to a
2823 * single-plane format. The exact meaning and effect of any
2824 * modifier is canonically defined by drm_fourcc.h, not as part of
2827 if (attrs->DMABufPlaneModifiersLo[i].IsPresent &&
2828 attrs->DMABufPlaneModifiersHi[i].IsPresent) {
2836 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2837 * incomplete, EGL_BAD_PARAMETER is generated."
2839 for (unsigned i = 0; i < plane_n; ++i) {
2840 if (!attrs->DMABufPlaneFds[i].IsPresent ||
2841 !attrs->DMABufPlaneOffsets[i].IsPresent ||
2842 !attrs->DMABufPlanePitches[i].IsPresent) {
2843 _eglError(EGL_BAD_PARAMETER, "plane attribute(s) missing");
2849 * The spec also says:
2851 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2852 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2853 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2854 * or EGL_DMA_BUF_PLANE3_* attributes are specified."
2856 for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; ++i) {
2857 if (attrs->DMABufPlaneFds[i].IsPresent ||
2858 attrs->DMABufPlaneOffsets[i].IsPresent ||
2859 attrs->DMABufPlanePitches[i].IsPresent) {
2860 _eglError(EGL_BAD_ATTRIBUTE, "too many plane attributes");
2869 dri2_query_dma_buf_formats(_EGLDisplay *disp, EGLint max,
2870 EGLint *formats, EGLint *count)
2872 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
2873 if (max < 0 || (max > 0 && formats == NULL)) {
2874 _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
2878 if (dri2_dpy->image->base.version < 15 ||
2879 dri2_dpy->image->queryDmaBufFormats == NULL)
2882 if (!dri2_dpy->image->queryDmaBufFormats(dri2_dpy->dri_screen, max,
2887 /* Assert that all of the formats returned are actually fourcc formats.
2888 * Some day, if we want the internal interface function to be able to
2889 * return the fake fourcc formats defined in dri_interface.h, we'll have
2890 * to do something more clever here to pair the list down to just real
2891 * fourcc formats so that we don't leak the fake internal ones.
2893 for (int i = 0; i < *count; i++) {
2894 assert(dri2_num_fourcc_format_planes(formats[i]) > 0);
2898 mtx_unlock(&dri2_dpy->lock);
2903 mtx_unlock(&dri2_dpy->lock);
2908 dri2_query_dma_buf_modifiers(_EGLDisplay *disp, EGLint format,
2909 EGLint max, EGLuint64KHR *modifiers,
2910 EGLBoolean *external_only, EGLint *count)
2912 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
2914 if (dri2_num_fourcc_format_planes(format) == 0)
2915 return dri2_egl_error_unlock(dri2_dpy, EGL_BAD_PARAMETER, "invalid fourcc format");
2918 return dri2_egl_error_unlock(dri2_dpy, EGL_BAD_PARAMETER, "invalid value for max count of formats");
2920 if (max > 0 && modifiers == NULL)
2921 return dri2_egl_error_unlock(dri2_dpy, EGL_BAD_PARAMETER, "invalid modifiers array");
2923 if (dri2_dpy->image->base.version < 15 ||
2924 dri2_dpy->image->queryDmaBufModifiers == NULL) {
2925 mtx_unlock(&dri2_dpy->lock);
2929 if (dri2_dpy->image->queryDmaBufModifiers(dri2_dpy->dri_screen, format,
2931 (unsigned int *) external_only,
2933 return dri2_egl_error_unlock(dri2_dpy, EGL_BAD_PARAMETER, "invalid format");
2935 mtx_unlock(&dri2_dpy->lock);
2943 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2944 * EGL will take a reference to the dma_buf(s) which it will release at any
2945 * time while the EGLDisplay is initialized. It is the responsibility of the
2946 * application to close the dma_buf file descriptors."
2948 * Therefore we must never close or otherwise modify the file descriptors.
2951 dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
2952 EGLClientBuffer buffer, const EGLint *attr_list)
2954 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2956 _EGLImageAttribs attrs;
2957 __DRIimage *dri_image;
2959 int fds[DMA_BUF_MAX_PLANES];
2960 int pitches[DMA_BUF_MAX_PLANES];
2961 int offsets[DMA_BUF_MAX_PLANES];
2963 bool has_modifier = false;
2970 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2971 * error EGL_BAD_PARAMETER is generated."
2973 if (buffer != NULL) {
2974 _eglError(EGL_BAD_PARAMETER, "buffer not NULL");
2978 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2981 if (!dri2_check_dma_buf_attribs(&attrs))
2984 num_fds = dri2_check_dma_buf_format(&attrs);
2988 for (unsigned i = 0; i < num_fds; ++i) {
2989 fds[i] = attrs.DMABufPlaneFds[i].Value;
2990 pitches[i] = attrs.DMABufPlanePitches[i].Value;
2991 offsets[i] = attrs.DMABufPlaneOffsets[i].Value;
2994 /* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2995 * will be present in attrs.DMABufPlaneModifiersLo[0] and
2996 * attrs.DMABufPlaneModifiersHi[0] */
2997 if (attrs.DMABufPlaneModifiersLo[0].IsPresent) {
2998 modifier = combine_u32_into_u64(attrs.DMABufPlaneModifiersHi[0].Value,
2999 attrs.DMABufPlaneModifiersLo[0].Value);
3000 has_modifier = true;
3003 if (attrs.ProtectedContent) {
3004 if (dri2_dpy->image->base.version < 18 ||
3005 dri2_dpy->image->createImageFromDmaBufs3 == NULL) {
3006 _eglError(EGL_BAD_MATCH, "unsupported protected_content attribute");
3007 return EGL_NO_IMAGE_KHR;
3010 modifier = DRM_FORMAT_MOD_INVALID;
3013 dri2_dpy->image->createImageFromDmaBufs3(dri2_dpy->dri_screen,
3014 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
3015 modifier, fds, num_fds, pitches, offsets,
3016 attrs.DMABufYuvColorSpaceHint.Value,
3017 attrs.DMABufSampleRangeHint.Value,
3018 attrs.DMABufChromaHorizontalSiting.Value,
3019 attrs.DMABufChromaVerticalSiting.Value,
3020 attrs.ProtectedContent ? __DRI_IMAGE_PROTECTED_CONTENT_FLAG : 0,
3024 else if (has_modifier) {
3025 if (dri2_dpy->image->base.version < 15 ||
3026 dri2_dpy->image->createImageFromDmaBufs2 == NULL) {
3027 _eglError(EGL_BAD_MATCH, "unsupported dma_buf format modifier");
3028 return EGL_NO_IMAGE_KHR;
3031 dri2_dpy->image->createImageFromDmaBufs2(dri2_dpy->dri_screen,
3032 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
3033 modifier, fds, num_fds, pitches, offsets,
3034 attrs.DMABufYuvColorSpaceHint.Value,
3035 attrs.DMABufSampleRangeHint.Value,
3036 attrs.DMABufChromaHorizontalSiting.Value,
3037 attrs.DMABufChromaVerticalSiting.Value,
3043 dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen,
3044 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
3045 fds, num_fds, pitches, offsets,
3046 attrs.DMABufYuvColorSpaceHint.Value,
3047 attrs.DMABufSampleRangeHint.Value,
3048 attrs.DMABufChromaHorizontalSiting.Value,
3049 attrs.DMABufChromaVerticalSiting.Value,
3054 egl_error = egl_error_from_dri_image_error(error);
3055 if (egl_error != EGL_SUCCESS)
3056 _eglError(egl_error, "createImageFromDmaBufs failed");
3059 return EGL_NO_IMAGE_KHR;
3061 res = dri2_create_image_from_dri(disp, dri_image);
3066 dri2_create_drm_image_mesa(_EGLDisplay *disp, const EGLint *attr_list)
3068 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
3069 struct dri2_egl_image *dri2_img;
3070 _EGLImageAttribs attrs;
3071 unsigned int dri_use, valid_mask;
3075 _eglError(EGL_BAD_PARAMETER, __func__);
3079 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
3082 if (attrs.Width <= 0 || attrs.Height <= 0) {
3083 _eglError(EGL_BAD_PARAMETER, __func__);
3087 switch (attrs.DRMBufferFormatMESA) {
3088 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
3089 format = __DRI_IMAGE_FORMAT_ARGB8888;
3092 _eglError(EGL_BAD_PARAMETER, __func__);
3097 EGL_DRM_BUFFER_USE_SCANOUT_MESA |
3098 EGL_DRM_BUFFER_USE_SHARE_MESA |
3099 EGL_DRM_BUFFER_USE_CURSOR_MESA;
3100 if (attrs.DRMBufferUseMESA & ~valid_mask) {
3101 _eglError(EGL_BAD_PARAMETER, __func__);
3106 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
3107 dri_use |= __DRI_IMAGE_USE_SHARE;
3108 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
3109 dri_use |= __DRI_IMAGE_USE_SCANOUT;
3110 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA)
3111 dri_use |= __DRI_IMAGE_USE_CURSOR;
3113 dri2_img = malloc(sizeof *dri2_img);
3115 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
3119 _eglInitImage(&dri2_img->base, disp);
3121 dri2_img->dri_image =
3122 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
3123 attrs.Width, attrs.Height,
3124 format, dri_use, dri2_img);
3125 if (dri2_img->dri_image == NULL) {
3127 _eglError(EGL_BAD_ALLOC, "dri2_create_drm_image_mesa");
3131 mtx_unlock(&dri2_dpy->lock);
3133 return &dri2_img->base;
3136 mtx_unlock(&dri2_dpy->lock);
3137 return EGL_NO_IMAGE_KHR;
3141 dri2_export_drm_image_mesa(_EGLDisplay *disp, _EGLImage *img,
3142 EGLint *name, EGLint *handle, EGLint *stride)
3144 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
3145 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3147 if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image,
3148 __DRI_IMAGE_ATTRIB_NAME, name))
3149 return _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa");
3152 dri2_dpy->image->queryImage(dri2_img->dri_image,
3153 __DRI_IMAGE_ATTRIB_HANDLE, handle);
3156 dri2_dpy->image->queryImage(dri2_img->dri_image,
3157 __DRI_IMAGE_ATTRIB_STRIDE, stride);
3159 mtx_unlock(&dri2_dpy->lock);
3165 * Checks if we can support EGL_MESA_image_dma_buf_export on this image.
3167 * The spec provides a boolean return for the driver to reject exporting for
3168 * basically any reason, but doesn't specify any particular error cases. For
3169 * now, we just fail if we don't have a DRM fourcc for the format.
3172 dri2_can_export_dma_buf_image(_EGLDisplay *disp, _EGLImage *img)
3174 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3175 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3178 if (!dri2_dpy->image->queryImage(dri2_img->dri_image,
3179 __DRI_IMAGE_ATTRIB_FOURCC, &fourcc)) {
3187 dri2_export_dma_buf_image_query_mesa(_EGLDisplay *disp, _EGLImage *img,
3188 EGLint *fourcc, EGLint *nplanes,
3189 EGLuint64KHR *modifiers)
3191 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
3192 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3195 if (!dri2_can_export_dma_buf_image(disp, img)) {
3196 mtx_unlock(&dri2_dpy->lock);
3200 dri2_dpy->image->queryImage(dri2_img->dri_image,
3201 __DRI_IMAGE_ATTRIB_NUM_PLANES, &num_planes);
3203 *nplanes = num_planes;
3206 dri2_dpy->image->queryImage(dri2_img->dri_image,
3207 __DRI_IMAGE_ATTRIB_FOURCC, fourcc);
3211 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
3214 query = dri2_dpy->image->queryImage(dri2_img->dri_image,
3215 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
3217 query &= dri2_dpy->image->queryImage(dri2_img->dri_image,
3218 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
3221 modifier = combine_u32_into_u64 (mod_hi, mod_lo);
3223 for (int i = 0; i < num_planes; i++)
3224 modifiers[i] = modifier;
3227 mtx_unlock(&dri2_dpy->lock);
3233 dri2_export_dma_buf_image_mesa(_EGLDisplay *disp, _EGLImage *img,
3234 int *fds, EGLint *strides, EGLint *offsets)
3236 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
3237 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3240 if (!dri2_can_export_dma_buf_image(disp, img))
3243 /* EGL_MESA_image_dma_buf_export spec says:
3244 * "If the number of fds is less than the number of planes, then
3245 * subsequent fd slots should contain -1."
3248 /* Query nplanes so that we know how big the given array is. */
3249 dri2_dpy->image->queryImage(dri2_img->dri_image,
3250 __DRI_IMAGE_ATTRIB_NUM_PLANES, &nplanes);
3251 memset(fds, -1, nplanes * sizeof(int));
3254 /* rework later to provide multiple fds/strides/offsets */
3256 dri2_dpy->image->queryImage(dri2_img->dri_image,
3257 __DRI_IMAGE_ATTRIB_FD, fds);
3260 dri2_dpy->image->queryImage(dri2_img->dri_image,
3261 __DRI_IMAGE_ATTRIB_STRIDE, strides);
3265 bool ret = dri2_dpy->image->queryImage(dri2_img->dri_image,
3266 __DRI_IMAGE_ATTRIB_OFFSET, &img_offset);
3268 offsets[0] = img_offset;
3273 mtx_unlock(&dri2_dpy->lock);
3281 dri2_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
3282 EGLClientBuffer buffer, const EGLint *attr_list)
3285 case EGL_GL_TEXTURE_2D_KHR:
3286 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
3287 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
3288 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
3289 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
3290 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
3291 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
3292 case EGL_GL_TEXTURE_3D_KHR:
3293 return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list);
3294 case EGL_GL_RENDERBUFFER_KHR:
3295 return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
3297 case EGL_DRM_BUFFER_MESA:
3298 return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list);
3299 case EGL_LINUX_DMA_BUF_EXT:
3300 return dri2_create_image_dma_buf(disp, ctx, buffer, attr_list);
3302 #ifdef HAVE_WAYLAND_PLATFORM
3303 case EGL_WAYLAND_BUFFER_WL:
3304 return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list);
3307 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
3308 return EGL_NO_IMAGE_KHR;
3313 dri2_destroy_image_khr(_EGLDisplay *disp, _EGLImage *image)
3315 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
3316 struct dri2_egl_image *dri2_img = dri2_egl_image(image);
3318 dri2_dpy->image->destroyImage(dri2_img->dri_image);
3321 mtx_unlock(&dri2_dpy->lock);
3326 #ifdef HAVE_WAYLAND_PLATFORM
3329 dri2_wl_reference_buffer(void *user_data, uint32_t name, int fd,
3330 struct wl_drm_buffer *buffer)
3332 _EGLDisplay *disp = user_data;
3333 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3335 int dri_components = 0;
3338 img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen,
3347 img = dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
3359 dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components);
3361 buffer->driver_format = NULL;
3362 for (int i = 0; i < ARRAY_SIZE(wl_drm_components); i++)
3363 if (wl_drm_components[i].dri_components == dri_components)
3364 buffer->driver_format = &wl_drm_components[i];
3366 if (buffer->driver_format == NULL)
3367 dri2_dpy->image->destroyImage(img);
3369 buffer->driver_buffer = img;
3373 dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer)
3375 _EGLDisplay *disp = user_data;
3376 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3378 dri2_dpy->image->destroyImage(buffer->driver_buffer);
3382 dri2_bind_wayland_display_wl(_EGLDisplay *disp, struct wl_display *wl_dpy)
3384 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
3385 const struct wayland_drm_callbacks wl_drm_callbacks = {
3386 .authenticate = (int(*)(void *, uint32_t)) dri2_dpy->vtbl->authenticate,
3387 .reference_buffer = dri2_wl_reference_buffer,
3388 .release_buffer = dri2_wl_release_buffer,
3389 .is_format_supported = dri2_wl_is_format_supported
3395 if (dri2_dpy->wl_server_drm)
3398 device_name = drmGetRenderDeviceNameFromFd(dri2_dpy->fd);
3400 device_name = strdup(dri2_dpy->device_name);
3404 if (drmGetCap(dri2_dpy->fd, DRM_CAP_PRIME, &cap) == 0 &&
3405 cap == (DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT) &&
3406 dri2_dpy->image->base.version >= 7 &&
3407 dri2_dpy->image->createImageFromFds != NULL)
3408 flags |= WAYLAND_DRM_PRIME;
3410 dri2_dpy->wl_server_drm =
3411 wayland_drm_init(wl_dpy, device_name,
3412 &wl_drm_callbacks, disp, flags);
3416 if (!dri2_dpy->wl_server_drm)
3419 #ifdef HAVE_DRM_PLATFORM
3420 /* We have to share the wl_drm instance with gbm, so gbm can convert
3421 * wl_buffers to gbm bos. */
3422 if (dri2_dpy->gbm_dri)
3423 dri2_dpy->gbm_dri->wl_drm = dri2_dpy->wl_server_drm;
3426 mtx_unlock(&dri2_dpy->lock);
3430 mtx_unlock(&dri2_dpy->lock);
3435 dri2_unbind_wayland_display_wl(_EGLDisplay *disp, struct wl_display *wl_dpy)
3437 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3439 if (!dri2_dpy->wl_server_drm)
3442 wayland_drm_uninit(dri2_dpy->wl_server_drm);
3443 dri2_dpy->wl_server_drm = NULL;
3449 dri2_query_wayland_buffer_wl(_EGLDisplay *disp, struct wl_resource *buffer_resource,
3450 EGLint attribute, EGLint *value)
3452 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3453 struct wl_drm_buffer *buffer;
3454 const struct wl_drm_components_descriptor *format;
3456 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm, buffer_resource);
3460 format = buffer->driver_format;
3461 switch (attribute) {
3462 case EGL_TEXTURE_FORMAT:
3463 *value = format->components;
3466 *value = buffer->width;
3469 *value = buffer->height;
3478 dri2_egl_ref_sync(struct dri2_egl_sync *sync)
3480 p_atomic_inc(&sync->refcount);
3484 dri2_egl_unref_sync(struct dri2_egl_display *dri2_dpy,
3485 struct dri2_egl_sync *dri2_sync)
3487 if (p_atomic_dec_zero(&dri2_sync->refcount)) {
3488 switch (dri2_sync->base.Type) {
3489 case EGL_SYNC_REUSABLE_KHR:
3490 cnd_destroy(&dri2_sync->cond);
3492 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3493 if (dri2_sync->base.SyncFd != EGL_NO_NATIVE_FENCE_FD_ANDROID)
3494 close(dri2_sync->base.SyncFd);
3500 if (dri2_sync->fence)
3501 dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, dri2_sync->fence);
3508 dri2_create_sync(_EGLDisplay *disp, EGLenum type, const EGLAttrib *attrib_list)
3510 _EGLContext *ctx = _eglGetCurrentContext();
3511 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
3512 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3513 struct dri2_egl_sync *dri2_sync;
3515 pthread_condattr_t attr;
3517 dri2_sync = calloc(1, sizeof(struct dri2_egl_sync));
3519 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3523 if (!_eglInitSync(&dri2_sync->base, disp, type, attrib_list)) {
3528 case EGL_SYNC_FENCE_KHR:
3529 dri2_sync->fence = dri2_dpy->fence->create_fence(dri2_ctx->dri_context);
3530 if (!dri2_sync->fence) {
3531 /* Why did it fail? DRI doesn't return an error code, so we emit
3532 * a generic EGL error that doesn't communicate user error.
3534 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3539 case EGL_SYNC_CL_EVENT_KHR:
3540 dri2_sync->fence = dri2_dpy->fence->get_fence_from_cl_event(
3541 dri2_dpy->dri_screen,
3542 dri2_sync->base.CLEvent);
3543 /* this can only happen if the cl_event passed in is invalid. */
3544 if (!dri2_sync->fence) {
3545 _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3549 /* the initial status must be "signaled" if the cl_event is signaled */
3550 if (dri2_dpy->fence->client_wait_sync(dri2_ctx->dri_context,
3551 dri2_sync->fence, 0, 0))
3552 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3555 case EGL_SYNC_REUSABLE_KHR:
3556 /* intialize attr */
3557 ret = pthread_condattr_init(&attr);
3560 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3564 /* change clock attribute to CLOCK_MONOTONIC */
3565 ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
3568 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3572 ret = pthread_cond_init(&dri2_sync->cond, &attr);
3575 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3579 /* initial status of reusable sync must be "unsignaled" */
3580 dri2_sync->base.SyncStatus = EGL_UNSIGNALED_KHR;
3583 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3584 if (dri2_dpy->fence->create_fence_fd) {
3585 dri2_sync->fence = dri2_dpy->fence->create_fence_fd(
3586 dri2_ctx->dri_context,
3587 dri2_sync->base.SyncFd);
3589 if (!dri2_sync->fence) {
3590 _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3596 p_atomic_set(&dri2_sync->refcount, 1);
3597 mtx_unlock(&dri2_dpy->lock);
3599 return &dri2_sync->base;
3603 mtx_unlock(&dri2_dpy->lock);
3608 dri2_destroy_sync(_EGLDisplay *disp, _EGLSync *sync)
3610 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
3611 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3612 EGLint ret = EGL_TRUE;
3615 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
3616 * then unlock all threads possibly blocked by the reusable sync before
3619 if (dri2_sync->base.Type == EGL_SYNC_REUSABLE_KHR &&
3620 dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3621 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3622 /* unblock all threads currently blocked by sync */
3623 err = cnd_broadcast(&dri2_sync->cond);
3626 _eglError(EGL_BAD_ACCESS, "eglDestroySyncKHR");
3631 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
3633 mtx_unlock(&dri2_dpy->lock);
3639 dri2_dup_native_fence_fd(_EGLDisplay *disp, _EGLSync *sync)
3641 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
3642 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3644 assert(sync->Type == EGL_SYNC_NATIVE_FENCE_ANDROID);
3646 if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3647 /* try to retrieve the actual native fence fd.. if rendering is
3648 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
3650 sync->SyncFd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
3654 mtx_unlock(&dri2_dpy->lock);
3656 if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3657 /* if native fence fd still not created, return an error: */
3658 _eglError(EGL_BAD_PARAMETER, "eglDupNativeFenceFDANDROID");
3659 return EGL_NO_NATIVE_FENCE_FD_ANDROID;
3662 assert(sync_valid_fd(sync->SyncFd));
3664 return os_dupfd_cloexec(sync->SyncFd);
3668 dri2_set_blob_cache_funcs(_EGLDisplay *disp,
3669 EGLSetBlobFuncANDROID set,
3670 EGLGetBlobFuncANDROID get)
3672 struct dri2_egl_display *dri2_dpy = dri2_egl_display_lock(disp);
3673 dri2_dpy->blob->set_cache_funcs(dri2_dpy->dri_screen, set, get);
3674 mtx_unlock(&dri2_dpy->lock);
3678 dri2_client_wait_sync(_EGLDisplay *disp, _EGLSync *sync,
3679 EGLint flags, EGLTime timeout)
3681 _EGLContext *ctx = _eglGetCurrentContext();
3682 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3683 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3684 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3685 unsigned wait_flags = 0;
3687 EGLint ret = EGL_CONDITION_SATISFIED_KHR;
3689 /* The EGL_KHR_fence_sync spec states:
3691 * "If no context is current for the bound API,
3692 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
3694 if (dri2_ctx && flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)
3695 wait_flags |= __DRI2_FENCE_FLAG_FLUSH_COMMANDS;
3697 /* the sync object should take a reference while waiting */
3698 dri2_egl_ref_sync(dri2_sync);
3700 switch (sync->Type) {
3701 case EGL_SYNC_FENCE_KHR:
3702 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3703 case EGL_SYNC_CL_EVENT_KHR:
3704 if (dri2_dpy->fence->client_wait_sync(dri2_ctx ? dri2_ctx->dri_context : NULL,
3705 dri2_sync->fence, wait_flags,
3707 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3709 ret = EGL_TIMEOUT_EXPIRED_KHR;
3712 case EGL_SYNC_REUSABLE_KHR:
3713 if (dri2_ctx && dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR &&
3714 (flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)) {
3715 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
3719 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
3720 if (timeout == EGL_FOREVER_KHR) {
3721 mtx_lock(&dri2_sync->mutex);
3722 cnd_wait(&dri2_sync->cond, &dri2_sync->mutex);
3723 mtx_unlock(&dri2_sync->mutex);
3725 /* if reusable sync has not been yet signaled */
3726 if (dri2_sync->base.SyncStatus != EGL_SIGNALED_KHR) {
3727 /* timespecs for cnd_timedwait */
3728 struct timespec current;
3729 struct timespec expire;
3731 /* We override the clock to monotonic when creating the condition
3733 clock_gettime(CLOCK_MONOTONIC, ¤t);
3735 /* calculating when to expire */
3736 expire.tv_nsec = timeout % 1000000000L;
3737 expire.tv_sec = timeout / 1000000000L;
3739 expire.tv_nsec += current.tv_nsec;
3740 expire.tv_sec += current.tv_sec;
3742 /* expire.nsec now is a number between 0 and 1999999998 */
3743 if (expire.tv_nsec > 999999999L) {
3745 expire.tv_nsec -= 1000000000L;
3748 mtx_lock(&dri2_sync->mutex);
3749 ret = cnd_timedwait(&dri2_sync->cond, &dri2_sync->mutex, &expire);
3750 mtx_unlock(&dri2_sync->mutex);
3752 if (ret == thrd_timedout) {
3753 if (dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3754 ret = EGL_TIMEOUT_EXPIRED_KHR;
3756 _eglError(EGL_BAD_ACCESS, "eglClientWaitSyncKHR");
3765 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
3771 dri2_signal_sync(_EGLDisplay *disp, _EGLSync *sync, EGLenum mode)
3773 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3776 if (sync->Type != EGL_SYNC_REUSABLE_KHR)
3777 return _eglError(EGL_BAD_MATCH, "eglSignalSyncKHR");
3779 if (mode != EGL_SIGNALED_KHR && mode != EGL_UNSIGNALED_KHR)
3780 return _eglError(EGL_BAD_ATTRIBUTE, "eglSignalSyncKHR");
3782 dri2_sync->base.SyncStatus = mode;
3784 if (mode == EGL_SIGNALED_KHR) {
3785 ret = cnd_broadcast(&dri2_sync->cond);
3787 /* fail to broadcast */
3789 return _eglError(EGL_BAD_ACCESS, "eglSignalSyncKHR");
3796 dri2_server_wait_sync(_EGLDisplay *disp, _EGLSync *sync)
3798 _EGLContext *ctx = _eglGetCurrentContext();
3799 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3800 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3801 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3803 dri2_dpy->fence->server_wait_sync(dri2_ctx->dri_context,
3804 dri2_sync->fence, 0);
3809 dri2_interop_query_device_info(_EGLDisplay *disp, _EGLContext *ctx,
3810 struct mesa_glinterop_device_info *out)
3812 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3813 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3815 if (!dri2_dpy->interop)
3816 return MESA_GLINTEROP_UNSUPPORTED;
3818 return dri2_dpy->interop->query_device_info(dri2_ctx->dri_context, out);
3822 dri2_interop_export_object(_EGLDisplay *disp, _EGLContext *ctx,
3823 struct mesa_glinterop_export_in *in,
3824 struct mesa_glinterop_export_out *out)
3826 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3827 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3829 if (!dri2_dpy->interop)
3830 return MESA_GLINTEROP_UNSUPPORTED;
3832 return dri2_dpy->interop->export_object(dri2_ctx->dri_context, in, out);
3836 dri2_interop_flush_objects(_EGLDisplay *disp, _EGLContext *ctx,
3837 unsigned count, struct mesa_glinterop_export_in *objects,
3840 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3841 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3843 if (!dri2_dpy->interop || dri2_dpy->interop->base.version < 2)
3844 return MESA_GLINTEROP_UNSUPPORTED;
3846 return dri2_dpy->interop->flush_objects(dri2_ctx->dri_context, count, objects, sync);
3849 const _EGLDriver _eglDriver = {
3850 .Initialize = dri2_initialize,
3851 .Terminate = dri2_terminate,
3852 .CreateContext = dri2_create_context,
3853 .DestroyContext = dri2_destroy_context,
3854 .MakeCurrent = dri2_make_current,
3855 .CreateWindowSurface = dri2_create_window_surface,
3856 .CreatePixmapSurface = dri2_create_pixmap_surface,
3857 .CreatePbufferSurface = dri2_create_pbuffer_surface,
3858 .DestroySurface = dri2_destroy_surface,
3859 .GetProcAddress = dri2_get_proc_address,
3860 .WaitClient = dri2_wait_client,
3861 .WaitNative = dri2_wait_native,
3862 .BindTexImage = dri2_bind_tex_image,
3863 .ReleaseTexImage = dri2_release_tex_image,
3864 .SwapInterval = dri2_swap_interval,
3865 .SwapBuffers = dri2_swap_buffers,
3866 .SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage,
3867 .SwapBuffersRegionNOK = dri2_swap_buffers_region,
3868 .SetDamageRegion = dri2_set_damage_region,
3869 .PostSubBufferNV = dri2_post_sub_buffer,
3870 .CopyBuffers = dri2_copy_buffers,
3871 .QueryBufferAge = dri2_query_buffer_age,
3872 .CreateImageKHR = dri2_create_image,
3873 .DestroyImageKHR = dri2_destroy_image_khr,
3874 .CreateWaylandBufferFromImageWL = dri2_create_wayland_buffer_from_image,
3875 .QuerySurface = dri2_query_surface,
3876 .QueryDriverName = dri2_query_driver_name,
3877 .QueryDriverConfig = dri2_query_driver_config,
3879 .CreateDRMImageMESA = dri2_create_drm_image_mesa,
3880 .ExportDRMImageMESA = dri2_export_drm_image_mesa,
3881 .ExportDMABUFImageQueryMESA = dri2_export_dma_buf_image_query_mesa,
3882 .ExportDMABUFImageMESA = dri2_export_dma_buf_image_mesa,
3883 .QueryDmaBufFormatsEXT = dri2_query_dma_buf_formats,
3884 .QueryDmaBufModifiersEXT = dri2_query_dma_buf_modifiers,
3886 #ifdef HAVE_WAYLAND_PLATFORM
3887 .BindWaylandDisplayWL = dri2_bind_wayland_display_wl,
3888 .UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl,
3889 .QueryWaylandBufferWL = dri2_query_wayland_buffer_wl,
3891 .GetSyncValuesCHROMIUM = dri2_get_sync_values_chromium,
3892 .GetMscRateANGLE = dri2_get_msc_rate_angle,
3893 .CreateSyncKHR = dri2_create_sync,
3894 .ClientWaitSyncKHR = dri2_client_wait_sync,
3895 .SignalSyncKHR = dri2_signal_sync,
3896 .WaitSyncKHR = dri2_server_wait_sync,
3897 .DestroySyncKHR = dri2_destroy_sync,
3898 .GLInteropQueryDeviceInfo = dri2_interop_query_device_info,
3899 .GLInteropExportObject = dri2_interop_export_object,
3900 .GLInteropFlushObjects = dri2_interop_flush_objects,
3901 .DupNativeFenceFDANDROID = dri2_dup_native_fence_fd,
3902 .SetBlobCacheFuncsANDROID = dri2_set_blob_cache_funcs,