Animations are mainly on the typescript side, not kotlin

This commit is contained in:
James Babcock committed 2026-09-23 22:05:06 -07:00
1 parent 063d8a0b03
commit 440674b261
26 files changed
+783 -128

No files matched your search

@@ -25,7 +25,7 @@ class ScenePlanner(private val cache: ResourceCacheState) {
CachedResource(DrawProtocol.rawImage(layer.width, layer.height), keys[i])
} }
owned.keys.retainAll(keys.toSet()); pixels.keys.retainAll(keys.toSet())
val selectedResources = scene.allSelections.map { it.resource }
val selectedResources = scene.retainedResources
cache.pin(surfaces + selectedResources)
val absent = surfaces.map { cache.resourceId(it) < 0 }
val prepared = cache.prepare(surfaces + selectedResources)
@@ -8,10 +8,10 @@ object DrawRecordKind {
const val MENU_SELECTION = 3
const val TRANSPARENT_IMAGE = 4
const val MASKED_IMAGE = 5
const val ANIMATED_MENU_SELECTION = 6
const val DISPLAY_LIST = 7
fun isPresentation(kind: Int): Boolean = when (kind) {
MENU_SELECTION, TRANSPARENT_IMAGE, MASKED_IMAGE, ANIMATED_MENU_SELECTION -> true
MENU_SELECTION, TRANSPARENT_IMAGE, MASKED_IMAGE, DISPLAY_LIST -> true
else -> false
}
}
@@ -0,0 +1,133 @@
package com.faceclaw.app
/** A retained draw submitted with frame pixels, before cache IDs have been assigned. */
interface RetainedDrawing {
val resources: List<CachedResource>
val fingerprint: String
fun translated(dx: Int, dy: Int): RetainedDrawing
fun calls(ids: IntArray, nowMs: Long): List<ByteArray>
}
internal fun readRetainedDrawing(reader: ByteReader, kind: Int): RetainedDrawing =
if (kind == DrawRecordKind.DISPLAY_LIST) FrameDisplayList.read(reader) else MenuSelection.read(reader, kind)
/** Generic TS-authored calls. No menu or easing policy lives in this bridge. */
class FrameDisplayList private constructor(
private val x: Int, private val y: Int, private val depth: Int,
override val resources: List<CachedResource>, private val commands: List<Command>,
internal val startedAt: Long, private val identity: String,
) : RetainedDrawing {
override val fingerprint: String get() = "$x,$y,$depth,$identity"
override fun translated(dx: Int, dy: Int) = FrameDisplayList(x + dx, y + dy, depth, resources, commands, startedAt, identity)
private class Command(val op: Int, val depth: Int, val values: IntArray,
val x: DrawValue = DrawValue.Integer(0), val y: DrawValue = DrawValue.Integer(0))
override fun calls(ids: IntArray, nowMs: Long): List<ByteArray> {
require(ids.size == resources.size)
val elapsed = (nowMs - startedAt).coerceIn(0, Int.MAX_VALUE.toLong()).toInt()
fun resource(index: Int) = if (index == DrawProtocol.SCREEN) index else ids[index]
return commands.map { c ->
val v = c.values
when (c.op) {
DRAW_OP_ROUNDED_RECT -> DrawProtocol.roundedRect(bind(c.x, elapsed, x), bind(c.y, elapsed, y),
v[0], v[1], v[2], v[3], v[4], depth + c.depth)
DRAW_OP_IMAGE -> DrawProtocol.image(resource(v[0]), x + v[1], y + v[2], v[3], depth = depth + c.depth)
DRAW_OP_RECT_COPY -> DrawProtocol.rectCopy(resource(v[0]),
v[1] + if (v[0] == DrawProtocol.SCREEN) x else 0,
v[2] + if (v[0] == DrawProtocol.SCREEN) y else 0,
v[3], v[4], x + v[5], y + v[6], depth = depth + c.depth)
else -> error("Unsupported frame display-list opcode")
}
}
}
companion object {
// Bridge-only constant binding, removed before a program reaches either renderer.
private const val ELAPSED = 128
private const val MAX_RECORD_BYTES = 4 * 1024 * 1024
private fun bind(value: DrawValue, elapsed: Int, offset: Int): DrawValue {
if (value is DrawValue.Integer) return DrawValue.Integer(value.value + offset)
val reader = DrawReader((value as DrawValue.Expression).program)
val out = ByteSink()
while (reader.remaining > 0) {
when (val op = reader.readU8()) {
ELAPSED -> { out.write(DrawExpression.PUSH_I32); out.write(ExtendedVarint.signed(elapsed)) }
DrawExpression.PUSH_I32 -> { out.write(op); out.write(ExtendedVarint.signed(ExtendedVarint.read(reader, true))) }
DrawExpression.PUSH_F32 -> { out.write(op); out.write(reader.readBytes(4)) }
else -> out.write(op)
}
}
if (offset != 0) {
out.write(DrawExpression.PUSH_I32); out.write(ExtendedVarint.signed(offset)); out.write(DrawExpression.IADD)
}
val program = out.toByteArray()
require(program.size <= DrawExpression.MAX_BYTES)
return DrawValue.Expression(program)
}
fun read(input: ByteReader, nowMs: Long = drawAnimationTimeMs()): FrameDisplayList {
val length = input.getInt()
require(length in 21..MAX_RECORD_BYTES && length <= input.remaining())
val bytes = ByteArray(length).also { input.get(it) }
val r = DrawReader(bytes)
val x = r.readS16(); val y = r.readS16()
val width = r.readU16(); val height = r.readU16()
require(width in 1..640 && height in 1..480)
val depth = r.readS8()
r.readBytes(4) // Stable timeline token is included in the identity below.
val elapsed = r.readU16() or (r.readU16() shl 16)
require(elapsed >= 0)
val resourceCount = r.readU16(); require(resourceCount <= 256)
val resources = List(resourceCount) {
val w = r.readU16(); val h = r.readU16()
require(w in 1..640 && h in 1..480 && 5 + (w + 1) / 2 * h <= 65536)
val gray = r.readBytes(w * h)
CachedResource(DrawProtocol.rawImage(w, h, BmpUtil.pack4bppFromGray8(gray, w, h)))
}
fun value(): DrawValue {
val first = r.readU8()
if (first != 255) return DrawValue.Integer(ExtendedVarint.read(r, true, first))
val size = ExtendedVarint.read(r, false).toUInt()
require(size <= 900u)
return DrawValue.Expression(r.readBytes(size.toInt())).also {
// Validate framing and worst-case binding growth before retaining the frame.
bind(it, Int.MAX_VALUE, Int.MAX_VALUE)
}
}
fun resource(): Int = r.readU16().also { require(it == DrawProtocol.SCREEN || it in resources.indices) }
val count = r.readU16(); require(count <= 4096)
val calls = List(count) {
val op = r.readU8(); val d = r.readS16(); require(depth + d in -128..127)
when (op) {
DRAW_OP_ROUNDED_RECT -> {
val xx = value(); val yy = value()
val w = r.readU16(); val h = r.readU16(); val radius = r.readU16()
val background = r.readU8(); val border = r.readU8()
require(w in 1..640 && h in 1..480 && background in 0..15 && border in 0..16)
Command(op, d, intArrayOf(w, h, radius, background, border), xx, yy)
}
DRAW_OP_IMAGE -> {
val id = resource(); require(id != DrawProtocol.SCREEN)
val xx = r.readS16(); val yy = r.readS16(); val options = r.readU8()
require(options == 15 || options == 31)
Command(op, d, intArrayOf(id, xx, yy, options))
}
DRAW_OP_RECT_COPY -> {
val id = resource(); val sx = r.readS16(); val sy = r.readS16()
val w = r.readU16(); val h = r.readU16(); val dx = r.readS16(); val dy = r.readS16()
require(w in 1..640 && h in 1..480)
require(id == DrawProtocol.SCREEN || sx >= 0 && sy >= 0)
Command(op, d, intArrayOf(id, sx, sy, w, h, dx, dy))
}
else -> error("Unsupported frame display-list opcode")
}
}
r.requireDone()
// Elapsed changes while the same timeline is resubmitted; it is not content identity.
bytes.fill(0, 13, 17)
return FrameDisplayList(x, y, depth, resources, calls, nowMs - elapsed, CachedResource(bytes).hash.toString())
}
}
}
@@ -2,17 +2,15 @@ package com.faceclaw.app
/** A retained selection or image, replayed at stereo depth over the screen and shell surfaces. */
class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val radius: Int,
val background: Int, val border: Int, val depth: Int, val packed: ByteArray, val occlusions: List<IntArray> = emptyList(), val kind: Int = DrawRecordKind.MENU_SELECTION, val mask: List<IntArray> = emptyList(), val animation: Animation? = null) {
class Animation(val dx: Int, val dy: Int, val startedAt: Long, val token: Int, val durationMs: Int)
val background: Int, val border: Int, val depth: Int, val packed: ByteArray, val occlusions: List<IntArray> = emptyList(), val kind: Int = DrawRecordKind.MENU_SELECTION, val mask: List<IntArray> = emptyList()) : RetainedDrawing {
val resource = CachedResource(DrawProtocol.rawImage(width, height, packed))
val fingerprint = "${animation?.token},${animation?.dx},${animation?.dy},${animation?.durationMs},$kind,${mask.joinToString { it.joinToString() }},$x,$y,$width,$height,$radius,$background,$border,$depth,${resource.hash},${occlusions.joinToString { it.joinToString() }}"
fun translated(dx: Int, dy: Int) = MenuSelection(x + dx, y + dy, width, height, radius, background, border, depth, packed, occlusions.map { intArrayOf(it[0] + dx, it[1] + dy, it[2], it[3]) }, kind, mask, animation)
fun calls(id: Int, nowMs: Long = drawAnimationTimeMs()): List<ByteArray> {
val elapsed = animation?.let { (nowMs - it.startedAt).coerceIn(0, it.durationMs.toLong()).toInt() } ?: 0
val highlightX = animation?.let { DrawValue.animate(x + it.dx, x, it.durationMs, elapsed) } ?: DrawValue.Integer(x)
val highlightY = animation?.let { DrawValue.animate(y + it.dy, y, it.durationMs, elapsed) } ?: DrawValue.Integer(y)
override val resources = listOf(resource)
override val fingerprint = "$kind,${mask.joinToString { it.joinToString() }},$x,$y,$width,$height,$radius,$background,$border,$depth,${resource.hash},${occlusions.joinToString { it.joinToString() }}"
override fun translated(dx: Int, dy: Int) = MenuSelection(x + dx, y + dy, width, height, radius, background, border, depth, packed, occlusions.map { intArrayOf(it[0] + dx, it[1] + dy, it[2], it[3]) }, kind, mask)
override fun calls(ids: IntArray, nowMs: Long) = calls(ids.single())
fun calls(id: Int): List<ByteArray> {
val image = when (kind) {
DrawRecordKind.MENU_SELECTION -> listOf(DrawProtocol.roundedRect(highlightX, highlightY, width, height, radius, background, border, depth),
DrawRecordKind.MENU_SELECTION -> listOf(DrawProtocol.roundedRect(x, y, width, height, radius, background, border, depth),
DrawProtocol.image(id, x, y, CFW_TEXTURE_OPT_BRIGHTNESS_MASK or CFW_TEXTURE_OPT_TRANSPARENT, depth = depth))
DrawRecordKind.TRANSPARENT_IMAGE -> listOf(DrawProtocol.image(id, x, y, CFW_TEXTURE_OPT_BRIGHTNESS_MASK or CFW_TEXTURE_OPT_TRANSPARENT, depth = depth))
// Gray8 zero is transparent but gray8 one is opaque black. Rect copies preserve
@@ -31,7 +29,7 @@ class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val
companion object {
/** The caller has consumed a presentation tag from [DrawRecordKind]. Colors and pixels cross in gray8. */
fun read(reader: ByteReader, kind: Int = DrawRecordKind.MENU_SELECTION): MenuSelection {
require(DrawRecordKind.isPresentation(kind))
require(kind in listOf(DrawRecordKind.MENU_SELECTION, DrawRecordKind.TRANSPARENT_IMAGE, DrawRecordKind.MASKED_IMAGE))
require(reader.remaining() >= 15)
val x = reader.getShort().toInt(); val y = reader.getShort().toInt()
val w = reader.getShort().toInt() and 65535; val h = reader.getShort().toInt() and 65535
@@ -42,16 +40,6 @@ class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val
val count = reader.getShort().toInt() and 65535
require(count <= 2048)
require(w in 1..640 && h in 1..480 && 5 + (w + 1) / 2 * h <= 65536 && reader.remaining() >= w * h)
val animation = if (kind == DrawRecordKind.ANIMATED_MENU_SELECTION) {
require(reader.remaining() >= 12)
val dx = reader.getShort().toInt()
val dy = reader.getShort().toInt()
val elapsed = reader.getShort().toInt() and 65535
val token = reader.getInt()
val duration = reader.getShort().toInt() and 65535
require(duration > 0 && elapsed <= duration)
Animation(dx, dy, drawAnimationTimeMs() - elapsed, token, duration)
} else null
val gray = ByteArray(w * h); reader.get(gray)
require(reader.remaining() >= count * 8)
val occlusions = List(count) { IntArray(4) { reader.getShort().toInt() and 65535 } }
@@ -68,8 +56,7 @@ class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val
}
}
}
val normalizedKind = if (kind == DrawRecordKind.ANIMATED_MENU_SELECTION) DrawRecordKind.MENU_SELECTION else kind
return MenuSelection(x, y, w, h, radius, background, border, depth, BmpUtil.pack4bppFromGray8(gray, w, h), occlusions, normalizedKind, mask, animation)
return MenuSelection(x, y, w, h, radius, background, border, depth, BmpUtil.pack4bppFromGray8(gray, w, h), occlusions, kind, mask)
}
}
}
@@ -1,18 +1,24 @@
package com.faceclaw.app
/** Immutable shell snapshot. Keys identify writable surfaces across repaints, not their pixels. */
class ShellScene(val layers: List<Layer>, val selections: List<MenuSelection> = emptyList()) {
class Layer(val key: Int, val x: Int, val y: Int, val width: Int, val height: Int, val dim: Int, val packed: ByteArray, val selections: List<MenuSelection> = emptyList(), val depth: Int = 0)
class ShellScene(val layers: List<Layer>, val selections: List<RetainedDrawing> = emptyList()) {
class Layer(val key: Int, val x: Int, val y: Int, val width: Int, val height: Int, val dim: Int, val packed: ByteArray, val selections: List<RetainedDrawing> = emptyList(), val depth: Int = 0)
val allSelections = selections + layers.flatMap { it.selections }
val retainedResources = allSelections.flatMap { it.resources }
init { require(retainedResources.size + layers.size < 511) }
val fingerprint: String = layers.joinToString(";") { "${it.key},${it.x},${it.y},${it.width},${it.height},${it.dim},${it.depth},${CachedResource(it.packed).hash},${it.selections.joinToString { row -> row.fingerprint }}" } + selections.joinToString { it.fingerprint }
fun calls(width: Int, height: Int, surfaces: IntArray, selected: IntArray): List<ByteArray> {
val calls = ArrayList<ByteArray>(); var rowId = 0
val now = drawAnimationTimeMs()
for (row in selections) calls.addAll(row.calls(selected[rowId++], now))
fun add(row: RetainedDrawing) {
calls.addAll(row.calls(selected.copyOfRange(rowId, rowId + row.resources.size), now))
rowId += row.resources.size
}
for (row in selections) add(row)
for ((index, layer) in layers.withIndex()) {
if (layer.dim < 256) calls.add(DrawProtocol.lut(width, height, layer.dim))
calls.add(DrawProtocol.image(surfaces[index], layer.x, layer.y, depth = layer.depth))
for (row in layer.selections) calls.addAll(row.calls(selected[rowId++], now))
for (row in layer.selections) add(row)
}
return calls
}
@@ -22,7 +28,7 @@ class ShellScene(val layers: List<Layer>, val selections: List<MenuSelection> =
val surfaces = IntArray(layers.size) { id ->
val layer = layers[id]; resources[id] = DrawProtocol.rawImage(layer.width, layer.height, layer.packed); id
}
val rows = IntArray(allSelections.size) { id -> resources[id + layers.size] = allSelections[id].resource.bytes; id + layers.size }
val rows = IntArray(retainedResources.size) { id -> resources[id + layers.size] = retainedResources[id].bytes; id + layers.size }
resources[511] = DrawProtocol.displayList(calls(width, height, surfaces, rows))
DisplayListRenderer(resources, rightLens = rightLens).render(511, DisplayListRenderer.Target(screen, width, height), DisplayListRenderer.Target(output, width, height))
val target = DisplayListRenderer.Target(output, width, height)
@@ -42,8 +48,8 @@ class ShellScene(val layers: List<Layer>, val selections: List<MenuSelection> =
resources[id] = DrawProtocol.rawImage(layer.width, layer.height, layer.packed)
id
}
val rows = IntArray(allSelections.size) { id ->
resources[id + layers.size] = allSelections[id].resource.bytes
val rows = IntArray(retainedResources.size) { id ->
resources[id + layers.size] = retainedResources[id].bytes
id + layers.size
}
resources[511] = DrawProtocol.displayList(calls(width, height, surfaces, rows))
@@ -73,11 +79,11 @@ class ShellScene(val layers: List<Layer>, val selections: List<MenuSelection> =
val depth = reader.getShort().toInt(); require(depth in -128..127)
require(w in 1..640 && h in 1..480 && 5 + (w + 1) / 2 * h <= 65536 && reader.remaining() >= w * h)
val gray = ByteArray(w * h); reader.get(gray)
val selections = List(selectionCount) { MenuSelection.read(reader, reader.get().toInt()) }
val selections = List(selectionCount) { readRetainedDrawing(reader, reader.get().toInt()) }
layers.add(Layer(key, x, y, w, h, dim, BmpUtil.pack4bppFromGray8(gray, w, h), selections, depth))
}
require(reader.remaining() == 0)
require(layers.sumOf { it.selections.size } + layers.size < 511)
require(layers.sumOf { layer -> layer.selections.sumOf { it.resources.size } } + layers.size < 511)
require(layers.map { it.key }.toSet().size == layers.size)
return ShellScene(layers)
}
@@ -51,7 +51,7 @@ class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFr
var out: MutableList<ScreenDraw> = ArrayList()
while ((cursor.remaining() >= 1)) {
var kind: Int = (cursor.get() and 0xff)
if (DrawRecordKind.isPresentation(kind)) { out.add(ScreenDraw.image(0, 0, 0).also { it.selection = MenuSelection.read(cursor, kind) }); continue }
if (DrawRecordKind.isPresentation(kind)) { out.add(ScreenDraw.image(0, 0, 0).also { it.selection = readRetainedDrawing(cursor, kind) }); continue }
if (((kind == ScreenDraw.KIND_GLYPH) && (cursor.remaining() >= 11))) {
var fontId: Int = (cursor.getShort().toInt() and 0xffff)
var encoding: Int = cursor.getInt()
@@ -108,7 +108,7 @@ class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFr
* raster comes from ImageAtlas under imageId.
*/
class ScreenDraw {
@JvmField var selection: MenuSelection? = null
@JvmField var selection: RetainedDrawing? = null
companion object {
const val KIND_GLYPH: Int = DrawRecordKind.GLYPH
@@ -631,7 +631,7 @@ class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFr
var fingerprint: StringBuilder = StringBuilder()
fingerprint.append(screenWidth).append('x').append(screenHeight)
var draws: MutableList<ScreenDraw> = ArrayList()
val selections = ArrayList<MenuSelection>()
val selections = ArrayList<RetainedDrawing>()
for (surface in ordered) {
if (!surface.visible || (shellScene != null && surface.id == "shell")) {
continue