@deepseek-ai/libreoffice-kit 0.2.0 → 0.2.3-rc.3
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 +1 -1
- package/README.md +5 -5
- package/README.zh.md +5 -5
- package/frame-forwarding/CMakeLists.txt +3 -4
- package/frame-forwarding/README.md +21 -17
- package/frame-forwarding/README.zh.md +22 -18
- package/frame-forwarding/include/dsh_frame_forwarding.h +42 -9
- package/frame-forwarding/index.d.ts +58 -54
- package/frame-forwarding/index.mjs +14 -17
- package/frame-forwarding/prebuilds/darwin-arm64/frame-forwarding.node +0 -0
- package/frame-forwarding/prebuilds/darwin-arm64/libdsh_frame_forwarding_sdk.a +0 -0
- package/frame-forwarding/prebuilds/darwin-arm64/manifest.json +23 -21
- package/frame-forwarding/prebuilds/darwin-x64/frame-forwarding.node +0 -0
- package/frame-forwarding/prebuilds/darwin-x64/libdsh_frame_forwarding_sdk.a +0 -0
- package/frame-forwarding/prebuilds/darwin-x64/manifest.json +23 -21
- package/frame-forwarding/prebuilds/win32-arm64/dsh_frame_forwarding_sdk.lib +0 -0
- package/frame-forwarding/prebuilds/win32-arm64/frame-forwarding.node +0 -0
- package/frame-forwarding/prebuilds/win32-arm64/manifest.json +23 -21
- package/frame-forwarding/prebuilds/win32-x64/dsh_frame_forwarding_sdk.lib +0 -0
- package/frame-forwarding/prebuilds/win32-x64/frame-forwarding.node +0 -0
- package/frame-forwarding/prebuilds/win32-x64/manifest.json +23 -21
- package/frame-forwarding/receipt.json +16 -14
- package/frame-forwarding/scripts/build-webgpu.mjs +1 -0
- package/frame-forwarding/scripts/build.mjs +3 -3
- package/frame-forwarding/scripts/metadata.mjs +12 -8
- package/frame-forwarding/scripts/source-identity.mjs +3 -3
- package/frame-forwarding/sdk.json +7 -5
- package/frame-forwarding/{include/dsh_webgpu.h → src/graphite-interop-internal.hxx} +22 -22
- package/frame-forwarding/src/{webgpu.cc → graphite-interop.cc} +34 -24
- package/frame-forwarding/src/node-producer.cc +24 -154
- package/frame-forwarding/src/producer.cc +120 -30
- package/frame-forwarding/src/receiver.cc +422 -212
- package/frame-forwarding/src/relay-transport.inc +38 -10
- package/frame-forwarding/src/transport.cc +328 -155
- package/frame-forwarding/src/transport.h +73 -9
- package/frame-forwarding/verify.d.mts +1 -1
- package/frame-forwarding/verify.mjs +19 -17
- package/lib/cli.js +6 -6
- package/lib/editor.js +15 -10
- package/lib/index.js +6 -6
- package/lib/types/editor.d.ts +17 -4
- package/lib/types/engine.d.ts +1 -1
- package/lib/types/fonts.d.ts +1 -1
- package/lib/worker.js +3 -2
- package/package.json +8 -10
- package/frame-forwarding/include/dsh_frame_forwarding_engine.h +0 -66
- package/frame-forwarding/include/dsh_frame_forwarding_node.h +0 -27
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#include "transport.h"
|
|
2
2
|
#include <algorithm>
|
|
3
3
|
#include <cstring>
|
|
4
|
+
#include <cstdio>
|
|
4
5
|
#include <sstream>
|
|
5
6
|
#include <stdexcept>
|
|
6
7
|
#include <vector>
|
|
@@ -25,15 +26,39 @@ namespace dsh_frame_forwarding {
|
|
|
25
26
|
Endpoint parse_endpoint(const std::string& value) {
|
|
26
27
|
auto a = value.find('|'), b = value.find('|', a + 1), c = value.find('|', b + 1);
|
|
27
28
|
if (a == std::string::npos || b == std::string::npos || c == std::string::npos || value.substr(0, a) != "1")
|
|
28
|
-
throw std::runtime_error("Unsupported
|
|
29
|
+
throw std::runtime_error("Unsupported frame-forwarding endpoint");
|
|
29
30
|
auto pid_text = value.substr(a + 1, b - a - 1);
|
|
30
31
|
size_t used = 0;
|
|
31
32
|
auto pid = std::stoul(pid_text, &used);
|
|
32
33
|
Endpoint result{static_cast<uint32_t>(pid), value.substr(b + 1, c - b - 1), value.substr(c + 1)};
|
|
33
34
|
if (used != pid_text.size() || !pid || pid > UINT32_MAX || result.token.size() != 64 || result.name.empty() || result.name.size() > 200)
|
|
34
|
-
throw std::runtime_error("Invalid
|
|
35
|
+
throw std::runtime_error("Invalid frame-forwarding endpoint");
|
|
35
36
|
return result;
|
|
36
37
|
}
|
|
38
|
+
uint64_t parse_connection(const char (&token)[65]) {
|
|
39
|
+
if (!std::memchr(token, 0, sizeof(token)) || !token[0] || token[0] == '0' || std::strlen(token) > 16) return 0;
|
|
40
|
+
uint64_t value = 0;
|
|
41
|
+
for (const char* digit = token; *digit; ++digit) {
|
|
42
|
+
if (*digit < '0' || *digit > '9') return 0;
|
|
43
|
+
value = value * 10 + static_cast<uint64_t>(*digit - '0');
|
|
44
|
+
}
|
|
45
|
+
return value <= 9007199254740991ULL ? value : 0;
|
|
46
|
+
}
|
|
47
|
+
void Channel::bind_connection(uint64_t id) {
|
|
48
|
+
connection_ = id;
|
|
49
|
+
std::snprintf(connection_text_, sizeof(connection_text_), "%llu", static_cast<unsigned long long>(id));
|
|
50
|
+
}
|
|
51
|
+
int Channel::receive(Received& out, uint32_t timeout_ms) {
|
|
52
|
+
const auto result = receive_packet(out, timeout_ms);
|
|
53
|
+
if (result != 1 || !connection_ || std::strncmp(out.packet.token, connection_text_, sizeof(out.packet.token)) == 0) return result;
|
|
54
|
+
release_resource(out.resource); out.resource = 0; foreign_ = true; interrupt(); return -1;
|
|
55
|
+
}
|
|
56
|
+
bool Channel::send(const Packet& packet, uintptr_t resource) {
|
|
57
|
+
if (!connection_) return send_packet(packet, resource);
|
|
58
|
+
Packet stamped = packet;
|
|
59
|
+
std::memcpy(stamped.token, connection_text_, sizeof(stamped.token));
|
|
60
|
+
return send_packet(stamped, resource);
|
|
61
|
+
}
|
|
37
62
|
#if defined(__APPLE__)
|
|
38
63
|
struct MachMessage {
|
|
39
64
|
mach_msg_header_t header;
|
|
@@ -42,42 +67,85 @@ struct MachMessage {
|
|
|
42
67
|
Packet packet;
|
|
43
68
|
mach_msg_audit_trailer_t trailer;
|
|
44
69
|
};
|
|
70
|
+
/**
|
|
71
|
+
* Receive one protocol message: 1 data, 0 timeout, -1 failure, -2 a message of another shape (destroyed), -3 the
|
|
72
|
+
* watched peer's dead-name notice. Releasing a notification request's send-once right posts a notice that is -2.
|
|
73
|
+
*/
|
|
74
|
+
int receive_mach(mach_port_t port, uint32_t timeout, MachMessage& msg, uint32_t& pid) {
|
|
75
|
+
auto result = mach_msg(&msg.header, MACH_RCV_MSG | MACH_RCV_TIMEOUT |
|
|
76
|
+
MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0) | MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AUDIT),
|
|
77
|
+
0, sizeof(msg), port, timeout, MACH_PORT_NULL);
|
|
78
|
+
if (result == MACH_RCV_TIMED_OUT) return 0;
|
|
79
|
+
if (result != KERN_SUCCESS) return -1;
|
|
80
|
+
if (msg.header.msgh_id == MACH_NOTIFY_DEAD_NAME) { mach_msg_destroy(&msg.header); return -3; }
|
|
81
|
+
if (msg.header.msgh_size != offsetof(MachMessage, trailer) || !(msg.header.msgh_bits & MACH_MSGH_BITS_COMPLEX) ||
|
|
82
|
+
msg.body.msgh_descriptor_count != 1 || msg.port.type != MACH_MSG_PORT_DESCRIPTOR) {
|
|
83
|
+
mach_msg_destroy(&msg.header); return -2;
|
|
84
|
+
}
|
|
85
|
+
auto* trailer = reinterpret_cast<mach_msg_audit_trailer_t*>(reinterpret_cast<char*>(&msg) + msg.header.msgh_size);
|
|
86
|
+
if (trailer->msgh_trailer_size < sizeof(mach_msg_audit_trailer_t)) { mach_msg_destroy(&msg.header); return -2; }
|
|
87
|
+
pid = static_cast<uint32_t>(audit_token_to_pid(trailer->msgh_audit));
|
|
88
|
+
return 1;
|
|
89
|
+
}
|
|
90
|
+
mach_port_t allocate_port() {
|
|
91
|
+
mach_port_t port = MACH_PORT_NULL;
|
|
92
|
+
if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port) != KERN_SUCCESS) throw std::runtime_error("Cannot allocate frame-forwarding Mach port");
|
|
93
|
+
if (mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS) {
|
|
94
|
+
mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_RECEIVE, -1); throw std::runtime_error("Cannot allocate frame-forwarding Mach port");
|
|
95
|
+
}
|
|
96
|
+
return port;
|
|
97
|
+
}
|
|
98
|
+
void destroy_port(mach_port_t port) {
|
|
99
|
+
if (!port) return;
|
|
100
|
+
mach_port_deallocate(mach_task_self(), port);
|
|
101
|
+
mach_port_mod_refs(mach_task_self(), port, MACH_PORT_RIGHT_RECEIVE, -1);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* One producer connection. The producer's hello carries its reply port; the receiver answers on it with ready, whose
|
|
105
|
+
* descriptor is the connection port this channel receives on, so every connection owns its ports.
|
|
106
|
+
*/
|
|
45
107
|
class MachChannel final : public Channel {
|
|
46
108
|
mach_port_t local_ = MACH_PORT_NULL, remote_ = MACH_PORT_NULL;
|
|
47
|
-
Endpoint endpoint_;
|
|
48
109
|
bool server_;
|
|
110
|
+
/** The process this connection's packets must come from: the receiver for a producer, the candidate for a receiver. */
|
|
111
|
+
uint32_t source_pid_;
|
|
49
112
|
std::atomic<bool> stopped_{false};
|
|
50
113
|
std::atomic<uint32_t> peer_{0};
|
|
51
114
|
std::mutex writes_;
|
|
52
|
-
public:
|
|
53
|
-
MachChannel(Endpoint endpoint, bool server) : endpoint_(std::move(endpoint)), server_(server) {
|
|
54
|
-
if (mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &local_) != KERN_SUCCESS ||
|
|
55
|
-
mach_port_insert_right(mach_task_self(), local_, local_, MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS)
|
|
56
|
-
{ if (local_) mach_port_mod_refs(mach_task_self(), local_, MACH_PORT_RIGHT_RECEIVE, -1); throw std::runtime_error("Cannot allocate native render Mach port"); }
|
|
57
|
-
kern_return_t result;
|
|
58
|
-
if (server_) result = bootstrap_register(bootstrap_port, const_cast<char*>(endpoint_.name.c_str()), local_);
|
|
59
|
-
else result = bootstrap_look_up(bootstrap_port, const_cast<char*>(endpoint_.name.c_str()), &remote_);
|
|
60
|
-
if (result != KERN_SUCCESS) {
|
|
61
|
-
mach_port_deallocate(mach_task_self(), local_);
|
|
62
|
-
mach_port_mod_refs(mach_task_self(), local_, MACH_PORT_RIGHT_RECEIVE, -1);
|
|
63
|
-
throw std::runtime_error("Cannot register or discover native render Mach endpoint");
|
|
64
|
-
}
|
|
65
|
-
if (!server_) watch_peer();
|
|
66
|
-
}
|
|
67
|
-
~MachChannel() override {
|
|
68
|
-
if (server_) { const auto ignored = bootstrap_register(bootstrap_port, const_cast<char*>(endpoint_.name.c_str()), MACH_PORT_NULL); (void)ignored; }
|
|
69
|
-
if (remote_) mach_port_deallocate(mach_task_self(), remote_);
|
|
70
|
-
if (local_) { mach_port_deallocate(mach_task_self(), local_); mach_port_mod_refs(mach_task_self(), local_, MACH_PORT_RIGHT_RECEIVE, -1); }
|
|
71
|
-
}
|
|
72
115
|
void watch_peer() {
|
|
73
116
|
mach_port_t previous = MACH_PORT_NULL;
|
|
74
117
|
auto result = mach_port_request_notification(mach_task_self(), remote_, MACH_NOTIFY_DEAD_NAME, 0, local_, MACH_MSG_TYPE_MAKE_SEND_ONCE, &previous);
|
|
75
118
|
if (previous) mach_port_deallocate(mach_task_self(), previous);
|
|
76
119
|
if (result != KERN_SUCCESS) stopped_ = true;
|
|
77
120
|
}
|
|
121
|
+
/** Cancel the dead-name request so releasing the right posts no port-deleted notice. */
|
|
122
|
+
void unwatch_peer() {
|
|
123
|
+
mach_port_t previous = MACH_PORT_NULL;
|
|
124
|
+
mach_port_request_notification(mach_task_self(), remote_, MACH_NOTIFY_DEAD_NAME, 0, MACH_PORT_NULL, MACH_MSG_TYPE_MAKE_SEND_ONCE, &previous);
|
|
125
|
+
if (previous) mach_port_deallocate(mach_task_self(), previous);
|
|
126
|
+
}
|
|
127
|
+
public:
|
|
128
|
+
/** Producer side: discover the listener; ready later replaces it with the connection port. */
|
|
129
|
+
explicit MachChannel(const Endpoint& endpoint) : server_(false), source_pid_(endpoint.pid) {
|
|
130
|
+
local_ = allocate_port();
|
|
131
|
+
if (bootstrap_look_up(bootstrap_port, const_cast<char*>(endpoint.name.c_str()), &remote_) != KERN_SUCCESS) {
|
|
132
|
+
destroy_port(local_); throw std::runtime_error("Cannot discover frame-forwarding Mach endpoint");
|
|
133
|
+
}
|
|
134
|
+
watch_peer();
|
|
135
|
+
}
|
|
136
|
+
/** Receiver side: adopt the candidate's reply port and open a connection port for it. */
|
|
137
|
+
MachChannel(mach_port_t reply, uint32_t pid) : remote_(reply), server_(true), source_pid_(pid) {
|
|
138
|
+
try { local_ = allocate_port(); } catch (...) { mach_port_deallocate(mach_task_self(), reply); throw; }
|
|
139
|
+
peer_ = pid; watch_peer();
|
|
140
|
+
}
|
|
141
|
+
~MachChannel() override {
|
|
142
|
+
if (remote_) { unwatch_peer(); mach_port_deallocate(mach_task_self(), remote_); }
|
|
143
|
+
destroy_port(local_);
|
|
144
|
+
}
|
|
78
145
|
uint32_t peer_pid() const override { return peer_; }
|
|
79
146
|
void interrupt() override { stopped_ = true; }
|
|
80
|
-
|
|
147
|
+
protected:
|
|
148
|
+
bool send_packet(const Packet& packet, uintptr_t resource) override {
|
|
81
149
|
std::lock_guard<std::mutex> lock(writes_);
|
|
82
150
|
if (stopped_ || !remote_) return false;
|
|
83
151
|
MachMessage msg{};
|
|
@@ -89,44 +157,64 @@ class MachChannel final : public Channel {
|
|
|
89
157
|
msg.port.type = MACH_MSG_PORT_DESCRIPTOR;
|
|
90
158
|
msg.port.disposition = MACH_MSG_TYPE_COPY_SEND;
|
|
91
159
|
mach_port_t surface_port = MACH_PORT_NULL;
|
|
92
|
-
if (packet.kind == hello) msg.port.name = local_;
|
|
160
|
+
if ((!server_ && packet.kind == hello) || (server_ && packet.kind == ready)) msg.port.name = local_;
|
|
93
161
|
else if (resource) msg.port.name = surface_port = IOSurfaceCreateMachPort(reinterpret_cast<IOSurfaceRef>(resource));
|
|
94
162
|
msg.packet = packet;
|
|
95
163
|
auto result = mach_msg(&msg.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, msg.header.msgh_size, 0, MACH_PORT_NULL, 1000, MACH_PORT_NULL);
|
|
96
164
|
if (surface_port) mach_port_deallocate(mach_task_self(), surface_port);
|
|
97
165
|
return result == KERN_SUCCESS;
|
|
98
166
|
}
|
|
99
|
-
int
|
|
167
|
+
int receive_packet(Received& out, uint32_t timeout) override {
|
|
100
168
|
if (stopped_) return -1;
|
|
101
|
-
MachMessage msg{};
|
|
102
|
-
auto result =
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (
|
|
107
|
-
if (
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
auto* trailer = reinterpret_cast<mach_msg_audit_trailer_t*>(reinterpret_cast<char*>(&msg) + msg.header.msgh_size);
|
|
112
|
-
if (trailer->msgh_trailer_size < sizeof(mach_msg_audit_trailer_t)) { mach_msg_destroy(&msg.header); return -1; }
|
|
113
|
-
out.pid = static_cast<uint32_t>(audit_token_to_pid(trailer->msgh_audit));
|
|
114
|
-
out.packet = msg.packet;
|
|
115
|
-
if (server_ && out.packet.kind == hello && !remote_ && std::strncmp(out.packet.token, endpoint_.token.c_str(), sizeof(out.packet.token)) == 0) {
|
|
116
|
-
remote_ = msg.port.name; peer_ = out.pid; msg.port.name = MACH_PORT_NULL; watch_peer();
|
|
169
|
+
MachMessage msg{}; uint32_t pid = 0;
|
|
170
|
+
const auto result = receive_mach(local_, timeout, msg, pid);
|
|
171
|
+
if (result == 0 || result == -2) return stopped_ ? -1 : 0;
|
|
172
|
+
if (result < 0) return -1;
|
|
173
|
+
out.pid = pid; out.packet = msg.packet;
|
|
174
|
+
if (pid != source_pid_) { mach_msg_destroy(&msg.header); return -1; }
|
|
175
|
+
if (!server_ && out.packet.kind == ready && msg.port.name) {
|
|
176
|
+
// The connection port replaces the listener for every later packet.
|
|
177
|
+
unwatch_peer(); mach_port_deallocate(mach_task_self(), remote_); remote_ = msg.port.name; msg.port.name = MACH_PORT_NULL; watch_peer();
|
|
117
178
|
} else if (out.packet.kind == frame && msg.port.name) {
|
|
118
179
|
out.resource = reinterpret_cast<uintptr_t>(IOSurfaceLookupFromMachPort(msg.port.name));
|
|
119
180
|
}
|
|
120
181
|
if (msg.port.name) mach_port_deallocate(mach_task_self(), msg.port.name);
|
|
121
|
-
if (!server_ && out.pid != endpoint_.pid) { release_resource(out.resource); out.resource = 0; return -1; }
|
|
122
|
-
if (!server_) peer_ = out.pid;
|
|
123
182
|
return 1;
|
|
124
183
|
}
|
|
125
184
|
};
|
|
126
|
-
|
|
185
|
+
class MachListener final : public Listener {
|
|
186
|
+
mach_port_t port_ = MACH_PORT_NULL;
|
|
187
|
+
std::string name_;
|
|
188
|
+
std::atomic<bool> stopped_{false};
|
|
189
|
+
public:
|
|
190
|
+
explicit MachListener(const Endpoint& endpoint) : name_(endpoint.name) {
|
|
191
|
+
port_ = allocate_port();
|
|
192
|
+
if (bootstrap_register(bootstrap_port, const_cast<char*>(name_.c_str()), port_) != KERN_SUCCESS) {
|
|
193
|
+
destroy_port(port_); throw std::runtime_error("Cannot register frame-forwarding Mach endpoint");
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
~MachListener() override {
|
|
197
|
+
const auto ignored = bootstrap_register(bootstrap_port, const_cast<char*>(name_.c_str()), MACH_PORT_NULL); (void)ignored;
|
|
198
|
+
destroy_port(port_);
|
|
199
|
+
}
|
|
200
|
+
void interrupt() override { stopped_ = true; }
|
|
201
|
+
int accept(Candidate& candidate, uint32_t timeout) override {
|
|
202
|
+
if (stopped_) return -1;
|
|
203
|
+
MachMessage msg{}; uint32_t pid = 0;
|
|
204
|
+
const auto result = receive_mach(port_, timeout, msg, pid);
|
|
205
|
+
if (result == 0 || result <= -2) return stopped_ ? -1 : 0;
|
|
206
|
+
if (result < 0) return -1;
|
|
207
|
+
if (msg.packet.kind != hello || !msg.port.name || msg.port.disposition != MACH_MSG_TYPE_PORT_SEND) { mach_msg_destroy(&msg.header); return 0; }
|
|
208
|
+
const auto reply = msg.port.name;
|
|
209
|
+
candidate.hello = Received{}; candidate.hello.packet = msg.packet; candidate.hello.pid = pid;
|
|
210
|
+
candidate.channel = std::make_unique<MachChannel>(reply, pid);
|
|
211
|
+
return 1;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
std::unique_ptr<Listener> listen(Endpoint& endpoint, uint32_t) { return std::make_unique<MachListener>(endpoint); }
|
|
127
215
|
std::unique_ptr<Channel> connect_channel(const Endpoint& endpoint, const std::function<bool()>& cancelled) {
|
|
128
|
-
if (cancelled && cancelled()) throw std::runtime_error("
|
|
129
|
-
return std::make_unique<MachChannel>(endpoint
|
|
216
|
+
if (cancelled && cancelled()) throw std::runtime_error("Frame-forwarding connection cancelled or timed out");
|
|
217
|
+
return std::make_unique<MachChannel>(endpoint);
|
|
130
218
|
}
|
|
131
219
|
uint32_t process_id() { return getpid(); }
|
|
132
220
|
void clear_validation_device() {}
|
|
@@ -134,7 +222,8 @@ std::string resource_adapter(uintptr_t) { return "IOSurface"; }
|
|
|
134
222
|
std::string device_adapter(void*) { return "IOSurface"; }
|
|
135
223
|
void release_resource(uintptr_t resource) { if (resource) CFRelease(reinterpret_cast<IOSurfaceRef>(resource)); }
|
|
136
224
|
uint64_t resource_bytes(uintptr_t resource) { return IOSurfaceGetAllocSize(reinterpret_cast<IOSurfaceRef>(resource)); }
|
|
137
|
-
bool resource_dimensions(uintptr_t resource, uint32_t width, uint32_t height, uint32_t stride,
|
|
225
|
+
bool resource_dimensions(uintptr_t resource, uint32_t width, uint32_t height, uint32_t stride,
|
|
226
|
+
std::chrono::steady_clock::time_point, std::string& diagnostic, std::string& adapter, bool* keyed_mutex) {
|
|
138
227
|
if (keyed_mutex) *keyed_mutex = false;
|
|
139
228
|
diagnostic = "IOSurface format or dimensions differ"; adapter = "IOSurface";
|
|
140
229
|
auto surface = reinterpret_cast<IOSurfaceRef>(resource);
|
|
@@ -221,140 +310,217 @@ std::string resource_adapter(uintptr_t resource) {
|
|
|
221
310
|
FAILED(factory->GetSharedResourceAdapterLuid(reinterpret_cast<HANDLE>(resource), &luid))) return {};
|
|
222
311
|
return adapter_id(luid);
|
|
223
312
|
}
|
|
313
|
+
/** Overlapped I/O wait shared by pipe channels and the listener: 1 completed, 0 timed out, -1 stopped or failed. */
|
|
314
|
+
int wait_pipe(HANDLE pipe, OVERLAPPED& operation, HANDLE stop, HANDLE deadline, DWORD timeout, DWORD& count) {
|
|
315
|
+
HANDLE events[] = {operation.hEvent, stop, deadline};
|
|
316
|
+
const bool timed = timeout != 0 && timeout != INFINITE;
|
|
317
|
+
if (timed) {
|
|
318
|
+
LARGE_INTEGER due; due.QuadPart = -static_cast<LONGLONG>(timeout) * 10000;
|
|
319
|
+
if (!SetWaitableTimer(deadline, &due, 0, nullptr, nullptr, FALSE)) {
|
|
320
|
+
CancelIoEx(pipe, &operation);
|
|
321
|
+
return GetOverlappedResult(pipe, &operation, &count, TRUE) ? 1 : -1;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
// A millisecond pipe poll must not round up to the system's coarse clock tick.
|
|
325
|
+
auto result = WaitForMultipleObjects(timed ? 3 : 2, events, FALSE, timed ? INFINITE : timeout);
|
|
326
|
+
if (timed) {
|
|
327
|
+
CancelWaitableTimer(deadline);
|
|
328
|
+
if (result == WAIT_OBJECT_0 + 2) result = WAIT_TIMEOUT;
|
|
329
|
+
}
|
|
330
|
+
if (result != WAIT_OBJECT_0) {
|
|
331
|
+
CancelIoEx(pipe, &operation);
|
|
332
|
+
const bool completed = GetOverlappedResult(pipe, &operation, &count, TRUE) != FALSE;
|
|
333
|
+
// Completion can race the deadline; preserve a successfully received message.
|
|
334
|
+
if (result == WAIT_TIMEOUT && completed) return 1;
|
|
335
|
+
return result == WAIT_TIMEOUT ? 0 : -1;
|
|
336
|
+
}
|
|
337
|
+
return GetOverlappedResult(pipe, &operation, &count, FALSE) ? 1 : -1;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* One connected pipe instance: the producer's client handle, or one instance the listener accepted. A producer sends
|
|
341
|
+
* its own handle values, and the receiving instance duplicates each one from the producer's process.
|
|
342
|
+
*/
|
|
224
343
|
class PipeChannel final : public Channel {
|
|
225
344
|
HANDLE pipe_ = INVALID_HANDLE_VALUE, stop_ = nullptr, deadline_ = nullptr;
|
|
226
|
-
|
|
227
|
-
|
|
345
|
+
/** The producer process a receiving instance duplicates handles from; null on the producer side. */
|
|
346
|
+
HANDLE producer_ = nullptr;
|
|
228
347
|
std::atomic<uint32_t> peer_{0};
|
|
229
348
|
std::mutex writes_;
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
if (
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
// A millisecond pipe poll must not round up to the system's coarse clock tick.
|
|
244
|
-
auto result = WaitForMultipleObjects(timed ? 3 : 2, events, FALSE, timed ? INFINITE : timeout);
|
|
245
|
-
if (timed) {
|
|
246
|
-
CancelWaitableTimer(deadline_);
|
|
247
|
-
if (result == WAIT_OBJECT_0 + 2) result = WAIT_TIMEOUT;
|
|
248
|
-
}
|
|
249
|
-
if (result != WAIT_OBJECT_0) {
|
|
250
|
-
CancelIoEx(pipe_, &operation);
|
|
251
|
-
const bool completed = GetOverlappedResult(pipe_, &operation, &count, TRUE) != FALSE;
|
|
252
|
-
// Completion can race the deadline; preserve a successfully received message.
|
|
253
|
-
if (result == WAIT_TIMEOUT && completed) return true;
|
|
254
|
-
SetLastError(result == WAIT_TIMEOUT ? ERROR_OPERATION_ABORTED : ERROR_BROKEN_PIPE);
|
|
255
|
-
return false;
|
|
256
|
-
}
|
|
257
|
-
return GetOverlappedResult(pipe_, &operation, &count, FALSE) != FALSE;
|
|
349
|
+
void open_events() {
|
|
350
|
+
stop_ = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
|
351
|
+
deadline_ = create_deadline_timer();
|
|
352
|
+
if (!stop_ || !deadline_) throw std::runtime_error("Cannot create frame-forwarding I/O wait events");
|
|
353
|
+
}
|
|
354
|
+
void close_handles() {
|
|
355
|
+
if (pipe_ != INVALID_HANDLE_VALUE) CloseHandle(pipe_);
|
|
356
|
+
if (stop_) CloseHandle(stop_);
|
|
357
|
+
if (deadline_) CloseHandle(deadline_);
|
|
358
|
+
if (producer_) CloseHandle(producer_);
|
|
359
|
+
pipe_ = INVALID_HANDLE_VALUE; stop_ = nullptr; deadline_ = nullptr; producer_ = nullptr;
|
|
258
360
|
}
|
|
259
361
|
public:
|
|
260
|
-
|
|
261
|
-
|
|
362
|
+
/** Producer side: connect to the receiver's pipe and check its process identity. */
|
|
363
|
+
PipeChannel(const Endpoint& endpoint, const std::function<bool()>& cancelled) {
|
|
262
364
|
try {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
if (server_) {
|
|
266
|
-
HANDLE token = nullptr; DWORD size = 0;
|
|
267
|
-
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) throw std::runtime_error("Cannot inspect receiver identity");
|
|
268
|
-
GetTokenInformation(token, TokenUser, nullptr, 0, &size);
|
|
269
|
-
std::vector<unsigned char> data(size);
|
|
270
|
-
bool ok = GetTokenInformation(token, TokenUser, data.data(), size, &size) != FALSE;
|
|
271
|
-
CloseHandle(token);
|
|
272
|
-
LPSTR sid = nullptr;
|
|
273
|
-
if (!ok || !ConvertSidToStringSidA(reinterpret_cast<TOKEN_USER*>(data.data())->User.Sid, &sid)) throw std::runtime_error("Cannot encode receiver identity");
|
|
274
|
-
std::string sddl = std::string("D:P(A;;GA;;;") + sid + ")"; LocalFree(sid);
|
|
275
|
-
PSECURITY_DESCRIPTOR descriptor = nullptr;
|
|
276
|
-
if (!ConvertStringSecurityDescriptorToSecurityDescriptorA(sddl.c_str(), SDDL_REVISION_1, &descriptor, nullptr)) throw std::runtime_error("Cannot restrict render pipe");
|
|
277
|
-
SECURITY_ATTRIBUTES security{sizeof(security), descriptor, FALSE};
|
|
278
|
-
pipe_ = CreateNamedPipeA(endpoint_.name.c_str(), PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | FILE_FLAG_FIRST_PIPE_INSTANCE,
|
|
279
|
-
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS, 1, sizeof(Packet) * 4, sizeof(Packet) * 4, 0, &security);
|
|
280
|
-
LocalFree(descriptor);
|
|
281
|
-
} else {
|
|
282
|
-
while (!WaitNamedPipeA(endpoint_.name.c_str(), cancelled ? 100 : 10000)) {
|
|
365
|
+
open_events();
|
|
366
|
+
while (!WaitNamedPipeA(endpoint.name.c_str(), cancelled ? 100 : 10000)) {
|
|
283
367
|
const auto error = GetLastError();
|
|
284
|
-
if (cancelled && cancelled()) throw std::runtime_error("
|
|
368
|
+
if (cancelled && cancelled()) throw std::runtime_error("Frame-forwarding connection cancelled or timed out");
|
|
285
369
|
if (!cancelled || (error != ERROR_SEM_TIMEOUT && error != ERROR_PIPE_BUSY))
|
|
286
|
-
throw std::runtime_error("
|
|
370
|
+
throw std::runtime_error("Frame-forwarding receiver unavailable");
|
|
287
371
|
}
|
|
288
|
-
pipe_ = CreateFileA(
|
|
372
|
+
pipe_ = CreateFileA(endpoint.name.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
|
|
373
|
+
if (pipe_ == INVALID_HANDLE_VALUE) throw std::runtime_error("Frame-forwarding receiver unavailable");
|
|
289
374
|
DWORD mode = PIPE_READMODE_MESSAGE; SetNamedPipeHandleState(pipe_, &mode, nullptr, nullptr);
|
|
290
375
|
ULONG pid = 0;
|
|
291
|
-
if (!GetNamedPipeServerProcessId(pipe_, &pid) || pid !=
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
} catch (...) {
|
|
298
|
-
|
|
299
|
-
if (stop_) CloseHandle(stop_);
|
|
300
|
-
if (deadline_) CloseHandle(deadline_);
|
|
301
|
-
throw;
|
|
302
|
-
}
|
|
376
|
+
if (!GetNamedPipeServerProcessId(pipe_, &pid) || pid != endpoint.pid) throw std::runtime_error("Frame-forwarding receiver identity mismatch");
|
|
377
|
+
peer_ = pid;
|
|
378
|
+
} catch (...) { close_handles(); throw; }
|
|
379
|
+
}
|
|
380
|
+
/** Receiver side: adopt an instance whose client connected, with that client's process for handle duplication. */
|
|
381
|
+
PipeChannel(HANDLE pipe, uint32_t pid, HANDLE producer) : pipe_(pipe), producer_(producer) {
|
|
382
|
+
try { open_events(); } catch (...) { close_handles(); throw; }
|
|
383
|
+
peer_ = pid;
|
|
303
384
|
}
|
|
304
|
-
~PipeChannel() override { interrupt();
|
|
385
|
+
~PipeChannel() override { interrupt(); close_handles(); }
|
|
305
386
|
uint32_t peer_pid() const override { return peer_; }
|
|
306
|
-
void interrupt() override { SetEvent(stop_); CancelIoEx(pipe_, nullptr); }
|
|
307
|
-
|
|
387
|
+
void interrupt() override { if (stop_) SetEvent(stop_); if (pipe_ != INVALID_HANDLE_VALUE) CancelIoEx(pipe_, nullptr); }
|
|
388
|
+
protected:
|
|
389
|
+
/** The sender keeps a sent handle open until the receiver releases its frame, so the receiver can duplicate it. */
|
|
390
|
+
bool send_packet(const Packet& source, uintptr_t resource) override {
|
|
308
391
|
std::lock_guard<std::mutex> lock(writes_);
|
|
309
|
-
if (
|
|
310
|
-
Packet packet = source;
|
|
311
|
-
HANDLE remote_handle = nullptr, target = nullptr;
|
|
312
|
-
if (resource) {
|
|
313
|
-
target = OpenProcess(PROCESS_DUP_HANDLE, FALSE, peer_);
|
|
314
|
-
if (!target || !DuplicateHandle(GetCurrentProcess(), reinterpret_cast<HANDLE>(resource), target, &remote_handle, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
|
|
315
|
-
if (target) CloseHandle(target); return false;
|
|
316
|
-
}
|
|
317
|
-
packet.resource = reinterpret_cast<uintptr_t>(remote_handle);
|
|
318
|
-
}
|
|
392
|
+
if (WaitForSingleObject(stop_, 0) == WAIT_OBJECT_0) return false;
|
|
393
|
+
Packet packet = source; packet.resource = static_cast<uint64_t>(resource);
|
|
319
394
|
OVERLAPPED operation{}; operation.hEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr); DWORD count = 0;
|
|
320
395
|
bool ok = WriteFile(pipe_, &packet, sizeof(packet), &count, &operation) != FALSE;
|
|
321
|
-
if (!ok && GetLastError() == ERROR_IO_PENDING) ok =
|
|
396
|
+
if (!ok && GetLastError() == ERROR_IO_PENDING) ok = wait_pipe(pipe_, operation, stop_, deadline_, INFINITE, count) == 1;
|
|
322
397
|
CloseHandle(operation.hEvent);
|
|
323
|
-
if (!ok && remote_handle) {
|
|
324
|
-
HANDLE reclaimed = nullptr;
|
|
325
|
-
if (DuplicateHandle(target, remote_handle, GetCurrentProcess(), &reclaimed, 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) CloseHandle(reclaimed);
|
|
326
|
-
}
|
|
327
|
-
if (target) CloseHandle(target);
|
|
328
398
|
return ok && count == sizeof(packet);
|
|
329
399
|
}
|
|
330
|
-
int
|
|
400
|
+
int receive_packet(Received& out, uint32_t timeout) override {
|
|
331
401
|
if (WaitForSingleObject(stop_, 0) == WAIT_OBJECT_0) return -1;
|
|
332
402
|
OVERLAPPED operation{}; operation.hEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr); DWORD count = 0;
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
403
|
+
int result = ReadFile(pipe_, &out.packet, sizeof(Packet), &count, &operation) != FALSE ? 1 : -1;
|
|
404
|
+
if (result < 0 && GetLastError() == ERROR_IO_PENDING) result = wait_pipe(pipe_, operation, stop_, deadline_, timeout, count);
|
|
405
|
+
CloseHandle(operation.hEvent);
|
|
406
|
+
if (result == 0) return WaitForSingleObject(stop_, 0) == WAIT_OBJECT_0 ? -1 : 0;
|
|
407
|
+
if (result < 0 || count != sizeof(Packet)) return -1;
|
|
408
|
+
out.pid = peer_; out.resource = 0;
|
|
409
|
+
// Only a receiving instance takes handles, and only from the producer's own handle table.
|
|
410
|
+
if (producer_ && out.packet.resource) {
|
|
411
|
+
HANDLE local = nullptr;
|
|
412
|
+
if (!DuplicateHandle(producer_, reinterpret_cast<HANDLE>(static_cast<uintptr_t>(out.packet.resource)), GetCurrentProcess(), &local, 0, FALSE,
|
|
413
|
+
DUPLICATE_SAME_ACCESS)) {
|
|
414
|
+
// An exited producer took its handles with it; a live one named a handle it does not own.
|
|
415
|
+
if (WaitForSingleObject(producer_, 0) != WAIT_OBJECT_0) invalid_resource_ = true;
|
|
416
|
+
interrupt(); return -1;
|
|
417
|
+
}
|
|
418
|
+
out.resource = reinterpret_cast<uintptr_t>(local);
|
|
342
419
|
}
|
|
343
|
-
bool ok = ReadFile(pipe_, &out.packet, sizeof(Packet), &count, &operation) != FALSE;
|
|
344
|
-
if (!ok && GetLastError() == ERROR_IO_PENDING) ok = wait(operation, timeout, count);
|
|
345
|
-
auto error = GetLastError(); CloseHandle(operation.hEvent);
|
|
346
|
-
if (!ok) return error == ERROR_OPERATION_ABORTED && WaitForSingleObject(stop_, 0) != WAIT_OBJECT_0 ? 0 : -1;
|
|
347
|
-
if (count != sizeof(Packet)) return -1;
|
|
348
|
-
out.pid = peer_; out.resource = static_cast<uintptr_t>(out.packet.resource);
|
|
349
420
|
return 1;
|
|
350
421
|
}
|
|
351
422
|
};
|
|
352
|
-
|
|
353
|
-
std::
|
|
423
|
+
/** Per-user pipe security; remote clients are rejected by the pipe mode. */
|
|
424
|
+
std::string user_pipe_sddl() {
|
|
425
|
+
HANDLE token = nullptr; DWORD size = 0;
|
|
426
|
+
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) throw std::runtime_error("Cannot inspect receiver identity");
|
|
427
|
+
GetTokenInformation(token, TokenUser, nullptr, 0, &size);
|
|
428
|
+
std::vector<unsigned char> data(size);
|
|
429
|
+
bool ok = GetTokenInformation(token, TokenUser, data.data(), size, &size) != FALSE;
|
|
430
|
+
CloseHandle(token);
|
|
431
|
+
LPSTR sid = nullptr;
|
|
432
|
+
if (!ok || !ConvertSidToStringSidA(reinterpret_cast<TOKEN_USER*>(data.data())->User.Sid, &sid)) throw std::runtime_error("Cannot encode receiver identity");
|
|
433
|
+
std::string sddl = std::string("D:P(A;;GA;;;") + sid + ")"; LocalFree(sid);
|
|
434
|
+
return sddl;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Pipe instances of one channel name. One instance waits for a client at a time; an instance whose client connected
|
|
438
|
+
* becomes a candidate after its hello arrives within the handshake deadline. The active connection, a waiting
|
|
439
|
+
* successor and a candidate being refused each hold an instance.
|
|
440
|
+
*/
|
|
441
|
+
class PipeListener final : public Listener {
|
|
442
|
+
std::string name_, sddl_;
|
|
443
|
+
uint32_t handshake_ms_;
|
|
444
|
+
HANDLE stop_ = nullptr, deadline_ = nullptr, instance_ = INVALID_HANDLE_VALUE;
|
|
445
|
+
OVERLAPPED connect_{};
|
|
446
|
+
bool first_ = true, connecting_ = false;
|
|
447
|
+
static constexpr DWORD instances = 4;
|
|
448
|
+
void open_instance() {
|
|
449
|
+
PSECURITY_DESCRIPTOR descriptor = nullptr;
|
|
450
|
+
if (!ConvertStringSecurityDescriptorToSecurityDescriptorA(sddl_.c_str(), SDDL_REVISION_1, &descriptor, nullptr)) throw std::runtime_error("Cannot restrict frame-forwarding pipe");
|
|
451
|
+
SECURITY_ATTRIBUTES security{sizeof(security), descriptor, FALSE};
|
|
452
|
+
const DWORD open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | (first_ ? FILE_FLAG_FIRST_PIPE_INSTANCE : 0);
|
|
453
|
+
instance_ = CreateNamedPipeA(name_.c_str(), open_mode, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS,
|
|
454
|
+
instances, sizeof(Packet) * 4, sizeof(Packet) * 4, 0, &security);
|
|
455
|
+
LocalFree(descriptor);
|
|
456
|
+
if (instance_ == INVALID_HANDLE_VALUE) throw std::runtime_error("Cannot open private frame-forwarding pipe");
|
|
457
|
+
first_ = false;
|
|
458
|
+
}
|
|
459
|
+
void close_handles() {
|
|
460
|
+
if (instance_ != INVALID_HANDLE_VALUE) { CancelIoEx(instance_, nullptr); CloseHandle(instance_); instance_ = INVALID_HANDLE_VALUE; }
|
|
461
|
+
if (connect_.hEvent) { CloseHandle(connect_.hEvent); connect_.hEvent = nullptr; }
|
|
462
|
+
if (stop_) { CloseHandle(stop_); stop_ = nullptr; }
|
|
463
|
+
if (deadline_) { CloseHandle(deadline_); deadline_ = nullptr; }
|
|
464
|
+
}
|
|
465
|
+
public:
|
|
466
|
+
PipeListener(const Endpoint& endpoint, uint32_t handshake_ms) : name_(endpoint.name), handshake_ms_(handshake_ms) {
|
|
467
|
+
try {
|
|
468
|
+
sddl_ = user_pipe_sddl();
|
|
469
|
+
stop_ = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
|
470
|
+
deadline_ = create_deadline_timer();
|
|
471
|
+
connect_.hEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
|
472
|
+
if (!stop_ || !deadline_ || !connect_.hEvent) throw std::runtime_error("Cannot create frame-forwarding I/O wait events");
|
|
473
|
+
open_instance();
|
|
474
|
+
} catch (...) { close_handles(); throw; }
|
|
475
|
+
}
|
|
476
|
+
~PipeListener() override { close_handles(); }
|
|
477
|
+
void interrupt() override { SetEvent(stop_); if (instance_ != INVALID_HANDLE_VALUE) CancelIoEx(instance_, nullptr); }
|
|
478
|
+
int accept(Candidate& candidate, uint32_t timeout) override {
|
|
479
|
+
if (WaitForSingleObject(stop_, 0) == WAIT_OBJECT_0) return -1;
|
|
480
|
+
if (instance_ == INVALID_HANDLE_VALUE) open_instance();
|
|
481
|
+
DWORD count = 0;
|
|
482
|
+
if (!connecting_) {
|
|
483
|
+
ResetEvent(connect_.hEvent);
|
|
484
|
+
const bool connected = ConnectNamedPipe(instance_, &connect_) != FALSE;
|
|
485
|
+
const auto error = GetLastError();
|
|
486
|
+
if (!connected && error == ERROR_IO_PENDING) connecting_ = true;
|
|
487
|
+
else if (!connected && error != ERROR_PIPE_CONNECTED) { CloseHandle(instance_); instance_ = INVALID_HANDLE_VALUE; return 0; }
|
|
488
|
+
}
|
|
489
|
+
if (connecting_) {
|
|
490
|
+
HANDLE events[] = {connect_.hEvent, stop_};
|
|
491
|
+
const auto waited = WaitForMultipleObjects(2, events, FALSE, timeout);
|
|
492
|
+
if (waited == WAIT_TIMEOUT) return 0;
|
|
493
|
+
if (waited != WAIT_OBJECT_0) return -1;
|
|
494
|
+
connecting_ = false;
|
|
495
|
+
if (!GetOverlappedResult(instance_, &connect_, &count, FALSE)) { CloseHandle(instance_); instance_ = INVALID_HANDLE_VALUE; return 0; }
|
|
496
|
+
}
|
|
497
|
+
// The connected instance belongs to its candidate; the next accept opens another one.
|
|
498
|
+
HANDLE pipe = instance_; instance_ = INVALID_HANDLE_VALUE;
|
|
499
|
+
ULONG pid = 0;
|
|
500
|
+
if (!GetNamedPipeClientProcessId(pipe, &pid)) { CloseHandle(pipe); return 0; }
|
|
501
|
+
HANDLE producer = OpenProcess(PROCESS_DUP_HANDLE | SYNCHRONIZE, FALSE, pid);
|
|
502
|
+
if (!producer) { CloseHandle(pipe); return 0; }
|
|
503
|
+
auto channel = std::make_unique<PipeChannel>(pipe, static_cast<uint32_t>(pid), producer);
|
|
504
|
+
Received hello;
|
|
505
|
+
if (channel->receive(hello, handshake_ms_) != 1 || hello.resource) { release_resource(hello.resource); return 0; }
|
|
506
|
+
candidate.hello = hello; candidate.channel = std::move(channel);
|
|
507
|
+
return 1;
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
std::unique_ptr<Listener> listen(Endpoint& endpoint, uint32_t handshake_ms) { return std::make_unique<PipeListener>(endpoint, handshake_ms); }
|
|
511
|
+
std::unique_ptr<Channel> connect_channel(const Endpoint& endpoint, const std::function<bool()>& cancelled) { return std::make_unique<PipeChannel>(endpoint, cancelled); }
|
|
354
512
|
uint32_t process_id() { return GetCurrentProcessId(); }
|
|
355
513
|
void release_resource(uintptr_t resource) { if (resource) CloseHandle(reinterpret_cast<HANDLE>(resource)); }
|
|
356
514
|
uint64_t resource_bytes(uintptr_t) { return 0; }
|
|
357
|
-
bool
|
|
515
|
+
bool source_ready(ID3D11Texture2D* texture, std::chrono::steady_clock::time_point deadline, std::string& diagnostic);
|
|
516
|
+
/**
|
|
517
|
+
* Open a producer frame on the validation device of its adapter and check its description. The texture stays open
|
|
518
|
+
* in `texture`, also when the description is invalid. While the producer renders, a keyed-mutex frame released
|
|
519
|
+
* without taking key 0, or opened a second time, stays charged to this process's GPU memory after release; callers
|
|
520
|
+
* take key 0 on this open and never open the frame again.
|
|
521
|
+
*/
|
|
522
|
+
bool open_frame(uintptr_t resource, uint32_t width, uint32_t height, uint32_t stride, std::string& diagnostic,
|
|
523
|
+
std::string& adapter, bool* keyed_mutex, ComPtr<ID3D11Texture2D>& texture) {
|
|
358
524
|
const auto failed = [&](const char* operation, HRESULT result) {
|
|
359
525
|
std::ostringstream text; text << operation << " HRESULT=0x" << std::hex << static_cast<uint32_t>(result);
|
|
360
526
|
if (!adapter.empty()) text << " adapter=" << adapter;
|
|
@@ -383,7 +549,6 @@ bool resource_dimensions(uintptr_t resource, uint32_t width, uint32_t height, ui
|
|
|
383
549
|
if (FAILED(result)) return failed("D3D11.1 receiver unavailable", result);
|
|
384
550
|
validation_device = device1.Detach();
|
|
385
551
|
}
|
|
386
|
-
ComPtr<ID3D11Texture2D> texture;
|
|
387
552
|
auto result = validation_device->OpenSharedResource1(reinterpret_cast<HANDLE>(resource), IID_PPV_ARGS(&texture));
|
|
388
553
|
if (FAILED(result)) return failed("Shared texture is incompatible with receiver GPU", result);
|
|
389
554
|
D3D11_TEXTURE2D_DESC desc{}; texture->GetDesc(&desc);
|
|
@@ -391,13 +556,21 @@ bool resource_dimensions(uintptr_t resource, uint32_t width, uint32_t height, ui
|
|
|
391
556
|
const bool valid = desc.Width == width && desc.Height == height && desc.Format == DXGI_FORMAT_B8G8R8A8_UNORM &&
|
|
392
557
|
stride == width * 4 && desc.MipLevels == 1 && desc.ArraySize == 1 && desc.SampleDesc.Count == 1 && (desc.MiscFlags & D3D11_RESOURCE_MISC_SHARED_NTHANDLE);
|
|
393
558
|
if (!valid) diagnostic = "Shared D3D11 texture format, dimensions or sharing flags differ; adapter=" + adapter;
|
|
559
|
+
return valid;
|
|
560
|
+
}
|
|
561
|
+
bool resource_dimensions(uintptr_t resource, uint32_t width, uint32_t height, uint32_t stride,
|
|
562
|
+
std::chrono::steady_clock::time_point deadline, std::string& diagnostic, std::string& adapter, bool* keyed_mutex) {
|
|
563
|
+
ComPtr<ID3D11Texture2D> texture;
|
|
564
|
+
bool valid = open_frame(resource, width, height, stride, diagnostic, adapter, keyed_mutex, texture);
|
|
565
|
+
if (!texture) return false;
|
|
566
|
+
std::string synchronization;
|
|
567
|
+
if (!source_ready(texture.Get(), deadline, synchronization) && valid) { diagnostic = synchronization; valid = false; }
|
|
394
568
|
texture.Reset();
|
|
395
569
|
// This context submits no GPU work; Flush retires released validation textures.
|
|
396
570
|
ComPtr<ID3D11DeviceContext> reclamation;
|
|
397
571
|
validation_device->GetImmediateContext(&reclamation);
|
|
398
572
|
reclamation->Flush();
|
|
399
573
|
return valid;
|
|
400
|
-
|
|
401
574
|
}
|
|
402
575
|
#include "relay-transport.inc"
|
|
403
576
|
#endif
|