Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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_view label)
 
void SetDestinationRect (Rect rect)
 
void SetTexture (std::shared_ptr< Texture > texture)
 
std::shared_ptr< TextureGetTexture () const
 
void SetSamplerDescriptor (const 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.
 
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).
 
bool Render (const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
 
void SetInheritedOpacity (Scalar opacity) override
 Inherit the provided opacity.
 
void SetDeferApplyingOpacity (bool defer_applying_opacity)
 
void SetNeedsRasterizationForRuntimeEffects (bool value)
 
- 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().
 
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.
 
std::optional< SizeGetColorSourceSize () const
 Return the color source's intrinsic size, if available.
 
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.
 
virtual bool ApplyColorFilter (const ColorFilterProc &color_filter_proc)
 If possible, applies a color filter to this contents inputs on the CPU.
 

Static Public Member Functions

static std::shared_ptr< TextureContentsMakeRect (Rect destination)
 
- 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

Represents the contents of a texture to be rendered.

This class encapsulates a texture along with parameters defining how it should be drawn, such as the source rectangle within the texture, the destination rectangle on the render target, opacity, and sampler settings. It's used by the rendering system to draw textured quads.

See also
TiledTextureContents for a tiled version.

Definition at line 25 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 67 of file texture_contents.cc.

67 {
68 if (GetOpacity() == 0) {
69 return std::nullopt;
70 }
71 return destination_rect_.TransformBounds(entity.GetTransform());
72};
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition rect.h:506

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

◆ GetOpacity()

Scalar impeller::TextureContents::GetOpacity ( ) const

Definition at line 63 of file texture_contents.cc.

63 {
64 return opacity_ * inherited_opacity_;
65}

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

◆ GetSamplerDescriptor()

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

Definition at line 259 of file texture_contents.cc.

259 {
260 return sampler_descriptor_;
261}

◆ GetSourceRect()

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

Definition at line 243 of file texture_contents.cc.

243 {
244 return source_rect_;
245}

◆ GetStrictSourceRect()

bool impeller::TextureContents::GetStrictSourceRect ( ) const

Definition at line 251 of file texture_contents.cc.

251 {
252 return strict_source_rect_enabled_;
253}

◆ GetTexture()

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

Definition at line 47 of file texture_contents.cc.

47 {
48 return texture_;
49}

◆ 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.

Parameters
destinationThe destination rectangle in the Entity's local coordinate space.

Definition at line 29 of file texture_contents.cc.

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

Referenced by impeller::Canvas::DrawImageRect(), impeller::Entity::FromSnapshot(), impeller::Canvas::SaveLayer(), impeller::testing::TEST_P(), 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#if defined(IMPELLER_ENABLE_OPENGLES) && !defined(FML_OS_EMSCRIPTEN)
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& data_host_buffer = renderer.GetTransientsDataBuffer();
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, data_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::kSrc;
160
161#if defined(IMPELLER_ENABLE_OPENGLES) && !defined(FML_OS_EMSCRIPTEN)
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, data_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 if (texture_->GetYCoordScale() < 0.0) {
189 FML_DCHECK(texture_->GetYCoordScale() == -1.0f);
190 frag_info.source_rect = Vector4(strict_texture_coords.GetLeft(),
191 1.0f - strict_texture_coords.GetBottom(),
192 strict_texture_coords.GetRight(),
193 1.0f - strict_texture_coords.GetTop());
194 } else {
195 frag_info.source_rect = Vector4(strict_texture_coords.GetLTRB());
196 }
197 frag_info.alpha = GetOpacity();
198 FSStrict::BindFragInfo(pass, data_host_buffer.EmplaceUniform((frag_info)));
199 FSStrict::BindTextureSampler(
200 pass, texture_,
201 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
202 sampler_descriptor_));
203#if defined(IMPELLER_ENABLE_OPENGLES) && !defined(FML_OS_EMSCRIPTEN)
204 } else if (is_external_texture) {
205 FSExternal::FragInfo frag_info;
206 frag_info.x_tile_mode =
207 static_cast<Scalar>(sampler_descriptor_.width_address_mode);
208 frag_info.y_tile_mode =
209 static_cast<Scalar>(sampler_descriptor_.height_address_mode);
210 frag_info.alpha = GetOpacity();
211 FSExternal::BindFragInfo(pass, data_host_buffer.EmplaceUniform(frag_info));
212
213 SamplerDescriptor sampler_desc;
214 // OES_EGL_image_external states that only CLAMP_TO_EDGE is valid, so
215 // we emulate all other tile modes here by remapping the texture
216 // coordinates.
217 sampler_desc.width_address_mode = SamplerAddressMode::kClampToEdge;
218 sampler_desc.height_address_mode = SamplerAddressMode::kClampToEdge;
219 sampler_desc.min_filter = sampler_descriptor_.min_filter;
220 sampler_desc.mag_filter = sampler_descriptor_.mag_filter;
221 sampler_desc.mip_filter = MipFilter::kBase;
222
223 FSExternal::BindSAMPLEREXTERNALOESTextureSampler(
224 pass, texture_,
225 renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc));
226#endif // IMPELLER_ENABLE_OPENGLES
227 } else {
228 FS::FragInfo frag_info;
229 frag_info.alpha = GetOpacity();
230 FS::BindFragInfo(pass, data_host_buffer.EmplaceUniform((frag_info)));
231 FS::BindTextureSampler(
232 pass, texture_,
233 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
234 sampler_descriptor_));
235 }
236 return pass.Draw().ok();
237}
#define FML_DCHECK(condition)
Definition logging.h:122
float Scalar
Definition scalar.h:19
LinePipeline::FragmentShader FS
VertexBuffer CreateVertexBuffer(std::array< VertexType, size > input, HostBuffer &data_host_buffer)
Create an index-less vertex buffer from a fixed size array.
@ kBase
The texture is sampled as if it only had a single mipmap level.
LinePipeline::VertexShader VS
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition contents.cc:34
@ kIgnore
Turn the stencil test off. Used when drawing without stencil-then-cover.
SamplerAddressMode width_address_mode
SamplerAddressMode height_address_mode
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:703
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition rect.h:331
constexpr TPoint< T > GetLeftTop() const
Definition rect.h:393
constexpr TPoint< T > GetRightBottom() const
Definition rect.h:405
constexpr TPoint< T > GetLeftBottom() const
Definition rect.h:401
static constexpr TRect MakeSize(const TSize< U > &size)
Definition rect.h:150
constexpr TPoint< T > GetRightTop() const
Definition rect.h:397
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:652

References impeller::CreateVertexBuffer(), impeller::RenderPass::Draw(), impeller::TRect< T >::Expand(), FML_DCHECK, 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::GetTransientsDataBuffer(), impeller::SamplerDescriptor::height_address_mode, impeller::TRect< T >::IsEmpty(), impeller::kBase, impeller::kClampToEdge, impeller::ContentContextOptions::kIgnore, impeller::kSrc, impeller::kTextureExternalOES, impeller::kTriangleStrip, impeller::SamplerDescriptor::mag_filter, impeller::TRect< Scalar >::MakeSize(), impeller::SamplerDescriptor::min_filter, impeller::SamplerDescriptor::mip_filter, fml::Status::ok(), 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,
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 74 of file texture_contents.cc.

77 {
78 // Passthrough textures that have simple rectangle paths and complete source
79 // rects.
80 auto bounds = destination_rect_;
81 auto opacity = GetOpacity();
82 if (source_rect_ == Rect::MakeSize(texture_->GetSize()) &&
83 (opacity >= 1 - kEhCloseEnough || defer_applying_opacity_)) {
84 auto scale = Vector2(bounds.GetSize() / Size(texture_->GetSize()));
85 return Snapshot{.texture = texture_,
86 .transform = entity.GetTransform() *
87 Matrix::MakeTranslation(bounds.GetOrigin()) *
88 Matrix::MakeScale(scale),
89 .sampler_descriptor = options.sampler_descriptor.value_or(
90 sampler_descriptor_),
91 .opacity = opacity,
92 .needs_rasterization_for_runtime_effects =
93 snapshots_need_rasterization_for_runtime_effects_};
94 }
96 renderer, entity,
97 {.coverage_limit = std::nullopt,
98 .sampler_descriptor =
99 options.sampler_descriptor.value_or(sampler_descriptor_),
100 .msaa_enabled = true,
101 .mip_count = options.mip_count,
102 .label = options.label,
103 .coverage_expansion = options.coverage_expansion});
104}
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
Point Vector2
Definition point.h:430
constexpr float kEhCloseEnough
Definition constants.h:57
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

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

◆ SetDeferApplyingOpacity()

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

Sets whether applying the opacity should be deferred.

When true, the opacity value (GetOpacity()) might not be applied directly during rendering operations like RenderToSnapshot. Instead, the opacity might be stored in the resulting Snapshot to be applied later when the snapshot is drawn. This is typically used as an optimization when the texture covers its destination rectangle completely and has near-full opacity, allowing the original texture to be used directly in the snapshot.

Parameters
defer_applying_opacityTrue to defer applying opacity, false to apply it during rendering.

Definition at line 263 of file texture_contents.cc.

263 {
264 defer_applying_opacity_ = defer_applying_opacity;
265}

◆ SetDestinationRect()

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

Sets the destination rectangle within the current render target where the texture will be drawn.

The texture, potentially clipped by the source_rect_, will be mapped to this rectangle. The coordinates are in the local coordinate space of the Entity.

Parameters
rectThe destination rectangle in the Entity's local coordinate space.

Definition at line 39 of file texture_contents.cc.

39 {
40 destination_rect_ = rect;
41}

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

59 {
60 inherited_opacity_ = opacity;
61}

◆ SetLabel()

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

Sets a debug label for this contents object.

This label is used for debugging purposes, for example, in graphics debuggers or logs.

Parameters
labelThe debug label string.

Definition at line 35 of file texture_contents.cc.

35 {
36 label_ = label;
37}

◆ SetNeedsRasterizationForRuntimeEffects()

void impeller::TextureContents::SetNeedsRasterizationForRuntimeEffects ( bool  value)
See also
Snapshot::needs_rasterization_for_runtime_effects

Definition at line 267 of file texture_contents.cc.

267 {
268 snapshots_need_rasterization_for_runtime_effects_ = value;
269}
int32_t value

References value.

◆ SetOpacity()

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

Definition at line 51 of file texture_contents.cc.

51 {
52 opacity_ = opacity;
53}

◆ SetSamplerDescriptor()

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

Definition at line 255 of file texture_contents.cc.

255 {
256 sampler_descriptor_ = desc;
257}

◆ SetSourceRect()

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

Sets the source rectangle within the texture to sample from.

This rectangle defines the portion of the texture that will be mapped to the destination_rect_. The coordinates are in the coordinate space of the texture (texels), with the top-left corner being (0, 0).

Parameters
source_rectThe rectangle defining the area of the texture to use.

Definition at line 239 of file texture_contents.cc.

239 {
240 source_rect_ = source_rect;
241}

◆ SetStencilEnabled()

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

Definition at line 55 of file texture_contents.cc.

55 {
56 stencil_enabled_ = enabled;
57}

◆ SetStrictSourceRect()

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

Sets whether strict source rect sampling should be used.

When enabled, the texture coordinates are adjusted slightly (typically by half a texel) to ensure that linear filtering does not sample pixels outside the specified source_rect_. This is useful for preventing edge artifacts when rendering sub-sections of a texture atlas.

Parameters
strictTrue to enable strict source rect sampling, false otherwise.

Definition at line 247 of file texture_contents.cc.

247 {
248 strict_source_rect_enabled_ = strict;
249}

◆ SetTexture()

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

Definition at line 43 of file texture_contents.cc.

43 {
44 texture_ = std::move(texture);
45}
FlTexture * texture

References texture.


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