Skip to main content

layout/display_list/
background.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use app_units::Au;
6use euclid::{Size2D, Vector2D};
7use style::computed_values::background_attachment::SingleComputedValue as BackgroundAttachment;
8use style::computed_values::background_blend_mode::SingleComputedValue as BackgroundBlendMode;
9use style::computed_values::background_clip::single_value::T as Clip;
10use style::computed_values::background_origin::single_value::T as Origin;
11use style::properties::ComputedValues;
12use style::values::computed::LengthPercentage;
13use style::values::computed::background::BackgroundSize as Size;
14use style::values::specified::background::{
15    BackgroundRepeat as RepeatXY, BackgroundRepeatKeyword as Repeat,
16};
17use webrender_api::{self as wr, units};
18use wr::ClipChainId;
19
20use crate::display_list::{DisplayListBuilder, TraversalState};
21use crate::replaced::NaturalSizes;
22
23pub(super) struct BackgroundLayer {
24    pub common: wr::CommonItemProperties,
25    pub bounds: units::LayoutRect,
26    pub tile_size: units::LayoutSize,
27    pub tile_spacing: units::LayoutSize,
28    pub repeat: bool,
29    pub blend_mode: BackgroundBlendMode,
30}
31
32#[derive(Debug)]
33struct Layout1DResult {
34    repeat: bool,
35    bounds_origin: f32,
36    bounds_size: f32,
37    tile_spacing: f32,
38}
39
40pub(crate) fn get_cyclic<T>(values: &[T], layer_index: usize) -> &T {
41    &values[layer_index % values.len()]
42}
43
44pub(super) struct BackgroundPainter<'a> {
45    pub style: &'a ComputedValues,
46    pub positioning_area_override: Option<units::LayoutRect>,
47    pub painting_area_override: Option<units::LayoutRect>,
48}
49
50impl<'a> BackgroundPainter<'a> {
51    /// Get the painting area for this background, which is the actual rectangle in the
52    /// current coordinate system that the background will be painted.
53    pub(super) fn painting_area(
54        &self,
55        fragment_builder: &'a super::BuilderForBoxFragment,
56        builder: &mut super::DisplayListBuilder,
57        layer_index: usize,
58    ) -> units::LayoutRect {
59        let fb = fragment_builder;
60        if let Some(painting_area_override) = self.painting_area_override.as_ref() {
61            return *painting_area_override;
62        }
63        if self.positioning_area_override.is_some() {
64            return fb.border_rect;
65        }
66
67        let background = self.style.get_background();
68        if &BackgroundAttachment::Fixed ==
69            get_cyclic(&background.background_attachment.0, layer_index)
70        {
71            return builder.paint_info.viewport_details.layout_size().into();
72        }
73
74        match get_cyclic(&background.background_clip.0, layer_index) {
75            Clip::ContentBox => *fragment_builder.content_rect(),
76            Clip::PaddingBox => *fragment_builder.padding_rect(),
77            Clip::BorderBox => fragment_builder.border_rect,
78        }
79    }
80
81    fn clip(
82        &self,
83        fragment_builder: &'a super::BuilderForBoxFragment,
84        builder: &mut DisplayListBuilder,
85        state: &TraversalState,
86        layer_index: usize,
87    ) -> Option<ClipChainId> {
88        if self.painting_area_override.is_some() {
89            return None;
90        }
91
92        if self.positioning_area_override.is_some() {
93            return fragment_builder.border_edge_clip(builder, state, false);
94        }
95
96        // The 'backgound-clip' property maps directly to `clip_rect` in `CommonItemProperties`:
97        let background = self.style.get_background();
98        let force_clip_creation = get_cyclic(&background.background_attachment.0, layer_index) ==
99            &BackgroundAttachment::Fixed;
100        match get_cyclic(&background.background_clip.0, layer_index) {
101            Clip::ContentBox => {
102                fragment_builder.content_edge_clip(builder, state, force_clip_creation)
103            },
104            Clip::PaddingBox => {
105                fragment_builder.padding_edge_clip(builder, state, force_clip_creation)
106            },
107            Clip::BorderBox => {
108                fragment_builder.border_edge_clip(builder, state, force_clip_creation)
109            },
110        }
111    }
112
113    /// Get the [`wr::CommonItemProperties`] for this background. This includes any clipping
114    /// established by border radii as well as special clipping and spatial node assignment
115    /// necessary for `background-attachment`.
116    pub(super) fn common_properties(
117        &self,
118        fragment_builder: &'a super::BuilderForBoxFragment,
119        builder: &mut super::DisplayListBuilder,
120        state: &TraversalState,
121        layer_index: usize,
122        painting_area: units::LayoutRect,
123    ) -> wr::CommonItemProperties {
124        let clip = self.clip(fragment_builder, builder, state, layer_index);
125        let style = fragment_builder.fragment.style();
126        let mut common = builder.common_properties(state, painting_area, style);
127        if let Some(clip_chain_id) = clip {
128            common.clip_chain_id = clip_chain_id;
129        }
130        if &BackgroundAttachment::Fixed ==
131            get_cyclic(&style.get_background().background_attachment.0, layer_index)
132        {
133            common.spatial_id = builder.spatial_id(builder.current_reference_frame_scroll_node_id);
134        }
135        common
136    }
137
138    /// Get the positioning area of the background which is the rectangle that defines where
139    /// the origin of the background content is, regardless of where the background is actual
140    /// painted.
141    pub(super) fn positioning_area(
142        &self,
143        fragment_builder: &'a super::BuilderForBoxFragment,
144        builder: &mut super::DisplayListBuilder,
145        layer_index: usize,
146    ) -> units::LayoutRect {
147        if let Some(positioning_area_override) = self.positioning_area_override {
148            return positioning_area_override;
149        }
150
151        match get_cyclic(
152            &self.style.get_background().background_attachment.0,
153            layer_index,
154        ) {
155            BackgroundAttachment::Scroll => match get_cyclic(
156                &self.style.get_background().background_origin.0,
157                layer_index,
158            ) {
159                Origin::ContentBox => *fragment_builder.content_rect(),
160                Origin::PaddingBox => *fragment_builder.padding_rect(),
161                Origin::BorderBox => fragment_builder.border_rect,
162            },
163            BackgroundAttachment::Fixed => builder.paint_info.viewport_details.layout_size().into(),
164        }
165    }
166}
167
168pub(super) fn layout_layer(
169    fragment_builder: &mut super::BuilderForBoxFragment,
170    painter: &BackgroundPainter,
171    builder: &mut DisplayListBuilder,
172    state: &TraversalState,
173    layer_index: usize,
174    natural_sizes: NaturalSizes,
175) -> Option<BackgroundLayer> {
176    let painting_area = painter.painting_area(fragment_builder, builder, layer_index);
177    let positioning_area = painter.positioning_area(fragment_builder, builder, layer_index);
178    let common =
179        painter.common_properties(fragment_builder, builder, state, layer_index, painting_area);
180
181    // https://drafts.csswg.org/css-backgrounds/#background-size
182    enum ContainOrCover {
183        Contain,
184        Cover,
185    }
186    let size_contain_or_cover = |background_size| {
187        let mut tile_size = positioning_area.size();
188        if let Some(natural_ratio) = natural_sizes.ratio {
189            let positioning_ratio = positioning_area.size().width / positioning_area.size().height;
190            // Whether the tile width (as opposed to height)
191            // is scaled to that of the positioning area
192            let fit_width = match background_size {
193                ContainOrCover::Contain => positioning_ratio <= natural_ratio,
194                ContainOrCover::Cover => positioning_ratio > natural_ratio,
195            };
196            // The other dimension needs to be adjusted
197            if fit_width {
198                tile_size.height = tile_size.width / natural_ratio
199            } else {
200                tile_size.width = tile_size.height * natural_ratio
201            }
202        }
203        tile_size
204    };
205
206    let b = painter.style.get_background();
207    let mut tile_size = match get_cyclic(&b.background_size.0, layer_index) {
208        Size::Contain => size_contain_or_cover(ContainOrCover::Contain),
209        Size::Cover => size_contain_or_cover(ContainOrCover::Cover),
210        Size::ExplicitSize { width, height } => {
211            let mut width = width.non_auto().map(|lp| {
212                lp.0.to_used_value(Au::from_f32_px(positioning_area.size().width))
213            });
214            let mut height = height.non_auto().map(|lp| {
215                lp.0.to_used_value(Au::from_f32_px(positioning_area.size().height))
216            });
217
218            if width.is_none() && height.is_none() {
219                // Both computed values are 'auto':
220                // use natural sizes, treating missing width or height as 'auto'
221                width = natural_sizes.width;
222                height = natural_sizes.height;
223            }
224
225            match (width, height) {
226                (Some(w), Some(h)) => units::LayoutSize::new(w.to_f32_px(), h.to_f32_px()),
227                (Some(w), None) => {
228                    let h = if let Some(natural_ratio) = natural_sizes.ratio {
229                        w.scale_by(1.0 / natural_ratio)
230                    } else if let Some(natural_height) = natural_sizes.height {
231                        natural_height
232                    } else {
233                        // Treated as 100%
234                        Au::from_f32_px(positioning_area.size().height)
235                    };
236                    units::LayoutSize::new(w.to_f32_px(), h.to_f32_px())
237                },
238                (None, Some(h)) => {
239                    let w = if let Some(natural_ratio) = natural_sizes.ratio {
240                        h.scale_by(natural_ratio)
241                    } else if let Some(natural_width) = natural_sizes.width {
242                        natural_width
243                    } else {
244                        // Treated as 100%
245                        Au::from_f32_px(positioning_area.size().width)
246                    };
247                    units::LayoutSize::new(w.to_f32_px(), h.to_f32_px())
248                },
249                // Both comptued values were 'auto', and neither natural size is present
250                (None, None) => size_contain_or_cover(ContainOrCover::Contain),
251            }
252        },
253    };
254
255    if tile_size.width == 0.0 || tile_size.height == 0.0 {
256        return None;
257    }
258
259    let RepeatXY(repeat_x, repeat_y) = *get_cyclic(&b.background_repeat.0, layer_index);
260    let result_x = layout_1d(
261        &mut tile_size.width,
262        repeat_x,
263        get_cyclic(&b.background_position_x.0, layer_index),
264        painting_area.min.x - positioning_area.min.x,
265        painting_area.size().width,
266        positioning_area.size().width,
267    );
268    let result_y = layout_1d(
269        &mut tile_size.height,
270        repeat_y,
271        get_cyclic(&b.background_position_y.0, layer_index),
272        painting_area.min.y - positioning_area.min.y,
273        painting_area.size().height,
274        positioning_area.size().height,
275    );
276    let bounds = units::LayoutRect::from_origin_and_size(
277        positioning_area.min + Vector2D::new(result_x.bounds_origin, result_y.bounds_origin),
278        Size2D::new(result_x.bounds_size, result_y.bounds_size),
279    );
280    let tile_spacing = units::LayoutSize::new(result_x.tile_spacing, result_y.tile_spacing);
281
282    let blend_mode = *get_cyclic(&b.background_blend_mode.0, layer_index);
283    Some(BackgroundLayer {
284        common,
285        bounds,
286        tile_size,
287        tile_spacing,
288        repeat: result_x.repeat || result_y.repeat,
289        blend_mode,
290    })
291}
292
293/// Abstract over the horizontal or vertical dimension
294/// Coordinates (0, 0) for the purpose of this function are the positioning area’s origin.
295fn layout_1d(
296    tile_size: &mut f32,
297    mut repeat: Repeat,
298    position: &LengthPercentage,
299    painting_area_origin: f32,
300    painting_area_size: f32,
301    positioning_area_size: f32,
302) -> Layout1DResult {
303    // https://drafts.csswg.org/css-backgrounds/#background-repeat
304    // > If background-repeat is round for one (or both) dimensions, there is a second step.
305    // > The UA must scale the image in that dimension (or both dimensions) so that it fits
306    // > a whole number of times in the background positioning area. In the case of the
307    // > width (height is analogous):
308    // >
309    // > | If X ≠ 0 is the width of the image after step one and W is the width of the
310    // > | background positioning area, then the rounded width X' = W / round(W / X) where
311    // > | round() is a function that returns the nearest natural number (integer greater than
312    // > | zero).
313    if let Repeat::Round = repeat {
314        let round = |number: f32| number.round().max(1.0);
315        if positioning_area_size != 0.0 {
316            *tile_size = positioning_area_size / round(positioning_area_size / *tile_size);
317        }
318    }
319    // https://drafts.csswg.org/css-backgrounds/#background-position
320    let mut position = position
321        .to_used_value(Au::from_f32_px(positioning_area_size - *tile_size))
322        .to_f32_px();
323    let mut tile_spacing = 0.0;
324    // https://drafts.csswg.org/css-backgrounds/#background-repeat
325    if let Repeat::Space = repeat {
326        // The most entire tiles we can fit
327        let tile_count = (positioning_area_size / *tile_size).floor();
328        if tile_count >= 2.0 {
329            position = 0.0;
330            // Make the outsides of the first and last of that many tiles
331            // touch the edges of the positioning area:
332            let total_space = positioning_area_size - *tile_size * tile_count;
333            let spaces_count = tile_count - 1.0;
334            tile_spacing = total_space / spaces_count;
335        } else {
336            repeat = Repeat::NoRepeat
337        }
338    }
339    match repeat {
340        Repeat::Repeat | Repeat::Round | Repeat::Space => {
341            // WebRender’s `RepeatingImageDisplayItem` contains a `bounds` rectangle and:
342            //
343            // * The tiling is clipped to the intersection of `clip_rect` and `bounds`
344            // * The origin (top-left corner) of `bounds` is the position
345            //   of the “first” (top-left-most) tile.
346            //
347            // In the general case that first tile is not the one that is positioned by
348            // `background-position`.
349            // We want it to be the top-left-most tile that intersects with `clip_rect`.
350            // We find it by offsetting by a whole number of strides,
351            // then compute `bounds` such that:
352            //
353            // * Its bottom-right is the bottom-right of `clip_rect`
354            // * Its top-left is the top-left of first tile.
355            let tile_stride = *tile_size + tile_spacing;
356            let offset = position - painting_area_origin;
357            let bounds_origin = position - tile_stride * (offset / tile_stride).ceil();
358            let bounds_end = painting_area_origin + painting_area_size;
359            let bounds_size = bounds_end - bounds_origin;
360            Layout1DResult {
361                repeat: true,
362                bounds_origin,
363                bounds_size,
364                tile_spacing,
365            }
366        },
367        Repeat::NoRepeat => {
368            // `RepeatingImageDisplayItem` always repeats in both dimension.
369            // When we want only one of the dimensions to repeat,
370            // we use the `bounds` rectangle to clip the tiling to one tile
371            // in that dimension.
372            Layout1DResult {
373                repeat: false,
374                bounds_origin: position,
375                bounds_size: *tile_size,
376                tile_spacing: 0.0,
377            }
378        },
379    }
380}