hex/src/rune_hud/windows_pacing.rs
Dax 687ea32d65
Bump Rune to 1899e64: overlays pace frames with the swap chain on Windows (#1832)
* Bump Rune to 1899e64: overlays pace frames with the swap chain on Windows

Windows HUD memory grew and the app could hang after dictation because the
overlay presented frames without waiting for swap chain room after Rune #844;
Rune #985 makes an overlay's frames wait for room in its swap chain, as a
window's do.

nix: vendor each Cargo source in its own directory. Rune's tiny-skia-path is a
path dependency inside Rune (a Git crate at 0.11.4 here), the same name and
version as resvg's from crates.io, and cargo's directory source keys a crate by
name and version alone.

Goldens: Rune's text drawing changed over the 152 commits since d6715352.

* Review: patch accesskit_macos, and make the nix vendor split name what breaks

Rune #975 put accesskit_macos in its [patch.crates-io], so Hex patches it too
(Rune's README asks consumers to); it is the fifth and last crate Rune patches.

The vendor script override now checks each fragment it rewrites and names the
missing one, its comment says crates.io goes to registry/ and Git sources to
git/, nixfmt formats the file, and check-nix-git-pins.py's docstring says the
hashes reach importCargoLock through the cargoDeps override.

* linux: keep Hex running when the window can't start

Rune r399 makes App::run print a startup error and exit 1 on native targets
instead of panicking, so a Linux Hex with no display died, the next launch
became a fresh instance, and Check Linux's single-instance exercise saw
`hex start` hang and `hex app --hidden` reset.

run_app now returns Rune's try_run error and run_window reports it and waits,
as it did for the panic (#979).

* macos, windows: take rune's startup error instead of exiting

Rune r399 prints a startup error and exits the process from `run` on
native targets, which skipped Hex's own stop: macOS's drain (workers,
event-log flush, a recording kept for the next launch) and Windows' GPU
draw-marker disarm, so the launch that failed before drawing was read as
a crash while drawing. Both apps take `try_run`'s error now, log it, and
stop the way Quit does; Windows also reports it from `hex app`. The
Linux window already took the error (#979).

* macos: a launch whose window never started exits 1

Taking Rune's try_run error lets the app stop through the drain, but
process_exit::finish ends with _exit(0), so a failed GUI launch reported
success. finish_with takes the code; the window's failure exits 1 while a
normal quit keeps 0.

* docs: overlays wait for swap-chain room on windows since rune #985

Rune #985 makes every overlay's frames wait for room in its swap chain, so
the notes that said only a window waits and that the HUD's 60 Hz timer worked
around a suspected cause were stale. The timer is a cap on top of the slot
wait now. The macOS and Windows startup-error comments cited #979, which is
the Linux window issue; they describe Rune r399 instead.

* windows: a failed start keeps the GPU draw marker

disarm() means a frame reached the screen or Hex quit cleanly; a start that
never drew is neither, so it keeps the marker and the next launch falls back to
the high-performance GPU.

* docs: say what was measured about overlay pacing

* docs: say what stays bounded on windows, not what shrinks

The meetings map and the dictation map described the fix as memory that no
longer grows, and as Rune holding a frame's slot and skipping it. Rune #985
defers the frame until its swap chain has room, then draws it, which bounds
the frames in flight; the growth itself hasn't been re-measured on Windows
with the fix.
2026-11-14 20:40:20 -05:00

88 lines
4 KiB
Rust

//! Windows' own frame pacing for the HUD: a fixed ~60 Hz cap on top of Rune's
//! swap-chain slot wait, so a moving HUD asks for one frame at a time instead
//! of one per loop turn.
//!
//! Rune #985 makes every overlay's frames wait for room in their swap chain,
//! as a window's do, so frames in flight stay bounded. Before it, a HUD that
//! drew, submitted and presented as fast as the loop turned grew the swap
//! chain's queue of presented but unretired frames, and whatever each queued
//! frame held (about 100 MB/s to 5+ GB, #1796). Hex still asks for one frame
//! each `HUD_FRAME`, a fixed ~60 Hz timer rather than the display's refresh,
//! and reports the HUD through `HudCompositor::animating` as not display-paced.
use std::sync::atomic::Ordering;
use std::time::{Duration, Instant};
use rune::Tx;
use super::{Hud, HudEvent, RESHOW_AFTER, wants_frame};
/// How often the Windows HUD asks Rune for its next frame while it is armed: a
/// fixed ~60 Hz timer, not the display's refresh. Rune floors a poll's timer to
/// a frame interval (`TIMER_FRAME`), so it asks no faster than that.
#[cfg(any(target_os = "windows", test))]
pub(super) const HUD_FRAME: Duration = Duration::from_micros(16_667);
/// Whether the timer should ask for another frame: the HUD moves on screen, and
/// either a frame drew since the last poll, which proves the next frame draws
/// too, or `last_drawn` is recent enough that it will. Rune runs a poll before
/// the frame's render, so without the first check a stall of `RESHOW_AFTER`
/// would disarm the timer on the very frame that just drew: Transcribing and
/// Completed send no meters to restart it, and the HUD would freeze on screen.
#[cfg(any(target_os = "windows", test))]
pub(super) fn should_arm(
wants: bool,
last_drawn: Option<Instant>,
polled_at: Option<Instant>,
now: Instant,
) -> bool {
wants
&& last_drawn.is_some_and(|at| {
polled_at.is_none_or(|polled| at > polled)
|| now.saturating_duration_since(at) < RESHOW_AFTER
})
}
/// The step into `IndicatorModel` for a frame `now` on Windows: Rune gives a
/// frame's running motion the real gap but caps what it hands over as `dt` at
/// one 120 Hz resume step (`RESUME_STEP`), so `Frame::dt` is not the gap
/// between the HUD's own timer frames. The model takes the gap since its last
/// stepped frame instead; `fallback` is `Frame::dt` before any frame stepped.
#[cfg(target_os = "windows")]
pub(super) fn wall_dt(stepped_at: Option<Instant>, now: Instant, fallback: f32) -> f32 {
stepped_at.map_or(fallback, |at| {
now.saturating_duration_since(at).as_secs_f32()
})
}
/// A Windows meter's update: draw only while the HUD's timer isn't armed, since
/// the timer's next frame shows the level and the view reads no loudness. The
/// event is handled either way; a frame this draws lets the next frame's poll
/// arm the timer again, so a stalled timer starts again from one meter.
#[cfg(any(target_os = "windows", test))]
pub(super) fn meter_update(hud: &mut Hud, event: HudEvent) -> bool {
let draws = !hud.armed;
hud.handle(event);
draws
}
/// Rune's poll for the Windows HUD: arm the next frame while the HUD is moving
/// and drawing, and ask for nothing otherwise, so a show nothing draws never
/// turns the loop at 60 Hz with Hex idle. The frame's own render follows this
/// pass, so `last_drawn` is the previous drawn frame's (the one this poll's own
/// frame drew lands after it); `polled_at` tells them apart. Nothing the view
/// reads changed, so the frame needs no new commit.
#[cfg(any(target_os = "windows", test))]
pub(super) fn poll(hud: &mut Hud, _now: f64, tx: &mut Tx) -> bool {
let wants = wants_frame(
hud.animating.load(Ordering::Acquire),
hud.shown.load(Ordering::Acquire),
);
let now = Instant::now();
hud.armed = should_arm(wants, hud.last_drawn, hud.polled_at, now);
hud.polled_at = Some(now);
if hud.armed {
tx.wake_after(HUD_FRAME);
}
false
}