mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
Refactor a bunch of Java to Kotlin, making it usable in an iOS port
This commit is contained in:
1 parent
c0f8f7e9c2
commit
410fd5840b
162 files changed
+9650
-6224
No files matched your search
+13
-2
@@ -1,5 +1,5 @@
|
||||
# NativeScript
|
||||
hooks/
|
||||
hooks/*
|
||||
node_modules/
|
||||
platforms/
|
||||
|
||||
@@ -15,6 +15,18 @@ yarn-error.log*
|
||||
|
||||
# npm test compile output (tests/tsconfig.json outDir)
|
||||
.test-build/
|
||||
tests/kotlin/.gradle/
|
||||
tests/kotlin/.kotlin/
|
||||
tests/kotlin/build/
|
||||
native/kotlin/.gradle/
|
||||
native/kotlin/.kotlin/
|
||||
native/kotlin/build/
|
||||
native/kotlin/shared/build/
|
||||
native/kotlin/plugin/platforms/android/*.aar
|
||||
native/kotlin/plugin/platforms/ios/
|
||||
!hooks/before-prepare/
|
||||
hooks/before-prepare/*
|
||||
!hooks/before-prepare/faceclaw-kotlin.js
|
||||
|
||||
# General
|
||||
.DS_Store
|
||||
@@ -42,4 +54,3 @@ faceclaw_settings.xml
|
||||
|
||||
# Developer configs
|
||||
build_paths.sh
|
||||
|
||||
@@ -255,10 +255,6 @@ def faceclawVersionCode = {
|
||||
return parts[0] * 10000 + parts[1] * 100 + parts[2]
|
||||
}()
|
||||
|
||||
// ios-port packages commonMain in an AAR. Keeping this conditional lets the
|
||||
// source relocation merge there without compiling the shared classes twice.
|
||||
def faceclawDependencies = new groovy.json.JsonSlurper().parse(file("$rootDir/../../package.json")).dependencies ?: [:]
|
||||
|
||||
android {
|
||||
compileSdkVersion 35
|
||||
buildToolsVersion "35"
|
||||
@@ -266,11 +262,7 @@ android {
|
||||
|
||||
sourceSets.main {
|
||||
jniLibs.srcDirs += ["$projectDir/src/main/jniLibs"]
|
||||
// Keep portable Kotlin at the same path as ios-port's KMP commonMain.
|
||||
// main compiles it directly with the existing Android Kotlin plugin.
|
||||
if (!faceclawDependencies.containsKey("@faceclaw/kotlin")) {
|
||||
java.srcDirs += ["$rootDir/../../native/kotlin/shared/src/commonMain/kotlin"]
|
||||
}
|
||||
// Shared Kotlin is packaged by the local @faceclaw/kotlin plugin.
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
@@ -301,12 +293,21 @@ android {
|
||||
// project. Remove the former copy before Kotlin compilation after this move.
|
||||
def removeLegacyKotlinBridgeSource = tasks.register("removeLegacyKotlinBridgeSource", Delete) {
|
||||
delete file("$projectDir/src/main/java/com/faceclaw/shared/KotlinBridge.kt")
|
||||
// Explicit, reviewed source moves: never infer deletions from class names.
|
||||
def migrations = new groovy.json.JsonSlurper().parse(file("$rootDir/../../native/kotlin/migrated-java-sources.json"))
|
||||
migrations.each { migration ->
|
||||
if (!file("$rootDir/../../native/kotlin/${migration.kotlin}").isFile()) {
|
||||
throw new GradleException("Missing Kotlin replacement: ${migration.kotlin}")
|
||||
}
|
||||
delete file("$projectDir/src/main/java/${migration.java}")
|
||||
}
|
||||
}
|
||||
tasks.matching { it.name.startsWith("compile") && it.name.endsWith("Kotlin") }.configureEach {
|
||||
dependsOn removeLegacyKotlinBridgeSource
|
||||
}
|
||||
|
||||
tasks.matching { it.name == "preBuild" }.configureEach {
|
||||
dependsOn removeLegacyKotlinBridgeSource
|
||||
dependsOn tasks.named("prepareFaceclawNativeLibs")
|
||||
dependsOn tasks.named("prepareFaceclawLlama")
|
||||
}
|
||||
@@ -10,7 +10,7 @@ project.ext {
|
||||
// androidXTransition = "1.4.1"
|
||||
// androidXViewPager = "1.0.0"
|
||||
|
||||
// NativeScript 9 already applies kotlin-android. Pin its current compiler
|
||||
// NativeScript 9 already applies kotlin-android. Match the shared KMP compiler
|
||||
// for our .kt sources; no separate Kotlin installation is required.
|
||||
kotlinVersion = "2.2.20"
|
||||
kotlinVersion = "2.4.20"
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.media.audiofx.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/** Android AudioRecord effect attachment; PCM processing lives in shared Kotlin. */
|
||||
public final class AndroidNoiseEffects {
|
||||
/** Whether this Android build ships the built-in capture noise suppressor. */
|
||||
public static boolean platformSuppressorAvailable() {
|
||||
try {
|
||||
return NoiseSuppressor.isAvailable();
|
||||
} catch (Throwable t) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Short engine name for the UI. */
|
||||
public static String engineDescription() {
|
||||
return "spectral (phone DSP)";
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach the platform's built-in capture-effect chain (NoiseSuppressor,
|
||||
* AcousticEchoCanceler, AutomaticGainControl — whichever this Android
|
||||
* build provides) to a phone AudioRecord session. Only applicable when the
|
||||
* phone itself captures the audio; the BLE glasses stream must go through
|
||||
* {@link #process(byte[])} instead. The caller keeps the returned effects
|
||||
* for the life of the recording and release()s them with it.
|
||||
*/
|
||||
public static AudioEffect[] attachPlatformEffects(int audioSessionId) {
|
||||
ArrayList<AudioEffect> effects = new ArrayList<>();
|
||||
try {
|
||||
if (NoiseSuppressor.isAvailable()) {
|
||||
NoiseSuppressor ns = NoiseSuppressor.create(audioSessionId);
|
||||
if (ns != null) {
|
||||
ns.setEnabled(true);
|
||||
effects.add(ns);
|
||||
}
|
||||
}
|
||||
if (AcousticEchoCanceler.isAvailable()) {
|
||||
AcousticEchoCanceler aec = AcousticEchoCanceler.create(audioSessionId);
|
||||
if (aec != null) {
|
||||
aec.setEnabled(true);
|
||||
effects.add(aec);
|
||||
}
|
||||
}
|
||||
if (AutomaticGainControl.isAvailable()) {
|
||||
AutomaticGainControl agc = AutomaticGainControl.create(audioSessionId);
|
||||
if (agc != null) {
|
||||
agc.setEnabled(true);
|
||||
effects.add(agc);
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
// A missing effect just means this tier is unavailable.
|
||||
}
|
||||
return effects.toArray(new AudioEffect[0]);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Phone-side alarm actions (dismiss / snooze), delivered to the JS engine; implemented in JS. */
|
||||
public interface FaceclawAlarmListener {
|
||||
void onPhoneAction(long id, String action, int minutes);
|
||||
}
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public interface FaceclawAmbientLightListener {
|
||||
/**
|
||||
* A CFW ambient-light report (settings-channel field 105) from the master
|
||||
* temple. body is the raw 24-byte ['A','L',ver,reason,...] record; it is
|
||||
* decoded on the TS side (app/native/ambient-light.ts).
|
||||
*/
|
||||
void onAmbientLight(byte[] body);
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public interface FaceclawAudioPacketListener {
|
||||
void onAudioPacket(byte[] data, String arm, long arrivalMs);
|
||||
}
|
||||
@@ -154,7 +154,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private int lastAudioControlAckMagic = 0;
|
||||
|
||||
private ConnectionOptions connectionOptions = new ConnectionOptions();
|
||||
private final BleMagicPool magicPool = new BleMagicPool();
|
||||
private final BleMagicPool magicPool = new BleMagicPool(AndroidProtocolPlatform.INSTANCE);
|
||||
private MessageBuilder messageBuilder = new MessageBuilder(magicPool);
|
||||
private int nextTransportSeq = 0x40;
|
||||
private int nextMapSessionId = 0;
|
||||
@@ -267,7 +267,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private final TextureCacheState textureCache = new TextureCacheState();
|
||||
|
||||
private final ArrayDeque<OutboundMessage> pendingMessages = new ArrayDeque<>();
|
||||
private final CfwTransport[] cfwTransports = { new CfwTransport(), new CfwTransport() };
|
||||
private final CfwTransport[] cfwTransports = { new CfwTransport(AndroidProtocolPlatform.INSTANCE), new CfwTransport(AndroidProtocolPlatform.INSTANCE) };
|
||||
private final ArrayDeque<OutboundMessage> inFlightMessages = new ArrayDeque<>();
|
||||
private OutboundMessage prewrittenMessage;
|
||||
private List<byte[]> prewrittenFrames = Collections.emptyList();
|
||||
@@ -360,6 +360,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
activeInstance = null;
|
||||
}
|
||||
disconnect();
|
||||
for (CfwTransport transport : cfwTransports) transport.close();
|
||||
if (phoneLockReceiverRegistered) {
|
||||
phoneLockReceiverRegistered = false;
|
||||
appContext.unregisterReceiver(phoneLockReceiver);
|
||||
@@ -1231,7 +1232,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
+ (glyphs == null ? " (no glyph draws)" : ""));
|
||||
FrameTimings.getInstance().spanStart(frameId, "composite");
|
||||
SurfaceCompositor.Composite composite = compositor.applyAndComposite(
|
||||
surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint, glyphs);
|
||||
surfaceId, new AndroidByteReader(pixels8bpp), rectX, rectY, rectWidth, rectHeight, contentFingerprint,
|
||||
glyphs == null ? null : new AndroidByteReader(glyphs));
|
||||
FrameTimings.getInstance().spanEnd(frameId, "composite");
|
||||
// Pack the composited 8bpp buffer down to the headerless 4bpp frame
|
||||
// format the wire planners consume; BMP framing is added later only for
|
||||
@@ -2622,7 +2624,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
writeAddress,
|
||||
BleProtocol.WRITE_CHAR_UUID,
|
||||
frames,
|
||||
ConnectionOptions.WRITE_TYPE,
|
||||
AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE),
|
||||
ConnectionOptions.WRITE_TIMEOUT_MS
|
||||
);
|
||||
|
||||
@@ -2669,7 +2671,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
writeAddress,
|
||||
BleProtocol.WRITE_CHAR_UUID,
|
||||
prefixFrames,
|
||||
ConnectionOptions.WRITE_TYPE,
|
||||
AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE),
|
||||
ConnectionOptions.WRITE_TIMEOUT_MS
|
||||
);
|
||||
if (!result) {
|
||||
@@ -2705,7 +2707,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
writeAddress,
|
||||
BleProtocol.WRITE_CHAR_UUID,
|
||||
Collections.singletonList(finalFrame),
|
||||
ConnectionOptions.WRITE_TYPE,
|
||||
AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE),
|
||||
ConnectionOptions.WRITE_TIMEOUT_MS
|
||||
);
|
||||
}
|
||||
@@ -3433,7 +3435,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (operation == BleProtocol.FACECLAW_WAKE_OP_ACQUIRE) {
|
||||
lastFaceclawWakeLeaseQueuedAtMs = SystemClock.elapsedRealtime();
|
||||
}
|
||||
Runnable onSent = () -> {
|
||||
MessageCallback onSent = () -> {
|
||||
if (faceclawWakeControlGeneration == generation) {
|
||||
faceclawWakeControlSentCount += 1;
|
||||
}
|
||||
@@ -3489,7 +3491,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (operation == BleProtocol.FACECLAW_FB_OP_ACQUIRE) {
|
||||
lastFaceclawFramebufferLeaseQueuedAtMs = SystemClock.elapsedRealtime();
|
||||
}
|
||||
Runnable onSent = () -> {
|
||||
MessageCallback onSent = () -> {
|
||||
if (faceclawFramebufferControlGeneration == generation) {
|
||||
faceclawFramebufferControlSentCount += 1;
|
||||
}
|
||||
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public interface FaceclawBleCommunicatorListener {
|
||||
void onLog(String line);
|
||||
void onStateChange(String phase, String status);
|
||||
void onRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId);
|
||||
void onBatteryState(int headsetBattery, int headsetCharging, int ringBattery, int ringCharging);
|
||||
void onSilentMode(boolean silent);
|
||||
void onWearState(boolean wearing);
|
||||
void onPhoneLockState(boolean locked);
|
||||
void onEvenAppConflict(String message);
|
||||
void onFrameMetrics(int paintMs, int transmitMs, int tileCount);
|
||||
void onFrameFinished(int frameId, String outcome);
|
||||
void onFirmwareInfo(String leftVersion, String rightVersion, String extension);
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public interface FaceclawBleListener {
|
||||
void onNotification(String address, String characteristicUuid, byte[] data);
|
||||
void onConnectionStateChange(String address, boolean connected);
|
||||
}
|
||||
@@ -268,7 +268,7 @@ public class FaceclawBleManager {
|
||||
return false;
|
||||
}
|
||||
|
||||
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(BleProtocol.CCCD_UUID);
|
||||
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(java.util.UUID.fromString(BleProtocol.CCCD_UUID));
|
||||
if (descriptor == null) {
|
||||
descriptorLatches.remove(address);
|
||||
return true;
|
||||
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public interface FaceclawCaptionEngineListener {
|
||||
/**
|
||||
* One finished utterance. text may be empty when no ASR model is loaded
|
||||
* or recognition produced nothing; embedding is the L2-normalized
|
||||
* voice-print or null when the speaker model is unavailable or the
|
||||
* utterance was too short. startMs/endMs are on the engine's sample
|
||||
* clock (milliseconds of audio since start()).
|
||||
*/
|
||||
void onUtterance(String text, float[] embedding, long startMs, long endMs, double peakRms);
|
||||
|
||||
/** A speech onset was detected; the utterance is now being collected. */
|
||||
void onSpeechStart(long startMs);
|
||||
|
||||
void onStatus(String status);
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Receives stock compass heading and calibration notifications on the thread that subscribed. */
|
||||
public interface FaceclawCompassListener {
|
||||
void onCompassEvent(int command, int headingDegrees, int magneticAccuracy, int magneticAnomalies,
|
||||
int orientationSource, int diagnosticFlags, long sampleTimeMs);
|
||||
}
|
||||
-18
@@ -1,18 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Callbacks from FaceclawDeviceDiscovery's live scan to the TypeScript layer. */
|
||||
public interface FaceclawDeviceDiscoveryListener {
|
||||
/**
|
||||
* One advertisement (or one bonded device) as a JSON object:
|
||||
* {address, name, manufacturerData (hex, company id included), rssi|null,
|
||||
* txPower|null, connectable|null, bonded, source, seenAtMs}.
|
||||
* Protocol decoding happens in TypeScript (see app/g2/even-advertisement.ts).
|
||||
*/
|
||||
void onAdvertisement(String json);
|
||||
|
||||
/** The platform refused to start or continue the scan. */
|
||||
void onScanFailed(int errorCode, String message);
|
||||
|
||||
/** Diagnostic line for the pairing log. */
|
||||
void onLog(String line);
|
||||
}
|
||||
@@ -362,7 +362,7 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener {
|
||||
boolean written;
|
||||
try {
|
||||
written = bleManager.writeFrames(
|
||||
address, BleProtocol.WRITE_CHAR_UUID, frames, ConnectionOptions.WRITE_TYPE, ConnectionOptions.WRITE_TIMEOUT_MS);
|
||||
address, BleProtocol.WRITE_CHAR_UUID, frames, AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), ConnectionOptions.WRITE_TIMEOUT_MS);
|
||||
} catch (IllegalStateException e) {
|
||||
// "Not connected" — the link went away underneath us.
|
||||
written = false;
|
||||
@@ -475,7 +475,7 @@ public class FaceclawDeviceInfoProbe implements FaceclawBleListener {
|
||||
address, sid, flag, magic, seq, payload.length));
|
||||
List<byte[]> frames = BleProtocol.framePb(payload, sid, flag, seq);
|
||||
boolean written = bleManager.writeFrames(
|
||||
address, BleProtocol.WRITE_CHAR_UUID, frames, ConnectionOptions.WRITE_TYPE, ConnectionOptions.WRITE_TIMEOUT_MS);
|
||||
address, BleProtocol.WRITE_CHAR_UUID, frames, AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), ConnectionOptions.WRITE_TIMEOUT_MS);
|
||||
if (!written) {
|
||||
emitLog("tx write FAILED sid=0x" + Integer.toHexString(sid));
|
||||
synchronized (lock) {
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/**
|
||||
* Callbacks from a hosted EvenHub app's WebView to the TypeScript host.
|
||||
* All methods are delivered on the Android main thread (NativeScript's JS
|
||||
* thread), regardless of which WebView-internal thread produced them.
|
||||
*/
|
||||
public interface FaceclawEvenHubListener {
|
||||
/**
|
||||
* The app invoked window.flutter_inappwebview.callHandler(handlerName, ...args).
|
||||
* argsJson is the JSON-encoded argument array; callId identifies the
|
||||
* promise to resolve via window.__fcResolve(callId, ok, value).
|
||||
*/
|
||||
void onEvenAppMessage(String handlerName, String argsJson, int callId);
|
||||
|
||||
/** The WebView finished loading the app's page. */
|
||||
void onPageFinished(String url);
|
||||
}
|
||||
@@ -301,7 +301,7 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener {
|
||||
private boolean writeOta(String address, String writeChar, int sid, byte[] payload, int seq) {
|
||||
List<byte[]> frames = BleProtocol.framePb(payload, sid, FLAG_OTA, seq);
|
||||
return bleManager.writeFrames(
|
||||
address, writeChar, frames, ConnectionOptions.WRITE_TYPE, ConnectionOptions.WRITE_TIMEOUT_MS);
|
||||
address, writeChar, frames, AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE), ConnectionOptions.WRITE_TIMEOUT_MS);
|
||||
}
|
||||
|
||||
private int waitAck(int wantOp, int timeoutMs) throws TimeoutException {
|
||||
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Callbacks from FaceclawFirmwareFlasher to the TypeScript layer. */
|
||||
public interface FaceclawFirmwareFlasherListener {
|
||||
void onLog(String line);
|
||||
|
||||
/**
|
||||
* Fine-grained progress for the UI bar. `lens` is "left"/"right";
|
||||
* component/block indices are 1-based against their counts. `bytesSent` /
|
||||
* `bytesTotal` cover the whole lens image, so a bar driven by them moves
|
||||
* evenly even though the components differ wildly in size.
|
||||
*/
|
||||
void onProgress(String lens, int componentIndex, int componentCount, int blockIndex, int blockCount,
|
||||
long bytesSent, long bytesTotal);
|
||||
|
||||
/**
|
||||
* Coarse lifecycle. `state` is one of: validating, connecting, flashing,
|
||||
* rebooting, done, error. `detail` carries the lens name or a message.
|
||||
*/
|
||||
void onState(String state, String detail);
|
||||
|
||||
/** Terminal callback: success or failure, with an explanatory message. */
|
||||
void onComplete(boolean success, String detail);
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
* Native helpers for the on-device custom-firmware build: SHA-256 hashing and
|
||||
* binary file writing. Both take a java.nio.ByteBuffer so NativeScript can
|
||||
* marshal a JS ArrayBuffer straight across without a per-element copy of the
|
||||
* ~4 MB firmware image (and with no overload ambiguity). Also home of the one
|
||||
* shared byte→hex encoder for com.faceclaw.app.
|
||||
*/
|
||||
public final class FaceclawFirmwareUtil {
|
||||
private FaceclawFirmwareUtil() {}
|
||||
|
||||
/** Lowercase hex of the bytes. The TS-side decoder is case-insensitive. */
|
||||
public static String bytesToHex(byte[] bytes) {
|
||||
if (bytes == null || bytes.length == 0) {
|
||||
return "";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder(bytes.length * 2);
|
||||
for (byte b : bytes) {
|
||||
int v = b & 0xff;
|
||||
sb.append(Character.forDigit(v >>> 4, 16));
|
||||
sb.append(Character.forDigit(v & 0x0f, 16));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/** Lowercase hex SHA-256 of the buffer's remaining bytes. */
|
||||
public static String sha256Hex(ByteBuffer data) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
md.update(data.duplicate());
|
||||
return bytesToHex(md.digest());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("SHA-256 unavailable", e);
|
||||
}
|
||||
}
|
||||
|
||||
/** Write the buffer's remaining bytes to `path`, overwriting any existing file. */
|
||||
public static void writeFile(String path, ByteBuffer data) {
|
||||
try (FileOutputStream fos = new FileOutputStream(path);
|
||||
FileChannel channel = fos.getChannel()) {
|
||||
ByteBuffer view = data.duplicate();
|
||||
while (view.hasRemaining()) {
|
||||
channel.write(view);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("firmware write failed: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -362,7 +362,7 @@ public class FaceclawFlashPromptCommunicator implements FaceclawBleListener {
|
||||
address,
|
||||
BleProtocol.WRITE_CHAR_UUID,
|
||||
frames,
|
||||
ConnectionOptions.WRITE_TYPE,
|
||||
AndroidProtocolPlatform.writeType(ConnectionOptions.WRITE_MODE),
|
||||
ConnectionOptions.WRITE_TIMEOUT_MS);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Callbacks from FaceclawFlashPromptCommunicator to the TypeScript layer. */
|
||||
public interface FaceclawFlashPromptListener {
|
||||
void onLog(String line);
|
||||
|
||||
/**
|
||||
* Lifecycle updates. `state` is one of: connecting, connected, prompting,
|
||||
* battery, result, cancelled, timeout, disconnected, error. `detail` carries the
|
||||
* "approved"/"declined" text for result, or an error/explanation message.
|
||||
*/
|
||||
void onState(String state, String detail);
|
||||
|
||||
/**
|
||||
* Battery percent of each arm, read after the user approved (or right
|
||||
* after auth when the prompt is skipped); -1 when an arm didn't answer.
|
||||
* Always fires before onResult(true).
|
||||
*/
|
||||
void onBattery(int rightPercent, int leftPercent);
|
||||
|
||||
/** Fires once the user picks a menu row (or, with skipPrompt, once the battery is read): true = flash, false = cancel. */
|
||||
void onResult(boolean approved);
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/**
|
||||
* Receives IMU (accelerometer) readings decoded from the glasses' sys-event
|
||||
* stream. Registered on FaceclawBleCommunicator via addImuListener; callbacks
|
||||
* are delivered on the Android main thread.
|
||||
*/
|
||||
public interface FaceclawImuListener {
|
||||
void onImuData(double x, double y, double z, int eventSource);
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/**
|
||||
* Callbacks for one on-phone LLM generation. Like the other bridge listeners,
|
||||
* implementations are typically created on the NativeScript JS thread and
|
||||
* FaceclawLlamaRunner posts these callbacks back to that thread's Looper.
|
||||
*/
|
||||
public interface FaceclawLlamaListener {
|
||||
/** A piece of generated text (may be a partial word). */
|
||||
void onToken(String piece);
|
||||
|
||||
/** Generation finished: "stop" (EOG), "length", or "cancelled". */
|
||||
void onDone(String stopReason);
|
||||
|
||||
void onError(String message);
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** One-shot Android location callbacks for the TypeScript Weather bridge. */
|
||||
public interface FaceclawLocationListener {
|
||||
void onLocation(double latitude, double longitude, float accuracyMeters, long timestampMs);
|
||||
|
||||
void onError(String message);
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Continuous location callbacks for the TypeScript navigation bridge. */
|
||||
public interface FaceclawLocationTrackerListener {
|
||||
/**
|
||||
* A new fix. bearingDeg/speedMps are -1 when the fix doesn't carry them
|
||||
* (common when stationary); accuracyMeters is -1 when unknown.
|
||||
*/
|
||||
void onLocation(double latitude, double longitude, float accuracyMeters,
|
||||
float bearingDeg, float speedMps, long timestampMs);
|
||||
|
||||
void onError(String message);
|
||||
}
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public interface FaceclawMediaBrowserListener {
|
||||
void onConnectResult(int requestId, boolean connected, String rootId, String error);
|
||||
|
||||
void onBrowseResult(int requestId, String childrenJson, String error);
|
||||
|
||||
/** The service connection dropped after connecting (player crashed or was killed). */
|
||||
void onDisconnected();
|
||||
}
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public interface FaceclawMediaControllerListener {
|
||||
/** All observed session owners, before filtering or choosing the active player. */
|
||||
void onSessionAppsChanged(String appsJson);
|
||||
|
||||
void onStateChange(
|
||||
String playbackState,
|
||||
String packageName,
|
||||
String appName,
|
||||
String title,
|
||||
String artist,
|
||||
String album,
|
||||
long positionMs,
|
||||
long durationMs,
|
||||
float playbackSpeed,
|
||||
boolean canPlayPause,
|
||||
boolean canSkipNext,
|
||||
boolean canSkipPrevious,
|
||||
boolean accessEnabled,
|
||||
String status
|
||||
);
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public interface FaceclawMicStatusListener {
|
||||
/**
|
||||
* A CFW mic_control field-104 status record from one temple. body is the
|
||||
* raw 21-byte ['M','C',ver,...] record (decoded on the TS side); arm is
|
||||
* "L" or "R" for the temple's own BLE link, "?" if unmatched.
|
||||
*/
|
||||
void onMicStatus(byte[] body, String arm);
|
||||
}
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Progress callbacks for FaceclawModelDownloader, posted to the creating thread's Looper. */
|
||||
public interface FaceclawModelDownloaderListener {
|
||||
void onProgress(long bytesDownloaded, long totalBytes);
|
||||
|
||||
/** The file is fully downloaded, verified, and moved to its final path. */
|
||||
void onDone(String path);
|
||||
|
||||
void onError(String message);
|
||||
}
|
||||
@@ -1,266 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.media.audiofx.AcousticEchoCanceler;
|
||||
import android.media.audiofx.AudioEffect;
|
||||
import android.media.audiofx.AutomaticGainControl;
|
||||
import android.media.audiofx.NoiseSuppressor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Phone-side noise suppression for the microphone stream.
|
||||
*
|
||||
* Android's built-in noise cancellation (the {@link NoiseSuppressor} capture
|
||||
* effect) only attaches to AudioRecord sessions on the phone's own microphones. Faceclaw's audio
|
||||
* arrives as raw PCM over BLE from the glasses, which that effect chain never
|
||||
* sees, so this class picks the best engine that can actually reach the
|
||||
* samples:
|
||||
*
|
||||
* - Audio captured by the phone itself: {@link #attachPlatformEffects(int)}
|
||||
* enables the device's built-in NS/AEC/AGC chain (hardware or OEM software,
|
||||
* whatever the Android build ships) on the AudioRecord session.
|
||||
* - Audio from the glasses (the normal case): {@link #process(byte[])} runs a
|
||||
* WebRTC-NS-family spectral suppressor here on the phone — 16 ms sqrt-Hann
|
||||
* frames at 50% overlap, per-bin asymmetric noise-floor tracking, and an
|
||||
* over-subtracted Wiener gain with a residual floor so speech never pumps
|
||||
* in and out of silence.
|
||||
*/
|
||||
public final class FaceclawNoiseSuppressor {
|
||||
|
||||
private static final int FFT_SIZE = 256;
|
||||
private static final int HOP = FFT_SIZE / 2;
|
||||
private static final float OVERSUBTRACTION = 1.6f;
|
||||
/** About -16 dB of residual: keeps the noise bed natural instead of gated. */
|
||||
private static final float GAIN_FLOOR = 0.15f;
|
||||
private static final float GAIN_SMOOTHING = 0.6f;
|
||||
/** Frames of pure noise-floor learning after start/reset (~100 ms). */
|
||||
private static final int NOISE_WARMUP_FRAMES = 12;
|
||||
|
||||
private final int sampleRate;
|
||||
private final float[] window = new float[FFT_SIZE];
|
||||
private final float[] history = new float[FFT_SIZE];
|
||||
private final float[] overlap = new float[HOP];
|
||||
private final float[] noise = new float[FFT_SIZE / 2 + 1];
|
||||
private final float[] gain = new float[FFT_SIZE / 2 + 1];
|
||||
private final float[] re = new float[FFT_SIZE];
|
||||
private final float[] im = new float[FFT_SIZE];
|
||||
private final short[] pending = new short[HOP];
|
||||
private int pendingCount = 0;
|
||||
private int warmupFrames = 0;
|
||||
|
||||
public FaceclawNoiseSuppressor(int sampleRate) {
|
||||
this.sampleRate = sampleRate;
|
||||
// sqrt-Hann (sine) analysis+synthesis windows: exact COLA at 50% overlap.
|
||||
for (int i = 0; i < FFT_SIZE; i++) {
|
||||
window[i] = (float) Math.sin(Math.PI * (i + 0.5) / FFT_SIZE);
|
||||
}
|
||||
reset();
|
||||
}
|
||||
|
||||
public int getSampleRate() {
|
||||
return sampleRate;
|
||||
}
|
||||
|
||||
/** Forget the learned noise floor (e.g. after the suppressor was toggled off). */
|
||||
public void reset() {
|
||||
Arrays.fill(history, 0f);
|
||||
Arrays.fill(overlap, 0f);
|
||||
Arrays.fill(noise, 0f);
|
||||
Arrays.fill(gain, 1f);
|
||||
pendingCount = 0;
|
||||
warmupFrames = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Suppress noise in 16-bit little-endian mono PCM. Streaming: input not
|
||||
* filling a whole 8 ms hop is carried into the next call, so the output
|
||||
* may be up to one hop shorter than the input (and longer on a later
|
||||
* call); total latency is one hop.
|
||||
*/
|
||||
public byte[] process(byte[] pcm16le) {
|
||||
if (pcm16le == null || pcm16le.length < 2) {
|
||||
return pcm16le;
|
||||
}
|
||||
int sampleCount = pcm16le.length / 2;
|
||||
int hops = (pendingCount + sampleCount) / HOP;
|
||||
byte[] out = new byte[hops * HOP * 2];
|
||||
int outPos = 0;
|
||||
int inPos = 0;
|
||||
while (inPos < sampleCount) {
|
||||
int take = Math.min(HOP - pendingCount, sampleCount - inPos);
|
||||
for (int i = 0; i < take; i++) {
|
||||
int lo = pcm16le[(inPos + i) * 2] & 0xff;
|
||||
int hi = pcm16le[(inPos + i) * 2 + 1];
|
||||
pending[pendingCount + i] = (short) ((hi << 8) | lo);
|
||||
}
|
||||
pendingCount += take;
|
||||
inPos += take;
|
||||
if (pendingCount == HOP) {
|
||||
outPos = processHop(out, outPos);
|
||||
pendingCount = 0;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private int processHop(byte[] out, int outPos) {
|
||||
System.arraycopy(history, HOP, history, 0, FFT_SIZE - HOP);
|
||||
for (int i = 0; i < HOP; i++) {
|
||||
history[FFT_SIZE - HOP + i] = pending[i] / 32768f;
|
||||
}
|
||||
for (int i = 0; i < FFT_SIZE; i++) {
|
||||
re[i] = history[i] * window[i];
|
||||
im[i] = 0f;
|
||||
}
|
||||
fft(re, im, false);
|
||||
|
||||
boolean warming = warmupFrames < NOISE_WARMUP_FRAMES;
|
||||
if (warming) {
|
||||
warmupFrames++;
|
||||
}
|
||||
for (int b = 0; b <= FFT_SIZE / 2; b++) {
|
||||
float power = re[b] * re[b] + im[b] * im[b];
|
||||
if (warming) {
|
||||
noise[b] = warmupFrames == 1 ? power : noise[b] + (power - noise[b]) / warmupFrames;
|
||||
} else if (power < noise[b]) {
|
||||
// Track the floor down quickly; drift up slowly so speech
|
||||
// energy never gets learned as noise.
|
||||
noise[b] += (power - noise[b]) * 0.2f;
|
||||
} else {
|
||||
noise[b] = Math.min(power, noise[b] * 1.006f + 1e-10f);
|
||||
}
|
||||
float clean = power - OVERSUBTRACTION * noise[b];
|
||||
float g = clean > 0f ? clean / power : 0f;
|
||||
if (g < GAIN_FLOOR) {
|
||||
g = GAIN_FLOOR;
|
||||
}
|
||||
g = GAIN_SMOOTHING * gain[b] + (1f - GAIN_SMOOTHING) * g;
|
||||
gain[b] = g;
|
||||
re[b] *= g;
|
||||
im[b] *= g;
|
||||
if (b > 0 && b < FFT_SIZE / 2) {
|
||||
re[FFT_SIZE - b] *= g;
|
||||
im[FFT_SIZE - b] *= g;
|
||||
}
|
||||
}
|
||||
|
||||
fft(re, im, true);
|
||||
for (int i = 0; i < HOP; i++) {
|
||||
float value = (overlap[i] + re[i] * window[i]) * 32767f;
|
||||
int sample = Math.round(value);
|
||||
if (sample > 32767) {
|
||||
sample = 32767;
|
||||
}
|
||||
if (sample < -32768) {
|
||||
sample = -32768;
|
||||
}
|
||||
out[outPos + i * 2] = (byte) (sample & 0xff);
|
||||
out[outPos + i * 2 + 1] = (byte) ((sample >> 8) & 0xff);
|
||||
}
|
||||
for (int i = 0; i < HOP; i++) {
|
||||
overlap[i] = re[HOP + i] * window[HOP + i];
|
||||
}
|
||||
return outPos + HOP * 2;
|
||||
}
|
||||
|
||||
/** In-place iterative radix-2 FFT (inverse includes the 1/N scale). */
|
||||
private static void fft(float[] re, float[] im, boolean inverse) {
|
||||
int n = re.length;
|
||||
for (int i = 1, j = 0; i < n; i++) {
|
||||
int bit = n >> 1;
|
||||
for (; (j & bit) != 0; bit >>= 1) {
|
||||
j ^= bit;
|
||||
}
|
||||
j ^= bit;
|
||||
if (i < j) {
|
||||
float t = re[i];
|
||||
re[i] = re[j];
|
||||
re[j] = t;
|
||||
t = im[i];
|
||||
im[i] = im[j];
|
||||
im[j] = t;
|
||||
}
|
||||
}
|
||||
for (int len = 2; len <= n; len <<= 1) {
|
||||
double ang = 2 * Math.PI / len * (inverse ? 1 : -1);
|
||||
float wRe = (float) Math.cos(ang);
|
||||
float wIm = (float) Math.sin(ang);
|
||||
for (int i = 0; i < n; i += len) {
|
||||
float curRe = 1f;
|
||||
float curIm = 0f;
|
||||
for (int k = 0; k < len / 2; k++) {
|
||||
int a = i + k;
|
||||
int b = a + len / 2;
|
||||
float xr = re[b] * curRe - im[b] * curIm;
|
||||
float xi = re[b] * curIm + im[b] * curRe;
|
||||
re[b] = re[a] - xr;
|
||||
im[b] = im[a] - xi;
|
||||
re[a] += xr;
|
||||
im[a] += xi;
|
||||
float nextRe = curRe * wRe - curIm * wIm;
|
||||
curIm = curRe * wIm + curIm * wRe;
|
||||
curRe = nextRe;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (inverse) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
re[i] /= n;
|
||||
im[i] /= n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether this Android build ships the built-in capture noise suppressor. */
|
||||
public static boolean platformSuppressorAvailable() {
|
||||
try {
|
||||
return NoiseSuppressor.isAvailable();
|
||||
} catch (Throwable t) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Short engine name for the UI. */
|
||||
public static String engineDescription() {
|
||||
return "spectral (phone DSP)";
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach the platform's built-in capture-effect chain (NoiseSuppressor,
|
||||
* AcousticEchoCanceler, AutomaticGainControl — whichever this Android
|
||||
* build provides) to a phone AudioRecord session. Only applicable when the
|
||||
* phone itself captures the audio; the BLE glasses stream must go through
|
||||
* {@link #process(byte[])} instead. The caller keeps the returned effects
|
||||
* for the life of the recording and release()s them with it.
|
||||
*/
|
||||
public static AudioEffect[] attachPlatformEffects(int audioSessionId) {
|
||||
ArrayList<AudioEffect> effects = new ArrayList<>();
|
||||
try {
|
||||
if (NoiseSuppressor.isAvailable()) {
|
||||
NoiseSuppressor ns = NoiseSuppressor.create(audioSessionId);
|
||||
if (ns != null) {
|
||||
ns.setEnabled(true);
|
||||
effects.add(ns);
|
||||
}
|
||||
}
|
||||
if (AcousticEchoCanceler.isAvailable()) {
|
||||
AcousticEchoCanceler aec = AcousticEchoCanceler.create(audioSessionId);
|
||||
if (aec != null) {
|
||||
aec.setEnabled(true);
|
||||
effects.add(aec);
|
||||
}
|
||||
}
|
||||
if (AutomaticGainControl.isAvailable()) {
|
||||
AutomaticGainControl agc = AutomaticGainControl.create(audioSessionId);
|
||||
if (agc != null) {
|
||||
agc.setEnabled(true);
|
||||
effects.add(agc);
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
// A missing effect just means this tier is unavailable.
|
||||
}
|
||||
return effects.toArray(new AudioEffect[0]);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public interface FaceclawNotificationListener {
|
||||
void onNotificationPosted(String key);
|
||||
}
|
||||
@@ -105,7 +105,8 @@ public final class FaceclawPreviewCompositor {
|
||||
java.nio.ByteBuffer glyphs
|
||||
) {
|
||||
compositor.applyAndComposite(
|
||||
surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint, glyphs);
|
||||
surfaceId, new AndroidByteReader(pixels8bpp), rectX, rectY, rectWidth, rectHeight, contentFingerprint,
|
||||
glyphs == null ? null : new AndroidByteReader(glyphs));
|
||||
FrameTimings.getInstance().finishFrame(frameId, "composited (preview-only, no glasses)");
|
||||
Runnable listener = frameListener;
|
||||
if (listener != null) {
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Callbacks for one FaceclawQrScanner.scan; exactly one method fires. */
|
||||
public interface FaceclawQrScannerListener {
|
||||
void onResult(String text);
|
||||
|
||||
void onCancelled();
|
||||
|
||||
void onError(String message);
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
final class FaceclawRingEventDecoder {
|
||||
private FaceclawRingEventDecoder() {
|
||||
}
|
||||
|
||||
static DirectRingEvent decode(byte[] raw) {
|
||||
if (raw == null || raw.length == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
DirectRingEvent chargerGesture = decodeChargerGesture(raw);
|
||||
if (chargerGesture != null) {
|
||||
return chargerGesture;
|
||||
}
|
||||
|
||||
return decodeDirectGesture(raw);
|
||||
}
|
||||
|
||||
private static DirectRingEvent decodeChargerGesture(byte[] raw) {
|
||||
if (raw.length != 11) {
|
||||
return null;
|
||||
}
|
||||
if (u8(raw[0]) != 0x00 || u8(raw[1]) != 0x09 || u8(raw[2]) != 0x61 || u8(raw[3]) != 0x00) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int code = u8(raw[4]);
|
||||
int param = u8(raw[5]) | (u8(raw[6]) << 8);
|
||||
long tick = (u8(raw[7]) | (u8(raw[8]) << 8) | (u8(raw[9]) << 16) | (u8(raw[10]) << 24)) & 0xffffffffL;
|
||||
String detail = String.format(Locale.US, "code=0x%02x(%s) param=0x%04x tick=%d", code, chargerCodeName(code), param, tick);
|
||||
|
||||
switch (code) {
|
||||
case 0x00:
|
||||
return event(BleProtocol.EVENT_RING_LONG_PRESS, "LONG_PRESS", detail);
|
||||
case 0x01:
|
||||
return event(BleProtocol.EVENT_CLICK, "TAP", detail);
|
||||
case 0x02:
|
||||
return event(BleProtocol.EVENT_DOUBLE_CLICK, "DOUBLE_TAP", detail);
|
||||
case 0x04:
|
||||
return event(BleProtocol.EVENT_SCROLL_TOP, "SWIPE_UP", detail);
|
||||
case 0x05:
|
||||
return event(BleProtocol.EVENT_SCROLL_BOTTOM, "SWIPE_DOWN", detail);
|
||||
case 0x08:
|
||||
return event(BleProtocol.EVENT_RING_LONG_PRESS_RELEASE, "LONG_PRESS_RELEASE", detail);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static DirectRingEvent decodeDirectGesture(byte[] raw) {
|
||||
if (raw.length != 3 || u8(raw[0]) != 0xff) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int type = u8(raw[1]);
|
||||
int param = u8(raw[2]);
|
||||
String detail = String.format(Locale.US, "type=0x%02x param=0x%02x", type, param);
|
||||
|
||||
if (type == 0x03 && param == 0x20) {
|
||||
return event(BleProtocol.EVENT_RING_LONG_PRESS, "HOLD", detail);
|
||||
}
|
||||
if (type == 0x04 && param == 0x01) {
|
||||
return event(BleProtocol.EVENT_CLICK, "TAP_SINGLE", detail);
|
||||
}
|
||||
if (type == 0x04 && param == 0x02) {
|
||||
return event(BleProtocol.EVENT_DOUBLE_CLICK, "TAP_DOUBLE", detail);
|
||||
}
|
||||
if (type == 0x05) {
|
||||
if (param <= 0x01) {
|
||||
return event(BleProtocol.EVENT_SCROLL_BOTTOM, "SWIPE_FORWARD", detail);
|
||||
}
|
||||
return event(BleProtocol.EVENT_SCROLL_TOP, "SWIPE_BACKWARD", detail);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static DirectRingEvent event(int eventType, String label, String detail) {
|
||||
G2Event event = new G2Event(
|
||||
"sys-event",
|
||||
"",
|
||||
eventType,
|
||||
BleProtocol.EVENT_SOURCE_RING,
|
||||
0
|
||||
);
|
||||
return new DirectRingEvent(event, label, detail);
|
||||
}
|
||||
|
||||
private static String chargerCodeName(int code) {
|
||||
switch (code) {
|
||||
case 0x00:
|
||||
return "LONG_PRESS";
|
||||
case 0x01:
|
||||
return "TAP";
|
||||
case 0x02:
|
||||
return "DOUBLE_TAP";
|
||||
case 0x04:
|
||||
return "SWIPE_UP";
|
||||
case 0x05:
|
||||
return "SWIPE_DOWN";
|
||||
case 0x08:
|
||||
return "LONG_PRESS_RELEASE";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
private static int u8(byte value) {
|
||||
return value & 0xff;
|
||||
}
|
||||
|
||||
static final class DirectRingEvent {
|
||||
final G2Event event;
|
||||
final String label;
|
||||
final String detail;
|
||||
|
||||
DirectRingEvent(G2Event event, String label, String detail) {
|
||||
this.event = event;
|
||||
this.label = label;
|
||||
this.detail = detail;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Change-notification callback for FaceclawSettings; implemented in JS. */
|
||||
public interface FaceclawSettingsListener {
|
||||
void onSettingChanged(String key);
|
||||
}
|
||||
-47
@@ -1,47 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public interface FaceclawVoiceControllerListener {
|
||||
void onStatus(String status);
|
||||
|
||||
/**
|
||||
* Best transcript of the current utterance so far. REPLACE semantics: text
|
||||
* is the complete transcript, not a delta — display it verbatim, replacing
|
||||
* any previous partial. isFinal marks the end of the utterance.
|
||||
*/
|
||||
void onTranscript(String text, boolean isFinal);
|
||||
|
||||
/** Capture has finished, after the final transcript or a startup/recognition error. */
|
||||
void onStopped(int captureId);
|
||||
|
||||
/**
|
||||
* Decoded microphone audio: 16 kHz mono signed 16-bit little-endian PCM.
|
||||
* Emitted in every mode so raw-PCM consumers (levels, recording, captions)
|
||||
* keep working while the onboard recognizer owns the transcript.
|
||||
*/
|
||||
void onPcm(byte[] pcm16le);
|
||||
|
||||
/**
|
||||
* Per-packet DSP metadata the glasses firmware appends to each 50 ms audio
|
||||
* packet, computed on the raw stereo capture before its mono downmix: the
|
||||
* direction-of-arrival in signed degrees (0 = straight ahead, positive to
|
||||
* the wearer's right) and a signal-strength ratio that serves as a
|
||||
* confidence/energy proxy for the angle.
|
||||
*/
|
||||
void onFrameMeta(int angleDegrees, int ssr);
|
||||
|
||||
/**
|
||||
* The speaker stopped, in a hands-free session that has no button release
|
||||
* to end it. Only fires when endpointing was enabled for the session; see
|
||||
* FaceclawVoiceController.setEndpointing.
|
||||
*/
|
||||
void onSpeechEnd();
|
||||
|
||||
/**
|
||||
* Result of speaker verification against the enrolled wearer voice-print,
|
||||
* emitted once per capture session just before the final transcript.
|
||||
* Only fires when verification was configured for the session; fails
|
||||
* open (isWearer=true) when the utterance was too short to verify or the
|
||||
* speaker model could not be loaded.
|
||||
*/
|
||||
void onSpeakerVerified(boolean isWearer, float similarity);
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* Streaming WAV writer for conversation recordings: the header is written up
|
||||
* front with zero sizes and patched on finish, so a crash mid-session leaves
|
||||
* a recoverable file (data length can be derived from the file size).
|
||||
*/
|
||||
public class FaceclawWavRecorder {
|
||||
private static final String TAG = "FaceclawWavRecorder";
|
||||
|
||||
private final String path;
|
||||
private final int sampleRate;
|
||||
private final int channels;
|
||||
private RandomAccessFile file;
|
||||
private long dataBytes;
|
||||
|
||||
public FaceclawWavRecorder(String path, int sampleRate, int channels) throws IOException {
|
||||
this.path = path;
|
||||
this.sampleRate = sampleRate;
|
||||
this.channels = channels;
|
||||
File parent = new File(path).getParentFile();
|
||||
if (parent != null && !parent.exists() && !parent.mkdirs()) {
|
||||
throw new IOException("could not create " + parent);
|
||||
}
|
||||
file = new RandomAccessFile(path, "rw");
|
||||
file.setLength(0);
|
||||
file.write(buildHeader(0));
|
||||
}
|
||||
|
||||
public synchronized void append(byte[] pcm16le) {
|
||||
if (file == null || pcm16le == null || pcm16le.length == 0) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
file.write(pcm16le);
|
||||
dataBytes += pcm16le.length;
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, "append failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized long getDurationMs() {
|
||||
return dataBytes * 1000L / (sampleRate * channels * 2L);
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
/** Patch the header and close. Returns the PCM byte count (0 = empty). */
|
||||
public synchronized long finish() {
|
||||
RandomAccessFile current = file;
|
||||
file = null;
|
||||
if (current == null) {
|
||||
return dataBytes;
|
||||
}
|
||||
try {
|
||||
current.seek(0);
|
||||
current.write(buildHeader((int) Math.min(dataBytes, Integer.MAX_VALUE)));
|
||||
current.close();
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, "finish failed", e);
|
||||
}
|
||||
return dataBytes;
|
||||
}
|
||||
|
||||
/** Delete the (finished or abandoned) recording. */
|
||||
public synchronized void discard() {
|
||||
finish();
|
||||
new File(path).delete();
|
||||
}
|
||||
|
||||
private byte[] buildHeader(int pcmBytes) {
|
||||
int byteRate = sampleRate * channels * 2;
|
||||
ByteBuffer b = ByteBuffer.allocate(44).order(ByteOrder.LITTLE_ENDIAN);
|
||||
b.put("RIFF".getBytes(StandardCharsets.US_ASCII));
|
||||
b.putInt(36 + pcmBytes);
|
||||
b.put("WAVE".getBytes(StandardCharsets.US_ASCII));
|
||||
b.put("fmt ".getBytes(StandardCharsets.US_ASCII));
|
||||
b.putInt(16);
|
||||
b.putShort((short) 1);
|
||||
b.putShort((short) channels);
|
||||
b.putInt(sampleRate);
|
||||
b.putInt(byteRate);
|
||||
b.putShort((short) (channels * 2));
|
||||
b.putShort((short) 16);
|
||||
b.put("data".getBytes(StandardCharsets.US_ASCII));
|
||||
b.putInt(pcmBytes);
|
||||
return b.array();
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/**
|
||||
* Callbacks from the Android Wear (Wear OS) bridge; implemented in JS. Every
|
||||
* call is delivered on the thread that registered the listener (see
|
||||
* FaceclawWearBridge.setListener).
|
||||
*/
|
||||
public interface FaceclawWearListener {
|
||||
/**
|
||||
* A message from the watch app. `path` is the Data Layer message path
|
||||
* (e.g. "/faceclaw/input"); `json` is the UTF-8 payload (always a JSON
|
||||
* object, "{}" when the watch sent none); `nodeId` identifies the watch
|
||||
* so a reply can be addressed to it.
|
||||
*/
|
||||
void onMessage(String path, String json, String nodeId);
|
||||
|
||||
/** The set of reachable watches running the Faceclaw watch app changed. */
|
||||
void onWatchConnection(boolean reachable, String watchName);
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
/** Callbacks for FaceclawWebSocket, delivered on the Android main thread. */
|
||||
public interface FaceclawWebSocketListener {
|
||||
void onOpen();
|
||||
void onTextMessage(String message);
|
||||
void onClosed(int code, String reason);
|
||||
void onFailure(String message);
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Process-wide registry of the glasses' builtin 20 px font's glyph rasters,
|
||||
* fed from the TS side (EvenHubFont's extracted firmware font data). Unlike
|
||||
* GlyphAtlas/ImageAtlas entries, these are never uploaded: the firmware
|
||||
* already owns the font, and mode 15 draws it directly. The planner needs
|
||||
* the rasters purely phone-side — for the "would this draw land correctly"
|
||||
* check against the composite and for punching drawn ink out of delta rects.
|
||||
*
|
||||
* Placement model matches the firmware's mode-15 math: a glyph draws at
|
||||
* (penX + ofsX, lineY + inkTop) where inkTop already folds in the root
|
||||
* font's line height and baseline. The TS side only registers glyphs whose
|
||||
* on-glasses rendering it predicts exactly (native-table glyphs; baseline-
|
||||
* shifted fallback symbols stay baked).
|
||||
*/
|
||||
public final class FwGlyphAtlas {
|
||||
private FwGlyphAtlas() {}
|
||||
|
||||
public static final class Entry {
|
||||
/** Draw x = pen x + ofsX. */
|
||||
public final int ofsX;
|
||||
/** First raster row relative to the run's line-top y. */
|
||||
public final int inkTop;
|
||||
public final int boxW;
|
||||
public final int boxH;
|
||||
/** 4bpp rows, stride ceil(boxW/2), high nibble = left pixel. */
|
||||
final byte[] nibbles;
|
||||
|
||||
Entry(int ofsX, int inkTop, int boxW, int boxH, byte[] nibbles) {
|
||||
this.ofsX = ofsX;
|
||||
this.inkTop = inkTop;
|
||||
this.boxW = boxW;
|
||||
this.boxH = boxH;
|
||||
this.nibbles = nibbles;
|
||||
}
|
||||
|
||||
/** The 4bpp source value at (col, row); the draw skips 0 (transparent). */
|
||||
public int nibbleAt(int col, int row) {
|
||||
int b = nibbles[row * ((boxW + 1) >> 1) + (col >> 1)] & 0xff;
|
||||
return (col & 1) != 0 ? (b & 0x0f) : (b >> 4);
|
||||
}
|
||||
}
|
||||
|
||||
private static final Object lock = new Object();
|
||||
private static final Map<Integer, Entry> entries = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Register glyph rasters. Little-endian buffer of records:
|
||||
* [cp u32][ofsX s16][inkTop s16][boxW u8][boxH u8]
|
||||
* [boxH x ceil(boxW/2) nibble bytes]
|
||||
* Re-registration of a codepoint is ignored (the builtin font is fixed).
|
||||
*/
|
||||
public static void register(ByteBuffer buffer) {
|
||||
if (buffer == null) return;
|
||||
ByteBuffer in = buffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||
synchronized (lock) {
|
||||
while (in.remaining() >= 10) {
|
||||
int cp = in.getInt();
|
||||
int ofsX = in.getShort();
|
||||
int inkTop = in.getShort();
|
||||
int boxW = in.get() & 0xff;
|
||||
int boxH = in.get() & 0xff;
|
||||
byte[] nibbles = new byte[((boxW + 1) >> 1) * boxH];
|
||||
in.get(nibbles);
|
||||
if (boxW > 0 && boxH > 0 && !entries.containsKey(cp)) {
|
||||
entries.put(cp, new Entry(ofsX, inkTop, boxW, boxH, nibbles));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Entry get(int cp) {
|
||||
synchronized (lock) {
|
||||
return entries.get(cp);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,273 +1,26 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.zip.DataFormatException;
|
||||
import java.util.zip.Inflater;
|
||||
|
||||
/**
|
||||
* Accumulates composited screen frames and saves them as a looping animated
|
||||
* GIF with a 16-entry grayscale palette. Stored frames are deflate-compressed
|
||||
* (a typical UI frame shrinks from 166KB of indices to a few KB) so long
|
||||
* recordings stay affordable in memory; consecutive identical frames are
|
||||
* deduplicated, with the elapsed time folded into the next frame's delay.
|
||||
*/
|
||||
/** Android storage adapter for the shared GIF recorder. */
|
||||
public final class GifScreenRecorder {
|
||||
/** Backstop against unbounded growth if a recording is left running. */
|
||||
private static final int MAX_FRAMES = 3600;
|
||||
/** Displayed duration of the final frame, which has no successor. */
|
||||
private static final int LAST_FRAME_DELAY_CS = 100;
|
||||
/** Below this, GIF renderers historically clamp or ignore the delay. */
|
||||
private static final int MIN_FRAME_DELAY_CS = 2;
|
||||
private final SharedGifScreenRecorder recorder = new SharedGifScreenRecorder();
|
||||
|
||||
private static final class Frame {
|
||||
final byte[] deflatedIndices;
|
||||
final long timestampMs;
|
||||
|
||||
Frame(byte[] deflatedIndices, long timestampMs) {
|
||||
this.deflatedIndices = deflatedIndices;
|
||||
this.timestampMs = timestampMs;
|
||||
}
|
||||
public void addFrame(byte[] gray, int width, int height, long timestampMs) {
|
||||
recorder.addFrame(gray, width, height, timestampMs);
|
||||
}
|
||||
|
||||
private final List<Frame> frames = new ArrayList<>();
|
||||
/** Undeflated copy of the newest frame, kept for duplicate detection. */
|
||||
private byte[] lastIndices;
|
||||
private int width;
|
||||
private int height;
|
||||
private boolean overflowed;
|
||||
public boolean isOverflowed() { return recorder.isOverflowed(); }
|
||||
|
||||
/**
|
||||
* Append the current screen. gray is 8bpp; only the top nibble (the
|
||||
* display's native depth, and the palette index) is kept.
|
||||
*/
|
||||
public synchronized void addFrame(byte[] gray, int width, int height, long timestampMs) {
|
||||
if (gray == null || width <= 0 || height <= 0 || gray.length < width * height) {
|
||||
return;
|
||||
}
|
||||
if (frames.isEmpty()) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
} else if (width != this.width || height != this.height) {
|
||||
return;
|
||||
}
|
||||
byte[] indices = new byte[width * height];
|
||||
for (int i = 0; i < indices.length; i++) {
|
||||
indices[i] = (byte) ((gray[i] & 0xff) >>> 4);
|
||||
}
|
||||
if (lastIndices != null && Arrays.equals(indices, lastIndices)) {
|
||||
return;
|
||||
}
|
||||
if (frames.size() >= MAX_FRAMES) {
|
||||
overflowed = true;
|
||||
return;
|
||||
}
|
||||
frames.add(new Frame(ScreenshotUtil.zlibDeflate(indices), timestampMs));
|
||||
lastIndices = indices;
|
||||
}
|
||||
|
||||
/** True if frames were dropped because the recording hit MAX_FRAMES. */
|
||||
public synchronized boolean isOverflowed() {
|
||||
return overflowed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the recording as an animated GIF in the screenshots directory.
|
||||
* Returns the absolute path, or "" if no frames were captured.
|
||||
*/
|
||||
public synchronized String save(Context context) throws IOException {
|
||||
if (frames.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
File file = new File(ScreenshotUtil.ensureScreenshotsDir(context), "recording-" + ScreenshotUtil.timestamp() + ".gif");
|
||||
try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
|
||||
writeGif(out);
|
||||
}
|
||||
public String save(Context context) throws IOException {
|
||||
byte[] gif = recorder.encode();
|
||||
if (gif.length == 0) return "";
|
||||
File file = new File(ScreenshotUtil.ensureScreenshotsDir(context),
|
||||
"recording-" + ScreenshotUtil.timestamp() + ".gif");
|
||||
try (FileOutputStream out = new FileOutputStream(file)) { out.write(gif); }
|
||||
return file.getAbsolutePath();
|
||||
}
|
||||
|
||||
private void writeGif(OutputStream out) throws IOException {
|
||||
out.write('G');
|
||||
out.write('I');
|
||||
out.write('F');
|
||||
out.write('8');
|
||||
out.write('9');
|
||||
out.write('a');
|
||||
// Logical screen descriptor: global color table present, 8 bits of
|
||||
// color resolution, 2^(3+1)=16 palette entries.
|
||||
writeShortLE(out, width);
|
||||
writeShortLE(out, height);
|
||||
out.write(0x80 | 0x70 | 0x03);
|
||||
out.write(0); // background color index
|
||||
out.write(0); // pixel aspect ratio
|
||||
for (int i = 0; i < 16; i++) {
|
||||
int v = i * 17;
|
||||
out.write(v);
|
||||
out.write(v);
|
||||
out.write(v);
|
||||
}
|
||||
// Netscape application extension: loop forever.
|
||||
out.write(0x21);
|
||||
out.write(0xff);
|
||||
out.write(11);
|
||||
out.write("NETSCAPE2.0".getBytes("US-ASCII"));
|
||||
out.write(3);
|
||||
out.write(1);
|
||||
writeShortLE(out, 0);
|
||||
out.write(0);
|
||||
|
||||
for (int i = 0; i < frames.size(); i++) {
|
||||
Frame frame = frames.get(i);
|
||||
// Each frame displays until the next one was captured; the last
|
||||
// frame has no successor, so give it a nominal one-second hold.
|
||||
int delayCs = LAST_FRAME_DELAY_CS;
|
||||
if (i + 1 < frames.size()) {
|
||||
long deltaMs = frames.get(i + 1).timestampMs - frame.timestampMs;
|
||||
delayCs = (int) Math.max(MIN_FRAME_DELAY_CS, Math.min(0xffff, Math.round(deltaMs / 10.0)));
|
||||
}
|
||||
// Graphic control extension: no disposal, no transparency.
|
||||
out.write(0x21);
|
||||
out.write(0xf9);
|
||||
out.write(4);
|
||||
out.write(0x04); // disposal method 1 (do not dispose)
|
||||
writeShortLE(out, delayCs);
|
||||
out.write(0); // transparent color index (unused)
|
||||
out.write(0);
|
||||
// Image descriptor: full frame, no local color table.
|
||||
out.write(0x2c);
|
||||
writeShortLE(out, 0);
|
||||
writeShortLE(out, 0);
|
||||
writeShortLE(out, width);
|
||||
writeShortLE(out, height);
|
||||
out.write(0);
|
||||
writeLzwImageData(out, inflate(frame.deflatedIndices, width * height));
|
||||
}
|
||||
out.write(0x3b); // trailer
|
||||
}
|
||||
|
||||
private static byte[] inflate(byte[] deflated, int size) throws IOException {
|
||||
Inflater inflater = new Inflater();
|
||||
inflater.setInput(deflated);
|
||||
byte[] data = new byte[size];
|
||||
try {
|
||||
int offset = 0;
|
||||
while (offset < size && !inflater.finished()) {
|
||||
int n = inflater.inflate(data, offset, size - offset);
|
||||
if (n == 0) break;
|
||||
offset += n;
|
||||
}
|
||||
if (offset != size) {
|
||||
throw new IOException("recorded frame truncated: " + offset + " of " + size);
|
||||
}
|
||||
} catch (DataFormatException e) {
|
||||
throw new IOException("recorded frame corrupt", e);
|
||||
} finally {
|
||||
inflater.end();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/** GIF LZW compression of a 4-bit index stream (minimum code size 4). */
|
||||
private static void writeLzwImageData(OutputStream out, byte[] indices) throws IOException {
|
||||
final int minCodeSize = 4;
|
||||
out.write(minCodeSize);
|
||||
SubBlockBitWriter writer = new SubBlockBitWriter(out);
|
||||
final int clearCode = 1 << minCodeSize;
|
||||
final int eoiCode = clearCode + 1;
|
||||
int codeSize = minCodeSize + 1;
|
||||
int nextCode = eoiCode + 1;
|
||||
HashMap<Integer, Integer> table = new HashMap<>();
|
||||
writer.writeCode(clearCode, codeSize);
|
||||
int prefix = indices[0] & 0xff;
|
||||
for (int i = 1; i < indices.length; i++) {
|
||||
int k = indices[i] & 0xff;
|
||||
int key = (prefix << 8) | k;
|
||||
Integer code = table.get(key);
|
||||
if (code != null) {
|
||||
prefix = code;
|
||||
continue;
|
||||
}
|
||||
writer.writeCode(prefix, codeSize);
|
||||
if (nextCode == 0x1000) {
|
||||
writer.writeCode(clearCode, codeSize);
|
||||
table.clear();
|
||||
codeSize = minCodeSize + 1;
|
||||
nextCode = eoiCode + 1;
|
||||
} else {
|
||||
if (nextCode >= (1 << codeSize)) {
|
||||
codeSize++;
|
||||
}
|
||||
table.put(key, nextCode++);
|
||||
}
|
||||
prefix = k;
|
||||
}
|
||||
writer.writeCode(prefix, codeSize);
|
||||
writer.writeCode(eoiCode, codeSize);
|
||||
writer.finish();
|
||||
}
|
||||
|
||||
/**
|
||||
* Packs LZW codes LSB-first into bytes and the bytes into the 255-byte
|
||||
* data sub-blocks GIF image data is carried in.
|
||||
*/
|
||||
private static final class SubBlockBitWriter {
|
||||
private final OutputStream out;
|
||||
private final byte[] block = new byte[255];
|
||||
private int blockLength;
|
||||
private int bitBuffer;
|
||||
private int bitCount;
|
||||
|
||||
SubBlockBitWriter(OutputStream out) {
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
void writeCode(int code, int codeSize) throws IOException {
|
||||
bitBuffer |= code << bitCount;
|
||||
bitCount += codeSize;
|
||||
while (bitCount >= 8) {
|
||||
writeByte(bitBuffer & 0xff);
|
||||
bitBuffer >>>= 8;
|
||||
bitCount -= 8;
|
||||
}
|
||||
}
|
||||
|
||||
void finish() throws IOException {
|
||||
if (bitCount > 0) {
|
||||
writeByte(bitBuffer & 0xff);
|
||||
bitBuffer = 0;
|
||||
bitCount = 0;
|
||||
}
|
||||
flushBlock();
|
||||
out.write(0); // block terminator
|
||||
}
|
||||
|
||||
private void writeByte(int b) throws IOException {
|
||||
block[blockLength++] = (byte) b;
|
||||
if (blockLength == 255) {
|
||||
flushBlock();
|
||||
}
|
||||
}
|
||||
|
||||
private void flushBlock() throws IOException {
|
||||
if (blockLength == 0) return;
|
||||
out.write(blockLength);
|
||||
out.write(block, 0, blockLength);
|
||||
blockLength = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeShortLE(OutputStream out, int value) throws IOException {
|
||||
out.write(value & 0xff);
|
||||
out.write((value >>> 8) & 0xff);
|
||||
}
|
||||
}
|
||||
@@ -1,267 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Process-wide registry of glyph rasters, fed from the TS side so glyph
|
||||
* identity survives to the BLE encoder (see notes/texture-cache-display-list-
|
||||
* design.md). A frame's glyph list references entries here by (fontId,
|
||||
* encoding); the texture-cache planner uses the raster three ways:
|
||||
*
|
||||
* - as the ink mask for the "would this draw land correctly" check against
|
||||
* the composited frame,
|
||||
* - to punch the ink pixels out of the baked delta rect it replaces,
|
||||
* - pre-encoded as the CFW cached-image bytes ([w][h][4bpp RLE]) uploaded
|
||||
* via mode 12 and drawn via mode 14 with a top-color LUT. 1bpp (BDF)
|
||||
* glyphs store ink at 15 so the LUT maps them to exactly the requested
|
||||
* level; AA (TTF) glyphs store true coverage nibbles the LUT scales.
|
||||
*
|
||||
* Fonts are identified by a stable string key (the embedded font name), NOT a
|
||||
* per-JS-context counter: worker threads register independently and must agree
|
||||
* on ids. Glyph rasters are immutable once registered; re-registration of the
|
||||
* same (fontId, encoding) is ignored.
|
||||
*
|
||||
* Cell model: a cached glyph image is bbxWidth x cellHeight (the font's line
|
||||
* height), with the ink rows at inkTop — so every glyph of a line is drawn at
|
||||
* the same y (the line top), which is what lets one mode-14 string carry a
|
||||
* whole run. The horizontal bearing (bbxX) is applied phone-side when
|
||||
* computing the draw x. Vertical padding is nearly free: it RLE-encodes to a
|
||||
* couple of tokens.
|
||||
*/
|
||||
public final class GlyphAtlas {
|
||||
private GlyphAtlas() {}
|
||||
|
||||
public static final class Glyph {
|
||||
/** Cached image width in pixels (the glyph's tight bbox width). */
|
||||
public final int width;
|
||||
/** Cached image height in pixels (the font's line height). */
|
||||
public final int cellHeight;
|
||||
/** First row of ink within the cell. */
|
||||
public final int inkTop;
|
||||
/** Number of ink rows. */
|
||||
public final int inkHeight;
|
||||
/** Horizontal bearing: draw x = pen x + bbxX. */
|
||||
public final int bbxX;
|
||||
/** 1bpp ink rows (inkHeight entries), bit (rowBitWidth-1-col) = ink; null for AA glyphs. */
|
||||
final int[] rows;
|
||||
final int rowBitWidth;
|
||||
/** AA coverage, width*inkHeight nibble values 0..15; null for 1bpp glyphs. */
|
||||
final byte[] coverage;
|
||||
/** CFW cached-image bytes: [w][cellHeight][RLE(w*cellHeight px)]. */
|
||||
public final byte[] cachedBytes;
|
||||
|
||||
Glyph(int width, int cellHeight, int inkTop, int inkHeight, int bbxX,
|
||||
int[] rows, byte[] coverage) {
|
||||
this.width = width;
|
||||
this.cellHeight = cellHeight;
|
||||
this.inkTop = inkTop;
|
||||
this.inkHeight = inkHeight;
|
||||
this.bbxX = bbxX;
|
||||
this.rows = rows;
|
||||
this.coverage = coverage;
|
||||
this.rowBitWidth = ((width + 7) >> 3) << 3;
|
||||
this.cachedBytes = encodeCachedImage();
|
||||
}
|
||||
|
||||
/**
|
||||
* The cell pixel's 4bpp source value at (col, row), row cell-relative.
|
||||
* 1bpp glyphs are stored at 15 so the draw-time LUT (source*top/15)
|
||||
* maps them to exactly the requested top color; AA glyphs carry their
|
||||
* true coverage level.
|
||||
*/
|
||||
public int nibbleAt(int col, int row) {
|
||||
if (col < 0 || col >= width) return 0;
|
||||
int inkRow = row - inkTop;
|
||||
if (inkRow < 0 || inkRow >= inkHeight) return 0;
|
||||
if (coverage != null) return coverage[inkRow * width + col];
|
||||
return ((rows[inkRow] >>> (rowBitWidth - 1 - col)) & 1) != 0 ? 15 : 0;
|
||||
}
|
||||
|
||||
/** Whether the cell pixel at (col, row) has any ink (row is cell-relative). */
|
||||
public boolean inkAt(int col, int row) {
|
||||
return nibbleAt(col, row) != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The firmware cached-image encoding: [width][height][RLE tokens]
|
||||
* covering exactly width*cellHeight pixels (no row padding), each
|
||||
* pixel's stored color being its nibbleAt value.
|
||||
*/
|
||||
private byte[] encodeCachedImage() {
|
||||
int total = width * cellHeight;
|
||||
// Worst case one token per pixel (1 byte each) + 2 header bytes.
|
||||
byte[] out = new byte[2 + total];
|
||||
out[0] = (byte) width;
|
||||
out[1] = (byte) cellHeight;
|
||||
int o = 2;
|
||||
int i = 0;
|
||||
while (i < total) {
|
||||
int color = nibbleAt(i % width, i / width);
|
||||
int j = i + 1;
|
||||
while (j < total && nibbleAt(j % width, j / width) == color) j++;
|
||||
int run = j - i;
|
||||
while (run > 0) {
|
||||
int c = Math.min(run, 0xffff);
|
||||
if (c <= 15) {
|
||||
out[o++] = (byte) ((c << 4) | color);
|
||||
} else if (c <= 255) {
|
||||
// Worst case can exceed the sizing above only via escape
|
||||
// forms, which occur for runs >= 16 (net win); safe.
|
||||
out[o++] = (byte) color;
|
||||
out[o++] = (byte) c;
|
||||
} else {
|
||||
out[o++] = (byte) color;
|
||||
out[o++] = 0;
|
||||
out[o++] = (byte) (c & 0xff);
|
||||
out[o++] = (byte) (c >> 8);
|
||||
}
|
||||
run -= c;
|
||||
}
|
||||
i = j;
|
||||
}
|
||||
byte[] trimmed = new byte[o];
|
||||
System.arraycopy(out, 0, trimmed, 0, o);
|
||||
return trimmed;
|
||||
}
|
||||
}
|
||||
|
||||
private static final Object lock = new Object();
|
||||
private static final Map<String, Integer> fontIds = new HashMap<>();
|
||||
private static final Map<Long, Glyph> glyphs = new HashMap<>();
|
||||
private static int nextFontId = 1;
|
||||
|
||||
/** Stable id for a font key; assigns one on first use. */
|
||||
public static int fontId(String key) {
|
||||
synchronized (lock) {
|
||||
Integer id = fontIds.get(key);
|
||||
if (id == null) {
|
||||
id = nextFontId++;
|
||||
fontIds.put(key, id);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
public static Glyph get(int fontId, int encoding) {
|
||||
synchronized (lock) {
|
||||
return glyphs.get(key(fontId, encoding));
|
||||
}
|
||||
}
|
||||
|
||||
private static long key(int fontId, int encoding) {
|
||||
return ((long) fontId << 32) | (encoding & 0xffffffffL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a batch of glyph rasters. Little-endian buffer, a sequence of
|
||||
* font groups:
|
||||
* [keyLen u8][key utf8][cellHeight u8][count u16]
|
||||
* each followed by count glyph records:
|
||||
* [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8]
|
||||
* [inkHeight x rows u32] (bit (ceil(width/8)*8 - 1 - col) = ink)
|
||||
* Records for an already-registered (font, encoding) are skipped (glyph
|
||||
* rasters are immutable for a given font key). Malformed buffers throw:
|
||||
* they indicate a phone-side marshalling bug, never device state.
|
||||
*/
|
||||
public static void register(ByteBuffer buffer) {
|
||||
if (buffer == null) return;
|
||||
ByteBuffer in = buffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||
synchronized (lock) {
|
||||
while (in.remaining() > 0) {
|
||||
int keyLen = in.get() & 0xff;
|
||||
byte[] keyBytes = new byte[keyLen];
|
||||
in.get(keyBytes);
|
||||
String fontKey = new String(keyBytes, StandardCharsets.UTF_8);
|
||||
int cellHeight = in.get() & 0xff;
|
||||
int count = in.getShort() & 0xffff;
|
||||
Integer idBoxed = fontIds.get(fontKey);
|
||||
int id;
|
||||
if (idBoxed == null) {
|
||||
id = nextFontId++;
|
||||
fontIds.put(fontKey, id);
|
||||
} else {
|
||||
id = idBoxed;
|
||||
}
|
||||
for (int g = 0; g < count; g++) {
|
||||
int encoding = in.getInt();
|
||||
int bbxX = in.get();
|
||||
int inkTop = in.get() & 0xff;
|
||||
int width = in.get() & 0xff;
|
||||
int inkHeight = in.get() & 0xff;
|
||||
int[] rows = new int[inkHeight];
|
||||
for (int r = 0; r < inkHeight; r++) {
|
||||
rows[r] = in.getInt();
|
||||
}
|
||||
long k = key(id, encoding);
|
||||
if (!glyphs.containsKey(k) && width > 0 && cellHeight > 0
|
||||
&& inkTop + inkHeight <= cellHeight) {
|
||||
glyphs.put(k, new Glyph(width, cellHeight, inkTop, inkHeight, bbxX,
|
||||
rows, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a batch of antialiased (4bpp) glyph rasters, e.g. TTF renders.
|
||||
* Same font-group framing as register(), but each glyph record carries
|
||||
* packed coverage instead of 1bpp rows:
|
||||
* [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8]
|
||||
* [inkHeight x ceil(width/2) packed bytes, high nibble = left pixel]
|
||||
* Nibble values are the final 4bpp source levels (the draw-time LUT
|
||||
* scales them by the top color). Already-registered (font, encoding)
|
||||
* records are skipped, as in register().
|
||||
*/
|
||||
public static void registerAa(ByteBuffer buffer) {
|
||||
if (buffer == null) return;
|
||||
ByteBuffer in = buffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||
synchronized (lock) {
|
||||
while (in.remaining() > 0) {
|
||||
int keyLen = in.get() & 0xff;
|
||||
byte[] keyBytes = new byte[keyLen];
|
||||
in.get(keyBytes);
|
||||
String fontKey = new String(keyBytes, StandardCharsets.UTF_8);
|
||||
int cellHeight = in.get() & 0xff;
|
||||
int count = in.getShort() & 0xffff;
|
||||
Integer idBoxed = fontIds.get(fontKey);
|
||||
int id;
|
||||
if (idBoxed == null) {
|
||||
id = nextFontId++;
|
||||
fontIds.put(fontKey, id);
|
||||
} else {
|
||||
id = idBoxed;
|
||||
}
|
||||
for (int g = 0; g < count; g++) {
|
||||
int encoding = in.getInt();
|
||||
int bbxX = in.get();
|
||||
int inkTop = in.get() & 0xff;
|
||||
int width = in.get() & 0xff;
|
||||
int inkHeight = in.get() & 0xff;
|
||||
int stride = (width + 1) >> 1;
|
||||
byte[] coverage = new byte[width * inkHeight];
|
||||
for (int r = 0; r < inkHeight; r++) {
|
||||
for (int b = 0; b < stride; b++) {
|
||||
int packed = in.get() & 0xff;
|
||||
int col = b * 2;
|
||||
coverage[r * width + col] = (byte) (packed >> 4);
|
||||
if (col + 1 < width) {
|
||||
coverage[r * width + col + 1] = (byte) (packed & 0x0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
long k = key(id, encoding);
|
||||
if (!glyphs.containsKey(k) && width > 0 && cellHeight > 0
|
||||
&& inkTop + inkHeight <= cellHeight) {
|
||||
glyphs.put(k, new Glyph(width, cellHeight, inkTop, inkHeight, bbxX,
|
||||
null, coverage));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Process-wide registry of icon/image rasters for the texture-cache pipeline,
|
||||
* the image counterpart of GlyphAtlas. Entries are content-addressed: the TS
|
||||
* side keys each image by a hash of its dimensions and pixels, so the same
|
||||
* icon registered from any thread or rendered at any time dedupes to one
|
||||
* entry, and an icon whose content changes is simply a new entry.
|
||||
*
|
||||
* Unlike glyphs (1-bit ink recolored at draw time), images keep their exact
|
||||
* 4bpp values: the cached bytes quantize the registered 8bpp pixels with the
|
||||
* same GRAY_TO_NIBBLE table the composite is packed with, and the planner
|
||||
* draws them via mode 13 with an identity LUT (top color 15) and the
|
||||
* transparent bit — the draw writes exactly the nonzero-nibble pixels, which
|
||||
* is what the eligibility check and hole punching are defined over.
|
||||
*/
|
||||
public final class ImageAtlas {
|
||||
private ImageAtlas() {}
|
||||
|
||||
public static final class Entry {
|
||||
public final int width;
|
||||
public final int height;
|
||||
/** One 4bpp value per pixel, row-major. */
|
||||
final byte[] nibbles;
|
||||
/** CFW cached-image bytes: [w][h][RLE(w*h pixels)]. */
|
||||
public final byte[] cachedBytes;
|
||||
|
||||
Entry(int width, int height, byte[] nibbles) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.nibbles = nibbles;
|
||||
this.cachedBytes = encodeCachedImage();
|
||||
}
|
||||
|
||||
/** The 4bpp value the mode-13 draw would write at (col, row); 0 = skipped. */
|
||||
public int nibbleAt(int col, int row) {
|
||||
return nibbles[row * width + col] & 0xff;
|
||||
}
|
||||
|
||||
private byte[] encodeCachedImage() {
|
||||
int total = width * height;
|
||||
byte[] out = new byte[2 + total];
|
||||
out[0] = (byte) width;
|
||||
out[1] = (byte) height;
|
||||
int o = 2;
|
||||
int i = 0;
|
||||
while (i < total) {
|
||||
int color = nibbles[i] & 0xff;
|
||||
int j = i + 1;
|
||||
while (j < total && (nibbles[j] & 0xff) == color) j++;
|
||||
int run = j - i;
|
||||
while (run > 0) {
|
||||
int c = Math.min(run, 0xffff);
|
||||
if (c <= 15) {
|
||||
out[o++] = (byte) ((c << 4) | color);
|
||||
} else if (c <= 255) {
|
||||
out[o++] = (byte) color;
|
||||
out[o++] = (byte) c;
|
||||
} else {
|
||||
out[o++] = (byte) color;
|
||||
out[o++] = 0;
|
||||
out[o++] = (byte) (c & 0xff);
|
||||
out[o++] = (byte) (c >> 8);
|
||||
}
|
||||
run -= c;
|
||||
}
|
||||
i = j;
|
||||
}
|
||||
byte[] trimmed = new byte[o];
|
||||
System.arraycopy(out, 0, trimmed, 0, o);
|
||||
return trimmed;
|
||||
}
|
||||
}
|
||||
|
||||
private static final Object lock = new Object();
|
||||
private static final Map<String, Integer> ids = new HashMap<>();
|
||||
private static final List<Entry> entries = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Register an image (8bpp grayscale pixels, width*height bytes, row-major)
|
||||
* under a content key, returning its id — or the existing id when the key
|
||||
* is already registered (the pixels are then ignored: same key means same
|
||||
* content). Ids are positive and stable for the process lifetime.
|
||||
*/
|
||||
public static int ensure(String key, int width, int height, ByteBuffer pixels8bpp) {
|
||||
if (key == null || width <= 0 || width > 255 || height <= 0 || height > 255
|
||||
|| pixels8bpp == null || pixels8bpp.remaining() < width * height) {
|
||||
throw new IllegalArgumentException("bad image registration " + width + "x" + height);
|
||||
}
|
||||
synchronized (lock) {
|
||||
Integer existing = ids.get(key);
|
||||
if (existing != null) return existing;
|
||||
byte[] nibbles = new byte[width * height];
|
||||
for (int i = 0; i < nibbles.length; i++) {
|
||||
nibbles[i] = (byte) BmpUtil.nibbleForGray(pixels8bpp.get(i) & 0xff);
|
||||
}
|
||||
entries.add(new Entry(width, height, nibbles));
|
||||
int id = entries.size(); // ids start at 1
|
||||
ids.put(key, id);
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
public static Entry get(int id) {
|
||||
synchronized (lock) {
|
||||
return id >= 1 && id <= entries.size() ? entries.get(id - 1) : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,229 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Reader for the serialized LVGL font image in the G2's CJK flash partition.
|
||||
* Faceclaw bundles that font because it is Source Han Sans SC Light under the
|
||||
* SIL OFL. Proprietary fonts embedded in the main firmware never pass through
|
||||
* this class and are instead extracted on the user's phone during CFW install.
|
||||
*/
|
||||
public final class LvglFontFile {
|
||||
private static final String TAG = "LvglFontFile";
|
||||
private static final int MAPPED_BASE = 0x80100000;
|
||||
private static final int FONT_CACHE_SIZE = 2;
|
||||
|
||||
private static final Map<String, FontData> fontCache =
|
||||
new LinkedHashMap<String, FontData>(FONT_CACHE_SIZE, 0.75f, true) {
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<String, FontData> eldest) {
|
||||
return size() > FONT_CACHE_SIZE;
|
||||
}
|
||||
};
|
||||
|
||||
private LvglFontFile() {}
|
||||
|
||||
/**
|
||||
* Returns [lineHeight, baseline] or an empty array when the font is invalid.
|
||||
* The baseline is measured down from the top of the line.
|
||||
*/
|
||||
public static byte[] getMetrics(String path) {
|
||||
FontData font = load(path);
|
||||
if (font == null) return new byte[0];
|
||||
return new byte[] { (byte) font.lineHeight, (byte) font.baseline };
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns one packed glyph:
|
||||
* u16 boxW, u16 boxH, i16 ofsX, i16 ofsY, then aligned 4bpp bitmap bytes.
|
||||
* Returns an empty array when the code point is not mapped.
|
||||
*/
|
||||
public static byte[] getGlyph(String path, int codePoint) {
|
||||
FontData font = load(path);
|
||||
if (font == null) return new byte[0];
|
||||
Integer glyphId = font.codePointToGlyph.get(codePoint);
|
||||
if (glyphId == null || glyphId <= 0) return new byte[0];
|
||||
try {
|
||||
int descriptor = font.glyphDescriptors + glyphId * 16;
|
||||
int bitmapIndex = font.u32(descriptor);
|
||||
int boxWidth = font.u16(descriptor + 8);
|
||||
int boxHeight = font.u16(descriptor + 10);
|
||||
int offsetX = font.i16(descriptor + 12);
|
||||
int offsetY = font.i16(descriptor + 14);
|
||||
int stride = boxWidth / 2 + 1;
|
||||
int bitmapLength = boxWidth > 0 && boxHeight > 0 ? stride * boxHeight : 0;
|
||||
font.check(font.glyphBitmaps + bitmapIndex, bitmapLength);
|
||||
|
||||
byte[] result = new byte[8 + bitmapLength];
|
||||
putU16(result, 0, boxWidth);
|
||||
putU16(result, 2, boxHeight);
|
||||
putU16(result, 4, offsetX);
|
||||
putU16(result, 6, offsetY);
|
||||
System.arraycopy(font.bytes, font.glyphBitmaps + bitmapIndex, result, 8, bitmapLength);
|
||||
return result;
|
||||
} catch (RuntimeException e) {
|
||||
Log.w(TAG, "invalid glyph U+" + Integer.toHexString(codePoint) + " in " + path, e);
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
|
||||
private static synchronized FontData load(String path) {
|
||||
if (path == null || path.isEmpty()) return null;
|
||||
FontData cached = fontCache.get(path);
|
||||
if (cached != null) return cached;
|
||||
try {
|
||||
FontData font = FontData.read(path);
|
||||
fontCache.put(path, font);
|
||||
return font;
|
||||
} catch (IOException | RuntimeException e) {
|
||||
Log.w(TAG, "could not load LVGL font " + path, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static void putU16(byte[] destination, int offset, int value) {
|
||||
destination[offset] = (byte) (value & 0xff);
|
||||
destination[offset + 1] = (byte) ((value >>> 8) & 0xff);
|
||||
}
|
||||
|
||||
private static final class FontData {
|
||||
final byte[] bytes;
|
||||
final ByteBuffer view;
|
||||
final int lineHeight;
|
||||
final int baseline;
|
||||
final int glyphBitmaps;
|
||||
final int glyphDescriptors;
|
||||
final Map<Integer, Integer> codePointToGlyph;
|
||||
|
||||
private FontData(byte[] bytes) {
|
||||
this.bytes = bytes;
|
||||
this.view = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
|
||||
if (bytes.length < 0x40 || bytes[0] != 'Z' || bytes[1] != 'Z' || bytes[2] != 'Z' || bytes[3] != 'Z') {
|
||||
throw new IllegalArgumentException("missing ZZZZ font header");
|
||||
}
|
||||
|
||||
lineHeight = u16(0x38);
|
||||
int baseLine = u16(0x3a);
|
||||
baseline = lineHeight - baseLine;
|
||||
|
||||
int descriptor = pointerOffset(u32(0x34));
|
||||
glyphBitmaps = pointerOffset(u32(descriptor));
|
||||
glyphDescriptors = pointerOffset(u32(descriptor + 4));
|
||||
int cmaps = pointerOffset(u32(descriptor + 8));
|
||||
int packed = u16(descriptor + 18);
|
||||
int cmapCount = packed & 0x1ff;
|
||||
int bitsPerPixel = (packed >>> 9) & 0xf;
|
||||
int bitmapFormat = (packed >>> 14) & 0x3;
|
||||
if (lineHeight <= 0 || baseline < 0 || cmapCount <= 0 ||
|
||||
bitsPerPixel != 4 || bitmapFormat != 3) {
|
||||
throw new IllegalArgumentException("unsupported LVGL font metadata");
|
||||
}
|
||||
|
||||
codePointToGlyph = new HashMap<>();
|
||||
for (int index = 0; index < cmapCount; index++) {
|
||||
readCmap(cmaps + index * 20);
|
||||
}
|
||||
}
|
||||
|
||||
static FontData read(String path) throws IOException {
|
||||
File file = new File(path);
|
||||
if (!file.isFile() || !file.canRead() || file.length() > Integer.MAX_VALUE) {
|
||||
throw new IOException("font is not a readable file");
|
||||
}
|
||||
byte[] bytes = new byte[(int) file.length()];
|
||||
try (RandomAccessFile input = new RandomAccessFile(file, "r")) {
|
||||
input.readFully(bytes);
|
||||
}
|
||||
return new FontData(bytes);
|
||||
}
|
||||
|
||||
void readCmap(int address) {
|
||||
int rangeStart = u32(address);
|
||||
int rangeLength = u16(address + 4);
|
||||
int glyphIdStart = u16(address + 6);
|
||||
int unicodeListPointer = u32(address + 8);
|
||||
int glyphIdOffsetsPointer = u32(address + 12);
|
||||
int listLength = u16(address + 16);
|
||||
int type = u8(address + 18);
|
||||
|
||||
if (type == 2) {
|
||||
for (int relative = 0; relative < rangeLength; relative++) {
|
||||
codePointToGlyph.put(rangeStart + relative, glyphIdStart + relative);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (type == 0) {
|
||||
int offsets = pointerOffset(glyphIdOffsetsPointer);
|
||||
for (int relative = 0; relative < rangeLength; relative++) {
|
||||
int offset = u8(offsets + relative);
|
||||
if (relative != 0 && offset == 0) continue;
|
||||
codePointToGlyph.put(rangeStart + relative, glyphIdStart + offset);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (type == 3 || type == 1) {
|
||||
int unicodeList = pointerOffset(unicodeListPointer);
|
||||
int offsets = glyphIdOffsetsPointer == 0 ? -1 : pointerOffset(glyphIdOffsetsPointer);
|
||||
for (int index = 0; index < listLength; index++) {
|
||||
int relative = u16(unicodeList + index * 2);
|
||||
int offset = index;
|
||||
if (offsets >= 0) {
|
||||
offset = type == 1 ? u16(offsets + index * 2) : u8(offsets + index);
|
||||
if (index != 0 && offset == 0) continue;
|
||||
}
|
||||
codePointToGlyph.put(rangeStart + relative, glyphIdStart + offset);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("unknown cmap type " + type);
|
||||
}
|
||||
|
||||
int u8(int offset) {
|
||||
check(offset, 1);
|
||||
return bytes[offset] & 0xff;
|
||||
}
|
||||
|
||||
int u16(int offset) {
|
||||
check(offset, 2);
|
||||
return view.getShort(offset) & 0xffff;
|
||||
}
|
||||
|
||||
int i16(int offset) {
|
||||
check(offset, 2);
|
||||
return view.getShort(offset);
|
||||
}
|
||||
|
||||
int u32(int offset) {
|
||||
check(offset, 4);
|
||||
return view.getInt(offset);
|
||||
}
|
||||
|
||||
int pointerOffset(int pointer) {
|
||||
long unsignedPointer = pointer & 0xffffffffL;
|
||||
long offset = unsignedPointer - (MAPPED_BASE & 0xffffffffL);
|
||||
if (offset < 0 || offset > Integer.MAX_VALUE) {
|
||||
throw new IllegalArgumentException("font pointer is outside the mapped partition");
|
||||
}
|
||||
check((int) offset, 1);
|
||||
return (int) offset;
|
||||
}
|
||||
|
||||
void check(int offset, int length) {
|
||||
if (offset < 0 || length < 0 || (long) offset + length > bytes.length) {
|
||||
throw new IllegalArgumentException("font range is out of bounds");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,15 +2,12 @@ package com.faceclaw.app;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.zip.CRC32;
|
||||
import java.util.zip.Deflater;
|
||||
|
||||
/**
|
||||
* Saves screenshots of the composited screen as 4-bit grayscale PNGs (the
|
||||
@@ -25,7 +22,7 @@ public final class ScreenshotUtil {
|
||||
if (gray == null || width <= 0 || height <= 0 || gray.length < width * height) {
|
||||
throw new IllegalArgumentException("invalid screenshot buffer");
|
||||
}
|
||||
byte[] png = encode4BitGrayPng(gray, width, height);
|
||||
byte[] png = SharedScreenshots.encode4BitGrayPng(gray, width, height);
|
||||
File file = new File(ensureScreenshotsDir(context), "screen-" + timestamp() + ".png");
|
||||
try (FileOutputStream out = new FileOutputStream(file)) {
|
||||
out.write(png);
|
||||
@@ -45,80 +42,4 @@ public final class ScreenshotUtil {
|
||||
return new SimpleDateFormat("yyyyMMdd-HHmmss-SSS", Locale.US).format(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode an 8bpp grayscale buffer as a bit-depth-4 grayscale PNG, keeping
|
||||
* the top nibble of each pixel (the same quantization the wire format uses).
|
||||
*/
|
||||
static byte[] encode4BitGrayPng(byte[] gray, int width, int height) {
|
||||
// Raw scanlines: one filter byte (0 = None) then two pixels per byte,
|
||||
// leftmost pixel in the high nibble.
|
||||
int rowBytes = (width + 1) / 2;
|
||||
byte[] raw = new byte[height * (1 + rowBytes)];
|
||||
int p = 0;
|
||||
for (int y = 0; y < height; y++) {
|
||||
raw[p++] = 0;
|
||||
int row = y * width;
|
||||
for (int x = 0; x < width; x += 2) {
|
||||
int hi = (gray[row + x] & 0xff) >>> 4;
|
||||
int lo = (x + 1 < width) ? (gray[row + x + 1] & 0xff) >>> 4 : 0;
|
||||
raw[p++] = (byte) ((hi << 4) | lo);
|
||||
}
|
||||
}
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(0x89);
|
||||
out.write('P');
|
||||
out.write('N');
|
||||
out.write('G');
|
||||
out.write(0x0d);
|
||||
out.write(0x0a);
|
||||
out.write(0x1a);
|
||||
out.write(0x0a);
|
||||
|
||||
ByteArrayOutputStream ihdr = new ByteArrayOutputStream();
|
||||
writeIntBE(ihdr, width);
|
||||
writeIntBE(ihdr, height);
|
||||
ihdr.write(4); // bit depth
|
||||
ihdr.write(0); // color type: grayscale
|
||||
ihdr.write(0); // compression
|
||||
ihdr.write(0); // filter
|
||||
ihdr.write(0); // interlace
|
||||
writeChunk(out, "IHDR", ihdr.toByteArray());
|
||||
writeChunk(out, "IDAT", zlibDeflate(raw));
|
||||
writeChunk(out, "IEND", new byte[0]);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
static byte[] zlibDeflate(byte[] data) {
|
||||
Deflater deflater = new Deflater();
|
||||
deflater.setInput(data);
|
||||
deflater.finish();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(64, data.length / 8));
|
||||
byte[] buffer = new byte[8192];
|
||||
while (!deflater.finished()) {
|
||||
out.write(buffer, 0, deflater.deflate(buffer));
|
||||
}
|
||||
deflater.end();
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
private static void writeChunk(ByteArrayOutputStream out, String type, byte[] data) {
|
||||
writeIntBE(out, data.length);
|
||||
CRC32 crc = new CRC32();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int b = type.charAt(i);
|
||||
out.write(b);
|
||||
crc.update(b);
|
||||
}
|
||||
out.write(data, 0, data.length);
|
||||
crc.update(data);
|
||||
writeIntBE(out, (int) crc.getValue());
|
||||
}
|
||||
|
||||
private static void writeIntBE(ByteArrayOutputStream out, int value) {
|
||||
out.write((value >>> 24) & 0xff);
|
||||
out.write((value >>> 16) & 0xff);
|
||||
out.write((value >>> 8) & 0xff);
|
||||
out.write(value & 0xff);
|
||||
}
|
||||
}
|
||||
@@ -1,509 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Retained-surface compositor: the phone-side model of what is on the glasses
|
||||
* screen.
|
||||
*
|
||||
* Each surface is an 8bpp grayscale buffer retained at its last-submitted
|
||||
* contents, positioned on the screen with a z-order and a transparency mode.
|
||||
* Sources on the TS side (today the dashboard; later the shell and per-app
|
||||
* worker threads) submit updates to their surface, and the compositor
|
||||
* recombines the retained surfaces into a full-screen frame that feeds the
|
||||
* existing dedupe/compress/transmit pipeline. Because every surface is
|
||||
* retained, a full screen frame can be regenerated at any time (frame drops,
|
||||
* reconnects, previews, screenshots) without asking sources to repaint.
|
||||
*
|
||||
* Surface format contract (mirrored by the TS side):
|
||||
* - Pixels are 8bpp grayscale, row-major, one byte per pixel. The wire
|
||||
* pipeline quantizes to 4bpp downstream, so the low nibble is not visible.
|
||||
* - TRANSPARENCY_COLOR_KEY surfaces treat pixel value 0 as fully transparent
|
||||
* and value 1 as black. Quantization collapses 1 into the same 4bpp level
|
||||
* as 0, so reserving 0 costs no visible shade; painters of color-key
|
||||
* surfaces must clamp intentional black to 1.
|
||||
* - An update may cover any rect of its surface; the rest is retained. The
|
||||
* update buffer holds exactly rectWidth*rectHeight bytes, row-major.
|
||||
* - Surfaces composite in ascending z-order onto a black (0) background.
|
||||
* - Surface geometry changes take effect when the next frame composites;
|
||||
* they do not trigger a recomposite by themselves.
|
||||
*
|
||||
* Thread safety: all methods are safe to call from any thread. Apply and
|
||||
* composite happen atomically under an internal lock, and each composite
|
||||
* carries a monotonic sequence number so callers can detect when a composite
|
||||
* was superseded by a concurrent one before being acted on.
|
||||
*/
|
||||
public final class SurfaceCompositor {
|
||||
public static final int TRANSPARENCY_OPAQUE = 0;
|
||||
public static final int TRANSPARENCY_COLOR_KEY = 1;
|
||||
|
||||
/**
|
||||
* One deferred draw (a text glyph or an icon image) within a frame, in
|
||||
* screen coordinates. The draw's pixels are already baked into the
|
||||
* composited gray buffer (the TS side bakes before submitting); this
|
||||
* record preserves the draw's identity so the texture-cache planner can
|
||||
* replay it as an on-glasses cached draw instead of image bytes.
|
||||
*
|
||||
* Glyphs: x/y are the pen position and line top; the raster (and its
|
||||
* bearing/cell placement) comes from GlyphAtlas under (fontId, encoding).
|
||||
* Images: x/y are the blit's top-left; the raster comes from ImageAtlas
|
||||
* under imageId.
|
||||
*/
|
||||
public static final class ScreenDraw {
|
||||
public static final int KIND_GLYPH = 0;
|
||||
public static final int KIND_IMAGE = 1;
|
||||
/** A firmware-builtin-font text run (CFW mode 15). */
|
||||
public static final int KIND_FWTEXT = 2;
|
||||
|
||||
public final int kind;
|
||||
/** Glyph draws only. */
|
||||
public final int fontId;
|
||||
/** Glyph draws only. */
|
||||
public final int encoding;
|
||||
/** Image draws only. */
|
||||
public final int imageId;
|
||||
public final int x;
|
||||
public final int y;
|
||||
/** Glyph and fw-text draws: 8-bit brightness. */
|
||||
public final int value;
|
||||
/** Fw-text runs only: member codepoints, in text order. */
|
||||
public final int[] fwCps;
|
||||
/** Fw-text runs only: member pen offsets relative to x. */
|
||||
public final int[] fwDx;
|
||||
/** Fw-text runs only: whether each member has visible pixels. */
|
||||
public final boolean[] fwInk;
|
||||
|
||||
ScreenDraw(int kind, int fontId, int encoding, int imageId, int x, int y, int value,
|
||||
int[] fwCps, int[] fwDx, boolean[] fwInk) {
|
||||
this.kind = kind;
|
||||
this.fontId = fontId;
|
||||
this.encoding = encoding;
|
||||
this.imageId = imageId;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.value = value;
|
||||
this.fwCps = fwCps;
|
||||
this.fwDx = fwDx;
|
||||
this.fwInk = fwInk;
|
||||
}
|
||||
|
||||
static ScreenDraw glyph(int fontId, int encoding, int penX, int lineY, int value) {
|
||||
return new ScreenDraw(KIND_GLYPH, fontId, encoding, 0, penX, lineY, value, null, null, null);
|
||||
}
|
||||
|
||||
static ScreenDraw image(int imageId, int x, int y) {
|
||||
return new ScreenDraw(KIND_IMAGE, 0, 0, imageId, x, y, 0, null, null, null);
|
||||
}
|
||||
|
||||
static ScreenDraw fwText(int x, int y, int value, int[] cps, int[] dx, boolean[] ink) {
|
||||
return new ScreenDraw(KIND_FWTEXT, 0, 0, 0, x, y, value, cps, dx, ink);
|
||||
}
|
||||
}
|
||||
|
||||
private static final ScreenDraw[] NO_DRAWS = new ScreenDraw[0];
|
||||
|
||||
/** One composited full-screen frame plus the metadata the pipeline needs. */
|
||||
public static final class Composite {
|
||||
/** Full-screen 8bpp grayscale pixels, screenWidth*screenHeight bytes. */
|
||||
public final byte[] gray;
|
||||
public final int width;
|
||||
public final int height;
|
||||
/**
|
||||
* Stable identifier of screen content, combining every surface's
|
||||
* geometry and content fingerprint; equal fingerprints mean equal
|
||||
* composited pixels.
|
||||
*/
|
||||
public final String fingerprint;
|
||||
/** Monotonic: a Composite with a higher seq contains strictly newer state. */
|
||||
public final long seq;
|
||||
/**
|
||||
* Screen-space deferred draws of every visible surface, in composite
|
||||
* order (surface z ascending, then each surface's draw order). Their
|
||||
* pixels are already baked into gray.
|
||||
*/
|
||||
public final ScreenDraw[] draws;
|
||||
|
||||
Composite(byte[] gray, int width, int height, String fingerprint, long seq, ScreenDraw[] draws) {
|
||||
this.gray = gray;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.fingerprint = fingerprint;
|
||||
this.seq = seq;
|
||||
this.draws = draws == null ? NO_DRAWS : draws;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Surface {
|
||||
final String id;
|
||||
int x;
|
||||
int y;
|
||||
int width;
|
||||
int height;
|
||||
int zOrder;
|
||||
int transparency;
|
||||
boolean visible = true;
|
||||
byte[] pixels;
|
||||
String fingerprint = "";
|
||||
/** Surface-local deferred draws of the retained content (already baked into pixels). */
|
||||
ScreenDraw[] draws = NO_DRAWS;
|
||||
|
||||
Surface(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
private final Object lock = new Object();
|
||||
private int screenWidth;
|
||||
private int screenHeight;
|
||||
private boolean blanked;
|
||||
/** See setUnderlayDim: surfaces with zOrder below this are dimmed by underlayDim/256. */
|
||||
private int underlayDimBelowZOrder = Integer.MIN_VALUE;
|
||||
private int underlayDim = 256;
|
||||
private final Map<String, Surface> surfaces = new HashMap<>();
|
||||
private long nextCompositeSeq = 1;
|
||||
|
||||
/** Set the output frame size. Must be called before any surface work. */
|
||||
public void configureScreen(int width, int height) {
|
||||
if (width <= 0 || height <= 0) {
|
||||
throw new IllegalArgumentException("bad screen size " + width + "x" + height);
|
||||
}
|
||||
synchronized (lock) {
|
||||
this.screenWidth = width;
|
||||
this.screenHeight = height;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a surface or update an existing one's geometry. A resize discards
|
||||
* the surface's retained pixels (reset to 0).
|
||||
*/
|
||||
public void configureSurface(String id, int x, int y, int width, int height, int zOrder, int transparency) {
|
||||
if (id == null || id.isEmpty()) {
|
||||
throw new IllegalArgumentException("surface id must be non-empty");
|
||||
}
|
||||
if (width <= 0 || height <= 0) {
|
||||
throw new IllegalArgumentException("bad surface size " + width + "x" + height + " for " + id);
|
||||
}
|
||||
if (transparency != TRANSPARENCY_OPAQUE && transparency != TRANSPARENCY_COLOR_KEY) {
|
||||
throw new IllegalArgumentException("bad transparency mode " + transparency + " for " + id);
|
||||
}
|
||||
synchronized (lock) {
|
||||
requireScreenConfiguredLocked();
|
||||
Surface surface = surfaces.get(id);
|
||||
if (surface == null) {
|
||||
surface = new Surface(id);
|
||||
surfaces.put(id, surface);
|
||||
}
|
||||
if (surface.pixels == null || surface.width != width || surface.height != height) {
|
||||
surface.pixels = new byte[width * height];
|
||||
surface.fingerprint = "";
|
||||
surface.draws = NO_DRAWS;
|
||||
}
|
||||
surface.x = x;
|
||||
surface.y = y;
|
||||
surface.width = width;
|
||||
surface.height = height;
|
||||
surface.zOrder = zOrder;
|
||||
surface.transparency = transparency;
|
||||
}
|
||||
}
|
||||
|
||||
public void removeSurface(String id) {
|
||||
synchronized (lock) {
|
||||
surfaces.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dim every surface whose zOrder is below belowZOrder to factor256/256 of
|
||||
* its brightness (256 = no dimming): how a shell overlay that dims what it
|
||||
* covers (the TS Layer.dimUnderneath) reaches the window surfaces beneath
|
||||
* the shell surface, which the shell's own layer stack cannot paint.
|
||||
* Visible pixels stay at least 1 (the color-key black); glyph and
|
||||
* firmware-text draws keep their cached-draw form with a dimmed value;
|
||||
* image draws leave the composite's draw list (their pixels, already
|
||||
* baked into the surface, dim as raster). Takes effect when the next
|
||||
* frame composites.
|
||||
*/
|
||||
public void setUnderlayDim(int belowZOrder, int factor256) {
|
||||
synchronized (lock) {
|
||||
underlayDimBelowZOrder = belowZOrder;
|
||||
underlayDim = Math.max(0, Math.min(256, factor256));
|
||||
}
|
||||
}
|
||||
|
||||
/** The dim factor (256 = none) that applies to a surface. */
|
||||
private int dimForLocked(Surface surface) {
|
||||
return surface.zOrder < underlayDimBelowZOrder ? underlayDim : 256;
|
||||
}
|
||||
|
||||
private static int dimValue(int value, int dim) {
|
||||
return Math.max(1, (value * dim + 128) >> 8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hidden surfaces keep their retained pixels and accept updates, but are
|
||||
* excluded from the composite and its fingerprint (used for background
|
||||
* windows). Takes effect when the next frame composites.
|
||||
*/
|
||||
public void setSurfaceVisible(String id, boolean visible) {
|
||||
synchronized (lock) {
|
||||
Surface surface = surfaces.get(id);
|
||||
if (surface == null) {
|
||||
throw new IllegalArgumentException("unknown surface " + id);
|
||||
}
|
||||
surface.visible = visible;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* While blanked (screen off), composites are all-zero regardless of
|
||||
* surface content; retained state is untouched, so unblanking restores
|
||||
* the screen without asking sources to repaint.
|
||||
*/
|
||||
public void setBlanked(boolean blanked) {
|
||||
synchronized (lock) {
|
||||
this.blanked = blanked;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply an update to one surface and composite the whole screen, as one
|
||||
* atomic step. The update covers the rect (rectX, rectY, rectWidth,
|
||||
* rectHeight) in surface-local coordinates; pixels must hold exactly
|
||||
* rectWidth*rectHeight bytes. contentFingerprint identifies the surface's
|
||||
* full content after this update.
|
||||
*/
|
||||
public Composite applyAndComposite(
|
||||
String surfaceId,
|
||||
ByteBuffer pixels,
|
||||
int rectX,
|
||||
int rectY,
|
||||
int rectWidth,
|
||||
int rectHeight,
|
||||
String contentFingerprint
|
||||
) {
|
||||
return applyAndComposite(surfaceId, pixels, rectX, rectY, rectWidth, rectHeight,
|
||||
contentFingerprint, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* As above, with the frame's deferred draws: a little-endian buffer of
|
||||
* tagged records
|
||||
* [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] (glyph)
|
||||
* [1][imageId u32][x s16][y s16] (image)
|
||||
* in surface-local coordinates and draw order. The list describes the
|
||||
* surface's FULL retained content and replaces the previous list, so it is
|
||||
* only meaningful for full-surface updates (which is what every submitter
|
||||
* sends); pass null to clear. Draw pixels must already be baked into pixels.
|
||||
*/
|
||||
public Composite applyAndComposite(
|
||||
String surfaceId,
|
||||
ByteBuffer pixels,
|
||||
int rectX,
|
||||
int rectY,
|
||||
int rectWidth,
|
||||
int rectHeight,
|
||||
String contentFingerprint,
|
||||
ByteBuffer glyphs
|
||||
) {
|
||||
ScreenDraw[] parsed = parseDraws(glyphs);
|
||||
synchronized (lock) {
|
||||
requireScreenConfiguredLocked();
|
||||
Surface surface = surfaces.get(surfaceId);
|
||||
if (surface == null) {
|
||||
throw new IllegalArgumentException("unknown surface " + surfaceId);
|
||||
}
|
||||
if (rectX < 0 || rectY < 0 || rectWidth <= 0 || rectHeight <= 0
|
||||
|| rectX + rectWidth > surface.width || rectY + rectHeight > surface.height) {
|
||||
throw new IllegalArgumentException("update rect " + rectWidth + "x" + rectHeight
|
||||
+ "+" + rectX + "+" + rectY + " outside surface " + surfaceId
|
||||
+ " (" + surface.width + "x" + surface.height + ")");
|
||||
}
|
||||
int expectedBytes = rectWidth * rectHeight;
|
||||
if (pixels == null || pixels.remaining() != expectedBytes) {
|
||||
throw new IllegalArgumentException("update buffer for " + surfaceId + " has "
|
||||
+ (pixels == null ? 0 : pixels.remaining()) + " bytes, expected " + expectedBytes);
|
||||
}
|
||||
for (int row = 0; row < rectHeight; row++) {
|
||||
int dstOffset = (rectY + row) * surface.width + rectX;
|
||||
pixels.get(surface.pixels, dstOffset, rectWidth);
|
||||
}
|
||||
surface.fingerprint = contentFingerprint == null ? "" : contentFingerprint;
|
||||
surface.draws = parsed;
|
||||
return compositeLocked();
|
||||
}
|
||||
}
|
||||
|
||||
private static ScreenDraw[] parseDraws(ByteBuffer draws) {
|
||||
if (draws == null || draws.remaining() < 1) {
|
||||
return NO_DRAWS;
|
||||
}
|
||||
ByteBuffer in = draws.order(java.nio.ByteOrder.LITTLE_ENDIAN);
|
||||
List<ScreenDraw> out = new ArrayList<>();
|
||||
while (in.remaining() >= 1) {
|
||||
int kind = in.get() & 0xff;
|
||||
if (kind == ScreenDraw.KIND_GLYPH && in.remaining() >= 11) {
|
||||
int fontId = in.getShort() & 0xffff;
|
||||
int encoding = in.getInt();
|
||||
int penX = in.getShort();
|
||||
int lineY = in.getShort();
|
||||
int value = in.get() & 0xff;
|
||||
out.add(ScreenDraw.glyph(fontId, encoding, penX, lineY, value));
|
||||
} else if (kind == ScreenDraw.KIND_IMAGE && in.remaining() >= 8) {
|
||||
int imageId = in.getInt();
|
||||
int x = in.getShort();
|
||||
int y = in.getShort();
|
||||
out.add(ScreenDraw.image(imageId, x, y));
|
||||
} else if (kind == ScreenDraw.KIND_FWTEXT && in.remaining() >= 6) {
|
||||
int x = in.getShort();
|
||||
int y = in.getShort();
|
||||
int value = in.get() & 0xff;
|
||||
int count = in.get() & 0xff;
|
||||
if (in.remaining() < count * 7) {
|
||||
break; // malformed tail: keep what parsed cleanly
|
||||
}
|
||||
int[] cps = new int[count];
|
||||
int[] dx = new int[count];
|
||||
boolean[] ink = new boolean[count];
|
||||
for (int i = 0; i < count; i++) {
|
||||
cps[i] = in.getInt();
|
||||
dx[i] = in.getShort();
|
||||
ink[i] = in.get() != 0;
|
||||
}
|
||||
out.add(ScreenDraw.fwText(x, y, value, cps, dx, ink));
|
||||
} else {
|
||||
break; // malformed tail: keep what parsed cleanly
|
||||
}
|
||||
}
|
||||
return out.toArray(new ScreenDraw[0]);
|
||||
}
|
||||
|
||||
/** Composite the current retained state without applying an update. */
|
||||
public Composite composite() {
|
||||
synchronized (lock) {
|
||||
requireScreenConfiguredLocked();
|
||||
return compositeLocked();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Current composited pixels for the phone-side preview / screenshot, or
|
||||
* null before the screen is configured. Does not consume a sequence
|
||||
* number (it is never stored as the desired frame).
|
||||
*/
|
||||
public Composite previewComposite() {
|
||||
synchronized (lock) {
|
||||
if (screenWidth <= 0 || screenHeight <= 0) {
|
||||
return null;
|
||||
}
|
||||
byte[] gray = buildGrayLocked();
|
||||
return new Composite(gray, screenWidth, screenHeight, "preview", 0, NO_DRAWS);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] buildGrayLocked() {
|
||||
byte[] gray = new byte[screenWidth * screenHeight];
|
||||
if (blanked) {
|
||||
return gray;
|
||||
}
|
||||
List<Surface> ordered = new ArrayList<>(surfaces.values());
|
||||
ordered.sort(Comparator
|
||||
.comparingInt((Surface s) -> s.zOrder)
|
||||
.thenComparing(s -> s.id));
|
||||
for (Surface surface : ordered) {
|
||||
if (surface.visible) {
|
||||
blendLocked(gray, surface);
|
||||
}
|
||||
}
|
||||
return gray;
|
||||
}
|
||||
|
||||
private Composite compositeLocked() {
|
||||
byte[] gray = new byte[screenWidth * screenHeight];
|
||||
if (blanked) {
|
||||
return new Composite(gray, screenWidth, screenHeight,
|
||||
"blanked:" + screenWidth + "x" + screenHeight, nextCompositeSeq++, NO_DRAWS);
|
||||
}
|
||||
List<Surface> ordered = new ArrayList<>(surfaces.values());
|
||||
ordered.sort(Comparator
|
||||
.comparingInt((Surface s) -> s.zOrder)
|
||||
.thenComparing(s -> s.id));
|
||||
StringBuilder fingerprint = new StringBuilder();
|
||||
fingerprint.append(screenWidth).append('x').append(screenHeight);
|
||||
List<ScreenDraw> draws = new ArrayList<>();
|
||||
for (Surface surface : ordered) {
|
||||
if (!surface.visible) continue;
|
||||
blendLocked(gray, surface);
|
||||
int dim = dimForLocked(surface);
|
||||
for (ScreenDraw draw : surface.draws) {
|
||||
if (dim < 256 && draw.kind == ScreenDraw.KIND_IMAGE) continue;
|
||||
int value = dim < 256 ? dimValue(draw.value, dim) : draw.value;
|
||||
draws.add(new ScreenDraw(draw.kind, draw.fontId, draw.encoding, draw.imageId,
|
||||
draw.x + surface.x, draw.y + surface.y, value,
|
||||
draw.fwCps, draw.fwDx, draw.fwInk));
|
||||
}
|
||||
fingerprint.append('|').append(surface.id)
|
||||
.append('@').append(surface.x).append(',').append(surface.y)
|
||||
.append('+').append(surface.width).append('x').append(surface.height)
|
||||
.append('#').append(surface.zOrder)
|
||||
.append(':').append(surface.transparency)
|
||||
.append(':').append(surface.fingerprint);
|
||||
if (dim < 256) {
|
||||
fingerprint.append(":dim").append(dim);
|
||||
}
|
||||
}
|
||||
return new Composite(gray, screenWidth, screenHeight, fingerprint.toString(), nextCompositeSeq++,
|
||||
draws.toArray(new ScreenDraw[0]));
|
||||
}
|
||||
|
||||
private void blendLocked(byte[] gray, Surface surface) {
|
||||
// Clip the surface rect to the screen.
|
||||
int srcX = Math.max(0, -surface.x);
|
||||
int srcY = Math.max(0, -surface.y);
|
||||
int dstX = Math.max(0, surface.x);
|
||||
int dstY = Math.max(0, surface.y);
|
||||
int copyWidth = Math.min(surface.width - srcX, screenWidth - dstX);
|
||||
int copyHeight = Math.min(surface.height - srcY, screenHeight - dstY);
|
||||
if (copyWidth <= 0 || copyHeight <= 0) {
|
||||
return;
|
||||
}
|
||||
int dim = dimForLocked(surface);
|
||||
for (int row = 0; row < copyHeight; row++) {
|
||||
int srcOffset = (srcY + row) * surface.width + srcX;
|
||||
int dstOffset = (dstY + row) * screenWidth + dstX;
|
||||
if (dim < 256) {
|
||||
// Dimmed: opaque or color-keyed, 0 stays 0 (black / transparent)
|
||||
// and everything visible scales, never below 1.
|
||||
for (int col = 0; col < copyWidth; col++) {
|
||||
int value = surface.pixels[srcOffset + col] & 0xff;
|
||||
if (value != 0) {
|
||||
gray[dstOffset + col] = (byte) dimValue(value, dim);
|
||||
} else if (surface.transparency == TRANSPARENCY_OPAQUE) {
|
||||
gray[dstOffset + col] = 0;
|
||||
}
|
||||
}
|
||||
} else if (surface.transparency == TRANSPARENCY_OPAQUE) {
|
||||
System.arraycopy(surface.pixels, srcOffset, gray, dstOffset, copyWidth);
|
||||
} else {
|
||||
for (int col = 0; col < copyWidth; col++) {
|
||||
byte value = surface.pixels[srcOffset + col];
|
||||
if (value != 0) {
|
||||
gray[dstOffset + col] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void requireScreenConfiguredLocked() {
|
||||
if (screenWidth <= 0 || screenHeight <= 0) {
|
||||
throw new IllegalStateException("configureScreen must be called first");
|
||||
}
|
||||
}
|
||||
}
|
||||
-555
@@ -1,555 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import android.util.Log;
|
||||
|
||||
public final class BleImageOptimizer {
|
||||
private BleImageOptimizer() {}
|
||||
private static final String TAG = "BleImageOptimizer";
|
||||
|
||||
/**
|
||||
* Split an image into fragments (packets), taking advantage of the fact that each packet can declare
|
||||
* a logical size longer than its byte payload to get free trailing zero-padding.
|
||||
*
|
||||
* Each fragment's logical size stays within maxFragmentSize, while trailing zeros inside that span can
|
||||
* be omitted from the payload.
|
||||
*/
|
||||
public static List<BleProtocol.ImageFragment> planImageFragments(byte[] bmp, int maxFragmentSize) {
|
||||
return planImageFragments(bmp, maxFragmentSize, false);
|
||||
}
|
||||
|
||||
public static List<BleProtocol.ImageFragment> planImageFragments(
|
||||
byte[] bmp,
|
||||
int maxFragmentSize,
|
||||
boolean reserveFinalByte
|
||||
) {
|
||||
if (bmp == null || bmp.length == 0) {
|
||||
return Collections.singletonList(new BleProtocol.ImageFragment(0, new byte[0], 0));
|
||||
}
|
||||
if (maxFragmentSize <= 0) {
|
||||
throw new IllegalArgumentException("maxFragmentSize must be positive");
|
||||
}
|
||||
|
||||
final int bulkLength = reserveFinalByte ? bmp.length - 1 : bmp.length;
|
||||
final int fragmentCount = ((bulkLength + maxFragmentSize - 1) / maxFragmentSize)
|
||||
+ (bulkLength < bmp.length ? 1 : 0);
|
||||
final List<BleProtocol.ImageFragment> fragments = new ArrayList<>(fragmentCount);
|
||||
for (int index = 0; index * maxFragmentSize < bulkLength; index++) {
|
||||
int start = index * maxFragmentSize;
|
||||
int end = Math.min(start + maxFragmentSize, bulkLength);
|
||||
fragments.add(buildFragment(index, bmp, start, end));
|
||||
}
|
||||
if (bulkLength < bmp.length) {
|
||||
fragments.add(new BleProtocol.ImageFragment(
|
||||
fragments.size(),
|
||||
Arrays.copyOfRange(bmp, bulkLength, bmp.length),
|
||||
bmp.length - bulkLength
|
||||
));
|
||||
}
|
||||
Log.i(TAG, "planImageFragments: fragments=" + fragments.size() + ", bmp.length=" + bmp.length + ", maxFragmentSize=" + maxFragmentSize);
|
||||
return fragments;
|
||||
}
|
||||
|
||||
private static BleProtocol.ImageFragment buildFragment(int index, byte[] bmp, int start, int end) {
|
||||
int dataEnd = end;
|
||||
while (dataEnd > start && bmp[dataEnd - 1] == 0) {
|
||||
dataEnd -= 1;
|
||||
}
|
||||
if (dataEnd == start) {
|
||||
return new BleProtocol.ImageFragment(index, new byte[0], end - start);
|
||||
}
|
||||
return new BleProtocol.ImageFragment(
|
||||
index,
|
||||
Arrays.copyOfRange(bmp, start, dataEnd),
|
||||
end - start
|
||||
);
|
||||
}
|
||||
|
||||
public static final class TileImagePlan {
|
||||
final int tileIndex;
|
||||
final BleProtocol.ImageTileOptions tile;
|
||||
// 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 rle(4bpp)
|
||||
final int sessionId;
|
||||
List<BleProtocol.ImageFragment> fragments = Collections.emptyList();
|
||||
|
||||
TileImagePlan(int tileIndex, BleProtocol.ImageTileOptions tile, byte[] packed, int width, int height, int sessionId) {
|
||||
this.tileIndex = tileIndex;
|
||||
this.tile = tile;
|
||||
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).
|
||||
* 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[] packed, int width, int height, int sessionId, byte[] payloadOverride) {
|
||||
this.tileIndex = tileIndex;
|
||||
this.tile = tile;
|
||||
this.packed = packed == null ? new byte[0] : packed;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
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][fid_lo][fid_hi][rle(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.
|
||||
*
|
||||
* frameId is a little-endian uint16 the CFW uses to detect reordered,
|
||||
* skipped or duplicated deltas (its diagnostic overlay); the caller advances
|
||||
* it by 1 per emitted delta so consecutive deltas are consecutive.
|
||||
*/
|
||||
/**
|
||||
* 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[] previous, byte[] next, int width, int height, int frameId) {
|
||||
int stride = (width + 1) >> 1;
|
||||
if (width <= 0 || height <= 0 || previous == null || next == null
|
||||
|| previous.length != stride * height || next.length != stride * height) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
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.
|
||||
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
|
||||
}
|
||||
|
||||
byte[] out = encodeMode3Rect(next, stride, left, top, boxWidth, boxHeight, frameId);
|
||||
return new IncrementalPlan(out, changedBytes, (boxWidth >> 1) * boxHeight, clusterCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* The aligned changed bounding box between two same-size packed frames, or
|
||||
* null when they are identical: pixel-space {left, top, width, height} with
|
||||
* left/width multiples of 4 and top/height multiples of 2 (mode-3 header
|
||||
* quantization). Exposed so the texture-cache planner can reuse the exact
|
||||
* rect geometry the single-bbox path would send.
|
||||
*/
|
||||
public static int[] computeChangedBox(byte[] previous, byte[] next, int width, int height) {
|
||||
int stride = (width + 1) >> 1;
|
||||
if (width <= 0 || height <= 0 || previous == null || next == null
|
||||
|| previous.length != stride * height || next.length != stride * height) {
|
||||
return null;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (maxY < 0) {
|
||||
return null;
|
||||
}
|
||||
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);
|
||||
return new int[]{left, top, rightExclusive - left, bottomExclusive - top};
|
||||
}
|
||||
|
||||
/**
|
||||
* The multi-rect split of the changed region (see computeSplitRects), or null
|
||||
* when it is one region / too fragmented / the frames aren't comparable.
|
||||
* Exposed for the texture-cache planner.
|
||||
*/
|
||||
public static List<int[]> computeChangedRects(byte[] previous, byte[] next, int width, int height, int maxRects) {
|
||||
int stride = (width + 1) >> 1;
|
||||
if (width <= 0 || height <= 0 || previous == null || next == null
|
||||
|| previous.length != stride * height || next.length != stride * height) {
|
||||
return null;
|
||||
}
|
||||
List<int[]> rects = computeSplitRects(previous, next, stride, width, height, maxRects);
|
||||
return (rects == null || rects.size() < 2) ? null : rects;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode one CFW mode-3 rectangle delta:
|
||||
* [3][left/4][top/2][width/4][height/2][fid_lo][fid_hi][rle(box pixels)]
|
||||
* left/width must be multiples of 4 (=> left>>1, width>>1 are whole bytes), top/
|
||||
* height multiples of 2. The box pixels are top-down rows of `next` (4bpp packed,
|
||||
* width>>1 bytes/row), run-length encoded before deflate. Shared by the single-bbox
|
||||
* path, each rect of a mode-8 multi-rect batch, and the texture-cache planner.
|
||||
*/
|
||||
static byte[] encodeMode3Rect(byte[] next, int stride, int left, int top,
|
||||
int boxWidth, int boxHeight, int fid) {
|
||||
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 = rleEncode(region);
|
||||
byte[] out = new byte[7 + 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);
|
||||
out[5] = (byte) (fid & 0xff); // fid_lo
|
||||
out[6] = (byte) ((fid >> 8) & 0xff); // fid_hi
|
||||
System.arraycopy(compressed, 0, out, 7, compressed.length);
|
||||
return out;
|
||||
}
|
||||
|
||||
/** A full repaint in independently decodable commands below the uint16 record limit. */
|
||||
static List<byte[]> encodeFullFrameBands(byte[] packed, int width, int height, int firstFid) {
|
||||
if (width <= 0 || width > 640 || (width & 3) != 0 || height <= 0 || height > 480
|
||||
|| (height & 1) != 0 || packed.length != width * height / 2) {
|
||||
throw new IllegalArgumentException("unsupported custom framebuffer dimensions");
|
||||
}
|
||||
List<byte[]> commands = new ArrayList<>();
|
||||
int fid = firstFid;
|
||||
for (int top = 0; top < height; top += 64) {
|
||||
commands.add(encodeMode3Rect(packed, width / 2, 0, top, width,
|
||||
Math.min(64, height - top), fid));
|
||||
fid = fid >= 0xfffe ? 1 : fid + 1;
|
||||
}
|
||||
return commands;
|
||||
}
|
||||
|
||||
/**
|
||||
* A CFW mode-8 multi-segment payload (`[8][count]` then per-rect
|
||||
* `[len16][mode-3 submsg]`) carrying several tight rectangle deltas instead of
|
||||
* one bounding box, plus diagnostics. Each rect is applied to the firmware
|
||||
* shadow with the panel push deferred, then presented once. Used when the change
|
||||
* splits into regions whose bounding box wastes bytes (distant edits, or a sparse
|
||||
* box); the caller falls back to the single-rect path when this returns null or
|
||||
* isn't smaller.
|
||||
*/
|
||||
public static final class MultiRectPlan {
|
||||
public final byte[] payload;
|
||||
public final int rectCount;
|
||||
public final int coveredBytes; // sum of rect region bytes, pre-compression
|
||||
public final int nextFid; // fid to resume from (rectCount consumed)
|
||||
|
||||
MultiRectPlan(byte[] payload, int rectCount, int coveredBytes, int nextFid) {
|
||||
this.payload = payload;
|
||||
this.rectCount = rectCount;
|
||||
this.coveredBytes = coveredBytes;
|
||||
this.nextFid = nextFid;
|
||||
}
|
||||
}
|
||||
|
||||
// Break-even tuning for the rect splitter. Splitting adds ~15 fixed bytes per
|
||||
// rect (seglen + mode-3 header + fid), so only split across gaps big enough to pay for that.
|
||||
// Gaps are in the changed-mask's native units: whole 4bpp bytes (2px) for
|
||||
// columns, rows for the vertical bands. Kept above the 4px/2px box alignment so
|
||||
// aligned rects never overlap.
|
||||
private static final int MULTI_RECT_V_GAP_ROWS = 4;
|
||||
private static final int MULTI_RECT_H_GAP_BYTES = 6;
|
||||
|
||||
/**
|
||||
* Build a mode-8 multi-rect payload for the change between two same-size 4bpp
|
||||
* frames, assigning consecutive frame ids starting at fidStart (each rect needs a
|
||||
* distinct fid — the CFW skips duplicate fids). Returns null when the change is a
|
||||
* single region (use the single-rect path), when it fragments past maxRects, or
|
||||
* when the frames aren't comparable.
|
||||
*/
|
||||
public static MultiRectPlan buildMultiRectImagePayload(byte[] previous, byte[] next, int width, int height, int fidStart, int maxRects) {
|
||||
int stride = (width + 1) >> 1;
|
||||
if (width <= 0 || height <= 0 || previous == null || next == null
|
||||
|| previous.length != stride * height || next.length != stride * height) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<int[]> rects = computeSplitRects(previous, next, stride, width, height, maxRects);
|
||||
if (rects == null || rects.size() < 2) {
|
||||
return null; // single region (or too fragmented): caller uses the bbox path
|
||||
}
|
||||
|
||||
List<byte[]> subs = new ArrayList<>(rects.size());
|
||||
int coveredBytes = 0;
|
||||
int fid = fidStart;
|
||||
int total = 2; // [8][count]
|
||||
for (int[] r : rects) {
|
||||
byte[] sub = encodeMode3Rect(next, stride, r[0], r[1], r[2], r[3], fid);
|
||||
subs.add(sub);
|
||||
coveredBytes += (r[2] >> 1) * r[3];
|
||||
total += 2 + sub.length; // len16 + submsg
|
||||
fid = (fid >= 0xfffe) ? 1 : fid + 1;
|
||||
}
|
||||
|
||||
byte[] out = new byte[total];
|
||||
out[0] = 8;
|
||||
out[1] = (byte) rects.size();
|
||||
int pos = 2;
|
||||
for (byte[] sub : subs) {
|
||||
out[pos] = (byte) (sub.length & 0xff);
|
||||
out[pos + 1] = (byte) ((sub.length >> 8) & 0xff);
|
||||
pos += 2;
|
||||
System.arraycopy(sub, 0, out, pos, sub.length);
|
||||
pos += sub.length;
|
||||
}
|
||||
return new MultiRectPlan(out, rects.size(), coveredBytes, fid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Split the changed region between two packed 4bpp frames into a small set of
|
||||
* aligned rectangles that together cover every changed pixel: maximal vertical
|
||||
* bands of changed rows (merging gaps < V_GAP), each split into horizontal column
|
||||
* clusters (merging gaps < H_GAP), with each cluster's rows tightened to where it
|
||||
* actually changes. Rects are pixel-space {left,top,width,height}, left/width
|
||||
* aligned to 4px and top/height to 2px. Returns null if there's no change or the
|
||||
* split exceeds maxRects (caller falls back to a single bounding box).
|
||||
*/
|
||||
private static List<int[]> computeSplitRects(byte[] previous, byte[] next, int stride, int width, int height, int maxRects) {
|
||||
boolean[] rowChanged = new boolean[height];
|
||||
boolean anyChange = false;
|
||||
for (int y = 0; y < height; y++) {
|
||||
int off = y * stride;
|
||||
for (int x = 0; x < stride; x++) {
|
||||
if (previous[off + x] != next[off + x]) {
|
||||
rowChanged[y] = true;
|
||||
anyChange = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!anyChange) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<int[]> rects = new ArrayList<>();
|
||||
boolean[] col = new boolean[stride];
|
||||
int y = 0;
|
||||
while (y < height) {
|
||||
if (!rowChanged[y]) { y++; continue; }
|
||||
// Extend a vertical band across changed rows, hopping unchanged gaps < V_GAP.
|
||||
int bandTop = y;
|
||||
int bandBot = y;
|
||||
int cur = y + 1;
|
||||
while (cur < height) {
|
||||
if (rowChanged[cur]) { bandBot = cur; cur++; continue; }
|
||||
int g = cur;
|
||||
while (g < height && !rowChanged[g]) g++;
|
||||
if (g < height && (g - cur) < MULTI_RECT_V_GAP_ROWS) { cur = g; } else break;
|
||||
}
|
||||
|
||||
// Columns changed anywhere in the band.
|
||||
Arrays.fill(col, false);
|
||||
for (int yy = bandTop; yy <= bandBot; yy++) {
|
||||
if (!rowChanged[yy]) continue;
|
||||
int off = yy * stride;
|
||||
for (int x = 0; x < stride; x++) {
|
||||
if (previous[off + x] != next[off + x]) col[x] = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Split the band into horizontal clusters, hopping column gaps < H_GAP.
|
||||
int x = 0;
|
||||
while (x < stride) {
|
||||
if (!col[x]) { x++; continue; }
|
||||
int cL = x;
|
||||
int cR = x;
|
||||
int cx = x + 1;
|
||||
while (cx < stride) {
|
||||
if (col[cx]) { cR = cx; cx++; continue; }
|
||||
int g = cx;
|
||||
while (g < stride && !col[g]) g++;
|
||||
if (g < stride && (g - cx) < MULTI_RECT_H_GAP_BYTES) { cx = g; } else break;
|
||||
}
|
||||
|
||||
// Tighten this cluster's rows to those that change within [cL,cR].
|
||||
int tTop = height;
|
||||
int tBot = -1;
|
||||
for (int yy = bandTop; yy <= bandBot; yy++) {
|
||||
if (!rowChanged[yy]) continue;
|
||||
int off = yy * stride;
|
||||
for (int xx = cL; xx <= cR; xx++) {
|
||||
if (previous[off + xx] != next[off + xx]) {
|
||||
if (yy < tTop) tTop = yy;
|
||||
tBot = yy;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (tBot >= 0) {
|
||||
int left = (cL * 2) & ~3;
|
||||
int rightExclusive = Math.min(width, (((cR + 1) * 2) + 3) & ~3);
|
||||
int top = tTop & ~1;
|
||||
int bottomExclusive = Math.min(height, (tBot + 2) & ~1);
|
||||
rects.add(new int[]{left, top, rightExclusive - left, bottomExclusive - top});
|
||||
if (rects.size() > maxRects) {
|
||||
return null; // too fragmented: a single bounding box is simpler
|
||||
}
|
||||
}
|
||||
x = cR + 1;
|
||||
}
|
||||
y = bandBot + 1;
|
||||
}
|
||||
return rects;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run-length encode headerless 4bpp pixels for CFW load_image_z mode 6.
|
||||
* Wire format: [6][rle(4bpp pixels)]. Always use mode 6: the logical
|
||||
* image can be larger than its EvenHub carrier, so a raw BMP fallback would
|
||||
* carry dimensions the legacy container loader cannot accept.
|
||||
*/
|
||||
static byte[] maybeCompress(byte[] packed, int width, int height) {
|
||||
if (packed == null || packed.length == 0) {
|
||||
return packed;
|
||||
}
|
||||
byte[] z = rleEncode(packed);
|
||||
byte[] out = new byte[z.length + 1];
|
||||
out[0] = 6;
|
||||
System.arraycopy(z, 0, out, 1, z.length);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run-length encode packed 4bpp pixels for CFW modes 3 and 6, whose payload is
|
||||
* RLE tokens; deflate belongs to the transport. Runs are over the pixel NIBBLES of
|
||||
* {@code pix} in wire order (high nibble = left pixel), including the pad nibble
|
||||
* that ends each row at odd widths — i.e. {@code pix} read as 2*length nibbles.
|
||||
* One token is:
|
||||
* <pre>
|
||||
* [cnt4|color4] cnt 1..15
|
||||
* [0|color4][cnt8] cnt 1..255
|
||||
* [0|color4][0][cntLo][cntHi] cnt 1..65535, little-endian
|
||||
* </pre>
|
||||
* with the low nibble always the color; runs longer than 65535 split across
|
||||
* tokens. Worst case (every nibble its own run) is exactly one byte per nibble,
|
||||
* which is what the buffer is sized for.
|
||||
*/
|
||||
static byte[] rleEncode(byte[] pix) {
|
||||
int n = pix.length * 2;
|
||||
byte[] out = new byte[n];
|
||||
int o = 0;
|
||||
int i = 0;
|
||||
while (i < n) {
|
||||
int v = nibbleAt(pix, i);
|
||||
int j = i + 1;
|
||||
while (j < n && nibbleAt(pix, j) == v) j++;
|
||||
int run = j - i;
|
||||
while (run > 0) {
|
||||
int c = Math.min(run, 0xffff);
|
||||
if (c <= 15) {
|
||||
out[o++] = (byte) ((c << 4) | v);
|
||||
} else if (c <= 255) {
|
||||
out[o++] = (byte) v; // high nibble 0 = escape to an 8-bit count
|
||||
out[o++] = (byte) c;
|
||||
} else {
|
||||
out[o++] = (byte) v;
|
||||
out[o++] = 0; // 8-bit count 0 = escape to a 16-bit count
|
||||
out[o++] = (byte) (c & 0xff);
|
||||
out[o++] = (byte) (c >> 8);
|
||||
}
|
||||
run -= c;
|
||||
}
|
||||
i = j;
|
||||
}
|
||||
return Arrays.copyOf(out, o);
|
||||
}
|
||||
|
||||
private static int nibbleAt(byte[] pix, int i) {
|
||||
int b = pix[i >> 1] & 0xff;
|
||||
return (i & 1) != 0 ? (b & 0x0f) : (b >> 4);
|
||||
}
|
||||
|
||||
public static final class ImageUpdateStats {
|
||||
final int paintMs;
|
||||
final int tileCount;
|
||||
final int frameId;
|
||||
long firstWriteStartedAtMs;
|
||||
|
||||
ImageUpdateStats(int paintMs, int tileCount, int frameId) {
|
||||
this.paintMs = Math.max(0, paintMs);
|
||||
this.tileCount = tileCount;
|
||||
this.frameId = frameId;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import android.os.SystemClock;
|
||||
|
||||
/**
|
||||
* Protobuf messages contain a 1-byte identifier that is used to identify
|
||||
* which message is referred to in ACK messages. We keep track of which
|
||||
* identifiers are in use for inflight messages, with some metadata about
|
||||
* what the message was. We allocate identifiers with an LRU policy so that
|
||||
* if we release a message's identifier because it timed out, we can still
|
||||
* detect late ACKs if it hasn't been reused yet.
|
||||
*/
|
||||
public final class BleMagicPool {
|
||||
static final int MIN_MAGIC = 100;
|
||||
static final int MAX_MAGIC = 255;
|
||||
|
||||
private final ArrayDeque<Integer> available = new ArrayDeque<>();
|
||||
private final boolean[] allocated = new boolean[256];
|
||||
private final Map<String, ReleaseRecord> releaseRecords = new HashMap<>();
|
||||
|
||||
BleMagicPool() {
|
||||
for (int magic = MIN_MAGIC; magic <= MAX_MAGIC; magic++) {
|
||||
available.addLast(magic);
|
||||
}
|
||||
}
|
||||
|
||||
synchronized int allocate() {
|
||||
Integer magic = available.pollFirst();
|
||||
if (magic == null) {
|
||||
throw new IllegalStateException("no BLE magic values available");
|
||||
}
|
||||
allocated[magic] = true;
|
||||
return magic;
|
||||
}
|
||||
|
||||
synchronized void release(int sid, int magic, String label, String reason) {
|
||||
if (magic < MIN_MAGIC || magic > MAX_MAGIC) {
|
||||
return;
|
||||
}
|
||||
releaseRecords.put(key(sid, magic), new ReleaseRecord(label, reason, SystemClock.elapsedRealtime()));
|
||||
if (!allocated[magic]) {
|
||||
return;
|
||||
}
|
||||
allocated[magic] = false;
|
||||
available.addLast(magic);
|
||||
}
|
||||
|
||||
synchronized ReleaseRecord getReleaseRecord(int sid, int magic) {
|
||||
return releaseRecords.get(key(sid, magic));
|
||||
}
|
||||
|
||||
private static String key(int sid, int magic) {
|
||||
return sid + ":" + magic;
|
||||
}
|
||||
|
||||
public static final class ReleaseRecord {
|
||||
final String label;
|
||||
final String reason;
|
||||
final long releasedAtMs;
|
||||
|
||||
ReleaseRecord(String label, String reason, long releasedAtMs) {
|
||||
this.label = label == null ? "" : label;
|
||||
this.reason = reason == null ? "" : reason;
|
||||
this.releasedAtMs = releasedAtMs;
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,38 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Ordered completion and go-back-N recovery for the custom-message window. */
|
||||
final class CfwMessageWindow {
|
||||
// Flappy capture: p99 ACK 63ms, max 77ms. Leave room for full-frame
|
||||
// processing while recovering a lost reply well before the stock timeout.
|
||||
static final int ACK_TIMEOUT_MS = 500;
|
||||
static final int MAX_RETRIES = 3;
|
||||
|
||||
private CfwMessageWindow() {}
|
||||
|
||||
static OutboundMessage acknowledgedHead(Iterable<OutboundMessage> messages) {
|
||||
for (OutboundMessage message : messages) {
|
||||
if (message.sid != CfwTransport.SID) continue;
|
||||
return !message.cfwRetryPending && message.cfwAckLenses == CfwTransport.BOTH ? message : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static List<OutboundMessage> replayWindow(Iterable<OutboundMessage> messages, long nowMs) {
|
||||
List<OutboundMessage> replay = new ArrayList<>();
|
||||
boolean failed = false;
|
||||
for (OutboundMessage message : messages) {
|
||||
if (message.sid != CfwTransport.SID) continue;
|
||||
failed |= message.cfwRetryPending || (message.cfwAckLenses != CfwTransport.BOTH
|
||||
&& message.ackDeadlineAtMs > 0 && message.ackDeadlineAtMs <= nowMs);
|
||||
replay.add(message);
|
||||
}
|
||||
// A lost ACK, first fragment, or NACK can leave an older attempt
|
||||
// unresolved. Replaying only the named failure strands that older
|
||||
// attempt at the head until its timeout. Rewind the unresolved window.
|
||||
if (!failed) replay.clear();
|
||||
return replay;
|
||||
}
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** Faceclaw/10 records: clear flags, wire length and decoded CRC; persistent zlib body. */
|
||||
public final class CfwTransport {
|
||||
public static final int SID = 0xf0;
|
||||
public static final int BOTH = 3;
|
||||
public static final int MAX_MESSAGE = 65535;
|
||||
public static final int COMPRESSED = 4, RESET_CONTEXT = 8;
|
||||
private final Deflater deflater = new Deflater();
|
||||
private boolean resetPending = true;
|
||||
private int previousLenses;
|
||||
|
||||
public CfwTransport() {}
|
||||
|
||||
public synchronized void reset() {
|
||||
deflater.reset();
|
||||
resetPending = true;
|
||||
previousLenses = 0;
|
||||
}
|
||||
|
||||
/** Compress only when writing, in exactly the order seen by the receiver. */
|
||||
public synchronized List<byte[]> encode(byte[] message, int streamId, int lenses, int mtu) {
|
||||
validate(message, streamId, lenses, mtu);
|
||||
if (previousLenses != lenses) reset();
|
||||
boolean reset = resetPending;
|
||||
deflater.setInput(message);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
byte[] chunk = new byte[4096];
|
||||
int count;
|
||||
do {
|
||||
count = deflater.deflate(chunk, 0, chunk.length, Deflater.SYNC_FLUSH);
|
||||
out.write(chunk, 0, count);
|
||||
} while (count == chunk.length);
|
||||
byte[] body = out.toByteArray();
|
||||
int flags = lenses | COMPRESSED | (reset ? RESET_CONTEXT : 0);
|
||||
if (body.length == 0) flags &= ~COMPRESSED; // repeated empty sync flush
|
||||
if (body.length > MAX_MESSAGE) {
|
||||
// Incompressible maximum-size records still fit as raw records.
|
||||
// The attempted deflate advanced history: reset both ends before reuse.
|
||||
reset();
|
||||
body = message;
|
||||
flags = lenses | RESET_CONTEXT;
|
||||
} else {
|
||||
resetPending = false;
|
||||
previousLenses = lenses;
|
||||
}
|
||||
return frameRecord(body, crc(message), streamId, flags, mtu);
|
||||
}
|
||||
|
||||
private static void validate(byte[] message, int streamId, int lenses, int mtu) {
|
||||
if (message == null || message.length > MAX_MESSAGE || streamId < 0 || streamId > 255
|
||||
|| lenses < 1 || lenses > BOTH || mtu < 23 || mtu > 517)
|
||||
throw new IllegalArgumentException("invalid CFW stream parameters");
|
||||
}
|
||||
|
||||
/** One logical command per stream for now. Packets are bounded by actual ATT MTU. */
|
||||
public static List<byte[]> frame(byte[] message, int streamId, int lenses, int mtu) {
|
||||
validate(message, streamId, lenses, mtu);
|
||||
return frameRecord(message, crc(message), streamId, lenses | RESET_CONTEXT, mtu);
|
||||
}
|
||||
|
||||
private static List<byte[]> frameRecord(byte[] body, int checksum, int streamId, int flags, int mtu) {
|
||||
int lenses = flags & BOTH;
|
||||
int capacity = Math.min(252, mtu - 14);
|
||||
byte[] stream = new byte[body.length + 5];
|
||||
stream[0] = (byte) flags;
|
||||
stream[1] = (byte) body.length;
|
||||
stream[2] = (byte) (body.length >>> 8);
|
||||
stream[3] = (byte) checksum;
|
||||
stream[4] = (byte) (checksum >>> 8);
|
||||
System.arraycopy(body, 0, stream, 5, body.length);
|
||||
List<byte[]> packets = new ArrayList<>();
|
||||
for (int offset = 0, index = 0; offset < stream.length; offset += capacity, index++) {
|
||||
int count = Math.min(capacity, stream.length - offset);
|
||||
byte[] packet = new byte[count + 11];
|
||||
packet[0] = (byte) 0xaa;
|
||||
packet[1] = 0x21;
|
||||
packet[2] = (byte) (streamId + index);
|
||||
packet[3] = (byte) (count + 3);
|
||||
packet[4] = packet[5] = 1;
|
||||
packet[6] = (byte) SID;
|
||||
packet[8] = (byte) (lenses | (offset == 0 ? 0x80 : 0)
|
||||
| (offset + count == stream.length ? 0x40 : 0));
|
||||
System.arraycopy(stream, offset, packet, 9, count);
|
||||
int crc = crc(packet, 8, count + 1);
|
||||
packet[packet.length - 2] = (byte) crc;
|
||||
packet[packet.length - 1] = (byte) (crc >>> 8);
|
||||
packets.add(packet);
|
||||
}
|
||||
return packets;
|
||||
}
|
||||
|
||||
public static int crc(byte[] data) { return crc(data, 0, data.length); }
|
||||
private static int crc(byte[] data, int offset, int count) {
|
||||
int crc = 0xffff;
|
||||
for (int i = offset; i < offset + count; i++) {
|
||||
crc ^= (data[i] & 255) << 8;
|
||||
for (int bit = 0; bit < 8; bit++)
|
||||
crc = ((crc << 1) ^ ((crc & 0x8000) != 0 ? 0x1021 : 0)) & 65535;
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
private static int u16(byte[] data, int offset) {
|
||||
return (data[offset] & 255) | ((data[offset + 1] & 255) << 8);
|
||||
}
|
||||
public static Ack parseAck(byte[] packet) {
|
||||
Ack[] acks = parseAcks(packet);
|
||||
return acks == null ? null : acks[0];
|
||||
}
|
||||
/** Primary ACK followed by up to three explicit preceding successes from
|
||||
* the same processing lens. Validate the entire packet before applying any. */
|
||||
public static Ack[] parseAcks(byte[] packet) {
|
||||
if (packet == null || packet.length < 19 || packet.length > 40
|
||||
|| (packet.length - 19) % 7 != 0 || (packet[0] & 255) != 0xaa
|
||||
|| packet[1] != 0x12 || (packet[3] & 255) != packet.length - 8 || packet[4] != 1 || packet[5] != 1
|
||||
|| (packet[6] & 255) != SID || packet[7] != 0 || (packet[8] != 1 && packet[8] != 3)
|
||||
|| (packet[12] != 1 && packet[12] != 2)
|
||||
|| (packet[8] == 3 && packet.length != 19)
|
||||
|| crc(packet, 8, packet.length - 10) != u16(packet, packet.length - 2)) return null;
|
||||
Ack[] acks = new Ack[1 + (packet.length - 19) / 7];
|
||||
acks[0] = new Ack(packet[8] == 3, packet[9] & 255, u16(packet, 10), packet[12], u16(packet, 13), u16(packet, 15));
|
||||
for (int i = 1, offset = 17; i < acks.length; i++, offset += 7)
|
||||
acks[i] = new Ack(false, packet[offset] & 255, u16(packet, offset + 1),
|
||||
packet[12], u16(packet, offset + 3), u16(packet, offset + 5));
|
||||
return acks;
|
||||
}
|
||||
public static final class Ack {
|
||||
public final boolean nack;
|
||||
public final int streamId, messageId, lens, size, checksum;
|
||||
Ack(boolean nack, int streamId, int messageId, int lens, int size, int checksum) {
|
||||
this.nack = nack;
|
||||
this.streamId = streamId; this.messageId = messageId; this.lens = lens;
|
||||
this.size = size; this.checksum = checksum;
|
||||
}
|
||||
}
|
||||
}
|
||||
-75
@@ -1,75 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
|
||||
public class ConnectionOptions {
|
||||
public static final int WRITE_TYPE = BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE;
|
||||
public static final int DESIRED_MTU = 512;
|
||||
|
||||
public static final int CONNECT_TIMEOUT_MS = 5_000;
|
||||
public static final int SERVICES_TIMEOUT_MS = 5_000;
|
||||
public static final int DESCRIPTOR_TIMEOUT_MS = 5_000;
|
||||
public static final int RING_DESIRED_MTU = 247;
|
||||
public static final int RING_RECONNECT_DELAY_MS = 2_000;
|
||||
public static final int WRITE_TIMEOUT_MS = 2_000;
|
||||
public static final int PRELUDE_TIMEOUT_MS = 2_000;
|
||||
// Application security-auth exchange (sid 0x80). The success notification
|
||||
// only arrives once the BLE link is encrypted, which on a first-time
|
||||
// connection means SMP pairing — possibly with an OS dialog the user has
|
||||
// to accept — so the stock-firmware paths wait generously.
|
||||
public static final int SECURITY_AUTH_TIMEOUT_MS = 30_000;
|
||||
// The main communicator's wait is soft (it proceeds on timeout so firmware
|
||||
// that never answers can't stall every reconnect), so it is kept short.
|
||||
public static final int SECURITY_AUTH_SOFT_TIMEOUT_MS = 6_000;
|
||||
public static final int ACK_TIMEOUT_MS = 3_500;
|
||||
public static final int HEARTBEAT_FAILURE_DEADLINE_MS = 10_000;
|
||||
public static final int HEARTBEAT_READY_MS = 4_000;
|
||||
public static final int HEARTBEAT_URGENT_MS = 6_000;
|
||||
public static final int BATTERY_REFRESH_INTERVAL_MS = 5 * 60_000;
|
||||
public static final int BATTERY_INPUT_QUIET_MS = 5_000;
|
||||
// Poll cadence while in charging mode; also bounds how quickly we notice
|
||||
// the glasses coming back out of the case.
|
||||
public static final int CHARGING_BATTERY_POLL_MS = 30_000;
|
||||
public static final int IMAGE_FRAGMENT_SIZE = 3800;
|
||||
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;
|
||||
public static final int IDLE_SLEEP_MS = 100;
|
||||
public static final int RECONNECT_DELAY_MS = 2_000;
|
||||
|
||||
final boolean sendImagesToLeft = true;
|
||||
final boolean skipSessionIds = true;
|
||||
// Max messages in flight (awaiting ack) at once — full pipelining. >1 lets a
|
||||
// frame be sent before the previous frame's ack returns, hiding the BLE ack
|
||||
// round-trip that otherwise serialized every update. Requires firmware that
|
||||
// accepts pipelined image messages (the CFW snapshot/deferred FIFO). At 1 the
|
||||
// communicator behaves exactly as before (serial sends + last-packet prewrite).
|
||||
final int WINDOW_SIZE = 3;
|
||||
// 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 = true;
|
||||
|
||||
// Send several tight rectangle deltas as one CFW mode-8 multi-segment message
|
||||
// instead of a single bounding box, when the change splits into regions whose
|
||||
// bounding box wastes bytes (distant edits, or a sparse box). Requires firmware
|
||||
// with mode-8 multi-rect support; needs INCREMENTAL_FRAMES (same seeded shadow).
|
||||
// Falls back to the single bounding box whenever multi-rect isn't smaller.
|
||||
final boolean MULTI_RECT_FRAMES = true;
|
||||
// Cap on rects per batch. Each rect consumes a distinct CFW frame id, and the
|
||||
// firmware's duplicate-fid ring holds 16, so keep several batches of history
|
||||
// within it. Above this the split is abandoned for a single bounding box.
|
||||
public static final int MULTI_RECT_MAX_RECTS = 6;
|
||||
// Don't bother splitting when the single bounding box already compresses this
|
||||
// small (one tight rect); only attempt multi-rect above it or when the box
|
||||
// spans multiple horizontal clusters.
|
||||
public static final int MULTI_RECT_MIN_PAYLOAD = 900;
|
||||
|
||||
// Ship text as on-glasses cached-glyph draws (CFW modes 12/14) instead of
|
||||
// pixels, punching glyph ink out of the baked deltas (see TexturePlanner).
|
||||
// Also gated at runtime on the firmware advertising texcache12 + texstr14.
|
||||
final boolean TEXTURE_CACHE_FRAMES = true;
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public final class G2Event {
|
||||
final String kind;
|
||||
final String containerName;
|
||||
final int eventType;
|
||||
final int eventSource;
|
||||
final int systemExitReasonCode;
|
||||
// IMU_Report_Data (Sys_ItemEvent.IMUData), populated when the firmware
|
||||
// attaches an accelerometer reading to a sys-event.
|
||||
final boolean hasImu;
|
||||
final double imuX;
|
||||
final double imuY;
|
||||
final double imuZ;
|
||||
|
||||
public G2Event(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode) {
|
||||
this(kind, containerName, eventType, eventSource, systemExitReasonCode, false, 0, 0, 0);
|
||||
}
|
||||
|
||||
public G2Event(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode,
|
||||
boolean hasImu, double imuX, double imuY, double imuZ) {
|
||||
this.kind = kind;
|
||||
this.containerName = containerName;
|
||||
this.eventType = eventType;
|
||||
this.eventSource = eventSource;
|
||||
this.systemExitReasonCode = systemExitReasonCode;
|
||||
this.hasImu = hasImu;
|
||||
this.imuX = imuX;
|
||||
this.imuY = imuY;
|
||||
this.imuZ = imuZ;
|
||||
}
|
||||
|
||||
public static G2Event decode(BleProtocol.ParsedFrame frame) {
|
||||
byte[] pb = BleProtocol.stripTrailingCrc(frame.pb);
|
||||
|
||||
// sid 0x07 is the stock Even AI app, not EvenHub, so it carries an
|
||||
// EvenAIDataPackage rather than the device-event wrapper below.
|
||||
// EvenAIDataPackage { command_id = 1, magic_random = 2, ctrl = 3 },
|
||||
// EvenAIControl { status = 1 }. The event we care about is
|
||||
// command_id=CTRL with status=WAKE_UP, which the firmware emits when the
|
||||
// on-glasses "Hey Even" wakeword fires.
|
||||
if (frame.sid == BleProtocol.SID_EVEN_AI) {
|
||||
int commandId = BleProtocol.readVarintFieldValue(pb, 1, -1);
|
||||
if (commandId != BleProtocol.EVEN_AI_CMD_CTRL) {
|
||||
return null;
|
||||
}
|
||||
byte[] ctrl = BleProtocol.readFieldBytes(pb, 3);
|
||||
if (ctrl == null) {
|
||||
return null;
|
||||
}
|
||||
int status = BleProtocol.readVarintFieldValue(ctrl, 1, -1);
|
||||
if (status < 0) {
|
||||
return null;
|
||||
}
|
||||
return new G2Event("even-ai", "", status, 0, 0);
|
||||
}
|
||||
|
||||
byte[] deviceEvent = BleProtocol.readFieldBytes(pb, 13);
|
||||
if (deviceEvent == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] listEvent = BleProtocol.readFieldBytes(deviceEvent, 1);
|
||||
if (listEvent != null) {
|
||||
return new G2Event(
|
||||
"list-click",
|
||||
BleProtocol.readStringFieldValue(listEvent, 2),
|
||||
BleProtocol.readVarintFieldValue(listEvent, 5, BleProtocol.EVENT_CLICK),
|
||||
0,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
byte[] textEvent = BleProtocol.readFieldBytes(deviceEvent, 2);
|
||||
if (textEvent != null) {
|
||||
return new G2Event(
|
||||
"text-click",
|
||||
BleProtocol.readStringFieldValue(textEvent, 2),
|
||||
BleProtocol.readVarintFieldValue(textEvent, 3, BleProtocol.EVENT_CLICK),
|
||||
0,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
byte[] sysEvent = BleProtocol.readFieldBytes(deviceEvent, 3);
|
||||
if (sysEvent != null) {
|
||||
int eventType = BleProtocol.readVarintFieldValue(sysEvent, 1, BleProtocol.EVENT_CLICK);
|
||||
int eventSource = BleProtocol.readVarintFieldValue(sysEvent, 2, 0);
|
||||
int exitReason = BleProtocol.readVarintFieldValue(sysEvent, 4, 0);
|
||||
// IMUData (field 3) is IMU_Report_Data { double x=1, y=2, z=3 }. It
|
||||
// may accompany any sys-event, or arrive as a standalone
|
||||
// IMU_DATA_REPORT sample.
|
||||
byte[] imu = BleProtocol.readFieldBytes(sysEvent, 3);
|
||||
if (imu != null) {
|
||||
double x = BleProtocol.readFloatFieldValue(imu, 1, 0);
|
||||
double y = BleProtocol.readFloatFieldValue(imu, 2, 0);
|
||||
double z = BleProtocol.readFloatFieldValue(imu, 3, 0);
|
||||
return new G2Event("sys-event", "", eventType, eventSource, exitReason, true, x, y, z);
|
||||
}
|
||||
return new G2Event("sys-event", "", eventType, eventSource, exitReason);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String hex(byte[] data) {
|
||||
if (data == null) {
|
||||
return "null";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder(data.length * 2);
|
||||
for (byte b : data) {
|
||||
sb.append(String.format("%02x", b & 0xff));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,281 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public class MessageBuilder {
|
||||
private static final int ACK_TIMEOUT_MS = 3_500;
|
||||
private static final int HEARTBEAT_TIMEOUT_MS = 1_500;
|
||||
|
||||
private BleMagicPool magicPool;
|
||||
|
||||
public MessageBuilder(BleMagicPool magicPool) {
|
||||
this.magicPool = magicPool;
|
||||
}
|
||||
|
||||
public OutboundMessage prelude() {
|
||||
return new OutboundMessage(
|
||||
"prelude", "prelude",
|
||||
BleProtocol.PRELUDE_ACK_SID,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
BleProtocol.PRELUDE_ACK_MAGIC,
|
||||
BleProtocol.PRELUDE_F5872_PAYLOAD,
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
/** The per-connection sid-0x80 security-auth request (one per arm). */
|
||||
public OutboundMessage securityAuth(boolean leftArm) {
|
||||
int magic = magicPool.allocate();
|
||||
return new OutboundMessage(
|
||||
"security-auth",
|
||||
"security auth " + (leftArm ? "L" : "R"),
|
||||
BleProtocol.SID_SECURITY_AUTH,
|
||||
BleProtocol.FLAG_SECURITY_AUTH,
|
||||
magic,
|
||||
BleProtocol.buildAuthenticationRequest(magic),
|
||||
ConnectionOptions.SECURITY_AUTH_SOFT_TIMEOUT_MS,
|
||||
-1,
|
||||
leftArm
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage shutdown(int exitMode) {
|
||||
int magic = magicPool.allocate();
|
||||
return new OutboundMessage(
|
||||
"shutdown",
|
||||
"shutdown mode=" + exitMode,
|
||||
BleProtocol.SID_EVENHUB,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildShutdown(magic, exitMode),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage imagePayload(
|
||||
BleProtocol.ImageTileOptions tile,
|
||||
int sessionId,
|
||||
byte[] payload,
|
||||
String label,
|
||||
boolean leftArm
|
||||
) {
|
||||
return imagePayload("sound", tile, sessionId, payload, label, leftArm);
|
||||
}
|
||||
|
||||
public OutboundMessage imagePayload(
|
||||
String kind,
|
||||
BleProtocol.ImageTileOptions tile,
|
||||
int sessionId,
|
||||
byte[] payload,
|
||||
String label,
|
||||
boolean leftArm
|
||||
) {
|
||||
// Keep these arguments for source compatibility with existing callers;
|
||||
// the private command no longer carries a tile, session, or image header.
|
||||
return customMessage(kind, payload, label, -1, leftArm);
|
||||
}
|
||||
|
||||
public OutboundMessage customMessage(String kind, byte[] payload, String label,
|
||||
int tileIndex, boolean leftArm) {
|
||||
if (payload == null || payload.length > CfwTransport.MAX_MESSAGE)
|
||||
throw new IllegalArgumentException("CFW message exceeds uint16 length");
|
||||
int streamId = magicPool.allocate();
|
||||
return new OutboundMessage(kind, label, CfwTransport.SID, 0, streamId, payload,
|
||||
CfwMessageWindow.ACK_TIMEOUT_MS, tileIndex, leftArm);
|
||||
}
|
||||
|
||||
public OutboundMessage cfwCleanup(
|
||||
BleProtocol.ImageTileOptions tile,
|
||||
int sessionId,
|
||||
boolean leftArm
|
||||
) {
|
||||
byte[] payload = new byte[] { (byte) BleProtocol.CFW_IMAGE_MODE_CLEANUP };
|
||||
return imagePayload(
|
||||
"cfw-cleanup",
|
||||
tile,
|
||||
sessionId,
|
||||
payload,
|
||||
"CFW cleanup",
|
||||
leftArm
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage enableOrDisableMic(boolean enable) {
|
||||
int magic = magicPool.allocate();
|
||||
return new OutboundMessage(
|
||||
"audio-control",
|
||||
enable ? "G2 mic enable" : "G2 mic disable",
|
||||
BleProtocol.SID_EVENHUB,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildAudioControl(magic, enable),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage enableOrDisableImu(boolean enable, int reportFrq) {
|
||||
int magic = magicPool.allocate();
|
||||
return new OutboundMessage(
|
||||
"imu-control",
|
||||
enable ? "IMU enable" : "IMU disable",
|
||||
BleProtocol.SID_EVENHUB,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildImuControl(magic, enable, reportFrq),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage setBrightness(boolean autoAdjust, int brightnessLevel) {
|
||||
int magic = magicPool.allocate();
|
||||
return new OutboundMessage(
|
||||
"brightness-control",
|
||||
autoAdjust ? "brightness auto" : "brightness level=" + brightnessLevel,
|
||||
BleProtocol.SID_UI_SETTING,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildSetBrightness(magic, autoAdjust, brightnessLevel),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage setWearDetection(boolean enabled) {
|
||||
int magic = magicPool.allocate();
|
||||
return new OutboundMessage(
|
||||
"wear-detection-control",
|
||||
"wear detection " + (enabled ? "enable" : "disable"),
|
||||
BleProtocol.SID_UI_SETTING,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildSetWearDetection(magic, enabled),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage createLayout() {
|
||||
int magic = magicPool.allocate();
|
||||
return new OutboundMessage(
|
||||
"create-layout",
|
||||
"create-layout",
|
||||
BleProtocol.SID_EVENHUB,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildCreateInputPage(magic),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage startupTextProbe() {
|
||||
int magic = magicPool.allocate();
|
||||
return new OutboundMessage(
|
||||
"startup-text-probe",
|
||||
"startup text probe",
|
||||
BleProtocol.SID_EVENHUB,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildDashboardTextUpgrade(magic),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage heartbeat() {
|
||||
int magic = magicPool.allocate();
|
||||
return new OutboundMessage(
|
||||
"heartbeat",
|
||||
"heartbeat",
|
||||
BleProtocol.SID_EVENHUB,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildHeartbeat(magic),
|
||||
HEARTBEAT_TIMEOUT_MS,
|
||||
-1,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage batteryQuery() {
|
||||
int magic = magicPool.allocate();
|
||||
return new OutboundMessage(
|
||||
"battery",
|
||||
"battery",
|
||||
BleProtocol.SID_UI_SETTING,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildSettingsQuery(magic),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage faceclawWakeControl(int operation, int nonce, boolean leftArm) {
|
||||
return new OutboundMessage(
|
||||
"wake-lease-control",
|
||||
"wake lease op=" + operation + " nonce=" + nonce + (leftArm ? " L" : " R"),
|
||||
BleProtocol.SID_UI_SETTING,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
0,
|
||||
BleProtocol.buildFaceclawWakeControl(operation, nonce),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
leftArm
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage faceclawFramebufferControl(int operation, boolean leftArm) {
|
||||
return new OutboundMessage(
|
||||
"framebuffer-lease-control",
|
||||
"framebuffer lease op=" + operation + (leftArm ? " L" : " R"),
|
||||
BleProtocol.SID_UI_SETTING,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
0,
|
||||
BleProtocol.buildFaceclawWakeControl(operation, 0),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
leftArm
|
||||
);
|
||||
}
|
||||
|
||||
/** CFW mic_control write (settings field 103) targeted at one temple. */
|
||||
public OutboundMessage faceclawMicControl(byte[] record, String label, boolean leftArm) {
|
||||
return new OutboundMessage(
|
||||
"mic-control",
|
||||
"mic control " + label + (leftArm ? " L" : " R"),
|
||||
BleProtocol.SID_UI_SETTING,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
0,
|
||||
BleProtocol.buildFaceclawMicControl(record),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
leftArm
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage faceclawWearQuery(boolean leftArm) {
|
||||
return new OutboundMessage(
|
||||
"wear-query-control",
|
||||
"wear state query" + (leftArm ? " L" : " R"),
|
||||
BleProtocol.SID_UI_SETTING,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
0,
|
||||
BleProtocol.buildFaceclawWakeControl(BleProtocol.FACECLAW_WEAR_OP_QUERY, 0),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
leftArm
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
import java.util.Arrays;
|
||||
|
||||
public final class OutboundMessage {
|
||||
final String kind;
|
||||
final String label;
|
||||
final int sid;
|
||||
final int flag;
|
||||
int magic;
|
||||
final byte[] message;
|
||||
final int ackTimeoutMs;
|
||||
final int tileIndex;
|
||||
final boolean isLeftArmMessage;
|
||||
|
||||
int cfwAckLenses, cfwRetries;
|
||||
boolean cfwRetryPending;
|
||||
final int cfwChecksum;
|
||||
|
||||
int imageUpdateId;
|
||||
int imageMessageNumber;
|
||||
int imageMessageCount;
|
||||
long sentAtMs;
|
||||
long writeStartedAtMs;
|
||||
long ackDeadlineAtMs;
|
||||
byte[] ackPayload = new byte[0];
|
||||
Runnable onSent;
|
||||
Runnable onAck;
|
||||
Runnable onTimeout;
|
||||
|
||||
OutboundMessage(String kind, String label, int sid, int flag, int magic, byte[] message, int ackTimeoutMs, int tileIndex, boolean isLeftArmMessage) {
|
||||
this.kind = kind;
|
||||
this.label = label;
|
||||
this.sid = sid;
|
||||
this.flag = flag;
|
||||
this.magic = magic;
|
||||
this.message = message == null ? new byte[0] : Arrays.copyOf(message, message.length);
|
||||
this.cfwChecksum = sid == CfwTransport.SID ? CfwTransport.crc(this.message) : 0;
|
||||
this.ackTimeoutMs = ackTimeoutMs;
|
||||
this.tileIndex = tileIndex;
|
||||
this.isLeftArmMessage = isLeftArmMessage;
|
||||
}
|
||||
|
||||
/** NACK names this attempt's stream/message sequence, even without a decoded size. */
|
||||
boolean acceptCfwAck(CfwTransport.Ack ack) {
|
||||
if (ack == null || sid != CfwTransport.SID || magic != ack.streamId
|
||||
|| ack.messageId != 0 || (ack.lens != 1 && ack.lens != 2)) return false;
|
||||
if (ack.nack) {
|
||||
cfwRetryPending = true;
|
||||
return false;
|
||||
}
|
||||
if (message.length != ack.size || cfwChecksum != ack.checksum) return false;
|
||||
cfwAckLenses |= ack.lens;
|
||||
return !cfwRetryPending && cfwAckLenses == CfwTransport.BOTH;
|
||||
}
|
||||
|
||||
/** Replace an attempt atomically under the communicator lock. */
|
||||
void prepareCfwReplay(int streamId) {
|
||||
magic = streamId;
|
||||
++cfwRetries;
|
||||
cfwAckLenses = 0;
|
||||
cfwRetryPending = false;
|
||||
ackDeadlineAtMs = 0;
|
||||
ackPayload = new byte[0];
|
||||
}
|
||||
|
||||
void setImageUpdatePosition(int updateId, int messageNumber, int messageCount) {
|
||||
this.imageUpdateId = updateId;
|
||||
this.imageMessageNumber = messageNumber;
|
||||
this.imageMessageCount = messageCount;
|
||||
}
|
||||
}
|
||||
-179
@@ -1,179 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Phone-side model of the CFW's flat 256 KiB texture cache (zlib_glue mode 18).
|
||||
* The firmware holds only bytes; every structural decision — where font
|
||||
* tables and glyph images live, what is resident, when to start over — is
|
||||
* made here. Layout per font: a 96-entry uint32 offset table (chars 32..127)
|
||||
* whose entries are written lazily as each glyph is uploaded; glyph images
|
||||
* are the GlyphAtlas cached bytes ([w][h][4bpp RLE]).
|
||||
*
|
||||
* Allocation is a bump pointer with reset-on-full: the realistic working set
|
||||
* (a few fonts' tables plus ~hundreds of glyphs at tens of bytes each) is far
|
||||
* below 256 KiB, so eviction sophistication buys nothing. After a reset the
|
||||
* firmware cache still holds stale bytes; that is safe because a draw only
|
||||
* ever references (font table entry, glyph image) pairs uploaded in the
|
||||
* current generation — stale table entries are never named in a mode-20
|
||||
* string.
|
||||
*
|
||||
* Upload entries accumulate as complete [offset u32][len u16][data] records
|
||||
* and drain into one or more standalone mode-18 payloads, each independently
|
||||
* valid, enqueued ahead of the image message that references them (the
|
||||
* transport is FIFO, so no ack round-trip is needed before use).
|
||||
*
|
||||
* Not thread-safe by itself: the communicator calls it under its own lock.
|
||||
*/
|
||||
public final class TextureCacheState {
|
||||
public static final int CACHE_SIZE = 262144;
|
||||
private static final int FONT_TABLE_BYTES = 96 * 4;
|
||||
|
||||
private int allocEnd = 0;
|
||||
private final Map<Integer, Integer> fontTableOffsets = new HashMap<>();
|
||||
private final Map<Long, Integer> glyphOffsets = new HashMap<>();
|
||||
private final Map<Integer, Integer> imageOffsets = new HashMap<>();
|
||||
private final List<byte[]> pendingEntries = new ArrayList<>();
|
||||
private int pendingBytes = 0;
|
||||
private int generation = 0;
|
||||
|
||||
/**
|
||||
* Forget everything: the on-glasses cache is gone (session teardown, lease
|
||||
* loss) or no longer trustworthy (upload timeout), or we ran out of space.
|
||||
* Glyphs and images re-upload lazily on next use.
|
||||
*/
|
||||
public void reset() {
|
||||
allocEnd = 0;
|
||||
fontTableOffsets.clear();
|
||||
glyphOffsets.clear();
|
||||
imageOffsets.clear();
|
||||
pendingEntries.clear();
|
||||
pendingBytes = 0;
|
||||
generation++;
|
||||
}
|
||||
|
||||
/** Bumped by every reset; lets a planner detect a mid-plan reset. */
|
||||
public int generation() {
|
||||
return generation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bytes currently allocated out of CACHE_SIZE, for logging/tuning (a
|
||||
* sudden drop in the frame log means a reset-on-full re-upload cycle —
|
||||
* the signal that the 256 KiB budget is being outgrown).
|
||||
*/
|
||||
public int usedBytes() {
|
||||
return allocEnd;
|
||||
}
|
||||
|
||||
/** The font's 96-entry table offset in the cache, or -1 when it exists but this glyph can't fit. */
|
||||
public int fontTableOffset(int fontId) {
|
||||
Integer existing = fontTableOffsets.get(fontId);
|
||||
return existing == null ? -1 : existing;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure a glyph image (and its font table slot) is resident, queuing the
|
||||
* mode-18 upload entries for anything newly placed. Returns the glyph's
|
||||
* image offset, or -1 when the cache is full (caller resets and retries)
|
||||
* or the encoding is outside the mode-20 table (32..127).
|
||||
*/
|
||||
public int ensureGlyph(int fontId, int encoding, GlyphAtlas.Glyph glyph) {
|
||||
if (glyph == null || encoding < 32 || encoding > 127) return -1;
|
||||
long key = ((long) fontId << 32) | (encoding & 0xffffffffL);
|
||||
Integer existing = glyphOffsets.get(key);
|
||||
if (existing != null) return existing;
|
||||
|
||||
Integer table = fontTableOffsets.get(fontId);
|
||||
int need = glyph.cachedBytes.length + (table == null ? FONT_TABLE_BYTES : 0);
|
||||
if (allocEnd + need > CACHE_SIZE) return -1;
|
||||
|
||||
if (table == null) {
|
||||
table = allocEnd;
|
||||
allocEnd += FONT_TABLE_BYTES;
|
||||
fontTableOffsets.put(fontId, table);
|
||||
// No upload for the table itself: entries are written per glyph,
|
||||
// and only uploaded entries are ever referenced by a draw.
|
||||
}
|
||||
int offset = allocEnd;
|
||||
allocEnd += glyph.cachedBytes.length;
|
||||
glyphOffsets.put(key, offset);
|
||||
|
||||
addEntry(offset, glyph.cachedBytes);
|
||||
addEntry(table + 4 * (encoding - 32),
|
||||
new byte[]{(byte) offset, (byte) (offset >> 8),
|
||||
(byte) (offset >> 16), (byte) (offset >> 24)});
|
||||
return offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure an ImageAtlas entry is resident (mode-19 draws reference it by
|
||||
* raw cache offset; no table involved). Returns the image offset, or -1
|
||||
* when the cache is full (caller resets and retries).
|
||||
*/
|
||||
public int ensureImage(int imageId, ImageAtlas.Entry image) {
|
||||
if (image == null) return -1;
|
||||
Integer existing = imageOffsets.get(imageId);
|
||||
if (existing != null) return existing;
|
||||
if (allocEnd + image.cachedBytes.length > CACHE_SIZE) return -1;
|
||||
int offset = allocEnd;
|
||||
allocEnd += image.cachedBytes.length;
|
||||
imageOffsets.put(imageId, offset);
|
||||
addEntry(offset, image.cachedBytes);
|
||||
return offset;
|
||||
}
|
||||
|
||||
private void addEntry(int offset, byte[] data) {
|
||||
byte[] entry = new byte[6 + data.length];
|
||||
entry[0] = (byte) (offset & 0xff);
|
||||
entry[1] = (byte) ((offset >> 8) & 0xff);
|
||||
entry[2] = (byte) ((offset >> 16) & 0xff);
|
||||
entry[3] = (byte) ((offset >> 24) & 0xff);
|
||||
entry[4] = (byte) (data.length & 0xff);
|
||||
entry[5] = (byte) ((data.length >> 8) & 0xff);
|
||||
System.arraycopy(data, 0, entry, 6, data.length);
|
||||
pendingEntries.add(entry);
|
||||
pendingBytes += entry.length;
|
||||
}
|
||||
|
||||
public boolean hasPendingUploads() {
|
||||
return !pendingEntries.isEmpty();
|
||||
}
|
||||
|
||||
/** Total queued upload bytes (entry framing included), for logging. */
|
||||
public int pendingUploadBytes() {
|
||||
return pendingBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drain the queued entries into complete mode-18 payloads ([18][entries]),
|
||||
* each at most maxPayloadBytes so a payload fits one BLE image message
|
||||
* without fragmentation. A single entry larger than the cap still gets its
|
||||
* own payload (the transport can fragment it).
|
||||
*/
|
||||
public List<byte[]> drainUploadPayloads(int maxPayloadBytes) {
|
||||
List<byte[]> payloads = new ArrayList<>();
|
||||
ByteArrayOutputStream current = null;
|
||||
for (byte[] entry : pendingEntries) {
|
||||
if (current != null && current.size() + entry.length > maxPayloadBytes) {
|
||||
payloads.add(current.toByteArray());
|
||||
current = null;
|
||||
}
|
||||
if (current == null) {
|
||||
current = new ByteArrayOutputStream(Math.min(maxPayloadBytes, 4 + entry.length));
|
||||
current.write(18);
|
||||
}
|
||||
current.write(entry, 0, entry.length);
|
||||
}
|
||||
if (current != null) {
|
||||
payloads.add(current.toByteArray());
|
||||
}
|
||||
pendingEntries.clear();
|
||||
pendingBytes = 0;
|
||||
return payloads;
|
||||
}
|
||||
}
|
||||
@@ -1,721 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Plans a screen update that ships text and icons as on-glasses cached draws
|
||||
* instead of pixels (CFW modes 18/19/20; see g2flash/patches/zlib_glue.c and
|
||||
* texture_cache.c).
|
||||
*
|
||||
* Approach: dirty rects come from comparing the fully composited old and new
|
||||
* frames, exactly as the plain incremental path does. For each deferred draw
|
||||
* (glyph or image) whose pixels intersect a sent rect, check that every pixel
|
||||
* the on-glasses draw would write lands correct — i.e. the new composite's
|
||||
* 4bpp value at each in-panel nonzero-source pixel equals what the draw
|
||||
* produces (this one check subsumes plane occlusion, surface
|
||||
* occlusion/clipping, and draw-on-draw overlap). A draw that passes and is
|
||||
* (or can be made) resident in the texture cache is replayed on-glasses: its
|
||||
* written pixels are punched to 0 in the delta rect content — which is what
|
||||
* makes text and icons nearly free to compress — and re-emitted as a mode-20
|
||||
* string or mode-19 image sub-message in the same atomic mode-8 batch, so
|
||||
* the shadow after apply equals the full composite exactly. Everything else
|
||||
* stays baked.
|
||||
*
|
||||
* Fallback shape: when nothing qualifies, plan() returns null and the caller
|
||||
* uses the plain single-bbox/multi-rect/full-frame paths unchanged.
|
||||
*/
|
||||
public final class TexturePlanner {
|
||||
private TexturePlanner() {}
|
||||
|
||||
/** Cap on mode-20 string sub-messages (mode-8 count is a u8). */
|
||||
private static final int MAX_GLYPH_RUNS = 180;
|
||||
/**
|
||||
* Soft cap on cached-glyph selection per update, before run grouping.
|
||||
* Sized above a full-screen 6x12 terminal repaint (~4,240 cells) so dense
|
||||
* screens draw everything; the run budget is the real bound.
|
||||
*/
|
||||
private static final int MAX_SELECTED_GLYPHS = 4600;
|
||||
/** Cap on mode-19 image sub-messages per update. */
|
||||
private static final int MAX_IMAGE_DRAWS = 60;
|
||||
/** Cap on mode-15 builtin-font string sub-messages per update. */
|
||||
private static final int MAX_FWTEXT_RUNS = 80;
|
||||
/** Mode-18 payload size that fits one BLE image message comfortably. */
|
||||
private static final int UPLOAD_PAYLOAD_MAX = 3600;
|
||||
/** Max x-adjust control bytes between two glyphs before starting a new run. */
|
||||
private static final int MAX_ADJUST_BYTES = 4;
|
||||
/** Options: identity LUT (top 15) + transparent, for mode-19 image draws. */
|
||||
private static final int IMAGE_DRAW_OPTIONS = 0x1f;
|
||||
|
||||
public static final class Result {
|
||||
/** Complete image payload: a mode-8 batch of rect deltas + cached draws. */
|
||||
public final byte[] payload;
|
||||
/** Mode-18 upload payloads to enqueue BEFORE the image message. */
|
||||
public final List<byte[]> uploads;
|
||||
/** Next mode-3 frame id (deltas consumed some). */
|
||||
public final int nextFid;
|
||||
public final int rectCount;
|
||||
public final int drawnGlyphs;
|
||||
public final int runCount;
|
||||
public final int drawnImages;
|
||||
public final int fwGlyphs;
|
||||
public final int fwRuns;
|
||||
public final int bakedCandidates;
|
||||
public final int uploadBytes;
|
||||
public final boolean fullFrame;
|
||||
/**
|
||||
* Where the planning time went, in ms. This runs on the send thread,
|
||||
* directly in the input-to-display path, so the split is reported in
|
||||
* the frame log: rects = finding the changed region, match = checking
|
||||
* each draw against the composite, cache = residency/uploads plus run
|
||||
* building, punch = clearing replayed draws out of a copy of the
|
||||
* frame, encode = compressing the leftover pixels.
|
||||
*/
|
||||
public int rectsMs;
|
||||
public int matchMs;
|
||||
public int cacheMs;
|
||||
public int punchMs;
|
||||
public int encodeMs;
|
||||
|
||||
Result(byte[] payload, List<byte[]> uploads, int nextFid, int rectCount,
|
||||
int drawnGlyphs, int runCount, int drawnImages, int fwGlyphs, int fwRuns,
|
||||
int bakedCandidates, int uploadBytes, boolean fullFrame) {
|
||||
this.payload = payload;
|
||||
this.uploads = uploads;
|
||||
this.nextFid = nextFid;
|
||||
this.rectCount = rectCount;
|
||||
this.drawnGlyphs = drawnGlyphs;
|
||||
this.runCount = runCount;
|
||||
this.drawnImages = drawnImages;
|
||||
this.fwGlyphs = fwGlyphs;
|
||||
this.fwRuns = fwRuns;
|
||||
this.bakedCandidates = bakedCandidates;
|
||||
this.uploadBytes = uploadBytes;
|
||||
this.fullFrame = fullFrame;
|
||||
}
|
||||
}
|
||||
|
||||
/** One draw selected for on-glasses replay. */
|
||||
private static final class Selected {
|
||||
final SurfaceCompositor.ScreenDraw draw;
|
||||
final GlyphAtlas.Glyph glyphAtlas; // glyph draws
|
||||
final ImageAtlas.Entry imageAtlas; // image draws
|
||||
final int gx; // screen x of the cached image (pen + bearing / blit left)
|
||||
final int top; // glyph draws: 4bpp top color
|
||||
|
||||
Selected(SurfaceCompositor.ScreenDraw draw, GlyphAtlas.Glyph glyphAtlas,
|
||||
ImageAtlas.Entry imageAtlas, int gx, int top) {
|
||||
this.draw = draw;
|
||||
this.glyphAtlas = glyphAtlas;
|
||||
this.imageAtlas = imageAtlas;
|
||||
this.gx = gx;
|
||||
this.top = top;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Plan a cached-draw update. previous is the delta base (the frame the
|
||||
* shadow currently holds), or null/mismatched for a full-frame keyframe.
|
||||
* Returns null when the plain paths should run instead (no replayable
|
||||
* draws, or identical frames).
|
||||
*/
|
||||
public static Result plan(
|
||||
byte[] previous, byte[] next, int width, int height,
|
||||
SurfaceCompositor.ScreenDraw[] draws,
|
||||
TextureCacheState cache, int fidStart,
|
||||
boolean allowMultiRect, int maxRects) {
|
||||
if (next == null || draws == null || draws.length == 0 || width <= 0 || height <= 0) {
|
||||
return null;
|
||||
}
|
||||
int stride = (width + 1) >> 1;
|
||||
if (next.length != stride * height) {
|
||||
return null;
|
||||
}
|
||||
long planStartedAtMs = android.os.SystemClock.elapsedRealtime();
|
||||
|
||||
// Rect geometry, matching what the plain paths would send.
|
||||
boolean fullFrame = previous == null || previous.length != next.length;
|
||||
List<int[]> rects;
|
||||
if (!fullFrame) {
|
||||
int[] box = BleImageOptimizer.computeChangedBox(previous, next, width, height);
|
||||
if (box == null) {
|
||||
return null; // identical frames; caller's dedup normally catches this
|
||||
}
|
||||
if (box[2] >= width && box[3] >= height) {
|
||||
fullFrame = true;
|
||||
rects = Collections.singletonList(new int[]{0, 0, width, height});
|
||||
} else {
|
||||
List<int[]> split = allowMultiRect
|
||||
? BleImageOptimizer.computeChangedRects(previous, next, width, height, maxRects)
|
||||
: null;
|
||||
rects = split != null ? split : Collections.singletonList(box);
|
||||
}
|
||||
} else {
|
||||
rects = Collections.singletonList(new int[]{0, 0, width, height});
|
||||
}
|
||||
|
||||
long matchStartedAtMs = android.os.SystemClock.elapsedRealtime();
|
||||
|
||||
// Candidates: draws whose written pixels intersect a sent rect, are
|
||||
// representable on the wire, and land correct against the composite.
|
||||
List<Selected> selected = new ArrayList<>();
|
||||
int bakedCandidates = 0;
|
||||
int selectedImages = 0;
|
||||
// Builtin-font (mode 15) runs: no cache residency, no uploads — the
|
||||
// firmware owns the font. Processed first because it has no cache
|
||||
// side effects; the counters feed the shared stats.
|
||||
List<byte[]> fwSubs = new ArrayList<>();
|
||||
List<FwPunch> fwPunches = new ArrayList<>();
|
||||
int fwGlyphCount = 0;
|
||||
int fwBaked = 0;
|
||||
for (SurfaceCompositor.ScreenDraw draw : draws) {
|
||||
if (draw.kind != SurfaceCompositor.ScreenDraw.KIND_FWTEXT) continue;
|
||||
fwBaked += planFwRun(draw, rects, next, stride, width, height, fwSubs, fwPunches);
|
||||
}
|
||||
fwGlyphCount = fwPunches.size();
|
||||
bakedCandidates += fwBaked;
|
||||
for (SurfaceCompositor.ScreenDraw draw : draws) {
|
||||
if (draw.kind == SurfaceCompositor.ScreenDraw.KIND_GLYPH) {
|
||||
GlyphAtlas.Glyph atlas = GlyphAtlas.get(draw.fontId, draw.encoding);
|
||||
if (atlas == null) continue; // unregistered: stays baked, not a "candidate"
|
||||
int gx = draw.x + atlas.bbxX;
|
||||
int inkTop = draw.y + atlas.inkTop;
|
||||
if (!intersectsAny(rects, gx, inkTop, atlas.width, atlas.inkHeight)) {
|
||||
continue;
|
||||
}
|
||||
if (draw.encoding < 32 || draw.encoding > 127
|
||||
|| gx < 0 || draw.y < 0 || gx > 0xffff || draw.y > 0xffff) {
|
||||
bakedCandidates++;
|
||||
continue;
|
||||
}
|
||||
int top = BmpUtil.nibbleForGray(draw.value);
|
||||
if (!glyphMatchesComposite(next, stride, width, height, atlas, gx, draw.y, top)) {
|
||||
bakedCandidates++;
|
||||
continue;
|
||||
}
|
||||
if (selected.size() >= MAX_SELECTED_GLYPHS) {
|
||||
bakedCandidates++;
|
||||
continue;
|
||||
}
|
||||
selected.add(new Selected(draw, atlas, null, gx, top));
|
||||
} else {
|
||||
ImageAtlas.Entry atlas = ImageAtlas.get(draw.imageId);
|
||||
if (atlas == null) continue;
|
||||
if (!intersectsAny(rects, draw.x, draw.y, atlas.width, atlas.height)) {
|
||||
continue;
|
||||
}
|
||||
if (draw.x < 0 || draw.y < 0 || draw.x > 0xffff || draw.y > 0xffff
|
||||
|| selectedImages >= MAX_IMAGE_DRAWS) {
|
||||
bakedCandidates++;
|
||||
continue;
|
||||
}
|
||||
if (!imageMatchesComposite(next, stride, width, height, atlas, draw.x, draw.y)) {
|
||||
bakedCandidates++;
|
||||
continue;
|
||||
}
|
||||
selected.add(new Selected(draw, null, atlas, draw.x, 0));
|
||||
selectedImages++;
|
||||
}
|
||||
}
|
||||
if (selected.isEmpty() && fwSubs.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
long cacheStartedAtMs = android.os.SystemClock.elapsedRealtime();
|
||||
|
||||
// Residency: ensure every selected draw is in the cache, retrying once
|
||||
// after a reset when the bump allocator fills. A draw that still fails
|
||||
// falls back to baked. IMPORTANT: once ensure* has run, every bail-out
|
||||
// to the plain path must cache.reset() — ensure* marked entries
|
||||
// resident and queued uploads that only a returned Result would
|
||||
// enqueue. (Fw-text needs no residency, so a fw-only plan skips this.)
|
||||
List<Selected> drawable = selected.isEmpty()
|
||||
? Collections.<Selected>emptyList()
|
||||
: ensureResident(cache, selected);
|
||||
bakedCandidates += selected.size() - drawable.size();
|
||||
|
||||
// Group glyphs into mode-20 runs; leftover-budget glyphs bake.
|
||||
List<Selected> glyphDrawable = new ArrayList<>();
|
||||
List<Selected> imageDrawable = new ArrayList<>();
|
||||
for (Selected sel : drawable) {
|
||||
if (sel.glyphAtlas != null) glyphDrawable.add(sel);
|
||||
else imageDrawable.add(sel);
|
||||
}
|
||||
List<byte[]> runs = new ArrayList<>();
|
||||
List<Selected> drawnGlyphs = buildRuns(cache, glyphDrawable, runs);
|
||||
bakedCandidates += glyphDrawable.size() - drawnGlyphs.size();
|
||||
List<Selected> drawn = new ArrayList<>(drawnGlyphs);
|
||||
drawn.addAll(imageDrawable);
|
||||
if ((drawn.isEmpty() && fwSubs.isEmpty())
|
||||
|| rects.size() + runs.size() + imageDrawable.size() + fwSubs.size() > 255) {
|
||||
if (!selected.isEmpty()) {
|
||||
cache.reset();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Punch the drawn pixels out of a copy of the composite, then encode
|
||||
// the rect deltas from the punched content.
|
||||
long punchStartedAtMs = android.os.SystemClock.elapsedRealtime();
|
||||
byte[] punched = next.clone();
|
||||
for (Selected sel : drawn) {
|
||||
punch(punched, stride, width, height, sel);
|
||||
}
|
||||
for (FwPunch fw : fwPunches) {
|
||||
punchFw(punched, stride, width, height, fw);
|
||||
}
|
||||
|
||||
long encodeStartedAtMs = android.os.SystemClock.elapsedRealtime();
|
||||
List<byte[]> subs = new ArrayList<>();
|
||||
int fid = fidStart;
|
||||
if (fullFrame) {
|
||||
subs.add(BleImageOptimizer.maybeCompress(punched, width, height));
|
||||
} else {
|
||||
for (int[] r : rects) {
|
||||
subs.add(BleImageOptimizer.encodeMode3Rect(punched, stride, r[0], r[1], r[2], r[3], fid));
|
||||
fid = (fid >= 0xfffe) ? 1 : fid + 1;
|
||||
}
|
||||
}
|
||||
for (Selected sel : imageDrawable) {
|
||||
subs.add(encodeImageDraw(cache, sel));
|
||||
}
|
||||
subs.addAll(runs);
|
||||
subs.addAll(fwSubs);
|
||||
byte[] payload = assembleMode8(subs);
|
||||
|
||||
int uploadBytes = cache.pendingUploadBytes();
|
||||
List<byte[]> uploads = cache.drainUploadPayloads(UPLOAD_PAYLOAD_MAX);
|
||||
Result result = new Result(payload, uploads, fid, rects.size(), drawnGlyphs.size(), runs.size(),
|
||||
imageDrawable.size(), fwGlyphCount, fwSubs.size(),
|
||||
bakedCandidates, uploadBytes, fullFrame);
|
||||
long doneAtMs = android.os.SystemClock.elapsedRealtime();
|
||||
result.rectsMs = (int) (matchStartedAtMs - planStartedAtMs);
|
||||
result.matchMs = (int) (cacheStartedAtMs - matchStartedAtMs);
|
||||
result.cacheMs = (int) (punchStartedAtMs - cacheStartedAtMs);
|
||||
result.punchMs = (int) (encodeStartedAtMs - punchStartedAtMs);
|
||||
result.encodeMs = (int) (doneAtMs - encodeStartedAtMs);
|
||||
return result;
|
||||
}
|
||||
|
||||
/** One punch task for an emitted firmware-font glyph. */
|
||||
private static final class FwPunch {
|
||||
final FwGlyphAtlas.Entry entry;
|
||||
final int gx;
|
||||
final int gy;
|
||||
|
||||
FwPunch(FwGlyphAtlas.Entry entry, int gx, int gy) {
|
||||
this.entry = entry;
|
||||
this.gx = gx;
|
||||
this.gy = gy;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Plan one firmware-font run (mode 15): classify every member, and when
|
||||
* any correct ink member intersects a sent rect, emit sub-messages over
|
||||
* the maximal correct stretches. A stretch never spans an incorrect
|
||||
* member — the sub-message boundary restarts the firmware's advance walk
|
||||
* at an explicit pen x, so occluded or unknown glyphs simply stay baked
|
||||
* without disturbing their neighbors' positions. Emitted ink members are
|
||||
* queued for punching. Returns how many ink members stay baked.
|
||||
*/
|
||||
private static int planFwRun(
|
||||
SurfaceCompositor.ScreenDraw draw, List<int[]> rects,
|
||||
byte[] next, int stride, int width, int height,
|
||||
List<byte[]> outSubs, List<FwPunch> outPunches) {
|
||||
int n = draw.fwCps.length;
|
||||
if (n == 0 || draw.y < 0 || draw.y > 0xffff) {
|
||||
return countInk(draw);
|
||||
}
|
||||
int top = BmpUtil.nibbleForGray(draw.value);
|
||||
boolean[] ok = new boolean[n];
|
||||
FwGlyphAtlas.Entry[] entries = new FwGlyphAtlas.Entry[n];
|
||||
boolean relevant = false;
|
||||
int baked = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (!draw.fwInk[i]) {
|
||||
ok[i] = true; // inkless connector (space): draws nothing
|
||||
continue;
|
||||
}
|
||||
FwGlyphAtlas.Entry entry = FwGlyphAtlas.get(draw.fwCps[i]);
|
||||
if (entry == null) {
|
||||
baked++;
|
||||
continue;
|
||||
}
|
||||
int gx = draw.x + draw.fwDx[i] + entry.ofsX;
|
||||
int gy = draw.y + entry.inkTop;
|
||||
if (!fwGlyphMatchesComposite(next, stride, width, height, entry, gx, gy, top)) {
|
||||
baked++;
|
||||
continue;
|
||||
}
|
||||
ok[i] = true;
|
||||
entries[i] = entry;
|
||||
if (intersectsAny(rects, gx, gy, entry.boxW, entry.boxH)) {
|
||||
relevant = true;
|
||||
}
|
||||
}
|
||||
if (!relevant) {
|
||||
return 0; // nothing in a sent rect: leave the run's pixels alone
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
while (i < n && outSubs.size() < MAX_FWTEXT_RUNS) {
|
||||
while (i < n && !(ok[i] && draw.fwInk[i])) i++;
|
||||
if (i >= n) break;
|
||||
int startX = draw.x + draw.fwDx[i];
|
||||
if (startX < 0 || startX > 0xffff) {
|
||||
baked++;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
// Extend across correct members while the UTF-8 payload fits,
|
||||
// tracking the last ink member so trailing connectors trim away.
|
||||
int j = i;
|
||||
int lastInk = -1;
|
||||
int bytesToLastInk = 0;
|
||||
int bytesSoFar = 0;
|
||||
while (j < n && ok[j]) {
|
||||
int encodedLength = utf8Length(draw.fwCps[j]);
|
||||
if (bytesSoFar + encodedLength > 255) break;
|
||||
bytesSoFar += encodedLength;
|
||||
if (draw.fwInk[j]) {
|
||||
lastInk = j;
|
||||
bytesToLastInk = bytesSoFar;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
if (lastInk < i) { // a single over-long codepoint; not realistic
|
||||
baked++;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
byte[] sub = new byte[7 + bytesToLastInk];
|
||||
sub[0] = 15;
|
||||
sub[1] = (byte) (startX & 0xff);
|
||||
sub[2] = (byte) ((startX >> 8) & 0xff);
|
||||
sub[3] = (byte) (draw.y & 0xff);
|
||||
sub[4] = (byte) ((draw.y >> 8) & 0xff);
|
||||
sub[5] = (byte) (top | 0x10); // top color + transparent
|
||||
sub[6] = (byte) bytesToLastInk;
|
||||
int pos = 7;
|
||||
for (int k = i; k <= lastInk; k++) {
|
||||
byte[] encoded = new StringBuilder().appendCodePoint(draw.fwCps[k])
|
||||
.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8);
|
||||
System.arraycopy(encoded, 0, sub, pos, encoded.length);
|
||||
pos += encoded.length;
|
||||
}
|
||||
outSubs.add(sub);
|
||||
for (int k = i; k <= lastInk; k++) {
|
||||
if (draw.fwInk[k]) {
|
||||
outPunches.add(new FwPunch(entries[k],
|
||||
draw.x + draw.fwDx[k] + entries[k].ofsX,
|
||||
draw.y + entries[k].inkTop));
|
||||
}
|
||||
}
|
||||
i = lastInk + 1;
|
||||
}
|
||||
return baked;
|
||||
}
|
||||
|
||||
private static int utf8Length(int cp) {
|
||||
if (cp < 0x80) return 1;
|
||||
if (cp < 0x800) return 2;
|
||||
if (cp < 0x10000) return 3;
|
||||
return 4;
|
||||
}
|
||||
|
||||
private static int countInk(SurfaceCompositor.ScreenDraw draw) {
|
||||
int count = 0;
|
||||
for (boolean ink : draw.fwInk) {
|
||||
if (ink) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether every in-panel nonzero pixel of the builtin-font glyph lands
|
||||
* correct: the composite's 4bpp value must equal the firmware's LUT
|
||||
* output src*top/15 (integer division — a general LUT check, since these
|
||||
* glyphs are anti-aliased).
|
||||
*/
|
||||
private static boolean fwGlyphMatchesComposite(byte[] packed, int stride, int width, int height,
|
||||
FwGlyphAtlas.Entry entry, int gx, int gy, int top) {
|
||||
for (int row = 0; row < entry.boxH; row++) {
|
||||
int y = gy + row;
|
||||
if (y < 0 || y >= height) continue;
|
||||
for (int col = 0; col < entry.boxW; col++) {
|
||||
int source = entry.nibbleAt(col, row);
|
||||
if (source == 0) continue;
|
||||
int x = gx + col;
|
||||
if (x < 0 || x >= width) continue;
|
||||
if (nibbleAt(packed, stride, x, y) != (source * top) / 15) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void punchFw(byte[] packed, int stride, int width, int height, FwPunch fw) {
|
||||
for (int row = 0; row < fw.entry.boxH; row++) {
|
||||
int y = fw.gy + row;
|
||||
if (y < 0 || y >= height) continue;
|
||||
for (int col = 0; col < fw.entry.boxW; col++) {
|
||||
if (fw.entry.nibbleAt(col, row) == 0) continue;
|
||||
punchPixel(packed, stride, width, fw.gx + col, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean intersectsAny(List<int[]> rects, int x, int y, int w, int h) {
|
||||
if (w <= 0 || h <= 0) return false;
|
||||
for (int[] r : rects) {
|
||||
if (x < r[0] + r[2] && x + w > r[0] && y < r[1] + r[3] && y + h > r[1]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether every in-panel ink pixel of the glyph lands correct in the
|
||||
* packed new frame: the composite's 4bpp value must equal the firmware's
|
||||
* LUT output source*top/15 (integer). For 1bpp glyphs (source 15) this is
|
||||
* exactly `== top`; AA glyphs get the general check. Ink outside the
|
||||
* panel is clipped by the firmware and irrelevant.
|
||||
*/
|
||||
private static boolean glyphMatchesComposite(byte[] packed, int stride, int width, int height,
|
||||
GlyphAtlas.Glyph atlas, int gx, int lineY, int top) {
|
||||
for (int row = atlas.inkTop; row < atlas.inkTop + atlas.inkHeight; row++) {
|
||||
int y = lineY + row;
|
||||
if (y < 0 || y >= height) continue;
|
||||
for (int col = 0; col < atlas.width; col++) {
|
||||
int source = atlas.nibbleAt(col, row);
|
||||
if (source == 0) continue;
|
||||
int x = gx + col;
|
||||
if (x < 0 || x >= width) continue;
|
||||
if (nibbleAt(packed, stride, x, y) != (source * top) / 15) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether every in-panel nonzero pixel of the cached image equals the
|
||||
* packed new frame — the mode-19 transparent draw writes exactly those.
|
||||
*/
|
||||
private static boolean imageMatchesComposite(byte[] packed, int stride, int width, int height,
|
||||
ImageAtlas.Entry atlas, int left, int top) {
|
||||
for (int row = 0; row < atlas.height; row++) {
|
||||
int y = top + row;
|
||||
if (y < 0 || y >= height) continue;
|
||||
for (int col = 0; col < atlas.width; col++) {
|
||||
int source = atlas.nibbleAt(col, row);
|
||||
if (source == 0) continue;
|
||||
int x = left + col;
|
||||
if (x < 0 || x >= width) continue;
|
||||
if (nibbleAt(packed, stride, x, y) != source) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static int nibbleAt(byte[] packed, int stride, int x, int y) {
|
||||
int b = packed[y * stride + (x >> 1)] & 0xff;
|
||||
return (x & 1) != 0 ? (b & 0x0f) : (b >> 4);
|
||||
}
|
||||
|
||||
private static void punch(byte[] packed, int stride, int width, int height, Selected sel) {
|
||||
if (sel.glyphAtlas != null) {
|
||||
GlyphAtlas.Glyph atlas = sel.glyphAtlas;
|
||||
for (int row = atlas.inkTop; row < atlas.inkTop + atlas.inkHeight; row++) {
|
||||
int y = sel.draw.y + row;
|
||||
if (y < 0 || y >= height) continue;
|
||||
for (int col = 0; col < atlas.width; col++) {
|
||||
if (!atlas.inkAt(col, row)) continue;
|
||||
punchPixel(packed, stride, width, sel.gx + col, y);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ImageAtlas.Entry atlas = sel.imageAtlas;
|
||||
for (int row = 0; row < atlas.height; row++) {
|
||||
int y = sel.draw.y + row;
|
||||
if (y < 0 || y >= height) continue;
|
||||
for (int col = 0; col < atlas.width; col++) {
|
||||
if (atlas.nibbleAt(col, row) == 0) continue;
|
||||
punchPixel(packed, stride, width, sel.gx + col, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void punchPixel(byte[] packed, int stride, int width, int x, int y) {
|
||||
if (x < 0 || x >= width) return;
|
||||
int index = y * stride + (x >> 1);
|
||||
if ((x & 1) != 0) {
|
||||
packed[index] = (byte) (packed[index] & 0xf0);
|
||||
} else {
|
||||
packed[index] = (byte) (packed[index] & 0x0f);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure residency for all of `selected`, resetting the cache once if the
|
||||
* allocator fills mid-plan (a reset invalidates offsets handed out earlier
|
||||
* in the same pass, so the whole pass reruns). Returns the draws that are
|
||||
* resident afterwards.
|
||||
*/
|
||||
private static List<Selected> ensureResident(TextureCacheState cache, List<Selected> selected) {
|
||||
for (int attempt = 0; attempt < 2; attempt++) {
|
||||
List<Selected> resident = new ArrayList<>(selected.size());
|
||||
boolean full = false;
|
||||
for (Selected sel : selected) {
|
||||
int offset = sel.glyphAtlas != null
|
||||
? cache.ensureGlyph(sel.draw.fontId, sel.draw.encoding, sel.glyphAtlas)
|
||||
: cache.ensureImage(sel.draw.imageId, sel.imageAtlas);
|
||||
if (offset >= 0) {
|
||||
resident.add(sel);
|
||||
} else {
|
||||
full = true;
|
||||
}
|
||||
}
|
||||
if (!full || attempt == 1) {
|
||||
return resident;
|
||||
}
|
||||
cache.reset();
|
||||
}
|
||||
return Collections.emptyList(); // unreachable
|
||||
}
|
||||
|
||||
/** Mode-19 cached-image draw: [19][offset u32][x u16][y u16][options u8]. */
|
||||
private static byte[] encodeImageDraw(TextureCacheState cache, Selected sel) {
|
||||
int offset = cache.ensureImage(sel.draw.imageId, sel.imageAtlas); // resident: returns the offset
|
||||
byte[] sub = new byte[10];
|
||||
sub[0] = 19;
|
||||
sub[1] = (byte) (offset & 0xff);
|
||||
sub[2] = (byte) ((offset >> 8) & 0xff);
|
||||
sub[3] = (byte) ((offset >> 16) & 0xff);
|
||||
sub[4] = (byte) ((offset >> 24) & 0xff);
|
||||
sub[5] = (byte) (sel.draw.x & 0xff);
|
||||
sub[6] = (byte) ((sel.draw.x >> 8) & 0xff);
|
||||
sub[7] = (byte) (sel.draw.y & 0xff);
|
||||
sub[8] = (byte) ((sel.draw.y >> 8) & 0xff);
|
||||
sub[9] = (byte) IMAGE_DRAW_OPTIONS;
|
||||
return sub;
|
||||
}
|
||||
|
||||
/**
|
||||
* Group selected glyphs into mode-20 string sub-messages:
|
||||
* [20][fontTable u32][x u16][y u16][options u8][strlen u8][string]
|
||||
* One run per (font, line y, top color) span; within a run, control bytes
|
||||
* 1..31 adjust x by -10..+20 to hit each glyph's exact position, and each
|
||||
* glyph advances x by its cached width. Order across runs is free: every
|
||||
* drawn glyph's ink equals the composite, so overlaps write equal values.
|
||||
* Returns the glyphs actually emitted (run budget can drop stragglers).
|
||||
*/
|
||||
private static List<Selected> buildRuns(TextureCacheState cache, List<Selected> drawable, List<byte[]> outRuns) {
|
||||
List<Selected> sorted = new ArrayList<>(drawable);
|
||||
sorted.sort(Comparator
|
||||
.comparingInt((Selected s) -> s.draw.fontId)
|
||||
.thenComparingInt(s -> s.draw.y)
|
||||
.thenComparingInt(s -> s.top)
|
||||
.thenComparingInt(s -> s.gx));
|
||||
|
||||
List<Selected> drawn = new ArrayList<>(sorted.size());
|
||||
int i = 0;
|
||||
while (i < sorted.size() && outRuns.size() < MAX_GLYPH_RUNS) {
|
||||
Selected first = sorted.get(i);
|
||||
int fontTable = cache.fontTableOffset(first.draw.fontId);
|
||||
if (fontTable < 0) { // shouldn't happen: residency ensured above
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
java.io.ByteArrayOutputStream string = new java.io.ByteArrayOutputStream();
|
||||
List<Selected> runGlyphs = new ArrayList<>();
|
||||
int cursor = first.gx; // the run's wire x starts exactly at the first glyph
|
||||
int j = i;
|
||||
while (j < sorted.size()) {
|
||||
Selected sel = sorted.get(j);
|
||||
if (sel.draw.fontId != first.draw.fontId
|
||||
|| sel.draw.y != first.draw.y
|
||||
|| sel.top != first.top) {
|
||||
break;
|
||||
}
|
||||
int delta = sel.gx - cursor;
|
||||
int adjustBytes = adjustByteCount(delta);
|
||||
if (adjustBytes > MAX_ADJUST_BYTES && j > i) {
|
||||
break; // far gap: cheaper to start a fresh run
|
||||
}
|
||||
if (string.size() + adjustBytes + 1 > 255) {
|
||||
break; // strlen is a u8
|
||||
}
|
||||
emitAdjust(string, delta);
|
||||
string.write(sel.draw.encoding);
|
||||
runGlyphs.add(sel);
|
||||
cursor = sel.gx + sel.glyphAtlas.width;
|
||||
j++;
|
||||
}
|
||||
if (!runGlyphs.isEmpty()) {
|
||||
byte[] stringBytes = string.toByteArray();
|
||||
byte[] run = new byte[11 + stringBytes.length];
|
||||
run[0] = 20;
|
||||
run[1] = (byte) (fontTable & 0xff);
|
||||
run[2] = (byte) ((fontTable >> 8) & 0xff);
|
||||
run[3] = (byte) ((fontTable >> 16) & 0xff);
|
||||
run[4] = (byte) ((fontTable >> 24) & 0xff);
|
||||
run[5] = (byte) (first.gx & 0xff);
|
||||
run[6] = (byte) ((first.gx >> 8) & 0xff);
|
||||
run[7] = (byte) (first.draw.y & 0xff);
|
||||
run[8] = (byte) ((first.draw.y >> 8) & 0xff);
|
||||
run[9] = (byte) (first.top | 0x10); // top color + transparent
|
||||
run[10] = (byte) stringBytes.length;
|
||||
System.arraycopy(stringBytes, 0, run, 11, stringBytes.length);
|
||||
outRuns.add(run);
|
||||
drawn.addAll(runGlyphs);
|
||||
i = j;
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return drawn;
|
||||
}
|
||||
|
||||
/** How many 1..31 control bytes are needed to move the cursor by delta. */
|
||||
private static int adjustByteCount(int delta) {
|
||||
if (delta == 0) return 0;
|
||||
return delta > 0 ? (delta + 19) / 20 : (-delta + 9) / 10;
|
||||
}
|
||||
|
||||
private static void emitAdjust(java.io.ByteArrayOutputStream out, int delta) {
|
||||
while (delta != 0) {
|
||||
int step = delta > 0 ? Math.min(delta, 20) : Math.max(delta, -10);
|
||||
out.write(step + 11); // byte 1..31 adjusts x by (byte - 11)
|
||||
delta -= step;
|
||||
}
|
||||
}
|
||||
|
||||
/** [8][count]([len u16][sub])* — one atomic present for deltas + draws. */
|
||||
private static byte[] assembleMode8(List<byte[]> subs) {
|
||||
int total = 2;
|
||||
for (byte[] sub : subs) {
|
||||
total += 2 + sub.length;
|
||||
}
|
||||
byte[] out = new byte[total];
|
||||
out[0] = 8;
|
||||
out[1] = (byte) subs.size();
|
||||
int pos = 2;
|
||||
for (byte[] sub : subs) {
|
||||
out[pos] = (byte) (sub.length & 0xff);
|
||||
out[pos + 1] = (byte) ((sub.length >> 8) & 0xff);
|
||||
pos += 2;
|
||||
System.arraycopy(sub, 0, out, pos, sub.length);
|
||||
pos += sub.length;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BmpUtil {
|
||||
public static byte[] copyTileBmp(byte[] bmp) {
|
||||
if (bmp == null || bmp.length == 0) {
|
||||
return new byte[0];
|
||||
}
|
||||
return Arrays.copyOf(bmp, bmp.length);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The 4bpp level an 8-bit gray value packs to. Public so the texture-cache
|
||||
* planner computes a glyph draw's top color with the exact quantization
|
||||
* the composited frame was packed with.
|
||||
*/
|
||||
public static int nibbleForGray(int gray) {
|
||||
return GRAY_TO_NIBBLE[gray & 0xff] & 0xff;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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[] pack4bppFromGray8(byte[] gray8, int width, int height) {
|
||||
if (width <= 0 || height <= 0) {
|
||||
return new byte[0];
|
||||
}
|
||||
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
|
||||
* fallback used when mode-6 compression does not shrink the frame still
|
||||
* speaks BMP on the wire; 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;
|
||||
int dibHeaderSize = 40;
|
||||
int paletteSize = 16 * 4;
|
||||
int pixelOffset = fileHeaderSize + dibHeaderSize + paletteSize;
|
||||
int fileSize = pixelOffset + pixelDataSize;
|
||||
|
||||
byte[] buf = new byte[fileSize];
|
||||
buf[0] = 0x42; // 'B'
|
||||
buf[1] = 0x4d; // 'M'
|
||||
putUint32Le(buf, 2, fileSize);
|
||||
putUint32Le(buf, 10, pixelOffset);
|
||||
putUint32Le(buf, 14, dibHeaderSize);
|
||||
putInt32Le(buf, 18, width);
|
||||
putInt32Le(buf, 22, height);
|
||||
putUint16Le(buf, 26, 1); // planes
|
||||
putUint16Le(buf, 28, 4); // bits per pixel
|
||||
putUint32Le(buf, 30, 0); // compression (BI_RGB)
|
||||
putUint32Le(buf, 34, pixelDataSize);
|
||||
putUint32Le(buf, 46, 16); // palette entry count
|
||||
|
||||
for (int i = 0; i < 16; i++) {
|
||||
int v = i * 17;
|
||||
int base = fileHeaderSize + dibHeaderSize + i * 4;
|
||||
buf[base] = (byte) v;
|
||||
buf[base + 1] = (byte) v;
|
||||
buf[base + 2] = (byte) v;
|
||||
buf[base + 3] = 0;
|
||||
}
|
||||
|
||||
for (int bmpRow = 0; bmpRow < height; bmpRow++) {
|
||||
int srcY = height - 1 - bmpRow; // BMP rows run bottom-up
|
||||
System.arraycopy(packed, srcY * packedStride,
|
||||
buf, pixelOffset + bmpRow * rowStride, packedStride);
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
private static void putUint16Le(byte[] buf, int offset, int value) {
|
||||
buf[offset] = (byte) (value & 0xff);
|
||||
buf[offset + 1] = (byte) ((value >>> 8) & 0xff);
|
||||
}
|
||||
|
||||
private static void putUint32Le(byte[] buf, int offset, int value) {
|
||||
buf[offset] = (byte) (value & 0xff);
|
||||
buf[offset + 1] = (byte) ((value >>> 8) & 0xff);
|
||||
buf[offset + 2] = (byte) ((value >>> 16) & 0xff);
|
||||
buf[offset + 3] = (byte) ((value >>> 24) & 0xff);
|
||||
}
|
||||
|
||||
private static void putInt32Le(byte[] buf, int offset, int value) {
|
||||
putUint32Le(buf, offset, value);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CollectionUtils {
|
||||
public static List<byte[]> singletonList(byte[] value) {
|
||||
List<byte[]> list = new ArrayList<>(1);
|
||||
list.add(value);
|
||||
return list;
|
||||
}
|
||||
|
||||
public static List<byte[]> listOf(byte[] a, byte[] b) {
|
||||
List<byte[]> list = new ArrayList<>(2);
|
||||
list.add(a);
|
||||
list.add(b);
|
||||
return list;
|
||||
}
|
||||
|
||||
public static List<byte[]> listOf(byte[] a, byte[] b, byte[] c) {
|
||||
List<byte[]> list = new ArrayList<>(3);
|
||||
list.add(a);
|
||||
list.add(b);
|
||||
list.add(c);
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -116,7 +116,7 @@ function prepareFwTextRun(placed: PlacedFwText): boolean {
|
||||
bytes.set(entry.data.nibbles, offset + 10);
|
||||
offset += 10 + entry.data.nibbles.length;
|
||||
}
|
||||
com.faceclaw.app.FwGlyphAtlas.register(buffer);
|
||||
com.faceclaw.app.FwGlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(buffer));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -130,7 +130,7 @@ function imageId(placed: PlacedImage): number | null {
|
||||
let id: number | null = null;
|
||||
if (source.width > 0 && source.width <= 255 && source.height > 0 && source.height <= 255) {
|
||||
const key = `img:${source.width}x${source.height}:${source.contentHash32().toString(16)}`;
|
||||
id = com.faceclaw.app.ImageAtlas.ensure(key, source.width, source.height, source.pixels.buffer);
|
||||
id = com.faceclaw.app.ImageAtlas.ensure(key, source.width, source.height, new com.faceclaw.app.AndroidByteReader(source.pixels.buffer));
|
||||
if (!(typeof id === "number") || id <= 0) id = null;
|
||||
}
|
||||
imageIds.set(source, id);
|
||||
@@ -184,9 +184,9 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
|
||||
}
|
||||
if (registration) {
|
||||
const plainBuffer = buildRegistrationBuffer(registration);
|
||||
if (plainBuffer) com.faceclaw.app.GlyphAtlas.register(plainBuffer);
|
||||
if (plainBuffer) com.faceclaw.app.GlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(plainBuffer));
|
||||
const aaBuffer = buildAaRegistrationBuffer(registration);
|
||||
if (aaBuffer) com.faceclaw.app.GlyphAtlas.registerAa(aaBuffer);
|
||||
if (aaBuffer) com.faceclaw.app.GlyphAtlas.registerAa(new com.faceclaw.app.AndroidByteReader(aaBuffer));
|
||||
}
|
||||
if (bytes === 0) return null;
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { runKotlinProtocolSmokeTest } from './kotlin-protocol-smoke';
|
||||
import { isAndroid } from "@nativescript/core";
|
||||
|
||||
// Handwritten declarations for this small native surface. NativeScript builds
|
||||
@@ -18,6 +19,7 @@ export function runKotlinBridgeSmokeTest(): void {
|
||||
if (!isAndroid) return;
|
||||
|
||||
try {
|
||||
runKotlinProtocolSmokeTest();
|
||||
const bridge = new com.faceclaw.shared.KotlinBridge();
|
||||
const greeting = bridge.greet("Faceclaw");
|
||||
const message = "Faceclaw \u2194 Kotlin \ud83d\udc53";
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
declare const com: any
|
||||
|
||||
/** Development-only checks of NativeScript metadata and bulk ByteBuffer crossings. */
|
||||
export function runKotlinProtocolSmokeTest(): void {
|
||||
const native = com.faceclaw.app
|
||||
const hex = (data: any) => Array.from({ length: data.length }, (_, i) => (data[i] & 255).toString(16).padStart(2, '0')).join('')
|
||||
const assert = (condition: boolean, what: string) => { if (!condition) throw new Error(`Kotlin protocol bridge: ${what}`) }
|
||||
const auth = native.BleProtocol.buildAuthenticationRequest(101)
|
||||
assert(hex(auth) === '080410651a0408011004', 'authentication')
|
||||
const compositor = new native.SurfaceCompositor()
|
||||
compositor.configureScreen(3, 2)
|
||||
compositor.configureSurface('test', 0, 0, 3, 2, 0, 0)
|
||||
const pixels = new Uint8Array([0, 16, 255, 32, 48, 64])
|
||||
compositor.submitSurface('test', new native.AndroidByteReader(pixels.buffer), 0, 0, 3, 2, '1')
|
||||
assert(hex(compositor.composite().gray) === '0010ff203040', 'composition/ByteBuffer')
|
||||
assert(hex(native.BmpUtil.pack4bppFromGray8(compositor.composite().gray, 3, 2)) === '01f02340', 'packing')
|
||||
const id = native.ImageAtlas.ensure('kotlin-smoke', 3, 2, new native.AndroidByteReader(pixels.buffer))
|
||||
assert(id > 0 && native.ImageAtlas.get(id).width === 3, 'image atlas/reused buffer')
|
||||
console.log('FACECLAW_KOTLIN_PROTOCOL_PASS Android')
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
const { buildKotlin, checkAndroidRuntime } = require('../../scripts/kotlin-build.cjs');
|
||||
|
||||
module.exports = function (hookArgs) {
|
||||
// NativeScript parses hooks with an older JavaScript parser; avoid optional chaining.
|
||||
const { platform, release } = hookArgs.prepareData;
|
||||
if (platform === 'android') checkAndroidRuntime();
|
||||
return buildKotlin(platform, release ? 'release' : 'debug');
|
||||
};
|
||||
@@ -0,0 +1,79 @@
|
||||
# Shared Kotlin
|
||||
|
||||
Both `main` and `ios-port` build the same Kotlin Multiplatform sources in
|
||||
`native/kotlin/shared/src`. NativeScript's `before-prepare` hook builds an AAR
|
||||
for Android or a static `FaceclawKit.xcframework` for iOS, then stages it in the
|
||||
local `@faceclaw/kotlin` plugin. Generated binaries and Gradle caches are ignored.
|
||||
|
||||
- `commonMain/kotlin`: wire protocol, fragmentation/reassembly, event decoding,
|
||||
image/RLE/texture planning, retained surface composition, glyph/image atlases,
|
||||
LVGL font parsing, spectral noise suppression, PNG/GIF/WAV encodings and
|
||||
callback interfaces.
|
||||
- `androidMain/kotlin`: monotonic clock, reentrant locks, JVM zlib and file I/O,
|
||||
zero-copy `ByteBuffer` readers, firmware hashing/file writes and WAV file storage.
|
||||
- `iosMain/kotlin`: monotonic clock, recursive locks, native zlib and POSIX file
|
||||
reads, plus `NSData` facades for NativeScript's Objective-C bridge.
|
||||
|
||||
The `com.faceclaw.app` package, `@JvmStatic` methods and `@JvmField` properties
|
||||
preserve existing Java/NativeScript callers. Android's `java.nio.ByteBuffer`
|
||||
entry points for atlases/composition now take `AndroidByteReader`, keeping the
|
||||
bulk buffer crossing without putting Java APIs in common code. iOS uses
|
||||
`IosByteReader` and `NSData`; binary bridges avoid per-element JS/native calls.
|
||||
|
||||
## Platform boundaries
|
||||
|
||||
`ProtocolPlatform` supplies clocks, locks and persistent deflaters. `expect` /
|
||||
`actual` functions select default platform services at compilation. Stateful
|
||||
transports and caches retain reentrant locking; callbacks still run on their
|
||||
caller's thread. The migration does not introduce implicit UI-thread dispatch.
|
||||
`OutboundMessage` and `CfwMessageWindow` require their owner's synchronization.
|
||||
|
||||
`CfwTransport` uses ordered `SYNC_FLUSH` records with one persistent compression
|
||||
context. Lens changes and replay resets reset that context. Owners can close it
|
||||
explicitly; iOS also cleans up abandoned native allocations. GATT write modes
|
||||
are mapped by platform adapters.
|
||||
|
||||
`migrated-java-sources.json` lists each removed Java source and its Kotlin
|
||||
replacement. Android's build removes precisely those stale generated copies
|
||||
before compilation. `android-java-boundaries.json` inventories every retained
|
||||
Java file and its Android API boundary: services/receivers, GATT, media,
|
||||
WebView, SQLite, rendering, networking and JNI. The sherpa-onnx Java classes
|
||||
remain the vendored Android JNI ABI. PNG/GIF adapters retain only Android
|
||||
storage responsibilities; their codecs are shared.
|
||||
|
||||
On `ios-port`, platform adapters route the existing TypeScript session and UI
|
||||
logic through shared Kotlin for protocol builders, CRC/framing/reassembly,
|
||||
event decoding, CFW compression/ACKs, changed-image regions, surface composition,
|
||||
and LVGL glyphs. The original pure TypeScript implementations remain as Node
|
||||
test references; NativeScript selects the `.ios.ts` native adapters. Features
|
||||
not yet present in the iOS app can use the shared audio/recording and texture
|
||||
classes without another Java port.
|
||||
|
||||
## Build and validation
|
||||
|
||||
Requires JDK 21, Android SDK 35 and the existing `wear/gradlew` wrapper. iOS
|
||||
additionally requires macOS, Xcode and an arm64 simulator. Configure paths in
|
||||
`build_paths.sh`. Both production and tests use Kotlin 2.4.20 and Android KMP
|
||||
plugin 8.13.2.
|
||||
|
||||
```sh
|
||||
npm ci
|
||||
# Once when upgrading an existing generated Android platform to runtime 9.1.1:
|
||||
npx nativescript platform clean android
|
||||
npm run test:kotlin
|
||||
npm run test:kotlin:ios
|
||||
./build.sh
|
||||
# In ios-port:
|
||||
./scripts/ios.sh build ios --emulator
|
||||
```
|
||||
|
||||
`tests/kotlin` compiles production sources directly. Common tests run on JVM
|
||||
and iOS: wire vectors, framing/corruption/expiry, streaming compression, replay,
|
||||
composition, PNG pixels, font glyphs, and Java SHA-256 golden outputs for DSP
|
||||
and GIF. Android additionally runs the existing Java protocol fixtures and
|
||||
concurrency checks; iOS exercises `NSData` and compositor adapters. Debug app
|
||||
startup checks real NativeScript metadata and bidirectional callbacks, logging
|
||||
`FACECLAW_KOTLIN_BRIDGE_PASS` and `FACECLAW_KOTLIN_PROTOCOL_PASS`.
|
||||
|
||||
These checks require no BLE hardware. Physical glasses/ring behavior still
|
||||
needs device testing, particularly sustained rendering and microphone traffic.
|
||||
@@ -0,0 +1,136 @@
|
||||
[
|
||||
{
|
||||
"reason": "BluetoothGatt, BluetoothLeScanner, Android callback threads",
|
||||
"files": [
|
||||
"com/faceclaw/app/FaceclawBleCommunicator.java",
|
||||
"com/faceclaw/app/FaceclawBleManager.java",
|
||||
"com/faceclaw/app/FaceclawDeviceDiscovery.java",
|
||||
"com/faceclaw/app/FaceclawDeviceInfoProbe.java",
|
||||
"com/faceclaw/app/FaceclawFirmwareFlasher.java",
|
||||
"com/faceclaw/app/FaceclawFlashPromptCommunicator.java"
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "AlarmManager, PendingIntent, Android activities/services/receivers",
|
||||
"files": [
|
||||
"com/faceclaw/app/FaceclawAlarmActivity.java",
|
||||
"com/faceclaw/app/FaceclawAlarmReceiver.java",
|
||||
"com/faceclaw/app/FaceclawAlarmRescheduleReceiver.java",
|
||||
"com/faceclaw/app/FaceclawAlarmService.java",
|
||||
"com/faceclaw/app/FaceclawAlarms.java",
|
||||
"com/faceclaw/app/FaceclawForegroundService.java"
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Android providers, sensors, permissions and preferences",
|
||||
"files": [
|
||||
"com/faceclaw/app/FaceclawCalendarProvider.java",
|
||||
"com/faceclaw/app/FaceclawLocationProvider.java",
|
||||
"com/faceclaw/app/FaceclawLocationTracker.java",
|
||||
"com/faceclaw/app/FaceclawSettings.java",
|
||||
"com/faceclaw/app/FaceclawSettingsPortReceiver.java",
|
||||
"com/faceclaw/app/FaceclawResourceUsage.java"
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Android MediaSession, notifications, status icons and wearable services",
|
||||
"files": [
|
||||
"com/faceclaw/app/FaceclawMediaBrowser.java",
|
||||
"com/faceclaw/app/FaceclawMediaController.java",
|
||||
"com/faceclaw/app/FaceclawMediaNotificationListenerService.java",
|
||||
"com/faceclaw/app/FaceclawSystemStatusIconProvider.java",
|
||||
"com/faceclaw/app/FaceclawWearBridge.java",
|
||||
"com/faceclaw/app/FaceclawWearListenerService.java"
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Android WebView/JavascriptInterface, package manager and intent integration",
|
||||
"files": [
|
||||
"com/faceclaw/app/FaceclawEvenAppDetector.java",
|
||||
"com/faceclaw/app/FaceclawEvenHubDocumentStart.java",
|
||||
"com/faceclaw/app/FaceclawEvenHubJsBridge.java",
|
||||
"com/faceclaw/app/FaceclawEvenHubWebView.java",
|
||||
"com/faceclaw/app/FaceclawEvenHubWebViewClient.java",
|
||||
"com/faceclaw/app/FaceclawEvenHubWebViewHost.java"
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Android AudioRecord/AudioTrack/MediaCodec, Handler delivery and JNI speech/model engines",
|
||||
"files": [
|
||||
"com/faceclaw/app/AndroidNoiseEffects.java",
|
||||
"com/faceclaw/app/FaceclawAudioTranscoder.java",
|
||||
"com/faceclaw/app/FaceclawCaptionEngine.java",
|
||||
"com/faceclaw/app/FaceclawDiarizer.java",
|
||||
"com/faceclaw/app/FaceclawLc3Decoder.java",
|
||||
"com/faceclaw/app/FaceclawLlamaRunner.java",
|
||||
"com/faceclaw/app/FaceclawSpeakerId.java",
|
||||
"com/faceclaw/app/FaceclawVoiceController.java"
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Android SQLite persistence and cursor/content APIs",
|
||||
"files": [
|
||||
"com/faceclaw/app/FaceclawConversationStore.java"
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Android OkHttp backend, Handler/Log delivery, download lifecycle",
|
||||
"files": [
|
||||
"com/faceclaw/app/FaceclawHttp.java",
|
||||
"com/faceclaw/app/FaceclawModelDownloader.java",
|
||||
"com/faceclaw/app/FaceclawSseRequest.java",
|
||||
"com/faceclaw/app/FaceclawWebSocket.java"
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Android Bitmap/Canvas/Paint/typeface, SVG rasterization and camera scanning",
|
||||
"files": [
|
||||
"com/faceclaw/app/FontFileRenderer.java",
|
||||
"com/faceclaw/app/IconRenderer.java",
|
||||
"com/faceclaw/app/ImageFileLoader.java",
|
||||
"com/faceclaw/app/PreviewBitmapUtil.java",
|
||||
"com/faceclaw/app/FaceclawQrScanner.java"
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Android preview/UI delivery and app-scoped export paths; codecs/composition are shared",
|
||||
"files": [
|
||||
"com/faceclaw/app/FaceclawPreviewCompositor.java",
|
||||
"com/faceclaw/app/GifScreenRecorder.java",
|
||||
"com/faceclaw/app/ScreenshotUtil.java"
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Android elapsedRealtime, worker wait/notify and Context-backed trace export",
|
||||
"files": [
|
||||
"com/faceclaw/app/FrameTimings.java",
|
||||
"com/faceclaw/app/util/InterruptibleSleep.java"
|
||||
]
|
||||
},
|
||||
{
|
||||
"reason": "Vendored sherpa-onnx JNI ABI: Java classes/fields are consumed by the Android native library; iOS uses its native engine APIs.",
|
||||
"files": [
|
||||
"com/k2fsa/sherpa/onnx/FastClusteringConfig.java",
|
||||
"com/k2fsa/sherpa/onnx/FeatureConfig.java",
|
||||
"com/k2fsa/sherpa/onnx/HomophoneReplacerConfig.java",
|
||||
"com/k2fsa/sherpa/onnx/OfflineModelConfig.java",
|
||||
"com/k2fsa/sherpa/onnx/OfflineMoonshineModelConfig.java",
|
||||
"com/k2fsa/sherpa/onnx/OfflineRecognizer.java",
|
||||
"com/k2fsa/sherpa/onnx/OfflineRecognizerConfig.java",
|
||||
"com/k2fsa/sherpa/onnx/OfflineRecognizerResult.java",
|
||||
"com/k2fsa/sherpa/onnx/OfflineSpeakerDiarization.java",
|
||||
"com/k2fsa/sherpa/onnx/OfflineSpeakerDiarizationCallback.java",
|
||||
"com/k2fsa/sherpa/onnx/OfflineSpeakerDiarizationConfig.java",
|
||||
"com/k2fsa/sherpa/onnx/OfflineSpeakerDiarizationSegment.java",
|
||||
"com/k2fsa/sherpa/onnx/OfflineSpeakerSegmentationModelConfig.java",
|
||||
"com/k2fsa/sherpa/onnx/OfflineSpeakerSegmentationPyannoteModelConfig.java",
|
||||
"com/k2fsa/sherpa/onnx/OfflineStream.java",
|
||||
"com/k2fsa/sherpa/onnx/OfflineWhisperModelConfig.java",
|
||||
"com/k2fsa/sherpa/onnx/OnlineStream.java",
|
||||
"com/k2fsa/sherpa/onnx/QnnConfig.java",
|
||||
"com/k2fsa/sherpa/onnx/SpeakerEmbeddingExtractor.java",
|
||||
"com/k2fsa/sherpa/onnx/SpeakerEmbeddingExtractorConfig.java",
|
||||
"com/k2fsa/sherpa/onnx/SpeakerEmbeddingManager.java"
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,4 @@
|
||||
plugins {
|
||||
kotlin("multiplatform") version "2.4.20" apply false
|
||||
id("com.android.kotlin.multiplatform.library") version "8.13.2" apply false
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
[
|
||||
{
|
||||
"java": "com/faceclaw/app/LvglFontFile.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/graphics/LvglFontFile.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawAlarmListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAlarmListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawAmbientLightListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAmbientLightListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawAudioPacketListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawAudioPacketListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawBleCommunicatorListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleCommunicatorListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawBleListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawBleListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawCaptionEngineListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawCaptionEngineListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawCompassListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawCompassListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawDeviceDiscoveryListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawDeviceDiscoveryListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawDeviceInfoProbeListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawDeviceInfoProbeListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawEvenHubListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawEvenHubListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawFirmwareFlasherListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFirmwareFlasherListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawFlashPromptListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawFlashPromptListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawImuListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawImuListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawLlamaListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLlamaListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawLocationListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLocationListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawLocationTrackerListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawLocationTrackerListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawMediaBrowserListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMediaBrowserListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawMediaControllerListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMediaControllerListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawMicStatusListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawMicStatusListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawModelDownloaderListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawModelDownloaderListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawNoiseSuppressor.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/audio/FaceclawNoiseSuppressor.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawNotificationListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawNotificationListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawQrScannerListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawQrScannerListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawRingEventDecoder.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/FaceclawRingEventDecoder.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawSettingsListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSettingsListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawSseListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawSseListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawVoiceControllerListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawVoiceControllerListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawWearListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawWearListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawWebSocketListener.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/callbacks/FaceclawWebSocketListener.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FwGlyphAtlas.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/graphics/FwGlyphAtlas.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/g2protocol/BleImageOptimizer.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleImageOptimizer.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/g2protocol/BleMagicPool.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleMagicPool.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/g2protocol/BleProtocol.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/g2protocol/CfwMessageWindow.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwMessageWindow.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/g2protocol/CfwTransport.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwTransport.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/g2protocol/ConnectionOptions.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ConnectionOptions.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/g2protocol/G2Event.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/G2Event.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/g2protocol/MessageBuilder.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/MessageBuilder.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/g2protocol/OutboundMessage.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/OutboundMessage.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/g2protocol/TextureCacheState.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TextureCacheState.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/g2protocol/TexturePlanner.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TexturePlanner.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/GlyphAtlas.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/graphics/GlyphAtlas.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/ImageAtlas.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/graphics/ImageAtlas.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/SurfaceCompositor.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/graphics/SurfaceCompositor.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/util/BmpUtil.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/util/BmpUtil.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/util/CollectionUtils.java",
|
||||
"kotlin": "shared/src/commonMain/kotlin/com/faceclaw/app/util/CollectionUtils.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawFirmwareUtil.java",
|
||||
"kotlin": "shared/src/androidMain/kotlin/com/faceclaw/app/FaceclawFirmwareUtil.kt"
|
||||
},
|
||||
{
|
||||
"java": "com/faceclaw/app/FaceclawWavRecorder.java",
|
||||
"kotlin": "shared/src/androidMain/kotlin/com/faceclaw/app/FaceclawWavRecorder.kt"
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1 @@
|
||||
// Native APIs are exposed by NativeScript's platform metadata.
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "@faceclaw/kotlin",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"main": "index.js",
|
||||
"nativescript": { "platforms": { "android": "9.1.1", "ios": "9.0.3" } }
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
pluginManagement { repositories { google(); mavenCentral(); gradlePluginPortal() } }
|
||||
dependencyResolutionManagement { repositories { google(); mavenCentral() } }
|
||||
rootProject.name = "FaceclawKotlin"
|
||||
include(":shared")
|
||||
@@ -0,0 +1,25 @@
|
||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework
|
||||
|
||||
plugins {
|
||||
kotlin("multiplatform")
|
||||
id("com.android.kotlin.multiplatform.library")
|
||||
}
|
||||
kotlin {
|
||||
android {
|
||||
namespace = "com.faceclaw.shared"
|
||||
compileSdk = 35
|
||||
minSdk = 24
|
||||
compilerOptions { jvmTarget.set(JvmTarget.JVM_17) }
|
||||
withHostTestBuilder {}.configure {}
|
||||
}
|
||||
val framework = XCFramework("FaceclawKit")
|
||||
listOf(iosArm64(), iosSimulatorArm64()).forEach { target ->
|
||||
target.binaries.framework {
|
||||
baseName = "FaceclawKit"
|
||||
isStatic = true
|
||||
binaryOption("bundleId", "com.faceclaw.shared")
|
||||
framework.add(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
/** NativeScript's ArrayBuffer-backed ByteBuffer stays on the JVM side. */
|
||||
class AndroidByteReader(buffer: ByteBuffer) : ByteReader() {
|
||||
// NativeScript can reuse the same ByteBuffer for an ArrayBuffer. Reading one
|
||||
// frame must not advance the cursor used by a later atlas/preview crossing.
|
||||
private val buffer = buffer.duplicate()
|
||||
override fun remaining(): Int = buffer.remaining()
|
||||
|
||||
override fun get(): Byte = buffer.get()
|
||||
|
||||
override fun get(index: Int): Byte = buffer.get(index)
|
||||
|
||||
override fun get(bytes: ByteArray, offset: Int, length: Int): ByteReader {
|
||||
buffer.get(bytes, offset, length)
|
||||
return this
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import java.io.FileOutputStream
|
||||
import java.nio.ByteBuffer
|
||||
import java.security.MessageDigest
|
||||
|
||||
/** Android's bulk ByteBuffer boundary avoids per-byte NativeScript crossings. */
|
||||
object FaceclawFirmwareUtil {
|
||||
@JvmStatic fun bytesToHex(bytes: ByteArray?): String = BinaryEncoding.bytesToHex(bytes)
|
||||
|
||||
@JvmStatic
|
||||
fun sha256Hex(data: ByteBuffer): String {
|
||||
val digest = MessageDigest.getInstance("SHA-256")
|
||||
digest.update(data.duplicate())
|
||||
return bytesToHex(digest.digest())
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun writeFile(path: String, data: ByteBuffer) {
|
||||
try {
|
||||
FileOutputStream(path).use { file ->
|
||||
file.channel.use { channel ->
|
||||
val view = data.duplicate()
|
||||
while (view.hasRemaining()) channel.write(view)
|
||||
}
|
||||
}
|
||||
} catch (e: java.io.IOException) {
|
||||
throw RuntimeException("firmware write failed: ${e.message}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import android.util.Log
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.io.RandomAccessFile
|
||||
|
||||
/** Android streaming file adapter; the PCM/WAV format is shared Kotlin. */
|
||||
class FaceclawWavRecorder
|
||||
@Throws(IOException::class)
|
||||
constructor(
|
||||
private val path: String,
|
||||
private val sampleRate: Int,
|
||||
private val channels: Int,
|
||||
) {
|
||||
private var file: RandomAccessFile?
|
||||
private var dataBytes = 0L
|
||||
|
||||
init {
|
||||
val parent = File(path).parentFile
|
||||
if (parent != null && !parent.exists() && !parent.mkdirs())
|
||||
throw IOException("could not create $parent")
|
||||
file =
|
||||
RandomAccessFile(path, "rw").also {
|
||||
it.setLength(0)
|
||||
it.write(BinaryEncoding.wavHeader(0, sampleRate, channels))
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun append(pcm16le: ByteArray?) {
|
||||
val current = file ?: return
|
||||
if (pcm16le == null || pcm16le.isEmpty()) return
|
||||
try {
|
||||
current.write(pcm16le)
|
||||
dataBytes += pcm16le.size
|
||||
} catch (e: IOException) {
|
||||
Log.w("FaceclawWavRecorder", "append failed", e)
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized fun getDurationMs(): Long = dataBytes * 1000L / (sampleRate * channels * 2L)
|
||||
|
||||
fun getPath(): String = path
|
||||
|
||||
@Synchronized
|
||||
fun finish(): Long {
|
||||
val current = file ?: return dataBytes
|
||||
file = null
|
||||
try {
|
||||
current.use {
|
||||
it.seek(0)
|
||||
it.write(
|
||||
BinaryEncoding.wavHeader(
|
||||
minOf(dataBytes, Int.MAX_VALUE.toLong()).toInt(),
|
||||
sampleRate,
|
||||
channels,
|
||||
)
|
||||
)
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
Log.w("FaceclawWavRecorder", "finish failed", e)
|
||||
}
|
||||
return dataBytes
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun discard() {
|
||||
finish()
|
||||
File(path).delete()
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
internal actual fun protocolPlatform(): ProtocolPlatform = AndroidProtocolPlatform
|
||||
|
||||
internal actual fun readFileData(path: String): ByteArray {
|
||||
val file = java.io.File(path)
|
||||
require(file.isFile && file.canRead() && file.length() <= Int.MAX_VALUE)
|
||||
return file.readBytes()
|
||||
}
|
||||
|
||||
internal actual fun deflateData(bytes: ByteArray): ByteArray {
|
||||
val encoder = java.util.zip.Deflater()
|
||||
try {
|
||||
encoder.setInput(bytes)
|
||||
encoder.finish()
|
||||
val out = ByteSink()
|
||||
val buffer = ByteArray(8192)
|
||||
while (!encoder.finished()) out.write(buffer, 0, encoder.deflate(buffer))
|
||||
return out.toByteArray()
|
||||
} finally {
|
||||
encoder.end()
|
||||
}
|
||||
}
|
||||
|
||||
internal actual fun inflateData(bytes: ByteArray, size: Int): ByteArray {
|
||||
val decoder = java.util.zip.Inflater()
|
||||
try {
|
||||
decoder.setInput(bytes)
|
||||
val result = ByteArray(size)
|
||||
var offset = 0
|
||||
while (offset < size && !decoder.finished()) {
|
||||
val count = decoder.inflate(result, offset, size - offset)
|
||||
if (count == 0) break
|
||||
offset += count
|
||||
}
|
||||
check(offset == size) { "recorded frame truncated: $offset of $size" }
|
||||
return result
|
||||
} finally {
|
||||
decoder.end()
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import android.bluetooth.BluetoothGattCharacteristic
|
||||
import android.os.SystemClock
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.util.concurrent.locks.ReentrantLock
|
||||
import java.util.zip.Deflater
|
||||
|
||||
object AndroidProtocolPlatform : ProtocolPlatform {
|
||||
override fun elapsedRealtimeMs(): Long = SystemClock.elapsedRealtime()
|
||||
|
||||
override fun createLock(): ProtocolLock =
|
||||
object : ProtocolLock {
|
||||
private val lock = ReentrantLock()
|
||||
|
||||
override fun lock() = lock.lock()
|
||||
|
||||
override fun unlock() = lock.unlock()
|
||||
}
|
||||
|
||||
override fun createDeflater(): ProtocolDeflater = AndroidDeflater()
|
||||
|
||||
@JvmStatic
|
||||
fun writeType(mode: GattWriteMode): Int =
|
||||
when (mode) {
|
||||
GattWriteMode.WITH_RESPONSE -> BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
|
||||
GattWriteMode.WITHOUT_RESPONSE -> BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE
|
||||
}
|
||||
}
|
||||
|
||||
private class AndroidDeflater : ProtocolDeflater {
|
||||
private val deflater = Deflater()
|
||||
|
||||
override fun reset() = deflater.reset()
|
||||
|
||||
override fun syncFlush(message: ByteArray): ByteArray {
|
||||
deflater.setInput(message)
|
||||
val out = ByteArrayOutputStream()
|
||||
val chunk = ByteArray(4096)
|
||||
do {
|
||||
val count = deflater.deflate(chunk, 0, chunk.size, Deflater.SYNC_FLUSH)
|
||||
out.write(chunk, 0, count)
|
||||
} while (count == chunk.size)
|
||||
return out.toByteArray()
|
||||
}
|
||||
|
||||
override fun close() = deflater.end()
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.faceclaw.shared
|
||||
|
||||
import android.os.Looper
|
||||
|
||||
internal actual object PlatformInfo {
|
||||
actual val name: String = "Android"
|
||||
|
||||
actual fun threadName(): String = Thread.currentThread().name
|
||||
|
||||
actual fun isMainThread(): Boolean = Looper.myLooper() == Looper.getMainLooper()
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** Platform selection happens at compilation; protocol algorithms stay shared. */
|
||||
internal expect fun protocolPlatform(): ProtocolPlatform
|
||||
|
||||
internal expect fun deflateData(bytes: ByteArray): ByteArray
|
||||
|
||||
internal expect fun inflateData(bytes: ByteArray, size: Int): ByteArray
|
||||
|
||||
internal expect fun readFileData(path: String): ByteArray
|
||||
+263
@@ -0,0 +1,263 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/**
|
||||
* Spectral suppression for PCM from glasses microphones: 16 ms sqrt-Hann frames at 50% overlap,
|
||||
* asymmetric noise-floor tracking and a smoothed Wiener gain. Android capture-session effects live
|
||||
* in AndroidNoiseEffects. This processor has no platform dependencies and must be owned by a single
|
||||
* audio worker.
|
||||
*/
|
||||
class FaceclawNoiseSuppressor {
|
||||
companion object {
|
||||
private const val FFT_SIZE: Int = 256
|
||||
|
||||
private val HOP: Int = (FFT_SIZE / 2)
|
||||
|
||||
private val OVERSUBTRACTION: Float = 1.6f
|
||||
|
||||
/** About -16 dB of residual: keeps the noise bed natural instead of gated. */
|
||||
private val GAIN_FLOOR: Float = 0.15f
|
||||
|
||||
private val GAIN_SMOOTHING: Float = 0.6f
|
||||
|
||||
/** Frames of pure noise-floor learning after start/reset (~100 ms). */
|
||||
private const val NOISE_WARMUP_FRAMES: Int = 12
|
||||
|
||||
/** In-place iterative radix-2 FFT (inverse includes the 1/N scale). */
|
||||
private fun fft(re: FloatArray, im: FloatArray, inverse: Boolean): Unit {
|
||||
var n: Int = re.size
|
||||
run {
|
||||
var i: Int = 1
|
||||
var j: Int = 0
|
||||
while ((i < n)) {
|
||||
var bit: Int = (n shr 1)
|
||||
run {
|
||||
while (((j and bit) != 0)) {
|
||||
j = (j xor bit)
|
||||
bit = (bit shr 1)
|
||||
}
|
||||
}
|
||||
j = (j xor bit)
|
||||
if ((i < j)) {
|
||||
var t: Float = re[i]
|
||||
re[i] = re[j]
|
||||
re[j] = t
|
||||
t = im[i]
|
||||
im[i] = im[j]
|
||||
im[j] = t
|
||||
}
|
||||
i++
|
||||
}
|
||||
}
|
||||
run {
|
||||
var len: Int = 2
|
||||
while ((len <= n)) {
|
||||
var ang: Double = (((2 * kotlin.math.PI) / len) * (if (inverse) 1 else -1))
|
||||
var wRe: Float = (kotlin.math.cos(ang)).toFloat()
|
||||
var wIm: Float = (kotlin.math.sin(ang)).toFloat()
|
||||
run {
|
||||
var i: Int = 0
|
||||
while ((i < n)) {
|
||||
var curRe: Float = 1f
|
||||
var curIm: Float = 0f
|
||||
run {
|
||||
var k: Int = 0
|
||||
while ((k < (len / 2))) {
|
||||
var a: Int = (i + k)
|
||||
var b: Int = (a + (len / 2))
|
||||
var xr: Float = ((re[b] * curRe) - (im[b] * curIm))
|
||||
var xi: Float = ((re[b] * curIm) + (im[b] * curRe))
|
||||
re[b] = (re[a] - xr)
|
||||
im[b] = (im[a] - xi)
|
||||
re[a] += xr
|
||||
im[a] += xi
|
||||
var nextRe: Float = ((curRe * wRe) - (curIm * wIm))
|
||||
curIm = ((curRe * wIm) + (curIm * wRe))
|
||||
curRe = nextRe
|
||||
k++
|
||||
}
|
||||
}
|
||||
i += len
|
||||
}
|
||||
}
|
||||
len = (len shl 1)
|
||||
}
|
||||
}
|
||||
if (inverse) {
|
||||
run {
|
||||
var i: Int = 0
|
||||
while ((i < n)) {
|
||||
re[i] /= n
|
||||
im[i] /= n
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val sampleRate: Int
|
||||
|
||||
private val window: FloatArray = FloatArray(FFT_SIZE)
|
||||
|
||||
private val history: FloatArray = FloatArray(FFT_SIZE)
|
||||
|
||||
private val overlap: FloatArray = FloatArray(HOP)
|
||||
|
||||
private val noise: FloatArray = FloatArray(((FFT_SIZE / 2) + 1))
|
||||
|
||||
private val gain: FloatArray = FloatArray(((FFT_SIZE / 2) + 1))
|
||||
|
||||
private val re: FloatArray = FloatArray(FFT_SIZE)
|
||||
|
||||
private val im: FloatArray = FloatArray(FFT_SIZE)
|
||||
|
||||
private val pending: ShortArray = ShortArray(HOP)
|
||||
|
||||
private var pendingCount: Int = 0
|
||||
|
||||
private var warmupFrames: Int = 0
|
||||
|
||||
constructor(sampleRate: Int) {
|
||||
this.sampleRate = sampleRate
|
||||
run {
|
||||
var i: Int = 0
|
||||
while ((i < FFT_SIZE)) {
|
||||
window[i] = (kotlin.math.sin(((kotlin.math.PI * (i + 0.5)) / FFT_SIZE))).toFloat()
|
||||
i++
|
||||
}
|
||||
}
|
||||
reset()
|
||||
}
|
||||
|
||||
fun getSampleRate(): Int {
|
||||
return sampleRate
|
||||
}
|
||||
|
||||
/** Forget the learned noise floor (e.g. after the suppressor was toggled off). */
|
||||
fun reset(): Unit {
|
||||
history.fill(0f)
|
||||
overlap.fill(0f)
|
||||
noise.fill(0f)
|
||||
gain.fill(1f)
|
||||
pendingCount = 0
|
||||
warmupFrames = 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Suppress noise in 16-bit little-endian mono PCM. Streaming: input not filling a whole 8 ms
|
||||
* hop is carried into the next call, so the output may be up to one hop shorter than the input
|
||||
* (and longer on a later call); total latency is one hop.
|
||||
*/
|
||||
fun process(pcm16le: ByteArray?): ByteArray? {
|
||||
if (((pcm16le == null) || (pcm16le.size < 2))) {
|
||||
return pcm16le
|
||||
}
|
||||
var sampleCount: Int = (pcm16le.size / 2)
|
||||
var hops: Int = ((pendingCount + sampleCount) / HOP)
|
||||
var out: ByteArray = ByteArray(((hops * HOP) * 2))
|
||||
var outPos: Int = 0
|
||||
var inPos: Int = 0
|
||||
while ((inPos < sampleCount)) {
|
||||
var take: Int = minOf((HOP - pendingCount), (sampleCount - inPos))
|
||||
run {
|
||||
var i: Int = 0
|
||||
while ((i < take)) {
|
||||
var lo: Int = (pcm16le[((inPos + i) * 2)] and 0xff)
|
||||
var hi: Int = pcm16le[(((inPos + i) * 2) + 1)].toInt()
|
||||
pending[(pendingCount + i)] = (((hi shl 8) or lo)).toShort()
|
||||
i++
|
||||
}
|
||||
}
|
||||
pendingCount += take
|
||||
inPos += take
|
||||
if ((pendingCount == HOP)) {
|
||||
outPos = processHop(out, outPos)
|
||||
pendingCount = 0
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
private fun processHop(out: ByteArray, outPos: Int): Int {
|
||||
var outPos = outPos
|
||||
history.copyInto(history, 0, HOP, HOP + (FFT_SIZE - HOP))
|
||||
run {
|
||||
var i: Int = 0
|
||||
while ((i < HOP)) {
|
||||
history[((FFT_SIZE - HOP) + i)] = (pending[i] / 32768f)
|
||||
i++
|
||||
}
|
||||
}
|
||||
run {
|
||||
var i: Int = 0
|
||||
while ((i < FFT_SIZE)) {
|
||||
re[i] = (history[i] * window[i])
|
||||
im[i] = 0f
|
||||
i++
|
||||
}
|
||||
}
|
||||
fft(re, im, false)
|
||||
var warming: Boolean = (warmupFrames < NOISE_WARMUP_FRAMES)
|
||||
if (warming) {
|
||||
warmupFrames++
|
||||
}
|
||||
run {
|
||||
var b: Int = 0
|
||||
while ((b <= (FFT_SIZE / 2))) {
|
||||
var power: Float = ((re[b] * re[b]) + (im[b] * im[b]))
|
||||
if (warming) {
|
||||
noise[b] =
|
||||
(if ((warmupFrames == 1)) power
|
||||
else (noise[b] + ((power - noise[b]) / warmupFrames)))
|
||||
} else {
|
||||
if ((power < noise[b])) {
|
||||
noise[b] += ((power - noise[b]) * 0.2f)
|
||||
} else {
|
||||
noise[b] = minOf(power, ((noise[b] * 1.006f) + 1e-10f))
|
||||
}
|
||||
}
|
||||
var clean: Float = (power - (OVERSUBTRACTION * noise[b]))
|
||||
var g: Float = (if ((clean > 0f)) (clean / power) else 0f)
|
||||
if ((g < GAIN_FLOOR)) {
|
||||
g = GAIN_FLOOR
|
||||
}
|
||||
g = ((GAIN_SMOOTHING * gain[b]) + ((1f - GAIN_SMOOTHING) * g))
|
||||
gain[b] = g
|
||||
re[b] *= g
|
||||
im[b] *= g
|
||||
if (((b > 0) && (b < (FFT_SIZE / 2)))) {
|
||||
re[(FFT_SIZE - b)] *= g
|
||||
im[(FFT_SIZE - b)] *= g
|
||||
}
|
||||
b++
|
||||
}
|
||||
}
|
||||
fft(re, im, true)
|
||||
run {
|
||||
var i: Int = 0
|
||||
while ((i < HOP)) {
|
||||
var value: Float = ((overlap[i] + (re[i] * window[i])) * 32767f)
|
||||
var sample: Int = value.roundToInt()
|
||||
if ((sample > 32767)) {
|
||||
sample = 32767
|
||||
}
|
||||
if ((sample < -32768)) {
|
||||
sample = -32768
|
||||
}
|
||||
out[(outPos + (i * 2))] = ((sample and 0xff)).toByte()
|
||||
out[((outPos + (i * 2)) + 1)] = (((sample shr 8) and 0xff)).toByte()
|
||||
i++
|
||||
}
|
||||
}
|
||||
run {
|
||||
var i: Int = 0
|
||||
while ((i < HOP)) {
|
||||
overlap[i] = (re[(HOP + i)] * window[(HOP + i)])
|
||||
i++
|
||||
}
|
||||
}
|
||||
return (outPos + (HOP * 2))
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** Phone-side alarm actions (dismiss / snooze), delivered to the JS engine; implemented in JS. */
|
||||
interface FaceclawAlarmListener {
|
||||
fun onPhoneAction(id: Long, action: String?, minutes: Int): Unit
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
interface FaceclawAmbientLightListener {
|
||||
/**
|
||||
* A CFW ambient-light report (settings-channel field 105) from the master temple. body is the
|
||||
* raw 24-byte ['A','L',ver,reason,...] record; it is decoded on the TS side
|
||||
* (app/native/ambient-light.ts).
|
||||
*/
|
||||
fun onAmbientLight(body: ByteArray?): Unit
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
interface FaceclawAudioPacketListener {
|
||||
fun onAudioPacket(data: ByteArray?, arm: String?, arrivalMs: Long): Unit
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
interface FaceclawBleCommunicatorListener {
|
||||
fun onLog(line: String?): Unit
|
||||
|
||||
fun onStateChange(phase: String?, status: String?): Unit
|
||||
|
||||
fun onRingEvent(
|
||||
kind: String?,
|
||||
containerName: String?,
|
||||
eventType: Int,
|
||||
eventSource: Int,
|
||||
systemExitReasonCode: Int,
|
||||
frameId: Int,
|
||||
): Unit
|
||||
|
||||
fun onBatteryState(
|
||||
headsetBattery: Int,
|
||||
headsetCharging: Int,
|
||||
ringBattery: Int,
|
||||
ringCharging: Int,
|
||||
): Unit
|
||||
|
||||
fun onSilentMode(silent: Boolean): Unit
|
||||
|
||||
fun onWearState(wearing: Boolean): Unit
|
||||
|
||||
fun onPhoneLockState(locked: Boolean): Unit
|
||||
|
||||
fun onEvenAppConflict(message: String?): Unit
|
||||
|
||||
fun onFrameMetrics(paintMs: Int, transmitMs: Int, tileCount: Int): Unit
|
||||
|
||||
fun onFrameFinished(frameId: Int, outcome: String?): Unit
|
||||
|
||||
fun onFirmwareInfo(leftVersion: String?, rightVersion: String?, extension: String?): Unit
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
interface FaceclawBleListener {
|
||||
fun onNotification(address: String?, characteristicUuid: String?, data: ByteArray?): Unit
|
||||
|
||||
fun onConnectionStateChange(address: String?, connected: Boolean): Unit
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
interface FaceclawCaptionEngineListener {
|
||||
/**
|
||||
* One finished utterance. text may be empty when no ASR model is loaded or recognition produced
|
||||
* nothing; embedding is the L2-normalized voice-print or null when the speaker model is
|
||||
* unavailable or the utterance was too short. startMs/endMs are on the engine's sample clock
|
||||
* (milliseconds of audio since start()).
|
||||
*/
|
||||
fun onUtterance(
|
||||
text: String?,
|
||||
embedding: FloatArray?,
|
||||
startMs: Long,
|
||||
endMs: Long,
|
||||
peakRms: Double,
|
||||
): Unit
|
||||
|
||||
/** A speech onset was detected; the utterance is now being collected. */
|
||||
fun onSpeechStart(startMs: Long): Unit
|
||||
|
||||
fun onStatus(status: String?): Unit
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** Receives stock compass heading and calibration notifications on the thread that subscribed. */
|
||||
interface FaceclawCompassListener {
|
||||
fun onCompassEvent(
|
||||
command: Int,
|
||||
headingDegrees: Int,
|
||||
magneticAccuracy: Int,
|
||||
magneticAnomalies: Int,
|
||||
orientationSource: Int,
|
||||
diagnosticFlags: Int,
|
||||
sampleTimeMs: Long,
|
||||
): Unit
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** Callbacks from FaceclawDeviceDiscovery's live scan to the TypeScript layer. */
|
||||
interface FaceclawDeviceDiscoveryListener {
|
||||
/**
|
||||
* One advertisement (or one bonded device) as a JSON object: {address, name, manufacturerData
|
||||
* (hex, company id included), rssi|null, txPower|null, connectable|null, bonded, source,
|
||||
* seenAtMs}. Protocol decoding happens in TypeScript (see app/g2/even-advertisement.ts).
|
||||
*/
|
||||
fun onAdvertisement(json: String?): Unit
|
||||
|
||||
/** The platform refused to start or continue the scan. */
|
||||
fun onScanFailed(errorCode: Int, message: String?): Unit
|
||||
|
||||
/** Diagnostic line for the pairing log. */
|
||||
fun onLog(line: String?): Unit
|
||||
}
|
||||
+6
-6
@@ -1,15 +1,15 @@
|
||||
package com.faceclaw.app;
|
||||
package com.faceclaw.app
|
||||
|
||||
/** Callbacks from FaceclawDeviceInfoProbe to the TypeScript layer. */
|
||||
public interface FaceclawDeviceInfoProbeListener {
|
||||
void onLog(String line);
|
||||
interface FaceclawDeviceInfoProbeListener {
|
||||
fun onLog(line: String?): Unit
|
||||
|
||||
/** Lifecycle: connecting/authenticating for each arm, then querying. */
|
||||
void onState(String state, String detail);
|
||||
fun onState(state: String?, detail: String?): Unit
|
||||
|
||||
/** Terminal success: firmware versions and the firmware-extension string (empty on stock). */
|
||||
void onResult(String leftVersion, String rightVersion, String extension);
|
||||
fun onResult(leftVersion: String?, rightVersion: String?, extension: String?): Unit
|
||||
|
||||
/** Terminal failure (couldn't connect or read the version). */
|
||||
void onError(String message);
|
||||
fun onError(message: String?): Unit
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/**
|
||||
* Callbacks from a hosted EvenHub app's WebView to the TypeScript host. All methods are delivered
|
||||
* on the Android main thread (NativeScript's JS thread), regardless of which WebView-internal
|
||||
* thread produced them.
|
||||
*/
|
||||
interface FaceclawEvenHubListener {
|
||||
/**
|
||||
* The app invoked window.flutter_inappwebview.callHandler(handlerName, ...args). argsJson is
|
||||
* the JSON-encoded argument array; callId identifies the promise to resolve via
|
||||
* window.__fcResolve(callId, ok, value).
|
||||
*/
|
||||
fun onEvenAppMessage(handlerName: String?, argsJson: String?, callId: Int): Unit
|
||||
|
||||
/** The WebView finished loading the app's page. */
|
||||
fun onPageFinished(url: String?): Unit
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** Callbacks from FaceclawFirmwareFlasher to the TypeScript layer. */
|
||||
interface FaceclawFirmwareFlasherListener {
|
||||
fun onLog(line: String?): Unit
|
||||
|
||||
/**
|
||||
* Fine-grained progress for the UI bar. `lens` is "left"/"right"; component/block indices are
|
||||
* 1-based against their counts. `bytesSent` / `bytesTotal` cover the whole lens image, so a bar
|
||||
* driven by them moves evenly even though the components differ wildly in size.
|
||||
*/
|
||||
fun onProgress(
|
||||
lens: String?,
|
||||
componentIndex: Int,
|
||||
componentCount: Int,
|
||||
blockIndex: Int,
|
||||
blockCount: Int,
|
||||
bytesSent: Long,
|
||||
bytesTotal: Long,
|
||||
): Unit
|
||||
|
||||
/**
|
||||
* Coarse lifecycle. `state` is one of: validating, connecting, flashing, rebooting, done,
|
||||
* error. `detail` carries the lens name or a message.
|
||||
*/
|
||||
fun onState(state: String?, detail: String?): Unit
|
||||
|
||||
/** Terminal callback: success or failure, with an explanatory message. */
|
||||
fun onComplete(success: Boolean, detail: String?): Unit
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** Callbacks from FaceclawFlashPromptCommunicator to the TypeScript layer. */
|
||||
interface FaceclawFlashPromptListener {
|
||||
fun onLog(line: String?): Unit
|
||||
|
||||
/**
|
||||
* Lifecycle updates. `state` is one of: connecting, connected, prompting, battery, result,
|
||||
* cancelled, timeout, disconnected, error. `detail` carries the "approved"/"declined" text for
|
||||
* result, or an error/explanation message.
|
||||
*/
|
||||
fun onState(state: String?, detail: String?): Unit
|
||||
|
||||
/**
|
||||
* Battery percent of each arm, read after the user approved (or right after auth when the
|
||||
* prompt is skipped); -1 when an arm didn't answer. Always fires before onResult(true).
|
||||
*/
|
||||
fun onBattery(rightPercent: Int, leftPercent: Int): Unit
|
||||
|
||||
/**
|
||||
* Fires once the user picks a menu row (or, with skipPrompt, once the battery is read): true =
|
||||
* flash, false = cancel.
|
||||
*/
|
||||
fun onResult(approved: Boolean): Unit
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/**
|
||||
* Receives IMU (accelerometer) readings decoded from the glasses' sys-event stream. Registered on
|
||||
* FaceclawBleCommunicator via addImuListener; callbacks are delivered on the Android main thread.
|
||||
*/
|
||||
interface FaceclawImuListener {
|
||||
fun onImuData(x: Double, y: Double, z: Double, eventSource: Int): Unit
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/**
|
||||
* Callbacks for one on-phone LLM generation. Like the other bridge listeners, implementations are
|
||||
* typically created on the NativeScript JS thread and FaceclawLlamaRunner posts these callbacks
|
||||
* back to that thread's Looper.
|
||||
*/
|
||||
interface FaceclawLlamaListener {
|
||||
/** A piece of generated text (may be a partial word). */
|
||||
fun onToken(piece: String?): Unit
|
||||
|
||||
/** Generation finished: "stop" (EOG), "length", or "cancelled". */
|
||||
fun onDone(stopReason: String?): Unit
|
||||
|
||||
fun onError(message: String?): Unit
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** One-shot Android location callbacks for the TypeScript Weather bridge. */
|
||||
interface FaceclawLocationListener {
|
||||
fun onLocation(
|
||||
latitude: Double,
|
||||
longitude: Double,
|
||||
accuracyMeters: Float,
|
||||
timestampMs: Long,
|
||||
): Unit
|
||||
|
||||
fun onError(message: String?): Unit
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** Continuous location callbacks for the TypeScript navigation bridge. */
|
||||
interface FaceclawLocationTrackerListener {
|
||||
/**
|
||||
* A new fix. bearingDeg/speedMps are -1 when the fix doesn't carry them (common when
|
||||
* stationary); accuracyMeters is -1 when unknown.
|
||||
*/
|
||||
fun onLocation(
|
||||
latitude: Double,
|
||||
longitude: Double,
|
||||
accuracyMeters: Float,
|
||||
bearingDeg: Float,
|
||||
speedMps: Float,
|
||||
timestampMs: Long,
|
||||
): Unit
|
||||
|
||||
fun onError(message: String?): Unit
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
interface FaceclawMediaBrowserListener {
|
||||
fun onConnectResult(requestId: Int, connected: Boolean, rootId: String?, error: String?): Unit
|
||||
|
||||
fun onBrowseResult(requestId: Int, childrenJson: String?, error: String?): Unit
|
||||
|
||||
/** The service connection dropped after connecting (player crashed or was killed). */
|
||||
fun onDisconnected(): Unit
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
interface FaceclawMediaControllerListener {
|
||||
/** All observed session owners, before filtering or choosing the active player. */
|
||||
fun onSessionAppsChanged(appsJson: String?): Unit
|
||||
|
||||
fun onStateChange(
|
||||
playbackState: String?,
|
||||
packageName: String?,
|
||||
appName: String?,
|
||||
title: String?,
|
||||
artist: String?,
|
||||
album: String?,
|
||||
positionMs: Long,
|
||||
durationMs: Long,
|
||||
playbackSpeed: Float,
|
||||
canPlayPause: Boolean,
|
||||
canSkipNext: Boolean,
|
||||
canSkipPrevious: Boolean,
|
||||
accessEnabled: Boolean,
|
||||
status: String?,
|
||||
): Unit
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
interface FaceclawMicStatusListener {
|
||||
/**
|
||||
* A CFW mic_control field-104 status record from one temple. body is the raw 21-byte
|
||||
* ['M','C',ver,...] record (decoded on the TS side); arm is "L" or "R" for the temple's own BLE
|
||||
* link, "?" if unmatched.
|
||||
*/
|
||||
fun onMicStatus(body: ByteArray?, arm: String?): Unit
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** Progress callbacks for FaceclawModelDownloader, posted to the creating thread's Looper. */
|
||||
interface FaceclawModelDownloaderListener {
|
||||
fun onProgress(bytesDownloaded: Long, totalBytes: Long): Unit
|
||||
|
||||
/** The file is fully downloaded, verified, and moved to its final path. */
|
||||
fun onDone(path: String?): Unit
|
||||
|
||||
fun onError(message: String?): Unit
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
interface FaceclawNotificationListener {
|
||||
fun onNotificationPosted(key: String?): Unit
|
||||
}
|
||||
Loaded 100 of 162 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user