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::ColorSourceContents
using BindFragmentCallback = std::function< bool(RenderPass &pass)>
 
using PipelineBuilderCallback = std::function< PipelineRef(ContentContextOptions)>
 
using CreateGeometryCallback = std::function< GeometryResult(const ContentContext &renderer, const Entity &entity, RenderPass &pass, const Geometry *geom)>
 
- 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 Matrix &transform) 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 (const 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, std::string_view 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 (const Geometry *geometry)
 Set the geometry that this contents will use to render. More...
 
const GeometryGetGeometry () 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...
 
void SetInheritedOpacity (Scalar opacity) override
 Inherit the provided opacity. More...
 
- Public Member Functions inherited from impeller::Contents
 Contents ()
 
virtual ~Contents ()
 
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
 
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 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::ColorSourceContents
template<typename VertexShaderT >
static bool DrawGeometry (const Contents *contents, const Geometry *geometry, const ContentContext &renderer, const Entity &entity, RenderPass &pass, const PipelineBuilderCallback &pipeline_callback, typename VertexShaderT::FrameInfo frame_info, const BindFragmentCallback &bind_fragment_callback, bool force_stencil=false, const CreateGeometryCallback &create_geom_callback=DefaultCreateGeometryCallback)
 
- Static Public Member Functions inherited from impeller::Contents
static std::shared_ptr< ContentsMakeAnonymous (RenderProc render_proc, CoverageProc coverage_proc)
 
- Protected Member Functions inherited from impeller::ColorSourceContents
bool AppliesAlphaForStrokeCoverage (const Matrix &transform) const
 Whether the entity should be treated as non-opaque due to stroke geometry requiring alpha for coverage. More...
 
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 force_stencil=false, const CreateGeometryCallback &create_geom_callback=DefaultCreateGeometryCallback) const
 
- Static Protected Member Functions inherited from impeller::ColorSourceContents
static GeometryResult DefaultCreateGeometryCallback (const ContentContext &renderer, const Entity &entity, RenderPass &pass, const Geometry *geom)
 

Detailed Description

Definition at line 19 of file tiled_texture_contents.h.

Member Typedef Documentation

◆ ColorFilterProc

Definition at line 25 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 Matrix transform) 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.

Parameters
transformThe current transform matrix of the entity that will render this contents.

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() && !AppliesAlphaForStrokeCoverage(transform);
109 }
Scalar GetOpacityFactor() const
Get the opacity factor for this color source.
bool AppliesAlphaForStrokeCoverage(const Matrix &transform) const
Whether the entity should be treated as non-opaque due to stroke geometry requiring alpha for coverag...

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

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 = TextureUvFillVertexShader;
119  using FS = TiledTextureFillFragmentShader;
120 
121  const auto texture_size = texture_->GetSize();
122  if (texture_size.IsEmpty()) {
123  return true;
124  }
125 
126  VS::FrameInfo frame_info;
127  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
128  frame_info.uv_transform =
129  Rect::MakeSize(texture_size).GetNormalizingTransform() *
131 
132 #ifdef IMPELLER_ENABLE_OPENGLES
133  using FSExternal = TiledTextureFillExternalFragmentShader;
134  if (texture_->GetTextureDescriptor().type ==
136  return ColorSourceContents::DrawGeometry<VS>(
137  renderer, entity, pass,
138  [&renderer](ContentContextOptions options) {
139  return renderer.GetTiledTextureUvExternalPipeline(options);
140  },
141  frame_info,
142  [this, &renderer](RenderPass& pass) {
143  auto& host_buffer = renderer.GetTransientsBuffer();
144 #ifdef IMPELLER_DEBUG
145  pass.SetCommandLabel("TextureFill External");
146 #endif // IMPELLER_DEBUG
147 
148  FML_DCHECK(!color_filter_);
149  FSExternal::FragInfo frag_info;
150  frag_info.x_tile_mode =
151  static_cast<Scalar>(sampler_descriptor_.width_address_mode);
152  frag_info.y_tile_mode =
153  static_cast<Scalar>(sampler_descriptor_.height_address_mode);
154  frag_info.alpha = GetOpacityFactor();
155  FSExternal::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
156 
157  SamplerDescriptor sampler_desc;
158  // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid,
159  // so we emulate all other tile modes here by remapping the texture
160  // coordinates.
161  sampler_desc.width_address_mode = SamplerAddressMode::kClampToEdge;
162  sampler_desc.height_address_mode = SamplerAddressMode::kClampToEdge;
163  sampler_desc.min_filter = sampler_descriptor_.min_filter;
164  sampler_desc.mag_filter = sampler_descriptor_.mag_filter;
165  sampler_desc.mip_filter = MipFilter::kBase;
166 
167  FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
168  pass, texture_,
169  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
170  sampler_desc));
171  return true;
172  });
173  }
174 #endif // IMPELLER_ENABLE_OPENGLES
175 
176  PipelineBuilderCallback pipeline_callback =
177  [&renderer](ContentContextOptions options) {
178  return renderer.GetTiledTexturePipeline(options);
179  };
180  return ColorSourceContents::DrawGeometry<VS>(
181  renderer, entity, pass, pipeline_callback, frame_info,
182  [this, &renderer, &entity](RenderPass& pass) {
183  auto& host_buffer = renderer.GetTransientsBuffer();
184 #ifdef IMPELLER_DEBUG
185  pass.SetCommandLabel("TextureFill");
186 #endif // IMPELLER_DEBUG
187 
188  FS::FragInfo frag_info;
189  frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
190  frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
191  frag_info.alpha =
192  GetOpacityFactor() *
193  GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
194  FS::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
195 
196  if (color_filter_) {
197  auto filtered_texture = CreateFilterTexture(renderer);
198  if (!filtered_texture) {
199  return false;
200  }
201  FS::BindTextureSampler(
202  pass, filtered_texture,
203  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
204  CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
205  } else {
206  FS::BindTextureSampler(
207  pass, texture_,
208  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
209  CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
210  }
211 
212  return true;
213  });
214 }
const Geometry * GetGeometry() const
Get the geometry that this contents will use to render.
const Matrix & GetInverseEffectTransform() const
Set the inverted effect transform for this color source.
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
virtual Scalar ComputeAlphaCoverage(const Matrix &transform) const
Definition: geometry.h:125
float Scalar
Definition: scalar.h:19
LinePipeline::FragmentShader FS
@ kBase
The texture is sampled as if it only had a single mipmap level.
LinePipeline::VertexShader VS
SamplerAddressMode width_address_mode
SamplerAddressMode height_address_mode
constexpr Matrix GetNormalizingTransform() const
Constructs a Matrix that will map all points in the coordinate space of the rectangle into a new norm...
Definition: rect.h:495
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150

References impeller::Geometry::ComputeAlphaCoverage(), impeller::ContentContext::GetContext(), impeller::ContentContext::GetDeviceCapabilities(), impeller::ColorSourceContents::GetGeometry(), impeller::ColorSourceContents::GetInverseEffectTransform(), impeller::TRect< T >::GetNormalizingTransform(), impeller::ColorSourceContents::GetOpacityFactor(), impeller::ContentContext::GetTiledTexturePipeline(), impeller::Entity::GetTransform(), impeller::ContentContext::GetTransientsBuffer(), impeller::SamplerDescriptor::height_address_mode, impeller::kBase, impeller::kClampToEdge, impeller::kTextureExternalOES, impeller::SamplerDescriptor::mag_filter, impeller::TRect< Scalar >::MakeSize(), impeller::SamplerDescriptor::min_filter, impeller::SamplerDescriptor::mip_filter, impeller::RenderPass::SetCommandLabel(), and impeller::SamplerDescriptor::width_address_mode.

Referenced by 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,
std::string_view  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 216 of file tiled_texture_contents.cc.

223  {
224  std::optional<Rect> geometry_coverage = GetGeometry()->GetCoverage({});
225  if (GetInverseEffectTransform().IsIdentity() &&
226  GetGeometry()->IsAxisAlignedRect() &&
227  (!geometry_coverage.has_value() ||
228  Rect::MakeSize(texture_->GetSize())
229  .Contains(geometry_coverage.value()))) {
230  auto coverage = GetCoverage(entity);
231  if (!coverage.has_value()) {
232  return std::nullopt;
233  }
234  auto scale = Vector2(coverage->GetSize() / Size(texture_->GetSize()));
235 
236  return Snapshot{
237  .texture = texture_,
238  .transform = Matrix::MakeTranslation(coverage->GetOrigin()) *
239  Matrix::MakeScale(scale),
240  .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
241  .opacity = GetOpacityFactor(),
242  };
243  }
244 
246  renderer, // renderer
247  entity, // entity
248  std::nullopt, // coverage_limit
249  sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
250  true, // msaa_enabled
251  /*mip_count=*/1,
252  label); // label
253 }
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.
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, std::string_view label="Snapshot") const
Render this contents to a snapshot, respecting the entity's transform, path, clip depth,...
Definition: contents.cc:56
virtual std::optional< Rect > GetCoverage(const Matrix &transform) const =0
Point Vector2
Definition: point.h:331
TSize< Scalar > Size
Definition: size.h:159
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
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:235

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

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

◆ SetSamplerDescriptor()

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

Definition at line 51 of file tiled_texture_contents.cc.

51  {
52  sampler_descriptor_ = 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::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: