diff options
author | Patrick Walton <pcwalton@mimiga.net> | 2014-11-11 22:07:39 -0800 |
---|---|---|
committer | Patrick Walton <pcwalton@mimiga.net> | 2014-11-14 17:31:15 -0800 |
commit | a4a9a46a87221aaa40e145602a9211e5542753a0 (patch) | |
tree | 104a88e5e9b5eb28a093127480d72ea05b52e4cc /components/gfx/display_list/optimizer.rs | |
parent | 0ab70dd539798022ebadd07fb799096809f14d3f (diff) | |
download | servo-a4a9a46a87221aaa40e145602a9211e5542753a0.tar.gz servo-a4a9a46a87221aaa40e145602a9211e5542753a0.zip |
gfx: Rewrite display list construction to make stacking-contexts more
first-class.
This implements the scheme described here:
https://groups.google.com/forum/#!topic/mozilla.dev.servo/sZVPSfPVfkg
This commit changes Servo to generate one display list per stacking
context instead of one display list per layer. This is purely a
refactoring; there are no functional changes. Performance is essentially
the same as before. However, there should be numerous future benefits
that this is intended to allow for:
* It makes the code simpler to understand because the "new layer needed"
vs. "no new layer needed" code paths are more consolidated.
* It makes it easy to support CSS properties that did not fit into our
previous flat display list model (without unconditionally layerizing
them):
o `opacity` should be easy to support because the stacking context
provides the higher-level grouping of display items to which opacity
is to be applied.
o `transform` can be easily supported because the stacking context
provides a place to stash the transformation matrix. This has the side
benefit of nicely separating the transformation matrix from the
clipping regions.
* The `flatten` logic is now O(1) instead of O(n) and now only needs to
be invoked for pseudo-stacking contexts (right now: just floats),
instead of for every stacking context.
* Layers are now a proper tree instead of a flat list as far as layout
is concerned, bringing us closer to a production-quality
compositing/layers framework.
* This commit opens the door to incremental display list construction at
the level of stacking contexts.
Future performance improvements could come from optimizing allocation of
display list items, and, of course, incremental display list
construction.
Diffstat (limited to 'components/gfx/display_list/optimizer.rs')
-rw-r--r-- | components/gfx/display_list/optimizer.rs | 64 |
1 files changed, 39 insertions, 25 deletions
diff --git a/components/gfx/display_list/optimizer.rs b/components/gfx/display_list/optimizer.rs index 69cdc469de3..9ce3fbfc3f1 100644 --- a/components/gfx/display_list/optimizer.rs +++ b/components/gfx/display_list/optimizer.rs @@ -2,52 +2,66 @@ * 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 display_list::{DisplayItem, DisplayList}; +//! Transforms a display list to produce a visually-equivalent, but cheaper-to-render, one. + +use display_list::{DisplayItem, DisplayList, StackingContext}; use collections::dlist::DList; use geom::rect::Rect; -use servo_util::geometry::Au; +use servo_util::geometry::{mod, Au}; use sync::Arc; +/// Transforms a display list to produce a visually-equivalent, but cheaper-to-render, one. pub struct DisplayListOptimizer { - display_list: Arc<DisplayList>, /// The visible rect in page coordinates. visible_rect: Rect<Au>, } impl DisplayListOptimizer { - /// `visible_rect` specifies the visible rect in page coordinates. - pub fn new(display_list: Arc<DisplayList>, visible_rect: Rect<Au>) -> DisplayListOptimizer { + /// Creates a new display list optimizer object. `visible_rect` specifies the visible rect in + /// page coordinates. + pub fn new(visible_rect: &Rect<f32>) -> DisplayListOptimizer { DisplayListOptimizer { - display_list: display_list, - visible_rect: visible_rect, + visible_rect: geometry::f32_rect_to_au_rect(*visible_rect), } } - pub fn optimize(self) -> DisplayList { - self.process_display_list(&*self.display_list) + /// Optimizes the given display list, returning an equivalent, but cheaper-to-paint, one. + pub fn optimize(self, display_list: &DisplayList) -> DisplayList { + let mut result = DisplayList::new(); + self.add_in_bounds_display_items(&mut result.background_and_borders, + display_list.background_and_borders.iter()); + self.add_in_bounds_display_items(&mut result.block_backgrounds_and_borders, + display_list.block_backgrounds_and_borders.iter()); + self.add_in_bounds_display_items(&mut result.floats, display_list.floats.iter()); + self.add_in_bounds_display_items(&mut result.content, display_list.content.iter()); + self.add_in_bounds_stacking_contexts(&mut result.children, display_list.children.iter()); + result } - fn process_display_list(&self, display_list: &DisplayList) -> DisplayList { - let mut result = DList::new(); - for item in display_list.iter() { - match self.process_display_item(item) { - None => {} - Some(display_item) => result.push_back(display_item), + /// Adds display items that intersect the visible rect to `result_list`. + fn add_in_bounds_display_items<'a,I>(&self, + result_list: &mut DList<DisplayItem>, + mut display_items: I) + where I: Iterator<&'a DisplayItem> { + for display_item in display_items { + if self.visible_rect.intersects(&display_item.base().bounds) && + self.visible_rect.intersects(&display_item.base().clip_rect) { + result_list.push_back((*display_item).clone()) } } - DisplayList { - list: result, - } } - fn process_display_item(&self, display_item: &DisplayItem) -> Option<DisplayItem> { - // Eliminate display items outside the visible region. - if !self.visible_rect.intersects(&display_item.base().bounds) || - !self.visible_rect.intersects(&display_item.base().clip_rect) { - None - } else { - Some((*display_item).clone()) + /// Adds child stacking contexts whose boundaries intersect the visible rect to `result_list`. + fn add_in_bounds_stacking_contexts<'a,I>(&self, + result_list: &mut DList<Arc<StackingContext>>, + mut stacking_contexts: I) + where I: Iterator<&'a Arc<StackingContext>> { + for stacking_context in stacking_contexts { + if self.visible_rect.intersects(&stacking_context.bounds) && + self.visible_rect.intersects(&stacking_context.clip_rect) { + result_list.push_back((*stacking_context).clone()) + } } } } |