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,5 +1,5 @@
/*
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
* Copyright (c) 2024-2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -15,6 +15,7 @@
#include <LibGfx/ImmutableBitmap.h>
#include <LibGfx/PaintStyle.h>
#include <LibWeb/CSS/Enums.h>
#include <LibWeb/Painting/ClipFrame.h>
#include <LibWeb/Painting/Command.h>
#include <LibWeb/Painting/ScrollState.h>
@ -74,6 +75,9 @@ private:
virtual void apply_mask_bitmap(ApplyMaskBitmap const&) = 0;
virtual bool would_be_fully_clipped_by_painter(Gfx::IntRect) const = 0;
void apply_clip_frame(ClipFrame const&, DevicePixelConverter const&);
void remove_clip_frame(ClipFrame const&);
Vector<NonnullRefPtr<Gfx::PaintingSurface>, 1> m_surfaces;
};
@ -84,10 +88,11 @@ public:
return adopt_ref(*new DisplayList());
}
void append(Command&& command, Optional<i32> scroll_frame_id);
void append(Command&& command, Optional<i32> scroll_frame_id, RefPtr<ClipFrame const>);
struct CommandListItem {
Optional<i32> scroll_frame_id;
RefPtr<ClipFrame const> clip_frame;
Command command;
};