Flutter Impeller
impeller::TiledTextureContents Class Referencefinal

#include <tiled_texture_contents.h>

Inheritance diagram for impeller::TiledTextureContents:
impeller::ColorSourceContents impeller::Contents

Public Types

using ColorFilterProc = std::function< std::shared_ptr< ColorFilterContents >(FilterInput::Ref)>
 
- 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)>
 

Public Member Functions

 TiledTextureContents ()
 
 ~TiledTextureContents () override
 
bool IsOpaque () const override
 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...
 
bool Render (const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
 
void SetTexture (std::shared_ptr< Texture > texture)
 
void SetTileModes (Entity::TileMode x_tile_mode, Entity::TileMode y_tile_mode)
 
void SetSamplerDescriptor (SamplerDescriptor desc)
 
void SetColorFilter (ColorFilterProc color_filter)
 Set a color filter to apply directly to this tiled texture. 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, const std::string &label="Tiled Texture Snapshot") const override
 Render this contents to a snapshot, respecting the entity's transform, path, stencil depth, and blend mode. The result texture size is always the size of GetCoverage(entity). More...
 
- Public Member Functions inherited from impeller::ColorSourceContents
 ColorSourceContents ()
 
 ~ColorSourceContents () override
 
void SetGeometry (std::shared_ptr< Geometry > geometry)
 Set the geometry that this contents will use to render. More...
 
const std::shared_ptr< Geometry > & GetGeometry () const
 Get the geometry that this contents will use to render. More...
 
void SetEffectTransform (Matrix matrix)
 Set the effect transform for this color source. More...
 
const MatrixGetInverseEffectTransform () const
 Set the inverted effect transform for this color source. More...
 
void SetOpacityFactor (Scalar opacity)
 Set the opacity factor for this color source. More...
 
Scalar GetOpacityFactor () const
 Get the opacity factor for this color source. More...
 
virtual bool IsSolidColor () const
 
std::optional< RectGetCoverage (const Entity &entity) const override
 Get the screen space bounding rectangle that this contents affects. More...
 
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...
 
- 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 StencilCoverage GetStencilCoverage (const Entity &entity, const std::optional< Rect > &current_stencil_coverage) const
 Given the current screen space bounding rectangle of the stencil, return the expected stencil coverage after this draw call. This should only be implemented for contents that may write to the stencil buffer. More...
 
virtual bool ShouldRender (const Entity &entity, const std::optional< Rect > &stencil_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...
 

Additional Inherited Members

- Static Public Member Functions inherited from impeller::Contents
static std::shared_ptr< ContentsMakeAnonymous (RenderProc render_proc, CoverageProc coverage_proc)
 

Detailed Description

Definition at line 21 of file tiled_texture_contents.h.

Member Typedef Documentation

◆ ColorFilterProc

Definition at line 28 of file tiled_texture_contents.h.

Constructor & Destructor Documentation

◆ TiledTextureContents()

impeller::TiledTextureContents::TiledTextureContents ( )
default

◆ ~TiledTextureContents()

impeller::TiledTextureContents::~TiledTextureContents ( )
overridedefault

Member Function Documentation

◆ IsOpaque()

bool impeller::TiledTextureContents::IsOpaque ( ) const
overridevirtual

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.

Reimplemented from impeller::Contents.

Definition at line 102 of file tiled_texture_contents.cc.

102  {
103  if (GetOpacityFactor() < 1 || x_tile_mode_ == Entity::TileMode::kDecal ||
104  y_tile_mode_ == Entity::TileMode::kDecal) {
105  return false;
106  }
107  if (color_filter_) {
108  return false;
109  }
110  return texture_->IsOpaque();
111 }

References impeller::ColorSourceContents::GetOpacityFactor(), and impeller::Entity::kDecal.

Referenced by impeller::testing::TEST_P().

◆ Render()

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

Implements impeller::Contents.

Definition at line 113 of file tiled_texture_contents.cc.

115  {
116  if (texture_ == nullptr) {
117  return true;
118  }
119 
120  using VS = TextureFillVertexShader;
121  using FS = TiledTextureFillFragmentShader;
122 
123  const auto texture_size = texture_->GetSize();
124  if (texture_size.IsEmpty()) {
125  return true;
126  }
127 
128  auto& host_buffer = pass.GetTransientsBuffer();
129 
130  auto geometry_result = GetGeometry()->GetPositionUVBuffer(
131  Rect({0, 0}, Size(texture_size)), GetInverseEffectTransform(), renderer,
132  entity, pass);
133  bool uses_emulated_tile_mode =
134  UsesEmulatedTileMode(renderer.GetDeviceCapabilities());
135 
136  VS::FrameInfo frame_info;
137  frame_info.mvp = geometry_result.transform;
138  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
139  frame_info.alpha = GetOpacityFactor();
140 
141  Command cmd;
142  if (uses_emulated_tile_mode) {
143  DEBUG_COMMAND_INFO(cmd, "TiledTextureFill");
144  } else {
145  DEBUG_COMMAND_INFO(cmd, "TextureFill");
146  }
147 
148  cmd.stencil_reference = entity.GetStencilDepth();
149 
150  auto options = OptionsFromPassAndEntity(pass, entity);
151  if (geometry_result.prevent_overdraw) {
152  options.stencil_compare = CompareFunction::kEqual;
153  options.stencil_operation = StencilOperation::kIncrementClamp;
154  }
155  options.primitive_type = geometry_result.type;
156  cmd.pipeline = uses_emulated_tile_mode
157  ? renderer.GetTiledTexturePipeline(options)
158  : renderer.GetTexturePipeline(options);
159 
160  cmd.BindVertices(geometry_result.vertex_buffer);
161  VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
162 
163  if (uses_emulated_tile_mode) {
164  FS::FragInfo frag_info;
165  frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
166  frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
167  FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
168  }
169 
170  if (color_filter_) {
171  auto filtered_texture = CreateFilterTexture(renderer);
172  if (!filtered_texture) {
173  return false;
174  }
175  FS::BindTextureSampler(
176  cmd, filtered_texture,
177  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
178  CreateDescriptor(renderer.GetDeviceCapabilities())));
179  } else {
180  FS::BindTextureSampler(
181  cmd, texture_,
182  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
183  CreateDescriptor(renderer.GetDeviceCapabilities())));
184  }
185 
186  if (!pass.AddCommand(std::move(cmd))) {
187  return false;
188  }
189 
190  if (geometry_result.prevent_overdraw) {
191  auto restore = ClipRestoreContents();
192  restore.SetRestoreCoverage(GetCoverage(entity));
193  return restore.Render(renderer, entity, pass);
194  }
195  return true;
196 }

References impeller::RenderPass::AddCommand(), impeller::Command::BindVertices(), DEBUG_COMMAND_INFO, impeller::ContentContext::GetContext(), impeller::ColorSourceContents::GetCoverage(), impeller::ContentContext::GetDeviceCapabilities(), impeller::ColorSourceContents::GetGeometry(), impeller::ColorSourceContents::GetInverseEffectTransform(), impeller::ColorSourceContents::GetOpacityFactor(), impeller::ContentContext::GetTexturePipeline(), impeller::ContentContext::GetTiledTexturePipeline(), impeller::RenderPass::GetTransientsBuffer(), impeller::kEqual, impeller::kIncrementClamp, impeller::OptionsFromPassAndEntity(), impeller::Command::pipeline, and impeller::Command::stencil_reference.

Referenced by impeller::SceneContents::Render().

◆ RenderToSnapshot()

std::optional< Snapshot > impeller::TiledTextureContents::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,
const std::string &  label = "Tiled Texture Snapshot" 
) const
overridevirtual

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

Reimplemented from impeller::Contents.

Definition at line 198 of file tiled_texture_contents.cc.

204  {
205  if (GetInverseEffectTransform().IsIdentity()) {
206  return Snapshot{
207  .texture = texture_,
208  .transform = entity.GetTransformation(),
209  .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
210  .opacity = GetOpacityFactor(),
211  };
212  }
213 
215  renderer, // renderer
216  entity, // entity
217  std::nullopt, // coverage_limit
218  sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
219  true, // msaa_enabled
220  label); // label
221 }

References impeller::ColorSourceContents::GetInverseEffectTransform(), impeller::ColorSourceContents::GetOpacityFactor(), impeller::Entity::GetTransformation(), impeller::Contents::RenderToSnapshot(), and impeller::Snapshot::texture.

◆ SetColorFilter()

void impeller::TiledTextureContents::SetColorFilter ( ColorFilterProc  color_filter)

Set a color filter to apply directly to this tiled texture.

Parameters
color_filterWhen applying a color filter to a tiled texture, we can reduce the size and number of the subpasses required and the shader workloadby applying the filter to the untiled image and absorbing the opacity before tiling it into the final location.

This may not be a performance improvement if the image is tiled into a much smaller size that its original texture size.

Definition at line 58 of file tiled_texture_contents.cc.

58  {
59  color_filter_ = std::move(color_filter);
60 }

◆ SetSamplerDescriptor()

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

Definition at line 54 of file tiled_texture_contents.cc.

54  {
55  sampler_descriptor_ = std::move(desc);
56 }

◆ SetTexture()

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

Definition at line 44 of file tiled_texture_contents.cc.

44  {
45  texture_ = std::move(texture);
46 }

Referenced by impeller::SceneContents::Render(), and impeller::testing::TEST_P().

◆ SetTileModes()

void impeller::TiledTextureContents::SetTileModes ( Entity::TileMode  x_tile_mode,
Entity::TileMode  y_tile_mode 
)

Definition at line 48 of file tiled_texture_contents.cc.

49  {
50  x_tile_mode_ = x_tile_mode;
51  y_tile_mode_ = y_tile_mode;
52 }

The documentation for this class was generated from the following files:
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:29
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:31
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::Entity::TileMode::kDecal
@ kDecal
impeller::ColorSourceContents::GetGeometry
const std::shared_ptr< Geometry > & GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:21
impeller::Size
TSize< Scalar > Size
Definition: size.h:135
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:30
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:306
impeller::StencilOperation::kIncrementClamp
@ kIncrementClamp
Increment the current stencil value by 1. Clamp it to the maximum.
impeller::ColorSourceContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the screen space bounding rectangle that this contents affects.
Definition: color_source_contents.cc:45
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
impeller::ColorSourceContents::GetInverseEffectTransform
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
Definition: color_source_contents.cc:37
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, const std::string &label="Snapshot") const
Render this contents to a snapshot, respecting the entity's transform, path, stencil depth,...
Definition: contents.cc:59