vulkan/android: Add vk_android_import_anb_memory helper

Extract the AllocateMemory/import-FD portion of vk_android_import_anb()
into vk_android_import_anb_memory(). This lets drivers with deferred
image creation import ANB memory without triggering BindImageMemory2.

Signed-off-by: Valentine Burley <valentine.burley@collabora.com>
Reviewed-by: Yiwei Zhang <zzyiwei@chromium.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/40635>
This commit is contained in:
Valentine Burley 2026-03-25 13:25:47 +01:00 committed by Marge Bot
parent 8d4fb52919
commit e0965d82b7
2 changed files with 39 additions and 24 deletions

View file

@ -190,19 +190,12 @@ vk_gralloc_to_drm_explicit_layout(
}
VkResult
vk_android_import_anb(struct vk_device *device,
const VkImageCreateInfo *pCreateInfo,
const VkAllocationCallbacks *alloc,
struct vk_image *image)
vk_android_import_anb_memory(struct vk_device *device,
struct vk_image *image,
const VkNativeBufferANDROID *anb,
const VkAllocationCallbacks *alloc)
{
VkResult result;
const VkNativeBufferANDROID *native_buffer =
vk_find_struct_const(pCreateInfo->pNext, NATIVE_BUFFER_ANDROID);
assert(native_buffer);
assert(native_buffer->handle);
assert(native_buffer->handle->numFds > 0);
assert(anb && anb->handle && anb->handle->numFds > 0);
const VkMemoryDedicatedAllocateInfo ded_alloc = {
.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
@ -214,24 +207,40 @@ vk_android_import_anb(struct vk_device *device,
.sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
.pNext = &ded_alloc,
.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
.fd = os_dupfd_cloexec(native_buffer->handle->data[0]),
.fd = os_dupfd_cloexec(anb->handle->data[0]),
};
result = device->dispatch_table.AllocateMemory(
(VkDevice)device,
&(VkMemoryAllocateInfo){
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.pNext = &import_info,
.allocationSize = lseek(import_info.fd, 0, SEEK_END),
.memoryTypeIndex = 0, /* Should we be smarter here? */
},
alloc, &image->anb_memory);
const VkMemoryAllocateInfo alloc_info = {
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
.pNext = &import_info,
.allocationSize = lseek(import_info.fd, 0, SEEK_END),
.memoryTypeIndex = 0,
};
VkResult result = device->dispatch_table.AllocateMemory(
(VkDevice)device, &alloc_info, alloc, &image->anb_memory);
if (result != VK_SUCCESS) {
close(import_info.fd);
return result;
}
return VK_SUCCESS;
}
VkResult
vk_android_import_anb(struct vk_device *device,
const VkImageCreateInfo *pCreateInfo,
const VkAllocationCallbacks *alloc,
struct vk_image *image)
{
const VkNativeBufferANDROID *native_buffer =
vk_find_struct_const(pCreateInfo->pNext, NATIVE_BUFFER_ANDROID);
assert(native_buffer);
VkResult result = vk_android_import_anb_memory(device, image,
native_buffer, alloc);
if (result != VK_SUCCESS)
return result;
VkBindImageMemoryInfo bind_info = {
.sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO,
.image = (VkImage)image,

View file

@ -26,6 +26,7 @@
#include <stdbool.h>
#include "vulkan/vulkan_core.h"
#include "vulkan/vk_android_native_buffer.h"
#include "util/detect_os.h"
@ -46,6 +47,11 @@ VkResult vk_android_import_anb(struct vk_device *device,
const VkAllocationCallbacks *alloc,
struct vk_image *image);
VkResult vk_android_import_anb_memory(struct vk_device *device,
struct vk_image *image,
const VkNativeBufferANDROID *anb,
const VkAllocationCallbacks *alloc);
VkResult vk_android_get_anb_layout(
const VkImageCreateInfo *pCreateInfo,
VkImageDrmFormatModifierExplicitCreateInfoEXT *out,