style: Parameterize the update and rebuild methods to take an iterator.

In preparation to avoid cloning the stylesheets while rebuilding in Gecko.
This commit is contained in:
Emilio Cobos Álvarez 2017-05-11 01:45:00 +02:00
parent 50e0c67e2c
commit 677daaabc5
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
5 changed files with 39 additions and 30 deletions

View file

@ -1091,7 +1091,7 @@ impl LayoutThread {
marker: PhantomData,
};
let needs_dirtying = self.stylist.update(
&data.document_stylesheets,
data.document_stylesheets.iter(),
&guards,
Some(ua_stylesheets),
data.stylesheets_changed,

View file

@ -105,7 +105,7 @@ impl PerDocumentStyleDataImpl {
let mut stylesheets = Vec::<Arc<Stylesheet>>::new();
self.stylesheets.flush(&mut stylesheets);
self.stylist.clear();
self.stylist.rebuild(stylesheets.as_slice(),
self.stylist.rebuild(stylesheets.iter(),
&StylesheetGuards::same(guard),
/* ua_sheets = */ None,
/* stylesheets_changed = */ true,

View file

@ -268,13 +268,15 @@ impl Stylist {
/// This method resets all the style data each time the stylesheets change
/// (which is indicated by the `stylesheets_changed` parameter), or the
/// device is dirty, which means we need to re-evaluate media queries.
pub fn rebuild<'a>(&mut self,
doc_stylesheets: &[Arc<Stylesheet>],
pub fn rebuild<'a, 'b, I>(&mut self,
doc_stylesheets: I,
guards: &StylesheetGuards,
ua_stylesheets: Option<&UserAgentStylesheets>,
stylesheets_changed: bool,
author_style_disabled: bool,
extra_data: &mut ExtraStyleData<'a>) -> bool {
extra_data: &mut ExtraStyleData<'a>) -> bool
where I: Iterator<Item = &'b Arc<Stylesheet>> + Clone,
{
debug_assert!(!self.is_cleared || self.is_device_dirty);
self.is_cleared = false;
@ -287,7 +289,7 @@ impl Stylist {
let cascaded_rule = ViewportRule {
declarations: viewport::Cascade::from_stylesheets(
doc_stylesheets, guards.author, &self.device
doc_stylesheets.clone(), guards.author, &self.device
).finish(),
};
@ -317,7 +319,7 @@ impl Stylist {
}
// Only use author stylesheets if author styles are enabled.
let sheets_to_add = doc_stylesheets.iter().filter(|s| {
let sheets_to_add = doc_stylesheets.filter(|s| {
!author_style_disabled || s.origin != Origin::Author
});
@ -338,13 +340,15 @@ impl Stylist {
/// clear the stylist and then rebuild it. Chances are, you want to use
/// either clear() or rebuild(), with the latter done lazily, instead.
pub fn update<'a>(&mut self,
doc_stylesheets: &[Arc<Stylesheet>],
pub fn update<'a, 'b, I>(&mut self,
doc_stylesheets: I,
guards: &StylesheetGuards,
ua_stylesheets: Option<&UserAgentStylesheets>,
stylesheets_changed: bool,
author_style_disabled: bool,
extra_data: &mut ExtraStyleData<'a>) -> bool {
extra_data: &mut ExtraStyleData<'a>) -> bool
where I: Iterator<Item = &'b Arc<Stylesheet>> + Clone,
{
debug_assert!(!self.is_cleared || self.is_device_dirty);
// We have to do a dirtiness check before clearing, because if
@ -357,7 +361,9 @@ impl Stylist {
author_style_disabled, extra_data)
}
fn add_stylesheet<'a>(&mut self, stylesheet: &Stylesheet, guard: &SharedRwLockReadGuard,
fn add_stylesheet<'a>(&mut self,
stylesheet: &Stylesheet,
guard: &SharedRwLockReadGuard,
extra_data: &mut ExtraStyleData<'a>) {
if stylesheet.disabled() || !stylesheet.is_effective_for_device(&self.device, guard) {
return;
@ -656,10 +662,12 @@ impl Stylist {
/// FIXME(emilio): The semantics of the device for Servo and Gecko are
/// different enough we may want to unify them.
#[cfg(feature = "servo")]
pub fn set_device(&mut self, mut device: Device, guard: &SharedRwLockReadGuard,
pub fn set_device(&mut self,
mut device: Device,
guard: &SharedRwLockReadGuard,
stylesheets: &[Arc<Stylesheet>]) {
let cascaded_rule = ViewportRule {
declarations: viewport::Cascade::from_stylesheets(stylesheets, guard, &device).finish(),
declarations: viewport::Cascade::from_stylesheets(stylesheets.iter(), guard, &device).finish(),
};
self.viewport_constraints =

View file

@ -26,6 +26,7 @@ use std::iter::Enumerate;
use std::str::Chars;
use style_traits::{PinchZoomFactor, ToCss};
use style_traits::viewport::{Orientation, UserZoom, ViewportConstraints, Zoom};
use stylearc::Arc;
use stylesheets::{Stylesheet, Origin};
use values::computed::{Context, ToComputedValue};
use values::specified::{NoCalcLength, LengthOrPercentageOrAuto, ViewportPercentageLength};
@ -329,7 +330,6 @@ fn is_whitespace_separator_or_equals(c: &char) -> bool {
}
impl Parse for ViewportRule {
#[allow(missing_docs)]
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
let parser = ViewportRuleParser { context: context };
@ -545,14 +545,15 @@ impl Cascade {
}
}
pub fn from_stylesheets<'a, I>(stylesheets: I, guard: &SharedRwLockReadGuard,
device: &Device) -> Self
where I: IntoIterator,
I::Item: AsRef<Stylesheet>,
pub fn from_stylesheets<'a, I>(stylesheets: I,
guard: &SharedRwLockReadGuard,
device: &Device)
-> Self
where I: Iterator<Item = &'a Arc<Stylesheet>>,
{
let mut cascade = Self::new();
for stylesheet in stylesheets {
stylesheet.as_ref().effective_viewport_rules(device, guard, |rule| {
stylesheet.effective_viewport_rules(device, guard, |rule| {
for declaration in &rule.declarations {
cascade.add(Cow::Borrowed(declaration))
}

View file

@ -25,7 +25,7 @@ macro_rules! stylesheet {
stylesheet!($css, $origin, $error_reporter, SharedRwLock::new())
};
($css:expr, $origin:ident, $error_reporter:expr, $shared_lock:expr) => {
Box::new(Stylesheet::from_str(
Arc::new(Stylesheet::from_str(
$css,
ServoUrl::parse("http://localhost").unwrap(),
Origin::$origin,
@ -269,7 +269,7 @@ fn multiple_stylesheets_cascading() {
Author, error_reporter, shared_lock.clone())
];
let declarations = Cascade::from_stylesheets(&stylesheets, &shared_lock.read(), &device).finish();
let declarations = Cascade::from_stylesheets(stylesheets.iter(), &shared_lock.read(), &device).finish();
assert_decl_len!(declarations == 3);
assert_decl_eq!(&declarations[0], UserAgent, Zoom: Zoom::Number(1.));
assert_decl_eq!(&declarations[1], User, MinHeight: viewport_length!(200., px));
@ -283,7 +283,7 @@ fn multiple_stylesheets_cascading() {
stylesheet!("@viewport { min-width: 300px !important; min-height: 300px !important; zoom: 3 !important; }",
Author, error_reporter, shared_lock.clone())
];
let declarations = Cascade::from_stylesheets(&stylesheets, &shared_lock.read(), &device).finish();
let declarations = Cascade::from_stylesheets(stylesheets.iter(), &shared_lock.read(), &device).finish();
assert_decl_len!(declarations == 3);
assert_decl_eq!(&declarations[0], UserAgent, MinWidth: viewport_length!(100., px), !important);
assert_decl_eq!(&declarations[1], User, MinHeight: viewport_length!(200., px), !important);