| 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/renderer/render_pass.h"
11
12 namespace impeller {
13
14 SolidColorContents::SolidColorContents() = default;
15
16 SolidColorContents::~SolidColorContents() = default;
17
18 void SolidColorContents::SetColor(Color color) {
19 color_ = color;
20 }
21
22 Color SolidColorContents::GetColor() const {
23 return color_.WithAlpha(color_.alpha * GetOpacityFactor());
24 }
25
26 bool SolidColorContents::IsSolidColor() const {
27 return true;
28 }
29
30 bool SolidColorContents::IsOpaque(const Matrix& transform) const {
31 return GetColor().IsOpaque() && !AppliesAlphaForStrokeCoverage(transform);
32 }
33
34 std::optional<Rect> SolidColorContents::GetCoverage(
35 const Entity& entity) const {
36 if (GetColor().IsTransparent()) {
37 return std::nullopt;
38 }
39
40 const Geometry* geometry = GetGeometry();
41 if (geometry == nullptr) {
42 return std::nullopt;
43 }
44 return geometry->GetCoverage(entity.GetTransform());
45 };
46
47 bool SolidColorContents::Render(const ContentContext& renderer,
48 const Entity& entity,
49 RenderPass& pass) const {
50 using VS = SolidFillPipeline::VertexShader;
51 using FS = SolidFillPipeline::FragmentShader;
52 auto& host_buffer = renderer.GetTransientsBuffer();
53
54 VS::FrameInfo frame_info;
55 FS::FragInfo frag_info;
56 frag_info.color = GetColor().Premultiply() *
57 GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
58
59 PipelineBuilderCallback pipeline_callback =
60 [&renderer](ContentContextOptions options) {
61 return renderer.GetSolidFillPipeline(options);
62 };
63 return ColorSourceContents::DrawGeometry<VS>(
64 renderer, entity, pass, pipeline_callback, frame_info,
65 [&frag_info, &host_buffer](RenderPass& pass) {
66 FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
67 pass.SetCommandLabel("Solid Fill");
68 return true;
69 });
70 }
71
72 std::optional<Color> SolidColorContents::AsBackgroundColor(
73 const Entity& entity,
74 ISize target_size) const {
75 Rect target_rect = Rect::MakeSize(target_size);
76 return GetGeometry()->CoversArea(entity.GetTransform(), target_rect)
77 ? GetColor()
78 : std::optional<Color>();
79 }
80
81 bool SolidColorContents::ApplyColorFilter(
82 const ColorFilterProc& color_filter_proc) {
83 color_ = color_filter_proc(color_);
84 return true;
85 }
86
87 } // namespace impeller
impeller::ColorSourceContents::GetGeometry
const Geometry * GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:20
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:28
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::ColorSourceContents::PipelineBuilderCallback
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
Definition: color_source_contents.h:42
impeller::ContentContext
Definition: content_context.h:138
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:297
impeller::ContentContext::GetSolidFillPipeline
PipelineRef GetSolidFillPipeline(ContentContextOptions opts) const
Definition: content_context.cc:1103
impeller::Contents::ColorFilterProc
std::function< Color(Color)> ColorFilterProc
Definition: contents.h:35
impeller::Entity
Definition: entity.h:26
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
impeller::Geometry
Definition: geometry.h:50
impeller::Geometry::ComputeAlphaCoverage
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:125
impeller::Geometry::GetCoverage
virtual std::optional< Rect > GetCoverage(const Matrix &transform) const =0
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:136
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
impeller::RenderPass::SetCommandLabel
virtual void SetCommandLabel(std::string_view label)
The debugging label to use for the command.
Definition: render_pass.cc:97
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:163
impeller::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:164
impeller::SolidColorContents::~SolidColorContents
~SolidColorContents() override
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:30
impeller::SolidColorContents::IsSolidColor
bool IsSolidColor() const override
Definition: solid_color_contents.cc:26
impeller::SolidColorContents::SetColor
void SetColor(Color color)
Definition: solid_color_contents.cc:18
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:81
impeller::SolidColorContents::SolidColorContents
SolidColorContents()
impeller::SolidColorContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: solid_color_contents.cc:47
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:72
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:34
impeller::SolidColorContents::GetColor
Color GetColor() const
Definition: solid_color_contents.cc:22
content_context.h
entity.h
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:214
geometry.h
impeller
Definition: allocation.cc:12
impeller::FS
LinePipeline::FragmentShader FS
Definition: line_contents.cc:14
impeller::VS
LinePipeline::VertexShader VS
Definition: line_contents.cc:13
render_pass.h
solid_color_contents.h
impeller::Color
Definition: color.h:124
impeller::Color::IsOpaque
constexpr bool IsOpaque() const
Definition: color.h:894
impeller::Color::alpha
Scalar alpha
Definition: color.h:143
impeller::Color::WithAlpha
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:278
impeller::Color::Premultiply
constexpr Color Premultiply() const
Definition: color.h:212
impeller::ContentContextOptions
Definition: content_context.h:40
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::TRect< Scalar >
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
impeller::TSize
Definition: size.h:25