From e4a194b75288c0470b5d214def071619dd79b459 Mon Sep 17 00:00:00 2001 From: Hawk <28853569+WebF1yin@users.noreply.github.com> Date: Sun, 22 Nov 2026 16:05:28 -0800 Subject: [PATCH] The car's screen is drawn at the stream's frame rate on a clock of its own, instead of at the phone screen's 120 frames a second, so its animations stop keeping the phone busy for frames the car never shows Co-Authored-By: Claude Opus 5.5 --- .../gearslip/GearslipActivity.kt | 6 +- .../seb3thehacker/gearslip/GearslipRunner.kt | 4 +- .../app/seb3thehacker/gearslip/Projection.kt | 7 ++- .../seb3thehacker/gearslip/ScreenProjector.kt | 63 ++++++++++++++++++- .../gearslip/ConnectionLifecycleTest.kt | 2 +- 5 files changed, 74 insertions(+), 8 deletions(-) diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipActivity.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipActivity.kt index bcac7bd..c4043bf 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipActivity.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipActivity.kt @@ -59,6 +59,7 @@ class GearslipActivity : ComponentActivity(), Projection { private var frameWidth = 0 private var frameHeight = 0 private var frameDensity = 0 + private var frameRate = 0 /** * Screen capture always begins with a consent dialog, which only an activity can show - @@ -317,7 +318,7 @@ class GearslipActivity : ComponentActivity(), Projection { if (screenProjector != null) return val projector = ScreenProjector(this).also { screenProjector = it } // The frame's density, not frameDensity: that stays the car's own, for the phone mirror. - projector.start(target, frameWidth, frameHeight, CarEnvironment.frame.value.densityDpi) { CarUi() } + projector.start(target, frameWidth, frameHeight, CarEnvironment.frame.value.densityDpi, frameRate) { CarUi() } } /** @@ -507,12 +508,13 @@ class GearslipActivity : ComponentActivity(), Projection { moveTaskToBack(true) } - override fun onSurfaceReady(surface: Surface, width: Int, height: Int, densityDpi: Int) { + override fun onSurfaceReady(surface: Surface, width: Int, height: Int, densityDpi: Int, frameRate: Int) { runOnUiThread { this.surface = surface frameWidth = width frameHeight = height frameDensity = densityDpi + this.frameRate = frameRate CarEnvironment.setDisplay(width, height, densityDpi) CarEnvironment.start(this) showCarUi() diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipRunner.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipRunner.kt index 46b0e4b..fdd1fbc 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipRunner.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipRunner.kt @@ -1264,9 +1264,9 @@ class GearslipRunner( stretcher = stretch if (stretch != null) { drawSize = stretch.width to stretch.height - projection.onSurfaceReady(stretch.input, stretch.width, stretch.height, config.density) + projection.onSurfaceReady(stretch.input, stretch.width, stretch.height, config.density, videoFrameRate) } else { - projection.onSurfaceReady(surface, width, height, config.density) + projection.onSurfaceReady(surface, width, height, config.density, videoFrameRate) } SessionStatus.projecting() BluetoothAudioCheck.onProjecting() diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/Projection.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/Projection.kt index 6175b52..f701ff8 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/Projection.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/Projection.kt @@ -11,8 +11,11 @@ import android.view.Surface */ interface Projection { - /** Called once the encoder's input Surface exists and content should start rendering. */ - fun onSurfaceReady(surface: Surface, width: Int, height: Int, densityDpi: Int) + /** + * Called once the encoder's input Surface exists and content should start rendering, at + * no more than [frameRate] frames a second: the encoder takes no more. + */ + fun onSurfaceReady(surface: Surface, width: Int, height: Int, densityDpi: Int, frameRate: Int) /** * A touch from the head unit, already scaled into projected-video coordinates. diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/ScreenProjector.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/ScreenProjector.kt index 0f21f56..8674ba1 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/ScreenProjector.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/ScreenProjector.kt @@ -16,6 +16,9 @@ import android.view.ContextThemeWrapper import android.view.View import android.view.ViewGroup import androidx.compose.runtime.Composable +import androidx.compose.runtime.MonotonicFrameClock +import androidx.compose.runtime.Recomposer +import androidx.compose.ui.platform.AndroidUiDispatcher import androidx.compose.ui.platform.ComposeView import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner @@ -28,6 +31,10 @@ import androidx.savedstate.SavedStateRegistry import androidx.savedstate.SavedStateRegistryController import androidx.savedstate.SavedStateRegistryOwner import androidx.savedstate.setViewTreeSavedStateRegistryOwner +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.CoroutineStart +import kotlinx.coroutines.cancel +import kotlinx.coroutines.launch /** * Renders the car UI into the encoder's input Surface. @@ -44,6 +51,8 @@ class ScreenProjector(private val context: Context) { private var presentation: Presentation? = null private var owner: ViewTreeOwner? = null private var root: View? = null + private var recomposer: Recomposer? = null + private var recomposeScope: CoroutineScope? = null /** Tracks the synthetic gesture so injected MotionEvents have a coherent downTime. */ private var downTime = 0L @@ -58,6 +67,8 @@ class ScreenProjector(private val context: Context) { width: Int, height: Int, densityDpi: Int, + /** Frames a second the car UI is drawn at; 0 draws it at the display's own rate. */ + frameRate: Int = 0, name: String = "gearslip-projection", content: @Composable () -> Unit, ) { @@ -99,7 +110,27 @@ class ScreenProjector(private val context: Context) { decor.setViewTreeViewModelStoreOwner(treeOwner) decor.setBackgroundColor(Color.BLACK) - show.setContentView(ComposeView(withAppLanguage(show.context)).apply { setContent(content) }) + // The car UI is drawn on the main thread, which keeps time with the phone's own screen: + // 120 times a second on a Pixel, though the stream carries 30. Every animation (a song's + // progress bar, a sliding panel) was worked out and drawn up to four times for each frame + // the car got, keeping the main thread busy. Its own clock, ticking at the stream's rate, + // has it draw only what the car will show. The display itself keeps its own, faster rate: + // run at 30 too, its pickups and these draws drifted in and out of step, and a frame that + // finished just after a pickup waited a whole one, so animations stuttered. + val fps = if (frameRate > 0) frameRate.toFloat() else display.display.refreshRate + val clock = CarFrameClock(AndroidUiDispatcher.Main[MonotonicFrameClock]!!, fps) + val recomposeContext = AndroidUiDispatcher.Main + clock + val carRecomposer = Recomposer(recomposeContext) + val scope = CoroutineScope(recomposeContext) + scope.launch(start = CoroutineStart.UNDISPATCHED) { carRecomposer.runRecomposeAndApplyChanges() } + recomposer = carRecomposer + recomposeScope = scope + show.setContentView( + ComposeView(withAppLanguage(show.context)).apply { + setParentCompositionContext(carRecomposer) + setContent(content) + }, + ) show.show() presentation = show @@ -249,6 +280,10 @@ class ScreenProjector(private val context: Context) { fun stop() { runCatching { presentation?.dismiss() } + runCatching { recomposer?.cancel() } + runCatching { recomposeScope?.cancel() } + recomposer = null + recomposeScope = null runCatching { owner?.destroy() } runCatching { virtualDisplay?.release() } presentation = null @@ -277,3 +312,29 @@ class ScreenProjector(private val context: Context) { } } } + +/** + * Frames from the phone's own clock, but no more than [fps] of them a second: the rest are let + * pass, so whatever waits on a frame (animations, recomposition) runs at the car display's rate. + */ +private class CarFrameClock(private val phone: MonotonicFrameClock, fps: Float) : MonotonicFrameClock { + // A little short of a whole gap, so a 120 Hz phone's every fourth frame still counts at 30. + private val gapNs = (1_000_000_000L / fps.coerceIn(1f, 240f)).toLong() - 2_000_000L + private var lastNs = 0L + + override suspend fun withFrameNanos(onFrame: (frameTimeNanos: Long) -> R): R { + while (true) { + val ran = phone.withFrameNanos { now -> + if (now - lastNs < gapNs) { + null + } else { + lastNs = now + Ran(onFrame(now)) + } + } + if (ran != null) return ran.value + } + } + + private class Ran(val value: R) +} diff --git a/gearslip/src/test/java/app/seb3thehacker/gearslip/ConnectionLifecycleTest.kt b/gearslip/src/test/java/app/seb3thehacker/gearslip/ConnectionLifecycleTest.kt index 5575ad6..593bf5e 100644 --- a/gearslip/src/test/java/app/seb3thehacker/gearslip/ConnectionLifecycleTest.kt +++ b/gearslip/src/test/java/app/seb3thehacker/gearslip/ConnectionLifecycleTest.kt @@ -234,7 +234,7 @@ class ConnectionLifecycleTest { @Test fun `touchpad reports are not injected as touchscreen taps`() { var touches = 0 val projection = object : Projection { - override fun onSurfaceReady(surface: Surface, width: Int, height: Int, densityDpi: Int) = Unit + override fun onSurfaceReady(surface: Surface, width: Int, height: Int, densityDpi: Int, frameRate: Int) = Unit override fun onTouch(action: Int, actionIndex: Int, points: List) { touches++ } override fun onProjectionStopped() = Unit }