From c56ff090e61a41d03ddb85232f8c945c405e992f Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Tue, 10 Nov 2026 09:46:03 -0800 Subject: [PATCH] Destroy an image fill's texture its material never took A material takes a texture, and destroys it later, only when it next updates. A canvas fill replaced by one of another size twice before a frame, or disposed before one, left the texture in between to no one. Such a texture is now destroyed when it is replaced or its shapes go. --- src/render/cesium/draw.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/render/cesium/draw.js b/src/render/cesium/draw.js index bb1f26d13..39511154c 100644 --- a/src/render/cesium/draw.js +++ b/src/render/cesium/draw.js @@ -3710,10 +3710,24 @@ export function createCesiumDraw({ texture.copyFrom({ source: canvas }); return; } + dropPendingTexture(image); image.texture = new Cesium.Texture({ context, source: canvas }); image.material.uniforms.image = image.texture; } + /** + * Destroys an image fill's newest texture if its material has not taken + * it yet. A material takes a texture (and destroys it later) only when it + * next updates, so one replaced or disposed before then is no one's. + */ + function dropPendingTexture(image) { + const texture = image.texture; + if (!texture || texture.isDestroyed()) return; + if (image.material._textures?.image === texture) return; + texture.destroy(); + image.texture = null; + } + /** An instance's colour, in a ready primitive. */ function colourOn(primitive, id, color) { const attributes = primitive.getGeometryInstanceAttributes(id); @@ -4009,10 +4023,13 @@ export function createCesiumDraw({ // Image fills' materials hold their textures, which primitives leave // alone: the host destroys them as it goes. const materials = []; + // Image fills, whose newest texture the material may not hold yet. + const images = []; const ownMaterial = (material) => { if (!materials.length) { const destroyHost = host.destroy.bind(host); host.destroy = () => { + for (const image of images.splice(0)) dropPendingTexture(image); for (const owned of materials.splice(0)) if (!owned.isDestroyed()) owned.destroy(); return destroyHost(); @@ -4071,6 +4088,7 @@ export function createCesiumDraw({ ); // Its image is changed in place (`writeImage`); it never recolours. part.image = { material, texture: null }; + images.push(part.image); part.translucent.fill = true; part.fill = host.add( new Cesium.Primitive({