2 * Copyright © 2011-2012 Intel Corporation
3 * Copyright © 2012 Collabora, Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Kristian Høgsberg <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
39 #include "drm-uapi/drm_fourcc.h"
43 #include "egl_dri2_fallbacks.h"
45 #include "util/u_vector.h"
46 #include "util/anon_file.h"
47 #include "eglglobals.h"
49 #include <wayland-egl-backend.h>
50 #include <wayland-client.h>
51 #include "wayland-drm-client-protocol.h"
52 #include "linux-dmabuf-unstable-v1-client-protocol.h"
54 /* cheesy workaround until wayland 1.18 is released */
55 #if WAYLAND_VERSION_MAJOR > 1 || \
56 (WAYLAND_VERSION_MAJOR == 1 && WAYLAND_VERSION_MINOR < 18)
57 #define WL_SHM_FORMAT_ABGR16161616F 0x48344241
58 #define WL_SHM_FORMAT_XBGR16161616F 0x48344258
62 * The index of entries in this table is used as a bitmask in
63 * dri2_dpy->formats, which tracks the formats supported by our server.
65 static const struct dri2_wl_visual
{
66 const char *format_name
;
67 uint32_t wl_drm_format
;
68 uint32_t wl_shm_format
;
70 /* alt_dri_image_format is a substitute wl_buffer format to use for a
71 * wl-server unsupported dri_image_format, ie. some other dri_image_format in
72 * the table, of the same precision but with different channel ordering, or
73 * __DRI_IMAGE_FORMAT_NONE if an alternate format is not needed or supported.
74 * The code checks if alt_dri_image_format can be used as a fallback for a
75 * dri_image_format for a given wl-server implementation.
77 int alt_dri_image_format
;
80 unsigned int rgba_sizes
[4];
81 } dri2_wl_visuals
[] = {
84 WL_DRM_FORMAT_ABGR16F
, WL_SHM_FORMAT_ABGR16161616F
,
85 __DRI_IMAGE_FORMAT_ABGR16161616F
, 0, 64,
91 WL_DRM_FORMAT_XBGR16F
, WL_SHM_FORMAT_XBGR16161616F
,
92 __DRI_IMAGE_FORMAT_XBGR16161616F
, 0, 64,
98 WL_DRM_FORMAT_XRGB2101010
, WL_SHM_FORMAT_XRGB2101010
,
99 __DRI_IMAGE_FORMAT_XRGB2101010
, __DRI_IMAGE_FORMAT_XBGR2101010
, 32,
105 WL_DRM_FORMAT_ARGB2101010
, WL_SHM_FORMAT_ARGB2101010
,
106 __DRI_IMAGE_FORMAT_ARGB2101010
, __DRI_IMAGE_FORMAT_ABGR2101010
, 32,
112 WL_DRM_FORMAT_XBGR2101010
, WL_SHM_FORMAT_XBGR2101010
,
113 __DRI_IMAGE_FORMAT_XBGR2101010
, __DRI_IMAGE_FORMAT_XRGB2101010
, 32,
119 WL_DRM_FORMAT_ABGR2101010
, WL_SHM_FORMAT_ABGR2101010
,
120 __DRI_IMAGE_FORMAT_ABGR2101010
, __DRI_IMAGE_FORMAT_ARGB2101010
, 32,
126 WL_DRM_FORMAT_XRGB8888
, WL_SHM_FORMAT_XRGB8888
,
127 __DRI_IMAGE_FORMAT_XRGB8888
, __DRI_IMAGE_FORMAT_NONE
, 32,
133 WL_DRM_FORMAT_ARGB8888
, WL_SHM_FORMAT_ARGB8888
,
134 __DRI_IMAGE_FORMAT_ARGB8888
, __DRI_IMAGE_FORMAT_NONE
, 32,
140 WL_DRM_FORMAT_RGB565
, WL_SHM_FORMAT_RGB565
,
141 __DRI_IMAGE_FORMAT_RGB565
, __DRI_IMAGE_FORMAT_NONE
, 16,
147 static_assert(ARRAY_SIZE(dri2_wl_visuals
) <= EGL_DRI2_MAX_FORMATS
,
148 "dri2_egl_display::formats is not large enough for "
149 "the formats in dri2_wl_visuals");
152 dri2_wl_visual_idx_from_config(struct dri2_egl_display
*dri2_dpy
,
153 const __DRIconfig
*config
)
156 unsigned int sizes
[4];
158 dri2_get_shifts_and_sizes(dri2_dpy
->core
, config
, shifts
, sizes
);
160 for (unsigned int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
161 const struct dri2_wl_visual
*wl_visual
= &dri2_wl_visuals
[i
];
163 if (shifts
[0] == wl_visual
->rgba_shifts
[0] &&
164 shifts
[1] == wl_visual
->rgba_shifts
[1] &&
165 shifts
[2] == wl_visual
->rgba_shifts
[2] &&
166 shifts
[3] == wl_visual
->rgba_shifts
[3] &&
167 sizes
[0] == wl_visual
->rgba_sizes
[0] &&
168 sizes
[1] == wl_visual
->rgba_sizes
[1] &&
169 sizes
[2] == wl_visual
->rgba_sizes
[2] &&
170 sizes
[3] == wl_visual
->rgba_sizes
[3]) {
179 dri2_wl_visual_idx_from_fourcc(uint32_t fourcc
)
181 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
182 /* wl_drm format codes overlap with DRIImage FourCC codes for all formats
184 if (dri2_wl_visuals
[i
].wl_drm_format
== fourcc
)
192 dri2_wl_visual_idx_from_dri_image_format(uint32_t dri_image_format
)
194 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
195 if (dri2_wl_visuals
[i
].dri_image_format
== dri_image_format
)
203 dri2_wl_visual_idx_from_shm_format(uint32_t shm_format
)
205 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
206 if (dri2_wl_visuals
[i
].wl_shm_format
== shm_format
)
214 dri2_wl_is_format_supported(void* user_data
, uint32_t format
)
216 _EGLDisplay
*disp
= (_EGLDisplay
*) user_data
;
217 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
218 int j
= dri2_wl_visual_idx_from_fourcc(format
);
223 for (int i
= 0; dri2_dpy
->driver_configs
[i
]; i
++)
224 if (j
== dri2_wl_visual_idx_from_config(dri2_dpy
,
225 dri2_dpy
->driver_configs
[i
]))
232 roundtrip(struct dri2_egl_display
*dri2_dpy
)
234 return wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
, dri2_dpy
->wl_queue
);
238 wl_buffer_release(void *data
, struct wl_buffer
*buffer
)
240 struct dri2_egl_surface
*dri2_surf
= data
;
243 for (i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); ++i
)
244 if (dri2_surf
->color_buffers
[i
].wl_buffer
== buffer
)
247 assert (i
< ARRAY_SIZE(dri2_surf
->color_buffers
));
249 if (dri2_surf
->color_buffers
[i
].wl_release
) {
250 wl_buffer_destroy(buffer
);
251 dri2_surf
->color_buffers
[i
].wl_release
= false;
252 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
255 dri2_surf
->color_buffers
[i
].locked
= false;
258 static const struct wl_buffer_listener wl_buffer_listener
= {
259 .release
= wl_buffer_release
263 resize_callback(struct wl_egl_window
*wl_win
, void *data
)
265 struct dri2_egl_surface
*dri2_surf
= data
;
266 struct dri2_egl_display
*dri2_dpy
=
267 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
269 if (dri2_surf
->base
.Width
== wl_win
->width
&&
270 dri2_surf
->base
.Height
== wl_win
->height
)
273 /* Update the surface size as soon as native window is resized; from user
274 * pov, this makes the effect that resize is done immediately after native
275 * window resize, without requiring to wait until the first draw.
277 * A more detailed and lengthy explanation can be found at
278 * https://lists.freedesktop.org/archives/mesa-dev/2018-June/196474.html
280 if (!dri2_surf
->back
) {
281 dri2_surf
->base
.Width
= wl_win
->width
;
282 dri2_surf
->base
.Height
= wl_win
->height
;
284 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
288 destroy_window_callback(void *data
)
290 struct dri2_egl_surface
*dri2_surf
= data
;
291 dri2_surf
->wl_win
= NULL
;
294 static struct wl_surface
*
295 get_wl_surface_proxy(struct wl_egl_window
*window
)
297 /* Version 3 of wl_egl_window introduced a version field at the same
298 * location where a pointer to wl_surface was stored. Thus, if
299 * window->version is dereferenceable, we've been given an older version of
300 * wl_egl_window, and window->version points to wl_surface */
301 if (_eglPointerIsDereferencable((void *)(window
->version
))) {
302 return wl_proxy_create_wrapper((void *)(window
->version
));
304 return wl_proxy_create_wrapper(window
->surface
);
308 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
311 dri2_wl_create_window_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
312 _EGLConfig
*conf
, void *native_window
,
313 const EGLint
*attrib_list
)
315 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
316 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
317 struct wl_egl_window
*window
= native_window
;
318 struct dri2_egl_surface
*dri2_surf
;
320 const __DRIconfig
*config
;
322 dri2_surf
= calloc(1, sizeof *dri2_surf
);
324 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
328 if (!dri2_init_surface(&dri2_surf
->base
, disp
, EGL_WINDOW_BIT
, conf
,
329 attrib_list
, false, native_window
))
332 config
= dri2_get_dri_config(dri2_conf
, EGL_WINDOW_BIT
,
333 dri2_surf
->base
.GLColorspace
);
336 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
340 dri2_surf
->base
.Width
= window
->width
;
341 dri2_surf
->base
.Height
= window
->height
;
343 visual_idx
= dri2_wl_visual_idx_from_config(dri2_dpy
, config
);
344 assert(visual_idx
!= -1);
346 if (dri2_dpy
->wl_dmabuf
|| dri2_dpy
->wl_drm
) {
347 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
349 assert(dri2_dpy
->wl_shm
);
350 dri2_surf
->format
= dri2_wl_visuals
[visual_idx
].wl_shm_format
;
353 dri2_surf
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
354 if (!dri2_surf
->wl_queue
) {
355 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
359 if (dri2_dpy
->wl_drm
) {
360 dri2_surf
->wl_drm_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_drm
);
361 if (!dri2_surf
->wl_drm_wrapper
) {
362 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
365 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_drm_wrapper
,
366 dri2_surf
->wl_queue
);
369 dri2_surf
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
370 if (!dri2_surf
->wl_dpy_wrapper
) {
371 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
374 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_dpy_wrapper
,
375 dri2_surf
->wl_queue
);
377 dri2_surf
->wl_surface_wrapper
= get_wl_surface_proxy(window
);
378 if (!dri2_surf
->wl_surface_wrapper
) {
379 _eglError(EGL_BAD_ALLOC
, "dri2_create_surface");
380 goto cleanup_dpy_wrapper
;
382 wl_proxy_set_queue((struct wl_proxy
*)dri2_surf
->wl_surface_wrapper
,
383 dri2_surf
->wl_queue
);
385 dri2_surf
->wl_win
= window
;
386 dri2_surf
->wl_win
->driver_private
= dri2_surf
;
387 dri2_surf
->wl_win
->destroy_window_callback
= destroy_window_callback
;
389 dri2_surf
->wl_win
->resize_callback
= resize_callback
;
391 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
392 goto cleanup_surf_wrapper
;
394 dri2_surf
->base
.SwapInterval
= dri2_dpy
->default_swap_interval
;
396 return &dri2_surf
->base
;
398 cleanup_surf_wrapper
:
399 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
401 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
403 if (dri2_surf
->wl_drm_wrapper
)
404 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
406 wl_event_queue_destroy(dri2_surf
->wl_queue
);
414 dri2_wl_create_pixmap_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
415 _EGLConfig
*conf
, void *native_window
,
416 const EGLint
*attrib_list
)
418 /* From the EGL_EXT_platform_wayland spec, version 3:
420 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
421 * that belongs to Wayland. Any such call fails and generates
424 _eglError(EGL_BAD_PARAMETER
, "cannot create EGL pixmap surfaces on "
430 * Called via eglDestroySurface(), drv->API.DestroySurface().
433 dri2_wl_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
435 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
436 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
440 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
442 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
443 if (dri2_surf
->color_buffers
[i
].wl_buffer
)
444 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
445 if (dri2_surf
->color_buffers
[i
].dri_image
)
446 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
447 if (dri2_surf
->color_buffers
[i
].linear_copy
)
448 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
449 if (dri2_surf
->color_buffers
[i
].data
)
450 munmap(dri2_surf
->color_buffers
[i
].data
,
451 dri2_surf
->color_buffers
[i
].data_size
);
455 dri2_egl_surface_free_local_buffers(dri2_surf
);
457 if (dri2_surf
->throttle_callback
)
458 wl_callback_destroy(dri2_surf
->throttle_callback
);
460 if (dri2_surf
->wl_win
) {
461 dri2_surf
->wl_win
->driver_private
= NULL
;
462 dri2_surf
->wl_win
->resize_callback
= NULL
;
463 dri2_surf
->wl_win
->destroy_window_callback
= NULL
;
466 wl_proxy_wrapper_destroy(dri2_surf
->wl_surface_wrapper
);
467 wl_proxy_wrapper_destroy(dri2_surf
->wl_dpy_wrapper
);
468 if (dri2_surf
->wl_drm_wrapper
)
469 wl_proxy_wrapper_destroy(dri2_surf
->wl_drm_wrapper
);
470 wl_event_queue_destroy(dri2_surf
->wl_queue
);
472 dri2_fini_surface(surf
);
479 dri2_wl_release_buffers(struct dri2_egl_surface
*dri2_surf
)
481 struct dri2_egl_display
*dri2_dpy
=
482 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
484 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
485 if (dri2_surf
->color_buffers
[i
].wl_buffer
) {
486 if (dri2_surf
->color_buffers
[i
].locked
) {
487 dri2_surf
->color_buffers
[i
].wl_release
= true;
489 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
490 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
493 if (dri2_surf
->color_buffers
[i
].dri_image
)
494 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
495 if (dri2_surf
->color_buffers
[i
].linear_copy
)
496 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
497 if (dri2_surf
->color_buffers
[i
].data
)
498 munmap(dri2_surf
->color_buffers
[i
].data
,
499 dri2_surf
->color_buffers
[i
].data_size
);
501 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
502 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
503 dri2_surf
->color_buffers
[i
].data
= NULL
;
507 dri2_egl_surface_free_local_buffers(dri2_surf
);
511 get_back_bo(struct dri2_egl_surface
*dri2_surf
)
513 struct dri2_egl_display
*dri2_dpy
=
514 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
517 unsigned int dri_image_format
;
518 unsigned int linear_dri_image_format
;
522 visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
523 assert(visual_idx
!= -1);
524 dri_image_format
= dri2_wl_visuals
[visual_idx
].dri_image_format
;
525 linear_dri_image_format
= dri_image_format
;
526 modifiers
= u_vector_tail(&dri2_dpy
->wl_modifiers
[visual_idx
]);
527 num_modifiers
= u_vector_length(&dri2_dpy
->wl_modifiers
[visual_idx
]);
529 if (num_modifiers
== 1 && modifiers
[0] == DRM_FORMAT_MOD_INVALID
) {
530 /* For the purposes of this function, an INVALID modifier on its own
531 * means the modifiers aren't supported.
536 /* Substitute dri image format if server does not support original format */
537 if (!BITSET_TEST(dri2_dpy
->formats
, visual_idx
))
538 linear_dri_image_format
= dri2_wl_visuals
[visual_idx
].alt_dri_image_format
;
540 /* These asserts hold, as long as dri2_wl_visuals[] is self-consistent and
541 * the PRIME substitution logic in dri2_wl_add_configs_for_visuals() is free
544 assert(linear_dri_image_format
!= __DRI_IMAGE_FORMAT_NONE
);
545 assert(BITSET_TEST(dri2_dpy
->formats
,
546 dri2_wl_visual_idx_from_dri_image_format(linear_dri_image_format
)));
548 /* There might be a buffer release already queued that wasn't processed */
549 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
551 while (dri2_surf
->back
== NULL
) {
552 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
553 /* Get an unlocked buffer, preferably one with a dri_buffer
554 * already allocated. */
555 if (dri2_surf
->color_buffers
[i
].locked
)
557 if (dri2_surf
->back
== NULL
)
558 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
559 else if (dri2_surf
->back
->dri_image
== NULL
)
560 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
566 /* If we don't have a buffer, then block on the server to release one for
567 * us, and try again. wl_display_dispatch_queue will process any pending
568 * events, however not all servers flush on issuing a buffer release
569 * event. So, we spam the server with roundtrips as they always cause a
572 if (wl_display_roundtrip_queue(dri2_dpy
->wl_dpy
,
573 dri2_surf
->wl_queue
) < 0)
577 if (dri2_surf
->back
== NULL
)
580 use_flags
= __DRI_IMAGE_USE_SHARE
| __DRI_IMAGE_USE_BACKBUFFER
;
582 if (dri2_dpy
->is_different_gpu
&&
583 dri2_surf
->back
->linear_copy
== NULL
) {
584 /* The LINEAR modifier should be a perfect alias of the LINEAR use
585 * flag; try the new interface first before the old, then fall back. */
586 if (dri2_dpy
->image
->base
.version
>= 15 &&
587 dri2_dpy
->image
->createImageWithModifiers
) {
588 uint64_t linear_mod
= DRM_FORMAT_MOD_LINEAR
;
590 dri2_surf
->back
->linear_copy
=
591 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
592 dri2_surf
->base
.Width
,
593 dri2_surf
->base
.Height
,
594 linear_dri_image_format
,
599 dri2_surf
->back
->linear_copy
=
600 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
601 dri2_surf
->base
.Width
,
602 dri2_surf
->base
.Height
,
603 linear_dri_image_format
,
605 __DRI_IMAGE_USE_LINEAR
,
608 if (dri2_surf
->back
->linear_copy
== NULL
)
612 if (dri2_surf
->back
->dri_image
== NULL
) {
613 /* If our DRIImage implementation does not support
614 * createImageWithModifiers, then fall back to the old createImage,
615 * and hope it allocates an image which is acceptable to the winsys.
617 if (num_modifiers
&& dri2_dpy
->image
->base
.version
>= 15 &&
618 dri2_dpy
->image
->createImageWithModifiers
) {
619 dri2_surf
->back
->dri_image
=
620 dri2_dpy
->image
->createImageWithModifiers(dri2_dpy
->dri_screen
,
621 dri2_surf
->base
.Width
,
622 dri2_surf
->base
.Height
,
628 dri2_surf
->back
->dri_image
=
629 dri2_dpy
->image
->createImage(dri2_dpy
->dri_screen
,
630 dri2_surf
->base
.Width
,
631 dri2_surf
->base
.Height
,
633 dri2_dpy
->is_different_gpu
?
638 dri2_surf
->back
->age
= 0;
640 if (dri2_surf
->back
->dri_image
== NULL
)
643 dri2_surf
->back
->locked
= true;
650 back_bo_to_dri_buffer(struct dri2_egl_surface
*dri2_surf
, __DRIbuffer
*buffer
)
652 struct dri2_egl_display
*dri2_dpy
=
653 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
657 image
= dri2_surf
->back
->dri_image
;
659 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
660 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &pitch
);
662 buffer
->attachment
= __DRI_BUFFER_BACK_LEFT
;
664 buffer
->pitch
= pitch
;
670 update_buffers(struct dri2_egl_surface
*dri2_surf
)
672 struct dri2_egl_display
*dri2_dpy
=
673 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
675 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
676 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
678 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
679 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
680 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
681 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
684 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->attached_width
||
685 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->attached_height
) {
686 dri2_wl_release_buffers(dri2_surf
);
689 if (get_back_bo(dri2_surf
) < 0) {
690 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
694 /* If we have an extra unlocked buffer at this point, we had to do triple
695 * buffering for a while, but now can go back to just double buffering.
696 * That means we can free any unlocked buffer now. */
697 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
698 if (!dri2_surf
->color_buffers
[i
].locked
&&
699 dri2_surf
->color_buffers
[i
].wl_buffer
) {
700 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
701 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].dri_image
);
702 if (dri2_dpy
->is_different_gpu
)
703 dri2_dpy
->image
->destroyImage(dri2_surf
->color_buffers
[i
].linear_copy
);
704 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
705 dri2_surf
->color_buffers
[i
].dri_image
= NULL
;
706 dri2_surf
->color_buffers
[i
].linear_copy
= NULL
;
714 update_buffers_if_needed(struct dri2_egl_surface
*dri2_surf
)
716 if (dri2_surf
->back
!= NULL
)
719 return update_buffers(dri2_surf
);
723 dri2_wl_get_buffers_with_format(__DRIdrawable
* driDrawable
,
724 int *width
, int *height
,
725 unsigned int *attachments
, int count
,
726 int *out_count
, void *loaderPrivate
)
728 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
731 if (update_buffers(dri2_surf
) < 0)
734 for (i
= 0, j
= 0; i
< 2 * count
; i
+= 2, j
++) {
737 switch (attachments
[i
]) {
738 case __DRI_BUFFER_BACK_LEFT
:
739 back_bo_to_dri_buffer(dri2_surf
, &dri2_surf
->buffers
[j
]);
742 local
= dri2_egl_surface_alloc_local_buffer(dri2_surf
, attachments
[i
],
746 _eglError(EGL_BAD_ALLOC
, "failed to allocate local buffer");
749 dri2_surf
->buffers
[j
] = *local
;
758 *width
= dri2_surf
->base
.Width
;
759 *height
= dri2_surf
->base
.Height
;
761 return dri2_surf
->buffers
;
765 dri2_wl_get_buffers(__DRIdrawable
* driDrawable
,
766 int *width
, int *height
,
767 unsigned int *attachments
, int count
,
768 int *out_count
, void *loaderPrivate
)
770 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
771 unsigned int *attachments_with_format
;
773 int visual_idx
= dri2_wl_visual_idx_from_fourcc(dri2_surf
->format
);
775 if (visual_idx
== -1)
778 attachments_with_format
= calloc(count
, 2 * sizeof(unsigned int));
779 if (!attachments_with_format
) {
784 for (int i
= 0; i
< count
; ++i
) {
785 attachments_with_format
[2*i
] = attachments
[i
];
786 attachments_with_format
[2*i
+ 1] = dri2_wl_visuals
[visual_idx
].bpp
;
790 dri2_wl_get_buffers_with_format(driDrawable
,
792 attachments_with_format
, count
,
793 out_count
, loaderPrivate
);
795 free(attachments_with_format
);
801 image_get_buffers(__DRIdrawable
*driDrawable
,
805 uint32_t buffer_mask
,
806 struct __DRIimageList
*buffers
)
808 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
810 if (update_buffers(dri2_surf
) < 0)
813 buffers
->image_mask
= __DRI_IMAGE_BUFFER_BACK
;
814 buffers
->back
= dri2_surf
->back
->dri_image
;
820 dri2_wl_flush_front_buffer(__DRIdrawable
* driDrawable
, void *loaderPrivate
)
823 (void) loaderPrivate
;
827 dri2_wl_get_capability(void *loaderPrivate
, enum dri_loader_cap cap
)
830 case DRI_LOADER_CAP_FP16
:
837 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
838 .base
= { __DRI_DRI2_LOADER
, 4 },
840 .getBuffers
= dri2_wl_get_buffers
,
841 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
842 .getBuffersWithFormat
= dri2_wl_get_buffers_with_format
,
843 .getCapability
= dri2_wl_get_capability
,
846 static const __DRIimageLoaderExtension image_loader_extension
= {
847 .base
= { __DRI_IMAGE_LOADER
, 2 },
849 .getBuffers
= image_get_buffers
,
850 .flushFrontBuffer
= dri2_wl_flush_front_buffer
,
851 .getCapability
= dri2_wl_get_capability
,
855 wayland_throttle_callback(void *data
,
856 struct wl_callback
*callback
,
859 struct dri2_egl_surface
*dri2_surf
= data
;
861 dri2_surf
->throttle_callback
= NULL
;
862 wl_callback_destroy(callback
);
865 static const struct wl_callback_listener throttle_listener
= {
866 .done
= wayland_throttle_callback
870 get_fourcc(struct dri2_egl_display
*dri2_dpy
,
871 __DRIimage
*image
, int *fourcc
)
877 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FOURCC
,
882 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
,
887 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(dri_format
);
888 if (visual_idx
== -1)
891 *fourcc
= dri2_wl_visuals
[visual_idx
].wl_drm_format
;
895 static struct wl_buffer
*
896 create_wl_buffer(struct dri2_egl_display
*dri2_dpy
,
897 struct dri2_egl_surface
*dri2_surf
,
900 struct wl_buffer
*ret
;
902 int width
, height
, fourcc
, num_planes
;
903 uint64_t modifier
= DRM_FORMAT_MOD_INVALID
;
905 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_WIDTH
, &width
);
906 query
&= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
908 query
&= get_fourcc(dri2_dpy
, image
, &fourcc
);
912 query
= dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NUM_PLANES
,
917 if (dri2_dpy
->image
->base
.version
>= 15) {
920 query
= dri2_dpy
->image
->queryImage(image
,
921 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER
,
923 query
&= dri2_dpy
->image
->queryImage(image
,
924 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER
,
927 modifier
= combine_u32_into_u64(mod_hi
, mod_lo
);
931 bool supported_modifier
= false;
932 if (modifier
!= DRM_FORMAT_MOD_INVALID
) {
933 supported_modifier
= true;
935 int visual_idx
= dri2_wl_visual_idx_from_fourcc(fourcc
);
936 assert(visual_idx
!= -1);
939 u_vector_foreach(mod
, &dri2_dpy
->wl_modifiers
[visual_idx
]) {
940 if (*mod
== DRM_FORMAT_MOD_INVALID
) {
941 supported_modifier
= true;
947 if (dri2_dpy
->wl_dmabuf
&& supported_modifier
) {
948 struct zwp_linux_buffer_params_v1
*params
;
951 /* We don't need a wrapper for wl_dmabuf objects, because we have to
952 * create the intermediate params object; we can set the queue on this,
953 * and the wl_buffer inherits it race-free. */
954 params
= zwp_linux_dmabuf_v1_create_params(dri2_dpy
->wl_dmabuf
);
956 wl_proxy_set_queue((struct wl_proxy
*) params
, dri2_surf
->wl_queue
);
958 for (i
= 0; i
< num_planes
; i
++) {
963 p_image
= dri2_dpy
->image
->fromPlanar(image
, i
, NULL
);
969 query
= dri2_dpy
->image
->queryImage(p_image
,
970 __DRI_IMAGE_ATTRIB_FD
,
972 query
&= dri2_dpy
->image
->queryImage(p_image
,
973 __DRI_IMAGE_ATTRIB_STRIDE
,
975 query
&= dri2_dpy
->image
->queryImage(p_image
,
976 __DRI_IMAGE_ATTRIB_OFFSET
,
978 if (image
!= p_image
)
979 dri2_dpy
->image
->destroyImage(p_image
);
984 zwp_linux_buffer_params_v1_destroy(params
);
988 zwp_linux_buffer_params_v1_add(params
, fd
, i
, offset
, stride
,
989 modifier
>> 32, modifier
& 0xffffffff);
993 ret
= zwp_linux_buffer_params_v1_create_immed(params
, width
, height
,
995 zwp_linux_buffer_params_v1_destroy(params
);
996 } else if (dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
) {
997 struct wl_drm
*wl_drm
=
998 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
1004 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
1005 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
1006 ret
= wl_drm_create_prime_buffer(wl_drm
, fd
, width
, height
, fourcc
, 0,
1007 stride
, 0, 0, 0, 0);
1010 struct wl_drm
*wl_drm
=
1011 dri2_surf
? dri2_surf
->wl_drm_wrapper
: dri2_dpy
->wl_drm
;
1017 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_NAME
, &name
);
1018 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_STRIDE
, &stride
);
1019 ret
= wl_drm_create_buffer(wl_drm
, name
, width
, height
, stride
, fourcc
);
1026 try_damage_buffer(struct dri2_egl_surface
*dri2_surf
,
1027 const EGLint
*rects
,
1030 if (wl_proxy_get_version((struct wl_proxy
*) dri2_surf
->wl_surface_wrapper
)
1031 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
)
1034 for (int i
= 0; i
< n_rects
; i
++) {
1035 const int *rect
= &rects
[i
* 4];
1037 wl_surface_damage_buffer(dri2_surf
->wl_surface_wrapper
,
1039 dri2_surf
->base
.Height
- rect
[1] - rect
[3],
1045 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
1048 dri2_wl_swap_buffers_with_damage(_EGLDriver
*drv
,
1051 const EGLint
*rects
,
1054 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1055 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1057 while (dri2_surf
->throttle_callback
!= NULL
)
1058 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1059 dri2_surf
->wl_queue
) == -1)
1062 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++)
1063 if (dri2_surf
->color_buffers
[i
].age
> 0)
1064 dri2_surf
->color_buffers
[i
].age
++;
1066 /* Make sure we have a back buffer in case we're swapping without ever
1068 if (update_buffers_if_needed(dri2_surf
) < 0)
1069 return _eglError(EGL_BAD_ALLOC
, "dri2_swap_buffers");
1071 if (draw
->SwapInterval
> 0) {
1072 dri2_surf
->throttle_callback
=
1073 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1074 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1075 &throttle_listener
, dri2_surf
);
1078 dri2_surf
->back
->age
= 1;
1079 dri2_surf
->current
= dri2_surf
->back
;
1080 dri2_surf
->back
= NULL
;
1082 if (!dri2_surf
->current
->wl_buffer
) {
1085 if (dri2_dpy
->is_different_gpu
)
1086 image
= dri2_surf
->current
->linear_copy
;
1088 image
= dri2_surf
->current
->dri_image
;
1090 dri2_surf
->current
->wl_buffer
=
1091 create_wl_buffer(dri2_dpy
, dri2_surf
, image
);
1093 dri2_surf
->current
->wl_release
= false;
1095 wl_buffer_add_listener(dri2_surf
->current
->wl_buffer
,
1096 &wl_buffer_listener
, dri2_surf
);
1099 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1100 dri2_surf
->current
->wl_buffer
,
1101 dri2_surf
->dx
, dri2_surf
->dy
);
1103 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1104 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1105 /* reset resize growing parameters */
1109 /* If the compositor doesn't support damage_buffer, we deliberately
1110 * ignore the damage region and post maximum damage, due to
1111 * https://bugs.freedesktop.org/78190 */
1112 if (!n_rects
|| !try_damage_buffer(dri2_surf
, rects
, n_rects
))
1113 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1114 0, 0, INT32_MAX
, INT32_MAX
);
1116 if (dri2_dpy
->is_different_gpu
) {
1117 _EGLContext
*ctx
= _eglGetCurrentContext();
1118 struct dri2_egl_context
*dri2_ctx
= dri2_egl_context(ctx
);
1119 dri2_dpy
->image
->blitImage(dri2_ctx
->dri_context
,
1120 dri2_surf
->current
->linear_copy
,
1121 dri2_surf
->current
->dri_image
,
1122 0, 0, dri2_surf
->base
.Width
,
1123 dri2_surf
->base
.Height
,
1124 0, 0, dri2_surf
->base
.Width
,
1125 dri2_surf
->base
.Height
, 0);
1128 dri2_flush_drawable_for_swapbuffers(disp
, draw
);
1129 dri2_dpy
->flush
->invalidate(dri2_surf
->dri_drawable
);
1131 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1133 /* If we're not waiting for a frame callback then we'll at least throttle
1134 * to a sync callback so that we always give a chance for the compositor to
1135 * handle the commit and send a release event before checking for a free
1137 if (dri2_surf
->throttle_callback
== NULL
) {
1138 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1139 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1140 &throttle_listener
, dri2_surf
);
1143 wl_display_flush(dri2_dpy
->wl_dpy
);
1149 dri2_wl_query_buffer_age(_EGLDriver
*drv
,
1150 _EGLDisplay
*disp
, _EGLSurface
*surface
)
1152 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surface
);
1154 if (update_buffers_if_needed(dri2_surf
) < 0) {
1155 _eglError(EGL_BAD_ALLOC
, "dri2_query_buffer_age");
1159 return dri2_surf
->back
->age
;
1163 dri2_wl_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1165 return dri2_wl_swap_buffers_with_damage(drv
, disp
, draw
, NULL
, 0);
1168 static struct wl_buffer
*
1169 dri2_wl_create_wayland_buffer_from_image(_EGLDriver
*drv
,
1173 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1174 struct dri2_egl_image
*dri2_img
= dri2_egl_image(img
);
1175 __DRIimage
*image
= dri2_img
->dri_image
;
1176 struct wl_buffer
*buffer
;
1177 int format
, visual_idx
;
1179 /* Check the upstream display supports this buffer's format. */
1180 dri2_dpy
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &format
);
1181 visual_idx
= dri2_wl_visual_idx_from_dri_image_format(format
);
1182 if (visual_idx
== -1)
1185 if (!BITSET_TEST(dri2_dpy
->formats
, visual_idx
))
1188 buffer
= create_wl_buffer(dri2_dpy
, NULL
, image
);
1190 /* The buffer object will have been created with our internal event queue
1191 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1192 * buffer to be used by the application so we'll reset it to the display's
1193 * default event queue. This isn't actually racy, as the only event the
1194 * buffer can get is a buffer release, which doesn't happen with an explicit
1197 wl_proxy_set_queue((struct wl_proxy
*) buffer
, NULL
);
1202 _eglError(EGL_BAD_MATCH
, "unsupported image format");
1207 dri2_wl_authenticate(_EGLDisplay
*disp
, uint32_t id
)
1209 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1212 if (dri2_dpy
->is_render_node
) {
1213 _eglLog(_EGL_WARNING
, "wayland-egl: client asks server to "
1214 "authenticate for render-nodes");
1217 dri2_dpy
->authenticated
= false;
1219 wl_drm_authenticate(dri2_dpy
->wl_drm
, id
);
1220 if (roundtrip(dri2_dpy
) < 0)
1223 if (!dri2_dpy
->authenticated
)
1226 /* reset authenticated */
1227 dri2_dpy
->authenticated
= true;
1233 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *device
)
1235 struct dri2_egl_display
*dri2_dpy
= data
;
1238 dri2_dpy
->device_name
= strdup(device
);
1239 if (!dri2_dpy
->device_name
)
1242 dri2_dpy
->fd
= loader_open_device(dri2_dpy
->device_name
);
1243 if (dri2_dpy
->fd
== -1) {
1244 _eglLog(_EGL_WARNING
, "wayland-egl: could not open %s (%s)",
1245 dri2_dpy
->device_name
, strerror(errno
));
1246 free(dri2_dpy
->device_name
);
1247 dri2_dpy
->device_name
= NULL
;
1251 if (drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
) {
1252 dri2_dpy
->authenticated
= true;
1254 if (drmGetMagic(dri2_dpy
->fd
, &magic
)) {
1255 close(dri2_dpy
->fd
);
1257 free(dri2_dpy
->device_name
);
1258 dri2_dpy
->device_name
= NULL
;
1259 _eglLog(_EGL_WARNING
, "wayland-egl: drmGetMagic failed");
1262 wl_drm_authenticate(dri2_dpy
->wl_drm
, magic
);
1267 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t format
)
1269 struct dri2_egl_display
*dri2_dpy
= data
;
1270 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1272 if (visual_idx
== -1)
1275 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1279 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t value
)
1281 struct dri2_egl_display
*dri2_dpy
= data
;
1283 dri2_dpy
->capabilities
= value
;
1287 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
1289 struct dri2_egl_display
*dri2_dpy
= data
;
1291 dri2_dpy
->authenticated
= true;
1294 static const struct wl_drm_listener drm_listener
= {
1295 .device
= drm_handle_device
,
1296 .format
= drm_handle_format
,
1297 .authenticated
= drm_handle_authenticated
,
1298 .capabilities
= drm_handle_capabilities
1302 dmabuf_ignore_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1305 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1309 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
1310 uint32_t format
, uint32_t modifier_hi
,
1311 uint32_t modifier_lo
)
1313 struct dri2_egl_display
*dri2_dpy
= data
;
1314 int visual_idx
= dri2_wl_visual_idx_from_fourcc(format
);
1317 if (visual_idx
== -1)
1320 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1322 mod
= u_vector_add(&dri2_dpy
->wl_modifiers
[visual_idx
]);
1323 *mod
= combine_u32_into_u64(modifier_hi
, modifier_lo
);
1326 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
1327 .format
= dmabuf_ignore_format
,
1328 .modifier
= dmabuf_handle_modifier
,
1332 registry_handle_global_drm(void *data
, struct wl_registry
*registry
,
1333 uint32_t name
, const char *interface
,
1336 struct dri2_egl_display
*dri2_dpy
= data
;
1338 if (strcmp(interface
, "wl_drm") == 0) {
1340 wl_registry_bind(registry
, name
, &wl_drm_interface
, MIN2(version
, 2));
1341 wl_drm_add_listener(dri2_dpy
->wl_drm
, &drm_listener
, dri2_dpy
);
1342 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
1343 dri2_dpy
->wl_dmabuf
=
1344 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
,
1346 zwp_linux_dmabuf_v1_add_listener(dri2_dpy
->wl_dmabuf
, &dmabuf_listener
,
1352 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
1357 static const struct wl_registry_listener registry_listener_drm
= {
1358 .global
= registry_handle_global_drm
,
1359 .global_remove
= registry_handle_global_remove
1363 dri2_wl_setup_swap_interval(_EGLDisplay
*disp
)
1365 /* We can't use values greater than 1 on Wayland because we are using the
1366 * frame callback to synchronise the frame and the only way we be sure to
1367 * get a frame callback is to attach a new buffer. Therefore we can't just
1368 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1370 dri2_setup_swap_interval(disp
, 1);
1373 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl
= {
1374 .authenticate
= dri2_wl_authenticate
,
1375 .create_window_surface
= dri2_wl_create_window_surface
,
1376 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1377 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1378 .destroy_surface
= dri2_wl_destroy_surface
,
1379 .create_image
= dri2_create_image_khr
,
1380 .swap_buffers
= dri2_wl_swap_buffers
,
1381 .swap_buffers_with_damage
= dri2_wl_swap_buffers_with_damage
,
1382 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
1383 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
1384 .copy_buffers
= dri2_fallback_copy_buffers
,
1385 .query_buffer_age
= dri2_wl_query_buffer_age
,
1386 .create_wayland_buffer_from_image
= dri2_wl_create_wayland_buffer_from_image
,
1387 .get_sync_values
= dri2_fallback_get_sync_values
,
1388 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
1391 static const __DRIextension
*dri2_loader_extensions
[] = {
1392 &dri2_loader_extension
.base
,
1393 &image_loader_extension
.base
,
1394 &image_lookup_extension
.base
,
1395 &use_invalidate
.base
,
1399 static const __DRIextension
*image_loader_extensions
[] = {
1400 &image_loader_extension
.base
,
1401 &image_lookup_extension
.base
,
1402 &use_invalidate
.base
,
1407 dri2_wl_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1409 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1410 unsigned int format_count
[ARRAY_SIZE(dri2_wl_visuals
)] = { 0 };
1411 unsigned int count
= 0;
1414 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
]; i
++) {
1417 for (unsigned j
= 0; j
< ARRAY_SIZE(dri2_wl_visuals
); j
++) {
1418 struct dri2_egl_config
*dri2_conf
;
1420 if (!BITSET_TEST(dri2_dpy
->formats
, j
))
1423 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1424 count
+ 1, EGL_WINDOW_BIT
, NULL
, dri2_wl_visuals
[j
].rgba_shifts
, dri2_wl_visuals
[j
].rgba_sizes
);
1426 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1433 if (!assigned
&& dri2_dpy
->is_different_gpu
) {
1434 struct dri2_egl_config
*dri2_conf
;
1435 int alt_dri_image_format
, c
, s
;
1437 /* No match for config. Try if we can blitImage convert to a visual */
1438 c
= dri2_wl_visual_idx_from_config(dri2_dpy
,
1439 dri2_dpy
->driver_configs
[i
]);
1444 /* Find optimal target visual for blitImage conversion, if any. */
1445 alt_dri_image_format
= dri2_wl_visuals
[c
].alt_dri_image_format
;
1446 s
= dri2_wl_visual_idx_from_dri_image_format(alt_dri_image_format
);
1448 if (s
== -1 || !BITSET_TEST(dri2_dpy
->formats
, s
))
1451 /* Visual s works for the Wayland server, and c can be converted into s
1452 * by our client gpu during PRIME blitImage conversion to a linear
1453 * wl_buffer, so add visual c as supported by the client renderer.
1455 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
1456 count
+ 1, EGL_WINDOW_BIT
, NULL
,
1457 dri2_wl_visuals
[c
].rgba_shifts
,
1458 dri2_wl_visuals
[c
].rgba_sizes
);
1460 if (dri2_conf
->base
.ConfigID
== count
+ 1)
1463 if (format_count
[c
] == 1)
1464 _eglLog(_EGL_DEBUG
, "Client format %s to server format %s via "
1465 "PRIME blitImage.", dri2_wl_visuals
[c
].format_name
,
1466 dri2_wl_visuals
[s
].format_name
);
1471 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
1472 if (!format_count
[i
]) {
1473 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
1474 dri2_wl_visuals
[i
].format_name
);
1478 return (count
!= 0);
1482 dri2_initialize_wayland_drm(_EGLDriver
*drv
, _EGLDisplay
*disp
)
1485 struct dri2_egl_display
*dri2_dpy
;
1487 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
1489 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
1492 disp
->DriverData
= (void *) dri2_dpy
;
1493 if (disp
->PlatformDisplay
== NULL
) {
1494 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
1495 if (dri2_dpy
->wl_dpy
== NULL
)
1497 dri2_dpy
->own_device
= true;
1499 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
1502 dri2_dpy
->wl_modifiers
=
1503 calloc(ARRAY_SIZE(dri2_wl_visuals
), sizeof(*dri2_dpy
->wl_modifiers
));
1504 if (!dri2_dpy
->wl_modifiers
)
1506 for (int i
= 0; i
< ARRAY_SIZE(dri2_wl_visuals
); i
++) {
1507 if (!u_vector_init(&dri2_dpy
->wl_modifiers
[i
], sizeof(uint64_t), 32))
1511 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
1513 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
1514 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
1517 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
1518 dri2_dpy
->wl_queue
);
1520 if (dri2_dpy
->own_device
)
1521 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
1523 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
1524 wl_registry_add_listener(dri2_dpy
->wl_registry
,
1525 ®istry_listener_drm
, dri2_dpy
);
1526 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_drm
== NULL
)
1529 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->fd
== -1)
1532 if (roundtrip(dri2_dpy
) < 0 || !dri2_dpy
->authenticated
)
1535 dri2_dpy
->fd
= loader_get_user_preferred_fd(dri2_dpy
->fd
,
1536 &dri2_dpy
->is_different_gpu
);
1537 dev
= _eglAddDevice(dri2_dpy
->fd
, false);
1539 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
1545 if (dri2_dpy
->is_different_gpu
) {
1546 free(dri2_dpy
->device_name
);
1547 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
1548 if (!dri2_dpy
->device_name
) {
1549 _eglError(EGL_BAD_ALLOC
, "wayland-egl: failed to get device name "
1550 "for requested GPU");
1555 /* we have to do the check now, because loader_get_user_preferred_fd
1556 * will return a render-node when the requested gpu is different
1557 * to the server, but also if the client asks for the same gpu than
1558 * the server by requesting its pci-id */
1559 dri2_dpy
->is_render_node
= drmGetNodeTypeFromFd(dri2_dpy
->fd
) == DRM_NODE_RENDER
;
1561 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
1562 if (dri2_dpy
->driver_name
== NULL
) {
1563 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to get driver name");
1567 /* render nodes cannot use Gem names, and thus do not support
1568 * the __DRI_DRI2_LOADER extension */
1569 if (!dri2_dpy
->is_render_node
) {
1570 dri2_dpy
->loader_extensions
= dri2_loader_extensions
;
1571 if (!dri2_load_driver(disp
)) {
1572 _eglError(EGL_BAD_ALLOC
, "DRI2: failed to load driver");
1576 dri2_dpy
->loader_extensions
= image_loader_extensions
;
1577 if (!dri2_load_driver_dri3(disp
)) {
1578 _eglError(EGL_BAD_ALLOC
, "DRI3: failed to load driver");
1583 if (!dri2_create_screen(disp
))
1586 if (!dri2_setup_extensions(disp
))
1589 dri2_setup_screen(disp
);
1591 dri2_wl_setup_swap_interval(disp
);
1593 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1594 * createImageFromFds support indicates that Prime export/import
1595 * is supported by the driver. Fall back to
1596 * gem names if we don't have Prime support. */
1598 if (dri2_dpy
->image
->base
.version
< 7 ||
1599 dri2_dpy
->image
->createImageFromFds
== NULL
)
1600 dri2_dpy
->capabilities
&= ~WL_DRM_CAPABILITY_PRIME
;
1602 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1603 * The server needs to accept them */
1604 if (dri2_dpy
->is_render_node
&&
1605 !(dri2_dpy
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
1606 _eglLog(_EGL_WARNING
, "wayland-egl: display is not render-node capable");
1610 if (dri2_dpy
->is_different_gpu
&&
1611 (dri2_dpy
->image
->base
.version
< 9 ||
1612 dri2_dpy
->image
->blitImage
== NULL
)) {
1613 _eglLog(_EGL_WARNING
, "wayland-egl: Different GPU selected, but the "
1614 "Image extension in the driver is not "
1615 "compatible. Version 9 or later and blitImage() "
1620 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
1621 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
1625 dri2_set_WL_bind_wayland_display(drv
, disp
);
1626 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1627 * because the buffer of the EGLImage has likely a tiling mode the server
1628 * gpu won't support. These is no way to check for now. Thus do not support the
1630 if (!dri2_dpy
->is_different_gpu
)
1631 disp
->Extensions
.WL_create_wayland_buffer_from_image
= EGL_TRUE
;
1633 disp
->Extensions
.EXT_buffer_age
= EGL_TRUE
;
1635 disp
->Extensions
.EXT_swap_buffers_with_damage
= EGL_TRUE
;
1637 /* Fill vtbl last to prevent accidentally calling virtual function during
1640 dri2_dpy
->vtbl
= &dri2_wl_display_vtbl
;
1645 dri2_display_destroy(disp
);
1650 dri2_wl_swrast_get_stride_for_format(int format
, int w
)
1652 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1654 assume(visual_idx
!= -1);
1656 return w
* (dri2_wl_visuals
[visual_idx
].bpp
/ 8);
1660 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface
*dri2_surf
,
1661 int format
, int w
, int h
,
1662 void **data
, int *size
,
1663 struct wl_buffer
**buffer
)
1665 struct dri2_egl_display
*dri2_dpy
=
1666 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1667 struct wl_shm_pool
*pool
;
1668 int fd
, stride
, size_map
;
1671 stride
= dri2_wl_swrast_get_stride_for_format(format
, w
);
1672 size_map
= h
* stride
;
1674 /* Create a shareable buffer */
1675 fd
= os_create_anonymous_file(size_map
, NULL
);
1679 data_map
= mmap(NULL
, size_map
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
1680 if (data_map
== MAP_FAILED
) {
1685 /* Share it in a wl_buffer */
1686 pool
= wl_shm_create_pool(dri2_dpy
->wl_shm
, fd
, size_map
);
1687 wl_proxy_set_queue((struct wl_proxy
*)pool
, dri2_surf
->wl_queue
);
1688 *buffer
= wl_shm_pool_create_buffer(pool
, 0, w
, h
, stride
, format
);
1689 wl_shm_pool_destroy(pool
);
1698 swrast_update_buffers(struct dri2_egl_surface
*dri2_surf
)
1700 struct dri2_egl_display
*dri2_dpy
=
1701 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1703 /* we need to do the following operations only once per frame */
1704 if (dri2_surf
->back
)
1707 if (dri2_surf
->base
.Width
!= dri2_surf
->wl_win
->width
||
1708 dri2_surf
->base
.Height
!= dri2_surf
->wl_win
->height
) {
1710 dri2_wl_release_buffers(dri2_surf
);
1712 dri2_surf
->base
.Width
= dri2_surf
->wl_win
->width
;
1713 dri2_surf
->base
.Height
= dri2_surf
->wl_win
->height
;
1714 dri2_surf
->dx
= dri2_surf
->wl_win
->dx
;
1715 dri2_surf
->dy
= dri2_surf
->wl_win
->dy
;
1716 dri2_surf
->current
= NULL
;
1719 /* find back buffer */
1721 /* There might be a buffer release already queued that wasn't processed */
1722 wl_display_dispatch_queue_pending(dri2_dpy
->wl_dpy
, dri2_surf
->wl_queue
);
1724 /* try get free buffer already created */
1725 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1726 if (!dri2_surf
->color_buffers
[i
].locked
&&
1727 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1728 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1733 /* else choose any another free location */
1734 if (!dri2_surf
->back
) {
1735 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1736 if (!dri2_surf
->color_buffers
[i
].locked
) {
1737 dri2_surf
->back
= &dri2_surf
->color_buffers
[i
];
1738 if (!dri2_wl_swrast_allocate_buffer(dri2_surf
,
1740 dri2_surf
->base
.Width
,
1741 dri2_surf
->base
.Height
,
1742 &dri2_surf
->back
->data
,
1743 &dri2_surf
->back
->data_size
,
1744 &dri2_surf
->back
->wl_buffer
)) {
1745 _eglError(EGL_BAD_ALLOC
, "failed to allocate color buffer");
1748 wl_buffer_add_listener(dri2_surf
->back
->wl_buffer
,
1749 &wl_buffer_listener
, dri2_surf
);
1755 if (!dri2_surf
->back
) {
1756 _eglError(EGL_BAD_ALLOC
, "failed to find free buffer");
1760 dri2_surf
->back
->locked
= true;
1762 /* If we have an extra unlocked buffer at this point, we had to do triple
1763 * buffering for a while, but now can go back to just double buffering.
1764 * That means we can free any unlocked buffer now. */
1765 for (int i
= 0; i
< ARRAY_SIZE(dri2_surf
->color_buffers
); i
++) {
1766 if (!dri2_surf
->color_buffers
[i
].locked
&&
1767 dri2_surf
->color_buffers
[i
].wl_buffer
) {
1768 wl_buffer_destroy(dri2_surf
->color_buffers
[i
].wl_buffer
);
1769 munmap(dri2_surf
->color_buffers
[i
].data
,
1770 dri2_surf
->color_buffers
[i
].data_size
);
1771 dri2_surf
->color_buffers
[i
].wl_buffer
= NULL
;
1772 dri2_surf
->color_buffers
[i
].data
= NULL
;
1780 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1782 /* if there has been a resize: */
1783 if (!dri2_surf
->current
)
1786 return dri2_surf
->current
->data
;
1790 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface
*dri2_surf
)
1792 assert(dri2_surf
->back
);
1793 return dri2_surf
->back
->data
;
1797 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface
*dri2_surf
)
1799 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
1801 while (dri2_surf
->throttle_callback
!= NULL
)
1802 if (wl_display_dispatch_queue(dri2_dpy
->wl_dpy
,
1803 dri2_surf
->wl_queue
) == -1)
1806 if (dri2_surf
->base
.SwapInterval
> 0) {
1807 dri2_surf
->throttle_callback
=
1808 wl_surface_frame(dri2_surf
->wl_surface_wrapper
);
1809 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1810 &throttle_listener
, dri2_surf
);
1813 dri2_surf
->current
= dri2_surf
->back
;
1814 dri2_surf
->back
= NULL
;
1816 wl_surface_attach(dri2_surf
->wl_surface_wrapper
,
1817 dri2_surf
->current
->wl_buffer
,
1818 dri2_surf
->dx
, dri2_surf
->dy
);
1820 dri2_surf
->wl_win
->attached_width
= dri2_surf
->base
.Width
;
1821 dri2_surf
->wl_win
->attached_height
= dri2_surf
->base
.Height
;
1822 /* reset resize growing parameters */
1826 wl_surface_damage(dri2_surf
->wl_surface_wrapper
,
1827 0, 0, INT32_MAX
, INT32_MAX
);
1828 wl_surface_commit(dri2_surf
->wl_surface_wrapper
);
1830 /* If we're not waiting for a frame callback then we'll at least throttle
1831 * to a sync callback so that we always give a chance for the compositor to
1832 * handle the commit and send a release event before checking for a free
1834 if (dri2_surf
->throttle_callback
== NULL
) {
1835 dri2_surf
->throttle_callback
= wl_display_sync(dri2_surf
->wl_dpy_wrapper
);
1836 wl_callback_add_listener(dri2_surf
->throttle_callback
,
1837 &throttle_listener
, dri2_surf
);
1840 wl_display_flush(dri2_dpy
->wl_dpy
);
1844 dri2_wl_swrast_get_drawable_info(__DRIdrawable
* draw
,
1845 int *x
, int *y
, int *w
, int *h
,
1846 void *loaderPrivate
)
1848 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1850 (void) swrast_update_buffers(dri2_surf
);
1853 *w
= dri2_surf
->base
.Width
;
1854 *h
= dri2_surf
->base
.Height
;
1858 dri2_wl_swrast_get_image(__DRIdrawable
* read
,
1859 int x
, int y
, int w
, int h
,
1860 char *data
, void *loaderPrivate
)
1862 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1863 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1864 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1865 int src_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1866 int dst_stride
= copy_width
;
1869 src
= dri2_wl_swrast_get_frontbuffer_data(dri2_surf
);
1871 memset(data
, 0, copy_width
* h
);
1875 assert(data
!= src
);
1876 assert(copy_width
<= src_stride
);
1879 src
+= y
* src_stride
;
1882 if (copy_width
> src_stride
-x_offset
)
1883 copy_width
= src_stride
-x_offset
;
1884 if (h
> dri2_surf
->base
.Height
-y
)
1885 h
= dri2_surf
->base
.Height
-y
;
1888 memcpy(dst
, src
, copy_width
);
1895 dri2_wl_swrast_put_image2(__DRIdrawable
* draw
, int op
,
1896 int x
, int y
, int w
, int h
, int stride
,
1897 char *data
, void *loaderPrivate
)
1899 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1900 int copy_width
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1901 int dst_stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, dri2_surf
->base
.Width
);
1902 int x_offset
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, x
);
1905 assert(copy_width
<= stride
);
1907 (void) swrast_update_buffers(dri2_surf
);
1908 dst
= dri2_wl_swrast_get_backbuffer_data(dri2_surf
);
1910 /* partial copy, copy old content */
1911 if (copy_width
< dst_stride
)
1912 dri2_wl_swrast_get_image(draw
, 0, 0,
1913 dri2_surf
->base
.Width
, dri2_surf
->base
.Height
,
1914 dst
, loaderPrivate
);
1917 dst
+= y
* dst_stride
;
1921 /* drivers expect we do these checks (and some rely on it) */
1922 if (copy_width
> dst_stride
-x_offset
)
1923 copy_width
= dst_stride
-x_offset
;
1924 if (h
> dri2_surf
->base
.Height
-y
)
1925 h
= dri2_surf
->base
.Height
-y
;
1928 memcpy(dst
, src
, copy_width
);
1932 dri2_wl_swrast_commit_backbuffer(dri2_surf
);
1936 dri2_wl_swrast_put_image(__DRIdrawable
* draw
, int op
,
1937 int x
, int y
, int w
, int h
,
1938 char *data
, void *loaderPrivate
)
1940 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
1943 stride
= dri2_wl_swrast_get_stride_for_format(dri2_surf
->format
, w
);
1944 dri2_wl_swrast_put_image2(draw
, op
, x
, y
, w
, h
,
1945 stride
, data
, loaderPrivate
);
1949 dri2_wl_swrast_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
1951 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
1952 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(draw
);
1954 dri2_dpy
->core
->swapBuffers(dri2_surf
->dri_drawable
);
1959 shm_handle_format(void *data
, struct wl_shm
*shm
, uint32_t format
)
1961 struct dri2_egl_display
*dri2_dpy
= data
;
1962 int visual_idx
= dri2_wl_visual_idx_from_shm_format(format
);
1964 if (visual_idx
== -1)
1967 BITSET_SET(dri2_dpy
->formats
, visual_idx
);
1970 static const struct wl_shm_listener shm_listener
= {
1971 .format
= shm_handle_format
1975 registry_handle_global_swrast(void *data
, struct wl_registry
*registry
,
1976 uint32_t name
, const char *interface
,
1979 struct dri2_egl_display
*dri2_dpy
= data
;
1981 if (strcmp(interface
, "wl_shm") == 0) {
1983 wl_registry_bind(registry
, name
, &wl_shm_interface
, 1);
1984 wl_shm_add_listener(dri2_dpy
->wl_shm
, &shm_listener
, dri2_dpy
);
1988 static const struct wl_registry_listener registry_listener_swrast
= {
1989 .global
= registry_handle_global_swrast
,
1990 .global_remove
= registry_handle_global_remove
1993 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl
= {
1994 .authenticate
= NULL
,
1995 .create_window_surface
= dri2_wl_create_window_surface
,
1996 .create_pixmap_surface
= dri2_wl_create_pixmap_surface
,
1997 .create_pbuffer_surface
= dri2_fallback_create_pbuffer_surface
,
1998 .destroy_surface
= dri2_wl_destroy_surface
,
1999 .create_image
= dri2_create_image_khr
,
2000 .swap_buffers
= dri2_wl_swrast_swap_buffers
,
2001 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
2002 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
2003 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
2004 .copy_buffers
= dri2_fallback_copy_buffers
,
2005 .query_buffer_age
= dri2_fallback_query_buffer_age
,
2006 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
2007 .get_sync_values
= dri2_fallback_get_sync_values
,
2008 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
2011 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
2012 .base
= { __DRI_SWRAST_LOADER
, 2 },
2014 .getDrawableInfo
= dri2_wl_swrast_get_drawable_info
,
2015 .putImage
= dri2_wl_swrast_put_image
,
2016 .getImage
= dri2_wl_swrast_get_image
,
2017 .putImage2
= dri2_wl_swrast_put_image2
,
2020 static const __DRIextension
*swrast_loader_extensions
[] = {
2021 &swrast_loader_extension
.base
,
2022 &image_lookup_extension
.base
,
2027 dri2_initialize_wayland_swrast(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2030 struct dri2_egl_display
*dri2_dpy
;
2032 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
2034 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
2037 disp
->DriverData
= (void *) dri2_dpy
;
2038 if (disp
->PlatformDisplay
== NULL
) {
2039 dri2_dpy
->wl_dpy
= wl_display_connect(NULL
);
2040 if (dri2_dpy
->wl_dpy
== NULL
)
2042 dri2_dpy
->own_device
= true;
2044 dri2_dpy
->wl_dpy
= disp
->PlatformDisplay
;
2047 dev
= _eglAddDevice(dri2_dpy
->fd
, true);
2049 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to find EGLDevice");
2055 dri2_dpy
->wl_queue
= wl_display_create_queue(dri2_dpy
->wl_dpy
);
2057 dri2_dpy
->wl_dpy_wrapper
= wl_proxy_create_wrapper(dri2_dpy
->wl_dpy
);
2058 if (dri2_dpy
->wl_dpy_wrapper
== NULL
)
2061 wl_proxy_set_queue((struct wl_proxy
*) dri2_dpy
->wl_dpy_wrapper
,
2062 dri2_dpy
->wl_queue
);
2064 if (dri2_dpy
->own_device
)
2065 wl_display_dispatch_pending(dri2_dpy
->wl_dpy
);
2067 dri2_dpy
->wl_registry
= wl_display_get_registry(dri2_dpy
->wl_dpy_wrapper
);
2068 wl_registry_add_listener(dri2_dpy
->wl_registry
,
2069 ®istry_listener_swrast
, dri2_dpy
);
2071 if (roundtrip(dri2_dpy
) < 0 || dri2_dpy
->wl_shm
== NULL
)
2074 if (roundtrip(dri2_dpy
) < 0 || !BITSET_TEST_RANGE(dri2_dpy
->formats
,
2075 0, EGL_DRI2_MAX_FORMATS
))
2078 dri2_dpy
->driver_name
= strdup("swrast");
2079 if (!dri2_load_driver_swrast(disp
))
2082 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
2084 if (!dri2_create_screen(disp
))
2087 if (!dri2_setup_extensions(disp
))
2090 dri2_setup_screen(disp
);
2092 dri2_wl_setup_swap_interval(disp
);
2094 if (!dri2_wl_add_configs_for_visuals(drv
, disp
)) {
2095 _eglError(EGL_NOT_INITIALIZED
, "DRI2: failed to add configs");
2099 /* Fill vtbl last to prevent accidentally calling virtual function during
2102 dri2_dpy
->vtbl
= &dri2_wl_swrast_display_vtbl
;
2107 dri2_display_destroy(disp
);
2112 dri2_initialize_wayland(_EGLDriver
*drv
, _EGLDisplay
*disp
)
2114 EGLBoolean initialized
= EGL_FALSE
;
2116 if (!disp
->Options
.ForceSoftware
)
2117 initialized
= dri2_initialize_wayland_drm(drv
, disp
);
2120 initialized
= dri2_initialize_wayland_swrast(drv
, disp
);
2127 dri2_teardown_wayland(struct dri2_egl_display
*dri2_dpy
)
2129 if (dri2_dpy
->wl_drm
)
2130 wl_drm_destroy(dri2_dpy
->wl_drm
);
2131 if (dri2_dpy
->wl_dmabuf
)
2132 zwp_linux_dmabuf_v1_destroy(dri2_dpy
->wl_dmabuf
);
2133 if (dri2_dpy
->wl_shm
)
2134 wl_shm_destroy(dri2_dpy
->wl_shm
);
2135 if (dri2_dpy
->wl_registry
)
2136 wl_registry_destroy(dri2_dpy
->wl_registry
);
2137 if (dri2_dpy
->wl_queue
)
2138 wl_event_queue_destroy(dri2_dpy
->wl_queue
);
2139 if (dri2_dpy
->wl_dpy_wrapper
)
2140 wl_proxy_wrapper_destroy(dri2_dpy
->wl_dpy_wrapper
);
2142 for (int i
= 0; dri2_dpy
->wl_modifiers
&& i
< ARRAY_SIZE(dri2_wl_visuals
); i
++)
2143 u_vector_finish(&dri2_dpy
->wl_modifiers
[i
]);
2144 free(dri2_dpy
->wl_modifiers
);
2146 if (dri2_dpy
->own_device
)
2147 wl_display_disconnect(dri2_dpy
->wl_dpy
);