Files
ladybird/Libraries/LibWeb/HTML/Canvas/CanvasDrawImage.cpp
Aliaksandr Kalenik 40f2abb7fe LibGfx+LibWeb: Add DecodedImageFrame
Decoded image data should not continue to traffic in ImmutableBitmap now
that the bitmap wrapper is being retired. Introduce DecodedImageFrame as
the paintable decoded-image unit and store a Bitmap plus ColorSpace in
it directly.

Thread the new frame type through decoded image data, display-list
image commands, filters, canvas drawImage, patterns, WebGL texture
upload, and CSS/SVG image consumers. ImmutableBitmap remains only at
the legacy boundaries that still need it, such as HTML video snapshots
and callers that explicitly ask for a bitmap snapshot.

This keeps color-space ownership with the decoded frame while making
the expensive or legacy ImmutableBitmap path explicit at the few call
sites that still need it.
2026-05-05 14:39:17 -05:00

122 lines
6.4 KiB
C++

/*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/Bitmap.h>
#include <LibGfx/DecodedImageFrame.h>
#include <LibGfx/ImmutableBitmap.h>
#include <LibWeb/HTML/Canvas/CanvasDrawImage.h>
#include <LibWeb/HTML/DecodedImageData.h>
#include <LibWeb/HTML/ImageBitmap.h>
namespace Web::HTML {
Gfx::IntSize canvas_image_source_dimensions(CanvasImageSource const& image)
{
return image.visit(
[](GC::Root<HTMLImageElement> const& source) -> Gfx::IntSize {
if (auto immutable_bitmap = source->immutable_bitmap())
return immutable_bitmap->size();
// FIXME: This is very janky and not correct.
return { source->width(), source->height() };
},
[](GC::Root<SVG::SVGImageElement> const& source) -> Gfx::IntSize {
if (auto immutable_bitmap = source->current_image_bitmap())
return immutable_bitmap->size();
// FIXME: This is very janky and not correct.
return { source->width()->anim_val()->value(), source->height()->anim_val()->value() };
},
[](GC::Root<HTMLCanvasElement> const& source) -> Gfx::IntSize {
if (auto painting_surface = source->surface())
return painting_surface->size();
return { source->width(), source->height() };
},
[](GC::Root<ImageBitmap> const& source) -> Gfx::IntSize {
if (auto* bitmap = source->bitmap())
return bitmap->size();
return { source->width(), source->height() };
},
[](GC::Root<OffscreenCanvas> const& source) -> Gfx::IntSize {
if (auto bitmap = source->bitmap())
return bitmap->size();
return {};
},
[](GC::Root<HTMLVideoElement> const& source) -> Gfx::IntSize {
return { source->video_width(), source->video_height() };
});
}
RefPtr<Gfx::DecodedImageFrame> canvas_image_source_frame(CanvasImageSource const& image)
{
return image.visit(
[](OneOf<GC::Root<HTMLImageElement>, GC::Root<SVG::SVGImageElement>> auto const& element) -> RefPtr<Gfx::DecodedImageFrame> {
auto image_data = element->decoded_image_data();
if (!image_data)
return {};
Gfx::IntSize size;
auto intrinsic_width = element->intrinsic_width();
auto intrinsic_height = element->intrinsic_height();
if (intrinsic_width.has_value() && intrinsic_height.has_value())
size = { intrinsic_width->to_int(), intrinsic_height->to_int() };
return image_data->frame(0, size);
},
[](GC::Root<HTMLCanvasElement> const& canvas) -> RefPtr<Gfx::DecodedImageFrame> {
canvas->present();
auto surface = canvas->surface();
if (!surface)
return Gfx::DecodedImageFrame::create(*canvas->get_bitmap_from_surface());
return Gfx::DecodedImageFrame::create(*surface->snapshot_bitmap());
},
[](OneOf<GC::Root<ImageBitmap>, GC::Root<OffscreenCanvas>> auto const& source) -> RefPtr<Gfx::DecodedImageFrame> {
auto bitmap = source->bitmap();
if (!bitmap)
return {};
return Gfx::DecodedImageFrame::create(*bitmap);
},
[](GC::Root<HTMLVideoElement> const& source) -> RefPtr<Gfx::DecodedImageFrame> {
Gfx::ColorSpace color_space;
if (auto* frame_color_space = source->current_frame_color_space())
color_space = *frame_color_space;
auto immutable_bitmap = source->bitmap();
if (!immutable_bitmap)
return {};
auto bitmap = immutable_bitmap->bitmap();
if (!bitmap)
return {};
return Gfx::DecodedImageFrame::create(*bitmap, move(color_space));
});
}
WebIDL::ExceptionOr<void> CanvasDrawImage::draw_image(CanvasImageSource const& image, float destination_x, float destination_y)
{
// If not specified, the dw and dh arguments must default to the values of sw and sh, interpreted such that one CSS pixel in the image is treated as one unit in the output bitmap's coordinate space.
// If the sx, sy, sw, and sh arguments are omitted, then they must default to 0, 0, the image's intrinsic width in image pixels, and the image's intrinsic height in image pixels, respectively.
// If the image has no intrinsic dimensions, then the concrete object size must be used instead, as determined using the CSS "Concrete Object Size Resolution" algorithm, with the specified size having
// neither a definite width nor height, nor any additional constraints, the object's intrinsic properties being those of the image argument, and the default object size being the size of the output bitmap.
auto size = canvas_image_source_dimensions(image);
return draw_image_internal(image, 0, 0, size.width(), size.height(), destination_x, destination_y, size.width(), size.height());
}
WebIDL::ExceptionOr<void> CanvasDrawImage::draw_image(CanvasImageSource const& image, float destination_x, float destination_y, float destination_width, float destination_height)
{
// If the sx, sy, sw, and sh arguments are omitted, then they must default to 0, 0, the image's intrinsic width in image pixels, and the image's intrinsic height in image pixels, respectively.
// If the image has no intrinsic dimensions, then the concrete object size must be used instead, as determined using the CSS "Concrete Object Size Resolution" algorithm, with the specified size having
// neither a definite width nor height, nor any additional constraints, the object's intrinsic properties being those of the image argument, and the default object size being the size of the output bitmap.
auto size = canvas_image_source_dimensions(image);
return draw_image_internal(image, 0, 0, size.width(), size.height(), destination_x, destination_y, destination_width, destination_height);
}
WebIDL::ExceptionOr<void> CanvasDrawImage::draw_image(CanvasImageSource const& image, float source_x, float source_y, float source_width, float source_height, float destination_x, float destination_y, float destination_width, float destination_height)
{
return draw_image_internal(image, source_x, source_y, source_width, source_height, destination_x, destination_y, destination_width, destination_height);
}
}