LibWeb: Expand ClipFrame into clip rectangles during display list replay

Until now, every paint phase of every PaintableBox injected its own
clipping sequence into the display list:
```
before_paint: Save
              AddClipRect (1)
              ...clip rectangles for each containing block with clip...
              AddClipRect (N)

paint:        ...paint phase items...

after_paint:  Restore
```

Because we ran that sequence for every phase of every box, Skia had to
rebuild clip stack `paint_phases * paintable_boxes` times. Worse,
usually most paint phases contribute no visible drawing at all, yet we
still had to emit clipping items because `before_paint()` has no way to
know that in advance.

This change takes a different approach:
- Clip information is now attached as metadata `ClipFrame` to each
  DisplayList item.
- `DisplayListPlayer` groups consecutive commands that share a
  `ClipFrame`, applying the clip once at the start of the group and
  restoring it once at the end.

Going from 10 ms to 5 ms in rasterization on Discord might not sound
like much, but keep in mind that for 60fps we have 16 ms per frame and
there is a lot more work besides display list rasterization we do in
each frame.

* https://discord.com/channels/1247070541085671459/1247090064480014443
  - DisplayList items:  81844  -> 3671
  - rasterize time:     10 ms  -> 5 ms
  - record time:        5 ms   -> 3 ms

* https://github.com/LadybirdBrowser/ladybird
  - DisplayList items:  7902  -> 1176
  - rasterize time:     4 ms  -> 4 ms
  - record time:        3 ms  -> 2 ms
This commit is contained in:
Aliaksandr Kalenik 2025-07-13 03:55:24 +02:00 committed by Jelle Raaijmakers
commit eed47acb1f
Notes: github-actions[bot] 2025-07-14 13:49:39 +00:00
8 changed files with 104 additions and 72 deletions

View file

@ -327,7 +327,7 @@ void PaintableBox::before_paint(PaintContext& context, PaintPhase phase) const
return;
if (first_is_one_of(phase, PaintPhase::Background, PaintPhase::Foreground) && own_clip_frame()) {
apply_clip(context, own_clip_frame());
context.display_list_recorder().push_clip_frame(own_clip_frame());
} else if (!has_css_transform()) {
apply_clip_overflow_rect(context, phase);
}
@ -341,7 +341,7 @@ void PaintableBox::after_paint(PaintContext& context, PaintPhase phase) const
reset_scroll_offset(context);
if (first_is_one_of(phase, PaintPhase::Background, PaintPhase::Foreground) && own_clip_frame()) {
restore_clip(context, own_clip_frame());
context.display_list_recorder().pop_clip_frame();
} else if (!has_css_transform()) {
clear_clip_overflow_rect(context, phase);
}
@ -647,39 +647,6 @@ Optional<CSSPixelRect> PaintableBox::clip_rect_for_hit_testing() const
return {};
}
void PaintableBox::apply_clip(PaintContext& context, RefPtr<ClipFrame const> const& from_clip_frame)
{
auto const& clip_rects = from_clip_frame->clip_rects();
if (clip_rects.is_empty())
return;
auto& display_list_recorder = context.display_list_recorder();
display_list_recorder.save();
for (auto const& clip_rect : clip_rects) {
Optional<i32> clip_scroll_frame_id;
if (clip_rect.enclosing_scroll_frame)
clip_scroll_frame_id = clip_rect.enclosing_scroll_frame->id();
display_list_recorder.push_scroll_frame_id(clip_scroll_frame_id);
auto rect = context.rounded_device_rect(clip_rect.rect).to_type<int>();
auto corner_radii = clip_rect.corner_radii.as_corners(context.device_pixel_converter());
if (corner_radii.has_any_radius()) {
display_list_recorder.add_rounded_rect_clip(corner_radii, rect, CornerClip::Outside);
} else {
display_list_recorder.add_clip_rect(rect);
}
display_list_recorder.pop_scroll_frame_id();
}
}
void PaintableBox::restore_clip(PaintContext& context, RefPtr<ClipFrame const> const& from_clip_frame)
{
auto const& clip_rects = from_clip_frame->clip_rects();
if (clip_rects.is_empty())
return;
context.display_list_recorder().restore();
}
void PaintableBox::apply_scroll_offset(PaintContext& context) const
{
if (scroll_frame_id().has_value()) {
@ -702,7 +669,7 @@ void PaintableBox::apply_clip_overflow_rect(PaintContext& context, PaintPhase ph
if (!AK::first_is_one_of(phase, PaintPhase::Background, PaintPhase::Border, PaintPhase::TableCollapsedBorder, PaintPhase::Foreground, PaintPhase::Outline))
return;
apply_clip(context, enclosing_clip_frame());
context.display_list_recorder().push_clip_frame(enclosing_clip_frame());
}
void PaintableBox::clear_clip_overflow_rect(PaintContext& context, PaintPhase phase) const
@ -713,7 +680,7 @@ void PaintableBox::clear_clip_overflow_rect(PaintContext& context, PaintPhase ph
if (!AK::first_is_one_of(phase, PaintPhase::Background, PaintPhase::Border, PaintPhase::TableCollapsedBorder, PaintPhase::Foreground, PaintPhase::Outline))
return;
restore_clip(context, enclosing_clip_frame());
context.display_list_recorder().pop_clip_frame();
}
void paint_cursor_if_needed(PaintContext& context, TextPaintable const& paintable, PaintableFragment const& fragment)