@margoengine/headless 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/frames.js ADDED
@@ -0,0 +1,70 @@
1
+ import { n as e, t } from "./errors-CDgOgzCd.js";
2
+ import { a as n, i as r, n as i, r as a } from "./readback-HJLttuJt.js";
3
+ //#region src/frames.ts
4
+ function o(o, { depth: s = 3, signal: c } = {}) {
5
+ if (!Number.isInteger(s) || s < 1 || s > 16) throw e("createFrameReader depth is a whole number from 1 to 16", "headless");
6
+ c?.throwIfAborted();
7
+ let l = [], u = [], d = /* @__PURE__ */ new Set(), f = !1, p = () => {
8
+ if (c?.throwIfAborted(), f) throw t("state", "The frame reader is disposed", "headless");
9
+ }, m = (e) => {
10
+ d.delete(e), e.destroy();
11
+ }, h = () => {
12
+ if (!f) {
13
+ f = !0, c?.removeEventListener("abort", h);
14
+ for (let e of d) e.destroy();
15
+ d.clear(), l.length = u.length = 0;
16
+ }
17
+ };
18
+ c?.addEventListener("abort", h, { once: !0 });
19
+ let g = (e, t) => {
20
+ for (let n = u.pop(); n; n = u.pop()) {
21
+ if (n.device === e && n.size === t) return n;
22
+ m(n.buffer);
23
+ }
24
+ let n = a(e, t);
25
+ return d.add(n), {
26
+ buffer: n,
27
+ device: e,
28
+ size: t
29
+ };
30
+ }, _ = async ({ buffer: e, device: t, size: r, copy: i, mapped: a }) => {
31
+ try {
32
+ await a, p();
33
+ let o = n(e.getMappedRange(), i);
34
+ return e.unmap(), u.push({
35
+ buffer: e,
36
+ device: t,
37
+ size: r
38
+ }), {
39
+ width: i.width,
40
+ height: i.height,
41
+ data: o
42
+ };
43
+ } catch (t) {
44
+ throw m(e), p(), t;
45
+ }
46
+ };
47
+ return {
48
+ depth: s,
49
+ get pending() {
50
+ return l.length;
51
+ },
52
+ async push() {
53
+ p();
54
+ let e = r(o, "createFrameReader"), t = g(e.device, e.size), n = i(e, t.buffer), a = t.buffer.mapAsync(1);
55
+ return a.catch(() => {}), l.push({
56
+ ...t,
57
+ copy: n,
58
+ mapped: a
59
+ }), l.length >= s ? _(l.shift()) : void 0;
60
+ },
61
+ async *drain() {
62
+ p();
63
+ for (let e = l.shift(); e; e = l.shift()) yield await _(e);
64
+ },
65
+ dispose: h,
66
+ [Symbol.dispose]: h
67
+ };
68
+ }
69
+ //#endregion
70
+ export { o as createFrameReader };
package/dist/gif.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ // @ts-ignore optional peer: its types apply when it is installed
2
+ import { type GIFOptions } from '@margoengine/exporters/capture';
3
+ import type { HeadlessPixels } from './index.js';
4
+ /** GIF frame: RGBA pixels and optionally a per-frame delay. */
5
+ export interface GIFFrame extends HeadlessPixels {
6
+ /** Seconds, 0.02..655.35. Default 1/fps. */
7
+ duration?: number;
8
+ }
9
+ /** Options for animated GIF output. */
10
+ export interface EncodeGIFOptions extends GIFOptions {
11
+ }
12
+ /**
13
+ * Encodes renderFrames output or any sync/async RGBA stream to GIF89a bytes.
14
+ * Defaults: fps 30, no dithering, loop 0 (forever), no signal. loop -1 plays
15
+ * once; positive values repeat that many times. Explicit frame durations take
16
+ * precedence over fps. Pixels below alpha 128 are transparent; others opaque.
17
+ * Uses the same quantization as @margoengine/exporters/palette and the existing
18
+ * exporter LZW writer, keeping at most one raw frame plus compressed output.
19
+ */
20
+ export declare function encodeGIF(frames: AsyncIterable<GIFFrame> | Iterable<GIFFrame>, options?: EncodeGIFOptions): Promise<Uint8Array>;
package/dist/gif.js ADDED
@@ -0,0 +1,7 @@
1
+ import { encodeGIFFrames as e } from "@margoengine/exporters/capture";
2
+ //#region src/gif.ts
3
+ function t(t, n = {}) {
4
+ return e(t, n);
5
+ }
6
+ //#endregion
7
+ export { t as encodeGIF };
@@ -0,0 +1,66 @@
1
+ import type { Engine } from '@margoengine/core';
2
+ /** The part of a WebGPU implementation {@link installGPU} needs. `getPreferredCanvasFormat` is optional because Node
3
+ * bindings have no canvas to prefer a format for. */
4
+ export interface GPUBinding {
5
+ requestAdapter(options?: GPURequestAdapterOptions): Promise<GPUAdapter | null>;
6
+ getPreferredCanvasFormat?(): GPUTextureFormat;
7
+ readonly wgslLanguageFeatures?: unknown;
8
+ }
9
+ /** Options of {@link installGPU}. */
10
+ export interface InstallGPUOptions {
11
+ /** The WebGPU implementation to render with: `create([])` from a Node binding, or `navigator.gpu` in a browser. */
12
+ gpu: GPUBinding;
13
+ /** Globals the binding exports (`GPUBufferUsage` and so on). A name already on `globalThis` stays as it is; a flag
14
+ * table neither has gets the specification's values. Default none. */
15
+ globals?: Readonly<Record<string, unknown>>;
16
+ }
17
+ /** What {@link installGPU} returns. `dispose()` (or `using`) restores `navigator.gpu` and removes the added globals. */
18
+ export interface InstalledGPU {
19
+ /** The object now at `navigator.gpu`. */
20
+ readonly gpu: GPU;
21
+ /** Restores what was there before. Safe to call twice. */
22
+ dispose(): void;
23
+ /** `dispose()`, for `using`. */
24
+ [Symbol.dispose](): void;
25
+ }
26
+ /**
27
+ * Makes `gpu` the WebGPU implementation core sees: sets `navigator.gpu` and the `GPU*` flag globals a runtime without
28
+ * WebGPU lacks, and supplies `getPreferredCanvasFormat` (`'rgba8unorm'`) when the binding has none. Call it before
29
+ * `Engine.create`; nothing happens on import.
30
+ */
31
+ export declare function installGPU({ gpu, globals }: InstallGPUOptions): InstalledGPU;
32
+ /** Options of {@link createHeadlessCanvas}. */
33
+ export interface HeadlessCanvasOptions {
34
+ /** Width of the image in pixels, a whole number from 1. Required. */
35
+ width: number;
36
+ /** Height of the image in pixels, a whole number from 1. Required. */
37
+ height: number;
38
+ }
39
+ /**
40
+ * A stand-in for `Engine.create`'s canvas: `width`, `height`, `clientWidth` and `clientHeight` (0, so core keeps
41
+ * `width` and `height` as the image size) and `getContext('webgpu')`. It has no other element members, so use it with
42
+ * `Engine` and packages that take an engine, not with `createStage`, which needs a page.
43
+ */
44
+ export declare function createHeadlessCanvas({ width, height }: HeadlessCanvasOptions): HTMLCanvasElement;
45
+ /** RGBA pixels from {@link readPixels}: 8 bits per channel, straight alpha, top row first, no padding. */
46
+ export interface HeadlessPixels {
47
+ width: number;
48
+ height: number;
49
+ data: Uint8Array;
50
+ }
51
+ /** Options of {@link readPixels} and {@link toPNG}. */
52
+ export interface HeadlessReadOptions {
53
+ /** Stops waiting for the GPU; the promise rejects with the signal's reason. Default none. */
54
+ signal?: AbortSignal;
55
+ }
56
+ /**
57
+ * Reads the last frame of an engine on a {@link createHeadlessCanvas} canvas. Call `engine.render()` first; this draws
58
+ * nothing itself. A context configured `opaque` (the default) reads alpha 255, as a page would show it. The readback
59
+ * buffer stays on the canvas for the next read of the same size. To read every frame of a film without waiting for the
60
+ * GPU after each one, use `createFrameReader` from `@margoengine/headless/frames`.
61
+ */
62
+ export declare function readPixels(engine: Engine, { signal }?: HeadlessReadOptions): Promise<HeadlessPixels>;
63
+ /** Encodes RGBA pixels as an 8-bit PNG file (each row filtered against the one above). */
64
+ export declare function encodePNG({ width, height, data }: HeadlessPixels): Promise<Uint8Array>;
65
+ /** The last frame of an engine on a {@link createHeadlessCanvas} canvas as PNG file bytes. See {@link readPixels}. */
66
+ export declare function toPNG(engine: Engine, options?: HeadlessReadOptions): Promise<Uint8Array>;
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { t as createHeadlessCanvas, n as encodePNG, r as installGPU, i as readPixels, a as toPNG } from "./src-VufTH2fo.js";