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 
51 static Point eccentricity(Point v, double sInverse) {
52  Point vOverS = v * sInverse * 0.5;
53  Point vOverS_squared = -(vOverS * vOverS);
54  return {std::exp(vOverS_squared.x), std::exp(vOverS_squared.y)};
55 }
56 
57 static Scalar kTwoOverSqrtPi = 2.0 / std::sqrt(kPi);
58 
59 // use crate::math::compute_erf7;
61  x *= kTwoOverSqrtPi;
62  float xx = x * x;
63  x = x + (0.24295 + (0.03395 + 0.0104 * xx) * xx) * (x * xx);
64  return x / sqrt(1.0 + x * x);
65 }
66 
67 static Point NegPos(Scalar v) {
68  return {std::min(v, 0.0f), std::max(v, 0.0f)};
69 }
70 
71 static void SetupFragInfo(
72  RRectBlurPipeline::FragmentShader::FragInfo& frag_info,
73  Scalar blurSigma,
74  Point center,
75  Point rSize,
76  Scalar radius) {
77  Scalar sigma = std::max(blurSigma * kSqrt2, 1.f);
78 
79  frag_info.center = rSize * 0.5f;
80  frag_info.minEdge = std::min(rSize.x, rSize.y);
81  double rMax = 0.5 * frag_info.minEdge;
82  double r0 = std::min(std::hypot(radius, sigma * 1.15), rMax);
83  frag_info.r1 = std::min(std::hypot(radius, sigma * 2.0), rMax);
84 
85  frag_info.exponent = 2.0 * frag_info.r1 / r0;
86 
87  frag_info.sInv = 1.0 / sigma;
88 
89  // Pull in long end (make less eccentric).
90  Point eccentricV = eccentricity(rSize, frag_info.sInv);
91  double delta = 1.25 * sigma * (eccentricV.x - eccentricV.y);
92  rSize += NegPos(delta);
93 
94  frag_info.adjust = rSize * 0.5 - frag_info.r1;
95  frag_info.exponentInv = 1.0 / frag_info.exponent;
96  frag_info.scale =
97  0.5 * computeErf7(frag_info.sInv * 0.5 *
98  (std::max(rSize.x, rSize.y) - 0.5 * radius));
99 }
100 
102  const Entity& entity) const {
103  if (!rect_.has_value()) {
104  return std::nullopt;
105  }
106 
107  Scalar radius = PadForSigma(sigma_.sigma);
108 
109  return rect_->Expand(radius).TransformBounds(entity.GetTransform());
110 }
111 
113  const Entity& entity,
114  RenderPass& pass) const {
115  if (!rect_.has_value()) {
116  return true;
117  }
118 
121 
122  // Clamp the max kernel width/height to 1000 to limit the extent
123  // of the blur and to kEhCloseEnough to prevent NaN calculations
124  // trying to evaluate a Guassian distribution with a sigma of 0.
125  Scalar blur_sigma = std::clamp(sigma_.sigma, kEhCloseEnough, 250.0f);
126  // Increase quality by making the radius a bit bigger than the typical
127  // sigma->radius conversion we use for slower blurs.
128  Scalar blur_radius = PadForSigma(blur_sigma);
129  Rect positive_rect = rect_->GetPositive();
130  Scalar left = -blur_radius;
131  Scalar top = -blur_radius;
132  Scalar right = positive_rect.GetWidth() + blur_radius;
133  Scalar bottom = positive_rect.GetHeight() + blur_radius;
134 
135  std::array<VS::PerVertexData, 4> vertices = {
136  VS::PerVertexData{Point(left, top)},
137  VS::PerVertexData{Point(right, top)},
138  VS::PerVertexData{Point(left, bottom)},
139  VS::PerVertexData{Point(right, bottom)},
140  };
141 
142  ContentContextOptions opts = OptionsFromPassAndEntity(pass, entity);
144  Color color = color_;
145  if (entity.GetBlendMode() == BlendMode::kClear) {
146  opts.is_for_rrect_blur_clear = true;
147  color = Color::White();
148  }
149 
150  VS::FrameInfo frame_info;
151  frame_info.mvp = Entity::GetShaderTransform(
152  entity.GetShaderClipDepth(), pass,
153  entity.GetTransform() *
154  Matrix::MakeTranslation(positive_rect.GetOrigin()));
155 
156  FS::FragInfo frag_info;
157  frag_info.color = color;
158  Scalar radius = std::min(std::clamp(corner_radii_.width, kEhCloseEnough,
159  positive_rect.GetWidth() * 0.5f),
160  std::clamp(corner_radii_.height, kEhCloseEnough,
161  positive_rect.GetHeight() * 0.5f));
162  SetupFragInfo(frag_info, blur_sigma, positive_rect.GetCenter(),
163  Point(positive_rect.GetSize()), radius);
164  auto& host_buffer = renderer.GetTransientsBuffer();
165  pass.SetCommandLabel("RRect Shadow");
166  pass.SetPipeline(renderer.GetRRectBlurPipeline(opts));
167  pass.SetVertexBuffer(CreateVertexBuffer(vertices, host_buffer));
168 
169  VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
170  FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
171 
172  if (!pass.Draw().ok()) {
173  return false;
174  }
175 
176  return true;
177 }
178 
180  const ColorFilterProc& color_filter_proc) {
181  color_ = color_filter_proc(color_);
182  return true;
183 }
184 
185 } // 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:51
impeller::Entity::GetShaderClipDepth
float GetShaderClipDepth() const
Definition: entity.cc:99
impeller::TPoint::y
Type y
Definition: point.h:31
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::kSqrt2
constexpr float kSqrt2
Definition: constants.h:47
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:47
entity.h
impeller::Color
Definition: color.h:123
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::kPi
constexpr float kPi
Definition: constants.h:26
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::computeErf7
static Scalar computeErf7(Scalar x)
Definition: solid_rrect_blur_contents.cc:60
impeller::SolidRRectBlurContents::SolidRRectBlurContents
SolidRRectBlurContents()
impeller::TRect::GetCenter
constexpr Point GetCenter() const
Get the center point as a |Point|.
Definition: rect.h:376
impeller::TRect::GetHeight
constexpr Type GetHeight() const
Returns the height of the rectangle, equivalent to |GetSize().height|.
Definition: rect.h:341
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::TRect::GetOrigin
constexpr TPoint< Type > GetOrigin() const
Returns the upper left corner of the rectangle as specified by the left/top or x/y values when it was...
Definition: rect.h:314
impeller::RenderPass::Draw
virtual fml::Status Draw()
Record the currently pending command.
Definition: render_pass.cc:127
impeller::SetupFragInfo
static void SetupFragInfo(RRectBlurPipeline::FragmentShader::FragInfo &frag_info, Scalar blurSigma, Point center, Point rSize, Scalar radius)
Definition: solid_rrect_blur_contents.cc:71
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:327
render_pass.h
impeller::NegPos
static Point NegPos(Scalar v)
Definition: solid_rrect_blur_contents.cc:67
impeller::BlendMode::kClear
@ kClear
impeller::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(std::array< VertexType, size > input, HostBuffer &host_buffer)
Create an index-less vertex buffer from a fixed size array.
Definition: vertex_buffer_builder.h:24
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
impeller::TRect::GetWidth
constexpr Type GetWidth() const
Returns the width of the rectangle, equivalent to |GetSize().width|.
Definition: rect.h:335
impeller::kTwoOverSqrtPi
static Scalar kTwoOverSqrtPi
Definition: solid_rrect_blur_contents.cc:57
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:106
impeller::Color::White
static constexpr Color White()
Definition: color.h:263
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:423
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:179
impeller::TSize::width
Type width
Definition: size.h:22
impeller::Entity::GetBlendMode
BlendMode GetBlendMode() const
Definition: entity.cc:112
impeller::TPoint::x
Type x
Definition: point.h:30
impeller::ContentContextOptions::primitive_type
PrimitiveType primitive_type
Definition: content_context.h:311
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
impeller::TRect::GetSize
constexpr TSize< Type > GetSize() const
Returns the size of the rectangle which may be negative in either width or height and may have been c...
Definition: rect.h:321
constants.h
impeller::TPoint
Definition: point.h:27
impeller::SolidRRectBlurContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: solid_rrect_blur_contents.cc:112
blur_radius
Vector2 blur_radius
Blur radius in source pixels based on scaled_sigma.
Definition: gaussian_blur_filter_contents.cc:83
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:24
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::eccentricity
static Point eccentricity(Point v, double sInverse)
Definition: solid_rrect_blur_contents.cc:51
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:101
impeller::ContentContextOptions
Definition: content_context.h:254
impeller
Definition: allocation.cc:12
solid_rrect_blur_contents.h
impeller::ContentContext
Definition: content_context.h:366
impeller::TRect
Definition: rect.h:122
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:753
vertex_buffer_builder.h
impeller::SolidRRectBlurContents::~SolidRRectBlurContents
~SolidRRectBlurContents() override