| 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/path.h"
12 #include "impeller/geometry/path_builder.h"
13 #include "impeller/renderer/render_pass.h"
14 #include "impeller/renderer/vertex_buffer_builder.h"
15 #include "impeller/tessellator/tessellator.h"
16
17 namespace impeller {
18
19 SolidRRectBlurContents::SolidRRectBlurContents() = default;
20
21 SolidRRectBlurContents::~SolidRRectBlurContents() = default;
22
23 void SolidRRectBlurContents::SetRRect(std::optional<Rect> rect,
24 Scalar corner_radius) {
25 rect_ = rect;
26 corner_radius_ = corner_radius;
27 }
28
29 void SolidRRectBlurContents::SetSigma(Sigma sigma) {
30 sigma_ = sigma;
31 }
32
33 void SolidRRectBlurContents::SetColor(Color color) {
34 color_ = color.Premultiply();
35 }
36
37 Color SolidRRectBlurContents::GetColor() const {
38 return color_;
39 }
40
41 std::optional<Rect> SolidRRectBlurContents::GetCoverage(
42 const Entity& entity) const {
43 if (!rect_.has_value()) {
44 return std::nullopt;
45 }
46
47 Scalar radius = sigma_.sigma * 2;
48
49 auto ltrb = rect_->GetLTRB();
50 Rect bounds = Rect::MakeLTRB(ltrb[0] - radius, ltrb[1] - radius,
51 ltrb[2] + radius, ltrb[3] + radius);
52 return bounds.TransformBounds(entity.GetTransformation());
53 };
54
55 bool SolidRRectBlurContents::Render(const ContentContext& renderer,
56 const Entity& entity,
57 RenderPass& pass) const {
58 if (!rect_.has_value()) {
59 return true;
60 }
61
62 using VS = RRectBlurPipeline::VertexShader;
63 using FS = RRectBlurPipeline::FragmentShader;
64
65 VertexBufferBuilder<VS::PerVertexData> vtx_builder;
66
67 // Clamp the max kernel width/height to 1000.
68 auto blur_sigma = std::min(sigma_.sigma, 250.0f);
69 // Increase quality by make the radius a bit bigger than the typical
70 // sigma->radius conversion we use for slower blurs.
71 auto blur_radius = blur_sigma * 2;
72 auto positive_rect = rect_->GetPositive();
73 {
74 auto left = -blur_radius;
75 auto top = -blur_radius;
76 auto right = positive_rect.size.width + blur_radius;
77 auto bottom = positive_rect.size.height + blur_radius;
78
79 vtx_builder.AddVertices({
80 {Point(left, top)},
81 {Point(right, top)},
82 {Point(left, bottom)},
83 {Point(left, bottom)},
84 {Point(right, top)},
85 {Point(right, bottom)},
86 });
87 }
88
89 Command cmd;
90 DEBUG_COMMAND_INFO(cmd, "RRect Shadow");
91 ContentContextOptions opts = OptionsFromPassAndEntity(pass, entity);
92 opts.primitive_type = PrimitiveType::kTriangle;
93 Color color = color_;
94 if (entity.GetBlendMode() == BlendMode::kClear) {
95 opts.is_for_rrect_blur_clear = true;
96 color = Color::White();
97 }
98 cmd.pipeline = renderer.GetRRectBlurPipeline(opts);
99 cmd.stencil_reference = entity.GetStencilDepth();
100
101 cmd.BindVertices(vtx_builder.CreateVertexBuffer(pass.GetTransientsBuffer()));
102
103 VS::FrameInfo frame_info;
104 frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
105 entity.GetTransformation() *
106 Matrix::MakeTranslation({positive_rect.origin});
107 VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
108
109 FS::FragInfo frag_info;
110 frag_info.color = color;
111 frag_info.blur_sigma = blur_sigma;
112 frag_info.rect_size = Point(positive_rect.size);
113 frag_info.corner_radius =
114 std::min(corner_radius_, std::min(positive_rect.size.width / 2.0f,
115 positive_rect.size.height / 2.0f));
116 FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info));
117
118 if (!pass.AddCommand(std::move(cmd))) {
119 return false;
120 }
121
122 return true;
123 }
124
125 bool SolidRRectBlurContents::ApplyColorFilter(
126 const ColorFilterProc& color_filter_proc) {
127 color_ = color_filter_proc(color_);
128 return true;
129 }
130
131 } // 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:89
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:37
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:29
impeller::SolidRRectBlurContents::SetColor
void SetColor(Color color)
Definition: solid_rrect_blur_contents.cc:33
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:125
impeller::Entity::GetBlendMode
BlendMode GetBlendMode() const
Definition: entity.cc:101
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
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:55
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:41
impeller::SolidRRectBlurContents::SetRRect
void SetRRect(std::optional< Rect > rect, Scalar corner_radius=0)
Definition: solid_rrect_blur_contents.cc:23
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