Auto merge of #29827 - mrobinson:fragment-tree-directory, r=atbrakhi

Layout 2020: Move all Fragment code to the `fragment_tree` directory

This is a simple code organization change with no behavior change with the idea of making Layout 2020 easier to understand by new folks to the project. The idea is that we will have a cleaner separation between the different parts of layout ie one directory for the fragment tree and one (currently multiple) directory for the box tree.

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes do not require tests because they do not change behavior.

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
This commit is contained in:
bors-servo 2023-06-04 20:06:31 +02:00 committed by GitHub
commit 6cf6b53b68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 592 additions and 558 deletions

View file

@ -10,7 +10,7 @@ use crate::context::LayoutContext;
use crate::dom::NodeExt;
use crate::dom_traversal::{Contents, NodeAndStyleInfo};
use crate::formatting_contexts::IndependentFormattingContext;
use crate::fragments::{
use crate::fragment_tree::{
BoxFragment, CollapsedBlockMargins, CollapsedMargin, FloatFragment, Fragment,
};
use crate::geom::flow_relative::{Rect, Vec2};

View file

@ -7,9 +7,9 @@ use crate::context::LayoutContext;
use crate::flow::float::{FloatBox, SequentialLayoutState};
use crate::flow::FlowLayout;
use crate::formatting_contexts::IndependentFormattingContext;
use crate::fragment_tree::BaseFragmentInfo;
use crate::fragments::{
AnonymousFragment, BoxFragment, CollapsedBlockMargins, FontMetrics, Fragment, TextFragment,
use crate::fragment_tree::{
AnonymousFragment, BaseFragmentInfo, BoxFragment, CollapsedBlockMargins, FontMetrics, Fragment,
TextFragment,
};
use crate::geom::flow_relative::{Rect, Sides, Vec2};
use crate::positioned::{

View file

@ -13,8 +13,9 @@ use crate::flow::inline::InlineFormattingContext;
use crate::formatting_contexts::{
IndependentFormattingContext, IndependentLayout, NonReplacedFormattingContext,
};
use crate::fragment_tree::BaseFragmentInfo;
use crate::fragments::{BoxFragment, CollapsedBlockMargins, CollapsedMargin, Fragment};
use crate::fragment_tree::{
BaseFragmentInfo, BoxFragment, CollapsedBlockMargins, CollapsedMargin, Fragment,
};
use crate::geom::flow_relative::{Rect, Sides, Vec2};
use crate::positioned::{AbsolutelyPositionedBox, PositioningContext};
use crate::replaced::ReplacedContent;
@ -34,7 +35,7 @@ pub mod float;
pub mod inline;
mod root;
pub use root::{BoxTree, FragmentTree};
pub use root::{BoxTree, CanvasBackground};
#[derive(Debug, Serialize)]
pub(crate) struct BlockFormattingContext {

View file

@ -4,7 +4,6 @@
use crate::cell::ArcRefCell;
use crate::context::LayoutContext;
use crate::display_list::StackingContext;
use crate::dom::{LayoutBox, NodeExt};
use crate::dom_traversal::{iter_child_nodes, Contents, NodeAndStyleInfo};
use crate::flexbox::FlexLevelBox;
@ -13,8 +12,7 @@ use crate::flow::float::FloatBox;
use crate::flow::inline::InlineLevelBox;
use crate::flow::{BlockContainer, BlockFormattingContext, BlockLevelBox};
use crate::formatting_contexts::IndependentFormattingContext;
use crate::fragment_tree::{ContainingBlockManager, Tag};
use crate::fragments::Fragment;
use crate::fragment_tree::FragmentTree;
use crate::geom::flow_relative::Vec2;
use crate::geom::{PhysicalPoint, PhysicalRect, PhysicalSize};
use crate::positioned::AbsolutelyPositionedBox;
@ -23,15 +21,10 @@ use crate::replaced::ReplacedContent;
use crate::style_ext::ComputedValuesExt;
use crate::style_ext::{Display, DisplayGeneratingBox, DisplayInside};
use crate::DefiniteContainingBlock;
use app_units::Au;
use atomic_refcell::AtomicRef;
use euclid::default::{Point2D, Rect, Size2D};
use fxhash::FxHashSet;
use gfx_traits::print_tree::PrintTree;
use script_layout_interface::wrapper_traits::LayoutNode;
use script_layout_interface::{LayoutElementType, LayoutNodeType};
use servo_arc::Arc;
use style::animation::AnimationSetKey;
use style::dom::OpaqueNode;
use style::properties::ComputedValues;
use style::values::computed::Length;
@ -47,30 +40,6 @@ pub struct BoxTree {
canvas_background: CanvasBackground,
}
#[derive(Serialize)]
pub struct FragmentTree {
/// Fragments at the top-level of the tree.
///
/// If the root element has `display: none`, there are zero fragments.
/// Otherwise, there is at least one:
///
/// * The first fragment is generated by the root element.
/// * There may be additional fragments generated by positioned boxes
/// that have the initial containing block.
pub(crate) root_fragments: Vec<ArcRefCell<Fragment>>,
/// The scrollable overflow rectangle for the entire tree
/// https://drafts.csswg.org/css-overflow/#scrollable
pub(crate) scrollable_overflow: PhysicalRect<Length>,
/// The containing block used in the layout of this fragment tree.
pub(crate) initial_containing_block: PhysicalRect<Length>,
/// https://drafts.csswg.org/css-backgrounds/#special-backgrounds
#[serde(skip)]
pub(crate) canvas_background: CanvasBackground,
}
impl BoxTree {
pub fn construct<'dom, Node>(context: &LayoutContext, root_element: Node) -> Self
where
@ -380,173 +349,9 @@ impl BoxTree {
}
}
impl FragmentTree {
pub(crate) fn build_display_list(
&self,
builder: &mut crate::display_list::DisplayListBuilder,
root_stacking_context: &StackingContext,
) {
// Paint the canvas background (if any) before/under everything else
root_stacking_context.build_canvas_background_display_list(
builder,
self,
&self.initial_containing_block,
);
root_stacking_context.build_display_list(builder);
}
pub fn print(&self) {
let mut print_tree = PrintTree::new("Fragment Tree".to_string());
for fragment in &self.root_fragments {
fragment.borrow().print(&mut print_tree);
}
}
pub fn scrollable_overflow(&self) -> webrender_api::units::LayoutSize {
webrender_api::units::LayoutSize::from_untyped(Size2D::new(
self.scrollable_overflow.size.width.px(),
self.scrollable_overflow.size.height.px(),
))
}
pub(crate) fn find<T>(
&self,
mut process_func: impl FnMut(&Fragment, usize, &PhysicalRect<Length>) -> Option<T>,
) -> Option<T> {
let info = ContainingBlockManager {
for_non_absolute_descendants: &self.initial_containing_block,
for_absolute_descendants: None,
for_absolute_and_fixed_descendants: &self.initial_containing_block,
};
self.root_fragments
.iter()
.find_map(|child| child.borrow().find(&info, 0, &mut process_func))
}
pub fn remove_nodes_in_fragment_tree_from_set(&self, set: &mut FxHashSet<AnimationSetKey>) {
self.find(|fragment, _, _| {
let tag = fragment.tag()?;
set.remove(&AnimationSetKey::new(tag.node, tag.pseudo));
None::<()>
});
}
pub fn get_content_box_for_node(&self, requested_node: OpaqueNode) -> Rect<Au> {
let mut bounding_box = PhysicalRect::zero();
let tag_to_find = Tag::new(requested_node);
self.find(|fragment, _, containing_block| {
if fragment.tag() != Some(tag_to_find) {
return None::<()>;
}
let fragment_relative_rect = match fragment {
Fragment::Box(fragment) | Fragment::Float(fragment) => fragment
.border_rect()
.to_physical(fragment.style.writing_mode, &containing_block),
Fragment::Text(fragment) => fragment
.rect
.to_physical(fragment.parent_style.writing_mode, &containing_block),
Fragment::AbsoluteOrFixedPositioned(_) |
Fragment::Image(_) |
Fragment::IFrame(_) |
Fragment::Anonymous(_) => return None,
};
bounding_box = fragment_relative_rect
.translate(containing_block.origin.to_vector())
.union(&bounding_box);
None::<()>
});
Rect::new(
Point2D::new(
Au::from_f32_px(bounding_box.origin.x.px()),
Au::from_f32_px(bounding_box.origin.y.px()),
),
Size2D::new(
Au::from_f32_px(bounding_box.size.width.px()),
Au::from_f32_px(bounding_box.size.height.px()),
),
)
}
pub fn get_border_dimensions_for_node(&self, requested_node: OpaqueNode) -> Rect<i32> {
let tag_to_find = Tag::new(requested_node);
self.find(|fragment, _, containing_block| {
if fragment.tag() != Some(tag_to_find) {
return None;
}
let (style, padding_rect) = match fragment {
Fragment::Box(fragment) => (&fragment.style, fragment.padding_rect()),
_ => return None,
};
// https://drafts.csswg.org/cssom-view/#dom-element-clienttop
// " If the element has no associated CSS layout box or if the
// CSS layout box is inline, return zero." For this check we
// also explicitly ignore the list item portion of the display
// style.
let display = &style.get_box().display;
if display.inside() == style::values::specified::box_::DisplayInside::Flow &&
display.outside() == style::values::specified::box_::DisplayOutside::Inline
{
return Some(Rect::zero());
}
let padding_rect = padding_rect.to_physical(style.writing_mode, &containing_block);
let border = style.get_border();
Some(Rect::new(
Point2D::new(
border.border_left_width.px() as i32,
border.border_top_width.px() as i32,
),
Size2D::new(
padding_rect.size.width.px() as i32,
padding_rect.size.height.px() as i32,
),
))
})
.unwrap_or_else(Rect::zero)
}
pub fn get_scroll_area_for_node(&self, requested_node: OpaqueNode) -> Rect<i32> {
let mut scroll_area: PhysicalRect<Length> = PhysicalRect::zero();
let tag_to_find = Tag::new(requested_node);
self.find(|fragment, _, containing_block| {
if fragment.tag() != Some(tag_to_find) {
return None::<()>;
}
scroll_area = match fragment {
Fragment::Box(fragment) | Fragment::Float(fragment) => fragment
.scrollable_overflow(&containing_block)
.translate(containing_block.origin.to_vector()),
Fragment::Text(_) |
Fragment::AbsoluteOrFixedPositioned(_) |
Fragment::Image(_) |
Fragment::IFrame(_) |
Fragment::Anonymous(_) => return None,
};
None::<()>
});
Rect::new(
Point2D::new(
scroll_area.origin.x.px() as i32,
scroll_area.origin.y.px() as i32,
),
Size2D::new(
scroll_area.size.width.px() as i32,
scroll_area.size.height.px() as i32,
),
)
}
}
/// https://drafts.csswg.org/css-backgrounds/#root-background
#[derive(Clone, Serialize)]
pub(crate) struct CanvasBackground {
pub struct CanvasBackground {
/// DOM node for the root element
pub root_element: OpaqueNode,