-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathSurfaceRegistry.h
More file actions
323 lines (279 loc) · 8.94 KB
/
SurfaceRegistry.h
File metadata and controls
323 lines (279 loc) · 8.94 KB
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
#pragma once
#include <memory>
#include <optional>
#include <shared_mutex>
#include <unordered_map>
#include <utility>
#include <vector>
#ifdef __APPLE__
#include <dispatch/dispatch.h>
#include <pthread.h>
#endif
#include "webgpu/webgpu_cpp.h"
#include "api/Canvas.h"
#ifdef __APPLE__
namespace dawn::native::metal {
void WaitForCommandsToBeScheduled(WGPUDevice device);
} // namespace dawn::native::metal
#endif
namespace rnwgpu {
struct NativeInfo {
void *nativeSurface;
int width;
int height;
};
struct Size {
int width;
int height;
};
class SurfaceInfo {
public:
SurfaceInfo(wgpu::Instance gpu, int width, int height)
: gpu(std::move(gpu)), width(width), height(height) {}
~SurfaceInfo() { surface = nullptr; }
struct PresentRequest {
wgpu::Device device;
wgpu::Surface surface;
int width;
int height;
std::weak_ptr<Canvas> canvas;
};
void reconfigure(int newWidth, int newHeight) {
std::unique_lock<std::shared_mutex> lock(_mutex);
config.width = newWidth;
config.height = newHeight;
_configure();
}
void configure(wgpu::SurfaceConfiguration &newConfig) {
std::unique_lock<std::shared_mutex> lock(_mutex);
config = newConfig;
config.width = width;
config.height = height;
config.presentMode = wgpu::PresentMode::Fifo;
_needsPresent = false;
_configure();
}
void unconfigure() {
std::unique_lock<std::shared_mutex> lock(_mutex);
if (surface) {
surface.Unconfigure();
} else {
texture = nullptr;
}
_needsPresent = false;
}
void *switchToOffscreen() {
std::unique_lock<std::shared_mutex> lock(_mutex);
// We only do this if the onscreen surface is configured.
auto isConfigured = config.device != nullptr;
if (isConfigured) {
wgpu::TextureDescriptor textureDesc;
textureDesc.usage = wgpu::TextureUsage::RenderAttachment |
wgpu::TextureUsage::CopySrc |
wgpu::TextureUsage::TextureBinding;
textureDesc.format = config.format;
textureDesc.size.width = config.width;
textureDesc.size.height = config.height;
texture = config.device.CreateTexture(&textureDesc);
}
surface = nullptr;
_needsPresent = false;
return nativeSurface;
}
void switchToOnscreen(void *newNativeSurface, wgpu::Surface newSurface) {
std::unique_lock<std::shared_mutex> lock(_mutex);
nativeSurface = newNativeSurface;
surface = std::move(newSurface);
// If we are comming from an offscreen context, we need to configure the new
// surface
if (texture != nullptr) {
config.usage = config.usage | wgpu::TextureUsage::CopyDst;
_configure();
// We flush the offscreen texture to the onscreen one
// TODO: there is a faster way to do this without validation?
wgpu::CommandEncoderDescriptor encoderDesc;
auto device = config.device;
wgpu::CommandEncoder encoder = device.CreateCommandEncoder(&encoderDesc);
wgpu::TexelCopyTextureInfo sourceTexture = {};
sourceTexture.texture = texture;
wgpu::TexelCopyTextureInfo destinationTexture = {};
wgpu::SurfaceTexture surfaceTexture;
surface.GetCurrentTexture(&surfaceTexture);
destinationTexture.texture = surfaceTexture.texture;
wgpu::Extent3D size = {sourceTexture.texture.GetWidth(),
sourceTexture.texture.GetHeight(),
sourceTexture.texture.GetDepthOrArrayLayers()};
encoder.CopyTextureToTexture(&sourceTexture, &destinationTexture, &size);
wgpu::CommandBuffer commands = encoder.Finish();
wgpu::Queue queue = device.GetQueue();
queue.Submit(1, &commands);
surface.Present();
texture = nullptr;
}
_needsPresent = false;
}
void resize(int newWidth, int newHeight) {
std::unique_lock<std::shared_mutex> lock(_mutex);
width = newWidth;
height = newHeight;
}
void setCanvas(std::weak_ptr<Canvas> canvas) {
std::unique_lock<std::shared_mutex> lock(_mutex);
_canvas = std::move(canvas);
}
wgpu::Texture getCurrentTexture() {
std::unique_lock<std::shared_mutex> lock(_mutex);
if (surface) {
wgpu::SurfaceTexture surfaceTexture;
surface.GetCurrentTexture(&surfaceTexture);
_needsPresent = true;
return surfaceTexture.texture;
} else {
_needsPresent = false;
return texture;
}
}
std::optional<PresentRequest> takePendingPresent() {
std::unique_lock<std::shared_mutex> lock(_mutex);
if (!_needsPresent || !surface) {
_needsPresent = false;
return std::nullopt;
}
PresentRequest request{.device = config.device,
.surface = surface,
.width = width,
.height = height,
.canvas = _canvas};
_needsPresent = false;
return request;
}
NativeInfo getNativeInfo() {
std::shared_lock<std::shared_mutex> lock(_mutex);
return {.nativeSurface = nativeSurface, .width = width, .height = height};
}
Size getSize() {
std::shared_lock<std::shared_mutex> lock(_mutex);
return {.width = width, .height = height};
}
wgpu::SurfaceConfiguration getConfig() {
std::shared_lock<std::shared_mutex> lock(_mutex);
return config;
}
wgpu::Device getDevice() {
std::shared_lock<std::shared_mutex> lock(_mutex);
return config.device;
}
private:
void _configure() {
if (surface) {
#ifdef __APPLE__
struct ConfigureSurfaceContext {
wgpu::Surface surface;
wgpu::SurfaceConfiguration config;
};
ConfigureSurfaceContext context{surface, config};
auto configure = [](void *ctx) {
auto *context =
static_cast<ConfigureSurfaceContext *>(ctx);
context->surface.Configure(&context->config);
};
if (pthread_main_np() != 0) {
configure(&context);
} else {
dispatch_sync_f(dispatch_get_main_queue(), &context, configure);
}
#else
surface.Configure(&config);
#endif
} else {
wgpu::TextureDescriptor textureDesc;
textureDesc.format = config.format;
textureDesc.size.width = config.width;
textureDesc.size.height = config.height;
textureDesc.usage = wgpu::TextureUsage::RenderAttachment |
wgpu::TextureUsage::CopySrc |
wgpu::TextureUsage::TextureBinding;
texture = config.device.CreateTexture(&textureDesc);
}
}
mutable std::shared_mutex _mutex;
std::weak_ptr<Canvas> _canvas;
void *nativeSurface = nullptr;
wgpu::Surface surface = nullptr;
wgpu::Texture texture = nullptr;
wgpu::Instance gpu;
wgpu::SurfaceConfiguration config;
int width;
int height;
bool _needsPresent = false;
};
class SurfaceRegistry {
public:
static SurfaceRegistry &getInstance() {
static SurfaceRegistry instance;
return instance;
}
SurfaceRegistry(const SurfaceRegistry &) = delete;
SurfaceRegistry &operator=(const SurfaceRegistry &) = delete;
std::shared_ptr<SurfaceInfo> getSurfaceInfo(int id) {
std::shared_lock<std::shared_mutex> lock(_mutex);
auto it = _registry.find(id);
if (it != _registry.end()) {
return it->second;
}
return nullptr;
}
void removeSurfaceInfo(int id) {
std::unique_lock<std::shared_mutex> lock(_mutex);
_registry.erase(id);
}
std::shared_ptr<SurfaceInfo> addSurfaceInfo(int id, wgpu::Instance gpu,
int width, int height) {
std::unique_lock<std::shared_mutex> lock(_mutex);
auto info = std::make_shared<SurfaceInfo>(gpu, width, height);
_registry[id] = info;
return info;
}
std::shared_ptr<SurfaceInfo>
getSurfaceInfoOrCreate(int id, wgpu::Instance gpu, int width, int height) {
std::unique_lock<std::shared_mutex> lock(_mutex);
auto it = _registry.find(id);
if (it != _registry.end()) {
return it->second;
}
auto info = std::make_shared<SurfaceInfo>(gpu, width, height);
_registry[id] = info;
return info;
}
void presentPendingSurfaces() {
std::vector<std::shared_ptr<SurfaceInfo>> infosCopy;
{
std::shared_lock<std::shared_mutex> lock(_mutex);
infosCopy.reserve(_registry.size());
for (auto &entry : _registry) {
infosCopy.push_back(entry.second);
}
}
for (auto &info : infosCopy) {
auto request = info->takePendingPresent();
if (!request.has_value()) {
continue;
}
auto presentRequest = std::move(request.value());
#ifdef __APPLE__
dawn::native::metal::WaitForCommandsToBeScheduled(
presentRequest.device.Get());
#endif
if (auto canvas = presentRequest.canvas.lock()) {
canvas->setClientWidth(presentRequest.width);
canvas->setClientHeight(presentRequest.height);
}
presentRequest.surface.Present();
}
}
private:
SurfaceRegistry() = default;
mutable std::shared_mutex _mutex;
std::unordered_map<int, std::shared_ptr<SurfaceInfo>> _registry;
};
} // namespace rnwgpu