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 "loader/loader.h"
60 #include "util/u_atomic.h"
62 /* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
63 * some of the definitions here so that building Mesa won't bleeding-edge
67 #define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
70 #ifndef DRM_FORMAT_RG88
71 #define DRM_FORMAT_RG88 fourcc_code('R', 'G', '8', '8') /* [15:0] R:G 8:8 little endian */
74 #ifndef DRM_FORMAT_GR88
75 #define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
78 const __DRIuseInvalidateExtension use_invalidate
= {
79 .base
= { __DRI_USE_INVALIDATE
, 1 }
82 EGLint dri2_to_egl_attribute_map
[] = {
84 EGL_BUFFER_SIZE
, /* __DRI_ATTRIB_BUFFER_SIZE */
85 EGL_LEVEL
, /* __DRI_ATTRIB_LEVEL */
86 EGL_RED_SIZE
, /* __DRI_ATTRIB_RED_SIZE */
87 EGL_GREEN_SIZE
, /* __DRI_ATTRIB_GREEN_SIZE */
88 EGL_BLUE_SIZE
, /* __DRI_ATTRIB_BLUE_SIZE */
89 EGL_LUMINANCE_SIZE
, /* __DRI_ATTRIB_LUMINANCE_SIZE */
90 EGL_ALPHA_SIZE
, /* __DRI_ATTRIB_ALPHA_SIZE */
91 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
92 EGL_DEPTH_SIZE
, /* __DRI_ATTRIB_DEPTH_SIZE */
93 EGL_STENCIL_SIZE
, /* __DRI_ATTRIB_STENCIL_SIZE */
94 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
95 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
96 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
97 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
98 EGL_SAMPLE_BUFFERS
, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
99 EGL_SAMPLES
, /* __DRI_ATTRIB_SAMPLES */
100 0, /* __DRI_ATTRIB_RENDER_TYPE, */
101 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
102 0, /* __DRI_ATTRIB_CONFORMANT */
103 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
104 0, /* __DRI_ATTRIB_STEREO */
105 0, /* __DRI_ATTRIB_AUX_BUFFERS */
106 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
107 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
108 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
109 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
110 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
111 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
112 0, /* __DRI_ATTRIB_FLOAT_MODE (deprecated) */
113 0, /* __DRI_ATTRIB_RED_MASK */
114 0, /* __DRI_ATTRIB_GREEN_MASK */
115 0, /* __DRI_ATTRIB_BLUE_MASK */
116 0, /* __DRI_ATTRIB_ALPHA_MASK */
117 EGL_MAX_PBUFFER_WIDTH
, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
118 EGL_MAX_PBUFFER_HEIGHT
, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
119 EGL_MAX_PBUFFER_PIXELS
, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
120 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
121 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
122 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
123 0, /* __DRI_ATTRIB_SWAP_METHOD */
124 EGL_MAX_SWAP_INTERVAL
, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
125 EGL_MIN_SWAP_INTERVAL
, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
126 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
127 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
128 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
129 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
130 EGL_Y_INVERTED_NOK
, /* __DRI_ATTRIB_YINVERTED */
131 0, /* __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE */
135 dri2_get_dri_config(struct dri2_egl_config
*conf
, EGLint surface_type
,
138 const bool srgb
= colorspace
== EGL_GL_COLORSPACE_SRGB_KHR
;
140 return surface_type
== EGL_WINDOW_BIT
? conf
->dri_double_config
[srgb
] :
141 conf
->dri_single_config
[srgb
];
145 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
147 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
150 if (!_eglMatchConfig(conf
, criteria
))
156 struct dri2_egl_config
*
157 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
158 EGLint surface_type
, const EGLint
*attr_list
,
159 const unsigned int *rgba_masks
)
161 struct dri2_egl_config
*conf
;
162 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
164 unsigned int attrib
, value
, double_buffer
;
166 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
167 unsigned int dri_masks
[4] = { 0, 0, 0, 0 };
168 _EGLConfig
*matching_config
;
169 EGLint num_configs
= 0;
173 _eglInitConfig(&base
, disp
, id
);
177 bind_to_texture_rgb
= 0;
178 bind_to_texture_rgba
= 0;
180 while (dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
++, &attrib
, &value
)) {
182 case __DRI_ATTRIB_RENDER_TYPE
:
183 if (value
& __DRI_ATTRIB_RGBA_BIT
)
184 value
= EGL_RGB_BUFFER
;
185 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
186 value
= EGL_LUMINANCE_BUFFER
;
189 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
192 case __DRI_ATTRIB_CONFIG_CAVEAT
:
193 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
194 value
= EGL_NON_CONFORMANT_CONFIG
;
195 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
196 value
= EGL_SLOW_CONFIG
;
199 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
202 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
203 bind_to_texture_rgb
= value
;
206 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
207 bind_to_texture_rgba
= value
;
210 case __DRI_ATTRIB_DOUBLE_BUFFER
:
211 double_buffer
= value
;
214 case __DRI_ATTRIB_RED_MASK
:
215 dri_masks
[0] = value
;
218 case __DRI_ATTRIB_GREEN_MASK
:
219 dri_masks
[1] = value
;
222 case __DRI_ATTRIB_BLUE_MASK
:
223 dri_masks
[2] = value
;
226 case __DRI_ATTRIB_ALPHA_MASK
:
227 dri_masks
[3] = value
;
230 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
231 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
232 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
233 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
234 /* Don't expose visuals with the accumulation buffer. */
239 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
241 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
245 case __DRI_ATTRIB_MAX_PBUFFER_WIDTH
:
246 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_WIDTH
,
247 _EGL_MAX_PBUFFER_WIDTH
);
249 case __DRI_ATTRIB_MAX_PBUFFER_HEIGHT
:
250 _eglSetConfigKey(&base
, EGL_MAX_PBUFFER_HEIGHT
,
251 _EGL_MAX_PBUFFER_HEIGHT
);
255 key
= dri2_to_egl_attribute_map
[attrib
];
257 _eglSetConfigKey(&base
, key
, value
);
263 for (i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
264 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
266 if (rgba_masks
&& memcmp(rgba_masks
, dri_masks
, sizeof(dri_masks
)))
269 base
.NativeRenderable
= EGL_TRUE
;
271 base
.SurfaceType
= surface_type
;
272 if (surface_type
& (EGL_PBUFFER_BIT
|
273 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
274 base
.BindToTextureRGB
= bind_to_texture_rgb
;
275 if (base
.AlphaSize
> 0)
276 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
279 base
.RenderableType
= disp
->ClientAPIs
;
280 base
.Conformant
= disp
->ClientAPIs
;
282 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
283 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
285 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
286 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
290 config_id
= base
.ConfigID
;
291 base
.ConfigID
= EGL_DONT_CARE
;
292 base
.SurfaceType
= EGL_DONT_CARE
;
293 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
294 (_EGLArrayForEach
) dri2_match_config
, &base
);
296 if (num_configs
== 1) {
297 conf
= (struct dri2_egl_config
*) matching_config
;
299 if (double_buffer
&& !conf
->dri_double_config
[srgb
])
300 conf
->dri_double_config
[srgb
] = dri_config
;
301 else if (!double_buffer
&& !conf
->dri_single_config
[srgb
])
302 conf
->dri_single_config
[srgb
] = dri_config
;
304 /* a similar config type is already added (unlikely) => discard */
307 else if (num_configs
== 0) {
308 conf
= calloc(1, sizeof *conf
);
313 conf
->dri_double_config
[srgb
] = dri_config
;
315 conf
->dri_single_config
[srgb
] = dri_config
;
317 memcpy(&conf
->base
, &base
, sizeof base
);
318 conf
->base
.SurfaceType
= 0;
319 conf
->base
.ConfigID
= config_id
;
321 _eglLinkConfig(&conf
->base
);
329 surface_type
&= ~EGL_PIXMAP_BIT
;
332 /* No support for pbuffer + MSAA for now.
334 * XXX TODO: pbuffer + MSAA does not work and causes crashes.
335 * See QT bugreport: https://bugreports.qt.io/browse/QTBUG-47509
338 surface_type
&= ~EGL_PBUFFER_BIT
;
341 conf
->base
.SurfaceType
|= surface_type
;
347 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
349 _EGLDisplay
*disp
= data
;
350 struct dri2_egl_image
*dri2_img
;
355 img
= _eglLookupImage(image
, disp
);
357 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
361 dri2_img
= dri2_egl_image(image
);
363 return dri2_img
->dri_image
;
366 const __DRIimageLookupExtension image_lookup_extension
= {
367 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
369 .lookupEGLImage
= dri2_lookup_egl_image
372 struct dri2_extension_match
{
378 static const struct dri2_extension_match dri3_driver_extensions
[] = {
379 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
380 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
384 static const struct dri2_extension_match dri2_driver_extensions
[] = {
385 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
386 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
390 static const struct dri2_extension_match dri2_core_extensions
[] = {
391 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
392 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
393 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
397 static const struct dri2_extension_match swrast_driver_extensions
[] = {
398 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
399 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
403 static const struct dri2_extension_match swrast_core_extensions
[] = {
404 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
408 static const struct dri2_extension_match optional_core_extensions
[] = {
409 { __DRI2_ROBUSTNESS
, 1, offsetof(struct dri2_egl_display
, robustness
) },
410 { __DRI2_CONFIG_QUERY
, 1, offsetof(struct dri2_egl_display
, config
) },
411 { __DRI2_FENCE
, 1, offsetof(struct dri2_egl_display
, fence
) },
412 { __DRI2_RENDERER_QUERY
, 1, offsetof(struct dri2_egl_display
, rendererQuery
) },
413 { __DRI2_INTEROP
, 1, offsetof(struct dri2_egl_display
, interop
) },
418 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
419 const struct dri2_extension_match
*matches
,
420 const __DRIextension
**extensions
,
423 int i
, j
, ret
= EGL_TRUE
;
426 for (i
= 0; extensions
[i
]; i
++) {
427 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
428 for (j
= 0; matches
[j
].name
; j
++) {
429 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
430 extensions
[i
]->version
>= matches
[j
].version
) {
431 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
432 *(const __DRIextension
**) field
= extensions
[i
];
433 _eglLog(_EGL_INFO
, "found extension %s version %d",
434 extensions
[i
]->name
, extensions
[i
]->version
);
440 for (j
= 0; matches
[j
].name
; j
++) {
441 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
442 if (*(const __DRIextension
**) field
== NULL
) {
444 _eglLog(_EGL_DEBUG
, "did not find optional extension %s version %d",
445 matches
[j
].name
, matches
[j
].version
);
447 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
448 matches
[j
].name
, matches
[j
].version
);
457 static const __DRIextension
**
458 dri2_open_driver(_EGLDisplay
*disp
)
460 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
461 const __DRIextension
**extensions
= NULL
;
462 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
463 char *get_extensions_name
;
464 const __DRIextension
**(*get_extensions
)(void);
467 if (geteuid() == getuid()) {
468 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
469 search_paths
= getenv("LIBGL_DRIVERS_PATH");
471 if (search_paths
== NULL
)
472 search_paths
= DEFAULT_DRIVER_DIR
;
474 dri2_dpy
->driver
= NULL
;
475 end
= search_paths
+ strlen(search_paths
);
476 for (p
= search_paths
; p
< end
; p
= next
+ 1) {
478 next
= strchr(p
, ':');
484 snprintf(path
, sizeof path
,
485 "%.*s/tls/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
486 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
488 if (dri2_dpy
->driver
== NULL
) {
489 snprintf(path
, sizeof path
,
490 "%.*s/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
491 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
492 if (dri2_dpy
->driver
== NULL
)
493 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
495 /* not need continue to loop all paths once the driver is found */
496 if (dri2_dpy
->driver
!= NULL
)
500 snprintf(path
, sizeof path
, "%.*s/gallium_dri.so", len
, p
);
501 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
502 if (dri2_dpy
->driver
== NULL
)
503 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
509 if (dri2_dpy
->driver
== NULL
) {
510 _eglLog(_EGL_WARNING
,
511 "DRI2: failed to open %s (search paths %s)",
512 dri2_dpy
->driver_name
, search_paths
);
516 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
518 get_extensions_name
= loader_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
;
661 if (dri2_dpy
->fence
->base
.version
>= 2) {
662 unsigned capabilities
=
663 dri2_dpy
->fence
->get_capabilities(dri2_dpy
->dri_screen
);
664 disp
->Extensions
.ANDROID_native_fence_sync
=
665 (capabilities
& __DRI_FENCE_CAP_NATIVE_FD
) != 0;
669 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
671 if (dri2_dpy
->image
) {
672 if (dri2_dpy
->image
->base
.version
>= 10 &&
673 dri2_dpy
->image
->getCapabilities
!= NULL
) {
676 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
677 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
679 if (dri2_dpy
->image
->base
.version
>= 11)
680 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
682 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
683 if (dri2_dpy
->image
->base
.version
>= 11)
684 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
687 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
688 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
689 if (dri2_dpy
->image
->base
.version
>= 5 &&
690 dri2_dpy
->image
->createImageFromTexture
) {
691 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
692 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
694 if (dri2_renderer_query_integer(dri2_dpy
,
695 __DRI2_RENDERER_HAS_TEXTURE_3D
))
696 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
698 if (dri2_dpy
->image
->base
.version
>= 8 &&
699 dri2_dpy
->image
->createImageFromDmaBufs
) {
700 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
706 /* All platforms but DRM call this function to create the screen, query the
707 * dri extensions, setup the vtables and populate the driver_configs.
708 * DRM inherits all that information from its display - GBM.
711 dri2_create_screen(_EGLDisplay
*disp
)
713 const __DRIextension
**extensions
;
714 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
716 if (dri2_dpy
->image_driver
) {
717 dri2_dpy
->dri_screen
=
718 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
719 dri2_dpy
->loader_extensions
,
720 dri2_dpy
->driver_extensions
,
721 &dri2_dpy
->driver_configs
,
723 } else if (dri2_dpy
->dri2
) {
724 if (dri2_dpy
->dri2
->base
.version
>= 4) {
725 dri2_dpy
->dri_screen
=
726 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
727 dri2_dpy
->loader_extensions
,
728 dri2_dpy
->driver_extensions
,
729 &dri2_dpy
->driver_configs
, disp
);
731 dri2_dpy
->dri_screen
=
732 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
733 dri2_dpy
->loader_extensions
,
734 &dri2_dpy
->driver_configs
, disp
);
737 assert(dri2_dpy
->swrast
);
738 if (dri2_dpy
->swrast
->base
.version
>= 4) {
739 dri2_dpy
->dri_screen
=
740 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->loader_extensions
,
741 dri2_dpy
->driver_extensions
,
742 &dri2_dpy
->driver_configs
, disp
);
744 dri2_dpy
->dri_screen
=
745 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->loader_extensions
,
746 &dri2_dpy
->driver_configs
, disp
);
750 if (dri2_dpy
->dri_screen
== NULL
) {
751 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
755 dri2_dpy
->own_dri_screen
= 1;
757 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
759 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
) {
760 if (!dri2_bind_extensions(dri2_dpy
, dri2_core_extensions
, extensions
, false))
761 goto cleanup_dri_screen
;
763 assert(dri2_dpy
->swrast
);
764 if (!dri2_bind_extensions(dri2_dpy
, swrast_core_extensions
, extensions
, false))
765 goto cleanup_dri_screen
;
768 dri2_bind_extensions(dri2_dpy
, optional_core_extensions
, extensions
, true);
769 dri2_setup_screen(disp
);
774 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
780 * Called via eglInitialize(), GLX_drv->API.Initialize().
782 * This must be guaranteed to be called exactly once, even if eglInitialize is
783 * called many times (without a eglTerminate in between).
786 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
788 EGLBoolean ret
= EGL_FALSE
;
789 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
791 /* In the case where the application calls eglMakeCurrent(context1),
792 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
793 * or eglMakeCurrent(NULL) before that), dri2_dpy structure is still
794 * initialized, as we need it to be able to free context1 correctly.
796 * It would probably be safest to forcibly release the display with
797 * dri2_display_release, to make sure the display is reinitialized correctly.
798 * However, the EGL spec states that we need to keep a reference to the
799 * current context (so we cannot call dri2_make_current(NULL)), and therefore
800 * we would leak context1 as we would be missing the old display connection
801 * to free it up correctly.
804 dri2_dpy
->ref_count
++;
808 /* not until swrast_dri is supported */
809 if (disp
->Options
.UseFallback
)
812 /* Nothing to initialize for a test only display */
813 if (disp
->Options
.TestOnly
)
816 switch (disp
->Platform
) {
817 #ifdef HAVE_SURFACELESS_PLATFORM
818 case _EGL_PLATFORM_SURFACELESS
:
819 ret
= dri2_initialize_surfaceless(drv
, disp
);
822 #ifdef HAVE_X11_PLATFORM
823 case _EGL_PLATFORM_X11
:
824 ret
= dri2_initialize_x11(drv
, disp
);
827 #ifdef HAVE_DRM_PLATFORM
828 case _EGL_PLATFORM_DRM
:
829 ret
= dri2_initialize_drm(drv
, disp
);
832 #ifdef HAVE_WAYLAND_PLATFORM
833 case _EGL_PLATFORM_WAYLAND
:
834 ret
= dri2_initialize_wayland(drv
, disp
);
837 #ifdef HAVE_ANDROID_PLATFORM
838 case _EGL_PLATFORM_ANDROID
:
839 ret
= dri2_initialize_android(drv
, disp
);
843 _eglLog(_EGL_WARNING
, "No EGL platform enabled.");
848 dri2_dpy
= dri2_egl_display(disp
);
854 dri2_dpy
->ref_count
++;
861 * Decrement display reference count, and free up display if necessary.
864 dri2_display_release(_EGLDisplay
*disp
)
866 struct dri2_egl_display
*dri2_dpy
;
872 dri2_dpy
= dri2_egl_display(disp
);
874 assert(dri2_dpy
->ref_count
> 0);
875 dri2_dpy
->ref_count
--;
877 if (dri2_dpy
->ref_count
> 0)
880 _eglCleanupDisplay(disp
);
882 if (dri2_dpy
->own_dri_screen
)
883 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
884 if (dri2_dpy
->fd
>= 0)
886 if (dri2_dpy
->driver
)
887 dlclose(dri2_dpy
->driver
);
888 free(dri2_dpy
->driver_name
);
890 #ifdef HAVE_WAYLAND_PLATFORM
891 free(dri2_dpy
->device_name
);
894 switch (disp
->Platform
) {
895 #ifdef HAVE_X11_PLATFORM
896 case _EGL_PLATFORM_X11
:
897 if (dri2_dpy
->own_device
) {
898 xcb_disconnect(dri2_dpy
->conn
);
902 #ifdef HAVE_DRM_PLATFORM
903 case _EGL_PLATFORM_DRM
:
904 if (dri2_dpy
->own_device
) {
905 gbm_device_destroy(&dri2_dpy
->gbm_dri
->base
.base
);
909 #ifdef HAVE_WAYLAND_PLATFORM
910 case _EGL_PLATFORM_WAYLAND
:
911 if (dri2_dpy
->wl_drm
)
912 wl_drm_destroy(dri2_dpy
->wl_drm
);
913 if (dri2_dpy
->wl_shm
)
914 wl_shm_destroy(dri2_dpy
->wl_shm
);
915 wl_registry_destroy(dri2_dpy
->wl_registry
);
916 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
917 if (dri2_dpy
->own_device
) {
918 wl_display_disconnect(dri2_dpy
->wl_dpy
);
926 /* The drm platform does not create the screen/driver_configs but reuses
927 * the ones from the gbm device. As such the gbm itself is responsible
930 if (disp
->Platform
!= _EGL_PLATFORM_DRM
) {
931 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
932 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
933 free(dri2_dpy
->driver_configs
);
936 disp
->DriverData
= NULL
;
940 * Called via eglTerminate(), drv->API.Terminate().
942 * This must be guaranteed to be called exactly once, even if eglTerminate is
943 * called many times (without a eglInitialize in between).
946 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
948 /* Release all non-current Context/Surfaces. */
949 _eglReleaseDisplayResources(drv
, disp
);
951 dri2_display_release(disp
);
957 * Set the error code after a call to
958 * dri2_egl_display::dri2::createContextAttribs.
961 dri2_create_context_attribs_error(int dri_error
)
966 case __DRI_CTX_ERROR_SUCCESS
:
969 case __DRI_CTX_ERROR_NO_MEMORY
:
970 egl_error
= EGL_BAD_ALLOC
;
973 /* From the EGL_KHR_create_context spec, section "Errors":
975 * * If <config> does not support a client API context compatible
976 * with the requested API major and minor version, [...] context flags,
977 * and context reset notification behavior (for client API types where
978 * these attributes are supported), then an EGL_BAD_MATCH error is
981 * * If an OpenGL ES context is requested and the values for
982 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
983 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
984 * is not defined, than an EGL_BAD_MATCH error is generated.
986 * * If an OpenGL context is requested, the requested version is
987 * greater than 3.2, and the value for attribute
988 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
989 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
990 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
991 * one of these bits set; or if the implementation does not support
992 * the requested profile, then an EGL_BAD_MATCH error is generated.
994 case __DRI_CTX_ERROR_BAD_API
:
995 case __DRI_CTX_ERROR_BAD_VERSION
:
996 case __DRI_CTX_ERROR_BAD_FLAG
:
997 egl_error
= EGL_BAD_MATCH
;
1000 /* From the EGL_KHR_create_context spec, section "Errors":
1002 * * If an attribute name or attribute value in <attrib_list> is not
1003 * recognized (including unrecognized bits in bitmask attributes),
1004 * then an EGL_BAD_ATTRIBUTE error is generated."
1006 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
1007 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
1008 egl_error
= EGL_BAD_ATTRIBUTE
;
1013 egl_error
= EGL_BAD_MATCH
;
1017 _eglError(egl_error
, "dri2_create_context");
1021 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1022 struct dri2_egl_display
*dri2_dpy
,
1023 uint32_t *ctx_attribs
,
1024 unsigned *num_attribs
)
1028 assert(*num_attribs
>= 8);
1030 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1031 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1032 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1033 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1035 if (dri2_ctx
->base
.Flags
!= 0) {
1036 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1037 * extension, don't even try to send it the robust-access flag.
1038 * It may explode. Instead, generate the required EGL error here.
1040 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1041 && !dri2_dpy
->robustness
) {
1042 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1046 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1047 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
;
1050 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1051 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1052 * extension, don't even try to send it a reset strategy. It may
1053 * explode. Instead, generate the required EGL error here.
1055 if (!dri2_dpy
->robustness
) {
1056 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1060 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1061 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1070 * Called via eglCreateContext(), drv->API.CreateContext().
1072 static _EGLContext
*
1073 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1074 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1076 struct dri2_egl_context
*dri2_ctx
;
1077 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1078 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1079 __DRIcontext
*shared
=
1080 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1081 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1082 const __DRIconfig
*dri_config
;
1087 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1089 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1093 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1096 switch (dri2_ctx
->base
.ClientAPI
) {
1097 case EGL_OPENGL_ES_API
:
1098 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1100 api
= __DRI_API_GLES
;
1103 api
= __DRI_API_GLES2
;
1106 api
= __DRI_API_GLES3
;
1109 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1114 case EGL_OPENGL_API
:
1115 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1116 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1117 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1118 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1119 api
= __DRI_API_OPENGL_CORE
;
1121 api
= __DRI_API_OPENGL
;
1124 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1130 /* The config chosen here isn't necessarily
1131 * used for surfaces later.
1132 * A pixmap surface will use the single config.
1133 * This opportunity depends on disabling the
1134 * doubleBufferMode check in
1135 * src/mesa/main/context.c:check_compatible()
1137 if (dri2_config
->dri_double_config
[0])
1138 dri_config
= dri2_config
->dri_double_config
[0];
1140 dri_config
= dri2_config
->dri_single_config
[0];
1142 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
1143 * makes sure the back buffer will always be used.
1145 if (conf
->SurfaceType
& EGL_WINDOW_BIT
)
1146 dri2_ctx
->base
.WindowRenderBuffer
= EGL_BACK_BUFFER
;
1151 if (dri2_dpy
->image_driver
) {
1153 unsigned num_attribs
= 8;
1154 uint32_t ctx_attribs
[8];
1156 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1160 dri2_ctx
->dri_context
=
1161 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1169 dri2_create_context_attribs_error(error
);
1170 } else if (dri2_dpy
->dri2
) {
1171 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1173 unsigned num_attribs
= 8;
1174 uint32_t ctx_attribs
[8];
1176 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1180 dri2_ctx
->dri_context
=
1181 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1189 dri2_create_context_attribs_error(error
);
1191 dri2_ctx
->dri_context
=
1192 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1199 assert(dri2_dpy
->swrast
);
1200 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1202 unsigned num_attribs
= 8;
1203 uint32_t ctx_attribs
[8];
1205 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1209 dri2_ctx
->dri_context
=
1210 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1218 dri2_create_context_attribs_error(error
);
1220 dri2_ctx
->dri_context
=
1221 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1229 if (!dri2_ctx
->dri_context
)
1232 return &dri2_ctx
->base
;
1240 * Called via eglDestroyContext(), drv->API.DestroyContext().
1243 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1245 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1246 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1248 if (_eglPutContext(ctx
)) {
1249 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1257 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1259 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1261 if (!_eglPutSurface(surf
))
1264 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1268 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1271 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1272 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1274 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1275 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1276 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1277 _EGLContext
*old_ctx
;
1278 _EGLSurface
*old_dsurf
, *old_rsurf
;
1279 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1280 __DRIdrawable
*ddraw
, *rdraw
;
1285 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1287 /* make new bindings */
1288 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1289 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1293 /* flush before context switch */
1295 dri2_drv
->glFlush();
1297 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1298 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1299 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1302 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1303 dri2_dpy
->core
->unbindContext(old_cctx
);
1306 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1308 if (unbind
|| dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1309 dri2_destroy_surface(drv
, disp
, old_dsurf
);
1310 dri2_destroy_surface(drv
, disp
, old_rsurf
);
1313 dri2_dpy
->ref_count
++;
1315 EGLDisplay old_disp
= _eglGetDisplayHandle(old_ctx
->Resource
.Display
);
1316 dri2_destroy_context(drv
, disp
, old_ctx
);
1317 dri2_display_release(old_disp
);
1322 /* undo the previous _eglBindContext */
1323 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1324 assert(&dri2_ctx
->base
== ctx
&&
1325 tmp_dsurf
== dsurf
&&
1326 tmp_rsurf
== rsurf
);
1328 _eglPutSurface(dsurf
);
1329 _eglPutSurface(rsurf
);
1330 _eglPutContext(ctx
);
1332 _eglPutSurface(old_dsurf
);
1333 _eglPutSurface(old_rsurf
);
1334 _eglPutContext(old_ctx
);
1336 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1337 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1340 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1345 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1347 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1349 return dri2_surf
->dri_drawable
;
1353 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1356 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1358 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1360 return dri2_drv
->get_proc_address(procname
);
1364 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1365 _EGLConfig
*conf
, void *native_window
,
1366 const EGLint
*attrib_list
)
1368 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1369 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1374 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1375 _EGLConfig
*conf
, void *native_pixmap
,
1376 const EGLint
*attrib_list
)
1378 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1379 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1384 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1385 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1387 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1388 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
1392 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1395 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1396 return dri2_dpy
->vtbl
->swap_interval(drv
, dpy
, surf
, interval
);
1400 * Asks the client API to flush any rendering to the drawable so that we can
1401 * do our swapbuffers.
1404 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1406 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1407 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1409 if (dri2_dpy
->flush
) {
1410 if (dri2_dpy
->flush
->base
.version
>= 4) {
1411 /* We know there's a current context because:
1413 * "If surface is not bound to the calling thread’s current
1414 * context, an EGL_BAD_SURFACE error is generated."
1416 _EGLContext
*ctx
= _eglGetCurrentContext();
1417 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1419 /* From the EGL 1.4 spec (page 52):
1421 * "The contents of ancillary buffers are always undefined
1422 * after calling eglSwapBuffers."
1424 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1426 __DRI2_FLUSH_DRAWABLE
|
1427 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1428 __DRI2_THROTTLE_SWAPBUFFER
);
1430 dri2_dpy
->flush
->flush(dri_drawable
);
1436 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1438 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1439 return dri2_dpy
->vtbl
->swap_buffers(drv
, dpy
, surf
);
1443 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1445 const EGLint
*rects
, EGLint n_rects
)
1447 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1448 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, dpy
, surf
,
1453 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1454 EGLint numRects
, const EGLint
*rects
)
1456 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1457 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, dpy
, surf
, numRects
, rects
);
1461 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1462 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1464 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1465 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, dpy
, surf
, x
, y
, width
, height
);
1469 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1470 void *native_pixmap_target
)
1472 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1473 return dri2_dpy
->vtbl
->copy_buffers(drv
, dpy
, surf
, native_pixmap_target
);
1477 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1479 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1480 return dri2_dpy
->vtbl
->query_buffer_age(drv
, dpy
, surf
);
1484 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1486 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1487 _EGLSurface
*surf
= ctx
->DrawSurface
;
1488 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1492 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1493 * we need to copy fake to real here.*/
1495 if (dri2_dpy
->flush
!= NULL
)
1496 dri2_dpy
->flush
->flush(dri_drawable
);
1502 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1507 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1508 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1515 dri2_bind_tex_image(_EGLDriver
*drv
,
1516 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1518 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1519 struct dri2_egl_context
*dri2_ctx
;
1521 GLint format
, target
;
1522 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1524 ctx
= _eglGetCurrentContext();
1525 dri2_ctx
= dri2_egl_context(ctx
);
1527 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1530 switch (surf
->TextureFormat
) {
1531 case EGL_TEXTURE_RGB
:
1532 format
= __DRI_TEXTURE_FORMAT_RGB
;
1534 case EGL_TEXTURE_RGBA
:
1535 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1538 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1539 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1542 switch (surf
->TextureTarget
) {
1543 case EGL_TEXTURE_2D
:
1544 target
= GL_TEXTURE_2D
;
1547 target
= GL_TEXTURE_2D
;
1548 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1551 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1559 dri2_release_tex_image(_EGLDriver
*drv
,
1560 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1562 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1563 struct dri2_egl_context
*dri2_ctx
;
1566 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1568 ctx
= _eglGetCurrentContext();
1569 dri2_ctx
= dri2_egl_context(ctx
);
1571 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1574 switch (surf
->TextureTarget
) {
1575 case EGL_TEXTURE_2D
:
1576 target
= GL_TEXTURE_2D
;
1582 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1583 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1584 (*dri2_dpy
->tex_buffer
->releaseTexBuffer
)(dri2_ctx
->dri_context
,
1593 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
1594 EGLenum target
, EGLClientBuffer buffer
,
1595 const EGLint
*attr_list
)
1597 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1598 return dri2_dpy
->vtbl
->create_image(drv
, dpy
, ctx
, target
, buffer
,
1603 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1605 struct dri2_egl_image
*dri2_img
;
1607 if (dri_image
== NULL
) {
1608 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1612 dri2_img
= malloc(sizeof *dri2_img
);
1614 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1618 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1623 dri2_img
->dri_image
= dri_image
;
1625 return &dri2_img
->base
;
1629 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1630 EGLClientBuffer buffer
,
1631 const EGLint
*attr_list
)
1633 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1634 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1635 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1636 __DRIimage
*dri_image
;
1638 if (renderbuffer
== 0) {
1639 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1640 return EGL_NO_IMAGE_KHR
;
1644 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1645 renderbuffer
, NULL
);
1647 return dri2_create_image_from_dri(disp
, dri_image
);
1650 #ifdef HAVE_WAYLAND_PLATFORM
1652 /* This structure describes how a wl_buffer maps to one or more
1653 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1654 * offsets and strides of the planes in the buffer. This table maps a
1655 * wl_drm format code to a description of the planes in the buffer
1656 * that lets us create a __DRIimage for each of the planes. */
1658 static const struct wl_drm_components_descriptor
{
1659 uint32_t dri_components
;
1662 } wl_drm_components
[] = {
1663 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1664 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1665 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1666 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1667 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1671 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1672 EGLClientBuffer _buffer
,
1673 const EGLint
*attr_list
)
1675 struct wl_drm_buffer
*buffer
;
1676 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1677 const struct wl_drm_components_descriptor
*f
;
1678 __DRIimage
*dri_image
;
1679 _EGLImageAttribs attrs
;
1683 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1684 (struct wl_resource
*) _buffer
);
1688 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1689 plane
= attrs
.PlaneWL
;
1690 if (err
!= EGL_SUCCESS
) {
1691 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1695 f
= buffer
->driver_format
;
1696 if (plane
< 0 || plane
>= f
->nplanes
) {
1697 _eglError(EGL_BAD_PARAMETER
,
1698 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1702 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1704 if (dri_image
== NULL
) {
1705 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1709 return dri2_create_image_from_dri(disp
, dri_image
);
1714 dri2_get_sync_values_chromium(_EGLDisplay
*dpy
, _EGLSurface
*surf
,
1715 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1718 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1719 return dri2_dpy
->vtbl
->get_sync_values(dpy
, surf
, ust
, msc
, sbc
);
1723 * Set the error code after a call to
1724 * dri2_egl_image::dri_image::createImageFromTexture.
1727 dri2_create_image_khr_texture_error(int dri_error
)
1731 switch (dri_error
) {
1732 case __DRI_IMAGE_ERROR_SUCCESS
:
1735 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1736 egl_error
= EGL_BAD_ALLOC
;
1739 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1740 egl_error
= EGL_BAD_MATCH
;
1743 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1744 egl_error
= EGL_BAD_PARAMETER
;
1747 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
1748 egl_error
= EGL_BAD_ACCESS
;
1753 egl_error
= EGL_BAD_MATCH
;
1757 _eglError(egl_error
, "dri2_create_image_khr_texture");
1761 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1763 EGLClientBuffer buffer
,
1764 const EGLint
*attr_list
)
1766 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1767 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1768 struct dri2_egl_image
*dri2_img
;
1769 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
1770 _EGLImageAttribs attrs
;
1776 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1777 return EGL_NO_IMAGE_KHR
;
1780 if (_eglParseImageAttribList(&attrs
, disp
, attr_list
) != EGL_SUCCESS
)
1781 return EGL_NO_IMAGE_KHR
;
1784 case EGL_GL_TEXTURE_2D_KHR
:
1786 gl_target
= GL_TEXTURE_2D
;
1788 case EGL_GL_TEXTURE_3D_KHR
:
1789 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1790 depth
= attrs
.GLTextureZOffset
;
1791 gl_target
= GL_TEXTURE_3D
;
1795 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1796 return EGL_NO_IMAGE_KHR
;
1798 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1799 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1800 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1801 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1802 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1803 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1804 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
1805 gl_target
= GL_TEXTURE_CUBE_MAP
;
1808 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1809 return EGL_NO_IMAGE_KHR
;
1812 dri2_img
= malloc(sizeof *dri2_img
);
1814 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1815 return EGL_NO_IMAGE_KHR
;
1818 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1819 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1821 return EGL_NO_IMAGE_KHR
;
1824 dri2_img
->dri_image
=
1825 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
1829 attrs
.GLTextureLevel
,
1832 dri2_create_image_khr_texture_error(error
);
1834 if (!dri2_img
->dri_image
) {
1836 return EGL_NO_IMAGE_KHR
;
1838 return &dri2_img
->base
;
1842 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1843 EGLint attribute
, EGLint
*value
)
1845 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1846 if (!dri2_dpy
->vtbl
->query_surface
)
1847 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
1848 return dri2_dpy
->vtbl
->query_surface(drv
, dpy
, surf
, attribute
, value
);
1851 static struct wl_buffer
*
1852 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1855 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1856 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, dpy
, img
);
1861 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1862 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1864 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1865 EGLint format
, name
, pitch
, err
;
1866 _EGLImageAttribs attrs
;
1867 __DRIimage
*dri_image
;
1869 name
= (EGLint
) (uintptr_t) buffer
;
1871 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1872 if (err
!= EGL_SUCCESS
)
1875 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
1876 attrs
.DRMBufferStrideMESA
<= 0) {
1877 _eglError(EGL_BAD_PARAMETER
,
1878 "bad width, height or stride");
1882 switch (attrs
.DRMBufferFormatMESA
) {
1883 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1884 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1885 pitch
= attrs
.DRMBufferStrideMESA
;
1888 _eglError(EGL_BAD_PARAMETER
,
1889 "dri2_create_image_khr: unsupported pixmap depth");
1894 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1902 return dri2_create_image_from_dri(disp
, dri_image
);
1906 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
1913 * "Required attributes and their values are as follows:
1915 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1917 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1918 * by drm_fourcc.h and used as the pixel_format parameter of the
1919 * drm_mode_fb_cmd2 ioctl."
1923 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1924 * incomplete, EGL_BAD_PARAMETER is generated."
1926 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
1927 !attrs
->DMABufFourCC
.IsPresent
) {
1928 _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
1935 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1936 * specified for a plane's pitch or offset isn't supported by EGL,
1937 * EGL_BAD_ACCESS is generated."
1939 for (i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
1940 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
1941 attrs
->DMABufPlanePitches
[i
].Value
<= 0) {
1942 _eglError(EGL_BAD_ACCESS
, "invalid pitch");
1950 /* Returns the total number of file descriptors. Zero indicates an error. */
1952 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
1954 unsigned i
, plane_n
;
1956 switch (attrs
->DMABufFourCC
.Value
) {
1958 case DRM_FORMAT_RG88
:
1959 case DRM_FORMAT_GR88
:
1960 case DRM_FORMAT_RGB332
:
1961 case DRM_FORMAT_BGR233
:
1962 case DRM_FORMAT_XRGB4444
:
1963 case DRM_FORMAT_XBGR4444
:
1964 case DRM_FORMAT_RGBX4444
:
1965 case DRM_FORMAT_BGRX4444
:
1966 case DRM_FORMAT_ARGB4444
:
1967 case DRM_FORMAT_ABGR4444
:
1968 case DRM_FORMAT_RGBA4444
:
1969 case DRM_FORMAT_BGRA4444
:
1970 case DRM_FORMAT_XRGB1555
:
1971 case DRM_FORMAT_XBGR1555
:
1972 case DRM_FORMAT_RGBX5551
:
1973 case DRM_FORMAT_BGRX5551
:
1974 case DRM_FORMAT_ARGB1555
:
1975 case DRM_FORMAT_ABGR1555
:
1976 case DRM_FORMAT_RGBA5551
:
1977 case DRM_FORMAT_BGRA5551
:
1978 case DRM_FORMAT_RGB565
:
1979 case DRM_FORMAT_BGR565
:
1980 case DRM_FORMAT_RGB888
:
1981 case DRM_FORMAT_BGR888
:
1982 case DRM_FORMAT_XRGB8888
:
1983 case DRM_FORMAT_XBGR8888
:
1984 case DRM_FORMAT_RGBX8888
:
1985 case DRM_FORMAT_BGRX8888
:
1986 case DRM_FORMAT_ARGB8888
:
1987 case DRM_FORMAT_ABGR8888
:
1988 case DRM_FORMAT_RGBA8888
:
1989 case DRM_FORMAT_BGRA8888
:
1990 case DRM_FORMAT_XRGB2101010
:
1991 case DRM_FORMAT_XBGR2101010
:
1992 case DRM_FORMAT_RGBX1010102
:
1993 case DRM_FORMAT_BGRX1010102
:
1994 case DRM_FORMAT_ARGB2101010
:
1995 case DRM_FORMAT_ABGR2101010
:
1996 case DRM_FORMAT_RGBA1010102
:
1997 case DRM_FORMAT_BGRA1010102
:
1998 case DRM_FORMAT_YUYV
:
1999 case DRM_FORMAT_YVYU
:
2000 case DRM_FORMAT_UYVY
:
2001 case DRM_FORMAT_VYUY
:
2004 case DRM_FORMAT_NV12
:
2005 case DRM_FORMAT_NV21
:
2006 case DRM_FORMAT_NV16
:
2007 case DRM_FORMAT_NV61
:
2010 case DRM_FORMAT_YUV410
:
2011 case DRM_FORMAT_YVU410
:
2012 case DRM_FORMAT_YUV411
:
2013 case DRM_FORMAT_YVU411
:
2014 case DRM_FORMAT_YUV420
:
2015 case DRM_FORMAT_YVU420
:
2016 case DRM_FORMAT_YUV422
:
2017 case DRM_FORMAT_YVU422
:
2018 case DRM_FORMAT_YUV444
:
2019 case DRM_FORMAT_YVU444
:
2023 _eglError(EGL_BAD_ATTRIBUTE
, "invalid format");
2030 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2031 * incomplete, EGL_BAD_PARAMETER is generated."
2033 for (i
= 0; i
< plane_n
; ++i
) {
2034 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2035 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2036 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2037 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2043 * The spec also says:
2045 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2046 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2047 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2048 * attributes are specified."
2050 for (i
= plane_n
; i
< 3; ++i
) {
2051 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2052 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2053 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2054 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2065 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2066 * EGL will take a reference to the dma_buf(s) which it will release at any
2067 * time while the EGLDisplay is initialized. It is the responsibility of the
2068 * application to close the dma_buf file descriptors."
2070 * Therefore we must never close or otherwise modify the file descriptors.
2073 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2074 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2076 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2079 _EGLImageAttribs attrs
;
2080 __DRIimage
*dri_image
;
2091 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2092 * error EGL_BAD_PARAMETER is generated."
2094 if (buffer
!= NULL
) {
2095 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2099 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2100 if (err
!= EGL_SUCCESS
) {
2101 _eglError(err
, "bad attribute");
2105 if (!dri2_check_dma_buf_attribs(&attrs
))
2108 num_fds
= dri2_check_dma_buf_format(&attrs
);
2112 for (i
= 0; i
< num_fds
; ++i
) {
2113 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2114 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2115 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2119 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2120 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2121 fds
, num_fds
, pitches
, offsets
,
2122 attrs
.DMABufYuvColorSpaceHint
.Value
,
2123 attrs
.DMABufSampleRangeHint
.Value
,
2124 attrs
.DMABufChromaHorizontalSiting
.Value
,
2125 attrs
.DMABufChromaVerticalSiting
.Value
,
2128 dri2_create_image_khr_texture_error(error
);
2131 return EGL_NO_IMAGE_KHR
;
2133 res
= dri2_create_image_from_dri(disp
, dri_image
);
2138 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2139 const EGLint
*attr_list
)
2141 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2142 struct dri2_egl_image
*dri2_img
;
2143 _EGLImageAttribs attrs
;
2144 unsigned int dri_use
, valid_mask
;
2146 EGLint err
= EGL_SUCCESS
;
2150 dri2_img
= malloc(sizeof *dri2_img
);
2152 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2153 return EGL_NO_IMAGE_KHR
;
2157 err
= EGL_BAD_PARAMETER
;
2161 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
2162 err
= EGL_BAD_PARAMETER
;
2166 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2167 if (err
!= EGL_SUCCESS
)
2170 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2171 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)",
2172 attrs
.Width
, attrs
.Height
);
2176 switch (attrs
.DRMBufferFormatMESA
) {
2177 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2178 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2181 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x",
2182 attrs
.DRMBufferFormatMESA
);
2187 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2188 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2189 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2190 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2191 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x",
2192 attrs
.DRMBufferUseMESA
& ~valid_mask
);
2197 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2198 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2199 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2200 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2201 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2202 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2204 dri2_img
->dri_image
=
2205 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2206 attrs
.Width
, attrs
.Height
,
2207 format
, dri_use
, dri2_img
);
2208 if (dri2_img
->dri_image
== NULL
) {
2209 err
= EGL_BAD_ALLOC
;
2213 return &dri2_img
->base
;
2217 _eglError(err
, "dri2_create_drm_image_mesa");
2219 return EGL_NO_IMAGE_KHR
;
2223 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2224 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2226 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2227 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2231 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2232 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
2233 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2238 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2239 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2242 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2243 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2249 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2251 EGLint
*fourcc
, EGLint
*nplanes
,
2252 EGLuint64KHR
*modifiers
)
2254 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2255 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2261 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2262 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2264 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2265 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2274 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2275 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2277 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2278 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2282 /* rework later to provide multiple fds/strides/offsets */
2284 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2285 __DRI_IMAGE_ATTRIB_FD
, fds
);
2288 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2289 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2293 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2294 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2296 offsets
[0] = img_offset
;
2307 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2308 _EGLContext
*ctx
, EGLenum target
,
2309 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2314 case EGL_GL_TEXTURE_2D_KHR
:
2315 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2316 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2317 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2318 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2319 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2320 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2321 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2322 case EGL_GL_TEXTURE_3D_KHR
:
2323 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
2324 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2327 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2328 return EGL_NO_IMAGE_KHR
;
2330 case EGL_GL_RENDERBUFFER_KHR
:
2331 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2333 case EGL_DRM_BUFFER_MESA
:
2334 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2335 case EGL_LINUX_DMA_BUF_EXT
:
2336 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2338 #ifdef HAVE_WAYLAND_PLATFORM
2339 case EGL_WAYLAND_BUFFER_WL
:
2340 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2343 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2344 return EGL_NO_IMAGE_KHR
;
2349 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2351 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2352 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2356 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2362 #ifdef HAVE_WAYLAND_PLATFORM
2365 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2366 struct wl_drm_buffer
*buffer
)
2368 _EGLDisplay
*disp
= user_data
;
2369 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2371 int i
, dri_components
= 0;
2374 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2383 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2395 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2397 buffer
->driver_format
= NULL
;
2398 for (i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2399 if (wl_drm_components
[i
].dri_components
== dri_components
)
2400 buffer
->driver_format
= &wl_drm_components
[i
];
2402 if (buffer
->driver_format
== NULL
)
2403 dri2_dpy
->image
->destroyImage(img
);
2405 buffer
->driver_buffer
= img
;
2409 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2411 _EGLDisplay
*disp
= user_data
;
2412 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2414 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2417 static struct wayland_drm_callbacks wl_drm_callbacks
= {
2418 .authenticate
= NULL
,
2419 .reference_buffer
= dri2_wl_reference_buffer
,
2420 .release_buffer
= dri2_wl_release_buffer
2424 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2425 struct wl_display
*wl_dpy
)
2427 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2433 if (dri2_dpy
->wl_server_drm
)
2436 wl_drm_callbacks
.authenticate
=
2437 (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
;
2439 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2440 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2441 dri2_dpy
->image
->base
.version
>= 7 &&
2442 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2443 flags
|= WAYLAND_DRM_PRIME
;
2445 dri2_dpy
->wl_server_drm
=
2446 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2447 &wl_drm_callbacks
, disp
, flags
);
2449 if (!dri2_dpy
->wl_server_drm
)
2452 #ifdef HAVE_DRM_PLATFORM
2453 /* We have to share the wl_drm instance with gbm, so gbm can convert
2454 * wl_buffers to gbm bos. */
2455 if (dri2_dpy
->gbm_dri
)
2456 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2463 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2464 struct wl_display
*wl_dpy
)
2466 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2470 if (!dri2_dpy
->wl_server_drm
)
2473 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2474 dri2_dpy
->wl_server_drm
= NULL
;
2480 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2481 struct wl_resource
*buffer_resource
,
2482 EGLint attribute
, EGLint
*value
)
2484 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2485 struct wl_drm_buffer
*buffer
;
2486 const struct wl_drm_components_descriptor
*format
;
2488 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2492 format
= buffer
->driver_format
;
2493 switch (attribute
) {
2494 case EGL_TEXTURE_FORMAT
:
2495 *value
= format
->components
;
2498 *value
= buffer
->width
;
2501 *value
= buffer
->height
;
2510 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2512 p_atomic_inc(&sync
->refcount
);
2516 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2517 struct dri2_egl_sync
*dri2_sync
)
2519 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2520 switch (dri2_sync
->base
.Type
) {
2521 case EGL_SYNC_REUSABLE_KHR
:
2522 cnd_destroy(&dri2_sync
->cond
);
2524 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2525 if (dri2_sync
->base
.SyncFd
!= EGL_NO_NATIVE_FENCE_FD_ANDROID
)
2526 close(dri2_sync
->base
.SyncFd
);
2532 if (dri2_sync
->fence
)
2533 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2540 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
2541 EGLenum type
, const EGLAttrib
*attrib_list
)
2543 _EGLContext
*ctx
= _eglGetCurrentContext();
2544 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2545 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2546 struct dri2_egl_sync
*dri2_sync
;
2548 pthread_condattr_t attr
;
2550 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2552 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2556 if (!_eglInitSync(&dri2_sync
->base
, dpy
, type
, attrib_list
)) {
2562 case EGL_SYNC_FENCE_KHR
:
2563 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2564 if (!dri2_sync
->fence
) {
2565 /* Why did it fail? DRI doesn't return an error code, so we emit
2566 * a generic EGL error that doesn't communicate user error.
2568 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2574 case EGL_SYNC_CL_EVENT_KHR
:
2575 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2576 dri2_dpy
->dri_screen
,
2577 dri2_sync
->base
.CLEvent
);
2578 /* this can only happen if the cl_event passed in is invalid. */
2579 if (!dri2_sync
->fence
) {
2580 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2585 /* the initial status must be "signaled" if the cl_event is signaled */
2586 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2587 dri2_sync
->fence
, 0, 0))
2588 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2591 case EGL_SYNC_REUSABLE_KHR
:
2592 /* intialize attr */
2593 ret
= pthread_condattr_init(&attr
);
2596 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2601 /* change clock attribute to CLOCK_MONOTONIC */
2602 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
2605 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2610 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
2613 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2618 /* initial status of reusable sync must be "unsignaled" */
2619 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
2622 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2623 if (dri2_dpy
->fence
->create_fence_fd
) {
2624 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence_fd(
2625 dri2_ctx
->dri_context
,
2626 dri2_sync
->base
.SyncFd
);
2628 if (!dri2_sync
->fence
) {
2629 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2636 p_atomic_set(&dri2_sync
->refcount
, 1);
2637 return &dri2_sync
->base
;
2641 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2643 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2644 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2645 EGLint ret
= EGL_TRUE
;
2648 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
2649 * then unlock all threads possibly blocked by the reusable sync before
2652 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
2653 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2654 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2655 /* unblock all threads currently blocked by sync */
2656 err
= cnd_broadcast(&dri2_sync
->cond
);
2659 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
2664 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2670 dri2_dup_native_fence_fd(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2672 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2673 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2675 assert(sync
->Type
== EGL_SYNC_NATIVE_FENCE_ANDROID
);
2677 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
2678 /* try to retrieve the actual native fence fd.. if rendering is
2679 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
2681 sync
->SyncFd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
2685 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
2686 /* if native fence fd still not created, return an error: */
2687 _eglError(EGL_BAD_PARAMETER
, "eglDupNativeFenceFDANDROID");
2688 return EGL_NO_NATIVE_FENCE_FD_ANDROID
;
2691 return dup(sync
->SyncFd
);
2695 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2696 EGLint flags
, EGLTime timeout
)
2698 _EGLContext
*ctx
= _eglGetCurrentContext();
2699 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2700 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2701 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2702 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2703 unsigned wait_flags
= 0;
2705 /* timespecs for cnd_timedwait */
2706 struct timespec current
;
2709 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
2711 /* The EGL_KHR_fence_sync spec states:
2713 * "If no context is current for the bound API,
2714 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
2716 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
2717 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
2719 /* the sync object should take a reference while waiting */
2720 dri2_egl_ref_sync(dri2_sync
);
2722 switch (sync
->Type
) {
2723 case EGL_SYNC_FENCE_KHR
:
2724 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2725 case EGL_SYNC_CL_EVENT_KHR
:
2726 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
2727 dri2_sync
->fence
, wait_flags
,
2729 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2731 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2734 case EGL_SYNC_REUSABLE_KHR
:
2735 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
2736 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
2737 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
2738 dri2_drv
->glFlush();
2741 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
2742 if (timeout
== EGL_FOREVER_KHR
) {
2743 mtx_lock(&dri2_sync
->mutex
);
2744 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
2745 mtx_unlock(&dri2_sync
->mutex
);
2747 /* if reusable sync has not been yet signaled */
2748 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
2749 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
2751 /* calculating when to expire */
2752 expire
.nsec
= timeout
% 1000000000L;
2753 expire
.sec
= timeout
/ 1000000000L;
2755 expire
.nsec
+= current
.tv_nsec
;
2756 expire
.sec
+= current
.tv_sec
;
2758 /* expire.nsec now is a number between 0 and 1999999998 */
2759 if (expire
.nsec
> 999999999L) {
2761 expire
.nsec
-= 1000000000L;
2764 mtx_lock(&dri2_sync
->mutex
);
2765 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
2766 mtx_unlock(&dri2_sync
->mutex
);
2768 if (ret
== thrd_busy
) {
2769 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2770 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2772 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2780 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2786 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2789 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2792 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
) {
2793 _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
2797 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
) {
2798 _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
2802 dri2_sync
->base
.SyncStatus
= mode
;
2804 if (mode
== EGL_SIGNALED_KHR
) {
2805 ret
= cnd_broadcast(&dri2_sync
->cond
);
2807 /* fail to broadcast */
2809 _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
2818 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2820 _EGLContext
*ctx
= _eglGetCurrentContext();
2821 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2822 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2823 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2825 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
2826 dri2_sync
->fence
, 0);
2831 dri2_interop_query_device_info(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2832 struct mesa_glinterop_device_info
*out
)
2834 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2835 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2837 if (!dri2_dpy
->interop
)
2838 return MESA_GLINTEROP_UNSUPPORTED
;
2840 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
2844 dri2_interop_export_object(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2845 struct mesa_glinterop_export_in
*in
,
2846 struct mesa_glinterop_export_out
*out
)
2848 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2849 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2851 if (!dri2_dpy
->interop
)
2852 return MESA_GLINTEROP_UNSUPPORTED
;
2854 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
2858 dri2_unload(_EGLDriver
*drv
)
2860 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2862 dlclose(dri2_drv
->handle
);
2867 dri2_load(_EGLDriver
*drv
)
2869 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2870 #ifdef HAVE_ANDROID_PLATFORM
2871 const char *libname
= "libglapi.so";
2872 #elif defined(__APPLE__)
2873 const char *libname
= "libglapi.0.dylib";
2874 #elif defined(__CYGWIN__)
2875 const char *libname
= "cygglapi-0.dll";
2877 const char *libname
= "libglapi.so.0";
2881 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2882 handle
= dlopen(libname
, RTLD_LAZY
| RTLD_GLOBAL
);
2884 _eglLog(_EGL_WARNING
, "DRI2: failed to open glapi provider");
2888 dri2_drv
->get_proc_address
= (_EGLProc (*)(const char *))
2889 dlsym(handle
, "_glapi_get_proc_address");
2891 /* if glapi is not available, loading DRI drivers will fail */
2892 if (!dri2_drv
->get_proc_address
) {
2893 _eglLog(_EGL_WARNING
, "DRI2: failed to find _glapi_get_proc_address");
2897 dri2_drv
->glFlush
= (void (*)(void))
2898 dri2_drv
->get_proc_address("glFlush");
2900 /* if glFlush is not available things are horribly broken */
2901 if (!dri2_drv
->glFlush
) {
2902 _eglLog(_EGL_WARNING
, "DRI2: failed to find glFlush entry point");
2906 dri2_drv
->handle
= handle
;
2916 * This is the main entrypoint into the driver, called by libEGL.
2917 * Create a new _EGLDriver object and init its dispatch table.
2920 _eglBuiltInDriverDRI2(const char *args
)
2922 struct dri2_egl_driver
*dri2_drv
;
2926 dri2_drv
= calloc(1, sizeof *dri2_drv
);
2930 if (!dri2_load(&dri2_drv
->base
)) {
2935 _eglInitDriverFallbacks(&dri2_drv
->base
);
2936 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
2937 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
2938 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
2939 dri2_drv
->base
.API
.DestroyContext
= dri2_destroy_context
;
2940 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
2941 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
2942 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
2943 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
2944 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
2945 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
2946 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
2947 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
2948 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
2949 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
2950 dri2_drv
->base
.API
.SwapInterval
= dri2_swap_interval
;
2951 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
2952 dri2_drv
->base
.API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
2953 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
2954 dri2_drv
->base
.API
.PostSubBufferNV
= dri2_post_sub_buffer
;
2955 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
,
2956 dri2_drv
->base
.API
.QueryBufferAge
= dri2_query_buffer_age
;
2957 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image
;
2958 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
2959 dri2_drv
->base
.API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
2960 dri2_drv
->base
.API
.QuerySurface
= dri2_query_surface
;
2962 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
2963 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
2964 dri2_drv
->base
.API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
2965 dri2_drv
->base
.API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
2967 #ifdef HAVE_WAYLAND_PLATFORM
2968 dri2_drv
->base
.API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
2969 dri2_drv
->base
.API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
2970 dri2_drv
->base
.API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
2972 dri2_drv
->base
.API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
2973 dri2_drv
->base
.API
.CreateSyncKHR
= dri2_create_sync
;
2974 dri2_drv
->base
.API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
2975 dri2_drv
->base
.API
.SignalSyncKHR
= dri2_signal_sync
;
2976 dri2_drv
->base
.API
.WaitSyncKHR
= dri2_server_wait_sync
;
2977 dri2_drv
->base
.API
.DestroySyncKHR
= dri2_destroy_sync
;
2978 dri2_drv
->base
.API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
2979 dri2_drv
->base
.API
.GLInteropExportObject
= dri2_interop_export_object
;
2980 dri2_drv
->base
.API
.DupNativeFenceFDANDROID
= dri2_dup_native_fence_fd
;
2982 dri2_drv
->base
.Name
= "DRI2";
2983 dri2_drv
->base
.Unload
= dri2_unload
;
2985 return &dri2_drv
->base
;