forked from Blendman974/faceclaw
Background battery permission; incremental images
This commit is contained in:
1 parent
fab6f6689e
commit
875956e140
9 files changed
+233
-2
No files matched your search
@@ -1246,10 +1246,23 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
FrameTimings.getInstance().spanStart(frameId, "compress-and-plan");
|
||||
BleImageOptimizer.TileImagePlan plan =
|
||||
new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, bmp, nextMapSessionId());
|
||||
// Incremental (mode 3 bounding box) update when the previous frame is
|
||||
// known-displayed; displayedBmp is cleared on connect/reset/timeout
|
||||
// paths, so a non-empty value means display state is trusted.
|
||||
byte[] incrementalPayload = null;
|
||||
if (connectionOptions.INCREMENTAL_FRAMES && displayedBmp.length > 0) {
|
||||
incrementalPayload = BleImageOptimizer.buildIncrementalImagePayload(displayedBmp, bmp);
|
||||
}
|
||||
BleImageOptimizer.TileImagePlan plan = incrementalPayload != null
|
||||
? new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, bmp, nextMapSessionId(), incrementalPayload)
|
||||
: new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, bmp, nextMapSessionId());
|
||||
plan.fragments = BleImageOptimizer.planImageFragments(plan.payload, ConnectionOptions.IMAGE_FRAGMENT_SIZE);
|
||||
FrameTimings.getInstance().spanEnd(frameId, "compress-and-plan");
|
||||
if (incrementalPayload != null) {
|
||||
FrameTimings.getInstance().log(frameId, "incremental update bbox="
|
||||
+ ((incrementalPayload[3] & 0xff) * 4) + "x" + ((incrementalPayload[4] & 0xff) * 2)
|
||||
+ "+" + ((incrementalPayload[1] & 0xff) * 4) + "+" + ((incrementalPayload[2] & 0xff) * 2));
|
||||
}
|
||||
|
||||
int updateId = nextImageUpdateId++;
|
||||
int messageCount = plan.fragments.size();
|
||||
|
||||
+96
@@ -85,6 +85,102 @@ public final class BleImageOptimizer {
|
||||
this.payload = maybeCompress(this.bmp);
|
||||
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
|
||||
* dedup cache, which after an applied incremental equals the full frame.
|
||||
*/
|
||||
TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] bmp, int sessionId, byte[] payloadOverride) {
|
||||
this.tileIndex = tileIndex;
|
||||
this.tile = tile;
|
||||
this.bmp = BmpUtil.copyTileBmp(bmp);
|
||||
this.payload = payloadOverride;
|
||||
this.sessionId = sessionId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a mode-3 bounding-box incremental payload, or null when a full
|
||||
* update should be sent instead (frames not comparable, or the changed
|
||||
* region spans the whole screen). Wire format:
|
||||
* [3][left/4][top/2][width/4][height/2][zlib of headerless 4bpp region]
|
||||
* The region is top-down rows of the NEW frame, stride width/2 bytes. The
|
||||
* box is aligned so left/width are multiples of 4 pixels and top/height
|
||||
* multiples of 2 rows, letting each coordinate fit one byte and avoiding
|
||||
* 4bpp byte-boundary corner cases. Identical frames yield null too; the
|
||||
* caller's dedup check should normally catch that first.
|
||||
*/
|
||||
public static byte[] buildIncrementalImagePayload(byte[] previousBmp, byte[] newBmp) {
|
||||
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);
|
||||
int stride = (width + 1) >> 1;
|
||||
if (previous.length != stride * height || next.length != stride * height) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Changed bounding box in packed-byte coordinates (1 byte = 2 pixels).
|
||||
int minByteX = stride;
|
||||
int maxByteX = -1;
|
||||
int minY = height;
|
||||
int maxY = -1;
|
||||
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]) {
|
||||
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
|
||||
}
|
||||
|
||||
// Pixel-space box, aligned: left/width to multiples of 4, top/height to
|
||||
// multiples of 2, clamped to the frame.
|
||||
int left = (minByteX * 2) & ~3;
|
||||
int rightExclusive = Math.min(width, (((maxByteX + 1) * 2) + 3) & ~3);
|
||||
int top = minY & ~1;
|
||||
int bottomExclusive = Math.min(height, (maxY + 2) & ~1);
|
||||
int boxWidth = rightExclusive - left;
|
||||
int boxHeight = bottomExclusive - top;
|
||||
if (boxWidth >= width && boxHeight >= height) {
|
||||
return null; // whole screen changed; full update is strictly better
|
||||
}
|
||||
|
||||
int regionStride = boxWidth >> 1;
|
||||
byte[] region = new byte[regionStride * boxHeight];
|
||||
for (int y = 0; y < boxHeight; y++) {
|
||||
System.arraycopy(next, (top + y) * stride + (left >> 1), region, y * regionStride, regionStride);
|
||||
}
|
||||
byte[] compressed = deflate(region);
|
||||
byte[] out = new byte[5 + compressed.length];
|
||||
out[0] = 3;
|
||||
out[1] = (byte) (left / 4);
|
||||
out[2] = (byte) (top / 2);
|
||||
out[3] = (byte) (boxWidth / 4);
|
||||
out[4] = (byte) (boxHeight / 2);
|
||||
System.arraycopy(compressed, 0, out, 5, compressed.length);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -28,4 +28,12 @@ public class ConnectionOptions {
|
||||
final boolean sendImagesToLeft = true;
|
||||
final boolean skipSessionIds = true;
|
||||
final int WINDOW_SIZE = 1;
|
||||
// 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
|
||||
// display buffer is not always the previous frame (occasionally two frames
|
||||
// back, apparently display-driver buffer swapping), so partial updates
|
||||
// composite onto stale content per-lens. Re-enable once the firmware
|
||||
// guarantees the compositing base.
|
||||
final boolean INCREMENTAL_FRAMES = false;
|
||||
}
|
||||
@@ -107,6 +107,21 @@ public class BmpUtil {
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user