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 MAYBE_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 }
152 static const EGLint dri2_to_egl_attribute_map
[__DRI_ATTRIB_MAX
] = {
153 [__DRI_ATTRIB_BUFFER_SIZE
] = EGL_BUFFER_SIZE
,
154 [__DRI_ATTRIB_LEVEL
] = EGL_LEVEL
,
155 [__DRI_ATTRIB_RED_SIZE
] = EGL_RED_SIZE
,
156 [__DRI_ATTRIB_GREEN_SIZE
] = EGL_GREEN_SIZE
,
157 [__DRI_ATTRIB_BLUE_SIZE
] = EGL_BLUE_SIZE
,
158 [__DRI_ATTRIB_LUMINANCE_SIZE
] = EGL_LUMINANCE_SIZE
,
159 [__DRI_ATTRIB_ALPHA_SIZE
] = EGL_ALPHA_SIZE
,
160 [__DRI_ATTRIB_DEPTH_SIZE
] = EGL_DEPTH_SIZE
,
161 [__DRI_ATTRIB_STENCIL_SIZE
] = EGL_STENCIL_SIZE
,
162 [__DRI_ATTRIB_SAMPLE_BUFFERS
] = EGL_SAMPLE_BUFFERS
,
163 [__DRI_ATTRIB_SAMPLES
] = EGL_SAMPLES
,
164 [__DRI_ATTRIB_MAX_PBUFFER_WIDTH
] = EGL_MAX_PBUFFER_WIDTH
,
165 [__DRI_ATTRIB_MAX_PBUFFER_HEIGHT
] = EGL_MAX_PBUFFER_HEIGHT
,
166 [__DRI_ATTRIB_MAX_PBUFFER_PIXELS
] = EGL_MAX_PBUFFER_PIXELS
,
167 [__DRI_ATTRIB_MAX_SWAP_INTERVAL
] = EGL_MAX_SWAP_INTERVAL
,
168 [__DRI_ATTRIB_MIN_SWAP_INTERVAL
] = EGL_MIN_SWAP_INTERVAL
,
169 [__DRI_ATTRIB_YINVERTED
] = EGL_Y_INVERTED_NOK
,
173 dri2_get_dri_config(struct dri2_egl_config
*conf
, EGLint surface_type
,
176 const bool double_buffer
= surface_type
== EGL_WINDOW_BIT
;
177 const bool srgb
= colorspace
== EGL_GL_COLORSPACE_SRGB_KHR
;
179 return conf
->dri_config
[double_buffer
][srgb
];
183 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
185 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
188 if (!_eglMatchConfig(conf
, criteria
))
194 struct dri2_egl_config
*
195 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
196 EGLint surface_type
, const EGLint
*attr_list
,
197 const unsigned int *rgba_masks
)
199 struct dri2_egl_config
*conf
;
200 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
202 unsigned int attrib
, value
, double_buffer
;
204 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
205 unsigned int dri_masks
[4] = { 0, 0, 0, 0 };
206 _EGLConfig
*matching_config
;
207 EGLint num_configs
= 0;
210 _eglInitConfig(&base
, disp
, id
);
213 bind_to_texture_rgb
= 0;
214 bind_to_texture_rgba
= 0;
216 for (int i
= 0; i
< __DRI_ATTRIB_MAX
; ++i
) {
217 if (!dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
, &attrib
, &value
))
221 case __DRI_ATTRIB_RENDER_TYPE
:
222 if (value
& __DRI_ATTRIB_RGBA_BIT
)
223 value
= EGL_RGB_BUFFER
;
224 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
225 value
= EGL_LUMINANCE_BUFFER
;
228 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
231 case __DRI_ATTRIB_CONFIG_CAVEAT
:
232 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
233 value
= EGL_NON_CONFORMANT_CONFIG
;
234 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
235 value
= EGL_SLOW_CONFIG
;
238 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
241 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
242 bind_to_texture_rgb
= value
;
245 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
246 bind_to_texture_rgba
= value
;
249 case __DRI_ATTRIB_DOUBLE_BUFFER
:
250 double_buffer
= value
;
253 case __DRI_ATTRIB_RED_MASK
:
254 dri_masks
[0] = value
;
257 case __DRI_ATTRIB_GREEN_MASK
:
258 dri_masks
[1] = value
;
261 case __DRI_ATTRIB_BLUE_MASK
:
262 dri_masks
[2] = value
;
265 case __DRI_ATTRIB_ALPHA_MASK
:
266 dri_masks
[3] = value
;
269 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
270 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
271 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
272 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
273 /* Don't expose visuals with the accumulation buffer. */
278 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
280 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
284 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH
:
285 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_WIDTH
,
286 _EGL_MAX_PBUFFER_WIDTH
);
288 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT
:
289 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_HEIGHT
,
290 _EGL_MAX_PBUFFER_HEIGHT
);
292 case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER
:
293 if (disp
->Extensions
.KHR_mutable_render_buffer
)
294 surface_type
|= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR
;
297 key
= dri2_to_egl_attribute_map
[attrib
];
299 _eglSetConfigKey(&base
, key
, value
);
305 for (int i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
306 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
308 if (rgba_masks
&& memcmp(rgba_masks
, dri_masks
, sizeof(dri_masks
)))
311 base
.NativeRenderable
= EGL_TRUE
;
313 base
.SurfaceType
= surface_type
;
314 if (surface_type
& (EGL_PBUFFER_BIT
|
315 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
316 base
.BindToTextureRGB
= bind_to_texture_rgb
;
317 if (base
.AlphaSize
> 0)
318 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
321 base
.RenderableType
= disp
->ClientAPIs
;
322 base
.Conformant
= disp
->ClientAPIs
;
324 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
325 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
327 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
328 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
332 config_id
= base
.ConfigID
;
333 base
.ConfigID
= EGL_DONT_CARE
;
334 base
.SurfaceType
= EGL_DONT_CARE
;
335 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
336 (_EGLArrayForEach
) dri2_match_config
, &base
);
338 if (num_configs
== 1) {
339 conf
= (struct dri2_egl_config
*) matching_config
;
341 if (!conf
->dri_config
[double_buffer
][srgb
])
342 conf
->dri_config
[double_buffer
][srgb
] = dri_config
;
344 /* a similar config type is already added (unlikely) => discard */
347 else if (num_configs
== 0) {
348 conf
= calloc(1, sizeof *conf
);
352 conf
->dri_config
[double_buffer
][srgb
] = dri_config
;
354 memcpy(&conf
->base
, &base
, sizeof base
);
355 conf
->base
.SurfaceType
= 0;
356 conf
->base
.ConfigID
= config_id
;
358 _eglLinkConfig(&conf
->base
);
361 unreachable("duplicates should not be possible");
366 surface_type
&= ~EGL_PIXMAP_BIT
;
369 /* No support for pbuffer + MSAA for now.
371 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
372 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
375 surface_type
&= ~EGL_PBUFFER_BIT
;
378 conf
->base
.SurfaceType
|= surface_type
;
384 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
386 _EGLDisplay
*disp
= data
;
387 struct dri2_egl_image
*dri2_img
;
392 img
= _eglLookupImage(image
, disp
);
394 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
398 dri2_img
= dri2_egl_image(image
);
400 return dri2_img
->dri_image
;
403 const __DRIimageLookupExtension image_lookup_extension
= {
404 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
406 .lookupEGLImage
= dri2_lookup_egl_image
409 struct dri2_extension_match
{
415 static const struct dri2_extension_match dri3_driver_extensions
[] = {
416 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
417 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
421 static const struct dri2_extension_match dri2_driver_extensions
[] = {
422 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
423 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
427 static const struct dri2_extension_match dri2_core_extensions
[] = {
428 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
429 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
430 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
434 static const struct dri2_extension_match swrast_driver_extensions
[] = {
435 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
436 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
440 static const struct dri2_extension_match swrast_core_extensions
[] = {
441 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
445 static const struct dri2_extension_match optional_driver_extensions
[] = {
446 { __DRI_CONFIG_OPTIONS
, 1, offsetof(struct dri2_egl_display
, configOptions
) },
450 static const struct dri2_extension_match optional_core_extensions
[] = {
451 { __DRI2_ROBUSTNESS
, 1, offsetof(struct dri2_egl_display
, robustness
) },
452 { __DRI2_NO_ERROR
, 1, offsetof(struct dri2_egl_display
, no_error
) },
453 { __DRI2_CONFIG_QUERY
, 1, offsetof(struct dri2_egl_display
, config
) },
454 { __DRI2_FENCE
, 1, offsetof(struct dri2_egl_display
, fence
) },
455 { __DRI2_RENDERER_QUERY
, 1, offsetof(struct dri2_egl_display
, rendererQuery
) },
456 { __DRI2_INTEROP
, 1, offsetof(struct dri2_egl_display
, interop
) },
457 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
458 { __DRI2_FLUSH_CONTROL
, 1, offsetof(struct dri2_egl_display
, flush_control
) },
459 { __DRI2_BLOB
, 1, offsetof(struct dri2_egl_display
, blob
) },
460 { __DRI_MUTABLE_RENDER_BUFFER_DRIVER
, 1, offsetof(struct dri2_egl_display
, mutable_render_buffer
) },
465 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
466 const struct dri2_extension_match
*matches
,
467 const __DRIextension
**extensions
,
473 for (int i
= 0; extensions
[i
]; i
++) {
474 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
475 for (int j
= 0; matches
[j
].name
; j
++) {
476 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
477 extensions
[i
]->version
>= matches
[j
].version
) {
478 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
479 *(const __DRIextension
**) field
= extensions
[i
];
480 _eglLog(_EGL_INFO
, "found extension %s version %d",
481 extensions
[i
]->name
, extensions
[i
]->version
);
487 for (int j
= 0; matches
[j
].name
; j
++) {
488 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
489 if (*(const __DRIextension
**) field
== NULL
) {
491 _eglLog(_EGL_DEBUG
, "did not find optional extension %s version %d",
492 matches
[j
].name
, matches
[j
].version
);
494 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
495 matches
[j
].name
, matches
[j
].version
);
504 static const __DRIextension
**
505 dri2_open_driver(_EGLDisplay
*disp
)
507 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
508 static const char *search_path_vars
[] = {
509 "LIBGL_DRIVERS_PATH",
513 return loader_open_driver(dri2_dpy
->driver_name
,
519 dri2_load_driver_common(_EGLDisplay
*disp
,
520 const struct dri2_extension_match
*driver_extensions
)
522 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
523 const __DRIextension
**extensions
;
525 extensions
= dri2_open_driver(disp
);
529 if (!dri2_bind_extensions(dri2_dpy
, driver_extensions
, extensions
, false)) {
530 dlclose(dri2_dpy
->driver
);
533 dri2_dpy
->driver_extensions
= extensions
;
535 dri2_bind_extensions(dri2_dpy
, optional_driver_extensions
, extensions
, true);
541 dri2_load_driver(_EGLDisplay
*disp
)
543 return dri2_load_driver_common(disp
, dri2_driver_extensions
);
547 dri2_load_driver_dri3(_EGLDisplay
*disp
)
549 return dri2_load_driver_common(disp
, dri3_driver_extensions
);
553 dri2_load_driver_swrast(_EGLDisplay
*disp
)
555 return dri2_load_driver_common(disp
, swrast_driver_extensions
);
559 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
561 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
562 unsigned int value
= 0;
564 if (!rendererQuery
||
565 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
572 dri2_query_driver_name(_EGLDisplay
*disp
)
574 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
575 return dri2_dpy
->driver_name
;
579 dri2_query_driver_config(_EGLDisplay
*disp
)
581 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
582 const __DRIconfigOptionsExtension
*ext
= dri2_dpy
->configOptions
;
584 if (ext
->base
.version
>= 2)
585 return ext
->getXml(dri2_dpy
->driver_name
);
587 return strdup(ext
->xml
);
592 dri2_setup_screen(_EGLDisplay
*disp
)
594 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
595 unsigned int api_mask
;
598 * EGL 1.5 specification defines the default value to 1. Moreover,
599 * eglSwapInterval() is required to clamp requested value to the supported
600 * range. Since the default value is implicitly assumed to be supported,
601 * use it as both minimum and maximum for the platforms that do not allow
602 * changing the interval. Platforms, which allow it (e.g. x11, wayland)
603 * override these values already.
605 dri2_dpy
->min_swap_interval
= 1;
606 dri2_dpy
->max_swap_interval
= 1;
607 dri2_dpy
->default_swap_interval
= 1;
609 if (dri2_dpy
->image_driver
) {
610 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
611 } else if (dri2_dpy
->dri2
) {
612 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
614 assert(dri2_dpy
->swrast
);
615 api_mask
= 1 << __DRI_API_OPENGL
|
616 1 << __DRI_API_GLES
|
617 1 << __DRI_API_GLES2
|
618 1 << __DRI_API_GLES3
;
621 disp
->ClientAPIs
= 0;
622 if ((api_mask
& (1 <<__DRI_API_OPENGL
)) && _eglIsApiValid(EGL_OPENGL_API
))
623 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
624 if ((api_mask
& (1 << __DRI_API_GLES
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
625 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
626 if ((api_mask
& (1 << __DRI_API_GLES2
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
627 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
628 if ((api_mask
& (1 << __DRI_API_GLES3
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
629 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
631 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
632 disp
->Extensions
.KHR_no_config_context
= EGL_TRUE
;
633 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
635 if (dri2_dpy
->configOptions
) {
636 disp
->Extensions
.MESA_query_driver
= EGL_TRUE
;
639 /* Report back to EGL the bitmask of priorities supported */
640 disp
->Extensions
.IMG_context_priority
=
641 dri2_renderer_query_integer(dri2_dpy
,
642 __DRI2_RENDERER_HAS_CONTEXT_PRIORITY
);
644 disp
->Extensions
.EXT_pixel_format_float
= EGL_TRUE
;
646 if (dri2_renderer_query_integer(dri2_dpy
,
647 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
648 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
650 if (dri2_dpy
->image_driver
||
651 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
652 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
653 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
655 if (dri2_dpy
->robustness
)
656 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
659 if (dri2_dpy
->no_error
)
660 disp
->Extensions
.KHR_create_context_no_error
= EGL_TRUE
;
662 if (dri2_dpy
->fence
) {
663 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
664 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
665 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
666 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
667 if (dri2_dpy
->fence
->base
.version
>= 2 &&
668 dri2_dpy
->fence
->get_capabilities
) {
669 unsigned capabilities
=
670 dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
);
671 disp
->Extensions
.ANDROID_native_fence_sync
=
672 (capabilities
& __DRI_FENCE_CAP_NATIVE_FD
) != 0;
677 disp
->Extensions
.ANDROID_blob_cache
= EGL_TRUE
;
679 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
681 if (dri2_dpy
->image
) {
682 if (dri2_dpy
->image
->base
.version
>= 10 &&
683 dri2_dpy
->image
->getCapabilities
!= NULL
) {
686 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
687 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
689 if (dri2_dpy
->image
->base
.version
>= 11)
690 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
692 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
693 if (dri2_dpy
->image
->base
.version
>= 11)
694 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
697 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
698 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
699 if (dri2_dpy
->image
->base
.version
>= 5 &&
700 dri2_dpy
->image
->createImageFromTexture
) {
701 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
702 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
704 if (dri2_renderer_query_integer(dri2_dpy
,
705 __DRI2_RENDERER_HAS_TEXTURE_3D
))
706 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
709 if (dri2_dpy
->image
->base
.version
>= 8 &&
710 dri2_dpy
->image
->createImageFromDmaBufs
) {
711 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
713 if (dri2_dpy
->image
->base
.version
>= 15 &&
714 dri2_dpy
->image
->createImageFromDmaBufs2
&&
715 dri2_dpy
->image
->queryDmaBufFormats
&&
716 dri2_dpy
->image
->queryDmaBufModifiers
) {
717 disp
->Extensions
.EXT_image_dma_buf_import_modifiers
= EGL_TRUE
;
722 if (dri2_dpy
->flush_control
)
723 disp
->Extensions
.KHR_context_flush_control
= EGL_TRUE
;
727 dri2_setup_swap_interval(_EGLDisplay
*disp
, int max_swap_interval
)
729 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
730 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
732 /* Allow driconf to override applications.*/
733 if (dri2_dpy
->config
)
734 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
735 "vblank_mode", &vblank_mode
);
736 switch (vblank_mode
) {
737 case DRI_CONF_VBLANK_NEVER
:
738 dri2_dpy
->min_swap_interval
= 0;
739 dri2_dpy
->max_swap_interval
= 0;
740 dri2_dpy
->default_swap_interval
= 0;
742 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
743 dri2_dpy
->min_swap_interval
= 1;
744 dri2_dpy
->max_swap_interval
= max_swap_interval
;
745 dri2_dpy
->default_swap_interval
= 1;
747 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
748 dri2_dpy
->min_swap_interval
= 0;
749 dri2_dpy
->max_swap_interval
= max_swap_interval
;
750 dri2_dpy
->default_swap_interval
= 0;
753 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
754 dri2_dpy
->min_swap_interval
= 0;
755 dri2_dpy
->max_swap_interval
= max_swap_interval
;
756 dri2_dpy
->default_swap_interval
= 1;
761 /* All platforms but DRM call this function to create the screen and populate
762 * the driver_configs. DRM inherits that information from its display - GBM.
765 dri2_create_screen(_EGLDisplay
*disp
)
767 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
769 if (dri2_dpy
->image_driver
) {
770 dri2_dpy
->dri_screen
=
771 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
772 dri2_dpy
->loader_extensions
,
773 dri2_dpy
->driver_extensions
,
774 &dri2_dpy
->driver_configs
,
776 } else if (dri2_dpy
->dri2
) {
777 if (dri2_dpy
->dri2
->base
.version
>= 4) {
778 dri2_dpy
->dri_screen
=
779 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
780 dri2_dpy
->loader_extensions
,
781 dri2_dpy
->driver_extensions
,
782 &dri2_dpy
->driver_configs
, disp
);
784 dri2_dpy
->dri_screen
=
785 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
786 dri2_dpy
->loader_extensions
,
787 &dri2_dpy
->driver_configs
, disp
);
790 assert(dri2_dpy
->swrast
);
791 if (dri2_dpy
->swrast
->base
.version
>= 4) {
792 dri2_dpy
->dri_screen
=
793 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->loader_extensions
,
794 dri2_dpy
->driver_extensions
,
795 &dri2_dpy
->driver_configs
, disp
);
797 dri2_dpy
->dri_screen
=
798 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->loader_extensions
,
799 &dri2_dpy
->driver_configs
, disp
);
803 if (dri2_dpy
->dri_screen
== NULL
) {
804 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
808 dri2_dpy
->own_dri_screen
= true;
813 dri2_setup_extensions(_EGLDisplay
*disp
)
815 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
816 const struct dri2_extension_match
*mandatory_core_extensions
;
817 const __DRIextension
**extensions
;
819 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
821 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
)
822 mandatory_core_extensions
= dri2_core_extensions
;
824 mandatory_core_extensions
= swrast_core_extensions
;
826 if (!dri2_bind_extensions(dri2_dpy
, mandatory_core_extensions
, extensions
, false))
829 #ifdef HAVE_DRI3_MODIFIERS
830 dri2_dpy
->multibuffers_available
=
831 (dri2_dpy
->dri3_major_version
> 1 || (dri2_dpy
->dri3_major_version
== 1 &&
832 dri2_dpy
->dri3_minor_version
>= 2)) &&
833 (dri2_dpy
->present_major_version
> 1 || (dri2_dpy
->present_major_version
== 1 &&
834 dri2_dpy
->present_minor_version
>= 2)) &&
835 (dri2_dpy
->image
&& dri2_dpy
->image
->base
.version
>= 15);
838 dri2_bind_extensions(dri2_dpy
, optional_core_extensions
, extensions
, true);
843 * Called via eglInitialize(), GLX_drv->API.Initialize().
845 * This must be guaranteed to be called exactly once, even if eglInitialize is
846 * called many times (without a eglTerminate in between).
849 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
851 EGLBoolean ret
= EGL_FALSE
;
852 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
854 /* In the case where the application calls eglMakeCurrent(context1),
855 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
856 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
857 * initialized, as we need it to be able to free context1 correctly.
859 * It would probably be safest to forcibly release the display with
860 * dri2_display_release, to make sure the display is reinitialized correctly.
861 * However, the EGL spec states that we need to keep a reference to the
862 * current context (so we cannot call dri2_make_current(NULL)), and therefore
863 * we would leak context1 as we would be missing the old display connection
864 * to free it up correctly.
867 dri2_dpy
->ref_count
++;
871 loader_set_logger(_eglLog
);
873 switch (disp
->Platform
) {
874 case _EGL_PLATFORM_SURFACELESS
:
875 ret
= dri2_initialize_surfaceless(drv
, disp
);
877 case _EGL_PLATFORM_DEVICE
:
878 ret
= dri2_initialize_device(drv
, disp
);
880 case _EGL_PLATFORM_X11
:
881 ret
= dri2_initialize_x11(drv
, disp
);
883 case _EGL_PLATFORM_DRM
:
884 ret
= dri2_initialize_drm(drv
, disp
);
886 case _EGL_PLATFORM_WAYLAND
:
887 ret
= dri2_initialize_wayland(drv
, disp
);
889 case _EGL_PLATFORM_ANDROID
:
890 ret
= dri2_initialize_android(drv
, disp
);
893 unreachable("Callers ensure we cannot get here.");
900 dri2_dpy
= dri2_egl_display(disp
);
901 dri2_dpy
->ref_count
++;
907 * Decrement display reference count, and free up display if necessary.
910 dri2_display_release(_EGLDisplay
*disp
)
912 struct dri2_egl_display
*dri2_dpy
;
917 dri2_dpy
= dri2_egl_display(disp
);
919 assert(dri2_dpy
->ref_count
> 0);
920 dri2_dpy
->ref_count
--;
922 if (dri2_dpy
->ref_count
> 0)
925 _eglCleanupDisplay(disp
);
926 dri2_display_destroy(disp
);
930 dri2_display_destroy(_EGLDisplay
*disp
)
932 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
934 if (dri2_dpy
->own_dri_screen
) {
935 if (dri2_dpy
->vtbl
&& dri2_dpy
->vtbl
->close_screen_notify
)
936 dri2_dpy
->vtbl
->close_screen_notify(disp
);
937 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
939 if (dri2_dpy
->fd
>= 0)
941 if (dri2_dpy
->driver
)
942 dlclose(dri2_dpy
->driver
);
943 free(dri2_dpy
->driver_name
);
945 #ifdef HAVE_WAYLAND_PLATFORM
946 free(dri2_dpy
->device_name
);
949 switch (disp
->Platform
) {
950 case _EGL_PLATFORM_X11
:
951 dri2_teardown_x11(dri2_dpy
);
953 case _EGL_PLATFORM_DRM
:
954 dri2_teardown_drm(dri2_dpy
);
956 case _EGL_PLATFORM_WAYLAND
:
957 dri2_teardown_wayland(dri2_dpy
);
960 /* TODO: add teardown for other platforms */
964 /* The drm platform does not create the screen/driver_configs but reuses
965 * the ones from the gbm device. As such the gbm itself is responsible
968 if (disp
->Platform
!= _EGL_PLATFORM_DRM
&& dri2_dpy
->driver_configs
) {
969 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
970 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
971 free(dri2_dpy
->driver_configs
);
974 disp
->DriverData
= NULL
;
978 dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface
*dri2_surf
,
979 unsigned int att
, unsigned int format
)
981 struct dri2_egl_display
*dri2_dpy
=
982 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
984 if (att
>= ARRAY_SIZE(dri2_surf
->local_buffers
))
987 if (!dri2_surf
->local_buffers
[att
]) {
988 dri2_surf
->local_buffers
[att
] =
989 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
, att
, format
,
990 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
993 return dri2_surf
->local_buffers
[att
];
997 dri2_egl_surface_free_local_buffers(struct dri2_egl_surface
*dri2_surf
)
999 struct dri2_egl_display
*dri2_dpy
=
1000 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1002 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->local_buffers
); i
++) {
1003 if (dri2_surf
->local_buffers
[i
]) {
1004 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
1005 dri2_surf
->local_buffers
[i
]);
1006 dri2_surf
->local_buffers
[i
] = NULL
;
1012 * Called via eglTerminate(), drv->API.Terminate().
1014 * This must be guaranteed to be called exactly once, even if eglTerminate is
1015 * called many times (without a eglInitialize in between).
1018 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1020 /* Release all non-current Context/Surfaces. */
1021 _eglReleaseDisplayResources(drv
, disp
);
1023 dri2_display_release(disp
);
1029 * Set the error code after a call to
1030 * dri2_egl_display::dri2::createContextAttribs.
1033 dri2_create_context_attribs_error(int dri_error
)
1037 switch (dri_error
) {
1038 case __DRI_CTX_ERROR_SUCCESS
:
1041 case __DRI_CTX_ERROR_NO_MEMORY
:
1042 egl_error
= EGL_BAD_ALLOC
;
1045 /* From the EGL_KHR_create_context spec, section "Errors":
1047 * * If <config> does not support a client API context compatible
1048 * with the requested API major and minor version, [...] context flags,
1049 * and context reset notification behavior (for client API types where
1050 * these attributes are supported), then an EGL_BAD_MATCH error is
1053 * * If an OpenGL ES context is requested and the values for
1054 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1055 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1056 * is not defined, than an EGL_BAD_MATCH error is generated.
1058 * * If an OpenGL context is requested, the requested version is
1059 * greater than 3.2, and the value for attribute
1060 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1061 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1062 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1063 * one of these bits set; or if the implementation does not support
1064 * the requested profile, then an EGL_BAD_MATCH error is generated.
1066 case __DRI_CTX_ERROR_BAD_API
:
1067 case __DRI_CTX_ERROR_BAD_VERSION
:
1068 case __DRI_CTX_ERROR_BAD_FLAG
:
1069 egl_error
= EGL_BAD_MATCH
;
1072 /* From the EGL_KHR_create_context spec, section "Errors":
1074 * * If an attribute name or attribute value in <attrib_list> is not
1075 * recognized (including unrecognized bits in bitmask attributes),
1076 * then an EGL_BAD_ATTRIBUTE error is generated."
1078 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
1079 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
1080 egl_error
= EGL_BAD_ATTRIBUTE
;
1084 assert(!"unknown dri_error code");
1085 egl_error
= EGL_BAD_MATCH
;
1089 _eglError(egl_error
, "dri2_create_context");
1093 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1094 struct dri2_egl_display
*dri2_dpy
,
1095 uint32_t *ctx_attribs
,
1096 unsigned *num_attribs
)
1100 assert(*num_attribs
>= NUM_ATTRIBS
);
1102 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1103 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1104 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1105 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1107 if (dri2_ctx
->base
.Flags
!= 0 || dri2_ctx
->base
.NoError
) {
1108 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1109 * extension, don't even try to send it the robust-access flag.
1110 * It may explode. Instead, generate the required EGL error here.
1112 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1113 && !dri2_dpy
->robustness
) {
1114 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1118 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1119 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
|
1120 (dri2_ctx
->base
.NoError
? __DRI_CTX_FLAG_NO_ERROR
: 0);
1123 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1124 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1125 * extension, don't even try to send it a reset strategy. It may
1126 * explode. Instead, generate the required EGL error here.
1128 if (!dri2_dpy
->robustness
) {
1129 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1133 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1134 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1137 if (dri2_ctx
->base
.ContextPriority
!= EGL_CONTEXT_PRIORITY_MEDIUM_IMG
) {
1140 switch (dri2_ctx
->base
.ContextPriority
) {
1141 case EGL_CONTEXT_PRIORITY_HIGH_IMG
:
1142 val
= __DRI_CTX_PRIORITY_HIGH
;
1144 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG
:
1145 val
= __DRI_CTX_PRIORITY_MEDIUM
;
1147 case EGL_CONTEXT_PRIORITY_LOW_IMG
:
1148 val
= __DRI_CTX_PRIORITY_LOW
;
1151 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1155 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_PRIORITY
;
1156 ctx_attribs
[pos
++] = val
;
1159 if (dri2_ctx
->base
.ReleaseBehavior
== EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR
) {
1160 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR
;
1161 ctx_attribs
[pos
++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE
;
1170 * Called via eglCreateContext(), drv->API.CreateContext().
1172 static _EGLContext
*
1173 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1174 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1176 struct dri2_egl_context
*dri2_ctx
;
1177 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1178 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1179 __DRIcontext
*shared
=
1180 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1181 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1182 const __DRIconfig
*dri_config
;
1185 unsigned num_attribs
= NUM_ATTRIBS
;
1186 uint32_t ctx_attribs
[NUM_ATTRIBS
];
1190 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1192 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1196 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1199 /* The EGL_EXT_create_context_robustness spec says:
1201 * "Add to the eglCreateContext context creation errors: [...]
1203 * * If the reset notification behavior of <share_context> and the
1204 * newly created context are different then an EGL_BAD_MATCH error is
1207 if (share_list
&& share_list
->ResetNotificationStrategy
!=
1208 dri2_ctx
->base
.ResetNotificationStrategy
) {
1209 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1213 /* The EGL_KHR_create_context_no_error spec says:
1215 * "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
1216 * used to create <share_context> does not match the value of
1217 * EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
1219 if (share_list
&& share_list
->NoError
!= dri2_ctx
->base
.NoError
) {
1220 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1224 switch (dri2_ctx
->base
.ClientAPI
) {
1225 case EGL_OPENGL_ES_API
:
1226 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1228 api
= __DRI_API_GLES
;
1231 api
= __DRI_API_GLES2
;
1234 api
= __DRI_API_GLES3
;
1237 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1242 case EGL_OPENGL_API
:
1243 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1244 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1245 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1246 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1247 api
= __DRI_API_OPENGL_CORE
;
1249 api
= __DRI_API_OPENGL
;
1252 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1258 /* The config chosen here isn't necessarily
1259 * used for surfaces later.
1260 * A pixmap surface will use the single config.
1261 * This opportunity depends on disabling the
1262 * doubleBufferMode check in
1263 * src/mesa/main/context.c:check_compatible()
1265 if (dri2_config
->dri_config
[1][0])
1266 dri_config
= dri2_config
->dri_config
[1][0];
1268 dri_config
= dri2_config
->dri_config
[0][0];
1273 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1277 if (dri2_dpy
->image_driver
) {
1278 dri2_ctx
->dri_context
=
1279 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1287 dri2_create_context_attribs_error(error
);
1288 } else if (dri2_dpy
->dri2
) {
1289 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1290 dri2_ctx
->dri_context
=
1291 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1299 dri2_create_context_attribs_error(error
);
1301 dri2_ctx
->dri_context
=
1302 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1309 assert(dri2_dpy
->swrast
);
1310 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1311 dri2_ctx
->dri_context
=
1312 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1320 dri2_create_context_attribs_error(error
);
1322 dri2_ctx
->dri_context
=
1323 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1331 if (!dri2_ctx
->dri_context
)
1334 return &dri2_ctx
->base
;
1342 * Called via eglDestroyContext(), drv->API.DestroyContext().
1345 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1347 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1348 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1350 if (_eglPutContext(ctx
)) {
1351 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1359 dri2_init_surface(_EGLSurface
*surf
, _EGLDisplay
*disp
, EGLint type
,
1360 _EGLConfig
*conf
, const EGLint
*attrib_list
,
1361 EGLBoolean enable_out_fence
, void *native_surface
)
1363 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1364 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1366 dri2_surf
->out_fence_fd
= -1;
1367 dri2_surf
->enable_out_fence
= false;
1368 if (dri2_dpy
->fence
&& dri2_dpy
->fence
->base
.version
>= 2 &&
1369 dri2_dpy
->fence
->get_capabilities
&&
1370 (dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
) &
1371 __DRI_FENCE_CAP_NATIVE_FD
)) {
1372 dri2_surf
->enable_out_fence
= enable_out_fence
;
1375 return _eglInitSurface(surf
, disp
, type
, conf
, attrib_list
, native_surface
);
1379 dri2_surface_set_out_fence_fd( _EGLSurface
*surf
, int fence_fd
)
1381 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1383 if (dri2_surf
->out_fence_fd
>= 0)
1384 close(dri2_surf
->out_fence_fd
);
1386 dri2_surf
->out_fence_fd
= fence_fd
;
1390 dri2_fini_surface(_EGLSurface
*surf
)
1392 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1394 dri2_surface_set_out_fence_fd(surf
, -1);
1395 dri2_surf
->enable_out_fence
= false;
1399 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1401 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1403 if (!_eglPutSurface(surf
))
1406 return dri2_dpy
->vtbl
->destroy_surface(drv
, disp
, surf
);
1410 dri2_surf_update_fence_fd(_EGLContext
*ctx
,
1411 _EGLDisplay
*disp
, _EGLSurface
*surf
)
1413 __DRIcontext
*dri_ctx
= dri2_egl_context(ctx
)->dri_context
;
1414 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1415 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1419 if (!dri2_surf
->enable_out_fence
)
1422 fence
= dri2_dpy
->fence
->create_fence_fd(dri_ctx
, -1);
1424 fence_fd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
1426 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, fence
);
1428 dri2_surface_set_out_fence_fd(surf
, fence_fd
);
1432 dri2_create_drawable(struct dri2_egl_display
*dri2_dpy
,
1433 const __DRIconfig
*config
,
1434 struct dri2_egl_surface
*dri2_surf
)
1436 __DRIcreateNewDrawableFunc createNewDrawable
;
1437 void *loaderPrivate
= dri2_surf
;
1439 if (dri2_dpy
->image_driver
)
1440 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
1441 else if (dri2_dpy
->dri2
)
1442 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
1443 else if (dri2_dpy
->swrast
)
1444 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
1446 return _eglError(EGL_BAD_ALLOC
, "no createNewDrawable");
1448 /* As always gbm is a bit special.. */
1449 #ifdef HAVE_DRM_PLATFORM
1450 if (dri2_surf
->gbm_surf
)
1451 loaderPrivate
= dri2_surf
->gbm_surf
;
1454 dri2_surf
->dri_drawable
= (*createNewDrawable
)(dri2_dpy
->dri_screen
,
1455 config
, loaderPrivate
);
1456 if (dri2_surf
->dri_drawable
== NULL
)
1457 return _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
1463 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1466 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1467 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1469 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1470 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1471 _EGLDisplay
*old_disp
= NULL
;
1472 struct dri2_egl_display
*old_dri2_dpy
= NULL
;
1473 _EGLContext
*old_ctx
;
1474 _EGLSurface
*old_dsurf
, *old_rsurf
;
1475 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1476 __DRIdrawable
*ddraw
, *rdraw
;
1481 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1483 /* make new bindings */
1484 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1485 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1490 old_disp
= old_ctx
->Resource
.Display
;
1491 old_dri2_dpy
= dri2_egl_display(old_disp
);
1494 /* flush before context switch */
1498 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1499 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1500 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1503 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1506 dri2_surf_update_fence_fd(old_ctx
, disp
, old_dsurf
);
1508 /* Disable shared buffer mode */
1509 if (old_dsurf
&& _eglSurfaceInSharedBufferMode(old_dsurf
) &&
1510 old_dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1511 old_dri2_dpy
->vtbl
->set_shared_buffer_mode(old_disp
, old_dsurf
, false);
1514 dri2_dpy
->core
->unbindContext(old_cctx
);
1517 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1519 if (!unbind
&& !dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1520 /* undo the previous _eglBindContext */
1521 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1522 assert(&dri2_ctx
->base
== ctx
&&
1523 tmp_dsurf
== dsurf
&&
1524 tmp_rsurf
== rsurf
);
1526 if (old_dsurf
&& _eglSurfaceInSharedBufferMode(old_dsurf
) &&
1527 old_dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1528 old_dri2_dpy
->vtbl
->set_shared_buffer_mode(old_disp
, old_dsurf
, true);
1531 _eglPutSurface(dsurf
);
1532 _eglPutSurface(rsurf
);
1533 _eglPutContext(ctx
);
1535 _eglPutSurface(old_dsurf
);
1536 _eglPutSurface(old_rsurf
);
1537 _eglPutContext(old_ctx
);
1539 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1540 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1543 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1546 dri2_destroy_surface(drv
, disp
, old_dsurf
);
1547 dri2_destroy_surface(drv
, disp
, old_rsurf
);
1550 dri2_dpy
->ref_count
++;
1553 dri2_destroy_context(drv
, disp
, old_ctx
);
1554 dri2_display_release(old_disp
);
1557 if (dsurf
&& _eglSurfaceHasMutableRenderBuffer(dsurf
) &&
1558 dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1559 /* Always update the shared buffer mode. This is obviously needed when
1560 * the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When
1561 * EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the
1562 * case where external non-EGL API may have changed window's shared
1563 * buffer mode since we last saw it.
1565 bool mode
= (dsurf
->ActiveRenderBuffer
== EGL_SINGLE_BUFFER
);
1566 dri2_dpy
->vtbl
->set_shared_buffer_mode(disp
, dsurf
, mode
);
1573 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1575 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1577 return dri2_surf
->dri_drawable
;
1581 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1584 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1586 return _glapi_get_proc_address(procname
);
1590 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1591 _EGLConfig
*conf
, void *native_window
,
1592 const EGLint
*attrib_list
)
1594 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1595 return dri2_dpy
->vtbl
->create_window_surface(drv
, disp
, conf
, native_window
,
1600 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1601 _EGLConfig
*conf
, void *native_pixmap
,
1602 const EGLint
*attrib_list
)
1604 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1605 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, disp
, conf
, native_pixmap
,
1610 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1611 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1613 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1614 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, disp
, conf
, attrib_list
);
1618 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1621 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1622 if (!dri2_dpy
->vtbl
->swap_interval
)
1624 return dri2_dpy
->vtbl
->swap_interval(drv
, disp
, surf
, interval
);
1628 * Asks the client API to flush any rendering to the drawable so that we can
1629 * do our swapbuffers.
1632 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1634 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1635 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1637 if (dri2_dpy
->flush
) {
1638 if (dri2_dpy
->flush
->base
.version
>= 4) {
1639 /* We know there's a current context because:
1641 * "If surface is not bound to the calling thread’s current
1642 * context, an EGL_BAD_SURFACE error is generated."
1644 _EGLContext
*ctx
= _eglGetCurrentContext();
1645 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1647 /* From the EGL 1.4 spec (page 52):
1649 * "The contents of ancillary buffers are always undefined
1650 * after calling eglSwapBuffers."
1652 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1654 __DRI2_FLUSH_DRAWABLE
|
1655 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1656 __DRI2_THROTTLE_SWAPBUFFER
);
1658 dri2_dpy
->flush
->flush(dri_drawable
);
1664 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1666 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1667 _EGLContext
*ctx
= _eglGetCurrentContext();
1670 dri2_surf_update_fence_fd(ctx
, disp
, surf
);
1671 return dri2_dpy
->vtbl
->swap_buffers(drv
, disp
, surf
);
1675 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*disp
,
1677 const EGLint
*rects
, EGLint n_rects
)
1679 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1680 _EGLContext
*ctx
= _eglGetCurrentContext();
1683 dri2_surf_update_fence_fd(ctx
, disp
, surf
);
1684 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, disp
, surf
,
1689 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1690 EGLint numRects
, const EGLint
*rects
)
1692 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1693 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, disp
, surf
, numRects
, rects
);
1697 dri2_set_damage_region(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1698 EGLint
*rects
, EGLint n_rects
)
1700 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1701 return dri2_dpy
->vtbl
->set_damage_region(drv
, disp
, surf
, rects
, n_rects
);
1705 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1706 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1708 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1709 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, disp
, surf
, x
, y
, width
, height
);
1713 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1714 void *native_pixmap_target
)
1716 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1717 return dri2_dpy
->vtbl
->copy_buffers(drv
, disp
, surf
, native_pixmap_target
);
1721 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1723 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1724 return dri2_dpy
->vtbl
->query_buffer_age(drv
, disp
, surf
);
1728 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1730 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1731 _EGLSurface
*surf
= ctx
->DrawSurface
;
1732 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1736 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1737 * we need to copy fake to real here.*/
1739 if (dri2_dpy
->flush
!= NULL
)
1740 dri2_dpy
->flush
->flush(dri_drawable
);
1746 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1751 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1752 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1759 dri2_bind_tex_image(_EGLDriver
*drv
,
1760 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1762 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1763 struct dri2_egl_context
*dri2_ctx
;
1765 GLint format
, target
;
1766 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1768 ctx
= _eglGetCurrentContext();
1769 dri2_ctx
= dri2_egl_context(ctx
);
1771 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1774 switch (surf
->TextureFormat
) {
1775 case EGL_TEXTURE_RGB
:
1776 format
= __DRI_TEXTURE_FORMAT_RGB
;
1778 case EGL_TEXTURE_RGBA
:
1779 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1782 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1783 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1786 switch (surf
->TextureTarget
) {
1787 case EGL_TEXTURE_2D
:
1788 target
= GL_TEXTURE_2D
;
1791 target
= GL_TEXTURE_2D
;
1792 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1795 dri2_dpy
->tex_buffer
->setTexBuffer2(dri2_ctx
->dri_context
,
1803 dri2_release_tex_image(_EGLDriver
*drv
,
1804 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1806 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1807 struct dri2_egl_context
*dri2_ctx
;
1810 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1812 ctx
= _eglGetCurrentContext();
1813 dri2_ctx
= dri2_egl_context(ctx
);
1815 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1818 switch (surf
->TextureTarget
) {
1819 case EGL_TEXTURE_2D
:
1820 target
= GL_TEXTURE_2D
;
1823 assert(!"missing texture target");
1826 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1827 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1828 dri2_dpy
->tex_buffer
->releaseTexBuffer(dri2_ctx
->dri_context
,
1829 target
, dri_drawable
);
1836 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
,
1837 EGLenum target
, EGLClientBuffer buffer
,
1838 const EGLint
*attr_list
)
1840 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1841 return dri2_dpy
->vtbl
->create_image(drv
, disp
, ctx
, target
, buffer
,
1846 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1848 struct dri2_egl_image
*dri2_img
;
1850 if (dri_image
== NULL
) {
1851 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1855 dri2_img
= malloc(sizeof *dri2_img
);
1857 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1861 _eglInitImage(&dri2_img
->base
, disp
);
1863 dri2_img
->dri_image
= dri_image
;
1865 return &dri2_img
->base
;
1869 * Translate a DRI Image extension error code into an EGL error code.
1872 egl_error_from_dri_image_error(int dri_error
)
1874 switch (dri_error
) {
1875 case __DRI_IMAGE_ERROR_SUCCESS
:
1877 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1878 return EGL_BAD_ALLOC
;
1879 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1880 return EGL_BAD_MATCH
;
1881 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1882 return EGL_BAD_PARAMETER
;
1883 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
1884 return EGL_BAD_ACCESS
;
1886 assert(!"unknown dri_error code");
1887 return EGL_BAD_ALLOC
;
1892 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1893 EGLClientBuffer buffer
,
1894 const EGLint
*attr_list
)
1896 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1897 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1898 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1899 __DRIimage
*dri_image
;
1901 if (renderbuffer
== 0) {
1902 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1903 return EGL_NO_IMAGE_KHR
;
1906 if (!disp
->Extensions
.KHR_gl_renderbuffer_image
) {
1907 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1908 return EGL_NO_IMAGE_KHR
;
1911 if (dri2_dpy
->image
->base
.version
>= 17 &&
1912 dri2_dpy
->image
->createImageFromRenderbuffer2
) {
1913 unsigned error
= ~0;
1915 dri_image
= dri2_dpy
->image
->createImageFromRenderbuffer2(
1916 dri2_ctx
->dri_context
, renderbuffer
, NULL
, &error
);
1918 assert(!!dri_image
== (error
== __DRI_IMAGE_ERROR_SUCCESS
));
1921 _eglError(egl_error_from_dri_image_error(error
), "dri2_create_image_khr");
1922 return EGL_NO_IMAGE_KHR
;
1925 dri_image
= dri2_dpy
->image
->createImageFromRenderbuffer(
1926 dri2_ctx
->dri_context
, renderbuffer
, NULL
);
1928 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1929 return EGL_NO_IMAGE_KHR
;
1933 return dri2_create_image_from_dri(disp
, dri_image
);
1936 #ifdef HAVE_WAYLAND_PLATFORM
1938 /* This structure describes how a wl_buffer maps to one or more
1939 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1940 * offsets and strides of the planes in the buffer. This table maps a
1941 * wl_drm format code to a description of the planes in the buffer
1942 * that lets us create a __DRIimage for each of the planes. */
1944 static const struct wl_drm_components_descriptor
{
1945 uint32_t dri_components
;
1948 } wl_drm_components
[] = {
1949 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1950 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1951 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1952 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1953 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1957 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1958 EGLClientBuffer _buffer
,
1959 const EGLint
*attr_list
)
1961 struct wl_drm_buffer
*buffer
;
1962 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1963 const struct wl_drm_components_descriptor
*f
;
1964 __DRIimage
*dri_image
;
1965 _EGLImageAttribs attrs
;
1968 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1969 (struct wl_resource
*) _buffer
);
1973 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
1976 plane
= attrs
.PlaneWL
;
1977 f
= buffer
->driver_format
;
1978 if (plane
< 0 || plane
>= f
->nplanes
) {
1979 _eglError(EGL_BAD_PARAMETER
,
1980 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1984 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1985 if (dri_image
== NULL
&& plane
== 0)
1986 dri_image
= dri2_dpy
->image
->dupImage(buffer
->driver_buffer
, NULL
);
1987 if (dri_image
== NULL
) {
1988 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1992 return dri2_create_image_from_dri(disp
, dri_image
);
1997 dri2_get_sync_values_chromium(_EGLDisplay
*disp
, _EGLSurface
*surf
,
1998 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
2001 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2002 return dri2_dpy
->vtbl
->get_sync_values(disp
, surf
, ust
, msc
, sbc
);
2006 * Set the error code after a call to
2007 * dri2_egl_image::dri_image::createImageFromTexture.
2010 dri2_create_image_khr_texture_error(int dri_error
)
2012 EGLint egl_error
= egl_error_from_dri_image_error(dri_error
);
2014 if (egl_error
!= EGL_SUCCESS
)
2015 _eglError(egl_error
, "dri2_create_image_khr_texture");
2019 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2021 EGLClientBuffer buffer
,
2022 const EGLint
*attr_list
)
2024 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2025 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2026 struct dri2_egl_image
*dri2_img
;
2027 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
2028 _EGLImageAttribs attrs
;
2034 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2035 return EGL_NO_IMAGE_KHR
;
2038 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2039 return EGL_NO_IMAGE_KHR
;
2042 case EGL_GL_TEXTURE_2D_KHR
:
2043 if (!disp
->Extensions
.KHR_gl_texture_2D_image
) {
2044 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2045 return EGL_NO_IMAGE_KHR
;
2048 gl_target
= GL_TEXTURE_2D
;
2050 case EGL_GL_TEXTURE_3D_KHR
:
2051 if (!disp
->Extensions
.KHR_gl_texture_3D_image
) {
2052 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2053 return EGL_NO_IMAGE_KHR
;
2056 depth
= attrs
.GLTextureZOffset
;
2057 gl_target
= GL_TEXTURE_3D
;
2059 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2060 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2061 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2062 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2063 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2064 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2065 if (!disp
->Extensions
.KHR_gl_texture_cubemap_image
) {
2066 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2067 return EGL_NO_IMAGE_KHR
;
2070 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
2071 gl_target
= GL_TEXTURE_CUBE_MAP
;
2074 unreachable("Unexpected target in dri2_create_image_khr_texture()");
2075 return EGL_NO_IMAGE_KHR
;
2078 dri2_img
= malloc(sizeof *dri2_img
);
2080 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2081 return EGL_NO_IMAGE_KHR
;
2084 _eglInitImage(&dri2_img
->base
, disp
);
2086 dri2_img
->dri_image
=
2087 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
2091 attrs
.GLTextureLevel
,
2094 dri2_create_image_khr_texture_error(error
);
2096 if (!dri2_img
->dri_image
) {
2098 return EGL_NO_IMAGE_KHR
;
2100 return &dri2_img
->base
;
2104 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
2105 EGLint attribute
, EGLint
*value
)
2107 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2108 if (!dri2_dpy
->vtbl
->query_surface
)
2109 return _eglQuerySurface(drv
, disp
, surf
, attribute
, value
);
2110 return dri2_dpy
->vtbl
->query_surface(drv
, disp
, surf
, attribute
, value
);
2113 static struct wl_buffer
*
2114 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2117 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2118 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, disp
, img
);
2123 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2124 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2126 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2127 EGLint format
, name
, pitch
;
2128 _EGLImageAttribs attrs
;
2129 __DRIimage
*dri_image
;
2131 name
= (EGLint
) (uintptr_t) buffer
;
2133 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2136 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
2137 attrs
.DRMBufferStrideMESA
<= 0) {
2138 _eglError(EGL_BAD_PARAMETER
,
2139 "bad width, height or stride");
2143 switch (attrs
.DRMBufferFormatMESA
) {
2144 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2145 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2146 pitch
= attrs
.DRMBufferStrideMESA
;
2149 _eglError(EGL_BAD_PARAMETER
,
2150 "dri2_create_image_khr: unsupported pixmap depth");
2155 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
2163 return dri2_create_image_from_dri(disp
, dri_image
);
2167 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
2172 * "Required attributes and their values are as follows:
2174 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
2176 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
2177 * by drm_fourcc.h and used as the pixel_format parameter of the
2178 * drm_mode_fb_cmd2 ioctl."
2182 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2183 * incomplete, EGL_BAD_PARAMETER is generated."
2185 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
2186 !attrs
->DMABufFourCC
.IsPresent
)
2187 return _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
2192 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
2193 * specified for a plane's pitch or offset isn't supported by EGL,
2194 * EGL_BAD_ACCESS is generated."
2196 for (unsigned i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
2197 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
2198 attrs
->DMABufPlanePitches
[i
].Value
<= 0)
2199 return _eglError(EGL_BAD_ACCESS
, "invalid pitch");
2203 * If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2204 * attribute values may be given.
2206 * This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2207 * EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2209 for (unsigned i
= 0; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2210 if (attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
!=
2211 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
)
2212 return _eglError(EGL_BAD_PARAMETER
, "modifier attribute lo or hi missing");
2215 /* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2216 * mandate it, we only accept the same modifier across all planes. */
2217 for (unsigned i
= 1; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2218 if (attrs
->DMABufPlaneFds
[i
].IsPresent
) {
2219 if ((attrs
->DMABufPlaneModifiersLo
[0].IsPresent
!=
2220 attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
) ||
2221 (attrs
->DMABufPlaneModifiersLo
[0].Value
!=
2222 attrs
->DMABufPlaneModifiersLo
[i
].Value
) ||
2223 (attrs
->DMABufPlaneModifiersHi
[0].Value
!=
2224 attrs
->DMABufPlaneModifiersHi
[i
].Value
))
2225 return _eglError(EGL_BAD_PARAMETER
, "modifier attributes not equal");
2232 /* Returns the total number of planes for the format or zero if it isn't a
2233 * valid fourcc format.
2236 dri2_num_fourcc_format_planes(EGLint format
)
2240 case DRM_FORMAT_RG88
:
2241 case DRM_FORMAT_GR88
:
2242 case DRM_FORMAT_R16
:
2243 case DRM_FORMAT_GR1616
:
2244 case DRM_FORMAT_RGB332
:
2245 case DRM_FORMAT_BGR233
:
2246 case DRM_FORMAT_XRGB4444
:
2247 case DRM_FORMAT_XBGR4444
:
2248 case DRM_FORMAT_RGBX4444
:
2249 case DRM_FORMAT_BGRX4444
:
2250 case DRM_FORMAT_ARGB4444
:
2251 case DRM_FORMAT_ABGR4444
:
2252 case DRM_FORMAT_RGBA4444
:
2253 case DRM_FORMAT_BGRA4444
:
2254 case DRM_FORMAT_XRGB1555
:
2255 case DRM_FORMAT_XBGR1555
:
2256 case DRM_FORMAT_RGBX5551
:
2257 case DRM_FORMAT_BGRX5551
:
2258 case DRM_FORMAT_ARGB1555
:
2259 case DRM_FORMAT_ABGR1555
:
2260 case DRM_FORMAT_RGBA5551
:
2261 case DRM_FORMAT_BGRA5551
:
2262 case DRM_FORMAT_RGB565
:
2263 case DRM_FORMAT_BGR565
:
2264 case DRM_FORMAT_RGB888
:
2265 case DRM_FORMAT_BGR888
:
2266 case DRM_FORMAT_XRGB8888
:
2267 case DRM_FORMAT_XBGR8888
:
2268 case DRM_FORMAT_RGBX8888
:
2269 case DRM_FORMAT_BGRX8888
:
2270 case DRM_FORMAT_ARGB8888
:
2271 case DRM_FORMAT_ABGR8888
:
2272 case DRM_FORMAT_RGBA8888
:
2273 case DRM_FORMAT_BGRA8888
:
2274 case DRM_FORMAT_XRGB2101010
:
2275 case DRM_FORMAT_XBGR2101010
:
2276 case DRM_FORMAT_RGBX1010102
:
2277 case DRM_FORMAT_BGRX1010102
:
2278 case DRM_FORMAT_ARGB2101010
:
2279 case DRM_FORMAT_ABGR2101010
:
2280 case DRM_FORMAT_RGBA1010102
:
2281 case DRM_FORMAT_BGRA1010102
:
2282 case DRM_FORMAT_YUYV
:
2283 case DRM_FORMAT_YVYU
:
2284 case DRM_FORMAT_UYVY
:
2285 case DRM_FORMAT_VYUY
:
2286 case DRM_FORMAT_AYUV
:
2287 case DRM_FORMAT_XYUV8888
:
2290 case DRM_FORMAT_NV12
:
2291 case DRM_FORMAT_NV21
:
2292 case DRM_FORMAT_NV16
:
2293 case DRM_FORMAT_NV61
:
2294 case DRM_FORMAT_P010
:
2295 case DRM_FORMAT_P012
:
2296 case DRM_FORMAT_P016
:
2299 case DRM_FORMAT_YUV410
:
2300 case DRM_FORMAT_YVU410
:
2301 case DRM_FORMAT_YUV411
:
2302 case DRM_FORMAT_YVU411
:
2303 case DRM_FORMAT_YUV420
:
2304 case DRM_FORMAT_YVU420
:
2305 case DRM_FORMAT_YUV422
:
2306 case DRM_FORMAT_YVU422
:
2307 case DRM_FORMAT_YUV444
:
2308 case DRM_FORMAT_YVU444
:
2316 /* Returns the total number of file descriptors. Zero indicates an error. */
2318 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
2320 unsigned plane_n
= dri2_num_fourcc_format_planes(attrs
->DMABufFourCC
.Value
);
2322 _eglError(EGL_BAD_MATCH
, "unknown drm fourcc format");
2326 for (unsigned i
= plane_n
; i
< DMA_BUF_MAX_PLANES
; i
++) {
2328 * The modifiers extension spec says:
2330 * "Modifiers may modify any attribute of a buffer import, including
2331 * but not limited to adding extra planes to a format which
2332 * otherwise does not have those planes. As an example, a modifier
2333 * may add a plane for an external compression buffer to a
2334 * single-plane format. The exact meaning and effect of any
2335 * modifier is canonically defined by drm_fourcc.h, not as part of
2338 if (attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
&&
2339 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
) {
2347 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2348 * incomplete, EGL_BAD_PARAMETER is generated."
2350 for (unsigned i
= 0; i
< plane_n
; ++i
) {
2351 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2352 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2353 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2354 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2360 * The spec also says:
2362 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2363 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2364 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2365 * or EGL_DMA_BUF_PLANE3_* attributes are specified."
2367 for (unsigned i
= plane_n
; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2368 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2369 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2370 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2371 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2380 dri2_query_dma_buf_formats(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2381 EGLint max
, EGLint
*formats
, EGLint
*count
)
2383 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2384 if (max
< 0 || (max
> 0 && formats
== NULL
))
2385 return _eglError(EGL_BAD_PARAMETER
, "invalid value for max count of formats");
2387 if (dri2_dpy
->image
->base
.version
< 15 ||
2388 dri2_dpy
->image
->queryDmaBufFormats
== NULL
)
2391 if (!dri2_dpy
->image
->queryDmaBufFormats(dri2_dpy
->dri_screen
, max
,
2396 /* Assert that all of the formats returned are actually fourcc formats.
2397 * Some day, if we want the internal interface function to be able to
2398 * return the fake fourcc formats defined in dri_interface.h, we'll have
2399 * to do something more clever here to pair the list down to just real
2400 * fourcc formats so that we don't leak the fake internal ones.
2402 for (int i
= 0; i
< *count
; i
++) {
2403 assert(dri2_num_fourcc_format_planes(formats
[i
]) > 0);
2411 dri2_query_dma_buf_modifiers(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint format
,
2412 EGLint max
, EGLuint64KHR
*modifiers
,
2413 EGLBoolean
*external_only
, EGLint
*count
)
2415 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2417 if (dri2_num_fourcc_format_planes(format
) == 0)
2418 return _eglError(EGL_BAD_PARAMETER
, "invalid fourcc format");
2421 return _eglError(EGL_BAD_PARAMETER
, "invalid value for max count of formats");
2423 if (max
> 0 && modifiers
== NULL
)
2424 return _eglError(EGL_BAD_PARAMETER
, "invalid modifiers array");
2426 if (dri2_dpy
->image
->base
.version
< 15 ||
2427 dri2_dpy
->image
->queryDmaBufModifiers
== NULL
)
2430 if (dri2_dpy
->image
->queryDmaBufModifiers(dri2_dpy
->dri_screen
, format
,
2432 (unsigned int *) external_only
,
2434 return _eglError(EGL_BAD_PARAMETER
, "invalid format");
2442 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2443 * EGL will take a reference to the dma_buf(s) which it will release at any
2444 * time while the EGLDisplay is initialized. It is the responsibility of the
2445 * application to close the dma_buf file descriptors."
2447 * Therefore we must never close or otherwise modify the file descriptors.
2450 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2451 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2453 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2455 _EGLImageAttribs attrs
;
2456 __DRIimage
*dri_image
;
2458 int fds
[DMA_BUF_MAX_PLANES
];
2459 int pitches
[DMA_BUF_MAX_PLANES
];
2460 int offsets
[DMA_BUF_MAX_PLANES
];
2462 bool has_modifier
= false;
2468 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2469 * error EGL_BAD_PARAMETER is generated."
2471 if (buffer
!= NULL
) {
2472 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2476 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2479 if (!dri2_check_dma_buf_attribs(&attrs
))
2482 num_fds
= dri2_check_dma_buf_format(&attrs
);
2486 for (unsigned i
= 0; i
< num_fds
; ++i
) {
2487 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2488 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2489 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2492 /* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2493 * will be present in attrs.DMABufPlaneModifiersLo[0] and
2494 * attrs.DMABufPlaneModifiersHi[0] */
2495 if (attrs
.DMABufPlaneModifiersLo
[0].IsPresent
) {
2496 modifier
= combine_u32_into_u64(attrs
.DMABufPlaneModifiersHi
[0].Value
,
2497 attrs
.DMABufPlaneModifiersLo
[0].Value
);
2498 has_modifier
= true;
2502 if (dri2_dpy
->image
->base
.version
< 15 ||
2503 dri2_dpy
->image
->createImageFromDmaBufs2
== NULL
) {
2504 _eglError(EGL_BAD_MATCH
, "unsupported dma_buf format modifier");
2505 return EGL_NO_IMAGE_KHR
;
2508 dri2_dpy
->image
->createImageFromDmaBufs2(dri2_dpy
->dri_screen
,
2509 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2510 modifier
, fds
, num_fds
, pitches
, offsets
,
2511 attrs
.DMABufYuvColorSpaceHint
.Value
,
2512 attrs
.DMABufSampleRangeHint
.Value
,
2513 attrs
.DMABufChromaHorizontalSiting
.Value
,
2514 attrs
.DMABufChromaVerticalSiting
.Value
,
2520 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2521 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2522 fds
, num_fds
, pitches
, offsets
,
2523 attrs
.DMABufYuvColorSpaceHint
.Value
,
2524 attrs
.DMABufSampleRangeHint
.Value
,
2525 attrs
.DMABufChromaHorizontalSiting
.Value
,
2526 attrs
.DMABufChromaVerticalSiting
.Value
,
2530 dri2_create_image_khr_texture_error(error
);
2533 return EGL_NO_IMAGE_KHR
;
2535 res
= dri2_create_image_from_dri(disp
, dri_image
);
2540 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2541 const EGLint
*attr_list
)
2543 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2544 struct dri2_egl_image
*dri2_img
;
2545 _EGLImageAttribs attrs
;
2546 unsigned int dri_use
, valid_mask
;
2552 _eglError(EGL_BAD_PARAMETER
, __func__
);
2553 return EGL_NO_IMAGE_KHR
;
2556 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2557 return EGL_NO_IMAGE_KHR
;
2559 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2560 _eglError(EGL_BAD_PARAMETER
, __func__
);
2561 return EGL_NO_IMAGE_KHR
;
2564 switch (attrs
.DRMBufferFormatMESA
) {
2565 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2566 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2569 _eglError(EGL_BAD_PARAMETER
, __func__
);
2570 return EGL_NO_IMAGE_KHR
;
2574 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2575 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2576 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2577 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2578 _eglError(EGL_BAD_PARAMETER
, __func__
);
2579 return EGL_NO_IMAGE_KHR
;
2583 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2584 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2585 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2586 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2587 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2588 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2590 dri2_img
= malloc(sizeof *dri2_img
);
2592 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2593 return EGL_NO_IMAGE_KHR
;
2596 _eglInitImage(&dri2_img
->base
, disp
);
2598 dri2_img
->dri_image
=
2599 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2600 attrs
.Width
, attrs
.Height
,
2601 format
, dri_use
, dri2_img
);
2602 if (dri2_img
->dri_image
== NULL
) {
2604 _eglError(EGL_BAD_ALLOC
, "dri2_create_drm_image_mesa");
2605 return EGL_NO_IMAGE_KHR
;
2608 return &dri2_img
->base
;
2612 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2613 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2615 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2616 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2620 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2621 __DRI_IMAGE_ATTRIB_NAME
, name
))
2622 return _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2625 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2626 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2629 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2630 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2636 * Checks if we can support EGL_MESA_image_dma_buf_export on this image.
2638 * The spec provides a boolean return for the driver to reject exporting for
2639 * basically any reason, but doesn't specify any particular error cases. For
2640 * now, we just fail if we don't have a DRM fourcc for the format.
2643 dri2_can_export_dma_buf_image(_EGLDisplay
*disp
, _EGLImage
*img
)
2645 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2646 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2649 if (!dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2650 __DRI_IMAGE_ATTRIB_FOURCC
, &fourcc
)) {
2658 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2660 EGLint
*fourcc
, EGLint
*nplanes
,
2661 EGLuint64KHR
*modifiers
)
2663 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2664 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2668 if (!dri2_can_export_dma_buf_image(disp
, img
))
2672 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2673 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2675 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2676 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2685 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2686 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2688 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2689 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2694 if (!dri2_can_export_dma_buf_image(disp
, img
))
2697 /* EGL_MESA_image_dma_buf_export spec says:
2698 * "If the number of fds is less than the number of planes, then
2699 * subsequent fd slots should contain -1."
2702 /* Query nplanes so that we know how big the given array is. */
2703 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2704 __DRI_IMAGE_ATTRIB_NUM_PLANES
, &nplanes
);
2705 memset(fds
, -1, nplanes
* sizeof(int));
2708 /* rework later to provide multiple fds/strides/offsets */
2710 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2711 __DRI_IMAGE_ATTRIB_FD
, fds
);
2714 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2715 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2719 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2720 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2722 offsets
[0] = img_offset
;
2733 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2734 _EGLContext
*ctx
, EGLenum target
,
2735 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2740 case EGL_GL_TEXTURE_2D_KHR
:
2741 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2742 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2743 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2744 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2745 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2746 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2747 case EGL_GL_TEXTURE_3D_KHR
:
2748 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2749 case EGL_GL_RENDERBUFFER_KHR
:
2750 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2752 case EGL_DRM_BUFFER_MESA
:
2753 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2754 case EGL_LINUX_DMA_BUF_EXT
:
2755 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2757 #ifdef HAVE_WAYLAND_PLATFORM
2758 case EGL_WAYLAND_BUFFER_WL
:
2759 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2762 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2763 return EGL_NO_IMAGE_KHR
;
2768 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2770 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2771 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2775 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2781 #ifdef HAVE_WAYLAND_PLATFORM
2784 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2785 struct wl_drm_buffer
*buffer
)
2787 _EGLDisplay
*disp
= user_data
;
2788 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2790 int dri_components
= 0;
2793 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2802 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2814 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2816 buffer
->driver_format
= NULL
;
2817 for (int i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2818 if (wl_drm_components
[i
].dri_components
== dri_components
)
2819 buffer
->driver_format
= &wl_drm_components
[i
];
2821 if (buffer
->driver_format
== NULL
)
2822 dri2_dpy
->image
->destroyImage(img
);
2824 buffer
->driver_buffer
= img
;
2828 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2830 _EGLDisplay
*disp
= user_data
;
2831 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2833 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2837 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2838 struct wl_display
*wl_dpy
)
2840 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2841 const struct wayland_drm_callbacks wl_drm_callbacks
= {
2842 .authenticate
= (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
,
2843 .reference_buffer
= dri2_wl_reference_buffer
,
2844 .release_buffer
= dri2_wl_release_buffer
,
2845 .is_format_supported
= dri2_wl_is_format_supported
2852 if (dri2_dpy
->wl_server_drm
)
2855 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2856 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2857 dri2_dpy
->image
->base
.version
>= 7 &&
2858 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2859 flags
|= WAYLAND_DRM_PRIME
;
2861 dri2_dpy
->wl_server_drm
=
2862 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2863 &wl_drm_callbacks
, disp
, flags
);
2865 if (!dri2_dpy
->wl_server_drm
)
2868 #ifdef HAVE_DRM_PLATFORM
2869 /* We have to share the wl_drm instance with gbm, so gbm can convert
2870 * wl_buffers to gbm bos. */
2871 if (dri2_dpy
->gbm_dri
)
2872 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2879 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2880 struct wl_display
*wl_dpy
)
2882 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2886 if (!dri2_dpy
->wl_server_drm
)
2889 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2890 dri2_dpy
->wl_server_drm
= NULL
;
2896 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2897 struct wl_resource
*buffer_resource
,
2898 EGLint attribute
, EGLint
*value
)
2900 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2901 struct wl_drm_buffer
*buffer
;
2902 const struct wl_drm_components_descriptor
*format
;
2904 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2908 format
= buffer
->driver_format
;
2909 switch (attribute
) {
2910 case EGL_TEXTURE_FORMAT
:
2911 *value
= format
->components
;
2914 *value
= buffer
->width
;
2917 *value
= buffer
->height
;
2926 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2928 p_atomic_inc(&sync
->refcount
);
2932 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2933 struct dri2_egl_sync
*dri2_sync
)
2935 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2936 switch (dri2_sync
->base
.Type
) {
2937 case EGL_SYNC_REUSABLE_KHR
:
2938 cnd_destroy(&dri2_sync
->cond
);
2940 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2941 if (dri2_sync
->base
.SyncFd
!= EGL_NO_NATIVE_FENCE_FD_ANDROID
)
2942 close(dri2_sync
->base
.SyncFd
);
2948 if (dri2_sync
->fence
)
2949 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2956 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2957 EGLenum type
, const EGLAttrib
*attrib_list
)
2959 _EGLContext
*ctx
= _eglGetCurrentContext();
2960 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2961 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2962 struct dri2_egl_sync
*dri2_sync
;
2964 pthread_condattr_t attr
;
2966 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2968 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2972 if (!_eglInitSync(&dri2_sync
->base
, disp
, type
, attrib_list
)) {
2978 case EGL_SYNC_FENCE_KHR
:
2979 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2980 if (!dri2_sync
->fence
) {
2981 /* Why did it fail? DRI doesn't return an error code, so we emit
2982 * a generic EGL error that doesn't communicate user error.
2984 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2990 case EGL_SYNC_CL_EVENT_KHR
:
2991 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2992 dri2_dpy
->dri_screen
,
2993 dri2_sync
->base
.CLEvent
);
2994 /* this can only happen if the cl_event passed in is invalid. */
2995 if (!dri2_sync
->fence
) {
2996 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
3001 /* the initial status must be "signaled" if the cl_event is signaled */
3002 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
3003 dri2_sync
->fence
, 0, 0))
3004 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3007 case EGL_SYNC_REUSABLE_KHR
:
3008 /* intialize attr */
3009 ret
= pthread_condattr_init(&attr
);
3012 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3017 /* change clock attribute to CLOCK_MONOTONIC */
3018 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
3021 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3026 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
3029 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3034 /* initial status of reusable sync must be "unsignaled" */
3035 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
3038 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3039 if (dri2_dpy
->fence
->create_fence_fd
) {
3040 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence_fd(
3041 dri2_ctx
->dri_context
,
3042 dri2_sync
->base
.SyncFd
);
3044 if (!dri2_sync
->fence
) {
3045 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
3052 p_atomic_set(&dri2_sync
->refcount
, 1);
3053 return &dri2_sync
->base
;
3057 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3059 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3060 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3061 EGLint ret
= EGL_TRUE
;
3064 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
3065 * then unlock all threads possibly blocked by the reusable sync before
3068 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
3069 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
3070 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3071 /* unblock all threads currently blocked by sync */
3072 err
= cnd_broadcast(&dri2_sync
->cond
);
3075 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
3080 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
3086 dri2_dup_native_fence_fd(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3088 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3089 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3091 assert(sync
->Type
== EGL_SYNC_NATIVE_FENCE_ANDROID
);
3093 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
3094 /* try to retrieve the actual native fence fd.. if rendering is
3095 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
3097 sync
->SyncFd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
3101 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
3102 /* if native fence fd still not created, return an error: */
3103 _eglError(EGL_BAD_PARAMETER
, "eglDupNativeFenceFDANDROID");
3104 return EGL_NO_NATIVE_FENCE_FD_ANDROID
;
3107 return dup(sync
->SyncFd
);
3111 dri2_set_blob_cache_funcs(_EGLDriver
*drv
, _EGLDisplay
*disp
,
3112 EGLSetBlobFuncANDROID set
,
3113 EGLGetBlobFuncANDROID get
)
3115 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3116 dri2_dpy
->blob
->set_cache_funcs(dri2_dpy
->dri_screen
,
3118 disp
->BlobCacheGet
);
3122 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
,
3123 EGLint flags
, EGLTime timeout
)
3125 _EGLContext
*ctx
= _eglGetCurrentContext();
3126 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3127 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3128 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3129 unsigned wait_flags
= 0;
3131 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
3133 /* The EGL_KHR_fence_sync spec states:
3135 * "If no context is current for the bound API,
3136 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
3138 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
3139 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
3141 /* the sync object should take a reference while waiting */
3142 dri2_egl_ref_sync(dri2_sync
);
3144 switch (sync
->Type
) {
3145 case EGL_SYNC_FENCE_KHR
:
3146 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3147 case EGL_SYNC_CL_EVENT_KHR
:
3148 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
3149 dri2_sync
->fence
, wait_flags
,
3151 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3153 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
3156 case EGL_SYNC_REUSABLE_KHR
:
3157 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
3158 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
3159 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
3163 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
3164 if (timeout
== EGL_FOREVER_KHR
) {
3165 mtx_lock(&dri2_sync
->mutex
);
3166 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
3167 mtx_unlock(&dri2_sync
->mutex
);
3169 /* if reusable sync has not been yet signaled */
3170 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
3171 /* timespecs for cnd_timedwait */
3172 struct timespec current
;
3173 struct timespec expire
;
3175 /* We override the clock to monotonic when creating the condition
3177 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
3179 /* calculating when to expire */
3180 expire
.tv_nsec
= timeout
% 1000000000L;
3181 expire
.tv_sec
= timeout
/ 1000000000L;
3183 expire
.tv_nsec
+= current
.tv_nsec
;
3184 expire
.tv_sec
+= current
.tv_sec
;
3186 /* expire.nsec now is a number between 0 and 1999999998 */
3187 if (expire
.tv_nsec
> 999999999L) {
3189 expire
.tv_nsec
-= 1000000000L;
3192 mtx_lock(&dri2_sync
->mutex
);
3193 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
3194 mtx_unlock(&dri2_sync
->mutex
);
3196 if (ret
== thrd_busy
) {
3197 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
3198 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
3200 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
3208 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
3214 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
,
3217 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3220 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
)
3221 return _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
3223 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
)
3224 return _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
3226 dri2_sync
->base
.SyncStatus
= mode
;
3228 if (mode
== EGL_SIGNALED_KHR
) {
3229 ret
= cnd_broadcast(&dri2_sync
->cond
);
3231 /* fail to broadcast */
3233 return _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
3240 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3242 _EGLContext
*ctx
= _eglGetCurrentContext();
3243 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3244 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3245 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3247 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
3248 dri2_sync
->fence
, 0);
3253 dri2_interop_query_device_info(_EGLDisplay
*disp
, _EGLContext
*ctx
,
3254 struct mesa_glinterop_device_info
*out
)
3256 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3257 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3259 if (!dri2_dpy
->interop
)
3260 return MESA_GLINTEROP_UNSUPPORTED
;
3262 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
3266 dri2_interop_export_object(_EGLDisplay
*disp
, _EGLContext
*ctx
,
3267 struct mesa_glinterop_export_in
*in
,
3268 struct mesa_glinterop_export_out
*out
)
3270 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3271 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3273 if (!dri2_dpy
->interop
)
3274 return MESA_GLINTEROP_UNSUPPORTED
;
3276 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
3280 * This is the main entrypoint into the driver, called by libEGL.
3281 * Gets an _EGLDriver object and init its dispatch table.
3284 _eglInitDriver(_EGLDriver
*dri2_drv
)
3286 dri2_drv
->API
.Initialize
= dri2_initialize
;
3287 dri2_drv
->API
.Terminate
= dri2_terminate
;
3288 dri2_drv
->API
.CreateContext
= dri2_create_context
;
3289 dri2_drv
->API
.DestroyContext
= dri2_destroy_context
;
3290 dri2_drv
->API
.MakeCurrent
= dri2_make_current
;
3291 dri2_drv
->API
.CreateWindowSurface
= dri2_create_window_surface
;
3292 dri2_drv
->API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
3293 dri2_drv
->API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
3294 dri2_drv
->API
.DestroySurface
= dri2_destroy_surface
;
3295 dri2_drv
->API
.GetProcAddress
= dri2_get_proc_address
;
3296 dri2_drv
->API
.WaitClient
= dri2_wait_client
;
3297 dri2_drv
->API
.WaitNative
= dri2_wait_native
;
3298 dri2_drv
->API
.BindTexImage
= dri2_bind_tex_image
;
3299 dri2_drv
->API
.ReleaseTexImage
= dri2_release_tex_image
;
3300 dri2_drv
->API
.SwapInterval
= dri2_swap_interval
;
3301 dri2_drv
->API
.SwapBuffers
= dri2_swap_buffers
;
3302 dri2_drv
->API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
3303 dri2_drv
->API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
3304 dri2_drv
->API
.SetDamageRegion
= dri2_set_damage_region
;
3305 dri2_drv
->API
.PostSubBufferNV
= dri2_post_sub_buffer
;
3306 dri2_drv
->API
.CopyBuffers
= dri2_copy_buffers
,
3307 dri2_drv
->API
.QueryBufferAge
= dri2_query_buffer_age
;
3308 dri2_drv
->API
.CreateImageKHR
= dri2_create_image
;
3309 dri2_drv
->API
.DestroyImageKHR
= dri2_destroy_image_khr
;
3310 dri2_drv
->API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
3311 dri2_drv
->API
.QuerySurface
= dri2_query_surface
;
3312 dri2_drv
->API
.QueryDriverName
= dri2_query_driver_name
;
3313 dri2_drv
->API
.QueryDriverConfig
= dri2_query_driver_config
;
3315 dri2_drv
->API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
3316 dri2_drv
->API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
3317 dri2_drv
->API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
3318 dri2_drv
->API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
3319 dri2_drv
->API
.QueryDmaBufFormatsEXT
= dri2_query_dma_buf_formats
;
3320 dri2_drv
->API
.QueryDmaBufModifiersEXT
= dri2_query_dma_buf_modifiers
;
3322 #ifdef HAVE_WAYLAND_PLATFORM
3323 dri2_drv
->API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
3324 dri2_drv
->API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
3325 dri2_drv
->API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
3327 dri2_drv
->API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
3328 dri2_drv
->API
.CreateSyncKHR
= dri2_create_sync
;
3329 dri2_drv
->API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
3330 dri2_drv
->API
.SignalSyncKHR
= dri2_signal_sync
;
3331 dri2_drv
->API
.WaitSyncKHR
= dri2_server_wait_sync
;
3332 dri2_drv
->API
.DestroySyncKHR
= dri2_destroy_sync
;
3333 dri2_drv
->API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
3334 dri2_drv
->API
.GLInteropExportObject
= dri2_interop_export_object
;
3335 dri2_drv
->API
.DupNativeFenceFDANDROID
= dri2_dup_native_fence_fd
;
3336 dri2_drv
->API
.SetBlobCacheFuncsANDROID
= dri2_set_blob_cache_funcs
;