small optimizations
This commit is contained in:
@ -476,21 +476,36 @@ export class FakeShader {
|
||||
}
|
||||
}
|
||||
|
||||
private compositeTiles(): void {
|
||||
const width = this.canvas.width;
|
||||
private async compositeTiles(): Promise<void> {
|
||||
const height = this.canvas.height;
|
||||
const tileHeight = Math.ceil(height / this.workerCount);
|
||||
|
||||
// Clear main canvas
|
||||
this.ctx.clearRect(0, 0, width, height);
|
||||
|
||||
// Composite all tiles directly on main canvas
|
||||
for (let i = 0; i < this.workerCount; i++) {
|
||||
const tileData = this.tileResults.get(i);
|
||||
if (tileData) {
|
||||
const startY = i * tileHeight;
|
||||
this.ctx.putImageData(tileData, 0, startY);
|
||||
// Use ImageBitmap for faster compositing if available
|
||||
if (typeof createImageBitmap !== 'undefined') {
|
||||
try {
|
||||
const bitmapPromises: Promise<ImageBitmap>[] = [];
|
||||
const positions: number[] = [];
|
||||
|
||||
for (let i = 0; i < this.workerCount; i++) {
|
||||
const tileData = this.tileResults.get(i);
|
||||
if (tileData) {
|
||||
bitmapPromises.push(createImageBitmap(tileData));
|
||||
positions.push(i * tileHeight);
|
||||
}
|
||||
}
|
||||
|
||||
const bitmaps = await Promise.all(bitmapPromises);
|
||||
|
||||
for (let i = 0; i < bitmaps.length; i++) {
|
||||
this.ctx.drawImage(bitmaps[i], 0, positions[i]);
|
||||
bitmaps[i].close(); // Free memory
|
||||
}
|
||||
} catch (error) {
|
||||
// Fallback to putImageData if ImageBitmap fails
|
||||
this.fallbackCompositeTiles();
|
||||
}
|
||||
} else {
|
||||
this.fallbackCompositeTiles();
|
||||
}
|
||||
|
||||
// Clear tile results
|
||||
@ -510,6 +525,18 @@ export class FakeShader {
|
||||
}
|
||||
}
|
||||
|
||||
private fallbackCompositeTiles(): void {
|
||||
const tileHeight = Math.ceil(this.canvas.height / this.workerCount);
|
||||
|
||||
for (let i = 0; i < this.workerCount; i++) {
|
||||
const tileData = this.tileResults.get(i);
|
||||
if (tileData) {
|
||||
const startY = i * tileHeight;
|
||||
this.ctx.putImageData(tileData, 0, startY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Simplified method - kept for backward compatibility but always uses all cores
|
||||
setMultiWorkerMode(_enabled: boolean, _workerCount?: number): void {
|
||||
// Always use all available cores, ignore the enabled parameter
|
||||
|
||||
Reference in New Issue
Block a user