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 (const Geometry *geometry)
 
 ~TiledTextureContents () override
 
const GeometryGetGeometry () const override
 Get the geometry that this contents will use to render. More...
 
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, const SnapshotOptions &options) 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 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 27 of file tiled_texture_contents.h.

Constructor & Destructor Documentation

◆ TiledTextureContents()

impeller::TiledTextureContents::TiledTextureContents ( const Geometry geometry)
explicit

Definition at line 37 of file tiled_texture_contents.cc.

38  : geometry_(geometry) {}

◆ ~TiledTextureContents()

impeller::TiledTextureContents::~TiledTextureContents ( )
overridedefault

Member Function Documentation

◆ GetGeometry()

const Geometry * impeller::TiledTextureContents::GetGeometry ( ) const
overridevirtual

Get the geometry that this contents will use to render.

Implements impeller::ColorSourceContents.

Definition at line 42 of file tiled_texture_contents.cc.

42  {
43  return geometry_;
44 }

Referenced by Render(), and RenderToSnapshot().

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

106  {
107  if (GetOpacityFactor() < 1 || x_tile_mode_ == Entity::TileMode::kDecal ||
108  y_tile_mode_ == Entity::TileMode::kDecal) {
109  return false;
110  }
111  if (color_filter_) {
112  return false;
113  }
114  return texture_->IsOpaque() && !AppliesAlphaForStrokeCoverage(transform);
115 }
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 117 of file tiled_texture_contents.cc.

119  {
120  if (texture_ == nullptr) {
121  return true;
122  }
123 
124  using VS = TextureUvFillVertexShader;
125  using FS = TiledTextureFillFragmentShader;
126 
127  const auto texture_size = texture_->GetSize();
128  if (texture_size.IsEmpty()) {
129  return true;
130  }
131 
132  VS::FrameInfo frame_info;
133  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
134  frame_info.uv_transform =
135  Rect::MakeSize(texture_size).GetNormalizingTransform() *
137 
138 #if defined(IMPELLER_ENABLE_OPENGLES) && !defined(FML_OS_EMSCRIPTEN)
139  using FSExternal = TiledTextureFillExternalFragmentShader;
140  if (texture_->GetTextureDescriptor().type ==
142  return ColorSourceContents::DrawGeometry<VS>(
143  renderer, entity, pass,
144  [&renderer](ContentContextOptions options) {
145  return renderer.GetTiledTextureUvExternalPipeline(options);
146  },
147  frame_info,
148  [this, &renderer](RenderPass& pass) {
149  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
150 #ifdef IMPELLER_DEBUG
151  pass.SetCommandLabel("TextureFill External");
152 #endif // IMPELLER_DEBUG
153 
154  FML_DCHECK(!color_filter_);
155  FSExternal::FragInfo frag_info;
156  frag_info.x_tile_mode =
157  static_cast<Scalar>(sampler_descriptor_.width_address_mode);
158  frag_info.y_tile_mode =
159  static_cast<Scalar>(sampler_descriptor_.height_address_mode);
160  frag_info.alpha = GetOpacityFactor();
161  FSExternal::BindFragInfo(pass,
162  data_host_buffer.EmplaceUniform(frag_info));
163 
164  SamplerDescriptor sampler_desc;
165  // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid,
166  // so we emulate all other tile modes here by remapping the texture
167  // coordinates.
168  sampler_desc.width_address_mode = SamplerAddressMode::kClampToEdge;
169  sampler_desc.height_address_mode = SamplerAddressMode::kClampToEdge;
170  sampler_desc.min_filter = sampler_descriptor_.min_filter;
171  sampler_desc.mag_filter = sampler_descriptor_.mag_filter;
172  sampler_desc.mip_filter = MipFilter::kBase;
173 
174  FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
175  pass, texture_,
176  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
177  sampler_desc));
178  return true;
179  });
180  }
181 #endif // IMPELLER_ENABLE_OPENGLES
182 
183  PipelineBuilderCallback pipeline_callback =
184  [&renderer](ContentContextOptions options) {
185  return renderer.GetTiledTexturePipeline(options);
186  };
187  return ColorSourceContents::DrawGeometry<VS>(
188  renderer, entity, pass, pipeline_callback, frame_info,
189  [this, &renderer, &entity](RenderPass& pass) {
190  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
191 #ifdef IMPELLER_DEBUG
192  pass.SetCommandLabel("TextureFill");
193 #endif // IMPELLER_DEBUG
194 
195  FS::FragInfo frag_info;
196  frag_info.x_tile_mode = static_cast<Scalar>(x_tile_mode_);
197  frag_info.y_tile_mode = static_cast<Scalar>(y_tile_mode_);
198  frag_info.alpha =
199  GetOpacityFactor() *
200  GetGeometry()->ComputeAlphaCoverage(entity.GetTransform());
201  FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
202 
203  if (color_filter_) {
204  auto filtered_texture = CreateFilterTexture(renderer);
205  if (!filtered_texture) {
206  return false;
207  }
208  FS::BindTextureSampler(
209  pass, filtered_texture,
210  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
211  CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
212  } else {
213  FS::BindTextureSampler(
214  pass, texture_,
215  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
216  CreateSamplerDescriptor(renderer.GetDeviceCapabilities())));
217  }
218 
219  return true;
220  });
221 }
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
const Geometry * GetGeometry() const override
Get the geometry that this contents will use to render.
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:491
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150

References impeller::Geometry::ComputeAlphaCoverage(), impeller::ContentContext::GetContext(), impeller::ContentContext::GetDeviceCapabilities(), GetGeometry(), impeller::ColorSourceContents::GetInverseEffectTransform(), impeller::TRect< T >::GetNormalizingTransform(), impeller::ColorSourceContents::GetOpacityFactor(), impeller::ContentContext::GetTiledTexturePipeline(), impeller::Entity::GetTransform(), impeller::ContentContext::GetTransientsDataBuffer(), 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,
const SnapshotOptions options 
) 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.

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

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

60  {
61  color_filter_ = std::move(color_filter);
62 }

◆ SetSamplerDescriptor()

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

Definition at line 56 of file tiled_texture_contents.cc.

56  {
57  sampler_descriptor_ = desc;
58 }

◆ SetTexture()

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

Definition at line 46 of file tiled_texture_contents.cc.

46  {
47  texture_ = std::move(texture);
48 }

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

◆ SetTileModes()

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

Definition at line 50 of file tiled_texture_contents.cc.

51  {
52  x_tile_mode_ = x_tile_mode;
53  y_tile_mode_ = y_tile_mode;
54 }

The documentation for this class was generated from the following files: