cranpose_app_shell/frame_rate_preference.rs
1/// How the platform should vote the display's frame rate on behalf of the app.
2///
3/// Compose apps get 120 Hz gameplay on a 120 Hz panel not by presenting faster
4/// but because HWUI votes a rate on the window while gestures run, content
5/// moves or the window redraws continuously, and clears it when they stop. A window that never votes is pinned by
6/// SurfaceFlinger's cadence inference instead — which also throttles the app's
7/// choreographer, so the inference reinforces itself. `Auto` reproduces the
8/// HWUI behaviour; the platform backends read it every frame and vote through
9/// the native window when the desired rate changes.
10#[derive(Debug, Clone, Copy, PartialEq, Default)]
11pub enum FrameRatePreference {
12 /// Ask for the panel's fastest rate while [`FrameRateBoost`](crate::FrameRateBoost) holds, a
13 /// quiet rate while frames arrive only intermittently, no preference
14 /// when the scene is still. This is the default, matching what Compose/HWUI do for every
15 /// app without the app's involvement.
16 #[default]
17 Auto,
18 /// Never vote; the OS infers a rate from presentation cadence.
19 NoPreference,
20 /// Always vote exactly this rate in Hz. Values `<= 0` behave like
21 /// [`FrameRatePreference::NoPreference`].
22 Exact(f32),
23}
24
25impl FrameRatePreference {
26 /// The baseline `Auto` votes while frames arrive without a boost — the
27 /// same rate HWUI's NORMAL frame-rate category resolves to on phone
28 /// panels. The quiet vote cannot simply be "no vote": SurfaceFlinger
29 /// infers a non-voting window's rate from whatever cadence it last
30 /// observed and pins it, so an app that ever ran the panel's fast rate
31 /// would stay there forever (measured on a Pixel 9 Pro, both directions).
32 pub const AUTO_QUIET_RATE_HZ: f32 = 60.0;
33
34 /// The rate the platform should vote right now, in Hz, where `0.0` means
35 /// "clear the vote". `producing_frames` is whether the frame loop has a
36 /// frame scheduled, `boosted` what [`FrameRateBoost::boosted`](crate::FrameRateBoost::boosted) says, and
37 /// `panel_max_hz` the display's fastest supported rate when the platform
38 /// knows it.
39 ///
40 /// While boosted, `Auto` holds the boost even through moments with no
41 /// frame scheduled: a gesture sequence crosses still screens (a tap lands,
42 /// the old scene stops animating, the new one hasn't started), and letting
43 /// each of those instantly clear the vote flapped the display between the
44 /// boost rate and no-vote several times per second on device. This mirrors
45 /// SurfaceFlinger's own touch boost, which also outlives the touch by
46 /// seconds regardless of what the app presents in between.
47 pub fn desired_rate_hz(
48 self,
49 producing_frames: bool,
50 boosted: bool,
51 panel_max_hz: Option<f32>,
52 ) -> f32 {
53 match self {
54 Self::Auto => {
55 if boosted {
56 panel_max_hz
57 .filter(|rate| *rate > 0.0)
58 .unwrap_or(Self::AUTO_QUIET_RATE_HZ)
59 } else if producing_frames {
60 Self::AUTO_QUIET_RATE_HZ
61 } else {
62 0.0
63 }
64 }
65 Self::NoPreference => 0.0,
66 Self::Exact(rate) if rate > 0.0 => rate,
67 Self::Exact(_) => 0.0,
68 }
69 }
70}