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 
15 
16 namespace impeller {
17 
18 Entity Entity::FromSnapshot(const Snapshot& snapshot, BlendMode blend_mode) {
19  auto texture_rect = Rect::MakeSize(snapshot.texture->GetSize());
20 
21  auto contents = TextureContents::MakeRect(texture_rect);
22  contents->SetTexture(snapshot.texture);
23  contents->SetSamplerDescriptor(snapshot.sampler_descriptor);
24  contents->SetSourceRect(texture_rect);
25  contents->SetOpacity(snapshot.opacity);
26 
27  Entity entity;
28  entity.SetBlendMode(blend_mode);
29  entity.SetTransform(snapshot.transform);
30  entity.SetContents(contents);
31  return entity;
32 }
33 
34 Entity::Entity() = default;
35 
36 Entity::~Entity() = default;
37 
38 Entity::Entity(Entity&&) = default;
39 
40 Entity::Entity(const Entity&) = default;
41 
42 Entity& Entity::operator=(Entity&&) = default;
43 
44 const Matrix& Entity::GetTransform() const {
45  return transform_;
46 }
47 
49  return Entity::GetShaderTransform(GetShaderClipDepth(), pass, transform_);
50 }
51 
53  const RenderPass& pass,
54  const Matrix& transform) {
56  {0, 0, shader_clip_depth}) *
58 }
59 
61  transform_ = transform;
62 }
63 
64 std::optional<Rect> Entity::GetCoverage() const {
65  if (!contents_) {
66  return std::nullopt;
67  }
68 
69  return contents_->GetCoverage(*this);
70 }
71 
72 void Entity::SetContents(std::shared_ptr<Contents> contents) {
73  contents_ = std::move(contents);
74 }
75 
76 const std::shared_ptr<Contents>& Entity::GetContents() const {
77  return contents_;
78 }
79 
80 void Entity::SetClipDepth(uint32_t clip_depth) {
81  clip_depth_ = clip_depth;
82 }
83 
84 uint32_t Entity::GetClipDepth() const {
85  return clip_depth_;
86 }
87 
89  return Entity::GetShaderClipDepth(clip_depth_);
90 }
91 
92 Scalar Entity::GetShaderClipDepth(uint32_t clip_depth) {
93  Scalar result = std::clamp(clip_depth * kDepthEpsilon, 0.0f, 1.0f);
94  return std::min(result, 1.0f - kDepthEpsilon);
95 }
96 
97 void Entity::SetBlendMode(BlendMode blend_mode) {
98  blend_mode_ = blend_mode;
99 }
100 
102  return blend_mode_;
103 }
104 
106  if (alpha >= 1.0) {
107  return true;
108  }
109  if (blend_mode_ == BlendMode::kSrc && contents_->IsOpaque(GetTransform())) {
110  blend_mode_ = BlendMode::kSrcOver;
111  }
112  contents_->SetInheritedOpacity(alpha);
113  return true;
114 }
115 
116 std::optional<Color> Entity::AsBackgroundColor(ISize target_size) const {
117  return contents_->AsBackgroundColor(*this, target_size);
118 }
119 
120 /// @brief Returns true if the blend mode is "destructive", meaning that even
121 /// fully transparent source colors would result in the destination
122 /// getting changed.
123 ///
124 /// This is useful for determining if EntityPass textures can be
125 /// shrinkwrapped to their Entities' coverage; they can be shrinkwrapped
126 /// if all of the contained Entities have non-destructive blends.
128  switch (blend_mode) {
129  case BlendMode::kClear:
130  case BlendMode::kSrc:
131  case BlendMode::kSrcIn:
132  case BlendMode::kDstIn:
133  case BlendMode::kSrcOut:
134  case BlendMode::kDstOut:
135  case BlendMode::kDstATop:
136  case BlendMode::kXor:
138  return true;
139  default:
140  return false;
141  }
142 }
143 
144 bool Entity::Render(const ContentContext& renderer,
145  RenderPass& parent_pass) const {
146  if (!contents_) {
147  return true;
148  }
149 
150  if (!contents_->GetCoverageHint().has_value()) {
151  contents_->SetCoverageHint(
152  Rect::MakeSize(parent_pass.GetRenderTargetSize()));
153  }
154 
155  return contents_->Render(renderer, *this, parent_pass);
156 }
157 
159  return Entity(*this);
160 }
161 
162 } // namespace impeller
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition: entity.cc:60
std::optional< Rect > GetCoverage() const
Definition: entity.cc:64
bool SetInheritedOpacity(Scalar alpha)
Definition: entity.cc:105
const std::shared_ptr< Contents > & GetContents() const
Definition: entity.cc:76
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:48
void SetClipDepth(uint32_t clip_depth)
Definition: entity.cc:80
BlendMode GetBlendMode() const
Definition: entity.cc:101
void SetContents(std::shared_ptr< Contents > contents)
Definition: entity.cc:72
void SetBlendMode(BlendMode blend_mode)
Definition: entity.cc:97
static constexpr Scalar kDepthEpsilon
Definition: entity.h:31
bool Render(const ContentContext &renderer, RenderPass &parent_pass) const
Definition: entity.cc:144
Entity Clone() const
Definition: entity.cc:158
std::optional< Color > AsBackgroundColor(ISize target_size) const
Definition: entity.cc:116
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:44
uint32_t GetClipDepth() const
Definition: entity.cc:84
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:127
static Entity FromSnapshot(const Snapshot &snapshot, BlendMode blend_mode=BlendMode::kSrcOver)
Create an entity that can be used to render a given snapshot.
Definition: entity.cc:18
Entity & operator=(Entity &&)
float GetShaderClipDepth() const
Definition: entity.cc:88
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
const Matrix & GetOrthographicTransform() const
Definition: render_pass.cc:51
ISize GetRenderTargetSize() const
Definition: render_pass.cc:47
static std::shared_ptr< TextureContents > MakeRect(Rect destination)
float Scalar
Definition: scalar.h:19
BlendMode
Definition: color.h:58
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static constexpr Matrix MakeTranslateScale(const Vector3 &s, const Vector3 &t)
Definition: matrix.h:113
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
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150