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-uapi/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
,
87 struct u_vector
*formats
, VkFormat format
)
89 /* Don't add a format that's already in the list */
91 u_vector_foreach(f
, formats
)
95 /* Don't add formats that aren't renderable. */
96 VkFormatProperties props
;
98 display
->wsi_wl
->wsi
->GetPhysicalDeviceFormatProperties(display
->wsi_wl
->physical_device
,
100 if (!(props
.optimalTilingFeatures
& VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
))
103 f
= u_vector_add(formats
);
109 wsi_wl_display_add_wl_format(struct wsi_wl_display
*display
,
110 struct u_vector
*formats
, uint32_t wl_format
)
114 case WL_DRM_FORMAT_ABGR4444
:
115 case WL_DRM_FORMAT_XBGR4444
:
116 wsi_wl_display_add_vk_format(display
, formats
,
117 VK_FORMAT_R4G4B4A4_UNORM
);
119 case WL_DRM_FORMAT_BGR565
:
120 wsi_wl_display_add_vk_format(display
, formats
,
121 VK_FORMAT_R5G6B5_UNORM
);
123 case WL_DRM_FORMAT_ABGR1555
:
124 case WL_DRM_FORMAT_XBGR1555
:
125 wsi_wl_display_add_vk_format(display
, formats
,
126 VK_FORMAT_R5G5B5A1_UNORM
);
128 case WL_DRM_FORMAT_XBGR8888
:
129 wsi_wl_display_add_vk_format(display
, formats
,
130 VK_FORMAT_R8G8B8_UNORM
);
132 case WL_DRM_FORMAT_ABGR8888
:
133 wsi_wl_display_add_vk_format(display
, formats
,
134 VK_FORMAT_R8G8B8A8_UNORM
);
136 case WL_DRM_FORMAT_ABGR2101010
:
137 case WL_DRM_FORMAT_XBGR2101010
:
138 wsi_wl_display_add_vk_format(display
, formats
,
139 VK_FORMAT_R10G10B10A2_UNORM
);
141 case WL_DRM_FORMAT_ARGB4444
:
142 case WL_DRM_FORMAT_XRGB4444
:
143 wsi_wl_display_add_vk_format(display
, formats
,
144 VK_FORMAT_B4G4R4A4_UNORM
);
146 case WL_DRM_FORMAT_RGB565
:
147 wsi_wl_display_add_vk_format(display
, formats
,
148 VK_FORMAT_B5G6R5_UNORM
);
150 case WL_DRM_FORMAT_ARGB1555
:
151 case WL_DRM_FORMAT_XRGB1555
:
152 wsi_wl_display_add_vk_format(display
, formats
,
153 VK_FORMAT_B5G5R5A1_UNORM
);
156 case WL_DRM_FORMAT_XRGB8888
:
157 wsi_wl_display_add_vk_format(display
, formats
,
158 VK_FORMAT_B8G8R8_SRGB
);
159 wsi_wl_display_add_vk_format(display
, formats
,
160 VK_FORMAT_B8G8R8_UNORM
);
162 case WL_DRM_FORMAT_ARGB8888
:
163 wsi_wl_display_add_vk_format(display
, formats
,
164 VK_FORMAT_B8G8R8A8_SRGB
);
165 wsi_wl_display_add_vk_format(display
, formats
,
166 VK_FORMAT_B8G8R8A8_UNORM
);
169 case WL_DRM_FORMAT_ARGB2101010
:
170 case WL_DRM_FORMAT_XRGB2101010
:
171 wsi_wl_display_add_vk_format(display
, formats
,
172 VK_FORMAT_B10G10R10A2_UNORM
);
179 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *name
)
184 wl_drm_format_for_vk_format(VkFormat vk_format
, bool alpha
)
187 /* TODO: Figure out what all the formats mean and make this table
191 case VK_FORMAT_R4G4B4A4_UNORM
:
192 return alpha
? WL_DRM_FORMAT_ABGR4444
: WL_DRM_FORMAT_XBGR4444
;
193 case VK_FORMAT_R5G6B5_UNORM
:
194 return WL_DRM_FORMAT_BGR565
;
195 case VK_FORMAT_R5G5B5A1_UNORM
:
196 return alpha
? WL_DRM_FORMAT_ABGR1555
: WL_DRM_FORMAT_XBGR1555
;
197 case VK_FORMAT_R8G8B8_UNORM
:
198 return WL_DRM_FORMAT_XBGR8888
;
199 case VK_FORMAT_R8G8B8A8_UNORM
:
200 return alpha
? WL_DRM_FORMAT_ABGR8888
: WL_DRM_FORMAT_XBGR8888
;
201 case VK_FORMAT_R10G10B10A2_UNORM
:
202 return alpha
? WL_DRM_FORMAT_ABGR2101010
: WL_DRM_FORMAT_XBGR2101010
;
203 case VK_FORMAT_B4G4R4A4_UNORM
:
204 return alpha
? WL_DRM_FORMAT_ARGB4444
: WL_DRM_FORMAT_XRGB4444
;
205 case VK_FORMAT_B5G6R5_UNORM
:
206 return WL_DRM_FORMAT_RGB565
;
207 case VK_FORMAT_B5G5R5A1_UNORM
:
208 return alpha
? WL_DRM_FORMAT_XRGB1555
: WL_DRM_FORMAT_XRGB1555
;
210 case VK_FORMAT_B8G8R8_UNORM
:
211 case VK_FORMAT_B8G8R8_SRGB
:
212 return WL_DRM_FORMAT_BGRX8888
;
213 case VK_FORMAT_B8G8R8A8_UNORM
:
214 case VK_FORMAT_B8G8R8A8_SRGB
:
215 return alpha
? WL_DRM_FORMAT_ARGB8888
: WL_DRM_FORMAT_XRGB8888
;
217 case VK_FORMAT_B10G10R10A2_UNORM
:
218 return alpha
? WL_DRM_FORMAT_ARGB2101010
: WL_DRM_FORMAT_XRGB2101010
;
222 assert(!"Unsupported Vulkan format");
228 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t wl_format
)
230 struct wsi_wl_display
*display
= data
;
231 if (display
->formats
.element_size
== 0)
234 wsi_wl_display_add_wl_format(display
, &display
->formats
, wl_format
);
238 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
243 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t capabilities
)
245 struct wsi_wl_display
*display
= data
;
247 display
->capabilities
= capabilities
;
250 static const struct wl_drm_listener drm_listener
= {
253 drm_handle_authenticated
,
254 drm_handle_capabilities
,
258 dmabuf_handle_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
261 /* Formats are implicitly advertised by the modifier event, so we ignore
266 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
267 uint32_t format
, uint32_t modifier_hi
,
268 uint32_t modifier_lo
)
270 struct wsi_wl_display
*display
= data
;
271 uint64_t *mod
= NULL
;
273 /* If we're not fetching formats, don't fetch modifiers either. */
274 if (display
->formats
.element_size
== 0)
277 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
278 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
282 case WL_DRM_FORMAT_ARGB8888
:
283 mod
= u_vector_add(&display
->modifiers
.argb8888
);
285 case WL_DRM_FORMAT_XRGB8888
:
286 mod
= u_vector_add(&display
->modifiers
.xrgb8888
);
295 *mod
= (uint64_t) modifier_hi
<< 32;
296 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
299 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
300 dmabuf_handle_format
,
301 dmabuf_handle_modifier
,
305 registry_handle_global(void *data
, struct wl_registry
*registry
,
306 uint32_t name
, const char *interface
, uint32_t version
)
308 struct wsi_wl_display
*display
= data
;
310 if (strcmp(interface
, "wl_drm") == 0) {
311 assert(display
->drm
== NULL
);
313 assert(version
>= 2);
314 display
->drm
= wl_registry_bind(registry
, name
, &wl_drm_interface
, 2);
317 wl_drm_add_listener(display
->drm
, &drm_listener
, display
);
318 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
320 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
, 3);
321 zwp_linux_dmabuf_v1_add_listener(display
->dmabuf
, &dmabuf_listener
,
327 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
331 static const struct wl_registry_listener registry_listener
= {
332 registry_handle_global
,
333 registry_handle_global_remove
337 wsi_wl_display_finish(struct wsi_wl_display
*display
)
339 assert(display
->refcount
== 0);
341 u_vector_finish(&display
->formats
);
342 u_vector_finish(&display
->modifiers
.argb8888
);
343 u_vector_finish(&display
->modifiers
.xrgb8888
);
345 wl_drm_destroy(display
->drm
);
347 zwp_linux_dmabuf_v1_destroy(display
->dmabuf
);
348 if (display
->wl_display_wrapper
)
349 wl_proxy_wrapper_destroy(display
->wl_display_wrapper
);
351 wl_event_queue_destroy(display
->queue
);
355 wsi_wl_display_init(struct wsi_wayland
*wsi_wl
,
356 struct wsi_wl_display
*display
,
357 struct wl_display
*wl_display
,
358 bool get_format_list
)
360 VkResult result
= VK_SUCCESS
;
361 memset(display
, 0, sizeof(*display
));
363 display
->wsi_wl
= wsi_wl
;
364 display
->wl_display
= wl_display
;
366 if (get_format_list
) {
367 if (!u_vector_init(&display
->formats
, sizeof(VkFormat
), 8) ||
368 !u_vector_init(&display
->modifiers
.argb8888
, sizeof(uint64_t), 32) ||
369 !u_vector_init(&display
->modifiers
.xrgb8888
, sizeof(uint64_t), 32)) {
370 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
375 display
->queue
= wl_display_create_queue(wl_display
);
376 if (!display
->queue
) {
377 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
381 display
->wl_display_wrapper
= wl_proxy_create_wrapper(wl_display
);
382 if (!display
->wl_display_wrapper
) {
383 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
387 wl_proxy_set_queue((struct wl_proxy
*) display
->wl_display_wrapper
,
390 struct wl_registry
*registry
=
391 wl_display_get_registry(display
->wl_display_wrapper
);
393 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
397 wl_registry_add_listener(registry
, ®istry_listener
, display
);
399 /* Round-trip to get the wl_drm global */
400 wl_display_roundtrip_queue(display
->wl_display
, display
->queue
);
403 result
= VK_ERROR_SURFACE_LOST_KHR
;
407 /* Round-trip to get wl_drm formats and capabilities */
408 wl_display_roundtrip_queue(display
->wl_display
, display
->queue
);
410 /* We need prime support */
411 if (!(display
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
412 result
= VK_ERROR_SURFACE_LOST_KHR
;
416 /* We don't need this anymore */
417 wl_registry_destroy(registry
);
419 display
->refcount
= 0;
425 wl_registry_destroy(registry
);
428 wsi_wl_display_finish(display
);
433 wsi_wl_display_create(struct wsi_wayland
*wsi
, struct wl_display
*wl_display
,
434 struct wsi_wl_display
**display_out
)
436 struct wsi_wl_display
*display
=
437 vk_alloc(wsi
->alloc
, sizeof(*display
), 8,
438 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
440 return VK_ERROR_OUT_OF_HOST_MEMORY
;
442 VkResult result
= wsi_wl_display_init(wsi
, display
, wl_display
, true);
443 if (result
!= VK_SUCCESS
) {
444 vk_free(wsi
->alloc
, display
);
449 *display_out
= display
;
454 static struct wsi_wl_display
*
455 wsi_wl_display_ref(struct wsi_wl_display
*display
)
462 wsi_wl_display_unref(struct wsi_wl_display
*display
)
464 if (display
->refcount
-- > 1)
467 struct wsi_wayland
*wsi
= display
->wsi_wl
;
468 wsi_wl_display_finish(display
);
469 vk_free(wsi
->alloc
, display
);
473 wsi_wl_get_presentation_support(struct wsi_device
*wsi_device
,
474 struct wl_display
*wl_display
)
476 struct wsi_wayland
*wsi
=
477 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
479 struct wsi_wl_display display
;
480 VkResult ret
= wsi_wl_display_init(wsi
, &display
, wl_display
, false);
481 if (ret
== VK_SUCCESS
)
482 wsi_wl_display_finish(&display
);
484 return ret
== VK_SUCCESS
;
488 wsi_wl_surface_get_support(VkIcdSurfaceBase
*surface
,
489 struct wsi_device
*wsi_device
,
490 uint32_t queueFamilyIndex
,
491 VkBool32
* pSupported
)
498 static const VkPresentModeKHR present_modes
[] = {
499 VK_PRESENT_MODE_MAILBOX_KHR
,
500 VK_PRESENT_MODE_FIFO_KHR
,
504 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase
*surface
,
505 struct wsi_device
*wsi_device
,
506 VkSurfaceCapabilitiesKHR
* caps
)
508 /* For true mailbox mode, we need at least 4 images:
509 * 1) One to scan out from
510 * 2) One to have queued for scan-out
511 * 3) One to be currently held by the Wayland compositor
512 * 4) One to render to
514 caps
->minImageCount
= 4;
515 /* There is no real maximum */
516 caps
->maxImageCount
= 0;
518 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
519 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
520 caps
->maxImageExtent
= (VkExtent2D
) {
521 wsi_device
->maxImageDimension2D
,
522 wsi_device
->maxImageDimension2D
,
525 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
526 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
527 caps
->maxImageArrayLayers
= 1;
529 caps
->supportedCompositeAlpha
=
530 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
|
531 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
533 caps
->supportedUsageFlags
=
534 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
535 VK_IMAGE_USAGE_SAMPLED_BIT
|
536 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
537 VK_IMAGE_USAGE_STORAGE_BIT
|
538 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
544 wsi_wl_surface_get_capabilities2(VkIcdSurfaceBase
*surface
,
545 struct wsi_device
*wsi_device
,
546 const void *info_next
,
547 VkSurfaceCapabilities2KHR
* caps
)
549 assert(caps
->sType
== VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
);
552 wsi_wl_surface_get_capabilities(surface
, wsi_device
,
553 &caps
->surfaceCapabilities
);
555 vk_foreach_struct(ext
, caps
->pNext
) {
556 switch (ext
->sType
) {
557 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR
: {
558 VkSurfaceProtectedCapabilitiesKHR
*protected = (void *)ext
;
559 protected->supportsProtected
= VK_FALSE
;
573 wsi_wl_surface_get_formats(VkIcdSurfaceBase
*icd_surface
,
574 struct wsi_device
*wsi_device
,
575 uint32_t* pSurfaceFormatCount
,
576 VkSurfaceFormatKHR
* pSurfaceFormats
)
578 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
579 struct wsi_wayland
*wsi
=
580 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
582 struct wsi_wl_display display
;
583 if (wsi_wl_display_init(wsi
, &display
, surface
->display
, true))
584 return VK_ERROR_SURFACE_LOST_KHR
;
586 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
589 u_vector_foreach(disp_fmt
, &display
.formats
) {
590 vk_outarray_append(&out
, out_fmt
) {
591 out_fmt
->format
= *disp_fmt
;
592 out_fmt
->colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
596 wsi_wl_display_finish(&display
);
598 return vk_outarray_status(&out
);
602 wsi_wl_surface_get_formats2(VkIcdSurfaceBase
*icd_surface
,
603 struct wsi_device
*wsi_device
,
604 const void *info_next
,
605 uint32_t* pSurfaceFormatCount
,
606 VkSurfaceFormat2KHR
* pSurfaceFormats
)
608 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
609 struct wsi_wayland
*wsi
=
610 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
612 struct wsi_wl_display display
;
613 if (wsi_wl_display_init(wsi
, &display
, surface
->display
, true))
614 return VK_ERROR_SURFACE_LOST_KHR
;
616 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
619 u_vector_foreach(disp_fmt
, &display
.formats
) {
620 vk_outarray_append(&out
, out_fmt
) {
621 out_fmt
->surfaceFormat
.format
= *disp_fmt
;
622 out_fmt
->surfaceFormat
.colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
626 wsi_wl_display_finish(&display
);
628 return vk_outarray_status(&out
);
632 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
633 uint32_t* pPresentModeCount
,
634 VkPresentModeKHR
* pPresentModes
)
636 if (pPresentModes
== NULL
) {
637 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
641 *pPresentModeCount
= MIN2(*pPresentModeCount
, ARRAY_SIZE(present_modes
));
642 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
644 if (*pPresentModeCount
< ARRAY_SIZE(present_modes
))
645 return VK_INCOMPLETE
;
651 wsi_wl_surface_get_present_rectangles(VkIcdSurfaceBase
*surface
,
652 struct wsi_device
*wsi_device
,
653 uint32_t* pRectCount
,
656 VK_OUTARRAY_MAKE(out
, pRects
, pRectCount
);
658 vk_outarray_append(&out
, rect
) {
659 /* We don't know a size so just return the usual "I don't know." */
662 .extent
= { -1, -1 },
666 return vk_outarray_status(&out
);
669 VkResult
wsi_create_wl_surface(const VkAllocationCallbacks
*pAllocator
,
670 const VkWaylandSurfaceCreateInfoKHR
*pCreateInfo
,
671 VkSurfaceKHR
*pSurface
)
673 VkIcdSurfaceWayland
*surface
;
675 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
676 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
678 return VK_ERROR_OUT_OF_HOST_MEMORY
;
680 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_WAYLAND
;
681 surface
->display
= pCreateInfo
->display
;
682 surface
->surface
= pCreateInfo
->surface
;
684 *pSurface
= VkIcdSurfaceBase_to_handle(&surface
->base
);
689 struct wsi_wl_image
{
690 struct wsi_image base
;
691 struct wl_buffer
* buffer
;
695 struct wsi_wl_swapchain
{
696 struct wsi_swapchain base
;
698 struct wsi_wl_display
*display
;
700 struct wl_surface
* surface
;
701 uint32_t surface_version
;
703 /* non-NULL when wl_drm should be used for wl_buffer creation; otherwise,
704 * zwp_linux_dmabuf_v1 should be used.
706 struct wl_drm
* drm_wrapper
;
708 struct wl_callback
* frame
;
714 uint32_t num_drm_modifiers
;
715 const uint64_t * drm_modifiers
;
717 VkPresentModeKHR present_mode
;
720 struct wsi_wl_image images
[0];
722 WSI_DEFINE_NONDISP_HANDLE_CASTS(wsi_wl_swapchain
, VkSwapchainKHR
)
724 static struct wsi_image
*
725 wsi_wl_swapchain_get_wsi_image(struct wsi_swapchain
*wsi_chain
,
726 uint32_t image_index
)
728 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
729 return &chain
->images
[image_index
].base
;
733 wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain
*wsi_chain
,
734 const VkAcquireNextImageInfoKHR
*info
,
735 uint32_t *image_index
)
737 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
739 int ret
= wl_display_dispatch_queue_pending(chain
->display
->wl_display
,
740 chain
->display
->queue
);
741 /* XXX: I'm not sure if out-of-date is the right error here. If
742 * wl_display_dispatch_queue_pending fails it most likely means we got
743 * kicked by the server so this seems more-or-less correct.
746 return VK_ERROR_OUT_OF_DATE_KHR
;
749 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
750 if (!chain
->images
[i
].busy
) {
751 /* We found a non-busy image */
753 chain
->images
[i
].busy
= true;
758 /* This time we do a blocking dispatch because we can't go
759 * anywhere until we get an event.
761 int ret
= wl_display_roundtrip_queue(chain
->display
->wl_display
,
762 chain
->display
->queue
);
764 return VK_ERROR_OUT_OF_DATE_KHR
;
769 frame_handle_done(void *data
, struct wl_callback
*callback
, uint32_t serial
)
771 struct wsi_wl_swapchain
*chain
= data
;
774 chain
->fifo_ready
= true;
776 wl_callback_destroy(callback
);
779 static const struct wl_callback_listener frame_listener
= {
784 wsi_wl_swapchain_queue_present(struct wsi_swapchain
*wsi_chain
,
785 uint32_t image_index
,
786 const VkPresentRegionKHR
*damage
)
788 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
790 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
791 while (!chain
->fifo_ready
) {
792 int ret
= wl_display_dispatch_queue(chain
->display
->wl_display
,
793 chain
->display
->queue
);
795 return VK_ERROR_OUT_OF_DATE_KHR
;
799 assert(image_index
< chain
->base
.image_count
);
800 wl_surface_attach(chain
->surface
, chain
->images
[image_index
].buffer
, 0, 0);
802 if (chain
->surface_version
>= 4 && damage
&&
803 damage
->pRectangles
&& damage
->rectangleCount
> 0) {
804 for (unsigned i
= 0; i
< damage
->rectangleCount
; i
++) {
805 const VkRectLayerKHR
*rect
= &damage
->pRectangles
[i
];
806 assert(rect
->layer
== 0);
807 wl_surface_damage_buffer(chain
->surface
,
808 rect
->offset
.x
, rect
->offset
.y
,
809 rect
->extent
.width
, rect
->extent
.height
);
812 wl_surface_damage(chain
->surface
, 0, 0, INT32_MAX
, INT32_MAX
);
815 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
816 chain
->frame
= wl_surface_frame(chain
->surface
);
817 wl_callback_add_listener(chain
->frame
, &frame_listener
, chain
);
818 chain
->fifo_ready
= false;
821 chain
->images
[image_index
].busy
= true;
822 wl_surface_commit(chain
->surface
);
823 wl_display_flush(chain
->display
->wl_display
);
829 buffer_handle_release(void *data
, struct wl_buffer
*buffer
)
831 struct wsi_wl_image
*image
= data
;
833 assert(image
->buffer
== buffer
);
838 static const struct wl_buffer_listener buffer_listener
= {
839 buffer_handle_release
,
843 wsi_wl_image_init(struct wsi_wl_swapchain
*chain
,
844 struct wsi_wl_image
*image
,
845 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
846 const VkAllocationCallbacks
* pAllocator
)
848 struct wsi_wl_display
*display
= chain
->display
;
851 result
= wsi_create_native_image(&chain
->base
, pCreateInfo
,
852 chain
->num_drm_modifiers
> 0 ? 1 : 0,
853 &chain
->num_drm_modifiers
,
854 &chain
->drm_modifiers
, &image
->base
);
856 if (result
!= VK_SUCCESS
)
859 if (!chain
->drm_wrapper
) {
860 /* Only request modifiers if we have dmabuf, else it must be implicit. */
861 assert(display
->dmabuf
);
862 assert(image
->base
.drm_modifier
!= DRM_FORMAT_MOD_INVALID
);
864 struct zwp_linux_buffer_params_v1
*params
=
865 zwp_linux_dmabuf_v1_create_params(display
->dmabuf
);
866 wl_proxy_set_queue((struct wl_proxy
*) params
, chain
->display
->queue
);
868 for (int i
= 0; i
< image
->base
.num_planes
; i
++) {
869 zwp_linux_buffer_params_v1_add(params
,
872 image
->base
.offsets
[i
],
873 image
->base
.row_pitches
[i
],
874 image
->base
.drm_modifier
>> 32,
875 image
->base
.drm_modifier
& 0xffffffff);
876 close(image
->base
.fds
[i
]);
880 zwp_linux_buffer_params_v1_create_immed(params
,
882 chain
->extent
.height
,
885 zwp_linux_buffer_params_v1_destroy(params
);
887 /* Without passing modifiers, we can't have multi-plane RGB images. */
888 assert(image
->base
.num_planes
== 1);
889 assert(image
->base
.drm_modifier
== DRM_FORMAT_MOD_INVALID
);
892 wl_drm_create_prime_buffer(chain
->drm_wrapper
,
893 image
->base
.fds
[0], /* name */
895 chain
->extent
.height
,
897 image
->base
.offsets
[0],
898 image
->base
.row_pitches
[0],
899 0, 0, 0, 0 /* unused */);
900 close(image
->base
.fds
[0]);
906 wl_buffer_add_listener(image
->buffer
, &buffer_listener
, image
);
911 wsi_destroy_image(&chain
->base
, &image
->base
);
917 wsi_wl_swapchain_destroy(struct wsi_swapchain
*wsi_chain
,
918 const VkAllocationCallbacks
*pAllocator
)
920 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
922 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
923 if (chain
->images
[i
].buffer
) {
924 wl_buffer_destroy(chain
->images
[i
].buffer
);
925 wsi_destroy_image(&chain
->base
, &chain
->images
[i
].base
);
930 wl_callback_destroy(chain
->frame
);
932 wl_proxy_wrapper_destroy(chain
->surface
);
933 if (chain
->drm_wrapper
)
934 wl_proxy_wrapper_destroy(chain
->drm_wrapper
);
937 wsi_wl_display_unref(chain
->display
);
939 wsi_swapchain_finish(&chain
->base
);
941 vk_free(pAllocator
, chain
);
947 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
949 struct wsi_device
*wsi_device
,
950 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
951 const VkAllocationCallbacks
* pAllocator
,
952 struct wsi_swapchain
**swapchain_out
)
954 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
955 struct wsi_wayland
*wsi
=
956 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
957 struct wsi_wl_swapchain
*chain
;
960 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
962 int num_images
= pCreateInfo
->minImageCount
;
964 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
965 chain
= vk_alloc(pAllocator
, size
, 8,
966 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
968 return VK_ERROR_OUT_OF_HOST_MEMORY
;
970 result
= wsi_swapchain_init(wsi_device
, &chain
->base
, device
,
971 pCreateInfo
, pAllocator
);
972 if (result
!= VK_SUCCESS
) {
973 vk_free(pAllocator
, chain
);
977 /* Mark a bunch of stuff as NULL. This way we can just call
978 * destroy_swapchain for cleanup.
980 for (uint32_t i
= 0; i
< num_images
; i
++)
981 chain
->images
[i
].buffer
= NULL
;
982 chain
->surface
= NULL
;
983 chain
->drm_wrapper
= NULL
;
986 bool alpha
= pCreateInfo
->compositeAlpha
==
987 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
989 chain
->base
.destroy
= wsi_wl_swapchain_destroy
;
990 chain
->base
.get_wsi_image
= wsi_wl_swapchain_get_wsi_image
;
991 chain
->base
.acquire_next_image
= wsi_wl_swapchain_acquire_next_image
;
992 chain
->base
.queue_present
= wsi_wl_swapchain_queue_present
;
993 chain
->base
.present_mode
= pCreateInfo
->presentMode
;
994 chain
->base
.image_count
= num_images
;
995 chain
->extent
= pCreateInfo
->imageExtent
;
996 chain
->vk_format
= pCreateInfo
->imageFormat
;
997 chain
->drm_format
= wl_drm_format_for_vk_format(chain
->vk_format
, alpha
);
999 if (pCreateInfo
->oldSwapchain
) {
1000 /* If we have an oldSwapchain parameter, copy the display struct over
1001 * from the old one so we don't have to fully re-initialize it.
1003 WSI_FROM_HANDLE(wsi_wl_swapchain
, old_chain
, pCreateInfo
->oldSwapchain
);
1004 chain
->display
= wsi_wl_display_ref(old_chain
->display
);
1006 chain
->display
= NULL
;
1007 result
= wsi_wl_display_create(wsi
, surface
->display
, &chain
->display
);
1008 if (result
!= VK_SUCCESS
)
1012 chain
->surface
= wl_proxy_create_wrapper(surface
->surface
);
1013 if (!chain
->surface
) {
1014 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1017 wl_proxy_set_queue((struct wl_proxy
*) chain
->surface
,
1018 chain
->display
->queue
);
1019 chain
->surface_version
= wl_proxy_get_version((void *)surface
->surface
);
1021 chain
->num_drm_modifiers
= 0;
1022 chain
->drm_modifiers
= 0;
1024 /* Use explicit DRM format modifiers when both the server and the driver
1027 if (chain
->display
->dmabuf
&& chain
->base
.wsi
->supports_modifiers
) {
1028 struct u_vector
*modifiers
;
1029 switch (chain
->drm_format
) {
1030 case WL_DRM_FORMAT_ARGB8888
:
1031 modifiers
= &chain
->display
->modifiers
.argb8888
;
1033 case WL_DRM_FORMAT_XRGB8888
:
1034 modifiers
= &chain
->display
->modifiers
.xrgb8888
;
1042 chain
->drm_modifiers
= u_vector_tail(modifiers
);
1043 chain
->num_drm_modifiers
= u_vector_length(modifiers
);
1047 /* When there are explicit DRM format modifiers, we must use
1048 * zwp_linux_dmabuf_v1 for wl_buffer creation. Otherwise, we must use
1051 if (!chain
->num_drm_modifiers
) {
1052 chain
->drm_wrapper
= wl_proxy_create_wrapper(chain
->display
->drm
);
1053 if (!chain
->drm_wrapper
) {
1054 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1057 wl_proxy_set_queue((struct wl_proxy
*) chain
->drm_wrapper
,
1058 chain
->display
->queue
);
1061 chain
->fifo_ready
= true;
1063 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
1064 result
= wsi_wl_image_init(chain
, &chain
->images
[i
],
1065 pCreateInfo
, pAllocator
);
1066 if (result
!= VK_SUCCESS
)
1068 chain
->images
[i
].busy
= false;
1071 *swapchain_out
= &chain
->base
;
1076 wsi_wl_swapchain_destroy(&chain
->base
, pAllocator
);
1082 wsi_wl_init_wsi(struct wsi_device
*wsi_device
,
1083 const VkAllocationCallbacks
*alloc
,
1084 VkPhysicalDevice physical_device
)
1086 struct wsi_wayland
*wsi
;
1089 wsi
= vk_alloc(alloc
, sizeof(*wsi
), 8,
1090 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
1092 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1096 wsi
->physical_device
= physical_device
;
1098 wsi
->wsi
= wsi_device
;
1100 wsi
->base
.get_support
= wsi_wl_surface_get_support
;
1101 wsi
->base
.get_capabilities2
= wsi_wl_surface_get_capabilities2
;
1102 wsi
->base
.get_formats
= wsi_wl_surface_get_formats
;
1103 wsi
->base
.get_formats2
= wsi_wl_surface_get_formats2
;
1104 wsi
->base
.get_present_modes
= wsi_wl_surface_get_present_modes
;
1105 wsi
->base
.get_present_rectangles
= wsi_wl_surface_get_present_rectangles
;
1106 wsi
->base
.create_swapchain
= wsi_wl_surface_create_swapchain
;
1108 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = &wsi
->base
;
1113 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = NULL
;
1119 wsi_wl_finish_wsi(struct wsi_device
*wsi_device
,
1120 const VkAllocationCallbacks
*alloc
)
1122 struct wsi_wayland
*wsi
=
1123 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
1127 vk_free(alloc
, wsi
);