| 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
5 #include "impeller/entity/contents/filters/border_mask_blur_filter_contents.h"
6 #include "impeller/entity/contents/content_context.h"
7
8 #include "impeller/entity/contents/anonymous_contents.h"
9 #include "impeller/entity/contents/contents.h"
10 #include "impeller/renderer/render_pass.h"
11 #include "impeller/renderer/vertex_buffer_builder.h"
12
13 namespace impeller {
14
15 BorderMaskBlurFilterContents::BorderMaskBlurFilterContents() = default;
16
17 BorderMaskBlurFilterContents::~BorderMaskBlurFilterContents() = default;
18
19 void BorderMaskBlurFilterContents::SetSigma(Sigma sigma_x, Sigma sigma_y) {
20 sigma_x_ = sigma_x;
21 sigma_y_ = sigma_y;
22 }
23
24 void BorderMaskBlurFilterContents::SetBlurStyle(BlurStyle blur_style) {
25 blur_style_ = blur_style;
26
27 switch (blur_style) {
28 case FilterContents::BlurStyle::kNormal:
29 src_color_factor_ = false;
30 inner_blur_factor_ = true;
31 outer_blur_factor_ = true;
32 break;
33 case FilterContents::BlurStyle::kSolid:
34 src_color_factor_ = true;
35 inner_blur_factor_ = false;
36 outer_blur_factor_ = true;
37 break;
38 case FilterContents::BlurStyle::kOuter:
39 src_color_factor_ = false;
40 inner_blur_factor_ = false;
41 outer_blur_factor_ = true;
42 break;
43 case FilterContents::BlurStyle::kInner:
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 {
58 using VS = BorderMaskBlurPipeline::VertexShader;
59 using FS = BorderMaskBlurPipeline::FragmentShader;
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 auto origin = coverage.GetOrigin();
96 auto size = coverage.GetSize();
97 vtx_builder.AddVertices({
98 {origin, input_uvs[0]},
99 {{origin.x + size.width, origin.y}, input_uvs[1]},
100 {{origin.x, origin.y + size.height}, input_uvs[2]},
101 {{origin.x + size.width, origin.y + size.height}, input_uvs[3]},
102 });
103
104 Command cmd;
105 DEBUG_COMMAND_INFO(cmd, "Border Mask Blur Filter");
106 auto options = OptionsFromPassAndEntity(pass, entity);
107 options.primitive_type = PrimitiveType::kTriangleStrip;
108
109 cmd.pipeline = renderer.GetBorderMaskBlurPipeline(options);
110 cmd.BindVertices(vtx_builder.CreateVertexBuffer(host_buffer));
111 cmd.stencil_reference = entity.GetClipDepth();
112
113 VS::FrameInfo frame_info;
114 frame_info.mvp = pass.GetOrthographicTransform() * entity.GetTransform();
115 frame_info.texture_sampler_y_coord_scale =
116 input_snapshot->texture->GetYCoordScale();
117
118 FS::FragInfo frag_info;
119 frag_info.sigma_uv = sigma.Abs() / input_snapshot->texture->GetSize();
120 frag_info.src_factor = src_color_factor;
121 frag_info.inner_blur_factor = inner_blur_factor;
122 frag_info.outer_blur_factor = outer_blur_factor;
123
124 FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
125 VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
126
127 auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
128 FS::BindTextureSampler(cmd, input_snapshot->texture, sampler);
129
130 return pass.AddCommand(std::move(cmd));
131 };
132
133 CoverageProc coverage_proc =
134 [coverage](const Entity& entity) -> std::optional<Rect> {
135 return coverage.TransformBounds(entity.GetTransform());
136 };
137
138 auto contents = AnonymousContents::Make(render_proc, coverage_proc);
139
140 Entity sub_entity;
141 sub_entity.SetContents(std::move(contents));
142 sub_entity.SetClipDepth(entity.GetClipDepth());
143 sub_entity.SetBlendMode(entity.GetBlendMode());
144 return sub_entity;
145 }
146
147 std::optional<Rect> BorderMaskBlurFilterContents::GetFilterCoverage(
148 const FilterInput::Vector& inputs,
149 const Entity& entity,
150 const Matrix& effect_transform) const {
151 if (inputs.empty()) {
152 return std::nullopt;
153 }
154
155 auto coverage = inputs[0]->GetCoverage(entity);
156 if (!coverage.has_value()) {
157 return std::nullopt;
158 }
159 auto transform = inputs[0]->GetTransform(entity) * effect_transform;
160 auto transformed_blur_vector =
161 transform.TransformDirection(Vector2(Radius{sigma_x_}.radius, 0)).Abs() +
162 transform.TransformDirection(Vector2(0, Radius{sigma_y_}.radius)).Abs();
163 return coverage->Expand(transformed_blur_vector);
164 }
165
166 std::optional<Rect> BorderMaskBlurFilterContents::GetFilterSourceCoverage(
167 const Matrix& effect_transform,
168 const Rect& output_limit) const {
169 auto transformed_blur_vector =
170 effect_transform.TransformDirection(Vector2(Radius{sigma_x_}.radius, 0))
171 .Abs() +
172 effect_transform.TransformDirection(Vector2(0, Radius{sigma_y_}.radius))
173 .Abs();
174 return output_limit.Expand(transformed_blur_vector);
175 }
176
177 } // namespace impeller
impeller::Sigma::sigma
Scalar sigma
Definition: sigma.h:33
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
contents.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:23
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:264
border_mask_blur_filter_contents.h
impeller::Vector2
Point Vector2
Definition: point.h:312
impeller::BorderMaskBlurFilterContents::SetBlurStyle
void SetBlurStyle(BlurStyle blur_style)
Definition: border_mask_blur_filter_contents.cc:24
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:93
impeller::BorderMaskBlurFilterContents::~BorderMaskBlurFilterContents
~BorderMaskBlurFilterContents() override
impeller::FilterContents::BlurStyle::kNormal
@ kNormal
Blurred inside and outside.
impeller::TRect::GetOrigin
constexpr TPoint< Type > GetOrigin() const
Returns the upper left corner of the rectangle as specified when it was constructed.
Definition: rect.h:154
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:28
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
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:48
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:32
impeller::FilterContents::BlurStyle::kInner
@ kInner
Blurred inside, nothing outside.
anonymous_contents.h
impeller::Matrix::TransformDirection
constexpr Vector4 TransformDirection(const Vector4 &v) const
Definition: matrix.h:433
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:49
impeller::TRect::GetSize
constexpr TSize< Type > GetSize() const
Returns the size of the rectangle as specified when it was constructed and which may be negative in e...
Definition: rect.h:159
impeller::Contents::CoverageProc
std::function< std::optional< Rect >(const Entity &entity)> CoverageProc
Definition: contents.h:50
impeller::FilterInput::Vector
std::vector< FilterInput::Ref > Vector
Definition: filter_input.h:33
impeller::BorderMaskBlurFilterContents::GetFilterCoverage
std::optional< Rect > GetFilterCoverage(const FilterInput::Vector &inputs, const Entity &entity, const Matrix &effect_transform) const override
Internal utility method for |GetLocalCoverage| that computes the output coverage of this filter acros...
Definition: border_mask_blur_filter_contents.cc:147
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:92
impeller
Definition: aiks_context.cc:10
impeller::BorderMaskBlurFilterContents::GetFilterSourceCoverage
std::optional< Rect > GetFilterSourceCoverage(const Matrix &effect_transform, const Rect &output_limit) const override
Internal utility method for |GetSourceCoverage| that computes the inverse effect of this transform on...
Definition: border_mask_blur_filter_contents.cc:166
impeller::ContentContext
Definition: content_context.h:332
impeller::TRect
Definition: rect.h:22
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::AnonymousContents::Make
static std::shared_ptr< Contents > Make(RenderProc render_proc, CoverageProc coverage_proc)
Definition: anonymous_contents.cc:11
impeller::TRect::Expand
constexpr TRect< T > Expand(T left, T top, T right, T bottom) const
Returns a rectangle with expanded edges. Negative expansion results in shrinking.
Definition: rect.h:373
vertex_buffer_builder.h