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"
12 #include "impeller/core/texture.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,
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, TextureDescriptor desc);
79 
80  // |Texture|
81  ~TextureGLES() override;
82 
83  // |Texture|
84  bool IsValid() const override;
85 
86  std::optional<GLuint> GetGLHandle() const;
87 
88  [[nodiscard]] bool Bind() const;
89 
90  [[nodiscard]] bool GenerateMipmap();
91 
92  enum class AttachmentType {
93  kColor0,
94  kDepth,
95  kStencil,
96  };
97  [[nodiscard]] bool SetAsFramebufferAttachment(
98  GLenum target,
99  AttachmentType attachment_type) const;
100 
101  Type GetType() const;
102 
103  bool IsWrapped() const;
104 
105  /// @brief Reset the internal texture state so that the reactor will not free
106  /// the associated handle.
107  void Leak();
108 
109  std::optional<GLuint> GetFBO() const;
110 
111  //----------------------------------------------------------------------------
112  /// @brief Indicates that all texture storage has already been allocated
113  /// and contents initialized.
114  ///
115  /// This is similar to calling `MarkSliceInitialized` with all
116  /// slices.
117  ///
118  /// @see MarkSliceInitialized.
119  ///
121 
122  //----------------------------------------------------------------------------
123  /// @brief Indicates that a specific texture slice has been initialized.
124  ///
125  /// @param[in] slice The slice to mark as being initialized.
126  ///
127  void MarkSliceInitialized(size_t slice) const;
128 
129  bool IsSliceInitialized(size_t slice) const;
130 
131  //----------------------------------------------------------------------------
132  /// @brief Attach a sync fence to this texture that will be waited on
133  /// before encoding a rendering operation that references it.
134  ///
135  /// @param[in] fence A handle to a sync fence.
136  ///
137  void SetFence(HandleGLES fence);
138 
139  /// Store the FBO object for recycling in the 2D renderer.
140  ///
141  /// The color0 texture used by the 2D renderer will use this texture
142  /// object to store the associated FBO the first time it is used.
143  void SetCachedFBO(HandleGLES fbo);
144 
145  /// Retrieve the cached FBO object, or a dead handle if there is no object.
146  const HandleGLES& GetCachedFBO() const;
147 
148  // Visible for testing.
149  std::optional<HandleGLES> GetSyncFence() const;
150 
151  // visible for testing
152  Type ComputeTypeForBinding(GLenum target) const;
153 
154  private:
155  std::shared_ptr<ReactorGLES> reactor_;
156  const Type type_;
157  HandleGLES handle_;
158  mutable std::optional<HandleGLES> fence_ = std::nullopt;
159  mutable std::bitset<6> slices_initialized_ = 0;
160  const bool is_wrapped_;
161  const std::optional<GLuint> wrapped_fbo_;
162  HandleGLES cached_fbo_ = HandleGLES::DeadHandle();
163  bool is_valid_ = false;
164 
165  TextureGLES(std::shared_ptr<ReactorGLES> reactor,
166  TextureDescriptor desc,
167  std::optional<GLuint> fbo,
168  std::optional<HandleGLES> external_handle);
169 
170  // |Texture|
171  void SetLabel(std::string_view label) override;
172 
173  // |Texture|
174  void SetLabel(std::string_view label, std::string_view trailing) override;
175 
176  // |Texture|
177  bool OnSetContents(const uint8_t* contents,
178  size_t length,
179  size_t slice) override;
180 
181  // |Texture|
182  bool OnSetContents(std::shared_ptr<const fml::Mapping> mapping,
183  size_t slice) override;
184 
185  // |Texture|
186  ISize GetSize() const override;
187 
188  // |Texture|
189  Scalar GetYCoordScale() const override;
190 
191  void InitializeContentsIfNecessary() const;
192 
193  TextureGLES(const TextureGLES&) = delete;
194 
195  TextureGLES& operator=(const TextureGLES&) = delete;
196 };
197 
198 } // namespace impeller
199 
200 #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:37
static HandleGLES DeadHandle()
Creates a dead handle.
Definition: handle_gles.h:44
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.
std::optional< HandleGLES > GetSyncFence() const
bool IsSliceInitialized(size_t slice) const
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)
bool SetAsFramebufferAttachment(GLenum target, AttachmentType attachment_type) const
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....
TextureGLES(std::shared_ptr< ReactorGLES > reactor, TextureDescriptor desc)
std::optional< GLuint > GetFBO() const
Type ComputeTypeForBinding(GLenum target) const
void MarkSliceInitialized(size_t slice) const
Indicates that a specific texture slice has been initialized.
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...
bool IsWrapped() const
float Scalar
Definition: scalar.h:19
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...