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
= dri2_egl_display(disp
);
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 _eglInitConfig(&base
, disp
, id
);
176 bind_to_texture_rgb
= 0;
177 bind_to_texture_rgba
= 0;
179 while (dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
++, &attrib
, &value
)) {
181 case __DRI_ATTRIB_RENDER_TYPE
:
182 if (value
& __DRI_ATTRIB_RGBA_BIT
)
183 value
= EGL_RGB_BUFFER
;
184 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
185 value
= EGL_LUMINANCE_BUFFER
;
188 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
191 case __DRI_ATTRIB_CONFIG_CAVEAT
:
192 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
193 value
= EGL_NON_CONFORMANT_CONFIG
;
194 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
195 value
= EGL_SLOW_CONFIG
;
198 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
201 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
202 bind_to_texture_rgb
= value
;
205 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
206 bind_to_texture_rgba
= value
;
209 case __DRI_ATTRIB_DOUBLE_BUFFER
:
210 double_buffer
= value
;
213 case __DRI_ATTRIB_RED_MASK
:
214 dri_masks
[0] = value
;
217 case __DRI_ATTRIB_GREEN_MASK
:
218 dri_masks
[1] = value
;
221 case __DRI_ATTRIB_BLUE_MASK
:
222 dri_masks
[2] = value
;
225 case __DRI_ATTRIB_ALPHA_MASK
:
226 dri_masks
[3] = value
;
229 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
230 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
231 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
232 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
233 /* Don't expose visuals with the accumulation buffer. */
238 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
240 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
244 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH
:
245 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_WIDTH
,
246 _EGL_MAX_PBUFFER_WIDTH
);
248 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT
:
249 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_HEIGHT
,
250 _EGL_MAX_PBUFFER_HEIGHT
);
254 key
= dri2_to_egl_attribute_map
[attrib
];
256 _eglSetConfigKey(&base
, key
, value
);
262 for (i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
263 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
265 if (rgba_masks
&& memcmp(rgba_masks
, dri_masks
, sizeof(dri_masks
)))
268 base
.NativeRenderable
= EGL_TRUE
;
270 base
.SurfaceType
= surface_type
;
271 if (surface_type
& (EGL_PBUFFER_BIT
|
272 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
273 base
.BindToTextureRGB
= bind_to_texture_rgb
;
274 if (base
.AlphaSize
> 0)
275 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
278 base
.RenderableType
= disp
->ClientAPIs
;
279 base
.Conformant
= disp
->ClientAPIs
;
281 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
282 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
284 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
285 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
289 config_id
= base
.ConfigID
;
290 base
.ConfigID
= EGL_DONT_CARE
;
291 base
.SurfaceType
= EGL_DONT_CARE
;
292 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
293 (_EGLArrayForEach
) dri2_match_config
, &base
);
295 if (num_configs
== 1) {
296 conf
= (struct dri2_egl_config
*) matching_config
;
298 if (double_buffer
&& !conf
->dri_double_config
[srgb
])
299 conf
->dri_double_config
[srgb
] = dri_config
;
300 else if (!double_buffer
&& !conf
->dri_single_config
[srgb
])
301 conf
->dri_single_config
[srgb
] = dri_config
;
303 /* a similar config type is already added (unlikely) => discard */
306 else if (num_configs
== 0) {
307 conf
= calloc(1, sizeof *conf
);
312 conf
->dri_double_config
[srgb
] = dri_config
;
314 conf
->dri_single_config
[srgb
] = dri_config
;
316 memcpy(&conf
->base
, &base
, sizeof base
);
317 conf
->base
.SurfaceType
= 0;
318 conf
->base
.ConfigID
= config_id
;
320 _eglLinkConfig(&conf
->base
);
328 surface_type
&= ~EGL_PIXMAP_BIT
;
331 /* No support for pbuffer + MSAA for now.
333 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
334 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
337 surface_type
&= ~EGL_PBUFFER_BIT
;
340 conf
->base
.SurfaceType
|= surface_type
;
346 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
348 _EGLDisplay
*disp
= data
;
349 struct dri2_egl_image
*dri2_img
;
354 img
= _eglLookupImage(image
, disp
);
356 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
360 dri2_img
= dri2_egl_image(image
);
362 return dri2_img
->dri_image
;
365 const __DRIimageLookupExtension image_lookup_extension
= {
366 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
368 .lookupEGLImage
= dri2_lookup_egl_image
371 struct dri2_extension_match
{
377 static const struct dri2_extension_match dri3_driver_extensions
[] = {
378 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
379 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
383 static const struct dri2_extension_match dri2_driver_extensions
[] = {
384 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
385 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
389 static const struct dri2_extension_match dri2_core_extensions
[] = {
390 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
391 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
392 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
396 static const struct dri2_extension_match swrast_driver_extensions
[] = {
397 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
398 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
402 static const struct dri2_extension_match swrast_core_extensions
[] = {
403 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
407 static const struct dri2_extension_match optional_core_extensions
[] = {
408 { __DRI2_ROBUSTNESS
, 1, offsetof(struct dri2_egl_display
, robustness
) },
409 { __DRI2_CONFIG_QUERY
, 1, offsetof(struct dri2_egl_display
, config
) },
410 { __DRI2_FENCE
, 1, offsetof(struct dri2_egl_display
, fence
) },
411 { __DRI2_RENDERER_QUERY
, 1, offsetof(struct dri2_egl_display
, rendererQuery
) },
412 { __DRI2_INTEROP
, 1, offsetof(struct dri2_egl_display
, interop
) },
417 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
418 const struct dri2_extension_match
*matches
,
419 const __DRIextension
**extensions
,
422 int i
, j
, ret
= EGL_TRUE
;
425 for (i
= 0; extensions
[i
]; i
++) {
426 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
427 for (j
= 0; matches
[j
].name
; j
++) {
428 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
429 extensions
[i
]->version
>= matches
[j
].version
) {
430 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
431 *(const __DRIextension
**) field
= extensions
[i
];
432 _eglLog(_EGL_INFO
, "found extension %s version %d",
433 extensions
[i
]->name
, extensions
[i
]->version
);
439 for (j
= 0; matches
[j
].name
; j
++) {
440 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
441 if (*(const __DRIextension
**) field
== NULL
) {
443 _eglLog(_EGL_DEBUG
, "did not find optional extension %s version %d",
444 matches
[j
].name
, matches
[j
].version
);
446 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
447 matches
[j
].name
, matches
[j
].version
);
456 static const __DRIextension
**
457 dri2_open_driver(_EGLDisplay
*disp
)
459 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
460 const __DRIextension
**extensions
= NULL
;
461 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
462 char *get_extensions_name
;
463 const __DRIextension
**(*get_extensions
)(void);
466 if (geteuid() == getuid()) {
467 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
468 search_paths
= getenv("LIBGL_DRIVERS_PATH");
470 if (search_paths
== NULL
)
471 search_paths
= DEFAULT_DRIVER_DIR
;
473 dri2_dpy
->driver
= NULL
;
474 end
= search_paths
+ strlen(search_paths
);
475 for (p
= search_paths
; p
< end
; p
= next
+ 1) {
477 next
= strchr(p
, ':');
483 snprintf(path
, sizeof path
,
484 "%.*s/tls/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
485 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
487 if (dri2_dpy
->driver
== NULL
) {
488 snprintf(path
, sizeof path
,
489 "%.*s/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
490 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
491 if (dri2_dpy
->driver
== NULL
)
492 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
494 /* not need continue to loop all paths once the driver is found */
495 if (dri2_dpy
->driver
!= NULL
)
499 snprintf(path
, sizeof path
, "%.*s/gallium_dri.so", len
, p
);
500 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
501 if (dri2_dpy
->driver
== NULL
)
502 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
508 if (dri2_dpy
->driver
== NULL
) {
509 _eglLog(_EGL_WARNING
,
510 "DRI2: failed to open %s (search paths %s)",
511 dri2_dpy
->driver_name
, search_paths
);
515 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
517 if (asprintf(&get_extensions_name
, "%s_%s",
518 __DRI_DRIVER_GET_EXTENSIONS
, dri2_dpy
->driver_name
) != -1) {
519 get_extensions
= dlsym(dri2_dpy
->driver
, get_extensions_name
);
520 if (get_extensions
) {
521 extensions
= get_extensions();
523 _eglLog(_EGL_DEBUG
, "driver does not expose %s(): %s\n",
524 get_extensions_name
, dlerror());
526 free(get_extensions_name
);
530 extensions
= dlsym(dri2_dpy
->driver
, __DRI_DRIVER_EXTENSIONS
);
531 if (extensions
== NULL
) {
532 _eglLog(_EGL_WARNING
,
533 "DRI2: driver exports no extensions (%s)", dlerror());
534 dlclose(dri2_dpy
->driver
);
541 dri2_load_driver_dri3(_EGLDisplay
*disp
)
543 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
544 const __DRIextension
**extensions
;
546 extensions
= dri2_open_driver(disp
);
550 if (!dri2_bind_extensions(dri2_dpy
, dri3_driver_extensions
, extensions
, false)) {
551 dlclose(dri2_dpy
->driver
);
554 dri2_dpy
->driver_extensions
= extensions
;
560 dri2_load_driver(_EGLDisplay
*disp
)
562 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
563 const __DRIextension
**extensions
;
565 extensions
= dri2_open_driver(disp
);
569 if (!dri2_bind_extensions(dri2_dpy
, dri2_driver_extensions
, extensions
, false)) {
570 dlclose(dri2_dpy
->driver
);
573 dri2_dpy
->driver_extensions
= extensions
;
579 dri2_load_driver_swrast(_EGLDisplay
*disp
)
581 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
582 const __DRIextension
**extensions
;
584 extensions
= dri2_open_driver(disp
);
588 if (!dri2_bind_extensions(dri2_dpy
, swrast_driver_extensions
, extensions
, false)) {
589 dlclose(dri2_dpy
->driver
);
592 dri2_dpy
->driver_extensions
= extensions
;
598 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
600 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
601 unsigned int value
= 0;
603 if (!rendererQuery
||
604 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
611 dri2_setup_screen(_EGLDisplay
*disp
)
613 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
614 unsigned int api_mask
;
616 if (dri2_dpy
->image_driver
) {
617 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
618 } else if (dri2_dpy
->dri2
) {
619 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
621 assert(dri2_dpy
->swrast
);
622 api_mask
= 1 << __DRI_API_OPENGL
|
623 1 << __DRI_API_GLES
|
624 1 << __DRI_API_GLES2
|
625 1 << __DRI_API_GLES3
;
628 disp
->ClientAPIs
= 0;
629 if ((api_mask
& (1 <<__DRI_API_OPENGL
)) && _eglIsApiValid(EGL_OPENGL_API
))
630 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
631 if ((api_mask
& (1 << __DRI_API_GLES
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
632 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
633 if ((api_mask
& (1 << __DRI_API_GLES2
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
634 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
635 if ((api_mask
& (1 << __DRI_API_GLES3
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
636 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
638 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
639 disp
->Extensions
.KHR_no_config_context
= EGL_TRUE
;
640 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
642 if (dri2_renderer_query_integer(dri2_dpy
,
643 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
644 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
646 if (dri2_dpy
->image_driver
||
647 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
648 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
649 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
651 if (dri2_dpy
->robustness
)
652 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
655 if (dri2_dpy
->fence
) {
656 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
657 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
658 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
659 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
662 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
664 if (dri2_dpy
->image
) {
665 if (dri2_dpy
->image
->base
.version
>= 10 &&
666 dri2_dpy
->image
->getCapabilities
!= NULL
) {
669 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
670 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
672 if (dri2_dpy
->image
->base
.version
>= 11)
673 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
675 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
676 if (dri2_dpy
->image
->base
.version
>= 11)
677 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
680 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
681 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
682 if (dri2_dpy
->image
->base
.version
>= 5 &&
683 dri2_dpy
->image
->createImageFromTexture
) {
684 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
685 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
687 if (dri2_renderer_query_integer(dri2_dpy
,
688 __DRI2_RENDERER_HAS_TEXTURE_3D
))
689 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
691 if (dri2_dpy
->image
->base
.version
>= 8 &&
692 dri2_dpy
->image
->createImageFromDmaBufs
) {
693 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
699 /* All platforms but DRM call this function to create the screen, query the
700 * dri extensions, setup the vtables and populate the driver_configs.
701 * DRM inherits all that information from its display - GBM.
704 dri2_create_screen(_EGLDisplay
*disp
)
706 const __DRIextension
**extensions
;
707 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
709 if (dri2_dpy
->image_driver
) {
710 dri2_dpy
->dri_screen
=
711 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
712 dri2_dpy
->loader_extensions
,
713 dri2_dpy
->driver_extensions
,
714 &dri2_dpy
->driver_configs
,
716 } else if (dri2_dpy
->dri2
) {
717 if (dri2_dpy
->dri2
->base
.version
>= 4) {
718 dri2_dpy
->dri_screen
=
719 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
720 dri2_dpy
->loader_extensions
,
721 dri2_dpy
->driver_extensions
,
722 &dri2_dpy
->driver_configs
, disp
);
724 dri2_dpy
->dri_screen
=
725 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
726 dri2_dpy
->loader_extensions
,
727 &dri2_dpy
->driver_configs
, disp
);
730 assert(dri2_dpy
->swrast
);
731 if (dri2_dpy
->swrast
->base
.version
>= 4) {
732 dri2_dpy
->dri_screen
=
733 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->loader_extensions
,
734 dri2_dpy
->driver_extensions
,
735 &dri2_dpy
->driver_configs
, disp
);
737 dri2_dpy
->dri_screen
=
738 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->loader_extensions
,
739 &dri2_dpy
->driver_configs
, disp
);
743 if (dri2_dpy
->dri_screen
== NULL
) {
744 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
748 dri2_dpy
->own_dri_screen
= 1;
750 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
752 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
) {
753 if (!dri2_bind_extensions(dri2_dpy
, dri2_core_extensions
, extensions
, false))
754 goto cleanup_dri_screen
;
756 assert(dri2_dpy
->swrast
);
757 if (!dri2_bind_extensions(dri2_dpy
, swrast_core_extensions
, extensions
, false))
758 goto cleanup_dri_screen
;
761 dri2_bind_extensions(dri2_dpy
, optional_core_extensions
, extensions
, true);
762 dri2_setup_screen(disp
);
767 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
773 * Called via eglInitialize(), GLX_drv->API.Initialize().
775 * This must be guaranteed to be called exactly once, even if eglInitialize is
776 * called many times (without a eglTerminate in between).
779 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
781 EGLBoolean ret
= EGL_FALSE
;
782 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
784 /* In the case where the application calls eglMakeCurrent(context1),
785 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
786 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
787 * initialized, as we need it to be able to free context1 correctly.
789 * It would probably be safest to forcibly release the display with
790 * dri2_display_release, to make sure the display is reinitialized correctly.
791 * However, the EGL spec states that we need to keep a reference to the
792 * current context (so we cannot call dri2_make_current(NULL)), and therefore
793 * we would leak context1 as we would be missing the old display connection
794 * to free it up correctly.
797 dri2_dpy
->ref_count
++;
801 /* not until swrast_dri is supported */
802 if (disp
->Options
.UseFallback
)
805 /* Nothing to initialize for a test only display */
806 if (disp
->Options
.TestOnly
)
809 switch (disp
->Platform
) {
810 #ifdef HAVE_SURFACELESS_PLATFORM
811 case _EGL_PLATFORM_SURFACELESS
:
812 ret
= dri2_initialize_surfaceless(drv
, disp
);
815 #ifdef HAVE_X11_PLATFORM
816 case _EGL_PLATFORM_X11
:
817 ret
= dri2_initialize_x11(drv
, disp
);
820 #ifdef HAVE_DRM_PLATFORM
821 case _EGL_PLATFORM_DRM
:
822 ret
= dri2_initialize_drm(drv
, disp
);
825 #ifdef HAVE_WAYLAND_PLATFORM
826 case _EGL_PLATFORM_WAYLAND
:
827 ret
= dri2_initialize_wayland(drv
, disp
);
830 #ifdef HAVE_ANDROID_PLATFORM
831 case _EGL_PLATFORM_ANDROID
:
832 ret
= dri2_initialize_android(drv
, disp
);
836 _eglLog(_EGL_WARNING
, "No EGL platform enabled.");
841 dri2_dpy
= dri2_egl_display(disp
);
847 dri2_dpy
->ref_count
++;
854 * Decrement display reference count, and free up display if necessary.
857 dri2_display_release(_EGLDisplay
*disp
)
859 struct dri2_egl_display
*dri2_dpy
;
865 dri2_dpy
= dri2_egl_display(disp
);
867 assert(dri2_dpy
->ref_count
> 0);
868 dri2_dpy
->ref_count
--;
870 if (dri2_dpy
->ref_count
> 0)
873 _eglCleanupDisplay(disp
);
875 if (dri2_dpy
->own_dri_screen
)
876 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
877 if (dri2_dpy
->fd
>= 0)
879 if (dri2_dpy
->driver
)
880 dlclose(dri2_dpy
->driver
);
881 free(dri2_dpy
->driver_name
);
883 #ifdef HAVE_WAYLAND_PLATFORM
884 free(dri2_dpy
->device_name
);
887 switch (disp
->Platform
) {
888 #ifdef HAVE_X11_PLATFORM
889 case _EGL_PLATFORM_X11
:
890 if (dri2_dpy
->own_device
) {
891 xcb_disconnect(dri2_dpy
->conn
);
895 #ifdef HAVE_DRM_PLATFORM
896 case _EGL_PLATFORM_DRM
:
897 if (dri2_dpy
->own_device
) {
898 gbm_device_destroy(&dri2_dpy
->gbm_dri
->base
.base
);
902 #ifdef HAVE_WAYLAND_PLATFORM
903 case _EGL_PLATFORM_WAYLAND
:
904 if (dri2_dpy
->wl_drm
)
905 wl_drm_destroy(dri2_dpy
->wl_drm
);
906 if (dri2_dpy
->wl_shm
)
907 wl_shm_destroy(dri2_dpy
->wl_shm
);
908 wl_registry_destroy(dri2_dpy
->wl_registry
);
909 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
910 if (dri2_dpy
->own_device
) {
911 wl_display_disconnect(dri2_dpy
->wl_dpy
);
919 /* The drm platform does not create the screen/driver_configs but reuses
920 * the ones from the gbm device. As such the gbm itself is responsible
923 if (disp
->Platform
!= _EGL_PLATFORM_DRM
) {
924 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
925 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
926 free(dri2_dpy
->driver_configs
);
929 disp
->DriverData
= NULL
;
933 * Called via eglTerminate(), drv->API.Terminate().
935 * This must be guaranteed to be called exactly once, even if eglTerminate is
936 * called many times (without a eglInitialize in between).
939 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
941 /* Release all non-current Context/Surfaces. */
942 _eglReleaseDisplayResources(drv
, disp
);
944 dri2_display_release(disp
);
950 * Set the error code after a call to
951 * dri2_egl_display::dri2::createContextAttribs.
954 dri2_create_context_attribs_error(int dri_error
)
959 case __DRI_CTX_ERROR_SUCCESS
:
962 case __DRI_CTX_ERROR_NO_MEMORY
:
963 egl_error
= EGL_BAD_ALLOC
;
966 /* From the EGL_KHR_create_context spec, section "Errors":
968 * * If <config> does not support a client API context compatible
969 * with the requested API major and minor version, [...] context flags,
970 * and context reset notification behavior (for client API types where
971 * these attributes are supported), then an EGL_BAD_MATCH error is
974 * * If an OpenGL ES context is requested and the values for
975 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
976 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
977 * is not defined, than an EGL_BAD_MATCH error is generated.
979 * * If an OpenGL context is requested, the requested version is
980 * greater than 3.2, and the value for attribute
981 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
982 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
983 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
984 * one of these bits set; or if the implementation does not support
985 * the requested profile, then an EGL_BAD_MATCH error is generated.
987 case __DRI_CTX_ERROR_BAD_API
:
988 case __DRI_CTX_ERROR_BAD_VERSION
:
989 case __DRI_CTX_ERROR_BAD_FLAG
:
990 egl_error
= EGL_BAD_MATCH
;
993 /* From the EGL_KHR_create_context spec, section "Errors":
995 * * If an attribute name or attribute value in <attrib_list> is not
996 * recognized (including unrecognized bits in bitmask attributes),
997 * then an EGL_BAD_ATTRIBUTE error is generated."
999 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
1000 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
1001 egl_error
= EGL_BAD_ATTRIBUTE
;
1006 egl_error
= EGL_BAD_MATCH
;
1010 _eglError(egl_error
, "dri2_create_context");
1014 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1015 struct dri2_egl_display
*dri2_dpy
,
1016 uint32_t *ctx_attribs
,
1017 unsigned *num_attribs
)
1021 assert(*num_attribs
>= 8);
1023 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1024 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1025 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1026 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1028 if (dri2_ctx
->base
.Flags
!= 0) {
1029 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1030 * extension, don't even try to send it the robust-access flag.
1031 * It may explode. Instead, generate the required EGL error here.
1033 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1034 && !dri2_dpy
->robustness
) {
1035 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1039 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1040 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
;
1043 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1044 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1045 * extension, don't even try to send it a reset strategy. It may
1046 * explode. Instead, generate the required EGL error here.
1048 if (!dri2_dpy
->robustness
) {
1049 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1053 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1054 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1063 * Called via eglCreateContext(), drv->API.CreateContext().
1065 static _EGLContext
*
1066 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1067 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1069 struct dri2_egl_context
*dri2_ctx
;
1070 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1071 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1072 __DRIcontext
*shared
=
1073 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1074 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1075 const __DRIconfig
*dri_config
;
1080 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1082 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1086 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1089 switch (dri2_ctx
->base
.ClientAPI
) {
1090 case EGL_OPENGL_ES_API
:
1091 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1093 api
= __DRI_API_GLES
;
1096 api
= __DRI_API_GLES2
;
1099 api
= __DRI_API_GLES3
;
1102 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1107 case EGL_OPENGL_API
:
1108 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1109 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1110 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1111 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1112 api
= __DRI_API_OPENGL_CORE
;
1114 api
= __DRI_API_OPENGL
;
1117 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1123 /* The config chosen here isn't necessarily
1124 * used for surfaces later.
1125 * A pixmap surface will use the single config.
1126 * This opportunity depends on disabling the
1127 * doubleBufferMode check in
1128 * src/mesa/main/context.c:check_compatible()
1130 if (dri2_config
->dri_double_config
[0])
1131 dri_config
= dri2_config
->dri_double_config
[0];
1133 dri_config
= dri2_config
->dri_single_config
[0];
1135 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
1136 * makes sure the back buffer will always be used.
1138 if (conf
->SurfaceType
& EGL_WINDOW_BIT
)
1139 dri2_ctx
->base
.WindowRenderBuffer
= EGL_BACK_BUFFER
;
1144 if (dri2_dpy
->image_driver
) {
1146 unsigned num_attribs
= 8;
1147 uint32_t ctx_attribs
[8];
1149 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1153 dri2_ctx
->dri_context
=
1154 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1162 dri2_create_context_attribs_error(error
);
1163 } else if (dri2_dpy
->dri2
) {
1164 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1166 unsigned num_attribs
= 8;
1167 uint32_t ctx_attribs
[8];
1169 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1173 dri2_ctx
->dri_context
=
1174 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1182 dri2_create_context_attribs_error(error
);
1184 dri2_ctx
->dri_context
=
1185 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1192 assert(dri2_dpy
->swrast
);
1193 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1195 unsigned num_attribs
= 8;
1196 uint32_t ctx_attribs
[8];
1198 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1202 dri2_ctx
->dri_context
=
1203 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1211 dri2_create_context_attribs_error(error
);
1213 dri2_ctx
->dri_context
=
1214 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1222 if (!dri2_ctx
->dri_context
)
1225 return &dri2_ctx
->base
;
1233 * Called via eglDestroyContext(), drv->API.DestroyContext().
1236 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1238 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1239 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1241 if (_eglPutContext(ctx
)) {
1242 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1250 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1252 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1254 if (!_eglPutSurface(surf
))
1257 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1261 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1264 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1265 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1267 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1268 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1269 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1270 _EGLContext
*old_ctx
;
1271 _EGLSurface
*old_dsurf
, *old_rsurf
;
1272 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1273 __DRIdrawable
*ddraw
, *rdraw
;
1278 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1280 /* make new bindings */
1281 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1282 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1286 /* flush before context switch */
1288 dri2_drv
->glFlush();
1290 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1291 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1292 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1295 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1296 dri2_dpy
->core
->unbindContext(old_cctx
);
1299 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1301 if (unbind
|| dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1302 dri2_destroy_surface(drv
, disp
, old_dsurf
);
1303 dri2_destroy_surface(drv
, disp
, old_rsurf
);
1306 dri2_dpy
->ref_count
++;
1308 EGLDisplay old_disp
= _eglGetDisplayHandle(old_ctx
->Resource
.Display
);
1309 dri2_destroy_context(drv
, disp
, old_ctx
);
1310 dri2_display_release(old_disp
);
1315 /* undo the previous _eglBindContext */
1316 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1317 assert(&dri2_ctx
->base
== ctx
&&
1318 tmp_dsurf
== dsurf
&&
1319 tmp_rsurf
== rsurf
);
1321 _eglPutSurface(dsurf
);
1322 _eglPutSurface(rsurf
);
1323 _eglPutContext(ctx
);
1325 _eglPutSurface(old_dsurf
);
1326 _eglPutSurface(old_rsurf
);
1327 _eglPutContext(old_ctx
);
1329 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1330 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1333 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1338 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1340 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1342 return dri2_surf
->dri_drawable
;
1346 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1349 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1351 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1353 return dri2_drv
->get_proc_address(procname
);
1357 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1358 _EGLConfig
*conf
, void *native_window
,
1359 const EGLint
*attrib_list
)
1361 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1362 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1367 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1368 _EGLConfig
*conf
, void *native_pixmap
,
1369 const EGLint
*attrib_list
)
1371 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1372 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1377 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1378 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1380 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1381 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
1385 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1388 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1389 return dri2_dpy
->vtbl
->swap_interval(drv
, dpy
, surf
, interval
);
1393 * Asks the client API to flush any rendering to the drawable so that we can
1394 * do our swapbuffers.
1397 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1399 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1400 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1402 if (dri2_dpy
->flush
) {
1403 if (dri2_dpy
->flush
->base
.version
>= 4) {
1404 /* We know there's a current context because:
1406 * "If surface is not bound to the calling thread’s current
1407 * context, an EGL_BAD_SURFACE error is generated."
1409 _EGLContext
*ctx
= _eglGetCurrentContext();
1410 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1412 /* From the EGL 1.4 spec (page 52):
1414 * "The contents of ancillary buffers are always undefined
1415 * after calling eglSwapBuffers."
1417 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1419 __DRI2_FLUSH_DRAWABLE
|
1420 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1421 __DRI2_THROTTLE_SWAPBUFFER
);
1423 dri2_dpy
->flush
->flush(dri_drawable
);
1429 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1431 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1432 return dri2_dpy
->vtbl
->swap_buffers(drv
, dpy
, surf
);
1436 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1438 const EGLint
*rects
, EGLint n_rects
)
1440 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1441 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, dpy
, surf
,
1446 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1447 EGLint numRects
, const EGLint
*rects
)
1449 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1450 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, dpy
, surf
, numRects
, rects
);
1454 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1455 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1457 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1458 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, dpy
, surf
, x
, y
, width
, height
);
1462 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1463 void *native_pixmap_target
)
1465 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1466 return dri2_dpy
->vtbl
->copy_buffers(drv
, dpy
, surf
, native_pixmap_target
);
1470 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1472 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1473 return dri2_dpy
->vtbl
->query_buffer_age(drv
, dpy
, surf
);
1477 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1479 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1480 _EGLSurface
*surf
= ctx
->DrawSurface
;
1481 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1485 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1486 * we need to copy fake to real here.*/
1488 if (dri2_dpy
->flush
!= NULL
)
1489 dri2_dpy
->flush
->flush(dri_drawable
);
1495 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1500 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1501 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1508 dri2_bind_tex_image(_EGLDriver
*drv
,
1509 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1511 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1512 struct dri2_egl_context
*dri2_ctx
;
1514 GLint format
, target
;
1515 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1517 ctx
= _eglGetCurrentContext();
1518 dri2_ctx
= dri2_egl_context(ctx
);
1520 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1523 switch (surf
->TextureFormat
) {
1524 case EGL_TEXTURE_RGB
:
1525 format
= __DRI_TEXTURE_FORMAT_RGB
;
1527 case EGL_TEXTURE_RGBA
:
1528 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1531 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1532 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1535 switch (surf
->TextureTarget
) {
1536 case EGL_TEXTURE_2D
:
1537 target
= GL_TEXTURE_2D
;
1540 target
= GL_TEXTURE_2D
;
1541 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1544 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1552 dri2_release_tex_image(_EGLDriver
*drv
,
1553 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1555 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1556 struct dri2_egl_context
*dri2_ctx
;
1559 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1561 ctx
= _eglGetCurrentContext();
1562 dri2_ctx
= dri2_egl_context(ctx
);
1564 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1567 switch (surf
->TextureTarget
) {
1568 case EGL_TEXTURE_2D
:
1569 target
= GL_TEXTURE_2D
;
1575 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1576 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1577 (*dri2_dpy
->tex_buffer
->releaseTexBuffer
)(dri2_ctx
->dri_context
,
1586 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
1587 EGLenum target
, EGLClientBuffer buffer
,
1588 const EGLint
*attr_list
)
1590 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1591 return dri2_dpy
->vtbl
->create_image(drv
, dpy
, ctx
, target
, buffer
,
1596 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1598 struct dri2_egl_image
*dri2_img
;
1600 if (dri_image
== NULL
) {
1601 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1605 dri2_img
= malloc(sizeof *dri2_img
);
1607 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1611 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1616 dri2_img
->dri_image
= dri_image
;
1618 return &dri2_img
->base
;
1622 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1623 EGLClientBuffer buffer
,
1624 const EGLint
*attr_list
)
1626 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1627 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1628 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1629 __DRIimage
*dri_image
;
1631 if (renderbuffer
== 0) {
1632 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1633 return EGL_NO_IMAGE_KHR
;
1637 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1638 renderbuffer
, NULL
);
1640 return dri2_create_image_from_dri(disp
, dri_image
);
1643 #ifdef HAVE_WAYLAND_PLATFORM
1645 /* This structure describes how a wl_buffer maps to one or more
1646 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1647 * offsets and strides of the planes in the buffer. This table maps a
1648 * wl_drm format code to a description of the planes in the buffer
1649 * that lets us create a __DRIimage for each of the planes. */
1651 static const struct wl_drm_components_descriptor
{
1652 uint32_t dri_components
;
1655 } wl_drm_components
[] = {
1656 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1657 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1658 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1659 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1660 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1664 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1665 EGLClientBuffer _buffer
,
1666 const EGLint
*attr_list
)
1668 struct wl_drm_buffer
*buffer
;
1669 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1670 const struct wl_drm_components_descriptor
*f
;
1671 __DRIimage
*dri_image
;
1672 _EGLImageAttribs attrs
;
1676 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1677 (struct wl_resource
*) _buffer
);
1681 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1682 plane
= attrs
.PlaneWL
;
1683 if (err
!= EGL_SUCCESS
) {
1684 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1688 f
= buffer
->driver_format
;
1689 if (plane
< 0 || plane
>= f
->nplanes
) {
1690 _eglError(EGL_BAD_PARAMETER
,
1691 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1695 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1697 if (dri_image
== NULL
) {
1698 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1702 return dri2_create_image_from_dri(disp
, dri_image
);
1707 dri2_get_sync_values_chromium(_EGLDisplay
*dpy
, _EGLSurface
*surf
,
1708 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1711 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1712 return dri2_dpy
->vtbl
->get_sync_values(dpy
, surf
, ust
, msc
, sbc
);
1716 * Set the error code after a call to
1717 * dri2_egl_image::dri_image::createImageFromTexture.
1720 dri2_create_image_khr_texture_error(int dri_error
)
1724 switch (dri_error
) {
1725 case __DRI_IMAGE_ERROR_SUCCESS
:
1728 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1729 egl_error
= EGL_BAD_ALLOC
;
1732 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1733 egl_error
= EGL_BAD_MATCH
;
1736 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1737 egl_error
= EGL_BAD_PARAMETER
;
1740 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
1741 egl_error
= EGL_BAD_ACCESS
;
1746 egl_error
= EGL_BAD_MATCH
;
1750 _eglError(egl_error
, "dri2_create_image_khr_texture");
1754 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1756 EGLClientBuffer buffer
,
1757 const EGLint
*attr_list
)
1759 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1760 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1761 struct dri2_egl_image
*dri2_img
;
1762 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
1763 _EGLImageAttribs attrs
;
1769 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1770 return EGL_NO_IMAGE_KHR
;
1773 if (_eglParseImageAttribList(&attrs
, disp
, attr_list
) != EGL_SUCCESS
)
1774 return EGL_NO_IMAGE_KHR
;
1777 case EGL_GL_TEXTURE_2D_KHR
:
1779 gl_target
= GL_TEXTURE_2D
;
1781 case EGL_GL_TEXTURE_3D_KHR
:
1782 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1783 depth
= attrs
.GLTextureZOffset
;
1784 gl_target
= GL_TEXTURE_3D
;
1788 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1789 return EGL_NO_IMAGE_KHR
;
1791 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1792 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1793 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1794 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1795 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1796 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1797 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
1798 gl_target
= GL_TEXTURE_CUBE_MAP
;
1801 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1802 return EGL_NO_IMAGE_KHR
;
1805 dri2_img
= malloc(sizeof *dri2_img
);
1807 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1808 return EGL_NO_IMAGE_KHR
;
1811 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1812 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1814 return EGL_NO_IMAGE_KHR
;
1817 dri2_img
->dri_image
=
1818 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
1822 attrs
.GLTextureLevel
,
1825 dri2_create_image_khr_texture_error(error
);
1827 if (!dri2_img
->dri_image
) {
1829 return EGL_NO_IMAGE_KHR
;
1831 return &dri2_img
->base
;
1835 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1836 EGLint attribute
, EGLint
*value
)
1838 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1839 if (!dri2_dpy
->vtbl
->query_surface
)
1840 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
1841 return dri2_dpy
->vtbl
->query_surface(drv
, dpy
, surf
, attribute
, value
);
1844 static struct wl_buffer
*
1845 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1848 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1849 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, dpy
, img
);
1854 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1855 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1857 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1858 EGLint format
, name
, pitch
, err
;
1859 _EGLImageAttribs attrs
;
1860 __DRIimage
*dri_image
;
1862 name
= (EGLint
) (uintptr_t) buffer
;
1864 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1865 if (err
!= EGL_SUCCESS
)
1868 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
1869 attrs
.DRMBufferStrideMESA
<= 0) {
1870 _eglError(EGL_BAD_PARAMETER
,
1871 "bad width, height or stride");
1875 switch (attrs
.DRMBufferFormatMESA
) {
1876 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1877 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1878 pitch
= attrs
.DRMBufferStrideMESA
;
1881 _eglError(EGL_BAD_PARAMETER
,
1882 "dri2_create_image_khr: unsupported pixmap depth");
1887 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1895 return dri2_create_image_from_dri(disp
, dri_image
);
1899 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
1906 * "Required attributes and their values are as follows:
1908 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1910 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1911 * by drm_fourcc.h and used as the pixel_format parameter of the
1912 * drm_mode_fb_cmd2 ioctl."
1916 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1917 * incomplete, EGL_BAD_PARAMETER is generated."
1919 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
1920 !attrs
->DMABufFourCC
.IsPresent
) {
1921 _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
1928 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1929 * specified for a plane's pitch or offset isn't supported by EGL,
1930 * EGL_BAD_ACCESS is generated."
1932 for (i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
1933 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
1934 attrs
->DMABufPlanePitches
[i
].Value
<= 0) {
1935 _eglError(EGL_BAD_ACCESS
, "invalid pitch");
1943 /* Returns the total number of file descriptors. Zero indicates an error. */
1945 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
1947 unsigned i
, plane_n
;
1949 switch (attrs
->DMABufFourCC
.Value
) {
1951 case DRM_FORMAT_RG88
:
1952 case DRM_FORMAT_GR88
:
1953 case DRM_FORMAT_RGB332
:
1954 case DRM_FORMAT_BGR233
:
1955 case DRM_FORMAT_XRGB4444
:
1956 case DRM_FORMAT_XBGR4444
:
1957 case DRM_FORMAT_RGBX4444
:
1958 case DRM_FORMAT_BGRX4444
:
1959 case DRM_FORMAT_ARGB4444
:
1960 case DRM_FORMAT_ABGR4444
:
1961 case DRM_FORMAT_RGBA4444
:
1962 case DRM_FORMAT_BGRA4444
:
1963 case DRM_FORMAT_XRGB1555
:
1964 case DRM_FORMAT_XBGR1555
:
1965 case DRM_FORMAT_RGBX5551
:
1966 case DRM_FORMAT_BGRX5551
:
1967 case DRM_FORMAT_ARGB1555
:
1968 case DRM_FORMAT_ABGR1555
:
1969 case DRM_FORMAT_RGBA5551
:
1970 case DRM_FORMAT_BGRA5551
:
1971 case DRM_FORMAT_RGB565
:
1972 case DRM_FORMAT_BGR565
:
1973 case DRM_FORMAT_RGB888
:
1974 case DRM_FORMAT_BGR888
:
1975 case DRM_FORMAT_XRGB8888
:
1976 case DRM_FORMAT_XBGR8888
:
1977 case DRM_FORMAT_RGBX8888
:
1978 case DRM_FORMAT_BGRX8888
:
1979 case DRM_FORMAT_ARGB8888
:
1980 case DRM_FORMAT_ABGR8888
:
1981 case DRM_FORMAT_RGBA8888
:
1982 case DRM_FORMAT_BGRA8888
:
1983 case DRM_FORMAT_XRGB2101010
:
1984 case DRM_FORMAT_XBGR2101010
:
1985 case DRM_FORMAT_RGBX1010102
:
1986 case DRM_FORMAT_BGRX1010102
:
1987 case DRM_FORMAT_ARGB2101010
:
1988 case DRM_FORMAT_ABGR2101010
:
1989 case DRM_FORMAT_RGBA1010102
:
1990 case DRM_FORMAT_BGRA1010102
:
1991 case DRM_FORMAT_YUYV
:
1992 case DRM_FORMAT_YVYU
:
1993 case DRM_FORMAT_UYVY
:
1994 case DRM_FORMAT_VYUY
:
1997 case DRM_FORMAT_NV12
:
1998 case DRM_FORMAT_NV21
:
1999 case DRM_FORMAT_NV16
:
2000 case DRM_FORMAT_NV61
:
2003 case DRM_FORMAT_YUV410
:
2004 case DRM_FORMAT_YVU410
:
2005 case DRM_FORMAT_YUV411
:
2006 case DRM_FORMAT_YVU411
:
2007 case DRM_FORMAT_YUV420
:
2008 case DRM_FORMAT_YVU420
:
2009 case DRM_FORMAT_YUV422
:
2010 case DRM_FORMAT_YVU422
:
2011 case DRM_FORMAT_YUV444
:
2012 case DRM_FORMAT_YVU444
:
2016 _eglError(EGL_BAD_ATTRIBUTE
, "invalid format");
2023 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2024 * incomplete, EGL_BAD_PARAMETER is generated."
2026 for (i
= 0; i
< plane_n
; ++i
) {
2027 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2028 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2029 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2030 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2036 * The spec also says:
2038 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2039 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2040 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2041 * attributes are specified."
2043 for (i
= plane_n
; i
< 3; ++i
) {
2044 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2045 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2046 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2047 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2058 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2059 * EGL will take a reference to the dma_buf(s) which it will release at any
2060 * time while the EGLDisplay is initialized. It is the responsibility of the
2061 * application to close the dma_buf file descriptors."
2063 * Therefore we must never close or otherwise modify the file descriptors.
2066 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2067 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2069 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2072 _EGLImageAttribs attrs
;
2073 __DRIimage
*dri_image
;
2084 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2085 * error EGL_BAD_PARAMETER is generated."
2087 if (buffer
!= NULL
) {
2088 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2092 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2093 if (err
!= EGL_SUCCESS
) {
2094 _eglError(err
, "bad attribute");
2098 if (!dri2_check_dma_buf_attribs(&attrs
))
2101 num_fds
= dri2_check_dma_buf_format(&attrs
);
2105 for (i
= 0; i
< num_fds
; ++i
) {
2106 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2107 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2108 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2112 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2113 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2114 fds
, num_fds
, pitches
, offsets
,
2115 attrs
.DMABufYuvColorSpaceHint
.Value
,
2116 attrs
.DMABufSampleRangeHint
.Value
,
2117 attrs
.DMABufChromaHorizontalSiting
.Value
,
2118 attrs
.DMABufChromaVerticalSiting
.Value
,
2121 dri2_create_image_khr_texture_error(error
);
2124 return EGL_NO_IMAGE_KHR
;
2126 res
= dri2_create_image_from_dri(disp
, dri_image
);
2131 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2132 const EGLint
*attr_list
)
2134 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2135 struct dri2_egl_image
*dri2_img
;
2136 _EGLImageAttribs attrs
;
2137 unsigned int dri_use
, valid_mask
;
2139 EGLint err
= EGL_SUCCESS
;
2143 dri2_img
= malloc(sizeof *dri2_img
);
2145 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2146 return EGL_NO_IMAGE_KHR
;
2150 err
= EGL_BAD_PARAMETER
;
2154 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
2155 err
= EGL_BAD_PARAMETER
;
2159 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2160 if (err
!= EGL_SUCCESS
)
2163 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2164 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)",
2165 attrs
.Width
, attrs
.Height
);
2169 switch (attrs
.DRMBufferFormatMESA
) {
2170 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2171 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2174 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x",
2175 attrs
.DRMBufferFormatMESA
);
2180 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2181 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2182 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2183 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2184 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x",
2185 attrs
.DRMBufferUseMESA
& ~valid_mask
);
2190 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2191 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2192 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2193 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2194 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2195 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2197 dri2_img
->dri_image
=
2198 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2199 attrs
.Width
, attrs
.Height
,
2200 format
, dri_use
, dri2_img
);
2201 if (dri2_img
->dri_image
== NULL
) {
2202 err
= EGL_BAD_ALLOC
;
2206 return &dri2_img
->base
;
2210 _eglError(err
, "dri2_create_drm_image_mesa");
2212 return EGL_NO_IMAGE_KHR
;
2216 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2217 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2219 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2220 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2224 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2225 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
2226 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2231 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2232 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2235 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2236 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2242 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2244 EGLint
*fourcc
, EGLint
*nplanes
,
2245 EGLuint64KHR
*modifiers
)
2247 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2248 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2254 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2255 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2257 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2258 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2267 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2268 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2270 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2271 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2275 /* rework later to provide multiple fds/strides/offsets */
2277 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2278 __DRI_IMAGE_ATTRIB_FD
, fds
);
2281 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2282 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2286 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2287 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2289 offsets
[0] = img_offset
;
2300 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2301 _EGLContext
*ctx
, EGLenum target
,
2302 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2307 case EGL_GL_TEXTURE_2D_KHR
:
2308 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2309 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2310 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2311 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2312 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2313 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2314 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2315 case EGL_GL_TEXTURE_3D_KHR
:
2316 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
2317 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2320 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2321 return EGL_NO_IMAGE_KHR
;
2323 case EGL_GL_RENDERBUFFER_KHR
:
2324 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2326 case EGL_DRM_BUFFER_MESA
:
2327 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2328 case EGL_LINUX_DMA_BUF_EXT
:
2329 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2331 #ifdef HAVE_WAYLAND_PLATFORM
2332 case EGL_WAYLAND_BUFFER_WL
:
2333 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2336 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2337 return EGL_NO_IMAGE_KHR
;
2342 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2344 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2345 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2349 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2355 #ifdef HAVE_WAYLAND_PLATFORM
2358 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2359 struct wl_drm_buffer
*buffer
)
2361 _EGLDisplay
*disp
= user_data
;
2362 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2364 int i
, dri_components
= 0;
2367 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2376 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2388 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2390 buffer
->driver_format
= NULL
;
2391 for (i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2392 if (wl_drm_components
[i
].dri_components
== dri_components
)
2393 buffer
->driver_format
= &wl_drm_components
[i
];
2395 if (buffer
->driver_format
== NULL
)
2396 dri2_dpy
->image
->destroyImage(img
);
2398 buffer
->driver_buffer
= img
;
2402 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2404 _EGLDisplay
*disp
= user_data
;
2405 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2407 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2410 static struct wayland_drm_callbacks wl_drm_callbacks
= {
2411 .authenticate
= NULL
,
2412 .reference_buffer
= dri2_wl_reference_buffer
,
2413 .release_buffer
= dri2_wl_release_buffer
2417 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2418 struct wl_display
*wl_dpy
)
2420 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2426 if (dri2_dpy
->wl_server_drm
)
2429 wl_drm_callbacks
.authenticate
=
2430 (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
;
2432 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2433 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2434 dri2_dpy
->image
->base
.version
>= 7 &&
2435 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2436 flags
|= WAYLAND_DRM_PRIME
;
2438 dri2_dpy
->wl_server_drm
=
2439 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2440 &wl_drm_callbacks
, disp
, flags
);
2442 if (!dri2_dpy
->wl_server_drm
)
2445 #ifdef HAVE_DRM_PLATFORM
2446 /* We have to share the wl_drm instance with gbm, so gbm can convert
2447 * wl_buffers to gbm bos. */
2448 if (dri2_dpy
->gbm_dri
)
2449 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2456 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2457 struct wl_display
*wl_dpy
)
2459 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2463 if (!dri2_dpy
->wl_server_drm
)
2466 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2467 dri2_dpy
->wl_server_drm
= NULL
;
2473 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2474 struct wl_resource
*buffer_resource
,
2475 EGLint attribute
, EGLint
*value
)
2477 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2478 struct wl_drm_buffer
*buffer
;
2479 const struct wl_drm_components_descriptor
*format
;
2481 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2485 format
= buffer
->driver_format
;
2486 switch (attribute
) {
2487 case EGL_TEXTURE_FORMAT
:
2488 *value
= format
->components
;
2491 *value
= buffer
->width
;
2494 *value
= buffer
->height
;
2503 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2505 p_atomic_inc(&sync
->refcount
);
2509 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2510 struct dri2_egl_sync
*dri2_sync
)
2512 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2513 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
)
2514 cnd_destroy(&dri2_sync
->cond
);
2516 if (dri2_sync
->fence
)
2517 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2524 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
2525 EGLenum type
, const EGLAttrib
*attrib_list
)
2527 _EGLContext
*ctx
= _eglGetCurrentContext();
2528 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2529 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2530 struct dri2_egl_sync
*dri2_sync
;
2532 pthread_condattr_t attr
;
2534 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2536 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2540 if (!_eglInitSync(&dri2_sync
->base
, dpy
, type
, attrib_list
)) {
2546 case EGL_SYNC_FENCE_KHR
:
2547 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2548 if (!dri2_sync
->fence
) {
2549 /* Why did it fail? DRI doesn't return an error code, so we emit
2550 * a generic EGL error that doesn't communicate user error.
2552 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2558 case EGL_SYNC_CL_EVENT_KHR
:
2559 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2560 dri2_dpy
->dri_screen
,
2561 dri2_sync
->base
.CLEvent
);
2562 /* this can only happen if the cl_event passed in is invalid. */
2563 if (!dri2_sync
->fence
) {
2564 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2569 /* the initial status must be "signaled" if the cl_event is signaled */
2570 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2571 dri2_sync
->fence
, 0, 0))
2572 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2575 case EGL_SYNC_REUSABLE_KHR
:
2576 /* intialize attr */
2577 ret
= pthread_condattr_init(&attr
);
2580 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2585 /* change clock attribute to CLOCK_MONOTONIC */
2586 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
2589 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2594 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
2597 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2602 /* initial status of reusable sync must be "unsignaled" */
2603 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
2607 p_atomic_set(&dri2_sync
->refcount
, 1);
2608 return &dri2_sync
->base
;
2612 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2614 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2615 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2616 EGLint ret
= EGL_TRUE
;
2619 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
2620 * then unlock all threads possibly blocked by the reusable sync before
2623 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
2624 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2625 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2626 /* unblock all threads currently blocked by sync */
2627 err
= cnd_broadcast(&dri2_sync
->cond
);
2630 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
2634 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2640 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2641 EGLint flags
, EGLTime timeout
)
2643 _EGLContext
*ctx
= _eglGetCurrentContext();
2644 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2645 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2646 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2647 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2648 unsigned wait_flags
= 0;
2650 /* timespecs for cnd_timedwait */
2651 struct timespec current
;
2654 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
2656 /* The EGL_KHR_fence_sync spec states:
2658 * "If no context is current for the bound API,
2659 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
2661 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
2662 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
2664 /* the sync object should take a reference while waiting */
2665 dri2_egl_ref_sync(dri2_sync
);
2667 switch (sync
->Type
) {
2668 case EGL_SYNC_FENCE_KHR
:
2669 case EGL_SYNC_CL_EVENT_KHR
:
2670 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
2671 dri2_sync
->fence
, wait_flags
,
2673 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2675 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2678 case EGL_SYNC_REUSABLE_KHR
:
2679 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
2680 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
2681 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
2682 dri2_drv
->glFlush();
2685 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
2686 if (timeout
== EGL_FOREVER_KHR
) {
2687 mtx_lock(&dri2_sync
->mutex
);
2688 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
2689 mtx_unlock(&dri2_sync
->mutex
);
2691 /* if reusable sync has not been yet signaled */
2692 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
2693 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
2695 /* calculating when to expire */
2696 expire
.nsec
= timeout
% 1000000000L;
2697 expire
.sec
= timeout
/ 1000000000L;
2699 expire
.nsec
+= current
.tv_nsec
;
2700 expire
.sec
+= current
.tv_sec
;
2702 /* expire.nsec now is a number between 0 and 1999999998 */
2703 if (expire
.nsec
> 999999999L) {
2705 expire
.nsec
-= 1000000000L;
2708 mtx_lock(&dri2_sync
->mutex
);
2709 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
2710 mtx_unlock(&dri2_sync
->mutex
);
2712 if (ret
== thrd_busy
) {
2713 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2714 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2716 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2724 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2730 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2733 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2736 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
) {
2737 _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
2741 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
) {
2742 _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
2746 dri2_sync
->base
.SyncStatus
= mode
;
2748 if (mode
== EGL_SIGNALED_KHR
) {
2749 ret
= cnd_broadcast(&dri2_sync
->cond
);
2751 /* fail to broadcast */
2753 _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
2762 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2764 _EGLContext
*ctx
= _eglGetCurrentContext();
2765 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2766 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2767 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2769 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
2770 dri2_sync
->fence
, 0);
2775 dri2_interop_query_device_info(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2776 struct mesa_glinterop_device_info
*out
)
2778 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2779 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2781 if (!dri2_dpy
->interop
)
2782 return MESA_GLINTEROP_UNSUPPORTED
;
2784 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
2788 dri2_interop_export_object(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2789 struct mesa_glinterop_export_in
*in
,
2790 struct mesa_glinterop_export_out
*out
)
2792 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2793 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2795 if (!dri2_dpy
->interop
)
2796 return MESA_GLINTEROP_UNSUPPORTED
;
2798 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
2802 dri2_unload(_EGLDriver
*drv
)
2804 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2806 dlclose(dri2_drv
->handle
);
2811 dri2_load(_EGLDriver
*drv
)
2813 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2814 #ifdef HAVE_ANDROID_PLATFORM
2815 const char *libname
= "libglapi.so";
2816 #elif defined(__APPLE__)
2817 const char *libname
= "libglapi.0.dylib";
2818 #elif defined(__CYGWIN__)
2819 const char *libname
= "cygglapi-0.dll";
2821 const char *libname
= "libglapi.so.0";
2825 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2826 handle
= dlopen(libname
, RTLD_LAZY
| RTLD_GLOBAL
);
2828 _eglLog(_EGL_WARNING
, "DRI2: failed to open glapi provider");
2832 dri2_drv
->get_proc_address
= (_EGLProc (*)(const char *))
2833 dlsym(handle
, "_glapi_get_proc_address");
2835 /* if glapi is not available, loading DRI drivers will fail */
2836 if (!dri2_drv
->get_proc_address
) {
2837 _eglLog(_EGL_WARNING
, "DRI2: failed to find _glapi_get_proc_address");
2841 dri2_drv
->glFlush
= (void (*)(void))
2842 dri2_drv
->get_proc_address("glFlush");
2844 /* if glFlush is not available things are horribly broken */
2845 if (!dri2_drv
->glFlush
) {
2846 _eglLog(_EGL_WARNING
, "DRI2: failed to find glFlush entry point");
2850 dri2_drv
->handle
= handle
;
2860 * This is the main entrypoint into the driver, called by libEGL.
2861 * Create a new _EGLDriver object and init its dispatch table.
2864 _eglBuiltInDriverDRI2(const char *args
)
2866 struct dri2_egl_driver
*dri2_drv
;
2870 dri2_drv
= calloc(1, sizeof *dri2_drv
);
2874 if (!dri2_load(&dri2_drv
->base
)) {
2879 _eglInitDriverFallbacks(&dri2_drv
->base
);
2880 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
2881 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
2882 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
2883 dri2_drv
->base
.API
.DestroyContext
= dri2_destroy_context
;
2884 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
2885 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
2886 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
2887 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
2888 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
2889 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
2890 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
2891 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
2892 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
2893 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
2894 dri2_drv
->base
.API
.SwapInterval
= dri2_swap_interval
;
2895 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
2896 dri2_drv
->base
.API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
2897 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
2898 dri2_drv
->base
.API
.PostSubBufferNV
= dri2_post_sub_buffer
;
2899 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
,
2900 dri2_drv
->base
.API
.QueryBufferAge
= dri2_query_buffer_age
;
2901 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image
;
2902 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
2903 dri2_drv
->base
.API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
2904 dri2_drv
->base
.API
.QuerySurface
= dri2_query_surface
;
2906 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
2907 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
2908 dri2_drv
->base
.API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
2909 dri2_drv
->base
.API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
2911 #ifdef HAVE_WAYLAND_PLATFORM
2912 dri2_drv
->base
.API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
2913 dri2_drv
->base
.API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
2914 dri2_drv
->base
.API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
2916 dri2_drv
->base
.API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
2917 dri2_drv
->base
.API
.CreateSyncKHR
= dri2_create_sync
;
2918 dri2_drv
->base
.API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
2919 dri2_drv
->base
.API
.SignalSyncKHR
= dri2_signal_sync
;
2920 dri2_drv
->base
.API
.WaitSyncKHR
= dri2_server_wait_sync
;
2921 dri2_drv
->base
.API
.DestroySyncKHR
= dri2_destroy_sync
;
2922 dri2_drv
->base
.API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
2923 dri2_drv
->base
.API
.GLInteropExportObject
= dri2_interop_export_object
;
2925 dri2_drv
->base
.Name
= "DRI2";
2926 dri2_drv
->base
.Unload
= dri2_unload
;
2928 return &dri2_drv
->base
;