Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement concept of dirty root
  • Loading branch information
nox committed May 19, 2020
1 parent 518c066 commit 036f123
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 64 deletions.
17 changes: 17 additions & 0 deletions components/layout/traversal.rs
Expand Up @@ -30,6 +30,10 @@ impl<'a> RecalcStyleAndConstructFlows<'a> {
RecalcStyleAndConstructFlows { context: context }
}

pub fn context(&self) -> &LayoutContext<'a> {
&self.context
}

/// Consumes this traversal context, returning ownership of the shared layout
/// context to the caller.
pub fn destroy(self) -> LayoutContext<'a> {
Expand Down Expand Up @@ -183,6 +187,19 @@ where
fn process(&mut self, node: &ConcreteThreadSafeLayoutNode);
}

#[allow(unsafe_code)]
#[inline]
pub unsafe fn construct_flows_at_ancestors<'dom>(
context: &LayoutContext,
mut node: impl LayoutNode<'dom>,
) {
while let Some(element) = node.traversal_parent() {
element.set_dirty_descendants();
node = element.as_node();
construct_flows_at(context, node);
}
}

/// The flow construction traversal, which builds flows for styled nodes.
#[inline]
#[allow(unsafe_code)]
Expand Down
10 changes: 8 additions & 2 deletions components/layout/wrapper.rs
Expand Up @@ -34,7 +34,7 @@ use crate::data::{LayoutData, LayoutDataFlags, StyleAndLayoutData};
use atomic_refcell::{AtomicRef, AtomicRefMut};
use script_layout_interface::wrapper_traits::GetStyleAndOpaqueLayoutData;
use script_layout_interface::wrapper_traits::{ThreadSafeLayoutElement, ThreadSafeLayoutNode};
use style::dom::{NodeInfo, TNode};
use style::dom::{NodeInfo, TElement, TNode};
use style::selector_parser::RestyleDamage;
use style::values::computed::counters::ContentItem;
use style::values::generics::counters::Content;
Expand Down Expand Up @@ -148,7 +148,13 @@ where
}

let damage = {
let data = node.get_style_and_layout_data().unwrap();
let data = match node.get_style_and_layout_data() {
Some(data) => data,
None => panic!(
"could not get style and layout data for <{}>",
node.as_element().unwrap().local_name()
),
};

if !data
.layout_data
Expand Down
42 changes: 27 additions & 15 deletions components/layout_thread/lib.rs
Expand Up @@ -57,7 +57,8 @@ use layout::query::{process_node_scroll_area_request, process_node_scroll_id_req
use layout::query::{process_offset_parent_query, process_resolved_style_request};
use layout::sequential;
use layout::traversal::{
ComputeStackingRelativePositions, PreorderFlowTraversal, RecalcStyleAndConstructFlows,
construct_flows_at_ancestors, ComputeStackingRelativePositions, PreorderFlowTraversal,
RecalcStyleAndConstructFlows,
};
use layout::wrapper::LayoutNodeLayoutData;
use layout_traits::LayoutThreadFactory;
Expand Down Expand Up @@ -1200,7 +1201,7 @@ impl LayoutThread {
.expect("layout: wrong layout query timestamp");
};

let element = match document.root_element() {
let root_element = match document.root_element() {
None => {
// Since we cannot compute anything, give spec-required placeholders.
debug!("layout: No root node: bailing");
Expand Down Expand Up @@ -1250,9 +1251,9 @@ impl LayoutThread {

debug!(
"layout: processing reflow request for: {:?} ({}) (query={:?})",
element, self.url, data.reflow_goal
root_element, self.url, data.reflow_goal
);
trace!("{:?}", ShowSubtree(element.as_node()));
trace!("{:?}", ShowSubtree(root_element.as_node()));

let initial_viewport = data.window_size.initial_viewport;
let device_pixel_ratio = data.window_size.device_pixel_ratio;
Expand Down Expand Up @@ -1309,7 +1310,7 @@ impl LayoutThread {
.unwrap();
}
if had_used_viewport_units {
if let Some(mut data) = element.mutate_data() {
if let Some(mut data) = root_element.mutate_data() {
data.hint.insert(RestyleHint::recascade_subtree());
}
}
Expand Down Expand Up @@ -1344,7 +1345,7 @@ impl LayoutThread {
}

if viewport_size_changed {
if let Some(mut flow) = self.try_get_layout_root(element.as_node()) {
if let Some(mut flow) = self.try_get_layout_root(root_element.as_node()) {
LayoutThread::reflow_all_nodes(FlowRef::deref_mut(&mut flow));
}
}
Expand Down Expand Up @@ -1395,7 +1396,7 @@ impl LayoutThread {
debug!("Noting restyle for {:?}: {:?}", el, style_data);
}

self.stylist.flush(&guards, Some(element), Some(&map));
self.stylist.flush(&guards, Some(root_element), Some(&map));

// Create a layout context for use throughout the following passes.
let mut layout_context = self.build_layout_context(
Expand All @@ -1414,13 +1415,19 @@ impl LayoutThread {
(None, 1)
};

let dirty_root = unsafe {
ServoLayoutNode::new(&data.dirty_root.unwrap())
.as_element()
.unwrap()
};

let traversal = RecalcStyleAndConstructFlows::new(layout_context);
let token = {
let shared =
<RecalcStyleAndConstructFlows as DomTraversal<ServoLayoutElement>>::shared_context(
&traversal,
);
RecalcStyleAndConstructFlows::pre_traverse(element, shared)
RecalcStyleAndConstructFlows::pre_traverse(dirty_root, shared)
};

if token.should_traverse() {
Expand All @@ -1431,11 +1438,13 @@ impl LayoutThread {
self.time_profiler_chan.clone(),
|| {
// Perform CSS selector matching and flow construction.
driver::traverse_dom::<ServoLayoutElement, RecalcStyleAndConstructFlows>(
&traversal,
token,
thread_pool,
);
let root = driver::traverse_dom::<
ServoLayoutElement,
RecalcStyleAndConstructFlows,
>(&traversal, token, thread_pool);
unsafe {
construct_flows_at_ancestors(traversal.context(), root.as_node());
}
},
);
// TODO(pcwalton): Measure energy usage of text shaping, perhaps?
Expand All @@ -1452,7 +1461,7 @@ impl LayoutThread {
);

// Retrieve the (possibly rebuilt) root flow.
*self.root_flow.borrow_mut() = self.try_get_layout_root(element.as_node());
*self.root_flow.borrow_mut() = self.try_get_layout_root(root_element.as_node());
}

for element in elements_with_snapshot {
Expand All @@ -1462,7 +1471,10 @@ impl LayoutThread {
layout_context = traversal.destroy();

if self.dump_style_tree {
println!("{:?}", ShowSubtreeDataAndPrimaryValues(element.as_node()));
println!(
"{:?}",
ShowSubtreeDataAndPrimaryValues(root_element.as_node())
);
}

if self.dump_rule_tree {
Expand Down
23 changes: 11 additions & 12 deletions components/layout_thread_2020/lib.rs
Expand Up @@ -87,7 +87,6 @@ use style::dom::{TDocument, TElement, TNode};
use style::driver;
use style::error_reporting::RustLogReporter;
use style::global_style_data::{GLOBAL_STYLE_DATA, STYLE_THREAD_POOL};
use style::invalidation::element::restyle_hints::RestyleHint;
use style::media_queries::{Device, MediaList, MediaType};
use style::properties::PropertyId;
use style::selector_parser::SnapshotMap;
Expand Down Expand Up @@ -888,7 +887,7 @@ impl LayoutThread {

let mut rw_data = possibly_locked_rw_data.lock();

let element = match document.root_element() {
let root_element = match document.root_element() {
None => {
// Since we cannot compute anything, give spec-required placeholders.
debug!("layout: No root node: bailing");
Expand Down Expand Up @@ -959,7 +958,6 @@ impl LayoutThread {
ua_or_user: &ua_or_user_guard,
};

let had_used_viewport_units = self.stylist.device().used_viewport_units();
let device = Device::new(MediaType::screen(), initial_viewport, device_pixel_ratio);
let sheet_origins_affected_by_device_change = self.stylist.set_device(device, &guards);

Expand Down Expand Up @@ -987,11 +985,6 @@ impl LayoutThread {
))
.unwrap();
}
if had_used_viewport_units {
if let Some(mut data) = element.mutate_data() {
data.hint.insert(RestyleHint::recascade_subtree());
}
}
}

if self.first_reflow.get() {
Expand Down Expand Up @@ -1059,16 +1052,22 @@ impl LayoutThread {
debug!("Noting restyle for {:?}: {:?}", el, style_data);
}

self.stylist.flush(&guards, Some(element), Some(&map));
self.stylist.flush(&guards, Some(root_element), Some(&map));

// Create a layout context for use throughout the following passes.
let mut layout_context =
self.build_layout_context(guards.clone(), &map, origin, data.animation_timeline_value);

let dirty_root = unsafe {
ServoLayoutNode::new(&data.dirty_root.unwrap())
.as_element()
.unwrap()
};

let traversal = RecalcStyle::new(layout_context);
let token = {
let shared = DomTraversal::<ServoLayoutElement>::shared_context(&traversal);
RecalcStyle::pre_traverse(element, shared)
RecalcStyle::pre_traverse(dirty_root, shared)
};

let rayon_pool = STYLE_THREAD_POOL.pool();
Expand All @@ -1077,7 +1076,7 @@ impl LayoutThread {
let box_tree = if token.should_traverse() {
driver::traverse_dom(&traversal, token, rayon_pool);

let root_node = document.root_element().unwrap().as_node();
let root_node = root_element.as_node();
let build_box_tree = || BoxTree::construct(traversal.context(), root_node);
let box_tree = if let Some(pool) = rayon_pool {
pool.install(build_box_tree)
Expand Down Expand Up @@ -1114,7 +1113,7 @@ impl LayoutThread {
if self.dump_style_tree {
println!(
"{:?}",
style::dom::ShowSubtreeDataAndPrimaryValues(element.as_node())
style::dom::ShowSubtreeDataAndPrimaryValues(root_element.as_node())
);
}

Expand Down

0 comments on commit 036f123

Please sign in to comment.