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 screen space bounding rectangle that this contents affects. 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, stencil 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 StencilCoverage GetStencilCoverage (const Entity &entity, const std::optional< Rect > &current_stencil_coverage) const
 Given the current screen space bounding rectangle of the stencil, return the expected stencil coverage after this draw call. This should only be implemented for contents that may write to the stencil buffer. More...
 
virtual bool ShouldRender (const Entity &entity, const std::optional< Rect > &stencil_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 20 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 58 of file texture_contents.cc.

58  {
59  return true;
60 }

◆ GetCoverage()

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

Get the screen space bounding rectangle that this contents affects.

Implements impeller::Contents.

Definition at line 70 of file texture_contents.cc.

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

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

◆ GetOpacity()

Scalar impeller::TextureContents::GetOpacity ( ) const

Definition at line 66 of file texture_contents.cc.

66  {
67  return opacity_ * inherited_opacity_;
68 }

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

◆ GetSamplerDescriptor()

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

Definition at line 204 of file texture_contents.cc.

204  {
205  return sampler_descriptor_;
206 }

◆ GetSourceRect()

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

Definition at line 196 of file texture_contents.cc.

196  {
197  return source_rect_;
198 }

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

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

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::ContentContext::GetContext(), GetOpacity(), impeller::RenderPass::GetRenderTargetSize(), impeller::Entity::GetStencilDepth(), impeller::ContentContext::GetTexturePipeline(), impeller::Entity::GetTransformation(), impeller::RenderPass::GetTransientsBuffer(), impeller::TSize< T >::IsEmpty(), impeller::TRect< T >::IsEmpty(), impeller::kAlways, impeller::kTextureExternalOES, impeller::kTriangleStrip, impeller::Matrix::MakeOrthographic(), impeller::TRect< Scalar >::MakeSize(), impeller::OptionsFromPassAndEntity(), impeller::Command::pipeline, impeller::TRect< T >::Project(), impeller::TRect< T >::size, 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, stencil depth, and blend mode. The result texture size is always the size of GetCoverage(entity).

Reimplemented from impeller::Contents.

Definition at line 77 of file texture_contents.cc.

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

References GetOpacity(), impeller::Entity::GetTransformation(), 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 208 of file texture_contents.cc.

208  {
209  defer_applying_opacity_ = defer_applying_opacity;
210 }

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

62  {
63  inherited_opacity_ = opacity;
64 }

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

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

◆ SetSourceRect()

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

Definition at line 192 of file texture_contents.cc.

192  {
193  source_rect_ = source_rect;
194 }

◆ SetStencilEnabled()

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

Definition at line 54 of file texture_contents.cc.

54  {
55  stencil_enabled_ = enabled;
56 }

◆ 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::TextureType::kTextureExternalOES
@ kTextureExternalOES
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:31
impeller::TRect::size
TSize< Type > size
Definition: rect.h:24
impeller::kEhCloseEnough
constexpr float kEhCloseEnough
Definition: constants.h:55
impeller::TRect::TransformBounds
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition: rect.h:204
impeller::Vector2
Point Vector2
Definition: point.h:310
impeller::Size
TSize< Scalar > Size
Definition: size.h:135
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:94
impeller::PrimitiveType::kTriangleStrip
@ kTriangleStrip
impeller::TextureContents::GetOpacity
Scalar GetOpacity() const
Definition: texture_contents.cc:66
impeller::TRect::IsEmpty
constexpr bool IsEmpty() const
Definition: rect.h:133
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:30
impeller::CompareFunction::kAlways
@ kAlways
Comparison test passes always passes.
impeller::TRect< Scalar >::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:52
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:299
impeller::Matrix::MakeOrthographic
static constexpr Matrix MakeOrthographic(TSize< T > size)
Definition: matrix.h:448
impeller::TSize::IsEmpty
constexpr bool IsEmpty() const
Definition: size.h:105
impeller::Matrix::MakeScale
static constexpr Matrix MakeScale(const Vector3 &s)
Definition: matrix.h:103
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, stencil depth,...
Definition: contents.cc:59