1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:38:12 +00:00

LibWeb+LibGfx: Move the blit image through 2D transfrom to Gfx::Painter

Even though this code is obnoxiously slow, it still belongs in LibGfx
and should not be hidden away in LibWeb's CanvasRenderingContext2D.
This commit is contained in:
Andreas Kling 2022-09-24 13:00:53 +02:00
parent 37ea6de772
commit b52165c5d7
3 changed files with 59 additions and 54 deletions

View file

@ -155,60 +155,7 @@ DOM::ExceptionOr<void> CanvasRenderingContext2D::draw_image_internal(CanvasImage
if (!painter)
return {};
auto& drawing_state = this->drawing_state();
if (drawing_state.transform.is_identity_or_translation()) {
painter->translate(drawing_state.transform.e(), drawing_state.transform.f());
painter->draw_scaled_bitmap(destination_rect.to_rounded<int>(), *bitmap, source_rect, 1.0f, Gfx::Painter::ScalingMode::BilinearBlend);
painter->translate(-drawing_state.transform.e(), -drawing_state.transform.f());
} else {
// The context has an affine transform, we have to draw through it!
// FIXME: This is *super* inefficient.
// What we currently do, roughly:
// - Map the destination rect through the context's transform.
// - Compute the bounding rect of the destination quad.
// - For each point in the computed bounding rect, reverse-map it to a point in the source image.
// - Sample the source image at the computed point.
// - Set or blend (depending on alpha values) one pixel in the canvas.
// - Loop.
// FIXME: Gfx::Painter should have an affine transform as part of its state and handle all of this instead.
auto inverse_transform = drawing_state.transform.inverse();
if (!inverse_transform.has_value())
return {};
auto destination_quad = drawing_state.transform.map_to_quad(destination_rect);
auto destination_bounding_rect = destination_quad.bounding_rect().to_rounded<int>();
Gfx::AffineTransform source_transform;
source_transform.translate(source_x, source_y);
source_transform.scale(source_width / destination_width, source_height / destination_height);
source_transform.translate(-destination_x, -destination_y);
for (int y = destination_bounding_rect.y(); y <= destination_bounding_rect.bottom(); ++y) {
for (int x = destination_bounding_rect.x(); x <= destination_bounding_rect.right(); ++x) {
auto destination_point = Gfx::IntPoint { x, y };
if (!painter->clip_rect().contains(destination_point))
continue;
if (!destination_quad.contains(destination_point.to_type<float>()))
continue;
auto source_point = source_transform.map(inverse_transform->map(destination_point)).to_rounded<int>();
if (!bitmap->rect().contains(source_point))
continue;
auto source_color = bitmap->get_pixel(source_point);
if (source_color.alpha() == 0)
continue;
if (source_color.alpha() == 255) {
painter->set_pixel(destination_point, source_color);
continue;
}
auto dst_color = painter->target()->get_pixel(destination_point);
painter->set_pixel(destination_point, dst_color.blend(source_color));
}
}
}
painter->draw_scaled_bitmap_with_transform(destination_rect.to_rounded<int>(), *bitmap, source_rect, drawing_state().transform, 1.0f, Gfx::Painter::ScalingMode::BilinearBlend);
// 7. If image is not origin-clean, then set the CanvasRenderingContext2D's origin-clean flag to false.
if (image_is_not_origin_clean(image))