Flutter Impeller
border_mask_blur_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 
7 
12 
13 namespace impeller {
14 
16 
18 
20  sigma_x_ = sigma_x;
21  sigma_y_ = sigma_y;
22 }
23 
25  blur_style_ = blur_style;
26 
27  switch (blur_style) {
29  src_color_factor_ = false;
30  inner_blur_factor_ = true;
31  outer_blur_factor_ = true;
32  break;
34  src_color_factor_ = true;
35  inner_blur_factor_ = false;
36  outer_blur_factor_ = true;
37  break;
39  src_color_factor_ = false;
40  inner_blur_factor_ = false;
41  outer_blur_factor_ = true;
42  break;
44  src_color_factor_ = false;
45  inner_blur_factor_ = true;
46  outer_blur_factor_ = false;
47  break;
48  }
49 }
50 
51 std::optional<Entity> BorderMaskBlurFilterContents::RenderFilter(
52  const FilterInput::Vector& inputs,
53  const ContentContext& renderer,
54  const Entity& entity,
55  const Matrix& effect_transform,
56  const Rect& coverage,
57  const std::optional<Rect>& coverage_hint) const {
60 
61  //----------------------------------------------------------------------------
62  /// Handle inputs.
63  ///
64 
65  if (inputs.empty()) {
66  return std::nullopt;
67  }
68 
69  auto input_snapshot =
70  inputs[0]->GetSnapshot("BorderMaskBlur", renderer, entity);
71  if (!input_snapshot.has_value()) {
72  return std::nullopt;
73  }
74 
75  auto maybe_input_uvs = input_snapshot->GetCoverageUVs(coverage);
76  if (!maybe_input_uvs.has_value()) {
77  return std::nullopt;
78  }
79  auto input_uvs = maybe_input_uvs.value();
80 
81  //----------------------------------------------------------------------------
82  /// Create AnonymousContents for rendering.
83  ///
84 
85  auto sigma = effect_transform * Vector2(sigma_x_.sigma, sigma_y_.sigma);
86  RenderProc render_proc = [coverage, input_snapshot, input_uvs = input_uvs,
87  src_color_factor = src_color_factor_,
88  inner_blur_factor = inner_blur_factor_,
89  outer_blur_factor = outer_blur_factor_, sigma](
90  const ContentContext& renderer,
91  const Entity& entity, RenderPass& pass) -> bool {
92  auto& host_buffer = pass.GetTransientsBuffer();
93 
94  VertexBufferBuilder<VS::PerVertexData> vtx_builder;
95  vtx_builder.AddVertices({
96  {coverage.origin, input_uvs[0]},
97  {{coverage.origin.x + coverage.size.width, coverage.origin.y},
98  input_uvs[1]},
99  {{coverage.origin.x + coverage.size.width,
100  coverage.origin.y + coverage.size.height},
101  input_uvs[3]},
102  {coverage.origin, input_uvs[0]},
103  {{coverage.origin.x + coverage.size.width,
104  coverage.origin.y + coverage.size.height},
105  input_uvs[3]},
106  {{coverage.origin.x, coverage.origin.y + coverage.size.height},
107  input_uvs[2]},
108  });
109  auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
110 
111  Command cmd;
112  DEBUG_COMMAND_INFO(cmd, "Border Mask Blur Filter");
113  auto options = OptionsFromPassAndEntity(pass, entity);
114 
115  cmd.pipeline = renderer.GetBorderMaskBlurPipeline(options);
116  cmd.BindVertices(vtx_buffer);
117  cmd.stencil_reference = entity.GetStencilDepth();
118 
119  VS::FrameInfo frame_info;
120  frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
121  entity.GetTransformation();
122  frame_info.texture_sampler_y_coord_scale =
123  input_snapshot->texture->GetYCoordScale();
124 
125  FS::FragInfo frag_info;
126  frag_info.sigma_uv = sigma.Abs() / input_snapshot->texture->GetSize();
127  frag_info.src_factor = src_color_factor;
128  frag_info.inner_blur_factor = inner_blur_factor;
129  frag_info.outer_blur_factor = outer_blur_factor;
130 
131  FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
132  VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
133 
134  auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
135  FS::BindTextureSampler(cmd, input_snapshot->texture, sampler);
136 
137  return pass.AddCommand(std::move(cmd));
138  };
139 
140  CoverageProc coverage_proc =
141  [coverage](const Entity& entity) -> std::optional<Rect> {
142  return coverage.TransformBounds(entity.GetTransformation());
143  };
144 
145  auto contents = AnonymousContents::Make(render_proc, coverage_proc);
146 
147  Entity sub_entity;
148  sub_entity.SetContents(std::move(contents));
149  sub_entity.SetStencilDepth(entity.GetStencilDepth());
150  sub_entity.SetBlendMode(entity.GetBlendMode());
151  return sub_entity;
152 }
153 
155  const FilterInput::Vector& inputs,
156  const Entity& entity,
157  const Matrix& effect_transform) const {
158  if (inputs.empty()) {
159  return std::nullopt;
160  }
161 
162  auto coverage = inputs[0]->GetCoverage(entity);
163  if (!coverage.has_value()) {
164  return std::nullopt;
165  }
166  auto transform = inputs[0]->GetTransform(entity) * effect_transform;
167  auto transformed_blur_vector =
168  transform.TransformDirection(Vector2(Radius{sigma_x_}.radius, 0)).Abs() +
169  transform.TransformDirection(Vector2(0, Radius{sigma_y_}.radius)).Abs();
170  auto extent = coverage->size + transformed_blur_vector * 2;
171  return Rect(coverage->origin - transformed_blur_vector,
172  Size(extent.x, extent.y));
173 }
174 
175 } // namespace impeller
impeller::Sigma::sigma
Scalar sigma
Definition: sigma.h:32
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:31
contents.h
impeller::TRect::size
TSize< Type > size
Definition: rect.h:24
impeller::TPoint::y
Type y
Definition: point.h:24
sampler_library.h
impeller::BorderMaskBlurFilterContents::SetSigma
void SetSigma(Sigma sigma_x, Sigma sigma_y)
Definition: border_mask_blur_filter_contents.cc:19
impeller::FilterContents::BlurStyle
BlurStyle
Definition: filter_contents.h:21
impeller::BorderMaskBlurFilterContents::BorderMaskBlurFilterContents
BorderMaskBlurFilterContents()
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:204
border_mask_blur_filter_contents.h
impeller::Vector2
Point Vector2
Definition: point.h:310
impeller::BorderMaskBlurFilterContents::SetBlurStyle
void SetBlurStyle(BlurStyle blur_style)
Definition: border_mask_blur_filter_contents.cc:24
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:91
impeller::BorderMaskBlurFilterContents::~BorderMaskBlurFilterContents
~BorderMaskBlurFilterContents() override
impeller::FilterContents::BlurStyle::kNormal
@ kNormal
Blurred inside and outside.
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:30
impeller::TSize
Definition: size.h:18
render_pass.h
impeller::Radius
For convolution filters, the "radius" is the size of the convolution kernel to use on the local space...
Definition: sigma.h:47
impeller::FilterContents::BlurStyle::kSolid
@ kSolid
Solid inside, blurred outside.
impeller::Sigma
In filters that use Gaussian distributions, "sigma" is a size of one standard deviation in terms of t...
Definition: sigma.h:31
impeller::TRect::origin
TPoint< Type > origin
Definition: rect.h:23
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:306
impeller::FilterContents::BlurStyle::kInner
@ kInner
Blurred inside, nothing outside.
impeller::TSize::width
Type width
Definition: size.h:21
anonymous_contents.h
impeller::TPoint::x
Type x
Definition: point.h:23
impeller::Matrix::TransformDirection
constexpr Vector4 TransformDirection(const Vector4 &v) const
Definition: matrix.h:431
content_context.h
impeller::FilterContents::BlurStyle::kOuter
@ kOuter
Nothing inside, blurred outside.
impeller::Contents::RenderProc
std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)> RenderProc
Definition: contents.h:48
impeller::Matrix::MakeOrthographic
static constexpr Matrix MakeOrthographic(TSize< T > size)
Definition: matrix.h:448
impeller::TSize::height
Type height
Definition: size.h:22
impeller::Contents::CoverageProc
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition: contents.h:49
impeller::FilterInput::Vector
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:32
impeller::BorderMaskBlurFilterContents::GetFilterCoverage
std::optional< Rect > GetFilterCoverage(const FilterInput::Vector &inputs, const Entity &entity, const Matrix &effect_transform) const override
Definition: border_mask_blur_filter_contents.cc:154
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:90
impeller
Definition: aiks_context.cc:10
impeller::ContentContext
Definition: content_context.h:344
impeller::TRect
Definition: rect.h:20
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:36
impeller::AnonymousContents::Make
static std::shared_ptr< Contents > Make(RenderProc render_proc, CoverageProc coverage_proc)
Definition: anonymous_contents.cc:11