From 9de59472174633812d62a1be7a2a06c8724b978d Mon Sep 17 00:00:00 2001 From: Jeongik Cha Date: Wed, 14 Oct 2026 22:26:00 +0900 Subject: [PATCH] vulkan: export memory info for non-colorbuffer allocations in AndroidAHB mode When running in AndroidAHB mode, setupVkBuffer allocates host visible buffers without colorBufferInfo. Omission of VkExportMemoryAllocateInfo causes driver allocation failure or crash on hosts that require exportable memory for external buffers. Include VkExportMemoryAllocateInfo whenever AHB import is not used in AndroidAHB mode. Test: Run vkcube on Android guest VM --- host/vulkan/vk_common_operations.cpp | 48 +++++++++++++++++----------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/host/vulkan/vk_common_operations.cpp b/host/vulkan/vk_common_operations.cpp index 50de4f4a4..2bdbfa9cf 100644 --- a/host/vulkan/vk_common_operations.cpp +++ b/host/vulkan/vk_common_operations.cpp @@ -2233,21 +2233,6 @@ bool VkEmulation::allocExternalMemory(VulkanDispatch* vk, VkEmulation::ExternalM auto allocInfoChain = vk_make_chain_iterator(&allocInfo); - // HostAllocation mode uses host side allocation and should not add VkExportMemoryAllocateInfo. - // AndroidAHB mode uses AHardwareBuffer_allocate + import instead of export (see switch below). - if (mDeviceInfo.supportsExternalMemoryExport && - getExternalMemoryMode() != ExternalMemory::Mode::HostAllocation && - getExternalMemoryMode() != ExternalMemory::Mode::AndroidAHB) { - exportAi.handleTypes = - static_cast(getDefaultExternalMemoryHandleType()); - - if (mDeviceInfo.supportsDmaBuf) { - exportAi.handleTypes |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT; - } - - vk_append_struct(&allocInfoChain, &exportAi); - } - if (bufferForDedicatedAllocation.hasValue() || imageForDedicatedAllocation.hasValue()) { info->dedicatedAllocation = true; if (bufferForDedicatedAllocation.hasValue()) { @@ -2489,6 +2474,29 @@ bool VkEmulation::allocExternalMemory(VulkanDispatch* vk, VkEmulation::ExternalM break; } + // HostAllocation mode uses host side allocation and should not add VkExportMemoryAllocateInfo. + // AndroidAHB mode uses AHardwareBuffer_allocate + import for ColorBuffers instead of export. + // Non-ColorBuffer allocations (e.g. VkBuffer in setupVkBuffer) or when AHB import was not used + // (e.g. allocAhb failure) still rely on export, so they must add VkExportMemoryAllocateInfo. + bool isAndroidAhbImport = false; +#ifdef __ANDROID__ + isAndroidAhbImport = (getExternalMemoryMode() == ExternalMemory::Mode::AndroidAHB && + importAhbInfo.buffer != nullptr); +#endif + + if (mDeviceInfo.supportsExternalMemoryExport && + getExternalMemoryMode() != ExternalMemory::Mode::HostAllocation && + !isAndroidAhbImport) { + exportAi.handleTypes = + static_cast(getDefaultExternalMemoryHandleType()); + + if (mDeviceInfo.supportsDmaBuf) { + exportAi.handleTypes |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT; + } + + vk_append_struct(&allocInfoChain, &exportAi); + } + bool memoryAllocated = false; std::vector allocationAttempts; constexpr size_t kMaxAllocationAttempts = 20u; @@ -2626,10 +2634,12 @@ bool VkEmulation::allocExternalMemory(VulkanDispatch* vk, VkEmulation::ExternalM exportRes = vk->vkGetMemoryAndroidHardwareBufferANDROID(mDevice, &getAhbInfo, &exportHandle); validHandle = (VK_SUCCESS == exportRes) && (NULL != exportHandle); - info->handleInfo = ExternalHandleInfo{ - .handle = reinterpret_cast(exportHandle), - .streamHandleType = STREAM_HANDLE_TYPE_PLATFORM_AHB, - }; + if (validHandle) { + info->handleInfo = ExternalHandleInfo{ + .handle = reinterpret_cast(exportHandle), + .streamHandleType = STREAM_HANDLE_TYPE_PLATFORM_AHB, + }; + } } #endif break;