Truly independent web browser
LibWeb: Hand a child's lines back with its laid out box
Aliaksandr Kalenikb0485cbc6896
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex 7809513cd92..0db501c7f99 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -127,6 +127,7 @@ pub(crate) fn run_root_without_parent_fragments(result,laid_out_box,fragment_arrival,+ ..} = run_formatting_context(None,None,@@ -1970,9 +1971,29 @@ fn body_input_with_inner_available_space(run: &FormattingContextRun, input: &Laypub(crate) struct ChildRunOutcome {pub(crate) result: ChildLayoutResult,pub(crate) laid_out_box: run_records::LaidOutChildBox,+ pub(crate) line_data: Option<std::sync::Arc<inline_content::InlineContent>>,pub(crate) fragment_arrival: Option<fragment_tree::ChildFragmentArrival>,}+impl ChildRunOutcome {+ /// Hands the laid out box to `run`, the run that holds `child`, and returns the rest of the outcome.+ pub(crate) fn hand_box_back_to(+ self,+ run: &FormattingContextRun,+ child: Node,+ ) -> (ChildLayoutResult, Option<fragment_tree::ChildFragmentArrival>) {+ run.records.receive_laid_out_child_box(+ child,+ run_records::ChildLaidOutByItsOwnRun {+ laid_out: self.laid_out_box,+ line_data: self.line_data,+ frame: run.callbacks.frame,+ },+ );+ (self.result, self.fragment_arrival)+ }+}+#[expect(clippy::too_many_arguments)]/// Runs the formatting context of `box_` with the input cells `root_cells` its parent sized it in. A parent that still/// keeps a record of the box hands it as `parent_used`, which takes the cells and what the run hands back.@@ -2035,7 +2056,7 @@ pub(super) fn run_formatting_context() {Ok(attempt) => attempt,Err(hit) => {- if let Some((result, laid_out_box, fragment)) = reuse_committed_subtree_as_it_is(+ if let Some(outcome) = reuse_committed_subtree_as_it_is(parent_fragments,parent_used,box_,@@ -2045,14 +2066,7 @@ pub(super) fn run_formatting_context(&cache_key,&hit,) {- return ChildRunOutcome {- result,- laid_out_box,- fragment_arrival: Some(fragment_tree::ChildFragmentArrival::CommittedFragment {- fragment,- reuses_subtree: true,- }),- };+ return outcome;}let reuses_committed_subtree = hit.reuses_committed_subtree;let action = if !builds_fragments {@@ -2170,11 +2184,7 @@ fn reuse_committed_subtree_as_it_is(callbacks: &LayoutPass<'_>,cache_key: &fc_run_cache::FcRunCacheKey,hit: &fc_run_cache::FcRunCacheHit,-) -> Option<(- ChildLayoutResult,- run_records::LaidOutChildBox,- std::sync::Arc<fragment_tree::Fragment>,-)> {+) -> Option<ChildRunOutcome> {parent_fragments?;let root_used = root_used.expect("a parent that builds fragments keeps a record of its child");let entry = &*hit.entry;@@ -2215,7 +2225,15 @@ fn reuse_committed_subtree_as_it_is(&entry.outputs.result,content_block_size_adopted_after_run,);- Some((entry.outputs.result, laid_out_box, fragment))+ Some(ChildRunOutcome {+ result: entry.outputs.result,+ laid_out_box,+ line_data: entry.outputs.root_outcome.line_data.clone(),+ fragment_arrival: Some(fragment_tree::ChildFragmentArrival::CommittedFragment {+ fragment,+ reuses_subtree: true,+ }),+ })}pub(crate) fn explicit_intrinsic_inner_inline_size(facts: &NodeFacts<'_>) -> CssPixels {@@ -2940,11 +2958,7 @@ pub(crate) fn layout_inside_child_with_input_cells(}return ChildLayout::without_fragments(ChildLayoutOutcome::ReenterCurrent);};- let ChildRunOutcome {- result,- laid_out_box,- fragment_arrival,- } = run_formatting_context(+ let (result, fragment_arrival) = run_formatting_context(run.fragments.as_deref(),Some(used),used_values::RootInputCells::capture(used),@@ -2956,8 +2970,8 @@ pub(crate) fn layout_inside_child_with_input_cells(run.callbacks,input,run.records.take_table_inline_layout(child),- );- run.records.receive_laid_out_child_box(child, laid_out_box);+ )+ .hand_box_back_to(run, child);ChildLayout {outcome: ChildLayoutOutcome::Created(result),fragment_arrival,@@ -3306,6 +3320,7 @@ fn absorb_run_outputs(let content_block_size_adopted_after_run =content_block_size_adopted_after_run(input, result.automatic_content_block_size);let laid_out_box = root_outcome.laid_out_child_box(root_cells, &result, content_block_size_adopted_after_run);+ let line_data = root_outcome.line_data.clone();if let Some(parent_used) = parent_used {root_outcome.apply_to_record(parent_used, content_block_size_adopted_after_run);}@@ -3319,6 +3334,7 @@ fn absorb_run_outputs(ChildRunOutcome {result,laid_out_box,+ line_data,fragment_arrival,}}@@ -3586,7 +3602,7 @@ fn compute_root_layout(input: LayoutStageInput<'_>, should_collect_devtools_layo).with_forced_sizes(viewport_inline_size, viewport_block_size);let fc_type = independent_formatting_context_type(root_for_layout, &callbacks);- let root_outcome = run_formatting_context(+ let (_, fragment_arrival) = run_formatting_context(Some(&entry_fragments),Some(root_for_layout_used),used_values::RootInputCells::capture(root_for_layout_used),@@ -3598,13 +3614,14 @@ fn compute_root_layout(input: LayoutStageInput<'_>, should_collect_devtools_layocallbacks,input,None,- );+ )+ .hand_box_back_to(&entry_run, root_for_layout);place_laid_out_child(&entry_run,root_for_layout,FfiCssPixelPoint::default(),None,- root_outcome.fragment_arrival,+ fragment_arrival,);finish_entry_pass(entry_records,@@ -3739,7 +3756,7 @@ fn layout_subtree_with_frozen_root_geometry(run: &FormattingContextRun<'_>) {debug_assert!(facts.is_svg_svg_box());let fc_type = formatting_context_type_created_by_box(facts).expect("partial relayout root must establish an independent formatting context");- let root_outcome = run_formatting_context(+ let (_, fragment_arrival) = run_formatting_context(Some(fragments),Some(root_used),used_values::RootInputCells::capture(root_used),@@ -3751,13 +3768,14 @@ fn layout_subtree_with_frozen_root_geometry(run: &FormattingContextRun<'_>) {*callbacks,input,None,- );+ )+ .hand_box_back_to(run, root);// The retained offset already includes relative positioning. Publish it directly instead// of applying placement adjustments a second time.let Some(fragment_tree::ChildFragmentArrival::RunRoot {root: unplaced_root,reuses_committed_subtree,- }) = root_outcome.fragment_arrival+ }) = fragment_arrivalelse {panic!("the root of a partial relayout is laid out by a run of its own");};diff --git a/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rsindex e7dbfb3170e..ab250b0865a 100644--- a/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/replaced_with_children_formatting_context.rs@@ -47,11 +47,7 @@ pub(super) fn layout_replaced_with_children(let mut wrapper_cells = used_values::RootInputCells::capture(wrapper_record);wrapper_cells.set_content_inline_size(content_inline_size);- let formatting_context::ChildRunOutcome {- result: wrapper_result,- fragment_arrival,- ..- } = formatting_context::run_formatting_context(+ let (wrapper_result, fragment_arrival) = formatting_context::run_formatting_context(run.fragments.as_deref(),Some(wrapper_record),wrapper_cells,@@ -71,7 +67,8 @@ pub(super) fn layout_replaced_with_children(participation: geometry::ParticipationInParentFormattingContext::Item,},None,- );+ )+ .hand_box_back_to(run, wrapper);formatting_context::propagate_percentage_block_size_dependency_to_containing_block(run.records,&run.callbacks,diff --git a/Libraries/LibWeb/Rust/src/layout/run_records.rs b/Libraries/LibWeb/Rust/src/layout/run_records.rsindex 851f4ad098c..3b574c41409 100644--- a/Libraries/LibWeb/Rust/src/layout/run_records.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_records.rs@@ -137,10 +137,15 @@ enum KeptBoxHolding {/// returned, for a child laid out by a run of its own.Record {record: u32,- handed_back_by_its_run: Option<LaidOutChildBox>,+ handed_back_by_its_run: Option<ChildLaidOutByItsOwnRun>,},}+/// Whether a run checks the boxes it keeps for its children against their records, which it keeps as well for now.+fn checks_kept_boxes_against_records() -> bool {+ cfg!(debug_assertions) || super::run_repeatability::verification_is_enabled()+}+impl Default for LayoutScratch {fn default() -> Self {Self {@@ -387,6 +392,15 @@ pub(crate) struct LaidOutChildBox {pub(crate) orthogonal_flow_baselines: Option<DerivedBaselines>,}+/// What the parent of a box laid out by a run of its own keeps of it once that run returns: the box, the lines the run+/// laid out in it, and the frame the box's metrics are in.+#[derive(Clone)]+pub(crate) struct ChildLaidOutByItsOwnRun {+ pub(crate) laid_out: LaidOutChildBox,+ pub(crate) line_data: Option<std::sync::Arc<inline_content::InlineContent>>,+ pub(crate) frame: geometry::Frame,+}+/// The sizes, size constraints, box sides and baselines of a box as a run sees them: kept as a value for a child laid/// out by a run of its own, or read from the record of a box the run lays out itself.#[derive(Clone, Copy)]@@ -974,14 +988,14 @@ impl<'arena> RunRecords<'arena> {/// Keeps what the run of `child`, a box this run holds the record of, handed back once it returned.#[track_caller]- pub(crate) fn receive_laid_out_child_box(&self, child: Node, laid_out: LaidOutChildBox) {+ pub(crate) fn receive_laid_out_child_box(&self, child: Node, handed_back: ChildLaidOutByItsOwnRun) {let Some(HeldBox::Kept(index)) = self.held_box(child) else {self.panic_for_box_not_held(child)};let KeptBoxHolding::Record {handed_back_by_its_run, ..} = &mut self.scratch.kept_boxes.borrow_mut()[index as usize].holding;- *handed_back_by_its_run = Some(laid_out);+ *handed_back_by_its_run = Some(handed_back);}/// The sizes, size constraints, box sides and baselines of `node`, a box this run lays out or a child of one laid@@ -1001,16 +1015,21 @@ impl<'arena> RunRecords<'arena> {HeldBox::RootWithRecord => return self.root_used.as_ref().map(BoxMetrics::LaidOutByThisRun),HeldBox::Kept(index) => index,};- let KeptBoxHolding::Record {- record,- handed_back_by_its_run,- } = self.scratch.kept_boxes.borrow()[index as usize].holding;+ let (record, laid_out) = match &self.scratch.kept_boxes.borrow()[index as usize].holding {+ KeptBoxHolding::Record {+ record,+ handed_back_by_its_run,+ } => (+ *record,+ handed_back_by_its_run.as_ref().map(|handed_back| handed_back.laid_out),+ ),+ };// SAFETY: This run registered the record, and it stays on the stack until this run returns.let record = unsafe { self.scratch.run_record_stack.get(record).as_ref() };- let Some(laid_out) = handed_back_by_its_run else {+ let Some(laid_out) = laid_out else {return Some(BoxMetrics::LaidOutByThisRun(record));};- if cfg!(debug_assertions) || super::run_repeatability::verification_is_enabled() {+ if checks_kept_boxes_against_records() {assert_eq!(laid_out.cells,used_values::RootInputCells::capture(record),@@ -1040,7 +1059,8 @@ impl<'arena> RunRecords<'arena> {} = &mut kept_boxes[index as usize].holding;// SAFETY: This run registered the record, and it stays on the stack until this run returns.unsafe { self.scratch.run_record_stack.get(*record).as_ref() }.move_box_metrics_to_frame(from, to);- if let Some(laid_out) = handed_back_by_its_run {+ if let Some(ChildLaidOutByItsOwnRun { laid_out, frame, .. }) = handed_back_by_its_run {+ debug_assert_eq!(*frame, from, "a kept box moves out of the frame it is in");// NB: The baselines stay those of the parent's frame, which every reader of them is in.laid_out.cells = used_values::UsedValuesCellState {inputs: laid_out.cells,@@ -1048,6 +1068,7 @@ impl<'arena> RunRecords<'arena> {}.in_frame(from, to).inputs;+ *frame = to;}}@@ -1060,9 +1081,39 @@ impl<'arena> RunRecords<'arena> {) -> fragment_tree::FragmentGeometry {let used = self.used_values(node);let line_data = used.finish_line_data(callbacks);- fragment_tree::FragmentGeometry {+ let geometry_of_record = fragment_tree::FragmentGeometry {cells: used_values::RootInputCells::capture(used),line_data,+ };+ let Some(HeldBox::Kept(index)) = self.held_box(node) else {+ return geometry_of_record;+ };+ let handed_back = match &self.scratch.kept_boxes.borrow()[index as usize].holding {+ KeptBoxHolding::Record {+ handed_back_by_its_run, ..+ } => handed_back_by_its_run.clone(),+ };+ let Some(handed_back) = handed_back else {+ return geometry_of_record;+ };+ if checks_kept_boxes_against_records() {+ assert_eq!(+ handed_back.frame, callbacks.frame,+ "the fragment of {node:?} is built in another frame than the one its box is kept in"+ );+ assert!(+ handed_back.laid_out.cells == geometry_of_record.cells+ && match (&handed_back.line_data, &geometry_of_record.line_data) {+ (None, None) => true,+ (Some(kept), Some(recorded)) => std::sync::Arc::ptr_eq(kept, recorded),+ _ => false,+ },+ "the parent's record of {node:?} changed after its run handed the box back"+ );+ }+ fragment_tree::FragmentGeometry {+ cells: handed_back.laid_out.cells,+ line_data: handed_back.line_data,}}