perf(compositor): screen tick on its own thread, pacing counters (#1678)

The Choreographer frame callback that drives the compositor ran on the
app's main thread, so main-thread work delayed the tick before it
reached the compositor thread. It now runs on a display-priority
HandlerThread with its own Choreographer; the session pref vsyncThread
(default true) switches back to the main thread. Each resume posts a new
callback with its own generation, so one left queued from an earlier
resume, on either thread, stops itself instead of ticking twice.

A new 10 s "pacing" log line reports how late the tick reaches the
compositor, how long a new frame waits to be drawn, game frames against
buffers re-attached after an empty attach, empty attaches and distinct
buffers, and presentation feedback shown, replaced before drawing and
not drawn.
This commit is contained in:
The412Banner authored and GitHub committed 2026-10-27 12:32:15 -04:00
1 parent 378beacfac
commit 067dabe81c
3 files changed
+90 -6

No files matched your search

@@ -602,6 +602,7 @@ object SessionPrefs {
}
fun mangoapp(context: Context): Boolean = prefs(context).getBoolean("mangoapp", true)
fun vsyncThread(context: Context): Boolean = prefs(context).getBoolean("vsyncThread", true)
fun setMangoapp(context: Context, on: Boolean) { prefs(context).edit().putBoolean("mangoapp", on).apply() }
fun zinkLazy(context: Context): Boolean = prefs(context).getBoolean("zinkLazy", true)
@@ -1,7 +1,11 @@
package com.droiddeck.launcher.wayland
import android.os.Handler
import android.os.HandlerThread
import android.os.Process
import android.view.Choreographer
import android.view.Surface
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.SessionState
object CompositorHost {
@@ -14,8 +18,22 @@ object CompositorHost {
@Volatile
private var vsyncRunning = false
private val vsync = object : Choreographer.FrameCallback {
@Volatile
private var ownVsyncThread = true
// The screen tick gets its own thread so a busy main thread cannot delay the compositor's frames.
private val vsyncThread: HandlerThread by lazy {
HandlerThread("droiddeck-vsync", Process.THREAD_PRIORITY_DISPLAY).apply { start() }
}
private val vsyncHandler: Handler by lazy { Handler(vsyncThread.looper) }
@Volatile
private var vsyncGeneration = 0
// Each resume gets a new callback; one left queued from an earlier resume, on either thread, stops itself.
private fun vsyncCallback(generation: Int) = object : Choreographer.FrameCallback {
override fun doFrame(frameTimeNanos: Long) {
if (generation != vsyncGeneration) return
WaylandCompositor.nativeVsync(frameTimeNanos)
if (vsyncRunning) Choreographer.getInstance().postFrameCallback(this)
}
@@ -34,6 +52,7 @@ object CompositorHost {
refreshHz: Float,
fpsLimit: Int,
): Boolean {
ownVsyncThread = SessionPrefs.vsyncThread(context)
pace(refreshHz, fpsLimit)
if (!SessionState.running) WaylandCompositor.nativeSetOutputSize(outputWidth, outputHeight)
if (started) {
@@ -103,8 +122,14 @@ object CompositorHost {
private fun resumeVsync() {
if (vsyncRunning) return
val callback = vsyncCallback(++vsyncGeneration)
vsyncRunning = true
Choreographer.getInstance().postFrameCallback(vsync)
WaylandCompositor.nativeLog("screen", if (ownVsyncThread) "screen tick runs on its own display-priority thread" else "screen tick runs on the main thread")
if (!ownVsyncThread) {
Choreographer.getInstance().postFrameCallback(callback)
return
}
vsyncHandler.post { Choreographer.getInstance().postFrameCallback(callback) }
}
private fun pauseVsync() {