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_vector.h"
69 #include "mapi/glapi/glapi.h"
70 #include "util/bitscan.h"
71 #include "util/u_math.h"
73 #define NUM_ATTRIBS 12
75 static const struct dri2_pbuffer_visual {
76 const char *format_name;
77 unsigned int dri_image_format;
79 unsigned int rgba_sizes[4];
80 } dri2_pbuffer_visuals[] = {
83 __DRI_IMAGE_FORMAT_ABGR16161616F,
89 __DRI_IMAGE_FORMAT_XBGR16161616F,
95 __DRI_IMAGE_FORMAT_ARGB2101010,
101 __DRI_IMAGE_FORMAT_XRGB2101010,
107 __DRI_IMAGE_FORMAT_ARGB8888,
113 __DRI_IMAGE_FORMAT_XRGB8888,
119 __DRI_IMAGE_FORMAT_RGB565,
126 dri_set_background_context(void *loaderPrivate)
128 _EGLContext *ctx = _eglGetCurrentContext();
129 _EGLThreadInfo *t = _eglGetCurrentThread();
131 _eglBindContextToThread(ctx, t);
137 static void (*glFlush)(void);
138 static mtx_t glFlushMutex = _MTX_INITIALIZER_NP;
140 mtx_lock(&glFlushMutex);
142 glFlush = _glapi_get_proc_address("glFlush");
143 mtx_unlock(&glFlushMutex);
145 /* if glFlush is not available things are horribly broken */
147 _eglLog(_EGL_WARNING, "DRI2: failed to find glFlush entry point");
155 dri_is_thread_safe(void *loaderPrivate)
157 struct dri2_egl_surface *dri2_surf = loaderPrivate;
158 UNUSED _EGLDisplay *display = dri2_surf->base.Resource.Display;
160 #ifdef HAVE_X11_PLATFORM
161 Display *xdpy = (Display*)display->PlatformDisplay;
163 /* Check Xlib is running in thread safe mode when running on EGL/X11-xlib
166 * 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
167 * It wll be NULL if XInitThreads wasn't called.
169 if (display->Platform == _EGL_PLATFORM_X11 && xdpy && !xdpy->lock_fns)
176 const __DRIbackgroundCallableExtension background_callable_extension = {
177 .base = { __DRI_BACKGROUND_CALLABLE, 2 },
179 .setBackgroundContext = dri_set_background_context,
180 .isThreadSafe = dri_is_thread_safe,
183 const __DRIuseInvalidateExtension use_invalidate = {
184 .base = { __DRI_USE_INVALIDATE, 1 }
188 dri2_get_pbuffer_drawable_info(__DRIdrawable * draw,
189 int *x, int *y, int *w, int *h,
192 struct dri2_egl_surface *dri2_surf = loaderPrivate;
195 *w = dri2_surf->base.Width;
196 *h = dri2_surf->base.Height;
200 dri2_get_bytes_per_pixel(struct dri2_egl_surface *dri2_surf)
202 const int depth = dri2_surf->base.Config->BufferSize;
203 return depth ? util_next_power_of_two(depth / 8) : 0;
207 dri2_put_image(__DRIdrawable * draw, int op,
208 int x, int y, int w, int h,
209 char *data, void *loaderPrivate)
211 struct dri2_egl_surface *dri2_surf = loaderPrivate;
212 const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
213 const int width = dri2_surf->base.Width;
214 const int height = dri2_surf->base.Height;
215 const int dst_stride = width*bpp;
216 const int src_stride = w*bpp;
217 const int x_offset = x*bpp;
218 int copy_width = src_stride;
220 if (!dri2_surf->swrast_device_buffer)
221 dri2_surf->swrast_device_buffer = malloc(height*dst_stride);
223 if (dri2_surf->swrast_device_buffer) {
224 const char *src = data;
225 char *dst = dri2_surf->swrast_device_buffer;
230 /* Drivers are allowed to submit OOB PutImage requests, so clip here. */
231 if (copy_width > dst_stride - x_offset)
232 copy_width = dst_stride - x_offset;
237 memcpy(dst, src, copy_width);
245 dri2_get_image(__DRIdrawable * read,
246 int x, int y, int w, int h,
247 char *data, void *loaderPrivate)
249 struct dri2_egl_surface *dri2_surf = loaderPrivate;
250 const int bpp = dri2_get_bytes_per_pixel(dri2_surf);
251 const int width = dri2_surf->base.Width;
252 const int height = dri2_surf->base.Height;
253 const int src_stride = width*bpp;
254 const int dst_stride = w*bpp;
255 const int x_offset = x*bpp;
256 int copy_width = dst_stride;
257 const char *src = dri2_surf->swrast_device_buffer;
261 memset(data, 0, copy_width * h);
268 /* Drivers are allowed to submit OOB GetImage requests, so clip here. */
269 if (copy_width > src_stride - x_offset)
270 copy_width = src_stride - x_offset;
275 memcpy(dst, src, copy_width);
282 /* HACK: technically we should have swrast_null, instead of these.
284 const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension = {
285 .base = { __DRI_SWRAST_LOADER, 1 },
286 .getDrawableInfo = dri2_get_pbuffer_drawable_info,
287 .putImage = dri2_put_image,
288 .getImage = dri2_get_image,
291 static const EGLint dri2_to_egl_attribute_map[__DRI_ATTRIB_MAX] = {
292 [__DRI_ATTRIB_BUFFER_SIZE ] = EGL_BUFFER_SIZE,
293 [__DRI_ATTRIB_LEVEL] = EGL_LEVEL,
294 [__DRI_ATTRIB_LUMINANCE_SIZE] = EGL_LUMINANCE_SIZE,
295 [__DRI_ATTRIB_DEPTH_SIZE] = EGL_DEPTH_SIZE,
296 [__DRI_ATTRIB_STENCIL_SIZE] = EGL_STENCIL_SIZE,
297 [__DRI_ATTRIB_SAMPLE_BUFFERS] = EGL_SAMPLE_BUFFERS,
298 [__DRI_ATTRIB_SAMPLES] = EGL_SAMPLES,
299 [__DRI_ATTRIB_MAX_PBUFFER_WIDTH] = EGL_MAX_PBUFFER_WIDTH,
300 [__DRI_ATTRIB_MAX_PBUFFER_HEIGHT] = EGL_MAX_PBUFFER_HEIGHT,
301 [__DRI_ATTRIB_MAX_PBUFFER_PIXELS] = EGL_MAX_PBUFFER_PIXELS,
302 [__DRI_ATTRIB_MAX_SWAP_INTERVAL] = EGL_MAX_SWAP_INTERVAL,
303 [__DRI_ATTRIB_MIN_SWAP_INTERVAL] = EGL_MIN_SWAP_INTERVAL,
304 [__DRI_ATTRIB_YINVERTED] = EGL_Y_INVERTED_NOK,
308 dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,
311 const bool double_buffer = surface_type == EGL_WINDOW_BIT;
312 const bool srgb = colorspace == EGL_GL_COLORSPACE_SRGB_KHR;
314 return conf->dri_config[double_buffer][srgb];
318 dri2_match_config(const _EGLConfig *conf, const _EGLConfig *criteria)
320 if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0)
323 if (!_eglMatchConfig(conf, criteria))
330 dri2_get_shifts_and_sizes(const __DRIcoreExtension *core,
331 const __DRIconfig *config, int *shifts,
336 if (core->getConfigAttrib(config, __DRI_ATTRIB_RED_SHIFT, (unsigned int *)&shifts[0])) {
337 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SHIFT, (unsigned int *)&shifts[1]);
338 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SHIFT, (unsigned int *)&shifts[2]);
339 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SHIFT, (unsigned int *)&shifts[3]);
341 /* Driver isn't exposing shifts, so convert masks to shifts */
342 core->getConfigAttrib(config, __DRI_ATTRIB_RED_MASK, &mask);
343 shifts[0] = ffs(mask) - 1;
344 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_MASK, &mask);
345 shifts[1] = ffs(mask) - 1;
346 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_MASK, &mask);
347 shifts[2] = ffs(mask) - 1;
348 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_MASK, &mask);
349 shifts[3] = ffs(mask) - 1;
352 core->getConfigAttrib(config, __DRI_ATTRIB_RED_SIZE, &sizes[0]);
353 core->getConfigAttrib(config, __DRI_ATTRIB_GREEN_SIZE, &sizes[1]);
354 core->getConfigAttrib(config, __DRI_ATTRIB_BLUE_SIZE, &sizes[2]);
355 core->getConfigAttrib(config, __DRI_ATTRIB_ALPHA_SIZE, &sizes[3]);
359 dri2_get_render_type_float(const __DRIcoreExtension *core,
360 const __DRIconfig *config,
363 unsigned int render_type;
365 core->getConfigAttrib(config, __DRI_ATTRIB_RENDER_TYPE, &render_type);
366 *is_float = (render_type & __DRI_ATTRIB_FLOAT_BIT) ? true : false;
370 dri2_image_format_for_pbuffer_config(struct dri2_egl_display *dri2_dpy,
371 const __DRIconfig *config)
374 unsigned int sizes[4];
376 dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
378 for (unsigned i = 0; i < ARRAY_SIZE(dri2_pbuffer_visuals); ++i) {
379 const struct dri2_pbuffer_visual *visual = &dri2_pbuffer_visuals[i];
381 if (shifts[0] == visual->rgba_shifts[0] &&
382 shifts[1] == visual->rgba_shifts[1] &&
383 shifts[2] == visual->rgba_shifts[2] &&
384 shifts[3] == visual->rgba_shifts[3] &&
385 sizes[0] == visual->rgba_sizes[0] &&
386 sizes[1] == visual->rgba_sizes[1] &&
387 sizes[2] == visual->rgba_sizes[2] &&
388 sizes[3] == visual->rgba_sizes[3]) {
389 return visual->dri_image_format;
393 return __DRI_IMAGE_FORMAT_NONE;
396 struct dri2_egl_config *
397 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
398 EGLint surface_type, const EGLint *attr_list,
399 const int *rgba_shifts, const unsigned int *rgba_sizes)
401 struct dri2_egl_config *conf;
402 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
404 unsigned int attrib, value, double_buffer;
406 EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
407 int dri_shifts[4] = { -1, -1, -1, -1 };
408 unsigned int dri_sizes[4] = { 0, 0, 0, 0 };
409 _EGLConfig *matching_config;
410 EGLint num_configs = 0;
413 _eglInitConfig(&base, disp, id);
416 bind_to_texture_rgb = 0;
417 bind_to_texture_rgba = 0;
419 for (int i = 0; i < __DRI_ATTRIB_MAX; ++i) {
420 if (!dri2_dpy->core->indexConfigAttrib(dri_config, i, &attrib, &value))
424 case __DRI_ATTRIB_RENDER_TYPE:
425 if (value & __DRI_ATTRIB_FLOAT_BIT)
426 base.ComponentType = EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT;
427 if (value & __DRI_ATTRIB_RGBA_BIT)
428 value = EGL_RGB_BUFFER;
429 else if (value & __DRI_ATTRIB_LUMINANCE_BIT)
430 value = EGL_LUMINANCE_BUFFER;
433 base.ColorBufferType = value;
436 case __DRI_ATTRIB_CONFIG_CAVEAT:
437 if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
438 value = EGL_NON_CONFORMANT_CONFIG;
439 else if (value & __DRI_ATTRIB_SLOW_BIT)
440 value = EGL_SLOW_CONFIG;
443 base.ConfigCaveat = value;
446 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
447 bind_to_texture_rgb = value;
450 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
451 bind_to_texture_rgba = value;
454 case __DRI_ATTRIB_DOUBLE_BUFFER:
455 double_buffer = value;
458 case __DRI_ATTRIB_RED_SIZE:
459 dri_sizes[0] = value;
460 base.RedSize = value;
463 case __DRI_ATTRIB_RED_MASK:
464 dri_shifts[0] = ffs(value) - 1;
467 case __DRI_ATTRIB_RED_SHIFT:
468 dri_shifts[0] = value;
471 case __DRI_ATTRIB_GREEN_SIZE:
472 dri_sizes[1] = value;
473 base.GreenSize = value;
476 case __DRI_ATTRIB_GREEN_MASK:
477 dri_shifts[1] = ffs(value) - 1;
480 case __DRI_ATTRIB_GREEN_SHIFT:
481 dri_shifts[1] = value;
484 case __DRI_ATTRIB_BLUE_SIZE:
485 dri_sizes[2] = value;
486 base.BlueSize = value;
489 case __DRI_ATTRIB_BLUE_MASK:
490 dri_shifts[2] = ffs(value) - 1;
493 case __DRI_ATTRIB_BLUE_SHIFT:
494 dri_shifts[2] = value;
497 case __DRI_ATTRIB_ALPHA_SIZE:
498 dri_sizes[3] = value;
499 base.AlphaSize = value;
502 case __DRI_ATTRIB_ALPHA_MASK:
503 dri_shifts[3] = ffs(value) - 1;
506 case __DRI_ATTRIB_ALPHA_SHIFT:
507 dri_shifts[3] = value;
510 case __DRI_ATTRIB_ACCUM_RED_SIZE:
511 case __DRI_ATTRIB_ACCUM_GREEN_SIZE:
512 case __DRI_ATTRIB_ACCUM_BLUE_SIZE:
513 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE:
514 /* Don't expose visuals with the accumulation buffer. */
519 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE:
521 if (!disp->Extensions.KHR_gl_colorspace && srgb)
525 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH:
526 base.MaxPbufferWidth = _EGL_MAX_PBUFFER_WIDTH;
528 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT:
529 base.MaxPbufferHeight = _EGL_MAX_PBUFFER_HEIGHT;
531 case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER:
532 if (disp->Extensions.KHR_mutable_render_buffer)
533 surface_type |= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR;
536 key = dri2_to_egl_attribute_map[attrib];
538 _eglSetConfigKey(&base, key, value);
544 for (int i = 0; attr_list[i] != EGL_NONE; i += 2)
545 _eglSetConfigKey(&base, attr_list[i], attr_list[i+1]);
547 if (rgba_shifts && memcmp(rgba_shifts, dri_shifts, sizeof(dri_shifts)))
550 if (rgba_sizes && memcmp(rgba_sizes, dri_sizes, sizeof(dri_sizes)))
553 base.NativeRenderable = EGL_TRUE;
555 base.SurfaceType = surface_type;
556 if (surface_type & (EGL_PBUFFER_BIT |
557 (disp->Extensions.NOK_texture_from_pixmap ? EGL_PIXMAP_BIT : 0))) {
558 base.BindToTextureRGB = bind_to_texture_rgb;
559 if (base.AlphaSize > 0)
560 base.BindToTextureRGBA = bind_to_texture_rgba;
564 surface_type &= ~EGL_PIXMAP_BIT;
570 base.RenderableType = disp->ClientAPIs;
571 base.Conformant = disp->ClientAPIs;
573 base.MinSwapInterval = dri2_dpy->min_swap_interval;
574 base.MaxSwapInterval = dri2_dpy->max_swap_interval;
576 if (!_eglValidateConfig(&base, EGL_FALSE)) {
577 _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);
581 config_id = base.ConfigID;
582 base.ConfigID = EGL_DONT_CARE;
583 base.SurfaceType = EGL_DONT_CARE;
584 num_configs = _eglFilterArray(disp->Configs, (void **) &matching_config, 1,
585 (_EGLArrayForEach) dri2_match_config, &base);
587 if (num_configs == 1) {
588 conf = (struct dri2_egl_config *) matching_config;
590 if (!conf->dri_config[double_buffer][srgb])
591 conf->dri_config[double_buffer][srgb] = dri_config;
593 /* a similar config type is already added (unlikely) => discard */
596 else if (num_configs == 0) {
597 conf = calloc(1, sizeof *conf);
601 conf->dri_config[double_buffer][srgb] = dri_config;
603 memcpy(&conf->base, &base, sizeof base);
604 conf->base.SurfaceType = 0;
605 conf->base.ConfigID = config_id;
607 _eglLinkConfig(&conf->base);
610 unreachable("duplicates should not be possible");
614 conf->base.SurfaceType |= surface_type;
620 dri2_add_pbuffer_configs_for_visuals(_EGLDisplay *disp)
622 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
623 unsigned int format_count[ARRAY_SIZE(dri2_pbuffer_visuals)] = { 0 };
624 unsigned int config_count = 0;
626 for (unsigned i = 0; dri2_dpy->driver_configs[i] != NULL; i++) {
627 for (unsigned j = 0; j < ARRAY_SIZE(dri2_pbuffer_visuals); j++) {
628 struct dri2_egl_config *dri2_conf;
630 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
631 config_count + 1, EGL_PBUFFER_BIT, NULL,
632 dri2_pbuffer_visuals[j].rgba_shifts, dri2_pbuffer_visuals[j].rgba_sizes);
635 if (dri2_conf->base.ConfigID == config_count + 1)
642 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
643 if (!format_count[i]) {
644 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
645 dri2_pbuffer_visuals[i].format_name);
649 return (config_count != 0);
653 dri2_validate_egl_image(void *image, void *data)
655 _EGLDisplay *disp = data;
658 mtx_lock(&disp->Mutex);
659 img = _eglLookupImage(image, disp);
660 mtx_unlock(&disp->Mutex);
663 _eglError(EGL_BAD_PARAMETER, "dri2_validate_egl_image");
671 dri2_lookup_egl_image_validated(void *image, void *data)
673 struct dri2_egl_image *dri2_img;
677 dri2_img = dri2_egl_image(image);
679 return dri2_img->dri_image;
683 dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
687 if (!dri2_validate_egl_image(image, data))
690 return dri2_lookup_egl_image_validated(image, data);
693 const __DRIimageLookupExtension image_lookup_extension = {
694 .base = { __DRI_IMAGE_LOOKUP, 2 },
696 .lookupEGLImage = dri2_lookup_egl_image,
697 .validateEGLImage = dri2_validate_egl_image,
698 .lookupEGLImageValidated = dri2_lookup_egl_image_validated,
701 struct dri2_extension_match {
707 static const struct dri2_extension_match dri3_driver_extensions[] = {
708 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
709 { __DRI_IMAGE_DRIVER, 1, offsetof(struct dri2_egl_display, image_driver) },
713 static const struct dri2_extension_match dri2_driver_extensions[] = {
714 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
715 { __DRI_DRI2, 2, offsetof(struct dri2_egl_display, dri2) },
719 static const struct dri2_extension_match dri2_core_extensions[] = {
720 { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) },
721 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
722 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
726 static const struct dri2_extension_match swrast_driver_extensions[] = {
727 { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
728 { __DRI_SWRAST, 2, offsetof(struct dri2_egl_display, swrast) },
732 static const struct dri2_extension_match swrast_core_extensions[] = {
733 { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
737 static const struct dri2_extension_match optional_driver_extensions[] = {
738 { __DRI_CONFIG_OPTIONS, 1, offsetof(struct dri2_egl_display, configOptions) },
742 static const struct dri2_extension_match optional_core_extensions[] = {
743 { __DRI2_ROBUSTNESS, 1, offsetof(struct dri2_egl_display, robustness) },
744 { __DRI2_CONFIG_QUERY, 1, offsetof(struct dri2_egl_display, config) },
745 { __DRI2_FENCE, 1, offsetof(struct dri2_egl_display, fence) },
746 { __DRI2_BUFFER_DAMAGE, 1, offsetof(struct dri2_egl_display, buffer_damage) },
747 { __DRI2_RENDERER_QUERY, 1, offsetof(struct dri2_egl_display, rendererQuery) },
748 { __DRI2_INTEROP, 1, offsetof(struct dri2_egl_display, interop) },
749 { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
750 { __DRI2_FLUSH_CONTROL, 1, offsetof(struct dri2_egl_display, flush_control) },
751 { __DRI2_BLOB, 1, offsetof(struct dri2_egl_display, blob) },
752 { __DRI_MUTABLE_RENDER_BUFFER_DRIVER, 1, offsetof(struct dri2_egl_display, mutable_render_buffer) },
757 dri2_bind_extensions(struct dri2_egl_display *dri2_dpy,
758 const struct dri2_extension_match *matches,
759 const __DRIextension **extensions,
765 for (int i = 0; extensions[i]; i++) {
766 _eglLog(_EGL_DEBUG, "found extension `%s'", extensions[i]->name);
767 for (int j = 0; matches[j].name; j++) {
768 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
769 extensions[i]->version >= matches[j].version) {
770 field = ((char *) dri2_dpy + matches[j].offset);
771 *(const __DRIextension **) field = extensions[i];
772 _eglLog(_EGL_INFO, "found extension %s version %d",
773 extensions[i]->name, extensions[i]->version);
779 for (int j = 0; matches[j].name; j++) {
780 field = ((char *) dri2_dpy + matches[j].offset);
781 if (*(const __DRIextension **) field == NULL) {
783 _eglLog(_EGL_DEBUG, "did not find optional extension %s version %d",
784 matches[j].name, matches[j].version);
786 _eglLog(_EGL_WARNING, "did not find extension %s version %d",
787 matches[j].name, matches[j].version);
796 static const __DRIextension **
797 dri2_open_driver(_EGLDisplay *disp)
799 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
800 static const char *search_path_vars[] = {
801 "LIBGL_DRIVERS_PATH",
805 return loader_open_driver(dri2_dpy->driver_name,
811 dri2_load_driver_common(_EGLDisplay *disp,
812 const struct dri2_extension_match *driver_extensions)
814 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
815 const __DRIextension **extensions;
817 extensions = dri2_open_driver(disp);
821 if (!dri2_bind_extensions(dri2_dpy, driver_extensions, extensions, false)) {
822 dlclose(dri2_dpy->driver);
823 dri2_dpy->driver = NULL;
826 dri2_dpy->driver_extensions = extensions;
828 dri2_bind_extensions(dri2_dpy, optional_driver_extensions, extensions, true);
834 dri2_load_driver(_EGLDisplay *disp)
836 return dri2_load_driver_common(disp, dri2_driver_extensions);
840 dri2_load_driver_dri3(_EGLDisplay *disp)
842 return dri2_load_driver_common(disp, dri3_driver_extensions);
846 dri2_load_driver_swrast(_EGLDisplay *disp)
848 return dri2_load_driver_common(disp, swrast_driver_extensions);
852 dri2_renderer_query_integer(struct dri2_egl_display *dri2_dpy, int param)
854 const __DRI2rendererQueryExtension *rendererQuery = dri2_dpy->rendererQuery;
855 unsigned int value = 0;
857 if (!rendererQuery ||
858 rendererQuery->queryInteger(dri2_dpy->dri_screen, param, &value) == -1)
865 dri2_query_driver_name(_EGLDisplay *disp)
867 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
868 return dri2_dpy->driver_name;
872 dri2_query_driver_config(_EGLDisplay *disp)
874 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
875 const __DRIconfigOptionsExtension *ext = dri2_dpy->configOptions;
877 if (ext->base.version >= 2)
878 return ext->getXml(dri2_dpy->driver_name);
880 return strdup(ext->xml);
885 dri2_setup_screen(_EGLDisplay *disp)
887 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
888 unsigned int api_mask;
891 * EGL 1.5 specification defines the default value to 1. Moreover,
892 * eglSwapInterval() is required to clamp requested value to the supported
893 * range. Since the default value is implicitly assumed to be supported,
894 * use it as both minimum and maximum for the platforms that do not allow
895 * changing the interval. Platforms, which allow it (e.g. x11, wayland)
896 * override these values already.
898 dri2_dpy->min_swap_interval = 1;
899 dri2_dpy->max_swap_interval = 1;
900 dri2_dpy->default_swap_interval = 1;
902 if (dri2_dpy->image_driver) {
903 api_mask = dri2_dpy->image_driver->getAPIMask(dri2_dpy->dri_screen);
904 } else if (dri2_dpy->dri2) {
905 api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);
907 assert(dri2_dpy->swrast);
908 api_mask = 1 << __DRI_API_OPENGL |
909 1 << __DRI_API_GLES |
910 1 << __DRI_API_GLES2 |
911 1 << __DRI_API_GLES3;
914 disp->ClientAPIs = 0;
915 if ((api_mask & (1 <<__DRI_API_OPENGL)) && _eglIsApiValid(EGL_OPENGL_API))
916 disp->ClientAPIs |= EGL_OPENGL_BIT;
917 if ((api_mask & (1 << __DRI_API_GLES)) && _eglIsApiValid(EGL_OPENGL_ES_API))
918 disp->ClientAPIs |= EGL_OPENGL_ES_BIT;
919 if ((api_mask & (1 << __DRI_API_GLES2)) && _eglIsApiValid(EGL_OPENGL_ES_API))
920 disp->ClientAPIs |= EGL_OPENGL_ES2_BIT;
921 if ((api_mask & (1 << __DRI_API_GLES3)) && _eglIsApiValid(EGL_OPENGL_ES_API))
922 disp->ClientAPIs |= EGL_OPENGL_ES3_BIT_KHR;
924 assert(dri2_dpy->image_driver || dri2_dpy->dri2 || dri2_dpy->swrast);
925 disp->Extensions.KHR_no_config_context = EGL_TRUE;
926 disp->Extensions.KHR_surfaceless_context = EGL_TRUE;
928 if (dri2_dpy->configOptions) {
929 disp->Extensions.MESA_query_driver = EGL_TRUE;
932 /* Report back to EGL the bitmask of priorities supported */
933 disp->Extensions.IMG_context_priority =
934 dri2_renderer_query_integer(dri2_dpy,
935 __DRI2_RENDERER_HAS_CONTEXT_PRIORITY);
937 disp->Extensions.EXT_pixel_format_float = EGL_TRUE;
939 if (dri2_renderer_query_integer(dri2_dpy,
940 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB))
941 disp->Extensions.KHR_gl_colorspace = EGL_TRUE;
943 if (dri2_dpy->image_driver ||
944 (dri2_dpy->dri2 && dri2_dpy->dri2->base.version >= 3) ||
945 (dri2_dpy->swrast && dri2_dpy->swrast->base.version >= 3)) {
946 disp->Extensions.KHR_create_context = EGL_TRUE;
948 if (dri2_dpy->robustness)
949 disp->Extensions.EXT_create_context_robustness = EGL_TRUE;
952 if (dri2_renderer_query_integer(dri2_dpy,
953 __DRI2_RENDERER_HAS_NO_ERROR_CONTEXT))
954 disp->Extensions.KHR_create_context_no_error = EGL_TRUE;
956 if (dri2_dpy->fence) {
957 disp->Extensions.KHR_fence_sync = EGL_TRUE;
958 disp->Extensions.KHR_wait_sync = EGL_TRUE;
959 if (dri2_dpy->fence->get_fence_from_cl_event)
960 disp->Extensions.KHR_cl_event2 = EGL_TRUE;
961 if (dri2_dpy->fence->base.version >= 2 &&
962 dri2_dpy->fence->get_capabilities) {
963 unsigned capabilities =
964 dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen);
965 disp->Extensions.ANDROID_native_fence_sync =
966 (capabilities & __DRI_FENCE_CAP_NATIVE_FD) != 0;
971 disp->Extensions.ANDROID_blob_cache = EGL_TRUE;
973 disp->Extensions.KHR_reusable_sync = EGL_TRUE;
975 if (dri2_dpy->image) {
976 if (dri2_dpy->image->base.version >= 10 &&
977 dri2_dpy->image->getCapabilities != NULL) {
980 capabilities = dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
981 disp->Extensions.MESA_drm_image = (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
983 if (dri2_dpy->image->base.version >= 11)
984 disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
986 disp->Extensions.MESA_drm_image = EGL_TRUE;
987 if (dri2_dpy->image->base.version >= 11)
988 disp->Extensions.MESA_image_dma_buf_export = EGL_TRUE;
991 disp->Extensions.KHR_image_base = EGL_TRUE;
992 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
993 if (dri2_dpy->image->base.version >= 5 &&
994 dri2_dpy->image->createImageFromTexture) {
995 disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
996 disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
998 if (dri2_renderer_query_integer(dri2_dpy,
999 __DRI2_RENDERER_HAS_TEXTURE_3D))
1000 disp->Extensions.KHR_gl_texture_3D_image = EGL_TRUE;
1003 if (dri2_dpy->image->base.version >= 8 &&
1004 dri2_dpy->image->createImageFromDmaBufs) {
1005 disp->Extensions.EXT_image_dma_buf_import = EGL_TRUE;
1006 disp->Extensions.EXT_image_dma_buf_import_modifiers = EGL_TRUE;
1011 if (dri2_dpy->flush_control)
1012 disp->Extensions.KHR_context_flush_control = EGL_TRUE;
1014 if (dri2_dpy->buffer_damage && dri2_dpy->buffer_damage->set_damage_region)
1015 disp->Extensions.KHR_partial_update = EGL_TRUE;
1017 disp->Extensions.EXT_protected_surface =
1018 dri2_renderer_query_integer(dri2_dpy,
1019 __DRI2_RENDERER_HAS_PROTECTED_CONTENT);
1023 dri2_setup_swap_interval(_EGLDisplay *disp, int max_swap_interval)
1025 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1026 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1028 /* Allow driconf to override applications.*/
1029 if (dri2_dpy->config)
1030 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
1031 "vblank_mode", &vblank_mode);
1032 switch (vblank_mode) {
1033 case DRI_CONF_VBLANK_NEVER:
1034 dri2_dpy->min_swap_interval = 0;
1035 dri2_dpy->max_swap_interval = 0;
1036 dri2_dpy->default_swap_interval = 0;
1038 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1039 dri2_dpy->min_swap_interval = 1;
1040 dri2_dpy->max_swap_interval = max_swap_interval;
1041 dri2_dpy->default_swap_interval = 1;
1043 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1044 dri2_dpy->min_swap_interval = 0;
1045 dri2_dpy->max_swap_interval = max_swap_interval;
1046 dri2_dpy->default_swap_interval = 0;
1049 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1050 dri2_dpy->min_swap_interval = 0;
1051 dri2_dpy->max_swap_interval = max_swap_interval;
1052 dri2_dpy->default_swap_interval = 1;
1057 /* All platforms but DRM call this function to create the screen and populate
1058 * the driver_configs. DRM inherits that information from its display - GBM.
1061 dri2_create_screen(_EGLDisplay *disp)
1063 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1065 if (dri2_dpy->image_driver) {
1066 dri2_dpy->dri_screen =
1067 dri2_dpy->image_driver->createNewScreen2(0, dri2_dpy->fd,
1068 dri2_dpy->loader_extensions,
1069 dri2_dpy->driver_extensions,
1070 &dri2_dpy->driver_configs,
1072 } else if (dri2_dpy->dri2) {
1073 if (dri2_dpy->dri2->base.version >= 4) {
1074 dri2_dpy->dri_screen =
1075 dri2_dpy->dri2->createNewScreen2(0, dri2_dpy->fd,
1076 dri2_dpy->loader_extensions,
1077 dri2_dpy->driver_extensions,
1078 &dri2_dpy->driver_configs, disp);
1080 dri2_dpy->dri_screen =
1081 dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd,
1082 dri2_dpy->loader_extensions,
1083 &dri2_dpy->driver_configs, disp);
1086 assert(dri2_dpy->swrast);
1087 if (dri2_dpy->swrast->base.version >= 4) {
1088 dri2_dpy->dri_screen =
1089 dri2_dpy->swrast->createNewScreen2(0, dri2_dpy->loader_extensions,
1090 dri2_dpy->driver_extensions,
1091 &dri2_dpy->driver_configs, disp);
1093 dri2_dpy->dri_screen =
1094 dri2_dpy->swrast->createNewScreen(0, dri2_dpy->loader_extensions,
1095 &dri2_dpy->driver_configs, disp);
1099 if (dri2_dpy->dri_screen == NULL) {
1100 _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen");
1104 dri2_dpy->own_dri_screen = true;
1109 dri2_setup_extensions(_EGLDisplay *disp)
1111 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1112 const struct dri2_extension_match *mandatory_core_extensions;
1113 const __DRIextension **extensions;
1115 extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
1117 if (dri2_dpy->image_driver || dri2_dpy->dri2)
1118 mandatory_core_extensions = dri2_core_extensions;
1120 mandatory_core_extensions = swrast_core_extensions;
1122 if (!dri2_bind_extensions(dri2_dpy, mandatory_core_extensions, extensions, false))
1125 #ifdef HAVE_DRI3_MODIFIERS
1126 dri2_dpy->multibuffers_available =
1127 (dri2_dpy->dri3_major_version > 1 || (dri2_dpy->dri3_major_version == 1 &&
1128 dri2_dpy->dri3_minor_version >= 2)) &&
1129 (dri2_dpy->present_major_version > 1 || (dri2_dpy->present_major_version == 1 &&
1130 dri2_dpy->present_minor_version >= 2)) &&
1131 (dri2_dpy->image && dri2_dpy->image->base.version >= 15);
1134 dri2_bind_extensions(dri2_dpy, optional_core_extensions, extensions, true);
1139 * Called via eglInitialize(), drv->Initialize().
1141 * This must be guaranteed to be called exactly once, even if eglInitialize is
1142 * called many times (without a eglTerminate in between).
1145 dri2_initialize(_EGLDisplay *disp)
1147 EGLBoolean ret = EGL_FALSE;
1148 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1150 /* In the case where the application calls eglMakeCurrent(context1),
1151 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
1152 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
1153 * initialized, as we need it to be able to free context1 correctly.
1155 * It would probably be safest to forcibly release the display with
1156 * dri2_display_release, to make sure the display is reinitialized correctly.
1157 * However, the EGL spec states that we need to keep a reference to the
1158 * current context (so we cannot call dri2_make_current(NULL)), and therefore
1159 * we would leak context1 as we would be missing the old display connection
1160 * to free it up correctly.
1163 dri2_dpy->ref_count++;
1167 loader_set_logger(_eglLog);
1169 switch (disp->Platform) {
1170 case _EGL_PLATFORM_SURFACELESS:
1171 ret = dri2_initialize_surfaceless(disp);
1173 case _EGL_PLATFORM_DEVICE:
1174 ret = dri2_initialize_device(disp);
1176 case _EGL_PLATFORM_X11:
1177 case _EGL_PLATFORM_XCB:
1178 ret = dri2_initialize_x11(disp);
1180 case _EGL_PLATFORM_DRM:
1181 ret = dri2_initialize_drm(disp);
1183 case _EGL_PLATFORM_WAYLAND:
1184 ret = dri2_initialize_wayland(disp);
1186 case _EGL_PLATFORM_ANDROID:
1187 ret = dri2_initialize_android(disp);
1190 unreachable("Callers ensure we cannot get here.");
1197 dri2_dpy = dri2_egl_display(disp);
1198 dri2_dpy->ref_count++;
1204 * Decrement display reference count, and free up display if necessary.
1207 dri2_display_release(_EGLDisplay *disp)
1209 struct dri2_egl_display *dri2_dpy;
1214 dri2_dpy = dri2_egl_display(disp);
1216 assert(dri2_dpy->ref_count > 0);
1217 dri2_dpy->ref_count--;
1219 if (dri2_dpy->ref_count > 0)
1222 _eglCleanupDisplay(disp);
1223 dri2_display_destroy(disp);
1227 dri2_display_destroy(_EGLDisplay *disp)
1229 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1231 if (dri2_dpy->own_dri_screen) {
1232 if (dri2_dpy->vtbl && dri2_dpy->vtbl->close_screen_notify)
1233 dri2_dpy->vtbl->close_screen_notify(disp);
1234 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1236 if (dri2_dpy->fd >= 0)
1237 close(dri2_dpy->fd);
1239 /* Don't dlclose the driver when building with the address sanitizer, so you
1240 * get good symbols from the leak reports.
1242 #if !BUILT_WITH_ASAN || defined(NDEBUG)
1243 if (dri2_dpy->driver)
1244 dlclose(dri2_dpy->driver);
1247 free(dri2_dpy->driver_name);
1249 #ifdef HAVE_WAYLAND_PLATFORM
1250 free(dri2_dpy->device_name);
1253 switch (disp->Platform) {
1254 case _EGL_PLATFORM_X11:
1255 dri2_teardown_x11(dri2_dpy);
1257 case _EGL_PLATFORM_DRM:
1258 dri2_teardown_drm(dri2_dpy);
1260 case _EGL_PLATFORM_WAYLAND:
1261 dri2_teardown_wayland(dri2_dpy);
1264 /* TODO: add teardown for other platforms */
1268 /* The drm platform does not create the screen/driver_configs but reuses
1269 * the ones from the gbm device. As such the gbm itself is responsible
1272 if (disp->Platform != _EGL_PLATFORM_DRM && dri2_dpy->driver_configs) {
1273 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++)
1274 free((__DRIconfig *) dri2_dpy->driver_configs[i]);
1275 free(dri2_dpy->driver_configs);
1278 disp->DriverData = NULL;
1282 dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
1283 unsigned int att, unsigned int format)
1285 struct dri2_egl_display *dri2_dpy =
1286 dri2_egl_display(dri2_surf->base.Resource.Display);
1288 if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
1291 if (!dri2_surf->local_buffers[att]) {
1292 dri2_surf->local_buffers[att] =
1293 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
1294 dri2_surf->base.Width, dri2_surf->base.Height);
1297 return dri2_surf->local_buffers[att];
1301 dri2_egl_surface_free_local_buffers(struct dri2_egl_surface *dri2_surf)
1303 struct dri2_egl_display *dri2_dpy =
1304 dri2_egl_display(dri2_surf->base.Resource.Display);
1306 for (int i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
1307 if (dri2_surf->local_buffers[i]) {
1308 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
1309 dri2_surf->local_buffers[i]);
1310 dri2_surf->local_buffers[i] = NULL;
1316 * Called via eglTerminate(), drv->Terminate().
1318 * This must be guaranteed to be called exactly once, even if eglTerminate is
1319 * called many times (without a eglInitialize in between).
1322 dri2_terminate(_EGLDisplay *disp)
1324 /* Release all non-current Context/Surfaces. */
1325 _eglReleaseDisplayResources(disp);
1327 dri2_display_release(disp);
1333 * Set the error code after a call to
1334 * dri2_egl_display::dri2::createContextAttribs.
1337 dri2_create_context_attribs_error(int dri_error)
1341 switch (dri_error) {
1342 case __DRI_CTX_ERROR_SUCCESS:
1345 case __DRI_CTX_ERROR_NO_MEMORY:
1346 egl_error = EGL_BAD_ALLOC;
1349 /* From the EGL_KHR_create_context spec, section "Errors":
1351 * * If <config> does not support a client API context compatible
1352 * with the requested API major and minor version, [...] context flags,
1353 * and context reset notification behavior (for client API types where
1354 * these attributes are supported), then an EGL_BAD_MATCH error is
1357 * * If an OpenGL ES context is requested and the values for
1358 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1359 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1360 * is not defined, than an EGL_BAD_MATCH error is generated.
1362 * * If an OpenGL context is requested, the requested version is
1363 * greater than 3.2, and the value for attribute
1364 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1365 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1366 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1367 * one of these bits set; or if the implementation does not support
1368 * the requested profile, then an EGL_BAD_MATCH error is generated.
1370 case __DRI_CTX_ERROR_BAD_API:
1371 case __DRI_CTX_ERROR_BAD_VERSION:
1372 case __DRI_CTX_ERROR_BAD_FLAG:
1373 egl_error = EGL_BAD_MATCH;
1376 /* From the EGL_KHR_create_context spec, section "Errors":
1378 * * If an attribute name or attribute value in <attrib_list> is not
1379 * recognized (including unrecognized bits in bitmask attributes),
1380 * then an EGL_BAD_ATTRIBUTE error is generated."
1382 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE:
1383 case __DRI_CTX_ERROR_UNKNOWN_FLAG:
1384 egl_error = EGL_BAD_ATTRIBUTE;
1388 assert(!"unknown dri_error code");
1389 egl_error = EGL_BAD_MATCH;
1393 _eglError(egl_error, "dri2_create_context");
1397 dri2_fill_context_attribs(struct dri2_egl_context *dri2_ctx,
1398 struct dri2_egl_display *dri2_dpy,
1399 uint32_t *ctx_attribs,
1400 unsigned *num_attribs)
1404 assert(*num_attribs >= NUM_ATTRIBS);
1406 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MAJOR_VERSION;
1407 ctx_attribs[pos++] = dri2_ctx->base.ClientMajorVersion;
1408 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_MINOR_VERSION;
1409 ctx_attribs[pos++] = dri2_ctx->base.ClientMinorVersion;
1411 if (dri2_ctx->base.Flags != 0) {
1412 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1413 * extension, don't even try to send it the robust-access flag.
1414 * It may explode. Instead, generate the required EGL error here.
1416 if ((dri2_ctx->base.Flags & EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR) != 0
1417 && !dri2_dpy->robustness) {
1418 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1422 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_FLAGS;
1423 ctx_attribs[pos++] = dri2_ctx->base.Flags;
1426 if (dri2_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION_KHR) {
1427 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1428 * extension, don't even try to send it a reset strategy. It may
1429 * explode. Instead, generate the required EGL error here.
1431 if (!dri2_dpy->robustness) {
1432 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1436 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RESET_STRATEGY;
1437 ctx_attribs[pos++] = __DRI_CTX_RESET_LOSE_CONTEXT;
1440 if (dri2_ctx->base.ContextPriority != EGL_CONTEXT_PRIORITY_MEDIUM_IMG) {
1443 switch (dri2_ctx->base.ContextPriority) {
1444 case EGL_CONTEXT_PRIORITY_HIGH_IMG:
1445 val = __DRI_CTX_PRIORITY_HIGH;
1447 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG:
1448 val = __DRI_CTX_PRIORITY_MEDIUM;
1450 case EGL_CONTEXT_PRIORITY_LOW_IMG:
1451 val = __DRI_CTX_PRIORITY_LOW;
1454 _eglError(EGL_BAD_CONFIG, "eglCreateContext");
1458 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_PRIORITY;
1459 ctx_attribs[pos++] = val;
1462 if (dri2_ctx->base.ReleaseBehavior == EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR) {
1463 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR;
1464 ctx_attribs[pos++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE;
1467 if (dri2_ctx->base.NoError) {
1468 ctx_attribs[pos++] = __DRI_CTX_ATTRIB_NO_ERROR;
1469 ctx_attribs[pos++] = true;
1478 * Called via eglCreateContext(), drv->CreateContext().
1480 static _EGLContext *
1481 dri2_create_context(_EGLDisplay *disp, _EGLConfig *conf,
1482 _EGLContext *share_list, const EGLint *attrib_list)
1484 struct dri2_egl_context *dri2_ctx;
1485 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1486 struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
1487 __DRIcontext *shared =
1488 dri2_ctx_shared ? dri2_ctx_shared->dri_context : NULL;
1489 struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
1490 const __DRIconfig *dri_config;
1493 unsigned num_attribs = NUM_ATTRIBS;
1494 uint32_t ctx_attribs[NUM_ATTRIBS];
1496 dri2_ctx = malloc(sizeof *dri2_ctx);
1498 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
1502 if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
1505 /* The EGL_EXT_create_context_robustness spec says:
1507 * "Add to the eglCreateContext context creation errors: [...]
1509 * * If the reset notification behavior of <share_context> and the
1510 * newly created context are different then an EGL_BAD_MATCH error is
1513 if (share_list && share_list->ResetNotificationStrategy !=
1514 dri2_ctx->base.ResetNotificationStrategy) {
1515 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1519 /* The EGL_KHR_create_context_no_error spec says:
1521 * "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
1522 * used to create <share_context> does not match the value of
1523 * EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
1525 if (share_list && share_list->NoError != dri2_ctx->base.NoError) {
1526 _eglError(EGL_BAD_MATCH, "eglCreateContext");
1530 switch (dri2_ctx->base.ClientAPI) {
1531 case EGL_OPENGL_ES_API:
1532 switch (dri2_ctx->base.ClientMajorVersion) {
1534 api = __DRI_API_GLES;
1537 api = __DRI_API_GLES2;
1540 api = __DRI_API_GLES3;
1543 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1548 case EGL_OPENGL_API:
1549 if ((dri2_ctx->base.ClientMajorVersion >= 4
1550 || (dri2_ctx->base.ClientMajorVersion == 3
1551 && dri2_ctx->base.ClientMinorVersion >= 2))
1552 && dri2_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)
1553 api = __DRI_API_OPENGL_CORE;
1554 else if (dri2_ctx->base.ClientMajorVersion == 3 &&
1555 dri2_ctx->base.ClientMinorVersion == 1)
1556 api = __DRI_API_OPENGL_CORE;
1558 api = __DRI_API_OPENGL;
1561 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
1567 /* The config chosen here isn't necessarily
1568 * used for surfaces later.
1569 * A pixmap surface will use the single config.
1570 * This opportunity depends on disabling the
1571 * doubleBufferMode check in
1572 * src/mesa/main/context.c:check_compatible()
1574 if (dri2_config->dri_config[1][0])
1575 dri_config = dri2_config->dri_config[1][0];
1577 dri_config = dri2_config->dri_config[0][0];
1582 if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs,
1586 if (dri2_dpy->image_driver) {
1587 dri2_ctx->dri_context =
1588 dri2_dpy->image_driver->createContextAttribs(dri2_dpy->dri_screen,
1596 dri2_create_context_attribs_error(error);
1597 } else if (dri2_dpy->dri2) {
1598 if (dri2_dpy->dri2->base.version >= 3) {
1599 dri2_ctx->dri_context =
1600 dri2_dpy->dri2->createContextAttribs(dri2_dpy->dri_screen,
1608 dri2_create_context_attribs_error(error);
1610 dri2_ctx->dri_context =
1611 dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
1618 assert(dri2_dpy->swrast);
1619 if (dri2_dpy->swrast->base.version >= 3) {
1620 dri2_ctx->dri_context =
1621 dri2_dpy->swrast->createContextAttribs(dri2_dpy->dri_screen,
1629 dri2_create_context_attribs_error(error);
1631 dri2_ctx->dri_context =
1632 dri2_dpy->swrast->createNewContextForAPI(dri2_dpy->dri_screen,
1640 if (!dri2_ctx->dri_context)
1643 return &dri2_ctx->base;
1651 * Called via eglDestroyContext(), drv->DestroyContext().
1654 dri2_destroy_context(_EGLDisplay *disp, _EGLContext *ctx)
1656 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1657 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1659 if (_eglPutContext(ctx)) {
1660 dri2_dpy->core->destroyContext(dri2_ctx->dri_context);
1668 dri2_init_surface(_EGLSurface *surf, _EGLDisplay *disp, EGLint type,
1669 _EGLConfig *conf, const EGLint *attrib_list,
1670 EGLBoolean enable_out_fence, void *native_surface)
1672 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1673 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1675 dri2_surf->out_fence_fd = -1;
1676 dri2_surf->enable_out_fence = false;
1677 if (dri2_dpy->fence && dri2_dpy->fence->base.version >= 2 &&
1678 dri2_dpy->fence->get_capabilities &&
1679 (dri2_dpy->fence->get_capabilities(dri2_dpy->dri_screen) &
1680 __DRI_FENCE_CAP_NATIVE_FD)) {
1681 dri2_surf->enable_out_fence = enable_out_fence;
1684 return _eglInitSurface(surf, disp, type, conf, attrib_list, native_surface);
1688 dri2_surface_set_out_fence_fd( _EGLSurface *surf, int fence_fd)
1690 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1692 if (dri2_surf->out_fence_fd >= 0)
1693 close(dri2_surf->out_fence_fd);
1695 dri2_surf->out_fence_fd = fence_fd;
1699 dri2_fini_surface(_EGLSurface *surf)
1701 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1703 dri2_surface_set_out_fence_fd(surf, -1);
1704 dri2_surf->enable_out_fence = false;
1708 dri2_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
1710 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1712 if (!_eglPutSurface(surf))
1715 return dri2_dpy->vtbl->destroy_surface(disp, surf);
1719 dri2_surf_update_fence_fd(_EGLContext *ctx,
1720 _EGLDisplay *disp, _EGLSurface *surf)
1722 __DRIcontext *dri_ctx = dri2_egl_context(ctx)->dri_context;
1723 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1724 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1728 if (!dri2_surf->enable_out_fence)
1731 fence = dri2_dpy->fence->create_fence_fd(dri_ctx, -1);
1733 fence_fd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
1735 dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, fence);
1737 dri2_surface_set_out_fence_fd(surf, fence_fd);
1741 dri2_create_drawable(struct dri2_egl_display *dri2_dpy,
1742 const __DRIconfig *config,
1743 struct dri2_egl_surface *dri2_surf,
1744 void *loaderPrivate)
1746 __DRIcreateNewDrawableFunc createNewDrawable;
1748 if (dri2_dpy->image_driver)
1749 createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
1750 else if (dri2_dpy->dri2)
1751 createNewDrawable = dri2_dpy->dri2->createNewDrawable;
1752 else if (dri2_dpy->swrast)
1753 createNewDrawable = dri2_dpy->swrast->createNewDrawable;
1755 return _eglError(EGL_BAD_ALLOC, "no createNewDrawable");
1757 dri2_surf->dri_drawable = createNewDrawable(dri2_dpy->dri_screen,
1758 config, loaderPrivate);
1759 if (dri2_surf->dri_drawable == NULL)
1760 return _eglError(EGL_BAD_ALLOC, "createNewDrawable");
1766 * Called via eglMakeCurrent(), drv->MakeCurrent().
1769 dri2_make_current(_EGLDisplay *disp, _EGLSurface *dsurf,
1770 _EGLSurface *rsurf, _EGLContext *ctx)
1772 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1773 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1774 _EGLDisplay *old_disp = NULL;
1775 struct dri2_egl_display *old_dri2_dpy = NULL;
1776 _EGLContext *old_ctx;
1777 _EGLSurface *old_dsurf, *old_rsurf;
1778 _EGLSurface *tmp_dsurf, *tmp_rsurf;
1779 __DRIdrawable *ddraw, *rdraw;
1781 EGLint egl_error = EGL_SUCCESS;
1784 return _eglError(EGL_NOT_INITIALIZED, "eglMakeCurrent");
1786 /* make new bindings, set the EGL error otherwise */
1787 if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))
1790 if (old_ctx == ctx && old_dsurf == dsurf && old_rsurf == rsurf) {
1791 _eglPutSurface(old_dsurf);
1792 _eglPutSurface(old_rsurf);
1793 _eglPutContext(old_ctx);
1798 __DRIcontext *old_cctx = dri2_egl_context(old_ctx)->dri_context;
1799 old_disp = old_ctx->Resource.Display;
1800 old_dri2_dpy = dri2_egl_display(old_disp);
1802 /* Disable shared buffer mode */
1803 if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1804 old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1805 old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, false);
1808 dri2_dpy->core->unbindContext(old_cctx);
1811 dri2_surf_update_fence_fd(old_ctx, disp, old_dsurf);
1814 ddraw = (dsurf) ? dri2_dpy->vtbl->get_dri_drawable(dsurf) : NULL;
1815 rdraw = (rsurf) ? dri2_dpy->vtbl->get_dri_drawable(rsurf) : NULL;
1816 cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
1818 if (cctx || ddraw || rdraw) {
1819 if (!dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1820 _EGLContext *tmp_ctx;
1822 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1823 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1826 egl_error = EGL_BAD_MATCH;
1828 /* undo the previous _eglBindContext */
1829 _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &tmp_dsurf, &tmp_rsurf);
1830 assert(&dri2_ctx->base == ctx &&
1831 tmp_dsurf == dsurf &&
1832 tmp_rsurf == rsurf);
1834 _eglPutSurface(dsurf);
1835 _eglPutSurface(rsurf);
1836 _eglPutContext(ctx);
1838 _eglPutSurface(old_dsurf);
1839 _eglPutSurface(old_rsurf);
1840 _eglPutContext(old_ctx);
1842 ddraw = (old_dsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_dsurf) : NULL;
1843 rdraw = (old_rsurf) ? dri2_dpy->vtbl->get_dri_drawable(old_rsurf) : NULL;
1844 cctx = (old_ctx) ? dri2_egl_context(old_ctx)->dri_context : NULL;
1846 /* undo the previous dri2_dpy->core->unbindContext */
1847 if (dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
1848 if (old_dsurf && _eglSurfaceInSharedBufferMode(old_dsurf) &&
1849 old_dri2_dpy->vtbl->set_shared_buffer_mode) {
1850 old_dri2_dpy->vtbl->set_shared_buffer_mode(old_disp, old_dsurf, true);
1853 return _eglError(egl_error, "eglMakeCurrent");
1856 /* We cannot restore the same state as it was before calling
1857 * eglMakeCurrent() and the spec isn't clear about what to do. We
1858 * can prevent EGL from calling into the DRI driver with no DRI
1861 dsurf = rsurf = NULL;
1864 _eglBindContext(ctx, dsurf, rsurf, &tmp_ctx, &tmp_dsurf, &tmp_rsurf);
1865 assert(tmp_ctx == old_ctx && tmp_dsurf == old_dsurf &&
1866 tmp_rsurf == old_rsurf);
1868 _eglLog(_EGL_WARNING, "DRI2: failed to rebind the previous context");
1870 /* dri2_dpy->core->bindContext succeeded, so take a reference on the
1871 * dri2_dpy. This prevents dri2_dpy from being reinitialized when a
1872 * EGLDisplay is terminated and then initialized again while a
1873 * context is still bound. See dri2_intitialize() for a more in depth
1875 dri2_dpy->ref_count++;
1879 dri2_destroy_surface(disp, old_dsurf);
1880 dri2_destroy_surface(disp, old_rsurf);
1883 dri2_destroy_context(disp, old_ctx);
1884 dri2_display_release(old_disp);
1887 if (egl_error != EGL_SUCCESS)
1888 return _eglError(egl_error, "eglMakeCurrent");
1890 if (dsurf && _eglSurfaceHasMutableRenderBuffer(dsurf) &&
1891 dri2_dpy->vtbl->set_shared_buffer_mode) {
1892 /* Always update the shared buffer mode. This is obviously needed when
1893 * the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When
1894 * EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the
1895 * case where external non-EGL API may have changed window's shared
1896 * buffer mode since we last saw it.
1898 bool mode = (dsurf->ActiveRenderBuffer == EGL_SINGLE_BUFFER);
1899 dri2_dpy->vtbl->set_shared_buffer_mode(disp, dsurf, mode);
1906 dri2_surface_get_dri_drawable(_EGLSurface *surf)
1908 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1910 return dri2_surf->dri_drawable;
1914 * Called from eglGetProcAddress() via drv->GetProcAddress().
1917 dri2_get_proc_address(const char *procname)
1919 return _glapi_get_proc_address(procname);
1923 dri2_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
1924 void *native_window, const EGLint *attrib_list)
1926 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1927 return dri2_dpy->vtbl->create_window_surface(disp, conf, native_window,
1932 dri2_create_pixmap_surface(_EGLDisplay *disp, _EGLConfig *conf,
1933 void *native_pixmap, const EGLint *attrib_list)
1935 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1936 if (!dri2_dpy->vtbl->create_pixmap_surface)
1938 return dri2_dpy->vtbl->create_pixmap_surface(disp, conf, native_pixmap,
1943 dri2_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
1944 const EGLint *attrib_list)
1946 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1947 if (!dri2_dpy->vtbl->create_pbuffer_surface)
1949 return dri2_dpy->vtbl->create_pbuffer_surface(disp, conf, attrib_list);
1953 dri2_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
1955 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1956 if (!dri2_dpy->vtbl->swap_interval)
1958 return dri2_dpy->vtbl->swap_interval(disp, surf, interval);
1962 * Asks the client API to flush any rendering to the drawable so that we can
1963 * do our swapbuffers.
1966 dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw)
1968 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1969 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(draw);
1971 if (dri2_dpy->flush) {
1972 if (dri2_dpy->flush->base.version >= 4) {
1973 /* We know there's a current context because:
1975 * "If surface is not bound to the calling thread’s current
1976 * context, an EGL_BAD_SURFACE error is generated."
1978 _EGLContext *ctx = _eglGetCurrentContext();
1979 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1981 /* From the EGL 1.4 spec (page 52):
1983 * "The contents of ancillary buffers are always undefined
1984 * after calling eglSwapBuffers."
1986 dri2_dpy->flush->flush_with_flags(dri2_ctx->dri_context,
1988 __DRI2_FLUSH_DRAWABLE |
1989 __DRI2_FLUSH_INVALIDATE_ANCILLARY,
1990 __DRI2_THROTTLE_SWAPBUFFER);
1992 dri2_dpy->flush->flush(dri_drawable);
1998 dri2_swap_buffers(_EGLDisplay *disp, _EGLSurface *surf)
2000 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2001 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2002 _EGLContext *ctx = _eglGetCurrentContext();
2006 dri2_surf_update_fence_fd(ctx, disp, surf);
2007 ret = dri2_dpy->vtbl->swap_buffers(disp, surf);
2009 /* SwapBuffers marks the end of the frame; reset the damage region for
2010 * use again next time.
2012 if (ret && dri2_dpy->buffer_damage &&
2013 dri2_dpy->buffer_damage->set_damage_region)
2014 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2020 dri2_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *surf,
2021 const EGLint *rects, EGLint n_rects)
2023 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2024 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2025 _EGLContext *ctx = _eglGetCurrentContext();
2029 dri2_surf_update_fence_fd(ctx, disp, surf);
2030 if (dri2_dpy->vtbl->swap_buffers_with_damage)
2031 ret = dri2_dpy->vtbl->swap_buffers_with_damage(disp, surf,
2034 ret = dri2_dpy->vtbl->swap_buffers(disp, surf);
2036 /* SwapBuffers marks the end of the frame; reset the damage region for
2037 * use again next time.
2039 if (ret && dri2_dpy->buffer_damage &&
2040 dri2_dpy->buffer_damage->set_damage_region)
2041 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2047 dri2_swap_buffers_region(_EGLDisplay *disp, _EGLSurface *surf,
2048 EGLint numRects, const EGLint *rects)
2050 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2051 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2054 if (!dri2_dpy->vtbl->swap_buffers_region)
2056 ret = dri2_dpy->vtbl->swap_buffers_region(disp, surf, numRects, rects);
2058 /* SwapBuffers marks the end of the frame; reset the damage region for
2059 * use again next time.
2061 if (ret && dri2_dpy->buffer_damage &&
2062 dri2_dpy->buffer_damage->set_damage_region)
2063 dri2_dpy->buffer_damage->set_damage_region(dri_drawable, 0, NULL);
2069 dri2_set_damage_region(_EGLDisplay *disp, _EGLSurface *surf,
2070 EGLint *rects, EGLint n_rects)
2072 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2073 __DRIdrawable *drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2075 if (!dri2_dpy->buffer_damage || !dri2_dpy->buffer_damage->set_damage_region)
2078 dri2_dpy->buffer_damage->set_damage_region(drawable, n_rects, rects);
2083 dri2_post_sub_buffer(_EGLDisplay *disp, _EGLSurface *surf,
2084 EGLint x, EGLint y, EGLint width, EGLint height)
2086 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2087 if (!dri2_dpy->vtbl->post_sub_buffer)
2089 return dri2_dpy->vtbl->post_sub_buffer(disp, surf, x, y, width, height);
2093 dri2_copy_buffers(_EGLDisplay *disp, _EGLSurface *surf, void *native_pixmap_target)
2095 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2096 if (!dri2_dpy->vtbl->copy_buffers)
2097 return _eglError(EGL_BAD_NATIVE_PIXMAP, "no support for native pixmaps");
2098 return dri2_dpy->vtbl->copy_buffers(disp, surf, native_pixmap_target);
2102 dri2_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surf)
2104 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2105 if (!dri2_dpy->vtbl->query_buffer_age)
2107 return dri2_dpy->vtbl->query_buffer_age(disp, surf);
2111 dri2_wait_client(_EGLDisplay *disp, _EGLContext *ctx)
2113 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2114 _EGLSurface *surf = ctx->DrawSurface;
2115 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2117 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
2118 * we need to copy fake to real here.*/
2120 if (dri2_dpy->flush != NULL)
2121 dri2_dpy->flush->flush(dri_drawable);
2127 dri2_wait_native(EGLint engine)
2129 if (engine != EGL_CORE_NATIVE_ENGINE)
2130 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
2137 dri2_bind_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
2139 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2140 struct dri2_egl_context *dri2_ctx;
2142 GLint format, target;
2143 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2145 ctx = _eglGetCurrentContext();
2146 dri2_ctx = dri2_egl_context(ctx);
2148 if (!_eglBindTexImage(disp, surf, buffer))
2151 switch (surf->TextureFormat) {
2152 case EGL_TEXTURE_RGB:
2153 format = __DRI_TEXTURE_FORMAT_RGB;
2155 case EGL_TEXTURE_RGBA:
2156 format = __DRI_TEXTURE_FORMAT_RGBA;
2159 assert(!"Unexpected texture format in dri2_bind_tex_image()");
2160 format = __DRI_TEXTURE_FORMAT_RGBA;
2163 switch (surf->TextureTarget) {
2164 case EGL_TEXTURE_2D:
2165 target = GL_TEXTURE_2D;
2168 target = GL_TEXTURE_2D;
2169 assert(!"Unexpected texture target in dri2_bind_tex_image()");
2172 dri2_dpy->tex_buffer->setTexBuffer2(dri2_ctx->dri_context,
2180 dri2_release_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
2182 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2183 struct dri2_egl_context *dri2_ctx;
2186 __DRIdrawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(surf);
2188 ctx = _eglGetCurrentContext();
2189 dri2_ctx = dri2_egl_context(ctx);
2191 if (!_eglReleaseTexImage(disp, surf, buffer))
2194 switch (surf->TextureTarget) {
2195 case EGL_TEXTURE_2D:
2196 target = GL_TEXTURE_2D;
2199 assert(!"missing texture target");
2202 if (dri2_dpy->tex_buffer->base.version >= 3 &&
2203 dri2_dpy->tex_buffer->releaseTexBuffer != NULL) {
2204 dri2_dpy->tex_buffer->releaseTexBuffer(dri2_ctx->dri_context,
2205 target, dri_drawable);
2212 dri2_create_image(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
2213 EGLClientBuffer buffer, const EGLint *attr_list)
2215 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2216 return dri2_dpy->vtbl->create_image(disp, ctx, target, buffer,
2221 dri2_create_image_from_dri(_EGLDisplay *disp, __DRIimage *dri_image)
2223 struct dri2_egl_image *dri2_img;
2225 if (dri_image == NULL) {
2226 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
2230 dri2_img = malloc(sizeof *dri2_img);
2232 _eglError(EGL_BAD_ALLOC, "dri2_create_image");
2236 _eglInitImage(&dri2_img->base, disp);
2238 dri2_img->dri_image = dri_image;
2240 return &dri2_img->base;
2244 * Translate a DRI Image extension error code into an EGL error code.
2247 egl_error_from_dri_image_error(int dri_error)
2249 switch (dri_error) {
2250 case __DRI_IMAGE_ERROR_SUCCESS:
2252 case __DRI_IMAGE_ERROR_BAD_ALLOC:
2253 return EGL_BAD_ALLOC;
2254 case __DRI_IMAGE_ERROR_BAD_MATCH:
2255 return EGL_BAD_MATCH;
2256 case __DRI_IMAGE_ERROR_BAD_PARAMETER:
2257 return EGL_BAD_PARAMETER;
2258 case __DRI_IMAGE_ERROR_BAD_ACCESS:
2259 return EGL_BAD_ACCESS;
2261 assert(!"unknown dri_error code");
2262 return EGL_BAD_ALLOC;
2267 dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
2268 EGLClientBuffer buffer,
2269 const EGLint *attr_list)
2271 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2272 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2273 GLuint renderbuffer = (GLuint) (uintptr_t) buffer;
2274 __DRIimage *dri_image;
2276 if (renderbuffer == 0) {
2277 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2278 return EGL_NO_IMAGE_KHR;
2281 if (!disp->Extensions.KHR_gl_renderbuffer_image) {
2282 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2283 return EGL_NO_IMAGE_KHR;
2286 if (dri2_dpy->image->base.version >= 17 &&
2287 dri2_dpy->image->createImageFromRenderbuffer2) {
2288 unsigned error = ~0;
2290 dri_image = dri2_dpy->image->createImageFromRenderbuffer2(
2291 dri2_ctx->dri_context, renderbuffer, NULL, &error);
2293 assert(!!dri_image == (error == __DRI_IMAGE_ERROR_SUCCESS));
2296 _eglError(egl_error_from_dri_image_error(error), "dri2_create_image_khr");
2297 return EGL_NO_IMAGE_KHR;
2300 dri_image = dri2_dpy->image->createImageFromRenderbuffer(
2301 dri2_ctx->dri_context, renderbuffer, NULL);
2303 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2304 return EGL_NO_IMAGE_KHR;
2308 return dri2_create_image_from_dri(disp, dri_image);
2311 #ifdef HAVE_WAYLAND_PLATFORM
2313 /* This structure describes how a wl_buffer maps to one or more
2314 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
2315 * offsets and strides of the planes in the buffer. This table maps a
2316 * wl_drm format code to a description of the planes in the buffer
2317 * that lets us create a __DRIimage for each of the planes. */
2319 static const struct wl_drm_components_descriptor {
2320 uint32_t dri_components;
2323 } wl_drm_components[] = {
2324 { __DRI_IMAGE_COMPONENTS_RGB, EGL_TEXTURE_RGB, 1 },
2325 { __DRI_IMAGE_COMPONENTS_RGBA, EGL_TEXTURE_RGBA, 1 },
2326 { __DRI_IMAGE_COMPONENTS_Y_U_V, EGL_TEXTURE_Y_U_V_WL, 3 },
2327 { __DRI_IMAGE_COMPONENTS_Y_UV, EGL_TEXTURE_Y_UV_WL, 2 },
2328 { __DRI_IMAGE_COMPONENTS_Y_XUXV, EGL_TEXTURE_Y_XUXV_WL, 2 },
2332 dri2_create_image_wayland_wl_buffer(_EGLDisplay *disp, _EGLContext *ctx,
2333 EGLClientBuffer _buffer,
2334 const EGLint *attr_list)
2336 struct wl_drm_buffer *buffer;
2337 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2338 const struct wl_drm_components_descriptor *f;
2339 __DRIimage *dri_image;
2340 _EGLImageAttribs attrs;
2343 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm,
2344 (struct wl_resource *) _buffer);
2348 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2351 plane = attrs.PlaneWL;
2352 f = buffer->driver_format;
2353 if (plane < 0 || plane >= f->nplanes) {
2354 _eglError(EGL_BAD_PARAMETER,
2355 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
2359 dri_image = dri2_dpy->image->fromPlanar(buffer->driver_buffer, plane, NULL);
2360 if (dri_image == NULL && plane == 0)
2361 dri_image = dri2_dpy->image->dupImage(buffer->driver_buffer, NULL);
2362 if (dri_image == NULL) {
2363 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_wayland_wl_buffer");
2367 return dri2_create_image_from_dri(disp, dri_image);
2372 dri2_get_sync_values_chromium(_EGLDisplay *disp, _EGLSurface *surf,
2373 EGLuint64KHR *ust, EGLuint64KHR *msc,
2376 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2377 if (!dri2_dpy->vtbl->get_sync_values)
2379 return dri2_dpy->vtbl->get_sync_values(disp, surf, ust, msc, sbc);
2383 * Set the error code after a call to
2384 * dri2_egl_image::dri_image::createImageFromTexture.
2387 dri2_create_image_khr_texture_error(int dri_error)
2389 EGLint egl_error = egl_error_from_dri_image_error(dri_error);
2391 if (egl_error != EGL_SUCCESS)
2392 _eglError(egl_error, "dri2_create_image_khr_texture");
2396 dri2_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx,
2398 EGLClientBuffer buffer,
2399 const EGLint *attr_list)
2401 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2402 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
2403 struct dri2_egl_image *dri2_img;
2404 GLuint texture = (GLuint) (uintptr_t) buffer;
2405 _EGLImageAttribs attrs;
2411 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2412 return EGL_NO_IMAGE_KHR;
2415 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2416 return EGL_NO_IMAGE_KHR;
2419 case EGL_GL_TEXTURE_2D_KHR:
2420 if (!disp->Extensions.KHR_gl_texture_2D_image) {
2421 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2422 return EGL_NO_IMAGE_KHR;
2425 gl_target = GL_TEXTURE_2D;
2427 case EGL_GL_TEXTURE_3D_KHR:
2428 if (!disp->Extensions.KHR_gl_texture_3D_image) {
2429 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2430 return EGL_NO_IMAGE_KHR;
2433 depth = attrs.GLTextureZOffset;
2434 gl_target = GL_TEXTURE_3D;
2436 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
2437 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
2438 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
2439 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
2440 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
2441 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
2442 if (!disp->Extensions.KHR_gl_texture_cubemap_image) {
2443 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
2444 return EGL_NO_IMAGE_KHR;
2447 depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
2448 gl_target = GL_TEXTURE_CUBE_MAP;
2451 unreachable("Unexpected target in dri2_create_image_khr_texture()");
2452 return EGL_NO_IMAGE_KHR;
2455 dri2_img = malloc(sizeof *dri2_img);
2457 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2458 return EGL_NO_IMAGE_KHR;
2461 _eglInitImage(&dri2_img->base, disp);
2463 dri2_img->dri_image =
2464 dri2_dpy->image->createImageFromTexture(dri2_ctx->dri_context,
2468 attrs.GLTextureLevel,
2471 dri2_create_image_khr_texture_error(error);
2473 if (!dri2_img->dri_image) {
2475 return EGL_NO_IMAGE_KHR;
2477 return &dri2_img->base;
2481 dri2_query_surface(_EGLDisplay *disp, _EGLSurface *surf,
2482 EGLint attribute, EGLint *value)
2484 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2485 if (!dri2_dpy->vtbl->query_surface)
2486 return _eglQuerySurface(disp, surf, attribute, value);
2487 return dri2_dpy->vtbl->query_surface(disp, surf, attribute, value);
2490 static struct wl_buffer*
2491 dri2_create_wayland_buffer_from_image(_EGLDisplay *disp, _EGLImage *img)
2493 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2494 if (!dri2_dpy->vtbl->create_wayland_buffer_from_image)
2496 return dri2_dpy->vtbl->create_wayland_buffer_from_image(disp, img);
2501 dri2_create_image_mesa_drm_buffer(_EGLDisplay *disp, _EGLContext *ctx,
2502 EGLClientBuffer buffer, const EGLint *attr_list)
2504 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2505 EGLint format, name, pitch;
2506 _EGLImageAttribs attrs;
2507 __DRIimage *dri_image;
2509 name = (EGLint) (uintptr_t) buffer;
2511 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2514 if (attrs.Width <= 0 || attrs.Height <= 0 ||
2515 attrs.DRMBufferStrideMESA <= 0) {
2516 _eglError(EGL_BAD_PARAMETER,
2517 "bad width, height or stride");
2521 switch (attrs.DRMBufferFormatMESA) {
2522 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2523 format = __DRI_IMAGE_FORMAT_ARGB8888;
2524 pitch = attrs.DRMBufferStrideMESA;
2527 _eglError(EGL_BAD_PARAMETER,
2528 "dri2_create_image_khr: unsupported pixmap depth");
2533 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
2541 return dri2_create_image_from_dri(disp, dri_image);
2545 dri2_check_dma_buf_attribs(const _EGLImageAttribs *attrs)
2550 * "Required attributes and their values are as follows:
2552 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
2554 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
2555 * by drm_fourcc.h and used as the pixel_format parameter of the
2556 * drm_mode_fb_cmd2 ioctl."
2560 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2561 * incomplete, EGL_BAD_PARAMETER is generated."
2563 if (attrs->Width <= 0 || attrs->Height <= 0 ||
2564 !attrs->DMABufFourCC.IsPresent)
2565 return _eglError(EGL_BAD_PARAMETER, "attribute(s) missing");
2570 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
2571 * specified for a plane's pitch or offset isn't supported by EGL,
2572 * EGL_BAD_ACCESS is generated."
2574 for (unsigned i = 0; i < ARRAY_SIZE(attrs->DMABufPlanePitches); ++i) {
2575 if (attrs->DMABufPlanePitches[i].IsPresent &&
2576 attrs->DMABufPlanePitches[i].Value <= 0)
2577 return _eglError(EGL_BAD_ACCESS, "invalid pitch");
2581 * If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2582 * attribute values may be given.
2584 * This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2585 * EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2587 for (unsigned i = 0; i < DMA_BUF_MAX_PLANES; ++i) {
2588 if (attrs->DMABufPlaneModifiersLo[i].IsPresent !=
2589 attrs->DMABufPlaneModifiersHi[i].IsPresent)
2590 return _eglError(EGL_BAD_PARAMETER, "modifier attribute lo or hi missing");
2593 /* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2594 * mandate it, we only accept the same modifier across all planes. */
2595 for (unsigned i = 1; i < DMA_BUF_MAX_PLANES; ++i) {
2596 if (attrs->DMABufPlaneFds[i].IsPresent) {
2597 if ((attrs->DMABufPlaneModifiersLo[0].IsPresent !=
2598 attrs->DMABufPlaneModifiersLo[i].IsPresent) ||
2599 (attrs->DMABufPlaneModifiersLo[0].Value !=
2600 attrs->DMABufPlaneModifiersLo[i].Value) ||
2601 (attrs->DMABufPlaneModifiersHi[0].Value !=
2602 attrs->DMABufPlaneModifiersHi[i].Value))
2603 return _eglError(EGL_BAD_PARAMETER, "modifier attributes not equal");
2610 /* Returns the total number of planes for the format or zero if it isn't a
2611 * valid fourcc format.
2614 dri2_num_fourcc_format_planes(EGLint format)
2618 case DRM_FORMAT_RG88:
2619 case DRM_FORMAT_GR88:
2620 case DRM_FORMAT_R16:
2621 case DRM_FORMAT_GR1616:
2622 case DRM_FORMAT_RGB332:
2623 case DRM_FORMAT_BGR233:
2624 case DRM_FORMAT_XRGB4444:
2625 case DRM_FORMAT_XBGR4444:
2626 case DRM_FORMAT_RGBX4444:
2627 case DRM_FORMAT_BGRX4444:
2628 case DRM_FORMAT_ARGB4444:
2629 case DRM_FORMAT_ABGR4444:
2630 case DRM_FORMAT_RGBA4444:
2631 case DRM_FORMAT_BGRA4444:
2632 case DRM_FORMAT_XRGB1555:
2633 case DRM_FORMAT_XBGR1555:
2634 case DRM_FORMAT_RGBX5551:
2635 case DRM_FORMAT_BGRX5551:
2636 case DRM_FORMAT_ARGB1555:
2637 case DRM_FORMAT_ABGR1555:
2638 case DRM_FORMAT_RGBA5551:
2639 case DRM_FORMAT_BGRA5551:
2640 case DRM_FORMAT_RGB565:
2641 case DRM_FORMAT_BGR565:
2642 case DRM_FORMAT_RGB888:
2643 case DRM_FORMAT_BGR888:
2644 case DRM_FORMAT_XRGB8888:
2645 case DRM_FORMAT_XBGR8888:
2646 case DRM_FORMAT_RGBX8888:
2647 case DRM_FORMAT_BGRX8888:
2648 case DRM_FORMAT_ARGB8888:
2649 case DRM_FORMAT_ABGR8888:
2650 case DRM_FORMAT_RGBA8888:
2651 case DRM_FORMAT_BGRA8888:
2652 case DRM_FORMAT_XRGB2101010:
2653 case DRM_FORMAT_XBGR2101010:
2654 case DRM_FORMAT_RGBX1010102:
2655 case DRM_FORMAT_BGRX1010102:
2656 case DRM_FORMAT_ARGB2101010:
2657 case DRM_FORMAT_ABGR2101010:
2658 case DRM_FORMAT_RGBA1010102:
2659 case DRM_FORMAT_BGRA1010102:
2660 case DRM_FORMAT_XBGR16161616F:
2661 case DRM_FORMAT_ABGR16161616F:
2662 case DRM_FORMAT_YUYV:
2663 case DRM_FORMAT_YVYU:
2664 case DRM_FORMAT_UYVY:
2665 case DRM_FORMAT_VYUY:
2666 case DRM_FORMAT_AYUV:
2667 case DRM_FORMAT_XYUV8888:
2668 case DRM_FORMAT_Y210:
2669 case DRM_FORMAT_Y212:
2670 case DRM_FORMAT_Y216:
2671 case DRM_FORMAT_Y410:
2672 case DRM_FORMAT_Y412:
2673 case DRM_FORMAT_Y416:
2676 case DRM_FORMAT_NV12:
2677 case DRM_FORMAT_NV21:
2678 case DRM_FORMAT_NV16:
2679 case DRM_FORMAT_NV61:
2680 case DRM_FORMAT_P010:
2681 case DRM_FORMAT_P012:
2682 case DRM_FORMAT_P016:
2685 case DRM_FORMAT_YUV410:
2686 case DRM_FORMAT_YVU410:
2687 case DRM_FORMAT_YUV411:
2688 case DRM_FORMAT_YVU411:
2689 case DRM_FORMAT_YUV420:
2690 case DRM_FORMAT_YVU420:
2691 case DRM_FORMAT_YUV422:
2692 case DRM_FORMAT_YVU422:
2693 case DRM_FORMAT_YUV444:
2694 case DRM_FORMAT_YVU444:
2702 /* Returns the total number of file descriptors. Zero indicates an error. */
2704 dri2_check_dma_buf_format(const _EGLImageAttribs *attrs)
2706 unsigned plane_n = dri2_num_fourcc_format_planes(attrs->DMABufFourCC.Value);
2708 _eglError(EGL_BAD_MATCH, "unknown drm fourcc format");
2712 for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; i++) {
2714 * The modifiers extension spec says:
2716 * "Modifiers may modify any attribute of a buffer import, including
2717 * but not limited to adding extra planes to a format which
2718 * otherwise does not have those planes. As an example, a modifier
2719 * may add a plane for an external compression buffer to a
2720 * single-plane format. The exact meaning and effect of any
2721 * modifier is canonically defined by drm_fourcc.h, not as part of
2724 if (attrs->DMABufPlaneModifiersLo[i].IsPresent &&
2725 attrs->DMABufPlaneModifiersHi[i].IsPresent) {
2733 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2734 * incomplete, EGL_BAD_PARAMETER is generated."
2736 for (unsigned i = 0; i < plane_n; ++i) {
2737 if (!attrs->DMABufPlaneFds[i].IsPresent ||
2738 !attrs->DMABufPlaneOffsets[i].IsPresent ||
2739 !attrs->DMABufPlanePitches[i].IsPresent) {
2740 _eglError(EGL_BAD_PARAMETER, "plane attribute(s) missing");
2746 * The spec also says:
2748 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2749 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2750 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2751 * or EGL_DMA_BUF_PLANE3_* attributes are specified."
2753 for (unsigned i = plane_n; i < DMA_BUF_MAX_PLANES; ++i) {
2754 if (attrs->DMABufPlaneFds[i].IsPresent ||
2755 attrs->DMABufPlaneOffsets[i].IsPresent ||
2756 attrs->DMABufPlanePitches[i].IsPresent) {
2757 _eglError(EGL_BAD_ATTRIBUTE, "too many plane attributes");
2766 dri2_query_dma_buf_formats(_EGLDisplay *disp, EGLint max,
2767 EGLint *formats, EGLint *count)
2769 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2770 if (max < 0 || (max > 0 && formats == NULL))
2771 return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
2773 if (dri2_dpy->image->base.version < 15 ||
2774 dri2_dpy->image->queryDmaBufFormats == NULL)
2777 if (!dri2_dpy->image->queryDmaBufFormats(dri2_dpy->dri_screen, max,
2782 /* Assert that all of the formats returned are actually fourcc formats.
2783 * Some day, if we want the internal interface function to be able to
2784 * return the fake fourcc formats defined in dri_interface.h, we'll have
2785 * to do something more clever here to pair the list down to just real
2786 * fourcc formats so that we don't leak the fake internal ones.
2788 for (int i = 0; i < *count; i++) {
2789 assert(dri2_num_fourcc_format_planes(formats[i]) > 0);
2797 dri2_query_dma_buf_modifiers(_EGLDisplay *disp, EGLint format,
2798 EGLint max, EGLuint64KHR *modifiers,
2799 EGLBoolean *external_only, EGLint *count)
2801 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2803 if (dri2_num_fourcc_format_planes(format) == 0)
2804 return _eglError(EGL_BAD_PARAMETER, "invalid fourcc format");
2807 return _eglError(EGL_BAD_PARAMETER, "invalid value for max count of formats");
2809 if (max > 0 && modifiers == NULL)
2810 return _eglError(EGL_BAD_PARAMETER, "invalid modifiers array");
2812 if (dri2_dpy->image->base.version < 15 ||
2813 dri2_dpy->image->queryDmaBufModifiers == NULL)
2816 if (dri2_dpy->image->queryDmaBufModifiers(dri2_dpy->dri_screen, format,
2818 (unsigned int *) external_only,
2820 return _eglError(EGL_BAD_PARAMETER, "invalid format");
2828 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2829 * EGL will take a reference to the dma_buf(s) which it will release at any
2830 * time while the EGLDisplay is initialized. It is the responsibility of the
2831 * application to close the dma_buf file descriptors."
2833 * Therefore we must never close or otherwise modify the file descriptors.
2836 dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
2837 EGLClientBuffer buffer, const EGLint *attr_list)
2839 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2841 _EGLImageAttribs attrs;
2842 __DRIimage *dri_image;
2844 int fds[DMA_BUF_MAX_PLANES];
2845 int pitches[DMA_BUF_MAX_PLANES];
2846 int offsets[DMA_BUF_MAX_PLANES];
2848 bool has_modifier = false;
2854 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2855 * error EGL_BAD_PARAMETER is generated."
2857 if (buffer != NULL) {
2858 _eglError(EGL_BAD_PARAMETER, "buffer not NULL");
2862 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2865 if (!dri2_check_dma_buf_attribs(&attrs))
2868 num_fds = dri2_check_dma_buf_format(&attrs);
2872 for (unsigned i = 0; i < num_fds; ++i) {
2873 fds[i] = attrs.DMABufPlaneFds[i].Value;
2874 pitches[i] = attrs.DMABufPlanePitches[i].Value;
2875 offsets[i] = attrs.DMABufPlaneOffsets[i].Value;
2878 /* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2879 * will be present in attrs.DMABufPlaneModifiersLo[0] and
2880 * attrs.DMABufPlaneModifiersHi[0] */
2881 if (attrs.DMABufPlaneModifiersLo[0].IsPresent) {
2882 modifier = combine_u32_into_u64(attrs.DMABufPlaneModifiersHi[0].Value,
2883 attrs.DMABufPlaneModifiersLo[0].Value);
2884 has_modifier = true;
2887 if (attrs.ProtectedContent) {
2888 if (dri2_dpy->image->base.version < 18 ||
2889 dri2_dpy->image->createImageFromDmaBufs3 == NULL) {
2890 _eglError(EGL_BAD_MATCH, "unsupported protected_content attribute");
2891 return EGL_NO_IMAGE_KHR;
2894 modifier = DRM_FORMAT_MOD_INVALID;
2897 dri2_dpy->image->createImageFromDmaBufs3(dri2_dpy->dri_screen,
2898 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2899 modifier, fds, num_fds, pitches, offsets,
2900 attrs.DMABufYuvColorSpaceHint.Value,
2901 attrs.DMABufSampleRangeHint.Value,
2902 attrs.DMABufChromaHorizontalSiting.Value,
2903 attrs.DMABufChromaVerticalSiting.Value,
2904 attrs.ProtectedContent ? __DRI_IMAGE_PROTECTED_CONTENT_FLAG : 0,
2908 else if (has_modifier) {
2909 if (dri2_dpy->image->base.version < 15 ||
2910 dri2_dpy->image->createImageFromDmaBufs2 == NULL) {
2911 _eglError(EGL_BAD_MATCH, "unsupported dma_buf format modifier");
2912 return EGL_NO_IMAGE_KHR;
2915 dri2_dpy->image->createImageFromDmaBufs2(dri2_dpy->dri_screen,
2916 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2917 modifier, fds, num_fds, pitches, offsets,
2918 attrs.DMABufYuvColorSpaceHint.Value,
2919 attrs.DMABufSampleRangeHint.Value,
2920 attrs.DMABufChromaHorizontalSiting.Value,
2921 attrs.DMABufChromaVerticalSiting.Value,
2927 dri2_dpy->image->createImageFromDmaBufs(dri2_dpy->dri_screen,
2928 attrs.Width, attrs.Height, attrs.DMABufFourCC.Value,
2929 fds, num_fds, pitches, offsets,
2930 attrs.DMABufYuvColorSpaceHint.Value,
2931 attrs.DMABufSampleRangeHint.Value,
2932 attrs.DMABufChromaHorizontalSiting.Value,
2933 attrs.DMABufChromaVerticalSiting.Value,
2937 dri2_create_image_khr_texture_error(error);
2940 return EGL_NO_IMAGE_KHR;
2942 res = dri2_create_image_from_dri(disp, dri_image);
2947 dri2_create_drm_image_mesa(_EGLDisplay *disp, const EGLint *attr_list)
2949 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2950 struct dri2_egl_image *dri2_img;
2951 _EGLImageAttribs attrs;
2952 unsigned int dri_use, valid_mask;
2956 _eglError(EGL_BAD_PARAMETER, __func__);
2957 return EGL_NO_IMAGE_KHR;
2960 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
2961 return EGL_NO_IMAGE_KHR;
2963 if (attrs.Width <= 0 || attrs.Height <= 0) {
2964 _eglError(EGL_BAD_PARAMETER, __func__);
2965 return EGL_NO_IMAGE_KHR;
2968 switch (attrs.DRMBufferFormatMESA) {
2969 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA:
2970 format = __DRI_IMAGE_FORMAT_ARGB8888;
2973 _eglError(EGL_BAD_PARAMETER, __func__);
2974 return EGL_NO_IMAGE_KHR;
2978 EGL_DRM_BUFFER_USE_SCANOUT_MESA |
2979 EGL_DRM_BUFFER_USE_SHARE_MESA |
2980 EGL_DRM_BUFFER_USE_CURSOR_MESA;
2981 if (attrs.DRMBufferUseMESA & ~valid_mask) {
2982 _eglError(EGL_BAD_PARAMETER, __func__);
2983 return EGL_NO_IMAGE_KHR;
2987 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SHARE_MESA)
2988 dri_use |= __DRI_IMAGE_USE_SHARE;
2989 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_SCANOUT_MESA)
2990 dri_use |= __DRI_IMAGE_USE_SCANOUT;
2991 if (attrs.DRMBufferUseMESA & EGL_DRM_BUFFER_USE_CURSOR_MESA)
2992 dri_use |= __DRI_IMAGE_USE_CURSOR;
2994 dri2_img = malloc(sizeof *dri2_img);
2996 _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
2997 return EGL_NO_IMAGE_KHR;
3000 _eglInitImage(&dri2_img->base, disp);
3002 dri2_img->dri_image =
3003 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
3004 attrs.Width, attrs.Height,
3005 format, dri_use, dri2_img);
3006 if (dri2_img->dri_image == NULL) {
3008 _eglError(EGL_BAD_ALLOC, "dri2_create_drm_image_mesa");
3009 return EGL_NO_IMAGE_KHR;
3012 return &dri2_img->base;
3016 dri2_export_drm_image_mesa(_EGLDisplay *disp, _EGLImage *img,
3017 EGLint *name, EGLint *handle, EGLint *stride)
3019 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3020 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3022 if (name && !dri2_dpy->image->queryImage(dri2_img->dri_image,
3023 __DRI_IMAGE_ATTRIB_NAME, name))
3024 return _eglError(EGL_BAD_ALLOC, "dri2_export_drm_image_mesa");
3027 dri2_dpy->image->queryImage(dri2_img->dri_image,
3028 __DRI_IMAGE_ATTRIB_HANDLE, handle);
3031 dri2_dpy->image->queryImage(dri2_img->dri_image,
3032 __DRI_IMAGE_ATTRIB_STRIDE, stride);
3038 * Checks if we can support EGL_MESA_image_dma_buf_export on this image.
3040 * The spec provides a boolean return for the driver to reject exporting for
3041 * basically any reason, but doesn't specify any particular error cases. For
3042 * now, we just fail if we don't have a DRM fourcc for the format.
3045 dri2_can_export_dma_buf_image(_EGLDisplay *disp, _EGLImage *img)
3047 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3048 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3051 if (!dri2_dpy->image->queryImage(dri2_img->dri_image,
3052 __DRI_IMAGE_ATTRIB_FOURCC, &fourcc)) {
3060 dri2_export_dma_buf_image_query_mesa(_EGLDisplay *disp, _EGLImage *img,
3061 EGLint *fourcc, EGLint *nplanes,
3062 EGLuint64KHR *modifiers)
3064 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3065 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3068 if (!dri2_can_export_dma_buf_image(disp, img))
3071 dri2_dpy->image->queryImage(dri2_img->dri_image,
3072 __DRI_IMAGE_ATTRIB_NUM_PLANES, &num_planes);
3074 *nplanes = num_planes;
3077 dri2_dpy->image->queryImage(dri2_img->dri_image,
3078 __DRI_IMAGE_ATTRIB_FOURCC, fourcc);
3082 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
3085 query = dri2_dpy->image->queryImage(dri2_img->dri_image,
3086 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
3088 query &= dri2_dpy->image->queryImage(dri2_img->dri_image,
3089 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
3092 modifier = combine_u32_into_u64 (mod_hi, mod_lo);
3094 for (int i = 0; i < num_planes; i++)
3095 modifiers[i] = modifier;
3102 dri2_export_dma_buf_image_mesa(_EGLDisplay *disp, _EGLImage *img,
3103 int *fds, EGLint *strides, EGLint *offsets)
3105 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3106 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
3109 if (!dri2_can_export_dma_buf_image(disp, img))
3112 /* EGL_MESA_image_dma_buf_export spec says:
3113 * "If the number of fds is less than the number of planes, then
3114 * subsequent fd slots should contain -1."
3117 /* Query nplanes so that we know how big the given array is. */
3118 dri2_dpy->image->queryImage(dri2_img->dri_image,
3119 __DRI_IMAGE_ATTRIB_NUM_PLANES, &nplanes);
3120 memset(fds, -1, nplanes * sizeof(int));
3123 /* rework later to provide multiple fds/strides/offsets */
3125 dri2_dpy->image->queryImage(dri2_img->dri_image,
3126 __DRI_IMAGE_ATTRIB_FD, fds);
3129 dri2_dpy->image->queryImage(dri2_img->dri_image,
3130 __DRI_IMAGE_ATTRIB_STRIDE, strides);
3134 bool ret = dri2_dpy->image->queryImage(dri2_img->dri_image,
3135 __DRI_IMAGE_ATTRIB_OFFSET, &img_offset);
3137 offsets[0] = img_offset;
3148 dri2_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
3149 EGLClientBuffer buffer, const EGLint *attr_list)
3152 case EGL_GL_TEXTURE_2D_KHR:
3153 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
3154 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
3155 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
3156 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
3157 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
3158 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
3159 case EGL_GL_TEXTURE_3D_KHR:
3160 return dri2_create_image_khr_texture(disp, ctx, target, buffer, attr_list);
3161 case EGL_GL_RENDERBUFFER_KHR:
3162 return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
3164 case EGL_DRM_BUFFER_MESA:
3165 return dri2_create_image_mesa_drm_buffer(disp, ctx, buffer, attr_list);
3166 case EGL_LINUX_DMA_BUF_EXT:
3167 return dri2_create_image_dma_buf(disp, ctx, buffer, attr_list);
3169 #ifdef HAVE_WAYLAND_PLATFORM
3170 case EGL_WAYLAND_BUFFER_WL:
3171 return dri2_create_image_wayland_wl_buffer(disp, ctx, buffer, attr_list);
3174 _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
3175 return EGL_NO_IMAGE_KHR;
3180 dri2_destroy_image_khr(_EGLDisplay *disp, _EGLImage *image)
3182 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3183 struct dri2_egl_image *dri2_img = dri2_egl_image(image);
3185 dri2_dpy->image->destroyImage(dri2_img->dri_image);
3191 #ifdef HAVE_WAYLAND_PLATFORM
3194 dri2_wl_reference_buffer(void *user_data, uint32_t name, int fd,
3195 struct wl_drm_buffer *buffer)
3197 _EGLDisplay *disp = user_data;
3198 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3200 int dri_components = 0;
3203 img = dri2_dpy->image->createImageFromNames(dri2_dpy->dri_screen,
3212 img = dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
3224 dri2_dpy->image->queryImage(img, __DRI_IMAGE_ATTRIB_COMPONENTS, &dri_components);
3226 buffer->driver_format = NULL;
3227 for (int i = 0; i < ARRAY_SIZE(wl_drm_components); i++)
3228 if (wl_drm_components[i].dri_components == dri_components)
3229 buffer->driver_format = &wl_drm_components[i];
3231 if (buffer->driver_format == NULL)
3232 dri2_dpy->image->destroyImage(img);
3234 buffer->driver_buffer = img;
3238 dri2_wl_release_buffer(void *user_data, struct wl_drm_buffer *buffer)
3240 _EGLDisplay *disp = user_data;
3241 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3243 dri2_dpy->image->destroyImage(buffer->driver_buffer);
3247 dri2_bind_wayland_display_wl(_EGLDisplay *disp, struct wl_display *wl_dpy)
3249 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3250 const struct wayland_drm_callbacks wl_drm_callbacks = {
3251 .authenticate = (int(*)(void *, uint32_t)) dri2_dpy->vtbl->authenticate,
3252 .reference_buffer = dri2_wl_reference_buffer,
3253 .release_buffer = dri2_wl_release_buffer,
3254 .is_format_supported = dri2_wl_is_format_supported
3260 if (dri2_dpy->wl_server_drm)
3263 device_name = drmGetRenderDeviceNameFromFd(dri2_dpy->fd);
3265 device_name = strdup(dri2_dpy->device_name);
3269 if (drmGetCap(dri2_dpy->fd, DRM_CAP_PRIME, &cap) == 0 &&
3270 cap == (DRM_PRIME_CAP_IMPORT | DRM_PRIME_CAP_EXPORT) &&
3271 dri2_dpy->image->base.version >= 7 &&
3272 dri2_dpy->image->createImageFromFds != NULL)
3273 flags |= WAYLAND_DRM_PRIME;
3275 dri2_dpy->wl_server_drm =
3276 wayland_drm_init(wl_dpy, device_name,
3277 &wl_drm_callbacks, disp, flags);
3281 if (!dri2_dpy->wl_server_drm)
3284 #ifdef HAVE_DRM_PLATFORM
3285 /* We have to share the wl_drm instance with gbm, so gbm can convert
3286 * wl_buffers to gbm bos. */
3287 if (dri2_dpy->gbm_dri)
3288 dri2_dpy->gbm_dri->wl_drm = dri2_dpy->wl_server_drm;
3295 dri2_unbind_wayland_display_wl(_EGLDisplay *disp, struct wl_display *wl_dpy)
3297 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3299 if (!dri2_dpy->wl_server_drm)
3302 wayland_drm_uninit(dri2_dpy->wl_server_drm);
3303 dri2_dpy->wl_server_drm = NULL;
3309 dri2_query_wayland_buffer_wl(_EGLDisplay *disp, struct wl_resource *buffer_resource,
3310 EGLint attribute, EGLint *value)
3312 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3313 struct wl_drm_buffer *buffer;
3314 const struct wl_drm_components_descriptor *format;
3316 buffer = wayland_drm_buffer_get(dri2_dpy->wl_server_drm, buffer_resource);
3320 format = buffer->driver_format;
3321 switch (attribute) {
3322 case EGL_TEXTURE_FORMAT:
3323 *value = format->components;
3326 *value = buffer->width;
3329 *value = buffer->height;
3338 dri2_egl_ref_sync(struct dri2_egl_sync *sync)
3340 p_atomic_inc(&sync->refcount);
3344 dri2_egl_unref_sync(struct dri2_egl_display *dri2_dpy,
3345 struct dri2_egl_sync *dri2_sync)
3347 if (p_atomic_dec_zero(&dri2_sync->refcount)) {
3348 switch (dri2_sync->base.Type) {
3349 case EGL_SYNC_REUSABLE_KHR:
3350 cnd_destroy(&dri2_sync->cond);
3352 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3353 if (dri2_sync->base.SyncFd != EGL_NO_NATIVE_FENCE_FD_ANDROID)
3354 close(dri2_sync->base.SyncFd);
3360 if (dri2_sync->fence)
3361 dri2_dpy->fence->destroy_fence(dri2_dpy->dri_screen, dri2_sync->fence);
3368 dri2_create_sync(_EGLDisplay *disp, EGLenum type, const EGLAttrib *attrib_list)
3370 _EGLContext *ctx = _eglGetCurrentContext();
3371 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3372 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3373 struct dri2_egl_sync *dri2_sync;
3375 pthread_condattr_t attr;
3377 dri2_sync = calloc(1, sizeof(struct dri2_egl_sync));
3379 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3383 if (!_eglInitSync(&dri2_sync->base, disp, type, attrib_list)) {
3389 case EGL_SYNC_FENCE_KHR:
3390 dri2_sync->fence = dri2_dpy->fence->create_fence(dri2_ctx->dri_context);
3391 if (!dri2_sync->fence) {
3392 /* Why did it fail? DRI doesn't return an error code, so we emit
3393 * a generic EGL error that doesn't communicate user error.
3395 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
3401 case EGL_SYNC_CL_EVENT_KHR:
3402 dri2_sync->fence = dri2_dpy->fence->get_fence_from_cl_event(
3403 dri2_dpy->dri_screen,
3404 dri2_sync->base.CLEvent);
3405 /* this can only happen if the cl_event passed in is invalid. */
3406 if (!dri2_sync->fence) {
3407 _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3412 /* the initial status must be "signaled" if the cl_event is signaled */
3413 if (dri2_dpy->fence->client_wait_sync(dri2_ctx->dri_context,
3414 dri2_sync->fence, 0, 0))
3415 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3418 case EGL_SYNC_REUSABLE_KHR:
3419 /* intialize attr */
3420 ret = pthread_condattr_init(&attr);
3423 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3428 /* change clock attribute to CLOCK_MONOTONIC */
3429 ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
3432 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3437 ret = pthread_cond_init(&dri2_sync->cond, &attr);
3440 _eglError(EGL_BAD_ACCESS, "eglCreateSyncKHR");
3445 /* initial status of reusable sync must be "unsignaled" */
3446 dri2_sync->base.SyncStatus = EGL_UNSIGNALED_KHR;
3449 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3450 if (dri2_dpy->fence->create_fence_fd) {
3451 dri2_sync->fence = dri2_dpy->fence->create_fence_fd(
3452 dri2_ctx->dri_context,
3453 dri2_sync->base.SyncFd);
3455 if (!dri2_sync->fence) {
3456 _eglError(EGL_BAD_ATTRIBUTE, "eglCreateSyncKHR");
3463 p_atomic_set(&dri2_sync->refcount, 1);
3464 return &dri2_sync->base;
3468 dri2_destroy_sync(_EGLDisplay *disp, _EGLSync *sync)
3470 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3471 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3472 EGLint ret = EGL_TRUE;
3475 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
3476 * then unlock all threads possibly blocked by the reusable sync before
3479 if (dri2_sync->base.Type == EGL_SYNC_REUSABLE_KHR &&
3480 dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3481 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3482 /* unblock all threads currently blocked by sync */
3483 err = cnd_broadcast(&dri2_sync->cond);
3486 _eglError(EGL_BAD_ACCESS, "eglDestroySyncKHR");
3491 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
3497 dri2_dup_native_fence_fd(_EGLDisplay *disp, _EGLSync *sync)
3499 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3500 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3502 assert(sync->Type == EGL_SYNC_NATIVE_FENCE_ANDROID);
3504 if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3505 /* try to retrieve the actual native fence fd.. if rendering is
3506 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
3508 sync->SyncFd = dri2_dpy->fence->get_fence_fd(dri2_dpy->dri_screen,
3512 if (sync->SyncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3513 /* if native fence fd still not created, return an error: */
3514 _eglError(EGL_BAD_PARAMETER, "eglDupNativeFenceFDANDROID");
3515 return EGL_NO_NATIVE_FENCE_FD_ANDROID;
3518 assert(sync_valid_fd(sync->SyncFd));
3520 return os_dupfd_cloexec(sync->SyncFd);
3524 dri2_set_blob_cache_funcs(_EGLDisplay *disp,
3525 EGLSetBlobFuncANDROID set,
3526 EGLGetBlobFuncANDROID get)
3528 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3529 dri2_dpy->blob->set_cache_funcs(dri2_dpy->dri_screen,
3531 disp->BlobCacheGet);
3535 dri2_client_wait_sync(_EGLDisplay *disp, _EGLSync *sync,
3536 EGLint flags, EGLTime timeout)
3538 _EGLContext *ctx = _eglGetCurrentContext();
3539 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3540 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3541 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3542 unsigned wait_flags = 0;
3544 EGLint ret = EGL_CONDITION_SATISFIED_KHR;
3546 /* The EGL_KHR_fence_sync spec states:
3548 * "If no context is current for the bound API,
3549 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
3551 if (dri2_ctx && flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)
3552 wait_flags |= __DRI2_FENCE_FLAG_FLUSH_COMMANDS;
3554 /* the sync object should take a reference while waiting */
3555 dri2_egl_ref_sync(dri2_sync);
3557 switch (sync->Type) {
3558 case EGL_SYNC_FENCE_KHR:
3559 case EGL_SYNC_NATIVE_FENCE_ANDROID:
3560 case EGL_SYNC_CL_EVENT_KHR:
3561 if (dri2_dpy->fence->client_wait_sync(dri2_ctx ? dri2_ctx->dri_context : NULL,
3562 dri2_sync->fence, wait_flags,
3564 dri2_sync->base.SyncStatus = EGL_SIGNALED_KHR;
3566 ret = EGL_TIMEOUT_EXPIRED_KHR;
3569 case EGL_SYNC_REUSABLE_KHR:
3570 if (dri2_ctx && dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR &&
3571 (flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)) {
3572 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
3576 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
3577 if (timeout == EGL_FOREVER_KHR) {
3578 mtx_lock(&dri2_sync->mutex);
3579 cnd_wait(&dri2_sync->cond, &dri2_sync->mutex);
3580 mtx_unlock(&dri2_sync->mutex);
3582 /* if reusable sync has not been yet signaled */
3583 if (dri2_sync->base.SyncStatus != EGL_SIGNALED_KHR) {
3584 /* timespecs for cnd_timedwait */
3585 struct timespec current;
3586 struct timespec expire;
3588 /* We override the clock to monotonic when creating the condition
3590 clock_gettime(CLOCK_MONOTONIC, ¤t);
3592 /* calculating when to expire */
3593 expire.tv_nsec = timeout % 1000000000L;
3594 expire.tv_sec = timeout / 1000000000L;
3596 expire.tv_nsec += current.tv_nsec;
3597 expire.tv_sec += current.tv_sec;
3599 /* expire.nsec now is a number between 0 and 1999999998 */
3600 if (expire.tv_nsec > 999999999L) {
3602 expire.tv_nsec -= 1000000000L;
3605 mtx_lock(&dri2_sync->mutex);
3606 ret = cnd_timedwait(&dri2_sync->cond, &dri2_sync->mutex, &expire);
3607 mtx_unlock(&dri2_sync->mutex);
3609 if (ret == thrd_timedout) {
3610 if (dri2_sync->base.SyncStatus == EGL_UNSIGNALED_KHR) {
3611 ret = EGL_TIMEOUT_EXPIRED_KHR;
3613 _eglError(EGL_BAD_ACCESS, "eglClientWaitSyncKHR");
3621 dri2_egl_unref_sync(dri2_dpy, dri2_sync);
3627 dri2_signal_sync(_EGLDisplay *disp, _EGLSync *sync, EGLenum mode)
3629 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3632 if (sync->Type != EGL_SYNC_REUSABLE_KHR)
3633 return _eglError(EGL_BAD_MATCH, "eglSignalSyncKHR");
3635 if (mode != EGL_SIGNALED_KHR && mode != EGL_UNSIGNALED_KHR)
3636 return _eglError(EGL_BAD_ATTRIBUTE, "eglSignalSyncKHR");
3638 dri2_sync->base.SyncStatus = mode;
3640 if (mode == EGL_SIGNALED_KHR) {
3641 ret = cnd_broadcast(&dri2_sync->cond);
3643 /* fail to broadcast */
3645 return _eglError(EGL_BAD_ACCESS, "eglSignalSyncKHR");
3652 dri2_server_wait_sync(_EGLDisplay *disp, _EGLSync *sync)
3654 _EGLContext *ctx = _eglGetCurrentContext();
3655 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3656 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3657 struct dri2_egl_sync *dri2_sync = dri2_egl_sync(sync);
3659 dri2_dpy->fence->server_wait_sync(dri2_ctx->dri_context,
3660 dri2_sync->fence, 0);
3665 dri2_interop_query_device_info(_EGLDisplay *disp, _EGLContext *ctx,
3666 struct mesa_glinterop_device_info *out)
3668 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3669 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3671 if (!dri2_dpy->interop)
3672 return MESA_GLINTEROP_UNSUPPORTED;
3674 return dri2_dpy->interop->query_device_info(dri2_ctx->dri_context, out);
3678 dri2_interop_export_object(_EGLDisplay *disp, _EGLContext *ctx,
3679 struct mesa_glinterop_export_in *in,
3680 struct mesa_glinterop_export_out *out)
3682 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3683 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
3685 if (!dri2_dpy->interop)
3686 return MESA_GLINTEROP_UNSUPPORTED;
3688 return dri2_dpy->interop->export_object(dri2_ctx->dri_context, in, out);
3691 const _EGLDriver _eglDriver = {
3692 .Initialize = dri2_initialize,
3693 .Terminate = dri2_terminate,
3694 .CreateContext = dri2_create_context,
3695 .DestroyContext = dri2_destroy_context,
3696 .MakeCurrent = dri2_make_current,
3697 .CreateWindowSurface = dri2_create_window_surface,
3698 .CreatePixmapSurface = dri2_create_pixmap_surface,
3699 .CreatePbufferSurface = dri2_create_pbuffer_surface,
3700 .DestroySurface = dri2_destroy_surface,
3701 .GetProcAddress = dri2_get_proc_address,
3702 .WaitClient = dri2_wait_client,
3703 .WaitNative = dri2_wait_native,
3704 .BindTexImage = dri2_bind_tex_image,
3705 .ReleaseTexImage = dri2_release_tex_image,
3706 .SwapInterval = dri2_swap_interval,
3707 .SwapBuffers = dri2_swap_buffers,
3708 .SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage,
3709 .SwapBuffersRegionNOK = dri2_swap_buffers_region,
3710 .SetDamageRegion = dri2_set_damage_region,
3711 .PostSubBufferNV = dri2_post_sub_buffer,
3712 .CopyBuffers = dri2_copy_buffers,
3713 .QueryBufferAge = dri2_query_buffer_age,
3714 .CreateImageKHR = dri2_create_image,
3715 .DestroyImageKHR = dri2_destroy_image_khr,
3716 .CreateWaylandBufferFromImageWL = dri2_create_wayland_buffer_from_image,
3717 .QuerySurface = dri2_query_surface,
3718 .QueryDriverName = dri2_query_driver_name,
3719 .QueryDriverConfig = dri2_query_driver_config,
3721 .CreateDRMImageMESA = dri2_create_drm_image_mesa,
3722 .ExportDRMImageMESA = dri2_export_drm_image_mesa,
3723 .ExportDMABUFImageQueryMESA = dri2_export_dma_buf_image_query_mesa,
3724 .ExportDMABUFImageMESA = dri2_export_dma_buf_image_mesa,
3725 .QueryDmaBufFormatsEXT = dri2_query_dma_buf_formats,
3726 .QueryDmaBufModifiersEXT = dri2_query_dma_buf_modifiers,
3728 #ifdef HAVE_WAYLAND_PLATFORM
3729 .BindWaylandDisplayWL = dri2_bind_wayland_display_wl,
3730 .UnbindWaylandDisplayWL = dri2_unbind_wayland_display_wl,
3731 .QueryWaylandBufferWL = dri2_query_wayland_buffer_wl,
3733 .GetSyncValuesCHROMIUM = dri2_get_sync_values_chromium,
3734 .CreateSyncKHR = dri2_create_sync,
3735 .ClientWaitSyncKHR = dri2_client_wait_sync,
3736 .SignalSyncKHR = dri2_signal_sync,
3737 .WaitSyncKHR = dri2_server_wait_sync,
3738 .DestroySyncKHR = dri2_destroy_sync,
3739 .GLInteropQueryDeviceInfo = dri2_interop_query_device_info,
3740 .GLInteropExportObject = dri2_interop_export_object,
3741 .DupNativeFenceFDANDROID = dri2_dup_native_fence_fd,
3742 .SetBlobCacheFuncsANDROID = dri2_set_blob_cache_funcs,