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

+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]));
}
});
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');
}
/** 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;
}