mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 02:07:36 +00:00
LibWeb: Move clip rect calculation to happen before painting
With this change, clip rectangles for boxes with hidden overflow or the clip property are no longer calculated during the recording of painting commands. Instead, it has moved to the "pre-paint" phase, along with the assignment of scrolling offsets, and works in the following way: 1. The paintable tree is traversed to collect all paintable boxes that have hidden overflow or use the CSS clip property. For each of these boxes, the "final" clip rectangle is calculated by intersecting clip rectangles in the containing block chain for a box. 2. The paintable tree is traversed another time, and a clip rectangle is assigned for each paintable box contained by a node with hidden overflow or the clip property. This way, clipping becomes much easier during the painting commands recording phase, as it only concerns the use of already assigned clip rectangles. The same approach is applied to handle scrolling offsets. Also, clip rectangle calculation is now implemented more correctly, as we no longer stop at the stacking context boundary while intersecting clip rectangles in the containing block chain. Fixes: https://github.com/SerenityOS/serenity/issues/22932 https://github.com/SerenityOS/serenity/issues/22883 https://github.com/SerenityOS/serenity/issues/22679 https://github.com/SerenityOS/serenity/issues/22534
This commit is contained in:
parent
eaf9a56c10
commit
0bf82f748f
21 changed files with 365 additions and 166 deletions
|
@ -56,19 +56,97 @@ void ViewportPaintable::paint_all_phases(PaintContext& context)
|
|||
stacking_context()->paint(context);
|
||||
}
|
||||
|
||||
void ViewportPaintable::collect_scroll_frames(PaintContext& context) const
|
||||
void ViewportPaintable::assign_scroll_frame_ids(HashMap<Painting::PaintableBox const*, ScrollFrame>& scroll_frames) const
|
||||
{
|
||||
i32 next_id = 0;
|
||||
// Collect scroll frames with their offsets (accumulated offset for nested scroll frames).
|
||||
for_each_in_subtree_of_type<PaintableBox>([&](auto const& paintable_box) {
|
||||
if (paintable_box.has_scrollable_overflow()) {
|
||||
auto offset = paintable_box.scroll_offset();
|
||||
auto ancestor = paintable_box.parent();
|
||||
auto ancestor = paintable_box.containing_block();
|
||||
while (ancestor) {
|
||||
if (ancestor->is_paintable_box() && static_cast<PaintableBox const*>(ancestor)->has_scrollable_overflow())
|
||||
offset.translate_by(static_cast<PaintableBox const*>(ancestor)->scroll_offset());
|
||||
ancestor = ancestor->parent();
|
||||
if (ancestor->paintable()->is_paintable_box() && static_cast<PaintableBox const*>(ancestor->paintable())->has_scrollable_overflow())
|
||||
offset.translate_by(static_cast<PaintableBox const*>(ancestor->paintable())->scroll_offset());
|
||||
ancestor = ancestor->containing_block();
|
||||
}
|
||||
scroll_frames.set(&paintable_box, { .id = next_id++, .offset = -offset });
|
||||
}
|
||||
return TraversalDecision::Continue;
|
||||
});
|
||||
|
||||
// Assign scroll frame id to all paintables contained in a scroll frame.
|
||||
for_each_in_subtree([&](auto const& paintable) {
|
||||
for (auto block = paintable.containing_block(); block; block = block->containing_block()) {
|
||||
auto const& block_paintable_box = *block->paintable_box();
|
||||
if (auto scroll_frame_id = scroll_frames.get(&block_paintable_box); scroll_frame_id.has_value()) {
|
||||
if (paintable.is_paintable_box()) {
|
||||
auto const& paintable_box = static_cast<PaintableBox const&>(paintable);
|
||||
const_cast<PaintableBox&>(paintable_box).set_scroll_frame_id(scroll_frame_id->id);
|
||||
} else if (paintable.is_inline_paintable()) {
|
||||
auto const& inline_paintable = static_cast<InlinePaintable const&>(paintable);
|
||||
const_cast<InlinePaintable&>(inline_paintable).set_scroll_frame_id(scroll_frame_id->id);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return TraversalDecision::Continue;
|
||||
});
|
||||
}
|
||||
|
||||
void ViewportPaintable::assign_clip_rectangles(PaintContext const& context)
|
||||
{
|
||||
HashMap<Paintable const*, CSSPixelRect> clip_rects;
|
||||
// Calculate clip rects for all boxes that either have hidden overflow or a CSS clip property.
|
||||
for_each_in_subtree_of_type<PaintableBox>([&](auto const& paintable_box) {
|
||||
auto overflow_x = paintable_box.computed_values().overflow_x();
|
||||
auto overflow_y = paintable_box.computed_values().overflow_y();
|
||||
// Start from CSS clip property if it exists.
|
||||
Optional<CSSPixelRect> clip_rect = paintable_box.get_clip_rect();
|
||||
// FIXME: Support overflow clip in one direction only.
|
||||
if (overflow_x != CSS::Overflow::Visible && overflow_y != CSS::Overflow::Visible) {
|
||||
auto overflow_clip_rect = paintable_box.compute_absolute_padding_rect_with_css_transform_applied();
|
||||
for (auto block = &paintable_box.layout_box(); !block->is_viewport(); block = block->containing_block()) {
|
||||
auto const& block_paintable_box = *block->paintable_box();
|
||||
auto block_overflow_x = block_paintable_box.computed_values().overflow_x();
|
||||
auto block_overflow_y = block_paintable_box.computed_values().overflow_y();
|
||||
if (block_overflow_x != CSS::Overflow::Visible && block_overflow_y != CSS::Overflow::Visible)
|
||||
overflow_clip_rect.intersect(block_paintable_box.compute_absolute_padding_rect_with_css_transform_applied());
|
||||
if (auto css_clip_property_rect = block->paintable_box()->get_clip_rect(); css_clip_property_rect.has_value())
|
||||
overflow_clip_rect.intersect(css_clip_property_rect.value());
|
||||
}
|
||||
clip_rect = overflow_clip_rect;
|
||||
}
|
||||
if (clip_rect.has_value())
|
||||
clip_rects.set(&paintable_box, *clip_rect);
|
||||
return TraversalDecision::Continue;
|
||||
});
|
||||
|
||||
// Assign clip rects to all paintable boxes contained by a box with a hidden overflow or a CSS clip property.
|
||||
for_each_in_subtree_of_type<PaintableBox>([&](auto const& paintable_box) {
|
||||
Optional<CSSPixelRect> clip_rect = paintable_box.get_clip_rect();
|
||||
for (auto block = paintable_box.containing_block(); block; block = block->containing_block()) {
|
||||
if (auto containing_block_clip_rect = clip_rects.get(block->paintable()); containing_block_clip_rect.has_value()) {
|
||||
auto border_radii_data = block->paintable_box()->normalized_border_radii_data(ShrinkRadiiForBorders::Yes);
|
||||
CornerRadii corner_radii = border_radii_data.as_corners(context);
|
||||
if (corner_radii.has_any_radius()) {
|
||||
// FIXME: Border radii of all boxes in containing block chain should be taken into account instead of just the closest one.
|
||||
const_cast<PaintableBox&>(paintable_box).set_corner_clip_radii(corner_radii);
|
||||
}
|
||||
clip_rect = *containing_block_clip_rect;
|
||||
break;
|
||||
}
|
||||
}
|
||||
const_cast<PaintableBox&>(paintable_box).set_clip_rect(clip_rect);
|
||||
return TraversalDecision::Continue;
|
||||
});
|
||||
|
||||
// Assign clip rects to all inline paintables contained by a box with hidden overflow or a CSS clip property.
|
||||
for_each_in_subtree_of_type<InlinePaintable>([&](auto const& paintable_box) {
|
||||
for (auto block = paintable_box.containing_block(); block; block = block->containing_block()) {
|
||||
if (auto clip_rect = clip_rects.get(block->paintable()); clip_rect.has_value()) {
|
||||
const_cast<InlinePaintable&>(paintable_box).set_clip_rect(clip_rect);
|
||||
break;
|
||||
}
|
||||
context.scroll_frames().set(&paintable_box, { .id = next_id++, .offset = -offset });
|
||||
}
|
||||
return TraversalDecision::Continue;
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue