LibWeb: Implement support for drawing with CanvasPattern

We already had the API, but drawing to the canvas was not affected by
any created CanvasPattern. This moves CanvasPatternPaintStyle to LibGfx
so we don't have to reach into LibWeb, and implements the plumbing to
let Skia use images as a fill pattern.
This commit is contained in:
Jelle Raaijmakers 2025-10-22 20:00:17 +02:00 committed by Tim Ledbetter
parent 9753b8e62c
commit 62ae4e878f
Notes: github-actions[bot] 2025-10-23 12:21:23 +00:00
20 changed files with 237 additions and 167 deletions

View file

@ -21,6 +21,8 @@
#include <LibWeb/HTML/CanvasRenderingContext2D.h>
#include <LibWeb/HTML/HTMLCanvasElement.h>
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/HTML/HTMLMediaElement.h>
#include <LibWeb/HTML/HTMLVideoElement.h>
#include <LibWeb/HTML/ImageBitmap.h>
#include <LibWeb/HTML/ImageData.h>
#include <LibWeb/HTML/ImageRequest.h>
@ -135,31 +137,7 @@ WebIDL::ExceptionOr<void> CanvasRenderingContext2D::draw_image_internal(CanvasIm
if (usability == CanvasImageSourceUsability::Bad)
return {};
auto bitmap = image.visit(
[](GC::Root<HTMLImageElement> const& source) -> RefPtr<Gfx::ImmutableBitmap> {
auto width = source->intrinsic_width().value_or({}).to_int();
auto height = source->intrinsic_height().value_or({}).to_int();
return source->default_image_bitmap_sized({ width, height });
},
[](GC::Root<SVG::SVGImageElement> const& source) -> RefPtr<Gfx::ImmutableBitmap> {
auto width = source->intrinsic_width().value_or({}).to_int();
auto height = source->intrinsic_height().value_or({}).to_int();
return source->default_image_bitmap_sized({ width, height });
},
[](GC::Root<OffscreenCanvas> const& source) -> RefPtr<Gfx::ImmutableBitmap> { return Gfx::ImmutableBitmap::create(*source->bitmap()); },
[](GC::Root<HTMLCanvasElement> const& source) -> RefPtr<Gfx::ImmutableBitmap> {
auto surface = source->surface();
if (!surface)
return {};
return Gfx::ImmutableBitmap::create_snapshot_from_painting_surface(*surface);
},
[](GC::Root<HTMLVideoElement> const& source) -> RefPtr<Gfx::ImmutableBitmap> { return Gfx::ImmutableBitmap::create(*source->bitmap()); },
[](GC::Root<ImageBitmap> const& source) -> RefPtr<Gfx::ImmutableBitmap> {
auto* bitmap = source->bitmap();
if (!bitmap)
return {};
return Gfx::ImmutableBitmap::create(*bitmap);
});
auto bitmap = canvas_image_source_bitmap(image);
if (!bitmap)
return {};