runmat-plot 0.7.1

GPU-accelerated and static plotting for RunMat with WGPU and Plotters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Main-thread owner of native windows and their post-present frame ports.
//!
//! Only metadata, figures, and Send frame tickets cross into this loop. A
//! RunMat session, RuntimeContext, callable, or GC root never does.

use super::frame_command::{FrameCommand, FrameError, FramePort};
use super::lifecycle::{self, frame_ports};
use super::{PlotWindow, WindowConfig};
use futures::channel::oneshot;
use runmat_time::Instant;
use std::collections::HashMap;
use std::future::Future;
use std::num::NonZeroUsize;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
use std::sync::mpsc::{self, Receiver, SyncSender, TryRecvError, TrySendError};
use std::sync::{Arc, Weak};
use std::task::{Context, Poll};
use winit::application::ApplicationHandler;
use winit::dpi::PhysicalSize;
use winit::event::{ElementState, Event, WindowEvent};
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopProxy};
use winit::keyboard::{KeyCode, ModifiersState, PhysicalKey};
use winit::window::{Window, WindowId};

const QUEUED: u8 = 0;
const RETIRED: u8 = 1;
const DISPATCHED: u8 = 2;
const OPENED: u8 = 3;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WindowOpenDisposition {
    NotOpened,
    Indeterminate,
    Opened,
}

#[derive(Debug, thiserror::Error)]
pub enum WindowOpenError {
    #[error("native window-open queue is full")]
    Full,
    #[error("native window host is closed")]
    Closed,
    #[error("a zero figure handle cannot own a native window")]
    InvalidHandle,
    #[error("native window capacity is exhausted")]
    Capacity,
    #[error("native figure-window route admission failed: {0}")]
    Routing(String),
    #[error("native window creation failed: {0}")]
    Create(String),
}

struct OpenRequest {
    figure_handle: u32,
    config: WindowConfig,
    response: oneshot::Sender<Result<WindowOpenReceipt, WindowOpenError>>,
    state: Arc<AtomicU8>,
}

/// Send-only opener; polling the returned ticket admits a window request.
#[derive(Clone)]
pub struct WindowOpenPort {
    sender: SyncSender<OpenRequest>,
    shutdown: Arc<AtomicBool>,
    host_lifetime: Weak<()>,
}

impl WindowOpenPort {
    /// Descriptive liveness for admission, not a reservation against later close.
    pub fn is_closed(&self) -> bool {
        self.shutdown.load(Ordering::Acquire) || self.host_lifetime.upgrade().is_none()
    }
    pub fn open(&self, figure_handle: u32, config: WindowConfig) -> WindowOpenTicket {
        let (response, receiver) = oneshot::channel();
        let state = Arc::new(AtomicU8::new(QUEUED));
        WindowOpenTicket {
            sender: self.sender.clone(),
            request: Some(OpenRequest {
                figure_handle,
                config,
                response,
                state: Arc::clone(&state),
            }),
            receiver,
            state,
        }
    }

    /// Signals the OS-main-thread loop to retire its windows. This is not a
    /// callback or session cancellation mechanism.
    pub fn shutdown(&self) {
        self.shutdown.store(true, Ordering::Release);
    }
}

/// Dropping a queued open ticket atomically excludes later window creation.
pub struct WindowOpenTicket {
    sender: SyncSender<OpenRequest>,
    request: Option<OpenRequest>,
    receiver: oneshot::Receiver<Result<WindowOpenReceipt, WindowOpenError>>,
    state: Arc<AtomicU8>,
}

impl WindowOpenTicket {
    pub fn disposition(&self) -> WindowOpenDisposition {
        match self.state.load(Ordering::Acquire) {
            QUEUED | RETIRED => WindowOpenDisposition::NotOpened,
            DISPATCHED => WindowOpenDisposition::Indeterminate,
            OPENED => WindowOpenDisposition::Opened,
            _ => unreachable!("native window-open state"),
        }
    }

    pub fn abandon(&self) -> WindowOpenDisposition {
        let state =
            match self
                .state
                .compare_exchange(QUEUED, RETIRED, Ordering::AcqRel, Ordering::Acquire)
            {
                Ok(_) => RETIRED,
                Err(state) => state,
            };
        match state {
            QUEUED | RETIRED => WindowOpenDisposition::NotOpened,
            DISPATCHED => WindowOpenDisposition::Indeterminate,
            OPENED => WindowOpenDisposition::Opened,
            _ => unreachable!("native window-open state"),
        }
    }
}

impl Future for WindowOpenTicket {
    type Output = Result<WindowOpenReceipt, WindowOpenError>;

    fn poll(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
        if let Some(request) = self.request.take() {
            if self.state.load(Ordering::Acquire) == RETIRED {
                return Poll::Ready(Err(WindowOpenError::Closed));
            }
            if request.figure_handle == 0 {
                self.abandon();
                return Poll::Ready(Err(WindowOpenError::InvalidHandle));
            }
            match self.sender.try_send(request) {
                Ok(()) => {}
                Err(TrySendError::Full(_)) => {
                    self.abandon();
                    return Poll::Ready(Err(WindowOpenError::Full));
                }
                Err(TrySendError::Disconnected(_)) => {
                    self.abandon();
                    return Poll::Ready(Err(WindowOpenError::Closed));
                }
            }
        }
        match Pin::new(&mut self.receiver).poll(context) {
            Poll::Ready(Ok(result)) => Poll::Ready(result),
            Poll::Ready(Err(_)) => Poll::Ready(Err(WindowOpenError::Closed)),
            Poll::Pending => Poll::Pending,
        }
    }
}

impl Drop for WindowOpenTicket {
    fn drop(&mut self) {
        self.abandon();
    }
}

/// Linear window lifetime authority. Routing-table clones do not retain it.
/// Even an acknowledged open dropped before its receiver polls closes only
/// this window, including the dispatched-open/cancellation race.
pub struct WindowOpenReceipt {
    port: FramePort,
}

impl WindowOpenReceipt {
    pub fn frame_port(&self) -> &FramePort {
        &self.port
    }
}

impl Drop for WindowOpenReceipt {
    fn drop(&mut self) {
        self.port.close();
    }
}

struct HostedWindow {
    window: PlotWindow<'static>,
    last_render_time: Instant,
    modifiers: ModifiersState,
    _frame_registration: frame_ports::FramePortRegistration,
    _lifecycle_registration: lifecycle::WindowRegistration,
}

/// Create and run this owner on the OS main thread. The execution session is
/// constructed separately on its own current-thread lane after `open_port`
/// has been transferred; no session-local Rc may be moved into this owner.
pub struct NativeGraphicsHost {
    event_loop: EventLoop<FrameCommand>,
    opens: Receiver<OpenRequest>,
    sender: SyncSender<OpenRequest>,
    shutdown: Arc<AtomicBool>,
    maximum_windows: NonZeroUsize,
    host_lifetime: Arc<()>,
}

impl NativeGraphicsHost {
    pub fn new(
        maximum_pending_opens: NonZeroUsize,
        maximum_windows: NonZeroUsize,
    ) -> Result<Self, WindowOpenError> {
        let event_loop = EventLoop::<FrameCommand>::with_user_event()
            .build()
            .map_err(|error| WindowOpenError::Create(error.to_string()))?;
        let (sender, opens) = mpsc::sync_channel(maximum_pending_opens.get());
        Ok(Self {
            event_loop,
            opens,
            sender,
            shutdown: Arc::new(AtomicBool::new(false)),
            maximum_windows,
            host_lifetime: Arc::new(()),
        })
    }

    pub fn open_port(&self) -> WindowOpenPort {
        WindowOpenPort {
            sender: self.sender.clone(),
            shutdown: Arc::clone(&self.shutdown),
            host_lifetime: Arc::downgrade(&self.host_lifetime),
        }
    }

    pub fn run(self) -> Result<(), WindowOpenError> {
        let Self {
            event_loop,
            opens,
            sender,
            shutdown,
            maximum_windows,
            host_lifetime,
        } = self;
        // The host is a receiver, not its own producer. Releasing this clone
        // lets worker-lane retirement terminate the loop if no opener remains.
        drop(sender);
        let mut owner = HostApplication {
            opens,
            proxy: event_loop.create_proxy(),
            shutdown,
            maximum_windows: maximum_windows.get(),
            windows: HashMap::new(),
            opens_disconnected: false,
            _host_lifetime: host_lifetime,
        };
        event_loop
            .run_app(&mut owner)
            .map_err(|error| WindowOpenError::Create(error.to_string()))
    }
}

struct HostApplication {
    opens: Receiver<OpenRequest>,
    proxy: EventLoopProxy<FrameCommand>,
    shutdown: Arc<AtomicBool>,
    maximum_windows: usize,
    windows: HashMap<WindowId, HostedWindow>,
    opens_disconnected: bool,
    _host_lifetime: Arc<()>,
}

impl ApplicationHandler<FrameCommand> for HostApplication {
    fn resumed(&mut self, _target: &ActiveEventLoop) {}

    fn user_event(&mut self, _target: &ActiveEventLoop, command: FrameCommand) {
        let Some(window_id) = command.window_id() else {
            return;
        };
        if self
            .windows
            .get(&window_id)
            .is_some_and(HostedWindow::close_requested)
        {
            self.windows.remove(&window_id);
            return;
        }
        let Some(hosted) = self.windows.get_mut(&window_id) else {
            return;
        };
        let Some(lifetime) = hosted._lifecycle_registration.signal() else {
            return;
        };
        if !command.matches_window_lifetime(&lifetime) {
            return;
        }
        let Some(mut frame) = command.begin() else {
            return;
        };
        let figure = frame.take_figure();
        let result = hosted
            .window
            .set_figure(figure)
            .map_err(|error| FrameError::Prepare(error.to_string()))
            .and_then(|()| {
                hosted
                    .window
                    .render(std::time::Duration::ZERO)
                    .map_err(|error| FrameError::Render(error.to_string()))
            });
        // render() returns only after output.present(); mere delivery is not a receipt.
        frame.complete(result);
    }

    fn window_event(&mut self, target: &ActiveEventLoop, window_id: WindowId, event: WindowEvent) {
        if self
            .windows
            .get(&window_id)
            .is_some_and(HostedWindow::close_requested)
        {
            self.windows.remove(&window_id);
            return;
        }
        let Some(hosted) = self.windows.get_mut(&window_id) else {
            return;
        };
        let close = match &event {
            WindowEvent::CloseRequested => true,
            WindowEvent::KeyboardInput { event, .. } if event.state == ElementState::Pressed => {
                matches!(event.physical_key, PhysicalKey::Code(KeyCode::Escape))
                    || (matches!(event.physical_key, PhysicalKey::Code(KeyCode::KeyQ))
                        && (hosted.modifiers.super_key() || hosted.modifiers.control_key()))
            }
            _ => false,
        };
        if close {
            self.windows.remove(&window_id);
            return;
        }
        hosted.window.handle_event(
            Event::WindowEvent { window_id, event },
            target,
            &mut hosted.last_render_time,
            &None,
            &mut hosted.modifiers,
        );
    }

    fn about_to_wait(&mut self, target: &ActiveEventLoop) {
        if self.shutdown.load(Ordering::Acquire) {
            target.exit();
            return;
        }
        // Retire exact old window routes before admitting an explicitly
        // requested replacement at a reused figure coordinate.
        self.windows.retain(|_, hosted| !hosted.close_requested());
        loop {
            match self.opens.try_recv() {
                Ok(request) => open_window(
                    request,
                    target,
                    &self.proxy,
                    &mut self.windows,
                    self.maximum_windows,
                ),
                Err(TryRecvError::Empty) => break,
                Err(TryRecvError::Disconnected) => {
                    self.opens_disconnected = true;
                    break;
                }
            }
        }
        for hosted in self.windows.values_mut() {
            hosted.window.handle_event(
                Event::AboutToWait,
                target,
                &mut hosted.last_render_time,
                &None,
                &mut hosted.modifiers,
            );
        }
        if self.windows.is_empty() && self.opens_disconnected {
            target.exit();
        } else {
            // Open ingress is a bounded channel, not a winit event source.
            target.set_control_flow(ControlFlow::WaitUntil(
                std::time::Instant::now() + std::time::Duration::from_millis(16),
            ));
        }
    }
}

impl HostedWindow {
    fn close_requested(&self) -> bool {
        self._lifecycle_registration
            .signal()
            .is_some_and(|signal| signal.load(Ordering::Acquire))
    }
}

fn open_window(
    request: OpenRequest,
    target: &ActiveEventLoop,
    proxy: &EventLoopProxy<FrameCommand>,
    windows: &mut HashMap<WindowId, HostedWindow>,
    maximum_windows: usize,
) {
    if windows.len() >= maximum_windows {
        reject_unpublished_open(request, WindowOpenError::Capacity);
        return;
    }
    let mut frame_registration = match frame_ports::reserve(request.figure_handle) {
        Ok(registration) => registration,
        Err(error) => {
            reject_unpublished_open(request, WindowOpenError::Routing(error));
            return;
        }
    };
    let lifecycle_registration = match lifecycle::register_handle(request.figure_handle) {
        Ok(registration) => registration,
        Err(error) => {
            reject_unpublished_open(request, WindowOpenError::Routing(error));
            return;
        }
    };
    if request
        .state
        .compare_exchange(QUEUED, DISPATCHED, Ordering::AcqRel, Ordering::Acquire)
        .is_err()
    {
        return;
    }
    let result = (|| {
        let attributes = Window::default_attributes()
            .with_title(&request.config.title)
            .with_inner_size(PhysicalSize::new(
                request.config.width,
                request.config.height,
            ))
            .with_resizable(request.config.resizable)
            .with_maximized(request.config.maximized);
        let native_window = Arc::new(
            target
                .create_window(attributes)
                .map_err(|error| WindowOpenError::Create(error.to_string()))?,
        );
        let mut window =
            pollster::block_on(PlotWindow::new_with_window(request.config, native_window))
                .map_err(|error| WindowOpenError::Create(error.to_string()))?;
        let close_signal = lifecycle_registration
            .signal()
            .expect("nonzero registered window");
        let port = FramePort::new(proxy.clone(), window.window.id(), close_signal);
        frame_registration
            .activate(port.clone())
            .map_err(WindowOpenError::Routing)?;
        if let Some(signal) = lifecycle_registration.signal() {
            window.install_close_signal(signal);
        }
        Ok((
            port,
            HostedWindow {
                window,
                last_render_time: Instant::now(),
                modifiers: ModifiersState::default(),
                _frame_registration: frame_registration,
                _lifecycle_registration: lifecycle_registration,
            },
        ))
    })();
    match result {
        Ok((port, hosted)) => {
            request.state.store(OPENED, Ordering::Release);
            if request
                .response
                .send(Ok(WindowOpenReceipt { port }))
                .is_ok()
            {
                windows.insert(hosted.window.window.id(), hosted);
            }
        }
        Err(error) => {
            let _ = request.response.send(Err(error));
        }
    }
}

fn reject_unpublished_open(request: OpenRequest, error: WindowOpenError) {
    if request
        .state
        .compare_exchange(QUEUED, RETIRED, Ordering::AcqRel, Ordering::Acquire)
        .is_ok()
    {
        let _ = request.response.send(Err(error));
    }
}

#[cfg(test)]
#[path = "host_loop/tests.rs"]
mod tests;