Flutter Impeller
impeller::TextureContents Class Referencefinal

#include <texture_contents.h>

Inheritance diagram for impeller::TextureContents:
impeller::Contents

Public Member Functions

 TextureContents ()
 
 ~TextureContents () override
 
void SetLabel (std::string label)
 
void SetDestinationRect (Rect rect)
 
void SetTexture (std::shared_ptr< Texture > texture)
 
std::shared_ptr< TextureGetTexture () const
 
void SetSamplerDescriptor (SamplerDescriptor desc)
 
const SamplerDescriptorGetSamplerDescriptor () const
 
void SetSourceRect (const Rect &source_rect)
 
const RectGetSourceRect () const
 
void SetStrictSourceRect (bool strict)
 
bool GetStrictSourceRect () const
 
void SetOpacity (Scalar opacity)
 
Scalar GetOpacity () const
 
void SetStencilEnabled (bool enabled)
 
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...
 
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="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...
 
bool Render (const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
 
void SetInheritedOpacity (Scalar opacity) override
 Inherit the provided opacity. More...
 
void SetDeferApplyingOpacity (bool defer_applying_opacity)
 
- 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
 
virtual bool IsOpaque (const Matrix &transform) const
 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...
 
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...
 
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...
 

Static Public Member Functions

static std::shared_ptr< TextureContentsMakeRect (Rect destination)
 A common case factory that marks the texture contents as having a destination rectangle. In this situation, a subpass can be avoided when image filters are applied. More...
 
- Static Public Member Functions inherited from impeller::Contents
static std::shared_ptr< ContentsMakeAnonymous (RenderProc render_proc, CoverageProc coverage_proc)
 

Additional Inherited Members

- 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)>
 

Detailed Description

Definition at line 17 of file texture_contents.h.

Constructor & Destructor Documentation

◆ TextureContents()

impeller::TextureContents::TextureContents ( )
default

◆ ~TextureContents()

impeller::TextureContents::~TextureContents ( )
overridedefault

Member Function Documentation

◆ GetCoverage()

std::optional< Rect > impeller::TextureContents::GetCoverage ( const Entity entity) const
overridevirtual

Get the area of the render pass that will be affected when this contents is rendered.

During rendering, coverage coordinates count pixels from the top left corner of the framebuffer.

Returns
The coverage rectangle. An std::nullopt result means that rendering this contents has no effect on the output color.

Implements impeller::Contents.

Definition at line 66 of file texture_contents.cc.

66  {
67  if (GetOpacity() == 0) {
68  return std::nullopt;
69  }
70  return destination_rect_.TransformBounds(entity.GetTransform());
71 };

References GetOpacity(), impeller::Entity::GetTransform(), and impeller::TRect< T >::TransformBounds().

◆ GetOpacity()

Scalar impeller::TextureContents::GetOpacity ( ) const

Definition at line 62 of file texture_contents.cc.

62  {
63  return opacity_ * inherited_opacity_;
64 }

Referenced by GetCoverage(), Render(), and RenderToSnapshot().

◆ GetSamplerDescriptor()

const SamplerDescriptor & impeller::TextureContents::GetSamplerDescriptor ( ) const

Definition at line 247 of file texture_contents.cc.

247  {
248  return sampler_descriptor_;
249 }

◆ GetSourceRect()

const Rect & impeller::TextureContents::GetSourceRect ( ) const

Definition at line 231 of file texture_contents.cc.

231  {
232  return source_rect_;
233 }

◆ GetStrictSourceRect()

bool impeller::TextureContents::GetStrictSourceRect ( ) const

Definition at line 239 of file texture_contents.cc.

239  {
240  return strict_source_rect_enabled_;
241 }

◆ GetTexture()

std::shared_ptr< Texture > impeller::TextureContents::GetTexture ( ) const

Definition at line 46 of file texture_contents.cc.

46  {
47  return texture_;
48 }

◆ MakeRect()

std::shared_ptr< TextureContents > impeller::TextureContents::MakeRect ( Rect  destination)
static

A common case factory that marks the texture contents as having a destination rectangle. In this situation, a subpass can be avoided when image filters are applied.

Definition at line 28 of file texture_contents.cc.

28  {
29  auto contents = std::make_shared<TextureContents>();
30  contents->destination_rect_ = destination;
31  return contents;
32 }

Referenced by impeller::Canvas::DrawImageRect(), impeller::Entity::FromSnapshot(), and impeller::testing::TEST_P().

◆ Render()

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

Implements impeller::Contents.

Definition at line 106 of file texture_contents.cc.

108  {
109  using VS = TextureFillVertexShader;
110  using FS = TextureFillFragmentShader;
111  using FSStrict = TextureFillStrictSrcFragmentShader;
112 
113  if (destination_rect_.IsEmpty() || source_rect_.IsEmpty() ||
114  texture_ == nullptr || texture_->GetSize().IsEmpty()) {
115  return true; // Nothing to render.
116  }
117 
118 #ifdef IMPELLER_ENABLE_OPENGLES
119  using FSExternal = TiledTextureFillExternalFragmentShader;
120  bool is_external_texture =
121  texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
122 #endif // IMPELLER_ENABLE_OPENGLES
123 
124  auto texture_coords =
125  Rect::MakeSize(texture_->GetSize()).Project(source_rect_);
126  auto& host_buffer = renderer.GetTransientsBuffer();
127 
128  std::array<VS::PerVertexData, 4> vertices = {
129  VS::PerVertexData{destination_rect_.GetLeftTop(),
130  texture_coords.GetLeftTop()},
131  VS::PerVertexData{destination_rect_.GetRightTop(),
132  texture_coords.GetRightTop()},
133  VS::PerVertexData{destination_rect_.GetLeftBottom(),
134  texture_coords.GetLeftBottom()},
135  VS::PerVertexData{destination_rect_.GetRightBottom(),
136  texture_coords.GetRightBottom()},
137  };
138  auto vertex_buffer = CreateVertexBuffer(vertices, host_buffer);
139 
140  VS::FrameInfo frame_info;
141  frame_info.mvp = entity.GetShaderTransform(pass);
142  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
143 
144 #ifdef IMPELLER_DEBUG
145  if (label_.empty()) {
146  pass.SetCommandLabel("Texture Fill");
147  } else {
148  pass.SetCommandLabel("Texture Fill: " + label_);
149  }
150 #endif // IMPELLER_DEBUG
151 
152  auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
153  if (!stencil_enabled_) {
154  pipeline_options.stencil_mode = ContentContextOptions::StencilMode::kIgnore;
155  }
156  pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
157 
158  pipeline_options.depth_write_enabled =
159  stencil_enabled_ && pipeline_options.blend_mode == BlendMode::kSource;
160 
161 #ifdef IMPELLER_ENABLE_OPENGLES
162  if (is_external_texture) {
163  pass.SetPipeline(
164  renderer.GetTiledTextureExternalPipeline(pipeline_options));
165  } else {
166  pass.SetPipeline(
167  strict_source_rect_enabled_
168  ? renderer.GetTextureStrictSrcPipeline(pipeline_options)
169  : renderer.GetTexturePipeline(pipeline_options));
170  }
171 #else
172  pass.SetPipeline(strict_source_rect_enabled_
173  ? renderer.GetTextureStrictSrcPipeline(pipeline_options)
174  : renderer.GetTexturePipeline(pipeline_options));
175 #endif // IMPELLER_ENABLE_OPENGLES
176 
177  pass.SetVertexBuffer(vertex_buffer);
178  VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
179 
180  if (strict_source_rect_enabled_) {
181  // For a strict source rect, shrink the texture coordinate range by half a
182  // texel to ensure that linear filtering does not sample anything outside
183  // the source rect bounds.
184  auto strict_texture_coords =
185  Rect::MakeSize(texture_->GetSize()).Project(source_rect_.Expand(-0.5));
186 
187  FSStrict::FragInfo frag_info;
188  frag_info.source_rect = Vector4(strict_texture_coords.GetLTRB());
189  frag_info.alpha = GetOpacity();
190  FSStrict::BindFragInfo(pass, host_buffer.EmplaceUniform((frag_info)));
191  FSStrict::BindTextureSampler(
192  pass, texture_,
193  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
194  sampler_descriptor_));
195 #ifdef IMPELLER_ENABLE_OPENGLES
196  } else if (is_external_texture) {
197  FSExternal::FragInfo frag_info;
198  frag_info.x_tile_mode =
199  static_cast<Scalar>(sampler_descriptor_.width_address_mode);
200  frag_info.y_tile_mode =
201  static_cast<Scalar>(sampler_descriptor_.height_address_mode);
202  frag_info.alpha = GetOpacity();
203  FSExternal::BindFragInfo(pass, host_buffer.EmplaceUniform(frag_info));
204 
205  SamplerDescriptor sampler_desc;
206  // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid, so
207  // we emulate all other tile modes here by remapping the texture
208  // coordinates.
209  sampler_desc.width_address_mode = SamplerAddressMode::kClampToEdge;
210  sampler_desc.height_address_mode = SamplerAddressMode::kClampToEdge;
211  FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
212  pass, texture_,
213  renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc));
214 #endif // IMPELLER_ENABLE_OPENGLES
215  } else {
216  FS::FragInfo frag_info;
217  frag_info.alpha = GetOpacity();
218  FS::BindFragInfo(pass, host_buffer.EmplaceUniform((frag_info)));
219  FS::BindTextureSampler(
220  pass, texture_,
221  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
222  sampler_descriptor_));
223  }
224  return pass.Draw().ok();
225 }

References impeller::CreateVertexBuffer(), impeller::RenderPass::Draw(), impeller::TRect< T >::Expand(), impeller::ContentContext::GetContext(), impeller::TRect< T >::GetLeftBottom(), impeller::TRect< T >::GetLeftTop(), GetOpacity(), impeller::TRect< T >::GetRightBottom(), impeller::TRect< T >::GetRightTop(), impeller::Entity::GetShaderTransform(), impeller::ContentContext::GetTexturePipeline(), impeller::ContentContext::GetTextureStrictSrcPipeline(), impeller::ContentContext::GetTransientsBuffer(), impeller::SamplerDescriptor::height_address_mode, impeller::TRect< T >::IsEmpty(), impeller::kClampToEdge, impeller::ContentContextOptions::kIgnore, impeller::kSource, impeller::kTextureExternalOES, impeller::kTriangleStrip, impeller::TRect< Scalar >::MakeSize(), impeller::OptionsFromPassAndEntity(), impeller::TRect< T >::Project(), impeller::RenderPass::SetCommandLabel(), impeller::RenderPass::SetPipeline(), impeller::RenderPass::SetVertexBuffer(), and impeller::SamplerDescriptor::width_address_mode.

◆ RenderToSnapshot()

std::optional< Snapshot > impeller::TextureContents::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 = "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 73 of file texture_contents.cc.

80  {
81  // Passthrough textures that have simple rectangle paths and complete source
82  // rects.
83  auto bounds = destination_rect_;
84  auto opacity = GetOpacity();
85  if (source_rect_ == Rect::MakeSize(texture_->GetSize()) &&
86  (opacity >= 1 - kEhCloseEnough || defer_applying_opacity_)) {
87  auto scale = Vector2(bounds.GetSize() / Size(texture_->GetSize()));
88  return Snapshot{
89  .texture = texture_,
90  .transform = entity.GetTransform() *
91  Matrix::MakeTranslation(bounds.GetOrigin()) *
93  .sampler_descriptor = sampler_descriptor.value_or(sampler_descriptor_),
94  .opacity = opacity};
95  }
97  renderer, // renderer
98  entity, // entity
99  std::nullopt, // coverage_limit
100  sampler_descriptor.value_or(sampler_descriptor_), // sampler_descriptor
101  true, // msaa_enabled
102  /*mip_count=*/mip_count,
103  label); // label
104 }

References GetOpacity(), impeller::Entity::GetTransform(), impeller::kEhCloseEnough, impeller::Matrix::MakeScale(), impeller::TRect< Scalar >::MakeSize(), impeller::Matrix::MakeTranslation(), impeller::Contents::RenderToSnapshot(), scale, and impeller::Snapshot::texture.

◆ SetDeferApplyingOpacity()

void impeller::TextureContents::SetDeferApplyingOpacity ( bool  defer_applying_opacity)

Definition at line 251 of file texture_contents.cc.

251  {
252  defer_applying_opacity_ = defer_applying_opacity;
253 }

◆ SetDestinationRect()

void impeller::TextureContents::SetDestinationRect ( Rect  rect)

Definition at line 38 of file texture_contents.cc.

38  {
39  destination_rect_ = rect;
40 }

◆ SetInheritedOpacity()

void impeller::TextureContents::SetInheritedOpacity ( Scalar  opacity)
overridevirtual

Inherit the provided opacity.

   Use of this method is invalid if CanAcceptOpacity returns false.

Reimplemented from impeller::Contents.

Definition at line 58 of file texture_contents.cc.

58  {
59  inherited_opacity_ = opacity;
60 }

◆ SetLabel()

void impeller::TextureContents::SetLabel ( std::string  label)

Definition at line 34 of file texture_contents.cc.

34  {
35  label_ = std::move(label);
36 }

◆ SetOpacity()

void impeller::TextureContents::SetOpacity ( Scalar  opacity)

Definition at line 50 of file texture_contents.cc.

50  {
51  opacity_ = opacity;
52 }

◆ SetSamplerDescriptor()

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

Definition at line 243 of file texture_contents.cc.

243  {
244  sampler_descriptor_ = std::move(desc);
245 }

◆ SetSourceRect()

void impeller::TextureContents::SetSourceRect ( const Rect source_rect)

Definition at line 227 of file texture_contents.cc.

227  {
228  source_rect_ = source_rect;
229 }

◆ SetStencilEnabled()

void impeller::TextureContents::SetStencilEnabled ( bool  enabled)

Definition at line 54 of file texture_contents.cc.

54  {
55  stencil_enabled_ = enabled;
56 }

◆ SetStrictSourceRect()

void impeller::TextureContents::SetStrictSourceRect ( bool  strict)

Definition at line 235 of file texture_contents.cc.

235  {
236  strict_source_rect_enabled_ = strict;
237 }

◆ SetTexture()

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

Definition at line 42 of file texture_contents.cc.

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

The documentation for this class was generated from the following files:
impeller::ContentContextOptions::StencilMode::kIgnore
@ kIgnore
impeller::TextureType::kTextureExternalOES
@ kTextureExternalOES
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:56
impeller::BlendMode::kSource
@ kSource
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:63
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:466
impeller::TRect::GetLeftTop
constexpr TPoint< T > GetLeftTop() const
Definition: rect.h:353
impeller::Vector2
Point Vector2
Definition: point.h:331
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::TRect::GetRightTop
constexpr TPoint< T > GetRightTop() const
Definition: rect.h:357
impeller::TextureContents::GetOpacity
Scalar GetOpacity() const
Definition: texture_contents.cc:62
impeller::TRect::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: rect.h:291
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::CreateVertexBuffer
VertexBuffer CreateVertexBuffer(std::array< VertexType, size > input, HostBuffer &host_buffer)
Create an index-less vertex buffer from a fixed size array.
Definition: vertex_buffer_builder.h:24
impeller::TRect::GetLeftBottom
constexpr TPoint< T > GetLeftBottom() const
Definition: rect.h:361
impeller::SamplerDescriptor::width_address_mode
SamplerAddressMode width_address_mode
Definition: sampler_descriptor.h:20
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
impeller::TRect::Project
constexpr TRect< T > Project(TRect< T > source) const
Returns a new rectangle that represents the projection of the source rectangle onto this rectangle....
Definition: rect.h:663
scale
const Scalar scale
Definition: stroke_path_geometry.cc:301
impeller::TRect::GetRightBottom
constexpr TPoint< T > GetRightBottom() const
Definition: rect.h:365
impeller::SamplerDescriptor::height_address_mode
SamplerAddressMode height_address_mode
Definition: sampler_descriptor.h:21
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
impeller::TRect::Expand
constexpr TRect< T > Expand(T left, T top, T right, T bottom) const
Returns a rectangle with expanded edges. Negative expansion results in shrinking.
Definition: rect.h:612