vtome 0.3.5

Show images and video on a specific monitor — or a specific quadrilateral of one — with royalty-free codecs and no FFmpeg. GPU presentation with perspective-correct corner pinning, optional windowing, and a frame pipeline that stays in YUV until the shader.
Documentation
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//! Windows of vtome's own, on the monitor you asked for.
//!
//! This is the optional half. An application that already has a window — a
//! Tauri front end, a game engine — takes the `render` feature and hands
//! [`Renderer`](crate::render::Renderer) a surface of its own; nothing here is
//! compiled in that case, and neither is winit.
//!
//! ```no_run
//! use vtome::{MonitorSelector, Placement};
//! use vtome::geometry::{Quad, Rect};
//! use vtome::window::Viewer;
//!
//! let frame = vtome::load_image("poster.png")?;
//!
//! // The projector, keystoned because it is aimed upwards at the wall.
//! let placement = Placement::new(MonitorSelector::Name("EPSON".into()))
//!     .corners(Quad::keystone(Rect::from_size(1920.0, 1080.0), 180.0));
//!
//! Viewer::new(frame, placement).show()?;
//! # Ok::<(), vtome::Error>(())
//! ```
//!
//! A film is the same call with a [`crate::VideoSource`], and
//! shows up as an overlay — above other applications and, if asked, letting
//! the mouse through to them:
//!
//! ```no_run
//! # #[cfg(feature = "demux")] {
//! use vtome::{MonitorSelector, Placement, VideoSource};
//! use vtome::geometry::Rect;
//! use vtome::window::Viewer;
//!
//! let source = VideoSource::from_file("clip.mp4")?;
//! let placement = Placement::new(MonitorSelector::Primary)
//!     .area(Rect::new(40.0, 40.0, 640.0, 360.0))
//!     .always_on_top(true);
//!
//! Viewer::video(source, placement).click_through(true).show()?;
//! # }
//! # Ok::<(), vtome::Error>(())
//! ```
//!
//! # Mobile has no monitors
//!
//! iOS and Android give an application one surface and no desktop to place it
//! on. The placement still resolves — it reports a single monitor the size of
//! that surface — so the same code runs, but "the second screen" means nothing
//! there. See `planning/TODO.md` §11.

use std::sync::Arc;

use winit::application::ApplicationHandler;
use winit::event::{ElementState, KeyEvent, WindowEvent};
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
use winit::window::{Window, WindowId, WindowLevel};

use crate::error::{Error, Result};
use crate::frame::Frame;
use crate::geometry::Rect;
use crate::placement::{monitor_id, Monitor, Placement};
#[cfg(feature = "demux")]
use crate::playback::{Playback, Tick};
use crate::render::{Gpu, Renderer};
#[cfg(feature = "demux")]
use crate::video_source::VideoSource;

/// The monitors attached right now.
///
/// # Errors
///
/// [`Error::Render`] if the platform will not start an event loop, which on
/// macOS also means "this was not called from the main thread".
///
/// # Panics on a second call
///
/// An event loop can be built once per process on most platforms, and this
/// builds one. Call it before [`Viewer::show`] or not at all — a viewer reports
/// the monitor it landed on through
/// [`ResolvedPlacement`](crate::ResolvedPlacement) anyway.
pub fn monitors() -> Result<Vec<Monitor>> {
    // Only an *active* event loop can be asked about monitors, so this starts
    // one, takes the list on the first callback, and stops it again. No window
    // is ever created.
    struct Collector(Vec<Monitor>);

    impl ApplicationHandler for Collector {
        fn resumed(&mut self, event_loop: &ActiveEventLoop) {
            self.0 = attached(event_loop);
            event_loop.exit();
        }

        fn window_event(&mut self, _: &ActiveEventLoop, _: WindowId, _: WindowEvent) {}
    }

    let event_loop = EventLoop::new().map_err(|error| Error::Render {
        reason: format!("no event loop, so no monitors to list: {error}"),
    })?;

    let mut collector = Collector(Vec::new());

    event_loop
        .run_app(&mut collector)
        .map_err(|error| Error::Render {
            reason: format!("the event loop stopped before it listed anything: {error}"),
        })?;

    Ok(collector.0)
}

/// The monitors an active event loop can see.
pub(crate) fn attached(event_loop: &ActiveEventLoop) -> Vec<Monitor> {
    let primary = event_loop.primary_monitor();

    event_loop
        .available_monitors()
        .map(|handle| {
            let is_primary = primary
                .as_ref()
                .is_some_and(|candidate| *candidate == handle);

            describe(&handle, is_primary)
        })
        .collect()
}

/// One winit monitor as vtome describes it.
fn describe(handle: &winit::monitor::MonitorHandle, is_primary: bool) -> Monitor {
    let position = handle.position();
    let size = handle.size();
    let name = handle.name().unwrap_or_default();
    let scale_factor = handle.scale_factor();
    let refresh_millihertz = handle.refresh_rate_millihertz();
    let persistent_id = monitor_id(&name, position.x, position.y, scale_factor);

    Monitor {
        name,
        bounds: Rect::new(
            f64::from(position.x),
            f64::from(position.y),
            f64::from(size.width),
            f64::from(size.height),
        ),
        scale_factor,
        refresh_millihertz,
        is_primary,
        persistent_id,
    }
}

/// Shows a picture or a film in one place, as an overlay, until it is closed.
///
/// The window is the picture's own shape — the bounds of the placement's quad
/// and no larger — with no title bar or border, and transparent wherever the
/// quad does not reach, so a corner-pinned picture shows the desktop around it.
/// With [`Placement::always_on_top`] it stays above other applications, and
/// [`click_through`](Viewer::click_through) lets the mouse fall through to
/// whatever is underneath: a graphic laid over the screen rather than an
/// application window.
///
/// This is the "put that there" path. Seeking, playlists, and several layers on
/// one output belong to an application built on
/// [`Renderer`], which does not need to own the event
/// loop the way this does.
pub struct Viewer {
    content: Content,
    placement: Placement,
    title: String,
    decorations: bool,
    click_through: bool,
    looping: bool,
}

impl Viewer {
    /// A viewer for one frame at one placement.
    pub fn new(frame: Frame, placement: Placement) -> Self {
        Viewer::showing(Content::Still(frame), placement)
    }

    /// A viewer that plays `source` at one placement, looping unless told
    /// [not to](Viewer::looping).
    ///
    /// Each picture goes up when its own timestamp says, against a clock that
    /// starts once the first one is on screen — not as fast as they decode and
    /// not one per refresh. Late pictures are dropped rather than shown late,
    /// so a stall costs a few frames instead of permanent lag.
    #[cfg(feature = "demux")]
    pub fn video(source: VideoSource, placement: Placement) -> Self {
        Viewer::showing(
            Content::Video(Box::new(Playback::new(
                source,
                true,
                crate::clock::Monotonic::shared(),
                None,
            ))),
            placement,
        )
    }

    fn showing(content: Content, placement: Placement) -> Self {
        Viewer {
            content,
            placement,
            title: "vtome".to_string(),
            decorations: false,
            click_through: false,
            looping: true,
        }
    }

    /// Whether the mouse passes through to whatever is underneath.
    ///
    /// Off by default. On, the overlay can no longer be clicked to give it
    /// focus, and Escape only reaches a focused window — so something else has
    /// to end it: the application that opened it, or a film that does not loop.
    pub fn click_through(mut self, click_through: bool) -> Self {
        self.click_through = click_through;
        self
    }

    /// Whether a film starts again when it ends — the default — or the viewer
    /// closes. A still ignores this.
    pub fn looping(mut self, looping: bool) -> Self {
        self.looping = looping;
        self
    }

    /// The window title, where the platform shows one.
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = title.into();
        self
    }

    /// Whether to draw a title bar and a border.
    ///
    /// Off by default: a corner-pinned picture in a decorated window is a
    /// trapezoid inside a rectangle, which is rarely what anybody wanted.
    pub fn decorations(mut self, decorations: bool) -> Self {
        self.decorations = decorations;
        self
    }

    /// Opens the window and runs until it is closed, Escape is pressed, or a
    /// film that does not loop ends. Space pauses a film.
    ///
    /// Blocks. On macOS it has to be called from the main thread, which is the
    /// platform's rule rather than this crate's.
    ///
    /// # Errors
    ///
    /// [`Error::Render`] for anything the event loop, the GPU, or the window
    /// refuses; [`Error::NoSuchMonitor`] or [`Error::Placement`] from resolving
    /// the placement against the monitors that are actually there;
    /// [`Error::Decode`] if a film stops decoding partway.
    pub fn show(self) -> Result<Report> {
        let event_loop = EventLoop::new().map_err(|error| Error::Render {
            reason: format!("the event loop would not start: {error}"),
        })?;

        // A still waits for events rather than spinning: redrawing one picture
        // at the refresh rate would burn a core to show nothing new. A film
        // polls, and the surface's vsync is what paces it.
        event_loop.set_control_flow(match self.content {
            Content::Still(_) => ControlFlow::Wait,
            #[cfg(feature = "demux")]
            Content::Video(_) => ControlFlow::Poll,
        });

        let mut application = Application {
            viewer: self,
            state: None,
            failure: None,
        };

        event_loop
            .run_app(&mut application)
            .map_err(|error| Error::Render {
                reason: format!("the event loop stopped: {error}"),
            })?;

        match application.failure {
            Some(error) => Err(error),
            None => Ok(application.viewer.content.report()),
        }
    }
}

/// What happened while a [`Viewer`] was up.
///
/// The counters are [`crate::clock::Pacing`]'s: dropped pictures mean
/// decoding fell behind the clock, and repeats mean the display refreshes
/// faster than the film changes — normal, at 24 fps on a 60 Hz screen.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Report {
    /// Pictures put on screen. One, for a still.
    pub presented: u64,
    /// Pictures that arrived too late to be worth showing.
    pub dropped: u64,
    /// Refreshes that showed the previous picture again.
    pub repeated: u64,
    /// How many times a film started again from the top.
    pub loops: u32,
    /// Whether the film was decoded in hardware. `None` for a still.
    pub hardware_decode: Option<bool>,
}

/// What a viewer shows.
enum Content {
    Still(Frame),
    #[cfg(feature = "demux")]
    Video(Box<Playback>),
}

impl Content {
    /// The picture, when there is only ever one.
    fn still(&self) -> Option<&Frame> {
        match self {
            Content::Still(frame) => Some(frame),
            #[cfg(feature = "demux")]
            Content::Video(_) => None,
        }
    }

    /// The picture's size, known before any window exists.
    fn size(&self) -> (u32, u32) {
        match self {
            Content::Still(frame) => (frame.width(), frame.height()),
            // As stored rather than as rotated: the renderer draws the planes
            // as they are, so the placement has to be sized to match.
            #[cfg(feature = "demux")]
            Content::Video(playback) => playback.size(),
        }
    }

    fn report(&self) -> Report {
        match self {
            Content::Still(_) => Report {
                presented: 1,
                ..Report::default()
            },
            #[cfg(feature = "demux")]
            Content::Video(playback) => {
                let (presented, dropped, repeated) = playback.counts();

                Report {
                    presented,
                    dropped,
                    repeated,
                    loops: playback.loops(),
                    hardware_decode: Some(playback.source().decoder().is_hardware()),
                }
            }
        }
    }
}

/// Everything that exists only once the window does.
struct State {
    window: Arc<Window>,
    surface: wgpu::Surface<'static>,
    config: wgpu::SurfaceConfiguration,
    gpu: Gpu,
    renderer: Renderer,
    /// Whether anything has been uploaded yet. A film has nothing to draw
    /// until its first picture decodes.
    has_picture: bool,
    /// The quad in the window's own coordinates, ready for the shader.
    quad: crate::geometry::Quad,
    opacity: f32,
}

struct Application {
    viewer: Viewer,
    state: Option<State>,
    /// Kept rather than returned: `ApplicationHandler` has nowhere to put an
    /// error, and exiting the loop silently would leave a caller thinking the
    /// window had been shown and closed.
    failure: Option<Error>,
}

impl Application {
    /// Builds the window, the surface, and the renderer. Called once.
    fn start(&mut self, event_loop: &ActiveEventLoop) -> Result<State> {
        let monitors = attached(event_loop);
        let (width, height) = self.viewer.content.size();

        let resolved = self.viewer.placement.resolve(width, height, &monitors)?;

        if resolved.fell_back {
            eprintln!(
                "vtome: {} is not attached — showing on {} instead",
                self.viewer.placement.monitor, resolved.monitor.name
            );
        }

        let rect = resolved.window_rect();
        let (x, y, width, height) = rect.to_physical();

        let attributes = Window::default_attributes()
            .with_title(&self.viewer.title)
            .with_decorations(self.viewer.decorations)
            // Transparent so that everything outside a corner-pinned quad shows
            // what is behind the window rather than black.
            .with_transparent(true)
            .with_position(winit::dpi::PhysicalPosition::new(x, y))
            .with_inner_size(winit::dpi::PhysicalSize::new(width.max(1), height.max(1)))
            .with_window_level(if resolved.always_on_top {
                WindowLevel::AlwaysOnTop
            } else {
                WindowLevel::Normal
            });

        let window =
            Arc::new(
                event_loop
                    .create_window(attributes)
                    .map_err(|error| Error::Render {
                        reason: format!("the window would not open: {error}"),
                    })?,
            );

        if self.viewer.click_through {
            window
                .set_cursor_hittest(false)
                .map_err(|error| Error::Render {
                    reason: format!("this platform will not let clicks through a window: {error}"),
                })?;
        }

        // The instance has to know about the display to make a surface from it,
        // which is why this is not `Gpu::new`.
        let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_with_display_handle(
            Box::new(Arc::clone(&window)),
        ));

        let surface = instance
            .create_surface(Arc::clone(&window))
            .map_err(|error| Error::Render {
                reason: format!("no surface for that window: {error}"),
            })?;

        let gpu = Gpu::from_instance(instance, Some(&surface))?;

        let capabilities = surface.get_capabilities(&gpu.adapter);
        let format = crate::render::surface_format(&capabilities);

        let size = window.inner_size();

        let config = wgpu::SurfaceConfiguration {
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
            format,
            color_space: wgpu::SurfaceColorSpace::Srgb,
            width: size.width.max(1),
            height: size.height.max(1),
            present_mode: wgpu::PresentMode::AutoVsync,
            alpha_mode: crate::render::see_through(&capabilities),
            view_formats: vec![],
            desired_maximum_frame_latency: 2,
        };

        surface.configure(&gpu.device, &config);

        let mut renderer = Renderer::new(&gpu, format)?;
        let mut has_picture = false;

        if let Some(frame) = self.viewer.content.still() {
            renderer.upload(&gpu, frame)?;
            has_picture = true;
        }

        Ok(State {
            window,
            surface,
            config,
            gpu,
            renderer,
            has_picture,
            quad: resolved.quad_in_window(),
            opacity: resolved.opacity,
        })
    }

    /// One refresh: a film decides what is due, then whatever is uploaded is
    /// drawn.
    fn refresh(&mut self, event_loop: &ActiveEventLoop) -> Result<()> {
        let Some(state) = self.state.as_mut() else {
            return Ok(());
        };

        #[cfg(feature = "demux")]
        if let Content::Video(playback) = &mut self.viewer.content {
            playback.looping = self.viewer.looping;

            match playback.tick()? {
                Tick::Show(frame) => {
                    state.renderer.upload(&state.gpu, &frame)?;
                    state.has_picture = true;
                }
                Tick::Hold => {}
                Tick::Ended => {
                    event_loop.exit();
                    return Ok(());
                }
            }
        }

        if !state.has_picture {
            return Ok(());
        }

        Application::redraw(state)?;

        #[cfg(feature = "demux")]
        if let Content::Video(playback) = &mut self.viewer.content {
            playback.presented();
        }

        // Only a film uses the loop to end itself.
        let _ = event_loop;

        Ok(())
    }

    /// Draws one frame into the surface.
    fn redraw(state: &mut State) -> Result<()> {
        use wgpu::CurrentSurfaceTexture;

        let texture = match state.surface.get_current_texture() {
            CurrentSurfaceTexture::Success(texture)
            | CurrentSurfaceTexture::Suboptimal(texture) => texture,

            // A resize, a monitor change, or a minimised window. The next
            // redraw gets a fresh swapchain; none of these is fatal.
            CurrentSurfaceTexture::Timeout
            | CurrentSurfaceTexture::Occluded
            | CurrentSurfaceTexture::Outdated
            | CurrentSurfaceTexture::Lost => return Ok(()),

            other => {
                return Err(Error::Render {
                    reason: format!("no surface texture to draw into: {other:?}"),
                })
            }
        };

        let view = texture
            .texture
            .create_view(&wgpu::TextureViewDescriptor::default());

        let size = state.window.inner_size();

        state.renderer.draw(
            &state.gpu,
            &view,
            size.width.max(1),
            size.height.max(1),
            state.quad,
            state.opacity,
        )?;

        // Presenting is the queue's job in this version of wgpu, not the
        // texture's.
        state.gpu.queue.present(texture);

        Ok(())
    }
}

impl ApplicationHandler for Application {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        if self.state.is_some() {
            return;
        }

        match self.start(event_loop) {
            Ok(state) => {
                state.window.request_redraw();
                self.state = Some(state);
            }
            Err(error) => {
                self.failure = Some(error);
                event_loop.exit();
            }
        }
    }

    fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
        let Some(state) = self.state.as_mut() else {
            return;
        };

        match event {
            WindowEvent::CloseRequested
            | WindowEvent::KeyboardInput {
                event:
                    KeyEvent {
                        logical_key: Key::Named(NamedKey::Escape),
                        state: ElementState::Pressed,
                        ..
                    },
                ..
            } => event_loop.exit(),

            #[cfg(feature = "demux")]
            WindowEvent::KeyboardInput {
                event:
                    KeyEvent {
                        logical_key: Key::Named(NamedKey::Space),
                        state: ElementState::Pressed,
                        repeat: false,
                        ..
                    },
                ..
            } => {
                if let Content::Video(playback) = &mut self.viewer.content {
                    playback.toggle_pause();
                }
            }

            WindowEvent::Resized(size) => {
                state.config.width = size.width.max(1);
                state.config.height = size.height.max(1);
                state.surface.configure(&state.gpu.device, &state.config);

                state.window.request_redraw();
            }

            WindowEvent::RedrawRequested => {
                if let Err(error) = self.refresh(event_loop) {
                    self.failure = Some(error);
                    event_loop.exit();
                }
            }

            _ => {}
        }
    }

    fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
        // A film asks for the next refresh as soon as this one is done; the
        // surface's vsync is what keeps that to one per display frame.
        #[cfg(feature = "demux")]
        if let (Content::Video(_), Some(state)) = (&self.viewer.content, &self.state) {
            state.window.request_redraw();
        }
    }
}