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/os_file.h"
66 #include "util/u_atomic.h"
67 #include "util/u_vector.h"
68 #include "mapi/glapi/glapi.h"
69 #include "util/bitscan.h"
70 #include "util/u_math.h"
72 /* Additional definitions not yet in the drm_fourcc.h.
74 #ifndef DRM_FORMAT_P010
75 #define DRM_FORMAT_P010 fourcc_code('P', '0', '1', '0') /* 2x2 subsampled Cb:Cr plane 10 bits per channel */
78 #ifndef DRM_FORMAT_P012
79 #define DRM_FORMAT_P012 fourcc_code('P', '0', '1', '2') /* 2x2 subsampled Cb:Cr plane 12 bits per channel */
82 #ifndef DRM_FORMAT_P016
83 #define DRM_FORMAT_P016 fourcc_code('P', '0', '1', '6') /* 2x2 subsampled Cb:Cr plane 16 bits per channel */
86 #define NUM_ATTRIBS 12
88 static const struct dri2_pbuffer_visual
{
89 const char *format_name
;
90 unsigned int dri_image_format
;
92 unsigned int rgba_sizes
[4];
93 } dri2_pbuffer_visuals
[] = {
96 __DRI_IMAGE_FORMAT_ABGR16161616F
,
102 __DRI_IMAGE_FORMAT_XBGR16161616F
,
108 __DRI_IMAGE_FORMAT_ARGB2101010
,
114 __DRI_IMAGE_FORMAT_XRGB2101010
,
120 __DRI_IMAGE_FORMAT_ARGB8888
,
126 __DRI_IMAGE_FORMAT_XRGB8888
,
132 __DRI_IMAGE_FORMAT_RGB565
,
139 dri_set_background_context(void *loaderPrivate
)
141 _EGLContext
*ctx
= _eglGetCurrentContext();
142 _EGLThreadInfo
*t
= _eglGetCurrentThread();
144 _eglBindContextToThread(ctx
, t
);
150 static void (*glFlush
)(void);
151 static mtx_t glFlushMutex
= _MTX_INITIALIZER_NP
;
153 mtx_lock(&glFlushMutex
);
155 glFlush
= _glapi_get_proc_address("glFlush");
156 mtx_unlock(&glFlushMutex
);
158 /* if glFlush is not available things are horribly broken */
160 _eglLog(_EGL_WARNING
, "DRI2: failed to find glFlush entry point");
168 dri_is_thread_safe(void *loaderPrivate
)
170 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
171 UNUSED _EGLDisplay
*display
= dri2_surf
->base
.Resource
.Display
;
173 #ifdef HAVE_X11_PLATFORM
174 Display
*xdpy
= (Display
*)display
->PlatformDisplay
;
176 /* Check Xlib is running in thread safe mode when running on EGL/X11-xlib
179 * 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
180 * It wll be NULL if XInitThreads wasn't called.
182 if (display
->Platform
== _EGL_PLATFORM_X11
&& xdpy
&& !xdpy
->lock_fns
)
186 #ifdef HAVE_WAYLAND_PLATFORM
187 if (display
->Platform
== _EGL_PLATFORM_WAYLAND
)
194 const __DRIbackgroundCallableExtension background_callable_extension
= {
195 .base
= { __DRI_BACKGROUND_CALLABLE
, 2 },
197 .setBackgroundContext
= dri_set_background_context
,
198 .isThreadSafe
= dri_is_thread_safe
,
201 const __DRIuseInvalidateExtension use_invalidate
= {
202 .base
= { __DRI_USE_INVALIDATE
, 1 }
206 dri2_get_pbuffer_drawable_info(__DRIdrawable
* draw
,
207 int *x
, int *y
, int *w
, int *h
,
210 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
213 *w
= dri2_surf
->base
.Width
;
214 *h
= dri2_surf
->base
.Height
;
218 dri2_get_bytes_per_pixel(struct dri2_egl_surface
*dri2_surf
)
220 const int depth
= dri2_surf
->base
.Config
->BufferSize
;
221 return depth
? util_next_power_of_two(depth
/ 8) : 0;
225 dri2_put_image(__DRIdrawable
* draw
, int op
,
226 int x
, int y
, int w
, int h
,
227 char *data
, void *loaderPrivate
)
229 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
230 const int bpp
= dri2_get_bytes_per_pixel(dri2_surf
);
231 const int width
= dri2_surf
->base
.Width
;
232 const int height
= dri2_surf
->base
.Height
;
233 const int dst_stride
= width
*bpp
;
234 const int src_stride
= w
*bpp
;
235 const int x_offset
= x
*bpp
;
236 int copy_width
= src_stride
;
238 if (!dri2_surf
->swrast_device_buffer
)
239 dri2_surf
->swrast_device_buffer
= malloc(height
*dst_stride
);
241 if (dri2_surf
->swrast_device_buffer
) {
242 const char *src
= data
;
243 char *dst
= dri2_surf
->swrast_device_buffer
;
248 /* Drivers are allowed to submit OOB PutImage requests, so clip here. */
249 if (copy_width
> dst_stride
- x_offset
)
250 copy_width
= dst_stride
- x_offset
;
255 memcpy(dst
, src
, copy_width
);
263 dri2_get_image(__DRIdrawable
* read
,
264 int x
, int y
, int w
, int h
,
265 char *data
, void *loaderPrivate
)
267 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
268 const int bpp
= dri2_get_bytes_per_pixel(dri2_surf
);
269 const int width
= dri2_surf
->base
.Width
;
270 const int height
= dri2_surf
->base
.Height
;
271 const int src_stride
= width
*bpp
;
272 const int dst_stride
= w
*bpp
;
273 const int x_offset
= x
*bpp
;
274 int copy_width
= dst_stride
;
275 const char *src
= dri2_surf
->swrast_device_buffer
;
279 memset(data
, 0, copy_width
* h
);
286 /* Drivers are allowed to submit OOB GetImage requests, so clip here. */
287 if (copy_width
> src_stride
- x_offset
)
288 copy_width
= src_stride
- x_offset
;
293 memcpy(dst
, src
, copy_width
);
300 /* HACK: technically we should have swrast_null, instead of these.
302 const __DRIswrastLoaderExtension swrast_pbuffer_loader_extension
= {
303 .base
= { __DRI_SWRAST_LOADER
, 1 },
304 .getDrawableInfo
= dri2_get_pbuffer_drawable_info
,
305 .putImage
= dri2_put_image
,
306 .getImage
= dri2_get_image
,
309 static const EGLint dri2_to_egl_attribute_map
[__DRI_ATTRIB_MAX
] = {
310 [__DRI_ATTRIB_BUFFER_SIZE
] = EGL_BUFFER_SIZE
,
311 [__DRI_ATTRIB_LEVEL
] = EGL_LEVEL
,
312 [__DRI_ATTRIB_LUMINANCE_SIZE
] = EGL_LUMINANCE_SIZE
,
313 [__DRI_ATTRIB_DEPTH_SIZE
] = EGL_DEPTH_SIZE
,
314 [__DRI_ATTRIB_STENCIL_SIZE
] = EGL_STENCIL_SIZE
,
315 [__DRI_ATTRIB_SAMPLE_BUFFERS
] = EGL_SAMPLE_BUFFERS
,
316 [__DRI_ATTRIB_SAMPLES
] = EGL_SAMPLES
,
317 [__DRI_ATTRIB_MAX_PBUFFER_WIDTH
] = EGL_MAX_PBUFFER_WIDTH
,
318 [__DRI_ATTRIB_MAX_PBUFFER_HEIGHT
] = EGL_MAX_PBUFFER_HEIGHT
,
319 [__DRI_ATTRIB_MAX_PBUFFER_PIXELS
] = EGL_MAX_PBUFFER_PIXELS
,
320 [__DRI_ATTRIB_MAX_SWAP_INTERVAL
] = EGL_MAX_SWAP_INTERVAL
,
321 [__DRI_ATTRIB_MIN_SWAP_INTERVAL
] = EGL_MIN_SWAP_INTERVAL
,
322 [__DRI_ATTRIB_YINVERTED
] = EGL_Y_INVERTED_NOK
,
326 dri2_get_dri_config(struct dri2_egl_config
*conf
, EGLint surface_type
,
329 const bool double_buffer
= surface_type
== EGL_WINDOW_BIT
;
330 const bool srgb
= colorspace
== EGL_GL_COLORSPACE_SRGB_KHR
;
332 return conf
->dri_config
[double_buffer
][srgb
];
336 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
338 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
341 if (!_eglMatchConfig(conf
, criteria
))
348 dri2_get_shifts_and_sizes(const __DRIcoreExtension
*core
,
349 const __DRIconfig
*config
, int *shifts
,
354 if (core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_SHIFT
, (unsigned int *)&shifts
[0])) {
355 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_SHIFT
, (unsigned int *)&shifts
[1]);
356 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_SHIFT
, (unsigned int *)&shifts
[2]);
357 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_SHIFT
, (unsigned int *)&shifts
[3]);
359 /* Driver isn't exposing shifts, so convert masks to shifts */
360 core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_MASK
, &mask
);
361 shifts
[0] = ffs(mask
) - 1;
362 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_MASK
, &mask
);
363 shifts
[1] = ffs(mask
) - 1;
364 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_MASK
, &mask
);
365 shifts
[2] = ffs(mask
) - 1;
366 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_MASK
, &mask
);
367 shifts
[3] = ffs(mask
) - 1;
370 core
->getConfigAttrib(config
, __DRI_ATTRIB_RED_SIZE
, &sizes
[0]);
371 core
->getConfigAttrib(config
, __DRI_ATTRIB_GREEN_SIZE
, &sizes
[1]);
372 core
->getConfigAttrib(config
, __DRI_ATTRIB_BLUE_SIZE
, &sizes
[2]);
373 core
->getConfigAttrib(config
, __DRI_ATTRIB_ALPHA_SIZE
, &sizes
[3]);
377 dri2_get_render_type_float(const __DRIcoreExtension
*core
,
378 const __DRIconfig
*config
,
381 unsigned int render_type
;
383 core
->getConfigAttrib(config
, __DRI_ATTRIB_RENDER_TYPE
, &render_type
);
384 *is_float
= (render_type
& __DRI_ATTRIB_FLOAT_BIT
) ? true : false;
388 dri2_image_format_for_pbuffer_config(struct dri2_egl_display
*dri2_dpy
,
389 const __DRIconfig
*config
)
392 unsigned int sizes
[4];
394 dri2_get_shifts_and_sizes(dri2_dpy
->core
, config
, shifts
, sizes
);
396 for (unsigned i
= 0; i
< ARRAY_SIZE(dri2_pbuffer_visuals
); ++i
) {
397 const struct dri2_pbuffer_visual
*visual
= &dri2_pbuffer_visuals
[i
];
399 if (shifts
[0] == visual
->rgba_shifts
[0] &&
400 shifts
[1] == visual
->rgba_shifts
[1] &&
401 shifts
[2] == visual
->rgba_shifts
[2] &&
402 shifts
[3] == visual
->rgba_shifts
[3] &&
403 sizes
[0] == visual
->rgba_sizes
[0] &&
404 sizes
[1] == visual
->rgba_sizes
[1] &&
405 sizes
[2] == visual
->rgba_sizes
[2] &&
406 sizes
[3] == visual
->rgba_sizes
[3]) {
407 return visual
->dri_image_format
;
411 return __DRI_IMAGE_FORMAT_NONE
;
414 struct dri2_egl_config
*
415 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
416 EGLint surface_type
, const EGLint
*attr_list
,
417 const int *rgba_shifts
, const unsigned int *rgba_sizes
)
419 struct dri2_egl_config
*conf
;
420 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
422 unsigned int attrib
, value
, double_buffer
;
424 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
425 int dri_shifts
[4] = { -1, -1, -1, -1 };
426 unsigned int dri_sizes
[4] = { 0, 0, 0, 0 };
427 _EGLConfig
*matching_config
;
428 EGLint num_configs
= 0;
431 _eglInitConfig(&base
, disp
, id
);
434 bind_to_texture_rgb
= 0;
435 bind_to_texture_rgba
= 0;
437 for (int i
= 0; i
< __DRI_ATTRIB_MAX
; ++i
) {
438 if (!dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
, &attrib
, &value
))
442 case __DRI_ATTRIB_RENDER_TYPE
:
443 if (value
& __DRI_ATTRIB_FLOAT_BIT
)
444 base
.ComponentType
= EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT
;
445 if (value
& __DRI_ATTRIB_RGBA_BIT
)
446 value
= EGL_RGB_BUFFER
;
447 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
448 value
= EGL_LUMINANCE_BUFFER
;
451 base
.ColorBufferType
= value
;
454 case __DRI_ATTRIB_CONFIG_CAVEAT
:
455 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
456 value
= EGL_NON_CONFORMANT_CONFIG
;
457 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
458 value
= EGL_SLOW_CONFIG
;
461 base
.ConfigCaveat
= value
;
464 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
465 bind_to_texture_rgb
= value
;
468 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
469 bind_to_texture_rgba
= value
;
472 case __DRI_ATTRIB_DOUBLE_BUFFER
:
473 double_buffer
= value
;
476 case __DRI_ATTRIB_RED_SIZE
:
477 dri_sizes
[0] = value
;
478 base
.RedSize
= value
;
481 case __DRI_ATTRIB_RED_MASK
:
482 dri_shifts
[0] = ffs(value
) - 1;
485 case __DRI_ATTRIB_RED_SHIFT
:
486 dri_shifts
[0] = value
;
489 case __DRI_ATTRIB_GREEN_SIZE
:
490 dri_sizes
[1] = value
;
491 base
.GreenSize
= value
;
494 case __DRI_ATTRIB_GREEN_MASK
:
495 dri_shifts
[1] = ffs(value
) - 1;
498 case __DRI_ATTRIB_GREEN_SHIFT
:
499 dri_shifts
[1] = value
;
502 case __DRI_ATTRIB_BLUE_SIZE
:
503 dri_sizes
[2] = value
;
504 base
.BlueSize
= value
;
507 case __DRI_ATTRIB_BLUE_MASK
:
508 dri_shifts
[2] = ffs(value
) - 1;
511 case __DRI_ATTRIB_BLUE_SHIFT
:
512 dri_shifts
[2] = value
;
515 case __DRI_ATTRIB_ALPHA_SIZE
:
516 dri_sizes
[3] = value
;
517 base
.AlphaSize
= value
;
520 case __DRI_ATTRIB_ALPHA_MASK
:
521 dri_shifts
[3] = ffs(value
) - 1;
524 case __DRI_ATTRIB_ALPHA_SHIFT
:
525 dri_shifts
[3] = value
;
528 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
529 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
530 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
531 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
532 /* Don't expose visuals with the accumulation buffer. */
537 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
539 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
543 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH
:
544 base
.MaxPbufferWidth
= _EGL_MAX_PBUFFER_WIDTH
;
546 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT
:
547 base
.MaxPbufferHeight
= _EGL_MAX_PBUFFER_HEIGHT
;
549 case __DRI_ATTRIB_MUTABLE_RENDER_BUFFER
:
550 if (disp
->Extensions
.KHR_mutable_render_buffer
)
551 surface_type
|= EGL_MUTABLE_RENDER_BUFFER_BIT_KHR
;
554 key
= dri2_to_egl_attribute_map
[attrib
];
556 _eglSetConfigKey(&base
, key
, value
);
562 for (int i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
563 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
565 if (rgba_shifts
&& memcmp(rgba_shifts
, dri_shifts
, sizeof(dri_shifts
)))
568 if (rgba_sizes
&& memcmp(rgba_sizes
, dri_sizes
, sizeof(dri_sizes
)))
571 base
.NativeRenderable
= EGL_TRUE
;
573 base
.SurfaceType
= surface_type
;
574 if (surface_type
& (EGL_PBUFFER_BIT
|
575 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
576 base
.BindToTextureRGB
= bind_to_texture_rgb
;
577 if (base
.AlphaSize
> 0)
578 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
582 surface_type
&= ~EGL_PIXMAP_BIT
;
585 /* No support for pbuffer + MSAA for now.
587 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
588 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
591 surface_type
&= ~EGL_PBUFFER_BIT
;
597 base
.RenderableType
= disp
->ClientAPIs
;
598 base
.Conformant
= disp
->ClientAPIs
;
600 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
601 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
603 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
604 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
608 config_id
= base
.ConfigID
;
609 base
.ConfigID
= EGL_DONT_CARE
;
610 base
.SurfaceType
= EGL_DONT_CARE
;
611 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
612 (_EGLArrayForEach
) dri2_match_config
, &base
);
614 if (num_configs
== 1) {
615 conf
= (struct dri2_egl_config
*) matching_config
;
617 if (!conf
->dri_config
[double_buffer
][srgb
])
618 conf
->dri_config
[double_buffer
][srgb
] = dri_config
;
620 /* a similar config type is already added (unlikely) => discard */
623 else if (num_configs
== 0) {
624 conf
= calloc(1, sizeof *conf
);
628 conf
->dri_config
[double_buffer
][srgb
] = dri_config
;
630 memcpy(&conf
->base
, &base
, sizeof base
);
631 conf
->base
.SurfaceType
= 0;
632 conf
->base
.ConfigID
= config_id
;
634 _eglLinkConfig(&conf
->base
);
637 unreachable("duplicates should not be possible");
641 conf
->base
.SurfaceType
|= surface_type
;
647 dri2_add_pbuffer_configs_for_visuals(_EGLDisplay
*disp
)
649 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
650 unsigned int format_count
[ARRAY_SIZE(dri2_pbuffer_visuals
)] = { 0 };
651 unsigned int config_count
= 0;
653 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
] != NULL
; i
++) {
654 for (unsigned j
= 0; j
< ARRAY_SIZE(dri2_pbuffer_visuals
); j
++) {
655 struct dri2_egl_config
*dri2_conf
;
657 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
658 config_count
+ 1, EGL_PBUFFER_BIT
, NULL
,
659 dri2_pbuffer_visuals
[j
].rgba_shifts
, dri2_pbuffer_visuals
[j
].rgba_sizes
);
662 if (dri2_conf
->base
.ConfigID
== config_count
+ 1)
669 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
670 if (!format_count
[i
]) {
671 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
672 dri2_pbuffer_visuals
[i
].format_name
);
676 return (config_count
!= 0);
680 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
682 _EGLDisplay
*disp
= data
;
683 struct dri2_egl_image
*dri2_img
;
688 img
= _eglLookupImage(image
, disp
);
690 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
694 dri2_img
= dri2_egl_image(image
);
696 return dri2_img
->dri_image
;
699 const __DRIimageLookupExtension image_lookup_extension
= {
700 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
702 .lookupEGLImage
= dri2_lookup_egl_image
705 struct dri2_extension_match
{
711 static const struct dri2_extension_match dri3_driver_extensions
[] = {
712 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
713 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
717 static const struct dri2_extension_match dri2_driver_extensions
[] = {
718 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
719 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
723 static const struct dri2_extension_match dri2_core_extensions
[] = {
724 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
725 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
726 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
730 static const struct dri2_extension_match swrast_driver_extensions
[] = {
731 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
732 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
736 static const struct dri2_extension_match swrast_core_extensions
[] = {
737 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
741 static const struct dri2_extension_match optional_driver_extensions
[] = {
742 { __DRI_CONFIG_OPTIONS
, 1, offsetof(struct dri2_egl_display
, configOptions
) },
746 static const struct dri2_extension_match optional_core_extensions
[] = {
747 { __DRI2_ROBUSTNESS
, 1, offsetof(struct dri2_egl_display
, robustness
) },
748 { __DRI2_NO_ERROR
, 1, offsetof(struct dri2_egl_display
, no_error
) },
749 { __DRI2_CONFIG_QUERY
, 1, offsetof(struct dri2_egl_display
, config
) },
750 { __DRI2_FENCE
, 1, offsetof(struct dri2_egl_display
, fence
) },
751 { __DRI2_BUFFER_DAMAGE
, 1, offsetof(struct dri2_egl_display
, buffer_damage
) },
752 { __DRI2_RENDERER_QUERY
, 1, offsetof(struct dri2_egl_display
, rendererQuery
) },
753 { __DRI2_INTEROP
, 1, offsetof(struct dri2_egl_display
, interop
) },
754 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
755 { __DRI2_FLUSH_CONTROL
, 1, offsetof(struct dri2_egl_display
, flush_control
) },
756 { __DRI2_BLOB
, 1, offsetof(struct dri2_egl_display
, blob
) },
757 { __DRI_MUTABLE_RENDER_BUFFER_DRIVER
, 1, offsetof(struct dri2_egl_display
, mutable_render_buffer
) },
762 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
763 const struct dri2_extension_match
*matches
,
764 const __DRIextension
**extensions
,
770 for (int i
= 0; extensions
[i
]; i
++) {
771 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
772 for (int j
= 0; matches
[j
].name
; j
++) {
773 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
774 extensions
[i
]->version
>= matches
[j
].version
) {
775 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
776 *(const __DRIextension
**) field
= extensions
[i
];
777 _eglLog(_EGL_INFO
, "found extension %s version %d",
778 extensions
[i
]->name
, extensions
[i
]->version
);
784 for (int j
= 0; matches
[j
].name
; j
++) {
785 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
786 if (*(const __DRIextension
**) field
== NULL
) {
788 _eglLog(_EGL_DEBUG
, "did not find optional extension %s version %d",
789 matches
[j
].name
, matches
[j
].version
);
791 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
792 matches
[j
].name
, matches
[j
].version
);
801 static const __DRIextension
**
802 dri2_open_driver(_EGLDisplay
*disp
)
804 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
805 static const char *search_path_vars
[] = {
806 "LIBGL_DRIVERS_PATH",
810 return loader_open_driver(dri2_dpy
->driver_name
,
816 dri2_load_driver_common(_EGLDisplay
*disp
,
817 const struct dri2_extension_match
*driver_extensions
)
819 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
820 const __DRIextension
**extensions
;
822 extensions
= dri2_open_driver(disp
);
826 if (!dri2_bind_extensions(dri2_dpy
, driver_extensions
, extensions
, false)) {
827 dlclose(dri2_dpy
->driver
);
828 dri2_dpy
->driver
= NULL
;
831 dri2_dpy
->driver_extensions
= extensions
;
833 dri2_bind_extensions(dri2_dpy
, optional_driver_extensions
, extensions
, true);
839 dri2_load_driver(_EGLDisplay
*disp
)
841 return dri2_load_driver_common(disp
, dri2_driver_extensions
);
845 dri2_load_driver_dri3(_EGLDisplay
*disp
)
847 return dri2_load_driver_common(disp
, dri3_driver_extensions
);
851 dri2_load_driver_swrast(_EGLDisplay
*disp
)
853 return dri2_load_driver_common(disp
, swrast_driver_extensions
);
857 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
859 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
860 unsigned int value
= 0;
862 if (!rendererQuery
||
863 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
870 dri2_query_driver_name(_EGLDisplay
*disp
)
872 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
873 return dri2_dpy
->driver_name
;
877 dri2_query_driver_config(_EGLDisplay
*disp
)
879 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
880 const __DRIconfigOptionsExtension
*ext
= dri2_dpy
->configOptions
;
882 if (ext
->base
.version
>= 2)
883 return ext
->getXml(dri2_dpy
->driver_name
);
885 return strdup(ext
->xml
);
890 dri2_setup_screen(_EGLDisplay
*disp
)
892 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
893 unsigned int api_mask
;
896 * EGL 1.5 specification defines the default value to 1. Moreover,
897 * eglSwapInterval() is required to clamp requested value to the supported
898 * range. Since the default value is implicitly assumed to be supported,
899 * use it as both minimum and maximum for the platforms that do not allow
900 * changing the interval. Platforms, which allow it (e.g. x11, wayland)
901 * override these values already.
903 dri2_dpy
->min_swap_interval
= 1;
904 dri2_dpy
->max_swap_interval
= 1;
905 dri2_dpy
->default_swap_interval
= 1;
907 if (dri2_dpy
->image_driver
) {
908 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
909 } else if (dri2_dpy
->dri2
) {
910 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
912 assert(dri2_dpy
->swrast
);
913 api_mask
= 1 << __DRI_API_OPENGL
|
914 1 << __DRI_API_GLES
|
915 1 << __DRI_API_GLES2
|
916 1 << __DRI_API_GLES3
;
919 disp
->ClientAPIs
= 0;
920 if ((api_mask
& (1 <<__DRI_API_OPENGL
)) && _eglIsApiValid(EGL_OPENGL_API
))
921 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
922 if ((api_mask
& (1 << __DRI_API_GLES
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
923 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
924 if ((api_mask
& (1 << __DRI_API_GLES2
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
925 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
926 if ((api_mask
& (1 << __DRI_API_GLES3
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
927 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
929 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
930 disp
->Extensions
.KHR_no_config_context
= EGL_TRUE
;
931 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
933 if (dri2_dpy
->configOptions
) {
934 disp
->Extensions
.MESA_query_driver
= EGL_TRUE
;
937 /* Report back to EGL the bitmask of priorities supported */
938 disp
->Extensions
.IMG_context_priority
=
939 dri2_renderer_query_integer(dri2_dpy
,
940 __DRI2_RENDERER_HAS_CONTEXT_PRIORITY
);
942 disp
->Extensions
.EXT_pixel_format_float
= EGL_TRUE
;
944 if (dri2_renderer_query_integer(dri2_dpy
,
945 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
946 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
948 if (dri2_dpy
->image_driver
||
949 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
950 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
951 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
953 if (dri2_dpy
->robustness
)
954 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
957 if (dri2_dpy
->no_error
)
958 disp
->Extensions
.KHR_create_context_no_error
= EGL_TRUE
;
960 if (dri2_dpy
->fence
) {
961 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
962 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
963 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
964 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
965 if (dri2_dpy
->fence
->base
.version
>= 2 &&
966 dri2_dpy
->fence
->get_capabilities
) {
967 unsigned capabilities
=
968 dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
);
969 disp
->Extensions
.ANDROID_native_fence_sync
=
970 (capabilities
& __DRI_FENCE_CAP_NATIVE_FD
) != 0;
975 disp
->Extensions
.ANDROID_blob_cache
= EGL_TRUE
;
977 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
979 if (dri2_dpy
->image
) {
980 if (dri2_dpy
->image
->base
.version
>= 10 &&
981 dri2_dpy
->image
->getCapabilities
!= NULL
) {
984 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
985 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
987 if (dri2_dpy
->image
->base
.version
>= 11)
988 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
990 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
991 if (dri2_dpy
->image
->base
.version
>= 11)
992 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
995 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
996 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
997 if (dri2_dpy
->image
->base
.version
>= 5 &&
998 dri2_dpy
->image
->createImageFromTexture
) {
999 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
1000 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
1002 if (dri2_renderer_query_integer(dri2_dpy
,
1003 __DRI2_RENDERER_HAS_TEXTURE_3D
))
1004 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
1007 if (dri2_dpy
->image
->base
.version
>= 8 &&
1008 dri2_dpy
->image
->createImageFromDmaBufs
) {
1009 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
1010 disp
->Extensions
.EXT_image_dma_buf_import_modifiers
= EGL_TRUE
;
1015 if (dri2_dpy
->flush_control
)
1016 disp
->Extensions
.KHR_context_flush_control
= EGL_TRUE
;
1018 if (dri2_dpy
->buffer_damage
&& dri2_dpy
->buffer_damage
->set_damage_region
)
1019 disp
->Extensions
.KHR_partial_update
= EGL_TRUE
;
1023 dri2_setup_swap_interval(_EGLDisplay
*disp
, int max_swap_interval
)
1025 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1026 GLint vblank_mode
= DRI_CONF_VBLANK_DEF_INTERVAL_1
;
1028 /* Allow driconf to override applications.*/
1029 if (dri2_dpy
->config
)
1030 dri2_dpy
->config
->configQueryi(dri2_dpy
->dri_screen
,
1031 "vblank_mode", &vblank_mode
);
1032 switch (vblank_mode
) {
1033 case DRI_CONF_VBLANK_NEVER
:
1034 dri2_dpy
->min_swap_interval
= 0;
1035 dri2_dpy
->max_swap_interval
= 0;
1036 dri2_dpy
->default_swap_interval
= 0;
1038 case DRI_CONF_VBLANK_ALWAYS_SYNC
:
1039 dri2_dpy
->min_swap_interval
= 1;
1040 dri2_dpy
->max_swap_interval
= max_swap_interval
;
1041 dri2_dpy
->default_swap_interval
= 1;
1043 case DRI_CONF_VBLANK_DEF_INTERVAL_0
:
1044 dri2_dpy
->min_swap_interval
= 0;
1045 dri2_dpy
->max_swap_interval
= max_swap_interval
;
1046 dri2_dpy
->default_swap_interval
= 0;
1049 case DRI_CONF_VBLANK_DEF_INTERVAL_1
:
1050 dri2_dpy
->min_swap_interval
= 0;
1051 dri2_dpy
->max_swap_interval
= max_swap_interval
;
1052 dri2_dpy
->default_swap_interval
= 1;
1057 /* All platforms but DRM call this function to create the screen and populate
1058 * the driver_configs. DRM inherits that information from its display - GBM.
1061 dri2_create_screen(_EGLDisplay
*disp
)
1063 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1065 if (dri2_dpy
->image_driver
) {
1066 dri2_dpy
->dri_screen
=
1067 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
1068 dri2_dpy
->loader_extensions
,
1069 dri2_dpy
->driver_extensions
,
1070 &dri2_dpy
->driver_configs
,
1072 } else if (dri2_dpy
->dri2
) {
1073 if (dri2_dpy
->dri2
->base
.version
>= 4) {
1074 dri2_dpy
->dri_screen
=
1075 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
1076 dri2_dpy
->loader_extensions
,
1077 dri2_dpy
->driver_extensions
,
1078 &dri2_dpy
->driver_configs
, disp
);
1080 dri2_dpy
->dri_screen
=
1081 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
1082 dri2_dpy
->loader_extensions
,
1083 &dri2_dpy
->driver_configs
, disp
);
1086 assert(dri2_dpy
->swrast
);
1087 if (dri2_dpy
->swrast
->base
.version
>= 4) {
1088 dri2_dpy
->dri_screen
=
1089 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->loader_extensions
,
1090 dri2_dpy
->driver_extensions
,
1091 &dri2_dpy
->driver_configs
, disp
);
1093 dri2_dpy
->dri_screen
=
1094 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->loader_extensions
,
1095 &dri2_dpy
->driver_configs
, disp
);
1099 if (dri2_dpy
->dri_screen
== NULL
) {
1100 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
1104 dri2_dpy
->own_dri_screen
= true;
1109 dri2_setup_extensions(_EGLDisplay
*disp
)
1111 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1112 const struct dri2_extension_match
*mandatory_core_extensions
;
1113 const __DRIextension
**extensions
;
1115 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
1117 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
)
1118 mandatory_core_extensions
= dri2_core_extensions
;
1120 mandatory_core_extensions
= swrast_core_extensions
;
1122 if (!dri2_bind_extensions(dri2_dpy
, mandatory_core_extensions
, extensions
, false))
1125 #ifdef HAVE_DRI3_MODIFIERS
1126 dri2_dpy
->multibuffers_available
=
1127 (dri2_dpy
->dri3_major_version
> 1 || (dri2_dpy
->dri3_major_version
== 1 &&
1128 dri2_dpy
->dri3_minor_version
>= 2)) &&
1129 (dri2_dpy
->present_major_version
> 1 || (dri2_dpy
->present_major_version
== 1 &&
1130 dri2_dpy
->present_minor_version
>= 2)) &&
1131 (dri2_dpy
->image
&& dri2_dpy
->image
->base
.version
>= 15);
1134 dri2_bind_extensions(dri2_dpy
, optional_core_extensions
, extensions
, true);
1139 * Called via eglInitialize(), drv->Initialize().
1141 * This must be guaranteed to be called exactly once, even if eglInitialize is
1142 * called many times (without a eglTerminate in between).
1145 dri2_initialize(_EGLDisplay
*disp
)
1147 EGLBoolean ret
= EGL_FALSE
;
1148 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1150 /* In the case where the application calls eglMakeCurrent(context1),
1151 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
1152 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
1153 * initialized, as we need it to be able to free context1 correctly.
1155 * It would probably be safest to forcibly release the display with
1156 * dri2_display_release, to make sure the display is reinitialized correctly.
1157 * However, the EGL spec states that we need to keep a reference to the
1158 * current context (so we cannot call dri2_make_current(NULL)), and therefore
1159 * we would leak context1 as we would be missing the old display connection
1160 * to free it up correctly.
1163 dri2_dpy
->ref_count
++;
1167 loader_set_logger(_eglLog
);
1169 switch (disp
->Platform
) {
1170 case _EGL_PLATFORM_SURFACELESS
:
1171 ret
= dri2_initialize_surfaceless(disp
);
1173 case _EGL_PLATFORM_DEVICE
:
1174 ret
= dri2_initialize_device(disp
);
1176 case _EGL_PLATFORM_X11
:
1177 ret
= dri2_initialize_x11(disp
);
1179 case _EGL_PLATFORM_DRM
:
1180 ret
= dri2_initialize_drm(disp
);
1182 case _EGL_PLATFORM_WAYLAND
:
1183 ret
= dri2_initialize_wayland(disp
);
1185 case _EGL_PLATFORM_ANDROID
:
1186 ret
= dri2_initialize_android(disp
);
1189 unreachable("Callers ensure we cannot get here.");
1196 dri2_dpy
= dri2_egl_display(disp
);
1197 dri2_dpy
->ref_count
++;
1203 * Decrement display reference count, and free up display if necessary.
1206 dri2_display_release(_EGLDisplay
*disp
)
1208 struct dri2_egl_display
*dri2_dpy
;
1213 dri2_dpy
= dri2_egl_display(disp
);
1215 assert(dri2_dpy
->ref_count
> 0);
1216 dri2_dpy
->ref_count
--;
1218 if (dri2_dpy
->ref_count
> 0)
1221 _eglCleanupDisplay(disp
);
1222 dri2_display_destroy(disp
);
1226 dri2_display_destroy(_EGLDisplay
*disp
)
1228 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1230 if (dri2_dpy
->own_dri_screen
) {
1231 if (dri2_dpy
->vtbl
&& dri2_dpy
->vtbl
->close_screen_notify
)
1232 dri2_dpy
->vtbl
->close_screen_notify(disp
);
1233 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
1235 if (dri2_dpy
->fd
>= 0)
1236 close(dri2_dpy
->fd
);
1237 if (dri2_dpy
->driver
)
1238 dlclose(dri2_dpy
->driver
);
1239 free(dri2_dpy
->driver_name
);
1241 #ifdef HAVE_WAYLAND_PLATFORM
1242 free(dri2_dpy
->device_name
);
1245 switch (disp
->Platform
) {
1246 case _EGL_PLATFORM_X11
:
1247 dri2_teardown_x11(dri2_dpy
);
1249 case _EGL_PLATFORM_DRM
:
1250 dri2_teardown_drm(dri2_dpy
);
1252 case _EGL_PLATFORM_WAYLAND
:
1253 dri2_teardown_wayland(dri2_dpy
);
1256 /* TODO: add teardown for other platforms */
1260 /* The drm platform does not create the screen/driver_configs but reuses
1261 * the ones from the gbm device. As such the gbm itself is responsible
1264 if (disp
->Platform
!= _EGL_PLATFORM_DRM
&& dri2_dpy
->driver_configs
) {
1265 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
1266 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
1267 free(dri2_dpy
->driver_configs
);
1270 disp
->DriverData
= NULL
;
1274 dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface
*dri2_surf
,
1275 unsigned int att
, unsigned int format
)
1277 struct dri2_egl_display
*dri2_dpy
=
1278 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1280 if (att
>= ARRAY_SIZE(dri2_surf
->local_buffers
))
1283 if (!dri2_surf
->local_buffers
[att
]) {
1284 dri2_surf
->local_buffers
[att
] =
1285 dri2_dpy
->dri2
->allocateBuffer(dri2_dpy
->dri_screen
, att
, format
,
1286 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
);
1289 return dri2_surf
->local_buffers
[att
];
1293 dri2_egl_surface_free_local_buffers(struct dri2_egl_surface
*dri2_surf
)
1295 struct dri2_egl_display
*dri2_dpy
=
1296 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1298 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->local_buffers
); i
++) {
1299 if (dri2_surf
->local_buffers
[i
]) {
1300 dri2_dpy
->dri2
->releaseBuffer(dri2_dpy
->dri_screen
,
1301 dri2_surf
->local_buffers
[i
]);
1302 dri2_surf
->local_buffers
[i
] = NULL
;
1308 * Called via eglTerminate(), drv->Terminate().
1310 * This must be guaranteed to be called exactly once, even if eglTerminate is
1311 * called many times (without a eglInitialize in between).
1314 dri2_terminate(_EGLDisplay
*disp
)
1316 /* Release all non-current Context/Surfaces. */
1317 _eglReleaseDisplayResources(disp
);
1319 dri2_display_release(disp
);
1325 * Set the error code after a call to
1326 * dri2_egl_display::dri2::createContextAttribs.
1329 dri2_create_context_attribs_error(int dri_error
)
1333 switch (dri_error
) {
1334 case __DRI_CTX_ERROR_SUCCESS
:
1337 case __DRI_CTX_ERROR_NO_MEMORY
:
1338 egl_error
= EGL_BAD_ALLOC
;
1341 /* From the EGL_KHR_create_context spec, section "Errors":
1343 * * If <config> does not support a client API context compatible
1344 * with the requested API major and minor version, [...] context flags,
1345 * and context reset notification behavior (for client API types where
1346 * these attributes are supported), then an EGL_BAD_MATCH error is
1349 * * If an OpenGL ES context is requested and the values for
1350 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1351 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1352 * is not defined, than an EGL_BAD_MATCH error is generated.
1354 * * If an OpenGL context is requested, the requested version is
1355 * greater than 3.2, and the value for attribute
1356 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1357 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1358 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1359 * one of these bits set; or if the implementation does not support
1360 * the requested profile, then an EGL_BAD_MATCH error is generated.
1362 case __DRI_CTX_ERROR_BAD_API
:
1363 case __DRI_CTX_ERROR_BAD_VERSION
:
1364 case __DRI_CTX_ERROR_BAD_FLAG
:
1365 egl_error
= EGL_BAD_MATCH
;
1368 /* From the EGL_KHR_create_context spec, section "Errors":
1370 * * If an attribute name or attribute value in <attrib_list> is not
1371 * recognized (including unrecognized bits in bitmask attributes),
1372 * then an EGL_BAD_ATTRIBUTE error is generated."
1374 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
1375 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
1376 egl_error
= EGL_BAD_ATTRIBUTE
;
1380 assert(!"unknown dri_error code");
1381 egl_error
= EGL_BAD_MATCH
;
1385 _eglError(egl_error
, "dri2_create_context");
1389 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1390 struct dri2_egl_display
*dri2_dpy
,
1391 uint32_t *ctx_attribs
,
1392 unsigned *num_attribs
)
1396 assert(*num_attribs
>= NUM_ATTRIBS
);
1398 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1399 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1400 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1401 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1403 if (dri2_ctx
->base
.Flags
!= 0 || dri2_ctx
->base
.NoError
) {
1404 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1405 * extension, don't even try to send it the robust-access flag.
1406 * It may explode. Instead, generate the required EGL error here.
1408 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1409 && !dri2_dpy
->robustness
) {
1410 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1414 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1415 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
|
1416 (dri2_ctx
->base
.NoError
? __DRI_CTX_FLAG_NO_ERROR
: 0);
1419 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1420 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1421 * extension, don't even try to send it a reset strategy. It may
1422 * explode. Instead, generate the required EGL error here.
1424 if (!dri2_dpy
->robustness
) {
1425 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1429 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1430 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1433 if (dri2_ctx
->base
.ContextPriority
!= EGL_CONTEXT_PRIORITY_MEDIUM_IMG
) {
1436 switch (dri2_ctx
->base
.ContextPriority
) {
1437 case EGL_CONTEXT_PRIORITY_HIGH_IMG
:
1438 val
= __DRI_CTX_PRIORITY_HIGH
;
1440 case EGL_CONTEXT_PRIORITY_MEDIUM_IMG
:
1441 val
= __DRI_CTX_PRIORITY_MEDIUM
;
1443 case EGL_CONTEXT_PRIORITY_LOW_IMG
:
1444 val
= __DRI_CTX_PRIORITY_LOW
;
1447 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1451 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_PRIORITY
;
1452 ctx_attribs
[pos
++] = val
;
1455 if (dri2_ctx
->base
.ReleaseBehavior
== EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR
) {
1456 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RELEASE_BEHAVIOR
;
1457 ctx_attribs
[pos
++] = __DRI_CTX_RELEASE_BEHAVIOR_NONE
;
1466 * Called via eglCreateContext(), drv->CreateContext().
1468 static _EGLContext
*
1469 dri2_create_context(_EGLDisplay
*disp
, _EGLConfig
*conf
,
1470 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1472 struct dri2_egl_context
*dri2_ctx
;
1473 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1474 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1475 __DRIcontext
*shared
=
1476 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1477 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1478 const __DRIconfig
*dri_config
;
1481 unsigned num_attribs
= NUM_ATTRIBS
;
1482 uint32_t ctx_attribs
[NUM_ATTRIBS
];
1484 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1486 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1490 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1493 /* The EGL_EXT_create_context_robustness spec says:
1495 * "Add to the eglCreateContext context creation errors: [...]
1497 * * If the reset notification behavior of <share_context> and the
1498 * newly created context are different then an EGL_BAD_MATCH error is
1501 if (share_list
&& share_list
->ResetNotificationStrategy
!=
1502 dri2_ctx
->base
.ResetNotificationStrategy
) {
1503 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1507 /* The EGL_KHR_create_context_no_error spec says:
1509 * "BAD_MATCH is generated if the value of EGL_CONTEXT_OPENGL_NO_ERROR_KHR
1510 * used to create <share_context> does not match the value of
1511 * EGL_CONTEXT_OPENGL_NO_ERROR_KHR for the context being created."
1513 if (share_list
&& share_list
->NoError
!= dri2_ctx
->base
.NoError
) {
1514 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1518 switch (dri2_ctx
->base
.ClientAPI
) {
1519 case EGL_OPENGL_ES_API
:
1520 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1522 api
= __DRI_API_GLES
;
1525 api
= __DRI_API_GLES2
;
1528 api
= __DRI_API_GLES3
;
1531 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1536 case EGL_OPENGL_API
:
1537 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1538 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1539 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1540 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1541 api
= __DRI_API_OPENGL_CORE
;
1542 else if (dri2_ctx
->base
.ClientMajorVersion
== 3 &&
1543 dri2_ctx
->base
.ClientMinorVersion
== 1)
1544 api
= __DRI_API_OPENGL_CORE
;
1546 api
= __DRI_API_OPENGL
;
1549 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1555 /* The config chosen here isn't necessarily
1556 * used for surfaces later.
1557 * A pixmap surface will use the single config.
1558 * This opportunity depends on disabling the
1559 * doubleBufferMode check in
1560 * src/mesa/main/context.c:check_compatible()
1562 if (dri2_config
->dri_config
[1][0])
1563 dri_config
= dri2_config
->dri_config
[1][0];
1565 dri_config
= dri2_config
->dri_config
[0][0];
1570 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1574 if (dri2_dpy
->image_driver
) {
1575 dri2_ctx
->dri_context
=
1576 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1584 dri2_create_context_attribs_error(error
);
1585 } else if (dri2_dpy
->dri2
) {
1586 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1587 dri2_ctx
->dri_context
=
1588 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1596 dri2_create_context_attribs_error(error
);
1598 dri2_ctx
->dri_context
=
1599 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1606 assert(dri2_dpy
->swrast
);
1607 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1608 dri2_ctx
->dri_context
=
1609 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1617 dri2_create_context_attribs_error(error
);
1619 dri2_ctx
->dri_context
=
1620 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1628 if (!dri2_ctx
->dri_context
)
1631 return &dri2_ctx
->base
;
1639 * Called via eglDestroyContext(), drv->DestroyContext().
1642 dri2_destroy_context(_EGLDisplay
*disp
, _EGLContext
*ctx
)
1644 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1645 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1647 if (_eglPutContext(ctx
)) {
1648 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1656 dri2_init_surface(_EGLSurface
*surf
, _EGLDisplay
*disp
, EGLint type
,
1657 _EGLConfig
*conf
, const EGLint
*attrib_list
,
1658 EGLBoolean enable_out_fence
, void *native_surface
)
1660 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1661 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1663 dri2_surf
->out_fence_fd
= -1;
1664 dri2_surf
->enable_out_fence
= false;
1665 if (dri2_dpy
->fence
&& dri2_dpy
->fence
->base
.version
>= 2 &&
1666 dri2_dpy
->fence
->get_capabilities
&&
1667 (dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
) &
1668 __DRI_FENCE_CAP_NATIVE_FD
)) {
1669 dri2_surf
->enable_out_fence
= enable_out_fence
;
1672 return _eglInitSurface(surf
, disp
, type
, conf
, attrib_list
, native_surface
);
1676 dri2_surface_set_out_fence_fd( _EGLSurface
*surf
, int fence_fd
)
1678 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1680 if (dri2_surf
->out_fence_fd
>= 0)
1681 close(dri2_surf
->out_fence_fd
);
1683 dri2_surf
->out_fence_fd
= fence_fd
;
1687 dri2_fini_surface(_EGLSurface
*surf
)
1689 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1691 dri2_surface_set_out_fence_fd(surf
, -1);
1692 dri2_surf
->enable_out_fence
= false;
1696 dri2_destroy_surface(_EGLDisplay
*disp
, _EGLSurface
*surf
)
1698 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1700 if (!_eglPutSurface(surf
))
1703 return dri2_dpy
->vtbl
->destroy_surface(disp
, surf
);
1707 dri2_surf_update_fence_fd(_EGLContext
*ctx
,
1708 _EGLDisplay
*disp
, _EGLSurface
*surf
)
1710 __DRIcontext
*dri_ctx
= dri2_egl_context(ctx
)->dri_context
;
1711 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1712 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1716 if (!dri2_surf
->enable_out_fence
)
1719 fence
= dri2_dpy
->fence
->create_fence_fd(dri_ctx
, -1);
1721 fence_fd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
1723 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, fence
);
1725 dri2_surface_set_out_fence_fd(surf
, fence_fd
);
1729 dri2_create_drawable(struct dri2_egl_display
*dri2_dpy
,
1730 const __DRIconfig
*config
,
1731 struct dri2_egl_surface
*dri2_surf
,
1732 void *loaderPrivate
)
1734 __DRIcreateNewDrawableFunc createNewDrawable
;
1736 if (dri2_dpy
->image_driver
)
1737 createNewDrawable
= dri2_dpy
->image_driver
->createNewDrawable
;
1738 else if (dri2_dpy
->dri2
)
1739 createNewDrawable
= dri2_dpy
->dri2
->createNewDrawable
;
1740 else if (dri2_dpy
->swrast
)
1741 createNewDrawable
= dri2_dpy
->swrast
->createNewDrawable
;
1743 return _eglError(EGL_BAD_ALLOC
, "no createNewDrawable");
1745 dri2_surf
->dri_drawable
= createNewDrawable(dri2_dpy
->dri_screen
,
1746 config
, loaderPrivate
);
1747 if (dri2_surf
->dri_drawable
== NULL
)
1748 return _eglError(EGL_BAD_ALLOC
, "createNewDrawable");
1754 * Called via eglMakeCurrent(), drv->MakeCurrent().
1757 dri2_make_current(_EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1758 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1760 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1761 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1762 _EGLDisplay
*old_disp
= NULL
;
1763 struct dri2_egl_display
*old_dri2_dpy
= NULL
;
1764 _EGLContext
*old_ctx
;
1765 _EGLSurface
*old_dsurf
, *old_rsurf
;
1766 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1767 __DRIdrawable
*ddraw
, *rdraw
;
1769 EGLint egl_error
= EGL_SUCCESS
;
1772 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1774 /* make new bindings, set the EGL error otherwise */
1775 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
))
1779 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1780 old_disp
= old_ctx
->Resource
.Display
;
1781 old_dri2_dpy
= dri2_egl_display(old_disp
);
1783 /* flush before context switch */
1787 dri2_surf_update_fence_fd(old_ctx
, disp
, old_dsurf
);
1789 /* Disable shared buffer mode */
1790 if (old_dsurf
&& _eglSurfaceInSharedBufferMode(old_dsurf
) &&
1791 old_dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1792 old_dri2_dpy
->vtbl
->set_shared_buffer_mode(old_disp
, old_dsurf
, false);
1795 dri2_dpy
->core
->unbindContext(old_cctx
);
1798 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1799 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1800 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1802 if (cctx
|| ddraw
|| rdraw
) {
1803 if (!dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1804 _EGLContext
*tmp_ctx
;
1806 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1807 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1810 egl_error
= EGL_BAD_MATCH
;
1812 /* undo the previous _eglBindContext */
1813 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1814 assert(&dri2_ctx
->base
== ctx
&&
1815 tmp_dsurf
== dsurf
&&
1816 tmp_rsurf
== rsurf
);
1818 _eglPutSurface(dsurf
);
1819 _eglPutSurface(rsurf
);
1820 _eglPutContext(ctx
);
1822 _eglPutSurface(old_dsurf
);
1823 _eglPutSurface(old_rsurf
);
1824 _eglPutContext(old_ctx
);
1826 ddraw
= (old_dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(old_dsurf
) : NULL
;
1827 rdraw
= (old_rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(old_rsurf
) : NULL
;
1828 cctx
= (old_ctx
) ? dri2_egl_context(old_ctx
)->dri_context
: NULL
;
1830 /* undo the previous dri2_dpy->core->unbindContext */
1831 if (dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1832 if (old_dsurf
&& _eglSurfaceInSharedBufferMode(old_dsurf
) &&
1833 old_dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1834 old_dri2_dpy
->vtbl
->set_shared_buffer_mode(old_disp
, old_dsurf
, true);
1837 return _eglError(egl_error
, "eglMakeCurrent");
1840 /* We cannot restore the same state as it was before calling
1841 * eglMakeCurrent() and the spec isn't clear about what to do. We
1842 * can prevent EGL from calling into the DRI driver with no DRI
1845 dsurf
= rsurf
= NULL
;
1848 _eglBindContext(ctx
, dsurf
, rsurf
, &tmp_ctx
, &tmp_dsurf
, &tmp_rsurf
);
1849 assert(tmp_ctx
== old_ctx
&& tmp_dsurf
== old_dsurf
&&
1850 tmp_rsurf
== old_rsurf
);
1852 _eglLog(_EGL_WARNING
, "DRI2: failed to rebind the previous context");
1854 /* dri2_dpy->core->bindContext succeeded, so take a reference on the
1855 * dri2_dpy. This prevents dri2_dpy from being reinitialized when a
1856 * EGLDisplay is terminated and then initialized again while a
1857 * context is still bound. See dri2_intitialize() for a more in depth
1859 dri2_dpy
->ref_count
++;
1863 dri2_destroy_surface(disp
, old_dsurf
);
1864 dri2_destroy_surface(disp
, old_rsurf
);
1867 dri2_destroy_context(disp
, old_ctx
);
1868 dri2_display_release(old_disp
);
1871 if (egl_error
!= EGL_SUCCESS
)
1872 return _eglError(egl_error
, "eglMakeCurrent");
1874 if (dsurf
&& _eglSurfaceHasMutableRenderBuffer(dsurf
) &&
1875 dri2_dpy
->vtbl
->set_shared_buffer_mode
) {
1876 /* Always update the shared buffer mode. This is obviously needed when
1877 * the active EGL_RENDER_BUFFER is EGL_SINGLE_BUFFER. When
1878 * EGL_RENDER_BUFFER is EGL_BACK_BUFFER, the update protects us in the
1879 * case where external non-EGL API may have changed window's shared
1880 * buffer mode since we last saw it.
1882 bool mode
= (dsurf
->ActiveRenderBuffer
== EGL_SINGLE_BUFFER
);
1883 dri2_dpy
->vtbl
->set_shared_buffer_mode(disp
, dsurf
, mode
);
1890 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1892 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1894 return dri2_surf
->dri_drawable
;
1898 * Called from eglGetProcAddress() via drv->GetProcAddress().
1901 dri2_get_proc_address(const _EGLDriver
*drv
, const char *procname
)
1903 return _glapi_get_proc_address(procname
);
1907 dri2_create_window_surface(_EGLDisplay
*disp
, _EGLConfig
*conf
,
1908 void *native_window
, const EGLint
*attrib_list
)
1910 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1911 return dri2_dpy
->vtbl
->create_window_surface(disp
, conf
, native_window
,
1916 dri2_create_pixmap_surface(_EGLDisplay
*disp
, _EGLConfig
*conf
,
1917 void *native_pixmap
, const EGLint
*attrib_list
)
1919 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1920 if (!dri2_dpy
->vtbl
->create_pixmap_surface
)
1922 return dri2_dpy
->vtbl
->create_pixmap_surface(disp
, conf
, native_pixmap
,
1927 dri2_create_pbuffer_surface(_EGLDisplay
*disp
, _EGLConfig
*conf
,
1928 const EGLint
*attrib_list
)
1930 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1931 if (!dri2_dpy
->vtbl
->create_pbuffer_surface
)
1933 return dri2_dpy
->vtbl
->create_pbuffer_surface(disp
, conf
, attrib_list
);
1937 dri2_swap_interval(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
1940 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1941 if (!dri2_dpy
->vtbl
->swap_interval
)
1943 return dri2_dpy
->vtbl
->swap_interval(drv
, disp
, surf
, interval
);
1947 * Asks the client API to flush any rendering to the drawable so that we can
1948 * do our swapbuffers.
1951 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1953 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1954 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1956 if (dri2_dpy
->flush
) {
1957 if (dri2_dpy
->flush
->base
.version
>= 4) {
1958 /* We know there's a current context because:
1960 * "If surface is not bound to the calling thread’s current
1961 * context, an EGL_BAD_SURFACE error is generated."
1963 _EGLContext
*ctx
= _eglGetCurrentContext();
1964 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1966 /* From the EGL 1.4 spec (page 52):
1968 * "The contents of ancillary buffers are always undefined
1969 * after calling eglSwapBuffers."
1971 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1973 __DRI2_FLUSH_DRAWABLE
|
1974 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1975 __DRI2_THROTTLE_SWAPBUFFER
);
1977 dri2_dpy
->flush
->flush(dri_drawable
);
1983 dri2_swap_buffers(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
1985 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1986 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1987 _EGLContext
*ctx
= _eglGetCurrentContext();
1991 dri2_surf_update_fence_fd(ctx
, disp
, surf
);
1992 ret
= dri2_dpy
->vtbl
->swap_buffers(drv
, disp
, surf
);
1994 /* SwapBuffers marks the end of the frame; reset the damage region for
1995 * use again next time.
1997 if (ret
&& dri2_dpy
->buffer_damage
&&
1998 dri2_dpy
->buffer_damage
->set_damage_region
)
1999 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
2005 dri2_swap_buffers_with_damage(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
2007 const EGLint
*rects
, EGLint n_rects
)
2009 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2010 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
2011 _EGLContext
*ctx
= _eglGetCurrentContext();
2015 dri2_surf_update_fence_fd(ctx
, disp
, surf
);
2016 if (dri2_dpy
->vtbl
->swap_buffers_with_damage
)
2017 ret
= dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, disp
, surf
,
2020 ret
= dri2_dpy
->vtbl
->swap_buffers(drv
, disp
, surf
);
2022 /* SwapBuffers marks the end of the frame; reset the damage region for
2023 * use again next time.
2025 if (ret
&& dri2_dpy
->buffer_damage
&&
2026 dri2_dpy
->buffer_damage
->set_damage_region
)
2027 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
2033 dri2_swap_buffers_region(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
2034 EGLint numRects
, const EGLint
*rects
)
2036 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2037 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
2040 if (!dri2_dpy
->vtbl
->swap_buffers_region
)
2042 ret
= dri2_dpy
->vtbl
->swap_buffers_region(drv
, disp
, surf
, numRects
, rects
);
2044 /* SwapBuffers marks the end of the frame; reset the damage region for
2045 * use again next time.
2047 if (ret
&& dri2_dpy
->buffer_damage
&&
2048 dri2_dpy
->buffer_damage
->set_damage_region
)
2049 dri2_dpy
->buffer_damage
->set_damage_region(dri_drawable
, 0, NULL
);
2055 dri2_set_damage_region(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
2056 EGLint
*rects
, EGLint n_rects
)
2058 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2059 __DRIdrawable
*drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
2061 if (!dri2_dpy
->buffer_damage
|| !dri2_dpy
->buffer_damage
->set_damage_region
)
2064 dri2_dpy
->buffer_damage
->set_damage_region(drawable
, n_rects
, rects
);
2069 dri2_post_sub_buffer(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
2070 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
2072 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2073 if (!dri2_dpy
->vtbl
->post_sub_buffer
)
2075 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, disp
, surf
, x
, y
, width
, height
);
2079 dri2_copy_buffers(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
,
2080 void *native_pixmap_target
)
2082 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2083 if (!dri2_dpy
->vtbl
->copy_buffers
)
2084 return _eglError(EGL_BAD_NATIVE_PIXMAP
, "no support for native pixmaps");
2085 return dri2_dpy
->vtbl
->copy_buffers(drv
, disp
, surf
, native_pixmap_target
);
2089 dri2_query_buffer_age(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
2091 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2092 if (!dri2_dpy
->vtbl
->query_buffer_age
)
2094 return dri2_dpy
->vtbl
->query_buffer_age(drv
, disp
, surf
);
2098 dri2_wait_client(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
2100 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2101 _EGLSurface
*surf
= ctx
->DrawSurface
;
2102 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
2106 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
2107 * we need to copy fake to real here.*/
2109 if (dri2_dpy
->flush
!= NULL
)
2110 dri2_dpy
->flush
->flush(dri_drawable
);
2116 dri2_wait_native(const _EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
2121 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
2122 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
2129 dri2_bind_tex_image(_EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
2131 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2132 struct dri2_egl_context
*dri2_ctx
;
2134 GLint format
, target
;
2135 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
2137 ctx
= _eglGetCurrentContext();
2138 dri2_ctx
= dri2_egl_context(ctx
);
2140 if (!_eglBindTexImage(disp
, surf
, buffer
))
2143 switch (surf
->TextureFormat
) {
2144 case EGL_TEXTURE_RGB
:
2145 format
= __DRI_TEXTURE_FORMAT_RGB
;
2147 case EGL_TEXTURE_RGBA
:
2148 format
= __DRI_TEXTURE_FORMAT_RGBA
;
2151 assert(!"Unexpected texture format in dri2_bind_tex_image()");
2152 format
= __DRI_TEXTURE_FORMAT_RGBA
;
2155 switch (surf
->TextureTarget
) {
2156 case EGL_TEXTURE_2D
:
2157 target
= GL_TEXTURE_2D
;
2160 target
= GL_TEXTURE_2D
;
2161 assert(!"Unexpected texture target in dri2_bind_tex_image()");
2164 dri2_dpy
->tex_buffer
->setTexBuffer2(dri2_ctx
->dri_context
,
2172 dri2_release_tex_image(_EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
2174 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2175 struct dri2_egl_context
*dri2_ctx
;
2178 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
2180 ctx
= _eglGetCurrentContext();
2181 dri2_ctx
= dri2_egl_context(ctx
);
2183 if (!_eglReleaseTexImage(disp
, surf
, buffer
))
2186 switch (surf
->TextureTarget
) {
2187 case EGL_TEXTURE_2D
:
2188 target
= GL_TEXTURE_2D
;
2191 assert(!"missing texture target");
2194 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
2195 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
2196 dri2_dpy
->tex_buffer
->releaseTexBuffer(dri2_ctx
->dri_context
,
2197 target
, dri_drawable
);
2204 dri2_create_image(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
,
2205 EGLenum target
, EGLClientBuffer buffer
,
2206 const EGLint
*attr_list
)
2208 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2209 return dri2_dpy
->vtbl
->create_image(drv
, disp
, ctx
, target
, buffer
,
2214 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
2216 struct dri2_egl_image
*dri2_img
;
2218 if (dri_image
== NULL
) {
2219 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
2223 dri2_img
= malloc(sizeof *dri2_img
);
2225 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
2229 _eglInitImage(&dri2_img
->base
, disp
);
2231 dri2_img
->dri_image
= dri_image
;
2233 return &dri2_img
->base
;
2237 * Translate a DRI Image extension error code into an EGL error code.
2240 egl_error_from_dri_image_error(int dri_error
)
2242 switch (dri_error
) {
2243 case __DRI_IMAGE_ERROR_SUCCESS
:
2245 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
2246 return EGL_BAD_ALLOC
;
2247 case __DRI_IMAGE_ERROR_BAD_MATCH
:
2248 return EGL_BAD_MATCH
;
2249 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
2250 return EGL_BAD_PARAMETER
;
2251 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
2252 return EGL_BAD_ACCESS
;
2254 assert(!"unknown dri_error code");
2255 return EGL_BAD_ALLOC
;
2260 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2261 EGLClientBuffer buffer
,
2262 const EGLint
*attr_list
)
2264 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2265 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2266 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
2267 __DRIimage
*dri_image
;
2269 if (renderbuffer
== 0) {
2270 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2271 return EGL_NO_IMAGE_KHR
;
2274 if (!disp
->Extensions
.KHR_gl_renderbuffer_image
) {
2275 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2276 return EGL_NO_IMAGE_KHR
;
2279 if (dri2_dpy
->image
->base
.version
>= 17 &&
2280 dri2_dpy
->image
->createImageFromRenderbuffer2
) {
2281 unsigned error
= ~0;
2283 dri_image
= dri2_dpy
->image
->createImageFromRenderbuffer2(
2284 dri2_ctx
->dri_context
, renderbuffer
, NULL
, &error
);
2286 assert(!!dri_image
== (error
== __DRI_IMAGE_ERROR_SUCCESS
));
2289 _eglError(egl_error_from_dri_image_error(error
), "dri2_create_image_khr");
2290 return EGL_NO_IMAGE_KHR
;
2293 dri_image
= dri2_dpy
->image
->createImageFromRenderbuffer(
2294 dri2_ctx
->dri_context
, renderbuffer
, NULL
);
2296 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2297 return EGL_NO_IMAGE_KHR
;
2301 return dri2_create_image_from_dri(disp
, dri_image
);
2304 #ifdef HAVE_WAYLAND_PLATFORM
2306 /* This structure describes how a wl_buffer maps to one or more
2307 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
2308 * offsets and strides of the planes in the buffer. This table maps a
2309 * wl_drm format code to a description of the planes in the buffer
2310 * that lets us create a __DRIimage for each of the planes. */
2312 static const struct wl_drm_components_descriptor
{
2313 uint32_t dri_components
;
2316 } wl_drm_components
[] = {
2317 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
2318 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
2319 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
2320 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
2321 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
2325 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2326 EGLClientBuffer _buffer
,
2327 const EGLint
*attr_list
)
2329 struct wl_drm_buffer
*buffer
;
2330 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2331 const struct wl_drm_components_descriptor
*f
;
2332 __DRIimage
*dri_image
;
2333 _EGLImageAttribs attrs
;
2336 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
2337 (struct wl_resource
*) _buffer
);
2341 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2344 plane
= attrs
.PlaneWL
;
2345 f
= buffer
->driver_format
;
2346 if (plane
< 0 || plane
>= f
->nplanes
) {
2347 _eglError(EGL_BAD_PARAMETER
,
2348 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
2352 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
2353 if (dri_image
== NULL
&& plane
== 0)
2354 dri_image
= dri2_dpy
->image
->dupImage(buffer
->driver_buffer
, NULL
);
2355 if (dri_image
== NULL
) {
2356 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
2360 return dri2_create_image_from_dri(disp
, dri_image
);
2365 dri2_get_sync_values_chromium(_EGLDisplay
*disp
, _EGLSurface
*surf
,
2366 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
2369 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2370 if (!dri2_dpy
->vtbl
->get_sync_values
)
2372 return dri2_dpy
->vtbl
->get_sync_values(disp
, surf
, ust
, msc
, sbc
);
2376 * Set the error code after a call to
2377 * dri2_egl_image::dri_image::createImageFromTexture.
2380 dri2_create_image_khr_texture_error(int dri_error
)
2382 EGLint egl_error
= egl_error_from_dri_image_error(dri_error
);
2384 if (egl_error
!= EGL_SUCCESS
)
2385 _eglError(egl_error
, "dri2_create_image_khr_texture");
2389 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2391 EGLClientBuffer buffer
,
2392 const EGLint
*attr_list
)
2394 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2395 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2396 struct dri2_egl_image
*dri2_img
;
2397 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
2398 _EGLImageAttribs attrs
;
2404 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2405 return EGL_NO_IMAGE_KHR
;
2408 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2409 return EGL_NO_IMAGE_KHR
;
2412 case EGL_GL_TEXTURE_2D_KHR
:
2413 if (!disp
->Extensions
.KHR_gl_texture_2D_image
) {
2414 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2415 return EGL_NO_IMAGE_KHR
;
2418 gl_target
= GL_TEXTURE_2D
;
2420 case EGL_GL_TEXTURE_3D_KHR
:
2421 if (!disp
->Extensions
.KHR_gl_texture_3D_image
) {
2422 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2423 return EGL_NO_IMAGE_KHR
;
2426 depth
= attrs
.GLTextureZOffset
;
2427 gl_target
= GL_TEXTURE_3D
;
2429 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2430 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2431 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2432 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2433 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2434 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2435 if (!disp
->Extensions
.KHR_gl_texture_cubemap_image
) {
2436 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2437 return EGL_NO_IMAGE_KHR
;
2440 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
2441 gl_target
= GL_TEXTURE_CUBE_MAP
;
2444 unreachable("Unexpected target in dri2_create_image_khr_texture()");
2445 return EGL_NO_IMAGE_KHR
;
2448 dri2_img
= malloc(sizeof *dri2_img
);
2450 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2451 return EGL_NO_IMAGE_KHR
;
2454 _eglInitImage(&dri2_img
->base
, disp
);
2456 dri2_img
->dri_image
=
2457 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
2461 attrs
.GLTextureLevel
,
2464 dri2_create_image_khr_texture_error(error
);
2466 if (!dri2_img
->dri_image
) {
2468 return EGL_NO_IMAGE_KHR
;
2470 return &dri2_img
->base
;
2474 dri2_query_surface(_EGLDisplay
*disp
, _EGLSurface
*surf
,
2475 EGLint attribute
, EGLint
*value
)
2477 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2478 if (!dri2_dpy
->vtbl
->query_surface
)
2479 return _eglQuerySurface(disp
, surf
, attribute
, value
);
2480 return dri2_dpy
->vtbl
->query_surface(disp
, surf
, attribute
, value
);
2483 static struct wl_buffer
*
2484 dri2_create_wayland_buffer_from_image(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
2487 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2488 if (!dri2_dpy
->vtbl
->create_wayland_buffer_from_image
)
2490 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, disp
, img
);
2495 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2496 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2498 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2499 EGLint format
, name
, pitch
;
2500 _EGLImageAttribs attrs
;
2501 __DRIimage
*dri_image
;
2503 name
= (EGLint
) (uintptr_t) buffer
;
2505 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2508 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
2509 attrs
.DRMBufferStrideMESA
<= 0) {
2510 _eglError(EGL_BAD_PARAMETER
,
2511 "bad width, height or stride");
2515 switch (attrs
.DRMBufferFormatMESA
) {
2516 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2517 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2518 pitch
= attrs
.DRMBufferStrideMESA
;
2521 _eglError(EGL_BAD_PARAMETER
,
2522 "dri2_create_image_khr: unsupported pixmap depth");
2527 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
2535 return dri2_create_image_from_dri(disp
, dri_image
);
2539 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
2544 * "Required attributes and their values are as follows:
2546 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
2548 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
2549 * by drm_fourcc.h and used as the pixel_format parameter of the
2550 * drm_mode_fb_cmd2 ioctl."
2554 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2555 * incomplete, EGL_BAD_PARAMETER is generated."
2557 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
2558 !attrs
->DMABufFourCC
.IsPresent
)
2559 return _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
2564 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
2565 * specified for a plane's pitch or offset isn't supported by EGL,
2566 * EGL_BAD_ACCESS is generated."
2568 for (unsigned i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
2569 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
2570 attrs
->DMABufPlanePitches
[i
].Value
<= 0)
2571 return _eglError(EGL_BAD_ACCESS
, "invalid pitch");
2575 * If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2576 * attribute values may be given.
2578 * This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2579 * EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2581 for (unsigned i
= 0; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2582 if (attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
!=
2583 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
)
2584 return _eglError(EGL_BAD_PARAMETER
, "modifier attribute lo or hi missing");
2587 /* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2588 * mandate it, we only accept the same modifier across all planes. */
2589 for (unsigned i
= 1; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2590 if (attrs
->DMABufPlaneFds
[i
].IsPresent
) {
2591 if ((attrs
->DMABufPlaneModifiersLo
[0].IsPresent
!=
2592 attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
) ||
2593 (attrs
->DMABufPlaneModifiersLo
[0].Value
!=
2594 attrs
->DMABufPlaneModifiersLo
[i
].Value
) ||
2595 (attrs
->DMABufPlaneModifiersHi
[0].Value
!=
2596 attrs
->DMABufPlaneModifiersHi
[i
].Value
))
2597 return _eglError(EGL_BAD_PARAMETER
, "modifier attributes not equal");
2604 /* Returns the total number of planes for the format or zero if it isn't a
2605 * valid fourcc format.
2608 dri2_num_fourcc_format_planes(EGLint format
)
2612 case DRM_FORMAT_RG88
:
2613 case DRM_FORMAT_GR88
:
2614 case DRM_FORMAT_R16
:
2615 case DRM_FORMAT_GR1616
:
2616 case DRM_FORMAT_RGB332
:
2617 case DRM_FORMAT_BGR233
:
2618 case DRM_FORMAT_XRGB4444
:
2619 case DRM_FORMAT_XBGR4444
:
2620 case DRM_FORMAT_RGBX4444
:
2621 case DRM_FORMAT_BGRX4444
:
2622 case DRM_FORMAT_ARGB4444
:
2623 case DRM_FORMAT_ABGR4444
:
2624 case DRM_FORMAT_RGBA4444
:
2625 case DRM_FORMAT_BGRA4444
:
2626 case DRM_FORMAT_XRGB1555
:
2627 case DRM_FORMAT_XBGR1555
:
2628 case DRM_FORMAT_RGBX5551
:
2629 case DRM_FORMAT_BGRX5551
:
2630 case DRM_FORMAT_ARGB1555
:
2631 case DRM_FORMAT_ABGR1555
:
2632 case DRM_FORMAT_RGBA5551
:
2633 case DRM_FORMAT_BGRA5551
:
2634 case DRM_FORMAT_RGB565
:
2635 case DRM_FORMAT_BGR565
:
2636 case DRM_FORMAT_RGB888
:
2637 case DRM_FORMAT_BGR888
:
2638 case DRM_FORMAT_XRGB8888
:
2639 case DRM_FORMAT_XBGR8888
:
2640 case DRM_FORMAT_RGBX8888
:
2641 case DRM_FORMAT_BGRX8888
:
2642 case DRM_FORMAT_ARGB8888
:
2643 case DRM_FORMAT_ABGR8888
:
2644 case DRM_FORMAT_RGBA8888
:
2645 case DRM_FORMAT_BGRA8888
:
2646 case DRM_FORMAT_XRGB2101010
:
2647 case DRM_FORMAT_XBGR2101010
:
2648 case DRM_FORMAT_RGBX1010102
:
2649 case DRM_FORMAT_BGRX1010102
:
2650 case DRM_FORMAT_ARGB2101010
:
2651 case DRM_FORMAT_ABGR2101010
:
2652 case DRM_FORMAT_RGBA1010102
:
2653 case DRM_FORMAT_BGRA1010102
:
2654 case DRM_FORMAT_XBGR16161616F
:
2655 case DRM_FORMAT_ABGR16161616F
:
2656 case DRM_FORMAT_YUYV
:
2657 case DRM_FORMAT_YVYU
:
2658 case DRM_FORMAT_UYVY
:
2659 case DRM_FORMAT_VYUY
:
2660 case DRM_FORMAT_AYUV
:
2661 case DRM_FORMAT_XYUV8888
:
2664 case DRM_FORMAT_NV12
:
2665 case DRM_FORMAT_NV21
:
2666 case DRM_FORMAT_NV16
:
2667 case DRM_FORMAT_NV61
:
2668 case DRM_FORMAT_P010
:
2669 case DRM_FORMAT_P012
:
2670 case DRM_FORMAT_P016
:
2673 case DRM_FORMAT_YUV410
:
2674 case DRM_FORMAT_YVU410
:
2675 case DRM_FORMAT_YUV411
:
2676 case DRM_FORMAT_YVU411
:
2677 case DRM_FORMAT_YUV420
:
2678 case DRM_FORMAT_YVU420
:
2679 case DRM_FORMAT_YUV422
:
2680 case DRM_FORMAT_YVU422
:
2681 case DRM_FORMAT_YUV444
:
2682 case DRM_FORMAT_YVU444
:
2690 /* Returns the total number of file descriptors. Zero indicates an error. */
2692 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
2694 unsigned plane_n
= dri2_num_fourcc_format_planes(attrs
->DMABufFourCC
.Value
);
2696 _eglError(EGL_BAD_MATCH
, "unknown drm fourcc format");
2700 for (unsigned i
= plane_n
; i
< DMA_BUF_MAX_PLANES
; i
++) {
2702 * The modifiers extension spec says:
2704 * "Modifiers may modify any attribute of a buffer import, including
2705 * but not limited to adding extra planes to a format which
2706 * otherwise does not have those planes. As an example, a modifier
2707 * may add a plane for an external compression buffer to a
2708 * single-plane format. The exact meaning and effect of any
2709 * modifier is canonically defined by drm_fourcc.h, not as part of
2712 if (attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
&&
2713 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
) {
2721 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2722 * incomplete, EGL_BAD_PARAMETER is generated."
2724 for (unsigned i
= 0; i
< plane_n
; ++i
) {
2725 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2726 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2727 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2728 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2734 * The spec also says:
2736 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2737 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2738 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2739 * or EGL_DMA_BUF_PLANE3_* attributes are specified."
2741 for (unsigned i
= plane_n
; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2742 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2743 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2744 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2745 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2754 dri2_query_dma_buf_formats(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
2755 EGLint max
, EGLint
*formats
, EGLint
*count
)
2757 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2758 if (max
< 0 || (max
> 0 && formats
== NULL
))
2759 return _eglError(EGL_BAD_PARAMETER
, "invalid value for max count of formats");
2761 if (dri2_dpy
->image
->base
.version
< 15 ||
2762 dri2_dpy
->image
->queryDmaBufFormats
== NULL
)
2765 if (!dri2_dpy
->image
->queryDmaBufFormats(dri2_dpy
->dri_screen
, max
,
2770 /* Assert that all of the formats returned are actually fourcc formats.
2771 * Some day, if we want the internal interface function to be able to
2772 * return the fake fourcc formats defined in dri_interface.h, we'll have
2773 * to do something more clever here to pair the list down to just real
2774 * fourcc formats so that we don't leak the fake internal ones.
2776 for (int i
= 0; i
< *count
; i
++) {
2777 assert(dri2_num_fourcc_format_planes(formats
[i
]) > 0);
2785 dri2_query_dma_buf_modifiers(const _EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint format
,
2786 EGLint max
, EGLuint64KHR
*modifiers
,
2787 EGLBoolean
*external_only
, EGLint
*count
)
2789 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2791 if (dri2_num_fourcc_format_planes(format
) == 0)
2792 return _eglError(EGL_BAD_PARAMETER
, "invalid fourcc format");
2795 return _eglError(EGL_BAD_PARAMETER
, "invalid value for max count of formats");
2797 if (max
> 0 && modifiers
== NULL
)
2798 return _eglError(EGL_BAD_PARAMETER
, "invalid modifiers array");
2800 if (dri2_dpy
->image
->base
.version
< 15 ||
2801 dri2_dpy
->image
->queryDmaBufModifiers
== NULL
)
2804 if (dri2_dpy
->image
->queryDmaBufModifiers(dri2_dpy
->dri_screen
, format
,
2806 (unsigned int *) external_only
,
2808 return _eglError(EGL_BAD_PARAMETER
, "invalid format");
2816 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2817 * EGL will take a reference to the dma_buf(s) which it will release at any
2818 * time while the EGLDisplay is initialized. It is the responsibility of the
2819 * application to close the dma_buf file descriptors."
2821 * Therefore we must never close or otherwise modify the file descriptors.
2824 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2825 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2827 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2829 _EGLImageAttribs attrs
;
2830 __DRIimage
*dri_image
;
2832 int fds
[DMA_BUF_MAX_PLANES
];
2833 int pitches
[DMA_BUF_MAX_PLANES
];
2834 int offsets
[DMA_BUF_MAX_PLANES
];
2836 bool has_modifier
= false;
2842 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2843 * error EGL_BAD_PARAMETER is generated."
2845 if (buffer
!= NULL
) {
2846 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2850 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2853 if (!dri2_check_dma_buf_attribs(&attrs
))
2856 num_fds
= dri2_check_dma_buf_format(&attrs
);
2860 for (unsigned i
= 0; i
< num_fds
; ++i
) {
2861 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2862 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2863 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2866 /* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2867 * will be present in attrs.DMABufPlaneModifiersLo[0] and
2868 * attrs.DMABufPlaneModifiersHi[0] */
2869 if (attrs
.DMABufPlaneModifiersLo
[0].IsPresent
) {
2870 modifier
= combine_u32_into_u64(attrs
.DMABufPlaneModifiersHi
[0].Value
,
2871 attrs
.DMABufPlaneModifiersLo
[0].Value
);
2872 has_modifier
= true;
2876 if (dri2_dpy
->image
->base
.version
< 15 ||
2877 dri2_dpy
->image
->createImageFromDmaBufs2
== NULL
) {
2878 _eglError(EGL_BAD_MATCH
, "unsupported dma_buf format modifier");
2879 return EGL_NO_IMAGE_KHR
;
2882 dri2_dpy
->image
->createImageFromDmaBufs2(dri2_dpy
->dri_screen
,
2883 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2884 modifier
, fds
, num_fds
, pitches
, offsets
,
2885 attrs
.DMABufYuvColorSpaceHint
.Value
,
2886 attrs
.DMABufSampleRangeHint
.Value
,
2887 attrs
.DMABufChromaHorizontalSiting
.Value
,
2888 attrs
.DMABufChromaVerticalSiting
.Value
,
2894 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2895 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2896 fds
, num_fds
, pitches
, offsets
,
2897 attrs
.DMABufYuvColorSpaceHint
.Value
,
2898 attrs
.DMABufSampleRangeHint
.Value
,
2899 attrs
.DMABufChromaHorizontalSiting
.Value
,
2900 attrs
.DMABufChromaVerticalSiting
.Value
,
2904 dri2_create_image_khr_texture_error(error
);
2907 return EGL_NO_IMAGE_KHR
;
2909 res
= dri2_create_image_from_dri(disp
, dri_image
);
2914 dri2_create_drm_image_mesa(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
2915 const EGLint
*attr_list
)
2917 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2918 struct dri2_egl_image
*dri2_img
;
2919 _EGLImageAttribs attrs
;
2920 unsigned int dri_use
, valid_mask
;
2926 _eglError(EGL_BAD_PARAMETER
, __func__
);
2927 return EGL_NO_IMAGE_KHR
;
2930 if (!_eglParseImageAttribList(&attrs
, disp
, attr_list
))
2931 return EGL_NO_IMAGE_KHR
;
2933 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2934 _eglError(EGL_BAD_PARAMETER
, __func__
);
2935 return EGL_NO_IMAGE_KHR
;
2938 switch (attrs
.DRMBufferFormatMESA
) {
2939 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2940 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2943 _eglError(EGL_BAD_PARAMETER
, __func__
);
2944 return EGL_NO_IMAGE_KHR
;
2948 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2949 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2950 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2951 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2952 _eglError(EGL_BAD_PARAMETER
, __func__
);
2953 return EGL_NO_IMAGE_KHR
;
2957 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2958 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2959 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2960 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2961 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2962 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2964 dri2_img
= malloc(sizeof *dri2_img
);
2966 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2967 return EGL_NO_IMAGE_KHR
;
2970 _eglInitImage(&dri2_img
->base
, disp
);
2972 dri2_img
->dri_image
=
2973 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2974 attrs
.Width
, attrs
.Height
,
2975 format
, dri_use
, dri2_img
);
2976 if (dri2_img
->dri_image
== NULL
) {
2978 _eglError(EGL_BAD_ALLOC
, "dri2_create_drm_image_mesa");
2979 return EGL_NO_IMAGE_KHR
;
2982 return &dri2_img
->base
;
2986 dri2_export_drm_image_mesa(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2987 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2989 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2990 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2994 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2995 __DRI_IMAGE_ATTRIB_NAME
, name
))
2996 return _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2999 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
3000 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
3003 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
3004 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
3010 * Checks if we can support EGL_MESA_image_dma_buf_export on this image.
3012 * The spec provides a boolean return for the driver to reject exporting for
3013 * basically any reason, but doesn't specify any particular error cases. For
3014 * now, we just fail if we don't have a DRM fourcc for the format.
3017 dri2_can_export_dma_buf_image(_EGLDisplay
*disp
, _EGLImage
*img
)
3019 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3020 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
3023 if (!dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
3024 __DRI_IMAGE_ATTRIB_FOURCC
, &fourcc
)) {
3032 dri2_export_dma_buf_image_query_mesa(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
3034 EGLint
*fourcc
, EGLint
*nplanes
,
3035 EGLuint64KHR
*modifiers
)
3037 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3038 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
3043 if (!dri2_can_export_dma_buf_image(disp
, img
))
3046 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
3047 __DRI_IMAGE_ATTRIB_NUM_PLANES
, &num_planes
);
3049 *nplanes
= num_planes
;
3052 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
3053 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
3057 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
3060 query
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
3061 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
3063 query
&= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
3064 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
3067 modifier
= combine_u32_into_u64 (mod_hi
, mod_lo
);
3069 for (int i
= 0; i
< num_planes
; i
++)
3070 modifiers
[i
] = modifier
;
3077 dri2_export_dma_buf_image_mesa(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
3078 int *fds
, EGLint
*strides
, EGLint
*offsets
)
3080 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3081 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
3086 if (!dri2_can_export_dma_buf_image(disp
, img
))
3089 /* EGL_MESA_image_dma_buf_export spec says:
3090 * "If the number of fds is less than the number of planes, then
3091 * subsequent fd slots should contain -1."
3094 /* Query nplanes so that we know how big the given array is. */
3095 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
3096 __DRI_IMAGE_ATTRIB_NUM_PLANES
, &nplanes
);
3097 memset(fds
, -1, nplanes
* sizeof(int));
3100 /* rework later to provide multiple fds/strides/offsets */
3102 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
3103 __DRI_IMAGE_ATTRIB_FD
, fds
);
3106 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
3107 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
3111 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
3112 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
3114 offsets
[0] = img_offset
;
3125 dri2_create_image_khr(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
3126 _EGLContext
*ctx
, EGLenum target
,
3127 EGLClientBuffer buffer
, const EGLint
*attr_list
)
3132 case EGL_GL_TEXTURE_2D_KHR
:
3133 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
3134 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
3135 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
3136 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
3137 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
3138 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
3139 case EGL_GL_TEXTURE_3D_KHR
:
3140 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
3141 case EGL_GL_RENDERBUFFER_KHR
:
3142 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
3144 case EGL_DRM_BUFFER_MESA
:
3145 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
3146 case EGL_LINUX_DMA_BUF_EXT
:
3147 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
3149 #ifdef HAVE_WAYLAND_PLATFORM
3150 case EGL_WAYLAND_BUFFER_WL
:
3151 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
3154 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
3155 return EGL_NO_IMAGE_KHR
;
3160 dri2_destroy_image_khr(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
3162 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3163 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
3167 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
3173 #ifdef HAVE_WAYLAND_PLATFORM
3176 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
3177 struct wl_drm_buffer
*buffer
)
3179 _EGLDisplay
*disp
= user_data
;
3180 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3182 int dri_components
= 0;
3185 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
3194 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
3206 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
3208 buffer
->driver_format
= NULL
;
3209 for (int i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
3210 if (wl_drm_components
[i
].dri_components
== dri_components
)
3211 buffer
->driver_format
= &wl_drm_components
[i
];
3213 if (buffer
->driver_format
== NULL
)
3214 dri2_dpy
->image
->destroyImage(img
);
3216 buffer
->driver_buffer
= img
;
3220 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
3222 _EGLDisplay
*disp
= user_data
;
3223 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3225 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
3229 dri2_bind_wayland_display_wl(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
3230 struct wl_display
*wl_dpy
)
3232 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3233 const struct wayland_drm_callbacks wl_drm_callbacks
= {
3234 .authenticate
= (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
,
3235 .reference_buffer
= dri2_wl_reference_buffer
,
3236 .release_buffer
= dri2_wl_release_buffer
,
3237 .is_format_supported
= dri2_wl_is_format_supported
3244 if (dri2_dpy
->wl_server_drm
)
3247 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
3248 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
3249 dri2_dpy
->image
->base
.version
>= 7 &&
3250 dri2_dpy
->image
->createImageFromFds
!= NULL
)
3251 flags
|= WAYLAND_DRM_PRIME
;
3253 dri2_dpy
->wl_server_drm
=
3254 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
3255 &wl_drm_callbacks
, disp
, flags
);
3257 if (!dri2_dpy
->wl_server_drm
)
3260 #ifdef HAVE_DRM_PLATFORM
3261 /* We have to share the wl_drm instance with gbm, so gbm can convert
3262 * wl_buffers to gbm bos. */
3263 if (dri2_dpy
->gbm_dri
)
3264 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
3271 dri2_unbind_wayland_display_wl(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
3272 struct wl_display
*wl_dpy
)
3274 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3278 if (!dri2_dpy
->wl_server_drm
)
3281 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
3282 dri2_dpy
->wl_server_drm
= NULL
;
3288 dri2_query_wayland_buffer_wl(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
3289 struct wl_resource
*buffer_resource
,
3290 EGLint attribute
, EGLint
*value
)
3292 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3293 struct wl_drm_buffer
*buffer
;
3294 const struct wl_drm_components_descriptor
*format
;
3296 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
3300 format
= buffer
->driver_format
;
3301 switch (attribute
) {
3302 case EGL_TEXTURE_FORMAT
:
3303 *value
= format
->components
;
3306 *value
= buffer
->width
;
3309 *value
= buffer
->height
;
3318 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
3320 p_atomic_inc(&sync
->refcount
);
3324 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
3325 struct dri2_egl_sync
*dri2_sync
)
3327 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
3328 switch (dri2_sync
->base
.Type
) {
3329 case EGL_SYNC_REUSABLE_KHR
:
3330 cnd_destroy(&dri2_sync
->cond
);
3332 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3333 if (dri2_sync
->base
.SyncFd
!= EGL_NO_NATIVE_FENCE_FD_ANDROID
)
3334 close(dri2_sync
->base
.SyncFd
);
3340 if (dri2_sync
->fence
)
3341 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
3348 dri2_create_sync(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
3349 EGLenum type
, const EGLAttrib
*attrib_list
)
3351 _EGLContext
*ctx
= _eglGetCurrentContext();
3352 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3353 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3354 struct dri2_egl_sync
*dri2_sync
;
3356 pthread_condattr_t attr
;
3358 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
3360 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
3364 if (!_eglInitSync(&dri2_sync
->base
, disp
, type
, attrib_list
)) {
3370 case EGL_SYNC_FENCE_KHR
:
3371 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
3372 if (!dri2_sync
->fence
) {
3373 /* Why did it fail? DRI doesn't return an error code, so we emit
3374 * a generic EGL error that doesn't communicate user error.
3376 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
3382 case EGL_SYNC_CL_EVENT_KHR
:
3383 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
3384 dri2_dpy
->dri_screen
,
3385 dri2_sync
->base
.CLEvent
);
3386 /* this can only happen if the cl_event passed in is invalid. */
3387 if (!dri2_sync
->fence
) {
3388 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
3393 /* the initial status must be "signaled" if the cl_event is signaled */
3394 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
3395 dri2_sync
->fence
, 0, 0))
3396 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3399 case EGL_SYNC_REUSABLE_KHR
:
3400 /* intialize attr */
3401 ret
= pthread_condattr_init(&attr
);
3404 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3409 /* change clock attribute to CLOCK_MONOTONIC */
3410 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
3413 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3418 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
3421 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
3426 /* initial status of reusable sync must be "unsignaled" */
3427 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
3430 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3431 if (dri2_dpy
->fence
->create_fence_fd
) {
3432 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence_fd(
3433 dri2_ctx
->dri_context
,
3434 dri2_sync
->base
.SyncFd
);
3436 if (!dri2_sync
->fence
) {
3437 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
3444 p_atomic_set(&dri2_sync
->refcount
, 1);
3445 return &dri2_sync
->base
;
3449 dri2_destroy_sync(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3451 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3452 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3453 EGLint ret
= EGL_TRUE
;
3456 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
3457 * then unlock all threads possibly blocked by the reusable sync before
3460 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
3461 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
3462 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3463 /* unblock all threads currently blocked by sync */
3464 err
= cnd_broadcast(&dri2_sync
->cond
);
3467 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
3472 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
3478 dri2_dup_native_fence_fd(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3480 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3481 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3483 assert(sync
->Type
== EGL_SYNC_NATIVE_FENCE_ANDROID
);
3485 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
3486 /* try to retrieve the actual native fence fd.. if rendering is
3487 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
3489 sync
->SyncFd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
3493 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
3494 /* if native fence fd still not created, return an error: */
3495 _eglError(EGL_BAD_PARAMETER
, "eglDupNativeFenceFDANDROID");
3496 return EGL_NO_NATIVE_FENCE_FD_ANDROID
;
3499 return os_dupfd_cloexec(sync
->SyncFd
);
3503 dri2_set_blob_cache_funcs(const _EGLDriver
*drv
, _EGLDisplay
*disp
,
3504 EGLSetBlobFuncANDROID set
,
3505 EGLGetBlobFuncANDROID get
)
3507 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3508 dri2_dpy
->blob
->set_cache_funcs(dri2_dpy
->dri_screen
,
3510 disp
->BlobCacheGet
);
3514 dri2_client_wait_sync(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
,
3515 EGLint flags
, EGLTime timeout
)
3517 _EGLContext
*ctx
= _eglGetCurrentContext();
3518 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3519 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3520 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3521 unsigned wait_flags
= 0;
3523 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
3525 /* The EGL_KHR_fence_sync spec states:
3527 * "If no context is current for the bound API,
3528 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
3530 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
3531 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
3533 /* the sync object should take a reference while waiting */
3534 dri2_egl_ref_sync(dri2_sync
);
3536 switch (sync
->Type
) {
3537 case EGL_SYNC_FENCE_KHR
:
3538 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
3539 case EGL_SYNC_CL_EVENT_KHR
:
3540 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
3541 dri2_sync
->fence
, wait_flags
,
3543 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
3545 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
3548 case EGL_SYNC_REUSABLE_KHR
:
3549 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
3550 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
3551 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
3555 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
3556 if (timeout
== EGL_FOREVER_KHR
) {
3557 mtx_lock(&dri2_sync
->mutex
);
3558 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
3559 mtx_unlock(&dri2_sync
->mutex
);
3561 /* if reusable sync has not been yet signaled */
3562 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
3563 /* timespecs for cnd_timedwait */
3564 struct timespec current
;
3565 struct timespec expire
;
3567 /* We override the clock to monotonic when creating the condition
3569 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
3571 /* calculating when to expire */
3572 expire
.tv_nsec
= timeout
% 1000000000L;
3573 expire
.tv_sec
= timeout
/ 1000000000L;
3575 expire
.tv_nsec
+= current
.tv_nsec
;
3576 expire
.tv_sec
+= current
.tv_sec
;
3578 /* expire.nsec now is a number between 0 and 1999999998 */
3579 if (expire
.tv_nsec
> 999999999L) {
3581 expire
.tv_nsec
-= 1000000000L;
3584 mtx_lock(&dri2_sync
->mutex
);
3585 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
3586 mtx_unlock(&dri2_sync
->mutex
);
3588 if (ret
== thrd_busy
) {
3589 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
3590 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
3592 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
3600 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
3606 dri2_signal_sync(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
,
3609 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3612 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
)
3613 return _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
3615 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
)
3616 return _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
3618 dri2_sync
->base
.SyncStatus
= mode
;
3620 if (mode
== EGL_SIGNALED_KHR
) {
3621 ret
= cnd_broadcast(&dri2_sync
->cond
);
3623 /* fail to broadcast */
3625 return _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
3632 dri2_server_wait_sync(const _EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSync
*sync
)
3634 _EGLContext
*ctx
= _eglGetCurrentContext();
3635 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3636 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3637 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3639 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
3640 dri2_sync
->fence
, 0);
3645 dri2_interop_query_device_info(_EGLDisplay
*disp
, _EGLContext
*ctx
,
3646 struct mesa_glinterop_device_info
*out
)
3648 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3649 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3651 if (!dri2_dpy
->interop
)
3652 return MESA_GLINTEROP_UNSUPPORTED
;
3654 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
3658 dri2_interop_export_object(_EGLDisplay
*disp
, _EGLContext
*ctx
,
3659 struct mesa_glinterop_export_in
*in
,
3660 struct mesa_glinterop_export_out
*out
)
3662 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
3663 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3665 if (!dri2_dpy
->interop
)
3666 return MESA_GLINTEROP_UNSUPPORTED
;
3668 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
3671 const _EGLDriver _eglDriver
= {
3672 .Initialize
= dri2_initialize
,
3673 .Terminate
= dri2_terminate
,
3674 .CreateContext
= dri2_create_context
,
3675 .DestroyContext
= dri2_destroy_context
,
3676 .MakeCurrent
= dri2_make_current
,
3677 .CreateWindowSurface
= dri2_create_window_surface
,
3678 .CreatePixmapSurface
= dri2_create_pixmap_surface
,
3679 .CreatePbufferSurface
= dri2_create_pbuffer_surface
,
3680 .DestroySurface
= dri2_destroy_surface
,
3681 .GetProcAddress
= dri2_get_proc_address
,
3682 .WaitClient
= dri2_wait_client
,
3683 .WaitNative
= dri2_wait_native
,
3684 .BindTexImage
= dri2_bind_tex_image
,
3685 .ReleaseTexImage
= dri2_release_tex_image
,
3686 .SwapInterval
= dri2_swap_interval
,
3687 .SwapBuffers
= dri2_swap_buffers
,
3688 .SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
,
3689 .SwapBuffersRegionNOK
= dri2_swap_buffers_region
,
3690 .SetDamageRegion
= dri2_set_damage_region
,
3691 .PostSubBufferNV
= dri2_post_sub_buffer
,
3692 .CopyBuffers
= dri2_copy_buffers
,
3693 .QueryBufferAge
= dri2_query_buffer_age
,
3694 .CreateImageKHR
= dri2_create_image
,
3695 .DestroyImageKHR
= dri2_destroy_image_khr
,
3696 .CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
,
3697 .QuerySurface
= dri2_query_surface
,
3698 .QueryDriverName
= dri2_query_driver_name
,
3699 .QueryDriverConfig
= dri2_query_driver_config
,
3701 .CreateDRMImageMESA
= dri2_create_drm_image_mesa
,
3702 .ExportDRMImageMESA
= dri2_export_drm_image_mesa
,
3703 .ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
,
3704 .ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
,
3705 .QueryDmaBufFormatsEXT
= dri2_query_dma_buf_formats
,
3706 .QueryDmaBufModifiersEXT
= dri2_query_dma_buf_modifiers
,
3708 #ifdef HAVE_WAYLAND_PLATFORM
3709 .BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
,
3710 .UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
,
3711 .QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
,
3713 .GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
,
3714 .CreateSyncKHR
= dri2_create_sync
,
3715 .ClientWaitSyncKHR
= dri2_client_wait_sync
,
3716 .SignalSyncKHR
= dri2_signal_sync
,
3717 .WaitSyncKHR
= dri2_server_wait_sync
,
3718 .DestroySyncKHR
= dri2_destroy_sync
,
3719 .GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
,
3720 .GLInteropExportObject
= dri2_interop_export_object
,
3721 .DupNativeFenceFDANDROID
= dri2_dup_native_fence_fd
,
3722 .SetBlobCacheFuncsANDROID
= dri2_set_blob_cache_funcs
,