|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +// @lint-ignore-every CLANGTIDY facebook-hte-BadMemberName |
| 12 | + |
| 13 | +#include <executorch/backends/vulkan/runtime/api/api.h> |
| 14 | + |
| 15 | +#include <executorch/backends/vulkan/runtime/vk_api/VkUtils.h> |
| 16 | + |
| 17 | +namespace vkcompute { |
| 18 | +namespace utils { |
| 19 | +namespace { |
| 20 | + |
| 21 | +uint32_t find_mem_type_idx( |
| 22 | + api::Context* context, |
| 23 | + uint32_t mem_type_bits, |
| 24 | + const VkFlags flags) { |
| 25 | + VkPhysicalDeviceMemoryProperties mem_props; |
| 26 | + vkGetPhysicalDeviceMemoryProperties( |
| 27 | + context->adapter_ptr()->physical_handle(), &mem_props); |
| 28 | + |
| 29 | + for (uint32_t i = 0; i < mem_props.memoryTypeCount; ++i) { |
| 30 | + if ((mem_type_bits & 1u) && |
| 31 | + (mem_props.memoryTypes[i].propertyFlags & flags) == flags) { |
| 32 | + return i; |
| 33 | + } |
| 34 | + mem_type_bits >>= 1u; |
| 35 | + } |
| 36 | + VK_THROW("Failed to find memory type index for AHB"); |
| 37 | +} |
| 38 | + |
| 39 | +VkImage create_image( |
| 40 | + api::Context* context, |
| 41 | + AHardwareBuffer* ahb, |
| 42 | + const VkImageUsageFlags usage) { |
| 43 | + AHardwareBuffer_acquire(ahb); |
| 44 | + AHardwareBuffer_Desc desc; |
| 45 | + AHardwareBuffer_describe(ahb, &desc); |
| 46 | + |
| 47 | + VkAndroidHardwareBufferFormatPropertiesANDROID format_info = { |
| 48 | + .sType = |
| 49 | + VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID, |
| 50 | + .pNext = nullptr, |
| 51 | + }; |
| 52 | + |
| 53 | + VkAndroidHardwareBufferPropertiesANDROID ahb_props = { |
| 54 | + .sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID, |
| 55 | + .pNext = &format_info, |
| 56 | + }; |
| 57 | + |
| 58 | + VK_CHECK(vkGetAndroidHardwareBufferPropertiesANDROID( |
| 59 | + context->device(), ahb, &ahb_props)); |
| 60 | + |
| 61 | + const VkExternalMemoryImageCreateInfo ext_create_info = { |
| 62 | + .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO, |
| 63 | + .pNext = nullptr, |
| 64 | + .handleTypes = |
| 65 | + VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID, |
| 66 | + }; |
| 67 | + const auto format = VK_FORMAT_R8G8B8A8_UNORM; |
| 68 | + const auto tiling = VK_IMAGE_TILING_LINEAR; |
| 69 | + const VkImageCreateInfo createInfo = { |
| 70 | + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, |
| 71 | + .pNext = &ext_create_info, |
| 72 | + .flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, |
| 73 | + .imageType = VK_IMAGE_TYPE_2D, |
| 74 | + .format = format, |
| 75 | + .extent = {desc.width, desc.height, 1u}, |
| 76 | + .mipLevels = 1u, |
| 77 | + .arrayLayers = 1u, |
| 78 | + .samples = VK_SAMPLE_COUNT_1_BIT, |
| 79 | + .tiling = tiling, |
| 80 | + .usage = usage, |
| 81 | + .sharingMode = VK_SHARING_MODE_EXCLUSIVE, |
| 82 | + .queueFamilyIndexCount = 0, |
| 83 | + .pQueueFamilyIndices = nullptr, |
| 84 | + .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, |
| 85 | + }; |
| 86 | + |
| 87 | + VkImage image; |
| 88 | + VK_CHECK(vkCreateImage(context->device(), &createInfo, nullptr, &image)); |
| 89 | + |
| 90 | + const auto flags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | |
| 91 | + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; |
| 92 | + VkPhysicalDeviceMemoryProperties mem_props; |
| 93 | + vkGetPhysicalDeviceMemoryProperties( |
| 94 | + context->adapter_ptr()->physical_handle(), &mem_props); |
| 95 | + |
| 96 | + const auto mem_type_idx = |
| 97 | + find_mem_type_idx(context, ahb_props.memoryTypeBits, flags); |
| 98 | + |
| 99 | + const VkImportAndroidHardwareBufferInfoANDROID androidHardwareBufferInfo = { |
| 100 | + .sType = VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID, |
| 101 | + .pNext = nullptr, |
| 102 | + .buffer = ahb, |
| 103 | + }; |
| 104 | + const VkMemoryDedicatedAllocateInfo mem_alloc_info = { |
| 105 | + .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO, |
| 106 | + .pNext = &androidHardwareBufferInfo, |
| 107 | + .image = image, |
| 108 | + .buffer = VK_NULL_HANDLE, |
| 109 | + }; |
| 110 | + const VkMemoryAllocateInfo alloc_info = { |
| 111 | + .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, |
| 112 | + .pNext = &mem_alloc_info, |
| 113 | + .allocationSize = ahb_props.allocationSize, |
| 114 | + .memoryTypeIndex = mem_type_idx, |
| 115 | + }; |
| 116 | + |
| 117 | + VkDeviceMemory memory; |
| 118 | + VK_CHECK(vkAllocateMemory(context->device(), &alloc_info, nullptr, &memory)); |
| 119 | + VK_CHECK(vkBindImageMemory(context->device(), image, memory, 0)); |
| 120 | + |
| 121 | + return image; |
| 122 | +} |
| 123 | + |
| 124 | +VkImageView create_image_view(api::Context* context, const VkImage& image) { |
| 125 | + const VkImageViewCreateInfo image_view_ci = { |
| 126 | + .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, |
| 127 | + .pNext = nullptr, |
| 128 | + .flags = 0, |
| 129 | + .image = image, |
| 130 | + .viewType = VK_IMAGE_VIEW_TYPE_2D, |
| 131 | + .format = VK_FORMAT_R8G8B8A8_UNORM, |
| 132 | + .components = |
| 133 | + { |
| 134 | + VK_COMPONENT_SWIZZLE_IDENTITY, |
| 135 | + VK_COMPONENT_SWIZZLE_IDENTITY, |
| 136 | + VK_COMPONENT_SWIZZLE_IDENTITY, |
| 137 | + VK_COMPONENT_SWIZZLE_IDENTITY, |
| 138 | + }, |
| 139 | + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}, |
| 140 | + }; |
| 141 | + |
| 142 | + VkImageView image_view; |
| 143 | + VK_CHECK(vkCreateImageView( |
| 144 | + context->device(), &image_view_ci, nullptr, &image_view)); |
| 145 | + return image_view; |
| 146 | +} |
| 147 | + |
| 148 | +VkSampler create_sampler(api::Context* context) { |
| 149 | + vkapi::ImageSampler::Properties sampler_props{ |
| 150 | + VK_FILTER_LINEAR, |
| 151 | + VK_SAMPLER_MIPMAP_MODE_LINEAR, |
| 152 | + VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, |
| 153 | + VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE, |
| 154 | + }; |
| 155 | + auto sampler = |
| 156 | + context->adapter_ptr()->sampler_cache().retrieve(sampler_props); |
| 157 | + return sampler; |
| 158 | +} |
| 159 | + |
| 160 | +} // namespace |
| 161 | + |
| 162 | +vkapi::VulkanImage create_image_from_ahb( |
| 163 | + api::Context* context, |
| 164 | + AHardwareBuffer* ahb, |
| 165 | + const uint32_t width, |
| 166 | + const uint32_t height, |
| 167 | + const VkImageUsageFlags usage) { |
| 168 | + const auto image_type = VK_IMAGE_TYPE_2D; |
| 169 | + const auto image_format = VK_FORMAT_R8G8B8A8_UNORM; |
| 170 | + const utils::uvec3 extents({width, height, 1}); |
| 171 | + |
| 172 | + const vkapi::VulkanImage::ImageProperties image_props = { |
| 173 | + image_type, image_format, vkapi::create_extent3d(extents), usage}; |
| 174 | + |
| 175 | + auto image = create_image(context, ahb, usage); |
| 176 | + auto image_view = create_image_view(context, image); |
| 177 | + auto sampler = (usage & VK_IMAGE_USAGE_SAMPLED_BIT) ? create_sampler(context) |
| 178 | + : VK_NULL_HANDLE; |
| 179 | + |
| 180 | + return vkapi::VulkanImage( |
| 181 | + context->device(), image_props, image, image_view, sampler); |
| 182 | +} |
| 183 | + |
| 184 | +} // namespace utils |
| 185 | +} // namespace vkcompute |
0 commit comments