| Flutter Impeller |

paint.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/aiks/paint.h"
6
7 #include <memory>
8
9 #include "impeller/entity/contents/color_source_contents.h"
10 #include "impeller/entity/contents/filters/color_filter_contents.h"
11 #include "impeller/entity/contents/filters/filter_contents.h"
12 #include "impeller/entity/contents/filters/gaussian_blur_filter_contents.h"
13 #include "impeller/entity/contents/solid_color_contents.h"
14 #include "impeller/entity/geometry/geometry.h"
15
16 namespace impeller {
17
18 /// A color matrix which inverts colors.
19 // clang-format off
20 constexpr ColorMatrix kColorInversion = {
21 .array = {
22 -1.0, 0, 0, 1.0, 0, //
23 0, -1.0, 0, 1.0, 0, //
24 0, 0, -1.0, 1.0, 0, //
25 1.0, 1.0, 1.0, 1.0, 0 //
26 }
27 };
28 // clang-format on
29
30 std::shared_ptr<Contents> Paint::CreateContentsForGeometry(
31 const std::shared_ptr<Geometry>& geometry) const {
32 auto contents = color_source.GetContents(*this);
33
34 // Attempt to apply the color filter on the CPU first.
35 // Note: This is not just an optimization; some color sources rely on
36 // CPU-applied color filters to behave properly.
37 auto color_filter = GetColorFilter();
38 bool needs_color_filter = !!color_filter;
39 if (color_filter &&
40 contents->ApplyColorFilter(color_filter->GetCPUColorFilterProc())) {
41 needs_color_filter = false;
42 }
43
44 contents->SetGeometry(geometry);
45 if (mask_blur_descriptor.has_value()) {
46 // If there's a mask blur and we need to apply the color filter on the GPU,
47 // we need to be careful to only apply the color filter to the source
48 // colors. CreateMaskBlur is able to handle this case.
49 return mask_blur_descriptor->CreateMaskBlur(
50 contents, needs_color_filter ? color_filter : nullptr);
51 }
52
53 return contents;
54 }
55
56 std::shared_ptr<Contents> Paint::WithFilters(
57 std::shared_ptr<Contents> input) const {
58 input = WithColorFilter(input, ColorFilterContents::AbsorbOpacity::kYes);
59 auto image_filter =
60 WithImageFilter(input, Matrix(), Entity::RenderingMode::kDirect);
61 if (image_filter) {
62 input = image_filter;
63 }
64 return input;
65 }
66
67 std::shared_ptr<Contents> Paint::WithFiltersForSubpassTarget(
68 std::shared_ptr<Contents> input,
69 const Matrix& effect_transform) const {
70 auto image_filter =
71 WithImageFilter(input, effect_transform, Entity::RenderingMode::kSubpass);
72 if (image_filter) {
73 input = image_filter;
74 }
75 input = WithColorFilter(input, ColorFilterContents::AbsorbOpacity::kYes);
76 return input;
77 }
78
79 std::shared_ptr<Contents> Paint::WithMaskBlur(std::shared_ptr<Contents> input,
80 bool is_solid_color) const {
81 if (mask_blur_descriptor.has_value()) {
82 input = mask_blur_descriptor->CreateMaskBlur(FilterInput::Make(input),
83 is_solid_color);
84 }
85 return input;
86 }
87
88 std::shared_ptr<FilterContents> Paint::WithImageFilter(
89 const FilterInput::Variant& input,
90 const Matrix& effect_transform,
91 Entity::RenderingMode rendering_mode) const {
92 if (!image_filter) {
93 return nullptr;
94 }
95 auto filter = image_filter->WrapInput(FilterInput::Make(input));
96 filter->SetRenderingMode(rendering_mode);
97 filter->SetEffectTransform(effect_transform);
98 return filter;
99 }
100
101 std::shared_ptr<Contents> Paint::WithColorFilter(
102 std::shared_ptr<Contents> input,
103 ColorFilterContents::AbsorbOpacity absorb_opacity) const {
104 // Image input types will directly set their color filter,
105 // if any. See `TiledTextureContents.SetColorFilter`.
106 if (color_source.GetType() == ColorSource::Type::kImage) {
107 return input;
108 }
109
110 auto color_filter = GetColorFilter();
111 if (!color_filter) {
112 return input;
113 }
114
115 // Attempt to apply the color filter on the CPU first.
116 // Note: This is not just an optimization; some color sources rely on
117 // CPU-applied color filters to behave properly.
118 if (input->ApplyColorFilter(color_filter->GetCPUColorFilterProc())) {
119 return input;
120 }
121 return color_filter->WrapWithGPUColorFilter(FilterInput::Make(input),
122 absorb_opacity);
123 }
124
125 std::shared_ptr<FilterContents> Paint::MaskBlurDescriptor::CreateMaskBlur(
126 std::shared_ptr<TextureContents> texture_contents) const {
127 Scalar expand_amount = GaussianBlurFilterContents::CalculateBlurRadius(
128 GaussianBlurFilterContents::ScaleSigma(sigma.sigma));
129 texture_contents->SetSourceRect(
130 texture_contents->GetSourceRect().Expand(expand_amount, expand_amount));
131 auto mask = std::make_shared<SolidColorContents>();
132 mask->SetColor(Color::White());
133 std::optional<Rect> coverage = texture_contents->GetCoverage({});
134 std::shared_ptr<Geometry> geometry;
135 if (coverage) {
136 texture_contents->SetDestinationRect(
137 coverage.value().Expand(expand_amount, expand_amount));
138 geometry = Geometry::MakeRect(coverage.value());
139 }
140 mask->SetGeometry(geometry);
141 auto descriptor = texture_contents->GetSamplerDescriptor();
142 texture_contents->SetSamplerDescriptor(descriptor);
143 std::shared_ptr<FilterContents> blurred_mask =
144 FilterContents::MakeGaussianBlur(FilterInput::Make(mask), sigma, sigma,
145 Entity::TileMode::kDecal, style,
146 geometry);
147
148 return ColorFilterContents::MakeBlend(
149 BlendMode::kSourceIn,
150 {FilterInput::Make(blurred_mask), FilterInput::Make(texture_contents)});
151 }
152
153 std::shared_ptr<FilterContents> Paint::MaskBlurDescriptor::CreateMaskBlur(
154 std::shared_ptr<ColorSourceContents> color_source_contents,
155 const std::shared_ptr<ColorFilter>& color_filter) const {
156 // If it's a solid color and there is no color filter, then we can just get
157 // away with doing one Gaussian blur.
158 if (color_source_contents->IsSolidColor() && !color_filter) {
159 return FilterContents::MakeGaussianBlur(
160 FilterInput::Make(color_source_contents), sigma, sigma,
161 Entity::TileMode::kDecal, style, color_source_contents->GetGeometry());
162 }
163
164 /// 1. Create an opaque white mask of the original geometry.
165
166 auto mask = std::make_shared<SolidColorContents>();
167 mask->SetColor(Color::White());
168 mask->SetGeometry(color_source_contents->GetGeometry());
169
170 /// 2. Blur the mask.
171
172 auto blurred_mask = FilterContents::MakeGaussianBlur(
173 FilterInput::Make(mask), sigma, sigma, Entity::TileMode::kDecal, style,
174 color_source_contents->GetGeometry());
175
176 /// 3. Replace the geometry of the original color source with a rectangle that
177 /// covers the full region of the blurred mask. Note that geometry is in
178 /// local bounds.
179
180 auto expanded_local_bounds = blurred_mask->GetCoverage({});
181 if (!expanded_local_bounds.has_value()) {
182 expanded_local_bounds = Rect();
183 }
184 color_source_contents->SetGeometry(
185 Geometry::MakeRect(*expanded_local_bounds));
186 std::shared_ptr<Contents> color_contents = color_source_contents;
187
188 /// 4. Apply the user set color filter on the GPU, if applicable.
189
190 if (color_filter) {
191 color_contents = color_filter->WrapWithGPUColorFilter(
192 FilterInput::Make(color_source_contents),
193 ColorFilterContents::AbsorbOpacity::kYes);
194 }
195
196 /// 5. Composite the color source with the blurred mask.
197
198 return ColorFilterContents::MakeBlend(
199 BlendMode::kSourceIn,
200 {FilterInput::Make(blurred_mask), FilterInput::Make(color_contents)});
201 }
202
203 std::shared_ptr<FilterContents> Paint::MaskBlurDescriptor::CreateMaskBlur(
204 const FilterInput::Ref& input,
205 bool is_solid_color) const {
206 if (is_solid_color) {
207 return FilterContents::MakeGaussianBlur(input, sigma, sigma,
208 Entity::TileMode::kDecal, style);
209 }
210 return FilterContents::MakeBorderMaskBlur(input, sigma, sigma, style);
211 }
212
213 std::shared_ptr<ColorFilter> Paint::GetColorFilter() const {
214 if (invert_colors && color_filter) {
215 auto filter = ColorFilter::MakeMatrix(kColorInversion);
216 return ColorFilter::MakeComposed(filter, color_filter);
217 }
218 if (invert_colors) {
219 return ColorFilter::MakeMatrix(kColorInversion);
220 }
221 if (color_filter) {
222 return color_filter;
223 }
224 return nullptr;
225 }
226
227 bool Paint::HasColorFilter() const {
228 return !!color_filter || invert_colors;
229 }
230
231 } // namespace impeller
impeller::Sigma::sigma
Scalar sigma
Definition: sigma.h:33
impeller::Paint::WithMaskBlur
std::shared_ptr< Contents > WithMaskBlur(std::shared_ptr< Contents > input, bool is_solid_color) const
Definition: paint.cc:79
impeller::ColorSource::GetContents
std::shared_ptr< ColorSourceContents > GetContents(const Paint &paint) const
Definition: color_source.cc:234
impeller::Scalar
float Scalar
Definition: scalar.h:18
solid_color_contents.h
impeller::FilterInput::Make
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
impeller::Paint::MaskBlurDescriptor::sigma
Sigma sigma
Definition: paint.h:41
impeller::Entity::TileMode::kDecal
@ kDecal
impeller::ColorFilter::MakeComposed
static std::shared_ptr< ColorFilter > MakeComposed(const std::shared_ptr< ColorFilter > &outer, const std::shared_ptr< ColorFilter > &inner)
Definition: color_filter.cc:40
impeller::Paint::MaskBlurDescriptor::style
FilterContents::BlurStyle style
Definition: paint.h:40
impeller::FilterInput::Ref
std::shared_ptr< FilterInput > Ref
Definition: filter_input.h:32
impeller::Paint::MaskBlurDescriptor::CreateMaskBlur
std::shared_ptr< FilterContents > CreateMaskBlur(std::shared_ptr< ColorSourceContents > color_source_contents, const std::shared_ptr< ColorFilter > &color_filter) const
Definition: paint.cc:153
gaussian_blur_filter_contents.h
impeller::GaussianBlurFilterContents::CalculateBlurRadius
static Scalar CalculateBlurRadius(Scalar sigma)
Definition: gaussian_blur_filter_contents.cc:556
impeller::Paint::color_source
ColorSource color_source
Definition: paint.h:56
impeller::Paint::color_filter
std::shared_ptr< ColorFilter > color_filter
Definition: paint.h:68
impeller::kColorInversion
constexpr ColorMatrix kColorInversion
A color matrix which inverts colors.
Definition: paint.cc:20
filter_contents.h
color_source_contents.h
impeller::Paint::GetColorFilter
std::shared_ptr< ColorFilter > GetColorFilter() const
Definition: paint.cc:213
impeller::Paint::style
Style style
Definition: paint.h:63
impeller::ColorMatrix::array
Scalar array[20]
Definition: color.h:118
geometry.h
impeller::Color::White
static constexpr Color White()
Definition: color.h:256
impeller::Entity::RenderingMode::kDirect
@ kDirect
impeller::FilterInput::Variant
std::variant< std::shared_ptr< FilterContents >, std::shared_ptr< Contents >, std::shared_ptr< Texture >, Rect > Variant
Definition: filter_input.h:37
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:661
impeller::FilterContents::MakeBorderMaskBlur
static std::shared_ptr< FilterContents > MakeBorderMaskBlur(FilterInput::Ref input, Sigma sigma_x, Sigma sigma_y, BlurStyle blur_style=BlurStyle::kNormal)
Definition: filter_contents.cc:49
color_filter_contents.h
impeller::Entity::RenderingMode::kSubpass
@ kSubpass
impeller::ColorFilter::MakeMatrix
static std::shared_ptr< ColorFilter > MakeMatrix(ColorMatrix color_matrix)
Definition: color_filter.cc:28
impeller::Geometry::MakeRect
static std::shared_ptr< Geometry > MakeRect(const Rect &rect)
Definition: geometry.cc:204
impeller::ColorSource::Type::kImage
@ kImage
impeller::Paint::WithImageFilter
std::shared_ptr< FilterContents > WithImageFilter(const FilterInput::Variant &input, const Matrix &effect_transform, Entity::RenderingMode rendering_mode) const
Definition: paint.cc:88
impeller::Paint::invert_colors
bool invert_colors
Definition: paint.h:65
impeller::BlendMode::kSourceIn
@ kSourceIn
paint.h
impeller::Paint::HasColorFilter
bool HasColorFilter() const
Whether this paint has a color filter that can apply opacity.
Definition: paint.cc:227
impeller::Entity::RenderingMode
RenderingMode
Definition: entity.h:28
impeller::ColorFilterContents::AbsorbOpacity::kYes
@ kYes
impeller::ColorMatrix
Definition: color.h:117
impeller::ColorFilterContents::MakeBlend
static std::shared_ptr< ColorFilterContents > MakeBlend(BlendMode blend_mode, FilterInput::Vector inputs, std::optional< Color > foreground_color=std::nullopt)
the [inputs] are expected to be in the order of dst, src.
Definition: color_filter_contents.cc:17
impeller::ColorFilterContents::AbsorbOpacity
AbsorbOpacity
Definition: color_filter_contents.h:14
impeller
Definition: aiks_blur_unittests.cc:20
impeller::Paint::mask_blur_descriptor
std::optional< MaskBlurDescriptor > mask_blur_descriptor
Definition: paint.h:69
impeller::Paint::WithFilters
std::shared_ptr< Contents > WithFilters(std::shared_ptr< Contents > input) const
Wrap this paint's configured filters to the given contents.
Definition: paint.cc:56
impeller::ColorSource::GetType
Type GetType() const
Definition: color_source.cc:230
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Paint::WithFiltersForSubpassTarget
std::shared_ptr< Contents > WithFiltersForSubpassTarget(std::shared_ptr< Contents > input, const Matrix &effect_transform=Matrix()) const
Wrap this paint's configured filters to the given contents of subpass target.
Definition: paint.cc:67
impeller::Paint::CreateContentsForGeometry
std::shared_ptr< Contents > CreateContentsForGeometry(const std::shared_ptr< Geometry > &geometry) const
Definition: paint.cc:30
impeller::Paint::image_filter
std::shared_ptr< ImageFilter > image_filter
Definition: paint.h:67
impeller::GaussianBlurFilterContents::ScaleSigma
static Scalar ScaleSigma(Scalar sigma)
Definition: gaussian_blur_filter_contents.cc:576
impeller::FilterContents::MakeGaussianBlur
static std::shared_ptr< FilterContents > MakeGaussianBlur(const FilterInput::Ref &input, Sigma sigma_x, Sigma sigma_y, Entity::TileMode tile_mode=Entity::TileMode::kDecal, BlurStyle mask_blur_style=BlurStyle::kNormal, const std::shared_ptr< Geometry > &mask_geometry=nullptr)
Definition: filter_contents.cc:36