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 <noreply@anthropic.com>
This commit is contained in:
HawkandClaude Opus 5.5 committed 2026-11-22 19:25:09 -08:00
1 parent 4e3f8e6657
commit e4a194b752
5 files changed
+74 -8

No files matched your search

@@ -59,6 +59,7 @@ class GearslipActivity : ComponentActivity(), Projection {
private var frameWidth = 0 private var frameWidth = 0
private var frameHeight = 0 private var frameHeight = 0
private var frameDensity = 0 private var frameDensity = 0
private var frameRate = 0
/** /**
* Screen capture always begins with a consent dialog, which only an activity can show - * 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 if (screenProjector != null) return
val projector = ScreenProjector(this).also { screenProjector = it } val projector = ScreenProjector(this).also { screenProjector = it }
// The frame's density, not frameDensity: that stays the car's own, for the phone mirror. // 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) 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 { runOnUiThread {
this.surface = surface this.surface = surface
frameWidth = width frameWidth = width
frameHeight = height frameHeight = height
frameDensity = densityDpi frameDensity = densityDpi
this.frameRate = frameRate
CarEnvironment.setDisplay(width, height, densityDpi) CarEnvironment.setDisplay(width, height, densityDpi)
CarEnvironment.start(this) CarEnvironment.start(this)
showCarUi() showCarUi()
@@ -1264,9 +1264,9 @@ class GearslipRunner(
stretcher = stretch stretcher = stretch
if (stretch != null) { if (stretch != null) {
drawSize = stretch.width to stretch.height 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 { } else {
projection.onSurfaceReady(surface, width, height, config.density) projection.onSurfaceReady(surface, width, height, config.density, videoFrameRate)
} }
SessionStatus.projecting() SessionStatus.projecting()
BluetoothAudioCheck.onProjecting() BluetoothAudioCheck.onProjecting()
@@ -11,8 +11,11 @@ import android.view.Surface
*/ */
interface Projection { 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. * A touch from the head unit, already scaled into projected-video coordinates.
@@ -16,6 +16,9 @@ import android.view.ContextThemeWrapper
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.compose.runtime.Composable 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.compose.ui.platform.ComposeView
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
@@ -28,6 +31,10 @@ import androidx.savedstate.SavedStateRegistry
import androidx.savedstate.SavedStateRegistryController import androidx.savedstate.SavedStateRegistryController
import androidx.savedstate.SavedStateRegistryOwner import androidx.savedstate.SavedStateRegistryOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner 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. * 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 presentation: Presentation? = null
private var owner: ViewTreeOwner? = null private var owner: ViewTreeOwner? = null
private var root: View? = 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. */ /** Tracks the synthetic gesture so injected MotionEvents have a coherent downTime. */
private var downTime = 0L private var downTime = 0L
@@ -58,6 +67,8 @@ class ScreenProjector(private val context: Context) {
width: Int, width: Int,
height: Int, height: Int,
densityDpi: 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", name: String = "gearslip-projection",
content: @Composable () -> Unit, content: @Composable () -> Unit,
) { ) {
@@ -99,7 +110,27 @@ class ScreenProjector(private val context: Context) {
decor.setViewTreeViewModelStoreOwner(treeOwner) decor.setViewTreeViewModelStoreOwner(treeOwner)
decor.setBackgroundColor(Color.BLACK) 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() show.show()
presentation = show presentation = show
@@ -249,6 +280,10 @@ class ScreenProjector(private val context: Context) {
fun stop() { fun stop() {
runCatching { presentation?.dismiss() } runCatching { presentation?.dismiss() }
runCatching { recomposer?.cancel() }
runCatching { recomposeScope?.cancel() }
recomposer = null
recomposeScope = null
runCatching { owner?.destroy() } runCatching { owner?.destroy() }
runCatching { virtualDisplay?.release() } runCatching { virtualDisplay?.release() }
presentation = null 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 <R> 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<R>(val value: R)
}
@@ -234,7 +234,7 @@ class ConnectionLifecycleTest {
@Test fun `touchpad reports are not injected as touchscreen taps`() { @Test fun `touchpad reports are not injected as touchscreen taps`() {
var touches = 0 var touches = 0
val projection = object : Projection { 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<TouchPoint>) { touches++ } override fun onTouch(action: Int, actionIndex: Int, points: List<TouchPoint>) { touches++ }
override fun onProjectionStopped() = Unit override fun onProjectionStopped() = Unit
} }