| Flutter Impeller |

filter_contents.h
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 #pragma once
6
7 #include <memory>
8 #include <optional>
9 #include <variant>
10 #include <vector>
11
12 #include "impeller/core/formats.h"
13 #include "impeller/entity/contents/filters/inputs/filter_input.h"
14 #include "impeller/entity/entity.h"
15 #include "impeller/geometry/sigma.h"
16
17 namespace impeller {
18
19 class FilterContents : public Contents {
20 public:
21 enum class BlurStyle {
22 /// Blurred inside and outside.
23 kNormal,
24 /// Solid inside, blurred outside.
25 kSolid,
26 /// Nothing inside, blurred outside.
27 kOuter,
28 /// Blurred inside, nothing outside.
29 kInner,
30 };
31
32 enum class MorphType { kDilate, kErode };
33
34 static std::shared_ptr<FilterContents> MakeDirectionalGaussianBlur(
35 FilterInput::Ref input,
36 Sigma sigma,
37 Vector2 direction,
38 BlurStyle blur_style = BlurStyle::kNormal,
39 Entity::TileMode tile_mode = Entity::TileMode::kDecal,
40 bool is_second_pass = false,
41 Sigma secondary_sigma = {});
42
43 static std::shared_ptr<FilterContents> MakeGaussianBlur(
44 const FilterInput::Ref& input,
45 Sigma sigma_x,
46 Sigma sigma_y,
47 BlurStyle blur_style = BlurStyle::kNormal,
48 Entity::TileMode tile_mode = Entity::TileMode::kDecal);
49
50 static std::shared_ptr<FilterContents> MakeBorderMaskBlur(
51 FilterInput::Ref input,
52 Sigma sigma_x,
53 Sigma sigma_y,
54 BlurStyle blur_style = BlurStyle::kNormal);
55
56 static std::shared_ptr<FilterContents> MakeDirectionalMorphology(
57 FilterInput::Ref input,
58 Radius radius,
59 Vector2 direction,
60 MorphType morph_type);
61
62 static std::shared_ptr<FilterContents> MakeMorphology(FilterInput::Ref input,
63 Radius radius_x,
64 Radius radius_y,
65 MorphType morph_type);
66
67 static std::shared_ptr<FilterContents> MakeMatrixFilter(
68 FilterInput::Ref input,
69 const Matrix& matrix,
70 const SamplerDescriptor& desc);
71
72 static std::shared_ptr<FilterContents> MakeLocalMatrixFilter(
73 FilterInput::Ref input,
74 const Matrix& matrix);
75
76 static std::shared_ptr<FilterContents> MakeYUVToRGBFilter(
77 std::shared_ptr<Texture> y_texture,
78 std::shared_ptr<Texture> uv_texture,
79 YUVColorSpace yuv_color_space);
80
81 FilterContents();
82
83 ~FilterContents() override;
84
85 /// @brief The input texture sources for this filter. Each input's emitted
86 /// texture is expected to have premultiplied alpha colors.
87 ///
88 /// The number of required or optional textures depends on the
89 /// particular filter's implementation.
90 void SetInputs(FilterInput::Vector inputs);
91
92 /// @brief Sets the transform which gets appended to the effect of this
93 /// filter. Note that this is in addition to the entity's transform.
94 ///
95 /// This is useful for subpass rendering scenarios where it's
96 /// difficult to encode the current transform of the layer into the
97 /// Entity being rendered.
98 void SetEffectTransform(const Matrix& effect_transform);
99
100 /// @brief Create an Entity that renders this filter's output.
101 std::optional<Entity> GetEntity(
102 const ContentContext& renderer,
103 const Entity& entity,
104 const std::optional<Rect>& coverage_hint) const;
105
106 // |Contents|
107 bool Render(const ContentContext& renderer,
108 const Entity& entity,
109 RenderPass& pass) const override;
110
111 // |Contents|
112 std::optional<Rect> GetCoverage(const Entity& entity) const override;
113
114 // |Contents|
115 void PopulateGlyphAtlas(
116 const std::shared_ptr<LazyGlyphAtlas>& lazy_glyph_atlas,
117 Scalar scale) override;
118
119 // |Contents|
120 std::optional<Snapshot> RenderToSnapshot(
121 const ContentContext& renderer,
122 const Entity& entity,
123 std::optional<Rect> coverage_limit = std::nullopt,
124 const std::optional<SamplerDescriptor>& sampler_descriptor = std::nullopt,
125 bool msaa_enabled = true,
126 const std::string& label = "Filter Snapshot") const override;
127
128 // |Contents|
129 const FilterContents* AsFilter() const override;
130
131 virtual Matrix GetLocalTransform(const Matrix& parent_transform) const;
132
133 Matrix GetTransform(const Matrix& parent_transform) const;
134
135 /// @brief Returns true if this filter graph doesn't perform any basis
136 /// transformations to the filtered content. For example: Rotating,
137 /// scaling, and skewing are all basis transformations, but
138 /// translating is not.
139 ///
140 /// This is useful for determining whether a filtered object's space
141 /// is compatible enough with the parent pass space to perform certain
142 /// subpass clipping optimizations.
143 virtual bool IsTranslationOnly() const;
144
145 /// @brief Returns `true` if this filter does not have any `FilterInput`
146 /// children.
147 bool IsLeaf() const;
148
149 /// @brief Replaces the set of all leaf `FilterContents` with a new set
150 /// of `FilterInput`s.
151 /// @see `FilterContents::IsLeaf`
152 void SetLeafInputs(const FilterInput::Vector& inputs);
153
154 /// @brief Marks this filter chain as applying in a subpass scenario.
155 ///
156 /// Subpasses render in screenspace, and this setting informs filters
157 /// that the current transformation matrix of the entity is not stored
158 /// in the Entity transformation matrix. Instead, the effect transform
159 /// is used in this case.
160 virtual void SetRenderingMode(Entity::RenderingMode rendering_mode);
161
162 private:
163 virtual std::optional<Rect> GetFilterCoverage(
164 const FilterInput::Vector& inputs,
165 const Entity& entity,
166 const Matrix& effect_transform) const;
167
168 /// @brief Converts zero or more filter inputs into a render instruction.
169 virtual std::optional<Entity> RenderFilter(
170 const FilterInput::Vector& inputs,
171 const ContentContext& renderer,
172 const Entity& entity,
173 const Matrix& effect_transform,
174 const Rect& coverage,
175 const std::optional<Rect>& coverage_hint) const = 0;
176
177 std::optional<Rect> GetLocalCoverage(const Entity& local_entity) const;
178
179 FilterInput::Vector inputs_;
180 Matrix effect_transform_;
181
182 FML_DISALLOW_COPY_AND_ASSIGN(FilterContents);
183 };
184
185 } // namespace impeller
impeller::FilterContents::PopulateGlyphAtlas
void PopulateGlyphAtlas(const std::shared_ptr< LazyGlyphAtlas > &lazy_glyph_atlas, Scalar scale) override
Add any text data to the specified lazy atlas. The scale parameter must be used again later when draw...
Definition: filter_contents.cc:186
impeller::FilterContents::IsLeaf
bool IsLeaf() const
Returns true if this filter does not have any FilterInput children.
Definition: filter_contents.cc:282
impeller::FilterContents::SetInputs
void SetInputs(FilterInput::Vector inputs)
The input texture sources for this filter. Each input's emitted texture is expected to have premultip...
Definition: filter_contents.cc:138
impeller::FilterContents::MorphType::kErode
@ kErode
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::FilterContents::FilterContents
FilterContents()
entity.h
impeller::FilterContents::SetEffectTransform
void SetEffectTransform(const Matrix &effect_transform)
Sets the transform which gets appended to the effect of this filter. Note that this is in addition to...
Definition: filter_contents.cc:142
impeller::FilterContents::BlurStyle
BlurStyle
Definition: filter_contents.h:21
impeller::Entity::TileMode::kDecal
@ kDecal
impeller::FilterContents::GetTransform
Matrix GetTransform(const Matrix &parent_transform) const
Definition: filter_contents.cc:270
impeller::FilterContents::RenderToSnapshot
std::optional< Snapshot > RenderToSnapshot(const ContentContext &renderer, const Entity &entity, std::optional< Rect > coverage_limit=std::nullopt, const std::optional< SamplerDescriptor > &sampler_descriptor=std::nullopt, bool msaa_enabled=true, const std::string &label="Filter Snapshot") const override
Render this contents to a snapshot, respecting the entity's transform, path, stencil depth,...
Definition: filter_contents.cc:238
formats.h
impeller::FilterInput::Ref
std::shared_ptr< FilterInput > Ref
Definition: filter_input.h:31
impeller::Vector2
Point Vector2
Definition: point.h:310
impeller::FilterContents::MakeDirectionalGaussianBlur
static std::shared_ptr< FilterContents > MakeDirectionalGaussianBlur(FilterInput::Ref input, Sigma sigma, Vector2 direction, BlurStyle blur_style=BlurStyle::kNormal, Entity::TileMode tile_mode=Entity::TileMode::kDecal, bool is_second_pass=false, Sigma secondary_sigma={})
Definition: filter_contents.cc:33
impeller::FilterContents::BlurStyle::kNormal
@ kNormal
Blurred inside and outside.
impeller::FilterContents::MakeGaussianBlur
static std::shared_ptr< FilterContents > MakeGaussianBlur(const FilterInput::Ref &input, Sigma sigma_x, Sigma sigma_y, BlurStyle blur_style=BlurStyle::kNormal, Entity::TileMode tile_mode=Entity::TileMode::kDecal)
Definition: filter_contents.cc:52
impeller::FilterContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: filter_contents.cc:150
sigma.h
impeller::FilterContents::BlurStyle::kSolid
@ kSolid
Solid inside, blurred outside.
impeller::FilterContents::IsTranslationOnly
virtual bool IsTranslationOnly() const
Returns true if this filter graph doesn't perform any basis transformations to the filtered content....
Definition: filter_contents.cc:273
impeller::FilterContents::MakeDirectionalMorphology
static std::shared_ptr< FilterContents > MakeDirectionalMorphology(FilterInput::Ref input, Radius radius, Vector2 direction, MorphType morph_type)
Definition: filter_contents.cc:78
impeller::Sigma
In filters that use Gaussian distributions, "sigma" is a size of one standard deviation in terms of t...
Definition: sigma.h:31
filter_input.h
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:306
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:66
impeller::FilterContents::BlurStyle::kInner
@ kInner
Blurred inside, nothing outside.
impeller::Entity::TileMode
TileMode
Definition: entity.h:40
impeller::FilterContents::MakeYUVToRGBFilter
static std::shared_ptr< FilterContents > MakeYUVToRGBFilter(std::shared_ptr< Texture > y_texture, std::shared_ptr< Texture > uv_texture, YUVColorSpace yuv_color_space)
Definition: filter_contents.cc:123
impeller::FilterContents::MorphType
MorphType
Definition: filter_contents.h:32
impeller::FilterContents::BlurStyle::kOuter
@ kOuter
Nothing inside, blurred outside.
impeller::FilterContents::MakeMorphology
static std::shared_ptr< FilterContents > MakeMorphology(FilterInput::Ref input, Radius radius_x, Radius radius_y, MorphType morph_type)
Definition: filter_contents.cc:91
impeller::FilterContents::MorphType::kDilate
@ kDilate
impeller::FilterContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the screen space bounding rectangle that this contents affects.
Definition: filter_contents.cc:178
impeller::FilterContents::GetEntity
std::optional< Entity > GetEntity(const ContentContext &renderer, const Entity &entity, const std::optional< Rect > &coverage_hint) const
Create an Entity that renders this filter's output.
Definition: filter_contents.cc:221
impeller::FilterContents::SetLeafInputs
void SetLeafInputs(const FilterInput::Vector &inputs)
Replaces the set of all leaf FilterContents with a new set of FilterInputs.
Definition: filter_contents.cc:291
impeller::TPoint< Scalar >
impeller::FilterContents::MakeMatrixFilter
static std::shared_ptr< FilterContents > MakeMatrixFilter(FilterInput::Ref input, const Matrix &matrix, const SamplerDescriptor &desc)
Definition: filter_contents.cc:103
impeller::YUVColorSpace
YUVColorSpace
Definition: color.h:53
impeller::FilterContents::GetLocalTransform
virtual Matrix GetLocalTransform(const Matrix &parent_transform) const
Definition: filter_contents.cc:266
impeller::FilterContents::SetRenderingMode
virtual void SetRenderingMode(Entity::RenderingMode rendering_mode)
Marks this filter chain as applying in a subpass scenario.
Definition: filter_contents.cc:301
impeller::Entity::RenderingMode
RenderingMode
Definition: entity.h:26
impeller::FilterContents::MakeLocalMatrixFilter
static std::shared_ptr< FilterContents > MakeLocalMatrixFilter(FilterInput::Ref input, const Matrix &matrix)
Definition: filter_contents.cc:114
impeller::FilterInput::Vector
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:32
impeller::Contents
Definition: contents.h:33
impeller::FilterContents::~FilterContents
~FilterContents() override
impeller
Definition: aiks_context.cc:10
impeller::FilterContents::AsFilter
const FilterContents * AsFilter() const override
Cast to a filter. Returns nullptr if this Contents is not a filter.
Definition: filter_contents.cc:262
impeller::FilterContents
Definition: filter_contents.h:19