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 */
87 dri_set_background_context(void *loaderPrivate
)
89 _EGLContext
*ctx
= _eglGetCurrentContext();
90 _EGLThreadInfo
*t
= _eglGetCurrentThread();
92 _eglBindContextToThread(ctx
, t
);
95 const __DRIbackgroundCallableExtension background_callable_extension
= {
96 .base
= { __DRI_BACKGROUND_CALLABLE
, 1 },
98 .setBackgroundContext
= dri_set_background_context
,
101 const __DRIuseInvalidateExtension use_invalidate
= {
102 .base
= { __DRI_USE_INVALIDATE
, 1 }
105 EGLint dri2_to_egl_attribute_map
[] = {
107 EGL_BUFFER_SIZE
, /* __DRI_ATTRIB_BUFFER_SIZE */
108 EGL_LEVEL
, /* __DRI_ATTRIB_LEVEL */
109 EGL_RED_SIZE
, /* __DRI_ATTRIB_RED_SIZE */
110 EGL_GREEN_SIZE
, /* __DRI_ATTRIB_GREEN_SIZE */
111 EGL_BLUE_SIZE
, /* __DRI_ATTRIB_BLUE_SIZE */
112 EGL_LUMINANCE_SIZE
, /* __DRI_ATTRIB_LUMINANCE_SIZE */
113 EGL_ALPHA_SIZE
, /* __DRI_ATTRIB_ALPHA_SIZE */
114 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
115 EGL_DEPTH_SIZE
, /* __DRI_ATTRIB_DEPTH_SIZE */
116 EGL_STENCIL_SIZE
, /* __DRI_ATTRIB_STENCIL_SIZE */
117 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
118 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
119 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
120 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
121 EGL_SAMPLE_BUFFERS
, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
122 EGL_SAMPLES
, /* __DRI_ATTRIB_SAMPLES */
123 0, /* __DRI_ATTRIB_RENDER_TYPE, */
124 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
125 0, /* __DRI_ATTRIB_CONFORMANT */
126 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
127 0, /* __DRI_ATTRIB_STEREO */
128 0, /* __DRI_ATTRIB_AUX_BUFFERS */
129 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
130 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
131 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
132 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
133 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
134 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
135 0, /* __DRI_ATTRIB_FLOAT_MODE (deprecated) */
136 0, /* __DRI_ATTRIB_RED_MASK */
137 0, /* __DRI_ATTRIB_GREEN_MASK */
138 0, /* __DRI_ATTRIB_BLUE_MASK */
139 0, /* __DRI_ATTRIB_ALPHA_MASK */
140 EGL_MAX_PBUFFER_WIDTH
, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
141 EGL_MAX_PBUFFER_HEIGHT
, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
142 EGL_MAX_PBUFFER_PIXELS
, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
143 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
144 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
145 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
146 0, /* __DRI_ATTRIB_SWAP_METHOD */
147 EGL_MAX_SWAP_INTERVAL
, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
148 EGL_MIN_SWAP_INTERVAL
, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
149 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
150 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
151 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
152 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
153 EGL_Y_INVERTED_NOK
, /* __DRI_ATTRIB_YINVERTED */
154 0, /* __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE */
158 dri2_get_dri_config(struct dri2_egl_config
*conf
, EGLint surface_type
,
161 const bool srgb
= colorspace
== EGL_GL_COLORSPACE_SRGB_KHR
;
163 return surface_type
== EGL_WINDOW_BIT
? conf
->dri_double_config
[srgb
] :
164 conf
->dri_single_config
[srgb
];
168 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
170 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
173 if (!_eglMatchConfig(conf
, criteria
))
179 struct dri2_egl_config
*
180 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
181 EGLint surface_type
, const EGLint
*attr_list
,
182 const unsigned int *rgba_masks
)
184 struct dri2_egl_config
*conf
;
185 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
187 unsigned int attrib
, value
, double_buffer
;
189 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
190 unsigned int dri_masks
[4] = { 0, 0, 0, 0 };
191 _EGLConfig
*matching_config
;
192 EGLint num_configs
= 0;
196 _eglInitConfig(&base
, disp
, id
);
200 bind_to_texture_rgb
= 0;
201 bind_to_texture_rgba
= 0;
203 while (dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
++, &attrib
, &value
)) {
205 case __DRI_ATTRIB_RENDER_TYPE
:
206 if (value
& __DRI_ATTRIB_RGBA_BIT
)
207 value
= EGL_RGB_BUFFER
;
208 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
209 value
= EGL_LUMINANCE_BUFFER
;
212 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
215 case __DRI_ATTRIB_CONFIG_CAVEAT
:
216 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
217 value
= EGL_NON_CONFORMANT_CONFIG
;
218 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
219 value
= EGL_SLOW_CONFIG
;
222 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
225 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
226 bind_to_texture_rgb
= value
;
229 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
230 bind_to_texture_rgba
= value
;
233 case __DRI_ATTRIB_DOUBLE_BUFFER
:
234 double_buffer
= value
;
237 case __DRI_ATTRIB_RED_MASK
:
238 dri_masks
[0] = value
;
241 case __DRI_ATTRIB_GREEN_MASK
:
242 dri_masks
[1] = value
;
245 case __DRI_ATTRIB_BLUE_MASK
:
246 dri_masks
[2] = value
;
249 case __DRI_ATTRIB_ALPHA_MASK
:
250 dri_masks
[3] = value
;
253 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
254 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
255 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
256 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
257 /* Don't expose visuals with the accumulation buffer. */
262 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
264 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
268 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH
:
269 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_WIDTH
,
270 _EGL_MAX_PBUFFER_WIDTH
);
272 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT
:
273 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_HEIGHT
,
274 _EGL_MAX_PBUFFER_HEIGHT
);
278 key
= dri2_to_egl_attribute_map
[attrib
];
280 _eglSetConfigKey(&base
, key
, value
);
286 for (i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
287 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
289 if (rgba_masks
&& memcmp(rgba_masks
, dri_masks
, sizeof(dri_masks
)))
292 base
.NativeRenderable
= EGL_TRUE
;
294 base
.SurfaceType
= surface_type
;
295 if (surface_type
& (EGL_PBUFFER_BIT
|
296 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
297 base
.BindToTextureRGB
= bind_to_texture_rgb
;
298 if (base
.AlphaSize
> 0)
299 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
302 base
.RenderableType
= disp
->ClientAPIs
;
303 base
.Conformant
= disp
->ClientAPIs
;
305 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
306 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
308 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
309 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
313 config_id
= base
.ConfigID
;
314 base
.ConfigID
= EGL_DONT_CARE
;
315 base
.SurfaceType
= EGL_DONT_CARE
;
316 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
317 (_EGLArrayForEach
) dri2_match_config
, &base
);
319 if (num_configs
== 1) {
320 conf
= (struct dri2_egl_config
*) matching_config
;
322 if (double_buffer
&& !conf
->dri_double_config
[srgb
])
323 conf
->dri_double_config
[srgb
] = dri_config
;
324 else if (!double_buffer
&& !conf
->dri_single_config
[srgb
])
325 conf
->dri_single_config
[srgb
] = dri_config
;
327 /* a similar config type is already added (unlikely) => discard */
330 else if (num_configs
== 0) {
331 conf
= calloc(1, sizeof *conf
);
336 conf
->dri_double_config
[srgb
] = dri_config
;
338 conf
->dri_single_config
[srgb
] = dri_config
;
340 memcpy(&conf
->base
, &base
, sizeof base
);
341 conf
->base
.SurfaceType
= 0;
342 conf
->base
.ConfigID
= config_id
;
344 _eglLinkConfig(&conf
->base
);
352 surface_type
&= ~EGL_PIXMAP_BIT
;
355 /* No support for pbuffer + MSAA for now.
357 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
358 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
361 surface_type
&= ~EGL_PBUFFER_BIT
;
364 conf
->base
.SurfaceType
|= surface_type
;
370 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
372 _EGLDisplay
*disp
= data
;
373 struct dri2_egl_image
*dri2_img
;
378 img
= _eglLookupImage(image
, disp
);
380 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
384 dri2_img
= dri2_egl_image(image
);
386 return dri2_img
->dri_image
;
389 const __DRIimageLookupExtension image_lookup_extension
= {
390 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
392 .lookupEGLImage
= dri2_lookup_egl_image
395 struct dri2_extension_match
{
401 static const struct dri2_extension_match dri3_driver_extensions
[] = {
402 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
403 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
407 static const struct dri2_extension_match dri2_driver_extensions
[] = {
408 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
409 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
413 static const struct dri2_extension_match dri2_core_extensions
[] = {
414 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
415 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
416 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
420 static const struct dri2_extension_match swrast_driver_extensions
[] = {
421 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
422 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
426 static const struct dri2_extension_match swrast_core_extensions
[] = {
427 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
431 static const struct dri2_extension_match optional_core_extensions
[] = {
432 { __DRI2_ROBUSTNESS
, 1, offsetof(struct dri2_egl_display
, robustness
) },
433 { __DRI2_CONFIG_QUERY
, 1, offsetof(struct dri2_egl_display
, config
) },
434 { __DRI2_FENCE
, 1, offsetof(struct dri2_egl_display
, fence
) },
435 { __DRI2_RENDERER_QUERY
, 1, offsetof(struct dri2_egl_display
, rendererQuery
) },
436 { __DRI2_INTEROP
, 1, offsetof(struct dri2_egl_display
, interop
) },
441 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
442 const struct dri2_extension_match
*matches
,
443 const __DRIextension
**extensions
,
446 int i
, j
, ret
= EGL_TRUE
;
449 for (i
= 0; extensions
[i
]; i
++) {
450 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
451 for (j
= 0; matches
[j
].name
; j
++) {
452 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
453 extensions
[i
]->version
>= matches
[j
].version
) {
454 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
455 *(const __DRIextension
**) field
= extensions
[i
];
456 _eglLog(_EGL_INFO
, "found extension %s version %d",
457 extensions
[i
]->name
, extensions
[i
]->version
);
463 for (j
= 0; matches
[j
].name
; j
++) {
464 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
465 if (*(const __DRIextension
**) field
== NULL
) {
467 _eglLog(_EGL_DEBUG
, "did not find optional extension %s version %d",
468 matches
[j
].name
, matches
[j
].version
);
470 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
471 matches
[j
].name
, matches
[j
].version
);
480 static const __DRIextension
**
481 dri2_open_driver(_EGLDisplay
*disp
)
483 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
484 const __DRIextension
**extensions
= NULL
;
485 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
486 char *get_extensions_name
;
487 const __DRIextension
**(*get_extensions
)(void);
490 if (geteuid() == getuid()) {
491 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
492 search_paths
= getenv("LIBGL_DRIVERS_PATH");
494 if (search_paths
== NULL
)
495 search_paths
= DEFAULT_DRIVER_DIR
;
497 dri2_dpy
->driver
= NULL
;
498 end
= search_paths
+ strlen(search_paths
);
499 for (p
= search_paths
; p
< end
; p
= next
+ 1) {
501 next
= strchr(p
, ':');
507 snprintf(path
, sizeof path
,
508 "%.*s/tls/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
509 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
511 if (dri2_dpy
->driver
== NULL
) {
512 snprintf(path
, sizeof path
,
513 "%.*s/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
514 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
515 if (dri2_dpy
->driver
== NULL
)
516 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
518 /* not need continue to loop all paths once the driver is found */
519 if (dri2_dpy
->driver
!= NULL
)
523 snprintf(path
, sizeof path
, "%.*s/gallium_dri.so", len
, p
);
524 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
525 if (dri2_dpy
->driver
== NULL
)
526 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
532 if (dri2_dpy
->driver
== NULL
) {
533 _eglLog(_EGL_WARNING
,
534 "DRI2: failed to open %s (search paths %s)",
535 dri2_dpy
->driver_name
, search_paths
);
539 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
541 get_extensions_name
= loader_get_extensions_name(dri2_dpy
->driver_name
);
542 if (get_extensions_name
) {
543 get_extensions
= dlsym(dri2_dpy
->driver
, get_extensions_name
);
544 if (get_extensions
) {
545 extensions
= get_extensions();
547 _eglLog(_EGL_DEBUG
, "driver does not expose %s(): %s\n",
548 get_extensions_name
, dlerror());
550 free(get_extensions_name
);
554 extensions
= dlsym(dri2_dpy
->driver
, __DRI_DRIVER_EXTENSIONS
);
555 if (extensions
== NULL
) {
556 _eglLog(_EGL_WARNING
,
557 "DRI2: driver exports no extensions (%s)", dlerror());
558 dlclose(dri2_dpy
->driver
);
565 dri2_load_driver_dri3(_EGLDisplay
*disp
)
567 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
568 const __DRIextension
**extensions
;
570 extensions
= dri2_open_driver(disp
);
574 if (!dri2_bind_extensions(dri2_dpy
, dri3_driver_extensions
, extensions
, false)) {
575 dlclose(dri2_dpy
->driver
);
578 dri2_dpy
->driver_extensions
= extensions
;
584 dri2_load_driver(_EGLDisplay
*disp
)
586 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
587 const __DRIextension
**extensions
;
589 extensions
= dri2_open_driver(disp
);
593 if (!dri2_bind_extensions(dri2_dpy
, dri2_driver_extensions
, extensions
, false)) {
594 dlclose(dri2_dpy
->driver
);
597 dri2_dpy
->driver_extensions
= extensions
;
603 dri2_load_driver_swrast(_EGLDisplay
*disp
)
605 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
606 const __DRIextension
**extensions
;
608 extensions
= dri2_open_driver(disp
);
612 if (!dri2_bind_extensions(dri2_dpy
, swrast_driver_extensions
, extensions
, false)) {
613 dlclose(dri2_dpy
->driver
);
616 dri2_dpy
->driver_extensions
= extensions
;
622 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
624 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
625 unsigned int value
= 0;
627 if (!rendererQuery
||
628 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
635 dri2_setup_screen(_EGLDisplay
*disp
)
637 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
638 unsigned int api_mask
;
640 if (dri2_dpy
->image_driver
) {
641 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
642 } else if (dri2_dpy
->dri2
) {
643 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
645 assert(dri2_dpy
->swrast
);
646 api_mask
= 1 << __DRI_API_OPENGL
|
647 1 << __DRI_API_GLES
|
648 1 << __DRI_API_GLES2
|
649 1 << __DRI_API_GLES3
;
652 disp
->ClientAPIs
= 0;
653 if ((api_mask
& (1 <<__DRI_API_OPENGL
)) && _eglIsApiValid(EGL_OPENGL_API
))
654 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
655 if ((api_mask
& (1 << __DRI_API_GLES
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
656 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
657 if ((api_mask
& (1 << __DRI_API_GLES2
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
658 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
659 if ((api_mask
& (1 << __DRI_API_GLES3
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
660 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
662 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
663 disp
->Extensions
.KHR_no_config_context
= EGL_TRUE
;
664 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
666 if (dri2_renderer_query_integer(dri2_dpy
,
667 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
668 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
670 if (dri2_dpy
->image_driver
||
671 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
672 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
673 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
675 if (dri2_dpy
->robustness
)
676 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
679 if (dri2_dpy
->fence
) {
680 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
681 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
682 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
683 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
684 if (dri2_dpy
->fence
->base
.version
>= 2) {
685 unsigned capabilities
=
686 dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
);
687 disp
->Extensions
.ANDROID_native_fence_sync
=
688 (capabilities
& __DRI_FENCE_CAP_NATIVE_FD
) != 0;
692 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
694 if (dri2_dpy
->image
) {
695 if (dri2_dpy
->image
->base
.version
>= 10 &&
696 dri2_dpy
->image
->getCapabilities
!= NULL
) {
699 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
700 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
702 if (dri2_dpy
->image
->base
.version
>= 11)
703 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
705 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
706 if (dri2_dpy
->image
->base
.version
>= 11)
707 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
710 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
711 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
712 if (dri2_dpy
->image
->base
.version
>= 5 &&
713 dri2_dpy
->image
->createImageFromTexture
) {
714 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
715 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
717 if (dri2_renderer_query_integer(dri2_dpy
,
718 __DRI2_RENDERER_HAS_TEXTURE_3D
))
719 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
721 if (dri2_dpy
->image
->base
.version
>= 8 &&
722 dri2_dpy
->image
->createImageFromDmaBufs
) {
723 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
729 /* All platforms but DRM call this function to create the screen, query the
730 * dri extensions, setup the vtables and populate the driver_configs.
731 * DRM inherits all that information from its display - GBM.
734 dri2_create_screen(_EGLDisplay
*disp
)
736 const __DRIextension
**extensions
;
737 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
739 if (dri2_dpy
->image_driver
) {
740 dri2_dpy
->dri_screen
=
741 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
742 dri2_dpy
->loader_extensions
,
743 dri2_dpy
->driver_extensions
,
744 &dri2_dpy
->driver_configs
,
746 } else if (dri2_dpy
->dri2
) {
747 if (dri2_dpy
->dri2
->base
.version
>= 4) {
748 dri2_dpy
->dri_screen
=
749 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
750 dri2_dpy
->loader_extensions
,
751 dri2_dpy
->driver_extensions
,
752 &dri2_dpy
->driver_configs
, disp
);
754 dri2_dpy
->dri_screen
=
755 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
756 dri2_dpy
->loader_extensions
,
757 &dri2_dpy
->driver_configs
, disp
);
760 assert(dri2_dpy
->swrast
);
761 if (dri2_dpy
->swrast
->base
.version
>= 4) {
762 dri2_dpy
->dri_screen
=
763 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->loader_extensions
,
764 dri2_dpy
->driver_extensions
,
765 &dri2_dpy
->driver_configs
, disp
);
767 dri2_dpy
->dri_screen
=
768 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->loader_extensions
,
769 &dri2_dpy
->driver_configs
, disp
);
773 if (dri2_dpy
->dri_screen
== NULL
) {
774 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
778 dri2_dpy
->own_dri_screen
= 1;
780 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
782 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
) {
783 if (!dri2_bind_extensions(dri2_dpy
, dri2_core_extensions
, extensions
, false))
784 goto cleanup_dri_screen
;
786 assert(dri2_dpy
->swrast
);
787 if (!dri2_bind_extensions(dri2_dpy
, swrast_core_extensions
, extensions
, false))
788 goto cleanup_dri_screen
;
791 dri2_bind_extensions(dri2_dpy
, optional_core_extensions
, extensions
, true);
792 dri2_setup_screen(disp
);
797 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
803 * Called via eglInitialize(), GLX_drv->API.Initialize().
805 * This must be guaranteed to be called exactly once, even if eglInitialize is
806 * called many times (without a eglTerminate in between).
809 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
811 EGLBoolean ret
= EGL_FALSE
;
812 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
814 /* In the case where the application calls eglMakeCurrent(context1),
815 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
816 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
817 * initialized, as we need it to be able to free context1 correctly.
819 * It would probably be safest to forcibly release the display with
820 * dri2_display_release, to make sure the display is reinitialized correctly.
821 * However, the EGL spec states that we need to keep a reference to the
822 * current context (so we cannot call dri2_make_current(NULL)), and therefore
823 * we would leak context1 as we would be missing the old display connection
824 * to free it up correctly.
827 dri2_dpy
->ref_count
++;
831 /* not until swrast_dri is supported */
832 if (disp
->Options
.UseFallback
)
835 /* Nothing to initialize for a test only display */
836 if (disp
->Options
.TestOnly
)
839 switch (disp
->Platform
) {
840 #ifdef HAVE_SURFACELESS_PLATFORM
841 case _EGL_PLATFORM_SURFACELESS
:
842 ret
= dri2_initialize_surfaceless(drv
, disp
);
845 #ifdef HAVE_X11_PLATFORM
846 case _EGL_PLATFORM_X11
:
847 ret
= dri2_initialize_x11(drv
, disp
);
850 #ifdef HAVE_DRM_PLATFORM
851 case _EGL_PLATFORM_DRM
:
852 ret
= dri2_initialize_drm(drv
, disp
);
855 #ifdef HAVE_WAYLAND_PLATFORM
856 case _EGL_PLATFORM_WAYLAND
:
857 ret
= dri2_initialize_wayland(drv
, disp
);
860 #ifdef HAVE_ANDROID_PLATFORM
861 case _EGL_PLATFORM_ANDROID
:
862 ret
= dri2_initialize_android(drv
, disp
);
866 _eglLog(_EGL_WARNING
, "No EGL platform enabled.");
871 dri2_dpy
= dri2_egl_display(disp
);
877 dri2_dpy
->ref_count
++;
884 * Decrement display reference count, and free up display if necessary.
887 dri2_display_release(_EGLDisplay
*disp
)
889 struct dri2_egl_display
*dri2_dpy
;
895 dri2_dpy
= dri2_egl_display(disp
);
897 assert(dri2_dpy
->ref_count
> 0);
898 dri2_dpy
->ref_count
--;
900 if (dri2_dpy
->ref_count
> 0)
903 _eglCleanupDisplay(disp
);
905 if (dri2_dpy
->own_dri_screen
)
906 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
907 if (dri2_dpy
->fd
>= 0)
909 if (dri2_dpy
->driver
)
910 dlclose(dri2_dpy
->driver
);
911 free(dri2_dpy
->driver_name
);
913 #ifdef HAVE_WAYLAND_PLATFORM
914 free(dri2_dpy
->device_name
);
917 switch (disp
->Platform
) {
918 #ifdef HAVE_X11_PLATFORM
919 case _EGL_PLATFORM_X11
:
920 if (dri2_dpy
->own_device
) {
921 xcb_disconnect(dri2_dpy
->conn
);
925 #ifdef HAVE_DRM_PLATFORM
926 case _EGL_PLATFORM_DRM
:
927 if (dri2_dpy
->own_device
) {
928 gbm_device_destroy(&dri2_dpy
->gbm_dri
->base
.base
);
932 #ifdef HAVE_WAYLAND_PLATFORM
933 case _EGL_PLATFORM_WAYLAND
:
934 if (dri2_dpy
->wl_drm
)
935 wl_drm_destroy(dri2_dpy
->wl_drm
);
936 if (dri2_dpy
->wl_shm
)
937 wl_shm_destroy(dri2_dpy
->wl_shm
);
938 wl_registry_destroy(dri2_dpy
->wl_registry
);
939 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
940 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
941 if (dri2_dpy
->own_device
) {
942 wl_display_disconnect(dri2_dpy
->wl_dpy
);
950 /* The drm platform does not create the screen/driver_configs but reuses
951 * the ones from the gbm device. As such the gbm itself is responsible
954 if (disp
->Platform
!= _EGL_PLATFORM_DRM
) {
955 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
956 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
957 free(dri2_dpy
->driver_configs
);
960 disp
->DriverData
= NULL
;
964 * Called via eglTerminate(), drv->API.Terminate().
966 * This must be guaranteed to be called exactly once, even if eglTerminate is
967 * called many times (without a eglInitialize in between).
970 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
972 /* Release all non-current Context/Surfaces. */
973 _eglReleaseDisplayResources(drv
, disp
);
975 dri2_display_release(disp
);
981 * Set the error code after a call to
982 * dri2_egl_display::dri2::createContextAttribs.
985 dri2_create_context_attribs_error(int dri_error
)
990 case __DRI_CTX_ERROR_SUCCESS
:
993 case __DRI_CTX_ERROR_NO_MEMORY
:
994 egl_error
= EGL_BAD_ALLOC
;
997 /* From the EGL_KHR_create_context spec, section "Errors":
999 * * If <config> does not support a client API context compatible
1000 * with the requested API major and minor version, [...] context flags,
1001 * and context reset notification behavior (for client API types where
1002 * these attributes are supported), then an EGL_BAD_MATCH error is
1005 * * If an OpenGL ES context is requested and the values for
1006 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
1007 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
1008 * is not defined, than an EGL_BAD_MATCH error is generated.
1010 * * If an OpenGL context is requested, the requested version is
1011 * greater than 3.2, and the value for attribute
1012 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
1013 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
1014 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
1015 * one of these bits set; or if the implementation does not support
1016 * the requested profile, then an EGL_BAD_MATCH error is generated.
1018 case __DRI_CTX_ERROR_BAD_API
:
1019 case __DRI_CTX_ERROR_BAD_VERSION
:
1020 case __DRI_CTX_ERROR_BAD_FLAG
:
1021 egl_error
= EGL_BAD_MATCH
;
1024 /* From the EGL_KHR_create_context spec, section "Errors":
1026 * * If an attribute name or attribute value in <attrib_list> is not
1027 * recognized (including unrecognized bits in bitmask attributes),
1028 * then an EGL_BAD_ATTRIBUTE error is generated."
1030 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
1031 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
1032 egl_error
= EGL_BAD_ATTRIBUTE
;
1037 egl_error
= EGL_BAD_MATCH
;
1041 _eglError(egl_error
, "dri2_create_context");
1045 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1046 struct dri2_egl_display
*dri2_dpy
,
1047 uint32_t *ctx_attribs
,
1048 unsigned *num_attribs
)
1052 assert(*num_attribs
>= 8);
1054 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1055 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1056 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1057 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1059 if (dri2_ctx
->base
.Flags
!= 0) {
1060 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1061 * extension, don't even try to send it the robust-access flag.
1062 * It may explode. Instead, generate the required EGL error here.
1064 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1065 && !dri2_dpy
->robustness
) {
1066 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1070 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1071 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
;
1074 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1075 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1076 * extension, don't even try to send it a reset strategy. It may
1077 * explode. Instead, generate the required EGL error here.
1079 if (!dri2_dpy
->robustness
) {
1080 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1084 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1085 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1094 * Called via eglCreateContext(), drv->API.CreateContext().
1096 static _EGLContext
*
1097 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1098 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1100 struct dri2_egl_context
*dri2_ctx
;
1101 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1102 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1103 __DRIcontext
*shared
=
1104 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1105 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1106 const __DRIconfig
*dri_config
;
1111 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1113 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1117 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1120 /* The EGL_EXT_create_context_robustness spec says:
1122 * "Add to the eglCreateContext context creation errors: [...]
1124 * * If the reset notification behavior of <share_context> and the
1125 * newly created context are different then an EGL_BAD_MATCH error is
1128 if (share_list
&& share_list
->ResetNotificationStrategy
!=
1129 dri2_ctx
->base
.ResetNotificationStrategy
) {
1130 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1134 switch (dri2_ctx
->base
.ClientAPI
) {
1135 case EGL_OPENGL_ES_API
:
1136 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1138 api
= __DRI_API_GLES
;
1141 api
= __DRI_API_GLES2
;
1144 api
= __DRI_API_GLES3
;
1147 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1152 case EGL_OPENGL_API
:
1153 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1154 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1155 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1156 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1157 api
= __DRI_API_OPENGL_CORE
;
1159 api
= __DRI_API_OPENGL
;
1162 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1168 /* The config chosen here isn't necessarily
1169 * used for surfaces later.
1170 * A pixmap surface will use the single config.
1171 * This opportunity depends on disabling the
1172 * doubleBufferMode check in
1173 * src/mesa/main/context.c:check_compatible()
1175 if (dri2_config
->dri_double_config
[0])
1176 dri_config
= dri2_config
->dri_double_config
[0];
1178 dri_config
= dri2_config
->dri_single_config
[0];
1180 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
1181 * makes sure the back buffer will always be used.
1183 if (conf
->SurfaceType
& EGL_WINDOW_BIT
)
1184 dri2_ctx
->base
.WindowRenderBuffer
= EGL_BACK_BUFFER
;
1189 if (dri2_dpy
->image_driver
) {
1191 unsigned num_attribs
= 8;
1192 uint32_t ctx_attribs
[8];
1194 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1198 dri2_ctx
->dri_context
=
1199 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1207 dri2_create_context_attribs_error(error
);
1208 } else if (dri2_dpy
->dri2
) {
1209 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1211 unsigned num_attribs
= 8;
1212 uint32_t ctx_attribs
[8];
1214 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1218 dri2_ctx
->dri_context
=
1219 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1227 dri2_create_context_attribs_error(error
);
1229 dri2_ctx
->dri_context
=
1230 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1237 assert(dri2_dpy
->swrast
);
1238 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1240 unsigned num_attribs
= 8;
1241 uint32_t ctx_attribs
[8];
1243 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1247 dri2_ctx
->dri_context
=
1248 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1256 dri2_create_context_attribs_error(error
);
1258 dri2_ctx
->dri_context
=
1259 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1267 if (!dri2_ctx
->dri_context
)
1270 return &dri2_ctx
->base
;
1278 * Called via eglDestroyContext(), drv->API.DestroyContext().
1281 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1283 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1284 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1286 if (_eglPutContext(ctx
)) {
1287 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1295 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1297 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1299 if (!_eglPutSurface(surf
))
1302 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1306 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1309 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1310 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1312 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1313 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1314 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1315 _EGLContext
*old_ctx
;
1316 _EGLSurface
*old_dsurf
, *old_rsurf
;
1317 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1318 __DRIdrawable
*ddraw
, *rdraw
;
1323 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1325 /* make new bindings */
1326 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1327 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1331 /* flush before context switch */
1333 dri2_drv
->glFlush();
1335 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1336 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1337 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1340 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1341 dri2_dpy
->core
->unbindContext(old_cctx
);
1344 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1346 if (unbind
|| dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1347 dri2_destroy_surface(drv
, disp
, old_dsurf
);
1348 dri2_destroy_surface(drv
, disp
, old_rsurf
);
1351 dri2_dpy
->ref_count
++;
1353 EGLDisplay old_disp
= _eglGetDisplayHandle(old_ctx
->Resource
.Display
);
1354 dri2_destroy_context(drv
, disp
, old_ctx
);
1355 dri2_display_release(old_disp
);
1360 /* undo the previous _eglBindContext */
1361 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1362 assert(&dri2_ctx
->base
== ctx
&&
1363 tmp_dsurf
== dsurf
&&
1364 tmp_rsurf
== rsurf
);
1366 _eglPutSurface(dsurf
);
1367 _eglPutSurface(rsurf
);
1368 _eglPutContext(ctx
);
1370 _eglPutSurface(old_dsurf
);
1371 _eglPutSurface(old_rsurf
);
1372 _eglPutContext(old_ctx
);
1374 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1375 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1378 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1383 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1385 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1387 return dri2_surf
->dri_drawable
;
1391 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1394 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1396 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1398 return dri2_drv
->get_proc_address(procname
);
1402 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1403 _EGLConfig
*conf
, void *native_window
,
1404 const EGLint
*attrib_list
)
1406 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1407 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1412 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1413 _EGLConfig
*conf
, void *native_pixmap
,
1414 const EGLint
*attrib_list
)
1416 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1417 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1422 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1423 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1425 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1426 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
1430 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1433 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1434 return dri2_dpy
->vtbl
->swap_interval(drv
, dpy
, surf
, interval
);
1438 * Asks the client API to flush any rendering to the drawable so that we can
1439 * do our swapbuffers.
1442 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1444 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1445 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1447 if (dri2_dpy
->flush
) {
1448 if (dri2_dpy
->flush
->base
.version
>= 4) {
1449 /* We know there's a current context because:
1451 * "If surface is not bound to the calling thread’s current
1452 * context, an EGL_BAD_SURFACE error is generated."
1454 _EGLContext
*ctx
= _eglGetCurrentContext();
1455 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1457 /* From the EGL 1.4 spec (page 52):
1459 * "The contents of ancillary buffers are always undefined
1460 * after calling eglSwapBuffers."
1462 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1464 __DRI2_FLUSH_DRAWABLE
|
1465 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1466 __DRI2_THROTTLE_SWAPBUFFER
);
1468 dri2_dpy
->flush
->flush(dri_drawable
);
1474 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1476 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1477 return dri2_dpy
->vtbl
->swap_buffers(drv
, dpy
, surf
);
1481 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1483 const EGLint
*rects
, EGLint n_rects
)
1485 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1486 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, dpy
, surf
,
1491 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1492 EGLint numRects
, const EGLint
*rects
)
1494 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1495 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, dpy
, surf
, numRects
, rects
);
1499 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1500 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1502 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1503 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, dpy
, surf
, x
, y
, width
, height
);
1507 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1508 void *native_pixmap_target
)
1510 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1511 return dri2_dpy
->vtbl
->copy_buffers(drv
, dpy
, surf
, native_pixmap_target
);
1515 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1517 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1518 return dri2_dpy
->vtbl
->query_buffer_age(drv
, dpy
, surf
);
1522 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1524 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1525 _EGLSurface
*surf
= ctx
->DrawSurface
;
1526 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1530 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1531 * we need to copy fake to real here.*/
1533 if (dri2_dpy
->flush
!= NULL
)
1534 dri2_dpy
->flush
->flush(dri_drawable
);
1540 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1545 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1546 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1553 dri2_bind_tex_image(_EGLDriver
*drv
,
1554 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1556 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1557 struct dri2_egl_context
*dri2_ctx
;
1559 GLint format
, target
;
1560 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1562 ctx
= _eglGetCurrentContext();
1563 dri2_ctx
= dri2_egl_context(ctx
);
1565 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1568 switch (surf
->TextureFormat
) {
1569 case EGL_TEXTURE_RGB
:
1570 format
= __DRI_TEXTURE_FORMAT_RGB
;
1572 case EGL_TEXTURE_RGBA
:
1573 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1576 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1577 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1580 switch (surf
->TextureTarget
) {
1581 case EGL_TEXTURE_2D
:
1582 target
= GL_TEXTURE_2D
;
1585 target
= GL_TEXTURE_2D
;
1586 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1589 dri2_dpy
->tex_buffer
->setTexBuffer2(dri2_ctx
->dri_context
,
1597 dri2_release_tex_image(_EGLDriver
*drv
,
1598 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1600 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1601 struct dri2_egl_context
*dri2_ctx
;
1604 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1606 ctx
= _eglGetCurrentContext();
1607 dri2_ctx
= dri2_egl_context(ctx
);
1609 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1612 switch (surf
->TextureTarget
) {
1613 case EGL_TEXTURE_2D
:
1614 target
= GL_TEXTURE_2D
;
1620 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1621 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1622 dri2_dpy
->tex_buffer
->releaseTexBuffer(dri2_ctx
->dri_context
,
1623 target
, dri_drawable
);
1630 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
1631 EGLenum target
, EGLClientBuffer buffer
,
1632 const EGLint
*attr_list
)
1634 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1635 return dri2_dpy
->vtbl
->create_image(drv
, dpy
, ctx
, target
, buffer
,
1640 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1642 struct dri2_egl_image
*dri2_img
;
1644 if (dri_image
== NULL
) {
1645 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1649 dri2_img
= malloc(sizeof *dri2_img
);
1651 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1655 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1660 dri2_img
->dri_image
= dri_image
;
1662 return &dri2_img
->base
;
1666 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1667 EGLClientBuffer buffer
,
1668 const EGLint
*attr_list
)
1670 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1671 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1672 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1673 __DRIimage
*dri_image
;
1675 if (renderbuffer
== 0) {
1676 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1677 return EGL_NO_IMAGE_KHR
;
1681 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1682 renderbuffer
, NULL
);
1684 return dri2_create_image_from_dri(disp
, dri_image
);
1687 #ifdef HAVE_WAYLAND_PLATFORM
1689 /* This structure describes how a wl_buffer maps to one or more
1690 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1691 * offsets and strides of the planes in the buffer. This table maps a
1692 * wl_drm format code to a description of the planes in the buffer
1693 * that lets us create a __DRIimage for each of the planes. */
1695 static const struct wl_drm_components_descriptor
{
1696 uint32_t dri_components
;
1699 } wl_drm_components
[] = {
1700 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1701 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1702 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1703 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1704 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1708 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1709 EGLClientBuffer _buffer
,
1710 const EGLint
*attr_list
)
1712 struct wl_drm_buffer
*buffer
;
1713 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1714 const struct wl_drm_components_descriptor
*f
;
1715 __DRIimage
*dri_image
;
1716 _EGLImageAttribs attrs
;
1720 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1721 (struct wl_resource
*) _buffer
);
1725 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1726 plane
= attrs
.PlaneWL
;
1727 if (err
!= EGL_SUCCESS
) {
1728 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1732 f
= buffer
->driver_format
;
1733 if (plane
< 0 || plane
>= f
->nplanes
) {
1734 _eglError(EGL_BAD_PARAMETER
,
1735 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1739 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1741 if (dri_image
== NULL
) {
1742 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1746 return dri2_create_image_from_dri(disp
, dri_image
);
1751 dri2_get_sync_values_chromium(_EGLDisplay
*dpy
, _EGLSurface
*surf
,
1752 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1755 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1756 return dri2_dpy
->vtbl
->get_sync_values(dpy
, surf
, ust
, msc
, sbc
);
1760 * Set the error code after a call to
1761 * dri2_egl_image::dri_image::createImageFromTexture.
1764 dri2_create_image_khr_texture_error(int dri_error
)
1768 switch (dri_error
) {
1769 case __DRI_IMAGE_ERROR_SUCCESS
:
1772 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1773 egl_error
= EGL_BAD_ALLOC
;
1776 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1777 egl_error
= EGL_BAD_MATCH
;
1780 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1781 egl_error
= EGL_BAD_PARAMETER
;
1784 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
1785 egl_error
= EGL_BAD_ACCESS
;
1790 egl_error
= EGL_BAD_MATCH
;
1794 _eglError(egl_error
, "dri2_create_image_khr_texture");
1798 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1800 EGLClientBuffer buffer
,
1801 const EGLint
*attr_list
)
1803 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1804 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1805 struct dri2_egl_image
*dri2_img
;
1806 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
1807 _EGLImageAttribs attrs
;
1813 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1814 return EGL_NO_IMAGE_KHR
;
1817 if (_eglParseImageAttribList(&attrs
, disp
, attr_list
) != EGL_SUCCESS
)
1818 return EGL_NO_IMAGE_KHR
;
1821 case EGL_GL_TEXTURE_2D_KHR
:
1823 gl_target
= GL_TEXTURE_2D
;
1825 case EGL_GL_TEXTURE_3D_KHR
:
1826 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1827 depth
= attrs
.GLTextureZOffset
;
1828 gl_target
= GL_TEXTURE_3D
;
1832 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1833 return EGL_NO_IMAGE_KHR
;
1835 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1836 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1837 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1838 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1839 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1840 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1841 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
1842 gl_target
= GL_TEXTURE_CUBE_MAP
;
1845 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1846 return EGL_NO_IMAGE_KHR
;
1849 dri2_img
= malloc(sizeof *dri2_img
);
1851 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1852 return EGL_NO_IMAGE_KHR
;
1855 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1856 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1858 return EGL_NO_IMAGE_KHR
;
1861 dri2_img
->dri_image
=
1862 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
1866 attrs
.GLTextureLevel
,
1869 dri2_create_image_khr_texture_error(error
);
1871 if (!dri2_img
->dri_image
) {
1873 return EGL_NO_IMAGE_KHR
;
1875 return &dri2_img
->base
;
1879 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1880 EGLint attribute
, EGLint
*value
)
1882 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1883 if (!dri2_dpy
->vtbl
->query_surface
)
1884 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
1885 return dri2_dpy
->vtbl
->query_surface(drv
, dpy
, surf
, attribute
, value
);
1888 static struct wl_buffer
*
1889 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1892 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1893 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, dpy
, img
);
1898 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1899 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1901 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1902 EGLint format
, name
, pitch
, err
;
1903 _EGLImageAttribs attrs
;
1904 __DRIimage
*dri_image
;
1906 name
= (EGLint
) (uintptr_t) buffer
;
1908 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1909 if (err
!= EGL_SUCCESS
)
1912 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
1913 attrs
.DRMBufferStrideMESA
<= 0) {
1914 _eglError(EGL_BAD_PARAMETER
,
1915 "bad width, height or stride");
1919 switch (attrs
.DRMBufferFormatMESA
) {
1920 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1921 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1922 pitch
= attrs
.DRMBufferStrideMESA
;
1925 _eglError(EGL_BAD_PARAMETER
,
1926 "dri2_create_image_khr: unsupported pixmap depth");
1931 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1939 return dri2_create_image_from_dri(disp
, dri_image
);
1943 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
1950 * "Required attributes and their values are as follows:
1952 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1954 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1955 * by drm_fourcc.h and used as the pixel_format parameter of the
1956 * drm_mode_fb_cmd2 ioctl."
1960 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1961 * incomplete, EGL_BAD_PARAMETER is generated."
1963 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
1964 !attrs
->DMABufFourCC
.IsPresent
) {
1965 _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
1972 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1973 * specified for a plane's pitch or offset isn't supported by EGL,
1974 * EGL_BAD_ACCESS is generated."
1976 for (i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
1977 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
1978 attrs
->DMABufPlanePitches
[i
].Value
<= 0) {
1979 _eglError(EGL_BAD_ACCESS
, "invalid pitch");
1987 /* Returns the total number of file descriptors. Zero indicates an error. */
1989 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
1991 unsigned i
, plane_n
;
1993 switch (attrs
->DMABufFourCC
.Value
) {
1995 case DRM_FORMAT_RG88
:
1996 case DRM_FORMAT_GR88
:
1997 case DRM_FORMAT_R16
:
1998 case DRM_FORMAT_GR1616
:
1999 case DRM_FORMAT_RGB332
:
2000 case DRM_FORMAT_BGR233
:
2001 case DRM_FORMAT_XRGB4444
:
2002 case DRM_FORMAT_XBGR4444
:
2003 case DRM_FORMAT_RGBX4444
:
2004 case DRM_FORMAT_BGRX4444
:
2005 case DRM_FORMAT_ARGB4444
:
2006 case DRM_FORMAT_ABGR4444
:
2007 case DRM_FORMAT_RGBA4444
:
2008 case DRM_FORMAT_BGRA4444
:
2009 case DRM_FORMAT_XRGB1555
:
2010 case DRM_FORMAT_XBGR1555
:
2011 case DRM_FORMAT_RGBX5551
:
2012 case DRM_FORMAT_BGRX5551
:
2013 case DRM_FORMAT_ARGB1555
:
2014 case DRM_FORMAT_ABGR1555
:
2015 case DRM_FORMAT_RGBA5551
:
2016 case DRM_FORMAT_BGRA5551
:
2017 case DRM_FORMAT_RGB565
:
2018 case DRM_FORMAT_BGR565
:
2019 case DRM_FORMAT_RGB888
:
2020 case DRM_FORMAT_BGR888
:
2021 case DRM_FORMAT_XRGB8888
:
2022 case DRM_FORMAT_XBGR8888
:
2023 case DRM_FORMAT_RGBX8888
:
2024 case DRM_FORMAT_BGRX8888
:
2025 case DRM_FORMAT_ARGB8888
:
2026 case DRM_FORMAT_ABGR8888
:
2027 case DRM_FORMAT_RGBA8888
:
2028 case DRM_FORMAT_BGRA8888
:
2029 case DRM_FORMAT_XRGB2101010
:
2030 case DRM_FORMAT_XBGR2101010
:
2031 case DRM_FORMAT_RGBX1010102
:
2032 case DRM_FORMAT_BGRX1010102
:
2033 case DRM_FORMAT_ARGB2101010
:
2034 case DRM_FORMAT_ABGR2101010
:
2035 case DRM_FORMAT_RGBA1010102
:
2036 case DRM_FORMAT_BGRA1010102
:
2037 case DRM_FORMAT_YUYV
:
2038 case DRM_FORMAT_YVYU
:
2039 case DRM_FORMAT_UYVY
:
2040 case DRM_FORMAT_VYUY
:
2043 case DRM_FORMAT_NV12
:
2044 case DRM_FORMAT_NV21
:
2045 case DRM_FORMAT_NV16
:
2046 case DRM_FORMAT_NV61
:
2049 case DRM_FORMAT_YUV410
:
2050 case DRM_FORMAT_YVU410
:
2051 case DRM_FORMAT_YUV411
:
2052 case DRM_FORMAT_YVU411
:
2053 case DRM_FORMAT_YUV420
:
2054 case DRM_FORMAT_YVU420
:
2055 case DRM_FORMAT_YUV422
:
2056 case DRM_FORMAT_YVU422
:
2057 case DRM_FORMAT_YUV444
:
2058 case DRM_FORMAT_YVU444
:
2062 _eglError(EGL_BAD_ATTRIBUTE
, "invalid format");
2069 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2070 * incomplete, EGL_BAD_PARAMETER is generated."
2072 for (i
= 0; i
< plane_n
; ++i
) {
2073 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2074 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2075 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2076 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2082 * The spec also says:
2084 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2085 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2086 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2087 * attributes are specified."
2089 for (i
= plane_n
; i
< 3; ++i
) {
2090 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2091 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2092 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2093 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2104 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2105 * EGL will take a reference to the dma_buf(s) which it will release at any
2106 * time while the EGLDisplay is initialized. It is the responsibility of the
2107 * application to close the dma_buf file descriptors."
2109 * Therefore we must never close or otherwise modify the file descriptors.
2112 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2113 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2115 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2118 _EGLImageAttribs attrs
;
2119 __DRIimage
*dri_image
;
2130 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2131 * error EGL_BAD_PARAMETER is generated."
2133 if (buffer
!= NULL
) {
2134 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2138 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2139 if (err
!= EGL_SUCCESS
) {
2140 _eglError(err
, "bad attribute");
2144 if (!dri2_check_dma_buf_attribs(&attrs
))
2147 num_fds
= dri2_check_dma_buf_format(&attrs
);
2151 for (i
= 0; i
< num_fds
; ++i
) {
2152 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2153 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2154 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2158 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2159 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2160 fds
, num_fds
, pitches
, offsets
,
2161 attrs
.DMABufYuvColorSpaceHint
.Value
,
2162 attrs
.DMABufSampleRangeHint
.Value
,
2163 attrs
.DMABufChromaHorizontalSiting
.Value
,
2164 attrs
.DMABufChromaVerticalSiting
.Value
,
2167 dri2_create_image_khr_texture_error(error
);
2170 return EGL_NO_IMAGE_KHR
;
2172 res
= dri2_create_image_from_dri(disp
, dri_image
);
2177 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2178 const EGLint
*attr_list
)
2180 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2181 struct dri2_egl_image
*dri2_img
;
2182 _EGLImageAttribs attrs
;
2183 unsigned int dri_use
, valid_mask
;
2185 EGLint err
= EGL_SUCCESS
;
2189 dri2_img
= malloc(sizeof *dri2_img
);
2191 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2192 return EGL_NO_IMAGE_KHR
;
2196 err
= EGL_BAD_PARAMETER
;
2200 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
2201 err
= EGL_BAD_PARAMETER
;
2205 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2206 if (err
!= EGL_SUCCESS
)
2209 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2210 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)",
2211 attrs
.Width
, attrs
.Height
);
2215 switch (attrs
.DRMBufferFormatMESA
) {
2216 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2217 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2220 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x",
2221 attrs
.DRMBufferFormatMESA
);
2226 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2227 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2228 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2229 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2230 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x",
2231 attrs
.DRMBufferUseMESA
& ~valid_mask
);
2236 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2237 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2238 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2239 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2240 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2241 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2243 dri2_img
->dri_image
=
2244 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2245 attrs
.Width
, attrs
.Height
,
2246 format
, dri_use
, dri2_img
);
2247 if (dri2_img
->dri_image
== NULL
) {
2248 err
= EGL_BAD_ALLOC
;
2252 return &dri2_img
->base
;
2256 _eglError(err
, "dri2_create_drm_image_mesa");
2258 return EGL_NO_IMAGE_KHR
;
2262 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2263 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2265 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2266 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2270 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2271 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
2272 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2277 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2278 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2281 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2282 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2288 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2290 EGLint
*fourcc
, EGLint
*nplanes
,
2291 EGLuint64KHR
*modifiers
)
2293 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2294 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2300 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2301 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2303 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2304 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2313 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2314 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2316 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2317 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2321 /* rework later to provide multiple fds/strides/offsets */
2323 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2324 __DRI_IMAGE_ATTRIB_FD
, fds
);
2327 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2328 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2332 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2333 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2335 offsets
[0] = img_offset
;
2346 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2347 _EGLContext
*ctx
, EGLenum target
,
2348 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2353 case EGL_GL_TEXTURE_2D_KHR
:
2354 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2355 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2356 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2357 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2358 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2359 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2360 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2361 case EGL_GL_TEXTURE_3D_KHR
:
2362 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
2363 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2366 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2367 return EGL_NO_IMAGE_KHR
;
2369 case EGL_GL_RENDERBUFFER_KHR
:
2370 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2372 case EGL_DRM_BUFFER_MESA
:
2373 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2374 case EGL_LINUX_DMA_BUF_EXT
:
2375 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2377 #ifdef HAVE_WAYLAND_PLATFORM
2378 case EGL_WAYLAND_BUFFER_WL
:
2379 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2382 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2383 return EGL_NO_IMAGE_KHR
;
2388 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2390 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2391 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2395 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2401 #ifdef HAVE_WAYLAND_PLATFORM
2404 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2405 struct wl_drm_buffer
*buffer
)
2407 _EGLDisplay
*disp
= user_data
;
2408 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2410 int i
, dri_components
= 0;
2413 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2422 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2434 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2436 buffer
->driver_format
= NULL
;
2437 for (i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2438 if (wl_drm_components
[i
].dri_components
== dri_components
)
2439 buffer
->driver_format
= &wl_drm_components
[i
];
2441 if (buffer
->driver_format
== NULL
)
2442 dri2_dpy
->image
->destroyImage(img
);
2444 buffer
->driver_buffer
= img
;
2448 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2450 _EGLDisplay
*disp
= user_data
;
2451 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2453 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2456 static struct wayland_drm_callbacks wl_drm_callbacks
= {
2457 .authenticate
= NULL
,
2458 .reference_buffer
= dri2_wl_reference_buffer
,
2459 .release_buffer
= dri2_wl_release_buffer
2463 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2464 struct wl_display
*wl_dpy
)
2466 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2472 if (dri2_dpy
->wl_server_drm
)
2475 wl_drm_callbacks
.authenticate
=
2476 (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
;
2478 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2479 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2480 dri2_dpy
->image
->base
.version
>= 7 &&
2481 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2482 flags
|= WAYLAND_DRM_PRIME
;
2484 dri2_dpy
->wl_server_drm
=
2485 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2486 &wl_drm_callbacks
, disp
, flags
);
2488 if (!dri2_dpy
->wl_server_drm
)
2491 #ifdef HAVE_DRM_PLATFORM
2492 /* We have to share the wl_drm instance with gbm, so gbm can convert
2493 * wl_buffers to gbm bos. */
2494 if (dri2_dpy
->gbm_dri
)
2495 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2502 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2503 struct wl_display
*wl_dpy
)
2505 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2509 if (!dri2_dpy
->wl_server_drm
)
2512 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2513 dri2_dpy
->wl_server_drm
= NULL
;
2519 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2520 struct wl_resource
*buffer_resource
,
2521 EGLint attribute
, EGLint
*value
)
2523 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2524 struct wl_drm_buffer
*buffer
;
2525 const struct wl_drm_components_descriptor
*format
;
2527 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2531 format
= buffer
->driver_format
;
2532 switch (attribute
) {
2533 case EGL_TEXTURE_FORMAT
:
2534 *value
= format
->components
;
2537 *value
= buffer
->width
;
2540 *value
= buffer
->height
;
2549 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2551 p_atomic_inc(&sync
->refcount
);
2555 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2556 struct dri2_egl_sync
*dri2_sync
)
2558 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2559 switch (dri2_sync
->base
.Type
) {
2560 case EGL_SYNC_REUSABLE_KHR
:
2561 cnd_destroy(&dri2_sync
->cond
);
2563 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2564 if (dri2_sync
->base
.SyncFd
!= EGL_NO_NATIVE_FENCE_FD_ANDROID
)
2565 close(dri2_sync
->base
.SyncFd
);
2571 if (dri2_sync
->fence
)
2572 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2579 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
2580 EGLenum type
, const EGLAttrib
*attrib_list
)
2582 _EGLContext
*ctx
= _eglGetCurrentContext();
2583 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2584 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2585 struct dri2_egl_sync
*dri2_sync
;
2587 pthread_condattr_t attr
;
2589 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2591 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2595 if (!_eglInitSync(&dri2_sync
->base
, dpy
, type
, attrib_list
)) {
2601 case EGL_SYNC_FENCE_KHR
:
2602 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2603 if (!dri2_sync
->fence
) {
2604 /* Why did it fail? DRI doesn't return an error code, so we emit
2605 * a generic EGL error that doesn't communicate user error.
2607 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2613 case EGL_SYNC_CL_EVENT_KHR
:
2614 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2615 dri2_dpy
->dri_screen
,
2616 dri2_sync
->base
.CLEvent
);
2617 /* this can only happen if the cl_event passed in is invalid. */
2618 if (!dri2_sync
->fence
) {
2619 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2624 /* the initial status must be "signaled" if the cl_event is signaled */
2625 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2626 dri2_sync
->fence
, 0, 0))
2627 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2630 case EGL_SYNC_REUSABLE_KHR
:
2631 /* intialize attr */
2632 ret
= pthread_condattr_init(&attr
);
2635 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2640 /* change clock attribute to CLOCK_MONOTONIC */
2641 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
2644 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2649 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
2652 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2657 /* initial status of reusable sync must be "unsignaled" */
2658 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
2661 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2662 if (dri2_dpy
->fence
->create_fence_fd
) {
2663 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence_fd(
2664 dri2_ctx
->dri_context
,
2665 dri2_sync
->base
.SyncFd
);
2667 if (!dri2_sync
->fence
) {
2668 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2675 p_atomic_set(&dri2_sync
->refcount
, 1);
2676 return &dri2_sync
->base
;
2680 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2682 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2683 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2684 EGLint ret
= EGL_TRUE
;
2687 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
2688 * then unlock all threads possibly blocked by the reusable sync before
2691 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
2692 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2693 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2694 /* unblock all threads currently blocked by sync */
2695 err
= cnd_broadcast(&dri2_sync
->cond
);
2698 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
2703 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2709 dri2_dup_native_fence_fd(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2711 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2712 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2714 assert(sync
->Type
== EGL_SYNC_NATIVE_FENCE_ANDROID
);
2716 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
2717 /* try to retrieve the actual native fence fd.. if rendering is
2718 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
2720 sync
->SyncFd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
2724 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
2725 /* if native fence fd still not created, return an error: */
2726 _eglError(EGL_BAD_PARAMETER
, "eglDupNativeFenceFDANDROID");
2727 return EGL_NO_NATIVE_FENCE_FD_ANDROID
;
2730 return dup(sync
->SyncFd
);
2734 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2735 EGLint flags
, EGLTime timeout
)
2737 _EGLContext
*ctx
= _eglGetCurrentContext();
2738 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2739 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2740 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2741 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2742 unsigned wait_flags
= 0;
2744 /* timespecs for cnd_timedwait */
2745 struct timespec current
;
2748 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
2750 /* The EGL_KHR_fence_sync spec states:
2752 * "If no context is current for the bound API,
2753 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
2755 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
2756 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
2758 /* the sync object should take a reference while waiting */
2759 dri2_egl_ref_sync(dri2_sync
);
2761 switch (sync
->Type
) {
2762 case EGL_SYNC_FENCE_KHR
:
2763 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2764 case EGL_SYNC_CL_EVENT_KHR
:
2765 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
2766 dri2_sync
->fence
, wait_flags
,
2768 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2770 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2773 case EGL_SYNC_REUSABLE_KHR
:
2774 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
2775 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
2776 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
2777 dri2_drv
->glFlush();
2780 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
2781 if (timeout
== EGL_FOREVER_KHR
) {
2782 mtx_lock(&dri2_sync
->mutex
);
2783 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
2784 mtx_unlock(&dri2_sync
->mutex
);
2786 /* if reusable sync has not been yet signaled */
2787 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
2788 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
2790 /* calculating when to expire */
2791 expire
.nsec
= timeout
% 1000000000L;
2792 expire
.sec
= timeout
/ 1000000000L;
2794 expire
.nsec
+= current
.tv_nsec
;
2795 expire
.sec
+= current
.tv_sec
;
2797 /* expire.nsec now is a number between 0 and 1999999998 */
2798 if (expire
.nsec
> 999999999L) {
2800 expire
.nsec
-= 1000000000L;
2803 mtx_lock(&dri2_sync
->mutex
);
2804 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
2805 mtx_unlock(&dri2_sync
->mutex
);
2807 if (ret
== thrd_busy
) {
2808 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2809 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2811 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2819 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2825 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2828 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2831 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
) {
2832 _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
2836 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
) {
2837 _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
2841 dri2_sync
->base
.SyncStatus
= mode
;
2843 if (mode
== EGL_SIGNALED_KHR
) {
2844 ret
= cnd_broadcast(&dri2_sync
->cond
);
2846 /* fail to broadcast */
2848 _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
2857 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2859 _EGLContext
*ctx
= _eglGetCurrentContext();
2860 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2861 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2862 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2864 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
2865 dri2_sync
->fence
, 0);
2870 dri2_interop_query_device_info(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2871 struct mesa_glinterop_device_info
*out
)
2873 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2874 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2876 if (!dri2_dpy
->interop
)
2877 return MESA_GLINTEROP_UNSUPPORTED
;
2879 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
2883 dri2_interop_export_object(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2884 struct mesa_glinterop_export_in
*in
,
2885 struct mesa_glinterop_export_out
*out
)
2887 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2888 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2890 if (!dri2_dpy
->interop
)
2891 return MESA_GLINTEROP_UNSUPPORTED
;
2893 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
2897 dri2_unload(_EGLDriver
*drv
)
2899 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2901 dlclose(dri2_drv
->handle
);
2906 dri2_load(_EGLDriver
*drv
)
2908 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2909 #ifdef HAVE_ANDROID_PLATFORM
2910 const char *libname
= "libglapi.so";
2911 #elif defined(__APPLE__)
2912 const char *libname
= "libglapi.0.dylib";
2913 #elif defined(__CYGWIN__)
2914 const char *libname
= "cygglapi-0.dll";
2916 const char *libname
= "libglapi.so.0";
2920 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2921 handle
= dlopen(libname
, RTLD_LAZY
| RTLD_GLOBAL
);
2923 _eglLog(_EGL_WARNING
, "DRI2: failed to open glapi provider");
2927 dri2_drv
->get_proc_address
= (_EGLProc (*)(const char *))
2928 dlsym(handle
, "_glapi_get_proc_address");
2930 /* if glapi is not available, loading DRI drivers will fail */
2931 if (!dri2_drv
->get_proc_address
) {
2932 _eglLog(_EGL_WARNING
, "DRI2: failed to find _glapi_get_proc_address");
2936 dri2_drv
->glFlush
= (void (*)(void))
2937 dri2_drv
->get_proc_address("glFlush");
2939 /* if glFlush is not available things are horribly broken */
2940 if (!dri2_drv
->glFlush
) {
2941 _eglLog(_EGL_WARNING
, "DRI2: failed to find glFlush entry point");
2945 dri2_drv
->handle
= handle
;
2955 * This is the main entrypoint into the driver, called by libEGL.
2956 * Create a new _EGLDriver object and init its dispatch table.
2959 _eglBuiltInDriverDRI2(const char *args
)
2961 struct dri2_egl_driver
*dri2_drv
;
2965 dri2_drv
= calloc(1, sizeof *dri2_drv
);
2969 if (!dri2_load(&dri2_drv
->base
)) {
2974 _eglInitDriverFallbacks(&dri2_drv
->base
);
2975 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
2976 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
2977 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
2978 dri2_drv
->base
.API
.DestroyContext
= dri2_destroy_context
;
2979 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
2980 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
2981 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
2982 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
2983 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
2984 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
2985 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
2986 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
2987 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
2988 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
2989 dri2_drv
->base
.API
.SwapInterval
= dri2_swap_interval
;
2990 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
2991 dri2_drv
->base
.API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
2992 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
2993 dri2_drv
->base
.API
.PostSubBufferNV
= dri2_post_sub_buffer
;
2994 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
,
2995 dri2_drv
->base
.API
.QueryBufferAge
= dri2_query_buffer_age
;
2996 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image
;
2997 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
2998 dri2_drv
->base
.API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
2999 dri2_drv
->base
.API
.QuerySurface
= dri2_query_surface
;
3001 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
3002 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
3003 dri2_drv
->base
.API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
3004 dri2_drv
->base
.API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
3006 #ifdef HAVE_WAYLAND_PLATFORM
3007 dri2_drv
->base
.API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
3008 dri2_drv
->base
.API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
3009 dri2_drv
->base
.API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
3011 dri2_drv
->base
.API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
3012 dri2_drv
->base
.API
.CreateSyncKHR
= dri2_create_sync
;
3013 dri2_drv
->base
.API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
3014 dri2_drv
->base
.API
.SignalSyncKHR
= dri2_signal_sync
;
3015 dri2_drv
->base
.API
.WaitSyncKHR
= dri2_server_wait_sync
;
3016 dri2_drv
->base
.API
.DestroySyncKHR
= dri2_destroy_sync
;
3017 dri2_drv
->base
.API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
3018 dri2_drv
->base
.API
.GLInteropExportObject
= dri2_interop_export_object
;
3019 dri2_drv
->base
.API
.DupNativeFenceFDANDROID
= dri2_dup_native_fence_fd
;
3021 dri2_drv
->base
.Name
= "DRI2";
3022 dri2_drv
->base
.Unload
= dri2_unload
;
3024 return &dri2_drv
->base
;