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

@ -9,18 +9,16 @@ use std::fmt;
use std::marker::PhantomData;
use std::sync::Arc as StdArc;
use atomic_refcell::AtomicRefCell;
use gfx_traits::ByteIndex;
use html5ever::{local_name, namespace_url, ns};
use msg::constellation_msg::{BrowsingContextId, PipelineId};
use net_traits::image::base::{Image, ImageMetadata};
use range::Range;
use script_layout_interface::wrapper_traits::{
GetStyleAndOpaqueLayoutData, LayoutDataTrait, LayoutNode, PseudoElementType,
ThreadSafeLayoutNode,
LayoutDataTrait, LayoutNode, PseudoElementType, ThreadSafeLayoutNode,
};
use script_layout_interface::{
HTMLCanvasData, HTMLMediaData, LayoutNodeType, SVGSVGData, StyleAndOpaqueLayoutData, StyleData,
GenericLayoutData, HTMLCanvasData, HTMLMediaData, LayoutNodeType, SVGSVGData, StyleData,
TrustedNodeAddress,
};
use servo_arc::Arc;
@ -206,30 +204,35 @@ impl<'dom, LayoutDataType: LayoutDataTrait> LayoutNode<'dom>
self.script_type_id().into()
}
unsafe fn initialize_data(&self) {
if self.get_style_and_opaque_layout_data().is_some() {
return;
unsafe fn initialize_style_and_layout_data<RequestedLayoutDataType: LayoutDataTrait>(&self) {
let inner = self.get_jsmanaged();
if inner.style_data().is_none() {
inner.initialize_style_data();
}
if inner.layout_data().is_none() {
inner.initialize_layout_data(Box::<RequestedLayoutDataType>::default());
}
}
let opaque = StyleAndOpaqueLayoutData::new(
StyleData::default(),
AtomicRefCell::new(LayoutDataType::default()),
);
self.get_jsmanaged()
.init_style_and_opaque_layout_data(opaque);
fn initialize_layout_data<RequestedLayoutDataType: LayoutDataTrait>(&self) {
let inner = self.get_jsmanaged();
if inner.layout_data().is_none() {
unsafe {
inner.initialize_layout_data(Box::<RequestedLayoutDataType>::default());
}
}
}
fn is_connected(&self) -> bool {
unsafe { self.node.get_flag(NodeFlags::IS_CONNECTED) }
}
}
impl<'dom, LayoutDataType: LayoutDataTrait> GetStyleAndOpaqueLayoutData<'dom>
for ServoLayoutNode<'dom, LayoutDataType>
{
fn get_style_and_opaque_layout_data(self) -> Option<&'dom StyleAndOpaqueLayoutData> {
self.get_jsmanaged().get_style_and_opaque_layout_data()
fn style_data(&self) -> Option<&'dom StyleData> {
self.get_jsmanaged().style_data()
}
fn layout_data(&self) -> Option<&'dom GenericLayoutData> {
self.get_jsmanaged().layout_data()
}
}
@ -369,8 +372,12 @@ impl<'dom, LayoutDataType: LayoutDataTrait> ThreadSafeLayoutNode<'dom>
})
}
fn get_style_and_opaque_layout_data(self) -> Option<&'dom StyleAndOpaqueLayoutData> {
self.node.get_style_and_opaque_layout_data()
fn style_data(&self) -> Option<&'dom StyleData> {
self.node.style_data()
}
fn layout_data(&self) -> Option<&'dom GenericLayoutData> {
self.node.layout_data()
}
fn is_ignorable_whitespace(&self, context: &SharedStyleContext) -> bool {
@ -584,11 +591,3 @@ impl<'dom, LayoutDataType: LayoutDataTrait> Iterator
}
}
}
impl<'dom, LayoutDataType: LayoutDataTrait> GetStyleAndOpaqueLayoutData<'dom>
for ServoThreadSafeLayoutNode<'dom, LayoutDataType>
{
fn get_style_and_opaque_layout_data(self) -> Option<&'dom StyleAndOpaqueLayoutData> {
self.node.get_style_and_opaque_layout_data()
}
}