@deepseek-ai/libreoffice-kit 0.1.5 → 0.2.0-alpha6
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 +44 -8
- package/README.zh.md +45 -9
- package/frame-forwarding/CMakeLists.txt +40 -0
- package/frame-forwarding/LICENSE +21 -0
- package/frame-forwarding/README.md +83 -0
- package/frame-forwarding/README.zh.md +83 -0
- package/frame-forwarding/include/dsh_frame_forwarding.h +101 -0
- package/frame-forwarding/include/dsh_frame_forwarding_engine.h +66 -0
- package/frame-forwarding/include/dsh_frame_forwarding_node.h +27 -0
- package/frame-forwarding/include/dsh_webgpu.h +52 -0
- package/frame-forwarding/index.d.ts +145 -0
- package/frame-forwarding/index.mjs +71 -0
- package/frame-forwarding/lib/browser/display.d.ts +46 -0
- package/frame-forwarding/lib/browser/display.js +128 -0
- package/frame-forwarding/lib/browser/index.d.ts +4 -0
- package/frame-forwarding/lib/browser/index.js +3 -0
- package/frame-forwarding/lib/browser/painters.d.ts +9 -0
- package/frame-forwarding/lib/browser/painters.js +180 -0
- package/frame-forwarding/lib/browser/types.d.ts +51 -0
- package/frame-forwarding/lib/browser/types.js +1 -0
- package/frame-forwarding/lib/electron/adapter.d.ts +9 -0
- package/frame-forwarding/lib/electron/adapter.js +118 -0
- package/frame-forwarding/lib/electron/index.d.ts +3 -0
- package/frame-forwarding/lib/electron/index.js +3 -0
- package/frame-forwarding/lib/electron/preload.d.ts +13 -0
- package/frame-forwarding/lib/electron/preload.js +47 -0
- package/frame-forwarding/lib/electron/transfer.d.ts +15 -0
- package/frame-forwarding/lib/electron/transfer.js +36 -0
- 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 +34 -0
- 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 +34 -0
- 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 +34 -0
- 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 +34 -0
- package/frame-forwarding/receipt.json +23 -0
- package/frame-forwarding/scripts/build-webgpu.mjs +197 -0
- package/frame-forwarding/scripts/build.mjs +43 -0
- package/frame-forwarding/scripts/metadata.mjs +47 -0
- package/frame-forwarding/scripts/source-identity.mjs +15 -0
- package/frame-forwarding/scripts/verify-prebuilds.mjs +14 -0
- package/frame-forwarding/sdk.json +45 -0
- package/frame-forwarding/src/node-producer.cc +595 -0
- package/frame-forwarding/src/producer-internal.h +8 -0
- package/frame-forwarding/src/producer.cc +306 -0
- package/frame-forwarding/src/receiver.cc +1050 -0
- package/frame-forwarding/src/relay-transport.inc +293 -0
- package/frame-forwarding/src/transport.cc +405 -0
- package/frame-forwarding/src/transport.h +80 -0
- package/frame-forwarding/src/webgpu.cc +214 -0
- package/frame-forwarding/src/win-delay-load.LICENSE +24 -0
- package/frame-forwarding/src/win-delay-load.cc +43 -0
- package/frame-forwarding/verify.d.mts +4 -0
- package/frame-forwarding/verify.mjs +101 -0
- package/frame-forwarding/webgpu-dependencies.json +51 -0
- package/lib/cli.js +109 -46
- package/lib/editor.js +2061 -0
- package/lib/index.js +109 -46
- 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 +30 -2
- package/lib/types/runtime.d.ts +2 -0
- package/lib/types/worker.d.ts +2 -2
- package/lib/worker.js +38 -31
- package/package.json +94 -9
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
#include "transport.h"
|
|
2
|
+
#include "producer-internal.h"
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <cstring>
|
|
5
|
+
#include <vector>
|
|
6
|
+
#include <limits>
|
|
7
|
+
#include <chrono>
|
|
8
|
+
#if defined(__APPLE__)
|
|
9
|
+
#include <CoreFoundation/CoreFoundation.h>
|
|
10
|
+
#include <IOSurface/IOSurface.h>
|
|
11
|
+
#include <unistd.h>
|
|
12
|
+
#else
|
|
13
|
+
#define NOMINMAX
|
|
14
|
+
#include <windows.h>
|
|
15
|
+
#include <d3d11_1.h>
|
|
16
|
+
#include <dxgi1_2.h>
|
|
17
|
+
#include <wrl/client.h>
|
|
18
|
+
using Microsoft::WRL::ComPtr;
|
|
19
|
+
#endif
|
|
20
|
+
using namespace dsh_frame_forwarding;
|
|
21
|
+
struct dsh_frame_forwarding_surface {
|
|
22
|
+
uint32_t width = 0, height = 0, stride = 0;
|
|
23
|
+
uint64_t bytes = 0, frame_id = 0, allocation_id = 0;
|
|
24
|
+
enum { pending, granted, available, acquired, submitted } state = pending;
|
|
25
|
+
dsh_frame_forwarding_client* owner = nullptr;
|
|
26
|
+
#if defined(__APPLE__)
|
|
27
|
+
IOSurfaceRef surface = nullptr;
|
|
28
|
+
~dsh_frame_forwarding_surface() { if (surface) CFRelease(surface); }
|
|
29
|
+
#else
|
|
30
|
+
ComPtr<ID3D11Texture2D> texture;
|
|
31
|
+
ComPtr<IDXGIKeyedMutex> keyed_mutex;
|
|
32
|
+
HANDLE handle = nullptr;
|
|
33
|
+
~dsh_frame_forwarding_surface() { if (handle) CloseHandle(handle); }
|
|
34
|
+
#endif
|
|
35
|
+
};
|
|
36
|
+
struct dsh_frame_forwarding_client {
|
|
37
|
+
std::unique_ptr<Channel> channel;
|
|
38
|
+
std::mutex pool_mutex;
|
|
39
|
+
std::vector<std::unique_ptr<dsh_frame_forwarding_surface>> surfaces;
|
|
40
|
+
uint32_t max_frames = 0;
|
|
41
|
+
uint64_t max_bytes = 0, next_allocation = 1;
|
|
42
|
+
std::string error;
|
|
43
|
+
std::atomic<bool> stopped{false};
|
|
44
|
+
#if defined(_WIN32)
|
|
45
|
+
ComPtr<ID3D11Device> device;
|
|
46
|
+
ComPtr<ID3D11DeviceContext> context;
|
|
47
|
+
#endif
|
|
48
|
+
};
|
|
49
|
+
namespace {
|
|
50
|
+
void copy_error(char* output, size_t size, const char* message) {
|
|
51
|
+
if (output && size) { std::strncpy(output, message, size - 1); output[size - 1] = 0; }
|
|
52
|
+
}
|
|
53
|
+
bool set_text(Packet& packet, const char* text) {
|
|
54
|
+
if (!text || std::strlen(text) >= sizeof(packet.text)) return false;
|
|
55
|
+
std::strcpy(packet.text, text); return true;
|
|
56
|
+
}
|
|
57
|
+
#if defined(__APPLE__)
|
|
58
|
+
void set_number(CFMutableDictionaryRef dictionary, CFStringRef key, int64_t value) {
|
|
59
|
+
auto number = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, &value);
|
|
60
|
+
CFDictionarySetValue(dictionary, key, number); CFRelease(number);
|
|
61
|
+
}
|
|
62
|
+
#endif
|
|
63
|
+
}
|
|
64
|
+
extern "C" {
|
|
65
|
+
dsh_frame_forwarding_client* dsh_frame_forwarding_connect(const char* endpoint_text, char* error, size_t error_size) {
|
|
66
|
+
return dsh_frame_forwarding_connect_with_device(endpoint_text, nullptr, error, error_size);
|
|
67
|
+
}
|
|
68
|
+
static dsh_frame_forwarding_client* connect_impl(const char* endpoint_text, void* device,
|
|
69
|
+
const dsh_frame_forwarding_connect_options* options, char* error, size_t error_size) {
|
|
70
|
+
try {
|
|
71
|
+
if (!endpoint_text) throw std::runtime_error("Missing native render endpoint");
|
|
72
|
+
auto endpoint = parse_endpoint(endpoint_text);
|
|
73
|
+
auto client = std::make_unique<dsh_frame_forwarding_client>();
|
|
74
|
+
const auto started = std::chrono::steady_clock::now();
|
|
75
|
+
const auto cancelled = [&] {
|
|
76
|
+
return options && ((options->cancelled && options->cancelled(options->cancellation_context)) ||
|
|
77
|
+
std::chrono::steady_clock::now() - started >= std::chrono::milliseconds(options->timeout_ms));
|
|
78
|
+
};
|
|
79
|
+
client->channel = connect_channel(endpoint, options ? std::function<bool()>(cancelled) : std::function<bool()>{});
|
|
80
|
+
Packet packet; packet.kind = hello; std::strcpy(packet.token, endpoint.token.c_str());
|
|
81
|
+
#if defined(_WIN32)
|
|
82
|
+
if (device) {
|
|
83
|
+
client->device = static_cast<ID3D11Device*>(device);
|
|
84
|
+
client->device->GetImmediateContext(&client->context);
|
|
85
|
+
} else if (FAILED(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, D3D11_CREATE_DEVICE_BGRA_SUPPORT,
|
|
86
|
+
nullptr, 0, D3D11_SDK_VERSION, &client->device, nullptr, &client->context)))
|
|
87
|
+
throw std::runtime_error("D3D11 hardware device unavailable");
|
|
88
|
+
set_text(packet, device_adapter(client->device.Get()).c_str());
|
|
89
|
+
packet.max_frames = hello_keyed_mutex;
|
|
90
|
+
#else
|
|
91
|
+
if (device) throw std::runtime_error("IOSurface producer does not accept a graphics device");
|
|
92
|
+
#endif
|
|
93
|
+
if (!client->channel->send(packet)) throw std::runtime_error("Cannot send native render handshake");
|
|
94
|
+
Received received;
|
|
95
|
+
int result;
|
|
96
|
+
do {
|
|
97
|
+
if (cancelled()) throw std::runtime_error("Native render connection cancelled or timed out");
|
|
98
|
+
result = client->channel->receive(received, 100);
|
|
99
|
+
} while (result == 0);
|
|
100
|
+
if (result < 0 || received.packet.marker != magic || received.packet.version != DSH_FRAME_FORWARDING_PROTOCOL_VERSION || received.packet.kind != ready ||
|
|
101
|
+
!received.packet.max_frames || !received.packet.max_bytes)
|
|
102
|
+
throw std::runtime_error("Native render receiver rejected handshake");
|
|
103
|
+
client->max_frames = received.packet.max_frames; client->max_bytes = received.packet.max_bytes;
|
|
104
|
+
return client.release();
|
|
105
|
+
} catch (const std::exception& exception) { copy_error(error, error_size, exception.what()); return nullptr; }
|
|
106
|
+
}
|
|
107
|
+
dsh_frame_forwarding_client* dsh_frame_forwarding_connect_with_device(const char* endpoint, void* device, char* error, size_t error_size) {
|
|
108
|
+
return connect_impl(endpoint, device, nullptr, error, error_size);
|
|
109
|
+
}
|
|
110
|
+
dsh_frame_forwarding_client* dsh_frame_forwarding_connect_with_options(const char* endpoint,
|
|
111
|
+
const dsh_frame_forwarding_connect_options* options, char* error, size_t error_size) {
|
|
112
|
+
if (!options || !options->timeout_ms) { copy_error(error, error_size, "Connection requires a positive timeout"); return nullptr; }
|
|
113
|
+
return connect_impl(endpoint, options->device, options, error, error_size);
|
|
114
|
+
}
|
|
115
|
+
dsh_frame_forwarding_surface* dsh_frame_forwarding_acquire(dsh_frame_forwarding_client* client, uint32_t width, uint32_t height) {
|
|
116
|
+
if (!client || client->stopped) return nullptr;
|
|
117
|
+
std::lock_guard<std::mutex> lock(client->pool_mutex);
|
|
118
|
+
if (!width || !height || width > 16384 || height > 16384) { client->error = "Native render dimensions outside supported range"; return nullptr; }
|
|
119
|
+
dsh_frame_forwarding_surface* granted_surface = nullptr;
|
|
120
|
+
for (auto& surface : client->surfaces) {
|
|
121
|
+
if (surface->width != width || surface->height != height) continue;
|
|
122
|
+
if (surface->state == dsh_frame_forwarding_surface::available) {
|
|
123
|
+
surface->state = dsh_frame_forwarding_surface::acquired; return surface.get();
|
|
124
|
+
}
|
|
125
|
+
if (surface->state == dsh_frame_forwarding_surface::pending) return nullptr;
|
|
126
|
+
if (surface->state == dsh_frame_forwarding_surface::granted) { granted_surface = surface.get(); break; }
|
|
127
|
+
}
|
|
128
|
+
if (!granted_surface) {
|
|
129
|
+
client->surfaces.erase(std::remove_if(client->surfaces.begin(), client->surfaces.end(), [&](auto& surface) {
|
|
130
|
+
if (surface->state == dsh_frame_forwarding_surface::acquired || surface->state == dsh_frame_forwarding_surface::submitted) return false;
|
|
131
|
+
Packet packet; packet.kind = deallocate; packet.allocation_id = surface->allocation_id;
|
|
132
|
+
client->channel->send(packet); return true;
|
|
133
|
+
}), client->surfaces.end());
|
|
134
|
+
uint64_t allocated_bytes = 0;
|
|
135
|
+
for (auto& surface : client->surfaces) allocated_bytes += surface->bytes;
|
|
136
|
+
#if defined(__APPLE__)
|
|
137
|
+
auto row_bytes = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, static_cast<size_t>(width) * 4);
|
|
138
|
+
uint64_t required_bytes = IOSurfaceAlignProperty(kIOSurfaceAllocSize, row_bytes * height);
|
|
139
|
+
const uint64_t page_bytes = static_cast<uint64_t>(getpagesize());
|
|
140
|
+
required_bytes = (required_bytes + page_bytes - 1) / page_bytes * page_bytes;
|
|
141
|
+
#else
|
|
142
|
+
uint64_t required_bytes = static_cast<uint64_t>(width) * height * 4;
|
|
143
|
+
#endif
|
|
144
|
+
if (required_bytes > client->max_bytes) { client->error = "Native render texture exceeds view byte budget"; return nullptr; }
|
|
145
|
+
if (client->surfaces.size() >= client->max_frames || allocated_bytes + required_bytes > client->max_bytes) return nullptr;
|
|
146
|
+
auto pending_surface = std::make_unique<dsh_frame_forwarding_surface>();
|
|
147
|
+
pending_surface->width = width; pending_surface->height = height; pending_surface->owner = client;
|
|
148
|
+
pending_surface->bytes = required_bytes; pending_surface->allocation_id = client->next_allocation++;
|
|
149
|
+
Packet request; request.kind = allocate; request.allocation_id = pending_surface->allocation_id; request.max_bytes = required_bytes;
|
|
150
|
+
if (!client->channel->send(request)) { client->error = "Native render allocation channel closed"; return nullptr; }
|
|
151
|
+
client->surfaces.push_back(std::move(pending_surface)); return nullptr;
|
|
152
|
+
}
|
|
153
|
+
auto surface = granted_surface;
|
|
154
|
+
#if defined(__APPLE__)
|
|
155
|
+
auto properties = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
|
|
156
|
+
set_number(properties, kIOSurfaceWidth, width); set_number(properties, kIOSurfaceHeight, height);
|
|
157
|
+
set_number(properties, kIOSurfaceAllocSize, surface->bytes);
|
|
158
|
+
set_number(properties, kIOSurfaceBytesPerElement, 4); set_number(properties, kIOSurfacePixelFormat, 0x42475241);
|
|
159
|
+
surface->surface = IOSurfaceCreate(properties); CFRelease(properties);
|
|
160
|
+
if (!surface->surface) { client->error = "IOSurface allocation failed"; return nullptr; }
|
|
161
|
+
surface->stride = static_cast<uint32_t>(IOSurfaceGetBytesPerRow(surface->surface));
|
|
162
|
+
surface->bytes = IOSurfaceGetAllocSize(surface->surface);
|
|
163
|
+
#else
|
|
164
|
+
D3D11_TEXTURE2D_DESC desc{};
|
|
165
|
+
desc.Width = width; desc.Height = height; desc.MipLevels = 1; desc.ArraySize = 1;
|
|
166
|
+
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; desc.SampleDesc.Count = 1;
|
|
167
|
+
desc.Usage = D3D11_USAGE_DEFAULT; desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
|
|
168
|
+
desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
|
|
169
|
+
if (FAILED(client->device->CreateTexture2D(&desc, nullptr, &surface->texture))) { client->error = "D3D11 shared texture allocation failed"; return nullptr; }
|
|
170
|
+
if (FAILED(surface->texture.As(&surface->keyed_mutex))) { client->error = "DXGI producer keyed mutex unavailable"; return nullptr; }
|
|
171
|
+
ComPtr<IDXGIResource1> shared;
|
|
172
|
+
if (FAILED(surface->texture.As(&shared)) || FAILED(shared->CreateSharedHandle(nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr, &surface->handle))) { client->error = "DXGI shared handle creation failed"; return nullptr; }
|
|
173
|
+
surface->stride = width * 4; surface->bytes = static_cast<uint64_t>(width) * height * 4;
|
|
174
|
+
#endif
|
|
175
|
+
surface->state = dsh_frame_forwarding_surface::acquired;
|
|
176
|
+
return surface;
|
|
177
|
+
}
|
|
178
|
+
void* dsh_frame_forwarding_surface_native(dsh_frame_forwarding_surface* surface) {
|
|
179
|
+
if (!surface) return nullptr;
|
|
180
|
+
#if defined(__APPLE__)
|
|
181
|
+
return surface->surface;
|
|
182
|
+
#else
|
|
183
|
+
return surface->texture.Get();
|
|
184
|
+
#endif
|
|
185
|
+
}
|
|
186
|
+
void* dsh_frame_forwarding_surface_shared_handle(dsh_frame_forwarding_surface* surface) {
|
|
187
|
+
#if defined(_WIN32)
|
|
188
|
+
return surface && surface->state == dsh_frame_forwarding_surface::acquired ? surface->handle : nullptr;
|
|
189
|
+
#else
|
|
190
|
+
(void)surface; return nullptr;
|
|
191
|
+
#endif
|
|
192
|
+
}
|
|
193
|
+
void* dsh_frame_forwarding_device(dsh_frame_forwarding_client* client) {
|
|
194
|
+
#if defined(_WIN32)
|
|
195
|
+
return client ? client->device.Get() : nullptr;
|
|
196
|
+
#else
|
|
197
|
+
(void)client; return nullptr;
|
|
198
|
+
#endif
|
|
199
|
+
}
|
|
200
|
+
void* dsh_frame_forwarding_surface_keyed_mutex(dsh_frame_forwarding_surface* surface) {
|
|
201
|
+
#if defined(_WIN32)
|
|
202
|
+
return surface && surface->state == dsh_frame_forwarding_surface::acquired ? surface->keyed_mutex.Get() : nullptr;
|
|
203
|
+
#else
|
|
204
|
+
(void)surface; return nullptr;
|
|
205
|
+
#endif
|
|
206
|
+
}
|
|
207
|
+
static int write_bgra(dsh_frame_forwarding_surface* surface, const void* pixels, size_t stride, uint32_t timeout_ms) {
|
|
208
|
+
if (!surface || !pixels || stride < static_cast<size_t>(surface->width) * 4 || stride > UINT32_MAX) return 0;
|
|
209
|
+
auto client = surface->owner; std::lock_guard<std::mutex> lock(client->pool_mutex);
|
|
210
|
+
if (surface->state != dsh_frame_forwarding_surface::acquired || client->stopped) return 0;
|
|
211
|
+
#if defined(__APPLE__)
|
|
212
|
+
(void)timeout_ms;
|
|
213
|
+
if (IOSurfaceLock(surface->surface, 0, nullptr) != kIOReturnSuccess) return 0;
|
|
214
|
+
auto target = static_cast<unsigned char*>(IOSurfaceGetBaseAddress(surface->surface));
|
|
215
|
+
for (uint32_t row = 0; row < surface->height; ++row)
|
|
216
|
+
std::memcpy(target + static_cast<size_t>(row) * surface->stride, static_cast<const unsigned char*>(pixels) + static_cast<size_t>(row) * stride, surface->width * 4);
|
|
217
|
+
return IOSurfaceUnlock(surface->surface, 0, nullptr) == kIOReturnSuccess;
|
|
218
|
+
#else
|
|
219
|
+
const auto acquired = surface->keyed_mutex->AcquireSync(0, timeout_ms);
|
|
220
|
+
if (acquired != S_OK) { client->error = "Producer write keyed mutex acquisition failed or timed out"; return 0; }
|
|
221
|
+
client->context->UpdateSubresource(surface->texture.Get(), 0, nullptr, pixels, static_cast<UINT>(stride), 0);
|
|
222
|
+
const auto released = surface->keyed_mutex->ReleaseSync(0);
|
|
223
|
+
client->context->Flush();
|
|
224
|
+
if (released != S_OK) { client->error = "Producer write keyed mutex release failed"; return 0; }
|
|
225
|
+
return 1;
|
|
226
|
+
#endif
|
|
227
|
+
}
|
|
228
|
+
int dsh_frame_forwarding_write_bgra(dsh_frame_forwarding_surface* surface, const void* pixels, size_t stride) {
|
|
229
|
+
return write_bgra(surface, pixels, stride, 1000);
|
|
230
|
+
}
|
|
231
|
+
int dsh_frame_forwarding_write_bgra_with_timeout(dsh_frame_forwarding_surface* surface, const void* pixels, size_t stride, uint32_t timeout_ms) {
|
|
232
|
+
return timeout_ms ? write_bgra(surface, pixels, stride, timeout_ms) : 0;
|
|
233
|
+
}
|
|
234
|
+
int dsh_frame_forwarding_submit(dsh_frame_forwarding_client* client, dsh_frame_forwarding_surface* surface, uint64_t frame_id, const char* metadata_json) {
|
|
235
|
+
if (!client || !surface || surface->owner != client || !frame_id || frame_id > 9007199254740991ULL || client->stopped) return 0;
|
|
236
|
+
Packet packet; packet.kind = frame;
|
|
237
|
+
if (!set_text(packet, metadata_json)) return 0;
|
|
238
|
+
std::lock_guard<std::mutex> lock(client->pool_mutex);
|
|
239
|
+
if (surface->state != dsh_frame_forwarding_surface::acquired) return 0;
|
|
240
|
+
for (auto& other : client->surfaces) if (other->state == dsh_frame_forwarding_surface::submitted && other->frame_id == frame_id) return 0;
|
|
241
|
+
packet.width = surface->width; packet.height = surface->height; packet.stride = surface->stride;
|
|
242
|
+
packet.frame_id = frame_id; packet.allocation_id = surface->allocation_id;
|
|
243
|
+
#if defined(__APPLE__)
|
|
244
|
+
auto resource = reinterpret_cast<uintptr_t>(surface->surface);
|
|
245
|
+
#else
|
|
246
|
+
auto resource = reinterpret_cast<uintptr_t>(surface->handle);
|
|
247
|
+
#endif
|
|
248
|
+
if (!client->channel->send(packet, resource)) return 0;
|
|
249
|
+
surface->frame_id = frame_id; surface->state = dsh_frame_forwarding_surface::submitted; return 1;
|
|
250
|
+
}
|
|
251
|
+
void dsh_frame_forwarding_discard(dsh_frame_forwarding_client* client, dsh_frame_forwarding_surface* surface) {
|
|
252
|
+
if (!client || !surface || surface->owner != client) return;
|
|
253
|
+
std::lock_guard<std::mutex> lock(client->pool_mutex);
|
|
254
|
+
if (surface->state == dsh_frame_forwarding_surface::acquired) surface->state = dsh_frame_forwarding_surface::available;
|
|
255
|
+
}
|
|
256
|
+
int dsh_frame_forwarding_poll_activity(dsh_frame_forwarding_client* client, uint32_t timeout, dsh_frame_forwarding_event* event, bool* received_packet) {
|
|
257
|
+
*received_packet = false;
|
|
258
|
+
if (!client || !event) return -1;
|
|
259
|
+
event->kind = 0; event->text[0] = 0;
|
|
260
|
+
{
|
|
261
|
+
std::lock_guard<std::mutex> lock(client->pool_mutex);
|
|
262
|
+
if (!client->error.empty()) { event->kind = -1; copy_error(event->text, sizeof(event->text), client->error.c_str()); return -1; }
|
|
263
|
+
}
|
|
264
|
+
Received received;
|
|
265
|
+
auto result = client->channel->receive(received, std::min(timeout, 100U));
|
|
266
|
+
if (received.resource) release_resource(received.resource);
|
|
267
|
+
if (result < 0 || client->stopped) { event->kind = -1; return -1; }
|
|
268
|
+
if (!result) return 0;
|
|
269
|
+
*received_packet = true;
|
|
270
|
+
if (received.packet.marker != magic || received.packet.version != DSH_FRAME_FORWARDING_PROTOCOL_VERSION) { event->kind = -1; return -1; }
|
|
271
|
+
if (received.packet.kind == release) {
|
|
272
|
+
std::lock_guard<std::mutex> lock(client->pool_mutex);
|
|
273
|
+
for (auto& surface : client->surfaces) {
|
|
274
|
+
if (surface->state == dsh_frame_forwarding_surface::submitted && surface->frame_id == received.packet.frame_id) {
|
|
275
|
+
surface->state = dsh_frame_forwarding_surface::available; break;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
} else if (received.packet.kind == allocated) {
|
|
279
|
+
std::lock_guard<std::mutex> lock(client->pool_mutex);
|
|
280
|
+
for (auto& surface : client->surfaces) {
|
|
281
|
+
if (surface->allocation_id == received.packet.allocation_id && surface->state == dsh_frame_forwarding_surface::pending) {
|
|
282
|
+
surface->state = dsh_frame_forwarding_surface::granted; break;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
} else if (received.packet.kind == message && std::memchr(received.packet.text, 0, sizeof(received.packet.text))) {
|
|
286
|
+
event->kind = 1; std::strcpy(event->text, received.packet.text);
|
|
287
|
+
} else if (received.packet.kind == goodbye) { client->stopped = true; event->kind = -1; return -1; }
|
|
288
|
+
else { event->kind = -1; return -1; }
|
|
289
|
+
return event->kind;
|
|
290
|
+
}
|
|
291
|
+
int dsh_frame_forwarding_poll(dsh_frame_forwarding_client* client, uint32_t timeout, dsh_frame_forwarding_event* event) {
|
|
292
|
+
bool received;
|
|
293
|
+
return dsh_frame_forwarding_poll_activity(client, timeout, event, &received);
|
|
294
|
+
}
|
|
295
|
+
int dsh_frame_forwarding_send(dsh_frame_forwarding_client* client, const char* json) {
|
|
296
|
+
if (!client || client->stopped) return 0;
|
|
297
|
+
Packet packet; packet.kind = message;
|
|
298
|
+
return set_text(packet, json) && client->channel->send(packet);
|
|
299
|
+
}
|
|
300
|
+
void dsh_frame_forwarding_interrupt(dsh_frame_forwarding_client* client) { if (client) { client->stopped = true; client->channel->interrupt(); } }
|
|
301
|
+
void dsh_frame_forwarding_close(dsh_frame_forwarding_client* client) {
|
|
302
|
+
if (!client) return;
|
|
303
|
+
Packet packet; packet.kind = goodbye; client->channel->send(packet);
|
|
304
|
+
dsh_frame_forwarding_interrupt(client); delete client;
|
|
305
|
+
}
|
|
306
|
+
}
|