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 "wsi_common_private.h"
36 #include "wsi_common_wayland.h"
37 #include "wayland-drm-client-protocol.h"
39 #include <util/hash_table.h>
40 #include <util/u_vector.h>
42 #define typed_memcpy(dest, src, count) ({ \
43 STATIC_ASSERT(sizeof(*src) == sizeof(*dest)); \
44 memcpy((dest), (src), (count) * sizeof(*(src))); \
49 struct wsi_wl_display
{
50 /* The real wl_display */
51 struct wl_display
* wl_display
;
52 /* Actually a proxy wrapper around the event queue */
53 struct wl_display
* wl_display_wrapper
;
54 struct wl_event_queue
* queue
;
57 struct wsi_wayland
*wsi_wl
;
58 /* Vector of VkFormats supported */
59 struct u_vector formats
;
61 uint32_t capabilities
;
63 /* Only used for displays created by wsi_wl_display_create */
68 struct wsi_interface base
;
70 const VkAllocationCallbacks
*alloc
;
71 VkPhysicalDevice physical_device
;
73 const struct wsi_callbacks
*cbs
;
77 wsi_wl_display_add_vk_format(struct wsi_wl_display
*display
, VkFormat format
)
79 /* Don't add a format that's already in the list */
81 u_vector_foreach(f
, &display
->formats
)
85 /* Don't add formats that aren't renderable. */
86 VkFormatProperties props
;
88 display
->wsi_wl
->cbs
->GetPhysicalDeviceFormatProperties(display
->wsi_wl
->physical_device
,
90 if (!(props
.optimalTilingFeatures
& VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT
))
93 f
= u_vector_add(&display
->formats
);
99 drm_handle_device(void *data
, struct wl_drm
*drm
, const char *name
)
104 wl_drm_format_for_vk_format(VkFormat vk_format
, bool alpha
)
107 /* TODO: Figure out what all the formats mean and make this table
111 case VK_FORMAT_R4G4B4A4_UNORM
:
112 return alpha
? WL_DRM_FORMAT_ABGR4444
: WL_DRM_FORMAT_XBGR4444
;
113 case VK_FORMAT_R5G6B5_UNORM
:
114 return WL_DRM_FORMAT_BGR565
;
115 case VK_FORMAT_R5G5B5A1_UNORM
:
116 return alpha
? WL_DRM_FORMAT_ABGR1555
: WL_DRM_FORMAT_XBGR1555
;
117 case VK_FORMAT_R8G8B8_UNORM
:
118 return WL_DRM_FORMAT_XBGR8888
;
119 case VK_FORMAT_R8G8B8A8_UNORM
:
120 return alpha
? WL_DRM_FORMAT_ABGR8888
: WL_DRM_FORMAT_XBGR8888
;
121 case VK_FORMAT_R10G10B10A2_UNORM
:
122 return alpha
? WL_DRM_FORMAT_ABGR2101010
: WL_DRM_FORMAT_XBGR2101010
;
123 case VK_FORMAT_B4G4R4A4_UNORM
:
124 return alpha
? WL_DRM_FORMAT_ARGB4444
: WL_DRM_FORMAT_XRGB4444
;
125 case VK_FORMAT_B5G6R5_UNORM
:
126 return WL_DRM_FORMAT_RGB565
;
127 case VK_FORMAT_B5G5R5A1_UNORM
:
128 return alpha
? WL_DRM_FORMAT_XRGB1555
: WL_DRM_FORMAT_XRGB1555
;
130 case VK_FORMAT_B8G8R8_UNORM
:
131 case VK_FORMAT_B8G8R8_SRGB
:
132 return WL_DRM_FORMAT_BGRX8888
;
133 case VK_FORMAT_B8G8R8A8_UNORM
:
134 case VK_FORMAT_B8G8R8A8_SRGB
:
135 return alpha
? WL_DRM_FORMAT_ARGB8888
: WL_DRM_FORMAT_XRGB8888
;
137 case VK_FORMAT_B10G10R10A2_UNORM
:
138 return alpha
? WL_DRM_FORMAT_ARGB2101010
: WL_DRM_FORMAT_XRGB2101010
;
142 assert(!"Unsupported Vulkan format");
148 drm_handle_format(void *data
, struct wl_drm
*drm
, uint32_t wl_format
)
150 struct wsi_wl_display
*display
= data
;
151 if (display
->formats
.element_size
== 0)
156 case WL_DRM_FORMAT_ABGR4444
:
157 case WL_DRM_FORMAT_XBGR4444
:
158 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R4G4B4A4_UNORM
);
160 case WL_DRM_FORMAT_BGR565
:
161 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R5G6B5_UNORM
);
163 case WL_DRM_FORMAT_ABGR1555
:
164 case WL_DRM_FORMAT_XBGR1555
:
165 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R5G5B5A1_UNORM
);
167 case WL_DRM_FORMAT_XBGR8888
:
168 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R8G8B8_UNORM
);
170 case WL_DRM_FORMAT_ABGR8888
:
171 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R8G8B8A8_UNORM
);
173 case WL_DRM_FORMAT_ABGR2101010
:
174 case WL_DRM_FORMAT_XBGR2101010
:
175 wsi_wl_display_add_vk_format(display
, VK_FORMAT_R10G10B10A2_UNORM
);
177 case WL_DRM_FORMAT_ARGB4444
:
178 case WL_DRM_FORMAT_XRGB4444
:
179 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B4G4R4A4_UNORM
);
181 case WL_DRM_FORMAT_RGB565
:
182 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B5G6R5_UNORM
);
184 case WL_DRM_FORMAT_ARGB1555
:
185 case WL_DRM_FORMAT_XRGB1555
:
186 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B5G5R5A1_UNORM
);
189 case WL_DRM_FORMAT_XRGB8888
:
190 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8_SRGB
);
191 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8_UNORM
);
193 case WL_DRM_FORMAT_ARGB8888
:
194 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8A8_SRGB
);
195 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B8G8R8A8_UNORM
);
198 case WL_DRM_FORMAT_ARGB2101010
:
199 case WL_DRM_FORMAT_XRGB2101010
:
200 wsi_wl_display_add_vk_format(display
, VK_FORMAT_B10G10R10A2_UNORM
);
207 drm_handle_authenticated(void *data
, struct wl_drm
*drm
)
212 drm_handle_capabilities(void *data
, struct wl_drm
*drm
, uint32_t capabilities
)
214 struct wsi_wl_display
*display
= data
;
216 display
->capabilities
= capabilities
;
219 static const struct wl_drm_listener drm_listener
= {
222 drm_handle_authenticated
,
223 drm_handle_capabilities
,
227 registry_handle_global(void *data
, struct wl_registry
*registry
,
228 uint32_t name
, const char *interface
, uint32_t version
)
230 struct wsi_wl_display
*display
= data
;
232 if (strcmp(interface
, "wl_drm") == 0) {
233 assert(display
->drm
== NULL
);
235 assert(version
>= 2);
236 display
->drm
= wl_registry_bind(registry
, name
, &wl_drm_interface
, 2);
239 wl_drm_add_listener(display
->drm
, &drm_listener
, display
);
244 registry_handle_global_remove(void *data
, struct wl_registry
*registry
,
248 static const struct wl_registry_listener registry_listener
= {
249 registry_handle_global
,
250 registry_handle_global_remove
254 wsi_wl_display_finish(struct wsi_wl_display
*display
)
256 assert(display
->refcount
== 0);
258 u_vector_finish(&display
->formats
);
260 wl_drm_destroy(display
->drm
);
261 if (display
->wl_display_wrapper
)
262 wl_proxy_wrapper_destroy(display
->wl_display_wrapper
);
264 wl_event_queue_destroy(display
->queue
);
268 wsi_wl_display_init(struct wsi_wayland
*wsi_wl
,
269 struct wsi_wl_display
*display
,
270 struct wl_display
*wl_display
,
271 bool get_format_list
)
273 VkResult result
= VK_SUCCESS
;
274 memset(display
, 0, sizeof(*display
));
276 display
->wsi_wl
= wsi_wl
;
277 display
->wl_display
= wl_display
;
279 if (get_format_list
) {
280 if (!u_vector_init(&display
->formats
, sizeof(VkFormat
), 8)) {
281 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
286 display
->queue
= wl_display_create_queue(wl_display
);
287 if (!display
->queue
) {
288 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
292 display
->wl_display_wrapper
= wl_proxy_create_wrapper(wl_display
);
293 if (!display
->wl_display_wrapper
) {
294 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
298 wl_proxy_set_queue((struct wl_proxy
*) display
->wl_display_wrapper
,
301 struct wl_registry
*registry
=
302 wl_display_get_registry(display
->wl_display_wrapper
);
304 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
308 wl_registry_add_listener(registry
, ®istry_listener
, display
);
310 /* Round-trip to get the wl_drm global */
311 wl_display_roundtrip_queue(display
->wl_display
, display
->queue
);
314 result
= VK_ERROR_SURFACE_LOST_KHR
;
318 /* Round-trip to get wl_drm formats and capabilities */
319 wl_display_roundtrip_queue(display
->wl_display
, display
->queue
);
321 /* We need prime support */
322 if (!(display
->capabilities
& WL_DRM_CAPABILITY_PRIME
)) {
323 result
= VK_ERROR_SURFACE_LOST_KHR
;
327 /* We don't need this anymore */
328 wl_registry_destroy(registry
);
330 display
->refcount
= 0;
336 wl_registry_destroy(registry
);
339 wsi_wl_display_finish(display
);
344 wsi_wl_display_create(struct wsi_wayland
*wsi
, struct wl_display
*wl_display
,
345 struct wsi_wl_display
**display_out
)
347 struct wsi_wl_display
*display
=
348 vk_alloc(wsi
->alloc
, sizeof(*display
), 8,
349 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
351 return VK_ERROR_OUT_OF_HOST_MEMORY
;
353 VkResult result
= wsi_wl_display_init(wsi
, display
, wl_display
, true);
354 if (result
!= VK_SUCCESS
) {
355 vk_free(wsi
->alloc
, display
);
360 *display_out
= display
;
365 static struct wsi_wl_display
*
366 wsi_wl_display_ref(struct wsi_wl_display
*display
)
373 wsi_wl_display_unref(struct wsi_wl_display
*display
)
375 if (display
->refcount
-- > 1)
378 struct wsi_wayland
*wsi
= display
->wsi_wl
;
379 wsi_wl_display_finish(display
);
380 vk_free(wsi
->alloc
, display
);
384 wsi_wl_get_presentation_support(struct wsi_device
*wsi_device
,
385 struct wl_display
*wl_display
)
387 struct wsi_wayland
*wsi
=
388 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
390 struct wsi_wl_display display
;
391 int ret
= wsi_wl_display_init(wsi
, &display
, wl_display
, false);
392 wsi_wl_display_finish(&display
);
398 wsi_wl_surface_get_support(VkIcdSurfaceBase
*surface
,
399 struct wsi_device
*wsi_device
,
400 const VkAllocationCallbacks
*alloc
,
401 uint32_t queueFamilyIndex
,
403 bool can_handle_different_gpu
,
404 VkBool32
* pSupported
)
411 static const VkPresentModeKHR present_modes
[] = {
412 VK_PRESENT_MODE_MAILBOX_KHR
,
413 VK_PRESENT_MODE_FIFO_KHR
,
417 wsi_wl_surface_get_capabilities(VkIcdSurfaceBase
*surface
,
418 VkSurfaceCapabilitiesKHR
* caps
)
420 /* For true mailbox mode, we need at least 4 images:
421 * 1) One to scan out from
422 * 2) One to have queued for scan-out
423 * 3) One to be currently held by the Wayland compositor
424 * 4) One to render to
426 caps
->minImageCount
= 4;
427 /* There is no real maximum */
428 caps
->maxImageCount
= 0;
430 caps
->currentExtent
= (VkExtent2D
) { -1, -1 };
431 caps
->minImageExtent
= (VkExtent2D
) { 1, 1 };
432 /* This is the maximum supported size on Intel */
433 caps
->maxImageExtent
= (VkExtent2D
) { 1 << 14, 1 << 14 };
434 caps
->supportedTransforms
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
435 caps
->currentTransform
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
;
436 caps
->maxImageArrayLayers
= 1;
438 caps
->supportedCompositeAlpha
=
439 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
|
440 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
442 caps
->supportedUsageFlags
=
443 VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
444 VK_IMAGE_USAGE_SAMPLED_BIT
|
445 VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
446 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
;
452 wsi_wl_surface_get_capabilities2(VkIcdSurfaceBase
*surface
,
453 const void *info_next
,
454 VkSurfaceCapabilities2KHR
* caps
)
456 assert(caps
->sType
== VK_STRUCTURE_TYPE_SURFACE_CAPABILITIES_2_KHR
);
458 return wsi_wl_surface_get_capabilities(surface
, &caps
->surfaceCapabilities
);
462 wsi_wl_surface_get_formats(VkIcdSurfaceBase
*icd_surface
,
463 struct wsi_device
*wsi_device
,
464 uint32_t* pSurfaceFormatCount
,
465 VkSurfaceFormatKHR
* pSurfaceFormats
)
467 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
468 struct wsi_wayland
*wsi
=
469 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
471 struct wsi_wl_display display
;
472 if (wsi_wl_display_init(wsi
, &display
, surface
->display
, true))
473 return VK_ERROR_SURFACE_LOST_KHR
;
475 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
478 u_vector_foreach(disp_fmt
, &display
.formats
) {
479 vk_outarray_append(&out
, out_fmt
) {
480 out_fmt
->format
= *disp_fmt
;
481 out_fmt
->colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
485 wsi_wl_display_finish(&display
);
487 return vk_outarray_status(&out
);
491 wsi_wl_surface_get_formats2(VkIcdSurfaceBase
*icd_surface
,
492 struct wsi_device
*wsi_device
,
493 const void *info_next
,
494 uint32_t* pSurfaceFormatCount
,
495 VkSurfaceFormat2KHR
* pSurfaceFormats
)
497 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
498 struct wsi_wayland
*wsi
=
499 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
501 struct wsi_wl_display display
;
502 if (wsi_wl_display_init(wsi
, &display
, surface
->display
, true))
503 return VK_ERROR_SURFACE_LOST_KHR
;
505 VK_OUTARRAY_MAKE(out
, pSurfaceFormats
, pSurfaceFormatCount
);
508 u_vector_foreach(disp_fmt
, &display
.formats
) {
509 vk_outarray_append(&out
, out_fmt
) {
510 out_fmt
->surfaceFormat
.format
= *disp_fmt
;
511 out_fmt
->surfaceFormat
.colorSpace
= VK_COLORSPACE_SRGB_NONLINEAR_KHR
;
515 wsi_wl_display_finish(&display
);
517 return vk_outarray_status(&out
);
521 wsi_wl_surface_get_present_modes(VkIcdSurfaceBase
*surface
,
522 uint32_t* pPresentModeCount
,
523 VkPresentModeKHR
* pPresentModes
)
525 if (pPresentModes
== NULL
) {
526 *pPresentModeCount
= ARRAY_SIZE(present_modes
);
530 *pPresentModeCount
= MIN2(*pPresentModeCount
, ARRAY_SIZE(present_modes
));
531 typed_memcpy(pPresentModes
, present_modes
, *pPresentModeCount
);
533 if (*pPresentModeCount
< ARRAY_SIZE(present_modes
))
534 return VK_INCOMPLETE
;
539 VkResult
wsi_create_wl_surface(const VkAllocationCallbacks
*pAllocator
,
540 const VkWaylandSurfaceCreateInfoKHR
*pCreateInfo
,
541 VkSurfaceKHR
*pSurface
)
543 VkIcdSurfaceWayland
*surface
;
545 surface
= vk_alloc(pAllocator
, sizeof *surface
, 8,
546 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
548 return VK_ERROR_OUT_OF_HOST_MEMORY
;
550 surface
->base
.platform
= VK_ICD_WSI_PLATFORM_WAYLAND
;
551 surface
->display
= pCreateInfo
->display
;
552 surface
->surface
= pCreateInfo
->surface
;
554 *pSurface
= VkIcdSurfaceBase_to_handle(&surface
->base
);
559 struct wsi_wl_image
{
560 struct wsi_image base
;
561 struct wl_buffer
* buffer
;
565 struct wsi_wl_swapchain
{
566 struct wsi_swapchain base
;
568 struct wsi_wl_display
*display
;
570 struct wl_surface
* surface
;
571 uint32_t surface_version
;
572 struct wl_drm
* drm_wrapper
;
573 struct wl_callback
* frame
;
579 VkPresentModeKHR present_mode
;
582 struct wsi_wl_image images
[0];
586 wsi_wl_swapchain_get_images(struct wsi_swapchain
*wsi_chain
,
587 uint32_t *pCount
, VkImage
*pSwapchainImages
)
589 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
593 if (pSwapchainImages
== NULL
) {
594 *pCount
= chain
->base
.image_count
;
599 ret_count
= chain
->base
.image_count
;
600 if (chain
->base
.image_count
> *pCount
) {
602 result
= VK_INCOMPLETE
;
605 for (uint32_t i
= 0; i
< ret_count
; i
++)
606 pSwapchainImages
[i
] = chain
->images
[i
].base
.image
;
612 wsi_wl_swapchain_acquire_next_image(struct wsi_swapchain
*wsi_chain
,
614 VkSemaphore semaphore
,
615 uint32_t *image_index
)
617 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
619 int ret
= wl_display_dispatch_queue_pending(chain
->display
->wl_display
,
620 chain
->display
->queue
);
621 /* XXX: I'm not sure if out-of-date is the right error here. If
622 * wl_display_dispatch_queue_pending fails it most likely means we got
623 * kicked by the server so this seems more-or-less correct.
626 return VK_ERROR_OUT_OF_DATE_KHR
;
629 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
630 if (!chain
->images
[i
].busy
) {
631 /* We found a non-busy image */
633 chain
->images
[i
].busy
= true;
638 /* This time we do a blocking dispatch because we can't go
639 * anywhere until we get an event.
641 int ret
= wl_display_roundtrip_queue(chain
->display
->wl_display
,
642 chain
->display
->queue
);
644 return VK_ERROR_OUT_OF_DATE_KHR
;
649 frame_handle_done(void *data
, struct wl_callback
*callback
, uint32_t serial
)
651 struct wsi_wl_swapchain
*chain
= data
;
654 chain
->fifo_ready
= true;
656 wl_callback_destroy(callback
);
659 static const struct wl_callback_listener frame_listener
= {
664 wsi_wl_swapchain_queue_present(struct wsi_swapchain
*wsi_chain
,
666 uint32_t waitSemaphoreCount
,
667 const VkSemaphore
*pWaitSemaphores
,
668 uint32_t image_index
,
669 const VkPresentRegionKHR
*damage
)
671 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
673 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
674 while (!chain
->fifo_ready
) {
675 int ret
= wl_display_dispatch_queue(chain
->display
->wl_display
,
676 chain
->display
->queue
);
678 return VK_ERROR_OUT_OF_DATE_KHR
;
682 assert(image_index
< chain
->base
.image_count
);
683 wl_surface_attach(chain
->surface
, chain
->images
[image_index
].buffer
, 0, 0);
685 if (chain
->surface_version
>= 4 && damage
&&
686 damage
->pRectangles
&& damage
->rectangleCount
> 0) {
687 for (unsigned i
= 0; i
< damage
->rectangleCount
; i
++) {
688 const VkRectLayerKHR
*rect
= &damage
->pRectangles
[i
];
689 assert(rect
->layer
== 0);
690 wl_surface_damage_buffer(chain
->surface
,
691 rect
->offset
.x
, rect
->offset
.y
,
692 rect
->extent
.width
, rect
->extent
.height
);
695 wl_surface_damage(chain
->surface
, 0, 0, INT32_MAX
, INT32_MAX
);
698 if (chain
->base
.present_mode
== VK_PRESENT_MODE_FIFO_KHR
) {
699 chain
->frame
= wl_surface_frame(chain
->surface
);
700 wl_callback_add_listener(chain
->frame
, &frame_listener
, chain
);
701 chain
->fifo_ready
= false;
704 chain
->images
[image_index
].busy
= true;
705 wl_surface_commit(chain
->surface
);
706 wl_display_flush(chain
->display
->wl_display
);
712 buffer_handle_release(void *data
, struct wl_buffer
*buffer
)
714 struct wsi_wl_image
*image
= data
;
716 assert(image
->buffer
== buffer
);
721 static const struct wl_buffer_listener buffer_listener
= {
722 buffer_handle_release
,
726 wsi_wl_image_init(struct wsi_wl_swapchain
*chain
,
727 struct wsi_wl_image
*image
,
728 const VkSwapchainCreateInfoKHR
*pCreateInfo
,
729 const VkAllocationCallbacks
* pAllocator
)
731 VkDevice vk_device
= chain
->base
.device
;
734 result
= chain
->base
.image_fns
->create_wsi_image(vk_device
,
738 if (result
!= VK_SUCCESS
)
741 image
->buffer
= wl_drm_create_prime_buffer(chain
->drm_wrapper
,
742 image
->base
.fd
, /* name */
744 chain
->extent
.height
,
747 image
->base
.row_pitch
,
748 0, 0, 0, 0 /* unused */);
749 close(image
->base
.fd
);
754 wl_buffer_add_listener(image
->buffer
, &buffer_listener
, image
);
759 chain
->base
.image_fns
->free_wsi_image(vk_device
, pAllocator
, &image
->base
);
765 wsi_wl_swapchain_destroy(struct wsi_swapchain
*wsi_chain
,
766 const VkAllocationCallbacks
*pAllocator
)
768 struct wsi_wl_swapchain
*chain
= (struct wsi_wl_swapchain
*)wsi_chain
;
770 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
771 if (chain
->images
[i
].buffer
) {
772 wl_buffer_destroy(chain
->images
[i
].buffer
);
773 chain
->base
.image_fns
->free_wsi_image(chain
->base
.device
, pAllocator
,
774 &chain
->images
[i
].base
);
779 wl_callback_destroy(chain
->frame
);
781 wl_proxy_wrapper_destroy(chain
->surface
);
782 if (chain
->drm_wrapper
)
783 wl_proxy_wrapper_destroy(chain
->drm_wrapper
);
786 wsi_wl_display_unref(chain
->display
);
788 wsi_swapchain_finish(&chain
->base
);
790 vk_free(pAllocator
, chain
);
796 wsi_wl_surface_create_swapchain(VkIcdSurfaceBase
*icd_surface
,
798 struct wsi_device
*wsi_device
,
800 const VkSwapchainCreateInfoKHR
* pCreateInfo
,
801 const VkAllocationCallbacks
* pAllocator
,
802 const struct wsi_image_fns
*image_fns
,
803 struct wsi_swapchain
**swapchain_out
)
805 VkIcdSurfaceWayland
*surface
= (VkIcdSurfaceWayland
*)icd_surface
;
806 struct wsi_wayland
*wsi
=
807 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];
808 struct wsi_wl_swapchain
*chain
;
811 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
);
813 int num_images
= pCreateInfo
->minImageCount
;
815 size_t size
= sizeof(*chain
) + num_images
* sizeof(chain
->images
[0]);
816 chain
= vk_alloc(pAllocator
, size
, 8,
817 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
819 return VK_ERROR_OUT_OF_HOST_MEMORY
;
821 result
= wsi_swapchain_init(wsi_device
, &chain
->base
, device
,
822 pCreateInfo
, pAllocator
);
823 if (result
!= VK_SUCCESS
) {
824 vk_free(pAllocator
, chain
);
828 /* Mark a bunch of stuff as NULL. This way we can just call
829 * destroy_swapchain for cleanup.
831 for (uint32_t i
= 0; i
< num_images
; i
++)
832 chain
->images
[i
].buffer
= NULL
;
833 chain
->surface
= NULL
;
834 chain
->drm_wrapper
= NULL
;
837 bool alpha
= pCreateInfo
->compositeAlpha
==
838 VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR
;
840 chain
->base
.destroy
= wsi_wl_swapchain_destroy
;
841 chain
->base
.get_images
= wsi_wl_swapchain_get_images
;
842 chain
->base
.acquire_next_image
= wsi_wl_swapchain_acquire_next_image
;
843 chain
->base
.queue_present
= wsi_wl_swapchain_queue_present
;
844 chain
->base
.image_fns
= image_fns
;
845 chain
->base
.present_mode
= pCreateInfo
->presentMode
;
846 chain
->base
.image_count
= num_images
;
847 chain
->extent
= pCreateInfo
->imageExtent
;
848 chain
->vk_format
= pCreateInfo
->imageFormat
;
849 chain
->drm_format
= wl_drm_format_for_vk_format(chain
->vk_format
, alpha
);
851 if (pCreateInfo
->oldSwapchain
) {
852 /* If we have an oldSwapchain parameter, copy the display struct over
853 * from the old one so we don't have to fully re-initialize it.
855 struct wsi_wl_swapchain
*old_chain
= (void *)pCreateInfo
->oldSwapchain
;
856 chain
->display
= wsi_wl_display_ref(old_chain
->display
);
858 chain
->display
= NULL
;
859 result
= wsi_wl_display_create(wsi
, surface
->display
, &chain
->display
);
860 if (result
!= VK_SUCCESS
)
864 chain
->surface
= wl_proxy_create_wrapper(surface
->surface
);
865 if (!chain
->surface
) {
866 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
869 wl_proxy_set_queue((struct wl_proxy
*) chain
->surface
,
870 chain
->display
->queue
);
871 chain
->surface_version
= wl_proxy_get_version((void *)surface
->surface
);
873 chain
->drm_wrapper
= wl_proxy_create_wrapper(chain
->display
->drm
);
874 if (!chain
->drm_wrapper
) {
875 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
878 wl_proxy_set_queue((struct wl_proxy
*) chain
->drm_wrapper
,
879 chain
->display
->queue
);
881 chain
->fifo_ready
= true;
883 for (uint32_t i
= 0; i
< chain
->base
.image_count
; i
++) {
884 result
= wsi_wl_image_init(chain
, &chain
->images
[i
],
885 pCreateInfo
, pAllocator
);
886 if (result
!= VK_SUCCESS
)
888 chain
->images
[i
].busy
= false;
891 *swapchain_out
= &chain
->base
;
896 wsi_wl_swapchain_destroy(&chain
->base
, pAllocator
);
902 wsi_wl_init_wsi(struct wsi_device
*wsi_device
,
903 const VkAllocationCallbacks
*alloc
,
904 VkPhysicalDevice physical_device
,
905 const struct wsi_callbacks
*cbs
)
907 struct wsi_wayland
*wsi
;
910 wsi
= vk_alloc(alloc
, sizeof(*wsi
), 8,
911 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
);
913 result
= VK_ERROR_OUT_OF_HOST_MEMORY
;
917 wsi
->physical_device
= physical_device
;
921 wsi
->base
.get_support
= wsi_wl_surface_get_support
;
922 wsi
->base
.get_capabilities
= wsi_wl_surface_get_capabilities
;
923 wsi
->base
.get_capabilities2
= wsi_wl_surface_get_capabilities2
;
924 wsi
->base
.get_formats
= wsi_wl_surface_get_formats
;
925 wsi
->base
.get_formats2
= wsi_wl_surface_get_formats2
;
926 wsi
->base
.get_present_modes
= wsi_wl_surface_get_present_modes
;
927 wsi
->base
.create_swapchain
= wsi_wl_surface_create_swapchain
;
929 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = &wsi
->base
;
934 wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
] = NULL
;
940 wsi_wl_finish_wsi(struct wsi_device
*wsi_device
,
941 const VkAllocationCallbacks
*alloc
)
943 struct wsi_wayland
*wsi
=
944 (struct wsi_wayland
*)wsi_device
->wsi
[VK_ICD_WSI_PLATFORM_WAYLAND
];