Truly independent web browser
LibWeb: Lay out the boxes of a block flow in cells of the call
Aliaksandr Kalenikc1e01bfd6f8c
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rsindex a24774118b8..f7c1f170139 100644--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs@@ -155,7 +155,7 @@ struct LaidOutBlockLevelBox {broken_inside: Option<BrokenInside>,has_unbreakable_lines: bool,collapses_through: bool,- /// The box as its layout left it: as a run of its own handed it back, or as this flow laid it out in a record its+ /// The box as its layout left it: as a run of its own handed it back, or as this flow laid it out in cells its/// layout owns.laid_out_box: run_records::LaidOutChildBox,/// What this flow decided the box's fragment shows of it.@@ -187,7 +187,7 @@ impl LaidOutBlockLevelBox {}}-/// A box a block formatting context sizes, in what the call laying the box out owns: the record the context lays the+/// A box a block formatting context sizes, in cells the call laying the box out owns: the cells the context lays the/// box out in, or the cells it sizes a child in ahead of the child's layout.#[derive(Clone, Copy)]pub(crate) struct ChildSizedHere<'sized> {@@ -197,16 +197,16 @@ pub(crate) struct ChildSizedHere<'sized> {#[derive(Clone, Copy)]enum SizedIn<'sized> {- Record(&'sized UsedValues),+ LaidOutHere(&'sized Cell<used_values::RootInputCells>),CellsAheadOfItsRun(&'sized Cell<used_values::RootInputCells>),}impl<'sized> ChildSizedHere<'sized> {- /// `node`, laid out in `record`, which its layout owns.- pub(crate) fn laid_out_in(node: Node, record: &'sized UsedValues) -> Self {+ /// `node`, laid out in `cells`, which its layout owns.+ pub(crate) fn laid_out_in(node: Node, cells: &'sized Cell<used_values::RootInputCells>) -> Self {Self {node,- sized_in: SizedIn::Record(record),+ sized_in: SizedIn::LaidOutHere(cells),}}@@ -218,10 +218,10 @@ impl<'sized> ChildSizedHere<'sized> {}}- /// The record the box is laid out in, if the context lays it out itself.- fn record(self) -> Option<&'sized UsedValues> {+ /// The cells the box is laid out in, if the context lays it out itself.+ fn cells_laid_out_here(self) -> Option<&'sized Cell<used_values::RootInputCells>> {match self.sized_in {- SizedIn::Record(record) => Some(record),+ SizedIn::LaidOutHere(cells) => Some(cells),SizedIn::CellsAheadOfItsRun(_) => None,}}@@ -824,34 +824,34 @@ impl<'pass> BlockFormattingContext<'pass> {/// The sizes, size constraints and box sides of `child`, a box this block formatting context sizes.fn metrics_of(&self, child: ChildSizedHere<'_>) -> used_values::RootInputCells {match child.sized_in {- SizedIn::Record(record) => record.cells.get(),- SizedIn::CellsAheadOfItsRun(cells) => cells.get(),+ SizedIn::LaidOutHere(cells) | SizedIn::CellsAheadOfItsRun(cells) => cells.get(),}}fn update_cells_of(&self, child: ChildSizedHere<'_>, update: impl FnOnce(&mut used_values::RootInputCells)) {- let mut cells = self.metrics_of(child);- update(&mut cells);match child.sized_in {- SizedIn::Record(record) => record.cells.set(cells),- SizedIn::CellsAheadOfItsRun(cells_ahead) => cells_ahead.set(cells),+ SizedIn::LaidOutHere(cells) | SizedIn::CellsAheadOfItsRun(cells) => cells.update_cells(update),}}- /// The record `child` is laid out in, a box this block formatting context lays out in a record.+ /// The cells `child` is laid out in, a box this block formatting context lays out itself.#[track_caller]- fn record_of<'record>(&self, child: ChildSizedHere<'record>) -> &'record UsedValues {+ fn record_of<'record>(&self, child: ChildSizedHere<'record>) -> &'record Cell<used_values::RootInputCells> {child- .record()+ .cells_laid_out_here().unwrap_or_else(|| panic!("{:?} is sized ahead of its run, not laid out here", child.node))}- /// `child`, a box this block formatting context lays out in a record, as a box its sizing steps size there.+ /// `child`, a box this block formatting context lays out itself, as a box its sizing steps size there.#[track_caller]fn sized_record_of<'record>(&self, child: ChildSizedHere<'record>) -> sizing_context::SizedRecord<'record> {sizing_context::SizedRecord::new(child.node,- used_values::CellsInFrame::new(self.record_of(child), self.run.callbacks.frame),+ used_values::CellsInFrame::of_cells(+ self.record_of(child),+ self.run.callbacks.frame,+ self.run.callbacks.frame,+ ),)}@@ -2360,28 +2360,24 @@ impl<'pass> BlockFormattingContext<'pass> {// NOTE: It is possible to encounter SVGMaskBox and SVGClipBox nodes while doing layout of the// formatting context established by a <foreignObject> that references them. Skip them- // before creating any used values; SVGFormattingContext lays them out on behalf of the- // referencing element.+ // before sizing them; SVGFormattingContext lays them out on behalf of the referencing+ // element.if facts.is_svg_mask_box() || facts.is_svg_clip_box() {return None;}let block_container_inline_size = block_container.cells.content_inline_size;- // NB: A child with a formatting context of its own is sized in cells of this block formatting context, which its- // run takes. A box without one is laid out in a record of this call.- let cells_of_child_ahead_of_its_run;- let record_of_box_without_formatting_context_of_its_own;+ // NB: A child with a formatting context of its own is sized in these cells ahead of its run, which takes them.+ // A box without one is laid out in them.+ let cells_of_child = Cell::new(used_values::cells_before_sizing(+ &self.run.callbacks,+ node,+ input.containing_block_constraints,+ ));let sized_child = if formatting_context::formatting_context_type_created_by_box(facts).is_some() {- cells_of_child_ahead_of_its_run = Cell::new(used_values::cells_before_sizing(- &self.run.callbacks,- node,- input.containing_block_constraints,- ));- ChildSizedHere::ahead_of_its_run(node, &cells_of_child_ahead_of_its_run)+ ChildSizedHere::ahead_of_its_run(node, &cells_of_child)} else {- record_of_box_without_formatting_context_of_its_own =- used_values::create_used_values(&self.run.callbacks, node, input.containing_block_constraints);- ChildSizedHere::laid_out_in(node, &record_of_box_without_formatting_context_of_its_own)+ ChildSizedHere::laid_out_in(node, &cells_of_child)};let _percentage_block_size_dependency_tracking = self.run@@ -2712,7 +2708,7 @@ impl<'pass> BlockFormattingContext<'pass> {let mut baselines_of_contents_laid_out_in_this_flow = None;let mut summary_of_children_laid_out_in_this_flow = None;let mut fragment_arrival_of_child_with_own_formatting_context = None;- // NB: A child its own run laid out is placed as that run handed it back, and any other with its record here.+ // NB: A child its own run laid out is placed as that run handed it back, and any other with its cells here.let mut child_handed_back_by_its_run = None;let mut laid_out_box_handed_back_by_its_run = None;if has_independent_formatting_context {@@ -3022,8 +3018,8 @@ impl<'pass> BlockFormattingContext<'pass> {.and_then(|summary| summary.lines.take());let placed_child = child_handed_back_by_its_run.unwrap_or_else(|| {- formatting_context::PlacedChild::of_record(- self.record_of(sized_child),+ formatting_context::PlacedChild::laid_out_here(+ self.metrics_of(sized_child),lines_laid_out_in_this_flow,&self.run,)@@ -3066,7 +3062,7 @@ impl<'pass> BlockFormattingContext<'pass> {let collapses_through = !has_independent_formatting_context&& self.margins_collapse_through(sized_child, summary_of_children_laid_out_in_this_flow.as_ref());- // NB: A child its own run laid out has the metrics that run handed back, and any other those of its record here.+ // NB: A child its own run laid out has the metrics that run handed back, and any other those of its cells here.let metrics = match &laid_out_box_handed_back_by_its_run {Some(handed_back) => handed_back.cells,None => self.metrics_of(sized_child),@@ -3125,15 +3121,16 @@ impl<'pass> BlockFormattingContext<'pass> {})}- /// The box `child` as this flow laid it out in a record its layout owns, with the `baselines` of its contents.+ /// The box `child` as this flow laid it out in cells its layout owns, with the `baselines` of its contents.fn laid_out_box_in_record_its_layout_owns(child: ChildSizedHere<'_>,baselines: DerivedBaselines,) -> run_records::LaidOutChildBox {- run_records::LaidOutChildBox::sized_in_parent_run(+ run_records::LaidOutChildBox::sized_here(child- .record()- .expect("a box laid out in this flow is laid out in a record its layout owns"),+ .cells_laid_out_here()+ .expect("a box laid out in this flow is laid out in cells its layout owns")+ .get(),baselines,)}@@ -3232,8 +3229,7 @@ impl<'pass> BlockFormattingContext<'pass> {) -> (FfiCssPixelPoint, CssPixels) {let node = sized_child.node;self.run.callbacks.arena().layout_trace.reused_in_flow_subtree(node);- let used = self.record_of(sized_child);- used.cells.set(entry.cells);+ self.record_of(sized_child).set(entry.cells);let flow_position = self.flow_position();*self.margin_state.borrow_mut() = entry.outgoing_margins.clone();formatting_context::propagate_percentage_block_size_dependency_to_containing_block(@@ -3251,7 +3247,7 @@ impl<'pass> BlockFormattingContext<'pass> {&self.run,node,formatting_context::PlacedChild::new(fragment_tree::FragmentGeometry {- cells: used_values::CellsInFrame::new(used, self.run.callbacks.frame).cells(),+ cells: self.metrics_of(sized_child),line_data: None,}).with_in_flow_box_insets(insets),@@ -3306,7 +3302,7 @@ impl<'pass> BlockFormattingContext<'pass> {let entry = in_flow_block_cache::InFlowBlockEntry {key,validity,- cells: used.cells(),+ cells: self.metrics_of(sized_child),baselines,offset_from_flow_position: FfiCssPixelPoint {x: content_offset.x,diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex 2a3911a2a60..e623c1cbf39 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -378,15 +378,16 @@ impl PlacedChild {})}- /// A box that `run` lays out in `record` with `lines`, before its parent decided anything about it.- pub(crate) fn of_record(- record: &UsedValues,+ /// A box that `run` lays out in `cells`, which hold it in the run's frame, with `lines`, before its parent decided+ /// anything about it.+ pub(crate) fn laid_out_here(+ cells: used_values::RootInputCells,lines: Option<used_values::LineData>,run: &FormattingContextRun,) -> Self {let callbacks = &run.callbacks;Self::new(fragment_tree::FragmentGeometry {- cells: used_values::CellsInFrame::new(record, callbacks.frame).cells(),+ cells,// NB: Only a run that builds fragments shows the lines it laid out in the boxes it places.line_data: run.fragments@@ -396,8 +397,8 @@ impl PlacedChild {std::sync::Arc::new(inline_content::InlineContent::finish(lines,callbacks.arena(),- record.content_inline_size(),- record.frame.writing_mode(),+ cells.content_inline_size,+ callbacks.frame.writing_mode(),))})})@@ -405,6 +406,15 @@ impl PlacedChild {})}+ /// A box without lines that `run` lays out in `record`, before its parent decided anything about it.+ pub(crate) fn of_record(record: &UsedValues, run: &FormattingContextRun) -> Self {+ Self::laid_out_here(+ used_values::CellsInFrame::new(record, run.callbacks.frame).cells(),+ None,+ run,+ )+ }+/// The child with the insets its parent resolved for it as an in-flow box.pub(crate) fn with_in_flow_box_insets(mut self, insets: abspos_engine::InFlowBoxInsets) -> Self {if let Some(used_insets) = insets.used_insets {@@ -1942,18 +1952,18 @@ fn finalize_float_root();}-/// Sizes `child` in `record` without a run of its own. Returns the used insets of an absolutely positioned child.+/// Sizes `child` in `cells` without a run of its own. Returns the used insets of an absolutely positioned child.fn size_skipped_independent_root(run: &FormattingContextRun,parent_block: Option<&block_formatting_context::BlockFormattingContext>,child: Node,- record: &UsedValues,+ cells: &Cell<used_values::RootInputCells>,input: &LayoutInput,) -> Option<run_records::UsedInsets> {match input.participation {ParticipationInParentFormattingContext::Float { .. } => {let parent = parent_block.expect("a float requires an enclosing block formatting context");- let sized_float = block_formatting_context::ChildSizedHere::laid_out_in(child, record);+ let sized_float = block_formatting_context::ChildSizedHere::laid_out_in(child, cells);parent.resolve_float_root_block_size_in_parent(sized_float, input);parent.resolve_used_block_size_if_treated_as_auto(sized_float,@@ -1971,7 +1981,10 @@ fn size_skipped_independent_root(});let used_insets = Cell::new(run_records::UsedInsets::default());let sized_child = abspos_engine::OutOfFlowBox::new(- sizing_context::SizedRecord::new(child, used_values::CellsInFrame::new(record, run.callbacks.frame)),+ sizing_context::SizedRecord::new(+ child,+ used_values::CellsInFrame::of_cells(cells, run.callbacks.frame, run.callbacks.frame),+ ),&used_insets,anchor_resolved_values.as_ref(),);@@ -2900,11 +2913,11 @@ pub(crate) struct ChildLayout {}impl ChildLayout {- fn of_child_sized_in_parent_run(outcome: ChildLayoutOutcome, record: &UsedValues) -> Self {+ fn of_child_sized_in_parent_run(outcome: ChildLayoutOutcome, cells: used_values::RootInputCells) -> Self {Self {outcome,fragment_arrival: None,- laid_out_box: run_records::LaidOutChildBox::sized_in_parent_run(record, DerivedBaselines::default()),+ laid_out_box: run_records::LaidOutChildBox::sized_here(cells, DerivedBaselines::default()),}}}@@ -2948,24 +2961,24 @@ pub(crate) fn layout_inside_child_with_input_cells({// OPTIMIZATION: An empty atomic block has no formatting-context body output. Size it in the// parent run, which will also construct its fragment when placing the box.- let record_of_this_layout = child_input_cells.materialize_record(run.callbacks.frame);+ let cells_of_this_layout = Cell::new(child_input_cells);let committed_fragment = run.sizing().dimension_empty_atomic_root(sizing_context::SizedRecord::new(child,- used_values::CellsInFrame::new(&record_of_this_layout, run.callbacks.frame),+ used_values::CellsInFrame::of_cells(&cells_of_this_layout, run.callbacks.frame, run.callbacks.frame),),input.available_space,input.containing_block_constraints,child_run_builds_fragments,);// OPTIMIZATION: A box that took its metrics from its committed fragment is placed with that fragment, where- // it shows the box as the record holds it.+ // it shows the box as its cells hold it.let fragment_arrival = committed_fragment.filter(|fragment| {run.fragments.is_some()&& fragment.is_plain_box()&& fragment_tree::FragmentGeometry {- cells: record_of_this_layout.cells(),+ cells: cells_of_this_layout.get(),line_data: None,}.shows_box_of_fragment(fragment)@@ -2976,7 +2989,7 @@ pub(crate) fn layout_inside_child_with_input_cells(reuses_subtree: false,});let laid_out_box =- run_records::LaidOutChildBox::sized_in_parent_run(&record_of_this_layout, DerivedBaselines::default());+ run_records::LaidOutChildBox::sized_here(cells_of_this_layout.get(), DerivedBaselines::default());return ChildLayout {outcome: skipped(None),fragment_arrival,@@ -2988,12 +3001,12 @@ pub(crate) fn layout_inside_child_with_input_cells(Some(FormattingContextType::InternalReplaced | FormattingContextType::ReplacedWithChildren));if !facts.can_have_children() && !creates_replaced_context {- let record_of_this_layout = child_input_cells.materialize_record(run.callbacks.frame);+ let cells_of_this_layout = Cell::new(child_input_cells);let out_of_flow_root_used_insets =- size_skipped_independent_root(run, parent_block, child, &record_of_this_layout, &input);+ size_skipped_independent_root(run, parent_block, child, &cells_of_this_layout, &input);return ChildLayout::of_child_sized_in_parent_run(skipped(out_of_flow_root_used_insets),- &record_of_this_layout,+ cells_of_this_layout.get(),);}diff --git a/Libraries/LibWeb/Rust/src/layout/mod.rs b/Libraries/LibWeb/Rust/src/layout/mod.rsindex bf295c9069d..fbca0d8f948 100644--- a/Libraries/LibWeb/Rust/src/layout/mod.rs+++ b/Libraries/LibWeb/Rust/src/layout/mod.rs@@ -111,4 +111,6 @@ pub(crate) use style_values::StyleValues;pub(crate) use tree_builder::build_keeps_box;pub(crate) use update_layout::MainThreadFfiEntry as UpdateMainThreadFfiEntry;pub(crate) use update_layout::{ClockRound, ClockRoundDeclined, FlownRound, SealedRound};-pub(crate) use used_values::{BoxMetricCells, FfiCssPixelPoint, FfiCssPixelRect, FfiCssPixelSize, UsedValues};+pub(crate) use used_values::{+ BoxMetricCells, BoxMetricCellsMut, FfiCssPixelPoint, FfiCssPixelRect, FfiCssPixelSize, UsedValues,+};diff --git a/Libraries/LibWeb/Rust/src/layout/run_records.rs b/Libraries/LibWeb/Rust/src/layout/run_records.rsindex 8f5d38ed624..57bce5967f7 100644--- a/Libraries/LibWeb/Rust/src/layout/run_records.rs+++ b/Libraries/LibWeb/Rust/src/layout/run_records.rs@@ -300,11 +300,11 @@ pub(crate) struct LaidOutChildBox {}impl LaidOutChildBox {- /// A child as the run of its parent left it in `record`, with the `baselines` of its contents, where that run+ /// A child as the run of its parent left it in `cells`, with the `baselines` of its contents, where that run/// sizes the child itself instead of handing it to a run of its own.- pub(crate) fn sized_in_parent_run(record: &UsedValues, baselines: DerivedBaselines) -> Self {+ pub(crate) fn sized_here(cells: used_values::RootInputCells, baselines: DerivedBaselines) -> Self {Self {- cells: record.cells(),+ cells,baselines,// NB: A box that establishes an orthogonal flow is laid out by a run of its own.orthogonal_flow_baselines: None,diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rsindex 02c5443a491..180677b663d 100644--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs@@ -1879,7 +1879,7 @@ impl<'pass> SvgFormattingContext<'pass> {/// Places `node`, a box this context lays out in `record`, whose metrics are final once the box is placed, and/// returns what the parent of the box reads of it.fn place_child(&self, node: Node, record: &UsedValues, x: CssPixels, y: CssPixels) -> LaidOutSvgElement {- let placed_child = formatting_context::PlacedChild::of_record(record, None, &self.run);+ let placed_child = formatting_context::PlacedChild::of_record(record, &self.run);self.place_laid_out_child(node, placed_child, x, y, None);LaidOutSvgElement {cells: record.cells(),@@ -2246,7 +2246,7 @@ impl<'pass> SvgFormattingContext<'pass> {used.update_cells(|cells| cells.has_definite_block_size = true);self.place_laid_out_child(shape,- formatting_context::PlacedChild::of_record(used, None, &self.run),+ formatting_context::PlacedChild::of_record(used, &self.run),committed_offset.x,committed_offset.y,Some(fragment_tree::ChildFragmentArrival::CommittedFragment {diff --git a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rsindex 85f99dcd9c2..178e3d2fdfb 100644--- a/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/table_formatting_context.rs@@ -1271,7 +1271,7 @@ impl<'pass> TableFormattingContext<'pass> {formatting_context::place_child(&self.run,node,- formatting_context::PlacedChild::of_record(record, None, &self.run).with_fragment_facts(fragment_facts),+ formatting_context::PlacedChild::of_record(record, &self.run).with_fragment_facts(fragment_facts),content_offset,None,None,diff --git a/Libraries/LibWeb/Rust/src/layout/used_values.rs b/Libraries/LibWeb/Rust/src/layout/used_values.rsindex 5897165b28c..0a9c8755e5f 100644--- a/Libraries/LibWeb/Rust/src/layout/used_values.rs+++ b/Libraries/LibWeb/Rust/src/layout/used_values.rs@@ -467,6 +467,35 @@ impl BoxMetricCells for RootInputCells {}}+impl BoxMetricCells for Cell<RootInputCells> {+ #[inline]+ fn cells(&self) -> RootInputCells {+ self.get()+ }+}++/// Writes the metrics a cell holds. Not `update`: the inherent `Cell::update` would shadow it.+pub(crate) trait BoxMetricCellsMut {+ /// Writes the metrics, which `write` sees and changes as the cell holds them.+ fn update_cells(&self, write: impl FnOnce(&mut RootInputCells));++ fn set_content_inline_size(&self, value: CssPixels) {+ self.update_cells(|cells| cells.set_content_inline_size(value));+ }++ fn set_content_block_size(&self, value: CssPixels) {+ self.update_cells(|cells| cells.set_content_block_size(value));+ }+}++impl BoxMetricCellsMut for Cell<RootInputCells> {+ fn update_cells(&self, write: impl FnOnce(&mut RootInputCells)) {+ let mut cells = self.get();+ write(&mut cells);+ self.set(cells);+ }+}+/// Cells held in frame `held`, read and written in frame `reader`.#[derive(Clone, Copy)]pub(crate) struct CellsInFrame<'a> {