| Flutter Impeller |

texture_gles.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_RENDERER_BACKEND_GLES_TEXTURE_GLES_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_TEXTURE_GLES_H_
7
8 #include <bitset>
9
10 #include "fml/logging.h"
11 #include "impeller/base/backend_cast.h"
12 #include "impeller/core/texture.h"
13 #include "impeller/renderer/backend/gles/handle_gles.h"
14 #include "impeller/renderer/backend/gles/reactor_gles.h"
15
16 namespace impeller {
17
18 class TextureGLES final : public Texture,
19 public BackendCast<TextureGLES, Texture> {
20 public:
21 enum class Type {
22 kTexture,
23 kTextureMultisampled,
24 kRenderBuffer,
25 kRenderBufferMultisampled,
26 };
27
28 //----------------------------------------------------------------------------
29 /// @brief Create a texture by wrapping an external framebuffer object
30 /// whose lifecycle is owned by the caller.
31 ///
32 /// This is useful for creating a render target for the default
33 /// window managed framebuffer.
34 ///
35 /// @param[in] reactor The reactor
36 /// @param[in] desc The description
37 /// @param[in] fbo The fbo
38 ///
39 /// @return If a texture representation of the framebuffer could be
40 /// created.
41 ///
42 static std::shared_ptr<TextureGLES> WrapFBO(
43 std::shared_ptr<ReactorGLES> reactor,
44 TextureDescriptor desc,
45 GLuint fbo);
46
47 //----------------------------------------------------------------------------
48 /// @brief Create a texture by wrapping an external OpenGL texture
49 /// handle. Ownership of the texture handle is assumed by the
50 /// reactor.
51 ///
52 /// @param[in] reactor The reactor
53 /// @param[in] desc The description
54 /// @param[in] external_handle The external handle
55 ///
56 /// @return If a texture representation of the framebuffer could be
57 /// created.
58 ///
59 static std::shared_ptr<TextureGLES> WrapTexture(
60 std::shared_ptr<ReactorGLES> reactor,
61 TextureDescriptor desc,
62 HandleGLES external_handle);
63
64 //----------------------------------------------------------------------------
65 /// @brief Create a "texture" that is never expected to be bound/unbound
66 /// explicitly or initialized in any way. It only exists to setup
67 /// a render pass description.
68 ///
69 /// @param[in] reactor The reactor
70 /// @param[in] desc The description
71 ///
72 /// @return If a texture placeholder could be created.
73 ///
74 static std::shared_ptr<TextureGLES> CreatePlaceholder(
75 std::shared_ptr<ReactorGLES> reactor,
76 TextureDescriptor desc);
77
78 TextureGLES(std::shared_ptr<ReactorGLES> reactor,
79 TextureDescriptor desc,
80 bool threadsafe = false);
81
82 // |Texture|
83 ~TextureGLES() override;
84
85 // |Texture|
86 bool IsValid() const override;
87
88 std::optional<GLuint> GetGLHandle() const;
89
90 [[nodiscard]] bool Bind() const;
91
92 [[nodiscard]] bool GenerateMipmap();
93
94 enum class AttachmentType {
95 kColor0,
96 kDepth,
97 kStencil,
98 };
99 [[nodiscard]] bool SetAsFramebufferAttachment(
100 GLenum target,
101 AttachmentType attachment_type) const;
102
103 Type GetType() const;
104
105 bool IsWrapped() const;
106
107 /// @brief Reset the internal texture state so that the reactor will not free
108 /// the associated handle.
109 void Leak();
110
111 std::optional<GLuint> GetFBO() const;
112
113 //----------------------------------------------------------------------------
114 /// @brief Indicates that all texture storage has already been allocated
115 /// and contents initialized.
116 ///
117 /// This is similar to calling `MarkSliceInitialized` with all
118 /// slices.
119 ///
120 /// @see MarkSliceInitialized.
121 ///
122 void MarkContentsInitialized();
123
124 //----------------------------------------------------------------------------
125 /// @brief Indicates that a specific texture slice has been initialized.
126 ///
127 /// @param[in] slice The slice to mark as being initialized.
128 ///
129 void MarkSliceInitialized(size_t slice) const;
130
131 bool IsSliceInitialized(size_t slice) const;
132
133 //----------------------------------------------------------------------------
134 /// @brief Attach a sync fence to this texture that will be waited on
135 /// before encoding a rendering operation that references it.
136 ///
137 /// @param[in] fence A handle to a sync fence.
138 ///
139 void SetFence(HandleGLES fence);
140
141 /// Store the FBO object for recycling in the 2D renderer.
142 ///
143 /// The color0 texture used by the 2D renderer will use this texture
144 /// object to store the associated FBO the first time it is used.
145 void SetCachedFBO(HandleGLES fbo);
146
147 /// Retrieve the cached FBO object, or a dead handle if there is no object.
148 const HandleGLES& GetCachedFBO() const;
149
150 // Visible for testing.
151 std::optional<HandleGLES> GetSyncFence() const;
152
153 // visible for testing
154 Type ComputeTypeForBinding(GLenum target) const;
155
156 private:
157 std::shared_ptr<ReactorGLES> reactor_;
158 const Type type_;
159 HandleGLES handle_;
160 mutable std::optional<HandleGLES> fence_ = std::nullopt;
161 mutable std::bitset<6> slices_initialized_ = 0;
162 const bool is_wrapped_;
163 const std::optional<GLuint> wrapped_fbo_;
164 HandleGLES cached_fbo_ = HandleGLES::DeadHandle();
165 bool is_valid_ = false;
166
167 TextureGLES(std::shared_ptr<ReactorGLES> reactor,
168 TextureDescriptor desc,
169 bool threadsafe,
170 std::optional<GLuint> fbo,
171 std::optional<HandleGLES> external_handle);
172
173 // |Texture|
174 void SetLabel(std::string_view label) override;
175
176 // |Texture|
177 void SetLabel(std::string_view label, std::string_view trailing) override;
178
179 // |Texture|
180 bool OnSetContents(const uint8_t* contents,
181 size_t length,
182 size_t slice) override;
183
184 // |Texture|
185 bool OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
186 size_t slice) override;
187
188 // |Texture|
189 ISize GetSize() const override;
190
191 // |Texture|
192 Scalar GetYCoordScale() const override;
193
194 void InitializeContentsIfNecessary() const;
195
196 TextureGLES(const TextureGLES&) = delete;
197
198 TextureGLES& operator=(const TextureGLES&) = delete;
199 };
200
201 } // namespace impeller
202
203 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_TEXTURE_GLES_H_
backend_cast.h
impeller::BackendCast
Definition: backend_cast.h:11
impeller::HandleGLES
Represents a handle to an underlying OpenGL object. Unlike OpenGL object handles, these handles can b...
Definition: handle_gles.h:37
impeller::HandleGLES::DeadHandle
static HandleGLES DeadHandle()
Creates a dead handle.
Definition: handle_gles.h:44
impeller::TextureGLES
Definition: texture_gles.h:19
impeller::TextureGLES::WrapFBO
static std::shared_ptr< TextureGLES > WrapFBO(std::shared_ptr< ReactorGLES > reactor, TextureDescriptor desc, GLuint fbo)
Create a texture by wrapping an external framebuffer object whose lifecycle is owned by the caller.
Definition: texture_gles.cc:147
impeller::TextureGLES::MarkContentsInitialized
void MarkContentsInitialized()
Indicates that all texture storage has already been allocated and contents initialized.
Definition: texture_gles.cc:551
impeller::TextureGLES::GetCachedFBO
const HandleGLES & GetCachedFBO() const
Retrieve the cached FBO object, or a dead handle if there is no object.
Definition: texture_gles.cc:693
impeller::TextureGLES::GetSyncFence
std::optional< HandleGLES > GetSyncFence() const
Definition: texture_gles.cc:685
impeller::TextureGLES::IsSliceInitialized
bool IsSliceInitialized(size_t slice) const
Definition: texture_gles.cc:561
impeller::TextureGLES::Bind
bool Bind() const
Definition: texture_gles.cc:516
impeller::TextureGLES::Type
Type
Definition: texture_gles.h:21
impeller::TextureGLES::Type::kTexture
@ kTexture
impeller::TextureGLES::Type::kRenderBuffer
@ kRenderBuffer
impeller::TextureGLES::Type::kTextureMultisampled
@ kTextureMultisampled
impeller::TextureGLES::Type::kRenderBufferMultisampled
@ kRenderBufferMultisampled
impeller::TextureGLES::IsValid
bool IsValid() const override
Definition: texture_gles.cc:243
impeller::TextureGLES::SetFence
void SetFence(HandleGLES fence)
Attach a sync fence to this texture that will be waited on before encoding a rendering operation that...
Definition: texture_gles.cc:679
impeller::TextureGLES::AttachmentType
AttachmentType
Definition: texture_gles.h:94
impeller::TextureGLES::AttachmentType::kStencil
@ kStencil
impeller::TextureGLES::AttachmentType::kDepth
@ kDepth
impeller::TextureGLES::AttachmentType::kColor0
@ kColor0
impeller::TextureGLES::GetType
Type GetType() const
Definition: texture_gles.cc:599
impeller::TextureGLES::Leak
void Leak()
Reset the internal texture state so that the reactor will not free the associated handle.
Definition: texture_gles.cc:238
impeller::TextureGLES::SetCachedFBO
void SetCachedFBO(HandleGLES fbo)
Definition: texture_gles.cc:689
impeller::TextureGLES::TextureGLES
TextureGLES(std::shared_ptr< ReactorGLES > reactor, TextureDescriptor desc, bool threadsafe=false)
Definition: texture_gles.cc:185
impeller::TextureGLES::SetAsFramebufferAttachment
bool SetAsFramebufferAttachment(GLenum target, AttachmentType attachment_type) const
Definition: texture_gles.cc:614
impeller::TextureGLES::CreatePlaceholder
static std::shared_ptr< TextureGLES > CreatePlaceholder(std::shared_ptr< ReactorGLES > reactor, TextureDescriptor desc)
Create a "texture" that is never expected to be bound/unbound explicitly or initialized in any way....
Definition: texture_gles.cc:179
impeller::TextureGLES::GetFBO
std::optional< GLuint > GetFBO() const
Definition: texture_gles.cc:675
impeller::TextureGLES::ComputeTypeForBinding
Type ComputeTypeForBinding(GLenum target) const
Definition: texture_gles.cc:410
impeller::TextureGLES::MarkSliceInitialized
void MarkSliceInitialized(size_t slice) const
Indicates that a specific texture slice has been initialized.
Definition: texture_gles.cc:557
impeller::TextureGLES::GenerateMipmap
bool GenerateMipmap()
Definition: texture_gles.cc:565
impeller::TextureGLES::GetGLHandle
std::optional< GLuint > GetGLHandle() const
Definition: texture_gles.cc:509
impeller::TextureGLES::~TextureGLES
~TextureGLES() override
Definition: texture_gles.cc:231
impeller::TextureGLES::WrapTexture
static std::shared_ptr< TextureGLES > WrapTexture(std::shared_ptr< ReactorGLES > reactor, TextureDescriptor desc, HandleGLES external_handle)
Create a texture by wrapping an external OpenGL texture handle. Ownership of the texture handle is as...
Definition: texture_gles.cc:159
impeller::TextureGLES::IsWrapped
bool IsWrapped() const
Definition: texture_gles.cc:671
impeller::Texture
Definition: texture.h:17
texture.h
handle_gles.h
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:19
reactor_gles.h
impeller::TSize
Definition: size.h:25
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:38