Skip to content

Truly independent web browser

LibWeb: Name the innermost layout run in the run's view of the pass

Aliaksandr Kalenik8e071f3471b2
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index ca6a9bade5d..18fd0c948fb 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -2053,12 +2053,11 @@ fn execute_formatting_context_run(
assert!(!box_.is_invalid());
let root_used = root_cells.materialize_record();
RunRecords::with_root(
- callbacks.scratch(),
- callbacks.arena(),
+ callbacks,
box_,
root_containing_block,
&root_used,
- |records| {
+ |records, callbacks| {
let run = FormattingContextRun {
purpose,
records,
@@ -3134,7 +3133,7 @@ fn compute_root_layout(input: LayoutStageInput<'_>, should_collect_devtools_layo
percentage_basis_block_size: Some(viewport_block_size),
..ContainingBlockConstraints::default()
};
- RunRecords::with_unrooted(scratch, arena, root, NodeSlotId::INVALID, |entry_records| {
+ RunRecords::with_unrooted(callbacks, root, NodeSlotId::INVALID, |entry_records, callbacks| {
let _trace = arena.layout_trace.pass(None);
let viewport_used = entry_records.create_used_values(&callbacks, root, root_constraints);
let entry_fragments = std::rc::Rc::new(fragment_tree::RunFragmentBuilder::new_entry_accumulator(root));
@@ -3278,11 +3277,10 @@ fn compute_subtree_layout_fragments(input: LayoutStageInput<'_>, containing_bloc
(root, containing_block)
};
RunRecords::with_unrooted(
- scratch,
- arena,
+ callbacks,
entry_root,
entry_root_containing_block,
- |entry_records| {
+ |entry_records, callbacks| {
let _trace = arena.layout_trace.pass(Some(root));
let entry_fragments =
std::rc::Rc::new(fragment_tree::RunFragmentBuilder::new_entry_accumulator(entry_root));
@@ -3434,7 +3432,7 @@ mod baseline_tests {
false,
);
let parent_used = UsedValues::default();
- RunRecords::with_root(&scratch, &arena, parent, Node::INVALID, &parent_used, |records| {
+ RunRecords::with_root(callbacks, parent, Node::INVALID, &parent_used, |records, callbacks| {
let child_used = records.register(child, UsedValues::default());
store_derived_baselines(
child_used,
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index 1e58380c696..9e72731849c 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -2983,21 +2983,19 @@ impl<'pass> GridFormattingContext<'pass> {
scratch_root
.has_definite_block_size
.set(live.has_definite_block_size.get());
- let arena = self.run.callbacks.arena();
let containing_block = self.run.callbacks.in_flow_containing_block(subgrid.box_);
RunRecords::with_root(
- self.run.callbacks.scratch(),
- arena,
+ self.run.callbacks,
subgrid.box_,
containing_block,
&scratch_root,
- |records| {
+ |records, callbacks| {
let scratch_run = FormattingContextRun {
purpose: formatting_context::LayoutPurpose::Measurement,
records,
box_: subgrid.box_,
layout_mode: LayoutMode::IntrinsicSizing,
- callbacks: self.run.callbacks,
+ callbacks,
should_collect_devtools_layout_data: false,
treat_block_axis_percentage_insets_as_auto_beyond_root: false,
fragments: None,
diff --git a/Libraries/LibWeb/Rust/src/layout/intrinsic_sizing.rs b/Libraries/LibWeb/Rust/src/layout/intrinsic_sizing.rs
index aae196bc0af..3c8db6bed5e 100644
--- a/Libraries/LibWeb/Rust/src/layout/intrinsic_sizing.rs
+++ b/Libraries/LibWeb/Rust/src/layout/intrinsic_sizing.rs
@@ -33,51 +33,44 @@ pub(super) fn compute_inline_sizes(
{
return None;
}
- RunRecords::with_root(
- callbacks.scratch(),
- callbacks.arena(),
- node,
- node_containing_block,
- root,
- |records| {
- let run = FormattingContextRun {
- purpose: formatting_context::LayoutPurpose::Measurement,
- records,
- box_: node,
- layout_mode: LayoutMode::IntrinsicSizing,
- callbacks,
- should_collect_devtools_layout_data: false,
- treat_block_axis_percentage_insets_as_auto_beyond_root: false,
- fragments: None,
- previous_line_data: None,
- fragmentainer: None,
- };
- let sizing = run.sizing();
- let input = LayoutInput::new(
- AvailableSpace {
- inline_size: AvailableSize::MaxContent,
- block_size,
- },
- sizing.constraints_for_child_context(node, constraints),
- ParticipationInParentFormattingContext::BlockLevel,
- );
- let measurement = compute_from_atomic_children(&run, input).or_else(|| compute_from_items(&run, input))?;
- if !facts.lays_out_in_columns() {
- return Some(measurement);
- }
- Some(IntrinsicInlineSizeMeasurement {
- automatic_content_inline_size: fragmentation::multicol_intrinsic_inline_size(
- style,
- measurement.automatic_content_inline_size,
- false,
- ),
- min_content_inline_size_from_max_content_layout: measurement
- .min_content_inline_size_from_max_content_layout
- .map(|minimum| fragmentation::multicol_intrinsic_inline_size(style, minimum, true)),
- ..measurement
- })
- },
- )
+ RunRecords::with_root(callbacks, node, node_containing_block, root, |records, callbacks| {
+ let run = FormattingContextRun {
+ purpose: formatting_context::LayoutPurpose::Measurement,
+ records,
+ box_: node,
+ layout_mode: LayoutMode::IntrinsicSizing,
+ callbacks,
+ should_collect_devtools_layout_data: false,
+ treat_block_axis_percentage_insets_as_auto_beyond_root: false,
+ fragments: None,
+ previous_line_data: None,
+ fragmentainer: None,
+ };
+ let sizing = run.sizing();
+ let input = LayoutInput::new(
+ AvailableSpace {
+ inline_size: AvailableSize::MaxContent,
+ block_size,
+ },
+ sizing.constraints_for_child_context(node, constraints),
+ ParticipationInParentFormattingContext::BlockLevel,
+ );
+ let measurement = compute_from_atomic_children(&run, input).or_else(|| compute_from_items(&run, input))?;
+ if !facts.lays_out_in_columns() {
+ return Some(measurement);
+ }
+ Some(IntrinsicInlineSizeMeasurement {
+ automatic_content_inline_size: fragmentation::multicol_intrinsic_inline_size(
+ style,
+ measurement.automatic_content_inline_size,
+ false,
+ ),
+ min_content_inline_size_from_max_content_layout: measurement
+ .min_content_inline_size_from_max_content_layout
+ .map(|minimum| fragmentation::multicol_intrinsic_inline_size(style, minimum, true)),
+ ..measurement
+ })
+ })
}
fn compute_from_items(run: &FormattingContextRun<'_>, input: LayoutInput) -> Option<IntrinsicInlineSizeMeasurement> {
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index 87443c0bef5..00b4f9dfed7 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -1309,7 +1309,6 @@ pub(crate) struct LayoutNodeArena {
fc_run_cache_store: super::fc_run_cache::FcRunCacheArenaStore,
in_flow_block_cache_store: super::in_flow_block_cache::InFlowBlockCacheStore,
pub(super) layout_trace: super::trace::LayoutTrace,
- pub(super) innermost_run: Cell<(NodeSlotId, NodeSlotId)>,
pub(crate) paintable_rows: crate::painting::paintable_rows::PaintableRowStore,
paint_state: RefCell<crate::painting::paint_state::PaintState>,
// Hit testing can measure overflow and invalidate painting state while querying this list.
@@ -1426,7 +1425,6 @@ impl LayoutNodeArena {
fc_run_cache_store: super::fc_run_cache::FcRunCacheArenaStore::default(),
in_flow_block_cache_store: super::in_flow_block_cache::InFlowBlockCacheStore::default(),
layout_trace: super::trace::LayoutTrace::default(),
- innermost_run: Cell::new((NodeSlotId::INVALID, NodeSlotId::INVALID)),
paintable_rows: crate::painting::paintable_rows::PaintableRowStore::default(),
paint_state: RefCell::new(crate::painting::paint_state::PaintState::default()),
scrollable_overflow: Default::default(),
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
index 9f1524a9888..50db7c57dad 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
@@ -70,6 +70,9 @@ pub(crate) struct LayoutPass<'arena> {
pub(crate) document_in_quirks_mode: bool,
/// The frame of the formatting context the pass is laying out, in which style reads see sizes and box sides.
pub(crate) frame: super::geometry::Frame,
+ /// The root of the run this pass view was handed to and that root's containing block, where containing block
+ /// searches from inside the run stop. Invalid outside any run.
+ innermost_run: (Node, Node),
}
impl<'arena> LayoutPass<'arena> {
@@ -89,9 +92,33 @@ impl<'arena> LayoutPass<'arena> {
initial_containing_block_block_size,
document_in_quirks_mode,
frame: super::geometry::Frame::PHYSICAL,
+ innermost_run: (NodeSlotId::INVALID, NodeSlotId::INVALID),
}
}
+ /// A pass whose only question to the document has no answer, over `arena` and `scratch`.
+ #[cfg(test)]
+ pub(crate) fn for_test(arena: &'arena LayoutNodeArena, scratch: &'arena super::run_records::LayoutScratch) -> Self {
+ Self::new(
+ arena,
+ scratch,
+ ContainerLengthBasesQuery {
+ context: std::ptr::null_mut(),
+ query: None,
+ },
+ CssPixels::default(),
+ CssPixels::default(),
+ false,
+ )
+ }
+
+ /// This pass, as the run rooted at `root` sees it.
+ #[inline]
+ pub(super) fn for_run(mut self, root: Node, root_containing_block: Node) -> Self {
+ self.innermost_run = (root, root_containing_block);
+ self
+ }
+
/// This pass, laying out in the frame of `writing_mode`.
#[inline]
pub(crate) fn in_frame_of(mut self, writing_mode: u8) -> Self {
@@ -245,7 +272,7 @@ impl<'arena> LayoutPass<'arena> {
}
pub(crate) fn in_flow_containing_block(&self, node: Node) -> Node {
- let (innermost_root, innermost_root_containing_block) = self.arena.innermost_run.get();
+ let (innermost_root, innermost_root_containing_block) = self.innermost_run;
if node == innermost_root {
return innermost_root_containing_block;
}
diff --git a/Libraries/LibWeb/Rust/src/layout/run_records.rs b/Libraries/LibWeb/Rust/src/layout/run_records.rs
index 4ea1bb2e1ff..cb86e2125b5 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_records.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_records.rs
@@ -343,17 +343,6 @@ pub(crate) struct RunRecords<'arena> {
omitted_line_layout: Cell<bool>,
}
-struct InnermostRunGuard<'arena> {
- arena: &'arena LayoutNodeArena,
- previous: (Node, Node),
-}
-
-impl Drop for InnermostRunGuard<'_> {
- fn drop(&mut self) {
- self.arena.innermost_run.set(self.previous);
- }
-}
-
enum RunRecordClaim {
Claimed,
AlreadyClaimed,
@@ -361,43 +350,38 @@ enum RunRecordClaim {
}
impl<'arena> RunRecords<'arena> {
+ /// Runs `run` with the records of a run rooted at `root`, and the pass as that run sees it.
pub(crate) fn with_root<R>(
- scratch: &'arena LayoutScratch,
- arena: &'arena LayoutNodeArena,
+ callbacks: LayoutPass<'arena>,
root: Node,
root_containing_block: Node,
root_used: &'arena UsedValues,
- run: impl FnOnce(&Self) -> R,
+ run: impl FnOnce(&Self, LayoutPass<'arena>) -> R,
) -> R {
- Self::enter(scratch, arena, root, root_containing_block, Some(root_used), run)
+ Self::enter(callbacks, root, root_containing_block, Some(root_used), run)
}
pub(crate) fn with_unrooted<R>(
- scratch: &'arena LayoutScratch,
- arena: &'arena LayoutNodeArena,
+ callbacks: LayoutPass<'arena>,
root: Node,
root_containing_block: Node,
- run: impl FnOnce(&Self) -> R,
+ run: impl FnOnce(&Self, LayoutPass<'arena>) -> R,
) -> R {
- Self::enter(scratch, arena, root, root_containing_block, None, run)
+ Self::enter(callbacks, root, root_containing_block, None, run)
}
fn enter<R>(
- scratch: &'arena LayoutScratch,
- arena: &'arena LayoutNodeArena,
+ callbacks: LayoutPass<'arena>,
root: Node,
root_containing_block: Node,
root_used: Option<&'arena UsedValues>,
- run: impl FnOnce(&Self) -> R,
+ run: impl FnOnce(&Self, LayoutPass<'arena>) -> R,
) -> R {
debug_assert!(
- !arena.has_intrinsic_size_cache_drops(),
+ !callbacks.arena().has_intrinsic_size_cache_drops(),
"the layout stage drops released intrinsic size cache entries before its runs read a cache"
);
- let _innermost_run = InnermostRunGuard {
- arena,
- previous: arena.innermost_run.replace((root, root_containing_block)),
- };
+ let scratch = callbacks.scratch();
let records = Self {
root,
root_used,
@@ -408,7 +392,7 @@ impl<'arena> RunRecords<'arena> {
table_inline_layouts: RefCell::new(HashMap::default()),
omitted_line_layout: Cell::new(false),
};
- run(&records)
+ run(&records, callbacks.for_run(root, root_containing_block))
}
/// What this run found out for the host, which its outputs carry to the run that uses its result.
@@ -637,24 +621,41 @@ mod tests {
let child = arena.allocate_for_test().slot;
let root_used = UsedValues::default();
let nested_used = UsedValues::default();
- let parent_nonce = RunRecords::with_root(&scratch, &arena, root, NodeSlotId::INVALID, &root_used, |parent| {
- let child_used: *const UsedValues = parent.register(child, UsedValues::default());
- RunRecords::with_root(&scratch, &arena, child, root, &nested_used, |nested| {
- assert!(std::ptr::eq(parent.used_values(child), child_used));
- assert!(std::ptr::eq(parent.used_values(root), &raw const root_used));
- assert!(std::ptr::eq(nested.used_values(child), &raw const nested_used));
- assert!(nested.used_values_if_owned(root).is_none());
- RunRecords::with_unrooted(&scratch, &arena, child, root, |measurement| {
- let measured: *const UsedValues = measurement.register(child, UsedValues::default());
- assert!(std::ptr::eq(measurement.used_values(child), measured));
- assert!(std::ptr::eq(parent.used_values(child), child_used));
- assert!(std::ptr::eq(nested.used_values(child), &raw const nested_used));
- });
+ let parent_nonce = RunRecords::with_root(
+ LayoutPass::for_test(&arena, &scratch),
+ root,
+ NodeSlotId::INVALID,
+ &root_used,
+ |parent, _| {
+ let child_used: *const UsedValues = parent.register(child, UsedValues::default());
+ RunRecords::with_root(
+ LayoutPass::for_test(&arena, &scratch),
+ child,
+ root,
+ &nested_used,
+ |nested, _| {
+ assert!(std::ptr::eq(parent.used_values(child), child_used));
+ assert!(std::ptr::eq(parent.used_values(root), &raw const root_used));
+ assert!(std::ptr::eq(nested.used_values(child), &raw const nested_used));
+ assert!(nested.used_values_if_owned(root).is_none());
+ RunRecords::with_unrooted(
+ LayoutPass::for_test(&arena, &scratch),
+ child,
+ root,
+ |measurement, _| {
+ let measured: *const UsedValues = measurement.register(child, UsedValues::default());
+ assert!(std::ptr::eq(measurement.used_values(child), measured));
+ assert!(std::ptr::eq(parent.used_values(child), child_used));
+ assert!(std::ptr::eq(nested.used_values(child), &raw const nested_used));
+ },
+ );
+ assert!(std::ptr::eq(parent.used_values(child), child_used));
+ },
+ );
assert!(std::ptr::eq(parent.used_values(child), child_used));
- });
- assert!(std::ptr::eq(parent.used_values(child), child_used));
- parent.nonce
- });
+ parent.nonce
+ },
+ );
assert!(scratch.run_record(root.slot_index(), parent_nonce).is_none());
assert_eq!(scratch.run_record_stack.length(), 0);
}
@@ -668,25 +669,40 @@ mod tests {
let lines = std::sync::Arc::new(crate::layout::inline_content::InlineContent::default());
let weak_lines = std::sync::Arc::downgrade(&lines);
let root_used = UsedValues::default();
- let first_record = RunRecords::with_root(&scratch, &arena, root, NodeSlotId::INVALID, &root_used, |records| {
- let used = records.register(child, UsedValues::default());
- used.set_finished_line_data(lines);
- std::ptr::from_ref(used)
- });
+ let first_record = RunRecords::with_root(
+ LayoutPass::for_test(&arena, &scratch),
+ root,
+ NodeSlotId::INVALID,
+ &root_used,
+ |records, _| {
+ let used = records.register(child, UsedValues::default());
+ used.set_finished_line_data(lines);
+ std::ptr::from_ref(used)
+ },
+ );
assert!(weak_lines.upgrade().is_none());
- let second_record = RunRecords::with_root(&scratch, &arena, root, NodeSlotId::INVALID, &root_used, |records| {
- std::ptr::from_ref(records.register(child, UsedValues::default()))
- });
+ let second_record = RunRecords::with_root(
+ LayoutPass::for_test(&arena, &scratch),
+ root,
+ NodeSlotId::INVALID,
+ &root_used,
+ |records, _| std::ptr::from_ref(records.register(child, UsedValues::default())),
+ );
assert_eq!(first_record, second_record);
let nodes: Vec<_> = (0..RECORDS_PER_STACK_CHUNK * 2 + 1)
.map(|_| arena.allocate_for_test().slot)
.collect();
- RunRecords::with_unrooted(&scratch, &arena, root, NodeSlotId::INVALID, |records| {
- for node in nodes {
- records.register(node, UsedValues::default());
- }
- });
+ RunRecords::with_unrooted(
+ LayoutPass::for_test(&arena, &scratch),
+ root,
+ NodeSlotId::INVALID,
+ |records, _| {
+ for node in nodes {
+ records.register(node, UsedValues::default());
+ }
+ },
+ );
assert_eq!(scratch.run_record_stack.chunks.borrow().len(), 3);
scratch.end_layout_pass();
assert_eq!(scratch.run_record_stack.chunks.borrow().len(), 1);
@@ -699,9 +715,15 @@ mod tests {
let root = arena.allocate_for_test().slot;
let child = arena.allocate_for_test().slot;
let root_used = UsedValues::default();
- RunRecords::with_root(&scratch, &arena, root, NodeSlotId::INVALID, &root_used, |records| {
- records.register(child, UsedValues::default());
- });
+ RunRecords::with_root(
+ LayoutPass::for_test(&arena, &scratch),
+ root,
+ NodeSlotId::INVALID,
+ &root_used,
+ |records, _| {
+ records.register(child, UsedValues::default());
+ },
+ );
let _ = arena.free_subtree(child);
}
@@ -712,9 +734,15 @@ mod tests {
let scratch = LayoutScratch::default();
let root = arena.allocate_for_test().slot;
let root_used = UsedValues::default();
- RunRecords::with_root(&scratch, &arena, root, NodeSlotId::INVALID, &root_used, |records| {
- records.register(root, UsedValues::default());
- });
+ RunRecords::with_root(
+ LayoutPass::for_test(&arena, &scratch),
+ root,
+ NodeSlotId::INVALID,
+ &root_used,
+ |records, _| {
+ records.register(root, UsedValues::default());
+ },
+ );
}
#[test]
@@ -725,11 +753,22 @@ mod tests {
let root = arena.allocate_for_test().slot;
let child = arena.allocate_for_test().slot;
let root_used = UsedValues::default();
- RunRecords::with_unrooted(&scratch, &arena, root, NodeSlotId::INVALID, |parent| {
- RunRecords::with_root(&scratch, &arena, root, NodeSlotId::INVALID, &root_used, |_| {
- parent.register(child, UsedValues::default());
- });
- });
+ RunRecords::with_unrooted(
+ LayoutPass::for_test(&arena, &scratch),
+ root,
+ NodeSlotId::INVALID,
+ |parent, _| {
+ RunRecords::with_root(
+ LayoutPass::for_test(&arena, &scratch),
+ root,
+ NodeSlotId::INVALID,
+ &root_used,
+ |_, _| {
+ parent.register(child, UsedValues::default());
+ },
+ );
+ },
+ );
}
#[test]
@@ -740,17 +779,29 @@ mod tests {
let child = arena.allocate_for_test().slot;
let root_used = UsedValues::default();
let nested_used = UsedValues::default();
- RunRecords::with_root(&scratch, &arena, root, NodeSlotId::INVALID, &root_used, |parent| {
- let child_used: *const UsedValues = parent.register(child, UsedValues::default());
- let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
- RunRecords::with_root(&scratch, &arena, child, root, &nested_used, |nested| {
- nested.register(root, UsedValues::default());
- panic!("abort the nested measurement");
- });
- }));
- assert!(result.is_err());
- assert!(std::ptr::eq(parent.used_values(root), &raw const root_used));
- assert!(std::ptr::eq(parent.used_values(child), child_used));
- });
+ RunRecords::with_root(
+ LayoutPass::for_test(&arena, &scratch),
+ root,
+ NodeSlotId::INVALID,
+ &root_used,
+ |parent, _| {
+ let child_used: *const UsedValues = parent.register(child, UsedValues::default());
+ let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
+ RunRecords::with_root(
+ LayoutPass::for_test(&arena, &scratch),
+ child,
+ root,
+ &nested_used,
+ |nested, _| {
+ nested.register(root, UsedValues::default());
+ panic!("abort the nested measurement");
+ },
+ );
+ }));
+ assert!(result.is_err());
+ assert!(std::ptr::eq(parent.used_values(root), &raw const root_used));
+ assert!(std::ptr::eq(parent.used_values(child), child_used));
+ },
+ );
}
}
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index bffd32ff2e5..be03be4866f 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -3238,18 +3238,17 @@ impl<'pass> SizingContext<'pass> {
.set(table_style.padding_right().to_px(containing_block_inline_size));
RunRecords::with_root(
- measurement.callbacks().scratch(),
- measurement.callbacks().arena(),
+ *measurement.callbacks(),
table_box,
measurement.callbacks().in_flow_containing_block(table_box),
&table_used,
- |records| {
+ |records, callbacks| {
let table_run = FormattingContextRun {
purpose: formatting_context::LayoutPurpose::Measurement,
records,
box_: table_box,
layout_mode: LayoutMode::IntrinsicSizing,
- callbacks: *measurement.callbacks(),
+ callbacks,
should_collect_devtools_layout_data: false,
treat_block_axis_percentage_insets_as_auto_beyond_root: false,
fragments: None,