| 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 <optional>
9
10 #include "impeller/entity/contents/content_context.h"
11 #include "impeller/entity/contents/texture_contents.h"
12 #include "impeller/geometry/color.h"
13 #include "impeller/geometry/vector.h"
14 #include "impeller/renderer/render_pass.h"
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
48 Matrix Entity::GetShaderTransform(const RenderPass& pass) const {
49 return Entity::GetShaderTransform(GetShaderClipDepth(), pass, transform_);
50 }
51
52 Matrix Entity::GetShaderTransform(Scalar shader_clip_depth,
53 const RenderPass& pass,
54 const Matrix& transform) {
55 return Matrix::MakeTranslateScale({1, 1, Entity::kDepthEpsilon},
56 {0, 0, shader_clip_depth}) *
57 pass.GetOrthographicTransform() * transform;
58 }
59
60 void Entity::SetTransform(const Matrix& transform) {
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
88 Scalar Entity::GetShaderClipDepth() const {
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
101 BlendMode Entity::GetBlendMode() const {
102 return blend_mode_;
103 }
104
105 bool Entity::SetInheritedOpacity(Scalar alpha) {
106 if (alpha >= 1.0) {
107 return true;
108 }
109 if (blend_mode_ == BlendMode::kSource &&
110 contents_->IsOpaque(GetTransform())) {
111 blend_mode_ = BlendMode::kSourceOver;
112 }
113 contents_->SetInheritedOpacity(alpha);
114 return true;
115 }
116
117 std::optional<Color> Entity::AsBackgroundColor(ISize target_size) const {
118 return contents_->AsBackgroundColor(*this, target_size);
119 }
120
121 /// @brief Returns true if the blend mode is "destructive", meaning that even
122 /// fully transparent source colors would result in the destination
123 /// getting changed.
124 ///
125 /// This is useful for determining if EntityPass textures can be
126 /// shrinkwrapped to their Entities' coverage; they can be shrinkwrapped
127 /// if all of the contained Entities have non-destructive blends.
128 bool Entity::IsBlendModeDestructive(BlendMode blend_mode) {
129 switch (blend_mode) {
130 case BlendMode::kClear:
131 case BlendMode::kSource:
132 case BlendMode::kSourceIn:
133 case BlendMode::kDestinationIn:
134 case BlendMode::kSourceOut:
135 case BlendMode::kDestinationOut:
136 case BlendMode::kDestinationATop:
137 case BlendMode::kXor:
138 case BlendMode::kModulate:
139 return true;
140 default:
141 return false;
142 }
143 }
144
145 bool Entity::Render(const ContentContext& renderer,
146 RenderPass& parent_pass) const {
147 if (!contents_) {
148 return true;
149 }
150
151 if (!contents_->GetCoverageHint().has_value()) {
152 contents_->SetCoverageHint(
153 Rect::MakeSize(parent_pass.GetRenderTargetSize()));
154 }
155
156 return contents_->Render(renderer, *this, parent_pass);
157 }
158
159 Entity Entity::Clone() const {
160 return Entity(*this);
161 }
162
163 } // namespace impeller
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:48
impeller::Entity::SetClipDepth
void SetClipDepth(uint32_t clip_depth)
Definition: entity.cc:80
impeller::BlendMode::kDestinationATop
@ kDestinationATop
impeller::Entity::GetShaderClipDepth
float GetShaderClipDepth() const
Definition: entity.cc:88
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Entity::SetBlendMode
void SetBlendMode(BlendMode blend_mode)
Definition: entity.cc:97
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:44
entity.h
impeller::Matrix::MakeTranslateScale
static constexpr Matrix MakeTranslateScale(const Vector3 &s, const Vector3 &t)
Definition: matrix.h:113
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:128
impeller::Entity::~Entity
~Entity()
impeller::Entity::Entity
Entity()
impeller::RenderPass::GetOrthographicTransform
const Matrix & GetOrthographicTransform() const
Definition: render_pass.cc:51
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:72
impeller::Entity
Definition: entity.h:20
impeller::RenderPass::GetRenderTargetSize
ISize GetRenderTargetSize() const
Definition: render_pass.cc:47
impeller::TSize
Definition: size.h:25
render_pass.h
impeller::Entity::Render
bool Render(const ContentContext &renderer, RenderPass &parent_pass) const
Definition: entity.cc:145
impeller::Entity::SetInheritedOpacity
bool SetInheritedOpacity(Scalar alpha)
Definition: entity.cc:105
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:214
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:18
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:76
impeller::Entity::GetBlendMode
BlendMode GetBlendMode() const
Definition: entity.cc:101
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:30
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:84
impeller::BlendMode::kDestinationIn
@ kDestinationIn
impeller::Entity::AsBackgroundColor
std::optional< Color > AsBackgroundColor(ISize target_size) const
Definition: entity.cc:117
content_context.h
impeller::BlendMode::kDestinationOut
@ kDestinationOut
impeller::Entity::GetCoverage
std::optional< Rect > GetCoverage() const
Definition: entity.cc:64
impeller::Entity::SetTransform
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition: entity.cc:60
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:159
impeller::BlendMode::kSourceIn
@ kSourceIn
color.h
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::ContentContext
Definition: content_context.h:371
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