| Flutter Impeller |

solid_color_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 "solid_color_contents.h"
6
7 #include "impeller/entity/contents/clip_contents.h"
8 #include "impeller/entity/contents/content_context.h"
9 #include "impeller/entity/entity.h"
10 #include "impeller/geometry/path.h"
11 #include "impeller/renderer/render_pass.h"
12
13 namespace impeller {
14
15 SolidColorContents::SolidColorContents() = default;
16
17 SolidColorContents::~SolidColorContents() = default;
18
19 void SolidColorContents::SetColor(Color color) {
20 color_ = color;
21 }
22
23 Color SolidColorContents::GetColor() const {
24 return color_.WithAlpha(color_.alpha * GetOpacityFactor());
25 }
26
27 bool SolidColorContents::IsSolidColor() const {
28 return true;
29 }
30
31 bool SolidColorContents::IsOpaque() const {
32 return GetColor().IsOpaque();
33 }
34
35 std::optional<Rect> SolidColorContents::GetCoverage(
36 const Entity& entity) const {
37 if (GetColor().IsTransparent()) {
38 return std::nullopt;
39 }
40
41 auto geometry = GetGeometry();
42 if (geometry == nullptr) {
43 return std::nullopt;
44 }
45 return geometry->GetCoverage(entity.GetTransform());
46 };
47
48 bool SolidColorContents::Render(const ContentContext& renderer,
49 const Entity& entity,
50 RenderPass& pass) const {
51 auto capture = entity.GetCapture().CreateChild("SolidColorContents");
52
53 using VS = SolidFillPipeline::VertexShader;
54
55 Command cmd;
56 DEBUG_COMMAND_INFO(cmd, "Solid Fill");
57 cmd.stencil_reference = entity.GetClipDepth();
58
59 auto geometry_result =
60 GetGeometry()->GetPositionBuffer(renderer, entity, pass);
61
62 auto options = OptionsFromPassAndEntity(pass, entity);
63 if (geometry_result.prevent_overdraw) {
64 options.stencil_compare = CompareFunction::kEqual;
65 options.stencil_operation = StencilOperation::kIncrementClamp;
66 }
67
68 options.primitive_type = geometry_result.type;
69 cmd.pipeline = renderer.GetSolidFillPipeline(options);
70 cmd.BindVertices(std::move(geometry_result.vertex_buffer));
71
72 VS::FrameInfo frame_info;
73 frame_info.mvp = capture.AddMatrix("Transform", geometry_result.transform);
74 frame_info.color = capture.AddColor("Color", GetColor()).Premultiply();
75 VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
76
77 if (!pass.AddCommand(std::move(cmd))) {
78 return false;
79 }
80
81 if (geometry_result.prevent_overdraw) {
82 auto restore = ClipRestoreContents();
83 restore.SetRestoreCoverage(GetCoverage(entity));
84 return restore.Render(renderer, entity, pass);
85 }
86 return true;
87 }
88
89 std::unique_ptr<SolidColorContents> SolidColorContents::Make(Path path,
90 Color color) {
91 auto contents = std::make_unique<SolidColorContents>();
92 contents->SetGeometry(Geometry::MakeFillPath(std::move(path)));
93 contents->SetColor(color);
94 return contents;
95 }
96
97 std::optional<Color> SolidColorContents::AsBackgroundColor(
98 const Entity& entity,
99 ISize target_size) const {
100 Rect target_rect = Rect::MakeSize(target_size);
101 return GetGeometry()->CoversArea(entity.GetTransform(), target_rect)
102 ? GetColor()
103 : std::optional<Color>();
104 }
105
106 bool SolidColorContents::ApplyColorFilter(
107 const ColorFilterProc& color_filter_proc) {
108 color_ = color_filter_proc(color_);
109 return true;
110 }
111
112 } // namespace impeller
impeller::SolidColorContents::GetColor
Color GetColor() const
Definition: solid_color_contents.cc:23
path.h
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:29
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:92
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
impeller::SolidColorContents::IsOpaque
bool IsOpaque() const override
Whether this Contents only emits opaque source colors from the fragment stage. This value does not ac...
Definition: solid_color_contents.cc:31
impeller::Entity::GetCapture
Capture & GetCapture() const
Definition: entity.cc:177
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:49
entity.h
solid_color_contents.h
impeller::Color
Definition: color.h:124
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
impeller::SolidColorContents::IsSolidColor
bool IsSolidColor() const override
Definition: solid_color_contents.cc:27
impeller::StencilOperation::kIncrementClamp
@ kIncrementClamp
Increment the current stencil value by 1. Clamp it to the maximum.
impeller::SolidColorContents::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_color_contents.cc:35
impeller::Color::alpha
Scalar alpha
Definition: color.h:143
impeller::ColorSourceContents::GetGeometry
const std::shared_ptr< Geometry > & GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:21
impeller::SolidColorContents::~SolidColorContents
~SolidColorContents() override
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:28
impeller::TSize< int64_t >
render_pass.h
impeller::SolidColorContents::ApplyColorFilter
bool ApplyColorFilter(const ColorFilterProc &color_filter_proc) override
If possible, applies a color filter to this contents inputs on the CPU.
Definition: solid_color_contents.cc:106
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:55
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:38
impeller::Color::WithAlpha
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:270
impeller::Command::BindVertices
bool BindVertices(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
impeller::ContentContext::GetSolidFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetSolidFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:405
impeller::SolidColorContents::SolidColorContents
SolidColorContents()
clip_contents.h
impeller::ClipRestoreContents
Definition: clip_contents.h:60
impeller::Geometry::MakeFillPath
static std::shared_ptr< Geometry > MakeFillPath(Path path, std::optional< Rect > inner_rect=std::nullopt)
Definition: geometry.cc:150
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:29
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:93
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:122
content_context.h
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:44
impeller::SolidColorContents::SetColor
void SetColor(Color color)
Definition: solid_color_contents.cc:19
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:41
impeller::Capture::CreateChild
Capture CreateChild(std::string_view label)
Definition: capture.h:239
impeller::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:96
impeller::SolidColorContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: solid_color_contents.cc:48
impeller::Color::IsOpaque
constexpr bool IsOpaque() const
Definition: color.h:886
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:92
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:67
impeller
Definition: aiks_context.cc:10
impeller::SolidColorContents::Make
static std::unique_ptr< SolidColorContents > Make(Path path, Color color)
Definition: solid_color_contents.cc:89
impeller::ContentContext
Definition: content_context.h:332
impeller::TRect< Scalar >
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:55
impeller::SolidColorContents::AsBackgroundColor
std::optional< Color > AsBackgroundColor(const Entity &entity, ISize target_size) const override
Returns a color if this Contents will flood the given target_size with a color. This output color is ...
Definition: solid_color_contents.cc:97