/texture-bridge:receiving-shared-texturesfrom texture-bridge
Implements the receiving side of @napolab/texture-bridge shared-texture frames in an Electron renderer. Covers consuming forwardFrames/forwardSharedTexture output, displaying createSharedTextureReceiver frames, building a multiviewer/preview grid, and debugging leaked VideoFrames, black canvases, or frames from a disconnected source reappearing.
Ships in texture-bridge. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Every shared-texture producer in @napolab/texture-bridge — bridge.forwardFrames, forwardSharedTexture, createSharedTextureReceiver — delivers to a renderer through the same consuming pair:
import { sharedTexture } from "electron";
import { bindSharedTextureReceiver } from "@napolab/texture-bridge-renderer/client"; // the /client subpath — not the package root
const receiver = bindSharedTextureReceiver(sharedTexture);
const sub = receiver.consume({ onFrame: (frame, ...extraArgs) => { /* ... */ }, onError });
bindSharedTextureReceiver(sharedTexture) — once at startup, before any consume call. It binds Electron's single sharedTexture.setSharedTextureReceiver slot to a pool so multiple consumers coexist; that bind is its only side effect. It returns a handle: receiver.consume(handlers) registers a consumer and returns { dispose() }; receiver.dispose() (idempotent) empties the pool and frees the target. Never call sharedTexture.setSharedTextureReceiver yourself alongside it.
sharedTexture twice without disposing throws SharedTextureReceiverAlreadyBoundError. Keep one binding per window and share the handle; re-binding is allowed after receiver.dispose().receiver.consume() after receiver.dispose() throws SharedTextureReceiverDisposedError.receiver.dispose() the installed callback stays and keeps draining (releasing) frames that still arrive.The consuming code must run where electron is resolvable — a preload script, with:
new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, "../preload/monitor.js"),
nodeIntegration: true,
contextIsolation: false,
sandbox: false,
},
});
Importing /client from a Vite-driven renderer page fails in dev (Vite cannot pre-bundle the electron CJS module) — keep it in the preload. For context-isolated production setups: consume in the preload, then window.postMessage(videoFrame, "*", [videoFrame]) each frame into the isolated world (VideoFrame is transferable; the receiving side closes it).
This is where implementations go wrong:
onFrame(frame, ...extraArgs) receives { textureId, videoFrame }. The extraArgs are whatever the producer passed (forwardFrames' demux tag, etc.).frame.videoFrame after your handler settles. Do not close it yourself. There is no frame.release() — that's the sender-side paint-event API.clone() it. Close every clone you make, exactly once: when replaced by a newer frame, on disconnect, and on beforeunload.Latest-frame coalescing + rAF draw. Draw cost stays fixed at display refresh rate no matter how many sources or how fast they paint; decks keep showing the last frame between arrivals instead of flickering.
// preload/monitor.ts
import { sharedTexture } from "electron";
import { bindSharedTextureReceiver } from "@napolab/texture-bridge-renderer/client";
import type { SharedTextureConsumerFrame } from "@napolab/texture-bridge-renderer/client";
const receiver = bindSharedTextureReceiver(sharedTexture);
const SLOT_COUNT = 4;
const W = 480, H = 270;
// One held clone per slot; undefined = nothing to draw.
const latest: (SharedTextureConsumerFrame | undefined)[] = Array.from({ length: SLOT_COUNT });
// dispose() doesn't cancel deliveries already in flight, so a stray tagged
// frame can arrive just after disconnect — this guard drops it instead of
// letting it resurrect the slot.
const connected: boolean[] = Array.from({ length: SLOT_COUNT }, () => false);
receiver.consume({
onFrame: (frame, ...args) => {
const slot = args[0];
if (typeof slot !== "number" || slot < 0 || slot >= SLOT_COUNT) return;
if (!connected[slot]) return;
const held = frame.videoFrame.clone(); // clone FIRST…
latest[slot]?.videoFrame.close(); // …then close the superseded one
latest[slot] = { textureId: frame.textureId, videoFrame: held };
},
onError: (err) => console.error("[monitor]", err),
});
const draw = (): void => {
try {
for (const [slot, held] of latest.entries()) {
if (!held) continue;
const deckCtx = deckContexts[slot];
deckCtx?.drawImage(held.videoFrame, 0, 0, W, H); // GPU blit; draw scales full-res frame
// 2x2 composite = renderer-side atlas: blit each held frame into its quadrant
compositeCtx.drawImage(held.videoFrame, (slot % 2) * W, Math.floor(slot / 2) * H, W, H);
// do NOT close here — the held clone is redrawn every tick until replaced
}
} finally {
requestAnimationFrame(draw); // re-arm in finally: one throw must not end the loop
}
};
requestAnimationFrame(draw);
const disconnectSlot = (slot: number): void => {
connected[slot] = false; // before disposing the producer
latest[slot]?.videoFrame.close();
latest[slot] = undefined;
deckContexts[slot]?.clearRect(0, 0, W, H);
compositeCtx.clearRect((slot % 2) * W, Math.floor(slot / 2) * H, W, H); // clear the quadrant too
};
window.addEventListener("beforeunload", () => {
receiver.dispose();
for (const held of latest) held?.videoFrame.close();
});
Main-process side per slot: bridge.forwardFrames(monitorWC, { extraArgs: [slot] }) (local source) or createSharedTextureReceiver({ senderName, target: monitorWC, extraArgs: [slot] }) + .start() (external Syphon/Spout source) — both feed this same consumer. On disconnect, dispose() the producer AND run disconnectSlot.
For WebGPU, replace drawImage with device.importExternalTexture({ source: held.videoFrame }) — also zero-copy.
| Mistake | Reality |
|---|---|
frame.videoFrame.close() (or an invented frame.release()) inside onFrame | The pool closes the original after the handler settles. Close only your clones. |
Drawing directly in onFrame | Ties draw cost to arrival rate (N sources × paint rate). Hold latest clone, draw in one rAF loop. |
| Closing the held frame after drawing it | Held clone is redrawn every tick — closing it blanks the deck until the next arrival. Close only when superseded, disconnected, or unloading. |
Skipping the connected guard | In-flight frames arriving after disconnect resurrect the slot with a stale image. |
Closing the superseded clone before clone() returns | If clone() throws you are left holding a closed frame; the next drawImage throws InvalidStateError. Clone first, then close. |
requestAnimationFrame(draw) outside a finally | One drawImage/clone throw ends the loop for the session — every deck freezes while arrival counters keep ticking. |
| Clearing canvases between frames | Paint-driven sources go idle legitimately; clearing causes flicker. Clear only on disconnect (deck AND composite quadrant). |
setSharedTextureReceiver directly, or importing /client in a Vite renderer page | Single-slot API conflicts with the pool; Vite dev can't pre-bundle electron. Preload + bindSharedTextureReceiver(sharedTexture). |
Inventing exposeFrameReceiver / subscribeFrames / @napolab/texture-bridge/preload | None exist. The receiving surface is exactly bindSharedTextureReceiver(sharedTexture) + receiver.consume(handlers) (+ createMultiDispatcher for custom fan-out adapters). The old global install-then-consume function pair was removed — a zero-argument install call or a free-standing consume function is an invented API now; always go through the receiver handle. |
Calling bindSharedTextureReceiver(sharedTexture) in several modules of the same window | Second call throws SharedTextureReceiverAlreadyBoundError. Bind once, pass the handle around, and receiver.consume() per consumer. |
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub naporin0624/electron-texture-bridge --plugin texture-bridge8 skills in this plugin
/choosing-texture-bridge-apiUse when deciding which @napolab/texture-bridge API tier to use for a feature — simple vs core, Syphon/Spout output vs in-app preview/monitor, forwardSharedTexture vs forwardFrames — when reviewing an integration plan that uses the library, or when unsure whether a texture-bridge function, method, or option actually exists./delivering-imported-texturesUse when delivering a GPU texture to a renderer from the Electron main process — about to call `sharedTexture.importSharedTexture` / `sendSharedTexture` / `release()` by hand, deciding where `release()` belongs, wrapping delivery in try/finally or neverthrow tees to guarantee exactly-once release, writing the same deliver-and-release dance in a second module, or delivering a texture you imported yourself from a receiver's native handle./diagnosing-dead-frame-forwardsUse when forwarded frames stop arriving — a preview or monitor window that goes black or shows a "no signal" placeholder, a multiviewer deck frozen on its last frame, previews that die during a long session or for only some targets while Syphon/Spout output keeps working, a forwardFrames registration you suspect never delivered anything, or a dead preview with nothing in the logs./handling-texture-bridge-failuresUse when adding error handling, telemetry, or crash-safety to code that uses @napolab/texture-bridge — deciding whether a call needs a try/catch or a Result.fromThrowable wrapper, reviewing the failure handling of a texture-bridge integration, defining error classes for bridge or receiver operations, or diagnosing silently black output or a main-process crash from a bridge call./managing-frame-forward-lifecycleUse when registering or tearing down `TextureBridge.forwardFrames` targets — a monitor/multiviewer window the user can close and reopen, decks connected and disconnected repeatedly, a bridge disposed while forwards are still registered — or when about to add "destroyed"/"closed" listener bookkeeping, removeListener hygiene, isDestroyed guards, or try/catch around `forward.dispose()`, or when chasing a MaxListenersExceededWarning or a leak around forwarding./migrating-to-forward-framesUse when replacing an existing Electron preview/monitor implementation with @napolab/texture-bridge zero-copy forwarding — capturePage polling, paint-event bitmap IPC, toBitmap/toPNG transfers, or a Syphon loopback feeding an in-app window — or when upgrading to the release that ships forwardFrames / forwardSharedTexture.Other plugins' skills for the same job