Refactor document damage to distinguish it from layout/style damage.

Also, standardize on the name "reflow" instead of "relayout" or "build".
This commit is contained in:
Patrick Walton 2013-06-04 22:00:33 -07:00
parent 40a69fc517
commit 7a435fc6ed
4 changed files with 106 additions and 58 deletions

View file

@ -32,11 +32,12 @@ use newcss::stylesheet::Stylesheet;
use newcss::types::OriginAuthor; use newcss::types::OriginAuthor;
use script::dom::event::ReflowEvent; use script::dom::event::ReflowEvent;
use script::dom::node::{AbstractNode, LayoutView}; use script::dom::node::{AbstractNode, LayoutView};
use script::layout_interface::{AddStylesheetMsg, BuildData, BuildMsg, ContentBoxQuery}; use script::layout_interface::{AddStylesheetMsg, ContentBoxQuery};
use script::layout_interface::{HitTestQuery, ContentBoxResponse, HitTestResponse}; use script::layout_interface::{HitTestQuery, ContentBoxResponse, HitTestResponse};
use script::layout_interface::{ContentBoxesQuery, ContentBoxesResponse, ExitMsg, LayoutQuery}; use script::layout_interface::{ContentBoxesQuery, ContentBoxesResponse, ExitMsg, LayoutQuery};
use script::layout_interface::{LayoutResponse, LayoutTask, MatchSelectorsDamage, Msg, NoDamage}; use script::layout_interface::{LayoutResponse, LayoutTask, MatchSelectorsDocumentDamage, Msg};
use script::layout_interface::{QueryMsg, ReflowDamage, ReflowForDisplay}; use script::layout_interface::{QueryMsg, Reflow, ReflowDocumentDamage, ReflowForDisplay};
use script::layout_interface::{ReflowMsg};
use script::script_task::{ScriptMsg, SendEventMsg}; use script::script_task::{ScriptMsg, SendEventMsg};
use servo_net::image_cache_task::{ImageCacheTask, ImageResponseMsg}; use servo_net::image_cache_task::{ImageCacheTask, ImageResponseMsg};
use servo_net::local_image_cache::LocalImageCache; use servo_net::local_image_cache::LocalImageCache;
@ -126,11 +127,11 @@ impl Layout {
fn handle_request(&mut self) -> bool { fn handle_request(&mut self) -> bool {
match self.from_script.recv() { match self.from_script.recv() {
AddStylesheetMsg(sheet) => self.handle_add_stylesheet(sheet), AddStylesheetMsg(sheet) => self.handle_add_stylesheet(sheet),
BuildMsg(data) => { ReflowMsg(data) => {
let data = Cell(data); let data = Cell(data);
do profile(time::LayoutPerformCategory, self.profiler_chan.clone()) { do profile(time::LayoutPerformCategory, self.profiler_chan.clone()) {
self.handle_build(data.take()); self.handle_reflow(data.take());
} }
} }
QueryMsg(query, chan) => { QueryMsg(query, chan) => {
@ -154,10 +155,10 @@ impl Layout {
} }
/// The high-level routine that performs layout tasks. /// The high-level routine that performs layout tasks.
fn handle_build(&mut self, data: &BuildData) { fn handle_reflow(&mut self, data: &Reflow) {
// FIXME: Isolate this transmutation into a "bridge" module. // FIXME: Isolate this transmutation into a "bridge" module.
let node: &AbstractNode<LayoutView> = unsafe { let node: &AbstractNode<LayoutView> = unsafe {
transmute(&data.node) transmute(&data.document_root)
}; };
// FIXME: Bad copy! // FIXME: Bad copy!
@ -187,9 +188,9 @@ impl Layout {
} }
// Perform CSS selector matching if necessary. // Perform CSS selector matching if necessary.
match data.damage { match data.damage.level {
NoDamage | ReflowDamage => {} ReflowDocumentDamage => {}
MatchSelectorsDamage => { MatchSelectorsDocumentDamage => {
do profile(time::LayoutSelectorMatchCategory, self.profiler_chan.clone()) { do profile(time::LayoutSelectorMatchCategory, self.profiler_chan.clone()) {
node.restyle_subtree(self.css_select_ctx); node.restyle_subtree(self.css_select_ctx);
} }

View file

@ -4,7 +4,7 @@
use dom::bindings::utils::WrapperCache; use dom::bindings::utils::WrapperCache;
use dom::bindings::window; use dom::bindings::window;
use layout_interface::MatchSelectorsDamage; use layout_interface::ReflowForScriptQuery;
use script_task::{ExitMsg, FireTimerMsg, ScriptMsg, ScriptContext}; use script_task::{ExitMsg, FireTimerMsg, ScriptMsg, ScriptContext};
use core::comm::{Chan, SharedChan}; use core::comm::{Chan, SharedChan};
@ -83,7 +83,7 @@ pub impl Window {
fn content_changed(&self) { fn content_changed(&self) {
unsafe { unsafe {
(*self.script_context).trigger_relayout(MatchSelectorsDamage); (*self.script_context).reflow_all(ReflowForScriptQuery)
} }
} }

View file

@ -25,9 +25,7 @@ pub enum Msg {
AddStylesheetMsg(Stylesheet), AddStylesheetMsg(Stylesheet),
/// Requests a reflow. /// Requests a reflow.
/// ReflowMsg(~Reflow),
/// FIXME(pcwalton): Call this `reflow` instead?
BuildMsg(~BuildData),
/// Performs a synchronous layout request. /// Performs a synchronous layout request.
/// ///
@ -61,31 +59,37 @@ pub enum LayoutResponse {
HitTestResponse(AbstractNode<LayoutView>), HitTestResponse(AbstractNode<LayoutView>),
} }
/// Dirty bits for layout. /// Determines which part of the
pub enum Damage { pub enum DocumentDamageLevel {
/// The document is clean; nothing needs to be done.
NoDamage,
/// Reflow, but do not perform CSS selector matching.
ReflowDamage,
/// Perform CSS selector matching and reflow. /// Perform CSS selector matching and reflow.
MatchSelectorsDamage, MatchSelectorsDocumentDamage,
/// Reflow, but do not perform CSS selector matching.
ReflowDocumentDamage,
} }
impl Damage { impl DocumentDamageLevel {
/// Sets this damage to the maximum of this damage and the given damage. /// Sets this damage to the maximum of this damage and the given damage.
/// ///
/// FIXME(pcwalton): This could be refactored to use `max` and the `Ord` trait, and this /// FIXME(pcwalton): This could be refactored to use `max` and the `Ord` trait, and this
/// function removed. /// function removed.
fn add(&mut self, new_damage: Damage) { fn add(&mut self, new_damage: DocumentDamageLevel) {
match (*self, new_damage) { match (*self, new_damage) {
(NoDamage, _) => *self = new_damage, (ReflowDocumentDamage, new_damage) => *self = new_damage,
(ReflowDamage, NoDamage) => *self = ReflowDamage, (MatchSelectorsDocumentDamage, _) => *self = MatchSelectorsDocumentDamage,
(ReflowDamage, new_damage) => *self = new_damage,
(MatchSelectorsDamage, _) => *self = MatchSelectorsDamage
} }
} }
} }
/// What parts of the document have changed, as far as the script task can tell.
///
/// Note that this is fairly coarse-grained and is separate from layout's notion of the document
pub struct DocumentDamage {
/// The topmost node in the tree that has changed.
root: AbstractNode<ScriptView>,
/// The amount of damage that occurred.
level: DocumentDamageLevel,
}
/// Why we're doing reflow. /// Why we're doing reflow.
#[deriving(Eq)] #[deriving(Eq)]
pub enum ReflowGoal { pub enum ReflowGoal {
@ -96,10 +100,11 @@ pub enum ReflowGoal {
} }
/// Information needed for a reflow. /// Information needed for a reflow.
pub struct BuildData { pub struct Reflow {
node: AbstractNode<ScriptView>, /// The document node.
/// What reflow needs to be done. document_root: AbstractNode<ScriptView>,
damage: Damage, /// The style changes that need to be done.
damage: DocumentDamage,
/// The goal of reflow: either to render to the screen or to flush layout info for script. /// The goal of reflow: either to render to the screen or to flush layout info for script.
goal: ReflowGoal, goal: ReflowGoal,
/// The URL of the page. /// The URL of the page.
@ -108,6 +113,7 @@ pub struct BuildData {
script_chan: SharedChan<ScriptMsg>, script_chan: SharedChan<ScriptMsg>,
/// The current window size. /// The current window size.
window_size: Size2D<uint>, window_size: Size2D<uint>,
/// The channel that we send a notification to.
script_join_chan: Chan<()>, script_join_chan: Chan<()>,
} }

View file

@ -8,11 +8,12 @@
use dom::bindings::utils::GlobalStaticData; use dom::bindings::utils::GlobalStaticData;
use dom::document::Document; use dom::document::Document;
use dom::event::{Event, ResizeEvent, ReflowEvent, ClickEvent}; use dom::event::{Event, ResizeEvent, ReflowEvent, ClickEvent};
use dom::node::define_bindings; use dom::node::{AbstractNode, ScriptView, define_bindings};
use dom::window::Window; use dom::window::Window;
use layout_interface::{AddStylesheetMsg, BuildData, BuildMsg, Damage, LayoutQuery, HitTestQuery}; use layout_interface::{AddStylesheetMsg, DocumentDamage, DocumentDamageLevel, HitTestQuery};
use layout_interface::{LayoutResponse, HitTestResponse, LayoutTask, MatchSelectorsDamage, NoDamage}; use layout_interface::{HitTestResponse, LayoutQuery, LayoutResponse, LayoutTask};
use layout_interface::{QueryMsg, ReflowDamage, ReflowForDisplay, ReflowForScriptQuery, ReflowGoal}; use layout_interface::{MatchSelectorsDocumentDamage, QueryMsg, Reflow, ReflowDocumentDamage};
use layout_interface::{ReflowForDisplay, ReflowForScriptQuery, ReflowGoal, ReflowMsg};
use layout_interface; use layout_interface;
use core::cast::transmute; use core::cast::transmute;
@ -132,8 +133,8 @@ pub struct ScriptContext {
/// The current size of the window, in pixels. /// The current size of the window, in pixels.
window_size: Size2D<uint>, window_size: Size2D<uint>,
/// What parts of layout are dirty. /// What parts of the document are dirty, if any.
damage: Damage, damage: Option<DocumentDamage>,
} }
fn global_script_context_key(_: @ScriptContext) {} fn global_script_context_key(_: @ScriptContext) {}
@ -199,7 +200,7 @@ impl ScriptContext {
root_frame: None, root_frame: None,
window_size: Size2D(800, 600), window_size: Size2D(800, 600),
damage: MatchSelectorsDamage, damage: None,
}; };
// Indirection for Rust Issue #6248, dynamic freeze scope artifically extended // Indirection for Rust Issue #6248, dynamic freeze scope artifically extended
let script_context_ptr = { let script_context_ptr = {
@ -283,7 +284,7 @@ impl ScriptContext {
null(), null(),
&rval); &rval);
self.relayout(ReflowForScriptQuery) self.reflow(ReflowForScriptQuery)
} }
/// Handles a request to exit the script task and shut down layout. /// Handles a request to exit the script task and shut down layout.
@ -348,8 +349,11 @@ impl ScriptContext {
}); });
// Perform the initial reflow. // Perform the initial reflow.
self.damage.add(MatchSelectorsDamage); self.damage = Some(DocumentDamage {
self.relayout(ReflowForDisplay); root: root_node,
level: MatchSelectorsDocumentDamage,
});
self.reflow(ReflowForDisplay);
// Define debug functions. // Define debug functions.
self.js_compartment.define_functions(debug_fns); self.js_compartment.define_functions(debug_fns);
@ -383,19 +387,13 @@ impl ScriptContext {
} }
} }
/// Initiate an asynchronous relayout operation to handle a script layout query.
pub fn trigger_relayout(&mut self, damage: Damage) {
self.damage.add(damage);
self.relayout(ReflowForScriptQuery);
}
/// This method will wait until the layout task has completed its current action, join the /// This method will wait until the layout task has completed its current action, join the
/// layout task, and then request a new layout run. It won't wait for the new layout /// layout task, and then request a new layout run. It won't wait for the new layout
/// computation to finish. /// computation to finish.
/// ///
/// This function fails if there is no root frame. /// This function fails if there is no root frame.
fn relayout(&mut self, goal: ReflowGoal) { fn reflow(&mut self, goal: ReflowGoal) {
debug!("script: performing relayout"); debug!("script: performing reflow");
// Now, join the layout so that they will see the latest changes we have made. // Now, join the layout so that they will see the latest changes we have made.
self.join_layout(); self.join_layout();
@ -408,23 +406,36 @@ impl ScriptContext {
None => fail!(~"Tried to relayout with no root frame!"), None => fail!(~"Tried to relayout with no root frame!"),
Some(ref root_frame) => { Some(ref root_frame) => {
// Send new document and relevant styles to layout. // Send new document and relevant styles to layout.
let data = ~BuildData { let reflow = ~Reflow {
node: root_frame.document.root, document_root: root_frame.document.root,
url: copy root_frame.url, url: copy root_frame.url,
goal: goal, goal: goal,
script_chan: self.script_chan.clone(), script_chan: self.script_chan.clone(),
window_size: self.window_size, window_size: self.window_size,
script_join_chan: join_chan, script_join_chan: join_chan,
damage: replace(&mut self.damage, NoDamage), damage: replace(&mut self.damage, None).unwrap(),
}; };
self.layout_task.chan.send(BuildMsg(data)) self.layout_task.chan.send(ReflowMsg(reflow))
} }
} }
debug!("script: layout forked") debug!("script: layout forked")
} }
/// Reflows the entire document.
///
/// FIXME: This should basically never be used.
pub fn reflow_all(&mut self, goal: ReflowGoal) {
for self.root_frame.each |root_frame| {
ScriptContext::damage(&mut self.damage,
root_frame.document.root,
MatchSelectorsDocumentDamage)
}
self.reflow(goal)
}
/// Sends the given query to layout. /// Sends the given query to layout.
pub fn query_layout(&mut self, query: LayoutQuery) -> Result<LayoutResponse,()> { pub fn query_layout(&mut self, query: LayoutQuery) -> Result<LayoutResponse,()> {
self.join_layout(); self.join_layout();
@ -434,6 +445,26 @@ impl ScriptContext {
response_port.recv() response_port.recv()
} }
/// Adds the given damage.
fn damage(damage: &mut Option<DocumentDamage>,
root: AbstractNode<ScriptView>,
level: DocumentDamageLevel) {
match *damage {
None => {}
Some(ref mut damage) => {
// FIXME(pcwalton): This is wrong. We should trace up to the nearest ancestor.
damage.root = root;
damage.level.add(level);
return
}
}
*damage = Some(DocumentDamage {
root: root,
level: level,
})
}
/// This is the main entry point for receiving and dispatching DOM events. /// This is the main entry point for receiving and dispatching DOM events.
/// ///
/// TODO: Actually perform DOM event dispatch. /// TODO: Actually perform DOM event dispatch.
@ -442,23 +473,33 @@ impl ScriptContext {
ResizeEvent(new_width, new_height, response_chan) => { ResizeEvent(new_width, new_height, response_chan) => {
debug!("script got resize event: %u, %u", new_width, new_height); debug!("script got resize event: %u, %u", new_width, new_height);
self.damage.add(ReflowDamage);
self.window_size = Size2D(new_width, new_height); self.window_size = Size2D(new_width, new_height);
for self.root_frame.each |root_frame| {
ScriptContext::damage(&mut self.damage,
root_frame.document.root,
ReflowDocumentDamage);
}
if self.root_frame.is_some() { if self.root_frame.is_some() {
self.relayout(ReflowForDisplay) self.reflow(ReflowForDisplay)
} }
response_chan.send(()) response_chan.send(())
} }
// FIXME(pcwalton): This reflows the entire document and is not incremental-y.
ReflowEvent => { ReflowEvent => {
debug!("script got reflow event"); debug!("script got reflow event");
self.damage.add(MatchSelectorsDamage); for self.root_frame.each |root_frame| {
ScriptContext::damage(&mut self.damage,
root_frame.document.root,
MatchSelectorsDocumentDamage);
}
if self.root_frame.is_some() { if self.root_frame.is_some() {
self.relayout(ReflowForDisplay) self.reflow(ReflowForDisplay)
} }
} }