| Flutter Impeller |

entity.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/entity.h"
6
7 #include <algorithm>
8 #include <optional>
9
10 #include "impeller/base/validation.h"
11 #include "impeller/entity/contents/content_context.h"
12 #include "impeller/entity/contents/filters/filter_contents.h"
13 #include "impeller/entity/contents/texture_contents.h"
14 #include "impeller/entity/entity_pass.h"
15 #include "impeller/geometry/color.h"
16 #include "impeller/geometry/vector.h"
17 #include "impeller/renderer/render_pass.h"
18
19 namespace impeller {
20
21 std::optional<Entity> Entity::FromSnapshot(
22 const std::optional<Snapshot>& snapshot,
23 BlendMode blend_mode,
24 uint32_t stencil_depth) {
25 if (!snapshot.has_value()) {
26 return std::nullopt;
27 }
28
29 auto texture_rect = Rect::MakeSize(snapshot->texture->GetSize());
30
31 auto contents = TextureContents::MakeRect(texture_rect);
32 contents->SetTexture(snapshot->texture);
33 contents->SetSamplerDescriptor(snapshot->sampler_descriptor);
34 contents->SetSourceRect(texture_rect);
35 contents->SetOpacity(snapshot->opacity);
36
37 Entity entity;
38 entity.SetBlendMode(blend_mode);
39 entity.SetStencilDepth(stencil_depth);
40 entity.SetTransformation(snapshot->transform);
41 entity.SetContents(contents);
42 return entity;
43 }
44
45 Entity::Entity() = default;
46
47 Entity::~Entity() = default;
48
49 const Matrix& Entity::GetTransformation() const {
50 return transformation_;
51 }
52
53 void Entity::SetTransformation(const Matrix& transformation) {
54 transformation_ = transformation;
55 }
56
57 std::optional<Rect> Entity::GetCoverage() const {
58 if (!contents_) {
59 return std::nullopt;
60 }
61
62 return contents_->GetCoverage(*this);
63 }
64
65 Contents::StencilCoverage Entity::GetStencilCoverage(
66 const std::optional<Rect>& current_stencil_coverage) const {
67 if (!contents_) {
68 return {};
69 }
70 return contents_->GetStencilCoverage(*this, current_stencil_coverage);
71 }
72
73 bool Entity::ShouldRender(const std::optional<Rect>& stencil_coverage) const {
74 return contents_->ShouldRender(*this, stencil_coverage);
75 }
76
77 void Entity::SetContents(std::shared_ptr<Contents> contents) {
78 contents_ = std::move(contents);
79 }
80
81 const std::shared_ptr<Contents>& Entity::GetContents() const {
82 return contents_;
83 }
84
85 void Entity::SetStencilDepth(uint32_t depth) {
86 stencil_depth_ = depth;
87 }
88
89 uint32_t Entity::GetStencilDepth() const {
90 return stencil_depth_;
91 }
92
93 void Entity::IncrementStencilDepth(uint32_t increment) {
94 stencil_depth_ += increment;
95 }
96
97 void Entity::SetBlendMode(BlendMode blend_mode) {
98 blend_mode_ = blend_mode;
99 }
100
101 BlendMode Entity::GetBlendMode() const {
102 return blend_mode_;
103 }
104
105 bool Entity::CanInheritOpacity() const {
106 if (!contents_) {
107 return false;
108 }
109 if (!((blend_mode_ == BlendMode::kSource && contents_->IsOpaque()) ||
110 blend_mode_ == BlendMode::kSourceOver)) {
111 return false;
112 }
113 return contents_->CanInheritOpacity(*this);
114 }
115
116 bool Entity::SetInheritedOpacity(Scalar alpha) {
117 if (!CanInheritOpacity()) {
118 return false;
119 }
120 if (blend_mode_ == BlendMode::kSource && contents_->IsOpaque()) {
121 blend_mode_ = BlendMode::kSourceOver;
122 }
123 contents_->SetInheritedOpacity(alpha);
124 return true;
125 }
126
127 std::optional<Color> Entity::AsBackgroundColor(ISize target_size) const {
128 return contents_->AsBackgroundColor(*this, target_size);
129 }
130
131 /// @brief Returns true if the blend mode is "destructive", meaning that even
132 /// fully transparent source colors would result in the destination
133 /// getting changed.
134 ///
135 /// This is useful for determining if EntityPass textures can be
136 /// shrinkwrapped to their Entities' coverage; they can be shrinkwrapped
137 /// if all of the contained Entities have non-destructive blends.
138 bool Entity::IsBlendModeDestructive(BlendMode blend_mode) {
139 switch (blend_mode) {
140 case BlendMode::kClear:
141 case BlendMode::kSource:
142 case BlendMode::kSourceIn:
143 case BlendMode::kDestinationIn:
144 case BlendMode::kSourceOut:
145 case BlendMode::kDestinationOut:
146 case BlendMode::kDestinationATop:
147 case BlendMode::kXor:
148 case BlendMode::kModulate:
149 return true;
150 default:
151 return false;
152 }
153 }
154
155 bool Entity::Render(const ContentContext& renderer,
156 RenderPass& parent_pass) const {
157 if (!contents_) {
158 return true;
159 }
160
161 if (!contents_->GetCoverageHint().has_value()) {
162 contents_->SetCoverageHint(
163 Rect::MakeSize(parent_pass.GetRenderTargetSize()));
164 }
165
166 return contents_->Render(renderer, *this, parent_pass);
167 }
168
169 Scalar Entity::DeriveTextScale() const {
170 return GetTransformation().GetMaxBasisLengthXY();
171 }
172
173 Capture& Entity::GetCapture() const {
174 return capture_;
175 }
176
177 void Entity::SetCapture(Capture capture) const {
178 capture_ = std::move(capture);
179 }
180
181 } // namespace impeller
impeller::BlendMode
BlendMode
Definition: color.h:57
impeller::Entity::SetTransformation
void SetTransformation(const Matrix &transformation)
Definition: entity.cc:53
impeller::Capture
Definition: capture.h:226
impeller::Entity::GetStencilDepth
uint32_t GetStencilDepth() const
Definition: entity.cc:89
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::Entity::SetBlendMode
void SetBlendMode(BlendMode blend_mode)
Definition: entity.cc:97
impeller::Entity::GetCapture
Capture & GetCapture() const
Definition: entity.cc:173
impeller::Entity::FromSnapshot
static std::optional< Entity > FromSnapshot(const std::optional< Snapshot > &snapshot, BlendMode blend_mode=BlendMode::kSourceOver, uint32_t stencil_depth=0)
Create an entity that can be used to render a given snapshot.
Definition: entity.cc:21
texture_contents.h
entity.h
impeller::Entity::IsBlendModeDestructive
static bool IsBlendModeDestructive(BlendMode blend_mode)
Returns true if the blend mode is "destructive", meaning that even fully transparent source colors wo...
Definition: entity.cc:138
impeller::Entity::IncrementStencilDepth
void IncrementStencilDepth(uint32_t increment)
Definition: entity.cc:93
impeller::BlendMode::kSourceIn
@ kSourceIn
impeller::Entity::~Entity
~Entity()
impeller::Entity::Entity
Entity()
impeller::BlendMode::kDestinationIn
@ kDestinationIn
validation.h
impeller::Entity::SetCapture
void SetCapture(Capture capture) const
Definition: entity.cc:177
impeller::BlendMode::kXor
@ kXor
impeller::Entity::GetTransformation
const Matrix & GetTransformation() const
Definition: entity.cc:49
impeller::Entity::SetContents
void SetContents(std::shared_ptr< Contents > contents)
Definition: entity.cc:77
impeller::Entity::CanInheritOpacity
bool CanInheritOpacity() const
Definition: entity.cc:105
impeller::Entity
Definition: entity.h:21
impeller::RenderPass::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_pass.cc:30
impeller::TSize< int64_t >
filter_contents.h
render_pass.h
impeller::BlendMode::kSourceOver
@ kSourceOver
impeller::Contents::StencilCoverage
Definition: contents.h:39
impeller::Entity::Render
bool Render(const ContentContext &renderer, RenderPass &parent_pass) const
Definition: entity.cc:155
impeller::Entity::SetInheritedOpacity
bool SetInheritedOpacity(Scalar alpha)
Definition: entity.cc:116
impeller::BlendMode::kDestinationATop
@ kDestinationATop
impeller::Entity::GetContents
const std::shared_ptr< Contents > & GetContents() const
Definition: entity.cc:81
entity_pass.h
impeller::BlendMode::kDestinationOut
@ kDestinationOut
impeller::Entity::GetStencilCoverage
Contents::StencilCoverage GetStencilCoverage(const std::optional< Rect > &current_stencil_coverage) const
Definition: entity.cc:65
impeller::Entity::GetBlendMode
BlendMode GetBlendMode() const
Definition: entity.cc:101
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:27
impeller::Entity::AsBackgroundColor
std::optional< Color > AsBackgroundColor(ISize target_size) const
Definition: entity.cc:127
content_context.h
impeller::Entity::GetCoverage
std::optional< Rect > GetCoverage() const
Definition: entity.cc:57
vector.h
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:52
impeller::BlendMode::kClear
@ kClear
color.h
impeller::Entity::SetStencilDepth
void SetStencilDepth(uint32_t stencil_depth)
Definition: entity.cc:85
impeller::BlendMode::kModulate
@ kModulate
impeller::Entity::DeriveTextScale
Scalar DeriveTextScale() const
Definition: entity.cc:169
impeller::TextureContents::MakeRect
static std::shared_ptr< TextureContents > MakeRect(Rect destination)
A common case factory that marks the texture contents as having a destination rectangle....
Definition: texture_contents.cc:28
impeller
Definition: aiks_context.cc:10
impeller::ContentContext
Definition: content_context.h:344
impeller::BlendMode::kSourceOut
@ kSourceOut
impeller::BlendMode::kSource
@ kSource
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:36
impeller::Entity::ShouldRender
bool ShouldRender(const std::optional< Rect > &stencil_coverage) const
Definition: entity.cc:73
impeller::Matrix::GetMaxBasisLengthXY
Scalar GetMaxBasisLengthXY() const
Definition: matrix.cc:205