| 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 <limits>
9 #include <optional>
10
11 #include "impeller/base/validation.h"
12 #include "impeller/entity/contents/content_context.h"
13 #include "impeller/entity/contents/filters/filter_contents.h"
14 #include "impeller/entity/contents/texture_contents.h"
15 #include "impeller/entity/entity_pass.h"
16 #include "impeller/geometry/color.h"
17 #include "impeller/geometry/vector.h"
18 #include "impeller/renderer/render_pass.h"
19
20 namespace impeller {
21
22 Entity Entity::FromSnapshot(const Snapshot& snapshot, BlendMode blend_mode) {
23 auto texture_rect = Rect::MakeSize(snapshot.texture->GetSize());
24
25 auto contents = TextureContents::MakeRect(texture_rect);
26 contents->SetTexture(snapshot.texture);
27 contents->SetSamplerDescriptor(snapshot.sampler_descriptor);
28 contents->SetSourceRect(texture_rect);
29 contents->SetOpacity(snapshot.opacity);
30
31 Entity entity;
32 entity.SetBlendMode(blend_mode);
33 entity.SetTransform(snapshot.transform);
34 entity.SetContents(contents);
35 return entity;
36 }
37
38 Entity::Entity() = default;
39
40 Entity::~Entity() = default;
41
42 Entity::Entity(Entity&&) = default;
43
44 Entity::Entity(const Entity&) = default;
45
46 const Matrix& Entity::GetTransform() const {
47 return transform_;
48 }
49
50 Matrix Entity::GetShaderTransform(const RenderPass& pass) const {
51 return Entity::GetShaderTransform(GetShaderClipDepth(), pass, transform_);
52 }
53
54 Matrix Entity::GetShaderTransform(Scalar shader_clip_depth,
55 const RenderPass& pass,
56 const Matrix& transform) {
57 return Matrix::MakeTranslation({0, 0, shader_clip_depth}) *
58 Matrix::MakeScale({1, 1, Entity::kDepthEpsilon}) *
59 pass.GetOrthographicTransform() * transform;
60 }
61
62 void Entity::SetTransform(const Matrix& transform) {
63 transform_ = transform;
64 }
65
66 std::optional<Rect> Entity::GetCoverage() const {
67 if (!contents_) {
68 return std::nullopt;
69 }
70
71 return contents_->GetCoverage(*this);
72 }
73
74 Contents::ClipCoverage Entity::GetClipCoverage(
75 const std::optional<Rect>& current_clip_coverage) const {
76 if (!contents_) {
77 return {};
78 }
79 return contents_->GetClipCoverage(*this, current_clip_coverage);
80 }
81
82 bool Entity::ShouldRender(const std::optional<Rect>& clip_coverage) const {
83 #ifdef IMPELLER_CONTENT_CULLING
84 return contents_->ShouldRender(*this, clip_coverage);
85 #else
86 return true;
87 #endif // IMPELLER_CONTENT_CULLING
88 }
89
90 void Entity::SetContents(std::shared_ptr<Contents> contents) {
91 contents_ = std::move(contents);
92 }
93
94 const std::shared_ptr<Contents>& Entity::GetContents() const {
95 return contents_;
96 }
97
98 void Entity::SetClipDepth(uint32_t clip_depth) {
99 clip_depth_ = clip_depth;
100 }
101
102 uint32_t Entity::GetClipDepth() const {
103 return clip_depth_;
104 }
105
106 Scalar Entity::GetShaderClipDepth() const {
107 return Entity::GetShaderClipDepth(clip_depth_);
108 }
109
110 Scalar Entity::GetShaderClipDepth(uint32_t clip_depth) {
111 Scalar result = std::clamp(clip_depth * kDepthEpsilon, 0.0f, 1.0f);
112 return std::min(result, 1.0f - kDepthEpsilon);
113 }
114
115 void Entity::SetBlendMode(BlendMode blend_mode) {
116 blend_mode_ = blend_mode;
117 }
118
119 BlendMode Entity::GetBlendMode() const {
120 return blend_mode_;
121 }
122
123 bool Entity::CanInheritOpacity() const {
124 if (!contents_) {
125 return false;
126 }
127 if (!((blend_mode_ == BlendMode::kSource && contents_->IsOpaque()) ||
128 blend_mode_ == BlendMode::kSourceOver)) {
129 return false;
130 }
131 return contents_->CanInheritOpacity(*this);
132 }
133
134 bool Entity::SetInheritedOpacity(Scalar alpha) {
135 if (!CanInheritOpacity()) {
136 return false;
137 }
138 if (blend_mode_ == BlendMode::kSource && contents_->IsOpaque()) {
139 blend_mode_ = BlendMode::kSourceOver;
140 }
141 contents_->SetInheritedOpacity(alpha);
142 return true;
143 }
144
145 std::optional<Color> Entity::AsBackgroundColor(ISize target_size) const {
146 return contents_->AsBackgroundColor(*this, target_size);
147 }
148
149 /// @brief Returns true if the blend mode is "destructive", meaning that even
150 /// fully transparent source colors would result in the destination
151 /// getting changed.
152 ///
153 /// This is useful for determining if EntityPass textures can be
154 /// shrinkwrapped to their Entities' coverage; they can be shrinkwrapped
155 /// if all of the contained Entities have non-destructive blends.
156 bool Entity::IsBlendModeDestructive(BlendMode blend_mode) {
157 switch (blend_mode) {
158 case BlendMode::kClear:
159 case BlendMode::kSource:
160 case BlendMode::kSourceIn:
161 case BlendMode::kDestinationIn:
162 case BlendMode::kSourceOut:
163 case BlendMode::kDestinationOut:
164 case BlendMode::kDestinationATop:
165 case BlendMode::kXor:
166 case BlendMode::kModulate:
167 return true;
168 default:
169 return false;
170 }
171 }
172
173 bool Entity::Render(const ContentContext& renderer,
174 RenderPass& parent_pass) const {
175 if (!contents_) {
176 return true;
177 }
178
179 if (!contents_->GetCoverageHint().has_value()) {
180 contents_->SetCoverageHint(
181 Rect::MakeSize(parent_pass.GetRenderTargetSize()));
182 }
183
184 return contents_->Render(renderer, *this, parent_pass);
185 }
186
187 Scalar Entity::DeriveTextScale() const {
188 return GetTransform().GetMaxBasisLengthXY();
189 }
190
191 Entity Entity::Clone() const {
192 return Entity(*this);
193 }
194
195 } // namespace impeller
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:50
impeller::Entity::SetClipDepth
void SetClipDepth(uint32_t clip_depth)
Definition: entity.cc:98
impeller::BlendMode::kDestinationATop
@ kDestinationATop
impeller::Entity::GetShaderClipDepth
float GetShaderClipDepth() const
Definition: entity.cc:106
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Entity::SetBlendMode
void SetBlendMode(BlendMode blend_mode)
Definition: entity.cc:115
impeller::Entity::kDepthEpsilon
static constexpr Scalar kDepthEpsilon
Definition: entity.h:25
texture_contents.h
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
entity.h
impeller::BlendMode
BlendMode
Definition: color.h:59
impeller::BlendMode::kSource
@ kSource
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:156
impeller::Entity::~Entity
~Entity()
impeller::Entity::Entity
Entity()
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
validation.h
impeller::RenderPass::GetOrthographicTransform
const Matrix & GetOrthographicTransform() const
Definition: render_pass.cc:47
impeller::BlendMode::kModulate
@ kModulate
impeller::BlendMode::kSourceOut
@ kSourceOut
impeller::Snapshot::sampler_descriptor
SamplerDescriptor sampler_descriptor
Definition: snapshot.h:29
impeller::Entity::SetContents
void SetContents(std::shared_ptr< Contents > contents)
Definition: entity.cc:90
impeller::Entity::CanInheritOpacity
bool CanInheritOpacity() const
Definition: entity.cc:123
impeller::Entity
Definition: entity.h:20
impeller::RenderPass::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_pass.cc:43
impeller::TSize
Definition: size.h:19
impeller::Entity::ShouldRender
bool ShouldRender(const std::optional< Rect > &clip_coverage) const
Definition: entity.cc:82
filter_contents.h
render_pass.h
impeller::Entity::Render
bool Render(const ContentContext &renderer, RenderPass &parent_pass) const
Definition: entity.cc:173
impeller::Entity::SetInheritedOpacity
bool SetInheritedOpacity(Scalar alpha)
Definition: entity.cc:134
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:231
impeller::Snapshot::transform
Matrix transform
The transform that should be applied to this texture for rendering.
Definition: snapshot.h:27
impeller::Entity::FromSnapshot
static Entity FromSnapshot(const Snapshot &snapshot, BlendMode blend_mode=BlendMode::kSourceOver)
Create an entity that can be used to render a given snapshot.
Definition: entity.cc:22
impeller::BlendMode::kClear
@ kClear
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:24
impeller::Entity::GetContents
const std::shared_ptr< Contents > & GetContents() const
Definition: entity.cc:94
entity_pass.h
impeller::Entity::GetBlendMode
BlendMode GetBlendMode() const
Definition: entity.cc:119
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:102
impeller::BlendMode::kDestinationIn
@ kDestinationIn
impeller::Entity::AsBackgroundColor
std::optional< Color > AsBackgroundColor(ISize target_size) const
Definition: entity.cc:145
content_context.h
impeller::BlendMode::kDestinationOut
@ kDestinationOut
impeller::Entity::GetCoverage
std::optional< Rect > GetCoverage() const
Definition: entity.cc:66
impeller::Entity::SetTransform
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition: entity.cc:62
vector.h
impeller::Matrix::GetMaxBasisLengthXY
constexpr Scalar GetMaxBasisLengthXY() const
Definition: matrix.h:300
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146
impeller::Entity::Clone
Entity Clone() const
Definition: entity.cc:191
impeller::Contents::ClipCoverage
Definition: contents.h:37
impeller::BlendMode::kSourceIn
@ kSourceIn
color.h
impeller::Entity::GetClipCoverage
Contents::ClipCoverage GetClipCoverage(const std::optional< Rect > &current_clip_coverage) const
Definition: entity.cc:74
impeller::Snapshot::texture
std::shared_ptr< Texture > texture
Definition: snapshot.h:25
impeller::Entity::DeriveTextScale
Scalar DeriveTextScale() const
Definition: entity.cc:187
impeller::BlendMode::kXor
@ kXor
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:27
impeller
Definition: aiks_blend_unittests.cc:18
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::ContentContext
Definition: content_context.h:366
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::BlendMode::kSourceOver
@ kSourceOver
impeller::Snapshot::opacity
Scalar opacity
Definition: snapshot.h:35