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 "loader/loader.h"
60 #include "util/u_atomic.h"
62 /* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
63 * some of the definitions here so that building Mesa won't bleeding-edge
67 #define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
70 #ifndef DRM_FORMAT_RG88
71 #define DRM_FORMAT_RG88 fourcc_code('R', 'G', '8', '8') /* [15:0] R:G 8:8 little endian */
74 #ifndef DRM_FORMAT_GR88
75 #define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
78 #ifndef DRM_FORMAT_R16
79 #define DRM_FORMAT_R16 fourcc_code('R', '1', '6', ' ') /* [15:0] R 16 little endian */
82 #ifndef DRM_FORMAT_GR1616
83 #define DRM_FORMAT_GR1616 fourcc_code('G', 'R', '3', '2') /* [31:0] R:G 16:16 little endian */
86 const __DRIuseInvalidateExtension use_invalidate
= {
87 .base
= { __DRI_USE_INVALIDATE
, 1 }
90 EGLint dri2_to_egl_attribute_map
[] = {
92 EGL_BUFFER_SIZE
, /* __DRI_ATTRIB_BUFFER_SIZE */
93 EGL_LEVEL
, /* __DRI_ATTRIB_LEVEL */
94 EGL_RED_SIZE
, /* __DRI_ATTRIB_RED_SIZE */
95 EGL_GREEN_SIZE
, /* __DRI_ATTRIB_GREEN_SIZE */
96 EGL_BLUE_SIZE
, /* __DRI_ATTRIB_BLUE_SIZE */
97 EGL_LUMINANCE_SIZE
, /* __DRI_ATTRIB_LUMINANCE_SIZE */
98 EGL_ALPHA_SIZE
, /* __DRI_ATTRIB_ALPHA_SIZE */
99 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
100 EGL_DEPTH_SIZE
, /* __DRI_ATTRIB_DEPTH_SIZE */
101 EGL_STENCIL_SIZE
, /* __DRI_ATTRIB_STENCIL_SIZE */
102 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
103 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
104 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
105 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
106 EGL_SAMPLE_BUFFERS
, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
107 EGL_SAMPLES
, /* __DRI_ATTRIB_SAMPLES */
108 0, /* __DRI_ATTRIB_RENDER_TYPE, */
109 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
110 0, /* __DRI_ATTRIB_CONFORMANT */
111 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
112 0, /* __DRI_ATTRIB_STEREO */
113 0, /* __DRI_ATTRIB_AUX_BUFFERS */
114 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
115 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
116 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
117 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
118 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
119 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
120 0, /* __DRI_ATTRIB_FLOAT_MODE (deprecated) */
121 0, /* __DRI_ATTRIB_RED_MASK */
122 0, /* __DRI_ATTRIB_GREEN_MASK */
123 0, /* __DRI_ATTRIB_BLUE_MASK */
124 0, /* __DRI_ATTRIB_ALPHA_MASK */
125 EGL_MAX_PBUFFER_WIDTH
, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
126 EGL_MAX_PBUFFER_HEIGHT
, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
127 EGL_MAX_PBUFFER_PIXELS
, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
128 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
129 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
130 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
131 0, /* __DRI_ATTRIB_SWAP_METHOD */
132 EGL_MAX_SWAP_INTERVAL
, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
133 EGL_MIN_SWAP_INTERVAL
, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
134 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
135 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
136 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
137 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
138 EGL_Y_INVERTED_NOK
, /* __DRI_ATTRIB_YINVERTED */
139 0, /* __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE */
143 dri2_get_dri_config(struct dri2_egl_config
*conf
, EGLint surface_type
,
146 const bool srgb
= colorspace
== EGL_GL_COLORSPACE_SRGB_KHR
;
148 return surface_type
== EGL_WINDOW_BIT
? conf
->dri_double_config
[srgb
] :
149 conf
->dri_single_config
[srgb
];
153 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
155 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
158 if (!_eglMatchConfig(conf
, criteria
))
164 struct dri2_egl_config
*
165 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
166 EGLint surface_type
, const EGLint
*attr_list
,
167 const unsigned int *rgba_masks
)
169 struct dri2_egl_config
*conf
;
170 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
172 unsigned int attrib
, value
, double_buffer
;
174 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
175 unsigned int dri_masks
[4] = { 0, 0, 0, 0 };
176 _EGLConfig
*matching_config
;
177 EGLint num_configs
= 0;
181 _eglInitConfig(&base
, disp
, id
);
185 bind_to_texture_rgb
= 0;
186 bind_to_texture_rgba
= 0;
188 while (dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
++, &attrib
, &value
)) {
190 case __DRI_ATTRIB_RENDER_TYPE
:
191 if (value
& __DRI_ATTRIB_RGBA_BIT
)
192 value
= EGL_RGB_BUFFER
;
193 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
194 value
= EGL_LUMINANCE_BUFFER
;
197 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
200 case __DRI_ATTRIB_CONFIG_CAVEAT
:
201 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
202 value
= EGL_NON_CONFORMANT_CONFIG
;
203 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
204 value
= EGL_SLOW_CONFIG
;
207 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
210 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
211 bind_to_texture_rgb
= value
;
214 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
215 bind_to_texture_rgba
= value
;
218 case __DRI_ATTRIB_DOUBLE_BUFFER
:
219 double_buffer
= value
;
222 case __DRI_ATTRIB_RED_MASK
:
223 dri_masks
[0] = value
;
226 case __DRI_ATTRIB_GREEN_MASK
:
227 dri_masks
[1] = value
;
230 case __DRI_ATTRIB_BLUE_MASK
:
231 dri_masks
[2] = value
;
234 case __DRI_ATTRIB_ALPHA_MASK
:
235 dri_masks
[3] = value
;
238 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
239 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
240 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
241 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
242 /* Don't expose visuals with the accumulation buffer. */
247 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
249 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
253 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH
:
254 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_WIDTH
,
255 _EGL_MAX_PBUFFER_WIDTH
);
257 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT
:
258 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_HEIGHT
,
259 _EGL_MAX_PBUFFER_HEIGHT
);
263 key
= dri2_to_egl_attribute_map
[attrib
];
265 _eglSetConfigKey(&base
, key
, value
);
271 for (i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
272 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
274 if (rgba_masks
&& memcmp(rgba_masks
, dri_masks
, sizeof(dri_masks
)))
277 base
.NativeRenderable
= EGL_TRUE
;
279 base
.SurfaceType
= surface_type
;
280 if (surface_type
& (EGL_PBUFFER_BIT
|
281 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
282 base
.BindToTextureRGB
= bind_to_texture_rgb
;
283 if (base
.AlphaSize
> 0)
284 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
287 base
.RenderableType
= disp
->ClientAPIs
;
288 base
.Conformant
= disp
->ClientAPIs
;
290 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
291 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
293 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
294 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
298 config_id
= base
.ConfigID
;
299 base
.ConfigID
= EGL_DONT_CARE
;
300 base
.SurfaceType
= EGL_DONT_CARE
;
301 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
302 (_EGLArrayForEach
) dri2_match_config
, &base
);
304 if (num_configs
== 1) {
305 conf
= (struct dri2_egl_config
*) matching_config
;
307 if (double_buffer
&& !conf
->dri_double_config
[srgb
])
308 conf
->dri_double_config
[srgb
] = dri_config
;
309 else if (!double_buffer
&& !conf
->dri_single_config
[srgb
])
310 conf
->dri_single_config
[srgb
] = dri_config
;
312 /* a similar config type is already added (unlikely) => discard */
315 else if (num_configs
== 0) {
316 conf
= calloc(1, sizeof *conf
);
321 conf
->dri_double_config
[srgb
] = dri_config
;
323 conf
->dri_single_config
[srgb
] = dri_config
;
325 memcpy(&conf
->base
, &base
, sizeof base
);
326 conf
->base
.SurfaceType
= 0;
327 conf
->base
.ConfigID
= config_id
;
329 _eglLinkConfig(&conf
->base
);
337 surface_type
&= ~EGL_PIXMAP_BIT
;
340 /* No support for pbuffer + MSAA for now.
342 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
343 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
346 surface_type
&= ~EGL_PBUFFER_BIT
;
349 conf
->base
.SurfaceType
|= surface_type
;
355 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
357 _EGLDisplay
*disp
= data
;
358 struct dri2_egl_image
*dri2_img
;
363 img
= _eglLookupImage(image
, disp
);
365 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
369 dri2_img
= dri2_egl_image(image
);
371 return dri2_img
->dri_image
;
374 const __DRIimageLookupExtension image_lookup_extension
= {
375 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
377 .lookupEGLImage
= dri2_lookup_egl_image
380 struct dri2_extension_match
{
386 static const struct dri2_extension_match dri3_driver_extensions
[] = {
387 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
388 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
392 static const struct dri2_extension_match dri2_driver_extensions
[] = {
393 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
394 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
398 static const struct dri2_extension_match dri2_core_extensions
[] = {
399 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
400 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
401 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
405 static const struct dri2_extension_match swrast_driver_extensions
[] = {
406 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
407 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
411 static const struct dri2_extension_match swrast_core_extensions
[] = {
412 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
416 static const struct dri2_extension_match optional_core_extensions
[] = {
417 { __DRI2_ROBUSTNESS
, 1, offsetof(struct dri2_egl_display
, robustness
) },
418 { __DRI2_CONFIG_QUERY
, 1, offsetof(struct dri2_egl_display
, config
) },
419 { __DRI2_FENCE
, 1, offsetof(struct dri2_egl_display
, fence
) },
420 { __DRI2_RENDERER_QUERY
, 1, offsetof(struct dri2_egl_display
, rendererQuery
) },
421 { __DRI2_INTEROP
, 1, offsetof(struct dri2_egl_display
, interop
) },
426 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
427 const struct dri2_extension_match
*matches
,
428 const __DRIextension
**extensions
,
431 int i
, j
, ret
= EGL_TRUE
;
434 for (i
= 0; extensions
[i
]; i
++) {
435 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
436 for (j
= 0; matches
[j
].name
; j
++) {
437 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
438 extensions
[i
]->version
>= matches
[j
].version
) {
439 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
440 *(const __DRIextension
**) field
= extensions
[i
];
441 _eglLog(_EGL_INFO
, "found extension %s version %d",
442 extensions
[i
]->name
, extensions
[i
]->version
);
448 for (j
= 0; matches
[j
].name
; j
++) {
449 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
450 if (*(const __DRIextension
**) field
== NULL
) {
452 _eglLog(_EGL_DEBUG
, "did not find optional extension %s version %d",
453 matches
[j
].name
, matches
[j
].version
);
455 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
456 matches
[j
].name
, matches
[j
].version
);
465 static const __DRIextension
**
466 dri2_open_driver(_EGLDisplay
*disp
)
468 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
469 const __DRIextension
**extensions
= NULL
;
470 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
471 char *get_extensions_name
;
472 const __DRIextension
**(*get_extensions
)(void);
475 if (geteuid() == getuid()) {
476 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
477 search_paths
= getenv("LIBGL_DRIVERS_PATH");
479 if (search_paths
== NULL
)
480 search_paths
= DEFAULT_DRIVER_DIR
;
482 dri2_dpy
->driver
= NULL
;
483 end
= search_paths
+ strlen(search_paths
);
484 for (p
= search_paths
; p
< end
; p
= next
+ 1) {
486 next
= strchr(p
, ':');
492 snprintf(path
, sizeof path
,
493 "%.*s/tls/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
494 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
496 if (dri2_dpy
->driver
== NULL
) {
497 snprintf(path
, sizeof path
,
498 "%.*s/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
499 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
500 if (dri2_dpy
->driver
== NULL
)
501 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
503 /* not need continue to loop all paths once the driver is found */
504 if (dri2_dpy
->driver
!= NULL
)
508 snprintf(path
, sizeof path
, "%.*s/gallium_dri.so", len
, p
);
509 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
510 if (dri2_dpy
->driver
== NULL
)
511 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
517 if (dri2_dpy
->driver
== NULL
) {
518 _eglLog(_EGL_WARNING
,
519 "DRI2: failed to open %s (search paths %s)",
520 dri2_dpy
->driver_name
, search_paths
);
524 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
526 get_extensions_name
= loader_get_extensions_name(dri2_dpy
->driver_name
);
527 if (get_extensions_name
) {
528 get_extensions
= dlsym(dri2_dpy
->driver
, get_extensions_name
);
529 if (get_extensions
) {
530 extensions
= get_extensions();
532 _eglLog(_EGL_DEBUG
, "driver does not expose %s(): %s\n",
533 get_extensions_name
, dlerror());
535 free(get_extensions_name
);
539 extensions
= dlsym(dri2_dpy
->driver
, __DRI_DRIVER_EXTENSIONS
);
540 if (extensions
== NULL
) {
541 _eglLog(_EGL_WARNING
,
542 "DRI2: driver exports no extensions (%s)", dlerror());
543 dlclose(dri2_dpy
->driver
);
550 dri2_load_driver_dri3(_EGLDisplay
*disp
)
552 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
553 const __DRIextension
**extensions
;
555 extensions
= dri2_open_driver(disp
);
559 if (!dri2_bind_extensions(dri2_dpy
, dri3_driver_extensions
, extensions
, false)) {
560 dlclose(dri2_dpy
->driver
);
563 dri2_dpy
->driver_extensions
= extensions
;
569 dri2_load_driver(_EGLDisplay
*disp
)
571 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
572 const __DRIextension
**extensions
;
574 extensions
= dri2_open_driver(disp
);
578 if (!dri2_bind_extensions(dri2_dpy
, dri2_driver_extensions
, extensions
, false)) {
579 dlclose(dri2_dpy
->driver
);
582 dri2_dpy
->driver_extensions
= extensions
;
588 dri2_load_driver_swrast(_EGLDisplay
*disp
)
590 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
591 const __DRIextension
**extensions
;
593 extensions
= dri2_open_driver(disp
);
597 if (!dri2_bind_extensions(dri2_dpy
, swrast_driver_extensions
, extensions
, false)) {
598 dlclose(dri2_dpy
->driver
);
601 dri2_dpy
->driver_extensions
= extensions
;
607 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
609 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
610 unsigned int value
= 0;
612 if (!rendererQuery
||
613 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
620 dri2_setup_screen(_EGLDisplay
*disp
)
622 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
623 unsigned int api_mask
;
625 if (dri2_dpy
->image_driver
) {
626 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
627 } else if (dri2_dpy
->dri2
) {
628 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
630 assert(dri2_dpy
->swrast
);
631 api_mask
= 1 << __DRI_API_OPENGL
|
632 1 << __DRI_API_GLES
|
633 1 << __DRI_API_GLES2
|
634 1 << __DRI_API_GLES3
;
637 disp
->ClientAPIs
= 0;
638 if ((api_mask
& (1 <<__DRI_API_OPENGL
)) && _eglIsApiValid(EGL_OPENGL_API
))
639 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
640 if ((api_mask
& (1 << __DRI_API_GLES
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
641 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
642 if ((api_mask
& (1 << __DRI_API_GLES2
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
643 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
644 if ((api_mask
& (1 << __DRI_API_GLES3
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
645 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
647 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
648 disp
->Extensions
.KHR_no_config_context
= EGL_TRUE
;
649 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
651 if (dri2_renderer_query_integer(dri2_dpy
,
652 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
653 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
655 if (dri2_dpy
->image_driver
||
656 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
657 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
658 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
660 if (dri2_dpy
->robustness
)
661 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
664 if (dri2_dpy
->fence
) {
665 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
666 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
667 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
668 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
669 if (dri2_dpy
->fence
->base
.version
>= 2) {
670 unsigned capabilities
=
671 dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
);
672 disp
->Extensions
.ANDROID_native_fence_sync
=
673 (capabilities
& __DRI_FENCE_CAP_NATIVE_FD
) != 0;
677 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
679 if (dri2_dpy
->image
) {
680 if (dri2_dpy
->image
->base
.version
>= 10 &&
681 dri2_dpy
->image
->getCapabilities
!= NULL
) {
684 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
685 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
687 if (dri2_dpy
->image
->base
.version
>= 11)
688 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
690 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
691 if (dri2_dpy
->image
->base
.version
>= 11)
692 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
695 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
696 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
697 if (dri2_dpy
->image
->base
.version
>= 5 &&
698 dri2_dpy
->image
->createImageFromTexture
) {
699 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
700 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
702 if (dri2_renderer_query_integer(dri2_dpy
,
703 __DRI2_RENDERER_HAS_TEXTURE_3D
))
704 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
706 if (dri2_dpy
->image
->base
.version
>= 8 &&
707 dri2_dpy
->image
->createImageFromDmaBufs
) {
708 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
714 /* All platforms but DRM call this function to create the screen, query the
715 * dri extensions, setup the vtables and populate the driver_configs.
716 * DRM inherits all that information from its display - GBM.
719 dri2_create_screen(_EGLDisplay
*disp
)
721 const __DRIextension
**extensions
;
722 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
724 if (dri2_dpy
->image_driver
) {
725 dri2_dpy
->dri_screen
=
726 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
727 dri2_dpy
->loader_extensions
,
728 dri2_dpy
->driver_extensions
,
729 &dri2_dpy
->driver_configs
,
731 } else if (dri2_dpy
->dri2
) {
732 if (dri2_dpy
->dri2
->base
.version
>= 4) {
733 dri2_dpy
->dri_screen
=
734 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
735 dri2_dpy
->loader_extensions
,
736 dri2_dpy
->driver_extensions
,
737 &dri2_dpy
->driver_configs
, disp
);
739 dri2_dpy
->dri_screen
=
740 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
741 dri2_dpy
->loader_extensions
,
742 &dri2_dpy
->driver_configs
, disp
);
745 assert(dri2_dpy
->swrast
);
746 if (dri2_dpy
->swrast
->base
.version
>= 4) {
747 dri2_dpy
->dri_screen
=
748 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->loader_extensions
,
749 dri2_dpy
->driver_extensions
,
750 &dri2_dpy
->driver_configs
, disp
);
752 dri2_dpy
->dri_screen
=
753 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->loader_extensions
,
754 &dri2_dpy
->driver_configs
, disp
);
758 if (dri2_dpy
->dri_screen
== NULL
) {
759 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
763 dri2_dpy
->own_dri_screen
= 1;
765 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
767 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
) {
768 if (!dri2_bind_extensions(dri2_dpy
, dri2_core_extensions
, extensions
, false))
769 goto cleanup_dri_screen
;
771 assert(dri2_dpy
->swrast
);
772 if (!dri2_bind_extensions(dri2_dpy
, swrast_core_extensions
, extensions
, false))
773 goto cleanup_dri_screen
;
776 dri2_bind_extensions(dri2_dpy
, optional_core_extensions
, extensions
, true);
777 dri2_setup_screen(disp
);
782 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
788 * Called via eglInitialize(), GLX_drv->API.Initialize().
790 * This must be guaranteed to be called exactly once, even if eglInitialize is
791 * called many times (without a eglTerminate in between).
794 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
796 EGLBoolean ret
= EGL_FALSE
;
797 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
799 /* In the case where the application calls eglMakeCurrent(context1),
800 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
801 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
802 * initialized, as we need it to be able to free context1 correctly.
804 * It would probably be safest to forcibly release the display with
805 * dri2_display_release, to make sure the display is reinitialized correctly.
806 * However, the EGL spec states that we need to keep a reference to the
807 * current context (so we cannot call dri2_make_current(NULL)), and therefore
808 * we would leak context1 as we would be missing the old display connection
809 * to free it up correctly.
812 dri2_dpy
->ref_count
++;
816 /* not until swrast_dri is supported */
817 if (disp
->Options
.UseFallback
)
820 /* Nothing to initialize for a test only display */
821 if (disp
->Options
.TestOnly
)
824 switch (disp
->Platform
) {
825 #ifdef HAVE_SURFACELESS_PLATFORM
826 case _EGL_PLATFORM_SURFACELESS
:
827 ret
= dri2_initialize_surfaceless(drv
, disp
);
830 #ifdef HAVE_X11_PLATFORM
831 case _EGL_PLATFORM_X11
:
832 ret
= dri2_initialize_x11(drv
, disp
);
835 #ifdef HAVE_DRM_PLATFORM
836 case _EGL_PLATFORM_DRM
:
837 ret
= dri2_initialize_drm(drv
, disp
);
840 #ifdef HAVE_WAYLAND_PLATFORM
841 case _EGL_PLATFORM_WAYLAND
:
842 ret
= dri2_initialize_wayland(drv
, disp
);
845 #ifdef HAVE_ANDROID_PLATFORM
846 case _EGL_PLATFORM_ANDROID
:
847 ret
= dri2_initialize_android(drv
, disp
);
851 _eglLog(_EGL_WARNING
, "No EGL platform enabled.");
856 dri2_dpy
= dri2_egl_display(disp
);
862 dri2_dpy
->ref_count
++;
869 * Decrement display reference count, and free up display if necessary.
872 dri2_display_release(_EGLDisplay
*disp
)
874 struct dri2_egl_display
*dri2_dpy
;
880 dri2_dpy
= dri2_egl_display(disp
);
882 assert(dri2_dpy
->ref_count
> 0);
883 dri2_dpy
->ref_count
--;
885 if (dri2_dpy
->ref_count
> 0)
888 _eglCleanupDisplay(disp
);
890 if (dri2_dpy
->own_dri_screen
)
891 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
892 if (dri2_dpy
->fd
>= 0)
894 if (dri2_dpy
->driver
)
895 dlclose(dri2_dpy
->driver
);
896 free(dri2_dpy
->driver_name
);
898 #ifdef HAVE_WAYLAND_PLATFORM
899 free(dri2_dpy
->device_name
);
902 switch (disp
->Platform
) {
903 #ifdef HAVE_X11_PLATFORM
904 case _EGL_PLATFORM_X11
:
905 if (dri2_dpy
->own_device
) {
906 xcb_disconnect(dri2_dpy
->conn
);
910 #ifdef HAVE_DRM_PLATFORM
911 case _EGL_PLATFORM_DRM
:
912 if (dri2_dpy
->own_device
) {
913 gbm_device_destroy(&dri2_dpy
->gbm_dri
->base
.base
);
917 #ifdef HAVE_WAYLAND_PLATFORM
918 case _EGL_PLATFORM_WAYLAND
:
919 if (dri2_dpy
->wl_drm
)
920 wl_drm_destroy(dri2_dpy
->wl_drm
);
921 if (dri2_dpy
->wl_shm
)
922 wl_shm_destroy(dri2_dpy
->wl_shm
);
923 wl_registry_destroy(dri2_dpy
->wl_registry
);
924 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
925 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
926 if (dri2_dpy
->own_device
) {
927 wl_display_disconnect(dri2_dpy
->wl_dpy
);
935 /* The drm platform does not create the screen/driver_configs but reuses
936 * the ones from the gbm device. As such the gbm itself is responsible
939 if (disp
->Platform
!= _EGL_PLATFORM_DRM
) {
940 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
941 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
942 free(dri2_dpy
->driver_configs
);
945 disp
->DriverData
= NULL
;
949 * Called via eglTerminate(), drv->API.Terminate().
951 * This must be guaranteed to be called exactly once, even if eglTerminate is
952 * called many times (without a eglInitialize in between).
955 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
957 /* Release all non-current Context/Surfaces. */
958 _eglReleaseDisplayResources(drv
, disp
);
960 dri2_display_release(disp
);
966 * Set the error code after a call to
967 * dri2_egl_display::dri2::createContextAttribs.
970 dri2_create_context_attribs_error(int dri_error
)
975 case __DRI_CTX_ERROR_SUCCESS
:
978 case __DRI_CTX_ERROR_NO_MEMORY
:
979 egl_error
= EGL_BAD_ALLOC
;
982 /* From the EGL_KHR_create_context spec, section "Errors":
984 * * If <config> does not support a client API context compatible
985 * with the requested API major and minor version, [...] context flags,
986 * and context reset notification behavior (for client API types where
987 * these attributes are supported), then an EGL_BAD_MATCH error is
990 * * If an OpenGL ES context is requested and the values for
991 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
992 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
993 * is not defined, than an EGL_BAD_MATCH error is generated.
995 * * If an OpenGL context is requested, the requested version is
996 * greater than 3.2, and the value for attribute
997 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
998 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
999 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1000 * one of these bits set; or if the implementation does not support
1001 * the requested profile, then an EGL_BAD_MATCH error is generated.
1003 case __DRI_CTX_ERROR_BAD_API
:
1004 case __DRI_CTX_ERROR_BAD_VERSION
:
1005 case __DRI_CTX_ERROR_BAD_FLAG
:
1006 egl_error
= EGL_BAD_MATCH
;
1009 /* From the EGL_KHR_create_context spec, section "Errors":
1011 * * If an attribute name or attribute value in <attrib_list> is not
1012 * recognized (including unrecognized bits in bitmask attributes),
1013 * then an EGL_BAD_ATTRIBUTE error is generated."
1015 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
1016 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
1017 egl_error
= EGL_BAD_ATTRIBUTE
;
1022 egl_error
= EGL_BAD_MATCH
;
1026 _eglError(egl_error
, "dri2_create_context");
1030 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1031 struct dri2_egl_display
*dri2_dpy
,
1032 uint32_t *ctx_attribs
,
1033 unsigned *num_attribs
)
1037 assert(*num_attribs
>= 8);
1039 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1040 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1041 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1042 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1044 if (dri2_ctx
->base
.Flags
!= 0) {
1045 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1046 * extension, don't even try to send it the robust-access flag.
1047 * It may explode. Instead, generate the required EGL error here.
1049 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1050 && !dri2_dpy
->robustness
) {
1051 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1055 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1056 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
;
1059 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1060 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1061 * extension, don't even try to send it a reset strategy. It may
1062 * explode. Instead, generate the required EGL error here.
1064 if (!dri2_dpy
->robustness
) {
1065 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1069 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1070 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1079 * Called via eglCreateContext(), drv->API.CreateContext().
1081 static _EGLContext
*
1082 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1083 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1085 struct dri2_egl_context
*dri2_ctx
;
1086 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1087 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1088 __DRIcontext
*shared
=
1089 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1090 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1091 const __DRIconfig
*dri_config
;
1096 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1098 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1102 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1105 /* The EGL_EXT_create_context_robustness spec says:
1107 * "Add to the eglCreateContext context creation errors: [...]
1109 * * If the reset notification behavior of <share_context> and the
1110 * newly created context are different then an EGL_BAD_MATCH error is
1113 if (share_list
&& share_list
->ResetNotificationStrategy
!=
1114 dri2_ctx
->base
.ResetNotificationStrategy
) {
1115 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1119 switch (dri2_ctx
->base
.ClientAPI
) {
1120 case EGL_OPENGL_ES_API
:
1121 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1123 api
= __DRI_API_GLES
;
1126 api
= __DRI_API_GLES2
;
1129 api
= __DRI_API_GLES3
;
1132 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1137 case EGL_OPENGL_API
:
1138 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1139 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1140 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1141 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1142 api
= __DRI_API_OPENGL_CORE
;
1144 api
= __DRI_API_OPENGL
;
1147 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1153 /* The config chosen here isn't necessarily
1154 * used for surfaces later.
1155 * A pixmap surface will use the single config.
1156 * This opportunity depends on disabling the
1157 * doubleBufferMode check in
1158 * src/mesa/main/context.c:check_compatible()
1160 if (dri2_config
->dri_double_config
[0])
1161 dri_config
= dri2_config
->dri_double_config
[0];
1163 dri_config
= dri2_config
->dri_single_config
[0];
1165 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
1166 * makes sure the back buffer will always be used.
1168 if (conf
->SurfaceType
& EGL_WINDOW_BIT
)
1169 dri2_ctx
->base
.WindowRenderBuffer
= EGL_BACK_BUFFER
;
1174 if (dri2_dpy
->image_driver
) {
1176 unsigned num_attribs
= 8;
1177 uint32_t ctx_attribs
[8];
1179 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1183 dri2_ctx
->dri_context
=
1184 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1192 dri2_create_context_attribs_error(error
);
1193 } else if (dri2_dpy
->dri2
) {
1194 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1196 unsigned num_attribs
= 8;
1197 uint32_t ctx_attribs
[8];
1199 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1203 dri2_ctx
->dri_context
=
1204 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1212 dri2_create_context_attribs_error(error
);
1214 dri2_ctx
->dri_context
=
1215 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1222 assert(dri2_dpy
->swrast
);
1223 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1225 unsigned num_attribs
= 8;
1226 uint32_t ctx_attribs
[8];
1228 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1232 dri2_ctx
->dri_context
=
1233 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1241 dri2_create_context_attribs_error(error
);
1243 dri2_ctx
->dri_context
=
1244 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1252 if (!dri2_ctx
->dri_context
)
1255 return &dri2_ctx
->base
;
1263 * Called via eglDestroyContext(), drv->API.DestroyContext().
1266 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1268 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1269 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1271 if (_eglPutContext(ctx
)) {
1272 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1280 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1282 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1284 if (!_eglPutSurface(surf
))
1287 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1291 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1294 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1295 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1297 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1298 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1299 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1300 _EGLContext
*old_ctx
;
1301 _EGLSurface
*old_dsurf
, *old_rsurf
;
1302 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1303 __DRIdrawable
*ddraw
, *rdraw
;
1308 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1310 /* make new bindings */
1311 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1312 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1316 /* flush before context switch */
1318 dri2_drv
->glFlush();
1320 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1321 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1322 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1325 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1326 dri2_dpy
->core
->unbindContext(old_cctx
);
1329 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1331 if (unbind
|| dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1332 dri2_destroy_surface(drv
, disp
, old_dsurf
);
1333 dri2_destroy_surface(drv
, disp
, old_rsurf
);
1336 dri2_dpy
->ref_count
++;
1338 EGLDisplay old_disp
= _eglGetDisplayHandle(old_ctx
->Resource
.Display
);
1339 dri2_destroy_context(drv
, disp
, old_ctx
);
1340 dri2_display_release(old_disp
);
1345 /* undo the previous _eglBindContext */
1346 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1347 assert(&dri2_ctx
->base
== ctx
&&
1348 tmp_dsurf
== dsurf
&&
1349 tmp_rsurf
== rsurf
);
1351 _eglPutSurface(dsurf
);
1352 _eglPutSurface(rsurf
);
1353 _eglPutContext(ctx
);
1355 _eglPutSurface(old_dsurf
);
1356 _eglPutSurface(old_rsurf
);
1357 _eglPutContext(old_ctx
);
1359 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1360 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1363 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1368 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1370 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1372 return dri2_surf
->dri_drawable
;
1376 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1379 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1381 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1383 return dri2_drv
->get_proc_address(procname
);
1387 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1388 _EGLConfig
*conf
, void *native_window
,
1389 const EGLint
*attrib_list
)
1391 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1392 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1397 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1398 _EGLConfig
*conf
, void *native_pixmap
,
1399 const EGLint
*attrib_list
)
1401 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1402 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1407 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1408 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1410 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1411 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
1415 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1418 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1419 return dri2_dpy
->vtbl
->swap_interval(drv
, dpy
, surf
, interval
);
1423 * Asks the client API to flush any rendering to the drawable so that we can
1424 * do our swapbuffers.
1427 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1429 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1430 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1432 if (dri2_dpy
->flush
) {
1433 if (dri2_dpy
->flush
->base
.version
>= 4) {
1434 /* We know there's a current context because:
1436 * "If surface is not bound to the calling thread’s current
1437 * context, an EGL_BAD_SURFACE error is generated."
1439 _EGLContext
*ctx
= _eglGetCurrentContext();
1440 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1442 /* From the EGL 1.4 spec (page 52):
1444 * "The contents of ancillary buffers are always undefined
1445 * after calling eglSwapBuffers."
1447 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1449 __DRI2_FLUSH_DRAWABLE
|
1450 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1451 __DRI2_THROTTLE_SWAPBUFFER
);
1453 dri2_dpy
->flush
->flush(dri_drawable
);
1459 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1461 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1462 return dri2_dpy
->vtbl
->swap_buffers(drv
, dpy
, surf
);
1466 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1468 const EGLint
*rects
, EGLint n_rects
)
1470 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1471 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, dpy
, surf
,
1476 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1477 EGLint numRects
, const EGLint
*rects
)
1479 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1480 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, dpy
, surf
, numRects
, rects
);
1484 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1485 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1487 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1488 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, dpy
, surf
, x
, y
, width
, height
);
1492 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1493 void *native_pixmap_target
)
1495 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1496 return dri2_dpy
->vtbl
->copy_buffers(drv
, dpy
, surf
, native_pixmap_target
);
1500 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1502 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1503 return dri2_dpy
->vtbl
->query_buffer_age(drv
, dpy
, surf
);
1507 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1509 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1510 _EGLSurface
*surf
= ctx
->DrawSurface
;
1511 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1515 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1516 * we need to copy fake to real here.*/
1518 if (dri2_dpy
->flush
!= NULL
)
1519 dri2_dpy
->flush
->flush(dri_drawable
);
1525 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1530 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1531 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1538 dri2_bind_tex_image(_EGLDriver
*drv
,
1539 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1541 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1542 struct dri2_egl_context
*dri2_ctx
;
1544 GLint format
, target
;
1545 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1547 ctx
= _eglGetCurrentContext();
1548 dri2_ctx
= dri2_egl_context(ctx
);
1550 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1553 switch (surf
->TextureFormat
) {
1554 case EGL_TEXTURE_RGB
:
1555 format
= __DRI_TEXTURE_FORMAT_RGB
;
1557 case EGL_TEXTURE_RGBA
:
1558 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1561 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1562 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1565 switch (surf
->TextureTarget
) {
1566 case EGL_TEXTURE_2D
:
1567 target
= GL_TEXTURE_2D
;
1570 target
= GL_TEXTURE_2D
;
1571 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1574 dri2_dpy
->tex_buffer
->setTexBuffer2(dri2_ctx
->dri_context
,
1582 dri2_release_tex_image(_EGLDriver
*drv
,
1583 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1585 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1586 struct dri2_egl_context
*dri2_ctx
;
1589 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1591 ctx
= _eglGetCurrentContext();
1592 dri2_ctx
= dri2_egl_context(ctx
);
1594 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1597 switch (surf
->TextureTarget
) {
1598 case EGL_TEXTURE_2D
:
1599 target
= GL_TEXTURE_2D
;
1605 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1606 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1607 dri2_dpy
->tex_buffer
->releaseTexBuffer(dri2_ctx
->dri_context
,
1608 target
, dri_drawable
);
1615 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
1616 EGLenum target
, EGLClientBuffer buffer
,
1617 const EGLint
*attr_list
)
1619 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1620 return dri2_dpy
->vtbl
->create_image(drv
, dpy
, ctx
, target
, buffer
,
1625 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1627 struct dri2_egl_image
*dri2_img
;
1629 if (dri_image
== NULL
) {
1630 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1634 dri2_img
= malloc(sizeof *dri2_img
);
1636 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1640 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1645 dri2_img
->dri_image
= dri_image
;
1647 return &dri2_img
->base
;
1651 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1652 EGLClientBuffer buffer
,
1653 const EGLint
*attr_list
)
1655 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1656 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1657 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1658 __DRIimage
*dri_image
;
1660 if (renderbuffer
== 0) {
1661 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1662 return EGL_NO_IMAGE_KHR
;
1666 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1667 renderbuffer
, NULL
);
1669 return dri2_create_image_from_dri(disp
, dri_image
);
1672 #ifdef HAVE_WAYLAND_PLATFORM
1674 /* This structure describes how a wl_buffer maps to one or more
1675 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1676 * offsets and strides of the planes in the buffer. This table maps a
1677 * wl_drm format code to a description of the planes in the buffer
1678 * that lets us create a __DRIimage for each of the planes. */
1680 static const struct wl_drm_components_descriptor
{
1681 uint32_t dri_components
;
1684 } wl_drm_components
[] = {
1685 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1686 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1687 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1688 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1689 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1693 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1694 EGLClientBuffer _buffer
,
1695 const EGLint
*attr_list
)
1697 struct wl_drm_buffer
*buffer
;
1698 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1699 const struct wl_drm_components_descriptor
*f
;
1700 __DRIimage
*dri_image
;
1701 _EGLImageAttribs attrs
;
1705 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1706 (struct wl_resource
*) _buffer
);
1710 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1711 plane
= attrs
.PlaneWL
;
1712 if (err
!= EGL_SUCCESS
) {
1713 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1717 f
= buffer
->driver_format
;
1718 if (plane
< 0 || plane
>= f
->nplanes
) {
1719 _eglError(EGL_BAD_PARAMETER
,
1720 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1724 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1726 if (dri_image
== NULL
) {
1727 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1731 return dri2_create_image_from_dri(disp
, dri_image
);
1736 dri2_get_sync_values_chromium(_EGLDisplay
*dpy
, _EGLSurface
*surf
,
1737 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1740 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1741 return dri2_dpy
->vtbl
->get_sync_values(dpy
, surf
, ust
, msc
, sbc
);
1745 * Set the error code after a call to
1746 * dri2_egl_image::dri_image::createImageFromTexture.
1749 dri2_create_image_khr_texture_error(int dri_error
)
1753 switch (dri_error
) {
1754 case __DRI_IMAGE_ERROR_SUCCESS
:
1757 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1758 egl_error
= EGL_BAD_ALLOC
;
1761 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1762 egl_error
= EGL_BAD_MATCH
;
1765 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1766 egl_error
= EGL_BAD_PARAMETER
;
1769 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
1770 egl_error
= EGL_BAD_ACCESS
;
1775 egl_error
= EGL_BAD_MATCH
;
1779 _eglError(egl_error
, "dri2_create_image_khr_texture");
1783 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1785 EGLClientBuffer buffer
,
1786 const EGLint
*attr_list
)
1788 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1789 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1790 struct dri2_egl_image
*dri2_img
;
1791 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
1792 _EGLImageAttribs attrs
;
1798 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1799 return EGL_NO_IMAGE_KHR
;
1802 if (_eglParseImageAttribList(&attrs
, disp
, attr_list
) != EGL_SUCCESS
)
1803 return EGL_NO_IMAGE_KHR
;
1806 case EGL_GL_TEXTURE_2D_KHR
:
1808 gl_target
= GL_TEXTURE_2D
;
1810 case EGL_GL_TEXTURE_3D_KHR
:
1811 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1812 depth
= attrs
.GLTextureZOffset
;
1813 gl_target
= GL_TEXTURE_3D
;
1817 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1818 return EGL_NO_IMAGE_KHR
;
1820 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1821 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1822 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1823 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1824 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1825 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1826 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
1827 gl_target
= GL_TEXTURE_CUBE_MAP
;
1830 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1831 return EGL_NO_IMAGE_KHR
;
1834 dri2_img
= malloc(sizeof *dri2_img
);
1836 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1837 return EGL_NO_IMAGE_KHR
;
1840 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1841 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1843 return EGL_NO_IMAGE_KHR
;
1846 dri2_img
->dri_image
=
1847 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
1851 attrs
.GLTextureLevel
,
1854 dri2_create_image_khr_texture_error(error
);
1856 if (!dri2_img
->dri_image
) {
1858 return EGL_NO_IMAGE_KHR
;
1860 return &dri2_img
->base
;
1864 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1865 EGLint attribute
, EGLint
*value
)
1867 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1868 if (!dri2_dpy
->vtbl
->query_surface
)
1869 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
1870 return dri2_dpy
->vtbl
->query_surface(drv
, dpy
, surf
, attribute
, value
);
1873 static struct wl_buffer
*
1874 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1877 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1878 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, dpy
, img
);
1883 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1884 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1886 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1887 EGLint format
, name
, pitch
, err
;
1888 _EGLImageAttribs attrs
;
1889 __DRIimage
*dri_image
;
1891 name
= (EGLint
) (uintptr_t) buffer
;
1893 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1894 if (err
!= EGL_SUCCESS
)
1897 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
1898 attrs
.DRMBufferStrideMESA
<= 0) {
1899 _eglError(EGL_BAD_PARAMETER
,
1900 "bad width, height or stride");
1904 switch (attrs
.DRMBufferFormatMESA
) {
1905 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1906 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1907 pitch
= attrs
.DRMBufferStrideMESA
;
1910 _eglError(EGL_BAD_PARAMETER
,
1911 "dri2_create_image_khr: unsupported pixmap depth");
1916 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1924 return dri2_create_image_from_dri(disp
, dri_image
);
1928 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
1935 * "Required attributes and their values are as follows:
1937 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1939 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1940 * by drm_fourcc.h and used as the pixel_format parameter of the
1941 * drm_mode_fb_cmd2 ioctl."
1945 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1946 * incomplete, EGL_BAD_PARAMETER is generated."
1948 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
1949 !attrs
->DMABufFourCC
.IsPresent
) {
1950 _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
1957 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1958 * specified for a plane's pitch or offset isn't supported by EGL,
1959 * EGL_BAD_ACCESS is generated."
1961 for (i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
1962 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
1963 attrs
->DMABufPlanePitches
[i
].Value
<= 0) {
1964 _eglError(EGL_BAD_ACCESS
, "invalid pitch");
1972 /* Returns the total number of file descriptors. Zero indicates an error. */
1974 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
1976 unsigned i
, plane_n
;
1978 switch (attrs
->DMABufFourCC
.Value
) {
1980 case DRM_FORMAT_RG88
:
1981 case DRM_FORMAT_GR88
:
1982 case DRM_FORMAT_R16
:
1983 case DRM_FORMAT_GR1616
:
1984 case DRM_FORMAT_RGB332
:
1985 case DRM_FORMAT_BGR233
:
1986 case DRM_FORMAT_XRGB4444
:
1987 case DRM_FORMAT_XBGR4444
:
1988 case DRM_FORMAT_RGBX4444
:
1989 case DRM_FORMAT_BGRX4444
:
1990 case DRM_FORMAT_ARGB4444
:
1991 case DRM_FORMAT_ABGR4444
:
1992 case DRM_FORMAT_RGBA4444
:
1993 case DRM_FORMAT_BGRA4444
:
1994 case DRM_FORMAT_XRGB1555
:
1995 case DRM_FORMAT_XBGR1555
:
1996 case DRM_FORMAT_RGBX5551
:
1997 case DRM_FORMAT_BGRX5551
:
1998 case DRM_FORMAT_ARGB1555
:
1999 case DRM_FORMAT_ABGR1555
:
2000 case DRM_FORMAT_RGBA5551
:
2001 case DRM_FORMAT_BGRA5551
:
2002 case DRM_FORMAT_RGB565
:
2003 case DRM_FORMAT_BGR565
:
2004 case DRM_FORMAT_RGB888
:
2005 case DRM_FORMAT_BGR888
:
2006 case DRM_FORMAT_XRGB8888
:
2007 case DRM_FORMAT_XBGR8888
:
2008 case DRM_FORMAT_RGBX8888
:
2009 case DRM_FORMAT_BGRX8888
:
2010 case DRM_FORMAT_ARGB8888
:
2011 case DRM_FORMAT_ABGR8888
:
2012 case DRM_FORMAT_RGBA8888
:
2013 case DRM_FORMAT_BGRA8888
:
2014 case DRM_FORMAT_XRGB2101010
:
2015 case DRM_FORMAT_XBGR2101010
:
2016 case DRM_FORMAT_RGBX1010102
:
2017 case DRM_FORMAT_BGRX1010102
:
2018 case DRM_FORMAT_ARGB2101010
:
2019 case DRM_FORMAT_ABGR2101010
:
2020 case DRM_FORMAT_RGBA1010102
:
2021 case DRM_FORMAT_BGRA1010102
:
2022 case DRM_FORMAT_YUYV
:
2023 case DRM_FORMAT_YVYU
:
2024 case DRM_FORMAT_UYVY
:
2025 case DRM_FORMAT_VYUY
:
2028 case DRM_FORMAT_NV12
:
2029 case DRM_FORMAT_NV21
:
2030 case DRM_FORMAT_NV16
:
2031 case DRM_FORMAT_NV61
:
2034 case DRM_FORMAT_YUV410
:
2035 case DRM_FORMAT_YVU410
:
2036 case DRM_FORMAT_YUV411
:
2037 case DRM_FORMAT_YVU411
:
2038 case DRM_FORMAT_YUV420
:
2039 case DRM_FORMAT_YVU420
:
2040 case DRM_FORMAT_YUV422
:
2041 case DRM_FORMAT_YVU422
:
2042 case DRM_FORMAT_YUV444
:
2043 case DRM_FORMAT_YVU444
:
2047 _eglError(EGL_BAD_ATTRIBUTE
, "invalid format");
2054 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2055 * incomplete, EGL_BAD_PARAMETER is generated."
2057 for (i
= 0; i
< plane_n
; ++i
) {
2058 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2059 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2060 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2061 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2067 * The spec also says:
2069 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2070 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2071 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2072 * attributes are specified."
2074 for (i
= plane_n
; i
< 3; ++i
) {
2075 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2076 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2077 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2078 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2089 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2090 * EGL will take a reference to the dma_buf(s) which it will release at any
2091 * time while the EGLDisplay is initialized. It is the responsibility of the
2092 * application to close the dma_buf file descriptors."
2094 * Therefore we must never close or otherwise modify the file descriptors.
2097 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2098 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2100 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2103 _EGLImageAttribs attrs
;
2104 __DRIimage
*dri_image
;
2115 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2116 * error EGL_BAD_PARAMETER is generated."
2118 if (buffer
!= NULL
) {
2119 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2123 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2124 if (err
!= EGL_SUCCESS
) {
2125 _eglError(err
, "bad attribute");
2129 if (!dri2_check_dma_buf_attribs(&attrs
))
2132 num_fds
= dri2_check_dma_buf_format(&attrs
);
2136 for (i
= 0; i
< num_fds
; ++i
) {
2137 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2138 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2139 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2143 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2144 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2145 fds
, num_fds
, pitches
, offsets
,
2146 attrs
.DMABufYuvColorSpaceHint
.Value
,
2147 attrs
.DMABufSampleRangeHint
.Value
,
2148 attrs
.DMABufChromaHorizontalSiting
.Value
,
2149 attrs
.DMABufChromaVerticalSiting
.Value
,
2152 dri2_create_image_khr_texture_error(error
);
2155 return EGL_NO_IMAGE_KHR
;
2157 res
= dri2_create_image_from_dri(disp
, dri_image
);
2162 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2163 const EGLint
*attr_list
)
2165 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2166 struct dri2_egl_image
*dri2_img
;
2167 _EGLImageAttribs attrs
;
2168 unsigned int dri_use
, valid_mask
;
2170 EGLint err
= EGL_SUCCESS
;
2174 dri2_img
= malloc(sizeof *dri2_img
);
2176 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2177 return EGL_NO_IMAGE_KHR
;
2181 err
= EGL_BAD_PARAMETER
;
2185 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
2186 err
= EGL_BAD_PARAMETER
;
2190 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2191 if (err
!= EGL_SUCCESS
)
2194 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2195 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)",
2196 attrs
.Width
, attrs
.Height
);
2200 switch (attrs
.DRMBufferFormatMESA
) {
2201 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2202 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2205 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x",
2206 attrs
.DRMBufferFormatMESA
);
2211 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2212 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2213 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2214 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2215 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x",
2216 attrs
.DRMBufferUseMESA
& ~valid_mask
);
2221 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2222 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2223 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2224 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2225 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2226 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2228 dri2_img
->dri_image
=
2229 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2230 attrs
.Width
, attrs
.Height
,
2231 format
, dri_use
, dri2_img
);
2232 if (dri2_img
->dri_image
== NULL
) {
2233 err
= EGL_BAD_ALLOC
;
2237 return &dri2_img
->base
;
2241 _eglError(err
, "dri2_create_drm_image_mesa");
2243 return EGL_NO_IMAGE_KHR
;
2247 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2248 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2250 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2251 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2255 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2256 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
2257 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2262 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2263 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2266 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2267 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2273 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2275 EGLint
*fourcc
, EGLint
*nplanes
,
2276 EGLuint64KHR
*modifiers
)
2278 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2279 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2285 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2286 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2288 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2289 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2298 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2299 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2301 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2302 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2306 /* rework later to provide multiple fds/strides/offsets */
2308 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2309 __DRI_IMAGE_ATTRIB_FD
, fds
);
2312 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2313 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2317 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2318 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2320 offsets
[0] = img_offset
;
2331 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2332 _EGLContext
*ctx
, EGLenum target
,
2333 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2338 case EGL_GL_TEXTURE_2D_KHR
:
2339 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2340 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2341 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2342 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2343 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2344 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2345 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2346 case EGL_GL_TEXTURE_3D_KHR
:
2347 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
2348 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2351 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2352 return EGL_NO_IMAGE_KHR
;
2354 case EGL_GL_RENDERBUFFER_KHR
:
2355 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2357 case EGL_DRM_BUFFER_MESA
:
2358 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2359 case EGL_LINUX_DMA_BUF_EXT
:
2360 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2362 #ifdef HAVE_WAYLAND_PLATFORM
2363 case EGL_WAYLAND_BUFFER_WL
:
2364 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2367 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2368 return EGL_NO_IMAGE_KHR
;
2373 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2375 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2376 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2380 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2386 #ifdef HAVE_WAYLAND_PLATFORM
2389 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2390 struct wl_drm_buffer
*buffer
)
2392 _EGLDisplay
*disp
= user_data
;
2393 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2395 int i
, dri_components
= 0;
2398 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2407 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2419 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2421 buffer
->driver_format
= NULL
;
2422 for (i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2423 if (wl_drm_components
[i
].dri_components
== dri_components
)
2424 buffer
->driver_format
= &wl_drm_components
[i
];
2426 if (buffer
->driver_format
== NULL
)
2427 dri2_dpy
->image
->destroyImage(img
);
2429 buffer
->driver_buffer
= img
;
2433 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2435 _EGLDisplay
*disp
= user_data
;
2436 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2438 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2441 static struct wayland_drm_callbacks wl_drm_callbacks
= {
2442 .authenticate
= NULL
,
2443 .reference_buffer
= dri2_wl_reference_buffer
,
2444 .release_buffer
= dri2_wl_release_buffer
2448 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2449 struct wl_display
*wl_dpy
)
2451 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2457 if (dri2_dpy
->wl_server_drm
)
2460 wl_drm_callbacks
.authenticate
=
2461 (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
;
2463 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2464 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2465 dri2_dpy
->image
->base
.version
>= 7 &&
2466 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2467 flags
|= WAYLAND_DRM_PRIME
;
2469 dri2_dpy
->wl_server_drm
=
2470 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2471 &wl_drm_callbacks
, disp
, flags
);
2473 if (!dri2_dpy
->wl_server_drm
)
2476 #ifdef HAVE_DRM_PLATFORM
2477 /* We have to share the wl_drm instance with gbm, so gbm can convert
2478 * wl_buffers to gbm bos. */
2479 if (dri2_dpy
->gbm_dri
)
2480 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2487 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2488 struct wl_display
*wl_dpy
)
2490 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2494 if (!dri2_dpy
->wl_server_drm
)
2497 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2498 dri2_dpy
->wl_server_drm
= NULL
;
2504 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2505 struct wl_resource
*buffer_resource
,
2506 EGLint attribute
, EGLint
*value
)
2508 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2509 struct wl_drm_buffer
*buffer
;
2510 const struct wl_drm_components_descriptor
*format
;
2512 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2516 format
= buffer
->driver_format
;
2517 switch (attribute
) {
2518 case EGL_TEXTURE_FORMAT
:
2519 *value
= format
->components
;
2522 *value
= buffer
->width
;
2525 *value
= buffer
->height
;
2534 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2536 p_atomic_inc(&sync
->refcount
);
2540 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2541 struct dri2_egl_sync
*dri2_sync
)
2543 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2544 switch (dri2_sync
->base
.Type
) {
2545 case EGL_SYNC_REUSABLE_KHR
:
2546 cnd_destroy(&dri2_sync
->cond
);
2548 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2549 if (dri2_sync
->base
.SyncFd
!= EGL_NO_NATIVE_FENCE_FD_ANDROID
)
2550 close(dri2_sync
->base
.SyncFd
);
2556 if (dri2_sync
->fence
)
2557 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2564 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
2565 EGLenum type
, const EGLAttrib
*attrib_list
)
2567 _EGLContext
*ctx
= _eglGetCurrentContext();
2568 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2569 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2570 struct dri2_egl_sync
*dri2_sync
;
2572 pthread_condattr_t attr
;
2574 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2576 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2580 if (!_eglInitSync(&dri2_sync
->base
, dpy
, type
, attrib_list
)) {
2586 case EGL_SYNC_FENCE_KHR
:
2587 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2588 if (!dri2_sync
->fence
) {
2589 /* Why did it fail? DRI doesn't return an error code, so we emit
2590 * a generic EGL error that doesn't communicate user error.
2592 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2598 case EGL_SYNC_CL_EVENT_KHR
:
2599 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2600 dri2_dpy
->dri_screen
,
2601 dri2_sync
->base
.CLEvent
);
2602 /* this can only happen if the cl_event passed in is invalid. */
2603 if (!dri2_sync
->fence
) {
2604 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2609 /* the initial status must be "signaled" if the cl_event is signaled */
2610 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2611 dri2_sync
->fence
, 0, 0))
2612 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2615 case EGL_SYNC_REUSABLE_KHR
:
2616 /* intialize attr */
2617 ret
= pthread_condattr_init(&attr
);
2620 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2625 /* change clock attribute to CLOCK_MONOTONIC */
2626 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
2629 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2634 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
2637 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2642 /* initial status of reusable sync must be "unsignaled" */
2643 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
2646 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2647 if (dri2_dpy
->fence
->create_fence_fd
) {
2648 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence_fd(
2649 dri2_ctx
->dri_context
,
2650 dri2_sync
->base
.SyncFd
);
2652 if (!dri2_sync
->fence
) {
2653 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2660 p_atomic_set(&dri2_sync
->refcount
, 1);
2661 return &dri2_sync
->base
;
2665 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2667 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2668 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2669 EGLint ret
= EGL_TRUE
;
2672 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
2673 * then unlock all threads possibly blocked by the reusable sync before
2676 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
2677 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2678 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2679 /* unblock all threads currently blocked by sync */
2680 err
= cnd_broadcast(&dri2_sync
->cond
);
2683 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
2688 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2694 dri2_dup_native_fence_fd(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2696 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2697 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2699 assert(sync
->Type
== EGL_SYNC_NATIVE_FENCE_ANDROID
);
2701 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
2702 /* try to retrieve the actual native fence fd.. if rendering is
2703 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
2705 sync
->SyncFd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
2709 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
2710 /* if native fence fd still not created, return an error: */
2711 _eglError(EGL_BAD_PARAMETER
, "eglDupNativeFenceFDANDROID");
2712 return EGL_NO_NATIVE_FENCE_FD_ANDROID
;
2715 return dup(sync
->SyncFd
);
2719 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2720 EGLint flags
, EGLTime timeout
)
2722 _EGLContext
*ctx
= _eglGetCurrentContext();
2723 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2724 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2725 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2726 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2727 unsigned wait_flags
= 0;
2729 /* timespecs for cnd_timedwait */
2730 struct timespec current
;
2733 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
2735 /* The EGL_KHR_fence_sync spec states:
2737 * "If no context is current for the bound API,
2738 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
2740 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
2741 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
2743 /* the sync object should take a reference while waiting */
2744 dri2_egl_ref_sync(dri2_sync
);
2746 switch (sync
->Type
) {
2747 case EGL_SYNC_FENCE_KHR
:
2748 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2749 case EGL_SYNC_CL_EVENT_KHR
:
2750 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
2751 dri2_sync
->fence
, wait_flags
,
2753 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2755 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2758 case EGL_SYNC_REUSABLE_KHR
:
2759 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
2760 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
2761 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
2762 dri2_drv
->glFlush();
2765 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
2766 if (timeout
== EGL_FOREVER_KHR
) {
2767 mtx_lock(&dri2_sync
->mutex
);
2768 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
2769 mtx_unlock(&dri2_sync
->mutex
);
2771 /* if reusable sync has not been yet signaled */
2772 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
2773 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
2775 /* calculating when to expire */
2776 expire
.nsec
= timeout
% 1000000000L;
2777 expire
.sec
= timeout
/ 1000000000L;
2779 expire
.nsec
+= current
.tv_nsec
;
2780 expire
.sec
+= current
.tv_sec
;
2782 /* expire.nsec now is a number between 0 and 1999999998 */
2783 if (expire
.nsec
> 999999999L) {
2785 expire
.nsec
-= 1000000000L;
2788 mtx_lock(&dri2_sync
->mutex
);
2789 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
2790 mtx_unlock(&dri2_sync
->mutex
);
2792 if (ret
== thrd_busy
) {
2793 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2794 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2796 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2804 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2810 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2813 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2816 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
) {
2817 _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
2821 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
) {
2822 _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
2826 dri2_sync
->base
.SyncStatus
= mode
;
2828 if (mode
== EGL_SIGNALED_KHR
) {
2829 ret
= cnd_broadcast(&dri2_sync
->cond
);
2831 /* fail to broadcast */
2833 _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
2842 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2844 _EGLContext
*ctx
= _eglGetCurrentContext();
2845 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2846 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2847 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2849 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
2850 dri2_sync
->fence
, 0);
2855 dri2_interop_query_device_info(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2856 struct mesa_glinterop_device_info
*out
)
2858 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2859 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2861 if (!dri2_dpy
->interop
)
2862 return MESA_GLINTEROP_UNSUPPORTED
;
2864 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
2868 dri2_interop_export_object(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2869 struct mesa_glinterop_export_in
*in
,
2870 struct mesa_glinterop_export_out
*out
)
2872 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2873 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2875 if (!dri2_dpy
->interop
)
2876 return MESA_GLINTEROP_UNSUPPORTED
;
2878 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
2882 dri2_unload(_EGLDriver
*drv
)
2884 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2886 dlclose(dri2_drv
->handle
);
2891 dri2_load(_EGLDriver
*drv
)
2893 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2894 #ifdef HAVE_ANDROID_PLATFORM
2895 const char *libname
= "libglapi.so";
2896 #elif defined(__APPLE__)
2897 const char *libname
= "libglapi.0.dylib";
2898 #elif defined(__CYGWIN__)
2899 const char *libname
= "cygglapi-0.dll";
2901 const char *libname
= "libglapi.so.0";
2905 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2906 handle
= dlopen(libname
, RTLD_LAZY
| RTLD_GLOBAL
);
2908 _eglLog(_EGL_WARNING
, "DRI2: failed to open glapi provider");
2912 dri2_drv
->get_proc_address
= (_EGLProc (*)(const char *))
2913 dlsym(handle
, "_glapi_get_proc_address");
2915 /* if glapi is not available, loading DRI drivers will fail */
2916 if (!dri2_drv
->get_proc_address
) {
2917 _eglLog(_EGL_WARNING
, "DRI2: failed to find _glapi_get_proc_address");
2921 dri2_drv
->glFlush
= (void (*)(void))
2922 dri2_drv
->get_proc_address("glFlush");
2924 /* if glFlush is not available things are horribly broken */
2925 if (!dri2_drv
->glFlush
) {
2926 _eglLog(_EGL_WARNING
, "DRI2: failed to find glFlush entry point");
2930 dri2_drv
->handle
= handle
;
2940 * This is the main entrypoint into the driver, called by libEGL.
2941 * Create a new _EGLDriver object and init its dispatch table.
2944 _eglBuiltInDriverDRI2(const char *args
)
2946 struct dri2_egl_driver
*dri2_drv
;
2950 dri2_drv
= calloc(1, sizeof *dri2_drv
);
2954 if (!dri2_load(&dri2_drv
->base
)) {
2959 _eglInitDriverFallbacks(&dri2_drv
->base
);
2960 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
2961 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
2962 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
2963 dri2_drv
->base
.API
.DestroyContext
= dri2_destroy_context
;
2964 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
2965 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
2966 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
2967 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
2968 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
2969 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
2970 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
2971 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
2972 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
2973 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
2974 dri2_drv
->base
.API
.SwapInterval
= dri2_swap_interval
;
2975 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
2976 dri2_drv
->base
.API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
2977 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
2978 dri2_drv
->base
.API
.PostSubBufferNV
= dri2_post_sub_buffer
;
2979 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
,
2980 dri2_drv
->base
.API
.QueryBufferAge
= dri2_query_buffer_age
;
2981 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image
;
2982 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
2983 dri2_drv
->base
.API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
2984 dri2_drv
->base
.API
.QuerySurface
= dri2_query_surface
;
2986 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
2987 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
2988 dri2_drv
->base
.API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
2989 dri2_drv
->base
.API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
2991 #ifdef HAVE_WAYLAND_PLATFORM
2992 dri2_drv
->base
.API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
2993 dri2_drv
->base
.API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
2994 dri2_drv
->base
.API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
2996 dri2_drv
->base
.API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
2997 dri2_drv
->base
.API
.CreateSyncKHR
= dri2_create_sync
;
2998 dri2_drv
->base
.API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
2999 dri2_drv
->base
.API
.SignalSyncKHR
= dri2_signal_sync
;
3000 dri2_drv
->base
.API
.WaitSyncKHR
= dri2_server_wait_sync
;
3001 dri2_drv
->base
.API
.DestroySyncKHR
= dri2_destroy_sync
;
3002 dri2_drv
->base
.API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
3003 dri2_drv
->base
.API
.GLInteropExportObject
= dri2_interop_export_object
;
3004 dri2_drv
->base
.API
.DupNativeFenceFDANDROID
= dri2_dup_native_fence_fd
;
3006 dri2_drv
->base
.Name
= "DRI2";
3007 dri2_drv
->base
.Unload
= dri2_unload
;
3009 return &dri2_drv
->base
;