Skip to main content

tilo_presentation/
lib.rs

1//! Platform-owned observations for native input and exact buffer presentation.
2//! Missing protocol support returns false while still presenting the buffer.
3pub mod clock;
4mod damage;
5mod device;
6pub mod keys;
7pub mod text_input;
8
9pub use device::device_numbers;
10
11pub use damage::DamageRect;
12#[cfg(all(feature = "gpu-damage", not(target_arch = "wasm32")))]
13pub use damage::set_next_present_damage;
14
15use std::time::Duration;
16use winit::{event::WindowEvent, window::Window};
17
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub enum Feedback {
20    Presented {
21        clock_id: u32,
22        timestamp: Duration,
23        refresh: Duration,
24        sequence: u64,
25        flags: u32,
26    },
27    Discarded,
28}
29
30pub const fn supported() -> bool {
31    cfg!(target_os = "linux")
32}
33
34pub fn key_stamp(event: &WindowEvent) -> Option<(u32, u32)> {
35    #[cfg(target_os = "linux")]
36    if let WindowEvent::KeyboardInput { event, .. } = event {
37        use winit::platform::wayland::KeyEventExtWayland;
38        return event.wayland_serial_time();
39    }
40    let _ = event;
41    None
42}
43
44/// What the desktop said about the window's frame: `Some(true)` where it draws
45/// it (KDE, X11 window managers), `Some(false)` where it asks the app to (GNOME
46/// on Wayland), `None` until it says or where desktops always draw it.
47pub fn desktop_draws_frame(window: &Window) -> Option<bool> {
48    #[cfg(target_os = "linux")]
49    {
50        use winit::platform::wayland::WindowExtWayland as _;
51        window.server_side_decorations()
52    }
53    #[cfg(not(target_os = "linux"))]
54    {
55        let _ = window;
56        None
57    }
58}
59
60/// Whether a frame [`present`] shows may fill only the top-left corner of a larger buffer: the
61/// window shows the frame's own pixels, so its swapchain can outlast a change of size.
62pub fn crops_frames(window: &Window) -> bool {
63    #[cfg(target_os = "linux")]
64    {
65        use winit::platform::wayland::WindowExtWayland as _;
66        window.crops_frames()
67    }
68    #[cfg(not(target_os = "linux"))]
69    {
70        let _ = window;
71        false
72    }
73}
74
75/// Present a frame of `size` pixels that `present` commits, at the size it was drawn for, and
76/// report its presentation to `observer` when one is given. Returns whether it is observed.
77/// The window's next redraw waits for this frame's callback: frames are paced by the display,
78/// whichever thread commits them.
79pub fn present(
80    window: &Window,
81    size: (u32, u32),
82    observer: Option<Box<dyn FnOnce(Feedback) + Send>>,
83    present: impl FnOnce(),
84) -> bool {
85    window.pre_present_notify();
86    #[cfg(target_os = "linux")]
87    {
88        use winit::platform::wayland::{WaylandPresentation, WindowExtWayland};
89        window.present_frame(
90            winit::dpi::PhysicalSize::new(size.0, size.1),
91            observer.map(
92                |observer| -> winit::platform::wayland::WaylandPresentationObserver {
93                    Box::new(move |result| {
94                        observer(match result {
95                            WaylandPresentation::Presented {
96                                clock_id,
97                                timestamp,
98                                refresh,
99                                sequence,
100                                flags,
101                            } => Feedback::Presented {
102                                clock_id,
103                                timestamp,
104                                refresh,
105                                sequence,
106                                flags,
107                            },
108                            WaylandPresentation::Discarded => Feedback::Discarded,
109                        });
110                    })
111                },
112            ),
113            present,
114        )
115    }
116    #[cfg(not(target_os = "linux"))]
117    {
118        let _ = (window, size, observer);
119        present();
120        false
121    }
122}
123
124#[cfg(test)]
125#[cfg(target_os = "linux")]
126mod wayland_tests {
127    use std::time::Duration;
128    use winit::platform::wayland::WaylandPresentation;
129
130    #[test]
131    fn standard_wayland_dispatch_decoder_preserves_kernel_clock_and_sequence() {
132        assert_eq!(
133            WaylandPresentation::from_protocol(
134                Some(1),
135                (2, 7, 123_456),
136                16_666_667,
137                (3, 9),
138                Some(7)
139            ),
140            WaylandPresentation::Presented {
141                clock_id: 1,
142                timestamp: Duration::new((2u64 << 32) | 7, 123_456),
143                refresh: Duration::from_nanos(16_666_667),
144                sequence: (3u64 << 32) | 9,
145                flags: 7,
146            }
147        );
148        for (clock, nanos, flags) in [
149            (None, 0, Some(7)),
150            (Some(1), 1_000_000_000, Some(7)),
151            (Some(1), 0, None),
152        ] {
153            assert_eq!(
154                WaylandPresentation::from_protocol(clock, (0, 1, nanos), 16_666_667, (0, 1), flags),
155                WaylandPresentation::Discarded
156            );
157        }
158    }
159}