Expand texture cache to 256kb

This commit is contained in:
James Babcock committed 2026-09-17 18:52:50 -07:00
1 parent b9b8128756
commit 5698c584a3
4 files changed
+85 -78

No files matched your search

@@ -7,31 +7,31 @@ import java.util.List;
import java.util.Map;
/**
* Phone-side model of the CFW's flat 64 KiB texture cache (zlib_glue mode 12).
* 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 uint16 offset table (chars 32..127)
* 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 64 KiB, so eviction sophistication buys nothing. After a reset the
* 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-14
* current generation — stale table entries are never named in a mode-20
* string.
*
* Upload entries accumulate as complete [offset u16][len u16][data] records
* and drain into one or more standalone mode-12 payloads, each independently
* 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.
*/
public final class TextureCacheState {
public static final int CACHE_SIZE = 65536;
private static final int FONT_TABLE_BYTES = 96 * 2;
public static final int CACHE_SIZE = 262144;
private static final int FONT_TABLE_BYTES = 96 * 4;
private int allocEnd = 0;
private final Map<Integer, Integer> fontTableOffsets = new HashMap<>();
@@ -64,7 +64,7 @@ public final class TextureCacheState {
/**
* 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 64 KiB budget is being outgrown).
* the signal that the 256 KiB budget is being outgrown).
*/
public int usedBytes() {
return allocEnd;
@@ -78,9 +78,9 @@ public final class TextureCacheState {
/**
* Ensure a glyph image (and its font table slot) is resident, queuing the
* mode-12 upload entries for anything newly placed. Returns the glyph's
* 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-14 table (32..127).
* or the encoding is outside the mode-20 table (32..127).
*/
public int ensureGlyph(int fontId, int encoding, GlyphAtlas.Glyph glyph) {
if (glyph == null || encoding < 32 || encoding > 127) return -1;
@@ -104,13 +104,14 @@ public final class TextureCacheState {
glyphOffsets.put(key, offset);
addEntry(offset, glyph.cachedBytes);
addEntry(table + 2 * (encoding - 32),
new byte[]{(byte) (offset & 0xff), (byte) ((offset >> 8) & 0xff)});
addEntry(table + 4 * (encoding - 32),
new byte[]{(byte) offset, (byte) (offset >> 8),
(byte) (offset >> 16), (byte) (offset >> 24)});
return offset;
}
/**
* Ensure an ImageAtlas entry is resident (mode-13 draws reference it by
* 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).
*/
@@ -127,12 +128,14 @@ public final class TextureCacheState {
}
private void addEntry(int offset, byte[] data) {
byte[] entry = new byte[4 + data.length];
byte[] entry = new byte[6 + data.length];
entry[0] = (byte) (offset & 0xff);
entry[1] = (byte) ((offset >> 8) & 0xff);
entry[2] = (byte) (data.length & 0xff);
entry[3] = (byte) ((data.length >> 8) & 0xff);
System.arraycopy(data, 0, entry, 4, data.length);
entry[2] = (byte) ((offset >> 16) & 0xff);
entry[3] = (byte) ((offset >> 24) & 0xff);
entry[4] = (byte) (data.length & 0xff);
entry[5] = (byte) ((data.length >> 8) & 0xff);
System.arraycopy(data, 0, entry, 6, data.length);
pendingEntries.add(entry);
pendingBytes += entry.length;
}
@@ -147,7 +150,7 @@ public final class TextureCacheState {
}
/**
* Drain the queued entries into complete mode-12 payloads ([12][entries]),
* 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).
@@ -162,7 +165,7 @@ public final class TextureCacheState {
}
if (current == null) {
current = new ByteArrayOutputStream(Math.min(maxPayloadBytes, 4 + entry.length));
current.write(12);
current.write(18);
}
current.write(entry, 0, entry.length);
}
@@ -7,7 +7,7 @@ import java.util.List;
/**
* Plans a screen update that ships text and icons as on-glasses cached draws
* instead of pixels (CFW modes 12/13/14; see g2flash/patches/zlib_glue.c and
* instead of pixels (CFW modes 18/19/20; see g2flash/patches/zlib_glue.c and
* texture_cache.c).
*
* Approach: dirty rects come from comparing the fully composited old and new
@@ -19,8 +19,8 @@ import java.util.List;
* 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
* 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-14
* string or mode-13 image sub-message in the same atomic mode-8 batch, so
* 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 else
* stays baked.
*
@@ -30,7 +30,7 @@ import java.util.List;
public final class TexturePlanner {
private TexturePlanner() {}
/** Cap on mode-14 string sub-messages (mode-8 count is a u8). */
/** Cap on mode-20 string sub-messages (mode-8 count is a u8). */
private static final int MAX_GLYPH_RUNS = 180;
/**
* Soft cap on cached-glyph selection per update, before run grouping.
@@ -38,21 +38,21 @@ public final class TexturePlanner {
* screens draw everything; the run budget is the real bound.
*/
private static final int MAX_SELECTED_GLYPHS = 4600;
/** Cap on mode-13 image sub-messages per update. */
/** Cap on mode-19 image sub-messages per update. */
private static final int MAX_IMAGE_DRAWS = 60;
/** Cap on mode-15 builtin-font string sub-messages per update. */
private static final int MAX_FWTEXT_RUNS = 80;
/** Mode-12 payload size that fits one BLE image message comfortably. */
/** Mode-18 payload size that fits one BLE image message comfortably. */
private static final int UPLOAD_PAYLOAD_MAX = 3600;
/** Max x-adjust control bytes between two glyphs before starting a new run. */
private static final int MAX_ADJUST_BYTES = 4;
/** Options: identity LUT (top 15) + transparent, for mode-13 image draws. */
/** Options: identity LUT (top 15) + transparent, for mode-19 image draws. */
private static final int IMAGE_DRAW_OPTIONS = 0x1f;
public static final class Result {
/** Complete image payload: a mode-8 batch of rect deltas + cached draws. */
public final byte[] payload;
/** Mode-12 upload payloads to enqueue BEFORE the image message. */
/** Mode-18 upload payloads to enqueue BEFORE the image message. */
public final List<byte[]> uploads;
/** Next mode-3 frame id (deltas consumed some). */
public final int nextFid;
@@ -235,7 +235,7 @@ public final class TexturePlanner {
: ensureResident(cache, selected);
bakedCandidates += selected.size() - drawable.size();
// Group glyphs into mode-14 runs; leftover-budget glyphs bake.
// Group glyphs into mode-20 runs; leftover-budget glyphs bake.
List<Selected> glyphDrawable = new ArrayList<>();
List<Selected> imageDrawable = new ArrayList<>();
for (Selected sel : drawable) {
@@ -505,7 +505,7 @@ public final class TexturePlanner {
/**
* Whether every in-panel nonzero pixel of the cached image equals the
* packed new frame — the mode-13 transparent draw writes exactly those.
* packed new frame — the mode-19 transparent draw writes exactly those.
*/
private static boolean imageMatchesComposite(byte[] packed, int stride, int width, int height,
ImageAtlas.Entry atlas, int left, int top) {
@@ -592,24 +592,26 @@ public final class TexturePlanner {
return Collections.emptyList(); // unreachable
}
/** Mode-13 cached-image draw: [13][offset u16][x u16][y u16][options u8]. */
/** Mode-19 cached-image draw: [19][offset u32][x u16][y u16][options u8]. */
private static byte[] encodeImageDraw(TextureCacheState cache, Selected sel) {
int offset = cache.ensureImage(sel.draw.imageId, sel.imageAtlas); // resident: returns the offset
byte[] sub = new byte[8];
sub[0] = 13;
byte[] sub = new byte[10];
sub[0] = 19;
sub[1] = (byte) (offset & 0xff);
sub[2] = (byte) ((offset >> 8) & 0xff);
sub[3] = (byte) (sel.draw.x & 0xff);
sub[4] = (byte) ((sel.draw.x >> 8) & 0xff);
sub[5] = (byte) (sel.draw.y & 0xff);
sub[6] = (byte) ((sel.draw.y >> 8) & 0xff);
sub[7] = (byte) IMAGE_DRAW_OPTIONS;
sub[3] = (byte) ((offset >> 16) & 0xff);
sub[4] = (byte) ((offset >> 24) & 0xff);
sub[5] = (byte) (sel.draw.x & 0xff);
sub[6] = (byte) ((sel.draw.x >> 8) & 0xff);
sub[7] = (byte) (sel.draw.y & 0xff);
sub[8] = (byte) ((sel.draw.y >> 8) & 0xff);
sub[9] = (byte) IMAGE_DRAW_OPTIONS;
return sub;
}
/**
* Group selected glyphs into mode-14 string sub-messages:
* [14][fontTable u16][x u16][y u16][options u8][strlen u8][string]
* Group selected glyphs into mode-20 string sub-messages:
* [20][fontTable u32][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
@@ -660,17 +662,19 @@ public final class TexturePlanner {
}
if (!runGlyphs.isEmpty()) {
byte[] stringBytes = string.toByteArray();
byte[] run = new byte[9 + stringBytes.length];
run[0] = 14;
byte[] run = new byte[11 + stringBytes.length];
run[0] = 20;
run[1] = (byte) (fontTable & 0xff);
run[2] = (byte) ((fontTable >> 8) & 0xff);
run[3] = (byte) (first.gx & 0xff);
run[4] = (byte) ((first.gx >> 8) & 0xff);
run[5] = (byte) (first.draw.y & 0xff);
run[6] = (byte) ((first.draw.y >> 8) & 0xff);
run[7] = (byte) (first.top | 0x10); // top color + transparent
run[8] = (byte) stringBytes.length;
System.arraycopy(stringBytes, 0, run, 9, stringBytes.length);
run[3] = (byte) ((fontTable >> 16) & 0xff);
run[4] = (byte) ((fontTable >> 24) & 0xff);
run[5] = (byte) (first.gx & 0xff);
run[6] = (byte) ((first.gx >> 8) & 0xff);
run[7] = (byte) (first.draw.y & 0xff);
run[8] = (byte) ((first.draw.y >> 8) & 0xff);
run[9] = (byte) (first.top | 0x10); // top color + transparent
run[10] = (byte) stringBytes.length;
System.arraycopy(stringBytes, 0, run, 11, stringBytes.length);
outRuns.add(run);
drawn.addAll(runGlyphs);
i = j;