Auto merge of #14848 - bzbarsky:initial-styles, r=bholley

Stop using global initial styles for stylo; the initial styles need to be per-document

<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix https://bugzilla.mozilla.org/show_bug.cgi?id=1298588

<!-- Either: -->
- [ ] There are tests for these changes OR
- [X] These changes do not require tests on the servo side because behavior is unchanged.  Gecko-side tests probably exist.

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/14848)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-01-04 21:01:38 -08:00 committed by GitHub
commit 143dfc879e
20 changed files with 287 additions and 152 deletions

View file

@ -15,7 +15,7 @@ use custom_properties::ComputedValuesMap;
use gecko_bindings::bindings;
% for style_struct in data.style_structs:
use gecko_bindings::structs::${style_struct.gecko_ffi_name};
use gecko_bindings::bindings::Gecko_Construct_${style_struct.gecko_ffi_name};
use gecko_bindings::bindings::Gecko_Construct_Default_${style_struct.gecko_ffi_name};
use gecko_bindings::bindings::Gecko_CopyConstruct_${style_struct.gecko_ffi_name};
use gecko_bindings::bindings::Gecko_Destroy_${style_struct.gecko_ffi_name};
% endfor
@ -40,6 +40,7 @@ use gecko_bindings::bindings::Gecko_SetMozBinding;
use gecko_bindings::bindings::Gecko_SetNullImageValue;
use gecko_bindings::bindings::ServoComputedValuesBorrowedOrNull;
use gecko_bindings::bindings::{Gecko_ResetFilters, Gecko_CopyFiltersFrom};
use gecko_bindings::bindings::RawGeckoPresContextBorrowed;
use gecko_bindings::structs;
use gecko_bindings::structs::nsStyleVariables;
use gecko_bindings::sugar::ns_style_coord::{CoordDataValue, CoordData, CoordDataMut};
@ -53,7 +54,6 @@ use properties::longhands;
use std::fmt::{self, Debug};
use std::mem::{transmute, zeroed};
use std::ptr;
use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
use std::sync::Arc;
use std::cmp;
@ -76,7 +76,7 @@ pub struct ComputedValues {
}
impl ComputedValues {
pub fn inherit_from(parent: &Arc<Self>) -> Arc<Self> {
pub fn inherit_from(parent: &Arc<Self>, default: &Arc<Self>) -> Arc<Self> {
Arc::new(ComputedValues {
custom_properties: parent.custom_properties.clone(),
shareable: parent.shareable,
@ -86,7 +86,7 @@ impl ComputedValues {
% if style_struct.inherited:
${style_struct.ident}: parent.${style_struct.ident}.clone(),
% else:
${style_struct.ident}: Self::initial_values().${style_struct.ident}.clone(),
${style_struct.ident}: default.${style_struct.ident}.clone(),
% endif
% endfor
})
@ -111,37 +111,16 @@ impl ComputedValues {
}
}
pub fn style_for_child_text_node(parent: &Arc<Self>) -> Arc<Self> {
// Gecko expects text nodes to be styled as if they were elements that
// matched no rules (that is, inherited style structs are inherited and
// non-inherited style structs are set to their initial values).
ComputedValues::inherit_from(parent)
}
pub fn initial_values() -> &'static Self {
unsafe {
debug_assert!(!raw_initial_values().is_null());
&*raw_initial_values()
}
}
pub unsafe fn initialize() {
debug_assert!(raw_initial_values().is_null());
set_raw_initial_values(Box::into_raw(Box::new(ComputedValues {
% for style_struct in data.style_structs:
${style_struct.ident}: style_structs::${style_struct.name}::initial(),
% endfor
pub fn default_values(pres_context: RawGeckoPresContextBorrowed) -> Arc<Self> {
Arc::new(ComputedValues {
custom_properties: None,
shareable: true,
writing_mode: WritingMode::empty(),
root_font_size: longhands::font_size::get_initial_value(),
})));
}
pub unsafe fn shutdown() {
debug_assert!(!raw_initial_values().is_null());
let _ = Box::from_raw(raw_initial_values());
set_raw_initial_values(ptr::null_mut());
writing_mode: WritingMode::empty(), // FIXME(bz): This seems dubious
root_font_size: longhands::font_size::get_initial_value(), // FIXME(bz): Also seems dubious?
% for style_struct in data.style_structs:
${style_struct.ident}: style_structs::${style_struct.name}::default(pres_context),
% endfor
})
}
% for style_struct in data.style_structs:
@ -415,10 +394,11 @@ def set_gecko_property(ffi_name, expr):
<%def name="impl_style_struct(style_struct)">
impl ${style_struct.gecko_struct_name} {
#[allow(dead_code, unused_variables)]
pub fn initial() -> Arc<Self> {
pub fn default(pres_context: RawGeckoPresContextBorrowed) -> Arc<Self> {
let mut result = Arc::new(${style_struct.gecko_struct_name} { gecko: unsafe { zeroed() } });
unsafe {
Gecko_Construct_${style_struct.gecko_ffi_name}(&mut Arc::get_mut(&mut result).unwrap().gecko);
Gecko_Construct_Default_${style_struct.gecko_ffi_name}(&mut Arc::get_mut(&mut result).unwrap().gecko,
pres_context);
}
result
}
@ -2646,13 +2626,3 @@ pub unsafe extern "C" fn Servo_GetStyleVariables(_cv: ServoComputedValuesBorrowe
&*EMPTY_VARIABLES_STRUCT
}
// To avoid UB, we store the initial values as a atomic. It would be nice to
// store them as AtomicPtr, but we can't have static AtomicPtr without const
// fns, which aren't in stable Rust.
static INITIAL_VALUES_STORAGE: AtomicUsize = ATOMIC_USIZE_INIT;
unsafe fn raw_initial_values() -> *mut ComputedValues {
INITIAL_VALUES_STORAGE.load(Ordering::Relaxed) as *mut ComputedValues
}
unsafe fn set_raw_initial_values(v: *mut ComputedValues) {
INITIAL_VALUES_STORAGE.store(v as usize, Ordering::Relaxed);
}

View file

@ -210,6 +210,7 @@
#[allow(unused_variables)]
pub fn cascade_property(declaration: &PropertyDeclaration,
inherited_style: &ComputedValues,
default_style: &Arc<ComputedValues>,
context: &mut computed::Context,
seen: &mut PropertyBitField,
cacheable: &mut bool,
@ -260,7 +261,7 @@
DeclaredValue::Initial => {
// We assume that it's faster to use copy_*_from rather than
// set_*(get_initial_value());
let initial_struct = ComputedValues::initial_values()
let initial_struct = default_style
.get_${data.current_style_struct.name_lower}();
context.mutate_style().mutate_${data.current_style_struct.name_lower}()
.copy_${property.ident}_from(initial_struct ${maybe_wm});

View file

@ -1658,6 +1658,7 @@ mod lazy_static_module {
pub type CascadePropertyFn =
extern "Rust" fn(declaration: &PropertyDeclaration,
inherited_style: &ComputedValues,
default_style: &Arc<ComputedValues>,
context: &mut computed::Context,
seen: &mut PropertyBitField,
cacheable: &mut bool,
@ -1704,13 +1705,14 @@ bitflags! {
pub fn cascade(viewport_size: Size2D<Au>,
rule_node: &StrongRuleNode,
parent_style: Option<<&ComputedValues>,
default_style: &Arc<ComputedValues>,
cascade_info: Option<<&mut CascadeInfo>,
error_reporter: StdBox<ParseErrorReporter + Send>,
flags: CascadeFlags)
-> ComputedValues {
let (is_root_element, inherited_style) = match parent_style {
Some(parent_style) => (false, parent_style),
None => (true, ComputedValues::initial_values()),
None => (true, &**default_style),
};
// Hold locks until after the apply_declarations() call returns.
// Use filter_map because the root node has no style source.
@ -1735,6 +1737,7 @@ pub fn cascade(viewport_size: Size2D<Au>,
is_root_element,
iter_declarations,
inherited_style,
default_style,
cascade_info,
error_reporter,
None,
@ -1747,6 +1750,7 @@ pub fn apply_declarations<'a, F, I>(viewport_size: Size2D<Au>,
is_root_element: bool,
iter_declarations: F,
inherited_style: &ComputedValues,
default_style: &Arc<ComputedValues>,
mut cascade_info: Option<<&mut CascadeInfo>,
mut error_reporter: StdBox<ParseErrorReporter + Send>,
font_metrics_provider: Option<<&FontMetricsProvider>,
@ -1773,8 +1777,6 @@ pub fn apply_declarations<'a, F, I>(viewport_size: Size2D<Au>,
::custom_properties::finish_cascade(
custom_properties, &inherited_custom_properties);
let initial_values = ComputedValues::initial_values();
let starting_style = if !flags.contains(INHERIT_ALL) {
ComputedValues::new(custom_properties,
flags.contains(SHAREABLE),
@ -1784,7 +1786,7 @@ pub fn apply_declarations<'a, F, I>(viewport_size: Size2D<Au>,
% if style_struct.inherited:
inherited_style.clone_${style_struct.name_lower}(),
% else:
initial_values.clone_${style_struct.name_lower}(),
default_style.clone_${style_struct.name_lower}(),
% endif
% endfor
)
@ -1864,6 +1866,7 @@ pub fn apply_declarations<'a, F, I>(viewport_size: Size2D<Au>,
let discriminant = longhand_id as usize;
(CASCADE_PROPERTY[discriminant])(declaration,
inherited_style,
default_style,
&mut context,
&mut seen,
&mut cacheable,