Cesium draw: a material or quad drawing a blob keeps it alive

An image material loads its image on its first draw, and an atlas slot
given a new image no longer holds the old blob, so a hidden fill, pattern
or quad built from a slot could lose its blob to collection, its URL
revoked, before it was ever drawn. Each material built from a blob (shape
and ground image fills, line patterns) and each quad now holds the blob it
draws while it lives.
This commit is contained in:
Sameh Khamis committed 2026-11-11 20:25:55 -08:00
1 parent 151230f5fa
commit 2a1e2d9c0b
4 files changed
+123 -10

No files matched your search

+34 -8
View File
@@ -1253,6 +1253,8 @@ export function createCesiumDraw({
// when its icon is drawn, and registering an atlas again swaps the images
// of the icons that drew from it.
const atlases = new Map();
/** The blob each material or quad draws, held while it lives (`holdImage`). */
const heldBlobs = new WeakMap();
/**
* An icon's image: an atlas slot read through its atlas, or undefined
@@ -1270,11 +1272,27 @@ export function createCesiumDraw({
* registered is refused, as an image it cannot read).
*/
function imageAt(image) {
return toImage(
typeof image?.atlas === 'string'
? (atlases.get(image.atlas)?.[image.slot] ?? image)
: image,
);
return toImage(slotImage(image));
}
/** An atlas slot as the image it holds now; any other image as it is. */
function slotImage(image) {
return typeof image?.atlas === 'string'
? (atlases.get(image.atlas)?.[image.slot] ?? image)
: image;
}
/**
* Keep the blob behind `image` (itself, or the atlas slot's now) alive
* while `holder` (a material, a quad) lives. A material loads its image
* from the blob's URL on its first draw, which may come long after it was
* built (hidden till then), and a blob's URL is revoked once the blob is
* collected: an atlas slot given a new image no longer holds the old one.
*/
function holdImage(holder, image) {
const blob = slotImage(image)?.blob;
if (holder && blob) heldBlobs.set(holder, blob);
return holder;
}
function buildIcons(record) {
@@ -2117,6 +2135,7 @@ export function createCesiumDraw({
Object.assign(drawn.uniforms, fabric.uniforms);
const flow = flowOf(material, style);
if (flow) record.flowMaterials.push(flow);
if (style.kind === 'pattern') holdImage(material, style.image);
return material;
}
@@ -4244,7 +4263,10 @@ export function createCesiumDraw({
: UnpickableGroundPrimitive;
const grounds = batches.map((batch) => {
const material = batch.imageFill
? imageMaterial(batch.image, batch.imageFill.repeat)
? holdImage(
imageMaterial(batch.image, batch.imageFill.repeat),
batch.imageFill.image,
)
: null;
const ground = new Ground({
geometryInstances: batch.instances,
@@ -4948,8 +4970,11 @@ export function createCesiumDraw({
// `lit`: shaded by Cesium's light, or flat; by default solids are lit.
const lit = item.lit ?? solid;
if (imageFill && !item.outlineOnly) {
const material = ownMaterial(
imageMaterial(imageAt(imageFill.image), imageFill.repeat),
const material = holdImage(
ownMaterial(
imageMaterial(imageAt(imageFill.image), imageFill.repeat),
),
imageFill.image,
);
// Its image is changed in place (`writeImage`); it never recolours.
part.image = { material, texture: null };
@@ -5073,6 +5098,7 @@ export function createCesiumDraw({
const quadSets = createQuadSets({
toColor,
toImage: imageAt,
holdImage,
pickId,
replaceHost,
lineWidth,
+80
View File
@@ -8169,3 +8169,83 @@ test('image fills pulse their alpha through their material, clamped or not', ()
);
set.dispose();
}));
test('a material drawing a blob from an atlas slot keeps that blob alive after the slot changes', async () => {
const v8 = await import('node:v8');
const vm = await import('node:vm');
v8.setFlagsFromString('--expose-gc');
const gc = vm.runInNewContext('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 refs = {};
// Made in a function of their own, so nothing here holds the blobs.
(() => {
const fillBlob = new Blob(['fill']);
const lineBlob = new Blob(['line']);
refs.fill = new WeakRef(fillBlob);
refs.line = new WeakRef(lineBlob);
part.port.atlas('pics', {
fill: { blob: fillBlob },
line: { blob: lineBlob },
});
})();
const shapes = part.port.shapes({ id: 'blob-fills' });
shapes.set([
{
id: 'image',
geometry: {
kind: 'rectangle',
rect: { west: 2, south: 1, east: 2.1, north: 1.1 },
height: 200,
},
fill: { image: { atlas: 'pics', slot: 'fill' } },
},
]);
const lines = part.port.lines({ id: 'blob-lines' });
lines.set([
{
id: 'pattern',
positions: [
{ lon: 0, lat: 0, height: 10 },
{ lon: 1, lat: 1, height: 10 },
],
widthPx: 2,
style: {
kind: 'pattern',
image: { atlas: 'pics', slot: 'line' },
repeat: 2,
},
},
]);
// The slots move on: nothing in the atlas holds the first blobs now.
part.port.atlas('pics', {
fill: { blob: new Blob(['b']) },
line: { blob: new Blob(['c']) },
});
for (let i = 0; i < 3; i++) {
await new Promise((resolve) => setImmediate(resolve));
gc();
}
assert.ok(refs.fill.deref(), 'the fill’s material still has its blob');
assert.ok(refs.line.deref(), 'the pattern’s material still has its blob');
shapes.dispose();
lines.dispose();
} finally {
URL.createObjectURL = create;
names.forEach((name, i) => (globalThis[name] = real[i]));
}
});
+6 -1
View File
@@ -47,13 +47,14 @@ const outlineKey = (outline) =>
/**
* The quads part of a Cesium draw port.
* @param {{toColor: Function, toImage: Function,
* @param {{toColor: Function, toImage: Function, holdImage?: Function,
* pickId: Function, replaceHost: Function, lineWidth: (px: number) => number,
* requestRender: () => void}} options From the draw port.
*/
export function createQuadSets({
toColor,
toImage,
holdImage = () => {},
pickId,
replaceHost,
lineWidth,
@@ -148,6 +149,9 @@ 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);
entry.videoSize = null;
@@ -262,6 +266,7 @@ 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.