Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize _clipImageData #4200

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/browser/renderer/shared/TextureAtlas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,20 +770,24 @@ export class TextureAtlas implements ITextureAtlas {
}

private _clipImageData(imageData: ImageData, boundingBox: IBoundingBox): ImageData {
// Operate on pixels instead of channels to reduce the amount of work
const originalData = new Uint32Array(imageData.data.buffer);

// Create a new view on the same buffer for the clipped data. The clipping operation is done in
// place to avoid allocating another buffer
const width = boundingBox.right - boundingBox.left + 1;
const height = boundingBox.bottom - boundingBox.top + 1;
const clippedData = new Uint8ClampedArray(width * height * 4);
for (let y = boundingBox.top; y <= boundingBox.bottom; y++) {
for (let x = boundingBox.left; x <= boundingBox.right; x++) {
const oldOffset = y * this._tmpCanvas.width * 4 + x * 4;
const newOffset = (y - boundingBox.top) * width * 4 + (x - boundingBox.left) * 4;
clippedData[newOffset] = imageData.data[oldOffset];
clippedData[newOffset + 1] = imageData.data[oldOffset + 1];
clippedData[newOffset + 2] = imageData.data[oldOffset + 2];
clippedData[newOffset + 3] = imageData.data[oldOffset + 3];
const clippedData = new Uint32Array(imageData.data.buffer, 0, width * height);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh well - inplace overwriting is abit dangerous, if the overlapping from rewrites is not safe (guess its ok here?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safe as the data is read from low to high and the writing is always the same as writing (rare) or lower


// Perform clipping and return the result
let x = 0;
let y = 0;
for (y = boundingBox.top; y <= boundingBox.bottom; y++) {
for (x = boundingBox.left; x <= boundingBox.right; x++) {
clippedData[(y - boundingBox.top) * width + (x - boundingBox.left)] = originalData[y * imageData.width + x];
}
}
return new ImageData(clippedData, width, height);
return new ImageData(new Uint8ClampedArray(clippedData.buffer, clippedData.byteOffset, clippedData.byteLength), width, height);
Copy link
Member

@jerch jerch Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it is worth the trouble - since you already do inplace overwriting above, the new ImageData maybe can also be avoided?

Which raises the question - is the _clipImageData method needed at all? As far as I can see it just transfers a rectangular subarea over. Isnt it faster to simply take the orginal ImageData and apply the box offsets during drawImage? But maybe I am overlooking something...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right! We can remove this outright and just use boundingBox in drawImage. I'm guessing the reason it's as it is currently is because I just overlooked that or wasn't thinking drawImage had a this signature source+dest signature.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was just doing this, we can't actually use drawImage as it doesn't accept ImageData, looks like we can still use putImageData though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 #4201

Copy link
Member

@jerch jerch Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, was mixing the methods up (I use putImageData myself, lol) 😊

Though the question remains, if thats actually faster, or just creates runtime noise at different ends...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely it's faster, it's essentially doing what was being done manually before natively

}
}

Expand Down