mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-07 00:29:15 +00:00
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.
This commit is contained in:
parent
2dbf846ff8
commit
bc20e3ac6c
Notes:
github-actions[bot]
2024-08-20 18:01:47 +00:00
Author: https://github.com/kalenikaliaksandr
Commit: bc20e3ac6c
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1143
10 changed files with 25 additions and 32 deletions
Binary file not shown.
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 3.3 KiB |
|
@ -5,12 +5,13 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "BasicShapeStyleValue.h"
|
#include "BasicShapeStyleValue.h"
|
||||||
|
#include <LibGfx/Path.h>
|
||||||
|
|
||||||
namespace Web::CSS {
|
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;
|
bool first = true;
|
||||||
for (auto const& point : points) {
|
for (auto const& point : points) {
|
||||||
Gfx::FloatPoint resolved_point {
|
Gfx::FloatPoint resolved_point {
|
||||||
|
@ -44,7 +45,7 @@ String Polygon::to_string() const
|
||||||
|
|
||||||
BasicShapeStyleValue::~BasicShapeStyleValue() = default;
|
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 m_basic_shape.visit([&](auto const& shape) {
|
||||||
return shape.to_path(reference_box, node);
|
return shape.to_path(reference_box, node);
|
||||||
|
|
|
@ -20,7 +20,7 @@ struct Polygon {
|
||||||
LengthPercentage y;
|
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;
|
String to_string() const;
|
||||||
|
|
||||||
bool operator==(Polygon const&) const = default;
|
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; }
|
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:
|
private:
|
||||||
BasicShapeStyleValue(BasicShape basic_shape)
|
BasicShapeStyleValue(BasicShape basic_shape)
|
||||||
|
|
|
@ -119,6 +119,7 @@ struct PushStackingContext {
|
||||||
Gfx::IntPoint post_transform_translation;
|
Gfx::IntPoint post_transform_translation;
|
||||||
StackingContextTransform transform;
|
StackingContextTransform transform;
|
||||||
Optional<StackingContextMask> mask = {};
|
Optional<StackingContextMask> mask = {};
|
||||||
|
Optional<Gfx::Path> clip_path = {};
|
||||||
|
|
||||||
void translate_by(Gfx::IntPoint const& offset)
|
void translate_by(Gfx::IntPoint const& offset)
|
||||||
{
|
{
|
||||||
|
|
|
@ -475,6 +475,10 @@ void DisplayListPlayerSkia::push_stacking_context(PushStackingContext const& com
|
||||||
canvas.save();
|
canvas.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (command.clip_path.has_value()) {
|
||||||
|
canvas.clipPath(to_skia_path(command.clip_path.value()), true);
|
||||||
|
}
|
||||||
|
|
||||||
if (command.mask.has_value()) {
|
if (command.mask.has_value()) {
|
||||||
auto alpha_mask = alpha_mask_from_bitmap(*command.mask.value().mask_bitmap, command.mask.value().mask_kind);
|
auto alpha_mask = alpha_mask_from_bitmap(*command.mask.value().mask_bitmap, command.mask.value().mask_kind);
|
||||||
SkMatrix mask_matrix;
|
SkMatrix mask_matrix;
|
||||||
|
|
|
@ -314,7 +314,8 @@ void DisplayListRecorder::push_stacking_context(PushStackingContextParams params
|
||||||
.origin = params.transform.origin,
|
.origin = params.transform.origin,
|
||||||
.matrix = params.transform.matrix,
|
.matrix = params.transform.matrix,
|
||||||
},
|
},
|
||||||
.mask = params.mask });
|
.mask = params.mask,
|
||||||
|
.clip_path = params.clip_path });
|
||||||
m_state_stack.append(State());
|
m_state_stack.append(State());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -123,6 +123,7 @@ public:
|
||||||
Gfx::IntRect source_paintable_rect;
|
Gfx::IntRect source_paintable_rect;
|
||||||
StackingContextTransform transform;
|
StackingContextTransform transform;
|
||||||
Optional<StackingContextMask> mask = {};
|
Optional<StackingContextMask> mask = {};
|
||||||
|
Optional<Gfx::Path> clip_path = {};
|
||||||
};
|
};
|
||||||
void push_stacking_context(PushStackingContextParams params);
|
void push_stacking_context(PushStackingContextParams params);
|
||||||
void pop_stacking_context();
|
void pop_stacking_context();
|
||||||
|
|
|
@ -925,30 +925,6 @@ Optional<CSSPixelRect> PaintableBox::get_masking_area() const
|
||||||
return absolute_border_box_rect();
|
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()
|
void PaintableBox::resolve_paint_properties()
|
||||||
{
|
{
|
||||||
auto const& computed_values = this->computed_values();
|
auto const& computed_values = this->computed_values();
|
||||||
|
|
|
@ -31,8 +31,8 @@ public:
|
||||||
virtual void paint(PaintContext&, PaintPhase) const override;
|
virtual void paint(PaintContext&, PaintPhase) const override;
|
||||||
|
|
||||||
virtual Optional<CSSPixelRect> get_masking_area() const;
|
virtual Optional<CSSPixelRect> get_masking_area() const;
|
||||||
virtual Optional<Gfx::Bitmap::MaskKind> get_mask_type() const;
|
virtual Optional<Gfx::Bitmap::MaskKind> get_mask_type() const { return {}; }
|
||||||
virtual RefPtr<Gfx::Bitmap> calculate_mask(PaintContext&, CSSPixelRect const&) const;
|
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& 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()); }
|
Layout::Box const& layout_box() const { return static_cast<Layout::Box const&>(Paintable::layout_node()); }
|
||||||
|
|
|
@ -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();
|
auto has_css_transform = paintable().is_paintable_box() && paintable_box().has_css_transform();
|
||||||
context.display_list_recorder().save();
|
context.display_list_recorder().save();
|
||||||
if (has_css_transform) {
|
if (has_css_transform) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue