Make the DomTraversalContext own the SharedStyleContext and share it immutably across the traversal.

This allows us to get rid of a bunch of lifetimes and simplify a lot of code. It
also lets us get rid of that nasty lifetime transmute, which is awesome.

The situation with thread-local contexts is still suboptimal, but we fix that in
subsequent patches.
This commit is contained in:
Bobby Holley 2016-12-15 16:00:40 -08:00
parent 1b2daae453
commit 648ce1e44e
18 changed files with 329 additions and 323 deletions

View file

@ -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
}
}

View file

@ -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)
}
}