From 254d040ff4f81a0e04364d5d29a25c98d580cbb5 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 5 Jun 2024 10:10:38 +0200 Subject: [PATCH] LibGfx: Move Gfx::Painter::ScalingMode => Gfx::ScalingMode This will allow users to avoid including Painter.h --- Tests/LibGfx/TestScalingFunctions.cpp | 4 +-- Userland/Libraries/LibAccelGfx/Painter.cpp | 4 +-- Userland/Libraries/LibGfx/Painter.cpp | 34 +++++++++---------- Userland/Libraries/LibGfx/Painter.h | 9 +---- Userland/Libraries/LibGfx/ScalingMode.h | 19 +++++++++++ .../Libraries/LibWeb/CSS/ComputedValues.h | 12 +++---- Userland/Libraries/LibWeb/CSS/StyleComputer.h | 1 + .../LibWeb/HTML/CanvasRenderingContext2D.cpp | 4 +-- Userland/Libraries/LibWeb/Painting/Command.h | 5 +-- .../LibWeb/Painting/CommandExecutorCPU.cpp | 2 +- .../LibWeb/Painting/CommandExecutorCPU.h | 3 +- .../LibWeb/Painting/CommandExecutorGPU.cpp | 12 +++---- .../LibWeb/Painting/RecordingPainter.cpp | 4 +-- .../LibWeb/Painting/RecordingPainter.h | 5 +-- 14 files changed, 67 insertions(+), 51 deletions(-) create mode 100644 Userland/Libraries/LibGfx/ScalingMode.h diff --git a/Tests/LibGfx/TestScalingFunctions.cpp b/Tests/LibGfx/TestScalingFunctions.cpp index eac528e8ab9..aa5a41941a0 100644 --- a/Tests/LibGfx/TestScalingFunctions.cpp +++ b/Tests/LibGfx/TestScalingFunctions.cpp @@ -37,11 +37,11 @@ TEST_CASE(test_painter_scaling_uses_premultiplied_alpha) EXPECT_EQ(bottom_right_pixel, Color::Transparent); }; - test_scaling_mode(Gfx::Painter::ScalingMode::BilinearBlend); + test_scaling_mode(Gfx::ScalingMode::BilinearBlend); // FIXME: Include ScalingMode::SmoothPixels as part of this test // This mode does not currently pass this test, as it behave according to the spec // defined here: https://drafts.csswg.org/css-images/#valdef-image-rendering-pixelated - // test_scaling_mode(Gfx::Painter::ScalingMode::SmoothPixels); + // test_scaling_mode(Gfx::ScalingMode::SmoothPixels); } TEST_CASE(test_bitmap_scaling_uses_premultiplied_alpha) diff --git a/Userland/Libraries/LibAccelGfx/Painter.cpp b/Userland/Libraries/LibAccelGfx/Painter.cpp index 5225832735e..f50ca367bdd 100644 --- a/Userland/Libraries/LibAccelGfx/Painter.cpp +++ b/Userland/Libraries/LibAccelGfx/Painter.cpp @@ -642,13 +642,13 @@ void Painter::blit_canvas(Gfx::IntRect const& dst_rect, Canvas const& canvas, fl void Painter::blit_canvas(Gfx::FloatRect const& dst_rect, Canvas const& canvas, float opacity, Optional affine_transform) { auto texture = GL::Texture(canvas.framebuffer().texture); - blit_scaled_texture(dst_rect, texture, { { 0, 0 }, canvas.size() }, Painter::ScalingMode::NearestNeighbor, opacity, move(affine_transform)); + blit_scaled_texture(dst_rect, texture, { { 0, 0 }, canvas.size() }, ScalingMode::NearestNeighbor, opacity, move(affine_transform)); } void Painter::blit_canvas(Gfx::FloatRect const& dst_rect, Canvas const& canvas, Gfx::FloatRect const& src_rect, float opacity, Optional affine_transform, BlendingMode blending_mode) { auto texture = GL::Texture(canvas.framebuffer().texture); - blit_scaled_texture(dst_rect, texture, src_rect, Painter::ScalingMode::NearestNeighbor, opacity, move(affine_transform), blending_mode); + blit_scaled_texture(dst_rect, texture, src_rect, ScalingMode::NearestNeighbor, opacity, move(affine_transform), blending_mode); } void Painter::blit_scaled_texture(Gfx::FloatRect const& dst_rect, GL::Texture const& texture, Gfx::FloatRect const& src_rect, ScalingMode scaling_mode, float opacity, Optional affine_transform, BlendingMode blending_mode) diff --git a/Userland/Libraries/LibGfx/Painter.cpp b/Userland/Libraries/LibGfx/Painter.cpp index 83b3d37eba1..f8a1e661519 100644 --- a/Userland/Libraries/LibGfx/Painter.cpp +++ b/Userland/Libraries/LibGfx/Painter.cpp @@ -675,7 +675,7 @@ ALWAYS_INLINE static void do_draw_box_sampled_scaled_bitmap(Gfx::Bitmap& target, } } -template +template ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect const& dst_rect, IntRect const& clipped_rect, Gfx::Bitmap const& source, FloatRect const& src_rect, GetPixel get_pixel, float opacity) { auto int_src_rect = enclosing_int_rect(src_rect); @@ -683,7 +683,7 @@ ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect con if (clipped_src_rect.is_empty()) return; - if constexpr (scaling_mode == Painter::ScalingMode::NearestNeighbor || scaling_mode == Painter::ScalingMode::SmoothPixels) { + if constexpr (scaling_mode == ScalingMode::NearestNeighbor || scaling_mode == ScalingMode::SmoothPixels) { if (dst_rect == clipped_rect && int_src_rect == src_rect && !(dst_rect.width() % int_src_rect.width()) && !(dst_rect.height() % int_src_rect.height())) { int hfactor = dst_rect.width() / int_src_rect.width(); int vfactor = dst_rect.height() / int_src_rect.height(); @@ -697,7 +697,7 @@ ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect con } } - if constexpr (scaling_mode == Painter::ScalingMode::BoxSampling) + if constexpr (scaling_mode == ScalingMode::BoxSampling) return do_draw_box_sampled_scaled_bitmap(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity); bool has_opacity = opacity != 1.f; @@ -718,7 +718,7 @@ ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect con auto desired_x = (x - dst_rect.x()) * hscale + src_left; Color src_pixel; - if constexpr (scaling_mode == Painter::ScalingMode::BilinearBlend) { + if constexpr (scaling_mode == ScalingMode::BilinearBlend) { auto shifted_x = desired_x + bilinear_offset_x; auto shifted_y = desired_y + bilinear_offset_y; @@ -739,7 +739,7 @@ ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect con auto bottom = bottom_left.mixed_with(bottom_right, x_ratio); src_pixel = top.mixed_with(bottom, y_ratio); - } else if constexpr (scaling_mode == Painter::ScalingMode::SmoothPixels) { + } else if constexpr (scaling_mode == ScalingMode::SmoothPixels) { auto scaled_x1 = clamp(desired_x >> 32, clipped_src_rect.left(), clipped_src_rect.right() - 1); auto scaled_x0 = clamp(scaled_x1 - 1, clipped_src_rect.left(), clipped_src_rect.right() - 1); auto scaled_y1 = clamp(desired_y >> 32, clipped_src_rect.top(), clipped_src_rect.bottom() - 1); @@ -778,23 +778,23 @@ ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect con } template -ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect const& dst_rect, IntRect const& clipped_rect, Gfx::Bitmap const& source, FloatRect const& src_rect, GetPixel get_pixel, float opacity, Painter::ScalingMode scaling_mode) +ALWAYS_INLINE static void do_draw_scaled_bitmap(Gfx::Bitmap& target, IntRect const& dst_rect, IntRect const& clipped_rect, Gfx::Bitmap const& source, FloatRect const& src_rect, GetPixel get_pixel, float opacity, ScalingMode scaling_mode) { switch (scaling_mode) { - case Painter::ScalingMode::NearestNeighbor: - do_draw_scaled_bitmap(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity); + case ScalingMode::NearestNeighbor: + do_draw_scaled_bitmap(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity); break; - case Painter::ScalingMode::SmoothPixels: - do_draw_scaled_bitmap(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity); + case ScalingMode::SmoothPixels: + do_draw_scaled_bitmap(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity); break; - case Painter::ScalingMode::BilinearBlend: - do_draw_scaled_bitmap(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity); + case ScalingMode::BilinearBlend: + do_draw_scaled_bitmap(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity); break; - case Painter::ScalingMode::BoxSampling: - do_draw_scaled_bitmap(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity); + case ScalingMode::BoxSampling: + do_draw_scaled_bitmap(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity); break; - case Painter::ScalingMode::None: - do_draw_scaled_bitmap(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity); + case ScalingMode::None: + do_draw_scaled_bitmap(target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity); break; } } @@ -1600,7 +1600,7 @@ void Painter::stroke_path(Path const& path, Color color, int thickness) fill_path(path.stroke_to_fill(thickness), color); } -void Painter::draw_scaled_bitmap_with_transform(IntRect const& dst_rect, Bitmap const& bitmap, FloatRect const& src_rect, AffineTransform const& transform, float opacity, Painter::ScalingMode scaling_mode) +void Painter::draw_scaled_bitmap_with_transform(IntRect const& dst_rect, Bitmap const& bitmap, FloatRect const& src_rect, AffineTransform const& transform, float opacity, ScalingMode scaling_mode) { if (transform.is_identity_or_translation_or_scale()) { draw_scaled_bitmap(transform.map(dst_rect.to_type()).to_rounded(), bitmap, src_rect, opacity, scaling_mode); diff --git a/Userland/Libraries/LibGfx/Painter.h b/Userland/Libraries/LibGfx/Painter.h index 9d9e2c62129..f0765549974 100644 --- a/Userland/Libraries/LibGfx/Painter.h +++ b/Userland/Libraries/LibGfx/Painter.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -53,14 +54,6 @@ public: Dashed, }; - enum class ScalingMode { - NearestNeighbor, - SmoothPixels, - BilinearBlend, - BoxSampling, - None, - }; - void clear_rect(IntRect const&, Color); void fill_rect(IntRect const&, Color); void fill_rect(IntRect const&, PaintStyle const&); diff --git a/Userland/Libraries/LibGfx/ScalingMode.h b/Userland/Libraries/LibGfx/ScalingMode.h new file mode 100644 index 00000000000..1567fa283d5 --- /dev/null +++ b/Userland/Libraries/LibGfx/ScalingMode.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2024, Andreas Kling + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +namespace Gfx { + +enum class ScalingMode { + NearestNeighbor, + SmoothPixels, + BilinearBlend, + BoxSampling, + None, +}; + +} diff --git a/Userland/Libraries/LibWeb/CSS/ComputedValues.h b/Userland/Libraries/LibWeb/CSS/ComputedValues.h index dcb91245f0e..0aa9c69bbec 100644 --- a/Userland/Libraries/LibWeb/CSS/ComputedValues.h +++ b/Userland/Libraries/LibWeb/CSS/ComputedValues.h @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include #include @@ -319,19 +319,19 @@ struct BorderRadiusData { }; // FIXME: Find a better place for this helper. -inline Gfx::Painter::ScalingMode to_gfx_scaling_mode(CSS::ImageRendering css_value, Gfx::IntRect source, Gfx::IntRect target) +inline Gfx::ScalingMode to_gfx_scaling_mode(CSS::ImageRendering css_value, Gfx::IntRect source, Gfx::IntRect target) { switch (css_value) { case CSS::ImageRendering::Auto: case CSS::ImageRendering::HighQuality: case CSS::ImageRendering::Smooth: if (target.width() < source.width() || target.height() < source.height()) - return Gfx::Painter::ScalingMode::BoxSampling; - return Gfx::Painter::ScalingMode::BilinearBlend; + return Gfx::ScalingMode::BoxSampling; + return Gfx::ScalingMode::BilinearBlend; case CSS::ImageRendering::CrispEdges: - return Gfx::Painter::ScalingMode::NearestNeighbor; + return Gfx::ScalingMode::NearestNeighbor; case CSS::ImageRendering::Pixelated: - return Gfx::Painter::ScalingMode::SmoothPixels; + return Gfx::ScalingMode::SmoothPixels; } VERIFY_NOT_REACHED(); } diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.h b/Userland/Libraries/LibWeb/CSS/StyleComputer.h index f34a597656a..df8d0f92496 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.h +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index bd0cdae9863..89423ffd98d 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -155,10 +155,10 @@ WebIDL::ExceptionOr CanvasRenderingContext2D::draw_image_internal(CanvasIm // 6. Paint the region of the image argument specified by the source rectangle on the region of the rendering context's output bitmap specified by the destination rectangle, after applying the current transformation matrix to the destination rectangle. draw_clipped([&](auto& painter) { - auto scaling_mode = Gfx::Painter::ScalingMode::NearestNeighbor; + auto scaling_mode = Gfx::ScalingMode::NearestNeighbor; if (drawing_state().image_smoothing_enabled) { // FIXME: Honor drawing_state().image_smoothing_quality - scaling_mode = Gfx::Painter::ScalingMode::BilinearBlend; + scaling_mode = Gfx::ScalingMode::BilinearBlend; } painter.underlying_painter().draw_scaled_bitmap_with_transform(destination_rect.to_rounded(), *bitmap, source_rect, drawing_state().transform, drawing_state().global_alpha, scaling_mode); diff --git a/Userland/Libraries/LibWeb/Painting/Command.h b/Userland/Libraries/LibWeb/Painting/Command.h index 34db0557d6c..c6607b680f6 100644 --- a/Userland/Libraries/LibWeb/Painting/Command.h +++ b/Userland/Libraries/LibWeb/Painting/Command.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -74,7 +75,7 @@ struct DrawScaledBitmap { Gfx::IntRect dst_rect; NonnullRefPtr bitmap; Gfx::IntRect src_rect; - Gfx::Painter::ScalingMode scaling_mode; + Gfx::ScalingMode scaling_mode; [[nodiscard]] Gfx::IntRect bounding_rect() const { return dst_rect; } void translate_by(Gfx::IntPoint const& offset) { dst_rect.translate_by(offset); } @@ -84,7 +85,7 @@ struct DrawScaledImmutableBitmap { Gfx::IntRect dst_rect; NonnullRefPtr bitmap; Gfx::IntRect src_rect; - Gfx::Painter::ScalingMode scaling_mode; + Gfx::ScalingMode scaling_mode; Vector clip_paths; [[nodiscard]] Gfx::IntRect bounding_rect() const { return dst_rect; } diff --git a/Userland/Libraries/LibWeb/Painting/CommandExecutorCPU.cpp b/Userland/Libraries/LibWeb/Painting/CommandExecutorCPU.cpp index b42b1f2c75a..04acc99dd17 100644 --- a/Userland/Libraries/LibWeb/Painting/CommandExecutorCPU.cpp +++ b/Userland/Libraries/LibWeb/Painting/CommandExecutorCPU.cpp @@ -153,7 +153,7 @@ CommandResult CommandExecutorCPU::push_stacking_context(PushStackingContext cons .painter = AK::make(bitmap), .opacity = 1, .destination = command.source_paintable_rect.translated(command.post_transform_translation), - .scaling_mode = Gfx::Painter::ScalingMode::None, + .scaling_mode = Gfx::ScalingMode::None, .mask = command.mask }); painter().translate(-command.source_paintable_rect.location()); return CommandResult::Continue; diff --git a/Userland/Libraries/LibWeb/Painting/CommandExecutorCPU.h b/Userland/Libraries/LibWeb/Painting/CommandExecutorCPU.h index 21aa1310169..93151d53760 100644 --- a/Userland/Libraries/LibWeb/Painting/CommandExecutorCPU.h +++ b/Userland/Libraries/LibWeb/Painting/CommandExecutorCPU.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include @@ -71,7 +72,7 @@ private: MaybeOwned painter; float opacity; Gfx::IntRect destination; - Gfx::Painter::ScalingMode scaling_mode; + Gfx::ScalingMode scaling_mode; Optional mask = {}; }; diff --git a/Userland/Libraries/LibWeb/Painting/CommandExecutorGPU.cpp b/Userland/Libraries/LibWeb/Painting/CommandExecutorGPU.cpp index 1bac7f07ab9..d628e2641f0 100644 --- a/Userland/Libraries/LibWeb/Painting/CommandExecutorGPU.cpp +++ b/Userland/Libraries/LibWeb/Painting/CommandExecutorGPU.cpp @@ -61,15 +61,15 @@ CommandResult CommandExecutorGPU::fill_rect(FillRect const& command) return CommandResult::Continue; } -static AccelGfx::Painter::ScalingMode to_accelgfx_scaling_mode(Gfx::Painter::ScalingMode scaling_mode) +static AccelGfx::Painter::ScalingMode to_accelgfx_scaling_mode(Gfx::ScalingMode scaling_mode) { switch (scaling_mode) { - case Gfx::Painter::ScalingMode::NearestNeighbor: - case Gfx::Painter::ScalingMode::BoxSampling: - case Gfx::Painter::ScalingMode::SmoothPixels: - case Gfx::Painter::ScalingMode::None: + case Gfx::ScalingMode::NearestNeighbor: + case Gfx::ScalingMode::BoxSampling: + case Gfx::ScalingMode::SmoothPixels: + case Gfx::ScalingMode::None: return AccelGfx::Painter::ScalingMode::NearestNeighbor; - case Gfx::Painter::ScalingMode::BilinearBlend: + case Gfx::ScalingMode::BilinearBlend: return AccelGfx::Painter::ScalingMode::Bilinear; default: VERIFY_NOT_REACHED(); diff --git a/Userland/Libraries/LibWeb/Painting/RecordingPainter.cpp b/Userland/Libraries/LibWeb/Painting/RecordingPainter.cpp index 3424ffccc64..dbb296b33f1 100644 --- a/Userland/Libraries/LibWeb/Painting/RecordingPainter.cpp +++ b/Userland/Libraries/LibWeb/Painting/RecordingPainter.cpp @@ -185,7 +185,7 @@ void RecordingPainter::draw_rect(Gfx::IntRect const& rect, Color color, bool rou .rough = rough }); } -void RecordingPainter::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode) +void RecordingPainter::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::ScalingMode scaling_mode) { if (dst_rect.is_empty()) return; @@ -197,7 +197,7 @@ void RecordingPainter::draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bit }); } -void RecordingPainter::draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode, Vector const& clip_paths) +void RecordingPainter::draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::ScalingMode scaling_mode, Vector const& clip_paths) { if (dst_rect.is_empty()) return; diff --git a/Userland/Libraries/LibWeb/Painting/RecordingPainter.h b/Userland/Libraries/LibWeb/Painting/RecordingPainter.h index 1abcb6306b3..511d5f9b9d1 100644 --- a/Userland/Libraries/LibWeb/Painting/RecordingPainter.h +++ b/Userland/Libraries/LibWeb/Painting/RecordingPainter.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -89,8 +90,8 @@ public: void draw_rect(Gfx::IntRect const& rect, Color color, bool rough = false); - void draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode = Gfx::Painter::ScalingMode::NearestNeighbor); - void draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::Painter::ScalingMode scaling_mode = Gfx::Painter::ScalingMode::NearestNeighbor, Vector const& clip_paths = {}); + void draw_scaled_bitmap(Gfx::IntRect const& dst_rect, Gfx::Bitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::ScalingMode scaling_mode = Gfx::ScalingMode::NearestNeighbor); + void draw_scaled_immutable_bitmap(Gfx::IntRect const& dst_rect, Gfx::ImmutableBitmap const& bitmap, Gfx::IntRect const& src_rect, Gfx::ScalingMode scaling_mode = Gfx::ScalingMode::NearestNeighbor, Vector const& clip_paths = {}); void draw_line(Gfx::IntPoint from, Gfx::IntPoint to, Color color, int thickness = 1, Gfx::Painter::LineStyle style = Gfx::Painter::LineStyle::Solid, Color alternate_color = Color::Transparent);