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"
59 #include "util/u_atomic.h"
61 /* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
62 * some of the definitions here so that building Mesa won't bleeding-edge
66 #define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
69 #ifndef DRM_FORMAT_RG88
70 #define DRM_FORMAT_RG88 fourcc_code('R', 'G', '8', '8') /* [15:0] R:G 8:8 little endian */
73 #ifndef DRM_FORMAT_GR88
74 #define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
77 const __DRIuseInvalidateExtension use_invalidate
= {
78 .base
= { __DRI_USE_INVALIDATE
, 1 }
81 EGLint dri2_to_egl_attribute_map
[] = {
83 EGL_BUFFER_SIZE
, /* __DRI_ATTRIB_BUFFER_SIZE */
84 EGL_LEVEL
, /* __DRI_ATTRIB_LEVEL */
85 EGL_RED_SIZE
, /* __DRI_ATTRIB_RED_SIZE */
86 EGL_GREEN_SIZE
, /* __DRI_ATTRIB_GREEN_SIZE */
87 EGL_BLUE_SIZE
, /* __DRI_ATTRIB_BLUE_SIZE */
88 EGL_LUMINANCE_SIZE
, /* __DRI_ATTRIB_LUMINANCE_SIZE */
89 EGL_ALPHA_SIZE
, /* __DRI_ATTRIB_ALPHA_SIZE */
90 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
91 EGL_DEPTH_SIZE
, /* __DRI_ATTRIB_DEPTH_SIZE */
92 EGL_STENCIL_SIZE
, /* __DRI_ATTRIB_STENCIL_SIZE */
93 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
94 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
95 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
96 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
97 EGL_SAMPLE_BUFFERS
, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
98 EGL_SAMPLES
, /* __DRI_ATTRIB_SAMPLES */
99 0, /* __DRI_ATTRIB_RENDER_TYPE, */
100 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
101 0, /* __DRI_ATTRIB_CONFORMANT */
102 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
103 0, /* __DRI_ATTRIB_STEREO */
104 0, /* __DRI_ATTRIB_AUX_BUFFERS */
105 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
106 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
107 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
108 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
109 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
110 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
111 0, /* __DRI_ATTRIB_FLOAT_MODE (deprecated) */
112 0, /* __DRI_ATTRIB_RED_MASK */
113 0, /* __DRI_ATTRIB_GREEN_MASK */
114 0, /* __DRI_ATTRIB_BLUE_MASK */
115 0, /* __DRI_ATTRIB_ALPHA_MASK */
116 EGL_MAX_PBUFFER_WIDTH
, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
117 EGL_MAX_PBUFFER_HEIGHT
, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
118 EGL_MAX_PBUFFER_PIXELS
, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
119 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
120 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
121 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
122 0, /* __DRI_ATTRIB_SWAP_METHOD */
123 EGL_MAX_SWAP_INTERVAL
, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
124 EGL_MIN_SWAP_INTERVAL
, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
125 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
126 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
127 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
128 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
129 EGL_Y_INVERTED_NOK
, /* __DRI_ATTRIB_YINVERTED */
130 0, /* __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE */
134 dri2_get_dri_config(struct dri2_egl_config
*conf
, EGLint surface_type
,
137 const bool srgb
= colorspace
== EGL_GL_COLORSPACE_SRGB_KHR
;
139 return surface_type
== EGL_WINDOW_BIT
? conf
->dri_double_config
[srgb
] :
140 conf
->dri_single_config
[srgb
];
144 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
146 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
149 if (!_eglMatchConfig(conf
, criteria
))
155 struct dri2_egl_config
*
156 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
157 EGLint surface_type
, const EGLint
*attr_list
,
158 const unsigned int *rgba_masks
)
160 struct dri2_egl_config
*conf
;
161 struct dri2_egl_display
*dri2_dpy
;
163 unsigned int attrib
, value
, double_buffer
;
165 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
166 unsigned int dri_masks
[4] = { 0, 0, 0, 0 };
167 _EGLConfig
*matching_config
;
168 EGLint num_configs
= 0;
172 dri2_dpy
= disp
->DriverData
;
173 _eglInitConfig(&base
, disp
, id
);
177 bind_to_texture_rgb
= 0;
178 bind_to_texture_rgba
= 0;
180 while (dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
++, &attrib
, &value
)) {
182 case __DRI_ATTRIB_RENDER_TYPE
:
183 if (value
& __DRI_ATTRIB_RGBA_BIT
)
184 value
= EGL_RGB_BUFFER
;
185 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
186 value
= EGL_LUMINANCE_BUFFER
;
189 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
192 case __DRI_ATTRIB_CONFIG_CAVEAT
:
193 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
194 value
= EGL_NON_CONFORMANT_CONFIG
;
195 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
196 value
= EGL_SLOW_CONFIG
;
199 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
202 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
203 bind_to_texture_rgb
= value
;
206 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
207 bind_to_texture_rgba
= value
;
210 case __DRI_ATTRIB_DOUBLE_BUFFER
:
211 double_buffer
= value
;
214 case __DRI_ATTRIB_RED_MASK
:
215 dri_masks
[0] = value
;
218 case __DRI_ATTRIB_GREEN_MASK
:
219 dri_masks
[1] = value
;
222 case __DRI_ATTRIB_BLUE_MASK
:
223 dri_masks
[2] = value
;
226 case __DRI_ATTRIB_ALPHA_MASK
:
227 dri_masks
[3] = value
;
230 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
231 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
232 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
233 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
234 /* Don't expose visuals with the accumulation buffer. */
239 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
241 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
246 key
= dri2_to_egl_attribute_map
[attrib
];
248 _eglSetConfigKey(&base
, key
, value
);
254 for (i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
255 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
257 if (rgba_masks
&& memcmp(rgba_masks
, dri_masks
, sizeof(dri_masks
)))
260 base
.NativeRenderable
= EGL_TRUE
;
262 base
.SurfaceType
= surface_type
;
263 if (surface_type
& (EGL_PBUFFER_BIT
|
264 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
265 base
.BindToTextureRGB
= bind_to_texture_rgb
;
266 if (base
.AlphaSize
> 0)
267 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
270 base
.RenderableType
= disp
->ClientAPIs
;
271 base
.Conformant
= disp
->ClientAPIs
;
273 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
274 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
276 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
277 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
281 config_id
= base
.ConfigID
;
282 base
.ConfigID
= EGL_DONT_CARE
;
283 base
.SurfaceType
= EGL_DONT_CARE
;
284 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
285 (_EGLArrayForEach
) dri2_match_config
, &base
);
287 if (num_configs
== 1) {
288 conf
= (struct dri2_egl_config
*) matching_config
;
290 if (double_buffer
&& !conf
->dri_double_config
[srgb
])
291 conf
->dri_double_config
[srgb
] = dri_config
;
292 else if (!double_buffer
&& !conf
->dri_single_config
[srgb
])
293 conf
->dri_single_config
[srgb
] = dri_config
;
295 /* a similar config type is already added (unlikely) => discard */
298 else if (num_configs
== 0) {
299 conf
= calloc(1, sizeof *conf
);
304 conf
->dri_double_config
[srgb
] = dri_config
;
306 conf
->dri_single_config
[srgb
] = dri_config
;
308 memcpy(&conf
->base
, &base
, sizeof base
);
309 conf
->base
.SurfaceType
= 0;
310 conf
->base
.ConfigID
= config_id
;
312 _eglLinkConfig(&conf
->base
);
320 surface_type
&= ~EGL_PIXMAP_BIT
;
323 conf
->base
.SurfaceType
|= surface_type
;
329 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
331 _EGLDisplay
*disp
= data
;
332 struct dri2_egl_image
*dri2_img
;
337 img
= _eglLookupImage(image
, disp
);
339 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
343 dri2_img
= dri2_egl_image(image
);
345 return dri2_img
->dri_image
;
348 const __DRIimageLookupExtension image_lookup_extension
= {
349 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
351 .lookupEGLImage
= dri2_lookup_egl_image
354 struct dri2_extension_match
{
360 static struct dri2_extension_match dri3_driver_extensions
[] = {
361 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
362 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
366 static struct dri2_extension_match dri2_driver_extensions
[] = {
367 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
368 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
372 static struct dri2_extension_match dri2_core_extensions
[] = {
373 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
374 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
375 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
379 static struct dri2_extension_match swrast_driver_extensions
[] = {
380 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
381 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
385 static struct dri2_extension_match swrast_core_extensions
[] = {
386 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
391 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
392 struct dri2_extension_match
*matches
,
393 const __DRIextension
**extensions
)
395 int i
, j
, ret
= EGL_TRUE
;
398 for (i
= 0; extensions
[i
]; i
++) {
399 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
400 for (j
= 0; matches
[j
].name
; j
++) {
401 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
402 extensions
[i
]->version
>= matches
[j
].version
) {
403 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
404 *(const __DRIextension
**) field
= extensions
[i
];
405 _eglLog(_EGL_INFO
, "found extension %s version %d",
406 extensions
[i
]->name
, extensions
[i
]->version
);
411 for (j
= 0; matches
[j
].name
; j
++) {
412 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
413 if (*(const __DRIextension
**) field
== NULL
) {
414 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
415 matches
[j
].name
, matches
[j
].version
);
423 static const __DRIextension
**
424 dri2_open_driver(_EGLDisplay
*disp
)
426 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
427 const __DRIextension
**extensions
= NULL
;
428 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
429 char *get_extensions_name
;
430 const __DRIextension
**(*get_extensions
)(void);
433 if (geteuid() == getuid()) {
434 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
435 search_paths
= getenv("LIBGL_DRIVERS_PATH");
437 if (search_paths
== NULL
)
438 search_paths
= DEFAULT_DRIVER_DIR
;
440 dri2_dpy
->driver
= NULL
;
441 end
= search_paths
+ strlen(search_paths
);
442 for (p
= search_paths
; p
< end
; p
= next
+ 1) {
444 next
= strchr(p
, ':');
450 snprintf(path
, sizeof path
,
451 "%.*s/tls/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
452 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
454 if (dri2_dpy
->driver
== NULL
) {
455 snprintf(path
, sizeof path
,
456 "%.*s/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
457 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
458 if (dri2_dpy
->driver
== NULL
)
459 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
461 /* not need continue to loop all paths once the driver is found */
462 if (dri2_dpy
->driver
!= NULL
)
466 snprintf(path
, sizeof path
, "%.*s/gallium_dri.so", len
, p
);
467 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
468 if (dri2_dpy
->driver
== NULL
)
469 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
475 if (dri2_dpy
->driver
== NULL
) {
476 _eglLog(_EGL_WARNING
,
477 "DRI2: failed to open %s (search paths %s)",
478 dri2_dpy
->driver_name
, search_paths
);
482 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
484 if (asprintf(&get_extensions_name
, "%s_%s",
485 __DRI_DRIVER_GET_EXTENSIONS
, dri2_dpy
->driver_name
) != -1) {
486 get_extensions
= dlsym(dri2_dpy
->driver
, get_extensions_name
);
487 if (get_extensions
) {
488 extensions
= get_extensions();
490 _eglLog(_EGL_DEBUG
, "driver does not expose %s(): %s\n",
491 get_extensions_name
, dlerror());
493 free(get_extensions_name
);
497 extensions
= dlsym(dri2_dpy
->driver
, __DRI_DRIVER_EXTENSIONS
);
498 if (extensions
== NULL
) {
499 _eglLog(_EGL_WARNING
,
500 "DRI2: driver exports no extensions (%s)", dlerror());
501 dlclose(dri2_dpy
->driver
);
508 dri2_load_driver_dri3(_EGLDisplay
*disp
)
510 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
511 const __DRIextension
**extensions
;
513 extensions
= dri2_open_driver(disp
);
517 if (!dri2_bind_extensions(dri2_dpy
, dri3_driver_extensions
, extensions
)) {
518 dlclose(dri2_dpy
->driver
);
521 dri2_dpy
->driver_extensions
= extensions
;
527 dri2_load_driver(_EGLDisplay
*disp
)
529 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
530 const __DRIextension
**extensions
;
532 extensions
= dri2_open_driver(disp
);
536 if (!dri2_bind_extensions(dri2_dpy
, dri2_driver_extensions
, extensions
)) {
537 dlclose(dri2_dpy
->driver
);
540 dri2_dpy
->driver_extensions
= extensions
;
546 dri2_load_driver_swrast(_EGLDisplay
*disp
)
548 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
549 const __DRIextension
**extensions
;
551 extensions
= dri2_open_driver(disp
);
555 if (!dri2_bind_extensions(dri2_dpy
, swrast_driver_extensions
, extensions
)) {
556 dlclose(dri2_dpy
->driver
);
559 dri2_dpy
->driver_extensions
= extensions
;
565 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
567 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
568 unsigned int value
= 0;
570 if (!rendererQuery
||
571 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
578 dri2_setup_screen(_EGLDisplay
*disp
)
580 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
581 unsigned int api_mask
;
583 if (dri2_dpy
->image_driver
) {
584 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
585 } else if (dri2_dpy
->dri2
) {
586 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
588 assert(dri2_dpy
->swrast
);
589 api_mask
= 1 << __DRI_API_OPENGL
|
590 1 << __DRI_API_GLES
|
591 1 << __DRI_API_GLES2
|
592 1 << __DRI_API_GLES3
;
595 disp
->ClientAPIs
= 0;
596 if (api_mask
& (1 <<__DRI_API_OPENGL
))
597 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
598 if (api_mask
& (1 <<__DRI_API_GLES
))
599 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
600 if (api_mask
& (1 << __DRI_API_GLES2
))
601 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
602 if (api_mask
& (1 << __DRI_API_GLES3
))
603 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
605 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
606 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
607 disp
->Extensions
.MESA_configless_context
= EGL_TRUE
;
609 if (dri2_renderer_query_integer(dri2_dpy
,
610 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
611 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
613 if (dri2_dpy
->image_driver
||
614 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
615 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
616 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
618 if (dri2_dpy
->robustness
)
619 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
622 if (dri2_dpy
->fence
) {
623 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
624 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
625 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
626 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
629 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
631 if (dri2_dpy
->image
) {
632 if (dri2_dpy
->image
->base
.version
>= 10 &&
633 dri2_dpy
->image
->getCapabilities
!= NULL
) {
636 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
637 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
639 if (dri2_dpy
->image
->base
.version
>= 11)
640 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
642 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
643 if (dri2_dpy
->image
->base
.version
>= 11)
644 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
647 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
648 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
649 if (dri2_dpy
->image
->base
.version
>= 5 &&
650 dri2_dpy
->image
->createImageFromTexture
) {
651 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
652 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
654 if (dri2_renderer_query_integer(dri2_dpy
,
655 __DRI2_RENDERER_HAS_TEXTURE_3D
))
656 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
658 if (dri2_dpy
->image
->base
.version
>= 8 &&
659 dri2_dpy
->image
->createImageFromDmaBufs
) {
660 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
666 /* All platforms but DRM call this function to create the screen, query the
667 * dri extensions, setup the vtables and populate the driver_configs.
668 * DRM inherits all that information from its display - GBM.
671 dri2_create_screen(_EGLDisplay
*disp
)
673 const __DRIextension
**extensions
;
674 struct dri2_egl_display
*dri2_dpy
;
677 dri2_dpy
= disp
->DriverData
;
679 if (dri2_dpy
->image_driver
) {
680 dri2_dpy
->dri_screen
=
681 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
682 dri2_dpy
->extensions
,
683 dri2_dpy
->driver_extensions
,
684 &dri2_dpy
->driver_configs
,
686 } else if (dri2_dpy
->dri2
) {
687 if (dri2_dpy
->dri2
->base
.version
>= 4) {
688 dri2_dpy
->dri_screen
=
689 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
690 dri2_dpy
->extensions
,
691 dri2_dpy
->driver_extensions
,
692 &dri2_dpy
->driver_configs
, disp
);
694 dri2_dpy
->dri_screen
=
695 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
696 dri2_dpy
->extensions
,
697 &dri2_dpy
->driver_configs
, disp
);
700 assert(dri2_dpy
->swrast
);
701 if (dri2_dpy
->swrast
->base
.version
>= 4) {
702 dri2_dpy
->dri_screen
=
703 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->extensions
,
704 dri2_dpy
->driver_extensions
,
705 &dri2_dpy
->driver_configs
, disp
);
707 dri2_dpy
->dri_screen
=
708 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->extensions
,
709 &dri2_dpy
->driver_configs
, disp
);
713 if (dri2_dpy
->dri_screen
== NULL
) {
714 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
718 dri2_dpy
->own_dri_screen
= 1;
720 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
722 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
) {
723 if (!dri2_bind_extensions(dri2_dpy
, dri2_core_extensions
, extensions
))
724 goto cleanup_dri_screen
;
726 assert(dri2_dpy
->swrast
);
727 if (!dri2_bind_extensions(dri2_dpy
, swrast_core_extensions
, extensions
))
728 goto cleanup_dri_screen
;
731 for (i
= 0; extensions
[i
]; i
++) {
732 if (strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0) {
733 dri2_dpy
->robustness
= (__DRIrobustnessExtension
*) extensions
[i
];
735 if (strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0) {
736 dri2_dpy
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
738 if (strcmp(extensions
[i
]->name
, __DRI2_FENCE
) == 0) {
739 dri2_dpy
->fence
= (__DRI2fenceExtension
*) extensions
[i
];
741 if (strcmp(extensions
[i
]->name
, __DRI2_RENDERER_QUERY
) == 0) {
742 dri2_dpy
->rendererQuery
= (__DRI2rendererQueryExtension
*) extensions
[i
];
744 if (strcmp(extensions
[i
]->name
, __DRI2_INTEROP
) == 0)
745 dri2_dpy
->interop
= (__DRI2interopExtension
*) extensions
[i
];
748 dri2_setup_screen(disp
);
753 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
759 * Called via eglInitialize(), GLX_drv->API.Initialize().
762 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
764 /* not until swrast_dri is supported */
765 if (disp
->Options
.UseFallback
)
768 switch (disp
->Platform
) {
769 #ifdef HAVE_SURFACELESS_PLATFORM
770 case _EGL_PLATFORM_SURFACELESS
:
771 if (disp
->Options
.TestOnly
)
773 return dri2_initialize_surfaceless(drv
, disp
);
776 #ifdef HAVE_X11_PLATFORM
777 case _EGL_PLATFORM_X11
:
778 if (disp
->Options
.TestOnly
)
780 return dri2_initialize_x11(drv
, disp
);
783 #ifdef HAVE_DRM_PLATFORM
784 case _EGL_PLATFORM_DRM
:
785 if (disp
->Options
.TestOnly
)
787 return dri2_initialize_drm(drv
, disp
);
789 #ifdef HAVE_WAYLAND_PLATFORM
790 case _EGL_PLATFORM_WAYLAND
:
791 if (disp
->Options
.TestOnly
)
793 return dri2_initialize_wayland(drv
, disp
);
795 #ifdef HAVE_ANDROID_PLATFORM
796 case _EGL_PLATFORM_ANDROID
:
797 if (disp
->Options
.TestOnly
)
799 return dri2_initialize_android(drv
, disp
);
803 _eglLog(_EGL_WARNING
, "No EGL platform enabled.");
809 * Called via eglTerminate(), drv->API.Terminate().
812 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
814 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
817 _eglReleaseDisplayResources(drv
, disp
);
818 _eglCleanupDisplay(disp
);
820 if (dri2_dpy
->own_dri_screen
)
821 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
822 if (dri2_dpy
->fd
>= 0)
824 if (dri2_dpy
->driver
)
825 dlclose(dri2_dpy
->driver
);
826 free(dri2_dpy
->device_name
);
827 free(dri2_dpy
->driver_name
);
829 switch (disp
->Platform
) {
830 #ifdef HAVE_X11_PLATFORM
831 case _EGL_PLATFORM_X11
:
832 if (dri2_dpy
->own_device
) {
833 xcb_disconnect(dri2_dpy
->conn
);
837 #ifdef HAVE_DRM_PLATFORM
838 case _EGL_PLATFORM_DRM
:
839 if (dri2_dpy
->own_device
) {
840 gbm_device_destroy(&dri2_dpy
->gbm_dri
->base
.base
);
844 #ifdef HAVE_WAYLAND_PLATFORM
845 case _EGL_PLATFORM_WAYLAND
:
846 if (dri2_dpy
->wl_drm
)
847 wl_drm_destroy(dri2_dpy
->wl_drm
);
848 if (dri2_dpy
->wl_shm
)
849 wl_shm_destroy(dri2_dpy
->wl_shm
);
850 wl_registry_destroy(dri2_dpy
->wl_registry
);
851 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
852 if (dri2_dpy
->own_device
) {
853 wl_display_disconnect(dri2_dpy
->wl_dpy
);
861 /* The drm platform does not create the screen/driver_configs but reuses
862 * the ones from the gbm device. As such the gbm itself is responsible
865 if (disp
->Platform
!= _EGL_PLATFORM_DRM
) {
866 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
867 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
868 free(dri2_dpy
->driver_configs
);
871 disp
->DriverData
= NULL
;
877 * Set the error code after a call to
878 * dri2_egl_display::dri2::createContextAttribs.
881 dri2_create_context_attribs_error(int dri_error
)
886 case __DRI_CTX_ERROR_SUCCESS
:
889 case __DRI_CTX_ERROR_NO_MEMORY
:
890 egl_error
= EGL_BAD_ALLOC
;
893 /* From the EGL_KHR_create_context spec, section "Errors":
895 * * If <config> does not support a client API context compatible
896 * with the requested API major and minor version, [...] context flags,
897 * and context reset notification behavior (for client API types where
898 * these attributes are supported), then an EGL_BAD_MATCH error is
901 * * If an OpenGL ES context is requested and the values for
902 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
903 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
904 * is not defined, than an EGL_BAD_MATCH error is generated.
906 * * If an OpenGL context is requested, the requested version is
907 * greater than 3.2, and the value for attribute
908 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
909 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
910 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
911 * one of these bits set; or if the implementation does not support
912 * the requested profile, then an EGL_BAD_MATCH error is generated.
914 case __DRI_CTX_ERROR_BAD_API
:
915 case __DRI_CTX_ERROR_BAD_VERSION
:
916 case __DRI_CTX_ERROR_BAD_FLAG
:
917 egl_error
= EGL_BAD_MATCH
;
920 /* From the EGL_KHR_create_context spec, section "Errors":
922 * * If an attribute name or attribute value in <attrib_list> is not
923 * recognized (including unrecognized bits in bitmask attributes),
924 * then an EGL_BAD_ATTRIBUTE error is generated."
926 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
927 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
928 egl_error
= EGL_BAD_ATTRIBUTE
;
933 egl_error
= EGL_BAD_MATCH
;
937 _eglError(egl_error
, "dri2_create_context");
941 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
942 struct dri2_egl_display
*dri2_dpy
,
943 uint32_t *ctx_attribs
,
944 unsigned *num_attribs
)
948 assert(*num_attribs
>= 8);
950 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
951 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
952 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
953 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
955 if (dri2_ctx
->base
.Flags
!= 0) {
956 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
957 * extension, don't even try to send it the robust-access flag.
958 * It may explode. Instead, generate the required EGL error here.
960 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
961 && !dri2_dpy
->robustness
) {
962 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
966 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
967 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
;
970 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
971 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
972 * extension, don't even try to send it a reset strategy. It may
973 * explode. Instead, generate the required EGL error here.
975 if (!dri2_dpy
->robustness
) {
976 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
980 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
981 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
990 * Called via eglCreateContext(), drv->API.CreateContext().
993 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
994 _EGLContext
*share_list
, const EGLint
*attrib_list
)
996 struct dri2_egl_context
*dri2_ctx
;
997 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
998 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
999 __DRIcontext
*shared
=
1000 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1001 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1002 const __DRIconfig
*dri_config
;
1007 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1009 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1013 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1016 switch (dri2_ctx
->base
.ClientAPI
) {
1017 case EGL_OPENGL_ES_API
:
1018 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1020 api
= __DRI_API_GLES
;
1023 api
= __DRI_API_GLES2
;
1026 api
= __DRI_API_GLES3
;
1029 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1034 case EGL_OPENGL_API
:
1035 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1036 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1037 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1038 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1039 api
= __DRI_API_OPENGL_CORE
;
1041 api
= __DRI_API_OPENGL
;
1044 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1050 /* The config chosen here isn't necessarily
1051 * used for surfaces later.
1052 * A pixmap surface will use the single config.
1053 * This opportunity depends on disabling the
1054 * doubleBufferMode check in
1055 * src/mesa/main/context.c:check_compatible()
1057 if (dri2_config
->dri_double_config
[0])
1058 dri_config
= dri2_config
->dri_double_config
[0];
1060 dri_config
= dri2_config
->dri_single_config
[0];
1062 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
1063 * makes sure the back buffer will always be used.
1065 if (conf
->SurfaceType
& EGL_WINDOW_BIT
)
1066 dri2_ctx
->base
.WindowRenderBuffer
= EGL_BACK_BUFFER
;
1071 if (dri2_dpy
->image_driver
) {
1073 unsigned num_attribs
= 8;
1074 uint32_t ctx_attribs
[8];
1076 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1080 dri2_ctx
->dri_context
=
1081 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1089 dri2_create_context_attribs_error(error
);
1090 } else if (dri2_dpy
->dri2
) {
1091 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1093 unsigned num_attribs
= 8;
1094 uint32_t ctx_attribs
[8];
1096 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1100 dri2_ctx
->dri_context
=
1101 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1109 dri2_create_context_attribs_error(error
);
1111 dri2_ctx
->dri_context
=
1112 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1119 assert(dri2_dpy
->swrast
);
1120 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1122 unsigned num_attribs
= 8;
1123 uint32_t ctx_attribs
[8];
1125 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1129 dri2_ctx
->dri_context
=
1130 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1138 dri2_create_context_attribs_error(error
);
1140 dri2_ctx
->dri_context
=
1141 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1149 if (!dri2_ctx
->dri_context
)
1152 return &dri2_ctx
->base
;
1160 * Called via eglDestroyContext(), drv->API.DestroyContext().
1163 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1165 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1166 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1168 if (_eglPutContext(ctx
)) {
1169 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1177 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1180 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1181 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1183 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1184 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1185 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1186 _EGLContext
*old_ctx
;
1187 _EGLSurface
*old_dsurf
, *old_rsurf
;
1188 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1189 __DRIdrawable
*ddraw
, *rdraw
;
1192 /* make new bindings */
1193 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1194 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1198 /* flush before context switch */
1199 if (old_ctx
&& dri2_drv
->glFlush
)
1200 dri2_drv
->glFlush();
1202 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1203 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1204 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1207 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1208 dri2_dpy
->core
->unbindContext(old_cctx
);
1211 if ((cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
) ||
1212 dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1214 drv
->API
.DestroySurface(drv
, disp
, old_dsurf
);
1216 drv
->API
.DestroySurface(drv
, disp
, old_rsurf
);
1218 drv
->API
.DestroyContext(drv
, disp
, old_ctx
);
1222 /* undo the previous _eglBindContext */
1223 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1224 assert(&dri2_ctx
->base
== ctx
&&
1225 tmp_dsurf
== dsurf
&&
1226 tmp_rsurf
== rsurf
);
1228 _eglPutSurface(dsurf
);
1229 _eglPutSurface(rsurf
);
1230 _eglPutContext(ctx
);
1232 _eglPutSurface(old_dsurf
);
1233 _eglPutSurface(old_rsurf
);
1234 _eglPutContext(old_ctx
);
1236 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1237 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1240 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1245 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1247 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1249 return dri2_surf
->dri_drawable
;
1253 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1256 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1258 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1260 return dri2_drv
->get_proc_address(procname
);
1264 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1265 _EGLConfig
*conf
, void *native_window
,
1266 const EGLint
*attrib_list
)
1268 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1269 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1274 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1275 _EGLConfig
*conf
, void *native_pixmap
,
1276 const EGLint
*attrib_list
)
1278 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1279 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1284 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1285 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1287 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1288 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
1292 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1294 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1295 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1299 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1302 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1303 return dri2_dpy
->vtbl
->swap_interval(drv
, dpy
, surf
, interval
);
1307 * Asks the client API to flush any rendering to the drawable so that we can
1308 * do our swapbuffers.
1311 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1313 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1314 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1316 if (dri2_dpy
->flush
) {
1317 if (dri2_dpy
->flush
->base
.version
>= 4) {
1318 /* We know there's a current context because:
1320 * "If surface is not bound to the calling thread’s current
1321 * context, an EGL_BAD_SURFACE error is generated."
1323 _EGLContext
*ctx
= _eglGetCurrentContext();
1324 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1326 /* From the EGL 1.4 spec (page 52):
1328 * "The contents of ancillary buffers are always undefined
1329 * after calling eglSwapBuffers."
1331 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1333 __DRI2_FLUSH_DRAWABLE
|
1334 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1335 __DRI2_THROTTLE_SWAPBUFFER
);
1337 dri2_dpy
->flush
->flush(dri_drawable
);
1343 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1345 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1346 return dri2_dpy
->vtbl
->swap_buffers(drv
, dpy
, surf
);
1350 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1352 const EGLint
*rects
, EGLint n_rects
)
1354 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1355 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, dpy
, surf
,
1360 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1361 EGLint numRects
, const EGLint
*rects
)
1363 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1364 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, dpy
, surf
, numRects
, rects
);
1368 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1369 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1371 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1372 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, dpy
, surf
, x
, y
, width
, height
);
1376 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1377 void *native_pixmap_target
)
1379 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1380 return dri2_dpy
->vtbl
->copy_buffers(drv
, dpy
, surf
, native_pixmap_target
);
1384 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1386 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1387 return dri2_dpy
->vtbl
->query_buffer_age(drv
, dpy
, surf
);
1391 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1393 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1394 _EGLSurface
*surf
= ctx
->DrawSurface
;
1395 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1399 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1400 * we need to copy fake to real here.*/
1402 if (dri2_dpy
->flush
!= NULL
)
1403 dri2_dpy
->flush
->flush(dri_drawable
);
1409 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1414 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1415 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1422 dri2_bind_tex_image(_EGLDriver
*drv
,
1423 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1425 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1426 struct dri2_egl_context
*dri2_ctx
;
1428 GLint format
, target
;
1429 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1431 ctx
= _eglGetCurrentContext();
1432 dri2_ctx
= dri2_egl_context(ctx
);
1434 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1437 switch (surf
->TextureFormat
) {
1438 case EGL_TEXTURE_RGB
:
1439 format
= __DRI_TEXTURE_FORMAT_RGB
;
1441 case EGL_TEXTURE_RGBA
:
1442 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1445 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1446 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1449 switch (surf
->TextureTarget
) {
1450 case EGL_TEXTURE_2D
:
1451 target
= GL_TEXTURE_2D
;
1454 target
= GL_TEXTURE_2D
;
1455 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1458 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1466 dri2_release_tex_image(_EGLDriver
*drv
,
1467 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1469 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1470 struct dri2_egl_context
*dri2_ctx
;
1473 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1475 ctx
= _eglGetCurrentContext();
1476 dri2_ctx
= dri2_egl_context(ctx
);
1478 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1481 switch (surf
->TextureTarget
) {
1482 case EGL_TEXTURE_2D
:
1483 target
= GL_TEXTURE_2D
;
1489 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1490 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1491 (*dri2_dpy
->tex_buffer
->releaseTexBuffer
)(dri2_ctx
->dri_context
,
1500 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
1501 EGLenum target
, EGLClientBuffer buffer
,
1502 const EGLint
*attr_list
)
1504 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1505 return dri2_dpy
->vtbl
->create_image(drv
, dpy
, ctx
, target
, buffer
,
1510 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1512 struct dri2_egl_image
*dri2_img
;
1514 if (dri_image
== NULL
) {
1515 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1519 dri2_img
= malloc(sizeof *dri2_img
);
1521 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1525 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1530 dri2_img
->dri_image
= dri_image
;
1532 return &dri2_img
->base
;
1536 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1537 EGLClientBuffer buffer
,
1538 const EGLint
*attr_list
)
1540 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1541 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1542 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1543 __DRIimage
*dri_image
;
1545 if (renderbuffer
== 0) {
1546 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1547 return EGL_NO_IMAGE_KHR
;
1551 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1552 renderbuffer
, NULL
);
1554 return dri2_create_image_from_dri(disp
, dri_image
);
1557 #ifdef HAVE_WAYLAND_PLATFORM
1559 /* This structure describes how a wl_buffer maps to one or more
1560 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1561 * offsets and strides of the planes in the buffer. This table maps a
1562 * wl_drm format code to a description of the planes in the buffer
1563 * that lets us create a __DRIimage for each of the planes. */
1565 static const struct wl_drm_components_descriptor
{
1566 uint32_t dri_components
;
1569 } wl_drm_components
[] = {
1570 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1571 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1572 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1573 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1574 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1578 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1579 EGLClientBuffer _buffer
,
1580 const EGLint
*attr_list
)
1582 struct wl_drm_buffer
*buffer
;
1583 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1584 const struct wl_drm_components_descriptor
*f
;
1585 __DRIimage
*dri_image
;
1586 _EGLImageAttribs attrs
;
1590 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1591 (struct wl_resource
*) _buffer
);
1595 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1596 plane
= attrs
.PlaneWL
;
1597 if (err
!= EGL_SUCCESS
) {
1598 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1602 f
= buffer
->driver_format
;
1603 if (plane
< 0 || plane
>= f
->nplanes
) {
1604 _eglError(EGL_BAD_PARAMETER
,
1605 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1609 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1611 if (dri_image
== NULL
) {
1612 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1616 return dri2_create_image_from_dri(disp
, dri_image
);
1621 dri2_get_sync_values_chromium(_EGLDisplay
*dpy
, _EGLSurface
*surf
,
1622 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1625 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1626 return dri2_dpy
->vtbl
->get_sync_values(dpy
, surf
, ust
, msc
, sbc
);
1630 * Set the error code after a call to
1631 * dri2_egl_image::dri_image::createImageFromTexture.
1634 dri2_create_image_khr_texture_error(int dri_error
)
1638 switch (dri_error
) {
1639 case __DRI_IMAGE_ERROR_SUCCESS
:
1642 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1643 egl_error
= EGL_BAD_ALLOC
;
1646 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1647 egl_error
= EGL_BAD_MATCH
;
1650 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1651 egl_error
= EGL_BAD_PARAMETER
;
1654 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
1655 egl_error
= EGL_BAD_ACCESS
;
1660 egl_error
= EGL_BAD_MATCH
;
1664 _eglError(egl_error
, "dri2_create_image_khr_texture");
1668 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1670 EGLClientBuffer buffer
,
1671 const EGLint
*attr_list
)
1673 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1674 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1675 struct dri2_egl_image
*dri2_img
;
1676 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
1677 _EGLImageAttribs attrs
;
1683 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1684 return EGL_NO_IMAGE_KHR
;
1687 if (_eglParseImageAttribList(&attrs
, disp
, attr_list
) != EGL_SUCCESS
)
1688 return EGL_NO_IMAGE_KHR
;
1691 case EGL_GL_TEXTURE_2D_KHR
:
1693 gl_target
= GL_TEXTURE_2D
;
1695 case EGL_GL_TEXTURE_3D_KHR
:
1696 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1697 depth
= attrs
.GLTextureZOffset
;
1698 gl_target
= GL_TEXTURE_3D
;
1702 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1703 return EGL_NO_IMAGE_KHR
;
1705 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1706 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1707 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1708 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1709 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1710 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1711 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
1712 gl_target
= GL_TEXTURE_CUBE_MAP
;
1715 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1716 return EGL_NO_IMAGE_KHR
;
1719 dri2_img
= malloc(sizeof *dri2_img
);
1721 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1722 return EGL_NO_IMAGE_KHR
;
1725 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1726 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1728 return EGL_NO_IMAGE_KHR
;
1731 dri2_img
->dri_image
=
1732 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
1736 attrs
.GLTextureLevel
,
1739 dri2_create_image_khr_texture_error(error
);
1741 if (!dri2_img
->dri_image
) {
1743 return EGL_NO_IMAGE_KHR
;
1745 return &dri2_img
->base
;
1748 static struct wl_buffer
*
1749 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1752 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1753 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, dpy
, img
);
1758 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1759 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1761 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1762 EGLint format
, name
, pitch
, err
;
1763 _EGLImageAttribs attrs
;
1764 __DRIimage
*dri_image
;
1766 name
= (EGLint
) (uintptr_t) buffer
;
1768 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1769 if (err
!= EGL_SUCCESS
)
1772 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
1773 attrs
.DRMBufferStrideMESA
<= 0) {
1774 _eglError(EGL_BAD_PARAMETER
,
1775 "bad width, height or stride");
1779 switch (attrs
.DRMBufferFormatMESA
) {
1780 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1781 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1782 pitch
= attrs
.DRMBufferStrideMESA
;
1785 _eglError(EGL_BAD_PARAMETER
,
1786 "dri2_create_image_khr: unsupported pixmap depth");
1791 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1799 return dri2_create_image_from_dri(disp
, dri_image
);
1803 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
1810 * "Required attributes and their values are as follows:
1812 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1814 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1815 * by drm_fourcc.h and used as the pixel_format parameter of the
1816 * drm_mode_fb_cmd2 ioctl."
1820 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1821 * incomplete, EGL_BAD_PARAMETER is generated."
1823 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
1824 !attrs
->DMABufFourCC
.IsPresent
) {
1825 _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
1832 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1833 * specified for a plane's pitch or offset isn't supported by EGL,
1834 * EGL_BAD_ACCESS is generated."
1836 for (i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
1837 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
1838 attrs
->DMABufPlanePitches
[i
].Value
<= 0) {
1839 _eglError(EGL_BAD_ACCESS
, "invalid pitch");
1847 /* Returns the total number of file descriptors. Zero indicates an error. */
1849 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
1851 unsigned i
, plane_n
;
1853 switch (attrs
->DMABufFourCC
.Value
) {
1855 case DRM_FORMAT_RG88
:
1856 case DRM_FORMAT_GR88
:
1857 case DRM_FORMAT_RGB332
:
1858 case DRM_FORMAT_BGR233
:
1859 case DRM_FORMAT_XRGB4444
:
1860 case DRM_FORMAT_XBGR4444
:
1861 case DRM_FORMAT_RGBX4444
:
1862 case DRM_FORMAT_BGRX4444
:
1863 case DRM_FORMAT_ARGB4444
:
1864 case DRM_FORMAT_ABGR4444
:
1865 case DRM_FORMAT_RGBA4444
:
1866 case DRM_FORMAT_BGRA4444
:
1867 case DRM_FORMAT_XRGB1555
:
1868 case DRM_FORMAT_XBGR1555
:
1869 case DRM_FORMAT_RGBX5551
:
1870 case DRM_FORMAT_BGRX5551
:
1871 case DRM_FORMAT_ARGB1555
:
1872 case DRM_FORMAT_ABGR1555
:
1873 case DRM_FORMAT_RGBA5551
:
1874 case DRM_FORMAT_BGRA5551
:
1875 case DRM_FORMAT_RGB565
:
1876 case DRM_FORMAT_BGR565
:
1877 case DRM_FORMAT_RGB888
:
1878 case DRM_FORMAT_BGR888
:
1879 case DRM_FORMAT_XRGB8888
:
1880 case DRM_FORMAT_XBGR8888
:
1881 case DRM_FORMAT_RGBX8888
:
1882 case DRM_FORMAT_BGRX8888
:
1883 case DRM_FORMAT_ARGB8888
:
1884 case DRM_FORMAT_ABGR8888
:
1885 case DRM_FORMAT_RGBA8888
:
1886 case DRM_FORMAT_BGRA8888
:
1887 case DRM_FORMAT_XRGB2101010
:
1888 case DRM_FORMAT_XBGR2101010
:
1889 case DRM_FORMAT_RGBX1010102
:
1890 case DRM_FORMAT_BGRX1010102
:
1891 case DRM_FORMAT_ARGB2101010
:
1892 case DRM_FORMAT_ABGR2101010
:
1893 case DRM_FORMAT_RGBA1010102
:
1894 case DRM_FORMAT_BGRA1010102
:
1895 case DRM_FORMAT_YUYV
:
1896 case DRM_FORMAT_YVYU
:
1897 case DRM_FORMAT_UYVY
:
1898 case DRM_FORMAT_VYUY
:
1901 case DRM_FORMAT_NV12
:
1902 case DRM_FORMAT_NV21
:
1903 case DRM_FORMAT_NV16
:
1904 case DRM_FORMAT_NV61
:
1907 case DRM_FORMAT_YUV410
:
1908 case DRM_FORMAT_YVU410
:
1909 case DRM_FORMAT_YUV411
:
1910 case DRM_FORMAT_YVU411
:
1911 case DRM_FORMAT_YUV420
:
1912 case DRM_FORMAT_YVU420
:
1913 case DRM_FORMAT_YUV422
:
1914 case DRM_FORMAT_YVU422
:
1915 case DRM_FORMAT_YUV444
:
1916 case DRM_FORMAT_YVU444
:
1920 _eglError(EGL_BAD_ATTRIBUTE
, "invalid format");
1927 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1928 * incomplete, EGL_BAD_PARAMETER is generated."
1930 for (i
= 0; i
< plane_n
; ++i
) {
1931 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
1932 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
1933 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
1934 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
1940 * The spec also says:
1942 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
1943 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
1944 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
1945 * attributes are specified."
1947 for (i
= plane_n
; i
< 3; ++i
) {
1948 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
1949 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
1950 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
1951 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
1962 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
1963 * EGL will take a reference to the dma_buf(s) which it will release at any
1964 * time while the EGLDisplay is initialized. It is the responsibility of the
1965 * application to close the dma_buf file descriptors."
1967 * Therefore we must never close or otherwise modify the file descriptors.
1970 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1971 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1973 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1976 _EGLImageAttribs attrs
;
1977 __DRIimage
*dri_image
;
1988 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
1989 * error EGL_BAD_PARAMETER is generated."
1991 if (buffer
!= NULL
) {
1992 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
1996 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1997 if (err
!= EGL_SUCCESS
) {
1998 _eglError(err
, "bad attribute");
2002 if (!dri2_check_dma_buf_attribs(&attrs
))
2005 num_fds
= dri2_check_dma_buf_format(&attrs
);
2009 for (i
= 0; i
< num_fds
; ++i
) {
2010 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2011 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2012 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2016 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2017 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2018 fds
, num_fds
, pitches
, offsets
,
2019 attrs
.DMABufYuvColorSpaceHint
.Value
,
2020 attrs
.DMABufSampleRangeHint
.Value
,
2021 attrs
.DMABufChromaHorizontalSiting
.Value
,
2022 attrs
.DMABufChromaVerticalSiting
.Value
,
2025 dri2_create_image_khr_texture_error(error
);
2028 return EGL_NO_IMAGE_KHR
;
2030 res
= dri2_create_image_from_dri(disp
, dri_image
);
2035 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2036 const EGLint
*attr_list
)
2038 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2039 struct dri2_egl_image
*dri2_img
;
2040 _EGLImageAttribs attrs
;
2041 unsigned int dri_use
, valid_mask
;
2043 EGLint err
= EGL_SUCCESS
;
2047 dri2_img
= malloc(sizeof *dri2_img
);
2049 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2050 return EGL_NO_IMAGE_KHR
;
2054 err
= EGL_BAD_PARAMETER
;
2058 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
2059 err
= EGL_BAD_PARAMETER
;
2063 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2064 if (err
!= EGL_SUCCESS
)
2067 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2068 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)",
2069 attrs
.Width
, attrs
.Height
);
2073 switch (attrs
.DRMBufferFormatMESA
) {
2074 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2075 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2078 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x",
2079 attrs
.DRMBufferFormatMESA
);
2084 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2085 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2086 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2087 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2088 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x",
2089 attrs
.DRMBufferUseMESA
& ~valid_mask
);
2094 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2095 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2096 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2097 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2098 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2099 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2101 dri2_img
->dri_image
=
2102 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2103 attrs
.Width
, attrs
.Height
,
2104 format
, dri_use
, dri2_img
);
2105 if (dri2_img
->dri_image
== NULL
) {
2106 err
= EGL_BAD_ALLOC
;
2110 return &dri2_img
->base
;
2114 _eglError(err
, "dri2_create_drm_image_mesa");
2116 return EGL_NO_IMAGE_KHR
;
2120 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2121 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2123 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2124 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2128 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2129 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
2130 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2135 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2136 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2139 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2140 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2146 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2148 EGLint
*fourcc
, EGLint
*nplanes
,
2149 EGLuint64KHR
*modifiers
)
2151 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2152 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2158 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2159 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2161 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2162 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2171 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2172 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2174 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2175 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2179 /* rework later to provide multiple fds/strides/offsets */
2181 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2182 __DRI_IMAGE_ATTRIB_FD
, fds
);
2185 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2186 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2197 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2198 _EGLContext
*ctx
, EGLenum target
,
2199 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2204 case EGL_GL_TEXTURE_2D_KHR
:
2205 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2206 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2207 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2208 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2209 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2210 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2211 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2212 case EGL_GL_TEXTURE_3D_KHR
:
2213 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
2214 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2217 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2218 return EGL_NO_IMAGE_KHR
;
2220 case EGL_GL_RENDERBUFFER_KHR
:
2221 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2223 case EGL_DRM_BUFFER_MESA
:
2224 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2225 case EGL_LINUX_DMA_BUF_EXT
:
2226 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2228 #ifdef HAVE_WAYLAND_PLATFORM
2229 case EGL_WAYLAND_BUFFER_WL
:
2230 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2233 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2234 return EGL_NO_IMAGE_KHR
;
2239 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2241 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2242 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2246 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2252 #ifdef HAVE_WAYLAND_PLATFORM
2255 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2256 struct wl_drm_buffer
*buffer
)
2258 _EGLDisplay
*disp
= user_data
;
2259 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2261 int i
, dri_components
= 0;
2264 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2273 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2285 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2287 buffer
->driver_format
= NULL
;
2288 for (i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2289 if (wl_drm_components
[i
].dri_components
== dri_components
)
2290 buffer
->driver_format
= &wl_drm_components
[i
];
2292 if (buffer
->driver_format
== NULL
)
2293 dri2_dpy
->image
->destroyImage(img
);
2295 buffer
->driver_buffer
= img
;
2299 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2301 _EGLDisplay
*disp
= user_data
;
2302 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2304 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2307 static struct wayland_drm_callbacks wl_drm_callbacks
= {
2308 .authenticate
= NULL
,
2309 .reference_buffer
= dri2_wl_reference_buffer
,
2310 .release_buffer
= dri2_wl_release_buffer
2314 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2315 struct wl_display
*wl_dpy
)
2317 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2323 if (dri2_dpy
->wl_server_drm
)
2326 wl_drm_callbacks
.authenticate
=
2327 (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
;
2329 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2330 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2331 dri2_dpy
->image
->base
.version
>= 7 &&
2332 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2333 flags
|= WAYLAND_DRM_PRIME
;
2335 dri2_dpy
->wl_server_drm
=
2336 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2337 &wl_drm_callbacks
, disp
, flags
);
2339 if (!dri2_dpy
->wl_server_drm
)
2342 #ifdef HAVE_DRM_PLATFORM
2343 /* We have to share the wl_drm instance with gbm, so gbm can convert
2344 * wl_buffers to gbm bos. */
2345 if (dri2_dpy
->gbm_dri
)
2346 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2353 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2354 struct wl_display
*wl_dpy
)
2356 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2360 if (!dri2_dpy
->wl_server_drm
)
2363 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2364 dri2_dpy
->wl_server_drm
= NULL
;
2370 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2371 struct wl_resource
*buffer_resource
,
2372 EGLint attribute
, EGLint
*value
)
2374 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2375 struct wl_drm_buffer
*buffer
;
2376 const struct wl_drm_components_descriptor
*format
;
2378 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2382 format
= buffer
->driver_format
;
2383 switch (attribute
) {
2384 case EGL_TEXTURE_FORMAT
:
2385 *value
= format
->components
;
2388 *value
= buffer
->width
;
2391 *value
= buffer
->height
;
2400 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2402 p_atomic_inc(&sync
->refcount
);
2406 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2407 struct dri2_egl_sync
*dri2_sync
)
2409 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2410 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
)
2411 cnd_destroy(&dri2_sync
->cond
);
2413 if (dri2_sync
->fence
)
2414 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2421 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
2422 EGLenum type
, const EGLint
*attrib_list
,
2423 const EGLAttrib
*attrib_list64
)
2425 _EGLContext
*ctx
= _eglGetCurrentContext();
2426 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2427 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2428 struct dri2_egl_sync
*dri2_sync
;
2430 pthread_condattr_t attr
;
2432 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2434 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2438 if (!_eglInitSync(&dri2_sync
->base
, dpy
, type
, attrib_list
,
2445 case EGL_SYNC_FENCE_KHR
:
2446 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2447 if (!dri2_sync
->fence
) {
2448 /* Why did it fail? DRI doesn't return an error code, so we emit
2449 * a generic EGL error that doesn't communicate user error.
2451 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2457 case EGL_SYNC_CL_EVENT_KHR
:
2458 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2459 dri2_dpy
->dri_screen
,
2460 dri2_sync
->base
.CLEvent
);
2461 /* this can only happen if the cl_event passed in is invalid. */
2462 if (!dri2_sync
->fence
) {
2463 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2468 /* the initial status must be "signaled" if the cl_event is signaled */
2469 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2470 dri2_sync
->fence
, 0, 0))
2471 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2474 case EGL_SYNC_REUSABLE_KHR
:
2475 /* intialize attr */
2476 ret
= pthread_condattr_init(&attr
);
2479 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2484 /* change clock attribute to CLOCK_MONOTONIC */
2485 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
2488 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2493 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
2496 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2501 /* initial status of reusable sync must be "unsignaled" */
2502 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
2506 p_atomic_set(&dri2_sync
->refcount
, 1);
2507 return &dri2_sync
->base
;
2511 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2513 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2514 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2515 EGLint ret
= EGL_TRUE
;
2518 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
2519 * then unlock all threads possibly blocked by the reusable sync before
2522 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
2523 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2524 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2525 /* unblock all threads currently blocked by sync */
2526 err
= cnd_broadcast(&dri2_sync
->cond
);
2529 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
2533 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2539 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2540 EGLint flags
, EGLTime timeout
)
2542 _EGLContext
*ctx
= _eglGetCurrentContext();
2543 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2544 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2545 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2546 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2547 unsigned wait_flags
= 0;
2549 /* timespecs for cnd_timedwait */
2550 struct timespec current
;
2553 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
2555 /* The EGL_KHR_fence_sync spec states:
2557 * "If no context is current for the bound API,
2558 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
2560 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
2561 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
2563 /* the sync object should take a reference while waiting */
2564 dri2_egl_ref_sync(dri2_sync
);
2566 switch (sync
->Type
) {
2567 case EGL_SYNC_FENCE_KHR
:
2568 case EGL_SYNC_CL_EVENT_KHR
:
2569 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
2570 dri2_sync
->fence
, wait_flags
,
2572 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2574 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2577 case EGL_SYNC_REUSABLE_KHR
:
2578 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
2579 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
2580 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
2581 if (dri2_drv
->glFlush
)
2582 dri2_drv
->glFlush();
2585 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
2586 if (timeout
== EGL_FOREVER_KHR
) {
2587 if (mtx_lock(&dri2_sync
->mutex
)) {
2592 ret
= cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
2594 mtx_unlock(&dri2_sync
->mutex
);
2597 _eglError(EGL_BAD_PARAMETER
, "eglClientWaitSyncKHR");
2601 /* if reusable sync has not been yet signaled */
2602 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
2603 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
2605 /* calculating when to expire */
2606 expire
.nsec
= timeout
% 1000000000L;
2607 expire
.sec
= timeout
/ 1000000000L;
2609 expire
.nsec
+= current
.tv_nsec
;
2610 expire
.sec
+= current
.tv_sec
;
2612 /* expire.nsec now is a number between 0 and 1999999998 */
2613 if (expire
.nsec
> 999999999L) {
2615 expire
.nsec
-= 1000000000L;
2618 if (mtx_lock(&dri2_sync
->mutex
)) {
2623 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
2625 mtx_unlock(&dri2_sync
->mutex
);
2628 if (ret
== thrd_busy
) {
2629 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2630 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2632 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2642 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2644 if (ret
== EGL_FALSE
) {
2645 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2653 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2656 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2659 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
) {
2660 _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
2664 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
) {
2665 _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
2669 dri2_sync
->base
.SyncStatus
= mode
;
2671 if (mode
== EGL_SIGNALED_KHR
) {
2672 ret
= cnd_broadcast(&dri2_sync
->cond
);
2674 /* fail to broadcast */
2676 _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
2685 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2687 _EGLContext
*ctx
= _eglGetCurrentContext();
2688 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2689 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2690 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2692 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
2693 dri2_sync
->fence
, 0);
2698 dri2_interop_query_device_info(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2699 struct mesa_glinterop_device_info
*out
)
2701 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2702 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2704 if (!dri2_dpy
->interop
)
2705 return MESA_GLINTEROP_UNSUPPORTED
;
2707 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
2711 dri2_interop_export_object(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2712 struct mesa_glinterop_export_in
*in
,
2713 struct mesa_glinterop_export_out
*out
)
2715 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2716 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2718 if (!dri2_dpy
->interop
)
2719 return MESA_GLINTEROP_UNSUPPORTED
;
2721 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
2725 dri2_unload(_EGLDriver
*drv
)
2727 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2729 if (dri2_drv
->handle
)
2730 dlclose(dri2_drv
->handle
);
2735 dri2_load(_EGLDriver
*drv
)
2737 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2738 #ifdef HAVE_ANDROID_PLATFORM
2739 const char *libname
= "libglapi.so";
2740 #elif defined(__APPLE__)
2741 const char *libname
= "libglapi.0.dylib";
2742 #elif defined(__CYGWIN__)
2743 const char *libname
= "cygglapi-0.dll";
2745 const char *libname
= "libglapi.so.0";
2749 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2750 handle
= dlopen(libname
, RTLD_LAZY
| RTLD_GLOBAL
);
2752 dri2_drv
->get_proc_address
= (_EGLProc (*)(const char *))
2753 dlsym(handle
, "_glapi_get_proc_address");
2754 if (!dri2_drv
->get_proc_address
|| !libname
) {
2755 /* no need to keep a reference */
2761 /* if glapi is not available, loading DRI drivers will fail */
2762 if (!dri2_drv
->get_proc_address
) {
2763 _eglLog(_EGL_WARNING
, "DRI2: failed to find _glapi_get_proc_address");
2767 dri2_drv
->glFlush
= (void (*)(void))
2768 dri2_drv
->get_proc_address("glFlush");
2770 dri2_drv
->handle
= handle
;
2776 * This is the main entrypoint into the driver, called by libEGL.
2777 * Create a new _EGLDriver object and init its dispatch table.
2780 _eglBuiltInDriverDRI2(const char *args
)
2782 struct dri2_egl_driver
*dri2_drv
;
2786 dri2_drv
= calloc(1, sizeof *dri2_drv
);
2790 if (!dri2_load(&dri2_drv
->base
)) {
2795 _eglInitDriverFallbacks(&dri2_drv
->base
);
2796 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
2797 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
2798 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
2799 dri2_drv
->base
.API
.DestroyContext
= dri2_destroy_context
;
2800 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
2801 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
2802 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
2803 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
2804 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
2805 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
2806 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
2807 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
2808 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
2809 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
2810 dri2_drv
->base
.API
.SwapInterval
= dri2_swap_interval
;
2811 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
2812 dri2_drv
->base
.API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
2813 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
2814 dri2_drv
->base
.API
.PostSubBufferNV
= dri2_post_sub_buffer
;
2815 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
,
2816 dri2_drv
->base
.API
.QueryBufferAge
= dri2_query_buffer_age
;
2817 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image
;
2818 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
2819 dri2_drv
->base
.API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
2821 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
2822 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
2823 dri2_drv
->base
.API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
2824 dri2_drv
->base
.API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
2826 #ifdef HAVE_WAYLAND_PLATFORM
2827 dri2_drv
->base
.API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
2828 dri2_drv
->base
.API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
2829 dri2_drv
->base
.API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
2831 dri2_drv
->base
.API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
2832 dri2_drv
->base
.API
.CreateSyncKHR
= dri2_create_sync
;
2833 dri2_drv
->base
.API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
2834 dri2_drv
->base
.API
.SignalSyncKHR
= dri2_signal_sync
;
2835 dri2_drv
->base
.API
.WaitSyncKHR
= dri2_server_wait_sync
;
2836 dri2_drv
->base
.API
.DestroySyncKHR
= dri2_destroy_sync
;
2837 dri2_drv
->base
.API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
2838 dri2_drv
->base
.API
.GLInteropExportObject
= dri2_interop_export_object
;
2840 dri2_drv
->base
.Name
= "DRI2";
2841 dri2_drv
->base
.Unload
= dri2_unload
;
2843 return &dri2_drv
->base
;