| Flutter Impeller |

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