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