Flutter Impeller
impeller::SolidRRectBlurContents Class Referencefinal

Draws a fast solid color blur of an rounded rectangle. Only supports RRects with fully symmetrical radii. Also produces correct results for rectangles (corner_radius=0) and circles (corner_radius=width/2=height/2). More...

#include <solid_rrect_blur_contents.h>

Inheritance diagram for impeller::SolidRRectBlurContents:
impeller::Contents

Public Member Functions

 SolidRRectBlurContents ()
 
 ~SolidRRectBlurContents () override
 
void SetRRect (std::optional< Rect > rect, Size corner_radii={})
 
void SetSigma (Sigma sigma)
 
void SetColor (Color color)
 
Color GetColor () const
 
std::optional< RectGetCoverage (const Entity &entity) const override
 Get the area of the render pass that will be affected when this contents is rendered. More...
 
bool Render (const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
 
bool ApplyColorFilter (const ColorFilterProc &color_filter_proc) override
 If possible, applies a color filter to this contents inputs on the CPU. More...
 
- Public Member Functions inherited from impeller::Contents
 Contents ()
 
virtual ~Contents ()
 
void SetCoverageHint (std::optional< Rect > coverage_hint)
 Hint that specifies the coverage area of this Contents that will actually be used during rendering. This is for optimization purposes only and can not be relied on as a clip. May optionally affect the result of GetCoverage(). More...
 
const std::optional< Rect > & GetCoverageHint () const
 
virtual bool IsOpaque (const Matrix &transform) const
 Whether this Contents only emits opaque source colors from the fragment stage. This value does not account for any entity properties (e.g. the blend mode), clips/visibility culling, or inherited opacity. More...
 
virtual std::optional< SnapshotRenderToSnapshot (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="Snapshot") const
 Render this contents to a snapshot, respecting the entity's transform, path, clip depth, and blend mode. The result texture size is always the size of GetCoverage(entity). More...
 
std::optional< SizeGetColorSourceSize () const
 Return the color source's intrinsic size, if available. More...
 
void SetColorSourceSize (Size size)
 
virtual void SetInheritedOpacity (Scalar opacity)
 Inherit the provided opacity. More...
 
virtual std::optional< ColorAsBackgroundColor (const Entity &entity, ISize target_size) const
 Returns a color if this Contents will flood the given target_size with a color. This output color is the "Source" color that will be used for the Entity's blend operation. More...
 

Additional Inherited Members

- Public Types inherited from impeller::Contents
using ColorFilterProc = std::function< Color(Color)>
 
using RenderProc = std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)>
 
using CoverageProc = std::function< std::optional< Rect >(const Entity &entity)>
 
- Static Public Member Functions inherited from impeller::Contents
static std::shared_ptr< ContentsMakeAnonymous (RenderProc render_proc, CoverageProc coverage_proc)
 

Detailed Description

Draws a fast solid color blur of an rounded rectangle. Only supports RRects with fully symmetrical radii. Also produces correct results for rectangles (corner_radius=0) and circles (corner_radius=width/2=height/2).

Definition at line 25 of file solid_rrect_blur_contents.h.

Constructor & Destructor Documentation

◆ SolidRRectBlurContents()

impeller::SolidRRectBlurContents::SolidRRectBlurContents ( )
default

◆ ~SolidRRectBlurContents()

impeller::SolidRRectBlurContents::~SolidRRectBlurContents ( )
overridedefault

Member Function Documentation

◆ ApplyColorFilter()

bool impeller::SolidRRectBlurContents::ApplyColorFilter ( const ColorFilterProc color_filter_proc)
overridevirtual

If possible, applies a color filter to this contents inputs on the CPU.

This method will either fully apply the color filter or perform no action. Partial/incorrect application of the color filter will never occur.

Parameters
[in]color_filter_procA function that filters a given unpremultiplied color to produce a new unpremultiplied color.
Returns
True if the color filter was able to be fully applied to all all relevant inputs. Otherwise, this operation is a no-op and false is returned.

Reimplemented from impeller::Contents.

Definition at line 196 of file solid_rrect_blur_contents.cc.

197  {
198  color_ = color_filter_proc(color_);
199  return true;
200 }

◆ GetColor()

Color impeller::SolidRRectBlurContents::GetColor ( ) const

Definition at line 47 of file solid_rrect_blur_contents.cc.

47  {
48  return color_;
49 }

◆ GetCoverage()

std::optional< Rect > impeller::SolidRRectBlurContents::GetCoverage ( const Entity entity) const
overridevirtual

Get the area of the render pass that will be affected when this contents is rendered.

During rendering, coverage coordinates count pixels from the top left corner of the framebuffer.

Returns
The coverage rectangle. An std::nullopt result means that rendering this contents has no effect on the output color.

Implements impeller::Contents.

Definition at line 110 of file solid_rrect_blur_contents.cc.

111  {
112  if (!rect_.has_value()) {
113  return std::nullopt;
114  }
115 
116  Scalar radius = PadForSigma(sigma_.sigma);
117 
118  return rect_->Expand(radius).TransformBounds(entity.GetTransform());
119 }
float Scalar
Definition: scalar.h:18
Scalar sigma
Definition: sigma.h:33

References impeller::Entity::GetTransform(), and impeller::Sigma::sigma.

◆ Render()

bool impeller::SolidRRectBlurContents::Render ( const ContentContext renderer,
const Entity entity,
RenderPass pass 
) const
overridevirtual

Implements impeller::Contents.

Definition at line 121 of file solid_rrect_blur_contents.cc.

123  {
124  if (!rect_.has_value()) {
125  return true;
126  }
127 
130 
131  Matrix basis_invert = entity.GetTransform().Basis().Invert();
132  Vector2 max_sigmas =
133  Vector2((basis_invert * Vector2(500.f, 0.f)).GetLength(),
134  (basis_invert * Vector2(0.f, 500.f)).GetLength());
135  Scalar max_sigma = std::min(max_sigmas.x, max_sigmas.y);
136  // Clamp the max kernel width/height to 1000 (@ 2x) to limit the extent
137  // of the blur and to kEhCloseEnough to prevent NaN calculations
138  // trying to evaluate a Gaussian distribution with a sigma of 0.
139  auto blur_sigma = std::clamp(sigma_.sigma, kEhCloseEnough, max_sigma);
140  // Increase quality by making the radius a bit bigger than the typical
141  // sigma->radius conversion we use for slower blurs.
142  Scalar blur_radius = PadForSigma(blur_sigma);
143  Rect positive_rect = rect_->GetPositive();
144  Scalar left = -blur_radius;
145  Scalar top = -blur_radius;
146  Scalar right = positive_rect.GetWidth() + blur_radius;
147  Scalar bottom = positive_rect.GetHeight() + blur_radius;
148 
149  std::array<VS::PerVertexData, 4> vertices = {
150  VS::PerVertexData{Point(left, top)},
151  VS::PerVertexData{Point(right, top)},
152  VS::PerVertexData{Point(left, bottom)},
153  VS::PerVertexData{Point(right, bottom)},
154  };
155 
156  ContentContextOptions opts = OptionsFromPassAndEntity(pass, entity);
157  opts.primitive_type = PrimitiveType::kTriangleStrip;
158  Color color = color_;
159  if (entity.GetBlendMode() == BlendMode::kClear) {
160  opts.is_for_rrect_blur_clear = true;
161  color = Color::White();
162  }
163 
164  VS::FrameInfo frame_info;
165  frame_info.mvp = Entity::GetShaderTransform(
166  entity.GetShaderClipDepth(), pass,
167  entity.GetTransform() *
168  Matrix::MakeTranslation(positive_rect.GetOrigin()));
169 
170  FS::FragInfo frag_info;
171  frag_info.color = color;
172  Scalar radius = std::min(std::clamp(corner_radii_.width, kEhCloseEnough,
173  positive_rect.GetWidth() * 0.5f),
174  std::clamp(corner_radii_.height, kEhCloseEnough,
175  positive_rect.GetHeight() * 0.5f));
176  if (!SetupFragInfo(frag_info, blur_sigma, positive_rect.GetCenter(),
177  Point(positive_rect.GetSize()), radius)) {
178  return true;
179  }
180 
181  auto& host_buffer = renderer.GetTransientsBuffer();
182  pass.SetCommandLabel("RRect Shadow");
183  pass.SetPipeline(renderer.GetRRectBlurPipeline(opts));
184  pass.SetVertexBuffer(CreateVertexBuffer(vertices, host_buffer));
185 
186  VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
187  FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
188 
189  if (!pass.Draw().ok()) {
190  return false;
191  }
192 
193  return true;
194 }
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:48
FragmentShader_ FragmentShader
Definition: pipeline.h:120
Vector2 blur_radius
Blur radius in source pixels based on scaled_sigma.
Point Vector2
Definition: point.h:331
constexpr float kEhCloseEnough
Definition: constants.h:57
TRect< Scalar > Rect
Definition: rect.h:792
TPoint< Scalar > Point
Definition: point.h:327
LinePipeline::FragmentShader FS
VertexBuffer CreateVertexBuffer(std::array< VertexType, size > input, HostBuffer &host_buffer)
Create an index-less vertex buffer from a fixed size array.
static bool SetupFragInfo(RRectBlurPipeline::FragmentShader::FragInfo &frag_info, Scalar blurSigma, Point center, Point rSize, Scalar radius)
LinePipeline::VertexShader VS
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
static constexpr Color White()
Definition: color.h:264
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
Type height
Definition: size.h:29
Type width
Definition: size.h:28

References impeller::Matrix::Basis(), blur_radius, impeller::CreateVertexBuffer(), impeller::RenderPass::Draw(), impeller::Entity::GetBlendMode(), impeller::TRect< T >::GetCenter(), impeller::TRect< T >::GetHeight(), impeller::TRect< T >::GetOrigin(), impeller::ContentContext::GetRRectBlurPipeline(), impeller::Entity::GetShaderClipDepth(), impeller::Entity::GetShaderTransform(), impeller::TRect< T >::GetSize(), impeller::Entity::GetTransform(), impeller::ContentContext::GetTransientsBuffer(), impeller::TRect< T >::GetWidth(), impeller::TSize< T >::height, impeller::Matrix::Invert(), impeller::ContentContextOptions::is_for_rrect_blur_clear, impeller::kClear, impeller::kEhCloseEnough, impeller::kTriangleStrip, impeller::Matrix::MakeTranslation(), impeller::OptionsFromPassAndEntity(), impeller::ContentContextOptions::primitive_type, impeller::RenderPass::SetCommandLabel(), impeller::RenderPass::SetPipeline(), impeller::SetupFragInfo(), impeller::RenderPass::SetVertexBuffer(), impeller::Sigma::sigma, impeller::Color::White(), impeller::TSize< T >::width, impeller::TPoint< T >::x, and impeller::TPoint< T >::y.

◆ SetColor()

void impeller::SolidRRectBlurContents::SetColor ( Color  color)

Definition at line 43 of file solid_rrect_blur_contents.cc.

43  {
44  color_ = color.Premultiply();
45 }
constexpr Color Premultiply() const
Definition: color.h:212

References impeller::Color::Premultiply().

◆ SetRRect()

void impeller::SolidRRectBlurContents::SetRRect ( std::optional< Rect rect,
Size  corner_radii = {} 
)

Definition at line 33 of file solid_rrect_blur_contents.cc.

34  {
35  rect_ = rect;
36  corner_radii_ = corner_radii;
37 }

◆ SetSigma()

void impeller::SolidRRectBlurContents::SetSigma ( Sigma  sigma)

Definition at line 39 of file solid_rrect_blur_contents.cc.

39  {
40  sigma_ = sigma;
41 }

The documentation for this class was generated from the following files: