Use the texture cache to avoid resending icons and font glyphs

This commit is contained in:
James Babcock committed 2026-08-18 23:39:21 -07:00
1 parent 221dbb77ea
commit 83e0fa919f
26 files changed
+1724 -113

No files matched your search

+4 -2
View File
@@ -9,7 +9,8 @@
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { flattenPlanes, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import * as frameTimings from "../../native/frame-timings";
import type { DashboardInputEvent } from "../../ui/layers";
@@ -647,7 +648,7 @@ function renderAndSubmit(window: BlocksWindow, inputFrameId: number): void {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const image = frameTimings.span(frameId, "flatten", () => flattenPlanes(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
communicator.submitSurfaceFrame(
buffer.buffer,
@@ -659,6 +660,7 @@ function renderAndSubmit(window: BlocksWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+4 -2
View File
@@ -13,7 +13,8 @@
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { flattenPlanes, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import * as frameTimings from "../../native/frame-timings";
import type { DashboardInputEvent } from "../../ui/layers";
@@ -752,7 +753,7 @@ function renderAndSubmit(window: FreecellWindow, inputFrameId: number): void {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const image = frameTimings.span(frameId, "flatten", () => flattenPlanes(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
communicator.submitSurfaceFrame(
buffer.buffer,
@@ -764,6 +765,7 @@ function renderAndSubmit(window: FreecellWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+10 -4
View File
@@ -287,11 +287,17 @@ class LauncherGridLayer implements Layer {
: renderIcon("folder-filled", ICON_SIZE);
if (icon) {
// Clip the icon at the grid bottom so a peeking row shows only its top.
// A fully visible icon goes through the deferred-image path (texture
// cacheable); the clipped peeking row falls back to a raster blit,
// since cached draws are whole-image only.
const clipHeight = Math.min(icon.height, Math.floor(gridBottom - blockTop));
image.bitBlt(icon, Math.round(centerX - icon.width / 2), Math.round(blockTop), {
height: clipHeight,
transparentZero: true,
});
const iconX = Math.round(centerX - icon.width / 2);
const iconY = Math.round(blockTop);
if (clipHeight >= icon.height) {
image.drawImage(icon, iconX, iconY);
} else {
image.bitBlt(icon, iconX, iconY, { height: clipHeight, transparentZero: true });
}
}
const labelY = Math.round(blockTop + ICON_SIZE + LABEL_GAP);
if (labelY + font.lineHeight <= gridBottom) {
@@ -13,7 +13,8 @@
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { flattenPlanes, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import * as frameTimings from "../../native/frame-timings";
import type { DashboardInputEvent } from "../../ui/layers";
@@ -711,7 +712,7 @@ function renderAndSubmit(window: MinesweeperWindow, inputFrameId: number): void
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const image = frameTimings.span(frameId, "flatten", () => flattenPlanes(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
communicator.submitSurfaceFrame(
buffer.buffer,
@@ -723,6 +724,7 @@ function renderAndSubmit(window: MinesweeperWindow, inputFrameId: number): void
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+4 -2
View File
@@ -8,7 +8,8 @@
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { flattenPlanes, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import * as frameTimings from "../../native/frame-timings";
import type { DashboardInputEvent } from "../../ui/layers";
@@ -873,7 +874,7 @@ function renderAndSubmit(win: NavWindow, inputFrameId: number): void {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const image = frameTimings.span(frameId, "flatten", () => flattenPlanes(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
communicator.submitSurfaceFrame(
buffer.buffer,
@@ -885,6 +886,7 @@ function renderAndSubmit(win: NavWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
);
win.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+4 -2
View File
@@ -19,7 +19,8 @@
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { flattenPlanes, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import * as frameTimings from "../../native/frame-timings";
import { getStringSetting, setStringSetting } from "../../native/settings-store";
@@ -1040,7 +1041,7 @@ function renderAndSubmit(window: PinballWindow, inputFrameId: number): void {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const image = frameTimings.span(frameId, "flatten", () => flattenPlanes(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
communicator.submitSurfaceFrame(
buffer.buffer,
@@ -1052,6 +1053,7 @@ function renderAndSubmit(window: PinballWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+4 -2
View File
@@ -7,7 +7,8 @@
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { flattenPlanes, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { truncateText, wrapText } from "../../graphics/textwrap";
import * as frameTimings from "../../native/frame-timings";
@@ -593,7 +594,7 @@ function renderAndSubmit(win: RoamWindow, inputFrameId: number): void {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const image = frameTimings.span(frameId, "flatten", () => flattenPlanes(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
communicator.submitSurfaceFrame(
buffer.buffer,
@@ -605,6 +606,7 @@ function renderAndSubmit(win: RoamWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
);
win.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+4 -2
View File
@@ -32,7 +32,8 @@
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { flattenPlanes, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getFont } from "../../graphics/bdffont";
import { TERMINAL_ICON_GLYPHS } from "../../graphics/icons";
import * as frameTimings from "../../native/frame-timings";
@@ -1561,7 +1562,7 @@ function renderAndSubmit(window: TerminalWindow, inputFrameId: number): void {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const image = frameTimings.span(frameId, "flatten", () => flattenPlanes(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
communicator.submitSurfaceFrame(
buffer.buffer,
@@ -1573,6 +1574,7 @@ function renderAndSubmit(window: TerminalWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+4 -2
View File
@@ -5,7 +5,8 @@
*/
import "@nativescript/core/globals";
import { GrayImage } from "../../graphics/image";
import { flattenPlanes, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getDefaultSmallFont, getFont } from "../../graphics/bdffont";
import * as frameTimings from "../../native/frame-timings";
import {
@@ -660,7 +661,7 @@ function renderAndSubmit(window: TimerWindow, inputFrameId: number): void {
frameTimings.finishFrame(frameId, "discarded: no active communicator");
return;
}
const image = frameTimings.span(frameId, "flatten", () => flattenPlanes(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
communicator.submitSurfaceFrame(
buffer.buffer,
@@ -672,6 +673,7 @@ function renderAndSubmit(window: TimerWindow, inputFrameId: number): void {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
);
window.lastSubmittedFingerprint = fingerprint;
} catch (error) {
+6 -3
View File
@@ -16,7 +16,8 @@ import { isWelcomeSoundPending, setWelcomeSoundPending } from "../phone-ui/onboa
import { beginRenderPass, endRenderPass } from "../util/render-freshness";
import { voiceControlBridge } from "../native/voice-control";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image";
import { flattenPlanes, planesFingerprint, type Plane } from "../graphics/plane";
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../graphics/plane";
import { prepareFrameDraws } from "../graphics/glyph-wire";
import { getDefaultMediumFont } from "../graphics/bdffont";
import { wrapText } from "../graphics/textwrap";
import { rawInputEventToInputEvent, shell, type ShellInputOutcome } from "../ui/shell/shell";
@@ -1562,7 +1563,7 @@ class DashboardController {
return;
}
const fingerprint = planesFingerprint(planes);
const image = frameTimings.span(frameId, "flatten", () => flattenPlanes(planes));
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
const buffer = image.to8bppBuffer();
await communicator.submitSurfaceFrame(
surfaceId,
@@ -1571,6 +1572,7 @@ class DashboardController {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
);
}
@@ -1630,7 +1632,7 @@ class DashboardController {
return;
}
const fingerprint = frameTimings.span(frameId, "fingerprint", () => planesFingerprint(planes));
const image = frameTimings.span(frameId, "flatten", () => flattenPlanes(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,
@@ -1639,6 +1641,7 @@ class DashboardController {
fingerprint,
paintMs,
frameId,
prepareFrameDraws(draws),
);
await this.communicator.waitForFrameFinished(frameId, FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS);
this.updateCompositePreview();
+12 -2
View File
@@ -20,6 +20,13 @@ export class BdfFont {
* bitmap). Only meaningful within one JS context; never persist it.
*/
readonly fingerprintId = nextFontFingerprintId++;
/**
* Stable cross-context identity for the on-glasses glyph atlas (the
* embedded font name). Worker threads and the main thread must agree on
* it, unlike fingerprintId. Fonts without one (ad-hoc parses) simply never
* participate in texture caching — their glyphs stay baked.
*/
readonly atlasKey: string | null;
readonly ascent: number;
readonly descent: number;
readonly lineHeight: number;
@@ -40,6 +47,7 @@ export class BdfFont {
defaultChar: number;
glyphs: Map<number, Glyph>;
fallback?: BdfFont;
atlasKey?: string;
}) {
this.ascent = opts.ascent;
this.descent = opts.descent;
@@ -47,9 +55,10 @@ export class BdfFont {
this.defaultChar = opts.defaultChar;
this.glyphs = opts.glyphs;
this.fallback = opts.fallback;
this.atlasKey = opts.atlasKey ?? null;
}
static parse(source: string, opts: { fallback?: BdfFont; minEncoding?: number } = {}): BdfFont {
static parse(source: string, opts: { fallback?: BdfFont; minEncoding?: number; atlasKey?: string } = {}): BdfFont {
const minEncoding = opts.minEncoding ?? 0;
const lines = source.replace(/\r/g, "").split("\n");
let ascent = 10;
@@ -128,7 +137,7 @@ export class BdfFont {
}
}
return new BdfFont({ ascent, descent, defaultChar, glyphs, fallback: opts.fallback });
return new BdfFont({ ascent, descent, defaultChar, glyphs, fallback: opts.fallback, atlasKey: opts.atlasKey });
}
/** Exact lookup for a single code point, with no default-char substitution. */
@@ -192,6 +201,7 @@ function loadEmbeddedFont(name: EmbeddedFontName): BdfFont {
const font = BdfFont.parse(text, {
fallback: fallbackName ? loadEmbeddedFont(fallbackName) : undefined,
minEncoding: fontMinEncoding[name],
atlasKey: name,
});
cachedEmbeddedFonts.set(name, font);
return font;
+225
View File
@@ -0,0 +1,225 @@
/**
* Marshals deferred-draw identity (text glyphs and icon images) to the Java
* side for the texture-cache pipeline (CFW modes 12/13/14; see
* notes/texture-cache-display-list-design.md).
*
* Channels, all cheap ByteBuffer crossings:
* - glyph raster registration into the process-wide Java GlyphAtlas, once
* per (font, glyph) per JS context (main thread and each worker register
* independently; the atlas dedupes by the font's stable atlasKey);
* - image registration into the Java ImageAtlas, once per distinct image
* content per JS context (images are content-addressed: the key is a hash
* of dimensions + pixels, so the same icon rendered anywhere dedupes and
* a changed icon is simply a new image);
* - a per-frame draw list accompanying each submitted surface frame, which
* the Java planner may replay as on-glasses cached draws.
*
* Draws that can't participate (no atlasKey, outside the mode-14 ASCII
* table, ink outside the line cell, oversized images) are simply left out —
* their pixels are baked into the submitted frame either way, so exclusion
* only means "no wire savings for this draw".
*/
import { BdfFont, Glyph } from "./bdffont";
import { GrayImage, type DeferredDraw, type PlacedImage } from "./image";
declare const com: any;
const GLYPH_RECORD_BYTES = 12; // [0][fontId u16][encoding u32][penX s16][lineY s16][value u8]
const IMAGE_RECORD_BYTES = 9; // [1][imageId u32][x s16][y s16]
type FontWireState = {
fontId: number;
cellHeight: number;
/** Encodings already registered with the Java atlas from this JS context. */
registered: Set<number>;
};
const fontStates = new Map<BdfFont, FontWireState>();
/**
* Java-side image id per source image object, or null for images that can't
* participate. Sources handed to drawImage are immutable by contract, so
* memoizing by object identity is safe; a re-rendered icon is a new object
* and re-resolves (usually to the same content-addressed id).
*/
const imageIds = new WeakMap<GrayImage, number | null>();
function fontWireState(font: BdfFont): FontWireState | null {
if (!global.isAndroid || !font.atlasKey) return null;
if (font.lineHeight <= 0 || font.lineHeight > 255) return null;
let state = fontStates.get(font);
if (!state) {
state = {
fontId: com.faceclaw.app.GlyphAtlas.fontId(font.atlasKey),
cellHeight: font.lineHeight,
registered: new Set(),
};
fontStates.set(font, state);
}
return state;
}
/**
* Whether this glyph can be expressed as an on-glasses cached draw: within
* the mode-14 char table (32..127), a nonempty raster that fits the font's
* line cell (the cached image is bbxWidth x lineHeight with the ink placed at
* inkTop), and metrics that fit the wire fields.
*/
function representableGlyph(font: BdfFont, glyph: Glyph): boolean {
if (glyph.encoding < 32 || glyph.encoding > 127) return false;
if (glyph.bbxWidth <= 0 || glyph.bbxWidth > 255) return false;
if (glyph.bbxHeight <= 0 || glyph.bbxHeight > 255) return false;
if (glyph.bbxX < -128 || glyph.bbxX > 127) return false;
const inkTop = font.ascent - (glyph.bbxHeight + glyph.bbxY);
return inkTop >= 0 && inkTop + glyph.bbxHeight <= font.lineHeight;
}
/** Resolve (registering on first sight) the Java image id for a drawImage source. */
function imageId(placed: PlacedImage): number | null {
if (!global.isAndroid) return null;
const source = placed.source;
const memo = imageIds.get(source);
if (memo !== undefined) return memo;
let id: number | null = null;
if (source.width > 0 && source.width <= 255 && source.height > 0 && source.height <= 255) {
const key = `img:${source.width}x${source.height}:${source.contentHash32().toString(16)}`;
id = com.faceclaw.app.ImageAtlas.ensure(key, source.width, source.height, source.pixels.buffer);
if (!(typeof id === "number") || id <= 0) id = null;
}
imageIds.set(source, id);
return id;
}
function inRange16(v: number): boolean {
return v >= -32768 && v <= 32767;
}
/**
* Register any not-yet-registered glyph rasters and image contents among
* `draws` with the Java atlases, then build the per-frame draw buffer to pass
* alongside the frame's pixels: little-endian tagged records
* [0][fontId u16][encoding u32][penX s16][lineY s16][value u8] (glyph)
* [1][imageId u32][x s16][y s16] (image)
* in draw order. Returns null when nothing is expressible (or off-Android).
*/
export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer | null {
if (!global.isAndroid || draws.length === 0) return null;
// Pass 1: resolve ids, collect unregistered glyph rasters, size the buffer.
let registration: Map<FontWireState, { font: BdfFont; glyphs: Glyph[] }> | null = null;
let bytes = 0;
for (const placed of draws) {
if (placed.kind === "glyph") {
const state = fontWireState(placed.font);
if (!state || !representableGlyph(placed.font, placed.glyph)) continue;
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
bytes += GLYPH_RECORD_BYTES;
if (state.registered.has(placed.glyph.encoding)) continue;
state.registered.add(placed.glyph.encoding);
registration ??= new Map();
let group = registration.get(state);
if (!group) {
group = { font: placed.font, glyphs: [] };
registration.set(state, group);
}
group.glyphs.push(placed.glyph);
} else {
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
if (imageId(placed) !== null) bytes += IMAGE_RECORD_BYTES;
}
}
if (registration) {
com.faceclaw.app.GlyphAtlas.register(buildRegistrationBuffer(registration));
}
if (bytes === 0) return null;
// Pass 2: the frame's draw records.
const out = new DataView(new ArrayBuffer(bytes));
let offset = 0;
for (const placed of draws) {
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
if (placed.kind === "glyph") {
const state = fontWireState(placed.font);
if (!state || !representableGlyph(placed.font, placed.glyph)) continue;
out.setUint8(offset, 0);
out.setUint16(offset + 1, state.fontId, true);
out.setUint32(offset + 3, placed.glyph.encoding, true);
out.setInt16(offset + 7, placed.x, true);
out.setInt16(offset + 9, placed.y, true);
out.setUint8(offset + 11, placed.value);
offset += GLYPH_RECORD_BYTES;
} else {
const id = imageId(placed);
if (id === null) continue;
out.setUint8(offset, 1);
out.setUint32(offset + 1, id, true);
out.setInt16(offset + 5, placed.x, true);
out.setInt16(offset + 7, placed.y, true);
offset += IMAGE_RECORD_BYTES;
}
}
return out.buffer;
}
/**
* Registration buffer: per font group
* [keyLen u8][key utf8][cellHeight u8][count u16]
* then per glyph
* [encoding u32][bbxX s8][inkTop u8][width u8][inkHeight u8]
* [inkHeight x row u32] (bit (ceil(width/8)*8 - 1 - col) = ink)
* mirroring GlyphAtlas.register on the Java side.
*/
function buildRegistrationBuffer(
registration: Map<FontWireState, { font: BdfFont; glyphs: Glyph[] }>,
): ArrayBuffer {
let total = 0;
const encodedKeys = new Map<FontWireState, Uint8Array>();
registration.forEach((group, state) => {
const keyBytes = utf8Encode(group.font.atlasKey ?? "");
encodedKeys.set(state, keyBytes);
total += 1 + keyBytes.length + 1 + 2;
for (const glyph of group.glyphs) {
total += 8 + 4 * glyph.bbxHeight;
}
});
const buffer = new ArrayBuffer(total);
const view = new DataView(buffer);
const bytes = new Uint8Array(buffer);
let offset = 0;
registration.forEach((group, state) => {
const keyBytes = encodedKeys.get(state)!;
view.setUint8(offset, keyBytes.length);
bytes.set(keyBytes, offset + 1);
offset += 1 + keyBytes.length;
view.setUint8(offset, state.cellHeight);
view.setUint16(offset + 1, group.glyphs.length, true);
offset += 3;
for (const glyph of group.glyphs) {
const inkTop = group.font.ascent - (glyph.bbxHeight + glyph.bbxY);
view.setUint32(offset, glyph.encoding, true);
view.setInt8(offset + 4, glyph.bbxX);
view.setUint8(offset + 5, inkTop);
view.setUint8(offset + 6, glyph.bbxWidth);
view.setUint8(offset + 7, glyph.bbxHeight);
offset += 8;
for (let row = 0; row < glyph.bbxHeight; row++) {
view.setUint32(offset, (glyph.bitmapRows[row] ?? 0) >>> 0, true);
offset += 4;
}
}
});
return buffer;
}
function utf8Encode(text: string): Uint8Array {
// TextEncoder exists in NativeScript's JS runtime; fall back to code units
// (atlas keys are ASCII font names, so both paths agree).
if (typeof TextEncoder !== "undefined") {
return new TextEncoder().encode(text);
}
const out = new Uint8Array(text.length);
for (let i = 0; i < text.length; i++) {
out[i] = text.charCodeAt(i) & 0x7f;
}
return out;
}
+87 -42
View File
@@ -24,11 +24,12 @@ export function imageFromAsciiArt(lines: readonly string[], value = 255): GrayIm
/**
* One deferred glyph draw. Text drawn through drawGlyph/drawText/
* drawTextWrapped is retained here rather than baked into the pixel buffer,
* so the compositor can later ship glyphs as display-list references instead
* of pixels. (x, y) and value are the drawGlyph arguments (y is the top of
* so the compositor can ship glyphs as on-glasses cached draws instead of
* pixels. (x, y) and value are the drawGlyph arguments (y is the top of
* the line; the baseline is y + font.ascent).
*/
export type PlacedGlyph = {
kind: "glyph";
font: BdfFont;
glyph: Glyph;
x: number;
@@ -36,16 +37,32 @@ export type PlacedGlyph = {
value: number;
};
/**
* One deferred image draw (drawImage): a color-key blit of `source` at
* (x, y), retained so icons can ship as on-glasses cached-image draws.
* `source` is flat (no deferred draws of its own) and treated as immutable
* from the moment it is placed.
*/
export type PlacedImage = {
kind: "image";
source: GrayImage;
x: number;
y: number;
};
/**
* A retained draw, always rendered on top of the raster pixels regardless of
* draw order (deferred draws keep their order among themselves). An overlay
* that must cover deferred draws below it needs its own plane (see plane.ts)
* or must suppress the covered draws.
*/
export type DeferredDraw = PlacedGlyph | PlacedImage;
export class GrayImage {
readonly width: number;
readonly height: number;
readonly pixels: Uint8Array;
/**
* Deferred glyph draws, always rendered on top of the raster pixels
* regardless of draw order. An overlay that must cover text below it needs
* its own plane (see plane.ts) or must suppress the covered draws.
*/
private readonly glyphList: PlacedGlyph[] = [];
private readonly drawList: DeferredDraw[] = [];
constructor(width: number, height: number, fill = 0) {
this.width = width;
@@ -54,24 +71,24 @@ export class GrayImage {
this.clear(fill);
}
get glyphs(): readonly PlacedGlyph[] {
return this.glyphList;
get draws(): readonly DeferredDraw[] {
return this.drawList;
}
hasGlyphs(): boolean {
return this.glyphList.length > 0;
hasDeferredDraws(): boolean {
return this.drawList.length > 0;
}
clone(): GrayImage {
const clone = new GrayImage(this.width, this.height, 0);
clone.pixels.set(this.pixels);
clone.glyphList.push(...this.glyphList);
clone.drawList.push(...this.drawList);
return clone;
}
clear(value = 0): void {
this.pixels.fill(clampByte(value));
this.glyphList.length = 0;
this.drawList.length = 0;
}
setPixel(x: number, y: number, value: number): void {
@@ -199,19 +216,19 @@ export class GrayImage {
}
}
// Carry the source's deferred glyphs along, translated into destination
// space. Membership in the copied rect is judged by the glyph's anchor
// Carry the source's deferred draws along, translated into destination
// space. Membership in the copied rect is judged by the draw's anchor
// (fine for the whole-image blits this is used for; a sub-rect blit can
// clip a glyph's overhang imperfectly).
if (source.glyphList.length) {
// clip a draw's overhang imperfectly).
if (source.drawList.length) {
const offsetX = destX - srcX;
const offsetY = destY - srcY;
for (const placed of source.glyphList) {
for (const placed of source.drawList) {
if (
placed.x >= srcX && placed.x < srcX + copyWidth &&
placed.y >= srcY && placed.y < srcY + copyHeight
) {
this.glyphList.push({ ...placed, x: placed.x + offsetX, y: placed.y + offsetY });
this.drawList.push({ ...placed, x: placed.x + offsetX, y: placed.y + offsetY });
}
}
}
@@ -292,63 +309,91 @@ export class GrayImage {
}
}
fingerprint(): string {
/** 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);
}
for (const placed of this.glyphList) {
hash = mixInt(hash, placed.font.fingerprintId);
hash = mixInt(hash, placed.glyph.encoding);
for (const placed of this.drawList) {
if (placed.kind === "glyph") {
hash = mixInt(hash, placed.font.fingerprintId);
hash = mixInt(hash, placed.glyph.encoding);
hash = mixInt(hash, placed.value);
} else {
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.x);
hash = mixInt(hash, placed.y);
hash = mixInt(hash, placed.value);
}
return `fnv:${(hash >>> 0).toString(16)}`;
return hash >>> 0;
}
fingerprint(): string {
return `fnv:${this.contentHash32().toString(16)}`;
}
/**
* Snapshot of the full image as a single 8-bit-per-pixel grayscale buffer,
* row-major and top-to-bottom, with any deferred glyphs baked in. The Java
* row-major and top-to-bottom, with any deferred draws baked in. The Java
* side turns this into whatever wire format the firmware currently expects
* (today: a 4bpp BMP), so all framing concerns stay on one side of the
* bridge.
*/
to8bppBuffer(): Uint8Array {
if (!this.glyphList.length) {
if (!this.drawList.length) {
return Uint8Array.from(this.pixels);
}
// withGlyphsBaked's buffer is a private temporary, safe to hand out.
return this.withGlyphsBaked().pixels;
// withDrawsBaked's buffer is a private temporary, safe to hand out.
return this.withDrawsBaked().pixels;
}
/**
* Record a glyph draw. Deferred: the glyph joins the image's glyph list
* Record a glyph draw. Deferred: the glyph joins the image's draw list
* (rendered above all raster pixels) instead of being baked into the
* buffer.
*/
drawGlyph(font: BdfFont, glyph: Glyph, x: number, y: number, value: number): void {
this.glyphList.push({ font, glyph, x, y, value: clampByte(value) });
this.drawList.push({ kind: "glyph", font, glyph, x, y, value: clampByte(value) });
}
/**
* This image with its glyph list rasterized into the pixel buffer. Returns
* this image unchanged when there are no glyphs, otherwise a baked copy
* with an empty glyph list.
* Record a color-key image blit (source value 0 = transparent), deferred
* like glyph draws so icons keep their identity to the wire encoder. The
* source is snapshot-flattened (its own deferred draws baked) and must not
* be mutated afterwards — pass long-lived rendered assets (icons), not
* scratch buffers. For a clipped or mutable blit, use bitBlt instead.
*/
withGlyphsBaked(): GrayImage {
if (!this.glyphList.length) return this;
drawImage(source: GrayImage, x: number, y: number): void {
if (source.width <= 0 || source.height <= 0) return;
this.drawList.push({ kind: "image", source: source.withDrawsBaked(), x, y });
}
/**
* This image with its deferred draws rasterized into the pixel buffer.
* Returns this image unchanged when there are none, otherwise a baked copy
* with an empty draw list.
*/
withDrawsBaked(): GrayImage {
if (!this.drawList.length) return this;
const baked = new GrayImage(this.width, this.height, 0);
baked.pixels.set(this.pixels);
this.bakeGlyphsInto(baked, 0, 0);
this.bakeDrawsInto(baked, 0, 0);
return baked;
}
/** Rasterize this image's glyph list into target, translated by (dx, dy). */
bakeGlyphsInto(target: GrayImage, dx: number, dy: number): void {
for (const placed of this.glyphList) {
rasterizeGlyph(target, placed.font, placed.glyph, placed.x + dx, placed.y + dy, placed.value);
/** Rasterize this image's deferred draws into target, translated by (dx, dy). */
bakeDrawsInto(target: GrayImage, dx: number, dy: number): void {
for (const placed of this.drawList) {
if (placed.kind === "glyph") {
rasterizeGlyph(target, placed.font, placed.glyph, placed.x + dx, placed.y + dy, placed.value);
} else {
target.bitBlt(placed.source, placed.x + dx, placed.y + dy, { transparentZero: true });
}
}
}
}
+29 -3
View File
@@ -1,4 +1,4 @@
import { GrayImage } from "./image";
import { GrayImage, type DeferredDraw } from "./image";
/**
* The unit apps (and the shell) submit to the compositor: instead of one
@@ -49,7 +49,7 @@ export function flattenPlanes(
const only = planes.length === 1 ? planes[0]! : undefined;
if (only && only.x === 0 && only.y === 0 && only.image.width === width && only.image.height === height) {
return only.image.withGlyphsBaked();
return only.image.withDrawsBaked();
}
const target = new GrayImage(width, height, 0);
@@ -57,11 +57,37 @@ export function flattenPlanes(
// Baking first (rather than bitBlt's glyph carry-over) is what enforces
// the plane ordering: the next plane's raster must be able to cover this
// plane's glyphs.
target.bitBlt(plane.image.withGlyphsBaked(), plane.x, plane.y, { transparentZero: true });
target.bitBlt(plane.image.withDrawsBaked(), plane.x, plane.y, { transparentZero: true });
}
return target;
}
/**
* Flatten planes and also return the frame's deferred draws (glyphs and
* images), translated into frame coordinates and in bake order (plane order,
* then each image's draw order). The flattened image has every draw baked in,
* exactly as flattenPlanes produces; the list preserves the draws' identity
* so the texture-cache pipeline can replay them as on-glasses cached draws
* (see graphics/glyph-wire.ts).
*/
export function flattenPlanesWithDraws(
planes: readonly Plane[],
size?: { width: number; height: number },
): { image: GrayImage; draws: DeferredDraw[] } {
const image = flattenPlanes(planes, size);
const draws: DeferredDraw[] = [];
for (const plane of planes) {
for (const placed of plane.image.draws) {
draws.push(
plane.x === 0 && plane.y === 0
? placed
: { ...placed, x: placed.x + plane.x, y: placed.y + plane.y },
);
}
}
return { image, draws };
}
/**
* Stable identifier for a plane stack's composited content: equal
* fingerprints mean an equal flattened frame. Feeds the same dedupe paths
+7
View File
@@ -515,6 +515,12 @@ export class FaceclawCommunicatorBridge {
fingerprint: string,
paintMs = -1,
frameId = 0,
/**
* The frame's glyph draws in surface coordinates (see
* graphics/glyph-wire.ts prepareFrameGlyphs); lets the texture-cache
* pipeline ship text as on-glasses cached draws instead of pixels.
*/
glyphs: ArrayBuffer | null = null,
): Promise<void> {
// Snapshot because the Java call is deferred; the buffer is passed as an
// ArrayBuffer, which NativeScript marshals to a ByteBuffer without the
@@ -531,6 +537,7 @@ export class FaceclawCommunicatorBridge {
fingerprint,
Math.round(nonNegativeNumber(paintMs)),
Math.round(nonNegativeNumber(frameId)),
glyphs,
);
});
}
+1 -1
View File
@@ -17,7 +17,7 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
return null;
}
const baked = image.withGlyphsBaked();
const baked = image.withDrawsBaked();
const bitmap = com.faceclaw.app.PreviewBitmapUtil.fromGray(
baked.pixels.buffer,
baked.width,
+44 -35
View File
@@ -95,6 +95,30 @@ export function makeLetterWindowIcon(letter: string): ShellChromeWindow["drawIco
};
}
/**
* Inverted (black-on-white) variant of an icon raster, for the focused tab:
* coverage becomes darkness, with full coverage mapping to SHELL_OPAQUE_BLACK
* (1) rather than 0, since 0 is the surface's transparent color key. Memoized
* per source object so the variant is a stable image the texture cache can
* key on (mode-13's inverse LUT can't reproduce this mapping exactly, so it
* is simply a separate cached image).
*/
const invertedIconCache = new WeakMap<GrayImage, GrayImage>();
function invertedIcon(icon: GrayImage): GrayImage {
let inverted = invertedIconCache.get(icon);
if (!inverted) {
inverted = new GrayImage(icon.width, icon.height, 0);
for (let i = 0; i < icon.pixels.length; i++) {
const coverage = icon.pixels[i]!;
if (coverage > 0) {
inverted.pixels[i] = Math.max(SHELL_OPAQUE_BLACK, 255 - coverage);
}
}
invertedIconCache.set(icon, inverted);
}
return inverted;
}
/** Window icon rendered from an SVG (Lucide), rendered once per size and cached. */
export function makeSvgWindowIcon(name: IconName, glyph?: string): ShellChromeWindow["drawIcon"] {
return (image, x, y, size, inverted) => {
@@ -102,21 +126,7 @@ export function makeSvgWindowIcon(name: IconName, glyph?: string): ShellChromeWi
if (!icon) return;
const dx = x + Math.max(0, ((size - icon.width) / 2) | 0);
const dy = y + Math.max(0, ((size - icon.height) / 2) | 0);
if (!inverted) {
image.bitBlt(icon, dx, dy, { transparentZero: true });
return;
}
// Invert onto a white background: coverage becomes darkness. Full coverage
// maps to SHELL_OPAQUE_BLACK (1) rather than 0, since 0 is the surface's
// transparent color key.
for (let row = 0; row < icon.height; row++) {
for (let col = 0; col < icon.width; col++) {
const coverage = icon.pixels[row * icon.width + col] ?? 0;
if (coverage > 0) {
image.setPixel(dx + col, dy + row, Math.max(SHELL_OPAQUE_BLACK, 255 - coverage));
}
}
}
image.drawImage(inverted ? invertedIcon(icon) : icon, dx, dy);
};
}
@@ -138,18 +148,7 @@ export function makeImageWindowIcon(
}
const dx = x + Math.max(0, ((size - icon.width) / 2) | 0);
const dy = y + Math.max(0, ((size - icon.height) / 2) | 0);
if (!inverted) {
image.bitBlt(icon, dx, dy, { transparentZero: true });
return;
}
for (let row = 0; row < icon.height; row++) {
for (let col = 0; col < icon.width; col++) {
const coverage = icon.pixels[row * icon.width + col] ?? 0;
if (coverage > 0) {
image.setPixel(dx + col, dy + row, Math.max(SHELL_OPAQUE_BLACK, 255 - coverage));
}
}
}
image.drawImage(inverted ? invertedIcon(icon) : icon, dx, dy);
};
}
@@ -243,8 +242,10 @@ export class ShellChromeLayer implements Layer {
}
window.drawIcon(image, x, y, iconSize, focused);
if (window.attention) {
// Black dot on the white focused tab, white dot otherwise.
image.fillRoundedRect(x + iconSize - 7, y - 1, 8, 8, focused ? SHELL_OPAQUE_BLACK : 255, 4);
// Black dot on the white focused tab, white dot otherwise. A deferred
// image (not a raster fill) so it renders above the icon, which is
// itself a deferred draw.
image.drawImage(attentionDot(focused ? SHELL_OPAQUE_BLACK : 255), x + iconSize - 7, y - 1);
}
}
@@ -287,9 +288,7 @@ export class ShellChromeLayer implements Layer {
}
const iconY = barTop + (((TOP_BAR_HEIGHT - NOTIFICATION_ICON_SIZE) / 2) | 0);
for (let index = 0; index < icons.length; index++) {
image.bitBlt(icons[index]!, iconsX + index * (NOTIFICATION_ICON_SIZE + 4), iconY, {
transparentZero: true,
});
image.drawImage(icons[index]!, iconsX + index * (NOTIFICATION_ICON_SIZE + 4), iconY);
}
}
}
@@ -354,13 +353,23 @@ function drawTrayIcons(image: GrayImage, trayIcons: GrayImage[], rightEdge: numb
for (let index = trayIcons.length - 1; index >= 0; index--) {
const icon = trayIcons[index]!;
x -= icon.width + 10;
image.bitBlt(icon, x, barTop + Math.max(0, ((TOP_BAR_HEIGHT - icon.height) / 2) | 0), {
transparentZero: true,
});
image.drawImage(icon, x, barTop + Math.max(0, ((TOP_BAR_HEIGHT - icon.height) / 2) | 0));
}
return x;
}
/** The sidebar attention marker, cached per fill value (deferred-image source). */
const attentionDots = new Map<number, GrayImage>();
function attentionDot(value: number): GrayImage {
let dot = attentionDots.get(value);
if (!dot) {
dot = new GrayImage(8, 8, 0);
dot.fillRoundedRect(0, 0, 8, 8, value, 4);
attentionDots.set(value, dot);
}
return dot;
}
const TAB_RADIUS = 6;
// How far the diversion pokes past the separator into the main area.
const TAB_EXTEND = 1;