Flutter Impeller
entity.h
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 #ifndef FLUTTER_IMPELLER_ENTITY_ENTITY_H_
6 #define FLUTTER_IMPELLER_ENTITY_ENTITY_H_
7 
8 #include <cstdint>
9 
10 #include "impeller/core/capture.h"
14 #include "impeller/geometry/rect.h"
15 
16 namespace impeller {
17 
18 class Renderer;
19 class RenderPass;
20 
21 class Entity {
22  public:
25 
26  static constexpr Scalar kDepthEpsilon = 1.0f / 262144.0;
27 
28  enum class RenderingMode {
29  /// In direct mode, the Entity's transform is used as the current
30  /// local-to-screen transform matrix.
31  kDirect,
32  /// In subpass mode, the Entity passed through the filter is in screen space
33  /// rather than local space, and so some filters (namely,
34  /// MatrixFilterContents) need to interpret the given EffectTransform as the
35  /// current transform matrix.
36  kSubpass,
37  };
38 
39  /// An enum to define how to repeat, fold, or omit colors outside of the
40  /// typically defined range of the source of the colors (such as the
41  /// bounds of an image or the defining geometry of a gradient).
42  enum class TileMode {
43  /// Replicate the edge color if the shader draws outside of its original
44  /// bounds.
45  kClamp,
46 
47  /// Repeat the shader's image horizontally and vertically (or both along and
48  /// perpendicular to a gradient's geometry).
49  kRepeat,
50 
51  /// Repeat the shader's image horizontally and vertically, seamlessly
52  /// alternating mirrored images.
53  kMirror,
54 
55  /// Render the shader's image pixels only within its original bounds. If the
56  /// shader draws outside of its original bounds, transparent black is drawn
57  /// instead.
58  kDecal,
59  };
60 
61  enum class ClipOperation {
63  kIntersect,
64  };
65 
66  /// @brief Create an entity that can be used to render a given snapshot.
67  static Entity FromSnapshot(const Snapshot& snapshot,
68  BlendMode blend_mode = BlendMode::kSourceOver,
69  uint32_t clip_depth = 0);
70 
71  Entity();
72 
73  ~Entity();
74 
75  Entity(Entity&&);
76 
77  /// @brief Get the global transform matrix for this Entity.
78  const Matrix& GetTransform() const;
79 
80  /// @brief Get the vertex shader transform used for drawing this Entity.
81  Matrix GetShaderTransform(const RenderPass& pass) const;
82 
83  /// @brief Static utility that computes the vertex shader transform used for
84  /// drawing an Entity with a given the clip depth and RenderPass size.
85  static Matrix GetShaderTransform(Scalar clip_depth,
86  const RenderPass& pass,
87  const Matrix& transform);
88 
89  /// @brief Set the global transform matrix for this Entity.
90  void SetTransform(const Matrix& transform);
91 
92  std::optional<Rect> GetCoverage() const;
93 
95  const std::optional<Rect>& current_clip_coverage) const;
96 
97  bool ShouldRender(const std::optional<Rect>& clip_coverage) const;
98 
99  void SetContents(std::shared_ptr<Contents> contents);
100 
101  const std::shared_ptr<Contents>& GetContents() const;
102 
103  void SetClipDepth(uint32_t clip_depth);
104 
105  void IncrementStencilDepth(uint32_t increment);
106 
107  uint32_t GetClipDepth() const;
108 
109  void SetNewClipDepth(uint32_t clip_depth);
110 
111  uint32_t GetNewClipDepth() const;
112 
113  float GetShaderClipDepth() const;
114 
115  static float GetShaderClipDepth(uint32_t clip_depth);
116 
117  void SetBlendMode(BlendMode blend_mode);
118 
119  BlendMode GetBlendMode() const;
120 
121  bool Render(const ContentContext& renderer, RenderPass& parent_pass) const;
122 
123  static bool IsBlendModeDestructive(BlendMode blend_mode);
124 
125  bool CanInheritOpacity() const;
126 
127  bool SetInheritedOpacity(Scalar alpha);
128 
129  std::optional<Color> AsBackgroundColor(ISize target_size) const;
130 
131  Scalar DeriveTextScale() const;
132 
133  Capture& GetCapture() const;
134 
135  void SetCapture(Capture capture) const;
136 
137  Entity Clone() const;
138 
139  private:
140  Entity(const Entity&);
141 
142  Matrix transform_;
143  std::shared_ptr<Contents> contents_;
144  BlendMode blend_mode_ = BlendMode::kSourceOver;
145  uint32_t clip_depth_ = 0u;
146  uint32_t new_clip_depth_ = 1u;
147  mutable Capture capture_;
148 };
149 
150 } // namespace impeller
151 
152 #endif // FLUTTER_IMPELLER_ENTITY_ENTITY_H_
impeller::Entity::ClipOperation::kIntersect
@ kIntersect
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::Entity::TileMode::kClamp
@ kClamp
impeller::Entity::kLastPipelineBlendMode
static constexpr BlendMode kLastPipelineBlendMode
Definition: entity.h:23
impeller::Capture
Definition: capture.h:231
capture.h
contents.h
impeller::Entity::GetShaderClipDepth
float GetShaderClipDepth() const
Definition: entity.cc:117
impeller::Entity::GetNewClipDepth
uint32_t GetNewClipDepth() const
Definition: entity.cc:113
impeller::Entity::ClipOperation::kDifference
@ kDifference
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
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:49
impeller::BlendMode
BlendMode
Definition: color.h:59
impeller::BlendMode::kLuminosity
@ kLuminosity
impeller::Entity::TileMode::kDecal
@ kDecal
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::Entity::TileMode::kRepeat
@ kRepeat
impeller::BlendMode::kModulate
@ kModulate
impeller::Entity::SetCapture
void SetCapture(Capture capture) const
Definition: entity.cc:214
impeller::Entity::TileMode::kMirror
@ kMirror
impeller::Entity::SetContents
void SetContents(std::shared_ptr< Contents > contents)
Definition: entity.cc:93
impeller::Entity::CanInheritOpacity
bool CanInheritOpacity() const
Definition: entity.cc:138
matrix.h
impeller::Entity
Definition: entity.h:21
impeller::TSize< int64_t >
impeller::Entity::ShouldRender
bool ShouldRender(const std::optional< Rect > &clip_coverage) const
Definition: entity.cc:85
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::Entity::kLastAdvancedBlendMode
static constexpr BlendMode kLastAdvancedBlendMode
Definition: entity.h:24
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
impeller::Entity::RenderingMode::kDirect
@ kDirect
impeller::Entity::TileMode
TileMode
Definition: entity.h:42
impeller::Entity::RenderingMode::kSubpass
@ kSubpass
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::Entity::AsBackgroundColor
std::optional< Color > AsBackgroundColor(ISize target_size) const
Definition: entity.cc:160
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
rect.h
impeller::Entity::Clone
Entity Clone() const
Definition: entity.cc:210
impeller::Contents::ClipCoverage
Definition: contents.h:40
impeller::Entity::ClipOperation
ClipOperation
Definition: entity.h:61
color.h
impeller::Entity::GetClipCoverage
Contents::ClipCoverage GetClipCoverage(const std::optional< Rect > &current_clip_coverage) const
Definition: entity.cc:77
impeller::Entity::RenderingMode
RenderingMode
Definition: entity.h:28
impeller::Entity::DeriveTextScale
Scalar DeriveTextScale() const
Definition: entity.cc:202
impeller
Definition: aiks_blur_unittests.cc:20
impeller::ContentContext
Definition: content_context.h:392
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::BlendMode::kSourceOver
@ kSourceOver