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
)
854 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
857 assert(dri2_dpy
->ref_count
> 0);
858 dri2_dpy
->ref_count
--;
860 if (dri2_dpy
->ref_count
> 0)
863 _eglCleanupDisplay(disp
);
865 if (dri2_dpy
->own_dri_screen
)
866 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
867 if (dri2_dpy
->fd
>= 0)
869 if (dri2_dpy
->driver
)
870 dlclose(dri2_dpy
->driver
);
871 free(dri2_dpy
->driver_name
);
873 #ifdef HAVE_WAYLAND_PLATFORM
874 free(dri2_dpy
->device_name
);
877 switch (disp
->Platform
) {
878 #ifdef HAVE_X11_PLATFORM
879 case _EGL_PLATFORM_X11
:
880 if (dri2_dpy
->own_device
) {
881 xcb_disconnect(dri2_dpy
->conn
);
885 #ifdef HAVE_DRM_PLATFORM
886 case _EGL_PLATFORM_DRM
:
887 if (dri2_dpy
->own_device
) {
888 gbm_device_destroy(&dri2_dpy
->gbm_dri
->base
.base
);
892 #ifdef HAVE_WAYLAND_PLATFORM
893 case _EGL_PLATFORM_WAYLAND
:
894 if (dri2_dpy
->wl_drm
)
895 wl_drm_destroy(dri2_dpy
->wl_drm
);
896 if (dri2_dpy
->wl_shm
)
897 wl_shm_destroy(dri2_dpy
->wl_shm
);
898 wl_registry_destroy(dri2_dpy
->wl_registry
);
899 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
900 if (dri2_dpy
->own_device
) {
901 wl_display_disconnect(dri2_dpy
->wl_dpy
);
909 /* The drm platform does not create the screen/driver_configs but reuses
910 * the ones from the gbm device. As such the gbm itself is responsible
913 if (disp
->Platform
!= _EGL_PLATFORM_DRM
) {
914 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
915 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
916 free(dri2_dpy
->driver_configs
);
919 disp
->DriverData
= NULL
;
923 * Called via eglTerminate(), drv->API.Terminate().
925 * This must be guaranteed to be called exactly once, even if eglTerminate is
926 * called many times (without a eglInitialize in between).
929 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
931 /* Release all non-current Context/Surfaces. */
932 _eglReleaseDisplayResources(drv
, disp
);
934 dri2_display_release(disp
);
940 * Set the error code after a call to
941 * dri2_egl_display::dri2::createContextAttribs.
944 dri2_create_context_attribs_error(int dri_error
)
949 case __DRI_CTX_ERROR_SUCCESS
:
952 case __DRI_CTX_ERROR_NO_MEMORY
:
953 egl_error
= EGL_BAD_ALLOC
;
956 /* From the EGL_KHR_create_context spec, section "Errors":
958 * * If <config> does not support a client API context compatible
959 * with the requested API major and minor version, [...] context flags,
960 * and context reset notification behavior (for client API types where
961 * these attributes are supported), then an EGL_BAD_MATCH error is
964 * * If an OpenGL ES context is requested and the values for
965 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
966 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
967 * is not defined, than an EGL_BAD_MATCH error is generated.
969 * * If an OpenGL context is requested, the requested version is
970 * greater than 3.2, and the value for attribute
971 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
972 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
973 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
974 * one of these bits set; or if the implementation does not support
975 * the requested profile, then an EGL_BAD_MATCH error is generated.
977 case __DRI_CTX_ERROR_BAD_API
:
978 case __DRI_CTX_ERROR_BAD_VERSION
:
979 case __DRI_CTX_ERROR_BAD_FLAG
:
980 egl_error
= EGL_BAD_MATCH
;
983 /* From the EGL_KHR_create_context spec, section "Errors":
985 * * If an attribute name or attribute value in <attrib_list> is not
986 * recognized (including unrecognized bits in bitmask attributes),
987 * then an EGL_BAD_ATTRIBUTE error is generated."
989 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
990 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
991 egl_error
= EGL_BAD_ATTRIBUTE
;
996 egl_error
= EGL_BAD_MATCH
;
1000 _eglError(egl_error
, "dri2_create_context");
1004 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1005 struct dri2_egl_display
*dri2_dpy
,
1006 uint32_t *ctx_attribs
,
1007 unsigned *num_attribs
)
1011 assert(*num_attribs
>= 8);
1013 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1014 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1015 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1016 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1018 if (dri2_ctx
->base
.Flags
!= 0) {
1019 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1020 * extension, don't even try to send it the robust-access flag.
1021 * It may explode. Instead, generate the required EGL error here.
1023 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1024 && !dri2_dpy
->robustness
) {
1025 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1029 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1030 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
;
1033 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1034 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1035 * extension, don't even try to send it a reset strategy. It may
1036 * explode. Instead, generate the required EGL error here.
1038 if (!dri2_dpy
->robustness
) {
1039 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1043 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1044 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1053 * Called via eglCreateContext(), drv->API.CreateContext().
1055 static _EGLContext
*
1056 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1057 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1059 struct dri2_egl_context
*dri2_ctx
;
1060 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1061 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1062 __DRIcontext
*shared
=
1063 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1064 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1065 const __DRIconfig
*dri_config
;
1070 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1072 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1076 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1079 switch (dri2_ctx
->base
.ClientAPI
) {
1080 case EGL_OPENGL_ES_API
:
1081 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1083 api
= __DRI_API_GLES
;
1086 api
= __DRI_API_GLES2
;
1089 api
= __DRI_API_GLES3
;
1092 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1097 case EGL_OPENGL_API
:
1098 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1099 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1100 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1101 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1102 api
= __DRI_API_OPENGL_CORE
;
1104 api
= __DRI_API_OPENGL
;
1107 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1113 /* The config chosen here isn't necessarily
1114 * used for surfaces later.
1115 * A pixmap surface will use the single config.
1116 * This opportunity depends on disabling the
1117 * doubleBufferMode check in
1118 * src/mesa/main/context.c:check_compatible()
1120 if (dri2_config
->dri_double_config
[0])
1121 dri_config
= dri2_config
->dri_double_config
[0];
1123 dri_config
= dri2_config
->dri_single_config
[0];
1125 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
1126 * makes sure the back buffer will always be used.
1128 if (conf
->SurfaceType
& EGL_WINDOW_BIT
)
1129 dri2_ctx
->base
.WindowRenderBuffer
= EGL_BACK_BUFFER
;
1134 if (dri2_dpy
->image_driver
) {
1136 unsigned num_attribs
= 8;
1137 uint32_t ctx_attribs
[8];
1139 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1143 dri2_ctx
->dri_context
=
1144 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1152 dri2_create_context_attribs_error(error
);
1153 } else if (dri2_dpy
->dri2
) {
1154 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1156 unsigned num_attribs
= 8;
1157 uint32_t ctx_attribs
[8];
1159 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1163 dri2_ctx
->dri_context
=
1164 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1172 dri2_create_context_attribs_error(error
);
1174 dri2_ctx
->dri_context
=
1175 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1182 assert(dri2_dpy
->swrast
);
1183 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1185 unsigned num_attribs
= 8;
1186 uint32_t ctx_attribs
[8];
1188 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1192 dri2_ctx
->dri_context
=
1193 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1201 dri2_create_context_attribs_error(error
);
1203 dri2_ctx
->dri_context
=
1204 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1212 if (!dri2_ctx
->dri_context
)
1215 return &dri2_ctx
->base
;
1223 * Called via eglDestroyContext(), drv->API.DestroyContext().
1226 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1228 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1229 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1231 if (_eglPutContext(ctx
)) {
1232 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1240 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1243 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1244 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1246 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1247 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1248 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1249 _EGLContext
*old_ctx
;
1250 _EGLSurface
*old_dsurf
, *old_rsurf
;
1251 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1252 __DRIdrawable
*ddraw
, *rdraw
;
1257 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1259 /* make new bindings */
1260 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1261 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1265 /* flush before context switch */
1267 dri2_drv
->glFlush();
1269 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1270 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1271 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1274 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1275 dri2_dpy
->core
->unbindContext(old_cctx
);
1278 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1280 if (unbind
|| dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1282 drv
->API
.DestroySurface(drv
, disp
, old_dsurf
);
1284 drv
->API
.DestroySurface(drv
, disp
, old_rsurf
);
1287 dri2_dpy
->ref_count
++;
1289 EGLDisplay old_disp
= _eglGetDisplayHandle(old_ctx
->Resource
.Display
);
1290 drv
->API
.DestroyContext(drv
, disp
, old_ctx
);
1291 dri2_display_release(old_disp
);
1296 /* undo the previous _eglBindContext */
1297 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1298 assert(&dri2_ctx
->base
== ctx
&&
1299 tmp_dsurf
== dsurf
&&
1300 tmp_rsurf
== rsurf
);
1302 _eglPutSurface(dsurf
);
1303 _eglPutSurface(rsurf
);
1304 _eglPutContext(ctx
);
1306 _eglPutSurface(old_dsurf
);
1307 _eglPutSurface(old_rsurf
);
1308 _eglPutContext(old_ctx
);
1310 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1311 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1314 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1319 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1321 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1323 return dri2_surf
->dri_drawable
;
1327 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1330 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1332 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1334 return dri2_drv
->get_proc_address(procname
);
1338 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1339 _EGLConfig
*conf
, void *native_window
,
1340 const EGLint
*attrib_list
)
1342 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1343 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1348 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1349 _EGLConfig
*conf
, void *native_pixmap
,
1350 const EGLint
*attrib_list
)
1352 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1353 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1358 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1359 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1361 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1362 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
1366 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1368 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1370 if (!_eglPutSurface(surf
))
1373 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1377 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1380 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1381 return dri2_dpy
->vtbl
->swap_interval(drv
, dpy
, surf
, interval
);
1385 * Asks the client API to flush any rendering to the drawable so that we can
1386 * do our swapbuffers.
1389 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1391 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1392 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1394 if (dri2_dpy
->flush
) {
1395 if (dri2_dpy
->flush
->base
.version
>= 4) {
1396 /* We know there's a current context because:
1398 * "If surface is not bound to the calling thread’s current
1399 * context, an EGL_BAD_SURFACE error is generated."
1401 _EGLContext
*ctx
= _eglGetCurrentContext();
1402 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1404 /* From the EGL 1.4 spec (page 52):
1406 * "The contents of ancillary buffers are always undefined
1407 * after calling eglSwapBuffers."
1409 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1411 __DRI2_FLUSH_DRAWABLE
|
1412 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1413 __DRI2_THROTTLE_SWAPBUFFER
);
1415 dri2_dpy
->flush
->flush(dri_drawable
);
1421 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1423 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1424 return dri2_dpy
->vtbl
->swap_buffers(drv
, dpy
, surf
);
1428 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1430 const EGLint
*rects
, EGLint n_rects
)
1432 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1433 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, dpy
, surf
,
1438 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1439 EGLint numRects
, const EGLint
*rects
)
1441 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1442 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, dpy
, surf
, numRects
, rects
);
1446 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1447 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1449 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1450 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, dpy
, surf
, x
, y
, width
, height
);
1454 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1455 void *native_pixmap_target
)
1457 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1458 return dri2_dpy
->vtbl
->copy_buffers(drv
, dpy
, surf
, native_pixmap_target
);
1462 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1464 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1465 return dri2_dpy
->vtbl
->query_buffer_age(drv
, dpy
, surf
);
1469 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1471 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1472 _EGLSurface
*surf
= ctx
->DrawSurface
;
1473 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1477 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1478 * we need to copy fake to real here.*/
1480 if (dri2_dpy
->flush
!= NULL
)
1481 dri2_dpy
->flush
->flush(dri_drawable
);
1487 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1492 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1493 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1500 dri2_bind_tex_image(_EGLDriver
*drv
,
1501 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1503 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1504 struct dri2_egl_context
*dri2_ctx
;
1506 GLint format
, target
;
1507 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1509 ctx
= _eglGetCurrentContext();
1510 dri2_ctx
= dri2_egl_context(ctx
);
1512 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1515 switch (surf
->TextureFormat
) {
1516 case EGL_TEXTURE_RGB
:
1517 format
= __DRI_TEXTURE_FORMAT_RGB
;
1519 case EGL_TEXTURE_RGBA
:
1520 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1523 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1524 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1527 switch (surf
->TextureTarget
) {
1528 case EGL_TEXTURE_2D
:
1529 target
= GL_TEXTURE_2D
;
1532 target
= GL_TEXTURE_2D
;
1533 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1536 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1544 dri2_release_tex_image(_EGLDriver
*drv
,
1545 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1547 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1548 struct dri2_egl_context
*dri2_ctx
;
1551 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1553 ctx
= _eglGetCurrentContext();
1554 dri2_ctx
= dri2_egl_context(ctx
);
1556 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1559 switch (surf
->TextureTarget
) {
1560 case EGL_TEXTURE_2D
:
1561 target
= GL_TEXTURE_2D
;
1567 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1568 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1569 (*dri2_dpy
->tex_buffer
->releaseTexBuffer
)(dri2_ctx
->dri_context
,
1578 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
1579 EGLenum target
, EGLClientBuffer buffer
,
1580 const EGLint
*attr_list
)
1582 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1583 return dri2_dpy
->vtbl
->create_image(drv
, dpy
, ctx
, target
, buffer
,
1588 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1590 struct dri2_egl_image
*dri2_img
;
1592 if (dri_image
== NULL
) {
1593 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1597 dri2_img
= malloc(sizeof *dri2_img
);
1599 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1603 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1608 dri2_img
->dri_image
= dri_image
;
1610 return &dri2_img
->base
;
1614 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1615 EGLClientBuffer buffer
,
1616 const EGLint
*attr_list
)
1618 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1619 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1620 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1621 __DRIimage
*dri_image
;
1623 if (renderbuffer
== 0) {
1624 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1625 return EGL_NO_IMAGE_KHR
;
1629 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1630 renderbuffer
, NULL
);
1632 return dri2_create_image_from_dri(disp
, dri_image
);
1635 #ifdef HAVE_WAYLAND_PLATFORM
1637 /* This structure describes how a wl_buffer maps to one or more
1638 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1639 * offsets and strides of the planes in the buffer. This table maps a
1640 * wl_drm format code to a description of the planes in the buffer
1641 * that lets us create a __DRIimage for each of the planes. */
1643 static const struct wl_drm_components_descriptor
{
1644 uint32_t dri_components
;
1647 } wl_drm_components
[] = {
1648 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1649 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1650 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1651 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1652 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1656 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1657 EGLClientBuffer _buffer
,
1658 const EGLint
*attr_list
)
1660 struct wl_drm_buffer
*buffer
;
1661 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1662 const struct wl_drm_components_descriptor
*f
;
1663 __DRIimage
*dri_image
;
1664 _EGLImageAttribs attrs
;
1668 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1669 (struct wl_resource
*) _buffer
);
1673 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1674 plane
= attrs
.PlaneWL
;
1675 if (err
!= EGL_SUCCESS
) {
1676 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1680 f
= buffer
->driver_format
;
1681 if (plane
< 0 || plane
>= f
->nplanes
) {
1682 _eglError(EGL_BAD_PARAMETER
,
1683 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1687 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1689 if (dri_image
== NULL
) {
1690 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1694 return dri2_create_image_from_dri(disp
, dri_image
);
1699 dri2_get_sync_values_chromium(_EGLDisplay
*dpy
, _EGLSurface
*surf
,
1700 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1703 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1704 return dri2_dpy
->vtbl
->get_sync_values(dpy
, surf
, ust
, msc
, sbc
);
1708 * Set the error code after a call to
1709 * dri2_egl_image::dri_image::createImageFromTexture.
1712 dri2_create_image_khr_texture_error(int dri_error
)
1716 switch (dri_error
) {
1717 case __DRI_IMAGE_ERROR_SUCCESS
:
1720 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1721 egl_error
= EGL_BAD_ALLOC
;
1724 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1725 egl_error
= EGL_BAD_MATCH
;
1728 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1729 egl_error
= EGL_BAD_PARAMETER
;
1732 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
1733 egl_error
= EGL_BAD_ACCESS
;
1738 egl_error
= EGL_BAD_MATCH
;
1742 _eglError(egl_error
, "dri2_create_image_khr_texture");
1746 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1748 EGLClientBuffer buffer
,
1749 const EGLint
*attr_list
)
1751 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1752 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1753 struct dri2_egl_image
*dri2_img
;
1754 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
1755 _EGLImageAttribs attrs
;
1761 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1762 return EGL_NO_IMAGE_KHR
;
1765 if (_eglParseImageAttribList(&attrs
, disp
, attr_list
) != EGL_SUCCESS
)
1766 return EGL_NO_IMAGE_KHR
;
1769 case EGL_GL_TEXTURE_2D_KHR
:
1771 gl_target
= GL_TEXTURE_2D
;
1773 case EGL_GL_TEXTURE_3D_KHR
:
1774 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1775 depth
= attrs
.GLTextureZOffset
;
1776 gl_target
= GL_TEXTURE_3D
;
1780 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1781 return EGL_NO_IMAGE_KHR
;
1783 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1784 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1785 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1786 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1787 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1788 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1789 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
1790 gl_target
= GL_TEXTURE_CUBE_MAP
;
1793 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1794 return EGL_NO_IMAGE_KHR
;
1797 dri2_img
= malloc(sizeof *dri2_img
);
1799 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1800 return EGL_NO_IMAGE_KHR
;
1803 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1804 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1806 return EGL_NO_IMAGE_KHR
;
1809 dri2_img
->dri_image
=
1810 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
1814 attrs
.GLTextureLevel
,
1817 dri2_create_image_khr_texture_error(error
);
1819 if (!dri2_img
->dri_image
) {
1821 return EGL_NO_IMAGE_KHR
;
1823 return &dri2_img
->base
;
1827 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1828 EGLint attribute
, EGLint
*value
)
1830 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1831 if (!dri2_dpy
->vtbl
->query_surface
)
1832 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
1833 return dri2_dpy
->vtbl
->query_surface(drv
, dpy
, surf
, attribute
, value
);
1836 static struct wl_buffer
*
1837 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1840 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1841 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, dpy
, img
);
1846 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1847 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1849 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1850 EGLint format
, name
, pitch
, err
;
1851 _EGLImageAttribs attrs
;
1852 __DRIimage
*dri_image
;
1854 name
= (EGLint
) (uintptr_t) buffer
;
1856 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1857 if (err
!= EGL_SUCCESS
)
1860 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
1861 attrs
.DRMBufferStrideMESA
<= 0) {
1862 _eglError(EGL_BAD_PARAMETER
,
1863 "bad width, height or stride");
1867 switch (attrs
.DRMBufferFormatMESA
) {
1868 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1869 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1870 pitch
= attrs
.DRMBufferStrideMESA
;
1873 _eglError(EGL_BAD_PARAMETER
,
1874 "dri2_create_image_khr: unsupported pixmap depth");
1879 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1887 return dri2_create_image_from_dri(disp
, dri_image
);
1891 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
1898 * "Required attributes and their values are as follows:
1900 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1902 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1903 * by drm_fourcc.h and used as the pixel_format parameter of the
1904 * drm_mode_fb_cmd2 ioctl."
1908 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1909 * incomplete, EGL_BAD_PARAMETER is generated."
1911 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
1912 !attrs
->DMABufFourCC
.IsPresent
) {
1913 _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
1920 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1921 * specified for a plane's pitch or offset isn't supported by EGL,
1922 * EGL_BAD_ACCESS is generated."
1924 for (i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
1925 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
1926 attrs
->DMABufPlanePitches
[i
].Value
<= 0) {
1927 _eglError(EGL_BAD_ACCESS
, "invalid pitch");
1935 /* Returns the total number of file descriptors. Zero indicates an error. */
1937 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
1939 unsigned i
, plane_n
;
1941 switch (attrs
->DMABufFourCC
.Value
) {
1943 case DRM_FORMAT_RG88
:
1944 case DRM_FORMAT_GR88
:
1945 case DRM_FORMAT_RGB332
:
1946 case DRM_FORMAT_BGR233
:
1947 case DRM_FORMAT_XRGB4444
:
1948 case DRM_FORMAT_XBGR4444
:
1949 case DRM_FORMAT_RGBX4444
:
1950 case DRM_FORMAT_BGRX4444
:
1951 case DRM_FORMAT_ARGB4444
:
1952 case DRM_FORMAT_ABGR4444
:
1953 case DRM_FORMAT_RGBA4444
:
1954 case DRM_FORMAT_BGRA4444
:
1955 case DRM_FORMAT_XRGB1555
:
1956 case DRM_FORMAT_XBGR1555
:
1957 case DRM_FORMAT_RGBX5551
:
1958 case DRM_FORMAT_BGRX5551
:
1959 case DRM_FORMAT_ARGB1555
:
1960 case DRM_FORMAT_ABGR1555
:
1961 case DRM_FORMAT_RGBA5551
:
1962 case DRM_FORMAT_BGRA5551
:
1963 case DRM_FORMAT_RGB565
:
1964 case DRM_FORMAT_BGR565
:
1965 case DRM_FORMAT_RGB888
:
1966 case DRM_FORMAT_BGR888
:
1967 case DRM_FORMAT_XRGB8888
:
1968 case DRM_FORMAT_XBGR8888
:
1969 case DRM_FORMAT_RGBX8888
:
1970 case DRM_FORMAT_BGRX8888
:
1971 case DRM_FORMAT_ARGB8888
:
1972 case DRM_FORMAT_ABGR8888
:
1973 case DRM_FORMAT_RGBA8888
:
1974 case DRM_FORMAT_BGRA8888
:
1975 case DRM_FORMAT_XRGB2101010
:
1976 case DRM_FORMAT_XBGR2101010
:
1977 case DRM_FORMAT_RGBX1010102
:
1978 case DRM_FORMAT_BGRX1010102
:
1979 case DRM_FORMAT_ARGB2101010
:
1980 case DRM_FORMAT_ABGR2101010
:
1981 case DRM_FORMAT_RGBA1010102
:
1982 case DRM_FORMAT_BGRA1010102
:
1983 case DRM_FORMAT_YUYV
:
1984 case DRM_FORMAT_YVYU
:
1985 case DRM_FORMAT_UYVY
:
1986 case DRM_FORMAT_VYUY
:
1989 case DRM_FORMAT_NV12
:
1990 case DRM_FORMAT_NV21
:
1991 case DRM_FORMAT_NV16
:
1992 case DRM_FORMAT_NV61
:
1995 case DRM_FORMAT_YUV410
:
1996 case DRM_FORMAT_YVU410
:
1997 case DRM_FORMAT_YUV411
:
1998 case DRM_FORMAT_YVU411
:
1999 case DRM_FORMAT_YUV420
:
2000 case DRM_FORMAT_YVU420
:
2001 case DRM_FORMAT_YUV422
:
2002 case DRM_FORMAT_YVU422
:
2003 case DRM_FORMAT_YUV444
:
2004 case DRM_FORMAT_YVU444
:
2008 _eglError(EGL_BAD_ATTRIBUTE
, "invalid format");
2015 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2016 * incomplete, EGL_BAD_PARAMETER is generated."
2018 for (i
= 0; i
< plane_n
; ++i
) {
2019 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2020 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2021 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2022 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2028 * The spec also says:
2030 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2031 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2032 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2033 * attributes are specified."
2035 for (i
= plane_n
; i
< 3; ++i
) {
2036 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2037 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2038 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2039 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2050 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2051 * EGL will take a reference to the dma_buf(s) which it will release at any
2052 * time while the EGLDisplay is initialized. It is the responsibility of the
2053 * application to close the dma_buf file descriptors."
2055 * Therefore we must never close or otherwise modify the file descriptors.
2058 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2059 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2061 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2064 _EGLImageAttribs attrs
;
2065 __DRIimage
*dri_image
;
2076 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2077 * error EGL_BAD_PARAMETER is generated."
2079 if (buffer
!= NULL
) {
2080 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2084 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2085 if (err
!= EGL_SUCCESS
) {
2086 _eglError(err
, "bad attribute");
2090 if (!dri2_check_dma_buf_attribs(&attrs
))
2093 num_fds
= dri2_check_dma_buf_format(&attrs
);
2097 for (i
= 0; i
< num_fds
; ++i
) {
2098 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2099 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2100 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2104 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2105 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2106 fds
, num_fds
, pitches
, offsets
,
2107 attrs
.DMABufYuvColorSpaceHint
.Value
,
2108 attrs
.DMABufSampleRangeHint
.Value
,
2109 attrs
.DMABufChromaHorizontalSiting
.Value
,
2110 attrs
.DMABufChromaVerticalSiting
.Value
,
2113 dri2_create_image_khr_texture_error(error
);
2116 return EGL_NO_IMAGE_KHR
;
2118 res
= dri2_create_image_from_dri(disp
, dri_image
);
2123 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2124 const EGLint
*attr_list
)
2126 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2127 struct dri2_egl_image
*dri2_img
;
2128 _EGLImageAttribs attrs
;
2129 unsigned int dri_use
, valid_mask
;
2131 EGLint err
= EGL_SUCCESS
;
2135 dri2_img
= malloc(sizeof *dri2_img
);
2137 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2138 return EGL_NO_IMAGE_KHR
;
2142 err
= EGL_BAD_PARAMETER
;
2146 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
2147 err
= EGL_BAD_PARAMETER
;
2151 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2152 if (err
!= EGL_SUCCESS
)
2155 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2156 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)",
2157 attrs
.Width
, attrs
.Height
);
2161 switch (attrs
.DRMBufferFormatMESA
) {
2162 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2163 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2166 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x",
2167 attrs
.DRMBufferFormatMESA
);
2172 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2173 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2174 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2175 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2176 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x",
2177 attrs
.DRMBufferUseMESA
& ~valid_mask
);
2182 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2183 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2184 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2185 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2186 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2187 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2189 dri2_img
->dri_image
=
2190 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2191 attrs
.Width
, attrs
.Height
,
2192 format
, dri_use
, dri2_img
);
2193 if (dri2_img
->dri_image
== NULL
) {
2194 err
= EGL_BAD_ALLOC
;
2198 return &dri2_img
->base
;
2202 _eglError(err
, "dri2_create_drm_image_mesa");
2204 return EGL_NO_IMAGE_KHR
;
2208 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2209 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2211 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2212 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2216 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2217 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
2218 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2223 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2224 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2227 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2228 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2234 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2236 EGLint
*fourcc
, EGLint
*nplanes
,
2237 EGLuint64KHR
*modifiers
)
2239 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2240 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2246 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2247 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2249 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2250 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2259 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2260 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2262 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2263 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2267 /* rework later to provide multiple fds/strides/offsets */
2269 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2270 __DRI_IMAGE_ATTRIB_FD
, fds
);
2273 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2274 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2278 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2279 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2281 offsets
[0] = img_offset
;
2292 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2293 _EGLContext
*ctx
, EGLenum target
,
2294 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2299 case EGL_GL_TEXTURE_2D_KHR
:
2300 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2301 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2302 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2303 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2304 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2305 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2306 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2307 case EGL_GL_TEXTURE_3D_KHR
:
2308 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
2309 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2312 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2313 return EGL_NO_IMAGE_KHR
;
2315 case EGL_GL_RENDERBUFFER_KHR
:
2316 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2318 case EGL_DRM_BUFFER_MESA
:
2319 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2320 case EGL_LINUX_DMA_BUF_EXT
:
2321 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2323 #ifdef HAVE_WAYLAND_PLATFORM
2324 case EGL_WAYLAND_BUFFER_WL
:
2325 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2328 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2329 return EGL_NO_IMAGE_KHR
;
2334 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2336 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2337 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2341 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2347 #ifdef HAVE_WAYLAND_PLATFORM
2350 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2351 struct wl_drm_buffer
*buffer
)
2353 _EGLDisplay
*disp
= user_data
;
2354 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2356 int i
, dri_components
= 0;
2359 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2368 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2380 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2382 buffer
->driver_format
= NULL
;
2383 for (i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2384 if (wl_drm_components
[i
].dri_components
== dri_components
)
2385 buffer
->driver_format
= &wl_drm_components
[i
];
2387 if (buffer
->driver_format
== NULL
)
2388 dri2_dpy
->image
->destroyImage(img
);
2390 buffer
->driver_buffer
= img
;
2394 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2396 _EGLDisplay
*disp
= user_data
;
2397 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2399 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2402 static struct wayland_drm_callbacks wl_drm_callbacks
= {
2403 .authenticate
= NULL
,
2404 .reference_buffer
= dri2_wl_reference_buffer
,
2405 .release_buffer
= dri2_wl_release_buffer
2409 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2410 struct wl_display
*wl_dpy
)
2412 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2418 if (dri2_dpy
->wl_server_drm
)
2421 wl_drm_callbacks
.authenticate
=
2422 (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
;
2424 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2425 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2426 dri2_dpy
->image
->base
.version
>= 7 &&
2427 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2428 flags
|= WAYLAND_DRM_PRIME
;
2430 dri2_dpy
->wl_server_drm
=
2431 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2432 &wl_drm_callbacks
, disp
, flags
);
2434 if (!dri2_dpy
->wl_server_drm
)
2437 #ifdef HAVE_DRM_PLATFORM
2438 /* We have to share the wl_drm instance with gbm, so gbm can convert
2439 * wl_buffers to gbm bos. */
2440 if (dri2_dpy
->gbm_dri
)
2441 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2448 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2449 struct wl_display
*wl_dpy
)
2451 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2455 if (!dri2_dpy
->wl_server_drm
)
2458 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2459 dri2_dpy
->wl_server_drm
= NULL
;
2465 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2466 struct wl_resource
*buffer_resource
,
2467 EGLint attribute
, EGLint
*value
)
2469 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2470 struct wl_drm_buffer
*buffer
;
2471 const struct wl_drm_components_descriptor
*format
;
2473 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2477 format
= buffer
->driver_format
;
2478 switch (attribute
) {
2479 case EGL_TEXTURE_FORMAT
:
2480 *value
= format
->components
;
2483 *value
= buffer
->width
;
2486 *value
= buffer
->height
;
2495 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2497 p_atomic_inc(&sync
->refcount
);
2501 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2502 struct dri2_egl_sync
*dri2_sync
)
2504 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2505 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
)
2506 cnd_destroy(&dri2_sync
->cond
);
2508 if (dri2_sync
->fence
)
2509 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2516 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
2517 EGLenum type
, const EGLAttrib
*attrib_list
)
2519 _EGLContext
*ctx
= _eglGetCurrentContext();
2520 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2521 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2522 struct dri2_egl_sync
*dri2_sync
;
2524 pthread_condattr_t attr
;
2526 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2528 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2532 if (!_eglInitSync(&dri2_sync
->base
, dpy
, type
, attrib_list
)) {
2538 case EGL_SYNC_FENCE_KHR
:
2539 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2540 if (!dri2_sync
->fence
) {
2541 /* Why did it fail? DRI doesn't return an error code, so we emit
2542 * a generic EGL error that doesn't communicate user error.
2544 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2550 case EGL_SYNC_CL_EVENT_KHR
:
2551 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2552 dri2_dpy
->dri_screen
,
2553 dri2_sync
->base
.CLEvent
);
2554 /* this can only happen if the cl_event passed in is invalid. */
2555 if (!dri2_sync
->fence
) {
2556 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2561 /* the initial status must be "signaled" if the cl_event is signaled */
2562 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2563 dri2_sync
->fence
, 0, 0))
2564 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2567 case EGL_SYNC_REUSABLE_KHR
:
2568 /* intialize attr */
2569 ret
= pthread_condattr_init(&attr
);
2572 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2577 /* change clock attribute to CLOCK_MONOTONIC */
2578 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
2581 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2586 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
2589 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2594 /* initial status of reusable sync must be "unsignaled" */
2595 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
2599 p_atomic_set(&dri2_sync
->refcount
, 1);
2600 return &dri2_sync
->base
;
2604 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2606 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2607 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2608 EGLint ret
= EGL_TRUE
;
2611 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
2612 * then unlock all threads possibly blocked by the reusable sync before
2615 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
2616 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2617 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2618 /* unblock all threads currently blocked by sync */
2619 err
= cnd_broadcast(&dri2_sync
->cond
);
2622 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
2626 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2632 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2633 EGLint flags
, EGLTime timeout
)
2635 _EGLContext
*ctx
= _eglGetCurrentContext();
2636 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2637 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2638 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2639 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2640 unsigned wait_flags
= 0;
2642 /* timespecs for cnd_timedwait */
2643 struct timespec current
;
2646 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
2648 /* The EGL_KHR_fence_sync spec states:
2650 * "If no context is current for the bound API,
2651 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
2653 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
2654 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
2656 /* the sync object should take a reference while waiting */
2657 dri2_egl_ref_sync(dri2_sync
);
2659 switch (sync
->Type
) {
2660 case EGL_SYNC_FENCE_KHR
:
2661 case EGL_SYNC_CL_EVENT_KHR
:
2662 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
2663 dri2_sync
->fence
, wait_flags
,
2665 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2667 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2670 case EGL_SYNC_REUSABLE_KHR
:
2671 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
2672 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
2673 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
2674 dri2_drv
->glFlush();
2677 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
2678 if (timeout
== EGL_FOREVER_KHR
) {
2679 mtx_lock(&dri2_sync
->mutex
);
2680 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
2681 mtx_unlock(&dri2_sync
->mutex
);
2683 /* if reusable sync has not been yet signaled */
2684 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
2685 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
2687 /* calculating when to expire */
2688 expire
.nsec
= timeout
% 1000000000L;
2689 expire
.sec
= timeout
/ 1000000000L;
2691 expire
.nsec
+= current
.tv_nsec
;
2692 expire
.sec
+= current
.tv_sec
;
2694 /* expire.nsec now is a number between 0 and 1999999998 */
2695 if (expire
.nsec
> 999999999L) {
2697 expire
.nsec
-= 1000000000L;
2700 mtx_lock(&dri2_sync
->mutex
);
2701 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
2702 mtx_unlock(&dri2_sync
->mutex
);
2704 if (ret
== thrd_busy
) {
2705 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2706 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2708 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2716 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2722 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2725 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2728 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
) {
2729 _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
2733 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
) {
2734 _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
2738 dri2_sync
->base
.SyncStatus
= mode
;
2740 if (mode
== EGL_SIGNALED_KHR
) {
2741 ret
= cnd_broadcast(&dri2_sync
->cond
);
2743 /* fail to broadcast */
2745 _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
2754 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2756 _EGLContext
*ctx
= _eglGetCurrentContext();
2757 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2758 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2759 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2761 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
2762 dri2_sync
->fence
, 0);
2767 dri2_interop_query_device_info(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2768 struct mesa_glinterop_device_info
*out
)
2770 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2771 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2773 if (!dri2_dpy
->interop
)
2774 return MESA_GLINTEROP_UNSUPPORTED
;
2776 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
2780 dri2_interop_export_object(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2781 struct mesa_glinterop_export_in
*in
,
2782 struct mesa_glinterop_export_out
*out
)
2784 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2785 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2787 if (!dri2_dpy
->interop
)
2788 return MESA_GLINTEROP_UNSUPPORTED
;
2790 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
2794 dri2_unload(_EGLDriver
*drv
)
2796 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2798 dlclose(dri2_drv
->handle
);
2803 dri2_load(_EGLDriver
*drv
)
2805 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2806 #ifdef HAVE_ANDROID_PLATFORM
2807 const char *libname
= "libglapi.so";
2808 #elif defined(__APPLE__)
2809 const char *libname
= "libglapi.0.dylib";
2810 #elif defined(__CYGWIN__)
2811 const char *libname
= "cygglapi-0.dll";
2812 #elif defined(__linux__)
2813 const char *libname
= "libglapi.so.0";
2815 #error Unknown glapi provider for this platform
2819 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2820 handle
= dlopen(libname
, RTLD_LAZY
| RTLD_GLOBAL
);
2822 _eglLog(_EGL_WARNING
, "DRI2: failed to open glapi provider");
2826 dri2_drv
->get_proc_address
= (_EGLProc (*)(const char *))
2827 dlsym(handle
, "_glapi_get_proc_address");
2829 /* if glapi is not available, loading DRI drivers will fail */
2830 if (!dri2_drv
->get_proc_address
) {
2831 _eglLog(_EGL_WARNING
, "DRI2: failed to find _glapi_get_proc_address");
2835 dri2_drv
->glFlush
= (void (*)(void))
2836 dri2_drv
->get_proc_address("glFlush");
2838 /* if glFlush is not available things are horribly broken */
2839 if (!dri2_drv
->glFlush
) {
2840 _eglLog(_EGL_WARNING
, "DRI2: failed to find glFlush entry point");
2844 dri2_drv
->handle
= handle
;
2854 * This is the main entrypoint into the driver, called by libEGL.
2855 * Create a new _EGLDriver object and init its dispatch table.
2858 _eglBuiltInDriverDRI2(const char *args
)
2860 struct dri2_egl_driver
*dri2_drv
;
2864 dri2_drv
= calloc(1, sizeof *dri2_drv
);
2868 if (!dri2_load(&dri2_drv
->base
)) {
2873 _eglInitDriverFallbacks(&dri2_drv
->base
);
2874 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
2875 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
2876 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
2877 dri2_drv
->base
.API
.DestroyContext
= dri2_destroy_context
;
2878 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
2879 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
2880 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
2881 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
2882 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
2883 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
2884 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
2885 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
2886 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
2887 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
2888 dri2_drv
->base
.API
.SwapInterval
= dri2_swap_interval
;
2889 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
2890 dri2_drv
->base
.API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
2891 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
2892 dri2_drv
->base
.API
.PostSubBufferNV
= dri2_post_sub_buffer
;
2893 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
,
2894 dri2_drv
->base
.API
.QueryBufferAge
= dri2_query_buffer_age
;
2895 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image
;
2896 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
2897 dri2_drv
->base
.API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
2898 dri2_drv
->base
.API
.QuerySurface
= dri2_query_surface
;
2900 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
2901 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
2902 dri2_drv
->base
.API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
2903 dri2_drv
->base
.API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
2905 #ifdef HAVE_WAYLAND_PLATFORM
2906 dri2_drv
->base
.API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
2907 dri2_drv
->base
.API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
2908 dri2_drv
->base
.API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
2910 dri2_drv
->base
.API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
2911 dri2_drv
->base
.API
.CreateSyncKHR
= dri2_create_sync
;
2912 dri2_drv
->base
.API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
2913 dri2_drv
->base
.API
.SignalSyncKHR
= dri2_signal_sync
;
2914 dri2_drv
->base
.API
.WaitSyncKHR
= dri2_server_wait_sync
;
2915 dri2_drv
->base
.API
.DestroySyncKHR
= dri2_destroy_sync
;
2916 dri2_drv
->base
.API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
2917 dri2_drv
->base
.API
.GLInteropExportObject
= dri2_interop_export_object
;
2919 dri2_drv
->base
.Name
= "DRI2";
2920 dri2_drv
->base
.Unload
= dri2_unload
;
2922 return &dri2_drv
->base
;