| 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/content_context.h"
8 #include "impeller/entity/entity.h"
9 #include "impeller/entity/geometry/geometry.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 Matrix& transform) const {
32 return GetColor().IsOpaque() && !AppliesAlphaForStrokeCoverage(transform);
33 }
34
35 std::optional<Rect> SolidColorContents::GetCoverage(
36 const Entity& entity) const {
37 if (GetColor().IsTransparent()) {
38 return std::nullopt;
39 }
40
41 const Geometry* 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 using VS = SolidFillPipeline::VertexShader;
52 using FS = SolidFillPipeline::FragmentShader;
53 auto& host_buffer = renderer.GetTransientsBuffer();
54
55 VS::FrameInfo frame_info;
56 FS::FragInfo frag_info;
57 frag_info.color = GetColor().Premultiply() *
58 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
59
60 PipelineBuilderCallback pipeline_callback =
61 [&renderer](ContentContextOptions options) {
62 return renderer.GetSolidFillPipeline(options);
63 };
64 return ColorSourceContents::DrawGeometry<VS>(
65 renderer, entity, pass, pipeline_callback, frame_info,
66 [&frag_info, &host_buffer](RenderPass& pass) {
67 FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
68 pass.SetCommandLabel("Solid Fill");
69 return true;
70 });
71 }
72
73 std::optional<Color> SolidColorContents::AsBackgroundColor(
74 const Entity& entity,
75 ISize target_size) const {
76 Rect target_rect = Rect::MakeSize(target_size);
77 return GetGeometry()->CoversArea(entity.GetTransform(), target_rect)
78 ? GetColor()
79 : std::optional<Color>();
80 }
81
82 bool SolidColorContents::ApplyColorFilter(
83 const ColorFilterProc& color_filter_proc) {
84 color_ = color_filter_proc(color_);
85 return true;
86 }
87
88 } // namespace impeller
impeller::SolidColorContents::GetColor
Color GetColor() const
Definition: solid_color_contents.cc:23
impeller::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:107
impeller::ColorSourceContents::PipelineBuilderCallback
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
Definition: color_source_contents.h:108
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:28
impeller::Geometry::ComputeAlphaCoverage
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:124
geometry.h
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:47
entity.h
solid_color_contents.h
impeller::Color
Definition: color.h:123
impeller::Geometry::GetCoverage
virtual std::optional< Rect > GetCoverage(const Matrix &transform) const =0
impeller::SolidColorContents::IsSolidColor
bool IsSolidColor() const override
Definition: solid_color_contents.cc:27
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:142
impeller::SolidColorContents::~SolidColorContents
~SolidColorContents() override
impeller::Geometry::CoversArea
virtual bool CoversArea(const Matrix &transform, const Rect &rect) const
Determines if this geometry, transformed by the given transform, will completely cover all surface ar...
Definition: geometry.cc:120
impeller::RenderPass::SetCommandLabel
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::Entity
Definition: entity.h:20
impeller::TSize
Definition: size.h:19
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:82
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:213
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
impeller::Color::WithAlpha
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:277
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:106
impeller::ContentContext::GetSolidFillPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetSolidFillPipeline(ContentContextOptions opts) const
Definition: content_context.h:433
impeller::SolidColorContents::SolidColorContents
SolidColorContents()
impeller::ColorSourceContents::GetGeometry
const Geometry * GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:20
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::Geometry
Definition: geometry.h:55
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
impeller::SolidColorContents::SetColor
void SetColor(Color color)
Definition: solid_color_contents.cc:19
color
DlColor color
Definition: dl_golden_blur_unittests.cc:24
impeller::ColorSourceContents::AppliesAlphaForStrokeCoverage
bool AppliesAlphaForStrokeCoverage(const Matrix &transform) const
Whether the entity should be treated as non-opaque due to stroke geometry requiring alpha for coverag...
Definition: color_source_contents.cc:53
impeller::SolidColorContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: solid_color_contents.cc:48
path.h
impeller::Color::IsOpaque
constexpr bool IsOpaque() const
Definition: color.h:893
impeller::ContentContextOptions
Definition: content_context.h:254
impeller::SolidColorContents::IsOpaque
bool IsOpaque(const Matrix &transform) 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
Definition: allocation.cc:12
impeller::ContentContext
Definition: content_context.h:366
impeller::Color::Premultiply
constexpr Color Premultiply() const
Definition: color.h:211
impeller::TRect< Scalar >
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:753
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:73