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