Flutter Impeller
handle_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_HANDLE_GLES_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_HANDLE_GLES_H_
7 
8 #include <cstdint>
9 #include <optional>
10 #include <sstream>
11 #include <string>
12 #include <thread>
13 #include <type_traits>
14 
15 #include "flutter/fml/hash_combine.h"
17 
18 namespace impeller {
19 
20 enum class HandleType {
21  kUnknown,
22  kTexture,
23  kBuffer,
24  kProgram,
27  kFence,
28 };
29 
31 
32 // Returns whether this type of handle can be shared across OpenGL contexts.
34 
35 class ReactorGLES;
36 
37 //------------------------------------------------------------------------------
38 /// @brief Represents a handle to an underlying OpenGL object. Unlike
39 /// OpenGL object handles, these handles can be collected on any
40 /// thread as long as their destruction is scheduled in a reactor.
41 ///
42 class HandleGLES {
43  public:
44  //----------------------------------------------------------------------------
45  /// @brief Creates a dead handle.
46  ///
47  /// @return The handle.
48  ///
50  return HandleGLES{HandleType::kUnknown, std::nullopt};
51  }
52 
53  //----------------------------------------------------------------------------
54  /// @brief Determines if the handle is dead.
55  ///
56  /// @return True if dead, False otherwise.
57  ///
58  constexpr bool IsDead() const { return !name_.has_value(); }
59 
60  //----------------------------------------------------------------------------
61  /// @brief Get the hash value of this handle. Handles can be used as map
62  /// keys.
63  ///
64  struct Hash {
65  std::size_t operator()(const HandleGLES& handle) const {
66  return handle.GetHash();
67  }
68  };
69 
70  //----------------------------------------------------------------------------
71  /// @brief A comparer used to test the equality of two handles.
72  ///
73  struct Equal {
74  bool operator()(const HandleGLES& lhs, const HandleGLES& rhs) const {
75  return lhs.type_ == rhs.type_ && lhs.name_ == rhs.name_;
76  }
77  };
78 
79  HandleType GetType() const { return type_; }
80  const std::optional<UniqueID>& GetName() const { return name_; }
81  std::size_t GetHash() const { return hash_; }
82 
83  private:
85  std::optional<UniqueID> name_;
86  std::size_t hash_;
87  std::optional<uint64_t> untracked_id_;
88  std::optional<std::thread::id> owner_thread_;
89 
90  friend class ReactorGLES;
91 
92  HandleGLES(HandleType p_type, UniqueID p_name, std::thread::id p_owner_thread)
93  : type_(p_type),
94  name_(p_name),
95  hash_(fml::HashCombine(
96  static_cast<std::underlying_type_t<decltype(p_type)>>(p_type),
97  p_name)) {}
98 
99  HandleGLES(HandleType p_type, std::optional<UniqueID> p_name)
100  : type_(p_type),
101  name_(p_name),
102  hash_(fml::HashCombine(
103  static_cast<std::underlying_type_t<decltype(p_type)>>(p_type),
104  p_name)) {}
105 
106  static HandleGLES Create(HandleType type) {
107  HandleGLES handle{type, UniqueID{}};
108  if (!HandleTypeIsShareable(type)) {
109  handle.owner_thread_ = std::this_thread::get_id();
110  }
111  return handle;
112  }
113 };
114 
115 } // namespace impeller
116 
117 namespace std {
118 
119 inline std::ostream& operator<<(std::ostream& out,
120  const impeller::HandleGLES& handle) {
121  out << HandleTypeToString(handle.GetType()) << "(";
122  if (handle.IsDead()) {
123  out << "DEAD";
124  } else {
125  const std::optional<impeller::UniqueID>& name = handle.GetName();
126  if (name.has_value()) {
127  out << name.value().id;
128  } else {
129  out << "UNNAMED";
130  }
131  }
132  out << ")";
133  return out;
134 }
135 
136 } // namespace std
137 
138 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_GLES_HANDLE_GLES_H_
GLenum type
Represents a handle to an underlying OpenGL object. Unlike OpenGL object handles, these handles can b...
Definition: handle_gles.h:42
constexpr bool IsDead() const
Determines if the handle is dead.
Definition: handle_gles.h:58
const std::optional< UniqueID > & GetName() const
Definition: handle_gles.h:80
HandleType GetType() const
Definition: handle_gles.h:79
std::size_t GetHash() const
Definition: handle_gles.h:81
static HandleGLES DeadHandle()
Creates a dead handle.
Definition: handle_gles.h:49
The reactor attempts to make thread-safe usage of OpenGL ES easier to reason about.
Definition: reactor_gles.h:57
ScopedObject< Object > Create(CtorArgs &&... args)
Definition: object.h:161
bool HandleTypeIsShareable(HandleType type)
Definition: handle_gles.cc:31
std::string HandleTypeToString(HandleType type)
Definition: handle_gles.cc:11
Definition: comparable.h:95
std::ostream & operator<<(std::ostream &out, const impeller::Arc &a)
Definition: arc.h:141
A comparer used to test the equality of two handles.
Definition: handle_gles.h:73
bool operator()(const HandleGLES &lhs, const HandleGLES &rhs) const
Definition: handle_gles.h:74
Get the hash value of this handle. Handles can be used as map keys.
Definition: handle_gles.h:64
std::size_t operator()(const HandleGLES &handle) const
Definition: handle_gles.h:65