forked from RPCS3/rpcs3
-
Notifications
You must be signed in to change notification settings - Fork 4
/
overlays.h
329 lines (273 loc) · 6.99 KB
/
overlays.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#pragma once
#include "overlay_animation.h"
#include "overlay_controls.h"
#include "../../../Utilities/Thread.h"
#include "Emu/Memory/vm.h"
#include "Emu/IdManager.h"
#include "Utilities/Timer.h"
#include <list>
#include <thread>
// Utils
extern u64 get_system_time();
// Definition of user interface implementations
namespace rsx
{
namespace overlays
{
// Non-interactable UI element
struct overlay
{
u32 uid = UINT32_MAX;
u32 type_index = UINT32_MAX;
u16 virtual_width = 1280;
u16 virtual_height = 720;
u32 min_refresh_duration_us = 16600;
atomic_t<bool> visible = false;
virtual ~overlay() = default;
virtual void update() {}
virtual compiled_resource get_compiled() = 0;
void refresh();
};
// Interactable UI element
class user_interface : public overlay
{
public:
// Move this somewhere to avoid duplication
enum selection_code
{
new_save = -1,
canceled = -2,
error = -3
};
enum pad_button : u8
{
dpad_up = 0,
dpad_down,
dpad_left,
dpad_right,
select,
start,
triangle,
circle,
square,
cross,
L1,
R1,
pad_button_max_enum
};
protected:
Timer input_timer;
atomic_t<bool> exit = false;
atomic_t<u64> thread_bits = 0;
static thread_local u64 g_thread_bit;
u64 alloc_thread_bit();
std::function<void(s32 status)> on_close;
public:
s32 return_code = 0; // CELL_OK
public:
void update() override {}
compiled_resource get_compiled() override = 0;
virtual void on_button_pressed(pad_button /*button_press*/) {}
void close(bool use_callback, bool stop_pad_interception);
s32 run_input_loop();
};
class display_manager
{
private:
atomic_t<u32> m_uid_ctr = 0;
std::vector<std::shared_ptr<overlay>> m_iface_list;
std::vector<std::shared_ptr<overlay>> m_dirty_list;
shared_mutex m_list_mutex;
std::vector<u32> m_uids_to_remove;
std::vector<u32> m_type_ids_to_remove;
bool remove_type(u32 type_id)
{
bool found = false;
for (auto It = m_iface_list.begin(); It != m_iface_list.end();)
{
if (It->get()->type_index == type_id)
{
m_dirty_list.push_back(std::move(*It));
It = m_iface_list.erase(It);
found = true;
}
else
{
++It;
}
}
return found;
}
bool remove_uid(u32 uid)
{
for (auto It = m_iface_list.begin(); It != m_iface_list.end(); It++)
{
const auto e = It->get();
if (e->uid == uid)
{
m_dirty_list.push_back(std::move(*It));
m_iface_list.erase(It);
return true;
}
}
return false;
}
void cleanup_internal()
{
for (const auto &uid : m_uids_to_remove)
{
remove_uid(uid);
}
for (const auto &type_id : m_type_ids_to_remove)
{
remove_type(type_id);
}
m_uids_to_remove.clear();
m_type_ids_to_remove.clear();
}
public:
// Disable default construction to make it conditionally available in g_fxo
explicit display_manager(int) noexcept
{
}
// Adds an object to the internal list. Optionally removes other objects of the same type.
// Original handle loses ownership but a usable pointer is returned
template <typename T>
std::shared_ptr<T> add(std::shared_ptr<T>& entry, bool remove_existing = true)
{
std::lock_guard lock(m_list_mutex);
entry->uid = m_uid_ctr.fetch_add(1);
entry->type_index = id_manager::typeinfo::get_index<T>();
if (remove_existing)
{
for (auto It = m_iface_list.begin(); It != m_iface_list.end(); It++)
{
if (It->get()->type_index == entry->type_index)
{
// Replace
m_dirty_list.push_back(std::move(*It));
*It = std::move(entry);
return std::static_pointer_cast<T>(*It);
}
}
}
m_iface_list.push_back(std::move(entry));
return std::static_pointer_cast<T>(m_iface_list.back());
}
// Allocates object and adds to internal list. Returns pointer to created object
template <typename T, typename ...Args>
std::shared_ptr<T> create(Args&&... args)
{
auto object = std::make_shared<T>(std::forward<Args>(args)...);
return add(object);
}
// Removes item from list if it matches the uid
void remove(u32 uid)
{
if (m_list_mutex.try_lock())
{
remove_uid(uid);
m_list_mutex.unlock();
}
else
{
m_uids_to_remove.push_back(uid);
}
}
// Removes all objects of this type from the list
template <typename T>
void remove()
{
const auto type_id = id_manager::typeinfo::get_index<T>();
if (m_list_mutex.try_lock())
{
remove_type(type_id);
m_list_mutex.unlock();
}
else
{
m_type_ids_to_remove.push_back(type_id);
}
}
// True if any visible elements to draw exist
bool has_visible() const
{
return !m_iface_list.empty();
}
// True if any elements have been deleted but their resources may not have been cleaned up
bool has_dirty() const
{
return !m_dirty_list.empty();
}
// Returns current list for reading. Caller must ensure synchronization by first locking the list
const std::vector<std::shared_ptr<overlay>>& get_views() const
{
return m_iface_list;
}
// Returns current list of removed objects not yet deallocated for reading.
// Caller must ensure synchronization by first locking the list
const std::vector<std::shared_ptr<overlay>>& get_dirty() const
{
return m_dirty_list;
}
// Deallocate object. Object must first be removed via the remove() functions
void dispose(const std::vector<u32>& uids)
{
std::lock_guard lock(m_list_mutex);
if (!m_uids_to_remove.empty() || !m_type_ids_to_remove.empty())
{
cleanup_internal();
}
m_dirty_list.erase
(
std::remove_if(m_dirty_list.begin(), m_dirty_list.end(), [&uids](std::shared_ptr<overlay>& e)
{
return std::find(uids.begin(), uids.end(), e->uid) != uids.end();
}),
m_dirty_list.end()
);
}
// Returns pointer to the object matching the given uid
std::shared_ptr<overlay> get(u32 uid)
{
reader_lock lock(m_list_mutex);
for (const auto& iface : m_iface_list)
{
if (iface->uid == uid)
return iface;
}
return {};
}
// Returns pointer to the first object matching the given type
template <typename T>
std::shared_ptr<T> get()
{
reader_lock lock(m_list_mutex);
const auto type_id = id_manager::typeinfo::get_index<T>();
for (const auto& iface : m_iface_list)
{
if (iface->type_index == type_id)
{
return std::static_pointer_cast<T>(iface);
}
}
return {};
}
// Lock for read-only access (BasicLockable)
void lock()
{
m_list_mutex.lock_shared();
}
// Release read-only lock (BasicLockable). May perform internal cleanup before returning
void unlock()
{
m_list_mutex.unlock_shared();
if (!m_uids_to_remove.empty() || !m_type_ids_to_remove.empty())
{
std::lock_guard lock(m_list_mutex);
cleanup_internal();
}
}
};
}
}