2 * Copyright © 2015 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, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include <wayland-client.h>
34 #include <drm_fourcc.h>
37 #include "wsi_common_private.h"
38 #include "wsi_common_wayland.h"
39 #include "wayland-drm-client-protocol.h"
40 #include "linux-dmabuf-unstable-v1-client-protocol.h"
42 #include <util/hash_table.h>
43 #include <util/u_vector.h>
45 #define typed_memcpy(dest, src, count) ({ \
46 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
47 memcpy((dest), (src), (count) * sizeof(*(src))); \
52 struct wsi_wl_display
{
53 /* The real wl_display */
54 struct wl_display
* wl_display
;
55 /* Actually a proxy wrapper around the event queue */
56 struct wl_display
* wl_display_wrapper
;
57 struct wl_event_queue
* queue
;
59 struct zwp_linux_dmabuf_v1
* dmabuf
;
61 struct wsi_wayland
*wsi_wl
;
62 /* Vector of VkFormats supported */
63 struct u_vector formats
;
66 struct u_vector argb8888
;
67 struct u_vector xrgb8888
;
70 uint32_t capabilities
;
72 /* Only used for displays created by wsi_wl_display_create */
77 struct wsi_interface base
;
79 struct wsi_device
*wsi
;
81 const VkAllocationCallbacks
*alloc
;
82 VkPhysicalDevice physical_device
;
86 wsi_wl_display_add_vk_format(struct wsi_wl_display
*display
, VkFormat format
)
88 /* Don't add a format that's already in the list */
90 u_vector_foreach(f
, &display
->formats
)
94 /* Don't add formats that aren't renderable. */
95 VkFormatProperties props
;
97 display
->wsi_wl
->wsi
->GetPhysicalDeviceFormatProperties(display
->wsi_wl
->physical_device
,
99 if (!(props
.optimalTilingFeatures
& VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
))
102 f
= u_vector_add(&display
->formats
);
108 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *name
)
113 wl_drm_format_for_vk_format(VkFormat vk_format
, bool alpha
)
116 /* TODO: Figure out what all the formats mean and make this table
120 case VK_FORMAT_R4G4B4A4_UNORM
:
121 return alpha
? WL_DRM_FORMAT_ABGR4444
: WL_DRM_FORMAT_XBGR4444
;
122 case VK_FORMAT_R5G6B5_UNORM
:
123 return WL_DRM_FORMAT_BGR565
;
124 case VK_FORMAT_R5G5B5A1_UNORM
:
125 return alpha
? WL_DRM_FORMAT_ABGR1555
: WL_DRM_FORMAT_XBGR1555
;
126 case VK_FORMAT_R8G8B8_UNORM
:
127 return WL_DRM_FORMAT_XBGR8888
;
128 case VK_FORMAT_R8G8B8A8_UNORM
:
129 return alpha
? WL_DRM_FORMAT_ABGR8888
: WL_DRM_FORMAT_XBGR8888
;
130 case VK_FORMAT_R10G10B10A2_UNORM
:
131 return alpha
? WL_DRM_FORMAT_ABGR2101010
: WL_DRM_FORMAT_XBGR2101010
;
132 case VK_FORMAT_B4G4R4A4_UNORM
:
133 return alpha
? WL_DRM_FORMAT_ARGB4444
: WL_DRM_FORMAT_XRGB4444
;
134 case VK_FORMAT_B5G6R5_UNORM
:
135 return WL_DRM_FORMAT_RGB565
;
136 case VK_FORMAT_B5G5R5A1_UNORM
:
137 return alpha
? WL_DRM_FORMAT_XRGB1555
: WL_DRM_FORMAT_XRGB1555
;
139 case VK_FORMAT_B8G8R8_UNORM
:
140 case VK_FORMAT_B8G8R8_SRGB
:
141 return WL_DRM_FORMAT_BGRX8888
;
142 case VK_FORMAT_B8G8R8A8_UNORM
:
143 case VK_FORMAT_B8G8R8A8_SRGB
:
144 return alpha
? WL_DRM_FORMAT_ARGB8888
: WL_DRM_FORMAT_XRGB8888
;
146 case VK_FORMAT_B10G10R10A2_UNORM
:
147 return alpha
? WL_DRM_FORMAT_ARGB2101010
: WL_DRM_FORMAT_XRGB2101010
;
151 assert(!"Unsupported Vulkan format");
157 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t wl_format
)
159 struct wsi_wl_display
*display
= data
;
160 if (display
->formats
.element_size
== 0)
165 case WL_DRM_FORMAT_ABGR4444
:
166 case WL_DRM_FORMAT_XBGR4444
:
167 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R4G4B4A4_UNORM
);
169 case WL_DRM_FORMAT_BGR565
:
170 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R5G6B5_UNORM
);
172 case WL_DRM_FORMAT_ABGR1555
:
173 case WL_DRM_FORMAT_XBGR1555
:
174 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R5G5B5A1_UNORM
);
176 case WL_DRM_FORMAT_XBGR8888
:
177 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R8G8B8_UNORM
);
179 case WL_DRM_FORMAT_ABGR8888
:
180 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R8G8B8A8_UNORM
);
182 case WL_DRM_FORMAT_ABGR2101010
:
183 case WL_DRM_FORMAT_XBGR2101010
:
184 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R10G10B10A2_UNORM
);
186 case WL_DRM_FORMAT_ARGB4444
:
187 case WL_DRM_FORMAT_XRGB4444
:
188 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B4G4R4A4_UNORM
);
190 case WL_DRM_FORMAT_RGB565
:
191 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B5G6R5_UNORM
);
193 case WL_DRM_FORMAT_ARGB1555
:
194 case WL_DRM_FORMAT_XRGB1555
:
195 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B5G5R5A1_UNORM
);
198 case WL_DRM_FORMAT_XRGB8888
:
199 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8_SRGB
);
200 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8_UNORM
);
202 case WL_DRM_FORMAT_ARGB8888
:
203 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8A8_SRGB
);
204 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8A8_UNORM
);
207 case WL_DRM_FORMAT_ARGB2101010
:
208 case WL_DRM_FORMAT_XRGB2101010
:
209 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B10G10R10A2_UNORM
);
216 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
221 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t capabilities
)
223 struct wsi_wl_display
*display
= data
;
225 display
->capabilities
= capabilities
;
228 static const struct wl_drm_listener drm_listener
= {
231 drm_handle_authenticated
,
232 drm_handle_capabilities
,
236 dmabuf_handle_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
239 /* Formats are implicitly advertised by the modifier event, so we ignore
244 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
245 uint32_t format
, uint32_t modifier_hi
,
246 uint32_t modifier_lo
)
248 struct wsi_wl_display
*display
= data
;
249 uint64_t *mod
= NULL
;
251 /* If we're not fetching formats, don't fetch modifiers either. */
252 if (display
->formats
.element_size
== 0)
255 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
256 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
260 case WL_DRM_FORMAT_ARGB8888
:
261 mod
= u_vector_add(&display
->modifiers
.argb8888
);
263 case WL_DRM_FORMAT_XRGB8888
:
264 mod
= u_vector_add(&display
->modifiers
.xrgb8888
);
273 *mod
= (uint64_t) modifier_hi
<< 32;
274 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
277 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
278 dmabuf_handle_format
,
279 dmabuf_handle_modifier
,
283 registry_handle_global(void *data
, struct wl_registry
*registry
,
284 uint32_t name
, const char *interface
, uint32_t version
)
286 struct wsi_wl_display
*display
= data
;
288 if (strcmp(interface
, "wl_drm") == 0) {
289 assert(display
->drm
== NULL
);
291 assert(version
>= 2);
292 display
->drm
= wl_registry_bind(registry
, name
, &wl_drm_interface
, 2);
295 wl_drm_add_listener(display
->drm
, &drm_listener
, display
);
296 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
298 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
, 3);
299 zwp_linux_dmabuf_v1_add_listener(display
->dmabuf
, &dmabuf_listener
,
305 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
309 static const struct wl_registry_listener registry_listener
= {
310 registry_handle_global
,
311 registry_handle_global_remove
315 wsi_wl_display_finish(struct wsi_wl_display
*display
)
317 assert(display
->refcount
== 0);
319 u_vector_finish(&display
->formats
);
320 u_vector_finish(&display
->modifiers
.argb8888
);
321 u_vector_finish(&display
->modifiers
.xrgb8888
);
323 wl_drm_destroy(display
->drm
);
325 zwp_linux_dmabuf_v1_destroy(display
->dmabuf
);
326 if (display
->wl_display_wrapper
)
327 wl_proxy_wrapper_destroy(display
->wl_display_wrapper
);
329 wl_event_queue_destroy(display
->queue
);
333 wsi_wl_display_init(struct wsi_wayland
*wsi_wl
,
334 struct wsi_wl_display
*display
,
335 struct wl_display
*wl_display
,
336 bool get_format_list
)
338 VkResult result
= VK_SUCCESS
;
339 memset(display
, 0, sizeof(*display
));
341 display
->wsi_wl
= wsi_wl
;
342 display
->wl_display
= wl_display
;
344 if (get_format_list
) {
345 if (!u_vector_init(&display
->formats
, sizeof(VkFormat
), 8) ||
346 !u_vector_init(&display
->modifiers
.argb8888
, sizeof(uint64_t), 32) ||
347 !u_vector_init(&display
->modifiers
.xrgb8888
, sizeof(uint64_t), 32)) {
348 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
353 display
->queue
= wl_display_create_queue(wl_display
);
354 if (!display
->queue
) {
355 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
359 display
->wl_display_wrapper
= wl_proxy_create_wrapper(wl_display
);
360 if (!display
->wl_display_wrapper
) {
361 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
365 wl_proxy_set_queue((struct wl_proxy
*) display
->wl_display_wrapper
,
368 struct wl_registry
*registry
=
369 wl_display_get_registry(display
->wl_display_wrapper
);
371 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
375 wl_registry_add_listener(registry
, ®istry_listener
, display
);
377 /* Round-trip to get the wl_drm global */
378 wl_display_roundtrip_queue(display
->wl_display
, display
->queue
);
381 result
= VK_ERROR_SURFACE_LOST_KHR
;
385 /* Round-trip to get wl_drm formats and capabilities */
386 wl_display_roundtrip_queue(display
->wl_display
, display
->queue
);
388 /* We need prime support */
389 if (!(display
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
390 result
= VK_ERROR_SURFACE_LOST_KHR
;
394 /* We don't need this anymore */
395 wl_registry_destroy(registry
);
397 display
->refcount
= 0;
403 wl_registry_destroy(registry
);
406 wsi_wl_display_finish(display
);
411 wsi_wl_display_create(struct wsi_wayland
*wsi
, struct wl_display
*wl_display
,
412 struct wsi_wl_display
**display_out
)
414 struct wsi_wl_display
*display
=
415 vk_alloc(wsi
->alloc
, sizeof(*display
), 8,
416 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
418 return VK_ERROR_OUT_OF_HOST_MEMORY
;
420 VkResult result
= wsi_wl_display_init(wsi
, display
, wl_display
, true);
421 if (result
!= VK_SUCCESS
) {
422 vk_free(wsi
->alloc
, display
);
427 *display_out
= display
;
432 static struct wsi_wl_display
*
433 wsi_wl_display_ref(struct wsi_wl_display
*display
)
440 wsi_wl_display_unref(struct wsi_wl_display
*display
)
442 if (display
->refcount
-- > 1)
445 struct wsi_wayland
*wsi
= display
->wsi_wl
;
446 wsi_wl_display_finish(display
);
447 vk_free(wsi
->alloc
, display
);
451 wsi_wl_get_presentation_support(struct wsi_device
*wsi_device
,
452 struct wl_display
*wl_display
)
454 struct wsi_wayland
*wsi
=
455 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
457 struct wsi_wl_display display
;
458 int ret
= wsi_wl_display_init(wsi
, &display
, wl_display
, false);
459 wsi_wl_display_finish(&display
);
465 wsi_wl_surface_get_support(VkIcdSurfaceBase
*surface
,
466 struct wsi_device
*wsi_device
,
467 uint32_t queueFamilyIndex
,
468 VkBool32
* pSupported
)
475 static const VkPresentModeKHR present_modes
[] = {
476 VK_PRESENT_MODE_MAILBOX_KHR
,
477 VK_PRESENT_MODE_FIFO_KHR
,
481 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase
*surface
,
482 VkSurfaceCapabilitiesKHR
* caps
)
484 /* For true mailbox mode, we need at least 4 images:
485 * 1) One to scan out from
486 * 2) One to have queued for scan-out
487 * 3) One to be currently held by the Wayland compositor
488 * 4) One to render to
490 caps
->minImageCount
= 4;
491 /* There is no real maximum */
492 caps
->maxImageCount
= 0;
494 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
495 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
496 /* This is the maximum supported size on Intel */
497 caps
->maxImageExtent
= (VkExtent2D
) { 1 << 14, 1 << 14 };
498 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
499 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
500 caps
->maxImageArrayLayers
= 1;
502 caps
->supportedCompositeAlpha
=
503 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
|
504 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
506 caps
->supportedUsageFlags
=
507 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
508 VK_IMAGE_USAGE_SAMPLED_BIT
|
509 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
510 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
516 wsi_wl_surface_get_capabilities2(VkIcdSurfaceBase
*surface
,
517 const void *info_next
,
518 VkSurfaceCapabilities2KHR
* caps
)
520 assert(caps
->sType
== VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
);
522 return wsi_wl_surface_get_capabilities(surface
, &caps
->surfaceCapabilities
);
526 wsi_wl_surface_get_formats(VkIcdSurfaceBase
*icd_surface
,
527 struct wsi_device
*wsi_device
,
528 uint32_t* pSurfaceFormatCount
,
529 VkSurfaceFormatKHR
* pSurfaceFormats
)
531 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
532 struct wsi_wayland
*wsi
=
533 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
535 struct wsi_wl_display display
;
536 if (wsi_wl_display_init(wsi
, &display
, surface
->display
, true))
537 return VK_ERROR_SURFACE_LOST_KHR
;
539 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
542 u_vector_foreach(disp_fmt
, &display
.formats
) {
543 vk_outarray_append(&out
, out_fmt
) {
544 out_fmt
->format
= *disp_fmt
;
545 out_fmt
->colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
549 wsi_wl_display_finish(&display
);
551 return vk_outarray_status(&out
);
555 wsi_wl_surface_get_formats2(VkIcdSurfaceBase
*icd_surface
,
556 struct wsi_device
*wsi_device
,
557 const void *info_next
,
558 uint32_t* pSurfaceFormatCount
,
559 VkSurfaceFormat2KHR
* pSurfaceFormats
)
561 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
562 struct wsi_wayland
*wsi
=
563 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
565 struct wsi_wl_display display
;
566 if (wsi_wl_display_init(wsi
, &display
, surface
->display
, true))
567 return VK_ERROR_SURFACE_LOST_KHR
;
569 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
572 u_vector_foreach(disp_fmt
, &display
.formats
) {
573 vk_outarray_append(&out
, out_fmt
) {
574 out_fmt
->surfaceFormat
.format
= *disp_fmt
;
575 out_fmt
->surfaceFormat
.colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
579 wsi_wl_display_finish(&display
);
581 return vk_outarray_status(&out
);
585 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
586 uint32_t* pPresentModeCount
,
587 VkPresentModeKHR
* pPresentModes
)
589 if (pPresentModes
== NULL
) {
590 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
594 *pPresentModeCount
= MIN2(*pPresentModeCount
, ARRAY_SIZE(present_modes
));
595 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
597 if (*pPresentModeCount
< ARRAY_SIZE(present_modes
))
598 return VK_INCOMPLETE
;
604 wsi_wl_surface_get_present_rectangles(VkIcdSurfaceBase
*surface
,
605 struct wsi_device
*wsi_device
,
606 uint32_t* pRectCount
,
609 VK_OUTARRAY_MAKE(out
, pRects
, pRectCount
);
611 vk_outarray_append(&out
, rect
) {
612 /* We don't know a size so just return the usual "I don't know." */
615 .extent
= { -1, -1 },
619 return vk_outarray_status(&out
);
622 VkResult
wsi_create_wl_surface(const VkAllocationCallbacks
*pAllocator
,
623 const VkWaylandSurfaceCreateInfoKHR
*pCreateInfo
,
624 VkSurfaceKHR
*pSurface
)
626 VkIcdSurfaceWayland
*surface
;
628 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
629 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
631 return VK_ERROR_OUT_OF_HOST_MEMORY
;
633 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_WAYLAND
;
634 surface
->display
= pCreateInfo
->display
;
635 surface
->surface
= pCreateInfo
->surface
;
637 *pSurface
= VkIcdSurfaceBase_to_handle(&surface
->base
);
642 struct wsi_wl_image
{
643 struct wsi_image base
;
644 struct wl_buffer
* buffer
;
648 struct wsi_wl_swapchain
{
649 struct wsi_swapchain base
;
651 struct wsi_wl_display
*display
;
653 struct wl_surface
* surface
;
654 uint32_t surface_version
;
655 struct wl_drm
* drm_wrapper
;
656 struct wl_callback
* frame
;
662 VkPresentModeKHR present_mode
;
665 struct wsi_wl_image images
[0];
668 static struct wsi_image
*
669 wsi_wl_swapchain_get_wsi_image(struct wsi_swapchain
*wsi_chain
,
670 uint32_t image_index
)
672 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
673 return &chain
->images
[image_index
].base
;
677 wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain
*wsi_chain
,
678 const VkAcquireNextImageInfoKHR
*info
,
679 uint32_t *image_index
)
681 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
683 int ret
= wl_display_dispatch_queue_pending(chain
->display
->wl_display
,
684 chain
->display
->queue
);
685 /* XXX: I'm not sure if out-of-date is the right error here. If
686 * wl_display_dispatch_queue_pending fails it most likely means we got
687 * kicked by the server so this seems more-or-less correct.
690 return VK_ERROR_OUT_OF_DATE_KHR
;
693 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
694 if (!chain
->images
[i
].busy
) {
695 /* We found a non-busy image */
697 chain
->images
[i
].busy
= true;
702 /* This time we do a blocking dispatch because we can't go
703 * anywhere until we get an event.
705 int ret
= wl_display_roundtrip_queue(chain
->display
->wl_display
,
706 chain
->display
->queue
);
708 return VK_ERROR_OUT_OF_DATE_KHR
;
713 frame_handle_done(void *data
, struct wl_callback
*callback
, uint32_t serial
)
715 struct wsi_wl_swapchain
*chain
= data
;
718 chain
->fifo_ready
= true;
720 wl_callback_destroy(callback
);
723 static const struct wl_callback_listener frame_listener
= {
728 wsi_wl_swapchain_queue_present(struct wsi_swapchain
*wsi_chain
,
729 uint32_t image_index
,
730 const VkPresentRegionKHR
*damage
)
732 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
734 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
735 while (!chain
->fifo_ready
) {
736 int ret
= wl_display_dispatch_queue(chain
->display
->wl_display
,
737 chain
->display
->queue
);
739 return VK_ERROR_OUT_OF_DATE_KHR
;
743 assert(image_index
< chain
->base
.image_count
);
744 wl_surface_attach(chain
->surface
, chain
->images
[image_index
].buffer
, 0, 0);
746 if (chain
->surface_version
>= 4 && damage
&&
747 damage
->pRectangles
&& damage
->rectangleCount
> 0) {
748 for (unsigned i
= 0; i
< damage
->rectangleCount
; i
++) {
749 const VkRectLayerKHR
*rect
= &damage
->pRectangles
[i
];
750 assert(rect
->layer
== 0);
751 wl_surface_damage_buffer(chain
->surface
,
752 rect
->offset
.x
, rect
->offset
.y
,
753 rect
->extent
.width
, rect
->extent
.height
);
756 wl_surface_damage(chain
->surface
, 0, 0, INT32_MAX
, INT32_MAX
);
759 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
760 chain
->frame
= wl_surface_frame(chain
->surface
);
761 wl_callback_add_listener(chain
->frame
, &frame_listener
, chain
);
762 chain
->fifo_ready
= false;
765 chain
->images
[image_index
].busy
= true;
766 wl_surface_commit(chain
->surface
);
767 wl_display_flush(chain
->display
->wl_display
);
773 buffer_handle_release(void *data
, struct wl_buffer
*buffer
)
775 struct wsi_wl_image
*image
= data
;
777 assert(image
->buffer
== buffer
);
782 static const struct wl_buffer_listener buffer_listener
= {
783 buffer_handle_release
,
787 wsi_wl_image_init(struct wsi_wl_swapchain
*chain
,
788 struct wsi_wl_image
*image
,
789 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
790 const VkAllocationCallbacks
* pAllocator
)
792 struct wsi_wl_display
*display
= chain
->display
;
793 const uint64_t *modifiers
= NULL
;
794 uint32_t num_modifiers
= 0;
797 if (display
->dmabuf
&& chain
->base
.wsi
->supports_modifiers
) {
798 switch (chain
->drm_format
) {
799 case WL_DRM_FORMAT_ARGB8888
:
800 modifiers
= u_vector_tail(&display
->modifiers
.argb8888
);
801 num_modifiers
= u_vector_length(&display
->modifiers
.argb8888
);
803 case WL_DRM_FORMAT_XRGB8888
:
804 modifiers
= u_vector_tail(&display
->modifiers
.xrgb8888
);
805 num_modifiers
= u_vector_length(&display
->modifiers
.xrgb8888
);
812 result
= wsi_create_native_image(&chain
->base
, pCreateInfo
,
813 num_modifiers
> 0 ? 1 : 0,
814 &num_modifiers
, &modifiers
,
817 if (result
!= VK_SUCCESS
)
820 if (image
->base
.drm_modifier
!= DRM_FORMAT_MOD_INVALID
) {
821 /* Only request modifiers if we have dmabuf, else it must be implicit. */
822 assert(display
->dmabuf
);
824 struct zwp_linux_buffer_params_v1
*params
=
825 zwp_linux_dmabuf_v1_create_params(display
->dmabuf
);
826 wl_proxy_set_queue((struct wl_proxy
*) params
, chain
->display
->queue
);
828 for (int i
= 0; i
< image
->base
.num_planes
; i
++) {
829 zwp_linux_buffer_params_v1_add(params
,
832 image
->base
.offsets
[i
],
833 image
->base
.row_pitches
[i
],
834 image
->base
.drm_modifier
>> 32,
835 image
->base
.drm_modifier
& 0xffffffff);
836 close(image
->base
.fds
[i
]);
840 zwp_linux_buffer_params_v1_create_immed(params
,
842 chain
->extent
.height
,
845 zwp_linux_buffer_params_v1_destroy(params
);
847 /* Without passing modifiers, we can't have multi-plane RGB images. */
848 assert(image
->base
.num_planes
== 1);
851 wl_drm_create_prime_buffer(chain
->drm_wrapper
,
852 image
->base
.fds
[0], /* name */
854 chain
->extent
.height
,
856 image
->base
.offsets
[0],
857 image
->base
.row_pitches
[0],
858 0, 0, 0, 0 /* unused */);
859 close(image
->base
.fds
[0]);
865 wl_buffer_add_listener(image
->buffer
, &buffer_listener
, image
);
870 wsi_destroy_image(&chain
->base
, &image
->base
);
876 wsi_wl_swapchain_destroy(struct wsi_swapchain
*wsi_chain
,
877 const VkAllocationCallbacks
*pAllocator
)
879 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
881 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
882 if (chain
->images
[i
].buffer
) {
883 wl_buffer_destroy(chain
->images
[i
].buffer
);
884 wsi_destroy_image(&chain
->base
, &chain
->images
[i
].base
);
889 wl_callback_destroy(chain
->frame
);
891 wl_proxy_wrapper_destroy(chain
->surface
);
892 if (chain
->drm_wrapper
)
893 wl_proxy_wrapper_destroy(chain
->drm_wrapper
);
896 wsi_wl_display_unref(chain
->display
);
898 wsi_swapchain_finish(&chain
->base
);
900 vk_free(pAllocator
, chain
);
906 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
908 struct wsi_device
*wsi_device
,
909 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
910 const VkAllocationCallbacks
* pAllocator
,
911 struct wsi_swapchain
**swapchain_out
)
913 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
914 struct wsi_wayland
*wsi
=
915 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
916 struct wsi_wl_swapchain
*chain
;
919 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
921 int num_images
= pCreateInfo
->minImageCount
;
923 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
924 chain
= vk_alloc(pAllocator
, size
, 8,
925 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
927 return VK_ERROR_OUT_OF_HOST_MEMORY
;
929 result
= wsi_swapchain_init(wsi_device
, &chain
->base
, device
,
930 pCreateInfo
, pAllocator
);
931 if (result
!= VK_SUCCESS
) {
932 vk_free(pAllocator
, chain
);
936 /* Mark a bunch of stuff as NULL. This way we can just call
937 * destroy_swapchain for cleanup.
939 for (uint32_t i
= 0; i
< num_images
; i
++)
940 chain
->images
[i
].buffer
= NULL
;
941 chain
->surface
= NULL
;
942 chain
->drm_wrapper
= NULL
;
945 bool alpha
= pCreateInfo
->compositeAlpha
==
946 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
948 chain
->base
.destroy
= wsi_wl_swapchain_destroy
;
949 chain
->base
.get_wsi_image
= wsi_wl_swapchain_get_wsi_image
;
950 chain
->base
.acquire_next_image
= wsi_wl_swapchain_acquire_next_image
;
951 chain
->base
.queue_present
= wsi_wl_swapchain_queue_present
;
952 chain
->base
.present_mode
= pCreateInfo
->presentMode
;
953 chain
->base
.image_count
= num_images
;
954 chain
->extent
= pCreateInfo
->imageExtent
;
955 chain
->vk_format
= pCreateInfo
->imageFormat
;
956 chain
->drm_format
= wl_drm_format_for_vk_format(chain
->vk_format
, alpha
);
958 if (pCreateInfo
->oldSwapchain
) {
959 /* If we have an oldSwapchain parameter, copy the display struct over
960 * from the old one so we don't have to fully re-initialize it.
962 struct wsi_wl_swapchain
*old_chain
= (void *)pCreateInfo
->oldSwapchain
;
963 chain
->display
= wsi_wl_display_ref(old_chain
->display
);
965 chain
->display
= NULL
;
966 result
= wsi_wl_display_create(wsi
, surface
->display
, &chain
->display
);
967 if (result
!= VK_SUCCESS
)
971 chain
->surface
= wl_proxy_create_wrapper(surface
->surface
);
972 if (!chain
->surface
) {
973 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
976 wl_proxy_set_queue((struct wl_proxy
*) chain
->surface
,
977 chain
->display
->queue
);
978 chain
->surface_version
= wl_proxy_get_version((void *)surface
->surface
);
980 chain
->drm_wrapper
= wl_proxy_create_wrapper(chain
->display
->drm
);
981 if (!chain
->drm_wrapper
) {
982 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
985 wl_proxy_set_queue((struct wl_proxy
*) chain
->drm_wrapper
,
986 chain
->display
->queue
);
988 chain
->fifo_ready
= true;
990 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
991 result
= wsi_wl_image_init(chain
, &chain
->images
[i
],
992 pCreateInfo
, pAllocator
);
993 if (result
!= VK_SUCCESS
)
995 chain
->images
[i
].busy
= false;
998 *swapchain_out
= &chain
->base
;
1003 wsi_wl_swapchain_destroy(&chain
->base
, pAllocator
);
1009 wsi_wl_init_wsi(struct wsi_device
*wsi_device
,
1010 const VkAllocationCallbacks
*alloc
,
1011 VkPhysicalDevice physical_device
)
1013 struct wsi_wayland
*wsi
;
1016 wsi
= vk_alloc(alloc
, sizeof(*wsi
), 8,
1017 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
1019 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1023 wsi
->physical_device
= physical_device
;
1025 wsi
->wsi
= wsi_device
;
1027 wsi
->base
.get_support
= wsi_wl_surface_get_support
;
1028 wsi
->base
.get_capabilities2
= wsi_wl_surface_get_capabilities2
;
1029 wsi
->base
.get_formats
= wsi_wl_surface_get_formats
;
1030 wsi
->base
.get_formats2
= wsi_wl_surface_get_formats2
;
1031 wsi
->base
.get_present_modes
= wsi_wl_surface_get_present_modes
;
1032 wsi
->base
.get_present_rectangles
= wsi_wl_surface_get_present_rectangles
;
1033 wsi
->base
.create_swapchain
= wsi_wl_surface_create_swapchain
;
1035 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = &wsi
->base
;
1040 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = NULL
;
1046 wsi_wl_finish_wsi(struct wsi_device
*wsi_device
,
1047 const VkAllocationCallbacks
*alloc
)
1049 struct wsi_wayland
*wsi
=
1050 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
1054 vk_free(alloc
, wsi
);