| Flutter Impeller |

yuv_conversion_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/yuv_conversion_vk.h"
6
7 #include "flutter/fml/hash_combine.h"
8 #include "impeller/base/validation.h"
9 #include "impeller/core/sampler_descriptor.h"
10 #include "impeller/renderer/backend/vulkan/device_holder_vk.h"
11 #include "impeller/renderer/backend/vulkan/sampler_vk.h"
12
13 namespace impeller {
14
15 YUVConversionVK::YUVConversionVK(const vk::Device& device,
16 const YUVConversionDescriptorVK& chain)
17 : chain_(chain) {
18 auto conversion = device.createSamplerYcbcrConversionUnique(chain_.get());
19 if (conversion.result != vk::Result::eSuccess) {
20 VALIDATION_LOG << "Could not create YUV conversion: "
21 << vk::to_string(conversion.result);
22 return;
23 }
24 conversion_ = std::move(conversion.value);
25 }
26
27 YUVConversionVK::~YUVConversionVK() = default;
28
29 bool YUVConversionVK::IsValid() const {
30 return conversion_ && !!conversion_.get();
31 }
32
33 vk::SamplerYcbcrConversion YUVConversionVK::GetConversion() const {
34 return conversion_ ? conversion_.get()
35 : static_cast<vk::SamplerYcbcrConversion>(VK_NULL_HANDLE);
36 }
37
38 const YUVConversionDescriptorVK& YUVConversionVK::GetDescriptor() const {
39 return chain_;
40 }
41
42 std::size_t YUVConversionDescriptorVKHash::operator()(
43 const YUVConversionDescriptorVK& desc) const {
44 // Hashers in Vulkan HPP hash the pNext member which isn't what we want for
45 // these to be stable.
46 const auto& conv = desc.get();
47
48 std::size_t hash = fml::HashCombine(conv.format, //
49 conv.ycbcrModel, //
50 conv.ycbcrRange, //
51 conv.components.r, //
52 conv.components.g, //
53 conv.components.b, //
54 conv.components.a, //
55 conv.xChromaOffset, //
56 conv.yChromaOffset, //
57 conv.chromaFilter, //
58 conv.forceExplicitReconstruction //
59 );
60 #if FML_OS_ANDROID
61 const auto external_format = desc.get<vk::ExternalFormatANDROID>();
62 fml::HashCombineSeed(hash, external_format.externalFormat);
63 #endif // FML_OS_ANDROID
64
65 return hash;
66 };
67
68 bool YUVConversionDescriptorVKEqual::operator()(
69 const YUVConversionDescriptorVK& lhs_desc,
70 const YUVConversionDescriptorVK& rhs_desc) const {
71 // Default equality checks in Vulkan HPP checks pNext member members by
72 // pointer which isn't what we want.
73 {
74 const auto& lhs = lhs_desc.get();
75 const auto& rhs = rhs_desc.get();
76
77 if (lhs.format != rhs.format || //
78 lhs.ycbcrModel != rhs.ycbcrModel || //
79 lhs.ycbcrRange != rhs.ycbcrRange || //
80 lhs.components.r != rhs.components.r || //
81 lhs.components.g != rhs.components.g || //
82 lhs.components.b != rhs.components.b || //
83 lhs.components.a != rhs.components.a || //
84 lhs.xChromaOffset != rhs.xChromaOffset || //
85 lhs.yChromaOffset != rhs.yChromaOffset || //
86 lhs.chromaFilter != rhs.chromaFilter || //
87 lhs.forceExplicitReconstruction != rhs.forceExplicitReconstruction //
88 ) {
89 return false;
90 }
91 }
92 #if FML_OS_ANDROID
93 {
94 const auto lhs = lhs_desc.get<vk::ExternalFormatANDROID>();
95 const auto rhs = rhs_desc.get<vk::ExternalFormatANDROID>();
96 return lhs.externalFormat == rhs.externalFormat;
97 }
98 #else // FML_OS_ANDROID
99 return true;
100 #endif // FML_OS_ANDROID
101 }
102
103 ImmutableSamplerKeyVK::ImmutableSamplerKeyVK(const SamplerVK& sampler)
104 : sampler(sampler.GetDescriptor()) {
105 if (const auto& conversion = sampler.GetYUVConversion()) {
106 yuv_conversion = conversion->GetDescriptor();
107 }
108 }
109
110 bool ImmutableSamplerKeyVK::IsEqual(const ImmutableSamplerKeyVK& other) const {
111 return SamplerDescriptor::ToKey(sampler) ==
112 SamplerDescriptor::ToKey(other.sampler) &&
113 YUVConversionDescriptorVKEqual{}(yuv_conversion, other.yuv_conversion);
114 }
115
116 std::size_t ImmutableSamplerKeyVK::GetHash() const {
117 return fml::HashCombine(SamplerDescriptor::ToKey(sampler),
118 YUVConversionDescriptorVKHash{}(yuv_conversion));
119 }
120
121 } // namespace impeller
external_format
GLenum external_format
Definition: blit_command_gles.cc:150
impeller::SamplerVK
Definition: sampler_vk.h:18
impeller::YUVConversionVK::YUVConversionVK
YUVConversionVK(const YUVConversionVK &)=delete
device_holder_vk.h
impeller
Definition: allocation.cc:12
impeller::YUVConversionDescriptorVK
vk::StructureChain< vk::SamplerYcbcrConversionCreateInfo > YUVConversionDescriptorVK
Definition: yuv_conversion_vk.h:30
sampler_descriptor.h
sampler_vk.h
impeller::ImmutableSamplerKeyVK
Definition: yuv_conversion_vk.h:94
impeller::ImmutableSamplerKeyVK::IsEqual
bool IsEqual(const ImmutableSamplerKeyVK &other) const override
Definition: yuv_conversion_vk.cc:110
impeller::ImmutableSamplerKeyVK::yuv_conversion
YUVConversionDescriptorVK yuv_conversion
Definition: yuv_conversion_vk.h:96
impeller::ImmutableSamplerKeyVK::GetHash
std::size_t GetHash() const override
Definition: yuv_conversion_vk.cc:116
impeller::ImmutableSamplerKeyVK::sampler
SamplerDescriptor sampler
Definition: yuv_conversion_vk.h:95
impeller::SamplerDescriptor::ToKey
static uint64_t ToKey(const SamplerDescriptor &d)
Definition: sampler_descriptor.h:32
impeller::YUVConversionDescriptorVKEqual
Definition: yuv_conversion_vk.h:89
impeller::YUVConversionDescriptorVKHash
Definition: yuv_conversion_vk.h:85
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
yuv_conversion_vk.h