Massively improve framerate

This commit is contained in:
James Babcock committed 2026-07-04 12:29:50 -07:00
1 parent 630b511d32
commit c9fe92dc1c
6 files changed
+73 -92

No files matched your search

@@ -4,7 +4,6 @@ import android.annotation.SuppressLint;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
@@ -226,8 +225,13 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
}
/**
* image8bpp arrives as a ByteBuffer because NativeScript marshals a JS
* ArrayBuffer to one without the per-element bridge copy that a byte[]
* parameter would need (~150ms for a full frame).
*/
public void submitDashboardImage(
byte[] image8bpp,
java.nio.ByteBuffer image8bpp,
int width,
int height,
String fingerprint,
@@ -238,7 +242,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
// Convert the full-screen 8bpp grayscale 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 = image8bpp == null ? new byte[0] : BmpUtil.build4bppBmp(image8bpp, width, height);
byte[] gray;
if (image8bpp == null) {
gray = new byte[0];
} else {
gray = new byte[image8bpp.remaining()];
image8bpp.get(gray);
}
byte[] bmp = gray.length == 0 ? new byte[0] : BmpUtil.build4bppBmp(gray, width, height);
FrameTimings.getInstance().spanEnd(frameId, "bmp-convert");
int supersededFrameId;
synchronized (desiredTilesLock) {
@@ -279,16 +290,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
interruptibleSleep.interrupt();
}
public Bitmap createPreviewBitmap(int[] colors, int width, int height) {
if (colors == null) {
throw new IllegalArgumentException("colors are required");
}
if (width <= 0 || height <= 0 || colors.length < width * height) {
throw new IllegalArgumentException("invalid preview bitmap dimensions");
}
return Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888);
}
public boolean sendShutdown(int exitMode) {
int magic;
long startedAtMs = SystemClock.elapsedRealtime();
@@ -0,0 +1,34 @@
package com.faceclaw.app;
import android.graphics.Bitmap;
import java.nio.ByteBuffer;
/**
* Builds the phone-UI preview bitmap from an 8bpp grayscale frame. The
* grayscale buffer arrives as a ByteBuffer (NativeScript marshals a JS
* ArrayBuffer to one without copying element-by-element); doing the
* gray-to-ARGB expansion here keeps the 165KB-per-frame loop out of the
* JS/Java bridge, where it used to cost ~150ms per preview.
*/
public final class PreviewBitmapUtil {
private PreviewBitmapUtil() {}
public static Bitmap fromGray(ByteBuffer gray, int width, int height, double brightenGamma) {
if (gray == null || width <= 0 || height <= 0 || gray.remaining() < width * height) {
throw new IllegalArgumentException("invalid gray preview buffer");
}
int[] lut = new int[256];
for (int g = 0; g < 256; g++) {
int v = (int) Math.max(0, Math.min(255, Math.round(255 * Math.pow(g / 255.0, brightenGamma))));
lut[g] = 0xff000000 | (v << 16) | (v << 8) | v;
}
byte[] bytes = new byte[width * height];
gray.get(bytes);
int[] colors = new int[width * height];
for (int i = 0; i < colors.length; i++) {
colors[i] = lut[bytes[i] & 0xff];
}
return Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888);
}
}
@@ -111,7 +111,11 @@ public final class BleImageOptimizer {
}
private static byte[] deflate(byte[] data) {
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
// 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));