From 63aedb592266448de73a58593677f8989b535a86 Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Sun, 20 Jul 2025 22:50:21 -0700 Subject: [PATCH] lavapipe: use common vk_device_memory::ahardware_buffer This drops most of the lavapipe specific codes for AHB handling. Reviewed-by: Konstantin Seurer Part-of: --- src/gallium/frontends/lavapipe/lvp_android.c | 49 ++------------------ src/gallium/frontends/lavapipe/lvp_device.c | 32 +------------ src/gallium/frontends/lavapipe/lvp_private.h | 13 +----- 3 files changed, 7 insertions(+), 87 deletions(-) diff --git a/src/gallium/frontends/lavapipe/lvp_android.c b/src/gallium/frontends/lavapipe/lvp_android.c index a94c0e92741..2332d55eb39 100644 --- a/src/gallium/frontends/lavapipe/lvp_android.c +++ b/src/gallium/frontends/lavapipe/lvp_android.c @@ -25,6 +25,7 @@ #if ANDROID_API_LEVEL >= 26 #include +#include #endif #include @@ -115,10 +116,10 @@ lvp_QueueSignalReleaseImageANDROID(VkQueue _queue, } VkResult -lvp_import_ahb_memory(struct lvp_device *device, struct lvp_device_memory *mem, - const VkImportAndroidHardwareBufferInfoANDROID *info) +lvp_import_ahb_memory(struct lvp_device *device, struct lvp_device_memory *mem) { - const native_handle_t *handle = AHardwareBuffer_getNativeHandle(info->buffer); + const native_handle_t *handle = + AHardwareBuffer_getNativeHandle(mem->vk.ahardware_buffer); int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1; if (dma_buf < 0) return VK_ERROR_INVALID_EXTERNAL_HANDLE; @@ -128,51 +129,9 @@ lvp_import_ahb_memory(struct lvp_device *device, struct lvp_device_memory *mem, if (!result) return VK_ERROR_INVALID_EXTERNAL_HANDLE; - AHardwareBuffer_acquire(info->buffer); - mem->android_hardware_buffer = info->buffer; mem->size = size; mem->map = device->pscreen->map_memory(device->pscreen, mem->pmem); mem->memory_type = LVP_DEVICE_MEMORY_TYPE_DMA_BUF; return VK_SUCCESS; } - -VkResult -lvp_create_ahb_memory(struct lvp_device *device, struct lvp_device_memory *mem, - const VkMemoryAllocateInfo *pAllocateInfo) -{ - mem->android_hardware_buffer = vk_alloc_ahardware_buffer(pAllocateInfo); - if (mem->android_hardware_buffer == NULL) - return VK_ERROR_OUT_OF_HOST_MEMORY; - - const struct VkImportAndroidHardwareBufferInfoANDROID import_info = { - .buffer = mem->android_hardware_buffer, - }; - - VkResult result = lvp_import_ahb_memory(device, mem, &import_info); - - /* Release a reference to avoid leak for AHB allocation. */ - AHardwareBuffer_release(mem->android_hardware_buffer); - - return result; -} - -#if ANDROID_API_LEVEL >= 26 -VkResult -lvp_GetMemoryAndroidHardwareBufferANDROID( - VkDevice device, - const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo, - struct AHardwareBuffer **pBuffer) -{ - LVP_FROM_HANDLE(lvp_device_memory, mem, pInfo->memory); - - if (mem->android_hardware_buffer) { - *pBuffer = mem->android_hardware_buffer; - /* Increase refcount. */ - AHardwareBuffer_acquire(*pBuffer); - return VK_SUCCESS; - } - - return VK_ERROR_INVALID_EXTERNAL_HANDLE; -} -#endif diff --git a/src/gallium/frontends/lavapipe/lvp_device.c b/src/gallium/frontends/lavapipe/lvp_device.c index b05cc309d7d..ad46779fe4b 100644 --- a/src/gallium/frontends/lavapipe/lvp_device.c +++ b/src/gallium/frontends/lavapipe/lvp_device.c @@ -1960,14 +1960,10 @@ VKAPI_ATTR VkResult VKAPI_CALL lvp_AllocateMemory( ASSERTED const VkExportMemoryAllocateInfo *export_info = NULL; ASSERTED const VkImportMemoryFdInfoKHR *import_info = NULL; const VkMemoryAllocateFlagsInfo *mem_flags = NULL; -#if DETECT_OS_ANDROID - ASSERTED const VkImportAndroidHardwareBufferInfoANDROID *ahb_import_info = NULL; -#endif const VkImportMemoryHostPointerInfoEXT *host_ptr_info = NULL; VkResult error = VK_ERROR_OUT_OF_DEVICE_MEMORY; assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO); int priority = 0; - UNUSED bool is_ahb_export_alloc = false; vk_foreach_struct_const(ext, pAllocateInfo->pNext) { switch ((unsigned)ext->sType) { @@ -1991,22 +1987,11 @@ VKAPI_ATTR VkResult VKAPI_CALL lvp_AllocateMemory( case VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO: mem_flags = (void*)ext; break; -#if DETECT_OS_ANDROID - case VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID: { - ahb_import_info = (VkImportAndroidHardwareBufferInfoANDROID*)ext; - break; - } -#endif default: break; } } -#if DETECT_OS_ANDROID - is_ahb_export_alloc = !ahb_import_info && export_info && - export_info->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID; -#endif - #ifdef PIPE_MEMORY_FD if (import_info != NULL && import_info->fd < 0) { return vk_error(device->instance, VK_ERROR_INVALID_EXTERNAL_HANDLE); @@ -2022,10 +2007,6 @@ VKAPI_ATTR VkResult VKAPI_CALL lvp_AllocateMemory( mem->backed_fd = -1; mem->size = pAllocateInfo->allocationSize; -#if DETECT_OS_ANDROID - mem->android_hardware_buffer = NULL; -#endif - if (host_ptr_info) { mem->mem_alloc = (struct llvmpipe_memory_allocation) { .cpu_addr = host_ptr_info->pHostPointer, @@ -2035,12 +2016,8 @@ VKAPI_ATTR VkResult VKAPI_CALL lvp_AllocateMemory( mem->memory_type = LVP_DEVICE_MEMORY_TYPE_USER_PTR; } #if DETECT_OS_ANDROID - else if(ahb_import_info) { - error = lvp_import_ahb_memory(device, mem, ahb_import_info); - if (error != VK_SUCCESS) - goto fail; - } else if (is_ahb_export_alloc) { - error = lvp_create_ahb_memory(device, mem, pAllocateInfo); + else if (mem->vk.ahardware_buffer) { + error = lvp_import_ahb_memory(device, mem); if (error != VK_SUCCESS) goto fail; } @@ -2130,11 +2107,6 @@ VKAPI_ATTR void VKAPI_CALL lvp_FreeMemory( break; #ifdef PIPE_MEMORY_FD case LVP_DEVICE_MEMORY_TYPE_DMA_BUF: -#if DETECT_OS_ANDROID - if (mem->android_hardware_buffer) - AHardwareBuffer_release(mem->android_hardware_buffer); - FALLTHROUGH; -#endif case LVP_DEVICE_MEMORY_TYPE_OPAQUE_FD: device->pscreen->free_memory_fd(device->pscreen, mem->pmem); if(mem->backed_fd >= 0) diff --git a/src/gallium/frontends/lavapipe/lvp_private.h b/src/gallium/frontends/lavapipe/lvp_private.h index 5dc9c349dc5..69c47eedd6b 100644 --- a/src/gallium/frontends/lavapipe/lvp_private.h +++ b/src/gallium/frontends/lavapipe/lvp_private.h @@ -49,10 +49,6 @@ #include "drm-uapi/drm_fourcc.h" #endif -#if DETECT_OS_ANDROID -#include -#endif - /* Pre-declarations needed for WSI entrypoints */ struct wl_surface; struct wl_display; @@ -253,9 +249,6 @@ struct lvp_device_memory { void * map; enum lvp_device_memory_type memory_type; int backed_fd; -#if DETECT_OS_ANDROID - struct AHardwareBuffer *android_hardware_buffer; -#endif }; struct lvp_pipe_sync { @@ -809,11 +802,7 @@ lvp_nv_dgc_token_to_cmd_type(const VkIndirectCommandsLayoutTokenNV *token); #if DETECT_OS_ANDROID VkResult -lvp_import_ahb_memory(struct lvp_device *device, struct lvp_device_memory *mem, - const VkImportAndroidHardwareBufferInfoANDROID *info); -VkResult -lvp_create_ahb_memory(struct lvp_device *device, struct lvp_device_memory *mem, - const VkMemoryAllocateInfo *pAllocateInfo); +lvp_import_ahb_memory(struct lvp_device *device, struct lvp_device_memory *mem); #endif enum vk_cmd_type