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 stops repainting a still window, and the GPU draws it again

booboosuif6a9ceeeb840
diff --git a/build.gradle b/build.gradle
index ab2e716..fe92dd6 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,9 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
alias(libs.plugins.androidApplication) apply false
-id 'org.jetbrains.kotlin.android' version '1.9.22' apply false
-id 'org.jetbrains.kotlin.jvm' version '1.9.22' apply false
-// 1.6.0 is the Compose release built against Kotlin 1.9.22; moving either
-// one means moving all three modules together.
-id 'org.jetbrains.compose' version '1.6.0' apply false
+id 'org.jetbrains.kotlin.android' version '2.1.21' apply false
+id 'org.jetbrains.kotlin.jvm' version '2.1.21' apply false
+// Compose 1.9 is the release built against Kotlin 2.1; moving either one means
+// moving all three modules together. The version matters beyond API changes:
+// skiko 0.7.93 (Compose 1.6) crashes in its Metal backend on Intel Macs under
+// macOS 26, which forced this app to render the whole window on the CPU.
+id 'org.jetbrains.compose' version '1.9.3' apply false
+// Kotlin 2.0 moved the Compose compiler out of the Kotlin plugin into its own.
+id 'org.jetbrains.kotlin.plugin.compose' version '2.1.21' apply false
}
\ No newline at end of file
diff --git a/desktop/build.gradle b/desktop/build.gradle
index d50c89b..adbfc2b 100644
--- a/desktop/build.gradle
+++ b/desktop/build.gradle
@@ -7,6 +7,9 @@ import org.jetbrains.compose.desktop.application.dsl.TargetFormat
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'org.jetbrains.compose'
+ // Required since Kotlin 2.0: the Compose compiler is no longer part of the
+ // Kotlin plugin.
+ id 'org.jetbrains.kotlin.plugin.compose'
}
kotlin {
@@ -161,6 +164,36 @@ tasks.register('signApp') {
logger.lifecycle("Andee.app signed as '${signIdentity.get()}'")
}
}
+// Render at 1x instead of 2x, for a machine that has to draw the window on the CPU
+// (-Dandee.software=true): at 2x a 1280x909-point window is 2560x1818 pixels of Skia
+// work per frame, at 1x a quarter of that.
+//
+// Opt in with -PoneX, and not by default: measured on this Mac, 1x bought no reliable
+// saving (670/694 ms vs 681/372 ms of UI-thread raster per 5 s — the upscale moves to
+// the WindowServer, which is outside this process), and it costs sharpness, since the
+// display then stretches a half-resolution image over the same area. A smaller window
+// buys the same pixels back without that trade (see defaultChatSize).
+tasks.register('patchInfoPlist') {
+ def app = layout.buildDirectory.dir('compose/binaries/main/app/Andee.app')
+ def oneX = project.hasProperty('oneX')
+ onlyIf { oneX && osArch != 'aarch64' && app.get().asFile.exists() }
+ doLast {
+ def plist = "${app.get().asFile.path}/Contents/Info.plist"
+ exec {
+ // plutil, not PlistBuddy: PlistBuddy writes the value as the *string*
+ // "false", and AppKit reads any non-nil value as true, so the key looks
+ // right in the plist and does nothing. It has to be a real bool.
+ // '-replace' throws if the key is missing; then insert it instead.
+ commandLine 'sh', '-c',
+ "/usr/bin/plutil -replace NSHighResolutionCapable -bool false '$plist' || " +
+ "/usr/bin/plutil -insert NSHighResolutionCapable -bool false '$plist'"
+ }
+ logger.lifecycle('Andee.app renders at 1x (NSHighResolutionCapable=false)')
+ }
+}
+tasks.matching { it.name == 'createDistributable' }.configureEach { finalizedBy('patchInfoPlist') }
+// Signing seals the bundle, so it has to come after the plist is changed.
+tasks.named('signApp') { dependsOn('patchInfoPlist') }
tasks.matching { it.name == 'createDistributable' }.configureEach { finalizedBy('signApp') }
// The real window body, drawn offscreen against seeded throwaway data:
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Backdrop.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Backdrop.kt
index 5d3bdd2..962f8f5 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Backdrop.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Backdrop.kt
@@ -55,6 +55,15 @@ object Backdrop {
fun build(): Built? {
built?.let { return it }
+ // Only when the CPU is doing the drawing: nothing is retained between
+ // frames, so scaling this 512px photograph up to the whole window is redone
+ // every frame — measured at ~60% of all raster work (bilerp_clamp_8888 plus
+ // the full-window veil blend). On the GPU the same scaling is free, and the
+ // photo is what makes the cards read as glass, so keep it there.
+ if (System.getProperty("skiko.renderApi") == "SOFTWARE") {
+ Log.i("Backdrop", "software renderer: plain wash instead of the photograph")
+ return null
+ }
val src = runCatching {
Backdrop::class.java.getResourceAsStream("/backdrop.jpg")?.use { ImageIO.read(it) }
}.getOrNull() ?: run { Log.w("Backdrop", "backdrop.jpg missing; surfaces keep their plain fill"); return null }
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 cfe7f4e..0fa3d7e 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/BallRenderer.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/BallRenderer.kt
@@ -30,12 +30,24 @@ 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) {
+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
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
@@ -83,12 +95,22 @@ class BallRenderer(val width: Int, val height: Int) {
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.
+ Thread.sleep(POLL_NS)
+ continue
+ }
renderer.onDrawFrame()
buf.clear()
GL11.glReadPixels(0, 0, width, height, org.lwjgl.opengl.GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, buf)
frame = flipped(buf)
onFrame()
- val sleep = (FRAME_NS - (System.nanoTime() - t0)) / 1_000_000
+ val sleep = (delay - (System.nanoTime() - t0)) / 1_000_000
if (sleep > 0) Thread.sleep(sleep)
}
FBO.glDeleteRenderbuffers(depth)
@@ -141,22 +163,45 @@ class BallRenderer(val width: Int, val height: Int) {
renderer.mood = mood
renderer.listening = listening
renderer.emotion = emotion
+ 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) }
+ // 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() }
private fun post(f: () -> Unit) { commands.offer(f) }
+ /**
+ * 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 }
+
fun stop() {
stopRequested = true
thread.join(2000)
}
- private companion object {
+ companion object {
const val TAG = "Ball"
/**
* 30 fps. The ball's motion is slow springs and breathing, which read the
@@ -164,5 +209,17 @@ class BallRenderer(val width: Int, val height: Int) {
* window, so every frame costs twice, and halving them is most of the CPU.
*/
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/ChatScreen.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ChatScreen.kt
index fadc67d..264d6e8 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ChatScreen.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/ChatScreen.kt
@@ -16,7 +16,9 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.ui.focus.focusRequester
import androidx.compose.foundation.layout.Column
import androidx.compose.ui.draw.clip
-import androidx.compose.ui.onExternalDrag
+import androidx.compose.ui.draganddrop.DragAndDropEvent
+import androidx.compose.ui.draganddrop.DragAndDropTarget
+import androidx.compose.foundation.draganddrop.dragAndDropTarget
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
@@ -202,15 +204,28 @@ fun ChatScreen(onOpenArtifacts: () -> Unit, onOpenTopic: (Long) -> Unit = {}, on
Thread { Assistant.say(t, attachments = sending) }.start()
}
+ // Compose moved drag-and-drop into androidx.compose.ui.draganddrop (1.7+). The
+ // files themselves come from the AWT transferable, whose javaFileListFlavor is
+ // the one source of truth on the desktop — no need for Compose's own drag data.
Box(
- Modifier.fillMaxSize().onExternalDrag(
- onDragStart = { dropping = true },
- onDragExit = { dropping = false },
- onDrop = { v ->
- dropping = false
- val data = v.dragData
- if (data is androidx.compose.ui.DragData.FilesList) {
- attachFiles(data.readFiles().mapNotNull { runCatching { java.io.File(java.net.URI(it)) }.getOrNull() })
+ Modifier.fillMaxSize().dragAndDropTarget(
+ shouldStartDragAndDrop = { true },
+ target = remember {
+ object : DragAndDropTarget {
+ override fun onStarted(event: DragAndDropEvent) { dropping = true }
+ override fun onExited(event: DragAndDropEvent) { dropping = false }
+ override fun onEnded(event: DragAndDropEvent) { dropping = false }
+ override fun onDrop(event: DragAndDropEvent): Boolean {
+ dropping = false
+ val drop = event.nativeEvent as? java.awt.dnd.DropTargetDropEvent ?: return false
+ val flavor = java.awt.datatransfer.DataFlavor.javaFileListFlavor
+ if (!drop.transferable.isDataFlavorSupported(flavor)) return false
+ @Suppress("UNCHECKED_CAST")
+ val files = runCatching { drop.transferable.getTransferData(flavor) as List<java.io.File> }
+ .getOrNull() ?: return false
+ attachFiles(files)
+ return true
+ }
}
},
),
@@ -234,7 +249,12 @@ fun ChatScreen(onOpenArtifacts: () -> Unit, onOpenTopic: (Long) -> Unit = {}, on
// Same taps as the floating ball; sideways swipe switches character, like the full card's ball on the phone.
val chatBall = remember { ballActions(onType = { ComposerFocus.pending = true }, onNoVoice = { progress = noVoiceKey() }) }
Box(Modifier.fillMaxWidth().padding(bottom = 6.dp), contentAlignment = Alignment.Center) {
- BallView(150.dp, onClick = null, modifier = Modifier.ballTaps(chatBall).lookSwipe())
+ // Holds its last frame at rest, unlike the floating ball, which keeps
+ // breathing: this one shares a window with the conversation, so each of
+ // its frames redraws all of it. It animates from the moment something
+ // happens — listening, speaking, thinking, a tap — and settles after.
+ BallView(150.dp, onClick = null, modifier = Modifier.ballTaps(chatBall).lookSwipe(),
+ calmFrameNs = BallRenderer.HOLD)
}
LazyColumn(
state = list, modifier = Modifier.weight(1f).fillMaxWidth().fadeTop(36.dp),
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 c4d14d2..51a39de 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/MacBall.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/MacBall.kt
@@ -135,9 +135,20 @@ object MacBall {
* A Mac where OpenGL cannot be had simply shows no ball.
*/
@Composable
-fun BallView(size: Dp, onClick: (() -> Unit)?, modifier: Modifier = Modifier) {
+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) { BallRenderer(px, px) }
+ val renderer = remember(px, calmFrameNs) { BallRenderer(px, px, calmFrameNs) }
DisposableEffect(renderer) {
MacBall.attach(renderer)
onDispose { MacBall.detach(renderer); renderer.stop() }
@@ -153,12 +164,19 @@ fun BallView(size: Dp, onClick: (() -> Unit)?, modifier: Modifier = Modifier) {
DisposableEffect(renderer) {
// At most one repaint queued: a busy UI thread (a drag) skips frames instead of piling them up.
val queued = java.util.concurrent.atomic.AtomicBoolean(false)
- renderer.onFrame = {
+ fun paint(f: ByteArray) {
if (queued.compareAndSet(false, true)) javax.swing.SwingUtilities.invokeLater {
queued.set(false)
- renderer.frame?.let { f -> bitmap.installPixels(f); bitmap.notifyPixelsChanged(); tick++ }
+ bitmap.installPixels(f)
+ bitmap.notifyPixelsChanged()
+ tick++
}
}
+ renderer.onFrame = { renderer.frame?.let(::paint) }
+ // The renderer starts in its constructor, so it may have already drawn its
+ // one and only frame before this callback existed — a ball that idles by
+ // holding draws exactly once. Adopt whatever is there now, or it stays blank.
+ renderer.frame?.let(::paint)
onDispose { renderer.onFrame = {} }
}
val click = if (onClick == null) Modifier else Modifier.clickable(
diff --git a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Main.kt b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Main.kt
index e1fd865..59646ff 100644
--- a/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Main.kt
+++ b/desktop/src/main/kotlin/net/kuafuai/andee/desktop/Main.kt
@@ -21,14 +21,18 @@ import java.util.Date
import java.util.Locale
fun main() {
- // Intel Mac: skiko's Metal renderer crashes (SIGILL in
- // MetalRedrawer.createMetalDevice) on these machines, so fall back to
- // software rendering. Apple Silicon keeps the hardware Metal path. Must run
- // before any SkiaLayer is created, so before `application { }` below.
- if (System.getProperty("os.arch") in setOf("x86_64", "amd64")) {
+ // Intel Macs used to crash inside skiko's Metal backend (SIGILL in
+ // MetalRedrawer.createMetalDevice) under macOS 26, so the app had to draw the
+ // whole window on the CPU — which is why a full-screen window felt so heavy.
+ // skiko 0.9.22 (Compose 1.9) fixed that; verified here with a minimal Metal
+ // window, so the GPU is the default again on every machine. Keep the CPU path
+ // reachable for comparison or for a machine where Metal misbehaves:
+ // -Dandee.software=true
+ if (System.getProperty("andee.software").toBoolean()) {
System.setProperty("skiko.renderApi", "SOFTWARE")
}
installFileLog()
+ installUiLagLog()
net.kuafuai.andee.util.Log.i("Andee", "starting, data in ${AppDirs.root}")
Assistant.askApproval = { ApprovalGate.request(it) }
MacBall.restoreLook()
@@ -117,6 +121,50 @@ fun main() {
}
}
+/**
+ * Writes UI stalls to `andee.log`: how long an empty task waited in the event queue
+ * before the queue got to it.
+ *
+ * "It feels laggy" on a Mac that paints in software is invisible from the outside —
+ * the process looks idle while the window is frozen, and the only person driving the
+ * window is the one who is complaining. A number here is the stall itself, not a
+ * proxy for it: if the queue took 200 ms to run an empty task, that click really did
+ * wait 200 ms.
+ */
+private fun installUiLagLog() {
+ // java.management is not in the jlink'd runtime, so no ManagementFactory here:
+ // the heap delta is enough to tell a garbage collection from a real stall.
+ val rt = Runtime.getRuntime()
+ fun usedHeap() = rt.totalMemory() - rt.freeMemory()
+ kotlin.concurrent.thread(name = "UiLag", isDaemon = true) {
+ while (true) {
+ val t0 = System.nanoTime()
+ val heap0 = usedHeap()
+ runCatching { javax.swing.SwingUtilities.invokeAndWait {} }
+ val ms = (System.nanoTime() - t0) / 1_000_000
+ if (ms >= 60) {
+ // A long stall is never the rasteriser — software rendering costs tens of
+ // milliseconds, not seconds. Keep the stack: without it a 2 s pause is a
+ // number with no owner, and the guess costs another round trip.
+ val dHeap = usedHeap() - heap0
+ val msg = buildString {
+ append("UI thread stalled ${ms}ms")
+ append(" [heap ${rt.totalMemory() / 1048576}MB, ${dHeap / 1024}KB]")
+ if (ms >= 400) {
+ append('\n')
+ val edt = Thread.getAllStackTraces().entries
+ .firstOrNull { it.key.name.contains("AWT-EventQueue") }
+ if (edt == null) append(" (no EDT stack)")
+ else edt.value.take(16).forEach { append(" at ").append(it).append('\n') }
+ }
+ }
+ net.kuafuai.andee.util.Log.w("UiLag", msg)
+ }
+ Thread.sleep(150)
+ }
+ }
+}
+
/**
* Shared-module logging goes to `andee.log` next to the data, rotated at
* 2 MB. A packaged app has no terminal, so this file is the only place a
@@ -138,8 +186,15 @@ private fun installFileLog() {
/** The chat window's first size: 1280×960 points, shrunk to 90% of the main screen when that is smaller. */
private fun defaultChatSize(): Pair<androidx.compose.ui.unit.Dp, androidx.compose.ui.unit.Dp> {
val screen = runCatching { java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().maximumWindowBounds }.getOrNull()
- val w = minOf(1280, ((screen?.width ?: 1280) * 0.9).toInt())
- val h = minOf(960, ((screen?.height ?: 960) * 0.9).toInt())
+ // On a machine that paints in software every pixel of this window is filled by
+ // the CPU, so the window's pixel count is its frame cost. Rendering at 1x cuts
+ // that count too but blurs the text, because the display then stretches a
+ // half-resolution image over the same area. Shrinking the window cuts the count
+ // *without* that trade: HiDPI is pixels per point, so a 2x window at 220k points
+ // is as sharp as a 2x window at 470k points — it just costs less to paint.
+ val software = System.getProperty("skiko.renderApi") == "SOFTWARE"
+ val w = minOf(if (software) 1000 else 1280, ((screen?.width ?: 1280) * if (software) 0.72f else 0.9f).toInt())
+ val h = minOf(if (software) 720 else 960, ((screen?.height ?: 960) * if (software) 0.72f else 0.9f).toInt())
return w.dp to h.dp
}