diff --git a/apps/desktop-gpui/patches/zed-idle-frames.patch b/apps/desktop-gpui/patches/zed-idle-frames.patch new file mode 100644 index 00000000000..80f9b096024 --- /dev/null +++ b/apps/desktop-gpui/patches/zed-idle-frames.patch @@ -0,0 +1,334 @@ +diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs +index 0294b7d..6fe853b 100644 +--- a/crates/gpui/src/platform.rs ++++ b/crates/gpui/src/platform.rs +@@ -843,6 +843,12 @@ pub trait PlatformWindow: HasWindowHandle + HasDisplayHandle { + fn toggle_fullscreen(&self); + fn is_fullscreen(&self) -> bool; + fn on_request_frame(&self, callback: Box); ++ /// Returns a switch that stops (`false`) and restarts (`true`) the frame ++ /// requests delivered to [`Self::on_request_frame`]. Windows stop them while ++ /// they have nothing to draw. Platforms without one keep requesting frames. ++ fn frame_request_toggle(&self) -> Option> { ++ None ++ } + fn on_input(&self, callback: Box DispatchEventResult>); + fn on_active_status_change(&self, callback: Box); + fn on_hover_status_change(&self, callback: Box); +diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs +index 393e517..e5850bf 100644 +--- a/crates/gpui/src/window.rs ++++ b/crates/gpui/src/window.rs +@@ -120,6 +120,53 @@ struct WindowInvalidatorInner { + pub dirty_views: FxHashSet, + pub update_count: usize, + pub frame_dirty: FrameDirtyAccumulator, ++ pub frame_pacer: Rc, ++} ++ ++/// Stops the platform's frame requests once a window has had nothing to draw ++/// for [`FramePacer::IDLE_DELAY`], and restarts them as soon as anything ++/// invalidates the window or schedules a next-frame callback. Without this an ++/// idle window still wakes the main thread on every display refresh. ++pub(crate) struct FramePacer { ++ toggle: Option>, ++ paused: Cell, ++ busy_at: Cell, ++} ++ ++impl FramePacer { ++ /// Long enough that steady low-rate redraws (30fps video, timers) never ++ /// stop and restart the frame source between frames. ++ const IDLE_DELAY: Duration = Duration::from_millis(100); ++ ++ fn new(toggle: Option>) -> Self { ++ FramePacer { ++ toggle, ++ paused: Cell::new(false), ++ busy_at: Cell::new(Instant::now()), ++ } ++ } ++ ++ fn wake(&self) { ++ if let Some(toggle) = &self.toggle ++ && self.paused.replace(false) ++ { ++ self.busy_at.set(Instant::now()); ++ toggle(true); ++ } ++ } ++ ++ fn frame_finished(&self, busy: bool, now: Instant) { ++ let Some(toggle) = &self.toggle else { ++ return; ++ }; ++ if busy { ++ self.busy_at.set(now); ++ } else if now.duration_since(self.busy_at.get()) >= Self::IDLE_DELAY ++ && !self.paused.replace(true) ++ { ++ toggle(false); ++ } ++ } + } + + /// Per-frame invalidation bookkeeping, drained at draw time and emitted to the +@@ -138,7 +185,7 @@ pub(crate) struct WindowInvalidator { + } + + impl WindowInvalidator { +- pub fn new() -> Self { ++ pub fn new(frame_pacer: Rc) -> Self { + WindowInvalidator { + inner: Rc::new(RefCell::new(WindowInvalidatorInner { + dirty: true, +@@ -146,6 +193,7 @@ impl WindowInvalidator { + dirty_views: FxHashSet::default(), + update_count: 0, + frame_dirty: FrameDirtyAccumulator::default(), ++ frame_pacer, + })), + } + } +@@ -157,6 +205,9 @@ impl WindowInvalidator { + if inner.draw_phase == DrawPhase::None { + Self::record_frame_dirty(&mut inner); + inner.dirty = true; ++ let frame_pacer = inner.frame_pacer.clone(); ++ drop(inner); ++ frame_pacer.wake(); + cx.push_effect(Effect::Notify { emitter: entity }); + true + } else { +@@ -174,6 +225,9 @@ impl WindowInvalidator { + if dirty { + inner.update_count += 1; + Self::record_frame_dirty(&mut inner); ++ let frame_pacer = inner.frame_pacer.clone(); ++ drop(inner); ++ frame_pacer.wake(); + } + } + +@@ -1390,7 +1444,8 @@ impl Window { + let scale_factor = platform_window.scale_factor(); + let appearance = platform_window.appearance(); + let text_system = Arc::new(WindowTextSystem::new(cx.text_system().clone())); +- let invalidator = WindowInvalidator::new(); ++ let frame_pacer = Rc::new(FramePacer::new(platform_window.frame_request_toggle())); ++ let invalidator = WindowInvalidator::new(frame_pacer.clone()); + let active = Rc::new(Cell::new(platform_window.is_active())); + let hovered = Rc::new(Cell::new(platform_window.is_hovered())); + let needs_present = Rc::new(Cell::new(false)); +@@ -1507,9 +1562,10 @@ impl Window { + let invalidator = invalidator.clone(); + let active = active.clone(); + let needs_present = needs_present.clone(); +- let next_frame_callbacks = next_frame_callbacks.clone(); ++ let pending_frame_callbacks = next_frame_callbacks.clone(); + let input_rate_tracker = input_rate_tracker.clone(); + move |request_frame_options| { ++ let next_frame_callbacks = &pending_frame_callbacks; + let thermal_state = handle + .update(&mut cx, |_, _, cx| cx.thermal_state()) + .log_err(); +@@ -1551,8 +1607,11 @@ impl Window { + } + last_frame_time.set(Some(now)); + ++ let mut busy = request_frame_options.force_render ++ || request_frame_options.require_presentation; + let next_frame_callbacks = next_frame_callbacks.take(); + if !next_frame_callbacks.is_empty() { ++ busy = true; + handle + .update(&mut cx, |_, window, cx| { + for callback in next_frame_callbacks { +@@ -1570,6 +1629,7 @@ impl Window { + || (active.get() && input_rate_tracker.borrow_mut().is_high_rate()); + + if invalidator.is_dirty() || request_frame_options.force_render { ++ busy = true; + measure("frame duration", || { + handle + .update(&mut cx, |_, window, cx| { +@@ -1585,6 +1645,7 @@ impl Window { + .log_err(); + }) + } else if needs_present { ++ busy = true; + handle + .update(&mut cx, |_, window, _| window.present()) + .log_err(); +@@ -1595,6 +1656,9 @@ impl Window { + window.complete_frame(); + }) + .log_err(); ++ ++ busy |= invalidator.is_dirty() || !pending_frame_callbacks.borrow().is_empty(); ++ frame_pacer.frame_finished(busy, now); + } + })); + platform_window.on_resize(Box::new({ +@@ -2263,6 +2327,8 @@ impl Window { + /// Schedule the given closure to be run directly after the current frame is rendered. + pub fn on_next_frame(&self, callback: impl FnOnce(&mut Window, &mut App) + 'static) { + RefCell::borrow_mut(&self.next_frame_callbacks).push(Box::new(callback)); ++ let frame_pacer = self.invalidator.inner.borrow().frame_pacer.clone(); ++ frame_pacer.wake(); + } + + /// Schedule a frame to be drawn on the next animation frame. +@@ -6807,6 +6873,83 @@ pub fn outline( + } + } + ++#[cfg(test)] ++mod frame_pacer_tests { ++ use super::{FramePacer, Instant}; ++ use std::{cell::RefCell, rc::Rc, time::Duration}; ++ ++ fn recording_pacer() -> (FramePacer, Rc>>, Instant) { ++ let calls = Rc::new(RefCell::new(Vec::new())); ++ let recorded = calls.clone(); ++ let pacer = FramePacer::new(Some(Box::new(move |enabled| { ++ recorded.borrow_mut().push(enabled) ++ }))); ++ let created = pacer.busy_at.get(); ++ (pacer, calls, created) ++ } ++ ++ #[test] ++ fn idle_frames_stop_requests_only_after_the_delay() { ++ let (pacer, calls, start) = recording_pacer(); ++ pacer.frame_finished(false, start + Duration::from_millis(99)); ++ assert!(calls.borrow().is_empty()); ++ pacer.frame_finished(false, start + FramePacer::IDLE_DELAY); ++ assert_eq!(*calls.borrow(), [false]); ++ } ++ ++ #[test] ++ fn a_busy_frame_restarts_the_delay() { ++ let (pacer, calls, start) = recording_pacer(); ++ pacer.frame_finished(true, start + Duration::from_millis(90)); ++ pacer.frame_finished(false, start + Duration::from_millis(150)); ++ assert!(calls.borrow().is_empty()); ++ pacer.frame_finished(false, start + Duration::from_millis(190)); ++ assert_eq!(*calls.borrow(), [false]); ++ } ++ ++ #[test] ++ fn repeated_idle_frames_stop_requests_once() { ++ let (pacer, calls, start) = recording_pacer(); ++ for ms in [100, 120, 500] { ++ pacer.frame_finished(false, start + Duration::from_millis(ms)); ++ } ++ assert_eq!(*calls.borrow(), [false]); ++ } ++ ++ #[test] ++ fn wake_restarts_requests_once_and_only_when_stopped() { ++ let (pacer, calls, start) = recording_pacer(); ++ pacer.wake(); ++ assert!(calls.borrow().is_empty()); ++ pacer.frame_finished(false, start + FramePacer::IDLE_DELAY); ++ pacer.wake(); ++ pacer.wake(); ++ assert_eq!(*calls.borrow(), [false, true]); ++ } ++ ++ #[test] ++ fn a_woken_window_waits_a_full_delay_before_stopping_again() { ++ let (pacer, calls, start) = recording_pacer(); ++ pacer.frame_finished(false, start + FramePacer::IDLE_DELAY); ++ pacer.wake(); ++ let woken = pacer.busy_at.get(); ++ pacer.frame_finished(false, woken + Duration::from_millis(50)); ++ assert_eq!(*calls.borrow(), [false, true]); ++ pacer.frame_finished(false, woken + FramePacer::IDLE_DELAY); ++ assert_eq!(*calls.borrow(), [false, true, false]); ++ } ++ ++ #[test] ++ fn without_a_platform_toggle_nothing_is_tracked() { ++ let pacer = FramePacer::new(None); ++ let start = pacer.busy_at.get(); ++ pacer.frame_finished(false, start + FramePacer::IDLE_DELAY); ++ assert!(!pacer.paused.get()); ++ pacer.wake(); ++ assert!(!pacer.paused.get()); ++ } ++} ++ + #[cfg(test)] + mod tests { + use crate::{ +diff --git a/crates/gpui_macos/src/window.rs b/crates/gpui_macos/src/window.rs +index f5b2c1a..1d8d41b 100644 +--- a/crates/gpui_macos/src/window.rs ++++ b/crates/gpui_macos/src/window.rs +@@ -499,6 +499,7 @@ struct MacWindowState { + frame_source: Option, + renderer: renderer::Renderer, + request_frame_callback: Option>, ++ frame_requests_paused: bool, + event_callback: Option gpui::DispatchEventResult>>, + activate_callback: Option>, + resize_callback: Option, f32)>>, +@@ -657,6 +658,9 @@ impl MacWindowState { + + fn start_display_link(&mut self) { + self.stop_display_link(); ++ if self.frame_requests_paused { ++ return; ++ } + unsafe { + if !self + .native_window +@@ -896,6 +900,7 @@ impl MacWindow { + frame_source: None, + renderer, + request_frame_callback: None, ++ frame_requests_paused: false, + event_callback: None, + activate_callback: None, + resize_callback: None, +@@ -1670,6 +1675,38 @@ impl PlatformWindow for MacWindow { + self.0.as_ref().lock().request_frame_callback = Some(callback); + } + ++ fn frame_request_toggle(&self) -> Option> { ++ let window_state = Arc::downgrade(&self.0); ++ Some(Box::new(move |enabled| { ++ let Some(window_state) = window_state.upgrade() else { ++ return; ++ }; ++ let mut lock = window_state.lock(); ++ lock.frame_requests_paused = !enabled; ++ if !enabled { ++ lock.stop_display_link(); ++ return; ++ } ++ lock.start_display_link(); ++ // A restarted display link first fires a refresh or two later, so ++ // draw the frame that woke the window right away instead. ++ let window_state = Arc::downgrade(&window_state); ++ lock.foreground_executor ++ .spawn(async move { ++ let Some(window_state) = window_state.upgrade() else { ++ return; ++ }; ++ let Some(mut callback) = window_state.lock().request_frame_callback.take() ++ else { ++ return; ++ }; ++ callback(Default::default()); ++ window_state.lock().request_frame_callback = Some(callback); ++ }) ++ .detach(); ++ })) ++ } ++ + fn on_input(&self, callback: Box gpui::DispatchEventResult>) { + self.0.as_ref().lock().event_callback = Some(callback); + } diff --git a/scripts/prepare-gpui-dependency.sh b/scripts/prepare-gpui-dependency.sh index 3ccc9b4c4e6..bffbd90c10d 100755 --- a/scripts/prepare-gpui-dependency.sh +++ b/scripts/prepare-gpui-dependency.sh @@ -7,6 +7,7 @@ patch_paths=( "apps/desktop-gpui/patches/zed-gpui.patch" "apps/desktop-gpui/patches/zed-windows.patch" "apps/desktop-gpui/patches/zed-linux.patch" + "apps/desktop-gpui/patches/zed-idle-frames.patch" ) patch_files=("${patch_paths[@]/#/$repo_root/}") base_revision="5d1f83d9f27a19bec1fb241dc33b42238af9cf8d" diff --git a/scripts/prepare-gpui-dependency.test.mjs b/scripts/prepare-gpui-dependency.test.mjs index 8d786f7101d..e66507100a4 100644 --- a/scripts/prepare-gpui-dependency.test.mjs +++ b/scripts/prepare-gpui-dependency.test.mjs @@ -121,6 +121,7 @@ test("dependency verification handles overlapping patches without changing the c }; writeFileSync(path.join(upstream, "renderer.rs"), "base\n"); writeFileSync(path.join(upstream, "windows.rs"), "base\n"); + writeFileSync(path.join(upstream, "frames.rs"), "base\n"); commit(); const base = git(upstream, "rev-parse", "HEAD").trim(); const patch = (name) => { @@ -139,6 +140,8 @@ test("dependency verification handles overlapping patches without changing the c writeFileSync(path.join(upstream, "renderer.rs"), "Linux renderer\n"); rmSync(path.join(upstream, "temporary.rs")); patch("zed-linux.patch"); + writeFileSync(path.join(upstream, "frames.rs"), "paced frames\n"); + patch("zed-idle-frames.patch"); const script = path.join(cap, "scripts/prepare-gpui-dependency.sh"); writeFileSync( script, @@ -158,6 +161,10 @@ test("dependency verification handles overlapping patches without changing the c "Linux renderer\n", ); assert.equal(existsSync(path.join(zed, "temporary.rs")), false); + assert.equal( + readFileSync(path.join(zed, "frames.rs"), "utf8"), + "paced frames\n", + ); }); writeFileSync(path.join(zed, "unrelated.txt"), "preserve staged work\n"); @@ -242,6 +249,35 @@ test("dependency verification handles overlapping patches without changing the c }, ); + await t.test( + "a checkout prepared before a patch was added gains the new patch", + () => { + const addedPatch = path.join( + cap, + patchDirectory, + "zed-idle-frames.patch", + ); + const addedPatchBytes = readFileSync(addedPatch); + git(zed, "apply", "--reverse", addedPatch); + git(cap, "rm", "--quiet", addedPatch); + git(cap, "commit", "--quiet", "-m", "Stack without the added patch"); + writeFileSync(addedPatch, addedPatchBytes); + git(cap, "add", addedPatch); + git(cap, "commit", "--quiet", "-m", "Add a patch"); + const before = fingerprint(); + const result = run(cap, "bash", [script]); + assert.equal(result.status, 0, result.stderr); + assert.match(result.stderr, /Updated .* to the current ones/); + assert.equal( + readFileSync(path.join(zed, "frames.rs"), "utf8"), + "paced frames\n", + ); + const after = fingerprint(); + assert.deepEqual(after.unrelated, before.unrelated); + assert.equal(after.head, before.head); + }, + ); + await t.test( "local edits to a patched file are never overwritten by an update", () => {