Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
6
7#include <memory>
8
15
16namespace impeller {
17
18/// A color matrix which inverts colors.
19// clang-format off
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
30std::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.
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
56std::shared_ptr<Contents> Paint::WithFilters(
57 std::shared_ptr<Contents> input) const {
58 input = WithColorFilter(input, ColorFilterContents::AbsorbOpacity::kYes);
59 auto image_filter =
61 if (image_filter) {
62 input = image_filter;
63 }
64 return input;
65}
66
67std::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
79std::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
88std::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
101std::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`.
107 return input;
108 }
109
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
125std::shared_ptr<FilterContents> Paint::MaskBlurDescriptor::CreateMaskBlur(
126 std::shared_ptr<TextureContents> texture_contents) const {
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 =
146 geometry);
147
150 {FilterInput::Make(blurred_mask), FilterInput::Make(texture_contents)});
151}
152
153std::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) {
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(
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),
194 }
195
196 /// 5. Composite the color source with the blurred mask.
197
200 {FilterInput::Make(blurred_mask), FilterInput::Make(color_contents)});
201}
202
203std::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,
209 }
210 return FilterContents::MakeBorderMaskBlur(input, sigma, sigma, style);
211}
212
213std::shared_ptr<ColorFilter> Paint::GetColorFilter() const {
217 }
218 if (invert_colors) {
220 }
221 if (color_filter) {
222 return color_filter;
223 }
224 return nullptr;
225}
226
228 return !!color_filter || invert_colors;
229}
230
231} // namespace impeller
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.
static std::shared_ptr< ColorFilter > MakeComposed(const std::shared_ptr< ColorFilter > &outer, const std::shared_ptr< ColorFilter > &inner)
static std::shared_ptr< ColorFilter > MakeMatrix(ColorMatrix color_matrix)
std::shared_ptr< ColorSourceContents > GetContents(const Paint &paint) const
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)
static std::shared_ptr< FilterContents > MakeBorderMaskBlur(FilterInput::Ref input, Sigma sigma_x, Sigma sigma_y, BlurStyle blur_style=BlurStyle::kNormal)
std::shared_ptr< FilterInput > Ref
std::variant< std::shared_ptr< FilterContents >, std::shared_ptr< Contents >, std::shared_ptr< Texture >, Rect > Variant
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
static std::shared_ptr< Geometry > MakeRect(const Rect &rect)
Definition geometry.cc:89
float Scalar
Definition scalar.h:18
constexpr ColorMatrix kColorInversion
A color matrix which inverts colors.
Definition paint.cc:20
TRect< Scalar > Rect
Definition rect.h:746
Scalar array[20]
Definition color.h:118
static constexpr Color White()
Definition color.h:256
A 4x4 matrix using column-major storage.
Definition matrix.h:37
std::shared_ptr< FilterContents > CreateMaskBlur(std::shared_ptr< ColorSourceContents > color_source_contents, const std::shared_ptr< ColorFilter > &color_filter) const
Definition paint.cc:153
FilterContents::BlurStyle style
Definition paint.h:40
std::shared_ptr< Contents > WithMaskBlur(std::shared_ptr< Contents > input, bool is_solid_color) const
Definition paint.cc:79
ColorSource color_source
Definition paint.h:56
std::shared_ptr< ColorFilter > GetColorFilter() const
Definition paint.cc:213
bool HasColorFilter() const
Whether this paint has a color filter that can apply opacity.
Definition paint.cc:227
std::shared_ptr< Contents > CreateContentsForGeometry(const std::shared_ptr< Geometry > &geometry) const
Definition paint.cc:30
Style style
Definition paint.h:63
bool invert_colors
Definition paint.h:65
std::shared_ptr< ImageFilter > image_filter
Definition paint.h:67
std::optional< MaskBlurDescriptor > mask_blur_descriptor
Definition paint.h:69
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
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
std::shared_ptr< FilterContents > WithImageFilter(const FilterInput::Variant &input, const Matrix &effect_transform, Entity::RenderingMode rendering_mode) const
Definition paint.cc:88
std::shared_ptr< ColorFilter > color_filter
Definition paint.h:68
Scalar sigma
Definition sigma.h:33