Commit Graph

41 Commits

Author SHA1 Message Date
Jelle Raaijmakers
90a211bf47 LibWeb: Use device-pixel coordinates in display list and AVC
Stop converting between CSS and device pixels as part of rendering - the
display list should be as simple as possible, so convert to DevicePixels
once when constructing the display list.
2026-02-26 07:43:00 +01:00
Luke Wilde
21713377e2 LibWeb: Paint SVG-in-img with a nested display list
This makes SVG-in-img appear sharp no matter the current
(pinch-to-)zoom level.
2026-02-24 19:13:23 +01:00
Aliaksandr Kalenik
efbefb3b59 LibWeb: Skip display list commands under zero-area clips
Add a pre-computed `has_empty_effective_clip` flag on
AccumulatedVisualContext that propagates from parent to child. When a
clip rect or clip path has zero area, all descendant commands are
skipped at display list recording time in `DisplayList::append()`,
so they are never stored or executed.

This allows skipping ~10% of display list commands in the Discord app.
2026-02-24 16:41:20 +01:00
Aliaksandr Kalenik
beb7971762 Tests: Add display list test for zero-area clip commands 2026-02-24 16:41:20 +01:00
Aliaksandr Kalenik
1fc4c69ad8 LibWeb: Expand PaintNestedDisplayList in internals.dumpDisplayList()
Previously, PaintNestedDisplayList was treated as an opaque command,
printing only its name and rect without showing the nested display
list's contents. This made it impossible to debug painting issues
involving SVG masks/clips, CSS background-clip: text, and iframe
content through display list dumps.

Refactor the command dump loop into a recursive lambda that expands
nested display lists inline with increased indentation.
2026-02-24 14:37:29 +01:00
Aliaksandr Kalenik
d7a8db671b LibWeb: Skip overflow clip generation for SVG inner elements
Per the CSS Overflow spec, overflow properties apply only to block
containers, flex containers, and grid containers — not SVG graphics
elements. Add an `is<SVGPaintable>` check in
`overflow_property_applies()` to return false for SVG inner elements
like `<g>`, `<rect>`, `<path>`.

This doesn't affect `<svg>` elements (which use `SVGSVGPaintable`, a
direct `PaintableBox` subclass) or `<foreignObject>` (which uses
`SVGForeignObjectPaintable`, a `PaintableWithLines` subclass) — both
correctly keep their overflow clips.
2026-02-24 12:28:55 +01:00
Aliaksandr Kalenik
5a073fdbf1 Tests: Add display list test for overflow on SVG inner elements
This test captures the current (incorrect) behavior where setting
`overflow: hidden` on an SVG `<g>` element produces a clip in the
display list. Per the CSS Overflow spec, overflow properties only apply
to block containers, flex containers, and grid containers — not SVG
graphics elements.
2026-02-24 12:28:55 +01:00
Aliaksandr Kalenik
5ef132ba1a LibWeb: Replace AddMask/clipShader with saveLayer+DstIn compositing
This applies the same pattern used for background-clip: text (commit
f2e6f70fbb).

Results in visible performance improvement in Discord app where
previously, according to profiles, we spent lots of time allocating
surfaces for masks.
2026-02-24 07:14:16 +01:00
Aliaksandr Kalenik
533228f8ad LibWeb: Invalidate stacking context tree after partial SVG relayout
relayout_svg_root() clears individual stacking contexts via
reset_for_relayout() but didn't call invalidate_stacking_context_tree().
The viewport's stacking context remained non-null, so
build_stacking_context_tree_if_needed() skipped the rebuild. This caused
foreignObject to lose its stacking context after relayout, breaking SVG
mask application.
2026-02-24 06:15:07 +01:00
Aliaksandr Kalenik
19d3ca90b2 Tests: Add display list test for SVG foreignObject mask after relayout
This test verifies that SVG mask application on foreignObject is
preserved after partial SVG relayout. Currently the mask is not applied
to the foreignObject content after relayout because the stacking context
tree is not rebuilt.
2026-02-24 06:15:07 +01:00
Aliaksandr Kalenik
36acd1cf3d LibWeb: Rebuild stacking context tree on any z-index value change
The stacking context tree rebuild had an optimization that skipped
rebuilding when a property changed between two values that both create
stacking contexts. This is correct for most properties (e.g. opacity
0.5 -> 0.8 doesn't change tree structure), but incorrect for z-index.

During tree construction, elements with z-index 0/auto are placed in
m_positioned_descendants_and_stacking_contexts_with_stack_level_0,
while elements with other z-index values are painted from m_children
(negative at step 3, positive at step 9 per CSS 2.1 Appendix E).
When z-index changed between non-auto values (e.g. 0 -> 10), the
optimization skipped the rebuild, leaving the element in the wrong
list and causing it to be painted from both step 8 and step 9.

This was visible on pages like shopify.com where elements with
transition-all would transition z-index, producing a flood of
"Painting commands are recorded twice for stacking context" messages.
2026-02-17 20:44:46 +01:00
Aliaksandr Kalenik
94d7c76226 LibWeb: Add test for z-index change causing double paint in display list
When z-index changes from 0 to a positive value on a positioned element,
the stacking context tree is not rebuilt, causing the element to be
painted twice. This test captures the current (incorrect) behavior.
2026-02-17 20:44:46 +01:00
Jelle Raaijmakers
cca35080e6 Tests/LibWeb: Rebaseline failing input-caret.html test 2026-02-11 12:50:18 +01:00
Jelle Raaijmakers
d3a2e4bbbb LibWeb: Draw caret for empty contenteditable elements
We don't have a text node in these, so no fragments either. Maintaining
a special case for this situation seems much simpler than reworking
`contenteditable`s to always have a fragment.
2026-02-11 11:17:27 +01:00
Jelle Raaijmakers
3bc4374344 LibWeb: Draw caret for empty <input>s and <textarea>s
Our change to generate spans in PaintableWithLines from fragments also
broke drawing the caret for empty <input>s and <textarea>s, since spans
was empty in that case.

Fix this by moving caret drawing to PaintableWithLines, and only
invoking it if we have a cursor position set.
2026-02-11 11:17:27 +01:00
Aliaksandr Kalenik
da2c07c66d LibWeb: Apply intermediate stacking context effects to abspos elements
Previously, absolutely positioned elements jumped directly to their
containing block's accumulated visual context, skipping effects
(opacity, mix-blend-mode, isolation) from intermediate ancestors. Per
CSS spec, these properties create stacking contexts that abspos elements
cannot escape — they only escape scroll containers and overflow clips.
2026-02-04 20:54:47 +01:00
Aliaksandr Kalenik
44d90af3d6 LibGfx+LibWeb: Cache SkTextBlob in GlyphRun
Previously, SkTextBlob was built on every paint in
DisplayListPlayerSkia::draw_glyph_run(), which meant:
- Repeated work when the same display list is painted multiple times
- Glyph arrays were allocated and populated on each paint

Now the blob is built once during display list recording and cached in
GlyphRun.
2026-01-24 15:22:03 +01:00
Gingeh
7b3afbc11c LibWeb: Paint element overlay during Foreground paint phase
Also removed the FocusAndOverlay phase because it is now useless
2026-01-24 11:54:39 +01:00
Aliaksandr Kalenik
3e54291813 LibWeb: Move VisualViewport transform to AccumulatedVisualContext tree
Move the visual viewport (pinch-to-zoom) transform from a reserved slot
in DisplayList to the AccumulatedVisualContext tree as a root transform
node. Fixed position elements now correctly inherit from this context.

This requires rebuilding the context tree and display list on each zoom
change, but this overhead will be eliminated by future partial context
tree rebuilds.
2026-01-23 18:56:24 +01:00
Aliaksandr Kalenik
660e1a62b2 LibWeb: Remove clip-path handling from PaintableBox::get_masking_area()
This is now handled by AccumulatedVisualContext.
2026-01-21 19:10:26 +01:00
Aliaksandr Kalenik
96a39aeaa6 LibWeb: Move effects application into AccumulatedVisualContext
Effects (opacity, blend mode, filters) must be applied in the parent's
coordinate space, before the element's transform. Previously this was
handled by manually switching to the parent's visual context when
applying effects at paint time.

By adding EffectsData to AccumulatedVisualContext and positioning it
before TransformData in the chain, effects are now naturally applied in
the correct order during display list replay, eliminating the special
case in StackingContext::paint().

For SVG filters that can generate content from empty elements (feFlood,
feImage, feTurbulence), a transparent FillRect command is emitted to
trigger the filter through the same AVC pipeline.
2026-01-21 16:19:18 +01:00
Aliaksandr Kalenik
69ffe99054 LibWeb: Avoid duplicate transform/perspective context allocations
For position:relative/static elements, use visual parent's state
directly instead of containing block's state + intermediate walk.
This reuses existing context nodes, avoids duplicate allocations,
and eliminates the intermediate ancestor vector construction.
2026-01-19 11:39:47 +01:00
Aliaksandr Kalenik
365e8abd55 Tests/LibWeb: Add test for duplicate transform context allocation
This test demonstrates an issue where TransformData/PerspectiveData can
be allocated twice for the same paintable box when a position:relative
element has an inline transformed ancestor.
2026-01-19 11:39:47 +01:00
Aliaksandr Kalenik
c9a8ca3b8c LibWeb: Rebuild AccumulatedVisualContext on clip property changes
AccumulatedVisualContext nodes capture a snapshot of the current clip
rect when the tree is built, so we must invalidate whenever the clip
property changes.
2026-01-19 04:01:37 +01:00
Aliaksandr Kalenik
f68b6cd0f0 Tests/LibWeb: Add failing test for clip property invalidation
This test verifies that modifying the CSS clip property via JavaScript
properly invalidates the accumulated visual context. Currently fails
because the clip rect in the display list remains unchanged after the
style update.
2026-01-19 04:01:37 +01:00
Aliaksandr Kalenik
eb860e2f10 LibWeb: Rebuild AccumulatedVisualContext on clip-path changes
AccumulatedVisualContext nodes capture a snapshot of the current
clip-path when the tree is built, so we must invalidate whenever the
clip-path changes.

Fixes regression introduced in 98afd82
2026-01-19 04:01:37 +01:00
Aliaksandr Kalenik
d208fa4316 Tests/LibWeb: Add failing test for clip-path invalidation
This test verifies that modifying clip-path via JavaScript properly
invalidates the accumulated visual context. Currently fails because the
clip-path in the display list remains unchanged after the style update.

Regression from 98afd82491.
2026-01-19 04:01:37 +01:00
Aliaksandr Kalenik
a87b5c722d LibWeb: Add AccumulatedVisualContext debugging infrastructure 2026-01-15 19:50:53 +01:00
Aliaksandr Kalenik
009ddd4823 LibWeb: Integrate AccumulatedVisualContext with display list
Integrate AccumulatedVisualContext with display list recording and
playback. This is the main commit of the refactoring that delivers the
architectural improvements enabled by AccumulatedVisualContext.

Recording changes:

Each display list command now stores a single
RefPtr<AccumulatedVisualContext> instead of separate scroll_frame_id
and ClipFrame. The recorder simply captures the current accumulated
context when appending commands.

The before_paint()/after_paint() hooks that pushed/popped scroll frame
IDs are replaced by directly setting accumulated_visual_context on the
recorder before painting each element.

Playback changes:

The display list player now uses LCA (Lowest Common Ancestor) based
traversal to switch between visual contexts efficiently. When
transitioning from context A to context B:

1. Find the LCA of A and B in the context tree
2. Pop (restore) states back to the LCA depth
3. Push (save + apply) states from LCA down to B

This approach minimizes redundant save/restore operations. For example,
when rendering siblings that share a common scroll container, the
player keeps that scroll state applied and only switches the divergent
parts of their context chains.

Key deletions:

- Remove translate_by() from all 45 display list commands - commands
  are now immutable
- Remove transform/perspective fields from PushStackingContext -
  transforms are tracked via AccumulatedVisualContext
- Remove push_scroll_frame_id()/pop_scroll_frame_id() from
  DisplayListRecorder
- Remove before_paint()/after_paint() hooks from Paintable
- Merge ApplyOpacity, ApplyCompositeAndBlendingOperator, ApplyFilter
  into single ApplyEffects command

Stacking context painting changes:

The StackingContext::paint() method is significantly simplified.
Instead of building a PushStackingContextParams struct with transform
matrices and pushing/popping stacking contexts, it now:

1. Sets the accumulated visual context (which already contains
   transforms)
2. Applies effects (opacity, blend mode, filters) if needed
3. Applies clip path if needed
4. Paints the content
5. Restores state

The visual state management that was interleaved throughout the
painting code is now handled uniformly by the context tree.
2026-01-15 19:50:53 +01:00
InvalidUsernameException
a08c175da2 LibWeb: Display clip rectangles when dumping display list 2025-12-01 17:46:44 +01:00
Jelle Raaijmakers
c0d08b68af LibWeb: Dump path_bounding_rect for FillPath
This makes it easier to differentiate between FillPath commands in the
display list output.
2025-10-27 16:42:27 -07:00
Aliaksandr Kalenik
9862d8b4a6 LibWeb: Implement pinch-to-zoom support
Adds pinch event handling that adjusts the VisualViewport scale and
offset. VisualViewport's (offset, scale) is then used to construct a
transformation matrix which is applied before display list execution.
2025-10-10 15:37:45 +02:00
Aliaksandr Kalenik
719a50c9bf LibWeb: Don't emit Push{Pop}StackingContext without visible effect
Before this change we would emit PushStackingContext/PopStackingContext
display list items regardless of whether the stacking context had any
transform/opacity/clip effects.

Display list size on https://x.com/ladybirdbrowser is reduced from ~2700
to ~800 items.
2025-09-23 19:05:01 +02:00
Callum Law
9aa2d1bd3e LibWeb: Make text-decoration lines entire width of fragment
This fixes an issue where text decorations (e.g. underlines) of text
split across multiple fragments would have unintended 1px gaps.

Gains us 2 WPT passes (imported)
2025-09-12 07:07:15 +01:00
Jelle Raaijmakers
0cf6bd0324 LibWeb: Maintain rect positioning when rounding to device pixel rects
When rounding a CSSPixelRect to a DevicePixelRect, we simply pulled its
width and height through round() and called it a day. Unfortunately this
could negatively affect the rect's perceived positioning.

A rect at { 0.5, 0.0 } with size { 19.5 x 20.0 } should have its right
edge at position 20, but after rounding it would end up at { 1, 0 } with
size { 20 x 20 }, causing its right edge to be at position 21 instead.

Fix this by first rounding the right and bottom edges of the input rect,
and then determining the dimensions by subtracting its rounded position.

Fixes #245.
2025-08-19 21:53:46 +02:00
Jelle Raaijmakers
9e29d0c040 LibWeb: Make button layout wrappers inherit styles correctly
There are some nuances to creating these wrappers, such as manually
propagating certain text styles that are not inherited by default. We
already have the logic for this in
`NodeWithStyle::create_anonymous_wrapper()`, so reuse that method in our
implementation of the button layout.

Fixes applying certain text styles (such as `text-decoration`) to the
text of a `<button>`.
2025-08-19 11:12:23 +02:00
Aliaksandr Kalenik
a41d586117 LibWeb: Merge FillPathUsingPaintStyle and FillPathUsingColor
Use `Variant<PaintStyle, Gfx::Color>` in new `FillPath` instead of
duplicating two almost identical display list items.
2025-08-03 10:42:33 +02:00
Jelle Raaijmakers
f28b7064ee LibWeb: Add indentation to display list dumps
Output display list dumps with an indentation level to show balanced
commands. It makes it much easier to see what is happening between e.g.
PushStackingContext and PopStackingContext, or SaveLayer and Restore.
2025-08-01 14:20:51 +02:00
Aliaksandr Kalenik
a6857a6ce1 LibWeb: Log more useful information in display list dump 2025-07-27 10:20:18 +02:00
Aliaksandr Kalenik
eed47acb1f 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
2025-07-14 15:48:28 +02:00
Aliaksandr Kalenik
8ae7417445 LibWeb: Add internals call to dump display list
It's useful to have tests that dump display list items, so we can more
easily see how changes to the display list recording process affect the
output. Even the small sample test added in this commit shows that we
currently record an unnecessary AddClipRect item for empty paint phases.

For now, the dump doesn't include every single property of an item, but
we can shape it to include more useful information as we iterate on it.
2025-07-13 19:15:05 +02:00