+VkResult
+anv_GetMemoryAndroidHardwareBufferANDROID(
+ VkDevice device_h,
+ const VkMemoryGetAndroidHardwareBufferInfoANDROID *pInfo,
+ struct AHardwareBuffer **pBuffer)
+{
+ ANV_FROM_HANDLE(anv_device_memory, mem, pInfo->memory);
+
+ /* Some quotes from Vulkan spec:
+ *
+ * "If the device memory was created by importing an Android hardware
+ * buffer, vkGetMemoryAndroidHardwareBufferANDROID must return that same
+ * Android hardware buffer object."
+ *
+ * "VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID must
+ * have been included in VkExportMemoryAllocateInfo::handleTypes when
+ * memory was created."
+ */
+ if (mem->ahw) {
+ *pBuffer = mem->ahw;
+ /* Increase refcount. */
+ AHardwareBuffer_acquire(mem->ahw);
+ return VK_SUCCESS;
+ }
+
+ return VK_ERROR_OUT_OF_HOST_MEMORY;
+}
+
+#endif
+
+/* Construct ahw usage mask from image usage bits, see
+ * 'AHardwareBuffer Usage Equivalence' in Vulkan spec.
+ */
+uint64_t
+anv_ahw_usage_from_vk_usage(const VkImageCreateFlags vk_create,
+ const VkImageUsageFlags vk_usage)
+{
+ uint64_t ahw_usage = 0;
+#if ANDROID_API_LEVEL >= 26
+ if (vk_usage & VK_IMAGE_USAGE_SAMPLED_BIT)
+ ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
+
+ if (vk_usage & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)
+ ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
+
+ if (vk_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
+ ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT;
+
+ if (vk_create & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT)
+ ahw_usage |= AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP;
+
+ if (vk_create & VK_IMAGE_CREATE_PROTECTED_BIT)
+ ahw_usage |= AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT;
+
+ /* No usage bits set - set at least one GPU usage. */
+ if (ahw_usage == 0)
+ ahw_usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
+#endif
+ return ahw_usage;
+}
+
+/*
+ * Called from anv_AllocateMemory when import AHardwareBuffer.
+ */
+VkResult
+anv_import_ahw_memory(VkDevice device_h,
+ struct anv_device_memory *mem,
+ const VkImportAndroidHardwareBufferInfoANDROID *info)
+{
+#if ANDROID_API_LEVEL >= 26
+ ANV_FROM_HANDLE(anv_device, device, device_h);
+
+ /* Import from AHardwareBuffer to anv_device_memory. */
+ const native_handle_t *handle =
+ AHardwareBuffer_getNativeHandle(info->buffer);
+
+ /* NOTE - We support buffers with only one handle but do not error on
+ * multiple handle case. Reason is that we want to support YUV formats
+ * where we have many logical planes but they all point to the same
+ * buffer, like is the case with VK_FORMAT_G8_B8R8_2PLANE_420_UNORM.
+ */
+ int dma_buf = (handle && handle->numFds) ? handle->data[0] : -1;
+ if (dma_buf < 0)
+ return VK_ERROR_INVALID_EXTERNAL_HANDLE;
+
+ uint64_t bo_flags = ANV_BO_EXTERNAL;
+ if (device->instance->physicalDevice.supports_48bit_addresses)
+ bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
+ if (device->instance->physicalDevice.use_softpin)
+ bo_flags |= EXEC_OBJECT_PINNED;
+
+ VkResult result = anv_bo_cache_import(device, &device->bo_cache,
+ dma_buf, bo_flags, &mem->bo);
+ assert(VK_SUCCESS);
+
+ /* "If the vkAllocateMemory command succeeds, the implementation must
+ * acquire a reference to the imported hardware buffer, which it must
+ * release when the device memory object is freed. If the command fails,
+ * the implementation must not retain a reference."
+ */
+ AHardwareBuffer_acquire(info->buffer);
+ mem->ahw = info->buffer;
+
+ return VK_SUCCESS;
+#else
+ return VK_ERROR_EXTENSION_NOT_PRESENT;
+#endif
+}
+
+VkResult
+anv_create_ahw_memory(VkDevice device_h,
+ struct anv_device_memory *mem,
+ const VkMemoryAllocateInfo *pAllocateInfo)
+{
+#if ANDROID_API_LEVEL >= 26
+ ANV_FROM_HANDLE(anv_device, dev, device_h);
+
+ const VkMemoryDedicatedAllocateInfo *dedicated_info =
+ vk_find_struct_const(pAllocateInfo->pNext,
+ MEMORY_DEDICATED_ALLOCATE_INFO);
+
+ uint32_t w = 0;
+ uint32_t h = 1;
+ uint32_t layers = 1;
+ uint32_t format = 0;
+ uint64_t usage = 0;
+
+ /* If caller passed dedicated information. */
+ if (dedicated_info && dedicated_info->image) {
+ ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
+ w = image->extent.width;
+ h = image->extent.height;
+ layers = image->array_size;
+ format = android_format_from_vk(image->vk_format);
+ usage = anv_ahw_usage_from_vk_usage(image->create_flags, image->usage);
+ } else if (dedicated_info && dedicated_info->buffer) {
+ ANV_FROM_HANDLE(anv_buffer, buffer, dedicated_info->buffer);
+ w = buffer->size;
+ format = AHARDWAREBUFFER_FORMAT_BLOB;
+ usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+ AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+ } else {
+ w = pAllocateInfo->allocationSize;
+ format = AHARDWAREBUFFER_FORMAT_BLOB;
+ usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN |
+ AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN;
+ }
+
+ struct AHardwareBuffer *ahw = NULL;
+ struct AHardwareBuffer_Desc desc = {
+ .width = w,
+ .height = h,
+ .layers = layers,
+ .format = format,
+ .usage = usage,
+ };
+
+ if (AHardwareBuffer_allocate(&desc, &ahw) != 0)
+ return VK_ERROR_OUT_OF_HOST_MEMORY;
+
+ mem->ahw = ahw;
+ return VK_SUCCESS;
+#else
+ return VK_ERROR_EXTENSION_NOT_PRESENT;
+#endif
+
+}
+
+VkResult
+anv_image_from_external(
+ VkDevice device_h,
+ const VkImageCreateInfo *base_info,
+ const struct VkExternalMemoryImageCreateInfo *create_info,
+ const VkAllocationCallbacks *alloc,
+ VkImage *out_image_h)
+{
+#if ANDROID_API_LEVEL >= 26
+ ANV_FROM_HANDLE(anv_device, device, device_h);
+
+ const struct VkExternalFormatANDROID *ext_info =
+ vk_find_struct_const(base_info->pNext, EXTERNAL_FORMAT_ANDROID);
+
+ if (ext_info && ext_info->externalFormat != 0) {
+ assert(base_info->format == VK_FORMAT_UNDEFINED);
+ assert(base_info->imageType == VK_IMAGE_TYPE_2D);
+ assert(base_info->usage == VK_IMAGE_USAGE_SAMPLED_BIT);
+ assert(base_info->tiling == VK_IMAGE_TILING_OPTIMAL);
+ }
+
+ struct anv_image_create_info anv_info = {
+ .vk_info = base_info,
+ .isl_extra_usage_flags = ISL_SURF_USAGE_DISABLE_AUX_BIT,
+ .external_format = true,
+ };
+
+ VkImage image_h;
+ VkResult result = anv_image_create(device_h, &anv_info, alloc, &image_h);
+ if (result != VK_SUCCESS)
+ return result;
+
+ *out_image_h = image_h;
+
+ return VK_SUCCESS;
+#else
+ return VK_ERROR_EXTENSION_NOT_PRESENT;
+#endif
+}
+
+