From 04b0ae64f2fe606744142e353388749fe283a8ad Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 17 Jul 2017 11:41:44 -0700 Subject: [PATCH] stylo: Introduce ComputedValuesInner --- .../style/gecko/generated/structs_debug.rs | 2 +- .../style/gecko/generated/structs_release.rs | 2 +- components/style/properties/gecko.mako.rs | 69 +++++++---- .../style/properties/properties.mako.rs | 113 +++++++++++------- components/style/selector_parser.rs | 2 +- components/style/servo/restyle_damage.rs | 10 +- 6 files changed, 125 insertions(+), 73 deletions(-) diff --git a/components/style/gecko/generated/structs_debug.rs b/components/style/gecko/generated/structs_debug.rs index 9c3c06c4fa9..3bebc33e413 100644 --- a/components/style/gecko/generated/structs_debug.rs +++ b/components/style/gecko/generated/structs_debug.rs @@ -13,7 +13,7 @@ pub type ServoWritingMode = ::logical_geometry::WritingMode; pub type ServoFontComputationData = ::properties::FontComputationData; pub type ServoCustomPropertiesMap = Option<::stylearc::Arc<::custom_properties::CustomPropertiesMap>>; pub type ServoRuleNode = Option<::rule_tree::StrongRuleNode>; -pub type ServoVisitedStyle = Option<::stylearc::Arc>; +pub type ServoVisitedStyle = Option<::stylearc::Arc<::properties::ComputedValues>>; pub type ServoComputedValueFlags = ::properties::computed_value_flags::ComputedValueFlags; pub type ServoRawOffsetArc = ::stylearc::RawOffsetArc; diff --git a/components/style/gecko/generated/structs_release.rs b/components/style/gecko/generated/structs_release.rs index b85f5b652cf..bc2e1a41e71 100644 --- a/components/style/gecko/generated/structs_release.rs +++ b/components/style/gecko/generated/structs_release.rs @@ -13,7 +13,7 @@ pub type ServoWritingMode = ::logical_geometry::WritingMode; pub type ServoFontComputationData = ::properties::FontComputationData; pub type ServoCustomPropertiesMap = Option<::stylearc::Arc<::custom_properties::CustomPropertiesMap>>; pub type ServoRuleNode = Option<::rule_tree::StrongRuleNode>; -pub type ServoVisitedStyle = Option<::stylearc::Arc>; +pub type ServoVisitedStyle = Option<::stylearc::Arc<::properties::ComputedValues>>; pub type ServoComputedValueFlags = ::properties::computed_value_flags::ComputedValueFlags; pub type ServoRawOffsetArc = ::stylearc::RawOffsetArc; diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index 23717b3daa7..a432e8d2369 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -59,9 +59,8 @@ use properties::{longhands, FontComputationData, Importance, LonghandId}; use properties::{PropertyDeclaration, PropertyDeclarationBlock, PropertyDeclarationId}; use rule_tree::StrongRuleNode; use std::mem::{forget, transmute, zeroed}; -use std::ptr; +use std::{cmp, ops, ptr}; use stylearc::{Arc, RawOffsetArc}; -use std::cmp; use values::{Auto, CustomIdent, Either, KeyframesName}; use values::computed::ToComputedValue; use values::computed::effects::{BoxShadow, Filter, SimpleShadow}; @@ -75,11 +74,16 @@ pub mod style_structs { } -pub use ::gecko_bindings::structs::mozilla::ServoComputedValues2 as ComputedValues; +pub use ::gecko_bindings::structs::mozilla::ServoComputedValues2 as ComputedValuesInner; -impl Clone for ComputedValues { +#[derive(Clone, Debug)] +pub struct ComputedValues { + pub inner: ComputedValuesInner +} + +impl Clone for ComputedValuesInner { fn clone(&self) -> Self { - ComputedValues { + ComputedValuesInner { % for style_struct in data.style_structs: ${style_struct.gecko_name}: self.${style_struct.gecko_name}.clone(), % endfor @@ -105,32 +109,51 @@ impl ComputedValues { % endfor ) -> Self { ComputedValues { - custom_properties, - writing_mode, - font_computation_data: FontComputationData::new(font_size_keyword), - flags, - rules, - visited_style: visited_style, - % for style_struct in data.style_structs: - ${style_struct.gecko_name}: Arc::into_raw_offset(${style_struct.ident}), - % endfor + inner: ComputedValuesInner { + custom_properties: custom_properties, + writing_mode: writing_mode, + font_computation_data: FontComputationData::new(font_size_keyword), + rules: rules, + visited_style: visited_style, + flags: flags, + % for style_struct in data.style_structs: + ${style_struct.gecko_name}: Arc::into_raw_offset(${style_struct.ident}), + % endfor + } } } pub fn default_values(pres_context: RawGeckoPresContextBorrowed) -> Arc { Arc::new(ComputedValues { - custom_properties: None, - writing_mode: WritingMode::empty(), // FIXME(bz): This seems dubious - font_computation_data: FontComputationData::default_values(), - flags: ComputedValueFlags::empty(), - rules: None, - visited_style: None, - % for style_struct in data.style_structs: - ${style_struct.gecko_name}: Arc::into_raw_offset(style_structs::${style_struct.name}::default(pres_context)), - % endfor + inner: ComputedValuesInner { + custom_properties: None, + writing_mode: WritingMode::empty(), // FIXME(bz): This seems dubious + font_computation_data: FontComputationData::default_values(), + rules: None, + visited_style: None, + flags: ComputedValueFlags::empty(), + % for style_struct in data.style_structs: + ${style_struct.gecko_name}: Arc::into_raw_offset(style_structs::${style_struct.name}::default(pres_context)), + % endfor + } }) } +} +impl ops::Deref for ComputedValues { + type Target = ComputedValuesInner; + fn deref(&self) -> &ComputedValuesInner { + &self.inner + } +} + +impl ops::DerefMut for ComputedValues { + fn deref_mut(&mut self) -> &mut ComputedValuesInner { + &mut self.inner + } +} + +impl ComputedValuesInner { #[inline] pub fn is_display_contents(&self) -> bool { self.get_box().clone_display() == longhands::display::computed_value::T::contents diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index c2d0289b606..cc3a38731a0 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -12,9 +12,7 @@ use std::borrow::Cow; use std::collections::HashSet; -use std::fmt; -use std::mem; -use std::ops::Deref; +use std::{fmt, mem, ops}; use stylearc::{Arc, UniqueArc}; use app_units::Au; @@ -113,7 +111,7 @@ pub struct FontComputationData { } -impl FontComputationData{ +impl FontComputationData { /// Assigns values for variables in struct FontComputationData pub fn new(font_size_keyword: Option<(longhands::font_size::KeywordSize, f32)>) -> Self { FontComputationData { @@ -1875,19 +1873,11 @@ pub mod style_structs { #[cfg(feature = "gecko")] pub use gecko_properties::ComputedValues; -/// A legacy alias for a servo-version of ComputedValues. Should go away soon. -#[cfg(feature = "servo")] -pub type ServoComputedValues = ComputedValues; -/// The struct that Servo uses to represent computed values. -/// -/// This struct contains an immutable atomically-reference-counted pointer to -/// every kind of style struct. -/// -/// When needed, the structs may be copied in order to get mutated. #[cfg(feature = "servo")] #[cfg_attr(feature = "servo", derive(Clone, Debug))] -pub struct ComputedValues { +/// Actual data of ComputedValues, to match up with Gecko +pub struct ComputedValuesInner { % for style_struct in data.active_style_structs(): ${style_struct.ident}: Arc, % endfor @@ -1911,6 +1901,24 @@ pub struct ComputedValues { visited_style: Option>, } +/// The struct that Servo uses to represent computed values. +/// +/// This struct contains an immutable atomically-reference-counted pointer to +/// every kind of style struct. +/// +/// When needed, the structs may be copied in order to get mutated. +#[cfg(feature = "servo")] +#[cfg_attr(feature = "servo", derive(Clone, Debug))] +pub struct ComputedValues { + /// The actual computed values + /// + /// In Gecko the outer ComputedValues is actually a style context, + /// whereas ComputedValuesInner is the core set of computed values. + /// + /// We maintain this distinction in servo to reduce the amount of special casing. + pub inner: ComputedValuesInner, +} + #[cfg(feature = "servo")] impl ComputedValues { /// Construct a `ComputedValues` instance. @@ -1925,23 +1933,42 @@ impl ComputedValues { ${style_struct.ident}: Arc, % endfor ) -> Self { - let font_computation_data = FontComputationData::new(font_size_keyword); ComputedValues { - custom_properties, - writing_mode, - font_computation_data, - flags, - rules, - visited_style, - % for style_struct in data.active_style_structs(): - ${style_struct.ident}, - % endfor + inner: ComputedValuesInner { + custom_properties: custom_properties, + writing_mode: writing_mode, + font_computation_data: FontComputationData::new(font_size_keyword), + rules: rules, + visited_style: visited_style, + flags: flags, + % for style_struct in data.active_style_structs(): + ${style_struct.ident}: ${style_struct.ident}, + % endfor + } } } /// Get the initial computed values. pub fn initial_values() -> &'static Self { &*INITIAL_SERVO_VALUES } +} +#[cfg(feature = "servo")] +impl ops::Deref for ComputedValues { + type Target = ComputedValuesInner; + fn deref(&self) -> &ComputedValuesInner { + &self.inner + } +} + +#[cfg(feature = "servo")] +impl ops::DerefMut for ComputedValues { + fn deref_mut(&mut self) -> &mut ComputedValuesInner { + &mut self.inner + } +} + +#[cfg(feature = "servo")] +impl ComputedValuesInner { % for style_struct in data.active_style_structs(): /// Clone the ${style_struct.name} struct. #[inline] @@ -2372,7 +2399,7 @@ impl<'a, T: 'a> StyleStructRef<'a, T> } } -impl<'a, T: 'a> Deref for StyleStructRef<'a, T> { +impl<'a, T: 'a> ops::Deref for StyleStructRef<'a, T> { type Target = T; fn deref(&self) -> &T { @@ -2559,28 +2586,30 @@ pub use self::lazy_static_module::INITIAL_SERVO_VALUES; mod lazy_static_module { use logical_geometry::WritingMode; use stylearc::Arc; - use super::{ComputedValues, longhands, style_structs, FontComputationData}; + use super::{ComputedValues, ComputedValuesInner, longhands, style_structs, FontComputationData}; use super::computed_value_flags::ComputedValueFlags; /// The initial values for all style structs as defined by the specification. lazy_static! { pub static ref INITIAL_SERVO_VALUES: ComputedValues = ComputedValues { - % for style_struct in data.active_style_structs(): - ${style_struct.ident}: Arc::new(style_structs::${style_struct.name} { - % for longhand in style_struct.longhands: - ${longhand.ident}: longhands::${longhand.ident}::get_initial_value(), - % endfor - % if style_struct.name == "Font": - hash: 0, - % endif - }), - % endfor - custom_properties: None, - writing_mode: WritingMode::empty(), - flags: ComputedValueFlags::empty(), - font_computation_data: FontComputationData::default_values(), - rules: None, - visited_style: None, + inner: ComputedValuesInner { + % for style_struct in data.active_style_structs(): + ${style_struct.ident}: Arc::new(style_structs::${style_struct.name} { + % for longhand in style_struct.longhands: + ${longhand.ident}: longhands::${longhand.ident}::get_initial_value(), + % endfor + % if style_struct.name == "Font": + hash: 0, + % endif + }), + % endfor + custom_properties: None, + writing_mode: WritingMode::empty(), + font_computation_data: FontComputationData::default_values(), + rules: None, + visited_style: None, + flags: ComputedValueFlags::empty(), + } }; } } diff --git a/components/style/selector_parser.rs b/components/style/selector_parser.rs index 1dbc96d3db2..3f5a43365cf 100644 --- a/components/style/selector_parser.rs +++ b/components/style/selector_parser.rs @@ -38,7 +38,7 @@ pub use gecko::restyle_damage::GeckoRestyleDamage as RestyleDamage; /// A type that represents the previous computed values needed for restyle /// damage calculation. #[cfg(feature = "servo")] -pub type PreExistingComputedValues = ::properties::ServoComputedValues; +pub type PreExistingComputedValues = ::properties::ComputedValues; /// A type that represents the previous computed values needed for restyle /// damage calculation. diff --git a/components/style/servo/restyle_damage.rs b/components/style/servo/restyle_damage.rs index 1e9cae4ea08..f99e6120bb9 100644 --- a/components/style/servo/restyle_damage.rs +++ b/components/style/servo/restyle_damage.rs @@ -10,7 +10,7 @@ use computed_values::display; use heapsize::HeapSizeOf; use matching::{StyleChange, StyleDifference}; -use properties::ServoComputedValues; +use properties::ComputedValues; use std::fmt; bitflags! { @@ -60,8 +60,8 @@ impl HeapSizeOf for ServoRestyleDamage { impl ServoRestyleDamage { /// Compute the `StyleDifference` (including the appropriate restyle damage) /// for a given style change between `old` and `new`. - pub fn compute_style_difference(old: &ServoComputedValues, - new: &ServoComputedValues) + pub fn compute_style_difference(old: &ComputedValues, + new: &ComputedValues) -> StyleDifference { let damage = compute_damage(old, new); let change = if damage.is_empty() { StyleChange::Unchanged } else { StyleChange::Changed }; @@ -182,11 +182,11 @@ macro_rules! add_if_not_equal( }) ); -fn compute_damage(old: &ServoComputedValues, new: &ServoComputedValues) -> ServoRestyleDamage { +fn compute_damage(old: &ComputedValues, new: &ComputedValues) -> ServoRestyleDamage { let mut damage = ServoRestyleDamage::empty(); // This should check every CSS property, as enumerated in the fields of - // http://doc.servo.org/style/properties/struct.ServoComputedValues.html + // http://doc.servo.org/style/properties/struct.ComputedValues.html // FIXME: Test somehow that every property is included.