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:
Martin Robinson 2024-04-04 09:56:51 +02:00 committed by GitHub
parent 1ed6b96684
commit 08ef158d4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 230 additions and 293 deletions

View file

@ -5,11 +5,11 @@
use std::marker::PhantomData;
use std::sync::{Arc, Mutex};
use atomic_refcell::{AtomicRefCell, AtomicRefMut};
use atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut};
use msg::constellation_msg::{BrowsingContextId, PipelineId};
use net_traits::image::base::Image as NetImage;
use script_layout_interface::wrapper_traits::{LayoutDataTrait, LayoutNode, ThreadSafeLayoutNode};
use script_layout_interface::{HTMLCanvasDataSource, StyleData};
use script_layout_interface::HTMLCanvasDataSource;
use servo_arc::Arc as ServoArc;
use style::properties::ComputedValues;
@ -24,7 +24,7 @@ use crate::replaced::{CanvasInfo, CanvasSource};
/// The data that is stored in each DOM node that is used by layout.
#[derive(Default)]
pub struct DOMLayoutData {
pub struct InnerDOMLayoutData {
pub(super) self_box: ArcRefCell<Option<LayoutBox>>,
pub(super) pseudo_before_box: ArcRefCell<Option<LayoutBox>>,
pub(super) pseudo_after_box: ArcRefCell<Option<LayoutBox>>,
@ -38,14 +38,15 @@ pub(super) enum LayoutBox {
FlexLevel(ArcRefCell<FlexLevelBox>),
}
/// A wrapper for [`InnerDOMLayoutData`]. 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(Default)]
pub struct DOMLayoutData(AtomicRefCell<InnerDOMLayoutData>);
// The implementation of this trait allows the data to be stored in the DOM.
impl LayoutDataTrait for DOMLayoutData {}
pub struct StyleAndLayoutData<'dom> {
pub style_data: &'dom StyleData,
pub(super) layout_data: &'dom AtomicRefCell<DOMLayoutData>,
}
pub struct BoxSlot<'dom> {
pub(crate) slot: Option<ArcRefCell<Option<LayoutBox>>>,
pub(crate) marker: PhantomData<&'dom ()>,
@ -96,8 +97,8 @@ pub(crate) trait NodeExt<'dom>: 'dom + LayoutNode<'dom> {
fn as_video(self) -> Option<(webrender_api::ImageKey, PhysicalSize<f64>)>;
fn style(self, context: &LayoutContext) -> ServoArc<ComputedValues>;
fn get_style_and_layout_data(self) -> Option<StyleAndLayoutData<'dom>>;
fn layout_data_mut(self) -> AtomicRefMut<'dom, DOMLayoutData>;
fn layout_data_mut(self) -> AtomicRefMut<'dom, InnerDOMLayoutData>;
fn layout_data(self) -> Option<AtomicRef<'dom, InnerDOMLayoutData>>;
fn element_box_slot(&self) -> BoxSlot<'dom>;
fn pseudo_element_box_slot(&self, which: WhichPseudoElement) -> BoxSlot<'dom>;
fn unset_pseudo_element_box(self, which: WhichPseudoElement);
@ -166,10 +167,21 @@ where
self.to_threadsafe().style(context.shared_context())
}
fn layout_data_mut(self) -> AtomicRefMut<'dom, DOMLayoutData> {
self.get_style_and_layout_data()
.map(|d| d.layout_data.borrow_mut())
fn layout_data_mut(self) -> AtomicRefMut<'dom, InnerDOMLayoutData> {
if LayoutNode::layout_data(&self).is_none() {
self.initialize_layout_data::<DOMLayoutData>();
}
LayoutNode::layout_data(&self)
.unwrap()
.downcast_ref::<DOMLayoutData>()
.unwrap()
.0
.borrow_mut()
}
fn layout_data(self) -> Option<AtomicRef<'dom, InnerDOMLayoutData>> {
LayoutNode::layout_data(&self)
.map(|data| data.downcast_ref::<DOMLayoutData>().unwrap().0.borrow())
}
fn element_box_slot(&self) -> BoxSlot<'dom> {
@ -202,12 +214,4 @@ where
// Stylo already takes care of removing all layout data
// for DOM descendants of elements with `display: none`.
}
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(),
})
}
}