23 const AHardwareBuffer_Desc& ahb_desc) {
24 const auto& ahb_format =
25 ahb_props.get<vk::AndroidHardwareBufferFormatPropertiesANDROID>();
27 vk::StructureChain<vk::ImageCreateInfo,
29 vk::ExternalMemoryImageCreateInfo,
31 vk::ExternalFormatANDROID>
34 auto& image_info = image_chain.get<vk::ImageCreateInfo>();
36 vk::ImageUsageFlags image_usage_flags;
37 if (ahb_desc.usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE) {
38 image_usage_flags |= vk::ImageUsageFlagBits::eSampled;
40 if (ahb_desc.usage & AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER) {
41 image_usage_flags |= vk::ImageUsageFlagBits::eColorAttachment;
44 vk::ImageCreateFlags image_create_flags;
45 if (ahb_desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) {
46 image_create_flags |= vk::ImageCreateFlagBits::eProtected;
48 if (ahb_desc.usage & AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP) {
49 image_create_flags |= vk::ImageCreateFlagBits::eCubeCompatible;
52 image_info.imageType = vk::ImageType::e2D;
53 image_info.format = ahb_format.format;
54 image_info.extent.width = ahb_desc.width;
55 image_info.extent.height = ahb_desc.height;
56 image_info.extent.depth = 1;
57 image_info.mipLevels =
58 (ahb_desc.usage & AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE)
59 ?
ISize{ahb_desc.
width, ahb_desc.height}.MipCount()
61 image_info.arrayLayers = ahb_desc.layers;
62 image_info.samples = vk::SampleCountFlagBits::e1;
63 image_info.tiling = vk::ImageTiling::eOptimal;
64 image_info.usage = image_usage_flags;
65 image_info.flags = image_create_flags;
66 image_info.sharingMode = vk::SharingMode::eExclusive;
67 image_info.initialLayout = vk::ImageLayout::eUndefined;
69 image_chain.get<vk::ExternalMemoryImageCreateInfo>().handleTypes =
70 vk::ExternalMemoryHandleTypeFlagBits::eAndroidHardwareBufferANDROID;
75 if (ahb_format.format == vk::Format::eUndefined) {
76 image_chain.get<vk::ExternalFormatANDROID>().externalFormat =
77 ahb_format.externalFormat;
79 image_chain.unlink<vk::ExternalFormatANDROID>();
82 auto image =
device.createImageUnique(image_chain.get());
83 if (
image.result != vk::Result::eSuccess) {
85 << vk::to_string(
image.result);
89 return std::move(
image.value);
95 const vk::Image&
image,
98 vk::PhysicalDeviceMemoryProperties memory_properties;
101 ahb_props.get().memoryTypeBits, memory_properties);
102 if (memory_type_index < 0) {
103 VALIDATION_LOG <<
"Could not find memory type of external image.";
107 vk::StructureChain<vk::MemoryAllocateInfo,
109 vk::MemoryDedicatedAllocateInfo,
111 vk::ImportAndroidHardwareBufferInfoANDROID>
114 auto& mem_alloc_info = memory_chain.get<vk::MemoryAllocateInfo>();
115 mem_alloc_info.allocationSize = ahb_props.get().allocationSize;
116 mem_alloc_info.memoryTypeIndex = memory_type_index;
118 auto& dedicated_alloc_info =
119 memory_chain.get<vk::MemoryDedicatedAllocateInfo>();
120 dedicated_alloc_info.image =
image;
122 auto& ahb_import_info =
123 memory_chain.
get<vk::ImportAndroidHardwareBufferInfoANDROID>();
124 ahb_import_info.buffer = hardware_buffer;
126 auto device_memory =
device.allocateMemoryUnique(memory_chain.get());
127 if (device_memory.result != vk::Result::eSuccess) {
128 VALIDATION_LOG <<
"Could not allocate device memory for external image : "
129 << vk::to_string(device_memory.result);
133 return std::move(device_memory.value);
141 const auto& ahb_format =
142 ahb_props.get<vk::AndroidHardwareBufferFormatPropertiesANDROID>();
144 auto& conversion_info = conversion_chain.get();
146 conversion_info.format = ahb_format.format;
147 conversion_info.ycbcrModel = ahb_format.suggestedYcbcrModel;
148 conversion_info.ycbcrRange = ahb_format.suggestedYcbcrRange;
149 conversion_info.components = ahb_format.samplerYcbcrConversionComponents;
150 conversion_info.xChromaOffset = ahb_format.suggestedXChromaOffset;
151 conversion_info.yChromaOffset = ahb_format.suggestedYChromaOffset;
157 conversion_info.chromaFilter = vk::Filter::eNearest;
158 conversion_info.forceExplicitReconstruction =
false;
160 if (conversion_info.format == vk::Format::eUndefined) {
161 auto& external_format = conversion_chain.get<vk::ExternalFormatANDROID>();
162 external_format.externalFormat = ahb_format.externalFormat;
164 conversion_chain.unlink<vk::ExternalFormatANDROID>();
172 const vk::Image&
image,
173 const vk::SamplerYcbcrConversion& yuv_conversion,
175 const AHardwareBuffer_Desc& ahb_desc) {
176 const auto& ahb_format =
177 ahb_props.get<vk::AndroidHardwareBufferFormatPropertiesANDROID>();
179 vk::StructureChain<vk::ImageViewCreateInfo,
181 vk::SamplerYcbcrConversionInfo>
184 auto& view_info = view_chain.get();
186 view_info.image =
image;
187 view_info.viewType = vk::ImageViewType::e2D;
188 view_info.format = ahb_format.format;
189 view_info.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
190 view_info.subresourceRange.baseMipLevel = 0u;
191 view_info.subresourceRange.baseArrayLayer = 0u;
192 view_info.subresourceRange.levelCount =
193 (ahb_desc.usage & AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE)
194 ?
ISize{ahb_desc.
width, ahb_desc.height}.MipCount()
196 view_info.subresourceRange.layerCount = ahb_desc.layers;
199 if (view_info.format == vk::Format::eUndefined) {
200 view_chain.get<vk::SamplerYcbcrConversionInfo>().conversion =
203 view_chain.unlink<vk::SamplerYcbcrConversionInfo>();
206 auto image_view =
device.createImageViewUnique(view_info);
207 if (image_view.result != vk::Result::eSuccess) {
209 << vk::to_string(image_view.result);
213 return std::move(image_view.value);
218 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
220 case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
222 case AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT:
224 case AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT:
226 case AHARDWAREBUFFER_FORMAT_S8_UINT:
228 case AHARDWAREBUFFER_FORMAT_R8_UNORM:
230 case AHARDWAREBUFFER_FORMAT_R10G10B10A10_UNORM:
231 case AHARDWAREBUFFER_FORMAT_R16G16_UINT:
232 case AHARDWAREBUFFER_FORMAT_D32_FLOAT:
233 case AHARDWAREBUFFER_FORMAT_R16_UINT:
234 case AHARDWAREBUFFER_FORMAT_D24_UNORM:
235 case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420:
236 case AHARDWAREBUFFER_FORMAT_YCbCr_P010:
237 case AHARDWAREBUFFER_FORMAT_BLOB:
238 case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
239 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
240 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
241 case AHARDWAREBUFFER_FORMAT_D16_UNORM:
242 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
283 const std::shared_ptr<ContextVK>& context,
285 const AHardwareBuffer_Desc& ahb_desc)
292 const auto&
device = context->GetDevice();
297 if (
device.getAndroidHardwareBufferPropertiesANDROID(ahb, &ahb_props.get()) !=
298 vk::Result::eSuccess) {
299 VALIDATION_LOG <<
"Could not determine properties of the Android hardware "
304 const auto& ahb_format =
305 ahb_props.get<vk::AndroidHardwareBufferFormatPropertiesANDROID>();
317 if (!device_memory) {
323 result != vk::Result::eSuccess) {
324 VALIDATION_LOG <<
"Could not bind external device memory to image : "
331 if (!yuv_conversion || !yuv_conversion->IsValid()) {
338 yuv_conversion->GetConversion(),
346 needs_yuv_conversion_ = ahb_format.format == vk::Format::eUndefined;
347 device_memory_ = std::move(device_memory);
348 image_ = std::move(
image);
349 yuv_conversion_ = std::move(yuv_conversion);
350 image_view_ = std::move(image_view);
353 context->SetDebugName(device_memory_.get(),
"AHB Device Memory");
354 context->SetDebugName(image_.get(),
"AHB Image");
355 context->SetDebugName(yuv_conversion_->GetConversion(),
"AHB YUV Conversion");
356 context->SetDebugName(image_view_.get(),
"AHB ImageView");
bool IsSwapchainImage() const override
Determines if swapchain image. That is, an image used as the root render target.
vk::Image GetImage() const override
Get the image handle for this texture source.
std::shared_ptr< YUVConversionVK > GetYUVConversion() const override
When sampling from textures whose formats are not known to Vulkan, a custom conversion is necessary t...
AHBTextureSourceVK(const std::shared_ptr< ContextVK > &context, struct AHardwareBuffer *hardware_buffer, const AHardwareBuffer_Desc &hardware_buffer_desc)
~AHBTextureSourceVK() override
vk::ImageView GetImageView() const override
Retrieve the image view used for sampling/blitting/compute with this texture source.
vk::ImageView GetRenderTargetView() const override
Retrieve the image view used for render target attachments with this texture source.
static int32_t FindMemoryTypeIndex(uint32_t memory_type_bits_requirement, vk::PhysicalDeviceMemoryProperties &memory_properties)
Select a matching memory type for the given [memory_type_bits_requirement], or -1 if none is found.
const std::shared_ptr< YUVConversionLibraryVK > & GetYUVConversionLibrary() const
Abstract base class that represents a vkImage and an vkImageView.
VkPhysicalDevice physical_device
uint32_t uint32_t * format
static vk::UniqueImageView CreateVKImageView(const vk::Device &device, const vk::Image &image, const vk::SamplerYcbcrConversion &yuv_conversion, const AHBProperties &ahb_props, const AHardwareBuffer_Desc &ahb_desc)
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...