forked from Blendman974/faceclaw
Add animations to launcher and Files app
This commit is contained in:
1 parent
7ad9fe10ad
commit
b0a98f5942
24 files changed
+1608
-738
No files matched your search
@@ -1,15 +1,31 @@
|
||||
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;
|
||||
export const DrawOp = { RECT_COPY: 2, IMAGE: 4, ROUNDED_RECT: 8 } as const;
|
||||
/**
|
||||
* 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;
|
||||
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 = (
|
||||
| { op: typeof DrawOp.ROUNDED_RECT; x: DrawValue; y: DrawValue; width: number; height: number; radius: number; background: number; border: 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 }
|
||||
) & { depth?: number };
|
||||
/** 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[];
|
||||
@@ -45,12 +61,18 @@ export function encodeDisplayList(placed: PlacedDisplayList, now = Date.now()):
|
||||
}
|
||||
integer(list.calls.length, 0, 4096); out.u16(list.calls.length);
|
||||
for (const call of list.calls) {
|
||||
out.u8(call.op); out.i16(call.depth ?? 0);
|
||||
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));
|
||||
} 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');
|
||||
@@ -68,6 +90,9 @@ export function encodeDisplayList(placed: PlacedDisplayList, now = Date.now()):
|
||||
return Uint8Array.from(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');
|
||||
@@ -79,11 +104,22 @@ export function readDisplayList(bytes: Uint8Array, offset: number, now = Date.no
|
||||
for (let i = 0; i < count; i++) { const width = r.u16(), height = r.u16(); resources.push({ width, height, pixels: r.take(width * height) }); }
|
||||
const calls: ListCall[] = [], callCount = r.u16(); integer(callCount, 0, 4096);
|
||||
for (let i = 0; i < callCount; i++) {
|
||||
const op = r.u8(), depth = r.i16();
|
||||
if (op === DrawOp.ROUNDED_RECT) calls.push({ op, depth, x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), radius: r.u16(), background: r.u8(), border: r.u8() });
|
||||
else if (op === DrawOp.IMAGE) calls.push({ op, depth, resource: r.u16(), x: r.i16(), y: r.i16(), transparent: (r.u8() & 16) !== 0 });
|
||||
else if (op === DrawOp.RECT_COPY) calls.push({ op, depth, resource: r.u16(), x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), dx: readValue(r), dy: readValue(r) });
|
||||
else throw new Error('Unsupported display-list call');
|
||||
const header = r.u8(), op = header & ~CLIPPED, depth = r.i16();
|
||||
const clip = header & CLIPPED ? { x: r.i16(), y: r.i16(), width: r.u16(), height: r.u16() } : undefined;
|
||||
const common = clip ? { depth, clip } : { depth };
|
||||
if (op === DrawOp.ROUNDED_RECT) calls.push({ op, ...common, x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), radius: r.u16(), background: r.u8(), border: r.u8() });
|
||||
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 x = readValue(r), y = 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, y, 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 } } };
|
||||
@@ -92,13 +128,23 @@ export function readDisplayList(bytes: Uint8Array, offset: number, now = Date.no
|
||||
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) };
|
||||
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.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. */
|
||||
/**
|
||||
* 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;
|
||||
@@ -112,10 +158,20 @@ export function paintDisplayList(output: Uint8Array, screen: Uint8Array, width:
|
||||
};
|
||||
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);
|
||||
if (c.op === DrawOp.ROUNDED_RECT) {
|
||||
// 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 (x + xx < 0 || y + yy < 0 || x + xx >= width || y + yy >= height || !inside(xx, yy, c.width, c.height, c.radius)) continue;
|
||||
if (!writable(x + xx, y + yy) || !inside(xx, yy, c.width, c.height, c.radius)) continue;
|
||||
const p = (y + yy) * width + x + xx;
|
||||
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);
|
||||
}
|
||||
@@ -126,7 +182,7 @@ export function paintDisplayList(output: Uint8Array, screen: Uint8Array, width:
|
||||
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 (x + xx < 0 || y + yy < 0 || x + xx >= width || y + yy >= height || sx + xx < 0 || sy + yy < 0 || sx + xx >= source.width || sy + yy >= source.height) continue;
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user