2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Kristian Høgsberg <krh@bitplanet.net>
28 #define WL_HIDE_DEPRECATED
41 #include <c11/threads.h>
45 #include <drm_fourcc.h>
48 #include <GL/internal/dri_interface.h>
49 #include "GL/mesa_glinterop.h"
50 #include <sys/types.h>
53 #ifdef HAVE_WAYLAND_PLATFORM
54 #include "wayland-drm.h"
55 #include "wayland-drm-client-protocol.h"
59 #include "util/u_atomic.h"
61 /* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
62 * some of the definitions here so that building Mesa won't bleeding-edge
66 #define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
69 #ifndef DRM_FORMAT_RG88
70 #define DRM_FORMAT_RG88 fourcc_code('R', 'G', '8', '8') /* [15:0] R:G 8:8 little endian */
73 #ifndef DRM_FORMAT_GR88
74 #define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
77 const __DRIuseInvalidateExtension use_invalidate
= {
78 .base
= { __DRI_USE_INVALIDATE
, 1 }
81 EGLint dri2_to_egl_attribute_map
[] = {
83 EGL_BUFFER_SIZE
, /* __DRI_ATTRIB_BUFFER_SIZE */
84 EGL_LEVEL
, /* __DRI_ATTRIB_LEVEL */
85 EGL_RED_SIZE
, /* __DRI_ATTRIB_RED_SIZE */
86 EGL_GREEN_SIZE
, /* __DRI_ATTRIB_GREEN_SIZE */
87 EGL_BLUE_SIZE
, /* __DRI_ATTRIB_BLUE_SIZE */
88 EGL_LUMINANCE_SIZE
, /* __DRI_ATTRIB_LUMINANCE_SIZE */
89 EGL_ALPHA_SIZE
, /* __DRI_ATTRIB_ALPHA_SIZE */
90 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
91 EGL_DEPTH_SIZE
, /* __DRI_ATTRIB_DEPTH_SIZE */
92 EGL_STENCIL_SIZE
, /* __DRI_ATTRIB_STENCIL_SIZE */
93 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
94 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
95 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
96 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
97 EGL_SAMPLE_BUFFERS
, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
98 EGL_SAMPLES
, /* __DRI_ATTRIB_SAMPLES */
99 0, /* __DRI_ATTRIB_RENDER_TYPE, */
100 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
101 0, /* __DRI_ATTRIB_CONFORMANT */
102 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
103 0, /* __DRI_ATTRIB_STEREO */
104 0, /* __DRI_ATTRIB_AUX_BUFFERS */
105 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
106 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
107 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
108 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
109 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
110 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
111 0, /* __DRI_ATTRIB_FLOAT_MODE (deprecated) */
112 0, /* __DRI_ATTRIB_RED_MASK */
113 0, /* __DRI_ATTRIB_GREEN_MASK */
114 0, /* __DRI_ATTRIB_BLUE_MASK */
115 0, /* __DRI_ATTRIB_ALPHA_MASK */
116 EGL_MAX_PBUFFER_WIDTH
, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
117 EGL_MAX_PBUFFER_HEIGHT
, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
118 EGL_MAX_PBUFFER_PIXELS
, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
119 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
120 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
121 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
122 0, /* __DRI_ATTRIB_SWAP_METHOD */
123 EGL_MAX_SWAP_INTERVAL
, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
124 EGL_MIN_SWAP_INTERVAL
, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
125 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
126 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
127 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
128 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
129 EGL_Y_INVERTED_NOK
, /* __DRI_ATTRIB_YINVERTED */
130 0, /* __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE */
134 dri2_get_dri_config(struct dri2_egl_config
*conf
, EGLint surface_type
,
137 const bool srgb
= colorspace
== EGL_GL_COLORSPACE_SRGB_KHR
;
139 return surface_type
== EGL_WINDOW_BIT
? conf
->dri_double_config
[srgb
] :
140 conf
->dri_single_config
[srgb
];
144 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
146 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
149 if (!_eglMatchConfig(conf
, criteria
))
155 struct dri2_egl_config
*
156 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
157 EGLint surface_type
, const EGLint
*attr_list
,
158 const unsigned int *rgba_masks
)
160 struct dri2_egl_config
*conf
;
161 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
163 unsigned int attrib
, value
, double_buffer
;
165 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
166 unsigned int dri_masks
[4] = { 0, 0, 0, 0 };
167 _EGLConfig
*matching_config
;
168 EGLint num_configs
= 0;
172 _eglInitConfig(&base
, disp
, id
);
176 bind_to_texture_rgb
= 0;
177 bind_to_texture_rgba
= 0;
179 while (dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
++, &attrib
, &value
)) {
181 case __DRI_ATTRIB_RENDER_TYPE
:
182 if (value
& __DRI_ATTRIB_RGBA_BIT
)
183 value
= EGL_RGB_BUFFER
;
184 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
185 value
= EGL_LUMINANCE_BUFFER
;
188 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
191 case __DRI_ATTRIB_CONFIG_CAVEAT
:
192 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
193 value
= EGL_NON_CONFORMANT_CONFIG
;
194 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
195 value
= EGL_SLOW_CONFIG
;
198 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
201 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
202 bind_to_texture_rgb
= value
;
205 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
206 bind_to_texture_rgba
= value
;
209 case __DRI_ATTRIB_DOUBLE_BUFFER
:
210 double_buffer
= value
;
213 case __DRI_ATTRIB_RED_MASK
:
214 dri_masks
[0] = value
;
217 case __DRI_ATTRIB_GREEN_MASK
:
218 dri_masks
[1] = value
;
221 case __DRI_ATTRIB_BLUE_MASK
:
222 dri_masks
[2] = value
;
225 case __DRI_ATTRIB_ALPHA_MASK
:
226 dri_masks
[3] = value
;
229 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
230 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
231 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
232 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
233 /* Don't expose visuals with the accumulation buffer. */
238 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
240 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
245 key
= dri2_to_egl_attribute_map
[attrib
];
247 _eglSetConfigKey(&base
, key
, value
);
253 for (i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
254 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
256 if (rgba_masks
&& memcmp(rgba_masks
, dri_masks
, sizeof(dri_masks
)))
259 base
.NativeRenderable
= EGL_TRUE
;
261 base
.SurfaceType
= surface_type
;
262 if (surface_type
& (EGL_PBUFFER_BIT
|
263 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
264 base
.BindToTextureRGB
= bind_to_texture_rgb
;
265 if (base
.AlphaSize
> 0)
266 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
269 base
.RenderableType
= disp
->ClientAPIs
;
270 base
.Conformant
= disp
->ClientAPIs
;
272 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
273 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
275 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
276 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
280 config_id
= base
.ConfigID
;
281 base
.ConfigID
= EGL_DONT_CARE
;
282 base
.SurfaceType
= EGL_DONT_CARE
;
283 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
284 (_EGLArrayForEach
) dri2_match_config
, &base
);
286 if (num_configs
== 1) {
287 conf
= (struct dri2_egl_config
*) matching_config
;
289 if (double_buffer
&& !conf
->dri_double_config
[srgb
])
290 conf
->dri_double_config
[srgb
] = dri_config
;
291 else if (!double_buffer
&& !conf
->dri_single_config
[srgb
])
292 conf
->dri_single_config
[srgb
] = dri_config
;
294 /* a similar config type is already added (unlikely) => discard */
297 else if (num_configs
== 0) {
298 conf
= calloc(1, sizeof *conf
);
303 conf
->dri_double_config
[srgb
] = dri_config
;
305 conf
->dri_single_config
[srgb
] = dri_config
;
307 memcpy(&conf
->base
, &base
, sizeof base
);
308 conf
->base
.SurfaceType
= 0;
309 conf
->base
.ConfigID
= config_id
;
311 _eglLinkConfig(&conf
->base
);
319 surface_type
&= ~EGL_PIXMAP_BIT
;
322 /* No support for pbuffer + MSAA for now.
324 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
325 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
328 surface_type
&= ~EGL_PBUFFER_BIT
;
331 conf
->base
.SurfaceType
|= surface_type
;
337 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
339 _EGLDisplay
*disp
= data
;
340 struct dri2_egl_image
*dri2_img
;
345 img
= _eglLookupImage(image
, disp
);
347 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
351 dri2_img
= dri2_egl_image(image
);
353 return dri2_img
->dri_image
;
356 const __DRIimageLookupExtension image_lookup_extension
= {
357 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
359 .lookupEGLImage
= dri2_lookup_egl_image
362 struct dri2_extension_match
{
368 static struct dri2_extension_match dri3_driver_extensions
[] = {
369 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
370 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
374 static struct dri2_extension_match dri2_driver_extensions
[] = {
375 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
376 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
380 static struct dri2_extension_match dri2_core_extensions
[] = {
381 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
382 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
383 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
387 static struct dri2_extension_match swrast_driver_extensions
[] = {
388 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
389 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
393 static struct dri2_extension_match swrast_core_extensions
[] = {
394 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
399 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
400 struct dri2_extension_match
*matches
,
401 const __DRIextension
**extensions
)
403 int i
, j
, ret
= EGL_TRUE
;
406 for (i
= 0; extensions
[i
]; i
++) {
407 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
408 for (j
= 0; matches
[j
].name
; j
++) {
409 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
410 extensions
[i
]->version
>= matches
[j
].version
) {
411 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
412 *(const __DRIextension
**) field
= extensions
[i
];
413 _eglLog(_EGL_INFO
, "found extension %s version %d",
414 extensions
[i
]->name
, extensions
[i
]->version
);
419 for (j
= 0; matches
[j
].name
; j
++) {
420 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
421 if (*(const __DRIextension
**) field
== NULL
) {
422 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
423 matches
[j
].name
, matches
[j
].version
);
431 static const __DRIextension
**
432 dri2_open_driver(_EGLDisplay
*disp
)
434 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
435 const __DRIextension
**extensions
= NULL
;
436 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
437 char *get_extensions_name
;
438 const __DRIextension
**(*get_extensions
)(void);
441 if (geteuid() == getuid()) {
442 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
443 search_paths
= getenv("LIBGL_DRIVERS_PATH");
445 if (search_paths
== NULL
)
446 search_paths
= DEFAULT_DRIVER_DIR
;
448 dri2_dpy
->driver
= NULL
;
449 end
= search_paths
+ strlen(search_paths
);
450 for (p
= search_paths
; p
< end
; p
= next
+ 1) {
452 next
= strchr(p
, ':');
458 snprintf(path
, sizeof path
,
459 "%.*s/tls/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
460 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
462 if (dri2_dpy
->driver
== NULL
) {
463 snprintf(path
, sizeof path
,
464 "%.*s/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
465 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
466 if (dri2_dpy
->driver
== NULL
)
467 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
469 /* not need continue to loop all paths once the driver is found */
470 if (dri2_dpy
->driver
!= NULL
)
474 snprintf(path
, sizeof path
, "%.*s/gallium_dri.so", len
, p
);
475 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
476 if (dri2_dpy
->driver
== NULL
)
477 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
483 if (dri2_dpy
->driver
== NULL
) {
484 _eglLog(_EGL_WARNING
,
485 "DRI2: failed to open %s (search paths %s)",
486 dri2_dpy
->driver_name
, search_paths
);
490 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
492 if (asprintf(&get_extensions_name
, "%s_%s",
493 __DRI_DRIVER_GET_EXTENSIONS
, dri2_dpy
->driver_name
) != -1) {
494 get_extensions
= dlsym(dri2_dpy
->driver
, get_extensions_name
);
495 if (get_extensions
) {
496 extensions
= get_extensions();
498 _eglLog(_EGL_DEBUG
, "driver does not expose %s(): %s\n",
499 get_extensions_name
, dlerror());
501 free(get_extensions_name
);
505 extensions
= dlsym(dri2_dpy
->driver
, __DRI_DRIVER_EXTENSIONS
);
506 if (extensions
== NULL
) {
507 _eglLog(_EGL_WARNING
,
508 "DRI2: driver exports no extensions (%s)", dlerror());
509 dlclose(dri2_dpy
->driver
);
516 dri2_load_driver_dri3(_EGLDisplay
*disp
)
518 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
519 const __DRIextension
**extensions
;
521 extensions
= dri2_open_driver(disp
);
525 if (!dri2_bind_extensions(dri2_dpy
, dri3_driver_extensions
, extensions
)) {
526 dlclose(dri2_dpy
->driver
);
529 dri2_dpy
->driver_extensions
= extensions
;
535 dri2_load_driver(_EGLDisplay
*disp
)
537 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
538 const __DRIextension
**extensions
;
540 extensions
= dri2_open_driver(disp
);
544 if (!dri2_bind_extensions(dri2_dpy
, dri2_driver_extensions
, extensions
)) {
545 dlclose(dri2_dpy
->driver
);
548 dri2_dpy
->driver_extensions
= extensions
;
554 dri2_load_driver_swrast(_EGLDisplay
*disp
)
556 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
557 const __DRIextension
**extensions
;
559 extensions
= dri2_open_driver(disp
);
563 if (!dri2_bind_extensions(dri2_dpy
, swrast_driver_extensions
, extensions
)) {
564 dlclose(dri2_dpy
->driver
);
567 dri2_dpy
->driver_extensions
= extensions
;
573 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
575 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
576 unsigned int value
= 0;
578 if (!rendererQuery
||
579 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
586 dri2_setup_screen(_EGLDisplay
*disp
)
588 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
589 unsigned int api_mask
;
591 if (dri2_dpy
->image_driver
) {
592 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
593 } else if (dri2_dpy
->dri2
) {
594 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
596 assert(dri2_dpy
->swrast
);
597 api_mask
= 1 << __DRI_API_OPENGL
|
598 1 << __DRI_API_GLES
|
599 1 << __DRI_API_GLES2
|
600 1 << __DRI_API_GLES3
;
603 disp
->ClientAPIs
= 0;
604 if ((api_mask
& (1 <<__DRI_API_OPENGL
)) && _eglIsApiValid(EGL_OPENGL_API
))
605 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
606 if ((api_mask
& (1 << __DRI_API_GLES
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
607 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
608 if ((api_mask
& (1 << __DRI_API_GLES2
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
609 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
610 if ((api_mask
& (1 << __DRI_API_GLES3
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
611 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
613 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
614 disp
->Extensions
.KHR_no_config_context
= EGL_TRUE
;
615 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
617 if (dri2_renderer_query_integer(dri2_dpy
,
618 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
619 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
621 if (dri2_dpy
->image_driver
||
622 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
623 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
624 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
626 if (dri2_dpy
->robustness
)
627 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
630 if (dri2_dpy
->fence
) {
631 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
632 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
633 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
634 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
637 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
639 if (dri2_dpy
->image
) {
640 if (dri2_dpy
->image
->base
.version
>= 10 &&
641 dri2_dpy
->image
->getCapabilities
!= NULL
) {
644 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
645 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
647 if (dri2_dpy
->image
->base
.version
>= 11)
648 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
650 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
651 if (dri2_dpy
->image
->base
.version
>= 11)
652 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
655 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
656 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
657 if (dri2_dpy
->image
->base
.version
>= 5 &&
658 dri2_dpy
->image
->createImageFromTexture
) {
659 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
660 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
662 if (dri2_renderer_query_integer(dri2_dpy
,
663 __DRI2_RENDERER_HAS_TEXTURE_3D
))
664 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
666 if (dri2_dpy
->image
->base
.version
>= 8 &&
667 dri2_dpy
->image
->createImageFromDmaBufs
) {
668 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
674 /* All platforms but DRM call this function to create the screen, query the
675 * dri extensions, setup the vtables and populate the driver_configs.
676 * DRM inherits all that information from its display - GBM.
679 dri2_create_screen(_EGLDisplay
*disp
)
681 const __DRIextension
**extensions
;
682 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
685 if (dri2_dpy
->image_driver
) {
686 dri2_dpy
->dri_screen
=
687 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
688 dri2_dpy
->extensions
,
689 dri2_dpy
->driver_extensions
,
690 &dri2_dpy
->driver_configs
,
692 } else if (dri2_dpy
->dri2
) {
693 if (dri2_dpy
->dri2
->base
.version
>= 4) {
694 dri2_dpy
->dri_screen
=
695 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
696 dri2_dpy
->extensions
,
697 dri2_dpy
->driver_extensions
,
698 &dri2_dpy
->driver_configs
, disp
);
700 dri2_dpy
->dri_screen
=
701 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
702 dri2_dpy
->extensions
,
703 &dri2_dpy
->driver_configs
, disp
);
706 assert(dri2_dpy
->swrast
);
707 if (dri2_dpy
->swrast
->base
.version
>= 4) {
708 dri2_dpy
->dri_screen
=
709 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->extensions
,
710 dri2_dpy
->driver_extensions
,
711 &dri2_dpy
->driver_configs
, disp
);
713 dri2_dpy
->dri_screen
=
714 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->extensions
,
715 &dri2_dpy
->driver_configs
, disp
);
719 if (dri2_dpy
->dri_screen
== NULL
) {
720 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
724 dri2_dpy
->own_dri_screen
= 1;
726 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
728 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
) {
729 if (!dri2_bind_extensions(dri2_dpy
, dri2_core_extensions
, extensions
))
730 goto cleanup_dri_screen
;
732 assert(dri2_dpy
->swrast
);
733 if (!dri2_bind_extensions(dri2_dpy
, swrast_core_extensions
, extensions
))
734 goto cleanup_dri_screen
;
737 for (i
= 0; extensions
[i
]; i
++) {
738 if (strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0) {
739 dri2_dpy
->robustness
= (__DRIrobustnessExtension
*) extensions
[i
];
741 if (strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0) {
742 dri2_dpy
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
744 if (strcmp(extensions
[i
]->name
, __DRI2_FENCE
) == 0) {
745 dri2_dpy
->fence
= (__DRI2fenceExtension
*) extensions
[i
];
747 if (strcmp(extensions
[i
]->name
, __DRI2_RENDERER_QUERY
) == 0) {
748 dri2_dpy
->rendererQuery
= (__DRI2rendererQueryExtension
*) extensions
[i
];
750 if (strcmp(extensions
[i
]->name
, __DRI2_INTEROP
) == 0)
751 dri2_dpy
->interop
= (__DRI2interopExtension
*) extensions
[i
];
754 dri2_setup_screen(disp
);
759 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
765 * Called via eglInitialize(), GLX_drv->API.Initialize().
767 * This must be guaranteed to be called exactly once, even if eglInitialize is
768 * called many times (without a eglTerminate in between).
771 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
773 EGLBoolean ret
= EGL_FALSE
;
774 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
776 /* In the case where the application calls eglMakeCurrent(context1),
777 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
778 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
779 * initialized, as we need it to be able to free context1 correctly.
781 * It would probably be safest to forcibly release the display with
782 * dri2_display_release, to make sure the display is reinitialized correctly.
783 * However, the EGL spec states that we need to keep a reference to the
784 * current context (so we cannot call dri2_make_current(NULL)), and therefore
785 * we would leak context1 as we would be missing the old display connection
786 * to free it up correctly.
789 dri2_dpy
->ref_count
++;
793 /* not until swrast_dri is supported */
794 if (disp
->Options
.UseFallback
)
797 /* Nothing to initialize for a test only display */
798 if (disp
->Options
.TestOnly
)
801 switch (disp
->Platform
) {
802 #ifdef HAVE_SURFACELESS_PLATFORM
803 case _EGL_PLATFORM_SURFACELESS
:
804 ret
= dri2_initialize_surfaceless(drv
, disp
);
807 #ifdef HAVE_X11_PLATFORM
808 case _EGL_PLATFORM_X11
:
809 ret
= dri2_initialize_x11(drv
, disp
);
812 #ifdef HAVE_DRM_PLATFORM
813 case _EGL_PLATFORM_DRM
:
814 ret
= dri2_initialize_drm(drv
, disp
);
817 #ifdef HAVE_WAYLAND_PLATFORM
818 case _EGL_PLATFORM_WAYLAND
:
819 ret
= dri2_initialize_wayland(drv
, disp
);
822 #ifdef HAVE_ANDROID_PLATFORM
823 case _EGL_PLATFORM_ANDROID
:
824 ret
= dri2_initialize_android(drv
, disp
);
828 _eglLog(_EGL_WARNING
, "No EGL platform enabled.");
833 dri2_dpy
= dri2_egl_display(disp
);
839 dri2_dpy
->ref_count
++;
846 * Decrement display reference count, and free up display if necessary.
849 dri2_display_release(_EGLDisplay
*disp
)
851 struct dri2_egl_display
*dri2_dpy
;
857 dri2_dpy
= dri2_egl_display(disp
);
859 assert(dri2_dpy
->ref_count
> 0);
860 dri2_dpy
->ref_count
--;
862 if (dri2_dpy
->ref_count
> 0)
865 _eglCleanupDisplay(disp
);
867 if (dri2_dpy
->own_dri_screen
)
868 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
869 if (dri2_dpy
->fd
>= 0)
871 if (dri2_dpy
->driver
)
872 dlclose(dri2_dpy
->driver
);
873 free(dri2_dpy
->driver_name
);
875 #ifdef HAVE_WAYLAND_PLATFORM
876 free(dri2_dpy
->device_name
);
879 switch (disp
->Platform
) {
880 #ifdef HAVE_X11_PLATFORM
881 case _EGL_PLATFORM_X11
:
882 if (dri2_dpy
->own_device
) {
883 xcb_disconnect(dri2_dpy
->conn
);
887 #ifdef HAVE_DRM_PLATFORM
888 case _EGL_PLATFORM_DRM
:
889 if (dri2_dpy
->own_device
) {
890 gbm_device_destroy(&dri2_dpy
->gbm_dri
->base
.base
);
894 #ifdef HAVE_WAYLAND_PLATFORM
895 case _EGL_PLATFORM_WAYLAND
:
896 if (dri2_dpy
->wl_drm
)
897 wl_drm_destroy(dri2_dpy
->wl_drm
);
898 if (dri2_dpy
->wl_shm
)
899 wl_shm_destroy(dri2_dpy
->wl_shm
);
900 wl_registry_destroy(dri2_dpy
->wl_registry
);
901 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
902 if (dri2_dpy
->own_device
) {
903 wl_display_disconnect(dri2_dpy
->wl_dpy
);
911 /* The drm platform does not create the screen/driver_configs but reuses
912 * the ones from the gbm device. As such the gbm itself is responsible
915 if (disp
->Platform
!= _EGL_PLATFORM_DRM
) {
916 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
917 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
918 free(dri2_dpy
->driver_configs
);
921 disp
->DriverData
= NULL
;
925 * Called via eglTerminate(), drv->API.Terminate().
927 * This must be guaranteed to be called exactly once, even if eglTerminate is
928 * called many times (without a eglInitialize in between).
931 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
933 /* Release all non-current Context/Surfaces. */
934 _eglReleaseDisplayResources(drv
, disp
);
936 dri2_display_release(disp
);
942 * Set the error code after a call to
943 * dri2_egl_display::dri2::createContextAttribs.
946 dri2_create_context_attribs_error(int dri_error
)
951 case __DRI_CTX_ERROR_SUCCESS
:
954 case __DRI_CTX_ERROR_NO_MEMORY
:
955 egl_error
= EGL_BAD_ALLOC
;
958 /* From the EGL_KHR_create_context spec, section "Errors":
960 * * If <config> does not support a client API context compatible
961 * with the requested API major and minor version, [...] context flags,
962 * and context reset notification behavior (for client API types where
963 * these attributes are supported), then an EGL_BAD_MATCH error is
966 * * If an OpenGL ES context is requested and the values for
967 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
968 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
969 * is not defined, than an EGL_BAD_MATCH error is generated.
971 * * If an OpenGL context is requested, the requested version is
972 * greater than 3.2, and the value for attribute
973 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
974 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
975 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
976 * one of these bits set; or if the implementation does not support
977 * the requested profile, then an EGL_BAD_MATCH error is generated.
979 case __DRI_CTX_ERROR_BAD_API
:
980 case __DRI_CTX_ERROR_BAD_VERSION
:
981 case __DRI_CTX_ERROR_BAD_FLAG
:
982 egl_error
= EGL_BAD_MATCH
;
985 /* From the EGL_KHR_create_context spec, section "Errors":
987 * * If an attribute name or attribute value in <attrib_list> is not
988 * recognized (including unrecognized bits in bitmask attributes),
989 * then an EGL_BAD_ATTRIBUTE error is generated."
991 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
992 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
993 egl_error
= EGL_BAD_ATTRIBUTE
;
998 egl_error
= EGL_BAD_MATCH
;
1002 _eglError(egl_error
, "dri2_create_context");
1006 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1007 struct dri2_egl_display
*dri2_dpy
,
1008 uint32_t *ctx_attribs
,
1009 unsigned *num_attribs
)
1013 assert(*num_attribs
>= 8);
1015 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1016 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1017 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1018 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1020 if (dri2_ctx
->base
.Flags
!= 0) {
1021 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1022 * extension, don't even try to send it the robust-access flag.
1023 * It may explode. Instead, generate the required EGL error here.
1025 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1026 && !dri2_dpy
->robustness
) {
1027 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1031 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1032 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
;
1035 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1036 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1037 * extension, don't even try to send it a reset strategy. It may
1038 * explode. Instead, generate the required EGL error here.
1040 if (!dri2_dpy
->robustness
) {
1041 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1045 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1046 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1055 * Called via eglCreateContext(), drv->API.CreateContext().
1057 static _EGLContext
*
1058 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1059 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1061 struct dri2_egl_context
*dri2_ctx
;
1062 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1063 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1064 __DRIcontext
*shared
=
1065 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1066 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1067 const __DRIconfig
*dri_config
;
1072 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1074 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1078 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1081 switch (dri2_ctx
->base
.ClientAPI
) {
1082 case EGL_OPENGL_ES_API
:
1083 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1085 api
= __DRI_API_GLES
;
1088 api
= __DRI_API_GLES2
;
1091 api
= __DRI_API_GLES3
;
1094 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1099 case EGL_OPENGL_API
:
1100 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1101 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1102 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1103 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1104 api
= __DRI_API_OPENGL_CORE
;
1106 api
= __DRI_API_OPENGL
;
1109 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1115 /* The config chosen here isn't necessarily
1116 * used for surfaces later.
1117 * A pixmap surface will use the single config.
1118 * This opportunity depends on disabling the
1119 * doubleBufferMode check in
1120 * src/mesa/main/context.c:check_compatible()
1122 if (dri2_config
->dri_double_config
[0])
1123 dri_config
= dri2_config
->dri_double_config
[0];
1125 dri_config
= dri2_config
->dri_single_config
[0];
1127 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
1128 * makes sure the back buffer will always be used.
1130 if (conf
->SurfaceType
& EGL_WINDOW_BIT
)
1131 dri2_ctx
->base
.WindowRenderBuffer
= EGL_BACK_BUFFER
;
1136 if (dri2_dpy
->image_driver
) {
1138 unsigned num_attribs
= 8;
1139 uint32_t ctx_attribs
[8];
1141 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1145 dri2_ctx
->dri_context
=
1146 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1154 dri2_create_context_attribs_error(error
);
1155 } else if (dri2_dpy
->dri2
) {
1156 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1158 unsigned num_attribs
= 8;
1159 uint32_t ctx_attribs
[8];
1161 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1165 dri2_ctx
->dri_context
=
1166 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1174 dri2_create_context_attribs_error(error
);
1176 dri2_ctx
->dri_context
=
1177 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1184 assert(dri2_dpy
->swrast
);
1185 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1187 unsigned num_attribs
= 8;
1188 uint32_t ctx_attribs
[8];
1190 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1194 dri2_ctx
->dri_context
=
1195 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1203 dri2_create_context_attribs_error(error
);
1205 dri2_ctx
->dri_context
=
1206 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1214 if (!dri2_ctx
->dri_context
)
1217 return &dri2_ctx
->base
;
1225 * Called via eglDestroyContext(), drv->API.DestroyContext().
1228 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1230 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1231 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1233 if (_eglPutContext(ctx
)) {
1234 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1242 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1244 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1246 if (!_eglPutSurface(surf
))
1249 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1253 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1256 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1257 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1259 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1260 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1261 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1262 _EGLContext
*old_ctx
;
1263 _EGLSurface
*old_dsurf
, *old_rsurf
;
1264 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1265 __DRIdrawable
*ddraw
, *rdraw
;
1270 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1272 /* make new bindings */
1273 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1274 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1278 /* flush before context switch */
1280 dri2_drv
->glFlush();
1282 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1283 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1284 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1287 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1288 dri2_dpy
->core
->unbindContext(old_cctx
);
1291 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1293 if (unbind
|| dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1294 dri2_destroy_surface(drv
, disp
, old_dsurf
);
1295 dri2_destroy_surface(drv
, disp
, old_rsurf
);
1298 dri2_dpy
->ref_count
++;
1300 EGLDisplay old_disp
= _eglGetDisplayHandle(old_ctx
->Resource
.Display
);
1301 dri2_destroy_context(drv
, disp
, old_ctx
);
1302 dri2_display_release(old_disp
);
1307 /* undo the previous _eglBindContext */
1308 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1309 assert(&dri2_ctx
->base
== ctx
&&
1310 tmp_dsurf
== dsurf
&&
1311 tmp_rsurf
== rsurf
);
1313 _eglPutSurface(dsurf
);
1314 _eglPutSurface(rsurf
);
1315 _eglPutContext(ctx
);
1317 _eglPutSurface(old_dsurf
);
1318 _eglPutSurface(old_rsurf
);
1319 _eglPutContext(old_ctx
);
1321 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1322 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1325 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1330 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1332 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1334 return dri2_surf
->dri_drawable
;
1338 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1341 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1343 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1345 return dri2_drv
->get_proc_address(procname
);
1349 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1350 _EGLConfig
*conf
, void *native_window
,
1351 const EGLint
*attrib_list
)
1353 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1354 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1359 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1360 _EGLConfig
*conf
, void *native_pixmap
,
1361 const EGLint
*attrib_list
)
1363 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1364 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1369 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1370 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1372 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1373 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
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
;