1use 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 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 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 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 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 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 let fit_width = match background_size {
193 ContainOrCover::Contain => positioning_ratio <= natural_ratio,
194 ContainOrCover::Cover => positioning_ratio > natural_ratio,
195 };
196 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 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 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 Au::from_f32_px(positioning_area.size().width)
246 };
247 units::LayoutSize::new(w.to_f32_px(), h.to_f32_px())
248 },
249 (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
293fn 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 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 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 if let Repeat::Space = repeat {
326 let tile_count = (positioning_area_size / *tile_size).floor();
328 if tile_count >= 2.0 {
329 position = 0.0;
330 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 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 Layout1DResult {
373 repeat: false,
374 bounds_origin: position,
375 bounds_size: *tile_size,
376 tile_spacing: 0.0,
377 }
378 },
379 }
380}