Performance optimizations; add TerminusV variable-width font

This commit is contained in:
James Babcock committed 2026-07-11 23:41:57 -07:00
1 parent ff447d5161
commit b2c90bc043
16 files changed
+4267 -217

No files matched your search

@@ -1004,7 +1004,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
enqueueFirmwareDebugFlagsLocked();
}
if (messageToPrewrite == null && handleHeartbeat()) {
// A frame ready to send right now: don't inject a fresh
// heartbeat in front of it (the image's own ack resets the
// firmware heartbeat timer, so the heartbeat is redundant).
boolean imageWaiting = !shutdownRequested && fixedLayoutCreated && warmedUp
&& pendingMessages.isEmpty() && inFlightMessages.isEmpty()
&& now >= imageRetryAfterMs
&& !getDesiredFingerprint().equals(displayedFingerprint);
if (messageToPrewrite == null && handleHeartbeat(imageWaiting)) {
return ConnectionOptions.IDLE_SLEEP_MS;
}
@@ -1086,7 +1093,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
return message.message.length + 2 > 232;
}
private boolean handleHeartbeat() {
private boolean handleHeartbeat(boolean imageWaiting) {
long now = SystemClock.elapsedRealtime();
boolean heartbeatEligible = !shutdownRequested && warmedUp && fixedLayoutCreated;
boolean heartbeatPending = heartbeatEligible && hasPendingOrInflightKindLocked("heartbeat");
@@ -1096,6 +1103,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
boolean heartbeatBlocksLeftWrites = heartbeatReady || heartbeatPending;
if (heartbeatReady && !heartbeatPending && inFlightMessages.isEmpty()) {
if (imageWaiting && !heartbeatUrgent) {
// Defer to the waiting frame: sending it now satisfies the
// firmware heartbeat deadline (its ack resets the timer), and
// the heartbeat still fires once we reach the URGENT threshold
// if rendering goes quiet again. Preserves the pending-heartbeat
// inter-lens-sync invariant below (that path is untouched).
return false;
}
Log.i(TAG, "Writing heartbeat");
OutboundMessage heartbeatMessage = createHeartbeatMessage();
lastHeartbeatSentAtMs = now;
@@ -1479,9 +1494,11 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
// known-displayed; displayedBmp is cleared on connect/reset/timeout
// paths, so a non-empty value means display state is trusted.
byte[] incrementalPayload = null;
BleImageOptimizer.IncrementalPlan incrementalPlan = null;
if (connectionOptions.INCREMENTAL_FRAMES && displayedBmp.length > 0) {
incrementalPayload = BleImageOptimizer.buildIncrementalImagePayload(displayedBmp, bmp, nextImageFrameId);
if (incrementalPayload != null) {
incrementalPlan = BleImageOptimizer.buildIncrementalImagePayload(displayedBmp, bmp, nextImageFrameId);
if (incrementalPlan != null) {
incrementalPayload = incrementalPlan.payload;
// advance only when a delta is actually emitted, so consecutive
// deltas carry consecutive ids (CFW skip/reorder detection)
nextImageFrameId = nextImageFrameId >= 0xfffe ? 1 : nextImageFrameId + 1;
@@ -1493,9 +1510,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
plan.fragments = BleImageOptimizer.planImageFragments(plan.payload, ConnectionOptions.IMAGE_FRAGMENT_SIZE);
FrameTimings.getInstance().spanEnd(frameId, "compress-and-plan");
if (incrementalPayload != null) {
// changed/box are packed-byte counts (2px each); clusters>1 with
// changed<<box means distant small edits merged into an oversized
// box (see BleImageOptimizer.IncrementalPlan) — the prime suspect
// for a delta spilling into a second, serialized BLE message.
FrameTimings.getInstance().log(frameId, "incremental update bbox="
+ ((incrementalPayload[3] & 0xff) * 4) + "x" + ((incrementalPayload[4] & 0xff) * 2)
+ "+" + ((incrementalPayload[1] & 0xff) * 4) + "+" + ((incrementalPayload[2] & 0xff) * 2));
+ "+" + ((incrementalPayload[1] & 0xff) * 4) + "+" + ((incrementalPayload[2] & 0xff) * 2)
+ " changed=" + incrementalPlan.changedBytes + "/" + incrementalPlan.boxBytes + "B"
+ " clusters=" + incrementalPlan.clusterCount);
}
int updateId = nextImageUpdateId++;
@@ -115,7 +115,29 @@ public final class BleImageOptimizer {
* skipped or duplicated deltas (its diagnostic overlay); the caller advances
* it by 1 per emitted delta so consecutive deltas are consecutive.
*/
public static byte[] buildIncrementalImagePayload(byte[] previousBmp, byte[] newBmp, int frameId) {
/**
* A mode-3 incremental payload plus diagnostics about how tightly the
* bounding box fit the actual change. {@code changedBytes} is how many
* packed bytes actually differ; {@code boxBytes} is how many the box sends.
* A large box with few changed bytes / multiple clusters means distant
* small edits were merged into one oversized box (which can spill past the
* fragment size into an extra serialized BLE message).
*/
public static final class IncrementalPlan {
public final byte[] payload;
public final int changedBytes;
public final int boxBytes;
public final int clusterCount;
IncrementalPlan(byte[] payload, int changedBytes, int boxBytes, int clusterCount) {
this.payload = payload;
this.changedBytes = changedBytes;
this.boxBytes = boxBytes;
this.clusterCount = clusterCount;
}
}
public static IncrementalPlan buildIncrementalImagePayload(byte[] previousBmp, byte[] newBmp, int frameId) {
int width = BmpUtil.readBmpWidth(newBmp);
int height = BmpUtil.readBmpHeight(newBmp);
if (width <= 0 || height <= 0
@@ -130,34 +152,41 @@ public final class BleImageOptimizer {
return null;
}
// Changed bounding box in packed-byte coordinates (1 byte = 2 pixels).
// Full diff scan in packed-byte coordinates (1 byte = 2 pixels): the
// changed bounding box plus diagnostics (total changed bytes, and how
// many disjoint horizontal column-clusters the change splits into).
int minByteX = stride;
int maxByteX = -1;
int minY = height;
int maxY = -1;
int changedBytes = 0;
boolean[] columnChanged = new boolean[stride];
for (int y = 0; y < height; y++) {
int rowOffset = y * stride;
for (int x = 0; x < stride; x++) {
if (previous[rowOffset + x] != next[rowOffset + x]) {
changedBytes++;
columnChanged[x] = true;
if (x < minByteX) minByteX = x;
if (x > maxByteX) maxByteX = x;
if (y < minY) minY = y;
maxY = y;
// The rest of the row only matters for maxByteX; scan from
// the right end to finish this row quickly.
for (int rx = stride - 1; rx > maxByteX; rx--) {
if (previous[rowOffset + rx] != next[rowOffset + rx]) {
maxByteX = rx;
break;
}
}
break;
}
}
}
if (maxY < 0) {
return null; // identical
}
int clusterCount = 0;
boolean inCluster = false;
for (int x = minByteX; x <= maxByteX; x++) {
if (columnChanged[x]) {
if (!inCluster) clusterCount++;
inCluster = true;
} else {
inCluster = false;
}
}
// Pixel-space box, aligned: left/width to multiples of 4, top/height to
// multiples of 2, clamped to the frame.
@@ -186,7 +215,7 @@ public final class BleImageOptimizer {
out[5] = (byte) (frameId & 0xff); // fid_lo
out[6] = (byte) ((frameId >> 8) & 0xff); // fid_hi
System.arraycopy(compressed, 0, out, 7, compressed.length);
return out;
return new IncrementalPlan(out, changedBytes, region.length, clusterCount);
}
/**
@@ -30,7 +30,7 @@ public class ConnectionOptions {
final boolean sendImagesToLeft = true;
final boolean skipSessionIds = true;
final int WINDOW_SIZE = 1;
final int WINDOW_SIZE = 2;
// Send changed-region (mode 3 bounding box) updates instead of full frames
// when the previous frame is known to be displayed. Requires firmware with
// the experimental bbox-incremental mode. Disabled: the firmware-side
@@ -10,11 +10,19 @@ final class InterruptibleSleep {
long delayMs = Math.max(1, ms);
long deadline = SystemClock.elapsedRealtime() + delayMs;
synchronized (lock) {
interrupted = false;
while (!interrupted) {
// Consume (do not clear-and-ignore) an interrupt delivered before this
// sleep began. interrupt() runs on other threads with no lock held by
// the sleeper between driveSession() returning and sleep() starting, so
// clearing the flag at entry lost that wake and forced the worker to
// sleep the full interval — up to 250ms per idle frame. Honoring it
// costs at most one extra (cheap) driveSession pass.
if (interrupted) {
interrupted = false;
return false;
}
while (true) {
long remaining = deadline - SystemClock.elapsedRealtime();
if (remaining <= 0) {
interrupted = false;
return true;
}
try {
@@ -23,9 +31,11 @@ final class InterruptibleSleep {
interrupted = false;
return false;
}
if (interrupted) {
interrupted = false;
return false;
}
}
interrupted = false;
return false;
}
}