2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Kristian Høgsberg <krh@bitplanet.net>
39 #include <c11/threads.h>
43 #include "drm-uapi/drm_fourcc.h"
46 #include <GL/internal/dri_interface.h>
47 #include <sys/types.h>
50 #ifdef HAVE_WAYLAND_PLATFORM
51 #include <wayland-client.h>
52 #include "wayland-drm.h"
53 #include "wayland-drm-client-protocol.h"
54 #include "linux-dmabuf-unstable-v1-client-protocol.h"
57 #ifdef HAVE_X11_PLATFORM
58 #include "X11/Xlibint.h"
61 #include "egldefines.h"
63 #include "GL/mesa_glinterop.h"
64 #include "loader/loader.h"
65 #include "util/u_atomic.h"
66 #include "util/u_vector.h"
67 #include "mapi/glapi/glapi.h"
69 /* Additional definitions not yet in the drm_fourcc.h.
71 #ifndef DRM_FORMAT_P010
72 #define DRM_FORMAT_P010 fourcc_code('P', '0', '1', '0') /* 2x2 subsampled Cb:Cr plane 10 bits per channel */
75 #ifndef DRM_FORMAT_P012
76 #define DRM_FORMAT_P012 fourcc_code('P', '0', '1', '2') /* 2x2 subsampled Cb:Cr plane 12 bits per channel */
79 #ifndef DRM_FORMAT_P016
80 #define DRM_FORMAT_P016 fourcc_code('P', '0', '1', '6') /* 2x2 subsampled Cb:Cr plane 16 bits per channel */
83 #define NUM_ATTRIBS 12
86 dri_set_background_context(void *loaderPrivate
)
88 _EGLContext
*ctx
= _eglGetCurrentContext();
89 _EGLThreadInfo
*t
= _eglGetCurrentThread();
91 _eglBindContextToThread(ctx
, t
);
97 static void (*glFlush
)(void);
98 static mtx_t glFlushMutex
= _MTX_INITIALIZER_NP
;
100 mtx_lock(&glFlushMutex
);
102 glFlush
= _glapi_get_proc_address("glFlush");
103 mtx_unlock(&glFlushMutex
);
105 /* if glFlush is not available things are horribly broken */
107 _eglLog(_EGL_WARNING
, "DRI2: failed to find glFlush entry point");
115 dri_is_thread_safe(void *loaderPrivate
)
117 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
118 UNUSED _EGLDisplay
*display
= dri2_surf
->base
.Resource
.Display
;
120 #ifdef HAVE_X11_PLATFORM
121 Display
*xdpy
= (Display
*)display
->PlatformDisplay
;
123 /* Check Xlib is running in thread safe mode when running on EGL/X11-xlib
126 * 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
127 * It wll be NULL if XInitThreads wasn't called.
129 if (display
->Platform
== _EGL_PLATFORM_X11
&& xdpy
&& !xdpy
->lock_fns
)
133 #ifdef HAVE_WAYLAND_PLATFORM
134 if (display
->Platform
== _EGL_PLATFORM_WAYLAND
)
141 const __DRIbackgroundCallableExtension background_callable_extension
= {
142 .base
= { __DRI_BACKGROUND_CALLABLE
, 2 },
144 .setBackgroundContext
= dri_set_background_context
,
145 .isThreadSafe
= dri_is_thread_safe
,
148 const __DRIuseInvalidateExtension use_invalidate
= {
149 .base
= { __DRI_USE_INVALIDATE
, 1 }
153 dri2_get_pbuffer_drawable_info(__DRIdrawable
* draw
,
154 int *x
, int *y
, int *w
, int *h
,
157 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
160 *w
= dri2_surf
->base
.Width
;
161 *h
= dri2_surf
->base
.Height
;
164 /* HACK: technically we should have swrast_null, instead of these. We
165 * get away since only pbuffers are supported, thus the callbacks are
168 const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension
= {
169 .base
= { __DRI_SWRAST_LOADER
, 1 },
170 .getDrawableInfo
= dri2_get_pbuffer_drawable_info
,
175 static const EGLint dri2_to_egl_attribute_map
[__DRI_ATTRIB_MAX
] = {
176 [__DRI_ATTRIB_BUFFER_SIZE
] = EGL_BUFFER_SIZE
,
177 [__DRI_ATTRIB_LEVEL
] = EGL_LEVEL
,
178 [__DRI_ATTRIB_LUMINANCE_SIZE
] = EGL_LUMINANCE_SIZE
,
179 [__DRI_ATTRIB_DEPTH_SIZE
] = EGL_DEPTH_SIZE
,
180 [__DRI_ATTRIB_STENCIL_SIZE
] = EGL_STENCIL_SIZE
,
181 [__DRI_ATTRIB_SAMPLE_BUFFERS
] = EGL_SAMPLE_BUFFERS
,
182 [__DRI_ATTRIB_SAMPLES
] = EGL_SAMPLES
,
183 [__DRI_ATTRIB_MAX_PBUFFER_WIDTH
] = EGL_MAX_PBUFFER_WIDTH
,
184 [__DRI_ATTRIB_MAX_PBUFFER_HEIGHT
] = EGL_MAX_PBUFFER_HEIGHT
,
185 [__DRI_ATTRIB_MAX_PBUFFER_PIXELS
] = EGL_MAX_PBUFFER_PIXELS
,
186 [__DRI_ATTRIB_MAX_SWAP_INTERVAL
] = EGL_MAX_SWAP_INTERVAL
,
187 [__DRI_ATTRIB_MIN_SWAP_INTERVAL
] = EGL_MIN_SWAP_INTERVAL
,
188 [__DRI_ATTRIB_YINVERTED
] = EGL_Y_INVERTED_NOK
,
192 dri2_get_dri_config(struct dri2_egl_config
*conf
, EGLint surface_type
,
195 const bool double_buffer
= surface_type
== EGL_WINDOW_BIT
;
196 const bool srgb
= colorspace
== EGL_GL_COLORSPACE_SRGB_KHR
;
198 return conf
->dri_config
[double_buffer
][srgb
];
202 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
204 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
207 if (!_eglMatchConfig(conf
, criteria
))
214 dri2_get_shifts_and_sizes(const __DRIcoreExtension
*core
,
215 const __DRIconfig
*config
, int *shifts
,
220 if (core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_SHIFT
, (unsigned int *)&shifts
[0])) {
221 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_SHIFT
, (unsigned int *)&shifts
[1]);
222 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_SHIFT
, (unsigned int *)&shifts
[2]);
223 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_SHIFT
, (unsigned int *)&shifts
[3]);
225 /* Driver isn't exposing shifts, so convert masks to shifts */
226 core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_MASK
, &mask
);
227 shifts
[0] = ffs(mask
) - 1;
228 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_MASK
, &mask
);
229 shifts
[1] = ffs(mask
) - 1;
230 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_MASK
, &mask
);
231 shifts
[2] = ffs(mask
) - 1;
232 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_MASK
, &mask
);
233 shifts
[3] = ffs(mask
) - 1;
236 core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_SIZE
, &sizes
[0]);
237 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_SIZE
, &sizes
[1]);
238 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_SIZE
, &sizes
[2]);
239 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_SIZE
, &sizes
[3]);
242 struct dri2_egl_config
*
243 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
244 EGLint surface_type
, const EGLint
*attr_list
,
245 const int *rgba_shifts
, const unsigned int *rgba_sizes
)
247 struct dri2_egl_config
*conf
;
248 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
250 unsigned int attrib
, value
, double_buffer
;
252 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
253 int dri_shifts
[4] = { -1, -1, -1, -1 };
254 unsigned int dri_sizes
[4] = { 0, 0, 0, 0 };
255 _EGLConfig
*matching_config
;
256 EGLint num_configs
= 0;
259 _eglInitConfig(&base
, disp
, id
);
262 bind_to_texture_rgb
= 0;
263 bind_to_texture_rgba
= 0;
265 for (int i
= 0; i
< __DRI_ATTRIB_MAX
; ++i
) {
266 if (!dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
, &attrib
, &value
))
270 case __DRI_ATTRIB_RENDER_TYPE
:
271 if (value
& __DRI_ATTRIB_RGBA_BIT
)
272 value
= EGL_RGB_BUFFER
;
273 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
274 value
= EGL_LUMINANCE_BUFFER
;
277 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
280 case __DRI_ATTRIB_CONFIG_CAVEAT
:
281 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
282 value
= EGL_NON_CONFORMANT_CONFIG
;
283 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
284 value
= EGL_SLOW_CONFIG
;
287 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
290 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
291 bind_to_texture_rgb
= value
;
294 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
295 bind_to_texture_rgba
= value
;
298 case __DRI_ATTRIB_DOUBLE_BUFFER
:
299 double_buffer
= value
;
302 case __DRI_ATTRIB_RED_SIZE
:
303 dri_sizes
[0] = value
;
304 _eglSetConfigKey(&base
, EGL_RED_SIZE
, value
);
307 case __DRI_ATTRIB_RED_MASK
:
308 dri_shifts
[0] = ffs(value
) - 1;
311 case __DRI_ATTRIB_RED_SHIFT
:
312 dri_shifts
[0] = value
;
315 case __DRI_ATTRIB_GREEN_SIZE
:
316 dri_sizes
[1] = value
;
317 _eglSetConfigKey(&base
, EGL_GREEN_SIZE
, value
);
320 case __DRI_ATTRIB_GREEN_MASK
:
321 dri_shifts
[1] = ffs(value
) - 1;
324 case __DRI_ATTRIB_GREEN_SHIFT
:
325 dri_shifts
[1] = value
;
328 case __DRI_ATTRIB_BLUE_SIZE
:
329 dri_sizes
[2] = value
;
330 _eglSetConfigKey(&base
, EGL_BLUE_SIZE
, value
);
333 case __DRI_ATTRIB_BLUE_MASK
:
334 dri_shifts
[2] = ffs(value
) - 1;
337 case __DRI_ATTRIB_BLUE_SHIFT
:
338 dri_shifts
[2] = value
;
341 case __DRI_ATTRIB_ALPHA_SIZE
:
342 dri_sizes
[3] = value
;
343 _eglSetConfigKey(&base
, EGL_ALPHA_SIZE
, value
);
346 case __DRI_ATTRIB_ALPHA_MASK
:
347 dri_shifts
[3] = ffs(value
) - 1;
350 case __DRI_ATTRIB_ALPHA_SHIFT
:
351 dri_shifts
[3] = value
;
354 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
355 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
356 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
357 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
358 /* Don't expose visuals with the accumulation buffer. */
363 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
365 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
369 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH
:
370 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_WIDTH
,
371 _EGL_MAX_PBUFFER_WIDTH
);
373 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT
:
374 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_HEIGHT
,
375 _EGL_MAX_PBUFFER_HEIGHT
);
377 case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER
:
378 if (disp
->Extensions
.KHR_mutable_render_buffer
)
379 surface_type
|= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR
;
382 key
= dri2_to_egl_attribute_map
[attrib
];
384 _eglSetConfigKey(&base
, key
, value
);
390 for (int i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
391 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
393 if (rgba_shifts
&& memcmp(rgba_shifts
, dri_shifts
, sizeof(dri_shifts
)))
396 if (rgba_sizes
&& memcmp(rgba_sizes
, dri_sizes
, sizeof(dri_sizes
)))
399 base
.NativeRenderable
= EGL_TRUE
;
401 base
.SurfaceType
= surface_type
;
402 if (surface_type
& (EGL_PBUFFER_BIT
|
403 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
404 base
.BindToTextureRGB
= bind_to_texture_rgb
;
405 if (base
.AlphaSize
> 0)
406 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
409 base
.RenderableType
= disp
->ClientAPIs
;
410 base
.Conformant
= disp
->ClientAPIs
;
412 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
413 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
415 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
416 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
420 config_id
= base
.ConfigID
;
421 base
.ConfigID
= EGL_DONT_CARE
;
422 base
.SurfaceType
= EGL_DONT_CARE
;
423 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
424 (_EGLArrayForEach
) dri2_match_config
, &base
);
426 if (num_configs
== 1) {
427 conf
= (struct dri2_egl_config
*) matching_config
;
429 if (!conf
->dri_config
[double_buffer
][srgb
])
430 conf
->dri_config
[double_buffer
][srgb
] = dri_config
;
432 /* a similar config type is already added (unlikely) => discard */
435 else if (num_configs
== 0) {
436 conf
= calloc(1, sizeof *conf
);
440 conf
->dri_config
[double_buffer
][srgb
] = dri_config
;
442 memcpy(&conf
->base
, &base
, sizeof base
);
443 conf
->base
.SurfaceType
= 0;
444 conf
->base
.ConfigID
= config_id
;
446 _eglLinkConfig(&conf
->base
);
449 unreachable("duplicates should not be possible");
454 surface_type
&= ~EGL_PIXMAP_BIT
;
457 /* No support for pbuffer + MSAA for now.
459 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
460 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
463 surface_type
&= ~EGL_PBUFFER_BIT
;
466 conf
->base
.SurfaceType
|= surface_type
;
472 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
474 _EGLDisplay
*disp
= data
;
475 struct dri2_egl_image
*dri2_img
;
480 img
= _eglLookupImage(image
, disp
);
482 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
486 dri2_img
= dri2_egl_image(image
);
488 return dri2_img
->dri_image
;
491 const __DRIimageLookupExtension image_lookup_extension
= {
492 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
494 .lookupEGLImage
= dri2_lookup_egl_image
497 struct dri2_extension_match
{
503 static const struct dri2_extension_match dri3_driver_extensions
[] = {
504 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
505 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
509 static const struct dri2_extension_match dri2_driver_extensions
[] = {
510 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
511 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
515 static const struct dri2_extension_match dri2_core_extensions
[] = {
516 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
517 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
518 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
522 static const struct dri2_extension_match swrast_driver_extensions
[] = {
523 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
524 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
528 static const struct dri2_extension_match swrast_core_extensions
[] = {
529 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
533 static const struct dri2_extension_match optional_driver_extensions
[] = {
534 { __DRI_CONFIG_OPTIONS
, 1, offsetof(struct dri2_egl_display
, configOptions
) },
538 static const struct dri2_extension_match optional_core_extensions
[] = {
539 { __DRI2_ROBUSTNESS
, 1, offsetof(struct dri2_egl_display
, robustness
) },
540 { __DRI2_NO_ERROR
, 1, offsetof(struct dri2_egl_display
, no_error
) },
541 { __DRI2_CONFIG_QUERY
, 1, offsetof(struct dri2_egl_display
, config
) },
542 { __DRI2_FENCE
, 1, offsetof(struct dri2_egl_display
, fence
) },
543 { __DRI2_BUFFER_DAMAGE
, 1, offsetof(struct dri2_egl_display
, buffer_damage
) },
544 { __DRI2_RENDERER_QUERY
, 1, offsetof(struct dri2_egl_display
, rendererQuery
) },
545 { __DRI2_INTEROP
, 1, offsetof(struct dri2_egl_display
, interop
) },
546 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
547 { __DRI2_FLUSH_CONTROL
, 1, offsetof(struct dri2_egl_display
, flush_control
) },
548 { __DRI2_BLOB
, 1, offsetof(struct dri2_egl_display
, blob
) },
549 { __DRI_MUTABLE_RENDER_BUFFER_DRIVER
, 1, offsetof(struct dri2_egl_display
, mutable_render_buffer
) },
554 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
555 const struct dri2_extension_match
*matches
,
556 const __DRIextension
**extensions
,
562 for (int i
= 0; extensions
[i
]; i
++) {
563 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
564 for (int j
= 0; matches
[j
].name
; j
++) {
565 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
566 extensions
[i
]->version
>= matches
[j
].version
) {
567 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
568 *(const __DRIextension
**) field
= extensions
[i
];
569 _eglLog(_EGL_INFO
, "found extension %s version %d",
570 extensions
[i
]->name
, extensions
[i
]->version
);
576 for (int j
= 0; matches
[j
].name
; j
++) {
577 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
578 if (*(const __DRIextension
**) field
== NULL
) {
580 _eglLog(_EGL_DEBUG
, "did not find optional extension %s version %d",
581 matches
[j
].name
, matches
[j
].version
);
583 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
584 matches
[j
].name
, matches
[j
].version
);
593 static const __DRIextension
**
594 dri2_open_driver(_EGLDisplay
*disp
)
596 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
597 static const char *search_path_vars
[] = {
598 "LIBGL_DRIVERS_PATH",
602 return loader_open_driver(dri2_dpy
->driver_name
,
608 dri2_load_driver_common(_EGLDisplay
*disp
,
609 const struct dri2_extension_match
*driver_extensions
)
611 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
612 const __DRIextension
**extensions
;
614 extensions
= dri2_open_driver(disp
);
618 if (!dri2_bind_extensions(dri2_dpy
, driver_extensions
, extensions
, false)) {
619 dlclose(dri2_dpy
->driver
);
622 dri2_dpy
->driver_extensions
= extensions
;
624 dri2_bind_extensions(dri2_dpy
, optional_driver_extensions
, extensions
, true);
630 dri2_load_driver(_EGLDisplay
*disp
)
632 return dri2_load_driver_common(disp
, dri2_driver_extensions
);
636 dri2_load_driver_dri3(_EGLDisplay
*disp
)
638 return dri2_load_driver_common(disp
, dri3_driver_extensions
);
642 dri2_load_driver_swrast(_EGLDisplay
*disp
)
644 return dri2_load_driver_common(disp
, swrast_driver_extensions
);
648 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
650 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
651 unsigned int value
= 0;
653 if (!rendererQuery
||
654 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
661 dri2_query_driver_name(_EGLDisplay
*disp
)
663 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
664 return dri2_dpy
->driver_name
;
668 dri2_query_driver_config(_EGLDisplay
*disp
)
670 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
671 const __DRIconfigOptionsExtension
*ext
= dri2_dpy
->configOptions
;
673 if (ext
->base
.version
>= 2)
674 return ext
->getXml(dri2_dpy
->driver_name
);
676 return strdup(ext
->xml
);
681 dri2_setup_screen(_EGLDisplay
*disp
)
683 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
684 unsigned int api_mask
;
687 * EGL 1.5 specification defines the default value to 1. Moreover,
688 * eglSwapInterval() is required to clamp requested value to the supported
689 * range. Since the default value is implicitly assumed to be supported,
690 * use it as both minimum and maximum for the platforms that do not allow
691 * changing the interval. Platforms, which allow it (e.g. x11, wayland)
692 * override these values already.
694 dri2_dpy
->min_swap_interval
= 1;
695 dri2_dpy
->max_swap_interval
= 1;
696 dri2_dpy
->default_swap_interval
= 1;
698 if (dri2_dpy
->image_driver
) {
699 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
700 } else if (dri2_dpy
->dri2
) {
701 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
703 assert(dri2_dpy
->swrast
);
704 api_mask
= 1 << __DRI_API_OPENGL
|
705 1 << __DRI_API_GLES
|
706 1 << __DRI_API_GLES2
|
707 1 << __DRI_API_GLES3
;
710 disp
->ClientAPIs
= 0;
711 if ((api_mask
& (1 <<__DRI_API_OPENGL
)) && _eglIsApiValid(EGL_OPENGL_API
))
712 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
713 if ((api_mask
& (1 << __DRI_API_GLES
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
714 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
715 if ((api_mask
& (1 << __DRI_API_GLES2
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
716 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
717 if ((api_mask
& (1 << __DRI_API_GLES3
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
718 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
720 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
721 disp
->Extensions
.KHR_no_config_context
= EGL_TRUE
;
722 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
724 if (dri2_dpy
->configOptions
) {
725 disp
->Extensions
.MESA_query_driver
= EGL_TRUE
;
728 /* Report back to EGL the bitmask of priorities supported */
729 disp
->Extensions
.IMG_context_priority
=
730 dri2_renderer_query_integer(dri2_dpy
,
731 __DRI2_RENDERER_HAS_CONTEXT_PRIORITY
);
733 disp
->Extensions
.EXT_pixel_format_float
= EGL_TRUE
;
735 if (dri2_renderer_query_integer(dri2_dpy
,
736 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
737 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
739 if (dri2_dpy
->image_driver
||
740 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
741 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
742 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
744 if (dri2_dpy
->robustness
)
745 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
748 if (dri2_dpy
->no_error
)
749 disp
->Extensions
.KHR_create_context_no_error
= EGL_TRUE
;
751 if (dri2_dpy
->fence
) {
752 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
753 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
754 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
755 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
756 if (dri2_dpy
->fence
->base
.version
>= 2 &&
757 dri2_dpy
->fence
->get_capabilities
) {
758 unsigned capabilities
=
759 dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
);
760 disp
->Extensions
.ANDROID_native_fence_sync
=
761 (capabilities
& __DRI_FENCE_CAP_NATIVE_FD
) != 0;
766 disp
->Extensions
.ANDROID_blob_cache
= EGL_TRUE
;
768 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
770 if (dri2_dpy
->image
) {
771 if (dri2_dpy
->image
->base
.version
>= 10 &&
772 dri2_dpy
->image
->getCapabilities
!= NULL
) {
775 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
776 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
778 if (dri2_dpy
->image
->base
.version
>= 11)
779 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
781 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
782 if (dri2_dpy
->image
->base
.version
>= 11)
783 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
786 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
787 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
788 if (dri2_dpy
->image
->base
.version
>= 5 &&
789 dri2_dpy
->image
->createImageFromTexture
) {
790 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
791 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
793 if (dri2_renderer_query_integer(dri2_dpy
,
794 __DRI2_RENDERER_HAS_TEXTURE_3D
))
795 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
798 if (dri2_dpy
->image
->base
.version
>= 8 &&
799 dri2_dpy
->image
->createImageFromDmaBufs
) {
800 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
802 if (dri2_dpy
->image
->base
.version
>= 15 &&
803 dri2_dpy
->image
->createImageFromDmaBufs2
&&
804 dri2_dpy
->image
->queryDmaBufFormats
&&
805 dri2_dpy
->image
->queryDmaBufModifiers
) {
806 disp
->Extensions
.EXT_image_dma_buf_import_modifiers
= EGL_TRUE
;
811 if (dri2_dpy
->flush_control
)
812 disp
->Extensions
.KHR_context_flush_control
= EGL_TRUE
;
814 if (dri2_dpy
->buffer_damage
&& dri2_dpy
->buffer_damage
->set_damage_region
)
815 disp
->Extensions
.KHR_partial_update
= EGL_TRUE
;
819 dri2_setup_swap_interval(_EGLDisplay
*disp
, int max_swap_interval
)
821 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
822 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
824 /* Allow driconf to override applications.*/
825 if (dri2_dpy
->config
)
826 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
827 "vblank_mode", &vblank_mode
);
828 switch (vblank_mode
) {
829 case DRI_CONF_VBLANK_NEVER
:
830 dri2_dpy
->min_swap_interval
= 0;
831 dri2_dpy
->max_swap_interval
= 0;
832 dri2_dpy
->default_swap_interval
= 0;
834 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
835 dri2_dpy
->min_swap_interval
= 1;
836 dri2_dpy
->max_swap_interval
= max_swap_interval
;
837 dri2_dpy
->default_swap_interval
= 1;
839 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
840 dri2_dpy
->min_swap_interval
= 0;
841 dri2_dpy
->max_swap_interval
= max_swap_interval
;
842 dri2_dpy
->default_swap_interval
= 0;
845 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
846 dri2_dpy
->min_swap_interval
= 0;
847 dri2_dpy
->max_swap_interval
= max_swap_interval
;
848 dri2_dpy
->default_swap_interval
= 1;
853 /* All platforms but DRM call this function to create the screen and populate
854 * the driver_configs. DRM inherits that information from its display - GBM.
857 dri2_create_screen(_EGLDisplay
*disp
)
859 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
861 if (dri2_dpy
->image_driver
) {
862 dri2_dpy
->dri_screen
=
863 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
864 dri2_dpy
->loader_extensions
,
865 dri2_dpy
->driver_extensions
,
866 &dri2_dpy
->driver_configs
,
868 } else if (dri2_dpy
->dri2
) {
869 if (dri2_dpy
->dri2
->base
.version
>= 4) {
870 dri2_dpy
->dri_screen
=
871 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
872 dri2_dpy
->loader_extensions
,
873 dri2_dpy
->driver_extensions
,
874 &dri2_dpy
->driver_configs
, disp
);
876 dri2_dpy
->dri_screen
=
877 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
878 dri2_dpy
->loader_extensions
,
879 &dri2_dpy
->driver_configs
, disp
);
882 assert(dri2_dpy
->swrast
);
883 if (dri2_dpy
->swrast
->base
.version
>= 4) {
884 dri2_dpy
->dri_screen
=
885 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->loader_extensions
,
886 dri2_dpy
->driver_extensions
,
887 &dri2_dpy
->driver_configs
, disp
);
889 dri2_dpy
->dri_screen
=
890 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->loader_extensions
,
891 &dri2_dpy
->driver_configs
, disp
);
895 if (dri2_dpy
->dri_screen
== NULL
) {
896 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
900 dri2_dpy
->own_dri_screen
= true;
905 dri2_setup_extensions(_EGLDisplay
*disp
)
907 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
908 const struct dri2_extension_match
*mandatory_core_extensions
;
909 const __DRIextension
**extensions
;
911 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
913 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
)
914 mandatory_core_extensions
= dri2_core_extensions
;
916 mandatory_core_extensions
= swrast_core_extensions
;
918 if (!dri2_bind_extensions(dri2_dpy
, mandatory_core_extensions
, extensions
, false))
921 #ifdef HAVE_DRI3_MODIFIERS
922 dri2_dpy
->multibuffers_available
=
923 (dri2_dpy
->dri3_major_version
> 1 || (dri2_dpy
->dri3_major_version
== 1 &&
924 dri2_dpy
->dri3_minor_version
>= 2)) &&
925 (dri2_dpy
->present_major_version
> 1 || (dri2_dpy
->present_major_version
== 1 &&
926 dri2_dpy
->present_minor_version
>= 2)) &&
927 (dri2_dpy
->image
&& dri2_dpy
->image
->base
.version
>= 15);
930 dri2_bind_extensions(dri2_dpy
, optional_core_extensions
, extensions
, true);
935 * Called via eglInitialize(), GLX_drv->API.Initialize().
937 * This must be guaranteed to be called exactly once, even if eglInitialize is
938 * called many times (without a eglTerminate in between).
941 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
943 EGLBoolean ret
= EGL_FALSE
;
944 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
946 /* In the case where the application calls eglMakeCurrent(context1),
947 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
948 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
949 * initialized, as we need it to be able to free context1 correctly.
951 * It would probably be safest to forcibly release the display with
952 * dri2_display_release, to make sure the display is reinitialized correctly.
953 * However, the EGL spec states that we need to keep a reference to the
954 * current context (so we cannot call dri2_make_current(NULL)), and therefore
955 * we would leak context1 as we would be missing the old display connection
956 * to free it up correctly.
959 dri2_dpy
->ref_count
++;
963 loader_set_logger(_eglLog
);
965 switch (disp
->Platform
) {
966 case _EGL_PLATFORM_SURFACELESS
:
967 ret
= dri2_initialize_surfaceless(drv
, disp
);
969 case _EGL_PLATFORM_DEVICE
:
970 ret
= dri2_initialize_device(drv
, disp
);
972 case _EGL_PLATFORM_X11
:
973 ret
= dri2_initialize_x11(drv
, disp
);
975 case _EGL_PLATFORM_DRM
:
976 ret
= dri2_initialize_drm(drv
, disp
);
978 case _EGL_PLATFORM_WAYLAND
:
979 ret
= dri2_initialize_wayland(drv
, disp
);
981 case _EGL_PLATFORM_ANDROID
:
982 ret
= dri2_initialize_android(drv
, disp
);
985 unreachable("Callers ensure we cannot get here.");
992 dri2_dpy
= dri2_egl_display(disp
);
993 dri2_dpy
->ref_count
++;
999 * Decrement display reference count, and free up display if necessary.
1002 dri2_display_release(_EGLDisplay
*disp
)
1004 struct dri2_egl_display
*dri2_dpy
;
1009 dri2_dpy
= dri2_egl_display(disp
);
1011 assert(dri2_dpy
->ref_count
> 0);
1012 dri2_dpy
->ref_count
--;
1014 if (dri2_dpy
->ref_count
> 0)
1017 _eglCleanupDisplay(disp
);
1018 dri2_display_destroy(disp
);
1022 dri2_display_destroy(_EGLDisplay
*disp
)
1024 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1026 if (dri2_dpy
->own_dri_screen
) {
1027 if (dri2_dpy
->vtbl
&& dri2_dpy
->vtbl
->close_screen_notify
)
1028 dri2_dpy
->vtbl
->close_screen_notify(disp
);
1029 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1031 if (dri2_dpy
->fd
>= 0)
1032 close(dri2_dpy
->fd
);
1033 if (dri2_dpy
->driver
)
1034 dlclose(dri2_dpy
->driver
);
1035 free(dri2_dpy
->driver_name
);
1037 #ifdef HAVE_WAYLAND_PLATFORM
1038 free(dri2_dpy
->device_name
);
1041 switch (disp
->Platform
) {
1042 case _EGL_PLATFORM_X11
:
1043 dri2_teardown_x11(dri2_dpy
);
1045 case _EGL_PLATFORM_DRM
:
1046 dri2_teardown_drm(dri2_dpy
);
1048 case _EGL_PLATFORM_WAYLAND
:
1049 dri2_teardown_wayland(dri2_dpy
);
1052 /* TODO: add teardown for other platforms */
1056 /* The drm platform does not create the screen/driver_configs but reuses
1057 * the ones from the gbm device. As such the gbm itself is responsible
1060 if (disp
->Platform
!= _EGL_PLATFORM_DRM
&& dri2_dpy
->driver_configs
) {
1061 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
1062 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
1063 free(dri2_dpy
->driver_configs
);
1066 disp
->DriverData
= NULL
;
1070 dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface
*dri2_surf
,
1071 unsigned int att
, unsigned int format
)
1073 struct dri2_egl_display
*dri2_dpy
=
1074 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1076 if (att
>= ARRAY_SIZE(dri2_surf
->local_buffers
))
1079 if (!dri2_surf
->local_buffers
[att
]) {
1080 dri2_surf
->local_buffers
[att
] =
1081 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
, att
, format
,
1082 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
1085 return dri2_surf
->local_buffers
[att
];
1089 dri2_egl_surface_free_local_buffers(struct dri2_egl_surface
*dri2_surf
)
1091 struct dri2_egl_display
*dri2_dpy
=
1092 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1094 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->local_buffers
); i
++) {
1095 if (dri2_surf
->local_buffers
[i
]) {
1096 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
1097 dri2_surf
->local_buffers
[i
]);
1098 dri2_surf
->local_buffers
[i
] = NULL
;
1104 * Called via eglTerminate(), drv->API.Terminate().
1106 * This must be guaranteed to be called exactly once, even if eglTerminate is
1107 * called many times (without a eglInitialize in between).
1110 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1112 /* Release all non-current Context/Surfaces. */
1113 _eglReleaseDisplayResources(drv
, disp
);
1115 dri2_display_release(disp
);
1121 * Set the error code after a call to
1122 * dri2_egl_display::dri2::createContextAttribs.
1125 dri2_create_context_attribs_error(int dri_error
)
1129 switch (dri_error
) {
1130 case __DRI_CTX_ERROR_SUCCESS
:
1133 case __DRI_CTX_ERROR_NO_MEMORY
:
1134 egl_error
= EGL_BAD_ALLOC
;
1137 /* From the EGL_KHR_create_context spec, section "Errors":
1139 * * If <config> does not support a client API context compatible
1140 * with the requested API major and minor version, [...] context flags,
1141 * and context reset notification behavior (for client API types where
1142 * these attributes are supported), then an EGL_BAD_MATCH error is
1145 * * If an OpenGL ES context is requested and the values for
1146 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1147 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1148 * is not defined, than an EGL_BAD_MATCH error is generated.
1150 * * If an OpenGL context is requested, the requested version is
1151 * greater than 3.2, and the value for attribute
1152 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1153 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1154 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1155 * one of these bits set; or if the implementation does not support
1156 * the requested profile, then an EGL_BAD_MATCH error is generated.
1158 case __DRI_CTX_ERROR_BAD_API
:
1159 case __DRI_CTX_ERROR_BAD_VERSION
:
1160 case __DRI_CTX_ERROR_BAD_FLAG
:
1161 egl_error
= EGL_BAD_MATCH
;
1164 /* From the EGL_KHR_create_context spec, section "Errors":
1166 * * If an attribute name or attribute value in <attrib_list> is not
1167 * recognized (including unrecognized bits in bitmask attributes),
1168 * then an EGL_BAD_ATTRIBUTE error is generated."
1170 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
1171 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
1172 egl_error
= EGL_BAD_ATTRIBUTE
;
1176 assert(!"unknown dri_error code");
1177 egl_error
= EGL_BAD_MATCH
;
1181 _eglError(egl_error
, "dri2_create_context");
1185 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1186 struct dri2_egl_display
*dri2_dpy
,
1187 uint32_t *ctx_attribs
,
1188 unsigned *num_attribs
)
1192 assert(*num_attribs
>= NUM_ATTRIBS
);
1194 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1195 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1196 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1197 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1199 if (dri2_ctx
->base
.Flags
!= 0 || dri2_ctx
->base
.NoError
) {
1200 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1201 * extension, don't even try to send it the robust-access flag.
1202 * It may explode. Instead, generate the required EGL error here.
1204 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1205 && !dri2_dpy
->robustness
) {
1206 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1210 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1211 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
|
1212 (dri2_ctx
->base
.NoError
? __DRI_CTX_FLAG_NO_ERROR
: 0);
1215 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1216 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1217 * extension, don't even try to send it a reset strategy. It may
1218 * explode. Instead, generate the required EGL error here.
1220 if (!dri2_dpy
->robustness
) {
1221 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1225 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1226 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1229 if (dri2_ctx
->base
.ContextPriority
!= EGL_CONTEXT_PRIORITY_MEDIUM_IMG
) {
1232 switch (dri2_ctx
->base
.ContextPriority
) {
1233 case EGL_CONTEXT_PRIORITY_HIGH_IMG
:
1234 val
= __DRI_CTX_PRIORITY_HIGH
;
1236 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG
:
1237 val
= __DRI_CTX_PRIORITY_MEDIUM
;
1239 case EGL_CONTEXT_PRIORITY_LOW_IMG
:
1240 val
= __DRI_CTX_PRIORITY_LOW
;
1243 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1247 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_PRIORITY
;
1248 ctx_attribs
[pos
++] = val
;
1251 if (dri2_ctx
->base
.ReleaseBehavior
== EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR
) {
1252 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR
;
1253 ctx_attribs
[pos
++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE
;
1262 * Called via eglCreateContext(), drv->API.CreateContext().
1264 static _EGLContext
*
1265 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1266 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1268 struct dri2_egl_context
*dri2_ctx
;
1269 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1270 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1271 __DRIcontext
*shared
=
1272 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1273 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1274 const __DRIconfig
*dri_config
;
1277 unsigned num_attribs
= NUM_ATTRIBS
;
1278 uint32_t ctx_attribs
[NUM_ATTRIBS
];
1282 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1284 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1288 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1291 /* The EGL_EXT_create_context_robustness spec says:
1293 * "Add to the eglCreateContext context creation errors: [...]
1295 * * If the reset notification behavior of <share_context> and the
1296 * newly created context are different then an EGL_BAD_MATCH error is
1299 if (share_list
&& share_list
->ResetNotificationStrategy
!=
1300 dri2_ctx
->base
.ResetNotificationStrategy
) {
1301 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1305 /* The EGL_KHR_create_context_no_error spec says:
1307 * "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
1308 * used to create <share_context> does not match the value of
1309 * EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
1311 if (share_list
&& share_list
->NoError
!= dri2_ctx
->base
.NoError
) {
1312 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1316 switch (dri2_ctx
->base
.ClientAPI
) {
1317 case EGL_OPENGL_ES_API
:
1318 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1320 api
= __DRI_API_GLES
;
1323 api
= __DRI_API_GLES2
;
1326 api
= __DRI_API_GLES3
;
1329 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1334 case EGL_OPENGL_API
:
1335 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1336 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1337 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1338 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1339 api
= __DRI_API_OPENGL_CORE
;
1340 else if (dri2_ctx
->base
.ClientMajorVersion
== 3 &&
1341 dri2_ctx
->base
.ClientMinorVersion
== 1)
1342 api
= __DRI_API_OPENGL_CORE
;
1344 api
= __DRI_API_OPENGL
;
1347 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1353 /* The config chosen here isn't necessarily
1354 * used for surfaces later.
1355 * A pixmap surface will use the single config.
1356 * This opportunity depends on disabling the
1357 * doubleBufferMode check in
1358 * src/mesa/main/context.c:check_compatible()
1360 if (dri2_config
->dri_config
[1][0])
1361 dri_config
= dri2_config
->dri_config
[1][0];
1363 dri_config
= dri2_config
->dri_config
[0][0];
1368 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1372 if (dri2_dpy
->image_driver
) {
1373 dri2_ctx
->dri_context
=
1374 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1382 dri2_create_context_attribs_error(error
);
1383 } else if (dri2_dpy
->dri2
) {
1384 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1385 dri2_ctx
->dri_context
=
1386 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1394 dri2_create_context_attribs_error(error
);
1396 dri2_ctx
->dri_context
=
1397 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1404 assert(dri2_dpy
->swrast
);
1405 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1406 dri2_ctx
->dri_context
=
1407 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1415 dri2_create_context_attribs_error(error
);
1417 dri2_ctx
->dri_context
=
1418 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1426 if (!dri2_ctx
->dri_context
)
1429 return &dri2_ctx
->base
;
1437 * Called via eglDestroyContext(), drv->API.DestroyContext().
1440 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1442 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1443 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1445 if (_eglPutContext(ctx
)) {
1446 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1454 dri2_init_surface(_EGLSurface
*surf
, _EGLDisplay
*disp
, EGLint type
,
1455 _EGLConfig
*conf
, const EGLint
*attrib_list
,
1456 EGLBoolean enable_out_fence
, void *native_surface
)
1458 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1459 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1461 dri2_surf
->out_fence_fd
= -1;
1462 dri2_surf
->enable_out_fence
= false;
1463 if (dri2_dpy
->fence
&& dri2_dpy
->fence
->base
.version
>= 2 &&
1464 dri2_dpy
->fence
->get_capabilities
&&
1465 (dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
) &
1466 __DRI_FENCE_CAP_NATIVE_FD
)) {
1467 dri2_surf
->enable_out_fence
= enable_out_fence
;
1470 return _eglInitSurface(surf
, disp
, type
, conf
, attrib_list
, native_surface
);
1474 dri2_surface_set_out_fence_fd( _EGLSurface
*surf
, int fence_fd
)
1476 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1478 if (dri2_surf
->out_fence_fd
>= 0)
1479 close(dri2_surf
->out_fence_fd
);
1481 dri2_surf
->out_fence_fd
= fence_fd
;
1485 dri2_fini_surface(_EGLSurface
*surf
)
1487 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1489 dri2_surface_set_out_fence_fd(surf
, -1);
1490 dri2_surf
->enable_out_fence
= false;
1494 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1496 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1498 if (!_eglPutSurface(surf
))
1501 return dri2_dpy
->vtbl
->destroy_surface(drv
, disp
, surf
);
1505 dri2_surf_update_fence_fd(_EGLContext
*ctx
,
1506 _EGLDisplay
*disp
, _EGLSurface
*surf
)
1508 __DRIcontext
*dri_ctx
= dri2_egl_context(ctx
)->dri_context
;
1509 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1510 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1514 if (!dri2_surf
->enable_out_fence
)
1517 fence
= dri2_dpy
->fence
->create_fence_fd(dri_ctx
, -1);
1519 fence_fd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
1521 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, fence
);
1523 dri2_surface_set_out_fence_fd(surf
, fence_fd
);
1527 dri2_create_drawable(struct dri2_egl_display
*dri2_dpy
,
1528 const __DRIconfig
*config
,
1529 struct dri2_egl_surface
*dri2_surf
,
1530 void *loaderPrivate
)
1532 __DRIcreateNewDrawableFunc createNewDrawable
;
1534 if (dri2_dpy
->image_driver
)
1535 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
1536 else if (dri2_dpy
->dri2
)
1537 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
1538 else if (dri2_dpy
->swrast
)
1539 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
1541 return _eglError(EGL_BAD_ALLOC
, "no createNewDrawable");
1543 dri2_surf
->dri_drawable
= createNewDrawable(dri2_dpy
->dri_screen
,
1544 config
, loaderPrivate
);
1545 if (dri2_surf
->dri_drawable
== NULL
)
1546 return _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
1552 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1555 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1556 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1558 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1559 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1560 _EGLDisplay
*old_disp
= NULL
;
1561 struct dri2_egl_display
*old_dri2_dpy
= NULL
;
1562 _EGLContext
*old_ctx
;
1563 _EGLSurface
*old_dsurf
, *old_rsurf
;
1564 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1565 __DRIdrawable
*ddraw
, *rdraw
;
1570 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1572 /* make new bindings */
1573 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1574 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1579 old_disp
= old_ctx
->Resource
.Display
;
1580 old_dri2_dpy
= dri2_egl_display(old_disp
);
1583 /* flush before context switch */
1587 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1588 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1589 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1592 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1595 dri2_surf_update_fence_fd(old_ctx
, disp
, old_dsurf
);
1597 /* Disable shared buffer mode */
1598 if (old_dsurf
&& _eglSurfaceInSharedBufferMode(old_dsurf
) &&
1599 old_dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1600 old_dri2_dpy
->vtbl
->set_shared_buffer_mode(old_disp
, old_dsurf
, false);
1603 dri2_dpy
->core
->unbindContext(old_cctx
);
1606 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1608 if (!unbind
&& !dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1609 /* undo the previous _eglBindContext */
1610 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1611 assert(&dri2_ctx
->base
== ctx
&&
1612 tmp_dsurf
== dsurf
&&
1613 tmp_rsurf
== rsurf
);
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
, true);
1620 _eglPutSurface(dsurf
);
1621 _eglPutSurface(rsurf
);
1622 _eglPutContext(ctx
);
1624 _eglPutSurface(old_dsurf
);
1625 _eglPutSurface(old_rsurf
);
1626 _eglPutContext(old_ctx
);
1628 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1629 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1632 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1635 dri2_destroy_surface(drv
, disp
, old_dsurf
);
1636 dri2_destroy_surface(drv
, disp
, old_rsurf
);
1639 dri2_dpy
->ref_count
++;
1642 dri2_destroy_context(drv
, disp
, old_ctx
);
1643 dri2_display_release(old_disp
);
1646 if (dsurf
&& _eglSurfaceHasMutableRenderBuffer(dsurf
) &&
1647 dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1648 /* Always update the shared buffer mode. This is obviously needed when
1649 * the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When
1650 * EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the
1651 * case where external non-EGL API may have changed window's shared
1652 * buffer mode since we last saw it.
1654 bool mode
= (dsurf
->ActiveRenderBuffer
== EGL_SINGLE_BUFFER
);
1655 dri2_dpy
->vtbl
->set_shared_buffer_mode(disp
, dsurf
, mode
);
1662 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1664 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1666 return dri2_surf
->dri_drawable
;
1670 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1673 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1675 return _glapi_get_proc_address(procname
);
1679 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1680 _EGLConfig
*conf
, void *native_window
,
1681 const EGLint
*attrib_list
)
1683 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1684 return dri2_dpy
->vtbl
->create_window_surface(drv
, disp
, conf
, native_window
,
1689 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1690 _EGLConfig
*conf
, void *native_pixmap
,
1691 const EGLint
*attrib_list
)
1693 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1694 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, disp
, conf
, native_pixmap
,
1699 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1700 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1702 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1703 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, disp
, conf
, attrib_list
);
1707 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1710 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1711 if (!dri2_dpy
->vtbl
->swap_interval
)
1713 return dri2_dpy
->vtbl
->swap_interval(drv
, disp
, surf
, interval
);
1717 * Asks the client API to flush any rendering to the drawable so that we can
1718 * do our swapbuffers.
1721 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1723 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1724 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1726 if (dri2_dpy
->flush
) {
1727 if (dri2_dpy
->flush
->base
.version
>= 4) {
1728 /* We know there's a current context because:
1730 * "If surface is not bound to the calling thread’s current
1731 * context, an EGL_BAD_SURFACE error is generated."
1733 _EGLContext
*ctx
= _eglGetCurrentContext();
1734 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1736 /* From the EGL 1.4 spec (page 52):
1738 * "The contents of ancillary buffers are always undefined
1739 * after calling eglSwapBuffers."
1741 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1743 __DRI2_FLUSH_DRAWABLE
|
1744 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1745 __DRI2_THROTTLE_SWAPBUFFER
);
1747 dri2_dpy
->flush
->flush(dri_drawable
);
1753 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1755 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1756 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1757 _EGLContext
*ctx
= _eglGetCurrentContext();
1761 dri2_surf_update_fence_fd(ctx
, disp
, surf
);
1762 ret
= dri2_dpy
->vtbl
->swap_buffers(drv
, disp
, surf
);
1764 /* SwapBuffers marks the end of the frame; reset the damage region for
1765 * use again next time.
1767 if (ret
&& dri2_dpy
->buffer_damage
&&
1768 dri2_dpy
->buffer_damage
->set_damage_region
)
1769 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
1775 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1777 const EGLint
*rects
, EGLint n_rects
)
1779 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1780 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1781 _EGLContext
*ctx
= _eglGetCurrentContext();
1785 dri2_surf_update_fence_fd(ctx
, disp
, surf
);
1786 ret
= dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, disp
, surf
,
1789 /* SwapBuffers marks the end of the frame; reset the damage region for
1790 * use again next time.
1792 if (ret
&& dri2_dpy
->buffer_damage
&&
1793 dri2_dpy
->buffer_damage
->set_damage_region
)
1794 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
1800 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1801 EGLint numRects
, const EGLint
*rects
)
1803 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1804 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1807 ret
= dri2_dpy
->vtbl
->swap_buffers_region(drv
, disp
, surf
, numRects
, rects
);
1809 /* SwapBuffers marks the end of the frame; reset the damage region for
1810 * use again next time.
1812 if (ret
&& dri2_dpy
->buffer_damage
&&
1813 dri2_dpy
->buffer_damage
->set_damage_region
)
1814 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
1820 dri2_set_damage_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1821 EGLint
*rects
, EGLint n_rects
)
1823 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1824 __DRIdrawable
*drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1826 if (!dri2_dpy
->buffer_damage
|| !dri2_dpy
->buffer_damage
->set_damage_region
)
1829 dri2_dpy
->buffer_damage
->set_damage_region(drawable
, n_rects
, rects
);
1834 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1835 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1837 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1838 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, disp
, surf
, x
, y
, width
, height
);
1842 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1843 void *native_pixmap_target
)
1845 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1846 return dri2_dpy
->vtbl
->copy_buffers(drv
, disp
, surf
, native_pixmap_target
);
1850 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1852 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1853 return dri2_dpy
->vtbl
->query_buffer_age(drv
, disp
, surf
);
1857 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1859 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1860 _EGLSurface
*surf
= ctx
->DrawSurface
;
1861 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1865 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1866 * we need to copy fake to real here.*/
1868 if (dri2_dpy
->flush
!= NULL
)
1869 dri2_dpy
->flush
->flush(dri_drawable
);
1875 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1880 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1881 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1888 dri2_bind_tex_image(_EGLDriver
*drv
,
1889 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1891 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1892 struct dri2_egl_context
*dri2_ctx
;
1894 GLint format
, target
;
1895 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1897 ctx
= _eglGetCurrentContext();
1898 dri2_ctx
= dri2_egl_context(ctx
);
1900 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1903 switch (surf
->TextureFormat
) {
1904 case EGL_TEXTURE_RGB
:
1905 format
= __DRI_TEXTURE_FORMAT_RGB
;
1907 case EGL_TEXTURE_RGBA
:
1908 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1911 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1912 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1915 switch (surf
->TextureTarget
) {
1916 case EGL_TEXTURE_2D
:
1917 target
= GL_TEXTURE_2D
;
1920 target
= GL_TEXTURE_2D
;
1921 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1924 dri2_dpy
->tex_buffer
->setTexBuffer2(dri2_ctx
->dri_context
,
1932 dri2_release_tex_image(_EGLDriver
*drv
,
1933 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1935 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1936 struct dri2_egl_context
*dri2_ctx
;
1939 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1941 ctx
= _eglGetCurrentContext();
1942 dri2_ctx
= dri2_egl_context(ctx
);
1944 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1947 switch (surf
->TextureTarget
) {
1948 case EGL_TEXTURE_2D
:
1949 target
= GL_TEXTURE_2D
;
1952 assert(!"missing texture target");
1955 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1956 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1957 dri2_dpy
->tex_buffer
->releaseTexBuffer(dri2_ctx
->dri_context
,
1958 target
, dri_drawable
);
1965 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
,
1966 EGLenum target
, EGLClientBuffer buffer
,
1967 const EGLint
*attr_list
)
1969 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1970 return dri2_dpy
->vtbl
->create_image(drv
, disp
, ctx
, target
, buffer
,
1975 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1977 struct dri2_egl_image
*dri2_img
;
1979 if (dri_image
== NULL
) {
1980 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1984 dri2_img
= malloc(sizeof *dri2_img
);
1986 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1990 _eglInitImage(&dri2_img
->base
, disp
);
1992 dri2_img
->dri_image
= dri_image
;
1994 return &dri2_img
->base
;
1998 * Translate a DRI Image extension error code into an EGL error code.
2001 egl_error_from_dri_image_error(int dri_error
)
2003 switch (dri_error
) {
2004 case __DRI_IMAGE_ERROR_SUCCESS
:
2006 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
2007 return EGL_BAD_ALLOC
;
2008 case __DRI_IMAGE_ERROR_BAD_MATCH
:
2009 return EGL_BAD_MATCH
;
2010 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
2011 return EGL_BAD_PARAMETER
;
2012 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
2013 return EGL_BAD_ACCESS
;
2015 assert(!"unknown dri_error code");
2016 return EGL_BAD_ALLOC
;
2021 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2022 EGLClientBuffer buffer
,
2023 const EGLint
*attr_list
)
2025 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2026 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2027 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
2028 __DRIimage
*dri_image
;
2030 if (renderbuffer
== 0) {
2031 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2032 return EGL_NO_IMAGE_KHR
;
2035 if (!disp
->Extensions
.KHR_gl_renderbuffer_image
) {
2036 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2037 return EGL_NO_IMAGE_KHR
;
2040 if (dri2_dpy
->image
->base
.version
>= 17 &&
2041 dri2_dpy
->image
->createImageFromRenderbuffer2
) {
2042 unsigned error
= ~0;
2044 dri_image
= dri2_dpy
->image
->createImageFromRenderbuffer2(
2045 dri2_ctx
->dri_context
, renderbuffer
, NULL
, &error
);
2047 assert(!!dri_image
== (error
== __DRI_IMAGE_ERROR_SUCCESS
));
2050 _eglError(egl_error_from_dri_image_error(error
), "dri2_create_image_khr");
2051 return EGL_NO_IMAGE_KHR
;
2054 dri_image
= dri2_dpy
->image
->createImageFromRenderbuffer(
2055 dri2_ctx
->dri_context
, renderbuffer
, NULL
);
2057 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2058 return EGL_NO_IMAGE_KHR
;
2062 return dri2_create_image_from_dri(disp
, dri_image
);
2065 #ifdef HAVE_WAYLAND_PLATFORM
2067 /* This structure describes how a wl_buffer maps to one or more
2068 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
2069 * offsets and strides of the planes in the buffer. This table maps a
2070 * wl_drm format code to a description of the planes in the buffer
2071 * that lets us create a __DRIimage for each of the planes. */
2073 static const struct wl_drm_components_descriptor
{
2074 uint32_t dri_components
;
2077 } wl_drm_components
[] = {
2078 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
2079 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
2080 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
2081 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
2082 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
2086 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2087 EGLClientBuffer _buffer
,
2088 const EGLint
*attr_list
)
2090 struct wl_drm_buffer
*buffer
;
2091 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2092 const struct wl_drm_components_descriptor
*f
;
2093 __DRIimage
*dri_image
;
2094 _EGLImageAttribs attrs
;
2097 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
2098 (struct wl_resource
*) _buffer
);
2102 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2105 plane
= attrs
.PlaneWL
;
2106 f
= buffer
->driver_format
;
2107 if (plane
< 0 || plane
>= f
->nplanes
) {
2108 _eglError(EGL_BAD_PARAMETER
,
2109 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
2113 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
2114 if (dri_image
== NULL
&& plane
== 0)
2115 dri_image
= dri2_dpy
->image
->dupImage(buffer
->driver_buffer
, NULL
);
2116 if (dri_image
== NULL
) {
2117 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
2121 return dri2_create_image_from_dri(disp
, dri_image
);
2126 dri2_get_sync_values_chromium(_EGLDisplay
*disp
, _EGLSurface
*surf
,
2127 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
2130 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2131 return dri2_dpy
->vtbl
->get_sync_values(disp
, surf
, ust
, msc
, sbc
);
2135 * Set the error code after a call to
2136 * dri2_egl_image::dri_image::createImageFromTexture.
2139 dri2_create_image_khr_texture_error(int dri_error
)
2141 EGLint egl_error
= egl_error_from_dri_image_error(dri_error
);
2143 if (egl_error
!= EGL_SUCCESS
)
2144 _eglError(egl_error
, "dri2_create_image_khr_texture");
2148 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2150 EGLClientBuffer buffer
,
2151 const EGLint
*attr_list
)
2153 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2154 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2155 struct dri2_egl_image
*dri2_img
;
2156 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
2157 _EGLImageAttribs attrs
;
2163 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2164 return EGL_NO_IMAGE_KHR
;
2167 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2168 return EGL_NO_IMAGE_KHR
;
2171 case EGL_GL_TEXTURE_2D_KHR
:
2172 if (!disp
->Extensions
.KHR_gl_texture_2D_image
) {
2173 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2174 return EGL_NO_IMAGE_KHR
;
2177 gl_target
= GL_TEXTURE_2D
;
2179 case EGL_GL_TEXTURE_3D_KHR
:
2180 if (!disp
->Extensions
.KHR_gl_texture_3D_image
) {
2181 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2182 return EGL_NO_IMAGE_KHR
;
2185 depth
= attrs
.GLTextureZOffset
;
2186 gl_target
= GL_TEXTURE_3D
;
2188 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2189 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2190 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2191 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2192 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2193 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2194 if (!disp
->Extensions
.KHR_gl_texture_cubemap_image
) {
2195 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2196 return EGL_NO_IMAGE_KHR
;
2199 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
2200 gl_target
= GL_TEXTURE_CUBE_MAP
;
2203 unreachable("Unexpected target in dri2_create_image_khr_texture()");
2204 return EGL_NO_IMAGE_KHR
;
2207 dri2_img
= malloc(sizeof *dri2_img
);
2209 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2210 return EGL_NO_IMAGE_KHR
;
2213 _eglInitImage(&dri2_img
->base
, disp
);
2215 dri2_img
->dri_image
=
2216 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
2220 attrs
.GLTextureLevel
,
2223 dri2_create_image_khr_texture_error(error
);
2225 if (!dri2_img
->dri_image
) {
2227 return EGL_NO_IMAGE_KHR
;
2229 return &dri2_img
->base
;
2233 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
2234 EGLint attribute
, EGLint
*value
)
2236 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2237 if (!dri2_dpy
->vtbl
->query_surface
)
2238 return _eglQuerySurface(drv
, disp
, surf
, attribute
, value
);
2239 return dri2_dpy
->vtbl
->query_surface(drv
, disp
, surf
, attribute
, value
);
2242 static struct wl_buffer
*
2243 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2246 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2247 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, disp
, img
);
2252 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2253 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2255 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2256 EGLint format
, name
, pitch
;
2257 _EGLImageAttribs attrs
;
2258 __DRIimage
*dri_image
;
2260 name
= (EGLint
) (uintptr_t) buffer
;
2262 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2265 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
2266 attrs
.DRMBufferStrideMESA
<= 0) {
2267 _eglError(EGL_BAD_PARAMETER
,
2268 "bad width, height or stride");
2272 switch (attrs
.DRMBufferFormatMESA
) {
2273 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2274 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2275 pitch
= attrs
.DRMBufferStrideMESA
;
2278 _eglError(EGL_BAD_PARAMETER
,
2279 "dri2_create_image_khr: unsupported pixmap depth");
2284 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
2292 return dri2_create_image_from_dri(disp
, dri_image
);
2296 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
2301 * "Required attributes and their values are as follows:
2303 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
2305 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
2306 * by drm_fourcc.h and used as the pixel_format parameter of the
2307 * drm_mode_fb_cmd2 ioctl."
2311 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2312 * incomplete, EGL_BAD_PARAMETER is generated."
2314 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
2315 !attrs
->DMABufFourCC
.IsPresent
)
2316 return _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
2321 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
2322 * specified for a plane's pitch or offset isn't supported by EGL,
2323 * EGL_BAD_ACCESS is generated."
2325 for (unsigned i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
2326 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
2327 attrs
->DMABufPlanePitches
[i
].Value
<= 0)
2328 return _eglError(EGL_BAD_ACCESS
, "invalid pitch");
2332 * If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2333 * attribute values may be given.
2335 * This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2336 * EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2338 for (unsigned i
= 0; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2339 if (attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
!=
2340 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
)
2341 return _eglError(EGL_BAD_PARAMETER
, "modifier attribute lo or hi missing");
2344 /* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2345 * mandate it, we only accept the same modifier across all planes. */
2346 for (unsigned i
= 1; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2347 if (attrs
->DMABufPlaneFds
[i
].IsPresent
) {
2348 if ((attrs
->DMABufPlaneModifiersLo
[0].IsPresent
!=
2349 attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
) ||
2350 (attrs
->DMABufPlaneModifiersLo
[0].Value
!=
2351 attrs
->DMABufPlaneModifiersLo
[i
].Value
) ||
2352 (attrs
->DMABufPlaneModifiersHi
[0].Value
!=
2353 attrs
->DMABufPlaneModifiersHi
[i
].Value
))
2354 return _eglError(EGL_BAD_PARAMETER
, "modifier attributes not equal");
2361 /* Returns the total number of planes for the format or zero if it isn't a
2362 * valid fourcc format.
2365 dri2_num_fourcc_format_planes(EGLint format
)
2369 case DRM_FORMAT_RG88
:
2370 case DRM_FORMAT_GR88
:
2371 case DRM_FORMAT_R16
:
2372 case DRM_FORMAT_GR1616
:
2373 case DRM_FORMAT_RGB332
:
2374 case DRM_FORMAT_BGR233
:
2375 case DRM_FORMAT_XRGB4444
:
2376 case DRM_FORMAT_XBGR4444
:
2377 case DRM_FORMAT_RGBX4444
:
2378 case DRM_FORMAT_BGRX4444
:
2379 case DRM_FORMAT_ARGB4444
:
2380 case DRM_FORMAT_ABGR4444
:
2381 case DRM_FORMAT_RGBA4444
:
2382 case DRM_FORMAT_BGRA4444
:
2383 case DRM_FORMAT_XRGB1555
:
2384 case DRM_FORMAT_XBGR1555
:
2385 case DRM_FORMAT_RGBX5551
:
2386 case DRM_FORMAT_BGRX5551
:
2387 case DRM_FORMAT_ARGB1555
:
2388 case DRM_FORMAT_ABGR1555
:
2389 case DRM_FORMAT_RGBA5551
:
2390 case DRM_FORMAT_BGRA5551
:
2391 case DRM_FORMAT_RGB565
:
2392 case DRM_FORMAT_BGR565
:
2393 case DRM_FORMAT_RGB888
:
2394 case DRM_FORMAT_BGR888
:
2395 case DRM_FORMAT_XRGB8888
:
2396 case DRM_FORMAT_XBGR8888
:
2397 case DRM_FORMAT_RGBX8888
:
2398 case DRM_FORMAT_BGRX8888
:
2399 case DRM_FORMAT_ARGB8888
:
2400 case DRM_FORMAT_ABGR8888
:
2401 case DRM_FORMAT_RGBA8888
:
2402 case DRM_FORMAT_BGRA8888
:
2403 case DRM_FORMAT_XRGB2101010
:
2404 case DRM_FORMAT_XBGR2101010
:
2405 case DRM_FORMAT_RGBX1010102
:
2406 case DRM_FORMAT_BGRX1010102
:
2407 case DRM_FORMAT_ARGB2101010
:
2408 case DRM_FORMAT_ABGR2101010
:
2409 case DRM_FORMAT_RGBA1010102
:
2410 case DRM_FORMAT_BGRA1010102
:
2411 case DRM_FORMAT_YUYV
:
2412 case DRM_FORMAT_YVYU
:
2413 case DRM_FORMAT_UYVY
:
2414 case DRM_FORMAT_VYUY
:
2415 case DRM_FORMAT_AYUV
:
2416 case DRM_FORMAT_XYUV8888
:
2419 case DRM_FORMAT_NV12
:
2420 case DRM_FORMAT_NV21
:
2421 case DRM_FORMAT_NV16
:
2422 case DRM_FORMAT_NV61
:
2423 case DRM_FORMAT_P010
:
2424 case DRM_FORMAT_P012
:
2425 case DRM_FORMAT_P016
:
2428 case DRM_FORMAT_YUV410
:
2429 case DRM_FORMAT_YVU410
:
2430 case DRM_FORMAT_YUV411
:
2431 case DRM_FORMAT_YVU411
:
2432 case DRM_FORMAT_YUV420
:
2433 case DRM_FORMAT_YVU420
:
2434 case DRM_FORMAT_YUV422
:
2435 case DRM_FORMAT_YVU422
:
2436 case DRM_FORMAT_YUV444
:
2437 case DRM_FORMAT_YVU444
:
2445 /* Returns the total number of file descriptors. Zero indicates an error. */
2447 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
2449 unsigned plane_n
= dri2_num_fourcc_format_planes(attrs
->DMABufFourCC
.Value
);
2451 _eglError(EGL_BAD_MATCH
, "unknown drm fourcc format");
2455 for (unsigned i
= plane_n
; i
< DMA_BUF_MAX_PLANES
; i
++) {
2457 * The modifiers extension spec says:
2459 * "Modifiers may modify any attribute of a buffer import, including
2460 * but not limited to adding extra planes to a format which
2461 * otherwise does not have those planes. As an example, a modifier
2462 * may add a plane for an external compression buffer to a
2463 * single-plane format. The exact meaning and effect of any
2464 * modifier is canonically defined by drm_fourcc.h, not as part of
2467 if (attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
&&
2468 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
) {
2476 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2477 * incomplete, EGL_BAD_PARAMETER is generated."
2479 for (unsigned i
= 0; i
< plane_n
; ++i
) {
2480 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2481 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2482 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2483 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2489 * The spec also says:
2491 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2492 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2493 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2494 * or EGL_DMA_BUF_PLANE3_* attributes are specified."
2496 for (unsigned i
= plane_n
; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2497 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2498 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2499 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2500 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2509 dri2_query_dma_buf_formats(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2510 EGLint max
, EGLint
*formats
, EGLint
*count
)
2512 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2513 if (max
< 0 || (max
> 0 && formats
== NULL
))
2514 return _eglError(EGL_BAD_PARAMETER
, "invalid value for max count of formats");
2516 if (dri2_dpy
->image
->base
.version
< 15 ||
2517 dri2_dpy
->image
->queryDmaBufFormats
== NULL
)
2520 if (!dri2_dpy
->image
->queryDmaBufFormats(dri2_dpy
->dri_screen
, max
,
2525 /* Assert that all of the formats returned are actually fourcc formats.
2526 * Some day, if we want the internal interface function to be able to
2527 * return the fake fourcc formats defined in dri_interface.h, we'll have
2528 * to do something more clever here to pair the list down to just real
2529 * fourcc formats so that we don't leak the fake internal ones.
2531 for (int i
= 0; i
< *count
; i
++) {
2532 assert(dri2_num_fourcc_format_planes(formats
[i
]) > 0);
2540 dri2_query_dma_buf_modifiers(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint format
,
2541 EGLint max
, EGLuint64KHR
*modifiers
,
2542 EGLBoolean
*external_only
, EGLint
*count
)
2544 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2546 if (dri2_num_fourcc_format_planes(format
) == 0)
2547 return _eglError(EGL_BAD_PARAMETER
, "invalid fourcc format");
2550 return _eglError(EGL_BAD_PARAMETER
, "invalid value for max count of formats");
2552 if (max
> 0 && modifiers
== NULL
)
2553 return _eglError(EGL_BAD_PARAMETER
, "invalid modifiers array");
2555 if (dri2_dpy
->image
->base
.version
< 15 ||
2556 dri2_dpy
->image
->queryDmaBufModifiers
== NULL
)
2559 if (dri2_dpy
->image
->queryDmaBufModifiers(dri2_dpy
->dri_screen
, format
,
2561 (unsigned int *) external_only
,
2563 return _eglError(EGL_BAD_PARAMETER
, "invalid format");
2571 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2572 * EGL will take a reference to the dma_buf(s) which it will release at any
2573 * time while the EGLDisplay is initialized. It is the responsibility of the
2574 * application to close the dma_buf file descriptors."
2576 * Therefore we must never close or otherwise modify the file descriptors.
2579 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2580 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2582 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2584 _EGLImageAttribs attrs
;
2585 __DRIimage
*dri_image
;
2587 int fds
[DMA_BUF_MAX_PLANES
];
2588 int pitches
[DMA_BUF_MAX_PLANES
];
2589 int offsets
[DMA_BUF_MAX_PLANES
];
2591 bool has_modifier
= false;
2597 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2598 * error EGL_BAD_PARAMETER is generated."
2600 if (buffer
!= NULL
) {
2601 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2605 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2608 if (!dri2_check_dma_buf_attribs(&attrs
))
2611 num_fds
= dri2_check_dma_buf_format(&attrs
);
2615 for (unsigned i
= 0; i
< num_fds
; ++i
) {
2616 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2617 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2618 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2621 /* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2622 * will be present in attrs.DMABufPlaneModifiersLo[0] and
2623 * attrs.DMABufPlaneModifiersHi[0] */
2624 if (attrs
.DMABufPlaneModifiersLo
[0].IsPresent
) {
2625 modifier
= combine_u32_into_u64(attrs
.DMABufPlaneModifiersHi
[0].Value
,
2626 attrs
.DMABufPlaneModifiersLo
[0].Value
);
2627 has_modifier
= true;
2631 if (dri2_dpy
->image
->base
.version
< 15 ||
2632 dri2_dpy
->image
->createImageFromDmaBufs2
== NULL
) {
2633 _eglError(EGL_BAD_MATCH
, "unsupported dma_buf format modifier");
2634 return EGL_NO_IMAGE_KHR
;
2637 dri2_dpy
->image
->createImageFromDmaBufs2(dri2_dpy
->dri_screen
,
2638 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2639 modifier
, fds
, num_fds
, pitches
, offsets
,
2640 attrs
.DMABufYuvColorSpaceHint
.Value
,
2641 attrs
.DMABufSampleRangeHint
.Value
,
2642 attrs
.DMABufChromaHorizontalSiting
.Value
,
2643 attrs
.DMABufChromaVerticalSiting
.Value
,
2649 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2650 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2651 fds
, num_fds
, pitches
, offsets
,
2652 attrs
.DMABufYuvColorSpaceHint
.Value
,
2653 attrs
.DMABufSampleRangeHint
.Value
,
2654 attrs
.DMABufChromaHorizontalSiting
.Value
,
2655 attrs
.DMABufChromaVerticalSiting
.Value
,
2659 dri2_create_image_khr_texture_error(error
);
2662 return EGL_NO_IMAGE_KHR
;
2664 res
= dri2_create_image_from_dri(disp
, dri_image
);
2669 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2670 const EGLint
*attr_list
)
2672 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2673 struct dri2_egl_image
*dri2_img
;
2674 _EGLImageAttribs attrs
;
2675 unsigned int dri_use
, valid_mask
;
2681 _eglError(EGL_BAD_PARAMETER
, __func__
);
2682 return EGL_NO_IMAGE_KHR
;
2685 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2686 return EGL_NO_IMAGE_KHR
;
2688 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2689 _eglError(EGL_BAD_PARAMETER
, __func__
);
2690 return EGL_NO_IMAGE_KHR
;
2693 switch (attrs
.DRMBufferFormatMESA
) {
2694 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2695 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2698 _eglError(EGL_BAD_PARAMETER
, __func__
);
2699 return EGL_NO_IMAGE_KHR
;
2703 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2704 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2705 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2706 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2707 _eglError(EGL_BAD_PARAMETER
, __func__
);
2708 return EGL_NO_IMAGE_KHR
;
2712 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2713 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2714 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2715 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2716 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2717 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2719 dri2_img
= malloc(sizeof *dri2_img
);
2721 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2722 return EGL_NO_IMAGE_KHR
;
2725 _eglInitImage(&dri2_img
->base
, disp
);
2727 dri2_img
->dri_image
=
2728 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2729 attrs
.Width
, attrs
.Height
,
2730 format
, dri_use
, dri2_img
);
2731 if (dri2_img
->dri_image
== NULL
) {
2733 _eglError(EGL_BAD_ALLOC
, "dri2_create_drm_image_mesa");
2734 return EGL_NO_IMAGE_KHR
;
2737 return &dri2_img
->base
;
2741 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2742 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2744 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2745 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2749 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2750 __DRI_IMAGE_ATTRIB_NAME
, name
))
2751 return _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2754 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2755 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2758 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2759 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2765 * Checks if we can support EGL_MESA_image_dma_buf_export on this image.
2767 * The spec provides a boolean return for the driver to reject exporting for
2768 * basically any reason, but doesn't specify any particular error cases. For
2769 * now, we just fail if we don't have a DRM fourcc for the format.
2772 dri2_can_export_dma_buf_image(_EGLDisplay
*disp
, _EGLImage
*img
)
2774 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2775 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2778 if (!dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2779 __DRI_IMAGE_ATTRIB_FOURCC
, &fourcc
)) {
2787 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2789 EGLint
*fourcc
, EGLint
*nplanes
,
2790 EGLuint64KHR
*modifiers
)
2792 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2793 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2798 if (!dri2_can_export_dma_buf_image(disp
, img
))
2801 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2802 __DRI_IMAGE_ATTRIB_NUM_PLANES
, &num_planes
);
2804 *nplanes
= num_planes
;
2807 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2808 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2812 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
2815 query
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2816 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
2818 query
&= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2819 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
2822 modifier
= combine_u32_into_u64 (mod_hi
, mod_lo
);
2824 for (int i
= 0; i
< num_planes
; i
++)
2825 modifiers
[i
] = modifier
;
2832 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2833 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2835 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2836 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2841 if (!dri2_can_export_dma_buf_image(disp
, img
))
2844 /* EGL_MESA_image_dma_buf_export spec says:
2845 * "If the number of fds is less than the number of planes, then
2846 * subsequent fd slots should contain -1."
2849 /* Query nplanes so that we know how big the given array is. */
2850 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2851 __DRI_IMAGE_ATTRIB_NUM_PLANES
, &nplanes
);
2852 memset(fds
, -1, nplanes
* sizeof(int));
2855 /* rework later to provide multiple fds/strides/offsets */
2857 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2858 __DRI_IMAGE_ATTRIB_FD
, fds
);
2861 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2862 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2866 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2867 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2869 offsets
[0] = img_offset
;
2880 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2881 _EGLContext
*ctx
, EGLenum target
,
2882 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2887 case EGL_GL_TEXTURE_2D_KHR
:
2888 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2889 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2890 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2891 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2892 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2893 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2894 case EGL_GL_TEXTURE_3D_KHR
:
2895 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2896 case EGL_GL_RENDERBUFFER_KHR
:
2897 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2899 case EGL_DRM_BUFFER_MESA
:
2900 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2901 case EGL_LINUX_DMA_BUF_EXT
:
2902 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2904 #ifdef HAVE_WAYLAND_PLATFORM
2905 case EGL_WAYLAND_BUFFER_WL
:
2906 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2909 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2910 return EGL_NO_IMAGE_KHR
;
2915 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2917 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2918 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2922 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2928 #ifdef HAVE_WAYLAND_PLATFORM
2931 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2932 struct wl_drm_buffer
*buffer
)
2934 _EGLDisplay
*disp
= user_data
;
2935 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2937 int dri_components
= 0;
2940 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2949 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2961 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2963 buffer
->driver_format
= NULL
;
2964 for (int i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2965 if (wl_drm_components
[i
].dri_components
== dri_components
)
2966 buffer
->driver_format
= &wl_drm_components
[i
];
2968 if (buffer
->driver_format
== NULL
)
2969 dri2_dpy
->image
->destroyImage(img
);
2971 buffer
->driver_buffer
= img
;
2975 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2977 _EGLDisplay
*disp
= user_data
;
2978 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2980 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2984 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2985 struct wl_display
*wl_dpy
)
2987 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2988 const struct wayland_drm_callbacks wl_drm_callbacks
= {
2989 .authenticate
= (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
,
2990 .reference_buffer
= dri2_wl_reference_buffer
,
2991 .release_buffer
= dri2_wl_release_buffer
,
2992 .is_format_supported
= dri2_wl_is_format_supported
2999 if (dri2_dpy
->wl_server_drm
)
3002 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
3003 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
3004 dri2_dpy
->image
->base
.version
>= 7 &&
3005 dri2_dpy
->image
->createImageFromFds
!= NULL
)
3006 flags
|= WAYLAND_DRM_PRIME
;
3008 dri2_dpy
->wl_server_drm
=
3009 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
3010 &wl_drm_callbacks
, disp
, flags
);
3012 if (!dri2_dpy
->wl_server_drm
)
3015 #ifdef HAVE_DRM_PLATFORM
3016 /* We have to share the wl_drm instance with gbm, so gbm can convert
3017 * wl_buffers to gbm bos. */
3018 if (dri2_dpy
->gbm_dri
)
3019 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
3026 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3027 struct wl_display
*wl_dpy
)
3029 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3033 if (!dri2_dpy
->wl_server_drm
)
3036 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
3037 dri2_dpy
->wl_server_drm
= NULL
;
3043 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3044 struct wl_resource
*buffer_resource
,
3045 EGLint attribute
, EGLint
*value
)
3047 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3048 struct wl_drm_buffer
*buffer
;
3049 const struct wl_drm_components_descriptor
*format
;
3051 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
3055 format
= buffer
->driver_format
;
3056 switch (attribute
) {
3057 case EGL_TEXTURE_FORMAT
:
3058 *value
= format
->components
;
3061 *value
= buffer
->width
;
3064 *value
= buffer
->height
;
3073 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
3075 p_atomic_inc(&sync
->refcount
);
3079 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
3080 struct dri2_egl_sync
*dri2_sync
)
3082 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
3083 switch (dri2_sync
->base
.Type
) {
3084 case EGL_SYNC_REUSABLE_KHR
:
3085 cnd_destroy(&dri2_sync
->cond
);
3087 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3088 if (dri2_sync
->base
.SyncFd
!= EGL_NO_NATIVE_FENCE_FD_ANDROID
)
3089 close(dri2_sync
->base
.SyncFd
);
3095 if (dri2_sync
->fence
)
3096 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
3103 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3104 EGLenum type
, const EGLAttrib
*attrib_list
)
3106 _EGLContext
*ctx
= _eglGetCurrentContext();
3107 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3108 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3109 struct dri2_egl_sync
*dri2_sync
;
3111 pthread_condattr_t attr
;
3113 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
3115 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
3119 if (!_eglInitSync(&dri2_sync
->base
, disp
, type
, attrib_list
)) {
3125 case EGL_SYNC_FENCE_KHR
:
3126 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
3127 if (!dri2_sync
->fence
) {
3128 /* Why did it fail? DRI doesn't return an error code, so we emit
3129 * a generic EGL error that doesn't communicate user error.
3131 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
3137 case EGL_SYNC_CL_EVENT_KHR
:
3138 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
3139 dri2_dpy
->dri_screen
,
3140 dri2_sync
->base
.CLEvent
);
3141 /* this can only happen if the cl_event passed in is invalid. */
3142 if (!dri2_sync
->fence
) {
3143 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
3148 /* the initial status must be "signaled" if the cl_event is signaled */
3149 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
3150 dri2_sync
->fence
, 0, 0))
3151 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3154 case EGL_SYNC_REUSABLE_KHR
:
3155 /* intialize attr */
3156 ret
= pthread_condattr_init(&attr
);
3159 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3164 /* change clock attribute to CLOCK_MONOTONIC */
3165 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
3168 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3173 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
3176 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3181 /* initial status of reusable sync must be "unsignaled" */
3182 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
3185 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3186 if (dri2_dpy
->fence
->create_fence_fd
) {
3187 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence_fd(
3188 dri2_ctx
->dri_context
,
3189 dri2_sync
->base
.SyncFd
);
3191 if (!dri2_sync
->fence
) {
3192 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
3199 p_atomic_set(&dri2_sync
->refcount
, 1);
3200 return &dri2_sync
->base
;
3204 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3206 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3207 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3208 EGLint ret
= EGL_TRUE
;
3211 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
3212 * then unlock all threads possibly blocked by the reusable sync before
3215 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
3216 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
3217 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3218 /* unblock all threads currently blocked by sync */
3219 err
= cnd_broadcast(&dri2_sync
->cond
);
3222 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
3227 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
3233 dri2_dup_native_fence_fd(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3235 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3236 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3238 assert(sync
->Type
== EGL_SYNC_NATIVE_FENCE_ANDROID
);
3240 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
3241 /* try to retrieve the actual native fence fd.. if rendering is
3242 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
3244 sync
->SyncFd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
3248 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
3249 /* if native fence fd still not created, return an error: */
3250 _eglError(EGL_BAD_PARAMETER
, "eglDupNativeFenceFDANDROID");
3251 return EGL_NO_NATIVE_FENCE_FD_ANDROID
;
3254 return dup(sync
->SyncFd
);
3258 dri2_set_blob_cache_funcs(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3259 EGLSetBlobFuncANDROID set
,
3260 EGLGetBlobFuncANDROID get
)
3262 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3263 dri2_dpy
->blob
->set_cache_funcs(dri2_dpy
->dri_screen
,
3265 disp
->BlobCacheGet
);
3269 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
,
3270 EGLint flags
, EGLTime timeout
)
3272 _EGLContext
*ctx
= _eglGetCurrentContext();
3273 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3274 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3275 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3276 unsigned wait_flags
= 0;
3278 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
3280 /* The EGL_KHR_fence_sync spec states:
3282 * "If no context is current for the bound API,
3283 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
3285 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
3286 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
3288 /* the sync object should take a reference while waiting */
3289 dri2_egl_ref_sync(dri2_sync
);
3291 switch (sync
->Type
) {
3292 case EGL_SYNC_FENCE_KHR
:
3293 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3294 case EGL_SYNC_CL_EVENT_KHR
:
3295 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
3296 dri2_sync
->fence
, wait_flags
,
3298 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3300 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
3303 case EGL_SYNC_REUSABLE_KHR
:
3304 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
3305 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
3306 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
3310 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
3311 if (timeout
== EGL_FOREVER_KHR
) {
3312 mtx_lock(&dri2_sync
->mutex
);
3313 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
3314 mtx_unlock(&dri2_sync
->mutex
);
3316 /* if reusable sync has not been yet signaled */
3317 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
3318 /* timespecs for cnd_timedwait */
3319 struct timespec current
;
3320 struct timespec expire
;
3322 /* We override the clock to monotonic when creating the condition
3324 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
3326 /* calculating when to expire */
3327 expire
.tv_nsec
= timeout
% 1000000000L;
3328 expire
.tv_sec
= timeout
/ 1000000000L;
3330 expire
.tv_nsec
+= current
.tv_nsec
;
3331 expire
.tv_sec
+= current
.tv_sec
;
3333 /* expire.nsec now is a number between 0 and 1999999998 */
3334 if (expire
.tv_nsec
> 999999999L) {
3336 expire
.tv_nsec
-= 1000000000L;
3339 mtx_lock(&dri2_sync
->mutex
);
3340 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
3341 mtx_unlock(&dri2_sync
->mutex
);
3343 if (ret
== thrd_busy
) {
3344 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
3345 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
3347 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
3355 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
3361 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
,
3364 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3367 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
)
3368 return _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
3370 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
)
3371 return _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
3373 dri2_sync
->base
.SyncStatus
= mode
;
3375 if (mode
== EGL_SIGNALED_KHR
) {
3376 ret
= cnd_broadcast(&dri2_sync
->cond
);
3378 /* fail to broadcast */
3380 return _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
3387 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3389 _EGLContext
*ctx
= _eglGetCurrentContext();
3390 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3391 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3392 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3394 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
3395 dri2_sync
->fence
, 0);
3400 dri2_interop_query_device_info(_EGLDisplay
*disp
, _EGLContext
*ctx
,
3401 struct mesa_glinterop_device_info
*out
)
3403 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3404 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3406 if (!dri2_dpy
->interop
)
3407 return MESA_GLINTEROP_UNSUPPORTED
;
3409 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
3413 dri2_interop_export_object(_EGLDisplay
*disp
, _EGLContext
*ctx
,
3414 struct mesa_glinterop_export_in
*in
,
3415 struct mesa_glinterop_export_out
*out
)
3417 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3418 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3420 if (!dri2_dpy
->interop
)
3421 return MESA_GLINTEROP_UNSUPPORTED
;
3423 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
3427 * This is the main entrypoint into the driver, called by libEGL.
3428 * Gets an _EGLDriver object and init its dispatch table.
3431 _eglInitDriver(_EGLDriver
*dri2_drv
)
3433 dri2_drv
->API
.Initialize
= dri2_initialize
;
3434 dri2_drv
->API
.Terminate
= dri2_terminate
;
3435 dri2_drv
->API
.CreateContext
= dri2_create_context
;
3436 dri2_drv
->API
.DestroyContext
= dri2_destroy_context
;
3437 dri2_drv
->API
.MakeCurrent
= dri2_make_current
;
3438 dri2_drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
3439 dri2_drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
3440 dri2_drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
3441 dri2_drv
->API
.DestroySurface
= dri2_destroy_surface
;
3442 dri2_drv
->API
.GetProcAddress
= dri2_get_proc_address
;
3443 dri2_drv
->API
.WaitClient
= dri2_wait_client
;
3444 dri2_drv
->API
.WaitNative
= dri2_wait_native
;
3445 dri2_drv
->API
.BindTexImage
= dri2_bind_tex_image
;
3446 dri2_drv
->API
.ReleaseTexImage
= dri2_release_tex_image
;
3447 dri2_drv
->API
.SwapInterval
= dri2_swap_interval
;
3448 dri2_drv
->API
.SwapBuffers
= dri2_swap_buffers
;
3449 dri2_drv
->API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
3450 dri2_drv
->API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
3451 dri2_drv
->API
.SetDamageRegion
= dri2_set_damage_region
;
3452 dri2_drv
->API
.PostSubBufferNV
= dri2_post_sub_buffer
;
3453 dri2_drv
->API
.CopyBuffers
= dri2_copy_buffers
,
3454 dri2_drv
->API
.QueryBufferAge
= dri2_query_buffer_age
;
3455 dri2_drv
->API
.CreateImageKHR
= dri2_create_image
;
3456 dri2_drv
->API
.DestroyImageKHR
= dri2_destroy_image_khr
;
3457 dri2_drv
->API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
3458 dri2_drv
->API
.QuerySurface
= dri2_query_surface
;
3459 dri2_drv
->API
.QueryDriverName
= dri2_query_driver_name
;
3460 dri2_drv
->API
.QueryDriverConfig
= dri2_query_driver_config
;
3462 dri2_drv
->API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
3463 dri2_drv
->API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
3464 dri2_drv
->API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
3465 dri2_drv
->API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
3466 dri2_drv
->API
.QueryDmaBufFormatsEXT
= dri2_query_dma_buf_formats
;
3467 dri2_drv
->API
.QueryDmaBufModifiersEXT
= dri2_query_dma_buf_modifiers
;
3469 #ifdef HAVE_WAYLAND_PLATFORM
3470 dri2_drv
->API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
3471 dri2_drv
->API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
3472 dri2_drv
->API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
3474 dri2_drv
->API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
3475 dri2_drv
->API
.CreateSyncKHR
= dri2_create_sync
;
3476 dri2_drv
->API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
3477 dri2_drv
->API
.SignalSyncKHR
= dri2_signal_sync
;
3478 dri2_drv
->API
.WaitSyncKHR
= dri2_server_wait_sync
;
3479 dri2_drv
->API
.DestroySyncKHR
= dri2_destroy_sync
;
3480 dri2_drv
->API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
3481 dri2_drv
->API
.GLInteropExportObject
= dri2_interop_export_object
;
3482 dri2_drv
->API
.DupNativeFenceFDANDROID
= dri2_dup_native_fence_fd
;
3483 dri2_drv
->API
.SetBlobCacheFuncsANDROID
= dri2_set_blob_cache_funcs
;