From 4b0b75c27a79893803f1927fee5a847f6ce5c3d4 Mon Sep 17 00:00:00 2001 From: Faith Ekstrand Date: Mon, 20 Mar 2023 18:00:38 -0500 Subject: [PATCH] anv: Use the new vk_device_memory base struct Reviewed-by: Lina Versace Part-of: --- src/intel/vulkan/anv_android.c | 66 +--------------------- src/intel/vulkan/anv_android.h | 5 +- src/intel/vulkan/anv_android_stubs.c | 5 +- src/intel/vulkan/anv_device.c | 103 ++++++++--------------------------- src/intel/vulkan/anv_image.c | 6 +- src/intel/vulkan/anv_private.h | 16 +----- 6 files changed, 35 insertions(+), 166 deletions(-) diff --git a/src/intel/vulkan/anv_android.c b/src/intel/vulkan/anv_android.c index a0260aa..2b0b4fa 100644 --- a/src/intel/vulkan/anv_android.c +++ b/src/intel/vulkan/anv_android.c @@ -306,34 +306,6 @@ anv_GetAndroidHardwareBufferPropertiesANDROID( return VK_SUCCESS; } -VkResult -anv_GetMemoryAndroidHardwareBufferANDROID( - VkDevice device_h, - const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo, - struct AHardwareBuffer **pBuffer) -{ - ANV_FROM_HANDLE(anv_device_memory, mem, pInfo->memory); - - /* Some quotes from Vulkan spec: - * - * "If the device memory was created by importing an Android hardware - * buffer, vkGetMemoryAndroidHardwareBufferANDROID must return that same - * Android hardware buffer object." - * - * "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must - * have been included in VkExportMemoryAllocateInfo::handleTypes when - * memory was created." - */ - if (mem->ahw) { - *pBuffer = mem->ahw; - /* Increase refcount. */ - AHardwareBuffer_acquire(mem->ahw); - return VK_SUCCESS; - } - - return VK_ERROR_OUT_OF_HOST_MEMORY; -} - #endif /* @@ -341,15 +313,14 @@ anv_GetMemoryAndroidHardwareBufferANDROID( */ VkResult anv_import_ahw_memory(VkDevice device_h, - struct anv_device_memory *mem, - const VkImportAndroidHardwareBufferInfoANDROID *info) + struct anv_device_memory *mem) { #if ANDROID_API_LEVEL >= 26 ANV_FROM_HANDLE(anv_device, device, device_h); /* Import from AHardwareBuffer to anv_device_memory. */ const native_handle_t *handle = - AHardwareBuffer_getNativeHandle(info->buffer); + AHardwareBuffer_getNativeHandle(mem->vk.ahardware_buffer); /* NOTE - We support buffers with only one handle but do not error on * multiple handle case. Reason is that we want to support YUV formats @@ -365,14 +336,6 @@ anv_import_ahw_memory(VkDevice device_h, &mem->bo); assert(result == VK_SUCCESS); - /* "If the vkAllocateMemory command succeeds, the implementation must - * acquire a reference to the imported hardware buffer, which it must - * release when the device memory object is freed. If the command fails, - * the implementation must not retain a reference." - */ - AHardwareBuffer_acquire(info->buffer); - mem->ahw = info->buffer; - return VK_SUCCESS; #else return VK_ERROR_EXTENSION_NOT_PRESENT; @@ -380,31 +343,6 @@ anv_import_ahw_memory(VkDevice device_h, } VkResult -anv_create_ahw_memory(VkDevice device_h, - struct anv_device_memory *mem, - const VkMemoryAllocateInfo *pAllocateInfo) -{ -#if ANDROID_API_LEVEL >= 26 - struct AHardwareBuffer *ahw = vk_alloc_ahardware_buffer(pAllocateInfo); - if (ahw == NULL) - return VK_ERROR_OUT_OF_HOST_MEMORY; - - const VkImportAndroidHardwareBufferInfoANDROID import_info = { - .buffer = ahw, - }; - VkResult result = anv_import_ahw_memory(device_h, mem, &import_info); - - /* Release a reference to avoid leak for AHB allocation. */ - AHardwareBuffer_release(ahw); - - return result; -#else - return VK_ERROR_EXTENSION_NOT_PRESENT; -#endif - -} - -VkResult anv_image_init_from_gralloc(struct anv_device *device, struct anv_image *image, const VkImageCreateInfo *base_info, diff --git a/src/intel/vulkan/anv_android.h b/src/intel/vulkan/anv_android.h index d8c3113..e1f099e 100644 --- a/src/intel/vulkan/anv_android.h +++ b/src/intel/vulkan/anv_android.h @@ -47,10 +47,9 @@ VkResult anv_image_bind_from_gralloc(struct anv_device *device, unsigned anv_ahb_format_for_vk_format(VkFormat vk_format); VkResult anv_import_ahw_memory(VkDevice device_h, - struct anv_device_memory *mem, - const VkImportAndroidHardwareBufferInfoANDROID *info); + struct anv_device_memory *mem); VkResult anv_create_ahw_memory(VkDevice device_h, struct anv_device_memory *mem, - const VkMemoryAllocateInfo *pAllocateInfo); + const VkMemoryDedicatedAllocateInfo *dedicated_info); #endif /* ANV_ANDROID_H */ diff --git a/src/intel/vulkan/anv_android_stubs.c b/src/intel/vulkan/anv_android_stubs.c index 53e0af0..4e8c05f 100644 --- a/src/intel/vulkan/anv_android_stubs.c +++ b/src/intel/vulkan/anv_android_stubs.c @@ -41,8 +41,7 @@ VkResult anv_image_bind_from_gralloc(struct anv_device *device, VkResult anv_import_ahw_memory(VkDevice device_h, - struct anv_device_memory *mem, - const VkImportAndroidHardwareBufferInfoANDROID *info) + struct anv_device_memory *mem) { return VK_ERROR_EXTENSION_NOT_PRESENT; } @@ -50,7 +49,7 @@ anv_import_ahw_memory(VkDevice device_h, VkResult anv_create_ahw_memory(VkDevice device_h, struct anv_device_memory *mem, - const VkMemoryAllocateInfo *pAllocateInfo) + const VkMemoryDedicatedAllocateInfo *dedicated_info) { return VK_ERROR_EXTENSION_NOT_PRESENT; } diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c index 61949da..8a5e157 100644 --- a/src/intel/vulkan/anv_device.c +++ b/src/intel/vulkan/anv_device.c @@ -3668,53 +3668,28 @@ VkResult anv_AllocateMemory( if (mem_heap_used + aligned_alloc_size > mem_heap->size) return vk_error(device, VK_ERROR_OUT_OF_DEVICE_MEMORY); - mem = vk_object_alloc(&device->vk, pAllocator, sizeof(*mem), - VK_OBJECT_TYPE_DEVICE_MEMORY); + mem = vk_device_memory_create(&device->vk, pAllocateInfo, + pAllocator, sizeof(*mem)); if (mem == NULL) return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY); - mem->size = pAllocateInfo->allocationSize; mem->type = mem_type; mem->map = NULL; mem->map_size = 0; mem->map_delta = 0; - mem->ahw = NULL; - mem->host_ptr = NULL; enum anv_bo_alloc_flags alloc_flags = 0; - const VkExportMemoryAllocateInfo *export_info = NULL; - const VkImportAndroidHardwareBufferInfoANDROID *ahw_import_info = NULL; const VkImportMemoryFdInfoKHR *fd_info = NULL; - const VkImportMemoryHostPointerInfoEXT *host_ptr_info = NULL; const VkMemoryDedicatedAllocateInfo *dedicated_info = NULL; - VkMemoryAllocateFlags vk_flags = 0; uint64_t client_address = 0; vk_foreach_struct_const(ext, pAllocateInfo->pNext) { switch (ext->sType) { - case VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO: - export_info = (void *)ext; - break; - - case VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID: - ahw_import_info = (void *)ext; - break; - case VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR: fd_info = (void *)ext; break; - case VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT: - host_ptr_info = (void *)ext; - break; - - case VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO: { - const VkMemoryAllocateFlagsInfo *flags_info = (void *)ext; - vk_flags = flags_info->flags; - break; - } - case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO: dedicated_info = (void *)ext; break; @@ -3770,32 +3745,15 @@ VkResult anv_AllocateMemory( (mem_type->propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) alloc_flags |= ANV_BO_ALLOC_WRITE_COMBINE; - if (vk_flags & VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT) + if (mem->vk.alloc_flags & VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT) alloc_flags |= ANV_BO_ALLOC_CLIENT_VISIBLE_ADDRESS; - if ((export_info && export_info->handleTypes) || - (fd_info && fd_info->handleType) || - (host_ptr_info && host_ptr_info->handleType)) { - /* Anything imported or exported is EXTERNAL */ + /* Anything imported or exported is EXTERNAL */ + if (mem->vk.export_handle_types || mem->vk.import_handle_type) alloc_flags |= ANV_BO_ALLOC_EXTERNAL; - } - - /* Check if we need to support Android HW buffer export. If so, - * create AHardwareBuffer and import memory from it. - */ - bool android_export = false; - if (export_info && export_info->handleTypes & - VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID) - android_export = true; - - if (ahw_import_info) { - result = anv_import_ahw_memory(_device, mem, ahw_import_info); - if (result != VK_SUCCESS) - goto fail; - goto success; - } else if (android_export) { - result = anv_create_ahw_memory(_device, mem, pAllocateInfo); + if (mem->vk.ahardware_buffer) { + result = anv_import_ahw_memory(_device, mem); if (result != VK_SUCCESS) goto fail; @@ -3848,26 +3806,25 @@ VkResult anv_AllocateMemory( goto success; } - if (host_ptr_info && host_ptr_info->handleType) { - if (host_ptr_info->handleType == + if (mem->vk.host_ptr) { + if (mem->vk.import_handle_type == VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_MAPPED_FOREIGN_MEMORY_BIT_EXT) { result = vk_error(device, VK_ERROR_INVALID_EXTERNAL_HANDLE); goto fail; } - assert(host_ptr_info->handleType == + assert(mem->vk.import_handle_type == VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT); result = anv_device_import_bo_from_host_ptr(device, - host_ptr_info->pHostPointer, - pAllocateInfo->allocationSize, + mem->vk.host_ptr, + mem->vk.size, alloc_flags, client_address, &mem->bo); if (result != VK_SUCCESS) goto fail; - mem->host_ptr = host_ptr_info->pHostPointer; goto success; } @@ -3915,7 +3872,7 @@ VkResult anv_AllocateMemory( return VK_SUCCESS; fail: - vk_object_free(&device->vk, pAllocator, mem); + vk_device_memory_destroy(&device->vk, pAllocator, &mem->vk); return result; } @@ -4015,12 +3972,7 @@ void anv_FreeMemory( anv_device_release_bo(device, mem->bo); -#if defined(ANDROID) && ANDROID_API_LEVEL >= 26 - if (mem->ahw) - AHardwareBuffer_release(mem->ahw); -#endif - - vk_object_free(&device->vk, pAllocator, mem); + vk_device_memory_destroy(&device->vk, pAllocator, &mem->vk); } VkResult anv_MapMemory2KHR( @@ -4036,8 +3988,8 @@ VkResult anv_MapMemory2KHR( return VK_SUCCESS; } - if (mem->host_ptr) { - *ppData = mem->host_ptr + pMemoryMapInfo->offset; + if (mem->vk.host_ptr) { + *ppData = mem->vk.host_ptr + pMemoryMapInfo->offset; return VK_SUCCESS; } @@ -4051,20 +4003,11 @@ VkResult anv_MapMemory2KHR( "Memory object not mappable."); } + assert(pMemoryMapInfo->size > 0); const VkDeviceSize offset = pMemoryMapInfo->offset; - const VkDeviceSize size = pMemoryMapInfo->size == VK_WHOLE_SIZE ? - mem->size - offset : pMemoryMapInfo->size; - - /* From the Vulkan spec version 1.0.32 docs for MapMemory: - * - * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0 - * assert(size != 0); - * * If size is not equal to VK_WHOLE_SIZE, size must be less than or - * equal to the size of the memory minus offset - */ - assert(size > 0); - assert(offset < mem->size); - assert(size <= mem->size - offset); + const VkDeviceSize size = + vk_device_memory_range(&mem->vk, pMemoryMapInfo->offset, + pMemoryMapInfo->size); if (size != (size_t)size) { return vk_errorf(device, VK_ERROR_MEMORY_MAP_FAILED, @@ -4114,7 +4057,7 @@ VkResult anv_UnmapMemory2KHR( ANV_FROM_HANDLE(anv_device, device, _device); ANV_FROM_HANDLE(anv_device_memory, mem, pMemoryUnmapInfo->memory); - if (mem == NULL || mem->host_ptr) + if (mem == NULL || mem->vk.host_ptr) return VK_SUCCESS; anv_device_unmap_bo(device, mem->bo, mem->map, mem->map_size); @@ -4205,8 +4148,8 @@ anv_bind_buffer_memory(const VkBindBufferMemoryInfo *pBindInfo) assert(pBindInfo->sType == VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO); if (mem) { - assert(pBindInfo->memoryOffset < mem->size); - assert(mem->size - pBindInfo->memoryOffset >= buffer->vk.size); + assert(pBindInfo->memoryOffset < mem->vk.size); + assert(mem->vk.size - pBindInfo->memoryOffset >= buffer->vk.size); buffer->address = (struct anv_address) { .bo = mem->bo, .offset = pBindInfo->memoryOffset, diff --git a/src/intel/vulkan/anv_image.c b/src/intel/vulkan/anv_image.c index f94dc9c..a2ebc25 100644 --- a/src/intel/vulkan/anv_image.c +++ b/src/intel/vulkan/anv_image.c @@ -1571,9 +1571,9 @@ resolve_ahw_image(struct anv_device *device, struct anv_device_memory *mem) { #if defined(ANDROID) && ANDROID_API_LEVEL >= 26 - assert(mem->ahw); + assert(mem->vk.ahardware_buffer); AHardwareBuffer_Desc desc; - AHardwareBuffer_describe(mem->ahw, &desc); + AHardwareBuffer_describe(mem->vk.ahardware_buffer, &desc); VkResult result; /* Check tiling. */ @@ -1778,7 +1778,7 @@ VkResult anv_BindImageMemory2( bool did_bind = false; /* Resolve will alter the image's aspects, do this first. */ - if (mem && mem->ahw) + if (mem && mem->vk.ahardware_buffer) resolve_ahw_image(device, image, mem); vk_foreach_struct_const(s, bind_info->pNext) { diff --git a/src/intel/vulkan/anv_private.h b/src/intel/vulkan/anv_private.h index 8449760..45dd36a 100644 --- a/src/intel/vulkan/anv_private.h +++ b/src/intel/vulkan/anv_private.h @@ -75,6 +75,7 @@ #include "vk_debug_report.h" #include "vk_descriptor_update_template.h" #include "vk_device.h" +#include "vk_device_memory.h" #include "vk_drm_syncobj.h" #include "vk_enum_defines.h" #include "vk_format.h" @@ -1574,10 +1575,7 @@ _anv_combine_address(struct anv_batch *batch, void *location, /* #define __gen_address_offset anv_address_add */ struct anv_device_memory { - struct vk_object_base base; - - /** Client-requested allocaiton size */ - uint64_t size; + struct vk_device_memory vk; struct list_head link; @@ -1589,14 +1587,6 @@ struct anv_device_memory { /* The map, from the user PoV is map + map_delta */ uint64_t map_delta; - - /* If set, we are holding reference to AHardwareBuffer - * which we must release when memory is freed. - */ - struct AHardwareBuffer * ahw; - - /* If set, this memory comes from a host pointer. */ - void * host_ptr; }; /** @@ -4319,7 +4309,7 @@ VK_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set, base, VkDescriptorSet, VK_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set_layout, base, VkDescriptorSetLayout, VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT) -VK_DEFINE_NONDISP_HANDLE_CASTS(anv_device_memory, base, VkDeviceMemory, +VK_DEFINE_NONDISP_HANDLE_CASTS(anv_device_memory, vk.base, VkDeviceMemory, VK_OBJECT_TYPE_DEVICE_MEMORY) VK_DEFINE_NONDISP_HANDLE_CASTS(anv_event, base, VkEvent, VK_OBJECT_TYPE_EVENT) VK_DEFINE_NONDISP_HANDLE_CASTS(anv_image, vk.base, VkImage, VK_OBJECT_TYPE_IMAGE) -- 2.7.4