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>
35 #include "drm-uapi/drm_fourcc.h"
38 #include "wsi_common_private.h"
39 #include "wsi_common_wayland.h"
40 #include "wayland-drm-client-protocol.h"
41 #include "linux-dmabuf-unstable-v1-client-protocol.h"
43 #include <util/hash_table.h>
44 #include <util/timespec.h>
45 #include <util/u_vector.h>
47 #define typed_memcpy(dest, src, count) ({ \
48 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
49 memcpy((dest), (src), (count) * sizeof(*(src))); \
54 struct wsi_wl_display_drm
{
55 struct wl_drm
* wl_drm
;
56 struct u_vector formats
;
57 uint32_t capabilities
;
60 struct wsi_wl_display_dmabuf
{
61 struct zwp_linux_dmabuf_v1
* wl_dmabuf
;
62 struct u_vector formats
;
64 struct u_vector argb8888
;
65 struct u_vector xrgb8888
;
69 struct wsi_wl_display
{
70 /* The real wl_display */
71 struct wl_display
* wl_display
;
72 /* Actually a proxy wrapper around the event queue */
73 struct wl_display
* wl_display_wrapper
;
74 struct wl_event_queue
* queue
;
76 struct wsi_wl_display_drm drm
;
77 struct wsi_wl_display_dmabuf dmabuf
;
79 struct wsi_wayland
*wsi_wl
;
81 /* Points to formats in wsi_wl_display_drm or wsi_wl_display_dmabuf */
82 struct u_vector
* formats
;
84 /* Only used for displays created by wsi_wl_display_create */
89 struct wsi_interface base
;
91 struct wsi_device
*wsi
;
93 const VkAllocationCallbacks
*alloc
;
94 VkPhysicalDevice physical_device
;
98 wsi_wl_display_add_vk_format(struct wsi_wl_display
*display
,
99 struct u_vector
*formats
, VkFormat format
)
101 /* Don't add a format that's already in the list */
103 u_vector_foreach(f
, formats
)
107 /* Don't add formats that aren't renderable. */
108 VkFormatProperties props
;
110 display
->wsi_wl
->wsi
->GetPhysicalDeviceFormatProperties(display
->wsi_wl
->physical_device
,
112 if (!(props
.optimalTilingFeatures
& VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
))
115 f
= u_vector_add(formats
);
121 wsi_wl_display_add_wl_format(struct wsi_wl_display
*display
,
122 struct u_vector
*formats
, uint32_t wl_format
)
126 case WL_DRM_FORMAT_ABGR4444
:
127 case WL_DRM_FORMAT_XBGR4444
:
128 wsi_wl_display_add_vk_format(display
, formats
,
129 VK_FORMAT_R4G4B4A4_UNORM
);
131 case WL_DRM_FORMAT_BGR565
:
132 wsi_wl_display_add_vk_format(display
, formats
,
133 VK_FORMAT_R5G6B5_UNORM
);
135 case WL_DRM_FORMAT_ABGR1555
:
136 case WL_DRM_FORMAT_XBGR1555
:
137 wsi_wl_display_add_vk_format(display
, formats
,
138 VK_FORMAT_R5G5B5A1_UNORM
);
140 case WL_DRM_FORMAT_XBGR8888
:
141 wsi_wl_display_add_vk_format(display
, formats
,
142 VK_FORMAT_R8G8B8_UNORM
);
144 case WL_DRM_FORMAT_ABGR8888
:
145 wsi_wl_display_add_vk_format(display
, formats
,
146 VK_FORMAT_R8G8B8A8_UNORM
);
148 case WL_DRM_FORMAT_ABGR2101010
:
149 case WL_DRM_FORMAT_XBGR2101010
:
150 wsi_wl_display_add_vk_format(display
, formats
,
151 VK_FORMAT_R10G10B10A2_UNORM
);
153 case WL_DRM_FORMAT_ARGB4444
:
154 case WL_DRM_FORMAT_XRGB4444
:
155 wsi_wl_display_add_vk_format(display
, formats
,
156 VK_FORMAT_B4G4R4A4_UNORM
);
158 case WL_DRM_FORMAT_RGB565
:
159 wsi_wl_display_add_vk_format(display
, formats
,
160 VK_FORMAT_B5G6R5_UNORM
);
162 case WL_DRM_FORMAT_ARGB1555
:
163 case WL_DRM_FORMAT_XRGB1555
:
164 wsi_wl_display_add_vk_format(display
, formats
,
165 VK_FORMAT_B5G5R5A1_UNORM
);
168 case WL_DRM_FORMAT_XRGB8888
:
169 wsi_wl_display_add_vk_format(display
, formats
,
170 VK_FORMAT_B8G8R8_SRGB
);
171 wsi_wl_display_add_vk_format(display
, formats
,
172 VK_FORMAT_B8G8R8_UNORM
);
174 case WL_DRM_FORMAT_ARGB8888
:
175 wsi_wl_display_add_vk_format(display
, formats
,
176 VK_FORMAT_B8G8R8A8_SRGB
);
177 wsi_wl_display_add_vk_format(display
, formats
,
178 VK_FORMAT_B8G8R8A8_UNORM
);
181 case WL_DRM_FORMAT_ARGB2101010
:
182 case WL_DRM_FORMAT_XRGB2101010
:
183 wsi_wl_display_add_vk_format(display
, formats
,
184 VK_FORMAT_B10G10R10A2_UNORM
);
191 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *name
)
196 wl_drm_format_for_vk_format(VkFormat vk_format
, bool alpha
)
199 /* TODO: Figure out what all the formats mean and make this table
203 case VK_FORMAT_R4G4B4A4_UNORM
:
204 return alpha
? WL_DRM_FORMAT_ABGR4444
: WL_DRM_FORMAT_XBGR4444
;
205 case VK_FORMAT_R5G6B5_UNORM
:
206 return WL_DRM_FORMAT_BGR565
;
207 case VK_FORMAT_R5G5B5A1_UNORM
:
208 return alpha
? WL_DRM_FORMAT_ABGR1555
: WL_DRM_FORMAT_XBGR1555
;
209 case VK_FORMAT_R8G8B8_UNORM
:
210 return WL_DRM_FORMAT_XBGR8888
;
211 case VK_FORMAT_R8G8B8A8_UNORM
:
212 return alpha
? WL_DRM_FORMAT_ABGR8888
: WL_DRM_FORMAT_XBGR8888
;
213 case VK_FORMAT_R10G10B10A2_UNORM
:
214 return alpha
? WL_DRM_FORMAT_ABGR2101010
: WL_DRM_FORMAT_XBGR2101010
;
215 case VK_FORMAT_B4G4R4A4_UNORM
:
216 return alpha
? WL_DRM_FORMAT_ARGB4444
: WL_DRM_FORMAT_XRGB4444
;
217 case VK_FORMAT_B5G6R5_UNORM
:
218 return WL_DRM_FORMAT_RGB565
;
219 case VK_FORMAT_B5G5R5A1_UNORM
:
220 return alpha
? WL_DRM_FORMAT_XRGB1555
: WL_DRM_FORMAT_XRGB1555
;
222 case VK_FORMAT_B8G8R8_UNORM
:
223 case VK_FORMAT_B8G8R8_SRGB
:
224 return WL_DRM_FORMAT_BGRX8888
;
225 case VK_FORMAT_B8G8R8A8_UNORM
:
226 case VK_FORMAT_B8G8R8A8_SRGB
:
227 return alpha
? WL_DRM_FORMAT_ARGB8888
: WL_DRM_FORMAT_XRGB8888
;
229 case VK_FORMAT_B10G10R10A2_UNORM
:
230 return alpha
? WL_DRM_FORMAT_ARGB2101010
: WL_DRM_FORMAT_XRGB2101010
;
234 assert(!"Unsupported Vulkan format");
240 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t wl_format
)
242 struct wsi_wl_display
*display
= data
;
243 if (display
->drm
.formats
.element_size
== 0)
246 wsi_wl_display_add_wl_format(display
, &display
->drm
.formats
, wl_format
);
250 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
255 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t capabilities
)
257 struct wsi_wl_display
*display
= data
;
259 display
->drm
.capabilities
= capabilities
;
262 static const struct wl_drm_listener drm_listener
= {
265 drm_handle_authenticated
,
266 drm_handle_capabilities
,
270 dmabuf_handle_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
273 /* Formats are implicitly advertised by the modifier event, so we ignore
278 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
279 uint32_t format
, uint32_t modifier_hi
,
280 uint32_t modifier_lo
)
282 struct wsi_wl_display
*display
= data
;
283 uint64_t *mod
= NULL
;
285 /* If we're not fetching formats, don't fetch modifiers either. */
286 if (display
->dmabuf
.formats
.element_size
== 0)
289 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
290 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
294 case WL_DRM_FORMAT_ARGB8888
:
295 wsi_wl_display_add_wl_format(display
, &display
->dmabuf
.formats
, format
);
296 mod
= u_vector_add(&display
->dmabuf
.modifiers
.argb8888
);
298 case WL_DRM_FORMAT_XRGB8888
:
299 wsi_wl_display_add_wl_format(display
, &display
->dmabuf
.formats
, format
);
300 mod
= u_vector_add(&display
->dmabuf
.modifiers
.xrgb8888
);
309 *mod
= (uint64_t) modifier_hi
<< 32;
310 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
313 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
314 dmabuf_handle_format
,
315 dmabuf_handle_modifier
,
319 registry_handle_global(void *data
, struct wl_registry
*registry
,
320 uint32_t name
, const char *interface
, uint32_t version
)
322 struct wsi_wl_display
*display
= data
;
324 if (strcmp(interface
, "wl_drm") == 0) {
325 assert(display
->drm
.wl_drm
== NULL
);
327 assert(version
>= 2);
328 display
->drm
.wl_drm
=
329 wl_registry_bind(registry
, name
, &wl_drm_interface
, 2);
330 wl_drm_add_listener(display
->drm
.wl_drm
, &drm_listener
, display
);
331 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3 &&
332 display
->wsi_wl
->wsi
->supports_modifiers
) {
333 display
->dmabuf
.wl_dmabuf
=
334 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
, 3);
335 zwp_linux_dmabuf_v1_add_listener(display
->dmabuf
.wl_dmabuf
,
336 &dmabuf_listener
, display
);
341 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
345 static const struct wl_registry_listener registry_listener
= {
346 registry_handle_global
,
347 registry_handle_global_remove
351 wsi_wl_display_finish(struct wsi_wl_display
*display
)
353 assert(display
->refcount
== 0);
355 u_vector_finish(&display
->drm
.formats
);
356 u_vector_finish(&display
->dmabuf
.formats
);
357 u_vector_finish(&display
->dmabuf
.modifiers
.argb8888
);
358 u_vector_finish(&display
->dmabuf
.modifiers
.xrgb8888
);
359 if (display
->drm
.wl_drm
)
360 wl_drm_destroy(display
->drm
.wl_drm
);
361 if (display
->dmabuf
.wl_dmabuf
)
362 zwp_linux_dmabuf_v1_destroy(display
->dmabuf
.wl_dmabuf
);
363 if (display
->wl_display_wrapper
)
364 wl_proxy_wrapper_destroy(display
->wl_display_wrapper
);
366 wl_event_queue_destroy(display
->queue
);
370 wsi_wl_display_init(struct wsi_wayland
*wsi_wl
,
371 struct wsi_wl_display
*display
,
372 struct wl_display
*wl_display
,
373 bool get_format_list
)
375 VkResult result
= VK_SUCCESS
;
376 memset(display
, 0, sizeof(*display
));
378 display
->wsi_wl
= wsi_wl
;
379 display
->wl_display
= wl_display
;
381 if (get_format_list
) {
382 if (!u_vector_init(&display
->drm
.formats
, sizeof(VkFormat
), 8) ||
383 !u_vector_init(&display
->dmabuf
.formats
, sizeof(VkFormat
), 8) ||
384 !u_vector_init(&display
->dmabuf
.modifiers
.argb8888
,
385 sizeof(uint64_t), 32) ||
386 !u_vector_init(&display
->dmabuf
.modifiers
.xrgb8888
,
387 sizeof(uint64_t), 32)) {
388 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
393 display
->queue
= wl_display_create_queue(wl_display
);
394 if (!display
->queue
) {
395 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
399 display
->wl_display_wrapper
= wl_proxy_create_wrapper(wl_display
);
400 if (!display
->wl_display_wrapper
) {
401 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
405 wl_proxy_set_queue((struct wl_proxy
*) display
->wl_display_wrapper
,
408 struct wl_registry
*registry
=
409 wl_display_get_registry(display
->wl_display_wrapper
);
411 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
415 wl_registry_add_listener(registry
, ®istry_listener
, display
);
417 /* Round-trip to get wl_drms and zwp_linux_dmabuf_v1 globals */
418 wl_display_roundtrip_queue(display
->wl_display
, display
->queue
);
420 /* Round-trip again to get formats, modifiers and capabilities */
421 if (display
->drm
.wl_drm
|| display
->dmabuf
.wl_dmabuf
)
422 wl_display_roundtrip_queue(display
->wl_display
, display
->queue
);
424 /* We need prime support for wl_drm */
425 if (display
->drm
.wl_drm
&&
426 (display
->drm
.capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
427 display
->formats
= &display
->drm
.formats
;
428 } else if (display
->dmabuf
.wl_dmabuf
) {
429 display
->formats
= &display
->dmabuf
.formats
;
432 if (!display
->formats
) {
433 result
= VK_ERROR_SURFACE_LOST_KHR
;
437 /* We don't need this anymore */
438 wl_registry_destroy(registry
);
440 display
->refcount
= 0;
446 wl_registry_destroy(registry
);
449 wsi_wl_display_finish(display
);
454 wsi_wl_display_create(struct wsi_wayland
*wsi
, struct wl_display
*wl_display
,
455 struct wsi_wl_display
**display_out
)
457 struct wsi_wl_display
*display
=
458 vk_alloc(wsi
->alloc
, sizeof(*display
), 8,
459 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
461 return VK_ERROR_OUT_OF_HOST_MEMORY
;
463 VkResult result
= wsi_wl_display_init(wsi
, display
, wl_display
, true);
464 if (result
!= VK_SUCCESS
) {
465 vk_free(wsi
->alloc
, display
);
470 *display_out
= display
;
475 static struct wsi_wl_display
*
476 wsi_wl_display_ref(struct wsi_wl_display
*display
)
483 wsi_wl_display_unref(struct wsi_wl_display
*display
)
485 if (display
->refcount
-- > 1)
488 struct wsi_wayland
*wsi
= display
->wsi_wl
;
489 wsi_wl_display_finish(display
);
490 vk_free(wsi
->alloc
, display
);
494 wsi_wl_get_presentation_support(struct wsi_device
*wsi_device
,
495 struct wl_display
*wl_display
)
497 struct wsi_wayland
*wsi
=
498 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
500 struct wsi_wl_display display
;
501 VkResult ret
= wsi_wl_display_init(wsi
, &display
, wl_display
, false);
502 if (ret
== VK_SUCCESS
)
503 wsi_wl_display_finish(&display
);
505 return ret
== VK_SUCCESS
;
509 wsi_wl_surface_get_support(VkIcdSurfaceBase
*surface
,
510 struct wsi_device
*wsi_device
,
511 uint32_t queueFamilyIndex
,
512 VkBool32
* pSupported
)
519 static const VkPresentModeKHR present_modes
[] = {
520 VK_PRESENT_MODE_MAILBOX_KHR
,
521 VK_PRESENT_MODE_FIFO_KHR
,
525 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase
*surface
,
526 struct wsi_device
*wsi_device
,
527 VkSurfaceCapabilitiesKHR
* caps
)
529 /* For true mailbox mode, we need at least 4 images:
530 * 1) One to scan out from
531 * 2) One to have queued for scan-out
532 * 3) One to be currently held by the Wayland compositor
533 * 4) One to render to
535 caps
->minImageCount
= 4;
536 /* There is no real maximum */
537 caps
->maxImageCount
= 0;
539 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
540 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
541 caps
->maxImageExtent
= (VkExtent2D
) {
542 wsi_device
->maxImageDimension2D
,
543 wsi_device
->maxImageDimension2D
,
546 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
547 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
548 caps
->maxImageArrayLayers
= 1;
550 caps
->supportedCompositeAlpha
=
551 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
|
552 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
554 caps
->supportedUsageFlags
=
555 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
556 VK_IMAGE_USAGE_SAMPLED_BIT
|
557 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
558 VK_IMAGE_USAGE_STORAGE_BIT
|
559 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
565 wsi_wl_surface_get_capabilities2(VkIcdSurfaceBase
*surface
,
566 struct wsi_device
*wsi_device
,
567 const void *info_next
,
568 VkSurfaceCapabilities2KHR
* caps
)
570 assert(caps
->sType
== VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
);
573 wsi_wl_surface_get_capabilities(surface
, wsi_device
,
574 &caps
->surfaceCapabilities
);
576 vk_foreach_struct(ext
, caps
->pNext
) {
577 switch (ext
->sType
) {
578 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR
: {
579 VkSurfaceProtectedCapabilitiesKHR
*protected = (void *)ext
;
580 protected->supportsProtected
= VK_FALSE
;
594 wsi_wl_surface_get_formats(VkIcdSurfaceBase
*icd_surface
,
595 struct wsi_device
*wsi_device
,
596 uint32_t* pSurfaceFormatCount
,
597 VkSurfaceFormatKHR
* pSurfaceFormats
)
599 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
600 struct wsi_wayland
*wsi
=
601 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
603 struct wsi_wl_display display
;
604 if (wsi_wl_display_init(wsi
, &display
, surface
->display
, true))
605 return VK_ERROR_SURFACE_LOST_KHR
;
607 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
610 u_vector_foreach(disp_fmt
, display
.formats
) {
611 vk_outarray_append(&out
, out_fmt
) {
612 out_fmt
->format
= *disp_fmt
;
613 out_fmt
->colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
617 wsi_wl_display_finish(&display
);
619 return vk_outarray_status(&out
);
623 wsi_wl_surface_get_formats2(VkIcdSurfaceBase
*icd_surface
,
624 struct wsi_device
*wsi_device
,
625 const void *info_next
,
626 uint32_t* pSurfaceFormatCount
,
627 VkSurfaceFormat2KHR
* pSurfaceFormats
)
629 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
630 struct wsi_wayland
*wsi
=
631 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
633 struct wsi_wl_display display
;
634 if (wsi_wl_display_init(wsi
, &display
, surface
->display
, true))
635 return VK_ERROR_SURFACE_LOST_KHR
;
637 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
640 u_vector_foreach(disp_fmt
, display
.formats
) {
641 vk_outarray_append(&out
, out_fmt
) {
642 out_fmt
->surfaceFormat
.format
= *disp_fmt
;
643 out_fmt
->surfaceFormat
.colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
647 wsi_wl_display_finish(&display
);
649 return vk_outarray_status(&out
);
653 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
654 uint32_t* pPresentModeCount
,
655 VkPresentModeKHR
* pPresentModes
)
657 if (pPresentModes
== NULL
) {
658 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
662 *pPresentModeCount
= MIN2(*pPresentModeCount
, ARRAY_SIZE(present_modes
));
663 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
665 if (*pPresentModeCount
< ARRAY_SIZE(present_modes
))
666 return VK_INCOMPLETE
;
672 wsi_wl_surface_get_present_rectangles(VkIcdSurfaceBase
*surface
,
673 struct wsi_device
*wsi_device
,
674 uint32_t* pRectCount
,
677 VK_OUTARRAY_MAKE(out
, pRects
, pRectCount
);
679 vk_outarray_append(&out
, rect
) {
680 /* We don't know a size so just return the usual "I don't know." */
683 .extent
= { -1, -1 },
687 return vk_outarray_status(&out
);
690 VkResult
wsi_create_wl_surface(const VkAllocationCallbacks
*pAllocator
,
691 const VkWaylandSurfaceCreateInfoKHR
*pCreateInfo
,
692 VkSurfaceKHR
*pSurface
)
694 VkIcdSurfaceWayland
*surface
;
696 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
697 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
699 return VK_ERROR_OUT_OF_HOST_MEMORY
;
701 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_WAYLAND
;
702 surface
->display
= pCreateInfo
->display
;
703 surface
->surface
= pCreateInfo
->surface
;
705 *pSurface
= VkIcdSurfaceBase_to_handle(&surface
->base
);
710 struct wsi_wl_image
{
711 struct wsi_image base
;
712 struct wl_buffer
* buffer
;
716 struct wsi_wl_swapchain
{
717 struct wsi_swapchain base
;
719 struct wsi_wl_display
*display
;
721 struct wl_surface
* surface
;
722 uint32_t surface_version
;
724 /* non-NULL when wl_drm should be used for wl_buffer creation; otherwise,
725 * zwp_linux_dmabuf_v1 should be used.
727 struct wl_drm
* drm_wrapper
;
729 struct wl_callback
* frame
;
735 uint32_t num_drm_modifiers
;
736 const uint64_t * drm_modifiers
;
738 VkPresentModeKHR present_mode
;
741 struct wsi_wl_image images
[0];
743 WSI_DEFINE_NONDISP_HANDLE_CASTS(wsi_wl_swapchain
, VkSwapchainKHR
)
745 static struct wsi_image
*
746 wsi_wl_swapchain_get_wsi_image(struct wsi_swapchain
*wsi_chain
,
747 uint32_t image_index
)
749 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
750 return &chain
->images
[image_index
].base
;
754 wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain
*wsi_chain
,
755 const VkAcquireNextImageInfoKHR
*info
,
756 uint32_t *image_index
)
758 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
759 struct timespec start_time
, end_time
;
760 struct timespec rel_timeout
;
761 int wl_fd
= wl_display_get_fd(chain
->display
->wl_display
);
763 timespec_from_nsec(&rel_timeout
, info
->timeout
);
765 clock_gettime(CLOCK_MONOTONIC
, &start_time
);
766 timespec_add(&end_time
, &rel_timeout
, &start_time
);
769 /* Try to dispatch potential events. */
770 int ret
= wl_display_dispatch_queue_pending(chain
->display
->wl_display
,
771 chain
->display
->queue
);
773 return VK_ERROR_OUT_OF_DATE_KHR
;
775 /* Try to find a free image. */
776 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
777 if (!chain
->images
[i
].busy
) {
778 /* We found a non-busy image */
780 chain
->images
[i
].busy
= true;
785 /* Check for timeout. */
786 struct timespec current_time
;
787 clock_gettime(CLOCK_MONOTONIC
, ¤t_time
);
788 if (timespec_after(¤t_time
, &end_time
))
791 /* Try to read events from the server. */
792 ret
= wl_display_prepare_read_queue(chain
->display
->wl_display
,
793 chain
->display
->queue
);
795 /* Another thread might have read events for our queue already. Go
796 * back to dispatch them.
800 return VK_ERROR_OUT_OF_DATE_KHR
;
803 struct pollfd pollfd
= {
807 timespec_sub(&rel_timeout
, &end_time
, ¤t_time
);
808 ret
= ppoll(&pollfd
, 1, &rel_timeout
, NULL
);
811 wl_display_cancel_read(chain
->display
->wl_display
);
813 /* If ppoll() was interrupted, try again. */
814 if (lerrno
== EINTR
|| lerrno
== EAGAIN
)
816 return VK_ERROR_OUT_OF_DATE_KHR
;
822 ret
= wl_display_read_events(chain
->display
->wl_display
);
824 return VK_ERROR_OUT_OF_DATE_KHR
;
829 frame_handle_done(void *data
, struct wl_callback
*callback
, uint32_t serial
)
831 struct wsi_wl_swapchain
*chain
= data
;
834 chain
->fifo_ready
= true;
836 wl_callback_destroy(callback
);
839 static const struct wl_callback_listener frame_listener
= {
844 wsi_wl_swapchain_queue_present(struct wsi_swapchain
*wsi_chain
,
845 uint32_t image_index
,
846 const VkPresentRegionKHR
*damage
)
848 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
850 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
851 while (!chain
->fifo_ready
) {
852 int ret
= wl_display_dispatch_queue(chain
->display
->wl_display
,
853 chain
->display
->queue
);
855 return VK_ERROR_OUT_OF_DATE_KHR
;
859 assert(image_index
< chain
->base
.image_count
);
860 wl_surface_attach(chain
->surface
, chain
->images
[image_index
].buffer
, 0, 0);
862 if (chain
->surface_version
>= 4 && damage
&&
863 damage
->pRectangles
&& damage
->rectangleCount
> 0) {
864 for (unsigned i
= 0; i
< damage
->rectangleCount
; i
++) {
865 const VkRectLayerKHR
*rect
= &damage
->pRectangles
[i
];
866 assert(rect
->layer
== 0);
867 wl_surface_damage_buffer(chain
->surface
,
868 rect
->offset
.x
, rect
->offset
.y
,
869 rect
->extent
.width
, rect
->extent
.height
);
872 wl_surface_damage(chain
->surface
, 0, 0, INT32_MAX
, INT32_MAX
);
875 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
876 chain
->frame
= wl_surface_frame(chain
->surface
);
877 wl_callback_add_listener(chain
->frame
, &frame_listener
, chain
);
878 chain
->fifo_ready
= false;
881 chain
->images
[image_index
].busy
= true;
882 wl_surface_commit(chain
->surface
);
883 wl_display_flush(chain
->display
->wl_display
);
889 buffer_handle_release(void *data
, struct wl_buffer
*buffer
)
891 struct wsi_wl_image
*image
= data
;
893 assert(image
->buffer
== buffer
);
898 static const struct wl_buffer_listener buffer_listener
= {
899 buffer_handle_release
,
903 wsi_wl_image_init(struct wsi_wl_swapchain
*chain
,
904 struct wsi_wl_image
*image
,
905 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
906 const VkAllocationCallbacks
* pAllocator
)
908 struct wsi_wl_display
*display
= chain
->display
;
911 result
= wsi_create_native_image(&chain
->base
, pCreateInfo
,
912 chain
->num_drm_modifiers
> 0 ? 1 : 0,
913 &chain
->num_drm_modifiers
,
914 &chain
->drm_modifiers
, &image
->base
);
916 if (result
!= VK_SUCCESS
)
919 if (!chain
->drm_wrapper
) {
920 /* Only request modifiers if we have dmabuf, else it must be implicit. */
921 assert(display
->dmabuf
.wl_dmabuf
);
922 assert(image
->base
.drm_modifier
!= DRM_FORMAT_MOD_INVALID
);
924 struct zwp_linux_buffer_params_v1
*params
=
925 zwp_linux_dmabuf_v1_create_params(display
->dmabuf
.wl_dmabuf
);
926 wl_proxy_set_queue((struct wl_proxy
*) params
, chain
->display
->queue
);
928 for (int i
= 0; i
< image
->base
.num_planes
; i
++) {
929 zwp_linux_buffer_params_v1_add(params
,
932 image
->base
.offsets
[i
],
933 image
->base
.row_pitches
[i
],
934 image
->base
.drm_modifier
>> 32,
935 image
->base
.drm_modifier
& 0xffffffff);
936 close(image
->base
.fds
[i
]);
940 zwp_linux_buffer_params_v1_create_immed(params
,
942 chain
->extent
.height
,
945 zwp_linux_buffer_params_v1_destroy(params
);
947 /* Without passing modifiers, we can't have multi-plane RGB images. */
948 assert(image
->base
.num_planes
== 1);
949 assert(image
->base
.drm_modifier
== DRM_FORMAT_MOD_INVALID
);
952 wl_drm_create_prime_buffer(chain
->drm_wrapper
,
953 image
->base
.fds
[0], /* name */
955 chain
->extent
.height
,
957 image
->base
.offsets
[0],
958 image
->base
.row_pitches
[0],
959 0, 0, 0, 0 /* unused */);
960 close(image
->base
.fds
[0]);
966 wl_buffer_add_listener(image
->buffer
, &buffer_listener
, image
);
971 wsi_destroy_image(&chain
->base
, &image
->base
);
977 wsi_wl_swapchain_destroy(struct wsi_swapchain
*wsi_chain
,
978 const VkAllocationCallbacks
*pAllocator
)
980 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
982 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
983 if (chain
->images
[i
].buffer
) {
984 wl_buffer_destroy(chain
->images
[i
].buffer
);
985 wsi_destroy_image(&chain
->base
, &chain
->images
[i
].base
);
990 wl_callback_destroy(chain
->frame
);
992 wl_proxy_wrapper_destroy(chain
->surface
);
993 if (chain
->drm_wrapper
)
994 wl_proxy_wrapper_destroy(chain
->drm_wrapper
);
997 wsi_wl_display_unref(chain
->display
);
999 wsi_swapchain_finish(&chain
->base
);
1001 vk_free(pAllocator
, chain
);
1007 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
1009 struct wsi_device
*wsi_device
,
1010 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
1011 const VkAllocationCallbacks
* pAllocator
,
1012 struct wsi_swapchain
**swapchain_out
)
1014 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
1015 struct wsi_wayland
*wsi
=
1016 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
1017 struct wsi_wl_swapchain
*chain
;
1020 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
1022 int num_images
= pCreateInfo
->minImageCount
;
1024 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
1025 chain
= vk_alloc(pAllocator
, size
, 8,
1026 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1028 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1030 result
= wsi_swapchain_init(wsi_device
, &chain
->base
, device
,
1031 pCreateInfo
, pAllocator
);
1032 if (result
!= VK_SUCCESS
) {
1033 vk_free(pAllocator
, chain
);
1037 /* Mark a bunch of stuff as NULL. This way we can just call
1038 * destroy_swapchain for cleanup.
1040 for (uint32_t i
= 0; i
< num_images
; i
++)
1041 chain
->images
[i
].buffer
= NULL
;
1042 chain
->surface
= NULL
;
1043 chain
->drm_wrapper
= NULL
;
1044 chain
->frame
= NULL
;
1046 bool alpha
= pCreateInfo
->compositeAlpha
==
1047 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
1049 chain
->base
.destroy
= wsi_wl_swapchain_destroy
;
1050 chain
->base
.get_wsi_image
= wsi_wl_swapchain_get_wsi_image
;
1051 chain
->base
.acquire_next_image
= wsi_wl_swapchain_acquire_next_image
;
1052 chain
->base
.queue_present
= wsi_wl_swapchain_queue_present
;
1053 chain
->base
.present_mode
= wsi_swapchain_get_present_mode(wsi_device
, pCreateInfo
);
1054 chain
->base
.image_count
= num_images
;
1055 chain
->extent
= pCreateInfo
->imageExtent
;
1056 chain
->vk_format
= pCreateInfo
->imageFormat
;
1057 chain
->drm_format
= wl_drm_format_for_vk_format(chain
->vk_format
, alpha
);
1059 if (pCreateInfo
->oldSwapchain
) {
1060 /* If we have an oldSwapchain parameter, copy the display struct over
1061 * from the old one so we don't have to fully re-initialize it.
1063 WSI_FROM_HANDLE(wsi_wl_swapchain
, old_chain
, pCreateInfo
->oldSwapchain
);
1064 chain
->display
= wsi_wl_display_ref(old_chain
->display
);
1066 chain
->display
= NULL
;
1067 result
= wsi_wl_display_create(wsi
, surface
->display
, &chain
->display
);
1068 if (result
!= VK_SUCCESS
)
1072 chain
->surface
= wl_proxy_create_wrapper(surface
->surface
);
1073 if (!chain
->surface
) {
1074 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1077 wl_proxy_set_queue((struct wl_proxy
*) chain
->surface
,
1078 chain
->display
->queue
);
1079 chain
->surface_version
= wl_proxy_get_version((void *)surface
->surface
);
1081 chain
->num_drm_modifiers
= 0;
1082 chain
->drm_modifiers
= 0;
1084 /* Use explicit DRM format modifiers when both the server and the driver
1087 if (chain
->display
->dmabuf
.wl_dmabuf
&&
1088 chain
->base
.wsi
->supports_modifiers
) {
1089 struct u_vector
*modifiers
;
1090 switch (chain
->drm_format
) {
1091 case WL_DRM_FORMAT_ARGB8888
:
1092 modifiers
= &chain
->display
->dmabuf
.modifiers
.argb8888
;
1094 case WL_DRM_FORMAT_XRGB8888
:
1095 modifiers
= &chain
->display
->dmabuf
.modifiers
.xrgb8888
;
1103 chain
->drm_modifiers
= u_vector_tail(modifiers
);
1104 chain
->num_drm_modifiers
= u_vector_length(modifiers
);
1108 /* When there are explicit DRM format modifiers, we must use
1109 * zwp_linux_dmabuf_v1 for wl_buffer creation. Otherwise, we must use
1112 if (!chain
->num_drm_modifiers
) {
1113 assert(chain
->display
->drm
.wl_drm
);
1115 chain
->drm_wrapper
=
1116 wl_proxy_create_wrapper(chain
->display
->drm
.wl_drm
);
1117 if (!chain
->drm_wrapper
) {
1118 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1121 wl_proxy_set_queue((struct wl_proxy
*) chain
->drm_wrapper
,
1122 chain
->display
->queue
);
1125 chain
->fifo_ready
= true;
1127 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
1128 result
= wsi_wl_image_init(chain
, &chain
->images
[i
],
1129 pCreateInfo
, pAllocator
);
1130 if (result
!= VK_SUCCESS
)
1132 chain
->images
[i
].busy
= false;
1135 *swapchain_out
= &chain
->base
;
1140 wsi_wl_swapchain_destroy(&chain
->base
, pAllocator
);
1146 wsi_wl_init_wsi(struct wsi_device
*wsi_device
,
1147 const VkAllocationCallbacks
*alloc
,
1148 VkPhysicalDevice physical_device
)
1150 struct wsi_wayland
*wsi
;
1153 wsi
= vk_alloc(alloc
, sizeof(*wsi
), 8,
1154 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
1156 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1160 wsi
->physical_device
= physical_device
;
1162 wsi
->wsi
= wsi_device
;
1164 wsi
->base
.get_support
= wsi_wl_surface_get_support
;
1165 wsi
->base
.get_capabilities2
= wsi_wl_surface_get_capabilities2
;
1166 wsi
->base
.get_formats
= wsi_wl_surface_get_formats
;
1167 wsi
->base
.get_formats2
= wsi_wl_surface_get_formats2
;
1168 wsi
->base
.get_present_modes
= wsi_wl_surface_get_present_modes
;
1169 wsi
->base
.get_present_rectangles
= wsi_wl_surface_get_present_rectangles
;
1170 wsi
->base
.create_swapchain
= wsi_wl_surface_create_swapchain
;
1172 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = &wsi
->base
;
1177 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = NULL
;
1183 wsi_wl_finish_wsi(struct wsi_device
*wsi_device
,
1184 const VkAllocationCallbacks
*alloc
)
1186 struct wsi_wayland
*wsi
=
1187 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
1191 vk_free(alloc
, wsi
);