layout: Add a first pass at incremental box tree construction (#37751)

This change:

- Adds a new type of LayoutDamage that signifies that a box needs its
  children recollected, because one or more of them need to be rebuilt.
- During restyle damage propagation, propagate this new damage upward in
  the tree. Then box tree construction should be able to preserve any
  still-valid box tree nodes from box slots.
- During BlockLevelBox job finalization, if a box slot is valid and
  there is not LayoutDamage to the element, use the old box slot,
  ensuring that its fragment cache is invalidated.

Testing: This should not change observable behavior and thus is covered
by existing WPT tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
Co-authored-by: coding-joedow <ibluegalaxy_taoj@163.com>
This commit is contained in:
Martin Robinson 2025-07-03 10:13:20 +02:00 committed by GitHub
parent 9aa06b2c17
commit 6dafeb7a59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 156 additions and 78 deletions

View file

@ -689,6 +689,22 @@ impl<'dom> BlockContainerBuilder<'dom, '_> {
impl BlockLevelJob<'_> {
fn finish(self, context: &LayoutContext) -> ArcRefCell<BlockLevelBox> {
let info = &self.info;
// If this `BlockLevelBox` is undamaged and it has been laid out before, reuse
// the old one, while being sure to clear the layout cache.
if !info.damage.has_box_damage() {
if let Some(block_level_box) = match self.box_slot.slot.as_ref() {
Some(box_slot) => match &*box_slot.borrow() {
Some(LayoutBox::BlockLevel(block_level_box)) => Some(block_level_box.clone()),
_ => None,
},
None => None,
} {
block_level_box.borrow().invalidate_cached_fragment();
return block_level_box;
}
}
let block_level_box = match self.kind {
BlockLevelCreator::SameFormattingContextBlock(intermediate_block_container) => {
let contents = intermediate_block_container.finish(context, info);

View file

@ -140,7 +140,11 @@ fn construct_for_root_element(
context: &LayoutContext,
root_element: ServoLayoutNode<'_>,
) -> Vec<ArcRefCell<BlockLevelBox>> {
let info = NodeAndStyleInfo::new(root_element, root_element.style(&context.style_context));
let info = NodeAndStyleInfo::new(
root_element,
root_element.style(&context.style_context),
root_element.take_restyle_damage(),
);
let box_style = info.style.get_box();
let display_inside = match Display::from(box_style.display) {
@ -378,7 +382,13 @@ impl<'dom> IncrementalBoxTreeUpdate<'dom> {
fn update_from_dirty_root(&self, context: &LayoutContext) {
let contents = ReplacedContents::for_element(self.node, context)
.map_or_else(|| NonReplacedContents::OfElement.into(), Contents::Replaced);
let info = NodeAndStyleInfo::new(self.node, self.primary_style.clone());
let info = NodeAndStyleInfo::new(
self.node,
self.primary_style.clone(),
self.node.take_restyle_damage(),
);
let out_of_flow_absolutely_positioned_box = ArcRefCell::new(
AbsolutelyPositionedBox::construct(context, &info, self.display_inside, contents),
);
@ -428,6 +438,14 @@ impl<'dom> IncrementalBoxTreeUpdate<'dom> {
let mut invalidate_start_point = self.node;
while let Some(parent_node) = invalidate_start_point.parent_node() {
parent_node.invalidate_cached_fragment();
// Box tree reconstruction doesn't need to involve these ancestors, so their
// damage isn't useful for us.
//
// TODO: This isn't going to be good enough for incremental fragment tree
// reconstruction, as fragment tree damage might extend further up the tree.
parent_node.take_restyle_damage();
invalidate_start_point = parent_node;
}
}