| 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
5 #include "impeller/entity/entity.h"
6
7 #include <algorithm>
8 #include <limits>
9 #include <optional>
10
11 #include "impeller/base/validation.h"
12 #include "impeller/entity/contents/content_context.h"
13 #include "impeller/entity/contents/filters/filter_contents.h"
14 #include "impeller/entity/contents/texture_contents.h"
15 #include "impeller/geometry/color.h"
16 #include "impeller/geometry/vector.h"
17 #include "impeller/renderer/render_pass.h"
18
19 namespace impeller {
20
21 Entity Entity::FromSnapshot(const Snapshot& snapshot, BlendMode blend_mode) {
22 auto texture_rect = Rect::MakeSize(snapshot.texture->GetSize());
23
24 auto contents = TextureContents::MakeRect(texture_rect);
25 contents->SetTexture(snapshot.texture);
26 contents->SetSamplerDescriptor(snapshot.sampler_descriptor);
27 contents->SetSourceRect(texture_rect);
28 contents->SetOpacity(snapshot.opacity);
29
30 Entity entity;
31 entity.SetBlendMode(blend_mode);
32 entity.SetTransform(snapshot.transform);
33 entity.SetContents(contents);
34 return entity;
35 }
36
37 Entity::Entity() = default;
38
39 Entity::~Entity() = default;
40
41 Entity::Entity(Entity&&) = default;
42
43 Entity::Entity(const Entity&) = default;
44
45 Entity& Entity::operator=(Entity&&) = default;
46
47 const Matrix& Entity::GetTransform() const {
48 return transform_;
49 }
50
51 Matrix Entity::GetShaderTransform(const RenderPass& pass) const {
52 return Entity::GetShaderTransform(GetShaderClipDepth(), pass, transform_);
53 }
54
55 Matrix Entity::GetShaderTransform(Scalar shader_clip_depth,
56 const RenderPass& pass,
57 const Matrix& transform) {
58 return Matrix::MakeTranslation({0, 0, shader_clip_depth}) *
59 Matrix::MakeScale({1, 1, Entity::kDepthEpsilon}) *
60 pass.GetOrthographicTransform() * transform;
61 }
62
63 void Entity::SetTransform(const Matrix& transform) {
64 transform_ = transform;
65 }
66
67 std::optional<Rect> Entity::GetCoverage() const {
68 if (!contents_) {
69 return std::nullopt;
70 }
71
72 return contents_->GetCoverage(*this);
73 }
74
75 Contents::ClipCoverage Entity::GetClipCoverage(
76 const std::optional<Rect>& current_clip_coverage) const {
77 if (!contents_) {
78 return {};
79 }
80 return contents_->GetClipCoverage(*this, current_clip_coverage);
81 }
82
83 void Entity::SetContents(std::shared_ptr<Contents> contents) {
84 contents_ = std::move(contents);
85 }
86
87 const std::shared_ptr<Contents>& Entity::GetContents() const {
88 return contents_;
89 }
90
91 void Entity::SetClipDepth(uint32_t clip_depth) {
92 clip_depth_ = clip_depth;
93 }
94
95 uint32_t Entity::GetClipDepth() const {
96 return clip_depth_;
97 }
98
99 Scalar Entity::GetShaderClipDepth() const {
100 return Entity::GetShaderClipDepth(clip_depth_);
101 }
102
103 Scalar Entity::GetShaderClipDepth(uint32_t clip_depth) {
104 Scalar result = std::clamp(clip_depth * kDepthEpsilon, 0.0f, 1.0f);
105 return std::min(result, 1.0f - kDepthEpsilon);
106 }
107
108 void Entity::SetBlendMode(BlendMode blend_mode) {
109 blend_mode_ = blend_mode;
110 }
111
112 BlendMode Entity::GetBlendMode() const {
113 return blend_mode_;
114 }
115
116 bool Entity::SetInheritedOpacity(Scalar alpha) {
117 if (alpha >= 1.0) {
118 return true;
119 }
120 if (blend_mode_ == BlendMode::kSource &&
121 contents_->IsOpaque(GetTransform())) {
122 blend_mode_ = BlendMode::kSourceOver;
123 }
124 contents_->SetInheritedOpacity(alpha);
125 return true;
126 }
127
128 std::optional<Color> Entity::AsBackgroundColor(ISize target_size) const {
129 return contents_->AsBackgroundColor(*this, target_size);
130 }
131
132 /// @brief Returns true if the blend mode is "destructive", meaning that even
133 /// fully transparent source colors would result in the destination
134 /// getting changed.
135 ///
136 /// This is useful for determining if EntityPass textures can be
137 /// shrinkwrapped to their Entities' coverage; they can be shrinkwrapped
138 /// if all of the contained Entities have non-destructive blends.
139 bool Entity::IsBlendModeDestructive(BlendMode blend_mode) {
140 switch (blend_mode) {
141 case BlendMode::kClear:
142 case BlendMode::kSource:
143 case BlendMode::kSourceIn:
144 case BlendMode::kDestinationIn:
145 case BlendMode::kSourceOut:
146 case BlendMode::kDestinationOut:
147 case BlendMode::kDestinationATop:
148 case BlendMode::kXor:
149 case BlendMode::kModulate:
150 return true;
151 default:
152 return false;
153 }
154 }
155
156 bool Entity::Render(const ContentContext& renderer,
157 RenderPass& parent_pass) const {
158 if (!contents_) {
159 return true;
160 }
161
162 if (!contents_->GetCoverageHint().has_value()) {
163 contents_->SetCoverageHint(
164 Rect::MakeSize(parent_pass.GetRenderTargetSize()));
165 }
166
167 return contents_->Render(renderer, *this, parent_pass);
168 }
169
170 Entity Entity::Clone() const {
171 return Entity(*this);
172 }
173
174 } // namespace impeller
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:51
impeller::Entity::SetClipDepth
void SetClipDepth(uint32_t clip_depth)
Definition: entity.cc:91
impeller::BlendMode::kDestinationATop
@ kDestinationATop
impeller::Entity::GetShaderClipDepth
float GetShaderClipDepth() const
Definition: entity.cc:99
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Entity::SetBlendMode
void SetBlendMode(BlendMode blend_mode)
Definition: entity.cc:108
impeller::Entity::kDepthEpsilon
static constexpr Scalar kDepthEpsilon
Definition: entity.h:25
texture_contents.h
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:47
entity.h
impeller::BlendMode
BlendMode
Definition: color.h:58
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:139
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::BlendMode::kSourceOut
@ kSourceOut
impeller::Snapshot::sampler_descriptor
SamplerDescriptor sampler_descriptor
Definition: snapshot.h:29
impeller::Entity::SetContents
void SetContents(std::shared_ptr< Contents > contents)
Definition: entity.cc:83
impeller::Entity
Definition: entity.h:20
impeller::RenderPass::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_pass.cc:43
impeller::TSize
Definition: size.h:19
filter_contents.h
render_pass.h
impeller::Entity::Render
bool Render(const ContentContext &renderer, RenderPass &parent_pass) const
Definition: entity.cc:156
impeller::Entity::SetInheritedOpacity
bool SetInheritedOpacity(Scalar alpha)
Definition: entity.cc:116
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:213
impeller::Snapshot::transform
Matrix transform
The transform that should be applied to this texture for rendering.
Definition: snapshot.h:27
impeller::Entity::FromSnapshot
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:21
impeller::BlendMode::kClear
@ kClear
impeller::Snapshot
Represents a texture and its intended draw transform/sampler configuration.
Definition: snapshot.h:24
impeller::Entity::GetContents
const std::shared_ptr< Contents > & GetContents() const
Definition: entity.cc:87
impeller::Entity::GetBlendMode
BlendMode GetBlendMode() const
Definition: entity.cc:112
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:95
impeller::BlendMode::kDestinationIn
@ kDestinationIn
impeller::Entity::AsBackgroundColor
std::optional< Color > AsBackgroundColor(ISize target_size) const
Definition: entity.cc:128
content_context.h
impeller::BlendMode::kDestinationOut
@ kDestinationOut
impeller::Entity::GetCoverage
std::optional< Rect > GetCoverage() const
Definition: entity.cc:67
impeller::Entity::SetTransform
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition: entity.cc:63
vector.h
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
impeller::Entity::Clone
Entity Clone() const
Definition: entity.cc:170
impeller::Contents::ClipCoverage
Definition: contents.h:37
impeller::BlendMode::kSourceIn
@ kSourceIn
color.h
impeller::Entity::GetClipCoverage
Contents::ClipCoverage GetClipCoverage(const std::optional< Rect > &current_clip_coverage) const
Definition: entity.cc:75
impeller::Entity::operator=
Entity & operator=(Entity &&)
impeller::Snapshot::texture
std::shared_ptr< Texture > texture
Definition: snapshot.h:25
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:28
impeller
Definition: allocation.cc:12
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::ContentContext
Definition: content_context.h:366
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:35