servo/components/util/dlist.rs
Patrick Walton a4a9a46a87 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.
2014-11-14 17:31:15 -08:00

125 lines
3.6 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
//! Utility functions for doubly-linked lists.
use std::collections::DList;
use std::mem;
use std::ptr;
struct RawDList<T> {
length: uint,
head: *mut RawNode<T>,
tail: *mut RawNode<T>,
}
#[allow(dead_code)]
struct RawNode<T> {
next: *mut RawNode<T>,
prev: *mut RawNode<T>,
value: T,
}
#[unsafe_destructor]
impl<T> Drop for RawDList<T> {
fn drop(&mut self) {
panic!("shouldn't happen")
}
}
/// Workaround for a missing method on Rust's `DList` type. Splits the head off a list in O(1)
/// time.
pub fn split<T>(list: &mut DList<T>) -> DList<T> {
let list = unsafe {
mem::transmute::<&mut DList<T>,&mut RawDList<T>>(list)
};
if list.length == 0 {
panic!("split_dlist(): empty list")
}
let head_node = mem::replace(&mut list.head, ptr::null_mut());
let head_list = RawDList {
length: 1,
head: head_node,
tail: head_node,
};
debug_assert!(list.head.is_null());
unsafe {
mem::swap(&mut (*head_list.head).next, &mut list.head);
debug_assert!((*head_list.head).next.is_null());
debug_assert!((*head_list.head).prev.is_null());
(*head_list.head).prev = ptr::null_mut();
}
list.length -= 1;
if list.length == 0 {
list.tail = ptr::null_mut()
} else {
if list.length == 1 {
list.tail = list.head
}
unsafe {
(*list.head).prev = ptr::null_mut()
}
}
unsafe {
mem::transmute::<RawDList<T>,DList<T>>(head_list)
}
}
/// Appends the items in the other list to this one, leaving the other list empty.
#[inline]
pub fn append_from<T>(this: &mut DList<T>, other: &mut DList<T>) {
unsafe {
let this = mem::transmute::<&mut DList<T>,&mut RawDList<T>>(this);
let other = mem::transmute::<&mut DList<T>,&mut RawDList<T>>(other);
if this.length == 0 {
this.head = mem::replace(&mut other.head, ptr::null_mut());
this.tail = mem::replace(&mut other.tail, ptr::null_mut());
this.length = mem::replace(&mut other.length, 0);
return
}
let old_other_head = mem::replace(&mut other.head, ptr::null_mut());
if old_other_head.is_null() {
return
}
(*old_other_head).prev = this.tail;
(*this.tail).next = old_other_head;
this.tail = mem::replace(&mut other.tail, ptr::null_mut());
this.length += other.length;
other.length = 0;
}
}
/// Prepends the items in the other list to this one, leaving the other list empty.
#[inline]
pub fn prepend_from<T>(this: &mut DList<T>, other: &mut DList<T>) {
unsafe {
let this = mem::transmute::<&mut DList<T>,&mut RawDList<T>>(this);
let other = mem::transmute::<&mut DList<T>,&mut RawDList<T>>(other);
if this.length == 0 {
this.head = mem::replace(&mut other.head, ptr::null_mut());
this.tail = mem::replace(&mut other.tail, ptr::null_mut());
this.length = mem::replace(&mut other.length, 0);
return
}
let old_other_tail = mem::replace(&mut other.tail, ptr::null_mut());
if old_other_tail.is_null() {
return
}
(*old_other_tail).next = this.head;
(*this.head).prev = old_other_tail;
this.head = mem::replace(&mut other.head, ptr::null_mut());
this.length += other.length;
other.length = 0;
}
}