Flutter Impeller
solid_rrect_blur_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 #include <optional>
7 
14 
15 namespace impeller {
16 
17 namespace {
18 // Generous padding to make sure blurs with large sigmas are fully visible. Used
19 // to expand the geometry around the rrect. Larger sigmas have more subtle
20 // gradients so they need larger padding to avoid hard cutoffs. Sigma is
21 // maximized to 3.5 since that should cover 99.95% of all samples. 3.0 should
22 // cover 99.7% but that was seen to be not enough for large sigmas.
23 Scalar PadForSigma(Scalar sigma) {
24  Scalar scalar = std::min((1.0f / 47.6f) * sigma + 2.5f, 3.5f);
25  return sigma * scalar;
26 }
27 } // namespace
28 
30 
32 
33 void SolidRRectBlurContents::SetRRect(std::optional<Rect> rect,
34  Size corner_radii) {
35  rect_ = rect;
36  corner_radii_ = corner_radii;
37 }
38 
40  sigma_ = sigma;
41 }
42 
44  color_ = color.Premultiply();
45 }
46 
48  return color_;
49 }
50 
52  const Entity& entity) const {
53  if (!rect_.has_value()) {
54  return std::nullopt;
55  }
56 
57  Scalar radius = PadForSigma(sigma_.sigma);
58 
59  return rect_->Expand(radius).TransformBounds(entity.GetTransform());
60 }
61 
63  const Entity& entity,
64  RenderPass& pass) const {
65  if (!rect_.has_value()) {
66  return true;
67  }
68 
71 
73 
74  // Clamp the max kernel width/height to 1000 to limit the extent
75  // of the blur and to kEhCloseEnough to prevent NaN calculations
76  // trying to evaluate a Guassian distribution with a sigma of 0.
77  auto blur_sigma = std::clamp(sigma_.sigma, kEhCloseEnough, 250.0f);
78  // Increase quality by making the radius a bit bigger than the typical
79  // sigma->radius conversion we use for slower blurs.
80  auto blur_radius = PadForSigma(blur_sigma);
81  auto positive_rect = rect_->GetPositive();
82  {
83  auto left = -blur_radius;
84  auto top = -blur_radius;
85  auto right = positive_rect.GetWidth() + blur_radius;
86  auto bottom = positive_rect.GetHeight() + blur_radius;
87 
88  vtx_builder.AddVertices({
89  {Point(left, top)},
90  {Point(right, top)},
91  {Point(left, bottom)},
92  {Point(right, bottom)},
93  });
94  }
95 
98  Color color = color_;
99  if (entity.GetBlendMode() == BlendMode::kClear) {
100  opts.is_for_rrect_blur_clear = true;
101  color = Color::White();
102  }
103 
104  VS::FrameInfo frame_info;
105  frame_info.mvp = Entity::GetShaderTransform(
106  entity.GetShaderClipDepth(), pass,
107  entity.GetTransform() *
108  Matrix::MakeTranslation(positive_rect.GetOrigin()));
109 
110  FS::FragInfo frag_info;
111  frag_info.color = color;
112  frag_info.blur_sigma = blur_sigma;
113  frag_info.rect_size = Point(positive_rect.GetSize());
114  frag_info.corner_radii = {std::clamp(corner_radii_.width, kEhCloseEnough,
115  positive_rect.GetWidth() * 0.5f),
116  std::clamp(corner_radii_.height, kEhCloseEnough,
117  positive_rect.GetHeight() * 0.5f)};
118 
119  pass.SetCommandLabel("RRect Shadow");
120  pass.SetPipeline(renderer.GetRRectBlurPipeline(opts));
121  pass.SetVertexBuffer(
122  vtx_builder.CreateVertexBuffer(renderer.GetTransientsBuffer()));
123  VS::BindFrameInfo(pass,
124  renderer.GetTransientsBuffer().EmplaceUniform(frame_info));
125  FS::BindFragInfo(pass,
126  renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
127 
128  if (!pass.Draw().ok()) {
129  return false;
130  }
131 
132  return true;
133 }
134 
136  const ColorFilterProc& color_filter_proc) {
137  color_ = color_filter_proc(color_);
138  return true;
139 }
140 
141 } // namespace impeller
impeller::Sigma::sigma
Scalar sigma
Definition: sigma.h:33
impeller::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:107
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:50
impeller::Entity::GetShaderClipDepth
float GetShaderClipDepth() const
Definition: entity.cc:106
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
entity.h
impeller::Color
Definition: color.h:124
impeller::SolidRRectBlurContents::GetColor
Color GetColor() const
Definition: solid_rrect_blur_contents.cc:47
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:56
impeller::SolidRRectBlurContents::SetSigma
void SetSigma(Sigma sigma)
Definition: solid_rrect_blur_contents.cc:39
impeller::SolidRRectBlurContents::SetColor
void SetColor(Color color)
Definition: solid_rrect_blur_contents.cc:43
impeller::RenderPass::SetVertexBuffer
virtual bool SetVertexBuffer(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: render_pass.cc:123
impeller::VertexBufferBuilder::AddVertices
VertexBufferBuilder & AddVertices(std::initializer_list< VertexType_ > vertices)
Definition: vertex_buffer_builder.h:67
impeller::SolidRRectBlurContents::SolidRRectBlurContents
SolidRRectBlurContents()
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
impeller::RenderPass::SetCommandLabel
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
impeller::RenderPass::Draw
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:127
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::Entity
Definition: entity.h:20
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
impeller::TSize< Scalar >
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::Point
TPoint< Scalar > Point
Definition: point.h:322
render_pass.h
impeller::BlendMode::kClear
@ kClear
impeller::VertexBufferBuilder
Definition: vertex_buffer_builder.h:21
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:106
impeller::Color::White
static constexpr Color White()
Definition: color.h:266
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::ContentContext::GetRRectBlurPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetRRectBlurPipeline(ContentContextOptions opts) const
Definition: content_context.h:427
impeller::ContentContextOptions::is_for_rrect_blur_clear
bool is_for_rrect_blur_clear
Definition: content_context.h:316
impeller::SolidRRectBlurContents::ApplyColorFilter
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
Definition: solid_rrect_blur_contents.cc:135
impeller::TSize::width
Type width
Definition: size.h:22
impeller::Entity::GetBlendMode
BlendMode GetBlendMode() const
Definition: entity.cc:119
impeller::ContentContextOptions::primitive_type
PrimitiveType primitive_type
Definition: content_context.h:311
impeller::VertexBufferBuilder::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(HostBuffer &host_buffer) const
Definition: vertex_buffer_builder.h:81
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
content_context.h
constants.h
impeller::SolidRRectBlurContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: solid_rrect_blur_contents.cc:62
impeller::HostBuffer::EmplaceUniform
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:50
blur_radius
Vector2 blur_radius
Blur radius in source pixels based on scaled_sigma.
Definition: gaussian_blur_filter_contents.cc:89
impeller::SolidRRectBlurContents::SetRRect
void SetRRect(std::optional< Rect > rect, Size corner_radii={})
Definition: solid_rrect_blur_contents.cc:33
color.h
color
DlColor color
Definition: dl_golden_blur_unittests.cc:23
impeller::TSize::height
Type height
Definition: size.h:23
impeller::RenderPass::SetPipeline
virtual void SetPipeline(const std::shared_ptr< Pipeline< PipelineDescriptor >> &pipeline)
The pipeline to use for this command.
Definition: render_pass.cc:92
impeller::SolidRRectBlurContents::GetCoverage
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.
Definition: solid_rrect_blur_contents.cc:51
impeller::ContentContextOptions
Definition: content_context.h:254
impeller
Definition: aiks_blend_unittests.cc:18
solid_rrect_blur_contents.h
impeller::ContentContext
Definition: content_context.h:366
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:752
vertex_buffer_builder.h
impeller::SolidRRectBlurContents::~SolidRRectBlurContents
~SolidRRectBlurContents() override