-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
entity.cc
195 lines (158 loc) · 5.26 KB
/
entity.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "impeller/entity/entity.h"
#include <algorithm>
#include <limits>
#include <optional>
#include "impeller/base/validation.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/filters/filter_contents.h"
#include "impeller/entity/contents/texture_contents.h"
#include "impeller/entity/entity_pass.h"
#include "impeller/geometry/color.h"
#include "impeller/geometry/vector.h"
#include "impeller/renderer/render_pass.h"
namespace impeller {
Entity Entity::FromSnapshot(const Snapshot& snapshot, BlendMode blend_mode) {
auto texture_rect = Rect::MakeSize(snapshot.texture->GetSize());
auto contents = TextureContents::MakeRect(texture_rect);
contents->SetTexture(snapshot.texture);
contents->SetSamplerDescriptor(snapshot.sampler_descriptor);
contents->SetSourceRect(texture_rect);
contents->SetOpacity(snapshot.opacity);
Entity entity;
entity.SetBlendMode(blend_mode);
entity.SetTransform(snapshot.transform);
entity.SetContents(contents);
return entity;
}
Entity::Entity() = default;
Entity::~Entity() = default;
Entity::Entity(Entity&&) = default;
Entity::Entity(const Entity&) = default;
const Matrix& Entity::GetTransform() const {
return transform_;
}
Matrix Entity::GetShaderTransform(const RenderPass& pass) const {
return Entity::GetShaderTransform(GetShaderClipDepth(), pass, transform_);
}
Matrix Entity::GetShaderTransform(Scalar shader_clip_depth,
const RenderPass& pass,
const Matrix& transform) {
return Matrix::MakeTranslation({0, 0, shader_clip_depth}) *
Matrix::MakeScale({1, 1, Entity::kDepthEpsilon}) *
pass.GetOrthographicTransform() * transform;
}
void Entity::SetTransform(const Matrix& transform) {
transform_ = transform;
}
std::optional<Rect> Entity::GetCoverage() const {
if (!contents_) {
return std::nullopt;
}
return contents_->GetCoverage(*this);
}
Contents::ClipCoverage Entity::GetClipCoverage(
const std::optional<Rect>& current_clip_coverage) const {
if (!contents_) {
return {};
}
return contents_->GetClipCoverage(*this, current_clip_coverage);
}
bool Entity::ShouldRender(const std::optional<Rect>& clip_coverage) const {
#ifdef IMPELLER_CONTENT_CULLING
return contents_->ShouldRender(*this, clip_coverage);
#else
return true;
#endif // IMPELLER_CONTENT_CULLING
}
void Entity::SetContents(std::shared_ptr<Contents> contents) {
contents_ = std::move(contents);
}
const std::shared_ptr<Contents>& Entity::GetContents() const {
return contents_;
}
void Entity::SetClipDepth(uint32_t clip_depth) {
clip_depth_ = clip_depth;
}
uint32_t Entity::GetClipDepth() const {
return clip_depth_;
}
Scalar Entity::GetShaderClipDepth() const {
return Entity::GetShaderClipDepth(clip_depth_);
}
Scalar Entity::GetShaderClipDepth(uint32_t clip_depth) {
Scalar result = std::clamp(clip_depth * kDepthEpsilon, 0.0f, 1.0f);
return std::min(result, 1.0f - kDepthEpsilon);
}
void Entity::SetBlendMode(BlendMode blend_mode) {
blend_mode_ = blend_mode;
}
BlendMode Entity::GetBlendMode() const {
return blend_mode_;
}
bool Entity::CanInheritOpacity() const {
if (!contents_) {
return false;
}
if (!((blend_mode_ == BlendMode::kSource && contents_->IsOpaque()) ||
blend_mode_ == BlendMode::kSourceOver)) {
return false;
}
return contents_->CanInheritOpacity(*this);
}
bool Entity::SetInheritedOpacity(Scalar alpha) {
if (!CanInheritOpacity()) {
return false;
}
if (blend_mode_ == BlendMode::kSource && contents_->IsOpaque()) {
blend_mode_ = BlendMode::kSourceOver;
}
contents_->SetInheritedOpacity(alpha);
return true;
}
std::optional<Color> Entity::AsBackgroundColor(ISize target_size) const {
return contents_->AsBackgroundColor(*this, target_size);
}
/// @brief Returns true if the blend mode is "destructive", meaning that even
/// fully transparent source colors would result in the destination
/// getting changed.
///
/// This is useful for determining if EntityPass textures can be
/// shrinkwrapped to their Entities' coverage; they can be shrinkwrapped
/// if all of the contained Entities have non-destructive blends.
bool Entity::IsBlendModeDestructive(BlendMode blend_mode) {
switch (blend_mode) {
case BlendMode::kClear:
case BlendMode::kSource:
case BlendMode::kSourceIn:
case BlendMode::kDestinationIn:
case BlendMode::kSourceOut:
case BlendMode::kDestinationOut:
case BlendMode::kDestinationATop:
case BlendMode::kXor:
case BlendMode::kModulate:
return true;
default:
return false;
}
}
bool Entity::Render(const ContentContext& renderer,
RenderPass& parent_pass) const {
if (!contents_) {
return true;
}
if (!contents_->GetCoverageHint().has_value()) {
contents_->SetCoverageHint(
Rect::MakeSize(parent_pass.GetRenderTargetSize()));
}
return contents_->Render(renderer, *this, parent_pass);
}
Scalar Entity::DeriveTextScale() const {
return GetTransform().GetMaxBasisLengthXY();
}
Entity Entity::Clone() const {
return Entity(*this);
}
} // namespace impeller