fix: cap the session refresh rate at the device's peak refresh rate (#715)

This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-13 13:16:29 +00:00
1 parent 8f9ce19638
commit da8fe068d9
2 files changed
+26 -3

No files matched your search

@@ -3,7 +3,9 @@ package com.droiddeck.launcher.session
import android.app.Activity
import android.app.GameManager
import android.app.GameState
import android.content.Context
import android.os.Build
import android.provider.Settings
import android.util.Log
/**
@@ -16,8 +18,9 @@ import android.util.Log
* a level the device can hold indefinitely rather than holding a floor, which cost games and
* Big Picture their peak clocks. WinNative never asks for it; the thermal budget is left to
* the game;
* - the panel's fastest mode at its current size (Android 6+): a 120 Hz phone is switched to
* 120 Hz for the menu, not only when a game votes for it. With a frame cap, the fastest mode
* - the panel's fastest mode at its current size (Android 6+), up to the user's peak refresh rate
* ([peakRefreshCap]): a 120 Hz phone is switched to 120 Hz for the menu, not only when a game
* votes for it. With a frame cap, the fastest mode
* the cap divides evenly, so every capped frame is shown for the same number of refreshes;
* - GameManager's game state (Android 13+): "in gameplay", so an OEM framework that releases
* its boost on loading screens or menus keeps it up.
@@ -36,8 +39,11 @@ object PerfMode {
val display = if (Build.VERSION.SDK_INT >= 30) a.display else @Suppress("DEPRECATION") a.windowManager.defaultDisplay
if (display == null || Build.VERSION.SDK_INT < 23) "display mode unchanged" else {
val cur = display.mode
val cap = peakRefreshCap(a)
val sameSize = display.supportedModes
.filter { it.physicalWidth == cur.physicalWidth && it.physicalHeight == cur.physicalHeight }
.filter { it.refreshRate <= cap + 0.5f }
.ifEmpty { display.supportedModes.filter { it.modeId == cur.modeId } }
val best = sameSize.filter { cadenceFits(it.refreshRate, fpsLimit) }.maxByOrNull { it.refreshRate }
?: sameSize.maxByOrNull { it.refreshRate }
if (best != null && best.modeId != cur.modeId &&
@@ -68,6 +74,20 @@ object PerfMode {
return parts.joinToString(" · ")
}
/**
* The highest rate Android will run the panel at: the user's "peak refresh rate" (Smooth display,
* or the AYN/Odin quick-settings tile). Above it a mode can be requested and is even reported back
* as active, while SurfaceFlinger keeps composing at the cap. The Thor set to 60 Hz ran 120 Hz
* sessions at 60: gamescope and Steam were told 120, every game topped out at 60, and the QAM's
* Frame Limit read "60 FPS (120 Hz)" and changed nothing. Unset (most phones) is no cap.
*/
fun peakRefreshCap(context: Context): Float {
val peak = try {
Settings.System.getFloat(context.contentResolver, "peak_refresh_rate")
} catch (_: Throwable) { 0f }
return if (peak >= 1f) peak else Float.POSITIVE_INFINITY
}
/** A panel rate that shows every capped frame for a whole number of refreshes (no cap: any). */
fun cadenceFits(hz: Float, fpsLimit: Int): Boolean {
if (fpsLimit <= 0) return true