1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-12-22 07:15:47 +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();
onmessage = e => {
let bitmap = e.data.bitmap;
if(!(bitmap instanceof ImageBitmap)) {
postMessage(new Error('Bad type for worker data'));
return;
}
if(!imageSegmenter) {
// not ready yet
bitmap.close();
postMessage(null);
return;
}
try {
let bitmap = e.data.bitmap;
if(!(bitmap instanceof ImageBitmap))
throw new Error('Bad type for worker data');
let width = bitmap.width;
let height = bitmap.height;
imageSegmenter.segmentForVideo(
@ -52,6 +56,7 @@ onmessage = e => {
},
);
} catch(e) {
bitmap.close();
postMessage(e);
}
};

View file

@ -1252,46 +1252,52 @@ let filters = {
},
draw: async function(src, ctx) {
let bitmap = await createImageBitmap(src);
let p = new Promise((resolve, reject) => {
this.userdata.worker.onmessage = e => {
if(e && e.data) {
if(e.data instanceof Error)
reject(e.data);
else
resolve(e.data);
} else {
resolve(null);
}
};
});
this.userdata.worker.postMessage({
bitmap: bitmap,
timestamp: performance.now(),
}, [bitmap]);
let result = await p;
try {
let p = new Promise((resolve, reject) => {
this.userdata.worker.onmessage = e => {
if(e && e.data) {
if(e.data instanceof Error)
reject(e.data);
else
resolve(e.data);
} else {
resolve(null);
}
};
});
this.userdata.worker.postMessage({
bitmap: bitmap,
timestamp: performance.now(),
}, [bitmap]);
let result = await p;
if(!result)
return false;
if(!result)
return false;
let mask = result.mask;
bitmap = result.bitmap;
let mask = result.mask;
bitmap = result.bitmap;
if(ctx.canvas.width !== src.videoWidth ||
ctx.canvas.height !== src.videoHeight) {
ctx.canvas.width = src.videoWidth;
ctx.canvas.height = src.videoHeight;
if(ctx.canvas.width !== src.videoWidth ||
ctx.canvas.height !== src.videoHeight) {
ctx.canvas.width = src.videoWidth;
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;
},
},