Skip to content

Commit

Permalink
LibWeb: Use memcpy() in CanvasRenderingContext2D.getImageData()
Browse files Browse the repository at this point in the history
Instead of copying the image data pixel-by-pixel, we can memcpy full
scanlines at a time.

This knocks a 4% item down to <1% in profiles of Another World JS.
  • Loading branch information
awesomekling committed May 13, 2024
1 parent 9e15350 commit f58398a
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,9 @@ WebIDL::ExceptionOr<JS::GCPtr<ImageData>> CanvasRenderingContext2D::get_image_da
// 6. Set the pixel values of imageData to be the pixels of this's output bitmap in the area specified by the source rectangle in the bitmap's coordinate space units, converted from this's color space to imageData's colorSpace using 'relative-colorimetric' rendering intent.
// FIXME: Can't use a Gfx::Painter + blit() here as it doesn't support ImageData bitmap's RGBA8888 format.
for (int target_y = 0; target_y < source_rect_intersected.height(); ++target_y) {
for (int target_x = 0; target_x < source_rect_intersected.width(); ++target_x) {
auto pixel = bitmap.get_pixel(target_x + x, target_y + y);
image_data->bitmap().set_pixel(target_x, target_y, pixel);
}
auto* dst = image_data->bitmap().scanline(target_y);
auto const* src = bitmap.scanline(target_y + y) + x;
memcpy(dst, src, source_rect_intersected.width() * sizeof(Gfx::ARGB32));
}

// 7. Set the pixels values of imageData for areas of the source rectangle that are outside of the output bitmap to transparent black.
Expand Down

0 comments on commit f58398a

Please sign in to comment.