Flutter Impeller
impeller::TextureContents Class Referencefinal

#include <texture_contents.h>

Inheritance diagram for impeller::TextureContents:
impeller::Contents

Public Member Functions

 TextureContents ()
 
 ~TextureContents () override
 
void SetLabel (std::string label)
 
void SetDestinationRect (Rect rect)
 
void SetTexture (std::shared_ptr< Texture > texture)
 
std::shared_ptr< TextureGetTexture () const
 
void SetSamplerDescriptor (SamplerDescriptor desc)
 
const SamplerDescriptorGetSamplerDescriptor () const
 
void SetSourceRect (const Rect &source_rect)
 
const RectGetSourceRect () const
 
void SetStrictSourceRect (bool strict)
 
bool GetStrictSourceRect () const
 
void SetOpacity (Scalar opacity)
 
Scalar GetOpacity () const
 
void SetStencilEnabled (bool enabled)
 
std::optional< RectGetCoverage (const Entity &entity) const override
 Get the area of the render pass that will be affected when this contents is rendered. More...
 
std::optional< SnapshotRenderToSnapshot (const ContentContext &renderer, const Entity &entity, std::optional< Rect > coverage_limit=std::nullopt, const std::optional< SamplerDescriptor > &sampler_descriptor=std::nullopt, bool msaa_enabled=true, int32_t mip_count=1, const std::string &label="Texture Snapshot") const override
 Render this contents to a snapshot, respecting the entity's transform, path, clip depth, and blend mode. The result texture size is always the size of GetCoverage(entity). More...
 
bool Render (const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
 
bool CanInheritOpacity (const Entity &entity) const override
 Whether or not this contents can accept the opacity peephole optimization. More...
 
void SetInheritedOpacity (Scalar opacity) override
 Inherit the provided opacity. More...
 
void SetDeferApplyingOpacity (bool defer_applying_opacity)
 
- Public Member Functions inherited from impeller::Contents
 Contents ()
 
virtual ~Contents ()
 
virtual void PopulateGlyphAtlas (const std::shared_ptr< LazyGlyphAtlas > &lazy_glyph_atlas, Scalar scale)
 Add any text data to the specified lazy atlas. The scale parameter must be used again later when drawing the text. More...
 
void SetCoverageHint (std::optional< Rect > coverage_hint)
 Hint that specifies the coverage area of this Contents that will actually be used during rendering. This is for optimization purposes only and can not be relied on as a clip. May optionally affect the result of GetCoverage(). More...
 
const std::optional< Rect > & GetCoverageHint () const
 
virtual bool IsOpaque () const
 Whether this Contents only emits opaque source colors from the fragment stage. This value does not account for any entity properties (e.g. the blend mode), clips/visibility culling, or inherited opacity. More...
 
virtual ClipCoverage GetClipCoverage (const Entity &entity, const std::optional< Rect > &current_clip_coverage) const
 Given the current pass space bounding rectangle of the clip buffer, return the expected clip coverage after this draw call. This should only be implemented for contents that may write to the clip buffer. More...
 
virtual bool ShouldRender (const Entity &entity, const std::optional< Rect > clip_coverage) const
 
std::optional< SizeGetColorSourceSize () const
 Return the color source's intrinsic size, if available. More...
 
void SetColorSourceSize (Size size)
 
virtual std::optional< ColorAsBackgroundColor (const Entity &entity, ISize target_size) const
 Returns a color if this Contents will flood the given target_size with a color. This output color is the "Source" color that will be used for the Entity's blend operation. More...
 
virtual const FilterContentsAsFilter () const
 Cast to a filter. Returns nullptr if this Contents is not a filter. More...
 
virtual bool ApplyColorFilter (const ColorFilterProc &color_filter_proc)
 If possible, applies a color filter to this contents inputs on the CPU. More...
 

Static Public Member Functions

static std::shared_ptr< TextureContentsMakeRect (Rect destination)
 A common case factory that marks the texture contents as having a destination rectangle. In this situation, a subpass can be avoided when image filters are applied. More...
 
- Static Public Member Functions inherited from impeller::Contents
static std::shared_ptr< ContentsMakeAnonymous (RenderProc render_proc, CoverageProc coverage_proc)
 

Additional Inherited Members

- Public Types inherited from impeller::Contents
using ColorFilterProc = std::function< Color(Color)>
 
using RenderProc = std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)>
 
using CoverageProc = std::function< std::optional< Rect >(const Entity &entity)>
 

Detailed Description

Definition at line 17 of file texture_contents.h.

Constructor & Destructor Documentation

◆ TextureContents()

impeller::TextureContents::TextureContents ( )
default

◆ ~TextureContents()

impeller::TextureContents::~TextureContents ( )
overridedefault

Member Function Documentation

◆ CanInheritOpacity()

bool impeller::TextureContents::CanInheritOpacity ( const Entity entity) const
overridevirtual

Whether or not this contents can accept the opacity peephole optimization.

By default all contents return false. Contents are responsible for determining whether or not their own geometries intersect in a way that makes accepting opacity impossible. It is always safe to return false, especially if computing overlap would be computationally expensive.

Reimplemented from impeller::Contents.

Definition at line 57 of file texture_contents.cc.

57  {
58  return true;
59 }

◆ GetCoverage()

std::optional< Rect > impeller::TextureContents::GetCoverage ( const Entity entity) const
overridevirtual

Get the area of the render pass that will be affected when this contents is rendered.

During rendering, coverage coordinates count pixels from the top left corner of the framebuffer.

Returns
The coverage rectangle. An std::nullopt result means that rendering this contents has no effect on the output color.

Implements impeller::Contents.

Definition at line 69 of file texture_contents.cc.

69  {
70  if (GetOpacity() == 0) {
71  return std::nullopt;
72  }
73  return destination_rect_.TransformBounds(entity.GetTransform());
74 };

References GetOpacity(), impeller::Entity::GetTransform(), and impeller::TRect< T >::TransformBounds().

◆ GetOpacity()

Scalar impeller::TextureContents::GetOpacity ( ) const

Definition at line 65 of file texture_contents.cc.

65  {
66  return opacity_ * inherited_opacity_;
67 }

Referenced by GetCoverage(), Render(), and RenderToSnapshot().

◆ GetSamplerDescriptor()

const SamplerDescriptor & impeller::TextureContents::GetSamplerDescriptor ( ) const

Definition at line 210 of file texture_contents.cc.

210  {
211  return sampler_descriptor_;
212 }

◆ GetSourceRect()

const Rect & impeller::TextureContents::GetSourceRect ( ) const

Definition at line 194 of file texture_contents.cc.

194  {
195  return source_rect_;
196 }

◆ GetStrictSourceRect()

bool impeller::TextureContents::GetStrictSourceRect ( ) const

Definition at line 202 of file texture_contents.cc.

202  {
203  return strict_source_rect_enabled_;
204 }

◆ GetTexture()

std::shared_ptr< Texture > impeller::TextureContents::GetTexture ( ) const

Definition at line 45 of file texture_contents.cc.

45  {
46  return texture_;
47 }

◆ MakeRect()

std::shared_ptr< TextureContents > impeller::TextureContents::MakeRect ( Rect  destination)
static

A common case factory that marks the texture contents as having a destination rectangle. In this situation, a subpass can be avoided when image filters are applied.

Definition at line 27 of file texture_contents.cc.

27  {
28  auto contents = std::make_shared<TextureContents>();
29  contents->destination_rect_ = destination;
30  return contents;
31 }

Referenced by impeller::PaintPassDelegate::CreateContentsForSubpassTarget(), impeller::Entity::FromSnapshot(), impeller::EntityPass::Render(), impeller::ExperimentalCanvas::SaveLayer(), and impeller::testing::TEST_P().

◆ Render()

bool impeller::TextureContents::Render ( const ContentContext renderer,
const Entity entity,
RenderPass pass 
) const
overridevirtual

Implements impeller::Contents.

Definition at line 109 of file texture_contents.cc.

111  {
112  using VS = TextureFillVertexShader;
113  using FS = TextureFillFragmentShader;
114  using FSStrict = TextureFillStrictSrcFragmentShader;
115 
116  if (destination_rect_.IsEmpty() || source_rect_.IsEmpty() ||
117  texture_ == nullptr || texture_->GetSize().IsEmpty()) {
118  return true; // Nothing to render.
119  }
120 
121  [[maybe_unused]] bool is_external_texture =
122  texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
123  FML_DCHECK(!is_external_texture);
124 
125  auto texture_coords =
126  Rect::MakeSize(texture_->GetSize()).Project(source_rect_);
127 
128  VertexBufferBuilder<VS::PerVertexData> vertex_builder;
129  vertex_builder.AddVertices({
130  {destination_rect_.GetLeftTop(), texture_coords.GetLeftTop()},
131  {destination_rect_.GetRightTop(), texture_coords.GetRightTop()},
132  {destination_rect_.GetLeftBottom(), texture_coords.GetLeftBottom()},
133  {destination_rect_.GetRightBottom(), texture_coords.GetRightBottom()},
134  });
135 
136  auto& host_buffer = renderer.GetTransientsBuffer();
137 
138  VS::FrameInfo frame_info;
139  frame_info.mvp = entity.GetShaderTransform(pass);
140  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
141 
142 #ifdef IMPELLER_DEBUG
143  if (label_.empty()) {
144  pass.SetCommandLabel("Texture Fill");
145  } else {
146  pass.SetCommandLabel("Texture Fill: " + label_);
147  }
148 #endif // IMPELLER_DEBUG
149 
150  auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
151  if (!stencil_enabled_) {
152  pipeline_options.stencil_mode = ContentContextOptions::StencilMode::kIgnore;
153  }
154  pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
155 
156  pass.SetPipeline(strict_source_rect_enabled_
157  ? renderer.GetTextureStrictSrcPipeline(pipeline_options)
158  : renderer.GetTexturePipeline(pipeline_options));
159 
160  pass.SetVertexBuffer(vertex_builder.CreateVertexBuffer(host_buffer));
161  VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
162 
163  if (strict_source_rect_enabled_) {
164  // For a strict source rect, shrink the texture coordinate range by half a
165  // texel to ensure that linear filtering does not sample anything outside
166  // the source rect bounds.
167  auto strict_texture_coords =
168  Rect::MakeSize(texture_->GetSize()).Project(source_rect_.Expand(-0.5));
169 
170  FSStrict::FragInfo frag_info;
171  frag_info.source_rect = Vector4(strict_texture_coords.GetLTRB());
172  frag_info.alpha = GetOpacity();
173  FSStrict::BindFragInfo(pass, host_buffer.EmplaceUniform((frag_info)));
174  FSStrict::BindTextureSampler(
175  pass, texture_,
176  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
177  sampler_descriptor_));
178  } else {
179  FS::FragInfo frag_info;
180  frag_info.alpha = GetOpacity();
181  FS::BindFragInfo(pass, host_buffer.EmplaceUniform((frag_info)));
182  FS::BindTextureSampler(
183  pass, texture_,
184  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
185  sampler_descriptor_));
186  }
187  return pass.Draw().ok();
188 }

References impeller::VertexBufferBuilder< VertexType_, IndexType_ >::AddVertices(), impeller::VertexBufferBuilder< VertexType_, IndexType_ >::CreateVertexBuffer(), impeller::RenderPass::Draw(), impeller::TRect< T >::Expand(), impeller::ContentContext::GetContext(), impeller::TRect< T >::GetLeftBottom(), impeller::TRect< T >::GetLeftTop(), GetOpacity(), impeller::TRect< T >::GetRightBottom(), impeller::TRect< T >::GetRightTop(), impeller::Entity::GetShaderTransform(), impeller::ContentContext::GetTexturePipeline(), impeller::ContentContext::GetTextureStrictSrcPipeline(), impeller::ContentContext::GetTransientsBuffer(), impeller::TRect< T >::IsEmpty(), impeller::ContentContextOptions::kIgnore, impeller::kTextureExternalOES, impeller::kTriangleStrip, impeller::TRect< Scalar >::MakeSize(), impeller::OptionsFromPassAndEntity(), impeller::TRect< T >::Project(), impeller::RenderPass::SetCommandLabel(), impeller::RenderPass::SetPipeline(), and impeller::RenderPass::SetVertexBuffer().

◆ RenderToSnapshot()

std::optional< Snapshot > impeller::TextureContents::RenderToSnapshot ( const ContentContext renderer,
const Entity entity,
std::optional< Rect coverage_limit = std::nullopt,
const std::optional< SamplerDescriptor > &  sampler_descriptor = std::nullopt,
bool  msaa_enabled = true,
int32_t  mip_count = 1,
const std::string &  label = "Texture Snapshot" 
) const
overridevirtual

Render this contents to a snapshot, respecting the entity's transform, path, clip depth, and blend mode. The result texture size is always the size of GetCoverage(entity).

Reimplemented from impeller::Contents.

Definition at line 76 of file texture_contents.cc.

83  {
84  // Passthrough textures that have simple rectangle paths and complete source
85  // rects.
86  auto bounds = destination_rect_;
87  auto opacity = GetOpacity();
88  if (source_rect_ == Rect::MakeSize(texture_->GetSize()) &&
89  (opacity >= 1 - kEhCloseEnough || defer_applying_opacity_)) {
90  auto scale = Vector2(bounds.GetSize() / Size(texture_->GetSize()));
91  return Snapshot{
92  .texture = texture_,
93  .transform = entity.GetTransform() *
94  Matrix::MakeTranslation(bounds.GetOrigin()) *
96  .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
97  .opacity = opacity};
98  }
100  renderer, // renderer
101  entity, // entity
102  std::nullopt, // coverage_limit
103  sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
104  true, // msaa_enabled
105  /*mip_count=*/mip_count,
106  label); // label
107 }

References GetOpacity(), impeller::Entity::GetTransform(), impeller::kEhCloseEnough, impeller::Matrix::MakeScale(), impeller::TRect< Scalar >::MakeSize(), impeller::Matrix::MakeTranslation(), impeller::Contents::RenderToSnapshot(), scale, and impeller::Snapshot::texture.

◆ SetDeferApplyingOpacity()

void impeller::TextureContents::SetDeferApplyingOpacity ( bool  defer_applying_opacity)

Definition at line 214 of file texture_contents.cc.

214  {
215  defer_applying_opacity_ = defer_applying_opacity;
216 }

◆ SetDestinationRect()

void impeller::TextureContents::SetDestinationRect ( Rect  rect)

Definition at line 37 of file texture_contents.cc.

37  {
38  destination_rect_ = rect;
39 }

◆ SetInheritedOpacity()

void impeller::TextureContents::SetInheritedOpacity ( Scalar  opacity)
overridevirtual

Inherit the provided opacity.

   Use of this method is invalid if CanAcceptOpacity returns false.

Reimplemented from impeller::Contents.

Definition at line 61 of file texture_contents.cc.

61  {
62  inherited_opacity_ = opacity;
63 }

◆ SetLabel()

void impeller::TextureContents::SetLabel ( std::string  label)

Definition at line 33 of file texture_contents.cc.

33  {
34  label_ = std::move(label);
35 }

◆ SetOpacity()

void impeller::TextureContents::SetOpacity ( Scalar  opacity)

Definition at line 49 of file texture_contents.cc.

49  {
50  opacity_ = opacity;
51 }

◆ SetSamplerDescriptor()

void impeller::TextureContents::SetSamplerDescriptor ( SamplerDescriptor  desc)

Definition at line 206 of file texture_contents.cc.

206  {
207  sampler_descriptor_ = std::move(desc);
208 }

◆ SetSourceRect()

void impeller::TextureContents::SetSourceRect ( const Rect source_rect)

Definition at line 190 of file texture_contents.cc.

190  {
191  source_rect_ = source_rect;
192 }

◆ SetStencilEnabled()

void impeller::TextureContents::SetStencilEnabled ( bool  enabled)

Definition at line 53 of file texture_contents.cc.

53  {
54  stencil_enabled_ = enabled;
55 }

◆ SetStrictSourceRect()

void impeller::TextureContents::SetStrictSourceRect ( bool  strict)

Definition at line 198 of file texture_contents.cc.

198  {
199  strict_source_rect_enabled_ = strict;
200 }

◆ SetTexture()

void impeller::TextureContents::SetTexture ( std::shared_ptr< Texture texture)

Definition at line 41 of file texture_contents.cc.

41  {
42  texture_ = std::move(texture);
43 }

The documentation for this class was generated from the following files:
impeller::ContentContextOptions::StencilMode::kIgnore
@ kIgnore
impeller::TextureType::kTextureExternalOES
@ kTextureExternalOES
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:56
impeller::Contents::RenderToSnapshot
virtual std::optional< Snapshot > RenderToSnapshot(const ContentContext &renderer, const Entity &entity, std::optional< Rect > coverage_limit=std::nullopt, const std::optional< SamplerDescriptor > &sampler_descriptor=std::nullopt, bool msaa_enabled=true, int32_t mip_count=1, const std::string &label="Snapshot") const
Render this contents to a snapshot, respecting the entity's transform, path, clip depth,...
Definition: contents.cc:63
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:463
impeller::TRect::GetLeftTop
constexpr TPoint< T > GetLeftTop() const
Definition: rect.h:349
impeller::Vector2
Point Vector2
Definition: point.h:326
impeller::Size
TSize< Scalar > Size
Definition: size.h:137
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
impeller::TRect::GetRightTop
constexpr TPoint< T > GetRightTop() const
Definition: rect.h:353
impeller::TextureContents::GetOpacity
Scalar GetOpacity() const
Definition: texture_contents.cc:65
impeller::TRect::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: rect.h:287
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::TRect::GetLeftBottom
constexpr TPoint< T > GetLeftBottom() const
Definition: rect.h:357
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146
impeller::TRect::Project
constexpr TRect< T > Project(TRect< T > source) const
Returns a new rectangle that represents the projection of the source rectangle onto this rectangle....
Definition: rect.h:656
scale
const Scalar scale
Definition: stroke_path_geometry.cc:308
impeller::TRect::GetRightBottom
constexpr TPoint< T > GetRightBottom() const
Definition: rect.h:361
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::TRect::Expand
constexpr TRect< T > Expand(T left, T top, T right, T bottom) const
Returns a rectangle with expanded edges. Negative expansion results in shrinking.
Definition: rect.h:605