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
)
244 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH
:
245 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_WIDTH
,
246 _EGL_MAX_PBUFFER_WIDTH
);
248 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT
:
249 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_HEIGHT
,
250 _EGL_MAX_PBUFFER_HEIGHT
);
254 key
= dri2_to_egl_attribute_map
[attrib
];
256 _eglSetConfigKey(&base
, key
, value
);
262 for (i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
263 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
265 if (rgba_masks
&& memcmp(rgba_masks
, dri_masks
, sizeof(dri_masks
)))
268 base
.NativeRenderable
= EGL_TRUE
;
270 base
.SurfaceType
= surface_type
;
271 if (surface_type
& (EGL_PBUFFER_BIT
|
272 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
273 base
.BindToTextureRGB
= bind_to_texture_rgb
;
274 if (base
.AlphaSize
> 0)
275 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
278 base
.RenderableType
= disp
->ClientAPIs
;
279 base
.Conformant
= disp
->ClientAPIs
;
281 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
282 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
284 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
285 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
289 config_id
= base
.ConfigID
;
290 base
.ConfigID
= EGL_DONT_CARE
;
291 base
.SurfaceType
= EGL_DONT_CARE
;
292 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
293 (_EGLArrayForEach
) dri2_match_config
, &base
);
295 if (num_configs
== 1) {
296 conf
= (struct dri2_egl_config
*) matching_config
;
298 if (double_buffer
&& !conf
->dri_double_config
[srgb
])
299 conf
->dri_double_config
[srgb
] = dri_config
;
300 else if (!double_buffer
&& !conf
->dri_single_config
[srgb
])
301 conf
->dri_single_config
[srgb
] = dri_config
;
303 /* a similar config type is already added (unlikely) => discard */
306 else if (num_configs
== 0) {
307 conf
= calloc(1, sizeof *conf
);
312 conf
->dri_double_config
[srgb
] = dri_config
;
314 conf
->dri_single_config
[srgb
] = dri_config
;
316 memcpy(&conf
->base
, &base
, sizeof base
);
317 conf
->base
.SurfaceType
= 0;
318 conf
->base
.ConfigID
= config_id
;
320 _eglLinkConfig(&conf
->base
);
328 surface_type
&= ~EGL_PIXMAP_BIT
;
331 /* No support for pbuffer + MSAA for now.
333 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
334 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
337 surface_type
&= ~EGL_PBUFFER_BIT
;
340 conf
->base
.SurfaceType
|= surface_type
;
346 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
348 _EGLDisplay
*disp
= data
;
349 struct dri2_egl_image
*dri2_img
;
354 img
= _eglLookupImage(image
, disp
);
356 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
360 dri2_img
= dri2_egl_image(image
);
362 return dri2_img
->dri_image
;
365 const __DRIimageLookupExtension image_lookup_extension
= {
366 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
368 .lookupEGLImage
= dri2_lookup_egl_image
371 struct dri2_extension_match
{
377 static const struct dri2_extension_match dri3_driver_extensions
[] = {
378 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
379 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
383 static const struct dri2_extension_match dri2_driver_extensions
[] = {
384 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
385 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
389 static const struct dri2_extension_match dri2_core_extensions
[] = {
390 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
391 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
392 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
396 static const struct dri2_extension_match swrast_driver_extensions
[] = {
397 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
398 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
402 static const struct dri2_extension_match swrast_core_extensions
[] = {
403 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
407 static const struct dri2_extension_match optional_core_extensions
[] = {
408 { __DRI2_ROBUSTNESS
, 1, offsetof(struct dri2_egl_display
, robustness
) },
409 { __DRI2_CONFIG_QUERY
, 1, offsetof(struct dri2_egl_display
, config
) },
410 { __DRI2_FENCE
, 1, offsetof(struct dri2_egl_display
, fence
) },
411 { __DRI2_RENDERER_QUERY
, 1, offsetof(struct dri2_egl_display
, rendererQuery
) },
412 { __DRI2_INTEROP
, 1, offsetof(struct dri2_egl_display
, interop
) },
417 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
418 const struct dri2_extension_match
*matches
,
419 const __DRIextension
**extensions
,
422 int i
, j
, ret
= EGL_TRUE
;
425 for (i
= 0; extensions
[i
]; i
++) {
426 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
427 for (j
= 0; matches
[j
].name
; j
++) {
428 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
429 extensions
[i
]->version
>= matches
[j
].version
) {
430 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
431 *(const __DRIextension
**) field
= extensions
[i
];
432 _eglLog(_EGL_INFO
, "found extension %s version %d",
433 extensions
[i
]->name
, extensions
[i
]->version
);
439 for (j
= 0; matches
[j
].name
; j
++) {
440 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
441 if (*(const __DRIextension
**) field
== NULL
) {
443 _eglLog(_EGL_DEBUG
, "did not find optional extension %s version %d",
444 matches
[j
].name
, matches
[j
].version
);
446 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
447 matches
[j
].name
, matches
[j
].version
);
456 static const __DRIextension
**
457 dri2_open_driver(_EGLDisplay
*disp
)
459 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
460 const __DRIextension
**extensions
= NULL
;
461 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
462 char *get_extensions_name
;
463 const __DRIextension
**(*get_extensions
)(void);
466 if (geteuid() == getuid()) {
467 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
468 search_paths
= getenv("LIBGL_DRIVERS_PATH");
470 if (search_paths
== NULL
)
471 search_paths
= DEFAULT_DRIVER_DIR
;
473 dri2_dpy
->driver
= NULL
;
474 end
= search_paths
+ strlen(search_paths
);
475 for (p
= search_paths
; p
< end
; p
= next
+ 1) {
477 next
= strchr(p
, ':');
483 snprintf(path
, sizeof path
,
484 "%.*s/tls/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
485 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
487 if (dri2_dpy
->driver
== NULL
) {
488 snprintf(path
, sizeof path
,
489 "%.*s/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
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());
494 /* not need continue to loop all paths once the driver is found */
495 if (dri2_dpy
->driver
!= NULL
)
499 snprintf(path
, sizeof path
, "%.*s/gallium_dri.so", len
, p
);
500 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
501 if (dri2_dpy
->driver
== NULL
)
502 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
508 if (dri2_dpy
->driver
== NULL
) {
509 _eglLog(_EGL_WARNING
,
510 "DRI2: failed to open %s (search paths %s)",
511 dri2_dpy
->driver_name
, search_paths
);
515 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
517 get_extensions_name
= dri_get_extensions_name(dri2_dpy
->driver_name
);
519 if (get_extensions_name
) {
520 get_extensions
= dlsym(dri2_dpy
->driver
, get_extensions_name
);
521 if (get_extensions
) {
522 extensions
= get_extensions();
524 _eglLog(_EGL_DEBUG
, "driver does not expose %s(): %s\n",
525 get_extensions_name
, dlerror());
527 free(get_extensions_name
);
531 extensions
= dlsym(dri2_dpy
->driver
, __DRI_DRIVER_EXTENSIONS
);
532 if (extensions
== NULL
) {
533 _eglLog(_EGL_WARNING
,
534 "DRI2: driver exports no extensions (%s)", dlerror());
535 dlclose(dri2_dpy
->driver
);
542 dri2_load_driver_dri3(_EGLDisplay
*disp
)
544 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
545 const __DRIextension
**extensions
;
547 extensions
= dri2_open_driver(disp
);
551 if (!dri2_bind_extensions(dri2_dpy
, dri3_driver_extensions
, extensions
, false)) {
552 dlclose(dri2_dpy
->driver
);
555 dri2_dpy
->driver_extensions
= extensions
;
561 dri2_load_driver(_EGLDisplay
*disp
)
563 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
564 const __DRIextension
**extensions
;
566 extensions
= dri2_open_driver(disp
);
570 if (!dri2_bind_extensions(dri2_dpy
, dri2_driver_extensions
, extensions
, false)) {
571 dlclose(dri2_dpy
->driver
);
574 dri2_dpy
->driver_extensions
= extensions
;
580 dri2_load_driver_swrast(_EGLDisplay
*disp
)
582 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
583 const __DRIextension
**extensions
;
585 extensions
= dri2_open_driver(disp
);
589 if (!dri2_bind_extensions(dri2_dpy
, swrast_driver_extensions
, extensions
, false)) {
590 dlclose(dri2_dpy
->driver
);
593 dri2_dpy
->driver_extensions
= extensions
;
599 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
601 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
602 unsigned int value
= 0;
604 if (!rendererQuery
||
605 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
612 dri2_setup_screen(_EGLDisplay
*disp
)
614 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
615 unsigned int api_mask
;
617 if (dri2_dpy
->image_driver
) {
618 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
619 } else if (dri2_dpy
->dri2
) {
620 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
622 assert(dri2_dpy
->swrast
);
623 api_mask
= 1 << __DRI_API_OPENGL
|
624 1 << __DRI_API_GLES
|
625 1 << __DRI_API_GLES2
|
626 1 << __DRI_API_GLES3
;
629 disp
->ClientAPIs
= 0;
630 if ((api_mask
& (1 <<__DRI_API_OPENGL
)) && _eglIsApiValid(EGL_OPENGL_API
))
631 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
632 if ((api_mask
& (1 << __DRI_API_GLES
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
633 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
634 if ((api_mask
& (1 << __DRI_API_GLES2
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
635 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
636 if ((api_mask
& (1 << __DRI_API_GLES3
)) && _eglIsApiValid(EGL_OPENGL_ES_API
))
637 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
639 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
640 disp
->Extensions
.KHR_no_config_context
= EGL_TRUE
;
641 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
643 if (dri2_renderer_query_integer(dri2_dpy
,
644 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
645 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
647 if (dri2_dpy
->image_driver
||
648 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
649 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
650 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
652 if (dri2_dpy
->robustness
)
653 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
656 if (dri2_dpy
->fence
) {
657 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
658 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
659 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
660 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
663 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
665 if (dri2_dpy
->image
) {
666 if (dri2_dpy
->image
->base
.version
>= 10 &&
667 dri2_dpy
->image
->getCapabilities
!= NULL
) {
670 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
671 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
673 if (dri2_dpy
->image
->base
.version
>= 11)
674 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
676 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
677 if (dri2_dpy
->image
->base
.version
>= 11)
678 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
681 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
682 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
683 if (dri2_dpy
->image
->base
.version
>= 5 &&
684 dri2_dpy
->image
->createImageFromTexture
) {
685 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
686 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
688 if (dri2_renderer_query_integer(dri2_dpy
,
689 __DRI2_RENDERER_HAS_TEXTURE_3D
))
690 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
692 if (dri2_dpy
->image
->base
.version
>= 8 &&
693 dri2_dpy
->image
->createImageFromDmaBufs
) {
694 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
700 /* All platforms but DRM call this function to create the screen, query the
701 * dri extensions, setup the vtables and populate the driver_configs.
702 * DRM inherits all that information from its display - GBM.
705 dri2_create_screen(_EGLDisplay
*disp
)
707 const __DRIextension
**extensions
;
708 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
710 if (dri2_dpy
->image_driver
) {
711 dri2_dpy
->dri_screen
=
712 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
713 dri2_dpy
->loader_extensions
,
714 dri2_dpy
->driver_extensions
,
715 &dri2_dpy
->driver_configs
,
717 } else if (dri2_dpy
->dri2
) {
718 if (dri2_dpy
->dri2
->base
.version
>= 4) {
719 dri2_dpy
->dri_screen
=
720 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
721 dri2_dpy
->loader_extensions
,
722 dri2_dpy
->driver_extensions
,
723 &dri2_dpy
->driver_configs
, disp
);
725 dri2_dpy
->dri_screen
=
726 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
727 dri2_dpy
->loader_extensions
,
728 &dri2_dpy
->driver_configs
, disp
);
731 assert(dri2_dpy
->swrast
);
732 if (dri2_dpy
->swrast
->base
.version
>= 4) {
733 dri2_dpy
->dri_screen
=
734 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->loader_extensions
,
735 dri2_dpy
->driver_extensions
,
736 &dri2_dpy
->driver_configs
, disp
);
738 dri2_dpy
->dri_screen
=
739 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->loader_extensions
,
740 &dri2_dpy
->driver_configs
, disp
);
744 if (dri2_dpy
->dri_screen
== NULL
) {
745 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
749 dri2_dpy
->own_dri_screen
= 1;
751 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
753 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
) {
754 if (!dri2_bind_extensions(dri2_dpy
, dri2_core_extensions
, extensions
, false))
755 goto cleanup_dri_screen
;
757 assert(dri2_dpy
->swrast
);
758 if (!dri2_bind_extensions(dri2_dpy
, swrast_core_extensions
, extensions
, false))
759 goto cleanup_dri_screen
;
762 dri2_bind_extensions(dri2_dpy
, optional_core_extensions
, extensions
, true);
763 dri2_setup_screen(disp
);
768 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
774 * Called via eglInitialize(), GLX_drv->API.Initialize().
776 * This must be guaranteed to be called exactly once, even if eglInitialize is
777 * called many times (without a eglTerminate in between).
780 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
782 EGLBoolean ret
= EGL_FALSE
;
783 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
785 /* In the case where the application calls eglMakeCurrent(context1),
786 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
787 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
788 * initialized, as we need it to be able to free context1 correctly.
790 * It would probably be safest to forcibly release the display with
791 * dri2_display_release, to make sure the display is reinitialized correctly.
792 * However, the EGL spec states that we need to keep a reference to the
793 * current context (so we cannot call dri2_make_current(NULL)), and therefore
794 * we would leak context1 as we would be missing the old display connection
795 * to free it up correctly.
798 dri2_dpy
->ref_count
++;
802 /* not until swrast_dri is supported */
803 if (disp
->Options
.UseFallback
)
806 /* Nothing to initialize for a test only display */
807 if (disp
->Options
.TestOnly
)
810 switch (disp
->Platform
) {
811 #ifdef HAVE_SURFACELESS_PLATFORM
812 case _EGL_PLATFORM_SURFACELESS
:
813 ret
= dri2_initialize_surfaceless(drv
, disp
);
816 #ifdef HAVE_X11_PLATFORM
817 case _EGL_PLATFORM_X11
:
818 ret
= dri2_initialize_x11(drv
, disp
);
821 #ifdef HAVE_DRM_PLATFORM
822 case _EGL_PLATFORM_DRM
:
823 ret
= dri2_initialize_drm(drv
, disp
);
826 #ifdef HAVE_WAYLAND_PLATFORM
827 case _EGL_PLATFORM_WAYLAND
:
828 ret
= dri2_initialize_wayland(drv
, disp
);
831 #ifdef HAVE_ANDROID_PLATFORM
832 case _EGL_PLATFORM_ANDROID
:
833 ret
= dri2_initialize_android(drv
, disp
);
837 _eglLog(_EGL_WARNING
, "No EGL platform enabled.");
842 dri2_dpy
= dri2_egl_display(disp
);
848 dri2_dpy
->ref_count
++;
855 * Decrement display reference count, and free up display if necessary.
858 dri2_display_release(_EGLDisplay
*disp
)
860 struct dri2_egl_display
*dri2_dpy
;
866 dri2_dpy
= dri2_egl_display(disp
);
868 assert(dri2_dpy
->ref_count
> 0);
869 dri2_dpy
->ref_count
--;
871 if (dri2_dpy
->ref_count
> 0)
874 _eglCleanupDisplay(disp
);
876 if (dri2_dpy
->own_dri_screen
)
877 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
878 if (dri2_dpy
->fd
>= 0)
880 if (dri2_dpy
->driver
)
881 dlclose(dri2_dpy
->driver
);
882 free(dri2_dpy
->driver_name
);
884 #ifdef HAVE_WAYLAND_PLATFORM
885 free(dri2_dpy
->device_name
);
888 switch (disp
->Platform
) {
889 #ifdef HAVE_X11_PLATFORM
890 case _EGL_PLATFORM_X11
:
891 if (dri2_dpy
->own_device
) {
892 xcb_disconnect(dri2_dpy
->conn
);
896 #ifdef HAVE_DRM_PLATFORM
897 case _EGL_PLATFORM_DRM
:
898 if (dri2_dpy
->own_device
) {
899 gbm_device_destroy(&dri2_dpy
->gbm_dri
->base
.base
);
903 #ifdef HAVE_WAYLAND_PLATFORM
904 case _EGL_PLATFORM_WAYLAND
:
905 if (dri2_dpy
->wl_drm
)
906 wl_drm_destroy(dri2_dpy
->wl_drm
);
907 if (dri2_dpy
->wl_shm
)
908 wl_shm_destroy(dri2_dpy
->wl_shm
);
909 wl_registry_destroy(dri2_dpy
->wl_registry
);
910 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
911 if (dri2_dpy
->own_device
) {
912 wl_display_disconnect(dri2_dpy
->wl_dpy
);
920 /* The drm platform does not create the screen/driver_configs but reuses
921 * the ones from the gbm device. As such the gbm itself is responsible
924 if (disp
->Platform
!= _EGL_PLATFORM_DRM
) {
925 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
926 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
927 free(dri2_dpy
->driver_configs
);
930 disp
->DriverData
= NULL
;
934 * Called via eglTerminate(), drv->API.Terminate().
936 * This must be guaranteed to be called exactly once, even if eglTerminate is
937 * called many times (without a eglInitialize in between).
940 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
942 /* Release all non-current Context/Surfaces. */
943 _eglReleaseDisplayResources(drv
, disp
);
945 dri2_display_release(disp
);
951 * Set the error code after a call to
952 * dri2_egl_display::dri2::createContextAttribs.
955 dri2_create_context_attribs_error(int dri_error
)
960 case __DRI_CTX_ERROR_SUCCESS
:
963 case __DRI_CTX_ERROR_NO_MEMORY
:
964 egl_error
= EGL_BAD_ALLOC
;
967 /* From the EGL_KHR_create_context spec, section "Errors":
969 * * If <config> does not support a client API context compatible
970 * with the requested API major and minor version, [...] context flags,
971 * and context reset notification behavior (for client API types where
972 * these attributes are supported), then an EGL_BAD_MATCH error is
975 * * If an OpenGL ES context is requested and the values for
976 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
977 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
978 * is not defined, than an EGL_BAD_MATCH error is generated.
980 * * If an OpenGL context is requested, the requested version is
981 * greater than 3.2, and the value for attribute
982 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
983 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
984 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
985 * one of these bits set; or if the implementation does not support
986 * the requested profile, then an EGL_BAD_MATCH error is generated.
988 case __DRI_CTX_ERROR_BAD_API
:
989 case __DRI_CTX_ERROR_BAD_VERSION
:
990 case __DRI_CTX_ERROR_BAD_FLAG
:
991 egl_error
= EGL_BAD_MATCH
;
994 /* From the EGL_KHR_create_context spec, section "Errors":
996 * * If an attribute name or attribute value in <attrib_list> is not
997 * recognized (including unrecognized bits in bitmask attributes),
998 * then an EGL_BAD_ATTRIBUTE error is generated."
1000 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
1001 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
1002 egl_error
= EGL_BAD_ATTRIBUTE
;
1007 egl_error
= EGL_BAD_MATCH
;
1011 _eglError(egl_error
, "dri2_create_context");
1015 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1016 struct dri2_egl_display
*dri2_dpy
,
1017 uint32_t *ctx_attribs
,
1018 unsigned *num_attribs
)
1022 assert(*num_attribs
>= 8);
1024 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1025 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1026 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1027 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1029 if (dri2_ctx
->base
.Flags
!= 0) {
1030 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1031 * extension, don't even try to send it the robust-access flag.
1032 * It may explode. Instead, generate the required EGL error here.
1034 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1035 && !dri2_dpy
->robustness
) {
1036 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1040 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1041 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
;
1044 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1045 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1046 * extension, don't even try to send it a reset strategy. It may
1047 * explode. Instead, generate the required EGL error here.
1049 if (!dri2_dpy
->robustness
) {
1050 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1054 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1055 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1064 * Called via eglCreateContext(), drv->API.CreateContext().
1066 static _EGLContext
*
1067 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1068 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1070 struct dri2_egl_context
*dri2_ctx
;
1071 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1072 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1073 __DRIcontext
*shared
=
1074 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1075 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1076 const __DRIconfig
*dri_config
;
1081 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1083 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1087 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1090 switch (dri2_ctx
->base
.ClientAPI
) {
1091 case EGL_OPENGL_ES_API
:
1092 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1094 api
= __DRI_API_GLES
;
1097 api
= __DRI_API_GLES2
;
1100 api
= __DRI_API_GLES3
;
1103 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1108 case EGL_OPENGL_API
:
1109 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1110 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1111 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1112 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1113 api
= __DRI_API_OPENGL_CORE
;
1115 api
= __DRI_API_OPENGL
;
1118 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1124 /* The config chosen here isn't necessarily
1125 * used for surfaces later.
1126 * A pixmap surface will use the single config.
1127 * This opportunity depends on disabling the
1128 * doubleBufferMode check in
1129 * src/mesa/main/context.c:check_compatible()
1131 if (dri2_config
->dri_double_config
[0])
1132 dri_config
= dri2_config
->dri_double_config
[0];
1134 dri_config
= dri2_config
->dri_single_config
[0];
1136 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
1137 * makes sure the back buffer will always be used.
1139 if (conf
->SurfaceType
& EGL_WINDOW_BIT
)
1140 dri2_ctx
->base
.WindowRenderBuffer
= EGL_BACK_BUFFER
;
1145 if (dri2_dpy
->image_driver
) {
1147 unsigned num_attribs
= 8;
1148 uint32_t ctx_attribs
[8];
1150 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1154 dri2_ctx
->dri_context
=
1155 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1163 dri2_create_context_attribs_error(error
);
1164 } else if (dri2_dpy
->dri2
) {
1165 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1167 unsigned num_attribs
= 8;
1168 uint32_t ctx_attribs
[8];
1170 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1174 dri2_ctx
->dri_context
=
1175 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1183 dri2_create_context_attribs_error(error
);
1185 dri2_ctx
->dri_context
=
1186 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1193 assert(dri2_dpy
->swrast
);
1194 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1196 unsigned num_attribs
= 8;
1197 uint32_t ctx_attribs
[8];
1199 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1203 dri2_ctx
->dri_context
=
1204 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1212 dri2_create_context_attribs_error(error
);
1214 dri2_ctx
->dri_context
=
1215 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1223 if (!dri2_ctx
->dri_context
)
1226 return &dri2_ctx
->base
;
1234 * Called via eglDestroyContext(), drv->API.DestroyContext().
1237 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1239 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1240 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1242 if (_eglPutContext(ctx
)) {
1243 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1251 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1253 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1255 if (!_eglPutSurface(surf
))
1258 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1262 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1265 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1266 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1268 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1269 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1270 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1271 _EGLContext
*old_ctx
;
1272 _EGLSurface
*old_dsurf
, *old_rsurf
;
1273 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1274 __DRIdrawable
*ddraw
, *rdraw
;
1279 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1281 /* make new bindings */
1282 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1283 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1287 /* flush before context switch */
1289 dri2_drv
->glFlush();
1291 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1292 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1293 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1296 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1297 dri2_dpy
->core
->unbindContext(old_cctx
);
1300 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1302 if (unbind
|| dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1303 dri2_destroy_surface(drv
, disp
, old_dsurf
);
1304 dri2_destroy_surface(drv
, disp
, old_rsurf
);
1307 dri2_dpy
->ref_count
++;
1309 EGLDisplay old_disp
= _eglGetDisplayHandle(old_ctx
->Resource
.Display
);
1310 dri2_destroy_context(drv
, disp
, old_ctx
);
1311 dri2_display_release(old_disp
);
1316 /* undo the previous _eglBindContext */
1317 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1318 assert(&dri2_ctx
->base
== ctx
&&
1319 tmp_dsurf
== dsurf
&&
1320 tmp_rsurf
== rsurf
);
1322 _eglPutSurface(dsurf
);
1323 _eglPutSurface(rsurf
);
1324 _eglPutContext(ctx
);
1326 _eglPutSurface(old_dsurf
);
1327 _eglPutSurface(old_rsurf
);
1328 _eglPutContext(old_ctx
);
1330 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1331 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1334 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1339 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1341 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1343 return dri2_surf
->dri_drawable
;
1347 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1350 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1352 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1354 return dri2_drv
->get_proc_address(procname
);
1358 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1359 _EGLConfig
*conf
, void *native_window
,
1360 const EGLint
*attrib_list
)
1362 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1363 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1368 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1369 _EGLConfig
*conf
, void *native_pixmap
,
1370 const EGLint
*attrib_list
)
1372 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1373 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1378 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1379 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1381 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1382 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
1386 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1389 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1390 return dri2_dpy
->vtbl
->swap_interval(drv
, dpy
, surf
, interval
);
1394 * Asks the client API to flush any rendering to the drawable so that we can
1395 * do our swapbuffers.
1398 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1400 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1401 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1403 if (dri2_dpy
->flush
) {
1404 if (dri2_dpy
->flush
->base
.version
>= 4) {
1405 /* We know there's a current context because:
1407 * "If surface is not bound to the calling thread’s current
1408 * context, an EGL_BAD_SURFACE error is generated."
1410 _EGLContext
*ctx
= _eglGetCurrentContext();
1411 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1413 /* From the EGL 1.4 spec (page 52):
1415 * "The contents of ancillary buffers are always undefined
1416 * after calling eglSwapBuffers."
1418 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1420 __DRI2_FLUSH_DRAWABLE
|
1421 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1422 __DRI2_THROTTLE_SWAPBUFFER
);
1424 dri2_dpy
->flush
->flush(dri_drawable
);
1430 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1432 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1433 return dri2_dpy
->vtbl
->swap_buffers(drv
, dpy
, surf
);
1437 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1439 const EGLint
*rects
, EGLint n_rects
)
1441 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1442 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, dpy
, surf
,
1447 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1448 EGLint numRects
, const EGLint
*rects
)
1450 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1451 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, dpy
, surf
, numRects
, rects
);
1455 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1456 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1458 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1459 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, dpy
, surf
, x
, y
, width
, height
);
1463 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1464 void *native_pixmap_target
)
1466 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1467 return dri2_dpy
->vtbl
->copy_buffers(drv
, dpy
, surf
, native_pixmap_target
);
1471 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1473 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1474 return dri2_dpy
->vtbl
->query_buffer_age(drv
, dpy
, surf
);
1478 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1480 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1481 _EGLSurface
*surf
= ctx
->DrawSurface
;
1482 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1486 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1487 * we need to copy fake to real here.*/
1489 if (dri2_dpy
->flush
!= NULL
)
1490 dri2_dpy
->flush
->flush(dri_drawable
);
1496 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1501 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1502 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1509 dri2_bind_tex_image(_EGLDriver
*drv
,
1510 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1512 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1513 struct dri2_egl_context
*dri2_ctx
;
1515 GLint format
, target
;
1516 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1518 ctx
= _eglGetCurrentContext();
1519 dri2_ctx
= dri2_egl_context(ctx
);
1521 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1524 switch (surf
->TextureFormat
) {
1525 case EGL_TEXTURE_RGB
:
1526 format
= __DRI_TEXTURE_FORMAT_RGB
;
1528 case EGL_TEXTURE_RGBA
:
1529 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1532 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1533 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1536 switch (surf
->TextureTarget
) {
1537 case EGL_TEXTURE_2D
:
1538 target
= GL_TEXTURE_2D
;
1541 target
= GL_TEXTURE_2D
;
1542 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1545 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1553 dri2_release_tex_image(_EGLDriver
*drv
,
1554 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1556 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1557 struct dri2_egl_context
*dri2_ctx
;
1560 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1562 ctx
= _eglGetCurrentContext();
1563 dri2_ctx
= dri2_egl_context(ctx
);
1565 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1568 switch (surf
->TextureTarget
) {
1569 case EGL_TEXTURE_2D
:
1570 target
= GL_TEXTURE_2D
;
1576 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1577 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1578 (*dri2_dpy
->tex_buffer
->releaseTexBuffer
)(dri2_ctx
->dri_context
,
1587 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
1588 EGLenum target
, EGLClientBuffer buffer
,
1589 const EGLint
*attr_list
)
1591 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1592 return dri2_dpy
->vtbl
->create_image(drv
, dpy
, ctx
, target
, buffer
,
1597 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1599 struct dri2_egl_image
*dri2_img
;
1601 if (dri_image
== NULL
) {
1602 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1606 dri2_img
= malloc(sizeof *dri2_img
);
1608 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1612 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1617 dri2_img
->dri_image
= dri_image
;
1619 return &dri2_img
->base
;
1623 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1624 EGLClientBuffer buffer
,
1625 const EGLint
*attr_list
)
1627 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1628 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1629 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1630 __DRIimage
*dri_image
;
1632 if (renderbuffer
== 0) {
1633 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1634 return EGL_NO_IMAGE_KHR
;
1638 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1639 renderbuffer
, NULL
);
1641 return dri2_create_image_from_dri(disp
, dri_image
);
1644 #ifdef HAVE_WAYLAND_PLATFORM
1646 /* This structure describes how a wl_buffer maps to one or more
1647 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1648 * offsets and strides of the planes in the buffer. This table maps a
1649 * wl_drm format code to a description of the planes in the buffer
1650 * that lets us create a __DRIimage for each of the planes. */
1652 static const struct wl_drm_components_descriptor
{
1653 uint32_t dri_components
;
1656 } wl_drm_components
[] = {
1657 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1658 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1659 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1660 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1661 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1665 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1666 EGLClientBuffer _buffer
,
1667 const EGLint
*attr_list
)
1669 struct wl_drm_buffer
*buffer
;
1670 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1671 const struct wl_drm_components_descriptor
*f
;
1672 __DRIimage
*dri_image
;
1673 _EGLImageAttribs attrs
;
1677 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1678 (struct wl_resource
*) _buffer
);
1682 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1683 plane
= attrs
.PlaneWL
;
1684 if (err
!= EGL_SUCCESS
) {
1685 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1689 f
= buffer
->driver_format
;
1690 if (plane
< 0 || plane
>= f
->nplanes
) {
1691 _eglError(EGL_BAD_PARAMETER
,
1692 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1696 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1698 if (dri_image
== NULL
) {
1699 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1703 return dri2_create_image_from_dri(disp
, dri_image
);
1708 dri2_get_sync_values_chromium(_EGLDisplay
*dpy
, _EGLSurface
*surf
,
1709 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1712 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1713 return dri2_dpy
->vtbl
->get_sync_values(dpy
, surf
, ust
, msc
, sbc
);
1717 * Set the error code after a call to
1718 * dri2_egl_image::dri_image::createImageFromTexture.
1721 dri2_create_image_khr_texture_error(int dri_error
)
1725 switch (dri_error
) {
1726 case __DRI_IMAGE_ERROR_SUCCESS
:
1729 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1730 egl_error
= EGL_BAD_ALLOC
;
1733 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1734 egl_error
= EGL_BAD_MATCH
;
1737 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1738 egl_error
= EGL_BAD_PARAMETER
;
1741 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
1742 egl_error
= EGL_BAD_ACCESS
;
1747 egl_error
= EGL_BAD_MATCH
;
1751 _eglError(egl_error
, "dri2_create_image_khr_texture");
1755 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1757 EGLClientBuffer buffer
,
1758 const EGLint
*attr_list
)
1760 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1761 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1762 struct dri2_egl_image
*dri2_img
;
1763 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
1764 _EGLImageAttribs attrs
;
1770 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1771 return EGL_NO_IMAGE_KHR
;
1774 if (_eglParseImageAttribList(&attrs
, disp
, attr_list
) != EGL_SUCCESS
)
1775 return EGL_NO_IMAGE_KHR
;
1778 case EGL_GL_TEXTURE_2D_KHR
:
1780 gl_target
= GL_TEXTURE_2D
;
1782 case EGL_GL_TEXTURE_3D_KHR
:
1783 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1784 depth
= attrs
.GLTextureZOffset
;
1785 gl_target
= GL_TEXTURE_3D
;
1789 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1790 return EGL_NO_IMAGE_KHR
;
1792 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1793 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1794 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1795 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1796 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1797 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1798 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
1799 gl_target
= GL_TEXTURE_CUBE_MAP
;
1802 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1803 return EGL_NO_IMAGE_KHR
;
1806 dri2_img
= malloc(sizeof *dri2_img
);
1808 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1809 return EGL_NO_IMAGE_KHR
;
1812 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1813 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1815 return EGL_NO_IMAGE_KHR
;
1818 dri2_img
->dri_image
=
1819 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
1823 attrs
.GLTextureLevel
,
1826 dri2_create_image_khr_texture_error(error
);
1828 if (!dri2_img
->dri_image
) {
1830 return EGL_NO_IMAGE_KHR
;
1832 return &dri2_img
->base
;
1836 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1837 EGLint attribute
, EGLint
*value
)
1839 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1840 if (!dri2_dpy
->vtbl
->query_surface
)
1841 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
1842 return dri2_dpy
->vtbl
->query_surface(drv
, dpy
, surf
, attribute
, value
);
1845 static struct wl_buffer
*
1846 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1849 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1850 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, dpy
, img
);
1855 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1856 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1858 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1859 EGLint format
, name
, pitch
, err
;
1860 _EGLImageAttribs attrs
;
1861 __DRIimage
*dri_image
;
1863 name
= (EGLint
) (uintptr_t) buffer
;
1865 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1866 if (err
!= EGL_SUCCESS
)
1869 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
1870 attrs
.DRMBufferStrideMESA
<= 0) {
1871 _eglError(EGL_BAD_PARAMETER
,
1872 "bad width, height or stride");
1876 switch (attrs
.DRMBufferFormatMESA
) {
1877 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1878 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1879 pitch
= attrs
.DRMBufferStrideMESA
;
1882 _eglError(EGL_BAD_PARAMETER
,
1883 "dri2_create_image_khr: unsupported pixmap depth");
1888 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1896 return dri2_create_image_from_dri(disp
, dri_image
);
1900 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
1907 * "Required attributes and their values are as follows:
1909 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1911 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1912 * by drm_fourcc.h and used as the pixel_format parameter of the
1913 * drm_mode_fb_cmd2 ioctl."
1917 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1918 * incomplete, EGL_BAD_PARAMETER is generated."
1920 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
1921 !attrs
->DMABufFourCC
.IsPresent
) {
1922 _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
1929 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1930 * specified for a plane's pitch or offset isn't supported by EGL,
1931 * EGL_BAD_ACCESS is generated."
1933 for (i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
1934 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
1935 attrs
->DMABufPlanePitches
[i
].Value
<= 0) {
1936 _eglError(EGL_BAD_ACCESS
, "invalid pitch");
1944 /* Returns the total number of file descriptors. Zero indicates an error. */
1946 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
1948 unsigned i
, plane_n
;
1950 switch (attrs
->DMABufFourCC
.Value
) {
1952 case DRM_FORMAT_RG88
:
1953 case DRM_FORMAT_GR88
:
1954 case DRM_FORMAT_RGB332
:
1955 case DRM_FORMAT_BGR233
:
1956 case DRM_FORMAT_XRGB4444
:
1957 case DRM_FORMAT_XBGR4444
:
1958 case DRM_FORMAT_RGBX4444
:
1959 case DRM_FORMAT_BGRX4444
:
1960 case DRM_FORMAT_ARGB4444
:
1961 case DRM_FORMAT_ABGR4444
:
1962 case DRM_FORMAT_RGBA4444
:
1963 case DRM_FORMAT_BGRA4444
:
1964 case DRM_FORMAT_XRGB1555
:
1965 case DRM_FORMAT_XBGR1555
:
1966 case DRM_FORMAT_RGBX5551
:
1967 case DRM_FORMAT_BGRX5551
:
1968 case DRM_FORMAT_ARGB1555
:
1969 case DRM_FORMAT_ABGR1555
:
1970 case DRM_FORMAT_RGBA5551
:
1971 case DRM_FORMAT_BGRA5551
:
1972 case DRM_FORMAT_RGB565
:
1973 case DRM_FORMAT_BGR565
:
1974 case DRM_FORMAT_RGB888
:
1975 case DRM_FORMAT_BGR888
:
1976 case DRM_FORMAT_XRGB8888
:
1977 case DRM_FORMAT_XBGR8888
:
1978 case DRM_FORMAT_RGBX8888
:
1979 case DRM_FORMAT_BGRX8888
:
1980 case DRM_FORMAT_ARGB8888
:
1981 case DRM_FORMAT_ABGR8888
:
1982 case DRM_FORMAT_RGBA8888
:
1983 case DRM_FORMAT_BGRA8888
:
1984 case DRM_FORMAT_XRGB2101010
:
1985 case DRM_FORMAT_XBGR2101010
:
1986 case DRM_FORMAT_RGBX1010102
:
1987 case DRM_FORMAT_BGRX1010102
:
1988 case DRM_FORMAT_ARGB2101010
:
1989 case DRM_FORMAT_ABGR2101010
:
1990 case DRM_FORMAT_RGBA1010102
:
1991 case DRM_FORMAT_BGRA1010102
:
1992 case DRM_FORMAT_YUYV
:
1993 case DRM_FORMAT_YVYU
:
1994 case DRM_FORMAT_UYVY
:
1995 case DRM_FORMAT_VYUY
:
1998 case DRM_FORMAT_NV12
:
1999 case DRM_FORMAT_NV21
:
2000 case DRM_FORMAT_NV16
:
2001 case DRM_FORMAT_NV61
:
2004 case DRM_FORMAT_YUV410
:
2005 case DRM_FORMAT_YVU410
:
2006 case DRM_FORMAT_YUV411
:
2007 case DRM_FORMAT_YVU411
:
2008 case DRM_FORMAT_YUV420
:
2009 case DRM_FORMAT_YVU420
:
2010 case DRM_FORMAT_YUV422
:
2011 case DRM_FORMAT_YVU422
:
2012 case DRM_FORMAT_YUV444
:
2013 case DRM_FORMAT_YVU444
:
2017 _eglError(EGL_BAD_ATTRIBUTE
, "invalid format");
2024 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2025 * incomplete, EGL_BAD_PARAMETER is generated."
2027 for (i
= 0; i
< plane_n
; ++i
) {
2028 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2029 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2030 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2031 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2037 * The spec also says:
2039 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2040 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2041 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2042 * attributes are specified."
2044 for (i
= plane_n
; i
< 3; ++i
) {
2045 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2046 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2047 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2048 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2059 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2060 * EGL will take a reference to the dma_buf(s) which it will release at any
2061 * time while the EGLDisplay is initialized. It is the responsibility of the
2062 * application to close the dma_buf file descriptors."
2064 * Therefore we must never close or otherwise modify the file descriptors.
2067 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2068 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2070 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2073 _EGLImageAttribs attrs
;
2074 __DRIimage
*dri_image
;
2085 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2086 * error EGL_BAD_PARAMETER is generated."
2088 if (buffer
!= NULL
) {
2089 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2093 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2094 if (err
!= EGL_SUCCESS
) {
2095 _eglError(err
, "bad attribute");
2099 if (!dri2_check_dma_buf_attribs(&attrs
))
2102 num_fds
= dri2_check_dma_buf_format(&attrs
);
2106 for (i
= 0; i
< num_fds
; ++i
) {
2107 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2108 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2109 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2113 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2114 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2115 fds
, num_fds
, pitches
, offsets
,
2116 attrs
.DMABufYuvColorSpaceHint
.Value
,
2117 attrs
.DMABufSampleRangeHint
.Value
,
2118 attrs
.DMABufChromaHorizontalSiting
.Value
,
2119 attrs
.DMABufChromaVerticalSiting
.Value
,
2122 dri2_create_image_khr_texture_error(error
);
2125 return EGL_NO_IMAGE_KHR
;
2127 res
= dri2_create_image_from_dri(disp
, dri_image
);
2132 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2133 const EGLint
*attr_list
)
2135 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2136 struct dri2_egl_image
*dri2_img
;
2137 _EGLImageAttribs attrs
;
2138 unsigned int dri_use
, valid_mask
;
2140 EGLint err
= EGL_SUCCESS
;
2144 dri2_img
= malloc(sizeof *dri2_img
);
2146 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2147 return EGL_NO_IMAGE_KHR
;
2151 err
= EGL_BAD_PARAMETER
;
2155 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
2156 err
= EGL_BAD_PARAMETER
;
2160 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2161 if (err
!= EGL_SUCCESS
)
2164 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2165 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)",
2166 attrs
.Width
, attrs
.Height
);
2170 switch (attrs
.DRMBufferFormatMESA
) {
2171 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2172 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2175 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x",
2176 attrs
.DRMBufferFormatMESA
);
2181 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2182 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2183 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2184 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2185 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x",
2186 attrs
.DRMBufferUseMESA
& ~valid_mask
);
2191 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2192 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2193 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2194 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2195 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2196 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2198 dri2_img
->dri_image
=
2199 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2200 attrs
.Width
, attrs
.Height
,
2201 format
, dri_use
, dri2_img
);
2202 if (dri2_img
->dri_image
== NULL
) {
2203 err
= EGL_BAD_ALLOC
;
2207 return &dri2_img
->base
;
2211 _eglError(err
, "dri2_create_drm_image_mesa");
2213 return EGL_NO_IMAGE_KHR
;
2217 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2218 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2220 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2221 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2225 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2226 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
2227 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2232 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2233 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2236 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2237 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2243 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2245 EGLint
*fourcc
, EGLint
*nplanes
,
2246 EGLuint64KHR
*modifiers
)
2248 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2249 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2255 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2256 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2258 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2259 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2268 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2269 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2271 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2272 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2276 /* rework later to provide multiple fds/strides/offsets */
2278 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2279 __DRI_IMAGE_ATTRIB_FD
, fds
);
2282 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2283 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2287 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2288 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2290 offsets
[0] = img_offset
;
2301 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2302 _EGLContext
*ctx
, EGLenum target
,
2303 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2308 case EGL_GL_TEXTURE_2D_KHR
:
2309 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2310 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2311 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2312 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2313 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2314 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2315 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2316 case EGL_GL_TEXTURE_3D_KHR
:
2317 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
2318 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2321 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2322 return EGL_NO_IMAGE_KHR
;
2324 case EGL_GL_RENDERBUFFER_KHR
:
2325 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2327 case EGL_DRM_BUFFER_MESA
:
2328 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2329 case EGL_LINUX_DMA_BUF_EXT
:
2330 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2332 #ifdef HAVE_WAYLAND_PLATFORM
2333 case EGL_WAYLAND_BUFFER_WL
:
2334 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2337 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2338 return EGL_NO_IMAGE_KHR
;
2343 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2345 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2346 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2350 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2356 #ifdef HAVE_WAYLAND_PLATFORM
2359 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2360 struct wl_drm_buffer
*buffer
)
2362 _EGLDisplay
*disp
= user_data
;
2363 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2365 int i
, dri_components
= 0;
2368 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2377 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2389 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2391 buffer
->driver_format
= NULL
;
2392 for (i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2393 if (wl_drm_components
[i
].dri_components
== dri_components
)
2394 buffer
->driver_format
= &wl_drm_components
[i
];
2396 if (buffer
->driver_format
== NULL
)
2397 dri2_dpy
->image
->destroyImage(img
);
2399 buffer
->driver_buffer
= img
;
2403 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2405 _EGLDisplay
*disp
= user_data
;
2406 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2408 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2411 static struct wayland_drm_callbacks wl_drm_callbacks
= {
2412 .authenticate
= NULL
,
2413 .reference_buffer
= dri2_wl_reference_buffer
,
2414 .release_buffer
= dri2_wl_release_buffer
2418 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2419 struct wl_display
*wl_dpy
)
2421 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2427 if (dri2_dpy
->wl_server_drm
)
2430 wl_drm_callbacks
.authenticate
=
2431 (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
;
2433 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2434 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2435 dri2_dpy
->image
->base
.version
>= 7 &&
2436 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2437 flags
|= WAYLAND_DRM_PRIME
;
2439 dri2_dpy
->wl_server_drm
=
2440 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2441 &wl_drm_callbacks
, disp
, flags
);
2443 if (!dri2_dpy
->wl_server_drm
)
2446 #ifdef HAVE_DRM_PLATFORM
2447 /* We have to share the wl_drm instance with gbm, so gbm can convert
2448 * wl_buffers to gbm bos. */
2449 if (dri2_dpy
->gbm_dri
)
2450 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2457 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2458 struct wl_display
*wl_dpy
)
2460 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2464 if (!dri2_dpy
->wl_server_drm
)
2467 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2468 dri2_dpy
->wl_server_drm
= NULL
;
2474 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2475 struct wl_resource
*buffer_resource
,
2476 EGLint attribute
, EGLint
*value
)
2478 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2479 struct wl_drm_buffer
*buffer
;
2480 const struct wl_drm_components_descriptor
*format
;
2482 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2486 format
= buffer
->driver_format
;
2487 switch (attribute
) {
2488 case EGL_TEXTURE_FORMAT
:
2489 *value
= format
->components
;
2492 *value
= buffer
->width
;
2495 *value
= buffer
->height
;
2504 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2506 p_atomic_inc(&sync
->refcount
);
2510 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2511 struct dri2_egl_sync
*dri2_sync
)
2513 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2514 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
)
2515 cnd_destroy(&dri2_sync
->cond
);
2517 if (dri2_sync
->fence
)
2518 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2525 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
2526 EGLenum type
, const EGLAttrib
*attrib_list
)
2528 _EGLContext
*ctx
= _eglGetCurrentContext();
2529 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2530 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2531 struct dri2_egl_sync
*dri2_sync
;
2533 pthread_condattr_t attr
;
2535 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2537 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2541 if (!_eglInitSync(&dri2_sync
->base
, dpy
, type
, attrib_list
)) {
2547 case EGL_SYNC_FENCE_KHR
:
2548 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2549 if (!dri2_sync
->fence
) {
2550 /* Why did it fail? DRI doesn't return an error code, so we emit
2551 * a generic EGL error that doesn't communicate user error.
2553 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2559 case EGL_SYNC_CL_EVENT_KHR
:
2560 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2561 dri2_dpy
->dri_screen
,
2562 dri2_sync
->base
.CLEvent
);
2563 /* this can only happen if the cl_event passed in is invalid. */
2564 if (!dri2_sync
->fence
) {
2565 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2570 /* the initial status must be "signaled" if the cl_event is signaled */
2571 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2572 dri2_sync
->fence
, 0, 0))
2573 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2576 case EGL_SYNC_REUSABLE_KHR
:
2577 /* intialize attr */
2578 ret
= pthread_condattr_init(&attr
);
2581 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2586 /* change clock attribute to CLOCK_MONOTONIC */
2587 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
2590 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2595 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
2598 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2603 /* initial status of reusable sync must be "unsignaled" */
2604 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
2608 p_atomic_set(&dri2_sync
->refcount
, 1);
2609 return &dri2_sync
->base
;
2613 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2615 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2616 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2617 EGLint ret
= EGL_TRUE
;
2620 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
2621 * then unlock all threads possibly blocked by the reusable sync before
2624 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
2625 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2626 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2627 /* unblock all threads currently blocked by sync */
2628 err
= cnd_broadcast(&dri2_sync
->cond
);
2631 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
2635 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2641 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2642 EGLint flags
, EGLTime timeout
)
2644 _EGLContext
*ctx
= _eglGetCurrentContext();
2645 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2646 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2647 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2648 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2649 unsigned wait_flags
= 0;
2651 /* timespecs for cnd_timedwait */
2652 struct timespec current
;
2655 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
2657 /* The EGL_KHR_fence_sync spec states:
2659 * "If no context is current for the bound API,
2660 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
2662 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
2663 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
2665 /* the sync object should take a reference while waiting */
2666 dri2_egl_ref_sync(dri2_sync
);
2668 switch (sync
->Type
) {
2669 case EGL_SYNC_FENCE_KHR
:
2670 case EGL_SYNC_CL_EVENT_KHR
:
2671 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
2672 dri2_sync
->fence
, wait_flags
,
2674 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2676 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2679 case EGL_SYNC_REUSABLE_KHR
:
2680 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
2681 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
2682 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
2683 dri2_drv
->glFlush();
2686 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
2687 if (timeout
== EGL_FOREVER_KHR
) {
2688 mtx_lock(&dri2_sync
->mutex
);
2689 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
2690 mtx_unlock(&dri2_sync
->mutex
);
2692 /* if reusable sync has not been yet signaled */
2693 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
2694 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
2696 /* calculating when to expire */
2697 expire
.nsec
= timeout
% 1000000000L;
2698 expire
.sec
= timeout
/ 1000000000L;
2700 expire
.nsec
+= current
.tv_nsec
;
2701 expire
.sec
+= current
.tv_sec
;
2703 /* expire.nsec now is a number between 0 and 1999999998 */
2704 if (expire
.nsec
> 999999999L) {
2706 expire
.nsec
-= 1000000000L;
2709 mtx_lock(&dri2_sync
->mutex
);
2710 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
2711 mtx_unlock(&dri2_sync
->mutex
);
2713 if (ret
== thrd_busy
) {
2714 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2715 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2717 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2725 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2731 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2734 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2737 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
) {
2738 _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
2742 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
) {
2743 _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
2747 dri2_sync
->base
.SyncStatus
= mode
;
2749 if (mode
== EGL_SIGNALED_KHR
) {
2750 ret
= cnd_broadcast(&dri2_sync
->cond
);
2752 /* fail to broadcast */
2754 _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
2763 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2765 _EGLContext
*ctx
= _eglGetCurrentContext();
2766 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2767 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2768 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2770 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
2771 dri2_sync
->fence
, 0);
2776 dri2_interop_query_device_info(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2777 struct mesa_glinterop_device_info
*out
)
2779 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2780 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2782 if (!dri2_dpy
->interop
)
2783 return MESA_GLINTEROP_UNSUPPORTED
;
2785 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
2789 dri2_interop_export_object(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2790 struct mesa_glinterop_export_in
*in
,
2791 struct mesa_glinterop_export_out
*out
)
2793 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2794 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2796 if (!dri2_dpy
->interop
)
2797 return MESA_GLINTEROP_UNSUPPORTED
;
2799 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
2803 dri2_unload(_EGLDriver
*drv
)
2805 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2807 dlclose(dri2_drv
->handle
);
2812 dri2_load(_EGLDriver
*drv
)
2814 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2815 #ifdef HAVE_ANDROID_PLATFORM
2816 const char *libname
= "libglapi.so";
2817 #elif defined(__APPLE__)
2818 const char *libname
= "libglapi.0.dylib";
2819 #elif defined(__CYGWIN__)
2820 const char *libname
= "cygglapi-0.dll";
2822 const char *libname
= "libglapi.so.0";
2826 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2827 handle
= dlopen(libname
, RTLD_LAZY
| RTLD_GLOBAL
);
2829 _eglLog(_EGL_WARNING
, "DRI2: failed to open glapi provider");
2833 dri2_drv
->get_proc_address
= (_EGLProc (*)(const char *))
2834 dlsym(handle
, "_glapi_get_proc_address");
2836 /* if glapi is not available, loading DRI drivers will fail */
2837 if (!dri2_drv
->get_proc_address
) {
2838 _eglLog(_EGL_WARNING
, "DRI2: failed to find _glapi_get_proc_address");
2842 dri2_drv
->glFlush
= (void (*)(void))
2843 dri2_drv
->get_proc_address("glFlush");
2845 /* if glFlush is not available things are horribly broken */
2846 if (!dri2_drv
->glFlush
) {
2847 _eglLog(_EGL_WARNING
, "DRI2: failed to find glFlush entry point");
2851 dri2_drv
->handle
= handle
;
2861 * This is the main entrypoint into the driver, called by libEGL.
2862 * Create a new _EGLDriver object and init its dispatch table.
2865 _eglBuiltInDriverDRI2(const char *args
)
2867 struct dri2_egl_driver
*dri2_drv
;
2871 dri2_drv
= calloc(1, sizeof *dri2_drv
);
2875 if (!dri2_load(&dri2_drv
->base
)) {
2880 _eglInitDriverFallbacks(&dri2_drv
->base
);
2881 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
2882 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
2883 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
2884 dri2_drv
->base
.API
.DestroyContext
= dri2_destroy_context
;
2885 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
2886 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
2887 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
2888 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
2889 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
2890 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
2891 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
2892 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
2893 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
2894 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
2895 dri2_drv
->base
.API
.SwapInterval
= dri2_swap_interval
;
2896 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
2897 dri2_drv
->base
.API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
2898 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
2899 dri2_drv
->base
.API
.PostSubBufferNV
= dri2_post_sub_buffer
;
2900 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
,
2901 dri2_drv
->base
.API
.QueryBufferAge
= dri2_query_buffer_age
;
2902 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image
;
2903 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
2904 dri2_drv
->base
.API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
2905 dri2_drv
->base
.API
.QuerySurface
= dri2_query_surface
;
2907 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
2908 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
2909 dri2_drv
->base
.API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
2910 dri2_drv
->base
.API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
2912 #ifdef HAVE_WAYLAND_PLATFORM
2913 dri2_drv
->base
.API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
2914 dri2_drv
->base
.API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
2915 dri2_drv
->base
.API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
2917 dri2_drv
->base
.API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
2918 dri2_drv
->base
.API
.CreateSyncKHR
= dri2_create_sync
;
2919 dri2_drv
->base
.API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
2920 dri2_drv
->base
.API
.SignalSyncKHR
= dri2_signal_sync
;
2921 dri2_drv
->base
.API
.WaitSyncKHR
= dri2_server_wait_sync
;
2922 dri2_drv
->base
.API
.DestroySyncKHR
= dri2_destroy_sync
;
2923 dri2_drv
->base
.API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
2924 dri2_drv
->base
.API
.GLInteropExportObject
= dri2_interop_export_object
;
2926 dri2_drv
->base
.Name
= "DRI2";
2927 dri2_drv
->base
.Unload
= dri2_unload
;
2929 return &dri2_drv
->base
;