| 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
5 #include "impeller/entity/contents/solid_rrect_blur_contents.h"
6 #include <optional>
7
8 #include "impeller/entity/contents/content_context.h"
9 #include "impeller/entity/entity.h"
10 #include "impeller/geometry/color.h"
11 #include "impeller/geometry/constants.h"
12 #include "impeller/geometry/path.h"
13 #include "impeller/geometry/path_builder.h"
14 #include "impeller/renderer/render_pass.h"
15 #include "impeller/renderer/vertex_buffer_builder.h"
16 #include "impeller/tessellator/tessellator.h"
17
18 namespace impeller {
19
20 SolidRRectBlurContents::SolidRRectBlurContents() = default;
21
22 SolidRRectBlurContents::~SolidRRectBlurContents() = default;
23
24 void SolidRRectBlurContents::SetRRect(std::optional<Rect> rect,
25 Scalar corner_radius) {
26 rect_ = rect;
27 corner_radius_ = corner_radius;
28 }
29
30 void SolidRRectBlurContents::SetSigma(Sigma sigma) {
31 sigma_ = sigma;
32 }
33
34 void SolidRRectBlurContents::SetColor(Color color) {
35 color_ = color.Premultiply();
36 }
37
38 Color SolidRRectBlurContents::GetColor() const {
39 return color_;
40 }
41
42 std::optional<Rect> SolidRRectBlurContents::GetCoverage(
43 const Entity& entity) const {
44 if (!rect_.has_value()) {
45 return std::nullopt;
46 }
47
48 Scalar radius = sigma_.sigma * 2;
49
50 auto ltrb = rect_->GetLTRB();
51 Rect bounds = Rect::MakeLTRB(ltrb[0] - radius, ltrb[1] - radius,
52 ltrb[2] + radius, ltrb[3] + radius);
53 return bounds.TransformBounds(entity.GetTransformation());
54 };
55
56 bool SolidRRectBlurContents::Render(const ContentContext& renderer,
57 const Entity& entity,
58 RenderPass& pass) const {
59 // Early return if sigma is close to zero to avoid rendering NaNs.
60 if (!rect_.has_value() || std::fabs(sigma_.sigma) <= kEhCloseEnough) {
61 return true;
62 }
63
64 using VS = RRectBlurPipeline::VertexShader;
65 using FS = RRectBlurPipeline::FragmentShader;
66
67 VertexBufferBuilder<VS::PerVertexData> vtx_builder;
68
69 // Clamp the max kernel width/height to 1000.
70 auto blur_sigma = std::min(sigma_.sigma, 250.0f);
71 // Increase quality by make the radius a bit bigger than the typical
72 // sigma->radius conversion we use for slower blurs.
73 auto blur_radius = blur_sigma * 2;
74 auto positive_rect = rect_->GetPositive();
75 {
76 auto left = -blur_radius;
77 auto top = -blur_radius;
78 auto right = positive_rect.size.width + blur_radius;
79 auto bottom = positive_rect.size.height + blur_radius;
80
81 vtx_builder.AddVertices({
82 {Point(left, top)},
83 {Point(right, top)},
84 {Point(left, bottom)},
85 {Point(left, bottom)},
86 {Point(right, top)},
87 {Point(right, bottom)},
88 });
89 }
90
91 Command cmd;
92 DEBUG_COMMAND_INFO(cmd, "RRect Shadow");
93 ContentContextOptions opts = OptionsFromPassAndEntity(pass, entity);
94 opts.primitive_type = PrimitiveType::kTriangle;
95 Color color = color_;
96 if (entity.GetBlendMode() == BlendMode::kClear) {
97 opts.is_for_rrect_blur_clear = true;
98 color = Color::White();
99 }
100 cmd.pipeline = renderer.GetRRectBlurPipeline(opts);
101 cmd.stencil_reference = entity.GetStencilDepth();
102
103 cmd.BindVertices(vtx_builder.CreateVertexBuffer(pass.GetTransientsBuffer()));
104
105 VS::FrameInfo frame_info;
106 frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
107 entity.GetTransformation() *
108 Matrix::MakeTranslation({positive_rect.origin});
109 VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
110
111 FS::FragInfo frag_info;
112 frag_info.color = color;
113 frag_info.blur_sigma = blur_sigma;
114 frag_info.rect_size = Point(positive_rect.size);
115 frag_info.corner_radius =
116 std::min(corner_radius_, std::min(positive_rect.size.width / 2.0f,
117 positive_rect.size.height / 2.0f));
118 FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
119
120 if (!pass.AddCommand(std::move(cmd))) {
121 return false;
122 }
123
124 return true;
125 }
126
127 bool SolidRRectBlurContents::ApplyColorFilter(
128 const ColorFilterProc& color_filter_proc) {
129 color_ = color_filter_proc(color_);
130 return true;
131 }
132
133 } // namespace impeller
impeller::Sigma::sigma
Scalar sigma
Definition: sigma.h:32
path.h
impeller::Command
An object used to specify work to the GPU along with references to resources the GPU will used when d...
Definition: command.h:99
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:31
impeller::Entity::GetStencilDepth
uint32_t GetStencilDepth() const
Definition: entity.cc:93
impeller::Scalar
float Scalar
Definition: scalar.h:15
entity.h
impeller::Color
Definition: color.h:122
impeller::SolidRRectBlurContents::GetColor
Color GetColor() const
Definition: solid_rrect_blur_contents.cc:38
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:55
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:204
impeller::SolidRRectBlurContents::SetSigma
void SetSigma(Sigma sigma)
Definition: solid_rrect_blur_contents.cc:30
impeller::SolidRRectBlurContents::SetColor
void SetColor(Color color)
Definition: solid_rrect_blur_contents.cc:34
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:91
impeller::VertexBufferBuilder::AddVertices
VertexBufferBuilder & AddVertices(std::initializer_list< VertexType_ > vertices)
Definition: vertex_buffer_builder.h:64
impeller::SolidRRectBlurContents::SolidRRectBlurContents
SolidRRectBlurContents()
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:94
impeller::Entity::GetTransformation
const Matrix & GetTransformation() const
Definition: entity.cc:49
tessellator.h
path_builder.h
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:30
impeller::RenderPass::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_pass.cc:30
impeller::Point
TPoint< Scalar > Point
Definition: point.h:306
render_pass.h
impeller::VertexBufferBuilder
Definition: vertex_buffer_builder.h:23
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:37
impeller::Color::White
static constexpr Color White()
Definition: color.h:254
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::ContentContext::GetRRectBlurPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetRRectBlurPipeline(ContentContextOptions opts) const
Definition: content_context.h:407
impeller::ContentContextOptions::is_for_rrect_blur_clear
bool is_for_rrect_blur_clear
Definition: content_context.h:311
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:127
impeller::Entity::GetBlendMode
BlendMode GetBlendMode() const
Definition: entity.cc:105
impeller::ContentContextOptions::primitive_type
PrimitiveType primitive_type
Definition: content_context.h:307
impeller::VertexBufferBuilder::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(HostBuffer &host_buffer) const
Definition: vertex_buffer_builder.h:78
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:27
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:152
content_context.h
constants.h
impeller::BlendMode::kClear
@ kClear
impeller::SolidRRectBlurContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: solid_rrect_blur_contents.cc:56
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:43
impeller::Matrix::MakeOrthographic
static constexpr Matrix MakeOrthographic(TSize< T > size)
Definition: matrix.h:448
color.h
impeller::PrimitiveType::kTriangle
@ kTriangle
impeller::TRect< Scalar >::MakeLTRB
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:40
impeller::SolidRRectBlurContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the screen space bounding rectangle that this contents affects.
Definition: solid_rrect_blur_contents.cc:42
impeller::SolidRRectBlurContents::SetRRect
void SetRRect(std::optional< Rect > rect, Scalar corner_radius=0)
Definition: solid_rrect_blur_contents.cc:24
impeller::Command::BindVertices
bool BindVertices(const VertexBuffer &buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
impeller::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:103
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:90
impeller::ContentContextOptions
Definition: content_context.h:302
impeller::RenderPass::AddCommand
bool AddCommand(Command &&command)
Record a command for subsequent encoding to the underlying command buffer. No work is encoded into th...
Definition: render_pass.cc:46
impeller
Definition: aiks_context.cc:10
solid_rrect_blur_contents.h
impeller::ContentContext
Definition: content_context.h:344
impeller::Color::Premultiply
constexpr Color Premultiply() const
Definition: color.h:212
impeller::TRect
Definition: rect.h:20
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:34
vertex_buffer_builder.h
impeller::SolidRRectBlurContents::~SolidRRectBlurContents
~SolidRRectBlurContents() override