2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Kristian Høgsberg <krh@bitplanet.net>
39 #include <c11/threads.h>
43 #include "drm-uapi/drm_fourcc.h"
46 #include <GL/internal/dri_interface.h>
47 #include <sys/types.h>
50 #ifdef HAVE_WAYLAND_PLATFORM
51 #include <wayland-client.h>
52 #include "wayland-drm.h"
53 #include "wayland-drm-client-protocol.h"
54 #include "linux-dmabuf-unstable-v1-client-protocol.h"
57 #ifdef HAVE_X11_PLATFORM
58 #include "X11/Xlibint.h"
61 #include "egldefines.h"
63 #include "GL/mesa_glinterop.h"
64 #include "loader/loader.h"
65 #include "util/u_atomic.h"
66 #include "util/u_vector.h"
67 #include "mapi/glapi/glapi.h"
68 #include "util/bitscan.h"
70 /* Additional definitions not yet in the drm_fourcc.h.
72 #ifndef DRM_FORMAT_P010
73 #define DRM_FORMAT_P010 fourcc_code('P', '0', '1', '0') /* 2x2 subsampled Cb:Cr plane 10 bits per channel */
76 #ifndef DRM_FORMAT_P012
77 #define DRM_FORMAT_P012 fourcc_code('P', '0', '1', '2') /* 2x2 subsampled Cb:Cr plane 12 bits per channel */
80 #ifndef DRM_FORMAT_P016
81 #define DRM_FORMAT_P016 fourcc_code('P', '0', '1', '6') /* 2x2 subsampled Cb:Cr plane 16 bits per channel */
84 #define NUM_ATTRIBS 12
87 dri_set_background_context(void *loaderPrivate
)
89 _EGLContext
*ctx
= _eglGetCurrentContext();
90 _EGLThreadInfo
*t
= _eglGetCurrentThread();
92 _eglBindContextToThread(ctx
, t
);
98 static void (*glFlush
)(void);
99 static mtx_t glFlushMutex
= _MTX_INITIALIZER_NP
;
101 mtx_lock(&glFlushMutex
);
103 glFlush
= _glapi_get_proc_address("glFlush");
104 mtx_unlock(&glFlushMutex
);
106 /* if glFlush is not available things are horribly broken */
108 _eglLog(_EGL_WARNING
, "DRI2: failed to find glFlush entry point");
116 dri_is_thread_safe(void *loaderPrivate
)
118 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
119 UNUSED _EGLDisplay
*display
= dri2_surf
->base
.Resource
.Display
;
121 #ifdef HAVE_X11_PLATFORM
122 Display
*xdpy
= (Display
*)display
->PlatformDisplay
;
124 /* Check Xlib is running in thread safe mode when running on EGL/X11-xlib
127 * 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
128 * It wll be NULL if XInitThreads wasn't called.
130 if (display
->Platform
== _EGL_PLATFORM_X11
&& xdpy
&& !xdpy
->lock_fns
)
134 #ifdef HAVE_WAYLAND_PLATFORM
135 if (display
->Platform
== _EGL_PLATFORM_WAYLAND
)
142 const __DRIbackgroundCallableExtension background_callable_extension
= {
143 .base
= { __DRI_BACKGROUND_CALLABLE
, 2 },
145 .setBackgroundContext
= dri_set_background_context
,
146 .isThreadSafe
= dri_is_thread_safe
,
149 const __DRIuseInvalidateExtension use_invalidate
= {
150 .base
= { __DRI_USE_INVALIDATE
, 1 }
154 dri2_get_pbuffer_drawable_info(__DRIdrawable
* draw
,
155 int *x
, int *y
, int *w
, int *h
,
158 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
161 *w
= dri2_surf
->base
.Width
;
162 *h
= dri2_surf
->base
.Height
;
165 /* HACK: technically we should have swrast_null, instead of these. We
166 * get away since only pbuffers are supported, thus the callbacks are
169 const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension
= {
170 .base
= { __DRI_SWRAST_LOADER
, 1 },
171 .getDrawableInfo
= dri2_get_pbuffer_drawable_info
,
176 static const EGLint dri2_to_egl_attribute_map
[__DRI_ATTRIB_MAX
] = {
177 [__DRI_ATTRIB_BUFFER_SIZE
] = EGL_BUFFER_SIZE
,
178 [__DRI_ATTRIB_LEVEL
] = EGL_LEVEL
,
179 [__DRI_ATTRIB_LUMINANCE_SIZE
] = EGL_LUMINANCE_SIZE
,
180 [__DRI_ATTRIB_DEPTH_SIZE
] = EGL_DEPTH_SIZE
,
181 [__DRI_ATTRIB_STENCIL_SIZE
] = EGL_STENCIL_SIZE
,
182 [__DRI_ATTRIB_SAMPLE_BUFFERS
] = EGL_SAMPLE_BUFFERS
,
183 [__DRI_ATTRIB_SAMPLES
] = EGL_SAMPLES
,
184 [__DRI_ATTRIB_MAX_PBUFFER_WIDTH
] = EGL_MAX_PBUFFER_WIDTH
,
185 [__DRI_ATTRIB_MAX_PBUFFER_HEIGHT
] = EGL_MAX_PBUFFER_HEIGHT
,
186 [__DRI_ATTRIB_MAX_PBUFFER_PIXELS
] = EGL_MAX_PBUFFER_PIXELS
,
187 [__DRI_ATTRIB_MAX_SWAP_INTERVAL
] = EGL_MAX_SWAP_INTERVAL
,
188 [__DRI_ATTRIB_MIN_SWAP_INTERVAL
] = EGL_MIN_SWAP_INTERVAL
,
189 [__DRI_ATTRIB_YINVERTED
] = EGL_Y_INVERTED_NOK
,
190 [__DRI_ATTRIB_CONFIG_SELECT_GROUP
] = EGL_CONFIG_SELECT_GROUP_MESA
,
194 dri2_get_dri_config(struct dri2_egl_config
*conf
, EGLint surface_type
,
197 const bool double_buffer
= surface_type
== EGL_WINDOW_BIT
;
198 const bool srgb
= colorspace
== EGL_GL_COLORSPACE_SRGB_KHR
;
200 return conf
->dri_config
[double_buffer
][srgb
];
204 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
206 if (conf
->NativeVisualID
!= criteria
->NativeVisualID
)
209 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
212 if (!_eglMatchConfig(conf
, criteria
))
219 dri2_get_shifts_and_sizes(const __DRIcoreExtension
*core
,
220 const __DRIconfig
*config
, int *shifts
,
225 if (core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_SHIFT
, (unsigned int *)&shifts
[0])) {
226 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_SHIFT
, (unsigned int *)&shifts
[1]);
227 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_SHIFT
, (unsigned int *)&shifts
[2]);
228 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_SHIFT
, (unsigned int *)&shifts
[3]);
230 /* Driver isn't exposing shifts, so convert masks to shifts */
231 core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_MASK
, &mask
);
232 shifts
[0] = ffs(mask
) - 1;
233 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_MASK
, &mask
);
234 shifts
[1] = ffs(mask
) - 1;
235 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_MASK
, &mask
);
236 shifts
[2] = ffs(mask
) - 1;
237 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_MASK
, &mask
);
238 shifts
[3] = ffs(mask
) - 1;
241 core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_SIZE
, &sizes
[0]);
242 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_SIZE
, &sizes
[1]);
243 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_SIZE
, &sizes
[2]);
244 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_SIZE
, &sizes
[3]);
248 dri2_get_render_type_float(const __DRIcoreExtension
*core
,
249 const __DRIconfig
*config
,
252 unsigned int render_type
;
254 core
->getConfigAttrib(config
, __DRI_ATTRIB_RENDER_TYPE
, &render_type
);
255 *is_float
= (render_type
& __DRI_ATTRIB_FLOAT_BIT
) ? true : false;
258 struct dri2_egl_config
*
259 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
260 EGLint surface_type
, const EGLint
*attr_list
,
261 const int *rgba_shifts
, const unsigned int *rgba_sizes
)
263 struct dri2_egl_config
*conf
;
264 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
266 unsigned int attrib
, value
, double_buffer
;
268 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
269 int dri_shifts
[4] = { -1, -1, -1, -1 };
270 unsigned int dri_sizes
[4] = { 0, 0, 0, 0 };
271 _EGLConfig
*matching_config
;
272 EGLint num_configs
= 0;
275 _eglInitConfig(&base
, disp
, id
);
278 bind_to_texture_rgb
= 0;
279 bind_to_texture_rgba
= 0;
281 for (int i
= 0; i
< __DRI_ATTRIB_MAX
; ++i
) {
282 if (!dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
, &attrib
, &value
))
286 case __DRI_ATTRIB_RENDER_TYPE
:
287 if (value
& __DRI_ATTRIB_FLOAT_BIT
)
288 _eglSetConfigKey(&base
, EGL_COLOR_COMPONENT_TYPE_EXT
,
289 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT
);
290 if (value
& __DRI_ATTRIB_RGBA_BIT
)
291 value
= EGL_RGB_BUFFER
;
292 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
293 value
= EGL_LUMINANCE_BUFFER
;
296 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
299 case __DRI_ATTRIB_CONFIG_CAVEAT
:
300 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
301 value
= EGL_NON_CONFORMANT_CONFIG
;
302 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
303 value
= EGL_SLOW_CONFIG
;
306 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
309 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
310 bind_to_texture_rgb
= value
;
313 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
314 bind_to_texture_rgba
= value
;
317 case __DRI_ATTRIB_DOUBLE_BUFFER
:
318 double_buffer
= value
;
321 case __DRI_ATTRIB_RED_SIZE
:
322 dri_sizes
[0] = value
;
323 _eglSetConfigKey(&base
, EGL_RED_SIZE
, value
);
326 case __DRI_ATTRIB_RED_MASK
:
327 dri_shifts
[0] = ffs(value
) - 1;
330 case __DRI_ATTRIB_RED_SHIFT
:
331 dri_shifts
[0] = value
;
334 case __DRI_ATTRIB_GREEN_SIZE
:
335 dri_sizes
[1] = value
;
336 _eglSetConfigKey(&base
, EGL_GREEN_SIZE
, value
);
339 case __DRI_ATTRIB_GREEN_MASK
:
340 dri_shifts
[1] = ffs(value
) - 1;
343 case __DRI_ATTRIB_GREEN_SHIFT
:
344 dri_shifts
[1] = value
;
347 case __DRI_ATTRIB_BLUE_SIZE
:
348 dri_sizes
[2] = value
;
349 _eglSetConfigKey(&base
, EGL_BLUE_SIZE
, value
);
352 case __DRI_ATTRIB_BLUE_MASK
:
353 dri_shifts
[2] = ffs(value
) - 1;
356 case __DRI_ATTRIB_BLUE_SHIFT
:
357 dri_shifts
[2] = value
;
360 case __DRI_ATTRIB_ALPHA_SIZE
:
361 dri_sizes
[3] = value
;
362 _eglSetConfigKey(&base
, EGL_ALPHA_SIZE
, value
);
365 case __DRI_ATTRIB_ALPHA_MASK
:
366 dri_shifts
[3] = ffs(value
) - 1;
369 case __DRI_ATTRIB_ALPHA_SHIFT
:
370 dri_shifts
[3] = value
;
373 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
374 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
375 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
376 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
377 /* Don't expose visuals with the accumulation buffer. */
382 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
384 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
388 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH
:
389 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_WIDTH
,
390 _EGL_MAX_PBUFFER_WIDTH
);
392 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT
:
393 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_HEIGHT
,
394 _EGL_MAX_PBUFFER_HEIGHT
);
396 case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER
:
397 if (disp
->Extensions
.KHR_mutable_render_buffer
)
398 surface_type
|= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR
;
401 key
= dri2_to_egl_attribute_map
[attrib
];
403 _eglSetConfigKey(&base
, key
, value
);
409 for (int i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
410 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
412 if (rgba_shifts
&& memcmp(rgba_shifts
, dri_shifts
, sizeof(dri_shifts
)))
415 if (rgba_sizes
&& memcmp(rgba_sizes
, dri_sizes
, sizeof(dri_sizes
)))
418 base
.NativeRenderable
= EGL_TRUE
;
420 base
.SurfaceType
= surface_type
;
421 if (surface_type
& (EGL_PBUFFER_BIT
|
422 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
423 base
.BindToTextureRGB
= bind_to_texture_rgb
;
424 if (base
.AlphaSize
> 0)
425 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
429 surface_type
&= ~EGL_PIXMAP_BIT
;
432 /* No support for pbuffer + MSAA for now.
434 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
435 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
438 surface_type
&= ~EGL_PBUFFER_BIT
;
444 base
.RenderableType
= disp
->ClientAPIs
;
445 base
.Conformant
= disp
->ClientAPIs
;
447 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
448 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
450 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
451 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
455 config_id
= base
.ConfigID
;
456 base
.ConfigID
= EGL_DONT_CARE
;
457 base
.SurfaceType
= EGL_DONT_CARE
;
458 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
459 (_EGLArrayForEach
) dri2_match_config
, &base
);
461 if (num_configs
== 1) {
462 conf
= (struct dri2_egl_config
*) matching_config
;
464 if (!conf
->dri_config
[double_buffer
][srgb
])
465 conf
->dri_config
[double_buffer
][srgb
] = dri_config
;
467 /* a similar config type is already added (unlikely) => discard */
470 else if (num_configs
== 0) {
471 conf
= calloc(1, sizeof *conf
);
475 conf
->dri_config
[double_buffer
][srgb
] = dri_config
;
477 memcpy(&conf
->base
, &base
, sizeof base
);
478 conf
->base
.SurfaceType
= 0;
479 conf
->base
.ConfigID
= config_id
;
481 _eglLinkConfig(&conf
->base
);
484 unreachable("duplicates should not be possible");
488 conf
->base
.SurfaceType
|= surface_type
;
494 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
496 _EGLDisplay
*disp
= data
;
497 struct dri2_egl_image
*dri2_img
;
502 img
= _eglLookupImage(image
, disp
);
504 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
508 dri2_img
= dri2_egl_image(image
);
510 return dri2_img
->dri_image
;
513 const __DRIimageLookupExtension image_lookup_extension
= {
514 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
516 .lookupEGLImage
= dri2_lookup_egl_image
519 struct dri2_extension_match
{
525 static const struct dri2_extension_match dri3_driver_extensions
[] = {
526 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
527 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
531 static const struct dri2_extension_match dri2_driver_extensions
[] = {
532 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
533 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
537 static const struct dri2_extension_match dri2_core_extensions
[] = {
538 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
539 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
540 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
544 static const struct dri2_extension_match swrast_driver_extensions
[] = {
545 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
546 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
550 static const struct dri2_extension_match swrast_core_extensions
[] = {
551 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
555 static const struct dri2_extension_match optional_driver_extensions
[] = {
556 { __DRI_CONFIG_OPTIONS
, 1, offsetof(struct dri2_egl_display
, configOptions
) },
560 static const struct dri2_extension_match optional_core_extensions
[] = {
561 { __DRI2_ROBUSTNESS
, 1, offsetof(struct dri2_egl_display
, robustness
) },
562 { __DRI2_NO_ERROR
, 1, offsetof(struct dri2_egl_display
, no_error
) },
563 { __DRI2_CONFIG_QUERY
, 1, offsetof(struct dri2_egl_display
, config
) },
564 { __DRI2_FENCE
, 1, offsetof(struct dri2_egl_display
, fence
) },
565 { __DRI2_BUFFER_DAMAGE
, 1, offsetof(struct dri2_egl_display
, buffer_damage
) },
566 { __DRI2_RENDERER_QUERY
, 1, offsetof(struct dri2_egl_display
, rendererQuery
) },
567 { __DRI2_INTEROP
, 1, offsetof(struct dri2_egl_display
, interop
) },
568 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
569 { __DRI2_FLUSH_CONTROL
, 1, offsetof(struct dri2_egl_display
, flush_control
) },
570 { __DRI2_BLOB
, 1, offsetof(struct dri2_egl_display
, blob
) },
571 { __DRI_MUTABLE_RENDER_BUFFER_DRIVER
, 1, offsetof(struct dri2_egl_display
, mutable_render_buffer
) },
576 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
577 const struct dri2_extension_match
*matches
,
578 const __DRIextension
**extensions
,
584 for (int i
= 0; extensions
[i
]; i
++) {
585 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
586 for (int j
= 0; matches
[j
].name
; j
++) {
587 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
588 extensions
[i
]->version
>= matches
[j
].version
) {
589 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
590 *(const __DRIextension
**) field
= extensions
[i
];
591 _eglLog(_EGL_INFO
, "found extension %s version %d",
592 extensions
[i
]->name
, extensions
[i
]->version
);
598 for (int j
= 0; matches
[j
].name
; j
++) {
599 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
600 if (*(const __DRIextension
**) field
== NULL
) {
602 _eglLog(_EGL_DEBUG
, "did not find optional extension %s version %d",
603 matches
[j
].name
, matches
[j
].version
);
605 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
606 matches
[j
].name
, matches
[j
].version
);
615 static const __DRIextension
**
616 dri2_open_driver(_EGLDisplay
*disp
)
618 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
619 static const char *search_path_vars
[] = {
620 "LIBGL_DRIVERS_PATH",
624 return loader_open_driver(dri2_dpy
->driver_name
,
630 dri2_load_driver_common(_EGLDisplay
*disp
,
631 const struct dri2_extension_match
*driver_extensions
)
633 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
634 const __DRIextension
**extensions
;
636 extensions
= dri2_open_driver(disp
);
640 if (!dri2_bind_extensions(dri2_dpy
, driver_extensions
, extensions
, false))
643 dri2_dpy
->driver_extensions
= extensions
;
645 dri2_bind_extensions(dri2_dpy
, optional_driver_extensions
, extensions
, true);
651 dri2_load_driver(_EGLDisplay
*disp
)
653 return dri2_load_driver_common(disp
, dri2_driver_extensions
);
657 dri2_load_driver_dri3(_EGLDisplay
*disp
)
659 return dri2_load_driver_common(disp
, dri3_driver_extensions
);
663 dri2_load_driver_swrast(_EGLDisplay
*disp
)
665 return dri2_load_driver_common(disp
, swrast_driver_extensions
);
669 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
671 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
672 unsigned int value
= 0;
674 if (!rendererQuery
||
675 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
682 dri2_query_driver_name(_EGLDisplay
*disp
)
684 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
685 return dri2_dpy
->driver_name
;
689 dri2_query_driver_config(_EGLDisplay
*disp
)
691 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
692 const __DRIconfigOptionsExtension
*ext
= dri2_dpy
->configOptions
;
694 if (ext
->base
.version
>= 2)
695 return ext
->getXml(dri2_dpy
->driver_name
);
697 return strdup(ext
->xml
);
702 dri2_setup_screen(_EGLDisplay
*disp
)
704 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
705 unsigned int api_mask
;
708 * EGL 1.5 specification defines the default value to 1. Moreover,
709 * eglSwapInterval() is required to clamp requested value to the supported
710 * range. Since the default value is implicitly assumed to be supported,
711 * use it as both minimum and maximum for the platforms that do not allow
712 * changing the interval. Platforms, which allow it (e.g. x11, wayland)
713 * override these values already.
715 dri2_dpy
->min_swap_interval
= 1;
716 dri2_dpy
->max_swap_interval
= 1;
717 dri2_dpy
->default_swap_interval
= 1;
719 if (dri2_dpy
->image_driver
) {
720 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
721 } else if (dri2_dpy
->dri2
) {
722 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
724 assert(dri2_dpy
->swrast
);
725 api_mask
= 1 << __DRI_API_OPENGL
|
726 1 << __DRI_API_GLES
|
727 1 << __DRI_API_GLES2
|
728 1 << __DRI_API_GLES3
;
731 disp
->ClientAPIs
= 0;
732 if ((api_mask
& (1 <<__DRI_API_OPENGL
)) && _eglIsApiValid(EGL_OPENGL_API
))
733 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
734 if ((api_mask
& (1 << __DRI_API_GLES
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
735 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
736 if ((api_mask
& (1 << __DRI_API_GLES2
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
737 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
738 if ((api_mask
& (1 << __DRI_API_GLES3
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
739 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
741 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
742 disp
->Extensions
.KHR_no_config_context
= EGL_TRUE
;
743 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
745 if (dri2_dpy
->configOptions
) {
746 disp
->Extensions
.MESA_query_driver
= EGL_TRUE
;
749 /* Report back to EGL the bitmask of priorities supported */
750 disp
->Extensions
.IMG_context_priority
=
751 dri2_renderer_query_integer(dri2_dpy
,
752 __DRI2_RENDERER_HAS_CONTEXT_PRIORITY
);
754 disp
->Extensions
.EXT_pixel_format_float
= EGL_TRUE
;
756 if (dri2_renderer_query_integer(dri2_dpy
,
757 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
758 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
760 if (dri2_dpy
->image_driver
||
761 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
762 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
763 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
765 if (dri2_dpy
->robustness
)
766 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
769 if (dri2_dpy
->no_error
)
770 disp
->Extensions
.KHR_create_context_no_error
= EGL_TRUE
;
772 if (dri2_dpy
->fence
) {
773 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
774 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
775 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
776 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
777 if (dri2_dpy
->fence
->base
.version
>= 2 &&
778 dri2_dpy
->fence
->get_capabilities
) {
779 unsigned capabilities
=
780 dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
);
781 disp
->Extensions
.ANDROID_native_fence_sync
=
782 (capabilities
& __DRI_FENCE_CAP_NATIVE_FD
) != 0;
787 disp
->Extensions
.ANDROID_blob_cache
= EGL_TRUE
;
789 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
791 if (dri2_dpy
->image
) {
792 if (dri2_dpy
->image
->base
.version
>= 10 &&
793 dri2_dpy
->image
->getCapabilities
!= NULL
) {
796 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
797 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
799 if (dri2_dpy
->image
->base
.version
>= 11)
800 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
802 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
803 if (dri2_dpy
->image
->base
.version
>= 11)
804 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
807 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
808 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
809 if (dri2_dpy
->image
->base
.version
>= 5 &&
810 dri2_dpy
->image
->createImageFromTexture
) {
811 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
812 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
814 if (dri2_renderer_query_integer(dri2_dpy
,
815 __DRI2_RENDERER_HAS_TEXTURE_3D
))
816 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
819 if (dri2_dpy
->image
->base
.version
>= 8 &&
820 dri2_dpy
->image
->createImageFromDmaBufs
) {
821 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
823 if (dri2_dpy
->image
->base
.version
>= 15 &&
824 dri2_dpy
->image
->createImageFromDmaBufs2
&&
825 dri2_dpy
->image
->queryDmaBufFormats
&&
826 dri2_dpy
->image
->queryDmaBufModifiers
) {
827 disp
->Extensions
.EXT_image_dma_buf_import_modifiers
= EGL_TRUE
;
832 if (dri2_dpy
->flush_control
)
833 disp
->Extensions
.KHR_context_flush_control
= EGL_TRUE
;
835 if (dri2_dpy
->buffer_damage
&& dri2_dpy
->buffer_damage
->set_damage_region
)
836 disp
->Extensions
.KHR_partial_update
= EGL_TRUE
;
840 dri2_setup_swap_interval(_EGLDisplay
*disp
, int max_swap_interval
)
842 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
843 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
845 /* Allow driconf to override applications.*/
846 if (dri2_dpy
->config
)
847 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
848 "vblank_mode", &vblank_mode
);
849 switch (vblank_mode
) {
850 case DRI_CONF_VBLANK_NEVER
:
851 dri2_dpy
->min_swap_interval
= 0;
852 dri2_dpy
->max_swap_interval
= 0;
853 dri2_dpy
->default_swap_interval
= 0;
855 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
856 dri2_dpy
->min_swap_interval
= 1;
857 dri2_dpy
->max_swap_interval
= max_swap_interval
;
858 dri2_dpy
->default_swap_interval
= 1;
860 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
861 dri2_dpy
->min_swap_interval
= 0;
862 dri2_dpy
->max_swap_interval
= max_swap_interval
;
863 dri2_dpy
->default_swap_interval
= 0;
866 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
867 dri2_dpy
->min_swap_interval
= 0;
868 dri2_dpy
->max_swap_interval
= max_swap_interval
;
869 dri2_dpy
->default_swap_interval
= 1;
874 /* All platforms but DRM call this function to create the screen and populate
875 * the driver_configs. DRM inherits that information from its display - GBM.
878 dri2_create_screen(_EGLDisplay
*disp
)
880 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
882 if (dri2_dpy
->image_driver
) {
883 dri2_dpy
->dri_screen
=
884 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
885 dri2_dpy
->loader_extensions
,
886 dri2_dpy
->driver_extensions
,
887 &dri2_dpy
->driver_configs
,
889 } else if (dri2_dpy
->dri2
) {
890 if (dri2_dpy
->dri2
->base
.version
>= 4) {
891 dri2_dpy
->dri_screen
=
892 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
893 dri2_dpy
->loader_extensions
,
894 dri2_dpy
->driver_extensions
,
895 &dri2_dpy
->driver_configs
, disp
);
897 dri2_dpy
->dri_screen
=
898 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
899 dri2_dpy
->loader_extensions
,
900 &dri2_dpy
->driver_configs
, disp
);
903 assert(dri2_dpy
->swrast
);
904 if (dri2_dpy
->swrast
->base
.version
>= 4) {
905 dri2_dpy
->dri_screen
=
906 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->loader_extensions
,
907 dri2_dpy
->driver_extensions
,
908 &dri2_dpy
->driver_configs
, disp
);
910 dri2_dpy
->dri_screen
=
911 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->loader_extensions
,
912 &dri2_dpy
->driver_configs
, disp
);
916 if (dri2_dpy
->dri_screen
== NULL
) {
917 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
921 dri2_dpy
->own_dri_screen
= true;
926 dri2_setup_extensions(_EGLDisplay
*disp
)
928 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
929 const struct dri2_extension_match
*mandatory_core_extensions
;
930 const __DRIextension
**extensions
;
932 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
934 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
)
935 mandatory_core_extensions
= dri2_core_extensions
;
937 mandatory_core_extensions
= swrast_core_extensions
;
939 if (!dri2_bind_extensions(dri2_dpy
, mandatory_core_extensions
, extensions
, false))
942 #ifdef HAVE_DRI3_MODIFIERS
943 dri2_dpy
->multibuffers_available
=
944 (dri2_dpy
->dri3_major_version
> 1 || (dri2_dpy
->dri3_major_version
== 1 &&
945 dri2_dpy
->dri3_minor_version
>= 2)) &&
946 (dri2_dpy
->present_major_version
> 1 || (dri2_dpy
->present_major_version
== 1 &&
947 dri2_dpy
->present_minor_version
>= 2)) &&
948 (dri2_dpy
->image
&& dri2_dpy
->image
->base
.version
>= 15);
951 dri2_bind_extensions(dri2_dpy
, optional_core_extensions
, extensions
, true);
956 * Called via eglInitialize(), GLX_drv->API.Initialize().
958 * This must be guaranteed to be called exactly once, even if eglInitialize is
959 * called many times (without a eglTerminate in between).
962 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
964 EGLBoolean ret
= EGL_FALSE
;
965 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
967 /* In the case where the application calls eglMakeCurrent(context1),
968 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
969 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
970 * initialized, as we need it to be able to free context1 correctly.
972 * It would probably be safest to forcibly release the display with
973 * dri2_display_release, to make sure the display is reinitialized correctly.
974 * However, the EGL spec states that we need to keep a reference to the
975 * current context (so we cannot call dri2_make_current(NULL)), and therefore
976 * we would leak context1 as we would be missing the old display connection
977 * to free it up correctly.
980 dri2_dpy
->ref_count
++;
984 loader_set_logger(_eglLog
);
986 switch (disp
->Platform
) {
987 case _EGL_PLATFORM_SURFACELESS
:
988 ret
= dri2_initialize_surfaceless(drv
, disp
);
990 case _EGL_PLATFORM_DEVICE
:
991 ret
= dri2_initialize_device(drv
, disp
);
993 case _EGL_PLATFORM_X11
:
994 ret
= dri2_initialize_x11(drv
, disp
);
996 case _EGL_PLATFORM_DRM
:
997 ret
= dri2_initialize_drm(drv
, disp
);
999 case _EGL_PLATFORM_WAYLAND
:
1000 ret
= dri2_initialize_wayland(drv
, disp
);
1002 case _EGL_PLATFORM_ANDROID
:
1003 ret
= dri2_initialize_android(drv
, disp
);
1006 unreachable("Callers ensure we cannot get here.");
1013 dri2_dpy
= dri2_egl_display(disp
);
1014 dri2_dpy
->ref_count
++;
1020 * Decrement display reference count, and free up display if necessary.
1023 dri2_display_release(_EGLDisplay
*disp
)
1025 struct dri2_egl_display
*dri2_dpy
;
1030 dri2_dpy
= dri2_egl_display(disp
);
1032 assert(dri2_dpy
->ref_count
> 0);
1033 dri2_dpy
->ref_count
--;
1035 if (dri2_dpy
->ref_count
> 0)
1038 _eglCleanupDisplay(disp
);
1039 dri2_display_destroy(disp
);
1043 dri2_display_destroy(_EGLDisplay
*disp
)
1045 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1047 if (dri2_dpy
->own_dri_screen
) {
1048 if (dri2_dpy
->vtbl
&& dri2_dpy
->vtbl
->close_screen_notify
)
1049 dri2_dpy
->vtbl
->close_screen_notify(disp
);
1050 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1052 if (dri2_dpy
->fd
>= 0)
1053 close(dri2_dpy
->fd
);
1054 if (dri2_dpy
->driver
)
1055 dlclose(dri2_dpy
->driver
);
1056 free(dri2_dpy
->driver_name
);
1058 #ifdef HAVE_WAYLAND_PLATFORM
1059 free(dri2_dpy
->device_name
);
1062 switch (disp
->Platform
) {
1063 case _EGL_PLATFORM_X11
:
1064 dri2_teardown_x11(dri2_dpy
);
1066 case _EGL_PLATFORM_DRM
:
1067 dri2_teardown_drm(dri2_dpy
);
1069 case _EGL_PLATFORM_WAYLAND
:
1070 dri2_teardown_wayland(dri2_dpy
);
1073 /* TODO: add teardown for other platforms */
1077 /* The drm platform does not create the screen/driver_configs but reuses
1078 * the ones from the gbm device. As such the gbm itself is responsible
1081 if (disp
->Platform
!= _EGL_PLATFORM_DRM
&& dri2_dpy
->driver_configs
) {
1082 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
1083 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
1084 free(dri2_dpy
->driver_configs
);
1087 disp
->DriverData
= NULL
;
1091 dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface
*dri2_surf
,
1092 unsigned int att
, unsigned int format
)
1094 struct dri2_egl_display
*dri2_dpy
=
1095 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1097 if (att
>= ARRAY_SIZE(dri2_surf
->local_buffers
))
1100 if (!dri2_surf
->local_buffers
[att
]) {
1101 dri2_surf
->local_buffers
[att
] =
1102 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
, att
, format
,
1103 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
1106 return dri2_surf
->local_buffers
[att
];
1110 dri2_egl_surface_free_local_buffers(struct dri2_egl_surface
*dri2_surf
)
1112 struct dri2_egl_display
*dri2_dpy
=
1113 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1115 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->local_buffers
); i
++) {
1116 if (dri2_surf
->local_buffers
[i
]) {
1117 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
1118 dri2_surf
->local_buffers
[i
]);
1119 dri2_surf
->local_buffers
[i
] = NULL
;
1125 * Called via eglTerminate(), drv->API.Terminate().
1127 * This must be guaranteed to be called exactly once, even if eglTerminate is
1128 * called many times (without a eglInitialize in between).
1131 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1133 /* Release all non-current Context/Surfaces. */
1134 _eglReleaseDisplayResources(drv
, disp
);
1136 dri2_display_release(disp
);
1142 * Set the error code after a call to
1143 * dri2_egl_display::dri2::createContextAttribs.
1146 dri2_create_context_attribs_error(int dri_error
)
1150 switch (dri_error
) {
1151 case __DRI_CTX_ERROR_SUCCESS
:
1154 case __DRI_CTX_ERROR_NO_MEMORY
:
1155 egl_error
= EGL_BAD_ALLOC
;
1158 /* From the EGL_KHR_create_context spec, section "Errors":
1160 * * If <config> does not support a client API context compatible
1161 * with the requested API major and minor version, [...] context flags,
1162 * and context reset notification behavior (for client API types where
1163 * these attributes are supported), then an EGL_BAD_MATCH error is
1166 * * If an OpenGL ES context is requested and the values for
1167 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1168 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1169 * is not defined, than an EGL_BAD_MATCH error is generated.
1171 * * If an OpenGL context is requested, the requested version is
1172 * greater than 3.2, and the value for attribute
1173 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1174 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1175 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1176 * one of these bits set; or if the implementation does not support
1177 * the requested profile, then an EGL_BAD_MATCH error is generated.
1179 case __DRI_CTX_ERROR_BAD_API
:
1180 case __DRI_CTX_ERROR_BAD_VERSION
:
1181 case __DRI_CTX_ERROR_BAD_FLAG
:
1182 egl_error
= EGL_BAD_MATCH
;
1185 /* From the EGL_KHR_create_context spec, section "Errors":
1187 * * If an attribute name or attribute value in <attrib_list> is not
1188 * recognized (including unrecognized bits in bitmask attributes),
1189 * then an EGL_BAD_ATTRIBUTE error is generated."
1191 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
1192 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
1193 egl_error
= EGL_BAD_ATTRIBUTE
;
1197 assert(!"unknown dri_error code");
1198 egl_error
= EGL_BAD_MATCH
;
1202 _eglError(egl_error
, "dri2_create_context");
1206 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1207 struct dri2_egl_display
*dri2_dpy
,
1208 uint32_t *ctx_attribs
,
1209 unsigned *num_attribs
)
1213 assert(*num_attribs
>= NUM_ATTRIBS
);
1215 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1216 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1217 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1218 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1220 if (dri2_ctx
->base
.Flags
!= 0 || dri2_ctx
->base
.NoError
) {
1221 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1222 * extension, don't even try to send it the robust-access flag.
1223 * It may explode. Instead, generate the required EGL error here.
1225 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1226 && !dri2_dpy
->robustness
) {
1227 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1231 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1232 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
|
1233 (dri2_ctx
->base
.NoError
? __DRI_CTX_FLAG_NO_ERROR
: 0);
1236 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1237 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1238 * extension, don't even try to send it a reset strategy. It may
1239 * explode. Instead, generate the required EGL error here.
1241 if (!dri2_dpy
->robustness
) {
1242 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1246 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1247 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1250 if (dri2_ctx
->base
.ContextPriority
!= EGL_CONTEXT_PRIORITY_MEDIUM_IMG
) {
1253 switch (dri2_ctx
->base
.ContextPriority
) {
1254 case EGL_CONTEXT_PRIORITY_HIGH_IMG
:
1255 val
= __DRI_CTX_PRIORITY_HIGH
;
1257 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG
:
1258 val
= __DRI_CTX_PRIORITY_MEDIUM
;
1260 case EGL_CONTEXT_PRIORITY_LOW_IMG
:
1261 val
= __DRI_CTX_PRIORITY_LOW
;
1264 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1268 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_PRIORITY
;
1269 ctx_attribs
[pos
++] = val
;
1272 if (dri2_ctx
->base
.ReleaseBehavior
== EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR
) {
1273 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR
;
1274 ctx_attribs
[pos
++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE
;
1283 * Called via eglCreateContext(), drv->API.CreateContext().
1285 static _EGLContext
*
1286 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1287 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1289 struct dri2_egl_context
*dri2_ctx
;
1290 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1291 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1292 __DRIcontext
*shared
=
1293 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1294 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1295 const __DRIconfig
*dri_config
;
1298 unsigned num_attribs
= NUM_ATTRIBS
;
1299 uint32_t ctx_attribs
[NUM_ATTRIBS
];
1303 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1305 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1309 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1312 /* The EGL_EXT_create_context_robustness spec says:
1314 * "Add to the eglCreateContext context creation errors: [...]
1316 * * If the reset notification behavior of <share_context> and the
1317 * newly created context are different then an EGL_BAD_MATCH error is
1320 if (share_list
&& share_list
->ResetNotificationStrategy
!=
1321 dri2_ctx
->base
.ResetNotificationStrategy
) {
1322 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1326 /* The EGL_KHR_create_context_no_error spec says:
1328 * "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
1329 * used to create <share_context> does not match the value of
1330 * EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
1332 if (share_list
&& share_list
->NoError
!= dri2_ctx
->base
.NoError
) {
1333 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1337 switch (dri2_ctx
->base
.ClientAPI
) {
1338 case EGL_OPENGL_ES_API
:
1339 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1341 api
= __DRI_API_GLES
;
1344 api
= __DRI_API_GLES2
;
1347 api
= __DRI_API_GLES3
;
1350 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1355 case EGL_OPENGL_API
:
1356 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1357 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1358 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1359 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1360 api
= __DRI_API_OPENGL_CORE
;
1361 else if (dri2_ctx
->base
.ClientMajorVersion
== 3 &&
1362 dri2_ctx
->base
.ClientMinorVersion
== 1)
1363 api
= __DRI_API_OPENGL_CORE
;
1365 api
= __DRI_API_OPENGL
;
1368 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1374 /* The config chosen here isn't necessarily
1375 * used for surfaces later.
1376 * A pixmap surface will use the single config.
1377 * This opportunity depends on disabling the
1378 * doubleBufferMode check in
1379 * src/mesa/main/context.c:check_compatible()
1381 if (dri2_config
->dri_config
[1][0])
1382 dri_config
= dri2_config
->dri_config
[1][0];
1384 dri_config
= dri2_config
->dri_config
[0][0];
1389 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1393 if (dri2_dpy
->image_driver
) {
1394 dri2_ctx
->dri_context
=
1395 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1403 dri2_create_context_attribs_error(error
);
1404 } else if (dri2_dpy
->dri2
) {
1405 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1406 dri2_ctx
->dri_context
=
1407 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1415 dri2_create_context_attribs_error(error
);
1417 dri2_ctx
->dri_context
=
1418 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1425 assert(dri2_dpy
->swrast
);
1426 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1427 dri2_ctx
->dri_context
=
1428 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1436 dri2_create_context_attribs_error(error
);
1438 dri2_ctx
->dri_context
=
1439 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1447 if (!dri2_ctx
->dri_context
)
1450 return &dri2_ctx
->base
;
1458 * Called via eglDestroyContext(), drv->API.DestroyContext().
1461 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1463 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1464 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1466 if (_eglPutContext(ctx
)) {
1467 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1475 dri2_init_surface(_EGLSurface
*surf
, _EGLDisplay
*disp
, EGLint type
,
1476 _EGLConfig
*conf
, const EGLint
*attrib_list
,
1477 EGLBoolean enable_out_fence
, void *native_surface
)
1479 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1480 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1482 dri2_surf
->out_fence_fd
= -1;
1483 dri2_surf
->enable_out_fence
= false;
1484 if (dri2_dpy
->fence
&& dri2_dpy
->fence
->base
.version
>= 2 &&
1485 dri2_dpy
->fence
->get_capabilities
&&
1486 (dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
) &
1487 __DRI_FENCE_CAP_NATIVE_FD
)) {
1488 dri2_surf
->enable_out_fence
= enable_out_fence
;
1491 return _eglInitSurface(surf
, disp
, type
, conf
, attrib_list
, native_surface
);
1495 dri2_surface_set_out_fence_fd( _EGLSurface
*surf
, int fence_fd
)
1497 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1499 if (dri2_surf
->out_fence_fd
>= 0)
1500 close(dri2_surf
->out_fence_fd
);
1502 dri2_surf
->out_fence_fd
= fence_fd
;
1506 dri2_fini_surface(_EGLSurface
*surf
)
1508 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1510 dri2_surface_set_out_fence_fd(surf
, -1);
1511 dri2_surf
->enable_out_fence
= false;
1515 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1517 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1519 if (!_eglPutSurface(surf
))
1522 return dri2_dpy
->vtbl
->destroy_surface(drv
, disp
, surf
);
1526 dri2_surf_update_fence_fd(_EGLContext
*ctx
,
1527 _EGLDisplay
*disp
, _EGLSurface
*surf
)
1529 __DRIcontext
*dri_ctx
= dri2_egl_context(ctx
)->dri_context
;
1530 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1531 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1535 if (!dri2_surf
->enable_out_fence
)
1538 fence
= dri2_dpy
->fence
->create_fence_fd(dri_ctx
, -1);
1540 fence_fd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
1542 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, fence
);
1544 dri2_surface_set_out_fence_fd(surf
, fence_fd
);
1548 dri2_create_drawable(struct dri2_egl_display
*dri2_dpy
,
1549 const __DRIconfig
*config
,
1550 struct dri2_egl_surface
*dri2_surf
,
1551 void *loaderPrivate
)
1553 __DRIcreateNewDrawableFunc createNewDrawable
;
1555 if (dri2_dpy
->image_driver
)
1556 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
1557 else if (dri2_dpy
->dri2
)
1558 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
1559 else if (dri2_dpy
->swrast
)
1560 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
1562 return _eglError(EGL_BAD_ALLOC
, "no createNewDrawable");
1564 dri2_surf
->dri_drawable
= createNewDrawable(dri2_dpy
->dri_screen
,
1565 config
, loaderPrivate
);
1566 if (dri2_surf
->dri_drawable
== NULL
)
1567 return _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
1573 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1576 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1577 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1579 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1580 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1581 _EGLDisplay
*old_disp
= NULL
;
1582 struct dri2_egl_display
*old_dri2_dpy
= NULL
;
1583 _EGLContext
*old_ctx
;
1584 _EGLSurface
*old_dsurf
, *old_rsurf
;
1585 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1586 __DRIdrawable
*ddraw
, *rdraw
;
1591 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1593 /* make new bindings */
1594 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1595 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1600 old_disp
= old_ctx
->Resource
.Display
;
1601 old_dri2_dpy
= dri2_egl_display(old_disp
);
1604 /* flush before context switch */
1608 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1609 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1610 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1613 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1616 dri2_surf_update_fence_fd(old_ctx
, disp
, old_dsurf
);
1618 /* Disable shared buffer mode */
1619 if (old_dsurf
&& _eglSurfaceInSharedBufferMode(old_dsurf
) &&
1620 old_dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1621 old_dri2_dpy
->vtbl
->set_shared_buffer_mode(old_disp
, old_dsurf
, false);
1624 dri2_dpy
->core
->unbindContext(old_cctx
);
1627 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1629 if (!unbind
&& !dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1630 /* undo the previous _eglBindContext */
1631 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1632 assert(&dri2_ctx
->base
== ctx
&&
1633 tmp_dsurf
== dsurf
&&
1634 tmp_rsurf
== rsurf
);
1636 if (old_dsurf
&& _eglSurfaceInSharedBufferMode(old_dsurf
) &&
1637 old_dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1638 old_dri2_dpy
->vtbl
->set_shared_buffer_mode(old_disp
, old_dsurf
, true);
1641 _eglPutSurface(dsurf
);
1642 _eglPutSurface(rsurf
);
1643 _eglPutContext(ctx
);
1645 _eglPutSurface(old_dsurf
);
1646 _eglPutSurface(old_rsurf
);
1647 _eglPutContext(old_ctx
);
1649 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1650 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1653 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1656 dri2_destroy_surface(drv
, disp
, old_dsurf
);
1657 dri2_destroy_surface(drv
, disp
, old_rsurf
);
1660 dri2_dpy
->ref_count
++;
1663 dri2_destroy_context(drv
, disp
, old_ctx
);
1664 dri2_display_release(old_disp
);
1667 if (dsurf
&& _eglSurfaceHasMutableRenderBuffer(dsurf
) &&
1668 dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1669 /* Always update the shared buffer mode. This is obviously needed when
1670 * the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When
1671 * EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the
1672 * case where external non-EGL API may have changed window's shared
1673 * buffer mode since we last saw it.
1675 bool mode
= (dsurf
->ActiveRenderBuffer
== EGL_SINGLE_BUFFER
);
1676 dri2_dpy
->vtbl
->set_shared_buffer_mode(disp
, dsurf
, mode
);
1683 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1685 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1687 return dri2_surf
->dri_drawable
;
1691 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1694 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1696 return _glapi_get_proc_address(procname
);
1700 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1701 _EGLConfig
*conf
, void *native_window
,
1702 const EGLint
*attrib_list
)
1704 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1705 return dri2_dpy
->vtbl
->create_window_surface(drv
, disp
, conf
, native_window
,
1710 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1711 _EGLConfig
*conf
, void *native_pixmap
,
1712 const EGLint
*attrib_list
)
1714 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1715 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, disp
, conf
, native_pixmap
,
1720 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1721 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1723 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1724 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, disp
, conf
, attrib_list
);
1728 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1731 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1732 if (!dri2_dpy
->vtbl
->swap_interval
)
1734 return dri2_dpy
->vtbl
->swap_interval(drv
, disp
, surf
, interval
);
1738 * Asks the client API to flush any rendering to the drawable so that we can
1739 * do our swapbuffers.
1742 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1744 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1745 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1747 if (dri2_dpy
->flush
) {
1748 if (dri2_dpy
->flush
->base
.version
>= 4) {
1749 /* We know there's a current context because:
1751 * "If surface is not bound to the calling thread’s current
1752 * context, an EGL_BAD_SURFACE error is generated."
1754 _EGLContext
*ctx
= _eglGetCurrentContext();
1755 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1757 /* From the EGL 1.4 spec (page 52):
1759 * "The contents of ancillary buffers are always undefined
1760 * after calling eglSwapBuffers."
1762 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1764 __DRI2_FLUSH_DRAWABLE
|
1765 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1766 __DRI2_THROTTLE_SWAPBUFFER
);
1768 dri2_dpy
->flush
->flush(dri_drawable
);
1774 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1776 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1777 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1778 _EGLContext
*ctx
= _eglGetCurrentContext();
1782 dri2_surf_update_fence_fd(ctx
, disp
, surf
);
1783 ret
= dri2_dpy
->vtbl
->swap_buffers(drv
, disp
, surf
);
1785 /* SwapBuffers marks the end of the frame; reset the damage region for
1786 * use again next time.
1788 if (ret
&& dri2_dpy
->buffer_damage
&&
1789 dri2_dpy
->buffer_damage
->set_damage_region
)
1790 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
1796 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1798 const EGLint
*rects
, EGLint n_rects
)
1800 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1801 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1802 _EGLContext
*ctx
= _eglGetCurrentContext();
1806 dri2_surf_update_fence_fd(ctx
, disp
, surf
);
1807 ret
= dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, disp
, surf
,
1810 /* SwapBuffers marks the end of the frame; reset the damage region for
1811 * use again next time.
1813 if (ret
&& dri2_dpy
->buffer_damage
&&
1814 dri2_dpy
->buffer_damage
->set_damage_region
)
1815 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
1821 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1822 EGLint numRects
, const EGLint
*rects
)
1824 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1825 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1828 ret
= dri2_dpy
->vtbl
->swap_buffers_region(drv
, disp
, surf
, numRects
, rects
);
1830 /* SwapBuffers marks the end of the frame; reset the damage region for
1831 * use again next time.
1833 if (ret
&& dri2_dpy
->buffer_damage
&&
1834 dri2_dpy
->buffer_damage
->set_damage_region
)
1835 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
1841 dri2_set_damage_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1842 EGLint
*rects
, EGLint n_rects
)
1844 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1845 __DRIdrawable
*drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1847 if (!dri2_dpy
->buffer_damage
|| !dri2_dpy
->buffer_damage
->set_damage_region
)
1850 dri2_dpy
->buffer_damage
->set_damage_region(drawable
, n_rects
, rects
);
1855 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1856 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1858 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1859 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, disp
, surf
, x
, y
, width
, height
);
1863 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1864 void *native_pixmap_target
)
1866 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1867 return dri2_dpy
->vtbl
->copy_buffers(drv
, disp
, surf
, native_pixmap_target
);
1871 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1873 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1874 return dri2_dpy
->vtbl
->query_buffer_age(drv
, disp
, surf
);
1878 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1880 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1881 _EGLSurface
*surf
= ctx
->DrawSurface
;
1882 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1886 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1887 * we need to copy fake to real here.*/
1889 if (dri2_dpy
->flush
!= NULL
)
1890 dri2_dpy
->flush
->flush(dri_drawable
);
1896 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1901 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1902 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1909 dri2_bind_tex_image(_EGLDriver
*drv
,
1910 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1912 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1913 struct dri2_egl_context
*dri2_ctx
;
1915 GLint format
, target
;
1916 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1918 ctx
= _eglGetCurrentContext();
1919 dri2_ctx
= dri2_egl_context(ctx
);
1921 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1924 switch (surf
->TextureFormat
) {
1925 case EGL_TEXTURE_RGB
:
1926 format
= __DRI_TEXTURE_FORMAT_RGB
;
1928 case EGL_TEXTURE_RGBA
:
1929 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1932 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1933 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1936 switch (surf
->TextureTarget
) {
1937 case EGL_TEXTURE_2D
:
1938 target
= GL_TEXTURE_2D
;
1941 target
= GL_TEXTURE_2D
;
1942 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1945 dri2_dpy
->tex_buffer
->setTexBuffer2(dri2_ctx
->dri_context
,
1953 dri2_release_tex_image(_EGLDriver
*drv
,
1954 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1956 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1957 struct dri2_egl_context
*dri2_ctx
;
1960 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1962 ctx
= _eglGetCurrentContext();
1963 dri2_ctx
= dri2_egl_context(ctx
);
1965 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1968 switch (surf
->TextureTarget
) {
1969 case EGL_TEXTURE_2D
:
1970 target
= GL_TEXTURE_2D
;
1973 assert(!"missing texture target");
1976 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1977 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1978 dri2_dpy
->tex_buffer
->releaseTexBuffer(dri2_ctx
->dri_context
,
1979 target
, dri_drawable
);
1986 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
,
1987 EGLenum target
, EGLClientBuffer buffer
,
1988 const EGLint
*attr_list
)
1990 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1991 return dri2_dpy
->vtbl
->create_image(drv
, disp
, ctx
, target
, buffer
,
1996 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1998 struct dri2_egl_image
*dri2_img
;
2000 if (dri_image
== NULL
) {
2001 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
2005 dri2_img
= malloc(sizeof *dri2_img
);
2007 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
2011 _eglInitImage(&dri2_img
->base
, disp
);
2013 dri2_img
->dri_image
= dri_image
;
2015 return &dri2_img
->base
;
2019 * Translate a DRI Image extension error code into an EGL error code.
2022 egl_error_from_dri_image_error(int dri_error
)
2024 switch (dri_error
) {
2025 case __DRI_IMAGE_ERROR_SUCCESS
:
2027 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
2028 return EGL_BAD_ALLOC
;
2029 case __DRI_IMAGE_ERROR_BAD_MATCH
:
2030 return EGL_BAD_MATCH
;
2031 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
2032 return EGL_BAD_PARAMETER
;
2033 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
2034 return EGL_BAD_ACCESS
;
2036 assert(!"unknown dri_error code");
2037 return EGL_BAD_ALLOC
;
2042 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2043 EGLClientBuffer buffer
,
2044 const EGLint
*attr_list
)
2046 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2047 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2048 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
2049 __DRIimage
*dri_image
;
2051 if (renderbuffer
== 0) {
2052 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2053 return EGL_NO_IMAGE_KHR
;
2056 if (!disp
->Extensions
.KHR_gl_renderbuffer_image
) {
2057 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2058 return EGL_NO_IMAGE_KHR
;
2061 if (dri2_dpy
->image
->base
.version
>= 17 &&
2062 dri2_dpy
->image
->createImageFromRenderbuffer2
) {
2063 unsigned error
= ~0;
2065 dri_image
= dri2_dpy
->image
->createImageFromRenderbuffer2(
2066 dri2_ctx
->dri_context
, renderbuffer
, NULL
, &error
);
2068 assert(!!dri_image
== (error
== __DRI_IMAGE_ERROR_SUCCESS
));
2071 _eglError(egl_error_from_dri_image_error(error
), "dri2_create_image_khr");
2072 return EGL_NO_IMAGE_KHR
;
2075 dri_image
= dri2_dpy
->image
->createImageFromRenderbuffer(
2076 dri2_ctx
->dri_context
, renderbuffer
, NULL
);
2078 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2079 return EGL_NO_IMAGE_KHR
;
2083 return dri2_create_image_from_dri(disp
, dri_image
);
2086 #ifdef HAVE_WAYLAND_PLATFORM
2088 /* This structure describes how a wl_buffer maps to one or more
2089 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
2090 * offsets and strides of the planes in the buffer. This table maps a
2091 * wl_drm format code to a description of the planes in the buffer
2092 * that lets us create a __DRIimage for each of the planes. */
2094 static const struct wl_drm_components_descriptor
{
2095 uint32_t dri_components
;
2098 } wl_drm_components
[] = {
2099 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
2100 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
2101 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
2102 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
2103 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
2107 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2108 EGLClientBuffer _buffer
,
2109 const EGLint
*attr_list
)
2111 struct wl_drm_buffer
*buffer
;
2112 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2113 const struct wl_drm_components_descriptor
*f
;
2114 __DRIimage
*dri_image
;
2115 _EGLImageAttribs attrs
;
2118 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
2119 (struct wl_resource
*) _buffer
);
2123 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2126 plane
= attrs
.PlaneWL
;
2127 f
= buffer
->driver_format
;
2128 if (plane
< 0 || plane
>= f
->nplanes
) {
2129 _eglError(EGL_BAD_PARAMETER
,
2130 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
2134 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
2135 if (dri_image
== NULL
&& plane
== 0)
2136 dri_image
= dri2_dpy
->image
->dupImage(buffer
->driver_buffer
, NULL
);
2137 if (dri_image
== NULL
) {
2138 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
2142 return dri2_create_image_from_dri(disp
, dri_image
);
2147 dri2_get_sync_values_chromium(_EGLDisplay
*disp
, _EGLSurface
*surf
,
2148 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
2151 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2152 return dri2_dpy
->vtbl
->get_sync_values(disp
, surf
, ust
, msc
, sbc
);
2156 * Set the error code after a call to
2157 * dri2_egl_image::dri_image::createImageFromTexture.
2160 dri2_create_image_khr_texture_error(int dri_error
)
2162 EGLint egl_error
= egl_error_from_dri_image_error(dri_error
);
2164 if (egl_error
!= EGL_SUCCESS
)
2165 _eglError(egl_error
, "dri2_create_image_khr_texture");
2169 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2171 EGLClientBuffer buffer
,
2172 const EGLint
*attr_list
)
2174 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2175 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2176 struct dri2_egl_image
*dri2_img
;
2177 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
2178 _EGLImageAttribs attrs
;
2184 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2185 return EGL_NO_IMAGE_KHR
;
2188 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2189 return EGL_NO_IMAGE_KHR
;
2192 case EGL_GL_TEXTURE_2D_KHR
:
2193 if (!disp
->Extensions
.KHR_gl_texture_2D_image
) {
2194 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2195 return EGL_NO_IMAGE_KHR
;
2198 gl_target
= GL_TEXTURE_2D
;
2200 case EGL_GL_TEXTURE_3D_KHR
:
2201 if (!disp
->Extensions
.KHR_gl_texture_3D_image
) {
2202 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2203 return EGL_NO_IMAGE_KHR
;
2206 depth
= attrs
.GLTextureZOffset
;
2207 gl_target
= GL_TEXTURE_3D
;
2209 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2210 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2211 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2212 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2213 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2214 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2215 if (!disp
->Extensions
.KHR_gl_texture_cubemap_image
) {
2216 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2217 return EGL_NO_IMAGE_KHR
;
2220 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
2221 gl_target
= GL_TEXTURE_CUBE_MAP
;
2224 unreachable("Unexpected target in dri2_create_image_khr_texture()");
2225 return EGL_NO_IMAGE_KHR
;
2228 dri2_img
= malloc(sizeof *dri2_img
);
2230 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2231 return EGL_NO_IMAGE_KHR
;
2234 _eglInitImage(&dri2_img
->base
, disp
);
2236 dri2_img
->dri_image
=
2237 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
2241 attrs
.GLTextureLevel
,
2244 dri2_create_image_khr_texture_error(error
);
2246 if (!dri2_img
->dri_image
) {
2248 return EGL_NO_IMAGE_KHR
;
2250 return &dri2_img
->base
;
2254 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
2255 EGLint attribute
, EGLint
*value
)
2257 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2258 if (!dri2_dpy
->vtbl
->query_surface
)
2259 return _eglQuerySurface(drv
, disp
, surf
, attribute
, value
);
2260 return dri2_dpy
->vtbl
->query_surface(drv
, disp
, surf
, attribute
, value
);
2263 static struct wl_buffer
*
2264 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2267 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2268 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, disp
, img
);
2273 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2274 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2276 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2277 EGLint format
, name
, pitch
;
2278 _EGLImageAttribs attrs
;
2279 __DRIimage
*dri_image
;
2281 name
= (EGLint
) (uintptr_t) buffer
;
2283 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2286 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
2287 attrs
.DRMBufferStrideMESA
<= 0) {
2288 _eglError(EGL_BAD_PARAMETER
,
2289 "bad width, height or stride");
2293 switch (attrs
.DRMBufferFormatMESA
) {
2294 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2295 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2296 pitch
= attrs
.DRMBufferStrideMESA
;
2299 _eglError(EGL_BAD_PARAMETER
,
2300 "dri2_create_image_khr: unsupported pixmap depth");
2305 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
2313 return dri2_create_image_from_dri(disp
, dri_image
);
2317 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
2322 * "Required attributes and their values are as follows:
2324 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
2326 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
2327 * by drm_fourcc.h and used as the pixel_format parameter of the
2328 * drm_mode_fb_cmd2 ioctl."
2332 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2333 * incomplete, EGL_BAD_PARAMETER is generated."
2335 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
2336 !attrs
->DMABufFourCC
.IsPresent
)
2337 return _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
2342 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
2343 * specified for a plane's pitch or offset isn't supported by EGL,
2344 * EGL_BAD_ACCESS is generated."
2346 for (unsigned i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
2347 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
2348 attrs
->DMABufPlanePitches
[i
].Value
<= 0)
2349 return _eglError(EGL_BAD_ACCESS
, "invalid pitch");
2353 * If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2354 * attribute values may be given.
2356 * This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2357 * EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2359 for (unsigned i
= 0; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2360 if (attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
!=
2361 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
)
2362 return _eglError(EGL_BAD_PARAMETER
, "modifier attribute lo or hi missing");
2365 /* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2366 * mandate it, we only accept the same modifier across all planes. */
2367 for (unsigned i
= 1; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2368 if (attrs
->DMABufPlaneFds
[i
].IsPresent
) {
2369 if ((attrs
->DMABufPlaneModifiersLo
[0].IsPresent
!=
2370 attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
) ||
2371 (attrs
->DMABufPlaneModifiersLo
[0].Value
!=
2372 attrs
->DMABufPlaneModifiersLo
[i
].Value
) ||
2373 (attrs
->DMABufPlaneModifiersHi
[0].Value
!=
2374 attrs
->DMABufPlaneModifiersHi
[i
].Value
))
2375 return _eglError(EGL_BAD_PARAMETER
, "modifier attributes not equal");
2382 /* Returns the total number of planes for the format or zero if it isn't a
2383 * valid fourcc format.
2386 dri2_num_fourcc_format_planes(EGLint format
)
2390 case DRM_FORMAT_RG88
:
2391 case DRM_FORMAT_GR88
:
2392 case DRM_FORMAT_R16
:
2393 case DRM_FORMAT_GR1616
:
2394 case DRM_FORMAT_RGB332
:
2395 case DRM_FORMAT_BGR233
:
2396 case DRM_FORMAT_XRGB4444
:
2397 case DRM_FORMAT_XBGR4444
:
2398 case DRM_FORMAT_RGBX4444
:
2399 case DRM_FORMAT_BGRX4444
:
2400 case DRM_FORMAT_ARGB4444
:
2401 case DRM_FORMAT_ABGR4444
:
2402 case DRM_FORMAT_RGBA4444
:
2403 case DRM_FORMAT_BGRA4444
:
2404 case DRM_FORMAT_XRGB1555
:
2405 case DRM_FORMAT_XBGR1555
:
2406 case DRM_FORMAT_RGBX5551
:
2407 case DRM_FORMAT_BGRX5551
:
2408 case DRM_FORMAT_ARGB1555
:
2409 case DRM_FORMAT_ABGR1555
:
2410 case DRM_FORMAT_RGBA5551
:
2411 case DRM_FORMAT_BGRA5551
:
2412 case DRM_FORMAT_RGB565
:
2413 case DRM_FORMAT_BGR565
:
2414 case DRM_FORMAT_RGB888
:
2415 case DRM_FORMAT_BGR888
:
2416 case DRM_FORMAT_XRGB8888
:
2417 case DRM_FORMAT_XBGR8888
:
2418 case DRM_FORMAT_RGBX8888
:
2419 case DRM_FORMAT_BGRX8888
:
2420 case DRM_FORMAT_ARGB8888
:
2421 case DRM_FORMAT_ABGR8888
:
2422 case DRM_FORMAT_RGBA8888
:
2423 case DRM_FORMAT_BGRA8888
:
2424 case DRM_FORMAT_XRGB2101010
:
2425 case DRM_FORMAT_XBGR2101010
:
2426 case DRM_FORMAT_RGBX1010102
:
2427 case DRM_FORMAT_BGRX1010102
:
2428 case DRM_FORMAT_ARGB2101010
:
2429 case DRM_FORMAT_ABGR2101010
:
2430 case DRM_FORMAT_RGBA1010102
:
2431 case DRM_FORMAT_BGRA1010102
:
2432 case DRM_FORMAT_XBGR16161616F
:
2433 case DRM_FORMAT_ABGR16161616F
:
2434 case DRM_FORMAT_YUYV
:
2435 case DRM_FORMAT_YVYU
:
2436 case DRM_FORMAT_UYVY
:
2437 case DRM_FORMAT_VYUY
:
2438 case DRM_FORMAT_AYUV
:
2439 case DRM_FORMAT_XYUV8888
:
2442 case DRM_FORMAT_NV12
:
2443 case DRM_FORMAT_NV21
:
2444 case DRM_FORMAT_NV16
:
2445 case DRM_FORMAT_NV61
:
2446 case DRM_FORMAT_P010
:
2447 case DRM_FORMAT_P012
:
2448 case DRM_FORMAT_P016
:
2451 case DRM_FORMAT_YUV410
:
2452 case DRM_FORMAT_YVU410
:
2453 case DRM_FORMAT_YUV411
:
2454 case DRM_FORMAT_YVU411
:
2455 case DRM_FORMAT_YUV420
:
2456 case DRM_FORMAT_YVU420
:
2457 case DRM_FORMAT_YUV422
:
2458 case DRM_FORMAT_YVU422
:
2459 case DRM_FORMAT_YUV444
:
2460 case DRM_FORMAT_YVU444
:
2468 /* Returns the total number of file descriptors. Zero indicates an error. */
2470 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
2472 unsigned plane_n
= dri2_num_fourcc_format_planes(attrs
->DMABufFourCC
.Value
);
2474 _eglError(EGL_BAD_MATCH
, "unknown drm fourcc format");
2478 for (unsigned i
= plane_n
; i
< DMA_BUF_MAX_PLANES
; i
++) {
2480 * The modifiers extension spec says:
2482 * "Modifiers may modify any attribute of a buffer import, including
2483 * but not limited to adding extra planes to a format which
2484 * otherwise does not have those planes. As an example, a modifier
2485 * may add a plane for an external compression buffer to a
2486 * single-plane format. The exact meaning and effect of any
2487 * modifier is canonically defined by drm_fourcc.h, not as part of
2490 if (attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
&&
2491 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
) {
2499 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2500 * incomplete, EGL_BAD_PARAMETER is generated."
2502 for (unsigned i
= 0; i
< plane_n
; ++i
) {
2503 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2504 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2505 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2506 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2512 * The spec also says:
2514 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2515 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2516 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2517 * or EGL_DMA_BUF_PLANE3_* attributes are specified."
2519 for (unsigned i
= plane_n
; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2520 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2521 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2522 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2523 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2532 dri2_query_dma_buf_formats(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2533 EGLint max
, EGLint
*formats
, EGLint
*count
)
2535 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2536 if (max
< 0 || (max
> 0 && formats
== NULL
))
2537 return _eglError(EGL_BAD_PARAMETER
, "invalid value for max count of formats");
2539 if (dri2_dpy
->image
->base
.version
< 15 ||
2540 dri2_dpy
->image
->queryDmaBufFormats
== NULL
)
2543 if (!dri2_dpy
->image
->queryDmaBufFormats(dri2_dpy
->dri_screen
, max
,
2548 /* Assert that all of the formats returned are actually fourcc formats.
2549 * Some day, if we want the internal interface function to be able to
2550 * return the fake fourcc formats defined in dri_interface.h, we'll have
2551 * to do something more clever here to pair the list down to just real
2552 * fourcc formats so that we don't leak the fake internal ones.
2554 for (int i
= 0; i
< *count
; i
++) {
2555 assert(dri2_num_fourcc_format_planes(formats
[i
]) > 0);
2563 dri2_query_dma_buf_modifiers(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint format
,
2564 EGLint max
, EGLuint64KHR
*modifiers
,
2565 EGLBoolean
*external_only
, EGLint
*count
)
2567 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2569 if (dri2_num_fourcc_format_planes(format
) == 0)
2570 return _eglError(EGL_BAD_PARAMETER
, "invalid fourcc format");
2573 return _eglError(EGL_BAD_PARAMETER
, "invalid value for max count of formats");
2575 if (max
> 0 && modifiers
== NULL
)
2576 return _eglError(EGL_BAD_PARAMETER
, "invalid modifiers array");
2578 if (dri2_dpy
->image
->base
.version
< 15 ||
2579 dri2_dpy
->image
->queryDmaBufModifiers
== NULL
)
2582 if (dri2_dpy
->image
->queryDmaBufModifiers(dri2_dpy
->dri_screen
, format
,
2584 (unsigned int *) external_only
,
2586 return _eglError(EGL_BAD_PARAMETER
, "invalid format");
2594 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2595 * EGL will take a reference to the dma_buf(s) which it will release at any
2596 * time while the EGLDisplay is initialized. It is the responsibility of the
2597 * application to close the dma_buf file descriptors."
2599 * Therefore we must never close or otherwise modify the file descriptors.
2602 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2603 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2605 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2607 _EGLImageAttribs attrs
;
2608 __DRIimage
*dri_image
;
2610 int fds
[DMA_BUF_MAX_PLANES
];
2611 int pitches
[DMA_BUF_MAX_PLANES
];
2612 int offsets
[DMA_BUF_MAX_PLANES
];
2614 bool has_modifier
= false;
2620 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2621 * error EGL_BAD_PARAMETER is generated."
2623 if (buffer
!= NULL
) {
2624 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2628 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2631 if (!dri2_check_dma_buf_attribs(&attrs
))
2634 num_fds
= dri2_check_dma_buf_format(&attrs
);
2638 for (unsigned i
= 0; i
< num_fds
; ++i
) {
2639 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2640 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2641 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2644 /* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2645 * will be present in attrs.DMABufPlaneModifiersLo[0] and
2646 * attrs.DMABufPlaneModifiersHi[0] */
2647 if (attrs
.DMABufPlaneModifiersLo
[0].IsPresent
) {
2648 modifier
= combine_u32_into_u64(attrs
.DMABufPlaneModifiersHi
[0].Value
,
2649 attrs
.DMABufPlaneModifiersLo
[0].Value
);
2650 has_modifier
= true;
2654 if (dri2_dpy
->image
->base
.version
< 15 ||
2655 dri2_dpy
->image
->createImageFromDmaBufs2
== NULL
) {
2656 _eglError(EGL_BAD_MATCH
, "unsupported dma_buf format modifier");
2657 return EGL_NO_IMAGE_KHR
;
2660 dri2_dpy
->image
->createImageFromDmaBufs2(dri2_dpy
->dri_screen
,
2661 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2662 modifier
, fds
, num_fds
, pitches
, offsets
,
2663 attrs
.DMABufYuvColorSpaceHint
.Value
,
2664 attrs
.DMABufSampleRangeHint
.Value
,
2665 attrs
.DMABufChromaHorizontalSiting
.Value
,
2666 attrs
.DMABufChromaVerticalSiting
.Value
,
2672 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2673 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2674 fds
, num_fds
, pitches
, offsets
,
2675 attrs
.DMABufYuvColorSpaceHint
.Value
,
2676 attrs
.DMABufSampleRangeHint
.Value
,
2677 attrs
.DMABufChromaHorizontalSiting
.Value
,
2678 attrs
.DMABufChromaVerticalSiting
.Value
,
2682 dri2_create_image_khr_texture_error(error
);
2685 return EGL_NO_IMAGE_KHR
;
2687 res
= dri2_create_image_from_dri(disp
, dri_image
);
2692 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2693 const EGLint
*attr_list
)
2695 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2696 struct dri2_egl_image
*dri2_img
;
2697 _EGLImageAttribs attrs
;
2698 unsigned int dri_use
, valid_mask
;
2704 _eglError(EGL_BAD_PARAMETER
, __func__
);
2705 return EGL_NO_IMAGE_KHR
;
2708 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2709 return EGL_NO_IMAGE_KHR
;
2711 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2712 _eglError(EGL_BAD_PARAMETER
, __func__
);
2713 return EGL_NO_IMAGE_KHR
;
2716 switch (attrs
.DRMBufferFormatMESA
) {
2717 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2718 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2721 _eglError(EGL_BAD_PARAMETER
, __func__
);
2722 return EGL_NO_IMAGE_KHR
;
2726 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2727 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2728 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2729 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2730 _eglError(EGL_BAD_PARAMETER
, __func__
);
2731 return EGL_NO_IMAGE_KHR
;
2735 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2736 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2737 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2738 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2739 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2740 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2742 dri2_img
= malloc(sizeof *dri2_img
);
2744 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2745 return EGL_NO_IMAGE_KHR
;
2748 _eglInitImage(&dri2_img
->base
, disp
);
2750 dri2_img
->dri_image
=
2751 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2752 attrs
.Width
, attrs
.Height
,
2753 format
, dri_use
, dri2_img
);
2754 if (dri2_img
->dri_image
== NULL
) {
2756 _eglError(EGL_BAD_ALLOC
, "dri2_create_drm_image_mesa");
2757 return EGL_NO_IMAGE_KHR
;
2760 return &dri2_img
->base
;
2764 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2765 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2767 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2768 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2772 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2773 __DRI_IMAGE_ATTRIB_NAME
, name
))
2774 return _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2777 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2778 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2781 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2782 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2788 * Checks if we can support EGL_MESA_image_dma_buf_export on this image.
2790 * The spec provides a boolean return for the driver to reject exporting for
2791 * basically any reason, but doesn't specify any particular error cases. For
2792 * now, we just fail if we don't have a DRM fourcc for the format.
2795 dri2_can_export_dma_buf_image(_EGLDisplay
*disp
, _EGLImage
*img
)
2797 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2798 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2801 if (!dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2802 __DRI_IMAGE_ATTRIB_FOURCC
, &fourcc
)) {
2810 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2812 EGLint
*fourcc
, EGLint
*nplanes
,
2813 EGLuint64KHR
*modifiers
)
2815 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2816 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2821 if (!dri2_can_export_dma_buf_image(disp
, img
))
2824 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2825 __DRI_IMAGE_ATTRIB_NUM_PLANES
, &num_planes
);
2827 *nplanes
= num_planes
;
2830 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2831 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2835 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
2838 query
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2839 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
2841 query
&= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2842 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
2845 modifier
= combine_u32_into_u64 (mod_hi
, mod_lo
);
2847 for (int i
= 0; i
< num_planes
; i
++)
2848 modifiers
[i
] = modifier
;
2855 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2856 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2858 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2859 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2864 if (!dri2_can_export_dma_buf_image(disp
, img
))
2867 /* EGL_MESA_image_dma_buf_export spec says:
2868 * "If the number of fds is less than the number of planes, then
2869 * subsequent fd slots should contain -1."
2872 /* Query nplanes so that we know how big the given array is. */
2873 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2874 __DRI_IMAGE_ATTRIB_NUM_PLANES
, &nplanes
);
2875 memset(fds
, -1, nplanes
* sizeof(int));
2878 /* rework later to provide multiple fds/strides/offsets */
2880 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2881 __DRI_IMAGE_ATTRIB_FD
, fds
);
2884 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2885 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2889 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2890 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2892 offsets
[0] = img_offset
;
2903 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2904 _EGLContext
*ctx
, EGLenum target
,
2905 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2910 case EGL_GL_TEXTURE_2D_KHR
:
2911 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2912 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2913 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2914 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2915 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2916 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2917 case EGL_GL_TEXTURE_3D_KHR
:
2918 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2919 case EGL_GL_RENDERBUFFER_KHR
:
2920 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2922 case EGL_DRM_BUFFER_MESA
:
2923 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2924 case EGL_LINUX_DMA_BUF_EXT
:
2925 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2927 #ifdef HAVE_WAYLAND_PLATFORM
2928 case EGL_WAYLAND_BUFFER_WL
:
2929 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2932 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2933 return EGL_NO_IMAGE_KHR
;
2938 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2940 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2941 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2945 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2951 #ifdef HAVE_WAYLAND_PLATFORM
2954 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2955 struct wl_drm_buffer
*buffer
)
2957 _EGLDisplay
*disp
= user_data
;
2958 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2960 int dri_components
= 0;
2963 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2972 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2984 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2986 buffer
->driver_format
= NULL
;
2987 for (int i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2988 if (wl_drm_components
[i
].dri_components
== dri_components
)
2989 buffer
->driver_format
= &wl_drm_components
[i
];
2991 if (buffer
->driver_format
== NULL
)
2992 dri2_dpy
->image
->destroyImage(img
);
2994 buffer
->driver_buffer
= img
;
2998 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
3000 _EGLDisplay
*disp
= user_data
;
3001 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3003 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
3007 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3008 struct wl_display
*wl_dpy
)
3010 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3011 const struct wayland_drm_callbacks wl_drm_callbacks
= {
3012 .authenticate
= (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
,
3013 .reference_buffer
= dri2_wl_reference_buffer
,
3014 .release_buffer
= dri2_wl_release_buffer
,
3015 .is_format_supported
= dri2_wl_is_format_supported
3022 if (dri2_dpy
->wl_server_drm
)
3025 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
3026 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
3027 dri2_dpy
->image
->base
.version
>= 7 &&
3028 dri2_dpy
->image
->createImageFromFds
!= NULL
)
3029 flags
|= WAYLAND_DRM_PRIME
;
3031 dri2_dpy
->wl_server_drm
=
3032 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
3033 &wl_drm_callbacks
, disp
, flags
);
3035 if (!dri2_dpy
->wl_server_drm
)
3038 #ifdef HAVE_DRM_PLATFORM
3039 /* We have to share the wl_drm instance with gbm, so gbm can convert
3040 * wl_buffers to gbm bos. */
3041 if (dri2_dpy
->gbm_dri
)
3042 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
3049 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3050 struct wl_display
*wl_dpy
)
3052 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3056 if (!dri2_dpy
->wl_server_drm
)
3059 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
3060 dri2_dpy
->wl_server_drm
= NULL
;
3066 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3067 struct wl_resource
*buffer_resource
,
3068 EGLint attribute
, EGLint
*value
)
3070 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3071 struct wl_drm_buffer
*buffer
;
3072 const struct wl_drm_components_descriptor
*format
;
3074 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
3078 format
= buffer
->driver_format
;
3079 switch (attribute
) {
3080 case EGL_TEXTURE_FORMAT
:
3081 *value
= format
->components
;
3084 *value
= buffer
->width
;
3087 *value
= buffer
->height
;
3096 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
3098 p_atomic_inc(&sync
->refcount
);
3102 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
3103 struct dri2_egl_sync
*dri2_sync
)
3105 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
3106 switch (dri2_sync
->base
.Type
) {
3107 case EGL_SYNC_REUSABLE_KHR
:
3108 cnd_destroy(&dri2_sync
->cond
);
3110 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3111 if (dri2_sync
->base
.SyncFd
!= EGL_NO_NATIVE_FENCE_FD_ANDROID
)
3112 close(dri2_sync
->base
.SyncFd
);
3118 if (dri2_sync
->fence
)
3119 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
3126 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3127 EGLenum type
, const EGLAttrib
*attrib_list
)
3129 _EGLContext
*ctx
= _eglGetCurrentContext();
3130 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3131 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3132 struct dri2_egl_sync
*dri2_sync
;
3134 pthread_condattr_t attr
;
3136 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
3138 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
3142 if (!_eglInitSync(&dri2_sync
->base
, disp
, type
, attrib_list
)) {
3148 case EGL_SYNC_FENCE_KHR
:
3149 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
3150 if (!dri2_sync
->fence
) {
3151 /* Why did it fail? DRI doesn't return an error code, so we emit
3152 * a generic EGL error that doesn't communicate user error.
3154 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
3160 case EGL_SYNC_CL_EVENT_KHR
:
3161 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
3162 dri2_dpy
->dri_screen
,
3163 dri2_sync
->base
.CLEvent
);
3164 /* this can only happen if the cl_event passed in is invalid. */
3165 if (!dri2_sync
->fence
) {
3166 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
3171 /* the initial status must be "signaled" if the cl_event is signaled */
3172 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
3173 dri2_sync
->fence
, 0, 0))
3174 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3177 case EGL_SYNC_REUSABLE_KHR
:
3178 /* intialize attr */
3179 ret
= pthread_condattr_init(&attr
);
3182 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3187 /* change clock attribute to CLOCK_MONOTONIC */
3188 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
3191 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3196 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
3199 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3204 /* initial status of reusable sync must be "unsignaled" */
3205 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
3208 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3209 if (dri2_dpy
->fence
->create_fence_fd
) {
3210 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence_fd(
3211 dri2_ctx
->dri_context
,
3212 dri2_sync
->base
.SyncFd
);
3214 if (!dri2_sync
->fence
) {
3215 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
3222 p_atomic_set(&dri2_sync
->refcount
, 1);
3223 return &dri2_sync
->base
;
3227 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3229 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3230 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3231 EGLint ret
= EGL_TRUE
;
3234 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
3235 * then unlock all threads possibly blocked by the reusable sync before
3238 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
3239 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
3240 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3241 /* unblock all threads currently blocked by sync */
3242 err
= cnd_broadcast(&dri2_sync
->cond
);
3245 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
3250 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
3256 dri2_dup_native_fence_fd(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3258 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3259 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3261 assert(sync
->Type
== EGL_SYNC_NATIVE_FENCE_ANDROID
);
3263 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
3264 /* try to retrieve the actual native fence fd.. if rendering is
3265 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
3267 sync
->SyncFd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
3271 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
3272 /* if native fence fd still not created, return an error: */
3273 _eglError(EGL_BAD_PARAMETER
, "eglDupNativeFenceFDANDROID");
3274 return EGL_NO_NATIVE_FENCE_FD_ANDROID
;
3277 return dup(sync
->SyncFd
);
3281 dri2_set_blob_cache_funcs(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3282 EGLSetBlobFuncANDROID set
,
3283 EGLGetBlobFuncANDROID get
)
3285 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3286 dri2_dpy
->blob
->set_cache_funcs(dri2_dpy
->dri_screen
,
3288 disp
->BlobCacheGet
);
3292 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
,
3293 EGLint flags
, EGLTime timeout
)
3295 _EGLContext
*ctx
= _eglGetCurrentContext();
3296 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3297 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3298 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3299 unsigned wait_flags
= 0;
3301 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
3303 /* The EGL_KHR_fence_sync spec states:
3305 * "If no context is current for the bound API,
3306 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
3308 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
3309 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
3311 /* the sync object should take a reference while waiting */
3312 dri2_egl_ref_sync(dri2_sync
);
3314 switch (sync
->Type
) {
3315 case EGL_SYNC_FENCE_KHR
:
3316 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3317 case EGL_SYNC_CL_EVENT_KHR
:
3318 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
3319 dri2_sync
->fence
, wait_flags
,
3321 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3323 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
3326 case EGL_SYNC_REUSABLE_KHR
:
3327 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
3328 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
3329 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
3333 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
3334 if (timeout
== EGL_FOREVER_KHR
) {
3335 mtx_lock(&dri2_sync
->mutex
);
3336 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
3337 mtx_unlock(&dri2_sync
->mutex
);
3339 /* if reusable sync has not been yet signaled */
3340 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
3341 /* timespecs for cnd_timedwait */
3342 struct timespec current
;
3343 struct timespec expire
;
3345 /* We override the clock to monotonic when creating the condition
3347 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
3349 /* calculating when to expire */
3350 expire
.tv_nsec
= timeout
% 1000000000L;
3351 expire
.tv_sec
= timeout
/ 1000000000L;
3353 expire
.tv_nsec
+= current
.tv_nsec
;
3354 expire
.tv_sec
+= current
.tv_sec
;
3356 /* expire.nsec now is a number between 0 and 1999999998 */
3357 if (expire
.tv_nsec
> 999999999L) {
3359 expire
.tv_nsec
-= 1000000000L;
3362 mtx_lock(&dri2_sync
->mutex
);
3363 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
3364 mtx_unlock(&dri2_sync
->mutex
);
3366 if (ret
== thrd_busy
) {
3367 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
3368 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
3370 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
3378 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
3384 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
,
3387 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3390 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
)
3391 return _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
3393 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
)
3394 return _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
3396 dri2_sync
->base
.SyncStatus
= mode
;
3398 if (mode
== EGL_SIGNALED_KHR
) {
3399 ret
= cnd_broadcast(&dri2_sync
->cond
);
3401 /* fail to broadcast */
3403 return _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
3410 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3412 _EGLContext
*ctx
= _eglGetCurrentContext();
3413 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3414 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3415 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3417 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
3418 dri2_sync
->fence
, 0);
3423 dri2_interop_query_device_info(_EGLDisplay
*disp
, _EGLContext
*ctx
,
3424 struct mesa_glinterop_device_info
*out
)
3426 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3427 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3429 if (!dri2_dpy
->interop
)
3430 return MESA_GLINTEROP_UNSUPPORTED
;
3432 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
3436 dri2_interop_export_object(_EGLDisplay
*disp
, _EGLContext
*ctx
,
3437 struct mesa_glinterop_export_in
*in
,
3438 struct mesa_glinterop_export_out
*out
)
3440 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3441 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3443 if (!dri2_dpy
->interop
)
3444 return MESA_GLINTEROP_UNSUPPORTED
;
3446 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
3450 * This is the main entrypoint into the driver, called by libEGL.
3451 * Gets an _EGLDriver object and init its dispatch table.
3454 _eglInitDriver(_EGLDriver
*dri2_drv
)
3456 dri2_drv
->API
.Initialize
= dri2_initialize
;
3457 dri2_drv
->API
.Terminate
= dri2_terminate
;
3458 dri2_drv
->API
.CreateContext
= dri2_create_context
;
3459 dri2_drv
->API
.DestroyContext
= dri2_destroy_context
;
3460 dri2_drv
->API
.MakeCurrent
= dri2_make_current
;
3461 dri2_drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
3462 dri2_drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
3463 dri2_drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
3464 dri2_drv
->API
.DestroySurface
= dri2_destroy_surface
;
3465 dri2_drv
->API
.GetProcAddress
= dri2_get_proc_address
;
3466 dri2_drv
->API
.WaitClient
= dri2_wait_client
;
3467 dri2_drv
->API
.WaitNative
= dri2_wait_native
;
3468 dri2_drv
->API
.BindTexImage
= dri2_bind_tex_image
;
3469 dri2_drv
->API
.ReleaseTexImage
= dri2_release_tex_image
;
3470 dri2_drv
->API
.SwapInterval
= dri2_swap_interval
;
3471 dri2_drv
->API
.SwapBuffers
= dri2_swap_buffers
;
3472 dri2_drv
->API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
3473 dri2_drv
->API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
3474 dri2_drv
->API
.SetDamageRegion
= dri2_set_damage_region
;
3475 dri2_drv
->API
.PostSubBufferNV
= dri2_post_sub_buffer
;
3476 dri2_drv
->API
.CopyBuffers
= dri2_copy_buffers
,
3477 dri2_drv
->API
.QueryBufferAge
= dri2_query_buffer_age
;
3478 dri2_drv
->API
.CreateImageKHR
= dri2_create_image
;
3479 dri2_drv
->API
.DestroyImageKHR
= dri2_destroy_image_khr
;
3480 dri2_drv
->API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
3481 dri2_drv
->API
.QuerySurface
= dri2_query_surface
;
3482 dri2_drv
->API
.QueryDriverName
= dri2_query_driver_name
;
3483 dri2_drv
->API
.QueryDriverConfig
= dri2_query_driver_config
;
3485 dri2_drv
->API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
3486 dri2_drv
->API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
3487 dri2_drv
->API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
3488 dri2_drv
->API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
3489 dri2_drv
->API
.QueryDmaBufFormatsEXT
= dri2_query_dma_buf_formats
;
3490 dri2_drv
->API
.QueryDmaBufModifiersEXT
= dri2_query_dma_buf_modifiers
;
3492 #ifdef HAVE_WAYLAND_PLATFORM
3493 dri2_drv
->API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
3494 dri2_drv
->API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
3495 dri2_drv
->API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
3497 dri2_drv
->API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
3498 dri2_drv
->API
.CreateSyncKHR
= dri2_create_sync
;
3499 dri2_drv
->API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
3500 dri2_drv
->API
.SignalSyncKHR
= dri2_signal_sync
;
3501 dri2_drv
->API
.WaitSyncKHR
= dri2_server_wait_sync
;
3502 dri2_drv
->API
.DestroySyncKHR
= dri2_destroy_sync
;
3503 dri2_drv
->API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
3504 dri2_drv
->API
.GLInteropExportObject
= dri2_interop_export_object
;
3505 dri2_drv
->API
.DupNativeFenceFDANDROID
= dri2_dup_native_fence_fd
;
3506 dri2_drv
->API
.SetBlobCacheFuncsANDROID
= dri2_set_blob_cache_funcs
;