2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Kristian Høgsberg <krh@bitplanet.net>
28 #define WL_HIDE_DEPRECATED
41 #include <c11/threads.h>
45 #include <drm_fourcc.h>
48 #include <GL/internal/dri_interface.h>
49 #include "GL/mesa_glinterop.h"
50 #include <sys/types.h>
53 #ifdef HAVE_WAYLAND_PLATFORM
54 #include "wayland-drm.h"
55 #include "wayland-drm-client-protocol.h"
59 #include "util/u_atomic.h"
61 /* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
62 * some of the definitions here so that building Mesa won't bleeding-edge
66 #define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
69 #ifndef DRM_FORMAT_RG88
70 #define DRM_FORMAT_RG88 fourcc_code('R', 'G', '8', '8') /* [15:0] R:G 8:8 little endian */
73 #ifndef DRM_FORMAT_GR88
74 #define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
77 const __DRIuseInvalidateExtension use_invalidate
= {
78 .base
= { __DRI_USE_INVALIDATE
, 1 }
81 EGLint dri2_to_egl_attribute_map
[] = {
83 EGL_BUFFER_SIZE
, /* __DRI_ATTRIB_BUFFER_SIZE */
84 EGL_LEVEL
, /* __DRI_ATTRIB_LEVEL */
85 EGL_RED_SIZE
, /* __DRI_ATTRIB_RED_SIZE */
86 EGL_GREEN_SIZE
, /* __DRI_ATTRIB_GREEN_SIZE */
87 EGL_BLUE_SIZE
, /* __DRI_ATTRIB_BLUE_SIZE */
88 EGL_LUMINANCE_SIZE
, /* __DRI_ATTRIB_LUMINANCE_SIZE */
89 EGL_ALPHA_SIZE
, /* __DRI_ATTRIB_ALPHA_SIZE */
90 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
91 EGL_DEPTH_SIZE
, /* __DRI_ATTRIB_DEPTH_SIZE */
92 EGL_STENCIL_SIZE
, /* __DRI_ATTRIB_STENCIL_SIZE */
93 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
94 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
95 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
96 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
97 EGL_SAMPLE_BUFFERS
, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
98 EGL_SAMPLES
, /* __DRI_ATTRIB_SAMPLES */
99 0, /* __DRI_ATTRIB_RENDER_TYPE, */
100 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
101 0, /* __DRI_ATTRIB_CONFORMANT */
102 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
103 0, /* __DRI_ATTRIB_STEREO */
104 0, /* __DRI_ATTRIB_AUX_BUFFERS */
105 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
106 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
107 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
108 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
109 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
110 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
111 0, /* __DRI_ATTRIB_FLOAT_MODE (deprecated) */
112 0, /* __DRI_ATTRIB_RED_MASK */
113 0, /* __DRI_ATTRIB_GREEN_MASK */
114 0, /* __DRI_ATTRIB_BLUE_MASK */
115 0, /* __DRI_ATTRIB_ALPHA_MASK */
116 EGL_MAX_PBUFFER_WIDTH
, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
117 EGL_MAX_PBUFFER_HEIGHT
, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
118 EGL_MAX_PBUFFER_PIXELS
, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
119 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
120 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
121 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
122 0, /* __DRI_ATTRIB_SWAP_METHOD */
123 EGL_MAX_SWAP_INTERVAL
, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
124 EGL_MIN_SWAP_INTERVAL
, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
125 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
126 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
127 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
128 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
129 EGL_Y_INVERTED_NOK
, /* __DRI_ATTRIB_YINVERTED */
130 0, /* __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE */
134 dri2_get_dri_config(struct dri2_egl_config
*conf
, EGLint surface_type
,
137 const bool srgb
= colorspace
== EGL_GL_COLORSPACE_SRGB_KHR
;
139 return surface_type
== EGL_WINDOW_BIT
? conf
->dri_double_config
[srgb
] :
140 conf
->dri_single_config
[srgb
];
144 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
146 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
149 if (!_eglMatchConfig(conf
, criteria
))
155 struct dri2_egl_config
*
156 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
157 EGLint surface_type
, const EGLint
*attr_list
,
158 const unsigned int *rgba_masks
)
160 struct dri2_egl_config
*conf
;
161 struct dri2_egl_display
*dri2_dpy
;
163 unsigned int attrib
, value
, double_buffer
;
165 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
166 unsigned int dri_masks
[4] = { 0, 0, 0, 0 };
167 _EGLConfig
*matching_config
;
168 EGLint num_configs
= 0;
172 dri2_dpy
= disp
->DriverData
;
173 _eglInitConfig(&base
, disp
, id
);
177 bind_to_texture_rgb
= 0;
178 bind_to_texture_rgba
= 0;
180 while (dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
++, &attrib
, &value
)) {
182 case __DRI_ATTRIB_RENDER_TYPE
:
183 if (value
& __DRI_ATTRIB_RGBA_BIT
)
184 value
= EGL_RGB_BUFFER
;
185 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
186 value
= EGL_LUMINANCE_BUFFER
;
189 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
192 case __DRI_ATTRIB_CONFIG_CAVEAT
:
193 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
194 value
= EGL_NON_CONFORMANT_CONFIG
;
195 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
196 value
= EGL_SLOW_CONFIG
;
199 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
202 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
203 bind_to_texture_rgb
= value
;
206 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
207 bind_to_texture_rgba
= value
;
210 case __DRI_ATTRIB_DOUBLE_BUFFER
:
211 double_buffer
= value
;
214 case __DRI_ATTRIB_RED_MASK
:
215 dri_masks
[0] = value
;
218 case __DRI_ATTRIB_GREEN_MASK
:
219 dri_masks
[1] = value
;
222 case __DRI_ATTRIB_BLUE_MASK
:
223 dri_masks
[2] = value
;
226 case __DRI_ATTRIB_ALPHA_MASK
:
227 dri_masks
[3] = value
;
230 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
231 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
232 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
233 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
234 /* Don't expose visuals with the accumulation buffer. */
239 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
241 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
246 key
= dri2_to_egl_attribute_map
[attrib
];
248 _eglSetConfigKey(&base
, key
, value
);
254 for (i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
255 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
257 if (rgba_masks
&& memcmp(rgba_masks
, dri_masks
, sizeof(dri_masks
)))
260 base
.NativeRenderable
= EGL_TRUE
;
262 base
.SurfaceType
= surface_type
;
263 if (surface_type
& (EGL_PBUFFER_BIT
|
264 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
265 base
.BindToTextureRGB
= bind_to_texture_rgb
;
266 if (base
.AlphaSize
> 0)
267 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
270 base
.RenderableType
= disp
->ClientAPIs
;
271 base
.Conformant
= disp
->ClientAPIs
;
273 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
274 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
276 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
277 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
281 config_id
= base
.ConfigID
;
282 base
.ConfigID
= EGL_DONT_CARE
;
283 base
.SurfaceType
= EGL_DONT_CARE
;
284 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
285 (_EGLArrayForEach
) dri2_match_config
, &base
);
287 if (num_configs
== 1) {
288 conf
= (struct dri2_egl_config
*) matching_config
;
290 if (double_buffer
&& !conf
->dri_double_config
[srgb
])
291 conf
->dri_double_config
[srgb
] = dri_config
;
292 else if (!double_buffer
&& !conf
->dri_single_config
[srgb
])
293 conf
->dri_single_config
[srgb
] = dri_config
;
295 /* a similar config type is already added (unlikely) => discard */
298 else if (num_configs
== 0) {
299 conf
= calloc(1, sizeof *conf
);
304 conf
->dri_double_config
[srgb
] = dri_config
;
306 conf
->dri_single_config
[srgb
] = dri_config
;
308 memcpy(&conf
->base
, &base
, sizeof base
);
309 conf
->base
.SurfaceType
= 0;
310 conf
->base
.ConfigID
= config_id
;
312 _eglLinkConfig(&conf
->base
);
320 surface_type
&= ~EGL_PIXMAP_BIT
;
323 /* No support for pbuffer + MSAA for now.
325 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
326 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
329 surface_type
&= ~EGL_PBUFFER_BIT
;
332 conf
->base
.SurfaceType
|= surface_type
;
338 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
340 _EGLDisplay
*disp
= data
;
341 struct dri2_egl_image
*dri2_img
;
346 img
= _eglLookupImage(image
, disp
);
348 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
352 dri2_img
= dri2_egl_image(image
);
354 return dri2_img
->dri_image
;
357 const __DRIimageLookupExtension image_lookup_extension
= {
358 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
360 .lookupEGLImage
= dri2_lookup_egl_image
363 struct dri2_extension_match
{
369 static struct dri2_extension_match dri3_driver_extensions
[] = {
370 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
371 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
375 static struct dri2_extension_match dri2_driver_extensions
[] = {
376 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
377 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
381 static struct dri2_extension_match dri2_core_extensions
[] = {
382 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
383 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
384 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
388 static struct dri2_extension_match swrast_driver_extensions
[] = {
389 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
390 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
394 static struct dri2_extension_match swrast_core_extensions
[] = {
395 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
400 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
401 struct dri2_extension_match
*matches
,
402 const __DRIextension
**extensions
)
404 int i
, j
, ret
= EGL_TRUE
;
407 for (i
= 0; extensions
[i
]; i
++) {
408 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
409 for (j
= 0; matches
[j
].name
; j
++) {
410 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
411 extensions
[i
]->version
>= matches
[j
].version
) {
412 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
413 *(const __DRIextension
**) field
= extensions
[i
];
414 _eglLog(_EGL_INFO
, "found extension %s version %d",
415 extensions
[i
]->name
, extensions
[i
]->version
);
420 for (j
= 0; matches
[j
].name
; j
++) {
421 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
422 if (*(const __DRIextension
**) field
== NULL
) {
423 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
424 matches
[j
].name
, matches
[j
].version
);
432 static const __DRIextension
**
433 dri2_open_driver(_EGLDisplay
*disp
)
435 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
436 const __DRIextension
**extensions
= NULL
;
437 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
438 char *get_extensions_name
;
439 const __DRIextension
**(*get_extensions
)(void);
442 if (geteuid() == getuid()) {
443 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
444 search_paths
= getenv("LIBGL_DRIVERS_PATH");
446 if (search_paths
== NULL
)
447 search_paths
= DEFAULT_DRIVER_DIR
;
449 dri2_dpy
->driver
= NULL
;
450 end
= search_paths
+ strlen(search_paths
);
451 for (p
= search_paths
; p
< end
; p
= next
+ 1) {
453 next
= strchr(p
, ':');
459 snprintf(path
, sizeof path
,
460 "%.*s/tls/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
461 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
463 if (dri2_dpy
->driver
== NULL
) {
464 snprintf(path
, sizeof path
,
465 "%.*s/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
466 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
467 if (dri2_dpy
->driver
== NULL
)
468 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
470 /* not need continue to loop all paths once the driver is found */
471 if (dri2_dpy
->driver
!= NULL
)
475 snprintf(path
, sizeof path
, "%.*s/gallium_dri.so", len
, p
);
476 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
477 if (dri2_dpy
->driver
== NULL
)
478 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
484 if (dri2_dpy
->driver
== NULL
) {
485 _eglLog(_EGL_WARNING
,
486 "DRI2: failed to open %s (search paths %s)",
487 dri2_dpy
->driver_name
, search_paths
);
491 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
493 if (asprintf(&get_extensions_name
, "%s_%s",
494 __DRI_DRIVER_GET_EXTENSIONS
, dri2_dpy
->driver_name
) != -1) {
495 get_extensions
= dlsym(dri2_dpy
->driver
, get_extensions_name
);
496 if (get_extensions
) {
497 extensions
= get_extensions();
499 _eglLog(_EGL_DEBUG
, "driver does not expose %s(): %s\n",
500 get_extensions_name
, dlerror());
502 free(get_extensions_name
);
506 extensions
= dlsym(dri2_dpy
->driver
, __DRI_DRIVER_EXTENSIONS
);
507 if (extensions
== NULL
) {
508 _eglLog(_EGL_WARNING
,
509 "DRI2: driver exports no extensions (%s)", dlerror());
510 dlclose(dri2_dpy
->driver
);
517 dri2_load_driver_dri3(_EGLDisplay
*disp
)
519 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
520 const __DRIextension
**extensions
;
522 extensions
= dri2_open_driver(disp
);
526 if (!dri2_bind_extensions(dri2_dpy
, dri3_driver_extensions
, extensions
)) {
527 dlclose(dri2_dpy
->driver
);
530 dri2_dpy
->driver_extensions
= extensions
;
536 dri2_load_driver(_EGLDisplay
*disp
)
538 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
539 const __DRIextension
**extensions
;
541 extensions
= dri2_open_driver(disp
);
545 if (!dri2_bind_extensions(dri2_dpy
, dri2_driver_extensions
, extensions
)) {
546 dlclose(dri2_dpy
->driver
);
549 dri2_dpy
->driver_extensions
= extensions
;
555 dri2_load_driver_swrast(_EGLDisplay
*disp
)
557 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
558 const __DRIextension
**extensions
;
560 extensions
= dri2_open_driver(disp
);
564 if (!dri2_bind_extensions(dri2_dpy
, swrast_driver_extensions
, extensions
)) {
565 dlclose(dri2_dpy
->driver
);
568 dri2_dpy
->driver_extensions
= extensions
;
574 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
576 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
577 unsigned int value
= 0;
579 if (!rendererQuery
||
580 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
587 dri2_setup_screen(_EGLDisplay
*disp
)
589 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
590 unsigned int api_mask
;
592 if (dri2_dpy
->image_driver
) {
593 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
594 } else if (dri2_dpy
->dri2
) {
595 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
597 assert(dri2_dpy
->swrast
);
598 api_mask
= 1 << __DRI_API_OPENGL
|
599 1 << __DRI_API_GLES
|
600 1 << __DRI_API_GLES2
|
601 1 << __DRI_API_GLES3
;
604 disp
->ClientAPIs
= 0;
605 if ((api_mask
& (1 <<__DRI_API_OPENGL
)) && _eglIsApiValid(EGL_OPENGL_API
))
606 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
607 if ((api_mask
& (1 << __DRI_API_GLES
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
608 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
609 if ((api_mask
& (1 << __DRI_API_GLES2
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
610 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
611 if ((api_mask
& (1 << __DRI_API_GLES3
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
612 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
614 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
615 disp
->Extensions
.KHR_no_config_context
= EGL_TRUE
;
616 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
618 if (dri2_renderer_query_integer(dri2_dpy
,
619 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
620 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
622 if (dri2_dpy
->image_driver
||
623 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
624 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
625 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
627 if (dri2_dpy
->robustness
)
628 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
631 if (dri2_dpy
->fence
) {
632 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
633 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
634 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
635 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
638 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
640 if (dri2_dpy
->image
) {
641 if (dri2_dpy
->image
->base
.version
>= 10 &&
642 dri2_dpy
->image
->getCapabilities
!= NULL
) {
645 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
646 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
648 if (dri2_dpy
->image
->base
.version
>= 11)
649 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
651 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
652 if (dri2_dpy
->image
->base
.version
>= 11)
653 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
656 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
657 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
658 if (dri2_dpy
->image
->base
.version
>= 5 &&
659 dri2_dpy
->image
->createImageFromTexture
) {
660 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
661 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
663 if (dri2_renderer_query_integer(dri2_dpy
,
664 __DRI2_RENDERER_HAS_TEXTURE_3D
))
665 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
667 if (dri2_dpy
->image
->base
.version
>= 8 &&
668 dri2_dpy
->image
->createImageFromDmaBufs
) {
669 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
675 /* All platforms but DRM call this function to create the screen, query the
676 * dri extensions, setup the vtables and populate the driver_configs.
677 * DRM inherits all that information from its display - GBM.
680 dri2_create_screen(_EGLDisplay
*disp
)
682 const __DRIextension
**extensions
;
683 struct dri2_egl_display
*dri2_dpy
;
686 dri2_dpy
= disp
->DriverData
;
688 if (dri2_dpy
->image_driver
) {
689 dri2_dpy
->dri_screen
=
690 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
691 dri2_dpy
->extensions
,
692 dri2_dpy
->driver_extensions
,
693 &dri2_dpy
->driver_configs
,
695 } else if (dri2_dpy
->dri2
) {
696 if (dri2_dpy
->dri2
->base
.version
>= 4) {
697 dri2_dpy
->dri_screen
=
698 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
699 dri2_dpy
->extensions
,
700 dri2_dpy
->driver_extensions
,
701 &dri2_dpy
->driver_configs
, disp
);
703 dri2_dpy
->dri_screen
=
704 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
705 dri2_dpy
->extensions
,
706 &dri2_dpy
->driver_configs
, disp
);
709 assert(dri2_dpy
->swrast
);
710 if (dri2_dpy
->swrast
->base
.version
>= 4) {
711 dri2_dpy
->dri_screen
=
712 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->extensions
,
713 dri2_dpy
->driver_extensions
,
714 &dri2_dpy
->driver_configs
, disp
);
716 dri2_dpy
->dri_screen
=
717 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->extensions
,
718 &dri2_dpy
->driver_configs
, disp
);
722 if (dri2_dpy
->dri_screen
== NULL
) {
723 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
727 dri2_dpy
->own_dri_screen
= 1;
729 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
731 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
) {
732 if (!dri2_bind_extensions(dri2_dpy
, dri2_core_extensions
, extensions
))
733 goto cleanup_dri_screen
;
735 assert(dri2_dpy
->swrast
);
736 if (!dri2_bind_extensions(dri2_dpy
, swrast_core_extensions
, extensions
))
737 goto cleanup_dri_screen
;
740 for (i
= 0; extensions
[i
]; i
++) {
741 if (strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0) {
742 dri2_dpy
->robustness
= (__DRIrobustnessExtension
*) extensions
[i
];
744 if (strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0) {
745 dri2_dpy
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
747 if (strcmp(extensions
[i
]->name
, __DRI2_FENCE
) == 0) {
748 dri2_dpy
->fence
= (__DRI2fenceExtension
*) extensions
[i
];
750 if (strcmp(extensions
[i
]->name
, __DRI2_RENDERER_QUERY
) == 0) {
751 dri2_dpy
->rendererQuery
= (__DRI2rendererQueryExtension
*) extensions
[i
];
753 if (strcmp(extensions
[i
]->name
, __DRI2_INTEROP
) == 0)
754 dri2_dpy
->interop
= (__DRI2interopExtension
*) extensions
[i
];
757 dri2_setup_screen(disp
);
762 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
768 * Called via eglInitialize(), GLX_drv->API.Initialize().
770 * This must be guaranteed to be called exactly once, even if eglInitialize is
771 * called many times (without a eglTerminate in between).
774 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
776 EGLBoolean ret
= EGL_FALSE
;
777 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
779 /* In the case where the application calls eglMakeCurrent(context1),
780 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
781 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
782 * initialized, as we need it to be able to free context1 correctly.
784 * It would probably be safest to forcibly release the display with
785 * dri2_display_release, to make sure the display is reinitialized correctly.
786 * However, the EGL spec states that we need to keep a reference to the
787 * current context (so we cannot call dri2_make_current(NULL)), and therefore
788 * we would leak context1 as we would be missing the old display connection
789 * to free it up correctly.
792 dri2_dpy
->ref_count
++;
796 /* not until swrast_dri is supported */
797 if (disp
->Options
.UseFallback
)
800 /* Nothing to initialize for a test only display */
801 if (disp
->Options
.TestOnly
)
804 switch (disp
->Platform
) {
805 #ifdef HAVE_SURFACELESS_PLATFORM
806 case _EGL_PLATFORM_SURFACELESS
:
807 ret
= dri2_initialize_surfaceless(drv
, disp
);
810 #ifdef HAVE_X11_PLATFORM
811 case _EGL_PLATFORM_X11
:
812 ret
= dri2_initialize_x11(drv
, disp
);
815 #ifdef HAVE_DRM_PLATFORM
816 case _EGL_PLATFORM_DRM
:
817 ret
= dri2_initialize_drm(drv
, disp
);
820 #ifdef HAVE_WAYLAND_PLATFORM
821 case _EGL_PLATFORM_WAYLAND
:
822 ret
= dri2_initialize_wayland(drv
, disp
);
825 #ifdef HAVE_ANDROID_PLATFORM
826 case _EGL_PLATFORM_ANDROID
:
827 ret
= dri2_initialize_android(drv
, disp
);
831 _eglLog(_EGL_WARNING
, "No EGL platform enabled.");
836 dri2_dpy
= dri2_egl_display(disp
);
842 dri2_dpy
->ref_count
++;
849 * Decrement display reference count, and free up display if necessary.
852 dri2_display_release(_EGLDisplay
*disp
) {
853 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
856 assert(dri2_dpy
->ref_count
> 0);
857 dri2_dpy
->ref_count
--;
859 if (dri2_dpy
->ref_count
> 0)
862 _eglCleanupDisplay(disp
);
864 if (dri2_dpy
->own_dri_screen
)
865 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
866 if (dri2_dpy
->fd
>= 0)
868 if (dri2_dpy
->driver
)
869 dlclose(dri2_dpy
->driver
);
870 free(dri2_dpy
->driver_name
);
872 #ifdef HAVE_WAYLAND_PLATFORM
873 free(dri2_dpy
->device_name
);
876 switch (disp
->Platform
) {
877 #ifdef HAVE_X11_PLATFORM
878 case _EGL_PLATFORM_X11
:
879 if (dri2_dpy
->own_device
) {
880 xcb_disconnect(dri2_dpy
->conn
);
884 #ifdef HAVE_DRM_PLATFORM
885 case _EGL_PLATFORM_DRM
:
886 if (dri2_dpy
->own_device
) {
887 gbm_device_destroy(&dri2_dpy
->gbm_dri
->base
.base
);
891 #ifdef HAVE_WAYLAND_PLATFORM
892 case _EGL_PLATFORM_WAYLAND
:
893 if (dri2_dpy
->wl_drm
)
894 wl_drm_destroy(dri2_dpy
->wl_drm
);
895 if (dri2_dpy
->wl_shm
)
896 wl_shm_destroy(dri2_dpy
->wl_shm
);
897 wl_registry_destroy(dri2_dpy
->wl_registry
);
898 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
899 if (dri2_dpy
->own_device
) {
900 wl_display_disconnect(dri2_dpy
->wl_dpy
);
908 /* The drm platform does not create the screen/driver_configs but reuses
909 * the ones from the gbm device. As such the gbm itself is responsible
912 if (disp
->Platform
!= _EGL_PLATFORM_DRM
) {
913 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
914 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
915 free(dri2_dpy
->driver_configs
);
918 disp
->DriverData
= NULL
;
922 * Called via eglTerminate(), drv->API.Terminate().
924 * This must be guaranteed to be called exactly once, even if eglTerminate is
925 * called many times (without a eglInitialize in between).
928 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
930 /* Release all non-current Context/Surfaces. */
931 _eglReleaseDisplayResources(drv
, disp
);
933 dri2_display_release(disp
);
939 * Set the error code after a call to
940 * dri2_egl_display::dri2::createContextAttribs.
943 dri2_create_context_attribs_error(int dri_error
)
948 case __DRI_CTX_ERROR_SUCCESS
:
951 case __DRI_CTX_ERROR_NO_MEMORY
:
952 egl_error
= EGL_BAD_ALLOC
;
955 /* From the EGL_KHR_create_context spec, section "Errors":
957 * * If <config> does not support a client API context compatible
958 * with the requested API major and minor version, [...] context flags,
959 * and context reset notification behavior (for client API types where
960 * these attributes are supported), then an EGL_BAD_MATCH error is
963 * * If an OpenGL ES context is requested and the values for
964 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
965 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
966 * is not defined, than an EGL_BAD_MATCH error is generated.
968 * * If an OpenGL context is requested, the requested version is
969 * greater than 3.2, and the value for attribute
970 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
971 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
972 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
973 * one of these bits set; or if the implementation does not support
974 * the requested profile, then an EGL_BAD_MATCH error is generated.
976 case __DRI_CTX_ERROR_BAD_API
:
977 case __DRI_CTX_ERROR_BAD_VERSION
:
978 case __DRI_CTX_ERROR_BAD_FLAG
:
979 egl_error
= EGL_BAD_MATCH
;
982 /* From the EGL_KHR_create_context spec, section "Errors":
984 * * If an attribute name or attribute value in <attrib_list> is not
985 * recognized (including unrecognized bits in bitmask attributes),
986 * then an EGL_BAD_ATTRIBUTE error is generated."
988 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
989 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
990 egl_error
= EGL_BAD_ATTRIBUTE
;
995 egl_error
= EGL_BAD_MATCH
;
999 _eglError(egl_error
, "dri2_create_context");
1003 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1004 struct dri2_egl_display
*dri2_dpy
,
1005 uint32_t *ctx_attribs
,
1006 unsigned *num_attribs
)
1010 assert(*num_attribs
>= 8);
1012 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1013 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1014 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1015 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1017 if (dri2_ctx
->base
.Flags
!= 0) {
1018 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1019 * extension, don't even try to send it the robust-access flag.
1020 * It may explode. Instead, generate the required EGL error here.
1022 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1023 && !dri2_dpy
->robustness
) {
1024 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1028 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1029 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
;
1032 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1033 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1034 * extension, don't even try to send it a reset strategy. It may
1035 * explode. Instead, generate the required EGL error here.
1037 if (!dri2_dpy
->robustness
) {
1038 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1042 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1043 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1052 * Called via eglCreateContext(), drv->API.CreateContext().
1054 static _EGLContext
*
1055 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1056 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1058 struct dri2_egl_context
*dri2_ctx
;
1059 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1060 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1061 __DRIcontext
*shared
=
1062 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1063 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1064 const __DRIconfig
*dri_config
;
1069 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1071 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1075 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1078 switch (dri2_ctx
->base
.ClientAPI
) {
1079 case EGL_OPENGL_ES_API
:
1080 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1082 api
= __DRI_API_GLES
;
1085 api
= __DRI_API_GLES2
;
1088 api
= __DRI_API_GLES3
;
1091 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1096 case EGL_OPENGL_API
:
1097 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1098 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1099 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1100 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1101 api
= __DRI_API_OPENGL_CORE
;
1103 api
= __DRI_API_OPENGL
;
1106 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1112 /* The config chosen here isn't necessarily
1113 * used for surfaces later.
1114 * A pixmap surface will use the single config.
1115 * This opportunity depends on disabling the
1116 * doubleBufferMode check in
1117 * src/mesa/main/context.c:check_compatible()
1119 if (dri2_config
->dri_double_config
[0])
1120 dri_config
= dri2_config
->dri_double_config
[0];
1122 dri_config
= dri2_config
->dri_single_config
[0];
1124 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
1125 * makes sure the back buffer will always be used.
1127 if (conf
->SurfaceType
& EGL_WINDOW_BIT
)
1128 dri2_ctx
->base
.WindowRenderBuffer
= EGL_BACK_BUFFER
;
1133 if (dri2_dpy
->image_driver
) {
1135 unsigned num_attribs
= 8;
1136 uint32_t ctx_attribs
[8];
1138 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1142 dri2_ctx
->dri_context
=
1143 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1151 dri2_create_context_attribs_error(error
);
1152 } else if (dri2_dpy
->dri2
) {
1153 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1155 unsigned num_attribs
= 8;
1156 uint32_t ctx_attribs
[8];
1158 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1162 dri2_ctx
->dri_context
=
1163 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1171 dri2_create_context_attribs_error(error
);
1173 dri2_ctx
->dri_context
=
1174 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1181 assert(dri2_dpy
->swrast
);
1182 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1184 unsigned num_attribs
= 8;
1185 uint32_t ctx_attribs
[8];
1187 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1191 dri2_ctx
->dri_context
=
1192 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1200 dri2_create_context_attribs_error(error
);
1202 dri2_ctx
->dri_context
=
1203 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1211 if (!dri2_ctx
->dri_context
)
1214 return &dri2_ctx
->base
;
1222 * Called via eglDestroyContext(), drv->API.DestroyContext().
1225 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1227 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1228 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1230 if (_eglPutContext(ctx
)) {
1231 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1239 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1242 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1243 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1245 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1246 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1247 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1248 _EGLContext
*old_ctx
;
1249 _EGLSurface
*old_dsurf
, *old_rsurf
;
1250 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1251 __DRIdrawable
*ddraw
, *rdraw
;
1256 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1258 /* make new bindings */
1259 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1260 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1264 /* flush before context switch */
1265 if (old_ctx
&& dri2_drv
->glFlush
)
1266 dri2_drv
->glFlush();
1268 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1269 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1270 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1273 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1274 dri2_dpy
->core
->unbindContext(old_cctx
);
1277 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1279 if (unbind
|| dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1281 drv
->API
.DestroySurface(drv
, disp
, old_dsurf
);
1283 drv
->API
.DestroySurface(drv
, disp
, old_rsurf
);
1286 dri2_dpy
->ref_count
++;
1288 EGLDisplay old_disp
= _eglGetDisplayHandle(old_ctx
->Resource
.Display
);
1289 drv
->API
.DestroyContext(drv
, disp
, old_ctx
);
1290 dri2_display_release(old_disp
);
1295 /* undo the previous _eglBindContext */
1296 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1297 assert(&dri2_ctx
->base
== ctx
&&
1298 tmp_dsurf
== dsurf
&&
1299 tmp_rsurf
== rsurf
);
1301 _eglPutSurface(dsurf
);
1302 _eglPutSurface(rsurf
);
1303 _eglPutContext(ctx
);
1305 _eglPutSurface(old_dsurf
);
1306 _eglPutSurface(old_rsurf
);
1307 _eglPutContext(old_ctx
);
1309 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1310 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1313 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1318 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1320 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1322 return dri2_surf
->dri_drawable
;
1326 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1329 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1331 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1333 return dri2_drv
->get_proc_address(procname
);
1337 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1338 _EGLConfig
*conf
, void *native_window
,
1339 const EGLint
*attrib_list
)
1341 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1342 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1347 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1348 _EGLConfig
*conf
, void *native_pixmap
,
1349 const EGLint
*attrib_list
)
1351 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1352 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1357 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1358 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1360 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1361 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
1365 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1367 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1368 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1372 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1375 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1376 return dri2_dpy
->vtbl
->swap_interval(drv
, dpy
, surf
, interval
);
1380 * Asks the client API to flush any rendering to the drawable so that we can
1381 * do our swapbuffers.
1384 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1386 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1387 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1389 if (dri2_dpy
->flush
) {
1390 if (dri2_dpy
->flush
->base
.version
>= 4) {
1391 /* We know there's a current context because:
1393 * "If surface is not bound to the calling thread’s current
1394 * context, an EGL_BAD_SURFACE error is generated."
1396 _EGLContext
*ctx
= _eglGetCurrentContext();
1397 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1399 /* From the EGL 1.4 spec (page 52):
1401 * "The contents of ancillary buffers are always undefined
1402 * after calling eglSwapBuffers."
1404 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1406 __DRI2_FLUSH_DRAWABLE
|
1407 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1408 __DRI2_THROTTLE_SWAPBUFFER
);
1410 dri2_dpy
->flush
->flush(dri_drawable
);
1416 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1418 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1419 return dri2_dpy
->vtbl
->swap_buffers(drv
, dpy
, surf
);
1423 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1425 const EGLint
*rects
, EGLint n_rects
)
1427 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1428 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, dpy
, surf
,
1433 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1434 EGLint numRects
, const EGLint
*rects
)
1436 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1437 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, dpy
, surf
, numRects
, rects
);
1441 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1442 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1444 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1445 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, dpy
, surf
, x
, y
, width
, height
);
1449 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1450 void *native_pixmap_target
)
1452 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1453 return dri2_dpy
->vtbl
->copy_buffers(drv
, dpy
, surf
, native_pixmap_target
);
1457 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1459 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1460 return dri2_dpy
->vtbl
->query_buffer_age(drv
, dpy
, surf
);
1464 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1466 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1467 _EGLSurface
*surf
= ctx
->DrawSurface
;
1468 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1472 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1473 * we need to copy fake to real here.*/
1475 if (dri2_dpy
->flush
!= NULL
)
1476 dri2_dpy
->flush
->flush(dri_drawable
);
1482 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1487 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1488 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1495 dri2_bind_tex_image(_EGLDriver
*drv
,
1496 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1498 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1499 struct dri2_egl_context
*dri2_ctx
;
1501 GLint format
, target
;
1502 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1504 ctx
= _eglGetCurrentContext();
1505 dri2_ctx
= dri2_egl_context(ctx
);
1507 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1510 switch (surf
->TextureFormat
) {
1511 case EGL_TEXTURE_RGB
:
1512 format
= __DRI_TEXTURE_FORMAT_RGB
;
1514 case EGL_TEXTURE_RGBA
:
1515 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1518 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1519 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1522 switch (surf
->TextureTarget
) {
1523 case EGL_TEXTURE_2D
:
1524 target
= GL_TEXTURE_2D
;
1527 target
= GL_TEXTURE_2D
;
1528 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1531 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1539 dri2_release_tex_image(_EGLDriver
*drv
,
1540 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1542 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1543 struct dri2_egl_context
*dri2_ctx
;
1546 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1548 ctx
= _eglGetCurrentContext();
1549 dri2_ctx
= dri2_egl_context(ctx
);
1551 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1554 switch (surf
->TextureTarget
) {
1555 case EGL_TEXTURE_2D
:
1556 target
= GL_TEXTURE_2D
;
1562 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1563 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1564 (*dri2_dpy
->tex_buffer
->releaseTexBuffer
)(dri2_ctx
->dri_context
,
1573 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
1574 EGLenum target
, EGLClientBuffer buffer
,
1575 const EGLint
*attr_list
)
1577 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1578 return dri2_dpy
->vtbl
->create_image(drv
, dpy
, ctx
, target
, buffer
,
1583 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1585 struct dri2_egl_image
*dri2_img
;
1587 if (dri_image
== NULL
) {
1588 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1592 dri2_img
= malloc(sizeof *dri2_img
);
1594 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1598 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1603 dri2_img
->dri_image
= dri_image
;
1605 return &dri2_img
->base
;
1609 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1610 EGLClientBuffer buffer
,
1611 const EGLint
*attr_list
)
1613 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1614 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1615 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1616 __DRIimage
*dri_image
;
1618 if (renderbuffer
== 0) {
1619 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1620 return EGL_NO_IMAGE_KHR
;
1624 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1625 renderbuffer
, NULL
);
1627 return dri2_create_image_from_dri(disp
, dri_image
);
1630 #ifdef HAVE_WAYLAND_PLATFORM
1632 /* This structure describes how a wl_buffer maps to one or more
1633 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1634 * offsets and strides of the planes in the buffer. This table maps a
1635 * wl_drm format code to a description of the planes in the buffer
1636 * that lets us create a __DRIimage for each of the planes. */
1638 static const struct wl_drm_components_descriptor
{
1639 uint32_t dri_components
;
1642 } wl_drm_components
[] = {
1643 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1644 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1645 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1646 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1647 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1651 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1652 EGLClientBuffer _buffer
,
1653 const EGLint
*attr_list
)
1655 struct wl_drm_buffer
*buffer
;
1656 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1657 const struct wl_drm_components_descriptor
*f
;
1658 __DRIimage
*dri_image
;
1659 _EGLImageAttribs attrs
;
1663 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1664 (struct wl_resource
*) _buffer
);
1668 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1669 plane
= attrs
.PlaneWL
;
1670 if (err
!= EGL_SUCCESS
) {
1671 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1675 f
= buffer
->driver_format
;
1676 if (plane
< 0 || plane
>= f
->nplanes
) {
1677 _eglError(EGL_BAD_PARAMETER
,
1678 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1682 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1684 if (dri_image
== NULL
) {
1685 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1689 return dri2_create_image_from_dri(disp
, dri_image
);
1694 dri2_get_sync_values_chromium(_EGLDisplay
*dpy
, _EGLSurface
*surf
,
1695 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1698 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1699 return dri2_dpy
->vtbl
->get_sync_values(dpy
, surf
, ust
, msc
, sbc
);
1703 * Set the error code after a call to
1704 * dri2_egl_image::dri_image::createImageFromTexture.
1707 dri2_create_image_khr_texture_error(int dri_error
)
1711 switch (dri_error
) {
1712 case __DRI_IMAGE_ERROR_SUCCESS
:
1715 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1716 egl_error
= EGL_BAD_ALLOC
;
1719 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1720 egl_error
= EGL_BAD_MATCH
;
1723 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1724 egl_error
= EGL_BAD_PARAMETER
;
1727 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
1728 egl_error
= EGL_BAD_ACCESS
;
1733 egl_error
= EGL_BAD_MATCH
;
1737 _eglError(egl_error
, "dri2_create_image_khr_texture");
1741 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1743 EGLClientBuffer buffer
,
1744 const EGLint
*attr_list
)
1746 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1747 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1748 struct dri2_egl_image
*dri2_img
;
1749 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
1750 _EGLImageAttribs attrs
;
1756 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1757 return EGL_NO_IMAGE_KHR
;
1760 if (_eglParseImageAttribList(&attrs
, disp
, attr_list
) != EGL_SUCCESS
)
1761 return EGL_NO_IMAGE_KHR
;
1764 case EGL_GL_TEXTURE_2D_KHR
:
1766 gl_target
= GL_TEXTURE_2D
;
1768 case EGL_GL_TEXTURE_3D_KHR
:
1769 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1770 depth
= attrs
.GLTextureZOffset
;
1771 gl_target
= GL_TEXTURE_3D
;
1775 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1776 return EGL_NO_IMAGE_KHR
;
1778 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1779 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1780 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1781 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1782 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1783 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1784 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
1785 gl_target
= GL_TEXTURE_CUBE_MAP
;
1788 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1789 return EGL_NO_IMAGE_KHR
;
1792 dri2_img
= malloc(sizeof *dri2_img
);
1794 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1795 return EGL_NO_IMAGE_KHR
;
1798 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1799 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1801 return EGL_NO_IMAGE_KHR
;
1804 dri2_img
->dri_image
=
1805 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
1809 attrs
.GLTextureLevel
,
1812 dri2_create_image_khr_texture_error(error
);
1814 if (!dri2_img
->dri_image
) {
1816 return EGL_NO_IMAGE_KHR
;
1818 return &dri2_img
->base
;
1822 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1823 EGLint attribute
, EGLint
*value
)
1825 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1826 if (!dri2_dpy
->vtbl
->query_surface
)
1827 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
1828 return dri2_dpy
->vtbl
->query_surface(drv
, dpy
, surf
, attribute
, value
);
1831 static struct wl_buffer
*
1832 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1835 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1836 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, dpy
, img
);
1841 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1842 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1844 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1845 EGLint format
, name
, pitch
, err
;
1846 _EGLImageAttribs attrs
;
1847 __DRIimage
*dri_image
;
1849 name
= (EGLint
) (uintptr_t) buffer
;
1851 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1852 if (err
!= EGL_SUCCESS
)
1855 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
1856 attrs
.DRMBufferStrideMESA
<= 0) {
1857 _eglError(EGL_BAD_PARAMETER
,
1858 "bad width, height or stride");
1862 switch (attrs
.DRMBufferFormatMESA
) {
1863 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1864 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1865 pitch
= attrs
.DRMBufferStrideMESA
;
1868 _eglError(EGL_BAD_PARAMETER
,
1869 "dri2_create_image_khr: unsupported pixmap depth");
1874 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1882 return dri2_create_image_from_dri(disp
, dri_image
);
1886 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
1893 * "Required attributes and their values are as follows:
1895 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1897 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1898 * by drm_fourcc.h and used as the pixel_format parameter of the
1899 * drm_mode_fb_cmd2 ioctl."
1903 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1904 * incomplete, EGL_BAD_PARAMETER is generated."
1906 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
1907 !attrs
->DMABufFourCC
.IsPresent
) {
1908 _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
1915 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1916 * specified for a plane's pitch or offset isn't supported by EGL,
1917 * EGL_BAD_ACCESS is generated."
1919 for (i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
1920 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
1921 attrs
->DMABufPlanePitches
[i
].Value
<= 0) {
1922 _eglError(EGL_BAD_ACCESS
, "invalid pitch");
1930 /* Returns the total number of file descriptors. Zero indicates an error. */
1932 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
1934 unsigned i
, plane_n
;
1936 switch (attrs
->DMABufFourCC
.Value
) {
1938 case DRM_FORMAT_RG88
:
1939 case DRM_FORMAT_GR88
:
1940 case DRM_FORMAT_RGB332
:
1941 case DRM_FORMAT_BGR233
:
1942 case DRM_FORMAT_XRGB4444
:
1943 case DRM_FORMAT_XBGR4444
:
1944 case DRM_FORMAT_RGBX4444
:
1945 case DRM_FORMAT_BGRX4444
:
1946 case DRM_FORMAT_ARGB4444
:
1947 case DRM_FORMAT_ABGR4444
:
1948 case DRM_FORMAT_RGBA4444
:
1949 case DRM_FORMAT_BGRA4444
:
1950 case DRM_FORMAT_XRGB1555
:
1951 case DRM_FORMAT_XBGR1555
:
1952 case DRM_FORMAT_RGBX5551
:
1953 case DRM_FORMAT_BGRX5551
:
1954 case DRM_FORMAT_ARGB1555
:
1955 case DRM_FORMAT_ABGR1555
:
1956 case DRM_FORMAT_RGBA5551
:
1957 case DRM_FORMAT_BGRA5551
:
1958 case DRM_FORMAT_RGB565
:
1959 case DRM_FORMAT_BGR565
:
1960 case DRM_FORMAT_RGB888
:
1961 case DRM_FORMAT_BGR888
:
1962 case DRM_FORMAT_XRGB8888
:
1963 case DRM_FORMAT_XBGR8888
:
1964 case DRM_FORMAT_RGBX8888
:
1965 case DRM_FORMAT_BGRX8888
:
1966 case DRM_FORMAT_ARGB8888
:
1967 case DRM_FORMAT_ABGR8888
:
1968 case DRM_FORMAT_RGBA8888
:
1969 case DRM_FORMAT_BGRA8888
:
1970 case DRM_FORMAT_XRGB2101010
:
1971 case DRM_FORMAT_XBGR2101010
:
1972 case DRM_FORMAT_RGBX1010102
:
1973 case DRM_FORMAT_BGRX1010102
:
1974 case DRM_FORMAT_ARGB2101010
:
1975 case DRM_FORMAT_ABGR2101010
:
1976 case DRM_FORMAT_RGBA1010102
:
1977 case DRM_FORMAT_BGRA1010102
:
1978 case DRM_FORMAT_YUYV
:
1979 case DRM_FORMAT_YVYU
:
1980 case DRM_FORMAT_UYVY
:
1981 case DRM_FORMAT_VYUY
:
1984 case DRM_FORMAT_NV12
:
1985 case DRM_FORMAT_NV21
:
1986 case DRM_FORMAT_NV16
:
1987 case DRM_FORMAT_NV61
:
1990 case DRM_FORMAT_YUV410
:
1991 case DRM_FORMAT_YVU410
:
1992 case DRM_FORMAT_YUV411
:
1993 case DRM_FORMAT_YVU411
:
1994 case DRM_FORMAT_YUV420
:
1995 case DRM_FORMAT_YVU420
:
1996 case DRM_FORMAT_YUV422
:
1997 case DRM_FORMAT_YVU422
:
1998 case DRM_FORMAT_YUV444
:
1999 case DRM_FORMAT_YVU444
:
2003 _eglError(EGL_BAD_ATTRIBUTE
, "invalid format");
2010 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2011 * incomplete, EGL_BAD_PARAMETER is generated."
2013 for (i
= 0; i
< plane_n
; ++i
) {
2014 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2015 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2016 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2017 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2023 * The spec also says:
2025 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2026 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2027 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2028 * attributes are specified."
2030 for (i
= plane_n
; i
< 3; ++i
) {
2031 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2032 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2033 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2034 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2045 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2046 * EGL will take a reference to the dma_buf(s) which it will release at any
2047 * time while the EGLDisplay is initialized. It is the responsibility of the
2048 * application to close the dma_buf file descriptors."
2050 * Therefore we must never close or otherwise modify the file descriptors.
2053 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2054 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2056 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2059 _EGLImageAttribs attrs
;
2060 __DRIimage
*dri_image
;
2071 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2072 * error EGL_BAD_PARAMETER is generated."
2074 if (buffer
!= NULL
) {
2075 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2079 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2080 if (err
!= EGL_SUCCESS
) {
2081 _eglError(err
, "bad attribute");
2085 if (!dri2_check_dma_buf_attribs(&attrs
))
2088 num_fds
= dri2_check_dma_buf_format(&attrs
);
2092 for (i
= 0; i
< num_fds
; ++i
) {
2093 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2094 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2095 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2099 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2100 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2101 fds
, num_fds
, pitches
, offsets
,
2102 attrs
.DMABufYuvColorSpaceHint
.Value
,
2103 attrs
.DMABufSampleRangeHint
.Value
,
2104 attrs
.DMABufChromaHorizontalSiting
.Value
,
2105 attrs
.DMABufChromaVerticalSiting
.Value
,
2108 dri2_create_image_khr_texture_error(error
);
2111 return EGL_NO_IMAGE_KHR
;
2113 res
= dri2_create_image_from_dri(disp
, dri_image
);
2118 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2119 const EGLint
*attr_list
)
2121 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2122 struct dri2_egl_image
*dri2_img
;
2123 _EGLImageAttribs attrs
;
2124 unsigned int dri_use
, valid_mask
;
2126 EGLint err
= EGL_SUCCESS
;
2130 dri2_img
= malloc(sizeof *dri2_img
);
2132 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2133 return EGL_NO_IMAGE_KHR
;
2137 err
= EGL_BAD_PARAMETER
;
2141 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
2142 err
= EGL_BAD_PARAMETER
;
2146 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2147 if (err
!= EGL_SUCCESS
)
2150 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2151 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)",
2152 attrs
.Width
, attrs
.Height
);
2156 switch (attrs
.DRMBufferFormatMESA
) {
2157 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2158 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2161 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x",
2162 attrs
.DRMBufferFormatMESA
);
2167 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2168 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2169 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2170 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2171 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x",
2172 attrs
.DRMBufferUseMESA
& ~valid_mask
);
2177 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2178 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2179 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2180 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2181 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2182 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2184 dri2_img
->dri_image
=
2185 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2186 attrs
.Width
, attrs
.Height
,
2187 format
, dri_use
, dri2_img
);
2188 if (dri2_img
->dri_image
== NULL
) {
2189 err
= EGL_BAD_ALLOC
;
2193 return &dri2_img
->base
;
2197 _eglError(err
, "dri2_create_drm_image_mesa");
2199 return EGL_NO_IMAGE_KHR
;
2203 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2204 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2206 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2207 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2211 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2212 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
2213 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2218 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2219 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2222 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2223 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2229 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2231 EGLint
*fourcc
, EGLint
*nplanes
,
2232 EGLuint64KHR
*modifiers
)
2234 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2235 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2241 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2242 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2244 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2245 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2254 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2255 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2257 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2258 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2262 /* rework later to provide multiple fds/strides/offsets */
2264 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2265 __DRI_IMAGE_ATTRIB_FD
, fds
);
2268 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2269 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2273 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2274 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2276 offsets
[0] = img_offset
;
2287 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2288 _EGLContext
*ctx
, EGLenum target
,
2289 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2294 case EGL_GL_TEXTURE_2D_KHR
:
2295 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2296 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2297 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2298 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2299 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2300 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2301 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2302 case EGL_GL_TEXTURE_3D_KHR
:
2303 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
2304 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2307 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2308 return EGL_NO_IMAGE_KHR
;
2310 case EGL_GL_RENDERBUFFER_KHR
:
2311 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2313 case EGL_DRM_BUFFER_MESA
:
2314 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2315 case EGL_LINUX_DMA_BUF_EXT
:
2316 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2318 #ifdef HAVE_WAYLAND_PLATFORM
2319 case EGL_WAYLAND_BUFFER_WL
:
2320 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2323 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2324 return EGL_NO_IMAGE_KHR
;
2329 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2331 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2332 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2336 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2342 #ifdef HAVE_WAYLAND_PLATFORM
2345 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2346 struct wl_drm_buffer
*buffer
)
2348 _EGLDisplay
*disp
= user_data
;
2349 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2351 int i
, dri_components
= 0;
2354 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2363 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2375 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2377 buffer
->driver_format
= NULL
;
2378 for (i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2379 if (wl_drm_components
[i
].dri_components
== dri_components
)
2380 buffer
->driver_format
= &wl_drm_components
[i
];
2382 if (buffer
->driver_format
== NULL
)
2383 dri2_dpy
->image
->destroyImage(img
);
2385 buffer
->driver_buffer
= img
;
2389 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2391 _EGLDisplay
*disp
= user_data
;
2392 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2394 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2397 static struct wayland_drm_callbacks wl_drm_callbacks
= {
2398 .authenticate
= NULL
,
2399 .reference_buffer
= dri2_wl_reference_buffer
,
2400 .release_buffer
= dri2_wl_release_buffer
2404 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2405 struct wl_display
*wl_dpy
)
2407 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2413 if (dri2_dpy
->wl_server_drm
)
2416 wl_drm_callbacks
.authenticate
=
2417 (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
;
2419 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2420 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2421 dri2_dpy
->image
->base
.version
>= 7 &&
2422 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2423 flags
|= WAYLAND_DRM_PRIME
;
2425 dri2_dpy
->wl_server_drm
=
2426 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2427 &wl_drm_callbacks
, disp
, flags
);
2429 if (!dri2_dpy
->wl_server_drm
)
2432 #ifdef HAVE_DRM_PLATFORM
2433 /* We have to share the wl_drm instance with gbm, so gbm can convert
2434 * wl_buffers to gbm bos. */
2435 if (dri2_dpy
->gbm_dri
)
2436 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2443 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2444 struct wl_display
*wl_dpy
)
2446 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2450 if (!dri2_dpy
->wl_server_drm
)
2453 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2454 dri2_dpy
->wl_server_drm
= NULL
;
2460 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2461 struct wl_resource
*buffer_resource
,
2462 EGLint attribute
, EGLint
*value
)
2464 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2465 struct wl_drm_buffer
*buffer
;
2466 const struct wl_drm_components_descriptor
*format
;
2468 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2472 format
= buffer
->driver_format
;
2473 switch (attribute
) {
2474 case EGL_TEXTURE_FORMAT
:
2475 *value
= format
->components
;
2478 *value
= buffer
->width
;
2481 *value
= buffer
->height
;
2490 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2492 p_atomic_inc(&sync
->refcount
);
2496 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2497 struct dri2_egl_sync
*dri2_sync
)
2499 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2500 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
)
2501 cnd_destroy(&dri2_sync
->cond
);
2503 if (dri2_sync
->fence
)
2504 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2511 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
2512 EGLenum type
, const EGLAttrib
*attrib_list
)
2514 _EGLContext
*ctx
= _eglGetCurrentContext();
2515 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2516 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2517 struct dri2_egl_sync
*dri2_sync
;
2519 pthread_condattr_t attr
;
2521 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2523 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2527 if (!_eglInitSync(&dri2_sync
->base
, dpy
, type
, attrib_list
)) {
2533 case EGL_SYNC_FENCE_KHR
:
2534 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2535 if (!dri2_sync
->fence
) {
2536 /* Why did it fail? DRI doesn't return an error code, so we emit
2537 * a generic EGL error that doesn't communicate user error.
2539 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2545 case EGL_SYNC_CL_EVENT_KHR
:
2546 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2547 dri2_dpy
->dri_screen
,
2548 dri2_sync
->base
.CLEvent
);
2549 /* this can only happen if the cl_event passed in is invalid. */
2550 if (!dri2_sync
->fence
) {
2551 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2556 /* the initial status must be "signaled" if the cl_event is signaled */
2557 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2558 dri2_sync
->fence
, 0, 0))
2559 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2562 case EGL_SYNC_REUSABLE_KHR
:
2563 /* intialize attr */
2564 ret
= pthread_condattr_init(&attr
);
2567 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2572 /* change clock attribute to CLOCK_MONOTONIC */
2573 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
2576 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2581 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
2584 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2589 /* initial status of reusable sync must be "unsignaled" */
2590 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
2594 p_atomic_set(&dri2_sync
->refcount
, 1);
2595 return &dri2_sync
->base
;
2599 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2601 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2602 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2603 EGLint ret
= EGL_TRUE
;
2606 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
2607 * then unlock all threads possibly blocked by the reusable sync before
2610 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
2611 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2612 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2613 /* unblock all threads currently blocked by sync */
2614 err
= cnd_broadcast(&dri2_sync
->cond
);
2617 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
2621 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2627 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2628 EGLint flags
, EGLTime timeout
)
2630 _EGLContext
*ctx
= _eglGetCurrentContext();
2631 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2632 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2633 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2634 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2635 unsigned wait_flags
= 0;
2637 /* timespecs for cnd_timedwait */
2638 struct timespec current
;
2641 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
2643 /* The EGL_KHR_fence_sync spec states:
2645 * "If no context is current for the bound API,
2646 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
2648 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
2649 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
2651 /* the sync object should take a reference while waiting */
2652 dri2_egl_ref_sync(dri2_sync
);
2654 switch (sync
->Type
) {
2655 case EGL_SYNC_FENCE_KHR
:
2656 case EGL_SYNC_CL_EVENT_KHR
:
2657 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
2658 dri2_sync
->fence
, wait_flags
,
2660 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2662 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2665 case EGL_SYNC_REUSABLE_KHR
:
2666 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
2667 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
2668 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
2669 if (dri2_drv
->glFlush
)
2670 dri2_drv
->glFlush();
2673 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
2674 if (timeout
== EGL_FOREVER_KHR
) {
2675 mtx_lock(&dri2_sync
->mutex
);
2676 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
2677 mtx_unlock(&dri2_sync
->mutex
);
2679 /* if reusable sync has not been yet signaled */
2680 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
2681 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
2683 /* calculating when to expire */
2684 expire
.nsec
= timeout
% 1000000000L;
2685 expire
.sec
= timeout
/ 1000000000L;
2687 expire
.nsec
+= current
.tv_nsec
;
2688 expire
.sec
+= current
.tv_sec
;
2690 /* expire.nsec now is a number between 0 and 1999999998 */
2691 if (expire
.nsec
> 999999999L) {
2693 expire
.nsec
-= 1000000000L;
2696 mtx_lock(&dri2_sync
->mutex
);
2697 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
2698 mtx_unlock(&dri2_sync
->mutex
);
2700 if (ret
== thrd_busy
) {
2701 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2702 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2704 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2712 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2718 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2721 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2724 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
) {
2725 _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
2729 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
) {
2730 _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
2734 dri2_sync
->base
.SyncStatus
= mode
;
2736 if (mode
== EGL_SIGNALED_KHR
) {
2737 ret
= cnd_broadcast(&dri2_sync
->cond
);
2739 /* fail to broadcast */
2741 _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
2750 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2752 _EGLContext
*ctx
= _eglGetCurrentContext();
2753 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2754 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2755 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2757 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
2758 dri2_sync
->fence
, 0);
2763 dri2_interop_query_device_info(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2764 struct mesa_glinterop_device_info
*out
)
2766 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2767 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2769 if (!dri2_dpy
->interop
)
2770 return MESA_GLINTEROP_UNSUPPORTED
;
2772 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
2776 dri2_interop_export_object(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2777 struct mesa_glinterop_export_in
*in
,
2778 struct mesa_glinterop_export_out
*out
)
2780 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2781 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2783 if (!dri2_dpy
->interop
)
2784 return MESA_GLINTEROP_UNSUPPORTED
;
2786 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
2790 dri2_unload(_EGLDriver
*drv
)
2792 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2794 if (dri2_drv
->handle
)
2795 dlclose(dri2_drv
->handle
);
2800 dri2_load(_EGLDriver
*drv
)
2802 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2803 #ifdef HAVE_ANDROID_PLATFORM
2804 const char *libname
= "libglapi.so";
2805 #elif defined(__APPLE__)
2806 const char *libname
= "libglapi.0.dylib";
2807 #elif defined(__CYGWIN__)
2808 const char *libname
= "cygglapi-0.dll";
2810 const char *libname
= "libglapi.so.0";
2814 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2815 handle
= dlopen(libname
, RTLD_LAZY
| RTLD_GLOBAL
);
2817 dri2_drv
->get_proc_address
= (_EGLProc (*)(const char *))
2818 dlsym(handle
, "_glapi_get_proc_address");
2819 if (!dri2_drv
->get_proc_address
|| !libname
) {
2820 /* no need to keep a reference */
2826 /* if glapi is not available, loading DRI drivers will fail */
2827 if (!dri2_drv
->get_proc_address
) {
2828 _eglLog(_EGL_WARNING
, "DRI2: failed to find _glapi_get_proc_address");
2832 dri2_drv
->glFlush
= (void (*)(void))
2833 dri2_drv
->get_proc_address("glFlush");
2835 dri2_drv
->handle
= handle
;
2841 * This is the main entrypoint into the driver, called by libEGL.
2842 * Create a new _EGLDriver object and init its dispatch table.
2845 _eglBuiltInDriverDRI2(const char *args
)
2847 struct dri2_egl_driver
*dri2_drv
;
2851 dri2_drv
= calloc(1, sizeof *dri2_drv
);
2855 if (!dri2_load(&dri2_drv
->base
)) {
2860 _eglInitDriverFallbacks(&dri2_drv
->base
);
2861 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
2862 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
2863 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
2864 dri2_drv
->base
.API
.DestroyContext
= dri2_destroy_context
;
2865 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
2866 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
2867 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
2868 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
2869 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
2870 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
2871 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
2872 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
2873 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
2874 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
2875 dri2_drv
->base
.API
.SwapInterval
= dri2_swap_interval
;
2876 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
2877 dri2_drv
->base
.API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
2878 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
2879 dri2_drv
->base
.API
.PostSubBufferNV
= dri2_post_sub_buffer
;
2880 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
,
2881 dri2_drv
->base
.API
.QueryBufferAge
= dri2_query_buffer_age
;
2882 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image
;
2883 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
2884 dri2_drv
->base
.API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
2885 dri2_drv
->base
.API
.QuerySurface
= dri2_query_surface
;
2887 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
2888 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
2889 dri2_drv
->base
.API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
2890 dri2_drv
->base
.API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
2892 #ifdef HAVE_WAYLAND_PLATFORM
2893 dri2_drv
->base
.API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
2894 dri2_drv
->base
.API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
2895 dri2_drv
->base
.API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
2897 dri2_drv
->base
.API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
2898 dri2_drv
->base
.API
.CreateSyncKHR
= dri2_create_sync
;
2899 dri2_drv
->base
.API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
2900 dri2_drv
->base
.API
.SignalSyncKHR
= dri2_signal_sync
;
2901 dri2_drv
->base
.API
.WaitSyncKHR
= dri2_server_wait_sync
;
2902 dri2_drv
->base
.API
.DestroySyncKHR
= dri2_destroy_sync
;
2903 dri2_drv
->base
.API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
2904 dri2_drv
->base
.API
.GLInteropExportObject
= dri2_interop_export_object
;
2906 dri2_drv
->base
.Name
= "DRI2";
2907 dri2_drv
->base
.Unload
= dri2_unload
;
2909 return &dri2_drv
->base
;