Truly independent web browser
LibWeb: Track percentage block size dependencies on the run
Aliaksandr Kalenik2e34f1e80de2
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rsindex aba35510d6c..191997725d2 100644--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs@@ -1887,6 +1887,10 @@ impl<'pass> BlockFormattingContext<'pass> {let block_container_inline_size = self.used(block_container).content_inline_size.get();let used = self.create_used_values(node, input.containing_block_constraints);+ let _percentage_block_size_dependency_tracking = self+ .run+ .records+ .track_percentage_block_size_dependency_while_laying_out(node);used.is_invisible_for_line_clamp.set(self.laying_out_invisible_line_clamp_content.get());if facts.is_anonymous_button_content_wrapper() {@@ -2590,8 +2594,6 @@ impl<'pass> BlockFormattingContext<'pass> {}let flow_position = self.flow_position();*self.margin_state.borrow_mut() = entry.outgoing_margins.clone();- used.depends_on_percentage_block_size- .set(entry.depends_on_percentage_block_size);formatting_context::propagate_percentage_block_size_dependency_to_containing_block(self.run.records,&self.run.callbacks,@@ -3673,9 +3675,9 @@ impl<'pass> BlockFormattingContext<'pass> {let outputs =formatting_context::run_fragmentainer(&self.run, builds_fragments, input, fragmentainer.clone());if outputs.result.depends_on_percentage_block_size {- self.used(self.run.box_)- .has_descendant_that_depends_on_percentage_block_size- .set(true);+ self.run+ .records+ .note_descendant_depends_on_percentage_block_size(self.run.box_);}let mut outcome = fragmentainer.take_outcome();outcome.minimal_space_shortage = match (diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex cdb13ff5a36..7cf533bac55 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -2611,13 +2611,13 @@ pub(crate) fn propagate_percentage_block_size_dependency_to_containing_block(return;}let containing_block = callbacks.in_flow_containing_block(child);- let containing_block_record_or_run_root_that_forwarded_the_basis = (!containing_block.is_invalid())- .then(|| records.used_values_if_owned(containing_block))- .flatten()- .unwrap_or_else(|| records.used_values(records.root()));- containing_block_record_or_run_root_that_forwarded_the_basis- .has_descendant_that_depends_on_percentage_block_size- .set(true);+ let containing_block_or_run_root_that_forwarded_the_basis =+ if !containing_block.is_invalid() && records.used_values_if_owned(containing_block).is_some() {+ containing_block+ } else {+ records.root()+ };+ records.note_descendant_depends_on_percentage_block_size(containing_block_or_run_root_that_forwarded_the_basis);}pub(crate) fn layout_inside_child(@@ -2954,12 +2954,14 @@ fn own_block_size_observation_of_item_run(input: &LayoutInput,result: &ChildLayoutResult,) -> OwnBlockSizeObservation {- let root_used = run.records.used_values(run.box_);let item_is_plain_block_container_laid_out_without_its_own_block_size = fc_type == FormattingContextType::Block&& !run.callbacks.frame.is_transposed()&& parent_owns_root_input_cells(&run.callbacks, run.box_, input)&& !result.depends_on_percentage_block_size- && !root_used.has_descendant_that_depends_on_percentage_block_size.get()+ && !run+ .records+ .percentage_block_size_dependency_of(run.box_)+ .has_descendant_that_depends_on_percentage_block_size&& !block_formatting_context::root_lays_out_content_against_its_own_block_size(&run.sizing(), run.box_)&& !root_content_includes_an_orthogonal_flow_sized_by_the_root_block_size(&run.callbacks, run.box_);if item_is_plain_block_container_laid_out_without_its_own_block_size {diff --git a/Libraries/LibWeb/Rust/src/layout/run_records.rs b/Libraries/LibWeb/Rust/src/layout/run_records.rsindex 7b18045bb62..dc7b197ab42 100644--- a/Libraries/LibWeb/Rust/src/layout/run_records.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_records.rs@@ -300,6 +300,38 @@ pub(crate) struct RunRecords<'arena> {// out the table box. Keeping it on the run prevents measurement state from escaping a pass.table_inline_layouts: RefCell<HashMap<Node, table_formatting_context::TableInlineLayout>>,content_sizing: ContentSizing,+ /// What the run found so far about the percentage block size dependency of its root and of the boxes whose+ /// layout in it is in progress, the innermost last. Only these boxes have their dependency resolved.+ percentage_block_size_dependencies_in_progress: PercentageBlockSizeDependenciesInProgress,+}++/// Whether a box depends on the block size its percentages resolve against, and whether a box it contains does.+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]+pub(crate) struct PercentageBlockSizeDependency {+ pub(crate) depends_on_percentage_block_size: bool,+ pub(crate) has_descendant_that_depends_on_percentage_block_size: bool,+}++type PercentageBlockSizeDependenciesInProgress =+ RefCell<smallvec::SmallVec<[(Node, PercentageBlockSizeDependency); 8]>>;++/// Keeps tracking the percentage block size dependency of a box while its layout is in progress.+pub(crate) struct PercentageBlockSizeDependencyTracking<'records> {+ dependencies_in_progress: &'records PercentageBlockSizeDependenciesInProgress,+ node: Node,+}++impl Drop for PercentageBlockSizeDependencyTracking<'_> {+ fn drop(&mut self) {+ let finished = self.dependencies_in_progress.borrow_mut().pop();+ if !std::thread::panicking() {+ assert_eq!(+ finished.map(|(node, _)| node),+ Some(self.node),+ "the layouts of boxes in progress finished out of order"+ );+ }+ }}#[derive(Clone, Copy, PartialEq, Eq)]@@ -399,6 +431,10 @@ impl<'arena> RunRecords<'arena> {records_outside_table: RefCell::new(HashMap::default()),table_inline_layouts: RefCell::new(HashMap::default()),content_sizing,+ percentage_block_size_dependencies_in_progress: RefCell::new(smallvec::smallvec![(+ root,+ PercentageBlockSizeDependency::default()+ )]),};run(&records, callbacks.for_run(root, root_containing_block))}@@ -505,6 +541,66 @@ impl<'arena> RunRecords<'arena> {self.root}+ /// Tracks the percentage block size dependency of `node` until the returned tracking is dropped, which the layout+ /// of `node` holds while it is in progress.+ pub(crate) fn track_percentage_block_size_dependency_while_laying_out(+ &self,+ node: Node,+ ) -> PercentageBlockSizeDependencyTracking<'_> {+ self.percentage_block_size_dependencies_in_progress+ .borrow_mut()+ .push((node, PercentageBlockSizeDependency::default()));+ PercentageBlockSizeDependencyTracking {+ dependencies_in_progress: &self.percentage_block_size_dependencies_in_progress,+ node,+ }+ }++ /// The percentage block size dependency found so far for the run's root or a box whose layout is in progress.+ #[track_caller]+ pub(crate) fn percentage_block_size_dependency_of(&self, node: Node) -> PercentageBlockSizeDependency {+ self.percentage_block_size_dependencies_in_progress+ .borrow()+ .iter()+ .rev()+ .find(|(tracked, _)| *tracked == node)+ .map(|(_, dependency)| *dependency)+ .expect("only the run root and boxes whose layout is in progress have their dependency resolved")+ }++ /// Notes that `node` depends on the block size its percentages resolve against. Nothing resolves the dependency of+ /// a box that is neither the run root nor in progress, so the note is dropped for such a box.+ pub(crate) fn note_box_depends_on_percentage_block_size(&self, node: Node) {+ self.update_percentage_block_size_dependency_of(node, |dependency| {+ dependency.depends_on_percentage_block_size = true;+ });+ }++ /// Notes that a box `containing_block` contains depends on the block size its percentages resolve against.+ /// Nothing resolves the dependency of a box that is neither the run root nor in progress, so the note is dropped for+ /// such a box.+ pub(crate) fn note_descendant_depends_on_percentage_block_size(&self, containing_block: Node) {+ self.update_percentage_block_size_dependency_of(containing_block, |dependency| {+ dependency.has_descendant_that_depends_on_percentage_block_size = true;+ });+ }++ fn update_percentage_block_size_dependency_of(+ &self,+ node: Node,+ update: impl FnOnce(&mut PercentageBlockSizeDependency),+ ) {+ if let Some((_, dependency)) = self+ .percentage_block_size_dependencies_in_progress+ .borrow_mut()+ .iter_mut()+ .rev()+ .find(|(tracked, _)| *tracked == node)+ {+ update(dependency);+ }+ }+pub(crate) fn store_table_inline_layout(&self, table: Node, layout: table_formatting_context::TableInlineLayout) {self.table_inline_layouts.borrow_mut().insert(table, layout);}diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rsindex ed6f9747c08..15e67c6b2bc 100644--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs@@ -1227,13 +1227,14 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {}pub(crate) fn resolve_percentage_block_size_dependency(&self, node: Node) -> bool {- let used = self.used(node);- let depends_on_percentage_block_size = used.depends_on_percentage_block_size.get()+ let found = self.records.percentage_block_size_dependency_of(node);+ let depends_on_percentage_block_size = found.depends_on_percentage_block_size|| self.own_style_depends_on_percentage_block_size(node)- || (used.has_descendant_that_depends_on_percentage_block_size.get()+ || (found.has_descendant_that_depends_on_percentage_block_size&& self.passes_percentage_block_size_through_to_children(node));- used.depends_on_percentage_block_size- .set(depends_on_percentage_block_size);+ if depends_on_percentage_block_size {+ self.records.note_box_depends_on_percentage_block_size(node);+ }depends_on_percentage_block_size}@@ -1296,9 +1297,7 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {if !depends_on_percentage_block_size {return;}- if let Some(record) = self.records.used_values_if_owned(node) {- record.depends_on_percentage_block_size.set(true);- }+ self.records.note_box_depends_on_percentage_block_size(node);formatting_context::propagate_percentage_block_size_dependency_to_containing_block(self.records,&self.callbacks,@@ -2234,7 +2233,9 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {border_end: root_used.border_right.get(),margin_end: root_used.margin_right.get(),},- root_used.depends_on_percentage_block_size.get()+ records+ .percentage_block_size_dependency_of(node)+ .depends_on_percentage_block_size|| sizing.skipped_child_depends_on_percentage_block_size(node),)},diff --git a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rsindex edad592137d..85a4361642b 100644--- a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs@@ -1449,15 +1449,14 @@ impl<'pass> TableFormattingContext<'pass> {.map(|participant| (participant, create_row_used_values)).chain(self.cells.iter().map(|cell| (cell.box_, true)));let sizing = self.sizing();- let table_record = self.used_values(self.run.box_);for (participant, create_used_values) in participants {if create_used_values {self.create_used_values(participant, self.participant_constraints);}if sizing.own_style_depends_on_percentage_block_size(participant) {- table_record- .has_descendant_that_depends_on_percentage_block_size- .set(true);+ self.run+ .records+ .note_descendant_depends_on_percentage_block_size(self.run.box_);}}for cell in &self.cells {diff --git a/Libraries/LibWeb/Rust/src/layout/used_values.rs b/Libraries/LibWeb/Rust/src/layout/used_values.rsindex 3dc6fa50c85..d89f1d39bf4 100644--- a/Libraries/LibWeb/Rust/src/layout/used_values.rs+++ b/Libraries/LibWeb/Rust/src/layout/used_values.rs@@ -385,10 +385,6 @@ pub(crate) struct UsedValues {pub last_baseline: Cell<CssPixels>,pub inline_alignment_baseline: Cell<Option<CssPixels>>,- // Layout outputs, deliberately outside the cell state.- pub depends_on_percentage_block_size: Cell<bool>,- pub has_descendant_that_depends_on_percentage_block_size: Cell<bool>,-pub(crate) line_data: RefCell<LineDataState>,pub(crate) rare_data: LazyRefCell<UsedValuesRareData>,/// The anchor-size() resolutions of an absolutely positioned box, set once as the abspos engine places it.@@ -439,8 +435,6 @@ impl Default for UsedValues {has_last_baseline: Cell::new(false),last_baseline: Cell::new(zero),inline_alignment_baseline: Cell::new(None),- depends_on_percentage_block_size: Cell::new(false),- has_descendant_that_depends_on_percentage_block_size: Cell::new(false),line_data: RefCell::new(LineDataState::Empty),rare_data: LazyRefCell::new(),anchor_resolved_values: std::cell::OnceCell::new(),