mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
229 lines
15 KiB
TypeScript
229 lines
15 KiB
TypeScript
import { DrawBytes, encodeValue, evaluate, integer, readValue, type DrawValue } from './draw-expression';
|
|
import type { GrayImage } from './image';
|
|
|
|
/** Local resources are assigned firmware cache IDs by Kotlin. SCREEN reads the uncomposed frame. */
|
|
export const SCREEN = 65535;
|
|
/**
|
|
* Firmware opcodes, plus DRAWS: a bridge-only call that replays glyph and icon
|
|
* draws (from glyph-wire's encodeReplayDraws) as firmware image/text calls.
|
|
*/
|
|
export const DrawOp = { RECT_COPY: 2, IMAGE: 4, ROUNDED_RECT: 8, CLEAR: 9, DRAWS: 32 } as const;
|
|
export const DISPLAY_LIST_RECORD = 7;
|
|
/** Bridge opcode bit: a clip rect follows the call's depth. */
|
|
const CLIPPED = 128;
|
|
/** Bridge sentinel for a rounded rect without an outside color. */
|
|
const NO_OUTSIDE = 16;
|
|
export type ListImage = { readonly width: number; readonly height: number; readonly pixels: Uint8Array };
|
|
/** Revision 35: a call draws only inside its clip, in list coordinates (and moves with its depth). */
|
|
export type ListClip = { readonly x: number; readonly y: number; readonly width: number; readonly height: number };
|
|
export type ListCall = (
|
|
/**
|
|
* The background max-blends (0 leaves the interior alone); border 16 draws no
|
|
* outline. `outside` (revision 36) colors the box's pixels outside the
|
|
* rounded shape, cutting what is underneath to the corners.
|
|
*/
|
|
| { op: typeof DrawOp.ROUNDED_RECT; x: DrawValue; y: DrawValue; width: number; height: number; radius: number; background: number; border: number; outside?: number }
|
|
| { op: typeof DrawOp.IMAGE; resource: number; x: number; y: number; transparent: boolean }
|
|
| { op: typeof DrawOp.RECT_COPY; resource: number; x: DrawValue; y: DrawValue; width: number; height: number; dx: DrawValue; dy: DrawValue }
|
|
/** Fill with a 4-bit color: the clip rect when there is one, else the whole screen. */
|
|
| { op: typeof DrawOp.CLEAR; color: number }
|
|
/**
|
|
* Glyph and icon draws offset by (x, y). `records`/`count` are the wire form;
|
|
* `source` holds the same draws for software painting (a decoded list has none).
|
|
*/
|
|
| { op: typeof DrawOp.DRAWS; x: DrawValue; y: DrawValue; records: Uint8Array; count: number; source?: GrayImage }
|
|
) & { depth?: number; clip?: ListClip };
|
|
export type DisplayList = {
|
|
readonly resources: readonly ListImage[];
|
|
readonly calls: readonly ListCall[];
|
|
/** Stable identity plus JS clock origin. Native binds ELAPSED again for each PRESENT. */
|
|
/** Set by the decoder: the local PRESENT origin for software replay. */
|
|
readonly presentedAt?: number;
|
|
readonly timeline?: { readonly token: number; readonly startedAt: number };
|
|
};
|
|
export type PlacedDisplayList = { displayList: DisplayList; x: number; y: number; width: number; height: number; depth: number };
|
|
|
|
/**
|
|
* Growable byte buffer. Lists are encoded on every frame that carries one, and
|
|
* twice more for its fingerprint and the preview; resources (whole menu rows,
|
|
* scroll strips) are tens of KB, so they go in with one set() rather than a
|
|
* push per byte.
|
|
*/
|
|
class Writer {
|
|
private buffer = new Uint8Array(256);
|
|
length = 0;
|
|
private reserve(count: number): void {
|
|
const needed = this.length + count;
|
|
if (needed <= this.buffer.length) return;
|
|
let size = this.buffer.length * 2;
|
|
while (size < needed) size *= 2;
|
|
const grown = new Uint8Array(size);
|
|
grown.set(this.buffer.subarray(0, this.length));
|
|
this.buffer = grown;
|
|
}
|
|
u8(n: number): void { integer(n, 0, 255); this.reserve(1); this.buffer[this.length++] = n; }
|
|
i8(n: number): void { this.u8(integer(n, -128, 127) & 255); }
|
|
u16(n: number): void { integer(n, 0, 65535); this.reserve(2); this.buffer[this.length++] = n & 255; this.buffer[this.length++] = n >>> 8; }
|
|
i16(n: number): void { this.u16(integer(n, -32768, 32767) & 65535); }
|
|
u32(n: number): void { integer(n, 0, 4294967295); this.u16(n & 65535); this.u16(n >>> 16); }
|
|
raw(bytes: ArrayLike<number>): void { this.reserve(bytes.length); this.buffer.set(bytes, this.length); this.length += bytes.length; }
|
|
/** The written bytes, in a buffer of their own. */
|
|
bytes(): Uint8Array { return this.buffer.slice(0, this.length); }
|
|
/** A view of the written bytes; valid until the next write. */
|
|
view(): Uint8Array { return this.buffer.subarray(0, this.length); }
|
|
}
|
|
|
|
/** Length-delimited bridge envelope; call operands use fixed widths except extended x/y. */
|
|
export function encodeDisplayList(placed: PlacedDisplayList, now = Date.now()): Uint8Array {
|
|
const { displayList: list } = placed, out = new Writer();
|
|
integer(placed.width, 1, 640); integer(placed.height, 1, 480);
|
|
out.i16(placed.x); out.i16(placed.y); out.u16(placed.width); out.u16(placed.height); out.i8(placed.depth);
|
|
out.u32(list.timeline?.token ?? 0);
|
|
out.u32(list.timeline ? Math.min(2147483647, Math.max(0, Math.trunc(now - list.timeline.startedAt))) : 0);
|
|
integer(list.resources.length, 0, 256); out.u16(list.resources.length);
|
|
for (const image of list.resources) {
|
|
integer(image.width, 1, 640); integer(image.height, 1, 480);
|
|
if (image.pixels.length !== image.width * image.height || 5 + Math.ceil(image.width / 2) * image.height > 65536) throw new Error('Invalid display-list image');
|
|
out.u16(image.width); out.u16(image.height); out.raw(image.pixels);
|
|
}
|
|
integer(list.calls.length, 0, 4096); out.u16(list.calls.length);
|
|
for (const call of list.calls) {
|
|
out.u8(call.op | (call.clip ? CLIPPED : 0)); out.i16(call.depth ?? 0);
|
|
integer(placed.depth + (call.depth ?? 0), -128, 127);
|
|
if (call.clip) { out.i16(call.clip.x); out.i16(call.clip.y); out.u16(call.clip.width); out.u16(call.clip.height); }
|
|
if (call.op === DrawOp.ROUNDED_RECT) {
|
|
out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y));
|
|
out.u16(integer(call.width, 1, 640)); out.u16(integer(call.height, 1, 480)); out.u16(call.radius);
|
|
out.u8(integer(call.background, 0, 15)); out.u8(integer(call.border, 0, 16));
|
|
out.u8(call.outside === undefined ? NO_OUTSIDE : integer(call.outside, 0, 15));
|
|
} else if (call.op === DrawOp.CLEAR) {
|
|
out.u8(integer(call.color, 0, 15));
|
|
} else if (call.op === DrawOp.DRAWS) {
|
|
out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y));
|
|
out.u16(call.count); out.raw(call.records);
|
|
} else {
|
|
if (call.resource !== SCREEN) integer(call.resource, 0, list.resources.length - 1);
|
|
if (call.op === DrawOp.RECT_COPY && call.resource !== SCREEN && [call.x, call.y].some(v => typeof v === 'number' && v < 0)) throw new Error('Negative image source coordinate');
|
|
if (call.op === DrawOp.IMAGE && call.resource === SCREEN) throw new Error('Use rectCopy to read SCREEN');
|
|
out.u16(call.resource);
|
|
if (call.op === DrawOp.IMAGE) { out.i16(call.x); out.i16(call.y); out.u8(call.transparent ? 31 : 15); }
|
|
else {
|
|
out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y)); out.u16(integer(call.width, 1, 640)); out.u16(integer(call.height, 1, 480));
|
|
out.raw(encodeValue(call.dx)); out.raw(encodeValue(call.dy));
|
|
}
|
|
}
|
|
}
|
|
if (out.length > 4 * 1024 * 1024) throw new Error('Display list exceeds bridge limit');
|
|
const header = new Writer(); header.u8(DISPLAY_LIST_RECORD); header.u32(out.length); header.raw(out.view());
|
|
return header.bytes();
|
|
}
|
|
|
|
/** Replay-record sizes by tag, as in glyph-wire's frame draw buffer. */
|
|
const RECORD_BYTES: Record<number, number> = { 0: 12, 1: 9 };
|
|
|
|
export function readDisplayList(bytes: Uint8Array, offset: number, now = Date.now()): { placed: PlacedDisplayList; end: number } {
|
|
const envelope = new DrawBytes(bytes.subarray(offset));
|
|
if (envelope.u8() !== DISPLAY_LIST_RECORD) throw new Error('Invalid display-list record');
|
|
const length = envelope.u32(); integer(length, 0, 4 * 1024 * 1024);
|
|
const r = new DrawBytes(envelope.take(length));
|
|
const x = r.i16(), y = r.i16(), width = r.u16(), height = r.u16(), depth = r.u8() << 24 >> 24;
|
|
const token = r.u32(), elapsed = r.u32(), count = r.u16(); integer(count, 0, 256);
|
|
const resources: ListImage[] = [];
|
|
for (let i = 0; i < count; i++) { const w = r.u16(), h = r.u16(); resources.push({ width: w, height: h, pixels: r.take(w * h) }); }
|
|
const calls: ListCall[] = [], callCount = r.u16(); integer(callCount, 0, 4096);
|
|
for (let i = 0; i < callCount; i++) {
|
|
const header = r.u8(), op = header & ~CLIPPED, callDepth = r.i16();
|
|
const clip = header & CLIPPED ? { x: r.i16(), y: r.i16(), width: r.u16(), height: r.u16() } : undefined;
|
|
const common = clip ? { depth: callDepth, clip } : { depth: callDepth };
|
|
if (op === DrawOp.ROUNDED_RECT) {
|
|
const call = { op, ...common, x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), radius: r.u16(), background: r.u8(), border: r.u8() };
|
|
const outside = r.u8();
|
|
calls.push(outside === NO_OUTSIDE ? call : { ...call, outside });
|
|
}
|
|
else if (op === DrawOp.IMAGE) calls.push({ op, ...common, resource: r.u16(), x: r.i16(), y: r.i16(), transparent: (r.u8() & 16) !== 0 });
|
|
else if (op === DrawOp.RECT_COPY) calls.push({ op, ...common, resource: r.u16(), x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), dx: readValue(r), dy: readValue(r) });
|
|
else if (op === DrawOp.CLEAR) calls.push({ op, ...common, color: r.u8() });
|
|
else if (op === DrawOp.DRAWS) {
|
|
const drawX = readValue(r), drawY = readValue(r), records = r.u16(), start = r.offset;
|
|
for (let n = 0; n < records; n++) {
|
|
const size = RECORD_BYTES[r.bytes[r.offset]!];
|
|
if (!size) throw new Error('Unsupported display-list draw record');
|
|
r.take(size);
|
|
}
|
|
calls.push({ op, ...common, x: drawX, y: drawY, count: records, records: r.bytes.slice(start, r.offset) });
|
|
} else throw new Error('Unsupported display-list call');
|
|
}
|
|
if (r.offset !== length) throw new Error('Trailing display-list bytes');
|
|
const placed = { x, y, width, height, depth, displayList: { resources, calls, presentedAt: now, timeline: { token, startedAt: now - elapsed } } };
|
|
// The writer is also the shared structural validator for software preview records.
|
|
encodeDisplayList(placed, now);
|
|
return { placed, end: offset + envelope.offset };
|
|
}
|
|
|
|
/**
|
|
* The list at `factor` brightness. Replayed icons cannot be dimmed on the
|
|
* glasses, so a list with DRAWS calls becomes empty instead: the dimmed frame
|
|
* underneath then shows its final state.
|
|
*/
|
|
export function dimDisplayList(list: DisplayList, factor: number): DisplayList {
|
|
if (list.calls.some(c => c.op === DrawOp.DRAWS)) return { ...list, calls: [] };
|
|
const dim = (v: number) => v === 0 ? 0 : Math.max(1, Math.round(v * factor));
|
|
return { ...list, resources: list.resources.map(r => ({ ...r, pixels: r.pixels.map(dim) })),
|
|
calls: list.calls.map(c => c.op === DrawOp.ROUNDED_RECT ? { ...c, background: Math.min(15, Math.round(c.background * factor)), border: c.border === 16 ? 16 : Math.min(15, Math.round(c.border * factor)),
|
|
...(c.outside === undefined ? {} : { outside: Math.min(15, Math.round(c.outside * factor)) }) }
|
|
: c.op === DrawOp.CLEAR ? { ...c, color: Math.min(15, Math.round(c.color * factor)) } : c) };
|
|
}
|
|
|
|
/**
|
|
* Fallback compositor; native preview and glasses replay the compiled list with their existing timers.
|
|
* A DRAWS call paints from its `source`, so a decoded list skips them.
|
|
*/
|
|
export function paintDisplayList(output: Uint8Array, screen: Uint8Array, width: number, height: number, placed: PlacedDisplayList, right = false, now = Date.now()): void {
|
|
const q = (n: number) => Math.min(15, (n + 8) >> 4) * 16;
|
|
const list = placed.displayList;
|
|
const elapsed = list.timeline ? Math.max(0, (list.presentedAt ?? now) - list.timeline.startedAt) : 0;
|
|
const presentTime = list.presentedAt === undefined ? 0 : Math.max(0, now - list.presentedAt);
|
|
const inside = (x: number, y: number, w: number, h: number, radius: number) => {
|
|
if (x < 0 || y < 0 || x >= w || y >= h) return false;
|
|
const r = Math.min(radius, Math.floor(w / 2), Math.floor(h / 2));
|
|
const dx = Math.max(0, 2 * r - (2 * Math.min(x, w - 1 - x) + 1)), dy = Math.max(0, 2 * r - (2 * Math.min(y, h - 1 - y) + 1));
|
|
return dx * dx + dy * dy <= 4 * r * r;
|
|
};
|
|
for (const c of placed.displayList.calls) {
|
|
const depth = placed.depth + (c.depth ?? 0), shift = right ? -Math.floor((depth + 1) / 2) : Math.floor(depth / 2);
|
|
// Writable region in screen pixels: the clip moves with the call's depth, like its content.
|
|
const clip = c.clip ? { x: placed.x + c.clip.x + shift, y: placed.y + c.clip.y, width: c.clip.width, height: c.clip.height }
|
|
: { x: 0, y: 0, width, height };
|
|
const writable = (x: number, y: number) => x >= 0 && y >= 0 && x < width && y < height &&
|
|
x >= clip.x && y >= clip.y && x < clip.x + clip.width && y < clip.y + clip.height;
|
|
if (c.op === DrawOp.CLEAR) {
|
|
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) if (writable(x, y)) output[y * width + x] = c.color * 16;
|
|
} else if (c.op === DrawOp.DRAWS) {
|
|
const at = (v: DrawValue) => evaluate(v, elapsed, presentTime).value;
|
|
c.source?.paintReplayDraws(output, width, height, placed.x + at(c.x) + shift, placed.y + at(c.y), clip);
|
|
} else if (c.op === DrawOp.ROUNDED_RECT) {
|
|
const x = placed.x + evaluate(c.x, elapsed, presentTime).value + shift, y = placed.y + evaluate(c.y, elapsed, presentTime).value;
|
|
for (let yy = 0; yy < c.height; yy++) for (let xx = 0; xx < c.width; xx++) {
|
|
if (!writable(x + xx, y + yy)) continue;
|
|
const p = (y + yy) * width + x + xx;
|
|
if (!inside(xx, yy, c.width, c.height, c.radius)) {
|
|
if (c.outside !== undefined) output[p] = c.outside * 16;
|
|
continue;
|
|
}
|
|
output[p] = c.border !== 16 && !inside(xx - 1, yy - 1, c.width - 2, c.height - 2, Math.max(0, c.radius - 1)) ? c.border * 16 : Math.max(output[p], c.background * 16);
|
|
}
|
|
} else {
|
|
const source = c.resource === SCREEN ? { width, height, pixels: screen } : placed.displayList.resources[c.resource];
|
|
const w = c.op === DrawOp.IMAGE ? source.width : c.width, h = c.op === DrawOp.IMAGE ? source.height : c.height;
|
|
const at = (v: DrawValue) => evaluate(v, elapsed, presentTime).value;
|
|
const sx = c.op === DrawOp.IMAGE ? 0 : at(c.x) + (c.resource === SCREEN ? placed.x : 0), sy = c.op === DrawOp.IMAGE ? 0 : at(c.y) + (c.resource === SCREEN ? placed.y : 0);
|
|
const x = placed.x + (c.op === DrawOp.IMAGE ? c.x : at(c.dx)) + shift, y = placed.y + (c.op === DrawOp.IMAGE ? c.y : at(c.dy));
|
|
for (let yy = 0; yy < h; yy++) for (let xx = 0; xx < w; xx++) {
|
|
if (!writable(x + xx, y + yy) || sx + xx < 0 || sy + yy < 0 || sx + xx >= source.width || sy + yy >= source.height) continue;
|
|
const v = q(source.pixels[(sy + yy) * source.width + sx + xx]);
|
|
if (c.op !== DrawOp.IMAGE || !c.transparent || v !== 0) output[(y + yy) * width + x + xx] = v;
|
|
}
|
|
}
|
|
}
|
|
}
|