Skip to content

Commit

Permalink
anv/android: resolve ANB swapchain images on bind
Browse files Browse the repository at this point in the history
Like AHB, we don't know the layout for an image backed by gralloc
swapchain memory until bind when gralloc information is passed by the
platform.

Signed-off-by: Juston Li <[email protected]>
Reviewed-by: Tapani Pälli <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/29850>
(cherry picked from commit 43cb986)
  • Loading branch information
juston-li authored and JeevakaPrabu committed Dec 11, 2024
1 parent 925915f commit 60a899f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/intel/vulkan/anv_android.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,22 @@ anv_image_bind_from_gralloc(struct anv_device *device,
"failed to import dma-buf from VkNativeBufferANDROID");
}

uint64_t img_size = image->bindings[ANV_IMAGE_MEMORY_BINDING_MAIN].memory_range.size;
if (img_size < bo->size) {
VkMemoryRequirements2 mem_reqs = {
.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
};

anv_image_get_memory_requirements(device, image, image->vk.aspects,
&mem_reqs);

VkDeviceSize aligned_image_size =
align64(mem_reqs.memoryRequirements.size,
mem_reqs.memoryRequirements.alignment);

if (bo->size < aligned_image_size) {
result = vk_errorf(device, VK_ERROR_INVALID_EXTERNAL_HANDLE,
"dma-buf from VkNativeBufferANDROID is too small for "
"VkImage: %"PRIu64"B < %"PRIu64"B",
bo->size, img_size);
bo->size, aligned_image_size);
anv_device_release_bo(device, bo);
return result;
}
Expand Down
47 changes: 44 additions & 3 deletions src/intel/vulkan/anv_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,15 @@ anv_image_init(struct anv_device *device, struct anv_image *image,

image->n_planes = anv_get_format_planes(image->vk.format);

#ifdef VK_USE_PLATFORM_ANDROID_KHR
/* In the case of gralloc-backed swap chain image, we don't know the
* layout yet.
*/
if (vk_find_struct_const(pCreateInfo->pNext,
IMAGE_SWAPCHAIN_CREATE_INFO_KHR) != NULL)
return VK_SUCCESS;
#endif

image->from_wsi =
vk_find_struct_const(pCreateInfo->pNext, WSI_IMAGE_CREATE_INFO_MESA) != NULL;

Expand Down Expand Up @@ -1863,9 +1872,9 @@ VkResult anv_CreateImage(
__LINE__, pCreateInfo->flags);

#ifndef VK_USE_PLATFORM_ANDROID_KHR
/* Ignore swapchain creation info on Android. Since we don't have an
* implementation in Mesa, we're guaranteed to access an Android object
* incorrectly.
/* Skip the WSI common swapchain creation here on Android. Similar to ahw,
* this case is handled by a partial image init and then resolved when the
* image is bound and gralloc info is passed.
*/
const VkImageSwapchainCreateInfoKHR *swapchain_info =
vk_find_struct_const(pCreateInfo->pNext, IMAGE_SWAPCHAIN_CREATE_INFO_KHR);
Expand Down Expand Up @@ -1956,6 +1965,36 @@ resolve_ahw_image(struct anv_device *device,
#endif
}

static void
resolve_anb_image(struct anv_device *device,
struct anv_image *image,
const VkNativeBufferANDROID *gralloc_info)
{
#if DETECT_OS_ANDROID && ANDROID_API_LEVEL >= 29
VkResult result;

/* Check tiling. */
enum isl_tiling tiling;
struct u_gralloc_buffer_handle gr_handle = {
.handle = gralloc_info->handle,
.hal_format = gralloc_info->format,
.pixel_stride = gralloc_info->stride,
};
result = anv_android_get_tiling(device, &gr_handle, &tiling);
assert(result == VK_SUCCESS);

isl_tiling_flags_t isl_tiling_flags = (1u << tiling);

/* Now we are able to fill anv_image fields properly and create
* isl_surface for it.
*/
result = add_all_surfaces_implicit_layout(device, image, NULL, gralloc_info->stride,
isl_tiling_flags,
ISL_SURF_USAGE_DISABLE_AUX_BIT);
assert(result == VK_SUCCESS);
#endif
}

void
anv_image_get_memory_requirements(struct anv_device *device,
struct anv_image *image,
Expand Down Expand Up @@ -2366,6 +2405,8 @@ anv_bind_image_memory(struct anv_device *device,
gralloc_info);
if (result != VK_SUCCESS)
return result;

resolve_anb_image(device, image, gralloc_info);
did_bind = true;
break;
}
Expand Down

0 comments on commit 60a899f

Please sign in to comment.