Display lists

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

No files matched your search

@@ -260,11 +260,13 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
private final SurfaceCompositor compositor = new SurfaceCompositor();
// Phone-side model of the CFW's 256 KiB resource cache (modes 19/20/21/22).
// Phone-side model of the CFW's 192 KiB resource cache.
// 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 ResourceCacheState resourceCache = new ResourceCacheState();
private final ScenePlanner scenePlanner = new ScenePlanner(resourceCache);
private ShellScene desiredShellScene = ShellScene.Companion.getEMPTY();
private final ArrayDeque<OutboundMessage> pendingMessages = new ArrayDeque<>();
private final CfwTransport[] cfwTransports = { new CfwTransport(AndroidProtocolPlatform.INSTANCE), new CfwTransport(AndroidProtocolPlatform.INSTANCE) };
@@ -1141,7 +1143,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
"compositor:visible " + id + "=" + visible);
compositor.setSurfaceVisible(id, visible);
SurfaceCompositor.Composite composite = compositor.composite();
byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height);
byte[] packed = BmpUtil.pack4bppFromGray8(composite.screenGray, composite.width, composite.height);
storeDesiredComposite(composite, packed, 0, frameId);
}
@@ -1155,7 +1157,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
"compositor:" + (blanked ? "blank" : "unblank"));
compositor.setBlanked(blanked);
SurfaceCompositor.Composite composite = compositor.composite();
byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height);
byte[] packed = BmpUtil.pack4bppFromGray8(composite.screenGray, composite.width, composite.height);
storeDesiredComposite(composite, packed, 0, frameId);
}
@@ -1172,12 +1174,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
compositor.removeSurface(id);
}
/**
* Dim every surface below zOrder belowZOrder to factor256/256 (see
* SurfaceCompositor.setUnderlayDim). Takes effect with the next submitted
* frame: the shell always submits its own surface right after changing
* this, so no recomposite happens here.
*/
/** Stage an immutable shell snapshot alongside the retained app screen. */
public void submitShellScene(java.nio.ByteBuffer bytes, int paintMs, int frameId) {
compositor.setShellScene(new AndroidByteReader(bytes));
SurfaceCompositor.Composite composite = compositor.composite();
storeDesiredComposite(composite, BmpUtil.pack4bppFromGray8(composite.screenGray, composite.width, composite.height), paintMs, frameId);
}
/** Legacy compositor dimming for callers without a shell scene. */
public void setUnderlayDim(int belowZOrder, int factor256) {
compositor.setUnderlayDim(belowZOrder, factor256);
}
@@ -1239,7 +1243,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
// format the wire planners consume; BMP framing is added later only for
// the uncompressed fallback.
FrameTimings.getInstance().spanStart(frameId, "pack-4bpp");
byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height);
byte[] packed = BmpUtil.pack4bppFromGray8(composite.screenGray, composite.width, composite.height);
FrameTimings.getInstance().spanEnd(frameId, "pack-4bpp");
storeDesiredComposite(composite, packed, paintMs, frameId);
}
@@ -1263,6 +1267,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
desiredPaintMs = paintMs;
desiredFrameId = frameId;
desiredDraws = composite.draws;
desiredShellScene = composite.shellScene;
}
}
if (stale) {
@@ -2936,6 +2941,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
int paintMs;
int frameId;
SurfaceCompositor.ScreenDraw[] draws;
ShellScene scene;
synchronized (desiredTilesLock) {
packed = desiredPacked;
width = desiredWidth;
@@ -2943,58 +2949,25 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
paintMs = desiredPaintMs;
frameId = desiredFrameId;
draws = desiredDraws;
scene = desiredShellScene;
desiredFrameId = 0;
}
if (packed == null) {
packed = new byte[0];
}
if (lastEnqueuedWidth == width && lastEnqueuedHeight == height
&& Arrays.equals(packed, lastEnqueuedPacked)) {
lastEnqueuedFingerprint = fingerprint;
finishFrame(frameId, "discarded: image content identical to displayed");
if (customFirmwareDetected && packed.length > 0) {
ScenePlanner.Plan rendered = scenePlanner.plan(packed, width, height, draws, scene, nextImageFrameId,
connectionOptions.TEXTURE_CACHE_FRAMES, connectionOptions.INCREMENTAL_FRAMES,
connectionOptions.MULTI_RECT_FRAMES, ConnectionOptions.MULTI_RECT_MAX_RECTS);
nextImageFrameId = rendered.getNextFid();
List<byte[]> commands = rendered.getCommands();
for (int i = 0; i < commands.size() - 1; i++) enqueueResourceCommandLocked(commands.get(i));
BleImageOptimizer.TileImagePlan plan = new BleImageOptimizer.TileImagePlan(
0, DASHBOARD_TILE, packed, width, height, nextMapSessionId(), commands.get(commands.size() - 1));
finishEnqueueDesiredImageLocked(plan, fingerprint, paintMs, frameId);
return;
}
// 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
&& draws != null && draws.length > 0 && packed.length > 0) {
byte[] deltaBase = (connectionOptions.INCREMENTAL_FRAMES && lastEnqueuedPacked.length > 0
&& lastEnqueuedWidth == width && lastEnqueuedHeight == height)
? lastEnqueuedPacked : null;
FrameTimings.getInstance().spanStart(frameId, "texture-plan");
TexturePlanner.Result tex = TexturePlanner.plan(
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.resourceCommands) {
enqueueResourceCommandLocked(upload);
}
BleImageOptimizer.TileImagePlan plan = new BleImageOptimizer.TileImagePlan(
0, DASHBOARD_TILE, packed, width, height, nextMapSessionId(), tex.payload);
FrameTimings.getInstance().spanEnd(frameId, "texture-plan");
String texLog = "texture update " + (tex.fullFrame ? "full" : ("rects=" + tex.rectCount))
+ " glyphs=" + tex.drawnGlyphs + " runs=" + tex.runCount
+ " images=" + tex.drawnImages
+ " fw=" + tex.fwGlyphs + "/" + tex.fwRuns
+ " baked=" + tex.bakedCandidates
+ (tex.uploadBytes > 0 ? " upload=" + tex.uploadBytes + "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
+ "ms, encode " + tex.encodeMs + "ms)";
FrameTimings.getInstance().log(frameId, texLog);
finishEnqueueDesiredImageLocked(plan, fingerprint, paintMs, frameId);
return;
}
FrameTimings.getInstance().spanEnd(frameId, "texture-plan");
}
FrameTimings.getInstance().spanStart(frameId, "compress-and-plan");
// Incremental (mode 3 bounding box) update against the last ENQUEUED frame
// (the base the firmware shadow will hold when this update is applied).
@@ -73,6 +73,13 @@ public final class FaceclawPreviewCompositor {
}
/** Dim every surface below zOrder belowZOrder to factor256/256 (see SurfaceCompositor). */
public void submitShellScene(java.nio.ByteBuffer bytes, int paintMs, int frameId) {
compositor.setShellScene(new AndroidByteReader(bytes));
FrameTimings.getInstance().finishFrame(frameId, "composited (preview-only, no glasses)");
Runnable listener = frameListener;
if (listener != null) mainHandler.post(listener);
}
public void setUnderlayDim(int belowZOrder, int factor256) {
compositor.setUnderlayDim(belowZOrder, factor256);
}
+2 -27
View File
@@ -2467,7 +2467,7 @@ class DashboardController {
beginRenderPass(!wantFreshData);
const paintStartedAtMs = Date.now();
const planes = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => shell.paintSurface()),
frameTimings.runWithFrame(frameId, () => shell.paintScene()),
);
const paintMs = Date.now() - paintStartedAtMs;
const paintUsedStaleData = endRenderPass();
@@ -2484,32 +2484,7 @@ class DashboardController {
frameTimings.finishFrame(frameId, "discarded: shell render with no display target");
return;
}
// A shell overlay that dims what it covers (a context menu) must dim the
// window surfaces too, which live below the shell surface in the
// compositor: forward the factor before this frame composites.
const underlayDim = shell.underlayDim();
if (underlayDim !== this.appliedUnderlayDim) {
this.appliedUnderlayDim = underlayDim;
await display.setUnderlayDim(SHELL_SURFACE_Z_ORDER, underlayDim);
}
const fingerprint = frameTimings.span(frameId, "fingerprint", () => planesFingerprint(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws));
// Spanned because the bridge serializes Java calls: a frame can sit here
// behind another surface's submission, which is otherwise an unexplained
// jump between the paint spans and the composite.
await frameTimings.spanAsync(frameId, "submit", () =>
display.submitSurfaceFrame(
SHELL_SURFACE_ID,
buffer,
{ x: 0, y: 0, width: image.width, height: image.height },
fingerprint,
paintMs,
frameId,
preparedDraws,
),
);
await frameTimings.spanAsync(frameId, "submit", () => display.submitShellScene(planes, paintMs, frameId));
// Backpressure: the next shell render waits for this one to reach the
// glasses. Timing out here means the loop was blocked for the full timeout
// and any input arriving meanwhile had its chrome repaint delayed, so say
+14
View File
@@ -0,0 +1,14 @@
/** Adapter for raster-only senders; optimized legacy layouts remain internal to the phone. */
export function drawMessages(payload: Uint8Array, width = 640, height = 480): Uint8Array[] {
const word = (v: number) => [v & 255, (v >>> 8) & 255]
const mode = payload[0] & 127
let calls: Uint8Array[]
if (mode === 8) {
const output: Uint8Array[] = []; let p=2
for(let i=0;i<payload[1];i++) { const n=payload[p] | payload[p+1]<<8; p+=2; output.push(...drawMessages(payload.subarray(p,p+n),width,height));p+=n }
return output
} else if (mode === 3) calls=[new Uint8Array([1,0,0,...payload.subarray(1,5),...payload.subarray(7)])]
else if(mode === 6) calls=[new Uint8Array([1,0,1,0,0,0,0,...word(width),...word(height),...payload.subarray(1)])]
else throw new Error(`Unsupported raster draw ${mode}`)
return calls.map(call => new Uint8Array([26,1,0,...word(call.length),...call]))
}
+1 -1
View File
@@ -26,7 +26,7 @@ export type FirmwareInfo = {
};
/** The Faceclaw firmware revision this build of the app needs. */
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 23;
export const REQUIRED_FACECLAW_FIRMWARE_VERSION = 25;
const FACECLAW_PREFIX = "Faceclaw/";
const LEGACY_PREFIX = "EVENCFW";
+34 -34
View File
@@ -19,12 +19,12 @@ export type FirmwarePatchSet = {
export const CFW_PATCH_SET: FirmwarePatchSet = {
"base": "g2_2.3.0.24.bin",
"baseSha256": "187ccf2bcc5c17a212106e8a376745511e8289c4232b634a7ea94b9bf25a0979",
"outputSha256": "393ea24203d9a388975c732cae4698339f8af0e13139b73d71386181a1bc004e",
"outputSha256": "986930903c6ea41ad7fd7ed15c73c0a1af00cc9b58604abfe2092a79745bb641",
"patches": [
{
"offset": 1045797,
"old": "38b584b0",
"new": "59f3efbf",
"new": "5cf337be",
"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": "faf2fefc",
"new": "fdf246fb",
"desc": "cfw_ancs_event"
},
{
"offset": 1437951,
"old": "fef7e8fb",
"new": "faf238fa",
"new": "fdf280f8",
"desc": "cfw_ancs_open"
},
{
"offset": 1437969,
"old": "fef7e7fb",
"new": "faf24ffa",
"new": "fdf297f8",
"desc": "cfw_ancs_close"
},
{
"offset": 1388387,
"old": "39694d00",
"new": "e5317d00",
"new": "755e7d00",
"desc": "connection-bound ANCS EUS write callback"
},
{
"offset": 971497,
"old": "fff717f8",
"new": "6cf31ff9",
"new": "6ef367ff",
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
},
{
"offset": 971845,
"old": "fef769ff",
"new": "6cf371f8",
"new": "6ef3b9fe",
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
},
{
"offset": 979579,
"old": "fdf74ef8",
"new": "6af356f9",
"new": "6cf39eff",
"desc": "bl faceclaw_input_bridge_received (ring relay and ordered private transport before worker pool)"
},
{
"offset": 1428895,
"old": "f8f76cfd",
"new": "f9f28afd",
"new": "fcf292f8",
"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": "27f354fb",
"new": "2af3bef8",
"desc": "bl settings_send_wrapper (append caps field 100)"
},
{
"offset": 1246183,
"old": "f5f7e2f8",
"new": "27f374fd",
"new": "2af360fa",
"desc": "bl settings_decode_wrapper (Faceclaw lease field 101)"
},
{
"offset": 975219,
"old": "02f083fc",
"new": "69f3d6fd",
"new": "6cf3c2fa",
"desc": "bl faceclaw_display_start @ 0x467d68 (fail-open wake takeover)"
},
{
"offset": 975411,
"old": "02f023fc",
"new": "69f344fe",
"new": "6cf330fb",
"desc": "bl faceclaw_display_start_headup @ 0x467e28 (fail-open wake takeover)"
},
{
"offset": 975103,
"old": "07f085fb",
"new": "6bf328f9",
"new": "6df370ff",
"desc": "bl headup_gate (head-up sensor event -> EvenHub sys event 12 under lease)"
},
{
"offset": 830549,
"old": "19f03efb",
"new": "8ef3d7fa",
"new": "91f31ff9",
"desc": "bl gesture_ring_press_mode (observe ring touch-down, preserve stock dispatch)"
},
{
"offset": 830795,
"old": "1ff071ff",
"new": "8ef390fa",
"new": "91f3d8f8",
"desc": "bl gesture_press (owned event 9 before stock menu gate)"
},
{
"offset": 830887,
"old": "1ff043ff",
"new": "8ef38afa",
"new": "91f3d2f8",
"desc": "bl gesture_short_long (Faceclaw lease gates event 11 vs stock Menu path)"
},
{
"offset": 831763,
"old": "1ff08dfd",
"new": "8ef3c0f8",
"new": "90f308ff",
"desc": "bl gesture_release (owned event 10 before stock menu gate)"
},
{
"offset": 1570209,
"old": "7fb50600",
"new": "d8f2e5be",
"new": "dbf24fbc",
"desc": "even_ai_display_ctrl entry -> conditional Faceclaw lease trampoline"
},
{
"offset": 1051545,
"old": "f6f787f9",
"new": "53f3cbfc",
"new": "55f3fbfb",
"desc": "bl display_copy_hook @ 0x47a78e (640x480 direct framebuffer)"
},
{
"offset": 1051861,
"old": "f6f7e9f8",
"new": "53f32dfc",
"new": "55f35dfb",
"desc": "bl display_copy_hook @ 0x47a8ca (640x480 direct framebuffer)"
},
{
"offset": 1262237,
"old": "d9f7d0fd",
"new": "23f3b3fc",
"new": "26f333f9",
"desc": "bl faceclaw_send_wear_event @ 0x4ade92 (outside onboarding)"
},
{
"offset": 1262337,
"old": "d9f79efd",
"new": "23f381fc",
"new": "26f301f9",
"desc": "bl faceclaw_send_wear_event @ 0x4adef6 (outside onboarding)"
},
{
"offset": 1309405,
"old": "67f02dff",
"new": "17f39bfd",
"new": "1af319fa",
"desc": "bl compass_decode_capture (sample-matched GAF diagnostics)"
},
{
"offset": 1307865,
"old": "fff7c9fb",
"new": "18f319f9",
"new": "1af397fd",
"desc": "bl compass_report_event (stock UI + heading with diagnostics over BLE)"
},
{
"offset": 1300937,
"old": "00210170",
"new": "19f3fffd",
"new": "1cf37dfa",
"desc": "bl compass_preserve_accuracy (retain magnetic calibration under Faceclaw framebuffer lease)"
},
{
"offset": 4537963,
"old": "",
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf2de9f04fa5b04ef66c6a4ff29c5516460c46ccf2ff0ac2f229054ff6ff7800274ff00109019006e048f2a351c0f24501bff32f8f8847d5f800b0bff35f8f28680321cff680710140b1f1005fbff35f8f02d10168514543d04ff4417004f050fb002800f036810021421847540431b1f5417f57709770d770f6d1c0f800a080f80a90a0f81c80a0f81e80a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a8055e8001f5945aed1bff35f8f00bf45e8000121b155e8001f5945f8d0a3e7a34644ea064100260127c0f8701206e048f2a351c0f24501bff32f8f88472c68bff35f8f28680321cff680710140b1f1005fbff35f8f02d10168514542d04ff4417004f0edfa002800f0d380002100bf421846540431b1f5417f56709670d670f6d1c0f800a08772a0f81c80a0f81e80a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a8055e8001fa142afd1bff35f8f45e8000121b155e8001fa142f8d0a5e7d0f8746242f6f767c0f24407002ebff35f8f71d007f19a0230464ff0ff314ff0ff379047002840f0c28047f6df40c0f24f00804700274ff001090397adf8107007e000bf48f2a351c0f24501bff32f8f88472c68bff35f8f28680321cff680710140b1f1005fbff35f8f02d10168514567d04ff4417004f071fa002860d00021421847540431b1f5417f57709770d770f6d1c0f800a080f80a90a0f81c80a0f81e80a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a8055e8001fa142b0d1bff35f8f00bf45e8000149b355e8001fa142f8d0a5e704460020b847b0b104f51d7151e8006f4eb9bff35f8f00bf41e8000292b151e8006f002ef8d00021bff32f8fbff35f8f21b1064672e74ff0ff373ce007f5a97188476be70121f1e70020019f42f6f769002f4ff00104c0f2440918bfbbf1000f2dd102ad8046284602f054f903ab38465946012205f0eefc9df8111007462846002918bf4ff0ff3702f0f6f9002c044608d19df8100028b94af21f30c0f247004c308047b8f1000f18bfc8f80c4009f5807130468847384625b0bde8f08f3a7802f07f01a1f1150313f1030f0bd802f07b030b2b07d002f07e02082a03d0032901d00629bdd140b14af21f31c0f247010446884794f8641111b14ff0ff37d6e720460024ade700bf2de9f0474ef66c6a4ff29c5548f2a359ccf2ff0ac2f229054ff6ff7600274ff00108c0f2450902e0bff32f8fc8472c68bff35f8f2868bff35f8f01466ff39601b1f1005f02d10168514535d04ff4417004f084f978b30021421847540431b1f5417f57709770d770f6d1c0f800a080f80a808683c683068446848684c684068546858685c685068646868686c6860687468755e8001fa142c6d1bff35f8f00bf45e80001002908bfbde8f08755e8001fa142f5d0b8e70020bde8f08770b500284dd001684ef66c62ccf2ff02914218bf70bd90f8441090f845504bf6394c8d42c0f2510c2ad205eb8501014491f8492091f84a4091f846e091f8473091f8481042ea0424012c4ff0010298bf14466e1c054680f84560b9b164294eea032028bf6421012888bf024644f62060824228bf02460cf5e2731046984706e0002180f84410bde870406047e047286c38b142f64d42c0f244022146bde87040104770bd02464ff64b70cff6ff7042f6653c7844c0f2440c00210023604700bf2de9f04f9db04ff29c50c2f22900d0f800b0bff35f8f58466ff39600b0f1005f3fd1dbf800004ef66c65ccf2ff05a84237d19bf8640110b3dbf86041fcb140f24030c2f20000d0f8787502a802f020f8002f00f0f18040f20308384621464ff41632cff680784ff6ff760197fff764fc48f2a35900240127c0f245091ce09bf8660170b1dbf8680140b147f64c61c2f207010968401a002801f3cc8700208bf8660140f6a120c0f247001db0bde8f04f0047bff32f8fc8474ff29c50c2f22900d0f800a0bff35f8f0068bff35f8f00ea0801b1f1005f02d10168a94236d04ff4417004f07df8002801f08c87002100bf421844540431b1f5417f54709470d470f6d14ff29c52056087728683c683068446848684c684068546858685c685068646868686c68606874687c2f2290252e8001f5145bdd1bff35f8f00bf42e8000121b152e8001f5145f8d0b2e7817a002941f05887027e0021002a8df814107cd04ff29c5605aac2f22906535c002b00f0b68053185f78002f74d09f78002f73d0df78002f72d01f79002f71d05f79002f70d09f79002f6fd0df79002f6ed01f7a002f6dd05f7a002f6cd09f7a002f6bd0df7a002f6ad01f7b002f69d05f7b002f68d09f7b002f67d0df7b002f66d01f7c002f65d05f7c002f64d09f7c002f63d0df7c002f62d01f7d002f61d05f7d002f60d09f7d002f5fd0df7d002f5ed01f7e002f5dd05f7e002f5cd09f7e002f5bd0df7e002f5ad01f7f002f59d05f7f002f58d09f7f002f57d0db7f002b56d0203160297ff49aaf76e000208bf86401cbf860018bf8660101208bf8650140f6a120c0f24700804701f0e8be4ff29c56c2f2290664e001313ae0023138e0033136e0043134e0053132e0063130e007312ee008312ce009312ae00a3128e00b3126e00c3124e00d3122e00e3120e00f311ee010311ce011311ae0123118e0133116e0143114e0153112e0163110e017310ee018310ce019310ae01a3108e01b3106e01c3104e01d3102e01e3100e01f315f2921d84ff0520353541dd00a4445235e29537018d04Line truncated
"new": "b0b581ea00039b0701d0034637e0c2b110f0030315d000f1010ca2f1010e0023cc5c9e45c45403f1010404d0634413f003032346f4d121440319121b102a14d302e00346102a10d3d1f800c0d1f804e0cc688d68103adc609d60c3f804e043f810cb0f2a01f11001eed8042a07d300bf51f8045b043a032a43f8045bf8d8002a08bfb0bd0139013b11f8015f013a03f8015ff9d1b0bd00bf884218bf002a00d1704710b50b1a38bf431a934207d288420ad281ea00039b0726d0034669e0fff79fffbde81040704781ea000300eb020c9b0701eb020e42d15fea8c732bd0a0f1010c00bf8b1813f801ec8418531e04f801ec04d0624412f003021a46f2d101eb030e00eb030c17e0830736d0a2f1010c00f1010e002300bfcc5c9c45c45403f1010404d0734413f003032346f4d1121b2144031922e01346042b0ed3aef10402acf1040152f80449043b032b41f80449f8d802f1040e01f1040c1a4653b1aef10101acf1010300bf11f80149013a03f80149f9d1bde8104070470346042a06d351f8044b043a032a43f8044bf8d8002af0d00139013b00bf11f8014f013a03f8014ff9d1e6e700bf2de9f04fc5b04ff690454ef66c6b4ff29c591446cff6ff75ccf2ff0bc2f229094ff6ff78002701260d93cde90e0106e048f2a351c0f24501bff32f8f8847d9f800a0bff35f8fd9f800000321cff680710140b1f1005fbff35f8f03d10168594500f0ac804ff45c7007f0b4f8002800f0eb8129464218373182f8707382f8717382f8727382f8737382f8747382f8757382f8767382f8777382f8787382f8797382f87a7382f87b7382f87c7382f87d7382f87e7382f87f7382f8807382f8817382f8827382f8837382f8847382f8857382f8867382f8877382f8887382f8897382f88a7382f88b7382f88c7382f88d7382f88e7382f88f7382f8907382f8917382f8927382f8937382f8947382f8957382f8967382f8977382f8987382f8997382f89a7382f89b7382f89c7382f89d7382f89e7382f89f7382f8a07382f8a17382f8a27382f8a37382f8a47382f8a57382f8a6738ed1c0f800b08672a0f81c80a0f81e80a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a8059e8001f51457ff445afbff35f8f00bf49e8000121b159e8001f5145f8d039e70f99012641ea04410024c0f8701206e048f2a351c0f24501bff32f8f8847d9f80070bff35f8fd9f800000321cff680710140b1f1005fbff35f8f03d10168594500f0ac804ff45c7006f0e6ff002800f01d8129464218373182f8704382f8714382f8724382f8734382f8744382f8754382f8764382f8774382f8784382f8794382f87a4382f87b4382f87c4382f87d4382f87e4382f87f4382f8804382f8814382f8824382f8834382f8844382f8854382f8864382f8874382f8884382f8894382f88a4382f88b4382f88c4382f88d4382f88e4382f88f4382f8904382f8914382f8924382f8934382f8944382f8954382f8964382f8974382f8984382f8994382f89a4382f89b4382f89c4382f89d4382f89e4382f89f4382f8a04382f8a14382f8a24382f8a34382f8a44382f8a54382f8a6438ed1c0f800b08672a0f81c80a0f81e80a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a8059e8001fb9427ff445afbff35f8f00bf49e8000121b159e8001fb942f8d039e7d0f874a242f6f766c0f24406baf1000fbff35f8f4ad006f19a0250464ff0ff314ff0ff379047002840f08f810f98002861d00e980778a7f1170002285bd80f980c2840f078810e9e7078b178084300f07281f0780138022800f26d81b279f379747a42ea0322337a307942ea0342717942ea0463b27af67a192f42ea062200f07f83172f40f09583002a40f05481fe2800f0cb87812800f0bd87002840f0d28751ea030040f0478101f062b904460020b047e8b104f51d7151e800afbaf1000f0cd1bff35f8f00bf41e80002002a00f0e78051e800afbaf1000ff5d00021bff32f8f0029bff35f8f00f0d680824692e74ff0ff3729e147f6df40c0f24f008047002701261197adf8487006e048f2a351c0f24501bff32f8f8847d9f80040bff35f8fd9f800000321cff680710140b1f1005fbff35f8f03d10168594500f0b3804ff45c7006f09afe002800f0ab8029464218373182f8707382f8717382f8727382f8737382f8747382f8757382f8767382f8777382f8787382f8797382f87a7382f87b7382f87c7382f87d7382f87e7382f87f7382f8807382f8817382f8827382f8837382f8847382f8857382f8867382f8877382f8887382f8897382f88a7382f88b7382f88c7382f88d7382f88e7382f88f7382f8907382f8917382f8927382f8937382f8947382f8957382f8967382f8977382f8987382f8997382f89a7382f89b7382f89c7382f89d7382f89e7382f89f7382f8a07382f8a17382f8a27382f8a37382f8a47382f8a57382f8a6738ed1c0f800b08672a0f81c80a0f81e80a0f82080a0f82280a0f82480a0f82680a0f82880a0f82a80a0f82c80a0f82e80a0f83080a0f83280a0f83480a0f83680a0f83880a0f83a8059e8001fa1427ff445afbff35f8f00bf49e8000159b159e8001fa142f8d039e706f5a9718847bae601211ee700200e9999b10f9a8ab1097801f07f02b2f10b0418bf0124b2f11c0618bf01268a2924dc0b2926d01c2924d03fe00d9010a804f0e1f94ff0ff3701269df84900002810a818bf4ff0ff3704f0fffa054646b99df8480028b94af21f30c0f247004c3080470d98002818bfc56013e09c2901d08b291bd160b14af21f31c0f247010746884797f8641119b997f80413384669b14ff0ff3742f6f760c0f2440000f5807150468847384645b0bde8f08f0d9010a8264004f0a0f90e98007800f07f040f2c2ddd142c40f3a981152c00f01183162c00f01c831d2c40f0da830f9801380228c0f04f850e994878897840ea0124b4f5007f00f2468504eb4400032101eb40000f99884240f03d8505f02ffe002800f038850c96002601270f9400f055bd092c00f3d180052c00f00c82072c40f0af83002706e048f2a351c0f24501bff32f8f8847d9f80040bff35f8fd9f800000321cff680710140b1f1005fbff35f8f03d10168594500f085864ff45c7006f033fd002800f07d86294600bf4218373182f8707382f8717382f8727382f8737382f8747382f8757382f8767382f8777382f8787382f8797382f87a7382f87b7382f87c7382f87d7382f87e7382f87f7382f8807382f8817382f8827382f8837382f8847382f8857382f8867382f8877382f8887382f8897382f88a7382f88b7382f88c7382f88d7382f88e7382f88f7382f8907382f8917382f8927382f8937382f8947382f895738Line truncated
"desc": "append injected blobs to main-app payload"
},
{
"offset": 779123,
"old": "805a3900",
"new": "f00a3a00",
"new": "04403a00",
"desc": "main-app subheader payload size (ps)"
},
{
"offset": 152,
"old": "005b3900",
"new": "700b3a00",
"new": "84403a00",
"desc": "main-app TOC entry size (ps + 128)"
},
{
"offset": 779243,
"old": "805a3904",
"new": "f00a3a04",
"new": "04403a04",
"desc": "main-app preamble length (low 24 bits)"
},
{
"offset": 779247,
"old": "effdbcea",
"new": "09a9afac",
"new": "4c01635d",
"desc": "[5] ota/s200_firmware_ota.bin preamble crc32"
},
{
"offset": 156,
"old": "c0c6322a",
"new": "19292813",
"new": "155cd97f",
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (TOC)"
},
{
"offset": 779127,
"old": "c0c6322a",
"new": "19292813",
"new": "155cd97f",
"desc": "[5] ota/s200_firmware_ota.bin component crc32c (subheader)"
}
]
+6 -4
View File
@@ -1,3 +1,4 @@
import { drawMessages } from './draw-protocol'
import { setBrightness } from './brightness-protocol'
import { AncsClient, ANCS_FIRMWARE_VERSION } from './ancs-client'
import { type CompassEvent } from '../native/compass-types'
@@ -254,7 +255,7 @@ export class GlassesSession {
return Promise.all(['right', 'left'].map(role => this.send(role, protocol.SID.settings, 0x20, protocol.framebufferLease(acquire))))
}
private requestCfw(role: string, payload: Uint8Array): Promise<void> {
if (payload.length > CFW_MAX_MESSAGE) return Promise.reject(new Error('CFW message too large'))
if (payload.length > CFW_MAX_MESSAGE - 16) return Promise.reject(new Error('CFW message too large'))
return new Promise((resolve, reject) => {
const item: CfwPending = { role, payload, checksum: protocol.crc16(payload), magic: this.nextMagic(), ackLenses: 0, retries: 0,
retryPending: false, deadline: 0, resolve, reject }
@@ -508,7 +509,7 @@ export class GlassesSession {
const textureFrame = this.latestTextures; this.latestTextures = null
// Compare with the last enqueued image, not the last ACKed one. In an
// A -> B -> A sequence, B may still be in flight when A is requested.
if (this.lastEnqueued && packed.every((value, i) => value === this.lastEnqueued![i])) continue
if (!textureFrame && this.lastEnqueued && packed.every((value, i) => value === this.lastEnqueued![i])) continue
const texturePlan = textureFrame ? this.textures?.plan(this.lastEnqueued, packed, textureFrame, this.nextImageFrameId) : null
let commands: Uint8Array[]
if (texturePlan) {
@@ -521,9 +522,10 @@ export class GlassesSession {
const delta = buildBoundingBoxPayload(this.lastEnqueued, packed, 640, 480, this.nextImageFrameId)
const payload = delta ?? protocol.concat(new Uint8Array([6]), protocol.rle4(packed))
if (delta) this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
commands = payload.length <= CFW_MAX_MESSAGE ? [payload] : buildFullFrameBands(packed, 640, 480, this.nextImageFrameId)
if (payload.length > CFW_MAX_MESSAGE) for (const _ of commands)
commands = payload.length <= CFW_MAX_MESSAGE - 16 ? [payload] : buildFullFrameBands(packed, 640, 480, this.nextImageFrameId)
if (payload.length > CFW_MAX_MESSAGE - 16) for (const _ of commands)
this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
commands = [...commands.flatMap(command => drawMessages(command)), new Uint8Array([28])]
this.log(`Display ${delta ? 'bbox' : 'full'} (${commands.length} CFW commands)`)
}
this.displaySending = { packed, commands, offset: 0, pending: 0 }
+1 -3
View File
@@ -352,9 +352,7 @@ export class IosPreviewController {
try {
if (this.shellDirty) {
this.shellDirty = false
const { image, draws } = flattenPlanesWithDraws(shell.paintSurface(), { width: 640, height: 480 })
this.compositor.submitSurfaceFrame('shell', image.pixels, { x: 0, y: 0, width: 640, height: 480 }, prepareFrameDraws(draws))
this.compositor.setUnderlayDim(1, shell.underlayDim())
this.compositor.setShellScene(shell.paintScene())
}
const { pixels, textures } = this.compositor.compositeFrame()
this.session?.setFrame(pixels, textures)
+2
View File
@@ -15,6 +15,8 @@ import { GrayImage, type DeferredDraw } from "./image";
* existing incremental-update and screen-preview pipeline.
*/
export type Plane = {
shellKey?: number;
dimUnderneath?: number;
image: GrayImage;
/**
* Offset of the plane's top-left within the submitted frame. Applied
+33
View File
@@ -0,0 +1,33 @@
import { GrayImage } from './image'
import type { Plane } from './plane'
/** Crop one independently owned, opaque shell surface; never split it into tiles. */
export function shellCrop(image: GrayImage, x: number, y: number, width: number, height: number, key: number): Plane {
const baked = image.withDrawsBaked(), crop = new GrayImage(width, height, 0)
for (let row = 0; row < height; row++) crop.pixels.set(baked.pixels.subarray((y + row) * image.width + x, (y + row) * image.width + x + width), row * width)
return { image: crop, x, y, shellKey: key }
}
/** Shared Kotlin receives original 8-bit pixels and quantizes once, before composing. */
export function encodeShellScene(planes: readonly Plane[]): Uint8Array {
const layers: { image: GrayImage; x: number; y: number; key: number; dim: number }[] = []
for (const plane of planes) {
const image = plane.image.withDrawsBaked()
let left = image.width, top = image.height, right = -1, bottom = -1
for (let y = 0; y < image.height; y++) for (let x = 0; x < image.width; x++) {
if (image.pixels[y * image.width + x] === 0) continue
left = Math.min(left, x); right = Math.max(right, x); top = Math.min(top, y); bottom = Math.max(bottom, y)
}
if (right < left) continue
const w = right - left + 1, h = bottom - top + 1
if (5 + Math.ceil(w / 2) * h > 65536) throw new Error(`Shell surface ${plane.shellKey} exceeds 64 KiB (${w}×${h})`)
const cropped = shellCrop(image, left, top, w, h, plane.shellKey!)
layers.push({ image: cropped.image, x: plane.x + left, y: plane.y + top, key: plane.shellKey!, dim: Math.round((plane.dimUnderneath ?? 1) * 256) })
}
const result = new Uint8Array(2 + layers.reduce((n, l) => n + 14 + l.image.pixels.length, 0))
const view = new DataView(result.buffer); let p = 0
const word = (n: number) => { view.setUint16(p, n, true); p += 2 }
word(layers.length)
for (const l of layers) { word(l.key); word(l.x); word(l.y); word(l.image.width); word(l.image.height); word(l.dim); word(0); result.set(l.image.pixels, p); p += l.image.pixels.length }
return result
}
+1
View File
@@ -33,6 +33,7 @@ export class SurfaceCompositor {
this.native.submitDrawsIdDataXYWidthHeightDraws(id, toData(pixels), rect.x, rect.y, rect.width, rect.height,
draws ? toData(new Uint8Array(draws)) : null)
}
setShellScene(bytes: Uint8Array): void { this.native.shellData(toData(bytes)) }
composite(): Uint8Array { return fromData(this.native.composite()) }
compositeFrame(): { pixels: Uint8Array; textures: TextureFrame } {
const native = this.native.compositeFrame()
+19 -2
View File
@@ -9,6 +9,8 @@ type Surface = SurfaceConfiguration & { pixels: Uint8Array; visible: boolean }
export class SurfaceCompositor {
private readonly surfaces = new Map<string, Surface>()
private shellScene: Uint8Array | null = null
setShellScene(bytes: Uint8Array): void { this.shellScene = new Uint8Array(bytes) }
private dimBelow = 0
private dimFactor = 1
private blanked = false
@@ -55,11 +57,11 @@ export class SurfaceCompositor {
composite(): Uint8Array {
const output = new Uint8Array(this.width * this.height)
if (this.blanked) return output
const surfaces = [...this.surfaces.values()].filter(s => s.visible).sort((a, b) => a.zOrder - b.zOrder)
const surfaces = [...this.surfaces.values()].filter(s => s.visible && !(this.shellScene && s === this.surfaces.get('shell'))).sort((a, b) => a.zOrder - b.zOrder)
for (const surface of surfaces) {
const left = Math.max(0, surface.x), right = Math.min(this.width, surface.x + surface.width)
const top = Math.max(0, surface.y), bottom = Math.min(this.height, surface.y + surface.height)
const dim = surface.zOrder < this.dimBelow ? this.dimFactor : 1
const dim = !this.shellScene && surface.zOrder < this.dimBelow ? this.dimFactor : 1
for (let y = top; y < bottom; y++) {
let source = (y - surface.y) * surface.width + left - surface.x
let target = y * this.width + left
@@ -72,6 +74,21 @@ export class SurfaceCompositor {
}
}
}
if (this.shellScene && !surfaces.some(s => s.zOrder > 1)) {
const quantize = (v: number) => Math.min(15, (v + 8) >> 4)
for (let i=0;i<output.length;i++) output[i]=quantize(output[i])*16
const bytes=this.shellScene, view=new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
let p=2
for(let i=0;i<view.getUint16(0,true);i++) {
const x=view.getInt16(p+2,true), y=view.getInt16(p+4,true), w=view.getUint16(p+6,true), h=view.getUint16(p+8,true), dim=view.getUint16(p+10,true)
p+=14
if(dim<256) for(let j=0;j<output.length;j++) output[j]=Math.floor((output[j]/16)*dim/256)*16
for(let yy=0;yy<h;yy++) for(let xx=0;xx<w;xx++) {
if(x+xx>=0 && y+yy>=0 && x+xx<this.width && y+yy<this.height) output[(y+yy)*this.width+x+xx]=quantize(bytes[p+yy*w+xx])*16
}
p+=w*h
}
}
return output
}
}
+5
View File
@@ -541,6 +541,11 @@ export class FaceclawCommunicatorBridge {
* next composite. How a shell overlay's Layer.dimUnderneath reaches the
* window surfaces beneath the shell surface.
*/
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
const snapshot = new Uint8Array(bytes);
await this.enqueueJavaCall(() => this.communicator.submitShellScene(snapshot.buffer, paintMs, frameId), true);
}
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
await this.enqueueJavaCall(() => {
this.communicator.setUnderlayDim(Math.round(belowZOrder), dimFactor256(factor));
+3 -1
View File
@@ -31,6 +31,8 @@ export function runKotlinProtocolSmokeTest(): void {
c.submitSurfaceFrame('test', new Uint8Array([0, 16, 255, 32, 48, 64]), { x: 0, y: 0, width: 3, height: 2 })
assert(hex(c.composite()) === '0010ff203040', 'composition')
assert(hex(protocol.packGray4(c.composite(), 3, 2)) === '01f02340', 'packing')
c.setShellScene(new Uint8Array([1,0,1,0,0,0,0,0,1,0,1,0,128,0,0,0,255]))
assert(hex(c.composite()) === 'f00070101020', 'shell scene bridge and 4bpp composition')
c.setScreenBlanked(true); assert(c.composite().every(p => p === 0), 'blanking')
const textures = new IosTexturePlanner(), textured = new SurfaceCompositor(4, 2)
textured.configureSurface('icon', { x: 0, y: 0, width: 2, height: 2, zOrder: 0, transparency: 'opaque' })
@@ -40,7 +42,7 @@ 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.resourceCommands.some(command => command[0] === 21) && cold.payload[0] === 8, 'texture upload and draw')
assert(!!cold && cold.resourceCommands.some(command => command[0] === 21) && cold.payload[0] === 28, '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)?.resourceCommands.length ?? 0) > 0, 'texture reset')
+5
View File
@@ -22,6 +22,7 @@ export type DisplayTarget = Pick<
| "setUnderlayDim"
| "setScreenBlanked"
| "submitSurfaceFrame"
| "submitShellScene"
| "waitForFrameFinished"
| "getCompositePreview"
| "saveScreenshot"
@@ -97,6 +98,10 @@ export class PreviewDisplayTarget implements DisplayTarget {
this.compositor.setSurfaceVisible(id, Boolean(visible));
}
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
this.compositor.submitShellScene(new Uint8Array(bytes).buffer, paintMs, frameId);
}
async setUnderlayDim(belowZOrder: number, factor: number): Promise<void> {
this.compositor.setUnderlayDim(Math.round(belowZOrder), dimFactor256(factor));
}
+16 -4
View File
@@ -80,6 +80,7 @@ export interface LayerContext {
}
export interface Layer {
paintParts?(): Plane[];
readonly paintOverBase?: boolean;
/**
* Dim everything painted beneath this layer: a 0..1 brightness factor
@@ -116,6 +117,12 @@ export interface Layer {
export class LayerStack {
private readonly layers: Layer[];
private static nextShellKey = 3;
static allocateShellKey(): number {
if (this.nextShellKey > 65535) this.nextShellKey = 3;
return this.nextShellKey++;
}
private readonly shellKeys = new WeakMap<Layer, number>();
private readonly ctx: LayerContext;
private baseWidth: number;
private baseHeight: number;
@@ -225,6 +232,8 @@ export class LayerStack {
* A stack whose base is transparent over something else (the shell chrome
* over the window surfaces) forwards this to what lies beneath.
*/
paintUndimmed(): Plane[] { return this.paintLayer(this.layers.length - 1, 1, true); }
baseDim(): number | false {
return this.lastBaseDim;
}
@@ -259,8 +268,11 @@ export class LayerStack {
/** Paint layer `index`; `dimSoFar` is the factor the layers above apply to it. */
private paintLayer(index: number, dimSoFar: number): Plane[] {
private paintLayer(index: number, dimSoFar: number, raw = false): Plane[] {
const layer = this.layers[index]!;
if (raw && index === 0 && layer.paintParts) return layer.paintParts();
let key = this.shellKeys.get(layer);
if (key === undefined) { key = LayerStack.allocateShellKey(); this.shellKeys.set(layer, key); }
let canvas: GrayImage | null = null;
let belowRequested = false;
const image = spanCurrent(`paint[${index}]:${layer.constructor.name}`, () =>
@@ -272,7 +284,7 @@ export class LayerStack {
return canvas;
}),
);
const ownPlane: Plane = { image, x: 0, y: 0 };
const ownPlane: Plane = { image, x: 0, y: 0, shellKey: key, dimUnderneath: layer.dimUnderneath === false ? 1 : layer.dimUnderneath };
if (index <= 0) {
this.lastBaseDim = dimSoFar;
return [ownPlane];
@@ -281,7 +293,7 @@ export class LayerStack {
return [ownPlane];
}
const dim = layer.dimUnderneath || 1;
const below = this.paintLayer(layer.paintOverBase ? 0 : index - 1, dimSoFar * dim);
return [...(dim < 1 ? dimPlanes(below, dim) : below), ownPlane];
const below = this.paintLayer(layer.paintOverBase ? 0 : index - 1, dimSoFar * dim, raw);
return [...(!raw && dim < 1 ? dimPlanes(below, dim) : below), ownPlane];
}
}
+6 -2
View File
@@ -42,6 +42,7 @@ export type MenuLayout = {
* renders over whatever is below it (modal/window menus).
*/
opaque?: boolean;
squareCorners?: boolean;
};
export type MenuItemRenderArgs = {
@@ -226,6 +227,7 @@ export class MenuLayer implements Layer {
const maxHeight = Math.min(
this.layout.maxHeight ?? image.height - y - DEFAULT_MENU_Y,
image.height - y,
this.layout.squareCorners ? Math.floor(65530 / Math.ceil(width / 2)) : Infinity,
);
const footerHeight = this.layout.footer ? MENU_FOOTER_GAP + lineStep(font) : 0;
const contentHeight = chromeTop + this.items.length * rowHeight + footerHeight + MENU_BODY_PADDING;
@@ -243,9 +245,11 @@ export class MenuLayer implements Layer {
// Fill 1, not 0: identical after 4bpp quantization, but 0 is the
// transparent color key when a menu paints on the shell surface.
image.fillRoundedRect(x, y, width, height, 1);
if (this.layout.squareCorners) image.fillRect(x, y, width, height, 1);
else image.fillRoundedRect(x, y, width, height, 1);
if (this.layout.showBorder !== false) {
image.drawRoundedRect(x, y, width, height, 72);
if (this.layout.squareCorners) image.drawRect(x, y, width, height, 72);
else image.drawRoundedRect(x, y, width, height, 72);
}
if (this.title) {
image.drawText(font, x + 12, y + 8, this.title, 220);
+3 -3
View File
@@ -12,7 +12,7 @@ const DIALOG_X = 40;
const DIALOG_W = G2_LENS_WIDTH - 80;
// The dialog fits inside the min-height window band (like the other shell
// overlays), wherever the vertical position setting puts it.
const DIALOG_MARGIN_Y = 24;
const DIALOG_MARGIN_Y = 28;
const DIALOG_H = MIN_WINDOW_HEIGHT - 2 * DIALOG_MARGIN_Y;
const TEXT_MAX_WIDTH = DIALOG_W - 32;
const MENU_ROWS = 2;
@@ -95,8 +95,8 @@ export class AssistantLayer implements Layer {
// Solid dialog box (fill 1, matching the voice dialog: opaque after 4bpp
// quantization, but not the color-key transparent 0).
image.fillRoundedRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 1, 10);
image.drawRoundedRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 90, 10);
image.fillRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 1);
image.drawRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 90);
const left = DIALOG_X + 16;
image.drawText(font, left, top + 12, this.phase === "thinking" ? "Assistant ●" : "Assistant", 220);
+2 -2
View File
@@ -25,8 +25,8 @@ export class BrightnessPickerLayer implements Layer {
const height = BAR_HEIGHT + 3 * font.lineHeight + 56;
const left = viewport.x + Math.round((viewport.width - DIALOG_WIDTH) / 2);
const top = viewport.y + Math.round((viewport.height - height) / 2);
image.fillRoundedRect(left, top, DIALOG_WIDTH, height, SHELL_OPAQUE_BLACK, 8);
image.drawRoundedRect(left, top, DIALOG_WIDTH, height, 110, 8);
image.fillRect(left, top, DIALOG_WIDTH, height, SHELL_OPAQUE_BLACK);
image.drawRect(left, top, DIALOG_WIDTH, height, 110);
const centeredText = (text: string, y: number, value: number) => {
image.drawText(font, left + Math.round((DIALOG_WIDTH - font.measureText(text)) / 2), y, text, value);
+30 -6
View File
@@ -1,3 +1,5 @@
import { shellCrop } from "../../graphics/shell-scene";
import type { Plane } from "../../graphics/plane";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
import { truncateText } from "../../graphics/textwrap";
@@ -18,7 +20,7 @@ import {
} from "../dashboard-settings";
import { formatClockDate, formatClockTime } from "../clock-format";
import { getFont } from "../../graphics/bdffont";
import { Layer } from "../layers";
import { Layer, LayerStack } from "../layers";
import { scrollToKeepSelectionVisible } from "../menu";
import { lineStep } from "../metrics";
import {
@@ -216,6 +218,21 @@ export class ShellChromeLayer implements Layer {
return image;
}
paintParts(): Plane[] {
const state = this.getState(), parts: Plane[] = [];
if (sidebarStripVisible(state.focus, state.foregroundAppId)) {
const canvas = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
this.drawSidebar(canvas, state);
parts.push(shellCrop(canvas, 0, minWindowTop(state.foregroundAppId), SIDEBAR_WIDTH, MIN_WINDOW_HEIGHT, 1));
}
const canvas = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
this.drawTopBar(canvas, state);
const left = sidebarStripVisible(state.focus, state.foregroundAppId) ? SIDEBAR_WIDTH : 0;
parts.push(shellCrop(canvas, left, windowTop(state.foregroundHeightMode, state.foregroundAppId), G2_LENS_WIDTH-left, TOP_BAR_HEIGHT, 2));
drawAmbientCards(canvas, parts);
return parts;
}
handleInput(): void {
// Shell input is handled by the shell state machine before it reaches the
// layer stack; the chrome itself never consumes events.
@@ -500,7 +517,8 @@ const AMBIENT_MAX_LINES_PER_CARD = 3;
* bottom of the window band and newer ones stack above it. Cards that would
* cross into the top bar are dropped rather than clipped.
*/
function drawAmbientCards(image: GrayImage): void {
const ambientKeys = new Map<string, number>();
function drawAmbientCards(image: GrayImage, parts?: Plane[]): void {
const cards = activeAmbientCards();
if (!cards.length) return;
const font = getDefaultSmallFont();
@@ -514,10 +532,11 @@ function drawAmbientCards(image: GrayImage): void {
const height = 2 * AMBIENT_CARD_PADDING_Y + lines.length * lineStep(font);
const y = bottom - height;
if (y < bandTop + TOP_BAR_HEIGHT) break;
image.fillRoundedRect(x, y, AMBIENT_CARD_WIDTH, height, SHELL_OPAQUE_BLACK, 6);
image.drawRoundedRect(x, y, AMBIENT_CARD_WIDTH, height, 90, 6);
const target = parts ? new GrayImage(image.width, image.height, 0) : image;
target.fillRect(x, y, AMBIENT_CARD_WIDTH, height, SHELL_OPAQUE_BLACK);
target.drawRect(x, y, AMBIENT_CARD_WIDTH, height, 90);
for (let index = 0; index < lines.length; index++) {
image.drawText(
target.drawText(
font,
x + AMBIENT_CARD_PADDING_X,
y + AMBIENT_CARD_PADDING_Y + index * lineStep(font),
@@ -525,6 +544,11 @@ function drawAmbientCards(image: GrayImage): void {
index === 0 ? 220 : 160,
);
}
if (parts) {
let key = ambientKeys.get(card.id);
if (key === undefined) { key = LayerStack.allocateShellKey(); ambientKeys.set(card.id, key); }
parts.push(shellCrop(target, x, y, AMBIENT_CARD_WIDTH, height, key));
}
bottom = y - AMBIENT_CARD_GAP;
}
}
@@ -557,7 +581,7 @@ function attentionDot(value: number): GrayImage {
const TAB_RADIUS = 6;
// How far the diversion pokes past the separator into the main area.
const TAB_EXTEND = 1;
const TAB_EXTEND = 0;
const TAB_STROKE = 150;
/**
+3 -3
View File
@@ -16,7 +16,7 @@ const DIALOG_X = 40;
const DIALOG_W = G2_LENS_WIDTH - 80;
// The dialog fits inside the min-height window band (like the other shell
// overlays), wherever the vertical position setting puts it.
const DIALOG_MARGIN_Y = 24;
const DIALOG_MARGIN_Y = 28;
const DIALOG_H = MIN_WINDOW_HEIGHT - 2 * DIALOG_MARGIN_Y;
const TEXT_MAX_WIDTH = DIALOG_W - 32;
@@ -51,8 +51,8 @@ export function paintInputDialog(image: GrayImage, content: InputDialogContent):
// Solid dialog box over the underlying UI. Fill 1, not 0: identical after
// 4bpp quantization, but 0 is transparent on the color-key shell surface.
image.fillRoundedRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 1, 10);
image.drawRoundedRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 90, 10);
image.fillRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 1);
image.drawRect(DIALOG_X, top, DIALOG_W, DIALOG_H, 90);
const left = DIALOG_X + 16;
image.drawText(font, left, top + 12, content.title, 220);
+4 -4
View File
@@ -16,7 +16,7 @@ function modalInterior(): { width: number; height: number } {
const viewport = appViewportSize("min");
return {
width: viewport.width - 2 * MODAL_MARGIN - 2 * MODAL_PADDING,
height: viewport.height - 2 * MODAL_MARGIN - 2 * MODAL_PADDING,
height: Math.min(viewport.height - 2 * MODAL_MARGIN, Math.floor(65530 / Math.ceil((viewport.width - 2 * MODAL_MARGIN) / 2))) - 2 * MODAL_PADDING,
};
}
@@ -27,7 +27,7 @@ export function modalRect(): { x: number; y: number; width: number; height: numb
x: viewport.x + MODAL_MARGIN,
y: viewport.y + MODAL_MARGIN,
width: viewport.width - 2 * MODAL_MARGIN,
height: viewport.height - 2 * MODAL_MARGIN,
height: Math.min(viewport.height - 2 * MODAL_MARGIN, Math.floor(65530 / Math.ceil((viewport.width - 2 * MODAL_MARGIN) / 2))),
};
}
@@ -54,8 +54,8 @@ export class ShellModalLayer implements Layer {
// below transplants the finished modal content into this layer's plane.
const inner = flattenPlanes(this.stack.paint(), interior);
const rect = modalRect();
image.fillRoundedRect(rect.x, rect.y, rect.width, rect.height, SHELL_OPAQUE_BLACK, 8);
image.drawRoundedRect(rect.x, rect.y, rect.width, rect.height, 110, 8);
image.fillRect(rect.x, rect.y, rect.width, rect.height, SHELL_OPAQUE_BLACK);
image.drawRect(rect.x, rect.y, rect.width, rect.height, 110);
image.bitBlt(inner, rect.x + MODAL_PADDING, rect.y + MODAL_PADDING, { transparentZero: true });
return image;
}
+6 -2
View File
@@ -1,3 +1,4 @@
import { encodeShellScene } from "../../graphics/shell-scene";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image";
import { singlePlane, type Plane } from "../../graphics/plane";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
@@ -229,6 +230,7 @@ class ShellOverlayMenuLayer extends MenuLayer {
y: minWindowTop() + TOP_BAR_HEIGHT + 8,
width,
minHeight: 150,
squareCorners: true,
footer,
dimUnderneath: CONTEXT_MENU_DIM,
});
@@ -266,8 +268,8 @@ class ShellAlertLayer implements Layer {
const font = getDefaultSmallFont();
// Positioned within the min-height window band, like the other shell overlays.
const alertY = minWindowTop() + ALERT_Y;
image.fillRoundedRect(ALERT_X, alertY, ALERT_W, ALERT_H, 1, 10);
image.drawRoundedRect(ALERT_X, alertY, ALERT_W, ALERT_H, 90, 10);
image.fillRect(ALERT_X, alertY, ALERT_W, ALERT_H, 1);
image.drawRect(ALERT_X, alertY, ALERT_W, ALERT_H, 90);
image.drawText(font, ALERT_X + 16, alertY + 12, "Assistant", 200);
image.drawTextWrapped({
font,
@@ -690,6 +692,8 @@ class Shell {
}
/** Paint the shell surface: transparent chrome, or all-transparent when asleep. */
paintScene(): Uint8Array { return encodeShellScene(this.screenOn ? this.stack.paintUndimmed() : []); }
paintSurface(): Plane[] {
if (!this.screenOn) {
return singlePlane(new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0));
+4 -3
View File
@@ -19,7 +19,7 @@ import { lineStep } from "../metrics";
const DIALOG_WIDTH = 420;
// Shell overlays align to the min-height window band; the detail box fills
// the band below the top bar with an 8px margin on each side.
const DETAIL_HEIGHT = MIN_WINDOW_HEIGHT - TOP_BAR_HEIGHT - 16;
const DETAIL_HEIGHT = 208;
/** Dialog left edge: past the sidebar strip where one reserves width. */
function dialogX(): number {
@@ -42,6 +42,7 @@ export class ToolDebugMenuLayer extends MenuLayer {
x: dialogX(),
y: dialogY(),
width: DIALOG_WIDTH,
squareCorners: true,
minHeight: 0,
});
}
@@ -89,8 +90,8 @@ class ToolDetailLayer implements Layer {
const left = dialogX();
const width = detailWidth();
const top = dialogY();
image.fillRoundedRect(left, top, width, DETAIL_HEIGHT, 1, 10);
image.drawRoundedRect(left, top, width, DETAIL_HEIGHT, 72, 10);
image.fillRect(left, top, width, DETAIL_HEIGHT, 1);
image.drawRect(left, top, width, DETAIL_HEIGHT, 72);
const textX = left + DETAIL_PADDING;
const textWidth = width - 2 * DETAIL_PADDING - 8;
@@ -0,0 +1,90 @@
package com.faceclaw.app
/** Revision 25 wire grammar, shared by scene planning and the local renderer. */
object DrawProtocol {
const val DRAW = 26
const val ROOT = 27
const val PRESENT = 28
const val CREATE = 29
const val IMAGE = 0
const val FONT = 1
const val LIST = 2
const val LARGE = 4
const val RLE = 8
const val SCREEN = 65535
const val CURRENT = 65534
fun u16(b: ByteArray, p: Int): Int = (b[p].toInt() and 255) or ((b[p + 1].toInt() and 255) shl 8)
fun word(n: Int) = byteArrayOf(n.toByte(), (n ushr 8).toByte())
fun call(op: Int, args: ByteArray, target: Int? = null): ByteArray =
byteArrayOf(op.toByte(), if (target == null) 0 else 1) + (target?.let { word(it) } ?: byteArrayOf()) + args
fun sequence(calls: List<ByteArray>): ByteArray {
require(calls.size <= 4096)
val out = ByteSink(); out.write(word(calls.size))
for (call in calls) { require(call.size <= 65535); out.write(word(call.size)); out.write(call) }
return out.toByteArray()
}
fun message(calls: List<ByteArray>) = byteArrayOf(DRAW.toByte()) + sequence(calls)
fun displayList(calls: List<ByteArray>) = byteArrayOf(LIST.toByte()) + sequence(calls)
fun root(id: Int) = byteArrayOf(ROOT.toByte()) + word(id)
fun image(id: Int, x: Int, y: Int, options: Int = 15, target: Int? = null) =
call(4, word(id) + word(x) + word(y) + byteArrayOf(options.toByte()), target)
fun lut(width: Int, height: Int, factor: Int): ByteArray {
val table = ByteArray(8) { i ->
(((i * 2 * factor / 256).coerceIn(0, 15) shl 4) or ((i * 2 + 1) * factor / 256).coerceIn(0, 15)).toByte()
}
return call(6, word(0) + word(0) + word(width) + word(height) + table)
}
fun rawImage(width: Int, height: Int, pixels: ByteArray = ByteArray(((width + 1) / 2) * height)): ByteArray {
require(width in 1..640 && height in 1..480 && pixels.size == ((width + 1) / 2) * height)
val result = byteArrayOf(LARGE.toByte()) + word(width) + word(height) + pixels
require(result.size <= ResourceCacheState.MAX_RESOURCE_SIZE)
return result
}
/** Packed rows -> RLE of exactly width*height pixels, omitting odd-row padding. */
fun bbox(packed: ByteArray, stride: Int, x: Int, y: Int, width: Int, height: Int, target: Int? = null): ByteArray {
val compact = x % 4 == 0 && y % 2 == 0 && width % 4 == 0 && height % 2 == 0 && x / 4 < 256 && y / 2 < 256 && width / 4 < 256 && height / 2 < 256
val header = if (compact) byteArrayOf(0, (x / 4).toByte(), (y / 2).toByte(), (width / 4).toByte(), (height / 2).toByte())
else byteArrayOf(1) + word(x) + word(y) + word(width) + word(height)
val out = ByteSink(); out.write(header)
var color = -1; var count = 0
fun flush() {
if (count == 0) return
if (count <= 15) out.write((count shl 4) or color)
else { out.write(color); if (count <= 255) out.write(count) else { out.write(0); out.write(word(count)) } }
}
for (yy in y until y + height) for (xx in x until x + width) {
val value = (packed[yy * stride + xx / 2].toInt() ushr (if (xx % 2 == 0) 4 else 0)) and 15
if (value != color || count == 65535) { flush(); color = value; count = 0 }
count++
}
flush()
return call(1, out.toByteArray(), target)
}
/** Internal optimizer formats are translated here; they are never sent to revision 25. */
fun fromOptimized(payload: ByteArray, width: Int = 640, height: Int = 480): List<ByteArray> {
return when (val mode = payload[0].toInt() and 127) {
8 -> {
val result = ArrayList<ByteArray>(); var pos = 2
repeat(payload[1].toInt() and 255) { val n = u16(payload, pos); pos += 2; result.addAll(fromOptimized(payload.copyOfRange(pos, pos + n), width, height)); pos += n }
require(pos == payload.size); result
}
3 -> listOf(call(1, byteArrayOf(0) + payload.copyOfRange(1, 5) + payload.copyOfRange(7, payload.size)))
6 -> listOf(call(1, byteArrayOf(1) + word(0) + word(0) + word(width) + word(height) + payload.copyOfRange(1, payload.size)))
15 -> listOf(call(3, payload.copyOfRange(1, payload.size)))
19 -> listOf(call(4, payload.copyOfRange(1, payload.size)))
20 -> listOf(call(5, payload.copyOfRange(1, payload.size)))
9 -> listOf(call(2, word(CURRENT) + payload.copyOfRange(1, 9) + payload.copyOfRange(9, 13)))
else -> error("Unsupported internal draw format $mode")
}
}
fun messages(calls: List<ByteArray>, maxBytes: Int = 65535): List<ByteArray> {
val result = ArrayList<ByteArray>(); var batch = ArrayList<ByteArray>(); var size = 3
for (call in calls) {
require(call.size + 5 <= maxBytes)
if (size + 2 + call.size > maxBytes) { result.add(message(batch)); batch = ArrayList(); size = 3 }
batch.add(call); size += call.size + 2
}
if (batch.isNotEmpty()) result.add(message(batch))
return result
}
}
@@ -1,14 +1,16 @@
package com.faceclaw.app
import kotlin.jvm.JvmOverloads
/** Immutable content-addressed bytes; equality verifies bytes when hashes collide. */
class CachedResource(bytes: ByteArray) {
class CachedResource @JvmOverloads constructor(bytes: ByteArray, val owner: String? = null) {
val bytes: ByteArray = bytes.copyOf()
val hash: Long = this.bytes.fold(-3750763034362895579L) { hash, byte ->
(hash xor (byte.toLong() and 255)) * 1099511628211L
}
override fun hashCode(): Int = (hash xor (hash ushr 32)).toInt()
override fun hashCode(): Int = owner?.hashCode() ?: (hash xor (hash ushr 32)).toInt()
override fun equals(other: Any?): Boolean = this === other ||
other is CachedResource && hash == other.hash && bytes.contentEquals(other.bytes)
other is CachedResource && owner == other.owner && hash == other.hash && bytes.contentEquals(other.bytes)
}
/**
@@ -18,7 +20,7 @@ class CachedResource(bytes: ByteArray) {
*/
class ResourceCacheState {
companion object {
const val CACHE_SIZE = 262144
const val CACHE_SIZE = 196608
const val RESOURCE_COUNT = 512
const val TABLE_BYTES = RESOURCE_COUNT * 4
const val ARENA_BYTES = CACHE_SIZE - TABLE_BYTES
@@ -32,6 +34,10 @@ class ResourceCacheState {
private data class Resident(val id: Int, val resource: CachedResource, var lastUsed: Long)
private val residents = LinkedHashMap<CachedResource, Resident>()
private val occupied = BooleanArray(RESOURCE_COUNT)
var generation = 0
private set
private var pinned = emptySet<CachedResource>()
fun pin(resources: Collection<CachedResource>) { pinned = resources.toSet() }
private var allocated = 0
private var clock = 0L
private var resetRequired = true
@@ -40,6 +46,8 @@ class ResourceCacheState {
/** Forget uncertain device state. The next upload first evicts every ID on both lenses. */
fun reset() {
generation++
pinned = emptySet()
residents.clear(); occupied.fill(false); allocated = 0; clock = 0
evictions.clear(); uploads.clear(); resetRequired = true
}
@@ -52,6 +60,7 @@ class ResourceCacheState {
fun prepare(resources: List<CachedResource>): IntArray {
check(uploads.isEmpty() && evictions.isEmpty()) { "Previous resource commands have not been drained" }
val protected = HashSet<Int>()
for (resource in pinned) residents[resource]?.let { protected.add(it.id) }
for (resource in resources) residents[resource]?.let { protected.add(it.id) }
return IntArray(resources.size) { index ->
val resource = resources[index]
@@ -126,6 +135,11 @@ class ResourceCacheState {
}
for (entry in uploads) {
val data = entry.resource.bytes
if (entry.resource.owner != null) {
flush()
result.add(byteArrayOf(DrawProtocol.CREATE.toByte(), 1, 0) + DrawProtocol.word(entry.id) + data.copyOfRange(1, 5))
continue
}
var offset = 0
while (offset < data.size) {
val count = minOf(data.size - offset, maxPayloadBytes - 13)
@@ -0,0 +1,77 @@
package com.faceclaw.app
import kotlin.jvm.JvmOverloads
/** The delta base contains only app pixels. Shell resources are composed at presentation. */
class ScenePlanner(private val cache: ResourceCacheState) {
class Plan(val commands: List<ByteArray>, val nextFid: Int)
private val owned = HashMap<String, CachedResource>()
private val pixels = HashMap<String, ByteArray>()
private var previous: ByteArray? = null
private var screenWidth = 0
private var screenHeight = 0
private var epoch = -1
private var root = -1
@JvmOverloads
fun plan(screen: ByteArray, width: Int, height: Int, draws: Array<SurfaceCompositor.ScreenDraw>?, scene: ShellScene, firstId: Int, textures: Boolean = true, incremental: Boolean = true, multiRect: Boolean = true, maxRects: Int = 6): Plan {
require(width in 1..640 && height in 1..480)
if (epoch != cache.generation) { previous = null; pixels.clear(); root = -1; epoch = cache.generation }
require(screen.size == ((width + 1) / 2) * height)
if (width != screenWidth || height != screenHeight) previous = null
screenWidth = width; screenHeight = height
val commands = ArrayList<ByteArray>()
val keys = scene.layers.map { "shell:${it.key}:${it.width}x${it.height}" }
val surfaces = scene.layers.mapIndexed { i, layer -> owned.getOrPut(keys[i]) {
CachedResource(DrawProtocol.rawImage(layer.width, layer.height), keys[i])
} }
owned.keys.retainAll(keys.toSet()); pixels.keys.retainAll(keys.toSet())
cache.pin(surfaces)
val absent = surfaces.map { cache.resourceId(it) < 0 }
val ids = cache.prepare(surfaces)
check(ids.all { it >= 0 }) { "Shell surfaces exceed the resource budget" }
commands.addAll(cache.drainCommands(3600))
val rootCalls = ArrayList<ByteArray>()
for ((i, layer) in scene.layers.withIndex()) {
val old = if (absent[i]) null else pixels[keys[i]]
commands.addAll(DrawProtocol.messages(pixelCalls(old, layer.packed, layer.width, layer.height, ids[i])))
pixels[keys[i]] = layer.packed
if (layer.dim < 256) rootCalls.add(DrawProtocol.lut(width, height, layer.dim))
rootCalls.add(DrawProtocol.image(ids[i], layer.x, layer.y))
}
val base = if (incremental) previous else null
val texture = if (textures) TexturePlanner.plan(base, screen, width, height, draws, cache, firstId, multiRect, maxRects) else null
if (texture != null) {
commands.addAll(texture.resourceCommands)
commands.addAll(DrawProtocol.messages(DrawProtocol.fromOptimized(texture.payload, width, height)))
} else commands.addAll(DrawProtocol.messages(pixelCalls(base, screen, width, height, null)))
val list = CachedResource(DrawProtocol.displayList(rootCalls))
val rootId = cache.prepare(listOf(list))[0]
check(rootId >= 0) { "No space for root display list" }
commands.addAll(cache.drainCommands(3600))
cache.pin(surfaces + list)
val evicted = commands.any { it[0].toInt() == ResourceCacheState.EVICT_MODE }
if (evicted) commands.add(0, DrawProtocol.root(DrawProtocol.SCREEN))
if (root != rootId || evicted) commands.add(DrawProtocol.root(rootId))
commands.add(byteArrayOf(DrawProtocol.PRESENT.toByte()))
root = rootId; previous = screen.copyOf()
return Plan(commands, texture?.nextFid ?: firstId)
}
private fun pixelCalls(old: ByteArray?, next: ByteArray, width: Int, height: Int, target: Int?): List<ByteArray> {
val stride = (width + 1) / 2
if (old != null && old.contentEquals(next)) return emptyList()
val calls = ArrayList<ByteArray>()
for (top in 0 until height step 32) {
val rows = minOf(32, height - top)
if (old != null && (top * stride until (top + rows) * stride).all { old[it] == next[it] }) continue
var left = width; var right = -1
for (y in top until top + rows) for (x in 0 until width) {
val p = y * stride + x / 2; val shift = if (x % 2 == 0) 4 else 0
if (old == null || (((old[p].toInt() xor next[p].toInt()) ushr shift) and 15) != 0) { left = minOf(left, x); right = maxOf(right, x) }
}
if (right < left) continue
if (width % 4 == 0 && rows % 2 == 0) { left = left / 4 * 4; right = minOf(width, ((right + 4) / 4) * 4) - 1 }
calls.add(DrawProtocol.bbox(next, stride, left, top, right - left + 1, rows, target))
}
return calls
}
}
@@ -243,7 +243,7 @@ class TexturePlanner {
subs.addAll(runs)
subs.addAll(fwSubs)
var payload: ByteArray = assembleMode8(subs)
if (payload.size > CfwTransport.MAX_MESSAGE) {
if (payload.size > CfwTransport.MAX_MESSAGE - 16) {
rollback()
return null
}
@@ -0,0 +1,144 @@
package com.faceclaw.app
/** Reference interpreter. It uses the firmware's packed rows, clipping, LUT and call grammar. */
class DisplayListRenderer(private val resources: Map<Int, ByteArray>, private val builtin: ((Int, Int) -> BuiltinGlyph)? = null) {
class BuiltinGlyph(val image: ByteArray, val advance: Int, val x: Int = 0, val y: Int = 0)
class Target(val bytes: ByteArray, val width: Int, val height: Int, val offset: Int = 0) {
val stride = (width + 1) / 2
init { require(width > 0 && height > 0 && offset >= 0 && bytes.size - offset >= stride * height) }
fun get(x: Int, y: Int) = (bytes[offset + y * stride + x / 2].toInt() ushr (if (x % 2 == 0) 4 else 0)) and 15
fun put(x: Int, y: Int, v: Int) {
if (x !in 0 until width || y !in 0 until height) return
val index = offset + y * stride + x / 2; val old = bytes[index].toInt()
bytes[index] = (if (x % 2 == 0) (old and 15) or (v shl 4) else (old and 240) or v).toByte()
}
}
private class Image(val width: Int, val height: Int, val pixels: IntArray)
private fun word(b: ByteArray, p: Int): Int { require(p >= 0 && p + 2 <= b.size); return DrawProtocol.u16(b, p) }
private fun signed(b: ByteArray, p: Int) = word(b, p).toShort().toInt()
private fun resource(id: Int) = requireNotNull(resources[id]) { "Missing resource $id" }
private fun decodeRle(b: ByteArray, start: Int, count: Int, exact: Boolean): IntArray {
var p = start; var i = 0; val result = IntArray(count)
while (i < count) {
require(p < b.size); val token = b[p++].toInt() and 255; var n = token ushr 4
if (n == 0) { require(p < b.size); n = b[p++].toInt() and 255
if (n == 0) { n = word(b, p); p += 2 } }
require(n > 0 && n <= count - i); result.fill(token and 15, i, i + n); i += n
}
require(!exact || p == b.size)
return result
}
private fun image(b: ByteArray, start: Int = 0): Image {
require(start in b.indices); val flags = b[start].toInt() and 255
require(flags and 12 == flags)
val header = if (flags and 4 != 0) 5 else 3
require(b.size - start >= header)
val w = if (header == 5) word(b, start + 1) else b[start + 1].toInt() and 255
val h = if (header == 5) word(b, start + 3) else b[start + 2].toInt() and 255
require(w in 1..640 && h in 1..480)
val pixels = if (flags and 8 != 0) decodeRle(b, start + header, w * h, false)
else { val t = Target(b, w, h, start + header); IntArray(w * h) { t.get(it % w, it / w) } }
return Image(w, h, pixels)
}
private fun target(id: Int): Target {
val b = resource(id); require(b.isNotEmpty() && b[0].toInt() and 11 == 0)
image(b) // Validate the complete raw image before exposing a write target.
return if (b[0].toInt() and 4 != 0) Target(b, word(b, 1), word(b, 3), 5)
else Target(b, b[1].toInt() and 255, b[2].toInt() and 255, 3)
}
private fun draw(image: Image, target: Target, x: Int, y: Int, options: Int, apply: Boolean) {
if (!apply) return
for (i in image.pixels.indices) {
val v = image.pixels[i]; if (v == 0 && options and 16 != 0) continue
val source = if (options and 32 != 0) 15 - v else v
target.put(x + i % image.width, y + i / image.width, source * (options and 15) / 15)
}
}
/** Validate the whole graph before changing any pixels. Resource writes remain call-scoped. */
fun render(root: Int, screen: Target, composition: Target): Set<Int> {
val references = mutableSetOf<Int>()
for (apply in listOf(false, true)) {
if (apply) for (y in 0 until screen.height) for (x in 0 until screen.width) composition.put(x, y, screen.get(x, y))
if (root != DrawProtocol.SCREEN) executeList(root, composition, screen, apply, mutableListOf(), intArrayOf(4096), references)
}
return references
}
fun execute(sequence: ByteArray, target: Target, screen: Target = target): Set<Int> {
val references = mutableSetOf<Int>()
for (apply in listOf(false, true)) executeSequence(sequence, target, screen, apply, mutableListOf(), intArrayOf(4096), references)
return references
}
private fun executeList(id: Int, t: Target, screen: Target, apply: Boolean, stack: MutableList<Int>, budget: IntArray, refs: MutableSet<Int>) {
require(stack.size < 8 && id !in stack); val b = resource(id); require(b.size >= 3 && b[0].toInt() == 2)
refs.add(id); stack.add(id); executeSequence(b.copyOfRange(1, b.size), t, screen, apply, stack, budget, refs); stack.removeAt(stack.lastIndex)
}
private fun executeSequence(b: ByteArray, inherited: Target, screen: Target, apply: Boolean, stack: MutableList<Int>, budget: IntArray, refs: MutableSet<Int>) {
val count = word(b, 0); var pos = 2
repeat(count) {
val n = word(b, pos); pos += 2; require(n >= 2 && n <= b.size - pos && budget[0]-- > 0)
val call = b.copyOfRange(pos, pos + n); pos += n
val flags = call[1].toInt() and 255; require(flags <= 1)
var t = inherited; var header = 2
if (flags == 1) { val id = word(call, 2); refs.add(id); t = target(id); header += 2 }
val p = call.copyOfRange(header, call.size)
when (call[0].toInt()) {
1 -> {
require(p.isNotEmpty() && p[0].toInt() in 0..1); val wide = p[0].toInt() == 1
require(p.size >= if (wide) 9 else 5)
val x = if (wide) word(p, 1) else (p[1].toInt() and 255) * 4
val y = if (wide) word(p, 3) else (p[2].toInt() and 255) * 2
val w = if (wide) word(p, 5) else (p[3].toInt() and 255) * 4
val h = if (wide) word(p, 7) else (p[4].toInt() and 255) * 2
require(w > 0 && h > 0 && x + w <= t.width && y + h <= t.height)
val pixels = decodeRle(p, if (wide) 9 else 5, w * h, true)
if (apply) for (i in pixels.indices) t.put(x + i % w, y + i / w, pixels[i])
}
2 -> {
require(p.size == 14); val id = word(p, 0)
val src = when(id) { DrawProtocol.SCREEN -> screen; DrawProtocol.CURRENT -> t; else -> { refs.add(id); target(id) } }
val x = word(p, 2); val y = word(p, 4); val w = word(p, 6); val h = word(p, 8)
require(w > 0 && h > 0 && x + w <= src.width && y + h <= src.height)
val dx = signed(p, 10); val dy = signed(p, 12)
if (apply) { val copy = IntArray(w * h) { src.get(x + it % w, y + it / w) }
for (i in copy.indices) t.put(dx + i % w, dy + i / w, copy[i]) }
}
3 -> {
require(p.size >= 6 && p.size == 6 + (p[5].toInt() and 255))
val text = p.copyOfRange(6, p.size).decodeToString(throwOnInvalidSequence = true)
val cps = ArrayList<Int>(); var i = 0
while (i < text.length) { val c = text[i++].code
cps.add(if (c in 0xd800..0xdbff) { require(i < text.length); 0x10000 + ((c - 0xd800) shl 10) + text[i++].code - 0xdc00 } else c) }
var x = signed(p, 0); val y = signed(p, 2)
for (j in cps.indices) { val cp = cps[j]
if (cp in 1..31) { x += cp - 11; continue }
val next = cps.drop(j + 1).firstOrNull { it !in 1..31 } ?: 0
val g = requireNotNull(builtin) { "Builtin font provider required" }(cp, next)
draw(image(g.image), t, x + g.x, y + g.y, p[4].toInt() and 255, apply); x += g.advance
}
}
4, 5 -> {
val font = call[0].toInt() == 5; require(if(font) p.size >= 8 && p.size == 8 + (p[7].toInt() and 255) else p.size == 7)
val id = word(p, 0); refs.add(id); val data = resource(id)
var x = signed(p, 2); val y = signed(p, 4); val options = p[6].toInt() and 255
if (!font) draw(image(data), t, x, y, options, apply)
else { require(data.size >= 193 && data[0].toInt() == 1)
for (j in 8 until p.size) { val ch = p[j].toInt() and 255
if(ch in 1..31) { x += ch - 11; continue }; require(ch in 32..127)
val offset = word(data, 1 + (ch - 32) * 2); require(offset >= 193)
val glyph = image(data, offset); draw(glyph, t, x, y, options, apply); x += glyph.width
}
}
}
6 -> {
require(p.size == 16); val x = word(p, 0); val y = word(p, 2); val w = word(p, 4); val h = word(p, 6)
require(w > 0 && h > 0 && x + w <= t.width && y + h <= t.height)
if (apply) for(yy in y until y + h) for(xx in x until x + w) { val v = t.get(xx, yy)
t.put(xx, yy, (p[8 + v / 2].toInt() ushr (if(v % 2 == 0) 4 else 0)) and 15) }
}
7 -> { require(p.size == 2); executeList(word(p, 0), t, screen, apply, stack, budget, refs) }
else -> error("Unknown draw opcode")
}
}
require(pos == b.size)
}
}
@@ -13,7 +13,7 @@ class FontResourceAtlas {
val old = fonts[fontId]
if (old != null && old.encodings.containsAll(requested)) return old
val glyphs = HashMap<Int, GlyphAtlas.Glyph>()
var size = 192
var size = 193
// Current-frame glyphs take precedence if the entire font cannot fit.
for (encoding in requested.sorted() + (old?.encodings ?: emptySet()).sorted()) {
if (encoding !in 32..127 || glyphs.containsKey(encoding)) continue
@@ -22,10 +22,11 @@ class FontResourceAtlas {
glyphs[encoding] = glyph; size += glyph.cachedBytes.size
}
val bytes = ByteArray(size)
var offset = 192
bytes[0] = DrawProtocol.FONT.toByte()
var offset = 193
for (encoding in glyphs.keys.sorted()) {
val glyph = glyphs.getValue(encoding)
val slot = (encoding - 32) * 2
val slot = 1 + (encoding - 32) * 2
bytes[slot] = offset.toByte(); bytes[slot + 1] = (offset ushr 8).toByte()
glyph.cachedBytes.copyInto(bytes, offset)
offset += glyph.cachedBytes.size
@@ -10,8 +10,8 @@ import kotlin.jvm.JvmStatic
*
* - as the ink mask for the "would this draw land correctly" check against the composited frame,
* - to punch the ink pixels out of the baked delta rect it replaces,
* - pre-encoded as the CFW cached-image bytes ([w][h][4bpp RLE]) uploaded via mode 12 and drawn via
* mode 14 with a top-color LUT. 1bpp (BDF) glyphs store ink at 15 so the LUT maps them to exactly
* - pre-encoded as the CFW cached-image bytes ([flags=RLE][w][h][4bpp RLE]) uploaded as resources and drawn via
* the resource-text draw call with a top-color LUT. 1bpp (BDF) glyphs store ink at 15 so the LUT maps them to exactly
* the requested level; AA (TTF) glyphs store true coverage nibbles the LUT scales.
*
* Fonts are identified by a stable string key (the embedded font name), NOT a per-JS-context
@@ -232,7 +232,7 @@ class GlyphAtlas {
/** AA coverage, width*inkHeight nibble values 0..15; null for 1bpp glyphs. */
@JvmField val coverage: ByteArray?
/** CFW cached-image bytes: [w][cellHeight][RLE(w*cellHeight px)]. */
/** CFW cached-image bytes: [flags=RLE][w][cellHeight][RLE(w*cellHeight px)]. */
@JvmField val cachedBytes: ByteArray
constructor(
@@ -280,16 +280,17 @@ class GlyphAtlas {
}
/**
* The firmware cached-image encoding: [width][height][RLE tokens] covering exactly
* The firmware cached-image encoding: [flags=RLE][width][height][RLE tokens] covering exactly
* width*cellHeight pixels (no row padding), each pixel's stored color being its nibbleAt
* value.
*/
private fun encodeCachedImage(): ByteArray {
var total: Int = (width * cellHeight)
var out: ByteArray = ByteArray((2 + total))
out[0] = (width).toByte()
out[1] = (cellHeight).toByte()
var o: Int = 2
var out: ByteArray = ByteArray((3 + total))
out[0] = DrawProtocol.RLE.toByte()
out[1] = (width).toByte()
out[2] = (cellHeight).toByte()
var o: Int = 3
var i: Int = 0
while ((i < total)) {
var color: Int = nibbleAt((i % width), (i / width))
@@ -11,7 +11,7 @@ import kotlin.jvm.JvmStatic
*
* Unlike glyphs (1-bit ink recolored at draw time), images keep their exact 4bpp values: the cached
* bytes quantize the registered 8bpp pixels with the same GRAY_TO_NIBBLE table the composite is
* packed with, and the planner draws them via mode 13 with an identity LUT (top color 15) and the
* packed with, and the planner draws them via the image draw call with an identity LUT (top color 15) and the
* transparent bit — the draw writes exactly the nonzero-nibble pixels, which is what the
* eligibility check and hole punching are defined over.
*/
@@ -78,7 +78,7 @@ class ImageAtlas {
/** One 4bpp value per pixel, row-major. */
@JvmField val nibbles: ByteArray
/** CFW cached-image bytes: [w][h][RLE(w*h pixels)]. */
/** CFW cached-image bytes: [flags=RLE][w][h][RLE(w*h pixels)]. */
@JvmField val cachedBytes: ByteArray
val resource: CachedResource
@@ -98,10 +98,11 @@ class ImageAtlas {
private fun encodeCachedImage(): ByteArray {
var total: Int = (width * height)
var out: ByteArray = ByteArray((2 + total))
out[0] = (width).toByte()
out[1] = (height).toByte()
var o: Int = 2
var out: ByteArray = ByteArray((3 + total))
out[0] = DrawProtocol.RLE.toByte()
out[1] = (width).toByte()
out[2] = (height).toByte()
var o: Int = 3
var i: Int = 0
while ((i < total)) {
var color: Int = (nibbles[i] and 0xff)
@@ -0,0 +1,41 @@
package com.faceclaw.app
/** Immutable shell snapshot. Keys identify writable surfaces across repaints, not their pixels. */
class ShellScene(val layers: List<Layer>) {
class Layer(val key: Int, val x: Int, val y: Int, val width: Int, val height: Int, val dim: Int, val packed: ByteArray)
val fingerprint: String = layers.joinToString(";") { "${it.key},${it.x},${it.y},${it.width},${it.height},${it.dim},${CachedResource(it.packed).hash}" }
fun preview(screenGray: ByteArray, width: Int, height: Int): ByteArray {
val screen = BmpUtil.pack4bppFromGray8(screenGray, width, height)
val output = ByteArray(screen.size); val resources = HashMap<Int, ByteArray>(); val calls = ArrayList<ByteArray>()
for ((id, layer) in layers.withIndex()) {
if (layer.dim < 256) calls.add(DrawProtocol.lut(width, height, layer.dim))
resources[id] = DrawProtocol.rawImage(layer.width, layer.height, layer.packed)
calls.add(DrawProtocol.image(id, layer.x, layer.y))
}
resources[511] = DrawProtocol.displayList(calls)
DisplayListRenderer(resources).render(511, DisplayListRenderer.Target(screen, width, height), DisplayListRenderer.Target(output, width, height))
val target = DisplayListRenderer.Target(output, width, height)
return ByteArray(width * height) { (target.get(it % width, it / width) * 16).toByte() }
}
companion object {
val EMPTY = ShellScene(emptyList())
fun decode(reader: ByteReader): ShellScene {
val count = reader.getShort().toInt() and 65535; require(count <= 64)
val layers = ArrayList<Layer>()
repeat(count) {
require(reader.remaining() >= 14)
val key = reader.getShort().toInt() and 65535
val x = reader.getShort().toInt(); val y = reader.getShort().toInt()
val w = reader.getShort().toInt() and 65535; val h = reader.getShort().toInt() and 65535
val dim = reader.getShort().toInt() and 65535
val reserved = reader.getShort().toInt(); require(reserved == 0 && dim <= 256)
require(w in 1..640 && h in 1..480 && 5 + (w + 1) / 2 * h <= 65536 && reader.remaining() >= w * h)
val gray = ByteArray(w * h); reader.get(gray)
layers.add(Layer(key, x, y, w, h, dim, BmpUtil.pack4bppFromGray8(gray, w, h)))
}
require(reader.remaining() == 0)
require(layers.map { it.key }.toSet().size == layers.size)
return ShellScene(layers)
}
}
}
@@ -202,6 +202,8 @@ class SurfaceCompositor {
/** One composited full-screen frame plus the metadata the pipeline needs. */
class Composite {
@JvmField var screenGray: ByteArray
@JvmField var shellScene: ShellScene = ShellScene.EMPTY
/** Full-screen 8bpp grayscale pixels, screenWidth*screenHeight bytes. */
@JvmField val gray: ByteArray
@@ -233,6 +235,7 @@ class SurfaceCompositor {
draws: Array<ScreenDraw>?,
) {
this.gray = gray
this.screenGray = gray
this.width = width
this.height = height
this.fingerprint = fingerprint
@@ -241,6 +244,12 @@ class SurfaceCompositor {
}
}
private var shellScene: ShellScene? = null
fun setShellScene(reader: ByteReader) {
val scene = ShellScene.decode(reader)
lock.withLock { shellScene = scene }
}
private class Surface {
@JvmField val id: String
@@ -372,6 +381,7 @@ class SurfaceCompositor {
/** The dim factor (256 = none) that applies to a surface. */
private fun dimForLocked(surface: Surface): Int {
if (shellScene != null) return 256
return (if ((surface.zOrder < underlayDimBelowZOrder)) underlayDim else 256)
}
@@ -560,8 +570,10 @@ class SurfaceCompositor {
if (((screenWidth <= 0) || (screenHeight <= 0))) {
return null
}
var gray: ByteArray = buildGrayLocked()
return Composite(gray, screenWidth, screenHeight, "preview", 0, NO_DRAWS)
val seq = nextCompositeSeq
val result = compositeLocked()
nextCompositeSeq = seq
return result
}
}
@@ -598,7 +610,7 @@ class SurfaceCompositor {
fingerprint.append(screenWidth).append('x').append(screenHeight)
var draws: MutableList<ScreenDraw> = ArrayList()
for (surface in ordered) {
if (!surface.visible) {
if (!surface.visible || (shellScene != null && surface.id == "shell")) {
continue
}
blendLocked(gray, surface)
@@ -644,14 +656,10 @@ class SurfaceCompositor {
fingerprint.append(":dim").append(dim)
}
}
return Composite(
gray,
screenWidth,
screenHeight,
fingerprint.toString(),
nextCompositeSeq++,
draws.toTypedArray(),
)
val scene = if (surfaces.values.any { it.visible && it.zOrder > 1 }) ShellScene.EMPTY else shellScene ?: ShellScene.EMPTY
val preview = if (shellScene != null) scene.preview(gray, screenWidth, screenHeight) else gray
return Composite(preview, screenWidth, screenHeight, fingerprint.append("|shell:").append(scene.fingerprint).toString(),
nextCompositeSeq++, draws.toTypedArray()).also { it.screenGray = gray; it.shellScene = scene }
}
private fun blendLocked(gray: ByteArray, surface: Surface): Unit {
@@ -230,6 +230,8 @@ class IosSurfaceCompositor(width: Int, height: Int) {
fun blank(blanked: Boolean) = compositor.setBlanked(blanked)
fun shell(data: NSData) = compositor.setShellScene(IosByteReader(data))
fun submit(id: String, data: NSData, x: Int, y: Int, width: Int, height: Int) =
submitDraws(id, data, x, y, width, height, null)
@@ -21,24 +21,23 @@ class IosTextureFrame internal constructor(internal val composite: SurfaceCompos
val pixels: NSData = composite.gray.data()
}
class IosTexturePlan internal constructor(result: TexturePlanner.Result, val usedBytes: Int) {
val payload: NSData = result.payload.data()
val resourceCommands: List<NSData> = result.resourceCommands.map { it.data() }
class IosTexturePlan internal constructor(result: ScenePlanner.Plan, val usedBytes: Int) {
val payload: NSData = result.commands.last().data()
val resourceCommands: List<NSData> = result.commands.dropLast(1).map { it.data() }
val nextFid: Int = result.nextFid
}
/** One instance per BLE session, called on the session's serial JS thread. */
class IosTexturePlanner {
private val cache = ResourceCacheState()
private val planner = ScenePlanner(cache)
fun reset() = cache.reset()
fun plan(previous: NSData?, next: NSData, frame: IosTextureFrame, firstId: Int): IosTexturePlan? {
val composite = frame.composite
val result = TexturePlanner.plan(
previous?.byteArray(), next.byteArray(), composite.width, composite.height,
composite.draws, cache, firstId, true, ConnectionOptions.MULTI_RECT_MAX_RECTS,
) ?: return null
val result = planner.plan(BmpUtil.pack4bppFromGray8(composite.screenGray, composite.width, composite.height),
composite.width, composite.height, composite.draws, composite.shellScene, firstId)
return IosTexturePlan(result, cache.usedBytes())
}
}
+1
View File
@@ -248,6 +248,7 @@ function shellEnv({ wakeAction = 'voice-input', skipConfirmation = false } = {})
const settings = { wakeWordActionSetting: { get: () => wakeAction }, brightnessSetting: { get: () => 'auto' },
assistantSkipConfirmationSetting: { get: () => skipConfirmation } };
const { shell, rawInputEventToInputEvent } = load('app/ui/shell/shell.ts', {
'../../graphics/shell-scene': { encodeShellScene: () => new Uint8Array([0, 0]) },
'../../graphics/image': images, '../../graphics/plane': {}, '../../graphics/ui-fonts': {}, '../../g2/events': load('app/g2/events.ts', {}),
'../gestures': gestures, '../layers': layers, '../menu': { MenuLayer: Menu },
'../input-monitor': load('app/ui/input-monitor.ts', {}),
+16 -1
View File
@@ -2,7 +2,22 @@ const assert = require('node:assert/strict');
// Independent firmware-format decoder for reconstruction tests (not app code).
function applyDisplayPayload(base, payload, width = 640, height = 480) {
const mode = payload[0]; assert.ok(mode === 3 || mode === 6);
const mode = payload[0];
if (mode === 28) return base;
if (mode === 26) {
const word = i => payload[i] | payload[i+1] << 8;
let pos=3, out=base;
for(let n=0;n<word(1);n++) {
const length=word(pos);pos+=2;const call=payload.subarray(pos,pos+length);pos+=length;
assert.equal(call[0],1);assert.equal(call[1],0);
let legacy;
if(call[2]===0) legacy=Uint8Array.from([3,...call.subarray(3,7),0,0,...call.subarray(7)]);
else { assert.deepEqual([...call.subarray(3,11)],[0,0,0,0,width&255,width>>8,height&255,height>>8]);legacy=Uint8Array.from([6,...call.subarray(11)]); }
out=applyDisplayPayload(out,legacy,width,height);
}
assert.equal(pos,payload.length);return out;
}
assert.ok(mode === 3 || mode === 6);
const [left, top, w, h] = mode === 3
? [payload[1] * 4, payload[2] * 2, payload[3] * 4, payload[4] * 2] : [0, 0, width, height];
assert.ok(w > 0 && h > 0 && left + w <= width && top + h <= height);
+2 -2
View File
@@ -212,7 +212,7 @@ test('background glasses input still composites frames; phone resume preserves t
setScreenOn: on => screenStates.push(on), requestRender() {} };
const shell = { configure() {}, registerWindow() {}, wake() {}, focusWindow() {},
getWindows: () => [window], foregroundWindow: () => window, isScreenOn: () => true,
setBatteryLevels() {}, paintSurface() {}, underlayDim: () => 0, getFocus: () => 'app',
setBatteryLevels() {}, paintScene() { return new Uint8Array([0,0]); }, paintSurface() {}, underlayDim: () => 0, getFocus: () => 'app',
receiveInput: async input => { inputs.push(input); } };
class Session {
state = { phase: 'disconnected' };
@@ -247,7 +247,7 @@ test('background glasses input still composites frames; phone resume preserves t
'../apps/all-apps': { ALL_APPS: [] }, '../ui/dashboard-settings': settings,
'../native/phone-battery': { readPhoneBatteryState: () => ({ battery: 80, charging: false }) },
'../graphics/surface-compositor': { SurfaceCompositor: class {
configureSurface() {} setSurfaceVisible() {} submitSurfaceFrame() {} setUnderlayDim() {} setScreenBlanked() {}
setShellScene() {} configureSurface() {} setSurfaceVisible() {} submitSurfaceFrame() {} setUnderlayDim() {} setScreenBlanked() {}
compositeFrame() { return { pixels: new Uint8Array([1, 2]) }; }
} },
'../graphics/plane': { flattenPlanesWithDraws: () => ({ image: { pixels: new Uint8Array([1, 2]), width: 2, height: 1 }, draws: [] }) },
+47 -67
View File
@@ -178,9 +178,9 @@ test('session authenticates both arms, checks firmware and sends acknowledged di
assert.equal(h.session.state.charging, false);
assert.deepEqual(h.transport.sent.filter(s => s.message.sid === 128).map(s => s.id).sort(), ['L', 'R']);
h.session.setFrame(new Uint8Array(640 * 480)); await until(() => h.session.state.frames === 1);
const image = h.transport.sent.find(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 6);
const image = h.transport.sent.find(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 26);
assert.equal(image.id, 'L');
assert.equal(image.message.payload[0], 6);
assert.equal(image.message.payload[0], 26);
const layout = h.transport.sent.find(s => s.message.sid === p.SID.hub && s.message.command === 0);
assert.equal(p.readBytes(p.readBytes(layout.message.payload, 3), 4), undefined);
await h.session.stop();
@@ -399,14 +399,15 @@ test('CFW ACK identity and both-lens completion gate display metrics', async t =
const write = h.transport.write.bind(h.transport);
h.transport.write = async (...args) => { written += args[2].length; await write(...args); };
h.session.setFrame(new Uint8Array(640 * 480));
await until(() => imageMessages(h.transport).length === 1);
const sent = imageMessages(h.transport)[0];
await until(() => imageMessages(h.transport).length === 2);
const [draw, sent] = imageMessages(h.transport);
ack(h.transport,draw);
for (const wrong of [{ identifier: 'R' }, { size: 99 }, { checksum: 0 }, { ordinal: 1 }, { magic: sent.message.magic + 1 }]) ack(h.transport, sent, wrong);
ack(h.transport, sent, { lenses: [1] }); await tick();
assert.equal(h.session.state.frames, 0);
ack(h.transport, sent, { lenses: [2] }); await until(() => h.session.state.frames === 1);
assert.equal(iosBleTraffic.sample().bytes - before.bytes, written);
assert.equal(iosBleTraffic.sample().messages - before.messages, 1);
assert.equal(iosBleTraffic.sample().messages - before.messages, 2);
assert.equal(iosBleTraffic.sample().frames - before.frames, 1);
ack(h.transport, sent); await tick(); assert.equal(h.session.state.frames, 1);
});
@@ -421,7 +422,7 @@ test('large frames use bounded RLE bands and ordered three-message pipelining',
assert.equal(imageMessages(h.transport).length, 3); assert.equal(h.session.state.frames, 0);
ack(h.transport, first[0]); await until(() => imageMessages(h.transport).length === 6);
for (const sent of imageMessages(h.transport).slice(3)) ack(h.transport, sent);
await until(() => imageMessages(h.transport).length === 8);
await until(() => imageMessages(h.transport).length === 9);
assert.equal(h.session.state.frames, 0);
for (const sent of imageMessages(h.transport).slice(6)) ack(h.transport, sent);
await until(() => h.session.state.frames === 1);
@@ -433,19 +434,21 @@ test('large frames use bounded RLE bands and ordered three-message pipelining',
assert.deepEqual(shadow, p.packGray4(gray, 640, 480));
});
test('display pipelines A-B-A and coalesces newer waiting frames', async t => {
const h = harness(t); await h.session.start(addresses); h.transport.hold = true;
const submit = async value => { h.session.setFrame(new Uint8Array(640 * 480).fill(value)); await h.session.pump(); await tick(); };
await submit(0); await submit(0); assert.equal(imageMessages(h.transport).length, 1);
await submit(255); await submit(0); await until(() => imageMessages(h.transport).length === 3);
await submit(80); await submit(160);
const sent = imageMessages(h.transport);
ack(h.transport, sent[2]); ack(h.transport, sent[1]); await tick();
assert.equal(h.session.state.frames, 0); assert.equal(imageMessages(h.transport).length, 3);
ack(h.transport, sent[0]); await until(() => imageMessages(h.transport).length === 4);
assert.equal(h.session.state.frames, 3);
assert.deepEqual(imageBody(imageMessages(h.transport)[3]), p.concat(new Uint8Array([6]), p.rle4(p.packGray4(new Uint8Array(640 * 480).fill(160), 640, 480))));
ack(h.transport, imageMessages(h.transport)[3]); await until(() => h.session.state.frames === 4);
test('draw/present pairs pipeline and coalesce waiting frames', async t => {
const h=harness(t);await h.session.start(addresses);h.transport.hold=true;
const submit=async v=>{h.session.setFrame(new Uint8Array(640*480).fill(v));await h.session.pump();await tick();};
await submit(0);await submit(0);assert.equal(imageMessages(h.transport).length,2);
await submit(255);await until(()=>imageMessages(h.transport).length===3);
await submit(80);await submit(160);
const first=imageMessages(h.transport);ack(h.transport,first[2]);ack(h.transport,first[1]);await tick();
assert.equal(h.session.state.frames,0);assert.equal(imageMessages(h.transport).length,3);
ack(h.transport,first[0]);await until(()=>imageMessages(h.transport).length===6);
for(const message of imageMessages(h.transport).slice(3)) ack(h.transport,message);
await until(()=>h.session.state.frames===3);
const {applyDisplayPayload}=require('./helpers/display-payload.cjs');let screen=new Uint8Array(320*480);
for(const {message} of imageMessages(h.transport)) screen=applyDisplayPayload(screen,message.payload);
assert.deepEqual(screen,p.packGray4(new Uint8Array(640*480).fill(160),640,480));
assert.deepEqual(imageMessages(h.transport).map(s=>s.message.payload[0]),[26,28,26,28,26,28]);
});
test('NACK replays the unresolved window with fresh IDs and reset compression; stale ACKs are ignored', async t => {
@@ -459,6 +462,8 @@ test('NACK replays the unresolved window with fresh IDs and reset compression; s
for (const sent of old) ack(h.transport, sent);
await tick(); assert.equal(h.session.state.frames, 0);
replay.forEach((sent, i) => { assert.notEqual(sent.message.magic, old[i].message.magic); assert.ok(sent.message.flags & 8); assert.deepEqual(sent.message.payload, old[i].message.payload); ack(h.transport, sent); });
await until(() => imageMessages(h.transport).length === 9);
imageMessages(h.transport).slice(6).forEach(sent=>ack(h.transport,sent));
await until(() => h.session.state.frames === 3);
});
@@ -470,10 +475,10 @@ test('timeout starts after writes; missing ACK retries are bounded and invalidat
h.session.setFrame(new Uint8Array(640 * 480)); await h.session.pump();
await new Promise(resolve => setTimeout(resolve, 550));
assert.equal(h.session.state.phase, 'connected'); assert.equal(imageMessages(h.transport).length, 0);
release(); await until(() => imageMessages(h.transport).length === 1); await tick();
release(); await until(() => imageMessages(h.transport).length === 2); await tick();
for (let i = 1; i <= 4; i++) {
h.session.cfwPending.forEach(p => p.deadline = 1); h.session.recoverCfw();
if (i < 4) await until(() => imageMessages(h.transport).length === i + 1);
if (i < 4) await until(() => imageMessages(h.transport).length === 2 * (i + 1));
}
assert.equal(h.session.state.phase, 'retrying'); assert.match(h.session.state.status, /retry limit/);
assert.equal(h.session.lastEnqueued, null); assert.equal(h.session.cfwPending.length, 0);
@@ -509,55 +514,30 @@ test('full display window still services input and microphone control', async t
assert.deepEqual(audio, ['010203']); assert.equal(h.session.displayInFlight, 3);
});
test('pipelined bounding boxes reconstruct moving and erased content against the last queued frame', async t => {
const { applyDisplayPayload } = require('./helpers/display-payload.cjs');
const h = harness(t); await h.session.start(addresses); h.transport.hold = true;
const base = new Uint8Array(640 * 480), a = base.slice(), b = base.slice();
a[11 * 640 + 13] = 255; b[11 * 640 + 101] = 160;
const frames = [base, a, b];
for (let i = 0; i < frames.length; i++) {
h.session.setFrame(frames[i]); await h.session.pump();
await until(() => imageMessages(h.transport).length === i + 1);
test('draw updates reconstruct moving and erased pixels while present gates frame completion', async t => {
const {applyDisplayPayload}=require('./helpers/display-payload.cjs');
const h=harness(t);await h.session.start(addresses);h.transport.hold=true;
const base=new Uint8Array(640*480),a=base.slice(),b=base.slice();a[11*640+13]=255;b[11*640+101]=160;
let screen=new Uint8Array(320*480);
for(const [i,frame] of [base,a,b,base].entries()) {
h.session.setFrame(frame);await h.session.pump();await until(()=>imageMessages(h.transport).length===(i+1)*2);
const [draw,present]=imageMessages(h.transport).slice(i*2);
assert.equal(draw.message.payload[0],26);assert.equal(present.message.payload[0],28);
screen=applyDisplayPayload(screen,draw.message.payload);assert.deepEqual(screen,p.packGray4(frame,640,480));
ack(h.transport,draw);await tick();assert.equal(h.session.state.frames,i);
ack(h.transport,present);await until(()=>h.session.state.frames===i+1);
}
const messages = imageMessages(h.transport), payloads = messages.map(m => imageBody(m));
assert.deepEqual(payloads.map(b => b[0]), [6, 3, 3]);
assert.deepEqual(payloads.slice(1).map(b => b[5] | (b[6] << 8)), [1, 2]);
assert.equal(h.session.state.frames, 0); // All three updates precede any ACK.
let shadow = new Uint8Array(320 * 480);
for (let i = 0; i < payloads.length; i++) {
shadow = applyDisplayPayload(shadow, payloads[i]);
assert.deepEqual(shadow, p.packGray4(frames[i], 640, 480));
}
// A late ACK for the old full frame must not replace the delta comparison base.
ack(h.transport, messages[0]); await until(() => h.session.state.frames === 1);
h.session.setFrame(base); await h.session.pump();
await until(() => imageMessages(h.transport).length === 4);
const clear = imageBody(imageMessages(h.transport)[3]);
assert.equal(clear[0], 3); assert.equal(clear[1], 25); // Only B's dot needs clearing.
assert.deepEqual(applyDisplayPayload(shadow, clear), p.packGray4(base, 640, 480));
for (const message of imageMessages(h.transport).slice(1)) ack(h.transport, message);
await until(() => h.session.state.frames === 4);
assert.equal(imageMessages(h.transport)[2].message.payload[7],0); // compact bbox
});
test('delta ids skip reserved values and advance only on emitted deltas; reconnect reseeds with a full frame', async t => {
const h = harness(t); await h.session.start(addresses);
const submit = async gray => {
const before = h.session.state.frames; h.session.setFrame(gray);
await until(() => h.session.state.frames === before + 1);
return imageBody(imageMessages(h.transport).at(-1));
};
const base = new Uint8Array(640 * 480), next = base.slice(); next[1234] = 255;
assert.equal((await submit(base))[0], 6);
h.session.nextImageFrameId = 0xfffe;
const delta = await submit(next); assert.equal(delta[5] | (delta[6] << 8), 0xfffe);
h.session.setFrame(next); await h.session.pump(); await tick();
assert.equal(h.session.nextImageFrameId, 1);
const full = await submit(new Uint8Array(640 * 480).fill(255)); assert.equal(full[0], 6);
assert.equal(h.session.nextImageFrameId, 1);
const clear = await submit(next); assert.equal(clear[0], 6); // Whole-screen bounding box.
const wrapped = await submit(base); assert.equal(wrapped[5] | (wrapped[6] << 8), 1);
await h.session.stop(); await h.session.start(addresses);
assert.equal((await submit(base))[0], 6); // Even the same pixels need a trusted full base.
test('reconnect reseeds the screen and unified draw calls carry no legacy frame id', async t => {
const h=harness(t);await h.session.start(addresses);
const submit=async gray=>{const before=h.session.state.frames;h.session.setFrame(gray);await until(()=>h.session.state.frames===before+1);return imageBody(imageMessages(h.transport).at(-2));};
const base=new Uint8Array(640*480),next=base.slice();next[1234]=255;
assert.equal((await submit(base))[7],1); // wide full-screen bounds
const delta=await submit(next);assert.equal(delta[7],0); // compact bbox, immediately followed by RLE
const count=imageMessages(h.transport).length;h.session.setFrame(next);await h.session.pump();await tick();assert.equal(imageMessages(h.transport).length,count);
await h.session.stop();await h.session.start(addresses);assert.equal((await submit(next))[7],1);
});
const compassMessages = transport => transport.sent.filter(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 10);
+6 -6
View File
@@ -48,7 +48,7 @@ function fixture() {
isScreenOn: () => screenOn, getWindows: () => [window], foregroundWindow: () => window,
setBatteryLevels() {}, underlayDim: () => 1, getFocus: () => 'app', hasOverlay: () => false,
describeInputTarget: () => 'test app',
paintSurface: () => [{ image: new images.GrayImage(640, 480, 0), x: 0, y: 0 }],
paintScene: () => new Uint8Array([0, 0]), paintSurface: () => [{ image: new images.GrayImage(640, 480, 0), x: 0, y: 0 }],
receiveInput: async input => {
received.push(input);
if (input.type === 'display-wake' || !screenOn && input.type === 'double-click') shell.wake();
@@ -179,17 +179,17 @@ test('double-tap and other shell wakes replace Glanceboard without waking it aga
const f = fixture(); await f.controller.connect(); f.shell.sleep();
await f.hardware(9); await f.hardware(3);
assert.equal(f.shell.isScreenOn(), true); assert.equal(f.controller.glance.isVisible(), false);
assert.equal(f.sent.at(-1)[0], 75); assert.equal(f.boardStats.stops, 1);
assert.equal(f.sent.at(-1)[0], 80); assert.equal(f.boardStats.stops, 1);
f.shell.sleep(); await f.hardware(0); f.shell.wake(); await f.render();
assert.equal(f.controller.glance.isVisible(), false); assert.equal(f.sent.at(-1)[0], 75);
await f.advance(5000); await f.render(); assert.equal(f.sent.at(-1)[0], 75);
assert.equal(f.controller.glance.isVisible(), false); assert.equal(f.sent.at(-1)[0], 80);
await f.advance(5000); await f.render(); assert.equal(f.sent.at(-1)[0], 80);
});
test('head tilt uses Glanceboard when enabled and otherwise wakes the regular UI', async () => {
const f = fixture(); await f.controller.connect(); f.shell.sleep();
await f.hardware(12, 1); assertBoard(f.sent.at(-1)); assert.equal(f.shell.isScreenOn(), false);
await f.hardware(3); f.shell.sleep(); f.settings.tilt = false;
await f.hardware(12, 1); assert.equal(f.shell.isScreenOn(), true); assert.equal(f.sent.at(-1)[0], 75);
await f.hardware(12, 1); assert.equal(f.shell.isScreenOn(), true); assert.equal(f.sent.at(-1)[0], 80);
});
test('disabled triggers and scrolls do not show the board; awake taps still reach the shell', async () => {
@@ -234,7 +234,7 @@ test('iOS lock hides apps and Glanceboard, blocks input, and stays locked when p
await f.hardware(12, 1); assert.ok(f.sent.at(-1).every(p => p === 123));
f.observers.get('unlock')(); await f.render();
assert.equal(f.controller.glassesLocked, false);
assert.equal(f.sent.at(-1)[0], 75);
assert.equal(f.sent.at(-1)[0], 80);
await f.hardware(0); assert.equal(f.received.length, 1);
});
@@ -0,0 +1,83 @@
package com.faceclaw.app
import kotlin.test.*
class DisplayListTest {
private fun hex(s: String) = s.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
@Test fun oddPixelsRawRleClippingFontsNestedListsAndLut() {
val raw = DrawProtocol.rawImage(3, 3, hex("123045607890"))
val font = ByteArray(197); font[0]=1; font[67]=193.toByte(); hex("0801011f").copyInto(font,193)
val resources = mutableMapOf(10 to raw, 11 to hex("0802024f"), 12 to font)
val nested = listOf(DrawProtocol.image(11, -1, 0), DrawProtocol.bbox(hex("0c000000"), 2, 1, 0, 1, 1, 10))
resources[20]=DrawProtocol.displayList(nested)
val calls = listOf(DrawProtocol.call(7,DrawProtocol.word(20)), DrawProtocol.image(10,3,1),
DrawProtocol.call(5,hex("0c00070003000f0141")), DrawProtocol.lut(8,4,128))
resources[21]=DrawProtocol.displayList(calls)
val screen=hex("123456789abcdef0123456789abcdef0"); val output=ByteArray(16)
val renderer=DisplayListRenderer(resources)
assertEquals(setOf(10,11,12,20,21),renderer.render(21,DisplayListRenderer.Target(screen,8,4),DisplayListRenderer.Target(output,8,4)))
// Shared with the C sanitizer harness: signed clipping, resource target override, font and LUT.
assertContentEquals(hex("71122334755061700112233445534477"),output)
val first=output.copyOf(); renderer.render(21,DisplayListRenderer.Target(screen,8,4),DisplayListRenderer.Target(output,8,4))
assertContentEquals(first,output)
assertContentEquals(hex("123456789abcdef0123456789abcdef0"),screen)
}
@Test fun graphValidationPrecedesMutationAndCopyPreservesOverlap() {
val screen=hex("12345678"); val target=DisplayListRenderer.Target(screen,8,1)
val copy=DrawProtocol.call(2,hex("feff000000000600010002000000"))
DisplayListRenderer(emptyMap()).execute(DrawProtocol.sequence(listOf(copy)),target)
assertContentEquals(hex("12123456"),screen)
val valid=DrawProtocol.bbox(hex("ffffffff"),4,0,0,8,1)
assertFails { DisplayListRenderer(emptyMap()).execute(DrawProtocol.sequence(listOf(valid,byteArrayOf(99,0))),target) }
assertContentEquals(hex("12123456"),screen)
val cyclic=mapOf(1 to DrawProtocol.displayList(listOf(DrawProtocol.call(7,DrawProtocol.word(2)))),
2 to DrawProtocol.displayList(listOf(DrawProtocol.call(7,DrawProtocol.word(1)))))
assertFails { DisplayListRenderer(cyclic).render(1,target,target) }
}
private class Glasses {
val resources=mutableMapOf<Int,ByteArray>(); val screen=ByteArray(640*480/2);val composition=ByteArray(screen.size)
var root=65535
fun apply(commands: List<ByteArray>) {
for(b in commands) when(b[0].toInt()) {
21 -> { var p=3;repeat(DrawProtocol.u16(b,1)) {
val id=DrawProtocol.u16(b,p); val total=DrawProtocol.u16(b,p+2) or (DrawProtocol.u16(b,p+4) shl 16)
val offset=DrawProtocol.u16(b,p+6);val n=DrawProtocol.u16(b,p+8)
b.copyInto(resources.getOrPut(id){ByteArray(total)},offset,p+10,p+10+n);p+=10+n
} }
22 -> repeat(DrawProtocol.u16(b,1)) { resources.remove(DrawProtocol.u16(b,3+it*2)) }
29 -> repeat(DrawProtocol.u16(b,1)) { val p=3+it*6;resources.getOrPut(DrawProtocol.u16(b,p)) { DrawProtocol.rawImage(DrawProtocol.u16(b,p+2),DrawProtocol.u16(b,p+4)) } }
26 -> DisplayListRenderer(resources).execute(b.copyOfRange(1,b.size),DisplayListRenderer.Target(screen,640,480))
27 -> root=DrawProtocol.u16(b,1)
28 -> DisplayListRenderer(resources).render(root,DisplayListRenderer.Target(screen,640,480),DisplayListRenderer.Target(composition,640,480))
else -> error("legacy command ${b[0]}")
}
}
}
@Test fun persistentShellUpdatesDoNotPolluteScreenAndClosingRevealsCurrentApp() {
val cache=ResourceCacheState();val planner=ScenePlanner(cache);val glasses=Glasses()
val app=ByteArray(640*480/2){0x99.toByte()}
val overlay=ShellScene(listOf(ShellScene.Layer(1,0,0,3,3,128,hex("fff0fff0fff0"))))
val first=planner.plan(app,640,480,null,overlay,1);glasses.apply(first.commands)
assertContentEquals(app,glasses.screen);assertEquals(255,glasses.composition[0].toInt() and 255)
assertEquals(0x44,glasses.composition[100].toInt() and 255)
val changed=ShellScene(listOf(ShellScene.Layer(1,0,0,3,3,128,hex("111011101110"))))
val repaint=planner.plan(app,640,480,null,changed,1)
assertFalse(repaint.commands.any { it[0].toInt() in listOf(21,22,29) })
glasses.apply(repaint.commands);assertContentEquals(app,glasses.screen)
val newer=app.copyOf();newer[0]=0x22
glasses.apply(planner.plan(newer,640,480,null,changed,1).commands)
glasses.apply(planner.plan(newer,640,480,null,ShellScene.EMPTY,1).commands)
assertContentEquals(newer,glasses.composition)
cache.reset();val reconnect=planner.plan(newer,640,480,null,changed,1)
assertTrue(reconnect.commands.any { it[0].toInt()==29 });glasses.apply(reconnect.commands)
assertContentEquals(newer,glasses.screen)
}
@Test fun noisyFramesRemainBoundedAndResourceBudgetIs192KiB() {
val cache=ResourceCacheState();val planner=ScenePlanner(cache);val glasses=Glasses()
val app=ByteArray(640*480/2){(it*37).toByte()}
val plan=planner.plan(app,640,480,null,ShellScene.EMPTY,1)
assertTrue(plan.commands.all { it.size<=65535 });glasses.apply(plan.commands)
assertContentEquals(app,glasses.screen);assertContentEquals(app,glasses.composition)
assertEquals(196608,ResourceCacheState.CACHE_SIZE)
}
}
@@ -64,7 +64,7 @@ class ResourceCacheTest {
@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)
val a = resource(60000, 1); val b = resource(60000, 2); val c = resource(60000, 3); val d = resource(60000, 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.
@@ -142,12 +142,12 @@ class ResourceCacheTest {
@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))
assertEquals(193, u16(first.resource.bytes, 1 + (65 - 32) * 2))
assertEquals(0, u16(first.resource.bytes, 1 + (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))
assertEquals(193 + GlyphAtlas.get(id, 65)!!.cachedBytes.size, u16(second.resource.bytes, 1 + (66 - 32) * 2))
val cache = ResourceCacheState()
cache.prepare(listOf(second.resource)); val commands = cache.drainCommands(3600)
assertContentEquals(second.resource.bytes, uploaded(commands).getValue(0))
@@ -160,7 +160,7 @@ class ResourceCacheTest {
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))
assertEquals(0, u16(b.resource.bytes, 1 + (65 - 32) * 2))
assertEquals(193, u16(b.resource.bytes, 1 + (66 - 32) * 2))
}
}
@@ -24,9 +24,9 @@ class IosTextureApiTest {
val next = IosProtocol().pack(frame.pixels, 8, 4)
val planner = IosTexturePlanner()
val cold = assertNotNull(planner.plan(null, next, frame, 1))
assertEquals(22, cold.resourceCommands.first().byteArray()[0].toInt())
assertEquals(27, cold.resourceCommands.first().byteArray()[0].toInt())
assertTrue(cold.resourceCommands.any { it.byteArray()[0].toInt() == 21 })
assertEquals(8, cold.payload.byteArray()[0].toInt())
assertEquals(28, cold.payload.byteArray()[0].toInt())
val warm = assertNotNull(planner.plan(null, next, frame, 1))
assertTrue(warm.resourceCommands.isEmpty())
assertContentEquals(cold.payload.byteArray(), warm.payload.byteArray())
@@ -52,10 +52,11 @@ class IosTextureApiTest {
val frame = compositor.compositeFrame()
val plan = assertNotNull(IosTexturePlanner().plan(null, IosProtocol().pack(frame.pixels, 4, 2), frame, 1))
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 - 10].toInt())
assertEquals(65, payload.last().toInt())
assertEquals(28, plan.payload.byteArray()[0].toInt())
assertTrue(plan.resourceCommands.any { command ->
val bytes = command.byteArray()
bytes[0].toInt() == 26 && bytes.last().toInt() == 65
})
}
@Test
@@ -76,7 +77,9 @@ class IosTextureApiTest {
compositor.submitDraws("noise", gray.data(), 0, 0, 640, 480, imageDraw(id).data())
val frame = compositor.compositeFrame()
val planner = IosTexturePlanner()
assertNull(planner.plan(null, IosProtocol().pack(frame.pixels, 640, 480), frame, 1))
val noisy = assertNotNull(planner.plan(null, IosProtocol().pack(frame.pixels, 640, 480), frame, 1))
assertTrue(noisy.resourceCommands.all { it.length <= 65535uL })
planner.reset()
val small = IosSurfaceCompositor(4, 2)
small.configure("icon", 0, 0, 2, 2, 0, false)
small.submitDraws("icon", white, 0, 0, 2, 2, imageDraw(id).data())