Flutter Engine
The Flutter Engine
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
6
7#include <algorithm>
8#include <limits>
9#include <optional>
10
19
20namespace impeller {
21
22Entity 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
38Entity::Entity() = default;
39
40Entity::~Entity() = default;
41
42Entity::Entity(Entity&&) = default;
43
44Entity::Entity(const Entity&) = default;
45
47 return transform_;
48}
49
51 return Entity::GetShaderTransform(GetShaderClipDepth(), pass, transform_);
52}
53
55 const RenderPass& pass,
56 const Matrix& transform) {
57 return Matrix::MakeTranslation({0, 0, shader_clip_depth}) *
60}
61
63 transform_ = transform;
64}
65
66std::optional<Rect> Entity::GetCoverage() const {
67 if (!contents_) {
68 return std::nullopt;
69 }
70
71 return contents_->GetCoverage(*this);
72}
73
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
82bool 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
90void Entity::SetContents(std::shared_ptr<Contents> contents) {
91 contents_ = std::move(contents);
92}
93
94const std::shared_ptr<Contents>& Entity::GetContents() const {
95 return contents_;
96}
97
98void Entity::SetClipDepth(uint32_t clip_depth) {
99 clip_depth_ = clip_depth;
100}
101
102uint32_t Entity::GetClipDepth() const {
103 return clip_depth_;
104}
105
107 return Entity::GetShaderClipDepth(clip_depth_);
108}
109
110Scalar 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
116 blend_mode_ = blend_mode;
117}
118
120 return blend_mode_;
121}
122
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
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
145std::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.
157 switch (blend_mode) {
165 case BlendMode::kXor:
167 return true;
168 default:
169 return false;
170 }
171}
172
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
189}
190
192 return Entity(*this);
193}
194
195} // namespace impeller
static unsigned clamp(SkFixed fx, int max)
bool CanInheritOpacity() const
Definition: entity.cc:123
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition: entity.cc:62
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
std::optional< Rect > GetCoverage() const
Definition: entity.cc:66
bool SetInheritedOpacity(Scalar alpha)
Definition: entity.cc:134
const std::shared_ptr< Contents > & GetContents() const
Definition: entity.cc:94
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:50
void SetClipDepth(uint32_t clip_depth)
Definition: entity.cc:98
BlendMode GetBlendMode() const
Definition: entity.cc:119
void SetContents(std::shared_ptr< Contents > contents)
Definition: entity.cc:90
void SetBlendMode(BlendMode blend_mode)
Definition: entity.cc:115
static constexpr Scalar kDepthEpsilon
Definition: entity.h:25
bool Render(const ContentContext &renderer, RenderPass &parent_pass) const
Definition: entity.cc:173
Scalar DeriveTextScale() const
Definition: entity.cc:187
Entity Clone() const
Definition: entity.cc:191
std::optional< Color > AsBackgroundColor(ISize target_size) const
Definition: entity.cc:145
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
uint32_t GetClipDepth() const
Definition: entity.cc:102
bool ShouldRender(const std::optional< Rect > &clip_coverage) const
Definition: entity.cc:82
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
Contents::ClipCoverage GetClipCoverage(const std::optional< Rect > &current_clip_coverage) const
Definition: entity.cc:74
float GetShaderClipDepth() const
Definition: entity.cc:106
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
const Matrix & GetOrthographicTransform() const
Definition: render_pass.cc:47
ISize GetRenderTargetSize() const
Definition: render_pass.cc:43
static std::shared_ptr< TextureContents > MakeRect(Rect destination)
A common case factory that marks the texture contents as having a destination rectangle....
GAsyncResult * result
static float min(float r, float g, float b)
Definition: hsl.cpp:48
float Scalar
Definition: scalar.h:18
BlendMode
Definition: color.h:59
static SkColor4f transform(SkColor4f c, SkColorSpace *src, SkColorSpace *dst)
Definition: p3.cpp:47
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
constexpr Scalar GetMaxBasisLengthXY() const
Definition: matrix.h:300
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:24
Matrix transform
The transform that should be applied to this texture for rendering.
Definition: snapshot.h:27
std::shared_ptr< Texture > texture
Definition: snapshot.h:25
SamplerDescriptor sampler_descriptor
Definition: snapshot.h:29
static constexpr TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146