Module: Mesa Branch: main Commit: a47992734ad5ba7c582d5f39d601c2b3c0fdd146 URL: http://cgit.freedesktop.org/mesa/mesa/commit/?id=a47992734ad5ba7c582d5f39d601c2b3c0fdd146
Author: Yiwei Zhang <zzyi...@chromium.org> Date: Fri Sep 8 20:08:34 2023 -0700 venus: use common AHB management and export impl vk_device_memory_create handles AHB export alloc and import tracking. Also use common GetMemoryAndroidHardwareBufferANDROID impl. Signed-off-by: Yiwei Zhang <zzyi...@chromium.org> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/25184> --- src/virtio/vulkan/vn_android.c | 114 ++++++----------------------------- src/virtio/vulkan/vn_android.h | 42 +++---------- src/virtio/vulkan/vn_device_memory.c | 25 +++----- src/virtio/vulkan/vn_device_memory.h | 3 - 4 files changed, 35 insertions(+), 149 deletions(-) diff --git a/src/virtio/vulkan/vn_android.c b/src/virtio/vulkan/vn_android.c index 8738d14a78d..ff945c63014 100644 --- a/src/virtio/vulkan/vn_android.c +++ b/src/virtio/vulkan/vn_android.c @@ -915,24 +915,22 @@ vn_android_get_drm_format_modifier_info( } VkResult -vn_android_device_import_ahb(struct vn_device *dev, - struct vn_device_memory *mem, - const VkMemoryAllocateInfo *alloc_info, - struct AHardwareBuffer *ahb, - bool internal_ahb) +vn_android_device_import_ahb( + struct vn_device *dev, + struct vn_device_memory *mem, + const struct VkMemoryDedicatedAllocateInfo *dedicated_info) { - const VkMemoryDedicatedAllocateInfo *dedicated_info = - vk_find_struct_const(alloc_info->pNext, MEMORY_DEDICATED_ALLOCATE_INFO); + const struct vk_device_memory *mem_vk = &mem->base.base; const native_handle_t *handle = NULL; int dma_buf_fd = -1; int dup_fd = -1; uint64_t alloc_size = 0; uint32_t mem_type_bits = 0; - uint32_t mem_type_index = alloc_info->memoryTypeIndex; + uint32_t mem_type_index = mem_vk->memory_type_index; bool force_unmappable = false; VkResult result = VK_SUCCESS; - handle = AHardwareBuffer_getNativeHandle(ahb); + handle = AHardwareBuffer_getNativeHandle(mem_vk->ahardware_buffer); dma_buf_fd = vn_android_gralloc_get_dma_buf_fd(handle); if (dma_buf_fd < 0) return VK_ERROR_INVALID_EXTERNAL_HANDLE; @@ -967,14 +965,19 @@ vn_android_device_import_ahb(struct vn_device *dev, alloc_size = mem_req->size; - /* XXX Workaround before spec issue #2762 gets resolved. If importing an - * internally allocated AHB from the exportable path, memoryTypeIndex is - * undefined while defaulting to zero, which can be incompatible with - * the queried memoryTypeBits from the combined memory requirement and - * dma_buf fd properties. Thus we override the requested memoryTypeIndex - * to an applicable one if existed. + /* Per spec 11.2.3. Device Memory Allocation + * + * If the parameters define an export operation and the external handle + * type is + * VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID, + * implementations should not strictly follow memoryTypeIndex. Instead, + * they should modify the allocation internally to use the required + * memory type for the application’s given usage. This is because for an + * export operation, there is currently no way for the client to know + * the memory type index before allocating. */ - if (internal_ahb) { + if (!(mem_vk->import_handle_type & + VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)) { if ((mem_type_bits & mem_req->memoryTypeBits) == 0) { vn_log(dev->instance, "memoryTypeBits: img(0x%X) fd(0x%X)", mem_req->memoryTypeBits, mem_type_bits); @@ -1040,85 +1043,6 @@ vn_android_device_import_ahb(struct vn_device *dev, return result; } - AHardwareBuffer_acquire(ahb); - mem->ahb = ahb; - - return VK_SUCCESS; -} - -VkResult -vn_android_device_allocate_ahb(struct vn_device *dev, - struct vn_device_memory *mem, - const VkMemoryAllocateInfo *alloc_info, - const VkAllocationCallbacks *alloc) -{ - const VkMemoryDedicatedAllocateInfo *dedicated_info = - vk_find_struct_const(alloc_info->pNext, MEMORY_DEDICATED_ALLOCATE_INFO); - uint32_t width = 0; - uint32_t height = 1; - uint32_t layers = 1; - uint32_t format = 0; - uint64_t usage = 0; - struct AHardwareBuffer *ahb = NULL; - - if (dedicated_info && dedicated_info->image != VK_NULL_HANDLE) { - const VkImageCreateInfo *image_info = - &vn_image_from_handle(dedicated_info->image)->deferred_info->create; - assert(image_info); - width = image_info->extent.width; - height = image_info->extent.height; - layers = image_info->arrayLayers; - format = vk_image_format_to_ahb_format(image_info->format); - usage = - vk_image_usage_to_ahb_usage(image_info->flags, image_info->usage); - } else { - const VkPhysicalDeviceMemoryProperties *mem_props = - &dev->physical_device->memory_properties; - - assert(alloc_info->memoryTypeIndex < mem_props->memoryTypeCount); - - width = alloc_info->allocationSize; - format = AHARDWAREBUFFER_FORMAT_BLOB; - usage = AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER; - if (mem_props->memoryTypes[alloc_info->memoryTypeIndex].propertyFlags & - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { - usage |= AHARDWAREBUFFER_USAGE_CPU_READ_RARELY | - AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY; - } - } - - ahb = vn_android_ahb_allocate(width, height, layers, format, usage); - if (!ahb) - return VK_ERROR_OUT_OF_HOST_MEMORY; - - VkResult result = - vn_android_device_import_ahb(dev, mem, alloc_info, ahb, true); - - /* ahb alloc has already acquired a ref and import will acquire another, - * must release one here to avoid leak. - */ - AHardwareBuffer_release(ahb); - - return result; -} - -void -vn_android_release_ahb(struct AHardwareBuffer *ahb) -{ - AHardwareBuffer_release(ahb); -} - -VkResult -vn_GetMemoryAndroidHardwareBufferANDROID( - VkDevice device, - const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo, - struct AHardwareBuffer **pBuffer) -{ - struct vn_device_memory *mem = vn_device_memory_from_handle(pInfo->memory); - - AHardwareBuffer_acquire(mem->ahb); - *pBuffer = mem->ahb; - return VK_SUCCESS; } diff --git a/src/virtio/vulkan/vn_android.h b/src/virtio/vulkan/vn_android.h index 187ca4d0f64..8813255d852 100644 --- a/src/virtio/vulkan/vn_android.h +++ b/src/virtio/vulkan/vn_android.h @@ -42,20 +42,10 @@ vn_android_get_ahb_usage(const VkImageUsageFlags usage, const VkImageCreateFlags flags); VkResult -vn_android_device_import_ahb(struct vn_device *dev, - struct vn_device_memory *mem, - const VkMemoryAllocateInfo *alloc_info, - struct AHardwareBuffer *ahb, - bool internal_ahb); - -VkResult -vn_android_device_allocate_ahb(struct vn_device *dev, - struct vn_device_memory *mem, - const VkMemoryAllocateInfo *alloc_info, - const VkAllocationCallbacks *alloc); - -void -vn_android_release_ahb(struct AHardwareBuffer *ahb); +vn_android_device_import_ahb( + struct vn_device *dev, + struct vn_device_memory *mem, + const struct VkMemoryDedicatedAllocateInfo *dedicated_info); VkFormat vn_android_drm_format_to_vk_format(uint32_t format); @@ -101,30 +91,14 @@ vn_android_get_ahb_usage(UNUSED const VkImageUsageFlags usage, } static inline VkResult -vn_android_device_import_ahb(UNUSED struct vn_device *dev, - UNUSED struct vn_device_memory *mem, - UNUSED const VkMemoryAllocateInfo *alloc_info, - UNUSED struct AHardwareBuffer *ahb, - UNUSED bool internal_ahb) +vn_android_device_import_ahb( + UNUSED struct vn_device *dev, + UNUSED struct vn_device_memory *mem, + UNUSED const struct VkMemoryDedicatedAllocateInfo *dedicated_info) { return VK_ERROR_OUT_OF_HOST_MEMORY; } -static inline VkResult -vn_android_device_allocate_ahb(UNUSED struct vn_device *dev, - UNUSED struct vn_device_memory *mem, - UNUSED const VkMemoryAllocateInfo *alloc_info, - UNUSED const VkAllocationCallbacks *alloc) -{ - return VK_ERROR_OUT_OF_HOST_MEMORY; -} - -static inline void -vn_android_release_ahb(UNUSED struct AHardwareBuffer *ahb) -{ - return; -} - static inline VkFormat vn_android_drm_format_to_vk_format(UNUSED uint32_t format) { diff --git a/src/virtio/vulkan/vn_device_memory.c b/src/virtio/vulkan/vn_device_memory.c index 037901c84ea..e532fffbb3b 100644 --- a/src/virtio/vulkan/vn_device_memory.c +++ b/src/virtio/vulkan/vn_device_memory.c @@ -530,8 +530,6 @@ vn_AllocateMemory(VkDevice device, { VN_TRACE_FUNC(); struct vn_device *dev = vn_device_from_handle(device); - const VkAllocationCallbacks *alloc = - pAllocator ? pAllocator : &dev->base.base.alloc; /* see vn_physical_device_init_memory_properties */ VkMemoryAllocateInfo local_info; @@ -545,15 +543,12 @@ vn_AllocateMemory(VkDevice device, const VkExportMemoryAllocateInfo *export_info = NULL; const VkImportAndroidHardwareBufferInfoANDROID *import_ahb_info = NULL; const VkImportMemoryFdInfoKHR *import_fd_info = NULL; - bool export_ahb = false; + const VkMemoryDedicatedAllocateInfo *dedicated_info = NULL; vk_foreach_struct_const(pnext, pAllocateInfo->pNext) { switch (pnext->sType) { case VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO: export_info = (void *)pnext; - if (export_info->handleTypes & - VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID) - export_ahb = true; - else if (!export_info->handleTypes) + if (!export_info->handleTypes) export_info = NULL; break; case VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID: @@ -562,6 +557,9 @@ vn_AllocateMemory(VkDevice device, case VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR: import_fd_info = (void *)pnext; break; + case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO: + dedicated_info = (void *)pnext; + break; default: break; } @@ -580,13 +578,9 @@ vn_AllocateMemory(VkDevice device, mem->is_import = import_ahb_info || import_fd_info; mem->is_external = mem->is_import || export_info; - VkDeviceMemory mem_handle = vn_device_memory_to_handle(mem); VkResult result; - if (import_ahb_info) { - result = vn_android_device_import_ahb(dev, mem, pAllocateInfo, - import_ahb_info->buffer, false); - } else if (export_ahb) { - result = vn_android_device_allocate_ahb(dev, mem, pAllocateInfo, alloc); + if (mem->base.base.ahardware_buffer) { + result = vn_android_device_import_ahb(dev, mem, dedicated_info); } else if (import_fd_info) { result = vn_device_memory_import_dma_buf(dev, mem, pAllocateInfo, false, import_fd_info->fd); @@ -608,7 +602,7 @@ vn_AllocateMemory(VkDevice device, return vn_error(dev->instance, result); } - *pMemory = mem_handle; + *pMemory = vn_device_memory_to_handle(mem); return VK_SUCCESS; } @@ -638,9 +632,6 @@ vn_FreeMemory(VkDevice device, vn_device_memory_free_simple(dev, mem); } - if (mem->ahb) - vn_android_release_ahb(mem->ahb); - vk_device_memory_destroy(&dev->base.base, pAllocator, &mem->base.base); } diff --git a/src/virtio/vulkan/vn_device_memory.h b/src/virtio/vulkan/vn_device_memory.h index 6029850360b..978cbc6f60f 100644 --- a/src/virtio/vulkan/vn_device_memory.h +++ b/src/virtio/vulkan/vn_device_memory.h @@ -63,9 +63,6 @@ struct vn_device_memory { VkDeviceSize base_offset; VkDeviceSize map_end; - - /* non-NULL when backed by AHB */ - struct AHardwareBuffer *ahb; }; VK_DEFINE_NONDISP_HANDLE_CASTS(vn_device_memory, base.base.base,