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 21 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 230 of file texture_contents.cc.

230  {
231  return sampler_descriptor_;
232 }

◆ GetSourceRect()

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

Definition at line 214 of file texture_contents.cc.

214  {
215  return source_rect_;
216 }

◆ GetStrictSourceRect()

bool impeller::TextureContents::GetStrictSourceRect ( ) const

Definition at line 222 of file texture_contents.cc.

222  {
223  return strict_source_rect_enabled_;
224 }

◆ 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(), 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  auto capture = entity.GetCapture().CreateChild("TextureContents");
113 
114  using VS = TextureFillVertexShader;
115  using FS = TextureFillFragmentShader;
116  using FSStrictSrc = TextureFillStrictSrcFragmentShader;
117  using FSExternal = TextureFillExternalFragmentShader;
118 
119  if (destination_rect_.IsEmpty() || source_rect_.IsEmpty() ||
120  texture_ == nullptr || texture_->GetSize().IsEmpty()) {
121  return true; // Nothing to render.
122  }
123 
124  bool is_external_texture =
125  texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
126 
127  auto source_rect = capture.AddRect("Source rect", source_rect_);
128  auto texture_coords =
129  Rect::MakeSize(texture_->GetSize()).Project(source_rect);
130 
131  VertexBufferBuilder<VS::PerVertexData> vertex_builder;
132 
133  auto destination_rect =
134  capture.AddRect("Destination rect", destination_rect_);
135  vertex_builder.AddVertices({
136  {destination_rect.GetLeftTop(), texture_coords.GetLeftTop()},
137  {destination_rect.GetRightTop(), texture_coords.GetRightTop()},
138  {destination_rect.GetLeftBottom(), texture_coords.GetLeftBottom()},
139  {destination_rect.GetRightBottom(), texture_coords.GetRightBottom()},
140  });
141 
142  auto& host_buffer = renderer.GetTransientsBuffer();
143 
144  VS::FrameInfo frame_info;
145  frame_info.mvp = entity.GetShaderTransform(pass);
146  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
147  frame_info.alpha = capture.AddScalar("Alpha", GetOpacity());
148 
149 #ifdef IMPELLER_DEBUG
150  if (label_.empty()) {
151  pass.SetCommandLabel("Texture Fill");
152  } else {
153  pass.SetCommandLabel("Texture Fill: " + label_);
154  }
155 #endif // IMPELLER_DEBUG
156 
157  auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
158  if (!stencil_enabled_) {
159  pipeline_options.stencil_mode = ContentContextOptions::StencilMode::kIgnore;
160  }
161  pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
162 
163  std::shared_ptr<Pipeline<PipelineDescriptor>> pipeline;
164 #ifdef IMPELLER_ENABLE_OPENGLES
165  if (is_external_texture) {
166  pipeline = renderer.GetTextureExternalPipeline(pipeline_options);
167  }
168 #endif // IMPELLER_ENABLE_OPENGLES
169 
170  if (!pipeline) {
171  if (strict_source_rect_enabled_) {
172  pipeline = renderer.GetTextureStrictSrcPipeline(pipeline_options);
173  } else {
174  pipeline = renderer.GetTexturePipeline(pipeline_options);
175  }
176  }
177  pass.SetPipeline(pipeline);
178 
179  pass.SetStencilReference(entity.GetClipDepth());
180  pass.SetVertexBuffer(vertex_builder.CreateVertexBuffer(host_buffer));
181  VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
182  if (is_external_texture) {
183  FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
184  pass, texture_,
185  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
186  sampler_descriptor_));
187  } else if (strict_source_rect_enabled_) {
188  // For a strict source rect, shrink the texture coordinate range by half a
189  // texel to ensure that linear filtering does not sample anything outside
190  // the source rect bounds.
191  auto strict_texture_coords =
192  Rect::MakeSize(texture_->GetSize()).Project(source_rect.Expand(-0.5));
193 
194  FSStrictSrc::FragInfo frag_info;
195  frag_info.source_rect = Vector4(strict_texture_coords.GetLTRB());
196  FSStrictSrc::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
197  FSStrictSrc::BindTextureSampler(
198  pass, texture_,
199  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
200  sampler_descriptor_));
201  } else {
202  FS::BindTextureSampler(
203  pass, texture_,
204  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
205  sampler_descriptor_));
206  }
207  return pass.Draw().ok();
208 }

References impeller::VertexBufferBuilder< VertexType_, IndexType_ >::AddVertices(), impeller::Capture::CreateChild(), impeller::VertexBufferBuilder< VertexType_, IndexType_ >::CreateVertexBuffer(), impeller::RenderPass::Draw(), impeller::Entity::GetCapture(), impeller::Entity::GetClipDepth(), impeller::ContentContext::GetContext(), GetOpacity(), 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(), impeller::RenderPass::SetStencilReference(), 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 234 of file texture_contents.cc.

234  {
235  defer_applying_opacity_ = defer_applying_opacity;
236 }

◆ 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 226 of file texture_contents.cc.

226  {
227  sampler_descriptor_ = std::move(desc);
228 }

◆ SetSourceRect()

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

Definition at line 210 of file texture_contents.cc.

210  {
211  source_rect_ = source_rect;
212 }

◆ 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 218 of file texture_contents.cc.

218  {
219  strict_source_rect_enabled_ = strict;
220 }

◆ 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
Turn the stencil test off. Used when drawing without stencil-then-cover.
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:66
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:405
impeller::Vector2
Point Vector2
Definition: point.h:320
impeller::Size
TSize< Scalar > Size
Definition: size.h:137
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
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:264
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:15
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:37
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
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:598
scale
const Scalar scale
Definition: stroke_path_geometry.cc:297
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104