Performance optimizations

This commit is contained in:
James Babcock committed 2026-08-20 02:46:03 -07:00
1 parent e49c699062
commit f6035ea9ec
18 files changed
+715 -158

No files matched your search

+5 -2
View File
@@ -210,6 +210,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
}
window.focused = message.focused;
inferForeground(window, message.focused);
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
@@ -650,7 +653,7 @@ function renderAndSubmit(window: BlocksWindow, inputFrameId: number): void {
return;
}
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
@@ -661,7 +664,7 @@ function renderAndSubmit(window: BlocksWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+5 -2
View File
@@ -165,6 +165,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
break;
}
window.focused = message.focused;
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
@@ -755,7 +758,7 @@ function renderAndSubmit(window: FreecellWindow, inputFrameId: number): void {
return;
}
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
@@ -766,7 +769,7 @@ function renderAndSubmit(window: FreecellWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
@@ -175,6 +175,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
}
window.focused = message.focused;
inferForeground(window, message.focused);
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
@@ -714,7 +717,7 @@ function renderAndSubmit(window: MinesweeperWindow, inputFrameId: number): void
return;
}
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
@@ -725,7 +728,7 @@ function renderAndSubmit(window: MinesweeperWindow, inputFrameId: number): void
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+5 -2
View File
@@ -175,6 +175,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
break;
}
window.focused = message.focused;
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
case "text-input":
@@ -876,7 +879,7 @@ function renderAndSubmit(win: NavWindow, inputFrameId: number): void {
return;
}
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
win.surfaceId,
@@ -887,7 +890,7 @@ function renderAndSubmit(win: NavWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
win.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+5 -2
View File
@@ -346,6 +346,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
}
window.focused = message.focused;
inferForeground(window, message.focused);
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
@@ -1043,7 +1046,7 @@ function renderAndSubmit(window: PinballWindow, inputFrameId: number): void {
return;
}
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
@@ -1054,7 +1057,7 @@ function renderAndSubmit(window: PinballWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+5 -2
View File
@@ -176,6 +176,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
}
window.focused = message.focused;
inferForeground(message.focused);
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
case "text-input":
@@ -595,7 +598,7 @@ function renderAndSubmit(win: RoamWindow, inputFrameId: number): void {
return;
}
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
win.surfaceId,
@@ -606,7 +609,7 @@ function renderAndSubmit(win: RoamWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
win.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+5 -2
View File
@@ -320,6 +320,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
break;
}
window.focused = message.focused;
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
@@ -1615,7 +1618,7 @@ function renderAndSubmit(window: TerminalWindow, inputFrameId: number): void {
return;
}
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
@@ -1626,7 +1629,7 @@ function renderAndSubmit(window: TerminalWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+5 -2
View File
@@ -162,6 +162,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
break;
}
window.focused = message.focused;
// Marks the main-thread -> worker hop, which is otherwise an
// unexplained gap inside the shell's handle-input span.
frameTimings.logFrame(message.frameId, `input received in ${message.windowId} worker`);
handleInput(window, message.event as DashboardInputEvent, message.frameId);
break;
}
@@ -666,7 +669,7 @@ function renderAndSubmit(window: TimerWindow, inputFrameId: number): void {
return;
}
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
communicator.submitSurfaceFrame(
buffer.buffer,
window.surfaceId,
@@ -677,7 +680,7 @@ function renderAndSubmit(window: TimerWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws)),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+92 -33
View File
@@ -200,6 +200,8 @@ class DashboardController {
private lastSys = "none yet";
private shellRenderInProgress = false;
private shellRenderQueued = false;
/** Input frame that requested the next shell render; see requestShellRender. */
private pendingShellRenderCauseFrameId = 0;
private nextShellRenderWantsFreshData = false;
// One shared worker per app hosts all its windows; spawned on first launch.
private readonly appHosts = new Map<string, WorkerAppHost>();
@@ -467,7 +469,14 @@ class DashboardController {
* visible. Concurrent display-wake, shell, and wakeword callbacks share the
* same operation.
*/
private ensureEvenHubSessionActive(): Promise<boolean> {
/**
* frameId (0 when the caller has none) attributes the barrier's four
* round trips to the input frame waiting on them; a wake barrier is the
* largest single input-to-display term there is, so knowing which step
* owns the second matters. Callers that join an in-flight barrier share
* the first caller's spans.
*/
private ensureEvenHubSessionActive(frameId = 0): Promise<boolean> {
if (this.evenHubResumePromise) return this.evenHubResumePromise;
const communicator = this.communicator;
if (!communicator || this.phase === "charging" || this.phase === "disconnected") {
@@ -475,14 +484,19 @@ class DashboardController {
}
const operation = (async () => {
await communicator.setG2ScreenOn(true);
if (!(await communicator.resumeEvenHubSession())) {
await frameTimings.spanAsync(frameId, "wake:screen-on", () => communicator.setG2ScreenOn(true));
const resumed = await frameTimings.spanAsync(frameId, "wake:resume-session", () =>
communicator.resumeEvenHubSession(),
);
if (!resumed) {
return false;
}
// Resume first: setScreenBlanked(false) then recomposites retained state
// as the desired first frame for the fresh layout.
await communicator.setScreenBlanked(false);
const ready = await communicator.awaitEvenHubSessionReady(EVENHUB_WAKE_READY_TIMEOUT_MS);
await frameTimings.spanAsync(frameId, "wake:unblank", () => communicator.setScreenBlanked(false));
const ready = await frameTimings.spanAsync(frameId, "wake:await-ready", () =>
communicator.awaitEvenHubSessionReady(EVENHUB_WAKE_READY_TIMEOUT_MS),
);
if (ready && this.communicator === communicator) {
this.evenHubSessionSuspended = false;
}
@@ -1323,6 +1337,11 @@ class DashboardController {
let frameOwned = false;
try {
const inputEvent = rawInputEventToInputEvent(event);
// The gesture, plus which app is on screen and whether input goes to it,
// the sidebar, or a shell overlay. Java only knows the raw event codes,
// and without the target the export says what was pressed but not who
// spent the time drawing the answer.
frameTimings.annotateFrame(frameId, `${inputEvent.type} ${shell.describeInputTarget()}`);
if (this.glassesLocked) {
const ringDoubleTap =
inputEvent.type === "double-click" &&
@@ -1335,7 +1354,9 @@ class DashboardController {
if (ringDoubleTap) shell.sleep();
} else {
shell.wake("sidebar");
const ready = await this.ensureEvenHubSessionActive();
const ready = await frameTimings.spanAsync(frameId, "wake-barrier", () =>
this.ensureEvenHubSessionActive(frameId),
);
if (!ready) {
this.appendLog("EvenHub wake barrier timed out for locked display");
}
@@ -1356,7 +1377,9 @@ class DashboardController {
shellPreWoke = shell.wake("sidebar");
}
if (shellPreWoke || this.evenHubSessionSuspended || this.evenHubResumePromise) {
const ready = await this.ensureEvenHubSessionActive();
const ready = await frameTimings.spanAsync(frameId, "wake-barrier", () =>
this.ensureEvenHubSessionActive(frameId),
);
if (!ready) {
this.appendLog(`EvenHub wake barrier timed out for ${event.kind}`);
}
@@ -1391,14 +1414,17 @@ class DashboardController {
}
if (outcome.shell) {
this.requestShellRender();
// Pass frameId as the cause, not the owner: the chrome render gets its
// own child frame under this one, so the export shows the input and
// every render it triggered on a single timeline.
this.requestShellRender(frameId);
}
if (outcome.window) {
// The window adapter owned frameId (render or explicit finish).
frameOwned = true;
} else if (outcome.shell) {
frameOwned = true;
frameTimings.finishFrame(frameId, "input consumed by shell; chrome render scheduled");
frameTimings.finishFrame(frameId, "handled by shell; chrome render spawned");
}
} finally {
if (!frameOwned) {
@@ -1607,17 +1633,20 @@ class DashboardController {
frameTimings.finishFrame(frameId, "discarded: window frame with no active connection");
return;
}
const fingerprint = planesFingerprint(planes);
const fingerprint = frameTimings.span(frameId, "fingerprint", () => planesFingerprint(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
await communicator.submitSurfaceFrame(
surfaceId,
buffer,
{ x: 0, y: 0, width: image.width, height: image.height },
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws));
await frameTimings.spanAsync(frameId, "submit", () =>
communicator.submitSurfaceFrame(
surfaceId,
buffer,
{ x: 0, y: 0, width: image.width, height: image.height },
fingerprint,
paintMs,
frameId,
preparedDraws,
),
);
}
@@ -1635,7 +1664,14 @@ class DashboardController {
* overlays). Coalesces like requestRender: one render in flight, at most
* one queued.
*/
requestShellRender(): void {
requestShellRender(causeFrameId = 0): void {
// Remember the input frame that asked for this chrome repaint so
// renderShell can nest its frame under it; without that link an input
// event and the shell render it caused show up as two unrelated frames
// and the real input-to-pixels latency is never measured.
if (causeFrameId > 0 && this.pendingShellRenderCauseFrameId === 0) {
this.pendingShellRenderCauseFrameId = causeFrameId;
}
if (this.shellRenderInProgress) {
this.shellRenderQueued = true;
return;
@@ -1656,9 +1692,13 @@ class DashboardController {
}
private async renderShell(): Promise<void> {
const frameId = frameTimings.startFrame("render:shell");
const causeFrameId = this.pendingShellRenderCauseFrameId;
this.pendingShellRenderCauseFrameId = 0;
const frameId = frameTimings.startFrame("render:shell", causeFrameId);
frameTimings.annotateFrame(frameId, shell.describeInputTarget());
const wantFreshData = this.nextShellRenderWantsFreshData;
this.nextShellRenderWantsFreshData = false;
if (wantFreshData) frameTimings.logFrame(frameId, "follow-up repaint that must not use cached data");
beginRenderPass(!wantFreshData);
const paintStartedAtMs = Date.now();
const planes = frameTimings.span(frameId, "paint", () =>
@@ -1668,27 +1708,46 @@ class DashboardController {
const paintUsedStaleData = endRenderPass();
if (paintUsedStaleData) {
// Repaint with fresh data (e.g. notification icons) once this frame is
// out; mirrors renderDashboard's stale-data contract.
// out; mirrors renderDashboard's stale-data contract. Linked to this
// frame so the export shows one input costing two round trips instead of
// an unexplained extra render.
this.nextShellRenderWantsFreshData = true;
this.requestShellRender();
this.requestShellRender(frameId);
}
if (!this.communicator || this.phase === "charging") {
const communicator = this.communicator;
if (!communicator || this.phase === "charging") {
frameTimings.finishFrame(frameId, "discarded: shell render with no active connection");
return;
}
const fingerprint = frameTimings.span(frameId, "fingerprint", () => planesFingerprint(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
await this.communicator.submitSurfaceFrame(
SHELL_SURFACE_ID,
buffer,
{ x: 0, y: 0, width: image.width, height: image.height },
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws));
// Spanned because the bridge serializes Java calls: a frame can sit here
// behind another surface's submission, which is otherwise an unexplained
// jump between the paint spans and the composite.
await frameTimings.spanAsync(frameId, "submit", () =>
communicator.submitSurfaceFrame(
SHELL_SURFACE_ID,
buffer,
{ x: 0, y: 0, width: image.width, height: image.height },
fingerprint,
paintMs,
frameId,
preparedDraws,
),
);
await this.communicator.waitForFrameFinished(frameId, FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS);
// Backpressure: the next shell render waits for this one to reach the
// glasses. Timing out here means the loop was blocked for the full timeout
// and any input arriving meanwhile had its chrome repaint delayed, so say
// so in the frame rather than leaving a silent stall.
const outcome = await communicator.waitForFrameFinished(frameId, FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS);
if (outcome === null) {
frameTimings.logFrame(
frameId,
`still unsent after ${FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS}ms; shell render loop released`,
);
}
this.updateCompositePreview();
}
+44 -7
View File
@@ -141,6 +141,8 @@ export class GrayImage {
readonly height: number;
readonly pixels: Uint8Array;
private readonly drawList: DeferredDraw[] = [];
/** See sourceContentHash32; null until this image is hashed as a draw source. */
private memoizedSourceHash: number | null = null;
constructor(width: number, height: number, fill = 0) {
this.width = width;
@@ -378,11 +380,7 @@ export class GrayImage {
/** 32-bit content hash over pixels and deferred draws (fingerprint's core). */
contentHash32(): number {
let hash = 2166136261;
for (let i = 0; i < this.pixels.length; i++) {
hash ^= this.pixels[i]!;
hash = Math.imul(hash, 16777619);
}
let hash = this.pixelsHash32();
for (const placed of this.drawList) {
if (placed.kind === "glyph") {
hash = mixInt(hash, placed.font.fingerprintId);
@@ -392,7 +390,7 @@ export class GrayImage {
hash = mixInt(hash, 0x1c0e5);
hash = mixInt(hash, placed.source.width);
hash = mixInt(hash, placed.source.height);
hash = mixInt(hash, placed.source.contentHash32());
hash = mixInt(hash, placed.source.sourceContentHash32());
} else {
hash = mixInt(hash, 0xf17e);
hash = mixInt(hash, placed.font.atlasTag);
@@ -408,6 +406,44 @@ export class GrayImage {
return hash >>> 0;
}
/**
* FNV-1a over the raster pixels. Four bytes at a time where alignment
* allows: this runs on every frame's fingerprint, over the whole screen, on
* the thread that is about to hand the frame to the transport.
*/
private pixelsHash32(): number {
const pixels = this.pixels;
let hash = 2166136261;
let i = 0;
if ((pixels.byteOffset & 3) === 0) {
const words = new Uint32Array(pixels.buffer, pixels.byteOffset, pixels.length >>> 2);
for (let w = 0; w < words.length; w++) {
hash ^= words[w]!;
hash = Math.imul(hash, 16777619);
}
i = words.length << 2;
}
for (; i < pixels.length; i++) {
hash ^= pixels[i]!;
hash = Math.imul(hash, 16777619);
}
return hash >>> 0;
}
/**
* contentHash32 for an image being used as another image's deferred-draw
* source, memoized. drawImage/drawFwText document their source as immutable
* from the call onwards (glyph-wire's ImageAtlas id cache already depends on
* that), so the hash of a long-lived icon asset can be computed once instead
* of on every frame that draws it.
*/
sourceContentHash32(): number {
if (this.memoizedSourceHash === null) {
this.memoizedSourceHash = this.contentHash32();
}
return this.memoizedSourceHash;
}
fingerprint(): string {
return `fnv:${this.contentHash32().toString(16)}`;
}
@@ -421,7 +457,8 @@ export class GrayImage {
*/
to8bppBuffer(): Uint8Array {
if (!this.drawList.length) {
return Uint8Array.from(this.pixels);
// slice() is a memcpy; Uint8Array.from walks the buffer element by element.
return this.pixels.slice();
}
// withDrawsBaked's buffer is a private temporary, safe to hand out.
return this.withDrawsBaked().pixels;
+26 -2
View File
@@ -111,6 +111,8 @@ export class FaceclawCommunicatorBridge {
private readonly communicator: any;
private readonly listenerProxy: any;
private javaCallQueue: Promise<void> = Promise.resolve();
/** Calls waiting on javaCallQueue; 0 means enqueueJavaCall's fast path is safe. */
private queuedJavaCalls = 0;
private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>();
// Recent frame-finished outcomes from the Java side, so waitForFrameFinished
// does not race against finishes that land before the wait starts.
@@ -227,10 +229,30 @@ export class FaceclawCommunicatorBridge {
}, 0);
}
private enqueueJavaCall<T>(operation: () => T): Promise<T> {
/**
* Run a Java call, ordered against every other call made through here.
*
* inlineWhenIdle runs the call on the spot when nothing is queued ahead of
* it. Ordering still holds — the fast path only fires when no earlier call
* is waiting — and it skips a task-queue hop that measured ~18ms on device
* (the gap between "span submit start" and the Java side's own log line in
* the frame timings), which is pure latency on a call the caller is already
* awaiting. Off by default: a call that might block for a while should keep
* yielding to the main looper first.
*/
private enqueueJavaCall<T>(operation: () => T, inlineWhenIdle = false): Promise<T> {
if (inlineWhenIdle && this.queuedJavaCalls === 0) {
try {
return Promise.resolve(operation());
} catch (error) {
return Promise.reject(error);
}
}
this.queuedJavaCalls++;
const run = () =>
new Promise<T>((resolve, reject) => {
setTimeout(() => {
this.queuedJavaCalls--;
try {
resolve(operation());
} catch (error) {
@@ -526,6 +548,8 @@ export class FaceclawCommunicatorBridge {
// ArrayBuffer, which NativeScript marshals to a ByteBuffer without the
// ~150ms per-element copy a byte[] parameter would need.
const snapshot = new Uint8Array(pixels8bpp);
// inlineWhenIdle: this is the frame path, the Java side of it measures
// ~5ms (composite + pack), and the caller awaits it either way.
await this.enqueueJavaCall(() => {
this.communicator.submitSurfaceFrame(
snapshot.buffer,
@@ -539,7 +563,7 @@ export class FaceclawCommunicatorBridge {
Math.round(nonNegativeNumber(frameId)),
glyphs,
);
});
}, true);
}
async disconnect(): Promise<void> {
+41 -11
View File
@@ -3,13 +3,17 @@
* instrumentation; see FrameTimings.java). Frame ID 0 means "no frame" and is
* ignored by every method, so callers never need to null-check.
*
* Because the JS side is single-threaded, this module also tracks a "current
* frame" so that leaf drawing/IO code can record spans without threading a
* frame ID through every call signature.
* Frames form a tree: pass the causing frame's ID as `parentFrameId` when one
* frame fans out into several (an input event that repaints both the focused
* app and the shell chrome), and the export nests them under one timeline
* instead of showing unrelated frames.
*
* Because each JS context is single-threaded, this module also tracks a
* "current frame" so that leaf drawing/IO code can record spans without
* threading a frame ID through every call signature.
*/
declare const com: any;
declare const global: any;
let javaInstance: any = null;
let javaUnavailable = false;
@@ -20,12 +24,11 @@ let currentFrameId = 0;
function getJava(): any {
if (javaInstance === null && !javaUnavailable) {
// Deliberately not gated on global.isAndroid: this runs in app workers
// too, and a worker that had not yet imported the core globals would
// silently drop every frame it renders.
try {
if (typeof com !== "undefined" && global.isAndroid) {
javaInstance = com.faceclaw.app.FrameTimings.getInstance();
} else {
javaUnavailable = true;
}
javaInstance = com.faceclaw.app.FrameTimings.getInstance();
} catch {
javaUnavailable = true;
}
@@ -33,10 +36,23 @@ function getJava(): any {
return javaInstance;
}
export function startFrame(reason: string): number {
/** Begin a frame; parentFrameId links it under the frame that caused it. */
export function startFrame(reason: string, parentFrameId = 0): number {
const java = getJava();
if (!java) return fallbackNextFrameId--;
return Number(java.startFrame(reason));
// Guarded: requestRender-style callbacks can be invoked with an unrelated
// argument, and a NaN would blow up the int marshalling.
const parent = Number.isFinite(parentFrameId) ? Math.max(0, Math.round(parentFrameId)) : 0;
return Number(java.startFrame(reason, parent));
}
/**
* Append a fact to the frame's label, e.g. which app was in the foreground
* when the input arrived. Shows in the frame's header line in the export.
*/
export function annotateFrame(frameId: number, text: string): void {
if (frameId <= 0 || !text) return;
getJava()?.annotate(frameId, text);
}
export function logFrame(frameId: number, message: string): void {
@@ -68,6 +84,20 @@ export function span<T>(frameId: number, name: string, fn: () => T): T {
}
}
/**
* Span around an awaited step. Most multi-second gaps in the timing export are
* awaits (an app's async input handler, a worker round trip, the Java call
* queue), so instrument those with this rather than leaving a bare jump.
*/
export async function spanAsync<T>(frameId: number, name: string, fn: () => Promise<T>): Promise<T> {
spanStart(frameId, name);
try {
return await fn();
} finally {
spanEnd(frameId, name);
}
}
/** Set the current frame while running fn, for leaf code using the *Current helpers. */
export function runWithFrame<T>(frameId: number, fn: () => T): T {
const previous = currentFrameId;
+25 -5
View File
@@ -65,11 +65,21 @@ export type InProcessAppOptions = {
};
export function createInProcessWindow(options: InProcessWindowOptions): InProcessWindow {
const requestRender = () => {
void render(0).catch((error) => {
/**
* Repaint under a new frame. causeFrameId links it to the frame that made it
* necessary (a stale-cache paint asking for a redo); 0 for an app-initiated
* repaint. Each repaint gets a frame either way: an app-initiated one (a
* clock tick, arriving data) is a real screen update with real latency, and
* submitting it under frame 0 made it invisible in the timing export except
* as an anonymous "superseded by frame#0" line in some other frame's log.
*/
const renderInNewFrame = (causeFrameId: number) => {
void render(frameTimings.startFrame(`render:${options.windowId}`, causeFrameId)).catch((error) => {
console.error(`${options.windowId} render failed: ${error}`);
});
};
// Handed to app code as a bare callback, so it takes no arguments.
const requestRender = () => renderInNewFrame(0);
let heightMode = options.heightMode ?? "min";
const stack = new LayerStack(
options.baseLayer,
@@ -91,16 +101,21 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
frameTimings.finishFrame(frameId, "discarded: window closed");
return;
}
frameTimings.annotateFrame(frameId, `window=${options.windowId}`);
const wantFreshData = nextRenderWantsFreshData;
nextRenderWantsFreshData = false;
beginRenderPass(!wantFreshData);
const paintStartedAtMs = Date.now();
const planes = stack.paint();
// runWithFrame so leaf data sources (notification icons, calendar) attach
// their own spans to this frame.
const planes = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => stack.paint()),
);
const paintUsedStaleData = endRenderPass();
await options.submitFrame(planes, Date.now() - paintStartedAtMs, frameId);
if (paintUsedStaleData) {
nextRenderWantsFreshData = true;
requestRender();
renderInNewFrame(frameId);
}
}
@@ -165,7 +180,12 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
await render(frameId);
return;
}
await stack.handleInput(event);
// Spanned separately from the paint: an app's input handler may await
// real work (launching an app, a network call), and that used to show up
// as a bare multi-second gap inside the shell's handle-input span.
await frameTimings.spanAsync(frameId, `app-input:${options.windowId}`, () =>
stack.handleInput(event),
);
await render(frameId);
},
requestRender,
+14
View File
@@ -599,6 +599,20 @@ class Shell {
};
}
/**
* Compact description of where input is currently going, for the frame
* timing export: "which app was drawing" is usually the first thing you need
* to interpret an input-to-display latency, and it is not recoverable from
* the input event itself.
*/
describeInputTarget(): string {
const foreground = this.foregroundWindow()?.windowId ?? "none";
if (!this.screenOn) return `fg=${foreground} target=screen-off`;
if (this.activeVoiceLayer) return `fg=${foreground} target=voice`;
if (!this.stack.isAtBase()) return `fg=${foreground} target=shell-overlay`;
return `fg=${foreground} target=${this.focus}`;
}
/** Whether a window is the current input target (foreground + focus in-window). */
isWindowFocused(windowId: string): boolean {
return this.screenOn && this.focus === "window" && this.foregroundWindow()?.windowId === windowId;
+2
View File
@@ -3,6 +3,7 @@ import { windowIcon } from "./chrome-layer";
import { type IconName } from "../../graphics/icons";
import { toolRegistry, type ToolResult, type ToolSpec } from "../../assistant/tool-registry";
import { appViewportSize, type WindowHeightMode } from "./geometry";
import * as frameTimings from "../../native/frame-timings";
import { shell, type ShellWindow } from "./shell";
/**
@@ -293,6 +294,7 @@ export class WorkerAppHost {
},
drawIcon: windowIcon(spec.icon, spec.iconLetter, spec.iconGlyph),
handleInput: (event, frameId) => {
frameTimings.logFrame(frameId, `input posted to the ${this.options.appId} worker`);
this.post({
type: "input",
windowId: spec.windowId,