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 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, 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
 
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...
 
void SetDeferApplyingOpacity (bool defer_applying_opacity)
 
- 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 bool IsOpaque () 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...
 
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...
 

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 21 of file texture_contents.h.

Constructor & Destructor Documentation

◆ TextureContents()

impeller::TextureContents::TextureContents ( )
default

◆ ~TextureContents()

impeller::TextureContents::~TextureContents ( )
overridedefault

Member Function Documentation

◆ CanInheritOpacity()

bool impeller::TextureContents::CanInheritOpacity ( const Entity entity) const
overridevirtual

Whether or not this contents can accept the opacity peephole optimization.

By default all contents return false. Contents are responsible for determining whether or not their own geometries intersect in a way that makes accepting opacity impossible. It is always safe to return false, especially if computing overlap would be computationally expensive.

Reimplemented from impeller::Contents.

Definition at line 57 of file texture_contents.cc.

57  {
58  return true;
59 }

◆ 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 69 of file texture_contents.cc.

69  {
70  if (GetOpacity() == 0) {
71  return std::nullopt;
72  }
73  return destination_rect_.TransformBounds(entity.GetTransform());
74 };

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

◆ GetOpacity()

Scalar impeller::TextureContents::GetOpacity ( ) const

Definition at line 65 of file texture_contents.cc.

65  {
66  return opacity_ * inherited_opacity_;
67 }

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

◆ GetSamplerDescriptor()

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

Definition at line 203 of file texture_contents.cc.

203  {
204  return sampler_descriptor_;
205 }

◆ GetSourceRect()

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

Definition at line 195 of file texture_contents.cc.

195  {
196  return source_rect_;
197 }

◆ GetTexture()

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

Definition at line 45 of file texture_contents.cc.

45  {
46  return texture_;
47 }

◆ 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 27 of file texture_contents.cc.

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

Referenced by impeller::PaintPassDelegate::CreateContentsForSubpassTarget(), 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 107 of file texture_contents.cc.

109  {
110  auto capture = entity.GetCapture().CreateChild("TextureContents");
111 
112  using VS = TextureFillVertexShader;
113  using FS = TextureFillFragmentShader;
114  using FSExternal = TextureFillExternalFragmentShader;
115 
116  if (destination_rect_.IsEmpty() || source_rect_.IsEmpty() ||
117  texture_ == nullptr || texture_->GetSize().IsEmpty()) {
118  return true; // Nothing to render.
119  }
120 
121  bool is_external_texture =
122  texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
123 
124  // Expand the source rect by half a texel, which aligns sampled texels to the
125  // pixel grid if the source rect is the same size as the destination rect.
126  auto texture_coords =
127  Rect::MakeSize(texture_->GetSize())
128  .Project(capture.AddRect("Source rect", source_rect_).Expand(0.5));
129 
130  VertexBufferBuilder<VS::PerVertexData> vertex_builder;
131 
132  auto destination_rect =
133  capture.AddRect("Destination rect", destination_rect_);
134  vertex_builder.AddVertices({
135  {destination_rect.GetLeftTop(), texture_coords.GetLeftTop()},
136  {destination_rect.GetRightTop(), texture_coords.GetRightTop()},
137  {destination_rect.GetLeftBottom(), texture_coords.GetLeftBottom()},
138  {destination_rect.GetRightBottom(), texture_coords.GetRightBottom()},
139  });
140 
141  auto& host_buffer = pass.GetTransientsBuffer();
142 
143  VS::FrameInfo frame_info;
144  frame_info.mvp = pass.GetOrthographicTransform() *
145  capture.AddMatrix("Transform", entity.GetTransform());
146  frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
147  frame_info.alpha = capture.AddScalar("Alpha", GetOpacity());
148 
149  Command cmd;
150  if (label_.empty()) {
151  DEBUG_COMMAND_INFO(cmd, "Texture Fill");
152  } else {
153  DEBUG_COMMAND_INFO(cmd, "Texture Fill: " + label_);
154  }
155 
156  auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
157  if (!stencil_enabled_) {
158  pipeline_options.stencil_compare = CompareFunction::kAlways;
159  }
160  pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
161 
162 #ifdef IMPELLER_ENABLE_OPENGLES
163  if (is_external_texture) {
164  cmd.pipeline = renderer.GetTextureExternalPipeline(pipeline_options);
165  } else {
166  cmd.pipeline = renderer.GetTexturePipeline(pipeline_options);
167  }
168 #else
169  cmd.pipeline = renderer.GetTexturePipeline(pipeline_options);
170 #endif // IMPELLER_ENABLE_OPENGLES
171 
172  cmd.stencil_reference = entity.GetClipDepth();
173  cmd.BindVertices(vertex_builder.CreateVertexBuffer(host_buffer));
174  VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
175  if (is_external_texture) {
176  FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
177  cmd, texture_,
178  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
179  sampler_descriptor_));
180  } else {
181  FS::BindTextureSampler(
182  cmd, texture_,
183  renderer.GetContext()->GetSamplerLibrary()->GetSampler(
184  sampler_descriptor_));
185  }
186  pass.AddCommand(std::move(cmd));
187 
188  return true;
189 }

References impeller::RenderPass::AddCommand(), impeller::VertexBufferBuilder< VertexType_, IndexType_ >::AddVertices(), impeller::Command::BindVertices(), impeller::Capture::CreateChild(), impeller::VertexBufferBuilder< VertexType_, IndexType_ >::CreateVertexBuffer(), DEBUG_COMMAND_INFO, impeller::Entity::GetCapture(), impeller::Entity::GetClipDepth(), impeller::ContentContext::GetContext(), GetOpacity(), impeller::RenderPass::GetOrthographicTransform(), impeller::ContentContext::GetTexturePipeline(), impeller::Entity::GetTransform(), impeller::RenderPass::GetTransientsBuffer(), impeller::TRect< T >::IsEmpty(), impeller::kAlways, impeller::kTextureExternalOES, impeller::kTriangleStrip, impeller::TRect< Scalar >::MakeSize(), impeller::OptionsFromPassAndEntity(), impeller::Command::pipeline, impeller::TRect< T >::Project(), and impeller::Command::stencil_reference.

◆ 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,
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 76 of file texture_contents.cc.

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

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

◆ SetDeferApplyingOpacity()

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

Definition at line 207 of file texture_contents.cc.

207  {
208  defer_applying_opacity_ = defer_applying_opacity;
209 }

◆ SetDestinationRect()

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

Definition at line 37 of file texture_contents.cc.

37  {
38  destination_rect_ = rect;
39 }

◆ 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 61 of file texture_contents.cc.

61  {
62  inherited_opacity_ = opacity;
63 }

◆ SetLabel()

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

Definition at line 33 of file texture_contents.cc.

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

◆ SetOpacity()

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

Definition at line 49 of file texture_contents.cc.

49  {
50  opacity_ = opacity;
51 }

◆ SetSamplerDescriptor()

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

Definition at line 199 of file texture_contents.cc.

199  {
200  sampler_descriptor_ = std::move(desc);
201 }

◆ SetSourceRect()

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

Definition at line 191 of file texture_contents.cc.

191  {
192  source_rect_ = source_rect;
193 }

◆ SetStencilEnabled()

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

Definition at line 53 of file texture_contents.cc.

53  {
54  stencil_enabled_ = enabled;
55 }

◆ SetTexture()

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

Definition at line 41 of file texture_contents.cc.

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

The documentation for this class was generated from the following files:
impeller::TextureType::kTextureExternalOES
@ kTextureExternalOES
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:56
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:264
impeller::Vector2
Point Vector2
Definition: point.h:312
impeller::Size
TSize< Scalar > Size
Definition: size.h:137
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
impeller::TextureContents::GetOpacity
Scalar GetOpacity() const
Definition: texture_contents.cc:65
impeller::TRect::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: rect.h:138
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:28
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::CompareFunction::kAlways
@ kAlways
Comparison test passes always passes.
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:44
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:424
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:104
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