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>
28 #define WL_HIDE_DEPRECATED
41 #include <c11/threads.h>
45 #include <drm_fourcc.h>
48 #include <GL/internal/dri_interface.h>
49 #include "GL/mesa_glinterop.h"
50 #include <sys/types.h>
53 #ifdef HAVE_WAYLAND_PLATFORM
54 #include "wayland-drm.h"
55 #include "wayland-drm-client-protocol.h"
58 #ifdef HAVE_X11_PLATFORM
59 #include "X11/Xlibint.h"
63 #include "loader/loader.h"
64 #include "util/u_atomic.h"
66 /* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
67 * some of the definitions here so that building Mesa won't bleeding-edge
71 #define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
74 #ifndef DRM_FORMAT_RG88
75 #define DRM_FORMAT_RG88 fourcc_code('R', 'G', '8', '8') /* [15:0] R:G 8:8 little endian */
78 #ifndef DRM_FORMAT_GR88
79 #define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
82 #ifndef DRM_FORMAT_R16
83 #define DRM_FORMAT_R16 fourcc_code('R', '1', '6', ' ') /* [15:0] R 16 little endian */
86 #ifndef DRM_FORMAT_GR1616
87 #define DRM_FORMAT_GR1616 fourcc_code('G', 'R', '3', '2') /* [31:0] R:G 16:16 little endian */
90 #ifndef DRM_FORMAT_MOD_INVALID
91 #define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1)
95 dri_set_background_context(void *loaderPrivate
)
97 _EGLContext
*ctx
= _eglGetCurrentContext();
98 _EGLThreadInfo
*t
= _eglGetCurrentThread();
100 _eglBindContextToThread(ctx
, t
);
104 dri_is_thread_safe(void *loaderPrivate
)
106 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
107 _EGLDisplay
*display
= dri2_surf
->base
.Resource
.Display
;
109 #ifdef HAVE_X11_PLATFORM
110 Display
*xdpy
= (Display
*)display
->PlatformDisplay
;
112 /* Check Xlib is running in thread safe mode when running on EGL/X11-xlib
115 * 'lock_fns' is the XLockDisplay function pointer of the X11 display 'dpy'.
116 * It wll be NULL if XInitThreads wasn't called.
118 if (display
->Platform
== _EGL_PLATFORM_X11
&& xdpy
&& !xdpy
->lock_fns
)
122 #ifdef HAVE_WAYLAND_PLATFORM
123 if (display
->Platform
== _EGL_PLATFORM_WAYLAND
)
130 const __DRIbackgroundCallableExtension background_callable_extension
= {
131 .base
= { __DRI_BACKGROUND_CALLABLE
, 2 },
133 .setBackgroundContext
= dri_set_background_context
,
134 .isThreadSafe
= dri_is_thread_safe
,
137 const __DRIuseInvalidateExtension use_invalidate
= {
138 .base
= { __DRI_USE_INVALIDATE
, 1 }
141 static const EGLint dri2_to_egl_attribute_map
[__DRI_ATTRIB_MAX
] = {
142 [__DRI_ATTRIB_BUFFER_SIZE
] = EGL_BUFFER_SIZE
,
143 [__DRI_ATTRIB_LEVEL
] = EGL_LEVEL
,
144 [__DRI_ATTRIB_RED_SIZE
] = EGL_RED_SIZE
,
145 [__DRI_ATTRIB_GREEN_SIZE
] = EGL_GREEN_SIZE
,
146 [__DRI_ATTRIB_BLUE_SIZE
] = EGL_BLUE_SIZE
,
147 [__DRI_ATTRIB_LUMINANCE_SIZE
] = EGL_LUMINANCE_SIZE
,
148 [__DRI_ATTRIB_ALPHA_SIZE
] = EGL_ALPHA_SIZE
,
149 [__DRI_ATTRIB_DEPTH_SIZE
] = EGL_DEPTH_SIZE
,
150 [__DRI_ATTRIB_STENCIL_SIZE
] = EGL_STENCIL_SIZE
,
151 [__DRI_ATTRIB_SAMPLE_BUFFERS
] = EGL_SAMPLE_BUFFERS
,
152 [__DRI_ATTRIB_SAMPLES
] = EGL_SAMPLES
,
153 [__DRI_ATTRIB_MAX_PBUFFER_WIDTH
] = EGL_MAX_PBUFFER_WIDTH
,
154 [__DRI_ATTRIB_MAX_PBUFFER_HEIGHT
] = EGL_MAX_PBUFFER_HEIGHT
,
155 [__DRI_ATTRIB_MAX_PBUFFER_PIXELS
] = EGL_MAX_PBUFFER_PIXELS
,
156 [__DRI_ATTRIB_MAX_SWAP_INTERVAL
] = EGL_MAX_SWAP_INTERVAL
,
157 [__DRI_ATTRIB_MIN_SWAP_INTERVAL
] = EGL_MIN_SWAP_INTERVAL
,
158 [__DRI_ATTRIB_YINVERTED
] = EGL_Y_INVERTED_NOK
,
162 dri2_get_dri_config(struct dri2_egl_config
*conf
, EGLint surface_type
,
165 const bool srgb
= colorspace
== EGL_GL_COLORSPACE_SRGB_KHR
;
167 return surface_type
== EGL_WINDOW_BIT
? conf
->dri_double_config
[srgb
] :
168 conf
->dri_single_config
[srgb
];
172 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
174 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
177 if (!_eglMatchConfig(conf
, criteria
))
183 struct dri2_egl_config
*
184 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
185 EGLint surface_type
, const EGLint
*attr_list
,
186 const unsigned int *rgba_masks
)
188 struct dri2_egl_config
*conf
;
189 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
191 unsigned int attrib
, value
, double_buffer
;
193 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
194 unsigned int dri_masks
[4] = { 0, 0, 0, 0 };
195 _EGLConfig
*matching_config
;
196 EGLint num_configs
= 0;
200 _eglInitConfig(&base
, disp
, id
);
204 bind_to_texture_rgb
= 0;
205 bind_to_texture_rgba
= 0;
207 while (dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
++, &attrib
, &value
)) {
209 case __DRI_ATTRIB_RENDER_TYPE
:
210 if (value
& __DRI_ATTRIB_RGBA_BIT
)
211 value
= EGL_RGB_BUFFER
;
212 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
213 value
= EGL_LUMINANCE_BUFFER
;
216 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
219 case __DRI_ATTRIB_CONFIG_CAVEAT
:
220 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
221 value
= EGL_NON_CONFORMANT_CONFIG
;
222 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
223 value
= EGL_SLOW_CONFIG
;
226 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
229 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
230 bind_to_texture_rgb
= value
;
233 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
234 bind_to_texture_rgba
= value
;
237 case __DRI_ATTRIB_DOUBLE_BUFFER
:
238 double_buffer
= value
;
241 case __DRI_ATTRIB_RED_MASK
:
242 dri_masks
[0] = value
;
245 case __DRI_ATTRIB_GREEN_MASK
:
246 dri_masks
[1] = value
;
249 case __DRI_ATTRIB_BLUE_MASK
:
250 dri_masks
[2] = value
;
253 case __DRI_ATTRIB_ALPHA_MASK
:
254 dri_masks
[3] = value
;
257 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
258 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
259 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
260 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
261 /* Don't expose visuals with the accumulation buffer. */
266 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
268 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
272 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH
:
273 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_WIDTH
,
274 _EGL_MAX_PBUFFER_WIDTH
);
276 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT
:
277 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_HEIGHT
,
278 _EGL_MAX_PBUFFER_HEIGHT
);
282 key
= dri2_to_egl_attribute_map
[attrib
];
284 _eglSetConfigKey(&base
, key
, value
);
290 for (i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
291 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
293 if (rgba_masks
&& memcmp(rgba_masks
, dri_masks
, sizeof(dri_masks
)))
296 base
.NativeRenderable
= EGL_TRUE
;
298 base
.SurfaceType
= surface_type
;
299 if (surface_type
& (EGL_PBUFFER_BIT
|
300 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
301 base
.BindToTextureRGB
= bind_to_texture_rgb
;
302 if (base
.AlphaSize
> 0)
303 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
306 base
.RenderableType
= disp
->ClientAPIs
;
307 base
.Conformant
= disp
->ClientAPIs
;
309 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
310 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
312 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
313 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
317 config_id
= base
.ConfigID
;
318 base
.ConfigID
= EGL_DONT_CARE
;
319 base
.SurfaceType
= EGL_DONT_CARE
;
320 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
321 (_EGLArrayForEach
) dri2_match_config
, &base
);
323 if (num_configs
== 1) {
324 conf
= (struct dri2_egl_config
*) matching_config
;
326 if (double_buffer
&& !conf
->dri_double_config
[srgb
])
327 conf
->dri_double_config
[srgb
] = dri_config
;
328 else if (!double_buffer
&& !conf
->dri_single_config
[srgb
])
329 conf
->dri_single_config
[srgb
] = dri_config
;
331 /* a similar config type is already added (unlikely) => discard */
334 else if (num_configs
== 0) {
335 conf
= calloc(1, sizeof *conf
);
340 conf
->dri_double_config
[srgb
] = dri_config
;
342 conf
->dri_single_config
[srgb
] = dri_config
;
344 memcpy(&conf
->base
, &base
, sizeof base
);
345 conf
->base
.SurfaceType
= 0;
346 conf
->base
.ConfigID
= config_id
;
348 _eglLinkConfig(&conf
->base
);
356 surface_type
&= ~EGL_PIXMAP_BIT
;
359 /* No support for pbuffer + MSAA for now.
361 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
362 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
365 surface_type
&= ~EGL_PBUFFER_BIT
;
368 conf
->base
.SurfaceType
|= surface_type
;
374 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
376 _EGLDisplay
*disp
= data
;
377 struct dri2_egl_image
*dri2_img
;
382 img
= _eglLookupImage(image
, disp
);
384 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
388 dri2_img
= dri2_egl_image(image
);
390 return dri2_img
->dri_image
;
393 const __DRIimageLookupExtension image_lookup_extension
= {
394 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
396 .lookupEGLImage
= dri2_lookup_egl_image
399 struct dri2_extension_match
{
405 static const struct dri2_extension_match dri3_driver_extensions
[] = {
406 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
407 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
411 static const struct dri2_extension_match dri2_driver_extensions
[] = {
412 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
413 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
417 static const struct dri2_extension_match dri2_core_extensions
[] = {
418 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
419 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
420 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
424 static const struct dri2_extension_match swrast_driver_extensions
[] = {
425 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
426 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
430 static const struct dri2_extension_match swrast_core_extensions
[] = {
431 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
435 static const struct dri2_extension_match optional_core_extensions
[] = {
436 { __DRI2_ROBUSTNESS
, 1, offsetof(struct dri2_egl_display
, robustness
) },
437 { __DRI2_CONFIG_QUERY
, 1, offsetof(struct dri2_egl_display
, config
) },
438 { __DRI2_FENCE
, 1, offsetof(struct dri2_egl_display
, fence
) },
439 { __DRI2_RENDERER_QUERY
, 1, offsetof(struct dri2_egl_display
, rendererQuery
) },
440 { __DRI2_INTEROP
, 1, offsetof(struct dri2_egl_display
, interop
) },
445 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
446 const struct dri2_extension_match
*matches
,
447 const __DRIextension
**extensions
,
450 int i
, j
, ret
= EGL_TRUE
;
453 for (i
= 0; extensions
[i
]; i
++) {
454 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
455 for (j
= 0; matches
[j
].name
; j
++) {
456 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
457 extensions
[i
]->version
>= matches
[j
].version
) {
458 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
459 *(const __DRIextension
**) field
= extensions
[i
];
460 _eglLog(_EGL_INFO
, "found extension %s version %d",
461 extensions
[i
]->name
, extensions
[i
]->version
);
467 for (j
= 0; matches
[j
].name
; j
++) {
468 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
469 if (*(const __DRIextension
**) field
== NULL
) {
471 _eglLog(_EGL_DEBUG
, "did not find optional extension %s version %d",
472 matches
[j
].name
, matches
[j
].version
);
474 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
475 matches
[j
].name
, matches
[j
].version
);
484 static const __DRIextension
**
485 dri2_open_driver(_EGLDisplay
*disp
)
487 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
488 const __DRIextension
**extensions
= NULL
;
489 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
490 char *get_extensions_name
;
491 const __DRIextension
**(*get_extensions
)(void);
494 if (geteuid() == getuid()) {
495 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
496 search_paths
= getenv("LIBGL_DRIVERS_PATH");
498 if (search_paths
== NULL
)
499 search_paths
= DEFAULT_DRIVER_DIR
;
501 dri2_dpy
->driver
= NULL
;
502 end
= search_paths
+ strlen(search_paths
);
503 for (p
= search_paths
; p
< end
; p
= next
+ 1) {
505 next
= strchr(p
, ':');
511 snprintf(path
, sizeof path
,
512 "%.*s/tls/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
513 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
515 if (dri2_dpy
->driver
== NULL
) {
516 snprintf(path
, sizeof path
,
517 "%.*s/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
518 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
519 if (dri2_dpy
->driver
== NULL
)
520 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
522 /* not need continue to loop all paths once the driver is found */
523 if (dri2_dpy
->driver
!= NULL
)
527 snprintf(path
, sizeof path
, "%.*s/gallium_dri.so", len
, p
);
528 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
529 if (dri2_dpy
->driver
== NULL
)
530 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
536 if (dri2_dpy
->driver
== NULL
) {
537 _eglLog(_EGL_WARNING
,
538 "DRI2: failed to open %s (search paths %s)",
539 dri2_dpy
->driver_name
, search_paths
);
543 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
545 get_extensions_name
= loader_get_extensions_name(dri2_dpy
->driver_name
);
546 if (get_extensions_name
) {
547 get_extensions
= dlsym(dri2_dpy
->driver
, get_extensions_name
);
548 if (get_extensions
) {
549 extensions
= get_extensions();
551 _eglLog(_EGL_DEBUG
, "driver does not expose %s(): %s\n",
552 get_extensions_name
, dlerror());
554 free(get_extensions_name
);
558 extensions
= dlsym(dri2_dpy
->driver
, __DRI_DRIVER_EXTENSIONS
);
559 if (extensions
== NULL
) {
560 _eglLog(_EGL_WARNING
,
561 "DRI2: driver exports no extensions (%s)", dlerror());
562 dlclose(dri2_dpy
->driver
);
569 dri2_load_driver_dri3(_EGLDisplay
*disp
)
571 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
572 const __DRIextension
**extensions
;
574 extensions
= dri2_open_driver(disp
);
578 if (!dri2_bind_extensions(dri2_dpy
, dri3_driver_extensions
, extensions
, false)) {
579 dlclose(dri2_dpy
->driver
);
582 dri2_dpy
->driver_extensions
= extensions
;
588 dri2_load_driver(_EGLDisplay
*disp
)
590 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
591 const __DRIextension
**extensions
;
593 extensions
= dri2_open_driver(disp
);
597 if (!dri2_bind_extensions(dri2_dpy
, dri2_driver_extensions
, extensions
, false)) {
598 dlclose(dri2_dpy
->driver
);
601 dri2_dpy
->driver_extensions
= extensions
;
607 dri2_load_driver_swrast(_EGLDisplay
*disp
)
609 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
610 const __DRIextension
**extensions
;
612 extensions
= dri2_open_driver(disp
);
616 if (!dri2_bind_extensions(dri2_dpy
, swrast_driver_extensions
, extensions
, false)) {
617 dlclose(dri2_dpy
->driver
);
620 dri2_dpy
->driver_extensions
= extensions
;
626 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
628 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
629 unsigned int value
= 0;
631 if (!rendererQuery
||
632 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
639 dri2_setup_screen(_EGLDisplay
*disp
)
641 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
642 unsigned int api_mask
;
644 if (dri2_dpy
->image_driver
) {
645 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
646 } else if (dri2_dpy
->dri2
) {
647 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
649 assert(dri2_dpy
->swrast
);
650 api_mask
= 1 << __DRI_API_OPENGL
|
651 1 << __DRI_API_GLES
|
652 1 << __DRI_API_GLES2
|
653 1 << __DRI_API_GLES3
;
656 disp
->ClientAPIs
= 0;
657 if ((api_mask
& (1 <<__DRI_API_OPENGL
)) && _eglIsApiValid(EGL_OPENGL_API
))
658 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
659 if ((api_mask
& (1 << __DRI_API_GLES
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
660 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
661 if ((api_mask
& (1 << __DRI_API_GLES2
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
662 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
663 if ((api_mask
& (1 << __DRI_API_GLES3
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
664 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
666 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
667 disp
->Extensions
.KHR_no_config_context
= EGL_TRUE
;
668 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
670 if (dri2_renderer_query_integer(dri2_dpy
,
671 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
672 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
674 if (dri2_dpy
->image_driver
||
675 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
676 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
677 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
679 if (dri2_dpy
->robustness
)
680 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
683 if (dri2_dpy
->fence
) {
684 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
685 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
686 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
687 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
688 if (dri2_dpy
->fence
->base
.version
>= 2) {
689 unsigned capabilities
=
690 dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
);
691 disp
->Extensions
.ANDROID_native_fence_sync
=
692 (capabilities
& __DRI_FENCE_CAP_NATIVE_FD
) != 0;
696 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
698 if (dri2_dpy
->image
) {
699 if (dri2_dpy
->image
->base
.version
>= 10 &&
700 dri2_dpy
->image
->getCapabilities
!= NULL
) {
703 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
704 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
706 if (dri2_dpy
->image
->base
.version
>= 11)
707 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
709 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
710 if (dri2_dpy
->image
->base
.version
>= 11)
711 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
714 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
715 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
716 if (dri2_dpy
->image
->base
.version
>= 5 &&
717 dri2_dpy
->image
->createImageFromTexture
) {
718 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
719 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
721 if (dri2_renderer_query_integer(dri2_dpy
,
722 __DRI2_RENDERER_HAS_TEXTURE_3D
))
723 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
725 if (dri2_dpy
->image
->base
.version
>= 8 &&
726 dri2_dpy
->image
->createImageFromDmaBufs
) {
727 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
729 if (dri2_dpy
->image
->base
.version
>= 15 &&
730 dri2_dpy
->image
->createImageFromDmaBufs2
&&
731 dri2_dpy
->image
->queryDmaBufFormats
&&
732 dri2_dpy
->image
->queryDmaBufModifiers
) {
733 disp
->Extensions
.EXT_image_dma_buf_import_modifiers
= EGL_TRUE
;
739 /* All platforms but DRM call this function to create the screen and populate
740 * the driver_configs. DRM inherits that information from its display - GBM.
743 dri2_create_screen(_EGLDisplay
*disp
)
745 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
747 if (dri2_dpy
->image_driver
) {
748 dri2_dpy
->dri_screen
=
749 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
750 dri2_dpy
->loader_extensions
,
751 dri2_dpy
->driver_extensions
,
752 &dri2_dpy
->driver_configs
,
754 } else if (dri2_dpy
->dri2
) {
755 if (dri2_dpy
->dri2
->base
.version
>= 4) {
756 dri2_dpy
->dri_screen
=
757 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
758 dri2_dpy
->loader_extensions
,
759 dri2_dpy
->driver_extensions
,
760 &dri2_dpy
->driver_configs
, disp
);
762 dri2_dpy
->dri_screen
=
763 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
764 dri2_dpy
->loader_extensions
,
765 &dri2_dpy
->driver_configs
, disp
);
768 assert(dri2_dpy
->swrast
);
769 if (dri2_dpy
->swrast
->base
.version
>= 4) {
770 dri2_dpy
->dri_screen
=
771 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->loader_extensions
,
772 dri2_dpy
->driver_extensions
,
773 &dri2_dpy
->driver_configs
, disp
);
775 dri2_dpy
->dri_screen
=
776 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->loader_extensions
,
777 &dri2_dpy
->driver_configs
, disp
);
781 if (dri2_dpy
->dri_screen
== NULL
) {
782 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
786 dri2_dpy
->own_dri_screen
= 1;
791 dri2_setup_extensions(_EGLDisplay
*disp
)
793 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
794 const struct dri2_extension_match
*mandatory_core_extensions
;
795 const __DRIextension
**extensions
;
797 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
799 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
)
800 mandatory_core_extensions
= dri2_core_extensions
;
802 mandatory_core_extensions
= swrast_core_extensions
;
804 if (!dri2_bind_extensions(dri2_dpy
, mandatory_core_extensions
, extensions
, false))
807 dri2_bind_extensions(dri2_dpy
, optional_core_extensions
, extensions
, true);
812 * Called via eglInitialize(), GLX_drv->API.Initialize().
814 * This must be guaranteed to be called exactly once, even if eglInitialize is
815 * called many times (without a eglTerminate in between).
818 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
820 EGLBoolean ret
= EGL_FALSE
;
821 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
823 /* In the case where the application calls eglMakeCurrent(context1),
824 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
825 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
826 * initialized, as we need it to be able to free context1 correctly.
828 * It would probably be safest to forcibly release the display with
829 * dri2_display_release, to make sure the display is reinitialized correctly.
830 * However, the EGL spec states that we need to keep a reference to the
831 * current context (so we cannot call dri2_make_current(NULL)), and therefore
832 * we would leak context1 as we would be missing the old display connection
833 * to free it up correctly.
836 dri2_dpy
->ref_count
++;
840 /* not until swrast_dri is supported */
841 if (disp
->Options
.UseFallback
)
844 /* Nothing to initialize for a test only display */
845 if (disp
->Options
.TestOnly
)
848 switch (disp
->Platform
) {
849 #ifdef HAVE_SURFACELESS_PLATFORM
850 case _EGL_PLATFORM_SURFACELESS
:
851 ret
= dri2_initialize_surfaceless(drv
, disp
);
854 #ifdef HAVE_X11_PLATFORM
855 case _EGL_PLATFORM_X11
:
856 ret
= dri2_initialize_x11(drv
, disp
);
859 #ifdef HAVE_DRM_PLATFORM
860 case _EGL_PLATFORM_DRM
:
861 ret
= dri2_initialize_drm(drv
, disp
);
864 #ifdef HAVE_WAYLAND_PLATFORM
865 case _EGL_PLATFORM_WAYLAND
:
866 ret
= dri2_initialize_wayland(drv
, disp
);
869 #ifdef HAVE_ANDROID_PLATFORM
870 case _EGL_PLATFORM_ANDROID
:
871 ret
= dri2_initialize_android(drv
, disp
);
875 _eglLog(_EGL_WARNING
, "No EGL platform enabled.");
880 dri2_dpy
= dri2_egl_display(disp
);
886 dri2_dpy
->ref_count
++;
893 * Decrement display reference count, and free up display if necessary.
896 dri2_display_release(_EGLDisplay
*disp
)
898 struct dri2_egl_display
*dri2_dpy
;
903 dri2_dpy
= dri2_egl_display(disp
);
905 assert(dri2_dpy
->ref_count
> 0);
906 dri2_dpy
->ref_count
--;
908 if (dri2_dpy
->ref_count
> 0)
911 _eglCleanupDisplay(disp
);
912 dri2_display_destroy(disp
);
916 dri2_display_destroy(_EGLDisplay
*disp
)
918 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
921 if (dri2_dpy
->own_dri_screen
)
922 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
923 if (dri2_dpy
->fd
>= 0)
925 if (dri2_dpy
->driver
)
926 dlclose(dri2_dpy
->driver
);
927 free(dri2_dpy
->driver_name
);
929 #ifdef HAVE_WAYLAND_PLATFORM
930 free(dri2_dpy
->device_name
);
933 switch (disp
->Platform
) {
934 #ifdef HAVE_X11_PLATFORM
935 case _EGL_PLATFORM_X11
:
936 if (dri2_dpy
->own_device
) {
937 xcb_disconnect(dri2_dpy
->conn
);
941 #ifdef HAVE_DRM_PLATFORM
942 case _EGL_PLATFORM_DRM
:
943 if (dri2_dpy
->own_device
) {
944 gbm_device_destroy(&dri2_dpy
->gbm_dri
->base
);
948 #ifdef HAVE_WAYLAND_PLATFORM
949 case _EGL_PLATFORM_WAYLAND
:
950 if (dri2_dpy
->wl_drm
)
951 wl_drm_destroy(dri2_dpy
->wl_drm
);
952 if (dri2_dpy
->wl_shm
)
953 wl_shm_destroy(dri2_dpy
->wl_shm
);
954 wl_registry_destroy(dri2_dpy
->wl_registry
);
955 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
956 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
957 if (dri2_dpy
->own_device
) {
958 wl_display_disconnect(dri2_dpy
->wl_dpy
);
966 /* The drm platform does not create the screen/driver_configs but reuses
967 * the ones from the gbm device. As such the gbm itself is responsible
970 if (disp
->Platform
!= _EGL_PLATFORM_DRM
&& dri2_dpy
->driver_configs
) {
971 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
972 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
973 free(dri2_dpy
->driver_configs
);
976 disp
->DriverData
= NULL
;
980 * Called via eglTerminate(), drv->API.Terminate().
982 * This must be guaranteed to be called exactly once, even if eglTerminate is
983 * called many times (without a eglInitialize in between).
986 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
988 /* Release all non-current Context/Surfaces. */
989 _eglReleaseDisplayResources(drv
, disp
);
991 dri2_display_release(disp
);
997 * Set the error code after a call to
998 * dri2_egl_display::dri2::createContextAttribs.
1001 dri2_create_context_attribs_error(int dri_error
)
1005 switch (dri_error
) {
1006 case __DRI_CTX_ERROR_SUCCESS
:
1009 case __DRI_CTX_ERROR_NO_MEMORY
:
1010 egl_error
= EGL_BAD_ALLOC
;
1013 /* From the EGL_KHR_create_context spec, section "Errors":
1015 * * If <config> does not support a client API context compatible
1016 * with the requested API major and minor version, [...] context flags,
1017 * and context reset notification behavior (for client API types where
1018 * these attributes are supported), then an EGL_BAD_MATCH error is
1021 * * If an OpenGL ES context is requested and the values for
1022 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1023 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1024 * is not defined, than an EGL_BAD_MATCH error is generated.
1026 * * If an OpenGL context is requested, the requested version is
1027 * greater than 3.2, and the value for attribute
1028 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1029 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1030 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1031 * one of these bits set; or if the implementation does not support
1032 * the requested profile, then an EGL_BAD_MATCH error is generated.
1034 case __DRI_CTX_ERROR_BAD_API
:
1035 case __DRI_CTX_ERROR_BAD_VERSION
:
1036 case __DRI_CTX_ERROR_BAD_FLAG
:
1037 egl_error
= EGL_BAD_MATCH
;
1040 /* From the EGL_KHR_create_context spec, section "Errors":
1042 * * If an attribute name or attribute value in <attrib_list> is not
1043 * recognized (including unrecognized bits in bitmask attributes),
1044 * then an EGL_BAD_ATTRIBUTE error is generated."
1046 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
1047 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
1048 egl_error
= EGL_BAD_ATTRIBUTE
;
1053 egl_error
= EGL_BAD_MATCH
;
1057 _eglError(egl_error
, "dri2_create_context");
1061 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1062 struct dri2_egl_display
*dri2_dpy
,
1063 uint32_t *ctx_attribs
,
1064 unsigned *num_attribs
)
1068 assert(*num_attribs
>= 8);
1070 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1071 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1072 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1073 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1075 if (dri2_ctx
->base
.Flags
!= 0) {
1076 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1077 * extension, don't even try to send it the robust-access flag.
1078 * It may explode. Instead, generate the required EGL error here.
1080 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1081 && !dri2_dpy
->robustness
) {
1082 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1086 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1087 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
;
1090 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1091 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1092 * extension, don't even try to send it a reset strategy. It may
1093 * explode. Instead, generate the required EGL error here.
1095 if (!dri2_dpy
->robustness
) {
1096 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1100 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1101 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1110 * Called via eglCreateContext(), drv->API.CreateContext().
1112 static _EGLContext
*
1113 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1114 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1116 struct dri2_egl_context
*dri2_ctx
;
1117 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1118 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1119 __DRIcontext
*shared
=
1120 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1121 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1122 const __DRIconfig
*dri_config
;
1127 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1129 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1133 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1136 /* The EGL_EXT_create_context_robustness spec says:
1138 * "Add to the eglCreateContext context creation errors: [...]
1140 * * If the reset notification behavior of <share_context> and the
1141 * newly created context are different then an EGL_BAD_MATCH error is
1144 if (share_list
&& share_list
->ResetNotificationStrategy
!=
1145 dri2_ctx
->base
.ResetNotificationStrategy
) {
1146 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1150 switch (dri2_ctx
->base
.ClientAPI
) {
1151 case EGL_OPENGL_ES_API
:
1152 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1154 api
= __DRI_API_GLES
;
1157 api
= __DRI_API_GLES2
;
1160 api
= __DRI_API_GLES3
;
1163 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1168 case EGL_OPENGL_API
:
1169 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1170 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1171 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1172 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1173 api
= __DRI_API_OPENGL_CORE
;
1175 api
= __DRI_API_OPENGL
;
1178 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1184 /* The config chosen here isn't necessarily
1185 * used for surfaces later.
1186 * A pixmap surface will use the single config.
1187 * This opportunity depends on disabling the
1188 * doubleBufferMode check in
1189 * src/mesa/main/context.c:check_compatible()
1191 if (dri2_config
->dri_double_config
[0])
1192 dri_config
= dri2_config
->dri_double_config
[0];
1194 dri_config
= dri2_config
->dri_single_config
[0];
1196 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
1197 * makes sure the back buffer will always be used.
1199 if (conf
->SurfaceType
& EGL_WINDOW_BIT
)
1200 dri2_ctx
->base
.WindowRenderBuffer
= EGL_BACK_BUFFER
;
1205 if (dri2_dpy
->image_driver
) {
1207 unsigned num_attribs
= 8;
1208 uint32_t ctx_attribs
[8];
1210 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1214 dri2_ctx
->dri_context
=
1215 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1223 dri2_create_context_attribs_error(error
);
1224 } else if (dri2_dpy
->dri2
) {
1225 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1227 unsigned num_attribs
= 8;
1228 uint32_t ctx_attribs
[8];
1230 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1234 dri2_ctx
->dri_context
=
1235 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1243 dri2_create_context_attribs_error(error
);
1245 dri2_ctx
->dri_context
=
1246 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1253 assert(dri2_dpy
->swrast
);
1254 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1256 unsigned num_attribs
= 8;
1257 uint32_t ctx_attribs
[8];
1259 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1263 dri2_ctx
->dri_context
=
1264 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1272 dri2_create_context_attribs_error(error
);
1274 dri2_ctx
->dri_context
=
1275 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1283 if (!dri2_ctx
->dri_context
)
1286 return &dri2_ctx
->base
;
1294 * Called via eglDestroyContext(), drv->API.DestroyContext().
1297 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1299 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1300 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1302 if (_eglPutContext(ctx
)) {
1303 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1311 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1313 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1315 if (!_eglPutSurface(surf
))
1318 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1322 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1325 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1326 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1328 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1329 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1330 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1331 _EGLContext
*old_ctx
;
1332 _EGLSurface
*old_dsurf
, *old_rsurf
;
1333 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1334 __DRIdrawable
*ddraw
, *rdraw
;
1339 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1341 /* make new bindings */
1342 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1343 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1347 /* flush before context switch */
1349 dri2_drv
->glFlush();
1351 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1352 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1353 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1356 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1357 dri2_dpy
->core
->unbindContext(old_cctx
);
1360 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1362 if (unbind
|| dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1363 dri2_destroy_surface(drv
, disp
, old_dsurf
);
1364 dri2_destroy_surface(drv
, disp
, old_rsurf
);
1367 dri2_dpy
->ref_count
++;
1369 EGLDisplay old_disp
= _eglGetDisplayHandle(old_ctx
->Resource
.Display
);
1370 dri2_destroy_context(drv
, disp
, old_ctx
);
1371 dri2_display_release(old_disp
);
1376 /* undo the previous _eglBindContext */
1377 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1378 assert(&dri2_ctx
->base
== ctx
&&
1379 tmp_dsurf
== dsurf
&&
1380 tmp_rsurf
== rsurf
);
1382 _eglPutSurface(dsurf
);
1383 _eglPutSurface(rsurf
);
1384 _eglPutContext(ctx
);
1386 _eglPutSurface(old_dsurf
);
1387 _eglPutSurface(old_rsurf
);
1388 _eglPutContext(old_ctx
);
1390 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1391 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1394 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1399 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1401 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1403 return dri2_surf
->dri_drawable
;
1407 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1410 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1412 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1414 return dri2_drv
->get_proc_address(procname
);
1418 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1419 _EGLConfig
*conf
, void *native_window
,
1420 const EGLint
*attrib_list
)
1422 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1423 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1428 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1429 _EGLConfig
*conf
, void *native_pixmap
,
1430 const EGLint
*attrib_list
)
1432 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1433 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1438 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1439 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1441 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1442 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
1446 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1449 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1450 return dri2_dpy
->vtbl
->swap_interval(drv
, dpy
, surf
, interval
);
1454 * Asks the client API to flush any rendering to the drawable so that we can
1455 * do our swapbuffers.
1458 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1460 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1461 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1463 if (dri2_dpy
->flush
) {
1464 if (dri2_dpy
->flush
->base
.version
>= 4) {
1465 /* We know there's a current context because:
1467 * "If surface is not bound to the calling thread’s current
1468 * context, an EGL_BAD_SURFACE error is generated."
1470 _EGLContext
*ctx
= _eglGetCurrentContext();
1471 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1473 /* From the EGL 1.4 spec (page 52):
1475 * "The contents of ancillary buffers are always undefined
1476 * after calling eglSwapBuffers."
1478 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1480 __DRI2_FLUSH_DRAWABLE
|
1481 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1482 __DRI2_THROTTLE_SWAPBUFFER
);
1484 dri2_dpy
->flush
->flush(dri_drawable
);
1490 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1492 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1493 return dri2_dpy
->vtbl
->swap_buffers(drv
, dpy
, surf
);
1497 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1499 const EGLint
*rects
, EGLint n_rects
)
1501 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1502 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, dpy
, surf
,
1507 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1508 EGLint numRects
, const EGLint
*rects
)
1510 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1511 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, dpy
, surf
, numRects
, rects
);
1515 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1516 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1518 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1519 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, dpy
, surf
, x
, y
, width
, height
);
1523 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1524 void *native_pixmap_target
)
1526 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1527 return dri2_dpy
->vtbl
->copy_buffers(drv
, dpy
, surf
, native_pixmap_target
);
1531 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1533 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1534 return dri2_dpy
->vtbl
->query_buffer_age(drv
, dpy
, surf
);
1538 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1540 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1541 _EGLSurface
*surf
= ctx
->DrawSurface
;
1542 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1546 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1547 * we need to copy fake to real here.*/
1549 if (dri2_dpy
->flush
!= NULL
)
1550 dri2_dpy
->flush
->flush(dri_drawable
);
1556 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1561 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1562 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1569 dri2_bind_tex_image(_EGLDriver
*drv
,
1570 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1572 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1573 struct dri2_egl_context
*dri2_ctx
;
1575 GLint format
, target
;
1576 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1578 ctx
= _eglGetCurrentContext();
1579 dri2_ctx
= dri2_egl_context(ctx
);
1581 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1584 switch (surf
->TextureFormat
) {
1585 case EGL_TEXTURE_RGB
:
1586 format
= __DRI_TEXTURE_FORMAT_RGB
;
1588 case EGL_TEXTURE_RGBA
:
1589 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1592 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1593 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1596 switch (surf
->TextureTarget
) {
1597 case EGL_TEXTURE_2D
:
1598 target
= GL_TEXTURE_2D
;
1601 target
= GL_TEXTURE_2D
;
1602 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1605 dri2_dpy
->tex_buffer
->setTexBuffer2(dri2_ctx
->dri_context
,
1613 dri2_release_tex_image(_EGLDriver
*drv
,
1614 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1616 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1617 struct dri2_egl_context
*dri2_ctx
;
1620 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1622 ctx
= _eglGetCurrentContext();
1623 dri2_ctx
= dri2_egl_context(ctx
);
1625 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1628 switch (surf
->TextureTarget
) {
1629 case EGL_TEXTURE_2D
:
1630 target
= GL_TEXTURE_2D
;
1636 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1637 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1638 dri2_dpy
->tex_buffer
->releaseTexBuffer(dri2_ctx
->dri_context
,
1639 target
, dri_drawable
);
1646 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
1647 EGLenum target
, EGLClientBuffer buffer
,
1648 const EGLint
*attr_list
)
1650 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1651 return dri2_dpy
->vtbl
->create_image(drv
, dpy
, ctx
, target
, buffer
,
1656 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1658 struct dri2_egl_image
*dri2_img
;
1660 if (dri_image
== NULL
) {
1661 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1665 dri2_img
= malloc(sizeof *dri2_img
);
1667 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1671 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1676 dri2_img
->dri_image
= dri_image
;
1678 return &dri2_img
->base
;
1682 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1683 EGLClientBuffer buffer
,
1684 const EGLint
*attr_list
)
1686 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1687 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1688 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1689 __DRIimage
*dri_image
;
1691 if (renderbuffer
== 0) {
1692 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1693 return EGL_NO_IMAGE_KHR
;
1697 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1698 renderbuffer
, NULL
);
1700 return dri2_create_image_from_dri(disp
, dri_image
);
1703 #ifdef HAVE_WAYLAND_PLATFORM
1705 /* This structure describes how a wl_buffer maps to one or more
1706 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1707 * offsets and strides of the planes in the buffer. This table maps a
1708 * wl_drm format code to a description of the planes in the buffer
1709 * that lets us create a __DRIimage for each of the planes. */
1711 static const struct wl_drm_components_descriptor
{
1712 uint32_t dri_components
;
1715 } wl_drm_components
[] = {
1716 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1717 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1718 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1719 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1720 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1724 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1725 EGLClientBuffer _buffer
,
1726 const EGLint
*attr_list
)
1728 struct wl_drm_buffer
*buffer
;
1729 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1730 const struct wl_drm_components_descriptor
*f
;
1731 __DRIimage
*dri_image
;
1732 _EGLImageAttribs attrs
;
1736 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1737 (struct wl_resource
*) _buffer
);
1741 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1742 plane
= attrs
.PlaneWL
;
1743 if (err
!= EGL_SUCCESS
) {
1744 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1748 f
= buffer
->driver_format
;
1749 if (plane
< 0 || plane
>= f
->nplanes
) {
1750 _eglError(EGL_BAD_PARAMETER
,
1751 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1755 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1757 if (dri_image
== NULL
) {
1758 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1762 return dri2_create_image_from_dri(disp
, dri_image
);
1767 dri2_get_sync_values_chromium(_EGLDisplay
*dpy
, _EGLSurface
*surf
,
1768 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1771 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1772 return dri2_dpy
->vtbl
->get_sync_values(dpy
, surf
, ust
, msc
, sbc
);
1776 * Set the error code after a call to
1777 * dri2_egl_image::dri_image::createImageFromTexture.
1780 dri2_create_image_khr_texture_error(int dri_error
)
1784 switch (dri_error
) {
1785 case __DRI_IMAGE_ERROR_SUCCESS
:
1788 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1789 egl_error
= EGL_BAD_ALLOC
;
1792 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1793 egl_error
= EGL_BAD_MATCH
;
1796 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1797 egl_error
= EGL_BAD_PARAMETER
;
1800 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
1801 egl_error
= EGL_BAD_ACCESS
;
1806 egl_error
= EGL_BAD_MATCH
;
1810 _eglError(egl_error
, "dri2_create_image_khr_texture");
1814 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1816 EGLClientBuffer buffer
,
1817 const EGLint
*attr_list
)
1819 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1820 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1821 struct dri2_egl_image
*dri2_img
;
1822 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
1823 _EGLImageAttribs attrs
;
1829 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1830 return EGL_NO_IMAGE_KHR
;
1833 if (_eglParseImageAttribList(&attrs
, disp
, attr_list
) != EGL_SUCCESS
)
1834 return EGL_NO_IMAGE_KHR
;
1837 case EGL_GL_TEXTURE_2D_KHR
:
1839 gl_target
= GL_TEXTURE_2D
;
1841 case EGL_GL_TEXTURE_3D_KHR
:
1842 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1843 depth
= attrs
.GLTextureZOffset
;
1844 gl_target
= GL_TEXTURE_3D
;
1848 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1849 return EGL_NO_IMAGE_KHR
;
1851 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1852 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1853 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1854 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1855 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1856 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1857 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
1858 gl_target
= GL_TEXTURE_CUBE_MAP
;
1861 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1862 return EGL_NO_IMAGE_KHR
;
1865 dri2_img
= malloc(sizeof *dri2_img
);
1867 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1868 return EGL_NO_IMAGE_KHR
;
1871 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1872 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1874 return EGL_NO_IMAGE_KHR
;
1877 dri2_img
->dri_image
=
1878 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
1882 attrs
.GLTextureLevel
,
1885 dri2_create_image_khr_texture_error(error
);
1887 if (!dri2_img
->dri_image
) {
1889 return EGL_NO_IMAGE_KHR
;
1891 return &dri2_img
->base
;
1895 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1896 EGLint attribute
, EGLint
*value
)
1898 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1899 if (!dri2_dpy
->vtbl
->query_surface
)
1900 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
1901 return dri2_dpy
->vtbl
->query_surface(drv
, dpy
, surf
, attribute
, value
);
1904 static struct wl_buffer
*
1905 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1908 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1909 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, dpy
, img
);
1914 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1915 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1917 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1918 EGLint format
, name
, pitch
, err
;
1919 _EGLImageAttribs attrs
;
1920 __DRIimage
*dri_image
;
1922 name
= (EGLint
) (uintptr_t) buffer
;
1924 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1925 if (err
!= EGL_SUCCESS
)
1928 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
1929 attrs
.DRMBufferStrideMESA
<= 0) {
1930 _eglError(EGL_BAD_PARAMETER
,
1931 "bad width, height or stride");
1935 switch (attrs
.DRMBufferFormatMESA
) {
1936 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1937 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1938 pitch
= attrs
.DRMBufferStrideMESA
;
1941 _eglError(EGL_BAD_PARAMETER
,
1942 "dri2_create_image_khr: unsupported pixmap depth");
1947 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1955 return dri2_create_image_from_dri(disp
, dri_image
);
1959 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
1966 * "Required attributes and their values are as follows:
1968 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1970 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1971 * by drm_fourcc.h and used as the pixel_format parameter of the
1972 * drm_mode_fb_cmd2 ioctl."
1976 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1977 * incomplete, EGL_BAD_PARAMETER is generated."
1979 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
1980 !attrs
->DMABufFourCC
.IsPresent
) {
1981 _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
1988 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1989 * specified for a plane's pitch or offset isn't supported by EGL,
1990 * EGL_BAD_ACCESS is generated."
1992 for (i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
1993 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
1994 attrs
->DMABufPlanePitches
[i
].Value
<= 0) {
1995 _eglError(EGL_BAD_ACCESS
, "invalid pitch");
2001 * If <target> is EGL_LINUX_DMA_BUF_EXT, both or neither of the following
2002 * attribute values may be given.
2004 * This is referring to EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT and
2005 * EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, and the same for other planes.
2007 for (i
= 0; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2008 if (attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
!=
2009 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
) {
2010 _eglError(EGL_BAD_PARAMETER
, "modifier attribute lo or hi missing");
2015 /* Although the EGL_EXT_image_dma_buf_import_modifiers spec doesn't
2016 * mandate it, we only accept the same modifier across all planes. */
2017 for (i
= 1; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2018 if (attrs
->DMABufPlaneFds
[i
].IsPresent
) {
2019 if ((attrs
->DMABufPlaneModifiersLo
[0].IsPresent
!=
2020 attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
) ||
2021 (attrs
->DMABufPlaneModifiersLo
[0].Value
!=
2022 attrs
->DMABufPlaneModifiersLo
[i
].Value
) ||
2023 (attrs
->DMABufPlaneModifiersHi
[0].Value
!=
2024 attrs
->DMABufPlaneModifiersHi
[i
].Value
)) {
2025 _eglError(EGL_BAD_PARAMETER
, "modifier attributes not equal");
2034 /* Returns the total number of file descriptors. Zero indicates an error. */
2036 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
2038 unsigned i
, plane_n
;
2040 switch (attrs
->DMABufFourCC
.Value
) {
2042 case DRM_FORMAT_RG88
:
2043 case DRM_FORMAT_GR88
:
2044 case DRM_FORMAT_R16
:
2045 case DRM_FORMAT_GR1616
:
2046 case DRM_FORMAT_RGB332
:
2047 case DRM_FORMAT_BGR233
:
2048 case DRM_FORMAT_XRGB4444
:
2049 case DRM_FORMAT_XBGR4444
:
2050 case DRM_FORMAT_RGBX4444
:
2051 case DRM_FORMAT_BGRX4444
:
2052 case DRM_FORMAT_ARGB4444
:
2053 case DRM_FORMAT_ABGR4444
:
2054 case DRM_FORMAT_RGBA4444
:
2055 case DRM_FORMAT_BGRA4444
:
2056 case DRM_FORMAT_XRGB1555
:
2057 case DRM_FORMAT_XBGR1555
:
2058 case DRM_FORMAT_RGBX5551
:
2059 case DRM_FORMAT_BGRX5551
:
2060 case DRM_FORMAT_ARGB1555
:
2061 case DRM_FORMAT_ABGR1555
:
2062 case DRM_FORMAT_RGBA5551
:
2063 case DRM_FORMAT_BGRA5551
:
2064 case DRM_FORMAT_RGB565
:
2065 case DRM_FORMAT_BGR565
:
2066 case DRM_FORMAT_RGB888
:
2067 case DRM_FORMAT_BGR888
:
2068 case DRM_FORMAT_XRGB8888
:
2069 case DRM_FORMAT_XBGR8888
:
2070 case DRM_FORMAT_RGBX8888
:
2071 case DRM_FORMAT_BGRX8888
:
2072 case DRM_FORMAT_ARGB8888
:
2073 case DRM_FORMAT_ABGR8888
:
2074 case DRM_FORMAT_RGBA8888
:
2075 case DRM_FORMAT_BGRA8888
:
2076 case DRM_FORMAT_XRGB2101010
:
2077 case DRM_FORMAT_XBGR2101010
:
2078 case DRM_FORMAT_RGBX1010102
:
2079 case DRM_FORMAT_BGRX1010102
:
2080 case DRM_FORMAT_ARGB2101010
:
2081 case DRM_FORMAT_ABGR2101010
:
2082 case DRM_FORMAT_RGBA1010102
:
2083 case DRM_FORMAT_BGRA1010102
:
2084 case DRM_FORMAT_YUYV
:
2085 case DRM_FORMAT_YVYU
:
2086 case DRM_FORMAT_UYVY
:
2087 case DRM_FORMAT_VYUY
:
2090 case DRM_FORMAT_NV12
:
2091 case DRM_FORMAT_NV21
:
2092 case DRM_FORMAT_NV16
:
2093 case DRM_FORMAT_NV61
:
2096 case DRM_FORMAT_YUV410
:
2097 case DRM_FORMAT_YVU410
:
2098 case DRM_FORMAT_YUV411
:
2099 case DRM_FORMAT_YVU411
:
2100 case DRM_FORMAT_YUV420
:
2101 case DRM_FORMAT_YVU420
:
2102 case DRM_FORMAT_YUV422
:
2103 case DRM_FORMAT_YVU422
:
2104 case DRM_FORMAT_YUV444
:
2105 case DRM_FORMAT_YVU444
:
2109 _eglError(EGL_BAD_ATTRIBUTE
, "invalid format");
2116 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2117 * incomplete, EGL_BAD_PARAMETER is generated."
2119 for (i
= 0; i
< plane_n
; ++i
) {
2120 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2121 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2122 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2123 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2129 * The spec also says:
2131 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2132 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2133 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2134 * or EGL_DMA_BUF_PLANE3_* attributes are specified."
2136 for (i
= plane_n
; i
< DMA_BUF_MAX_PLANES
; ++i
) {
2137 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2138 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2139 attrs
->DMABufPlanePitches
[i
].IsPresent
||
2140 attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
||
2141 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
) {
2144 * The modifiers extension spec says:
2146 * "Modifiers may modify any attribute of a buffer import, including
2147 * but not limited to adding extra planes to a format which
2148 * otherwise does not have those planes. As an example, a modifier
2149 * may add a plane for an external compression buffer to a
2150 * single-plane format. The exact meaning and effect of any
2151 * modifier is canonically defined by drm_fourcc.h, not as part of
2154 if (attrs
->DMABufPlaneModifiersLo
[i
].IsPresent
&&
2155 attrs
->DMABufPlaneModifiersHi
[i
].IsPresent
)
2158 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2167 dri2_query_dma_buf_formats(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2168 EGLint max
, EGLint
*formats
, EGLint
*count
)
2170 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2171 if (max
< 0 || (max
> 0 && formats
== NULL
)) {
2172 _eglError(EGL_BAD_PARAMETER
, "invalid value for max count of formats");
2176 if (dri2_dpy
->image
->base
.version
< 15 ||
2177 dri2_dpy
->image
->queryDmaBufFormats
== NULL
)
2180 if (!dri2_dpy
->image
->queryDmaBufFormats(dri2_dpy
->dri_screen
, max
,
2188 dri2_query_dma_buf_modifiers(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint format
,
2189 EGLint max
, EGLuint64KHR
*modifiers
,
2190 EGLBoolean
*external_only
, EGLint
*count
)
2192 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2195 _eglError(EGL_BAD_PARAMETER
, "invalid value for max count of formats");
2199 if (max
> 0 && modifiers
== NULL
) {
2200 _eglError(EGL_BAD_PARAMETER
, "invalid modifiers array");
2204 if (dri2_dpy
->image
->base
.version
< 15 ||
2205 dri2_dpy
->image
->queryDmaBufModifiers
== NULL
)
2208 if (dri2_dpy
->image
->queryDmaBufModifiers(dri2_dpy
->dri_screen
, format
,
2210 (unsigned int *) external_only
,
2212 _eglError(EGL_BAD_PARAMETER
, "invalid format");
2222 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2223 * EGL will take a reference to the dma_buf(s) which it will release at any
2224 * time while the EGLDisplay is initialized. It is the responsibility of the
2225 * application to close the dma_buf file descriptors."
2227 * Therefore we must never close or otherwise modify the file descriptors.
2230 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2231 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2233 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2236 _EGLImageAttribs attrs
;
2237 __DRIimage
*dri_image
;
2240 int fds
[DMA_BUF_MAX_PLANES
];
2241 int pitches
[DMA_BUF_MAX_PLANES
];
2242 int offsets
[DMA_BUF_MAX_PLANES
];
2244 bool has_modifier
= false;
2250 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2251 * error EGL_BAD_PARAMETER is generated."
2253 if (buffer
!= NULL
) {
2254 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2258 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2259 if (err
!= EGL_SUCCESS
) {
2260 _eglError(err
, "bad attribute");
2264 if (!dri2_check_dma_buf_attribs(&attrs
))
2267 num_fds
= dri2_check_dma_buf_format(&attrs
);
2271 for (i
= 0; i
< num_fds
; ++i
) {
2272 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2273 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2274 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2277 /* dri2_check_dma_buf_attribs ensures that the modifier, if available,
2278 * will be present in attrs.DMABufPlaneModifiersLo[0] and
2279 * attrs.DMABufPlaneModifiersHi[0] */
2280 if (attrs
.DMABufPlaneModifiersLo
[0].IsPresent
) {
2281 modifier
= (uint64_t) attrs
.DMABufPlaneModifiersHi
[0].Value
<< 32;
2282 modifier
|= (uint64_t) (attrs
.DMABufPlaneModifiersLo
[0].Value
& 0xffffffff);
2283 has_modifier
= true;
2285 modifier
= DRM_FORMAT_MOD_INVALID
;
2289 if (dri2_dpy
->image
->base
.version
< 15 ||
2290 dri2_dpy
->image
->createImageFromDmaBufs2
== NULL
) {
2291 _eglError(EGL_BAD_MATCH
, "unsupported dma_buf format modifier");
2292 return EGL_NO_IMAGE_KHR
;
2295 dri2_dpy
->image
->createImageFromDmaBufs2(dri2_dpy
->dri_screen
,
2296 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2297 modifier
, fds
, num_fds
, pitches
, offsets
,
2298 attrs
.DMABufYuvColorSpaceHint
.Value
,
2299 attrs
.DMABufSampleRangeHint
.Value
,
2300 attrs
.DMABufChromaHorizontalSiting
.Value
,
2301 attrs
.DMABufChromaVerticalSiting
.Value
,
2307 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2308 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2309 fds
, num_fds
, pitches
, offsets
,
2310 attrs
.DMABufYuvColorSpaceHint
.Value
,
2311 attrs
.DMABufSampleRangeHint
.Value
,
2312 attrs
.DMABufChromaHorizontalSiting
.Value
,
2313 attrs
.DMABufChromaVerticalSiting
.Value
,
2317 dri2_create_image_khr_texture_error(error
);
2320 return EGL_NO_IMAGE_KHR
;
2322 res
= dri2_create_image_from_dri(disp
, dri_image
);
2327 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2328 const EGLint
*attr_list
)
2330 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2331 struct dri2_egl_image
*dri2_img
;
2332 _EGLImageAttribs attrs
;
2333 unsigned int dri_use
, valid_mask
;
2335 EGLint err
= EGL_SUCCESS
;
2339 dri2_img
= malloc(sizeof *dri2_img
);
2341 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2342 return EGL_NO_IMAGE_KHR
;
2346 err
= EGL_BAD_PARAMETER
;
2350 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
2351 err
= EGL_BAD_PARAMETER
;
2355 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2356 if (err
!= EGL_SUCCESS
)
2359 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2360 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)",
2361 attrs
.Width
, attrs
.Height
);
2365 switch (attrs
.DRMBufferFormatMESA
) {
2366 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2367 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2370 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x",
2371 attrs
.DRMBufferFormatMESA
);
2376 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2377 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2378 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2379 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2380 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x",
2381 attrs
.DRMBufferUseMESA
& ~valid_mask
);
2386 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2387 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2388 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2389 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2390 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2391 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2393 dri2_img
->dri_image
=
2394 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2395 attrs
.Width
, attrs
.Height
,
2396 format
, dri_use
, dri2_img
);
2397 if (dri2_img
->dri_image
== NULL
) {
2398 err
= EGL_BAD_ALLOC
;
2402 return &dri2_img
->base
;
2406 _eglError(err
, "dri2_create_drm_image_mesa");
2408 return EGL_NO_IMAGE_KHR
;
2412 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2413 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2415 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2416 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2420 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2421 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
2422 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2427 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2428 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2431 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2432 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2438 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2440 EGLint
*fourcc
, EGLint
*nplanes
,
2441 EGLuint64KHR
*modifiers
)
2443 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2444 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2450 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2451 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2453 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2454 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2463 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2464 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2466 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2467 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2471 /* rework later to provide multiple fds/strides/offsets */
2473 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2474 __DRI_IMAGE_ATTRIB_FD
, fds
);
2477 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2478 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2482 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2483 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2485 offsets
[0] = img_offset
;
2496 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2497 _EGLContext
*ctx
, EGLenum target
,
2498 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2503 case EGL_GL_TEXTURE_2D_KHR
:
2504 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2505 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2506 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2507 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2508 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2509 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2510 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2511 case EGL_GL_TEXTURE_3D_KHR
:
2512 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
2513 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2516 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2517 return EGL_NO_IMAGE_KHR
;
2519 case EGL_GL_RENDERBUFFER_KHR
:
2520 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2522 case EGL_DRM_BUFFER_MESA
:
2523 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2524 case EGL_LINUX_DMA_BUF_EXT
:
2525 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2527 #ifdef HAVE_WAYLAND_PLATFORM
2528 case EGL_WAYLAND_BUFFER_WL
:
2529 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2532 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2533 return EGL_NO_IMAGE_KHR
;
2538 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2540 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2541 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2545 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2551 #ifdef HAVE_WAYLAND_PLATFORM
2554 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2555 struct wl_drm_buffer
*buffer
)
2557 _EGLDisplay
*disp
= user_data
;
2558 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2560 int i
, dri_components
= 0;
2563 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2572 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2584 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2586 buffer
->driver_format
= NULL
;
2587 for (i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2588 if (wl_drm_components
[i
].dri_components
== dri_components
)
2589 buffer
->driver_format
= &wl_drm_components
[i
];
2591 if (buffer
->driver_format
== NULL
)
2592 dri2_dpy
->image
->destroyImage(img
);
2594 buffer
->driver_buffer
= img
;
2598 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2600 _EGLDisplay
*disp
= user_data
;
2601 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2603 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2606 static struct wayland_drm_callbacks wl_drm_callbacks
= {
2607 .authenticate
= NULL
,
2608 .reference_buffer
= dri2_wl_reference_buffer
,
2609 .release_buffer
= dri2_wl_release_buffer
2613 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2614 struct wl_display
*wl_dpy
)
2616 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2622 if (dri2_dpy
->wl_server_drm
)
2625 wl_drm_callbacks
.authenticate
=
2626 (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
;
2628 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2629 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2630 dri2_dpy
->image
->base
.version
>= 7 &&
2631 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2632 flags
|= WAYLAND_DRM_PRIME
;
2634 dri2_dpy
->wl_server_drm
=
2635 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2636 &wl_drm_callbacks
, disp
, flags
);
2638 if (!dri2_dpy
->wl_server_drm
)
2641 #ifdef HAVE_DRM_PLATFORM
2642 /* We have to share the wl_drm instance with gbm, so gbm can convert
2643 * wl_buffers to gbm bos. */
2644 if (dri2_dpy
->gbm_dri
)
2645 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2652 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2653 struct wl_display
*wl_dpy
)
2655 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2659 if (!dri2_dpy
->wl_server_drm
)
2662 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2663 dri2_dpy
->wl_server_drm
= NULL
;
2669 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2670 struct wl_resource
*buffer_resource
,
2671 EGLint attribute
, EGLint
*value
)
2673 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2674 struct wl_drm_buffer
*buffer
;
2675 const struct wl_drm_components_descriptor
*format
;
2677 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2681 format
= buffer
->driver_format
;
2682 switch (attribute
) {
2683 case EGL_TEXTURE_FORMAT
:
2684 *value
= format
->components
;
2687 *value
= buffer
->width
;
2690 *value
= buffer
->height
;
2699 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2701 p_atomic_inc(&sync
->refcount
);
2705 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2706 struct dri2_egl_sync
*dri2_sync
)
2708 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2709 switch (dri2_sync
->base
.Type
) {
2710 case EGL_SYNC_REUSABLE_KHR
:
2711 cnd_destroy(&dri2_sync
->cond
);
2713 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2714 if (dri2_sync
->base
.SyncFd
!= EGL_NO_NATIVE_FENCE_FD_ANDROID
)
2715 close(dri2_sync
->base
.SyncFd
);
2721 if (dri2_sync
->fence
)
2722 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2729 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
2730 EGLenum type
, const EGLAttrib
*attrib_list
)
2732 _EGLContext
*ctx
= _eglGetCurrentContext();
2733 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2734 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2735 struct dri2_egl_sync
*dri2_sync
;
2737 pthread_condattr_t attr
;
2739 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2741 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2745 if (!_eglInitSync(&dri2_sync
->base
, dpy
, type
, attrib_list
)) {
2751 case EGL_SYNC_FENCE_KHR
:
2752 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2753 if (!dri2_sync
->fence
) {
2754 /* Why did it fail? DRI doesn't return an error code, so we emit
2755 * a generic EGL error that doesn't communicate user error.
2757 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2763 case EGL_SYNC_CL_EVENT_KHR
:
2764 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2765 dri2_dpy
->dri_screen
,
2766 dri2_sync
->base
.CLEvent
);
2767 /* this can only happen if the cl_event passed in is invalid. */
2768 if (!dri2_sync
->fence
) {
2769 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2774 /* the initial status must be "signaled" if the cl_event is signaled */
2775 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2776 dri2_sync
->fence
, 0, 0))
2777 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2780 case EGL_SYNC_REUSABLE_KHR
:
2781 /* intialize attr */
2782 ret
= pthread_condattr_init(&attr
);
2785 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2790 /* change clock attribute to CLOCK_MONOTONIC */
2791 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
2794 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2799 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
2802 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2807 /* initial status of reusable sync must be "unsignaled" */
2808 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
2811 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2812 if (dri2_dpy
->fence
->create_fence_fd
) {
2813 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence_fd(
2814 dri2_ctx
->dri_context
,
2815 dri2_sync
->base
.SyncFd
);
2817 if (!dri2_sync
->fence
) {
2818 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2825 p_atomic_set(&dri2_sync
->refcount
, 1);
2826 return &dri2_sync
->base
;
2830 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2832 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2833 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2834 EGLint ret
= EGL_TRUE
;
2837 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
2838 * then unlock all threads possibly blocked by the reusable sync before
2841 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
2842 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2843 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2844 /* unblock all threads currently blocked by sync */
2845 err
= cnd_broadcast(&dri2_sync
->cond
);
2848 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
2853 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2859 dri2_dup_native_fence_fd(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2861 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2862 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2864 assert(sync
->Type
== EGL_SYNC_NATIVE_FENCE_ANDROID
);
2866 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
2867 /* try to retrieve the actual native fence fd.. if rendering is
2868 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
2870 sync
->SyncFd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
2874 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
2875 /* if native fence fd still not created, return an error: */
2876 _eglError(EGL_BAD_PARAMETER
, "eglDupNativeFenceFDANDROID");
2877 return EGL_NO_NATIVE_FENCE_FD_ANDROID
;
2880 return dup(sync
->SyncFd
);
2884 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2885 EGLint flags
, EGLTime timeout
)
2887 _EGLContext
*ctx
= _eglGetCurrentContext();
2888 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2889 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2890 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2891 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2892 unsigned wait_flags
= 0;
2894 /* timespecs for cnd_timedwait */
2895 struct timespec current
;
2898 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
2900 /* The EGL_KHR_fence_sync spec states:
2902 * "If no context is current for the bound API,
2903 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
2905 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
2906 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
2908 /* the sync object should take a reference while waiting */
2909 dri2_egl_ref_sync(dri2_sync
);
2911 switch (sync
->Type
) {
2912 case EGL_SYNC_FENCE_KHR
:
2913 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2914 case EGL_SYNC_CL_EVENT_KHR
:
2915 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
2916 dri2_sync
->fence
, wait_flags
,
2918 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2920 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2923 case EGL_SYNC_REUSABLE_KHR
:
2924 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
2925 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
2926 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
2927 dri2_drv
->glFlush();
2930 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
2931 if (timeout
== EGL_FOREVER_KHR
) {
2932 mtx_lock(&dri2_sync
->mutex
);
2933 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
2934 mtx_unlock(&dri2_sync
->mutex
);
2936 /* if reusable sync has not been yet signaled */
2937 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
2938 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
2940 /* calculating when to expire */
2941 expire
.nsec
= timeout
% 1000000000L;
2942 expire
.sec
= timeout
/ 1000000000L;
2944 expire
.nsec
+= current
.tv_nsec
;
2945 expire
.sec
+= current
.tv_sec
;
2947 /* expire.nsec now is a number between 0 and 1999999998 */
2948 if (expire
.nsec
> 999999999L) {
2950 expire
.nsec
-= 1000000000L;
2953 mtx_lock(&dri2_sync
->mutex
);
2954 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
2955 mtx_unlock(&dri2_sync
->mutex
);
2957 if (ret
== thrd_busy
) {
2958 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2959 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2961 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2969 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2975 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2978 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2981 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
) {
2982 _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
2986 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
) {
2987 _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
2991 dri2_sync
->base
.SyncStatus
= mode
;
2993 if (mode
== EGL_SIGNALED_KHR
) {
2994 ret
= cnd_broadcast(&dri2_sync
->cond
);
2996 /* fail to broadcast */
2998 _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
3007 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
3009 _EGLContext
*ctx
= _eglGetCurrentContext();
3010 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
3011 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3012 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
3014 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
3015 dri2_sync
->fence
, 0);
3020 dri2_interop_query_device_info(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
3021 struct mesa_glinterop_device_info
*out
)
3023 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
3024 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3026 if (!dri2_dpy
->interop
)
3027 return MESA_GLINTEROP_UNSUPPORTED
;
3029 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
3033 dri2_interop_export_object(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
3034 struct mesa_glinterop_export_in
*in
,
3035 struct mesa_glinterop_export_out
*out
)
3037 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
3038 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
3040 if (!dri2_dpy
->interop
)
3041 return MESA_GLINTEROP_UNSUPPORTED
;
3043 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
3047 dri2_unload(_EGLDriver
*drv
)
3049 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
3051 dlclose(dri2_drv
->handle
);
3056 dri2_load(_EGLDriver
*drv
)
3058 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
3059 #ifdef HAVE_ANDROID_PLATFORM
3060 const char *libname
= "libglapi.so";
3061 #elif defined(__APPLE__)
3062 const char *libname
= "libglapi.0.dylib";
3063 #elif defined(__CYGWIN__)
3064 const char *libname
= "cygglapi-0.dll";
3066 const char *libname
= "libglapi.so.0";
3070 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
3071 handle
= dlopen(libname
, RTLD_LAZY
| RTLD_GLOBAL
);
3073 _eglLog(_EGL_WARNING
, "DRI2: failed to open glapi provider");
3077 dri2_drv
->get_proc_address
= (_EGLProc (*)(const char *))
3078 dlsym(handle
, "_glapi_get_proc_address");
3080 /* if glapi is not available, loading DRI drivers will fail */
3081 if (!dri2_drv
->get_proc_address
) {
3082 _eglLog(_EGL_WARNING
, "DRI2: failed to find _glapi_get_proc_address");
3086 dri2_drv
->glFlush
= (void (*)(void))
3087 dri2_drv
->get_proc_address("glFlush");
3089 /* if glFlush is not available things are horribly broken */
3090 if (!dri2_drv
->glFlush
) {
3091 _eglLog(_EGL_WARNING
, "DRI2: failed to find glFlush entry point");
3095 dri2_drv
->handle
= handle
;
3105 * This is the main entrypoint into the driver, called by libEGL.
3106 * Create a new _EGLDriver object and init its dispatch table.
3109 _eglBuiltInDriverDRI2(const char *args
)
3111 struct dri2_egl_driver
*dri2_drv
;
3115 dri2_drv
= calloc(1, sizeof *dri2_drv
);
3119 if (!dri2_load(&dri2_drv
->base
)) {
3124 _eglInitDriverFallbacks(&dri2_drv
->base
);
3125 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
3126 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
3127 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
3128 dri2_drv
->base
.API
.DestroyContext
= dri2_destroy_context
;
3129 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
3130 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
3131 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
3132 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
3133 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
3134 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
3135 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
3136 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
3137 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
3138 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
3139 dri2_drv
->base
.API
.SwapInterval
= dri2_swap_interval
;
3140 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
3141 dri2_drv
->base
.API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
3142 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
3143 dri2_drv
->base
.API
.PostSubBufferNV
= dri2_post_sub_buffer
;
3144 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
,
3145 dri2_drv
->base
.API
.QueryBufferAge
= dri2_query_buffer_age
;
3146 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image
;
3147 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
3148 dri2_drv
->base
.API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
3149 dri2_drv
->base
.API
.QuerySurface
= dri2_query_surface
;
3151 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
3152 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
3153 dri2_drv
->base
.API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
3154 dri2_drv
->base
.API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
3155 dri2_drv
->base
.API
.QueryDmaBufFormatsEXT
= dri2_query_dma_buf_formats
;
3156 dri2_drv
->base
.API
.QueryDmaBufModifiersEXT
= dri2_query_dma_buf_modifiers
;
3158 #ifdef HAVE_WAYLAND_PLATFORM
3159 dri2_drv
->base
.API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
3160 dri2_drv
->base
.API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
3161 dri2_drv
->base
.API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
3163 dri2_drv
->base
.API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
3164 dri2_drv
->base
.API
.CreateSyncKHR
= dri2_create_sync
;
3165 dri2_drv
->base
.API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
3166 dri2_drv
->base
.API
.SignalSyncKHR
= dri2_signal_sync
;
3167 dri2_drv
->base
.API
.WaitSyncKHR
= dri2_server_wait_sync
;
3168 dri2_drv
->base
.API
.DestroySyncKHR
= dri2_destroy_sync
;
3169 dri2_drv
->base
.API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
3170 dri2_drv
->base
.API
.GLInteropExportObject
= dri2_interop_export_object
;
3171 dri2_drv
->base
.API
.DupNativeFenceFDANDROID
= dri2_dup_native_fence_fd
;
3173 dri2_drv
->base
.Name
= "DRI2";
3174 dri2_drv
->base
.Unload
= dri2_unload
;
3176 return &dri2_drv
->base
;