From b9bbeb77c76314cf3c3206a7e1bbd54e00aae1c5 Mon Sep 17 00:00:00 2001 From: Rob Clark Date: Mon, 6 May 2024 10:47:42 -0700 Subject: [PATCH] vulkan/android: Add helper to probe AHB support GetPhysicalDeviceImageFormatProperties() must that an image {format, flags, usage} combo is unsupported if gralloc will not be able to perform the allocation. The practical way to test this is to do a small test allocation. Signed-off-by: Rob Clark Reviewed-by: Roman Stratiienko Part-of: --- src/android_stub/nativewindow_stub.cpp | 6 +++++ src/vulkan/runtime/vk_android.c | 33 ++++++++++++++++++++++++++ src/vulkan/runtime/vk_android.h | 12 ++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/android_stub/nativewindow_stub.cpp b/src/android_stub/nativewindow_stub.cpp index b0600778691..9276a9c3d2e 100644 --- a/src/android_stub/nativewindow_stub.cpp +++ b/src/android_stub/nativewindow_stub.cpp @@ -31,6 +31,12 @@ AHardwareBuffer_allocate(const AHardwareBuffer_Desc *desc, return 0; } +int +AHardwareBuffer_isSupported(const AHardwareBuffer_Desc* desc) +{ + return 0; +} + const native_handle_t * AHardwareBuffer_getNativeHandle(const AHardwareBuffer *buffer) { diff --git a/src/vulkan/runtime/vk_android.c b/src/vulkan/runtime/vk_android.c index 59afd1b71e6..6073ee37ddf 100644 --- a/src/vulkan/runtime/vk_android.c +++ b/src/vulkan/runtime/vk_android.c @@ -477,6 +477,39 @@ vk_image_usage_to_ahb_usage(const VkImageCreateFlags vk_create, return ahb_usage; } +/* Probe gralloc implementation to test whether it can allocate a buffer + * for the given format and usage. Vk drivers must not advertise support + * for AHB backed VkImage's if the gralloc implementation is not able to + * perform the allocation. + */ +bool +vk_ahb_probe_format(VkFormat vk_format, + VkImageCreateFlags vk_create, + VkImageUsageFlags vk_usage) +{ + AHardwareBuffer_Desc desc = { + .width = 16, + .height = 16, + .layers = 1, + .format = vk_image_format_to_ahb_format(vk_format), + .usage = vk_image_usage_to_ahb_usage(vk_create, vk_usage), + }; +#if ANDROID_API_LEVEL >= 29 + return AHardwareBuffer_isSupported(&desc); +#else + AHardwareBuffer *ahb = NULL; + int ret = 0; + + ret = AHardwareBuffer_allocate(&desc, &ahb); + if (ret) + return false; + + AHardwareBuffer_release(ahb); + + return true; +#endif +} + struct AHardwareBuffer * vk_alloc_ahardware_buffer(const VkMemoryAllocateInfo *pAllocateInfo) { diff --git a/src/vulkan/runtime/vk_android.h b/src/vulkan/runtime/vk_android.h index 448887d5797..5c7fb10f299 100644 --- a/src/vulkan/runtime/vk_android.h +++ b/src/vulkan/runtime/vk_android.h @@ -110,6 +110,10 @@ uint32_t vk_image_format_to_ahb_format(VkFormat vk_format); uint64_t vk_image_usage_to_ahb_usage(const VkImageCreateFlags vk_create, const VkImageUsageFlags vk_usage); +bool vk_ahb_probe_format(VkFormat vk_format, + VkImageCreateFlags vk_create, + VkImageUsageFlags vk_usage); + struct AHardwareBuffer * vk_alloc_ahardware_buffer(const VkMemoryAllocateInfo *pAllocateInfo); @@ -134,6 +138,14 @@ vk_image_usage_to_ahb_usage(const VkImageCreateFlags vk_create, return 0; } +static inline bool +vk_ahb_probe_format(VkFormat vk_format, + VkImageCreateFlags vk_create, + VkImageUsageFlags vk_usage) +{ + return false; +} + static inline struct AHardwareBuffer * vk_alloc_ahardware_buffer(const VkMemoryAllocateInfo *pAllocateInfo) {