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 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 
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 
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 #ifdef IMPELLER_CONTENT_CULLING
75  return contents_->ShouldRender(*this, stencil_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::SetStencilDepth(uint32_t depth) {
90  stencil_depth_ = depth;
91 }
92 
93 uint32_t Entity::GetStencilDepth() const {
94  return stencil_depth_;
95 }
96 
97 void Entity::IncrementStencilDepth(uint32_t increment) {
98  stencil_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 
181 void Entity::SetCapture(Capture capture) const {
182  capture_ = std::move(capture);
183 }
184 
185 } // 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:93
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::Entity::SetBlendMode
void SetBlendMode(BlendMode blend_mode)
Definition: entity.cc:101
impeller::Entity::GetCapture
Capture & GetCapture() const
Definition: entity.cc:177
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:142
impeller::Entity::IncrementStencilDepth
void IncrementStencilDepth(uint32_t increment)
Definition: entity.cc:97
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:181
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: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: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:159
impeller::Entity::SetInheritedOpacity
bool SetInheritedOpacity(Scalar alpha)
Definition: entity.cc:120
impeller::BlendMode::kDestinationATop
@ kDestinationATop
impeller::Entity::GetContents
const std::shared_ptr< Contents > & GetContents() const
Definition: entity.cc:85
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:105
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:131
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:89
impeller::BlendMode::kModulate
@ kModulate
impeller::Entity::DeriveTextScale
Scalar DeriveTextScale() const
Definition: entity.cc:173
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