feat(display): black frame insertion on 100 Hz and faster screens (#2653)

* feat(display): black frame insertion on 100 Hz and faster screens

* feat(display): black frame insertion lives in Screen effects

* test(compositor): cover black frame insertion in the render-on-commit harness

* fix(display): keep HDR and pacing intact around black frame insertion

* fix(display): gate black frame insertion on the panel's refresh, not the tick rate

BFI checked the 100 Hz minimum against g_refresh_ns, an average of the time
between handled vsync ticks. BFI presents on every tick, so under load the
ticks run late, the average fell to 95-100 Hz on a 120 Hz panel, and BFI
switched off and on again many times a second (3793 times in one session on
a Retroid Pocket Duo). That showed as uneven flicker.

Use the display mode's refresh (g_output_refresh_mhz) instead, which only
changes when the mode does.

* fix(display): offer black frame insertion for the bottom screen and while it is on

* fix(display): capture a picture, not a black refresh, while black frame insertion strobes

* fix(display): hold black frame insertion off until the last capture ends

* fix(display): keep the game composed while a capture holds black frame insertion

* test(display): a picture hold presents only pictures while black frame insertion runs
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-16 07:33:39 -05:00
1 parent f65e7a3d43
commit 758e7983fa
46 files changed
+343 -29

No files matched your search

@@ -500,6 +500,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
private var drawerGameProfileId by mutableStateOf<Long?>(null)
private var drawerGameProfileSelectionMade = false
private var fpsLimit by mutableStateOf(SessionState.fpsLimit)
private var blackFrameInsertion by mutableStateOf(false)
private fun noSteamSession(): Boolean = (SessionState.mode == SessionService.MODE_RUN &&
SessionState.program in setOf(
@@ -1588,6 +1589,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
fpsLimit = fps
applyOutputPacing()
},
blackFrameInsertion = blackFrameInsertion,
blackFrameInsertionAvailable = blackFrameInsertion || blackFrameInsertionAvailable(),
onBlackFrameInsertion = { on ->
SessionPrefs.setBlackFrameInsertion(this@SessionActivity, on)
blackFrameInsertion = on
CompositorHost.setBlackFrameInsertion(on, refreshHz(), outputFpsLimit())
},
onFrameGenPick = { mode ->
val appId = frameGenTargetGameId()
@@ -2402,6 +2410,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
picturePosition = SessionPrefs.picturePosition(this)
integerScaling = SessionPrefs.integerScaling(this)
hudOn = SessionPrefs.hudEnabled(this)
blackFrameInsertion = SessionPrefs.blackFrameInsertion(this)
hudConfig = DroidDeckHudConfig.load(this)
hudColor = SessionPrefs.hudColor(this)
hudOpacity = SessionPrefs.hudOpacity(this)
@@ -2871,6 +2880,17 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
return if (capped > 1f) capped else 60f
}
private fun blackFrameInsertionAvailable(): Boolean {
// Glasses that present the game run at their own rate, uncapped, as refreshHz() reads them.
glassesScreen?.display?.let { glasses ->
return (glasses.supportedModes?.maxOfOrNull { it.refreshRate } ?: 0f) >= PerfMode.BFI_MIN_REFRESH_HZ
}
val display = bottomGame?.takeIf { bottomOutputActive }?.display
?: if (Build.VERSION.SDK_INT >= 30) display else @Suppress("DEPRECATION") windowManager.defaultDisplay
val peak = display?.supportedModes?.maxOfOrNull { it.refreshRate } ?: 0f
return minOf(peak, PerfMode.peakRefreshCap(this)) >= PerfMode.BFI_MIN_REFRESH_HZ
}
private fun mirrorRefreshLimit(): Int =
GlassesRendering.mirrorRefreshLimit(glassesSettings, glasses, glassesScreen != null)
@@ -4194,19 +4214,35 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
onFrame(null)
return
}
try {
android.view.PixelCopy.request(source, bitmap, { result ->
if (result != android.view.PixelCopy.SUCCESS) {
bitmap.recycle()
onFrame(null)
} else onFrame(bitmap)
}, uiHandler)
} catch (_: IllegalArgumentException) {
bitmap.recycle()
onFrame(null)
withPictureFrame { done ->
try {
android.view.PixelCopy.request(source, bitmap, { result ->
done()
if (result != android.view.PixelCopy.SUCCESS) {
bitmap.recycle()
onFrame(null)
} else onFrame(bitmap)
}, uiHandler)
} catch (_: IllegalArgumentException) {
done()
bitmap.recycle()
onFrame(null)
}
}
}
/** PixelCopy reads the last queued buffer, which black frame insertion makes black every other refresh. */
private fun withPictureFrame(copy: (done: () -> Unit) -> Unit) {
if (!CompositorHost.holdPicture()) return copy {}
uiHandler.postDelayed({
var released = false
copy {
if (!released) CompositorHost.releasePicture()
released = true
}
}, BFI_PICTURE_SETTLE_MS)
}
private fun tapSteamButton() {
if (SessionState.mode == SessionService.MODE_STEAM) padBridge?.triggerSteamMenu()
}
@@ -5311,17 +5347,21 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
val left = surfaceAt[0] - overlayAt[0]
val top = surfaceAt[1] - overlayAt[1]
val bounds = android.graphics.Rect(left, top, left + width, top + height)
try {
android.view.PixelCopy.request(surfaceView, bitmap, { result ->
if (result == android.view.PixelCopy.SUCCESS && !isDestroyed) {
pausedFrame = com.droiddeck.launcher.ui.PausedFrame(bitmap.asImageBitmap(), bounds)
pausedFrameWanted = false
} else {
bitmap.recycle()
}
}, uiHandler)
} catch (_: IllegalArgumentException) {
bitmap.recycle()
withPictureFrame { done ->
try {
android.view.PixelCopy.request(surfaceView, bitmap, { result ->
done()
if (result == android.view.PixelCopy.SUCCESS && !isDestroyed) {
pausedFrame = com.droiddeck.launcher.ui.PausedFrame(bitmap.asImageBitmap(), bounds)
pausedFrameWanted = false
} else {
bitmap.recycle()
}
}, uiHandler)
} catch (_: IllegalArgumentException) {
done()
bitmap.recycle()
}
}
}
@@ -6323,6 +6363,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
private const val LEAVE_TIMEOUT_MS = 1_500L
private const val TEXT_FIELD_TAP_MS = 1_000L
private const val PAUSED_FRAME_MAX_EDGE = 1920f
private const val BFI_PICTURE_SETTLE_MS = 50L
private const val PAUSED_FRAME_REDRAW_MS = 150L
private const val BOTTOM_RECOVERY_MS = 1_000L
private const val BOTTOM_RECOVERY_TRIES = 3
@@ -11,6 +11,7 @@ import android.view.Display
object PerfMode {
private const val TAG = "PerfMode"
const val BFI_MIN_REFRESH_HZ = 100f
fun apply(a: Activity, fpsLimit: Int = 0, refreshLimit: Int = 0): String {
val parts = ArrayList<String>(2)
@@ -85,6 +85,12 @@ object SessionPrefs {
prefs(context).edit().putBoolean("launcherFullscreen", on).apply()
}
fun blackFrameInsertion(context: Context): Boolean = prefs(context).getBoolean("blackFrameInsertion", false)
fun setBlackFrameInsertion(context: Context, on: Boolean) {
prefs(context).edit().putBoolean("blackFrameInsertion", on).apply()
}
fun animationsEnabled(context: Context): Boolean = prefs(context).getBoolean("animations", true)
fun setAnimationsEnabled(context: Context, on: Boolean) {
@@ -24,7 +24,7 @@ object SettingsReset {
"directAudio", "mic", "wifiDiscovery", "clientCpusOverride", "clientCpus", "gameCpus",
"noXalia", "fastSync", "fsyncFirst", "syncFallback", "prootNoSeccomp", "prootFastPath", "nativeUserland", "cursorComposite",
"guestHostname", "fexPreset", "forceSsbs", "logs", "gameDebugLogsEnabled", "enhancedDiagnosticsNext", "storageDiagnostics", "buildWatermark",
"upscaler", "upscaleSharpness", "screenEffects", "textureAnisotropy", "textureLodBias",
"upscaler", "upscaleSharpness", "screenEffects", "blackFrameInsertion", "textureAnisotropy", "textureLodBias",
"steamDownloadsInBackground", "steam_offline", "bootMovie", "bootMovieSound", "bootMovieVolume", "bootMovieSeconds", "skipBootMovie",
"bootMovieApplyNext", "bootMovieSteamSeen", "bootMovieShuffle",
"questDefaultsV1", "telemetryAuto", "telemetryOff", "statsStrip", "statsHud", "bottomHud", "bottomStripMetrics", "bottomStripTabs", "statsDash", "statsSpeedUnit",
@@ -275,6 +275,9 @@ class DrawerActions(
val onFrameGenReset: () -> Unit = {},
val fpsLimit: Int = 0,
val onFpsLimit: (Int) -> Unit = {},
val blackFrameInsertion: Boolean = false,
val blackFrameInsertionAvailable: Boolean = false,
val onBlackFrameInsertion: (Boolean) -> Unit = {},
val onImportLossless: () -> Unit,
val onKeyboard: () -> Unit,
val onHardwareKeyboard: () -> Unit,
@@ -1112,6 +1115,9 @@ private fun ScreenEffectsGroup(
ToggleRow(host, "crt", stringResource(R.string.drawer_crt), null, e.crt, chipModifier = track("crt")) { a.onEffects(e.copy(crt = it)) }
ToggleRow(host, "ntsc", stringResource(R.string.drawer_ntsc), null, e.ntsc, chipModifier = track("ntsc")) { a.onEffects(e.copy(ntsc = it)) }
ToggleRow(host, "fake-hdr", stringResource(R.string.drawer_fake_hdr), null, e.hdr, chipModifier = track("fake-hdr")) { a.onEffects(e.copy(hdr = it)) }
if (a.blackFrameInsertionAvailable) ToggleRow(host, "bfi", stringResource(R.string.drawer_bfi),
stringResource(R.string.drawer_bfi_note), a.blackFrameInsertion,
chipModifier = track("bfi"), onChange = a.onBlackFrameInsertion)
}
val source = remember { MutableInteractionSource() }
val hot = source.collectIsFocusedAsState().value || source.collectIsHoveredAsState().value
@@ -53,6 +53,8 @@ object CompositorHost {
WaylandCompositor.nativeSetClockControl(java.io.File(context.filesDir, "${com.droiddeck.launcher.runtime.LinuxRuntime.DIR}/tmp/.bl-clock").path)
ownVsyncThread = SessionPrefs.vsyncThread(context)
WaylandCompositor.nativeSetRenderOnCommit(SessionPrefs.renderOnCommit(context))
blackFrameInsertion = SessionPrefs.blackFrameInsertion(context)
WaylandCompositor.nativeSetBlackFrameInsertion(blackFrameInsertion)
pace(refreshHz, fpsLimit)
if (!SessionState.running) WaylandCompositor.nativeSetOutputSize(outputWidth, outputHeight)
if (started) {
@@ -72,9 +74,12 @@ object CompositorHost {
return true
}
private var blackFrameInsertion = false
private var pictureHolds = 0
private fun pace(refreshHz: Float, fpsLimit: Int) {
WaylandCompositor.nativeSetFpsLimit(fpsLimit)
WaylandCompositor.nativeSetLayerFrameRate(if (fpsLimit > 0) fpsLimit.toFloat() else refreshHz)
WaylandCompositor.nativeSetLayerFrameRate(if (fpsLimit > 0 && !blackFrameInsertion) fpsLimit.toFloat() else refreshHz)
WaylandCompositor.nativeSetOutputRefreshRate(refreshHz)
SessionState.refreshHz = refreshHz
}
@@ -84,6 +89,27 @@ object CompositorHost {
if (started) pace(refreshHz, fpsLimit)
}
@Synchronized
fun setBlackFrameInsertion(on: Boolean, refreshHz: Float, fpsLimit: Int) {
blackFrameInsertion = on
WaylandCompositor.nativeSetBlackFrameInsertion(on)
if (started) pace(refreshHz, fpsLimit)
}
/** Presents only pictures until the matching release; false when nothing is strobing. */
@Synchronized
fun holdPicture(): Boolean {
if (!started || !blackFrameInsertion) return false
if (pictureHolds++ == 0) WaylandCompositor.nativeSetPictureHold(true)
return true
}
@Synchronized
fun releasePicture() {
if (pictureHolds == 0) return
if (--pictureHolds == 0) WaylandCompositor.nativeSetPictureHold(false)
}
@Synchronized
fun resize(surface: Surface, rearm: () -> Unit) {
if (!started) return
@@ -321,6 +321,10 @@ public final class WaylandCompositor {
public static native void nativeSetFpsLimit(int fps);
public static native void nativeSetBlackFrameInsertion(boolean on);
public static native void nativeSetPictureHold(boolean on);
public static native void nativeSetRenderOnCommit(boolean on);
public static native void nativeSetGpuTurbo(boolean on);