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 struct u_vector formats
;
55 uint32_t capabilities
;
58 struct wsi_wl_display_dmabuf
{
59 struct zwp_linux_dmabuf_v1
* wl_dmabuf
;
60 struct u_vector formats
;
62 struct u_vector argb8888
;
63 struct u_vector xrgb8888
;
67 struct wsi_wl_display
{
68 /* The real wl_display */
69 struct wl_display
* wl_display
;
70 /* Actually a proxy wrapper around the event queue */
71 struct wl_display
* wl_display_wrapper
;
72 struct wl_event_queue
* queue
;
74 struct wsi_wl_display_drm drm
;
75 struct wsi_wl_display_dmabuf dmabuf
;
77 struct wsi_wayland
*wsi_wl
;
79 /* Points to formats in wsi_wl_display_drm or wsi_wl_display_dmabuf */
80 struct u_vector
* formats
;
82 /* Only used for displays created by wsi_wl_display_create */
87 struct wsi_interface base
;
89 struct wsi_device
*wsi
;
91 const VkAllocationCallbacks
*alloc
;
92 VkPhysicalDevice physical_device
;
96 wsi_wl_display_add_vk_format(struct wsi_wl_display
*display
,
97 struct u_vector
*formats
, VkFormat format
)
99 /* Don't add a format that's already in the list */
101 u_vector_foreach(f
, formats
)
105 /* Don't add formats that aren't renderable. */
106 VkFormatProperties props
;
108 display
->wsi_wl
->wsi
->GetPhysicalDeviceFormatProperties(display
->wsi_wl
->physical_device
,
110 if (!(props
.optimalTilingFeatures
& VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
))
113 f
= u_vector_add(formats
);
119 wsi_wl_display_add_wl_format(struct wsi_wl_display
*display
,
120 struct u_vector
*formats
, uint32_t wl_format
)
124 case WL_DRM_FORMAT_ABGR4444
:
125 case WL_DRM_FORMAT_XBGR4444
:
126 wsi_wl_display_add_vk_format(display
, formats
,
127 VK_FORMAT_R4G4B4A4_UNORM
);
129 case WL_DRM_FORMAT_BGR565
:
130 wsi_wl_display_add_vk_format(display
, formats
,
131 VK_FORMAT_R5G6B5_UNORM
);
133 case WL_DRM_FORMAT_ABGR1555
:
134 case WL_DRM_FORMAT_XBGR1555
:
135 wsi_wl_display_add_vk_format(display
, formats
,
136 VK_FORMAT_R5G5B5A1_UNORM
);
138 case WL_DRM_FORMAT_XBGR8888
:
139 wsi_wl_display_add_vk_format(display
, formats
,
140 VK_FORMAT_R8G8B8_UNORM
);
142 case WL_DRM_FORMAT_ABGR8888
:
143 wsi_wl_display_add_vk_format(display
, formats
,
144 VK_FORMAT_R8G8B8A8_UNORM
);
146 case WL_DRM_FORMAT_ABGR2101010
:
147 case WL_DRM_FORMAT_XBGR2101010
:
148 wsi_wl_display_add_vk_format(display
, formats
,
149 VK_FORMAT_R10G10B10A2_UNORM
);
151 case WL_DRM_FORMAT_ARGB4444
:
152 case WL_DRM_FORMAT_XRGB4444
:
153 wsi_wl_display_add_vk_format(display
, formats
,
154 VK_FORMAT_B4G4R4A4_UNORM
);
156 case WL_DRM_FORMAT_RGB565
:
157 wsi_wl_display_add_vk_format(display
, formats
,
158 VK_FORMAT_B5G6R5_UNORM
);
160 case WL_DRM_FORMAT_ARGB1555
:
161 case WL_DRM_FORMAT_XRGB1555
:
162 wsi_wl_display_add_vk_format(display
, formats
,
163 VK_FORMAT_B5G5R5A1_UNORM
);
166 case WL_DRM_FORMAT_XRGB8888
:
167 wsi_wl_display_add_vk_format(display
, formats
,
168 VK_FORMAT_B8G8R8_SRGB
);
169 wsi_wl_display_add_vk_format(display
, formats
,
170 VK_FORMAT_B8G8R8_UNORM
);
172 case WL_DRM_FORMAT_ARGB8888
:
173 wsi_wl_display_add_vk_format(display
, formats
,
174 VK_FORMAT_B8G8R8A8_SRGB
);
175 wsi_wl_display_add_vk_format(display
, formats
,
176 VK_FORMAT_B8G8R8A8_UNORM
);
179 case WL_DRM_FORMAT_ARGB2101010
:
180 case WL_DRM_FORMAT_XRGB2101010
:
181 wsi_wl_display_add_vk_format(display
, formats
,
182 VK_FORMAT_B10G10R10A2_UNORM
);
189 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *name
)
194 wl_drm_format_for_vk_format(VkFormat vk_format
, bool alpha
)
197 /* TODO: Figure out what all the formats mean and make this table
201 case VK_FORMAT_R4G4B4A4_UNORM
:
202 return alpha
? WL_DRM_FORMAT_ABGR4444
: WL_DRM_FORMAT_XBGR4444
;
203 case VK_FORMAT_R5G6B5_UNORM
:
204 return WL_DRM_FORMAT_BGR565
;
205 case VK_FORMAT_R5G5B5A1_UNORM
:
206 return alpha
? WL_DRM_FORMAT_ABGR1555
: WL_DRM_FORMAT_XBGR1555
;
207 case VK_FORMAT_R8G8B8_UNORM
:
208 return WL_DRM_FORMAT_XBGR8888
;
209 case VK_FORMAT_R8G8B8A8_UNORM
:
210 return alpha
? WL_DRM_FORMAT_ABGR8888
: WL_DRM_FORMAT_XBGR8888
;
211 case VK_FORMAT_R10G10B10A2_UNORM
:
212 return alpha
? WL_DRM_FORMAT_ABGR2101010
: WL_DRM_FORMAT_XBGR2101010
;
213 case VK_FORMAT_B4G4R4A4_UNORM
:
214 return alpha
? WL_DRM_FORMAT_ARGB4444
: WL_DRM_FORMAT_XRGB4444
;
215 case VK_FORMAT_B5G6R5_UNORM
:
216 return WL_DRM_FORMAT_RGB565
;
217 case VK_FORMAT_B5G5R5A1_UNORM
:
218 return alpha
? WL_DRM_FORMAT_XRGB1555
: WL_DRM_FORMAT_XRGB1555
;
220 case VK_FORMAT_B8G8R8_UNORM
:
221 case VK_FORMAT_B8G8R8_SRGB
:
222 return WL_DRM_FORMAT_BGRX8888
;
223 case VK_FORMAT_B8G8R8A8_UNORM
:
224 case VK_FORMAT_B8G8R8A8_SRGB
:
225 return alpha
? WL_DRM_FORMAT_ARGB8888
: WL_DRM_FORMAT_XRGB8888
;
227 case VK_FORMAT_B10G10R10A2_UNORM
:
228 return alpha
? WL_DRM_FORMAT_ARGB2101010
: WL_DRM_FORMAT_XRGB2101010
;
232 assert(!"Unsupported Vulkan format");
238 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t wl_format
)
240 struct wsi_wl_display
*display
= data
;
241 if (display
->drm
.formats
.element_size
== 0)
244 wsi_wl_display_add_wl_format(display
, &display
->drm
.formats
, wl_format
);
248 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
253 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t capabilities
)
255 struct wsi_wl_display
*display
= data
;
257 display
->drm
.capabilities
= capabilities
;
260 static const struct wl_drm_listener drm_listener
= {
263 drm_handle_authenticated
,
264 drm_handle_capabilities
,
268 dmabuf_handle_format(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
271 /* Formats are implicitly advertised by the modifier event, so we ignore
276 dmabuf_handle_modifier(void *data
, struct zwp_linux_dmabuf_v1
*dmabuf
,
277 uint32_t format
, uint32_t modifier_hi
,
278 uint32_t modifier_lo
)
280 struct wsi_wl_display
*display
= data
;
281 uint64_t *mod
= NULL
;
283 /* If we're not fetching formats, don't fetch modifiers either. */
284 if (display
->dmabuf
.formats
.element_size
== 0)
287 if (modifier_hi
== (DRM_FORMAT_MOD_INVALID
>> 32) &&
288 modifier_lo
== (DRM_FORMAT_MOD_INVALID
& 0xffffffff))
292 case WL_DRM_FORMAT_ARGB8888
:
293 wsi_wl_display_add_wl_format(display
, &display
->dmabuf
.formats
, format
);
294 mod
= u_vector_add(&display
->dmabuf
.modifiers
.argb8888
);
296 case WL_DRM_FORMAT_XRGB8888
:
297 wsi_wl_display_add_wl_format(display
, &display
->dmabuf
.formats
, format
);
298 mod
= u_vector_add(&display
->dmabuf
.modifiers
.xrgb8888
);
307 *mod
= (uint64_t) modifier_hi
<< 32;
308 *mod
|= (uint64_t) (modifier_lo
& 0xffffffff);
311 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener
= {
312 dmabuf_handle_format
,
313 dmabuf_handle_modifier
,
317 registry_handle_global(void *data
, struct wl_registry
*registry
,
318 uint32_t name
, const char *interface
, uint32_t version
)
320 struct wsi_wl_display
*display
= data
;
322 if (strcmp(interface
, "wl_drm") == 0) {
323 assert(display
->drm
.wl_drm
== NULL
);
325 assert(version
>= 2);
326 display
->drm
.wl_drm
=
327 wl_registry_bind(registry
, name
, &wl_drm_interface
, 2);
328 wl_drm_add_listener(display
->drm
.wl_drm
, &drm_listener
, display
);
329 } else if (strcmp(interface
, "zwp_linux_dmabuf_v1") == 0 && version
>= 3 &&
330 display
->wsi_wl
->wsi
->supports_modifiers
) {
331 display
->dmabuf
.wl_dmabuf
=
332 wl_registry_bind(registry
, name
, &zwp_linux_dmabuf_v1_interface
, 3);
333 zwp_linux_dmabuf_v1_add_listener(display
->dmabuf
.wl_dmabuf
,
334 &dmabuf_listener
, display
);
339 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
343 static const struct wl_registry_listener registry_listener
= {
344 registry_handle_global
,
345 registry_handle_global_remove
349 wsi_wl_display_finish(struct wsi_wl_display
*display
)
351 assert(display
->refcount
== 0);
353 u_vector_finish(&display
->drm
.formats
);
354 u_vector_finish(&display
->dmabuf
.formats
);
355 u_vector_finish(&display
->dmabuf
.modifiers
.argb8888
);
356 u_vector_finish(&display
->dmabuf
.modifiers
.xrgb8888
);
357 if (display
->drm
.wl_drm
)
358 wl_drm_destroy(display
->drm
.wl_drm
);
359 if (display
->dmabuf
.wl_dmabuf
)
360 zwp_linux_dmabuf_v1_destroy(display
->dmabuf
.wl_dmabuf
);
361 if (display
->wl_display_wrapper
)
362 wl_proxy_wrapper_destroy(display
->wl_display_wrapper
);
364 wl_event_queue_destroy(display
->queue
);
368 wsi_wl_display_init(struct wsi_wayland
*wsi_wl
,
369 struct wsi_wl_display
*display
,
370 struct wl_display
*wl_display
,
371 bool get_format_list
)
373 VkResult result
= VK_SUCCESS
;
374 memset(display
, 0, sizeof(*display
));
376 display
->wsi_wl
= wsi_wl
;
377 display
->wl_display
= wl_display
;
379 if (get_format_list
) {
380 if (!u_vector_init(&display
->drm
.formats
, sizeof(VkFormat
), 8) ||
381 !u_vector_init(&display
->dmabuf
.formats
, sizeof(VkFormat
), 8) ||
382 !u_vector_init(&display
->dmabuf
.modifiers
.argb8888
,
383 sizeof(uint64_t), 32) ||
384 !u_vector_init(&display
->dmabuf
.modifiers
.xrgb8888
,
385 sizeof(uint64_t), 32)) {
386 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
391 display
->queue
= wl_display_create_queue(wl_display
);
392 if (!display
->queue
) {
393 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
397 display
->wl_display_wrapper
= wl_proxy_create_wrapper(wl_display
);
398 if (!display
->wl_display_wrapper
) {
399 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
403 wl_proxy_set_queue((struct wl_proxy
*) display
->wl_display_wrapper
,
406 struct wl_registry
*registry
=
407 wl_display_get_registry(display
->wl_display_wrapper
);
409 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
413 wl_registry_add_listener(registry
, ®istry_listener
, display
);
415 /* Round-trip to get wl_drms and zwp_linux_dmabuf_v1 globals */
416 wl_display_roundtrip_queue(display
->wl_display
, display
->queue
);
418 /* Round-trip again to get formats, modifiers and capabilities */
419 if (display
->drm
.wl_drm
|| display
->dmabuf
.wl_dmabuf
)
420 wl_display_roundtrip_queue(display
->wl_display
, display
->queue
);
422 /* We need prime support for wl_drm */
423 if (display
->drm
.wl_drm
&&
424 (display
->drm
.capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
425 display
->formats
= &display
->drm
.formats
;
426 } else if (display
->dmabuf
.wl_dmabuf
) {
427 display
->formats
= &display
->dmabuf
.formats
;
430 if (!display
->formats
) {
431 result
= VK_ERROR_SURFACE_LOST_KHR
;
435 /* We don't need this anymore */
436 wl_registry_destroy(registry
);
438 display
->refcount
= 0;
444 wl_registry_destroy(registry
);
447 wsi_wl_display_finish(display
);
452 wsi_wl_display_create(struct wsi_wayland
*wsi
, struct wl_display
*wl_display
,
453 struct wsi_wl_display
**display_out
)
455 struct wsi_wl_display
*display
=
456 vk_alloc(wsi
->alloc
, sizeof(*display
), 8,
457 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
459 return VK_ERROR_OUT_OF_HOST_MEMORY
;
461 VkResult result
= wsi_wl_display_init(wsi
, display
, wl_display
, true);
462 if (result
!= VK_SUCCESS
) {
463 vk_free(wsi
->alloc
, display
);
468 *display_out
= display
;
473 static struct wsi_wl_display
*
474 wsi_wl_display_ref(struct wsi_wl_display
*display
)
481 wsi_wl_display_unref(struct wsi_wl_display
*display
)
483 if (display
->refcount
-- > 1)
486 struct wsi_wayland
*wsi
= display
->wsi_wl
;
487 wsi_wl_display_finish(display
);
488 vk_free(wsi
->alloc
, display
);
492 wsi_wl_get_presentation_support(struct wsi_device
*wsi_device
,
493 struct wl_display
*wl_display
)
495 struct wsi_wayland
*wsi
=
496 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
498 struct wsi_wl_display display
;
499 VkResult ret
= wsi_wl_display_init(wsi
, &display
, wl_display
, false);
500 if (ret
== VK_SUCCESS
)
501 wsi_wl_display_finish(&display
);
503 return ret
== VK_SUCCESS
;
507 wsi_wl_surface_get_support(VkIcdSurfaceBase
*surface
,
508 struct wsi_device
*wsi_device
,
509 uint32_t queueFamilyIndex
,
510 VkBool32
* pSupported
)
517 static const VkPresentModeKHR present_modes
[] = {
518 VK_PRESENT_MODE_MAILBOX_KHR
,
519 VK_PRESENT_MODE_FIFO_KHR
,
523 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase
*surface
,
524 struct wsi_device
*wsi_device
,
525 VkSurfaceCapabilitiesKHR
* caps
)
527 /* For true mailbox mode, we need at least 4 images:
528 * 1) One to scan out from
529 * 2) One to have queued for scan-out
530 * 3) One to be currently held by the Wayland compositor
531 * 4) One to render to
533 caps
->minImageCount
= 4;
534 /* There is no real maximum */
535 caps
->maxImageCount
= 0;
537 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
538 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
539 caps
->maxImageExtent
= (VkExtent2D
) {
540 wsi_device
->maxImageDimension2D
,
541 wsi_device
->maxImageDimension2D
,
544 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
545 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
546 caps
->maxImageArrayLayers
= 1;
548 caps
->supportedCompositeAlpha
=
549 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
|
550 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
552 caps
->supportedUsageFlags
=
553 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
554 VK_IMAGE_USAGE_SAMPLED_BIT
|
555 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
556 VK_IMAGE_USAGE_STORAGE_BIT
|
557 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
563 wsi_wl_surface_get_capabilities2(VkIcdSurfaceBase
*surface
,
564 struct wsi_device
*wsi_device
,
565 const void *info_next
,
566 VkSurfaceCapabilities2KHR
* caps
)
568 assert(caps
->sType
== VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
);
571 wsi_wl_surface_get_capabilities(surface
, wsi_device
,
572 &caps
->surfaceCapabilities
);
574 vk_foreach_struct(ext
, caps
->pNext
) {
575 switch (ext
->sType
) {
576 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR
: {
577 VkSurfaceProtectedCapabilitiesKHR
*protected = (void *)ext
;
578 protected->supportsProtected
= VK_FALSE
;
592 wsi_wl_surface_get_formats(VkIcdSurfaceBase
*icd_surface
,
593 struct wsi_device
*wsi_device
,
594 uint32_t* pSurfaceFormatCount
,
595 VkSurfaceFormatKHR
* pSurfaceFormats
)
597 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
598 struct wsi_wayland
*wsi
=
599 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
601 struct wsi_wl_display display
;
602 if (wsi_wl_display_init(wsi
, &display
, surface
->display
, true))
603 return VK_ERROR_SURFACE_LOST_KHR
;
605 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
608 u_vector_foreach(disp_fmt
, display
.formats
) {
609 vk_outarray_append(&out
, out_fmt
) {
610 out_fmt
->format
= *disp_fmt
;
611 out_fmt
->colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
615 wsi_wl_display_finish(&display
);
617 return vk_outarray_status(&out
);
621 wsi_wl_surface_get_formats2(VkIcdSurfaceBase
*icd_surface
,
622 struct wsi_device
*wsi_device
,
623 const void *info_next
,
624 uint32_t* pSurfaceFormatCount
,
625 VkSurfaceFormat2KHR
* pSurfaceFormats
)
627 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
628 struct wsi_wayland
*wsi
=
629 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
631 struct wsi_wl_display display
;
632 if (wsi_wl_display_init(wsi
, &display
, surface
->display
, true))
633 return VK_ERROR_SURFACE_LOST_KHR
;
635 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
638 u_vector_foreach(disp_fmt
, display
.formats
) {
639 vk_outarray_append(&out
, out_fmt
) {
640 out_fmt
->surfaceFormat
.format
= *disp_fmt
;
641 out_fmt
->surfaceFormat
.colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
645 wsi_wl_display_finish(&display
);
647 return vk_outarray_status(&out
);
651 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
652 uint32_t* pPresentModeCount
,
653 VkPresentModeKHR
* pPresentModes
)
655 if (pPresentModes
== NULL
) {
656 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
660 *pPresentModeCount
= MIN2(*pPresentModeCount
, ARRAY_SIZE(present_modes
));
661 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
663 if (*pPresentModeCount
< ARRAY_SIZE(present_modes
))
664 return VK_INCOMPLETE
;
670 wsi_wl_surface_get_present_rectangles(VkIcdSurfaceBase
*surface
,
671 struct wsi_device
*wsi_device
,
672 uint32_t* pRectCount
,
675 VK_OUTARRAY_MAKE(out
, pRects
, pRectCount
);
677 vk_outarray_append(&out
, rect
) {
678 /* We don't know a size so just return the usual "I don't know." */
681 .extent
= { -1, -1 },
685 return vk_outarray_status(&out
);
688 VkResult
wsi_create_wl_surface(const VkAllocationCallbacks
*pAllocator
,
689 const VkWaylandSurfaceCreateInfoKHR
*pCreateInfo
,
690 VkSurfaceKHR
*pSurface
)
692 VkIcdSurfaceWayland
*surface
;
694 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
695 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
697 return VK_ERROR_OUT_OF_HOST_MEMORY
;
699 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_WAYLAND
;
700 surface
->display
= pCreateInfo
->display
;
701 surface
->surface
= pCreateInfo
->surface
;
703 *pSurface
= VkIcdSurfaceBase_to_handle(&surface
->base
);
708 struct wsi_wl_image
{
709 struct wsi_image base
;
710 struct wl_buffer
* buffer
;
714 struct wsi_wl_swapchain
{
715 struct wsi_swapchain base
;
717 struct wsi_wl_display
*display
;
719 struct wl_surface
* surface
;
720 uint32_t surface_version
;
722 /* non-NULL when wl_drm should be used for wl_buffer creation; otherwise,
723 * zwp_linux_dmabuf_v1 should be used.
725 struct wl_drm
* drm_wrapper
;
727 struct wl_callback
* frame
;
733 uint32_t num_drm_modifiers
;
734 const uint64_t * drm_modifiers
;
736 VkPresentModeKHR present_mode
;
739 struct wsi_wl_image images
[0];
741 WSI_DEFINE_NONDISP_HANDLE_CASTS(wsi_wl_swapchain
, VkSwapchainKHR
)
743 static struct wsi_image
*
744 wsi_wl_swapchain_get_wsi_image(struct wsi_swapchain
*wsi_chain
,
745 uint32_t image_index
)
747 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
748 return &chain
->images
[image_index
].base
;
752 wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain
*wsi_chain
,
753 const VkAcquireNextImageInfoKHR
*info
,
754 uint32_t *image_index
)
756 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
758 int ret
= wl_display_dispatch_queue_pending(chain
->display
->wl_display
,
759 chain
->display
->queue
);
760 /* XXX: I'm not sure if out-of-date is the right error here. If
761 * wl_display_dispatch_queue_pending fails it most likely means we got
762 * kicked by the server so this seems more-or-less correct.
765 return VK_ERROR_OUT_OF_DATE_KHR
;
768 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
769 if (!chain
->images
[i
].busy
) {
770 /* We found a non-busy image */
772 chain
->images
[i
].busy
= true;
777 /* We now have to do a blocking dispatch, because all our images
778 * are in use and we cannot return one until the server does. However,
779 * if the client has requested non-blocking ANI, then we tell it up front
780 * that we have nothing to return.
782 if (info
->timeout
== 0)
785 int ret
= wl_display_roundtrip_queue(chain
->display
->wl_display
,
786 chain
->display
->queue
);
788 return VK_ERROR_OUT_OF_DATE_KHR
;
793 frame_handle_done(void *data
, struct wl_callback
*callback
, uint32_t serial
)
795 struct wsi_wl_swapchain
*chain
= data
;
798 chain
->fifo_ready
= true;
800 wl_callback_destroy(callback
);
803 static const struct wl_callback_listener frame_listener
= {
808 wsi_wl_swapchain_queue_present(struct wsi_swapchain
*wsi_chain
,
809 uint32_t image_index
,
810 const VkPresentRegionKHR
*damage
)
812 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
814 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
815 while (!chain
->fifo_ready
) {
816 int ret
= wl_display_dispatch_queue(chain
->display
->wl_display
,
817 chain
->display
->queue
);
819 return VK_ERROR_OUT_OF_DATE_KHR
;
823 assert(image_index
< chain
->base
.image_count
);
824 wl_surface_attach(chain
->surface
, chain
->images
[image_index
].buffer
, 0, 0);
826 if (chain
->surface_version
>= 4 && damage
&&
827 damage
->pRectangles
&& damage
->rectangleCount
> 0) {
828 for (unsigned i
= 0; i
< damage
->rectangleCount
; i
++) {
829 const VkRectLayerKHR
*rect
= &damage
->pRectangles
[i
];
830 assert(rect
->layer
== 0);
831 wl_surface_damage_buffer(chain
->surface
,
832 rect
->offset
.x
, rect
->offset
.y
,
833 rect
->extent
.width
, rect
->extent
.height
);
836 wl_surface_damage(chain
->surface
, 0, 0, INT32_MAX
, INT32_MAX
);
839 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
840 chain
->frame
= wl_surface_frame(chain
->surface
);
841 wl_callback_add_listener(chain
->frame
, &frame_listener
, chain
);
842 chain
->fifo_ready
= false;
845 chain
->images
[image_index
].busy
= true;
846 wl_surface_commit(chain
->surface
);
847 wl_display_flush(chain
->display
->wl_display
);
853 buffer_handle_release(void *data
, struct wl_buffer
*buffer
)
855 struct wsi_wl_image
*image
= data
;
857 assert(image
->buffer
== buffer
);
862 static const struct wl_buffer_listener buffer_listener
= {
863 buffer_handle_release
,
867 wsi_wl_image_init(struct wsi_wl_swapchain
*chain
,
868 struct wsi_wl_image
*image
,
869 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
870 const VkAllocationCallbacks
* pAllocator
)
872 struct wsi_wl_display
*display
= chain
->display
;
875 result
= wsi_create_native_image(&chain
->base
, pCreateInfo
,
876 chain
->num_drm_modifiers
> 0 ? 1 : 0,
877 &chain
->num_drm_modifiers
,
878 &chain
->drm_modifiers
, &image
->base
);
880 if (result
!= VK_SUCCESS
)
883 if (!chain
->drm_wrapper
) {
884 /* Only request modifiers if we have dmabuf, else it must be implicit. */
885 assert(display
->dmabuf
.wl_dmabuf
);
886 assert(image
->base
.drm_modifier
!= DRM_FORMAT_MOD_INVALID
);
888 struct zwp_linux_buffer_params_v1
*params
=
889 zwp_linux_dmabuf_v1_create_params(display
->dmabuf
.wl_dmabuf
);
890 wl_proxy_set_queue((struct wl_proxy
*) params
, chain
->display
->queue
);
892 for (int i
= 0; i
< image
->base
.num_planes
; i
++) {
893 zwp_linux_buffer_params_v1_add(params
,
896 image
->base
.offsets
[i
],
897 image
->base
.row_pitches
[i
],
898 image
->base
.drm_modifier
>> 32,
899 image
->base
.drm_modifier
& 0xffffffff);
900 close(image
->base
.fds
[i
]);
904 zwp_linux_buffer_params_v1_create_immed(params
,
906 chain
->extent
.height
,
909 zwp_linux_buffer_params_v1_destroy(params
);
911 /* Without passing modifiers, we can't have multi-plane RGB images. */
912 assert(image
->base
.num_planes
== 1);
913 assert(image
->base
.drm_modifier
== DRM_FORMAT_MOD_INVALID
);
916 wl_drm_create_prime_buffer(chain
->drm_wrapper
,
917 image
->base
.fds
[0], /* name */
919 chain
->extent
.height
,
921 image
->base
.offsets
[0],
922 image
->base
.row_pitches
[0],
923 0, 0, 0, 0 /* unused */);
924 close(image
->base
.fds
[0]);
930 wl_buffer_add_listener(image
->buffer
, &buffer_listener
, image
);
935 wsi_destroy_image(&chain
->base
, &image
->base
);
941 wsi_wl_swapchain_destroy(struct wsi_swapchain
*wsi_chain
,
942 const VkAllocationCallbacks
*pAllocator
)
944 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
946 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
947 if (chain
->images
[i
].buffer
) {
948 wl_buffer_destroy(chain
->images
[i
].buffer
);
949 wsi_destroy_image(&chain
->base
, &chain
->images
[i
].base
);
954 wl_callback_destroy(chain
->frame
);
956 wl_proxy_wrapper_destroy(chain
->surface
);
957 if (chain
->drm_wrapper
)
958 wl_proxy_wrapper_destroy(chain
->drm_wrapper
);
961 wsi_wl_display_unref(chain
->display
);
963 wsi_swapchain_finish(&chain
->base
);
965 vk_free(pAllocator
, chain
);
971 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
973 struct wsi_device
*wsi_device
,
974 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
975 const VkAllocationCallbacks
* pAllocator
,
976 struct wsi_swapchain
**swapchain_out
)
978 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
979 struct wsi_wayland
*wsi
=
980 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
981 struct wsi_wl_swapchain
*chain
;
984 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
986 int num_images
= pCreateInfo
->minImageCount
;
988 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
989 chain
= vk_alloc(pAllocator
, size
, 8,
990 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
992 return VK_ERROR_OUT_OF_HOST_MEMORY
;
994 result
= wsi_swapchain_init(wsi_device
, &chain
->base
, device
,
995 pCreateInfo
, pAllocator
);
996 if (result
!= VK_SUCCESS
) {
997 vk_free(pAllocator
, chain
);
1001 /* Mark a bunch of stuff as NULL. This way we can just call
1002 * destroy_swapchain for cleanup.
1004 for (uint32_t i
= 0; i
< num_images
; i
++)
1005 chain
->images
[i
].buffer
= NULL
;
1006 chain
->surface
= NULL
;
1007 chain
->drm_wrapper
= NULL
;
1008 chain
->frame
= NULL
;
1010 bool alpha
= pCreateInfo
->compositeAlpha
==
1011 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
1013 chain
->base
.destroy
= wsi_wl_swapchain_destroy
;
1014 chain
->base
.get_wsi_image
= wsi_wl_swapchain_get_wsi_image
;
1015 chain
->base
.acquire_next_image
= wsi_wl_swapchain_acquire_next_image
;
1016 chain
->base
.queue_present
= wsi_wl_swapchain_queue_present
;
1017 chain
->base
.present_mode
= wsi_swapchain_get_present_mode(wsi_device
, pCreateInfo
);
1018 chain
->base
.image_count
= num_images
;
1019 chain
->extent
= pCreateInfo
->imageExtent
;
1020 chain
->vk_format
= pCreateInfo
->imageFormat
;
1021 chain
->drm_format
= wl_drm_format_for_vk_format(chain
->vk_format
, alpha
);
1023 if (pCreateInfo
->oldSwapchain
) {
1024 /* If we have an oldSwapchain parameter, copy the display struct over
1025 * from the old one so we don't have to fully re-initialize it.
1027 WSI_FROM_HANDLE(wsi_wl_swapchain
, old_chain
, pCreateInfo
->oldSwapchain
);
1028 chain
->display
= wsi_wl_display_ref(old_chain
->display
);
1030 chain
->display
= NULL
;
1031 result
= wsi_wl_display_create(wsi
, surface
->display
, &chain
->display
);
1032 if (result
!= VK_SUCCESS
)
1036 chain
->surface
= wl_proxy_create_wrapper(surface
->surface
);
1037 if (!chain
->surface
) {
1038 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1041 wl_proxy_set_queue((struct wl_proxy
*) chain
->surface
,
1042 chain
->display
->queue
);
1043 chain
->surface_version
= wl_proxy_get_version((void *)surface
->surface
);
1045 chain
->num_drm_modifiers
= 0;
1046 chain
->drm_modifiers
= 0;
1048 /* Use explicit DRM format modifiers when both the server and the driver
1051 if (chain
->display
->dmabuf
.wl_dmabuf
&&
1052 chain
->base
.wsi
->supports_modifiers
) {
1053 struct u_vector
*modifiers
;
1054 switch (chain
->drm_format
) {
1055 case WL_DRM_FORMAT_ARGB8888
:
1056 modifiers
= &chain
->display
->dmabuf
.modifiers
.argb8888
;
1058 case WL_DRM_FORMAT_XRGB8888
:
1059 modifiers
= &chain
->display
->dmabuf
.modifiers
.xrgb8888
;
1067 chain
->drm_modifiers
= u_vector_tail(modifiers
);
1068 chain
->num_drm_modifiers
= u_vector_length(modifiers
);
1072 /* When there are explicit DRM format modifiers, we must use
1073 * zwp_linux_dmabuf_v1 for wl_buffer creation. Otherwise, we must use
1076 if (!chain
->num_drm_modifiers
) {
1077 assert(chain
->display
->drm
.wl_drm
);
1079 chain
->drm_wrapper
=
1080 wl_proxy_create_wrapper(chain
->display
->drm
.wl_drm
);
1081 if (!chain
->drm_wrapper
) {
1082 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1085 wl_proxy_set_queue((struct wl_proxy
*) chain
->drm_wrapper
,
1086 chain
->display
->queue
);
1089 chain
->fifo_ready
= true;
1091 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
1092 result
= wsi_wl_image_init(chain
, &chain
->images
[i
],
1093 pCreateInfo
, pAllocator
);
1094 if (result
!= VK_SUCCESS
)
1096 chain
->images
[i
].busy
= false;
1099 *swapchain_out
= &chain
->base
;
1104 wsi_wl_swapchain_destroy(&chain
->base
, pAllocator
);
1110 wsi_wl_init_wsi(struct wsi_device
*wsi_device
,
1111 const VkAllocationCallbacks
*alloc
,
1112 VkPhysicalDevice physical_device
)
1114 struct wsi_wayland
*wsi
;
1117 wsi
= vk_alloc(alloc
, sizeof(*wsi
), 8,
1118 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
1120 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1124 wsi
->physical_device
= physical_device
;
1126 wsi
->wsi
= wsi_device
;
1128 wsi
->base
.get_support
= wsi_wl_surface_get_support
;
1129 wsi
->base
.get_capabilities2
= wsi_wl_surface_get_capabilities2
;
1130 wsi
->base
.get_formats
= wsi_wl_surface_get_formats
;
1131 wsi
->base
.get_formats2
= wsi_wl_surface_get_formats2
;
1132 wsi
->base
.get_present_modes
= wsi_wl_surface_get_present_modes
;
1133 wsi
->base
.get_present_rectangles
= wsi_wl_surface_get_present_rectangles
;
1134 wsi
->base
.create_swapchain
= wsi_wl_surface_create_swapchain
;
1136 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = &wsi
->base
;
1141 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = NULL
;
1147 wsi_wl_finish_wsi(struct wsi_device
*wsi_device
,
1148 const VkAllocationCallbacks
*alloc
)
1150 struct wsi_wayland
*wsi
=
1151 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
1155 vk_free(alloc
, wsi
);