Files
unkernel/test/display.test.ts
Kotaro Chikuba c554197790 js: convert XRGB8888 framebuffer pixels with Wasm SIMD (#188)
* js: convert XRGB8888 framebuffer pixels with Wasm SIMD

* js: keep the pixel SIMD scratch buffer off address 0

* js: move the frame conversion test to test/display.test.ts and check the embedded module

The test needs no kernel. It now also covers readRGBA()'s packed and padded paths and a shared source, and rebuilds pixel-simd.zig where zig 0.16 is on PATH to compare it with the embedded bytes.

---------

Co-authored-by: Pooya Parsa <pooya@pi0.io>
2026-12-05 00:49:32 +01:00

95 lines
4.3 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (C) 2026 mizchi
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
// XRGB8888 to RGBA conversion (js/core/display.ts, js/core/pixel-simd.ts)
// without a kernel, and the embedded SIMD module against its Zig source.
//
// node --test test/display.test.ts
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { test } from "node:test";
import { fileURLToPath } from "node:url";
import { DisplayHost, FOURCC_XRGB8888, xrgbToRgba } from "../js/core/display.ts";
import { pixelSimdWasm } from "../js/core/pixel-simd.ts";
const root = fileURLToPath(new URL("..", import.meta.url));
/** Packed XRGB8888 pixels with distinct channels, and their RGBA. */
function pattern(width: number, height: number, buffer?: ArrayBufferLike, offset = 0): { input: Uint8Array; expected: Uint8ClampedArray } {
const n = width * height * 4;
const input = new Uint8Array(buffer ?? new ArrayBuffer(n), offset, n);
const expected = new Uint8ClampedArray(n);
for (let i = 0; i < n; i += 4) {
input[i] = (i >>> 2) & 255;
input[i + 1] = (i >>> 6) & 255;
input[i + 2] = (i >>> 12) & 255;
input[i + 3] = (i >>> 3) & 255;
expected[i] = input[i + 2]!;
expected[i + 1] = input[i + 1]!;
expected[i + 2] = input[i]!;
expected[i + 3] = 255;
}
return { input, expected };
}
test("packed framebuffer conversion covers a scratch boundary, tail pixels and in-place output", () => {
// 1025 x 257 pixels: more than the 1 MiB scratch, and not a multiple of 4.
const width = 1025, height = 257;
const { input, expected } = pattern(width, height);
assert.deepEqual(xrgbToRgba(input, width, height), expected);
const out = new Uint8ClampedArray(input.buffer);
assert.equal(xrgbToRgba(input, width, height, out), out);
assert.deepEqual(out, expected);
});
test("packed framebuffer conversion from a shared buffer at an offset, into a reused output at an offset", () => {
const width = 1023, height = 515;
const { input, expected } = pattern(width, height, new SharedArrayBuffer(width * height * 4 + 32), 12);
const out = new Uint8ClampedArray(new ArrayBuffer(expected.length + 16), 8, expected.length).fill(7);
assert.equal(xrgbToRgba(input, width, height, out), out);
assert.deepEqual(out, expected);
// Small frames take the scalar loop only.
const small = pattern(31, 3);
assert.deepEqual(xrgbToRgba(small.input, 31, 3), small.expected);
});
test("readRGBA() converts a packed scanout and one with padded rows", () => {
for (const pad of [0, 12]) {
const width = 1027, height = 300, stride = width * 4 + pad;
const { input, expected } = pattern(width, height);
const px = new Uint8Array(stride * height);
for (let y = 0; y < height; y++) px.set(input.subarray(y * width * 4, (y + 1) * width * 4), y * stride);
const display = new DisplayHost({ memory: () => new WebAssembly.Memory({ initial: 1 }) });
display.setScanout({ mode: { width, height, stride, format: FOURCC_XRGB8888 }, pixels: () => px });
const out = new Uint8ClampedArray(expected.length);
const frame = display.readRGBA(out);
assert.ok(frame, `pad ${pad}`);
assert.equal(frame.data, out);
assert.deepEqual([frame.width, frame.height], [width, height]);
assert.deepEqual(frame.data, expected, `pad ${pad}`);
}
});
const zig = process.env.ZIG ?? "zig";
const zigVersion = spawnSync(zig, ["version"], { encoding: "utf8" });
const noZig = zigVersion.status !== 0 ? `no ${zig} on PATH` :
!zigVersion.stdout.startsWith("0.16.") ? `zig ${zigVersion.stdout.trim()}, not 0.16` : false;
test("the embedded pixel SIMD module is the build of pixel-simd.zig", { skip: noZig }, () => {
const dir = mkdtempSync(path.join(tmpdir(), "pixel-simd-"));
try {
const out = path.join(dir, "pixel-simd.wasm");
// The command in js/core/pixel-simd.ts.
const r = spawnSync(zig, ["build-exe", "js/core/pixel-simd.zig", "-target", "wasm32-freestanding", "-mcpu=mvp+simd128",
"-O", "ReleaseFast", "-fno-entry", "-fstrip", "-rdynamic", "--stack", "0", "--global-base=16",
"--max-memory=1114112", `-femit-bin=${out}`], { cwd: root, encoding: "utf8" });
assert.equal(r.status, 0, r.stderr);
assert.deepEqual(new Uint8Array(readFileSync(out)), pixelSimdWasm);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});