implementing more chunk ops based on GPU

composing, toEdits
This commit is contained in:
りき萌 2025-09-08 22:10:55 +02:00
parent bb55e23979
commit 1bbf1b1d94
5 changed files with 259 additions and 38 deletions

View file

@ -55,39 +55,53 @@ export class Layer {
}
}
compositeAlpha(src) {
// TODO
// for (let { x, y, chunk: srcChunk } of src.chunks.values()) {
// srcChunk.syncFromPixmap();
// let dstChunk = this.getOrCreateChunk(x, y);
// if (dstChunk == null) continue;
// dstChunk.ctx.globalCompositeOperation = "source-over";
// dstChunk.ctx.drawImage(srcChunk.canvas, 0, 0);
// dstChunk.syncToPixmap();
// dstChunk.markModified();
// }
composite(chunkAllocator, src, op) {
for (let { x, y, chunk: srcChunk } of src.chunks.values()) {
let dstChunk = this.getOrCreateChunk(chunkAllocator, x, y);
if (dstChunk == null) continue;
chunkAllocator.composite(dstChunk.id, srcChunk.id, op);
}
}
async toEdits() {
async toEdits(chunkAllocator) {
console.time("toEdits");
let edits = [];
let encodeTime = 0;
for (let { x, y, chunk } of this.chunks.values()) {
edits.push({
chunk: { x, y },
data: chunk.download(chunkAllocator).then(async (downloaded) => {
let start = performance.now();
// TODO
let imageBitmap = await createImageBitmap(
new ImageData(
new Uint8ClampedArray(downloaded.data),
downloaded.width,
downloaded.height,
),
);
chunkAllocator.freeDownloaded(downloaded.data);
let canvas = new OffscreenCanvas(downloaded.width, downloaded.height);
let ctx = canvas.getContext("bitmaprenderer");
ctx.transferFromImageBitmap(imageBitmap);
let blob = canvas.convertToBlob({ type: "image/png" });
// let start = performance.now();
let end = performance.now();
console.log("encoding image took", end - start, "ms");
encodeTime += end - start;
// for (let { x, y, chunk } of this.chunks.values()) {
// edits.push({
// chunk: { x, y },
// data: chunk.canvas.convertToBlob({ type: "image/png" }),
// });
// }
return blob;
}),
});
}
// for (let edit of edits) {
// edit.data = await edit.data;
// }
for (let edit of edits) {
edit.data = await edit.data;
}
// let end = performance.now();
// console.debug("toEdits done", end - start);
console.timeEnd("toEdits");
return edits;
}