forked from Blendman974/faceclaw
Resource management for the texture cache
This commit is contained in:
1 parent
146fe66bdd
commit
0c3d7517bc
28 files changed
+569
-524
No files matched your search
@@ -130,7 +130,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private int faceclawWakePendingNonce = -1;
|
||||
/**
|
||||
* The last firmware-info read said the glasses run Faceclaw's custom
|
||||
* firmware. Gates the private modes (cleanup, texture cache, ...) so stock
|
||||
* firmware. Gates the private modes (cleanup, resource cache, ...) so stock
|
||||
* or third-party firmware never sees them; the TS side checks the actual
|
||||
* revision and disconnects on a mismatch, so no per-feature gating is
|
||||
* needed here.
|
||||
@@ -245,7 +245,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private int desiredPaintMs;
|
||||
private int desiredFrameId;
|
||||
// Screen-space deferred draws (glyphs + images) whose pixels are baked
|
||||
// into desiredPacked; the texture-cache planner may replay them as
|
||||
// into desiredPacked; the resource-cache planner may replay them as
|
||||
// on-glasses cached draws.
|
||||
private SurfaceCompositor.ScreenDraw[] desiredDraws = new SurfaceCompositor.ScreenDraw[0];
|
||||
// (frame, reason) of the last "waiting to send" line, so a frame that
|
||||
@@ -260,11 +260,11 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
|
||||
private final SurfaceCompositor compositor = new SurfaceCompositor();
|
||||
|
||||
// Phone-side model of the CFW's 64 KiB texture cache (modes 12/13/14).
|
||||
// Phone-side model of the CFW's 256 KiB resource cache (modes 19/20/21/22).
|
||||
// Reset whenever the image pipeline / EvenHub session is torn down: the
|
||||
// firmware frees the cache with the fb lease, and after any resync the
|
||||
// cheap safe assumption is an empty cache (glyphs re-upload lazily).
|
||||
private final TextureCacheState textureCache = new TextureCacheState();
|
||||
private final ResourceCacheState resourceCache = new ResourceCacheState();
|
||||
|
||||
private final ArrayDeque<OutboundMessage> pendingMessages = new ArrayDeque<>();
|
||||
private final CfwTransport[] cfwTransports = { new CfwTransport(AndroidProtocolPlatform.INSTANCE), new CfwTransport(AndroidProtocolPlatform.INSTANCE) };
|
||||
@@ -1210,7 +1210,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
/**
|
||||
* As above, with the frame's glyph draws (see SurfaceCompositor's glyph
|
||||
* overload for the buffer format): the surface's full text content as
|
||||
* structured draws, letting the texture-cache planner ship glyphs as
|
||||
* structured draws, letting the resource-cache planner ship glyphs as
|
||||
* on-glasses cached draws instead of pixels. Null when the submitter has
|
||||
* no glyph metadata; the pixels alone remain fully correct.
|
||||
*/
|
||||
@@ -1415,10 +1415,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
*/
|
||||
public boolean suspendEvenHubSession() {
|
||||
// Ending the plugin task releases the fb lease, which frees the
|
||||
// on-glasses texture cache; forget it phone-side either way (a lost
|
||||
// on-glasses resource cache; forget it phone-side either way (a lost
|
||||
// ack may still have taken effect).
|
||||
synchronized (lock) {
|
||||
textureCache.reset();
|
||||
resourceCache.reset();
|
||||
}
|
||||
if (sendShutdownInternal(0, false)) {
|
||||
return true;
|
||||
@@ -2439,7 +2439,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
return ConnectionOptions.IDLE_SLEEP_MS;
|
||||
}
|
||||
|
||||
if (messageToPrewrite == null && sessionReady && windowHasRoom && !pendingMessages.isEmpty()) {
|
||||
if (messageToPrewrite == null && sessionReady && windowHasRoom && !pendingMessages.isEmpty()
|
||||
&& CfwMessageWindow.canSend(inFlightMessages, pendingMessages.peekFirst())) {
|
||||
messageToWrite = pendingMessages.removeFirst();
|
||||
Log.i(TAG, "sending pending message: " + messageToWrite.label);
|
||||
} else if (messageToPrewrite == null && !shutdownRequested && !benchmarkActive
|
||||
@@ -2954,8 +2955,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
return;
|
||||
}
|
||||
|
||||
// Texture-cache path: ship text as cached-glyph draws, punching their
|
||||
// ink out of the baked deltas. Uploads (mode 12) ride ahead of the
|
||||
// Resource-cache path: ship text as cached-glyph draws, punching their
|
||||
// ink out of the baked deltas. Resource commands (modes 21/22) ride ahead of the
|
||||
// image message on the ordered transport. Falls through to the plain
|
||||
// paths whenever the planner has nothing to draw.
|
||||
if (customFirmwareDetected && connectionOptions.TEXTURE_CACHE_FRAMES
|
||||
@@ -2965,13 +2966,13 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
? lastEnqueuedPacked : null;
|
||||
FrameTimings.getInstance().spanStart(frameId, "texture-plan");
|
||||
TexturePlanner.Result tex = TexturePlanner.plan(
|
||||
deltaBase, packed, width, height, draws, textureCache,
|
||||
deltaBase, packed, width, height, draws, resourceCache,
|
||||
nextImageFrameId,
|
||||
connectionOptions.MULTI_RECT_FRAMES, ConnectionOptions.MULTI_RECT_MAX_RECTS);
|
||||
if (tex != null) {
|
||||
nextImageFrameId = tex.nextFid;
|
||||
for (byte[] upload : tex.uploads) {
|
||||
enqueueTextureUploadLocked(upload);
|
||||
for (byte[] upload : tex.resourceCommands) {
|
||||
enqueueResourceCommandLocked(upload);
|
||||
}
|
||||
BleImageOptimizer.TileImagePlan plan = new BleImageOptimizer.TileImagePlan(
|
||||
0, DASHBOARD_TILE, packed, width, height, nextMapSessionId(), tex.payload);
|
||||
@@ -2982,7 +2983,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
+ " fw=" + tex.fwGlyphs + "/" + tex.fwRuns
|
||||
+ " baked=" + tex.bakedCandidates
|
||||
+ (tex.uploadBytes > 0 ? " upload=" + tex.uploadBytes + "B" : "")
|
||||
+ " cache=" + textureCache.usedBytes() + "B"
|
||||
+ " cache=" + resourceCache.usedBytes() + "B"
|
||||
+ " payload=" + tex.payload.length + "B"
|
||||
+ " (rects " + tex.rectsMs + "ms, match " + tex.matchMs
|
||||
+ "ms, cache " + tex.cacheMs + "ms, punch " + tex.punchMs
|
||||
@@ -3081,23 +3082,23 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue one mode-12 texture-cache upload ahead of the image message that
|
||||
* Enqueue one resource eviction/upload command ahead of the image message that
|
||||
* references its glyphs (the transport is FIFO, so no ack round trip is
|
||||
* needed before use). A timeout means the on-glasses cache state is
|
||||
* unknown; forget everything phone-side (glyphs re-upload lazily) and let
|
||||
* the accompanying image update's own timeout drive the frame resync.
|
||||
*/
|
||||
private void enqueueTextureUploadLocked(byte[] payload) {
|
||||
private void enqueueResourceCommandLocked(byte[] payload) {
|
||||
OutboundMessage message = messageBuilder.imagePayload(
|
||||
"texcache",
|
||||
"resources",
|
||||
DASHBOARD_TILE,
|
||||
nextMapSessionId(),
|
||||
payload,
|
||||
"texture upload " + payload.length + "B",
|
||||
"resource command " + payload.length + "B",
|
||||
connectionOptions.sendImagesToLeft);
|
||||
message.onTimeout = () -> {
|
||||
textureCache.reset();
|
||||
logLine("texture upload ack timeout; texture cache state reset");
|
||||
resourceCache.reset();
|
||||
logLine("resource command ack timeout; resource cache state reset");
|
||||
};
|
||||
pendingMessages.addLast(message);
|
||||
logLine("queue " + message.label);
|
||||
@@ -3591,10 +3592,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
// image is a full keyframe rather than a delta onto a stale base.
|
||||
lastEnqueuedPacked = new byte[0];
|
||||
lastEnqueuedFingerprint = "";
|
||||
// Queued texture uploads (if any) were dropped with the rest, and the
|
||||
// Queued resource commands (if any) were dropped with the rest, and the
|
||||
// session churn behind a full clear may have freed the on-glasses
|
||||
// cache; forget it phone-side so glyphs re-upload lazily.
|
||||
textureCache.reset();
|
||||
resourceCache.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3625,7 +3626,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
clearInFlightMessagesLocked(reason);
|
||||
lastEnqueuedPacked = new byte[0];
|
||||
lastEnqueuedFingerprint = "";
|
||||
textureCache.reset();
|
||||
resourceCache.reset();
|
||||
}
|
||||
|
||||
/** Any image update whose fragments are still queued (not yet sent). */
|
||||
|
||||
@@ -26,7 +26,7 @@ export type FirmwareInfo = {
|
||||
};
|
||||
|
||||
/** The Faceclaw firmware revision this build of the app needs. */
|
||||
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 22;
|
||||
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 23;
|
||||
|
||||
const FACECLAW_PREFIX = "Faceclaw/";
|
||||
const LEGACY_PREFIX = "EVENCFW";
|
||||
|
||||
@@ -19,12 +19,12 @@ export type FirmwarePatchSet = {
|
||||
export const CFW_PATCH_SET: FirmwarePatchSet = {
|
||||
"base": "g2_2.3.0.24.bin",
|
||||
"baseSha256": "187ccf2bcc5c17a212106e8a376745511e8289c4232b634a7ea94b9bf25a0979",
|
||||
"outputSha256": "059ff37a5455b9b272b715ab62c2910ee4dc4966fbfb2e117159bda746bd7315",
|
||||
"outputSha256": "393ea24203d9a388975c732cae4698339f8af0e13139b73d71386181a1bc004e",
|
||||
"patches": [
|
||||
{
|
||||
"offset": 1045797,
|
||||
"old": "38b584b0",
|
||||
"new": "5af317bf",
|
||||
"new": "59f3efbf",
|
||||
"desc": "ring receiver entry: timestamped unfiltered reports under framebuffer lease"
|
||||
},
|
||||
{
|
||||
@@ -36,49 +36,49 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
|
||||
{
|
||||
"offset": 1437923,
|
||||
"old": "fef788ff",
|
||||
"new": "fbf226fc",
|
||||
"new": "faf2fefc",
|
||||
"desc": "cfw_ancs_event"
|
||||
},
|
||||
{
|
||||
"offset": 1437951,
|
||||
"old": "fef7e8fb",
|
||||
"new": "fbf260f9",
|
||||
"new": "faf238fa",
|
||||
"desc": "cfw_ancs_open"
|
||||
},
|
||||
{
|
||||
"offset": 1437969,
|
||||
"old": "fef7e7fb",
|
||||
"new": "fbf277f9",
|
||||
"new": "faf24ffa",
|
||||
"desc": "cfw_ancs_close"
|
||||
},
|
||||
{
|
||||
"offset": 1388387,
|
||||
"old": "39694d00",
|
||||
"new": "35407d00",
|
||||
"new": "e5317d00",
|
||||
"desc": "connection-bound ANCS EUS write callback"
|
||||
},
|
||||
{
|
||||
"offset": 971497,
|
||||
"old": "fff717f8",
|
||||
"new": "6df347f8",
|
||||
"new": "6cf31ff9",
|
||||
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 971845,
|
||||
"old": "fef769ff",
|
||||
"new": "6cf399ff",
|
||||
"new": "6cf371f8",
|
||||
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 979579,
|
||||
"old": "fdf74ef8",
|
||||
"new": "6bf37ef8",
|
||||
"new": "6af356f9",
|
||||
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
|
||||
},
|
||||
{
|
||||
"offset": 1428895,
|
||||
"old": "f8f76cfd",
|
||||
"new": "faf224f9",
|
||||
"new": "f9f28afd",
|
||||
"desc": "bl cfw_receive_packet (private SID-f0 probe before TPL reassembly)"
|
||||
},
|
||||
{
|
||||
@@ -108,145 +108,145 @@ export const CFW_PATCH_SET: FirmwarePatchSet = {
|
||||
{
|
||||
"offset": 1248487,
|
||||
"old": "d4f712fb",
|
||||
"new": "28f378f9",
|
||||
"new": "27f354fb",
|
||||
"desc": "bl settings_send_wrapper (append caps field 100)"
|
||||
},
|
||||
{
|
||||
"offset": 1246183,
|
||||
"old": "f5f7e2f8",
|
||||
"new": "28f310fb",
|
||||
"new": "27f374fd",
|
||||
"desc": "bl settings_decode_wrapper (Faceclaw lease field 101)"
|
||||
},
|
||||
{
|
||||
"offset": 975219,
|
||||
"old": "02f083fc",
|
||||
"new": "6af372fb",
|
||||
"new": "69f3d6fd",
|
||||
"desc": "bl faceclaw_display_start @ 0x467d68 (fail-open wake takeover)"
|
||||
},
|
||||
{
|
||||
"offset": 975411,
|
||||
"old": "02f023fc",
|
||||
"new": "6af3e0fb",
|
||||
"new": "69f344fe",
|
||||
"desc": "bl faceclaw_display_start_headup @ 0x467e28 (fail-open wake takeover)"
|
||||
},
|
||||
{
|
||||
"offset": 975103,
|
||||
"old": "07f085fb",
|
||||
"new": "6cf350f8",
|
||||
"new": "6bf328f9",
|
||||
"desc": "bl headup_gate (head-up sensor event -> EvenHub sys event 12 under lease)"
|
||||
},
|
||||
{
|
||||
"offset": 830549,
|
||||
"old": "19f03efb",
|
||||
"new": "8ff3fff9",
|
||||
"new": "8ef3d7fa",
|
||||
"desc": "bl gesture_ring_press_mode (observe ring touch-down, preserve stock dispatch)"
|
||||
},
|
||||
{
|
||||
"offset": 830795,
|
||||
"old": "1ff071ff",
|
||||
"new": "8ff3b8f9",
|
||||
"new": "8ef390fa",
|
||||
"desc": "bl gesture_press (owned event 9 before stock menu gate)"
|
||||
},
|
||||
{
|
||||
"offset": 830887,
|
||||
"old": "1ff043ff",
|
||||
"new": "8ff3b2f9",
|
||||
"new": "8ef38afa",
|
||||
"desc": "bl gesture_short_long (Faceclaw lease gates event 11 vs stock Menu path)"
|
||||
},
|
||||
{
|
||||
"offset": 831763,
|
||||
"old": "1ff08dfd",
|
||||
"new": "8ef3e8ff",
|
||||
"new": "8ef3c0f8",
|
||||
"desc": "bl gesture_release (owned event 10 before stock menu gate)"
|
||||
},
|
||||
{
|
||||
"offset": 1570209,
|
||||
"old": "7fb50600",
|
||||
"new": "d9f209bd",
|
||||
"new": "d8f2e5be",
|
||||
"desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline"
|
||||
},
|
||||
{
|
||||
"offset": 1051545,
|
||||
"old": "f6f787f9",
|
||||
"new": "53f3d7fe",
|
||||
"new": "53f3cbfc",
|
||||
"desc": "bl display_copy_hook @ 0x47a78e (640x480 direct framebuffer)"
|
||||
},
|
||||
{
|
||||
"offset": 1051861,
|
||||
"old": "f6f7e9f8",
|
||||
"new": "53f339fe",
|
||||
"new": "53f32dfc",
|
||||
"desc": "bl display_copy_hook @ 0x47a8ca (640x480 direct framebuffer)"
|
||||
},
|
||||
{
|
||||
"offset": 1262237,
|
||||
"old": "d9f7d0fd",
|
||||
"new": "24f3cff9",
|
||||
"new": "23f3b3fc",
|
||||
"desc": "bl faceclaw_send_wear_event @ 0x4ade92 (outside onboarding)"
|
||||
},
|
||||
{
|
||||
"offset": 1262337,
|
||||
"old": "d9f79efd",
|
||||
"new": "24f39df9",
|
||||
"new": "23f381fc",
|
||||
"desc": "bl faceclaw_send_wear_event @ 0x4adef6 (outside onboarding)"
|
||||
},
|
||||
{
|
||||
"offset": 1309405,
|
||||
"old": "67f02dff",
|
||||
"new": "18f3b7fa",
|
||||
"new": "17f39bfd",
|
||||
"desc": "bl compass_decode_capture (sample-matched GAF diagnostics)"
|
||||
},
|
||||
{
|
||||
"offset": 1307865,
|
||||
"old": "fff7c9fb",
|
||||
"new": "18f335fe",
|
||||
"new": "18f319f9",
|
||||
"desc": "bl compass_report_event (stock UI + heading with diagnostics over BLE)"
|
||||
},
|
||||
{
|
||||
"offset": 1300937,
|
||||
"old": "00210170",
|
||||
"new": "1af31bfb",
|
||||
"new": "19f3fffd",
|
||||
"desc": "bl compass_preserve_accuracy (retain magnetic calibration under Faceclaw framebuffer lease)"
|
||||
},
|
||||
{
|
||||
"offset": 4537963,
|
||||
"old": "",
|
||||
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf2de9f04fa5b04ef66c6b4ff29c5416460d46ccf2ff0bc2f229044ff6ff7900274ff00108019006e048f2a351c0f24501bff32f8f8847d4f800a0bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0c3804ff4407005f073f9002800f0b7824ff60051cff6ff714218403182f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382f8197382f81a7382f81b7382f81c7382f81d7382f81e7382f81f7382f8207382f8217382f8227382f8237382f8247382f8257382f8267382f8277382f8287382f8297382f82a7382f82b7382f82c7382f82d7382f82e7382f82f7382f8307382f8317382f8327382f8337382f8347382f8357382f8367382f8377382f8387382f8397382f83a7382f83b7382f83c7382f83d7382f83e7382f83f737ff47cafc0f800b080f80a80a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001f51457ff42fafbff35f8f00bf44e8000121b154e8001f5145f8d023e7aa4645ea064100260127c0f8701206e048f2a351c0f24501bff32f8f88472568bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0c2804ff4407005f090f8002800f0d4814ff60051cff6ff7100bf4218403182f8006382f8016382f8026382f8036382f8046382f8056382f8066382f8076382f8086382f8096382f80a6382f80b6382f80c6382f80d6382f80e6382f80f6382f8106382f8116382f8126382f8136382f8146382f8156382f8166382f8176382f8186382f8196382f81a6382f81b6382f81c6382f81d6382f81e6382f81f6382f8206382f8216382f8226382f8236382f8246382f8256382f8266382f8276382f8286382f8296382f82a6382f82b6382f82c6382f82d6382f82e6382f82f6382f8306382f8316382f8326382f8336382f8346382f8356382f8366382f8376382f8386382f8396382f83a6382f83b6382f83c6382f83d6382f83e6382f83f637ff47cafc0f800b08772a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001fa9427ff430afbff35f8f44e8000121b154e8001fa942f8d025e7d0f8746242f6f767c0f24407002ebff35f8f00f0f38007f19a0230464ff0ff314ff0ff379047002840f0498147f6df40c0f24f00804700274ff001080397adf8107006e048f2a351c0f24501bff32f8f88472568bff35f8f20680321cff680710140b1f1005fbff35f8f03d10168594500f0e9804ff4407004f094ff002800f0e1804ff60051cff6ff7100bf4218403182f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382f8197382f81a7382f81b7382f81c7382f81d7382f81e7382f81f7382f8207382f8217382f8227382f8237382f8247382f8257382f8267382f8277382f8287382f8297382f82a7382f82b7382f82c7382f82d7382f82e7382f82f7382f8307382f8317382f8327382f8337382f8347382f8357382f8367382f8377382f8387382f8397382f83a7382f83b7382f83c7382f83d7382f83e7382f83f737ff47cafc0f800b080f80a80a0f81c90a0f81e90a0f82090a0f82290a0f82490a0f82690a0f82890a0f82a90a0f82c90a0f82e90a0f83090a0f83290a0f83490a0f83690a0f83890a0f83a9054e8001fa9427ff42fafbff35f8f00bf44e8000149b354e8001fa942f8d023e705460020b847b0b105f51d7151e8006f4eb9bff35f8f00bf41e8000292b151e8006f002ef8d00021bff32f8fbff35f8f21b10646f1e64ff0ff3742e007f5a9718847eae60121f1e70020019d42f6f764002d4ff0010bc0f2440418bfbaf1000f33d10df108098046484602f061fa03ab28465146012206f093fa9df8111007464846002918bf4ff0ff3702f08dfbbbf1000f044608d19df8100028b94af21f30c0f247004c308047b8f1000f18bfc8f80c4042f6f764c0f2440404f5807130468847384625b0bde8f08f2a7802f07f01a1f1150313f1030f0bd802f07b030b2b07d002f07e02082a03d0032901d00629b7d140b14af21f31c0f247010746884797f8641111b14ff0ff37d6e738464ff0000ba6e72de9f04f81b04ef66c6b4ff29c554ff6005848f2a35accf2ff0bc2f229054ff6ff76cff6ff7800274ff00109c0f2450a03e000bfbff32f8fd0472c68bff35f8f2868bff35f8f01466ff39601b1f1005f03d10168594500f0b2804ff4407004f019fe002800f0aa80414600bf4218403182f8007382f8017382f8027382f8037382f8047382f8057382f8067382f8077382f8087382f8097382f80a7382f80b7382f80c7382f80d7382f80e7382f80f7382f8107382f8117382f8127382f8137382f8147382f8157382f8167382f8177382f8187382f8197382f81a7382f81b7382f81c7382f81d7382f81e7382f81f7382f8207382f8217382f8227382f8237382f8247382f8257382f8267382f8277382f8287382f8297382f82a7382f82b7382f82c7382f82d7382f82e7382f82f7382f8307382f8317382f8327382f8337382f8347382f8357382f8367382f8377382f8387382f8397382f83a7382f83b738Line truncated
|
||||
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf2de9f04fa5b04ef66c6a4ff29c5516460c46ccf2ff0ac2f229054ff6ff7800274ff00109019006e048f2a351c0f24501bff32f8f8847d5f800b0bff35f8f28680321cff680710140b1f1005fbff35f8f02d10168514543d04ff4417004f050fb002800f036810021421847540431b1f5417f57709770d770f6d1c0f800a080f80a90a0f81c80a0f81e80a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a8055e8001f5945aed1bff35f8f00bf45e8000121b155e8001f5945f8d0a3e7a34644ea064100260127c0f8701206e048f2a351c0f24501bff32f8f88472c68bff35f8f28680321cff680710140b1f1005fbff35f8f02d10168514542d04ff4417004f0edfa002800f0d380002100bf421846540431b1f5417f56709670d670f6d1c0f800a08772a0f81c80a0f81e80a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a8055e8001fa142afd1bff35f8f45e8000121b155e8001fa142f8d0a5e7d0f8746242f6f767c0f24407002ebff35f8f71d007f19a0230464ff0ff314ff0ff379047002840f0c28047f6df40c0f24f00804700274ff001090397adf8107007e000bf48f2a351c0f24501bff32f8f88472c68bff35f8f28680321cff680710140b1f1005fbff35f8f02d10168514567d04ff4417004f071fa002860d00021421847540431b1f5417f57709770d770f6d1c0f800a080f80a90a0f81c80a0f81e80a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a8055e8001fa142b0d1bff35f8f00bf45e8000149b355e8001fa142f8d0a5e704460020b847b0b104f51d7151e8006f4eb9bff35f8f00bf41e8000292b151e8006f002ef8d00021bff32f8fbff35f8f21b1064672e74ff0ff373ce007f5a97188476be70121f1e70020019f42f6f769002f4ff00104c0f2440918bfbbf1000f2dd102ad8046284602f054f903ab38465946012205f0eefc9df8111007462846002918bf4ff0ff3702f0f6f9002c044608d19df8100028b94af21f30c0f247004c308047b8f1000f18bfc8f80c4009f5807130468847384625b0bde8f08f3a7802f07f01a1f1150313f1030f0bd802f07b030b2b07d002f07e02082a03d0032901d00629bdd140b14af21f31c0f247010446884794f8641111b14ff0ff37d6e720460024ade700bf2de9f0474ef66c6a4ff29c5548f2a359ccf2ff0ac2f229054ff6ff7600274ff00108c0f2450902e0bff32f8fc8472c68bff35f8f2868bff35f8f01466ff39601b1f1005f02d10168514535d04ff4417004f084f978b30021421847540431b1f5417f57709770d770f6d1c0f800a080f80a808683c683068446848684c684068546858685c685068646868686c6860687468755e8001fa142c6d1bff35f8f00bf45e80001002908bfbde8f08755e8001fa142f5d0b8e70020bde8f08770b500284dd001684ef66c62ccf2ff02914218bf70bd90f8441090f845504bf6394c8d42c0f2510c2ad205eb8501014491f8492091f84a4091f846e091f8473091f8481042ea0424012c4ff0010298bf14466e1c054680f84560b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f84410bde870406047e047286c38b142f64d42c0f244022146bde87040104770bd02464ff64b70cff6ff7042f6653c7844c0f2440c00210023604700bf2de9f04f9db04ff29c50c2f22900d0f800b0bff35f8f58466ff39600b0f1005f3fd1dbf800004ef66c65ccf2ff05a84237d19bf8640110b3dbf86041fcb140f24030c2f20000d0f8787502a802f020f8002f00f0f18040f20308384621464ff41632cff680784ff6ff760197fff764fc48f2a35900240127c0f245091ce09bf8660170b1dbf8680140b147f64c61c2f207010968401a002801f3cc8700208bf8660140f6a120c0f247001db0bde8f04f0047bff32f8fc8474ff29c50c2f22900d0f800a0bff35f8f0068bff35f8f00ea0801b1f1005f02d10168a94236d04ff4417004f07df8002801f08c87002100bf421844540431b1f5417f54709470d470f6d14ff29c52056087728683c683068446848684c684068546858685c685068646868686c68606874687c2f2290252e8001f5145bdd1bff35f8f00bf42e8000121b152e8001f5145f8d0b2e7817a002941f05887027e0021002a8df814107cd04ff29c5605aac2f22906535c002b00f0b68053185f78002f74d09f78002f73d0df78002f72d01f79002f71d05f79002f70d09f79002f6fd0df79002f6ed01f7a002f6dd05f7a002f6cd09f7a002f6bd0df7a002f6ad01f7b002f69d05f7b002f68d09f7b002f67d0df7b002f66d01f7c002f65d05f7c002f64d09f7c002f63d0df7c002f62d01f7d002f61d05f7d002f60d09f7d002f5fd0df7d002f5ed01f7e002f5dd05f7e002f5cd09f7e002f5bd0df7e002f5ad01f7f002f59d05f7f002f58d09f7f002f57d0db7f002b56d0203160297ff49aaf76e000208bf86401cbf860018bf8660101208bf8650140f6a120c0f24700804701f0e8be4ff29c56c2f2290664e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2921d84ff0520353541dd00a4445235e29537018d04Line truncated
|
||||
"desc": "append injected blobs to main-app payload"
|
||||
},
|
||||
{
|
||||
"offset": 779123,
|
||||
"old": "805a3900",
|
||||
"new": "f0173a00",
|
||||
"new": "f00a3a00",
|
||||
"desc": "main-app subheader payload size (ps)"
|
||||
},
|
||||
{
|
||||
"offset": 152,
|
||||
"old": "005b3900",
|
||||
"new": "70183a00",
|
||||
"new": "700b3a00",
|
||||
"desc": "main-app TOC entry size (ps + 128)"
|
||||
},
|
||||
{
|
||||
"offset": 779243,
|
||||
"old": "805a3904",
|
||||
"new": "f0173a04",
|
||||
"new": "f00a3a04",
|
||||
"desc": "main-app preamble length (low 24 bits)"
|
||||
},
|
||||
{
|
||||
"offset": 779247,
|
||||
"old": "effdbcea",
|
||||
"new": "ed6be905",
|
||||
"new": "09a9afac",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin preamble crc32"
|
||||
},
|
||||
{
|
||||
"offset": 156,
|
||||
"old": "c0c6322a",
|
||||
"new": "18540264",
|
||||
"new": "19292813",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)"
|
||||
},
|
||||
{
|
||||
"offset": 779127,
|
||||
"old": "c0c6322a",
|
||||
"new": "18540264",
|
||||
"new": "19292813",
|
||||
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)"
|
||||
}
|
||||
]
|
||||
|
||||
@@ -496,6 +496,7 @@ export class GlassesSession {
|
||||
this.timer = setTimeout(() => { this.timer = null; void this.pump() }, delay)
|
||||
}
|
||||
private canSendDisplay(): boolean {
|
||||
if (this.displaySending?.commands[this.displaySending.offset][0] === 22 && this.cfwPending.length) return false
|
||||
// Also bound completed frames retained behind a missing/out-of-order ACK.
|
||||
return this.displayInFlight < DISPLAY_WINDOW_SIZE && !!(this.displaySending ||
|
||||
(this.latest && !this.charging && this.displayFrames.length < DISPLAY_WINDOW_SIZE))
|
||||
@@ -511,11 +512,11 @@ export class GlassesSession {
|
||||
const texturePlan = textureFrame ? this.textures?.plan(this.lastEnqueued, packed, textureFrame, this.nextImageFrameId) : null
|
||||
let commands: Uint8Array[]
|
||||
if (texturePlan) {
|
||||
// Uploads participate in the same ordered, acknowledged command window as
|
||||
// Resource commands participate in the same ordered, acknowledged command window as
|
||||
// the image. Coalescing only happens before planning mutates residency.
|
||||
commands = [...texturePlan.uploads, texturePlan.payload]
|
||||
commands = [...texturePlan.resourceCommands, texturePlan.payload]
|
||||
this.nextImageFrameId = texturePlan.nextFid
|
||||
this.log(`Display textures (${texturePlan.uploads.length} uploads, cache=${texturePlan.usedBytes}B)`)
|
||||
this.log(`Display textures (${texturePlan.resourceCommands.length} resource commands, cache=${texturePlan.usedBytes}B)`)
|
||||
} else {
|
||||
const delta = buildBoundingBoxPayload(this.lastEnqueued, packed, 640, 480, this.nextImageFrameId)
|
||||
const payload = delta ?? protocol.concat(new Uint8Array([6]), protocol.rle4(packed))
|
||||
@@ -528,7 +529,10 @@ export class GlassesSession {
|
||||
this.displaySending = { packed, commands, offset: 0, pending: 0 }
|
||||
this.displayFrames.push(this.displaySending); this.lastEnqueued = packed
|
||||
}
|
||||
const frame = this.displaySending, payload = frame.commands[frame.offset++]
|
||||
const frame = this.displaySending
|
||||
// Drain earlier commands before evicting: recovery may replay any unresolved draw/upload.
|
||||
if (frame.commands[frame.offset][0] === 22 && this.cfwPending.length) return
|
||||
const payload = frame.commands[frame.offset++]
|
||||
frame.pending++; this.displayInFlight++
|
||||
if (frame.offset >= frame.commands.length) this.displaySending = null
|
||||
// Writes serialize on L while up to three complete commands await their
|
||||
@@ -554,7 +558,7 @@ export class GlassesSession {
|
||||
this.pumping = true; const generation = this.generation
|
||||
try {
|
||||
const now = Date.now()
|
||||
// Firmware frees textures when its 90-second framebuffer lease expires.
|
||||
// Firmware frees resources when its 90-second framebuffer lease expires.
|
||||
// After a long iOS suspension, reconnect rather than replaying pending
|
||||
// draws against unknown memory. Leave a margin for the two arm writes.
|
||||
if (now - this.lastLease >= 80_000) throw new Error('Display lease lapsed; restarting session')
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/** Platform-owned snapshot of the same composite as the submitted pixels. */
|
||||
export type TextureFrame = { readonly native: object }
|
||||
export type TexturePlan = { uploads: Uint8Array[]; payload: Uint8Array; nextFid: number; usedBytes: number }
|
||||
export type TexturePlan = { resourceCommands: Uint8Array[]; payload: Uint8Array; nextFid: number; usedBytes: number }
|
||||
/** Sessions own residency; frame snapshots may be coalesced without changing it. */
|
||||
export interface SessionTexturePlanner {
|
||||
plan(previous: Uint8Array | null, next: Uint8Array, frame: TextureFrame, firstId: number): TexturePlan | null
|
||||
|
||||
@@ -40,10 +40,10 @@ export function runKotlinProtocolSmokeTest(): void {
|
||||
textured.submitSurfaceFrame('icon', white, { x: 0, y: 0, width: 2, height: 2 }, draw.buffer)
|
||||
const snapshot = textured.compositeFrame(), packed = protocol.packGray4(snapshot.pixels, 4, 2)
|
||||
const cold = textures.plan(null, packed, snapshot.textures, 1)
|
||||
assert(!!cold && cold.uploads[0]?.[0] === 18 && cold.payload[0] === 8, 'texture upload and draw')
|
||||
assert(textures.plan(null, packed, snapshot.textures, 1)?.uploads.length === 0, 'texture reuse')
|
||||
assert(!!cold && cold.resourceCommands.some(command => command[0] === 21) && cold.payload[0] === 8, 'texture upload and draw')
|
||||
assert(textures.plan(null, packed, snapshot.textures, 1)?.resourceCommands.length === 0, 'texture reuse')
|
||||
textures.reset()
|
||||
assert((textures.plan(null, packed, snapshot.textures, 1)?.uploads.length ?? 0) > 0, 'texture reset')
|
||||
assert((textures.plan(null, packed, snapshot.textures, 1)?.resourceCommands.length ?? 0) > 0, 'texture reset')
|
||||
const changed = new Uint8Array(16); changed[0] = 255
|
||||
assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === 3, 'image update')
|
||||
assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge')
|
||||
|
||||
@@ -7,7 +7,7 @@ export class IosTexturePlanner implements SessionTexturePlanner {
|
||||
reset(): void { this.native.reset() }
|
||||
plan(previous: Uint8Array | null, next: Uint8Array, frame: TextureFrame, firstId: number): TexturePlan | null {
|
||||
const result = this.native.planPreviousNextFrameFirstId(previous ? toData(previous) : null, toData(next), frame.native, firstId)
|
||||
return result ? { payload: fromData(result.payload), uploads: nativeArray(result.uploads, fromData),
|
||||
return result ? { payload: fromData(result.payload), resourceCommands: nativeArray(result.resourceCommands, fromData),
|
||||
nextFid: result.nextFid, usedBytes: result.usedBytes } : null
|
||||
}
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
+1
-1
@@ -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(
|
||||
|
||||
+7
@@ -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) {
|
||||
|
||||
+2
-2
@@ -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
|
||||
}
|
||||
+150
@@ -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) }
|
||||
}
|
||||
-191
@@ -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
|
||||
}
|
||||
}
|
||||
+49
-79
@@ -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?
|
||||
|
||||
+39
@@ -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. */
|
||||
|
||||
+1
-1
@@ -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())
|
||||
}
|
||||
}
|
||||
+30
-6
@@ -306,14 +306,14 @@ const ack = (transport, sent, overrides) => transport.ack(sent.id, sent.message,
|
||||
const tick = () => new Promise(resolve => setImmediate(resolve));
|
||||
const noisy = () => Uint8Array.from({ length: 640 * 480 }, (_, i) => (i % 2) * 255);
|
||||
|
||||
test('texture uploads precede their frame, share ACK accounting, and coalesce pixels with draw identities', async t => {
|
||||
test('resource evictions and uploads precede their frame, share ACK accounting, and coalesce pixels with draw identities', async t => {
|
||||
const plans = [];
|
||||
let resets = 0;
|
||||
const textures = {
|
||||
reset() { resets++; },
|
||||
plan(previous, next, frame, firstId) {
|
||||
plans.push({ previous, next, frame, firstId });
|
||||
return { uploads: plans.length === 1 ? [new Uint8Array([18, 1]), new Uint8Array([18, 2])] : [],
|
||||
return { resourceCommands: plans.length === 1 ? [new Uint8Array([22, 1]), new Uint8Array([21, 2])] : [],
|
||||
payload: new Uint8Array([8, plans.length]), nextFid: firstId + 2, usedBytes: 100 };
|
||||
},
|
||||
};
|
||||
@@ -323,7 +323,7 @@ test('texture uploads precede their frame, share ACK accounting, and coalesce pi
|
||||
h.session.setFrame(new Uint8Array(640 * 480), a);
|
||||
await until(() => imageMessages(h.transport).length === 3);
|
||||
const sent = imageMessages(h.transport);
|
||||
assert.deepEqual(sent.map(s => s.message.payload[0]), [18, 18, 8]);
|
||||
assert.deepEqual(sent.map(s => s.message.payload[0]), [22, 21, 8]);
|
||||
h.session.setFrame(new Uint8Array(640 * 480).fill(80), b);
|
||||
h.session.setFrame(new Uint8Array(640 * 480).fill(160), c);
|
||||
assert.equal(plans.length, 1, 'coalesced frames must not allocate textures');
|
||||
@@ -340,15 +340,39 @@ test('texture uploads precede their frame, share ACK accounting, and coalesce pi
|
||||
assert.ok(resets > 0);
|
||||
});
|
||||
|
||||
test('a failed texture upload invalidates residency and the pipelined frame base', async t => {
|
||||
test('evictions drain earlier replayable draws before resource IDs can be reused', async t => {
|
||||
let plans = 0;
|
||||
const textures = {
|
||||
reset() {},
|
||||
plan() { return { resourceCommands: ++plans === 1 ? [] : [new Uint8Array([22]), new Uint8Array([21])],
|
||||
payload: new Uint8Array([8]), nextFid: 1, usedBytes: 100 }; },
|
||||
};
|
||||
const h = harness(t, new FakeTransport(), textures); await h.session.start(addresses);
|
||||
h.transport.hold = true;
|
||||
h.session.setFrame(new Uint8Array(640 * 480), { native: {} });
|
||||
await until(() => imageMessages(h.transport).length === 1);
|
||||
h.session.setFrame(new Uint8Array(640 * 480).fill(80), { native: {} });
|
||||
await until(() => plans === 2); await tick();
|
||||
assert.equal(imageMessages(h.transport).length, 1, 'eviction cannot enter an unresolved window');
|
||||
h.session.recoverCfw(); await tick();
|
||||
assert.equal(imageMessages(h.transport).length, 1);
|
||||
ack(h.transport, imageMessages(h.transport)[0]);
|
||||
await until(() => imageMessages(h.transport).length === 4);
|
||||
assert.deepEqual(imageMessages(h.transport).map(s => s.message.payload[0]), [8, 22, 21, 8]);
|
||||
h.transport.hold = false;
|
||||
for (const sent of imageMessages(h.transport).slice(1)) ack(h.transport, sent);
|
||||
await until(() => h.session.state.frames === 2);
|
||||
});
|
||||
|
||||
test('a failed resource upload invalidates residency and the pipelined frame base', async t => {
|
||||
let resets = 0;
|
||||
const textures = {
|
||||
reset() { resets++; },
|
||||
plan() { return { uploads: [new Uint8Array([18])], payload: new Uint8Array([8]), nextFid: 1, usedBytes: 4 }; },
|
||||
plan() { return { resourceCommands: [new Uint8Array([21])], payload: new Uint8Array([8]), nextFid: 1, usedBytes: 4 }; },
|
||||
};
|
||||
const h = harness(t, new FakeTransport(), textures); await h.session.start(addresses);
|
||||
const original = h.session.requestCfw.bind(h.session);
|
||||
h.session.requestCfw = (role, payload) => payload[0] === 18 ? Promise.reject(new Error('texture upload failed')) : original(role, payload);
|
||||
h.session.requestCfw = (role, payload) => payload[0] === 21 ? Promise.reject(new Error('texture upload failed')) : original(role, payload);
|
||||
h.session.setFrame(new Uint8Array(640 * 480), { native: {} });
|
||||
await until(() => h.session.state.phase === 'retrying');
|
||||
assert.equal(resets, 1); assert.equal(h.session.lastEnqueued, null);
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/** Android's Java communicator and iOS share the same resource API and wire commands. */
|
||||
public class ResourceCacheProtocolTest {
|
||||
public static void main(String[] args) {
|
||||
ResourceCacheState cache = new ResourceCacheState();
|
||||
CachedResource image = new CachedResource(new byte[]{1, 1, 31});
|
||||
assert cache.prepare(Collections.singletonList(image))[0] == 0;
|
||||
List<byte[]> commands = cache.drainCommands(3600);
|
||||
assert commands.size() == 2 && commands.get(0)[0] == 22;
|
||||
assert Arrays.equals(commands.get(1), new byte[]{21,1,0,0,0,3,0,0,0,0,0,3,0,1,1,31});
|
||||
assert cache.prepare(Collections.singletonList(image))[0] == 0;
|
||||
assert cache.drainCommands(3600).isEmpty();
|
||||
cache.reset();
|
||||
assert cache.prepare(Collections.singletonList(image))[0] == 0;
|
||||
assert cache.drainCommands(3600).get(0)[0] == 22;
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
|
||||
public class TextureCacheProtocolTest {
|
||||
static int u16(byte[] b, int p) { return (b[p]&255) | (b[p+1]&255)<<8; }
|
||||
static int u32(byte[] b, int p) { return u16(b,p) | u16(b,p+2)<<16; }
|
||||
public static void main(String[] args) throws Exception {
|
||||
TextureCacheState cache = new TextureCacheState();
|
||||
byte[] pixels = new byte[255*255];
|
||||
for (int i=0;i<pixels.length;i++) pixels[i]=(byte)(i%15+1);
|
||||
ImageAtlas.Entry big = new ImageAtlas.Entry(255,255,pixels);
|
||||
for (int i=0;i<3;i++) assert cache.ensureImage(i,big)>=0;
|
||||
ImageAtlas.Entry tiny = new ImageAtlas.Entry(1,1,new byte[]{15});
|
||||
int imageOffset = cache.ensureImage(100,tiny);
|
||||
assert imageOffset>65535;
|
||||
GlyphAtlas.Glyph glyph = new GlyphAtlas.Glyph(1,1,0,1,0,null,new byte[]{15});
|
||||
int glyphOffset = cache.ensureGlyph(42,'A',glyph);
|
||||
int table = cache.fontTableOffset(42);
|
||||
assert table>65535 && glyphOffset==table+96*4;
|
||||
byte[] memory = new byte[262144];
|
||||
for (byte[] upload: cache.drainUploadPayloads(3600)) {
|
||||
assert upload[0]==18;
|
||||
int pos=1;
|
||||
while (pos<upload.length) {
|
||||
int offset=u32(upload,pos), n=u16(upload,pos+4);
|
||||
System.arraycopy(upload,pos+6,memory,offset,n); pos+=6+n;
|
||||
}
|
||||
assert pos==upload.length;
|
||||
}
|
||||
assert memory[imageOffset]==1 && memory[imageOffset+2]==0x1f;
|
||||
assert u32(memory,table+('A'-32)*4)==glyphOffset;
|
||||
assert !cache.hasPendingUploads();
|
||||
// Exercise the production planner encoders with offsets above 64 KiB.
|
||||
Class<?> selected=Class.forName("com.faceclaw.app.TexturePlanner$Selected");
|
||||
Constructor<?> ctor=selected.getDeclaredConstructors()[0]; ctor.setAccessible(true);
|
||||
Object image=ctor.newInstance(SurfaceCompositor.ScreenDraw.image(100,321,123),null,tiny,321,15);
|
||||
Method encode=TexturePlanner.class.getDeclaredMethod("encodeImageDraw",TextureCacheState.class,selected);
|
||||
encode.setAccessible(true);
|
||||
byte[] draw=(byte[])encode.invoke(null,cache,image);
|
||||
assert draw.length==10 && draw[0]==19 && u32(draw,1)==imageOffset;
|
||||
assert u16(draw,5)==321 && u16(draw,7)==123 && draw[9]==31;
|
||||
Object letter=ctor.newInstance(SurfaceCompositor.ScreenDraw.glyph(42,'A',321,123,255),glyph,null,321,15);
|
||||
Method runs=TexturePlanner.class.getDeclaredMethod("buildRuns",TextureCacheState.class,List.class,List.class);
|
||||
runs.setAccessible(true); List<byte[]> out=new ArrayList<>();
|
||||
runs.invoke(null,cache,Collections.singletonList(letter),out);
|
||||
byte[] run=out.get(0);
|
||||
assert run.length==12 && run[0]==20 && u32(run,1)==table;
|
||||
assert u16(run,5)==321 && u16(run,7)==123 && run[9]==31 && run[10]==1 && run[11]=='A';
|
||||
int id=200;
|
||||
while (cache.ensureImage(id++,big)>=0) {}
|
||||
int before=cache.usedBytes();
|
||||
assert cache.ensureImage(id,big)==-1 && cache.usedBytes()==before;
|
||||
cache.reset(); assert cache.usedBytes()==0 && !cache.hasPendingUploads();
|
||||
assert cache.ensureImage(id,tiny)==0;
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ class JavaCompatibilityTest {
|
||||
|
||||
@Test fun gestures() = FaceclawGestureEventTest.main(emptyArray())
|
||||
|
||||
@Test fun textureCache() = TextureCacheProtocolTest.main(emptyArray())
|
||||
@Test fun resourceCache() = ResourceCacheProtocolTest.main(emptyArray())
|
||||
}
|
||||
|
||||
class AndroidByteReaderTest {
|
||||
|
||||
@@ -198,6 +198,20 @@ class ProtocolTest {
|
||||
assertEquals(1, callbacks)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun evictionWaitsForAllEarlierReplayableCommands() {
|
||||
val earlier = message(input = byteArrayOf(19, 0, 0))
|
||||
val eviction = message(input = byteArrayOf(22, 1, 0, 0, 0))
|
||||
val upload = message(input = byteArrayOf(21, 1, 0))
|
||||
assertFalse(CfwMessageWindow.canSend(listOf(earlier), eviction))
|
||||
earlier.cfwAckLenses = 3 // Still retained behind an unresolved window head.
|
||||
assertFalse(CfwMessageWindow.canSend(listOf(earlier), eviction))
|
||||
assertTrue(CfwMessageWindow.canSend(emptyList(), eviction))
|
||||
assertTrue(CfwMessageWindow.canSend(listOf(message(sid = 1)), eviction))
|
||||
assertTrue(CfwMessageWindow.canSend(listOf(earlier), upload))
|
||||
assertTrue(CfwMessageWindow.canSend(listOf(eviction), upload))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun orderedCompletionAndWholeWindowRecovery() {
|
||||
val head = message()
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
class ResourceCacheTest {
|
||||
private fun resource(size: Int, value: Int) = CachedResource(ByteArray(size) { value.toByte() })
|
||||
private fun u16(bytes: ByteArray, offset: Int) = (bytes[offset].toInt() and 255) or ((bytes[offset + 1].toInt() and 255) shl 8)
|
||||
private fun u32(bytes: ByteArray, offset: Int) = u16(bytes, offset) or (u16(bytes, offset + 2) shl 16)
|
||||
private fun uploaded(commands: List<ByteArray>): Map<Int, ByteArray> {
|
||||
val resources = HashMap<Int, ByteArray>()
|
||||
val received = HashMap<Int, Int>()
|
||||
for (command in commands) {
|
||||
assertTrue(command.size <= 3600)
|
||||
if (command[0].toInt() != 21) continue
|
||||
var pos = 3
|
||||
val seen = HashSet<Int>()
|
||||
repeat(u16(command, 1)) {
|
||||
val id = u16(command, pos)
|
||||
assertTrue(id in 0..511 && seen.add(id))
|
||||
val total = u32(command, pos + 2)
|
||||
val offset = u16(command, pos + 6)
|
||||
val length = u16(command, pos + 8)
|
||||
assertEquals(received[id] ?: 0, offset)
|
||||
val bytes = resources.getOrPut(id) { ByteArray(total) }
|
||||
command.copyInto(bytes, offset, pos + 10, pos + 10 + length)
|
||||
received[id] = offset + length
|
||||
pos += 10 + length
|
||||
}
|
||||
assertEquals(command.size, pos)
|
||||
}
|
||||
resources.forEach { (id, data) -> assertEquals(data.size, received[id]) }
|
||||
return resources
|
||||
}
|
||||
|
||||
@Test fun contentAddressingAndWireVector() {
|
||||
val cache = ResourceCacheState()
|
||||
val image = CachedResource(byteArrayOf(1, 1, 31))
|
||||
val ids = cache.prepare(listOf(image, CachedResource(image.bytes)))
|
||||
assertContentEquals(intArrayOf(0, 0), ids)
|
||||
val commands = cache.drainCommands(3600)
|
||||
assertEquals(22, commands.first()[0].toInt())
|
||||
assertEquals(512, u16(commands.first(), 1))
|
||||
assertEquals(511, u16(commands.first(), 1025))
|
||||
assertContentEquals(byteArrayOf(21, 1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 1, 1, 31), commands.last())
|
||||
assertContentEquals(image.bytes, uploaded(commands).getValue(0))
|
||||
assertEquals(2068, cache.usedBytes())
|
||||
assertEquals(0, cache.prepare(listOf(image))[0])
|
||||
assertTrue(cache.drainCommands(3600).isEmpty())
|
||||
cache.reset(); cache.prepare(listOf(image))
|
||||
assertEquals(22, cache.drainCommands(3600).first()[0].toInt())
|
||||
}
|
||||
|
||||
@Test fun exactly64KiBUploadsInBoundedContiguousChunks() {
|
||||
val cache = ResourceCacheState()
|
||||
val data = CachedResource(ByteArray(65536) { (it * 37).toByte() })
|
||||
assertEquals(0, cache.prepare(listOf(data))[0])
|
||||
val commands = cache.drainCommands(3600)
|
||||
assertTrue(commands.count { it[0].toInt() == 21 } > 1)
|
||||
assertContentEquals(data.bytes, uploaded(commands).getValue(0))
|
||||
assertEquals(-1, cache.prepare(listOf(resource(65537, 1)))[0])
|
||||
assertTrue(cache.drainCommands(3600).isEmpty())
|
||||
assertEquals(0, cache.resourceId(data))
|
||||
}
|
||||
|
||||
@Test fun lruEvictsByBytesAndProtectsAllCurrentFrameHits() {
|
||||
val cache = ResourceCacheState()
|
||||
val a = resource(65536, 1); val b = resource(65536, 2); val c = resource(65536, 3); val d = resource(65536, 4)
|
||||
assertContentEquals(intArrayOf(0, 1, 2), cache.prepare(listOf(a, b, c)))
|
||||
cache.drainCommands(3600)
|
||||
cache.prepare(listOf(a)); cache.drainCommands(3600) // b is oldest now.
|
||||
assertContentEquals(intArrayOf(1, 2), cache.prepare(listOf(d, c))) // protect later hit c before d.
|
||||
val commands = cache.drainCommands(3600)
|
||||
assertContentEquals(byteArrayOf(22, 1, 0, 1, 0), commands.first())
|
||||
assertEquals(-1, cache.resourceId(b))
|
||||
assertEquals(0, cache.resourceId(a)); assertEquals(2, cache.resourceId(c))
|
||||
assertEquals(1, cache.resourceId(d))
|
||||
assertContentEquals(d.bytes, uploaded(commands).getValue(1))
|
||||
val tooLarge = cache.prepare(listOf(a, c, d, b))
|
||||
assertEquals(-1, tooLarge.last())
|
||||
assertTrue(cache.drainCommands(3600).isEmpty())
|
||||
assertEquals(3, cache.residentCount())
|
||||
}
|
||||
|
||||
@Test fun idPressureUsesLruEvenWhenBytesAreAvailable() {
|
||||
val cache = ResourceCacheState()
|
||||
val resources = (0..512).map { CachedResource(byteArrayOf(it.toByte(), (it ushr 8).toByte())) }
|
||||
assertContentEquals(IntArray(512) { it }, cache.prepare(resources.take(512)))
|
||||
cache.drainCommands(3600)
|
||||
cache.prepare(listOf(resources[0])); cache.drainCommands(3600)
|
||||
assertEquals(1, cache.prepare(listOf(resources[512]))[0])
|
||||
val commands = cache.drainCommands(3600)
|
||||
assertContentEquals(byteArrayOf(22, 1, 0, 1, 0), commands.first())
|
||||
assertEquals(0, cache.resourceId(resources[0]))
|
||||
assertEquals(-1, cache.resourceId(resources[1]))
|
||||
}
|
||||
|
||||
@Test fun rejectedPlansRestoreResidencyAndLru() {
|
||||
val cache = ResourceCacheState()
|
||||
val resources = (0..3).map { resource(65536, it) }
|
||||
cache.prepare(resources.take(3)); cache.drainCommands(3600)
|
||||
val rollback = cache.checkpoint()
|
||||
cache.prepare(listOf(resources[3]))
|
||||
assertEquals(-1, cache.resourceId(resources[0]))
|
||||
rollback()
|
||||
assertEquals(0, cache.resourceId(resources[0])); assertEquals(-1, cache.resourceId(resources[3]))
|
||||
assertTrue(cache.drainCommands(3600).isEmpty())
|
||||
cache.prepare(listOf(resources[3]))
|
||||
assertContentEquals(byteArrayOf(22, 1, 0, 0, 0), cache.drainCommands(3600).first())
|
||||
}
|
||||
|
||||
@Test fun imagePlannerEmitsResourceIdsAndReusesResidency() {
|
||||
val cache = ResourceCacheState()
|
||||
val id = ImageAtlas.ensure("resource-test-white", 2, 2, ArrayByteReader(ByteArray(4) { -1 }))
|
||||
val pixels = ByteArray(8 * 4)
|
||||
for (y in 0..1) for (x in 0..1) pixels[y * 8 + x] = -1
|
||||
val packed = BmpUtil.pack4bppFromGray8(pixels, 8, 4)
|
||||
val draws = arrayOf(SurfaceCompositor.ScreenDraw.image(id, 0, 0))
|
||||
fun plan() = assertNotNull(TexturePlanner.plan(null, packed, 8, 4, draws, cache, 1, true, 8, testPlatform()))
|
||||
val cold = plan()
|
||||
assertEquals(1, cold.drawnImages)
|
||||
assertContentEquals(byteArrayOf(19, 0, 0, 0, 0, 0, 0, 31), cold.payload.takeLast(8).toByteArray())
|
||||
assertTrue(cold.resourceCommands.isNotEmpty())
|
||||
val warm = plan()
|
||||
assertTrue(warm.resourceCommands.isEmpty()); assertContentEquals(cold.payload, warm.payload)
|
||||
cache.reset(); assertTrue(plan().resourceCommands.isNotEmpty())
|
||||
assertNull(TexturePlanner.plan(null, ByteArray(packed.size), 8, 4, draws, cache, 1, true, 8, testPlatform()))
|
||||
}
|
||||
|
||||
private fun registerFont(key: String, encodings: List<Int>, size: Int = 2): Int {
|
||||
val out = ByteSink(); val name = key.encodeToByteArray()
|
||||
out.write(name.size); out.write(name, 0, name.size); out.write(size)
|
||||
out.write(encodings.size); out.write(0)
|
||||
for (encoding in encodings) {
|
||||
out.write(encoding); repeat(3) { out.write(0) }
|
||||
out.write(0); out.write(0); out.write(size); out.write(size)
|
||||
repeat(size * ((size + 1) / 2)) { out.write(0x12) }
|
||||
}
|
||||
GlyphAtlas.registerAa(ArrayByteReader(out.toByteArray()))
|
||||
return GlyphAtlas.fontId(key)
|
||||
}
|
||||
|
||||
@Test fun fontTablesUseRelativeU16OffsetsAndGrowOnDemand() {
|
||||
val id = registerFont("resource-font-relative", listOf(65, 66))
|
||||
val first = FontResourceAtlas.get(id, setOf(65))
|
||||
assertEquals(192, u16(first.resource.bytes, (65 - 32) * 2))
|
||||
assertEquals(0, u16(first.resource.bytes, (66 - 32) * 2))
|
||||
assertSame(first, FontResourceAtlas.get(id, setOf(65)))
|
||||
val second = FontResourceAtlas.get(id, setOf(66))
|
||||
assertTrue(second.encodings.containsAll(setOf(65, 66)))
|
||||
assertEquals(192 + GlyphAtlas.get(id, 65)!!.cachedBytes.size, u16(second.resource.bytes, (66 - 32) * 2))
|
||||
val cache = ResourceCacheState()
|
||||
cache.prepare(listOf(second.resource)); val commands = cache.drainCommands(3600)
|
||||
assertContentEquals(second.resource.bytes, uploaded(commands).getValue(0))
|
||||
}
|
||||
|
||||
@Test fun oversizedFontsPrioritizeTheCurrentFrameWithin64KiB() {
|
||||
val id = registerFont("resource-font-limit", listOf(65, 66), 255)
|
||||
val a = FontResourceAtlas.get(id, setOf(65))
|
||||
assertEquals(setOf(65), a.encodings)
|
||||
val b = FontResourceAtlas.get(id, setOf(66))
|
||||
assertEquals(setOf(66), b.encodings)
|
||||
assertTrue(b.resource.bytes.size <= 65536)
|
||||
assertEquals(0, u16(b.resource.bytes, (65 - 32) * 2))
|
||||
assertEquals(192, u16(b.resource.bytes, (66 - 32) * 2))
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
class TextureCacheTest {
|
||||
private fun u32(bytes: ByteArray, offset: Int): Int =
|
||||
(0..3).fold(0) { n, i -> n or ((bytes[offset + i].toInt() and 255) shl (i * 8)) }
|
||||
|
||||
@Test
|
||||
fun offsetsAbove64KiBAndLazyFontSlotsRoundTripOnBothPlatforms() {
|
||||
val cache = TextureCacheState()
|
||||
val large = ImageAtlas.Entry(255, 255, ByteArray(255 * 255) { (it % 15 + 1).toByte() })
|
||||
repeat(3) { assertTrue(cache.ensureImage(it, large) >= 0) }
|
||||
val glyph = GlyphAtlas.Glyph(1, 1, 0, 1, 0, null, byteArrayOf(15))
|
||||
val offset = cache.ensureGlyph(42, 65, glyph)
|
||||
val table = cache.fontTableOffset(42)
|
||||
assertTrue(table > 65535)
|
||||
assertEquals(table + 384, offset)
|
||||
val memory = ByteArray(TextureCacheState.CACHE_SIZE)
|
||||
for (upload in cache.drainUploadPayloads(3600)) {
|
||||
assertEquals(18, upload[0].toInt())
|
||||
var pos = 1
|
||||
while (pos < upload.size) {
|
||||
val address = u32(upload, pos)
|
||||
val length = (upload[pos + 4].toInt() and 255) or ((upload[pos + 5].toInt() and 255) shl 8)
|
||||
upload.copyInto(memory, address, pos + 6, pos + 6 + length)
|
||||
pos += 6 + length
|
||||
}
|
||||
assertEquals(upload.size, pos)
|
||||
}
|
||||
assertEquals(offset, u32(memory, table + (65 - 32) * 4))
|
||||
assertContentEquals(glyph.cachedBytes, memory.copyOfRange(offset, offset + glyph.cachedBytes.size))
|
||||
assertEquals(offset, cache.ensureGlyph(42, 65, glyph))
|
||||
assertFalse(cache.hasPendingUploads())
|
||||
cache.reset()
|
||||
assertEquals(0, cache.usedBytes())
|
||||
assertEquals(384, cache.ensureGlyph(42, 65, glyph))
|
||||
assertTrue(cache.hasPendingUploads())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun plannerReusesResidencyAndReuploadsAfterReset() {
|
||||
val cache = TextureCacheState()
|
||||
val id = ImageAtlas.ensure("texture-test-white", 2, 2, ArrayByteReader(ByteArray(4) { -1 }))
|
||||
val pixels = ByteArray(8 * 4)
|
||||
for (y in 0..1) for (x in 0..1) pixels[y * 8 + x] = -1
|
||||
val packed = BmpUtil.pack4bppFromGray8(pixels, 8, 4)
|
||||
val draws = arrayOf(SurfaceCompositor.ScreenDraw.image(id, 0, 0))
|
||||
fun plan() = assertNotNull(TexturePlanner.plan(null, packed, 8, 4, draws, cache, 1, true, 8, testPlatform()))
|
||||
val cold = plan()
|
||||
assertEquals(1, cold.drawnImages)
|
||||
assertTrue(cold.uploads.isNotEmpty())
|
||||
val warm = plan()
|
||||
assertTrue(warm.uploads.isEmpty())
|
||||
assertContentEquals(cold.payload, warm.payload)
|
||||
cache.reset()
|
||||
assertTrue(plan().uploads.isNotEmpty())
|
||||
// Occlusion fails the correctness check and leaves the image baked.
|
||||
assertNull(TexturePlanner.plan(null, ByteArray(packed.size), 8, 4, draws, cache, 1, true, 8, testPlatform()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun fullCacheResetsAndRetriesTheCurrentFrame() {
|
||||
val cache = TextureCacheState()
|
||||
val large = ImageAtlas.Entry(255, 255, ByteArray(255 * 255) { (it % 15 + 1).toByte() })
|
||||
var fillerId = -1
|
||||
while (cache.ensureImage(fillerId--, large) >= 0) {}
|
||||
// Fill the remaining tail, forcing the next real allocation to fail.
|
||||
val remaining = TextureCacheState.CACHE_SIZE - cache.usedBytes()
|
||||
while (cache.ensureImage(fillerId--, ImageAtlas.Entry(1, 1, byteArrayOf(15))) >= 0) {}
|
||||
assertTrue(remaining > 0)
|
||||
val generation = cache.generation()
|
||||
val id = ImageAtlas.ensure("texture-test-full", 2, 2, ArrayByteReader(ByteArray(4) { -1 }))
|
||||
val result = assertNotNull(TexturePlanner.plan(null, ByteArray(4) { -1 }, 4, 2,
|
||||
arrayOf(SurfaceCompositor.ScreenDraw.image(id, 0, 0)), cache, 1, true, 8, testPlatform()))
|
||||
assertEquals(generation + 1, cache.generation())
|
||||
assertEquals(1, result.drawnImages)
|
||||
assertTrue(cache.usedBytes() < 100)
|
||||
assertTrue(result.uploads.isNotEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun workingSetLargerThanTheCacheFallsBackWithoutAResetLoop() {
|
||||
val cache = TextureCacheState()
|
||||
val raster = ByteArray(255 * 255) { if (it % 2 == 0) 16 else 32 }
|
||||
val draws = (0..4).map {
|
||||
val id = ImageAtlas.ensure("oversized-working-set-$it", 255, 255, ArrayByteReader(raster))
|
||||
SurfaceCompositor.ScreenDraw.image(id, 0, 0)
|
||||
}.toTypedArray()
|
||||
val gray = ByteArray(256 * 256)
|
||||
repeat(255) { raster.copyInto(gray, it * 256, it * 255, (it + 1) * 255) }
|
||||
val result = assertNotNull(TexturePlanner.plan(null,
|
||||
BmpUtil.pack4bppFromGray8(gray, 256, 256), 256, 256, draws, cache, 1, true, 8, testPlatform()))
|
||||
assertEquals(1, cache.generation())
|
||||
assertEquals(4, result.drawnImages)
|
||||
assertEquals(1, result.bakedCandidates)
|
||||
assertTrue(cache.usedBytes() <= TextureCacheState.CACHE_SIZE)
|
||||
}
|
||||
}
|
||||
@@ -24,13 +24,14 @@ class IosTextureApiTest {
|
||||
val next = IosProtocol().pack(frame.pixels, 8, 4)
|
||||
val planner = IosTexturePlanner()
|
||||
val cold = assertNotNull(planner.plan(null, next, frame, 1))
|
||||
assertEquals(18, cold.uploads.first().byteArray()[0].toInt())
|
||||
assertEquals(22, cold.resourceCommands.first().byteArray()[0].toInt())
|
||||
assertTrue(cold.resourceCommands.any { it.byteArray()[0].toInt() == 21 })
|
||||
assertEquals(8, cold.payload.byteArray()[0].toInt())
|
||||
val warm = assertNotNull(planner.plan(null, next, frame, 1))
|
||||
assertTrue(warm.uploads.isEmpty())
|
||||
assertTrue(warm.resourceCommands.isEmpty())
|
||||
assertContentEquals(cold.payload.byteArray(), warm.payload.byteArray())
|
||||
planner.reset()
|
||||
assertTrue(assertNotNull(planner.plan(null, next, frame, 1)).uploads.isNotEmpty())
|
||||
assertTrue(assertNotNull(planner.plan(null, next, frame, 1)).resourceCommands.isNotEmpty())
|
||||
// A partial update clears identities even if the caller supplies stale draws.
|
||||
compositor.submitDraws("app", byteArrayOf(-1).data(), 0, 0, 1, 1, imageDraw(id).data())
|
||||
assertTrue(compositor.compositeFrame().composite.draws!!.isEmpty())
|
||||
@@ -50,10 +51,10 @@ class IosTextureApiTest {
|
||||
compositor.submitDraws("text", ByteArray(4) { -1 }.data(), 0, 0, 2, 2, draws.data())
|
||||
val frame = compositor.compositeFrame()
|
||||
val plan = assertNotNull(IosTexturePlanner().plan(null, IosProtocol().pack(frame.pixels, 4, 2), frame, 1))
|
||||
assertTrue(plan.usedBytes > 384)
|
||||
assertTrue(plan.usedBytes > 2048 + 192)
|
||||
val payload = plan.payload.byteArray()
|
||||
// The last mode-8 submessage is the shared mode-20 cached glyph run.
|
||||
assertEquals(20, payload[payload.size - 12].toInt())
|
||||
assertEquals(20, payload[payload.size - 10].toInt())
|
||||
assertEquals(65, payload.last().toInt())
|
||||
}
|
||||
|
||||
@@ -81,6 +82,6 @@ class IosTextureApiTest {
|
||||
small.submitDraws("icon", white, 0, 0, 2, 2, imageDraw(id).data())
|
||||
val next = small.compositeFrame()
|
||||
val retry = assertNotNull(planner.plan(null, IosProtocol().pack(next.pixels, 4, 2), next, 1))
|
||||
assertTrue(retry.uploads.isNotEmpty(), "the rejected plan's image was never uploaded")
|
||||
assertTrue(retry.resourceCommands.isNotEmpty(), "the rejected plan's image was never uploaded")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user