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, clip 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 area of the render pass that will be affected when this contents is rendered. 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 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...
 

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 22 of file tiled_texture_contents.h.

Member Typedef Documentation

◆ ColorFilterProc

Definition at line 29 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 101 of file tiled_texture_contents.cc.

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

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 112 of file tiled_texture_contents.cc.

114  {
115  if (texture_ == nullptr) {
116  return true;
117  }
118 
119  using VS = TextureFillVertexShader;
120  using FS = TiledTextureFillFragmentShader;
121  using FSExternal = TiledTextureFillExternalFragmentShader;
122 
123  const auto texture_size = texture_->GetSize();
124  if (texture_size.IsEmpty()) {
125  return true;
126  }
127 
128  bool is_external_texture =
129  texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
130 
131  auto& host_buffer = pass.GetTransientsBuffer();
132 
133  auto geometry_result = GetGeometry()->GetPositionUVBuffer(
134  Rect::MakeSize(texture_size), GetInverseEffectTransform(), renderer,
135  entity, pass);
136  bool uses_emulated_tile_mode =
137  UsesEmulatedTileMode(renderer.GetDeviceCapabilities());
138 
139  VS::FrameInfo frame_info;
140  frame_info.mvp = geometry_result.transform;
141  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
142  frame_info.alpha = GetOpacityFactor();
143 
144  Command cmd;
145  if (uses_emulated_tile_mode) {
146  DEBUG_COMMAND_INFO(cmd, "TiledTextureFill");
147  } else {
148  DEBUG_COMMAND_INFO(cmd, "TextureFill");
149  }
150 
151  cmd.stencil_reference = entity.GetClipDepth();
152 
153  auto options = OptionsFromPassAndEntity(pass, entity);
154  if (geometry_result.prevent_overdraw) {
155  options.stencil_compare = CompareFunction::kEqual;
156  options.stencil_operation = StencilOperation::kIncrementClamp;
157  }
158  options.primitive_type = geometry_result.type;
159 
160 #ifdef IMPELLER_ENABLE_OPENGLES
161  if (is_external_texture) {
162  cmd.pipeline = renderer.GetTiledTextureExternalPipeline(options);
163  } else {
164  cmd.pipeline = uses_emulated_tile_mode
165  ? renderer.GetTiledTexturePipeline(options)
166  : renderer.GetTexturePipeline(options);
167  }
168 #else
169  cmd.pipeline = uses_emulated_tile_mode
170  ? renderer.GetTiledTexturePipeline(options)
171  : renderer.GetTexturePipeline(options);
172 #endif // IMPELLER_ENABLE_OPENGLES
173 
174  cmd.BindVertices(std::move(geometry_result.vertex_buffer));
175  VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
176 
177  if (is_external_texture) {
178  FSExternal::FragInfo frag_info;
179  frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
180  frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
181  FSExternal::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
182  } else if (uses_emulated_tile_mode) {
183  FS::FragInfo frag_info;
184  frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
185  frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
186  FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
187  }
188 
189  if (is_external_texture) {
190  SamplerDescriptor sampler_desc;
191  // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid, so we
192  // emulate all other tile modes here by remapping the texture coordinates.
193  sampler_desc.width_address_mode = SamplerAddressMode::kClampToEdge;
194  sampler_desc.height_address_mode = SamplerAddressMode::kClampToEdge;
195 
196  // Also, external textures cannot be bound to color filters, so ignore this
197  // case for now.
198  FML_DCHECK(!color_filter_)
199  << "Color filters are not currently supported for external textures.";
200 
201  FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
202  cmd, texture_,
203  renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc));
204  } else {
205  if (color_filter_) {
206  auto filtered_texture = CreateFilterTexture(renderer);
207  if (!filtered_texture) {
208  return false;
209  }
210  FS::BindTextureSampler(
211  cmd, filtered_texture,
212  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
213  CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
214  } else {
215  FS::BindTextureSampler(
216  cmd, texture_,
217  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
218  CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
219  }
220  }
221 
222  if (!pass.AddCommand(std::move(cmd))) {
223  return false;
224  }
225 
226  if (geometry_result.prevent_overdraw) {
227  auto restore = ClipRestoreContents();
228  restore.SetRestoreCoverage(GetCoverage(entity));
229  return restore.Render(renderer, entity, pass);
230  }
231  return true;
232 }

References impeller::RenderPass::AddCommand(), impeller::Command::BindVertices(), DEBUG_COMMAND_INFO, impeller::Entity::GetClipDepth(), 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::SamplerDescriptor::height_address_mode, impeller::kClampToEdge, impeller::kEqual, impeller::kIncrementClamp, impeller::kTextureExternalOES, impeller::TRect< Scalar >::MakeSize(), impeller::OptionsFromPassAndEntity(), impeller::Command::pipeline, impeller::Command::stencil_reference, and impeller::SamplerDescriptor::width_address_mode.

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

◆ 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, clip depth, and blend mode. The result texture size is always the size of GetCoverage(entity).

Reimplemented from impeller::Contents.

Definition at line 234 of file tiled_texture_contents.cc.

240  {
241  if (GetInverseEffectTransform().IsIdentity() &&
242  GetGeometry()->IsAxisAlignedRect()) {
243  auto coverage = GetCoverage(entity);
244  if (!coverage.has_value()) {
245  return std::nullopt;
246  }
247  auto scale = Vector2(coverage->GetSize() / Size(texture_->GetSize()));
248 
249  return Snapshot{
250  .texture = texture_,
251  .transform = Matrix::MakeTranslation(coverage->GetOrigin()) *
252  Matrix::MakeScale(scale),
253  .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
254  .opacity = GetOpacityFactor(),
255  };
256  }
257 
259  renderer, // renderer
260  entity, // entity
261  std::nullopt, // coverage_limit
262  sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
263  true, // msaa_enabled
264  label); // label
265 }

References impeller::ColorSourceContents::GetCoverage(), impeller::ColorSourceContents::GetGeometry(), impeller::ColorSourceContents::GetInverseEffectTransform(), impeller::ColorSourceContents::GetOpacityFactor(), impeller::Matrix::MakeScale(), impeller::Matrix::MakeTranslation(), 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 workload by 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 57 of file tiled_texture_contents.cc.

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

◆ SetSamplerDescriptor()

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

Definition at line 53 of file tiled_texture_contents.cc.

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

◆ SetTexture()

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

Definition at line 43 of file tiled_texture_contents.cc.

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

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 47 of file tiled_texture_contents.cc.

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

The documentation for this class was generated from the following files:
impeller::TextureType::kTextureExternalOES
@ kTextureExternalOES
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:28
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
impeller::Entity::TileMode::kDecal
@ kDecal
impeller::Vector2
Point Vector2
Definition: point.h:312
impeller::StencilOperation::kIncrementClamp
@ kIncrementClamp
Increment the current stencil value by 1. Clamp it to the maximum.
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::SamplerAddressMode::kClampToEdge
@ kClampToEdge
impeller::Size
TSize< Scalar > Size
Definition: size.h:137
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:28
impeller::ColorSourceContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
Definition: color_source_contents.cc:45
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:44
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
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, clip depth,...
Definition: contents.cc:57