From a312bb4285642ee25c530bbcfb8d170586910866 Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Sat, 17 May 2025 10:06:59 -0700 Subject: [PATCH] venus: refactor wsi acquire to use semaphore and fence SYNC_FD import This drops separate vn_fence_signal_wsi and vn_semaphore_signal_wsi helpers for consistency and robustness. The behavior now aligns with the Android WSI vkAcquireImageANDROID impl. This is to prepare for installing an actual payload from the compositor side. Part-of: --- src/virtio/vulkan/vn_queue.c | 22 ------------- src/virtio/vulkan/vn_queue.h | 6 ---- src/virtio/vulkan/vn_wsi.c | 64 +++++++++++++++++++++++++++++++----- 3 files changed, 55 insertions(+), 37 deletions(-) diff --git a/src/virtio/vulkan/vn_queue.c b/src/virtio/vulkan/vn_queue.c index 96acb8559fc..239c2443fe8 100644 --- a/src/virtio/vulkan/vn_queue.c +++ b/src/virtio/vulkan/vn_queue.c @@ -1427,17 +1427,6 @@ vn_fence_init_payloads(struct vn_device *dev, return VK_SUCCESS; } -void -vn_fence_signal_wsi(struct vn_device *dev, struct vn_fence *fence) -{ - struct vn_sync_payload *temp = &fence->temporary; - - vn_sync_payload_release(dev, temp); - temp->type = VN_SYNC_TYPE_IMPORTED_SYNC_FD; - temp->fd = -1; - fence->payload = temp; -} - static VkResult vn_fence_feedback_init(struct vn_device *dev, struct vn_fence *fence, @@ -1908,17 +1897,6 @@ vn_semaphore_wait_external(struct vn_device *dev, struct vn_semaphore *sem) return true; } -void -vn_semaphore_signal_wsi(struct vn_device *dev, struct vn_semaphore *sem) -{ - struct vn_sync_payload *temp = &sem->temporary; - - vn_sync_payload_release(dev, temp); - temp->type = VN_SYNC_TYPE_IMPORTED_SYNC_FD; - temp->fd = -1; - sem->payload = temp; -} - struct vn_semaphore_feedback_cmd * vn_semaphore_get_feedback_cmd(struct vn_device *dev, struct vn_semaphore *sem) { diff --git a/src/virtio/vulkan/vn_queue.h b/src/virtio/vulkan/vn_queue.h index 46b4b24c997..71f35e27528 100644 --- a/src/virtio/vulkan/vn_queue.h +++ b/src/virtio/vulkan/vn_queue.h @@ -152,10 +152,4 @@ VK_DEFINE_NONDISP_HANDLE_CASTS(vn_event, VkEvent, VK_OBJECT_TYPE_EVENT) -void -vn_fence_signal_wsi(struct vn_device *dev, struct vn_fence *fence); - -void -vn_semaphore_signal_wsi(struct vn_device *dev, struct vn_semaphore *sem); - #endif /* VN_QUEUE_H */ diff --git a/src/virtio/vulkan/vn_wsi.c b/src/virtio/vulkan/vn_wsi.c index 8a49af97d21..829b123ff4f 100644 --- a/src/virtio/vulkan/vn_wsi.c +++ b/src/virtio/vulkan/vn_wsi.c @@ -290,17 +290,63 @@ vn_AcquireNextImage2KHR(VkDevice device, vk_Result_to_str(result)); } - /* XXX this relies on implicit sync */ - if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) { - struct vn_semaphore *sem = - vn_semaphore_from_handle(pAcquireInfo->semaphore); - if (sem) - vn_semaphore_signal_wsi(dev, sem); + if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) + return vn_error(dev->instance, result); - struct vn_fence *fence = vn_fence_from_handle(pAcquireInfo->fence); - if (fence) - vn_fence_signal_wsi(dev, fence); + /* XXX this relies on implicit sync */ + int sync_fd = -1; + + int sem_fd = -1, fence_fd = -1; + if (sync_fd >= 0) { + if (pAcquireInfo->semaphore != VK_NULL_HANDLE && + pAcquireInfo->fence != VK_NULL_HANDLE) { + sem_fd = sync_fd; + fence_fd = dup(sync_fd); + if (fence_fd < 0) { + result = errno == EMFILE ? VK_ERROR_TOO_MANY_OBJECTS + : VK_ERROR_OUT_OF_HOST_MEMORY; + close(sync_fd); + return vn_error(dev->instance, result); + } + } else if (pAcquireInfo->semaphore != VK_NULL_HANDLE) { + sem_fd = sync_fd; + } else { + assert(pAcquireInfo->fence != VK_NULL_HANDLE); + fence_fd = sync_fd; + } } + if (pAcquireInfo->semaphore != VK_NULL_HANDLE) { + /* venus waits on the driver side when this semaphore is submitted */ + const VkImportSemaphoreFdInfoKHR info = { + .sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR, + .semaphore = pAcquireInfo->semaphore, + .flags = VK_SEMAPHORE_IMPORT_TEMPORARY_BIT, + .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT, + .fd = sem_fd, + }; + result = vn_ImportSemaphoreFdKHR(device, &info); + if (result == VK_SUCCESS) + sem_fd = -1; + } + + if (result == VK_SUCCESS && pAcquireInfo->fence != VK_NULL_HANDLE) { + const VkImportFenceFdInfoKHR info = { + .sType = VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR, + .fence = pAcquireInfo->fence, + .flags = VK_FENCE_IMPORT_TEMPORARY_BIT, + .handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT, + .fd = fence_fd, + }; + result = vn_ImportFenceFdKHR(device, &info); + if (result == VK_SUCCESS) + fence_fd = -1; + } + + if (sem_fd >= 0) + close(sem_fd); + if (fence_fd >= 0) + close(fence_fd); + return vn_result(dev->instance, result); }