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 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
918 if (dri2_dpy
->own_device
) {
919 wl_display_disconnect(dri2_dpy
->wl_dpy
);
927 /* The drm platform does not create the screen/driver_configs but reuses
928 * the ones from the gbm device. As such the gbm itself is responsible
931 if (disp
->Platform
!= _EGL_PLATFORM_DRM
) {
932 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
933 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
934 free(dri2_dpy
->driver_configs
);
937 disp
->DriverData
= NULL
;
941 * Called via eglTerminate(), drv->API.Terminate().
943 * This must be guaranteed to be called exactly once, even if eglTerminate is
944 * called many times (without a eglInitialize in between).
947 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
949 /* Release all non-current Context/Surfaces. */
950 _eglReleaseDisplayResources(drv
, disp
);
952 dri2_display_release(disp
);
958 * Set the error code after a call to
959 * dri2_egl_display::dri2::createContextAttribs.
962 dri2_create_context_attribs_error(int dri_error
)
967 case __DRI_CTX_ERROR_SUCCESS
:
970 case __DRI_CTX_ERROR_NO_MEMORY
:
971 egl_error
= EGL_BAD_ALLOC
;
974 /* From the EGL_KHR_create_context spec, section "Errors":
976 * * If <config> does not support a client API context compatible
977 * with the requested API major and minor version, [...] context flags,
978 * and context reset notification behavior (for client API types where
979 * these attributes are supported), then an EGL_BAD_MATCH error is
982 * * If an OpenGL ES context is requested and the values for
983 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
984 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
985 * is not defined, than an EGL_BAD_MATCH error is generated.
987 * * If an OpenGL context is requested, the requested version is
988 * greater than 3.2, and the value for attribute
989 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
990 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
991 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
992 * one of these bits set; or if the implementation does not support
993 * the requested profile, then an EGL_BAD_MATCH error is generated.
995 case __DRI_CTX_ERROR_BAD_API
:
996 case __DRI_CTX_ERROR_BAD_VERSION
:
997 case __DRI_CTX_ERROR_BAD_FLAG
:
998 egl_error
= EGL_BAD_MATCH
;
1001 /* From the EGL_KHR_create_context spec, section "Errors":
1003 * * If an attribute name or attribute value in <attrib_list> is not
1004 * recognized (including unrecognized bits in bitmask attributes),
1005 * then an EGL_BAD_ATTRIBUTE error is generated."
1007 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
1008 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
1009 egl_error
= EGL_BAD_ATTRIBUTE
;
1014 egl_error
= EGL_BAD_MATCH
;
1018 _eglError(egl_error
, "dri2_create_context");
1022 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
1023 struct dri2_egl_display
*dri2_dpy
,
1024 uint32_t *ctx_attribs
,
1025 unsigned *num_attribs
)
1029 assert(*num_attribs
>= 8);
1031 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
1032 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
1033 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
1034 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
1036 if (dri2_ctx
->base
.Flags
!= 0) {
1037 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1038 * extension, don't even try to send it the robust-access flag.
1039 * It may explode. Instead, generate the required EGL error here.
1041 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
1042 && !dri2_dpy
->robustness
) {
1043 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
1047 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
1048 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
;
1051 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
1052 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
1053 * extension, don't even try to send it a reset strategy. It may
1054 * explode. Instead, generate the required EGL error here.
1056 if (!dri2_dpy
->robustness
) {
1057 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
1061 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
1062 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
1071 * Called via eglCreateContext(), drv->API.CreateContext().
1073 static _EGLContext
*
1074 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
1075 _EGLContext
*share_list
, const EGLint
*attrib_list
)
1077 struct dri2_egl_context
*dri2_ctx
;
1078 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1079 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
1080 __DRIcontext
*shared
=
1081 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
1082 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
1083 const __DRIconfig
*dri_config
;
1088 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1090 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1094 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1097 switch (dri2_ctx
->base
.ClientAPI
) {
1098 case EGL_OPENGL_ES_API
:
1099 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1101 api
= __DRI_API_GLES
;
1104 api
= __DRI_API_GLES2
;
1107 api
= __DRI_API_GLES3
;
1110 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1115 case EGL_OPENGL_API
:
1116 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1117 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1118 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1119 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1120 api
= __DRI_API_OPENGL_CORE
;
1122 api
= __DRI_API_OPENGL
;
1125 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1131 /* The config chosen here isn't necessarily
1132 * used for surfaces later.
1133 * A pixmap surface will use the single config.
1134 * This opportunity depends on disabling the
1135 * doubleBufferMode check in
1136 * src/mesa/main/context.c:check_compatible()
1138 if (dri2_config
->dri_double_config
[0])
1139 dri_config
= dri2_config
->dri_double_config
[0];
1141 dri_config
= dri2_config
->dri_single_config
[0];
1143 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
1144 * makes sure the back buffer will always be used.
1146 if (conf
->SurfaceType
& EGL_WINDOW_BIT
)
1147 dri2_ctx
->base
.WindowRenderBuffer
= EGL_BACK_BUFFER
;
1152 if (dri2_dpy
->image_driver
) {
1154 unsigned num_attribs
= 8;
1155 uint32_t ctx_attribs
[8];
1157 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1161 dri2_ctx
->dri_context
=
1162 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1170 dri2_create_context_attribs_error(error
);
1171 } else if (dri2_dpy
->dri2
) {
1172 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1174 unsigned num_attribs
= 8;
1175 uint32_t ctx_attribs
[8];
1177 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1181 dri2_ctx
->dri_context
=
1182 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1190 dri2_create_context_attribs_error(error
);
1192 dri2_ctx
->dri_context
=
1193 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1200 assert(dri2_dpy
->swrast
);
1201 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1203 unsigned num_attribs
= 8;
1204 uint32_t ctx_attribs
[8];
1206 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1210 dri2_ctx
->dri_context
=
1211 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1219 dri2_create_context_attribs_error(error
);
1221 dri2_ctx
->dri_context
=
1222 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1230 if (!dri2_ctx
->dri_context
)
1233 return &dri2_ctx
->base
;
1241 * Called via eglDestroyContext(), drv->API.DestroyContext().
1244 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1246 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1247 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1249 if (_eglPutContext(ctx
)) {
1250 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1258 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1260 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1262 if (!_eglPutSurface(surf
))
1265 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1269 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1272 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1273 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1275 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1276 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1277 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1278 _EGLContext
*old_ctx
;
1279 _EGLSurface
*old_dsurf
, *old_rsurf
;
1280 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1281 __DRIdrawable
*ddraw
, *rdraw
;
1286 return _eglError(EGL_NOT_INITIALIZED
, "eglMakeCurrent");
1288 /* make new bindings */
1289 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
)) {
1290 /* _eglBindContext already sets the EGL error (in _eglCheckMakeCurrent) */
1294 /* flush before context switch */
1296 dri2_drv
->glFlush();
1298 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1299 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1300 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1303 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1304 dri2_dpy
->core
->unbindContext(old_cctx
);
1307 unbind
= (cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
);
1309 if (unbind
|| dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1310 dri2_destroy_surface(drv
, disp
, old_dsurf
);
1311 dri2_destroy_surface(drv
, disp
, old_rsurf
);
1314 dri2_dpy
->ref_count
++;
1316 EGLDisplay old_disp
= _eglGetDisplayHandle(old_ctx
->Resource
.Display
);
1317 dri2_destroy_context(drv
, disp
, old_ctx
);
1318 dri2_display_release(old_disp
);
1323 /* undo the previous _eglBindContext */
1324 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1325 assert(&dri2_ctx
->base
== ctx
&&
1326 tmp_dsurf
== dsurf
&&
1327 tmp_rsurf
== rsurf
);
1329 _eglPutSurface(dsurf
);
1330 _eglPutSurface(rsurf
);
1331 _eglPutContext(ctx
);
1333 _eglPutSurface(old_dsurf
);
1334 _eglPutSurface(old_rsurf
);
1335 _eglPutContext(old_ctx
);
1337 /* dri2_dpy->core->bindContext failed. We cannot tell for sure why, but
1338 * setting the error to EGL_BAD_MATCH is surely better than leaving it
1341 return _eglError(EGL_BAD_MATCH
, "eglMakeCurrent");
1346 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1348 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1350 return dri2_surf
->dri_drawable
;
1354 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1357 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1359 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1361 return dri2_drv
->get_proc_address(procname
);
1365 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1366 _EGLConfig
*conf
, void *native_window
,
1367 const EGLint
*attrib_list
)
1369 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1370 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1375 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1376 _EGLConfig
*conf
, void *native_pixmap
,
1377 const EGLint
*attrib_list
)
1379 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1380 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1385 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1386 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1388 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1389 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
1393 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1396 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1397 return dri2_dpy
->vtbl
->swap_interval(drv
, dpy
, surf
, interval
);
1401 * Asks the client API to flush any rendering to the drawable so that we can
1402 * do our swapbuffers.
1405 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1407 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1408 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1410 if (dri2_dpy
->flush
) {
1411 if (dri2_dpy
->flush
->base
.version
>= 4) {
1412 /* We know there's a current context because:
1414 * "If surface is not bound to the calling thread’s current
1415 * context, an EGL_BAD_SURFACE error is generated."
1417 _EGLContext
*ctx
= _eglGetCurrentContext();
1418 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1420 /* From the EGL 1.4 spec (page 52):
1422 * "The contents of ancillary buffers are always undefined
1423 * after calling eglSwapBuffers."
1425 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1427 __DRI2_FLUSH_DRAWABLE
|
1428 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1429 __DRI2_THROTTLE_SWAPBUFFER
);
1431 dri2_dpy
->flush
->flush(dri_drawable
);
1437 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1439 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1440 return dri2_dpy
->vtbl
->swap_buffers(drv
, dpy
, surf
);
1444 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1446 const EGLint
*rects
, EGLint n_rects
)
1448 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1449 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, dpy
, surf
,
1454 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1455 EGLint numRects
, const EGLint
*rects
)
1457 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1458 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, dpy
, surf
, numRects
, rects
);
1462 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1463 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1465 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1466 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, dpy
, surf
, x
, y
, width
, height
);
1470 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1471 void *native_pixmap_target
)
1473 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1474 return dri2_dpy
->vtbl
->copy_buffers(drv
, dpy
, surf
, native_pixmap_target
);
1478 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1480 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1481 return dri2_dpy
->vtbl
->query_buffer_age(drv
, dpy
, surf
);
1485 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1487 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1488 _EGLSurface
*surf
= ctx
->DrawSurface
;
1489 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1493 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1494 * we need to copy fake to real here.*/
1496 if (dri2_dpy
->flush
!= NULL
)
1497 dri2_dpy
->flush
->flush(dri_drawable
);
1503 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1508 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1509 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1516 dri2_bind_tex_image(_EGLDriver
*drv
,
1517 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1519 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1520 struct dri2_egl_context
*dri2_ctx
;
1522 GLint format
, target
;
1523 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1525 ctx
= _eglGetCurrentContext();
1526 dri2_ctx
= dri2_egl_context(ctx
);
1528 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1531 switch (surf
->TextureFormat
) {
1532 case EGL_TEXTURE_RGB
:
1533 format
= __DRI_TEXTURE_FORMAT_RGB
;
1535 case EGL_TEXTURE_RGBA
:
1536 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1539 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1540 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1543 switch (surf
->TextureTarget
) {
1544 case EGL_TEXTURE_2D
:
1545 target
= GL_TEXTURE_2D
;
1548 target
= GL_TEXTURE_2D
;
1549 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1552 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1560 dri2_release_tex_image(_EGLDriver
*drv
,
1561 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1563 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1564 struct dri2_egl_context
*dri2_ctx
;
1567 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1569 ctx
= _eglGetCurrentContext();
1570 dri2_ctx
= dri2_egl_context(ctx
);
1572 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1575 switch (surf
->TextureTarget
) {
1576 case EGL_TEXTURE_2D
:
1577 target
= GL_TEXTURE_2D
;
1583 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1584 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1585 (*dri2_dpy
->tex_buffer
->releaseTexBuffer
)(dri2_ctx
->dri_context
,
1594 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
1595 EGLenum target
, EGLClientBuffer buffer
,
1596 const EGLint
*attr_list
)
1598 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1599 return dri2_dpy
->vtbl
->create_image(drv
, dpy
, ctx
, target
, buffer
,
1604 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1606 struct dri2_egl_image
*dri2_img
;
1608 if (dri_image
== NULL
) {
1609 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1613 dri2_img
= malloc(sizeof *dri2_img
);
1615 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1619 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1624 dri2_img
->dri_image
= dri_image
;
1626 return &dri2_img
->base
;
1630 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1631 EGLClientBuffer buffer
,
1632 const EGLint
*attr_list
)
1634 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1635 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1636 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1637 __DRIimage
*dri_image
;
1639 if (renderbuffer
== 0) {
1640 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1641 return EGL_NO_IMAGE_KHR
;
1645 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1646 renderbuffer
, NULL
);
1648 return dri2_create_image_from_dri(disp
, dri_image
);
1651 #ifdef HAVE_WAYLAND_PLATFORM
1653 /* This structure describes how a wl_buffer maps to one or more
1654 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1655 * offsets and strides of the planes in the buffer. This table maps a
1656 * wl_drm format code to a description of the planes in the buffer
1657 * that lets us create a __DRIimage for each of the planes. */
1659 static const struct wl_drm_components_descriptor
{
1660 uint32_t dri_components
;
1663 } wl_drm_components
[] = {
1664 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1665 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1666 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1667 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1668 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1672 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1673 EGLClientBuffer _buffer
,
1674 const EGLint
*attr_list
)
1676 struct wl_drm_buffer
*buffer
;
1677 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1678 const struct wl_drm_components_descriptor
*f
;
1679 __DRIimage
*dri_image
;
1680 _EGLImageAttribs attrs
;
1684 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1685 (struct wl_resource
*) _buffer
);
1689 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1690 plane
= attrs
.PlaneWL
;
1691 if (err
!= EGL_SUCCESS
) {
1692 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1696 f
= buffer
->driver_format
;
1697 if (plane
< 0 || plane
>= f
->nplanes
) {
1698 _eglError(EGL_BAD_PARAMETER
,
1699 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1703 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1705 if (dri_image
== NULL
) {
1706 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1710 return dri2_create_image_from_dri(disp
, dri_image
);
1715 dri2_get_sync_values_chromium(_EGLDisplay
*dpy
, _EGLSurface
*surf
,
1716 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1719 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1720 return dri2_dpy
->vtbl
->get_sync_values(dpy
, surf
, ust
, msc
, sbc
);
1724 * Set the error code after a call to
1725 * dri2_egl_image::dri_image::createImageFromTexture.
1728 dri2_create_image_khr_texture_error(int dri_error
)
1732 switch (dri_error
) {
1733 case __DRI_IMAGE_ERROR_SUCCESS
:
1736 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1737 egl_error
= EGL_BAD_ALLOC
;
1740 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1741 egl_error
= EGL_BAD_MATCH
;
1744 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1745 egl_error
= EGL_BAD_PARAMETER
;
1748 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
1749 egl_error
= EGL_BAD_ACCESS
;
1754 egl_error
= EGL_BAD_MATCH
;
1758 _eglError(egl_error
, "dri2_create_image_khr_texture");
1762 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1764 EGLClientBuffer buffer
,
1765 const EGLint
*attr_list
)
1767 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1768 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1769 struct dri2_egl_image
*dri2_img
;
1770 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
1771 _EGLImageAttribs attrs
;
1777 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1778 return EGL_NO_IMAGE_KHR
;
1781 if (_eglParseImageAttribList(&attrs
, disp
, attr_list
) != EGL_SUCCESS
)
1782 return EGL_NO_IMAGE_KHR
;
1785 case EGL_GL_TEXTURE_2D_KHR
:
1787 gl_target
= GL_TEXTURE_2D
;
1789 case EGL_GL_TEXTURE_3D_KHR
:
1790 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1791 depth
= attrs
.GLTextureZOffset
;
1792 gl_target
= GL_TEXTURE_3D
;
1796 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1797 return EGL_NO_IMAGE_KHR
;
1799 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1800 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1801 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1802 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1803 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1804 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1805 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
1806 gl_target
= GL_TEXTURE_CUBE_MAP
;
1809 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1810 return EGL_NO_IMAGE_KHR
;
1813 dri2_img
= malloc(sizeof *dri2_img
);
1815 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1816 return EGL_NO_IMAGE_KHR
;
1819 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1820 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1822 return EGL_NO_IMAGE_KHR
;
1825 dri2_img
->dri_image
=
1826 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
1830 attrs
.GLTextureLevel
,
1833 dri2_create_image_khr_texture_error(error
);
1835 if (!dri2_img
->dri_image
) {
1837 return EGL_NO_IMAGE_KHR
;
1839 return &dri2_img
->base
;
1843 dri2_query_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1844 EGLint attribute
, EGLint
*value
)
1846 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1847 if (!dri2_dpy
->vtbl
->query_surface
)
1848 return _eglQuerySurface(drv
, dpy
, surf
, attribute
, value
);
1849 return dri2_dpy
->vtbl
->query_surface(drv
, dpy
, surf
, attribute
, value
);
1852 static struct wl_buffer
*
1853 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1856 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1857 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, dpy
, img
);
1862 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1863 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1865 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1866 EGLint format
, name
, pitch
, err
;
1867 _EGLImageAttribs attrs
;
1868 __DRIimage
*dri_image
;
1870 name
= (EGLint
) (uintptr_t) buffer
;
1872 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1873 if (err
!= EGL_SUCCESS
)
1876 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
1877 attrs
.DRMBufferStrideMESA
<= 0) {
1878 _eglError(EGL_BAD_PARAMETER
,
1879 "bad width, height or stride");
1883 switch (attrs
.DRMBufferFormatMESA
) {
1884 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1885 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1886 pitch
= attrs
.DRMBufferStrideMESA
;
1889 _eglError(EGL_BAD_PARAMETER
,
1890 "dri2_create_image_khr: unsupported pixmap depth");
1895 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1903 return dri2_create_image_from_dri(disp
, dri_image
);
1907 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
1914 * "Required attributes and their values are as follows:
1916 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1918 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1919 * by drm_fourcc.h and used as the pixel_format parameter of the
1920 * drm_mode_fb_cmd2 ioctl."
1924 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1925 * incomplete, EGL_BAD_PARAMETER is generated."
1927 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
1928 !attrs
->DMABufFourCC
.IsPresent
) {
1929 _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
1936 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1937 * specified for a plane's pitch or offset isn't supported by EGL,
1938 * EGL_BAD_ACCESS is generated."
1940 for (i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
1941 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
1942 attrs
->DMABufPlanePitches
[i
].Value
<= 0) {
1943 _eglError(EGL_BAD_ACCESS
, "invalid pitch");
1951 /* Returns the total number of file descriptors. Zero indicates an error. */
1953 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
1955 unsigned i
, plane_n
;
1957 switch (attrs
->DMABufFourCC
.Value
) {
1959 case DRM_FORMAT_RG88
:
1960 case DRM_FORMAT_GR88
:
1961 case DRM_FORMAT_RGB332
:
1962 case DRM_FORMAT_BGR233
:
1963 case DRM_FORMAT_XRGB4444
:
1964 case DRM_FORMAT_XBGR4444
:
1965 case DRM_FORMAT_RGBX4444
:
1966 case DRM_FORMAT_BGRX4444
:
1967 case DRM_FORMAT_ARGB4444
:
1968 case DRM_FORMAT_ABGR4444
:
1969 case DRM_FORMAT_RGBA4444
:
1970 case DRM_FORMAT_BGRA4444
:
1971 case DRM_FORMAT_XRGB1555
:
1972 case DRM_FORMAT_XBGR1555
:
1973 case DRM_FORMAT_RGBX5551
:
1974 case DRM_FORMAT_BGRX5551
:
1975 case DRM_FORMAT_ARGB1555
:
1976 case DRM_FORMAT_ABGR1555
:
1977 case DRM_FORMAT_RGBA5551
:
1978 case DRM_FORMAT_BGRA5551
:
1979 case DRM_FORMAT_RGB565
:
1980 case DRM_FORMAT_BGR565
:
1981 case DRM_FORMAT_RGB888
:
1982 case DRM_FORMAT_BGR888
:
1983 case DRM_FORMAT_XRGB8888
:
1984 case DRM_FORMAT_XBGR8888
:
1985 case DRM_FORMAT_RGBX8888
:
1986 case DRM_FORMAT_BGRX8888
:
1987 case DRM_FORMAT_ARGB8888
:
1988 case DRM_FORMAT_ABGR8888
:
1989 case DRM_FORMAT_RGBA8888
:
1990 case DRM_FORMAT_BGRA8888
:
1991 case DRM_FORMAT_XRGB2101010
:
1992 case DRM_FORMAT_XBGR2101010
:
1993 case DRM_FORMAT_RGBX1010102
:
1994 case DRM_FORMAT_BGRX1010102
:
1995 case DRM_FORMAT_ARGB2101010
:
1996 case DRM_FORMAT_ABGR2101010
:
1997 case DRM_FORMAT_RGBA1010102
:
1998 case DRM_FORMAT_BGRA1010102
:
1999 case DRM_FORMAT_YUYV
:
2000 case DRM_FORMAT_YVYU
:
2001 case DRM_FORMAT_UYVY
:
2002 case DRM_FORMAT_VYUY
:
2005 case DRM_FORMAT_NV12
:
2006 case DRM_FORMAT_NV21
:
2007 case DRM_FORMAT_NV16
:
2008 case DRM_FORMAT_NV61
:
2011 case DRM_FORMAT_YUV410
:
2012 case DRM_FORMAT_YVU410
:
2013 case DRM_FORMAT_YUV411
:
2014 case DRM_FORMAT_YVU411
:
2015 case DRM_FORMAT_YUV420
:
2016 case DRM_FORMAT_YVU420
:
2017 case DRM_FORMAT_YUV422
:
2018 case DRM_FORMAT_YVU422
:
2019 case DRM_FORMAT_YUV444
:
2020 case DRM_FORMAT_YVU444
:
2024 _eglError(EGL_BAD_ATTRIBUTE
, "invalid format");
2031 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
2032 * incomplete, EGL_BAD_PARAMETER is generated."
2034 for (i
= 0; i
< plane_n
; ++i
) {
2035 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
2036 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2037 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2038 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
2044 * The spec also says:
2046 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
2047 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
2048 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
2049 * attributes are specified."
2051 for (i
= plane_n
; i
< 3; ++i
) {
2052 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
2053 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
2054 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
2055 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
2066 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
2067 * EGL will take a reference to the dma_buf(s) which it will release at any
2068 * time while the EGLDisplay is initialized. It is the responsibility of the
2069 * application to close the dma_buf file descriptors."
2071 * Therefore we must never close or otherwise modify the file descriptors.
2074 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
2075 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2077 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2080 _EGLImageAttribs attrs
;
2081 __DRIimage
*dri_image
;
2092 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
2093 * error EGL_BAD_PARAMETER is generated."
2095 if (buffer
!= NULL
) {
2096 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
2100 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2101 if (err
!= EGL_SUCCESS
) {
2102 _eglError(err
, "bad attribute");
2106 if (!dri2_check_dma_buf_attribs(&attrs
))
2109 num_fds
= dri2_check_dma_buf_format(&attrs
);
2113 for (i
= 0; i
< num_fds
; ++i
) {
2114 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2115 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2116 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2120 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2121 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2122 fds
, num_fds
, pitches
, offsets
,
2123 attrs
.DMABufYuvColorSpaceHint
.Value
,
2124 attrs
.DMABufSampleRangeHint
.Value
,
2125 attrs
.DMABufChromaHorizontalSiting
.Value
,
2126 attrs
.DMABufChromaVerticalSiting
.Value
,
2129 dri2_create_image_khr_texture_error(error
);
2132 return EGL_NO_IMAGE_KHR
;
2134 res
= dri2_create_image_from_dri(disp
, dri_image
);
2139 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2140 const EGLint
*attr_list
)
2142 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2143 struct dri2_egl_image
*dri2_img
;
2144 _EGLImageAttribs attrs
;
2145 unsigned int dri_use
, valid_mask
;
2147 EGLint err
= EGL_SUCCESS
;
2151 dri2_img
= malloc(sizeof *dri2_img
);
2153 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2154 return EGL_NO_IMAGE_KHR
;
2158 err
= EGL_BAD_PARAMETER
;
2162 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
2163 err
= EGL_BAD_PARAMETER
;
2167 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2168 if (err
!= EGL_SUCCESS
)
2171 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2172 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)",
2173 attrs
.Width
, attrs
.Height
);
2177 switch (attrs
.DRMBufferFormatMESA
) {
2178 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2179 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2182 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x",
2183 attrs
.DRMBufferFormatMESA
);
2188 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2189 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2190 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2191 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2192 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x",
2193 attrs
.DRMBufferUseMESA
& ~valid_mask
);
2198 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2199 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2200 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2201 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2202 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2203 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2205 dri2_img
->dri_image
=
2206 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2207 attrs
.Width
, attrs
.Height
,
2208 format
, dri_use
, dri2_img
);
2209 if (dri2_img
->dri_image
== NULL
) {
2210 err
= EGL_BAD_ALLOC
;
2214 return &dri2_img
->base
;
2218 _eglError(err
, "dri2_create_drm_image_mesa");
2220 return EGL_NO_IMAGE_KHR
;
2224 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2225 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2227 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2228 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2232 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2233 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
2234 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2239 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2240 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2243 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2244 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2250 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2252 EGLint
*fourcc
, EGLint
*nplanes
,
2253 EGLuint64KHR
*modifiers
)
2255 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2256 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2262 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2263 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2265 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2266 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2275 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2276 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2278 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2279 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2283 /* rework later to provide multiple fds/strides/offsets */
2285 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2286 __DRI_IMAGE_ATTRIB_FD
, fds
);
2289 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2290 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2294 bool ret
= dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2295 __DRI_IMAGE_ATTRIB_OFFSET
, &img_offset
);
2297 offsets
[0] = img_offset
;
2308 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2309 _EGLContext
*ctx
, EGLenum target
,
2310 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2315 case EGL_GL_TEXTURE_2D_KHR
:
2316 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2317 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2318 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2319 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2320 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2321 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2322 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2323 case EGL_GL_TEXTURE_3D_KHR
:
2324 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
2325 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2328 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2329 return EGL_NO_IMAGE_KHR
;
2331 case EGL_GL_RENDERBUFFER_KHR
:
2332 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2334 case EGL_DRM_BUFFER_MESA
:
2335 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2336 case EGL_LINUX_DMA_BUF_EXT
:
2337 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2339 #ifdef HAVE_WAYLAND_PLATFORM
2340 case EGL_WAYLAND_BUFFER_WL
:
2341 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2344 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2345 return EGL_NO_IMAGE_KHR
;
2350 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2352 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2353 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2357 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2363 #ifdef HAVE_WAYLAND_PLATFORM
2366 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2367 struct wl_drm_buffer
*buffer
)
2369 _EGLDisplay
*disp
= user_data
;
2370 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2372 int i
, dri_components
= 0;
2375 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2384 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2396 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2398 buffer
->driver_format
= NULL
;
2399 for (i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2400 if (wl_drm_components
[i
].dri_components
== dri_components
)
2401 buffer
->driver_format
= &wl_drm_components
[i
];
2403 if (buffer
->driver_format
== NULL
)
2404 dri2_dpy
->image
->destroyImage(img
);
2406 buffer
->driver_buffer
= img
;
2410 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2412 _EGLDisplay
*disp
= user_data
;
2413 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2415 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2418 static struct wayland_drm_callbacks wl_drm_callbacks
= {
2419 .authenticate
= NULL
,
2420 .reference_buffer
= dri2_wl_reference_buffer
,
2421 .release_buffer
= dri2_wl_release_buffer
2425 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2426 struct wl_display
*wl_dpy
)
2428 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2434 if (dri2_dpy
->wl_server_drm
)
2437 wl_drm_callbacks
.authenticate
=
2438 (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
;
2440 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2441 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2442 dri2_dpy
->image
->base
.version
>= 7 &&
2443 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2444 flags
|= WAYLAND_DRM_PRIME
;
2446 dri2_dpy
->wl_server_drm
=
2447 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2448 &wl_drm_callbacks
, disp
, flags
);
2450 if (!dri2_dpy
->wl_server_drm
)
2453 #ifdef HAVE_DRM_PLATFORM
2454 /* We have to share the wl_drm instance with gbm, so gbm can convert
2455 * wl_buffers to gbm bos. */
2456 if (dri2_dpy
->gbm_dri
)
2457 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2464 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2465 struct wl_display
*wl_dpy
)
2467 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2471 if (!dri2_dpy
->wl_server_drm
)
2474 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2475 dri2_dpy
->wl_server_drm
= NULL
;
2481 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2482 struct wl_resource
*buffer_resource
,
2483 EGLint attribute
, EGLint
*value
)
2485 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2486 struct wl_drm_buffer
*buffer
;
2487 const struct wl_drm_components_descriptor
*format
;
2489 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2493 format
= buffer
->driver_format
;
2494 switch (attribute
) {
2495 case EGL_TEXTURE_FORMAT
:
2496 *value
= format
->components
;
2499 *value
= buffer
->width
;
2502 *value
= buffer
->height
;
2511 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2513 p_atomic_inc(&sync
->refcount
);
2517 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2518 struct dri2_egl_sync
*dri2_sync
)
2520 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2521 switch (dri2_sync
->base
.Type
) {
2522 case EGL_SYNC_REUSABLE_KHR
:
2523 cnd_destroy(&dri2_sync
->cond
);
2525 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2526 if (dri2_sync
->base
.SyncFd
!= EGL_NO_NATIVE_FENCE_FD_ANDROID
)
2527 close(dri2_sync
->base
.SyncFd
);
2533 if (dri2_sync
->fence
)
2534 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2541 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
2542 EGLenum type
, const EGLAttrib
*attrib_list
)
2544 _EGLContext
*ctx
= _eglGetCurrentContext();
2545 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2546 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2547 struct dri2_egl_sync
*dri2_sync
;
2549 pthread_condattr_t attr
;
2551 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2553 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2557 if (!_eglInitSync(&dri2_sync
->base
, dpy
, type
, attrib_list
)) {
2563 case EGL_SYNC_FENCE_KHR
:
2564 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2565 if (!dri2_sync
->fence
) {
2566 /* Why did it fail? DRI doesn't return an error code, so we emit
2567 * a generic EGL error that doesn't communicate user error.
2569 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2575 case EGL_SYNC_CL_EVENT_KHR
:
2576 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2577 dri2_dpy
->dri_screen
,
2578 dri2_sync
->base
.CLEvent
);
2579 /* this can only happen if the cl_event passed in is invalid. */
2580 if (!dri2_sync
->fence
) {
2581 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2586 /* the initial status must be "signaled" if the cl_event is signaled */
2587 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2588 dri2_sync
->fence
, 0, 0))
2589 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2592 case EGL_SYNC_REUSABLE_KHR
:
2593 /* intialize attr */
2594 ret
= pthread_condattr_init(&attr
);
2597 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2602 /* change clock attribute to CLOCK_MONOTONIC */
2603 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
2606 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2611 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
2614 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2619 /* initial status of reusable sync must be "unsignaled" */
2620 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
2623 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2624 if (dri2_dpy
->fence
->create_fence_fd
) {
2625 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence_fd(
2626 dri2_ctx
->dri_context
,
2627 dri2_sync
->base
.SyncFd
);
2629 if (!dri2_sync
->fence
) {
2630 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2637 p_atomic_set(&dri2_sync
->refcount
, 1);
2638 return &dri2_sync
->base
;
2642 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2644 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2645 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2646 EGLint ret
= EGL_TRUE
;
2649 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
2650 * then unlock all threads possibly blocked by the reusable sync before
2653 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
2654 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2655 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2656 /* unblock all threads currently blocked by sync */
2657 err
= cnd_broadcast(&dri2_sync
->cond
);
2660 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
2665 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2671 dri2_dup_native_fence_fd(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2673 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2674 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2676 assert(sync
->Type
== EGL_SYNC_NATIVE_FENCE_ANDROID
);
2678 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
2679 /* try to retrieve the actual native fence fd.. if rendering is
2680 * not flushed this will just return -1, aka NO_NATIVE_FENCE_FD:
2682 sync
->SyncFd
= dri2_dpy
->fence
->get_fence_fd(dri2_dpy
->dri_screen
,
2686 if (sync
->SyncFd
== EGL_NO_NATIVE_FENCE_FD_ANDROID
) {
2687 /* if native fence fd still not created, return an error: */
2688 _eglError(EGL_BAD_PARAMETER
, "eglDupNativeFenceFDANDROID");
2689 return EGL_NO_NATIVE_FENCE_FD_ANDROID
;
2692 return dup(sync
->SyncFd
);
2696 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2697 EGLint flags
, EGLTime timeout
)
2699 _EGLContext
*ctx
= _eglGetCurrentContext();
2700 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2701 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2702 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2703 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2704 unsigned wait_flags
= 0;
2706 /* timespecs for cnd_timedwait */
2707 struct timespec current
;
2710 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
2712 /* The EGL_KHR_fence_sync spec states:
2714 * "If no context is current for the bound API,
2715 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
2717 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
2718 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
2720 /* the sync object should take a reference while waiting */
2721 dri2_egl_ref_sync(dri2_sync
);
2723 switch (sync
->Type
) {
2724 case EGL_SYNC_FENCE_KHR
:
2725 case EGL_SYNC_NATIVE_FENCE_ANDROID
:
2726 case EGL_SYNC_CL_EVENT_KHR
:
2727 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
2728 dri2_sync
->fence
, wait_flags
,
2730 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2732 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2735 case EGL_SYNC_REUSABLE_KHR
:
2736 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
2737 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
2738 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
2739 dri2_drv
->glFlush();
2742 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
2743 if (timeout
== EGL_FOREVER_KHR
) {
2744 mtx_lock(&dri2_sync
->mutex
);
2745 cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
2746 mtx_unlock(&dri2_sync
->mutex
);
2748 /* if reusable sync has not been yet signaled */
2749 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
2750 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
2752 /* calculating when to expire */
2753 expire
.nsec
= timeout
% 1000000000L;
2754 expire
.sec
= timeout
/ 1000000000L;
2756 expire
.nsec
+= current
.tv_nsec
;
2757 expire
.sec
+= current
.tv_sec
;
2759 /* expire.nsec now is a number between 0 and 1999999998 */
2760 if (expire
.nsec
> 999999999L) {
2762 expire
.nsec
-= 1000000000L;
2765 mtx_lock(&dri2_sync
->mutex
);
2766 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
2767 mtx_unlock(&dri2_sync
->mutex
);
2769 if (ret
== thrd_busy
) {
2770 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2771 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2773 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2781 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2787 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2790 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2793 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
) {
2794 _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
2798 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
) {
2799 _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
2803 dri2_sync
->base
.SyncStatus
= mode
;
2805 if (mode
== EGL_SIGNALED_KHR
) {
2806 ret
= cnd_broadcast(&dri2_sync
->cond
);
2808 /* fail to broadcast */
2810 _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
2819 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2821 _EGLContext
*ctx
= _eglGetCurrentContext();
2822 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2823 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2824 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2826 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
2827 dri2_sync
->fence
, 0);
2832 dri2_interop_query_device_info(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2833 struct mesa_glinterop_device_info
*out
)
2835 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2836 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2838 if (!dri2_dpy
->interop
)
2839 return MESA_GLINTEROP_UNSUPPORTED
;
2841 return dri2_dpy
->interop
->query_device_info(dri2_ctx
->dri_context
, out
);
2845 dri2_interop_export_object(_EGLDisplay
*dpy
, _EGLContext
*ctx
,
2846 struct mesa_glinterop_export_in
*in
,
2847 struct mesa_glinterop_export_out
*out
)
2849 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2850 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2852 if (!dri2_dpy
->interop
)
2853 return MESA_GLINTEROP_UNSUPPORTED
;
2855 return dri2_dpy
->interop
->export_object(dri2_ctx
->dri_context
, in
, out
);
2859 dri2_unload(_EGLDriver
*drv
)
2861 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2863 dlclose(dri2_drv
->handle
);
2868 dri2_load(_EGLDriver
*drv
)
2870 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2871 #ifdef HAVE_ANDROID_PLATFORM
2872 const char *libname
= "libglapi.so";
2873 #elif defined(__APPLE__)
2874 const char *libname
= "libglapi.0.dylib";
2875 #elif defined(__CYGWIN__)
2876 const char *libname
= "cygglapi-0.dll";
2878 const char *libname
= "libglapi.so.0";
2882 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2883 handle
= dlopen(libname
, RTLD_LAZY
| RTLD_GLOBAL
);
2885 _eglLog(_EGL_WARNING
, "DRI2: failed to open glapi provider");
2889 dri2_drv
->get_proc_address
= (_EGLProc (*)(const char *))
2890 dlsym(handle
, "_glapi_get_proc_address");
2892 /* if glapi is not available, loading DRI drivers will fail */
2893 if (!dri2_drv
->get_proc_address
) {
2894 _eglLog(_EGL_WARNING
, "DRI2: failed to find _glapi_get_proc_address");
2898 dri2_drv
->glFlush
= (void (*)(void))
2899 dri2_drv
->get_proc_address("glFlush");
2901 /* if glFlush is not available things are horribly broken */
2902 if (!dri2_drv
->glFlush
) {
2903 _eglLog(_EGL_WARNING
, "DRI2: failed to find glFlush entry point");
2907 dri2_drv
->handle
= handle
;
2917 * This is the main entrypoint into the driver, called by libEGL.
2918 * Create a new _EGLDriver object and init its dispatch table.
2921 _eglBuiltInDriverDRI2(const char *args
)
2923 struct dri2_egl_driver
*dri2_drv
;
2927 dri2_drv
= calloc(1, sizeof *dri2_drv
);
2931 if (!dri2_load(&dri2_drv
->base
)) {
2936 _eglInitDriverFallbacks(&dri2_drv
->base
);
2937 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
2938 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
2939 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
2940 dri2_drv
->base
.API
.DestroyContext
= dri2_destroy_context
;
2941 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
2942 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
2943 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
2944 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
2945 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
2946 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
2947 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
2948 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
2949 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
2950 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
2951 dri2_drv
->base
.API
.SwapInterval
= dri2_swap_interval
;
2952 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
2953 dri2_drv
->base
.API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
2954 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
2955 dri2_drv
->base
.API
.PostSubBufferNV
= dri2_post_sub_buffer
;
2956 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
,
2957 dri2_drv
->base
.API
.QueryBufferAge
= dri2_query_buffer_age
;
2958 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image
;
2959 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
2960 dri2_drv
->base
.API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
2961 dri2_drv
->base
.API
.QuerySurface
= dri2_query_surface
;
2963 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
2964 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
2965 dri2_drv
->base
.API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
2966 dri2_drv
->base
.API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
2968 #ifdef HAVE_WAYLAND_PLATFORM
2969 dri2_drv
->base
.API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
2970 dri2_drv
->base
.API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
2971 dri2_drv
->base
.API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
2973 dri2_drv
->base
.API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
2974 dri2_drv
->base
.API
.CreateSyncKHR
= dri2_create_sync
;
2975 dri2_drv
->base
.API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
2976 dri2_drv
->base
.API
.SignalSyncKHR
= dri2_signal_sync
;
2977 dri2_drv
->base
.API
.WaitSyncKHR
= dri2_server_wait_sync
;
2978 dri2_drv
->base
.API
.DestroySyncKHR
= dri2_destroy_sync
;
2979 dri2_drv
->base
.API
.GLInteropQueryDeviceInfo
= dri2_interop_query_device_info
;
2980 dri2_drv
->base
.API
.GLInteropExportObject
= dri2_interop_export_object
;
2981 dri2_drv
->base
.API
.DupNativeFenceFDANDROID
= dri2_dup_native_fence_fd
;
2983 dri2_drv
->base
.Name
= "DRI2";
2984 dri2_drv
->base
.Unload
= dri2_unload
;
2986 return &dri2_drv
->base
;