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
) },
398 static struct dri2_extension_match optional_core_extensions
[] = {
399 { __DRI2_ROBUSTNESS
, 1, offsetof(struct dri2_egl_display
, robustness
) },
400 { __DRI2_CONFIG_QUERY
, 1, offsetof(struct dri2_egl_display
, config
) },
401 { __DRI2_FENCE
, 1, offsetof(struct dri2_egl_display
, fence
) },
402 { __DRI2_RENDERER_QUERY
, 1, offsetof(struct dri2_egl_display
, rendererQuery
) },
403 { __DRI2_INTEROP
, 1, offsetof(struct dri2_egl_display
, interop
) },
408 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
409 struct dri2_extension_match
*matches
,
410 const __DRIextension
**extensions
,
413 int i
, j
, ret
= EGL_TRUE
;
416 for (i
= 0; extensions
[i
]; i
++) {
417 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
418 for (j
= 0; matches
[j
].name
; j
++) {
419 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
420 extensions
[i
]->version
>= matches
[j
].version
) {
421 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
422 *(const __DRIextension
**) field
= extensions
[i
];
423 _eglLog(_EGL_INFO
, "found extension %s version %d",
424 extensions
[i
]->name
, extensions
[i
]->version
);
429 for (j
= 0; matches
[j
].name
; j
++) {
430 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
431 if (*(const __DRIextension
**) field
== NULL
) {
433 _eglLog(_EGL_DEBUG
, "did not find optional extension %s version %d",
434 matches
[j
].name
, matches
[j
].version
);
436 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
437 matches
[j
].name
, matches
[j
].version
);
446 static const __DRIextension
**
447 dri2_open_driver(_EGLDisplay
*disp
)
449 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
450 const __DRIextension
**extensions
= NULL
;
451 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
452 char *get_extensions_name
;
453 const __DRIextension
**(*get_extensions
)(void);
456 if (geteuid() == getuid()) {
457 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
458 search_paths
= getenv("LIBGL_DRIVERS_PATH");
460 if (search_paths
== NULL
)
461 search_paths
= DEFAULT_DRIVER_DIR
;
463 dri2_dpy
->driver
= NULL
;
464 end
= search_paths
+ strlen(search_paths
);
465 for (p
= search_paths
; p
< end
; p
= next
+ 1) {
467 next
= strchr(p
, ':');
473 snprintf(path
, sizeof path
,
474 "%.*s/tls/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
475 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
477 if (dri2_dpy
->driver
== NULL
) {
478 snprintf(path
, sizeof path
,
479 "%.*s/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
480 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
481 if (dri2_dpy
->driver
== NULL
)
482 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
484 /* not need continue to loop all paths once the driver is found */
485 if (dri2_dpy
->driver
!= NULL
)
489 snprintf(path
, sizeof path
, "%.*s/gallium_dri.so", len
, p
);
490 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
491 if (dri2_dpy
->driver
== NULL
)
492 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
498 if (dri2_dpy
->driver
== NULL
) {
499 _eglLog(_EGL_WARNING
,
500 "DRI2: failed to open %s (search paths %s)",
501 dri2_dpy
->driver_name
, search_paths
);
505 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
507 if (asprintf(&get_extensions_name
, "%s_%s",
508 __DRI_DRIVER_GET_EXTENSIONS
, dri2_dpy
->driver_name
) != -1) {
509 get_extensions
= dlsym(dri2_dpy
->driver
, get_extensions_name
);
510 if (get_extensions
) {
511 extensions
= get_extensions();
513 _eglLog(_EGL_DEBUG
, "driver does not expose %s(): %s\n",
514 get_extensions_name
, dlerror());
516 free(get_extensions_name
);
520 extensions
= dlsym(dri2_dpy
->driver
, __DRI_DRIVER_EXTENSIONS
);
521 if (extensions
== NULL
) {
522 _eglLog(_EGL_WARNING
,
523 "DRI2: driver exports no extensions (%s)", dlerror());
524 dlclose(dri2_dpy
->driver
);
531 dri2_load_driver_dri3(_EGLDisplay
*disp
)
533 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
534 const __DRIextension
**extensions
;
536 extensions
= dri2_open_driver(disp
);
540 if (!dri2_bind_extensions(dri2_dpy
, dri3_driver_extensions
, extensions
, false)) {
541 dlclose(dri2_dpy
->driver
);
544 dri2_dpy
->driver_extensions
= extensions
;
550 dri2_load_driver(_EGLDisplay
*disp
)
552 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
553 const __DRIextension
**extensions
;
555 extensions
= dri2_open_driver(disp
);
559 if (!dri2_bind_extensions(dri2_dpy
, dri2_driver_extensions
, extensions
, false)) {
560 dlclose(dri2_dpy
->driver
);
563 dri2_dpy
->driver_extensions
= extensions
;
569 dri2_load_driver_swrast(_EGLDisplay
*disp
)
571 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
572 const __DRIextension
**extensions
;
574 extensions
= dri2_open_driver(disp
);
578 if (!dri2_bind_extensions(dri2_dpy
, swrast_driver_extensions
, extensions
, false)) {
579 dlclose(dri2_dpy
->driver
);
582 dri2_dpy
->driver_extensions
= extensions
;
588 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
590 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
591 unsigned int value
= 0;
593 if (!rendererQuery
||
594 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
601 dri2_setup_screen(_EGLDisplay
*disp
)
603 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
604 unsigned int api_mask
;
606 if (dri2_dpy
->image_driver
) {
607 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
608 } else if (dri2_dpy
->dri2
) {
609 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
611 assert(dri2_dpy
->swrast
);
612 api_mask
= 1 << __DRI_API_OPENGL
|
613 1 << __DRI_API_GLES
|
614 1 << __DRI_API_GLES2
|
615 1 << __DRI_API_GLES3
;
618 disp
->ClientAPIs
= 0;
619 if ((api_mask
& (1 <<__DRI_API_OPENGL
)) && _eglIsApiValid(EGL_OPENGL_API
))
620 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
621 if ((api_mask
& (1 << __DRI_API_GLES
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
622 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
623 if ((api_mask
& (1 << __DRI_API_GLES2
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
624 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
625 if ((api_mask
& (1 << __DRI_API_GLES3
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
626 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
628 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
629 disp
->Extensions
.KHR_no_config_context
= EGL_TRUE
;
630 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
632 if (dri2_renderer_query_integer(dri2_dpy
,
633 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
634 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
636 if (dri2_dpy
->image_driver
||
637 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
638 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
639 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
641 if (dri2_dpy
->robustness
)
642 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
645 if (dri2_dpy
->fence
) {
646 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
647 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
648 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
649 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
652 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
654 if (dri2_dpy
->image
) {
655 if (dri2_dpy
->image
->base
.version
>= 10 &&
656 dri2_dpy
->image
->getCapabilities
!= NULL
) {
659 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
660 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
662 if (dri2_dpy
->image
->base
.version
>= 11)
663 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
665 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
666 if (dri2_dpy
->image
->base
.version
>= 11)
667 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
670 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
671 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
672 if (dri2_dpy
->image
->base
.version
>= 5 &&
673 dri2_dpy
->image
->createImageFromTexture
) {
674 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
675 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
677 if (dri2_renderer_query_integer(dri2_dpy
,
678 __DRI2_RENDERER_HAS_TEXTURE_3D
))
679 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
681 if (dri2_dpy
->image
->base
.version
>= 8 &&
682 dri2_dpy
->image
->createImageFromDmaBufs
) {
683 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
689 /* All platforms but DRM call this function to create the screen, query the
690 * dri extensions, setup the vtables and populate the driver_configs.
691 * DRM inherits all that information from its display - GBM.
694 dri2_create_screen(_EGLDisplay
*disp
)
696 const __DRIextension
**extensions
;
697 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
699 if (dri2_dpy
->image_driver
) {
700 dri2_dpy
->dri_screen
=
701 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
702 dri2_dpy
->loader_extensions
,
703 dri2_dpy
->driver_extensions
,
704 &dri2_dpy
->driver_configs
,
706 } else if (dri2_dpy
->dri2
) {
707 if (dri2_dpy
->dri2
->base
.version
>= 4) {
708 dri2_dpy
->dri_screen
=
709 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
710 dri2_dpy
->loader_extensions
,
711 dri2_dpy
->driver_extensions
,
712 &dri2_dpy
->driver_configs
, disp
);
714 dri2_dpy
->dri_screen
=
715 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
716 dri2_dpy
->loader_extensions
,
717 &dri2_dpy
->driver_configs
, disp
);
720 assert(dri2_dpy
->swrast
);
721 if (dri2_dpy
->swrast
->base
.version
>= 4) {
722 dri2_dpy
->dri_screen
=
723 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->loader_extensions
,
724 dri2_dpy
->driver_extensions
,
725 &dri2_dpy
->driver_configs
, disp
);
727 dri2_dpy
->dri_screen
=
728 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->loader_extensions
,
729 &dri2_dpy
->driver_configs
, disp
);
733 if (dri2_dpy
->dri_screen
== NULL
) {
734 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
738 dri2_dpy
->own_dri_screen
= 1;
740 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
742 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
) {
743 if (!dri2_bind_extensions(dri2_dpy
, dri2_core_extensions
, extensions
, false))
744 goto cleanup_dri_screen
;
746 assert(dri2_dpy
->swrast
);
747 if (!dri2_bind_extensions(dri2_dpy
, swrast_core_extensions
, extensions
, false))
748 goto cleanup_dri_screen
;
751 dri2_bind_extensions(dri2_dpy
, optional_core_extensions
, extensions
, true);
752 dri2_setup_screen(disp
);
757 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
763 * Called via eglInitialize(), GLX_drv->API.Initialize().
765 * This must be guaranteed to be called exactly once, even if eglInitialize is
766 * called many times (without a eglTerminate in between).
769 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
771 EGLBoolean ret
= EGL_FALSE
;
772 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
774 /* In the case where the application calls eglMakeCurrent(context1),
775 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
776 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
777 * initialized, as we need it to be able to free context1 correctly.
779 * It would probably be safest to forcibly release the display with
780 * dri2_display_release, to make sure the display is reinitialized correctly.
781 * However, the EGL spec states that we need to keep a reference to the
782 * current context (so we cannot call dri2_make_current(NULL)), and therefore
783 * we would leak context1 as we would be missing the old display connection
784 * to free it up correctly.
787 dri2_dpy
->ref_count
++;
791 /* not until swrast_dri is supported */
792 if (disp
->Options
.UseFallback
)
795 /* Nothing to initialize for a test only display */
796 if (disp
->Options
.TestOnly
)
799 switch (disp
->Platform
) {
800 #ifdef HAVE_SURFACELESS_PLATFORM
801 case _EGL_PLATFORM_SURFACELESS
:
802 ret
= dri2_initialize_surfaceless(drv
, disp
);
805 #ifdef HAVE_X11_PLATFORM
806 case _EGL_PLATFORM_X11
:
807 ret
= dri2_initialize_x11(drv
, disp
);
810 #ifdef HAVE_DRM_PLATFORM
811 case _EGL_PLATFORM_DRM
:
812 ret
= dri2_initialize_drm(drv
, disp
);
815 #ifdef HAVE_WAYLAND_PLATFORM
816 case _EGL_PLATFORM_WAYLAND
:
817 ret
= dri2_initialize_wayland(drv
, disp
);
820 #ifdef HAVE_ANDROID_PLATFORM
821 case _EGL_PLATFORM_ANDROID
:
822 ret
= dri2_initialize_android(drv
, disp
);
826 _eglLog(_EGL_WARNING
, "No EGL platform enabled.");
831 dri2_dpy
= dri2_egl_display(disp
);
837 dri2_dpy
->ref_count
++;
844 * Decrement display reference count, and free up display if necessary.
847 dri2_display_release(_EGLDisplay
*disp
)
849 struct dri2_egl_display
*dri2_dpy
;
855 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 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1242 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1244 if (!_eglPutSurface(surf
))
1247 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1251 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1254 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1255 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1257 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1258 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1259 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1260 _EGLContext
*old_ctx
;
1261 _EGLDisplay
*old_disp
= NULL
;
1262 _EGLSurface
*old_dsurf
, *old_rsurf
;
1263 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1264 __DRIdrawable
*ddraw
, *rdraw
;
1268 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1270 /* make new bindings */
1271 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1272 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1276 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1277 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1278 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1281 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1283 /* flush before context switch */
1284 dri2_drv
->glFlush();
1285 dri2_dpy
->core
->unbindContext(old_cctx
);
1287 /* Keep track of the old dpy as we'll need it for resource cleanup */
1288 old_disp
= old_ctx
->Resource
.Display
;
1291 /* Bind if at least one of the primitives is valid ... */
1292 if (cctx
|| ddraw
|| rdraw
) {
1293 if (dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1294 /* undo the previous _eglBindContext */
1295 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1296 assert(&dri2_ctx
->base
== ctx
&&
1297 tmp_dsurf
== dsurf
&&
1298 tmp_rsurf
== rsurf
);
1300 _eglPutSurface(dsurf
);
1301 _eglPutSurface(rsurf
);
1302 _eglPutContext(ctx
);
1304 _eglPutSurface(old_dsurf
);
1305 _eglPutSurface(old_rsurf
);
1306 _eglPutContext(old_ctx
);
1308 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1309 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1312 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1315 /* ... and refcount the dpy when successful. */
1316 dri2_dpy
->ref_count
++;
1319 dri2_destroy_surface(drv
, disp
, old_dsurf
);
1320 dri2_destroy_surface(drv
, disp
, old_rsurf
);
1321 dri2_destroy_context(drv
, disp
, old_ctx
);
1322 dri2_display_release(old_disp
);
1328 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1330 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1332 return dri2_surf
->dri_drawable
;
1336 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1339 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1341 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1343 return dri2_drv
->get_proc_address(procname
);
1347 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1348 _EGLConfig
*conf
, void *native_window
,
1349 const EGLint
*attrib_list
)
1351 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1352 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1357 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1358 _EGLConfig
*conf
, void *native_pixmap
,
1359 const EGLint
*attrib_list
)
1361 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1362 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1367 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1368 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1370 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1371 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
1375 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1378 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1379 return dri2_dpy
->vtbl
->swap_interval(drv
, dpy
, surf
, interval
);
1383 * Asks the client API to flush any rendering to the drawable so that we can
1384 * do our swapbuffers.
1387 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1389 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1390 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1392 if (dri2_dpy
->flush
) {
1393 if (dri2_dpy
->flush
->base
.version
>= 4) {
1394 /* We know there's a current context because:
1396 * "If surface is not bound to the calling thread’s current
1397 * context, an EGL_BAD_SURFACE error is generated."
1399 _EGLContext
*ctx
= _eglGetCurrentContext();
1400 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1402 /* From the EGL 1.4 spec (page 52):
1404 * "The contents of ancillary buffers are always undefined
1405 * after calling eglSwapBuffers."
1407 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1409 __DRI2_FLUSH_DRAWABLE
|
1410 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1411 __DRI2_THROTTLE_SWAPBUFFER
);
1413 dri2_dpy
->flush
->flush(dri_drawable
);
1419 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1421 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1422 return dri2_dpy
->vtbl
->swap_buffers(drv
, dpy
, surf
);
1426 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1428 const EGLint
*rects
, EGLint n_rects
)
1430 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1431 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, dpy
, surf
,
1436 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1437 EGLint numRects
, const EGLint
*rects
)
1439 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1440 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, dpy
, surf
, numRects
, rects
);
1444 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1445 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1447 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1448 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, dpy
, surf
, x
, y
, width
, height
);
1452 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1453 void *native_pixmap_target
)
1455 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1456 return dri2_dpy
->vtbl
->copy_buffers(drv
, dpy
, surf
, native_pixmap_target
);
1460 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1462 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1463 return dri2_dpy
->vtbl
->query_buffer_age(drv
, dpy
, surf
);
1467 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1469 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1470 _EGLSurface
*surf
= ctx
->DrawSurface
;
1471 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1475 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1476 * we need to copy fake to real here.*/
1478 if (dri2_dpy
->flush
!= NULL
)
1479 dri2_dpy
->flush
->flush(dri_drawable
);
1485 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1490 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1491 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1498 dri2_bind_tex_image(_EGLDriver
*drv
,
1499 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1501 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1502 struct dri2_egl_context
*dri2_ctx
;
1504 GLint format
, target
;
1505 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1507 ctx
= _eglGetCurrentContext();
1508 dri2_ctx
= dri2_egl_context(ctx
);
1510 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1513 switch (surf
->TextureFormat
) {
1514 case EGL_TEXTURE_RGB
:
1515 format
= __DRI_TEXTURE_FORMAT_RGB
;
1517 case EGL_TEXTURE_RGBA
:
1518 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1521 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1522 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1525 switch (surf
->TextureTarget
) {
1526 case EGL_TEXTURE_2D
:
1527 target
= GL_TEXTURE_2D
;
1530 target
= GL_TEXTURE_2D
;
1531 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1534 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1542 dri2_release_tex_image(_EGLDriver
*drv
,
1543 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1545 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1546 struct dri2_egl_context
*dri2_ctx
;
1549 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1551 ctx
= _eglGetCurrentContext();
1552 dri2_ctx
= dri2_egl_context(ctx
);
1554 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1557 switch (surf
->TextureTarget
) {
1558 case EGL_TEXTURE_2D
:
1559 target
= GL_TEXTURE_2D
;
1565 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1566 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1567 (*dri2_dpy
->tex_buffer
->releaseTexBuffer
)(dri2_ctx
->dri_context
,
1576 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
1577 EGLenum target
, EGLClientBuffer buffer
,
1578 const EGLint
*attr_list
)
1580 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1581 return dri2_dpy
->vtbl
->create_image(drv
, dpy
, ctx
, target
, buffer
,
1586 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1588 struct dri2_egl_image
*dri2_img
;
1590 if (dri_image
== NULL
) {
1591 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1595 dri2_img
= malloc(sizeof *dri2_img
);
1597 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1601 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1606 dri2_img
->dri_image
= dri_image
;
1608 return &dri2_img
->base
;
1612 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1613 EGLClientBuffer buffer
,
1614 const EGLint
*attr_list
)
1616 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1617 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1618 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1619 __DRIimage
*dri_image
;
1621 if (renderbuffer
== 0) {
1622 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1623 return EGL_NO_IMAGE_KHR
;
1627 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1628 renderbuffer
, NULL
);
1630 return dri2_create_image_from_dri(disp
, dri_image
);
1633 #ifdef HAVE_WAYLAND_PLATFORM
1635 /* This structure describes how a wl_buffer maps to one or more
1636 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1637 * offsets and strides of the planes in the buffer. This table maps a
1638 * wl_drm format code to a description of the planes in the buffer
1639 * that lets us create a __DRIimage for each of the planes. */
1641 static const struct wl_drm_components_descriptor
{
1642 uint32_t dri_components
;
1645 } wl_drm_components
[] = {
1646 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1647 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1648 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1649 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1650 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1654 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1655 EGLClientBuffer _buffer
,
1656 const EGLint
*attr_list
)
1658 struct wl_drm_buffer
*buffer
;
1659 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1660 const struct wl_drm_components_descriptor
*f
;
1661 __DRIimage
*dri_image
;
1662 _EGLImageAttribs attrs
;
1666 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1667 (struct wl_resource
*) _buffer
);
1671 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1672 plane
= attrs
.PlaneWL
;
1673 if (err
!= EGL_SUCCESS
) {
1674 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1678 f
= buffer
->driver_format
;
1679 if (plane
< 0 || plane
>= f
->nplanes
) {
1680 _eglError(EGL_BAD_PARAMETER
,
1681 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1685 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1687 if (dri_image
== NULL
) {
1688 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1692 return dri2_create_image_from_dri(disp
, dri_image
);
1697 dri2_get_sync_values_chromium(_EGLDisplay
*dpy
, _EGLSurface
*surf
,
1698 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1701 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1702 return dri2_dpy
->vtbl
->get_sync_values(dpy
, surf
, ust
, msc
, sbc
);
1706 * Set the error code after a call to
1707 * dri2_egl_image::dri_image::createImageFromTexture.
1710 dri2_create_image_khr_texture_error(int dri_error
)
1714 switch (dri_error
) {
1715 case __DRI_IMAGE_ERROR_SUCCESS
:
1718 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1719 egl_error
= EGL_BAD_ALLOC
;
1722 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1723 egl_error
= EGL_BAD_MATCH
;
1726 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1727 egl_error
= EGL_BAD_PARAMETER
;
1730 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
1731 egl_error
= EGL_BAD_ACCESS
;
1736 egl_error
= EGL_BAD_MATCH
;
1740 _eglError(egl_error
, "dri2_create_image_khr_texture");
1744 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1746 EGLClientBuffer buffer
,
1747 const EGLint
*attr_list
)
1749 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1750 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1751 struct dri2_egl_image
*dri2_img
;
1752 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
1753 _EGLImageAttribs attrs
;
1759 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1760 return EGL_NO_IMAGE_KHR
;
1763 if (_eglParseImageAttribList(&attrs
, disp
, attr_list
) != EGL_SUCCESS
)
1764 return EGL_NO_IMAGE_KHR
;
1767 case EGL_GL_TEXTURE_2D_KHR
:
1769 gl_target
= GL_TEXTURE_2D
;
1771 case EGL_GL_TEXTURE_3D_KHR
:
1772 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1773 depth
= attrs
.GLTextureZOffset
;
1774 gl_target
= GL_TEXTURE_3D
;
1778 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1779 return EGL_NO_IMAGE_KHR
;
1781 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1782 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1783 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1784 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1785 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1786 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1787 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
1788 gl_target
= GL_TEXTURE_CUBE_MAP
;
1791 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1792 return EGL_NO_IMAGE_KHR
;
1795 dri2_img
= malloc(sizeof *dri2_img
);
1797 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1798 return EGL_NO_IMAGE_KHR
;
1801 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1802 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1804 return EGL_NO_IMAGE_KHR
;
1807 dri2_img
->dri_image
=
1808 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
1812 attrs
.GLTextureLevel
,
1815 dri2_create_image_khr_texture_error(error
);
1817 if (!dri2_img
->dri_image
) {
1819 return EGL_NO_IMAGE_KHR
;
1821 return &dri2_img
->base
;
1825 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1826 EGLint attribute
, EGLint
*value
)
1828 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1829 if (!dri2_dpy
->vtbl
->query_surface
)
1830 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
1831 return dri2_dpy
->vtbl
->query_surface(drv
, dpy
, surf
, attribute
, value
);
1834 static struct wl_buffer
*
1835 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1838 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1839 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, dpy
, img
);
1844 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1845 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1847 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1848 EGLint format
, name
, pitch
, err
;
1849 _EGLImageAttribs attrs
;
1850 __DRIimage
*dri_image
;
1852 name
= (EGLint
) (uintptr_t) buffer
;
1854 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1855 if (err
!= EGL_SUCCESS
)
1858 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
1859 attrs
.DRMBufferStrideMESA
<= 0) {
1860 _eglError(EGL_BAD_PARAMETER
,
1861 "bad width, height or stride");
1865 switch (attrs
.DRMBufferFormatMESA
) {
1866 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1867 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1868 pitch
= attrs
.DRMBufferStrideMESA
;
1871 _eglError(EGL_BAD_PARAMETER
,
1872 "dri2_create_image_khr: unsupported pixmap depth");
1877 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1885 return dri2_create_image_from_dri(disp
, dri_image
);
1889 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
1896 * "Required attributes and their values are as follows:
1898 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1900 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1901 * by drm_fourcc.h and used as the pixel_format parameter of the
1902 * drm_mode_fb_cmd2 ioctl."
1906 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1907 * incomplete, EGL_BAD_PARAMETER is generated."
1909 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
1910 !attrs
->DMABufFourCC
.IsPresent
) {
1911 _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
1918 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1919 * specified for a plane's pitch or offset isn't supported by EGL,
1920 * EGL_BAD_ACCESS is generated."
1922 for (i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
1923 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
1924 attrs
->DMABufPlanePitches
[i
].Value
<= 0) {
1925 _eglError(EGL_BAD_ACCESS
, "invalid pitch");
1933 /* Returns the total number of file descriptors. Zero indicates an error. */
1935 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
1937 unsigned i
, plane_n
;
1939 switch (attrs
->DMABufFourCC
.Value
) {
1941 case DRM_FORMAT_RG88
:
1942 case DRM_FORMAT_GR88
:
1943 case DRM_FORMAT_RGB332
:
1944 case DRM_FORMAT_BGR233
:
1945 case DRM_FORMAT_XRGB4444
:
1946 case DRM_FORMAT_XBGR4444
:
1947 case DRM_FORMAT_RGBX4444
:
1948 case DRM_FORMAT_BGRX4444
:
1949 case DRM_FORMAT_ARGB4444
:
1950 case DRM_FORMAT_ABGR4444
:
1951 case DRM_FORMAT_RGBA4444
:
1952 case DRM_FORMAT_BGRA4444
:
1953 case DRM_FORMAT_XRGB1555
:
1954 case DRM_FORMAT_XBGR1555
:
1955 case DRM_FORMAT_RGBX5551
:
1956 case DRM_FORMAT_BGRX5551
:
1957 case DRM_FORMAT_ARGB1555
:
1958 case DRM_FORMAT_ABGR1555
:
1959 case DRM_FORMAT_RGBA5551
:
1960 case DRM_FORMAT_BGRA5551
:
1961 case DRM_FORMAT_RGB565
:
1962 case DRM_FORMAT_BGR565
:
1963 case DRM_FORMAT_RGB888
:
1964 case DRM_FORMAT_BGR888
:
1965 case DRM_FORMAT_XRGB8888
:
1966 case DRM_FORMAT_XBGR8888
:
1967 case DRM_FORMAT_RGBX8888
:
1968 case DRM_FORMAT_BGRX8888
:
1969 case DRM_FORMAT_ARGB8888
:
1970 case DRM_FORMAT_ABGR8888
:
1971 case DRM_FORMAT_RGBA8888
:
1972 case DRM_FORMAT_BGRA8888
:
1973 case DRM_FORMAT_XRGB2101010
:
1974 case DRM_FORMAT_XBGR2101010
:
1975 case DRM_FORMAT_RGBX1010102
:
1976 case DRM_FORMAT_BGRX1010102
:
1977 case DRM_FORMAT_ARGB2101010
:
1978 case DRM_FORMAT_ABGR2101010
:
1979 case DRM_FORMAT_RGBA1010102
:
1980 case DRM_FORMAT_BGRA1010102
:
1981 case DRM_FORMAT_YUYV
:
1982 case DRM_FORMAT_YVYU
:
1983 case DRM_FORMAT_UYVY
:
1984 case DRM_FORMAT_VYUY
:
1987 case DRM_FORMAT_NV12
:
1988 case DRM_FORMAT_NV21
:
1989 case DRM_FORMAT_NV16
:
1990 case DRM_FORMAT_NV61
:
1993 case DRM_FORMAT_YUV410
:
1994 case DRM_FORMAT_YVU410
:
1995 case DRM_FORMAT_YUV411
:
1996 case DRM_FORMAT_YVU411
:
1997 case DRM_FORMAT_YUV420
:
1998 case DRM_FORMAT_YVU420
:
1999 case DRM_FORMAT_YUV422
:
2000 case DRM_FORMAT_YVU422
:
2001 case DRM_FORMAT_YUV444
:
2002 case DRM_FORMAT_YVU444
:
2006 _eglError(EGL_BAD_ATTRIBUTE
, "invalid format");
2013 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2014 * incomplete, EGL_BAD_PARAMETER is generated."
2016 for (i
= 0; i
< plane_n
; ++i
) {
2017 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2018 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2019 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2020 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2026 * The spec also says:
2028 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2029 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2030 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2031 * attributes are specified."
2033 for (i
= plane_n
; i
< 3; ++i
) {
2034 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2035 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2036 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2037 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2048 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2049 * EGL will take a reference to the dma_buf(s) which it will release at any
2050 * time while the EGLDisplay is initialized. It is the responsibility of the
2051 * application to close the dma_buf file descriptors."
2053 * Therefore we must never close or otherwise modify the file descriptors.
2056 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2057 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2059 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2062 _EGLImageAttribs attrs
;
2063 __DRIimage
*dri_image
;
2074 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2075 * error EGL_BAD_PARAMETER is generated."
2077 if (buffer
!= NULL
) {
2078 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2082 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2083 if (err
!= EGL_SUCCESS
) {
2084 _eglError(err
, "bad attribute");
2088 if (!dri2_check_dma_buf_attribs(&attrs
))
2091 num_fds
= dri2_check_dma_buf_format(&attrs
);
2095 for (i
= 0; i
< num_fds
; ++i
) {
2096 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2097 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2098 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2102 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2103 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2104 fds
, num_fds
, pitches
, offsets
,
2105 attrs
.DMABufYuvColorSpaceHint
.Value
,
2106 attrs
.DMABufSampleRangeHint
.Value
,
2107 attrs
.DMABufChromaHorizontalSiting
.Value
,
2108 attrs
.DMABufChromaVerticalSiting
.Value
,
2111 dri2_create_image_khr_texture_error(error
);
2114 return EGL_NO_IMAGE_KHR
;
2116 res
= dri2_create_image_from_dri(disp
, dri_image
);
2121 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2122 const EGLint
*attr_list
)
2124 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2125 struct dri2_egl_image
*dri2_img
;
2126 _EGLImageAttribs attrs
;
2127 unsigned int dri_use
, valid_mask
;
2129 EGLint err
= EGL_SUCCESS
;
2133 dri2_img
= malloc(sizeof *dri2_img
);
2135 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2136 return EGL_NO_IMAGE_KHR
;
2140 err
= EGL_BAD_PARAMETER
;
2144 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
2145 err
= EGL_BAD_PARAMETER
;
2149 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2150 if (err
!= EGL_SUCCESS
)
2153 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2154 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)",
2155 attrs
.Width
, attrs
.Height
);
2159 switch (attrs
.DRMBufferFormatMESA
) {
2160 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2161 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2164 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x",
2165 attrs
.DRMBufferFormatMESA
);
2170 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2171 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2172 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2173 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2174 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x",
2175 attrs
.DRMBufferUseMESA
& ~valid_mask
);
2180 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2181 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2182 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2183 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2184 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2185 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2187 dri2_img
->dri_image
=
2188 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2189 attrs
.Width
, attrs
.Height
,
2190 format
, dri_use
, dri2_img
);
2191 if (dri2_img
->dri_image
== NULL
) {
2192 err
= EGL_BAD_ALLOC
;
2196 return &dri2_img
->base
;
2200 _eglError(err
, "dri2_create_drm_image_mesa");
2202 return EGL_NO_IMAGE_KHR
;
2206 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2207 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2209 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2210 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2214 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2215 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
2216 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2221 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2222 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2225 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2226 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2232 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2234 EGLint
*fourcc
, EGLint
*nplanes
,
2235 EGLuint64KHR
*modifiers
)
2237 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2238 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2244 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2245 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2247 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2248 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2257 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2258 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2260 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2261 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2265 /* rework later to provide multiple fds/strides/offsets */
2267 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2268 __DRI_IMAGE_ATTRIB_FD
, fds
);
2271 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2272 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2276 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2277 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2279 offsets
[0] = img_offset
;
2290 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2291 _EGLContext
*ctx
, EGLenum target
,
2292 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2297 case EGL_GL_TEXTURE_2D_KHR
:
2298 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2299 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2300 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2301 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2302 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2303 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2304 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2305 case EGL_GL_TEXTURE_3D_KHR
:
2306 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
2307 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2310 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2311 return EGL_NO_IMAGE_KHR
;
2313 case EGL_GL_RENDERBUFFER_KHR
:
2314 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2316 case EGL_DRM_BUFFER_MESA
:
2317 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2318 case EGL_LINUX_DMA_BUF_EXT
:
2319 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2321 #ifdef HAVE_WAYLAND_PLATFORM
2322 case EGL_WAYLAND_BUFFER_WL
:
2323 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2326 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2327 return EGL_NO_IMAGE_KHR
;
2332 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2334 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2335 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2339 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2345 #ifdef HAVE_WAYLAND_PLATFORM
2348 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2349 struct wl_drm_buffer
*buffer
)
2351 _EGLDisplay
*disp
= user_data
;
2352 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2354 int i
, dri_components
= 0;
2357 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2366 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2378 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2380 buffer
->driver_format
= NULL
;
2381 for (i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2382 if (wl_drm_components
[i
].dri_components
== dri_components
)
2383 buffer
->driver_format
= &wl_drm_components
[i
];
2385 if (buffer
->driver_format
== NULL
)
2386 dri2_dpy
->image
->destroyImage(img
);
2388 buffer
->driver_buffer
= img
;
2392 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2394 _EGLDisplay
*disp
= user_data
;
2395 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2397 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2400 static struct wayland_drm_callbacks wl_drm_callbacks
= {
2401 .authenticate
= NULL
,
2402 .reference_buffer
= dri2_wl_reference_buffer
,
2403 .release_buffer
= dri2_wl_release_buffer
2407 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2408 struct wl_display
*wl_dpy
)
2410 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2416 if (dri2_dpy
->wl_server_drm
)
2419 wl_drm_callbacks
.authenticate
=
2420 (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
;
2422 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2423 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2424 dri2_dpy
->image
->base
.version
>= 7 &&
2425 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2426 flags
|= WAYLAND_DRM_PRIME
;
2428 dri2_dpy
->wl_server_drm
=
2429 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2430 &wl_drm_callbacks
, disp
, flags
);
2432 if (!dri2_dpy
->wl_server_drm
)
2435 #ifdef HAVE_DRM_PLATFORM
2436 /* We have to share the wl_drm instance with gbm, so gbm can convert
2437 * wl_buffers to gbm bos. */
2438 if (dri2_dpy
->gbm_dri
)
2439 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2446 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2447 struct wl_display
*wl_dpy
)
2449 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2453 if (!dri2_dpy
->wl_server_drm
)
2456 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2457 dri2_dpy
->wl_server_drm
= NULL
;
2463 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2464 struct wl_resource
*buffer_resource
,
2465 EGLint attribute
, EGLint
*value
)
2467 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2468 struct wl_drm_buffer
*buffer
;
2469 const struct wl_drm_components_descriptor
*format
;
2471 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2475 format
= buffer
->driver_format
;
2476 switch (attribute
) {
2477 case EGL_TEXTURE_FORMAT
:
2478 *value
= format
->components
;
2481 *value
= buffer
->width
;
2484 *value
= buffer
->height
;
2493 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2495 p_atomic_inc(&sync
->refcount
);
2499 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2500 struct dri2_egl_sync
*dri2_sync
)
2502 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2503 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
)
2504 cnd_destroy(&dri2_sync
->cond
);
2506 if (dri2_sync
->fence
)
2507 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2514 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
2515 EGLenum type
, const EGLAttrib
*attrib_list
)
2517 _EGLContext
*ctx
= _eglGetCurrentContext();
2518 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2519 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2520 struct dri2_egl_sync
*dri2_sync
;
2522 pthread_condattr_t attr
;
2524 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2526 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2530 if (!_eglInitSync(&dri2_sync
->base
, dpy
, type
, attrib_list
)) {
2536 case EGL_SYNC_FENCE_KHR
:
2537 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2538 if (!dri2_sync
->fence
) {
2539 /* Why did it fail? DRI doesn't return an error code, so we emit
2540 * a generic EGL error that doesn't communicate user error.
2542 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2548 case EGL_SYNC_CL_EVENT_KHR
:
2549 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2550 dri2_dpy
->dri_screen
,
2551 dri2_sync
->base
.CLEvent
);
2552 /* this can only happen if the cl_event passed in is invalid. */
2553 if (!dri2_sync
->fence
) {
2554 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2559 /* the initial status must be "signaled" if the cl_event is signaled */
2560 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2561 dri2_sync
->fence
, 0, 0))
2562 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2565 case EGL_SYNC_REUSABLE_KHR
:
2566 /* intialize attr */
2567 ret
= pthread_condattr_init(&attr
);
2570 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2575 /* change clock attribute to CLOCK_MONOTONIC */
2576 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
2579 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2584 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
2587 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2592 /* initial status of reusable sync must be "unsignaled" */
2593 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
2597 p_atomic_set(&dri2_sync
->refcount
, 1);
2598 return &dri2_sync
->base
;
2602 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2604 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2605 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2606 EGLint ret
= EGL_TRUE
;
2609 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
2610 * then unlock all threads possibly blocked by the reusable sync before
2613 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
2614 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2615 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2616 /* unblock all threads currently blocked by sync */
2617 err
= cnd_broadcast(&dri2_sync
->cond
);
2620 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
2624 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2630 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2631 EGLint flags
, EGLTime timeout
)
2633 _EGLContext
*ctx
= _eglGetCurrentContext();
2634 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2635 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2636 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2637 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2638 unsigned wait_flags
= 0;
2640 /* timespecs for cnd_timedwait */
2641 struct timespec current
;
2644 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
2646 /* The EGL_KHR_fence_sync spec states:
2648 * "If no context is current for the bound API,
2649 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
2651 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
2652 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
2654 /* the sync object should take a reference while waiting */
2655 dri2_egl_ref_sync(dri2_sync
);
2657 switch (sync
->Type
) {
2658 case EGL_SYNC_FENCE_KHR
:
2659 case EGL_SYNC_CL_EVENT_KHR
:
2660 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
2661 dri2_sync
->fence
, wait_flags
,
2663 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2665 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2668 case EGL_SYNC_REUSABLE_KHR
:
2669 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
2670 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
2671 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
2672 dri2_drv
->glFlush();
2675 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
2676 if (timeout
== EGL_FOREVER_KHR
) {
2677 mtx_lock(&dri2_sync
->mutex
);
2678 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
2679 mtx_unlock(&dri2_sync
->mutex
);
2681 /* if reusable sync has not been yet signaled */
2682 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
2683 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
2685 /* calculating when to expire */
2686 expire
.nsec
= timeout
% 1000000000L;
2687 expire
.sec
= timeout
/ 1000000000L;
2689 expire
.nsec
+= current
.tv_nsec
;
2690 expire
.sec
+= current
.tv_sec
;
2692 /* expire.nsec now is a number between 0 and 1999999998 */
2693 if (expire
.nsec
> 999999999L) {
2695 expire
.nsec
-= 1000000000L;
2698 mtx_lock(&dri2_sync
->mutex
);
2699 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
2700 mtx_unlock(&dri2_sync
->mutex
);
2702 if (ret
== thrd_busy
) {
2703 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2704 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2706 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2714 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2720 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2723 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2726 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
) {
2727 _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
2731 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
) {
2732 _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
2736 dri2_sync
->base
.SyncStatus
= mode
;
2738 if (mode
== EGL_SIGNALED_KHR
) {
2739 ret
= cnd_broadcast(&dri2_sync
->cond
);
2741 /* fail to broadcast */
2743 _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
2752 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2754 _EGLContext
*ctx
= _eglGetCurrentContext();
2755 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2756 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2757 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2759 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
2760 dri2_sync
->fence
, 0);
2765 dri2_interop_query_device_info(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2766 struct mesa_glinterop_device_info
*out
)
2768 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2769 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2771 if (!dri2_dpy
->interop
)
2772 return MESA_GLINTEROP_UNSUPPORTED
;
2774 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
2778 dri2_interop_export_object(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2779 struct mesa_glinterop_export_in
*in
,
2780 struct mesa_glinterop_export_out
*out
)
2782 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2783 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2785 if (!dri2_dpy
->interop
)
2786 return MESA_GLINTEROP_UNSUPPORTED
;
2788 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
2792 dri2_unload(_EGLDriver
*drv
)
2794 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2796 dlclose(dri2_drv
->handle
);
2801 dri2_load(_EGLDriver
*drv
)
2803 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2804 #ifdef HAVE_ANDROID_PLATFORM
2805 const char *libname
= "libglapi.so";
2806 #elif defined(__APPLE__)
2807 const char *libname
= "libglapi.0.dylib";
2808 #elif defined(__CYGWIN__)
2809 const char *libname
= "cygglapi-0.dll";
2810 #elif defined(__linux__)
2811 const char *libname
= "libglapi.so.0";
2813 #error Unknown glapi provider for this platform
2817 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2818 handle
= dlopen(libname
, RTLD_LAZY
| RTLD_GLOBAL
);
2820 _eglLog(_EGL_WARNING
, "DRI2: failed to open glapi provider");
2824 dri2_drv
->get_proc_address
= (_EGLProc (*)(const char *))
2825 dlsym(handle
, "_glapi_get_proc_address");
2827 /* if glapi is not available, loading DRI drivers will fail */
2828 if (!dri2_drv
->get_proc_address
) {
2829 _eglLog(_EGL_WARNING
, "DRI2: failed to find _glapi_get_proc_address");
2833 dri2_drv
->glFlush
= (void (*)(void))
2834 dri2_drv
->get_proc_address("glFlush");
2836 /* if glFlush is not available things are horribly broken */
2837 if (!dri2_drv
->glFlush
) {
2838 _eglLog(_EGL_WARNING
, "DRI2: failed to find glFlush entry point");
2842 dri2_drv
->handle
= handle
;
2852 * This is the main entrypoint into the driver, called by libEGL.
2853 * Create a new _EGLDriver object and init its dispatch table.
2856 _eglBuiltInDriverDRI2(const char *args
)
2858 struct dri2_egl_driver
*dri2_drv
;
2862 dri2_drv
= calloc(1, sizeof *dri2_drv
);
2866 if (!dri2_load(&dri2_drv
->base
)) {
2871 _eglInitDriverFallbacks(&dri2_drv
->base
);
2872 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
2873 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
2874 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
2875 dri2_drv
->base
.API
.DestroyContext
= dri2_destroy_context
;
2876 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
2877 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
2878 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
2879 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
2880 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
2881 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
2882 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
2883 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
2884 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
2885 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
2886 dri2_drv
->base
.API
.SwapInterval
= dri2_swap_interval
;
2887 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
2888 dri2_drv
->base
.API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
2889 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
2890 dri2_drv
->base
.API
.PostSubBufferNV
= dri2_post_sub_buffer
;
2891 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
,
2892 dri2_drv
->base
.API
.QueryBufferAge
= dri2_query_buffer_age
;
2893 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image
;
2894 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
2895 dri2_drv
->base
.API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
2896 dri2_drv
->base
.API
.QuerySurface
= dri2_query_surface
;
2898 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
2899 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
2900 dri2_drv
->base
.API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
2901 dri2_drv
->base
.API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
2903 #ifdef HAVE_WAYLAND_PLATFORM
2904 dri2_drv
->base
.API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
2905 dri2_drv
->base
.API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
2906 dri2_drv
->base
.API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
2908 dri2_drv
->base
.API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
2909 dri2_drv
->base
.API
.CreateSyncKHR
= dri2_create_sync
;
2910 dri2_drv
->base
.API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
2911 dri2_drv
->base
.API
.SignalSyncKHR
= dri2_signal_sync
;
2912 dri2_drv
->base
.API
.WaitSyncKHR
= dri2_server_wait_sync
;
2913 dri2_drv
->base
.API
.DestroySyncKHR
= dri2_destroy_sync
;
2914 dri2_drv
->base
.API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
2915 dri2_drv
->base
.API
.GLInteropExportObject
= dri2_interop_export_object
;
2917 dri2_drv
->base
.Name
= "DRI2";
2918 dri2_drv
->base
.Unload
= dri2_unload
;
2920 return &dri2_drv
->base
;