Cesium quads: the blob a quad draws is held by its material, not the quad, so a quad that moves to another source lets the old blob go once the old material is freed after the frame; a change to or from a blob-backed image takes a material of its own

This commit is contained in:
Sameh Khamis committed 2026-11-11 20:56:47 -08:00
1 parent 9549e561e5
commit d440f5bfaf
3 files changed
+78 -7

No files matched your search

+5 -3
View File
@@ -160,9 +160,11 @@ draws each.
The Cesium port takes every kind of `ImageRef` wherever an image is drawn 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 (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, 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 revoked once the blob itself has been collected; a material drawing a blob,
a blob holds it while it lives, since a material loads its image on its first a quad's among them, holds it while it lives, since a material loads its image
draw, even after the atlas slot it came from has a new one), a 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 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` unregistered slot; anything else refuses it). The check `draw.icons.images`
draws each kind on still and receding icons. draws each kind on still and receding icons.
+57
View File
@@ -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])); 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]));
}
});
+16 -4
View File
@@ -39,6 +39,10 @@ function sourceOf(source, toImage) {
throw new Error('a quad takes an image, a video or a canvas'); 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. */ /** An outline as a key; '' for none. */
const outlineKey = (outline) => const outlineKey = (outline) =>
outline outline
@@ -149,11 +153,12 @@ export function createQuadSets({
const allowPicking = pickableOf(record, item); const allowPicking = pickableOf(record, item);
const modelMatrix = matrixOf(item, new Cesium.Matrix4()); const modelMatrix = matrixOf(item, new Cesium.Matrix4());
entry.source = sourceOf(item.source, toImage); 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.alpha = item.alpha ?? 1;
entry.material = imageMaterial(entry.source, entry.alpha); 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.videoSize = null;
entry.fill = host.add( entry.fill = host.add(
new Cesium.Primitive({ new Cesium.Primitive({
@@ -225,6 +230,8 @@ export function createQuadSets({
function rebind(entry) { function rebind(entry) {
const old = entry.material; const old = entry.material;
entry.material = imageMaterial(entry.source, entry.alpha); 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.fill.appearance.material = entry.material;
entry.videoSize = null; entry.videoSize = null;
entry.uploadFrames = 2; entry.uploadFrames = 2;
@@ -266,14 +273,19 @@ export function createQuadSets({
if (source !== entry.source) { if (source !== entry.source) {
const video = isVideo(source) || isVideo(entry.source); const video = isVideo(source) || isVideo(entry.source);
entry.source = source; entry.source = source;
holdImage(entry, item.source?.image);
if (video) { if (video) {
// To or from a video: a material of its own, so the video's frames // 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. // are not copied into another source's texture, or the other way.
rebind(entry); rebind(entry);
watchVideo(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 { } else {
// Another canvas or URL: the material reads it anew. // Another canvas or URL: the material reads it anew.
entry.image = item.source?.image;
entry.material.uniforms.image = source; entry.material.uniforms.image = source;
entry.uploadFrames = 2; entry.uploadFrames = 2;
} }