@deepseek-ai/libreoffice-kit 0.1.4 → 0.2.0-alpha5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +4 -0
- package/README.md +49 -9
- package/README.zh.md +50 -10
- package/lib/cli.js +155 -52
- package/lib/editor.js +2061 -0
- package/lib/index.js +155 -52
- package/lib/types/editor-protocol.d.ts +29 -0
- package/lib/types/editor-transport.d.ts +36 -0
- package/lib/types/editor-types.d.ts +119 -0
- package/lib/types/editor.d.ts +105 -0
- package/lib/types/engine.d.ts +4 -2
- package/lib/types/fonts.d.ts +4 -8
- package/lib/types/native-image-renderer.d.ts +0 -1
- package/lib/types/native-process.d.ts +1 -0
- package/lib/types/native.d.ts +31 -2
- package/lib/types/rendering.d.ts +2 -0
- package/lib/types/runtime.d.ts +2 -0
- package/lib/types/sheet-geometry.d.ts +1 -1
- package/lib/types/worker.d.ts +2 -2
- package/lib/worker.js +51 -32
- package/native-render/CMakeLists.txt +40 -0
- package/native-render/LICENSE +21 -0
- package/native-render/README.md +83 -0
- package/native-render/README.zh.md +83 -0
- package/native-render/include/dsh_render.h +101 -0
- package/native-render/include/dsh_render_engine.h +66 -0
- package/native-render/include/dsh_render_node.h +27 -0
- package/native-render/include/dsh_webgpu.h +52 -0
- package/native-render/index.d.ts +145 -0
- package/native-render/index.mjs +71 -0
- package/native-render/lib/browser/display.d.ts +46 -0
- package/native-render/lib/browser/display.js +128 -0
- package/native-render/lib/browser/index.d.ts +4 -0
- package/native-render/lib/browser/index.js +3 -0
- package/native-render/lib/browser/painters.d.ts +9 -0
- package/native-render/lib/browser/painters.js +180 -0
- package/native-render/lib/browser/types.d.ts +51 -0
- package/native-render/lib/browser/types.js +1 -0
- package/native-render/lib/electron/adapter.d.ts +9 -0
- package/native-render/lib/electron/adapter.js +118 -0
- package/native-render/lib/electron/index.d.ts +3 -0
- package/native-render/lib/electron/index.js +3 -0
- package/native-render/lib/electron/preload.d.ts +13 -0
- package/native-render/lib/electron/preload.js +47 -0
- package/native-render/lib/electron/transfer.d.ts +15 -0
- package/native-render/lib/electron/transfer.js +36 -0
- package/native-render/prebuilds/darwin-arm64/libdsh_render_sdk.a +0 -0
- package/native-render/prebuilds/darwin-arm64/manifest.json +34 -0
- package/native-render/prebuilds/darwin-arm64/render.node +0 -0
- package/native-render/prebuilds/darwin-x64/libdsh_render_sdk.a +0 -0
- package/native-render/prebuilds/darwin-x64/manifest.json +34 -0
- package/native-render/prebuilds/darwin-x64/render.node +0 -0
- package/native-render/prebuilds/win32-arm64/dsh_render_sdk.lib +0 -0
- package/native-render/prebuilds/win32-arm64/manifest.json +34 -0
- package/native-render/prebuilds/win32-arm64/render.node +0 -0
- package/native-render/prebuilds/win32-x64/dsh_render_sdk.lib +0 -0
- package/native-render/prebuilds/win32-x64/manifest.json +34 -0
- package/native-render/prebuilds/win32-x64/render.node +0 -0
- package/native-render/receipt.json +23 -0
- package/native-render/scripts/build-webgpu.mjs +197 -0
- package/native-render/scripts/build.mjs +43 -0
- package/native-render/scripts/metadata.mjs +47 -0
- package/native-render/scripts/source-identity.mjs +15 -0
- package/native-render/scripts/verify-prebuilds.mjs +14 -0
- package/native-render/sdk.json +45 -0
- package/native-render/src/node-producer.cc +595 -0
- package/native-render/src/producer-internal.h +8 -0
- package/native-render/src/producer.cc +306 -0
- package/native-render/src/receiver.cc +1050 -0
- package/native-render/src/relay-transport.inc +293 -0
- package/native-render/src/transport.cc +405 -0
- package/native-render/src/transport.h +80 -0
- package/native-render/src/webgpu.cc +214 -0
- package/native-render/src/win-delay-load.LICENSE +24 -0
- package/native-render/src/win-delay-load.cc +43 -0
- package/native-render/verify.d.mts +4 -0
- package/native-render/verify.mjs +101 -0
- package/native-render/webgpu-dependencies.json +51 -0
- package/package.json +97 -11
|
@@ -0,0 +1,1050 @@
|
|
|
1
|
+
#include "transport.h"
|
|
2
|
+
#include <node_api.h>
|
|
3
|
+
#include <uv.h>
|
|
4
|
+
#include <condition_variable>
|
|
5
|
+
#include <cstring>
|
|
6
|
+
#include <map>
|
|
7
|
+
#include <deque>
|
|
8
|
+
#include <thread>
|
|
9
|
+
#include <cmath>
|
|
10
|
+
#if defined(__APPLE__)
|
|
11
|
+
#include <stdlib.h>
|
|
12
|
+
#else
|
|
13
|
+
#define NOMINMAX
|
|
14
|
+
#include <windows.h>
|
|
15
|
+
#include <bcrypt.h>
|
|
16
|
+
#endif
|
|
17
|
+
using namespace dsh_render;
|
|
18
|
+
void register_node_producer(napi_env env, napi_value exports);
|
|
19
|
+
namespace {
|
|
20
|
+
enum class PresentationPath { iosurface, shared_texture, gpu_copy, cpu_relay };
|
|
21
|
+
PresentationPath resolve_presentation_path(const std::string& producer, const std::string& display, bool keyed_mutex = false) {
|
|
22
|
+
#if defined(_WIN32)
|
|
23
|
+
if (producer == display && keyed_mutex) return PresentationPath::shared_texture;
|
|
24
|
+
return producer == display ? PresentationPath::gpu_copy : PresentationPath::cpu_relay;
|
|
25
|
+
#else
|
|
26
|
+
(void)producer; (void)display; (void)keyed_mutex;
|
|
27
|
+
return PresentationPath::iosurface;
|
|
28
|
+
#endif
|
|
29
|
+
}
|
|
30
|
+
uint32_t allocation_bytes_factor(PresentationPath path) {
|
|
31
|
+
switch (path) {
|
|
32
|
+
case PresentationPath::iosurface: return 1;
|
|
33
|
+
case PresentationPath::shared_texture: return 1;
|
|
34
|
+
case PresentationPath::gpu_copy: return 2;
|
|
35
|
+
case PresentationPath::cpu_relay: return 1;
|
|
36
|
+
}
|
|
37
|
+
throw std::logic_error("Unknown native presentation path");
|
|
38
|
+
}
|
|
39
|
+
uint64_t source_byte_budget(PresentationPath path, uint32_t frames, uint64_t bytes) {
|
|
40
|
+
return path == PresentationPath::cpu_relay ? bytes / (2 * frames + 2) * frames : bytes / allocation_bytes_factor(path);
|
|
41
|
+
}
|
|
42
|
+
struct FrameLedger;
|
|
43
|
+
struct Event {
|
|
44
|
+
std::string type, text, adapter, presentation_adapter;
|
|
45
|
+
Packet packet; uintptr_t resource = 0; uint32_t pid = 0; uint64_t delivery = 0;
|
|
46
|
+
std::shared_ptr<FrameLedger> ledger;
|
|
47
|
+
bool delivered = false, source_keyed_mutex = false;
|
|
48
|
+
std::chrono::steady_clock::time_point deadline;
|
|
49
|
+
~Event();
|
|
50
|
+
};
|
|
51
|
+
struct GrantWake {
|
|
52
|
+
std::mutex mutex;
|
|
53
|
+
std::condition_variable ready;
|
|
54
|
+
std::deque<Packet> packets;
|
|
55
|
+
bool grants_pending = false, closed = false;
|
|
56
|
+
void notify() {
|
|
57
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
58
|
+
if (closed) return;
|
|
59
|
+
grants_pending = true; ready.notify_one();
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
struct Reservation {
|
|
63
|
+
uint64_t id, bytes;
|
|
64
|
+
std::atomic<bool> granted{false};
|
|
65
|
+
bool sent = false, cancelled = false;
|
|
66
|
+
std::weak_ptr<GrantWake> wake;
|
|
67
|
+
};
|
|
68
|
+
struct ReservationGroup {
|
|
69
|
+
std::vector<std::weak_ptr<Reservation>> members;
|
|
70
|
+
};
|
|
71
|
+
struct Budget {
|
|
72
|
+
uint64_t limit, used = 0;
|
|
73
|
+
std::mutex mutex;
|
|
74
|
+
std::deque<ReservationGroup> waiting;
|
|
75
|
+
explicit Budget(uint64_t bytes) : limit(bytes) {}
|
|
76
|
+
void advance() {
|
|
77
|
+
while (!waiting.empty()) {
|
|
78
|
+
auto& group = waiting.front();
|
|
79
|
+
uint64_t bytes = 0;
|
|
80
|
+
for (auto& member : group.members) if (auto next = member.lock()) if (!next->cancelled) bytes += next->bytes;
|
|
81
|
+
if (!bytes) { waiting.pop_front(); continue; }
|
|
82
|
+
if (bytes > limit - used) break;
|
|
83
|
+
for (auto& member : group.members) if (auto next = member.lock()) {
|
|
84
|
+
if (next->cancelled) continue;
|
|
85
|
+
used += next->bytes; next->granted = true;
|
|
86
|
+
if (auto wake = next->wake.lock()) wake->notify();
|
|
87
|
+
}
|
|
88
|
+
waiting.pop_front();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
std::shared_ptr<Reservation> request(uint64_t id, uint64_t bytes, const std::shared_ptr<GrantWake>& wake) {
|
|
92
|
+
auto result = std::make_shared<Reservation>(); result->id = id; result->bytes = bytes; result->wake = wake;
|
|
93
|
+
request_group({result}); return result;
|
|
94
|
+
}
|
|
95
|
+
void request_group(const std::vector<std::shared_ptr<Reservation>>& members) {
|
|
96
|
+
ReservationGroup group;
|
|
97
|
+
for (const auto& member : members) group.members.push_back(member);
|
|
98
|
+
std::lock_guard<std::mutex> lock(mutex); waiting.push_back(std::move(group)); advance();
|
|
99
|
+
}
|
|
100
|
+
void release(const std::shared_ptr<Reservation>& reservation) {
|
|
101
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
102
|
+
if (reservation->cancelled) return;
|
|
103
|
+
reservation->cancelled = true;
|
|
104
|
+
if (reservation->granted) used -= reservation->bytes;
|
|
105
|
+
advance();
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
#if defined(_WIN32)
|
|
109
|
+
template<class Texture> struct RelayStorage {
|
|
110
|
+
std::shared_ptr<Texture> texture;
|
|
111
|
+
std::shared_ptr<Budget> budget;
|
|
112
|
+
std::vector<std::shared_ptr<Reservation>> reservations;
|
|
113
|
+
~RelayStorage() { texture.reset(); for (auto& reservation : reservations) budget->release(reservation); }
|
|
114
|
+
};
|
|
115
|
+
using RelayTarget = RelayStorage<RelayTexture>;
|
|
116
|
+
using RelayStaging = RelayStorage<RelayReadback>;
|
|
117
|
+
#endif
|
|
118
|
+
struct HeldFrame {
|
|
119
|
+
uintptr_t resource; uint64_t bytes, allocation, source_id;
|
|
120
|
+
#if defined(_WIN32)
|
|
121
|
+
std::shared_ptr<RelayTexture> relay;
|
|
122
|
+
std::shared_ptr<RelayTarget> target;
|
|
123
|
+
#endif
|
|
124
|
+
bool delivered = false;
|
|
125
|
+
};
|
|
126
|
+
struct FrameLedger {
|
|
127
|
+
std::mutex mutex;
|
|
128
|
+
std::map<uint64_t, HeldFrame> frames;
|
|
129
|
+
std::map<uint64_t, std::shared_ptr<Reservation>> allocations;
|
|
130
|
+
std::shared_ptr<Budget> budget;
|
|
131
|
+
uint64_t bytes = 0;
|
|
132
|
+
std::atomic<bool> connected{false};
|
|
133
|
+
std::atomic<bool> accepting_frames{true};
|
|
134
|
+
~FrameLedger() {
|
|
135
|
+
for (auto& frame : frames) if (frame.second.resource) release_resource(frame.second.resource);
|
|
136
|
+
frames.clear();
|
|
137
|
+
for (auto& allocation : allocations) budget->release(allocation.second);
|
|
138
|
+
}
|
|
139
|
+
void retire_idle() {
|
|
140
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
141
|
+
for (auto allocation = allocations.begin(); allocation != allocations.end();) {
|
|
142
|
+
bool used = false;
|
|
143
|
+
for (auto& frame : frames) if (frame.second.resource && frame.second.allocation == allocation->first) { used = true; break; }
|
|
144
|
+
if (used) { ++allocation; continue; }
|
|
145
|
+
budget->release(allocation->second); allocation = allocations.erase(allocation);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
bool release(uint64_t id, bool delivered_only = false, uint64_t* source_id = nullptr) {
|
|
149
|
+
uintptr_t resource = 0;
|
|
150
|
+
bool released = false;
|
|
151
|
+
{
|
|
152
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
153
|
+
auto found = frames.find(id);
|
|
154
|
+
if (found != frames.end() && (!delivered_only || found->second.delivered)) {
|
|
155
|
+
resource = found->second.resource;
|
|
156
|
+
if (resource) {
|
|
157
|
+
bytes -= found->second.bytes;
|
|
158
|
+
if (source_id) *source_id = found->second.source_id;
|
|
159
|
+
}
|
|
160
|
+
frames.erase(found); released = true;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (!released) return false;
|
|
164
|
+
if (resource) release_resource(resource);
|
|
165
|
+
if (!connected) retire_idle();
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
uint64_t release_source(uint64_t delivery) {
|
|
169
|
+
uintptr_t resource; uint64_t source;
|
|
170
|
+
{
|
|
171
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
172
|
+
auto& frame = frames.at(delivery); resource = frame.resource; source = frame.source_id;
|
|
173
|
+
frame.resource = 0; bytes -= frame.bytes;
|
|
174
|
+
}
|
|
175
|
+
release_resource(resource); return source;
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
Event::~Event() {
|
|
179
|
+
// A frame discarded before its callback has no JavaScript owner to return its credit.
|
|
180
|
+
if (ledger && !delivered) ledger->release(delivery);
|
|
181
|
+
}
|
|
182
|
+
struct Dispatcher { std::atomic<bool> failed{false}; };
|
|
183
|
+
struct GpuJob {
|
|
184
|
+
std::unique_ptr<Event> event;
|
|
185
|
+
uint64_t allocation = 0;
|
|
186
|
+
std::shared_ptr<Reservation> retirement;
|
|
187
|
+
#if defined(_WIN32)
|
|
188
|
+
std::shared_ptr<RelayTexture> texture;
|
|
189
|
+
std::shared_ptr<RelayStaging> staging;
|
|
190
|
+
std::shared_ptr<RelayTarget> target;
|
|
191
|
+
#endif
|
|
192
|
+
bool ready = false;
|
|
193
|
+
};
|
|
194
|
+
struct CloseWork { napi_deferred deferred = nullptr; napi_ref keep = nullptr; };
|
|
195
|
+
struct Receiver {
|
|
196
|
+
napi_env env = nullptr;
|
|
197
|
+
napi_async_cleanup_hook_handle cleanup = nullptr;
|
|
198
|
+
uv_async_t done{};
|
|
199
|
+
std::atomic<bool> ended{false};
|
|
200
|
+
bool environment_closing = false, closing_handle = false;
|
|
201
|
+
bool main_released = false, worker_released = false, dispatcher_closed = false;
|
|
202
|
+
std::mutex dispatch_mutex;
|
|
203
|
+
std::unique_ptr<CloseWork> close_work;
|
|
204
|
+
std::unique_ptr<Channel> channel;
|
|
205
|
+
Endpoint endpoint;
|
|
206
|
+
uint32_t max_frames;
|
|
207
|
+
uint64_t max_bytes;
|
|
208
|
+
uint64_t next_delivery = 1;
|
|
209
|
+
PresentationPath presentation_path = PresentationPath::iosurface;
|
|
210
|
+
uint32_t readback_timeout = 0, allocation_factor = allocation_bytes_factor(presentation_path);
|
|
211
|
+
std::string display_adapter, producer_adapter;
|
|
212
|
+
bool latest = false;
|
|
213
|
+
#if defined(_WIN32)
|
|
214
|
+
std::shared_ptr<RelayDevice> relay_device;
|
|
215
|
+
std::map<uint64_t, std::shared_ptr<RelayTexture>> relay_textures;
|
|
216
|
+
std::vector<std::shared_ptr<RelayTarget>> relay_targets;
|
|
217
|
+
std::vector<std::shared_ptr<RelayStaging>> relay_staging;
|
|
218
|
+
uint64_t pool_bytes = 0;
|
|
219
|
+
HANDLE jobs_event = nullptr, retry_timer = nullptr;
|
|
220
|
+
bool wait_failed = false;
|
|
221
|
+
#endif
|
|
222
|
+
std::atomic<uint32_t> expected_pid{0};
|
|
223
|
+
std::atomic<bool> stopped{false};
|
|
224
|
+
std::condition_variable bound;
|
|
225
|
+
std::shared_ptr<FrameLedger> ledger = std::make_shared<FrameLedger>();
|
|
226
|
+
napi_threadsafe_function callback = nullptr;
|
|
227
|
+
std::shared_ptr<Dispatcher> dispatcher = std::make_shared<Dispatcher>();
|
|
228
|
+
std::thread worker, writer, copier;
|
|
229
|
+
std::mutex jobs_mutex;
|
|
230
|
+
std::condition_variable jobs_ready;
|
|
231
|
+
std::deque<GpuJob> jobs;
|
|
232
|
+
bool input_done = false;
|
|
233
|
+
std::shared_ptr<GrantWake> output = std::make_shared<GrantWake>();
|
|
234
|
+
bool closing = false;
|
|
235
|
+
~Receiver() {
|
|
236
|
+
#if defined(_WIN32)
|
|
237
|
+
if (jobs_event) CloseHandle(jobs_event);
|
|
238
|
+
if (retry_timer) CloseHandle(retry_timer);
|
|
239
|
+
#endif
|
|
240
|
+
}
|
|
241
|
+
void notify_jobs() {
|
|
242
|
+
jobs_ready.notify_all();
|
|
243
|
+
#if defined(_WIN32)
|
|
244
|
+
if (jobs_event) SetEvent(jobs_event);
|
|
245
|
+
#endif
|
|
246
|
+
}
|
|
247
|
+
void stop() {
|
|
248
|
+
{
|
|
249
|
+
std::lock_guard<std::mutex> lock(output->mutex);
|
|
250
|
+
if (stopped) return;
|
|
251
|
+
ledger->accepting_frames = false;
|
|
252
|
+
stopped = true;
|
|
253
|
+
output->closed = true;
|
|
254
|
+
}
|
|
255
|
+
bound.notify_all(); output->ready.notify_all(); notify_jobs();
|
|
256
|
+
if (channel) channel->interrupt();
|
|
257
|
+
}
|
|
258
|
+
bool enqueue(const Packet& packet) {
|
|
259
|
+
std::lock_guard<std::mutex> lock(output->mutex);
|
|
260
|
+
if (stopped || !ledger->connected || output->packets.size() >= 256) return false;
|
|
261
|
+
output->packets.push_back(packet); output->ready.notify_one(); return true;
|
|
262
|
+
}
|
|
263
|
+
void write_loop() {
|
|
264
|
+
while (!stopped) {
|
|
265
|
+
Packet packet; bool have_packet = false, have_grants = false;
|
|
266
|
+
{
|
|
267
|
+
std::unique_lock<std::mutex> lock(output->mutex);
|
|
268
|
+
output->ready.wait(lock, [&] { return stopped || output->grants_pending || !output->packets.empty(); });
|
|
269
|
+
if (stopped) break;
|
|
270
|
+
have_grants = output->grants_pending; output->grants_pending = false;
|
|
271
|
+
if (!output->packets.empty()) { packet = output->packets.front(); output->packets.pop_front(); have_packet = true; }
|
|
272
|
+
}
|
|
273
|
+
if (have_grants) grants();
|
|
274
|
+
if (have_packet && !stopped && !channel->send(packet)) { stop(); break; }
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
bool emit(Event* event) {
|
|
278
|
+
{
|
|
279
|
+
std::lock_guard<std::mutex> lock(dispatch_mutex);
|
|
280
|
+
if (dispatcher_closed || worker_released) { delete event; return false; }
|
|
281
|
+
const auto result = napi_call_threadsafe_function(callback, event, napi_tsfn_nonblocking);
|
|
282
|
+
if (result == napi_ok) return true;
|
|
283
|
+
// napi_closing relinquishes the native thread's TSFN reference.
|
|
284
|
+
if (result != napi_closing) napi_release_threadsafe_function(callback, napi_tsfn_release);
|
|
285
|
+
worker_released = true; dispatcher_closed = true;
|
|
286
|
+
}
|
|
287
|
+
delete event; stop(); return false;
|
|
288
|
+
}
|
|
289
|
+
void close_dispatcher() {
|
|
290
|
+
std::lock_guard<std::mutex> lock(dispatch_mutex);
|
|
291
|
+
dispatcher_closed = true;
|
|
292
|
+
if (!worker_released) { worker_released = true; napi_release_threadsafe_function(callback, napi_tsfn_release); }
|
|
293
|
+
if (!main_released) { main_released = true; napi_release_threadsafe_function(callback, napi_tsfn_abort); }
|
|
294
|
+
}
|
|
295
|
+
void fail(const char* text) { auto event = new Event; event->type = "error"; event->text = text; emit(event); }
|
|
296
|
+
void grants() {
|
|
297
|
+
std::lock_guard<std::mutex> lock(ledger->mutex);
|
|
298
|
+
for (auto& allocation : ledger->allocations) {
|
|
299
|
+
if (!allocation.second->granted || allocation.second->sent) continue;
|
|
300
|
+
Packet packet; packet.kind = allocated; packet.allocation_id = allocation.first;
|
|
301
|
+
if (!channel->send(packet)) { stop(); return; }
|
|
302
|
+
allocation.second->sent = true;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
#if defined(_WIN32)
|
|
306
|
+
std::shared_ptr<Reservation> reserve_relay(uint64_t id, uint64_t bytes) {
|
|
307
|
+
auto source = std::make_shared<Reservation>(); source->id = id; source->bytes = bytes; source->wake = output;
|
|
308
|
+
std::vector<std::shared_ptr<Reservation>> group{source};
|
|
309
|
+
if (relay_targets.empty()) {
|
|
310
|
+
for (uint32_t index = 0; index < max_frames; ++index) {
|
|
311
|
+
auto slot = std::make_shared<RelayTarget>(); slot->budget = ledger->budget; relay_targets.push_back(std::move(slot));
|
|
312
|
+
}
|
|
313
|
+
for (uint32_t index = 0; index < 2; ++index) {
|
|
314
|
+
auto slot = std::make_shared<RelayStaging>(); slot->budget = ledger->budget; relay_staging.push_back(std::move(slot));
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (bytes > pool_bytes) {
|
|
318
|
+
const auto reserve = [&](auto& slots) {
|
|
319
|
+
for (auto& slot : slots) {
|
|
320
|
+
auto reservation = std::make_shared<Reservation>(); reservation->id = 0; reservation->bytes = bytes - pool_bytes;
|
|
321
|
+
slot->reservations.push_back(reservation); group.push_back(std::move(reservation));
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
reserve(relay_targets); reserve(relay_staging); pool_bytes = bytes;
|
|
325
|
+
}
|
|
326
|
+
ledger->budget->request_group(group); return source;
|
|
327
|
+
}
|
|
328
|
+
#endif
|
|
329
|
+
bool post(GpuJob&& job) {
|
|
330
|
+
{
|
|
331
|
+
std::lock_guard<std::mutex> lock(jobs_mutex);
|
|
332
|
+
if (stopped || jobs.size() >= 256) return false;
|
|
333
|
+
try { jobs.push_back(std::move(job)); }
|
|
334
|
+
catch (const std::bad_alloc&) { return false; }
|
|
335
|
+
}
|
|
336
|
+
notify_jobs(); return true;
|
|
337
|
+
}
|
|
338
|
+
void prepare_frame(GpuJob& job) {
|
|
339
|
+
auto& event = *job.event;
|
|
340
|
+
std::string diagnostic, adapter;
|
|
341
|
+
if (stopped) return;
|
|
342
|
+
if (std::chrono::steady_clock::now() >= event.deadline) throw std::runtime_error("Native frame expired in GPU queue");
|
|
343
|
+
const auto& packet = event.packet;
|
|
344
|
+
if (!resource_dimensions(event.resource, packet.width, packet.height, packet.stride, diagnostic, adapter, &event.source_keyed_mutex)) throw std::runtime_error(diagnostic);
|
|
345
|
+
if (!producer_adapter.empty() && producer_adapter != adapter) throw std::runtime_error("Native texture adapter differs from producer handshake");
|
|
346
|
+
event.adapter = adapter;
|
|
347
|
+
#if defined(_WIN32)
|
|
348
|
+
switch (presentation_path) {
|
|
349
|
+
case PresentationPath::shared_texture:
|
|
350
|
+
if (!event.source_keyed_mutex) throw std::runtime_error("Producer declared keyed mutex support but supplied a legacy texture");
|
|
351
|
+
if (!resource_ready(event.resource, event.deadline, diagnostic)) throw std::runtime_error(diagnostic);
|
|
352
|
+
job.ready = true;
|
|
353
|
+
break;
|
|
354
|
+
case PresentationPath::gpu_copy: {
|
|
355
|
+
if (event.source_keyed_mutex) {
|
|
356
|
+
if (!resource_ready(event.resource, event.deadline, diagnostic)) throw std::runtime_error(diagnostic);
|
|
357
|
+
job.ready = true;
|
|
358
|
+
break;
|
|
359
|
+
}
|
|
360
|
+
// The pool owns the slot before submission, including failed fence notifications.
|
|
361
|
+
auto& slot = relay_textures[packet.allocation_id];
|
|
362
|
+
const auto submitted = relay_begin(event.resource, packet.width, packet.height, display_adapter, event.deadline, stopped, relay_device, slot, diagnostic);
|
|
363
|
+
job.texture = slot;
|
|
364
|
+
if (!submitted) throw std::runtime_error(diagnostic);
|
|
365
|
+
{
|
|
366
|
+
std::lock_guard<std::mutex> lock(ledger->mutex);
|
|
367
|
+
ledger->frames.at(event.delivery).relay = slot;
|
|
368
|
+
}
|
|
369
|
+
event.resource = relay_handle(slot);
|
|
370
|
+
break;
|
|
371
|
+
}
|
|
372
|
+
case PresentationPath::cpu_relay: throw std::logic_error("Relay frames require independent source preparation");
|
|
373
|
+
case PresentationPath::iosurface: break;
|
|
374
|
+
}
|
|
375
|
+
event.presentation_adapter = display_adapter;
|
|
376
|
+
#endif
|
|
377
|
+
}
|
|
378
|
+
void deliver_frame(std::unique_ptr<Event> event) {
|
|
379
|
+
if (stopped) return;
|
|
380
|
+
if (std::chrono::steady_clock::now() >= event->deadline) throw std::runtime_error("Native frame expired before delivery");
|
|
381
|
+
emit(event.release());
|
|
382
|
+
}
|
|
383
|
+
void process_frame(std::unique_ptr<Event> event) {
|
|
384
|
+
GpuJob job; job.event = std::move(event);
|
|
385
|
+
try { prepare_frame(job); deliver_frame(std::move(job.event)); }
|
|
386
|
+
catch (const std::exception& exception) { fail(exception.what()); stop(); }
|
|
387
|
+
}
|
|
388
|
+
#if defined(_WIN32)
|
|
389
|
+
bool gpu_idle(std::string& diagnostic) {
|
|
390
|
+
bool idle = true;
|
|
391
|
+
for (auto& slot : relay_textures) if (!relay_gpu_idle(slot.second, diagnostic)) idle = false;
|
|
392
|
+
return idle;
|
|
393
|
+
}
|
|
394
|
+
void wait_gpu(const std::deque<GpuJob>& active, bool retiring) {
|
|
395
|
+
if (wait_failed) { std::this_thread::sleep_for(std::chrono::milliseconds(5)); return; }
|
|
396
|
+
std::vector<HANDLE> waits;
|
|
397
|
+
if (!retiring) waits.push_back(jobs_event);
|
|
398
|
+
int64_t wait_ns = retiring ? 5000000 : INT64_MAX;
|
|
399
|
+
const auto add_event = [&](uintptr_t raw) {
|
|
400
|
+
if (raw && waits.size() < MAXIMUM_WAIT_OBJECTS - 1) waits.push_back(reinterpret_cast<HANDLE>(raw));
|
|
401
|
+
else wait_ns = std::min<int64_t>(wait_ns, 1000000);
|
|
402
|
+
};
|
|
403
|
+
if (retiring) {
|
|
404
|
+
for (auto& slot : relay_textures) add_event(relay_completion_event(slot.second));
|
|
405
|
+
} else for (const auto& job : active) {
|
|
406
|
+
if (job.ready) continue;
|
|
407
|
+
wait_ns = std::min<int64_t>(wait_ns, std::chrono::duration_cast<std::chrono::nanoseconds>(job.event->deadline - std::chrono::steady_clock::now()).count());
|
|
408
|
+
add_event(relay_completion_event(job.texture));
|
|
409
|
+
}
|
|
410
|
+
if (wait_ns != INT64_MAX) {
|
|
411
|
+
LARGE_INTEGER due; due.QuadPart = -std::max<int64_t>(1, wait_ns / 100);
|
|
412
|
+
if (!SetWaitableTimer(retry_timer, &due, 0, nullptr, nullptr, FALSE)) {
|
|
413
|
+
wait_failed = true; fail("Native GPU retry timer failed; retaining unfinished GPU resources"); stop(); return;
|
|
414
|
+
}
|
|
415
|
+
waits.push_back(retry_timer);
|
|
416
|
+
}
|
|
417
|
+
if (WaitForMultipleObjects(static_cast<DWORD>(waits.size()), waits.data(), FALSE, INFINITE) == WAIT_FAILED) {
|
|
418
|
+
wait_failed = true; fail("Native GPU worker wait failed; retaining unfinished GPU resources"); stop();
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
bool advance(GpuJob& job) {
|
|
422
|
+
if (job.ready) return true;
|
|
423
|
+
std::string diagnostic;
|
|
424
|
+
const auto ready = relay_advance(job.texture, stopped, job.event->deadline, diagnostic);
|
|
425
|
+
if (ready < 0) throw std::runtime_error(diagnostic);
|
|
426
|
+
job.ready = ready == 1; return job.ready;
|
|
427
|
+
}
|
|
428
|
+
void deliver_ready(std::deque<GpuJob>& active) {
|
|
429
|
+
while (!stopped && !active.empty() && advance(active.front())) {
|
|
430
|
+
deliver_frame(std::move(active.front().event)); active.pop_front();
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
template<class Slot> std::shared_ptr<Slot> available_slot(const std::vector<std::shared_ptr<Slot>>& slots) {
|
|
434
|
+
for (auto& slot : slots) if (slot.use_count() == 1) return slot;
|
|
435
|
+
return {};
|
|
436
|
+
}
|
|
437
|
+
bool relay_idle(std::string& diagnostic) {
|
|
438
|
+
bool idle = true;
|
|
439
|
+
for (auto& slot : relay_staging) if (relay_readback_ready(slot->texture, true, diagnostic) <= 0) idle = false;
|
|
440
|
+
for (auto& slot : relay_targets) if (!relay_gpu_idle(slot->texture, diagnostic)) idle = false;
|
|
441
|
+
return idle;
|
|
442
|
+
}
|
|
443
|
+
void wait_relay(const GpuJob* reading, const GpuJob* uploading, const std::deque<GpuJob>& pending, bool retiring) {
|
|
444
|
+
if (wait_failed) { std::this_thread::sleep_for(std::chrono::milliseconds(5)); return; }
|
|
445
|
+
std::vector<HANDLE> waits;
|
|
446
|
+
int64_t wait_ns = retiring ? 5000000 : INT64_MAX;
|
|
447
|
+
const auto add = [&](uintptr_t event) {
|
|
448
|
+
if (event && waits.size() < MAXIMUM_WAIT_OBJECTS - 1) waits.push_back(reinterpret_cast<HANDLE>(event));
|
|
449
|
+
else wait_ns = std::min<int64_t>(wait_ns, 1000000);
|
|
450
|
+
};
|
|
451
|
+
const auto deadline = [&](const GpuJob& job) {
|
|
452
|
+
wait_ns = std::min<int64_t>(wait_ns, std::chrono::duration_cast<std::chrono::nanoseconds>(job.event->deadline - std::chrono::steady_clock::now()).count());
|
|
453
|
+
};
|
|
454
|
+
if (retiring) {
|
|
455
|
+
for (auto& slot : relay_staging) add(relay_readback_event(slot->texture));
|
|
456
|
+
for (auto& slot : relay_targets) add(relay_completion_event(slot->texture));
|
|
457
|
+
} else {
|
|
458
|
+
{
|
|
459
|
+
std::lock_guard<std::mutex> lock(jobs_mutex);
|
|
460
|
+
ResetEvent(jobs_event);
|
|
461
|
+
if (stopped || (!jobs.empty() && (jobs.front().retirement || (!reading && available_slot(relay_staging))))) return;
|
|
462
|
+
}
|
|
463
|
+
waits.push_back(jobs_event);
|
|
464
|
+
if (reading) { add(relay_readback_event(reading->staging->texture)); deadline(*reading); }
|
|
465
|
+
if (uploading) { add(relay_completion_event(uploading->texture)); deadline(*uploading); }
|
|
466
|
+
if (!pending.empty()) {
|
|
467
|
+
deadline(pending.front());
|
|
468
|
+
if (!uploading && available_slot(relay_targets)) wait_ns = std::min<int64_t>(wait_ns, 1000000);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
if (wait_ns != INT64_MAX) {
|
|
472
|
+
LARGE_INTEGER due; due.QuadPart = -std::max<int64_t>(1, wait_ns / 100);
|
|
473
|
+
if (!SetWaitableTimer(retry_timer, &due, 0, nullptr, nullptr, FALSE)) {
|
|
474
|
+
wait_failed = true; fail("Native GPU retry timer failed; retaining unfinished GPU resources"); stop(); return;
|
|
475
|
+
}
|
|
476
|
+
waits.push_back(retry_timer);
|
|
477
|
+
}
|
|
478
|
+
if (WaitForMultipleObjects(static_cast<DWORD>(waits.size()), waits.data(), FALSE, INFINITE) == WAIT_FAILED) {
|
|
479
|
+
wait_failed = true; fail("Native GPU worker wait failed; retaining unfinished GPU resources"); stop();
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
void relay_loop() {
|
|
483
|
+
std::unique_ptr<GpuJob> reading, uploading;
|
|
484
|
+
std::deque<GpuJob> pending;
|
|
485
|
+
bool retiring = false, stalled_reported = false;
|
|
486
|
+
std::chrono::steady_clock::time_point cleanup_deadline;
|
|
487
|
+
for (;;) {
|
|
488
|
+
if (stopped) {
|
|
489
|
+
if (!retiring) { retiring = true; cleanup_deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(readback_timeout); }
|
|
490
|
+
std::string diagnostic;
|
|
491
|
+
if (!relay_idle(diagnostic)) {
|
|
492
|
+
if (!stalled_reported && std::chrono::steady_clock::now() >= cleanup_deadline) {
|
|
493
|
+
stalled_reported = true; fail("Native GPU cleanup is stalled; close is pending and resources and budget remain retained");
|
|
494
|
+
}
|
|
495
|
+
wait_relay(reading.get(), uploading.get(), pending, true); continue;
|
|
496
|
+
}
|
|
497
|
+
reading.reset(); uploading.reset(); pending.clear();
|
|
498
|
+
std::deque<GpuJob> remaining;
|
|
499
|
+
{ std::lock_guard<std::mutex> lock(jobs_mutex); remaining.swap(jobs); }
|
|
500
|
+
for (auto& job : remaining) if (job.retirement) ledger->budget->release(job.retirement);
|
|
501
|
+
break;
|
|
502
|
+
}
|
|
503
|
+
try {
|
|
504
|
+
std::string diagnostic;
|
|
505
|
+
if (uploading && advance(*uploading)) {
|
|
506
|
+
deliver_frame(std::move(uploading->event)); uploading.reset();
|
|
507
|
+
}
|
|
508
|
+
if (reading) {
|
|
509
|
+
if (std::chrono::steady_clock::now() >= reading->event->deadline) throw std::runtime_error("Native source readback timed out");
|
|
510
|
+
const auto complete = relay_readback_ready(reading->staging->texture, false, diagnostic);
|
|
511
|
+
if (complete < 0) throw std::runtime_error(diagnostic);
|
|
512
|
+
if (complete > 0) {
|
|
513
|
+
Packet packet; packet.kind = dsh_render::release; packet.frame_id = ledger->release_source(reading->event->delivery);
|
|
514
|
+
if (!enqueue(packet)) throw std::runtime_error("Cannot return native source credit");
|
|
515
|
+
if (latest) pending.clear();
|
|
516
|
+
pending.push_back(std::move(*reading)); reading.reset();
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
if (!uploading && !pending.empty()) {
|
|
520
|
+
auto& job = pending.front();
|
|
521
|
+
if (std::chrono::steady_clock::now() >= job.event->deadline) throw std::runtime_error("Native frame expired before upload");
|
|
522
|
+
auto target = available_slot(relay_targets);
|
|
523
|
+
if (target) {
|
|
524
|
+
const auto& packet = job.event->packet;
|
|
525
|
+
const auto submitted = relay_upload(job.staging->texture, packet.width, packet.height, display_adapter,
|
|
526
|
+
relay_device, target->texture, job.event->deadline, stopped, diagnostic);
|
|
527
|
+
if (submitted < 0) throw std::runtime_error(diagnostic);
|
|
528
|
+
if (submitted > 0) {
|
|
529
|
+
job.target = target; job.texture = target->texture; job.staging.reset();
|
|
530
|
+
job.event->resource = relay_handle(target->texture); job.event->presentation_adapter = display_adapter;
|
|
531
|
+
{ std::lock_guard<std::mutex> lock(ledger->mutex); ledger->frames.at(job.event->delivery).target = target; }
|
|
532
|
+
uploading = std::make_unique<GpuJob>(std::move(job)); pending.pop_front();
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
GpuJob job; bool found = false;
|
|
537
|
+
std::shared_ptr<RelayStaging> staging;
|
|
538
|
+
if (!reading) staging = available_slot(relay_staging);
|
|
539
|
+
{
|
|
540
|
+
std::lock_guard<std::mutex> lock(jobs_mutex);
|
|
541
|
+
if (!jobs.empty() && (jobs.front().retirement || staging)) {
|
|
542
|
+
job = std::move(jobs.front()); jobs.pop_front(); found = true;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
if (found) {
|
|
546
|
+
if (job.retirement) ledger->budget->release(job.retirement);
|
|
547
|
+
else {
|
|
548
|
+
auto& event = *job.event; const auto& packet = event.packet;
|
|
549
|
+
if (std::chrono::steady_clock::now() >= event.deadline) throw std::runtime_error("Native frame expired in source queue");
|
|
550
|
+
if (!resource_dimensions(event.resource, packet.width, packet.height, packet.stride, diagnostic, event.adapter, &event.source_keyed_mutex)) throw std::runtime_error(diagnostic);
|
|
551
|
+
if (producer_adapter != event.adapter) throw std::runtime_error("Native texture adapter differs from producer handshake");
|
|
552
|
+
job.staging = staging;
|
|
553
|
+
if (!relay_readback_begin(event.resource, packet.width, packet.height, staging->texture, diagnostic, event.deadline)) throw std::runtime_error(diagnostic);
|
|
554
|
+
reading = std::make_unique<GpuJob>(std::move(job));
|
|
555
|
+
}
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
} catch (const std::exception& exception) { if (!stopped) fail(exception.what()); stop(); continue; }
|
|
559
|
+
wait_relay(reading.get(), uploading.get(), pending, false);
|
|
560
|
+
}
|
|
561
|
+
{ std::lock_guard<std::mutex> lock(ledger->mutex); relay_staging.clear(); relay_targets.clear(); }
|
|
562
|
+
relay_device.reset(); clear_validation_device();
|
|
563
|
+
}
|
|
564
|
+
#endif
|
|
565
|
+
void copy_loop() {
|
|
566
|
+
#if defined(_WIN32)
|
|
567
|
+
{
|
|
568
|
+
std::unique_lock<std::mutex> lock(jobs_mutex);
|
|
569
|
+
jobs_ready.wait(lock, [&] { return input_done || !jobs.empty(); });
|
|
570
|
+
}
|
|
571
|
+
if (presentation_path == PresentationPath::cpu_relay) { relay_loop(); return; }
|
|
572
|
+
#endif
|
|
573
|
+
std::deque<GpuJob> active;
|
|
574
|
+
#if defined(_WIN32)
|
|
575
|
+
bool retiring = false, stalled_reported = false;
|
|
576
|
+
std::chrono::steady_clock::time_point cleanup_deadline;
|
|
577
|
+
#endif
|
|
578
|
+
for (;;) {
|
|
579
|
+
#if defined(_WIN32)
|
|
580
|
+
if (stopped) {
|
|
581
|
+
if (!retiring) { retiring = true; cleanup_deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(readback_timeout); }
|
|
582
|
+
std::string diagnostic;
|
|
583
|
+
if (!gpu_idle(diagnostic)) {
|
|
584
|
+
if (!stalled_reported && std::chrono::steady_clock::now() >= cleanup_deadline) {
|
|
585
|
+
stalled_reported = true;
|
|
586
|
+
fail("Native GPU cleanup is stalled; close is pending and resources and budget remain retained");
|
|
587
|
+
}
|
|
588
|
+
wait_gpu(active, true); continue;
|
|
589
|
+
}
|
|
590
|
+
// All submitted copies completed or their device was removed before ownership is released.
|
|
591
|
+
active.clear();
|
|
592
|
+
} else {
|
|
593
|
+
try {
|
|
594
|
+
deliver_ready(active);
|
|
595
|
+
for (size_t index = 1; !stopped && index < active.size(); ++index) {
|
|
596
|
+
// A completed head is delivered before another slot performs Map or upload.
|
|
597
|
+
const auto prior_size = active.size(); deliver_ready(active);
|
|
598
|
+
if (active.size() != prior_size) break;
|
|
599
|
+
advance(active[index]);
|
|
600
|
+
}
|
|
601
|
+
deliver_ready(active);
|
|
602
|
+
} catch (const std::exception& exception) { if (!stopped) fail(exception.what()); stop(); continue; }
|
|
603
|
+
}
|
|
604
|
+
#endif
|
|
605
|
+
GpuJob job; bool found = false, finished;
|
|
606
|
+
{
|
|
607
|
+
std::lock_guard<std::mutex> lock(jobs_mutex);
|
|
608
|
+
if (!jobs.empty()) { job = std::move(jobs.front()); jobs.pop_front(); found = true; }
|
|
609
|
+
finished = input_done && jobs.empty();
|
|
610
|
+
#if defined(_WIN32)
|
|
611
|
+
if (!found) ResetEvent(jobs_event);
|
|
612
|
+
#endif
|
|
613
|
+
}
|
|
614
|
+
if (found) {
|
|
615
|
+
if (job.retirement) {
|
|
616
|
+
#if defined(_WIN32)
|
|
617
|
+
relay_textures.erase(job.allocation);
|
|
618
|
+
#endif
|
|
619
|
+
ledger->budget->release(job.retirement);
|
|
620
|
+
} else if (!stopped) {
|
|
621
|
+
try {
|
|
622
|
+
prepare_frame(job);
|
|
623
|
+
#if defined(_WIN32)
|
|
624
|
+
if (job.texture) active.push_back(std::move(job));
|
|
625
|
+
else
|
|
626
|
+
#endif
|
|
627
|
+
deliver_frame(std::move(job.event));
|
|
628
|
+
} catch (const std::exception& exception) { if (!stopped) fail(exception.what()); stop(); }
|
|
629
|
+
}
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
if (finished && active.empty()) break;
|
|
633
|
+
#if defined(_WIN32)
|
|
634
|
+
wait_gpu(active, false);
|
|
635
|
+
#else
|
|
636
|
+
std::unique_lock<std::mutex> lock(jobs_mutex);
|
|
637
|
+
jobs_ready.wait(lock, [&] { return input_done || !jobs.empty(); });
|
|
638
|
+
#endif
|
|
639
|
+
}
|
|
640
|
+
clear_validation_device();
|
|
641
|
+
#if defined(_WIN32)
|
|
642
|
+
relay_textures.clear(); relay_device.reset();
|
|
643
|
+
#endif
|
|
644
|
+
}
|
|
645
|
+
void read_loop() {
|
|
646
|
+
bool authenticated = false;
|
|
647
|
+
while (!stopped && !dispatcher->failed) {
|
|
648
|
+
Received received;
|
|
649
|
+
int result = channel->receive(received, 100);
|
|
650
|
+
if (result == 0) continue;
|
|
651
|
+
if (result < 0) break;
|
|
652
|
+
const auto received_at = std::chrono::steady_clock::now();
|
|
653
|
+
auto& packet = received.packet;
|
|
654
|
+
if (packet.marker != magic || packet.version != DSH_RENDER_PROTOCOL_VERSION || !std::memchr(packet.text, 0, sizeof(packet.text))) {
|
|
655
|
+
release_resource(received.resource); fail("Native render protocol mismatch"); break;
|
|
656
|
+
}
|
|
657
|
+
if (!authenticated) {
|
|
658
|
+
if (packet.kind != hello || received.resource || std::strncmp(packet.token, endpoint.token.c_str(), sizeof(packet.token)) != 0) {
|
|
659
|
+
release_resource(received.resource); fail("Native render producer authentication failed"); break;
|
|
660
|
+
}
|
|
661
|
+
{
|
|
662
|
+
std::unique_lock<std::mutex> lock(ledger->mutex);
|
|
663
|
+
while (!stopped && !expected_pid) bound.wait_for(lock, std::chrono::milliseconds(100));
|
|
664
|
+
}
|
|
665
|
+
if (stopped) break;
|
|
666
|
+
if (received.pid != expected_pid) { fail("Native render producer identity mismatch"); break; }
|
|
667
|
+
producer_adapter = packet.text;
|
|
668
|
+
presentation_path = resolve_presentation_path(producer_adapter, display_adapter, (packet.max_frames & hello_keyed_mutex) != 0);
|
|
669
|
+
allocation_factor = allocation_bytes_factor(presentation_path);
|
|
670
|
+
Packet response; response.kind = ready; response.max_frames = max_frames; response.max_bytes = source_byte_budget(presentation_path, max_frames, max_bytes);
|
|
671
|
+
if (!channel->send(response)) break;
|
|
672
|
+
authenticated = true; ledger->connected = true;
|
|
673
|
+
auto event = new Event; event->type = "connected"; event->pid = received.pid; emit(event);
|
|
674
|
+
continue;
|
|
675
|
+
}
|
|
676
|
+
if (received.pid != expected_pid) { release_resource(received.resource); fail("Native render sender identity changed"); break; }
|
|
677
|
+
if (packet.kind == allocate && !received.resource) {
|
|
678
|
+
bool valid;
|
|
679
|
+
{
|
|
680
|
+
std::lock_guard<std::mutex> lock(ledger->mutex);
|
|
681
|
+
uint64_t reserved = 0; for (auto& allocation : ledger->allocations) reserved += allocation.second->bytes / allocation_factor;
|
|
682
|
+
valid = !stopped && packet.allocation_id && !ledger->allocations.count(packet.allocation_id) && ledger->allocations.size() < max_frames &&
|
|
683
|
+
packet.max_bytes && packet.max_bytes <= source_byte_budget(presentation_path, max_frames, max_bytes) &&
|
|
684
|
+
reserved <= source_byte_budget(presentation_path, max_frames, max_bytes) - packet.max_bytes;
|
|
685
|
+
#if defined(_WIN32)
|
|
686
|
+
if (presentation_path == PresentationPath::cpu_relay) {
|
|
687
|
+
valid = valid && packet.max_bytes <= max_bytes / (2 * max_frames + 2);
|
|
688
|
+
if (valid) ledger->allocations.emplace(packet.allocation_id, reserve_relay(packet.allocation_id, packet.max_bytes));
|
|
689
|
+
} else
|
|
690
|
+
#endif
|
|
691
|
+
if (valid) ledger->allocations.emplace(packet.allocation_id, ledger->budget->request(packet.allocation_id, packet.max_bytes * allocation_factor, output));
|
|
692
|
+
}
|
|
693
|
+
if (!valid) { fail("Invalid native render allocation request"); break; }
|
|
694
|
+
} else if (packet.kind == deallocate && !received.resource) {
|
|
695
|
+
bool used = false;
|
|
696
|
+
std::shared_ptr<Reservation> reservation;
|
|
697
|
+
{
|
|
698
|
+
std::lock_guard<std::mutex> lock(ledger->mutex);
|
|
699
|
+
for (auto& frame : ledger->frames) if (frame.second.resource && frame.second.allocation == packet.allocation_id) used = true;
|
|
700
|
+
auto allocation = ledger->allocations.find(packet.allocation_id);
|
|
701
|
+
if (!used && allocation != ledger->allocations.end()) {
|
|
702
|
+
reservation = allocation->second; ledger->allocations.erase(allocation);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
if (used) { fail("Producer released an in-use native render allocation"); break; }
|
|
706
|
+
if (reservation) {
|
|
707
|
+
GpuJob job{{}, packet.allocation_id, std::move(reservation)};
|
|
708
|
+
if (!post(std::move(job))) {
|
|
709
|
+
// Close retires this reservation after the worker has drained its GPU resources.
|
|
710
|
+
{ std::lock_guard<std::mutex> lock(ledger->mutex); ledger->allocations.emplace(packet.allocation_id, std::move(job.retirement)); }
|
|
711
|
+
fail("Native GPU job queue rejected allocation retirement"); break;
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
if (presentation_path == PresentationPath::iosurface) clear_validation_device();
|
|
715
|
+
} else if (packet.kind == frame) {
|
|
716
|
+
if (!received.resource || !packet.frame_id || packet.frame_id > 9007199254740991ULL ||
|
|
717
|
+
!packet.width || !packet.height || packet.width > 16384 || packet.height > 16384 ||
|
|
718
|
+
#if defined(__APPLE__)
|
|
719
|
+
// IOSurface aligns row pitch; resource_dimensions checks the actual surface stride.
|
|
720
|
+
packet.stride < packet.width * 4) {
|
|
721
|
+
#else
|
|
722
|
+
packet.stride != packet.width * 4) {
|
|
723
|
+
#endif
|
|
724
|
+
release_resource(received.resource); fail("Invalid native render frame fields"); break;
|
|
725
|
+
}
|
|
726
|
+
#if defined(__APPLE__)
|
|
727
|
+
const auto size = resource_bytes(received.resource);
|
|
728
|
+
#else
|
|
729
|
+
const auto size = static_cast<uint64_t>(packet.stride) * packet.height;
|
|
730
|
+
#endif
|
|
731
|
+
bool accepted;
|
|
732
|
+
const auto delivery = next_delivery++;
|
|
733
|
+
{
|
|
734
|
+
std::lock_guard<std::mutex> lock(ledger->mutex);
|
|
735
|
+
auto allocation = ledger->allocations.find(packet.allocation_id);
|
|
736
|
+
size_t source_count = 0;
|
|
737
|
+
for (auto& frame : ledger->frames) if (frame.second.resource) ++source_count;
|
|
738
|
+
accepted = allocation != ledger->allocations.end() && allocation->second->granted && size <= allocation->second->bytes / allocation_factor && source_count < max_frames && size <= max_bytes && ledger->bytes <= max_bytes - size && delivery <= 9007199254740991ULL;
|
|
739
|
+
for (auto& frame : ledger->frames) if (frame.second.resource && (frame.second.allocation == packet.allocation_id || frame.second.source_id == packet.frame_id)) accepted = false;
|
|
740
|
+
if (accepted) { ledger->frames.emplace(delivery, HeldFrame{received.resource, size, packet.allocation_id, packet.frame_id}); ledger->bytes += size; }
|
|
741
|
+
}
|
|
742
|
+
if (!accepted) { release_resource(received.resource); fail("Native render frame budget or identifier violated"); break; }
|
|
743
|
+
auto event = std::make_unique<Event>(); event->type = "frame"; event->packet = packet; event->resource = received.resource; event->delivery = delivery;
|
|
744
|
+
event->ledger = ledger; event->deadline = received_at + std::chrono::milliseconds(readback_timeout);
|
|
745
|
+
if (presentation_path == PresentationPath::iosurface) process_frame(std::move(event));
|
|
746
|
+
else {
|
|
747
|
+
GpuJob job; job.event = std::move(event);
|
|
748
|
+
if (!post(std::move(job))) { fail("Native GPU job queue rejected frame"); break; }
|
|
749
|
+
}
|
|
750
|
+
} else if (packet.kind == message && !received.resource) {
|
|
751
|
+
auto event = new Event; event->type = "message"; event->text = packet.text; if (!emit(event)) break;
|
|
752
|
+
} else if (packet.kind == goodbye && !received.resource) break;
|
|
753
|
+
else { release_resource(received.resource); fail("Unexpected native render message"); break; }
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
void run() {
|
|
757
|
+
try {
|
|
758
|
+
writer = std::thread([this] { write_loop(); });
|
|
759
|
+
copier = std::thread([this] { copy_loop(); });
|
|
760
|
+
read_loop();
|
|
761
|
+
} catch (const std::exception& exception) { fail(exception.what()); }
|
|
762
|
+
stop();
|
|
763
|
+
{ std::lock_guard<std::mutex> lock(jobs_mutex); input_done = true; }
|
|
764
|
+
notify_jobs();
|
|
765
|
+
if (copier.joinable()) copier.join();
|
|
766
|
+
if (writer.joinable()) writer.join();
|
|
767
|
+
clear_validation_device();
|
|
768
|
+
ledger->connected = false;
|
|
769
|
+
Packet closing; closing.kind = goodbye; channel->send(closing);
|
|
770
|
+
ledger->retire_idle();
|
|
771
|
+
auto event = new Event; event->type = "disconnected"; emit(event);
|
|
772
|
+
stop();
|
|
773
|
+
{
|
|
774
|
+
std::lock_guard<std::mutex> lock(dispatch_mutex);
|
|
775
|
+
if (!worker_released) { worker_released = true; napi_release_threadsafe_function(callback, napi_tsfn_release); }
|
|
776
|
+
}
|
|
777
|
+
ended = true; uv_async_send(&done);
|
|
778
|
+
}
|
|
779
|
+
};
|
|
780
|
+
void receiver_done_closed(uv_handle_t* handle) {
|
|
781
|
+
auto holder = static_cast<std::shared_ptr<Receiver>*>(handle->data);
|
|
782
|
+
if ((*holder)->cleanup) napi_remove_async_cleanup_hook((*holder)->cleanup);
|
|
783
|
+
delete holder;
|
|
784
|
+
}
|
|
785
|
+
void settle_close(Receiver* self) {
|
|
786
|
+
if (!self->close_work) return;
|
|
787
|
+
if (!self->environment_closing) {
|
|
788
|
+
napi_handle_scope scope;
|
|
789
|
+
if (napi_open_handle_scope(self->env, &scope) == napi_ok) {
|
|
790
|
+
napi_value result; napi_get_undefined(self->env, &result);
|
|
791
|
+
napi_resolve_deferred(self->env, self->close_work->deferred, result);
|
|
792
|
+
napi_close_handle_scope(self->env, scope);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
napi_delete_reference(self->env, self->close_work->keep);
|
|
796
|
+
self->close_work.reset();
|
|
797
|
+
}
|
|
798
|
+
void receiver_finish_on_main(Receiver* self) {
|
|
799
|
+
if (!self->ended || self->closing_handle) return;
|
|
800
|
+
self->closing_handle = true;
|
|
801
|
+
// The reader joins both I/O helpers and drains submitted GPU work before setting ended.
|
|
802
|
+
if (self->worker.joinable()) self->worker.join();
|
|
803
|
+
self->channel.reset();
|
|
804
|
+
settle_close(self);
|
|
805
|
+
{
|
|
806
|
+
std::lock_guard<std::mutex> lock(self->dispatch_mutex);
|
|
807
|
+
if (!self->main_released) { self->main_released = true; napi_release_threadsafe_function(self->callback, napi_tsfn_release); }
|
|
808
|
+
}
|
|
809
|
+
uv_close(reinterpret_cast<uv_handle_t*>(&self->done), receiver_done_closed);
|
|
810
|
+
}
|
|
811
|
+
void receiver_abandon_setup(Receiver* self, bool handle_initialized) {
|
|
812
|
+
self->stop(); self->ended = true; self->environment_closing = true; self->closing_handle = true;
|
|
813
|
+
if (self->callback) self->close_dispatcher();
|
|
814
|
+
if (handle_initialized) uv_close(reinterpret_cast<uv_handle_t*>(&self->done), receiver_done_closed);
|
|
815
|
+
else { delete static_cast<std::shared_ptr<Receiver>*>(self->done.data); self->done.data = nullptr; }
|
|
816
|
+
}
|
|
817
|
+
napi_value string(napi_env env, const std::string& value) { napi_value result; napi_create_string_utf8(env, value.data(), value.size(), &result); return result; }
|
|
818
|
+
napi_value number(napi_env env, double value) { napi_value result; napi_create_double(env, value, &result); return result; }
|
|
819
|
+
void property(napi_env env, napi_value object, const char* name, napi_value value) { napi_set_named_property(env, object, name, value); }
|
|
820
|
+
void dispatch(napi_env env, napi_value callback, void* context, void* data) {
|
|
821
|
+
std::unique_ptr<Event> event(static_cast<Event*>(data));
|
|
822
|
+
if (!env || !callback) return;
|
|
823
|
+
auto& dispatcher = **static_cast<std::shared_ptr<Dispatcher>*>(context);
|
|
824
|
+
if (event->type == "frame" && (dispatcher.failed || !event->ledger->accepting_frames)) return;
|
|
825
|
+
if (event->type == "frame" && std::chrono::steady_clock::now() >= event->deadline) {
|
|
826
|
+
dispatcher.failed = true; event->type = "error"; event->text = "Native frame expired in JavaScript delivery queue";
|
|
827
|
+
}
|
|
828
|
+
napi_value object; napi_create_object(env, &object);
|
|
829
|
+
property(env, object, "type", string(env, event->type));
|
|
830
|
+
if (event->type == "frame") {
|
|
831
|
+
property(env, object, "frameId", number(env, static_cast<double>(event->packet.frame_id)));
|
|
832
|
+
property(env, object, "deliveryId", number(env, static_cast<double>(event->delivery)));
|
|
833
|
+
property(env, object, "width", number(env, event->packet.width)); property(env, object, "height", number(env, event->packet.height));
|
|
834
|
+
property(env, object, "stride", number(env, event->packet.stride)); property(env, object, "metadata", string(env, event->packet.text));
|
|
835
|
+
property(env, object, "transport", string(env, "shared-texture"));
|
|
836
|
+
napi_value source_keyed_mutex;
|
|
837
|
+
napi_get_boolean(env, event->source_keyed_mutex, &source_keyed_mutex);
|
|
838
|
+
property(env, object, "sourceKeyedMutex", source_keyed_mutex);
|
|
839
|
+
napi_value buffer;
|
|
840
|
+
if (napi_create_buffer_copy(env, sizeof(event->resource), &event->resource, nullptr, &buffer) != napi_ok) {
|
|
841
|
+
dispatcher.failed = true;
|
|
842
|
+
bool pending = false;
|
|
843
|
+
napi_is_exception_pending(env, &pending);
|
|
844
|
+
if (pending) { napi_value exception; napi_get_and_clear_last_exception(env, &exception); }
|
|
845
|
+
napi_create_object(env, &object);
|
|
846
|
+
property(env, object, "type", string(env, "error"));
|
|
847
|
+
property(env, object, "text", string(env, "Cannot allocate native render handle buffer"));
|
|
848
|
+
} else {
|
|
849
|
+
property(env, object, "handle", buffer);
|
|
850
|
+
napi_value presentation;
|
|
851
|
+
if (event->presentation_adapter.empty()) napi_get_null(env, &presentation);
|
|
852
|
+
else presentation = string(env, event->presentation_adapter);
|
|
853
|
+
property(env, object, "presentationAdapterId", presentation);
|
|
854
|
+
}
|
|
855
|
+
property(env, object, "adapterId", string(env, event->adapter));
|
|
856
|
+
} else if (event->type == "connected") property(env, object, "pid", number(env, event->pid));
|
|
857
|
+
else if (!event->text.empty()) property(env, object, "text", string(env, event->text));
|
|
858
|
+
if (event->type == "frame" && !dispatcher.failed) {
|
|
859
|
+
std::lock_guard<std::mutex> lock(event->ledger->mutex);
|
|
860
|
+
if (!event->ledger->accepting_frames) return;
|
|
861
|
+
if (std::chrono::steady_clock::now() >= event->deadline) {
|
|
862
|
+
dispatcher.failed = true;
|
|
863
|
+
napi_create_object(env, &object);
|
|
864
|
+
property(env, object, "type", string(env, "error"));
|
|
865
|
+
property(env, object, "text", string(env, "Native frame expired during JavaScript delivery"));
|
|
866
|
+
} else {
|
|
867
|
+
event->ledger->frames.at(event->delivery).delivered = true;
|
|
868
|
+
event->delivered = true;
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
napi_value global, result; napi_get_global(env, &global); napi_call_function(env, global, callback, 1, &object, &result);
|
|
872
|
+
}
|
|
873
|
+
napi_value fail(napi_env env, const char* text) { napi_throw_error(env, nullptr, text); return nullptr; }
|
|
874
|
+
Receiver* receiver(napi_env env, napi_value value) {
|
|
875
|
+
void* result = nullptr;
|
|
876
|
+
if (napi_get_value_external(env, value, &result) != napi_ok || !result) { napi_throw_type_error(env, nullptr, "Invalid native receiver"); return nullptr; }
|
|
877
|
+
return static_cast<std::shared_ptr<Receiver>*>(result)->get();
|
|
878
|
+
}
|
|
879
|
+
bool uint_value(napi_env env, napi_value value, uint64_t& output) {
|
|
880
|
+
double number = 0;
|
|
881
|
+
if (napi_get_value_double(env, value, &number) != napi_ok || !std::isfinite(number) || number < 1 || number > 9007199254740991.0 || std::floor(number) != number) return false;
|
|
882
|
+
output = static_cast<uint64_t>(number); return true;
|
|
883
|
+
}
|
|
884
|
+
std::string token() {
|
|
885
|
+
unsigned char random[32];
|
|
886
|
+
#if defined(__APPLE__)
|
|
887
|
+
arc4random_buf(random, sizeof(random));
|
|
888
|
+
#else
|
|
889
|
+
if (BCryptGenRandom(nullptr, random, sizeof(random), BCRYPT_USE_SYSTEM_PREFERRED_RNG) != 0) throw std::runtime_error("Cannot generate render authentication secret");
|
|
890
|
+
#endif
|
|
891
|
+
const char* hex = "0123456789abcdef"; std::string result;
|
|
892
|
+
for (auto byte : random) { result += hex[byte >> 4]; result += hex[byte & 15]; } return result;
|
|
893
|
+
}
|
|
894
|
+
napi_value create(napi_env env, napi_callback_info info) {
|
|
895
|
+
napi_value args[7]; size_t count = 7; napi_get_cb_info(env, info, &count, args, nullptr, nullptr);
|
|
896
|
+
bool latest;
|
|
897
|
+
uint64_t max_frames, max_bytes, timeout; napi_valuetype callback_type; char adapter[32]{}; size_t adapter_size;
|
|
898
|
+
if (count != 7 || napi_get_value_bool(env, args[6], &latest) != napi_ok || !uint_value(env, args[0], max_frames) || max_frames > 1024 || !uint_value(env, args[1], max_bytes) ||
|
|
899
|
+
!uint_value(env, args[5], timeout) || timeout > UINT32_MAX ||
|
|
900
|
+
napi_get_value_string_utf8(env, args[4], adapter, sizeof(adapter), &adapter_size) != napi_ok || adapter_size >= sizeof(adapter) - 1 ||
|
|
901
|
+
napi_typeof(env, args[2], &callback_type) != napi_ok || callback_type != napi_function) return fail(env, "Invalid native receiver options");
|
|
902
|
+
#if defined(_WIN32)
|
|
903
|
+
if (!adapter_size) return fail(env, "Native rendering unavailable: Windows requires a display adapter");
|
|
904
|
+
#endif
|
|
905
|
+
std::shared_ptr<Receiver> value;
|
|
906
|
+
bool handle_initialized = false;
|
|
907
|
+
try {
|
|
908
|
+
value = std::make_shared<Receiver>(); value->env = env;
|
|
909
|
+
#if defined(_WIN32)
|
|
910
|
+
value->jobs_event = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
|
911
|
+
value->retry_timer = reinterpret_cast<HANDLE>(create_gpu_retry_timer());
|
|
912
|
+
if (!value->jobs_event || !value->retry_timer) return fail(env, "Cannot create GPU worker wake event or retry timer");
|
|
913
|
+
#endif
|
|
914
|
+
value->max_frames = static_cast<uint32_t>(max_frames); value->max_bytes = max_bytes;
|
|
915
|
+
value->display_adapter = adapter; value->readback_timeout = static_cast<uint32_t>(timeout); value->latest = latest;
|
|
916
|
+
void* budget_pointer = nullptr;
|
|
917
|
+
if (napi_get_value_external(env, args[3], &budget_pointer) != napi_ok || !budget_pointer) return fail(env, "Invalid native render budget");
|
|
918
|
+
value->ledger->budget = *static_cast<std::shared_ptr<Budget>*>(budget_pointer);
|
|
919
|
+
if (max_bytes > value->ledger->budget->limit) return fail(env, "View budget exceeds shared native render budget");
|
|
920
|
+
value->endpoint.pid = process_id(); value->endpoint.token = token();
|
|
921
|
+
#if defined(__APPLE__)
|
|
922
|
+
value->endpoint.name = "ai.deepseek.render." + token();
|
|
923
|
+
#else
|
|
924
|
+
value->endpoint.name = "\\\\.\\pipe\\dsh-render-" + token();
|
|
925
|
+
#endif
|
|
926
|
+
value->channel = listen_channel(value->endpoint);
|
|
927
|
+
auto endpoint = std::string("1|") + std::to_string(value->endpoint.pid) + "|" + value->endpoint.token + "|" + value->endpoint.name;
|
|
928
|
+
auto name = string(env, "DSH native render receiver");
|
|
929
|
+
auto dispatcher = std::make_unique<std::shared_ptr<Dispatcher>>(value->dispatcher);
|
|
930
|
+
if (napi_create_threadsafe_function(env, args[2], nullptr, name, 256, 2, dispatcher.get(), [](napi_env env, void* data, void*) {
|
|
931
|
+
std::unique_ptr<std::shared_ptr<Dispatcher>> dispatcher(static_cast<std::shared_ptr<Dispatcher>*>(data));
|
|
932
|
+
}, dispatcher.get(), dispatch, &value->callback) != napi_ok)
|
|
933
|
+
return fail(env, "Cannot create native render event dispatcher");
|
|
934
|
+
dispatcher.release();
|
|
935
|
+
uv_loop_t* loop;
|
|
936
|
+
if (napi_get_uv_event_loop(env, &loop) != napi_ok) {
|
|
937
|
+
receiver_abandon_setup(value.get(), false); return fail(env, "Cannot find native receiver event loop");
|
|
938
|
+
}
|
|
939
|
+
value->done.data = new std::shared_ptr<Receiver>(value);
|
|
940
|
+
if (uv_async_init(loop, &value->done, [](uv_async_t* handle) {
|
|
941
|
+
receiver_finish_on_main(static_cast<std::shared_ptr<Receiver>*>(handle->data)->get());
|
|
942
|
+
}) != 0) {
|
|
943
|
+
receiver_abandon_setup(value.get(), false); return fail(env, "Cannot create native receiver completion handle");
|
|
944
|
+
}
|
|
945
|
+
handle_initialized = true;
|
|
946
|
+
if (napi_add_async_cleanup_hook(env, [](napi_async_cleanup_hook_handle, void* data) {
|
|
947
|
+
auto self = static_cast<Receiver*>(data); self->environment_closing = true;
|
|
948
|
+
self->stop(); self->close_dispatcher(); receiver_finish_on_main(self);
|
|
949
|
+
}, value.get(), &value->cleanup) != napi_ok) {
|
|
950
|
+
receiver_abandon_setup(value.get(), true); return fail(env, "Cannot register native receiver cleanup");
|
|
951
|
+
}
|
|
952
|
+
napi_value external, result;
|
|
953
|
+
auto raw = value.get();
|
|
954
|
+
auto holder = std::make_unique<std::shared_ptr<Receiver>>(value);
|
|
955
|
+
if (napi_create_external(env, holder.get(), [](napi_env, void* data, void*) {
|
|
956
|
+
std::unique_ptr<std::shared_ptr<Receiver>> holder(static_cast<std::shared_ptr<Receiver>*>(data));
|
|
957
|
+
(*holder)->stop(); (*holder)->close_dispatcher();
|
|
958
|
+
}, nullptr, &external) != napi_ok) {
|
|
959
|
+
receiver_abandon_setup(raw, true); return fail(env, "Cannot create native receiver owner");
|
|
960
|
+
}
|
|
961
|
+
holder.release();
|
|
962
|
+
napi_create_object(env, &result); property(env, result, "native", external); property(env, result, "endpoint", string(env, endpoint));
|
|
963
|
+
raw->worker = std::thread([raw] { raw->run(); });
|
|
964
|
+
return result;
|
|
965
|
+
} catch (const std::exception& exception) {
|
|
966
|
+
if (value && value->callback) receiver_abandon_setup(value.get(), handle_initialized);
|
|
967
|
+
return fail(env, exception.what());
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
napi_value bind(napi_env env, napi_callback_info info) {
|
|
971
|
+
napi_value args[2]; size_t count = 2; napi_get_cb_info(env, info, &count, args, nullptr, nullptr);
|
|
972
|
+
if (count != 2) return fail(env, "Expected receiver and producer PID");
|
|
973
|
+
auto value = receiver(env, args[0]); uint64_t pid;
|
|
974
|
+
if (!value) return nullptr;
|
|
975
|
+
if (!uint_value(env, args[1], pid) || pid > UINT32_MAX || value->stopped || value->expected_pid) return fail(env, "Producer can only be bound once to a live receiver");
|
|
976
|
+
{ std::lock_guard<std::mutex> lock(value->ledger->mutex); value->expected_pid = static_cast<uint32_t>(pid); }
|
|
977
|
+
value->bound.notify_all();
|
|
978
|
+
napi_value result; napi_get_undefined(env, &result); return result;
|
|
979
|
+
}
|
|
980
|
+
napi_value send(napi_env env, napi_callback_info info) {
|
|
981
|
+
napi_value args[2]; size_t count = 2; napi_get_cb_info(env, info, &count, args, nullptr, nullptr);
|
|
982
|
+
if (count != 2) return fail(env, "Expected receiver and control JSON");
|
|
983
|
+
auto value = receiver(env, args[0]); if (!value) return nullptr;
|
|
984
|
+
Packet packet; packet.kind = message; size_t length;
|
|
985
|
+
if (napi_get_value_string_utf8(env, args[1], nullptr, 0, &length) != napi_ok || length >= sizeof(packet.text)) return fail(env, "Control message exceeds native render limit");
|
|
986
|
+
napi_get_value_string_utf8(env, args[1], packet.text, sizeof(packet.text), &length);
|
|
987
|
+
if (!value->enqueue(packet)) return fail(env, "Native render producer is disconnected");
|
|
988
|
+
napi_value result; napi_get_undefined(env, &result); return result;
|
|
989
|
+
}
|
|
990
|
+
napi_value release(napi_env env, napi_callback_info info) {
|
|
991
|
+
napi_value args[2]; size_t count = 2; napi_get_cb_info(env, info, &count, args, nullptr, nullptr);
|
|
992
|
+
if (count != 2) return fail(env, "Expected receiver and delivery ID");
|
|
993
|
+
auto value = receiver(env, args[0]); uint64_t id; if (!value) return nullptr;
|
|
994
|
+
if (!uint_value(env, args[1], id)) return fail(env, "Invalid delivery ID");
|
|
995
|
+
uint64_t source_id = 0;
|
|
996
|
+
const bool released = value->ledger->release(id, true, &source_id);
|
|
997
|
+
if (released) value->notify_jobs();
|
|
998
|
+
if (source_id) {
|
|
999
|
+
Packet packet; packet.kind = dsh_render::release; packet.frame_id = source_id;
|
|
1000
|
+
if (value->ledger->connected && !value->enqueue(packet)) value->stop();
|
|
1001
|
+
}
|
|
1002
|
+
napi_value result; napi_get_boolean(env, released, &result); return result;
|
|
1003
|
+
}
|
|
1004
|
+
napi_value close(napi_env env, napi_callback_info info) {
|
|
1005
|
+
napi_value args[1]; size_t count = 1; napi_get_cb_info(env, info, &count, args, nullptr, nullptr);
|
|
1006
|
+
if (count != 1) return fail(env, "Expected receiver");
|
|
1007
|
+
auto value = receiver(env, args[0]); if (!value) return nullptr;
|
|
1008
|
+
if (value->closing) return fail(env, "Native receiver close already requested");
|
|
1009
|
+
auto work = std::make_unique<CloseWork>(); napi_value promise;
|
|
1010
|
+
if (napi_create_promise(env, &work->deferred, &promise) != napi_ok ||
|
|
1011
|
+
napi_create_reference(env, args[0], 1, &work->keep) != napi_ok)
|
|
1012
|
+
return fail(env, "Cannot retain native receiver close operation");
|
|
1013
|
+
value->close_work = std::move(work); value->closing = true; value->stop();
|
|
1014
|
+
if (value->closing_handle) settle_close(value);
|
|
1015
|
+
else receiver_finish_on_main(value);
|
|
1016
|
+
return promise;
|
|
1017
|
+
}
|
|
1018
|
+
napi_value create_budget(napi_env env, napi_callback_info info) {
|
|
1019
|
+
napi_value args[1]; size_t count = 1; napi_get_cb_info(env, info, &count, args, nullptr, nullptr);
|
|
1020
|
+
uint64_t bytes;
|
|
1021
|
+
if (count != 1 || !uint_value(env, args[0], bytes)) return fail(env, "Invalid shared native render byte budget");
|
|
1022
|
+
auto budget = new std::shared_ptr<Budget>(std::make_shared<Budget>(bytes));
|
|
1023
|
+
napi_value result;
|
|
1024
|
+
napi_create_external(env, budget, [](napi_env, void* data, void*) { delete static_cast<std::shared_ptr<Budget>*>(data); }, nullptr, &result);
|
|
1025
|
+
return result;
|
|
1026
|
+
}
|
|
1027
|
+
/** Inspect an Electron-owned handle without selecting another GPU. */
|
|
1028
|
+
napi_value get_adapter(napi_env env, napi_callback_info info) {
|
|
1029
|
+
napi_value args[1]; size_t count = 1; napi_get_cb_info(env, info, &count, args, nullptr, nullptr);
|
|
1030
|
+
void* data = nullptr; size_t size = 0;
|
|
1031
|
+
if (count != 1 || napi_get_buffer_info(env, args[0], &data, &size) != napi_ok || size != sizeof(uintptr_t))
|
|
1032
|
+
return fail(env, "Expected an eight-byte native texture handle");
|
|
1033
|
+
uintptr_t handle; std::memcpy(&handle, data, size);
|
|
1034
|
+
return string(env, resource_adapter(handle));
|
|
1035
|
+
}
|
|
1036
|
+
napi_value initialize(napi_env env, napi_value exports) {
|
|
1037
|
+
register_node_producer(env, exports);
|
|
1038
|
+
const napi_property_descriptor methods[] = {
|
|
1039
|
+
{"createBudget", nullptr, create_budget, nullptr, nullptr, nullptr, napi_default, nullptr},
|
|
1040
|
+
{"getAdapter", nullptr, get_adapter, nullptr, nullptr, nullptr, napi_default, nullptr},
|
|
1041
|
+
{"create", nullptr, create, nullptr, nullptr, nullptr, napi_default, nullptr},
|
|
1042
|
+
{"bind", nullptr, bind, nullptr, nullptr, nullptr, napi_default, nullptr},
|
|
1043
|
+
{"send", nullptr, send, nullptr, nullptr, nullptr, napi_default, nullptr},
|
|
1044
|
+
{"release", nullptr, release, nullptr, nullptr, nullptr, napi_default, nullptr},
|
|
1045
|
+
{"close", nullptr, close, nullptr, nullptr, nullptr, napi_default, nullptr}
|
|
1046
|
+
};
|
|
1047
|
+
napi_define_properties(env, exports, sizeof(methods) / sizeof(methods[0]), methods); return exports;
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
NAPI_MODULE(NODE_GYP_MODULE_NAME, initialize)
|