1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-12-22 15:25:48 +01:00

Use ImageBitmap.close when no longer needed.

This should free the underlying textures without waiting
for a GC to happen.
This commit is contained in:
Juliusz Chroboczek 2024-12-13 15:54:52 +01:00
parent 6e10da0116
commit 39c063a9d5
2 changed files with 50 additions and 39 deletions

View file

@ -23,16 +23,20 @@ async function loadImageSegmenter() {
loadImageSegmenter(); loadImageSegmenter();
onmessage = e => { onmessage = e => {
let bitmap = e.data.bitmap;
if(!(bitmap instanceof ImageBitmap)) {
postMessage(new Error('Bad type for worker data'));
return;
}
if(!imageSegmenter) { if(!imageSegmenter) {
// not ready yet // not ready yet
bitmap.close();
postMessage(null); postMessage(null);
return; return;
} }
try { try {
let bitmap = e.data.bitmap;
if(!(bitmap instanceof ImageBitmap))
throw new Error('Bad type for worker data');
let width = bitmap.width; let width = bitmap.width;
let height = bitmap.height; let height = bitmap.height;
imageSegmenter.segmentForVideo( imageSegmenter.segmentForVideo(
@ -52,6 +56,7 @@ onmessage = e => {
}, },
); );
} catch(e) { } catch(e) {
bitmap.close();
postMessage(e); postMessage(e);
} }
}; };

View file

@ -1252,46 +1252,52 @@ let filters = {
}, },
draw: async function(src, ctx) { draw: async function(src, ctx) {
let bitmap = await createImageBitmap(src); let bitmap = await createImageBitmap(src);
let p = new Promise((resolve, reject) => { try {
this.userdata.worker.onmessage = e => { let p = new Promise((resolve, reject) => {
if(e && e.data) { this.userdata.worker.onmessage = e => {
if(e.data instanceof Error) if(e && e.data) {
reject(e.data); if(e.data instanceof Error)
else reject(e.data);
resolve(e.data); else
} else { resolve(e.data);
resolve(null); } else {
} resolve(null);
}; }
}); };
this.userdata.worker.postMessage({ });
bitmap: bitmap, this.userdata.worker.postMessage({
timestamp: performance.now(), bitmap: bitmap,
}, [bitmap]); timestamp: performance.now(),
let result = await p; }, [bitmap]);
let result = await p;
if(!result) if(!result)
return false; return false;
let mask = result.mask; let mask = result.mask;
bitmap = result.bitmap; bitmap = result.bitmap;
if(ctx.canvas.width !== src.videoWidth || if(ctx.canvas.width !== src.videoWidth ||
ctx.canvas.height !== src.videoHeight) { ctx.canvas.height !== src.videoHeight) {
ctx.canvas.width = src.videoWidth; ctx.canvas.width = src.videoWidth;
ctx.canvas.height = src.videoHeight; ctx.canvas.height = src.videoHeight;
}
ctx.globalCompositeOperation = 'copy';
ctx.filter = `blur(${src.videoWidth / 256}px)`;
ctx.drawImage(mask, 0, 0);
ctx.globalCompositeOperation = 'source-in';
ctx.filter = `blur(${src.videoWidth / 48}px)`;
ctx.drawImage(result.bitmap, 0, 0);
ctx.globalCompositeOperation = 'destination-atop';
ctx.filter = 'none';
ctx.drawImage(result.bitmap, 0, 0);
ctx.globalCompositeOperation = 'source-over';
mask.close();
} finally {
bitmap.close();
} }
ctx.globalCompositeOperation = 'copy';
ctx.filter = `blur(${src.videoWidth / 256}px)`;
ctx.drawImage(mask, 0, 0);
ctx.globalCompositeOperation = 'source-in';
ctx.filter = `blur(${src.videoWidth / 48}px)`;
ctx.drawImage(result.bitmap, 0, 0);
ctx.globalCompositeOperation = 'destination-atop';
ctx.filter = 'none';
ctx.drawImage(result.bitmap, 0, 0);
ctx.globalCompositeOperation = 'source-over';
return true; return true;
}, },
}, },