diff --git a/docs/RENDERER-CONTRACT.md b/docs/RENDERER-CONTRACT.md index 997ddeb9a..b9e31b8d9 100644 --- a/docs/RENDERER-CONTRACT.md +++ b/docs/RENDERER-CONTRACT.md @@ -160,9 +160,11 @@ draws each. The Cesium port takes every kind of `ImageRef` wherever an image is drawn (icons on billboards and on the GPU, line patterns, shape fills, quads and raster shells): a URL, SVG, a blob (by an object URL made once per blob, -revoked once the blob itself has been collected; a material or quad drawing -a blob holds it while it lives, since a material loads its image on its first -draw, even after the atlas slot it came from has a new one), a +revoked once the blob itself has been collected; a material drawing a blob, +a quad's among them, holds it while it lives, since a material loads its image +on its first draw, even after the atlas slot it came from has a new one; a +quad given another source lets the old blob go with its old material, once the +frame has drawn), a keyed canvas, and an atlas slot as the image it holds (an icon waits for an unregistered slot; anything else refuses it). The check `draw.icons.images` draws each kind on still and receding icons. diff --git a/src/render/cesium/draw.test.mjs b/src/render/cesium/draw.test.mjs index c20eeb73e..f7b44b67e 100644 --- a/src/render/cesium/draw.test.mjs +++ b/src/render/cesium/draw.test.mjs @@ -8249,3 +8249,60 @@ test('a material drawing a blob from an atlas slot keeps that blob alive after t names.forEach((name, i) => (globalThis[name] = real[i])); } }); + +test('a quad that moves from a blob to a URL lets the blob go once its old material is freed', async () => { + const v8 = await import('node:v8'); + const vm = await import('node:vm'); + v8.setFlagsFromString('--expose-gc'); + const gc = vm.runInNewContext('gc'); + const settle = async () => { + for (let i = 0; i < 3; i++) { + await new Promise((resolve) => setImmediate(resolve)); + gc(); + } + }; + const create = URL.createObjectURL; + URL.createObjectURL = () => `blob:test/${Math.random()}`; + const names = [ + 'HTMLCanvasElement', + 'HTMLImageElement', + 'ImageBitmap', + 'OffscreenCanvas', + ]; + const real = names.map((name) => globalThis[name]); + for (const name of names) globalThis[name] ??= class {}; + try { + const scene = fakeScene(); + const part = createCesiumDraw({ + scene, + report: (error) => assert.fail(error), + }); + const set = part.port.quads({ id: 'feeds' }); + const quad = (source) => ({ + id: 'q', + center: { x: 6378137, y: 10, z: 20 }, + orientation: { x: 0, y: 0, z: 0, w: 1 }, + size: [40, 20], + source, + }); + const refs = {}; + // Made in a function of its own, so nothing here holds the blob. + (() => { + const blob = new Blob(['frame']); + refs.blob = new WeakRef(blob); + set.set([quad({ image: { blob } })]); + })(); + await settle(); + assert.ok(refs.blob.deref(), 'held while its material draws it'); + set.set([quad({ image: 'https://example.test/still.png' })]); + await settle(); + assert.ok(refs.blob.deref(), 'held while the old material may draw'); + part.afterRender(); + await settle(); + assert.equal(refs.blob.deref(), undefined, 'let go with the old material'); + set.dispose(); + } finally { + URL.createObjectURL = create; + names.forEach((name, i) => (globalThis[name] = real[i])); + } +}); diff --git a/src/render/cesium/drawQuads.js b/src/render/cesium/drawQuads.js index b77baca69..056a7474e 100644 --- a/src/render/cesium/drawQuads.js +++ b/src/render/cesium/drawQuads.js @@ -39,6 +39,10 @@ function sourceOf(source, toImage) { throw new Error('a quad takes an image, a video or a canvas'); } +/** Whether an image may be drawn from a blob: one, or an atlas slot's. */ +const mayHoldBlob = (image) => + Boolean(image && (image.blob || typeof image.atlas === 'string')); + /** An outline as a key; '' for none. */ const outlineKey = (outline) => outline @@ -149,11 +153,12 @@ export function createQuadSets({ const allowPicking = pickableOf(record, item); const modelMatrix = matrixOf(item, new Cesium.Matrix4()); entry.source = sourceOf(item.source, toImage); - // A blob it draws stays alive while the quad does: its material loads - // the image on its first draw. - holdImage(entry, item.source?.image); entry.alpha = item.alpha ?? 1; entry.material = imageMaterial(entry.source, entry.alpha); + // A blob it draws stays alive while its material does: the material + // loads the image on its first draw. + entry.image = item.source?.image; + holdImage(entry.material, entry.image); entry.videoSize = null; entry.fill = host.add( new Cesium.Primitive({ @@ -225,6 +230,8 @@ export function createQuadSets({ function rebind(entry) { const old = entry.material; entry.material = imageMaterial(entry.source, entry.alpha); + entry.image = entry.item.source?.image; + holdImage(entry.material, entry.image); entry.fill.appearance.material = entry.material; entry.videoSize = null; entry.uploadFrames = 2; @@ -266,14 +273,19 @@ export function createQuadSets({ if (source !== entry.source) { const video = isVideo(source) || isVideo(entry.source); entry.source = source; - holdImage(entry, item.source?.image); if (video) { // To or from a video: a material of its own, so the video's frames // are not copied into another source's texture, or the other way. rebind(entry); watchVideo(entry); + } else if (mayHoldBlob(entry.image) || mayHoldBlob(item.source?.image)) { + // To or from an image a blob may back: a material of its own, + // holding the new blob, while the old material (and the old blob it + // holds) goes once this frame has drawn. + rebind(entry); } else { // Another canvas or URL: the material reads it anew. + entry.image = item.source?.image; entry.material.uniforms.image = source; entry.uploadFrames = 2; }