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 if (wsi_wl
->wsi
->force_bgra8_unorm_first
) {
425 /* Find BGRA8_UNORM in the list and swap it to the first position if we
426 * can find it. Some apps get confused if SRGB is first in the list.
428 VkFormat
*first_fmt
= u_vector_head(display
->formats
);
430 u_vector_foreach(iter_fmt
, display
->formats
) {
431 if (*iter_fmt
== VK_FORMAT_B8G8R8A8_UNORM
) {
432 *iter_fmt
= *first_fmt
;
433 *first_fmt
= VK_FORMAT_B8G8R8A8_UNORM
;
439 /* We need prime support for wl_drm */
440 if (display
->drm
.wl_drm
&&
441 (display
->drm
.capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
442 display
->formats
= &display
->drm
.formats
;
443 } else if (display
->dmabuf
.wl_dmabuf
) {
444 display
->formats
= &display
->dmabuf
.formats
;
447 if (!display
->formats
) {
448 result
= VK_ERROR_SURFACE_LOST_KHR
;
452 /* We don't need this anymore */
453 wl_registry_destroy(registry
);
455 display
->refcount
= 0;
461 wl_registry_destroy(registry
);
464 wsi_wl_display_finish(display
);
469 wsi_wl_display_create(struct wsi_wayland
*wsi
, struct wl_display
*wl_display
,
470 struct wsi_wl_display
**display_out
)
472 struct wsi_wl_display
*display
=
473 vk_alloc(wsi
->alloc
, sizeof(*display
), 8,
474 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
476 return VK_ERROR_OUT_OF_HOST_MEMORY
;
478 VkResult result
= wsi_wl_display_init(wsi
, display
, wl_display
, true);
479 if (result
!= VK_SUCCESS
) {
480 vk_free(wsi
->alloc
, display
);
485 *display_out
= display
;
490 static struct wsi_wl_display
*
491 wsi_wl_display_ref(struct wsi_wl_display
*display
)
498 wsi_wl_display_unref(struct wsi_wl_display
*display
)
500 if (display
->refcount
-- > 1)
503 struct wsi_wayland
*wsi
= display
->wsi_wl
;
504 wsi_wl_display_finish(display
);
505 vk_free(wsi
->alloc
, display
);
509 wsi_wl_get_presentation_support(struct wsi_device
*wsi_device
,
510 struct wl_display
*wl_display
)
512 struct wsi_wayland
*wsi
=
513 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
515 struct wsi_wl_display display
;
516 VkResult ret
= wsi_wl_display_init(wsi
, &display
, wl_display
, false);
517 if (ret
== VK_SUCCESS
)
518 wsi_wl_display_finish(&display
);
520 return ret
== VK_SUCCESS
;
524 wsi_wl_surface_get_support(VkIcdSurfaceBase
*surface
,
525 struct wsi_device
*wsi_device
,
526 uint32_t queueFamilyIndex
,
527 VkBool32
* pSupported
)
534 static const VkPresentModeKHR present_modes
[] = {
535 VK_PRESENT_MODE_MAILBOX_KHR
,
536 VK_PRESENT_MODE_FIFO_KHR
,
540 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase
*surface
,
541 struct wsi_device
*wsi_device
,
542 VkSurfaceCapabilitiesKHR
* caps
)
544 /* For true mailbox mode, we need at least 4 images:
545 * 1) One to scan out from
546 * 2) One to have queued for scan-out
547 * 3) One to be currently held by the Wayland compositor
548 * 4) One to render to
550 caps
->minImageCount
= 4;
551 /* There is no real maximum */
552 caps
->maxImageCount
= 0;
554 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
555 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
556 caps
->maxImageExtent
= (VkExtent2D
) {
557 wsi_device
->maxImageDimension2D
,
558 wsi_device
->maxImageDimension2D
,
561 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
562 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
563 caps
->maxImageArrayLayers
= 1;
565 caps
->supportedCompositeAlpha
=
566 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
|
567 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
569 caps
->supportedUsageFlags
=
570 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
571 VK_IMAGE_USAGE_SAMPLED_BIT
|
572 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
573 VK_IMAGE_USAGE_STORAGE_BIT
|
574 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
580 wsi_wl_surface_get_capabilities2(VkIcdSurfaceBase
*surface
,
581 struct wsi_device
*wsi_device
,
582 const void *info_next
,
583 VkSurfaceCapabilities2KHR
* caps
)
585 assert(caps
->sType
== VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
);
588 wsi_wl_surface_get_capabilities(surface
, wsi_device
,
589 &caps
->surfaceCapabilities
);
591 vk_foreach_struct(ext
, caps
->pNext
) {
592 switch (ext
->sType
) {
593 case VK_STRUCTURE_TYPE_SURFACE_PROTECTED_CAPABILITIES_KHR
: {
594 VkSurfaceProtectedCapabilitiesKHR
*protected = (void *)ext
;
595 protected->supportsProtected
= VK_FALSE
;
609 wsi_wl_surface_get_formats(VkIcdSurfaceBase
*icd_surface
,
610 struct wsi_device
*wsi_device
,
611 uint32_t* pSurfaceFormatCount
,
612 VkSurfaceFormatKHR
* pSurfaceFormats
)
614 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
615 struct wsi_wayland
*wsi
=
616 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
618 struct wsi_wl_display display
;
619 if (wsi_wl_display_init(wsi
, &display
, surface
->display
, true))
620 return VK_ERROR_SURFACE_LOST_KHR
;
622 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
625 u_vector_foreach(disp_fmt
, display
.formats
) {
626 vk_outarray_append(&out
, out_fmt
) {
627 out_fmt
->format
= *disp_fmt
;
628 out_fmt
->colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
632 wsi_wl_display_finish(&display
);
634 return vk_outarray_status(&out
);
638 wsi_wl_surface_get_formats2(VkIcdSurfaceBase
*icd_surface
,
639 struct wsi_device
*wsi_device
,
640 const void *info_next
,
641 uint32_t* pSurfaceFormatCount
,
642 VkSurfaceFormat2KHR
* pSurfaceFormats
)
644 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
645 struct wsi_wayland
*wsi
=
646 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
648 struct wsi_wl_display display
;
649 if (wsi_wl_display_init(wsi
, &display
, surface
->display
, true))
650 return VK_ERROR_SURFACE_LOST_KHR
;
652 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
655 u_vector_foreach(disp_fmt
, display
.formats
) {
656 vk_outarray_append(&out
, out_fmt
) {
657 out_fmt
->surfaceFormat
.format
= *disp_fmt
;
658 out_fmt
->surfaceFormat
.colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
662 wsi_wl_display_finish(&display
);
664 return vk_outarray_status(&out
);
668 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
669 uint32_t* pPresentModeCount
,
670 VkPresentModeKHR
* pPresentModes
)
672 if (pPresentModes
== NULL
) {
673 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
677 *pPresentModeCount
= MIN2(*pPresentModeCount
, ARRAY_SIZE(present_modes
));
678 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
680 if (*pPresentModeCount
< ARRAY_SIZE(present_modes
))
681 return VK_INCOMPLETE
;
687 wsi_wl_surface_get_present_rectangles(VkIcdSurfaceBase
*surface
,
688 struct wsi_device
*wsi_device
,
689 uint32_t* pRectCount
,
692 VK_OUTARRAY_MAKE(out
, pRects
, pRectCount
);
694 vk_outarray_append(&out
, rect
) {
695 /* We don't know a size so just return the usual "I don't know." */
698 .extent
= { -1, -1 },
702 return vk_outarray_status(&out
);
705 VkResult
wsi_create_wl_surface(const VkAllocationCallbacks
*pAllocator
,
706 const VkWaylandSurfaceCreateInfoKHR
*pCreateInfo
,
707 VkSurfaceKHR
*pSurface
)
709 VkIcdSurfaceWayland
*surface
;
711 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
712 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
714 return VK_ERROR_OUT_OF_HOST_MEMORY
;
716 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_WAYLAND
;
717 surface
->display
= pCreateInfo
->display
;
718 surface
->surface
= pCreateInfo
->surface
;
720 *pSurface
= VkIcdSurfaceBase_to_handle(&surface
->base
);
725 struct wsi_wl_image
{
726 struct wsi_image base
;
727 struct wl_buffer
* buffer
;
731 struct wsi_wl_swapchain
{
732 struct wsi_swapchain base
;
734 struct wsi_wl_display
*display
;
736 struct wl_surface
* surface
;
737 uint32_t surface_version
;
739 /* non-NULL when wl_drm should be used for wl_buffer creation; otherwise,
740 * zwp_linux_dmabuf_v1 should be used.
742 struct wl_drm
* drm_wrapper
;
744 struct wl_callback
* frame
;
750 uint32_t num_drm_modifiers
;
751 const uint64_t * drm_modifiers
;
753 VkPresentModeKHR present_mode
;
756 struct wsi_wl_image images
[0];
758 WSI_DEFINE_NONDISP_HANDLE_CASTS(wsi_wl_swapchain
, VkSwapchainKHR
)
760 static struct wsi_image
*
761 wsi_wl_swapchain_get_wsi_image(struct wsi_swapchain
*wsi_chain
,
762 uint32_t image_index
)
764 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
765 return &chain
->images
[image_index
].base
;
769 wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain
*wsi_chain
,
770 const VkAcquireNextImageInfoKHR
*info
,
771 uint32_t *image_index
)
773 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
774 struct timespec start_time
, end_time
;
775 struct timespec rel_timeout
;
776 int wl_fd
= wl_display_get_fd(chain
->display
->wl_display
);
778 timespec_from_nsec(&rel_timeout
, info
->timeout
);
780 clock_gettime(CLOCK_MONOTONIC
, &start_time
);
781 timespec_add(&end_time
, &rel_timeout
, &start_time
);
784 /* Try to dispatch potential events. */
785 int ret
= wl_display_dispatch_queue_pending(chain
->display
->wl_display
,
786 chain
->display
->queue
);
788 return VK_ERROR_OUT_OF_DATE_KHR
;
790 /* Try to find a free image. */
791 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
792 if (!chain
->images
[i
].busy
) {
793 /* We found a non-busy image */
795 chain
->images
[i
].busy
= true;
800 /* Check for timeout. */
801 struct timespec current_time
;
802 clock_gettime(CLOCK_MONOTONIC
, ¤t_time
);
803 if (timespec_after(¤t_time
, &end_time
))
806 /* Try to read events from the server. */
807 ret
= wl_display_prepare_read_queue(chain
->display
->wl_display
,
808 chain
->display
->queue
);
810 /* Another thread might have read events for our queue already. Go
811 * back to dispatch them.
815 return VK_ERROR_OUT_OF_DATE_KHR
;
818 struct pollfd pollfd
= {
822 timespec_sub(&rel_timeout
, &end_time
, ¤t_time
);
823 ret
= ppoll(&pollfd
, 1, &rel_timeout
, NULL
);
826 wl_display_cancel_read(chain
->display
->wl_display
);
828 /* If ppoll() was interrupted, try again. */
829 if (lerrno
== EINTR
|| lerrno
== EAGAIN
)
831 return VK_ERROR_OUT_OF_DATE_KHR
;
837 ret
= wl_display_read_events(chain
->display
->wl_display
);
839 return VK_ERROR_OUT_OF_DATE_KHR
;
844 frame_handle_done(void *data
, struct wl_callback
*callback
, uint32_t serial
)
846 struct wsi_wl_swapchain
*chain
= data
;
849 chain
->fifo_ready
= true;
851 wl_callback_destroy(callback
);
854 static const struct wl_callback_listener frame_listener
= {
859 wsi_wl_swapchain_queue_present(struct wsi_swapchain
*wsi_chain
,
860 uint32_t image_index
,
861 const VkPresentRegionKHR
*damage
)
863 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
865 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
866 while (!chain
->fifo_ready
) {
867 int ret
= wl_display_dispatch_queue(chain
->display
->wl_display
,
868 chain
->display
->queue
);
870 return VK_ERROR_OUT_OF_DATE_KHR
;
874 assert(image_index
< chain
->base
.image_count
);
875 wl_surface_attach(chain
->surface
, chain
->images
[image_index
].buffer
, 0, 0);
877 if (chain
->surface_version
>= 4 && damage
&&
878 damage
->pRectangles
&& damage
->rectangleCount
> 0) {
879 for (unsigned i
= 0; i
< damage
->rectangleCount
; i
++) {
880 const VkRectLayerKHR
*rect
= &damage
->pRectangles
[i
];
881 assert(rect
->layer
== 0);
882 wl_surface_damage_buffer(chain
->surface
,
883 rect
->offset
.x
, rect
->offset
.y
,
884 rect
->extent
.width
, rect
->extent
.height
);
887 wl_surface_damage(chain
->surface
, 0, 0, INT32_MAX
, INT32_MAX
);
890 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
891 chain
->frame
= wl_surface_frame(chain
->surface
);
892 wl_callback_add_listener(chain
->frame
, &frame_listener
, chain
);
893 chain
->fifo_ready
= false;
896 chain
->images
[image_index
].busy
= true;
897 wl_surface_commit(chain
->surface
);
898 wl_display_flush(chain
->display
->wl_display
);
904 buffer_handle_release(void *data
, struct wl_buffer
*buffer
)
906 struct wsi_wl_image
*image
= data
;
908 assert(image
->buffer
== buffer
);
913 static const struct wl_buffer_listener buffer_listener
= {
914 buffer_handle_release
,
918 wsi_wl_image_init(struct wsi_wl_swapchain
*chain
,
919 struct wsi_wl_image
*image
,
920 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
921 const VkAllocationCallbacks
* pAllocator
)
923 struct wsi_wl_display
*display
= chain
->display
;
926 result
= wsi_create_native_image(&chain
->base
, pCreateInfo
,
927 chain
->num_drm_modifiers
> 0 ? 1 : 0,
928 &chain
->num_drm_modifiers
,
929 &chain
->drm_modifiers
, &image
->base
);
931 if (result
!= VK_SUCCESS
)
934 if (!chain
->drm_wrapper
) {
935 /* Only request modifiers if we have dmabuf, else it must be implicit. */
936 assert(display
->dmabuf
.wl_dmabuf
);
937 assert(image
->base
.drm_modifier
!= DRM_FORMAT_MOD_INVALID
);
939 struct zwp_linux_buffer_params_v1
*params
=
940 zwp_linux_dmabuf_v1_create_params(display
->dmabuf
.wl_dmabuf
);
941 wl_proxy_set_queue((struct wl_proxy
*) params
, chain
->display
->queue
);
943 for (int i
= 0; i
< image
->base
.num_planes
; i
++) {
944 zwp_linux_buffer_params_v1_add(params
,
947 image
->base
.offsets
[i
],
948 image
->base
.row_pitches
[i
],
949 image
->base
.drm_modifier
>> 32,
950 image
->base
.drm_modifier
& 0xffffffff);
951 close(image
->base
.fds
[i
]);
955 zwp_linux_buffer_params_v1_create_immed(params
,
957 chain
->extent
.height
,
960 zwp_linux_buffer_params_v1_destroy(params
);
962 /* Without passing modifiers, we can't have multi-plane RGB images. */
963 assert(image
->base
.num_planes
== 1);
964 assert(image
->base
.drm_modifier
== DRM_FORMAT_MOD_INVALID
);
967 wl_drm_create_prime_buffer(chain
->drm_wrapper
,
968 image
->base
.fds
[0], /* name */
970 chain
->extent
.height
,
972 image
->base
.offsets
[0],
973 image
->base
.row_pitches
[0],
974 0, 0, 0, 0 /* unused */);
975 close(image
->base
.fds
[0]);
981 wl_buffer_add_listener(image
->buffer
, &buffer_listener
, image
);
986 wsi_destroy_image(&chain
->base
, &image
->base
);
992 wsi_wl_swapchain_destroy(struct wsi_swapchain
*wsi_chain
,
993 const VkAllocationCallbacks
*pAllocator
)
995 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
997 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
998 if (chain
->images
[i
].buffer
) {
999 wl_buffer_destroy(chain
->images
[i
].buffer
);
1000 wsi_destroy_image(&chain
->base
, &chain
->images
[i
].base
);
1005 wl_callback_destroy(chain
->frame
);
1007 wl_proxy_wrapper_destroy(chain
->surface
);
1008 if (chain
->drm_wrapper
)
1009 wl_proxy_wrapper_destroy(chain
->drm_wrapper
);
1012 wsi_wl_display_unref(chain
->display
);
1014 wsi_swapchain_finish(&chain
->base
);
1016 vk_free(pAllocator
, chain
);
1022 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
1024 struct wsi_device
*wsi_device
,
1025 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
1026 const VkAllocationCallbacks
* pAllocator
,
1027 struct wsi_swapchain
**swapchain_out
)
1029 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
1030 struct wsi_wayland
*wsi
=
1031 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
1032 struct wsi_wl_swapchain
*chain
;
1035 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
1037 int num_images
= pCreateInfo
->minImageCount
;
1039 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
1040 chain
= vk_alloc(pAllocator
, size
, 8,
1041 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1043 return VK_ERROR_OUT_OF_HOST_MEMORY
;
1045 result
= wsi_swapchain_init(wsi_device
, &chain
->base
, device
,
1046 pCreateInfo
, pAllocator
);
1047 if (result
!= VK_SUCCESS
) {
1048 vk_free(pAllocator
, chain
);
1052 /* Mark a bunch of stuff as NULL. This way we can just call
1053 * destroy_swapchain for cleanup.
1055 for (uint32_t i
= 0; i
< num_images
; i
++)
1056 chain
->images
[i
].buffer
= NULL
;
1057 chain
->surface
= NULL
;
1058 chain
->drm_wrapper
= NULL
;
1059 chain
->frame
= NULL
;
1061 bool alpha
= pCreateInfo
->compositeAlpha
==
1062 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
1064 chain
->base
.destroy
= wsi_wl_swapchain_destroy
;
1065 chain
->base
.get_wsi_image
= wsi_wl_swapchain_get_wsi_image
;
1066 chain
->base
.acquire_next_image
= wsi_wl_swapchain_acquire_next_image
;
1067 chain
->base
.queue_present
= wsi_wl_swapchain_queue_present
;
1068 chain
->base
.present_mode
= wsi_swapchain_get_present_mode(wsi_device
, pCreateInfo
);
1069 chain
->base
.image_count
= num_images
;
1070 chain
->extent
= pCreateInfo
->imageExtent
;
1071 chain
->vk_format
= pCreateInfo
->imageFormat
;
1072 chain
->drm_format
= wl_drm_format_for_vk_format(chain
->vk_format
, alpha
);
1074 if (pCreateInfo
->oldSwapchain
) {
1075 /* If we have an oldSwapchain parameter, copy the display struct over
1076 * from the old one so we don't have to fully re-initialize it.
1078 WSI_FROM_HANDLE(wsi_wl_swapchain
, old_chain
, pCreateInfo
->oldSwapchain
);
1079 chain
->display
= wsi_wl_display_ref(old_chain
->display
);
1081 chain
->display
= NULL
;
1082 result
= wsi_wl_display_create(wsi
, surface
->display
, &chain
->display
);
1083 if (result
!= VK_SUCCESS
)
1087 chain
->surface
= wl_proxy_create_wrapper(surface
->surface
);
1088 if (!chain
->surface
) {
1089 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1092 wl_proxy_set_queue((struct wl_proxy
*) chain
->surface
,
1093 chain
->display
->queue
);
1094 chain
->surface_version
= wl_proxy_get_version((void *)surface
->surface
);
1096 chain
->num_drm_modifiers
= 0;
1097 chain
->drm_modifiers
= 0;
1099 /* Use explicit DRM format modifiers when both the server and the driver
1102 if (chain
->display
->dmabuf
.wl_dmabuf
&&
1103 chain
->base
.wsi
->supports_modifiers
) {
1104 struct u_vector
*modifiers
;
1105 switch (chain
->drm_format
) {
1106 case WL_DRM_FORMAT_ARGB8888
:
1107 modifiers
= &chain
->display
->dmabuf
.modifiers
.argb8888
;
1109 case WL_DRM_FORMAT_XRGB8888
:
1110 modifiers
= &chain
->display
->dmabuf
.modifiers
.xrgb8888
;
1118 chain
->drm_modifiers
= u_vector_tail(modifiers
);
1119 chain
->num_drm_modifiers
= u_vector_length(modifiers
);
1123 /* When there are explicit DRM format modifiers, we must use
1124 * zwp_linux_dmabuf_v1 for wl_buffer creation. Otherwise, we must use
1127 if (!chain
->num_drm_modifiers
) {
1128 assert(chain
->display
->drm
.wl_drm
);
1130 chain
->drm_wrapper
=
1131 wl_proxy_create_wrapper(chain
->display
->drm
.wl_drm
);
1132 if (!chain
->drm_wrapper
) {
1133 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1136 wl_proxy_set_queue((struct wl_proxy
*) chain
->drm_wrapper
,
1137 chain
->display
->queue
);
1140 chain
->fifo_ready
= true;
1142 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
1143 result
= wsi_wl_image_init(chain
, &chain
->images
[i
],
1144 pCreateInfo
, pAllocator
);
1145 if (result
!= VK_SUCCESS
)
1147 chain
->images
[i
].busy
= false;
1150 *swapchain_out
= &chain
->base
;
1155 wsi_wl_swapchain_destroy(&chain
->base
, pAllocator
);
1161 wsi_wl_init_wsi(struct wsi_device
*wsi_device
,
1162 const VkAllocationCallbacks
*alloc
,
1163 VkPhysicalDevice physical_device
)
1165 struct wsi_wayland
*wsi
;
1168 wsi
= vk_alloc(alloc
, sizeof(*wsi
), 8,
1169 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
1171 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
1175 wsi
->physical_device
= physical_device
;
1177 wsi
->wsi
= wsi_device
;
1179 wsi
->base
.get_support
= wsi_wl_surface_get_support
;
1180 wsi
->base
.get_capabilities2
= wsi_wl_surface_get_capabilities2
;
1181 wsi
->base
.get_formats
= wsi_wl_surface_get_formats
;
1182 wsi
->base
.get_formats2
= wsi_wl_surface_get_formats2
;
1183 wsi
->base
.get_present_modes
= wsi_wl_surface_get_present_modes
;
1184 wsi
->base
.get_present_rectangles
= wsi_wl_surface_get_present_rectangles
;
1185 wsi
->base
.create_swapchain
= wsi_wl_surface_create_swapchain
;
1187 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = &wsi
->base
;
1192 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = NULL
;
1198 wsi_wl_finish_wsi(struct wsi_device
*wsi_device
,
1199 const VkAllocationCallbacks
*alloc
)
1201 struct wsi_wayland
*wsi
=
1202 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
1206 vk_free(alloc
, wsi
);