From 160638d8440a581264a0b1962005b54ef072a5ca Mon Sep 17 00:00:00 2001 From: Garvit Surana <251430630+garvitsurana271@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:11:01 +0000 Subject: [PATCH] fix(display): show a pointer move at the next vblank ahead of a desktop frame still waiting for its GPU fence, and time the trackpad's pointer reports, where the cursor's swaps queued behind that frame up to 1.7 s and CURSOR counted no touch frame macOS moves the pointer by swapping DCP layer 1 alone. The model latched swaps strictly in order, so each move waited behind WindowServer's fenced desktop frame (paulstelian97's 860M: deepest=33, latch max 1742 ms; boot 31: 74 ms). A ready swap whose slots no waiting swap in front of it set is now composed over the frame on screen at the next vblank; completions stay in submission order. DCP PACING reports these presents as overtaking=N. With --trackpad the touchpad gives touch frames, which the CURSOR line never counted (samples=0 in every log): a frame whose one finger moved now counts as a pointer report. --- crates/stela-boot/src/engine/cursor.rs | 42 ++++++++-- crates/stela-boot/src/engine/service.rs | 10 +-- crates/stela-soc/src/dcp/iomfb.rs | 93 ++++++++++++++++++--- crates/stela-soc/src/dcp/iomfb/tests.rs | 105 ++++++++++++++++++++++++ 4 files changed, 225 insertions(+), 25 deletions(-) diff --git a/crates/stela-boot/src/engine/cursor.rs b/crates/stela-boot/src/engine/cursor.rs index 79388f3c9c..d3cba78c06 100644 --- a/crates/stela-boot/src/engine/cursor.rs +++ b/crates/stela-boot/src/engine/cursor.rs @@ -5,21 +5,53 @@ //! counter, so its own rate (`DCP PACING`) is per guest second. //! //! The service notes the host read of the first pointer report a present -//! has not yet answered ([`motion`]); the boot PE looks at the display's +//! has not yet answered ([`inputs`]); the boot PE looks at the display's //! count of moved presents after each slice ([`Cursor::look`]), so a -//! latency is late by one slice at most. +//! latency is late by one slice at most. A pointer report is a mouse's or +//! a touchpad's motion, or, with `--trackpad`, a touch frame whose one +//! finger moved: the guest's trackpad moves its pointer by one finger's +//! travel. use alloc::string::String; use core::sync::atomic::{AtomicU64, Ordering}; +use stela_drivers::input::{Input, multitouch::Frame}; use stela_soc::dcp::iomfb::moved_presents; /// When the host read the oldest pointer report no moved present followed /// yet, in TSC ticks; zero for none. static MOTION_AT: AtomicU64 = AtomicU64::new(0); -/// The host read a pointer report at TSC `now`. -pub(super) fn motion(now: u64) { - let _ = MOTION_AT.compare_exchange(0, now.max(1), Ordering::Relaxed, Ordering::Relaxed); +/// The last touch frame's one finger ([`touched`]); zero for none. +static TOUCHED: AtomicU64 = AtomicU64::new(0); + +/// The host read `inputs` at TSC `now`. +pub(super) fn inputs(inputs: &[Input], now: u64) { + for input in inputs { + let moved = match input { + Input::Move { .. } | Input::Motion { .. } => true, + Input::Touch(frame) => { + let finger = touched(frame); + let last = TOUCHED.swap(finger, Ordering::Relaxed); + finger != 0 && last != 0 && finger != last + } + _ => false, + }; + if moved { + let _ = MOTION_AT.compare_exchange(0, now.max(1), Ordering::Relaxed, Ordering::Relaxed); + } + } +} + +/// Where `frame`'s one finger touching the pad is, as `x << 32 | y` in +/// micrometres with bit 63 set; zero unless one finger touches it. +fn touched(frame: &Frame) -> u64 { + let mut tips = frame.fingers().iter().filter(|finger| finger.tip); + match (tips.next(), tips.next()) { + (Some(finger), None) => { + 1 << 63 | u64::from(finger.x_um & 0x7fff_ffff) << 32 | u64::from(finger.y_um) + } + _ => 0, + } } /// The moved presents and latencies since the last report. diff --git a/crates/stela-boot/src/engine/service.rs b/crates/stela-boot/src/engine/service.rs index 013e04495e..84805c42f2 100644 --- a/crates/stela-boot/src/engine/service.rs +++ b/crates/stela-boot/src/engine/service.rs @@ -341,15 +341,7 @@ impl Service { input_turn.read_done = host::rdtsc(); input_turn.reports = inputs.len(); } - if inputs.iter().any(|input| { - matches!( - input, - stela_drivers::input::Input::Move { .. } - | stela_drivers::input::Input::Motion { .. } - ) - }) { - super::cursor::motion(host::rdtsc()); - } + super::cursor::inputs(&inputs, host::rdtsc()); if !inputs.is_empty() { devices.serve(|devices| { #[cfg(feature = "diagnostics")] diff --git a/crates/stela-soc/src/dcp/iomfb.rs b/crates/stela-soc/src/dcp/iomfb.rs index 229ed7da97..457b9e0fe9 100644 --- a/crates/stela-soc/src/dcp/iomfb.rs +++ b/crates/stela-soc/src/dcp/iomfb.rs @@ -64,7 +64,11 @@ //! itself: in the runs measured, WindowServer kept at most two of its own //! swaps open, while its cursor client (layer 1 only, one surface moved) //! kept submitting behind a fenced frame, 19 deep at most, until it -//! cleared. The vsync callback +//! cleared. Its layer is shown ahead meanwhile: a ready swap's slots that +//! no waiting swap in front of it set are composed over the frame on +//! screen at the next vblank, though it still completes in order +//! ([`Methods::ahead`]), so the pointer follows its client and not the +//! GPU's slowest frame. The vsync callback //! `D588` carries the vblank's own counter value, not the time the model //! looked, as its first value and as its timestamp, the second. The AP's //! library turns that timestamp from the coprocessor's clock, continuous @@ -289,6 +293,10 @@ pub struct Pacing { /// Presents that moved a layer: one whose destination kept its size at /// another place, as a pointer's does ([`moved_presents`]). pub moved: u32, + /// Presents that showed a later swap's slots ahead of a swap still + /// waiting for its GPU fence ([`Methods::ahead`]): the pointer moved + /// over a frame still drawing. + pub overtaking: u32, /// Where a swap's time goes, in counter ticks, so a slow frame rate can /// be told apart from a late display: `fence`, from its submit to the /// notice of its last GPU fence (swaps that waited for one); `notice`, @@ -521,7 +529,7 @@ impl core::fmt::Display for Pacing { let ms = |ticks: u64| ticks as f64 * 1000.0 / COUNTER_HZ as f64; write!( f, - "DCP PACING seconds={:.1} presented={} submitted={} merged={} per_second={:.1} gap_ms={:.1}/{:.1}/{:.1} stalls={} late_ms={:.1}/{:.1} deepest={} reordered={} ahead_of_gpu={} fence_waits={} fence_timeouts={} calls={} unmodeled={} color_matrix={} moved={} swap_ms=n:avg/max fence={} notice={} latch={} complete={} total={} vsync={}", + "DCP PACING seconds={:.1} presented={} submitted={} merged={} per_second={:.1} gap_ms={:.1}/{:.1}/{:.1} stalls={} late_ms={:.1}/{:.1} deepest={} reordered={} ahead_of_gpu={} fence_waits={} fence_timeouts={} calls={} unmodeled={} color_matrix={} moved={} overtaking={} swap_ms=n:avg/max fence={} notice={} latch={} complete={} total={} vsync={}", ms(self.ticks) / 1000.0, self.presented, self.submitted, @@ -546,6 +554,7 @@ impl core::fmt::Display for Pacing { self.unmodeled, self.color_matrix, self.moved, + self.overtaking, self.fence, self.notice, self.latch, @@ -779,6 +788,9 @@ struct Batch { planes: [Option; swap::LAYERS], layers: Vec, damage: Option>, + /// The slots shown ahead of the swaps waiting in front of theirs + /// ([`Methods::ahead`]): with `count` 0, all the batch shows anew. + ahead: u32, } /// The IOMFB methods of the one connected display, presenting to a host @@ -1191,18 +1203,61 @@ impl Methods { } /// Whether the last vblank latches a swap at counter `now` - /// ([`Self::frame_ready`]). + /// ([`Self::frame_ready`]), or shows a later swap's layers ahead of + /// one still drawing ([`Self::ahead`]). fn swap_ready(&mut self, now: u64) -> bool { if self.latched.is_none() { return false; } - if self.ready_swaps(now) != 0 { + if self.ready_swaps(now) != 0 || self.ahead_wanted() { return true; } self.latched = None; false } + /// `base` with the slots that swaps queued from the `count`th on set + /// and that may be shown ahead of the swaps waiting in front of them, + /// and those slots. A swap's slots go ahead when its own surfaces are + /// ready and no swap in front of it that is still waiting set any of + /// them, so each slot still shows its swaps in submission order. This + /// is the pointer: WindowServer's cursor client sets layer 1 only, with + /// a surface the CPU drew, and kept submitting behind a frame waiting + /// for its GPU fence, 33 deep in the runs measured, its moves latched + /// up to 1.7 s after they were ready. Only what is shown changes: every swap still + /// completes in order, once the ones in front of it are latched, so + /// the AP keeps each surface until after its swap left the screen. + fn ahead( + &self, + count: usize, + base: [Option; swap::LAYERS], + ) -> ([Option; swap::LAYERS], u32) { + let (mut planes, mut ahead, mut held) = (base, 0u32, 0u32); + for frame in self.frames.iter().skip(count) { + if frame.fenced != 0 || frame.updated & held != 0 { + held |= frame.updated; + continue; + } + for (slot, plane) in planes.iter_mut().enumerate() { + if frame.updated & 1 << slot != 0 { + *plane = frame.planes[slot]; + } + } + ahead |= frame.updated; + } + (planes, ahead) + } + + /// Whether, with no swap ready, a later one's slots would change the + /// frame on screen if shown ahead ([`Self::ahead`]). + fn ahead_wanted(&self) -> bool { + let Some(shown) = self.shown else { + return false; + }; + let (planes, ahead) = self.ahead(0, shown); + ahead != 0 && planes != shown + } + /// `io_fence_notify`: the GPU finished `surface` for `swap_id`. A queued /// swap stops waiting for each layer that shows it; one only started /// keeps the notice for its submission. @@ -1398,7 +1453,7 @@ impl Methods { read, &mut self.frame, ); - if batch.count == 0 { + if batch.count == 0 && batch.ahead == 0 { return self.repainted(presented); } presented?; @@ -1427,18 +1482,24 @@ impl Methods { planes, layers: planes.iter().flatten().copied().collect(), damage: None, + ahead: 0, }); } let due = self.latched.expect("a ready frame has an open latch"); let count = self.ready_swaps(now); - let newest = &self.frames[count - 1]; - let planes = newest.planes; - let layers = newest.layers(); + // With none ready, only a later swap's slots shown ahead: a frame + // on screen it changes (`ahead_wanted`). + let base = match count { + 0 => self.shown.expect("only a frame on screen is shown ahead"), + _ => self.frames[count - 1].planes, + }; + let (planes, ahead) = self.ahead(count, base); + let layers = planes.iter().flatten().copied().collect(); let updated = self .frames .iter() .take(count) - .fold(self.unshown, |slots, frame| slots | frame.updated); + .fold(self.unshown | ahead, |slots, frame| slots | frame.updated); let damage = self .shown .map(|shown| damage(&shown, &planes, updated, self.mode().height)); @@ -1449,6 +1510,7 @@ impl Methods { planes, layers, damage, + ahead, }) } @@ -1549,7 +1611,7 @@ impl Methods { self.in_place = true; return Ok(false); } - if batch.count == 0 { + if batch.count == 0 && batch.ahead == 0 { return self.repainted(result).map(|()| false); } result?; @@ -1578,7 +1640,8 @@ impl Methods { } /// Finish the present of `batch`, whose rows the scanout now holds: - /// complete its swaps, oldest first, and pace by it. + /// complete its swaps, oldest first, and pace by it. A batch of slots + /// shown ahead only ([`Self::ahead`]) completes nothing. fn presented(&mut self, batch: Batch) { let Batch { now, @@ -1587,6 +1650,7 @@ impl Methods { planes, layers, damage: _, + ahead, } = batch; // A vblank passed during a compose opened a new latch: it stays. if self.latched == Some(due) { @@ -1600,10 +1664,17 @@ impl Methods { self.pacing.moved += 1; MOVED.fetch_add(1, core::sync::atomic::Ordering::Release); } + self.pacing.overtaking += u32::from(ahead != 0); self.shown = Some(planes); self.unshown = 0; self.repaint_refused = false; let waiting = self.frames.len(); + if count == 0 { + // The newest swap on screen is still the last one latched. + let last = self.last_present.map_or(0, |(_, swap_id)| swap_id); + self.pace(now, due, last, waiting); + return; + } let batch: Vec = self.frames.drain(..count).collect(); let (shown, merged) = batch.split_last().expect("a latched batch is not empty"); for frame in &batch { diff --git a/crates/stela-soc/src/dcp/iomfb/tests.rs b/crates/stela-soc/src/dcp/iomfb/tests.rs index b0d4f9902e..ab0a17469b 100644 --- a/crates/stela-soc/src/dcp/iomfb/tests.rs +++ b/crates/stela-soc/src/dcp/iomfb/tests.rs @@ -2014,6 +2014,111 @@ fn a_swap_waits_for_the_gpu_fences_of_the_surfaces_it_shows() { assert!(!methods.frame_pending()); } +#[test] +fn a_pointer_swap_is_shown_ahead_of_a_frame_still_drawing_and_completes_after_it() { + let screen = TestScreen(Rc::default(), Some(MODE)); + let frames = screen.0.clone(); + let mut methods = Methods::new(Box::new(screen), interface()); + methods.set_swap_tracing(false); + let period = methods.refresh_ticks; + let fill = |_: u64, bytes: &mut [u8]| { + bytes.fill(0x40); + true + }; + let pointer_at = |methods: &mut Methods, x: u32| { + let id = start_client_swap(methods, 0xc0de); + let mut input = submission(id, &[(1, 1, 1, 0x9000)]); + place(&mut input, 1, rect(0, 0, 1, 1), rect(x, 1, 1, 1)); + returned(methods.call(*b"A407", &input, 0xc)); + id + }; + let shown = |methods: &Methods| { + let shown = methods.shown.expect("a frame is on screen"); + let (desktop, pointer) = (shown[0].unwrap(), shown[1].unwrap()); + (desktop.surface.iova, pointer.destination.x) + }; + // The desktop on layer 0 and the pointer on layer 1. + let first = start_swap(&mut methods); + let mut input = submission(first, &[(0, 4, 2, 0x1000), (1, 1, 1, 0x9000)]); + place(&mut input, 1, rect(0, 0, 1, 1), rect(0, 1, 1, 1)); + returned(methods.call(*b"A407", &input, 0xc)); + methods.refresh(0); + methods.present(0, fill).unwrap(); + complete(&mut methods, first, true); + assert_eq!(shown(&methods), (0x1000, 0)); + + // WindowServer's next desktop frame waits for the GPU; the pointer's + // client moves it twice meanwhile. + let drawing = start_swap(&mut methods); + returned(methods.call( + *b"A407", + &fenced_submission(drawing, &[(0, 4, 2, 0x2000)], 1, [5, 0, 0, 0]), + 0xc, + )); + let moved = pointer_at(&mut methods, 1); + methods.refresh(period); + assert!(methods.frame_ready(period)); + methods + .present(period, |dva, bytes| { + assert!( + !(0x2000..0x9000).contains(&dva), + "a surface still drawing is read" + ); + fill(dva, bytes) + }) + .unwrap(); + // The pointer moved over the desktop on screen, and nothing completed: + // its swap waits for the frame in front of it. + assert_eq!(shown(&methods), (0x1000, 1)); + assert_eq!(frames.borrow().len(), 2); + assert!(!methods.callback_pending()); + // One present a vblank, and none while nothing more moved. + assert!(!methods.frame_ready(period + 1)); + methods.refresh(2 * period); + assert!(!methods.frame_ready(2 * period)); + let again = pointer_at(&mut methods, 2); + methods.refresh(3 * period); + methods.present(3 * period, fill).unwrap(); + assert_eq!(shown(&methods), (0x1000, 2)); + + // A ready swap that sets the waiting frame's slot keeps its place, and + // so does a pointer move behind it: each slot shows in order. + let both = start_swap(&mut methods); + returned(methods.call( + *b"A407", + &submission(both, &[(0, 4, 2, 0x3000), (1, 1, 1, 0x9000)]), + 0xc, + )); + let behind = pointer_at(&mut methods, 3); + methods.refresh(4 * period); + assert!(!methods.frame_ready(4 * period)); + assert_eq!(shown(&methods), (0x1000, 2)); + + // The frame drawn: every swap is latched together and completes in + // submission order. + let input = Packed::new() + .u32(drawing) + .u32(5) + .u64(0x1234) + .u32(0) + .finish(0x14) + .unwrap(); + returned(methods.call(*b"A470", &input, 4)); + methods.refresh(5 * period); + methods.present(5 * period, fill).unwrap(); + assert_eq!(shown(&methods), (0x3000, 3)); + for id in [drawing, moved, again, both, behind] { + let callback = methods.take_callback().expect("a completion is due"); + assert_eq!(&callback.input[..4], &id.to_le_bytes()); + (callback.then)(&mut methods, &[]).unwrap(); + } + assert!(!methods.frame_pending()); + assert_eq!( + (methods.pacing.overtaking, methods.pacing.presented), + (2, 4) + ); +} + #[test] fn each_set_parameter_dcp_parameter_is_reported_with_its_answer() { let mut methods = methods();