Attachment #8822400: part 13. Make sure Device has a ComputedValues for stylo for bug #1298588

View | Details | Raw Unified | Return to bug 1298588
Collapse All | Expand All

(-)a/servo/components/style/gecko/data.rs (-2 / +7 lines)
Line     Link Here 
 Lines 58-74   lazy_static! { Link Here 
58
        }
58
        }
59
    };
59
    };
60
}
60
}
61
61
62
impl PerDocumentStyleData {
62
impl PerDocumentStyleData {
63
    pub fn new(pres_context: RawGeckoPresContextBorrowed) -> Self {
63
    pub fn new(pres_context: RawGeckoPresContextBorrowed) -> Self {
64
        // FIXME(bholley): Real window size.
64
        // FIXME(bholley): Real window size.
65
        let window_size: TypedSize2D<f32, ViewportPx> = TypedSize2D::new(800.0, 600.0);
65
        let window_size: TypedSize2D<f32, ViewportPx> = TypedSize2D::new(800.0, 600.0);
66
        let device = Device::new(MediaType::Screen, window_size);
66
        let default_computed_values = ComputedValues::default_values(pres_context);
67
68
        // FIXME(bz): We're going to need to either update the computed values
69
        // in the Stylist's Device or give the Stylist a new Device when our
70
        // default_computed_values changes.
71
        let device = Device::new(MediaType::Screen, window_size, &default_computed_values);
67
72
68
        let (new_anims_sender, new_anims_receiver) = channel();
73
        let (new_anims_sender, new_anims_receiver) = channel();
69
74
70
        PerDocumentStyleData(AtomicRefCell::new(PerDocumentStyleDataImpl {
75
        PerDocumentStyleData(AtomicRefCell::new(PerDocumentStyleDataImpl {
71
            stylist: Arc::new(Stylist::new(device)),
76
            stylist: Arc::new(Stylist::new(device)),
72
            stylesheets: vec![],
77
            stylesheets: vec![],
73
            stylesheets_changed: true,
78
            stylesheets_changed: true,
74
            new_animations_sender: new_anims_sender,
79
            new_animations_sender: new_anims_sender,
 Lines 78-94   impl PerDocumentStyleData { Link Here 
78
            work_queue: if *NUM_THREADS <= 1 {
83
            work_queue: if *NUM_THREADS <= 1 {
79
                None
84
                None
80
            } else {
85
            } else {
81
                let configuration =
86
                let configuration =
82
                    rayon::Configuration::new().set_num_threads(*NUM_THREADS);
87
                    rayon::Configuration::new().set_num_threads(*NUM_THREADS);
83
                rayon::ThreadPool::new(configuration).ok()
88
                rayon::ThreadPool::new(configuration).ok()
84
            },
89
            },
85
            num_threads: *NUM_THREADS,
90
            num_threads: *NUM_THREADS,
86
            default_computed_values: ComputedValues::default_values(pres_context),
91
            default_computed_values: default_computed_values,
87
        }))
92
        }))
88
    }
93
    }
89
94
90
    pub fn borrow(&self) -> AtomicRef<PerDocumentStyleDataImpl> {
95
    pub fn borrow(&self) -> AtomicRef<PerDocumentStyleDataImpl> {
91
        self.0.borrow()
96
        self.0.borrow()
92
    }
97
    }
93
98
94
    pub fn borrow_mut(&self) -> AtomicRefMut<PerDocumentStyleDataImpl> {
99
    pub fn borrow_mut(&self) -> AtomicRefMut<PerDocumentStyleDataImpl> {
(-)a/servo/components/style/media_queries.rs (-4 / +37 lines)
Line     Link Here 
 Lines 10-26   use Atom; Link Here 
10
use app_units::Au;
10
use app_units::Au;
11
use cssparser::{Delimiter, Parser, Token};
11
use cssparser::{Delimiter, Parser, Token};
12
use euclid::size::{Size2D, TypedSize2D};
12
use euclid::size::{Size2D, TypedSize2D};
13
use serialize_comma_separated_list;
13
use serialize_comma_separated_list;
14
use std::fmt::{self, Write};
14
use std::fmt::{self, Write};
15
use style_traits::{ToCss, ViewportPx};
15
use style_traits::{ToCss, ViewportPx};
16
use values::computed::{self, ToComputedValue};
16
use values::computed::{self, ToComputedValue};
17
use values::specified;
17
use values::specified;
18
18
use properties::ComputedValues;
19
#[cfg(feature = "gecko")]
20
use std::sync::Arc;
19
21
20
#[derive(Debug, PartialEq)]
22
#[derive(Debug, PartialEq)]
21
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
23
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
22
pub struct MediaList {
24
pub struct MediaList {
23
    pub media_queries: Vec<MediaQuery>
25
    pub media_queries: Vec<MediaQuery>
24
}
26
}
25
27
26
impl ToCss for MediaList {
28
impl ToCss for MediaList {
 Lines 41-60   impl Default for MediaList { Link Here 
41
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
43
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
42
pub enum Range<T> {
44
pub enum Range<T> {
43
    Min(T),
45
    Min(T),
44
    Max(T),
46
    Max(T),
45
    Eq(T),
47
    Eq(T),
46
}
48
}
47
49
48
impl Range<specified::Length> {
50
impl Range<specified::Length> {
49
    fn to_computed_range(&self, viewport_size: Size2D<Au>) -> Range<Au> {
51
    fn to_computed_range(&self, viewport_size: Size2D<Au>, default_values: &ComputedValues) -> Range<Au> {
50
        // http://dev.w3.org/csswg/mediaqueries3/#units
52
        // http://dev.w3.org/csswg/mediaqueries3/#units
51
        // em units are relative to the initial font-size.
53
        // em units are relative to the initial font-size.
52
        let context = computed::Context::initial(viewport_size, false);
54
        let context = computed::Context {
55
            is_root_element: false,
56
            viewport_size: viewport_size,
57
            inherited_style: default_values,
58
            // This cloning business is kind of dumb.... It's because Context
59
            // insists on having an actual ComputedValues inside itself.
60
            style: default_values.clone(),
61
            font_metrics_provider: None
62
        };
53
63
54
        match *self {
64
        match *self {
55
            Range::Min(ref width) => Range::Min(width.to_computed_value(&context)),
65
            Range::Min(ref width) => Range::Min(width.to_computed_value(&context)),
56
            Range::Max(ref width) => Range::Max(width.to_computed_value(&context)),
66
            Range::Max(ref width) => Range::Max(width.to_computed_value(&context)),
57
            Range::Eq(ref width) => Range::Eq(width.to_computed_value(&context))
67
            Range::Eq(ref width) => Range::Eq(width.to_computed_value(&context))
58
        }
68
        }
59
    }
69
    }
60
}
70
}
 Lines 159-184   pub enum MediaType { Link Here 
159
    Unknown(Atom),
169
    Unknown(Atom),
160
}
170
}
161
171
162
#[derive(Debug)]
172
#[derive(Debug)]
163
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
173
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
164
pub struct Device {
174
pub struct Device {
165
    pub media_type: MediaType,
175
    pub media_type: MediaType,
166
    pub viewport_size: TypedSize2D<f32, ViewportPx>,
176
    pub viewport_size: TypedSize2D<f32, ViewportPx>,
177
    #[cfg(feature = "gecko")]
178
    pub default_values: Arc<ComputedValues>,
167
}
179
}
168
180
169
impl Device {
181
impl Device {
182
    #[cfg(feature = "servo")]
170
    pub fn new(media_type: MediaType, viewport_size: TypedSize2D<f32, ViewportPx>) -> Device {
183
    pub fn new(media_type: MediaType, viewport_size: TypedSize2D<f32, ViewportPx>) -> Device {
171
        Device {
184
        Device {
172
            media_type: media_type,
185
            media_type: media_type,
173
            viewport_size: viewport_size,
186
            viewport_size: viewport_size,
174
        }
187
        }
175
    }
188
    }
176
189
190
    #[cfg(feature = "servo")]
191
    pub fn default_values(&self) -> &ComputedValues {
192
        ComputedValues::initial_values()
193
    }
194
195
    #[cfg(feature = "gecko")]
196
    pub fn new(media_type: MediaType, viewport_size: TypedSize2D<f32, ViewportPx>,
197
               default_values: &Arc<ComputedValues>) -> Device {
198
        Device {
199
            media_type: media_type,
200
            viewport_size: viewport_size,
201
            default_values: default_values.clone(),
202
        }
203
    }
204
205
    #[cfg(feature = "gecko")]
206
    pub fn default_values(&self) -> &ComputedValues {
207
        &*self.default_values
208
    }
209
177
    #[inline]
210
    #[inline]
178
    pub fn au_viewport_size(&self) -> Size2D<Au> {
211
    pub fn au_viewport_size(&self) -> Size2D<Au> {
179
        Size2D::new(Au::from_f32_px(self.viewport_size.width),
212
        Size2D::new(Au::from_f32_px(self.viewport_size.width),
180
                    Au::from_f32_px(self.viewport_size.height))
213
                    Au::from_f32_px(self.viewport_size.height))
181
    }
214
    }
182
215
183
}
216
}
184
217
 Lines 281-297   impl MediaList { Link Here 
281
                MediaQueryType::MediaType(ref media_type) => *media_type == device.media_type,
314
                MediaQueryType::MediaType(ref media_type) => *media_type == device.media_type,
282
                MediaQueryType::All => true,
315
                MediaQueryType::All => true,
283
            };
316
            };
284
317
285
            // Check if all conditions match (AND condition)
318
            // Check if all conditions match (AND condition)
286
            let query_match = media_match && mq.expressions.iter().all(|expression| {
319
            let query_match = media_match && mq.expressions.iter().all(|expression| {
287
                match *expression {
320
                match *expression {
288
                    Expression::Width(ref value) =>
321
                    Expression::Width(ref value) =>
289
                        value.to_computed_range(viewport_size).evaluate(viewport_size.width),
322
                        value.to_computed_range(viewport_size, device.default_values()).evaluate(viewport_size.width),
290
                }
323
                }
291
            });
324
            });
292
325
293
            // Apply the logical NOT qualifier to the result
326
            // Apply the logical NOT qualifier to the result
294
            match mq.qualifier {
327
            match mq.qualifier {
295
                Some(Qualifier::Not) => !query_match,
328
                Some(Qualifier::Not) => !query_match,
296
                _ => query_match,
329
                _ => query_match,
297
            }
330
            }
(-)a/servo/components/style/values/computed/mod.rs (-14 lines)
Line     Link Here 
 Lines 37-66   pub struct Context<'a> { Link Here 
37
}
37
}
38
38
39
impl<'a> Context<'a> {
39
impl<'a> Context<'a> {
40
    pub fn is_root_element(&self) -> bool { self.is_root_element }
40
    pub fn is_root_element(&self) -> bool { self.is_root_element }
41
    pub fn viewport_size(&self) -> Size2D<Au> { self.viewport_size }
41
    pub fn viewport_size(&self) -> Size2D<Au> { self.viewport_size }
42
    pub fn inherited_style(&self) -> &ComputedValues { &self.inherited_style }
42
    pub fn inherited_style(&self) -> &ComputedValues { &self.inherited_style }
43
    pub fn style(&self) -> &ComputedValues { &self.style }
43
    pub fn style(&self) -> &ComputedValues { &self.style }
44
    pub fn mutate_style(&mut self) -> &mut ComputedValues { &mut self.style }
44
    pub fn mutate_style(&mut self) -> &mut ComputedValues { &mut self.style }
45
46
    /// Creates a dummy computed context for use in multiple places, like
47
    /// evaluating media queries.
48
    pub fn initial(viewport_size: Size2D<Au>, is_root_element: bool) -> Self {
49
        let initial_style = ComputedValues::initial_values();
50
        // FIXME: Enforce a font metrics provider.
51
        Context {
52
            is_root_element: is_root_element,
53
            viewport_size: viewport_size,
54
            inherited_style: initial_style,
55
            style: initial_style.clone(),
56
            font_metrics_provider: None,
57
        }
58
    }
59
}
45
}
60
46
61
pub trait ToComputedValue {
47
pub trait ToComputedValue {
62
    type ComputedValue;
48
    type ComputedValue;
63
49
64
    #[inline]
50
    #[inline]
65
    fn to_computed_value(&self, _context: &Context) -> Self::ComputedValue;
51
    fn to_computed_value(&self, _context: &Context) -> Self::ComputedValue;
66
52
(-)a/servo/ports/geckolib/glue.rs (+1 lines)
Line     Link Here 
 Lines 557-572   pub extern "C" fn Servo_StyleSet_Init(pr Link Here 
557
}
557
}
558
558
559
#[no_mangle]
559
#[no_mangle]
560
pub extern "C" fn Servo_StyleSet_RecomputeDefaultStyles(
560
pub extern "C" fn Servo_StyleSet_RecomputeDefaultStyles(
561
  raw_data: RawServoStyleSetBorrowed,
561
  raw_data: RawServoStyleSetBorrowed,
562
  pres_context: RawGeckoPresContextBorrowed) {
562
  pres_context: RawGeckoPresContextBorrowed) {
563
    let mut data = PerDocumentStyleData::from_ffi(raw_data).borrow_mut();
563
    let mut data = PerDocumentStyleData::from_ffi(raw_data).borrow_mut();
564
    data.default_computed_values = ComputedValues::default_values(pres_context);
564
    data.default_computed_values = ComputedValues::default_values(pres_context);
565
    // FIXME(bz): We need to update our Stylist's Device's computed values, but how?
565
}
566
}
566
567
567
#[no_mangle]
568
#[no_mangle]
568
pub extern "C" fn Servo_StyleSet_Drop(data: RawServoStyleSetOwned) -> () {
569
pub extern "C" fn Servo_StyleSet_Drop(data: RawServoStyleSetOwned) -> () {
569
    let _ = data.into_box::<PerDocumentStyleData>();
570
    let _ = data.into_box::<PerDocumentStyleData>();
570
}
571
}
571
572
572
573

Return to bug 1298588