From 21fcafe605078c8a798bec3a70cf2e536ac13257 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kalenik Date: Wed, 10 Jun 2026 20:32:51 +0200 Subject: [PATCH] LibGfx: Reduce Painter to the bitmap-compositing interface After 2D canvas stops using Gfx::Painter as its drawing backend, the base Painter interface only needs to describe bitmap compositing work that still happens outside PainterSkia. Narrow the interface to clear_rect, fill_rect and draw_bitmap, which covers GIF/APNG frame compositing, CSS cursor bitmap painting, canvas readback and the Android UI blit. --- Libraries/LibGfx/Painter.h | 23 +-------------- Libraries/LibGfx/PainterSkia.cpp | 45 ------------------------------ Libraries/LibGfx/PainterSkia.h | 26 ++++++++--------- Libraries/LibGfx/VectorGraphic.cpp | 2 +- Libraries/LibGfx/VectorGraphic.h | 4 +-- 5 files changed, 17 insertions(+), 83 deletions(-) diff --git a/Libraries/LibGfx/Painter.h b/Libraries/LibGfx/Painter.h index c5f6e0d055..38fc31acad 100644 --- a/Libraries/LibGfx/Painter.h +++ b/Libraries/LibGfx/Painter.h @@ -7,13 +7,11 @@ #pragma once #include +#include #include #include #include -#include -#include #include -#include namespace Gfx { @@ -27,25 +25,6 @@ public: virtual void fill_rect(Gfx::FloatRect const&, Gfx::Color) = 0; virtual void draw_bitmap(Gfx::FloatRect const& dst_rect, Gfx::DecodedImageFrame const& source, Gfx::IntRect const& src_rect, Gfx::ScalingMode, Optional filters, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator) = 0; - - virtual void stroke_path(Gfx::Path const&, Gfx::Color, float thickness) = 0; - virtual void stroke_path(Gfx::Path const&, Gfx::Color, float thickness, float blur_radius, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::Path::CapStyle, Gfx::Path::JoinStyle, float miter_limit, Vector const& dash_array, float dash_offset) = 0; - virtual void stroke_path(Gfx::Path const&, Gfx::PaintStyle const&, Optional, float thickness, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator) = 0; - virtual void stroke_path(Gfx::Path const&, Gfx::PaintStyle const&, Optional, float thickness, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::Path::CapStyle const&, Gfx::Path::JoinStyle const&, float miter_limit, Vector const&, float dash_offset) = 0; - - virtual void fill_path(Gfx::Path const&, Gfx::Color, Gfx::WindingRule) = 0; - virtual void fill_path(Gfx::Path const&, Gfx::Color, Gfx::WindingRule, float blur_radius, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator) = 0; - virtual void fill_path(Gfx::Path const&, Gfx::PaintStyle const&, Optional, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::WindingRule) = 0; - - virtual void set_transform(Gfx::AffineTransform const&) = 0; - - virtual void save() = 0; - virtual void restore() = 0; - - virtual void clip(Gfx::Path const&, Gfx::WindingRule) = 0; - - virtual void reset() = 0; - virtual void prune_caches() { } }; } diff --git a/Libraries/LibGfx/PainterSkia.cpp b/Libraries/LibGfx/PainterSkia.cpp index 0917e0b667..9a50975d19 100644 --- a/Libraries/LibGfx/PainterSkia.cpp +++ b/Libraries/LibGfx/PainterSkia.cpp @@ -218,22 +218,6 @@ void PainterSkia::set_transform(Gfx::AffineTransform const& transform) canvas.setMatrix(matrix); } -void PainterSkia::stroke_path(Gfx::Path const& path, Gfx::Color color, float thickness) -{ - // Skia treats zero thickness as a special case and will draw a hairline, while we want to draw nothing. - if (thickness <= 0) - return; - - SkPaint paint; - paint.setAntiAlias(true); - paint.setStyle(SkPaint::kStroke_Style); - paint.setStrokeWidth(thickness); - paint.setColor(to_skia_color(color)); - auto sk_path = to_skia_path(path); - auto& canvas = impl().painting_surface->canvas(); - canvas.drawPath(sk_path, paint); -} - void PainterSkia::stroke_path(Gfx::Path const& path, Gfx::Color color, float thickness, float blur_radius, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::Path::CapStyle cap_style, Gfx::Path::JoinStyle join_style, float miter_limit, Vector const& dash_array, float dash_offset) { // Skia treats zero thickness as a special case and will draw a hairline, while we want to draw nothing. @@ -256,24 +240,6 @@ void PainterSkia::stroke_path(Gfx::Path const& path, Gfx::Color color, float thi canvas.drawPath(sk_path, paint); } -void PainterSkia::stroke_path(Gfx::Path const& path, Gfx::PaintStyle const& paint_style, Optional filter, float thickness, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator) -{ - // Skia treats zero thickness as a special case and will draw a hairline, while we want to draw nothing. - if (thickness <= 0) - return; - - auto sk_path = to_skia_path(path); - auto paint = to_skia_paint(paint_style, filter, impl().image_cache); - paint.setAntiAlias(true); - float alpha = paint.getAlphaf(); - paint.setAlphaf(alpha * global_alpha); - paint.setStyle(SkPaint::Style::kStroke_Style); - paint.setStrokeWidth(thickness); - paint.setBlender(to_skia_blender(compositing_and_blending_operator)); - auto& canvas = impl().painting_surface->canvas(); - canvas.drawPath(sk_path, paint); -} - void PainterSkia::stroke_path(Gfx::Path const& path, Gfx::PaintStyle const& paint_style, Optional filter, float thickness, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::Path::CapStyle const& cap_style, Gfx::Path::JoinStyle const& join_style, float miter_limit, Vector const& dash_array, float dash_offset) { // Skia treats zero thickness as a special case and will draw a hairline, while we want to draw nothing. @@ -296,17 +262,6 @@ void PainterSkia::stroke_path(Gfx::Path const& path, Gfx::PaintStyle const& pain canvas.drawPath(sk_path, paint); } -void PainterSkia::fill_path(Gfx::Path const& path, Gfx::Color color, Gfx::WindingRule winding_rule) -{ - SkPaint paint; - paint.setAntiAlias(true); - paint.setColor(to_skia_color(color)); - auto sk_path = to_skia_path(path); - sk_path.setFillType(to_skia_path_fill_type(winding_rule)); - auto& canvas = impl().painting_surface->canvas(); - canvas.drawPath(sk_path, paint); -} - void PainterSkia::fill_path(Gfx::Path const& path, Gfx::Color color, Gfx::WindingRule winding_rule, float blur_radius, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator) { SkPaint paint; diff --git a/Libraries/LibGfx/PainterSkia.h b/Libraries/LibGfx/PainterSkia.h index 0edc33690b..9bfd370cea 100644 --- a/Libraries/LibGfx/PainterSkia.h +++ b/Libraries/LibGfx/PainterSkia.h @@ -8,8 +8,11 @@ #include #include +#include #include #include +#include +#include namespace Gfx { @@ -21,19 +24,16 @@ public: virtual void clear_rect(Gfx::FloatRect const&, Color) override; virtual void fill_rect(Gfx::FloatRect const&, Color) override; virtual void draw_bitmap(Gfx::FloatRect const& dst_rect, Gfx::DecodedImageFrame const& source, Gfx::IntRect const& src_rect, Gfx::ScalingMode, Optional, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator) override; - virtual void stroke_path(Gfx::Path const&, Gfx::Color, float thickness) override; - virtual void stroke_path(Gfx::Path const&, Gfx::Color, float thickness, float blur_radius, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::Path::CapStyle, Gfx::Path::JoinStyle, float miter_limit, Vector const& dash_array, float dash_offset) override; - virtual void stroke_path(Gfx::Path const&, Gfx::PaintStyle const&, Optional, float thickness, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator) override; - virtual void stroke_path(Gfx::Path const&, Gfx::PaintStyle const&, Optional, float thickness, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::Path::CapStyle const&, Gfx::Path::JoinStyle const&, float miter_limit, Vector const&, float dash_offset) override; - virtual void fill_path(Gfx::Path const&, Gfx::Color, Gfx::WindingRule) override; - virtual void fill_path(Gfx::Path const&, Gfx::Color, Gfx::WindingRule, float blur_radius, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator) override; - virtual void fill_path(Gfx::Path const&, Gfx::PaintStyle const&, Optional, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::WindingRule) override; - virtual void set_transform(Gfx::AffineTransform const&) override; - virtual void save() override; - virtual void restore() override; - virtual void clip(Gfx::Path const&, Gfx::WindingRule) override; - virtual void reset() override; - virtual void prune_caches() override; + void stroke_path(Gfx::Path const&, Gfx::Color, float thickness, float blur_radius, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::Path::CapStyle, Gfx::Path::JoinStyle, float miter_limit, Vector const& dash_array, float dash_offset); + void stroke_path(Gfx::Path const&, Gfx::PaintStyle const&, Optional, float thickness, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::Path::CapStyle const&, Gfx::Path::JoinStyle const&, float miter_limit, Vector const&, float dash_offset); + void fill_path(Gfx::Path const&, Gfx::Color, Gfx::WindingRule, float blur_radius, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator); + void fill_path(Gfx::Path const&, Gfx::PaintStyle const&, Optional, float global_alpha, Gfx::CompositingAndBlendingOperator compositing_and_blending_operator, Gfx::WindingRule); + void set_transform(Gfx::AffineTransform const&); + void save(); + void restore(); + void clip(Gfx::Path const&, Gfx::WindingRule); + void reset(); + void prune_caches(); private: struct Impl; diff --git a/Libraries/LibGfx/VectorGraphic.cpp b/Libraries/LibGfx/VectorGraphic.cpp index 5c7efe86b1..3f30496b9a 100644 --- a/Libraries/LibGfx/VectorGraphic.cpp +++ b/Libraries/LibGfx/VectorGraphic.cpp @@ -13,7 +13,7 @@ namespace Gfx { ErrorOr> VectorGraphic::bitmap(IntSize size, AffineTransform transform) const { auto bitmap = TRY(Bitmap::create(Gfx::BitmapFormat::BGRA8888, size)); - auto painter = PainterSkia::create(bitmap); + auto painter = make(PaintingSurface::wrap_bitmap(bitmap)); // Apply the transform then center within destination rectangle (this ignores any translation from the transform): // This allows you to easily rotate or flip the image before painting. diff --git a/Libraries/LibGfx/VectorGraphic.h b/Libraries/LibGfx/VectorGraphic.h index b13c59e495..c78601e27f 100644 --- a/Libraries/LibGfx/VectorGraphic.h +++ b/Libraries/LibGfx/VectorGraphic.h @@ -7,8 +7,8 @@ #pragma once #include +#include #include -#include #include namespace Gfx { @@ -16,7 +16,7 @@ namespace Gfx { class VectorGraphic : public RefCounted { public: virtual IntSize intrinsic_size() const = 0; - virtual void draw(Painter&) const = 0; + virtual void draw(PainterSkia&) const = 0; IntSize size() const { return intrinsic_size(); } IntRect rect() const { return { {}, size() }; }