Display lists

This commit is contained in:
James Babcock committed 2026-09-22 00:25:40 -07:00
1 parent a7af84e231
commit 4320021222
44 files changed
+823 -284

No files matched your search

@@ -0,0 +1,90 @@
package com.faceclaw.app
/** Revision 25 wire grammar, shared by scene planning and the local renderer. */
object DrawProtocol {
const val DRAW = 26
const val ROOT = 27
const val PRESENT = 28
const val CREATE = 29
const val IMAGE = 0
const val FONT = 1
const val LIST = 2
const val LARGE = 4
const val RLE = 8
const val SCREEN = 65535
const val CURRENT = 65534
fun u16(b: ByteArray, p: Int): Int = (b[p].toInt() and 255) or ((b[p + 1].toInt() and 255) shl 8)
fun word(n: Int) = byteArrayOf(n.toByte(), (n ushr 8).toByte())
fun call(op: Int, args: ByteArray, target: Int? = null): ByteArray =
byteArrayOf(op.toByte(), if (target == null) 0 else 1) + (target?.let { word(it) } ?: byteArrayOf()) + args
fun sequence(calls: List<ByteArray>): ByteArray {
require(calls.size <= 4096)
val out = ByteSink(); out.write(word(calls.size))
for (call in calls) { require(call.size <= 65535); out.write(word(call.size)); out.write(call) }
return out.toByteArray()
}
fun message(calls: List<ByteArray>) = byteArrayOf(DRAW.toByte()) + sequence(calls)
fun displayList(calls: List<ByteArray>) = byteArrayOf(LIST.toByte()) + sequence(calls)
fun root(id: Int) = byteArrayOf(ROOT.toByte()) + word(id)
fun image(id: Int, x: Int, y: Int, options: Int = 15, target: Int? = null) =
call(4, word(id) + word(x) + word(y) + byteArrayOf(options.toByte()), target)
fun lut(width: Int, height: Int, factor: Int): ByteArray {
val table = ByteArray(8) { i ->
(((i * 2 * factor / 256).coerceIn(0, 15) shl 4) or ((i * 2 + 1) * factor / 256).coerceIn(0, 15)).toByte()
}
return call(6, word(0) + word(0) + word(width) + word(height) + table)
}
fun rawImage(width: Int, height: Int, pixels: ByteArray = ByteArray(((width + 1) / 2) * height)): ByteArray {
require(width in 1..640 && height in 1..480 && pixels.size == ((width + 1) / 2) * height)
val result = byteArrayOf(LARGE.toByte()) + word(width) + word(height) + pixels
require(result.size <= ResourceCacheState.MAX_RESOURCE_SIZE)
return result
}
/** Packed rows -> RLE of exactly width*height pixels, omitting odd-row padding. */
fun bbox(packed: ByteArray, stride: Int, x: Int, y: Int, width: Int, height: Int, target: Int? = null): ByteArray {
val compact = x % 4 == 0 && y % 2 == 0 && width % 4 == 0 && height % 2 == 0 && x / 4 < 256 && y / 2 < 256 && width / 4 < 256 && height / 2 < 256
val header = if (compact) byteArrayOf(0, (x / 4).toByte(), (y / 2).toByte(), (width / 4).toByte(), (height / 2).toByte())
else byteArrayOf(1) + word(x) + word(y) + word(width) + word(height)
val out = ByteSink(); out.write(header)
var color = -1; var count = 0
fun flush() {
if (count == 0) return
if (count <= 15) out.write((count shl 4) or color)
else { out.write(color); if (count <= 255) out.write(count) else { out.write(0); out.write(word(count)) } }
}
for (yy in y until y + height) for (xx in x until x + width) {
val value = (packed[yy * stride + xx / 2].toInt() ushr (if (xx % 2 == 0) 4 else 0)) and 15
if (value != color || count == 65535) { flush(); color = value; count = 0 }
count++
}
flush()
return call(1, out.toByteArray(), target)
}
/** Internal optimizer formats are translated here; they are never sent to revision 25. */
fun fromOptimized(payload: ByteArray, width: Int = 640, height: Int = 480): List<ByteArray> {
return when (val mode = payload[0].toInt() and 127) {
8 -> {
val result = ArrayList<ByteArray>(); var pos = 2
repeat(payload[1].toInt() and 255) { val n = u16(payload, pos); pos += 2; result.addAll(fromOptimized(payload.copyOfRange(pos, pos + n), width, height)); pos += n }
require(pos == payload.size); result
}
3 -> listOf(call(1, byteArrayOf(0) + payload.copyOfRange(1, 5) + payload.copyOfRange(7, payload.size)))
6 -> listOf(call(1, byteArrayOf(1) + word(0) + word(0) + word(width) + word(height) + payload.copyOfRange(1, payload.size)))
15 -> listOf(call(3, payload.copyOfRange(1, payload.size)))
19 -> listOf(call(4, payload.copyOfRange(1, payload.size)))
20 -> listOf(call(5, payload.copyOfRange(1, payload.size)))
9 -> listOf(call(2, word(CURRENT) + payload.copyOfRange(1, 9) + payload.copyOfRange(9, 13)))
else -> error("Unsupported internal draw format $mode")
}
}
fun messages(calls: List<ByteArray>, maxBytes: Int = 65535): List<ByteArray> {
val result = ArrayList<ByteArray>(); var batch = ArrayList<ByteArray>(); var size = 3
for (call in calls) {
require(call.size + 5 <= maxBytes)
if (size + 2 + call.size > maxBytes) { result.add(message(batch)); batch = ArrayList(); size = 3 }
batch.add(call); size += call.size + 2
}
if (batch.isNotEmpty()) result.add(message(batch))
return result
}
}
@@ -1,14 +1,16 @@
package com.faceclaw.app
import kotlin.jvm.JvmOverloads
/** Immutable content-addressed bytes; equality verifies bytes when hashes collide. */
class CachedResource(bytes: ByteArray) {
class CachedResource @JvmOverloads constructor(bytes: ByteArray, val owner: String? = null) {
val bytes: ByteArray = bytes.copyOf()
val hash: Long = this.bytes.fold(-3750763034362895579L) { hash, byte ->
(hash xor (byte.toLong() and 255)) * 1099511628211L
}
override fun hashCode(): Int = (hash xor (hash ushr 32)).toInt()
override fun hashCode(): Int = owner?.hashCode() ?: (hash xor (hash ushr 32)).toInt()
override fun equals(other: Any?): Boolean = this === other ||
other is CachedResource && hash == other.hash && bytes.contentEquals(other.bytes)
other is CachedResource && owner == other.owner && hash == other.hash && bytes.contentEquals(other.bytes)
}
/**
@@ -18,7 +20,7 @@ class CachedResource(bytes: ByteArray) {
*/
class ResourceCacheState {
companion object {
const val CACHE_SIZE = 262144
const val CACHE_SIZE = 196608
const val RESOURCE_COUNT = 512
const val TABLE_BYTES = RESOURCE_COUNT * 4
const val ARENA_BYTES = CACHE_SIZE - TABLE_BYTES
@@ -32,6 +34,10 @@ class ResourceCacheState {
private data class Resident(val id: Int, val resource: CachedResource, var lastUsed: Long)
private val residents = LinkedHashMap<CachedResource, Resident>()
private val occupied = BooleanArray(RESOURCE_COUNT)
var generation = 0
private set
private var pinned = emptySet<CachedResource>()
fun pin(resources: Collection<CachedResource>) { pinned = resources.toSet() }
private var allocated = 0
private var clock = 0L
private var resetRequired = true
@@ -40,6 +46,8 @@ class ResourceCacheState {
/** Forget uncertain device state. The next upload first evicts every ID on both lenses. */
fun reset() {
generation++
pinned = emptySet()
residents.clear(); occupied.fill(false); allocated = 0; clock = 0
evictions.clear(); uploads.clear(); resetRequired = true
}
@@ -52,6 +60,7 @@ class ResourceCacheState {
fun prepare(resources: List<CachedResource>): IntArray {
check(uploads.isEmpty() && evictions.isEmpty()) { "Previous resource commands have not been drained" }
val protected = HashSet<Int>()
for (resource in pinned) residents[resource]?.let { protected.add(it.id) }
for (resource in resources) residents[resource]?.let { protected.add(it.id) }
return IntArray(resources.size) { index ->
val resource = resources[index]
@@ -126,6 +135,11 @@ class ResourceCacheState {
}
for (entry in uploads) {
val data = entry.resource.bytes
if (entry.resource.owner != null) {
flush()
result.add(byteArrayOf(DrawProtocol.CREATE.toByte(), 1, 0) + DrawProtocol.word(entry.id) + data.copyOfRange(1, 5))
continue
}
var offset = 0
while (offset < data.size) {
val count = minOf(data.size - offset, maxPayloadBytes - 13)
@@ -0,0 +1,77 @@
package com.faceclaw.app
import kotlin.jvm.JvmOverloads
/** The delta base contains only app pixels. Shell resources are composed at presentation. */
class ScenePlanner(private val cache: ResourceCacheState) {
class Plan(val commands: List<ByteArray>, val nextFid: Int)
private val owned = HashMap<String, CachedResource>()
private val pixels = HashMap<String, ByteArray>()
private var previous: ByteArray? = null
private var screenWidth = 0
private var screenHeight = 0
private var epoch = -1
private var root = -1
@JvmOverloads
fun plan(screen: ByteArray, width: Int, height: Int, draws: Array<SurfaceCompositor.ScreenDraw>?, scene: ShellScene, firstId: Int, textures: Boolean = true, incremental: Boolean = true, multiRect: Boolean = true, maxRects: Int = 6): Plan {
require(width in 1..640 && height in 1..480)
if (epoch != cache.generation) { previous = null; pixels.clear(); root = -1; epoch = cache.generation }
require(screen.size == ((width + 1) / 2) * height)
if (width != screenWidth || height != screenHeight) previous = null
screenWidth = width; screenHeight = height
val commands = ArrayList<ByteArray>()
val keys = scene.layers.map { "shell:${it.key}:${it.width}x${it.height}" }
val surfaces = scene.layers.mapIndexed { i, layer -> owned.getOrPut(keys[i]) {
CachedResource(DrawProtocol.rawImage(layer.width, layer.height), keys[i])
} }
owned.keys.retainAll(keys.toSet()); pixels.keys.retainAll(keys.toSet())
cache.pin(surfaces)
val absent = surfaces.map { cache.resourceId(it) < 0 }
val ids = cache.prepare(surfaces)
check(ids.all { it >= 0 }) { "Shell surfaces exceed the resource budget" }
commands.addAll(cache.drainCommands(3600))
val rootCalls = ArrayList<ByteArray>()
for ((i, layer) in scene.layers.withIndex()) {
val old = if (absent[i]) null else pixels[keys[i]]
commands.addAll(DrawProtocol.messages(pixelCalls(old, layer.packed, layer.width, layer.height, ids[i])))
pixels[keys[i]] = layer.packed
if (layer.dim < 256) rootCalls.add(DrawProtocol.lut(width, height, layer.dim))
rootCalls.add(DrawProtocol.image(ids[i], layer.x, layer.y))
}
val base = if (incremental) previous else null
val texture = if (textures) TexturePlanner.plan(base, screen, width, height, draws, cache, firstId, multiRect, maxRects) else null
if (texture != null) {
commands.addAll(texture.resourceCommands)
commands.addAll(DrawProtocol.messages(DrawProtocol.fromOptimized(texture.payload, width, height)))
} else commands.addAll(DrawProtocol.messages(pixelCalls(base, screen, width, height, null)))
val list = CachedResource(DrawProtocol.displayList(rootCalls))
val rootId = cache.prepare(listOf(list))[0]
check(rootId >= 0) { "No space for root display list" }
commands.addAll(cache.drainCommands(3600))
cache.pin(surfaces + list)
val evicted = commands.any { it[0].toInt() == ResourceCacheState.EVICT_MODE }
if (evicted) commands.add(0, DrawProtocol.root(DrawProtocol.SCREEN))
if (root != rootId || evicted) commands.add(DrawProtocol.root(rootId))
commands.add(byteArrayOf(DrawProtocol.PRESENT.toByte()))
root = rootId; previous = screen.copyOf()
return Plan(commands, texture?.nextFid ?: firstId)
}
private fun pixelCalls(old: ByteArray?, next: ByteArray, width: Int, height: Int, target: Int?): List<ByteArray> {
val stride = (width + 1) / 2
if (old != null && old.contentEquals(next)) return emptyList()
val calls = ArrayList<ByteArray>()
for (top in 0 until height step 32) {
val rows = minOf(32, height - top)
if (old != null && (top * stride until (top + rows) * stride).all { old[it] == next[it] }) continue
var left = width; var right = -1
for (y in top until top + rows) for (x in 0 until width) {
val p = y * stride + x / 2; val shift = if (x % 2 == 0) 4 else 0
if (old == null || (((old[p].toInt() xor next[p].toInt()) ushr shift) and 15) != 0) { left = minOf(left, x); right = maxOf(right, x) }
}
if (right < left) continue
if (width % 4 == 0 && rows % 2 == 0) { left = left / 4 * 4; right = minOf(width, ((right + 4) / 4) * 4) - 1 }
calls.add(DrawProtocol.bbox(next, stride, left, top, right - left + 1, rows, target))
}
return calls
}
}
@@ -243,7 +243,7 @@ class TexturePlanner {
subs.addAll(runs)
subs.addAll(fwSubs)
var payload: ByteArray = assembleMode8(subs)
if (payload.size > CfwTransport.MAX_MESSAGE) {
if (payload.size > CfwTransport.MAX_MESSAGE - 16) {
rollback()
return null
}
@@ -0,0 +1,144 @@
package com.faceclaw.app
/** Reference interpreter. It uses the firmware's packed rows, clipping, LUT and call grammar. */
class DisplayListRenderer(private val resources: Map<Int, ByteArray>, private val builtin: ((Int, Int) -> BuiltinGlyph)? = null) {
class BuiltinGlyph(val image: ByteArray, val advance: Int, val x: Int = 0, val y: Int = 0)
class Target(val bytes: ByteArray, val width: Int, val height: Int, val offset: Int = 0) {
val stride = (width + 1) / 2
init { require(width > 0 && height > 0 && offset >= 0 && bytes.size - offset >= stride * height) }
fun get(x: Int, y: Int) = (bytes[offset + y * stride + x / 2].toInt() ushr (if (x % 2 == 0) 4 else 0)) and 15
fun put(x: Int, y: Int, v: Int) {
if (x !in 0 until width || y !in 0 until height) return
val index = offset + y * stride + x / 2; val old = bytes[index].toInt()
bytes[index] = (if (x % 2 == 0) (old and 15) or (v shl 4) else (old and 240) or v).toByte()
}
}
private class Image(val width: Int, val height: Int, val pixels: IntArray)
private fun word(b: ByteArray, p: Int): Int { require(p >= 0 && p + 2 <= b.size); return DrawProtocol.u16(b, p) }
private fun signed(b: ByteArray, p: Int) = word(b, p).toShort().toInt()
private fun resource(id: Int) = requireNotNull(resources[id]) { "Missing resource $id" }
private fun decodeRle(b: ByteArray, start: Int, count: Int, exact: Boolean): IntArray {
var p = start; var i = 0; val result = IntArray(count)
while (i < count) {
require(p < b.size); val token = b[p++].toInt() and 255; var n = token ushr 4
if (n == 0) { require(p < b.size); n = b[p++].toInt() and 255
if (n == 0) { n = word(b, p); p += 2 } }
require(n > 0 && n <= count - i); result.fill(token and 15, i, i + n); i += n
}
require(!exact || p == b.size)
return result
}
private fun image(b: ByteArray, start: Int = 0): Image {
require(start in b.indices); val flags = b[start].toInt() and 255
require(flags and 12 == flags)
val header = if (flags and 4 != 0) 5 else 3
require(b.size - start >= header)
val w = if (header == 5) word(b, start + 1) else b[start + 1].toInt() and 255
val h = if (header == 5) word(b, start + 3) else b[start + 2].toInt() and 255
require(w in 1..640 && h in 1..480)
val pixels = if (flags and 8 != 0) decodeRle(b, start + header, w * h, false)
else { val t = Target(b, w, h, start + header); IntArray(w * h) { t.get(it % w, it / w) } }
return Image(w, h, pixels)
}
private fun target(id: Int): Target {
val b = resource(id); require(b.isNotEmpty() && b[0].toInt() and 11 == 0)
image(b) // Validate the complete raw image before exposing a write target.
return if (b[0].toInt() and 4 != 0) Target(b, word(b, 1), word(b, 3), 5)
else Target(b, b[1].toInt() and 255, b[2].toInt() and 255, 3)
}
private fun draw(image: Image, target: Target, x: Int, y: Int, options: Int, apply: Boolean) {
if (!apply) return
for (i in image.pixels.indices) {
val v = image.pixels[i]; if (v == 0 && options and 16 != 0) continue
val source = if (options and 32 != 0) 15 - v else v
target.put(x + i % image.width, y + i / image.width, source * (options and 15) / 15)
}
}
/** Validate the whole graph before changing any pixels. Resource writes remain call-scoped. */
fun render(root: Int, screen: Target, composition: Target): Set<Int> {
val references = mutableSetOf<Int>()
for (apply in listOf(false, true)) {
if (apply) for (y in 0 until screen.height) for (x in 0 until screen.width) composition.put(x, y, screen.get(x, y))
if (root != DrawProtocol.SCREEN) executeList(root, composition, screen, apply, mutableListOf(), intArrayOf(4096), references)
}
return references
}
fun execute(sequence: ByteArray, target: Target, screen: Target = target): Set<Int> {
val references = mutableSetOf<Int>()
for (apply in listOf(false, true)) executeSequence(sequence, target, screen, apply, mutableListOf(), intArrayOf(4096), references)
return references
}
private fun executeList(id: Int, t: Target, screen: Target, apply: Boolean, stack: MutableList<Int>, budget: IntArray, refs: MutableSet<Int>) {
require(stack.size < 8 && id !in stack); val b = resource(id); require(b.size >= 3 && b[0].toInt() == 2)
refs.add(id); stack.add(id); executeSequence(b.copyOfRange(1, b.size), t, screen, apply, stack, budget, refs); stack.removeAt(stack.lastIndex)
}
private fun executeSequence(b: ByteArray, inherited: Target, screen: Target, apply: Boolean, stack: MutableList<Int>, budget: IntArray, refs: MutableSet<Int>) {
val count = word(b, 0); var pos = 2
repeat(count) {
val n = word(b, pos); pos += 2; require(n >= 2 && n <= b.size - pos && budget[0]-- > 0)
val call = b.copyOfRange(pos, pos + n); pos += n
val flags = call[1].toInt() and 255; require(flags <= 1)
var t = inherited; var header = 2
if (flags == 1) { val id = word(call, 2); refs.add(id); t = target(id); header += 2 }
val p = call.copyOfRange(header, call.size)
when (call[0].toInt()) {
1 -> {
require(p.isNotEmpty() && p[0].toInt() in 0..1); val wide = p[0].toInt() == 1
require(p.size >= if (wide) 9 else 5)
val x = if (wide) word(p, 1) else (p[1].toInt() and 255) * 4
val y = if (wide) word(p, 3) else (p[2].toInt() and 255) * 2
val w = if (wide) word(p, 5) else (p[3].toInt() and 255) * 4
val h = if (wide) word(p, 7) else (p[4].toInt() and 255) * 2
require(w > 0 && h > 0 && x + w <= t.width && y + h <= t.height)
val pixels = decodeRle(p, if (wide) 9 else 5, w * h, true)
if (apply) for (i in pixels.indices) t.put(x + i % w, y + i / w, pixels[i])
}
2 -> {
require(p.size == 14); val id = word(p, 0)
val src = when(id) { DrawProtocol.SCREEN -> screen; DrawProtocol.CURRENT -> t; else -> { refs.add(id); target(id) } }
val x = word(p, 2); val y = word(p, 4); val w = word(p, 6); val h = word(p, 8)
require(w > 0 && h > 0 && x + w <= src.width && y + h <= src.height)
val dx = signed(p, 10); val dy = signed(p, 12)
if (apply) { val copy = IntArray(w * h) { src.get(x + it % w, y + it / w) }
for (i in copy.indices) t.put(dx + i % w, dy + i / w, copy[i]) }
}
3 -> {
require(p.size >= 6 && p.size == 6 + (p[5].toInt() and 255))
val text = p.copyOfRange(6, p.size).decodeToString(throwOnInvalidSequence = true)
val cps = ArrayList<Int>(); var i = 0
while (i < text.length) { val c = text[i++].code
cps.add(if (c in 0xd800..0xdbff) { require(i < text.length); 0x10000 + ((c - 0xd800) shl 10) + text[i++].code - 0xdc00 } else c) }
var x = signed(p, 0); val y = signed(p, 2)
for (j in cps.indices) { val cp = cps[j]
if (cp in 1..31) { x += cp - 11; continue }
val next = cps.drop(j + 1).firstOrNull { it !in 1..31 } ?: 0
val g = requireNotNull(builtin) { "Builtin font provider required" }(cp, next)
draw(image(g.image), t, x + g.x, y + g.y, p[4].toInt() and 255, apply); x += g.advance
}
}
4, 5 -> {
val font = call[0].toInt() == 5; require(if(font) p.size >= 8 && p.size == 8 + (p[7].toInt() and 255) else p.size == 7)
val id = word(p, 0); refs.add(id); val data = resource(id)
var x = signed(p, 2); val y = signed(p, 4); val options = p[6].toInt() and 255
if (!font) draw(image(data), t, x, y, options, apply)
else { require(data.size >= 193 && data[0].toInt() == 1)
for (j in 8 until p.size) { val ch = p[j].toInt() and 255
if(ch in 1..31) { x += ch - 11; continue }; require(ch in 32..127)
val offset = word(data, 1 + (ch - 32) * 2); require(offset >= 193)
val glyph = image(data, offset); draw(glyph, t, x, y, options, apply); x += glyph.width
}
}
}
6 -> {
require(p.size == 16); val x = word(p, 0); val y = word(p, 2); val w = word(p, 4); val h = word(p, 6)
require(w > 0 && h > 0 && x + w <= t.width && y + h <= t.height)
if (apply) for(yy in y until y + h) for(xx in x until x + w) { val v = t.get(xx, yy)
t.put(xx, yy, (p[8 + v / 2].toInt() ushr (if(v % 2 == 0) 4 else 0)) and 15) }
}
7 -> { require(p.size == 2); executeList(word(p, 0), t, screen, apply, stack, budget, refs) }
else -> error("Unknown draw opcode")
}
}
require(pos == b.size)
}
}
@@ -13,7 +13,7 @@ class FontResourceAtlas {
val old = fonts[fontId]
if (old != null && old.encodings.containsAll(requested)) return old
val glyphs = HashMap<Int, GlyphAtlas.Glyph>()
var size = 192
var size = 193
// Current-frame glyphs take precedence if the entire font cannot fit.
for (encoding in requested.sorted() + (old?.encodings ?: emptySet()).sorted()) {
if (encoding !in 32..127 || glyphs.containsKey(encoding)) continue
@@ -22,10 +22,11 @@ class FontResourceAtlas {
glyphs[encoding] = glyph; size += glyph.cachedBytes.size
}
val bytes = ByteArray(size)
var offset = 192
bytes[0] = DrawProtocol.FONT.toByte()
var offset = 193
for (encoding in glyphs.keys.sorted()) {
val glyph = glyphs.getValue(encoding)
val slot = (encoding - 32) * 2
val slot = 1 + (encoding - 32) * 2
bytes[slot] = offset.toByte(); bytes[slot + 1] = (offset ushr 8).toByte()
glyph.cachedBytes.copyInto(bytes, offset)
offset += glyph.cachedBytes.size
@@ -10,8 +10,8 @@ import kotlin.jvm.JvmStatic
*
* - as the ink mask for the "would this draw land correctly" check against the composited frame,
* - to punch the ink pixels out of the baked delta rect it replaces,
* - pre-encoded as the CFW cached-image bytes ([w][h][4bpp RLE]) uploaded via mode 12 and drawn via
* mode 14 with a top-color LUT. 1bpp (BDF) glyphs store ink at 15 so the LUT maps them to exactly
* - pre-encoded as the CFW cached-image bytes ([flags=RLE][w][h][4bpp RLE]) uploaded as resources and drawn via
* the resource-text draw call with a top-color LUT. 1bpp (BDF) glyphs store ink at 15 so the LUT maps them to exactly
* the requested level; AA (TTF) glyphs store true coverage nibbles the LUT scales.
*
* Fonts are identified by a stable string key (the embedded font name), NOT a per-JS-context
@@ -232,7 +232,7 @@ class GlyphAtlas {
/** AA coverage, width*inkHeight nibble values 0..15; null for 1bpp glyphs. */
@JvmField val coverage: ByteArray?
/** CFW cached-image bytes: [w][cellHeight][RLE(w*cellHeight px)]. */
/** CFW cached-image bytes: [flags=RLE][w][cellHeight][RLE(w*cellHeight px)]. */
@JvmField val cachedBytes: ByteArray
constructor(
@@ -280,16 +280,17 @@ class GlyphAtlas {
}
/**
* The firmware cached-image encoding: [width][height][RLE tokens] covering exactly
* The firmware cached-image encoding: [flags=RLE][width][height][RLE tokens] covering exactly
* width*cellHeight pixels (no row padding), each pixel's stored color being its nibbleAt
* value.
*/
private fun encodeCachedImage(): ByteArray {
var total: Int = (width * cellHeight)
var out: ByteArray = ByteArray((2 + total))
out[0] = (width).toByte()
out[1] = (cellHeight).toByte()
var o: Int = 2
var out: ByteArray = ByteArray((3 + total))
out[0] = DrawProtocol.RLE.toByte()
out[1] = (width).toByte()
out[2] = (cellHeight).toByte()
var o: Int = 3
var i: Int = 0
while ((i < total)) {
var color: Int = nibbleAt((i % width), (i / width))
@@ -11,7 +11,7 @@ import kotlin.jvm.JvmStatic
*
* Unlike glyphs (1-bit ink recolored at draw time), images keep their exact 4bpp values: the cached
* bytes quantize the registered 8bpp pixels with the same GRAY_TO_NIBBLE table the composite is
* packed with, and the planner draws them via mode 13 with an identity LUT (top color 15) and the
* packed with, and the planner draws them via the image draw call with an identity LUT (top color 15) and the
* transparent bit — the draw writes exactly the nonzero-nibble pixels, which is what the
* eligibility check and hole punching are defined over.
*/
@@ -78,7 +78,7 @@ class ImageAtlas {
/** One 4bpp value per pixel, row-major. */
@JvmField val nibbles: ByteArray
/** CFW cached-image bytes: [w][h][RLE(w*h pixels)]. */
/** CFW cached-image bytes: [flags=RLE][w][h][RLE(w*h pixels)]. */
@JvmField val cachedBytes: ByteArray
val resource: CachedResource
@@ -98,10 +98,11 @@ class ImageAtlas {
private fun encodeCachedImage(): ByteArray {
var total: Int = (width * height)
var out: ByteArray = ByteArray((2 + total))
out[0] = (width).toByte()
out[1] = (height).toByte()
var o: Int = 2
var out: ByteArray = ByteArray((3 + total))
out[0] = DrawProtocol.RLE.toByte()
out[1] = (width).toByte()
out[2] = (height).toByte()
var o: Int = 3
var i: Int = 0
while ((i < total)) {
var color: Int = (nibbles[i] and 0xff)
@@ -0,0 +1,41 @@
package com.faceclaw.app
/** Immutable shell snapshot. Keys identify writable surfaces across repaints, not their pixels. */
class ShellScene(val layers: List<Layer>) {
class Layer(val key: Int, val x: Int, val y: Int, val width: Int, val height: Int, val dim: Int, val packed: ByteArray)
val fingerprint: String = layers.joinToString(";") { "${it.key},${it.x},${it.y},${it.width},${it.height},${it.dim},${CachedResource(it.packed).hash}" }
fun preview(screenGray: ByteArray, width: Int, height: Int): ByteArray {
val screen = BmpUtil.pack4bppFromGray8(screenGray, width, height)
val output = ByteArray(screen.size); val resources = HashMap<Int, ByteArray>(); val calls = ArrayList<ByteArray>()
for ((id, layer) in layers.withIndex()) {
if (layer.dim < 256) calls.add(DrawProtocol.lut(width, height, layer.dim))
resources[id] = DrawProtocol.rawImage(layer.width, layer.height, layer.packed)
calls.add(DrawProtocol.image(id, layer.x, layer.y))
}
resources[511] = DrawProtocol.displayList(calls)
DisplayListRenderer(resources).render(511, DisplayListRenderer.Target(screen, width, height), DisplayListRenderer.Target(output, width, height))
val target = DisplayListRenderer.Target(output, width, height)
return ByteArray(width * height) { (target.get(it % width, it / width) * 16).toByte() }
}
companion object {
val EMPTY = ShellScene(emptyList())
fun decode(reader: ByteReader): ShellScene {
val count = reader.getShort().toInt() and 65535; require(count <= 64)
val layers = ArrayList<Layer>()
repeat(count) {
require(reader.remaining() >= 14)
val key = reader.getShort().toInt() and 65535
val x = reader.getShort().toInt(); val y = reader.getShort().toInt()
val w = reader.getShort().toInt() and 65535; val h = reader.getShort().toInt() and 65535
val dim = reader.getShort().toInt() and 65535
val reserved = reader.getShort().toInt(); require(reserved == 0 && dim <= 256)
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)
layers.add(Layer(key, x, y, w, h, dim, BmpUtil.pack4bppFromGray8(gray, w, h)))
}
require(reader.remaining() == 0)
require(layers.map { it.key }.toSet().size == layers.size)
return ShellScene(layers)
}
}
}
@@ -202,6 +202,8 @@ class SurfaceCompositor {
/** One composited full-screen frame plus the metadata the pipeline needs. */
class Composite {
@JvmField var screenGray: ByteArray
@JvmField var shellScene: ShellScene = ShellScene.EMPTY
/** Full-screen 8bpp grayscale pixels, screenWidth*screenHeight bytes. */
@JvmField val gray: ByteArray
@@ -233,6 +235,7 @@ class SurfaceCompositor {
draws: Array<ScreenDraw>?,
) {
this.gray = gray
this.screenGray = gray
this.width = width
this.height = height
this.fingerprint = fingerprint
@@ -241,6 +244,12 @@ class SurfaceCompositor {
}
}
private var shellScene: ShellScene? = null
fun setShellScene(reader: ByteReader) {
val scene = ShellScene.decode(reader)
lock.withLock { shellScene = scene }
}
private class Surface {
@JvmField val id: String
@@ -372,6 +381,7 @@ class SurfaceCompositor {
/** The dim factor (256 = none) that applies to a surface. */
private fun dimForLocked(surface: Surface): Int {
if (shellScene != null) return 256
return (if ((surface.zOrder < underlayDimBelowZOrder)) underlayDim else 256)
}
@@ -560,8 +570,10 @@ class SurfaceCompositor {
if (((screenWidth <= 0) || (screenHeight <= 0))) {
return null
}
var gray: ByteArray = buildGrayLocked()
return Composite(gray, screenWidth, screenHeight, "preview", 0, NO_DRAWS)
val seq = nextCompositeSeq
val result = compositeLocked()
nextCompositeSeq = seq
return result
}
}
@@ -598,7 +610,7 @@ class SurfaceCompositor {
fingerprint.append(screenWidth).append('x').append(screenHeight)
var draws: MutableList<ScreenDraw> = ArrayList()
for (surface in ordered) {
if (!surface.visible) {
if (!surface.visible || (shellScene != null && surface.id == "shell")) {
continue
}
blendLocked(gray, surface)
@@ -644,14 +656,10 @@ class SurfaceCompositor {
fingerprint.append(":dim").append(dim)
}
}
return Composite(
gray,
screenWidth,
screenHeight,
fingerprint.toString(),
nextCompositeSeq++,
draws.toTypedArray(),
)
val scene = if (surfaces.values.any { it.visible && it.zOrder > 1 }) ShellScene.EMPTY else shellScene ?: ShellScene.EMPTY
val preview = if (shellScene != null) scene.preview(gray, screenWidth, screenHeight) else gray
return Composite(preview, screenWidth, screenHeight, fingerprint.append("|shell:").append(scene.fingerprint).toString(),
nextCompositeSeq++, draws.toTypedArray()).also { it.screenGray = gray; it.shellScene = scene }
}
private fun blendLocked(gray: ByteArray, surface: Surface): Unit {
@@ -230,6 +230,8 @@ class IosSurfaceCompositor(width: Int, height: Int) {
fun blank(blanked: Boolean) = compositor.setBlanked(blanked)
fun shell(data: NSData) = compositor.setShellScene(IosByteReader(data))
fun submit(id: String, data: NSData, x: Int, y: Int, width: Int, height: Int) =
submitDraws(id, data, x, y, width, height, null)
@@ -21,24 +21,23 @@ class IosTextureFrame internal constructor(internal val composite: SurfaceCompos
val pixels: NSData = composite.gray.data()
}
class IosTexturePlan internal constructor(result: TexturePlanner.Result, val usedBytes: Int) {
val payload: NSData = result.payload.data()
val resourceCommands: List<NSData> = result.resourceCommands.map { it.data() }
class IosTexturePlan internal constructor(result: ScenePlanner.Plan, val usedBytes: Int) {
val payload: NSData = result.commands.last().data()
val resourceCommands: List<NSData> = result.commands.dropLast(1).map { it.data() }
val nextFid: Int = result.nextFid
}
/** One instance per BLE session, called on the session's serial JS thread. */
class IosTexturePlanner {
private val cache = ResourceCacheState()
private val planner = ScenePlanner(cache)
fun reset() = cache.reset()
fun plan(previous: NSData?, next: NSData, frame: IosTextureFrame, firstId: Int): IosTexturePlan? {
val composite = frame.composite
val result = TexturePlanner.plan(
previous?.byteArray(), next.byteArray(), composite.width, composite.height,
composite.draws, cache, firstId, true, ConnectionOptions.MULTI_RECT_MAX_RECTS,
) ?: return null
val result = planner.plan(BmpUtil.pack4bppFromGray8(composite.screenGray, composite.width, composite.height),
composite.width, composite.height, composite.draws, composite.shellScene, firstId)
return IosTexturePlan(result, cache.usedBytes())
}
}