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().
761 * This must be guaranteed to be called exactly once, even if eglInitialize is
762 * called many times (without a eglTerminate in between).
765 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
767 EGLBoolean ret
= EGL_FALSE
;
768 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
770 /* In the case where the application calls eglMakeCurrent(context1),
771 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
772 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
773 * initialized, as we need it to be able to free context1 correctly.
775 * It would probably be safest to forcibly release the display with
776 * dri2_display_release, to make sure the display is reinitialized correctly.
777 * However, the EGL spec states that we need to keep a reference to the
778 * current context (so we cannot call dri2_make_current(NULL)), and therefore
779 * we would leak context1 as we would be missing the old display connection
780 * to free it up correctly.
783 dri2_dpy
->ref_count
++;
787 /* not until swrast_dri is supported */
788 if (disp
->Options
.UseFallback
)
791 /* Nothing to initialize for a test only display */
792 if (disp
->Options
.TestOnly
)
795 switch (disp
->Platform
) {
796 #ifdef HAVE_SURFACELESS_PLATFORM
797 case _EGL_PLATFORM_SURFACELESS
:
798 ret
= dri2_initialize_surfaceless(drv
, disp
);
801 #ifdef HAVE_X11_PLATFORM
802 case _EGL_PLATFORM_X11
:
803 ret
= dri2_initialize_x11(drv
, disp
);
806 #ifdef HAVE_DRM_PLATFORM
807 case _EGL_PLATFORM_DRM
:
808 ret
= dri2_initialize_drm(drv
, disp
);
811 #ifdef HAVE_WAYLAND_PLATFORM
812 case _EGL_PLATFORM_WAYLAND
:
813 ret
= dri2_initialize_wayland(drv
, disp
);
816 #ifdef HAVE_ANDROID_PLATFORM
817 case _EGL_PLATFORM_ANDROID
:
818 ret
= dri2_initialize_android(drv
, disp
);
822 _eglLog(_EGL_WARNING
, "No EGL platform enabled.");
827 dri2_dpy
= dri2_egl_display(disp
);
833 dri2_dpy
->ref_count
++;
840 * Decrement display reference count, and free up display if necessary.
843 dri2_display_release(_EGLDisplay
*disp
) {
844 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
847 assert(dri2_dpy
->ref_count
> 0);
848 dri2_dpy
->ref_count
--;
850 if (dri2_dpy
->ref_count
> 0)
853 _eglCleanupDisplay(disp
);
855 if (dri2_dpy
->own_dri_screen
)
856 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
857 if (dri2_dpy
->fd
>= 0)
859 if (dri2_dpy
->driver
)
860 dlclose(dri2_dpy
->driver
);
861 free(dri2_dpy
->device_name
);
862 free(dri2_dpy
->driver_name
);
864 switch (disp
->Platform
) {
865 #ifdef HAVE_X11_PLATFORM
866 case _EGL_PLATFORM_X11
:
867 if (dri2_dpy
->own_device
) {
868 xcb_disconnect(dri2_dpy
->conn
);
872 #ifdef HAVE_DRM_PLATFORM
873 case _EGL_PLATFORM_DRM
:
874 if (dri2_dpy
->own_device
) {
875 gbm_device_destroy(&dri2_dpy
->gbm_dri
->base
.base
);
879 #ifdef HAVE_WAYLAND_PLATFORM
880 case _EGL_PLATFORM_WAYLAND
:
881 if (dri2_dpy
->wl_drm
)
882 wl_drm_destroy(dri2_dpy
->wl_drm
);
883 if (dri2_dpy
->wl_shm
)
884 wl_shm_destroy(dri2_dpy
->wl_shm
);
885 wl_registry_destroy(dri2_dpy
->wl_registry
);
886 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
887 if (dri2_dpy
->own_device
) {
888 wl_display_disconnect(dri2_dpy
->wl_dpy
);
896 /* The drm platform does not create the screen/driver_configs but reuses
897 * the ones from the gbm device. As such the gbm itself is responsible
900 if (disp
->Platform
!= _EGL_PLATFORM_DRM
) {
901 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
902 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
903 free(dri2_dpy
->driver_configs
);
906 disp
->DriverData
= NULL
;
910 * Called via eglTerminate(), drv->API.Terminate().
912 * This must be guaranteed to be called exactly once, even if eglTerminate is
913 * called many times (without a eglInitialize in between).
916 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
918 /* Release all non-current Context/Surfaces. */
919 _eglReleaseDisplayResources(drv
, disp
);
921 dri2_display_release(disp
);
927 * Set the error code after a call to
928 * dri2_egl_display::dri2::createContextAttribs.
931 dri2_create_context_attribs_error(int dri_error
)
936 case __DRI_CTX_ERROR_SUCCESS
:
939 case __DRI_CTX_ERROR_NO_MEMORY
:
940 egl_error
= EGL_BAD_ALLOC
;
943 /* From the EGL_KHR_create_context spec, section "Errors":
945 * * If <config> does not support a client API context compatible
946 * with the requested API major and minor version, [...] context flags,
947 * and context reset notification behavior (for client API types where
948 * these attributes are supported), then an EGL_BAD_MATCH error is
951 * * If an OpenGL ES context is requested and the values for
952 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
953 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
954 * is not defined, than an EGL_BAD_MATCH error is generated.
956 * * If an OpenGL context is requested, the requested version is
957 * greater than 3.2, and the value for attribute
958 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
959 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
960 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
961 * one of these bits set; or if the implementation does not support
962 * the requested profile, then an EGL_BAD_MATCH error is generated.
964 case __DRI_CTX_ERROR_BAD_API
:
965 case __DRI_CTX_ERROR_BAD_VERSION
:
966 case __DRI_CTX_ERROR_BAD_FLAG
:
967 egl_error
= EGL_BAD_MATCH
;
970 /* From the EGL_KHR_create_context spec, section "Errors":
972 * * If an attribute name or attribute value in <attrib_list> is not
973 * recognized (including unrecognized bits in bitmask attributes),
974 * then an EGL_BAD_ATTRIBUTE error is generated."
976 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
977 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
978 egl_error
= EGL_BAD_ATTRIBUTE
;
983 egl_error
= EGL_BAD_MATCH
;
987 _eglError(egl_error
, "dri2_create_context");
991 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
992 struct dri2_egl_display
*dri2_dpy
,
993 uint32_t *ctx_attribs
,
994 unsigned *num_attribs
)
998 assert(*num_attribs
>= 8);
1000 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1001 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1002 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1003 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1005 if (dri2_ctx
->base
.Flags
!= 0) {
1006 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1007 * extension, don't even try to send it the robust-access flag.
1008 * It may explode. Instead, generate the required EGL error here.
1010 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1011 && !dri2_dpy
->robustness
) {
1012 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1016 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1017 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
;
1020 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1021 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1022 * extension, don't even try to send it a reset strategy. It may
1023 * explode. Instead, generate the required EGL error here.
1025 if (!dri2_dpy
->robustness
) {
1026 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1030 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1031 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1040 * Called via eglCreateContext(), drv->API.CreateContext().
1042 static _EGLContext
*
1043 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1044 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1046 struct dri2_egl_context
*dri2_ctx
;
1047 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1048 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1049 __DRIcontext
*shared
=
1050 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1051 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1052 const __DRIconfig
*dri_config
;
1057 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1059 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1063 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1066 switch (dri2_ctx
->base
.ClientAPI
) {
1067 case EGL_OPENGL_ES_API
:
1068 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1070 api
= __DRI_API_GLES
;
1073 api
= __DRI_API_GLES2
;
1076 api
= __DRI_API_GLES3
;
1079 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1084 case EGL_OPENGL_API
:
1085 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1086 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1087 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1088 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1089 api
= __DRI_API_OPENGL_CORE
;
1091 api
= __DRI_API_OPENGL
;
1094 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1100 /* The config chosen here isn't necessarily
1101 * used for surfaces later.
1102 * A pixmap surface will use the single config.
1103 * This opportunity depends on disabling the
1104 * doubleBufferMode check in
1105 * src/mesa/main/context.c:check_compatible()
1107 if (dri2_config
->dri_double_config
[0])
1108 dri_config
= dri2_config
->dri_double_config
[0];
1110 dri_config
= dri2_config
->dri_single_config
[0];
1112 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
1113 * makes sure the back buffer will always be used.
1115 if (conf
->SurfaceType
& EGL_WINDOW_BIT
)
1116 dri2_ctx
->base
.WindowRenderBuffer
= EGL_BACK_BUFFER
;
1121 if (dri2_dpy
->image_driver
) {
1123 unsigned num_attribs
= 8;
1124 uint32_t ctx_attribs
[8];
1126 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1130 dri2_ctx
->dri_context
=
1131 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1139 dri2_create_context_attribs_error(error
);
1140 } else if (dri2_dpy
->dri2
) {
1141 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1143 unsigned num_attribs
= 8;
1144 uint32_t ctx_attribs
[8];
1146 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1150 dri2_ctx
->dri_context
=
1151 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1159 dri2_create_context_attribs_error(error
);
1161 dri2_ctx
->dri_context
=
1162 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1169 assert(dri2_dpy
->swrast
);
1170 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1172 unsigned num_attribs
= 8;
1173 uint32_t ctx_attribs
[8];
1175 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1179 dri2_ctx
->dri_context
=
1180 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1188 dri2_create_context_attribs_error(error
);
1190 dri2_ctx
->dri_context
=
1191 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1199 if (!dri2_ctx
->dri_context
)
1202 return &dri2_ctx
->base
;
1210 * Called via eglDestroyContext(), drv->API.DestroyContext().
1213 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1215 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1216 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1218 if (_eglPutContext(ctx
)) {
1219 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1227 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1230 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1231 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1233 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1234 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1235 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1236 _EGLContext
*old_ctx
;
1237 _EGLSurface
*old_dsurf
, *old_rsurf
;
1238 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1239 __DRIdrawable
*ddraw
, *rdraw
;
1244 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1246 /* make new bindings */
1247 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1248 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1252 /* flush before context switch */
1253 if (old_ctx
&& dri2_drv
->glFlush
)
1254 dri2_drv
->glFlush();
1256 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1257 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1258 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1261 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1262 dri2_dpy
->core
->unbindContext(old_cctx
);
1265 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1267 if (unbind
|| dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1269 drv
->API
.DestroySurface(drv
, disp
, old_dsurf
);
1271 drv
->API
.DestroySurface(drv
, disp
, old_rsurf
);
1274 dri2_dpy
->ref_count
++;
1276 EGLDisplay old_disp
= _eglGetDisplayHandle(old_ctx
->Resource
.Display
);
1277 drv
->API
.DestroyContext(drv
, disp
, old_ctx
);
1278 dri2_display_release(old_disp
);
1283 /* undo the previous _eglBindContext */
1284 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1285 assert(&dri2_ctx
->base
== ctx
&&
1286 tmp_dsurf
== dsurf
&&
1287 tmp_rsurf
== rsurf
);
1289 _eglPutSurface(dsurf
);
1290 _eglPutSurface(rsurf
);
1291 _eglPutContext(ctx
);
1293 _eglPutSurface(old_dsurf
);
1294 _eglPutSurface(old_rsurf
);
1295 _eglPutContext(old_ctx
);
1297 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1298 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1301 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1306 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1308 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1310 return dri2_surf
->dri_drawable
;
1314 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1317 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1319 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1321 return dri2_drv
->get_proc_address(procname
);
1325 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1326 _EGLConfig
*conf
, void *native_window
,
1327 const EGLint
*attrib_list
)
1329 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1330 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1335 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1336 _EGLConfig
*conf
, void *native_pixmap
,
1337 const EGLint
*attrib_list
)
1339 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1340 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1345 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1346 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1348 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1349 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
1353 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1355 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1356 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1360 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1363 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1364 return dri2_dpy
->vtbl
->swap_interval(drv
, dpy
, surf
, interval
);
1368 * Asks the client API to flush any rendering to the drawable so that we can
1369 * do our swapbuffers.
1372 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1374 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1375 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1377 if (dri2_dpy
->flush
) {
1378 if (dri2_dpy
->flush
->base
.version
>= 4) {
1379 /* We know there's a current context because:
1381 * "If surface is not bound to the calling thread’s current
1382 * context, an EGL_BAD_SURFACE error is generated."
1384 _EGLContext
*ctx
= _eglGetCurrentContext();
1385 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1387 /* From the EGL 1.4 spec (page 52):
1389 * "The contents of ancillary buffers are always undefined
1390 * after calling eglSwapBuffers."
1392 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1394 __DRI2_FLUSH_DRAWABLE
|
1395 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1396 __DRI2_THROTTLE_SWAPBUFFER
);
1398 dri2_dpy
->flush
->flush(dri_drawable
);
1404 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1406 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1407 return dri2_dpy
->vtbl
->swap_buffers(drv
, dpy
, surf
);
1411 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1413 const EGLint
*rects
, EGLint n_rects
)
1415 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1416 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, dpy
, surf
,
1421 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1422 EGLint numRects
, const EGLint
*rects
)
1424 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1425 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, dpy
, surf
, numRects
, rects
);
1429 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1430 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1432 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1433 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, dpy
, surf
, x
, y
, width
, height
);
1437 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1438 void *native_pixmap_target
)
1440 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1441 return dri2_dpy
->vtbl
->copy_buffers(drv
, dpy
, surf
, native_pixmap_target
);
1445 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1447 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1448 return dri2_dpy
->vtbl
->query_buffer_age(drv
, dpy
, surf
);
1452 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1454 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1455 _EGLSurface
*surf
= ctx
->DrawSurface
;
1456 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1460 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1461 * we need to copy fake to real here.*/
1463 if (dri2_dpy
->flush
!= NULL
)
1464 dri2_dpy
->flush
->flush(dri_drawable
);
1470 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1475 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1476 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1483 dri2_bind_tex_image(_EGLDriver
*drv
,
1484 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1486 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1487 struct dri2_egl_context
*dri2_ctx
;
1489 GLint format
, target
;
1490 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1492 ctx
= _eglGetCurrentContext();
1493 dri2_ctx
= dri2_egl_context(ctx
);
1495 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1498 switch (surf
->TextureFormat
) {
1499 case EGL_TEXTURE_RGB
:
1500 format
= __DRI_TEXTURE_FORMAT_RGB
;
1502 case EGL_TEXTURE_RGBA
:
1503 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1506 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1507 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1510 switch (surf
->TextureTarget
) {
1511 case EGL_TEXTURE_2D
:
1512 target
= GL_TEXTURE_2D
;
1515 target
= GL_TEXTURE_2D
;
1516 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1519 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1527 dri2_release_tex_image(_EGLDriver
*drv
,
1528 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1530 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1531 struct dri2_egl_context
*dri2_ctx
;
1534 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1536 ctx
= _eglGetCurrentContext();
1537 dri2_ctx
= dri2_egl_context(ctx
);
1539 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1542 switch (surf
->TextureTarget
) {
1543 case EGL_TEXTURE_2D
:
1544 target
= GL_TEXTURE_2D
;
1550 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1551 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1552 (*dri2_dpy
->tex_buffer
->releaseTexBuffer
)(dri2_ctx
->dri_context
,
1561 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
1562 EGLenum target
, EGLClientBuffer buffer
,
1563 const EGLint
*attr_list
)
1565 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1566 return dri2_dpy
->vtbl
->create_image(drv
, dpy
, ctx
, target
, buffer
,
1571 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1573 struct dri2_egl_image
*dri2_img
;
1575 if (dri_image
== NULL
) {
1576 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1580 dri2_img
= malloc(sizeof *dri2_img
);
1582 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1586 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1591 dri2_img
->dri_image
= dri_image
;
1593 return &dri2_img
->base
;
1597 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1598 EGLClientBuffer buffer
,
1599 const EGLint
*attr_list
)
1601 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1602 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1603 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1604 __DRIimage
*dri_image
;
1606 if (renderbuffer
== 0) {
1607 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1608 return EGL_NO_IMAGE_KHR
;
1612 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1613 renderbuffer
, NULL
);
1615 return dri2_create_image_from_dri(disp
, dri_image
);
1618 #ifdef HAVE_WAYLAND_PLATFORM
1620 /* This structure describes how a wl_buffer maps to one or more
1621 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1622 * offsets and strides of the planes in the buffer. This table maps a
1623 * wl_drm format code to a description of the planes in the buffer
1624 * that lets us create a __DRIimage for each of the planes. */
1626 static const struct wl_drm_components_descriptor
{
1627 uint32_t dri_components
;
1630 } wl_drm_components
[] = {
1631 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1632 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1633 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1634 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1635 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1639 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1640 EGLClientBuffer _buffer
,
1641 const EGLint
*attr_list
)
1643 struct wl_drm_buffer
*buffer
;
1644 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1645 const struct wl_drm_components_descriptor
*f
;
1646 __DRIimage
*dri_image
;
1647 _EGLImageAttribs attrs
;
1651 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1652 (struct wl_resource
*) _buffer
);
1656 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1657 plane
= attrs
.PlaneWL
;
1658 if (err
!= EGL_SUCCESS
) {
1659 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1663 f
= buffer
->driver_format
;
1664 if (plane
< 0 || plane
>= f
->nplanes
) {
1665 _eglError(EGL_BAD_PARAMETER
,
1666 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1670 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1672 if (dri_image
== NULL
) {
1673 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1677 return dri2_create_image_from_dri(disp
, dri_image
);
1682 dri2_get_sync_values_chromium(_EGLDisplay
*dpy
, _EGLSurface
*surf
,
1683 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1686 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1687 return dri2_dpy
->vtbl
->get_sync_values(dpy
, surf
, ust
, msc
, sbc
);
1691 * Set the error code after a call to
1692 * dri2_egl_image::dri_image::createImageFromTexture.
1695 dri2_create_image_khr_texture_error(int dri_error
)
1699 switch (dri_error
) {
1700 case __DRI_IMAGE_ERROR_SUCCESS
:
1703 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1704 egl_error
= EGL_BAD_ALLOC
;
1707 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1708 egl_error
= EGL_BAD_MATCH
;
1711 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1712 egl_error
= EGL_BAD_PARAMETER
;
1715 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
1716 egl_error
= EGL_BAD_ACCESS
;
1721 egl_error
= EGL_BAD_MATCH
;
1725 _eglError(egl_error
, "dri2_create_image_khr_texture");
1729 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1731 EGLClientBuffer buffer
,
1732 const EGLint
*attr_list
)
1734 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1735 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1736 struct dri2_egl_image
*dri2_img
;
1737 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
1738 _EGLImageAttribs attrs
;
1744 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1745 return EGL_NO_IMAGE_KHR
;
1748 if (_eglParseImageAttribList(&attrs
, disp
, attr_list
) != EGL_SUCCESS
)
1749 return EGL_NO_IMAGE_KHR
;
1752 case EGL_GL_TEXTURE_2D_KHR
:
1754 gl_target
= GL_TEXTURE_2D
;
1756 case EGL_GL_TEXTURE_3D_KHR
:
1757 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1758 depth
= attrs
.GLTextureZOffset
;
1759 gl_target
= GL_TEXTURE_3D
;
1763 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1764 return EGL_NO_IMAGE_KHR
;
1766 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1767 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1768 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1769 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1770 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1771 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1772 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
1773 gl_target
= GL_TEXTURE_CUBE_MAP
;
1776 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1777 return EGL_NO_IMAGE_KHR
;
1780 dri2_img
= malloc(sizeof *dri2_img
);
1782 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1783 return EGL_NO_IMAGE_KHR
;
1786 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1787 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1789 return EGL_NO_IMAGE_KHR
;
1792 dri2_img
->dri_image
=
1793 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
1797 attrs
.GLTextureLevel
,
1800 dri2_create_image_khr_texture_error(error
);
1802 if (!dri2_img
->dri_image
) {
1804 return EGL_NO_IMAGE_KHR
;
1806 return &dri2_img
->base
;
1810 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1811 EGLint attribute
, EGLint
*value
)
1813 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1814 if (!dri2_dpy
->vtbl
->query_surface
)
1815 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
1816 return dri2_dpy
->vtbl
->query_surface(drv
, dpy
, surf
, attribute
, value
);
1819 static struct wl_buffer
*
1820 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1823 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1824 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, dpy
, img
);
1829 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1830 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1832 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1833 EGLint format
, name
, pitch
, err
;
1834 _EGLImageAttribs attrs
;
1835 __DRIimage
*dri_image
;
1837 name
= (EGLint
) (uintptr_t) buffer
;
1839 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1840 if (err
!= EGL_SUCCESS
)
1843 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
1844 attrs
.DRMBufferStrideMESA
<= 0) {
1845 _eglError(EGL_BAD_PARAMETER
,
1846 "bad width, height or stride");
1850 switch (attrs
.DRMBufferFormatMESA
) {
1851 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1852 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1853 pitch
= attrs
.DRMBufferStrideMESA
;
1856 _eglError(EGL_BAD_PARAMETER
,
1857 "dri2_create_image_khr: unsupported pixmap depth");
1862 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1870 return dri2_create_image_from_dri(disp
, dri_image
);
1874 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
1881 * "Required attributes and their values are as follows:
1883 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1885 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1886 * by drm_fourcc.h and used as the pixel_format parameter of the
1887 * drm_mode_fb_cmd2 ioctl."
1891 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1892 * incomplete, EGL_BAD_PARAMETER is generated."
1894 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
1895 !attrs
->DMABufFourCC
.IsPresent
) {
1896 _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
1903 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1904 * specified for a plane's pitch or offset isn't supported by EGL,
1905 * EGL_BAD_ACCESS is generated."
1907 for (i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
1908 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
1909 attrs
->DMABufPlanePitches
[i
].Value
<= 0) {
1910 _eglError(EGL_BAD_ACCESS
, "invalid pitch");
1918 /* Returns the total number of file descriptors. Zero indicates an error. */
1920 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
1922 unsigned i
, plane_n
;
1924 switch (attrs
->DMABufFourCC
.Value
) {
1926 case DRM_FORMAT_RG88
:
1927 case DRM_FORMAT_GR88
:
1928 case DRM_FORMAT_RGB332
:
1929 case DRM_FORMAT_BGR233
:
1930 case DRM_FORMAT_XRGB4444
:
1931 case DRM_FORMAT_XBGR4444
:
1932 case DRM_FORMAT_RGBX4444
:
1933 case DRM_FORMAT_BGRX4444
:
1934 case DRM_FORMAT_ARGB4444
:
1935 case DRM_FORMAT_ABGR4444
:
1936 case DRM_FORMAT_RGBA4444
:
1937 case DRM_FORMAT_BGRA4444
:
1938 case DRM_FORMAT_XRGB1555
:
1939 case DRM_FORMAT_XBGR1555
:
1940 case DRM_FORMAT_RGBX5551
:
1941 case DRM_FORMAT_BGRX5551
:
1942 case DRM_FORMAT_ARGB1555
:
1943 case DRM_FORMAT_ABGR1555
:
1944 case DRM_FORMAT_RGBA5551
:
1945 case DRM_FORMAT_BGRA5551
:
1946 case DRM_FORMAT_RGB565
:
1947 case DRM_FORMAT_BGR565
:
1948 case DRM_FORMAT_RGB888
:
1949 case DRM_FORMAT_BGR888
:
1950 case DRM_FORMAT_XRGB8888
:
1951 case DRM_FORMAT_XBGR8888
:
1952 case DRM_FORMAT_RGBX8888
:
1953 case DRM_FORMAT_BGRX8888
:
1954 case DRM_FORMAT_ARGB8888
:
1955 case DRM_FORMAT_ABGR8888
:
1956 case DRM_FORMAT_RGBA8888
:
1957 case DRM_FORMAT_BGRA8888
:
1958 case DRM_FORMAT_XRGB2101010
:
1959 case DRM_FORMAT_XBGR2101010
:
1960 case DRM_FORMAT_RGBX1010102
:
1961 case DRM_FORMAT_BGRX1010102
:
1962 case DRM_FORMAT_ARGB2101010
:
1963 case DRM_FORMAT_ABGR2101010
:
1964 case DRM_FORMAT_RGBA1010102
:
1965 case DRM_FORMAT_BGRA1010102
:
1966 case DRM_FORMAT_YUYV
:
1967 case DRM_FORMAT_YVYU
:
1968 case DRM_FORMAT_UYVY
:
1969 case DRM_FORMAT_VYUY
:
1972 case DRM_FORMAT_NV12
:
1973 case DRM_FORMAT_NV21
:
1974 case DRM_FORMAT_NV16
:
1975 case DRM_FORMAT_NV61
:
1978 case DRM_FORMAT_YUV410
:
1979 case DRM_FORMAT_YVU410
:
1980 case DRM_FORMAT_YUV411
:
1981 case DRM_FORMAT_YVU411
:
1982 case DRM_FORMAT_YUV420
:
1983 case DRM_FORMAT_YVU420
:
1984 case DRM_FORMAT_YUV422
:
1985 case DRM_FORMAT_YVU422
:
1986 case DRM_FORMAT_YUV444
:
1987 case DRM_FORMAT_YVU444
:
1991 _eglError(EGL_BAD_ATTRIBUTE
, "invalid format");
1998 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1999 * incomplete, EGL_BAD_PARAMETER is generated."
2001 for (i
= 0; i
< plane_n
; ++i
) {
2002 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2003 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2004 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2005 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2011 * The spec also says:
2013 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2014 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2015 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2016 * attributes are specified."
2018 for (i
= plane_n
; i
< 3; ++i
) {
2019 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2020 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2021 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2022 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2033 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2034 * EGL will take a reference to the dma_buf(s) which it will release at any
2035 * time while the EGLDisplay is initialized. It is the responsibility of the
2036 * application to close the dma_buf file descriptors."
2038 * Therefore we must never close or otherwise modify the file descriptors.
2041 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2042 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2044 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2047 _EGLImageAttribs attrs
;
2048 __DRIimage
*dri_image
;
2059 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2060 * error EGL_BAD_PARAMETER is generated."
2062 if (buffer
!= NULL
) {
2063 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2067 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2068 if (err
!= EGL_SUCCESS
) {
2069 _eglError(err
, "bad attribute");
2073 if (!dri2_check_dma_buf_attribs(&attrs
))
2076 num_fds
= dri2_check_dma_buf_format(&attrs
);
2080 for (i
= 0; i
< num_fds
; ++i
) {
2081 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2082 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2083 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2087 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2088 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2089 fds
, num_fds
, pitches
, offsets
,
2090 attrs
.DMABufYuvColorSpaceHint
.Value
,
2091 attrs
.DMABufSampleRangeHint
.Value
,
2092 attrs
.DMABufChromaHorizontalSiting
.Value
,
2093 attrs
.DMABufChromaVerticalSiting
.Value
,
2096 dri2_create_image_khr_texture_error(error
);
2099 return EGL_NO_IMAGE_KHR
;
2101 res
= dri2_create_image_from_dri(disp
, dri_image
);
2106 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2107 const EGLint
*attr_list
)
2109 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2110 struct dri2_egl_image
*dri2_img
;
2111 _EGLImageAttribs attrs
;
2112 unsigned int dri_use
, valid_mask
;
2114 EGLint err
= EGL_SUCCESS
;
2118 dri2_img
= malloc(sizeof *dri2_img
);
2120 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2121 return EGL_NO_IMAGE_KHR
;
2125 err
= EGL_BAD_PARAMETER
;
2129 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
2130 err
= EGL_BAD_PARAMETER
;
2134 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2135 if (err
!= EGL_SUCCESS
)
2138 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2139 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)",
2140 attrs
.Width
, attrs
.Height
);
2144 switch (attrs
.DRMBufferFormatMESA
) {
2145 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2146 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2149 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x",
2150 attrs
.DRMBufferFormatMESA
);
2155 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2156 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2157 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2158 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2159 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x",
2160 attrs
.DRMBufferUseMESA
& ~valid_mask
);
2165 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2166 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2167 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2168 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2169 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2170 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2172 dri2_img
->dri_image
=
2173 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2174 attrs
.Width
, attrs
.Height
,
2175 format
, dri_use
, dri2_img
);
2176 if (dri2_img
->dri_image
== NULL
) {
2177 err
= EGL_BAD_ALLOC
;
2181 return &dri2_img
->base
;
2185 _eglError(err
, "dri2_create_drm_image_mesa");
2187 return EGL_NO_IMAGE_KHR
;
2191 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2192 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2194 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2195 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2199 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2200 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
2201 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2206 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2207 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2210 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2211 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2217 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2219 EGLint
*fourcc
, EGLint
*nplanes
,
2220 EGLuint64KHR
*modifiers
)
2222 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2223 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2229 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2230 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2232 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2233 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2242 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2243 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2245 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2246 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2250 /* rework later to provide multiple fds/strides/offsets */
2252 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2253 __DRI_IMAGE_ATTRIB_FD
, fds
);
2256 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2257 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2268 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2269 _EGLContext
*ctx
, EGLenum target
,
2270 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2275 case EGL_GL_TEXTURE_2D_KHR
:
2276 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2277 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2278 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2279 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2280 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2281 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2282 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2283 case EGL_GL_TEXTURE_3D_KHR
:
2284 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
2285 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2288 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2289 return EGL_NO_IMAGE_KHR
;
2291 case EGL_GL_RENDERBUFFER_KHR
:
2292 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2294 case EGL_DRM_BUFFER_MESA
:
2295 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2296 case EGL_LINUX_DMA_BUF_EXT
:
2297 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2299 #ifdef HAVE_WAYLAND_PLATFORM
2300 case EGL_WAYLAND_BUFFER_WL
:
2301 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2304 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2305 return EGL_NO_IMAGE_KHR
;
2310 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2312 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2313 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2317 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2323 #ifdef HAVE_WAYLAND_PLATFORM
2326 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2327 struct wl_drm_buffer
*buffer
)
2329 _EGLDisplay
*disp
= user_data
;
2330 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2332 int i
, dri_components
= 0;
2335 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2344 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2356 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2358 buffer
->driver_format
= NULL
;
2359 for (i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2360 if (wl_drm_components
[i
].dri_components
== dri_components
)
2361 buffer
->driver_format
= &wl_drm_components
[i
];
2363 if (buffer
->driver_format
== NULL
)
2364 dri2_dpy
->image
->destroyImage(img
);
2366 buffer
->driver_buffer
= img
;
2370 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2372 _EGLDisplay
*disp
= user_data
;
2373 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2375 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2378 static struct wayland_drm_callbacks wl_drm_callbacks
= {
2379 .authenticate
= NULL
,
2380 .reference_buffer
= dri2_wl_reference_buffer
,
2381 .release_buffer
= dri2_wl_release_buffer
2385 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2386 struct wl_display
*wl_dpy
)
2388 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2394 if (dri2_dpy
->wl_server_drm
)
2397 wl_drm_callbacks
.authenticate
=
2398 (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
;
2400 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2401 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2402 dri2_dpy
->image
->base
.version
>= 7 &&
2403 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2404 flags
|= WAYLAND_DRM_PRIME
;
2406 dri2_dpy
->wl_server_drm
=
2407 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2408 &wl_drm_callbacks
, disp
, flags
);
2410 if (!dri2_dpy
->wl_server_drm
)
2413 #ifdef HAVE_DRM_PLATFORM
2414 /* We have to share the wl_drm instance with gbm, so gbm can convert
2415 * wl_buffers to gbm bos. */
2416 if (dri2_dpy
->gbm_dri
)
2417 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2424 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2425 struct wl_display
*wl_dpy
)
2427 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2431 if (!dri2_dpy
->wl_server_drm
)
2434 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2435 dri2_dpy
->wl_server_drm
= NULL
;
2441 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2442 struct wl_resource
*buffer_resource
,
2443 EGLint attribute
, EGLint
*value
)
2445 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2446 struct wl_drm_buffer
*buffer
;
2447 const struct wl_drm_components_descriptor
*format
;
2449 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2453 format
= buffer
->driver_format
;
2454 switch (attribute
) {
2455 case EGL_TEXTURE_FORMAT
:
2456 *value
= format
->components
;
2459 *value
= buffer
->width
;
2462 *value
= buffer
->height
;
2471 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2473 p_atomic_inc(&sync
->refcount
);
2477 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2478 struct dri2_egl_sync
*dri2_sync
)
2480 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2481 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
)
2482 cnd_destroy(&dri2_sync
->cond
);
2484 if (dri2_sync
->fence
)
2485 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2492 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
2493 EGLenum type
, const EGLint
*attrib_list
,
2494 const EGLAttrib
*attrib_list64
)
2496 _EGLContext
*ctx
= _eglGetCurrentContext();
2497 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2498 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2499 struct dri2_egl_sync
*dri2_sync
;
2501 pthread_condattr_t attr
;
2503 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2505 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2509 if (!_eglInitSync(&dri2_sync
->base
, dpy
, type
, attrib_list
,
2516 case EGL_SYNC_FENCE_KHR
:
2517 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2518 if (!dri2_sync
->fence
) {
2519 /* Why did it fail? DRI doesn't return an error code, so we emit
2520 * a generic EGL error that doesn't communicate user error.
2522 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2528 case EGL_SYNC_CL_EVENT_KHR
:
2529 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2530 dri2_dpy
->dri_screen
,
2531 dri2_sync
->base
.CLEvent
);
2532 /* this can only happen if the cl_event passed in is invalid. */
2533 if (!dri2_sync
->fence
) {
2534 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2539 /* the initial status must be "signaled" if the cl_event is signaled */
2540 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2541 dri2_sync
->fence
, 0, 0))
2542 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2545 case EGL_SYNC_REUSABLE_KHR
:
2546 /* intialize attr */
2547 ret
= pthread_condattr_init(&attr
);
2550 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2555 /* change clock attribute to CLOCK_MONOTONIC */
2556 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
2559 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2564 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
2567 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2572 /* initial status of reusable sync must be "unsignaled" */
2573 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
2577 p_atomic_set(&dri2_sync
->refcount
, 1);
2578 return &dri2_sync
->base
;
2582 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2584 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2585 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2586 EGLint ret
= EGL_TRUE
;
2589 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
2590 * then unlock all threads possibly blocked by the reusable sync before
2593 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
2594 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2595 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2596 /* unblock all threads currently blocked by sync */
2597 err
= cnd_broadcast(&dri2_sync
->cond
);
2600 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
2604 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2610 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2611 EGLint flags
, EGLTime timeout
)
2613 _EGLContext
*ctx
= _eglGetCurrentContext();
2614 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2615 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2616 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2617 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2618 unsigned wait_flags
= 0;
2620 /* timespecs for cnd_timedwait */
2621 struct timespec current
;
2624 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
2626 /* The EGL_KHR_fence_sync spec states:
2628 * "If no context is current for the bound API,
2629 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
2631 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
2632 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
2634 /* the sync object should take a reference while waiting */
2635 dri2_egl_ref_sync(dri2_sync
);
2637 switch (sync
->Type
) {
2638 case EGL_SYNC_FENCE_KHR
:
2639 case EGL_SYNC_CL_EVENT_KHR
:
2640 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
2641 dri2_sync
->fence
, wait_flags
,
2643 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2645 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2648 case EGL_SYNC_REUSABLE_KHR
:
2649 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
2650 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
2651 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
2652 if (dri2_drv
->glFlush
)
2653 dri2_drv
->glFlush();
2656 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
2657 if (timeout
== EGL_FOREVER_KHR
) {
2658 mtx_lock(&dri2_sync
->mutex
);
2659 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
2660 mtx_unlock(&dri2_sync
->mutex
);
2662 /* if reusable sync has not been yet signaled */
2663 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
2664 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
2666 /* calculating when to expire */
2667 expire
.nsec
= timeout
% 1000000000L;
2668 expire
.sec
= timeout
/ 1000000000L;
2670 expire
.nsec
+= current
.tv_nsec
;
2671 expire
.sec
+= current
.tv_sec
;
2673 /* expire.nsec now is a number between 0 and 1999999998 */
2674 if (expire
.nsec
> 999999999L) {
2676 expire
.nsec
-= 1000000000L;
2679 mtx_lock(&dri2_sync
->mutex
);
2680 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
2681 mtx_unlock(&dri2_sync
->mutex
);
2683 if (ret
== thrd_busy
) {
2684 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2685 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2687 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2695 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2701 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2704 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2707 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
) {
2708 _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
2712 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
) {
2713 _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
2717 dri2_sync
->base
.SyncStatus
= mode
;
2719 if (mode
== EGL_SIGNALED_KHR
) {
2720 ret
= cnd_broadcast(&dri2_sync
->cond
);
2722 /* fail to broadcast */
2724 _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
2733 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2735 _EGLContext
*ctx
= _eglGetCurrentContext();
2736 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2737 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2738 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2740 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
2741 dri2_sync
->fence
, 0);
2746 dri2_interop_query_device_info(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2747 struct mesa_glinterop_device_info
*out
)
2749 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2750 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2752 if (!dri2_dpy
->interop
)
2753 return MESA_GLINTEROP_UNSUPPORTED
;
2755 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
2759 dri2_interop_export_object(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2760 struct mesa_glinterop_export_in
*in
,
2761 struct mesa_glinterop_export_out
*out
)
2763 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2764 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2766 if (!dri2_dpy
->interop
)
2767 return MESA_GLINTEROP_UNSUPPORTED
;
2769 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
2773 dri2_unload(_EGLDriver
*drv
)
2775 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2777 if (dri2_drv
->handle
)
2778 dlclose(dri2_drv
->handle
);
2783 dri2_load(_EGLDriver
*drv
)
2785 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2786 #ifdef HAVE_ANDROID_PLATFORM
2787 const char *libname
= "libglapi.so";
2788 #elif defined(__APPLE__)
2789 const char *libname
= "libglapi.0.dylib";
2790 #elif defined(__CYGWIN__)
2791 const char *libname
= "cygglapi-0.dll";
2793 const char *libname
= "libglapi.so.0";
2797 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2798 handle
= dlopen(libname
, RTLD_LAZY
| RTLD_GLOBAL
);
2800 dri2_drv
->get_proc_address
= (_EGLProc (*)(const char *))
2801 dlsym(handle
, "_glapi_get_proc_address");
2802 if (!dri2_drv
->get_proc_address
|| !libname
) {
2803 /* no need to keep a reference */
2809 /* if glapi is not available, loading DRI drivers will fail */
2810 if (!dri2_drv
->get_proc_address
) {
2811 _eglLog(_EGL_WARNING
, "DRI2: failed to find _glapi_get_proc_address");
2815 dri2_drv
->glFlush
= (void (*)(void))
2816 dri2_drv
->get_proc_address("glFlush");
2818 dri2_drv
->handle
= handle
;
2824 * This is the main entrypoint into the driver, called by libEGL.
2825 * Create a new _EGLDriver object and init its dispatch table.
2828 _eglBuiltInDriverDRI2(const char *args
)
2830 struct dri2_egl_driver
*dri2_drv
;
2834 dri2_drv
= calloc(1, sizeof *dri2_drv
);
2838 if (!dri2_load(&dri2_drv
->base
)) {
2843 _eglInitDriverFallbacks(&dri2_drv
->base
);
2844 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
2845 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
2846 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
2847 dri2_drv
->base
.API
.DestroyContext
= dri2_destroy_context
;
2848 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
2849 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
2850 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
2851 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
2852 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
2853 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
2854 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
2855 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
2856 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
2857 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
2858 dri2_drv
->base
.API
.SwapInterval
= dri2_swap_interval
;
2859 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
2860 dri2_drv
->base
.API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
2861 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
2862 dri2_drv
->base
.API
.PostSubBufferNV
= dri2_post_sub_buffer
;
2863 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
,
2864 dri2_drv
->base
.API
.QueryBufferAge
= dri2_query_buffer_age
;
2865 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image
;
2866 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
2867 dri2_drv
->base
.API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
2868 dri2_drv
->base
.API
.QuerySurface
= dri2_query_surface
;
2870 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
2871 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
2872 dri2_drv
->base
.API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
2873 dri2_drv
->base
.API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
2875 #ifdef HAVE_WAYLAND_PLATFORM
2876 dri2_drv
->base
.API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
2877 dri2_drv
->base
.API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
2878 dri2_drv
->base
.API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
2880 dri2_drv
->base
.API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
2881 dri2_drv
->base
.API
.CreateSyncKHR
= dri2_create_sync
;
2882 dri2_drv
->base
.API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
2883 dri2_drv
->base
.API
.SignalSyncKHR
= dri2_signal_sync
;
2884 dri2_drv
->base
.API
.WaitSyncKHR
= dri2_server_wait_sync
;
2885 dri2_drv
->base
.API
.DestroySyncKHR
= dri2_destroy_sync
;
2886 dri2_drv
->base
.API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
2887 dri2_drv
->base
.API
.GLInteropExportObject
= dri2_interop_export_object
;
2889 dri2_drv
->base
.Name
= "DRI2";
2890 dri2_drv
->base
.Unload
= dri2_unload
;
2892 return &dri2_drv
->base
;