Resource management for the texture cache

This commit is contained in:
James Babcock committed 2026-09-20 18:23:33 -07:00
1 parent 146fe66bdd
commit 0c3d7517bc
28 files changed
+569 -524

No files matched your search

+1 -1
View File
@@ -161,7 +161,7 @@
},
{
"java": "com/faceclaw/app/g2protocol/TextureCacheState.java",
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TextureCacheState.kt"
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ResourceCacheState.kt"
},
{
"java": "com/faceclaw/app/g2protocol/TexturePlanner.java",
@@ -249,7 +249,7 @@ class BleImageOptimizer {
* multiples of 4 (=> left>>1, width>>1 are whole bytes), top/ height multiples of 2. The
* box pixels are top-down rows of `next` (4bpp packed, width>>1 bytes/row), run-length
* encoded before deflate. Shared by the single-bbox path, each rect of a mode-8 multi-rect
* batch, and the texture-cache planner.
* batch, and the resource-cache planner.
*/
@JvmStatic
fun encodeMode3Rect(
@@ -10,6 +10,13 @@ class CfwMessageWindow private constructor() {
const val ACK_TIMEOUT_MS = 500
const val MAX_RETRIES = 3
/** ID reuse must not invalidate an earlier command still eligible for replay. */
@JvmStatic
fun canSend(messages: Iterable<OutboundMessage>, candidate: OutboundMessage): Boolean =
candidate.sid != CfwTransport.SID ||
candidate.message.firstOrNull()?.toInt() != ResourceCacheState.EVICT_MODE ||
messages.none { it.sid == CfwTransport.SID }
@JvmStatic
fun acknowledgedHead(messages: Iterable<OutboundMessage>): OutboundMessage? {
for (message in messages) {
@@ -71,8 +71,8 @@ class ConnectionOptions {
// Falls back to the single bounding box whenever multi-rect isn't smaller.
@JvmField val MULTI_RECT_FRAMES = true
// Ship text as on-glasses cached-glyph draws (CFW modes 12/14) instead of
// Ship text as on-glasses cached-glyph draws (CFW modes 19/20/21/22) instead of
// pixels, punching glyph ink out of the baked deltas (see TexturePlanner).
// Also gated at runtime on the firmware advertising texcache12 + texstr14.
// Also gated at runtime on the firmware advertising Faceclaw/23 or later.
@JvmField val TEXTURE_CACHE_FRAMES = true
}
@@ -0,0 +1,150 @@
package com.faceclaw.app
/** Immutable content-addressed bytes; equality verifies bytes when hashes collide. */
class CachedResource(bytes: ByteArray) {
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 equals(other: Any?): Boolean = this === other ||
other is CachedResource && hash == other.hash && bytes.contentEquals(other.bytes)
}
/**
* Phone-side residency for the firmware's compacting resource arena. The owner serializes calls.
* prepare() protects the current frame's hits before admitting misses, evicting only unprotected
* LRU entries. Commands must be sent in returned order, before any dependent draws.
*/
class ResourceCacheState {
companion object {
const val CACHE_SIZE = 262144
const val RESOURCE_COUNT = 512
const val TABLE_BYTES = RESOURCE_COUNT * 4
const val ARENA_BYTES = CACHE_SIZE - TABLE_BYTES
const val MAX_RESOURCE_SIZE = 65536
const val BLOCK_HEADER_BYTES = 16
const val UPLOAD_MODE = 21
const val EVICT_MODE = 22
fun allocationBytes(size: Int): Int = BLOCK_HEADER_BYTES + ((size + 3) and -4)
}
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)
private var allocated = 0
private var clock = 0L
private var resetRequired = true
private val evictions = linkedSetOf<Int>()
private val uploads = ArrayList<Resident>()
/** Forget uncertain device state. The next upload first evicts every ID on both lenses. */
fun reset() {
residents.clear(); occupied.fill(false); allocated = 0; clock = 0
evictions.clear(); uploads.clear(); resetRequired = true
}
fun usedBytes(): Int = TABLE_BYTES + allocated
fun residentCount(): Int = residents.size
fun resourceId(resource: CachedResource): Int = residents[resource]?.id ?: -1
/** Called once per frame after coalescing. -1 entries stay baked into the pixel fallback. */
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 resources) residents[resource]?.let { protected.add(it.id) }
return IntArray(resources.size) { index ->
val resource = resources[index]
val hit = residents[resource]
if (hit != null) {
hit.lastUsed = ++clock
hit.id
} else if (resource.bytes.isEmpty() || resource.bytes.size > MAX_RESOURCE_SIZE) {
-1
} else {
val needed = allocationBytes(resource.bytes.size)
// Check feasibility before evicting anything for a request that cannot fit.
val available = ARENA_BYTES - allocated + residents.values
.filter { it.id !in protected }.sumOf { allocationBytes(it.resource.bytes.size) }
val canGetId = residents.size < RESOURCE_COUNT || residents.values.any { it.id !in protected }
if (needed > available || !canGetId) -1
else {
while (allocated + needed > ARENA_BYTES || residents.size >= RESOURCE_COUNT) {
val victim = residents.values.filter { it.id !in protected }.minBy { it.lastUsed }
residents.remove(victim.resource); occupied[victim.id] = false
allocated -= allocationBytes(victim.resource.bytes.size)
evictions.add(victim.id)
}
val id = occupied.indexOfFirst { !it }
val entry = Resident(id, resource, ++clock)
occupied[id] = true; allocated += needed
residents[resource] = entry; uploads.add(entry); protected.add(id)
id
}
}
}
}
/** A rejected image plan must not commit unsent allocations or LRU changes. */
fun checkpoint(): () -> Unit {
val oldResidents = residents.mapValues { it.value.copy() }
val oldOccupied = occupied.copyOf()
val oldAllocated = allocated
val oldClock = clock
val oldReset = resetRequired
val oldEvictions = evictions.toList()
val oldUploads = uploads.toList()
return {
residents.clear(); residents.putAll(oldResidents); oldOccupied.copyInto(occupied)
allocated = oldAllocated; clock = oldClock; resetRequired = oldReset
evictions.clear(); evictions.addAll(oldEvictions)
uploads.clear(); uploads.addAll(oldUploads)
}
}
/** Batched evictions followed by bounded upload chunks, including exactly 64 KiB resources. */
fun drainCommands(maxPayloadBytes: Int): MutableList<ByteArray> {
require(maxPayloadBytes in 14..65535)
val result = ArrayList<ByteArray>()
val ids = if (resetRequired && uploads.isNotEmpty()) (0 until RESOURCE_COUNT).toList() else evictions.toList()
for (batch in ids.chunked((maxPayloadBytes - 3) / 2)) {
val out = ByteSink()
out.write(EVICT_MODE); write16(out, batch.size)
batch.forEach { write16(out, it) }
result.add(out.toByteArray())
}
var entries = ArrayList<ByteArray>()
var entryIds = HashSet<Int>()
var size = 3
fun flush() {
if (entries.isEmpty()) return
val out = ByteSink()
out.write(UPLOAD_MODE); write16(out, entries.size)
entries.forEach { out.write(it, 0, it.size) }
result.add(out.toByteArray())
entries = ArrayList(); entryIds = HashSet(); size = 3
}
for (entry in uploads) {
val data = entry.resource.bytes
var offset = 0
while (offset < data.size) {
val count = minOf(data.size - offset, maxPayloadBytes - 13)
val out = ByteSink()
write16(out, entry.id); write32(out, data.size)
write16(out, offset); write16(out, count)
out.write(data, offset, count)
val bytes = out.toByteArray()
if (size + bytes.size > maxPayloadBytes || entry.id in entryIds) flush()
entries.add(bytes); entryIds.add(entry.id); size += bytes.size
offset += count
}
}
flush()
if (uploads.isNotEmpty()) resetRequired = false
evictions.clear(); uploads.clear()
return result
}
private fun write16(out: ByteSink, value: Int) { out.write(value and 255); out.write((value ushr 8) and 255) }
private fun write32(out: ByteSink, value: Int) { write16(out, value); write16(out, value ushr 16) }
}
@@ -1,191 +0,0 @@
package com.faceclaw.app
/**
* Phone-side model of the CFW's flat 256 KiB texture cache (zlib_glue mode 18). The firmware holds
* only bytes; every structural decision — where font tables and glyph images live, what is
* resident, when to start over — is made here. Layout per font: a 96-entry uint32 offset table
* (chars 32..127) whose entries are written lazily as each glyph is uploaded; glyph images are the
* GlyphAtlas cached bytes ([w][h][4bpp RLE]).
*
* Allocation is a bump pointer with reset-on-full: the realistic working set (a few fonts' tables
* plus ~hundreds of glyphs at tens of bytes each) is far below 256 KiB, so eviction sophistication
* buys nothing. After a reset the firmware cache still holds stale bytes; that is safe because a
* draw only ever references (font table entry, glyph image) pairs uploaded in the current
* generation — stale table entries are never named in a mode-20 string.
*
* Upload entries accumulate as complete [offset u32][len u16][data] records and drain into one or
* more standalone mode-18 payloads, each independently valid, enqueued ahead of the image message
* that references them (the transport is FIFO, so no ack round-trip is needed before use).
*
* Not thread-safe by itself: the communicator calls it under its own lock.
*/
class TextureCacheState {
companion object {
const val CACHE_SIZE: Int = 262144
private val FONT_TABLE_BYTES: Int = (96 * 4)
}
private var allocEnd: Int = 0
private val fontTableOffsets: MutableMap<Int, Int> = HashMap()
private val glyphOffsets: MutableMap<Long, Int> = HashMap()
private val imageOffsets: MutableMap<Int, Int> = HashMap()
private val pendingEntries: MutableList<ByteArray> = ArrayList()
private var pendingBytes: Int = 0
private var generation: Int = 0
/**
* Forget everything: the on-glasses cache is gone (session teardown, lease loss) or no longer
* trustworthy (upload timeout), or we ran out of space. Glyphs and images re-upload lazily on
* next use.
*/
fun reset(): Unit {
allocEnd = 0
fontTableOffsets.clear()
glyphOffsets.clear()
imageOffsets.clear()
pendingEntries.clear()
pendingBytes = 0
generation++
}
/** Bumped by every reset; lets a planner detect a mid-plan reset. */
fun generation(): Int {
return generation
}
/**
* Bytes currently allocated out of CACHE_SIZE, for logging/tuning (a sudden drop in the frame
* log means a reset-on-full re-upload cycle — the signal that the 256 KiB budget is being
* outgrown).
*/
fun usedBytes(): Int {
return allocEnd
}
/**
* The font's 96-entry table offset in the cache, or -1 when it exists but this glyph can't fit.
*/
fun fontTableOffset(fontId: Int): Int {
var existing: Int? = fontTableOffsets.get(fontId)
return (if ((existing == null)) -1 else existing)
}
/**
* Ensure a glyph image (and its font table slot) is resident, queuing the mode-18 upload
* entries for anything newly placed. Returns the glyph's image offset, or -1 when the cache is
* full (caller resets and retries) or the encoding is outside the mode-20 table (32..127).
*/
fun ensureGlyph(fontId: Int, encoding: Int, glyph: GlyphAtlas.Glyph?): Int {
if ((((glyph == null) || (encoding < 32)) || (encoding > 127))) {
return -1
}
var key: Long = (((fontId).toLong() shl 32) or (encoding.toLong() and 0xffffffffL))
var existing: Int? = glyphOffsets.get(key)
if ((existing != null)) {
return existing
}
var table: Int? = fontTableOffsets.get(fontId)
var need: Int = (glyph.cachedBytes.size + (if ((table == null)) FONT_TABLE_BYTES else 0))
if (((allocEnd + need) > CACHE_SIZE)) {
return -1
}
if ((table == null)) {
table = allocEnd
allocEnd += FONT_TABLE_BYTES
fontTableOffsets.put(fontId, table)
}
var offset: Int = allocEnd
allocEnd += glyph.cachedBytes.size
glyphOffsets.put(key, offset)
addEntry(offset, glyph.cachedBytes)
addEntry(
(table + (4 * (encoding - 32))),
byteArrayOf(
(offset).toByte(),
((offset shr 8)).toByte(),
((offset shr 16)).toByte(),
((offset shr 24)).toByte(),
),
)
return offset
}
/**
* Ensure an ImageAtlas entry is resident (mode-19 draws reference it by raw cache offset; no
* table involved). Returns the image offset, or -1 when the cache is full (caller resets and
* retries).
*/
fun ensureImage(imageId: Int, image: ImageAtlas.Entry?): Int {
if ((image == null)) {
return -1
}
var existing: Int? = imageOffsets.get(imageId)
if ((existing != null)) {
return existing
}
if (((allocEnd + image.cachedBytes.size) > CACHE_SIZE)) {
return -1
}
var offset: Int = allocEnd
allocEnd += image.cachedBytes.size
imageOffsets.put(imageId, offset)
addEntry(offset, image.cachedBytes)
return offset
}
private fun addEntry(offset: Int, data: ByteArray): Unit {
var entry: ByteArray = ByteArray((6 + data.size))
entry[0] = ((offset and 0xff)).toByte()
entry[1] = (((offset shr 8) and 0xff)).toByte()
entry[2] = (((offset shr 16) and 0xff)).toByte()
entry[3] = (((offset shr 24) and 0xff)).toByte()
entry[4] = ((data.size and 0xff)).toByte()
entry[5] = (((data.size shr 8) and 0xff)).toByte()
data.copyInto(entry, 6, 0, 0 + data.size)
pendingEntries.add(entry)
pendingBytes += entry.size
}
fun hasPendingUploads(): Boolean {
return !pendingEntries.isEmpty()
}
/** Total queued upload bytes (entry framing included), for logging. */
fun pendingUploadBytes(): Int {
return pendingBytes
}
/**
* Drain the queued entries into complete mode-18 payloads ([18][entries]), each at most
* maxPayloadBytes so a payload fits one BLE image message without fragmentation. A single entry
* larger than the cap still gets its own payload (the transport can fragment it).
*/
fun drainUploadPayloads(maxPayloadBytes: Int): MutableList<ByteArray> {
var payloads: MutableList<ByteArray> = ArrayList()
var current: ByteSink? = null
for (entry in pendingEntries) {
if (((current != null) && ((current.size() + entry.size) > maxPayloadBytes))) {
payloads.add(current.toByteArray())
current = null
}
if ((current == null)) {
current = ByteSink(minOf(maxPayloadBytes, (4 + entry.size)))
current.write(18)
}
current.write(entry, 0, entry.size)
}
if ((current != null)) {
payloads.add(current.toByteArray())
}
pendingEntries.clear()
pendingBytes = 0
return payloads
}
}
@@ -6,14 +6,14 @@ import kotlin.jvm.JvmStatic
/**
* Plans a screen update that ships text and icons as on-glasses cached draws instead of pixels (CFW
* modes 18/19/20; see g2flash/patches/zlib_glue.c and texture_cache.c).
* modes 19/20/21/22; see g2flash/patches/zlib_glue.c and resource_cache.c).
*
* Approach: dirty rects come from comparing the fully composited old and new frames, exactly as the
* plain incremental path does. For each deferred draw (glyph or image) whose pixels intersect a
* sent rect, check that every pixel the on-glasses draw would write lands correct — i.e. the new
* composite's 4bpp value at each in-panel nonzero-source pixel equals what the draw produces (this
* one check subsumes plane occlusion, surface occlusion/clipping, and draw-on-draw overlap). A draw
* that passes and is (or can be made) resident in the texture cache is replayed on-glasses: its
* that passes and is (or can be made) resident in the resource cache is replayed on-glasses: its
* written pixels are punched to 0 in the delta rect content — which is what makes text and icons
* nearly free to compress — and re-emitted as a mode-20 string or mode-19 image sub-message in the
* same atomic mode-8 batch, so the shadow after apply equals the full composite exactly. Everything
@@ -40,7 +40,7 @@ class TexturePlanner {
/** Cap on mode-15 builtin-font string sub-messages per update. */
private const val MAX_FWTEXT_RUNS: Int = 80
/** Mode-18 payload size that fits one BLE image message comfortably. */
/** Resource command payload size that fits one BLE image message comfortably. */
private const val UPLOAD_PAYLOAD_MAX: Int = 3600
/** Max x-adjust control bytes between two glyphs before starting a new run. */
@@ -62,7 +62,7 @@ class TexturePlanner {
width: Int,
height: Int,
draws: Array<SurfaceCompositor.ScreenDraw>?,
cache: TextureCacheState,
cache: ResourceCacheState,
fidStart: Int,
allowMultiRect: Boolean,
maxRects: Int,
@@ -180,6 +180,7 @@ class TexturePlanner {
if ((selected.isEmpty() && fwSubs.isEmpty())) {
return null
}
val rollback = cache.checkpoint()
var cacheStartedAtMs: Long = platform.elapsedRealtimeMs()
var drawable: MutableList<Selected> =
(if (selected.isEmpty()) mutableListOf() else ensureResident(cache, selected))
@@ -194,7 +195,7 @@ class TexturePlanner {
}
}
var runs: MutableList<ByteArray> = ArrayList()
var drawnGlyphs: MutableList<Selected> = buildRuns(cache, glyphDrawable, runs)
var drawnGlyphs: MutableList<Selected> = buildRuns(glyphDrawable, runs)
bakedCandidates += (glyphDrawable.size - drawnGlyphs.size)
var drawn: MutableList<Selected> = ArrayList(drawnGlyphs)
drawn.addAll(imageDrawable)
@@ -203,7 +204,7 @@ class TexturePlanner {
((((rects.size + runs.size) + imageDrawable.size) + fwSubs.size) > 255))
) {
if (!selected.isEmpty()) {
cache.reset()
rollback()
}
return null
}
@@ -237,17 +238,21 @@ class TexturePlanner {
}
}
for (sel in imageDrawable) {
subs.add(encodeImageDraw(cache, sel))
subs.add(encodeImageDraw(sel))
}
subs.addAll(runs)
subs.addAll(fwSubs)
var payload: ByteArray = assembleMode8(subs)
var uploadBytes: Int = cache.pendingUploadBytes()
var uploads: MutableList<ByteArray> = cache.drainUploadPayloads(UPLOAD_PAYLOAD_MAX)
if (payload.size > CfwTransport.MAX_MESSAGE) {
rollback()
return null
}
val resourceCommands = cache.drainCommands(UPLOAD_PAYLOAD_MAX)
val uploadBytes = resourceCommands.sumOf { it.size }
var result: Result =
Result(
payload,
uploads,
resourceCommands,
fid,
rects.size,
drawnGlyphs.size,
@@ -694,65 +699,31 @@ class TexturePlanner {
}
}
/**
* Ensure residency for all of `selected`, resetting the cache once if the allocator fills
* mid-plan (a reset invalidates offsets handed out earlier in the same pass, so the whole
* pass reruns). Returns the draws that are resident afterwards.
*/
/** Protect every hit before LRU admission so a miss cannot evict this frame's later draws. */
private fun ensureResident(
cache: TextureCacheState,
cache: ResourceCacheState,
selected: MutableList<Selected>,
): MutableList<Selected> {
run {
var attempt: Int = 0
while ((attempt < 2)) {
var resident: MutableList<Selected> = ArrayList(selected.size)
var full: Boolean = false
for (sel in selected) {
var offset: Int =
(if ((sel.glyphAtlas != null))
cache.ensureGlyph(
sel.draw.fontId,
sel.draw.encoding,
sel.glyphAtlas,
)
else cache.ensureImage(sel.draw.imageId, sel.imageAtlas))
if ((offset >= 0)) {
resident.add(sel)
} else {
full = true
}
}
if ((!full || (attempt == 1))) {
return resident
}
cache.reset()
attempt++
}
val fonts = selected.filter { it.glyphAtlas != null }.groupBy { it.draw.fontId }
.mapValues { (id, draws) -> FontResourceAtlas.get(id, draws.map { it.draw.encoding }.toSet()) }
val candidates = selected.filter { it.glyphAtlas == null || it.draw.encoding in fonts.getValue(it.draw.fontId).encodings }
val resources = candidates.map { sel ->
if (sel.glyphAtlas != null) fonts.getValue(sel.draw.fontId).resource else sel.imageAtlas!!.resource
}
return mutableListOf()
val ids = cache.prepare(resources)
return candidates.filterIndexed { index, sel -> sel.resourceId = ids[index]; ids[index] >= 0 }.toMutableList()
}
/** Mode-19 cached-image draw: [19][offset u32][x u16][y u16][options u8]. */
/** Mode-19 cached-image draw: [19][resourceId u16][x u16][y u16][options u8]. */
@JvmStatic
private fun encodeImageDraw(cache: TextureCacheState, sel: Selected): ByteArray {
var offset: Int = cache.ensureImage(sel.draw.imageId, sel.imageAtlas)
var sub: ByteArray = ByteArray(10)
sub[0] = 19
sub[1] = ((offset and 0xff)).toByte()
sub[2] = (((offset shr 8) and 0xff)).toByte()
sub[3] = (((offset shr 16) and 0xff)).toByte()
sub[4] = (((offset shr 24) and 0xff)).toByte()
sub[5] = ((sel.draw.x and 0xff)).toByte()
sub[6] = (((sel.draw.x shr 8) and 0xff)).toByte()
sub[7] = ((sel.draw.y and 0xff)).toByte()
sub[8] = (((sel.draw.y shr 8) and 0xff)).toByte()
sub[9] = (IMAGE_DRAW_OPTIONS).toByte()
return sub
}
private fun encodeImageDraw(sel: Selected): ByteArray = byteArrayOf(
19, sel.resourceId.toByte(), (sel.resourceId ushr 8).toByte(),
sel.draw.x.toByte(), (sel.draw.x ushr 8).toByte(),
sel.draw.y.toByte(), (sel.draw.y ushr 8).toByte(), IMAGE_DRAW_OPTIONS.toByte(),
)
/**
* Group selected glyphs into mode-20 string sub-messages: [20][fontTable u32][x u16][y
* Group selected glyphs into mode-20 string sub-messages: [20][fontResource u16][x u16][y
* u16][options u8][strlen u8][string] One run per (font, line y, top color) span; within a
* run, control bytes 1..31 adjust x by -10..+20 to hit each glyph's exact position, and
* each glyph advances x by its cached width. Order across runs is free: every drawn glyph's
@@ -761,7 +732,6 @@ class TexturePlanner {
*/
@JvmStatic
private fun buildRuns(
cache: TextureCacheState,
drawable: MutableList<Selected>,
outRuns: MutableList<ByteArray>,
): MutableList<Selected> {
@@ -776,8 +746,8 @@ class TexturePlanner {
var i: Int = 0
while (((i < sorted.size) && (outRuns.size < MAX_GLYPH_RUNS))) {
var first: Selected = sorted.get(i)
var fontTable: Int = cache.fontTableOffset(first.draw.fontId)
if ((fontTable < 0)) {
val fontResource = first.resourceId
if ((fontResource < 0)) {
i++
continue
}
@@ -809,19 +779,17 @@ class TexturePlanner {
}
if (!runGlyphs.isEmpty()) {
var stringBytes: ByteArray = string.toByteArray()
var run: ByteArray = ByteArray((11 + stringBytes.size))
val run = ByteArray(9 + stringBytes.size)
run[0] = 20
run[1] = ((fontTable and 0xff)).toByte()
run[2] = (((fontTable shr 8) and 0xff)).toByte()
run[3] = (((fontTable shr 16) and 0xff)).toByte()
run[4] = (((fontTable shr 24) and 0xff)).toByte()
run[5] = ((first.gx and 0xff)).toByte()
run[6] = (((first.gx shr 8) and 0xff)).toByte()
run[7] = ((first.draw.y and 0xff)).toByte()
run[8] = (((first.draw.y shr 8) and 0xff)).toByte()
run[9] = ((first.top or 0x10)).toByte()
run[10] = (stringBytes.size).toByte()
stringBytes.copyInto(run, 11, 0, 0 + stringBytes.size)
run[1] = fontResource.toByte()
run[2] = (fontResource ushr 8).toByte()
run[3] = first.gx.toByte()
run[4] = (first.gx ushr 8).toByte()
run[5] = first.draw.y.toByte()
run[6] = (first.draw.y ushr 8).toByte()
run[7] = (first.top or 0x10).toByte()
run[8] = stringBytes.size.toByte()
stringBytes.copyInto(run, 9)
outRuns.add(run)
drawn.addAll(runGlyphs)
i = j
@@ -876,8 +844,8 @@ class TexturePlanner {
/** Complete image payload: a mode-8 batch of rect deltas + cached draws. */
@JvmField val payload: ByteArray
/** Mode-18 upload payloads to enqueue BEFORE the image message. */
@JvmField val uploads: MutableList<ByteArray>
/** Mode-22 evictions and mode-21 uploads to enqueue BEFORE the image message. */
@JvmField val resourceCommands: MutableList<ByteArray>
/** Next mode-3 frame id (deltas consumed some). */
@JvmField val nextFid: Int
@@ -919,7 +887,7 @@ class TexturePlanner {
constructor(
payload: ByteArray,
uploads: MutableList<ByteArray>,
resourceCommands: MutableList<ByteArray>,
nextFid: Int,
rectCount: Int,
drawnGlyphs: Int,
@@ -932,7 +900,7 @@ class TexturePlanner {
fullFrame: Boolean,
) {
this.payload = payload
this.uploads = uploads
this.resourceCommands = resourceCommands
this.nextFid = nextFid
this.rectCount = rectCount
this.drawnGlyphs = drawnGlyphs
@@ -948,6 +916,8 @@ class TexturePlanner {
/** One draw selected for on-glasses replay. */
private class Selected {
var resourceId: Int = -1
@JvmField val draw: SurfaceCompositor.ScreenDraw
@JvmField val glyphAtlas: GlyphAtlas.Glyph?
@@ -0,0 +1,39 @@
package com.faceclaw.app
/** Self-contained font snapshots; their 96 u16 glyph offsets survive firmware compaction. */
class FontResourceAtlas {
class Font(val resource: CachedResource, val encodings: Set<Int>)
companion object {
private val lock = protocolPlatform().createLock()
private val fonts = HashMap<Int, Font>()
fun get(fontId: Int, requested: Set<Int>): Font {
lock.withLock {
val old = fonts[fontId]
if (old != null && old.encodings.containsAll(requested)) return old
val glyphs = HashMap<Int, GlyphAtlas.Glyph>()
var size = 192
// 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
val glyph = GlyphAtlas.get(fontId, encoding) ?: continue
if (size + glyph.cachedBytes.size > ResourceCacheState.MAX_RESOURCE_SIZE) continue
glyphs[encoding] = glyph; size += glyph.cachedBytes.size
}
val bytes = ByteArray(size)
var offset = 192
for (encoding in glyphs.keys.sorted()) {
val glyph = glyphs.getValue(encoding)
val slot = (encoding - 32) * 2
bytes[slot] = offset.toByte(); bytes[slot + 1] = (offset ushr 8).toByte()
glyph.cachedBytes.copyInto(bytes, offset)
offset += glyph.cachedBytes.size
}
val font = Font(CachedResource(bytes), glyphs.keys.toSet())
fonts[fontId] = font
return font
}
}
}
}
@@ -6,7 +6,7 @@ import kotlin.jvm.JvmStatic
/**
* Process-wide registry of glyph rasters, fed from the TS side so glyph identity survives to the
* BLE encoder (see notes/texture-cache-display-list- design.md). A frame's glyph list references
* entries here by (fontId, encoding); the texture-cache planner uses the raster three ways:
* entries here by (fontId, encoding); the resource-cache planner uses the raster three ways:
*
* - 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,
@@ -4,7 +4,7 @@ import kotlin.jvm.JvmField
import kotlin.jvm.JvmStatic
/**
* Process-wide registry of icon/image rasters for the texture-cache pipeline, the image counterpart
* Process-wide registry of icon/image rasters for the resource-cache pipeline, the image counterpart
* of GlyphAtlas. Entries are content-addressed: the TS side keys each image by a hash of its
* dimensions and pixels, so the same icon registered from any thread or rendered at any time
* dedupes to one entry, and an icon whose content changes is simply a new entry.
@@ -81,11 +81,14 @@ class ImageAtlas {
/** CFW cached-image bytes: [w][h][RLE(w*h pixels)]. */
@JvmField val cachedBytes: ByteArray
val resource: CachedResource
constructor(width: Int, height: Int, nibbles: ByteArray) {
this.width = width
this.height = height
this.nibbles = nibbles
this.cachedBytes = encodeCachedImage()
this.resource = CachedResource(cachedBytes)
}
/** The 4bpp value the mode-13 draw would write at (col, row); 0 = skipped. */
@@ -98,7 +98,7 @@ class SurfaceCompositor {
/**
* One deferred draw (a text glyph or an icon image) within a frame, in screen coordinates. The
* draw's pixels are already baked into the composited gray buffer (the TS side bakes before
* submitting); this record preserves the draw's identity so the texture-cache planner can
* submitting); this record preserves the draw's identity so the resource-cache planner can
* replay it as an on-glasses cached draw instead of image bytes.
*
* Glyphs: x/y are the pen position and line top; the raster (and its bearing/cell placement)
@@ -16,7 +16,7 @@ class BmpUtil {
ByteArray(256) { v -> if (v == 0) 0 else minOf(15, (v + 8) shr 4).toByte() }
/**
* The 4bpp level an 8-bit gray value packs to. Public so the texture-cache planner computes
* The 4bpp level an 8-bit gray value packs to. Public so the resource-cache planner computes
* a glyph draw's top color with the exact quantization the composited frame was packed
* with.
*/
@@ -23,13 +23,13 @@ class IosTextureFrame internal constructor(internal val composite: SurfaceCompos
class IosTexturePlan internal constructor(result: TexturePlanner.Result, val usedBytes: Int) {
val payload: NSData = result.payload.data()
val uploads: List<NSData> = result.uploads.map { it.data() }
val resourceCommands: List<NSData> = result.resourceCommands.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 = TextureCacheState()
private val cache = ResourceCacheState()
fun reset() = cache.reset()
@@ -39,12 +39,6 @@ class IosTexturePlanner {
previous?.byteArray(), next.byteArray(), composite.width, composite.height,
composite.draws, cache, firstId, true, ConnectionOptions.MULTI_RECT_MAX_RECTS,
) ?: return null
// A plan is atomic. If it cannot fit the CFW transport, discard its uploads
// and residency too, before the session falls back to ordinary pixel bands.
if (result.payload.size > 65535 || result.uploads.any { it.size > 65535 }) {
cache.reset()
return null
}
return IosTexturePlan(result, cache.usedBytes())
}
}