Skip to content

armgit / andee

Public Imported from GitHub
0

Andee lives on your phone or Mac and uses any app like you do—no integrations, no root, any model; memory and passwords never leave your device | Andee 住进你的手机或 Mac,像你一样操作任何 App——无需集成、无需 Root、支持任意模型,记忆和密码绝不离开设备。

perf(mac): the ball always runs at 30 fps

booboosui8591fad42467
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/BallRenderer.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/BallRenderer.kt
index 87798d5..1e141d7 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/BallRenderer.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/BallRenderer.kt
@@ -30,24 +30,12 @@ import kotlin.concurrent.thread
* paints. GL calls stay on this thread; the knobs are queued to it, so the
* renderer's own `@Volatile` contract is kept exactly as on the phone.
*/
-class BallRenderer(val width: Int, val height: Int, calmFrameNs: Long = CALM_FRAME_NS) {
-
- /**
- * `-Dandee.ballCalmMs=<ms>` overrides every ball's resting rate, whatever the
- * caller asked for. Only for measuring: a ball that is forced to 30 fps is a
- * constant stream of whole-window repaints, which is the only way to price one
- * repaint without being able to drive the UI by hand.
- */
- private val calmFrameNs: Long = System.getProperty("andee.ballCalmMs")
- ?.toLongOrNull()?.let { it * 1_000_000 } ?: calmFrameNs
+class BallRenderer(val width: Int, val height: Int) {
private val renderer = EmotionBallRenderer()
private val commands = ConcurrentLinkedQueue<() -> Unit>()
@Volatile private var stopRequested = false
- /** Until this moment the ball is doing something, so it runs at full rate. */
- @Volatile private var activeUntilNs = 0L
-
/** The latest finished frame: premultiplied BGRA bytes, top row first. Null until the first one. */
@Volatile var frame: ByteArray? = null
private set
@@ -112,16 +100,7 @@ class BallRenderer(val width: Int, val height: Int, calmFrameNs: Long = CALM_FRA
while (!stopRequested) {
val t0 = System.nanoTime()
while (true) commands.poll()?.invoke() ?: break
- val delay = frameDelayNs()
- // `frame == null` is the very first pass: there is nothing to hold yet,
- // so a ball that idles by holding must still draw once or it is never
- // there at all.
- if (delay == HOLD && frame != null) {
- // Holding the last frame: draw nothing, but wake often enough that
- // a change of face is picked up in POLL_MS, not a calm period later.
- if (!nap(POLL_NS)) break
- continue
- }
+ val delay = FRAME_NS
val g0 = System.nanoTime()
renderer.onDrawFrame()
val g1 = System.nanoTime()
@@ -186,39 +165,16 @@ class BallRenderer(val width: Int, val height: Int, calmFrameNs: Long = CALM_FRA
renderer.mood = mood
renderer.listening = listening
renderer.emotion = emotion
- markActive()
}
- // Also a reason to draw: the face can change while the mood stays calm, and a
- // ball that holds its last frame would keep the old face.
- fun setEmotion(m: Mood?) = post { renderer.emotion = m; markActive() }
- fun setWorking(w: Boolean) = post { renderer.working = w; if (w) markActive() }
- fun pulseTool() = post { renderer.pulseTool(); markActive() }
- fun signalOutcome(ok: Boolean) = post { renderer.signalOutcome(ok); markActive() }
- fun trigger(action: Action) = post { renderer.request(action); markActive() }
+ fun setEmotion(m: Mood?) = post { renderer.emotion = m }
+ fun setWorking(w: Boolean) = post { renderer.working = w }
+ fun pulseTool() = post { renderer.pulseTool() }
+ fun signalOutcome(ok: Boolean) = post { renderer.signalOutcome(ok) }
+ fun trigger(action: Action) = post { renderer.request(action) }
private fun post(f: () -> Unit) { commands.offer(f); java.util.concurrent.locks.LockSupport.unpark(thread) }
- /**
- * Full rate while there is something to watch — listening, speaking, thinking,
- * or an animation just started — and a slow breath when there is not.
- *
- * This is the single biggest lever on a Mac that renders in software: there,
- * every frame the ball publishes costs a fresh raster and blit of the whole
- * window it lives in, whether or not anything else changed. Measured idle,
- * a 30 fps ball kept the UI thread busy ~15% of the time doing nothing but
- * redrawing an unchanged window, and every click queued behind it. The
- * breathing reads the same at 4 fps as at 30.
- */
- private fun frameDelayNs(): Long = when {
- System.nanoTime() < activeUntilNs -> FRAME_NS
- renderer.mood == Mood.CALM && !renderer.working && !renderer.listening -> calmFrameNs
- else -> FRAME_NS
- }
-
- /** Hold full rate for a beat, so a pop or a nod finishes before it slows. */
- private fun markActive() { activeUntilNs = System.nanoTime() + ACTIVE_NS }
-
/**
* Ask the render thread to finish and return at once. It used to `join(2000)` on the caller, which is
* the UI thread (this runs from a view's onDispose): a thread parked in its frame sleep, or in a GL call,
@@ -236,22 +192,10 @@ class BallRenderer(val width: Int, val height: Int, calmFrameNs: Long = CALM_FRA
private val TRACE = System.getProperty("andee.ballTrace") != null
const val TAG = "Ball"
/**
- * 30 fps. The ball's motion is slow springs and breathing, which read the
- * same at 30 as at 60; the frame is then read back and handed to another
- * window, so every frame costs twice, and halving them is most of the CPU.
+ * 30 fps, always. The ball used to drop to 4 fps when it was only breathing; the motion then looked
+ * choppy (the springs and the breathing are the animation), so it no longer does. A frame costs ~2 ms
+ * on the render thread and ~0.2 ms on the UI thread, measured.
*/
const val FRAME_NS = 33_333_333L
-
- /** 4 fps: the rate of a ball that is only breathing. */
- const val CALM_FRAME_NS = 250_000_000L
-
- /** How long a triggered animation keeps the full rate. */
- const val ACTIVE_NS = 1_200_000_000L
-
- /** Passed as [calmFrameNs] to stop drawing entirely at rest; see [frameDelayNs]. */
- const val HOLD = 0L
-
- /** While holding, how often to wake and check whether the face changed. */
- const val POLL_NS = 100_000_000L
}
}
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/MacBall.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/MacBall.kt
index ca439fd..fb7f5ee 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/MacBall.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/MacBall.kt
@@ -139,16 +139,9 @@ fun BallView(
size: Dp,
onClick: (() -> Unit)?,
modifier: Modifier = Modifier,
- /**
- * The rate it breathes at when nothing is happening. Passed in because what a
- * frame costs depends on the window the ball sits in, not on the ball: inside
- * the chat window a frame redraws the whole conversation, so that one idles
- * slower than the floating ball, which is 112 points of its own window.
- */
- calmFrameNs: Long = BallRenderer.CALM_FRAME_NS,
) {
val px = with(LocalDensity.current) { size.roundToPx() }
- val renderer = remember(px, calmFrameNs) { BallRenderer(px, px, calmFrameNs) }
+ val renderer = remember(px) { BallRenderer(px, px) }
DisposableEffect(renderer) {
MacBall.attach(renderer)
onDispose { MacBall.detach(renderer); renderer.stop() }