forked from Blendman974/faceclaw
Merge branch 'evenhub-compatibility'
This commit is contained in:
commit
0c0fc7d7f6
152 files changed
+13199
-747
No files matched your search
@@ -42,6 +42,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private static final String G2_SCREEN_WAKE_LOCK_TAG = "Faceclaw:G2Screen";
|
||||
private static final long FACECLAW_WAKE_LEASE_RENEW_MS = 45_000;
|
||||
private static final long FACECLAW_WAKE_CONTROL_WAIT_MS = 1_500;
|
||||
private static final long CFW_CLEANUP_WAIT_MS = 4_000;
|
||||
|
||||
private final Context appContext;
|
||||
private final PowerManager powerManager;
|
||||
@@ -102,6 +103,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private int faceclawFramebufferControlGeneration;
|
||||
private int faceclawFramebufferControlSentCount;
|
||||
private int faceclawWakePendingNonce = -1;
|
||||
private boolean cfwCleanupSupported;
|
||||
private boolean cfwCleanupDelivered;
|
||||
private int lastCfwCleanupAckMagic;
|
||||
|
||||
private long reconnectAfterMs;
|
||||
private long ringReconnectAfterMs;
|
||||
@@ -174,6 +178,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private int desiredHeight;
|
||||
private int desiredPaintMs;
|
||||
private int desiredFrameId;
|
||||
// Screen-space deferred draws (glyphs + images) whose pixels are baked
|
||||
// into desiredPacked; the texture-cache planner may replay them as
|
||||
// on-glasses cached draws.
|
||||
private SurfaceCompositor.ScreenDraw[] desiredDraws = new SurfaceCompositor.ScreenDraw[0];
|
||||
// (frame, reason) of the last "waiting to send" line, so a frame that
|
||||
// stalls for seconds records one line per state change (see
|
||||
// noteImageStallLocked). Send-loop thread only.
|
||||
private int stallFrameId;
|
||||
private String stallReason = "";
|
||||
// Highest compositor sequence stored as the desired frame; composites that
|
||||
// lost a store race to a newer one are discarded (their content is already
|
||||
// included in the newer composite).
|
||||
@@ -181,6 +194,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
|
||||
private final SurfaceCompositor compositor = new SurfaceCompositor();
|
||||
|
||||
// Phone-side model of the CFW's 64 KiB texture cache (modes 12/13/14).
|
||||
// Reset whenever the image pipeline / EvenHub session is torn down: the
|
||||
// firmware frees the cache with the fb lease, and after any resync the
|
||||
// cheap safe assumption is an empty cache (glyphs re-upload lazily).
|
||||
private final TextureCacheState textureCache = new TextureCacheState();
|
||||
private boolean textureCacheSupported;
|
||||
private boolean textureImagesSupported;
|
||||
private boolean fwTextSupported;
|
||||
|
||||
private final ArrayDeque<OutboundMessage> pendingMessages = new ArrayDeque<>();
|
||||
private final ArrayDeque<OutboundMessage> inFlightMessages = new ArrayDeque<>();
|
||||
private OutboundMessage prewrittenMessage;
|
||||
@@ -226,7 +248,13 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
releaseFaceclawFramebufferLease();
|
||||
/* On the normal path DashboardController already sent mode 11 after
|
||||
* quiescing its producers. Also cover direct/early close callers here;
|
||||
* a successful cleanup must remain the final BLE message. Older CFWs
|
||||
* fall back to the standalone framebuffer-lease release. */
|
||||
if (!cfwCleanupDelivered && !sendCfwCleanup()) {
|
||||
releaseFaceclawFramebufferLease();
|
||||
}
|
||||
Thread threadToJoin;
|
||||
synchronized (lock) {
|
||||
userDisconnectRequested = true;
|
||||
@@ -623,10 +651,15 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
* frame landed while briefly hidden would stay blank until its next repaint.
|
||||
*/
|
||||
public void setSurfaceVisible(String id, boolean visible) {
|
||||
// Its own frame: this recomposite is a real screen update with real
|
||||
// latency, and without one it would show up in other frames' logs only
|
||||
// as an anonymous "superseded by frame#0".
|
||||
int frameId = FrameTimings.getInstance().startFrame(
|
||||
"compositor:visible " + id + "=" + visible);
|
||||
compositor.setSurfaceVisible(id, visible);
|
||||
SurfaceCompositor.Composite composite = compositor.composite();
|
||||
byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height);
|
||||
storeDesiredComposite(composite, packed, 0, 0);
|
||||
storeDesiredComposite(composite, packed, 0, frameId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -635,10 +668,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
* unblanking restores the previous screen content without repaints.
|
||||
*/
|
||||
public void setScreenBlanked(boolean blanked) {
|
||||
int frameId = FrameTimings.getInstance().startFrame(
|
||||
"compositor:" + (blanked ? "blank" : "unblank"));
|
||||
compositor.setBlanked(blanked);
|
||||
SurfaceCompositor.Composite composite = compositor.composite();
|
||||
byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height);
|
||||
storeDesiredComposite(composite, packed, 0, 0);
|
||||
storeDesiredComposite(composite, packed, 0, frameId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -674,11 +709,37 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
String contentFingerprint,
|
||||
int paintMs,
|
||||
int frameId
|
||||
) {
|
||||
submitSurfaceFrame(pixels8bpp, surfaceId, rectX, rectY, rectWidth, rectHeight,
|
||||
contentFingerprint, paintMs, frameId, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* As above, with the frame's glyph draws (see SurfaceCompositor's glyph
|
||||
* overload for the buffer format): the surface's full text content as
|
||||
* structured draws, letting the texture-cache planner ship glyphs as
|
||||
* on-glasses cached draws instead of pixels. Null when the submitter has
|
||||
* no glyph metadata; the pixels alone remain fully correct.
|
||||
*/
|
||||
public void submitSurfaceFrame(
|
||||
java.nio.ByteBuffer pixels8bpp,
|
||||
String surfaceId,
|
||||
int rectX,
|
||||
int rectY,
|
||||
int rectWidth,
|
||||
int rectHeight,
|
||||
String contentFingerprint,
|
||||
int paintMs,
|
||||
int frameId,
|
||||
java.nio.ByteBuffer glyphs
|
||||
) {
|
||||
Log.i(TAG, "Received an updated frame for surface " + surfaceId);
|
||||
FrameTimings.getInstance().log(frameId, "surface " + surfaceId + " updated rect="
|
||||
+ rectWidth + "x" + rectHeight + "+" + rectX + "+" + rectY
|
||||
+ (glyphs == null ? " (no glyph draws)" : ""));
|
||||
FrameTimings.getInstance().spanStart(frameId, "composite");
|
||||
SurfaceCompositor.Composite composite = compositor.applyAndComposite(
|
||||
surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint);
|
||||
surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint, 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 on
|
||||
@@ -707,6 +768,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
desiredFingerprint = composite.fingerprint;
|
||||
desiredPaintMs = paintMs;
|
||||
desiredFrameId = frameId;
|
||||
desiredDraws = composite.draws;
|
||||
}
|
||||
}
|
||||
if (stale) {
|
||||
@@ -760,12 +822,110 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
return sendShutdownInternal(exitMode, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send CFW image-handler mode 11 after quiescing normal traffic. A successful
|
||||
* return means the cleanup was ACKed and no later Faceclaw message should be
|
||||
* emitted before closing BLE. Unsupported/older CFWs return false so callers
|
||||
* can use the legacy shutdown-and-lease-release path.
|
||||
*/
|
||||
public boolean sendCfwCleanup() {
|
||||
int magic;
|
||||
synchronized (lock) {
|
||||
if (cfwCleanupDelivered) {
|
||||
return true;
|
||||
}
|
||||
if (!cfwCleanupSupported || !running || !sessionReady
|
||||
|| shutdownRequested || !fixedLayoutCreated || !warmedUp) {
|
||||
logLine("skip CFW cleanup; mode 11 unavailable or image path not ready");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Stop auto-renewals, heartbeats, image generation, and control
|
||||
* retries, then discard everything that has not reached BLE yet. */
|
||||
shutdownRequested = true;
|
||||
lastCfwCleanupAckMagic = 0;
|
||||
clearPendingMessagesLocked("CFW cleanup requested");
|
||||
logLine("quiescing transport for CFW cleanup");
|
||||
}
|
||||
interruptibleSleep.interrupt();
|
||||
|
||||
/* WINDOW_SIZE can exceed one, so merely appending cleanup would allow it
|
||||
* to overlap previously-written image fragments. Wait until all of those
|
||||
* ACK or time out; shutdownRequested prevents the drive loop from adding
|
||||
* any fresh automatic traffic meanwhile. */
|
||||
long drainDeadline = SystemClock.elapsedRealtime() + CFW_CLEANUP_WAIT_MS;
|
||||
synchronized (lock) {
|
||||
while (running && sessionReady && !inFlightMessages.isEmpty()) {
|
||||
long remaining = drainDeadline - SystemClock.elapsedRealtime();
|
||||
if (remaining <= 0) break;
|
||||
try {
|
||||
lock.wait(Math.min(remaining, 100));
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!running || !sessionReady || !inFlightMessages.isEmpty()) {
|
||||
shutdownRequested = false;
|
||||
logLine("CFW cleanup could not drain prior traffic");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* External producers are expected to be stopped by the caller, but
|
||||
* clear once more at the barrier so cleanup is definitely last. */
|
||||
clearPendingMessagesLocked("CFW cleanup barrier");
|
||||
OutboundMessage message = messageBuilder.cfwCleanup(
|
||||
DASHBOARD_TILE,
|
||||
nextMapSessionId(),
|
||||
connectionOptions.sendImagesToLeft
|
||||
);
|
||||
magic = message.magic;
|
||||
message.onAck = () -> {
|
||||
lastCfwCleanupAckMagic = message.magic;
|
||||
cfwCleanupDelivered = true;
|
||||
compassMaybeOn = false;
|
||||
faceclawWakeLeaseEnabled = false;
|
||||
logLine("CFW cleanup completed");
|
||||
};
|
||||
message.onTimeout = () -> logLine("CFW cleanup ack timeout");
|
||||
pendingMessages.addLast(message);
|
||||
logLine("queue CFW cleanup");
|
||||
}
|
||||
interruptibleSleep.interrupt();
|
||||
|
||||
long deadline = SystemClock.elapsedRealtime() + CFW_CLEANUP_WAIT_MS;
|
||||
synchronized (lock) {
|
||||
while (running
|
||||
&& sessionReady
|
||||
&& lastCfwCleanupAckMagic != magic
|
||||
&& hasPendingOrInflightMagicLocked(magic)) {
|
||||
long remaining = deadline - SystemClock.elapsedRealtime();
|
||||
if (remaining <= 0) break;
|
||||
try {
|
||||
lock.wait(Math.min(remaining, 100));
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
}
|
||||
boolean acked = lastCfwCleanupAckMagic == magic;
|
||||
if (!acked) shutdownRequested = false;
|
||||
return acked;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* End the EvenHub page while retaining both arm GATT connections and all
|
||||
* notification subscriptions. A missed ACK is intentionally non-fatal:
|
||||
* reconnecting Bluetooth here would defeat the power-saving mode.
|
||||
*/
|
||||
public boolean suspendEvenHubSession() {
|
||||
// Ending the plugin task releases the fb lease, which frees the
|
||||
// on-glasses texture cache; forget it phone-side either way (a lost
|
||||
// ack may still have taken effect).
|
||||
synchronized (lock) {
|
||||
textureCache.reset();
|
||||
}
|
||||
if (sendShutdownInternal(0, false)) {
|
||||
return true;
|
||||
}
|
||||
@@ -1253,6 +1413,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
lastAudioControlAckMagic = 0;
|
||||
audioCaptureActive = false;
|
||||
faceclawWakePendingNonce = -1;
|
||||
cfwCleanupDelivered = false;
|
||||
lastCfwCleanupAckMagic = 0;
|
||||
lastFaceclawWakeLeaseQueuedAtMs = 0;
|
||||
lastFaceclawFramebufferLeaseQueuedAtMs = 0;
|
||||
enqueueFaceclawFramebufferControlLocked(
|
||||
@@ -1456,6 +1618,13 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
maybeFinishNoChangeDesiredFrame();
|
||||
|
||||
synchronized (lock) {
|
||||
if (cfwCleanupDelivered) {
|
||||
/* Successful mode 11 must be the last Faceclaw write. Drop
|
||||
* anything a late external producer attempted to enqueue
|
||||
* while DashboardController was closing the transport. */
|
||||
clearPendingMessagesLocked("after CFW cleanup");
|
||||
return 250;
|
||||
}
|
||||
if (sessionReady
|
||||
&& now - lastFaceclawFramebufferLeaseQueuedAtMs >= FACECLAW_WAKE_LEASE_RENEW_MS
|
||||
&& !hasPendingOrInflightKindLocked("framebuffer-lease-control")) {
|
||||
@@ -1548,10 +1717,19 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
// A frame ready to send right now: don't inject a fresh
|
||||
// heartbeat in front of it (the image's own ack resets the
|
||||
// firmware heartbeat timer, so the heartbeat is redundant).
|
||||
// "Ready" covers both a fresh composite still to be planned
|
||||
// and a planned image already queued but not yet written --
|
||||
// enqueueing sets lastEnqueuedFingerprint, so testing only
|
||||
// the desired fingerprint left the queued-but-unwritten
|
||||
// window unprotected and a heartbeat that came due there
|
||||
// cost the frame a full ack round trip (measured 124ms on
|
||||
// frame#232 of the 2026-08-20 02:27 capture).
|
||||
boolean imageWaiting = !shutdownRequested && fixedLayoutCreated && warmedUp
|
||||
&& !hasPendingOrInflightKindLocked("heartbeat")
|
||||
&& now >= imageRetryAfterMs
|
||||
&& !getDesiredFingerprint().equals(lastEnqueuedFingerprint);
|
||||
&& (hasPendingImageLocked()
|
||||
|| !getDesiredFingerprint().equals(lastEnqueuedFingerprint));
|
||||
noteImageStallLocked(now, windowHasRoom);
|
||||
if (messageToPrewrite == null && handleHeartbeat(imageWaiting)) {
|
||||
return ConnectionOptions.IDLE_SLEEP_MS;
|
||||
}
|
||||
@@ -2044,12 +2222,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
int height;
|
||||
int paintMs;
|
||||
int frameId;
|
||||
SurfaceCompositor.ScreenDraw[] draws;
|
||||
synchronized (desiredTilesLock) {
|
||||
packed = desiredPacked;
|
||||
width = desiredWidth;
|
||||
height = desiredHeight;
|
||||
paintMs = desiredPaintMs;
|
||||
frameId = desiredFrameId;
|
||||
draws = desiredDraws;
|
||||
desiredFrameId = 0;
|
||||
}
|
||||
if (packed == null) {
|
||||
@@ -2062,6 +2242,48 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
return;
|
||||
}
|
||||
|
||||
// Texture-cache path: ship text as cached-glyph draws, punching their
|
||||
// ink out of the baked deltas. Uploads (mode 12) ride ahead of the
|
||||
// image message on the ordered transport. Falls through to the plain
|
||||
// paths whenever the planner has nothing to draw.
|
||||
if (textureCacheSupported && connectionOptions.TEXTURE_CACHE_FRAMES
|
||||
&& draws != null && draws.length > 0 && packed.length > 0) {
|
||||
byte[] deltaBase = (connectionOptions.INCREMENTAL_FRAMES && lastEnqueuedPacked.length > 0
|
||||
&& lastEnqueuedWidth == width && lastEnqueuedHeight == height)
|
||||
? lastEnqueuedPacked : null;
|
||||
FrameTimings.getInstance().spanStart(frameId, "texture-plan");
|
||||
TexturePlanner.Result tex = TexturePlanner.plan(
|
||||
deltaBase, packed, width, height, draws, textureCache,
|
||||
nextImageFrameId,
|
||||
connectionOptions.MULTI_RECT_FRAMES, ConnectionOptions.MULTI_RECT_MAX_RECTS,
|
||||
textureImagesSupported, fwTextSupported);
|
||||
if (tex != null) {
|
||||
nextImageFrameId = tex.nextFid;
|
||||
for (byte[] upload : tex.uploads) {
|
||||
enqueueTextureUploadLocked(upload);
|
||||
}
|
||||
BleImageOptimizer.TileImagePlan plan = new BleImageOptimizer.TileImagePlan(
|
||||
0, DASHBOARD_TILE, packed, width, height, nextMapSessionId(), tex.payload);
|
||||
plan.fragments = BleImageOptimizer.planImageFragments(plan.payload, ConnectionOptions.IMAGE_FRAGMENT_SIZE);
|
||||
FrameTimings.getInstance().spanEnd(frameId, "texture-plan");
|
||||
String texLog = "texture update " + (tex.fullFrame ? "full" : ("rects=" + tex.rectCount))
|
||||
+ " glyphs=" + tex.drawnGlyphs + " runs=" + tex.runCount
|
||||
+ " images=" + tex.drawnImages
|
||||
+ " fw=" + tex.fwGlyphs + "/" + tex.fwRuns
|
||||
+ " baked=" + tex.bakedCandidates
|
||||
+ (tex.uploadBytes > 0 ? " upload=" + tex.uploadBytes + "B" : "")
|
||||
+ " cache=" + textureCache.usedBytes() + "B"
|
||||
+ " payload=" + tex.payload.length + "B"
|
||||
+ " (rects " + tex.rectsMs + "ms, match " + tex.matchMs
|
||||
+ "ms, cache " + tex.cacheMs + "ms, punch " + tex.punchMs
|
||||
+ "ms, encode " + tex.encodeMs + "ms)";
|
||||
FrameTimings.getInstance().log(frameId, texLog);
|
||||
finishEnqueueDesiredImageLocked(plan, fingerprint, paintMs, frameId);
|
||||
return;
|
||||
}
|
||||
FrameTimings.getInstance().spanEnd(frameId, "texture-plan");
|
||||
}
|
||||
|
||||
FrameTimings.getInstance().spanStart(frameId, "compress-and-plan");
|
||||
// Incremental (mode 3 bounding box) update against the last ENQUEUED frame
|
||||
// (the base the firmware shadow will hold when this update is applied).
|
||||
@@ -2110,7 +2332,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (incrementalLog != null) {
|
||||
FrameTimings.getInstance().log(frameId, incrementalLog);
|
||||
}
|
||||
finishEnqueueDesiredImageLocked(plan, fingerprint, paintMs, frameId);
|
||||
}
|
||||
|
||||
/** Shared tail of enqueueDesiredImageLocked: enqueue fragments, advance the delta base, log. */
|
||||
private void finishEnqueueDesiredImageLocked(
|
||||
BleImageOptimizer.TileImagePlan plan, String fingerprint, int paintMs, int frameId) {
|
||||
int updateId = nextImageUpdateId++;
|
||||
int messageCount = plan.fragments.size();
|
||||
imageUpdateStats.put(updateId, new BleImageOptimizer.ImageUpdateStats(paintMs, 1, frameId));
|
||||
@@ -2133,6 +2360,29 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
+ " messages=" + messageCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue one mode-12 texture-cache upload ahead of the image message that
|
||||
* references its glyphs (the transport is FIFO, so no ack round trip is
|
||||
* needed before use). A timeout means the on-glasses cache state is
|
||||
* unknown; forget everything phone-side (glyphs re-upload lazily) and let
|
||||
* the accompanying image update's own timeout drive the frame resync.
|
||||
*/
|
||||
private void enqueueTextureUploadLocked(byte[] payload) {
|
||||
OutboundMessage message = messageBuilder.imagePayload(
|
||||
"texcache",
|
||||
DASHBOARD_TILE,
|
||||
nextMapSessionId(),
|
||||
payload,
|
||||
"texture upload " + payload.length + "B",
|
||||
connectionOptions.sendImagesToLeft);
|
||||
message.onTimeout = () -> {
|
||||
textureCache.reset();
|
||||
logLine("texture upload ack timeout; texture cache state reset");
|
||||
};
|
||||
pendingMessages.addLast(message);
|
||||
logLine("queue " + message.label);
|
||||
}
|
||||
|
||||
private void enqueueImageFragmentLocked(
|
||||
BleImageOptimizer.TileImagePlan plan,
|
||||
BleProtocol.ImageFragment fragment,
|
||||
@@ -2221,6 +2471,11 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
BleProtocol.FirmwareInfo firmwareInfo = BleProtocol.parseSettingsFirmwareInfo(message.ackPayload);
|
||||
if (firmwareInfo != null) {
|
||||
cfwCleanupSupported = hasCapability(firmwareInfo.capabilities, "cleanup11");
|
||||
textureCacheSupported = hasCapability(firmwareInfo.capabilities, "texcache12")
|
||||
&& hasCapability(firmwareInfo.capabilities, "texstr14");
|
||||
textureImagesSupported = hasCapability(firmwareInfo.capabilities, "teximg13");
|
||||
fwTextSupported = hasCapability(firmwareInfo.capabilities, "font15");
|
||||
emitFirmwareInfo(firmwareInfo);
|
||||
}
|
||||
};
|
||||
@@ -2283,6 +2538,117 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
: "Glasses charging.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Record, into the frame that is waiting, why it did not go out on this
|
||||
* pass of the send loop. Without this the export shows a bare multi-second
|
||||
* jump between "image submitted as desired frame" and the first BLE
|
||||
* packet, with no hint whether we were blocked on warmup, a heartbeat, the
|
||||
* BLE window, or another message queued ahead. Deduped on (frame, reason),
|
||||
* so a frame stalled for seconds gets one line per state change rather
|
||||
* than one per loop pass.
|
||||
*/
|
||||
private void noteImageStallLocked(long now, boolean windowHasRoom) {
|
||||
int frameId;
|
||||
synchronized (desiredTilesLock) {
|
||||
frameId = desiredFrameId;
|
||||
}
|
||||
String reason;
|
||||
if (frameId != 0 && !getDesiredFingerprint().equals(lastEnqueuedFingerprint)) {
|
||||
reason = describeEnqueueBlockerLocked(now, windowHasRoom);
|
||||
} else {
|
||||
// Nothing waiting to be planned; an already-planned image may still
|
||||
// be queued behind other traffic.
|
||||
OutboundMessage queuedImage = firstPendingImageLocked();
|
||||
frameId = queuedImage == null ? 0 : imageUpdateFrameIdLocked(queuedImage.imageUpdateId);
|
||||
reason = queuedImage == null ? null : describeWriteBlockerLocked(now, windowHasRoom, queuedImage);
|
||||
}
|
||||
if (frameId == 0 || reason == null) {
|
||||
stallFrameId = 0;
|
||||
stallReason = "";
|
||||
return;
|
||||
}
|
||||
if (frameId == stallFrameId && reason.equals(stallReason)) {
|
||||
return;
|
||||
}
|
||||
stallFrameId = frameId;
|
||||
stallReason = reason;
|
||||
FrameTimings.getInstance().log(frameId, "waiting to send: " + reason);
|
||||
}
|
||||
|
||||
/** Why the desired composite has not been turned into wire messages yet, or null. */
|
||||
private String describeEnqueueBlockerLocked(long now, boolean windowHasRoom) {
|
||||
if (shutdownRequested) {
|
||||
return "shutdown requested";
|
||||
}
|
||||
if (!sessionReady) {
|
||||
return "BLE session not ready";
|
||||
}
|
||||
if (!fixedLayoutCreated) {
|
||||
return "display layout not created yet";
|
||||
}
|
||||
if (!warmedUp) {
|
||||
return "waiting for the warmup frame";
|
||||
}
|
||||
if (now < imageRetryAfterMs) {
|
||||
return "image retry backoff (" + (imageRetryAfterMs - now) + "ms left)";
|
||||
}
|
||||
if (hasPendingImageLocked()) {
|
||||
return "an earlier image is still queued";
|
||||
}
|
||||
if (!windowHasRoom) {
|
||||
return "BLE window full (" + inFlightMessages.size() + " message(s) in flight)";
|
||||
}
|
||||
if (hasPendingOrInflightKindLocked("heartbeat")) {
|
||||
return "heartbeat in flight";
|
||||
}
|
||||
if (!pendingMessages.isEmpty()) {
|
||||
return pendingMessages.size() + " message(s) queued ahead, next "
|
||||
+ pendingMessages.peekFirst().label;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Why a planned image message has not been written to BLE yet, or null. */
|
||||
private String describeWriteBlockerLocked(long now, boolean windowHasRoom, OutboundMessage queuedImage) {
|
||||
if (!sessionReady) {
|
||||
return "BLE session not ready";
|
||||
}
|
||||
if (!windowHasRoom) {
|
||||
return "BLE window full (" + inFlightMessages.size() + " message(s) in flight)";
|
||||
}
|
||||
if (hasPendingOrInflightKindLocked("heartbeat")) {
|
||||
return "heartbeat in flight";
|
||||
}
|
||||
// handleHeartbeat is a barrier: while one is due it holds back every
|
||||
// other write, so a frame queued at the wrong moment waits a heartbeat
|
||||
// round trip. Reported explicitly because it is otherwise invisible --
|
||||
// heartbeats belong to no frame.
|
||||
long heartbeatElapsedMs = now - lastHeartbeatAckedAtMs;
|
||||
if (warmedUp && fixedLayoutCreated && !shutdownRequested
|
||||
&& heartbeatElapsedMs >= ConnectionOptions.HEARTBEAT_READY_MS) {
|
||||
return "heartbeat due (" + heartbeatElapsedMs + "ms since the last one acked)";
|
||||
}
|
||||
OutboundMessage head = pendingMessages.peekFirst();
|
||||
if (head != null && head != queuedImage) {
|
||||
return "queued behind " + head.label;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private OutboundMessage firstPendingImageLocked() {
|
||||
for (OutboundMessage message : pendingMessages) {
|
||||
if (message.imageUpdateId > 0) {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private int imageUpdateFrameIdLocked(int imageUpdateId) {
|
||||
BleImageOptimizer.ImageUpdateStats stats = imageUpdateStats.get(imageUpdateId);
|
||||
return stats == null ? 0 : stats.frameId;
|
||||
}
|
||||
|
||||
private void finishDesiredFrameLocked(String outcome) {
|
||||
int frameIdToFinish;
|
||||
synchronized (desiredTilesLock) {
|
||||
@@ -2509,6 +2875,10 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
// image is a full keyframe rather than a delta onto a stale base.
|
||||
lastEnqueuedPacked = new byte[0];
|
||||
lastEnqueuedFingerprint = "";
|
||||
// Queued texture uploads (if any) were dropped with the rest, and the
|
||||
// session churn behind a full clear may have freed the on-glasses
|
||||
// cache; forget it phone-side so glyphs re-upload lazily.
|
||||
textureCache.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2539,6 +2909,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
clearInFlightMessagesLocked(reason);
|
||||
lastEnqueuedPacked = new byte[0];
|
||||
lastEnqueuedFingerprint = "";
|
||||
textureCache.reset();
|
||||
}
|
||||
|
||||
/** Any image update whose fragments are still queued (not yet sent). */
|
||||
@@ -2656,6 +3027,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
faceclawWakePendingNonce = -1;
|
||||
lastFaceclawWakeLeaseQueuedAtMs = 0;
|
||||
faceclawWakeControlSentCount = 0;
|
||||
cfwCleanupDelivered = false;
|
||||
lastCfwCleanupAckMagic = 0;
|
||||
wearState = -1;
|
||||
displayedFingerprint = "";
|
||||
// Deliberately not clearing silentMode: it is a property of the glasses,
|
||||
@@ -2663,6 +3036,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
// the very cause of the session teardown that got us here.
|
||||
}
|
||||
|
||||
private static boolean hasCapability(String capabilities, String token) {
|
||||
if (capabilities == null || token == null || token.isEmpty()) return false;
|
||||
for (String capability : capabilities.trim().split("\\s+")) {
|
||||
if (token.equals(capability)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void emitRingEvent(String kind, String containerName, int eventType, int eventSource, int systemExitReasonCode, int frameId) {
|
||||
final FaceclawBleCommunicatorListener current = listener;
|
||||
if (current == null) {
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.webkit.JavascriptInterface;
|
||||
|
||||
/**
|
||||
* The object injected into a hosted EvenHub app's WebView as
|
||||
* window.__faceclawEvenHub. The document-start shim (served inline by
|
||||
* FaceclawEvenHubWebViewClient) wraps it in a promise-returning
|
||||
* window.flutter_inappwebview.callHandler, which is what the EvenHub SDK
|
||||
* actually talks to.
|
||||
*
|
||||
* JavascriptInterface methods run on a WebView-internal thread; everything
|
||||
* is bounced to the main thread before touching the NativeScript listener.
|
||||
*/
|
||||
public class FaceclawEvenHubJsBridge {
|
||||
private final FaceclawEvenHubListener listener;
|
||||
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
public FaceclawEvenHubJsBridge(FaceclawEvenHubListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
public void postMessage(final String handlerName, final String argsJson, final int callId) {
|
||||
mainHandler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
listener.onEvenAppMessage(handlerName, argsJson, callId);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.View;
|
||||
import android.webkit.WebView;
|
||||
|
||||
/**
|
||||
* A WebView for hosting an EvenHub app that keeps rendering and running JS even
|
||||
* when the whole Faceclaw activity is backgrounded (phone on the home screen or
|
||||
* another app).
|
||||
*
|
||||
* The problem: Chromium ties a WebView's page-visibility to its window's
|
||||
* visibility. When the host activity stops, every view gets
|
||||
* onWindowVisibilityChanged(GONE); Chromium then marks the page hidden and
|
||||
* freezes/throttles the renderer. A frozen renderer doesn't execute
|
||||
* evaluateJavascript, so the host timer/rAF ticks AND pushed input events queue
|
||||
* up and only run once the app is foregrounded again — exactly the "Hub apps
|
||||
* freeze in the background" symptom (also seen in early official-app versions).
|
||||
*
|
||||
* The occluded-overlay trick in FaceclawEvenHubWebViewHost keeps the VIEW
|
||||
* visible while the activity is foreground, but can't help once the WINDOW goes
|
||||
* away. So we lie about window visibility: this WebView always tells Chromium
|
||||
* the window is VISIBLE, so the page never goes hidden and the renderer keeps
|
||||
* running in the background. We drive the glasses over BLE from the foreground
|
||||
* service regardless of the phone's screen, so "always rendering" is what we
|
||||
* actually want; there is no on-phone cost while the app is unseen.
|
||||
*/
|
||||
public class FaceclawEvenHubWebView extends WebView {
|
||||
|
||||
public FaceclawEvenHubWebView(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onWindowVisibilityChanged(int visibility) {
|
||||
// Never propagate a hidden window: keep Chromium's page-visibility
|
||||
// "visible" so the renderer isn't frozen when Faceclaw backgrounds.
|
||||
super.onWindowVisibilityChanged(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.webkit.WebResourceRequest;
|
||||
import android.webkit.WebResourceResponse;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Serves an unpacked EvenHub app's files to its WebView from a fake per-app
|
||||
* https origin, entirely offline. HTML responses get the host bridge shim
|
||||
* injected at the top of the document, guaranteeing
|
||||
* window.flutter_inappwebview exists before any app JS runs (apps race
|
||||
* waitForEvenAppBridge against 4-6s timeouts and silently fall back to demo
|
||||
* modes if the handler appears late).
|
||||
*
|
||||
* shouldInterceptRequest runs on a WebView-internal thread; it only touches
|
||||
* files, never the NativeScript runtime. Requests to other hosts fall
|
||||
* through to the network (the manifest whitelist is not yet enforced).
|
||||
*/
|
||||
public class FaceclawEvenHubWebViewClient extends WebViewClient {
|
||||
private static final String TAG = "FaceclawEvenHub";
|
||||
|
||||
private final String rootDir;
|
||||
private final String host;
|
||||
private final String injectScript;
|
||||
private final FaceclawEvenHubListener listener;
|
||||
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
public FaceclawEvenHubWebViewClient(
|
||||
String rootDir, String host, String injectScript, FaceclawEvenHubListener listener) {
|
||||
this.rootDir = rootDir;
|
||||
this.host = host;
|
||||
this.injectScript = injectScript;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
|
||||
Uri url = request.getUrl();
|
||||
if (url == null || !host.equals(url.getHost())) {
|
||||
return null; // External hosts: normal network handling.
|
||||
}
|
||||
String path = url.getPath();
|
||||
if (path == null || path.isEmpty() || path.equals("/")) {
|
||||
path = "/index.html";
|
||||
}
|
||||
try {
|
||||
File root = new File(rootDir).getCanonicalFile();
|
||||
File target = new File(root, path.substring(1)).getCanonicalFile();
|
||||
if (!target.getPath().startsWith(root.getPath()) || !target.isFile()) {
|
||||
Log.w(TAG, "404 " + path);
|
||||
return new WebResourceResponse(
|
||||
"text/plain", "utf-8",
|
||||
new ByteArrayInputStream("not found".getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
String mime = mimeTypeFor(target.getName());
|
||||
if (mime.equals("text/html")) {
|
||||
return new WebResourceResponse(mime, "utf-8", new ByteArrayInputStream(injectIntoHtml(target)));
|
||||
}
|
||||
InputStream stream = new FileInputStream(target);
|
||||
return new WebResourceResponse(mime, null, stream);
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "serve failed for " + path, e);
|
||||
return new WebResourceResponse(
|
||||
"text/plain", "utf-8",
|
||||
new ByteArrayInputStream("error".getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageFinished(WebView view, final String url) {
|
||||
mainHandler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
listener.onPageFinished(url);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private byte[] injectIntoHtml(File file) throws IOException {
|
||||
byte[] raw = readAll(file);
|
||||
String html = new String(raw, StandardCharsets.UTF_8);
|
||||
String tag = "<script>" + injectScript + "</script>";
|
||||
// After <head...> if present, else before everything.
|
||||
int headIndex = indexOfIgnoreCase(html, "<head");
|
||||
if (headIndex >= 0) {
|
||||
int close = html.indexOf('>', headIndex);
|
||||
if (close >= 0) {
|
||||
return (html.substring(0, close + 1) + tag + html.substring(close + 1))
|
||||
.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
return (tag + html).getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static int indexOfIgnoreCase(String haystack, String needle) {
|
||||
return haystack.toLowerCase(Locale.ROOT).indexOf(needle);
|
||||
}
|
||||
|
||||
private static byte[] readAll(File file) throws IOException {
|
||||
FileInputStream in = new FileInputStream(file);
|
||||
try {
|
||||
byte[] data = new byte[(int) file.length()];
|
||||
int off = 0;
|
||||
while (off < data.length) {
|
||||
int n = in.read(data, off, data.length - off);
|
||||
if (n < 0) throw new IOException("short read: " + file);
|
||||
off += n;
|
||||
}
|
||||
return data;
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static String mimeTypeFor(String name) {
|
||||
String lower = name.toLowerCase(Locale.ROOT);
|
||||
if (lower.endsWith(".html") || lower.endsWith(".htm")) return "text/html";
|
||||
if (lower.endsWith(".js") || lower.endsWith(".mjs")) return "application/javascript";
|
||||
if (lower.endsWith(".css")) return "text/css";
|
||||
if (lower.endsWith(".json")) return "application/json";
|
||||
if (lower.endsWith(".png")) return "image/png";
|
||||
if (lower.endsWith(".jpg") || lower.endsWith(".jpeg")) return "image/jpeg";
|
||||
if (lower.endsWith(".gif")) return "image/gif";
|
||||
if (lower.endsWith(".svg")) return "image/svg+xml";
|
||||
if (lower.endsWith(".webp")) return "image/webp";
|
||||
if (lower.endsWith(".ico")) return "image/x-icon";
|
||||
if (lower.endsWith(".wasm")) return "application/wasm";
|
||||
if (lower.endsWith(".woff")) return "font/woff";
|
||||
if (lower.endsWith(".woff2")) return "font/woff2";
|
||||
if (lower.endsWith(".ttf")) return "font/ttf";
|
||||
if (lower.endsWith(".txt") || lower.endsWith(".map")) return "text/plain";
|
||||
return "application/octet-stream";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.WebView;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Keeps EvenHub app WebViews alive and rendering while the phone shows the
|
||||
* Faceclaw dashboard instead of the app.
|
||||
*
|
||||
* The problem: an EvenHub app only paints (canvas / requestAnimationFrame) and
|
||||
* runs un-throttled timers while its WebView is attached to the window and
|
||||
* VISIBLE. Faceclaw is glasses-first — the phone normally shows its own UI, not
|
||||
* the app — so the app's WebView must render off-screen.
|
||||
*
|
||||
* The trick: a single full-screen overlay FrameLayout is inserted as the FIRST
|
||||
* child of the activity's content view, i.e. BEHIND the NativeScript UI. Every
|
||||
* app WebView lives in it, full-size and VISIBLE, but occluded by the opaque
|
||||
* dashboard on top. Chromium doesn't stop rendering a view merely because a
|
||||
* sibling covers it (only VISIBILITY flags / detachment / zero-size do that),
|
||||
* so the apps keep driving their glasses windows while unseen. Touches go to
|
||||
* the NativeScript UI on top.
|
||||
*
|
||||
* To show one app's UI on the phone, the overlay is raised to the front (and
|
||||
* the chosen WebView to the top of the overlay); hiding sends it back behind.
|
||||
* All view work happens on the main thread (callers are on the NS/JS thread).
|
||||
*
|
||||
* Timer keep-alive: when the phone screen turns off, Chromium heavily throttles
|
||||
* the page's own setTimeout/setInterval (intensive background throttling clamps
|
||||
* them to ~1/sec), so timer-driven apps (e.g. snake's game loop) slow to a
|
||||
* crawl even though the renderer is still alive. Host-initiated
|
||||
* evaluateJavascript is NOT subject to that throttle, so we drive the app's
|
||||
* timers ourselves: a document-start shim replaces setTimeout/setInterval (and
|
||||
* requestAnimationFrame) with JS queues fired by window.__fcTimerTick() /
|
||||
* __fcRafTick(), and this host ticks those on the main thread at a fixed rate
|
||||
* regardless of screen state. The main Looper keeps running under the
|
||||
* foreground service, so the tick survives the screen turning off. (Matches the
|
||||
* official Even app's approach.)
|
||||
*
|
||||
* Background keep-alive: the occluded-overlay trick only works while the
|
||||
* activity is foreground. When Faceclaw itself is backgrounded, the window goes
|
||||
* invisible and Chromium freezes the renderer — the timer/rAF ticks and pushed
|
||||
* input events then queue up and only run once foregrounded again. Two things
|
||||
* prevent that: the WebViews are {@link FaceclawEvenHubWebView}, which lies to
|
||||
* Chromium about window visibility so the page never goes hidden; and each is
|
||||
* pinned to IMPORTANT renderer priority (not waived when not visible) with
|
||||
* timers/onResume asserted on attach. The main Looper keeps ticking under the
|
||||
* foreground service, so apps keep driving their glasses windows in the
|
||||
* background.
|
||||
*/
|
||||
public class FaceclawEvenHubWebViewHost {
|
||||
private static FaceclawEvenHubWebViewHost instance;
|
||||
|
||||
/** How often the host fires the JS timer queue. 60Hz keeps games smooth. */
|
||||
private static final long TICK_MS = 16;
|
||||
|
||||
private FrameLayout overlay;
|
||||
private boolean shown = false;
|
||||
|
||||
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
private final List<WebView> webViews = new ArrayList<>();
|
||||
private boolean ticking = false;
|
||||
private final Runnable ticker = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (int i = 0; i < webViews.size(); i++) {
|
||||
webViews.get(i).evaluateJavascript(
|
||||
"window.__fcTimerTick&&__fcTimerTick();window.__fcRafTick&&__fcRafTick()", null);
|
||||
}
|
||||
if (ticking) mainHandler.postDelayed(this, TICK_MS);
|
||||
}
|
||||
};
|
||||
|
||||
public static synchronized FaceclawEvenHubWebViewHost getInstance() {
|
||||
if (instance == null) instance = new FaceclawEvenHubWebViewHost();
|
||||
return instance;
|
||||
}
|
||||
|
||||
private FrameLayout ensureOverlay(Activity activity) {
|
||||
if (overlay != null) return overlay;
|
||||
ViewGroup content = activity.findViewById(android.R.id.content);
|
||||
overlay = new FrameLayout(activity);
|
||||
// index 0 = behind the NativeScript content view.
|
||||
content.addView(overlay, 0, new FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
||||
return overlay;
|
||||
}
|
||||
|
||||
/** Add a WebView to the host, full-size and rendering, hidden behind the UI. */
|
||||
public void attach(Activity activity, WebView web) {
|
||||
FrameLayout o = ensureOverlay(activity);
|
||||
web.setVisibility(View.VISIBLE);
|
||||
if (web.getParent() == null) {
|
||||
o.addView(web, new FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
||||
}
|
||||
// resumeTimers() is process-global; make sure nothing left timers paused.
|
||||
// onResume() undoes any per-WebView pause. Together with
|
||||
// FaceclawEvenHubWebView faking window visibility, this keeps the page
|
||||
// running JS while Faceclaw is backgrounded.
|
||||
web.resumeTimers();
|
||||
web.onResume();
|
||||
// Keep the renderer process at IMPORTANT priority even when the WebView
|
||||
// isn't visible (waivedWhenNotVisible=false), so Android doesn't
|
||||
// deprioritize/kill it in the background.
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
web.setRendererPriorityPolicy(WebView.RENDERER_PRIORITY_IMPORTANT, false);
|
||||
}
|
||||
if (!webViews.contains(web)) webViews.add(web);
|
||||
if (!ticking) {
|
||||
ticking = true;
|
||||
mainHandler.postDelayed(ticker, TICK_MS);
|
||||
}
|
||||
}
|
||||
|
||||
/** Bring an app's WebView to the front so it is visible on the phone. */
|
||||
public void showOnPhone(WebView web) {
|
||||
if (overlay == null) return;
|
||||
web.bringToFront();
|
||||
overlay.bringToFront();
|
||||
shown = true;
|
||||
}
|
||||
|
||||
/** Send the overlay back behind the NativeScript UI. */
|
||||
public void hideOnPhone() {
|
||||
shown = false;
|
||||
if (overlay == null) return;
|
||||
ViewGroup content = (ViewGroup) overlay.getParent();
|
||||
if (content != null) {
|
||||
content.removeView(overlay);
|
||||
content.addView(overlay, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isShown() {
|
||||
return shown;
|
||||
}
|
||||
|
||||
/** Remove and destroy a WebView (its app is closing). */
|
||||
public void detach(WebView web) {
|
||||
webViews.remove(web);
|
||||
if (webViews.isEmpty() && ticking) {
|
||||
ticking = false;
|
||||
mainHandler.removeCallbacks(ticker);
|
||||
}
|
||||
if (overlay != null) overlay.removeView(web);
|
||||
web.destroy();
|
||||
}
|
||||
}
|
||||
@@ -67,8 +67,10 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener {
|
||||
// END ack statuses that mean "component accepted": SUCCESS, UPDATING, SYS_RESTART.
|
||||
private static final int[] END_OK = new int[] {0, 8, 9};
|
||||
|
||||
// Firmware layout expectations (mirrors g2flash.py).
|
||||
private static final int EXPECTED_SEGMENTS = 5;
|
||||
// Firmware layout expectations (mirrors g2flash.py). Firmware 2.2.4 has 5
|
||||
// components; 2.2.6 has 6.
|
||||
private static final int MIN_SEGMENTS = 5;
|
||||
private static final int MAX_SEGMENTS = 6;
|
||||
private static final String REQUIRED_SEGMENT = "ota/s200_firmware_ota.bin";
|
||||
private static final long APP_LOAD_ADDR = 0x00438000L;
|
||||
private static final long APP_MAX_END = 0x007F0000L;
|
||||
@@ -444,8 +446,9 @@ public class FaceclawFirmwareFlasher implements FaceclawBleListener {
|
||||
|
||||
private List<Segment> validate(byte[] img) {
|
||||
List<Segment> segs = parseSegments(img);
|
||||
if (segs.size() != EXPECTED_SEGMENTS) {
|
||||
throw new IllegalStateException("expected " + EXPECTED_SEGMENTS + " components, found " + segs.size());
|
||||
if (segs.size() < MIN_SEGMENTS || segs.size() > MAX_SEGMENTS) {
|
||||
throw new IllegalStateException(
|
||||
"expected " + MIN_SEGMENTS + "-" + MAX_SEGMENTS + " components, found " + segs.size());
|
||||
}
|
||||
Segment main = null;
|
||||
for (Segment s : segs) {
|
||||
|
||||
@@ -175,6 +175,136 @@ public final class FontFileRenderer {
|
||||
}
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
||||||| d7d12af
|
||||
=======
|
||||
* Render one codepoint as a tight-ink-box antialiased glyph cell for the
|
||||
* texture-cache text pipeline (see app/graphics/ttf-font.ts). Ligatures
|
||||
* are disabled so a per-codepoint raster plus pairwise kerning (measured
|
||||
* via measureTextExact) fully describes a line. Little-endian packet:
|
||||
* [advance u16, 26.6 fixed point]
|
||||
* [bearingX s16] ink left relative to the pen position
|
||||
* [inkTop s16] ink top relative to the line top (ascent above baseline)
|
||||
* [width u16][height u16]
|
||||
* [width*height coverage bytes, gamma-mapped]
|
||||
* An ink-free glyph (space) has width = height = 0 but a valid advance.
|
||||
* Empty array when the font cannot be loaded or the render fails.
|
||||
*/
|
||||
public static byte[] renderGlyphCell(String path, float sizePx, int codePoint, double gamma) {
|
||||
Typeface typeface = loadTypeface(path);
|
||||
if (typeface == null || sizePx <= 0 || codePoint < 0 || codePoint > 0x10ffff) {
|
||||
return new byte[0];
|
||||
}
|
||||
try {
|
||||
Paint paint = glyphPaint(typeface, sizePx);
|
||||
String text = new String(Character.toChars(codePoint));
|
||||
float advance = paint.measureText(text);
|
||||
int advanceFixed = Math.max(0, Math.min(0xffff, Math.round(advance * 64f)));
|
||||
|
||||
android.graphics.Rect bounds = new android.graphics.Rect();
|
||||
paint.getTextBounds(text, 0, text.length(), bounds);
|
||||
if (bounds.isEmpty()) {
|
||||
return glyphCellPacket(advanceFixed, 0, 0, 0, 0, null);
|
||||
}
|
||||
// getTextBounds can be off by a hair on AA edges; render with
|
||||
// padding, then trim to the actually painted box.
|
||||
int pad = 2;
|
||||
int bw = bounds.width() + 2 * pad;
|
||||
int bh = bounds.height() + 2 * pad;
|
||||
if (bw <= 0 || bh <= 0 || bw * bh > 1_000_000) {
|
||||
return new byte[0];
|
||||
}
|
||||
int penX = pad - bounds.left;
|
||||
int baselineY = pad - bounds.top;
|
||||
Bitmap bitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ALPHA_8);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawText(text, penX, baselineY, paint);
|
||||
byte[] pixels = alpha8Pixels(bitmap, bw, bh);
|
||||
bitmap.recycle();
|
||||
|
||||
int x0 = bw, x1 = -1, y0 = bh, y1 = -1;
|
||||
for (int y = 0; y < bh; y++) {
|
||||
int row = y * bw;
|
||||
for (int x = 0; x < bw; x++) {
|
||||
if (pixels[row + x] != 0) {
|
||||
if (x < x0) x0 = x;
|
||||
if (x > x1) x1 = x;
|
||||
if (y < y0) y0 = y;
|
||||
if (y > y1) y1 = y;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (x1 < x0) {
|
||||
return glyphCellPacket(advanceFixed, 0, 0, 0, 0, null);
|
||||
}
|
||||
int width = x1 - x0 + 1;
|
||||
int height = y1 - y0 + 1;
|
||||
Paint.FontMetricsInt fm = paint.getFontMetricsInt();
|
||||
int bearingX = x0 - penX;
|
||||
int inkTop = (-fm.ascent) + (y0 - baselineY);
|
||||
|
||||
byte[] lut = gammaLut(gamma);
|
||||
byte[] coverage = new byte[width * height];
|
||||
for (int y = 0; y < height; y++) {
|
||||
int src = (y0 + y) * bw + x0;
|
||||
int dst = y * width;
|
||||
for (int x = 0; x < width; x++) {
|
||||
coverage[dst + x] = lut[pixels[src + x] & 0xff];
|
||||
}
|
||||
}
|
||||
return glyphCellPacket(advanceFixed, bearingX, inkTop, width, height, coverage);
|
||||
} catch (Exception | OutOfMemoryError e) {
|
||||
Log.w(TAG, "glyph render failed: " + path + " U+" + Integer.toHexString(codePoint), e);
|
||||
return new byte[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exact (float) advance width of a text run, with the same paint setup as
|
||||
* renderGlyphCell (subpixel advances, ligatures off), so pair kerning can
|
||||
* be derived as measure(ab) - measure(a) - measure(b).
|
||||
*/
|
||||
public static double measureTextExact(String path, String text, float sizePx) {
|
||||
Typeface typeface = loadTypeface(path);
|
||||
if (typeface == null || text == null || sizePx <= 0) {
|
||||
return 0;
|
||||
}
|
||||
return glyphPaint(typeface, sizePx).measureText(text);
|
||||
}
|
||||
|
||||
/** Paint used by both the glyph-cell renderer and its measurements. */
|
||||
private static Paint glyphPaint(Typeface typeface, float sizePx) {
|
||||
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
|
||||
paint.setTypeface(typeface);
|
||||
paint.setTextSize(sizePx);
|
||||
paint.setColor(Color.WHITE);
|
||||
// Ligatures merge codepoints into one glyph, which the per-codepoint
|
||||
// cache model cannot represent; kerning still applies.
|
||||
paint.setFontFeatureSettings("'liga' off, 'clig' off");
|
||||
return paint;
|
||||
}
|
||||
|
||||
private static byte[] glyphCellPacket(
|
||||
int advanceFixed, int bearingX, int inkTop, int width, int height, byte[] coverage) {
|
||||
byte[] out = new byte[10 + (coverage == null ? 0 : coverage.length)];
|
||||
out[0] = (byte) (advanceFixed & 0xff);
|
||||
out[1] = (byte) ((advanceFixed >> 8) & 0xff);
|
||||
out[2] = (byte) (bearingX & 0xff);
|
||||
out[3] = (byte) ((bearingX >> 8) & 0xff);
|
||||
out[4] = (byte) (inkTop & 0xff);
|
||||
out[5] = (byte) ((inkTop >> 8) & 0xff);
|
||||
out[6] = (byte) (width & 0xff);
|
||||
out[7] = (byte) ((width >> 8) & 0xff);
|
||||
out[8] = (byte) (height & 0xff);
|
||||
out[9] = (byte) ((height >> 8) & 0xff);
|
||||
if (coverage != null) {
|
||||
System.arraycopy(coverage, 0, out, 10, coverage.length);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
>>>>>>> evenhub-compatibility
|
||||
* Line metrics for a font at a pixel size, as "ascent descent lineGap"
|
||||
* (integers, pixels). Empty string if the font cannot be loaded.
|
||||
*/
|
||||
|
||||
@@ -14,8 +14,7 @@ import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@@ -27,14 +26,21 @@ import java.util.Map;
|
||||
* update has been fully transmitted to the glasses, is discarded unsent, or
|
||||
* times out because nobody reported finishing it.
|
||||
*
|
||||
* All public methods are thread-safe and cheap enough to call from the BLE
|
||||
* worker thread, the Android main thread, and the JS thread. Frame IDs are
|
||||
* positive ints; 0 means "no frame" and is silently ignored everywhere, so
|
||||
* callers do not need to null-check.
|
||||
* Frames form a tree. One input event can fan out into several renders (the
|
||||
* focused app's window plus the shell chrome, say), and each of those is its
|
||||
* own frame linked back to the input frame that caused it via
|
||||
* {@link #startFrame(String, int)}. The export nests descendants under their
|
||||
* root and renders every line against the root's clock, so a single block
|
||||
* shows the whole input-to-pixels story. Latency percentiles are measured over
|
||||
* roots: root start to the first descendant that actually reached the glasses.
|
||||
*
|
||||
* Statistics (p50/p90/p99 of event-to-last-ack for sent frames) and full log
|
||||
* lines for recent and slowest frames are periodically exported to
|
||||
* getExternalFilesDir()/frame-timings.txt, retrievable via
|
||||
* All public methods are thread-safe and cheap enough to call from the BLE
|
||||
* worker thread, the Android main thread, the JS thread, and app worker
|
||||
* threads. Frame IDs are positive ints; 0 means "no frame" and is silently
|
||||
* ignored everywhere, so callers do not need to null-check.
|
||||
*
|
||||
* Statistics and full log lines for recent and slowest frames are periodically
|
||||
* exported to getExternalFilesDir()/frame-timings.txt, retrievable via
|
||||
* adb pull /sdcard/Android/data/<pkg>/files/frame-timings.txt
|
||||
*/
|
||||
public final class FrameTimings {
|
||||
@@ -45,31 +51,66 @@ public final class FrameTimings {
|
||||
private static final long FRAME_TIMEOUT_MS = 30_000;
|
||||
private static final long EXPORT_INTERVAL_MS = 15_000;
|
||||
private static final long SWEEP_INTERVAL_MS = 5_000;
|
||||
private static final int RECENT_FRAMES_KEPT = 50;
|
||||
private static final int SLOWEST_FRAMES_KEPT = 20;
|
||||
private static final int RECENT_ROOTS_KEPT = 40;
|
||||
private static final int SLOWEST_ROOTS_KEPT = 20;
|
||||
private static final int SENT_DURATIONS_WINDOW = 512;
|
||||
private static final int MAX_LINES_PER_FRAME = 200;
|
||||
/**
|
||||
* Frames retained for ID lookup. Well above the number any export can
|
||||
* reference, so a late log/span/finish call always finds its frame; frames
|
||||
* older than this are unreachable and get collected.
|
||||
*/
|
||||
private static final int FRAMES_RETAINED = 1024;
|
||||
private static final String EXPORT_FILE_NAME = "frame-timings.txt";
|
||||
|
||||
public static FrameTimings getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
private static final class Line {
|
||||
final long atMs; // SystemClock.elapsedRealtime()
|
||||
final String thread;
|
||||
final String message;
|
||||
|
||||
Line(long atMs, String thread, String message) {
|
||||
this.atMs = atMs;
|
||||
this.thread = thread;
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Frame {
|
||||
final int id;
|
||||
final String reason;
|
||||
final long startedAtMs; // SystemClock.elapsedRealtime()
|
||||
final long startedWallClockMs;
|
||||
final List<String> lines = new ArrayList<>();
|
||||
final Map<String, Long> openSpans = new HashMap<>();
|
||||
final Frame parent;
|
||||
final List<Frame> children = new ArrayList<>(0);
|
||||
final List<Line> lines = new ArrayList<>();
|
||||
final Map<String, Long> openSpans = new LinkedHashMap<>();
|
||||
String reason;
|
||||
long finishedAtMs;
|
||||
String outcome; // null while open; "sent", "discarded: ...", "timeout: ..."
|
||||
/** Set on the root once some frame in its subtree reached the glasses. */
|
||||
boolean subtreeSentRecorded;
|
||||
|
||||
Frame(int id, String reason, long startedAtMs, long startedWallClockMs) {
|
||||
Frame(int id, String reason, long startedAtMs, long startedWallClockMs, Frame parent) {
|
||||
this.id = id;
|
||||
this.reason = reason;
|
||||
this.startedAtMs = startedAtMs;
|
||||
this.startedWallClockMs = startedWallClockMs;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
Frame root() {
|
||||
Frame frame = this;
|
||||
while (frame.parent != null) {
|
||||
frame = frame.parent;
|
||||
}
|
||||
return frame;
|
||||
}
|
||||
|
||||
boolean isOpen() {
|
||||
return outcome == null;
|
||||
}
|
||||
|
||||
long durationMs() {
|
||||
@@ -79,13 +120,45 @@ public final class FrameTimings {
|
||||
boolean wasSent() {
|
||||
return outcome != null && outcome.startsWith("sent");
|
||||
}
|
||||
|
||||
/** Last moment anything in this subtree happened, for whole-tree duration. */
|
||||
long subtreeEndAtMs(long nowMs) {
|
||||
long end = isOpen() ? nowMs : finishedAtMs;
|
||||
for (Frame child : children) {
|
||||
end = Math.max(end, child.subtreeEndAtMs(nowMs));
|
||||
}
|
||||
return end;
|
||||
}
|
||||
|
||||
boolean subtreeHasOpenFrame() {
|
||||
if (isOpen()) {
|
||||
return true;
|
||||
}
|
||||
for (Frame child : children) {
|
||||
if (child.subtreeHasOpenFrame()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private final Object lock = new Object();
|
||||
private final Map<Integer, Frame> activeFrames = new HashMap<>();
|
||||
private final ArrayDeque<Frame> recentFrames = new ArrayDeque<>();
|
||||
private final List<Frame> slowestSentFrames = new ArrayList<>();
|
||||
private final ArrayDeque<Long> sentDurations = new ArrayDeque<>();
|
||||
/**
|
||||
* Every frame we still retain, keyed by ID: open frames plus enough
|
||||
* history that late calls and the export can still resolve one.
|
||||
*/
|
||||
private final LinkedHashMap<Integer, Frame> framesById =
|
||||
new LinkedHashMap<Integer, Frame>(64, 0.75f, false) {
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<Integer, Frame> eldest) {
|
||||
return size() > FRAMES_RETAINED && !eldest.getValue().isOpen();
|
||||
}
|
||||
};
|
||||
private final ArrayDeque<Frame> recentRoots = new ArrayDeque<>();
|
||||
private final List<Frame> slowestRoots = new ArrayList<>();
|
||||
private final ArrayDeque<Long> inputLatencies = new ArrayDeque<>();
|
||||
private final ArrayDeque<Long> renderLatencies = new ArrayDeque<>();
|
||||
private int nextFrameId = 1;
|
||||
private long framesStarted;
|
||||
private long framesSent;
|
||||
@@ -114,22 +187,56 @@ public final class FrameTimings {
|
||||
}
|
||||
}
|
||||
|
||||
/** Begin a frame; reason is a short label like "input:sys-event:0". Returns the frame ID. */
|
||||
/** Begin a root frame; reason is a short label like "input:sys-event type=0". */
|
||||
public int startFrame(String reason) {
|
||||
return startFrame(reason, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Begin a frame caused by an existing one (parentFrameId; 0 for a root).
|
||||
* The child is nested under its root in the export and its latency counts
|
||||
* against the root, so an input event that fans out into an app render and
|
||||
* a shell-chrome render reads as one timeline instead of three.
|
||||
*/
|
||||
public int startFrame(String reason, int parentFrameId) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
Frame frame;
|
||||
synchronized (lock) {
|
||||
frame = new Frame(nextFrameId++, reason == null ? "" : reason, now, System.currentTimeMillis());
|
||||
activeFrames.put(frame.id, frame);
|
||||
Frame parent = framesById.get(parentFrameId);
|
||||
frame = new Frame(nextFrameId++, reason == null ? "" : reason, now,
|
||||
System.currentTimeMillis(), parent);
|
||||
framesById.put(frame.id, frame);
|
||||
framesStarted++;
|
||||
if (parent != null) {
|
||||
parent.children.add(frame);
|
||||
addLineLocked(parent, now, "spawned frame#" + frame.id + " (" + frame.reason + ")");
|
||||
}
|
||||
}
|
||||
return frame.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append to a frame's label, for facts only known after it started (which
|
||||
* app is in the foreground, which window a render is for). Shows up in the
|
||||
* frame's header line, so the export is scannable without reading bodies.
|
||||
*/
|
||||
public void annotate(int frameId, String text) {
|
||||
if (text == null || text.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
synchronized (lock) {
|
||||
Frame frame = framesById.get(frameId);
|
||||
if (frame == null) {
|
||||
return;
|
||||
}
|
||||
frame.reason = frame.reason.isEmpty() ? text : frame.reason + " " + text;
|
||||
}
|
||||
}
|
||||
|
||||
public void log(int frameId, String message) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
synchronized (lock) {
|
||||
Frame frame = activeFrames.get(frameId);
|
||||
Frame frame = openFrameLocked(frameId);
|
||||
if (frame == null) {
|
||||
return;
|
||||
}
|
||||
@@ -140,7 +247,7 @@ public final class FrameTimings {
|
||||
public void spanStart(int frameId, String name) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
synchronized (lock) {
|
||||
Frame frame = activeFrames.get(frameId);
|
||||
Frame frame = openFrameLocked(frameId);
|
||||
if (frame == null) {
|
||||
return;
|
||||
}
|
||||
@@ -152,7 +259,7 @@ public final class FrameTimings {
|
||||
public void spanEnd(int frameId, String name) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
synchronized (lock) {
|
||||
Frame frame = activeFrames.get(frameId);
|
||||
Frame frame = openFrameLocked(frameId);
|
||||
if (frame == null) {
|
||||
return;
|
||||
}
|
||||
@@ -174,7 +281,7 @@ public final class FrameTimings {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
Frame finished;
|
||||
synchronized (lock) {
|
||||
Frame frame = activeFrames.remove(frameId);
|
||||
Frame frame = openFrameLocked(frameId);
|
||||
if (frame == null) {
|
||||
return;
|
||||
}
|
||||
@@ -188,73 +295,94 @@ public final class FrameTimings {
|
||||
/** One-line stats summary, e.g. for showing in the phone UI. */
|
||||
public String statsSummary() {
|
||||
synchronized (lock) {
|
||||
long[] percentiles = sentPercentilesLocked();
|
||||
long[] input = percentilesLocked(inputLatencies);
|
||||
return "frames started=" + framesStarted + " sent=" + framesSent
|
||||
+ " discarded=" + framesDiscarded + " timeout=" + framesTimedOut
|
||||
+ (percentiles == null
|
||||
+ (input == null
|
||||
? ""
|
||||
: " | sent-latency p50=" + percentiles[0] + "ms p90=" + percentiles[1]
|
||||
+ "ms p99=" + percentiles[2] + "ms max=" + percentiles[3] + "ms");
|
||||
: " | input-to-display p50=" + input[0] + "ms p90=" + input[1]
|
||||
+ "ms p99=" + input[2] + "ms max=" + input[3] + "ms");
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
/** The frame with this ID if it exists and is still open, else null. */
|
||||
private Frame openFrameLocked(int frameId) {
|
||||
if (frameId <= 0) {
|
||||
return null;
|
||||
}
|
||||
Frame frame = framesById.get(frameId);
|
||||
return frame != null && frame.isOpen() ? frame : null;
|
||||
}
|
||||
|
||||
private void addLineLocked(Frame frame, long now, String message) {
|
||||
if (frame.lines.size() >= MAX_LINES_PER_FRAME) {
|
||||
if (frame.lines.size() == MAX_LINES_PER_FRAME) {
|
||||
frame.lines.add("... line cap reached");
|
||||
frame.lines.add(new Line(now, Thread.currentThread().getName(), "... line cap reached"));
|
||||
}
|
||||
return;
|
||||
}
|
||||
frame.lines.add(String.format(Locale.US, "%+6dms [%s] %s",
|
||||
now - frame.startedAtMs, Thread.currentThread().getName(), message));
|
||||
frame.lines.add(new Line(now, Thread.currentThread().getName(), message));
|
||||
}
|
||||
|
||||
private void finishFrameLocked(Frame frame, long now, String outcome) {
|
||||
frame.finishedAtMs = now;
|
||||
frame.outcome = outcome;
|
||||
for (Map.Entry<String, Long> open : frame.openSpans.entrySet()) {
|
||||
frame.lines.add(String.format(Locale.US, "%+6dms span %s never ended (started at +%dms)",
|
||||
now - frame.startedAtMs, open.getKey(), open.getValue() - frame.startedAtMs));
|
||||
frame.lines.add(new Line(now, Thread.currentThread().getName(),
|
||||
"span " + open.getKey() + " never ended (started at +"
|
||||
+ (open.getValue() - frame.startedAtMs) + "ms)"));
|
||||
}
|
||||
frame.openSpans.clear();
|
||||
addLineLocked(frame, now, "finished: " + outcome);
|
||||
|
||||
Frame root = frame.root();
|
||||
if (frame.wasSent()) {
|
||||
framesSent++;
|
||||
sentDurations.addLast(frame.durationMs());
|
||||
while (sentDurations.size() > SENT_DURATIONS_WINDOW) {
|
||||
sentDurations.removeFirst();
|
||||
// The user-visible latency is input (or timer) to the first pixels
|
||||
// that actually reached the glasses, wherever in the tree that was.
|
||||
if (!root.subtreeSentRecorded) {
|
||||
root.subtreeSentRecorded = true;
|
||||
recordLatencyLocked(root, now - root.startedAtMs);
|
||||
insertSlowestLocked(root);
|
||||
}
|
||||
insertSlowestLocked(frame);
|
||||
} else if (outcome.startsWith("timeout")) {
|
||||
framesTimedOut++;
|
||||
} else {
|
||||
framesDiscarded++;
|
||||
}
|
||||
|
||||
recentFrames.addLast(frame);
|
||||
while (recentFrames.size() > RECENT_FRAMES_KEPT) {
|
||||
recentFrames.removeFirst();
|
||||
if (root == frame) {
|
||||
recentRoots.addLast(frame);
|
||||
while (recentRoots.size() > RECENT_ROOTS_KEPT) {
|
||||
recentRoots.removeFirst();
|
||||
}
|
||||
}
|
||||
exportDirty = true;
|
||||
}
|
||||
|
||||
private void insertSlowestLocked(Frame frame) {
|
||||
slowestSentFrames.add(frame);
|
||||
Collections.sort(slowestSentFrames, (a, b) -> Long.compare(b.durationMs(), a.durationMs()));
|
||||
while (slowestSentFrames.size() > SLOWEST_FRAMES_KEPT) {
|
||||
slowestSentFrames.remove(slowestSentFrames.size() - 1);
|
||||
private void recordLatencyLocked(Frame root, long latencyMs) {
|
||||
ArrayDeque<Long> bucket = root.reason.startsWith("input:") ? inputLatencies : renderLatencies;
|
||||
bucket.addLast(latencyMs);
|
||||
while (bucket.size() > SENT_DURATIONS_WINDOW) {
|
||||
bucket.removeFirst();
|
||||
}
|
||||
}
|
||||
|
||||
/** {p50, p90, p99, max} over the rolling sent-duration window, or null if empty. */
|
||||
private long[] sentPercentilesLocked() {
|
||||
if (sentDurations.isEmpty()) {
|
||||
private void insertSlowestLocked(Frame root) {
|
||||
if (!slowestRoots.contains(root)) {
|
||||
slowestRoots.add(root);
|
||||
}
|
||||
// Sorted and trimmed at export time, when every subtree duration is final.
|
||||
}
|
||||
|
||||
/** {p50, p90, p99, max} over a rolling latency window, or null if empty. */
|
||||
private long[] percentilesLocked(ArrayDeque<Long> window) {
|
||||
if (window.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
List<Long> sorted = new ArrayList<>(sentDurations);
|
||||
List<Long> sorted = new ArrayList<>(window);
|
||||
Collections.sort(sorted);
|
||||
return new long[] {
|
||||
percentileOfSorted(sorted, 50),
|
||||
@@ -273,11 +401,8 @@ public final class FrameTimings {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
List<Frame> timedOut = new ArrayList<>();
|
||||
synchronized (lock) {
|
||||
Iterator<Map.Entry<Integer, Frame>> iterator = activeFrames.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Frame frame = iterator.next().getValue();
|
||||
if (now - frame.startedAtMs >= FRAME_TIMEOUT_MS) {
|
||||
iterator.remove();
|
||||
for (Frame frame : new ArrayList<>(framesById.values())) {
|
||||
if (frame.isOpen() && now - frame.startedAtMs >= FRAME_TIMEOUT_MS) {
|
||||
finishFrameLocked(frame, now, "timeout: never reported finished");
|
||||
timedOut.add(frame);
|
||||
}
|
||||
@@ -340,45 +465,103 @@ public final class FrameTimings {
|
||||
}
|
||||
|
||||
private String buildExportLocked() {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
SimpleDateFormat wallClockFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
|
||||
StringBuilder out = new StringBuilder(64 * 1024);
|
||||
out.append("FrameTimings export at ").append(wallClockFormat.format(new Date())).append('\n');
|
||||
out.append(statsSummaryLocked()).append('\n');
|
||||
out.append('\n');
|
||||
out.append("Frames are trees: an input event's own frame is the root, and the renders it\n");
|
||||
out.append("caused are indented under it with offsets measured from the root's start.\n");
|
||||
out.append('\n');
|
||||
|
||||
out.append("=== slowest sent frames ===\n");
|
||||
for (Frame frame : slowestSentFrames) {
|
||||
appendFrameLocked(out, frame, wallClockFormat);
|
||||
Collections.sort(slowestRoots, (a, b) ->
|
||||
Long.compare(b.subtreeEndAtMs(now) - b.startedAtMs, a.subtreeEndAtMs(now) - a.startedAtMs));
|
||||
while (slowestRoots.size() > SLOWEST_ROOTS_KEPT) {
|
||||
slowestRoots.remove(slowestRoots.size() - 1);
|
||||
}
|
||||
|
||||
out.append("=== slowest frames that reached the glasses ===\n");
|
||||
for (Frame frame : slowestRoots) {
|
||||
appendTreeLocked(out, frame, frame, wallClockFormat, now);
|
||||
}
|
||||
|
||||
out.append("=== recent frames (oldest first) ===\n");
|
||||
for (Frame frame : recentFrames) {
|
||||
appendFrameLocked(out, frame, wallClockFormat);
|
||||
for (Frame frame : recentRoots) {
|
||||
appendTreeLocked(out, frame, frame, wallClockFormat, now);
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private String statsSummaryLocked() {
|
||||
long[] percentiles = sentPercentilesLocked();
|
||||
return "frames started=" + framesStarted + " sent=" + framesSent
|
||||
+ " discarded=" + framesDiscarded + " timeout=" + framesTimedOut
|
||||
+ " open=" + activeFrames.size()
|
||||
+ (percentiles == null
|
||||
? ""
|
||||
: "\nsent-frame latency (last " + sentDurations.size() + "): p50=" + percentiles[0]
|
||||
+ "ms p90=" + percentiles[1] + "ms p99=" + percentiles[2]
|
||||
+ "ms max=" + percentiles[3] + "ms");
|
||||
long[] input = percentilesLocked(inputLatencies);
|
||||
long[] render = percentilesLocked(renderLatencies);
|
||||
StringBuilder out = new StringBuilder();
|
||||
out.append("frames started=").append(framesStarted).append(" sent=").append(framesSent)
|
||||
.append(" discarded=").append(framesDiscarded).append(" timeout=").append(framesTimedOut)
|
||||
.append(" open=").append(openFrameCountLocked());
|
||||
appendPercentilesLocked(out, "input-to-display latency", inputLatencies.size(), input);
|
||||
appendPercentilesLocked(out, "render-to-display latency", renderLatencies.size(), render);
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private void appendFrameLocked(StringBuilder out, Frame frame, SimpleDateFormat wallClockFormat) {
|
||||
out.append("frame#").append(frame.id)
|
||||
.append(" [").append(frame.reason).append(']')
|
||||
.append(" started ").append(wallClockFormat.format(new Date(frame.startedWallClockMs)))
|
||||
.append(" duration ").append(frame.durationMs()).append("ms")
|
||||
.append(" outcome ").append(frame.outcome)
|
||||
.append('\n');
|
||||
for (String line : frame.lines) {
|
||||
out.append(" ").append(line).append('\n');
|
||||
private void appendPercentilesLocked(StringBuilder out, String label, int count, long[] percentiles) {
|
||||
if (percentiles == null) {
|
||||
return;
|
||||
}
|
||||
out.append('\n').append(label).append(" (last ").append(count).append("): p50=")
|
||||
.append(percentiles[0]).append("ms p90=").append(percentiles[1])
|
||||
.append("ms p99=").append(percentiles[2]).append("ms max=").append(percentiles[3]).append("ms");
|
||||
}
|
||||
|
||||
private int openFrameCountLocked() {
|
||||
int open = 0;
|
||||
for (Frame frame : framesById.values()) {
|
||||
if (frame.isOpen()) {
|
||||
open++;
|
||||
}
|
||||
}
|
||||
return open;
|
||||
}
|
||||
|
||||
/** Print a frame and its descendants, all timed against root's start. */
|
||||
private void appendTreeLocked(
|
||||
StringBuilder out, Frame frame, Frame root, SimpleDateFormat wallClockFormat, long now) {
|
||||
int depth = 0;
|
||||
for (Frame walk = frame; walk != root; walk = walk.parent) {
|
||||
depth++;
|
||||
}
|
||||
String indent = repeat(" ", depth);
|
||||
long startOffsetMs = frame.startedAtMs - root.startedAtMs;
|
||||
out.append(indent).append("frame#").append(frame.id)
|
||||
.append(" [").append(frame.reason).append(']');
|
||||
if (frame == root) {
|
||||
out.append(" started ").append(wallClockFormat.format(new Date(frame.startedWallClockMs)));
|
||||
long totalMs = frame.subtreeEndAtMs(now) - frame.startedAtMs;
|
||||
out.append(" duration ").append(frame.durationMs()).append("ms");
|
||||
if (!frame.children.isEmpty()) {
|
||||
out.append(" (tree ").append(totalMs).append("ms")
|
||||
.append(frame.subtreeHasOpenFrame() ? ", still open" : "").append(')');
|
||||
}
|
||||
} else {
|
||||
out.append(" started +").append(startOffsetMs).append("ms")
|
||||
.append(" duration ").append(frame.isOpen() ? "open" : (frame.durationMs() + "ms"));
|
||||
}
|
||||
out.append(" outcome ").append(frame.outcome == null ? "(open)" : frame.outcome).append('\n');
|
||||
for (Line line : frame.lines) {
|
||||
out.append(indent).append(String.format(Locale.US, " %+7dms [%s] %s",
|
||||
line.atMs - root.startedAtMs, line.thread, line.message)).append('\n');
|
||||
}
|
||||
for (Frame child : frame.children) {
|
||||
appendTreeLocked(out, child, root, wallClockFormat, now);
|
||||
}
|
||||
}
|
||||
|
||||
private static String repeat(String unit, int times) {
|
||||
StringBuilder out = new StringBuilder(unit.length() * times);
|
||||
for (int i = 0; i < times; i++) {
|
||||
out.append(unit);
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,71 @@ 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. */
|
||||
@@ -56,13 +121,20 @@ public final class SurfaceCompositor {
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,6 +149,8 @@ public final class SurfaceCompositor {
|
||||
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;
|
||||
@@ -125,6 +199,7 @@ public final class SurfaceCompositor {
|
||||
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;
|
||||
@@ -183,6 +258,31 @@ public final class SurfaceCompositor {
|
||||
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);
|
||||
@@ -205,10 +305,55 @@ public final class SurfaceCompositor {
|
||||
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) {
|
||||
@@ -228,7 +373,7 @@ public final class SurfaceCompositor {
|
||||
return null;
|
||||
}
|
||||
byte[] gray = buildGrayLocked();
|
||||
return new Composite(gray, screenWidth, screenHeight, "preview", 0);
|
||||
return new Composite(gray, screenWidth, screenHeight, "preview", 0, NO_DRAWS);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,7 +398,7 @@ public final class SurfaceCompositor {
|
||||
byte[] gray = new byte[screenWidth * screenHeight];
|
||||
if (blanked) {
|
||||
return new Composite(gray, screenWidth, screenHeight,
|
||||
"blanked:" + screenWidth + "x" + screenHeight, nextCompositeSeq++);
|
||||
"blanked:" + screenWidth + "x" + screenHeight, nextCompositeSeq++, NO_DRAWS);
|
||||
}
|
||||
List<Surface> ordered = new ArrayList<>(surfaces.values());
|
||||
ordered.sort(Comparator
|
||||
@@ -261,9 +406,15 @@ public final class SurfaceCompositor {
|
||||
.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);
|
||||
for (ScreenDraw draw : surface.draws) {
|
||||
draws.add(new ScreenDraw(draw.kind, draw.fontId, draw.encoding, draw.imageId,
|
||||
draw.x + surface.x, draw.y + surface.y, draw.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)
|
||||
@@ -271,7 +422,8 @@ public final class SurfaceCompositor {
|
||||
.append(':').append(surface.transparency)
|
||||
.append(':').append(surface.fingerprint);
|
||||
}
|
||||
return new Composite(gray, screenWidth, screenHeight, fingerprint.toString(), nextCompositeSeq++);
|
||||
return new Composite(gray, screenWidth, screenHeight, fingerprint.toString(), nextCompositeSeq++,
|
||||
draws.toArray(new ScreenDraw[0]));
|
||||
}
|
||||
|
||||
private void blendLocked(byte[] gray, Surface surface) {
|
||||
|
||||
+55
-2
@@ -205,15 +205,68 @@ public final class BleImageOptimizer {
|
||||
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][zlib(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 and each rect of a mode-8 multi-rect batch.
|
||||
* path, each rect of a mode-8 multi-rect batch, and the texture-cache planner.
|
||||
*/
|
||||
private static byte[] encodeMode3Rect(byte[] next, int stride, int left, int top,
|
||||
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];
|
||||
|
||||
@@ -46,6 +46,7 @@ public class BleProtocol {
|
||||
public static final int FACECLAW_FB_OP_ACQUIRE = 5;
|
||||
public static final int FACECLAW_FB_OP_RELEASE = 6;
|
||||
public static final int FACECLAW_WEAR_OP_QUERY = 7;
|
||||
public static final int CFW_IMAGE_MODE_CLEANUP = 11;
|
||||
private static final int FACECLAW_WAKE_EVENT = 1;
|
||||
private static final int FACECLAW_WAKE_PROTOCOL_VERSION = 1;
|
||||
// UI_FOREGROUND_EVEN_AI_ID: the stock "Even AI" assistant app. It is a
|
||||
|
||||
@@ -59,4 +59,9 @@ public class ConnectionOptions {
|
||||
// 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;
|
||||
}
|
||||
@@ -103,6 +103,22 @@ public class MessageBuilder {
|
||||
);
|
||||
}
|
||||
|
||||
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(
|
||||
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
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 64 KiB texture cache (zlib_glue mode 12).
|
||||
* 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 uint16 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 64 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-14
|
||||
* string.
|
||||
*
|
||||
* Upload entries accumulate as complete [offset u16][len u16][data] records
|
||||
* and drain into one or more standalone mode-12 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 = 65536;
|
||||
private static final int FONT_TABLE_BYTES = 96 * 2;
|
||||
|
||||
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 64 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-12 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-14 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 + 2 * (encoding - 32),
|
||||
new byte[]{(byte) (offset & 0xff), (byte) ((offset >> 8) & 0xff)});
|
||||
return offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure an ImageAtlas entry is resident (mode-13 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[4 + data.length];
|
||||
entry[0] = (byte) (offset & 0xff);
|
||||
entry[1] = (byte) ((offset >> 8) & 0xff);
|
||||
entry[2] = (byte) (data.length & 0xff);
|
||||
entry[3] = (byte) ((data.length >> 8) & 0xff);
|
||||
System.arraycopy(data, 0, entry, 4, 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-12 payloads ([12][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(12);
|
||||
}
|
||||
current.write(entry, 0, entry.length);
|
||||
}
|
||||
if (current != null) {
|
||||
payloads.add(current.toByteArray());
|
||||
}
|
||||
pendingEntries.clear();
|
||||
pendingBytes = 0;
|
||||
return payloads;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,721 @@
|
||||
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 12/13/14; 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-14
|
||||
* string or mode-13 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-14 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-13 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-12 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-13 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-12 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.
|
||||
* allowImages requires the teximg13 capability; allowFwText requires
|
||||
* font15. 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, boolean allowImages, boolean allowFwText) {
|
||||
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;
|
||||
if (allowFwText) {
|
||||
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 {
|
||||
if (!allowImages) continue; // firmware lacks teximg13: stays baked
|
||||
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-14 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-13 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-13 cached-image draw: [13][offset u16][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[8];
|
||||
sub[0] = 13;
|
||||
sub[1] = (byte) (offset & 0xff);
|
||||
sub[2] = (byte) ((offset >> 8) & 0xff);
|
||||
sub[3] = (byte) (sel.draw.x & 0xff);
|
||||
sub[4] = (byte) ((sel.draw.x >> 8) & 0xff);
|
||||
sub[5] = (byte) (sel.draw.y & 0xff);
|
||||
sub[6] = (byte) ((sel.draw.y >> 8) & 0xff);
|
||||
sub[7] = (byte) IMAGE_DRAW_OPTIONS;
|
||||
return sub;
|
||||
}
|
||||
|
||||
/**
|
||||
* Group selected glyphs into mode-14 string sub-messages:
|
||||
* [14][fontTable u16][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[9 + stringBytes.length];
|
||||
run[0] = 14;
|
||||
run[1] = (byte) (fontTable & 0xff);
|
||||
run[2] = (byte) ((fontTable >> 8) & 0xff);
|
||||
run[3] = (byte) (first.gx & 0xff);
|
||||
run[4] = (byte) ((first.gx >> 8) & 0xff);
|
||||
run[5] = (byte) (first.draw.y & 0xff);
|
||||
run[6] = (byte) ((first.draw.y >> 8) & 0xff);
|
||||
run[7] = (byte) (first.top | 0x10); // top color + transparent
|
||||
run[8] = (byte) stringBytes.length;
|
||||
System.arraycopy(stringBytes, 0, run, 9, 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;
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,15 @@ public class BmpUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
Reference in new issue
Block a user