Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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
16namespace impeller {
17
18Entity 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 contents->SetNeedsRasterizationForRuntimeEffects(
28
29 Entity entity;
30 entity.SetBlendMode(blend_mode);
31 entity.SetTransform(snapshot.transform);
32 entity.SetContents(contents);
33 return entity;
34}
35
36Entity::Entity() = default;
37
38Entity::~Entity() = default;
39
40Entity::Entity(Entity&&) = default;
41
42Entity::Entity(const Entity&) = default;
43
44Entity& Entity::operator=(Entity&&) = default;
45
47 return transform_;
48}
49
51 return Entity::GetShaderTransform(GetShaderClipDepth(), pass, transform_);
52}
53
55 const RenderPass& pass,
56 const Matrix& transform) {
58 {0, 0, shader_clip_depth}) *
60}
61
63 transform_ = transform;
64}
65
66std::optional<Rect> Entity::GetCoverage() const {
67 if (!contents_) {
68 return std::nullopt;
69 }
70
71 return contents_->GetCoverage(*this);
72}
73
74void Entity::SetContents(std::shared_ptr<Contents> contents) {
75 contents_ = std::move(contents);
76}
77
78const std::shared_ptr<Contents>& Entity::GetContents() const {
79 return contents_;
80}
81
82void Entity::SetClipDepth(uint32_t clip_depth) {
83 clip_depth_ = clip_depth;
84}
85
86uint32_t Entity::GetClipDepth() const {
87 return clip_depth_;
88}
89
91 return Entity::GetShaderClipDepth(clip_depth_);
92}
93
94Scalar Entity::GetShaderClipDepth(uint32_t clip_depth) {
95 Scalar result = std::clamp(clip_depth * kDepthEpsilon, 0.0f, 1.0f);
96 return std::min(result, 1.0f - kDepthEpsilon);
97}
98
100 blend_mode_ = blend_mode;
101}
102
104 return blend_mode_;
105}
106
108 if (alpha >= 1.0) {
109 return true;
110 }
111 if (blend_mode_ == BlendMode::kSrc && contents_->IsOpaque(GetTransform())) {
112 blend_mode_ = BlendMode::kSrcOver;
113 }
114 contents_->SetInheritedOpacity(alpha);
115 return true;
116}
117
118std::optional<Color> Entity::AsBackgroundColor(ISize target_size) const {
119 return contents_->AsBackgroundColor(*this, target_size);
120}
121
122/// @brief Returns true if the blend mode is "destructive", meaning that even
123/// fully transparent source colors would result in the destination
124/// getting changed.
125///
126/// This is useful for determining if EntityPass textures can be
127/// shrinkwrapped to their Entities' coverage; they can be shrinkwrapped
128/// if all of the contained Entities have non-destructive blends.
130 switch (blend_mode) {
132 case BlendMode::kSrc:
138 case BlendMode::kXor:
140 return true;
141 default:
142 return false;
143 }
144}
145
146bool Entity::Render(const ContentContext& renderer,
147 RenderPass& parent_pass) const {
148 if (!contents_) {
149 return true;
150 }
151
152 if (!contents_->GetCoverageHint().has_value()) {
153 contents_->SetCoverageHint(
154 Rect::MakeSize(parent_pass.GetRenderTargetSize()));
155 }
156
157 return contents_->Render(renderer, *this, parent_pass);
158}
159
161 return Entity(*this);
162}
163
164} // namespace impeller
void SetTransform(const Matrix &transform)
Set the global transform matrix for this Entity.
Definition entity.cc:62
std::optional< Rect > GetCoverage() const
Definition entity.cc:66
bool SetInheritedOpacity(Scalar alpha)
Definition entity.cc:107
const std::shared_ptr< Contents > & GetContents() const
Definition entity.cc:78
Matrix GetShaderTransform(const RenderPass &pass) const
Definition entity.cc:50
void SetClipDepth(uint32_t clip_depth)
Definition entity.cc:82
BlendMode GetBlendMode() const
Definition entity.cc:103
void SetContents(std::shared_ptr< Contents > contents)
Definition entity.cc:74
void SetBlendMode(BlendMode blend_mode)
Definition entity.cc:99
static constexpr Scalar kDepthEpsilon
Definition entity.h:31
bool Render(const ContentContext &renderer, RenderPass &parent_pass) const
Definition entity.cc:146
Entity Clone() const
Definition entity.cc:160
std::optional< Color > AsBackgroundColor(ISize target_size) const
Definition entity.cc:118
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition entity.cc:46
uint32_t GetClipDepth() const
Definition entity.cc:86
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:129
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:90
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition render_pass.h:30
const Matrix & GetOrthographicTransform() const
ISize GetRenderTargetSize() const
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
bool needs_rasterization_for_runtime_effects
Whether this snapshot needs to be re-rasterized when used as an input to a runtime effect.
Definition snapshot.h:46
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
static constexpr TRect MakeSize(const TSize< U > &size)
Definition rect.h:150