Flutter Impeller
filter_contents.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 <algorithm>
8 #include <cmath>
9 #include <cstddef>
10 #include <memory>
11 #include <optional>
12 #include <tuple>
13 #include <utility>
14 
15 #include "flutter/fml/logging.h"
16 #include "impeller/core/formats.h"
27 #include "impeller/entity/entity.h"
32 
33 namespace impeller {
34 
37 
38 std::shared_ptr<FilterContents> FilterContents::MakeGaussianBlur(
39  const FilterInput::Ref& input,
40  Sigma sigma_x,
41  Sigma sigma_y,
42  Entity::TileMode tile_mode,
43  FilterContents::BlurStyle mask_blur_style,
44  const Geometry* mask_geometry) {
45  auto blur = std::make_shared<GaussianBlurFilterContents>(
46  sigma_x.sigma, sigma_y.sigma, tile_mode, mask_blur_style, mask_geometry);
47  blur->SetInputs({input});
48  return blur;
49 }
50 
51 std::shared_ptr<FilterContents> FilterContents::MakeBorderMaskBlur(
52  FilterInput::Ref input,
53  Sigma sigma_x,
54  Sigma sigma_y,
55  BlurStyle blur_style) {
56  auto filter = std::make_shared<BorderMaskBlurFilterContents>();
57  filter->SetInputs({std::move(input)});
58  filter->SetSigma(sigma_x, sigma_y);
59  filter->SetBlurStyle(blur_style);
60  return filter;
61 }
62 
63 std::shared_ptr<FilterContents> FilterContents::MakeDirectionalMorphology(
64  FilterInput::Ref input,
65  Radius radius,
66  Vector2 direction,
67  MorphType morph_type) {
68  auto filter = std::make_shared<DirectionalMorphologyFilterContents>();
69  filter->SetInputs({std::move(input)});
70  filter->SetRadius(radius);
71  filter->SetDirection(direction);
72  filter->SetMorphType(morph_type);
73  return filter;
74 }
75 
76 std::shared_ptr<FilterContents> FilterContents::MakeMorphology(
77  FilterInput::Ref input,
78  Radius radius_x,
79  Radius radius_y,
80  MorphType morph_type) {
81  auto x_morphology = MakeDirectionalMorphology(std::move(input), radius_x,
82  Point(1, 0), morph_type);
83  auto y_morphology = MakeDirectionalMorphology(
84  FilterInput::Make(x_morphology), radius_y, Point(0, 1), morph_type);
85  return y_morphology;
86 }
87 
88 std::shared_ptr<FilterContents> FilterContents::MakeMatrixFilter(
89  FilterInput::Ref input,
90  const Matrix& matrix,
91  const SamplerDescriptor& desc) {
92  auto filter = std::make_shared<MatrixFilterContents>();
93  filter->SetInputs({std::move(input)});
94  filter->SetMatrix(matrix);
95  filter->SetSamplerDescriptor(desc);
96  return filter;
97 }
98 
99 std::shared_ptr<FilterContents> FilterContents::MakeLocalMatrixFilter(
100  FilterInput::Ref input,
101  const Matrix& matrix) {
102  auto filter = std::make_shared<LocalMatrixFilterContents>();
103  filter->SetInputs({std::move(input)});
104  filter->SetMatrix(matrix);
105  return filter;
106 }
107 
108 std::shared_ptr<FilterContents> FilterContents::MakeYUVToRGBFilter(
109  std::shared_ptr<Texture> y_texture,
110  std::shared_ptr<Texture> uv_texture,
111  YUVColorSpace yuv_color_space) {
112  auto filter = std::make_shared<impeller::YUVToRGBFilterContents>();
113  filter->SetInputs({impeller::FilterInput::Make(y_texture),
114  impeller::FilterInput::Make(uv_texture)});
115  filter->SetYUVColorSpace(yuv_color_space);
116  return filter;
117 }
118 
119 std::shared_ptr<FilterContents> FilterContents::MakeRuntimeEffect(
120  FilterInput::Ref input,
121  std::shared_ptr<RuntimeStage> runtime_stage,
122  std::shared_ptr<std::vector<uint8_t>> uniforms,
123  std::vector<RuntimeEffectContents::TextureInput> texture_inputs) {
124  auto filter = std::make_shared<impeller::RuntimeEffectFilterContents>();
125  filter->SetInputs({std::move(input)});
126  filter->SetRuntimeStage(std::move(runtime_stage));
127  filter->SetUniforms(std::move(uniforms));
128  filter->SetTextureInputs(std::move(texture_inputs));
129  return filter;
130 }
131 
133 
135 
137  inputs_ = std::move(inputs);
138 }
139 
140 void FilterContents::SetEffectTransform(const Matrix& effect_transform) {
141  effect_transform_ = effect_transform;
142 
143  for (auto& input : inputs_) {
144  input->SetEffectTransform(effect_transform);
145  }
146 }
147 
149  const Entity& entity,
150  RenderPass& pass) const {
151  auto filter_coverage = GetCoverage(entity);
152  if (!filter_coverage.has_value()) {
153  return true;
154  }
155 
156  // Run the filter.
157 
158  auto maybe_entity = GetEntity(renderer, entity, GetCoverageHint());
159  if (!maybe_entity.has_value()) {
160  return true;
161  }
162  maybe_entity->SetClipDepth(entity.GetClipDepth());
163  return maybe_entity->Render(renderer, pass);
164 }
165 
166 std::optional<Rect> FilterContents::GetLocalCoverage(
167  const Entity& local_entity) const {
168  auto coverage = GetFilterCoverage(inputs_, local_entity, effect_transform_);
169  auto coverage_hint = GetCoverageHint();
170  if (coverage_hint.has_value() && coverage.has_value()) {
171  coverage = coverage->Intersection(coverage_hint.value());
172  }
173 
174  return coverage;
175 }
176 
177 std::optional<Rect> FilterContents::GetCoverage(const Entity& entity) const {
178  Entity entity_with_local_transform = entity.Clone();
179  entity_with_local_transform.SetTransform(GetTransform(entity.GetTransform()));
180 
181  return GetLocalCoverage(entity_with_local_transform);
182 }
183 
184 std::optional<Rect> FilterContents::GetFilterCoverage(
185  const FilterInput::Vector& inputs,
186  const Entity& entity,
187  const Matrix& effect_transform) const {
188  // The default coverage of FilterContents is just the union of its inputs'
189  // coverage. FilterContents implementations may choose to adjust this
190  // coverage depending on the use case.
191 
192  if (inputs_.empty()) {
193  return std::nullopt;
194  }
195 
196  std::optional<Rect> result;
197  for (const auto& input : inputs) {
198  auto coverage = input->GetCoverage(entity);
199  if (!coverage.has_value()) {
200  continue;
201  }
202  if (!result.has_value()) {
203  result = coverage;
204  continue;
205  }
206  result = result->Union(coverage.value());
207  }
208  return result;
209 }
210 
212  const Matrix& effect_transform,
213  const Rect& output_limit) const {
214  auto filter_input_coverage =
215  GetFilterSourceCoverage(effect_transform_, output_limit);
216 
217  if (!filter_input_coverage.has_value()) {
218  return std::nullopt;
219  }
220 
221  std::optional<Rect> inputs_coverage;
222  for (const auto& input : inputs_) {
223  auto input_coverage = input->GetSourceCoverage(
224  effect_transform, filter_input_coverage.value());
225  if (!input_coverage.has_value()) {
226  return std::nullopt;
227  }
228  inputs_coverage = Rect::Union(inputs_coverage, input_coverage.value());
229  }
230  return inputs_coverage;
231 }
232 
233 std::optional<Entity> FilterContents::GetEntity(
234  const ContentContext& renderer,
235  const Entity& entity,
236  const std::optional<Rect>& coverage_hint) const {
237  Entity entity_with_local_transform = entity.Clone();
238  entity_with_local_transform.SetTransform(GetTransform(entity.GetTransform()));
239 
240  auto coverage = GetLocalCoverage(entity_with_local_transform);
241  if (!coverage.has_value() || coverage->IsEmpty()) {
242  return std::nullopt;
243  }
244 
245  return RenderFilter(inputs_, renderer, entity_with_local_transform,
246  effect_transform_, coverage.value(), coverage_hint);
247 }
248 
249 std::optional<Snapshot> FilterContents::RenderToSnapshot(
250  const ContentContext& renderer,
251  const Entity& entity,
252  std::optional<Rect> coverage_limit,
253  const std::optional<SamplerDescriptor>& sampler_descriptor,
254  bool msaa_enabled,
255  int32_t mip_count,
256  std::string_view label) const {
257  // Resolve the render instruction (entity) from the filter and render it to a
258  // snapshot.
259  if (std::optional<Entity> result =
260  GetEntity(renderer, entity, coverage_limit);
261  result.has_value()) {
262  return result->GetContents()->RenderToSnapshot(
263  renderer, // renderer
264  result.value(), // entity
265  coverage_limit, // coverage_limit
266  std::nullopt, // sampler_descriptor
267  true, // msaa_enabled
268  /*mip_count=*/mip_count,
269  label // label
270  );
271  }
272 
273  return std::nullopt;
274 }
275 
276 Matrix FilterContents::GetLocalTransform(const Matrix& parent_transform) const {
277  return Matrix();
278 }
279 
280 Matrix FilterContents::GetTransform(const Matrix& parent_transform) const {
281  return parent_transform * GetLocalTransform(parent_transform);
282 }
283 
285  for (auto& input : inputs_) {
286  input->SetRenderingMode(rendering_mode);
287  }
288 }
289 
290 } // namespace impeller
const std::optional< Rect > & GetCoverageHint() const
Definition: contents.cc:140
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition: entity.cc:60
Entity Clone() const
Definition: entity.cc:158
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
uint32_t GetClipDepth() const
Definition: entity.cc:84
static const int32_t kBlurFilterRequiredMipCount
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)
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)
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, int32_t mip_count=1, std::string_view label="Filter Snapshot") const override
Render this contents to a snapshot, respecting the entity's transform, path, clip depth,...
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
static FilterInput::Ref Make(Variant input, bool msaa_enabled=true)
Definition: filter_input.cc:19
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
TPoint< Scalar > Point
Definition: point.h:327
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
Scalar sigma
Definition: sigma.h:33
constexpr TRect Union(const TRect &o) const
Definition: rect.h:517