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

@ -12,7 +12,7 @@ use layout_api::wrapper_traits::{
LayoutDataTrait, LayoutNode, ThreadSafeLayoutElement, ThreadSafeLayoutNode,
};
use layout_api::{
GenericLayoutDataTrait, LayoutElementType, LayoutNodeType as ScriptLayoutNodeType,
GenericLayoutDataTrait, LayoutDamage, LayoutElementType, LayoutNodeType as ScriptLayoutNodeType,
};
use malloc_size_of_derive::MallocSizeOf;
use net_traits::image_cache::Image;
@ -20,7 +20,7 @@ use script::layout_dom::ServoLayoutNode;
use servo_arc::Arc as ServoArc;
use style::context::SharedStyleContext;
use style::properties::ComputedValues;
use style::selector_parser::PseudoElement;
use style::selector_parser::{PseudoElement, RestyleDamage};
use crate::cell::ArcRefCell;
use crate::flexbox::FlexLevelBox;
@ -160,7 +160,6 @@ pub struct BoxSlot<'dom> {
/// A mutable reference to a `LayoutBox` stored in a DOM element.
impl BoxSlot<'_> {
pub(crate) fn new(slot: ArcRefCell<Option<LayoutBox>>) -> Self {
*slot.borrow_mut() = None;
let slot = Some(slot);
Self {
slot,
@ -216,6 +215,7 @@ pub(crate) trait NodeExt<'dom> {
fn invalidate_cached_fragment(&self);
fn repair_style(&self, context: &SharedStyleContext);
fn take_restyle_damage(&self) -> LayoutDamage;
}
impl<'dom> NodeExt<'dom> for ServoLayoutNode<'dom> {
@ -404,4 +404,12 @@ impl<'dom> NodeExt<'dom> for ServoLayoutNode<'dom> {
}
}
}
fn take_restyle_damage(&self) -> LayoutDamage {
let damage = self
.style_data()
.map(|style_data| std::mem::take(&mut style_data.element_data.borrow_mut().damage))
.unwrap_or_else(RestyleDamage::reconstruct);
LayoutDamage::from_bits_retain(damage.bits())
}
}