Simplify layout of absolutes with static insets

Absolutes with static insets need to be laid out at their ancestor
containing blocks, but their position is dependent on their parent's
layout. The static layout position is passed up the tree during hoisting
and ancestors each add their own offset to the position until it is
relative to the containing block that contains the absolute.

This is currently done with a closure and a fairly tricky "tree rank"
numbering system that needs to be threaded through the entire layout.
This change replaces that system.

Every time a child is laid out we create a positioning context to hold
any absolute children (this can be optimized away at a later time). At
each of these moments, we call a method to aggregate offsets to the
static insets of hoisted absolutes. This makes the logic easier to
follow and will also allow implementing this behavior for inline-blocks,
which was impossible with the old system.
This commit is contained in:
Martin Robinson 2023-06-19 18:04:05 +02:00
parent 836ae5fa48
commit 459a7d26aa
No known key found for this signature in database
GPG key ID: D56AA4FA55EFE6F8
7 changed files with 174 additions and 235 deletions

View file

@ -55,7 +55,6 @@ struct FlexContext<'a> {
/// A flex item with some intermediate results /// A flex item with some intermediate results
struct FlexItem<'a> { struct FlexItem<'a> {
box_: &'a mut IndependentFormattingContext, box_: &'a mut IndependentFormattingContext,
tree_rank: usize,
content_box_size: FlexRelativeVec2<LengthOrAuto>, content_box_size: FlexRelativeVec2<LengthOrAuto>,
content_min_size: FlexRelativeVec2<Length>, content_min_size: FlexRelativeVec2<Length>,
content_max_size: FlexRelativeVec2<Option<Length>>, content_max_size: FlexRelativeVec2<Option<Length>>,
@ -92,7 +91,7 @@ struct FlexItemLayoutResult {
/// Return type of `FlexLine::layout` /// Return type of `FlexLine::layout`
struct FlexLineLayoutResult { struct FlexLineLayoutResult {
cross_size: Length, cross_size: Length,
item_fragments: Vec<BoxFragment>, // One per flex item, in the given order item_fragments: Vec<(BoxFragment, PositioningContext)>, // One per flex item, in the given order
} }
impl FlexContext<'_> { impl FlexContext<'_> {
@ -150,7 +149,6 @@ impl FlexContainer {
layout_context: &LayoutContext, layout_context: &LayoutContext,
positioning_context: &mut PositioningContext, positioning_context: &mut PositioningContext,
containing_block: &ContainingBlock, containing_block: &ContainingBlock,
tree_rank: usize,
) -> IndependentLayout { ) -> IndependentLayout {
// Actual length may be less, but we guess that usually not by a lot // Actual length may be less, but we guess that usually not by a lot
let mut flex_items = Vec::with_capacity(self.children.len()); let mut flex_items = Vec::with_capacity(self.children.len());
@ -161,8 +159,7 @@ impl FlexContainer {
let original_order_with_absolutely_positioned = self let original_order_with_absolutely_positioned = self
.children .children
.iter() .iter()
.enumerate() .map(|arcrefcell| {
.map(|(tree_rank, arcrefcell)| {
let borrowed = arcrefcell.borrow_mut(); let borrowed = arcrefcell.borrow_mut();
match &*borrowed { match &*borrowed {
FlexLevelBox::OutOfFlowAbsolutelyPositionedBox(absolutely_positioned) => { FlexLevelBox::OutOfFlowAbsolutelyPositionedBox(absolutely_positioned) => {
@ -173,56 +170,54 @@ impl FlexContainer {
FlexLevelBox::FlexItem(item) => item, FlexLevelBox::FlexItem(item) => item,
_ => unreachable!(), _ => unreachable!(),
}); });
flex_items.push((tree_rank, item)); flex_items.push(item);
Err(()) Err(())
}, },
} }
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut content_block_size_option_dance = None; let (mut flex_item_fragments, content_block_size) = layout(
let fragments = layout_context,
positioning_context.adjust_static_positions(tree_rank, |positioning_context| { positioning_context,
let (mut flex_item_fragments, content_block_size) = layout( containing_block,
layout_context, flex_items.iter_mut().map(|child| &mut **child),
positioning_context, );
containing_block,
flex_items let fragments = original_order_with_absolutely_positioned
.iter_mut() .into_iter()
.map(|(tree_rank, child)| (*tree_rank, &mut **child)), .map(|child_as_abspos| match child_as_abspos {
); Err(()) => {
content_block_size_option_dance = Some(content_block_size); // The `()` here is a place-holder for a flex item.
let fragments = original_order_with_absolutely_positioned // The `flex_item_fragments` iterator yields one fragment
.into_iter() // per flex item, in the original order.
.enumerate() let (fragment, mut child_positioning_context) =
.map(|(tree_rank, child_as_abspos)| match child_as_abspos { flex_item_fragments.next().unwrap();
Err(()) => { let fragment = Fragment::Box(fragment);
// The `()` here is a place-holder for a flex item. child_positioning_context
// The `flex_item_fragments` iterator yields one fragment .adjust_static_position_of_hoisted_fragments(&fragment);
// per flex item, in the original order. positioning_context.append(child_positioning_context);
Fragment::Box(flex_item_fragments.next().unwrap()) fragment
}, },
Ok(absolutely_positioned) => { Ok(absolutely_positioned) => {
let hoisted_box = AbsolutelyPositionedBox::to_hoisted( let hoisted_box = AbsolutelyPositionedBox::to_hoisted(
absolutely_positioned, absolutely_positioned,
Vec2::zero(), Vec2::zero(),
tree_rank, containing_block,
containing_block, );
); let hoisted_fragment = hoisted_box.fragment.clone();
let hoisted_fragment = hoisted_box.fragment.clone(); positioning_context.push(hoisted_box);
positioning_context.push(hoisted_box); Fragment::AbsoluteOrFixedPositioned(hoisted_fragment)
Fragment::AbsoluteOrFixedPositioned(hoisted_fragment) },
}, })
}) .collect::<Vec<_>>();
.collect::<Vec<_>>();
// There should be no more flex items // There should be no more flex items
assert!(flex_item_fragments.next().is_none()); assert!(flex_item_fragments.next().is_none());
fragments
});
IndependentLayout { IndependentLayout {
fragments, fragments,
content_block_size: content_block_size_option_dance.unwrap(), content_block_size,
} }
} }
} }
@ -232,8 +227,11 @@ fn layout<'context, 'boxes>(
layout_context: &LayoutContext, layout_context: &LayoutContext,
positioning_context: &mut PositioningContext, positioning_context: &mut PositioningContext,
containing_block: &ContainingBlock, containing_block: &ContainingBlock,
flex_item_boxes: impl Iterator<Item = (usize, &'boxes mut IndependentFormattingContext)>, flex_item_boxes: impl Iterator<Item = &'boxes mut IndependentFormattingContext>,
) -> (impl Iterator<Item = BoxFragment>, Length) { ) -> (
impl Iterator<Item = (BoxFragment, PositioningContext)>,
Length,
) {
// FIXME: get actual min/max cross size for the flex container. // FIXME: get actual min/max cross size for the flex container.
// We have access to style for the flex container in `containing_block.style`, // We have access to style for the flex container in `containing_block.style`,
// but resolving percentages there requires access // but resolving percentages there requires access
@ -290,7 +288,7 @@ fn layout<'context, 'boxes>(
}; };
let mut flex_items = flex_item_boxes let mut flex_items = flex_item_boxes
.map(|(tree_rank, box_)| FlexItem::new(&flex_context, box_, tree_rank)) .map(|box_| FlexItem::new(&flex_context, box_))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// “Determine the main size of the flex container” // “Determine the main size of the flex container”
@ -387,7 +385,7 @@ fn layout<'context, 'boxes>(
container_main_size container_main_size
}, },
}; };
let fragments = flex_lines let fragments_and_positioning_contexts = flex_lines
.into_iter() .into_iter()
.zip(line_cross_start_positions) .zip(line_cross_start_positions)
.flat_map(move |(mut line, line_cross_start_position)| { .flat_map(move |(mut line, line_cross_start_position)| {
@ -409,21 +407,17 @@ fn layout<'context, 'boxes>(
inline: container_cross_size - line_cross_start_position - line.cross_size, inline: container_cross_size - line_cross_start_position - line.cross_size,
}, },
}; };
for fragment in &mut line.item_fragments { for (fragment, _) in &mut line.item_fragments {
fragment.content_rect.start_corner += &flow_relative_line_position fragment.content_rect.start_corner += &flow_relative_line_position
} }
line.item_fragments line.item_fragments
}) })
.into_iter(); .into_iter();
(fragments, content_block_size) (fragments_and_positioning_contexts, content_block_size)
} }
impl<'a> FlexItem<'a> { impl<'a> FlexItem<'a> {
fn new( fn new(flex_context: &FlexContext, box_: &'a mut IndependentFormattingContext) -> Self {
flex_context: &FlexContext,
box_: &'a mut IndependentFormattingContext,
tree_rank: usize,
) -> Self {
let containing_block = flex_context.containing_block; let containing_block = flex_context.containing_block;
let box_style = box_.style(); let box_style = box_.style();
@ -543,7 +537,6 @@ impl<'a> FlexItem<'a> {
Self { Self {
box_, box_,
tree_rank,
content_box_size, content_box_size,
content_min_size, content_min_size,
content_max_size, content_max_size,
@ -730,7 +723,7 @@ impl FlexLine<'_> {
// Determine the used cross size of each flex item // Determine the used cross size of each flex item
// https://drafts.csswg.org/css-flexbox/#algo-stretch // https://drafts.csswg.org/css-flexbox/#algo-stretch
let (item_used_cross_sizes, item_fragments): (Vec<_>, Vec<_>) = self let (item_used_cross_sizes, item_results): (Vec<_>, Vec<_>) = self
.items .items
.iter_mut() .iter_mut()
.zip(item_layout_results) .zip(item_layout_results)
@ -754,10 +747,7 @@ impl FlexLine<'_> {
// so that percentage-sized children can be resolved.” // so that percentage-sized children can be resolved.”
item_result = item.layout(used_main_size, flex_context, Some(cross_size)); item_result = item.layout(used_main_size, flex_context, Some(cross_size));
} }
flex_context (cross_size, item_result)
.positioning_context
.append(item_result.positioning_context);
(cross_size, item_result.fragments)
}) })
.unzip(); .unzip();
@ -838,7 +828,7 @@ impl FlexLine<'_> {
let item_fragments = self let item_fragments = self
.items .items
.iter() .iter()
.zip(item_fragments) .zip(item_results)
.zip( .zip(
item_used_main_sizes item_used_main_sizes
.iter() .iter()
@ -852,20 +842,23 @@ impl FlexLine<'_> {
.map(|(size, start_corner)| FlexRelativeRect { size, start_corner }), .map(|(size, start_corner)| FlexRelativeRect { size, start_corner }),
) )
.zip(&item_margins) .zip(&item_margins)
.map(|(((item, fragments), content_rect), margin)| { .map(|(((item, item_result), content_rect), margin)| {
let content_rect = flex_context.rect_to_flow_relative(line_size, content_rect); let content_rect = flex_context.rect_to_flow_relative(line_size, content_rect);
let margin = flex_context.sides_to_flow_relative(*margin); let margin = flex_context.sides_to_flow_relative(*margin);
let collapsed_margin = CollapsedBlockMargins::from_margin(&margin); let collapsed_margin = CollapsedBlockMargins::from_margin(&margin);
BoxFragment::new( (
item.box_.base_fragment_info(), BoxFragment::new(
item.box_.style().clone(), item.box_.base_fragment_info(),
fragments, item.box_.style().clone(),
content_rect, item_result.fragments,
flex_context.sides_to_flow_relative(item.padding), content_rect,
flex_context.sides_to_flow_relative(item.border), flex_context.sides_to_flow_relative(item.padding),
margin, flex_context.sides_to_flow_relative(item.border),
Length::zero(), margin,
collapsed_margin, Length::zero(),
collapsed_margin,
),
item_result.positioning_context,
) )
}) })
.collect(); .collect();
@ -1048,7 +1041,7 @@ impl<'a> FlexItem<'a> {
flex_context: &mut FlexContext, flex_context: &mut FlexContext,
used_cross_size_override: Option<Length>, used_cross_size_override: Option<Length>,
) -> FlexItemLayoutResult { ) -> FlexItemLayoutResult {
let mut positioning_context = PositioningContext::new_for_rayon( let mut positioning_context = PositioningContext::new_for_subtree(
flex_context flex_context
.positioning_context .positioning_context
.collects_for_nearest_positioned_ancestor(), .collects_for_nearest_positioned_ancestor(),
@ -1109,7 +1102,6 @@ impl<'a> FlexItem<'a> {
flex_context.layout_context, flex_context.layout_context,
&mut positioning_context, &mut positioning_context,
&item_as_containing_block, &item_as_containing_block,
self.tree_rank,
); );
let hypothetical_cross_size = self let hypothetical_cross_size = self

View file

@ -739,7 +739,6 @@ impl FloatBox {
layout_context, layout_context,
&mut positioning_context, &mut positioning_context,
&containing_block_for_children, &containing_block_for_children,
0,
); );
content_size = Vec2 { content_size = Vec2 {
inline: inline_size, inline: inline_size,

View file

@ -271,7 +271,6 @@ impl InlineFormattingContext {
layout_context: &LayoutContext, layout_context: &LayoutContext,
positioning_context: &mut PositioningContext, positioning_context: &mut PositioningContext,
containing_block: &ContainingBlock, containing_block: &ContainingBlock,
tree_rank: usize,
sequential_layout_state: Option<&mut SequentialLayoutState>, sequential_layout_state: Option<&mut SequentialLayoutState>,
) -> FlowLayout { ) -> FlowLayout {
let mut ifc = InlineFormattingContextState { let mut ifc = InlineFormattingContextState {
@ -342,7 +341,6 @@ impl InlineFormattingContext {
let hoisted_box = AbsolutelyPositionedBox::to_hoisted( let hoisted_box = AbsolutelyPositionedBox::to_hoisted(
box_.clone(), box_.clone(),
initial_start_corner, initial_start_corner,
tree_rank,
ifc.containing_block, ifc.containing_block,
); );
let hoisted_fragment = hoisted_box.fragment.clone(); let hoisted_fragment = hoisted_box.fragment.clone();
@ -661,14 +659,12 @@ fn layout_atomic(
containing_block_for_children.style.writing_mode, containing_block_for_children.style.writing_mode,
"Mixed writing modes are not supported yet" "Mixed writing modes are not supported yet"
); );
// FIXME is this correct? // FIXME: Do we need to adjust the static position of the hoisted fragments in the positioning
let dummy_tree_rank = 0; // context somewhere near here?
// FIXME: Do we need to call `adjust_static_positions` somewhere near here?
let independent_layout = non_replaced.layout( let independent_layout = non_replaced.layout(
layout_context, layout_context,
ifc.positioning_context, ifc.positioning_context,
&containing_block_for_children, &containing_block_for_children,
dummy_tree_rank,
); );
// https://drafts.csswg.org/css2/visudet.html#block-root-margin // https://drafts.csswg.org/css2/visudet.html#block-root-margin

View file

@ -22,8 +22,7 @@ use crate::replaced::ReplacedContent;
use crate::sizing::{self, ContentSizes}; use crate::sizing::{self, ContentSizes};
use crate::style_ext::{ComputedValuesExt, PaddingBorderMargin}; use crate::style_ext::{ComputedValuesExt, PaddingBorderMargin};
use crate::ContainingBlock; use crate::ContainingBlock;
use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator}; use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use rayon_croissant::ParallelIteratorExt;
use servo_arc::Arc; use servo_arc::Arc;
use style::computed_values::clear::T as Clear; use style::computed_values::clear::T as Clear;
use style::computed_values::float::T as Float; use style::computed_values::float::T as Float;
@ -79,7 +78,6 @@ impl BlockFormattingContext {
layout_context: &LayoutContext, layout_context: &LayoutContext,
positioning_context: &mut PositioningContext, positioning_context: &mut PositioningContext,
containing_block: &ContainingBlock, containing_block: &ContainingBlock,
tree_rank: usize,
) -> IndependentLayout { ) -> IndependentLayout {
let mut sequential_layout_state = if self.contains_floats || !layout_context.use_rayon { let mut sequential_layout_state = if self.contains_floats || !layout_context.use_rayon {
Some(SequentialLayoutState::new()) Some(SequentialLayoutState::new())
@ -91,7 +89,6 @@ impl BlockFormattingContext {
layout_context, layout_context,
positioning_context, positioning_context,
containing_block, containing_block,
tree_rank,
sequential_layout_state.as_mut(), sequential_layout_state.as_mut(),
CollapsibleWithParentStartMargin(false), CollapsibleWithParentStartMargin(false),
); );
@ -208,7 +205,6 @@ impl BlockContainer {
layout_context: &LayoutContext, layout_context: &LayoutContext,
positioning_context: &mut PositioningContext, positioning_context: &mut PositioningContext,
containing_block: &ContainingBlock, containing_block: &ContainingBlock,
tree_rank: usize,
sequential_layout_state: Option<&mut SequentialLayoutState>, sequential_layout_state: Option<&mut SequentialLayoutState>,
collapsible_with_parent_start_margin: CollapsibleWithParentStartMargin, collapsible_with_parent_start_margin: CollapsibleWithParentStartMargin,
) -> FlowLayout { ) -> FlowLayout {
@ -218,7 +214,6 @@ impl BlockContainer {
positioning_context, positioning_context,
child_boxes, child_boxes,
containing_block, containing_block,
tree_rank,
sequential_layout_state, sequential_layout_state,
collapsible_with_parent_start_margin, collapsible_with_parent_start_margin,
), ),
@ -226,7 +221,6 @@ impl BlockContainer {
layout_context, layout_context,
positioning_context, positioning_context,
containing_block, containing_block,
tree_rank,
sequential_layout_state, sequential_layout_state,
), ),
} }
@ -255,7 +249,6 @@ fn layout_block_level_children(
positioning_context: &mut PositioningContext, positioning_context: &mut PositioningContext,
child_boxes: &[ArcRefCell<BlockLevelBox>], child_boxes: &[ArcRefCell<BlockLevelBox>],
containing_block: &ContainingBlock, containing_block: &ContainingBlock,
tree_rank: usize,
mut sequential_layout_state: Option<&mut SequentialLayoutState>, mut sequential_layout_state: Option<&mut SequentialLayoutState>,
collapsible_with_parent_start_margin: CollapsibleWithParentStartMargin, collapsible_with_parent_start_margin: CollapsibleWithParentStartMargin,
) -> FlowLayout { ) -> FlowLayout {
@ -265,7 +258,6 @@ fn layout_block_level_children(
positioning_context, positioning_context,
child_boxes, child_boxes,
containing_block, containing_block,
tree_rank,
sequential_layout_state, sequential_layout_state,
collapsible_with_parent_start_margin, collapsible_with_parent_start_margin,
), ),
@ -274,7 +266,6 @@ fn layout_block_level_children(
positioning_context, positioning_context,
child_boxes, child_boxes,
containing_block, containing_block,
tree_rank,
collapsible_with_parent_start_margin, collapsible_with_parent_start_margin,
), ),
} }
@ -285,37 +276,35 @@ fn layout_block_level_children_in_parallel(
positioning_context: &mut PositioningContext, positioning_context: &mut PositioningContext,
child_boxes: &[ArcRefCell<BlockLevelBox>], child_boxes: &[ArcRefCell<BlockLevelBox>],
containing_block: &ContainingBlock, containing_block: &ContainingBlock,
tree_rank: usize,
collapsible_with_parent_start_margin: CollapsibleWithParentStartMargin, collapsible_with_parent_start_margin: CollapsibleWithParentStartMargin,
) -> FlowLayout { ) -> FlowLayout {
let mut placement_state = PlacementState::new(collapsible_with_parent_start_margin); let collects_for_nearest_positioned_ancestor =
positioning_context.collects_for_nearest_positioned_ancestor();
let layout_results: Vec<(Fragment, PositioningContext)> = child_boxes
.par_iter()
.map(|child_box| {
let mut child_positioning_context =
PositioningContext::new_for_subtree(collects_for_nearest_positioned_ancestor);
let fragment = child_box.borrow_mut().layout(
layout_context,
&mut child_positioning_context,
containing_block,
/* sequential_layout_state = */ None,
);
(fragment, child_positioning_context)
})
.collect();
let fragments = positioning_context.adjust_static_positions(tree_rank, |positioning_context| { let mut placement_state = PlacementState::new(collapsible_with_parent_start_margin);
let collects_for_nearest_positioned_ancestor = let fragments = layout_results
positioning_context.collects_for_nearest_positioned_ancestor(); .into_iter()
let mut fragments: Vec<Fragment> = child_boxes .map(|(mut fragment, mut child_positioning_context)| {
.par_iter() placement_state.place_fragment(&mut fragment);
.enumerate() child_positioning_context.adjust_static_position_of_hoisted_fragments(&fragment);
.mapfold_reduce_into( positioning_context.append(child_positioning_context);
positioning_context, fragment
|positioning_context, (tree_rank, box_)| { })
box_.borrow_mut().layout( .collect();
layout_context,
positioning_context,
containing_block,
tree_rank,
/* sequential_layout_state = */ None,
)
},
|| PositioningContext::new_for_rayon(collects_for_nearest_positioned_ancestor),
PositioningContext::append,
)
.collect();
for fragment in fragments.iter_mut() {
placement_state.place_fragment(fragment);
}
fragments
});
FlowLayout { FlowLayout {
fragments, fragments,
@ -329,35 +318,36 @@ fn layout_block_level_children_sequentially(
positioning_context: &mut PositioningContext, positioning_context: &mut PositioningContext,
child_boxes: &[ArcRefCell<BlockLevelBox>], child_boxes: &[ArcRefCell<BlockLevelBox>],
containing_block: &ContainingBlock, containing_block: &ContainingBlock,
tree_rank: usize,
sequential_layout_state: &mut SequentialLayoutState, sequential_layout_state: &mut SequentialLayoutState,
collapsible_with_parent_start_margin: CollapsibleWithParentStartMargin, collapsible_with_parent_start_margin: CollapsibleWithParentStartMargin,
) -> FlowLayout { ) -> FlowLayout {
let mut placement_state = PlacementState::new(collapsible_with_parent_start_margin); let mut placement_state = PlacementState::new(collapsible_with_parent_start_margin);
let collects_for_nearest_positioned_ancestor =
positioning_context.collects_for_nearest_positioned_ancestor();
let fragments = positioning_context.adjust_static_positions(tree_rank, |positioning_context| { // Because floats are involved, we do layout for this block formatting context in tree
// Because floats are involved, we do layout for this block formatting context in tree // order without parallelism. This enables mutable access to a `SequentialLayoutState` that
// order without parallelism. This enables mutable access to a `SequentialLayoutState` that // tracks every float encountered so far (again in tree order).
// tracks every float encountered so far (again in tree order). let fragments = child_boxes
child_boxes .iter()
.iter() .map(|child_box| {
.enumerate() let mut child_positioning_context =
.map(|(tree_rank, child_box)| { PositioningContext::new_for_subtree(collects_for_nearest_positioned_ancestor);
let mut fragment = child_box.borrow_mut().layout( let mut fragment = child_box.borrow_mut().layout(
layout_context, layout_context,
positioning_context, &mut child_positioning_context,
containing_block, containing_block,
tree_rank, Some(&mut *sequential_layout_state),
Some(&mut *sequential_layout_state), );
);
placement_state.place_fragment(&mut fragment); placement_state.place_fragment(&mut fragment);
placement_state placement_state
.adjust_positions_of_float_children(&mut fragment, sequential_layout_state); .adjust_positions_of_float_children(&mut fragment, sequential_layout_state);
fragment child_positioning_context.adjust_static_position_of_hoisted_fragments(&fragment);
}) positioning_context.append(child_positioning_context);
.collect() fragment
}); })
.collect();
FlowLayout { FlowLayout {
fragments, fragments,
@ -372,7 +362,6 @@ impl BlockLevelBox {
layout_context: &LayoutContext, layout_context: &LayoutContext,
positioning_context: &mut PositioningContext, positioning_context: &mut PositioningContext,
containing_block: &ContainingBlock, containing_block: &ContainingBlock,
tree_rank: usize,
sequential_layout_state: Option<&mut SequentialLayoutState>, sequential_layout_state: Option<&mut SequentialLayoutState>,
) -> Fragment { ) -> Fragment {
match self { match self {
@ -392,7 +381,6 @@ impl BlockLevelBox {
*tag, *tag,
style, style,
NonReplacedContents::SameFormattingContextBlock(contents), NonReplacedContents::SameFormattingContextBlock(contents),
tree_rank,
sequential_layout_state, sequential_layout_state,
) )
}, },
@ -429,7 +417,6 @@ impl BlockLevelBox {
NonReplacedContents::EstablishesAnIndependentFormattingContext( NonReplacedContents::EstablishesAnIndependentFormattingContext(
non_replaced, non_replaced,
), ),
tree_rank,
sequential_layout_state, sequential_layout_state,
) )
}, },
@ -443,7 +430,6 @@ impl BlockLevelBox {
// correct positioning until later, in place_block_level_fragment, // correct positioning until later, in place_block_level_fragment,
// and this value will be adjusted there // and this value will be adjusted there
Vec2::zero(), Vec2::zero(),
tree_rank,
containing_block, containing_block,
); );
let hoisted_fragment = hoisted_box.fragment.clone(); let hoisted_fragment = hoisted_box.fragment.clone();
@ -494,7 +480,6 @@ fn layout_in_flow_non_replaced_block_level(
base_fragment_info: BaseFragmentInfo, base_fragment_info: BaseFragmentInfo,
style: &Arc<ComputedValues>, style: &Arc<ComputedValues>,
block_level_kind: NonReplacedContents, block_level_kind: NonReplacedContents,
tree_rank: usize,
mut sequential_layout_state: Option<&mut SequentialLayoutState>, mut sequential_layout_state: Option<&mut SequentialLayoutState>,
) -> BoxFragment { ) -> BoxFragment {
let pbm = style.padding_border_margin(containing_block); let pbm = style.padding_border_margin(containing_block);
@ -623,7 +608,6 @@ fn layout_in_flow_non_replaced_block_level(
layout_context, layout_context,
positioning_context, positioning_context,
&containing_block_for_children, &containing_block_for_children,
tree_rank,
sequential_layout_state.as_mut().map(|x| &mut **x), sequential_layout_state.as_mut().map(|x| &mut **x),
CollapsibleWithParentStartMargin(start_margin_can_collapse_with_children), CollapsibleWithParentStartMargin(start_margin_can_collapse_with_children),
); );
@ -665,7 +649,6 @@ fn layout_in_flow_non_replaced_block_level(
layout_context, layout_context,
positioning_context, positioning_context,
&containing_block_for_children, &containing_block_for_children,
tree_rank,
); );
fragments = independent_layout.fragments; fragments = independent_layout.fragments;
content_block_size = independent_layout.content_block_size; content_block_size = independent_layout.content_block_size;

View file

@ -289,14 +289,12 @@ impl BoxTree {
style, style,
}; };
let dummy_tree_rank = 0;
let mut positioning_context = let mut positioning_context =
PositioningContext::new_for_containing_block_for_all_descendants(); PositioningContext::new_for_containing_block_for_all_descendants();
let independent_layout = self.root.layout( let independent_layout = self.root.layout(
layout_context, layout_context,
&mut positioning_context, &mut positioning_context,
&(&initial_containing_block).into(), &(&initial_containing_block).into(),
dummy_tree_rank,
); );
let mut root_fragments = independent_layout let mut root_fragments = independent_layout

View file

@ -192,21 +192,14 @@ impl NonReplacedFormattingContext {
layout_context: &LayoutContext, layout_context: &LayoutContext,
positioning_context: &mut PositioningContext, positioning_context: &mut PositioningContext,
containing_block: &ContainingBlock, containing_block: &ContainingBlock,
tree_rank: usize,
) -> IndependentLayout { ) -> IndependentLayout {
match &self.contents { match &self.contents {
NonReplacedFormattingContextContents::Flow(bfc) => bfc.layout( NonReplacedFormattingContextContents::Flow(bfc) => {
layout_context, bfc.layout(layout_context, positioning_context, containing_block)
positioning_context, },
containing_block, NonReplacedFormattingContextContents::Flex(fc) => {
tree_rank, fc.layout(layout_context, positioning_context, containing_block)
), },
NonReplacedFormattingContextContents::Flex(fc) => fc.layout(
layout_context,
positioning_context,
containing_block,
tree_rank,
),
} }
} }

View file

@ -39,11 +39,6 @@ pub(crate) struct PositioningContext {
pub(crate) struct HoistedAbsolutelyPositionedBox { pub(crate) struct HoistedAbsolutelyPositionedBox {
absolutely_positioned_box: ArcRefCell<AbsolutelyPositionedBox>, absolutely_positioned_box: ArcRefCell<AbsolutelyPositionedBox>,
/// The rank of the child from which this absolutely positioned fragment
/// came from, when doing the layout of a block container. Used to compute
/// static positions when going up the tree.
pub(crate) tree_rank: usize,
/// A reference to a Fragment which is shared between this `HoistedAbsolutelyPositionedBox` /// A reference to a Fragment which is shared between this `HoistedAbsolutelyPositionedBox`
/// and its placeholder `AbsoluteOrFixedPositionedFragment` in the original tree position. /// and its placeholder `AbsoluteOrFixedPositionedFragment` in the original tree position.
/// This will be used later in order to paint this hoisted box in tree order. /// This will be used later in order to paint this hoisted box in tree order.
@ -72,7 +67,6 @@ impl AbsolutelyPositionedBox {
pub(crate) fn to_hoisted( pub(crate) fn to_hoisted(
self_: ArcRefCell<Self>, self_: ArcRefCell<Self>,
initial_start_corner: Vec2<Length>, initial_start_corner: Vec2<Length>,
tree_rank: usize,
containing_block: &ContainingBlock, containing_block: &ContainingBlock,
) -> HoistedAbsolutelyPositionedBox { ) -> HoistedAbsolutelyPositionedBox {
fn absolute_box_offsets( fn absolute_box_offsets(
@ -112,7 +106,6 @@ impl AbsolutelyPositionedBox {
} }
}; };
HoistedAbsolutelyPositionedBox { HoistedAbsolutelyPositionedBox {
tree_rank,
fragment: ArcRefCell::new(HoistedSharedFragment::new(box_offsets)), fragment: ArcRefCell::new(HoistedSharedFragment::new(box_offsets)),
absolutely_positioned_box: self_, absolutely_positioned_box: self_,
} }
@ -127,7 +120,11 @@ impl PositioningContext {
} }
} }
pub(crate) fn new_for_rayon(collects_for_nearest_positioned_ancestor: bool) -> Self { /// Create a [PositioninContext] to use for laying out a subtree. The idea is that
/// when subtree layout is finished, the newly hoisted boxes can be processed
/// (normally adjusting their static insets) and then appended to the parent
/// [PositioningContext].
pub(crate) fn new_for_subtree(collects_for_nearest_positioned_ancestor: bool) -> Self {
Self { Self {
for_nearest_positioned_ancestor: if collects_for_nearest_positioned_ancestor { for_nearest_positioned_ancestor: if collects_for_nearest_positioned_ancestor {
Some(Vec::new()) Some(Vec::new())
@ -155,6 +152,47 @@ impl PositioningContext {
} }
} }
/// Absolute and fixed position fragments are hoisted up to their containing blocks
/// from their tree position. When these fragments have static inset start positions,
/// that position (relative to the ancestor containing block) needs to be included
/// with the hoisted fragment so that it can be laid out properly at the containing
/// block.
///
/// This function is used to update that static position at every level of the
/// fragment tree as the hoisted fragments move back up to their containing blocks.
/// Once an ancestor fragment is laid out, this function can be used to aggregate its
/// offset on the way back up.
pub(crate) fn adjust_static_position_of_hoisted_fragments(
&mut self,
parent_fragment: &Fragment,
) {
let fragment_rect = match &parent_fragment {
Fragment::Box(b) => &b.content_rect,
Fragment::AbsoluteOrFixedPositioned(_) | Fragment::Float(_) => return,
Fragment::Anonymous(a) => &a.rect,
_ => unreachable!(),
};
let update_fragment_if_needed = |hoisted_fragment: &mut HoistedAbsolutelyPositionedBox| {
let mut fragment = hoisted_fragment.fragment.borrow_mut();
if let AbsoluteBoxOffsets::StaticStart { start } = &mut fragment.box_offsets.inline {
*start += fragment_rect.start_corner.inline;
}
if let AbsoluteBoxOffsets::StaticStart { start } = &mut fragment.box_offsets.block {
*start += fragment_rect.start_corner.block;
}
};
self.for_nearest_positioned_ancestor
.as_mut()
.map(|hoisted_boxes| {
hoisted_boxes.iter_mut().for_each(update_fragment_if_needed);
});
self.for_nearest_containing_block_for_all_descendants
.iter_mut()
.for_each(update_fragment_if_needed);
}
/// Given `fragment_layout_fn`, a closure which lays out a fragment in a provided /// Given `fragment_layout_fn`, a closure which lays out a fragment in a provided
/// `PositioningContext`, create a new positioning context if necessary for the fragment and /// `PositioningContext`, create a new positioning context if necessary for the fragment and
/// lay out the fragment and all its children. Returns the newly created `BoxFragment`. /// lay out the fragment and all its children. Returns the newly created `BoxFragment`.
@ -267,36 +305,6 @@ impl PositioningContext {
} }
} }
pub(crate) fn adjust_static_positions(
&mut self,
tree_rank_in_parent: usize,
f: impl FnOnce(&mut Self) -> Vec<Fragment>,
) -> Vec<Fragment> {
let for_containing_block_for_all_descendants =
self.for_nearest_containing_block_for_all_descendants.len();
let for_nearest_so_far = self
.for_nearest_positioned_ancestor
.as_ref()
.map(|v| v.len());
let fragments = f(self);
adjust_static_positions(
&mut self.for_nearest_containing_block_for_all_descendants
[for_containing_block_for_all_descendants..],
&fragments,
tree_rank_in_parent,
);
if let Some(nearest) = &mut self.for_nearest_positioned_ancestor {
adjust_static_positions(
&mut nearest[for_nearest_so_far.unwrap()..],
&fragments,
tree_rank_in_parent,
);
}
fragments
}
pub(crate) fn layout_initial_containing_block_children( pub(crate) fn layout_initial_containing_block_children(
&mut self, &mut self,
layout_context: &LayoutContext, layout_context: &LayoutContext,
@ -512,7 +520,6 @@ impl HoistedAbsolutelyPositionedBox {
containing_block_for_children.style.writing_mode, containing_block_for_children.style.writing_mode,
"Mixed writing modes are not supported yet" "Mixed writing modes are not supported yet"
); );
let dummy_tree_rank = 0;
// Clear the context since we will lay out the same descendants // Clear the context since we will lay out the same descendants
// more than once. Otherwise, absolute descendants will create // more than once. Otherwise, absolute descendants will create
@ -524,7 +531,6 @@ impl HoistedAbsolutelyPositionedBox {
layout_context, layout_context,
&mut positioning_context, &mut positioning_context,
&containing_block_for_children, &containing_block_for_children,
dummy_tree_rank,
); );
let block_size = size.auto_is(|| independent_layout.content_block_size); let block_size = size.auto_is(|| independent_layout.content_block_size);
Result { Result {
@ -739,34 +745,6 @@ impl<'a> AbsoluteAxisSolver<'a> {
} }
} }
fn adjust_static_positions(
absolutely_positioned_fragments: &mut [HoistedAbsolutelyPositionedBox],
child_fragments: &[Fragment],
tree_rank_in_parent: usize,
) {
for abspos_fragment in absolutely_positioned_fragments {
let original_tree_rank = abspos_fragment.tree_rank;
abspos_fragment.tree_rank = tree_rank_in_parent;
let child_fragment_rect = match &child_fragments[original_tree_rank] {
Fragment::Box(b) => &b.content_rect,
Fragment::AbsoluteOrFixedPositioned(_) | Fragment::Float(_) => continue,
Fragment::Anonymous(a) => &a.rect,
_ => unreachable!(),
};
let mut shared_fragment = abspos_fragment.fragment.borrow_mut();
if let AbsoluteBoxOffsets::StaticStart { start } = &mut shared_fragment.box_offsets.inline {
*start += child_fragment_rect.start_corner.inline;
}
if let AbsoluteBoxOffsets::StaticStart { start } = &mut shared_fragment.box_offsets.block {
*start += child_fragment_rect.start_corner.block;
}
}
}
fn vec_append_owned<T>(a: &mut Vec<T>, mut b: Vec<T>) { fn vec_append_owned<T>(a: &mut Vec<T>, mut b: Vec<T>) {
if a.is_empty() { if a.is_empty() {
*a = b *a = b