Hoist exported ThreadSafeLayoutFoo functionality into traits.

This commit is contained in:
Bobby Holley 2015-11-20 19:12:04 -08:00
parent 05db7b3652
commit a85ae24c51
8 changed files with 237 additions and 174 deletions

View file

@ -57,7 +57,7 @@ use traversal::PostorderNodeMutTraversal;
use url::Url;
use util::linked_list;
use util::opts;
use wrapper::{PseudoElementType, TextContent, ThreadSafeLayoutNode};
use wrapper::{PseudoElementType, TextContent, ThreadSafeLayoutNode, TThreadSafeLayoutElement, TThreadSafeLayoutNode};
/// The results of flow construction for a DOM node.
#[derive(Clone)]

View file

@ -58,7 +58,7 @@ use table_rowgroup::TableRowGroupFlow;
use table_wrapper::TableWrapperFlow;
use util::geometry::ZERO_RECT;
use util::logical_geometry::{LogicalRect, LogicalSize, WritingMode};
use wrapper::{PseudoElementType, ThreadSafeLayoutNode};
use wrapper::{PseudoElementType, ThreadSafeLayoutNode, TThreadSafeLayoutNode};
/// Virtual methods that make up a float context.
///

View file

@ -50,7 +50,7 @@ use util::geometry::ZERO_POINT;
use util::logical_geometry::{LogicalMargin, LogicalRect, LogicalSize, WritingMode};
use util::range::*;
use util::str::slice_chars;
use wrapper::{PseudoElementType, ThreadSafeLayoutNode};
use wrapper::{PseudoElementType, ThreadSafeLayoutNode, TThreadSafeLayoutElement, TThreadSafeLayoutNode};
/// Fragments (`struct Fragment`) are the leaves of the layout tree. They cannot position
/// themselves. In general, fragments do not have a simple correspondence with CSS fragments in the

View file

@ -73,7 +73,7 @@ use util::opts;
use util::task;
use util::task_state;
use util::workqueue::WorkQueue;
use wrapper::{LayoutDocument, LayoutElement, LayoutNode, ServoLayoutNode};
use wrapper::{LayoutDocument, LayoutElement, LayoutNode, ServoLayoutNode, TThreadSafeLayoutNode};
/// The number of screens of data we're allowed to generate display lists for in each direction.
pub const DISPLAY_PORT_SIZE_FACTOR: i32 = 8;

View file

@ -31,7 +31,7 @@ use style::values::AuExtensionMethods;
use util::cursor::Cursor;
use util::geometry::ZERO_POINT;
use util::logical_geometry::WritingMode;
use wrapper::{LayoutNode, ServoLayoutNode, ThreadSafeLayoutNode};
use wrapper::{LayoutNode, ServoLayoutNode, ThreadSafeLayoutNode, TThreadSafeLayoutNode};
pub struct LayoutRPCImpl(pub Arc<Mutex<LayoutTaskData>>);

View file

@ -24,7 +24,7 @@ use style::properties::ComputedValues;
use table::InternalTable;
use table_row::{CollapsedBorder, CollapsedBorderProvenance};
use util::logical_geometry::{LogicalMargin, LogicalRect, LogicalSize, WritingMode};
use wrapper::ThreadSafeLayoutNode;
use wrapper::{ThreadSafeLayoutNode, TThreadSafeLayoutNode};
/// A table formatting context.
#[derive(RustcEncodable)]

View file

@ -18,7 +18,7 @@ use std::mem;
use util::opts;
use util::tid::tid;
use wrapper::{LayoutNode, ServoLayoutNode, layout_node_to_unsafe_layout_node};
use wrapper::{ThreadSafeLayoutNode, UnsafeLayoutNode};
use wrapper::{ThreadSafeLayoutNode, TThreadSafeLayoutNode, UnsafeLayoutNode};
/// Every time we do another layout, the old bloom filters are invalid. This is
/// detected by ticking a generation number every layout.

View file

@ -792,6 +792,163 @@ impl<T> PseudoElementType<T> {
/// A thread-safe version of `LayoutNode`, used during flow construction. This type of layout
/// node does not allow any parents or siblings of nodes to be accessed, to avoid races.
pub trait TThreadSafeLayoutNode<'ln> : Clone + Copy + Sized {
type ConcreteThreadSafeLayoutElement: TThreadSafeLayoutElement<'ln>;
/// Converts self into an `OpaqueNode`.
fn opaque(&self) -> OpaqueNode;
/// Returns the type ID of this node.
/// Returns `None` if this is a pseudo-element; otherwise, returns `Some`.
fn type_id(&self) -> Option<NodeTypeId>;
fn debug_id(self) -> usize;
fn flow_debug_id(self) -> usize;
/// Returns an iterator over this node's children.
fn children(&self) -> ThreadSafeLayoutNodeChildrenIterator<'ln, Self>;
/// If this is an element, accesses the element data. Fails if this is not an element node.
#[inline]
fn as_element(&self) -> Self::ConcreteThreadSafeLayoutElement;
#[inline]
fn get_pseudo_element_type(&self) -> PseudoElementType<display::T>;
#[inline]
fn get_before_pseudo(&self) -> Option<Self>;
#[inline]
fn get_after_pseudo(&self) -> Option<Self>;
/// Borrows the layout data immutably. Fails on a conflicting borrow.
///
/// TODO(pcwalton): Make this private. It will let us avoid borrow flag checks in some cases.
#[inline(always)]
fn borrow_layout_data(&self) -> Ref<Option<LayoutDataWrapper>>;
/// Borrows the layout data mutably. Fails on a conflicting borrow.
///
/// TODO(pcwalton): Make this private. It will let us avoid borrow flag checks in some cases.
#[inline(always)]
fn mutate_layout_data(&self) -> RefMut<Option<LayoutDataWrapper>>;
/// Returns the style results for the given node. If CSS selector matching
/// has not yet been performed, fails.
#[inline]
fn style(&self) -> Ref<Arc<ComputedValues>> {
Ref::map(self.borrow_layout_data(), |layout_data_ref| {
let layout_data = layout_data_ref.as_ref().expect("no layout data");
let style = match self.get_pseudo_element_type() {
PseudoElementType::Before(_) => &layout_data.data.before_style,
PseudoElementType::After(_) => &layout_data.data.after_style,
PseudoElementType::Normal => &layout_data.shared_data.style,
};
style.as_ref().unwrap()
})
}
/// Removes the style from this node.
fn unstyle(self) {
let mut layout_data_ref = self.mutate_layout_data();
let layout_data = layout_data_ref.as_mut().expect("no layout data");
let style =
match self.get_pseudo_element_type() {
PseudoElementType::Before(_) => &mut layout_data.data.before_style,
PseudoElementType::After (_) => &mut layout_data.data.after_style,
PseudoElementType::Normal => &mut layout_data.shared_data.style,
};
*style = None;
}
fn is_ignorable_whitespace(&self) -> bool;
/// Get the description of how to account for recent style changes.
/// This is a simple bitfield and fine to copy by value.
fn restyle_damage(self) -> RestyleDamage {
let layout_data_ref = self.borrow_layout_data();
layout_data_ref.as_ref().unwrap().data.restyle_damage
}
/// Set the restyle damage field.
fn set_restyle_damage(self, damage: RestyleDamage) {
let mut layout_data_ref = self.mutate_layout_data();
match *layout_data_ref {
Some(ref mut layout_data) => layout_data.data.restyle_damage = damage,
_ => panic!("no layout data for this node"),
}
}
/// Returns the layout data flags for this node.
fn flags(self) -> LayoutDataFlags;
/// Adds the given flags to this node.
fn insert_flags(self, new_flags: LayoutDataFlags) {
let mut layout_data_ref = self.mutate_layout_data();
match *layout_data_ref {
Some(ref mut layout_data) => layout_data.data.flags.insert(new_flags),
_ => panic!("no layout data for this node"),
}
}
/// Removes the given flags from this node.
fn remove_flags(self, flags: LayoutDataFlags) {
let mut layout_data_ref = self.mutate_layout_data();
match *layout_data_ref {
Some(ref mut layout_data) => layout_data.data.flags.remove(flags),
_ => panic!("no layout data for this node"),
}
}
/// Returns true if this node contributes content. This is used in the implementation of
/// `empty_cells` per CSS 2.1 § 17.6.1.1.
fn is_content(&self) -> bool {
match self.type_id() {
Some(NodeTypeId::Element(..)) | Some(NodeTypeId::CharacterData(CharacterDataTypeId::Text(..))) => true,
_ => false
}
}
/// If this is a text node, generated content, or a form element, copies out
/// its content. Otherwise, panics.
///
/// FIXME(pcwalton): This might have too much copying and/or allocation. Profile this.
fn text_content(&self) -> TextContent;
/// If the insertion point is within this node, returns it. Otherwise, returns `None`.
fn insertion_point(&self) -> Option<CharIndex>;
/// If this is an image element, returns its URL. If this is not an image element, fails.
///
/// FIXME(pcwalton): Don't copy URLs.
fn image_url(&self) -> Option<Url>;
fn canvas_data(&self) -> Option<HTMLCanvasData>;
/// If this node is an iframe element, returns its pipeline ID. If this node is
/// not an iframe element, fails.
fn iframe_pipeline_id(&self) -> PipelineId;
fn get_colspan(&self) -> u32;
}
// These can violate the thread-safety and therefore are not public.
trait DangerousThreadSafeLayoutNode<'ln> : TThreadSafeLayoutNode<'ln> {
unsafe fn dangerous_first_child(&self) -> Option<Self>;
unsafe fn dangerous_next_sibling(&self) -> Option<Self>;
}
pub trait TThreadSafeLayoutElement<'le> {
type ConcreteThreadSafeLayoutNode: TThreadSafeLayoutNode<'le>;
#[inline]
fn get_attr(&self, namespace: &Namespace, name: &Atom) -> Option<&'le str>;
}
#[derive(Copy, Clone)]
pub struct ThreadSafeLayoutNode<'ln> {
/// The wrapped node.
@ -800,6 +957,17 @@ pub struct ThreadSafeLayoutNode<'ln> {
pseudo: PseudoElementType<display::T>,
}
impl<'ln> DangerousThreadSafeLayoutNode<'ln> for ThreadSafeLayoutNode<'ln> {
unsafe fn dangerous_first_child(&self) -> Option<Self> {
self.get_jsmanaged().first_child_ref()
.map(|node| self.new_with_this_lifetime(&node))
}
unsafe fn dangerous_next_sibling(&self) -> Option<Self> {
self.get_jsmanaged().next_sibling_ref()
.map(|node| self.new_with_this_lifetime(&node))
}
}
impl<'ln> ThreadSafeLayoutNode<'ln> {
/// Creates a new layout node with the same lifetime as this layout node.
pub unsafe fn new_with_this_lifetime(&self, node: &LayoutJS<Node>) -> ThreadSafeLayoutNode<'ln> {
@ -832,14 +1000,23 @@ impl<'ln> ThreadSafeLayoutNode<'ln> {
self.node.get_jsmanaged()
}
/// Converts self into an `OpaqueNode`.
pub fn opaque(&self) -> OpaqueNode {
/// Borrows the layout data without checking.
#[inline(always)]
fn borrow_layout_data_unchecked(&self) -> *const Option<LayoutDataWrapper> {
unsafe {
self.node.borrow_layout_data_unchecked()
}
}
}
impl<'ln> TThreadSafeLayoutNode<'ln> for ThreadSafeLayoutNode<'ln> {
type ConcreteThreadSafeLayoutElement = ThreadSafeLayoutElement<'ln>;
fn opaque(&self) -> OpaqueNode {
OpaqueNodeMethods::from_jsmanaged(unsafe { self.get_jsmanaged() })
}
/// Returns the type ID of this node.
/// Returns `None` if this is a pseudo-element; otherwise, returns `Some`.
pub fn type_id(&self) -> Option<NodeTypeId> {
fn type_id(&self) -> Option<NodeTypeId> {
if self.pseudo != PseudoElementType::Normal {
return None
}
@ -847,22 +1024,19 @@ impl<'ln> ThreadSafeLayoutNode<'ln> {
Some(self.node.type_id())
}
pub fn debug_id(self) -> usize {
fn debug_id(self) -> usize {
self.node.debug_id()
}
pub fn flow_debug_id(self) -> usize {
fn flow_debug_id(self) -> usize {
self.node.flow_debug_id()
}
/// Returns an iterator over this node's children.
pub fn children(&self) -> ThreadSafeLayoutNodeChildrenIterator<'ln> {
fn children(&self) -> ThreadSafeLayoutNodeChildrenIterator<'ln, Self> {
ThreadSafeLayoutNodeChildrenIterator::new(*self)
}
/// If this is an element, accesses the element data. Fails if this is not an element node.
#[inline]
pub fn as_element(&self) -> ThreadSafeLayoutElement<'ln> {
fn as_element(&self) -> ThreadSafeLayoutElement<'ln> {
unsafe {
let element = match self.get_jsmanaged().downcast() {
Some(e) => e.unsafe_get(),
@ -876,13 +1050,11 @@ impl<'ln> ThreadSafeLayoutNode<'ln> {
}
}
#[inline]
pub fn get_pseudo_element_type(&self) -> PseudoElementType<display::T> {
fn get_pseudo_element_type(&self) -> PseudoElementType<display::T> {
self.pseudo
}
#[inline]
pub fn get_before_pseudo(&self) -> Option<ThreadSafeLayoutNode<'ln>> {
fn get_before_pseudo(&self) -> Option<ThreadSafeLayoutNode<'ln>> {
let layout_data_ref = self.borrow_layout_data();
let node_layout_data_wrapper = layout_data_ref.as_ref().unwrap();
node_layout_data_wrapper.data.before_style.as_ref().map(|style| {
@ -890,8 +1062,7 @@ impl<'ln> ThreadSafeLayoutNode<'ln> {
})
}
#[inline]
pub fn get_after_pseudo(&self) -> Option<ThreadSafeLayoutNode<'ln>> {
fn get_after_pseudo(&self) -> Option<ThreadSafeLayoutNode<'ln>> {
let layout_data_ref = self.borrow_layout_data();
let node_layout_data_wrapper = layout_data_ref.as_ref().unwrap();
node_layout_data_wrapper.data.after_style.as_ref().map(|style| {
@ -899,61 +1070,15 @@ impl<'ln> ThreadSafeLayoutNode<'ln> {
})
}
/// Borrows the layout data without checking.
#[inline(always)]
fn borrow_layout_data_unchecked(&self) -> *const Option<LayoutDataWrapper> {
unsafe {
self.node.borrow_layout_data_unchecked()
}
}
/// Borrows the layout data immutably. Fails on a conflicting borrow.
///
/// TODO(pcwalton): Make this private. It will let us avoid borrow flag checks in some cases.
#[inline(always)]
pub fn borrow_layout_data(&self) -> Ref<Option<LayoutDataWrapper>> {
fn borrow_layout_data(&self) -> Ref<Option<LayoutDataWrapper>> {
self.node.borrow_layout_data()
}
/// Borrows the layout data mutably. Fails on a conflicting borrow.
///
/// TODO(pcwalton): Make this private. It will let us avoid borrow flag checks in some cases.
#[inline(always)]
pub fn mutate_layout_data(&self) -> RefMut<Option<LayoutDataWrapper>> {
fn mutate_layout_data(&self) -> RefMut<Option<LayoutDataWrapper>> {
self.node.mutate_layout_data()
}
/// Returns the style results for the given node. If CSS selector matching
/// has not yet been performed, fails.
#[inline]
pub fn style(&self) -> Ref<Arc<ComputedValues>> {
Ref::map(self.borrow_layout_data(), |layout_data_ref| {
let layout_data = layout_data_ref.as_ref().expect("no layout data");
let style = match self.get_pseudo_element_type() {
PseudoElementType::Before(_) => &layout_data.data.before_style,
PseudoElementType::After(_) => &layout_data.data.after_style,
PseudoElementType::Normal => &layout_data.shared_data.style,
};
style.as_ref().unwrap()
})
}
/// Removes the style from this node.
pub fn unstyle(self) {
let mut layout_data_ref = self.mutate_layout_data();
let layout_data = layout_data_ref.as_mut().expect("no layout data");
let style =
match self.get_pseudo_element_type() {
PseudoElementType::Before(_) => &mut layout_data.data.before_style,
PseudoElementType::After (_) => &mut layout_data.data.after_style,
PseudoElementType::Normal => &mut layout_data.shared_data.style,
};
*style = None;
}
pub fn is_ignorable_whitespace(&self) -> bool {
fn is_ignorable_whitespace(&self) -> bool {
unsafe {
let text: LayoutJS<Text> = match self.get_jsmanaged().downcast() {
Some(text) => text,
@ -980,24 +1105,7 @@ impl<'ln> ThreadSafeLayoutNode<'ln> {
}
}
/// Get the description of how to account for recent style changes.
/// This is a simple bitfield and fine to copy by value.
pub fn restyle_damage(self) -> RestyleDamage {
let layout_data_ref = self.borrow_layout_data();
layout_data_ref.as_ref().unwrap().data.restyle_damage
}
/// Set the restyle damage field.
pub fn set_restyle_damage(self, damage: RestyleDamage) {
let mut layout_data_ref = self.mutate_layout_data();
match *layout_data_ref {
Some(ref mut layout_data) => layout_data.data.restyle_damage = damage,
_ => panic!("no layout data for this node"),
}
}
/// Returns the layout data flags for this node.
pub fn flags(self) -> LayoutDataFlags {
fn flags(self) -> LayoutDataFlags {
unsafe {
match *self.borrow_layout_data_unchecked() {
None => panic!(),
@ -1006,38 +1114,7 @@ impl<'ln> ThreadSafeLayoutNode<'ln> {
}
}
/// Adds the given flags to this node.
pub fn insert_flags(self, new_flags: LayoutDataFlags) {
let mut layout_data_ref = self.mutate_layout_data();
match *layout_data_ref {
Some(ref mut layout_data) => layout_data.data.flags.insert(new_flags),
_ => panic!("no layout data for this node"),
}
}
/// Removes the given flags from this node.
pub fn remove_flags(self, flags: LayoutDataFlags) {
let mut layout_data_ref = self.mutate_layout_data();
match *layout_data_ref {
Some(ref mut layout_data) => layout_data.data.flags.remove(flags),
_ => panic!("no layout data for this node"),
}
}
/// Returns true if this node contributes content. This is used in the implementation of
/// `empty_cells` per CSS 2.1 § 17.6.1.1.
pub fn is_content(&self) -> bool {
match self.type_id() {
Some(NodeTypeId::Element(..)) | Some(NodeTypeId::CharacterData(CharacterDataTypeId::Text(..))) => true,
_ => false
}
}
/// If this is a text node, generated content, or a form element, copies out
/// its content. Otherwise, panics.
///
/// FIXME(pcwalton): This might have too much copying and/or allocation. Profile this.
pub fn text_content(&self) -> TextContent {
fn text_content(&self) -> TextContent {
if self.pseudo != PseudoElementType::Normal {
let layout_data_ref = self.borrow_layout_data();
let data = &layout_data_ref.as_ref().unwrap().data;
@ -1074,8 +1151,7 @@ impl<'ln> ThreadSafeLayoutNode<'ln> {
panic!("not text!")
}
/// If the insertion point is within this node, returns it. Otherwise, returns `None`.
pub fn insertion_point(&self) -> Option<CharIndex> {
fn insertion_point(&self) -> Option<CharIndex> {
let this = unsafe {
self.get_jsmanaged()
};
@ -1094,10 +1170,7 @@ impl<'ln> ThreadSafeLayoutNode<'ln> {
None
}
/// If this is an image element, returns its URL. If this is not an image element, fails.
///
/// FIXME(pcwalton): Don't copy URLs.
pub fn image_url(&self) -> Option<Url> {
fn image_url(&self) -> Option<Url> {
unsafe {
self.get_jsmanaged().downcast()
.expect("not an image!")
@ -1105,16 +1178,14 @@ impl<'ln> ThreadSafeLayoutNode<'ln> {
}
}
pub fn canvas_data(&self) -> Option<HTMLCanvasData> {
fn canvas_data(&self) -> Option<HTMLCanvasData> {
unsafe {
let canvas_element = self.get_jsmanaged().downcast();
canvas_element.map(|canvas| canvas.data())
}
}
/// If this node is an iframe element, returns its pipeline ID. If this node is
/// not an iframe element, fails.
pub fn iframe_pipeline_id(&self) -> PipelineId {
fn iframe_pipeline_id(&self) -> PipelineId {
use script::dom::htmliframeelement::HTMLIFrameElementLayoutMethods;
unsafe {
let iframe_element = self.get_jsmanaged().downcast::<HTMLIFrameElement>()
@ -1123,65 +1194,56 @@ impl<'ln> ThreadSafeLayoutNode<'ln> {
}
}
pub fn get_colspan(&self) -> u32 {
fn get_colspan(&self) -> u32 {
unsafe {
self.get_jsmanaged().downcast::<Element>().unwrap().get_colspan()
}
}
}
pub struct ThreadSafeLayoutNodeChildrenIterator<'a> {
current_node: Option<ThreadSafeLayoutNode<'a>>,
parent_node: ThreadSafeLayoutNode<'a>,
pub struct ThreadSafeLayoutNodeChildrenIterator<'ln, ConcreteNode: TThreadSafeLayoutNode<'ln>> {
current_node: Option<ConcreteNode>,
parent_node: ConcreteNode,
// Satisfy the compiler about the unused lifetime.
phantom: PhantomData<&'ln ()>,
}
impl<'a> ThreadSafeLayoutNodeChildrenIterator<'a> {
fn new(parent: ThreadSafeLayoutNode<'a>) -> ThreadSafeLayoutNodeChildrenIterator<'a> {
fn first_child(parent: ThreadSafeLayoutNode)
-> Option<ThreadSafeLayoutNode> {
if parent.pseudo != PseudoElementType::Normal {
return None
}
parent.get_before_pseudo().or_else(|| {
unsafe {
parent.get_jsmanaged().first_child_ref()
.map(|node| parent.new_with_this_lifetime(&node))
}
})
}
impl<'ln, ConcreteNode> ThreadSafeLayoutNodeChildrenIterator<'ln, ConcreteNode>
where ConcreteNode: DangerousThreadSafeLayoutNode<'ln> {
fn new(parent: ConcreteNode) -> Self {
let first_child: Option<ConcreteNode> = match parent.get_pseudo_element_type() {
PseudoElementType::Normal => {
parent.get_before_pseudo().or_else(|| {
unsafe { parent.dangerous_first_child() }
})
},
_ => None,
};
ThreadSafeLayoutNodeChildrenIterator {
current_node: first_child(parent),
current_node: first_child,
parent_node: parent,
phantom: PhantomData,
}
}
}
impl<'a> Iterator for ThreadSafeLayoutNodeChildrenIterator<'a> {
type Item = ThreadSafeLayoutNode<'a>;
fn next(&mut self) -> Option<ThreadSafeLayoutNode<'a>> {
impl<'ln, ConcreteNode> Iterator for ThreadSafeLayoutNodeChildrenIterator<'ln, ConcreteNode>
where ConcreteNode: DangerousThreadSafeLayoutNode<'ln> {
type Item = ConcreteNode;
fn next(&mut self) -> Option<ConcreteNode> {
let node = self.current_node.clone();
if let Some(ref node) = node {
self.current_node = match node.pseudo {
self.current_node = match node.get_pseudo_element_type() {
PseudoElementType::Before(_) => {
match unsafe { self.parent_node.get_jsmanaged().first_child_ref() } {
Some(first) => {
Some(unsafe {
self.parent_node.new_with_this_lifetime(&first)
})
},
match unsafe { self.parent_node.dangerous_first_child() } {
Some(first) => Some(first),
None => self.parent_node.get_after_pseudo(),
}
},
PseudoElementType::Normal => {
match unsafe { node.get_jsmanaged().next_sibling_ref() } {
Some(next) => {
Some(unsafe {
self.parent_node.new_with_this_lifetime(&next)
})
},
match unsafe { node.dangerous_next_sibling() } {
Some(next) => Some(next),
None => self.parent_node.get_after_pseudo(),
}
},
@ -1201,9 +1263,10 @@ pub struct ThreadSafeLayoutElement<'le> {
element: &'le Element,
}
impl<'le> ThreadSafeLayoutElement<'le> {
#[inline]
pub fn get_attr(&self, namespace: &Namespace, name: &Atom) -> Option<&'le str> {
impl<'le> TThreadSafeLayoutElement<'le> for ThreadSafeLayoutElement<'le> {
type ConcreteThreadSafeLayoutNode = ThreadSafeLayoutNode<'le>;
fn get_attr(&self, namespace: &Namespace, name: &Atom) -> Option<&'le str> {
unsafe {
self.element.get_attr_val_for_layout(namespace, name)
}