2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Kristian Høgsberg <krh@bitplanet.net>
39 #include <c11/threads.h>
43 #include "drm-uapi/drm_fourcc.h"
46 #include <GL/internal/dri_interface.h>
47 #include <sys/types.h>
50 #ifdef HAVE_WAYLAND_PLATFORM
51 #include <wayland-client.h>
52 #include "wayland-drm.h"
53 #include "wayland-drm-client-protocol.h"
54 #include "linux-dmabuf-unstable-v1-client-protocol.h"
57 #ifdef HAVE_X11_PLATFORM
58 #include "X11/Xlibint.h"
61 #include "egldefines.h"
63 #include "GL/mesa_glinterop.h"
64 #include "loader/loader.h"
65 #include "util/u_atomic.h"
66 #include "util/u_vector.h"
67 #include "mapi/glapi/glapi.h"
69 /* Additional definitions not yet in the drm_fourcc.h.
71 #ifndef DRM_FORMAT_P010
72 #define DRM_FORMAT_P010 fourcc_code('P', '0', '1', '0') /* 2x2 subsampled Cb:Cr plane 10 bits per channel */
75 #ifndef DRM_FORMAT_P012
76 #define DRM_FORMAT_P012 fourcc_code('P', '0', '1', '2') /* 2x2 subsampled Cb:Cr plane 12 bits per channel */
79 #ifndef DRM_FORMAT_P016
80 #define DRM_FORMAT_P016 fourcc_code('P', '0', '1', '6') /* 2x2 subsampled Cb:Cr plane 16 bits per channel */
83 #define NUM_ATTRIBS 12
86 dri_set_background_context(void *loaderPrivate
)
88 _EGLContext
*ctx
= _eglGetCurrentContext();
89 _EGLThreadInfo
*t
= _eglGetCurrentThread();
91 _eglBindContextToThread(ctx
, t
);
97 static void (*glFlush
)(void);
98 static mtx_t glFlushMutex
= _MTX_INITIALIZER_NP
;
100 mtx_lock(&glFlushMutex
);
102 glFlush
= _glapi_get_proc_address("glFlush");
103 mtx_unlock(&glFlushMutex
);
105 /* if glFlush is not available things are horribly broken */
107 _eglLog(_EGL_WARNING
, "DRI2: failed to find glFlush entry point");
115 dri_is_thread_safe(void *loaderPrivate
)
117 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
118 UNUSED _EGLDisplay
*display
= dri2_surf
->base
.Resource
.Display
;
120 #ifdef HAVE_X11_PLATFORM
121 Display
*xdpy
= (Display
*)display
->PlatformDisplay
;
123 /* Check Xlib is running in thread safe mode when running on EGL/X11-xlib
126 * 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
127 * It wll be NULL if XInitThreads wasn't called.
129 if (display
->Platform
== _EGL_PLATFORM_X11
&& xdpy
&& !xdpy
->lock_fns
)
133 #ifdef HAVE_WAYLAND_PLATFORM
134 if (display
->Platform
== _EGL_PLATFORM_WAYLAND
)
141 const __DRIbackgroundCallableExtension background_callable_extension
= {
142 .base
= { __DRI_BACKGROUND_CALLABLE
, 2 },
144 .setBackgroundContext
= dri_set_background_context
,
145 .isThreadSafe
= dri_is_thread_safe
,
148 const __DRIuseInvalidateExtension use_invalidate
= {
149 .base
= { __DRI_USE_INVALIDATE
, 1 }
153 dri2_get_pbuffer_drawable_info(__DRIdrawable
* draw
,
154 int *x
, int *y
, int *w
, int *h
,
157 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
160 *w
= dri2_surf
->base
.Width
;
161 *h
= dri2_surf
->base
.Height
;
164 /* HACK: technically we should have swrast_null, instead of these. We
165 * get away since only pbuffers are supported, thus the callbacks are
168 const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension
= {
169 .base
= { __DRI_SWRAST_LOADER
, 1 },
170 .getDrawableInfo
= dri2_get_pbuffer_drawable_info
,
175 static const EGLint dri2_to_egl_attribute_map
[__DRI_ATTRIB_MAX
] = {
176 [__DRI_ATTRIB_BUFFER_SIZE
] = EGL_BUFFER_SIZE
,
177 [__DRI_ATTRIB_LEVEL
] = EGL_LEVEL
,
178 [__DRI_ATTRIB_LUMINANCE_SIZE
] = EGL_LUMINANCE_SIZE
,
179 [__DRI_ATTRIB_DEPTH_SIZE
] = EGL_DEPTH_SIZE
,
180 [__DRI_ATTRIB_STENCIL_SIZE
] = EGL_STENCIL_SIZE
,
181 [__DRI_ATTRIB_SAMPLE_BUFFERS
] = EGL_SAMPLE_BUFFERS
,
182 [__DRI_ATTRIB_SAMPLES
] = EGL_SAMPLES
,
183 [__DRI_ATTRIB_MAX_PBUFFER_WIDTH
] = EGL_MAX_PBUFFER_WIDTH
,
184 [__DRI_ATTRIB_MAX_PBUFFER_HEIGHT
] = EGL_MAX_PBUFFER_HEIGHT
,
185 [__DRI_ATTRIB_MAX_PBUFFER_PIXELS
] = EGL_MAX_PBUFFER_PIXELS
,
186 [__DRI_ATTRIB_MAX_SWAP_INTERVAL
] = EGL_MAX_SWAP_INTERVAL
,
187 [__DRI_ATTRIB_MIN_SWAP_INTERVAL
] = EGL_MIN_SWAP_INTERVAL
,
188 [__DRI_ATTRIB_YINVERTED
] = EGL_Y_INVERTED_NOK
,
192 dri2_get_dri_config(struct dri2_egl_config
*conf
, EGLint surface_type
,
195 const bool double_buffer
= surface_type
== EGL_WINDOW_BIT
;
196 const bool srgb
= colorspace
== EGL_GL_COLORSPACE_SRGB_KHR
;
198 return conf
->dri_config
[double_buffer
][srgb
];
202 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
204 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
207 if (!_eglMatchConfig(conf
, criteria
))
214 dri2_get_shifts_and_sizes(const __DRIcoreExtension
*core
,
215 const __DRIconfig
*config
, int *shifts
,
220 if (core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_SHIFT
, (unsigned int *)&shifts
[0])) {
221 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_SHIFT
, (unsigned int *)&shifts
[1]);
222 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_SHIFT
, (unsigned int *)&shifts
[2]);
223 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_SHIFT
, (unsigned int *)&shifts
[3]);
225 /* Driver isn't exposing shifts, so convert masks to shifts */
226 core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_MASK
, &mask
);
227 shifts
[0] = ffs(mask
) - 1;
228 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_MASK
, &mask
);
229 shifts
[1] = ffs(mask
) - 1;
230 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_MASK
, &mask
);
231 shifts
[2] = ffs(mask
) - 1;
232 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_MASK
, &mask
);
233 shifts
[3] = ffs(mask
) - 1;
236 core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_SIZE
, &sizes
[0]);
237 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_SIZE
, &sizes
[1]);
238 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_SIZE
, &sizes
[2]);
239 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_SIZE
, &sizes
[3]);
243 dri2_get_render_type_float(const __DRIcoreExtension
*core
,
244 const __DRIconfig
*config
,
247 unsigned int render_type
;
249 core
->getConfigAttrib(config
, __DRI_ATTRIB_RENDER_TYPE
, &render_type
);
250 *is_float
= (render_type
& __DRI_ATTRIB_FLOAT_BIT
) ? true : false;
253 struct dri2_egl_config
*
254 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
255 EGLint surface_type
, const EGLint
*attr_list
,
256 const int *rgba_shifts
, const unsigned int *rgba_sizes
)
258 struct dri2_egl_config
*conf
;
259 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
261 unsigned int attrib
, value
, double_buffer
;
263 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
264 int dri_shifts
[4] = { -1, -1, -1, -1 };
265 unsigned int dri_sizes
[4] = { 0, 0, 0, 0 };
266 _EGLConfig
*matching_config
;
267 EGLint num_configs
= 0;
270 _eglInitConfig(&base
, disp
, id
);
273 bind_to_texture_rgb
= 0;
274 bind_to_texture_rgba
= 0;
276 for (int i
= 0; i
< __DRI_ATTRIB_MAX
; ++i
) {
277 if (!dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
, &attrib
, &value
))
281 case __DRI_ATTRIB_RENDER_TYPE
:
282 if (value
& __DRI_ATTRIB_FLOAT_BIT
)
283 _eglSetConfigKey(&base
, EGL_COLOR_COMPONENT_TYPE_EXT
,
284 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT
);
285 if (value
& __DRI_ATTRIB_RGBA_BIT
)
286 value
= EGL_RGB_BUFFER
;
287 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
288 value
= EGL_LUMINANCE_BUFFER
;
291 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
294 case __DRI_ATTRIB_CONFIG_CAVEAT
:
295 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
296 value
= EGL_NON_CONFORMANT_CONFIG
;
297 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
298 value
= EGL_SLOW_CONFIG
;
301 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
304 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
305 bind_to_texture_rgb
= value
;
308 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
309 bind_to_texture_rgba
= value
;
312 case __DRI_ATTRIB_DOUBLE_BUFFER
:
313 double_buffer
= value
;
316 case __DRI_ATTRIB_RED_SIZE
:
317 dri_sizes
[0] = value
;
318 _eglSetConfigKey(&base
, EGL_RED_SIZE
, value
);
321 case __DRI_ATTRIB_RED_MASK
:
322 dri_shifts
[0] = ffs(value
) - 1;
325 case __DRI_ATTRIB_RED_SHIFT
:
326 dri_shifts
[0] = value
;
329 case __DRI_ATTRIB_GREEN_SIZE
:
330 dri_sizes
[1] = value
;
331 _eglSetConfigKey(&base
, EGL_GREEN_SIZE
, value
);
334 case __DRI_ATTRIB_GREEN_MASK
:
335 dri_shifts
[1] = ffs(value
) - 1;
338 case __DRI_ATTRIB_GREEN_SHIFT
:
339 dri_shifts
[1] = value
;
342 case __DRI_ATTRIB_BLUE_SIZE
:
343 dri_sizes
[2] = value
;
344 _eglSetConfigKey(&base
, EGL_BLUE_SIZE
, value
);
347 case __DRI_ATTRIB_BLUE_MASK
:
348 dri_shifts
[2] = ffs(value
) - 1;
351 case __DRI_ATTRIB_BLUE_SHIFT
:
352 dri_shifts
[2] = value
;
355 case __DRI_ATTRIB_ALPHA_SIZE
:
356 dri_sizes
[3] = value
;
357 _eglSetConfigKey(&base
, EGL_ALPHA_SIZE
, value
);
360 case __DRI_ATTRIB_ALPHA_MASK
:
361 dri_shifts
[3] = ffs(value
) - 1;
364 case __DRI_ATTRIB_ALPHA_SHIFT
:
365 dri_shifts
[3] = value
;
368 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
369 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
370 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
371 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
372 /* Don't expose visuals with the accumulation buffer. */
377 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
379 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
383 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH
:
384 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_WIDTH
,
385 _EGL_MAX_PBUFFER_WIDTH
);
387 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT
:
388 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_HEIGHT
,
389 _EGL_MAX_PBUFFER_HEIGHT
);
391 case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER
:
392 if (disp
->Extensions
.KHR_mutable_render_buffer
)
393 surface_type
|= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR
;
396 key
= dri2_to_egl_attribute_map
[attrib
];
398 _eglSetConfigKey(&base
, key
, value
);
404 for (int i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
405 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
407 if (rgba_shifts
&& memcmp(rgba_shifts
, dri_shifts
, sizeof(dri_shifts
)))
410 if (rgba_sizes
&& memcmp(rgba_sizes
, dri_sizes
, sizeof(dri_sizes
)))
413 base
.NativeRenderable
= EGL_TRUE
;
415 base
.SurfaceType
= surface_type
;
416 if (surface_type
& (EGL_PBUFFER_BIT
|
417 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
418 base
.BindToTextureRGB
= bind_to_texture_rgb
;
419 if (base
.AlphaSize
> 0)
420 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
423 base
.RenderableType
= disp
->ClientAPIs
;
424 base
.Conformant
= disp
->ClientAPIs
;
426 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
427 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
429 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
430 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
434 config_id
= base
.ConfigID
;
435 base
.ConfigID
= EGL_DONT_CARE
;
436 base
.SurfaceType
= EGL_DONT_CARE
;
437 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
438 (_EGLArrayForEach
) dri2_match_config
, &base
);
440 if (num_configs
== 1) {
441 conf
= (struct dri2_egl_config
*) matching_config
;
443 if (!conf
->dri_config
[double_buffer
][srgb
])
444 conf
->dri_config
[double_buffer
][srgb
] = dri_config
;
446 /* a similar config type is already added (unlikely) => discard */
449 else if (num_configs
== 0) {
450 conf
= calloc(1, sizeof *conf
);
454 conf
->dri_config
[double_buffer
][srgb
] = dri_config
;
456 memcpy(&conf
->base
, &base
, sizeof base
);
457 conf
->base
.SurfaceType
= 0;
458 conf
->base
.ConfigID
= config_id
;
460 _eglLinkConfig(&conf
->base
);
463 unreachable("duplicates should not be possible");
468 surface_type
&= ~EGL_PIXMAP_BIT
;
471 /* No support for pbuffer + MSAA for now.
473 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
474 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
477 surface_type
&= ~EGL_PBUFFER_BIT
;
480 conf
->base
.SurfaceType
|= surface_type
;
486 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
488 _EGLDisplay
*disp
= data
;
489 struct dri2_egl_image
*dri2_img
;
494 img
= _eglLookupImage(image
, disp
);
496 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
500 dri2_img
= dri2_egl_image(image
);
502 return dri2_img
->dri_image
;
505 const __DRIimageLookupExtension image_lookup_extension
= {
506 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
508 .lookupEGLImage
= dri2_lookup_egl_image
511 struct dri2_extension_match
{
517 static const struct dri2_extension_match dri3_driver_extensions
[] = {
518 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
519 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
523 static const struct dri2_extension_match dri2_driver_extensions
[] = {
524 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
525 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
529 static const struct dri2_extension_match dri2_core_extensions
[] = {
530 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
531 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
532 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
536 static const struct dri2_extension_match swrast_driver_extensions
[] = {
537 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
538 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
542 static const struct dri2_extension_match swrast_core_extensions
[] = {
543 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
547 static const struct dri2_extension_match optional_driver_extensions
[] = {
548 { __DRI_CONFIG_OPTIONS
, 1, offsetof(struct dri2_egl_display
, configOptions
) },
552 static const struct dri2_extension_match optional_core_extensions
[] = {
553 { __DRI2_ROBUSTNESS
, 1, offsetof(struct dri2_egl_display
, robustness
) },
554 { __DRI2_NO_ERROR
, 1, offsetof(struct dri2_egl_display
, no_error
) },
555 { __DRI2_CONFIG_QUERY
, 1, offsetof(struct dri2_egl_display
, config
) },
556 { __DRI2_FENCE
, 1, offsetof(struct dri2_egl_display
, fence
) },
557 { __DRI2_BUFFER_DAMAGE
, 1, offsetof(struct dri2_egl_display
, buffer_damage
) },
558 { __DRI2_RENDERER_QUERY
, 1, offsetof(struct dri2_egl_display
, rendererQuery
) },
559 { __DRI2_INTEROP
, 1, offsetof(struct dri2_egl_display
, interop
) },
560 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
561 { __DRI2_FLUSH_CONTROL
, 1, offsetof(struct dri2_egl_display
, flush_control
) },
562 { __DRI2_BLOB
, 1, offsetof(struct dri2_egl_display
, blob
) },
563 { __DRI_MUTABLE_RENDER_BUFFER_DRIVER
, 1, offsetof(struct dri2_egl_display
, mutable_render_buffer
) },
568 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
569 const struct dri2_extension_match
*matches
,
570 const __DRIextension
**extensions
,
576 for (int i
= 0; extensions
[i
]; i
++) {
577 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
578 for (int j
= 0; matches
[j
].name
; j
++) {
579 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
580 extensions
[i
]->version
>= matches
[j
].version
) {
581 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
582 *(const __DRIextension
**) field
= extensions
[i
];
583 _eglLog(_EGL_INFO
, "found extension %s version %d",
584 extensions
[i
]->name
, extensions
[i
]->version
);
590 for (int j
= 0; matches
[j
].name
; j
++) {
591 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
592 if (*(const __DRIextension
**) field
== NULL
) {
594 _eglLog(_EGL_DEBUG
, "did not find optional extension %s version %d",
595 matches
[j
].name
, matches
[j
].version
);
597 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
598 matches
[j
].name
, matches
[j
].version
);
607 static const __DRIextension
**
608 dri2_open_driver(_EGLDisplay
*disp
)
610 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
611 static const char *search_path_vars
[] = {
612 "LIBGL_DRIVERS_PATH",
616 return loader_open_driver(dri2_dpy
->driver_name
,
622 dri2_load_driver_common(_EGLDisplay
*disp
,
623 const struct dri2_extension_match
*driver_extensions
)
625 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
626 const __DRIextension
**extensions
;
628 extensions
= dri2_open_driver(disp
);
632 if (!dri2_bind_extensions(dri2_dpy
, driver_extensions
, extensions
, false)) {
633 dlclose(dri2_dpy
->driver
);
636 dri2_dpy
->driver_extensions
= extensions
;
638 dri2_bind_extensions(dri2_dpy
, optional_driver_extensions
, extensions
, true);
644 dri2_load_driver(_EGLDisplay
*disp
)
646 return dri2_load_driver_common(disp
, dri2_driver_extensions
);
650 dri2_load_driver_dri3(_EGLDisplay
*disp
)
652 return dri2_load_driver_common(disp
, dri3_driver_extensions
);
656 dri2_load_driver_swrast(_EGLDisplay
*disp
)
658 return dri2_load_driver_common(disp
, swrast_driver_extensions
);
662 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
664 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
665 unsigned int value
= 0;
667 if (!rendererQuery
||
668 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
675 dri2_query_driver_name(_EGLDisplay
*disp
)
677 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
678 return dri2_dpy
->driver_name
;
682 dri2_query_driver_config(_EGLDisplay
*disp
)
684 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
685 const __DRIconfigOptionsExtension
*ext
= dri2_dpy
->configOptions
;
687 if (ext
->base
.version
>= 2)
688 return ext
->getXml(dri2_dpy
->driver_name
);
690 return strdup(ext
->xml
);
695 dri2_setup_screen(_EGLDisplay
*disp
)
697 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
698 unsigned int api_mask
;
701 * EGL 1.5 specification defines the default value to 1. Moreover,
702 * eglSwapInterval() is required to clamp requested value to the supported
703 * range. Since the default value is implicitly assumed to be supported,
704 * use it as both minimum and maximum for the platforms that do not allow
705 * changing the interval. Platforms, which allow it (e.g. x11, wayland)
706 * override these values already.
708 dri2_dpy
->min_swap_interval
= 1;
709 dri2_dpy
->max_swap_interval
= 1;
710 dri2_dpy
->default_swap_interval
= 1;
712 if (dri2_dpy
->image_driver
) {
713 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
714 } else if (dri2_dpy
->dri2
) {
715 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
717 assert(dri2_dpy
->swrast
);
718 api_mask
= 1 << __DRI_API_OPENGL
|
719 1 << __DRI_API_GLES
|
720 1 << __DRI_API_GLES2
|
721 1 << __DRI_API_GLES3
;
724 disp
->ClientAPIs
= 0;
725 if ((api_mask
& (1 <<__DRI_API_OPENGL
)) && _eglIsApiValid(EGL_OPENGL_API
))
726 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
727 if ((api_mask
& (1 << __DRI_API_GLES
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
728 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
729 if ((api_mask
& (1 << __DRI_API_GLES2
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
730 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
731 if ((api_mask
& (1 << __DRI_API_GLES3
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
732 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
734 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
735 disp
->Extensions
.KHR_no_config_context
= EGL_TRUE
;
736 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
738 if (dri2_dpy
->configOptions
) {
739 disp
->Extensions
.MESA_query_driver
= EGL_TRUE
;
742 /* Report back to EGL the bitmask of priorities supported */
743 disp
->Extensions
.IMG_context_priority
=
744 dri2_renderer_query_integer(dri2_dpy
,
745 __DRI2_RENDERER_HAS_CONTEXT_PRIORITY
);
747 disp
->Extensions
.EXT_pixel_format_float
= EGL_TRUE
;
749 if (dri2_renderer_query_integer(dri2_dpy
,
750 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
751 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
753 if (dri2_dpy
->image_driver
||
754 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
755 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
756 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
758 if (dri2_dpy
->robustness
)
759 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
762 if (dri2_dpy
->no_error
)
763 disp
->Extensions
.KHR_create_context_no_error
= EGL_TRUE
;
765 if (dri2_dpy
->fence
) {
766 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
767 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
768 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
769 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
770 if (dri2_dpy
->fence
->base
.version
>= 2 &&
771 dri2_dpy
->fence
->get_capabilities
) {
772 unsigned capabilities
=
773 dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
);
774 disp
->Extensions
.ANDROID_native_fence_sync
=
775 (capabilities
& __DRI_FENCE_CAP_NATIVE_FD
) != 0;
780 disp
->Extensions
.ANDROID_blob_cache
= EGL_TRUE
;
782 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
784 if (dri2_dpy
->image
) {
785 if (dri2_dpy
->image
->base
.version
>= 10 &&
786 dri2_dpy
->image
->getCapabilities
!= NULL
) {
789 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
790 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
792 if (dri2_dpy
->image
->base
.version
>= 11)
793 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
795 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
796 if (dri2_dpy
->image
->base
.version
>= 11)
797 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
800 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
801 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
802 if (dri2_dpy
->image
->base
.version
>= 5 &&
803 dri2_dpy
->image
->createImageFromTexture
) {
804 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
805 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
807 if (dri2_renderer_query_integer(dri2_dpy
,
808 __DRI2_RENDERER_HAS_TEXTURE_3D
))
809 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
812 if (dri2_dpy
->image
->base
.version
>= 8 &&
813 dri2_dpy
->image
->createImageFromDmaBufs
) {
814 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
816 if (dri2_dpy
->image
->base
.version
>= 15 &&
817 dri2_dpy
->image
->createImageFromDmaBufs2
&&
818 dri2_dpy
->image
->queryDmaBufFormats
&&
819 dri2_dpy
->image
->queryDmaBufModifiers
) {
820 disp
->Extensions
.EXT_image_dma_buf_import_modifiers
= EGL_TRUE
;
825 if (dri2_dpy
->flush_control
)
826 disp
->Extensions
.KHR_context_flush_control
= EGL_TRUE
;
828 if (dri2_dpy
->buffer_damage
&& dri2_dpy
->buffer_damage
->set_damage_region
)
829 disp
->Extensions
.KHR_partial_update
= EGL_TRUE
;
833 dri2_setup_swap_interval(_EGLDisplay
*disp
, int max_swap_interval
)
835 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
836 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
838 /* Allow driconf to override applications.*/
839 if (dri2_dpy
->config
)
840 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
841 "vblank_mode", &vblank_mode
);
842 switch (vblank_mode
) {
843 case DRI_CONF_VBLANK_NEVER
:
844 dri2_dpy
->min_swap_interval
= 0;
845 dri2_dpy
->max_swap_interval
= 0;
846 dri2_dpy
->default_swap_interval
= 0;
848 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
849 dri2_dpy
->min_swap_interval
= 1;
850 dri2_dpy
->max_swap_interval
= max_swap_interval
;
851 dri2_dpy
->default_swap_interval
= 1;
853 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
854 dri2_dpy
->min_swap_interval
= 0;
855 dri2_dpy
->max_swap_interval
= max_swap_interval
;
856 dri2_dpy
->default_swap_interval
= 0;
859 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
860 dri2_dpy
->min_swap_interval
= 0;
861 dri2_dpy
->max_swap_interval
= max_swap_interval
;
862 dri2_dpy
->default_swap_interval
= 1;
867 /* All platforms but DRM call this function to create the screen and populate
868 * the driver_configs. DRM inherits that information from its display - GBM.
871 dri2_create_screen(_EGLDisplay
*disp
)
873 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
875 if (dri2_dpy
->image_driver
) {
876 dri2_dpy
->dri_screen
=
877 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
878 dri2_dpy
->loader_extensions
,
879 dri2_dpy
->driver_extensions
,
880 &dri2_dpy
->driver_configs
,
882 } else if (dri2_dpy
->dri2
) {
883 if (dri2_dpy
->dri2
->base
.version
>= 4) {
884 dri2_dpy
->dri_screen
=
885 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
886 dri2_dpy
->loader_extensions
,
887 dri2_dpy
->driver_extensions
,
888 &dri2_dpy
->driver_configs
, disp
);
890 dri2_dpy
->dri_screen
=
891 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
892 dri2_dpy
->loader_extensions
,
893 &dri2_dpy
->driver_configs
, disp
);
896 assert(dri2_dpy
->swrast
);
897 if (dri2_dpy
->swrast
->base
.version
>= 4) {
898 dri2_dpy
->dri_screen
=
899 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->loader_extensions
,
900 dri2_dpy
->driver_extensions
,
901 &dri2_dpy
->driver_configs
, disp
);
903 dri2_dpy
->dri_screen
=
904 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->loader_extensions
,
905 &dri2_dpy
->driver_configs
, disp
);
909 if (dri2_dpy
->dri_screen
== NULL
) {
910 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
914 dri2_dpy
->own_dri_screen
= true;
919 dri2_setup_extensions(_EGLDisplay
*disp
)
921 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
922 const struct dri2_extension_match
*mandatory_core_extensions
;
923 const __DRIextension
**extensions
;
925 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
927 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
)
928 mandatory_core_extensions
= dri2_core_extensions
;
930 mandatory_core_extensions
= swrast_core_extensions
;
932 if (!dri2_bind_extensions(dri2_dpy
, mandatory_core_extensions
, extensions
, false))
935 #ifdef HAVE_DRI3_MODIFIERS
936 dri2_dpy
->multibuffers_available
=
937 (dri2_dpy
->dri3_major_version
> 1 || (dri2_dpy
->dri3_major_version
== 1 &&
938 dri2_dpy
->dri3_minor_version
>= 2)) &&
939 (dri2_dpy
->present_major_version
> 1 || (dri2_dpy
->present_major_version
== 1 &&
940 dri2_dpy
->present_minor_version
>= 2)) &&
941 (dri2_dpy
->image
&& dri2_dpy
->image
->base
.version
>= 15);
944 dri2_bind_extensions(dri2_dpy
, optional_core_extensions
, extensions
, true);
949 * Called via eglInitialize(), GLX_drv->API.Initialize().
951 * This must be guaranteed to be called exactly once, even if eglInitialize is
952 * called many times (without a eglTerminate in between).
955 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
957 EGLBoolean ret
= EGL_FALSE
;
958 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
960 /* In the case where the application calls eglMakeCurrent(context1),
961 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
962 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
963 * initialized, as we need it to be able to free context1 correctly.
965 * It would probably be safest to forcibly release the display with
966 * dri2_display_release, to make sure the display is reinitialized correctly.
967 * However, the EGL spec states that we need to keep a reference to the
968 * current context (so we cannot call dri2_make_current(NULL)), and therefore
969 * we would leak context1 as we would be missing the old display connection
970 * to free it up correctly.
973 dri2_dpy
->ref_count
++;
977 loader_set_logger(_eglLog
);
979 switch (disp
->Platform
) {
980 case _EGL_PLATFORM_SURFACELESS
:
981 ret
= dri2_initialize_surfaceless(drv
, disp
);
983 case _EGL_PLATFORM_DEVICE
:
984 ret
= dri2_initialize_device(drv
, disp
);
986 case _EGL_PLATFORM_X11
:
987 ret
= dri2_initialize_x11(drv
, disp
);
989 case _EGL_PLATFORM_DRM
:
990 ret
= dri2_initialize_drm(drv
, disp
);
992 case _EGL_PLATFORM_WAYLAND
:
993 ret
= dri2_initialize_wayland(drv
, disp
);
995 case _EGL_PLATFORM_ANDROID
:
996 ret
= dri2_initialize_android(drv
, disp
);
999 unreachable("Callers ensure we cannot get here.");
1006 dri2_dpy
= dri2_egl_display(disp
);
1007 dri2_dpy
->ref_count
++;
1013 * Decrement display reference count, and free up display if necessary.
1016 dri2_display_release(_EGLDisplay
*disp
)
1018 struct dri2_egl_display
*dri2_dpy
;
1023 dri2_dpy
= dri2_egl_display(disp
);
1025 assert(dri2_dpy
->ref_count
> 0);
1026 dri2_dpy
->ref_count
--;
1028 if (dri2_dpy
->ref_count
> 0)
1031 _eglCleanupDisplay(disp
);
1032 dri2_display_destroy(disp
);
1036 dri2_display_destroy(_EGLDisplay
*disp
)
1038 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1040 if (dri2_dpy
->own_dri_screen
) {
1041 if (dri2_dpy
->vtbl
&& dri2_dpy
->vtbl
->close_screen_notify
)
1042 dri2_dpy
->vtbl
->close_screen_notify(disp
);
1043 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1045 if (dri2_dpy
->fd
>= 0)
1046 close(dri2_dpy
->fd
);
1047 if (dri2_dpy
->driver
)
1048 dlclose(dri2_dpy
->driver
);
1049 free(dri2_dpy
->driver_name
);
1051 #ifdef HAVE_WAYLAND_PLATFORM
1052 free(dri2_dpy
->device_name
);
1055 switch (disp
->Platform
) {
1056 case _EGL_PLATFORM_X11
:
1057 dri2_teardown_x11(dri2_dpy
);
1059 case _EGL_PLATFORM_DRM
:
1060 dri2_teardown_drm(dri2_dpy
);
1062 case _EGL_PLATFORM_WAYLAND
:
1063 dri2_teardown_wayland(dri2_dpy
);
1066 /* TODO: add teardown for other platforms */
1070 /* The drm platform does not create the screen/driver_configs but reuses
1071 * the ones from the gbm device. As such the gbm itself is responsible
1074 if (disp
->Platform
!= _EGL_PLATFORM_DRM
&& dri2_dpy
->driver_configs
) {
1075 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
1076 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
1077 free(dri2_dpy
->driver_configs
);
1080 disp
->DriverData
= NULL
;
1084 dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface
*dri2_surf
,
1085 unsigned int att
, unsigned int format
)
1087 struct dri2_egl_display
*dri2_dpy
=
1088 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1090 if (att
>= ARRAY_SIZE(dri2_surf
->local_buffers
))
1093 if (!dri2_surf
->local_buffers
[att
]) {
1094 dri2_surf
->local_buffers
[att
] =
1095 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
, att
, format
,
1096 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
1099 return dri2_surf
->local_buffers
[att
];
1103 dri2_egl_surface_free_local_buffers(struct dri2_egl_surface
*dri2_surf
)
1105 struct dri2_egl_display
*dri2_dpy
=
1106 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1108 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->local_buffers
); i
++) {
1109 if (dri2_surf
->local_buffers
[i
]) {
1110 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
1111 dri2_surf
->local_buffers
[i
]);
1112 dri2_surf
->local_buffers
[i
] = NULL
;
1118 * Called via eglTerminate(), drv->API.Terminate().
1120 * This must be guaranteed to be called exactly once, even if eglTerminate is
1121 * called many times (without a eglInitialize in between).
1124 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1126 /* Release all non-current Context/Surfaces. */
1127 _eglReleaseDisplayResources(drv
, disp
);
1129 dri2_display_release(disp
);
1135 * Set the error code after a call to
1136 * dri2_egl_display::dri2::createContextAttribs.
1139 dri2_create_context_attribs_error(int dri_error
)
1143 switch (dri_error
) {
1144 case __DRI_CTX_ERROR_SUCCESS
:
1147 case __DRI_CTX_ERROR_NO_MEMORY
:
1148 egl_error
= EGL_BAD_ALLOC
;
1151 /* From the EGL_KHR_create_context spec, section "Errors":
1153 * * If <config> does not support a client API context compatible
1154 * with the requested API major and minor version, [...] context flags,
1155 * and context reset notification behavior (for client API types where
1156 * these attributes are supported), then an EGL_BAD_MATCH error is
1159 * * If an OpenGL ES context is requested and the values for
1160 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1161 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1162 * is not defined, than an EGL_BAD_MATCH error is generated.
1164 * * If an OpenGL context is requested, the requested version is
1165 * greater than 3.2, and the value for attribute
1166 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1167 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1168 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1169 * one of these bits set; or if the implementation does not support
1170 * the requested profile, then an EGL_BAD_MATCH error is generated.
1172 case __DRI_CTX_ERROR_BAD_API
:
1173 case __DRI_CTX_ERROR_BAD_VERSION
:
1174 case __DRI_CTX_ERROR_BAD_FLAG
:
1175 egl_error
= EGL_BAD_MATCH
;
1178 /* From the EGL_KHR_create_context spec, section "Errors":
1180 * * If an attribute name or attribute value in <attrib_list> is not
1181 * recognized (including unrecognized bits in bitmask attributes),
1182 * then an EGL_BAD_ATTRIBUTE error is generated."
1184 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
1185 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
1186 egl_error
= EGL_BAD_ATTRIBUTE
;
1190 assert(!"unknown dri_error code");
1191 egl_error
= EGL_BAD_MATCH
;
1195 _eglError(egl_error
, "dri2_create_context");
1199 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1200 struct dri2_egl_display
*dri2_dpy
,
1201 uint32_t *ctx_attribs
,
1202 unsigned *num_attribs
)
1206 assert(*num_attribs
>= NUM_ATTRIBS
);
1208 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1209 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1210 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1211 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1213 if (dri2_ctx
->base
.Flags
!= 0 || dri2_ctx
->base
.NoError
) {
1214 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1215 * extension, don't even try to send it the robust-access flag.
1216 * It may explode. Instead, generate the required EGL error here.
1218 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1219 && !dri2_dpy
->robustness
) {
1220 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1224 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1225 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
|
1226 (dri2_ctx
->base
.NoError
? __DRI_CTX_FLAG_NO_ERROR
: 0);
1229 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1230 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1231 * extension, don't even try to send it a reset strategy. It may
1232 * explode. Instead, generate the required EGL error here.
1234 if (!dri2_dpy
->robustness
) {
1235 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1239 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1240 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1243 if (dri2_ctx
->base
.ContextPriority
!= EGL_CONTEXT_PRIORITY_MEDIUM_IMG
) {
1246 switch (dri2_ctx
->base
.ContextPriority
) {
1247 case EGL_CONTEXT_PRIORITY_HIGH_IMG
:
1248 val
= __DRI_CTX_PRIORITY_HIGH
;
1250 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG
:
1251 val
= __DRI_CTX_PRIORITY_MEDIUM
;
1253 case EGL_CONTEXT_PRIORITY_LOW_IMG
:
1254 val
= __DRI_CTX_PRIORITY_LOW
;
1257 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1261 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_PRIORITY
;
1262 ctx_attribs
[pos
++] = val
;
1265 if (dri2_ctx
->base
.ReleaseBehavior
== EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR
) {
1266 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR
;
1267 ctx_attribs
[pos
++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE
;
1276 * Called via eglCreateContext(), drv->API.CreateContext().
1278 static _EGLContext
*
1279 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1280 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1282 struct dri2_egl_context
*dri2_ctx
;
1283 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1284 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1285 __DRIcontext
*shared
=
1286 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1287 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1288 const __DRIconfig
*dri_config
;
1291 unsigned num_attribs
= NUM_ATTRIBS
;
1292 uint32_t ctx_attribs
[NUM_ATTRIBS
];
1296 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1298 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1302 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1305 /* The EGL_EXT_create_context_robustness spec says:
1307 * "Add to the eglCreateContext context creation errors: [...]
1309 * * If the reset notification behavior of <share_context> and the
1310 * newly created context are different then an EGL_BAD_MATCH error is
1313 if (share_list
&& share_list
->ResetNotificationStrategy
!=
1314 dri2_ctx
->base
.ResetNotificationStrategy
) {
1315 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1319 /* The EGL_KHR_create_context_no_error spec says:
1321 * "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
1322 * used to create <share_context> does not match the value of
1323 * EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
1325 if (share_list
&& share_list
->NoError
!= dri2_ctx
->base
.NoError
) {
1326 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1330 switch (dri2_ctx
->base
.ClientAPI
) {
1331 case EGL_OPENGL_ES_API
:
1332 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1334 api
= __DRI_API_GLES
;
1337 api
= __DRI_API_GLES2
;
1340 api
= __DRI_API_GLES3
;
1343 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1348 case EGL_OPENGL_API
:
1349 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1350 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1351 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1352 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1353 api
= __DRI_API_OPENGL_CORE
;
1354 else if (dri2_ctx
->base
.ClientMajorVersion
== 3 &&
1355 dri2_ctx
->base
.ClientMinorVersion
== 1)
1356 api
= __DRI_API_OPENGL_CORE
;
1358 api
= __DRI_API_OPENGL
;
1361 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1367 /* The config chosen here isn't necessarily
1368 * used for surfaces later.
1369 * A pixmap surface will use the single config.
1370 * This opportunity depends on disabling the
1371 * doubleBufferMode check in
1372 * src/mesa/main/context.c:check_compatible()
1374 if (dri2_config
->dri_config
[1][0])
1375 dri_config
= dri2_config
->dri_config
[1][0];
1377 dri_config
= dri2_config
->dri_config
[0][0];
1382 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1386 if (dri2_dpy
->image_driver
) {
1387 dri2_ctx
->dri_context
=
1388 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1396 dri2_create_context_attribs_error(error
);
1397 } else if (dri2_dpy
->dri2
) {
1398 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1399 dri2_ctx
->dri_context
=
1400 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1408 dri2_create_context_attribs_error(error
);
1410 dri2_ctx
->dri_context
=
1411 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1418 assert(dri2_dpy
->swrast
);
1419 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1420 dri2_ctx
->dri_context
=
1421 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1429 dri2_create_context_attribs_error(error
);
1431 dri2_ctx
->dri_context
=
1432 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1440 if (!dri2_ctx
->dri_context
)
1443 return &dri2_ctx
->base
;
1451 * Called via eglDestroyContext(), drv->API.DestroyContext().
1454 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1456 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1457 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1459 if (_eglPutContext(ctx
)) {
1460 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1468 dri2_init_surface(_EGLSurface
*surf
, _EGLDisplay
*disp
, EGLint type
,
1469 _EGLConfig
*conf
, const EGLint
*attrib_list
,
1470 EGLBoolean enable_out_fence
, void *native_surface
)
1472 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1473 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1475 dri2_surf
->out_fence_fd
= -1;
1476 dri2_surf
->enable_out_fence
= false;
1477 if (dri2_dpy
->fence
&& dri2_dpy
->fence
->base
.version
>= 2 &&
1478 dri2_dpy
->fence
->get_capabilities
&&
1479 (dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
) &
1480 __DRI_FENCE_CAP_NATIVE_FD
)) {
1481 dri2_surf
->enable_out_fence
= enable_out_fence
;
1484 return _eglInitSurface(surf
, disp
, type
, conf
, attrib_list
, native_surface
);
1488 dri2_surface_set_out_fence_fd( _EGLSurface
*surf
, int fence_fd
)
1490 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1492 if (dri2_surf
->out_fence_fd
>= 0)
1493 close(dri2_surf
->out_fence_fd
);
1495 dri2_surf
->out_fence_fd
= fence_fd
;
1499 dri2_fini_surface(_EGLSurface
*surf
)
1501 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1503 dri2_surface_set_out_fence_fd(surf
, -1);
1504 dri2_surf
->enable_out_fence
= false;
1508 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1510 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1512 if (!_eglPutSurface(surf
))
1515 return dri2_dpy
->vtbl
->destroy_surface(drv
, disp
, surf
);
1519 dri2_surf_update_fence_fd(_EGLContext
*ctx
,
1520 _EGLDisplay
*disp
, _EGLSurface
*surf
)
1522 __DRIcontext
*dri_ctx
= dri2_egl_context(ctx
)->dri_context
;
1523 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1524 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1528 if (!dri2_surf
->enable_out_fence
)
1531 fence
= dri2_dpy
->fence
->create_fence_fd(dri_ctx
, -1);
1533 fence_fd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
1535 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, fence
);
1537 dri2_surface_set_out_fence_fd(surf
, fence_fd
);
1541 dri2_create_drawable(struct dri2_egl_display
*dri2_dpy
,
1542 const __DRIconfig
*config
,
1543 struct dri2_egl_surface
*dri2_surf
,
1544 void *loaderPrivate
)
1546 __DRIcreateNewDrawableFunc createNewDrawable
;
1548 if (dri2_dpy
->image_driver
)
1549 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
1550 else if (dri2_dpy
->dri2
)
1551 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
1552 else if (dri2_dpy
->swrast
)
1553 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
1555 return _eglError(EGL_BAD_ALLOC
, "no createNewDrawable");
1557 dri2_surf
->dri_drawable
= createNewDrawable(dri2_dpy
->dri_screen
,
1558 config
, loaderPrivate
);
1559 if (dri2_surf
->dri_drawable
== NULL
)
1560 return _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
1566 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1569 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1570 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1572 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1573 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1574 _EGLDisplay
*old_disp
= NULL
;
1575 struct dri2_egl_display
*old_dri2_dpy
= NULL
;
1576 _EGLContext
*old_ctx
;
1577 _EGLSurface
*old_dsurf
, *old_rsurf
;
1578 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1579 __DRIdrawable
*ddraw
, *rdraw
;
1584 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1586 /* make new bindings */
1587 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1588 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1593 old_disp
= old_ctx
->Resource
.Display
;
1594 old_dri2_dpy
= dri2_egl_display(old_disp
);
1597 /* flush before context switch */
1601 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1602 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1603 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1606 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1609 dri2_surf_update_fence_fd(old_ctx
, disp
, old_dsurf
);
1611 /* Disable shared buffer mode */
1612 if (old_dsurf
&& _eglSurfaceInSharedBufferMode(old_dsurf
) &&
1613 old_dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1614 old_dri2_dpy
->vtbl
->set_shared_buffer_mode(old_disp
, old_dsurf
, false);
1617 dri2_dpy
->core
->unbindContext(old_cctx
);
1620 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1622 if (!unbind
&& !dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1623 /* undo the previous _eglBindContext */
1624 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1625 assert(&dri2_ctx
->base
== ctx
&&
1626 tmp_dsurf
== dsurf
&&
1627 tmp_rsurf
== rsurf
);
1629 if (old_dsurf
&& _eglSurfaceInSharedBufferMode(old_dsurf
) &&
1630 old_dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1631 old_dri2_dpy
->vtbl
->set_shared_buffer_mode(old_disp
, old_dsurf
, true);
1634 _eglPutSurface(dsurf
);
1635 _eglPutSurface(rsurf
);
1636 _eglPutContext(ctx
);
1638 _eglPutSurface(old_dsurf
);
1639 _eglPutSurface(old_rsurf
);
1640 _eglPutContext(old_ctx
);
1642 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1643 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1646 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1649 dri2_destroy_surface(drv
, disp
, old_dsurf
);
1650 dri2_destroy_surface(drv
, disp
, old_rsurf
);
1653 dri2_dpy
->ref_count
++;
1656 dri2_destroy_context(drv
, disp
, old_ctx
);
1657 dri2_display_release(old_disp
);
1660 if (dsurf
&& _eglSurfaceHasMutableRenderBuffer(dsurf
) &&
1661 dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1662 /* Always update the shared buffer mode. This is obviously needed when
1663 * the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When
1664 * EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the
1665 * case where external non-EGL API may have changed window's shared
1666 * buffer mode since we last saw it.
1668 bool mode
= (dsurf
->ActiveRenderBuffer
== EGL_SINGLE_BUFFER
);
1669 dri2_dpy
->vtbl
->set_shared_buffer_mode(disp
, dsurf
, mode
);
1676 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1678 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1680 return dri2_surf
->dri_drawable
;
1684 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1687 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1689 return _glapi_get_proc_address(procname
);
1693 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1694 _EGLConfig
*conf
, void *native_window
,
1695 const EGLint
*attrib_list
)
1697 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1698 return dri2_dpy
->vtbl
->create_window_surface(drv
, disp
, conf
, native_window
,
1703 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1704 _EGLConfig
*conf
, void *native_pixmap
,
1705 const EGLint
*attrib_list
)
1707 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1708 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, disp
, conf
, native_pixmap
,
1713 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1714 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1716 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1717 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, disp
, conf
, attrib_list
);
1721 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1724 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1725 if (!dri2_dpy
->vtbl
->swap_interval
)
1727 return dri2_dpy
->vtbl
->swap_interval(drv
, disp
, surf
, interval
);
1731 * Asks the client API to flush any rendering to the drawable so that we can
1732 * do our swapbuffers.
1735 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1737 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1738 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1740 if (dri2_dpy
->flush
) {
1741 if (dri2_dpy
->flush
->base
.version
>= 4) {
1742 /* We know there's a current context because:
1744 * "If surface is not bound to the calling thread’s current
1745 * context, an EGL_BAD_SURFACE error is generated."
1747 _EGLContext
*ctx
= _eglGetCurrentContext();
1748 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1750 /* From the EGL 1.4 spec (page 52):
1752 * "The contents of ancillary buffers are always undefined
1753 * after calling eglSwapBuffers."
1755 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1757 __DRI2_FLUSH_DRAWABLE
|
1758 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1759 __DRI2_THROTTLE_SWAPBUFFER
);
1761 dri2_dpy
->flush
->flush(dri_drawable
);
1767 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1769 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1770 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1771 _EGLContext
*ctx
= _eglGetCurrentContext();
1775 dri2_surf_update_fence_fd(ctx
, disp
, surf
);
1776 ret
= dri2_dpy
->vtbl
->swap_buffers(drv
, disp
, surf
);
1778 /* SwapBuffers marks the end of the frame; reset the damage region for
1779 * use again next time.
1781 if (ret
&& dri2_dpy
->buffer_damage
&&
1782 dri2_dpy
->buffer_damage
->set_damage_region
)
1783 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
1789 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1791 const EGLint
*rects
, EGLint n_rects
)
1793 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1794 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1795 _EGLContext
*ctx
= _eglGetCurrentContext();
1799 dri2_surf_update_fence_fd(ctx
, disp
, surf
);
1800 ret
= dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, disp
, surf
,
1803 /* SwapBuffers marks the end of the frame; reset the damage region for
1804 * use again next time.
1806 if (ret
&& dri2_dpy
->buffer_damage
&&
1807 dri2_dpy
->buffer_damage
->set_damage_region
)
1808 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
1814 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1815 EGLint numRects
, const EGLint
*rects
)
1817 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1818 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1821 ret
= dri2_dpy
->vtbl
->swap_buffers_region(drv
, disp
, surf
, numRects
, rects
);
1823 /* SwapBuffers marks the end of the frame; reset the damage region for
1824 * use again next time.
1826 if (ret
&& dri2_dpy
->buffer_damage
&&
1827 dri2_dpy
->buffer_damage
->set_damage_region
)
1828 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
1834 dri2_set_damage_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1835 EGLint
*rects
, EGLint n_rects
)
1837 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1838 __DRIdrawable
*drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1840 if (!dri2_dpy
->buffer_damage
|| !dri2_dpy
->buffer_damage
->set_damage_region
)
1843 dri2_dpy
->buffer_damage
->set_damage_region(drawable
, n_rects
, rects
);
1848 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1849 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1851 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1852 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, disp
, surf
, x
, y
, width
, height
);
1856 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1857 void *native_pixmap_target
)
1859 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1860 return dri2_dpy
->vtbl
->copy_buffers(drv
, disp
, surf
, native_pixmap_target
);
1864 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1866 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1867 return dri2_dpy
->vtbl
->query_buffer_age(drv
, disp
, surf
);
1871 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1873 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1874 _EGLSurface
*surf
= ctx
->DrawSurface
;
1875 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1879 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1880 * we need to copy fake to real here.*/
1882 if (dri2_dpy
->flush
!= NULL
)
1883 dri2_dpy
->flush
->flush(dri_drawable
);
1889 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1894 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1895 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1902 dri2_bind_tex_image(_EGLDriver
*drv
,
1903 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1905 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1906 struct dri2_egl_context
*dri2_ctx
;
1908 GLint format
, target
;
1909 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1911 ctx
= _eglGetCurrentContext();
1912 dri2_ctx
= dri2_egl_context(ctx
);
1914 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1917 switch (surf
->TextureFormat
) {
1918 case EGL_TEXTURE_RGB
:
1919 format
= __DRI_TEXTURE_FORMAT_RGB
;
1921 case EGL_TEXTURE_RGBA
:
1922 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1925 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1926 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1929 switch (surf
->TextureTarget
) {
1930 case EGL_TEXTURE_2D
:
1931 target
= GL_TEXTURE_2D
;
1934 target
= GL_TEXTURE_2D
;
1935 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1938 dri2_dpy
->tex_buffer
->setTexBuffer2(dri2_ctx
->dri_context
,
1946 dri2_release_tex_image(_EGLDriver
*drv
,
1947 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1949 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1950 struct dri2_egl_context
*dri2_ctx
;
1953 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1955 ctx
= _eglGetCurrentContext();
1956 dri2_ctx
= dri2_egl_context(ctx
);
1958 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1961 switch (surf
->TextureTarget
) {
1962 case EGL_TEXTURE_2D
:
1963 target
= GL_TEXTURE_2D
;
1966 assert(!"missing texture target");
1969 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1970 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1971 dri2_dpy
->tex_buffer
->releaseTexBuffer(dri2_ctx
->dri_context
,
1972 target
, dri_drawable
);
1979 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
,
1980 EGLenum target
, EGLClientBuffer buffer
,
1981 const EGLint
*attr_list
)
1983 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1984 return dri2_dpy
->vtbl
->create_image(drv
, disp
, ctx
, target
, buffer
,
1989 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1991 struct dri2_egl_image
*dri2_img
;
1993 if (dri_image
== NULL
) {
1994 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1998 dri2_img
= malloc(sizeof *dri2_img
);
2000 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
2004 _eglInitImage(&dri2_img
->base
, disp
);
2006 dri2_img
->dri_image
= dri_image
;
2008 return &dri2_img
->base
;
2012 * Translate a DRI Image extension error code into an EGL error code.
2015 egl_error_from_dri_image_error(int dri_error
)
2017 switch (dri_error
) {
2018 case __DRI_IMAGE_ERROR_SUCCESS
:
2020 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
2021 return EGL_BAD_ALLOC
;
2022 case __DRI_IMAGE_ERROR_BAD_MATCH
:
2023 return EGL_BAD_MATCH
;
2024 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
2025 return EGL_BAD_PARAMETER
;
2026 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
2027 return EGL_BAD_ACCESS
;
2029 assert(!"unknown dri_error code");
2030 return EGL_BAD_ALLOC
;
2035 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2036 EGLClientBuffer buffer
,
2037 const EGLint
*attr_list
)
2039 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2040 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2041 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
2042 __DRIimage
*dri_image
;
2044 if (renderbuffer
== 0) {
2045 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2046 return EGL_NO_IMAGE_KHR
;
2049 if (!disp
->Extensions
.KHR_gl_renderbuffer_image
) {
2050 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2051 return EGL_NO_IMAGE_KHR
;
2054 if (dri2_dpy
->image
->base
.version
>= 17 &&
2055 dri2_dpy
->image
->createImageFromRenderbuffer2
) {
2056 unsigned error
= ~0;
2058 dri_image
= dri2_dpy
->image
->createImageFromRenderbuffer2(
2059 dri2_ctx
->dri_context
, renderbuffer
, NULL
, &error
);
2061 assert(!!dri_image
== (error
== __DRI_IMAGE_ERROR_SUCCESS
));
2064 _eglError(egl_error_from_dri_image_error(error
), "dri2_create_image_khr");
2065 return EGL_NO_IMAGE_KHR
;
2068 dri_image
= dri2_dpy
->image
->createImageFromRenderbuffer(
2069 dri2_ctx
->dri_context
, renderbuffer
, NULL
);
2071 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2072 return EGL_NO_IMAGE_KHR
;
2076 return dri2_create_image_from_dri(disp
, dri_image
);
2079 #ifdef HAVE_WAYLAND_PLATFORM
2081 /* This structure describes how a wl_buffer maps to one or more
2082 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
2083 * offsets and strides of the planes in the buffer. This table maps a
2084 * wl_drm format code to a description of the planes in the buffer
2085 * that lets us create a __DRIimage for each of the planes. */
2087 static const struct wl_drm_components_descriptor
{
2088 uint32_t dri_components
;
2091 } wl_drm_components
[] = {
2092 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
2093 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
2094 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
2095 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
2096 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
2100 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2101 EGLClientBuffer _buffer
,
2102 const EGLint
*attr_list
)
2104 struct wl_drm_buffer
*buffer
;
2105 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2106 const struct wl_drm_components_descriptor
*f
;
2107 __DRIimage
*dri_image
;
2108 _EGLImageAttribs attrs
;
2111 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
2112 (struct wl_resource
*) _buffer
);
2116 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2119 plane
= attrs
.PlaneWL
;
2120 f
= buffer
->driver_format
;
2121 if (plane
< 0 || plane
>= f
->nplanes
) {
2122 _eglError(EGL_BAD_PARAMETER
,
2123 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
2127 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
2128 if (dri_image
== NULL
&& plane
== 0)
2129 dri_image
= dri2_dpy
->image
->dupImage(buffer
->driver_buffer
, NULL
);
2130 if (dri_image
== NULL
) {
2131 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
2135 return dri2_create_image_from_dri(disp
, dri_image
);
2140 dri2_get_sync_values_chromium(_EGLDisplay
*disp
, _EGLSurface
*surf
,
2141 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
2144 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2145 return dri2_dpy
->vtbl
->get_sync_values(disp
, surf
, ust
, msc
, sbc
);
2149 * Set the error code after a call to
2150 * dri2_egl_image::dri_image::createImageFromTexture.
2153 dri2_create_image_khr_texture_error(int dri_error
)
2155 EGLint egl_error
= egl_error_from_dri_image_error(dri_error
);
2157 if (egl_error
!= EGL_SUCCESS
)
2158 _eglError(egl_error
, "dri2_create_image_khr_texture");
2162 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2164 EGLClientBuffer buffer
,
2165 const EGLint
*attr_list
)
2167 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2168 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2169 struct dri2_egl_image
*dri2_img
;
2170 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
2171 _EGLImageAttribs attrs
;
2177 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2178 return EGL_NO_IMAGE_KHR
;
2181 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2182 return EGL_NO_IMAGE_KHR
;
2185 case EGL_GL_TEXTURE_2D_KHR
:
2186 if (!disp
->Extensions
.KHR_gl_texture_2D_image
) {
2187 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2188 return EGL_NO_IMAGE_KHR
;
2191 gl_target
= GL_TEXTURE_2D
;
2193 case EGL_GL_TEXTURE_3D_KHR
:
2194 if (!disp
->Extensions
.KHR_gl_texture_3D_image
) {
2195 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2196 return EGL_NO_IMAGE_KHR
;
2199 depth
= attrs
.GLTextureZOffset
;
2200 gl_target
= GL_TEXTURE_3D
;
2202 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2203 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2204 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2205 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2206 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2207 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2208 if (!disp
->Extensions
.KHR_gl_texture_cubemap_image
) {
2209 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2210 return EGL_NO_IMAGE_KHR
;
2213 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
2214 gl_target
= GL_TEXTURE_CUBE_MAP
;
2217 unreachable("Unexpected target in dri2_create_image_khr_texture()");
2218 return EGL_NO_IMAGE_KHR
;
2221 dri2_img
= malloc(sizeof *dri2_img
);
2223 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2224 return EGL_NO_IMAGE_KHR
;
2227 _eglInitImage(&dri2_img
->base
, disp
);
2229 dri2_img
->dri_image
=
2230 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
2234 attrs
.GLTextureLevel
,
2237 dri2_create_image_khr_texture_error(error
);
2239 if (!dri2_img
->dri_image
) {
2241 return EGL_NO_IMAGE_KHR
;
2243 return &dri2_img
->base
;
2247 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
2248 EGLint attribute
, EGLint
*value
)
2250 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2251 if (!dri2_dpy
->vtbl
->query_surface
)
2252 return _eglQuerySurface(drv
, disp
, surf
, attribute
, value
);
2253 return dri2_dpy
->vtbl
->query_surface(drv
, disp
, surf
, attribute
, value
);
2256 static struct wl_buffer
*
2257 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2260 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2261 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, disp
, img
);
2266 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2267 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2269 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2270 EGLint format
, name
, pitch
;
2271 _EGLImageAttribs attrs
;
2272 __DRIimage
*dri_image
;
2274 name
= (EGLint
) (uintptr_t) buffer
;
2276 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2279 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
2280 attrs
.DRMBufferStrideMESA
<= 0) {
2281 _eglError(EGL_BAD_PARAMETER
,
2282 "bad width, height or stride");
2286 switch (attrs
.DRMBufferFormatMESA
) {
2287 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2288 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2289 pitch
= attrs
.DRMBufferStrideMESA
;
2292 _eglError(EGL_BAD_PARAMETER
,
2293 "dri2_create_image_khr: unsupported pixmap depth");
2298 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
2306 return dri2_create_image_from_dri(disp
, dri_image
);
2310 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
2315 * "Required attributes and their values are as follows:
2317 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
2319 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
2320 * by drm_fourcc.h and used as the pixel_format parameter of the
2321 * drm_mode_fb_cmd2 ioctl."
2325 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2326 * incomplete, EGL_BAD_PARAMETER is generated."
2328 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
2329 !attrs
->DMABufFourCC
.IsPresent
)
2330 return _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
2335 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
2336 * specified for a plane's pitch or offset isn't supported by EGL,
2337 * EGL_BAD_ACCESS is generated."
2339 for (unsigned i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
2340 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
2341 attrs
->DMABufPlanePitches
[i
].Value
<= 0)
2342 return _eglError(EGL_BAD_ACCESS
, "invalid pitch");
2346 * If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2347 * attribute values may be given.
2349 * This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2350 * EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2352 for (unsigned i
= 0; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2353 if (attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
!=
2354 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
)
2355 return _eglError(EGL_BAD_PARAMETER
, "modifier attribute lo or hi missing");
2358 /* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2359 * mandate it, we only accept the same modifier across all planes. */
2360 for (unsigned i
= 1; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2361 if (attrs
->DMABufPlaneFds
[i
].IsPresent
) {
2362 if ((attrs
->DMABufPlaneModifiersLo
[0].IsPresent
!=
2363 attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
) ||
2364 (attrs
->DMABufPlaneModifiersLo
[0].Value
!=
2365 attrs
->DMABufPlaneModifiersLo
[i
].Value
) ||
2366 (attrs
->DMABufPlaneModifiersHi
[0].Value
!=
2367 attrs
->DMABufPlaneModifiersHi
[i
].Value
))
2368 return _eglError(EGL_BAD_PARAMETER
, "modifier attributes not equal");
2375 /* Returns the total number of planes for the format or zero if it isn't a
2376 * valid fourcc format.
2379 dri2_num_fourcc_format_planes(EGLint format
)
2383 case DRM_FORMAT_RG88
:
2384 case DRM_FORMAT_GR88
:
2385 case DRM_FORMAT_R16
:
2386 case DRM_FORMAT_GR1616
:
2387 case DRM_FORMAT_RGB332
:
2388 case DRM_FORMAT_BGR233
:
2389 case DRM_FORMAT_XRGB4444
:
2390 case DRM_FORMAT_XBGR4444
:
2391 case DRM_FORMAT_RGBX4444
:
2392 case DRM_FORMAT_BGRX4444
:
2393 case DRM_FORMAT_ARGB4444
:
2394 case DRM_FORMAT_ABGR4444
:
2395 case DRM_FORMAT_RGBA4444
:
2396 case DRM_FORMAT_BGRA4444
:
2397 case DRM_FORMAT_XRGB1555
:
2398 case DRM_FORMAT_XBGR1555
:
2399 case DRM_FORMAT_RGBX5551
:
2400 case DRM_FORMAT_BGRX5551
:
2401 case DRM_FORMAT_ARGB1555
:
2402 case DRM_FORMAT_ABGR1555
:
2403 case DRM_FORMAT_RGBA5551
:
2404 case DRM_FORMAT_BGRA5551
:
2405 case DRM_FORMAT_RGB565
:
2406 case DRM_FORMAT_BGR565
:
2407 case DRM_FORMAT_RGB888
:
2408 case DRM_FORMAT_BGR888
:
2409 case DRM_FORMAT_XRGB8888
:
2410 case DRM_FORMAT_XBGR8888
:
2411 case DRM_FORMAT_RGBX8888
:
2412 case DRM_FORMAT_BGRX8888
:
2413 case DRM_FORMAT_ARGB8888
:
2414 case DRM_FORMAT_ABGR8888
:
2415 case DRM_FORMAT_RGBA8888
:
2416 case DRM_FORMAT_BGRA8888
:
2417 case DRM_FORMAT_XRGB2101010
:
2418 case DRM_FORMAT_XBGR2101010
:
2419 case DRM_FORMAT_RGBX1010102
:
2420 case DRM_FORMAT_BGRX1010102
:
2421 case DRM_FORMAT_ARGB2101010
:
2422 case DRM_FORMAT_ABGR2101010
:
2423 case DRM_FORMAT_RGBA1010102
:
2424 case DRM_FORMAT_BGRA1010102
:
2425 case DRM_FORMAT_XBGR16161616F
:
2426 case DRM_FORMAT_ABGR16161616F
:
2427 case DRM_FORMAT_YUYV
:
2428 case DRM_FORMAT_YVYU
:
2429 case DRM_FORMAT_UYVY
:
2430 case DRM_FORMAT_VYUY
:
2431 case DRM_FORMAT_AYUV
:
2432 case DRM_FORMAT_XYUV8888
:
2435 case DRM_FORMAT_NV12
:
2436 case DRM_FORMAT_NV21
:
2437 case DRM_FORMAT_NV16
:
2438 case DRM_FORMAT_NV61
:
2439 case DRM_FORMAT_P010
:
2440 case DRM_FORMAT_P012
:
2441 case DRM_FORMAT_P016
:
2444 case DRM_FORMAT_YUV410
:
2445 case DRM_FORMAT_YVU410
:
2446 case DRM_FORMAT_YUV411
:
2447 case DRM_FORMAT_YVU411
:
2448 case DRM_FORMAT_YUV420
:
2449 case DRM_FORMAT_YVU420
:
2450 case DRM_FORMAT_YUV422
:
2451 case DRM_FORMAT_YVU422
:
2452 case DRM_FORMAT_YUV444
:
2453 case DRM_FORMAT_YVU444
:
2461 /* Returns the total number of file descriptors. Zero indicates an error. */
2463 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
2465 unsigned plane_n
= dri2_num_fourcc_format_planes(attrs
->DMABufFourCC
.Value
);
2467 _eglError(EGL_BAD_MATCH
, "unknown drm fourcc format");
2471 for (unsigned i
= plane_n
; i
< DMA_BUF_MAX_PLANES
; i
++) {
2473 * The modifiers extension spec says:
2475 * "Modifiers may modify any attribute of a buffer import, including
2476 * but not limited to adding extra planes to a format which
2477 * otherwise does not have those planes. As an example, a modifier
2478 * may add a plane for an external compression buffer to a
2479 * single-plane format. The exact meaning and effect of any
2480 * modifier is canonically defined by drm_fourcc.h, not as part of
2483 if (attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
&&
2484 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
) {
2492 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2493 * incomplete, EGL_BAD_PARAMETER is generated."
2495 for (unsigned i
= 0; i
< plane_n
; ++i
) {
2496 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2497 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2498 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2499 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2505 * The spec also says:
2507 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2508 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2509 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2510 * or EGL_DMA_BUF_PLANE3_* attributes are specified."
2512 for (unsigned i
= plane_n
; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2513 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2514 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2515 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2516 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2525 dri2_query_dma_buf_formats(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2526 EGLint max
, EGLint
*formats
, EGLint
*count
)
2528 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2529 if (max
< 0 || (max
> 0 && formats
== NULL
))
2530 return _eglError(EGL_BAD_PARAMETER
, "invalid value for max count of formats");
2532 if (dri2_dpy
->image
->base
.version
< 15 ||
2533 dri2_dpy
->image
->queryDmaBufFormats
== NULL
)
2536 if (!dri2_dpy
->image
->queryDmaBufFormats(dri2_dpy
->dri_screen
, max
,
2541 /* Assert that all of the formats returned are actually fourcc formats.
2542 * Some day, if we want the internal interface function to be able to
2543 * return the fake fourcc formats defined in dri_interface.h, we'll have
2544 * to do something more clever here to pair the list down to just real
2545 * fourcc formats so that we don't leak the fake internal ones.
2547 for (int i
= 0; i
< *count
; i
++) {
2548 assert(dri2_num_fourcc_format_planes(formats
[i
]) > 0);
2556 dri2_query_dma_buf_modifiers(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint format
,
2557 EGLint max
, EGLuint64KHR
*modifiers
,
2558 EGLBoolean
*external_only
, EGLint
*count
)
2560 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2562 if (dri2_num_fourcc_format_planes(format
) == 0)
2563 return _eglError(EGL_BAD_PARAMETER
, "invalid fourcc format");
2566 return _eglError(EGL_BAD_PARAMETER
, "invalid value for max count of formats");
2568 if (max
> 0 && modifiers
== NULL
)
2569 return _eglError(EGL_BAD_PARAMETER
, "invalid modifiers array");
2571 if (dri2_dpy
->image
->base
.version
< 15 ||
2572 dri2_dpy
->image
->queryDmaBufModifiers
== NULL
)
2575 if (dri2_dpy
->image
->queryDmaBufModifiers(dri2_dpy
->dri_screen
, format
,
2577 (unsigned int *) external_only
,
2579 return _eglError(EGL_BAD_PARAMETER
, "invalid format");
2587 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2588 * EGL will take a reference to the dma_buf(s) which it will release at any
2589 * time while the EGLDisplay is initialized. It is the responsibility of the
2590 * application to close the dma_buf file descriptors."
2592 * Therefore we must never close or otherwise modify the file descriptors.
2595 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2596 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2598 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2600 _EGLImageAttribs attrs
;
2601 __DRIimage
*dri_image
;
2603 int fds
[DMA_BUF_MAX_PLANES
];
2604 int pitches
[DMA_BUF_MAX_PLANES
];
2605 int offsets
[DMA_BUF_MAX_PLANES
];
2607 bool has_modifier
= false;
2613 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2614 * error EGL_BAD_PARAMETER is generated."
2616 if (buffer
!= NULL
) {
2617 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2621 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2624 if (!dri2_check_dma_buf_attribs(&attrs
))
2627 num_fds
= dri2_check_dma_buf_format(&attrs
);
2631 for (unsigned i
= 0; i
< num_fds
; ++i
) {
2632 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2633 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2634 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2637 /* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2638 * will be present in attrs.DMABufPlaneModifiersLo[0] and
2639 * attrs.DMABufPlaneModifiersHi[0] */
2640 if (attrs
.DMABufPlaneModifiersLo
[0].IsPresent
) {
2641 modifier
= combine_u32_into_u64(attrs
.DMABufPlaneModifiersHi
[0].Value
,
2642 attrs
.DMABufPlaneModifiersLo
[0].Value
);
2643 has_modifier
= true;
2647 if (dri2_dpy
->image
->base
.version
< 15 ||
2648 dri2_dpy
->image
->createImageFromDmaBufs2
== NULL
) {
2649 _eglError(EGL_BAD_MATCH
, "unsupported dma_buf format modifier");
2650 return EGL_NO_IMAGE_KHR
;
2653 dri2_dpy
->image
->createImageFromDmaBufs2(dri2_dpy
->dri_screen
,
2654 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2655 modifier
, fds
, num_fds
, pitches
, offsets
,
2656 attrs
.DMABufYuvColorSpaceHint
.Value
,
2657 attrs
.DMABufSampleRangeHint
.Value
,
2658 attrs
.DMABufChromaHorizontalSiting
.Value
,
2659 attrs
.DMABufChromaVerticalSiting
.Value
,
2665 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2666 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2667 fds
, num_fds
, pitches
, offsets
,
2668 attrs
.DMABufYuvColorSpaceHint
.Value
,
2669 attrs
.DMABufSampleRangeHint
.Value
,
2670 attrs
.DMABufChromaHorizontalSiting
.Value
,
2671 attrs
.DMABufChromaVerticalSiting
.Value
,
2675 dri2_create_image_khr_texture_error(error
);
2678 return EGL_NO_IMAGE_KHR
;
2680 res
= dri2_create_image_from_dri(disp
, dri_image
);
2685 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2686 const EGLint
*attr_list
)
2688 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2689 struct dri2_egl_image
*dri2_img
;
2690 _EGLImageAttribs attrs
;
2691 unsigned int dri_use
, valid_mask
;
2697 _eglError(EGL_BAD_PARAMETER
, __func__
);
2698 return EGL_NO_IMAGE_KHR
;
2701 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2702 return EGL_NO_IMAGE_KHR
;
2704 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2705 _eglError(EGL_BAD_PARAMETER
, __func__
);
2706 return EGL_NO_IMAGE_KHR
;
2709 switch (attrs
.DRMBufferFormatMESA
) {
2710 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2711 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2714 _eglError(EGL_BAD_PARAMETER
, __func__
);
2715 return EGL_NO_IMAGE_KHR
;
2719 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2720 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2721 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2722 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2723 _eglError(EGL_BAD_PARAMETER
, __func__
);
2724 return EGL_NO_IMAGE_KHR
;
2728 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2729 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2730 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2731 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2732 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2733 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2735 dri2_img
= malloc(sizeof *dri2_img
);
2737 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2738 return EGL_NO_IMAGE_KHR
;
2741 _eglInitImage(&dri2_img
->base
, disp
);
2743 dri2_img
->dri_image
=
2744 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2745 attrs
.Width
, attrs
.Height
,
2746 format
, dri_use
, dri2_img
);
2747 if (dri2_img
->dri_image
== NULL
) {
2749 _eglError(EGL_BAD_ALLOC
, "dri2_create_drm_image_mesa");
2750 return EGL_NO_IMAGE_KHR
;
2753 return &dri2_img
->base
;
2757 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2758 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2760 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2761 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2765 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2766 __DRI_IMAGE_ATTRIB_NAME
, name
))
2767 return _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2770 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2771 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2774 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2775 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2781 * Checks if we can support EGL_MESA_image_dma_buf_export on this image.
2783 * The spec provides a boolean return for the driver to reject exporting for
2784 * basically any reason, but doesn't specify any particular error cases. For
2785 * now, we just fail if we don't have a DRM fourcc for the format.
2788 dri2_can_export_dma_buf_image(_EGLDisplay
*disp
, _EGLImage
*img
)
2790 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2791 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2794 if (!dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2795 __DRI_IMAGE_ATTRIB_FOURCC
, &fourcc
)) {
2803 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2805 EGLint
*fourcc
, EGLint
*nplanes
,
2806 EGLuint64KHR
*modifiers
)
2808 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2809 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2814 if (!dri2_can_export_dma_buf_image(disp
, img
))
2817 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2818 __DRI_IMAGE_ATTRIB_NUM_PLANES
, &num_planes
);
2820 *nplanes
= num_planes
;
2823 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2824 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2828 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
2831 query
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2832 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
2834 query
&= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2835 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
2838 modifier
= combine_u32_into_u64 (mod_hi
, mod_lo
);
2840 for (int i
= 0; i
< num_planes
; i
++)
2841 modifiers
[i
] = modifier
;
2848 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2849 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2851 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2852 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2857 if (!dri2_can_export_dma_buf_image(disp
, img
))
2860 /* EGL_MESA_image_dma_buf_export spec says:
2861 * "If the number of fds is less than the number of planes, then
2862 * subsequent fd slots should contain -1."
2865 /* Query nplanes so that we know how big the given array is. */
2866 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2867 __DRI_IMAGE_ATTRIB_NUM_PLANES
, &nplanes
);
2868 memset(fds
, -1, nplanes
* sizeof(int));
2871 /* rework later to provide multiple fds/strides/offsets */
2873 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2874 __DRI_IMAGE_ATTRIB_FD
, fds
);
2877 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2878 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2882 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2883 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2885 offsets
[0] = img_offset
;
2896 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2897 _EGLContext
*ctx
, EGLenum target
,
2898 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2903 case EGL_GL_TEXTURE_2D_KHR
:
2904 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2905 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2906 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2907 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2908 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2909 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2910 case EGL_GL_TEXTURE_3D_KHR
:
2911 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2912 case EGL_GL_RENDERBUFFER_KHR
:
2913 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2915 case EGL_DRM_BUFFER_MESA
:
2916 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2917 case EGL_LINUX_DMA_BUF_EXT
:
2918 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2920 #ifdef HAVE_WAYLAND_PLATFORM
2921 case EGL_WAYLAND_BUFFER_WL
:
2922 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2925 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2926 return EGL_NO_IMAGE_KHR
;
2931 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2933 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2934 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2938 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2944 #ifdef HAVE_WAYLAND_PLATFORM
2947 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2948 struct wl_drm_buffer
*buffer
)
2950 _EGLDisplay
*disp
= user_data
;
2951 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2953 int dri_components
= 0;
2956 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2965 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2977 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2979 buffer
->driver_format
= NULL
;
2980 for (int i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2981 if (wl_drm_components
[i
].dri_components
== dri_components
)
2982 buffer
->driver_format
= &wl_drm_components
[i
];
2984 if (buffer
->driver_format
== NULL
)
2985 dri2_dpy
->image
->destroyImage(img
);
2987 buffer
->driver_buffer
= img
;
2991 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2993 _EGLDisplay
*disp
= user_data
;
2994 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2996 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
3000 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3001 struct wl_display
*wl_dpy
)
3003 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3004 const struct wayland_drm_callbacks wl_drm_callbacks
= {
3005 .authenticate
= (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
,
3006 .reference_buffer
= dri2_wl_reference_buffer
,
3007 .release_buffer
= dri2_wl_release_buffer
,
3008 .is_format_supported
= dri2_wl_is_format_supported
3015 if (dri2_dpy
->wl_server_drm
)
3018 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
3019 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
3020 dri2_dpy
->image
->base
.version
>= 7 &&
3021 dri2_dpy
->image
->createImageFromFds
!= NULL
)
3022 flags
|= WAYLAND_DRM_PRIME
;
3024 dri2_dpy
->wl_server_drm
=
3025 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
3026 &wl_drm_callbacks
, disp
, flags
);
3028 if (!dri2_dpy
->wl_server_drm
)
3031 #ifdef HAVE_DRM_PLATFORM
3032 /* We have to share the wl_drm instance with gbm, so gbm can convert
3033 * wl_buffers to gbm bos. */
3034 if (dri2_dpy
->gbm_dri
)
3035 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
3042 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3043 struct wl_display
*wl_dpy
)
3045 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3049 if (!dri2_dpy
->wl_server_drm
)
3052 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
3053 dri2_dpy
->wl_server_drm
= NULL
;
3059 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3060 struct wl_resource
*buffer_resource
,
3061 EGLint attribute
, EGLint
*value
)
3063 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3064 struct wl_drm_buffer
*buffer
;
3065 const struct wl_drm_components_descriptor
*format
;
3067 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
3071 format
= buffer
->driver_format
;
3072 switch (attribute
) {
3073 case EGL_TEXTURE_FORMAT
:
3074 *value
= format
->components
;
3077 *value
= buffer
->width
;
3080 *value
= buffer
->height
;
3089 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
3091 p_atomic_inc(&sync
->refcount
);
3095 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
3096 struct dri2_egl_sync
*dri2_sync
)
3098 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
3099 switch (dri2_sync
->base
.Type
) {
3100 case EGL_SYNC_REUSABLE_KHR
:
3101 cnd_destroy(&dri2_sync
->cond
);
3103 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3104 if (dri2_sync
->base
.SyncFd
!= EGL_NO_NATIVE_FENCE_FD_ANDROID
)
3105 close(dri2_sync
->base
.SyncFd
);
3111 if (dri2_sync
->fence
)
3112 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
3119 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3120 EGLenum type
, const EGLAttrib
*attrib_list
)
3122 _EGLContext
*ctx
= _eglGetCurrentContext();
3123 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3124 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3125 struct dri2_egl_sync
*dri2_sync
;
3127 pthread_condattr_t attr
;
3129 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
3131 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
3135 if (!_eglInitSync(&dri2_sync
->base
, disp
, type
, attrib_list
)) {
3141 case EGL_SYNC_FENCE_KHR
:
3142 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
3143 if (!dri2_sync
->fence
) {
3144 /* Why did it fail? DRI doesn't return an error code, so we emit
3145 * a generic EGL error that doesn't communicate user error.
3147 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
3153 case EGL_SYNC_CL_EVENT_KHR
:
3154 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
3155 dri2_dpy
->dri_screen
,
3156 dri2_sync
->base
.CLEvent
);
3157 /* this can only happen if the cl_event passed in is invalid. */
3158 if (!dri2_sync
->fence
) {
3159 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
3164 /* the initial status must be "signaled" if the cl_event is signaled */
3165 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
3166 dri2_sync
->fence
, 0, 0))
3167 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3170 case EGL_SYNC_REUSABLE_KHR
:
3171 /* intialize attr */
3172 ret
= pthread_condattr_init(&attr
);
3175 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3180 /* change clock attribute to CLOCK_MONOTONIC */
3181 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
3184 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3189 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
3192 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3197 /* initial status of reusable sync must be "unsignaled" */
3198 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
3201 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3202 if (dri2_dpy
->fence
->create_fence_fd
) {
3203 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence_fd(
3204 dri2_ctx
->dri_context
,
3205 dri2_sync
->base
.SyncFd
);
3207 if (!dri2_sync
->fence
) {
3208 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
3215 p_atomic_set(&dri2_sync
->refcount
, 1);
3216 return &dri2_sync
->base
;
3220 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3222 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3223 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3224 EGLint ret
= EGL_TRUE
;
3227 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
3228 * then unlock all threads possibly blocked by the reusable sync before
3231 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
3232 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
3233 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3234 /* unblock all threads currently blocked by sync */
3235 err
= cnd_broadcast(&dri2_sync
->cond
);
3238 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
3243 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
3249 dri2_dup_native_fence_fd(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3251 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3252 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3254 assert(sync
->Type
== EGL_SYNC_NATIVE_FENCE_ANDROID
);
3256 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
3257 /* try to retrieve the actual native fence fd.. if rendering is
3258 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
3260 sync
->SyncFd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
3264 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
3265 /* if native fence fd still not created, return an error: */
3266 _eglError(EGL_BAD_PARAMETER
, "eglDupNativeFenceFDANDROID");
3267 return EGL_NO_NATIVE_FENCE_FD_ANDROID
;
3270 return dup(sync
->SyncFd
);
3274 dri2_set_blob_cache_funcs(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3275 EGLSetBlobFuncANDROID set
,
3276 EGLGetBlobFuncANDROID get
)
3278 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3279 dri2_dpy
->blob
->set_cache_funcs(dri2_dpy
->dri_screen
,
3281 disp
->BlobCacheGet
);
3285 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
,
3286 EGLint flags
, EGLTime timeout
)
3288 _EGLContext
*ctx
= _eglGetCurrentContext();
3289 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3290 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3291 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3292 unsigned wait_flags
= 0;
3294 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
3296 /* The EGL_KHR_fence_sync spec states:
3298 * "If no context is current for the bound API,
3299 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
3301 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
3302 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
3304 /* the sync object should take a reference while waiting */
3305 dri2_egl_ref_sync(dri2_sync
);
3307 switch (sync
->Type
) {
3308 case EGL_SYNC_FENCE_KHR
:
3309 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3310 case EGL_SYNC_CL_EVENT_KHR
:
3311 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
3312 dri2_sync
->fence
, wait_flags
,
3314 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3316 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
3319 case EGL_SYNC_REUSABLE_KHR
:
3320 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
3321 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
3322 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
3326 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
3327 if (timeout
== EGL_FOREVER_KHR
) {
3328 mtx_lock(&dri2_sync
->mutex
);
3329 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
3330 mtx_unlock(&dri2_sync
->mutex
);
3332 /* if reusable sync has not been yet signaled */
3333 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
3334 /* timespecs for cnd_timedwait */
3335 struct timespec current
;
3336 struct timespec expire
;
3338 /* We override the clock to monotonic when creating the condition
3340 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
3342 /* calculating when to expire */
3343 expire
.tv_nsec
= timeout
% 1000000000L;
3344 expire
.tv_sec
= timeout
/ 1000000000L;
3346 expire
.tv_nsec
+= current
.tv_nsec
;
3347 expire
.tv_sec
+= current
.tv_sec
;
3349 /* expire.nsec now is a number between 0 and 1999999998 */
3350 if (expire
.tv_nsec
> 999999999L) {
3352 expire
.tv_nsec
-= 1000000000L;
3355 mtx_lock(&dri2_sync
->mutex
);
3356 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
3357 mtx_unlock(&dri2_sync
->mutex
);
3359 if (ret
== thrd_busy
) {
3360 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
3361 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
3363 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
3371 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
3377 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
,
3380 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3383 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
)
3384 return _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
3386 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
)
3387 return _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
3389 dri2_sync
->base
.SyncStatus
= mode
;
3391 if (mode
== EGL_SIGNALED_KHR
) {
3392 ret
= cnd_broadcast(&dri2_sync
->cond
);
3394 /* fail to broadcast */
3396 return _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
3403 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3405 _EGLContext
*ctx
= _eglGetCurrentContext();
3406 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3407 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3408 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3410 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
3411 dri2_sync
->fence
, 0);
3416 dri2_interop_query_device_info(_EGLDisplay
*disp
, _EGLContext
*ctx
,
3417 struct mesa_glinterop_device_info
*out
)
3419 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3420 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3422 if (!dri2_dpy
->interop
)
3423 return MESA_GLINTEROP_UNSUPPORTED
;
3425 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
3429 dri2_interop_export_object(_EGLDisplay
*disp
, _EGLContext
*ctx
,
3430 struct mesa_glinterop_export_in
*in
,
3431 struct mesa_glinterop_export_out
*out
)
3433 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3434 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3436 if (!dri2_dpy
->interop
)
3437 return MESA_GLINTEROP_UNSUPPORTED
;
3439 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
3443 * This is the main entrypoint into the driver, called by libEGL.
3444 * Gets an _EGLDriver object and init its dispatch table.
3447 _eglInitDriver(_EGLDriver
*dri2_drv
)
3449 dri2_drv
->API
.Initialize
= dri2_initialize
;
3450 dri2_drv
->API
.Terminate
= dri2_terminate
;
3451 dri2_drv
->API
.CreateContext
= dri2_create_context
;
3452 dri2_drv
->API
.DestroyContext
= dri2_destroy_context
;
3453 dri2_drv
->API
.MakeCurrent
= dri2_make_current
;
3454 dri2_drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
3455 dri2_drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
3456 dri2_drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
3457 dri2_drv
->API
.DestroySurface
= dri2_destroy_surface
;
3458 dri2_drv
->API
.GetProcAddress
= dri2_get_proc_address
;
3459 dri2_drv
->API
.WaitClient
= dri2_wait_client
;
3460 dri2_drv
->API
.WaitNative
= dri2_wait_native
;
3461 dri2_drv
->API
.BindTexImage
= dri2_bind_tex_image
;
3462 dri2_drv
->API
.ReleaseTexImage
= dri2_release_tex_image
;
3463 dri2_drv
->API
.SwapInterval
= dri2_swap_interval
;
3464 dri2_drv
->API
.SwapBuffers
= dri2_swap_buffers
;
3465 dri2_drv
->API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
3466 dri2_drv
->API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
3467 dri2_drv
->API
.SetDamageRegion
= dri2_set_damage_region
;
3468 dri2_drv
->API
.PostSubBufferNV
= dri2_post_sub_buffer
;
3469 dri2_drv
->API
.CopyBuffers
= dri2_copy_buffers
,
3470 dri2_drv
->API
.QueryBufferAge
= dri2_query_buffer_age
;
3471 dri2_drv
->API
.CreateImageKHR
= dri2_create_image
;
3472 dri2_drv
->API
.DestroyImageKHR
= dri2_destroy_image_khr
;
3473 dri2_drv
->API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
3474 dri2_drv
->API
.QuerySurface
= dri2_query_surface
;
3475 dri2_drv
->API
.QueryDriverName
= dri2_query_driver_name
;
3476 dri2_drv
->API
.QueryDriverConfig
= dri2_query_driver_config
;
3478 dri2_drv
->API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
3479 dri2_drv
->API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
3480 dri2_drv
->API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
3481 dri2_drv
->API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
3482 dri2_drv
->API
.QueryDmaBufFormatsEXT
= dri2_query_dma_buf_formats
;
3483 dri2_drv
->API
.QueryDmaBufModifiersEXT
= dri2_query_dma_buf_modifiers
;
3485 #ifdef HAVE_WAYLAND_PLATFORM
3486 dri2_drv
->API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
3487 dri2_drv
->API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
3488 dri2_drv
->API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
3490 dri2_drv
->API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
3491 dri2_drv
->API
.CreateSyncKHR
= dri2_create_sync
;
3492 dri2_drv
->API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
3493 dri2_drv
->API
.SignalSyncKHR
= dri2_signal_sync
;
3494 dri2_drv
->API
.WaitSyncKHR
= dri2_server_wait_sync
;
3495 dri2_drv
->API
.DestroySyncKHR
= dri2_destroy_sync
;
3496 dri2_drv
->API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
3497 dri2_drv
->API
.GLInteropExportObject
= dri2_interop_export_object
;
3498 dri2_drv
->API
.DupNativeFenceFDANDROID
= dri2_dup_native_fence_fd
;
3499 dri2_drv
->API
.SetBlobCacheFuncsANDROID
= dri2_set_blob_cache_funcs
;