forked from Blendman974/faceclaw
Support sending zlib-compressed images to modified firmware
This commit is contained in:
1 parent
7857e6337a
commit
401cf6d4c1
5 files changed
+50
-4
No files matched your search
@@ -1151,13 +1151,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean synchronizedCommits = changedTiles.size() > 1;
|
||||
//boolean synchronizedCommits = changedTiles.size() > 1;
|
||||
boolean synchronizedCommits = false;
|
||||
if (forceTiledCommit) {
|
||||
synchronizedCommits = false;
|
||||
}
|
||||
boolean reserveLastByte = false;
|
||||
for (BleImageOptimizer.TileImagePlan plan : changedTiles) {
|
||||
plan.fragments = BleImageOptimizer.planImageFragments(plan.bmp, ConnectionOptions.IMAGE_FRAGMENT_SIZE, reserveLastByte);
|
||||
plan.fragments = BleImageOptimizer.planImageFragments(plan.payload, ConnectionOptions.IMAGE_FRAGMENT_SIZE, reserveLastByte);
|
||||
}
|
||||
|
||||
int updateId = nextImageUpdateId++;
|
||||
|
||||
+38
-1
@@ -1,9 +1,11 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.zip.Deflater;
|
||||
import android.util.Log;
|
||||
|
||||
public final class BleImageOptimizer {
|
||||
@@ -71,7 +73,8 @@ public final class BleImageOptimizer {
|
||||
public static final class TileImagePlan {
|
||||
final int tileIndex;
|
||||
final BleProtocol.ImageTileOptions tile;
|
||||
final byte[] bmp;
|
||||
final byte[] bmp; // raw 4bpp BMP, kept for the displayed-tile dedup cache
|
||||
final byte[] payload; // bytes actually streamed: zlib BMP when it shrinks, else == bmp
|
||||
final int sessionId;
|
||||
List<BleProtocol.ImageFragment> fragments = Collections.emptyList();
|
||||
|
||||
@@ -79,10 +82,44 @@ public final class BleImageOptimizer {
|
||||
this.tileIndex = tileIndex;
|
||||
this.tile = tile;
|
||||
this.bmp = BmpUtil.copyTileBmp(bmp);
|
||||
this.payload = maybeCompress(this.bmp);
|
||||
this.sessionId = sessionId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* zlib-deflate a tile BMP for transmission when CFW image decompression is
|
||||
* enabled and it actually shrinks the payload. The CFW's load_image_z
|
||||
* inflates any image buffer whose first byte is a zlib stream (CM nibble 8);
|
||||
* a raw BMP (starts 0x42) is loaded unchanged, so the raw fallback is always
|
||||
* safe and mixing raw/compressed tiles is fine. java.util.zip.Deflater's
|
||||
* default is the zlib wrapper (2-byte header + adler32) at windowBits 15,
|
||||
* matching the firmware inflate.
|
||||
*/
|
||||
static byte[] maybeCompress(byte[] bmp) {
|
||||
if (!ConnectionOptions.COMPRESS_IMAGES_ZLIB || bmp == null || bmp.length == 0) {
|
||||
return bmp;
|
||||
}
|
||||
byte[] z = deflate(bmp);
|
||||
return (z != null && z.length < bmp.length) ? z : bmp;
|
||||
}
|
||||
|
||||
private static byte[] deflate(byte[] data) {
|
||||
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
|
||||
deflater.setInput(data);
|
||||
deflater.finish();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(64, data.length / 3));
|
||||
byte[] buf = new byte[4096];
|
||||
try {
|
||||
while (!deflater.finished()) {
|
||||
out.write(buf, 0, deflater.deflate(buf));
|
||||
}
|
||||
} finally {
|
||||
deflater.end();
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public static final class ImageUpdateStats {
|
||||
final int paintMs;
|
||||
final int tileCount;
|
||||
|
||||
@@ -128,6 +128,9 @@ public class BleProtocol {
|
||||
//inner.add(encodeVarintField(3, sessionId));
|
||||
inner.add(encodeVarintField(3, tile.containerId));
|
||||
inner.add(encodeVarintField(4, totalSize));
|
||||
// CompressMode stays 0: the CFW's zlib path is detected from the buffer's
|
||||
// zlib header at BMP-load time, and frag_write must copy our deflated bytes
|
||||
// VERBATIM (CompressMode!=0 would route them through the 1bpp expander).
|
||||
inner.add(encodeVarintField(5, 0)); //compression
|
||||
inner.add(encodeVarintField(6, fragment.index));
|
||||
inner.add(encodeVarintField(7, fragment.size));
|
||||
|
||||
@@ -19,6 +19,11 @@ public class ConnectionOptions {
|
||||
public static final int BATTERY_REFRESH_INTERVAL_MS = 5 * 60_000;
|
||||
public static final int BATTERY_INPUT_QUIET_MS = 5_000;
|
||||
public static final int IMAGE_FRAGMENT_SIZE = 3800;
|
||||
// Stream image tiles as zlib(DEFLATE)-compressed BMPs. REQUIRES the custom
|
||||
// firmware (load_image_z inflates any image buffer that starts with a zlib
|
||||
// stream; a raw BMP is loaded unchanged). Turn OFF when talking to stock
|
||||
// firmware, which would reject the compressed bytes as an invalid BMP.
|
||||
public static final boolean COMPRESS_IMAGES_ZLIB = true;
|
||||
public static final int IMAGE_RETRY_DELAY_MS = 2_000;
|
||||
public static final int MAX_CONSECUTIVE_ACK_TIMEOUTS = 8;
|
||||
public static final int EVEN_APP_WRITE_FAILURE_WINDOW_MS = 15_000;
|
||||
|
||||
@@ -62,7 +62,7 @@ public class MessageBuilder {
|
||||
BleProtocol.SID_EVENHUB,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildImageRawData(plan.tile, plan.sessionId, plan.bmp.length, fragment, magic),
|
||||
BleProtocol.buildImageRawData(plan.tile, plan.sessionId, plan.payload.length, fragment, magic),
|
||||
ACK_TIMEOUT_MS,
|
||||
plan.tileIndex,
|
||||
leftArm
|
||||
|
||||
Reference in new issue
Block a user