Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | 40x 7x 1x 1x 4x 2x 2x 1x 6x 6x 6x 6x 6x 18x 15x 15x 6x 2x 4x 4x 4x 4x 6x 4x 4x 4x 4x 9x 9x 15x 15x 9x 9x 5x 4x 4x 4x 1x 2x 1x 64x 1x 64x 1x 1x 1x 1x 1x 1x 1x 1x 1x 64x | import { type Container, type Texture } from 'pixi.js';
import { destroyTextureAndRevokeBlobURL } from '../media/textures.ts';
/**
* Removes every child of `container` AND destroys each removed child so its renderer-side state is released.
*
* Why this exists: Pixi v8's `Container.removeChildren()` only detaches children from the display tree — it does NOT
* free their underlying renderer resources. A bare `removeChildren()` called once per frame on a hot render loop
* accumulates orphaned `Graphics` instances (each with an owned `GraphicsContext`) and `Text` instances (each holding a
* slot in the dynamic glyph atlas). After a few minutes of gameplay the renderer cache grows large enough that the next
* reconcile / GC stalls the main thread for several seconds — visible to the user as "browser unresponsive" the moment
* the chart finishes and the result scene tries to spin up.
*
* The destroy options below are deliberate:
*
* - `children: true` — recurse into any descendants the orphan may itself own. Most of our hot-loop nodes are leaves,
* but skin elements occasionally compose nested sprites.
* - `context: true` — free the owned `GraphicsContext` for any `Graphics` child. Without this, the polyline / fallback
* measure-line `new Graphics()` allocations leak GPU geometry buffers each frame.
* - `texture` / `textureSource` are deliberately omitted (default `false`). Sprite textures (note atlases, skin sheets)
* are long-lived and shared across frames; freeing them here would blank out subsequent renders.
*
* Reach for this whenever a render pass calls `someLayer.removeChildren()` on a container whose children were built
* fresh that frame. Static stage scaffolding (the scene root, persistent layer containers themselves) doesn't need it.
*/
export function disposeChildren(container: Container): void {
for (const child of container.removeChildren()) {
child.destroy({ children: true, context: true });
}
}
export { ChildPool, LaggedDisposer } from '../skins/pixi-kit/pools.ts';
export function destroyUniqueTextures(textures: Iterable<Texture | undefined>, destroySource = true): number {
const destroyed = new Set<Texture>();
for (const texture of textures) {
if (texture === undefined || destroyed.has(texture)) continue;
destroyed.add(texture);
// `destroyTextureAndRevokeBlobURL` is `texture.destroy(destroySource)` plus a revoke of any blob URL the texture
// was decoded from. The LR2 skin asset loader stamps the URL onto the texture via `attachBlobURLToTexture`, so
// disposing through this helper releases both the GPU resource AND the in-memory blob the URL was holding open.
destroyTextureAndRevokeBlobURL(texture, destroySource);
}
return destroyed.size;
}
export interface StaggerDestroyTexturesHandle {
count: number;
drain: () => void;
cancel: () => void;
}
/**
* Drop-in {@link destroyUniqueTextures} replacement that spreads the destroy + blob-revoke calls across multiple
* frames via the supplied scheduler (typically `Application.ticker.add` / `requestAnimationFrame`). Returns a small
* handle with the unique texture count plus `drain()` for callers that must finish the queue synchronously before the
* scheduler can tick again (for example when the owning Pixi Application is being destroyed).
*
* Why this exists: the synchronous `destroyUniqueTextures` destroys every texture in one pass, which on a scene
* transition (gameplay → result) means several hundred `texture.destroy(true)` calls landing in the same frame.
* Pixi's GL backend issues a `gl.deleteTexture` per call; the driver-side flush adds up to a visible 30–80 ms freeze
* on the transition, observed as the result splash being late by one or two frames. Spreading the work over ~10
* frames keeps each frame's destroy pass under the per-frame budget while still releasing the GPU memory within
* 100–200 ms of dispose() returning. Mirrors the staggered teardown pattern in PixiJS's own `pixijs-performance`
* guidance.
*
* The scheduler signature is the lowest-common-denominator subset of `app.ticker.add` and `requestAnimationFrame`:
* pass a callback, get a stop function. Callers that don't have a host ticker handy can wrap `requestAnimationFrame`
* directly. The callback is fired once per frame and receives no arguments.
*
* Errors thrown by `destroyTextureAndRevokeBlobURL` are swallowed so that one corrupted texture doesn't strand the
* remaining queue; the underlying call already runs the URL revoke before the texture destroy, so a half-finished
* destroy still releases the blob.
*/
export function staggerDestroyTextures(
textures: Iterable<Texture | undefined>,
scheduler: (callback: () => void) => () => void,
options: { perFrame?: number; destroySource?: boolean } = {},
): StaggerDestroyTexturesHandle {
const perFrame = Math.max(1, options.perFrame ?? 8);
const destroySource = options.destroySource ?? true;
// Flatten + dedupe up-front so the iterable can't generate fresh references between frames (a `Map.values()` view
// of a Map that's still being mutated would otherwise give us shifting work). Filtering `undefined` here also
// simplifies the per-frame loop.
const queue: Texture[] = [];
const seen = new Set<Texture>();
for (const texture of textures) {
if (texture === undefined || seen.has(texture)) continue;
seen.add(texture);
queue.push(texture);
}
if (queue.length === 0) {
return { count: 0, drain: () => undefined, cancel: () => undefined };
}
let cursor = 0;
let stopImpl: (() => void) | undefined;
let stopRequested = false;
let stopped = false;
const stopScheduler = (): void => {
if (stopped) return;
Iif (stopImpl === undefined) {
stopRequested = true;
return;
}
stopped = true;
stopImpl();
};
const destroyBatch = (): void => {
const end = Math.min(cursor + perFrame, queue.length);
for (let i = cursor; i < end; i += 1) {
try {
destroyTextureAndRevokeBlobURL(queue[i]!, destroySource);
} catch {
// Already destroyed / detached — keep walking the queue so a single corrupted entry doesn't strand the rest.
}
}
cursor = end;
if (cursor >= queue.length) {
stopScheduler();
}
};
stopImpl = scheduler(destroyBatch);
Iif (stopRequested) {
stopScheduler();
}
return {
count: queue.length,
drain: () => {
while (cursor < queue.length) {
destroyBatch();
}
stopScheduler();
},
cancel: stopScheduler,
};
}
/** What {@link releaseTexturesOnHost} needs of a scene host: its ticker, and whether it is going. */
export interface TextureReleaseHost {
readonly app: { readonly ticker: { add(callback: () => void): unknown; remove(callback: () => void): unknown } };
isDisposed(): boolean;
}
/**
* Destroys `textures` a few per frame on `host`'s ticker ({@link staggerDestroyTextures}), on a macrotask loop without
* a host, and all at once when the host itself is going: a sync sweep over a play's textures stalls the next scene's
* first frames on lower-end devices.
*/
export function releaseTexturesOnHost(
host: TextureReleaseHost | undefined,
textures: readonly (Texture | undefined)[],
): void {
if (textures.length === 0) return;
const ticker = host?.app.ticker;
const cleanup = staggerDestroyTextures(
textures,
ticker !== undefined
? (callback) => {
ticker.add(callback);
return () => ticker.remove(callback);
}
: (callback) => {
let cancelled = false;
let timeout: ReturnType<typeof setTimeout> | undefined;
const loop = (): void => {
Iif (cancelled) return;
callback();
Iif (!cancelled) timeout = setTimeout(loop, 0);
};
timeout = setTimeout(loop, 0);
return () => {
cancelled = true;
Eif (timeout !== undefined) clearTimeout(timeout);
};
},
);
Iif (host?.isDisposed() === true) cleanup.drain();
}
|