Flutter Engine
 
Loading...
Searching...
No Matches
texture_contents.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_IMPELLER_ENTITY_CONTENTS_TEXTURE_CONTENTS_H_
6#define FLUTTER_IMPELLER_ENTITY_CONTENTS_TEXTURE_CONTENTS_H_
7
8#include <memory>
9
12
13namespace impeller {
14
15class Texture;
16
17/// Represents the contents of a texture to be rendered.
18///
19/// This class encapsulates a texture along with parameters defining how it
20/// should be drawn, such as the source rectangle within the texture, the
21/// destination rectangle on the render target, opacity, and sampler settings.
22/// It's used by the rendering system to draw textured quads.
23///
24/// @see `TiledTextureContents` for a tiled version.
25class TextureContents final : public Contents {
26 public:
28
29 ~TextureContents() override;
30
31 /// A common case factory that marks the texture contents as having a
32 /// destination rectangle.
33 ///
34 /// In this situation, a subpass can be avoided when image filters are
35 /// applied.
36 ///
37 /// @param destination The destination rectangle in the Entity's local
38 /// coordinate space.
39 static std::shared_ptr<TextureContents> MakeRect(Rect destination);
40
41 /// Sets a debug label for this contents object.
42 ///
43 /// This label is used for debugging purposes, for example, in graphics
44 /// debuggers or logs.
45 ///
46 /// @param label The debug label string.
47 void SetLabel(std::string_view label);
48
49 /// Sets the destination rectangle within the current render target
50 /// where the texture will be drawn.
51 ///
52 /// The texture, potentially clipped by the `source_rect_`, will be mapped to
53 /// this rectangle. The coordinates are in the local coordinate space of the
54 /// Entity.
55 ///
56 /// @param rect The destination rectangle in the Entity's local coordinate
57 /// space.
58 void SetDestinationRect(Rect rect);
59
60 void SetTexture(std::shared_ptr<Texture> texture);
61
62 std::shared_ptr<Texture> GetTexture() const;
63
65
67
68 /// Sets the source rectangle within the texture to sample from.
69 ///
70 /// This rectangle defines the portion of the texture that will be mapped to
71 /// the `destination_rect_`. The coordinates are in the coordinate space of
72 /// the texture (texels), with the top-left corner being (0, 0).
73 ///
74 /// @param source_rect The rectangle defining the area of the texture to use.
75 void SetSourceRect(const Rect& source_rect);
76
77 const Rect& GetSourceRect() const;
78
79 /// Sets whether strict source rect sampling should be used.
80 ///
81 /// When enabled, the texture coordinates are adjusted slightly (typically by
82 /// half a texel) to ensure that linear filtering does not sample pixels
83 /// outside the specified `source_rect_`. This is useful for preventing
84 /// edge artifacts when rendering sub-sections of a texture atlas.
85 ///
86 /// @param strict True to enable strict source rect sampling, false otherwise.
87 void SetStrictSourceRect(bool strict);
88
89 bool GetStrictSourceRect() const;
90
91 void SetOpacity(Scalar opacity);
92
93 Scalar GetOpacity() const;
94
95 void SetStencilEnabled(bool enabled);
96
97 // |Contents|
98 std::optional<Rect> GetCoverage(const Entity& entity) const override;
99
100 // |Contents|
101 std::optional<Snapshot> RenderToSnapshot(
102 const ContentContext& renderer,
103 const Entity& entity,
104 const SnapshotOptions& options) const override;
105
106 // |Contents|
107 bool Render(const ContentContext& renderer,
108 const Entity& entity,
109 RenderPass& pass) const override;
110
111 // |Contents|
112 void SetInheritedOpacity(Scalar opacity) override;
113
114 /// Sets whether applying the opacity should be deferred.
115 ///
116 /// When true, the opacity value (`GetOpacity()`) might not be applied
117 /// directly during rendering operations like `RenderToSnapshot`. Instead, the
118 /// opacity might be stored in the resulting `Snapshot` to be applied later
119 /// when the snapshot is drawn. This is typically used as an optimization when
120 /// the texture covers its destination rectangle completely and has near-full
121 /// opacity, allowing the original texture to be used directly in the
122 /// snapshot.
123 ///
124 /// @param defer_applying_opacity True to defer applying opacity, false to
125 /// apply it during rendering.
126 void SetDeferApplyingOpacity(bool defer_applying_opacity);
127
128 private:
129 std::string label_;
130
131 Rect destination_rect_;
132 bool stencil_enabled_ = true;
133
134 std::shared_ptr<Texture> texture_;
135 SamplerDescriptor sampler_descriptor_ = {};
136 Rect source_rect_;
137 bool strict_source_rect_enabled_ = false;
138 Scalar opacity_ = 1.0f;
139 Scalar inherited_opacity_ = 1.0f;
140 bool defer_applying_opacity_ = false;
141
142 TextureContents(const TextureContents&) = delete;
143
144 TextureContents& operator=(const TextureContents&) = delete;
145};
146
147} // namespace impeller
148
149#endif // FLUTTER_IMPELLER_ENTITY_CONTENTS_TEXTURE_CONTENTS_H_
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition render_pass.h:30
std::shared_ptr< Texture > GetTexture() const
void SetSourceRect(const Rect &source_rect)
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
void SetSamplerDescriptor(const SamplerDescriptor &desc)
void SetStrictSourceRect(bool strict)
void SetDeferApplyingOpacity(bool defer_applying_opacity)
const SamplerDescriptor & GetSamplerDescriptor() const
void SetOpacity(Scalar opacity)
static std::shared_ptr< TextureContents > MakeRect(Rect destination)
const Rect & GetSourceRect() const
void SetInheritedOpacity(Scalar opacity) override
Inherit the provided opacity.
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
void SetTexture(std::shared_ptr< Texture > texture)
void SetLabel(std::string_view label)
std::optional< Snapshot > RenderToSnapshot(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,...
void SetStencilEnabled(bool enabled)
void SetDestinationRect(Rect rect)
FlTexture * texture
float Scalar
Definition scalar.h:19