Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Make power and thermal frame intervals configurable (#66281)
Lukas Wirth7c8dbadcb5c3
diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rsindex a99c4ba3e46..fb12125cf11 100644--- a/crates/gpui/src/platform.rs+++ b/crates/gpui/src/platform.rs@@ -2634,11 +2634,24 @@ pub struct WindowOptions {/// Leave this `false` for windows that rely on AppKit's native titlebar dragging.pub app_owns_titlebar_drag: bool,- /// The minimum interval between animation frames while the window is inactive.+ /// The minimum frame interval while inactive without sustained high-rate input.///- /// Set to `None` to disable inactive-window animation frame throttling.+ /// Defaults to approximately 30 fps. Set to `None` to disable this limit.+ /// The longest applicable interval wins; mandatory platform presentations bypass all limits.pub inactive_frame_interval: Option<Duration>,+ /// The minimum frame interval while the platform reports Low Power Mode.+ ///+ /// Defaults to approximately 30 fps. Set to `None` to disable this limit.+ /// The longest applicable interval wins; mandatory platform presentations bypass all limits.+ pub low_power_frame_interval: Option<Duration>,++ /// The minimum frame interval under serious or critical thermal pressure.+ ///+ /// Defaults to approximately 60 fps. Set to `None` to disable this limit.+ /// The longest applicable interval wins; mandatory platform presentations bypass all limits.+ pub thermal_frame_interval: Option<Duration>,+/// Whether the window should be resizable by the userpub is_resizable: bool,@@ -2790,6 +2803,8 @@ impl Default for WindowOptions {is_movable: true,app_owns_titlebar_drag: false,inactive_frame_interval: Some(Duration::from_micros(33_333)),+ low_power_frame_interval: Some(Duration::from_micros(33_333)),+ thermal_frame_interval: Some(Duration::from_micros(16_667)),is_resizable: true,is_minimizable: true,display_id: None,diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rsindex 6be8eaeec8e..78738238c71 100644--- a/crates/gpui/src/window.rs+++ b/crates/gpui/src/window.rs@@ -1323,8 +1323,6 @@ pub(crate) struct InputRateTracker {sustain_duration: Duration,}-const THIRTY_FPS_INTERVAL: Duration = Duration::from_micros(33333);-const SIXTY_FPS_INTERVAL: Duration = Duration::from_micros(16667);const FRAME_INTERVAL_TOLERANCE: Duration = Duration::from_millis(1);/// Keeps throttled frames on a stable cadence despite callback delivery jitter.@@ -1361,30 +1359,28 @@ fn minimum_frame_interval(should_throttle: bool,inactive_without_high_rate_input: bool,inactive_frame_interval: Option<Duration>,+ low_power_frame_interval: Option<Duration>,+ thermal_frame_interval: Option<Duration>,thermal_state: ThermalState,power_mode: crate::PowerMode,) -> Option<Duration> {- let low_power_mode_enabled = power_mode == crate::PowerMode::LowPower;if !should_throttle {- None- } else if inactive_without_high_rate_input {- match (inactive_frame_interval, low_power_mode_enabled) {- (Some(inactive_frame_interval), true) => {- Some(inactive_frame_interval.max(THIRTY_FPS_INTERVAL))- }- (inactive_frame_interval, false) => inactive_frame_interval,- (None, true) => Some(THIRTY_FPS_INTERVAL),- }- } else if low_power_mode_enabled {- Some(THIRTY_FPS_INTERVAL)- } else if matches!(- thermal_state,- ThermalState::Critical | ThermalState::Serious- ) {- Some(SIXTY_FPS_INTERVAL)- } else {- None+ return None;}++ [+ inactive_frame_interval.filter(|_| inactive_without_high_rate_input),+ low_power_frame_interval.filter(|_| power_mode == crate::PowerMode::LowPower),+ thermal_frame_interval.filter(|_| {+ matches!(+ thermal_state,+ ThermalState::Critical | ThermalState::Serious+ )+ }),+ ]+ .into_iter()+ .flatten()+ .max()}impl Default for InputRateTracker {@@ -1650,6 +1646,8 @@ impl Window {is_movable,app_owns_titlebar_drag,inactive_frame_interval,+ low_power_frame_interval,+ thermal_frame_interval,is_resizable,is_minimizable,display_id,@@ -1900,10 +1898,6 @@ impl Window {|| needs_present.get()|| high_rate_input;- // Throttle frame rate based on conditions:- // - Thermal pressure (Serious/Critical): cap to ~60fps- // - Low power mode: cap to ~30fps- // - Inactive window (not focused): use its configured frame intervallet should_throttle = !request_frame_options.require_presentation;let inactive_without_high_rate_input =should_throttle && !active.get() && !high_rate_input;@@ -1913,6 +1907,8 @@ impl Window {should_throttle,inactive_without_high_rate_input,inactive_frame_interval,+ low_power_frame_interval,+ thermal_frame_interval,thermal_state,power_mode,);@@ -1936,9 +1932,13 @@ impl Window {profiler::journal::record_frame_skipped(window_id,Instant::now(),- if inactive_without_high_rate_input {+ if inactive_without_high_rate_input+ && inactive_frame_interval == min_frame_interval+ {profiler::journal::FrameSkipReason::InactiveFrameRateLimit- } else if power_mode == crate::PowerMode::LowPower {+ } else if power_mode == crate::PowerMode::LowPower+ && low_power_frame_interval == min_frame_interval+ {profiler::journal::FrameSkipReason::LowPowerFrameRateLimit} else {profiler::journal::FrameSkipReason::ThermalFrameRateLimit@@ -8494,8 +8494,8 @@ mod tests {fn frame_throttle_tracks_display_refreshes() {let start = super::Instant::now();for (elapsed, interval) in [- (Duration::from_micros(16_666), super::SIXTY_FPS_INTERVAL),- (Duration::from_micros(33_332), super::THIRTY_FPS_INTERVAL),+ (Duration::from_micros(16_666), Duration::from_micros(16_667)),+ (Duration::from_micros(33_332), Duration::from_micros(33_333)),] {let mut pacer = super::FramePacer::default();assert!(!pacer.should_defer(start, Some(interval)));@@ -8507,7 +8507,7 @@ mod tests {#[test]fn frame_throttle_preserves_cadence_without_catching_up_after_stalls() {let start = super::Instant::now();- let interval = super::THIRTY_FPS_INTERVAL;+ let interval = Duration::from_micros(33_333);let mut pacer = super::FramePacer::default();assert!(!pacer.should_defer(start, Some(interval)));let delayed_frame = start + interval + Duration::from_millis(5);@@ -8528,36 +8528,94 @@ mod tests {#[test]fn power_modes_preserve_frame_limits() {+ let options = WindowOptions::default();for (power_mode, active_interval, thermal_interval) in [(crate::PowerMode::LowPower,- Some(super::THIRTY_FPS_INTERVAL),- super::THIRTY_FPS_INTERVAL,+ Some(Duration::from_micros(33_333)),+ Duration::from_micros(33_333),+ ),+ (+ crate::PowerMode::Unknown,+ None,+ Duration::from_micros(16_667),+ ),+ (+ crate::PowerMode::Balanced,+ None,+ Duration::from_micros(16_667),+ ),+ (+ crate::PowerMode::HighPower,+ None,+ Duration::from_micros(16_667),),- (crate::PowerMode::Unknown, None, super::SIXTY_FPS_INTERVAL),- (crate::PowerMode::Balanced, None, super::SIXTY_FPS_INTERVAL),- (crate::PowerMode::HighPower, None, super::SIXTY_FPS_INTERVAL),] {assert_eq!(- super::minimum_frame_interval(true, false, None, ThermalState::Nominal, power_mode),+ super::minimum_frame_interval(+ true,+ false,+ options.inactive_frame_interval,+ options.low_power_frame_interval,+ options.thermal_frame_interval,+ ThermalState::Nominal,+ power_mode,+ ),active_interval,);assert_eq!(- super::minimum_frame_interval(true, false, None, ThermalState::Serious, power_mode),+ super::minimum_frame_interval(+ true,+ false,+ options.inactive_frame_interval,+ options.low_power_frame_interval,+ options.thermal_frame_interval,+ ThermalState::Serious,+ power_mode,+ ),Some(thermal_interval),);- let inactive_interval = Duration::from_millis(100);+ }+ }++ #[test]+ fn frame_limits_combine_enabled_intervals() {+ let short = Some(Duration::from_millis(10));+ let long = Some(Duration::from_millis(100));+ for (inactive, low_power, thermal, expected) in [+ (long, short, short, long),+ (short, long, short, long),+ (short, short, long, long),+ (long, None, None, long),+ (None, long, None, long),+ (None, None, long, long),+ (None, None, None, None),+ ] {assert_eq!(super::minimum_frame_interval(true,true,- Some(inactive_interval),- ThermalState::Nominal,- power_mode,+ inactive,+ low_power,+ thermal,+ ThermalState::Critical,+ crate::PowerMode::LowPower,),- Some(inactive_interval),+ expected,);}+ assert_eq!(+ super::minimum_frame_interval(+ false,+ true,+ long,+ long,+ long,+ ThermalState::Critical,+ crate::PowerMode::LowPower,+ ),+ None,+ );}struct EmptyView;