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 <sys/types.h>
52 #ifdef HAVE_WAYLAND_PLATFORM
53 #include "wayland-drm.h"
54 #include "wayland-drm-client-protocol.h"
58 #include "util/u_atomic.h"
60 /* The kernel header drm_fourcc.h defines the DRM formats below. We duplicate
61 * some of the definitions here so that building Mesa won't bleeding-edge
65 #define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
68 #ifndef DRM_FORMAT_RG88
69 #define DRM_FORMAT_RG88 fourcc_code('R', 'G', '8', '8') /* [15:0] R:G 8:8 little endian */
72 #ifndef DRM_FORMAT_GR88
73 #define DRM_FORMAT_GR88 fourcc_code('G', 'R', '8', '8') /* [15:0] G:R 8:8 little endian */
76 const __DRIuseInvalidateExtension use_invalidate
= {
77 .base
= { __DRI_USE_INVALIDATE
, 1 }
80 EGLint dri2_to_egl_attribute_map
[] = {
82 EGL_BUFFER_SIZE
, /* __DRI_ATTRIB_BUFFER_SIZE */
83 EGL_LEVEL
, /* __DRI_ATTRIB_LEVEL */
84 EGL_RED_SIZE
, /* __DRI_ATTRIB_RED_SIZE */
85 EGL_GREEN_SIZE
, /* __DRI_ATTRIB_GREEN_SIZE */
86 EGL_BLUE_SIZE
, /* __DRI_ATTRIB_BLUE_SIZE */
87 EGL_LUMINANCE_SIZE
, /* __DRI_ATTRIB_LUMINANCE_SIZE */
88 EGL_ALPHA_SIZE
, /* __DRI_ATTRIB_ALPHA_SIZE */
89 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
90 EGL_DEPTH_SIZE
, /* __DRI_ATTRIB_DEPTH_SIZE */
91 EGL_STENCIL_SIZE
, /* __DRI_ATTRIB_STENCIL_SIZE */
92 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
93 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
94 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
95 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
96 EGL_SAMPLE_BUFFERS
, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
97 EGL_SAMPLES
, /* __DRI_ATTRIB_SAMPLES */
98 0, /* __DRI_ATTRIB_RENDER_TYPE, */
99 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
100 0, /* __DRI_ATTRIB_CONFORMANT */
101 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
102 0, /* __DRI_ATTRIB_STEREO */
103 0, /* __DRI_ATTRIB_AUX_BUFFERS */
104 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
105 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
106 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
107 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
108 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
109 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
110 0, /* __DRI_ATTRIB_FLOAT_MODE (deprecated) */
111 0, /* __DRI_ATTRIB_RED_MASK */
112 0, /* __DRI_ATTRIB_GREEN_MASK */
113 0, /* __DRI_ATTRIB_BLUE_MASK */
114 0, /* __DRI_ATTRIB_ALPHA_MASK */
115 EGL_MAX_PBUFFER_WIDTH
, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
116 EGL_MAX_PBUFFER_HEIGHT
, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
117 EGL_MAX_PBUFFER_PIXELS
, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
118 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
119 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
120 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
121 0, /* __DRI_ATTRIB_SWAP_METHOD */
122 EGL_MAX_SWAP_INTERVAL
, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
123 EGL_MIN_SWAP_INTERVAL
, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
124 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
125 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
126 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
127 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
128 EGL_Y_INVERTED_NOK
, /* __DRI_ATTRIB_YINVERTED */
129 0, /* __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE */
133 dri2_get_dri_config(struct dri2_egl_config
*conf
, EGLint surface_type
,
136 const bool srgb
= colorspace
== EGL_GL_COLORSPACE_SRGB_KHR
;
138 return surface_type
== EGL_WINDOW_BIT
? conf
->dri_double_config
[srgb
] :
139 conf
->dri_single_config
[srgb
];
143 dri2_match_config(const _EGLConfig
*conf
, const _EGLConfig
*criteria
)
145 if (_eglCompareConfigs(conf
, criteria
, NULL
, EGL_FALSE
) != 0)
148 if (!_eglMatchConfig(conf
, criteria
))
154 struct dri2_egl_config
*
155 dri2_add_config(_EGLDisplay
*disp
, const __DRIconfig
*dri_config
, int id
,
156 EGLint surface_type
, const EGLint
*attr_list
,
157 const unsigned int *rgba_masks
)
159 struct dri2_egl_config
*conf
;
160 struct dri2_egl_display
*dri2_dpy
;
162 unsigned int attrib
, value
, double_buffer
;
164 EGLint key
, bind_to_texture_rgb
, bind_to_texture_rgba
;
165 unsigned int dri_masks
[4] = { 0, 0, 0, 0 };
166 _EGLConfig
*matching_config
;
167 EGLint num_configs
= 0;
171 dri2_dpy
= disp
->DriverData
;
172 _eglInitConfig(&base
, disp
, id
);
176 bind_to_texture_rgb
= 0;
177 bind_to_texture_rgba
= 0;
179 while (dri2_dpy
->core
->indexConfigAttrib(dri_config
, i
++, &attrib
, &value
)) {
181 case __DRI_ATTRIB_RENDER_TYPE
:
182 if (value
& __DRI_ATTRIB_RGBA_BIT
)
183 value
= EGL_RGB_BUFFER
;
184 else if (value
& __DRI_ATTRIB_LUMINANCE_BIT
)
185 value
= EGL_LUMINANCE_BUFFER
;
188 _eglSetConfigKey(&base
, EGL_COLOR_BUFFER_TYPE
, value
);
191 case __DRI_ATTRIB_CONFIG_CAVEAT
:
192 if (value
& __DRI_ATTRIB_NON_CONFORMANT_CONFIG
)
193 value
= EGL_NON_CONFORMANT_CONFIG
;
194 else if (value
& __DRI_ATTRIB_SLOW_BIT
)
195 value
= EGL_SLOW_CONFIG
;
198 _eglSetConfigKey(&base
, EGL_CONFIG_CAVEAT
, value
);
201 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB
:
202 bind_to_texture_rgb
= value
;
205 case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA
:
206 bind_to_texture_rgba
= value
;
209 case __DRI_ATTRIB_DOUBLE_BUFFER
:
210 double_buffer
= value
;
213 case __DRI_ATTRIB_RED_MASK
:
214 dri_masks
[0] = value
;
217 case __DRI_ATTRIB_GREEN_MASK
:
218 dri_masks
[1] = value
;
221 case __DRI_ATTRIB_BLUE_MASK
:
222 dri_masks
[2] = value
;
225 case __DRI_ATTRIB_ALPHA_MASK
:
226 dri_masks
[3] = value
;
229 case __DRI_ATTRIB_ACCUM_RED_SIZE
:
230 case __DRI_ATTRIB_ACCUM_GREEN_SIZE
:
231 case __DRI_ATTRIB_ACCUM_BLUE_SIZE
:
232 case __DRI_ATTRIB_ACCUM_ALPHA_SIZE
:
233 /* Don't expose visuals with the accumulation buffer. */
238 case __DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE
:
240 if (!disp
->Extensions
.KHR_gl_colorspace
&& srgb
)
245 key
= dri2_to_egl_attribute_map
[attrib
];
247 _eglSetConfigKey(&base
, key
, value
);
253 for (i
= 0; attr_list
[i
] != EGL_NONE
; i
+= 2)
254 _eglSetConfigKey(&base
, attr_list
[i
], attr_list
[i
+1]);
256 if (rgba_masks
&& memcmp(rgba_masks
, dri_masks
, sizeof(dri_masks
)))
259 base
.NativeRenderable
= EGL_TRUE
;
261 base
.SurfaceType
= surface_type
;
262 if (surface_type
& (EGL_PBUFFER_BIT
|
263 (disp
->Extensions
.NOK_texture_from_pixmap
? EGL_PIXMAP_BIT
: 0))) {
264 base
.BindToTextureRGB
= bind_to_texture_rgb
;
265 if (base
.AlphaSize
> 0)
266 base
.BindToTextureRGBA
= bind_to_texture_rgba
;
269 base
.RenderableType
= disp
->ClientAPIs
;
270 base
.Conformant
= disp
->ClientAPIs
;
272 base
.MinSwapInterval
= dri2_dpy
->min_swap_interval
;
273 base
.MaxSwapInterval
= dri2_dpy
->max_swap_interval
;
275 if (!_eglValidateConfig(&base
, EGL_FALSE
)) {
276 _eglLog(_EGL_DEBUG
, "DRI2: failed to validate config %d", id
);
280 config_id
= base
.ConfigID
;
281 base
.ConfigID
= EGL_DONT_CARE
;
282 base
.SurfaceType
= EGL_DONT_CARE
;
283 num_configs
= _eglFilterArray(disp
->Configs
, (void **) &matching_config
, 1,
284 (_EGLArrayForEach
) dri2_match_config
, &base
);
286 if (num_configs
== 1) {
287 conf
= (struct dri2_egl_config
*) matching_config
;
289 if (double_buffer
&& !conf
->dri_double_config
[srgb
])
290 conf
->dri_double_config
[srgb
] = dri_config
;
291 else if (!double_buffer
&& !conf
->dri_single_config
[srgb
])
292 conf
->dri_single_config
[srgb
] = dri_config
;
294 /* a similar config type is already added (unlikely) => discard */
297 else if (num_configs
== 0) {
298 conf
= calloc(1, sizeof *conf
);
303 conf
->dri_double_config
[srgb
] = dri_config
;
305 conf
->dri_single_config
[srgb
] = dri_config
;
307 memcpy(&conf
->base
, &base
, sizeof base
);
308 conf
->base
.SurfaceType
= 0;
309 conf
->base
.ConfigID
= config_id
;
311 _eglLinkConfig(&conf
->base
);
319 surface_type
&= ~EGL_PIXMAP_BIT
;
322 conf
->base
.SurfaceType
|= surface_type
;
328 dri2_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
330 _EGLDisplay
*disp
= data
;
331 struct dri2_egl_image
*dri2_img
;
336 img
= _eglLookupImage(image
, disp
);
338 _eglError(EGL_BAD_PARAMETER
, "dri2_lookup_egl_image");
342 dri2_img
= dri2_egl_image(image
);
344 return dri2_img
->dri_image
;
347 const __DRIimageLookupExtension image_lookup_extension
= {
348 .base
= { __DRI_IMAGE_LOOKUP
, 1 },
350 .lookupEGLImage
= dri2_lookup_egl_image
353 struct dri2_extension_match
{
359 static struct dri2_extension_match dri3_driver_extensions
[] = {
360 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
361 { __DRI_IMAGE_DRIVER
, 1, offsetof(struct dri2_egl_display
, image_driver
) },
365 static struct dri2_extension_match dri2_driver_extensions
[] = {
366 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
367 { __DRI_DRI2
, 2, offsetof(struct dri2_egl_display
, dri2
) },
371 static struct dri2_extension_match dri2_core_extensions
[] = {
372 { __DRI2_FLUSH
, 1, offsetof(struct dri2_egl_display
, flush
) },
373 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
374 { __DRI_IMAGE
, 1, offsetof(struct dri2_egl_display
, image
) },
378 static struct dri2_extension_match swrast_driver_extensions
[] = {
379 { __DRI_CORE
, 1, offsetof(struct dri2_egl_display
, core
) },
380 { __DRI_SWRAST
, 2, offsetof(struct dri2_egl_display
, swrast
) },
384 static struct dri2_extension_match swrast_core_extensions
[] = {
385 { __DRI_TEX_BUFFER
, 2, offsetof(struct dri2_egl_display
, tex_buffer
) },
390 dri2_bind_extensions(struct dri2_egl_display
*dri2_dpy
,
391 struct dri2_extension_match
*matches
,
392 const __DRIextension
**extensions
)
394 int i
, j
, ret
= EGL_TRUE
;
397 for (i
= 0; extensions
[i
]; i
++) {
398 _eglLog(_EGL_DEBUG
, "found extension `%s'", extensions
[i
]->name
);
399 for (j
= 0; matches
[j
].name
; j
++) {
400 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
401 extensions
[i
]->version
>= matches
[j
].version
) {
402 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
403 *(const __DRIextension
**) field
= extensions
[i
];
404 _eglLog(_EGL_INFO
, "found extension %s version %d",
405 extensions
[i
]->name
, extensions
[i
]->version
);
410 for (j
= 0; matches
[j
].name
; j
++) {
411 field
= ((char *) dri2_dpy
+ matches
[j
].offset
);
412 if (*(const __DRIextension
**) field
== NULL
) {
413 _eglLog(_EGL_WARNING
, "did not find extension %s version %d",
414 matches
[j
].name
, matches
[j
].version
);
422 static const __DRIextension
**
423 dri2_open_driver(_EGLDisplay
*disp
)
425 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
426 const __DRIextension
**extensions
= NULL
;
427 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
428 char *get_extensions_name
;
429 const __DRIextension
**(*get_extensions
)(void);
432 if (geteuid() == getuid()) {
433 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
434 search_paths
= getenv("LIBGL_DRIVERS_PATH");
436 if (search_paths
== NULL
)
437 search_paths
= DEFAULT_DRIVER_DIR
;
439 dri2_dpy
->driver
= NULL
;
440 end
= search_paths
+ strlen(search_paths
);
441 for (p
= search_paths
; p
< end
; p
= next
+ 1) {
443 next
= strchr(p
, ':');
449 snprintf(path
, sizeof path
,
450 "%.*s/tls/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
451 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
453 if (dri2_dpy
->driver
== NULL
) {
454 snprintf(path
, sizeof path
,
455 "%.*s/%s_dri.so", len
, p
, dri2_dpy
->driver_name
);
456 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
457 if (dri2_dpy
->driver
== NULL
)
458 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
460 /* not need continue to loop all paths once the driver is found */
461 if (dri2_dpy
->driver
!= NULL
)
465 snprintf(path
, sizeof path
, "%.*s/gallium_dri.so", len
, p
);
466 dri2_dpy
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
467 if (dri2_dpy
->driver
== NULL
)
468 _eglLog(_EGL_DEBUG
, "failed to open %s: %s\n", path
, dlerror());
474 if (dri2_dpy
->driver
== NULL
) {
475 _eglLog(_EGL_WARNING
,
476 "DRI2: failed to open %s (search paths %s)",
477 dri2_dpy
->driver_name
, search_paths
);
481 _eglLog(_EGL_DEBUG
, "DRI2: dlopen(%s)", path
);
483 if (asprintf(&get_extensions_name
, "%s_%s",
484 __DRI_DRIVER_GET_EXTENSIONS
, dri2_dpy
->driver_name
) != -1) {
485 get_extensions
= dlsym(dri2_dpy
->driver
, get_extensions_name
);
486 if (get_extensions
) {
487 extensions
= get_extensions();
489 _eglLog(_EGL_DEBUG
, "driver does not expose %s(): %s\n",
490 get_extensions_name
, dlerror());
492 free(get_extensions_name
);
496 extensions
= dlsym(dri2_dpy
->driver
, __DRI_DRIVER_EXTENSIONS
);
497 if (extensions
== NULL
) {
498 _eglLog(_EGL_WARNING
,
499 "DRI2: driver exports no extensions (%s)", dlerror());
500 dlclose(dri2_dpy
->driver
);
507 dri2_load_driver_dri3(_EGLDisplay
*disp
)
509 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
510 const __DRIextension
**extensions
;
512 extensions
= dri2_open_driver(disp
);
516 if (!dri2_bind_extensions(dri2_dpy
, dri3_driver_extensions
, extensions
)) {
517 dlclose(dri2_dpy
->driver
);
520 dri2_dpy
->driver_extensions
= extensions
;
526 dri2_load_driver(_EGLDisplay
*disp
)
528 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
529 const __DRIextension
**extensions
;
531 extensions
= dri2_open_driver(disp
);
535 if (!dri2_bind_extensions(dri2_dpy
, dri2_driver_extensions
, extensions
)) {
536 dlclose(dri2_dpy
->driver
);
539 dri2_dpy
->driver_extensions
= extensions
;
545 dri2_load_driver_swrast(_EGLDisplay
*disp
)
547 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
548 const __DRIextension
**extensions
;
550 extensions
= dri2_open_driver(disp
);
554 if (!dri2_bind_extensions(dri2_dpy
, swrast_driver_extensions
, extensions
)) {
555 dlclose(dri2_dpy
->driver
);
558 dri2_dpy
->driver_extensions
= extensions
;
564 dri2_renderer_query_integer(struct dri2_egl_display
*dri2_dpy
, int param
)
566 const __DRI2rendererQueryExtension
*rendererQuery
= dri2_dpy
->rendererQuery
;
567 unsigned int value
= 0;
569 if (!rendererQuery
||
570 rendererQuery
->queryInteger(dri2_dpy
->dri_screen
, param
, &value
) == -1)
577 dri2_setup_screen(_EGLDisplay
*disp
)
579 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
580 unsigned int api_mask
;
582 if (dri2_dpy
->image_driver
) {
583 api_mask
= dri2_dpy
->image_driver
->getAPIMask(dri2_dpy
->dri_screen
);
584 } else if (dri2_dpy
->dri2
) {
585 api_mask
= dri2_dpy
->dri2
->getAPIMask(dri2_dpy
->dri_screen
);
587 assert(dri2_dpy
->swrast
);
588 api_mask
= 1 << __DRI_API_OPENGL
|
589 1 << __DRI_API_GLES
|
590 1 << __DRI_API_GLES2
|
591 1 << __DRI_API_GLES3
;
594 disp
->ClientAPIs
= 0;
595 if (api_mask
& (1 <<__DRI_API_OPENGL
))
596 disp
->ClientAPIs
|= EGL_OPENGL_BIT
;
597 if (api_mask
& (1 <<__DRI_API_GLES
))
598 disp
->ClientAPIs
|= EGL_OPENGL_ES_BIT
;
599 if (api_mask
& (1 << __DRI_API_GLES2
))
600 disp
->ClientAPIs
|= EGL_OPENGL_ES2_BIT
;
601 if (api_mask
& (1 << __DRI_API_GLES3
))
602 disp
->ClientAPIs
|= EGL_OPENGL_ES3_BIT_KHR
;
604 assert(dri2_dpy
->image_driver
|| dri2_dpy
->dri2
|| dri2_dpy
->swrast
);
605 disp
->Extensions
.KHR_surfaceless_context
= EGL_TRUE
;
606 disp
->Extensions
.MESA_configless_context
= EGL_TRUE
;
608 if (dri2_renderer_query_integer(dri2_dpy
,
609 __DRI2_RENDERER_HAS_FRAMEBUFFER_SRGB
))
610 disp
->Extensions
.KHR_gl_colorspace
= EGL_TRUE
;
612 if (dri2_dpy
->image_driver
||
613 (dri2_dpy
->dri2
&& dri2_dpy
->dri2
->base
.version
>= 3) ||
614 (dri2_dpy
->swrast
&& dri2_dpy
->swrast
->base
.version
>= 3)) {
615 disp
->Extensions
.KHR_create_context
= EGL_TRUE
;
617 if (dri2_dpy
->robustness
)
618 disp
->Extensions
.EXT_create_context_robustness
= EGL_TRUE
;
621 if (dri2_dpy
->fence
) {
622 disp
->Extensions
.KHR_fence_sync
= EGL_TRUE
;
623 disp
->Extensions
.KHR_wait_sync
= EGL_TRUE
;
624 if (dri2_dpy
->fence
->get_fence_from_cl_event
)
625 disp
->Extensions
.KHR_cl_event2
= EGL_TRUE
;
628 disp
->Extensions
.KHR_reusable_sync
= EGL_TRUE
;
630 if (dri2_dpy
->image
) {
631 if (dri2_dpy
->image
->base
.version
>= 10 &&
632 dri2_dpy
->image
->getCapabilities
!= NULL
) {
635 capabilities
= dri2_dpy
->image
->getCapabilities(dri2_dpy
->dri_screen
);
636 disp
->Extensions
.MESA_drm_image
= (capabilities
& __DRI_IMAGE_CAP_GLOBAL_NAMES
) != 0;
638 if (dri2_dpy
->image
->base
.version
>= 11)
639 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
641 disp
->Extensions
.MESA_drm_image
= EGL_TRUE
;
642 if (dri2_dpy
->image
->base
.version
>= 11)
643 disp
->Extensions
.MESA_image_dma_buf_export
= EGL_TRUE
;
646 disp
->Extensions
.KHR_image_base
= EGL_TRUE
;
647 disp
->Extensions
.KHR_gl_renderbuffer_image
= EGL_TRUE
;
648 if (dri2_dpy
->image
->base
.version
>= 5 &&
649 dri2_dpy
->image
->createImageFromTexture
) {
650 disp
->Extensions
.KHR_gl_texture_2D_image
= EGL_TRUE
;
651 disp
->Extensions
.KHR_gl_texture_cubemap_image
= EGL_TRUE
;
653 if (dri2_renderer_query_integer(dri2_dpy
,
654 __DRI2_RENDERER_HAS_TEXTURE_3D
))
655 disp
->Extensions
.KHR_gl_texture_3D_image
= EGL_TRUE
;
657 if (dri2_dpy
->image
->base
.version
>= 8 &&
658 dri2_dpy
->image
->createImageFromDmaBufs
) {
659 disp
->Extensions
.EXT_image_dma_buf_import
= EGL_TRUE
;
665 /* All platforms but DRM call this function to create the screen, query the
666 * dri extensions, setup the vtables and populate the driver_configs.
667 * DRM inherits all that information from its display - GBM.
670 dri2_create_screen(_EGLDisplay
*disp
)
672 const __DRIextension
**extensions
;
673 struct dri2_egl_display
*dri2_dpy
;
676 dri2_dpy
= disp
->DriverData
;
678 if (dri2_dpy
->image_driver
) {
679 dri2_dpy
->dri_screen
=
680 dri2_dpy
->image_driver
->createNewScreen2(0, dri2_dpy
->fd
,
681 dri2_dpy
->extensions
,
682 dri2_dpy
->driver_extensions
,
683 &dri2_dpy
->driver_configs
,
685 } else if (dri2_dpy
->dri2
) {
686 if (dri2_dpy
->dri2
->base
.version
>= 4) {
687 dri2_dpy
->dri_screen
=
688 dri2_dpy
->dri2
->createNewScreen2(0, dri2_dpy
->fd
,
689 dri2_dpy
->extensions
,
690 dri2_dpy
->driver_extensions
,
691 &dri2_dpy
->driver_configs
, disp
);
693 dri2_dpy
->dri_screen
=
694 dri2_dpy
->dri2
->createNewScreen(0, dri2_dpy
->fd
,
695 dri2_dpy
->extensions
,
696 &dri2_dpy
->driver_configs
, disp
);
699 assert(dri2_dpy
->swrast
);
700 if (dri2_dpy
->swrast
->base
.version
>= 4) {
701 dri2_dpy
->dri_screen
=
702 dri2_dpy
->swrast
->createNewScreen2(0, dri2_dpy
->extensions
,
703 dri2_dpy
->driver_extensions
,
704 &dri2_dpy
->driver_configs
, disp
);
706 dri2_dpy
->dri_screen
=
707 dri2_dpy
->swrast
->createNewScreen(0, dri2_dpy
->extensions
,
708 &dri2_dpy
->driver_configs
, disp
);
712 if (dri2_dpy
->dri_screen
== NULL
) {
713 _eglLog(_EGL_WARNING
, "DRI2: failed to create dri screen");
717 dri2_dpy
->own_dri_screen
= 1;
719 extensions
= dri2_dpy
->core
->getExtensions(dri2_dpy
->dri_screen
);
721 if (dri2_dpy
->image_driver
|| dri2_dpy
->dri2
) {
722 if (!dri2_bind_extensions(dri2_dpy
, dri2_core_extensions
, extensions
))
723 goto cleanup_dri_screen
;
725 assert(dri2_dpy
->swrast
);
726 if (!dri2_bind_extensions(dri2_dpy
, swrast_core_extensions
, extensions
))
727 goto cleanup_dri_screen
;
730 for (i
= 0; extensions
[i
]; i
++) {
731 if (strcmp(extensions
[i
]->name
, __DRI2_ROBUSTNESS
) == 0) {
732 dri2_dpy
->robustness
= (__DRIrobustnessExtension
*) extensions
[i
];
734 if (strcmp(extensions
[i
]->name
, __DRI2_CONFIG_QUERY
) == 0) {
735 dri2_dpy
->config
= (__DRI2configQueryExtension
*) extensions
[i
];
737 if (strcmp(extensions
[i
]->name
, __DRI2_FENCE
) == 0) {
738 dri2_dpy
->fence
= (__DRI2fenceExtension
*) extensions
[i
];
740 if (strcmp(extensions
[i
]->name
, __DRI2_RENDERER_QUERY
) == 0) {
741 dri2_dpy
->rendererQuery
= (__DRI2rendererQueryExtension
*) extensions
[i
];
745 dri2_setup_screen(disp
);
750 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
756 * Called via eglInitialize(), GLX_drv->API.Initialize().
759 dri2_initialize(_EGLDriver
*drv
, _EGLDisplay
*disp
)
761 /* not until swrast_dri is supported */
762 if (disp
->Options
.UseFallback
)
765 switch (disp
->Platform
) {
766 #ifdef HAVE_SURFACELESS_PLATFORM
767 case _EGL_PLATFORM_SURFACELESS
:
768 if (disp
->Options
.TestOnly
)
770 return dri2_initialize_surfaceless(drv
, disp
);
773 #ifdef HAVE_X11_PLATFORM
774 case _EGL_PLATFORM_X11
:
775 if (disp
->Options
.TestOnly
)
777 return dri2_initialize_x11(drv
, disp
);
780 #ifdef HAVE_DRM_PLATFORM
781 case _EGL_PLATFORM_DRM
:
782 if (disp
->Options
.TestOnly
)
784 return dri2_initialize_drm(drv
, disp
);
786 #ifdef HAVE_WAYLAND_PLATFORM
787 case _EGL_PLATFORM_WAYLAND
:
788 if (disp
->Options
.TestOnly
)
790 return dri2_initialize_wayland(drv
, disp
);
792 #ifdef HAVE_ANDROID_PLATFORM
793 case _EGL_PLATFORM_ANDROID
:
794 if (disp
->Options
.TestOnly
)
796 return dri2_initialize_android(drv
, disp
);
800 _eglLog(_EGL_WARNING
, "No EGL platform enabled.");
806 * Called via eglTerminate(), drv->API.Terminate().
809 dri2_terminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
811 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
814 _eglReleaseDisplayResources(drv
, disp
);
815 _eglCleanupDisplay(disp
);
817 if (dri2_dpy
->own_dri_screen
)
818 dri2_dpy
->core
->destroyScreen(dri2_dpy
->dri_screen
);
819 if (dri2_dpy
->fd
>= 0)
821 if (dri2_dpy
->driver
)
822 dlclose(dri2_dpy
->driver
);
823 free(dri2_dpy
->device_name
);
824 free(dri2_dpy
->driver_name
);
826 switch (disp
->Platform
) {
827 #ifdef HAVE_X11_PLATFORM
828 case _EGL_PLATFORM_X11
:
829 if (dri2_dpy
->own_device
) {
830 xcb_disconnect(dri2_dpy
->conn
);
834 #ifdef HAVE_DRM_PLATFORM
835 case _EGL_PLATFORM_DRM
:
836 if (dri2_dpy
->own_device
) {
837 gbm_device_destroy(&dri2_dpy
->gbm_dri
->base
.base
);
841 #ifdef HAVE_WAYLAND_PLATFORM
842 case _EGL_PLATFORM_WAYLAND
:
843 if (dri2_dpy
->wl_drm
)
844 wl_drm_destroy(dri2_dpy
->wl_drm
);
845 if (dri2_dpy
->wl_shm
)
846 wl_shm_destroy(dri2_dpy
->wl_shm
);
847 wl_registry_destroy(dri2_dpy
->wl_registry
);
848 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
849 if (dri2_dpy
->own_device
) {
850 wl_display_disconnect(dri2_dpy
->wl_dpy
);
858 /* The drm platform does not create the screen/driver_configs but reuses
859 * the ones from the gbm device. As such the gbm itself is responsible
862 if (disp
->Platform
!= _EGL_PLATFORM_DRM
) {
863 for (i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
864 free((__DRIconfig
*) dri2_dpy
->driver_configs
[i
]);
865 free(dri2_dpy
->driver_configs
);
868 disp
->DriverData
= NULL
;
874 * Set the error code after a call to
875 * dri2_egl_display::dri2::createContextAttribs.
878 dri2_create_context_attribs_error(int dri_error
)
883 case __DRI_CTX_ERROR_SUCCESS
:
886 case __DRI_CTX_ERROR_NO_MEMORY
:
887 egl_error
= EGL_BAD_ALLOC
;
890 /* From the EGL_KHR_create_context spec, section "Errors":
892 * * If <config> does not support a client API context compatible
893 * with the requested API major and minor version, [...] context flags,
894 * and context reset notification behavior (for client API types where
895 * these attributes are supported), then an EGL_BAD_MATCH error is
898 * * If an OpenGL ES context is requested and the values for
899 * attributes EGL_CONTEXT_MAJOR_VERSION_KHR and
900 * EGL_CONTEXT_MINOR_VERSION_KHR specify an OpenGL ES version that
901 * is not defined, than an EGL_BAD_MATCH error is generated.
903 * * If an OpenGL context is requested, the requested version is
904 * greater than 3.2, and the value for attribute
905 * EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR has no bits set; has any
906 * bits set other than EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR and
907 * EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR; has more than
908 * one of these bits set; or if the implementation does not support
909 * the requested profile, then an EGL_BAD_MATCH error is generated.
911 case __DRI_CTX_ERROR_BAD_API
:
912 case __DRI_CTX_ERROR_BAD_VERSION
:
913 case __DRI_CTX_ERROR_BAD_FLAG
:
914 egl_error
= EGL_BAD_MATCH
;
917 /* From the EGL_KHR_create_context spec, section "Errors":
919 * * If an attribute name or attribute value in <attrib_list> is not
920 * recognized (including unrecognized bits in bitmask attributes),
921 * then an EGL_BAD_ATTRIBUTE error is generated."
923 case __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE
:
924 case __DRI_CTX_ERROR_UNKNOWN_FLAG
:
925 egl_error
= EGL_BAD_ATTRIBUTE
;
930 egl_error
= EGL_BAD_MATCH
;
934 _eglError(egl_error
, "dri2_create_context");
938 dri2_fill_context_attribs(struct dri2_egl_context
*dri2_ctx
,
939 struct dri2_egl_display
*dri2_dpy
,
940 uint32_t *ctx_attribs
,
941 unsigned *num_attribs
)
945 assert(*num_attribs
>= 8);
947 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MAJOR_VERSION
;
948 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMajorVersion
;
949 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_MINOR_VERSION
;
950 ctx_attribs
[pos
++] = dri2_ctx
->base
.ClientMinorVersion
;
952 if (dri2_ctx
->base
.Flags
!= 0) {
953 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
954 * extension, don't even try to send it the robust-access flag.
955 * It may explode. Instead, generate the required EGL error here.
957 if ((dri2_ctx
->base
.Flags
& EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
) != 0
958 && !dri2_dpy
->robustness
) {
959 _eglError(EGL_BAD_MATCH
, "eglCreateContext");
963 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_FLAGS
;
964 ctx_attribs
[pos
++] = dri2_ctx
->base
.Flags
;
967 if (dri2_ctx
->base
.ResetNotificationStrategy
!= EGL_NO_RESET_NOTIFICATION_KHR
) {
968 /* If the implementation doesn't support the __DRI2_ROBUSTNESS
969 * extension, don't even try to send it a reset strategy. It may
970 * explode. Instead, generate the required EGL error here.
972 if (!dri2_dpy
->robustness
) {
973 _eglError(EGL_BAD_CONFIG
, "eglCreateContext");
977 ctx_attribs
[pos
++] = __DRI_CTX_ATTRIB_RESET_STRATEGY
;
978 ctx_attribs
[pos
++] = __DRI_CTX_RESET_LOSE_CONTEXT
;
987 * Called via eglCreateContext(), drv->API.CreateContext().
990 dri2_create_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
991 _EGLContext
*share_list
, const EGLint
*attrib_list
)
993 struct dri2_egl_context
*dri2_ctx
;
994 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
995 struct dri2_egl_context
*dri2_ctx_shared
= dri2_egl_context(share_list
);
996 __DRIcontext
*shared
=
997 dri2_ctx_shared
? dri2_ctx_shared
->dri_context
: NULL
;
998 struct dri2_egl_config
*dri2_config
= dri2_egl_config(conf
);
999 const __DRIconfig
*dri_config
;
1004 dri2_ctx
= malloc(sizeof *dri2_ctx
);
1006 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
1010 if (!_eglInitContext(&dri2_ctx
->base
, disp
, conf
, attrib_list
))
1013 switch (dri2_ctx
->base
.ClientAPI
) {
1014 case EGL_OPENGL_ES_API
:
1015 switch (dri2_ctx
->base
.ClientMajorVersion
) {
1017 api
= __DRI_API_GLES
;
1020 api
= __DRI_API_GLES2
;
1023 api
= __DRI_API_GLES3
;
1026 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1031 case EGL_OPENGL_API
:
1032 if ((dri2_ctx
->base
.ClientMajorVersion
>= 4
1033 || (dri2_ctx
->base
.ClientMajorVersion
== 3
1034 && dri2_ctx
->base
.ClientMinorVersion
>= 2))
1035 && dri2_ctx
->base
.Profile
== EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
)
1036 api
= __DRI_API_OPENGL_CORE
;
1038 api
= __DRI_API_OPENGL
;
1041 _eglError(EGL_BAD_PARAMETER
, "eglCreateContext");
1047 /* The config chosen here isn't necessarily
1048 * used for surfaces later.
1049 * A pixmap surface will use the single config.
1050 * This opportunity depends on disabling the
1051 * doubleBufferMode check in
1052 * src/mesa/main/context.c:check_compatible()
1054 if (dri2_config
->dri_double_config
[0])
1055 dri_config
= dri2_config
->dri_double_config
[0];
1057 dri_config
= dri2_config
->dri_single_config
[0];
1059 /* EGL_WINDOW_BIT is set only when there is a dri_double_config. This
1060 * makes sure the back buffer will always be used.
1062 if (conf
->SurfaceType
& EGL_WINDOW_BIT
)
1063 dri2_ctx
->base
.WindowRenderBuffer
= EGL_BACK_BUFFER
;
1068 if (dri2_dpy
->image_driver
) {
1070 unsigned num_attribs
= 8;
1071 uint32_t ctx_attribs
[8];
1073 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1077 dri2_ctx
->dri_context
=
1078 dri2_dpy
->image_driver
->createContextAttribs(dri2_dpy
->dri_screen
,
1086 dri2_create_context_attribs_error(error
);
1087 } else if (dri2_dpy
->dri2
) {
1088 if (dri2_dpy
->dri2
->base
.version
>= 3) {
1090 unsigned num_attribs
= 8;
1091 uint32_t ctx_attribs
[8];
1093 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1097 dri2_ctx
->dri_context
=
1098 dri2_dpy
->dri2
->createContextAttribs(dri2_dpy
->dri_screen
,
1106 dri2_create_context_attribs_error(error
);
1108 dri2_ctx
->dri_context
=
1109 dri2_dpy
->dri2
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1116 assert(dri2_dpy
->swrast
);
1117 if (dri2_dpy
->swrast
->base
.version
>= 3) {
1119 unsigned num_attribs
= 8;
1120 uint32_t ctx_attribs
[8];
1122 if (!dri2_fill_context_attribs(dri2_ctx
, dri2_dpy
, ctx_attribs
,
1126 dri2_ctx
->dri_context
=
1127 dri2_dpy
->swrast
->createContextAttribs(dri2_dpy
->dri_screen
,
1135 dri2_create_context_attribs_error(error
);
1137 dri2_ctx
->dri_context
=
1138 dri2_dpy
->swrast
->createNewContextForAPI(dri2_dpy
->dri_screen
,
1146 if (!dri2_ctx
->dri_context
)
1149 return &dri2_ctx
->base
;
1157 * Called via eglDestroyContext(), drv->API.DestroyContext().
1160 dri2_destroy_context(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1162 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1163 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1165 if (_eglPutContext(ctx
)) {
1166 dri2_dpy
->core
->destroyContext(dri2_ctx
->dri_context
);
1174 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
1177 dri2_make_current(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
1178 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
1180 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1181 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1182 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1183 _EGLContext
*old_ctx
;
1184 _EGLSurface
*old_dsurf
, *old_rsurf
;
1185 _EGLSurface
*tmp_dsurf
, *tmp_rsurf
;
1186 __DRIdrawable
*ddraw
, *rdraw
;
1189 /* make new bindings */
1190 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
))
1193 /* flush before context switch */
1194 if (old_ctx
&& dri2_drv
->glFlush
)
1195 dri2_drv
->glFlush();
1197 ddraw
= (dsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(dsurf
) : NULL
;
1198 rdraw
= (rsurf
) ? dri2_dpy
->vtbl
->get_dri_drawable(rsurf
) : NULL
;
1199 cctx
= (dri2_ctx
) ? dri2_ctx
->dri_context
: NULL
;
1202 __DRIcontext
*old_cctx
= dri2_egl_context(old_ctx
)->dri_context
;
1203 dri2_dpy
->core
->unbindContext(old_cctx
);
1206 if ((cctx
== NULL
&& ddraw
== NULL
&& rdraw
== NULL
) ||
1207 dri2_dpy
->core
->bindContext(cctx
, ddraw
, rdraw
)) {
1209 drv
->API
.DestroySurface(drv
, disp
, old_dsurf
);
1211 drv
->API
.DestroySurface(drv
, disp
, old_rsurf
);
1213 drv
->API
.DestroyContext(drv
, disp
, old_ctx
);
1217 /* undo the previous _eglBindContext */
1218 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &tmp_dsurf
, &tmp_rsurf
);
1219 assert(&dri2_ctx
->base
== ctx
&&
1220 tmp_dsurf
== dsurf
&&
1221 tmp_rsurf
== rsurf
);
1223 _eglPutSurface(dsurf
);
1224 _eglPutSurface(rsurf
);
1225 _eglPutContext(ctx
);
1227 _eglPutSurface(old_dsurf
);
1228 _eglPutSurface(old_rsurf
);
1229 _eglPutContext(old_ctx
);
1236 dri2_surface_get_dri_drawable(_EGLSurface
*surf
)
1238 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
1240 return dri2_surf
->dri_drawable
;
1244 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
1247 dri2_get_proc_address(_EGLDriver
*drv
, const char *procname
)
1249 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
1251 return dri2_drv
->get_proc_address(procname
);
1255 dri2_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1256 _EGLConfig
*conf
, void *native_window
,
1257 const EGLint
*attrib_list
)
1259 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1260 return dri2_dpy
->vtbl
->create_window_surface(drv
, dpy
, conf
, native_window
,
1265 dri2_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1266 _EGLConfig
*conf
, void *native_pixmap
,
1267 const EGLint
*attrib_list
)
1269 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1270 return dri2_dpy
->vtbl
->create_pixmap_surface(drv
, dpy
, conf
, native_pixmap
,
1275 dri2_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1276 _EGLConfig
*conf
, const EGLint
*attrib_list
)
1278 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1279 return dri2_dpy
->vtbl
->create_pbuffer_surface(drv
, dpy
, conf
, attrib_list
);
1283 dri2_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1285 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1286 return dri2_dpy
->vtbl
->destroy_surface(drv
, dpy
, surf
);
1290 dri2_swap_interval(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1293 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1294 return dri2_dpy
->vtbl
->swap_interval(drv
, dpy
, surf
, interval
);
1298 * Asks the client API to flush any rendering to the drawable so that we can
1299 * do our swapbuffers.
1302 dri2_flush_drawable_for_swapbuffers(_EGLDisplay
*disp
, _EGLSurface
*draw
)
1304 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1305 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(draw
);
1307 if (dri2_dpy
->flush
) {
1308 if (dri2_dpy
->flush
->base
.version
>= 4) {
1309 /* We know there's a current context because:
1311 * "If surface is not bound to the calling thread’s current
1312 * context, an EGL_BAD_SURFACE error is generated."
1314 _EGLContext
*ctx
= _eglGetCurrentContext();
1315 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1317 /* From the EGL 1.4 spec (page 52):
1319 * "The contents of ancillary buffers are always undefined
1320 * after calling eglSwapBuffers."
1322 dri2_dpy
->flush
->flush_with_flags(dri2_ctx
->dri_context
,
1324 __DRI2_FLUSH_DRAWABLE
|
1325 __DRI2_FLUSH_INVALIDATE_ANCILLARY
,
1326 __DRI2_THROTTLE_SWAPBUFFER
);
1328 dri2_dpy
->flush
->flush(dri_drawable
);
1334 dri2_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1336 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1337 return dri2_dpy
->vtbl
->swap_buffers(drv
, dpy
, surf
);
1341 dri2_swap_buffers_with_damage(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1343 const EGLint
*rects
, EGLint n_rects
)
1345 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1346 return dri2_dpy
->vtbl
->swap_buffers_with_damage(drv
, dpy
, surf
,
1351 dri2_swap_buffers_region(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1352 EGLint numRects
, const EGLint
*rects
)
1354 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1355 return dri2_dpy
->vtbl
->swap_buffers_region(drv
, dpy
, surf
, numRects
, rects
);
1359 dri2_post_sub_buffer(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1360 EGLint x
, EGLint y
, EGLint width
, EGLint height
)
1362 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1363 return dri2_dpy
->vtbl
->post_sub_buffer(drv
, dpy
, surf
, x
, y
, width
, height
);
1367 dri2_copy_buffers(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
,
1368 void *native_pixmap_target
)
1370 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1371 return dri2_dpy
->vtbl
->copy_buffers(drv
, dpy
, surf
, native_pixmap_target
);
1375 dri2_query_buffer_age(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSurface
*surf
)
1377 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1378 return dri2_dpy
->vtbl
->query_buffer_age(drv
, dpy
, surf
);
1382 dri2_wait_client(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLContext
*ctx
)
1384 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1385 _EGLSurface
*surf
= ctx
->DrawSurface
;
1386 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1390 /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
1391 * we need to copy fake to real here.*/
1393 if (dri2_dpy
->flush
!= NULL
)
1394 dri2_dpy
->flush
->flush(dri_drawable
);
1400 dri2_wait_native(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint engine
)
1405 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1406 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1413 dri2_bind_tex_image(_EGLDriver
*drv
,
1414 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1416 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1417 struct dri2_egl_context
*dri2_ctx
;
1419 GLint format
, target
;
1420 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1422 ctx
= _eglGetCurrentContext();
1423 dri2_ctx
= dri2_egl_context(ctx
);
1425 if (!_eglBindTexImage(drv
, disp
, surf
, buffer
))
1428 switch (surf
->TextureFormat
) {
1429 case EGL_TEXTURE_RGB
:
1430 format
= __DRI_TEXTURE_FORMAT_RGB
;
1432 case EGL_TEXTURE_RGBA
:
1433 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1436 assert(!"Unexpected texture format in dri2_bind_tex_image()");
1437 format
= __DRI_TEXTURE_FORMAT_RGBA
;
1440 switch (surf
->TextureTarget
) {
1441 case EGL_TEXTURE_2D
:
1442 target
= GL_TEXTURE_2D
;
1445 target
= GL_TEXTURE_2D
;
1446 assert(!"Unexpected texture target in dri2_bind_tex_image()");
1449 (*dri2_dpy
->tex_buffer
->setTexBuffer2
)(dri2_ctx
->dri_context
,
1457 dri2_release_tex_image(_EGLDriver
*drv
,
1458 _EGLDisplay
*disp
, _EGLSurface
*surf
, EGLint buffer
)
1460 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1461 struct dri2_egl_context
*dri2_ctx
;
1464 __DRIdrawable
*dri_drawable
= dri2_dpy
->vtbl
->get_dri_drawable(surf
);
1466 ctx
= _eglGetCurrentContext();
1467 dri2_ctx
= dri2_egl_context(ctx
);
1469 if (!_eglReleaseTexImage(drv
, disp
, surf
, buffer
))
1472 switch (surf
->TextureTarget
) {
1473 case EGL_TEXTURE_2D
:
1474 target
= GL_TEXTURE_2D
;
1480 if (dri2_dpy
->tex_buffer
->base
.version
>= 3 &&
1481 dri2_dpy
->tex_buffer
->releaseTexBuffer
!= NULL
) {
1482 (*dri2_dpy
->tex_buffer
->releaseTexBuffer
)(dri2_ctx
->dri_context
,
1491 dri2_create_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
,
1492 EGLenum target
, EGLClientBuffer buffer
,
1493 const EGLint
*attr_list
)
1495 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1496 return dri2_dpy
->vtbl
->create_image(drv
, dpy
, ctx
, target
, buffer
,
1501 dri2_create_image_from_dri(_EGLDisplay
*disp
, __DRIimage
*dri_image
)
1503 struct dri2_egl_image
*dri2_img
;
1505 if (dri_image
== NULL
) {
1506 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1510 dri2_img
= malloc(sizeof *dri2_img
);
1512 _eglError(EGL_BAD_ALLOC
, "dri2_create_image");
1516 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1521 dri2_img
->dri_image
= dri_image
;
1523 return &dri2_img
->base
;
1527 dri2_create_image_khr_renderbuffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1528 EGLClientBuffer buffer
,
1529 const EGLint
*attr_list
)
1531 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1532 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1533 GLuint renderbuffer
= (GLuint
) (uintptr_t) buffer
;
1534 __DRIimage
*dri_image
;
1536 if (renderbuffer
== 0) {
1537 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1538 return EGL_NO_IMAGE_KHR
;
1542 dri2_dpy
->image
->createImageFromRenderbuffer(dri2_ctx
->dri_context
,
1543 renderbuffer
, NULL
);
1545 return dri2_create_image_from_dri(disp
, dri_image
);
1548 #ifdef HAVE_WAYLAND_PLATFORM
1550 /* This structure describes how a wl_buffer maps to one or more
1551 * __DRIimages. A wl_drm_buffer stores the wl_drm format code and the
1552 * offsets and strides of the planes in the buffer. This table maps a
1553 * wl_drm format code to a description of the planes in the buffer
1554 * that lets us create a __DRIimage for each of the planes. */
1556 static const struct wl_drm_components_descriptor
{
1557 uint32_t dri_components
;
1560 } wl_drm_components
[] = {
1561 { __DRI_IMAGE_COMPONENTS_RGB
, EGL_TEXTURE_RGB
, 1 },
1562 { __DRI_IMAGE_COMPONENTS_RGBA
, EGL_TEXTURE_RGBA
, 1 },
1563 { __DRI_IMAGE_COMPONENTS_Y_U_V
, EGL_TEXTURE_Y_U_V_WL
, 3 },
1564 { __DRI_IMAGE_COMPONENTS_Y_UV
, EGL_TEXTURE_Y_UV_WL
, 2 },
1565 { __DRI_IMAGE_COMPONENTS_Y_XUXV
, EGL_TEXTURE_Y_XUXV_WL
, 2 },
1569 dri2_create_image_wayland_wl_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1570 EGLClientBuffer _buffer
,
1571 const EGLint
*attr_list
)
1573 struct wl_drm_buffer
*buffer
;
1574 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1575 const struct wl_drm_components_descriptor
*f
;
1576 __DRIimage
*dri_image
;
1577 _EGLImageAttribs attrs
;
1581 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
,
1582 (struct wl_resource
*) _buffer
);
1586 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1587 plane
= attrs
.PlaneWL
;
1588 if (err
!= EGL_SUCCESS
) {
1589 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1593 f
= buffer
->driver_format
;
1594 if (plane
< 0 || plane
>= f
->nplanes
) {
1595 _eglError(EGL_BAD_PARAMETER
,
1596 "dri2_create_image_wayland_wl_buffer (plane out of bounds)");
1600 dri_image
= dri2_dpy
->image
->fromPlanar(buffer
->driver_buffer
, plane
, NULL
);
1602 if (dri_image
== NULL
) {
1603 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_wayland_wl_buffer");
1607 return dri2_create_image_from_dri(disp
, dri_image
);
1612 dri2_get_sync_values_chromium(_EGLDisplay
*dpy
, _EGLSurface
*surf
,
1613 EGLuint64KHR
*ust
, EGLuint64KHR
*msc
,
1616 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1617 return dri2_dpy
->vtbl
->get_sync_values(dpy
, surf
, ust
, msc
, sbc
);
1621 * Set the error code after a call to
1622 * dri2_egl_image::dri_image::createImageFromTexture.
1625 dri2_create_image_khr_texture_error(int dri_error
)
1629 switch (dri_error
) {
1630 case __DRI_IMAGE_ERROR_SUCCESS
:
1633 case __DRI_IMAGE_ERROR_BAD_ALLOC
:
1634 egl_error
= EGL_BAD_ALLOC
;
1637 case __DRI_IMAGE_ERROR_BAD_MATCH
:
1638 egl_error
= EGL_BAD_MATCH
;
1641 case __DRI_IMAGE_ERROR_BAD_PARAMETER
:
1642 egl_error
= EGL_BAD_PARAMETER
;
1645 case __DRI_IMAGE_ERROR_BAD_ACCESS
:
1646 egl_error
= EGL_BAD_ACCESS
;
1651 egl_error
= EGL_BAD_MATCH
;
1655 _eglError(egl_error
, "dri2_create_image_khr_texture");
1659 dri2_create_image_khr_texture(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1661 EGLClientBuffer buffer
,
1662 const EGLint
*attr_list
)
1664 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1665 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1666 struct dri2_egl_image
*dri2_img
;
1667 GLuint texture
= (GLuint
) (uintptr_t) buffer
;
1668 _EGLImageAttribs attrs
;
1674 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1675 return EGL_NO_IMAGE_KHR
;
1678 if (_eglParseImageAttribList(&attrs
, disp
, attr_list
) != EGL_SUCCESS
)
1679 return EGL_NO_IMAGE_KHR
;
1682 case EGL_GL_TEXTURE_2D_KHR
:
1684 gl_target
= GL_TEXTURE_2D
;
1686 case EGL_GL_TEXTURE_3D_KHR
:
1687 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
1688 depth
= attrs
.GLTextureZOffset
;
1689 gl_target
= GL_TEXTURE_3D
;
1693 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1694 return EGL_NO_IMAGE_KHR
;
1696 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
1697 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
1698 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
1699 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
1700 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
1701 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
1702 depth
= target
- EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
;
1703 gl_target
= GL_TEXTURE_CUBE_MAP
;
1706 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
1707 return EGL_NO_IMAGE_KHR
;
1710 dri2_img
= malloc(sizeof *dri2_img
);
1712 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1713 return EGL_NO_IMAGE_KHR
;
1716 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
1717 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
1719 return EGL_NO_IMAGE_KHR
;
1722 dri2_img
->dri_image
=
1723 dri2_dpy
->image
->createImageFromTexture(dri2_ctx
->dri_context
,
1727 attrs
.GLTextureLevel
,
1730 dri2_create_image_khr_texture_error(error
);
1732 if (!dri2_img
->dri_image
) {
1734 return EGL_NO_IMAGE_KHR
;
1736 return &dri2_img
->base
;
1739 static struct wl_buffer
*
1740 dri2_create_wayland_buffer_from_image(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
1743 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
1744 return dri2_dpy
->vtbl
->create_wayland_buffer_from_image(drv
, dpy
, img
);
1749 dri2_create_image_mesa_drm_buffer(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1750 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1752 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1753 EGLint format
, name
, pitch
, err
;
1754 _EGLImageAttribs attrs
;
1755 __DRIimage
*dri_image
;
1757 name
= (EGLint
) (uintptr_t) buffer
;
1759 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1760 if (err
!= EGL_SUCCESS
)
1763 if (attrs
.Width
<= 0 || attrs
.Height
<= 0 ||
1764 attrs
.DRMBufferStrideMESA
<= 0) {
1765 _eglError(EGL_BAD_PARAMETER
,
1766 "bad width, height or stride");
1770 switch (attrs
.DRMBufferFormatMESA
) {
1771 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
1772 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
1773 pitch
= attrs
.DRMBufferStrideMESA
;
1776 _eglError(EGL_BAD_PARAMETER
,
1777 "dri2_create_image_khr: unsupported pixmap depth");
1782 dri2_dpy
->image
->createImageFromName(dri2_dpy
->dri_screen
,
1790 return dri2_create_image_from_dri(disp
, dri_image
);
1794 dri2_check_dma_buf_attribs(const _EGLImageAttribs
*attrs
)
1801 * "Required attributes and their values are as follows:
1803 * * EGL_WIDTH & EGL_HEIGHT: The logical dimensions of the buffer in pixels
1805 * * EGL_LINUX_DRM_FOURCC_EXT: The pixel format of the buffer, as specified
1806 * by drm_fourcc.h and used as the pixel_format parameter of the
1807 * drm_mode_fb_cmd2 ioctl."
1811 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1812 * incomplete, EGL_BAD_PARAMETER is generated."
1814 if (attrs
->Width
<= 0 || attrs
->Height
<= 0 ||
1815 !attrs
->DMABufFourCC
.IsPresent
) {
1816 _eglError(EGL_BAD_PARAMETER
, "attribute(s) missing");
1823 * "If <target> is EGL_LINUX_DMA_BUF_EXT and one or more of the values
1824 * specified for a plane's pitch or offset isn't supported by EGL,
1825 * EGL_BAD_ACCESS is generated."
1827 for (i
= 0; i
< ARRAY_SIZE(attrs
->DMABufPlanePitches
); ++i
) {
1828 if (attrs
->DMABufPlanePitches
[i
].IsPresent
&&
1829 attrs
->DMABufPlanePitches
[i
].Value
<= 0) {
1830 _eglError(EGL_BAD_ACCESS
, "invalid pitch");
1838 /* Returns the total number of file descriptors. Zero indicates an error. */
1840 dri2_check_dma_buf_format(const _EGLImageAttribs
*attrs
)
1842 unsigned i
, plane_n
;
1844 switch (attrs
->DMABufFourCC
.Value
) {
1846 case DRM_FORMAT_RG88
:
1847 case DRM_FORMAT_GR88
:
1848 case DRM_FORMAT_RGB332
:
1849 case DRM_FORMAT_BGR233
:
1850 case DRM_FORMAT_XRGB4444
:
1851 case DRM_FORMAT_XBGR4444
:
1852 case DRM_FORMAT_RGBX4444
:
1853 case DRM_FORMAT_BGRX4444
:
1854 case DRM_FORMAT_ARGB4444
:
1855 case DRM_FORMAT_ABGR4444
:
1856 case DRM_FORMAT_RGBA4444
:
1857 case DRM_FORMAT_BGRA4444
:
1858 case DRM_FORMAT_XRGB1555
:
1859 case DRM_FORMAT_XBGR1555
:
1860 case DRM_FORMAT_RGBX5551
:
1861 case DRM_FORMAT_BGRX5551
:
1862 case DRM_FORMAT_ARGB1555
:
1863 case DRM_FORMAT_ABGR1555
:
1864 case DRM_FORMAT_RGBA5551
:
1865 case DRM_FORMAT_BGRA5551
:
1866 case DRM_FORMAT_RGB565
:
1867 case DRM_FORMAT_BGR565
:
1868 case DRM_FORMAT_RGB888
:
1869 case DRM_FORMAT_BGR888
:
1870 case DRM_FORMAT_XRGB8888
:
1871 case DRM_FORMAT_XBGR8888
:
1872 case DRM_FORMAT_RGBX8888
:
1873 case DRM_FORMAT_BGRX8888
:
1874 case DRM_FORMAT_ARGB8888
:
1875 case DRM_FORMAT_ABGR8888
:
1876 case DRM_FORMAT_RGBA8888
:
1877 case DRM_FORMAT_BGRA8888
:
1878 case DRM_FORMAT_XRGB2101010
:
1879 case DRM_FORMAT_XBGR2101010
:
1880 case DRM_FORMAT_RGBX1010102
:
1881 case DRM_FORMAT_BGRX1010102
:
1882 case DRM_FORMAT_ARGB2101010
:
1883 case DRM_FORMAT_ABGR2101010
:
1884 case DRM_FORMAT_RGBA1010102
:
1885 case DRM_FORMAT_BGRA1010102
:
1886 case DRM_FORMAT_YUYV
:
1887 case DRM_FORMAT_YVYU
:
1888 case DRM_FORMAT_UYVY
:
1889 case DRM_FORMAT_VYUY
:
1892 case DRM_FORMAT_NV12
:
1893 case DRM_FORMAT_NV21
:
1894 case DRM_FORMAT_NV16
:
1895 case DRM_FORMAT_NV61
:
1898 case DRM_FORMAT_YUV410
:
1899 case DRM_FORMAT_YVU410
:
1900 case DRM_FORMAT_YUV411
:
1901 case DRM_FORMAT_YVU411
:
1902 case DRM_FORMAT_YUV420
:
1903 case DRM_FORMAT_YVU420
:
1904 case DRM_FORMAT_YUV422
:
1905 case DRM_FORMAT_YVU422
:
1906 case DRM_FORMAT_YUV444
:
1907 case DRM_FORMAT_YVU444
:
1911 _eglError(EGL_BAD_ATTRIBUTE
, "invalid format");
1918 * "* If <target> is EGL_LINUX_DMA_BUF_EXT, and the list of attributes is
1919 * incomplete, EGL_BAD_PARAMETER is generated."
1921 for (i
= 0; i
< plane_n
; ++i
) {
1922 if (!attrs
->DMABufPlaneFds
[i
].IsPresent
||
1923 !attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
1924 !attrs
->DMABufPlanePitches
[i
].IsPresent
) {
1925 _eglError(EGL_BAD_PARAMETER
, "plane attribute(s) missing");
1931 * The spec also says:
1933 * "If <target> is EGL_LINUX_DMA_BUF_EXT, and the EGL_LINUX_DRM_FOURCC_EXT
1934 * attribute indicates a single-plane format, EGL_BAD_ATTRIBUTE is
1935 * generated if any of the EGL_DMA_BUF_PLANE1_* or EGL_DMA_BUF_PLANE2_*
1936 * attributes are specified."
1938 for (i
= plane_n
; i
< 3; ++i
) {
1939 if (attrs
->DMABufPlaneFds
[i
].IsPresent
||
1940 attrs
->DMABufPlaneOffsets
[i
].IsPresent
||
1941 attrs
->DMABufPlanePitches
[i
].IsPresent
) {
1942 _eglError(EGL_BAD_ATTRIBUTE
, "too many plane attributes");
1953 * "If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT target, the
1954 * EGL will take a reference to the dma_buf(s) which it will release at any
1955 * time while the EGLDisplay is initialized. It is the responsibility of the
1956 * application to close the dma_buf file descriptors."
1958 * Therefore we must never close or otherwise modify the file descriptors.
1961 dri2_create_image_dma_buf(_EGLDisplay
*disp
, _EGLContext
*ctx
,
1962 EGLClientBuffer buffer
, const EGLint
*attr_list
)
1964 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1967 _EGLImageAttribs attrs
;
1968 __DRIimage
*dri_image
;
1979 * ""* If <target> is EGL_LINUX_DMA_BUF_EXT and <buffer> is not NULL, the
1980 * error EGL_BAD_PARAMETER is generated."
1982 if (buffer
!= NULL
) {
1983 _eglError(EGL_BAD_PARAMETER
, "buffer not NULL");
1987 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
1988 if (err
!= EGL_SUCCESS
) {
1989 _eglError(err
, "bad attribute");
1993 if (!dri2_check_dma_buf_attribs(&attrs
))
1996 num_fds
= dri2_check_dma_buf_format(&attrs
);
2000 for (i
= 0; i
< num_fds
; ++i
) {
2001 fds
[i
] = attrs
.DMABufPlaneFds
[i
].Value
;
2002 pitches
[i
] = attrs
.DMABufPlanePitches
[i
].Value
;
2003 offsets
[i
] = attrs
.DMABufPlaneOffsets
[i
].Value
;
2007 dri2_dpy
->image
->createImageFromDmaBufs(dri2_dpy
->dri_screen
,
2008 attrs
.Width
, attrs
.Height
, attrs
.DMABufFourCC
.Value
,
2009 fds
, num_fds
, pitches
, offsets
,
2010 attrs
.DMABufYuvColorSpaceHint
.Value
,
2011 attrs
.DMABufSampleRangeHint
.Value
,
2012 attrs
.DMABufChromaHorizontalSiting
.Value
,
2013 attrs
.DMABufChromaVerticalSiting
.Value
,
2016 dri2_create_image_khr_texture_error(error
);
2019 return EGL_NO_IMAGE_KHR
;
2021 res
= dri2_create_image_from_dri(disp
, dri_image
);
2026 dri2_create_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2027 const EGLint
*attr_list
)
2029 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2030 struct dri2_egl_image
*dri2_img
;
2031 _EGLImageAttribs attrs
;
2032 unsigned int dri_use
, valid_mask
;
2034 EGLint err
= EGL_SUCCESS
;
2038 dri2_img
= malloc(sizeof *dri2_img
);
2040 _eglError(EGL_BAD_ALLOC
, "dri2_create_image_khr");
2041 return EGL_NO_IMAGE_KHR
;
2045 err
= EGL_BAD_PARAMETER
;
2049 if (!_eglInitImage(&dri2_img
->base
, disp
)) {
2050 err
= EGL_BAD_PARAMETER
;
2054 err
= _eglParseImageAttribList(&attrs
, disp
, attr_list
);
2055 if (err
!= EGL_SUCCESS
)
2058 if (attrs
.Width
<= 0 || attrs
.Height
<= 0) {
2059 _eglLog(_EGL_WARNING
, "bad width or height (%dx%d)",
2060 attrs
.Width
, attrs
.Height
);
2064 switch (attrs
.DRMBufferFormatMESA
) {
2065 case EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
:
2066 format
= __DRI_IMAGE_FORMAT_ARGB8888
;
2069 _eglLog(_EGL_WARNING
, "bad image format value 0x%04x",
2070 attrs
.DRMBufferFormatMESA
);
2075 EGL_DRM_BUFFER_USE_SCANOUT_MESA
|
2076 EGL_DRM_BUFFER_USE_SHARE_MESA
|
2077 EGL_DRM_BUFFER_USE_CURSOR_MESA
;
2078 if (attrs
.DRMBufferUseMESA
& ~valid_mask
) {
2079 _eglLog(_EGL_WARNING
, "bad image use bit 0x%04x",
2080 attrs
.DRMBufferUseMESA
& ~valid_mask
);
2085 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SHARE_MESA
)
2086 dri_use
|= __DRI_IMAGE_USE_SHARE
;
2087 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_SCANOUT_MESA
)
2088 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
2089 if (attrs
.DRMBufferUseMESA
& EGL_DRM_BUFFER_USE_CURSOR_MESA
)
2090 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
2092 dri2_img
->dri_image
=
2093 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
2094 attrs
.Width
, attrs
.Height
,
2095 format
, dri_use
, dri2_img
);
2096 if (dri2_img
->dri_image
== NULL
) {
2097 err
= EGL_BAD_ALLOC
;
2101 return &dri2_img
->base
;
2105 _eglError(err
, "dri2_create_drm_image_mesa");
2107 return EGL_NO_IMAGE_KHR
;
2111 dri2_export_drm_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2112 EGLint
*name
, EGLint
*handle
, EGLint
*stride
)
2114 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2115 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2119 if (name
&& !dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2120 __DRI_IMAGE_ATTRIB_NAME
, name
)) {
2121 _eglError(EGL_BAD_ALLOC
, "dri2_export_drm_image_mesa");
2126 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2127 __DRI_IMAGE_ATTRIB_HANDLE
, handle
);
2130 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2131 __DRI_IMAGE_ATTRIB_STRIDE
, stride
);
2137 dri2_export_dma_buf_image_query_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2139 EGLint
*fourcc
, EGLint
*nplanes
,
2140 EGLuint64KHR
*modifiers
)
2142 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2143 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2149 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2150 __DRI_IMAGE_ATTRIB_NUM_PLANES
, nplanes
);
2152 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2153 __DRI_IMAGE_ATTRIB_FOURCC
, fourcc
);
2162 dri2_export_dma_buf_image_mesa(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*img
,
2163 int *fds
, EGLint
*strides
, EGLint
*offsets
)
2165 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2166 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
2170 /* rework later to provide multiple fds/strides/offsets */
2172 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2173 __DRI_IMAGE_ATTRIB_FD
, fds
);
2176 dri2_dpy
->image
->queryImage(dri2_img
->dri_image
,
2177 __DRI_IMAGE_ATTRIB_STRIDE
, strides
);
2188 dri2_create_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2189 _EGLContext
*ctx
, EGLenum target
,
2190 EGLClientBuffer buffer
, const EGLint
*attr_list
)
2195 case EGL_GL_TEXTURE_2D_KHR
:
2196 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR
:
2197 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR
:
2198 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR
:
2199 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR
:
2200 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR
:
2201 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR
:
2202 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2203 case EGL_GL_TEXTURE_3D_KHR
:
2204 if (disp
->Extensions
.KHR_gl_texture_3D_image
) {
2205 return dri2_create_image_khr_texture(disp
, ctx
, target
, buffer
, attr_list
);
2208 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2209 return EGL_NO_IMAGE_KHR
;
2211 case EGL_GL_RENDERBUFFER_KHR
:
2212 return dri2_create_image_khr_renderbuffer(disp
, ctx
, buffer
, attr_list
);
2214 case EGL_DRM_BUFFER_MESA
:
2215 return dri2_create_image_mesa_drm_buffer(disp
, ctx
, buffer
, attr_list
);
2216 case EGL_LINUX_DMA_BUF_EXT
:
2217 return dri2_create_image_dma_buf(disp
, ctx
, buffer
, attr_list
);
2219 #ifdef HAVE_WAYLAND_PLATFORM
2220 case EGL_WAYLAND_BUFFER_WL
:
2221 return dri2_create_image_wayland_wl_buffer(disp
, ctx
, buffer
, attr_list
);
2224 _eglError(EGL_BAD_PARAMETER
, "dri2_create_image_khr");
2225 return EGL_NO_IMAGE_KHR
;
2230 dri2_destroy_image_khr(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLImage
*image
)
2232 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2233 struct dri2_egl_image
*dri2_img
= dri2_egl_image(image
);
2237 dri2_dpy
->image
->destroyImage(dri2_img
->dri_image
);
2243 #ifdef HAVE_WAYLAND_PLATFORM
2246 dri2_wl_reference_buffer(void *user_data
, uint32_t name
, int fd
,
2247 struct wl_drm_buffer
*buffer
)
2249 _EGLDisplay
*disp
= user_data
;
2250 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2252 int i
, dri_components
= 0;
2255 img
= dri2_dpy
->image
->createImageFromNames(dri2_dpy
->dri_screen
,
2264 img
= dri2_dpy
->image
->createImageFromFds(dri2_dpy
->dri_screen
,
2276 dri2_dpy
->image
->queryImage(img
, __DRI_IMAGE_ATTRIB_COMPONENTS
, &dri_components
);
2278 buffer
->driver_format
= NULL
;
2279 for (i
= 0; i
< ARRAY_SIZE(wl_drm_components
); i
++)
2280 if (wl_drm_components
[i
].dri_components
== dri_components
)
2281 buffer
->driver_format
= &wl_drm_components
[i
];
2283 if (buffer
->driver_format
== NULL
)
2284 dri2_dpy
->image
->destroyImage(img
);
2286 buffer
->driver_buffer
= img
;
2290 dri2_wl_release_buffer(void *user_data
, struct wl_drm_buffer
*buffer
)
2292 _EGLDisplay
*disp
= user_data
;
2293 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2295 dri2_dpy
->image
->destroyImage(buffer
->driver_buffer
);
2298 static struct wayland_drm_callbacks wl_drm_callbacks
= {
2299 .authenticate
= NULL
,
2300 .reference_buffer
= dri2_wl_reference_buffer
,
2301 .release_buffer
= dri2_wl_release_buffer
2305 dri2_bind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2306 struct wl_display
*wl_dpy
)
2308 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2314 if (dri2_dpy
->wl_server_drm
)
2317 wl_drm_callbacks
.authenticate
=
2318 (int(*)(void *, uint32_t)) dri2_dpy
->vtbl
->authenticate
;
2320 if (drmGetCap(dri2_dpy
->fd
, DRM_CAP_PRIME
, &cap
) == 0 &&
2321 cap
== (DRM_PRIME_CAP_IMPORT
| DRM_PRIME_CAP_EXPORT
) &&
2322 dri2_dpy
->image
->base
.version
>= 7 &&
2323 dri2_dpy
->image
->createImageFromFds
!= NULL
)
2324 flags
|= WAYLAND_DRM_PRIME
;
2326 dri2_dpy
->wl_server_drm
=
2327 wayland_drm_init(wl_dpy
, dri2_dpy
->device_name
,
2328 &wl_drm_callbacks
, disp
, flags
);
2330 if (!dri2_dpy
->wl_server_drm
)
2333 #ifdef HAVE_DRM_PLATFORM
2334 /* We have to share the wl_drm instance with gbm, so gbm can convert
2335 * wl_buffers to gbm bos. */
2336 if (dri2_dpy
->gbm_dri
)
2337 dri2_dpy
->gbm_dri
->wl_drm
= dri2_dpy
->wl_server_drm
;
2344 dri2_unbind_wayland_display_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2345 struct wl_display
*wl_dpy
)
2347 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2351 if (!dri2_dpy
->wl_server_drm
)
2354 wayland_drm_uninit(dri2_dpy
->wl_server_drm
);
2355 dri2_dpy
->wl_server_drm
= NULL
;
2361 dri2_query_wayland_buffer_wl(_EGLDriver
*drv
, _EGLDisplay
*disp
,
2362 struct wl_resource
*buffer_resource
,
2363 EGLint attribute
, EGLint
*value
)
2365 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
2366 struct wl_drm_buffer
*buffer
;
2367 const struct wl_drm_components_descriptor
*format
;
2369 buffer
= wayland_drm_buffer_get(dri2_dpy
->wl_server_drm
, buffer_resource
);
2373 format
= buffer
->driver_format
;
2374 switch (attribute
) {
2375 case EGL_TEXTURE_FORMAT
:
2376 *value
= format
->components
;
2379 *value
= buffer
->width
;
2382 *value
= buffer
->height
;
2391 dri2_egl_ref_sync(struct dri2_egl_sync
*sync
)
2393 p_atomic_inc(&sync
->refcount
);
2397 dri2_egl_unref_sync(struct dri2_egl_display
*dri2_dpy
,
2398 struct dri2_egl_sync
*dri2_sync
)
2400 if (p_atomic_dec_zero(&dri2_sync
->refcount
)) {
2401 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
)
2402 cnd_destroy(&dri2_sync
->cond
);
2404 if (dri2_sync
->fence
)
2405 dri2_dpy
->fence
->destroy_fence(dri2_dpy
->dri_screen
, dri2_sync
->fence
);
2412 dri2_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
2413 EGLenum type
, const EGLint
*attrib_list
,
2414 const EGLAttrib
*attrib_list64
)
2416 _EGLContext
*ctx
= _eglGetCurrentContext();
2417 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2418 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2419 struct dri2_egl_sync
*dri2_sync
;
2421 pthread_condattr_t attr
;
2423 dri2_sync
= calloc(1, sizeof(struct dri2_egl_sync
));
2425 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2429 if (!_eglInitSync(&dri2_sync
->base
, dpy
, type
, attrib_list
,
2436 case EGL_SYNC_FENCE_KHR
:
2437 dri2_sync
->fence
= dri2_dpy
->fence
->create_fence(dri2_ctx
->dri_context
);
2438 if (!dri2_sync
->fence
) {
2439 /* Why did it fail? DRI doesn't return an error code, so we emit
2440 * a generic EGL error that doesn't communicate user error.
2442 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
2448 case EGL_SYNC_CL_EVENT_KHR
:
2449 dri2_sync
->fence
= dri2_dpy
->fence
->get_fence_from_cl_event(
2450 dri2_dpy
->dri_screen
,
2451 dri2_sync
->base
.CLEvent
);
2452 /* this can only happen if the cl_event passed in is invalid. */
2453 if (!dri2_sync
->fence
) {
2454 _eglError(EGL_BAD_ATTRIBUTE
, "eglCreateSyncKHR");
2459 /* the initial status must be "signaled" if the cl_event is signaled */
2460 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
->dri_context
,
2461 dri2_sync
->fence
, 0, 0))
2462 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2465 case EGL_SYNC_REUSABLE_KHR
:
2466 /* intialize attr */
2467 ret
= pthread_condattr_init(&attr
);
2470 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2475 /* change clock attribute to CLOCK_MONOTONIC */
2476 ret
= pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
2479 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2484 ret
= pthread_cond_init(&dri2_sync
->cond
, &attr
);
2487 _eglError(EGL_BAD_ACCESS
, "eglCreateSyncKHR");
2492 /* initial status of reusable sync must be "unsignaled" */
2493 dri2_sync
->base
.SyncStatus
= EGL_UNSIGNALED_KHR
;
2497 p_atomic_set(&dri2_sync
->refcount
, 1);
2498 return &dri2_sync
->base
;
2502 dri2_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2504 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2505 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2506 EGLint ret
= EGL_TRUE
;
2509 /* if type of sync is EGL_SYNC_REUSABLE_KHR and it is not signaled yet,
2510 * then unlock all threads possibly blocked by the reusable sync before
2513 if (dri2_sync
->base
.Type
== EGL_SYNC_REUSABLE_KHR
&&
2514 dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2515 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2516 /* unblock all threads currently blocked by sync */
2517 err
= cnd_broadcast(&dri2_sync
->cond
);
2520 _eglError(EGL_BAD_ACCESS
, "eglDestroySyncKHR");
2524 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2530 dri2_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2531 EGLint flags
, EGLTime timeout
)
2533 _EGLContext
*ctx
= _eglGetCurrentContext();
2534 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2535 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2536 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2537 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2538 unsigned wait_flags
= 0;
2540 /* timespecs for cnd_timedwait */
2541 struct timespec current
;
2544 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
2546 /* The EGL_KHR_fence_sync spec states:
2548 * "If no context is current for the bound API,
2549 * the EGL_SYNC_FLUSH_COMMANDS_BIT_KHR bit is ignored.
2551 if (dri2_ctx
&& flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)
2552 wait_flags
|= __DRI2_FENCE_FLAG_FLUSH_COMMANDS
;
2554 /* the sync object should take a reference while waiting */
2555 dri2_egl_ref_sync(dri2_sync
);
2557 switch (sync
->Type
) {
2558 case EGL_SYNC_FENCE_KHR
:
2559 case EGL_SYNC_CL_EVENT_KHR
:
2560 if (dri2_dpy
->fence
->client_wait_sync(dri2_ctx
? dri2_ctx
->dri_context
: NULL
,
2561 dri2_sync
->fence
, wait_flags
,
2563 dri2_sync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
2565 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2568 case EGL_SYNC_REUSABLE_KHR
:
2569 if (dri2_ctx
&& dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
&&
2570 (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
)) {
2571 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
2572 if (dri2_drv
->glFlush
)
2573 dri2_drv
->glFlush();
2576 /* if timeout is EGL_FOREVER_KHR, it should wait without any timeout.*/
2577 if (timeout
== EGL_FOREVER_KHR
) {
2578 if (mtx_lock(&dri2_sync
->mutex
)) {
2583 ret
= cnd_wait(&dri2_sync
->cond
, &dri2_sync
->mutex
);
2585 if (mtx_unlock(&dri2_sync
->mutex
)) {
2591 _eglError(EGL_BAD_PARAMETER
, "eglClientWaitSyncKHR");
2595 /* if reusable sync has not been yet signaled */
2596 if (dri2_sync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
2597 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
2599 /* calculating when to expire */
2600 expire
.nsec
= timeout
% 1000000000L;
2601 expire
.sec
= timeout
/ 1000000000L;
2603 expire
.nsec
+= current
.tv_nsec
;
2604 expire
.sec
+= current
.tv_sec
;
2606 /* expire.nsec now is a number between 0 and 1999999998 */
2607 if (expire
.nsec
> 999999999L) {
2609 expire
.nsec
-= 1000000000L;
2612 if (mtx_lock(&dri2_sync
->mutex
)) {
2617 ret
= cnd_timedwait(&dri2_sync
->cond
, &dri2_sync
->mutex
, &expire
);
2619 if (mtx_unlock(&dri2_sync
->mutex
)) {
2625 if (ret
== thrd_busy
) {
2626 if (dri2_sync
->base
.SyncStatus
== EGL_UNSIGNALED_KHR
) {
2627 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
2629 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2639 dri2_egl_unref_sync(dri2_dpy
, dri2_sync
);
2641 if (ret
== EGL_FALSE
) {
2642 _eglError(EGL_BAD_ACCESS
, "eglClientWaitSyncKHR");
2650 dri2_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
2653 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2656 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
) {
2657 _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
2661 if (mode
!= EGL_SIGNALED_KHR
&& mode
!= EGL_UNSIGNALED_KHR
) {
2662 _eglError(EGL_BAD_ATTRIBUTE
, "eglSignalSyncKHR");
2666 dri2_sync
->base
.SyncStatus
= mode
;
2668 if (mode
== EGL_SIGNALED_KHR
) {
2669 ret
= cnd_broadcast(&dri2_sync
->cond
);
2671 /* fail to broadcast */
2673 _eglError(EGL_BAD_ACCESS
, "eglSignalSyncKHR");
2682 dri2_server_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
2684 _EGLContext
*ctx
= _eglGetCurrentContext();
2685 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
2686 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
2687 struct dri2_egl_sync
*dri2_sync
= dri2_egl_sync(sync
);
2689 dri2_dpy
->fence
->server_wait_sync(dri2_ctx
->dri_context
,
2690 dri2_sync
->fence
, 0);
2695 dri2_unload(_EGLDriver
*drv
)
2697 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2699 if (dri2_drv
->handle
)
2700 dlclose(dri2_drv
->handle
);
2705 dri2_load(_EGLDriver
*drv
)
2707 struct dri2_egl_driver
*dri2_drv
= dri2_egl_driver(drv
);
2708 #ifdef HAVE_ANDROID_PLATFORM
2709 const char *libname
= "libglapi.so";
2710 #elif defined(__APPLE__)
2711 const char *libname
= "libglapi.0.dylib";
2713 const char *libname
= "libglapi.so.0";
2717 /* RTLD_GLOBAL to make sure glapi symbols are visible to DRI drivers */
2718 handle
= dlopen(libname
, RTLD_LAZY
| RTLD_GLOBAL
);
2720 dri2_drv
->get_proc_address
= (_EGLProc (*)(const char *))
2721 dlsym(handle
, "_glapi_get_proc_address");
2722 if (!dri2_drv
->get_proc_address
|| !libname
) {
2723 /* no need to keep a reference */
2729 /* if glapi is not available, loading DRI drivers will fail */
2730 if (!dri2_drv
->get_proc_address
) {
2731 _eglLog(_EGL_WARNING
, "DRI2: failed to find _glapi_get_proc_address");
2735 dri2_drv
->glFlush
= (void (*)(void))
2736 dri2_drv
->get_proc_address("glFlush");
2738 dri2_drv
->handle
= handle
;
2744 * This is the main entrypoint into the driver, called by libEGL.
2745 * Create a new _EGLDriver object and init its dispatch table.
2748 _eglBuiltInDriverDRI2(const char *args
)
2750 struct dri2_egl_driver
*dri2_drv
;
2754 dri2_drv
= calloc(1, sizeof *dri2_drv
);
2758 if (!dri2_load(&dri2_drv
->base
)) {
2763 _eglInitDriverFallbacks(&dri2_drv
->base
);
2764 dri2_drv
->base
.API
.Initialize
= dri2_initialize
;
2765 dri2_drv
->base
.API
.Terminate
= dri2_terminate
;
2766 dri2_drv
->base
.API
.CreateContext
= dri2_create_context
;
2767 dri2_drv
->base
.API
.DestroyContext
= dri2_destroy_context
;
2768 dri2_drv
->base
.API
.MakeCurrent
= dri2_make_current
;
2769 dri2_drv
->base
.API
.CreateWindowSurface
= dri2_create_window_surface
;
2770 dri2_drv
->base
.API
.CreatePixmapSurface
= dri2_create_pixmap_surface
;
2771 dri2_drv
->base
.API
.CreatePbufferSurface
= dri2_create_pbuffer_surface
;
2772 dri2_drv
->base
.API
.DestroySurface
= dri2_destroy_surface
;
2773 dri2_drv
->base
.API
.GetProcAddress
= dri2_get_proc_address
;
2774 dri2_drv
->base
.API
.WaitClient
= dri2_wait_client
;
2775 dri2_drv
->base
.API
.WaitNative
= dri2_wait_native
;
2776 dri2_drv
->base
.API
.BindTexImage
= dri2_bind_tex_image
;
2777 dri2_drv
->base
.API
.ReleaseTexImage
= dri2_release_tex_image
;
2778 dri2_drv
->base
.API
.SwapInterval
= dri2_swap_interval
;
2779 dri2_drv
->base
.API
.SwapBuffers
= dri2_swap_buffers
;
2780 dri2_drv
->base
.API
.SwapBuffersWithDamageEXT
= dri2_swap_buffers_with_damage
;
2781 dri2_drv
->base
.API
.SwapBuffersRegionNOK
= dri2_swap_buffers_region
;
2782 dri2_drv
->base
.API
.PostSubBufferNV
= dri2_post_sub_buffer
;
2783 dri2_drv
->base
.API
.CopyBuffers
= dri2_copy_buffers
,
2784 dri2_drv
->base
.API
.QueryBufferAge
= dri2_query_buffer_age
;
2785 dri2_drv
->base
.API
.CreateImageKHR
= dri2_create_image
;
2786 dri2_drv
->base
.API
.DestroyImageKHR
= dri2_destroy_image_khr
;
2787 dri2_drv
->base
.API
.CreateWaylandBufferFromImageWL
= dri2_create_wayland_buffer_from_image
;
2789 dri2_drv
->base
.API
.CreateDRMImageMESA
= dri2_create_drm_image_mesa
;
2790 dri2_drv
->base
.API
.ExportDRMImageMESA
= dri2_export_drm_image_mesa
;
2791 dri2_drv
->base
.API
.ExportDMABUFImageQueryMESA
= dri2_export_dma_buf_image_query_mesa
;
2792 dri2_drv
->base
.API
.ExportDMABUFImageMESA
= dri2_export_dma_buf_image_mesa
;
2794 #ifdef HAVE_WAYLAND_PLATFORM
2795 dri2_drv
->base
.API
.BindWaylandDisplayWL
= dri2_bind_wayland_display_wl
;
2796 dri2_drv
->base
.API
.UnbindWaylandDisplayWL
= dri2_unbind_wayland_display_wl
;
2797 dri2_drv
->base
.API
.QueryWaylandBufferWL
= dri2_query_wayland_buffer_wl
;
2799 dri2_drv
->base
.API
.GetSyncValuesCHROMIUM
= dri2_get_sync_values_chromium
;
2800 dri2_drv
->base
.API
.CreateSyncKHR
= dri2_create_sync
;
2801 dri2_drv
->base
.API
.ClientWaitSyncKHR
= dri2_client_wait_sync
;
2802 dri2_drv
->base
.API
.SignalSyncKHR
= dri2_signal_sync
;
2803 dri2_drv
->base
.API
.WaitSyncKHR
= dri2_server_wait_sync
;
2804 dri2_drv
->base
.API
.DestroySyncKHR
= dri2_destroy_sync
;
2806 dri2_drv
->base
.Name
= "DRI2";
2807 dri2_drv
->base
.Unload
= dri2_unload
;
2809 return &dri2_drv
->base
;