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 
6 
7 #include <algorithm>
8 #include <optional>
9 
18 
19 namespace impeller {
20 
21 std::optional<Entity> Entity::FromSnapshot(
22  const std::optional<Snapshot>& snapshot,
23  BlendMode blend_mode,
24  uint32_t clip_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.SetClipDepth(clip_depth);
40  entity.SetTransform(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::GetTransform() const {
50  return transform_;
51 }
52 
53 void Entity::SetTransform(const Matrix& transform) {
54  transform_ = transform;
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 
66  const std::optional<Rect>& current_clip_coverage) const {
67  if (!contents_) {
68  return {};
69  }
70  return contents_->GetClipCoverage(*this, current_clip_coverage);
71 }
72 
73 bool Entity::ShouldRender(const std::optional<Rect>& clip_coverage) const {
74 #ifdef IMPELLER_CONTENT_CULLING
75  return contents_->ShouldRender(*this, clip_coverage);
76 #else
77  return true;
78 #endif // IMPELLER_CONTENT_CULLING
79 }
80 
81 void Entity::SetContents(std::shared_ptr<Contents> contents) {
82  contents_ = std::move(contents);
83 }
84 
85 const std::shared_ptr<Contents>& Entity::GetContents() const {
86  return contents_;
87 }
88 
89 void Entity::SetClipDepth(uint32_t depth) {
90  clip_depth_ = depth;
91 }
92 
93 uint32_t Entity::GetClipDepth() const {
94  return clip_depth_;
95 }
96 
97 void Entity::IncrementStencilDepth(uint32_t increment) {
98  clip_depth_ += increment;
99 }
100 
101 void Entity::SetBlendMode(BlendMode blend_mode) {
102  blend_mode_ = blend_mode;
103 }
104 
106  return blend_mode_;
107 }
108 
110  if (!contents_) {
111  return false;
112  }
113  if (!((blend_mode_ == BlendMode::kSource && contents_->IsOpaque()) ||
114  blend_mode_ == BlendMode::kSourceOver)) {
115  return false;
116  }
117  return contents_->CanInheritOpacity(*this);
118 }
119 
121  if (!CanInheritOpacity()) {
122  return false;
123  }
124  if (blend_mode_ == BlendMode::kSource && contents_->IsOpaque()) {
125  blend_mode_ = BlendMode::kSourceOver;
126  }
127  contents_->SetInheritedOpacity(alpha);
128  return true;
129 }
130 
131 std::optional<Color> Entity::AsBackgroundColor(ISize target_size) const {
132  return contents_->AsBackgroundColor(*this, target_size);
133 }
134 
135 /// @brief Returns true if the blend mode is "destructive", meaning that even
136 /// fully transparent source colors would result in the destination
137 /// getting changed.
138 ///
139 /// This is useful for determining if EntityPass textures can be
140 /// shrinkwrapped to their Entities' coverage; they can be shrinkwrapped
141 /// if all of the contained Entities have non-destructive blends.
143  switch (blend_mode) {
144  case BlendMode::kClear:
145  case BlendMode::kSource:
151  case BlendMode::kXor:
153  return true;
154  default:
155  return false;
156  }
157 }
158 
159 bool Entity::Render(const ContentContext& renderer,
160  RenderPass& parent_pass) const {
161  if (!contents_) {
162  return true;
163  }
164 
165  if (!contents_->GetCoverageHint().has_value()) {
166  contents_->SetCoverageHint(
167  Rect::MakeSize(parent_pass.GetRenderTargetSize()));
168  }
169 
170  return contents_->Render(renderer, *this, parent_pass);
171 }
172 
175 }
176 
178  return capture_;
179 }
180 
182  return Entity(*this);
183 }
184 
185 void Entity::SetCapture(Capture capture) const {
186  capture_ = std::move(capture);
187 }
188 
189 } // namespace impeller
impeller::Entity::SetClipDepth
void SetClipDepth(uint32_t clip_depth)
Definition: entity.cc:89
impeller::Capture
Definition: capture.h:231
impeller::BlendMode::kDestinationATop
@ kDestinationATop
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Entity::SetBlendMode
void SetBlendMode(BlendMode blend_mode)
Definition: entity.cc:101
impeller::Entity::GetCapture
Capture & GetCapture() const
Definition: entity.cc:177
texture_contents.h
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:49
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:142
impeller::Entity::IncrementStencilDepth
void IncrementStencilDepth(uint32_t increment)
Definition: entity.cc:97
impeller::Entity::~Entity
~Entity()
impeller::Entity::Entity
Entity()
validation.h
impeller::BlendMode::kModulate
@ kModulate
impeller::Entity::SetCapture
void SetCapture(Capture capture) const
Definition: entity.cc:185
impeller::BlendMode::kSourceOut
@ kSourceOut
impeller::Entity::SetContents
void SetContents(std::shared_ptr< Contents > contents)
Definition: entity.cc:81
impeller::Entity::CanInheritOpacity
bool CanInheritOpacity() const
Definition: entity.cc:109
impeller::Entity
Definition: entity.h:21
impeller::RenderPass::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_pass.cc:47
impeller::TSize< int64_t >
impeller::Entity::ShouldRender
bool ShouldRender(const std::optional< Rect > &clip_coverage) const
Definition: entity.cc:73
filter_contents.h
render_pass.h
impeller::Entity::Render
bool Render(const ContentContext &renderer, RenderPass &parent_pass) const
Definition: entity.cc:159
impeller::Entity::SetInheritedOpacity
bool SetInheritedOpacity(Scalar alpha)
Definition: entity.cc:120
impeller::BlendMode::kClear
@ kClear
impeller::Entity::GetContents
const std::shared_ptr< Contents > & GetContents() const
Definition: entity.cc:85
entity_pass.h
impeller::Entity::GetBlendMode
BlendMode GetBlendMode() const
Definition: entity.cc:105
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:29
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:93
impeller::BlendMode::kDestinationIn
@ kDestinationIn
impeller::Entity::AsBackgroundColor
std::optional< Color > AsBackgroundColor(ISize target_size) const
Definition: entity.cc:131
content_context.h
impeller::BlendMode::kDestinationOut
@ kDestinationOut
impeller::Entity::GetCoverage
std::optional< Rect > GetCoverage() const
Definition: entity.cc:57
impeller::Entity::SetTransform
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition: entity.cc:53
vector.h
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:44
impeller::Entity::Clone
Entity Clone() const
Definition: entity.cc:181
impeller::Contents::ClipCoverage
Definition: contents.h:40
impeller::BlendMode::kSourceIn
@ kSourceIn
color.h
impeller::Entity::GetClipCoverage
Contents::ClipCoverage GetClipCoverage(const std::optional< Rect > &current_clip_coverage) const
Definition: entity.cc:65
impeller::Entity::DeriveTextScale
Scalar DeriveTextScale() const
Definition: entity.cc:173
impeller::BlendMode::kXor
@ kXor
impeller::Entity::FromSnapshot
static std::optional< Entity > FromSnapshot(const std::optional< Snapshot > &snapshot, BlendMode blend_mode=BlendMode::kSourceOver, uint32_t clip_depth=0)
Create an entity that can be used to render a given snapshot.
Definition: entity.cc:21
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_context.cc:10
impeller::ContentContext
Definition: content_context.h:332
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::BlendMode::kSourceOver
@ kSourceOver
impeller::Matrix::GetMaxBasisLengthXY
Scalar GetMaxBasisLengthXY() const
Definition: matrix.cc:205