mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Bug 1317016 - Basic infrastructure for RestyleHint-driven traversal.
MozReview-Commit-ID: 7wH5XcILVmX
This commit is contained in:
parent
e1eff691f8
commit
992f7dddf4
35 changed files with 1465 additions and 901 deletions
|
@ -4,16 +4,48 @@
|
|||
|
||||
//! Per-node data used in style calculation.
|
||||
|
||||
use dom::TRestyleDamage;
|
||||
use properties::ComputedValues;
|
||||
use properties::longhands::display::computed_value as display;
|
||||
use restyle_hints::RestyleHint;
|
||||
use rule_tree::StrongRuleNode;
|
||||
use selector_parser::PseudoElement;
|
||||
use selector_parser::{PseudoElement, RestyleDamage, Snapshot};
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use std::hash::BuildHasherDefault;
|
||||
use std::mem;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::sync::Arc;
|
||||
|
||||
type PseudoStylesInner = HashMap<PseudoElement, (Arc<ComputedValues>, StrongRuleNode),
|
||||
#[derive(Clone)]
|
||||
pub struct ComputedStyle {
|
||||
/// The rule node representing the ordered list of rules matched for this
|
||||
/// node.
|
||||
pub rules: StrongRuleNode,
|
||||
|
||||
/// The computed values for each property obtained by cascading the
|
||||
/// matched rules.
|
||||
pub values: Arc<ComputedValues>,
|
||||
}
|
||||
|
||||
impl ComputedStyle {
|
||||
pub fn new(rules: StrongRuleNode, values: Arc<ComputedValues>) -> Self {
|
||||
ComputedStyle {
|
||||
rules: rules,
|
||||
values: values,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We manually implement Debug for ComputedStyle so tht we can avoid the verbose
|
||||
// stringification of ComputedValues for normal logging.
|
||||
impl fmt::Debug for ComputedStyle {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "ComputedStyle {{ rules: {:?}, values: {{..}} }}", self.rules)
|
||||
}
|
||||
}
|
||||
|
||||
type PseudoStylesInner = HashMap<PseudoElement, ComputedStyle,
|
||||
BuildHasherDefault<::fnv::FnvHasher>>;
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PseudoStyles(PseudoStylesInner);
|
||||
|
@ -37,56 +69,124 @@ impl DerefMut for PseudoStyles {
|
|||
/// pseudo-elements.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ElementStyles {
|
||||
/// The results of CSS styling for this node.
|
||||
pub primary: Arc<ComputedValues>,
|
||||
|
||||
/// The rule node representing the last rule matched for this node.
|
||||
pub rule_node: StrongRuleNode,
|
||||
|
||||
/// The results of CSS styling for each pseudo-element (if any).
|
||||
pub primary: ComputedStyle,
|
||||
pub pseudos: PseudoStyles,
|
||||
}
|
||||
|
||||
impl ElementStyles {
|
||||
pub fn new(primary: Arc<ComputedValues>, rule_node: StrongRuleNode) -> Self {
|
||||
pub fn new(primary: ComputedStyle) -> Self {
|
||||
ElementStyles {
|
||||
primary: primary,
|
||||
rule_node: rule_node,
|
||||
pseudos: PseudoStyles::empty(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_display_none(&self) -> bool {
|
||||
self.primary.values.get_box().clone_display() == display::T::none
|
||||
}
|
||||
}
|
||||
|
||||
/// Enum to describe the different requirements that a restyle hint may impose
|
||||
/// on its descendants.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum DescendantRestyleHint {
|
||||
/// This hint does not require any descendants to be restyled.
|
||||
Empty,
|
||||
/// This hint requires direct children to be restyled.
|
||||
Children,
|
||||
/// This hint requires all descendants to be restyled.
|
||||
Descendants,
|
||||
}
|
||||
|
||||
impl DescendantRestyleHint {
|
||||
/// Propagates this descendant behavior to a child element.
|
||||
fn propagate(self) -> Self {
|
||||
use self::DescendantRestyleHint::*;
|
||||
if self == Descendants {
|
||||
Descendants
|
||||
} else {
|
||||
Empty
|
||||
}
|
||||
}
|
||||
|
||||
fn union(self, other: Self) -> Self {
|
||||
use self::DescendantRestyleHint::*;
|
||||
if self == Descendants || other == Descendants {
|
||||
Descendants
|
||||
} else if self == Children || other == Children {
|
||||
Children
|
||||
} else {
|
||||
Empty
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Restyle hint for storing on ElementData. We use a separate representation
|
||||
/// to provide more type safety while propagating restyle hints down the tree.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StoredRestyleHint {
|
||||
pub restyle_self: bool,
|
||||
pub descendants: DescendantRestyleHint,
|
||||
}
|
||||
|
||||
impl StoredRestyleHint {
|
||||
/// Propagates this restyle hint to a child element.
|
||||
pub fn propagate(&self) -> Self {
|
||||
StoredRestyleHint {
|
||||
restyle_self: self.descendants == DescendantRestyleHint::Empty,
|
||||
descendants: self.descendants.propagate(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn empty() -> Self {
|
||||
StoredRestyleHint {
|
||||
restyle_self: false,
|
||||
descendants: DescendantRestyleHint::Empty,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn subtree() -> Self {
|
||||
StoredRestyleHint {
|
||||
restyle_self: true,
|
||||
descendants: DescendantRestyleHint::Descendants,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
!self.restyle_self && self.descendants == DescendantRestyleHint::Empty
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, other: &Self) {
|
||||
self.restyle_self = self.restyle_self || other.restyle_self;
|
||||
self.descendants = self.descendants.union(other.descendants);
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for StoredRestyleHint {
|
||||
fn default() -> Self {
|
||||
StoredRestyleHint {
|
||||
restyle_self: false,
|
||||
descendants: DescendantRestyleHint::Empty,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RestyleHint> for StoredRestyleHint {
|
||||
fn from(hint: RestyleHint) -> Self {
|
||||
use restyle_hints::*;
|
||||
use self::DescendantRestyleHint::*;
|
||||
debug_assert!(!hint.contains(RESTYLE_LATER_SIBLINGS), "Caller should apply sibling hints");
|
||||
StoredRestyleHint {
|
||||
restyle_self: hint.contains(RESTYLE_SELF),
|
||||
descendants: if hint.contains(RESTYLE_DESCENDANTS) { Descendants } else { Empty },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum ElementDataStyles {
|
||||
/// The field has not been initialized.
|
||||
Uninitialized,
|
||||
|
||||
/// The field holds the previous style of the node. If this is None, the
|
||||
/// node has not been previously styled.
|
||||
///
|
||||
/// This is the input to the styling algorithm. It would ideally be
|
||||
/// immutable, but for now we need to mutate it a bit before styling to
|
||||
/// handle animations.
|
||||
///
|
||||
/// Note that since ElementStyles contains an Arc, the null pointer
|
||||
/// optimization prevents the Option<> here from consuming an extra word.
|
||||
Previous(Option<ElementStyles>),
|
||||
|
||||
/// The field holds the current, up-to-date style.
|
||||
///
|
||||
/// This is the output of the styling algorithm.
|
||||
Current(ElementStyles),
|
||||
}
|
||||
|
||||
impl ElementDataStyles {
|
||||
fn is_previous(&self) -> bool {
|
||||
use self::ElementDataStyles::*;
|
||||
match *self {
|
||||
Previous(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
pub enum RestyleDataStyles {
|
||||
Previous(ElementStyles),
|
||||
New(ElementStyles),
|
||||
}
|
||||
|
||||
/// Transient data used by the restyle algorithm. This structure is instantiated
|
||||
|
@ -94,16 +194,55 @@ impl ElementDataStyles {
|
|||
/// processing.
|
||||
#[derive(Debug)]
|
||||
pub struct RestyleData {
|
||||
// FIXME(bholley): Start adding the fields from the algorithm doc.
|
||||
pub _dummy: u64,
|
||||
pub styles: RestyleDataStyles,
|
||||
pub hint: StoredRestyleHint,
|
||||
pub damage: RestyleDamage,
|
||||
pub snapshot: Option<Snapshot>,
|
||||
}
|
||||
|
||||
impl RestyleData {
|
||||
fn new() -> Self {
|
||||
fn new(previous: ElementStyles) -> Self {
|
||||
RestyleData {
|
||||
_dummy: 42,
|
||||
styles: RestyleDataStyles::Previous(previous),
|
||||
hint: StoredRestyleHint::default(),
|
||||
damage: RestyleDamage::empty(),
|
||||
snapshot: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_current_styles(&self) -> Option<&ElementStyles> {
|
||||
use self::RestyleDataStyles::*;
|
||||
match self.styles {
|
||||
Previous(_) => None,
|
||||
New(ref x) => Some(x),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_styles(&self) -> &ElementStyles {
|
||||
self.get_current_styles().unwrap()
|
||||
}
|
||||
|
||||
pub fn current_styles_mut(&mut self) -> &mut ElementStyles {
|
||||
use self::RestyleDataStyles::*;
|
||||
match self.styles {
|
||||
New(ref mut x) => x,
|
||||
Previous(_) => panic!("Calling current_styles_mut before styling"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_or_previous_styles(&self) -> &ElementStyles {
|
||||
use self::RestyleDataStyles::*;
|
||||
match self.styles {
|
||||
Previous(ref x) => x,
|
||||
New(ref x) => x,
|
||||
}
|
||||
}
|
||||
|
||||
fn finish_styling(&mut self, styles: ElementStyles, damage: RestyleDamage) {
|
||||
debug_assert!(self.get_current_styles().is_none());
|
||||
self.styles = RestyleDataStyles::New(styles);
|
||||
self.damage |= damage;
|
||||
}
|
||||
}
|
||||
|
||||
/// Style system data associated with a node.
|
||||
|
@ -118,81 +257,225 @@ impl RestyleData {
|
|||
/// In both cases, it is wrapped inside an AtomicRefCell to ensure thread
|
||||
/// safety.
|
||||
#[derive(Debug)]
|
||||
pub struct ElementData {
|
||||
styles: ElementDataStyles,
|
||||
pub restyle_data: Option<RestyleData>,
|
||||
pub enum ElementData {
|
||||
Initial(Option<ElementStyles>),
|
||||
Restyle(RestyleData),
|
||||
Persistent(ElementStyles),
|
||||
}
|
||||
|
||||
impl ElementData {
|
||||
pub fn new() -> Self {
|
||||
ElementData {
|
||||
styles: ElementDataStyles::Uninitialized,
|
||||
restyle_data: None,
|
||||
pub fn new(existing: Option<ElementStyles>) -> Self {
|
||||
if let Some(s) = existing {
|
||||
ElementData::Persistent(s)
|
||||
} else {
|
||||
ElementData::Initial(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_current_styles(&self) -> bool {
|
||||
match self.styles {
|
||||
ElementDataStyles::Current(_) => true,
|
||||
pub fn is_initial(&self) -> bool {
|
||||
match *self {
|
||||
ElementData::Initial(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_current_styles(&self) -> Option<&ElementStyles> {
|
||||
match self.styles {
|
||||
ElementDataStyles::Current(ref s) => Some(s),
|
||||
pub fn is_unstyled_initial(&self) -> bool {
|
||||
match *self {
|
||||
ElementData::Initial(None) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_styled_initial(&self) -> bool {
|
||||
match *self {
|
||||
ElementData::Initial(Some(_)) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_restyle(&self) -> bool {
|
||||
match *self {
|
||||
ElementData::Restyle(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_restyle(&self) -> Option<&RestyleData> {
|
||||
match *self {
|
||||
ElementData::Restyle(ref x) => Some(x),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_styles(&self) -> &ElementStyles {
|
||||
self.get_current_styles().expect("Calling current_styles before or during styling")
|
||||
pub fn as_restyle_mut(&mut self) -> Option<&mut RestyleData> {
|
||||
match *self {
|
||||
ElementData::Restyle(ref mut x) => Some(x),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
// Servo does lazy pseudo computation in layout and needs mutable access
|
||||
// to the current styles
|
||||
#[cfg(not(feature = "gecko"))]
|
||||
pub fn current_pseudos_mut(&mut self) -> &mut PseudoStyles {
|
||||
match self.styles {
|
||||
ElementDataStyles::Current(ref mut s) => &mut s.pseudos,
|
||||
_ => panic!("Calling current_pseudos_mut before or during styling"),
|
||||
pub fn is_persistent(&self) -> bool {
|
||||
match *self {
|
||||
ElementData::Persistent(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets an element up for restyle, returning None for an unstyled element.
|
||||
pub fn restyle(&mut self) -> Option<&mut RestyleData> {
|
||||
if self.is_unstyled_initial() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// If the caller never consumed the initial style, make sure that the
|
||||
// change hint represents the delta from zero, rather than a delta from
|
||||
// a previous style that was never observed. Ideally this shouldn't
|
||||
// happen, but we handle it for robustness' sake.
|
||||
let damage_override = if self.is_styled_initial() {
|
||||
RestyleDamage::rebuild_and_reflow()
|
||||
} else {
|
||||
RestyleDamage::empty()
|
||||
};
|
||||
|
||||
if !self.is_restyle() {
|
||||
// Play some tricks to reshape the enum without cloning ElementStyles.
|
||||
let old = mem::replace(self, ElementData::new(None));
|
||||
let styles = match old {
|
||||
ElementData::Initial(Some(s)) => s,
|
||||
ElementData::Persistent(s) => s,
|
||||
_ => unreachable!()
|
||||
};
|
||||
*self = ElementData::Restyle(RestyleData::new(styles));
|
||||
}
|
||||
|
||||
let restyle = self.as_restyle_mut().unwrap();
|
||||
restyle.damage |= damage_override;
|
||||
Some(restyle)
|
||||
}
|
||||
|
||||
/// Converts Initial and Restyle to Persistent. No-op for Persistent.
|
||||
pub fn persist(&mut self) {
|
||||
if self.is_persistent() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Play some tricks to reshape the enum without cloning ElementStyles.
|
||||
let old = mem::replace(self, ElementData::new(None));
|
||||
let styles = match old {
|
||||
ElementData::Initial(i) => i.unwrap(),
|
||||
ElementData::Restyle(r) => match r.styles {
|
||||
RestyleDataStyles::New(n) => n,
|
||||
RestyleDataStyles::Previous(_) => panic!("Never restyled element"),
|
||||
},
|
||||
ElementData::Persistent(_) => unreachable!(),
|
||||
};
|
||||
*self = ElementData::Persistent(styles);
|
||||
}
|
||||
|
||||
pub fn damage(&self) -> RestyleDamage {
|
||||
use self::ElementData::*;
|
||||
match *self {
|
||||
Initial(ref s) => {
|
||||
debug_assert!(s.is_some());
|
||||
RestyleDamage::rebuild_and_reflow()
|
||||
},
|
||||
Restyle(ref r) => {
|
||||
debug_assert!(r.get_current_styles().is_some());
|
||||
r.damage
|
||||
},
|
||||
Persistent(_) => RestyleDamage::empty(),
|
||||
}
|
||||
}
|
||||
|
||||
// A version of the above, with the assertions replaced with warnings to
|
||||
// be more robust in corner-cases. This will go away soon.
|
||||
#[cfg(feature = "gecko")]
|
||||
pub fn damage_sloppy(&self) -> RestyleDamage {
|
||||
use self::ElementData::*;
|
||||
match *self {
|
||||
Initial(ref s) => {
|
||||
if s.is_none() {
|
||||
error!("Accessing damage on unstyled element");
|
||||
}
|
||||
RestyleDamage::rebuild_and_reflow()
|
||||
},
|
||||
Restyle(ref r) => {
|
||||
if r.get_current_styles().is_none() {
|
||||
error!("Accessing damage on dirty element");
|
||||
}
|
||||
r.damage
|
||||
},
|
||||
Persistent(_) => RestyleDamage::empty(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_styles(&self) -> &ElementStyles {
|
||||
self.get_current_styles().unwrap()
|
||||
}
|
||||
|
||||
pub fn get_current_styles(&self) -> Option<&ElementStyles> {
|
||||
use self::ElementData::*;
|
||||
match *self {
|
||||
Initial(ref x) => x.as_ref(),
|
||||
Restyle(ref x) => x.get_current_styles(),
|
||||
Persistent(ref x) => Some(x),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_styles_mut(&mut self) -> &mut ElementStyles {
|
||||
use self::ElementData::*;
|
||||
match *self {
|
||||
Initial(ref mut x) => x.as_mut().unwrap(),
|
||||
Restyle(ref mut x) => x.current_styles_mut(),
|
||||
Persistent(ref mut x) => x,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn previous_styles(&self) -> Option<&ElementStyles> {
|
||||
match self.styles {
|
||||
ElementDataStyles::Previous(ref s) => s.as_ref(),
|
||||
_ => panic!("Calling previous_styles without having gathered it"),
|
||||
use self::ElementData::*;
|
||||
use self::RestyleDataStyles::*;
|
||||
match *self {
|
||||
Initial(_) => None,
|
||||
Restyle(ref x) => match x.styles {
|
||||
Previous(ref styles) => Some(styles),
|
||||
New(_) => panic!("Calling previous_styles after finish_styling"),
|
||||
},
|
||||
Persistent(_) => panic!("Calling previous_styles on Persistent ElementData"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn previous_styles_mut(&mut self) -> Option<&mut ElementStyles> {
|
||||
match self.styles {
|
||||
ElementDataStyles::Previous(ref mut s) => s.as_mut(),
|
||||
_ => panic!("Calling previous_styles without having gathered it"),
|
||||
use self::ElementData::*;
|
||||
use self::RestyleDataStyles::*;
|
||||
match *self {
|
||||
Initial(_) => None,
|
||||
Restyle(ref mut x) => match x.styles {
|
||||
Previous(ref mut styles) => Some(styles),
|
||||
New(_) => panic!("Calling previous_styles after finish_styling"),
|
||||
},
|
||||
Persistent(_) => panic!("Calling previous_styles on Persistent ElementData"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gather_previous_styles<F>(&mut self, f: F)
|
||||
where F: FnOnce() -> Option<ElementStyles>
|
||||
{
|
||||
use self::ElementDataStyles::*;
|
||||
self.styles = match mem::replace(&mut self.styles, Uninitialized) {
|
||||
Uninitialized => Previous(f()),
|
||||
Current(x) => Previous(Some(x)),
|
||||
Previous(x) => Previous(x),
|
||||
pub fn current_or_previous_styles(&self) -> &ElementStyles {
|
||||
use self::ElementData::*;
|
||||
match *self {
|
||||
Initial(ref x) => x.as_ref().unwrap(),
|
||||
Restyle(ref x) => x.current_or_previous_styles(),
|
||||
Persistent(ref x) => x,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finish_styling(&mut self, styles: ElementStyles, damage: RestyleDamage) {
|
||||
use self::ElementData::*;
|
||||
match *self {
|
||||
Initial(ref mut x) => {
|
||||
debug_assert!(x.is_none());
|
||||
debug_assert!(damage == RestyleDamage::rebuild_and_reflow());
|
||||
*x = Some(styles);
|
||||
},
|
||||
Restyle(ref mut x) => x.finish_styling(styles, damage),
|
||||
Persistent(_) => panic!("Calling finish_styling on Persistent ElementData"),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn ensure_restyle_data(&mut self) {
|
||||
if self.restyle_data.is_none() {
|
||||
self.restyle_data = Some(RestyleData::new());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finish_styling(&mut self, styles: ElementStyles) {
|
||||
debug_assert!(self.styles.is_previous());
|
||||
self.styles = ElementDataStyles::Current(styles);
|
||||
self.restyle_data = None;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue