From beb80858d853b9d043cd574377a4f390d5f5fcd8 Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Thu, 27 May 2021 19:47:53 +0000 Subject: [PATCH] venus: refactor for property query of dma_buf fd With a TODO to route host storage size instead of that from the guest. Signed-off-by: Yiwei Zhang Reviewed-by: Chia-I Wu Part-of: --- src/virtio/vulkan/vn_android.c | 48 ++++++++-------------- src/virtio/vulkan/vn_device_memory.c | 61 +++++++++++++++++++--------- src/virtio/vulkan/vn_device_memory.h | 6 +++ 3 files changed, 64 insertions(+), 51 deletions(-) diff --git a/src/virtio/vulkan/vn_android.c b/src/virtio/vulkan/vn_android.c index d597d9dd30c..0311a5807f4 100644 --- a/src/virtio/vulkan/vn_android.c +++ b/src/virtio/vulkan/vn_android.c @@ -259,30 +259,6 @@ vn_android_get_dma_buf_from_native_handle(const native_handle_t *handle, return VK_SUCCESS; } -static VkResult -vn_android_get_mem_type_bits_from_dma_buf(VkDevice device, - int dma_buf, - uint32_t *out_mem_type_bits) -{ - VkMemoryFdPropertiesKHR fd_props = { - .sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR, - .pNext = NULL, - .memoryTypeBits = 0, - }; - VkResult result = vn_GetMemoryFdPropertiesKHR( - device, VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, dma_buf, - &fd_props); - if (result != VK_SUCCESS) - return result; - - if (!fd_props.memoryTypeBits) - return VK_ERROR_INVALID_EXTERNAL_HANDLE; - - *out_mem_type_bits = fd_props.memoryTypeBits; - - return VK_SUCCESS; -} - static bool vn_android_get_gralloc_buffer_info(buffer_handle_t handle, uint32_t out_strides[4], @@ -376,6 +352,7 @@ vn_android_image_from_anb(struct vn_device *dev, VkDeviceMemory memory = VK_NULL_HANDLE; VkImage image = VK_NULL_HANDLE; struct vn_image *img = NULL; + uint64_t alloc_size = 0; uint32_t mem_type_bits = 0; int dma_buf_fd = -1; int dup_fd = -1; @@ -470,15 +447,21 @@ vn_android_image_from_anb(struct vn_device *dev, goto fail; } - result = vn_android_get_mem_type_bits_from_dma_buf(device, dma_buf_fd, - &mem_type_bits); + result = vn_get_memory_dma_buf_properties(dev, dma_buf_fd, &alloc_size, + &mem_type_bits); if (result != VK_SUCCESS) goto fail; - if (VN_DEBUG(WSI)) - vn_log(dev->instance, "memoryTypeBits = img(0x%X) & fd(0x%X)", - mem_req.memoryTypeBits, mem_type_bits); + if (VN_DEBUG(WSI)) { + vn_log(dev->instance, + "size = img(%" PRIu64 ") fd(%" PRIu64 "), " + "memoryTypeBits = img(0x%X) & fd(0x%X)", + mem_req.size, alloc_size, mem_req.memoryTypeBits, mem_type_bits); + } + /* TODO When alloc_size is fixed to return host storage size, we will + * also check alloc_size is not smaller than mem_req.size here. + */ mem_type_bits &= mem_req.memoryTypeBits; if (!mem_type_bits) { result = VK_ERROR_INVALID_EXTERNAL_HANDLE; @@ -845,6 +828,7 @@ vn_GetAndroidHardwareBufferPropertiesANDROID( struct vn_device *dev = vn_device_from_handle(device); VkResult result = VK_SUCCESS; int dma_buf_fd = -1; + uint64_t alloc_size = 0; uint32_t mem_type_bits = 0; VkAndroidHardwareBufferFormatPropertiesANDROID *format_props = @@ -862,12 +846,12 @@ vn_GetAndroidHardwareBufferPropertiesANDROID( if (result != VK_SUCCESS) return vn_error(dev->instance, result); - result = vn_android_get_mem_type_bits_from_dma_buf(device, dma_buf_fd, - &mem_type_bits); + result = vn_get_memory_dma_buf_properties(dev, dma_buf_fd, &alloc_size, + &mem_type_bits); if (result != VK_SUCCESS) return vn_error(dev->instance, result); - pProperties->allocationSize = lseek(dma_buf_fd, 0, SEEK_END); + pProperties->allocationSize = alloc_size; pProperties->memoryTypeBits = mem_type_bits; return VK_SUCCESS; diff --git a/src/virtio/vulkan/vn_device_memory.c b/src/virtio/vulkan/vn_device_memory.c index 639887f17e1..8b4feb62595 100644 --- a/src/virtio/vulkan/vn_device_memory.c +++ b/src/virtio/vulkan/vn_device_memory.c @@ -466,6 +466,41 @@ vn_GetMemoryFdKHR(VkDevice device, return VK_SUCCESS; } +VkResult +vn_get_memory_dma_buf_properties(struct vn_device *dev, + int fd, + uint64_t *out_alloc_size, + uint32_t *out_mem_type_bits) +{ + VkDevice device = vn_device_to_handle(dev); + struct vn_renderer_bo *bo = NULL; + VkResult result = VK_SUCCESS; + + result = vn_renderer_bo_create_from_dma_buf(dev->renderer, 0 /* size */, + fd, 0 /* flags */, &bo); + if (result != VK_SUCCESS) + return result; + + vn_instance_roundtrip(dev->instance); + + VkMemoryResourcePropertiesMESA props = { + .sType = VK_STRUCTURE_TYPE_MEMORY_RESOURCE_PROPERTIES_MESA, + .pNext = NULL, + .memoryTypeBits = 0, + }; + result = vn_call_vkGetMemoryResourcePropertiesMESA(dev->instance, device, + bo->res_id, &props); + vn_renderer_bo_unref(dev->renderer, bo); + if (result != VK_SUCCESS) + return result; + + /* XXX extend VkMemoryResourcePropertiesMESA for host storage size */ + *out_alloc_size = lseek(fd, 0, SEEK_END); + *out_mem_type_bits = props.memoryTypeBits; + + return VK_SUCCESS; +} + VkResult vn_GetMemoryFdPropertiesKHR(VkDevice device, VkExternalMemoryHandleTypeFlagBits handleType, @@ -473,31 +508,19 @@ vn_GetMemoryFdPropertiesKHR(VkDevice device, VkMemoryFdPropertiesKHR *pMemoryFdProperties) { struct vn_device *dev = vn_device_from_handle(device); + uint64_t alloc_size = 0; + uint32_t mem_type_bits = 0; + VkResult result = VK_SUCCESS; if (handleType != VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT) return vn_error(dev->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE); - struct vn_renderer_bo *bo; - VkResult result = - vn_renderer_bo_create_from_dma_buf(dev->renderer, 0, fd, 0, &bo); - if (result != VK_SUCCESS) - return vn_error(dev->instance, result); - vn_instance_roundtrip(dev->instance); - - VkMemoryResourcePropertiesMESA memory_resource_properties = { - .sType = VK_STRUCTURE_TYPE_MEMORY_RESOURCE_PROPERTIES_MESA, - .pNext = NULL, - .memoryTypeBits = 0, - }; - result = vn_call_vkGetMemoryResourcePropertiesMESA( - dev->instance, device, bo->res_id, &memory_resource_properties); + result = + vn_get_memory_dma_buf_properties(dev, fd, &alloc_size, &mem_type_bits); if (result != VK_SUCCESS) return vn_error(dev->instance, result); - pMemoryFdProperties->memoryTypeBits = - memory_resource_properties.memoryTypeBits; + pMemoryFdProperties->memoryTypeBits = mem_type_bits; - vn_renderer_bo_unref(dev->renderer, bo); - - return result; + return VK_SUCCESS; } diff --git a/src/virtio/vulkan/vn_device_memory.h b/src/virtio/vulkan/vn_device_memory.h index d2452082618..f480ad811bc 100644 --- a/src/virtio/vulkan/vn_device_memory.h +++ b/src/virtio/vulkan/vn_device_memory.h @@ -49,4 +49,10 @@ vn_device_memory_import_dma_buf(struct vn_device *dev, const VkMemoryAllocateInfo *alloc_info, int fd); +VkResult +vn_get_memory_dma_buf_properties(struct vn_device *dev, + int fd, + uint64_t *out_alloc_size, + uint32_t *out_mem_type_bits); + #endif /* VN_DEVICE_MEMORY_H */