Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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 <array>
9#include <bitset>
10
11#include "fml/logging.h"
16
17namespace impeller {
18
19class TextureGLES final : public Texture,
20 public BackendCast<TextureGLES, Texture> {
21 public:
28
29 //----------------------------------------------------------------------------
30 /// @brief Create a texture by wrapping an external framebuffer object
31 /// whose lifecycle is owned by the caller.
32 ///
33 /// This is useful for creating a render target for the default
34 /// window managed framebuffer.
35 ///
36 /// @param[in] reactor The reactor
37 /// @param[in] desc The description
38 /// @param[in] fbo The fbo
39 ///
40 /// @return If a texture representation of the framebuffer could be
41 /// created.
42 ///
43 static std::shared_ptr<TextureGLES> WrapFBO(
44 std::shared_ptr<ReactorGLES> reactor,
46 GLuint fbo);
47
48 //----------------------------------------------------------------------------
49 /// @brief Create a texture by wrapping an external OpenGL texture
50 /// handle. Ownership of the texture handle is assumed by the
51 /// reactor.
52 ///
53 /// @param[in] reactor The reactor
54 /// @param[in] desc The description
55 /// @param[in] external_handle The external handle
56 ///
57 /// @return If a texture representation of the framebuffer could be
58 /// created.
59 ///
60 static std::shared_ptr<TextureGLES> WrapTexture(
61 std::shared_ptr<ReactorGLES> reactor,
63 HandleGLES external_handle);
64
65 //----------------------------------------------------------------------------
66 /// @brief Create a "texture" that is never expected to be bound/unbound
67 /// explicitly or initialized in any way. It only exists to setup
68 /// a render pass description.
69 ///
70 /// @param[in] reactor The reactor
71 /// @param[in] desc The description
72 ///
73 /// @return If a texture placeholder could be created.
74 ///
75 static std::shared_ptr<TextureGLES> CreatePlaceholder(
76 std::shared_ptr<ReactorGLES> reactor,
78
79 TextureGLES(std::shared_ptr<ReactorGLES> reactor,
81 bool threadsafe = false);
82
83 // |Texture|
84 ~TextureGLES() override;
85
86 // |Texture|
87 bool IsValid() const override;
88
89 std::optional<GLuint> GetGLHandle() const;
90
91 [[nodiscard]] bool Bind();
92
93 [[nodiscard]] bool GenerateMipmap();
94
95 enum class AttachmentType {
96 kColor0,
97 kDepth,
99 };
100 [[nodiscard]] bool SetAsFramebufferAttachment(GLenum target,
101 AttachmentType attachment_type);
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 ///
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);
130
131 bool IsSliceInitialized(size_t slice) const;
132
133 //----------------------------------------------------------------------------
134 /// @brief Indicates that storage for `mip_level` of `slice` has been
135 /// allocated by a `glTexImage2D` call (or equivalent).
136 ///
137 /// GLES raises `GL_INVALID_OPERATION` when `glTexSubImage2D`
138 /// targets a level that has not been previously defined, so
139 /// every per-level upload must check this first and allocate
140 /// on demand.
141 ///
142 /// @param[in] slice The slice (cubemap face for cubemaps, otherwise
143 /// always 0).
144 /// @param[in] mip_level The mip level whose storage was allocated.
145 ///
146 void MarkSliceMipLevelInitialized(size_t slice, size_t mip_level);
147
148 bool IsSliceMipLevelInitialized(size_t slice, size_t mip_level) const;
149
150 //----------------------------------------------------------------------------
151 /// @brief Attach a sync fence to this texture that will be waited on
152 /// before encoding a rendering operation that references it.
153 ///
154 /// @param[in] fence A handle to a sync fence.
155 ///
156 void SetFence(HandleGLES fence);
157
158 /// Store the FBO object for recycling in the 2D renderer.
159 ///
160 /// The color0 texture used by the 2D renderer will use this texture
161 /// object to store the associated FBO the first time it is used.
162 void SetCachedFBO(HandleGLES fbo);
163
164 /// Retrieve the cached FBO object, or a dead handle if there is no object.
165 const HandleGLES& GetCachedFBO() const;
166
167 // Visible for testing.
168 std::optional<HandleGLES> GetSyncFence() const;
169
170 // visible for testing
171 Type ComputeTypeForBinding(GLenum target) const;
172
173 private:
174 std::shared_ptr<ReactorGLES> reactor_;
175 const Type type_;
176 HandleGLES handle_;
177 std::optional<HandleGLES> fence_ = std::nullopt;
178 // Tracks which `(slice, mip_level)` pairs have had their storage allocated
179 // by a `glTexImage2D` call. Allocation is performed lazily on first write
180 // to a level so the only-renders-then-mipmaps path (Impeller's snapshot
181 // pipeline) keeps its single base-level allocation, and per-level uploads
182 // only pay for the levels they actually touch.
183 //
184 // Sized for up to 6 cubemap faces × 16 mip levels (covers a 32k base
185 // dimension); requested levels above this are simply not tracked.
186 static constexpr size_t kMaxTrackedMipLevels = 16;
187 std::array<std::bitset<kMaxTrackedMipLevels>, 6> slice_mip_initialized_ = {};
188 const bool is_wrapped_;
189 const std::optional<GLuint> wrapped_fbo_;
190 HandleGLES cached_fbo_ = HandleGLES::DeadHandle();
191 bool is_valid_ = false;
192
193 TextureGLES(std::shared_ptr<ReactorGLES> reactor,
194 TextureDescriptor desc,
195 bool threadsafe,
196 std::optional<GLuint> fbo,
197 std::optional<HandleGLES> external_handle);
198
199 // |Texture|
200 void SetLabel(std::string_view label) override;
201
202 // |Texture|
203 void SetLabel(std::string_view label, std::string_view trailing) override;
204
205 // |Texture|
206 bool OnSetContents(const uint8_t* contents,
207 size_t length,
208 size_t slice) override;
209
210 // |Texture|
211 bool OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
212 size_t slice) override;
213
214 // |Texture|
215 ISize GetSize() const override;
216
217 // |Texture|
218 Scalar GetYCoordScale() const override;
219
220 void InitializeContentsIfNecessary();
221
222 TextureGLES(const TextureGLES&) = delete;
223
224 TextureGLES& operator=(const TextureGLES&) = delete;
225};
226
227} // namespace impeller
228
229#endif // FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_TEXTURE_GLES_H_
Represents a handle to an underlying OpenGL object. Unlike OpenGL object handles, these handles can b...
Definition handle_gles.h:42
static HandleGLES DeadHandle()
Creates a dead handle.
Definition handle_gles.h:49
void MarkSliceInitialized(size_t slice)
Indicates that a specific texture slice has been initialized.
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.
void MarkContentsInitialized()
Indicates that all texture storage has already been allocated and contents initialized.
const HandleGLES & GetCachedFBO() const
Retrieve the cached FBO object, or a dead handle if there is no object.
bool SetAsFramebufferAttachment(GLenum target, AttachmentType attachment_type)
std::optional< HandleGLES > GetSyncFence() const
bool IsSliceMipLevelInitialized(size_t slice, size_t mip_level) const
bool IsSliceInitialized(size_t slice) const
void MarkSliceMipLevelInitialized(size_t slice, size_t mip_level)
Indicates that storage for mip_level of slice has been allocated by a glTexImage2D call (or equivalen...
bool IsValid() const override
void SetFence(HandleGLES fence)
Attach a sync fence to this texture that will be waited on before encoding a rendering operation that...
void Leak()
Reset the internal texture state so that the reactor will not free the associated handle.
void SetCachedFBO(HandleGLES fbo)
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....
std::optional< GLuint > GetFBO() const
Type ComputeTypeForBinding(GLenum target) const
std::optional< GLuint > GetGLHandle() const
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...
uint32_t * target
size_t length
float Scalar
Definition scalar.h:19
ISize64 ISize
Definition size.h:162
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...