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_drm
{
53 struct wl_drm
* wl_drm
;
54 uint32_t capabilities
;
57 struct wsi_wl_display_dmabuf
{
58 struct zwp_linux_dmabuf_v1
* wl_dmabuf
;
60 struct u_vector argb8888
;
61 struct u_vector xrgb8888
;
65 struct wsi_wl_display
{
66 /* The real wl_display */
67 struct wl_display
* wl_display
;
68 /* Actually a proxy wrapper around the event queue */
69 struct wl_display
* wl_display_wrapper
;
70 struct wl_event_queue
* queue
;
72 struct wsi_wl_display_drm drm
;
73 struct wsi_wl_display_dmabuf dmabuf
;
75 struct wsi_wayland
*wsi_wl
;
76 /* Vector of VkFormats supported */
77 struct u_vector formats
;
79 /* Only used for displays created by wsi_wl_display_create */
84 struct wsi_interface base
;
86 struct wsi_device
*wsi
;
88 const VkAllocationCallbacks
*alloc
;
89 VkPhysicalDevice physical_device
;
93 wsi_wl_display_add_vk_format(struct wsi_wl_display
*display
,
94 struct u_vector
*formats
, VkFormat format
)
96 /* Don't add a format that's already in the list */
98 u_vector_foreach(f
, formats
)
102 /* Don't add formats that aren't renderable. */
103 VkFormatProperties props
;
105 display
->wsi_wl
->wsi
->GetPhysicalDeviceFormatProperties(display
->wsi_wl
->physical_device
,
107 if (!(props
.optimalTilingFeatures
& VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
))
110 f
= u_vector_add(formats
);
116 wsi_wl_display_add_wl_format(struct wsi_wl_display
*display
,
117 struct u_vector
*formats
, uint32_t wl_format
)
121 case WL_DRM_FORMAT_ABGR4444
:
122 case WL_DRM_FORMAT_XBGR4444
:
123 wsi_wl_display_add_vk_format(display
, formats
,
124 VK_FORMAT_R4G4B4A4_UNORM
);
126 case WL_DRM_FORMAT_BGR565
:
127 wsi_wl_display_add_vk_format(display
, formats
,
128 VK_FORMAT_R5G6B5_UNORM
);
130 case WL_DRM_FORMAT_ABGR1555
:
131 case WL_DRM_FORMAT_XBGR1555
:
132 wsi_wl_display_add_vk_format(display
, formats
,
133 VK_FORMAT_R5G5B5A1_UNORM
);
135 case WL_DRM_FORMAT_XBGR8888
:
136 wsi_wl_display_add_vk_format(display
, formats
,
137 VK_FORMAT_R8G8B8_UNORM
);
139 case WL_DRM_FORMAT_ABGR8888
:
140 wsi_wl_display_add_vk_format(display
, formats
,
141 VK_FORMAT_R8G8B8A8_UNORM
);
143 case WL_DRM_FORMAT_ABGR2101010
:
144 case WL_DRM_FORMAT_XBGR2101010
:
145 wsi_wl_display_add_vk_format(display
, formats
,
146 VK_FORMAT_R10G10B10A2_UNORM
);
148 case WL_DRM_FORMAT_ARGB4444
:
149 case WL_DRM_FORMAT_XRGB4444
:
150 wsi_wl_display_add_vk_format(display
, formats
,
151 VK_FORMAT_B4G4R4A4_UNORM
);
153 case WL_DRM_FORMAT_RGB565
:
154 wsi_wl_display_add_vk_format(display
, formats
,
155 VK_FORMAT_B5G6R5_UNORM
);
157 case WL_DRM_FORMAT_ARGB1555
:
158 case WL_DRM_FORMAT_XRGB1555
:
159 wsi_wl_display_add_vk_format(display
, formats
,
160 VK_FORMAT_B5G5R5A1_UNORM
);
163 case WL_DRM_FORMAT_XRGB8888
:
164 wsi_wl_display_add_vk_format(display
, formats
,
165 VK_FORMAT_B8G8R8_SRGB
);
166 wsi_wl_display_add_vk_format(display
, formats
,
167 VK_FORMAT_B8G8R8_UNORM
);
169 case WL_DRM_FORMAT_ARGB8888
:
170 wsi_wl_display_add_vk_format(display
, formats
,
171 VK_FORMAT_B8G8R8A8_SRGB
);
172 wsi_wl_display_add_vk_format(display
, formats
,
173 VK_FORMAT_B8G8R8A8_UNORM
);
176 case WL_DRM_FORMAT_ARGB2101010
:
177 case WL_DRM_FORMAT_XRGB2101010
:
178 wsi_wl_display_add_vk_format(display
, formats
,
179 VK_FORMAT_B10G10R10A2_UNORM
);
186 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *name
)
191 wl_drm_format_for_vk_format(VkFormat vk_format
, bool alpha
)
194 /* TODO: Figure out what all the formats mean and make this table
198 case VK_FORMAT_R4G4B4A4_UNORM
:
199 return alpha
? WL_DRM_FORMAT_ABGR4444
: WL_DRM_FORMAT_XBGR4444
;
200 case VK_FORMAT_R5G6B5_UNORM
:
201 return WL_DRM_FORMAT_BGR565
;
202 case VK_FORMAT_R5G5B5A1_UNORM
:
203 return alpha
? WL_DRM_FORMAT_ABGR1555
: WL_DRM_FORMAT_XBGR1555
;
204 case VK_FORMAT_R8G8B8_UNORM
:
205 return WL_DRM_FORMAT_XBGR8888
;
206 case VK_FORMAT_R8G8B8A8_UNORM
:
207 return alpha
? WL_DRM_FORMAT_ABGR8888
: WL_DRM_FORMAT_XBGR8888
;
208 case VK_FORMAT_R10G10B10A2_UNORM
:
209 return alpha
? WL_DRM_FORMAT_ABGR2101010
: WL_DRM_FORMAT_XBGR2101010
;
210 case VK_FORMAT_B4G4R4A4_UNORM
:
211 return alpha
? WL_DRM_FORMAT_ARGB4444
: WL_DRM_FORMAT_XRGB4444
;
212 case VK_FORMAT_B5G6R5_UNORM
:
213 return WL_DRM_FORMAT_RGB565
;
214 case VK_FORMAT_B5G5R5A1_UNORM
:
215 return alpha
? WL_DRM_FORMAT_XRGB1555
: WL_DRM_FORMAT_XRGB1555
;
217 case VK_FORMAT_B8G8R8_UNORM
:
218 case VK_FORMAT_B8G8R8_SRGB
:
219 return WL_DRM_FORMAT_BGRX8888
;
220 case VK_FORMAT_B8G8R8A8_UNORM
:
221 case VK_FORMAT_B8G8R8A8_SRGB
:
222 return alpha
? WL_DRM_FORMAT_ARGB8888
: WL_DRM_FORMAT_XRGB8888
;
224 case VK_FORMAT_B10G10R10A2_UNORM
:
225 return alpha
? WL_DRM_FORMAT_ARGB2101010
: WL_DRM_FORMAT_XRGB2101010
;
229 assert(!"Unsupported Vulkan format");
235 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t wl_format
)
237 struct wsi_wl_display
*display
= data
;
238 if (display
->formats
.element_size
== 0)
241 wsi_wl_display_add_wl_format(display
, &display
->formats
, wl_format
);
245 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
250 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t capabilities
)
252 struct wsi_wl_display
*display
= data
;
254 display
->drm
.capabilities
= capabilities
;
257 static const struct wl_drm_listener drm_listener
= {
260 drm_handle_authenticated
,
261 drm_handle_capabilities
,
265 dmabuf_handle_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
268 /* Formats are implicitly advertised by the modifier event, so we ignore
273 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
274 uint32_t format
, uint32_t modifier_hi
,
275 uint32_t modifier_lo
)
277 struct wsi_wl_display
*display
= data
;
278 uint64_t *mod
= NULL
;
280 /* If we're not fetching formats, don't fetch modifiers either. */
281 if (display
->formats
.element_size
== 0)
284 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
285 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
289 case WL_DRM_FORMAT_ARGB8888
:
290 mod
= u_vector_add(&display
->dmabuf
.modifiers
.argb8888
);
292 case WL_DRM_FORMAT_XRGB8888
:
293 mod
= u_vector_add(&display
->dmabuf
.modifiers
.xrgb8888
);
302 *mod
= (uint64_t) modifier_hi
<< 32;
303 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
306 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
307 dmabuf_handle_format
,
308 dmabuf_handle_modifier
,
312 registry_handle_global(void *data
, struct wl_registry
*registry
,
313 uint32_t name
, const char *interface
, uint32_t version
)
315 struct wsi_wl_display
*display
= data
;
317 if (strcmp(interface
, "wl_drm") == 0) {
318 assert(display
->drm
.wl_drm
== NULL
);
320 assert(version
>= 2);
321 display
->drm
.wl_drm
=
322 wl_registry_bind(registry
, name
, &wl_drm_interface
, 2);
323 wl_drm_add_listener(display
->drm
.wl_drm
, &drm_listener
, display
);
324 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3) {
325 display
->dmabuf
.wl_dmabuf
=
326 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
, 3);
327 zwp_linux_dmabuf_v1_add_listener(display
->dmabuf
.wl_dmabuf
,
328 &dmabuf_listener
, display
);
333 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
337 static const struct wl_registry_listener registry_listener
= {
338 registry_handle_global
,
339 registry_handle_global_remove
343 wsi_wl_display_finish(struct wsi_wl_display
*display
)
345 assert(display
->refcount
== 0);
347 u_vector_finish(&display
->formats
);
348 u_vector_finish(&display
->dmabuf
.modifiers
.argb8888
);
349 u_vector_finish(&display
->dmabuf
.modifiers
.xrgb8888
);
350 if (display
->drm
.wl_drm
)
351 wl_drm_destroy(display
->drm
.wl_drm
);
352 if (display
->dmabuf
.wl_dmabuf
)
353 zwp_linux_dmabuf_v1_destroy(display
->dmabuf
.wl_dmabuf
);
354 if (display
->wl_display_wrapper
)
355 wl_proxy_wrapper_destroy(display
->wl_display_wrapper
);
357 wl_event_queue_destroy(display
->queue
);
361 wsi_wl_display_init(struct wsi_wayland
*wsi_wl
,
362 struct wsi_wl_display
*display
,
363 struct wl_display
*wl_display
,
364 bool get_format_list
)
366 VkResult result
= VK_SUCCESS
;
367 memset(display
, 0, sizeof(*display
));
369 display
->wsi_wl
= wsi_wl
;
370 display
->wl_display
= wl_display
;
372 if (get_format_list
) {
373 if (!u_vector_init(&display
->formats
, sizeof(VkFormat
), 8) ||
374 !u_vector_init(&display
->dmabuf
.modifiers
.argb8888
,
375 sizeof(uint64_t), 32) ||
376 !u_vector_init(&display
->dmabuf
.modifiers
.xrgb8888
,
377 sizeof(uint64_t), 32)) {
378 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
383 display
->queue
= wl_display_create_queue(wl_display
);
384 if (!display
->queue
) {
385 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
389 display
->wl_display_wrapper
= wl_proxy_create_wrapper(wl_display
);
390 if (!display
->wl_display_wrapper
) {
391 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
395 wl_proxy_set_queue((struct wl_proxy
*) display
->wl_display_wrapper
,
398 struct wl_registry
*registry
=
399 wl_display_get_registry(display
->wl_display_wrapper
);
401 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
405 wl_registry_add_listener(registry
, ®istry_listener
, display
);
407 /* Round-trip to get the wl_drm global */
408 wl_display_roundtrip_queue(display
->wl_display
, display
->queue
);
410 if (!display
->drm
.wl_drm
) {
411 result
= VK_ERROR_SURFACE_LOST_KHR
;
415 /* Round-trip to get wl_drm formats and capabilities */
416 wl_display_roundtrip_queue(display
->wl_display
, display
->queue
);
418 /* We need prime support */
419 if (!(display
->drm
.capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
420 result
= VK_ERROR_SURFACE_LOST_KHR
;
424 /* We don't need this anymore */
425 wl_registry_destroy(registry
);
427 display
->refcount
= 0;
433 wl_registry_destroy(registry
);
436 wsi_wl_display_finish(display
);
441 wsi_wl_display_create(struct wsi_wayland
*wsi
, struct wl_display
*wl_display
,
442 struct wsi_wl_display
**display_out
)
444 struct wsi_wl_display
*display
=
445 vk_alloc(wsi
->alloc
, sizeof(*display
), 8,
446 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
448 return VK_ERROR_OUT_OF_HOST_MEMORY
;
450 VkResult result
= wsi_wl_display_init(wsi
, display
, wl_display
, true);
451 if (result
!= VK_SUCCESS
) {
452 vk_free(wsi
->alloc
, display
);
457 *display_out
= display
;
462 static struct wsi_wl_display
*
463 wsi_wl_display_ref(struct wsi_wl_display
*display
)
470 wsi_wl_display_unref(struct wsi_wl_display
*display
)
472 if (display
->refcount
-- > 1)
475 struct wsi_wayland
*wsi
= display
->wsi_wl
;
476 wsi_wl_display_finish(display
);
477 vk_free(wsi
->alloc
, display
);
481 wsi_wl_get_presentation_support(struct wsi_device
*wsi_device
,
482 struct wl_display
*wl_display
)
484 struct wsi_wayland
*wsi
=
485 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
487 struct wsi_wl_display display
;
488 VkResult ret
= wsi_wl_display_init(wsi
, &display
, wl_display
, false);
489 if (ret
== VK_SUCCESS
)
490 wsi_wl_display_finish(&display
);
492 return ret
== VK_SUCCESS
;
496 wsi_wl_surface_get_support(VkIcdSurfaceBase
*surface
,
497 struct wsi_device
*wsi_device
,
498 uint32_t queueFamilyIndex
,
499 VkBool32
* pSupported
)
506 static const VkPresentModeKHR present_modes
[] = {
507 VK_PRESENT_MODE_MAILBOX_KHR
,
508 VK_PRESENT_MODE_FIFO_KHR
,
512 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase
*surface
,
513 struct wsi_device
*wsi_device
,
514 VkSurfaceCapabilitiesKHR
* caps
)
516 /* For true mailbox mode, we need at least 4 images:
517 * 1) One to scan out from
518 * 2) One to have queued for scan-out
519 * 3) One to be currently held by the Wayland compositor
520 * 4) One to render to
522 caps
->minImageCount
= 4;
523 /* There is no real maximum */
524 caps
->maxImageCount
= 0;
526 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
527 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
528 caps
->maxImageExtent
= (VkExtent2D
) {
529 wsi_device
->maxImageDimension2D
,
530 wsi_device
->maxImageDimension2D
,
533 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
534 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
535 caps
->maxImageArrayLayers
= 1;
537 caps
->supportedCompositeAlpha
=
538 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
|
539 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
541 caps
->supportedUsageFlags
=
542 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
543 VK_IMAGE_USAGE_SAMPLED_BIT
|
544 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
545 VK_IMAGE_USAGE_STORAGE_BIT
|
546 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
552 wsi_wl_surface_get_capabilities2(VkIcdSurfaceBase
*surface
,
553 struct wsi_device
*wsi_device
,
554 const void *info_next
,
555 VkSurfaceCapabilities2KHR
* caps
)
557 assert(caps
->sType
== VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
);
560 wsi_wl_surface_get_capabilities(surface
, wsi_device
,
561 &caps
->surfaceCapabilities
);
563 vk_foreach_struct(ext
, caps
->pNext
) {
564 switch (ext
->sType
) {
565 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR
: {
566 VkSurfaceProtectedCapabilitiesKHR
*protected = (void *)ext
;
567 protected->supportsProtected
= VK_FALSE
;
581 wsi_wl_surface_get_formats(VkIcdSurfaceBase
*icd_surface
,
582 struct wsi_device
*wsi_device
,
583 uint32_t* pSurfaceFormatCount
,
584 VkSurfaceFormatKHR
* pSurfaceFormats
)
586 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
587 struct wsi_wayland
*wsi
=
588 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
590 struct wsi_wl_display display
;
591 if (wsi_wl_display_init(wsi
, &display
, surface
->display
, true))
592 return VK_ERROR_SURFACE_LOST_KHR
;
594 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
597 u_vector_foreach(disp_fmt
, &display
.formats
) {
598 vk_outarray_append(&out
, out_fmt
) {
599 out_fmt
->format
= *disp_fmt
;
600 out_fmt
->colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
604 wsi_wl_display_finish(&display
);
606 return vk_outarray_status(&out
);
610 wsi_wl_surface_get_formats2(VkIcdSurfaceBase
*icd_surface
,
611 struct wsi_device
*wsi_device
,
612 const void *info_next
,
613 uint32_t* pSurfaceFormatCount
,
614 VkSurfaceFormat2KHR
* pSurfaceFormats
)
616 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
617 struct wsi_wayland
*wsi
=
618 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
620 struct wsi_wl_display display
;
621 if (wsi_wl_display_init(wsi
, &display
, surface
->display
, true))
622 return VK_ERROR_SURFACE_LOST_KHR
;
624 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
627 u_vector_foreach(disp_fmt
, &display
.formats
) {
628 vk_outarray_append(&out
, out_fmt
) {
629 out_fmt
->surfaceFormat
.format
= *disp_fmt
;
630 out_fmt
->surfaceFormat
.colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
634 wsi_wl_display_finish(&display
);
636 return vk_outarray_status(&out
);
640 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
641 uint32_t* pPresentModeCount
,
642 VkPresentModeKHR
* pPresentModes
)
644 if (pPresentModes
== NULL
) {
645 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
649 *pPresentModeCount
= MIN2(*pPresentModeCount
, ARRAY_SIZE(present_modes
));
650 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
652 if (*pPresentModeCount
< ARRAY_SIZE(present_modes
))
653 return VK_INCOMPLETE
;
659 wsi_wl_surface_get_present_rectangles(VkIcdSurfaceBase
*surface
,
660 struct wsi_device
*wsi_device
,
661 uint32_t* pRectCount
,
664 VK_OUTARRAY_MAKE(out
, pRects
, pRectCount
);
666 vk_outarray_append(&out
, rect
) {
667 /* We don't know a size so just return the usual "I don't know." */
670 .extent
= { -1, -1 },
674 return vk_outarray_status(&out
);
677 VkResult
wsi_create_wl_surface(const VkAllocationCallbacks
*pAllocator
,
678 const VkWaylandSurfaceCreateInfoKHR
*pCreateInfo
,
679 VkSurfaceKHR
*pSurface
)
681 VkIcdSurfaceWayland
*surface
;
683 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
684 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
686 return VK_ERROR_OUT_OF_HOST_MEMORY
;
688 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_WAYLAND
;
689 surface
->display
= pCreateInfo
->display
;
690 surface
->surface
= pCreateInfo
->surface
;
692 *pSurface
= VkIcdSurfaceBase_to_handle(&surface
->base
);
697 struct wsi_wl_image
{
698 struct wsi_image base
;
699 struct wl_buffer
* buffer
;
703 struct wsi_wl_swapchain
{
704 struct wsi_swapchain base
;
706 struct wsi_wl_display
*display
;
708 struct wl_surface
* surface
;
709 uint32_t surface_version
;
711 /* non-NULL when wl_drm should be used for wl_buffer creation; otherwise,
712 * zwp_linux_dmabuf_v1 should be used.
714 struct wl_drm
* drm_wrapper
;
716 struct wl_callback
* frame
;
722 uint32_t num_drm_modifiers
;
723 const uint64_t * drm_modifiers
;
725 VkPresentModeKHR present_mode
;
728 struct wsi_wl_image images
[0];
730 WSI_DEFINE_NONDISP_HANDLE_CASTS(wsi_wl_swapchain
, VkSwapchainKHR
)
732 static struct wsi_image
*
733 wsi_wl_swapchain_get_wsi_image(struct wsi_swapchain
*wsi_chain
,
734 uint32_t image_index
)
736 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
737 return &chain
->images
[image_index
].base
;
741 wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain
*wsi_chain
,
742 const VkAcquireNextImageInfoKHR
*info
,
743 uint32_t *image_index
)
745 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
747 int ret
= wl_display_dispatch_queue_pending(chain
->display
->wl_display
,
748 chain
->display
->queue
);
749 /* XXX: I'm not sure if out-of-date is the right error here. If
750 * wl_display_dispatch_queue_pending fails it most likely means we got
751 * kicked by the server so this seems more-or-less correct.
754 return VK_ERROR_OUT_OF_DATE_KHR
;
757 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
758 if (!chain
->images
[i
].busy
) {
759 /* We found a non-busy image */
761 chain
->images
[i
].busy
= true;
766 /* This time we do a blocking dispatch because we can't go
767 * anywhere until we get an event.
769 int ret
= wl_display_roundtrip_queue(chain
->display
->wl_display
,
770 chain
->display
->queue
);
772 return VK_ERROR_OUT_OF_DATE_KHR
;
777 frame_handle_done(void *data
, struct wl_callback
*callback
, uint32_t serial
)
779 struct wsi_wl_swapchain
*chain
= data
;
782 chain
->fifo_ready
= true;
784 wl_callback_destroy(callback
);
787 static const struct wl_callback_listener frame_listener
= {
792 wsi_wl_swapchain_queue_present(struct wsi_swapchain
*wsi_chain
,
793 uint32_t image_index
,
794 const VkPresentRegionKHR
*damage
)
796 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
798 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
799 while (!chain
->fifo_ready
) {
800 int ret
= wl_display_dispatch_queue(chain
->display
->wl_display
,
801 chain
->display
->queue
);
803 return VK_ERROR_OUT_OF_DATE_KHR
;
807 assert(image_index
< chain
->base
.image_count
);
808 wl_surface_attach(chain
->surface
, chain
->images
[image_index
].buffer
, 0, 0);
810 if (chain
->surface_version
>= 4 && damage
&&
811 damage
->pRectangles
&& damage
->rectangleCount
> 0) {
812 for (unsigned i
= 0; i
< damage
->rectangleCount
; i
++) {
813 const VkRectLayerKHR
*rect
= &damage
->pRectangles
[i
];
814 assert(rect
->layer
== 0);
815 wl_surface_damage_buffer(chain
->surface
,
816 rect
->offset
.x
, rect
->offset
.y
,
817 rect
->extent
.width
, rect
->extent
.height
);
820 wl_surface_damage(chain
->surface
, 0, 0, INT32_MAX
, INT32_MAX
);
823 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
824 chain
->frame
= wl_surface_frame(chain
->surface
);
825 wl_callback_add_listener(chain
->frame
, &frame_listener
, chain
);
826 chain
->fifo_ready
= false;
829 chain
->images
[image_index
].busy
= true;
830 wl_surface_commit(chain
->surface
);
831 wl_display_flush(chain
->display
->wl_display
);
837 buffer_handle_release(void *data
, struct wl_buffer
*buffer
)
839 struct wsi_wl_image
*image
= data
;
841 assert(image
->buffer
== buffer
);
846 static const struct wl_buffer_listener buffer_listener
= {
847 buffer_handle_release
,
851 wsi_wl_image_init(struct wsi_wl_swapchain
*chain
,
852 struct wsi_wl_image
*image
,
853 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
854 const VkAllocationCallbacks
* pAllocator
)
856 struct wsi_wl_display
*display
= chain
->display
;
859 result
= wsi_create_native_image(&chain
->base
, pCreateInfo
,
860 chain
->num_drm_modifiers
> 0 ? 1 : 0,
861 &chain
->num_drm_modifiers
,
862 &chain
->drm_modifiers
, &image
->base
);
864 if (result
!= VK_SUCCESS
)
867 if (!chain
->drm_wrapper
) {
868 /* Only request modifiers if we have dmabuf, else it must be implicit. */
869 assert(display
->dmabuf
.wl_dmabuf
);
870 assert(image
->base
.drm_modifier
!= DRM_FORMAT_MOD_INVALID
);
872 struct zwp_linux_buffer_params_v1
*params
=
873 zwp_linux_dmabuf_v1_create_params(display
->dmabuf
.wl_dmabuf
);
874 wl_proxy_set_queue((struct wl_proxy
*) params
, chain
->display
->queue
);
876 for (int i
= 0; i
< image
->base
.num_planes
; i
++) {
877 zwp_linux_buffer_params_v1_add(params
,
880 image
->base
.offsets
[i
],
881 image
->base
.row_pitches
[i
],
882 image
->base
.drm_modifier
>> 32,
883 image
->base
.drm_modifier
& 0xffffffff);
884 close(image
->base
.fds
[i
]);
888 zwp_linux_buffer_params_v1_create_immed(params
,
890 chain
->extent
.height
,
893 zwp_linux_buffer_params_v1_destroy(params
);
895 /* Without passing modifiers, we can't have multi-plane RGB images. */
896 assert(image
->base
.num_planes
== 1);
897 assert(image
->base
.drm_modifier
== DRM_FORMAT_MOD_INVALID
);
900 wl_drm_create_prime_buffer(chain
->drm_wrapper
,
901 image
->base
.fds
[0], /* name */
903 chain
->extent
.height
,
905 image
->base
.offsets
[0],
906 image
->base
.row_pitches
[0],
907 0, 0, 0, 0 /* unused */);
908 close(image
->base
.fds
[0]);
914 wl_buffer_add_listener(image
->buffer
, &buffer_listener
, image
);
919 wsi_destroy_image(&chain
->base
, &image
->base
);
925 wsi_wl_swapchain_destroy(struct wsi_swapchain
*wsi_chain
,
926 const VkAllocationCallbacks
*pAllocator
)
928 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
930 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
931 if (chain
->images
[i
].buffer
) {
932 wl_buffer_destroy(chain
->images
[i
].buffer
);
933 wsi_destroy_image(&chain
->base
, &chain
->images
[i
].base
);
938 wl_callback_destroy(chain
->frame
);
940 wl_proxy_wrapper_destroy(chain
->surface
);
941 if (chain
->drm_wrapper
)
942 wl_proxy_wrapper_destroy(chain
->drm_wrapper
);
945 wsi_wl_display_unref(chain
->display
);
947 wsi_swapchain_finish(&chain
->base
);
949 vk_free(pAllocator
, chain
);
955 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
957 struct wsi_device
*wsi_device
,
958 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
959 const VkAllocationCallbacks
* pAllocator
,
960 struct wsi_swapchain
**swapchain_out
)
962 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
963 struct wsi_wayland
*wsi
=
964 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
965 struct wsi_wl_swapchain
*chain
;
968 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
970 int num_images
= pCreateInfo
->minImageCount
;
972 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
973 chain
= vk_alloc(pAllocator
, size
, 8,
974 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
976 return VK_ERROR_OUT_OF_HOST_MEMORY
;
978 result
= wsi_swapchain_init(wsi_device
, &chain
->base
, device
,
979 pCreateInfo
, pAllocator
);
980 if (result
!= VK_SUCCESS
) {
981 vk_free(pAllocator
, chain
);
985 /* Mark a bunch of stuff as NULL. This way we can just call
986 * destroy_swapchain for cleanup.
988 for (uint32_t i
= 0; i
< num_images
; i
++)
989 chain
->images
[i
].buffer
= NULL
;
990 chain
->surface
= NULL
;
991 chain
->drm_wrapper
= NULL
;
994 bool alpha
= pCreateInfo
->compositeAlpha
==
995 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
997 chain
->base
.destroy
= wsi_wl_swapchain_destroy
;
998 chain
->base
.get_wsi_image
= wsi_wl_swapchain_get_wsi_image
;
999 chain
->base
.acquire_next_image
= wsi_wl_swapchain_acquire_next_image
;
1000 chain
->base
.queue_present
= wsi_wl_swapchain_queue_present
;
1001 chain
->base
.present_mode
= pCreateInfo
->presentMode
;
1002 chain
->base
.image_count
= num_images
;
1003 chain
->extent
= pCreateInfo
->imageExtent
;
1004 chain
->vk_format
= pCreateInfo
->imageFormat
;
1005 chain
->drm_format
= wl_drm_format_for_vk_format(chain
->vk_format
, alpha
);
1007 if (pCreateInfo
->oldSwapchain
) {
1008 /* If we have an oldSwapchain parameter, copy the display struct over
1009 * from the old one so we don't have to fully re-initialize it.
1011 WSI_FROM_HANDLE(wsi_wl_swapchain
, old_chain
, pCreateInfo
->oldSwapchain
);
1012 chain
->display
= wsi_wl_display_ref(old_chain
->display
);
1014 chain
->display
= NULL
;
1015 result
= wsi_wl_display_create(wsi
, surface
->display
, &chain
->display
);
1016 if (result
!= VK_SUCCESS
)
1020 chain
->surface
= wl_proxy_create_wrapper(surface
->surface
);
1021 if (!chain
->surface
) {
1022 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1025 wl_proxy_set_queue((struct wl_proxy
*) chain
->surface
,
1026 chain
->display
->queue
);
1027 chain
->surface_version
= wl_proxy_get_version((void *)surface
->surface
);
1029 chain
->num_drm_modifiers
= 0;
1030 chain
->drm_modifiers
= 0;
1032 /* Use explicit DRM format modifiers when both the server and the driver
1035 if (chain
->display
->dmabuf
.wl_dmabuf
&&
1036 chain
->base
.wsi
->supports_modifiers
) {
1037 struct u_vector
*modifiers
;
1038 switch (chain
->drm_format
) {
1039 case WL_DRM_FORMAT_ARGB8888
:
1040 modifiers
= &chain
->display
->dmabuf
.modifiers
.argb8888
;
1042 case WL_DRM_FORMAT_XRGB8888
:
1043 modifiers
= &chain
->display
->dmabuf
.modifiers
.xrgb8888
;
1051 chain
->drm_modifiers
= u_vector_tail(modifiers
);
1052 chain
->num_drm_modifiers
= u_vector_length(modifiers
);
1056 /* When there are explicit DRM format modifiers, we must use
1057 * zwp_linux_dmabuf_v1 for wl_buffer creation. Otherwise, we must use
1060 if (!chain
->num_drm_modifiers
) {
1061 chain
->drm_wrapper
=
1062 wl_proxy_create_wrapper(chain
->display
->drm
.wl_drm
);
1063 if (!chain
->drm_wrapper
) {
1064 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1067 wl_proxy_set_queue((struct wl_proxy
*) chain
->drm_wrapper
,
1068 chain
->display
->queue
);
1071 chain
->fifo_ready
= true;
1073 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
1074 result
= wsi_wl_image_init(chain
, &chain
->images
[i
],
1075 pCreateInfo
, pAllocator
);
1076 if (result
!= VK_SUCCESS
)
1078 chain
->images
[i
].busy
= false;
1081 *swapchain_out
= &chain
->base
;
1086 wsi_wl_swapchain_destroy(&chain
->base
, pAllocator
);
1092 wsi_wl_init_wsi(struct wsi_device
*wsi_device
,
1093 const VkAllocationCallbacks
*alloc
,
1094 VkPhysicalDevice physical_device
)
1096 struct wsi_wayland
*wsi
;
1099 wsi
= vk_alloc(alloc
, sizeof(*wsi
), 8,
1100 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
1102 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1106 wsi
->physical_device
= physical_device
;
1108 wsi
->wsi
= wsi_device
;
1110 wsi
->base
.get_support
= wsi_wl_surface_get_support
;
1111 wsi
->base
.get_capabilities2
= wsi_wl_surface_get_capabilities2
;
1112 wsi
->base
.get_formats
= wsi_wl_surface_get_formats
;
1113 wsi
->base
.get_formats2
= wsi_wl_surface_get_formats2
;
1114 wsi
->base
.get_present_modes
= wsi_wl_surface_get_present_modes
;
1115 wsi
->base
.get_present_rectangles
= wsi_wl_surface_get_present_rectangles
;
1116 wsi
->base
.create_swapchain
= wsi_wl_surface_create_swapchain
;
1118 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = &wsi
->base
;
1123 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = NULL
;
1129 wsi_wl_finish_wsi(struct wsi_device
*wsi_device
,
1130 const VkAllocationCallbacks
*alloc
)
1132 struct wsi_wayland
*wsi
=
1133 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
1137 vk_free(alloc
, wsi
);