Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
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.
 
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).
 
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.
 
void SetInheritedOpacity (Scalar opacity) override
 Inherit the provided opacity.
 
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.
 
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
 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.
 
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.
 
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.
 
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 const FilterContentsAsFilter () const
 Cast to a filter. Returns nullptr if this Contents is not a filter.
 
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)
 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.
 
- 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};
constexpr TRect TransformBounds(const Matrix &transform) const
Creates a new bounding box that contains this transformed rectangle.
Definition rect.h:440

◆ GetOpacity()

Scalar impeller::TextureContents::GetOpacity ( ) const

Definition at line 65 of file texture_contents.cc.

65 {
66 return opacity_ * inherited_opacity_;
67}

◆ GetSamplerDescriptor()

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

Definition at line 215 of file texture_contents.cc.

215 {
216 return sampler_descriptor_;
217}

◆ GetSourceRect()

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

Definition at line 199 of file texture_contents.cc.

199 {
200 return source_rect_;
201}

◆ GetStrictSourceRect()

bool impeller::TextureContents::GetStrictSourceRect ( ) const

Definition at line 207 of file texture_contents.cc.

207 {
208 return strict_source_rect_enabled_;
209}

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

◆ Render()

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

Implements impeller::Contents.

Definition at line 109 of file texture_contents.cc.

111 {
112 auto capture = entity.GetCapture().CreateChild("TextureContents");
113
114 using VS = TextureFillVertexShader;
115 using FS = TextureFillFragmentShader;
116 using FSStrict = TextureFillStrictSrcFragmentShader;
117
118 if (destination_rect_.IsEmpty() || source_rect_.IsEmpty() ||
119 texture_ == nullptr || texture_->GetSize().IsEmpty()) {
120 return true; // Nothing to render.
121 }
122
123 [[maybe_unused]] bool is_external_texture =
124 texture_->GetTextureDescriptor().type == TextureType::kTextureExternalOES;
125 FML_DCHECK(!is_external_texture);
126
127 auto source_rect = capture.AddRect("Source rect", source_rect_);
128 auto texture_coords =
129 Rect::MakeSize(texture_->GetSize()).Project(source_rect);
130
131 VertexBufferBuilder<VS::PerVertexData> vertex_builder;
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 = renderer.GetTransientsBuffer();
142
143 VS::FrameInfo frame_info;
144 frame_info.mvp = entity.GetShaderTransform(pass);
145 frame_info.texture_sampler_y_coord_scale = texture_->GetYCoordScale();
146
147#ifdef IMPELLER_DEBUG
148 if (label_.empty()) {
149 pass.SetCommandLabel("Texture Fill");
150 } else {
151 pass.SetCommandLabel("Texture Fill: " + label_);
152 }
153#endif // IMPELLER_DEBUG
154
155 auto pipeline_options = OptionsFromPassAndEntity(pass, entity);
156 if (!stencil_enabled_) {
157 pipeline_options.stencil_mode = ContentContextOptions::StencilMode::kIgnore;
158 }
159 pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
160
161 pass.SetPipeline(strict_source_rect_enabled_
162 ? renderer.GetTextureStrictSrcPipeline(pipeline_options)
163 : renderer.GetTexturePipeline(pipeline_options));
164
165 pass.SetVertexBuffer(vertex_builder.CreateVertexBuffer(host_buffer));
166 VS::BindFrameInfo(pass, host_buffer.EmplaceUniform(frame_info));
167
168 if (strict_source_rect_enabled_) {
169 // For a strict source rect, shrink the texture coordinate range by half a
170 // texel to ensure that linear filtering does not sample anything outside
171 // the source rect bounds.
172 auto strict_texture_coords =
173 Rect::MakeSize(texture_->GetSize()).Project(source_rect.Expand(-0.5));
174
175 FSStrict::FragInfo frag_info;
176 frag_info.source_rect = Vector4(strict_texture_coords.GetLTRB());
177 frag_info.alpha = capture.AddScalar("Alpha", GetOpacity());
178 FSStrict::BindFragInfo(pass, host_buffer.EmplaceUniform((frag_info)));
179 FSStrict::BindTextureSampler(
180 pass, texture_,
181 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
182 sampler_descriptor_));
183 } else {
184 FS::FragInfo frag_info;
185 frag_info.alpha = capture.AddScalar("Alpha", GetOpacity());
186 FS::BindFragInfo(pass, host_buffer.EmplaceUniform((frag_info)));
187 FS::BindTextureSampler(
188 pass, texture_,
189 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
190 sampler_descriptor_));
191 }
192 return pass.Draw().ok();
193}
#define FML_DCHECK(condition)
Definition logging.h:103
SolidFillVertexShader VS
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition contents.cc:35
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:633
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition rect.h:264
static constexpr TRect MakeSize(const TSize< U > &size)
Definition rect.h:146

◆ 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 76 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.GetSize() / Size(texture_->GetSize()));
91 return Snapshot{
92 .texture = texture_,
93 .transform = entity.GetTransform() *
94 Matrix::MakeTranslation(bounds.GetOrigin()) *
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 /*mip_count=*/mip_count,
106 label); // label
107}
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:64
Optional< SkRect > bounds
Definition SkRecords.h:189
Point Vector2
Definition point.h:320
constexpr float kEhCloseEnough
Definition constants.h:56
TSize< Scalar > Size
Definition size.h:137
const Scalar scale
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition matrix.h:95
static constexpr Matrix MakeScale(const Vector3 &s)
Definition matrix.h:104

◆ SetDeferApplyingOpacity()

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

Definition at line 219 of file texture_contents.cc.

219 {
220 defer_applying_opacity_ = defer_applying_opacity;
221}

◆ SetDestinationRect()

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

Definition at line 37 of file texture_contents.cc.

37 {
38 destination_rect_ = rect;
39}
sk_sp< SkBlender > blender SkRect rect
Definition SkRecords.h:350

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

211 {
212 sampler_descriptor_ = std::move(desc);
213}

◆ SetSourceRect()

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

Definition at line 195 of file texture_contents.cc.

195 {
196 source_rect_ = source_rect;
197}

◆ SetStencilEnabled()

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

Definition at line 53 of file texture_contents.cc.

53 {
54 stencil_enabled_ = enabled;
55}

◆ SetStrictSourceRect()

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

Definition at line 203 of file texture_contents.cc.

203 {
204 strict_source_rect_enabled_ = strict;
205}

◆ 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}
FlTexture * texture

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