-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy patheditor_window.rs
More file actions
301 lines (254 loc) · 9.85 KB
/
editor_window.rs
File metadata and controls
301 lines (254 loc) · 9.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use std::{collections::HashMap, ops::Deref, path::PathBuf, sync::Arc, time::Instant};
use tauri::{AppHandle, Manager, Runtime, Window, ipc::CommandArg};
use tokio::sync::{RwLock, watch};
use tokio_util::sync::CancellationToken;
use cap_rendering::GpuOutputFormat;
use crate::{
create_editor_instance_impl,
frame_ws::{WSFrame, WSFrameFormat, create_watch_frame_ws},
};
pub struct EditorInstance {
inner: Arc<cap_editor::EditorInstance>,
pub ws_port: u16,
pub ws_shutdown_token: CancellationToken,
}
type PendingResult = Result<Arc<EditorInstance>, String>;
type PendingReceiver = tokio::sync::watch::Receiver<Option<PendingResult>>;
#[derive(Clone, Default)]
pub struct PendingEditorInstances(Arc<RwLock<HashMap<String, PendingReceiver>>>);
async fn do_prewarm(app: AppHandle, path: PathBuf) -> PendingResult {
let (frame_tx, frame_rx) = watch::channel(None);
let (ws_port, ws_shutdown_token) = create_watch_frame_ws(frame_rx).await;
let inner = create_editor_instance_impl(
&app,
path,
Box::new(move |output| {
let ws_frame = match output {
cap_editor::EditorFrameOutput::Nv12(frame) => {
let ws_format = match frame.format {
GpuOutputFormat::Nv12 => WSFrameFormat::Nv12,
GpuOutputFormat::Rgba => WSFrameFormat::Rgba,
};
WSFrame {
data: frame.data,
width: frame.width,
height: frame.height,
stride: frame.y_stride,
frame_number: frame.frame_number,
target_time_ns: frame.target_time_ns,
format: ws_format,
created_at: Instant::now(),
}
}
cap_editor::EditorFrameOutput::Rgba(frame) => WSFrame {
data: frame.data,
width: frame.width,
height: frame.height,
stride: frame.padded_bytes_per_row,
frame_number: frame.frame_number,
target_time_ns: frame.target_time_ns,
format: WSFrameFormat::Rgba,
created_at: Instant::now(),
},
};
let _ = frame_tx.send(Some(std::sync::Arc::new(ws_frame)));
}),
)
.await?;
Ok(Arc::new(EditorInstance {
inner,
ws_port,
ws_shutdown_token,
}))
}
impl PendingEditorInstances {
pub fn get(app: &AppHandle) -> Self {
match app.try_state::<Self>() {
Some(s) => (*s).clone(),
None => {
let pending = Self::default();
app.manage(pending.clone());
pending
}
}
}
pub async fn start_prewarm(app: &AppHandle, window_label: String, path: PathBuf) {
let pending = Self::get(app);
let app = app.clone();
{
let instances = pending.0.read().await;
if instances.contains_key(&window_label) {
return;
}
}
let (tx, rx) = tokio::sync::watch::channel(None);
{
let mut instances = pending.0.write().await;
instances.insert(window_label.clone(), rx);
}
tokio::spawn(async move {
let result = do_prewarm(app, path).await;
tx.send(Some(result)).ok();
});
}
pub async fn take_prewarmed(&self, window_label: &str) -> Option<PendingReceiver> {
let mut instances = self.0.write().await;
instances.remove(window_label)
}
}
impl EditorInstance {
pub async fn dispose(&self) {
self.inner.dispose().await;
self.ws_shutdown_token.cancel();
}
}
impl Deref for EditorInstance {
type Target = Arc<cap_editor::EditorInstance>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
#[derive(Clone)]
pub struct EditorInstances(Arc<RwLock<HashMap<String, Arc<EditorInstance>>>>);
pub struct WindowEditorInstance(pub Arc<EditorInstance>);
impl specta::function::FunctionArg for WindowEditorInstance {
fn to_datatype(_: &mut specta::TypeMap) -> Option<specta::DataType> {
None
}
}
impl Deref for WindowEditorInstance {
type Target = Arc<EditorInstance>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl AsRef<EditorInstance> for WindowEditorInstance {
fn as_ref(&self) -> &EditorInstance {
&self.0
}
}
impl<'de, R: Runtime> CommandArg<'de, R> for WindowEditorInstance {
fn from_command(
command: tauri::ipc::CommandItem<'de, R>,
) -> Result<Self, tauri::ipc::InvokeError> {
let window = Window::from_command(command)?;
let instances = window.state::<EditorInstances>();
let instance = futures::executor::block_on(instances.0.read());
Ok(Self(instance.get(window.label()).cloned().unwrap()))
}
}
pub struct OptionalWindowEditorInstance(pub Option<Arc<EditorInstance>>);
impl specta::function::FunctionArg for OptionalWindowEditorInstance {
fn to_datatype(_: &mut specta::TypeMap) -> Option<specta::DataType> {
None
}
}
impl Deref for OptionalWindowEditorInstance {
type Target = Option<Arc<EditorInstance>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'de, R: Runtime> CommandArg<'de, R> for OptionalWindowEditorInstance {
fn from_command(
command: tauri::ipc::CommandItem<'de, R>,
) -> Result<Self, tauri::ipc::InvokeError> {
let Ok(window) = Window::from_command(command) else {
return Ok(Self(None));
};
let Some(instances) = window.try_state::<EditorInstances>() else {
return Ok(Self(None));
};
let instance = futures::executor::block_on(instances.0.read());
Ok(Self(instance.get(window.label()).cloned()))
}
}
impl EditorInstances {
pub async fn get_or_create(
window: &Window,
path: PathBuf,
) -> Result<Arc<EditorInstance>, String> {
let instances = match window.try_state::<EditorInstances>() {
Some(s) => (*s).clone(),
None => {
let instances = Self(Arc::new(RwLock::new(HashMap::new())));
window.manage(instances.clone());
instances
}
};
let mut instances = instances.0.write().await;
use std::collections::hash_map::Entry;
match instances.entry(window.label().to_string()) {
Entry::Vacant(entry) => {
let pending = PendingEditorInstances::get(window.app_handle());
if let Some(mut prewarmed_rx) = pending.take_prewarmed(window.label()).await {
loop {
if let Some(result) = prewarmed_rx.borrow_and_update().clone() {
let instance = result?;
entry.insert(instance.clone());
return Ok(instance);
}
if prewarmed_rx.changed().await.is_err() {
break;
}
}
}
let (frame_tx, frame_rx) = watch::channel(None);
let (ws_port, ws_shutdown_token) = create_watch_frame_ws(frame_rx).await;
let inner = create_editor_instance_impl(
window.app_handle(),
path,
Box::new(move |output| {
let ws_frame = match output {
cap_editor::EditorFrameOutput::Nv12(frame) => {
let ws_format = match frame.format {
GpuOutputFormat::Nv12 => WSFrameFormat::Nv12,
GpuOutputFormat::Rgba => WSFrameFormat::Rgba,
};
WSFrame {
data: frame.data,
width: frame.width,
height: frame.height,
stride: frame.y_stride,
frame_number: frame.frame_number,
target_time_ns: frame.target_time_ns,
format: ws_format,
created_at: Instant::now(),
}
}
cap_editor::EditorFrameOutput::Rgba(frame) => WSFrame {
data: frame.data,
width: frame.width,
height: frame.height,
stride: frame.padded_bytes_per_row,
frame_number: frame.frame_number,
target_time_ns: frame.target_time_ns,
format: WSFrameFormat::Rgba,
created_at: Instant::now(),
},
};
let _ = frame_tx.send(Some(std::sync::Arc::new(ws_frame)));
}),
)
.await?;
let instance = Arc::new(EditorInstance {
inner,
ws_port,
ws_shutdown_token,
});
entry.insert(instance.clone());
Ok(instance)
}
Entry::Occupied(entry) => Ok(entry.get().clone()),
}
}
pub async fn remove(window: Window) {
let Some(instances) = window.try_state::<EditorInstances>() else {
return;
};
let mut instances = instances.0.write().await;
if let Some(instance) = instances.remove(window.label()) {
instance.dispose().await;
}
}
}