mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
Auto merge of #14610 - bholley:style_context_refactor, r=emilio
Simplify style context architecture and make it safer See the discussion at https://bugzilla.mozilla.org/show_bug.cgi?id=1323372 Not done here, but want to get a try run in on the first patch. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/14610) <!-- Reviewable:end -->
This commit is contained in:
commit
a4ecdf2d5f
18 changed files with 329 additions and 323 deletions
|
@ -19,13 +19,13 @@ use stylist::Stylist;
|
|||
use timer::Timer;
|
||||
|
||||
/// This structure is used to create a local style context from a shared one.
|
||||
pub struct LocalStyleContextCreationInfo {
|
||||
pub struct ThreadLocalStyleContextCreationInfo {
|
||||
new_animations_sender: Sender<Animation>,
|
||||
}
|
||||
|
||||
impl LocalStyleContextCreationInfo {
|
||||
impl ThreadLocalStyleContextCreationInfo {
|
||||
pub fn new(animations_sender: Sender<Animation>) -> Self {
|
||||
LocalStyleContextCreationInfo {
|
||||
ThreadLocalStyleContextCreationInfo {
|
||||
new_animations_sender: animations_sender,
|
||||
}
|
||||
}
|
||||
|
@ -57,33 +57,33 @@ pub struct SharedStyleContext {
|
|||
///The CSS error reporter for all CSS loaded in this layout thread
|
||||
pub error_reporter: Box<ParseErrorReporter + Sync>,
|
||||
|
||||
/// Data needed to create the local style context from the shared one.
|
||||
pub local_context_creation_data: Mutex<LocalStyleContextCreationInfo>,
|
||||
/// Data needed to create the thread-local style context from the shared one.
|
||||
pub local_context_creation_data: Mutex<ThreadLocalStyleContextCreationInfo>,
|
||||
|
||||
/// The current timer for transitions and animations. This is needed to test
|
||||
/// them.
|
||||
pub timer: Timer,
|
||||
}
|
||||
|
||||
pub struct LocalStyleContext {
|
||||
pub struct ThreadLocalStyleContext {
|
||||
pub style_sharing_candidate_cache: RefCell<StyleSharingCandidateCache>,
|
||||
/// A channel on which new animations that have been triggered by style
|
||||
/// recalculation can be sent.
|
||||
pub new_animations_sender: Sender<Animation>,
|
||||
}
|
||||
|
||||
impl LocalStyleContext {
|
||||
pub fn new(local_context_creation_data: &LocalStyleContextCreationInfo) -> Self {
|
||||
LocalStyleContext {
|
||||
impl ThreadLocalStyleContext {
|
||||
pub fn new(local_context_creation_data: &ThreadLocalStyleContextCreationInfo) -> Self {
|
||||
ThreadLocalStyleContext {
|
||||
style_sharing_candidate_cache: RefCell::new(StyleSharingCandidateCache::new()),
|
||||
new_animations_sender: local_context_creation_data.new_animations_sender.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait StyleContext<'a> {
|
||||
fn shared_context(&self) -> &'a SharedStyleContext;
|
||||
fn local_context(&self) -> &LocalStyleContext;
|
||||
pub struct StyleContext<'a> {
|
||||
pub shared: &'a SharedStyleContext,
|
||||
pub thread_local: &'a ThreadLocalStyleContext,
|
||||
}
|
||||
|
||||
/// Why we're doing reflow.
|
||||
|
|
|
@ -2,20 +2,20 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use context::{LocalStyleContext, StyleContext, SharedStyleContext};
|
||||
use context::{SharedStyleContext, ThreadLocalStyleContext};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
thread_local!(static LOCAL_CONTEXT_KEY: RefCell<Option<Rc<LocalStyleContext>>> = RefCell::new(None));
|
||||
thread_local!(static LOCAL_CONTEXT_KEY: RefCell<Option<Rc<ThreadLocalStyleContext>>> = RefCell::new(None));
|
||||
|
||||
// Keep this implementation in sync with the one in components/layout/context.rs.
|
||||
fn create_or_get_local_context(shared: &SharedStyleContext) -> Rc<LocalStyleContext> {
|
||||
pub fn create_or_get_local_context(shared: &SharedStyleContext) -> Rc<ThreadLocalStyleContext> {
|
||||
LOCAL_CONTEXT_KEY.with(|r| {
|
||||
let mut r = r.borrow_mut();
|
||||
if let Some(context) = r.clone() {
|
||||
context
|
||||
} else {
|
||||
let context = Rc::new(LocalStyleContext::new(&shared.local_context_creation_data.lock().unwrap()));
|
||||
let context = Rc::new(ThreadLocalStyleContext::new(&shared.local_context_creation_data.lock().unwrap()));
|
||||
*r = Some(context.clone());
|
||||
context
|
||||
}
|
||||
|
@ -25,28 +25,3 @@ fn create_or_get_local_context(shared: &SharedStyleContext) -> Rc<LocalStyleCont
|
|||
pub fn clear_local_context() {
|
||||
LOCAL_CONTEXT_KEY.with(|r| *r.borrow_mut() = None);
|
||||
}
|
||||
|
||||
pub struct StandaloneStyleContext<'a> {
|
||||
pub shared: &'a SharedStyleContext,
|
||||
cached_local_context: Rc<LocalStyleContext>,
|
||||
}
|
||||
|
||||
impl<'a> StandaloneStyleContext<'a> {
|
||||
pub fn new(shared: &'a SharedStyleContext) -> Self {
|
||||
let local_context = create_or_get_local_context(shared);
|
||||
StandaloneStyleContext {
|
||||
shared: shared,
|
||||
cached_local_context: local_context,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> StyleContext<'a> for StandaloneStyleContext<'a> {
|
||||
fn shared_context(&self) -> &'a SharedStyleContext {
|
||||
&self.shared
|
||||
}
|
||||
|
||||
fn local_context(&self) -> &LocalStyleContext {
|
||||
&self.cached_local_context
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,35 +3,38 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use atomic_refcell::AtomicRefCell;
|
||||
use context::{LocalStyleContext, SharedStyleContext, StyleContext};
|
||||
use context::{SharedStyleContext, StyleContext, ThreadLocalStyleContext};
|
||||
use data::ElementData;
|
||||
use dom::{NodeInfo, OpaqueNode, TNode};
|
||||
use gecko::context::StandaloneStyleContext;
|
||||
use dom::{NodeInfo, TNode};
|
||||
use gecko::context::create_or_get_local_context;
|
||||
use gecko::wrapper::{GeckoElement, GeckoNode};
|
||||
use std::mem;
|
||||
use traversal::{DomTraversalContext, PerLevelTraversalData, recalc_style_at};
|
||||
use std::rc::Rc; use traversal::{DomTraversal, PerLevelTraversalData, recalc_style_at};
|
||||
|
||||
pub struct RecalcStyleOnly<'lc> {
|
||||
context: StandaloneStyleContext<'lc>,
|
||||
pub struct RecalcStyleOnly {
|
||||
shared: SharedStyleContext,
|
||||
}
|
||||
|
||||
impl<'lc, 'ln> DomTraversalContext<GeckoNode<'ln>> for RecalcStyleOnly<'lc> {
|
||||
type SharedContext = SharedStyleContext;
|
||||
#[allow(unsafe_code)]
|
||||
fn new<'a>(shared: &'a Self::SharedContext, _root: OpaqueNode) -> Self {
|
||||
// See the comment in RecalcStyleAndConstructFlows::new for an explanation of why this is
|
||||
// necessary.
|
||||
let shared_lc: &'lc Self::SharedContext = unsafe { mem::transmute(shared) };
|
||||
impl RecalcStyleOnly {
|
||||
pub fn new(shared: SharedStyleContext) -> Self {
|
||||
RecalcStyleOnly {
|
||||
context: StandaloneStyleContext::new(shared_lc),
|
||||
shared: shared,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ln> DomTraversal<GeckoNode<'ln>> for RecalcStyleOnly {
|
||||
type ThreadLocalContext = ThreadLocalStyleContext;
|
||||
|
||||
fn process_preorder(&self, node: GeckoNode<'ln>, traversal_data: &mut PerLevelTraversalData) {
|
||||
if node.is_element() {
|
||||
let el = node.as_element().unwrap();
|
||||
let mut data = unsafe { el.ensure_data() }.borrow_mut();
|
||||
recalc_style_at::<_, _, Self>(&self.context, traversal_data, el, &mut data);
|
||||
let tlc = self.create_or_get_thread_local_context();
|
||||
let context = StyleContext {
|
||||
shared: &self.shared,
|
||||
thread_local: &*tlc,
|
||||
};
|
||||
recalc_style_at(self, traversal_data, &context, el, &mut data);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,7 +53,11 @@ impl<'lc, 'ln> DomTraversalContext<GeckoNode<'ln>> for RecalcStyleOnly<'lc> {
|
|||
element.clear_data()
|
||||
}
|
||||
|
||||
fn local_context(&self) -> &LocalStyleContext {
|
||||
self.context.local_context()
|
||||
fn shared_context(&self) -> &SharedStyleContext {
|
||||
&self.shared
|
||||
}
|
||||
|
||||
fn create_or_get_thread_local_context(&self) -> Rc<ThreadLocalStyleContext> {
|
||||
create_or_get_local_context(&self.shared)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -391,16 +391,15 @@ trait PrivateMatchMethods: TElement {
|
|||
///
|
||||
/// Note that animations only apply to nodes or ::before or ::after
|
||||
/// pseudo-elements.
|
||||
fn cascade_node_pseudo_element<'a, Ctx>(&self,
|
||||
context: &Ctx,
|
||||
parent_style: Option<&Arc<ComputedValues>>,
|
||||
old_style: Option<&Arc<ComputedValues>>,
|
||||
rule_node: &StrongRuleNode,
|
||||
possibly_expired_animations: &[PropertyAnimation],
|
||||
booleans: CascadeBooleans)
|
||||
-> Arc<ComputedValues>
|
||||
where Ctx: StyleContext<'a> {
|
||||
let shared_context = context.shared_context();
|
||||
fn cascade_node_pseudo_element<'a>(&self,
|
||||
context: &StyleContext,
|
||||
parent_style: Option<&Arc<ComputedValues>>,
|
||||
old_style: Option<&Arc<ComputedValues>>,
|
||||
rule_node: &StrongRuleNode,
|
||||
possibly_expired_animations: &[PropertyAnimation],
|
||||
booleans: CascadeBooleans)
|
||||
-> Arc<ComputedValues> {
|
||||
let shared_context = context.shared;
|
||||
let mut cascade_info = CascadeInfo::new();
|
||||
let mut cascade_flags = CascadeFlags::empty();
|
||||
if booleans.shareable {
|
||||
|
@ -433,7 +432,7 @@ trait PrivateMatchMethods: TElement {
|
|||
let mut this_style = Arc::new(this_style);
|
||||
|
||||
if booleans.animate {
|
||||
let new_animations_sender = &context.local_context().new_animations_sender;
|
||||
let new_animations_sender = &context.thread_local.new_animations_sender;
|
||||
let this_opaque = self.as_node().opaque();
|
||||
// Trigger any present animations if necessary.
|
||||
animation::maybe_start_animations(&shared_context,
|
||||
|
@ -509,26 +508,23 @@ trait PrivateMatchMethods: TElement {
|
|||
}
|
||||
}
|
||||
|
||||
fn compute_rule_node<'a, Ctx>(context: &Ctx,
|
||||
applicable_declarations: &mut Vec<ApplicableDeclarationBlock>)
|
||||
-> StrongRuleNode
|
||||
where Ctx: StyleContext<'a>
|
||||
fn compute_rule_node(context: &StyleContext,
|
||||
applicable_declarations: &mut Vec<ApplicableDeclarationBlock>)
|
||||
-> StrongRuleNode
|
||||
{
|
||||
let shared_context = context.shared_context();
|
||||
let rules = applicable_declarations.drain(..).map(|d| (d.source, d.importance));
|
||||
let rule_node = shared_context.stylist.rule_tree.insert_ordered_rules(rules);
|
||||
let rule_node = context.shared.stylist.rule_tree.insert_ordered_rules(rules);
|
||||
rule_node
|
||||
}
|
||||
|
||||
impl<E: TElement> PrivateMatchMethods for E {}
|
||||
|
||||
pub trait MatchMethods : TElement {
|
||||
fn match_element<'a, Ctx>(&self, context: &Ctx, parent_bf: Option<&BloomFilter>)
|
||||
-> MatchResults
|
||||
where Ctx: StyleContext<'a>
|
||||
fn match_element(&self, context: &StyleContext, parent_bf: Option<&BloomFilter>)
|
||||
-> MatchResults
|
||||
{
|
||||
let mut applicable_declarations: Vec<ApplicableDeclarationBlock> = Vec::with_capacity(16);
|
||||
let stylist = &context.shared_context().stylist;
|
||||
let stylist = &context.shared.stylist;
|
||||
let style_attribute = self.style_attribute();
|
||||
|
||||
// Compute the primary rule node.
|
||||
|
@ -721,14 +717,13 @@ pub trait MatchMethods : TElement {
|
|||
}
|
||||
}
|
||||
|
||||
unsafe fn cascade_node<'a, Ctx>(&self,
|
||||
context: &Ctx,
|
||||
mut data: &mut AtomicRefMut<ElementData>,
|
||||
parent: Option<Self>,
|
||||
primary_rule_node: StrongRuleNode,
|
||||
pseudo_rule_nodes: PseudoRuleNodes,
|
||||
primary_is_shareable: bool)
|
||||
where Ctx: StyleContext<'a>
|
||||
unsafe fn cascade_node(&self,
|
||||
context: &StyleContext,
|
||||
mut data: &mut AtomicRefMut<ElementData>,
|
||||
parent: Option<Self>,
|
||||
primary_rule_node: StrongRuleNode,
|
||||
pseudo_rule_nodes: PseudoRuleNodes,
|
||||
primary_is_shareable: bool)
|
||||
{
|
||||
// Get our parent's style.
|
||||
let parent_data = parent.as_ref().map(|x| x.borrow_data().unwrap());
|
||||
|
@ -753,7 +748,7 @@ pub trait MatchMethods : TElement {
|
|||
Some(previous) => {
|
||||
// Update animations before the cascade. This may modify the
|
||||
// value of the old primary style.
|
||||
self.update_animations_for_cascade(context.shared_context(),
|
||||
self.update_animations_for_cascade(&context.shared,
|
||||
&mut previous.primary.values,
|
||||
&mut possibly_expired_animations);
|
||||
(Some(&previous.primary.values), Some(&mut previous.pseudos))
|
||||
|
@ -794,17 +789,16 @@ pub trait MatchMethods : TElement {
|
|||
data.finish_styling(new_styles, damage);
|
||||
}
|
||||
|
||||
fn compute_damage_and_cascade_pseudos<'a, Ctx>(
|
||||
fn compute_damage_and_cascade_pseudos(
|
||||
&self,
|
||||
old_primary: Option<&Arc<ComputedValues>>,
|
||||
mut old_pseudos: Option<&mut PseudoStyles>,
|
||||
new_primary: &Arc<ComputedValues>,
|
||||
new_pseudos: &mut PseudoStyles,
|
||||
context: &Ctx,
|
||||
context: &StyleContext,
|
||||
mut pseudo_rule_nodes: PseudoRuleNodes,
|
||||
possibly_expired_animations: &mut Vec<PropertyAnimation>)
|
||||
-> RestyleDamage
|
||||
where Ctx: StyleContext<'a>
|
||||
{
|
||||
// Here we optimise the case of the style changing but both the
|
||||
// previous and the new styles having display: none. In this
|
||||
|
@ -859,7 +853,7 @@ pub trait MatchMethods : TElement {
|
|||
if let Some(ref mut old_pseudo_style) = maybe_old_pseudo_style {
|
||||
// Update animations before the cascade. This may modify
|
||||
// the value of old_pseudo_style.
|
||||
self.update_animations_for_cascade(context.shared_context(),
|
||||
self.update_animations_for_cascade(&context.shared,
|
||||
&mut old_pseudo_style.values,
|
||||
possibly_expired_animations);
|
||||
}
|
||||
|
|
|
@ -9,19 +9,20 @@
|
|||
use dom::{OpaqueNode, TElement, TNode, UnsafeNode};
|
||||
use rayon;
|
||||
use servo_config::opts;
|
||||
use std::borrow::Borrow;
|
||||
use std::sync::atomic::Ordering;
|
||||
use traversal::{DomTraversalContext, PerLevelTraversalData, PreTraverseToken};
|
||||
use traversal::{DomTraversal, PerLevelTraversalData, PreTraverseToken};
|
||||
use traversal::{STYLE_SHARING_CACHE_HITS, STYLE_SHARING_CACHE_MISSES};
|
||||
|
||||
pub const CHUNK_SIZE: usize = 64;
|
||||
|
||||
pub fn traverse_dom<N, C>(root: N::ConcreteElement,
|
||||
pub fn traverse_dom<N, D>(traversal: &D,
|
||||
root: N::ConcreteElement,
|
||||
known_root_dom_depth: Option<usize>,
|
||||
shared_context: &C::SharedContext,
|
||||
token: PreTraverseToken,
|
||||
queue: &rayon::ThreadPool)
|
||||
where N: TNode,
|
||||
C: DomTraversalContext<N>
|
||||
D: DomTraversal<N>
|
||||
{
|
||||
if opts::get().style_sharing_stats {
|
||||
STYLE_SHARING_CACHE_HITS.store(0, Ordering::SeqCst);
|
||||
|
@ -32,7 +33,7 @@ pub fn traverse_dom<N, C>(root: N::ConcreteElement,
|
|||
// in conjunction with bottom-up traversal. If we did, we'd need to put
|
||||
// it on the context to make it available to the bottom-up phase.
|
||||
let (nodes, depth) = if token.traverse_unstyled_children_only() {
|
||||
debug_assert!(!C::needs_postorder_traversal());
|
||||
debug_assert!(!D::needs_postorder_traversal());
|
||||
let mut children = vec![];
|
||||
for kid in root.as_node().children() {
|
||||
if kid.as_element().map_or(false, |el| el.get_data().is_none()) {
|
||||
|
@ -51,7 +52,7 @@ pub fn traverse_dom<N, C>(root: N::ConcreteElement,
|
|||
let root = root.as_node().opaque();
|
||||
queue.install(|| {
|
||||
rayon::scope(|scope| {
|
||||
traverse_nodes::<_, C>(nodes, root, data, scope, shared_context);
|
||||
traverse_nodes(nodes, root, data, scope, traversal);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -68,16 +69,14 @@ pub fn traverse_dom<N, C>(root: N::ConcreteElement,
|
|||
/// A parallel top-down DOM traversal.
|
||||
#[inline(always)]
|
||||
#[allow(unsafe_code)]
|
||||
fn top_down_dom<'a, 'scope, N, C>(unsafe_nodes: &'a [UnsafeNode],
|
||||
fn top_down_dom<'a, 'scope, N, D>(unsafe_nodes: &'a [UnsafeNode],
|
||||
root: OpaqueNode,
|
||||
mut data: PerLevelTraversalData,
|
||||
scope: &'a rayon::Scope<'scope>,
|
||||
shared_context: &'scope C::SharedContext)
|
||||
traversal: &'scope D)
|
||||
where N: TNode,
|
||||
C: DomTraversalContext<N>,
|
||||
D: DomTraversal<N>,
|
||||
{
|
||||
let context = C::new(shared_context, root);
|
||||
|
||||
let mut discovered_child_nodes = vec![];
|
||||
for unsafe_node in unsafe_nodes {
|
||||
// Get a real layout node.
|
||||
|
@ -85,9 +84,9 @@ fn top_down_dom<'a, 'scope, N, C>(unsafe_nodes: &'a [UnsafeNode],
|
|||
|
||||
// Perform the appropriate traversal.
|
||||
let mut children_to_process = 0isize;
|
||||
context.process_preorder(node, &mut data);
|
||||
traversal.process_preorder(node, &mut data);
|
||||
if let Some(el) = node.as_element() {
|
||||
C::traverse_children(el, |kid| {
|
||||
D::traverse_children(el, |kid| {
|
||||
children_to_process += 1;
|
||||
discovered_child_nodes.push(kid.to_unsafe())
|
||||
});
|
||||
|
@ -95,10 +94,10 @@ fn top_down_dom<'a, 'scope, N, C>(unsafe_nodes: &'a [UnsafeNode],
|
|||
|
||||
// Reset the count of children if we need to do a bottom-up traversal
|
||||
// after the top up.
|
||||
if C::needs_postorder_traversal() {
|
||||
if D::needs_postorder_traversal() {
|
||||
if children_to_process == 0 {
|
||||
// If there were no more children, start walking back up.
|
||||
bottom_up_dom::<N, C>(root, *unsafe_node, shared_context)
|
||||
bottom_up_dom(root, *unsafe_node, traversal)
|
||||
} else {
|
||||
// Otherwise record the number of children to process when the
|
||||
// time comes.
|
||||
|
@ -109,21 +108,22 @@ fn top_down_dom<'a, 'scope, N, C>(unsafe_nodes: &'a [UnsafeNode],
|
|||
|
||||
// NB: In parallel traversal mode we have to purge the LRU cache in order to
|
||||
// be able to access it without races.
|
||||
context.local_context().style_sharing_candidate_cache.borrow_mut().clear();
|
||||
let tlc = traversal.create_or_get_thread_local_context();
|
||||
(*tlc).borrow().style_sharing_candidate_cache.borrow_mut().clear();
|
||||
|
||||
if let Some(ref mut depth) = data.current_dom_depth {
|
||||
*depth += 1;
|
||||
}
|
||||
|
||||
traverse_nodes::<_, C>(discovered_child_nodes, root, data, scope, shared_context);
|
||||
traverse_nodes(discovered_child_nodes, root, data, scope, traversal);
|
||||
}
|
||||
|
||||
fn traverse_nodes<'a, 'scope, N, C>(nodes: Vec<UnsafeNode>, root: OpaqueNode,
|
||||
fn traverse_nodes<'a, 'scope, N, D>(nodes: Vec<UnsafeNode>, root: OpaqueNode,
|
||||
data: PerLevelTraversalData,
|
||||
scope: &'a rayon::Scope<'scope>,
|
||||
shared_context: &'scope C::SharedContext)
|
||||
traversal: &'scope D)
|
||||
where N: TNode,
|
||||
C: DomTraversalContext<N>,
|
||||
D: DomTraversal<N>,
|
||||
{
|
||||
if nodes.is_empty() {
|
||||
return;
|
||||
|
@ -133,7 +133,7 @@ fn traverse_nodes<'a, 'scope, N, C>(nodes: Vec<UnsafeNode>, root: OpaqueNode,
|
|||
// we're only pushing one work unit.
|
||||
if nodes.len() <= CHUNK_SIZE {
|
||||
let nodes = nodes.into_boxed_slice();
|
||||
top_down_dom::<N, C>(&nodes, root, data, scope, shared_context);
|
||||
top_down_dom(&nodes, root, data, scope, traversal);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -143,7 +143,7 @@ fn traverse_nodes<'a, 'scope, N, C>(nodes: Vec<UnsafeNode>, root: OpaqueNode,
|
|||
let data = data.clone();
|
||||
scope.spawn(move |scope| {
|
||||
let nodes = nodes;
|
||||
top_down_dom::<N, C>(&nodes, root, data, scope, shared_context)
|
||||
top_down_dom(&nodes, root, data, scope, traversal)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -160,19 +160,17 @@ fn traverse_nodes<'a, 'scope, N, C>(nodes: Vec<UnsafeNode>, root: OpaqueNode,
|
|||
/// The only communication between siblings is that they both
|
||||
/// fetch-and-subtract the parent's children count.
|
||||
#[allow(unsafe_code)]
|
||||
fn bottom_up_dom<N, C>(root: OpaqueNode,
|
||||
fn bottom_up_dom<N, D>(root: OpaqueNode,
|
||||
unsafe_node: UnsafeNode,
|
||||
shared_context: &C::SharedContext)
|
||||
traversal: &D)
|
||||
where N: TNode,
|
||||
C: DomTraversalContext<N>
|
||||
D: DomTraversal<N>
|
||||
{
|
||||
let context = C::new(shared_context, root);
|
||||
|
||||
// Get a real layout node.
|
||||
let mut node = unsafe { N::from_unsafe(&unsafe_node) };
|
||||
loop {
|
||||
// Perform the appropriate operation.
|
||||
context.process_postorder(node);
|
||||
traversal.process_postorder(node);
|
||||
|
||||
if node.opaque() == root {
|
||||
break;
|
||||
|
|
|
@ -5,53 +5,54 @@
|
|||
//! Implements sequential traversal over the DOM tree.
|
||||
|
||||
use dom::{TElement, TNode};
|
||||
use traversal::{DomTraversalContext, PerLevelTraversalData, PreTraverseToken};
|
||||
use std::borrow::Borrow;
|
||||
use traversal::{DomTraversal, PerLevelTraversalData, PreTraverseToken};
|
||||
|
||||
pub fn traverse_dom<N, C>(root: N::ConcreteElement,
|
||||
shared: &C::SharedContext,
|
||||
pub fn traverse_dom<N, D>(traversal: &D,
|
||||
root: N::ConcreteElement,
|
||||
token: PreTraverseToken)
|
||||
where N: TNode,
|
||||
C: DomTraversalContext<N>
|
||||
D: DomTraversal<N>
|
||||
{
|
||||
debug_assert!(token.should_traverse());
|
||||
|
||||
fn doit<'a, N, C>(context: &'a C, node: N, data: &mut PerLevelTraversalData)
|
||||
fn doit<N, D>(traversal: &D, node: N, data: &mut PerLevelTraversalData)
|
||||
where N: TNode,
|
||||
C: DomTraversalContext<N>
|
||||
D: DomTraversal<N>
|
||||
{
|
||||
context.process_preorder(node, data);
|
||||
traversal.process_preorder(node, data);
|
||||
if let Some(el) = node.as_element() {
|
||||
if let Some(ref mut depth) = data.current_dom_depth {
|
||||
*depth += 1;
|
||||
}
|
||||
|
||||
C::traverse_children(el, |kid| doit::<N, C>(context, kid, data));
|
||||
D::traverse_children(el, |kid| doit(traversal, kid, data));
|
||||
|
||||
if let Some(ref mut depth) = data.current_dom_depth {
|
||||
*depth -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
if C::needs_postorder_traversal() {
|
||||
context.process_postorder(node);
|
||||
if D::needs_postorder_traversal() {
|
||||
traversal.process_postorder(node);
|
||||
}
|
||||
}
|
||||
|
||||
let mut data = PerLevelTraversalData {
|
||||
current_dom_depth: None,
|
||||
};
|
||||
let context = C::new(shared, root.as_node().opaque());
|
||||
|
||||
if token.traverse_unstyled_children_only() {
|
||||
for kid in root.as_node().children() {
|
||||
if kid.as_element().map_or(false, |el| el.get_data().is_none()) {
|
||||
doit::<N, C>(&context, kid, &mut data);
|
||||
doit(traversal, kid, &mut data);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
doit::<N, C>(&context, root.as_node(), &mut data);
|
||||
doit(traversal, root.as_node(), &mut data);
|
||||
}
|
||||
|
||||
// Clear the local LRU cache since we store stateful elements inside.
|
||||
context.local_context().style_sharing_candidate_cache.borrow_mut().clear();
|
||||
let tlc = traversal.create_or_get_thread_local_context();
|
||||
(*tlc).borrow().style_sharing_candidate_cache.borrow_mut().clear();
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
use atomic_refcell::{AtomicRefCell, AtomicRefMut};
|
||||
use bloom::StyleBloom;
|
||||
use context::{LocalStyleContext, SharedStyleContext, StyleContext};
|
||||
use context::{SharedStyleContext, StyleContext, ThreadLocalStyleContext};
|
||||
use data::{ElementData, StoredRestyleHint};
|
||||
use dom::{OpaqueNode, TElement, TNode};
|
||||
use matching::{MatchMethods, StyleSharingResult};
|
||||
|
@ -18,6 +18,7 @@ use servo_config::opts;
|
|||
use std::borrow::Borrow;
|
||||
use std::cell::RefCell;
|
||||
use std::mem;
|
||||
use std::rc::Rc;
|
||||
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
|
||||
use stylist::Stylist;
|
||||
|
||||
|
@ -64,9 +65,8 @@ pub fn put_thread_local_bloom_filter(bf: StyleBloom) {
|
|||
///
|
||||
/// This is mostly useful for sequential traversal, where the element will
|
||||
/// always be the last one.
|
||||
pub fn remove_from_bloom_filter<'a, E, C>(context: &C, root: OpaqueNode, element: E)
|
||||
where E: TElement,
|
||||
C: StyleContext<'a>
|
||||
pub fn remove_from_bloom_filter<E: TElement>(context: &SharedStyleContext,
|
||||
root: OpaqueNode, element: E)
|
||||
{
|
||||
trace!("[{}] remove_from_bloom_filter", ::tid::tid());
|
||||
|
||||
|
@ -78,7 +78,7 @@ pub fn remove_from_bloom_filter<'a, E, C>(context: &C, root: OpaqueNode, element
|
|||
});
|
||||
|
||||
if let Some(mut bf) = bf {
|
||||
if context.shared_context().generation == bf.generation() {
|
||||
if context.generation == bf.generation() {
|
||||
bf.maybe_pop(element);
|
||||
|
||||
// If we're the root of the reflow, just get rid of the bloom
|
||||
|
@ -127,10 +127,8 @@ impl LogBehavior {
|
|||
fn allow(&self) -> bool { match *self { MayLog => true, DontLog => false, } }
|
||||
}
|
||||
|
||||
pub trait DomTraversalContext<N: TNode> {
|
||||
type SharedContext: Sync + 'static + Borrow<SharedStyleContext>;
|
||||
|
||||
fn new<'a>(&'a Self::SharedContext, OpaqueNode) -> Self;
|
||||
pub trait DomTraversal<N: TNode> : Sync {
|
||||
type ThreadLocalContext: Borrow<ThreadLocalStyleContext>;
|
||||
|
||||
/// Process `node` on the way down, before its children have been processed.
|
||||
fn process_preorder(&self, node: N, data: &mut PerLevelTraversalData);
|
||||
|
@ -321,7 +319,9 @@ pub trait DomTraversalContext<N: TNode> {
|
|||
/// children of |element|.
|
||||
unsafe fn clear_element_data(element: &N::ConcreteElement);
|
||||
|
||||
fn local_context(&self) -> &LocalStyleContext;
|
||||
fn shared_context(&self) -> &SharedStyleContext;
|
||||
|
||||
fn create_or_get_thread_local_context(&self) -> Rc<Self::ThreadLocalContext>;
|
||||
}
|
||||
|
||||
/// Determines the amount of relations where we're going to share style.
|
||||
|
@ -337,11 +337,9 @@ pub fn relations_are_shareable(relations: &StyleRelations) -> bool {
|
|||
|
||||
/// Handles lazy resolution of style in display:none subtrees. See the comment
|
||||
/// at the callsite in query.rs.
|
||||
pub fn style_element_in_display_none_subtree<'a, E, C, F>(element: E,
|
||||
init_data: &F,
|
||||
context: &'a C) -> E
|
||||
pub fn style_element_in_display_none_subtree<E, F>(context: &StyleContext,
|
||||
element: E, init_data: &F) -> E
|
||||
where E: TElement,
|
||||
C: StyleContext<'a>,
|
||||
F: Fn(E),
|
||||
{
|
||||
// Check the base case.
|
||||
|
@ -354,7 +352,7 @@ pub fn style_element_in_display_none_subtree<'a, E, C, F>(element: E,
|
|||
|
||||
// Ensure the parent is styled.
|
||||
let parent = element.parent_element().unwrap();
|
||||
let display_none_root = style_element_in_display_none_subtree(parent, init_data, context);
|
||||
let display_none_root = style_element_in_display_none_subtree(context, parent, init_data);
|
||||
|
||||
// Initialize our data.
|
||||
init_data(element);
|
||||
|
@ -376,13 +374,13 @@ pub fn style_element_in_display_none_subtree<'a, E, C, F>(element: E,
|
|||
/// Calculates the style for a single node.
|
||||
#[inline]
|
||||
#[allow(unsafe_code)]
|
||||
pub fn recalc_style_at<'a, E, C, D>(context: &'a C,
|
||||
traversal_data: &mut PerLevelTraversalData,
|
||||
element: E,
|
||||
mut data: &mut AtomicRefMut<ElementData>)
|
||||
pub fn recalc_style_at<E, D>(traversal: &D,
|
||||
traversal_data: &mut PerLevelTraversalData,
|
||||
context: &StyleContext,
|
||||
element: E,
|
||||
mut data: &mut AtomicRefMut<ElementData>)
|
||||
where E: TElement,
|
||||
C: StyleContext<'a>,
|
||||
D: DomTraversalContext<E::ConcreteNode>
|
||||
D: DomTraversal<E::ConcreteNode>
|
||||
{
|
||||
debug_assert!(data.as_restyle().map_or(true, |r| r.snapshot.is_none()),
|
||||
"Snapshots should be expanded by the caller");
|
||||
|
@ -395,7 +393,7 @@ pub fn recalc_style_at<'a, E, C, D>(context: &'a C,
|
|||
|
||||
// Compute style for this element if necessary.
|
||||
if compute_self {
|
||||
inherited_style_changed = compute_style::<_, _, D>(context, &mut data, traversal_data, element);
|
||||
inherited_style_changed = compute_style(traversal, traversal_data, context, element, &mut data);
|
||||
}
|
||||
|
||||
// Now that matching and cascading is done, clear the bits corresponding to
|
||||
|
@ -414,7 +412,7 @@ pub fn recalc_style_at<'a, E, C, D>(context: &'a C,
|
|||
// Preprocess children, propagating restyle hints and handling sibling relationships.
|
||||
if D::should_traverse_children(element, &data, DontLog) &&
|
||||
(element.has_dirty_descendants() || !propagated_hint.is_empty() || inherited_style_changed) {
|
||||
preprocess_children::<_, _, D>(context, element, propagated_hint, inherited_style_changed);
|
||||
preprocess_children(traversal, element, propagated_hint, inherited_style_changed);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -423,15 +421,15 @@ pub fn recalc_style_at<'a, E, C, D>(context: &'a C,
|
|||
//
|
||||
// FIXME(bholley): This should differentiate between matching and cascading,
|
||||
// since we have separate bits for each now.
|
||||
fn compute_style<'a, E, C, D>(context: &'a C,
|
||||
mut data: &mut AtomicRefMut<ElementData>,
|
||||
traversal_data: &mut PerLevelTraversalData,
|
||||
element: E) -> bool
|
||||
fn compute_style<E, D>(_traversal: &D,
|
||||
traversal_data: &mut PerLevelTraversalData,
|
||||
context: &StyleContext,
|
||||
element: E,
|
||||
mut data: &mut AtomicRefMut<ElementData>) -> bool
|
||||
where E: TElement,
|
||||
C: StyleContext<'a>,
|
||||
D: DomTraversalContext<E::ConcreteNode>,
|
||||
D: DomTraversal<E::ConcreteNode>,
|
||||
{
|
||||
let shared_context = context.shared_context();
|
||||
let shared_context = context.shared;
|
||||
let mut bf = take_thread_local_bloom_filter(shared_context);
|
||||
// Ensure the bloom filter is up to date.
|
||||
let dom_depth = bf.insert_parents_recovering(element,
|
||||
|
@ -447,13 +445,13 @@ fn compute_style<'a, E, C, D>(context: &'a C,
|
|||
bf.assert_complete(element);
|
||||
|
||||
// Check to see whether we can share a style with someone.
|
||||
let style_sharing_candidate_cache =
|
||||
&mut context.local_context().style_sharing_candidate_cache.borrow_mut();
|
||||
let mut style_sharing_candidate_cache =
|
||||
context.thread_local.style_sharing_candidate_cache.borrow_mut();
|
||||
|
||||
let sharing_result = if element.parent_element().is_none() {
|
||||
StyleSharingResult::CannotShare
|
||||
} else {
|
||||
unsafe { element.share_style_if_possible(style_sharing_candidate_cache,
|
||||
unsafe { element.share_style_if_possible(&mut style_sharing_candidate_cache,
|
||||
shared_context, &mut data) }
|
||||
};
|
||||
|
||||
|
@ -522,13 +520,12 @@ fn compute_style<'a, E, C, D>(context: &'a C,
|
|||
inherited_styles_changed
|
||||
}
|
||||
|
||||
fn preprocess_children<'a, E, C, D>(context: &'a C,
|
||||
element: E,
|
||||
mut propagated_hint: StoredRestyleHint,
|
||||
parent_inherited_style_changed: bool)
|
||||
fn preprocess_children<E, D>(traversal: &D,
|
||||
element: E,
|
||||
mut propagated_hint: StoredRestyleHint,
|
||||
parent_inherited_style_changed: bool)
|
||||
where E: TElement,
|
||||
C: StyleContext<'a>,
|
||||
D: DomTraversalContext<E::ConcreteNode>
|
||||
D: DomTraversal<E::ConcreteNode>
|
||||
{
|
||||
// Loop over all the children.
|
||||
for child in element.as_node().children() {
|
||||
|
@ -554,7 +551,7 @@ fn preprocess_children<'a, E, C, D>(context: &'a C,
|
|||
}
|
||||
|
||||
// Handle element snapshots.
|
||||
let stylist = &context.shared_context().stylist;
|
||||
let stylist = &traversal.shared_context().stylist;
|
||||
let later_siblings = restyle_data.expand_snapshot(child, stylist);
|
||||
if later_siblings {
|
||||
propagated_hint.insert(&(RESTYLE_SELF | RESTYLE_DESCENDANTS).into());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue