Skip to content

Truly independent web browser

LibWeb: Commit a layout pass by walking its fragments

Aliaksandr Kalenik8e9cf4274c90
diff --git a/Libraries/LibWeb/Rust/src/layout/commit.rs b/Libraries/LibWeb/Rust/src/layout/commit.rs
index 779f98def51..f076d6b60ab 100644
--- a/Libraries/LibWeb/Rust/src/layout/commit.rs
+++ b/Libraries/LibWeb/Rust/src/layout/commit.rs
@@ -111,190 +111,576 @@ impl CommitNotifications {
}
}
-/// Whether a box whose new fragment holds the lines and child fragments it committed last keeps its committed subtree,
-/// which then has nothing a recommit would write, so that only the box itself is committed. A stored run hit at another
-/// unobserved block size gives the box such a fragment around the committed contents. The box's committed geometry must
-/// still be current: every layout invalidation inside the box moves that epoch on, and a layout-affecting change since
-/// that commit can keep the fragment while changing the facts a recommit gathers, such as paint order inputs. The
-/// fragment shows every box inside it, except an absolutely positioned one whose containing block is outside, and
-/// boxes whose links a fragment around it holds (see CompletedPassFragments::links_by_slot). A box broken into several
-/// fragments is recommitted.
-fn keeps_committed_subtree(arena: &LayoutNodeArena, link: &FragmentLink) -> bool {
- let node = link.fragment.node;
- let data = arena.data(node);
- node_facts::kind_is_box(data.kind.get())
- && !node_facts::has_flag(data, NodeFlag::AbsposDescendantEscapes)
- && !link.fragment.continues_from_previous_fragment
- && !link.fragment.continues_in_next_fragment
- && arena.paintable_row_is_populated(node)
- && arena
- .with_current_committed_fragment(node, |committed| {
- std::sync::Arc::ptr_eq(committed, &link.fragment) || link.fragment.holds_same_contents_as(committed)
- })
- .unwrap_or(false)
+/// The fragment holding the links a commit meets: a box's fragment, or the top of the pass.
+#[derive(Clone, Copy)]
+struct Holder {
+ /// Invalid at the top of the pass.
+ node: Node,
+ /// Which of its box's fragments the fragment is, or None at the top of the pass.
+ fragment_index: Option<u32>,
+ /// How many lines the box's earlier fragments hold, by which the fragment's line indices count on.
+ line_index_offset: usize,
+ line_root_changes: crate::painting::paintable_build::LineRootChanges,
}
-fn commit_subtree(
- node: Node,
- messages: &mut Vec<FfiCommitMessage>,
- paintables: &mut crate::painting::paintable_build::PaintableCommit<'_>,
- links_by_slot: &fragment_tree::LinksBySlot<'_>,
+impl Holder {
+ fn top_of_pass() -> Self {
+ Self {
+ node: Node::INVALID,
+ fragment_index: None,
+ line_index_offset: 0,
+ line_root_changes: Default::default(),
+ }
+ }
+
+ /// Which fragment of its containing block shows `link`, and how its line indices count on there: a link the
+ /// fragment of its containing block holds is shown by that fragment, any other by the first one.
+ fn placement_of(&self, link: &FragmentLink) -> (Option<u32>, usize) {
+ match self.fragment_index {
+ None => (None, 0),
+ Some(index) if link.containing_block == self.node => (Some(index), self.line_index_offset),
+ Some(_) => (Some(0), 0),
+ }
+ }
+}
+
+/// The fragments of a box broken into several, which commit commits once it met them all.
+struct BrokenBox<'tree> {
+ fragments: fragment_tree::SlotFragments<'tree>,
+ line_count: usize,
enclosing_line_root_changes: crate::painting::paintable_build::LineRootChanges,
-) {
- let slot_index = node.slot_index();
- let fragments = links_by_slot.fragments(slot_index);
- let fragments = fragments.as_slice();
- let link_with_merged_line_index;
- let entry = match fragments.first() {
- Some(fragment)
- if fragment.containing_block_line_index_offset != 0
- && fragment.link.containing_line_box_index.is_some() =>
- {
- link_with_merged_line_index = fragment_tree::FragmentLink {
- containing_line_box_index: fragment
- .link
- .containing_line_box_index
- .map(|index| index + fragment.containing_block_line_index_offset),
- ..fragment.link.clone()
- };
- Some(&link_with_merged_line_index)
+}
+
+/// A box a commit committed anew, with the fragments it committed before, whose links the commit clears unless it met
+/// them again.
+struct RecommittedBox {
+ node: Node,
+ previous_fragments: smallvec::SmallVec<[std::sync::Arc<fragment_tree::Fragment>; 1]>,
+ previous_inline_content: Option<std::sync::Arc<inline_content::InlineContent>>,
+}
+
+/// A commit of the fragments of a layout pass, which walks them from the top of the pass and commits each link that
+/// changed. The fragment of a box whose committed link it meets again unchanged, in geometry still current, holds the
+/// links its subtree committed, so the walk leaves that subtree as it is.
+struct Commit<'arena, 'tree> {
+ paintables: crate::painting::paintable_build::PaintableCommit<'arena>,
+ messages: Vec<FfiCommitMessage>,
+ /// The number of this commit, which tells the links it met from those it did not.
+ number: u32,
+ root: Node,
+ is_full_commit: bool,
+ committed_anew: Vec<Node>,
+ recommitted: Vec<RecommittedBox>,
+ broken_boxes: crate::fast_hash::FastMap<u32, BrokenBox<'tree>>,
+ broken_box_order: Vec<Node>,
+ out_of_flow_boxes: Vec<(Node, std::sync::Arc<crate::layout::abspos_inputs::AbsposLayoutInputs>)>,
+ pass_level_links: Vec<Node>,
+ line_roots: Vec<(Node, crate::painting::paintable_build::LineRootChanges)>,
+}
+
+impl<'tree> Commit<'_, 'tree> {
+ fn arena(&self) -> &LayoutNodeArena {
+ self.paintables.arena()
+ }
+
+ fn arena_mut(&mut self) -> &mut LayoutNodeArena {
+ self.paintables.arena_mut()
+ }
+
+ fn was_met(&self, node: Node) -> bool {
+ self.arena()
+ .committed_link_holder(node)
+ .is_some_and(|(_, number)| number == self.number)
+ }
+
+ fn meet(&mut self, link: &'tree FragmentLink, holder: &Holder) {
+ let node = link.fragment.node;
+ // NB: A partial relayout's root keeps the fragment of its parent as its holder, which the commit does not walk.
+ let keeps_its_holder = holder.node.is_invalid() && !self.is_full_commit && node == self.root;
+ let recorded_holder = if keeps_its_holder {
+ self.arena()
+ .committed_link_holder(node)
+ .map_or(Node::INVALID, |(holder, _)| holder)
+ } else {
+ holder.node
+ };
+ let was_at_pass_level = self
+ .arena()
+ .committed_link_holder(node)
+ .is_some_and(|(previous_holder, _)| previous_holder.is_invalid());
+ let number = self.number;
+ self.arena_mut()
+ .note_committed_link_holder(node, recorded_holder, number);
+ if holder.node.is_invalid() && !keeps_its_holder && (self.is_full_commit || !was_at_pass_level) {
+ self.pass_level_links.push(node);
+ }
+ if let Some(inputs) = &link.abspos_layout_inputs {
+ self.out_of_flow_boxes.push((node, inputs.clone()));
+ }
+ let (containing_block_fragment, line_index_offset) = holder.placement_of(link);
+ if link.fragment.continues_from_previous_fragment || link.fragment.continues_in_next_fragment {
+ self.meet_broken_box(link, containing_block_fragment, line_index_offset, holder);
+ return;
+ }
+ if self.is_unchanged(node, link, containing_block_fragment, holder) {
+ return;
}
- first => first.map(|fragment| fragment.link),
- };
- let has_more_fragments = fragments.len() > 1;
- let reuses_committed_subtree = links_by_slot.keeps_committed_subtree(slot_index);
- debug_assert!(!reuses_committed_subtree || (entry.is_some() && !has_more_fragments));
- // A box that keeps its committed subtree and committed this very link already, in geometry that is still current,
- // has nothing left to commit. A box placing an absolutely positioned box, or a navigable's viewport, notes itself on every commit, and
- // a link without a containing block leaves the commit to find it among ancestors that may have changed.
- if reuses_committed_subtree
- && let Some(link) = entry
- && link.abspos_layout_inputs.is_none()
- && !link.containing_block.is_invalid()
- && paintables.arena().data(node).kind.get() != NodeKind::NavigableContainerViewport
- && paintables.arena().holds_current_committed_fragment_link(node, link)
- && fragments
- .first()
- .and_then(|fragment| fragment.containing_block_fragment)
- .is_none_or(|fragment| paintables.arena().committed_side_data(node).containing_block_fragment == fragment)
- {
- return;
+ let fragments: fragment_tree::SlotFragments<'tree> = smallvec::smallvec![fragment_tree::SlotFragment {
+ link,
+ containing_block_fragment,
+ containing_block_line_index_offset: line_index_offset,
+ }];
+ let Some(children_holder) = self.commit_box(node, fragments, holder.line_root_changes) else {
+ return;
+ };
+ for child in &link.fragment.children {
+ self.meet(child, &children_holder);
+ }
+ self.finish_box(node, children_holder.line_root_changes);
+ }
+
+ /// Whether `link` is the committed link of its box already, placed the same way, in geometry still current, so
+ /// that its subtree committed the links its fragment holds.
+ fn is_unchanged(
+ &self,
+ node: Node,
+ link: &FragmentLink,
+ containing_block_fragment: Option<u32>,
+ holder: &Holder,
+ ) -> bool {
+ let arena = self.arena();
+ let data = arena.data(node);
+ let kind = data.kind.get();
+ // NB: A link without a containing block leaves the commit to find it among ancestors that may have changed,
+ // a navigable's viewport notes itself on every commit, and the paint of an inline box shows the lines of
+ // its line root.
+ node_facts::kind_is_box(kind)
+ && kind != NodeKind::NavigableContainerViewport
+ && !link.containing_block.is_invalid()
+ && !(holder.line_root_changes.any()
+ && node_facts::node_is_fragmented_inline(data, node_facts::node_style_view(data)))
+ && arena.paintable_row_is_populated(node)
+ && arena.holds_current_committed_fragment_link(node, link)
+ && arena.committed_side_data(node).box_fragments().is_empty()
+ && containing_block_fragment
+ .is_none_or(|fragment| arena.committed_side_data(node).containing_block_fragment == fragment)
}
- if let Some(shift) = entry.and_then(|link| link.default_scroll_shift) {
- paintables.arena_mut().set_default_scroll_shift(
+
+ fn meet_broken_box(
+ &mut self,
+ link: &'tree FragmentLink,
+ containing_block_fragment: Option<u32>,
+ line_index_offset: usize,
+ holder: &Holder,
+ ) {
+ let node = link.fragment.node;
+ let line_count = link
+ .fragment
+ .line_data
+ .as_ref()
+ .map_or(0, |line_data| line_data.lines.len());
+ let fragment = fragment_tree::SlotFragment {
+ link,
+ containing_block_fragment,
+ containing_block_line_index_offset: line_index_offset,
+ };
+ let (fragment_index, lines_before) = match self.broken_boxes.get_mut(&node.slot_index()) {
+ Some(broken) => {
+ let lines_before = broken.line_count;
+ broken.line_count += line_count;
+ broken.fragments.push(fragment);
+ (broken.fragments.len() as u32 - 1, lines_before)
+ }
+ None => {
+ self.broken_box_order.push(node);
+ self.broken_boxes.insert(
+ node.slot_index(),
+ BrokenBox {
+ fragments: smallvec::smallvec![fragment],
+ line_count,
+ enclosing_line_root_changes: holder.line_root_changes,
+ },
+ );
+ (0, 0)
+ }
+ };
+ // NB: The box commits once all its fragments are met. Its children see its lines as changed meanwhile.
+ let children_holder = Holder {
node,
- shift.anchor,
- shift.compensates_for_horizontal_scroll,
- shift.compensates_for_vertical_scroll,
- );
+ fragment_index: Some(fragment_index),
+ line_index_offset: lines_before,
+ line_root_changes: crate::painting::paintable_build::LineRootChanges::all(),
+ };
+ for child in &link.fragment.children {
+ self.meet(child, &children_holder);
+ }
}
- if !reuses_committed_subtree {
- paintables.arena_mut().forget_committed_out_of_flow_facts(node);
+
+ fn commit_broken_boxes(&mut self) {
+ for node in std::mem::take(&mut self.broken_box_order) {
+ let broken = self
+ .broken_boxes
+ .remove(&node.slot_index())
+ .expect("a met broken box is listed");
+ if let Some(holder) = self.commit_box(node, broken.fragments, broken.enclosing_line_root_changes) {
+ self.finish_box(node, holder.line_root_changes);
+ }
+ }
}
- if let Some(inputs) = entry.and_then(|link| link.abspos_layout_inputs.as_ref()) {
- paintables.arena_mut().note_committed_out_of_flow_box(node, inputs);
- paintables
- .arena_mut()
- .set_position_option_in_use(node, inputs.position_option);
+
+ /// Whether the box of `link` keeps its committed subtree: its new fragment holds the lines and child fragments it
+ /// committed last, placed the same way, in geometry still current, so that only the box itself commits. A stored run
+ /// hit at another unobserved block size gives the box such a fragment around the committed contents.
+ fn keeps_committed_subtree(&self, node: Node, link: &FragmentLink) -> bool {
+ let arena = self.arena();
+ node_facts::kind_is_box(arena.data(node).kind.get())
+ && arena.paintable_row_is_populated(node)
+ && arena.committed_side_data(node).box_fragments().is_empty()
+ && arena
+ .with_current_committed_fragment(node, |committed| {
+ std::sync::Arc::ptr_eq(committed, &link.fragment) || link.fragment.holds_same_contents_as(committed)
+ })
+ .unwrap_or(false)
}
- let previous_fragmented_content = (paintables.arena().paintable_row_is_populated(node)
- && (has_more_fragments || !paintables.arena().committed_side_data(node).box_fragments().is_empty()))
- .then(|| paintables.arena().committed_side_data(node).inline_content.clone())
- .flatten();
- let prepared = paintables.prepare_node(
- node,
- entry.is_some(),
- reuses_committed_subtree,
- enclosing_line_root_changes,
- );
- let mut has_recommitted_inline_box_pieces = false;
- let mut line_root_changes_for_children = enclosing_line_root_changes;
- if let Some(link) = entry
- && prepared.has_paintable_row
- {
+ /// Commits the box `node` anew with its `fragments`, answering how its fragment holds the links of its children, or
+ /// None where the box keeps its committed subtree.
+ fn commit_box(
+ &mut self,
+ node: Node,
+ fragments: fragment_tree::SlotFragments<'tree>,
+ enclosing_line_root_changes: crate::painting::paintable_build::LineRootChanges,
+ ) -> Option<Holder> {
+ let first = fragments.first().expect("a committed box has a fragment");
+ let link_with_merged_line_index;
+ let link = if first.containing_block_line_index_offset != 0 && first.link.containing_line_box_index.is_some() {
+ link_with_merged_line_index = FragmentLink {
+ containing_line_box_index: first
+ .link
+ .containing_line_box_index
+ .map(|index| index + first.containing_block_line_index_offset),
+ ..first.link.clone()
+ };
+ &link_with_merged_line_index
+ } else {
+ first.link
+ };
+ let has_more_fragments = fragments.len() > 1;
+ let keeps_committed_subtree = !has_more_fragments && self.keeps_committed_subtree(node, link);
+ let previous = (!keeps_committed_subtree)
+ .then(|| {
+ let arena = self.arena();
+ let previous_link = arena.with_committed_fragment_link(node, |link| link.cloned());
+ previous_link.map(|previous_link| {
+ let row_is_populated = arena.paintable_row_is_populated(node);
+ let mut previous_fragments = smallvec::smallvec![previous_link.fragment];
+ let mut previous_inline_content = None;
+ if row_is_populated {
+ let side = arena.committed_side_data(node);
+ previous_fragments.extend(
+ side.box_fragments()
+ .iter()
+ .map(|fragment| fragment.link.fragment.clone()),
+ );
+ previous_inline_content = side.inline_content.clone();
+ }
+ RecommittedBox {
+ node,
+ previous_fragments,
+ previous_inline_content,
+ }
+ })
+ })
+ .flatten();
+ if let Some(shift) = link.default_scroll_shift {
+ self.arena_mut().set_default_scroll_shift(
+ node,
+ shift.anchor,
+ shift.compensates_for_horizontal_scroll,
+ shift.compensates_for_vertical_scroll,
+ );
+ }
+ if !keeps_committed_subtree {
+ self.arena_mut().forget_committed_out_of_flow_facts(node);
+ }
+ let previous_fragmented_content = (has_more_fragments
+ || previous
+ .as_ref()
+ .is_some_and(|previous| previous.previous_fragments.len() > 1))
+ .then(|| {
+ previous
+ .as_ref()
+ .and_then(|previous| previous.previous_inline_content.clone())
+ })
+ .flatten();
+ let prepared = self
+ .paintables
+ .prepare_node(node, true, keeps_committed_subtree, enclosing_line_root_changes);
+ self.committed_anew.push(node);
+ if let Some(previous) = previous {
+ self.recommitted.push(previous);
+ }
+ if !prepared.has_paintable_row {
+ self.arena_mut().gather_layout_style_snapshot_geometry(node);
+ return Some(Holder {
+ node,
+ fragment_index: Some(0),
+ line_index_offset: 0,
+ line_root_changes: enclosing_line_root_changes,
+ });
+ }
let fragment = &link.fragment;
debug_assert!(
fragment.computed_svg_path.is_some()
- || !crate::painting::node_painting::is_svg_path(paintables.arena().data(node).kind.get()),
+ || !crate::painting::node_painting::is_svg_path(self.arena().data(node).kind.get()),
"committed path-like fragment carries no computed SVG path"
);
- let replaced = paintables.replace_committed_fragment_link(
+ let replaced = self.paintables.replace_committed_fragment_link(
node,
link,
enclosing_line_root_changes,
prepared.previous_offset,
);
- paintables.set_box_fragments(node, fragments);
+ self.paintables.set_box_fragments(node, &fragments);
+ let mut line_root_changes_for_children = enclosing_line_root_changes;
if fragment.line_data.is_some() {
line_root_changes_for_children = replaced.line_root_changes;
}
if prepared.row_existed_before_this_commit
&& let Some((old_content_size, new_content_size)) = replaced.content_size_change
- && crate::layout::node_facts::node_style_view(paintables.arena().data(node)).is_some_and(|style| {
+ && crate::layout::node_facts::node_style_view(self.arena().data(node)).is_some_and(|style| {
content_size_change_affects_container_queries(style, old_content_size, new_content_size)
})
- && let Some(style_node) = paintables.arena().commit_message_style_node(node)
+ && let Some(style_node) = self.arena().commit_message_style_node(node)
{
- messages.push(FfiCommitMessage::new(
+ self.messages.push(FfiCommitMessage::new(
style_node,
FfiCommitMessageKind::ContentSizeChangedForContainerQueries,
));
}
-
- if !reuses_committed_subtree {
- let merged_line_data = has_more_fragments
- .then(|| {
- inline_content::InlineContent::merge_box_fragments(
- fragments
- .iter()
- .map(|fragment| fragment.link.fragment.line_data.as_deref()),
- )
- })
- .flatten();
- if let Some(line_data) = merged_line_data.as_ref().or(fragment.line_data.as_ref()) {
- let change = paintables.set_line_data(node, line_data, previous_fragmented_content);
- has_recommitted_inline_box_pieces = change.has_pieces;
- line_root_changes_for_children.fragment_changed |= change.content_changed;
- line_root_changes_for_children.inline_content_changed |= change.content_changed;
- line_root_changes_for_children.inline_item_order_changed |= change.item_order_changed;
- } else {
- paintables.clear_line_data(node);
- }
+ if keeps_committed_subtree {
+ self.arena_mut().gather_layout_style_snapshot_geometry(node);
+ self.arena_mut().refresh_paint_order_inputs(node);
+ return None;
}
- }
- if entry.is_none() && prepared.has_paintable_row {
- paintables.set_box_fragments(node, fragments);
- paintables.clear_line_data(node);
- paintables.schedule_scrollable_overflow_recalculation(node);
+ let merged_line_data = has_more_fragments
+ .then(|| {
+ inline_content::InlineContent::merge_box_fragments(
+ fragments
+ .iter()
+ .map(|fragment| fragment.link.fragment.line_data.as_deref()),
+ )
+ })
+ .flatten();
+ if let Some(line_data) = merged_line_data.as_ref().or(fragment.line_data.as_ref()) {
+ let change = self
+ .paintables
+ .set_line_data(node, line_data, previous_fragmented_content);
+ line_root_changes_for_children.fragment_changed |= change.content_changed;
+ line_root_changes_for_children.inline_content_changed |= change.content_changed;
+ line_root_changes_for_children.inline_item_order_changed |= change.item_order_changed;
+ } else {
+ self.paintables.clear_line_data(node);
+ }
+ self.arena_mut().gather_layout_style_snapshot_geometry(node);
+ // NB: A link can change what orders the painting of a box that keeps its fragment, such as whether collapsed
+ // columns hide it.
+ self.arena_mut().refresh_paint_order_inputs(node);
+ Some(Holder {
+ node,
+ fragment_index: Some(0),
+ line_index_offset: 0,
+ line_root_changes: line_root_changes_for_children,
+ })
}
- paintables.arena_mut().gather_layout_style_snapshot_geometry(node);
+ /// Notes a box committed anew whose fragment may hold lines, whose inline boxes commit after the walk.
+ fn finish_box(&mut self, node: Node, line_root_changes: crate::painting::paintable_build::LineRootChanges) {
+ if self.arena().paintable_row_is_populated(node)
+ && self
+ .arena()
+ .committed_side_data(node)
+ .inline_content
+ .as_ref()
+ .is_some_and(|content| content.inline_boxes.boxes().next().is_some())
+ {
+ self.line_roots.push((node, line_root_changes));
+ }
+ }
- paintables.stamp_containing_block(node, entry);
- // NB: A link can change what orders the painting of a box that keeps its fragment, such as whether collapsed
- // columns hide it.
- paintables.arena_mut().refresh_paint_order_inputs(node);
- if reuses_committed_subtree {
- return;
+ /// Commits the rows of the inline boxes of the line roots this commit committed anew that have pieces but no link
+ /// of their own, as an inline box laid out where no fragments are built has.
+ fn commit_inline_boxes_without_links(&mut self) {
+ for index in 0..self.line_roots.len() {
+ let (root, line_root_changes) = self.line_roots[index];
+ if !self.arena().slot_is_live(root) || !self.arena().paintable_row_is_populated(root) {
+ continue;
+ }
+ let inline_boxes: Vec<Node> = self
+ .arena()
+ .committed_side_data(root)
+ .inline_content
+ .as_ref()
+ .map(|content| content.inline_boxes.boxes().map(|(inline_box, _)| inline_box).collect())
+ .unwrap_or_default();
+ for inline_box in inline_boxes {
+ if !self.arena().slot_is_live(inline_box) || self.was_met(inline_box) {
+ continue;
+ }
+ // NB: A link a fragment the walk left as it was holds stays, and one the walk did not meet again goes
+ // with the lost links, after which the box commits here.
+ if self
+ .arena()
+ .with_committed_fragment_link(inline_box, |link| link.is_some())
+ {
+ continue;
+ }
+ let number = self.number;
+ self.arena_mut().note_committed_link_holder(inline_box, root, number);
+ let prepared = self
+ .paintables
+ .prepare_node(inline_box, false, false, line_root_changes);
+ self.committed_anew.push(inline_box);
+ if prepared.has_paintable_row {
+ self.paintables.set_box_fragments(inline_box, &[]);
+ self.paintables.clear_line_data(inline_box);
+ self.paintables.schedule_scrollable_overflow_recalculation(inline_box);
+ }
+ self.arena_mut().gather_layout_style_snapshot_geometry(inline_box);
+ self.arena_mut().refresh_paint_order_inputs(inline_box);
+ }
+ }
}
- let mut child = paintables.arena().data(node).first_child.get();
- while !child.is_invalid() {
- let next = paintables.arena().data(child).next_sibling.get();
- commit_subtree(
- child,
- messages,
- &mut *paintables,
- links_by_slot,
- line_root_changes_for_children,
- );
- child = next;
+ /// Clears what a box committed when this commit met it nowhere though the fragment that held it last committed
+ /// anew, and so on for what the box's own fragments held.
+ fn clear_lost_links(&mut self) {
+ let mut lost: Vec<(Node, Node)> = Vec::new();
+ for recommitted in std::mem::take(&mut self.recommitted) {
+ for fragment in &recommitted.previous_fragments {
+ lost.extend(
+ fragment
+ .children
+ .iter()
+ .map(|link| (link.fragment.node, recommitted.node)),
+ );
+ }
+ if let Some(content) = &recommitted.previous_inline_content {
+ lost.extend(
+ content
+ .inline_boxes
+ .boxes()
+ .map(|(inline_box, _)| (inline_box, recommitted.node)),
+ );
+ }
+ }
+ if self.is_full_commit {
+ let previous_pass_level_links = self.arena_mut().take_pass_level_committed_links();
+ lost.extend(previous_pass_level_links.into_iter().map(|node| (node, Node::INVALID)));
+ let pass_level_links = std::mem::take(&mut self.pass_level_links);
+ self.arena_mut().set_pass_level_committed_links(pass_level_links);
+ } else {
+ let mut pass_level_links = self.arena_mut().take_pass_level_committed_links();
+ pass_level_links.append(&mut self.pass_level_links);
+ self.arena_mut().set_pass_level_committed_links(pass_level_links);
+ }
+ while let Some((node, holder)) = lost.pop() {
+ if !self.arena().slot_is_live(node)
+ || self
+ .arena()
+ .committed_link_holder(node)
+ .is_none_or(|(last_holder, number)| last_holder != holder || number == self.number)
+ {
+ continue;
+ }
+ let number = self.number;
+ self.arena_mut().note_committed_link_holder(node, Node::INVALID, number);
+ let previous_link = self.arena().with_committed_fragment_link(node, |link| link.cloned());
+ let row_is_populated = self.arena().paintable_row_is_populated(node);
+ if let Some(previous_link) = &previous_link {
+ lost.extend(
+ previous_link
+ .fragment
+ .children
+ .iter()
+ .map(|link| (link.fragment.node, node)),
+ );
+ }
+ if row_is_populated {
+ let side = self.arena().committed_side_data(node);
+ for fragment in side.box_fragments() {
+ lost.extend(
+ fragment
+ .link
+ .fragment
+ .children
+ .iter()
+ .map(|link| (link.fragment.node, node)),
+ );
+ }
+ if let Some(content) = &side.inline_content {
+ lost.extend(content.inline_boxes.boxes().map(|(inline_box, _)| (inline_box, node)));
+ }
+ }
+ if previous_link.is_none() && !row_is_populated {
+ continue;
+ }
+ self.arena_mut().forget_committed_out_of_flow_facts(node);
+ self.paintables.clear_node(node);
+ self.arena_mut().gather_layout_style_snapshot_geometry(node);
+ }
}
- if has_recommitted_inline_box_pieces {
- // NB: The rows of this block's inline boxes exist only now that its whole subtree has committed.
- paintables.note_inline_boxes_of_line_root_recommitted(node);
+ fn finish(mut self, pass_fragments: &'tree fragment_tree::CompletedPassFragments) -> CommitNotifications {
+ for link in &pass_fragments.roots {
+ self.meet(link, &Holder::top_of_pass());
+ }
+ self.commit_broken_boxes();
+ self.commit_inline_boxes_without_links();
+ self.clear_lost_links();
+ // NB: An inline box whose link the commit cleared as lost commits again where its line root's pieces show it.
+ self.commit_inline_boxes_without_links();
+ for (root, _) in std::mem::take(&mut self.line_roots) {
+ if self.arena().slot_is_live(root) && self.arena().paintable_row_is_populated(root) {
+ self.paintables.note_inline_boxes_of_line_root_recommitted(root);
+ }
+ }
+ for node in std::mem::take(&mut self.committed_anew) {
+ if !self.arena().slot_is_live(node) {
+ continue;
+ }
+ let link = self.arena().with_committed_fragment_link(node, |link| link.cloned());
+ self.paintables.stamp_containing_block(node, link.as_ref());
+ }
+ for (node, inputs) in std::mem::take(&mut self.out_of_flow_boxes) {
+ if !self.arena().slot_is_live(node)
+ || !self.arena().with_committed_fragment_link(node, |link| link.is_some())
+ {
+ continue;
+ }
+ self.arena_mut().note_committed_out_of_flow_box(node, &inputs);
+ self.arena_mut()
+ .set_position_option_in_use(node, inputs.position_option);
+ }
+ let Self {
+ mut paintables,
+ mut messages,
+ ..
+ } = self;
+ paintables.discard_absolute_rects_memoized_during_commit();
+ for &viewport in paintables.committed_navigable_container_viewports() {
+ if let Some(style_node) = paintables.arena().commit_message_style_node(viewport) {
+ messages.push(FfiCommitMessage::new(
+ style_node,
+ FfiCommitMessageKind::NavigableContainerViewportCommitted,
+ ));
+ }
+ }
+ paintables.arena_mut().publish_layout_style_snapshot_commit();
+ CommitNotifications {
+ row_resets: paintables.take_row_reset_notifications(),
+ messages,
+ }
}
}
@@ -323,27 +709,23 @@ pub(crate) fn commit_replacing(
pass_fragments: &fragment_tree::CompletedPassFragments,
) -> CommitNotifications {
arena.note_layout_commit();
- let links_by_slot = pass_fragments.links_by_slot(
- root,
- |node| arena.data(node).parent.get(),
- |link| keeps_committed_subtree(arena, link),
- );
+ let number = arena.layout_commit_count() as u32;
+ let is_full_commit = arena.data(root).kind.get() == NodeKind::Viewport;
let mut paintables = crate::painting::paintable_build::PaintableCommit::new(arena, root);
paintables.begin_commit();
- let mut messages = Vec::new();
- commit_subtree(root, &mut messages, &mut paintables, &links_by_slot, Default::default());
- paintables.discard_absolute_rects_memoized_during_commit();
- for &viewport in paintables.committed_navigable_container_viewports() {
- if let Some(style_node) = paintables.arena().commit_message_style_node(viewport) {
- messages.push(FfiCommitMessage::new(
- style_node,
- FfiCommitMessageKind::NavigableContainerViewportCommitted,
- ));
- }
- }
- paintables.arena_mut().publish_layout_style_snapshot_commit();
- CommitNotifications {
- row_resets: paintables.take_row_reset_notifications(),
- messages,
+ Commit {
+ paintables,
+ messages: Vec::new(),
+ number,
+ root,
+ is_full_commit,
+ committed_anew: Vec::new(),
+ recommitted: Vec::new(),
+ broken_boxes: Default::default(),
+ broken_box_order: Vec::new(),
+ out_of_flow_boxes: Vec::new(),
+ pass_level_links: Vec::new(),
+ line_roots: Vec::new(),
}
+ .finish(pass_fragments)
}
diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
index 0cefa147ded..42dd9cd124d 100644
--- a/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fragment_tree.rs
@@ -830,175 +830,10 @@ pub(crate) struct SlotFragment<'tree> {
pub(crate) type SlotFragments<'tree> = SmallVec<[SlotFragment<'tree>; 1]>;
-pub(crate) struct LinksBySlot<'tree> {
- in_first_containing_block_fragment: HashMap<u32, &'tree FragmentLink>,
- other: HashMap<u32, (SlotFragments<'tree>, usize)>,
- kept_subtree_roots: HashSet<u32>,
-}
-
-impl<'tree> LinksBySlot<'tree> {
- /// Whether the box in `slot` keeps its committed subtree, which commit then leaves as it is below the box.
- pub(crate) fn keeps_committed_subtree(&self, slot: u32) -> bool {
- self.kept_subtree_roots.contains(&slot)
- }
-
- pub(crate) fn fragments(&self, slot: u32) -> SlotFragments<'tree> {
- if let Some(link) = self.in_first_containing_block_fragment.get(&slot) {
- return smallvec::smallvec![SlotFragment {
- link,
- containing_block_fragment: Some(0),
- containing_block_line_index_offset: 0,
- }];
- }
- self.other
- .get(&slot)
- .map(|(fragments, _)| fragments.clone())
- .unwrap_or_default()
- }
-
- fn push(&mut self, slot: u32, fragment: SlotFragment<'tree>) -> (u32, usize) {
- let line_count = |fragment: &SlotFragment<'_>| {
- fragment
- .link
- .fragment
- .line_data
- .as_ref()
- .map_or(0, |line_data| line_data.lines.len())
- };
- if let Some(link) = self.in_first_containing_block_fragment.remove(&slot) {
- let first = SlotFragment {
- link,
- containing_block_fragment: Some(0),
- containing_block_line_index_offset: 0,
- };
- let first_line_count = line_count(&first);
- self.other.insert(slot, (smallvec::smallvec![first], first_line_count));
- }
- let Some((fragments, line_count_before)) = self.other.get_mut(&slot) else {
- if fragment.containing_block_fragment == Some(0) && fragment.containing_block_line_index_offset == 0 {
- self.in_first_containing_block_fragment.insert(slot, fragment.link);
- } else {
- let fragment_line_count = line_count(&fragment);
- self.other
- .insert(slot, (smallvec::smallvec![fragment], fragment_line_count));
- }
- return (0, 0);
- };
- let fragment_index = fragments.len() as u32;
- let line_index_offset = *line_count_before;
- *line_count_before += line_count(&fragment);
- fragments.push(fragment);
- (fragment_index, line_index_offset)
- }
-}
-
pub(crate) struct CompletedPassFragments {
pub(crate) roots: Vec<FragmentLink>,
}
-impl CompletedPassFragments {
- /// The links of the pass by the slot of their box, below `pass_root`. A box keeps its committed subtree where
- /// `keeps_committed_subtree` holds for its link and the fragment of no box around it holds the link of a box inside
- /// it, which the box's own fragment would then not show. Commit looks up nothing below such a box.
- pub(crate) fn links_by_slot(
- &self,
- pass_root: crate::layout::node_data::NodeSlotId,
- parent_of: impl Fn(crate::layout::node_data::NodeSlotId) -> crate::layout::node_data::NodeSlotId,
- keeps_committed_subtree: impl Fn(&FragmentLink) -> bool,
- ) -> LinksBySlot<'_> {
- struct KeptSubtrees<ParentOf, KeepsCommittedSubtree> {
- parent_of: ParentOf,
- keeps_committed_subtree: KeepsCommittedSubtree,
- boxes_around_links_held_outside_them: HashSet<u32>,
- kept_subtree_roots: HashSet<u32>,
- }
- impl<ParentOf, KeepsCommittedSubtree> KeptSubtrees<ParentOf, KeepsCommittedSubtree>
- where
- ParentOf: Fn(crate::layout::node_data::NodeSlotId) -> crate::layout::node_data::NodeSlotId,
- KeepsCommittedSubtree: Fn(&FragmentLink) -> bool,
- {
- /// Decides which of `links`, held by the fragment of `holder`, keep their committed subtrees, and answers
- /// how many links commit looks up among them.
- fn decide(&mut self, links: &[FragmentLink], holder: crate::layout::node_data::NodeSlotId) -> usize {
- for link in links {
- if link.fragment.node == holder {
- continue;
- }
- let mut ancestor = (self.parent_of)(link.fragment.node);
- while !ancestor.is_invalid() && ancestor != holder {
- self.boxes_around_links_held_outside_them.insert(ancestor.slot_index());
- ancestor = (self.parent_of)(ancestor);
- }
- }
- links
- .iter()
- .map(|link| {
- let node = link.fragment.node;
- if !self.boxes_around_links_held_outside_them.contains(&node.slot_index())
- && (self.keeps_committed_subtree)(link)
- {
- self.kept_subtree_roots.insert(node.slot_index());
- return 1;
- }
- 1 + self.decide(&link.fragment.children, node)
- })
- .sum()
- }
- }
- fn insert_links<'tree>(
- links: &'tree [FragmentLink],
- parent: Option<(crate::layout::node_data::NodeSlotId, u32, usize)>,
- kept_subtree_roots: &HashSet<u32>,
- links_by_slot: &mut LinksBySlot<'tree>,
- ) {
- for link in links {
- let slot = link.fragment.node.slot_index();
- let (containing_block_fragment, containing_block_line_index_offset) = match parent {
- None => (None, 0),
- Some((parent_node, parent_fragment, parent_line_index_offset))
- if parent_node == link.containing_block =>
- {
- (Some(parent_fragment), parent_line_index_offset)
- }
- Some(_) => (Some(0), 0),
- };
- let (fragment_index, line_index_offset) = links_by_slot.push(
- slot,
- SlotFragment {
- link,
- containing_block_fragment,
- containing_block_line_index_offset,
- },
- );
- if !kept_subtree_roots.contains(&slot) {
- insert_links(
- &link.fragment.children,
- Some((link.fragment.node, fragment_index, line_index_offset)),
- kept_subtree_roots,
- links_by_slot,
- );
- }
- }
- }
- let mut kept_subtrees = KeptSubtrees {
- parent_of,
- keeps_committed_subtree,
- boxes_around_links_held_outside_them: HashSet::default(),
- kept_subtree_roots: HashSet::default(),
- };
- let link_count = kept_subtrees.decide(&self.roots, pass_root);
- let kept_subtree_roots = kept_subtrees.kept_subtree_roots;
- let mut links_by_slot = LinksBySlot {
- in_first_containing_block_fragment: HashMap::with_capacity_and_hasher(link_count, Default::default()),
- other: HashMap::default(),
- kept_subtree_roots: HashSet::default(),
- };
- insert_links(&self.roots, None, &kept_subtree_roots, &mut links_by_slot);
- links_by_slot.kept_subtree_roots = kept_subtree_roots;
- links_by_slot
- }
-}
-
struct PendingFragment {
node: crate::layout::node_data::NodeSlotId,
/// The fragment the box's layout handed its placement: the root fragment of its own run, or its committed fragment.
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index ed8614528d6..27caed8ad54 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -1040,6 +1040,13 @@ impl HostStyle {
}
}
+#[derive(Clone, Copy, Default)]
+struct CommittedLinkHolder {
+ generation: u8,
+ holder: Option<NodeSlotId>,
+ met_at_commit: u32,
+}
+
#[derive(Clone)]
pub(crate) struct LayoutNodeArena {
chunks: Vec<Box<Chunk>>,
@@ -1102,6 +1109,10 @@ pub(crate) struct LayoutNodeArena {
full_layout_count: u64,
/// How many layouts were committed, full and partial.
layout_commit_count: u64,
+ /// Per slot, which fragment held the box's committed link when a commit last met it.
+ committed_link_holders: Vec<CommittedLinkHolder>,
+ /// The boxes whose committed links a full commit met at the top of its pass, which no fragment holds.
+ pass_level_committed_links: Vec<NodeSlotId>,
layout_tree_build_stats: FfiLayoutTreeBuildStats,
pre_order_labels: Vec<u64>,
pre_order_relabel_count: u64,
@@ -1226,6 +1237,8 @@ impl LayoutNodeArena {
partial_layout_count: (0),
full_layout_count: (0),
layout_commit_count: 0,
+ committed_link_holders: Vec::new(),
+ pass_level_committed_links: Vec::new(),
layout_tree_build_stats: (FfiLayoutTreeBuildStats::default()),
pre_order_labels: Vec::new(),
pre_order_relabel_count: 0,
@@ -1280,6 +1293,37 @@ impl LayoutNodeArena {
}
}
+ /// Notes that the commit numbered `commit` met the committed link of `id` in the fragment of `holder`, or at the top
+ /// of its pass where `holder` is invalid.
+ pub(crate) fn note_committed_link_holder(&mut self, id: NodeSlotId, holder: NodeSlotId, commit: u32) {
+ let index = id.slot_index() as usize;
+ if self.committed_link_holders.len() <= index {
+ self.committed_link_holders
+ .resize(index + 1, CommittedLinkHolder::default());
+ }
+ self.committed_link_holders[index] = CommittedLinkHolder {
+ generation: id.generation(),
+ holder: Some(holder),
+ met_at_commit: commit,
+ };
+ }
+
+ /// The fragment that held the committed link of `id` when a commit last met it, and that commit's number.
+ pub(crate) fn committed_link_holder(&self, id: NodeSlotId) -> Option<(NodeSlotId, u32)> {
+ self.committed_link_holders
+ .get(id.slot_index() as usize)
+ .filter(|holder| holder.generation == id.generation())
+ .and_then(|holder| holder.holder.map(|node| (node, holder.met_at_commit)))
+ }
+
+ pub(crate) fn take_pass_level_committed_links(&mut self) -> Vec<NodeSlotId> {
+ std::mem::take(&mut self.pass_level_committed_links)
+ }
+
+ pub(crate) fn set_pass_level_committed_links(&mut self, links: Vec<NodeSlotId>) {
+ self.pass_level_committed_links = links;
+ }
+
pub(crate) fn layout_commit_count(&self) -> u64 {
self.layout_commit_count
}
@@ -4518,9 +4562,11 @@ impl LayoutNodeArena {
}
self.write_shape(id).set_flags(updated);
// A box that becomes a flex or grid item can lay out to the fragment it committed before, but orders its
- // painting differently, so its committed geometry no longer lets its commit be skipped.
+ // painting differently, so its committed geometry no longer lets its commit be skipped, nor that of the boxes
+ // whose committed fragments hold it.
if matches!(flag, NodeFlag::IsFlexItem | NodeFlag::IsGridItem) && updated != previous {
self.paintable_rows.invalidate_committed_geometry(id.slot_index());
+ self.invalidate_committed_geometry_of_ancestors(id);
}
// Retaining compositor-animated content decides whether a non-invertible transform
// still records its stacking context.
@@ -5233,9 +5279,7 @@ impl LayoutNodeArena {
let mut ancestor = self.data(id).parent.get();
while !ancestor.is_invalid()
&& self.slot_is_live(ancestor)
- && self
- .with_current_committed_fragment_link(ancestor, |_| ())
- .is_some()
+ && self.with_current_committed_fragment_link(ancestor, |_| ()).is_some()
{
self.paintable_rows.invalidate_committed_geometry(ancestor.slot_index());
ancestor = self.data(ancestor).parent.get();
diff --git a/Libraries/LibWeb/Rust/src/painting/paintable_build.rs b/Libraries/LibWeb/Rust/src/painting/paintable_build.rs
index 8289adbd319..a3dad4a1b75 100644
--- a/Libraries/LibWeb/Rust/src/painting/paintable_build.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paintable_build.rs
@@ -27,7 +27,6 @@ pub(crate) struct ReplacedCommittedFragmentLink {
#[derive(Clone, Copy, Default)]
pub(crate) struct LineDataChange {
- pub(crate) has_pieces: bool,
pub(crate) content_changed: bool,
pub(crate) item_order_changed: bool,
}
@@ -39,6 +38,20 @@ pub(crate) struct LineRootChanges {
pub(crate) inline_item_order_changed: bool,
}
+impl LineRootChanges {
+ pub(crate) fn all() -> Self {
+ Self {
+ fragment_changed: true,
+ inline_content_changed: true,
+ inline_item_order_changed: true,
+ }
+ }
+
+ pub(crate) fn any(self) -> bool {
+ self.fragment_changed || self.inline_content_changed || self.inline_item_order_changed
+ }
+}
+
fn same_inline_content(left: &fragment_tree::Fragment, right: &fragment_tree::Fragment) -> bool {
match (&left.line_data, &right.line_data) {
(None, None) => true,
@@ -114,6 +127,19 @@ impl<'a> PaintableCommit<'a> {
self.arena_mut().clear_absolute_rect_memo();
}
+ /// Clears the committed link and row of a box the commit left without them.
+ pub(crate) fn clear_node(&mut self, node: formatting_context::Node) {
+ self.arena_mut().clear_committed_fragment_link(node);
+ if self.arena().paintable_rows().paintable_row_is_populated(node) {
+ let reset = self
+ .arena()
+ .prepare_paintable_row_cleared_reset(node)
+ .expect("live row for node could not be cleared");
+ self.row_reset_notifications.push(reset);
+ self.arena_mut().paintable_row_cleared(reset);
+ }
+ }
+
pub(crate) fn prepare_node(
&mut self,
node: formatting_context::Node,
@@ -134,17 +160,7 @@ impl<'a> PaintableCommit<'a> {
let row_existed_before_this_commit = self.arena().paintable_rows().paintable_row_is_populated(node);
let previous_offset = row_existed_before_this_commit.then(|| self.arena().live_paintable_data(node).offset);
if !wants_paintable {
- self.arena_mut().clear_committed_fragment_link(node);
- if row_existed_before_this_commit {
- let reset = {
- let arena = self.arena();
- arena
- .prepare_paintable_row_cleared_reset(node)
- .expect("live row for node could not be cleared")
- };
- self.row_reset_notifications.push(reset);
- self.arena_mut().paintable_row_cleared(reset);
- }
+ self.clear_node(node);
return PreparedPaintable {
has_paintable_row: false,
row_existed_before_this_commit: false,
@@ -495,7 +511,6 @@ impl<'a> PaintableCommit<'a> {
self.arena_mut().note_line_root_needs_fragment_ownership(slot);
}
LineDataChange {
- has_pieces,
content_changed,
item_order_changed,
}