aboutsummaryrefslogtreecommitdiffstats
path: root/components/layout/traversal.rs
diff options
context:
space:
mode:
Diffstat (limited to 'components/layout/traversal.rs')
-rw-r--r--components/layout/traversal.rs131
1 files changed, 59 insertions, 72 deletions
diff --git a/components/layout/traversal.rs b/components/layout/traversal.rs
index e20d3ab98ef..b3672ecf32c 100644
--- a/components/layout/traversal.rs
+++ b/components/layout/traversal.rs
@@ -5,102 +5,83 @@
//! Traversals over the DOM and flow trees, running the layout computations.
use construct::FlowConstructor;
-use context::{LayoutContext, SharedLayoutContext};
+use context::{LayoutContext, ScopedThreadLocalLayoutContext, SharedLayoutContext};
use display_list_builder::DisplayListBuildState;
use flow::{self, PreorderFlowTraversal};
use flow::{CAN_BE_FRAGMENTED, Flow, ImmutableFlowUtils, PostorderFlowTraversal};
-use gfx::display_list::OpaqueNode;
use script_layout_interface::wrapper_traits::{LayoutNode, ThreadSafeLayoutNode};
-use std::mem;
+use servo_config::opts;
use style::atomic_refcell::AtomicRefCell;
-use style::context::{LocalStyleContext, SharedStyleContext, StyleContext};
+use style::context::{SharedStyleContext, StyleContext};
use style::data::ElementData;
-use style::dom::{StylingMode, TElement, TNode};
+use style::dom::{TElement, TNode};
use style::selector_parser::RestyleDamage;
use style::servo::restyle_damage::{BUBBLE_ISIZES, REFLOW, REFLOW_OUT_OF_FLOW, REPAINT};
-use style::traversal::{DomTraversalContext, recalc_style_at, remove_from_bloom_filter};
+use style::traversal::{DomTraversal, recalc_style_at};
use style::traversal::PerLevelTraversalData;
-use util::opts;
use wrapper::{GetRawData, LayoutNodeHelpers, LayoutNodeLayoutData};
-pub struct RecalcStyleAndConstructFlows<'lc> {
- context: LayoutContext<'lc>,
- root: OpaqueNode,
+pub struct RecalcStyleAndConstructFlows {
+ shared: SharedLayoutContext,
}
-#[allow(unsafe_code)]
-impl<'lc, N> DomTraversalContext<N> for RecalcStyleAndConstructFlows<'lc>
- where N: LayoutNode + TNode,
- N::ConcreteElement: TElement
+impl RecalcStyleAndConstructFlows {
+ pub fn shared_layout_context(&self) -> &SharedLayoutContext {
+ &self.shared
+ }
+}
-{
- type SharedContext = SharedLayoutContext;
- #[allow(unsafe_code)]
- fn new<'a>(shared: &'a Self::SharedContext, root: OpaqueNode) -> Self {
- // FIXME(bholley): This transmutation from &'a to &'lc is very unfortunate, but I haven't
- // found a way to avoid it despite spending several days on it (and consulting Manishearth,
- // brson, and nmatsakis).
- //
- // The crux of the problem is that parameterizing DomTraversalContext on the lifetime of
- // the SharedContext doesn't work for a variety of reasons [1]. However, the code in
- // parallel.rs needs to be able to use the DomTraversalContext trait (or something similar)
- // to stack-allocate a struct (a generalized LayoutContext<'a>) that holds a borrowed
- // SharedContext, which means that the struct needs to be parameterized on a lifetime.
- // Given the aforementioned constraint, the only way to accomplish this is to avoid
- // propagating the borrow lifetime from the struct to the trait, but that means that the
- // new() method on the trait cannot require the lifetime of its argument to match the
- // lifetime of the Self object it creates.
- //
- // This could be solved with an associated type with an unbound lifetime parameter, but
- // that would require higher-kinded types, which don't exist yet and probably aren't coming
- // for a while.
- //
- // So we transmute. :-( This is safe because the DomTravesalContext is stack-allocated on
- // the worker thread while processing a WorkUnit, whereas the borrowed SharedContext is
- // live for the entire duration of the restyle. This really could _almost_ compile: all
- // we'd need to do is change the signature to to |new<'a: 'lc>|, and everything would
- // work great. But we can't do that, because that would cause a mismatch with the signature
- // in the trait we're implementing, and we can't mention 'lc in that trait at all for the
- // reasons described above.
- //
- // [1] For example, the WorkQueue type needs to be parameterized on the concrete type of
- // DomTraversalContext::SharedContext, and the WorkQueue lifetime is similar to that of the
- // LayoutThread, generally much longer than that of a given SharedLayoutContext borrow.
- let shared_lc: &'lc SharedLayoutContext = unsafe { mem::transmute(shared) };
+impl RecalcStyleAndConstructFlows {
+ /// Creates a traversal context, taking ownership of the shared layout context.
+ pub fn new(shared: SharedLayoutContext) -> Self {
RecalcStyleAndConstructFlows {
- context: LayoutContext::new(shared_lc),
- root: root,
+ shared: shared,
}
}
- fn process_preorder(&self, node: N, data: &mut PerLevelTraversalData) {
+ /// Consumes this traversal context, returning ownership of the shared layout
+ /// context to the caller.
+ pub fn destroy(self) -> SharedLayoutContext {
+ self.shared
+ }
+}
+
+#[allow(unsafe_code)]
+impl<N> DomTraversal<N> for RecalcStyleAndConstructFlows
+ where N: LayoutNode + TNode,
+ N::ConcreteElement: TElement
+{
+ type ThreadLocalContext = ScopedThreadLocalLayoutContext<N::ConcreteElement>;
+
+ fn process_preorder(&self, traversal_data: &mut PerLevelTraversalData,
+ thread_local: &mut Self::ThreadLocalContext, node: N) {
// FIXME(pcwalton): Stop allocating here. Ideally this should just be
// done by the HTML parser.
node.initialize_data();
if !node.is_text_node() {
let el = node.as_element().unwrap();
- recalc_style_at::<_, _, Self>(&self.context, data, el);
+ let mut data = el.mutate_data().unwrap();
+ let mut context = StyleContext {
+ shared: &self.shared.style_context,
+ thread_local: &mut thread_local.style_context,
+ };
+ recalc_style_at(self, traversal_data, &mut context, el, &mut data);
}
}
- fn process_postorder(&self, node: N) {
- construct_flows_at(&self.context, self.root, node);
+ fn process_postorder(&self, thread_local: &mut Self::ThreadLocalContext, node: N) {
+ let context = LayoutContext::new(&self.shared);
+ construct_flows_at(&context, thread_local, node);
}
- fn should_traverse_child(child: N) -> bool {
- match child.as_element() {
- // Elements should be traversed if they need styling or flow construction.
- Some(el) => el.styling_mode() != StylingMode::Stop ||
- el.as_node().to_threadsafe().restyle_damage() != RestyleDamage::empty(),
-
- // Text nodes never need styling. However, there are two cases they may need
- // 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).
- None => child.get_raw_data().is_none() ||
- child.parent_node().unwrap().to_threadsafe().restyle_damage() != RestyleDamage::empty(),
- }
+ fn text_node_needs_traversal(node: N) -> bool {
+ // Text nodes never need styling. However, there are two cases they may need
+ // 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_raw_data().is_none() ||
+ node.parent_node().unwrap().to_threadsafe().restyle_damage() != RestyleDamage::empty()
}
unsafe fn ensure_element_data(element: &N::ConcreteElement) -> &AtomicRefCell<ElementData> {
@@ -112,8 +93,12 @@ impl<'lc, N> DomTraversalContext<N> for RecalcStyleAndConstructFlows<'lc>
element.as_node().clear_data();
}
- fn local_context(&self) -> &LocalStyleContext {
- self.context.local_context()
+ fn shared_context(&self) -> &SharedStyleContext {
+ &self.shared.style_context
+ }
+
+ fn create_thread_local_context(&self) -> Self::ThreadLocalContext {
+ ScopedThreadLocalLayoutContext::new(&self.shared)
}
}
@@ -126,7 +111,11 @@ pub trait PostorderNodeMutTraversal<ConcreteThreadSafeLayoutNode: ThreadSafeLayo
/// The flow construction traversal, which builds flows for styled nodes.
#[inline]
#[allow(unsafe_code)]
-fn construct_flows_at<'a, N: LayoutNode>(context: &'a LayoutContext<'a>, root: OpaqueNode, node: N) {
+fn construct_flows_at<'a, N>(context: &LayoutContext<'a>,
+ _thread_local: &mut ScopedThreadLocalLayoutContext<N::ConcreteElement>,
+ node: N)
+ where N: LayoutNode,
+{
debug!("construct_flows_at: {:?}", node);
// Construct flows for this node.
@@ -150,8 +139,6 @@ fn construct_flows_at<'a, N: LayoutNode>(context: &'a LayoutContext<'a>, root: O
if let Some(el) = node.as_element() {
el.mutate_data().unwrap().persist();
unsafe { el.unset_dirty_descendants(); }
-
- remove_from_bloom_filter(context, root, el);
}
}