mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
script: Split style and layout data in DOM nodes (#31985)
This change splits the style and layout data in DOM nodes that is populated by style and layout passes. This makes Servo's data design more like Gecko's. This allows: 1. Removing the various `StyleAndLayout` data structures used by layout. 2. Removing the `GetStyleAndLayoutData` and `GetStyleAndOpaqueLayoutData` traits. Accessing style and layout data are now just functions on the `LayoutNode` and `ThreadSafeLayoutNode` traits. 3. Styling now doesn't populate layout data. This is is postponed until layout itself. 4. Allows the DOM wrappers to no longer have to be generic over the layout data. This data was already stored using `std::any::Any` and the new code just makes layout responsible for downcasting. Cleaning up the generic type parameter in the DOM wrappers can happen in a followup change. The main benefit to all of this is that we should be able to remove unsafe creation of `ServoLayoutNode` in layout and `TrustedLayoutNodeAddress` entirely, because `ServoLayoutNode` will be able to be passed directly from script to layout. In addition, this removes one more abstraction layer from the layout DOM wrappers, making the code a lot more understandable. Note: This increases the measured size of DOM types, but the same data is stored. It's simply that before that data was stored behind a heap pointer.
This commit is contained in:
parent
1ed6b96684
commit
08ef158d4e
15 changed files with 230 additions and 293 deletions
|
@ -43,7 +43,7 @@ use style::LocalName;
|
|||
|
||||
use crate::block::BlockFlow;
|
||||
use crate::context::{with_thread_local_font_context, LayoutContext};
|
||||
use crate::data::{LayoutData, LayoutDataFlags};
|
||||
use crate::data::{InnerLayoutData, LayoutDataFlags};
|
||||
use crate::display_list::items::OpaqueNode;
|
||||
use crate::flex::FlexFlow;
|
||||
use crate::floats::FloatKind;
|
||||
|
@ -71,14 +71,15 @@ use crate::table_rowgroup::TableRowGroupFlow;
|
|||
use crate::table_wrapper::TableWrapperFlow;
|
||||
use crate::text::TextRunScanner;
|
||||
use crate::traversal::PostorderNodeMutTraversal;
|
||||
use crate::wrapper::{LayoutNodeLayoutData, TextContent, ThreadSafeLayoutNodeHelpers};
|
||||
use crate::wrapper::{TextContent, ThreadSafeLayoutNodeHelpers};
|
||||
use crate::{parallel, ServoArc};
|
||||
|
||||
/// The results of flow construction for a DOM node.
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Default)]
|
||||
pub enum ConstructionResult {
|
||||
/// This node contributes nothing at all (`display: none`). Alternately, this is what newly
|
||||
/// created nodes have their `ConstructionResult` set to.
|
||||
#[default]
|
||||
None,
|
||||
|
||||
/// This node contributed a flow at the proper position in the tree.
|
||||
|
@ -1992,7 +1993,7 @@ trait NodeUtils {
|
|||
/// Returns true if this node doesn't render its kids and false otherwise.
|
||||
fn is_replaced_content(&self) -> bool;
|
||||
|
||||
fn construction_result_mut(self, layout_data: &mut LayoutData) -> &mut ConstructionResult;
|
||||
fn construction_result_mut(self, layout_data: &mut InnerLayoutData) -> &mut ConstructionResult;
|
||||
|
||||
/// Sets the construction result of a flow.
|
||||
fn set_flow_construction_result(self, result: ConstructionResult);
|
||||
|
@ -2029,7 +2030,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn construction_result_mut(self, data: &mut LayoutData) -> &mut ConstructionResult {
|
||||
fn construction_result_mut(self, data: &mut InnerLayoutData) -> &mut ConstructionResult {
|
||||
match self.get_pseudo_element_type() {
|
||||
PseudoElementType::Before => &mut data.before_flow_construction_result,
|
||||
PseudoElementType::After => &mut data.after_flow_construction_result,
|
||||
|
|
|
@ -5,20 +5,12 @@
|
|||
use atomic_refcell::AtomicRefCell;
|
||||
use bitflags::bitflags;
|
||||
use script_layout_interface::wrapper_traits::LayoutDataTrait;
|
||||
use script_layout_interface::StyleData;
|
||||
|
||||
use crate::construct::ConstructionResult;
|
||||
|
||||
pub struct StyleAndLayoutData<'dom> {
|
||||
/// The style data associated with a node.
|
||||
pub style_data: &'dom StyleData,
|
||||
/// The layout data associated with a node.
|
||||
pub layout_data: &'dom AtomicRefCell<LayoutData>,
|
||||
}
|
||||
|
||||
/// Data that layout associates with a node.
|
||||
#[derive(Clone)]
|
||||
pub struct LayoutData {
|
||||
#[derive(Clone, Default)]
|
||||
pub struct InnerLayoutData {
|
||||
/// The current results of flow construction for this node. This is either a
|
||||
/// flow or a `ConstructionItem`. See comments in `construct.rs` for more
|
||||
/// details.
|
||||
|
@ -29,31 +21,14 @@ pub struct LayoutData {
|
|||
pub after_flow_construction_result: ConstructionResult,
|
||||
|
||||
pub details_summary_flow_construction_result: ConstructionResult,
|
||||
|
||||
pub details_content_flow_construction_result: ConstructionResult,
|
||||
|
||||
/// Various flags.
|
||||
pub flags: LayoutDataFlags,
|
||||
}
|
||||
|
||||
impl LayoutDataTrait for LayoutData {}
|
||||
|
||||
impl Default for LayoutData {
|
||||
/// Creates new layout data.
|
||||
fn default() -> LayoutData {
|
||||
Self {
|
||||
flow_construction_result: ConstructionResult::None,
|
||||
before_flow_construction_result: ConstructionResult::None,
|
||||
after_flow_construction_result: ConstructionResult::None,
|
||||
details_summary_flow_construction_result: ConstructionResult::None,
|
||||
details_content_flow_construction_result: ConstructionResult::None,
|
||||
flags: LayoutDataFlags::empty(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
#[derive(Clone, Copy)]
|
||||
#[derive(Clone, Copy, Default)]
|
||||
pub struct LayoutDataFlags: u8 {
|
||||
/// Whether a flow has been newly constructed.
|
||||
const HAS_NEWLY_CONSTRUCTED_FLOW = 0x01;
|
||||
|
@ -61,3 +36,12 @@ bitflags! {
|
|||
const HAS_BEEN_TRAVERSED = 0x02;
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper for [`InnerLayoutData`]. This is necessary to give the entire data
|
||||
/// structure interior mutability, as we will need to mutate the layout data of
|
||||
/// non-mutable DOM nodes.
|
||||
#[derive(Clone, Default)]
|
||||
pub struct LayoutData(pub AtomicRefCell<InnerLayoutData>);
|
||||
|
||||
// The implementation of this trait allows the data to be stored in the DOM.
|
||||
impl LayoutDataTrait for LayoutData {}
|
||||
|
|
|
@ -40,7 +40,7 @@ use crate::flow::{Flow, GetBaseFlow};
|
|||
use crate::fragment::{Fragment, FragmentBorderBoxIterator, FragmentFlags, SpecificFragmentInfo};
|
||||
use crate::inline::InlineFragmentNodeFlags;
|
||||
use crate::sequential;
|
||||
use crate::wrapper::LayoutNodeLayoutData;
|
||||
use crate::wrapper::ThreadSafeLayoutNodeHelpers;
|
||||
|
||||
// https://drafts.csswg.org/cssom-view/#overflow-directions
|
||||
fn overflow_direction(writing_mode: &WritingMode) -> OverflowDirection {
|
||||
|
@ -851,7 +851,7 @@ fn process_resolved_style_request_internal<'dom>(
|
|||
where
|
||||
N: LayoutNode<'dom>,
|
||||
{
|
||||
let maybe_data = layout_el.borrow_layout_data();
|
||||
let maybe_data = layout_el.as_node().borrow_layout_data();
|
||||
let position = maybe_data.map_or(Point2D::zero(), |data| {
|
||||
match data.flow_construction_result {
|
||||
ConstructionResult::Flow(ref flow_ref, _) => flow_ref
|
||||
|
@ -1021,8 +1021,8 @@ fn inner_text_collection_steps<'dom>(
|
|||
_ => child,
|
||||
};
|
||||
|
||||
let element_data = match node.get_style_and_opaque_layout_data() {
|
||||
Some(data) => &data.style_data.element_data,
|
||||
let element_data = match node.style_data() {
|
||||
Some(data) => &data.element_data,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
|
|
|
@ -18,7 +18,8 @@ use crate::construct::FlowConstructor;
|
|||
use crate::context::LayoutContext;
|
||||
use crate::display_list::DisplayListBuildState;
|
||||
use crate::flow::{Flow, FlowFlags, GetBaseFlow, ImmutableFlowUtils};
|
||||
use crate::wrapper::{GetStyleAndLayoutData, LayoutNodeLayoutData, ThreadSafeLayoutNodeHelpers};
|
||||
use crate::wrapper::ThreadSafeLayoutNodeHelpers;
|
||||
use crate::LayoutData;
|
||||
|
||||
pub struct RecalcStyleAndConstructFlows<'a> {
|
||||
context: LayoutContext<'a>,
|
||||
|
@ -58,7 +59,7 @@ where
|
|||
{
|
||||
// FIXME(pcwalton): Stop allocating here. Ideally this should just be
|
||||
// done by the HTML parser.
|
||||
unsafe { node.initialize_data() };
|
||||
unsafe { node.initialize_style_and_layout_data::<LayoutData>() };
|
||||
|
||||
if !node.is_text_node() {
|
||||
let el = node.as_element().unwrap();
|
||||
|
@ -76,7 +77,7 @@ where
|
|||
// flow construction:
|
||||
// (1) They child doesn't yet have layout data (preorder traversal initializes it).
|
||||
// (2) The parent element has restyle damage (so the text flow also needs fixup).
|
||||
node.get_style_and_layout_data().is_none() || !parent_data.damage.is_empty()
|
||||
node.layout_data().is_none() || !parent_data.damage.is_empty()
|
||||
}
|
||||
|
||||
fn shared_context(&self) -> &SharedStyleContext {
|
||||
|
|
|
@ -32,59 +32,20 @@
|
|||
|
||||
use atomic_refcell::{AtomicRef, AtomicRefMut};
|
||||
use script_layout_interface::wrapper_traits::{
|
||||
GetStyleAndOpaqueLayoutData, ThreadSafeLayoutElement, ThreadSafeLayoutNode,
|
||||
LayoutNode, ThreadSafeLayoutElement, ThreadSafeLayoutNode,
|
||||
};
|
||||
use style::dom::{NodeInfo, TElement, TNode};
|
||||
use style::selector_parser::RestyleDamage;
|
||||
use style::values::computed::counters::ContentItem;
|
||||
use style::values::generics::counters::Content;
|
||||
|
||||
use crate::data::{LayoutData, LayoutDataFlags, StyleAndLayoutData};
|
||||
use crate::data::{InnerLayoutData, LayoutData, LayoutDataFlags};
|
||||
|
||||
pub trait LayoutNodeLayoutData<'dom> {
|
||||
fn borrow_layout_data(self) -> Option<AtomicRef<'dom, LayoutData>>;
|
||||
fn mutate_layout_data(self) -> Option<AtomicRefMut<'dom, LayoutData>>;
|
||||
pub trait ThreadSafeLayoutNodeHelpers<'dom> {
|
||||
fn borrow_layout_data(self) -> Option<AtomicRef<'dom, InnerLayoutData>>;
|
||||
fn mutate_layout_data(self) -> Option<AtomicRefMut<'dom, InnerLayoutData>>;
|
||||
fn flow_debug_id(self) -> usize;
|
||||
}
|
||||
|
||||
impl<'dom, T> LayoutNodeLayoutData<'dom> for T
|
||||
where
|
||||
T: GetStyleAndOpaqueLayoutData<'dom>,
|
||||
{
|
||||
fn borrow_layout_data(self) -> Option<AtomicRef<'dom, LayoutData>> {
|
||||
self.get_style_and_layout_data()
|
||||
.map(|d| d.layout_data.borrow())
|
||||
}
|
||||
|
||||
fn mutate_layout_data(self) -> Option<AtomicRefMut<'dom, LayoutData>> {
|
||||
self.get_style_and_layout_data()
|
||||
.map(|d| d.layout_data.borrow_mut())
|
||||
}
|
||||
|
||||
fn flow_debug_id(self) -> usize {
|
||||
self.borrow_layout_data()
|
||||
.map_or(0, |d| d.flow_construction_result.debug_id())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GetStyleAndLayoutData<'dom> {
|
||||
fn get_style_and_layout_data(self) -> Option<StyleAndLayoutData<'dom>>;
|
||||
}
|
||||
|
||||
impl<'dom, T> GetStyleAndLayoutData<'dom> for T
|
||||
where
|
||||
T: GetStyleAndOpaqueLayoutData<'dom>,
|
||||
{
|
||||
fn get_style_and_layout_data(self) -> Option<StyleAndLayoutData<'dom>> {
|
||||
self.get_style_and_opaque_layout_data()
|
||||
.map(|data| StyleAndLayoutData {
|
||||
style_data: &data.style_data,
|
||||
layout_data: data.generic_data.downcast_ref().unwrap(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ThreadSafeLayoutNodeHelpers {
|
||||
/// Returns the layout data flags for this node.
|
||||
fn flags(self) -> LayoutDataFlags;
|
||||
|
||||
|
@ -107,10 +68,26 @@ pub trait ThreadSafeLayoutNodeHelpers {
|
|||
fn restyle_damage(self) -> RestyleDamage;
|
||||
}
|
||||
|
||||
impl<'dom, T> ThreadSafeLayoutNodeHelpers for T
|
||||
impl<'dom, T> ThreadSafeLayoutNodeHelpers<'dom> for T
|
||||
where
|
||||
T: ThreadSafeLayoutNode<'dom>,
|
||||
{
|
||||
fn borrow_layout_data(self) -> Option<AtomicRef<'dom, InnerLayoutData>> {
|
||||
self.layout_data()
|
||||
.map(|data| data.downcast_ref::<LayoutData>().unwrap().0.borrow())
|
||||
}
|
||||
|
||||
fn mutate_layout_data(self) -> Option<AtomicRefMut<'dom, InnerLayoutData>> {
|
||||
self.layout_data()
|
||||
.and_then(|data| data.downcast_ref::<LayoutData>())
|
||||
.map(|data| data.0.borrow_mut())
|
||||
}
|
||||
|
||||
fn flow_debug_id(self) -> usize {
|
||||
self.borrow_layout_data()
|
||||
.map_or(0, |d| d.flow_construction_result.debug_id())
|
||||
}
|
||||
|
||||
fn flags(self) -> LayoutDataFlags {
|
||||
self.borrow_layout_data().as_ref().unwrap().flags
|
||||
}
|
||||
|
@ -150,17 +127,16 @@ where
|
|||
}
|
||||
|
||||
let damage = {
|
||||
let data = match node.get_style_and_layout_data() {
|
||||
Some(data) => data,
|
||||
None => panic!(
|
||||
let (layout_data, style_data) = match (node.layout_data(), node.style_data()) {
|
||||
(Some(layout_data), Some(style_data)) => (layout_data, style_data),
|
||||
_ => panic!(
|
||||
"could not get style and layout data for <{}>",
|
||||
node.as_element().unwrap().local_name()
|
||||
),
|
||||
};
|
||||
|
||||
if !data
|
||||
.layout_data
|
||||
.borrow()
|
||||
let layout_data = layout_data.downcast_ref::<LayoutData>().unwrap().0.borrow();
|
||||
if !layout_data
|
||||
.flags
|
||||
.contains(crate::data::LayoutDataFlags::HAS_BEEN_TRAVERSED)
|
||||
{
|
||||
|
@ -169,7 +145,7 @@ where
|
|||
// because that's what the code expects.
|
||||
RestyleDamage::rebuild_and_reflow()
|
||||
} else {
|
||||
data.style_data.element_data.borrow().damage
|
||||
style_data.element_data.borrow().damage
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue