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

@ -1,16 +1,17 @@
/*
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
* Copyright (c) 2024-2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Painting/DevicePixelConverter.h>
#include <LibWeb/Painting/DisplayList.h>
namespace Web::Painting {
void DisplayList::append(Command&& command, Optional<i32> scroll_frame_id)
void DisplayList::append(Command&& command, Optional<i32> scroll_frame_id, RefPtr<ClipFrame const> clip_frame)
{
m_commands.append({ scroll_frame_id, move(command) });
m_commands.append({ scroll_frame_id, clip_frame, move(command) });
}
String DisplayList::dump() const
@ -56,6 +57,35 @@ void DisplayListPlayer::execute(DisplayList& display_list, ScrollStateSnapshot c
}
}
void DisplayListPlayer::apply_clip_frame(ClipFrame const& clip_frame, DevicePixelConverter const& device_pixel_converter)
{
auto const& clip_rects = clip_frame.clip_rects();
if (clip_rects.is_empty())
return;
save({});
for (auto const& clip_rect : clip_rects) {
auto css_rect = clip_rect.rect;
if (auto scroll_frame = clip_rect.enclosing_scroll_frame) {
css_rect.translate_by(scroll_frame->cumulative_offset());
}
auto device_rect = device_pixel_converter.rounded_device_rect(css_rect).to_type<int>();
auto corner_radii = clip_rect.corner_radii.as_corners(device_pixel_converter);
if (corner_radii.has_any_radius()) {
add_rounded_rect_clip({ .corner_radii = corner_radii, .border_rect = device_rect, .corner_clip = CornerClip::Outside });
} else {
add_clip_rect({ .rect = device_rect });
}
}
}
void DisplayListPlayer::remove_clip_frame(ClipFrame const& clip_frame)
{
if (clip_frame.clip_rects().is_empty())
return;
restore({});
}
void DisplayListPlayer::execute_impl(DisplayList& display_list, ScrollStateSnapshot const& scroll_state, RefPtr<Gfx::PaintingSurface> surface)
{
if (surface)
@ -68,11 +98,36 @@ void DisplayListPlayer::execute_impl(DisplayList& display_list, ScrollStateSnaps
auto const& commands = display_list.commands();
auto device_pixels_per_css_pixel = display_list.device_pixels_per_css_pixel();
DevicePixelConverter device_pixel_converter { device_pixels_per_css_pixel };
VERIFY(!m_surfaces.is_empty());
Vector<RefPtr<ClipFrame const>> clip_frames_stack;
clip_frames_stack.append({});
for (size_t command_index = 0; command_index < commands.size(); command_index++) {
auto scroll_frame_id = commands[command_index].scroll_frame_id;
auto command = commands[command_index].command;
auto [scroll_frame_id, clip_frame, command] = commands[command_index];
if (clip_frames_stack.last() != clip_frame) {
if (auto clip_frame = clip_frames_stack.take_last()) {
remove_clip_frame(*clip_frame);
}
clip_frames_stack.append(clip_frame);
if (clip_frame) {
apply_clip_frame(*clip_frame, device_pixel_converter);
}
}
// After entering a new stacking context, we keep the outer clip frame applied.
// This is necessary when the stacking context has a CSS transform, and all
// nested ClipFrames aggregate clip rectangles only up to the stacking context
// node.
if (command.has<PushStackingContext>()) {
clip_frames_stack.append({});
} else if (command.has<PopStackingContext>()) {
if (auto clip_frame = clip_frames_stack.take_last()) {
remove_clip_frame(*clip_frame);
}
}
if (command.has<PaintScrollBar>()) {
auto& paint_scroll_bar = command.get<PaintScrollBar>();
@ -159,6 +214,12 @@ void DisplayListPlayer::execute_impl(DisplayList& display_list, ScrollStateSnaps
// clang-format on
}
while (!clip_frames_stack.is_empty()) {
if (auto clip_frame = clip_frames_stack.take_last()) {
remove_clip_frame(*clip_frame);
}
}
if (surface)
flush();
}