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
,
193 dri2_get_dri_config(struct dri2_egl_config
*conf
, EGLint surface_type
,
196 const bool double_buffer
= surface_type
== EGL_WINDOW_BIT
;
197 const bool srgb
= colorspace
== EGL_GL_COLORSPACE_SRGB_KHR
;
199 return conf
->dri_config
[double_buffer
][srgb
];
203 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
205 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
208 if (!_eglMatchConfig(conf
, criteria
))
215 dri2_get_shifts_and_sizes(const __DRIcoreExtension
*core
,
216 const __DRIconfig
*config
, int *shifts
,
221 if (core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_SHIFT
, (unsigned int *)&shifts
[0])) {
222 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_SHIFT
, (unsigned int *)&shifts
[1]);
223 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_SHIFT
, (unsigned int *)&shifts
[2]);
224 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_SHIFT
, (unsigned int *)&shifts
[3]);
226 /* Driver isn't exposing shifts, so convert masks to shifts */
227 core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_MASK
, &mask
);
228 shifts
[0] = ffs(mask
) - 1;
229 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_MASK
, &mask
);
230 shifts
[1] = ffs(mask
) - 1;
231 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_MASK
, &mask
);
232 shifts
[2] = ffs(mask
) - 1;
233 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_MASK
, &mask
);
234 shifts
[3] = ffs(mask
) - 1;
237 core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_SIZE
, &sizes
[0]);
238 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_SIZE
, &sizes
[1]);
239 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_SIZE
, &sizes
[2]);
240 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_SIZE
, &sizes
[3]);
244 dri2_get_render_type_float(const __DRIcoreExtension
*core
,
245 const __DRIconfig
*config
,
248 unsigned int render_type
;
250 core
->getConfigAttrib(config
, __DRI_ATTRIB_RENDER_TYPE
, &render_type
);
251 *is_float
= (render_type
& __DRI_ATTRIB_FLOAT_BIT
) ? true : false;
254 struct dri2_egl_config
*
255 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
256 EGLint surface_type
, const EGLint
*attr_list
,
257 const int *rgba_shifts
, const unsigned int *rgba_sizes
)
259 struct dri2_egl_config
*conf
;
260 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
262 unsigned int attrib
, value
, double_buffer
;
264 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
265 int dri_shifts
[4] = { -1, -1, -1, -1 };
266 unsigned int dri_sizes
[4] = { 0, 0, 0, 0 };
267 _EGLConfig
*matching_config
;
268 EGLint num_configs
= 0;
271 _eglInitConfig(&base
, disp
, id
);
274 bind_to_texture_rgb
= 0;
275 bind_to_texture_rgba
= 0;
277 for (int i
= 0; i
< __DRI_ATTRIB_MAX
; ++i
) {
278 if (!dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
, &attrib
, &value
))
282 case __DRI_ATTRIB_RENDER_TYPE
:
283 if (value
& __DRI_ATTRIB_FLOAT_BIT
)
284 _eglSetConfigKey(&base
, EGL_COLOR_COMPONENT_TYPE_EXT
,
285 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT
);
286 if (value
& __DRI_ATTRIB_RGBA_BIT
)
287 value
= EGL_RGB_BUFFER
;
288 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
289 value
= EGL_LUMINANCE_BUFFER
;
292 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
295 case __DRI_ATTRIB_CONFIG_CAVEAT
:
296 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
297 value
= EGL_NON_CONFORMANT_CONFIG
;
298 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
299 value
= EGL_SLOW_CONFIG
;
302 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
305 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
306 bind_to_texture_rgb
= value
;
309 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
310 bind_to_texture_rgba
= value
;
313 case __DRI_ATTRIB_DOUBLE_BUFFER
:
314 double_buffer
= value
;
317 case __DRI_ATTRIB_RED_SIZE
:
318 dri_sizes
[0] = value
;
319 _eglSetConfigKey(&base
, EGL_RED_SIZE
, value
);
322 case __DRI_ATTRIB_RED_MASK
:
323 dri_shifts
[0] = ffs(value
) - 1;
326 case __DRI_ATTRIB_RED_SHIFT
:
327 dri_shifts
[0] = value
;
330 case __DRI_ATTRIB_GREEN_SIZE
:
331 dri_sizes
[1] = value
;
332 _eglSetConfigKey(&base
, EGL_GREEN_SIZE
, value
);
335 case __DRI_ATTRIB_GREEN_MASK
:
336 dri_shifts
[1] = ffs(value
) - 1;
339 case __DRI_ATTRIB_GREEN_SHIFT
:
340 dri_shifts
[1] = value
;
343 case __DRI_ATTRIB_BLUE_SIZE
:
344 dri_sizes
[2] = value
;
345 _eglSetConfigKey(&base
, EGL_BLUE_SIZE
, value
);
348 case __DRI_ATTRIB_BLUE_MASK
:
349 dri_shifts
[2] = ffs(value
) - 1;
352 case __DRI_ATTRIB_BLUE_SHIFT
:
353 dri_shifts
[2] = value
;
356 case __DRI_ATTRIB_ALPHA_SIZE
:
357 dri_sizes
[3] = value
;
358 _eglSetConfigKey(&base
, EGL_ALPHA_SIZE
, value
);
361 case __DRI_ATTRIB_ALPHA_MASK
:
362 dri_shifts
[3] = ffs(value
) - 1;
365 case __DRI_ATTRIB_ALPHA_SHIFT
:
366 dri_shifts
[3] = value
;
369 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
370 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
371 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
372 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
373 /* Don't expose visuals with the accumulation buffer. */
378 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
380 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
384 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH
:
385 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_WIDTH
,
386 _EGL_MAX_PBUFFER_WIDTH
);
388 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT
:
389 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_HEIGHT
,
390 _EGL_MAX_PBUFFER_HEIGHT
);
392 case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER
:
393 if (disp
->Extensions
.KHR_mutable_render_buffer
)
394 surface_type
|= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR
;
397 key
= dri2_to_egl_attribute_map
[attrib
];
399 _eglSetConfigKey(&base
, key
, value
);
405 for (int i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
406 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
408 if (rgba_shifts
&& memcmp(rgba_shifts
, dri_shifts
, sizeof(dri_shifts
)))
411 if (rgba_sizes
&& memcmp(rgba_sizes
, dri_sizes
, sizeof(dri_sizes
)))
414 base
.NativeRenderable
= EGL_TRUE
;
416 base
.SurfaceType
= surface_type
;
417 if (surface_type
& (EGL_PBUFFER_BIT
|
418 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
419 base
.BindToTextureRGB
= bind_to_texture_rgb
;
420 if (base
.AlphaSize
> 0)
421 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
425 surface_type
&= ~EGL_PIXMAP_BIT
;
428 /* No support for pbuffer + MSAA for now.
430 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
431 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
434 surface_type
&= ~EGL_PBUFFER_BIT
;
440 base
.RenderableType
= disp
->ClientAPIs
;
441 base
.Conformant
= disp
->ClientAPIs
;
443 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
444 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
446 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
447 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
451 config_id
= base
.ConfigID
;
452 base
.ConfigID
= EGL_DONT_CARE
;
453 base
.SurfaceType
= EGL_DONT_CARE
;
454 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
455 (_EGLArrayForEach
) dri2_match_config
, &base
);
457 if (num_configs
== 1) {
458 conf
= (struct dri2_egl_config
*) matching_config
;
460 if (!conf
->dri_config
[double_buffer
][srgb
])
461 conf
->dri_config
[double_buffer
][srgb
] = dri_config
;
463 /* a similar config type is already added (unlikely) => discard */
466 else if (num_configs
== 0) {
467 conf
= calloc(1, sizeof *conf
);
471 conf
->dri_config
[double_buffer
][srgb
] = dri_config
;
473 memcpy(&conf
->base
, &base
, sizeof base
);
474 conf
->base
.SurfaceType
= 0;
475 conf
->base
.ConfigID
= config_id
;
477 _eglLinkConfig(&conf
->base
);
480 unreachable("duplicates should not be possible");
484 conf
->base
.SurfaceType
|= surface_type
;
490 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
492 _EGLDisplay
*disp
= data
;
493 struct dri2_egl_image
*dri2_img
;
498 img
= _eglLookupImage(image
, disp
);
500 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
504 dri2_img
= dri2_egl_image(image
);
506 return dri2_img
->dri_image
;
509 const __DRIimageLookupExtension image_lookup_extension
= {
510 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
512 .lookupEGLImage
= dri2_lookup_egl_image
515 struct dri2_extension_match
{
521 static const struct dri2_extension_match dri3_driver_extensions
[] = {
522 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
523 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
527 static const struct dri2_extension_match dri2_driver_extensions
[] = {
528 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
529 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
533 static const struct dri2_extension_match dri2_core_extensions
[] = {
534 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
535 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
536 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
540 static const struct dri2_extension_match swrast_driver_extensions
[] = {
541 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
542 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
546 static const struct dri2_extension_match swrast_core_extensions
[] = {
547 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
551 static const struct dri2_extension_match optional_driver_extensions
[] = {
552 { __DRI_CONFIG_OPTIONS
, 1, offsetof(struct dri2_egl_display
, configOptions
) },
556 static const struct dri2_extension_match optional_core_extensions
[] = {
557 { __DRI2_ROBUSTNESS
, 1, offsetof(struct dri2_egl_display
, robustness
) },
558 { __DRI2_NO_ERROR
, 1, offsetof(struct dri2_egl_display
, no_error
) },
559 { __DRI2_CONFIG_QUERY
, 1, offsetof(struct dri2_egl_display
, config
) },
560 { __DRI2_FENCE
, 1, offsetof(struct dri2_egl_display
, fence
) },
561 { __DRI2_BUFFER_DAMAGE
, 1, offsetof(struct dri2_egl_display
, buffer_damage
) },
562 { __DRI2_RENDERER_QUERY
, 1, offsetof(struct dri2_egl_display
, rendererQuery
) },
563 { __DRI2_INTEROP
, 1, offsetof(struct dri2_egl_display
, interop
) },
564 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
565 { __DRI2_FLUSH_CONTROL
, 1, offsetof(struct dri2_egl_display
, flush_control
) },
566 { __DRI2_BLOB
, 1, offsetof(struct dri2_egl_display
, blob
) },
567 { __DRI_MUTABLE_RENDER_BUFFER_DRIVER
, 1, offsetof(struct dri2_egl_display
, mutable_render_buffer
) },
572 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
573 const struct dri2_extension_match
*matches
,
574 const __DRIextension
**extensions
,
580 for (int i
= 0; extensions
[i
]; i
++) {
581 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
582 for (int j
= 0; matches
[j
].name
; j
++) {
583 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
584 extensions
[i
]->version
>= matches
[j
].version
) {
585 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
586 *(const __DRIextension
**) field
= extensions
[i
];
587 _eglLog(_EGL_INFO
, "found extension %s version %d",
588 extensions
[i
]->name
, extensions
[i
]->version
);
594 for (int j
= 0; matches
[j
].name
; j
++) {
595 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
596 if (*(const __DRIextension
**) field
== NULL
) {
598 _eglLog(_EGL_DEBUG
, "did not find optional extension %s version %d",
599 matches
[j
].name
, matches
[j
].version
);
601 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
602 matches
[j
].name
, matches
[j
].version
);
611 static const __DRIextension
**
612 dri2_open_driver(_EGLDisplay
*disp
)
614 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
615 static const char *search_path_vars
[] = {
616 "LIBGL_DRIVERS_PATH",
620 return loader_open_driver(dri2_dpy
->driver_name
,
626 dri2_load_driver_common(_EGLDisplay
*disp
,
627 const struct dri2_extension_match
*driver_extensions
)
629 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
630 const __DRIextension
**extensions
;
632 extensions
= dri2_open_driver(disp
);
636 if (!dri2_bind_extensions(dri2_dpy
, driver_extensions
, extensions
, false))
639 dri2_dpy
->driver_extensions
= extensions
;
641 dri2_bind_extensions(dri2_dpy
, optional_driver_extensions
, extensions
, true);
647 dri2_load_driver(_EGLDisplay
*disp
)
649 return dri2_load_driver_common(disp
, dri2_driver_extensions
);
653 dri2_load_driver_dri3(_EGLDisplay
*disp
)
655 return dri2_load_driver_common(disp
, dri3_driver_extensions
);
659 dri2_load_driver_swrast(_EGLDisplay
*disp
)
661 return dri2_load_driver_common(disp
, swrast_driver_extensions
);
665 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
667 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
668 unsigned int value
= 0;
670 if (!rendererQuery
||
671 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
678 dri2_query_driver_name(_EGLDisplay
*disp
)
680 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
681 return dri2_dpy
->driver_name
;
685 dri2_query_driver_config(_EGLDisplay
*disp
)
687 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
688 const __DRIconfigOptionsExtension
*ext
= dri2_dpy
->configOptions
;
690 if (ext
->base
.version
>= 2)
691 return ext
->getXml(dri2_dpy
->driver_name
);
693 return strdup(ext
->xml
);
698 dri2_setup_screen(_EGLDisplay
*disp
)
700 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
701 unsigned int api_mask
;
704 * EGL 1.5 specification defines the default value to 1. Moreover,
705 * eglSwapInterval() is required to clamp requested value to the supported
706 * range. Since the default value is implicitly assumed to be supported,
707 * use it as both minimum and maximum for the platforms that do not allow
708 * changing the interval. Platforms, which allow it (e.g. x11, wayland)
709 * override these values already.
711 dri2_dpy
->min_swap_interval
= 1;
712 dri2_dpy
->max_swap_interval
= 1;
713 dri2_dpy
->default_swap_interval
= 1;
715 if (dri2_dpy
->image_driver
) {
716 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
717 } else if (dri2_dpy
->dri2
) {
718 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
720 assert(dri2_dpy
->swrast
);
721 api_mask
= 1 << __DRI_API_OPENGL
|
722 1 << __DRI_API_GLES
|
723 1 << __DRI_API_GLES2
|
724 1 << __DRI_API_GLES3
;
727 disp
->ClientAPIs
= 0;
728 if ((api_mask
& (1 <<__DRI_API_OPENGL
)) && _eglIsApiValid(EGL_OPENGL_API
))
729 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
730 if ((api_mask
& (1 << __DRI_API_GLES
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
731 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
732 if ((api_mask
& (1 << __DRI_API_GLES2
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
733 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
734 if ((api_mask
& (1 << __DRI_API_GLES3
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
735 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
737 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
738 disp
->Extensions
.KHR_no_config_context
= EGL_TRUE
;
739 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
741 if (dri2_dpy
->configOptions
) {
742 disp
->Extensions
.MESA_query_driver
= EGL_TRUE
;
745 /* Report back to EGL the bitmask of priorities supported */
746 disp
->Extensions
.IMG_context_priority
=
747 dri2_renderer_query_integer(dri2_dpy
,
748 __DRI2_RENDERER_HAS_CONTEXT_PRIORITY
);
750 disp
->Extensions
.EXT_pixel_format_float
= EGL_TRUE
;
752 if (dri2_renderer_query_integer(dri2_dpy
,
753 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
754 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
756 if (dri2_dpy
->image_driver
||
757 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
758 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
759 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
761 if (dri2_dpy
->robustness
)
762 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
765 if (dri2_dpy
->no_error
)
766 disp
->Extensions
.KHR_create_context_no_error
= EGL_TRUE
;
768 if (dri2_dpy
->fence
) {
769 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
770 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
771 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
772 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
773 if (dri2_dpy
->fence
->base
.version
>= 2 &&
774 dri2_dpy
->fence
->get_capabilities
) {
775 unsigned capabilities
=
776 dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
);
777 disp
->Extensions
.ANDROID_native_fence_sync
=
778 (capabilities
& __DRI_FENCE_CAP_NATIVE_FD
) != 0;
783 disp
->Extensions
.ANDROID_blob_cache
= EGL_TRUE
;
785 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
787 if (dri2_dpy
->image
) {
788 if (dri2_dpy
->image
->base
.version
>= 10 &&
789 dri2_dpy
->image
->getCapabilities
!= NULL
) {
792 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
793 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
795 if (dri2_dpy
->image
->base
.version
>= 11)
796 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
798 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
799 if (dri2_dpy
->image
->base
.version
>= 11)
800 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
803 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
804 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
805 if (dri2_dpy
->image
->base
.version
>= 5 &&
806 dri2_dpy
->image
->createImageFromTexture
) {
807 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
808 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
810 if (dri2_renderer_query_integer(dri2_dpy
,
811 __DRI2_RENDERER_HAS_TEXTURE_3D
))
812 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
815 if (dri2_dpy
->image
->base
.version
>= 8 &&
816 dri2_dpy
->image
->createImageFromDmaBufs
) {
817 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
819 if (dri2_dpy
->image
->base
.version
>= 15 &&
820 dri2_dpy
->image
->createImageFromDmaBufs2
&&
821 dri2_dpy
->image
->queryDmaBufFormats
&&
822 dri2_dpy
->image
->queryDmaBufModifiers
) {
823 disp
->Extensions
.EXT_image_dma_buf_import_modifiers
= EGL_TRUE
;
828 if (dri2_dpy
->flush_control
)
829 disp
->Extensions
.KHR_context_flush_control
= EGL_TRUE
;
831 if (dri2_dpy
->buffer_damage
&& dri2_dpy
->buffer_damage
->set_damage_region
)
832 disp
->Extensions
.KHR_partial_update
= EGL_TRUE
;
836 dri2_setup_swap_interval(_EGLDisplay
*disp
, int max_swap_interval
)
838 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
839 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
841 /* Allow driconf to override applications.*/
842 if (dri2_dpy
->config
)
843 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
844 "vblank_mode", &vblank_mode
);
845 switch (vblank_mode
) {
846 case DRI_CONF_VBLANK_NEVER
:
847 dri2_dpy
->min_swap_interval
= 0;
848 dri2_dpy
->max_swap_interval
= 0;
849 dri2_dpy
->default_swap_interval
= 0;
851 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
852 dri2_dpy
->min_swap_interval
= 1;
853 dri2_dpy
->max_swap_interval
= max_swap_interval
;
854 dri2_dpy
->default_swap_interval
= 1;
856 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
857 dri2_dpy
->min_swap_interval
= 0;
858 dri2_dpy
->max_swap_interval
= max_swap_interval
;
859 dri2_dpy
->default_swap_interval
= 0;
862 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
863 dri2_dpy
->min_swap_interval
= 0;
864 dri2_dpy
->max_swap_interval
= max_swap_interval
;
865 dri2_dpy
->default_swap_interval
= 1;
870 /* All platforms but DRM call this function to create the screen and populate
871 * the driver_configs. DRM inherits that information from its display - GBM.
874 dri2_create_screen(_EGLDisplay
*disp
)
876 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
878 if (dri2_dpy
->image_driver
) {
879 dri2_dpy
->dri_screen
=
880 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
881 dri2_dpy
->loader_extensions
,
882 dri2_dpy
->driver_extensions
,
883 &dri2_dpy
->driver_configs
,
885 } else if (dri2_dpy
->dri2
) {
886 if (dri2_dpy
->dri2
->base
.version
>= 4) {
887 dri2_dpy
->dri_screen
=
888 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
889 dri2_dpy
->loader_extensions
,
890 dri2_dpy
->driver_extensions
,
891 &dri2_dpy
->driver_configs
, disp
);
893 dri2_dpy
->dri_screen
=
894 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
895 dri2_dpy
->loader_extensions
,
896 &dri2_dpy
->driver_configs
, disp
);
899 assert(dri2_dpy
->swrast
);
900 if (dri2_dpy
->swrast
->base
.version
>= 4) {
901 dri2_dpy
->dri_screen
=
902 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->loader_extensions
,
903 dri2_dpy
->driver_extensions
,
904 &dri2_dpy
->driver_configs
, disp
);
906 dri2_dpy
->dri_screen
=
907 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->loader_extensions
,
908 &dri2_dpy
->driver_configs
, disp
);
912 if (dri2_dpy
->dri_screen
== NULL
) {
913 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
917 dri2_dpy
->own_dri_screen
= true;
922 dri2_setup_extensions(_EGLDisplay
*disp
)
924 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
925 const struct dri2_extension_match
*mandatory_core_extensions
;
926 const __DRIextension
**extensions
;
928 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
930 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
)
931 mandatory_core_extensions
= dri2_core_extensions
;
933 mandatory_core_extensions
= swrast_core_extensions
;
935 if (!dri2_bind_extensions(dri2_dpy
, mandatory_core_extensions
, extensions
, false))
938 #ifdef HAVE_DRI3_MODIFIERS
939 dri2_dpy
->multibuffers_available
=
940 (dri2_dpy
->dri3_major_version
> 1 || (dri2_dpy
->dri3_major_version
== 1 &&
941 dri2_dpy
->dri3_minor_version
>= 2)) &&
942 (dri2_dpy
->present_major_version
> 1 || (dri2_dpy
->present_major_version
== 1 &&
943 dri2_dpy
->present_minor_version
>= 2)) &&
944 (dri2_dpy
->image
&& dri2_dpy
->image
->base
.version
>= 15);
947 dri2_bind_extensions(dri2_dpy
, optional_core_extensions
, extensions
, true);
952 * Called via eglInitialize(), GLX_drv->API.Initialize().
954 * This must be guaranteed to be called exactly once, even if eglInitialize is
955 * called many times (without a eglTerminate in between).
958 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
960 EGLBoolean ret
= EGL_FALSE
;
961 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
963 /* In the case where the application calls eglMakeCurrent(context1),
964 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
965 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
966 * initialized, as we need it to be able to free context1 correctly.
968 * It would probably be safest to forcibly release the display with
969 * dri2_display_release, to make sure the display is reinitialized correctly.
970 * However, the EGL spec states that we need to keep a reference to the
971 * current context (so we cannot call dri2_make_current(NULL)), and therefore
972 * we would leak context1 as we would be missing the old display connection
973 * to free it up correctly.
976 dri2_dpy
->ref_count
++;
980 loader_set_logger(_eglLog
);
982 switch (disp
->Platform
) {
983 case _EGL_PLATFORM_SURFACELESS
:
984 ret
= dri2_initialize_surfaceless(drv
, disp
);
986 case _EGL_PLATFORM_DEVICE
:
987 ret
= dri2_initialize_device(drv
, disp
);
989 case _EGL_PLATFORM_X11
:
990 ret
= dri2_initialize_x11(drv
, disp
);
992 case _EGL_PLATFORM_DRM
:
993 ret
= dri2_initialize_drm(drv
, disp
);
995 case _EGL_PLATFORM_WAYLAND
:
996 ret
= dri2_initialize_wayland(drv
, disp
);
998 case _EGL_PLATFORM_ANDROID
:
999 ret
= dri2_initialize_android(drv
, disp
);
1002 unreachable("Callers ensure we cannot get here.");
1009 dri2_dpy
= dri2_egl_display(disp
);
1010 dri2_dpy
->ref_count
++;
1016 * Decrement display reference count, and free up display if necessary.
1019 dri2_display_release(_EGLDisplay
*disp
)
1021 struct dri2_egl_display
*dri2_dpy
;
1026 dri2_dpy
= dri2_egl_display(disp
);
1028 assert(dri2_dpy
->ref_count
> 0);
1029 dri2_dpy
->ref_count
--;
1031 if (dri2_dpy
->ref_count
> 0)
1034 _eglCleanupDisplay(disp
);
1035 dri2_display_destroy(disp
);
1039 dri2_display_destroy(_EGLDisplay
*disp
)
1041 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1043 if (dri2_dpy
->own_dri_screen
) {
1044 if (dri2_dpy
->vtbl
&& dri2_dpy
->vtbl
->close_screen_notify
)
1045 dri2_dpy
->vtbl
->close_screen_notify(disp
);
1046 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1048 if (dri2_dpy
->fd
>= 0)
1049 close(dri2_dpy
->fd
);
1050 if (dri2_dpy
->driver
)
1051 dlclose(dri2_dpy
->driver
);
1052 free(dri2_dpy
->driver_name
);
1054 #ifdef HAVE_WAYLAND_PLATFORM
1055 free(dri2_dpy
->device_name
);
1058 switch (disp
->Platform
) {
1059 case _EGL_PLATFORM_X11
:
1060 dri2_teardown_x11(dri2_dpy
);
1062 case _EGL_PLATFORM_DRM
:
1063 dri2_teardown_drm(dri2_dpy
);
1065 case _EGL_PLATFORM_WAYLAND
:
1066 dri2_teardown_wayland(dri2_dpy
);
1069 /* TODO: add teardown for other platforms */
1073 /* The drm platform does not create the screen/driver_configs but reuses
1074 * the ones from the gbm device. As such the gbm itself is responsible
1077 if (disp
->Platform
!= _EGL_PLATFORM_DRM
&& dri2_dpy
->driver_configs
) {
1078 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
1079 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
1080 free(dri2_dpy
->driver_configs
);
1083 disp
->DriverData
= NULL
;
1087 dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface
*dri2_surf
,
1088 unsigned int att
, unsigned int format
)
1090 struct dri2_egl_display
*dri2_dpy
=
1091 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1093 if (att
>= ARRAY_SIZE(dri2_surf
->local_buffers
))
1096 if (!dri2_surf
->local_buffers
[att
]) {
1097 dri2_surf
->local_buffers
[att
] =
1098 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
, att
, format
,
1099 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
1102 return dri2_surf
->local_buffers
[att
];
1106 dri2_egl_surface_free_local_buffers(struct dri2_egl_surface
*dri2_surf
)
1108 struct dri2_egl_display
*dri2_dpy
=
1109 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1111 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->local_buffers
); i
++) {
1112 if (dri2_surf
->local_buffers
[i
]) {
1113 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
1114 dri2_surf
->local_buffers
[i
]);
1115 dri2_surf
->local_buffers
[i
] = NULL
;
1121 * Called via eglTerminate(), drv->API.Terminate().
1123 * This must be guaranteed to be called exactly once, even if eglTerminate is
1124 * called many times (without a eglInitialize in between).
1127 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1129 /* Release all non-current Context/Surfaces. */
1130 _eglReleaseDisplayResources(drv
, disp
);
1132 dri2_display_release(disp
);
1138 * Set the error code after a call to
1139 * dri2_egl_display::dri2::createContextAttribs.
1142 dri2_create_context_attribs_error(int dri_error
)
1146 switch (dri_error
) {
1147 case __DRI_CTX_ERROR_SUCCESS
:
1150 case __DRI_CTX_ERROR_NO_MEMORY
:
1151 egl_error
= EGL_BAD_ALLOC
;
1154 /* From the EGL_KHR_create_context spec, section "Errors":
1156 * * If <config> does not support a client API context compatible
1157 * with the requested API major and minor version, [...] context flags,
1158 * and context reset notification behavior (for client API types where
1159 * these attributes are supported), then an EGL_BAD_MATCH error is
1162 * * If an OpenGL ES context is requested and the values for
1163 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1164 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1165 * is not defined, than an EGL_BAD_MATCH error is generated.
1167 * * If an OpenGL context is requested, the requested version is
1168 * greater than 3.2, and the value for attribute
1169 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1170 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1171 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1172 * one of these bits set; or if the implementation does not support
1173 * the requested profile, then an EGL_BAD_MATCH error is generated.
1175 case __DRI_CTX_ERROR_BAD_API
:
1176 case __DRI_CTX_ERROR_BAD_VERSION
:
1177 case __DRI_CTX_ERROR_BAD_FLAG
:
1178 egl_error
= EGL_BAD_MATCH
;
1181 /* From the EGL_KHR_create_context spec, section "Errors":
1183 * * If an attribute name or attribute value in <attrib_list> is not
1184 * recognized (including unrecognized bits in bitmask attributes),
1185 * then an EGL_BAD_ATTRIBUTE error is generated."
1187 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
1188 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
1189 egl_error
= EGL_BAD_ATTRIBUTE
;
1193 assert(!"unknown dri_error code");
1194 egl_error
= EGL_BAD_MATCH
;
1198 _eglError(egl_error
, "dri2_create_context");
1202 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1203 struct dri2_egl_display
*dri2_dpy
,
1204 uint32_t *ctx_attribs
,
1205 unsigned *num_attribs
)
1209 assert(*num_attribs
>= NUM_ATTRIBS
);
1211 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1212 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1213 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1214 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1216 if (dri2_ctx
->base
.Flags
!= 0 || dri2_ctx
->base
.NoError
) {
1217 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1218 * extension, don't even try to send it the robust-access flag.
1219 * It may explode. Instead, generate the required EGL error here.
1221 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1222 && !dri2_dpy
->robustness
) {
1223 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1227 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1228 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
|
1229 (dri2_ctx
->base
.NoError
? __DRI_CTX_FLAG_NO_ERROR
: 0);
1232 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1233 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1234 * extension, don't even try to send it a reset strategy. It may
1235 * explode. Instead, generate the required EGL error here.
1237 if (!dri2_dpy
->robustness
) {
1238 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1242 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1243 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1246 if (dri2_ctx
->base
.ContextPriority
!= EGL_CONTEXT_PRIORITY_MEDIUM_IMG
) {
1249 switch (dri2_ctx
->base
.ContextPriority
) {
1250 case EGL_CONTEXT_PRIORITY_HIGH_IMG
:
1251 val
= __DRI_CTX_PRIORITY_HIGH
;
1253 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG
:
1254 val
= __DRI_CTX_PRIORITY_MEDIUM
;
1256 case EGL_CONTEXT_PRIORITY_LOW_IMG
:
1257 val
= __DRI_CTX_PRIORITY_LOW
;
1260 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1264 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_PRIORITY
;
1265 ctx_attribs
[pos
++] = val
;
1268 if (dri2_ctx
->base
.ReleaseBehavior
== EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR
) {
1269 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR
;
1270 ctx_attribs
[pos
++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE
;
1279 * Called via eglCreateContext(), drv->API.CreateContext().
1281 static _EGLContext
*
1282 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1283 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1285 struct dri2_egl_context
*dri2_ctx
;
1286 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1287 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1288 __DRIcontext
*shared
=
1289 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1290 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1291 const __DRIconfig
*dri_config
;
1294 unsigned num_attribs
= NUM_ATTRIBS
;
1295 uint32_t ctx_attribs
[NUM_ATTRIBS
];
1299 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1301 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1305 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1308 /* The EGL_EXT_create_context_robustness spec says:
1310 * "Add to the eglCreateContext context creation errors: [...]
1312 * * If the reset notification behavior of <share_context> and the
1313 * newly created context are different then an EGL_BAD_MATCH error is
1316 if (share_list
&& share_list
->ResetNotificationStrategy
!=
1317 dri2_ctx
->base
.ResetNotificationStrategy
) {
1318 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1322 /* The EGL_KHR_create_context_no_error spec says:
1324 * "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
1325 * used to create <share_context> does not match the value of
1326 * EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
1328 if (share_list
&& share_list
->NoError
!= dri2_ctx
->base
.NoError
) {
1329 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1333 switch (dri2_ctx
->base
.ClientAPI
) {
1334 case EGL_OPENGL_ES_API
:
1335 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1337 api
= __DRI_API_GLES
;
1340 api
= __DRI_API_GLES2
;
1343 api
= __DRI_API_GLES3
;
1346 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1351 case EGL_OPENGL_API
:
1352 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1353 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1354 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1355 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1356 api
= __DRI_API_OPENGL_CORE
;
1357 else if (dri2_ctx
->base
.ClientMajorVersion
== 3 &&
1358 dri2_ctx
->base
.ClientMinorVersion
== 1)
1359 api
= __DRI_API_OPENGL_CORE
;
1361 api
= __DRI_API_OPENGL
;
1364 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1370 /* The config chosen here isn't necessarily
1371 * used for surfaces later.
1372 * A pixmap surface will use the single config.
1373 * This opportunity depends on disabling the
1374 * doubleBufferMode check in
1375 * src/mesa/main/context.c:check_compatible()
1377 if (dri2_config
->dri_config
[1][0])
1378 dri_config
= dri2_config
->dri_config
[1][0];
1380 dri_config
= dri2_config
->dri_config
[0][0];
1385 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1389 if (dri2_dpy
->image_driver
) {
1390 dri2_ctx
->dri_context
=
1391 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1399 dri2_create_context_attribs_error(error
);
1400 } else if (dri2_dpy
->dri2
) {
1401 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1402 dri2_ctx
->dri_context
=
1403 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1411 dri2_create_context_attribs_error(error
);
1413 dri2_ctx
->dri_context
=
1414 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1421 assert(dri2_dpy
->swrast
);
1422 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1423 dri2_ctx
->dri_context
=
1424 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1432 dri2_create_context_attribs_error(error
);
1434 dri2_ctx
->dri_context
=
1435 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1443 if (!dri2_ctx
->dri_context
)
1446 return &dri2_ctx
->base
;
1454 * Called via eglDestroyContext(), drv->API.DestroyContext().
1457 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1459 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1460 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1462 if (_eglPutContext(ctx
)) {
1463 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1471 dri2_init_surface(_EGLSurface
*surf
, _EGLDisplay
*disp
, EGLint type
,
1472 _EGLConfig
*conf
, const EGLint
*attrib_list
,
1473 EGLBoolean enable_out_fence
, void *native_surface
)
1475 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1476 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1478 dri2_surf
->out_fence_fd
= -1;
1479 dri2_surf
->enable_out_fence
= false;
1480 if (dri2_dpy
->fence
&& dri2_dpy
->fence
->base
.version
>= 2 &&
1481 dri2_dpy
->fence
->get_capabilities
&&
1482 (dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
) &
1483 __DRI_FENCE_CAP_NATIVE_FD
)) {
1484 dri2_surf
->enable_out_fence
= enable_out_fence
;
1487 return _eglInitSurface(surf
, disp
, type
, conf
, attrib_list
, native_surface
);
1491 dri2_surface_set_out_fence_fd( _EGLSurface
*surf
, int fence_fd
)
1493 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1495 if (dri2_surf
->out_fence_fd
>= 0)
1496 close(dri2_surf
->out_fence_fd
);
1498 dri2_surf
->out_fence_fd
= fence_fd
;
1502 dri2_fini_surface(_EGLSurface
*surf
)
1504 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1506 dri2_surface_set_out_fence_fd(surf
, -1);
1507 dri2_surf
->enable_out_fence
= false;
1511 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1513 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1515 if (!_eglPutSurface(surf
))
1518 return dri2_dpy
->vtbl
->destroy_surface(drv
, disp
, surf
);
1522 dri2_surf_update_fence_fd(_EGLContext
*ctx
,
1523 _EGLDisplay
*disp
, _EGLSurface
*surf
)
1525 __DRIcontext
*dri_ctx
= dri2_egl_context(ctx
)->dri_context
;
1526 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1527 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1531 if (!dri2_surf
->enable_out_fence
)
1534 fence
= dri2_dpy
->fence
->create_fence_fd(dri_ctx
, -1);
1536 fence_fd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
1538 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, fence
);
1540 dri2_surface_set_out_fence_fd(surf
, fence_fd
);
1544 dri2_create_drawable(struct dri2_egl_display
*dri2_dpy
,
1545 const __DRIconfig
*config
,
1546 struct dri2_egl_surface
*dri2_surf
,
1547 void *loaderPrivate
)
1549 __DRIcreateNewDrawableFunc createNewDrawable
;
1551 if (dri2_dpy
->image_driver
)
1552 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
1553 else if (dri2_dpy
->dri2
)
1554 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
1555 else if (dri2_dpy
->swrast
)
1556 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
1558 return _eglError(EGL_BAD_ALLOC
, "no createNewDrawable");
1560 dri2_surf
->dri_drawable
= createNewDrawable(dri2_dpy
->dri_screen
,
1561 config
, loaderPrivate
);
1562 if (dri2_surf
->dri_drawable
== NULL
)
1563 return _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
1569 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1572 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1573 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1575 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1576 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1577 _EGLDisplay
*old_disp
= NULL
;
1578 struct dri2_egl_display
*old_dri2_dpy
= NULL
;
1579 _EGLContext
*old_ctx
;
1580 _EGLSurface
*old_dsurf
, *old_rsurf
;
1581 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1582 __DRIdrawable
*ddraw
, *rdraw
;
1587 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1589 /* make new bindings */
1590 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1591 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1596 old_disp
= old_ctx
->Resource
.Display
;
1597 old_dri2_dpy
= dri2_egl_display(old_disp
);
1600 /* flush before context switch */
1604 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1605 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1606 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1609 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1612 dri2_surf_update_fence_fd(old_ctx
, disp
, old_dsurf
);
1614 /* Disable shared buffer mode */
1615 if (old_dsurf
&& _eglSurfaceInSharedBufferMode(old_dsurf
) &&
1616 old_dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1617 old_dri2_dpy
->vtbl
->set_shared_buffer_mode(old_disp
, old_dsurf
, false);
1620 dri2_dpy
->core
->unbindContext(old_cctx
);
1623 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1625 if (!unbind
&& !dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1626 /* undo the previous _eglBindContext */
1627 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1628 assert(&dri2_ctx
->base
== ctx
&&
1629 tmp_dsurf
== dsurf
&&
1630 tmp_rsurf
== rsurf
);
1632 if (old_dsurf
&& _eglSurfaceInSharedBufferMode(old_dsurf
) &&
1633 old_dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1634 old_dri2_dpy
->vtbl
->set_shared_buffer_mode(old_disp
, old_dsurf
, true);
1637 _eglPutSurface(dsurf
);
1638 _eglPutSurface(rsurf
);
1639 _eglPutContext(ctx
);
1641 _eglPutSurface(old_dsurf
);
1642 _eglPutSurface(old_rsurf
);
1643 _eglPutContext(old_ctx
);
1645 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1646 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1649 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1652 dri2_destroy_surface(drv
, disp
, old_dsurf
);
1653 dri2_destroy_surface(drv
, disp
, old_rsurf
);
1656 dri2_dpy
->ref_count
++;
1659 dri2_destroy_context(drv
, disp
, old_ctx
);
1660 dri2_display_release(old_disp
);
1663 if (dsurf
&& _eglSurfaceHasMutableRenderBuffer(dsurf
) &&
1664 dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1665 /* Always update the shared buffer mode. This is obviously needed when
1666 * the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When
1667 * EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the
1668 * case where external non-EGL API may have changed window's shared
1669 * buffer mode since we last saw it.
1671 bool mode
= (dsurf
->ActiveRenderBuffer
== EGL_SINGLE_BUFFER
);
1672 dri2_dpy
->vtbl
->set_shared_buffer_mode(disp
, dsurf
, mode
);
1679 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1681 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1683 return dri2_surf
->dri_drawable
;
1687 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1690 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1692 return _glapi_get_proc_address(procname
);
1696 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1697 _EGLConfig
*conf
, void *native_window
,
1698 const EGLint
*attrib_list
)
1700 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1701 return dri2_dpy
->vtbl
->create_window_surface(drv
, disp
, conf
, native_window
,
1706 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1707 _EGLConfig
*conf
, void *native_pixmap
,
1708 const EGLint
*attrib_list
)
1710 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1711 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, disp
, conf
, native_pixmap
,
1716 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1717 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1719 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1720 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, disp
, conf
, attrib_list
);
1724 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1727 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1728 if (!dri2_dpy
->vtbl
->swap_interval
)
1730 return dri2_dpy
->vtbl
->swap_interval(drv
, disp
, surf
, interval
);
1734 * Asks the client API to flush any rendering to the drawable so that we can
1735 * do our swapbuffers.
1738 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1740 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1741 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1743 if (dri2_dpy
->flush
) {
1744 if (dri2_dpy
->flush
->base
.version
>= 4) {
1745 /* We know there's a current context because:
1747 * "If surface is not bound to the calling thread’s current
1748 * context, an EGL_BAD_SURFACE error is generated."
1750 _EGLContext
*ctx
= _eglGetCurrentContext();
1751 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1753 /* From the EGL 1.4 spec (page 52):
1755 * "The contents of ancillary buffers are always undefined
1756 * after calling eglSwapBuffers."
1758 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1760 __DRI2_FLUSH_DRAWABLE
|
1761 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1762 __DRI2_THROTTLE_SWAPBUFFER
);
1764 dri2_dpy
->flush
->flush(dri_drawable
);
1770 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1772 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1773 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1774 _EGLContext
*ctx
= _eglGetCurrentContext();
1778 dri2_surf_update_fence_fd(ctx
, disp
, surf
);
1779 ret
= dri2_dpy
->vtbl
->swap_buffers(drv
, disp
, surf
);
1781 /* SwapBuffers marks the end of the frame; reset the damage region for
1782 * use again next time.
1784 if (ret
&& dri2_dpy
->buffer_damage
&&
1785 dri2_dpy
->buffer_damage
->set_damage_region
)
1786 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
1792 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1794 const EGLint
*rects
, EGLint n_rects
)
1796 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1797 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1798 _EGLContext
*ctx
= _eglGetCurrentContext();
1802 dri2_surf_update_fence_fd(ctx
, disp
, surf
);
1803 ret
= dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, disp
, surf
,
1806 /* SwapBuffers marks the end of the frame; reset the damage region for
1807 * use again next time.
1809 if (ret
&& dri2_dpy
->buffer_damage
&&
1810 dri2_dpy
->buffer_damage
->set_damage_region
)
1811 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
1817 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1818 EGLint numRects
, const EGLint
*rects
)
1820 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1821 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1824 ret
= dri2_dpy
->vtbl
->swap_buffers_region(drv
, disp
, surf
, numRects
, rects
);
1826 /* SwapBuffers marks the end of the frame; reset the damage region for
1827 * use again next time.
1829 if (ret
&& dri2_dpy
->buffer_damage
&&
1830 dri2_dpy
->buffer_damage
->set_damage_region
)
1831 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
1837 dri2_set_damage_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1838 EGLint
*rects
, EGLint n_rects
)
1840 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1841 __DRIdrawable
*drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1843 if (!dri2_dpy
->buffer_damage
|| !dri2_dpy
->buffer_damage
->set_damage_region
)
1846 dri2_dpy
->buffer_damage
->set_damage_region(drawable
, n_rects
, rects
);
1851 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1852 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1854 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1855 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, disp
, surf
, x
, y
, width
, height
);
1859 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1860 void *native_pixmap_target
)
1862 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1863 return dri2_dpy
->vtbl
->copy_buffers(drv
, disp
, surf
, native_pixmap_target
);
1867 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1869 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1870 return dri2_dpy
->vtbl
->query_buffer_age(drv
, disp
, surf
);
1874 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1876 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1877 _EGLSurface
*surf
= ctx
->DrawSurface
;
1878 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1882 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1883 * we need to copy fake to real here.*/
1885 if (dri2_dpy
->flush
!= NULL
)
1886 dri2_dpy
->flush
->flush(dri_drawable
);
1892 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1897 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1898 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1905 dri2_bind_tex_image(_EGLDriver
*drv
,
1906 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1908 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1909 struct dri2_egl_context
*dri2_ctx
;
1911 GLint format
, target
;
1912 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1914 ctx
= _eglGetCurrentContext();
1915 dri2_ctx
= dri2_egl_context(ctx
);
1917 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1920 switch (surf
->TextureFormat
) {
1921 case EGL_TEXTURE_RGB
:
1922 format
= __DRI_TEXTURE_FORMAT_RGB
;
1924 case EGL_TEXTURE_RGBA
:
1925 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1928 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1929 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1932 switch (surf
->TextureTarget
) {
1933 case EGL_TEXTURE_2D
:
1934 target
= GL_TEXTURE_2D
;
1937 target
= GL_TEXTURE_2D
;
1938 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1941 dri2_dpy
->tex_buffer
->setTexBuffer2(dri2_ctx
->dri_context
,
1949 dri2_release_tex_image(_EGLDriver
*drv
,
1950 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1952 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1953 struct dri2_egl_context
*dri2_ctx
;
1956 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1958 ctx
= _eglGetCurrentContext();
1959 dri2_ctx
= dri2_egl_context(ctx
);
1961 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1964 switch (surf
->TextureTarget
) {
1965 case EGL_TEXTURE_2D
:
1966 target
= GL_TEXTURE_2D
;
1969 assert(!"missing texture target");
1972 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1973 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1974 dri2_dpy
->tex_buffer
->releaseTexBuffer(dri2_ctx
->dri_context
,
1975 target
, dri_drawable
);
1982 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
,
1983 EGLenum target
, EGLClientBuffer buffer
,
1984 const EGLint
*attr_list
)
1986 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1987 return dri2_dpy
->vtbl
->create_image(drv
, disp
, ctx
, target
, buffer
,
1992 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1994 struct dri2_egl_image
*dri2_img
;
1996 if (dri_image
== NULL
) {
1997 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
2001 dri2_img
= malloc(sizeof *dri2_img
);
2003 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
2007 _eglInitImage(&dri2_img
->base
, disp
);
2009 dri2_img
->dri_image
= dri_image
;
2011 return &dri2_img
->base
;
2015 * Translate a DRI Image extension error code into an EGL error code.
2018 egl_error_from_dri_image_error(int dri_error
)
2020 switch (dri_error
) {
2021 case __DRI_IMAGE_ERROR_SUCCESS
:
2023 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
2024 return EGL_BAD_ALLOC
;
2025 case __DRI_IMAGE_ERROR_BAD_MATCH
:
2026 return EGL_BAD_MATCH
;
2027 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
2028 return EGL_BAD_PARAMETER
;
2029 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
2030 return EGL_BAD_ACCESS
;
2032 assert(!"unknown dri_error code");
2033 return EGL_BAD_ALLOC
;
2038 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2039 EGLClientBuffer buffer
,
2040 const EGLint
*attr_list
)
2042 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2043 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2044 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
2045 __DRIimage
*dri_image
;
2047 if (renderbuffer
== 0) {
2048 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2049 return EGL_NO_IMAGE_KHR
;
2052 if (!disp
->Extensions
.KHR_gl_renderbuffer_image
) {
2053 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2054 return EGL_NO_IMAGE_KHR
;
2057 if (dri2_dpy
->image
->base
.version
>= 17 &&
2058 dri2_dpy
->image
->createImageFromRenderbuffer2
) {
2059 unsigned error
= ~0;
2061 dri_image
= dri2_dpy
->image
->createImageFromRenderbuffer2(
2062 dri2_ctx
->dri_context
, renderbuffer
, NULL
, &error
);
2064 assert(!!dri_image
== (error
== __DRI_IMAGE_ERROR_SUCCESS
));
2067 _eglError(egl_error_from_dri_image_error(error
), "dri2_create_image_khr");
2068 return EGL_NO_IMAGE_KHR
;
2071 dri_image
= dri2_dpy
->image
->createImageFromRenderbuffer(
2072 dri2_ctx
->dri_context
, renderbuffer
, NULL
);
2074 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2075 return EGL_NO_IMAGE_KHR
;
2079 return dri2_create_image_from_dri(disp
, dri_image
);
2082 #ifdef HAVE_WAYLAND_PLATFORM
2084 /* This structure describes how a wl_buffer maps to one or more
2085 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
2086 * offsets and strides of the planes in the buffer. This table maps a
2087 * wl_drm format code to a description of the planes in the buffer
2088 * that lets us create a __DRIimage for each of the planes. */
2090 static const struct wl_drm_components_descriptor
{
2091 uint32_t dri_components
;
2094 } wl_drm_components
[] = {
2095 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
2096 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
2097 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
2098 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
2099 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
2103 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2104 EGLClientBuffer _buffer
,
2105 const EGLint
*attr_list
)
2107 struct wl_drm_buffer
*buffer
;
2108 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2109 const struct wl_drm_components_descriptor
*f
;
2110 __DRIimage
*dri_image
;
2111 _EGLImageAttribs attrs
;
2114 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
2115 (struct wl_resource
*) _buffer
);
2119 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2122 plane
= attrs
.PlaneWL
;
2123 f
= buffer
->driver_format
;
2124 if (plane
< 0 || plane
>= f
->nplanes
) {
2125 _eglError(EGL_BAD_PARAMETER
,
2126 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
2130 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
2131 if (dri_image
== NULL
&& plane
== 0)
2132 dri_image
= dri2_dpy
->image
->dupImage(buffer
->driver_buffer
, NULL
);
2133 if (dri_image
== NULL
) {
2134 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
2138 return dri2_create_image_from_dri(disp
, dri_image
);
2143 dri2_get_sync_values_chromium(_EGLDisplay
*disp
, _EGLSurface
*surf
,
2144 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
2147 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2148 return dri2_dpy
->vtbl
->get_sync_values(disp
, surf
, ust
, msc
, sbc
);
2152 * Set the error code after a call to
2153 * dri2_egl_image::dri_image::createImageFromTexture.
2156 dri2_create_image_khr_texture_error(int dri_error
)
2158 EGLint egl_error
= egl_error_from_dri_image_error(dri_error
);
2160 if (egl_error
!= EGL_SUCCESS
)
2161 _eglError(egl_error
, "dri2_create_image_khr_texture");
2165 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2167 EGLClientBuffer buffer
,
2168 const EGLint
*attr_list
)
2170 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2171 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2172 struct dri2_egl_image
*dri2_img
;
2173 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
2174 _EGLImageAttribs attrs
;
2180 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2181 return EGL_NO_IMAGE_KHR
;
2184 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2185 return EGL_NO_IMAGE_KHR
;
2188 case EGL_GL_TEXTURE_2D_KHR
:
2189 if (!disp
->Extensions
.KHR_gl_texture_2D_image
) {
2190 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2191 return EGL_NO_IMAGE_KHR
;
2194 gl_target
= GL_TEXTURE_2D
;
2196 case EGL_GL_TEXTURE_3D_KHR
:
2197 if (!disp
->Extensions
.KHR_gl_texture_3D_image
) {
2198 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2199 return EGL_NO_IMAGE_KHR
;
2202 depth
= attrs
.GLTextureZOffset
;
2203 gl_target
= GL_TEXTURE_3D
;
2205 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2206 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2207 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2208 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2209 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2210 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2211 if (!disp
->Extensions
.KHR_gl_texture_cubemap_image
) {
2212 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2213 return EGL_NO_IMAGE_KHR
;
2216 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
2217 gl_target
= GL_TEXTURE_CUBE_MAP
;
2220 unreachable("Unexpected target in dri2_create_image_khr_texture()");
2221 return EGL_NO_IMAGE_KHR
;
2224 dri2_img
= malloc(sizeof *dri2_img
);
2226 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2227 return EGL_NO_IMAGE_KHR
;
2230 _eglInitImage(&dri2_img
->base
, disp
);
2232 dri2_img
->dri_image
=
2233 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
2237 attrs
.GLTextureLevel
,
2240 dri2_create_image_khr_texture_error(error
);
2242 if (!dri2_img
->dri_image
) {
2244 return EGL_NO_IMAGE_KHR
;
2246 return &dri2_img
->base
;
2250 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
2251 EGLint attribute
, EGLint
*value
)
2253 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2254 if (!dri2_dpy
->vtbl
->query_surface
)
2255 return _eglQuerySurface(drv
, disp
, surf
, attribute
, value
);
2256 return dri2_dpy
->vtbl
->query_surface(drv
, disp
, surf
, attribute
, value
);
2259 static struct wl_buffer
*
2260 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2263 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2264 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, disp
, img
);
2269 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2270 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2272 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2273 EGLint format
, name
, pitch
;
2274 _EGLImageAttribs attrs
;
2275 __DRIimage
*dri_image
;
2277 name
= (EGLint
) (uintptr_t) buffer
;
2279 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2282 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
2283 attrs
.DRMBufferStrideMESA
<= 0) {
2284 _eglError(EGL_BAD_PARAMETER
,
2285 "bad width, height or stride");
2289 switch (attrs
.DRMBufferFormatMESA
) {
2290 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2291 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2292 pitch
= attrs
.DRMBufferStrideMESA
;
2295 _eglError(EGL_BAD_PARAMETER
,
2296 "dri2_create_image_khr: unsupported pixmap depth");
2300 if (dri2_dpy
->image
->base
.version
>= 18) {
2303 if (attrs
.ImageFlushExternal
)
2304 use
|= __DRI_IMAGE_USE_FLUSH_EXTERNAL
;
2307 dri2_dpy
->image
->createImageFromName2(dri2_dpy
->dri_screen
,
2308 attrs
.Width
, attrs
.Height
,
2309 format
, name
, pitch
, use
,
2313 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
2322 return dri2_create_image_from_dri(disp
, dri_image
);
2326 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
2331 * "Required attributes and their values are as follows:
2333 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
2335 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
2336 * by drm_fourcc.h and used as the pixel_format parameter of the
2337 * drm_mode_fb_cmd2 ioctl."
2341 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2342 * incomplete, EGL_BAD_PARAMETER is generated."
2344 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
2345 !attrs
->DMABufFourCC
.IsPresent
)
2346 return _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
2351 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
2352 * specified for a plane's pitch or offset isn't supported by EGL,
2353 * EGL_BAD_ACCESS is generated."
2355 for (unsigned i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
2356 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
2357 attrs
->DMABufPlanePitches
[i
].Value
<= 0)
2358 return _eglError(EGL_BAD_ACCESS
, "invalid pitch");
2362 * If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2363 * attribute values may be given.
2365 * This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2366 * EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2368 for (unsigned i
= 0; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2369 if (attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
!=
2370 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
)
2371 return _eglError(EGL_BAD_PARAMETER
, "modifier attribute lo or hi missing");
2374 /* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2375 * mandate it, we only accept the same modifier across all planes. */
2376 for (unsigned i
= 1; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2377 if (attrs
->DMABufPlaneFds
[i
].IsPresent
) {
2378 if ((attrs
->DMABufPlaneModifiersLo
[0].IsPresent
!=
2379 attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
) ||
2380 (attrs
->DMABufPlaneModifiersLo
[0].Value
!=
2381 attrs
->DMABufPlaneModifiersLo
[i
].Value
) ||
2382 (attrs
->DMABufPlaneModifiersHi
[0].Value
!=
2383 attrs
->DMABufPlaneModifiersHi
[i
].Value
))
2384 return _eglError(EGL_BAD_PARAMETER
, "modifier attributes not equal");
2391 /* Returns the total number of planes for the format or zero if it isn't a
2392 * valid fourcc format.
2395 dri2_num_fourcc_format_planes(EGLint format
)
2399 case DRM_FORMAT_RG88
:
2400 case DRM_FORMAT_GR88
:
2401 case DRM_FORMAT_R16
:
2402 case DRM_FORMAT_GR1616
:
2403 case DRM_FORMAT_RGB332
:
2404 case DRM_FORMAT_BGR233
:
2405 case DRM_FORMAT_XRGB4444
:
2406 case DRM_FORMAT_XBGR4444
:
2407 case DRM_FORMAT_RGBX4444
:
2408 case DRM_FORMAT_BGRX4444
:
2409 case DRM_FORMAT_ARGB4444
:
2410 case DRM_FORMAT_ABGR4444
:
2411 case DRM_FORMAT_RGBA4444
:
2412 case DRM_FORMAT_BGRA4444
:
2413 case DRM_FORMAT_XRGB1555
:
2414 case DRM_FORMAT_XBGR1555
:
2415 case DRM_FORMAT_RGBX5551
:
2416 case DRM_FORMAT_BGRX5551
:
2417 case DRM_FORMAT_ARGB1555
:
2418 case DRM_FORMAT_ABGR1555
:
2419 case DRM_FORMAT_RGBA5551
:
2420 case DRM_FORMAT_BGRA5551
:
2421 case DRM_FORMAT_RGB565
:
2422 case DRM_FORMAT_BGR565
:
2423 case DRM_FORMAT_RGB888
:
2424 case DRM_FORMAT_BGR888
:
2425 case DRM_FORMAT_XRGB8888
:
2426 case DRM_FORMAT_XBGR8888
:
2427 case DRM_FORMAT_RGBX8888
:
2428 case DRM_FORMAT_BGRX8888
:
2429 case DRM_FORMAT_ARGB8888
:
2430 case DRM_FORMAT_ABGR8888
:
2431 case DRM_FORMAT_RGBA8888
:
2432 case DRM_FORMAT_BGRA8888
:
2433 case DRM_FORMAT_XRGB2101010
:
2434 case DRM_FORMAT_XBGR2101010
:
2435 case DRM_FORMAT_RGBX1010102
:
2436 case DRM_FORMAT_BGRX1010102
:
2437 case DRM_FORMAT_ARGB2101010
:
2438 case DRM_FORMAT_ABGR2101010
:
2439 case DRM_FORMAT_RGBA1010102
:
2440 case DRM_FORMAT_BGRA1010102
:
2441 case DRM_FORMAT_XBGR16161616F
:
2442 case DRM_FORMAT_ABGR16161616F
:
2443 case DRM_FORMAT_YUYV
:
2444 case DRM_FORMAT_YVYU
:
2445 case DRM_FORMAT_UYVY
:
2446 case DRM_FORMAT_VYUY
:
2447 case DRM_FORMAT_AYUV
:
2448 case DRM_FORMAT_XYUV8888
:
2451 case DRM_FORMAT_NV12
:
2452 case DRM_FORMAT_NV21
:
2453 case DRM_FORMAT_NV16
:
2454 case DRM_FORMAT_NV61
:
2455 case DRM_FORMAT_P010
:
2456 case DRM_FORMAT_P012
:
2457 case DRM_FORMAT_P016
:
2460 case DRM_FORMAT_YUV410
:
2461 case DRM_FORMAT_YVU410
:
2462 case DRM_FORMAT_YUV411
:
2463 case DRM_FORMAT_YVU411
:
2464 case DRM_FORMAT_YUV420
:
2465 case DRM_FORMAT_YVU420
:
2466 case DRM_FORMAT_YUV422
:
2467 case DRM_FORMAT_YVU422
:
2468 case DRM_FORMAT_YUV444
:
2469 case DRM_FORMAT_YVU444
:
2477 /* Returns the total number of file descriptors. Zero indicates an error. */
2479 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
2481 unsigned plane_n
= dri2_num_fourcc_format_planes(attrs
->DMABufFourCC
.Value
);
2483 _eglError(EGL_BAD_MATCH
, "unknown drm fourcc format");
2487 for (unsigned i
= plane_n
; i
< DMA_BUF_MAX_PLANES
; i
++) {
2489 * The modifiers extension spec says:
2491 * "Modifiers may modify any attribute of a buffer import, including
2492 * but not limited to adding extra planes to a format which
2493 * otherwise does not have those planes. As an example, a modifier
2494 * may add a plane for an external compression buffer to a
2495 * single-plane format. The exact meaning and effect of any
2496 * modifier is canonically defined by drm_fourcc.h, not as part of
2499 if (attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
&&
2500 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
) {
2508 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2509 * incomplete, EGL_BAD_PARAMETER is generated."
2511 for (unsigned i
= 0; i
< plane_n
; ++i
) {
2512 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2513 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2514 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2515 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2521 * The spec also says:
2523 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2524 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2525 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2526 * or EGL_DMA_BUF_PLANE3_* attributes are specified."
2528 for (unsigned i
= plane_n
; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2529 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2530 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2531 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2532 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2541 dri2_query_dma_buf_formats(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2542 EGLint max
, EGLint
*formats
, EGLint
*count
)
2544 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2545 if (max
< 0 || (max
> 0 && formats
== NULL
))
2546 return _eglError(EGL_BAD_PARAMETER
, "invalid value for max count of formats");
2548 if (dri2_dpy
->image
->base
.version
< 15 ||
2549 dri2_dpy
->image
->queryDmaBufFormats
== NULL
)
2552 if (!dri2_dpy
->image
->queryDmaBufFormats(dri2_dpy
->dri_screen
, max
,
2557 /* Assert that all of the formats returned are actually fourcc formats.
2558 * Some day, if we want the internal interface function to be able to
2559 * return the fake fourcc formats defined in dri_interface.h, we'll have
2560 * to do something more clever here to pair the list down to just real
2561 * fourcc formats so that we don't leak the fake internal ones.
2563 for (int i
= 0; i
< *count
; i
++) {
2564 assert(dri2_num_fourcc_format_planes(formats
[i
]) > 0);
2572 dri2_query_dma_buf_modifiers(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint format
,
2573 EGLint max
, EGLuint64KHR
*modifiers
,
2574 EGLBoolean
*external_only
, EGLint
*count
)
2576 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2578 if (dri2_num_fourcc_format_planes(format
) == 0)
2579 return _eglError(EGL_BAD_PARAMETER
, "invalid fourcc format");
2582 return _eglError(EGL_BAD_PARAMETER
, "invalid value for max count of formats");
2584 if (max
> 0 && modifiers
== NULL
)
2585 return _eglError(EGL_BAD_PARAMETER
, "invalid modifiers array");
2587 if (dri2_dpy
->image
->base
.version
< 15 ||
2588 dri2_dpy
->image
->queryDmaBufModifiers
== NULL
)
2591 if (dri2_dpy
->image
->queryDmaBufModifiers(dri2_dpy
->dri_screen
, format
,
2593 (unsigned int *) external_only
,
2595 return _eglError(EGL_BAD_PARAMETER
, "invalid format");
2603 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2604 * EGL will take a reference to the dma_buf(s) which it will release at any
2605 * time while the EGLDisplay is initialized. It is the responsibility of the
2606 * application to close the dma_buf file descriptors."
2608 * Therefore we must never close or otherwise modify the file descriptors.
2611 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2612 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2614 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2616 _EGLImageAttribs attrs
;
2617 __DRIimage
*dri_image
;
2619 int fds
[DMA_BUF_MAX_PLANES
];
2620 int pitches
[DMA_BUF_MAX_PLANES
];
2621 int offsets
[DMA_BUF_MAX_PLANES
];
2623 bool has_modifier
= false;
2629 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2630 * error EGL_BAD_PARAMETER is generated."
2632 if (buffer
!= NULL
) {
2633 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2637 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2640 if (!dri2_check_dma_buf_attribs(&attrs
))
2643 num_fds
= dri2_check_dma_buf_format(&attrs
);
2647 for (unsigned i
= 0; i
< num_fds
; ++i
) {
2648 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2649 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2650 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2653 /* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2654 * will be present in attrs.DMABufPlaneModifiersLo[0] and
2655 * attrs.DMABufPlaneModifiersHi[0] */
2656 if (attrs
.DMABufPlaneModifiersLo
[0].IsPresent
) {
2657 modifier
= combine_u32_into_u64(attrs
.DMABufPlaneModifiersHi
[0].Value
,
2658 attrs
.DMABufPlaneModifiersLo
[0].Value
);
2659 has_modifier
= true;
2662 if (dri2_dpy
->image
->base
.version
>= 18) {
2665 if (attrs
.ImageFlushExternal
)
2666 use
|= __DRI_IMAGE_USE_FLUSH_EXTERNAL
;
2669 modifier
= DRM_FORMAT_MOD_INVALID
;
2672 dri2_dpy
->image
->createImageFromDmaBufs3(dri2_dpy
->dri_screen
,
2673 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2674 modifier
, use
, fds
, num_fds
, pitches
, offsets
,
2675 attrs
.DMABufYuvColorSpaceHint
.Value
,
2676 attrs
.DMABufSampleRangeHint
.Value
,
2677 attrs
.DMABufChromaHorizontalSiting
.Value
,
2678 attrs
.DMABufChromaVerticalSiting
.Value
,
2681 } else if (has_modifier
) {
2682 if (dri2_dpy
->image
->base
.version
< 15 ||
2683 dri2_dpy
->image
->createImageFromDmaBufs2
== NULL
) {
2684 _eglError(EGL_BAD_MATCH
, "unsupported dma_buf format modifier");
2685 return EGL_NO_IMAGE_KHR
;
2688 dri2_dpy
->image
->createImageFromDmaBufs2(dri2_dpy
->dri_screen
,
2689 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2690 modifier
, fds
, num_fds
, pitches
, offsets
,
2691 attrs
.DMABufYuvColorSpaceHint
.Value
,
2692 attrs
.DMABufSampleRangeHint
.Value
,
2693 attrs
.DMABufChromaHorizontalSiting
.Value
,
2694 attrs
.DMABufChromaVerticalSiting
.Value
,
2699 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2700 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2701 fds
, num_fds
, pitches
, offsets
,
2702 attrs
.DMABufYuvColorSpaceHint
.Value
,
2703 attrs
.DMABufSampleRangeHint
.Value
,
2704 attrs
.DMABufChromaHorizontalSiting
.Value
,
2705 attrs
.DMABufChromaVerticalSiting
.Value
,
2709 dri2_create_image_khr_texture_error(error
);
2712 return EGL_NO_IMAGE_KHR
;
2714 res
= dri2_create_image_from_dri(disp
, dri_image
);
2719 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2720 const EGLint
*attr_list
)
2722 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2723 struct dri2_egl_image
*dri2_img
;
2724 _EGLImageAttribs attrs
;
2725 unsigned int dri_use
, valid_mask
;
2731 _eglError(EGL_BAD_PARAMETER
, __func__
);
2732 return EGL_NO_IMAGE_KHR
;
2735 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2736 return EGL_NO_IMAGE_KHR
;
2738 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2739 _eglError(EGL_BAD_PARAMETER
, __func__
);
2740 return EGL_NO_IMAGE_KHR
;
2743 switch (attrs
.DRMBufferFormatMESA
) {
2744 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2745 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2748 _eglError(EGL_BAD_PARAMETER
, __func__
);
2749 return EGL_NO_IMAGE_KHR
;
2753 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2754 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2755 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2756 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2757 _eglError(EGL_BAD_PARAMETER
, __func__
);
2758 return EGL_NO_IMAGE_KHR
;
2762 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2763 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2764 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2765 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2766 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2767 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2768 if (attrs
.ImageFlushExternal
)
2769 dri_use
|= __DRI_IMAGE_USE_FLUSH_EXTERNAL
;
2771 dri2_img
= malloc(sizeof *dri2_img
);
2773 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2774 return EGL_NO_IMAGE_KHR
;
2777 _eglInitImage(&dri2_img
->base
, disp
);
2779 dri2_img
->dri_image
=
2780 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2781 attrs
.Width
, attrs
.Height
,
2782 format
, dri_use
, dri2_img
);
2783 if (dri2_img
->dri_image
== NULL
) {
2785 _eglError(EGL_BAD_ALLOC
, "dri2_create_drm_image_mesa");
2786 return EGL_NO_IMAGE_KHR
;
2789 return &dri2_img
->base
;
2793 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2794 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2796 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2797 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2801 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2802 __DRI_IMAGE_ATTRIB_NAME
, name
))
2803 return _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2806 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2807 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2810 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2811 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2817 * Checks if we can support EGL_MESA_image_dma_buf_export on this image.
2819 * The spec provides a boolean return for the driver to reject exporting for
2820 * basically any reason, but doesn't specify any particular error cases. For
2821 * now, we just fail if we don't have a DRM fourcc for the format.
2824 dri2_can_export_dma_buf_image(_EGLDisplay
*disp
, _EGLImage
*img
)
2826 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2827 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2830 if (!dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2831 __DRI_IMAGE_ATTRIB_FOURCC
, &fourcc
)) {
2839 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2841 EGLint
*fourcc
, EGLint
*nplanes
,
2842 EGLuint64KHR
*modifiers
)
2844 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2845 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2850 if (!dri2_can_export_dma_buf_image(disp
, img
))
2853 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2854 __DRI_IMAGE_ATTRIB_NUM_PLANES
, &num_planes
);
2856 *nplanes
= num_planes
;
2859 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2860 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2864 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
2867 query
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2868 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
2870 query
&= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2871 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
2874 modifier
= combine_u32_into_u64 (mod_hi
, mod_lo
);
2876 for (int i
= 0; i
< num_planes
; i
++)
2877 modifiers
[i
] = modifier
;
2884 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2885 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2887 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2888 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2893 if (!dri2_can_export_dma_buf_image(disp
, img
))
2896 /* EGL_MESA_image_dma_buf_export spec says:
2897 * "If the number of fds is less than the number of planes, then
2898 * subsequent fd slots should contain -1."
2901 /* Query nplanes so that we know how big the given array is. */
2902 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2903 __DRI_IMAGE_ATTRIB_NUM_PLANES
, &nplanes
);
2904 memset(fds
, -1, nplanes
* sizeof(int));
2907 /* rework later to provide multiple fds/strides/offsets */
2909 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2910 __DRI_IMAGE_ATTRIB_FD
, fds
);
2913 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2914 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2918 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2919 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2921 offsets
[0] = img_offset
;
2932 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2933 _EGLContext
*ctx
, EGLenum target
,
2934 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2939 case EGL_GL_TEXTURE_2D_KHR
:
2940 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2941 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2942 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2943 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2944 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2945 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2946 case EGL_GL_TEXTURE_3D_KHR
:
2947 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2948 case EGL_GL_RENDERBUFFER_KHR
:
2949 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2951 case EGL_DRM_BUFFER_MESA
:
2952 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2953 case EGL_LINUX_DMA_BUF_EXT
:
2954 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2956 #ifdef HAVE_WAYLAND_PLATFORM
2957 case EGL_WAYLAND_BUFFER_WL
:
2958 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2961 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2962 return EGL_NO_IMAGE_KHR
;
2967 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2969 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2970 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2974 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2980 #ifdef HAVE_WAYLAND_PLATFORM
2983 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2984 struct wl_drm_buffer
*buffer
)
2986 _EGLDisplay
*disp
= user_data
;
2987 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2989 int dri_components
= 0;
2992 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
3001 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
3013 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
3015 buffer
->driver_format
= NULL
;
3016 for (int i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
3017 if (wl_drm_components
[i
].dri_components
== dri_components
)
3018 buffer
->driver_format
= &wl_drm_components
[i
];
3020 if (buffer
->driver_format
== NULL
)
3021 dri2_dpy
->image
->destroyImage(img
);
3023 buffer
->driver_buffer
= img
;
3027 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
3029 _EGLDisplay
*disp
= user_data
;
3030 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3032 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
3036 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3037 struct wl_display
*wl_dpy
)
3039 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3040 const struct wayland_drm_callbacks wl_drm_callbacks
= {
3041 .authenticate
= (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
,
3042 .reference_buffer
= dri2_wl_reference_buffer
,
3043 .release_buffer
= dri2_wl_release_buffer
,
3044 .is_format_supported
= dri2_wl_is_format_supported
3051 if (dri2_dpy
->wl_server_drm
)
3054 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
3055 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
3056 dri2_dpy
->image
->base
.version
>= 7 &&
3057 dri2_dpy
->image
->createImageFromFds
!= NULL
)
3058 flags
|= WAYLAND_DRM_PRIME
;
3060 dri2_dpy
->wl_server_drm
=
3061 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
3062 &wl_drm_callbacks
, disp
, flags
);
3064 if (!dri2_dpy
->wl_server_drm
)
3067 #ifdef HAVE_DRM_PLATFORM
3068 /* We have to share the wl_drm instance with gbm, so gbm can convert
3069 * wl_buffers to gbm bos. */
3070 if (dri2_dpy
->gbm_dri
)
3071 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
3078 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3079 struct wl_display
*wl_dpy
)
3081 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3085 if (!dri2_dpy
->wl_server_drm
)
3088 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
3089 dri2_dpy
->wl_server_drm
= NULL
;
3095 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3096 struct wl_resource
*buffer_resource
,
3097 EGLint attribute
, EGLint
*value
)
3099 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3100 struct wl_drm_buffer
*buffer
;
3101 const struct wl_drm_components_descriptor
*format
;
3103 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
3107 format
= buffer
->driver_format
;
3108 switch (attribute
) {
3109 case EGL_TEXTURE_FORMAT
:
3110 *value
= format
->components
;
3113 *value
= buffer
->width
;
3116 *value
= buffer
->height
;
3125 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
3127 p_atomic_inc(&sync
->refcount
);
3131 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
3132 struct dri2_egl_sync
*dri2_sync
)
3134 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
3135 switch (dri2_sync
->base
.Type
) {
3136 case EGL_SYNC_REUSABLE_KHR
:
3137 cnd_destroy(&dri2_sync
->cond
);
3139 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3140 if (dri2_sync
->base
.SyncFd
!= EGL_NO_NATIVE_FENCE_FD_ANDROID
)
3141 close(dri2_sync
->base
.SyncFd
);
3147 if (dri2_sync
->fence
)
3148 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
3155 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3156 EGLenum type
, const EGLAttrib
*attrib_list
)
3158 _EGLContext
*ctx
= _eglGetCurrentContext();
3159 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3160 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3161 struct dri2_egl_sync
*dri2_sync
;
3163 pthread_condattr_t attr
;
3165 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
3167 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
3171 if (!_eglInitSync(&dri2_sync
->base
, disp
, type
, attrib_list
)) {
3177 case EGL_SYNC_FENCE_KHR
:
3178 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
3179 if (!dri2_sync
->fence
) {
3180 /* Why did it fail? DRI doesn't return an error code, so we emit
3181 * a generic EGL error that doesn't communicate user error.
3183 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
3189 case EGL_SYNC_CL_EVENT_KHR
:
3190 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
3191 dri2_dpy
->dri_screen
,
3192 dri2_sync
->base
.CLEvent
);
3193 /* this can only happen if the cl_event passed in is invalid. */
3194 if (!dri2_sync
->fence
) {
3195 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
3200 /* the initial status must be "signaled" if the cl_event is signaled */
3201 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
3202 dri2_sync
->fence
, 0, 0))
3203 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3206 case EGL_SYNC_REUSABLE_KHR
:
3207 /* intialize attr */
3208 ret
= pthread_condattr_init(&attr
);
3211 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3216 /* change clock attribute to CLOCK_MONOTONIC */
3217 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
3220 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3225 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
3228 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3233 /* initial status of reusable sync must be "unsignaled" */
3234 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
3237 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3238 if (dri2_dpy
->fence
->create_fence_fd
) {
3239 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence_fd(
3240 dri2_ctx
->dri_context
,
3241 dri2_sync
->base
.SyncFd
);
3243 if (!dri2_sync
->fence
) {
3244 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
3251 p_atomic_set(&dri2_sync
->refcount
, 1);
3252 return &dri2_sync
->base
;
3256 dri2_destroy_sync(_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
);
3260 EGLint ret
= EGL_TRUE
;
3263 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
3264 * then unlock all threads possibly blocked by the reusable sync before
3267 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
3268 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
3269 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3270 /* unblock all threads currently blocked by sync */
3271 err
= cnd_broadcast(&dri2_sync
->cond
);
3274 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
3279 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
3285 dri2_dup_native_fence_fd(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3287 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3288 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3290 assert(sync
->Type
== EGL_SYNC_NATIVE_FENCE_ANDROID
);
3292 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
3293 /* try to retrieve the actual native fence fd.. if rendering is
3294 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
3296 sync
->SyncFd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
3300 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
3301 /* if native fence fd still not created, return an error: */
3302 _eglError(EGL_BAD_PARAMETER
, "eglDupNativeFenceFDANDROID");
3303 return EGL_NO_NATIVE_FENCE_FD_ANDROID
;
3306 return dup(sync
->SyncFd
);
3310 dri2_set_blob_cache_funcs(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3311 EGLSetBlobFuncANDROID set
,
3312 EGLGetBlobFuncANDROID get
)
3314 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3315 dri2_dpy
->blob
->set_cache_funcs(dri2_dpy
->dri_screen
,
3317 disp
->BlobCacheGet
);
3321 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
,
3322 EGLint flags
, EGLTime timeout
)
3324 _EGLContext
*ctx
= _eglGetCurrentContext();
3325 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3326 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3327 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3328 unsigned wait_flags
= 0;
3330 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
3332 /* The EGL_KHR_fence_sync spec states:
3334 * "If no context is current for the bound API,
3335 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
3337 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
3338 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
3340 /* the sync object should take a reference while waiting */
3341 dri2_egl_ref_sync(dri2_sync
);
3343 switch (sync
->Type
) {
3344 case EGL_SYNC_FENCE_KHR
:
3345 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3346 case EGL_SYNC_CL_EVENT_KHR
:
3347 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
3348 dri2_sync
->fence
, wait_flags
,
3350 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3352 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
3355 case EGL_SYNC_REUSABLE_KHR
:
3356 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
3357 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
3358 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
3362 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
3363 if (timeout
== EGL_FOREVER_KHR
) {
3364 mtx_lock(&dri2_sync
->mutex
);
3365 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
3366 mtx_unlock(&dri2_sync
->mutex
);
3368 /* if reusable sync has not been yet signaled */
3369 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
3370 /* timespecs for cnd_timedwait */
3371 struct timespec current
;
3372 struct timespec expire
;
3374 /* We override the clock to monotonic when creating the condition
3376 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
3378 /* calculating when to expire */
3379 expire
.tv_nsec
= timeout
% 1000000000L;
3380 expire
.tv_sec
= timeout
/ 1000000000L;
3382 expire
.tv_nsec
+= current
.tv_nsec
;
3383 expire
.tv_sec
+= current
.tv_sec
;
3385 /* expire.nsec now is a number between 0 and 1999999998 */
3386 if (expire
.tv_nsec
> 999999999L) {
3388 expire
.tv_nsec
-= 1000000000L;
3391 mtx_lock(&dri2_sync
->mutex
);
3392 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
3393 mtx_unlock(&dri2_sync
->mutex
);
3395 if (ret
== thrd_busy
) {
3396 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
3397 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
3399 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
3407 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
3413 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
,
3416 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3419 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
)
3420 return _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
3422 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
)
3423 return _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
3425 dri2_sync
->base
.SyncStatus
= mode
;
3427 if (mode
== EGL_SIGNALED_KHR
) {
3428 ret
= cnd_broadcast(&dri2_sync
->cond
);
3430 /* fail to broadcast */
3432 return _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
3439 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3441 _EGLContext
*ctx
= _eglGetCurrentContext();
3442 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3443 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3444 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3446 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
3447 dri2_sync
->fence
, 0);
3452 dri2_interop_query_device_info(_EGLDisplay
*disp
, _EGLContext
*ctx
,
3453 struct mesa_glinterop_device_info
*out
)
3455 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3456 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3458 if (!dri2_dpy
->interop
)
3459 return MESA_GLINTEROP_UNSUPPORTED
;
3461 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
3465 dri2_interop_export_object(_EGLDisplay
*disp
, _EGLContext
*ctx
,
3466 struct mesa_glinterop_export_in
*in
,
3467 struct mesa_glinterop_export_out
*out
)
3469 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3470 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3472 if (!dri2_dpy
->interop
)
3473 return MESA_GLINTEROP_UNSUPPORTED
;
3475 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
3479 * This is the main entrypoint into the driver, called by libEGL.
3480 * Gets an _EGLDriver object and init its dispatch table.
3483 _eglInitDriver(_EGLDriver
*dri2_drv
)
3485 dri2_drv
->API
.Initialize
= dri2_initialize
;
3486 dri2_drv
->API
.Terminate
= dri2_terminate
;
3487 dri2_drv
->API
.CreateContext
= dri2_create_context
;
3488 dri2_drv
->API
.DestroyContext
= dri2_destroy_context
;
3489 dri2_drv
->API
.MakeCurrent
= dri2_make_current
;
3490 dri2_drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
3491 dri2_drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
3492 dri2_drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
3493 dri2_drv
->API
.DestroySurface
= dri2_destroy_surface
;
3494 dri2_drv
->API
.GetProcAddress
= dri2_get_proc_address
;
3495 dri2_drv
->API
.WaitClient
= dri2_wait_client
;
3496 dri2_drv
->API
.WaitNative
= dri2_wait_native
;
3497 dri2_drv
->API
.BindTexImage
= dri2_bind_tex_image
;
3498 dri2_drv
->API
.ReleaseTexImage
= dri2_release_tex_image
;
3499 dri2_drv
->API
.SwapInterval
= dri2_swap_interval
;
3500 dri2_drv
->API
.SwapBuffers
= dri2_swap_buffers
;
3501 dri2_drv
->API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
3502 dri2_drv
->API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
3503 dri2_drv
->API
.SetDamageRegion
= dri2_set_damage_region
;
3504 dri2_drv
->API
.PostSubBufferNV
= dri2_post_sub_buffer
;
3505 dri2_drv
->API
.CopyBuffers
= dri2_copy_buffers
,
3506 dri2_drv
->API
.QueryBufferAge
= dri2_query_buffer_age
;
3507 dri2_drv
->API
.CreateImageKHR
= dri2_create_image
;
3508 dri2_drv
->API
.DestroyImageKHR
= dri2_destroy_image_khr
;
3509 dri2_drv
->API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
3510 dri2_drv
->API
.QuerySurface
= dri2_query_surface
;
3511 dri2_drv
->API
.QueryDriverName
= dri2_query_driver_name
;
3512 dri2_drv
->API
.QueryDriverConfig
= dri2_query_driver_config
;
3514 dri2_drv
->API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
3515 dri2_drv
->API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
3516 dri2_drv
->API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
3517 dri2_drv
->API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
3518 dri2_drv
->API
.QueryDmaBufFormatsEXT
= dri2_query_dma_buf_formats
;
3519 dri2_drv
->API
.QueryDmaBufModifiersEXT
= dri2_query_dma_buf_modifiers
;
3521 #ifdef HAVE_WAYLAND_PLATFORM
3522 dri2_drv
->API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
3523 dri2_drv
->API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
3524 dri2_drv
->API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
3526 dri2_drv
->API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
3527 dri2_drv
->API
.CreateSyncKHR
= dri2_create_sync
;
3528 dri2_drv
->API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
3529 dri2_drv
->API
.SignalSyncKHR
= dri2_signal_sync
;
3530 dri2_drv
->API
.WaitSyncKHR
= dri2_server_wait_sync
;
3531 dri2_drv
->API
.DestroySyncKHR
= dri2_destroy_sync
;
3532 dri2_drv
->API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
3533 dri2_drv
->API
.GLInteropExportObject
= dri2_interop_export_object
;
3534 dri2_drv
->API
.DupNativeFenceFDANDROID
= dri2_dup_native_fence_fd
;
3535 dri2_drv
->API
.SetBlobCacheFuncsANDROID
= dri2_set_blob_cache_funcs
;