Skip to content

Code-first video & image, rendered natively on WebGPU.

3.8 KB
Raw Blame
import { premultiplyPixels } from "../color.js";
import { toLocalPath } from "../local-path.js";
import type { RenderContextValue } from "../render-context.js";
import { textureCache } from "../texture-cache.js";
import { createImageTexture } from "./image-texture.js";
import type { ImageNodeProps } from "./types.js";

interface LoadedImageData {
	width: number;
	height: number;
	bitmap?: ImageBitmap;
	buffer?: Uint8Array;
}

class ImageLoader {
	private pending = new Map<string, Promise<LoadedImageData>>();

	async load(src: string, isHeadless: boolean): Promise<LoadedImageData> {
		const key = `${src}-${isHeadless}`;
		let promise = this.pending.get(key);
		if (!promise) {
			promise = (async () => {
				if (
					typeof src === "string" &&
					(/\.(json|lottie)(\?.*)?$/i.test(src) ||
						src.startsWith("data:application/json"))
				) {
					throw new Error(
						`Unsupported image format for '${src}'. Lottie vector animations must be rendered with Layer.lottie(), not Layer.image().`,
					);
				}
				let arrayBuffer: ArrayBuffer;
				const isRemote =
					src.startsWith("http://") ||
					src.startsWith("https://") ||
					src.startsWith("data:");

				if (isRemote || !isHeadless) {
					const response = await fetch(src);
					if (!response.ok) {
						throw new Error(`Failed to fetch image: ${src}`);
					}
					arrayBuffer = await response.arrayBuffer();
				} else {
					// Local filesystem file in headless Node.js
					const filePath = (await toLocalPath(src)) ?? src;
					const fs = await import(/* webpackIgnore: true */ "node:fs/promises");
					const fileBuf = await fs.readFile(filePath);
					arrayBuffer = fileBuf.buffer.slice(
						fileBuf.byteOffset,
						fileBuf.byteOffset + fileBuf.byteLength,
					);
				}

				if (isHeadless) {
					const sharp = (await import(/* webpackIgnore: true */ "sharp"))
						.default;
					const { data, info } = await sharp(Buffer.from(arrayBuffer))
						.ensureAlpha()
						.raw()
						.toBuffer({ resolveWithObject: true });
					// Decoded PNG/WebP is straight alpha; the compositor expects premultiplied.
					return {
						width: info.width,
						height: info.height,
						buffer: premultiplyPixels(new Uint8Array(data)),
					};
				}

				const blob = new Blob([arrayBuffer]);
				const bitmap = await createImageBitmap(blob);
				return { width: bitmap.width, height: bitmap.height, bitmap };
			})();
			this.pending.set(key, promise);
			promise.catch((err) => {
				this.pending.delete(key);
				console.error(`Failed to load image texture from ${src}:`, err);
			});
		}
		return promise;
	}
}

export const imageLoader = new ImageLoader();

export async function drawImageNode(
	ctx: RenderContextValue,
	pass: GPURenderPassEncoder,
	props: ImageNodeProps,
): Promise<void> {
	if (
		typeof props.src === "string" &&
		(/\.(json|lottie)(\?.*)?$/i.test(props.src) ||
			props.src.startsWith("data:application/json"))
	) {
		console.warn(
			`[ImageRenderer] '${props.src}' is a Lottie vector animation, not a static image. Use Layer.lottie() instead.`,
		);
	}
	let tex = textureCache.acquire(props.src);

	const isHeadless =
		props.isHeadless ??
		(typeof window === "undefined" ||
			Boolean(
				(globalThis as { __IS_HEADLESS_RENDERER__?: boolean })
					.__IS_HEADLESS_RENDERER__,
			));

	if (!tex) {
		try {
			const data = await imageLoader.load(props.src, isHeadless);

			// Check the cache again in case another concurrent render task already created and cached the texture!
			tex = textureCache.acquire(props.src);
			if (!tex) {
				tex = createImageTexture(ctx.device, data);

				textureCache.set(props.src, tex, ctx.device);
			}
		} catch (err) {
			if (isHeadless) {
				throw err;
			}
			console.error("Failed to load image texture:", err);
			return;
		}
	}

	ctx.renderer.drawTexture(pass, tex, props.dstRect, {
		opacity: props.opacity ?? 1,
		transform: props.matrix,
	});

	textureCache.release(props.src);
}