| Flutter Impeller |

Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
impeller::Entity Class Reference

`#include <entity.h>`

## Public Types
enum class | RenderingMode { kDirect , kSubpassAppendSnapshotTransform , kSubpassPrependSnapshotTransform }
enum class | TileMode { kClamp , kRepeat , kMirror , kDecal }
enum class | ClipOperation { kDifference , kIntersect }

## Public Member Functions
| Entity ()
| ~Entity ()
| Entity (Entity &&)
Entity & | operator= (Entity &&)
const Matrix & | GetTransform () const
| Get the global transform matrix for this Entity. More...
Matrix | GetShaderTransform (const RenderPass &pass) const
void | SetTransform (const Matrix &transform)
| Set the global transform matrix for this Entity. More...
std::optional< Rect > | GetCoverage () const
void | SetContents (std::shared_ptr< Contents > contents)
const std::shared_ptr< Contents > & | GetContents () const
void | SetClipDepth (uint32_t clip_depth)
uint32_t | GetClipDepth () const
float | GetShaderClipDepth () const
void | SetBlendMode (BlendMode blend_mode)
BlendMode | GetBlendMode () const
bool | Render (const ContentContext &renderer, RenderPass &parent_pass) const
bool | SetInheritedOpacity (Scalar alpha)
std::optional< Color > | AsBackgroundColor (ISize target_size) const
Entity | Clone () const

## Static Public Member Functions
static Entity | FromSnapshot (const Snapshot &snapshot, BlendMode blend_mode=BlendMode::kSrcOver)
| Create an entity that can be used to render a given snapshot. More...
static Matrix | GetShaderTransform (Scalar clip_depth, const RenderPass &pass, const Matrix &transform)
| Static utility that computes the vertex shader transform used for drawing an Entity with a given the clip depth and RenderPass size. More...
static float | GetShaderClipDepth (uint32_t clip_depth)
static bool | IsBlendModeDestructive (BlendMode blend_mode)
| Returns true if the blend mode is "destructive", meaning that even fully transparent source colors would result in the destination getting changed. More...

## Static Public Attributes
static constexpr BlendMode | kLastPipelineBlendMode = BlendMode::kModulate
static constexpr BlendMode | kLastAdvancedBlendMode = BlendMode::kLuminosity
static constexpr Scalar | kDepthEpsilon = 1.0f / 262144.0

## Detailed Description

Represents a renderable object within the Impeller scene.

An Entity combines graphical content (`Contents`) with properties like transformation (`Matrix`), blend mode (`BlendMode`), and stencil clip depth. It serves as the primary unit for constructing and rendering scenes. Entities can be created directly or from `Snapshot` objects.

Definition at line 26 of file entity.h.

## Member Enumeration Documentation

## ◆ ClipOperation

enum impeller::Entity::ClipOperation
| strong

Enumerator
kDifference |
kIntersect |

Definition at line 67 of file entity.h.

67 {
68 kDifference,
69 kIntersect,
70 };
impeller::BlendSelectValues::kDifference
@ kDifference

## ◆ RenderingMode

enum impeller::Entity::RenderingMode
| strong

Enumerator
kDirect | In direct mode, the Entity's transform is used as the current local-to-screen transform matrix.
kSubpassAppendSnapshotTransform | In subpass mode, the Entity passed through the filter is in screen space rather than local space, and so some filters (namely, MatrixFilterContents) need to interpret the given EffectTransform as the current transform matrix.
kSubpassPrependSnapshotTransform |

Definition at line 33 of file entity.h.

33 {
34 /// In direct mode, the Entity's transform is used as the current
35 /// local-to-screen transform matrix.
36 kDirect,
37 /// In subpass mode, the Entity passed through the filter is in screen space
38 /// rather than local space, and so some filters (namely,
39 /// MatrixFilterContents) need to interpret the given EffectTransform as the
40 /// current transform matrix.
41 kSubpassAppendSnapshotTransform,
42 kSubpassPrependSnapshotTransform,
43 };

## ◆ TileMode

enum impeller::Entity::TileMode
| strong

An enum to define how to repeat, fold, or omit colors outside of the typically defined range of the source of the colors (such as the bounds of an image or the defining geometry of a gradient).

Enumerator
kClamp | Replicate the edge color if the shader draws outside of its original bounds.
kRepeat | Repeat the shader's image horizontally and vertically (or both along and perpendicular to a gradient's geometry).
kMirror | Repeat the shader's image horizontally and vertically, seamlessly alternating mirrored images.
kDecal | Render the shader's image pixels only within its original bounds. If the shader draws outside of its original bounds, transparent black is drawn instead.

Definition at line 48 of file entity.h.

48 {
49 /// Replicate the edge color if the shader draws outside of its original
50 /// bounds.
51 kClamp,
52
53 /// Repeat the shader's image horizontally and vertically (or both along and
54 /// perpendicular to a gradient's geometry).
55 kRepeat,
56
57 /// Repeat the shader's image horizontally and vertically, seamlessly
58 /// alternating mirrored images.
59 kMirror,
60
61 /// Render the shader's image pixels only within its original bounds. If the
62 /// shader draws outside of its original bounds, transparent black is drawn
63 /// instead.
64 kDecal,
65 };
impeller::SamplerAddressMode::kDecal
@ kDecal
decal sampling mode is only supported on devices that pass the Capabilities.SupportsDecalSamplerAddre...
impeller::SamplerAddressMode::kRepeat
@ kRepeat
impeller::SamplerAddressMode::kMirror
@ kMirror

## Constructor & Destructor Documentation

## ◆ Entity() [1/2]

impeller::Entity::Entity | ( |  | ) |
| default

Referenced by Clone().

## ◆ ~Entity()

impeller::Entity::~Entity | ( |  | ) |
| default

## ◆ Entity() [2/2]

impeller::Entity::Entity | ( | Entity && |  | ) |
| default

## Member Function Documentation

## ◆ AsBackgroundColor()

std::optional< Color > impeller::Entity::AsBackgroundColor | ( | ISize | target_size | ) | const

Attempts to represent this entity as a solid background color.

This is an optimization. If the entity's contents can be represented as a solid color covering the entire target area, this method returns that color.

Parameters

  [in] | target_size | The size of the render target.

Returns
  The background color if representable, otherwise `std::nullopt`.

Definition at line 117 of file entity.cc.

117 {
118 return contents_->AsBackgroundColor(*this, target_size);
119 }

## ◆ Clone()

Entity impeller::Entity::Clone | ( |  | ) | const

Definition at line 159 of file entity.cc.

159 {
160 return Entity(*this);
161 }
impeller::Entity::Entity
Entity()

References Entity().

Referenced by impeller::FilterContents::GetCoverage(), impeller::FilterContents::GetEntity(), impeller::FilterInput::GetLocalCoverage(), and impeller::TextShadowCache::Lookup().

## ◆ FromSnapshot()

Entity impeller::Entity::FromSnapshot | ( | const Snapshot & | snapshot,
|  | BlendMode | blend_mode = `BlendMode::kSrcOver`
| ) |  |
| static

Create an entity that can be used to render a given snapshot.

Definition at line 18 of file entity.cc.

18 {
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(
27 snapshot.needs_rasterization_for_runtime_effects);
28
29 Entity entity;
30 entity.SetBlendMode(blend_mode);
31 entity.SetTransform(snapshot.transform);
32 entity.SetContents(contents);
33 return entity;
34 }
impeller::TextureContents::MakeRect
static std::shared_ptr< TextureContents > MakeRect(Rect destination)
Definition: texture_contents.cc:28
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150

References impeller::TextureContents::MakeRect(), impeller::TRect< Scalar >::MakeSize(), impeller::Snapshot::needs_rasterization_for_runtime_effects, impeller::Snapshot::opacity, impeller::Snapshot::sampler_descriptor, SetBlendMode(), SetContents(), SetTransform(), impeller::Snapshot::texture, and impeller::Snapshot::transform.

Referenced by impeller::AdvancedBlend(), and impeller::PipelineBlend().

## ◆ GetBlendMode()

BlendMode impeller::Entity::GetBlendMode | ( |  | ) | const

Definition at line 102 of file entity.cc.

102 {
103 return blend_mode_;
104 }

Referenced by impeller::AdvancedBlend(), impeller::ColorSourceContents::DrawGeometry(), impeller::OptionsFromPassAndEntity(), impeller::PipelineBlend(), impeller::SolidRRectLikeBlurContents::Render(), impeller::Canvas::Restore(), and impeller::testing::TEST_P().

## ◆ GetClipDepth()

uint32_t impeller::Entity::GetClipDepth | ( |  | ) | const

Gets the stencil clip depth level for this entity.

See also
  SetClipDepth(uint32_t)
  GetShaderClipDepth()

Returns
  The current integer clip depth level.

Definition at line 86 of file entity.cc.

86 {
87 return clip_depth_;
88 }

Referenced by impeller::ColorSourceContents::DrawGeometry(), impeller::TextShadowCache::Lookup(), and impeller::FilterContents::Render().

## ◆ GetContents()

const std::shared_ptr< Contents > & impeller::Entity::GetContents | ( |  | ) | const

Definition at line 78 of file entity.cc.

78 {
79 return contents_;
80 }

Referenced by impeller::AdvancedBlend(), impeller::ContentsFilterInput::GetSnapshot(), impeller::PipelineBlend(), and impeller::Canvas::Restore().

## ◆ GetCoverage()

std::optional< Rect > impeller::Entity::GetCoverage | ( |  | ) | const

Calculates the axis-aligned bounding box covering this entity after its transformation is applied.

Returns
  The coverage rectangle in the parent coordinate space, or `std::nullopt` if the entity has no contents.

Definition at line 66 of file entity.cc.

66 {
67 if (!contents_) {
68 return std::nullopt;
69 }
70
71 return contents_->GetCoverage(*this);
72 }

Referenced by impeller::TextShadowCache::Lookup(), impeller::Canvas::Restore(), impeller::testing::TEST(), and impeller::testing::TEST_P().

## ◆ GetShaderClipDepth() [1/2]

Scalar impeller::Entity::GetShaderClipDepth | ( |  | ) | const

Gets the shader-compatible depth value based on the entity's current clip depth level (`clip_depth_`).

See also
  GetShaderClipDepth(uint32_t) for details on the conversion logic.

Returns
  The floating-point depth value for shaders corresponding to the entity's `clip_depth_`.

Definition at line 90 of file entity.cc.

90 {
91 return Entity::GetShaderClipDepth(clip_depth_);
92 }
impeller::Entity::GetShaderClipDepth
float GetShaderClipDepth() const
Definition: entity.cc:90

Referenced by impeller::ColorSourceContents::DrawGeometry(), impeller::GetShaderClipDepth(), GetShaderTransform(), impeller::SolidRRectLikeBlurContents::Render(), and impeller::TextContents::Render().

## ◆ GetShaderClipDepth() [2/2]

Scalar impeller::Entity::GetShaderClipDepth | ( | uint32_t | clip_depth | ) |
| static

Converts an integer clip depth level into a floating-point depth value suitable for use in shaders.

The integer `clip_depth` represents discrete layers used for stencil clipping. This function maps that integer to a depth value within the [0, 1) range for the depth buffer. Each increment in `clip_depth` corresponds to a small step (`kDepthEpsilon`) in the shader depth.

The result is clamped to ensure it stays within the valid depth range and slightly below 1.0 to avoid potential issues with the maximum depth value.

Parameters

  [in] | clip_depth | The integer clip depth level.

Returns
  The corresponding floating-point depth value for shaders.

Definition at line 94 of file entity.cc.

94 {
95 return std::clamp(clip_depth * kDepthEpsilon, 0.0f, 1.0f - kDepthEpsilon);
96 }
impeller::Entity::kDepthEpsilon
static constexpr Scalar kDepthEpsilon
Definition: entity.h:31

References kDepthEpsilon.

## ◆ GetShaderTransform() [1/2]

Matrix impeller::Entity::GetShaderTransform | ( | const RenderPass & | pass | ) | const

Calculates the final transformation matrix for rendering in a shader.

This combines the entity's transform with the render pass's orthographic projection and applies the necessary adjustments based on the entity's shader clip depth.

Parameters

  [in] | pass | The current render pass.

Returns
  The combined model-view-projection matrix for the shader.

Definition at line 50 of file entity.cc.

50 {
51 return Entity::GetShaderTransform(GetShaderClipDepth(), pass, transform_);
52 }
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Definition: entity.cc:50

References GetShaderClipDepth().

Referenced by impeller::Geometry::ComputePositionGeometry(), impeller::DlVerticesGeometry::GetPositionBuffer(), impeller::FillRectGeometry::GetPositionBuffer(), impeller::StrokeRectGeometry::GetPositionBuffer(), impeller::DlVerticesGeometry::GetPositionUVColorBuffer(), impeller::AtlasContents::Render(), impeller::ColorFilterAtlasContents::Render(), impeller::SolidRRectLikeBlurContents::Render(), impeller::TextContents::Render(), and impeller::TextureContents::Render().

## ◆ GetShaderTransform() [2/2]

Matrix impeller::Entity::GetShaderTransform | ( | Scalar | clip_depth,
|  | const RenderPass & | pass,
|  | const Matrix & | transform
| ) |  |
| static

Static utility that computes the vertex shader transform used for drawing an Entity with a given the clip depth and RenderPass size.

Definition at line 54 of file entity.cc.

56 {
57 return Matrix::MakeTranslateScale({1, 1, Entity::kDepthEpsilon},
58 {0, 0, shader_clip_depth}) *
59 pass.GetOrthographicTransform() * transform;
60 }
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:214
impeller::Matrix::MakeTranslateScale
static constexpr Matrix MakeTranslateScale(const Vector3 &s, const Vector3 &t)
Definition: matrix.h:113

References impeller::RenderPass::GetOrthographicTransform(), kDepthEpsilon, impeller::Matrix::MakeTranslateScale(), and transform.

## ◆ GetTransform()

const Matrix & impeller::Entity::GetTransform | ( |  | ) | const

Get the global transform matrix for this Entity.

Definition at line 46 of file entity.cc.

46 {
47 return transform_;
48 }

Referenced by impeller::SolidColorContents::AsBackgroundColor(), impeller::AtlasContents::GetCoverage(), impeller::ColorFilterAtlasContents::GetCoverage(), impeller::CircleContents::GetCoverage(), impeller::ColorSourceContents::GetCoverage(), impeller::FilterContents::GetCoverage(), impeller::LineContents::GetCoverage(), impeller::SolidColorContents::GetCoverage(), impeller::SolidRRectLikeBlurContents::GetCoverage(), impeller::TextContents::GetCoverage(), impeller::TextureContents::GetCoverage(), impeller::VerticesSimpleBlendContents::GetCoverage(), impeller::FilterContents::GetEntity(), impeller::FilterContentsFilterInput::GetLocalTransform(), impeller::CircleGeometry::GetPositionBuffer(), impeller::StrokeRectGeometry::GetPositionBuffer(), impeller::FilterInput::GetTransform(), impeller::FilterContentsFilterInput::GetTransform(), impeller::TextShadowCache::Lookup(), impeller::LineContents::Render(), impeller::SolidColorContents::Render(), impeller::SolidRRectLikeBlurContents::Render(), impeller::TextContents::Render(), impeller::TiledTextureContents::Render(), impeller::TextureContents::RenderToSnapshot(), impeller::Canvas::Restore(), SetInheritedOpacity(), and impeller::testing::TEST_P().

## ◆ IsBlendModeDestructive()

bool impeller::Entity::IsBlendModeDestructive | ( | BlendMode | blend_mode | ) |
| static

Returns true if the blend mode is "destructive", meaning that even fully transparent source colors would result in the destination getting changed.

This is useful for determining if EntityPass textures can be shrinkwrapped to their Entities' coverage; they can be shrinkwrapped if all of the contained Entities have non-destructive blends.

Definition at line 128 of file entity.cc.

128 {
129 switch (blend_mode) {
130 case BlendMode::kClear:
131 case BlendMode::kSrc:
132 case BlendMode::kSrcIn:
133 case BlendMode::kDstIn:
134 case BlendMode::kSrcOut:
135 case BlendMode::kDstOut:
136 case BlendMode::kDstATop:
137 case BlendMode::kXor:
138 case BlendMode::kModulate:
139 return true;
140 default:
141 return false;
142 }
143 }
impeller::BlendMode::kSrcOut
@ kSrcOut
impeller::BlendMode::kDstIn
@ kDstIn
impeller::BlendMode::kModulate
@ kModulate
impeller::BlendMode::kXor
@ kXor
impeller::BlendMode::kSrc
@ kSrc
impeller::BlendMode::kDstATop
@ kDstATop
impeller::BlendMode::kDstOut
@ kDstOut
impeller::BlendMode::kSrcIn
@ kSrcIn
impeller::BlendMode::kClear
@ kClear

References impeller::kClear, impeller::kDstATop, impeller::kDstIn, impeller::kDstOut, impeller::kModulate, impeller::kSrc, impeller::kSrcIn, impeller::kSrcOut, and impeller::kXor.

Referenced by impeller::Canvas::SaveLayer().

## ◆ operator=()

Entity & impeller::Entity::operator= | ( | Entity && |  | ) |
| default

## ◆ Render()

bool impeller::Entity::Render | ( | const ContentContext & | renderer,
|  | RenderPass & | parent_pass
| ) |  | const

Definition at line 145 of file entity.cc.

146 {
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 }

References impeller::RenderPass::GetRenderTargetSize(), and impeller::TRect< Scalar >::MakeSize().

Referenced by impeller::EntityPlayground::OpenPlaygroundHere(), impeller::PipelineBlend(), impeller::Canvas::Restore(), impeller::Canvas::SaveLayer(), and impeller::testing::TEST_P().

## ◆ SetBlendMode()

void impeller::Entity::SetBlendMode | ( | BlendMode | blend_mode | ) |

Definition at line 98 of file entity.cc.

98 {
99 blend_mode_ = blend_mode;
100 }

Referenced by impeller::Canvas::DrawArc(), impeller::Canvas::DrawAtlas(), impeller::Canvas::DrawCircle(), impeller::Canvas::DrawDashedLine(), impeller::Canvas::DrawDiffRoundRect(), impeller::Canvas::DrawImageRect(), impeller::Canvas::DrawLine(), impeller::Canvas::DrawOval(), impeller::Canvas::DrawPaint(), impeller::Canvas::DrawPath(), impeller::Canvas::DrawPoints(), impeller::Canvas::DrawRect(), impeller::Canvas::DrawRoundRect(), impeller::Canvas::DrawRoundSuperellipse(), impeller::Canvas::DrawTextFrame(), impeller::Canvas::DrawVertices(), FromSnapshot(), impeller::DlImageImpeller::MakeFromYUVTextures(), impeller::PipelineBlend(), impeller::Canvas::Restore(), impeller::Canvas::SaveLayer(), and impeller::testing::TEST_P().

## ◆ SetClipDepth()

void impeller::Entity::SetClipDepth | ( | uint32_t | clip_depth | ) |

Sets the stencil clip depth for this entity.

The clip depth determines the entity's level within a stack of stencil clips. Higher values indicate the entity is nested deeper within clips. This value is used during rendering to configure stencil buffer operations.

Parameters

  [in] | clip_depth | The integer clip depth level.

Definition at line 82 of file entity.cc.

82 {
83 clip_depth_ = clip_depth;
84 }

Referenced by impeller::Canvas::ClipGeometry(), impeller::Canvas::DrawTextFrame(), impeller::TextShadowCache::Lookup(), impeller::Canvas::Restore(), and impeller::Canvas::SaveLayer().

## ◆ SetContents()

void impeller::Entity::SetContents | ( | std::shared_ptr< Contents > | contents | ) |

Definition at line 74 of file entity.cc.

74 {
75 contents_ = std::move(contents);
76 }

Referenced by impeller::Canvas::DrawAtlas(), impeller::Canvas::DrawImageRect(), impeller::Canvas::DrawLine(), impeller::Canvas::DrawTextFrame(), impeller::Canvas::DrawVertices(), FromSnapshot(), impeller::PipelineBlend(), impeller::Canvas::Restore(), impeller::Canvas::SaveLayer(), and impeller::testing::TEST_P().

## ◆ SetInheritedOpacity()

bool impeller::Entity::SetInheritedOpacity | ( | Scalar | alpha | ) |

Definition at line 106 of file entity.cc.

106 {
107 if (alpha >= 1.0) {
108 return true;
109 }
110 if (blend_mode_ == BlendMode::kSrc && contents_->IsOpaque(GetTransform())) {
111 blend_mode_ = BlendMode::kSrcOver;
112 }
113 contents_->SetInheritedOpacity(alpha);
114 return true;
115 }
impeller::Entity::GetTransform
const Matrix & GetTransform() const
Get the global transform matrix for this Entity.
Definition: entity.cc:46
impeller::BlendMode::kSrcOver
@ kSrcOver

References GetTransform(), impeller::kSrc, and impeller::kSrcOver.

## ◆ SetTransform()

void impeller::Entity::SetTransform | ( | const Matrix & | transform | ) |

Set the global transform matrix for this Entity.

Definition at line 62 of file entity.cc.

62 {
63 transform_ = transform;
64 }

References transform.

Referenced by impeller::Canvas::ClipGeometry(), impeller::Canvas::DrawArc(), impeller::Canvas::DrawAtlas(), impeller::Canvas::DrawCircle(), impeller::Canvas::DrawDashedLine(), impeller::Canvas::DrawDiffRoundRect(), impeller::Canvas::DrawImageRect(), impeller::Canvas::DrawLine(), impeller::Canvas::DrawOval(), impeller::Canvas::DrawPaint(), impeller::Canvas::DrawPath(), impeller::Canvas::DrawPoints(), impeller::Canvas::DrawRect(), impeller::Canvas::DrawRoundRect(), impeller::Canvas::DrawRoundSuperellipse(), impeller::Canvas::DrawTextFrame(), impeller::Canvas::DrawVertices(), FromSnapshot(), impeller::FilterContents::GetCoverage(), impeller::FilterContents::GetEntity(), impeller::FilterInput::GetLocalCoverage(), impeller::TextShadowCache::Lookup(), impeller::Canvas::Restore(), impeller::Canvas::SaveLayer(), impeller::testing::TEST(), and impeller::testing::TEST_P().

## Member Data Documentation

## ◆ kDepthEpsilon

constexpr Scalar impeller::Entity::kDepthEpsilon = 1.0f / 262144.0
| static constexpr

Definition at line 31 of file entity.h.

Referenced by GetShaderClipDepth(), and GetShaderTransform().

## ◆ kLastAdvancedBlendMode

constexpr BlendMode impeller::Entity::kLastAdvancedBlendMode = BlendMode::kLuminosity
| static constexpr

Definition at line 29 of file entity.h.

Referenced by impeller::testing::GetBlendModeSelection(), impeller::ColorFilterContents::MakeBlend(), and impeller::BlendFilterContents::SetBlendMode().

## ◆ kLastPipelineBlendMode

constexpr BlendMode impeller::Entity::kLastPipelineBlendMode = BlendMode::kModulate
| static constexpr

Definition at line 28 of file entity.h.

Referenced by impeller::ContentContextOptions::ApplyToPipelineDescriptor(), impeller::ColorFilterContents::MakeBlend(), impeller::VerticesSimpleBlendContents::Render(), impeller::RequiresReadbackForBlends(), impeller::Canvas::Restore(), impeller::BlendFilterContents::SetBlendMode(), and impeller::testing::TEST_P().

The documentation for this class was generated from the following files:

- impeller/entity/entity.h
- impeller/entity/entity.cc