diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java index 45810fa7..6e819e5a 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java @@ -120,20 +120,25 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { private PowerManager.WakeLock g2ScreenWakeLock; private String displayedFingerprint = ""; - private byte[] displayedBmp = new byte[0]; // The frame the firmware shadow will hold once the current image pipeline - // drains: the most recently ENQUEUED image, which is the correct base for the - // next delta when frames are pipelined (displayedBmp only reflects the last - // ACKED frame and lags behind). Set at enqueue; cleared whenever the image + // drains: the most recently ENQUEUED image (headerless packed 4bpp, see + // BmpUtil.pack4bppFromGray8), which is the correct base for the next delta + // when frames are pipelined. Set at enqueue; cleared whenever the image // pipeline is cleared (clearAllMessagesLocked / clearMessagesOfKindLocked // "image"), so it is only ever read while it holds a valid current-session base. - private byte[] lastEnqueuedImageBmp = new byte[0]; + private byte[] lastEnqueuedPacked = new byte[0]; + private int lastEnqueuedWidth; + private int lastEnqueuedHeight; private String lastEnqueuedFingerprint = ""; private final Map imageUpdateStats = new HashMap<>(); private final Object desiredTilesLock = new Object(); private String desiredFingerprint = ""; - private byte[] desiredBmp = new byte[0]; + // Headerless packed 4bpp frame (see BmpUtil.pack4bppFromGray8) plus its + // pixel dimensions. + private byte[] desiredPacked = new byte[0]; + private int desiredWidth; + private int desiredHeight; private int desiredPaintMs; private int desiredFrameId; // Highest compositor sequence stored as the desired frame; composites that @@ -492,8 +497,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { public void setSurfaceVisible(String id, boolean visible) { compositor.setSurfaceVisible(id, visible); SurfaceCompositor.Composite composite = compositor.composite(); - byte[] bmp = BmpUtil.build4bppBmp(composite.gray, composite.width, composite.height); - storeDesiredComposite(composite, bmp, 0, 0); + byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height); + storeDesiredComposite(composite, packed, 0, 0); } /** @@ -504,8 +509,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { public void setScreenBlanked(boolean blanked) { compositor.setBlanked(blanked); SurfaceCompositor.Composite composite = compositor.composite(); - byte[] bmp = BmpUtil.build4bppBmp(composite.gray, composite.width, composite.height); - storeDesiredComposite(composite, bmp, 0, 0); + byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height); + storeDesiredComposite(composite, packed, 0, 0); } /** @@ -547,16 +552,17 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { SurfaceCompositor.Composite composite = compositor.applyAndComposite( surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint); FrameTimings.getInstance().spanEnd(frameId, "composite"); - // Convert the composited 8bpp buffer into the BMP wire format here so - // that all framing concerns live on the Java side. - FrameTimings.getInstance().spanStart(frameId, "bmp-convert"); - byte[] bmp = BmpUtil.build4bppBmp(composite.gray, composite.width, composite.height); - FrameTimings.getInstance().spanEnd(frameId, "bmp-convert"); - storeDesiredComposite(composite, bmp, paintMs, frameId); + // Pack the composited 8bpp buffer down to the headerless 4bpp frame + // format the wire planners consume; BMP framing is added later only on + // the rare paths that still need it (warmup, uncompressed fallback). + FrameTimings.getInstance().spanStart(frameId, "pack-4bpp"); + byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height); + FrameTimings.getInstance().spanEnd(frameId, "pack-4bpp"); + storeDesiredComposite(composite, packed, paintMs, frameId); } /** Store a composite as the desired frame unless a newer one won the race. */ - private void storeDesiredComposite(SurfaceCompositor.Composite composite, byte[] bmp, int paintMs, int frameId) { + private void storeDesiredComposite(SurfaceCompositor.Composite composite, byte[] packed, int paintMs, int frameId) { int supersededFrameId = 0; boolean stale = false; synchronized (desiredTilesLock) { @@ -567,7 +573,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { } else { lastStoredCompositeSeq = composite.seq; supersededFrameId = desiredFrameId; - desiredBmp = bmp; + desiredPacked = packed; + desiredWidth = composite.width; + desiredHeight = composite.height; desiredFingerprint = composite.fingerprint; desiredPaintMs = paintMs; desiredFrameId = frameId; @@ -644,7 +652,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { /** * Start a fresh EvenHub plugin task on the existing BLE transport, then let * the session driver create the layout, warm up the image path, and send - * desiredBmp. + * the desired frame. */ public boolean resumeEvenHubSession() { int claimGeneration = 0; @@ -695,7 +703,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { audioCaptureActive = false; clearAllMessagesPreservingWakeLeaseLocked("EvenHub resume"); displayedFingerprint = ""; - displayedBmp = new byte[0]; imageRetryAfterMs = 0; lastHeartbeatSentAtMs = 0; lastHeartbeatAckedAtMs = 0; @@ -728,7 +735,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { fixedLayoutCreated = false; warmedUp = false; displayedFingerprint = ""; - displayedBmp = new byte[0]; }; message.onTimeout = () -> { if (reconnectOnTimeout) { @@ -954,7 +960,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { fixedLayoutCreated = false; warmedUp = false; displayedFingerprint = ""; - displayedBmp = new byte[0]; clearAllMessagesLocked("firmware exit event"); } } @@ -1049,7 +1054,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { audioPacketListener = null; clearAllMessagesLocked("connection lost"); displayedFingerprint = ""; - displayedBmp = new byte[0]; reconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RECONNECT_DELAY_MS; } } @@ -1081,7 +1085,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { warmedUp = false; clearAllMessagesLocked("session ready"); displayedFingerprint = ""; - displayedBmp = new byte[0]; lastAckAtMs = SystemClock.elapsedRealtime(); lastIncomingAtMs = lastAckAtMs; lastConnectionOrInputAtMs = lastAckAtMs; @@ -1384,7 +1387,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { && windowHasRoom && !hasPendingImageLocked() && now >= imageRetryAfterMs && !getDesiredFingerprint().equals(lastEnqueuedFingerprint)) { - // Enqueue the next frame's delta against lastEnqueuedImageBmp + // Enqueue the next frame's delta against lastEnqueuedPacked // (what the shadow will be), so it can pipeline behind an // image still awaiting its ack. Log.i(TAG, "Enqueued image update"); @@ -1744,7 +1747,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { clearMessagesOfKindLocked("startup-text-probe"); fixedLayoutCreated = true; displayedFingerprint = ""; - displayedBmp = new byte[0]; }; message.onTimeout = () -> { if (startupProbePending) { @@ -1770,7 +1772,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { fixedLayoutCreated = true; warmedUp = false; displayedFingerprint = ""; - displayedBmp = new byte[0]; logLine("existing dashboard layout accepted text probe; image warmup still required"); }; message.onTimeout = () -> { @@ -1786,14 +1787,17 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { } private void enqueueWarmupLocked() { - byte[] bmp; + int width; + int height; synchronized (desiredTilesLock) { - bmp = desiredBmp; + width = desiredWidth; + height = desiredHeight; } - if (bmp == null || bmp.length == 0) { - bmp = new byte[] {0}; - } - bmp = BmpUtil.buildBlankWarmupBmp(bmp); + // A blank full-size raw BMP: warmup primes the firmware image path with + // a frame shaped like the real ones, without flashing stale content. + byte[] bmp = (width > 0 && height > 0) + ? BmpUtil.build4bppBmpFromPacked(new byte[((width + 1) >> 1) * height], width, height) + : new byte[] {0}; BleProtocol.ImageTileOptions tile = DASHBOARD_TILE; int sessionId = nextMapSessionId(); List fragments = BleImageOptimizer.planImageFragments(bmp, ConnectionOptions.IMAGE_FRAGMENT_SIZE); @@ -1813,7 +1817,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { warmedUp = false; clearMessagesOfKindLocked("warmup"); displayedFingerprint = ""; - displayedBmp = new byte[0]; }; } logLine("queue blank warmup"); @@ -1840,19 +1843,24 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { private void enqueueDesiredImageLocked() { String fingerprint = getDesiredFingerprint(); - byte[] bmp; + byte[] packed; + int width; + int height; int paintMs; int frameId; synchronized (desiredTilesLock) { - bmp = desiredBmp; + packed = desiredPacked; + width = desiredWidth; + height = desiredHeight; paintMs = desiredPaintMs; frameId = desiredFrameId; desiredFrameId = 0; } - if (bmp == null) { - bmp = new byte[0]; + if (packed == null) { + packed = new byte[0]; } - if (Arrays.equals(bmp, lastEnqueuedImageBmp)) { + if (lastEnqueuedWidth == width && lastEnqueuedHeight == height + && Arrays.equals(packed, lastEnqueuedPacked)) { lastEnqueuedFingerprint = fingerprint; finishFrame(frameId, "discarded: image content identical to displayed"); return; @@ -1861,14 +1869,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { 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). - // lastEnqueuedImageBmp is cleared whenever the image pipeline is cleared, so + // lastEnqueuedPacked is cleared whenever the image pipeline is cleared, so // a non-empty value means the display base is trusted. byte[] incrementalPayload = null; String incrementalLog = null; - if (connectionOptions.INCREMENTAL_FRAMES && lastEnqueuedImageBmp.length > 0) { + if (connectionOptions.INCREMENTAL_FRAMES && lastEnqueuedPacked.length > 0 + && lastEnqueuedWidth == width && lastEnqueuedHeight == height) { int baseFid = nextImageFrameId; BleImageOptimizer.IncrementalPlan single = - BleImageOptimizer.buildIncrementalImagePayload(lastEnqueuedImageBmp, bmp, baseFid); + BleImageOptimizer.buildIncrementalImagePayload(lastEnqueuedPacked, packed, width, height, baseFid); if (single != null) { incrementalPayload = single.payload; // advance only when a delta is actually emitted, so consecutive @@ -1886,7 +1895,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { if (connectionOptions.MULTI_RECT_FRAMES && (single.clusterCount > 1 || single.payload.length > ConnectionOptions.MULTI_RECT_MIN_PAYLOAD)) { BleImageOptimizer.MultiRectPlan multi = BleImageOptimizer.buildMultiRectImagePayload( - lastEnqueuedImageBmp, bmp, baseFid, ConnectionOptions.MULTI_RECT_MAX_RECTS); + lastEnqueuedPacked, packed, width, height, baseFid, ConnectionOptions.MULTI_RECT_MAX_RECTS); if (multi != null && multi.payload.length < single.payload.length) { incrementalPayload = multi.payload; nextImageFrameId = multi.nextFid; // rectCount fids consumed @@ -1898,8 +1907,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { } } BleImageOptimizer.TileImagePlan plan = incrementalPayload != null - ? new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, bmp, nextMapSessionId(), incrementalPayload) - : new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, bmp, nextMapSessionId()); + ? new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, packed, width, height, nextMapSessionId(), incrementalPayload) + : new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, packed, width, height, nextMapSessionId()); plan.fragments = BleImageOptimizer.planImageFragments(plan.payload, ConnectionOptions.IMAGE_FRAGMENT_SIZE); FrameTimings.getInstance().spanEnd(frameId, "compress-and-plan"); if (incrementalLog != null) { @@ -1915,8 +1924,11 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { } // This frame is now the base for the next delta (it will be the firmware // shadow once applied), even though it hasn't been acked yet — that is what - // lets the next frame pipeline behind it. plan.bmp is the full frame. - lastEnqueuedImageBmp = BmpUtil.copyTileBmp(plan.bmp); + // lets the next frame pipeline behind it. plan.packed is the full frame; + // frames are immutable by convention, so referencing it is safe. + lastEnqueuedPacked = plan.packed; + lastEnqueuedWidth = plan.width; + lastEnqueuedHeight = plan.height; lastEnqueuedFingerprint = fingerprint; FrameTimings.getInstance().log(frameId, "queued image update#" + updateId @@ -1944,9 +1956,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { // with active rendering. lastHeartbeatAckedAtMs = SystemClock.elapsedRealtime(); logImageUpdateAckLandmarkLocked(message); - if (message.tileIndex >= 0 && !hasPendingOrInflightTileLocked(message.tileIndex)) { - displayedBmp = BmpUtil.copyTileBmp(plan.bmp); - } boolean imageStillInFlight = false; for (OutboundMessage inFlight : inFlightMessages) { if ("image".equals(inFlight.kind)) { @@ -1971,7 +1980,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { discardImageUpdateStatsLocked(message.imageUpdateId, "image ack timeout (will retry)"); clearMessagesOfKindLocked("image"); displayedFingerprint = ""; - displayedBmp = new byte[0]; imageRetryAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.IMAGE_RETRY_DELAY_MS; }; pendingMessages.addLast(message); @@ -2063,7 +2071,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { warmedUp = false; startupProbePending = false; displayedFingerprint = ""; - displayedBmp = new byte[0]; finishDesiredFrameLocked("discarded: glasses charging"); logLine("glasses are charging; pausing display communication"); setStateDisplay("charging", chargingStatusText(battery)); @@ -2220,7 +2227,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { if ("image".equals(kind)) { // The image pipeline was flushed (e.g. ack timeout -> keyframe resync): // drop the pipelined delta base so the next image is a full keyframe. - lastEnqueuedImageBmp = new byte[0]; + lastEnqueuedPacked = new byte[0]; lastEnqueuedFingerprint = ""; } } @@ -2230,7 +2237,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { clearInFlightMessagesLocked(reason); // The image pipeline is gone: drop the pipelined delta base so the next // image is a full keyframe rather than a delta onto a stale base. - lastEnqueuedImageBmp = new byte[0]; + lastEnqueuedPacked = new byte[0]; lastEnqueuedFingerprint = ""; } @@ -2260,7 +2267,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { ); } clearInFlightMessagesLocked(reason); - lastEnqueuedImageBmp = new byte[0]; + lastEnqueuedPacked = new byte[0]; lastEnqueuedFingerprint = ""; } @@ -2338,7 +2345,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { chargingMode = false; imageRetryAfterMs = 0; displayedFingerprint = ""; - displayedBmp = new byte[0]; faceclawWakePendingNonce = -1; lastFaceclawWakeLeaseQueuedAtMs = 0; faceclawWakeControlSentCount = 0; @@ -2377,26 +2383,11 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { lastFaceclawWakeLeaseQueuedAtMs = 0; faceclawWakeControlSentCount = 0; displayedFingerprint = ""; - displayedBmp = new byte[0]; // Deliberately not clearing silentMode: it is a property of the glasses, // not of our session, and silent mode blocks app launches, so it can be // the very cause of the session teardown that got us here. } - private boolean hasPendingOrInflightTileLocked(int tileIndex) { - for (OutboundMessage queued : pendingMessages) { - if (queued.tileIndex == tileIndex && ("image".equals(queued.kind) || "warmup".equals(queued.kind))) { - return true; - } - } - for (OutboundMessage inFlight : inFlightMessages) { - if (inFlight.tileIndex == tileIndex && ("image".equals(inFlight.kind) || "warmup".equals(inFlight.kind))) { - return true; - } - } - return false; - } - private void emitRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId) { final FaceclawBleCommunicatorListener current = listener; if (current == null) { diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java index 442d0649..5cf6303f 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleImageOptimizer.java @@ -73,28 +73,37 @@ public final class BleImageOptimizer { public static final class TileImagePlan { final int tileIndex; final BleProtocol.ImageTileOptions tile; - final byte[] bmp; // raw 4bpp BMP, kept for the displayed-tile dedup cache - final byte[] payload; // bytes actually streamed: mode-6 zlib(rle(4bpp)) when it shrinks, else == bmp + // Headerless packed 4bpp frame (top-down, stride ceil(width/2)), kept for + // the displayed-tile dedup cache. Frames are immutable by convention: + // producers always allocate a fresh buffer, so no defensive copy here. + public final byte[] packed; + public final int width; + public final int height; + final byte[] payload; // bytes actually streamed: mode-6 zlib(rle(4bpp)) when it shrinks, else a raw BMP final int sessionId; List fragments = Collections.emptyList(); - TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] bmp, int sessionId) { + TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] packed, int width, int height, int sessionId) { this.tileIndex = tileIndex; this.tile = tile; - this.bmp = BmpUtil.copyTileBmp(bmp); - this.payload = maybeCompress(this.bmp); + this.packed = packed == null ? new byte[0] : packed; + this.width = width; + this.height = height; + this.payload = maybeCompress(this.packed, width, height); this.sessionId = sessionId; } /** * Plan with an explicit wire payload (e.g. a mode-3 incremental update). - * bmp must still be the FULL new frame: it feeds the displayed-frame + * packed must still be the FULL new frame: it feeds the displayed-frame * dedup cache, which after an applied incremental equals the full frame. */ - TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] bmp, int sessionId, byte[] payloadOverride) { + TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] packed, int width, int height, int sessionId, byte[] payloadOverride) { this.tileIndex = tileIndex; this.tile = tile; - this.bmp = BmpUtil.copyTileBmp(bmp); + this.packed = packed == null ? new byte[0] : packed; + this.width = width; + this.height = height; this.payload = payloadOverride; this.sessionId = sessionId; } @@ -137,18 +146,10 @@ public final class BleImageOptimizer { } } - 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 - || width != BmpUtil.readBmpWidth(previousBmp) - || height != BmpUtil.readBmpHeight(previousBmp)) { - return null; - } - byte[] previous = BmpUtil.pack4bppFromBmp(previousBmp); - byte[] next = BmpUtil.pack4bppFromBmp(newBmp); + public static IncrementalPlan buildIncrementalImagePayload(byte[] previous, byte[] next, int width, int height, int frameId) { int stride = (width + 1) >> 1; - if (previous.length != stride * height || next.length != stride * height) { + if (width <= 0 || height <= 0 || previous == null || next == null + || previous.length != stride * height || next.length != stride * height) { return null; } @@ -271,18 +272,10 @@ public final class BleImageOptimizer { * single region (use the single-rect path), when it fragments past maxRects, or * when the frames aren't comparable. */ - public static MultiRectPlan buildMultiRectImagePayload(byte[] previousBmp, byte[] newBmp, int fidStart, int maxRects) { - int width = BmpUtil.readBmpWidth(newBmp); - int height = BmpUtil.readBmpHeight(newBmp); - if (width <= 0 || height <= 0 - || width != BmpUtil.readBmpWidth(previousBmp) - || height != BmpUtil.readBmpHeight(previousBmp)) { - return null; - } - byte[] previous = BmpUtil.pack4bppFromBmp(previousBmp); - byte[] next = BmpUtil.pack4bppFromBmp(newBmp); + public static MultiRectPlan buildMultiRectImagePayload(byte[] previous, byte[] next, int width, int height, int fidStart, int maxRects) { int stride = (width + 1) >> 1; - if (previous.length != stride * height || next.length != stride * height) { + if (width <= 0 || height <= 0 || previous == null || next == null + || previous.length != stride * height || next.length != stride * height) { return null; } @@ -417,19 +410,16 @@ public final class BleImageOptimizer { /** * RLE- then zlib-compress headerless 4bpp pixels for CFW load_image_z mode 6 when * it shrinks the payload. Wire format: [6][zlib(rle(4bpp pixels))]. A raw BMP - * (starts 'B') is sent verbatim when compression does not help. + * (starts 'B') is sent when compression does not help; that is the only spot + * where BMP framing is still built. */ - static byte[] maybeCompress(byte[] bmp) { - if (bmp == null || bmp.length == 0) { - return bmp; - } - byte[] packed = BmpUtil.pack4bppFromBmp(bmp); - if (packed.length == 0) { - return bmp; + static byte[] maybeCompress(byte[] packed, int width, int height) { + if (packed == null || packed.length == 0) { + return packed; } byte[] z = deflate(rleEncode(packed)); if (z == null || z.length + 1 >= packed.length) { - return bmp; + return BmpUtil.build4bppBmpFromPacked(packed, width, height); } byte[] out = new byte[z.length + 1]; out[0] = 6; @@ -487,24 +477,33 @@ public final class BleImageOptimizer { return (i & 1) != 0 ? (b & 0x0f) : (b >> 4); } + // Level 6: BEST_COMPRESSION cost 18-109ms per frame on the BLE worker, + // but BEST_SPEED inflated typical payloads from ~2.7KB past the 3800-byte + // fragment boundary, adding a whole extra ack round trip (~350ms). The + // default level keeps payloads under one fragment at about half the CPU. + // One long-lived Deflater per thread: constructing one allocates and + // initializes a native zlib stream, which is measurable at per-frame rates. + private static final ThreadLocal DEFLATER = + new ThreadLocal() { + @Override protected Deflater initialValue() { + return new Deflater(Deflater.DEFAULT_COMPRESSION); + } + }; + private static byte[] deflate(byte[] data) { - // Level 6: BEST_COMPRESSION cost 18-109ms per frame on the BLE worker, - // but BEST_SPEED inflated typical payloads from ~2.7KB past the 3800-byte - // fragment boundary, adding a whole extra ack round trip (~350ms). The - // default level keeps payloads under one fragment at about half the CPU. - Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION); - deflater.setInput(data); - deflater.finish(); - ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(64, data.length / 3)); - byte[] buf = new byte[4096]; + Deflater deflater = DEFLATER.get(); try { + deflater.setInput(data); + deflater.finish(); + ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(64, data.length / 3)); + byte[] buf = new byte[4096]; while (!deflater.finished()) { out.write(buf, 0, deflater.deflate(buf)); } + return out.toByteArray(); } finally { - deflater.end(); + deflater.reset(); } - return out.toByteArray(); } public static final class ImageUpdateStats { diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java b/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java index c1879376..f07f21d6 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java @@ -9,33 +9,64 @@ public class BmpUtil { return Arrays.copyOf(bmp, bmp.length); } - public static byte[] buildBlankWarmupBmp(byte[] bmp) { - byte[] warmup = BmpUtil.copyTileBmp(bmp); - int pixelOffset = readBmpPixelOffset(warmup); - if (pixelOffset <= 0 || pixelOffset >= warmup.length) { - Arrays.fill(warmup, (byte) 0); - return warmup; + // Quantize an 8-bit gray value to a 4-bit nibble. Truncate rather than round + // up at the bottom: the shell's color-key convention paints intentional + // black as 1 (0 being transparent), which must land on nibble 0, not a + // faintly-visible level 1. + private static final byte[] GRAY_TO_NIBBLE = new byte[256]; + static { + for (int v = 1; v < 256; v++) { + GRAY_TO_NIBBLE[v] = (byte) Math.min(15, (v + 8) >> 4); } - Arrays.fill(warmup, pixelOffset, warmup.length, (byte) 0); - return warmup; } /** - * Build a 4bpp grayscale BMP (BITMAPINFOHEADER + 16-entry gray palette, - * bottom-up rows padded to a 4-byte stride) from a full-resolution 8bpp - * grayscale buffer. The buffer is row-major, top-to-bottom, one byte per - * pixel with values 0..255; each pixel is quantized to a 4-bit nibble. - * - * Building the wire format here, rather than on the TypeScript side, keeps - * all framing concerns in one place so future firmware changes that drop or - * replace the BMP header (e.g. 8-bit color depth or iframes) only touch Java. + * Pack a full-resolution 8bpp grayscale buffer (row-major, top-to-bottom, + * one byte per pixel) into the headerless 4bpp wire format used by CFW + * load_image_z modes 3/6/8: top-down rows, stride ceil(width/2), high + * nibble = left pixel. This is the canonical in-memory frame format; BMP + * framing (needed only when an uncompressed full frame goes on the wire) + * is added on demand by build4bppBmpFromPacked. */ - public static byte[] build4bppBmp(byte[] gray8, int width, int height) { + public static byte[] pack4bppFromGray8(byte[] gray8, int width, int height) { if (width <= 0 || height <= 0) { return new byte[0]; } - int bytesPerPixelRow = (width + 1) >> 1; - int rowStride = (bytesPerPixelRow + 3) & ~3; + int stride = (width + 1) >> 1; + byte[] out = new byte[stride * height]; + if (gray8 == null || gray8.length < width * height) { + return out; // short buffer: treat missing pixels as black + } + int pairs = width >> 1; + int src = 0; + int dst = 0; + for (int y = 0; y < height; y++) { + for (int i = 0; i < pairs; i++) { + out[dst++] = (byte) ((GRAY_TO_NIBBLE[gray8[src] & 0xff] << 4) + | GRAY_TO_NIBBLE[gray8[src + 1] & 0xff]); + src += 2; + } + if ((width & 1) != 0) { + out[dst++] = (byte) (GRAY_TO_NIBBLE[gray8[src++] & 0xff] << 4); + } + } + return out; + } + + /** + * Wrap a packed 4bpp frame in BMP framing (BITMAPINFOHEADER + 16-entry gray + * palette, bottom-up rows padded to a 4-byte stride). Only the full-frame + * image path still speaks BMP on the wire (warmup, and the fallback when + * mode-6 compression does not shrink the frame); everything else uses the + * headerless packed format directly. + */ + public static byte[] build4bppBmpFromPacked(byte[] packed, int width, int height) { + int packedStride = (width + 1) >> 1; + if (width <= 0 || height <= 0 + || packed == null || packed.length < packedStride * height) { + return new byte[0]; + } + int rowStride = (packedStride + 3) & ~3; int pixelDataSize = rowStride * height; int fileHeaderSize = 14; @@ -69,31 +100,12 @@ public class BmpUtil { for (int bmpRow = 0; bmpRow < height; bmpRow++) { int srcY = height - 1 - bmpRow; // BMP rows run bottom-up - int srcRowOffset = srcY * width; - int rowOffset = pixelOffset + bmpRow * rowStride; - for (int x = 0; x < width; x += 2) { - int hi = grayToNibble(sampleGray(gray8, srcRowOffset + x)); - int lo = (x + 1 < width) ? grayToNibble(sampleGray(gray8, srcRowOffset + x + 1)) : 0; - buf[rowOffset + (x >> 1)] = (byte) ((hi << 4) | lo); - } + System.arraycopy(packed, srcY * packedStride, + buf, pixelOffset + bmpRow * rowStride, packedStride); } return buf; } - private static int sampleGray(byte[] gray8, int index) { - return (index >= 0 && index < gray8.length) ? (gray8[index] & 0xff) : 0; - } - - private static int grayToNibble(int value) { - // Truncate rather than round up: the shell's color-key convention - // paints intentional black as 1 (0 being transparent), which must - // land on nibble 0, not a faintly-visible level 1. - if (value <= 0) { - return 0; - } - return Math.min(15, (value+8) >> 4); - } - private static void putUint16Le(byte[] buf, int offset, int value) { buf[offset] = (byte) (value & 0xff); buf[offset + 1] = (byte) ((value >>> 8) & 0xff); @@ -109,70 +121,4 @@ public class BmpUtil { private static void putInt32Le(byte[] buf, int offset, int value) { putUint32Le(buf, offset, value); } - - public static int readBmpWidth(byte[] bmp) { - if (bmp == null || bmp.length < 0x36 || bmp[0] != 0x42 || bmp[1] != 0x4d) { - return 0; - } - return readUint32Le(bmp, 18); - } - - public static int readBmpHeight(byte[] bmp) { - if (bmp == null || bmp.length < 0x36 || bmp[0] != 0x42 || bmp[1] != 0x4d) { - return 0; - } - int heightSigned = readInt32Le(bmp, 22); - return heightSigned < 0 ? -heightSigned : heightSigned; - } - - public static int readBmpPixelOffset(byte[] bmp) { - if (bmp == null || bmp.length < 14 || bmp[0] != 0x42 || bmp[1] != 0x4d) { - return -1; - } - return (bmp[10] & 0xff) - | ((bmp[11] & 0xff) << 8) - | ((bmp[12] & 0xff) << 16) - | ((bmp[13] & 0xff) << 24); - } - - /** - * Strip the BMP header and row padding, producing the headerless 4bpp wire - * format expected by CFW load_image_z mode 6: top-down rows, stride = - * ceil(width/2), high nibble = left pixel. - */ - public static byte[] pack4bppFromBmp(byte[] bmp) { - if (bmp == null || bmp.length < 0x36 || bmp[0] != 0x42 || bmp[1] != 0x4d) { - return new byte[0]; - } - int width = readUint32Le(bmp, 18); - int heightSigned = readInt32Le(bmp, 22); - int height = heightSigned < 0 ? -heightSigned : heightSigned; - int pixelOffset = readBmpPixelOffset(bmp); - if (width <= 0 || height <= 0 || pixelOffset < 0 || pixelOffset >= bmp.length) { - return new byte[0]; - } - int packedStride = (width + 1) >> 1; - int bmpStride = (packedStride + 3) & ~3; - if (pixelOffset + (long) bmpStride * height > bmp.length) { - return new byte[0]; - } - byte[] out = new byte[packedStride * height]; - for (int y = 0; y < height; y++) { - int srcY = height - 1 - y; - int srcRow = pixelOffset + srcY * bmpStride; - System.arraycopy(bmp, srcRow, out, y * packedStride, packedStride); - } - return out; - } - - private static int readUint32Le(byte[] buf, int offset) { - return (buf[offset] & 0xff) - | ((buf[offset + 1] & 0xff) << 8) - | ((buf[offset + 2] & 0xff) << 16) - | ((buf[offset + 3] & 0xff) << 24); - } - - private static int readInt32Le(byte[] buf, int offset) { - return readUint32Le(buf, offset); - } }