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 #ifndef FLUTTER_IMPELLER_ENTITY_CONTENTS_FILTERS_FILTER_CONTENTS_H_
6 #define FLUTTER_IMPELLER_ENTITY_CONTENTS_FILTERS_FILTER_CONTENTS_H_
7 
8 #include <memory>
9 #include <optional>
10 #include <variant>
11 #include <vector>
12 
13 #include "impeller/core/formats.h"
16 #include "impeller/entity/entity.h"
21 
22 namespace impeller {
23 
24 class FilterContents : public Contents {
25  public:
26  static const int32_t kBlurFilterRequiredMipCount;
27 
28  enum class BlurStyle {
29  /// Blurred inside and outside.
30  kNormal,
31  /// Solid inside, blurred outside.
32  kSolid,
33  /// Nothing inside, blurred outside.
34  kOuter,
35  /// Blurred inside, nothing outside.
36  kInner,
37  };
38 
39  enum class MorphType { kDilate, kErode };
40 
41  /// Creates a gaussian blur that operates in 2 dimensions.
42  /// See also: `MakeDirectionalGaussianBlur`
43  static std::shared_ptr<FilterContents> MakeGaussianBlur(
44  const FilterInput::Ref& input,
45  Sigma sigma_x,
46  Sigma sigma_y,
48  BlurStyle mask_blur_style = BlurStyle::kNormal,
49  const Geometry* mask_geometry = nullptr);
50 
51  static std::shared_ptr<FilterContents> MakeBorderMaskBlur(
52  FilterInput::Ref input,
53  Sigma sigma_x,
54  Sigma sigma_y,
55  BlurStyle blur_style = BlurStyle::kNormal);
56 
57  static std::shared_ptr<FilterContents> MakeDirectionalMorphology(
58  FilterInput::Ref input,
59  Radius radius,
60  Vector2 direction,
61  MorphType morph_type);
62 
63  static std::shared_ptr<FilterContents> MakeMorphology(FilterInput::Ref input,
64  Radius radius_x,
65  Radius radius_y,
66  MorphType morph_type);
67 
68  static std::shared_ptr<FilterContents> MakeMatrixFilter(
69  FilterInput::Ref input,
70  const Matrix& matrix,
71  const SamplerDescriptor& desc);
72 
73  static std::shared_ptr<FilterContents> MakeLocalMatrixFilter(
74  FilterInput::Ref input,
75  const Matrix& matrix);
76 
77  static std::shared_ptr<FilterContents> MakeYUVToRGBFilter(
78  std::shared_ptr<Texture> y_texture,
79  std::shared_ptr<Texture> uv_texture,
80  YUVColorSpace yuv_color_space);
81 
82  static std::shared_ptr<FilterContents> MakeRuntimeEffect(
83  FilterInput::Ref input,
84  std::shared_ptr<RuntimeStage> runtime_stage,
85  std::shared_ptr<std::vector<uint8_t>> uniforms,
86  std::vector<RuntimeEffectContents::TextureInput> texture_inputs);
87 
89 
90  ~FilterContents() override;
91 
92  /// @brief The input texture sources for this filter. Each input's emitted
93  /// texture is expected to have premultiplied alpha colors.
94  ///
95  /// The number of required or optional textures depends on the
96  /// particular filter's implementation.
97  void SetInputs(FilterInput::Vector inputs);
98 
99  /// @brief Sets the transform which gets appended to the effect of this
100  /// filter. Note that this is in addition to the entity's transform.
101  ///
102  /// This is useful for subpass rendering scenarios where it's
103  /// difficult to encode the current transform of the layer into the
104  /// Entity being rendered.
105  void SetEffectTransform(const Matrix& effect_transform);
106 
107  /// @brief Create an Entity that renders this filter's output.
108  std::optional<Entity> GetEntity(
109  const ContentContext& renderer,
110  const Entity& entity,
111  const std::optional<Rect>& coverage_hint) const;
112 
113  // |Contents|
114  bool Render(const ContentContext& renderer,
115  const Entity& entity,
116  RenderPass& pass) const override;
117 
118  // |Contents|
119  std::optional<Rect> GetCoverage(const Entity& entity) const override;
120 
121  // |Contents|
122  std::optional<Snapshot> RenderToSnapshot(
123  const ContentContext& renderer,
124  const Entity& entity,
125  const SnapshotOptions& options) const override;
126 
127  /// @brief Determines the coverage of source pixels that will be needed
128  /// to produce results for the specified |output_limit| under the
129  /// specified |effect_transform|. This is essentially a reverse of
130  /// the |GetCoverage| method computing a source coverage from
131  /// an intended |output_limit| coverage.
132  ///
133  /// Both the |output_limit| and the return value are in the
134  /// transformed coordinate space, and so do not need to be
135  /// transformed or inverse transformed by the |effect_transform|
136  /// but individual parameters on the filter might be in the
137  /// untransformed space and should be transformed by the
138  /// |effect_transform| before applying them to the coverages.
139  ///
140  /// The method computes a result such that if the filter is applied
141  /// to a set of pixels filling the computed source coverage, it
142  /// should produce an output that covers the entire specified
143  /// |output_limit|.
144  ///
145  /// This is useful for subpass rendering scenarios where a filter
146  /// will be applied to the output of the subpass and we need to
147  /// determine how large of a render target to allocate in order
148  /// to collect all pixels that might affect the supplied output
149  /// coverage limit. While we might end up clipping the rendering
150  /// of the subpass to its destination, we want to avoid clipping
151  /// out any pixels that contribute to the output limit via the
152  /// filtering operation.
153  ///
154  /// @return The coverage bounds in the transformed space of any source pixel
155  /// that may be needed to produce output for the indicated filter
156  /// that covers the indicated |output_limit|.
157  std::optional<Rect> GetSourceCoverage(const Matrix& effect_transform,
158  const Rect& output_limit) const;
159 
160  virtual Matrix GetLocalTransform(const Matrix& parent_transform) const;
161 
162  Matrix GetTransform(const Matrix& parent_transform) const;
163 
164  /// @brief Marks this filter chain as applying in a subpass scenario.
165  ///
166  /// Subpasses render in screenspace, and this setting informs filters
167  /// that the current transform matrix of the entity is not stored
168  /// in the Entity transform matrix. Instead, the effect transform
169  /// is used in this case.
170  virtual void SetRenderingMode(Entity::RenderingMode rendering_mode);
171 
172  private:
173  /// @brief Internal utility method for |GetLocalCoverage| that computes
174  /// the output coverage of this filter across the specified inputs,
175  /// ignoring the coverage hint.
176  virtual std::optional<Rect> GetFilterCoverage(
177  const FilterInput::Vector& inputs,
178  const Entity& entity,
179  const Matrix& effect_transform) const;
180 
181  /// @brief Internal utility method for |GetSourceCoverage| that computes
182  /// the inverse effect of this transform on the specified output
183  /// coverage, ignoring the inputs which will be accommodated by
184  /// the caller.
185  virtual std::optional<Rect> GetFilterSourceCoverage(
186  const Matrix& effect_transform,
187  const Rect& output_limit) const = 0;
188 
189  /// Applies the specific filter logic to the given inputs and returns an
190  /// Entity representing the filtered result.
191  ///
192  /// This is the primary method that subclasses must implement to define their
193  /// filtering behavior. It takes the results of evaluating the filter inputs
194  /// (as Snapshots) and produces a new Entity containing the filtered output.
195  ///
196  /// @param[in] inputs The evaluated inputs to the filter, typically as
197  /// Snapshots.
198  /// @param[in] renderer The content context providing rendering resources.
199  /// @param[in] entity The entity applying this filter, providing transform,
200  /// blend mode, and other context.
201  /// @param[in] effect_transform An additional transform applied after the
202  /// entity's transform, often used in subpass scenarios.
203  /// @param[in] coverage The calculated coverage area of the filter's output
204  /// in the coordinate space after applying the entity and effect transforms.
205  /// @param[in] coverage_hint An optional hint representing the desired output
206  /// coverage area, which can be used for optimization (e.g., rendering only a
207  /// portion of the input).
208  ///
209  /// @return An optional Entity containing the rendered result of the filter.
210  /// Returns `std::nullopt` if the filter cannot be applied or results in empty
211  /// output.
212  virtual std::optional<Entity> RenderFilter(
213  const FilterInput::Vector& inputs,
214  const ContentContext& renderer,
215  const Entity& entity,
216  const Matrix& effect_transform,
217  const Rect& coverage,
218  const std::optional<Rect>& coverage_hint) const = 0;
219 
220  /// @brief Internal utility method to compute the coverage of this
221  /// filter across its internally specified inputs and subject
222  /// to the coverage hint.
223  ///
224  /// Uses |GetFilterCoverage|.
225  std::optional<Rect> GetLocalCoverage(const Entity& local_entity) const;
226 
227  FilterInput::Vector inputs_;
228  Matrix effect_transform_ = Matrix();
229 
230  FilterContents(const FilterContents&) = delete;
231 
232  FilterContents& operator=(const FilterContents&) = delete;
233 };
234 
235 } // namespace impeller
236 
237 #endif // FLUTTER_IMPELLER_ENTITY_CONTENTS_FILTERS_FILTER_CONTENTS_H_
static const int32_t kBlurFilterRequiredMipCount
@ kNormal
Blurred inside and outside.
@ kOuter
Nothing inside, blurred outside.
@ kInner
Blurred inside, nothing outside.
@ kSolid
Solid inside, blurred outside.
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.
static std::shared_ptr< FilterContents > MakeDirectionalMorphology(FilterInput::Ref input, Radius radius, Vector2 direction, MorphType morph_type)
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
Matrix GetTransform(const Matrix &parent_transform) const
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
static std::shared_ptr< FilterContents > MakeMorphology(FilterInput::Ref input, Radius radius_x, Radius radius_y, MorphType morph_type)
static std::shared_ptr< FilterContents > MakeBorderMaskBlur(FilterInput::Ref input, Sigma sigma_x, Sigma sigma_y, BlurStyle blur_style=BlurStyle::kNormal)
static std::shared_ptr< FilterContents > MakeLocalMatrixFilter(FilterInput::Ref input, const Matrix &matrix)
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 Geometry *mask_geometry=nullptr)
std::optional< Snapshot > RenderToSnapshot(const ContentContext &renderer, const Entity &entity, const SnapshotOptions &options) const override
Render this contents to a snapshot, respecting the entity's transform, path, clip depth,...
void SetInputs(FilterInput::Vector inputs)
The input texture sources for this filter. Each input's emitted texture is expected to have premultip...
virtual void SetRenderingMode(Entity::RenderingMode rendering_mode)
Marks this filter chain as applying in a subpass scenario.
std::optional< Rect > GetSourceCoverage(const Matrix &effect_transform, const Rect &output_limit) const
Determines the coverage of source pixels that will be needed to produce results for the specified |ou...
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...
static std::shared_ptr< FilterContents > MakeMatrixFilter(FilterInput::Ref input, const Matrix &matrix, const SamplerDescriptor &desc)
static std::shared_ptr< FilterContents > MakeYUVToRGBFilter(std::shared_ptr< Texture > y_texture, std::shared_ptr< Texture > uv_texture, YUVColorSpace yuv_color_space)
static std::shared_ptr< FilterContents > MakeRuntimeEffect(FilterInput::Ref input, std::shared_ptr< RuntimeStage > runtime_stage, std::shared_ptr< std::vector< uint8_t >> uniforms, std::vector< RuntimeEffectContents::TextureInput > texture_inputs)
virtual Matrix GetLocalTransform(const Matrix &parent_transform) const
std::shared_ptr< FilterInput > Ref
Definition: filter_input.h:32
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:33
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
YUVColorSpace
Definition: color.h:54
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
For convolution filters, the "radius" is the size of the convolution kernel to use on the local space...
Definition: sigma.h:48
In filters that use Gaussian distributions, "sigma" is a size of one standard deviation in terms of t...
Definition: sigma.h:32