Browse Source

LibWeb: Do not allocate mask bitmap for CSS "clip-path" property

Instead, it could be applied directly as a clip path in Skia painter.

As a side bonus, we get rid of some DeprecatedPath and
AntiAliasingPainter usage.
Aliaksandr Kalenik 11 months ago
parent
commit
bc20e3ac6c

BIN
Tests/LibWeb/Screenshot/images/clip-path-polygon-ref.png


+ 4 - 3
Userland/Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.cpp

@@ -5,12 +5,13 @@
  */
 
 #include "BasicShapeStyleValue.h"
+#include <LibGfx/Path.h>
 
 namespace Web::CSS {
 
-Gfx::DeprecatedPath Polygon::to_path(CSSPixelRect reference_box, Layout::Node const& node) const
+Gfx::Path Polygon::to_path(CSSPixelRect reference_box, Layout::Node const& node) const
 {
-    Gfx::DeprecatedPath path;
+    Gfx::Path path;
     bool first = true;
     for (auto const& point : points) {
         Gfx::FloatPoint resolved_point {
@@ -44,7 +45,7 @@ String Polygon::to_string() const
 
 BasicShapeStyleValue::~BasicShapeStyleValue() = default;
 
-Gfx::DeprecatedPath BasicShapeStyleValue::to_path(CSSPixelRect reference_box, Layout::Node const& node) const
+Gfx::Path BasicShapeStyleValue::to_path(CSSPixelRect reference_box, Layout::Node const& node) const
 {
     return m_basic_shape.visit([&](auto const& shape) {
         return shape.to_path(reference_box, node);

+ 2 - 2
Userland/Libraries/LibWeb/CSS/StyleValues/BasicShapeStyleValue.h

@@ -20,7 +20,7 @@ struct Polygon {
         LengthPercentage y;
     };
 
-    Gfx::DeprecatedPath to_path(CSSPixelRect reference_box, Layout::Node const&) const;
+    Gfx::Path to_path(CSSPixelRect reference_box, Layout::Node const&) const;
     String to_string() const;
 
     bool operator==(Polygon const&) const = default;
@@ -46,7 +46,7 @@ public:
 
     bool properties_equal(BasicShapeStyleValue const& other) const { return m_basic_shape == other.m_basic_shape; }
 
-    Gfx::DeprecatedPath to_path(CSSPixelRect reference_box, Layout::Node const&) const;
+    Gfx::Path to_path(CSSPixelRect reference_box, Layout::Node const&) const;
 
 private:
     BasicShapeStyleValue(BasicShape basic_shape)

+ 1 - 0
Userland/Libraries/LibWeb/Painting/Command.h

@@ -119,6 +119,7 @@ struct PushStackingContext {
     Gfx::IntPoint post_transform_translation;
     StackingContextTransform transform;
     Optional<StackingContextMask> mask = {};
+    Optional<Gfx::Path> clip_path = {};
 
     void translate_by(Gfx::IntPoint const& offset)
     {

+ 4 - 0
Userland/Libraries/LibWeb/Painting/DisplayListPlayerSkia.cpp

@@ -475,6 +475,10 @@ void DisplayListPlayerSkia::push_stacking_context(PushStackingContext const& com
         canvas.save();
     }
 
+    if (command.clip_path.has_value()) {
+        canvas.clipPath(to_skia_path(command.clip_path.value()), true);
+    }
+
     if (command.mask.has_value()) {
         auto alpha_mask = alpha_mask_from_bitmap(*command.mask.value().mask_bitmap, command.mask.value().mask_kind);
         SkMatrix mask_matrix;

+ 2 - 1
Userland/Libraries/LibWeb/Painting/DisplayListRecorder.cpp

@@ -314,7 +314,8 @@ void DisplayListRecorder::push_stacking_context(PushStackingContextParams params
             .origin = params.transform.origin,
             .matrix = params.transform.matrix,
         },
-        .mask = params.mask });
+        .mask = params.mask,
+        .clip_path = params.clip_path });
     m_state_stack.append(State());
 }
 

+ 1 - 0
Userland/Libraries/LibWeb/Painting/DisplayListRecorder.h

@@ -123,6 +123,7 @@ public:
         Gfx::IntRect source_paintable_rect;
         StackingContextTransform transform;
         Optional<StackingContextMask> mask = {};
+        Optional<Gfx::Path> clip_path = {};
     };
     void push_stacking_context(PushStackingContextParams params);
     void pop_stacking_context();

+ 0 - 24
Userland/Libraries/LibWeb/Painting/PaintableBox.cpp

@@ -925,30 +925,6 @@ Optional<CSSPixelRect> PaintableBox::get_masking_area() const
     return absolute_border_box_rect();
 }
 
-Optional<Gfx::Bitmap::MaskKind> PaintableBox::get_mask_type() const
-{
-    // Always an alpha mask as only basic shapes are supported right now.
-    return Gfx::Bitmap::MaskKind::Alpha;
-}
-
-RefPtr<Gfx::Bitmap> PaintableBox::calculate_mask(PaintContext& context, CSSPixelRect const& masking_area) const
-{
-    VERIFY(computed_values().clip_path()->is_basic_shape());
-    auto const& basic_shape = computed_values().clip_path()->basic_shape();
-    auto path = basic_shape.to_path(masking_area, layout_node());
-    auto device_pixel_scale = context.device_pixels_per_css_pixel();
-    path = path.copy_transformed(Gfx::AffineTransform {}.set_scale(device_pixel_scale, device_pixel_scale));
-    auto mask_rect = context.enclosing_device_rect(masking_area);
-    auto maybe_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, mask_rect.size().to_type<int>());
-    if (maybe_bitmap.is_error())
-        return {};
-    auto bitmap = maybe_bitmap.release_value();
-    Gfx::DeprecatedPainter painter(*bitmap);
-    Gfx::AntiAliasingPainter aa_painter(painter);
-    aa_painter.fill_path(path, Color::Black);
-    return bitmap;
-}
-
 void PaintableBox::resolve_paint_properties()
 {
     auto const& computed_values = this->computed_values();

+ 2 - 2
Userland/Libraries/LibWeb/Painting/PaintableBox.h

@@ -31,8 +31,8 @@ public:
     virtual void paint(PaintContext&, PaintPhase) const override;
 
     virtual Optional<CSSPixelRect> get_masking_area() const;
-    virtual Optional<Gfx::Bitmap::MaskKind> get_mask_type() const;
-    virtual RefPtr<Gfx::Bitmap> calculate_mask(PaintContext&, CSSPixelRect const&) const;
+    virtual Optional<Gfx::Bitmap::MaskKind> get_mask_type() const { return {}; }
+    virtual RefPtr<Gfx::Bitmap> calculate_mask(PaintContext&, CSSPixelRect const&) const { return {}; }
 
     Layout::Box& layout_box() { return static_cast<Layout::Box&>(Paintable::layout_node()); }
     Layout::Box const& layout_box() const { return static_cast<Layout::Box const&>(Paintable::layout_node()); }

+ 9 - 0
Userland/Libraries/LibWeb/Painting/StackingContext.cpp

@@ -315,6 +315,15 @@ void StackingContext::paint(PaintContext& context) const
         }
     }
 
+    auto const& computed_values = paintable().computed_values();
+    if (auto clip_path = computed_values.clip_path(); clip_path.has_value() && clip_path->is_basic_shape()) {
+        auto const& masking_area = paintable_box().get_masking_area();
+        auto const& basic_shape = computed_values.clip_path()->basic_shape();
+        auto path = basic_shape.to_path(*masking_area, paintable().layout_node());
+        auto device_pixel_scale = context.device_pixels_per_css_pixel();
+        push_stacking_context_params.clip_path = path.copy_transformed(Gfx::AffineTransform {}.set_scale(device_pixel_scale, device_pixel_scale).set_translation(source_paintable_rect.location().to_type<float>()));
+    }
+
     auto has_css_transform = paintable().is_paintable_box() && paintable_box().has_css_transform();
     context.display_list_recorder().save();
     if (has_css_transform) {