Flutter Engine
The Flutter Engine
ahb_texture_source_vk.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
11
12namespace impeller {
13
14using AHBProperties = vk::StructureChain<
15 // For VK_ANDROID_external_memory_android_hardware_buffer
16 vk::AndroidHardwareBufferPropertiesANDROID,
17 // For VK_ANDROID_external_memory_android_hardware_buffer
18 vk::AndroidHardwareBufferFormatPropertiesANDROID>;
19
21 const vk::Device& device,
22 const AHBProperties& ahb_props,
23 const AHardwareBuffer_Desc& ahb_desc) {
24 const auto& ahb_format =
25 ahb_props.get<vk::AndroidHardwareBufferFormatPropertiesANDROID>();
26
27 vk::StructureChain<vk::ImageCreateInfo,
28 // For VK_KHR_external_memory
29 vk::ExternalMemoryImageCreateInfo,
30 // For VK_ANDROID_external_memory_android_hardware_buffer
31 vk::ExternalFormatANDROID>
32 image_chain;
33
34 auto& image_info = image_chain.get<vk::ImageCreateInfo>();
35
36 vk::ImageUsageFlags image_usage_flags;
37 if (ahb_desc.usage & AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE) {
38 image_usage_flags |= vk::ImageUsageFlagBits::eSampled;
39 }
40 if (ahb_desc.usage & AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER) {
41 image_usage_flags |= vk::ImageUsageFlagBits::eColorAttachment;
42 }
43 if (ahb_desc.usage & AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY) {
44 image_usage_flags |= vk::ImageUsageFlagBits::eColorAttachment;
45 image_usage_flags |= vk::ImageUsageFlagBits::eInputAttachment;
46 image_usage_flags |= vk::ImageUsageFlagBits::eTransferDst;
47 }
48
49 vk::ImageCreateFlags image_create_flags;
50 if (ahb_desc.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) {
51 image_create_flags |= vk::ImageCreateFlagBits::eProtected;
52 }
53 if (ahb_desc.usage & AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP) {
54 image_create_flags |= vk::ImageCreateFlagBits::eCubeCompatible;
55 }
56
57 image_info.imageType = vk::ImageType::e2D;
58 image_info.format = ahb_format.format;
59 image_info.extent.width = ahb_desc.width;
60 image_info.extent.height = ahb_desc.height;
61 image_info.extent.depth = 1;
62 image_info.mipLevels =
63 (ahb_desc.usage & AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE)
64 ? ISize{ahb_desc.width, ahb_desc.height}.MipCount()
65 : 1u;
66 image_info.arrayLayers = ahb_desc.layers;
67 image_info.samples = vk::SampleCountFlagBits::e1;
68 image_info.tiling = vk::ImageTiling::eOptimal;
69 image_info.usage = image_usage_flags;
70 image_info.flags = image_create_flags;
71 image_info.sharingMode = vk::SharingMode::eExclusive;
72 image_info.initialLayout = vk::ImageLayout::eUndefined;
73
74 image_chain.get<vk::ExternalMemoryImageCreateInfo>().handleTypes =
75 vk::ExternalMemoryHandleTypeFlagBits::eAndroidHardwareBufferANDROID;
76
77 // If the format isn't natively supported by Vulkan (i.e, be a part of the
78 // base vkFormat enum), an untyped "external format" must be specified when
79 // creating the image and the image views. Usually includes YUV formats.
80 if (ahb_format.format == vk::Format::eUndefined) {
81 image_chain.get<vk::ExternalFormatANDROID>().externalFormat =
82 ahb_format.externalFormat;
83 } else {
84 image_chain.unlink<vk::ExternalFormatANDROID>();
85 }
86
87 auto image = device.createImageUnique(image_chain.get());
88 if (image.result != vk::Result::eSuccess) {
89 VALIDATION_LOG << "Could not create image for external buffer: "
90 << vk::to_string(image.result);
91 return {};
92 }
93
94 return std::move(image.value);
95}
96
98 const vk::Device& device,
99 const vk::PhysicalDevice& physical_device,
100 const vk::Image& image,
101 struct AHardwareBuffer* hardware_buffer,
102 const AHBProperties& ahb_props) {
103 vk::PhysicalDeviceMemoryProperties memory_properties;
104 physical_device.getMemoryProperties(&memory_properties);
105 int memory_type_index = AllocatorVK::FindMemoryTypeIndex(
106 ahb_props.get().memoryTypeBits, memory_properties);
107 if (memory_type_index < 0) {
108 VALIDATION_LOG << "Could not find memory type of external image.";
109 return {};
110 }
111
112 vk::StructureChain<vk::MemoryAllocateInfo,
113 // Core in 1.1
114 vk::MemoryDedicatedAllocateInfo,
115 // For VK_ANDROID_external_memory_android_hardware_buffer
116 vk::ImportAndroidHardwareBufferInfoANDROID>
117 memory_chain;
118
119 auto& mem_alloc_info = memory_chain.get<vk::MemoryAllocateInfo>();
120 mem_alloc_info.allocationSize = ahb_props.get().allocationSize;
121 mem_alloc_info.memoryTypeIndex = memory_type_index;
122
123 auto& dedicated_alloc_info =
124 memory_chain.get<vk::MemoryDedicatedAllocateInfo>();
125 dedicated_alloc_info.image = image;
126
127 auto& ahb_import_info =
128 memory_chain.get<vk::ImportAndroidHardwareBufferInfoANDROID>();
129 ahb_import_info.buffer = hardware_buffer;
130
131 auto device_memory = device.allocateMemoryUnique(memory_chain.get());
132 if (device_memory.result != vk::Result::eSuccess) {
133 VALIDATION_LOG << "Could not allocate device memory for external image : "
134 << vk::to_string(device_memory.result);
135 return {};
136 }
137
138 return std::move(device_memory.value);
139}
140
141static std::shared_ptr<YUVConversionVK> CreateYUVConversion(
142 const ContextVK& context,
143 const AHBProperties& ahb_props) {
144 YUVConversionDescriptorVK conversion_chain;
145
146 const auto& ahb_format =
147 ahb_props.get<vk::AndroidHardwareBufferFormatPropertiesANDROID>();
148
149 auto& conversion_info = conversion_chain.get();
150
151 conversion_info.format = ahb_format.format;
152 conversion_info.ycbcrModel = ahb_format.suggestedYcbcrModel;
153 conversion_info.ycbcrRange = ahb_format.suggestedYcbcrRange;
154 conversion_info.components = ahb_format.samplerYcbcrConversionComponents;
155 conversion_info.xChromaOffset = ahb_format.suggestedXChromaOffset;
156 conversion_info.yChromaOffset = ahb_format.suggestedYChromaOffset;
157 // If the potential format features of the sampler Y′CBCR conversion do not
158 // support VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT,
159 // chromaFilter must not be VK_FILTER_LINEAR.
160 //
161 // Since we are not checking, let's just default to a safe value.
162 conversion_info.chromaFilter = vk::Filter::eNearest;
163 conversion_info.forceExplicitReconstruction = false;
164
165 if (conversion_info.format == vk::Format::eUndefined) {
166 auto& external_format = conversion_chain.get<vk::ExternalFormatANDROID>();
167 external_format.externalFormat = ahb_format.externalFormat;
168 } else {
169 conversion_chain.unlink<vk::ExternalFormatANDROID>();
170 }
171
172 return context.GetYUVConversionLibrary()->GetConversion(conversion_chain);
173}
174
175static vk::UniqueImageView CreateVKImageView(
176 const vk::Device& device,
177 const vk::Image& image,
178 const vk::SamplerYcbcrConversion& yuv_conversion,
179 const AHBProperties& ahb_props,
180 const AHardwareBuffer_Desc& ahb_desc) {
181 const auto& ahb_format =
182 ahb_props.get<vk::AndroidHardwareBufferFormatPropertiesANDROID>();
183
184 vk::StructureChain<vk::ImageViewCreateInfo,
185 // Core in 1.1
186 vk::SamplerYcbcrConversionInfo>
187 view_chain;
188
189 auto& view_info = view_chain.get();
190
191 view_info.image = image;
192 view_info.viewType = vk::ImageViewType::e2D;
193 view_info.format = ahb_format.format;
194 view_info.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
195 view_info.subresourceRange.baseMipLevel = 0u;
196 view_info.subresourceRange.baseArrayLayer = 0u;
197 view_info.subresourceRange.levelCount =
198 (ahb_desc.usage & AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE)
199 ? ISize{ahb_desc.width, ahb_desc.height}.MipCount()
200 : 1u;
201 view_info.subresourceRange.layerCount = ahb_desc.layers;
202
203 // We need a custom YUV conversion only if we don't recognize the format.
204 if (view_info.format == vk::Format::eUndefined) {
205 view_chain.get<vk::SamplerYcbcrConversionInfo>().conversion =
206 yuv_conversion;
207 } else {
208 view_chain.unlink<vk::SamplerYcbcrConversionInfo>();
209 }
210
211 auto image_view = device.createImageViewUnique(view_info);
212 if (image_view.result != vk::Result::eSuccess) {
213 VALIDATION_LOG << "Could not create external image view: "
214 << vk::to_string(image_view.result);
215 return {};
216 }
217
218 return std::move(image_view.value);
219}
220
221static PixelFormat ToPixelFormat(AHardwareBuffer_Format format) {
222 switch (format) {
223 case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
225 case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
227 case AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT:
229 case AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT:
231 case AHARDWAREBUFFER_FORMAT_S8_UINT:
233 case AHARDWAREBUFFER_FORMAT_R8_UNORM:
235 case AHARDWAREBUFFER_FORMAT_R10G10B10A10_UNORM:
236 case AHARDWAREBUFFER_FORMAT_R16G16_UINT:
237 case AHARDWAREBUFFER_FORMAT_D32_FLOAT:
238 case AHARDWAREBUFFER_FORMAT_R16_UINT:
239 case AHARDWAREBUFFER_FORMAT_D24_UNORM:
240 case AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420:
241 case AHARDWAREBUFFER_FORMAT_YCbCr_P010:
242 case AHARDWAREBUFFER_FORMAT_BLOB:
243 case AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM:
244 case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
245 case AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM:
246 case AHARDWAREBUFFER_FORMAT_D16_UNORM:
247 case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
248 // Not understood by the rest of Impeller. Use a placeholder but create
249 // the native image and image views using the right external format.
250 break;
251 }
253}
254
255static TextureType ToTextureType(const AHardwareBuffer_Desc& ahb_desc) {
256 if (ahb_desc.layers == 1u) {
258 }
259 if (ahb_desc.layers % 6u == 0 &&
260 (ahb_desc.usage & AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP)) {
262 }
263 // Our texture types seem to understand external OES textures. Should these be
264 // wired up instead?
266}
267
269 const AHardwareBuffer_Desc& ahb_desc) {
270 const auto ahb_size = ISize{ahb_desc.width, ahb_desc.height};
272 // We are not going to touch hardware buffers on the CPU or use them as
273 // transient attachments. Just treat them as device private.
274 desc.storage_mode = StorageMode::kDevicePrivate;
275 desc.format =
276 ToPixelFormat(static_cast<AHardwareBuffer_Format>(ahb_desc.format));
277 desc.size = ahb_size;
278 desc.type = ToTextureType(ahb_desc);
279 desc.sample_count = SampleCount::kCount1;
280 desc.compression_type = CompressionType::kLossless;
281 desc.mip_count = (ahb_desc.usage & AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE)
282 ? ahb_size.MipCount()
283 : 1u;
284 if (ahb_desc.usage & AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY) {
286 }
287 return desc;
288}
289
291 const std::shared_ptr<Context>& p_context,
292 struct AHardwareBuffer* ahb,
293 const AHardwareBuffer_Desc& ahb_desc)
295 if (!p_context) {
296 return;
297 }
298
299 const auto& context = ContextVK::Cast(*p_context);
300
301 const auto& device = context.GetDevice();
302 const auto& physical_device = context.GetPhysicalDevice();
303
304 AHBProperties ahb_props;
305
306 if (device.getAndroidHardwareBufferPropertiesANDROID(ahb, &ahb_props.get()) !=
307 vk::Result::eSuccess) {
308 VALIDATION_LOG << "Could not determine properties of the Android hardware "
309 "buffer.";
310 return;
311 }
312
313 const auto& ahb_format =
314 ahb_props.get<vk::AndroidHardwareBufferFormatPropertiesANDROID>();
315
316 // Create an image to refer to our external image.
317 auto image =
319 if (!image) {
320 return;
321 }
322
323 // Create a device memory allocation to refer to our external image.
325 device, physical_device, image.get(), ahb, ahb_props);
326 if (!device_memory) {
327 return;
328 }
329
330 // Bind the image to the image memory.
331 if (auto result = device.bindImageMemory(image.get(), device_memory.get(), 0);
332 result != vk::Result::eSuccess) {
333 VALIDATION_LOG << "Could not bind external device memory to image : "
335 return;
336 }
337
338 // Figure out how to perform YUV conversions.
339 auto yuv_conversion = CreateYUVConversion(context, ahb_props);
340 if (!yuv_conversion || !yuv_conversion->IsValid()) {
341 return;
342 }
343
344 // Create image view for the newly created image.
345 auto image_view = CreateVKImageView(device, //
346 image.get(), //
347 yuv_conversion->GetConversion(), //
348 ahb_props, //
349 ahb_desc //
350 );
351 if (!image_view) {
352 return;
353 }
354
355 needs_yuv_conversion_ = ahb_format.format == vk::Format::eUndefined;
356 device_memory_ = std::move(device_memory);
357 image_ = std::move(image);
358 yuv_conversion_ = std::move(yuv_conversion);
359 image_view_ = std::move(image_view);
360
361#ifdef IMPELLER_DEBUG
362 context.SetDebugName(device_memory_.get(), "AHB Device Memory");
363 context.SetDebugName(image_.get(), "AHB Image");
364 context.SetDebugName(yuv_conversion_->GetConversion(), "AHB YUV Conversion");
365 context.SetDebugName(image_view_.get(), "AHB ImageView");
366#endif // IMPELLER_DEBUG
367
368 is_valid_ = true;
369}
370
372 const std::shared_ptr<Context>& context,
373 std::unique_ptr<android::HardwareBuffer> backing_store,
374 bool is_swapchain_image)
375 : AHBTextureSourceVK(context,
376 backing_store->GetHandle(),
377 backing_store->GetAndroidDescriptor()) {
378 backing_store_ = std::move(backing_store);
379 is_swapchain_image_ = is_swapchain_image;
380}
381
382// |TextureSourceVK|
384
386 return is_valid_;
387}
388
389// |TextureSourceVK|
391 return image_.get();
392}
393
394// |TextureSourceVK|
395vk::ImageView AHBTextureSourceVK::GetImageView() const {
396 return image_view_.get();
397}
398
399// |TextureSourceVK|
401 return image_view_.get();
402}
403
404// |TextureSourceVK|
406 return is_swapchain_image_;
407}
408
409// |TextureSourceVK|
410std::shared_ptr<YUVConversionVK> AHBTextureSourceVK::GetYUVConversion() const {
411 return needs_yuv_conversion_ ? yuv_conversion_ : nullptr;
412}
413
415 return backing_store_.get();
416}
417
418} // namespace impeller
struct AHardwareBuffer AHardwareBuffer
float e1
GLenum external_format
A texture source that wraps an instance of AHardwareBuffer.
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.
const android::HardwareBuffer * GetBackingStore() const
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< Context > &context, struct AHardwareBuffer *hardware_buffer, const AHardwareBuffer_Desc &hardware_buffer_desc)
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.
static ContextVK & Cast(Context &base)
Definition: backend_cast.h:13
const std::shared_ptr< YUVConversionLibraryVK > & GetYUVConversionLibrary() const
Definition: context_vk.cc:621
Abstract base class that represents a vkImage and an vkImageView.
A wrapper for AHardwareBuffer https://developer.android.com/ndk/reference/group/a-hardware-buffer.
T * get() const
Definition: SkRefCnt.h:303
VkPhysicalDevice physical_device
Definition: main.cc:51
VkDevice device
Definition: main.cc:53
GAsyncResult * result
uint32_t uint32_t * format
sk_sp< const SkImage > image
Definition: SkRecords.h:269
CanvasImage Image
Definition: dart_ui.cc:55
vk::StructureChain< vk::AndroidHardwareBufferPropertiesANDROID, vk::AndroidHardwareBufferFormatPropertiesANDROID > AHBProperties
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
constexpr GLenum ToTextureType(TextureType type)
Definition: formats_gles.h:169
static vk::UniqueImage CreateVKImageWrapperForAndroidHarwareBuffer(const vk::Device &device, const AHBProperties &ahb_props, const AHardwareBuffer_Desc &ahb_desc)
static std::shared_ptr< YUVConversionVK > CreateYUVConversion(const ContextVK &context, const AHBProperties &ahb_props)
TextureType
Definition: formats.h:262
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)
static TextureDescriptor ToTextureDescriptor(const AHardwareBuffer_Desc &ahb_desc)
static PixelFormat ToPixelFormat(AHardwareBuffer_Format format)
vk::StructureChain< vk::SamplerYcbcrConversionCreateInfo > YUVConversionDescriptorVK
static vk::UniqueDeviceMemory ImportVKDeviceMemoryFromAndroidHarwareBuffer(const vk::Device &device, const vk::PhysicalDevice &physical_device, const vk::Image &image, struct AHardwareBuffer *hardware_buffer, const AHBProperties &ahb_props)
static SkString to_string(int n)
Definition: nanobench.cpp:119
Type width
Definition: size.h:22
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
#define VALIDATION_LOG
Definition: validation.h:73