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, int32_t mip_count=1, 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)
 
- Protected Types inherited from impeller::ColorSourceContents
using BindFragmentCallback = std::function< bool(RenderPass &pass)>
 
using PipelineBuilderMethod = std::shared_ptr< Pipeline< PipelineDescriptor > >(impeller::ContentContext::*)(ContentContextOptions) const
 
using PipelineBuilderCallback = std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)>
 
- Protected Member Functions inherited from impeller::ColorSourceContents
template<typename VertexShaderT >
bool DrawGeometry (const ContentContext &renderer, const Entity &entity, RenderPass &pass, const PipelineBuilderCallback &pipeline_callback, typename VertexShaderT::FrameInfo frame_info, const BindFragmentCallback &bind_fragment_callback, bool enable_uvs=false, Rect texture_coverage={}, const Matrix &effect_transform={}) const
 

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

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

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

113  {
114  if (texture_ == nullptr) {
115  return true;
116  }
117 
118  using VS = TextureFillVertexShader;
119  using FS = TiledTextureFillFragmentShader;
120  using FSExternal = TiledTextureFillExternalFragmentShader;
121 
122  const auto texture_size = texture_->GetSize();
123  if (texture_size.IsEmpty()) {
124  return true;
125  }
126 
127  bool is_external_texture =
128  texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
129 
130  bool uses_emulated_tile_mode =
131  UsesEmulatedTileMode(renderer.GetDeviceCapabilities());
132 
133  VS::FrameInfo frame_info;
134  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
135  frame_info.alpha = GetOpacityFactor();
136 
137  PipelineBuilderMethod pipeline_method;
138 
139 #ifdef IMPELLER_ENABLE_OPENGLES
140  if (is_external_texture) {
141  pipeline_method = &ContentContext::GetTiledTextureExternalPipeline;
142  } else {
143  pipeline_method = uses_emulated_tile_mode
146  }
147 #else
148  pipeline_method = uses_emulated_tile_mode
151 #endif // IMPELLER_ENABLE_OPENGLES
152 
153  PipelineBuilderCallback pipeline_callback =
154  [&renderer, &pipeline_method](ContentContextOptions options) {
155  return (renderer.*pipeline_method)(options);
156  };
157  return ColorSourceContents::DrawGeometry<VS>(
158  renderer, entity, pass, pipeline_callback, frame_info,
159  [this, &renderer, &is_external_texture,
160  &uses_emulated_tile_mode](RenderPass& pass) {
161  auto& host_buffer = renderer.GetTransientsBuffer();
162 
163  if (uses_emulated_tile_mode) {
164  pass.SetCommandLabel("TiledTextureFill");
165  } else {
166  pass.SetCommandLabel("TextureFill");
167  }
168 
169  if (is_external_texture) {
170  FSExternal::FragInfo frag_info;
171  frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
172  frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
173  FSExternal::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
174  } else if (uses_emulated_tile_mode) {
175  FS::FragInfo frag_info;
176  frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
177  frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
178  FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
179  }
180 
181  if (is_external_texture) {
182  SamplerDescriptor sampler_desc;
183  // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid, so
184  // we emulate all other tile modes here by remapping the texture
185  // coordinates.
186  sampler_desc.width_address_mode = SamplerAddressMode::kClampToEdge;
187  sampler_desc.height_address_mode = SamplerAddressMode::kClampToEdge;
188 
189  // Also, external textures cannot be bound to color filters, so ignore
190  // this case for now.
191  FML_DCHECK(!color_filter_) << "Color filters are not currently "
192  "supported for external textures.";
193 
194  FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
195  pass, texture_,
196  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
197  sampler_desc));
198  } else {
199  if (color_filter_) {
200  auto filtered_texture = CreateFilterTexture(renderer);
201  if (!filtered_texture) {
202  return false;
203  }
204  FS::BindTextureSampler(
205  pass, filtered_texture,
206  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
207  CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
208  } else {
209  FS::BindTextureSampler(
210  pass, texture_,
211  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
212  CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
213  }
214  }
215 
216  return true;
217  },
218  /*enable_uvs=*/true,
219  /*texture_coverage=*/Rect::MakeSize(texture_size),
220  /*effect_transform=*/GetInverseEffectTransform());
221 }

References impeller::ContentContext::GetContext(), impeller::ContentContext::GetDeviceCapabilities(), impeller::ColorSourceContents::GetInverseEffectTransform(), impeller::ColorSourceContents::GetOpacityFactor(), impeller::ContentContext::GetTexturePipeline(), impeller::ContentContext::GetTiledTexturePipeline(), impeller::ContentContext::GetTransientsBuffer(), impeller::SamplerDescriptor::height_address_mode, impeller::kClampToEdge, impeller::kTextureExternalOES, impeller::TRect< Scalar >::MakeSize(), impeller::RenderPass::SetCommandLabel(), 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,
int32_t  mip_count = 1,
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 223 of file tiled_texture_contents.cc.

230  {
231  std::optional<Rect> geometry_coverage = GetGeometry()->GetCoverage({});
232  if (GetInverseEffectTransform().IsIdentity() &&
233  GetGeometry()->IsAxisAlignedRect() &&
234  (!geometry_coverage.has_value() ||
235  Rect::MakeSize(texture_->GetSize())
236  .Contains(geometry_coverage.value()))) {
237  auto coverage = GetCoverage(entity);
238  if (!coverage.has_value()) {
239  return std::nullopt;
240  }
241  auto scale = Vector2(coverage->GetSize() / Size(texture_->GetSize()));
242 
243  return Snapshot{
244  .texture = texture_,
245  .transform = Matrix::MakeTranslation(coverage->GetOrigin()) *
247  .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
248  .opacity = GetOpacityFactor(),
249  };
250  }
251 
253  renderer, // renderer
254  entity, // entity
255  std::nullopt, // coverage_limit
256  sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
257  true, // msaa_enabled
258  /*mip_count=*/1,
259  label); // label
260 }

References impeller::TRect< T >::Contains(), impeller::ColorSourceContents::GetCoverage(), impeller::ColorSourceContents::GetGeometry(), impeller::ColorSourceContents::GetInverseEffectTransform(), impeller::ColorSourceContents::GetOpacityFactor(), impeller::Matrix::MakeScale(), impeller::TRect< Scalar >::MakeSize(), impeller::Matrix::MakeTranslation(), impeller::Contents::RenderToSnapshot(), scale, 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 55 of file tiled_texture_contents.cc.

55  {
56  color_filter_ = std::move(color_filter);
57 }

◆ SetSamplerDescriptor()

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

Definition at line 51 of file tiled_texture_contents.cc.

51  {
52  sampler_descriptor_ = std::move(desc);
53 }

◆ SetTexture()

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

Definition at line 41 of file tiled_texture_contents.cc.

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

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

46  {
47  x_tile_mode_ = x_tile_mode;
48  y_tile_mode_ = y_tile_mode;
49 }

The documentation for this class was generated from the following files:
impeller::ContentContext::GetTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:485
impeller::TextureType::kTextureExternalOES
@ kTextureExternalOES
impeller::ColorSourceContents::PipelineBuilderCallback
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
Definition: color_source_contents.h:113
impeller::ColorSourceContents::GetOpacityFactor
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
Definition: color_source_contents.cc:28
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::Entity::TileMode::kDecal
@ kDecal
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::Vector2
Point Vector2
Definition: point.h:320
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:20
impeller::ContentContext::GetTiledTexturePipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetTiledTexturePipeline(ContentContextOptions opts) const
Definition: content_context.h:516
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::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:15
impeller::TRect::Contains
constexpr bool Contains(const TPoint< Type > &p) const
Returns true iff the provided point |p| is inside the half-open interior of this rectangle.
Definition: rect.h:217
impeller::ColorSourceContents::PipelineBuilderMethod
std::shared_ptr< Pipeline< PipelineDescriptor > >(impeller::ContentContext::*)(ContentContextOptions) const PipelineBuilderMethod
Definition: color_source_contents.h:110
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:44
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:146
scale
const Scalar scale
Definition: stroke_path_geometry.cc:297
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:36