From 127263dc4a2bd425495b915e0fab223661884fd7 Mon Sep 17 00:00:00 2001 From: Chia-I Wu Date: Thu, 13 Jan 2022 12:02:46 -0800 Subject: [PATCH] venus: remember the memory bound to a swapchain image It will be needed for VkBindImageMemorySwapchainInfoKHR. Reviewed-by: Yiwei Zhang Part-of: --- src/virtio/vulkan/vn_android.c | 5 +++-- src/virtio/vulkan/vn_command_buffer.c | 6 +++--- src/virtio/vulkan/vn_image.c | 22 ++++++++++++++++++++-- src/virtio/vulkan/vn_image.h | 19 ++++++++++++++----- src/virtio/vulkan/vn_wsi.c | 4 ++-- 5 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/virtio/vulkan/vn_android.c b/src/virtio/vulkan/vn_android.c index 4fd7d618da0..0067b8c3f87 100644 --- a/src/virtio/vulkan/vn_android.c +++ b/src/virtio/vulkan/vn_android.c @@ -515,9 +515,10 @@ vn_android_image_from_anb(struct vn_device *dev, if (result != VK_SUCCESS) goto fail; - img->is_wsi = true; + img->wsi.is_wsi = true; /* Android WSI image owns the memory */ - img->private_memory = memory; + img->wsi.memory = vn_device_memory_from_handle(memory); + img->wsi.memory_owned = true; *out_img = img; return VK_SUCCESS; diff --git a/src/virtio/vulkan/vn_command_buffer.c b/src/virtio/vulkan/vn_command_buffer.c index 1933eb8da5f..0e71328fd5b 100644 --- a/src/virtio/vulkan/vn_command_buffer.c +++ b/src/virtio/vulkan/vn_command_buffer.c @@ -126,13 +126,13 @@ vn_cmd_fix_image_memory_barrier(const struct vn_command_buffer *cmd, out_barrier->newLayout != VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) return; - assert(img->is_wsi); + assert(img->wsi.is_wsi); if (VN_PRESENT_SRC_INTERNAL_LAYOUT == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) return; /* prime blit src or no layout transition */ - if (img->is_prime_blit_src || + if (img->wsi.is_prime_blit_src || out_barrier->oldLayout == out_barrier->newLayout) { if (out_barrier->oldLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) out_barrier->oldLayout = VN_PRESENT_SRC_INTERNAL_LAYOUT; @@ -1251,7 +1251,7 @@ vn_CmdCopyImageToBuffer(VkCommandBuffer commandBuffer, /* sanity check */ const struct vn_image *img = vn_image_from_handle(srcImage); - prime_blit = img->is_wsi && img->is_prime_blit_src; + prime_blit = img->wsi.is_wsi && img->wsi.is_prime_blit_src; assert(prime_blit); } diff --git a/src/virtio/vulkan/vn_image.c b/src/virtio/vulkan/vn_image.c index 2dd63309927..206b458e8e0 100644 --- a/src/virtio/vulkan/vn_image.c +++ b/src/virtio/vulkan/vn_image.c @@ -295,8 +295,10 @@ vn_DestroyImage(VkDevice device, if (!img) return; - if (img->private_memory != VK_NULL_HANDLE) - vn_FreeMemory(device, img->private_memory, pAllocator); + if (img->wsi.memory && img->wsi.memory_owned) { + VkDeviceMemory mem_handle = vn_device_memory_to_handle(img->wsi.memory); + vn_FreeMemory(device, mem_handle, pAllocator); + } vn_async_vkDestroyImage(dev->instance, device, image, NULL); @@ -396,6 +398,13 @@ vn_GetImageSparseMemoryRequirements2( pSparseMemoryRequirements); } +static void +vn_image_bind_wsi_memory(struct vn_image *img, struct vn_device_memory *mem) +{ + assert(img->wsi.is_wsi && !img->wsi.memory); + img->wsi.memory = mem; +} + VkResult vn_BindImageMemory(VkDevice device, VkImage image, @@ -403,8 +412,12 @@ vn_BindImageMemory(VkDevice device, VkDeviceSize memoryOffset) { struct vn_device *dev = vn_device_from_handle(device); + struct vn_image *img = vn_image_from_handle(image); struct vn_device_memory *mem = vn_device_memory_from_handle(memory); + if (img->wsi.is_wsi) + vn_image_bind_wsi_memory(img, mem); + if (mem->base_memory) { memory = vn_device_memory_to_handle(mem->base_memory); memoryOffset += mem->base_offset; @@ -427,8 +440,13 @@ vn_BindImageMemory2(VkDevice device, VkBindImageMemoryInfo *local_infos = NULL; for (uint32_t i = 0; i < bindInfoCount; i++) { const VkBindImageMemoryInfo *info = &pBindInfos[i]; + struct vn_image *img = vn_image_from_handle(info->image); struct vn_device_memory *mem = vn_device_memory_from_handle(info->memory); + + if (img->wsi.is_wsi) + vn_image_bind_wsi_memory(img, mem); + /* TODO handle VkBindImageMemorySwapchainInfoKHR */ if (!mem || !mem->base_memory) continue; diff --git a/src/virtio/vulkan/vn_image.h b/src/virtio/vulkan/vn_image.h index f91d5294aea..02a3a4f76fa 100644 --- a/src/virtio/vulkan/vn_image.h +++ b/src/virtio/vulkan/vn_image.h @@ -36,15 +36,24 @@ struct vn_image { struct vn_image_memory_requirements requirements[4]; - bool is_wsi; - bool is_prime_blit_src; - - /* For VK_ANDROID_native_buffer, the WSI image owns the memory, */ - VkDeviceMemory private_memory; /* For VK_ANDROID_external_memory_android_hardware_buffer, real image * creation is deferred until bind image memory. */ struct vn_image_create_deferred_info *deferred_info; + + struct { + /* True if this is a swapchain image and VK_IMAGE_LAYOUT_PRESENT_SRC_KHR + * is a valid layout. A swapchain image can be created internally + * (wsi_image_create_info) or externally (VkNativeBufferANDROID). + */ + bool is_wsi; + bool is_prime_blit_src; + + struct vn_device_memory *memory; + + /* For VK_ANDROID_native_buffer, the WSI image owns the memory. */ + bool memory_owned; + } wsi; }; VK_DEFINE_NONDISP_HANDLE_CASTS(vn_image, base.base, diff --git a/src/virtio/vulkan/vn_wsi.c b/src/virtio/vulkan/vn_wsi.c index 663062ad0b1..795b6a9fb16 100644 --- a/src/virtio/vulkan/vn_wsi.c +++ b/src/virtio/vulkan/vn_wsi.c @@ -132,8 +132,8 @@ vn_wsi_create_image(struct vn_device *dev, if (result != VK_SUCCESS) return result; - img->is_wsi = true; - img->is_prime_blit_src = wsi_info->prime_blit_src; + img->wsi.is_wsi = true; + img->wsi.is_prime_blit_src = wsi_info->prime_blit_src; *out_img = img; return VK_SUCCESS;