1use app_units::Au;
6use euclid::{Size2D, Vector2D};
7use style::computed_values::background_attachment::SingleComputedValue as BackgroundAttachment;
8use style::computed_values::background_clip::single_value::T as Clip;
9use style::computed_values::background_origin::single_value::T as Origin;
10use style::properties::ComputedValues;
11use style::values::computed::LengthPercentage;
12use style::values::computed::background::BackgroundSize as Size;
13use style::values::specified::background::{
14 BackgroundRepeat as RepeatXY, BackgroundRepeatKeyword as Repeat,
15};
16use webrender_api::{self as wr, units};
17use wr::ClipChainId;
18
19use crate::replaced::NaturalSizes;
20
21pub(super) struct BackgroundLayer {
22 pub common: wr::CommonItemProperties,
23 pub bounds: units::LayoutRect,
24 pub tile_size: units::LayoutSize,
25 pub tile_spacing: units::LayoutSize,
26 pub repeat: bool,
27}
28
29#[derive(Debug)]
30struct Layout1DResult {
31 repeat: bool,
32 bounds_origin: f32,
33 bounds_size: f32,
34 tile_spacing: f32,
35}
36
37fn get_cyclic<T>(values: &[T], layer_index: usize) -> &T {
38 &values[layer_index % values.len()]
39}
40
41pub(super) struct BackgroundPainter<'a> {
42 pub style: &'a ComputedValues,
43 pub positioning_area_override: Option<units::LayoutRect>,
44 pub painting_area_override: Option<units::LayoutRect>,
45}
46
47impl<'a> BackgroundPainter<'a> {
48 pub(super) fn painting_area(
51 &self,
52 fragment_builder: &'a super::BuilderForBoxFragment,
53 builder: &mut super::DisplayListBuilder,
54 layer_index: usize,
55 ) -> units::LayoutRect {
56 let fb = fragment_builder;
57 if let Some(painting_area_override) = self.painting_area_override.as_ref() {
58 return *painting_area_override;
59 }
60 if self.positioning_area_override.is_some() {
61 return fb.border_rect;
62 }
63
64 let background = self.style.get_background();
65 if &BackgroundAttachment::Fixed ==
66 get_cyclic(&background.background_attachment.0, layer_index)
67 {
68 return builder
69 .compositor_info
70 .viewport_details
71 .layout_size()
72 .into();
73 }
74
75 match get_cyclic(&background.background_clip.0, layer_index) {
76 Clip::ContentBox => *fragment_builder.content_rect(),
77 Clip::PaddingBox => *fragment_builder.padding_rect(),
78 Clip::BorderBox => fragment_builder.border_rect,
79 }
80 }
81
82 fn clip(
83 &self,
84 fragment_builder: &'a super::BuilderForBoxFragment,
85 builder: &mut super::DisplayListBuilder,
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, 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 => fragment_builder.content_edge_clip(builder, force_clip_creation),
102 Clip::PaddingBox => fragment_builder.padding_edge_clip(builder, force_clip_creation),
103 Clip::BorderBox => fragment_builder.border_edge_clip(builder, force_clip_creation),
104 }
105 }
106
107 pub(super) fn common_properties(
111 &self,
112 fragment_builder: &'a super::BuilderForBoxFragment,
113 builder: &mut super::DisplayListBuilder,
114 layer_index: usize,
115 painting_area: units::LayoutRect,
116 ) -> wr::CommonItemProperties {
117 let clip = self.clip(fragment_builder, builder, layer_index);
118 let style = &fragment_builder.fragment.style;
119 let mut common = builder.common_properties(painting_area, style);
120 if let Some(clip_chain_id) = clip {
121 common.clip_chain_id = clip_chain_id;
122 }
123 if &BackgroundAttachment::Fixed ==
124 get_cyclic(&style.get_background().background_attachment.0, layer_index)
125 {
126 common.spatial_id = builder.spatial_id(builder.current_reference_frame_scroll_node_id);
127 }
128 common
129 }
130
131 pub(super) fn positioning_area(
135 &self,
136 fragment_builder: &'a super::BuilderForBoxFragment,
137 builder: &mut super::DisplayListBuilder,
138 layer_index: usize,
139 ) -> units::LayoutRect {
140 if let Some(positioning_area_override) = self.positioning_area_override {
141 return positioning_area_override;
142 }
143
144 match get_cyclic(
145 &self.style.get_background().background_attachment.0,
146 layer_index,
147 ) {
148 BackgroundAttachment::Scroll => match get_cyclic(
149 &self.style.get_background().background_origin.0,
150 layer_index,
151 ) {
152 Origin::ContentBox => *fragment_builder.content_rect(),
153 Origin::PaddingBox => *fragment_builder.padding_rect(),
154 Origin::BorderBox => fragment_builder.border_rect,
155 },
156 BackgroundAttachment::Fixed => builder
157 .compositor_info
158 .viewport_details
159 .layout_size()
160 .into(),
161 }
162 }
163}
164
165pub(super) fn layout_layer(
166 fragment_builder: &mut super::BuilderForBoxFragment,
167 painter: &BackgroundPainter,
168 builder: &mut super::DisplayListBuilder,
169 layer_index: usize,
170 natural_sizes: NaturalSizes,
171) -> Option<BackgroundLayer> {
172 let painting_area = painter.painting_area(fragment_builder, builder, layer_index);
173 let positioning_area = painter.positioning_area(fragment_builder, builder, layer_index);
174 let common = painter.common_properties(fragment_builder, builder, layer_index, painting_area);
175
176 enum ContainOrCover {
178 Contain,
179 Cover,
180 }
181 let size_contain_or_cover = |background_size| {
182 let mut tile_size = positioning_area.size();
183 if let Some(natural_ratio) = natural_sizes.ratio {
184 let positioning_ratio = positioning_area.size().width / positioning_area.size().height;
185 let fit_width = match background_size {
188 ContainOrCover::Contain => positioning_ratio <= natural_ratio,
189 ContainOrCover::Cover => positioning_ratio > natural_ratio,
190 };
191 if fit_width {
193 tile_size.height = tile_size.width / natural_ratio
194 } else {
195 tile_size.width = tile_size.height * natural_ratio
196 }
197 }
198 tile_size
199 };
200
201 let b = painter.style.get_background();
202 let mut tile_size = match get_cyclic(&b.background_size.0, layer_index) {
203 Size::Contain => size_contain_or_cover(ContainOrCover::Contain),
204 Size::Cover => size_contain_or_cover(ContainOrCover::Cover),
205 Size::ExplicitSize { width, height } => {
206 let mut width = width.non_auto().map(|lp| {
207 lp.0.to_used_value(Au::from_f32_px(positioning_area.size().width))
208 });
209 let mut height = height.non_auto().map(|lp| {
210 lp.0.to_used_value(Au::from_f32_px(positioning_area.size().height))
211 });
212
213 if width.is_none() && height.is_none() {
214 width = natural_sizes.width;
217 height = natural_sizes.height;
218 }
219
220 match (width, height) {
221 (Some(w), Some(h)) => units::LayoutSize::new(w.to_f32_px(), h.to_f32_px()),
222 (Some(w), None) => {
223 let h = if let Some(natural_ratio) = natural_sizes.ratio {
224 w.scale_by(1.0 / natural_ratio)
225 } else if let Some(natural_height) = natural_sizes.height {
226 natural_height
227 } else {
228 Au::from_f32_px(positioning_area.size().height)
230 };
231 units::LayoutSize::new(w.to_f32_px(), h.to_f32_px())
232 },
233 (None, Some(h)) => {
234 let w = if let Some(natural_ratio) = natural_sizes.ratio {
235 h.scale_by(natural_ratio)
236 } else if let Some(natural_width) = natural_sizes.width {
237 natural_width
238 } else {
239 Au::from_f32_px(positioning_area.size().width)
241 };
242 units::LayoutSize::new(w.to_f32_px(), h.to_f32_px())
243 },
244 (None, None) => size_contain_or_cover(ContainOrCover::Contain),
246 }
247 },
248 };
249
250 if tile_size.width == 0.0 || tile_size.height == 0.0 {
251 return None;
252 }
253
254 let RepeatXY(repeat_x, repeat_y) = *get_cyclic(&b.background_repeat.0, layer_index);
255 let result_x = layout_1d(
256 &mut tile_size.width,
257 repeat_x,
258 get_cyclic(&b.background_position_x.0, layer_index),
259 painting_area.min.x - positioning_area.min.x,
260 painting_area.size().width,
261 positioning_area.size().width,
262 );
263 let result_y = layout_1d(
264 &mut tile_size.height,
265 repeat_y,
266 get_cyclic(&b.background_position_y.0, layer_index),
267 painting_area.min.y - positioning_area.min.y,
268 painting_area.size().height,
269 positioning_area.size().height,
270 );
271 let bounds = units::LayoutRect::from_origin_and_size(
272 positioning_area.min + Vector2D::new(result_x.bounds_origin, result_y.bounds_origin),
273 Size2D::new(result_x.bounds_size, result_y.bounds_size),
274 );
275 let tile_spacing = units::LayoutSize::new(result_x.tile_spacing, result_y.tile_spacing);
276
277 Some(BackgroundLayer {
278 common,
279 bounds,
280 tile_size,
281 tile_spacing,
282 repeat: result_x.repeat || result_y.repeat,
283 })
284}
285
286fn layout_1d(
289 tile_size: &mut f32,
290 mut repeat: Repeat,
291 position: &LengthPercentage,
292 painting_area_origin: f32,
293 painting_area_size: f32,
294 positioning_area_size: f32,
295) -> Layout1DResult {
296 if let Repeat::Round = repeat {
298 *tile_size = positioning_area_size / (positioning_area_size / *tile_size).round();
299 }
300 let mut position = position
302 .to_used_value(Au::from_f32_px(positioning_area_size - *tile_size))
303 .to_f32_px();
304 let mut tile_spacing = 0.0;
305 if let Repeat::Space = repeat {
307 let tile_count = (positioning_area_size / *tile_size).floor();
309 if tile_count >= 2.0 {
310 position = 0.0;
311 let total_space = positioning_area_size - *tile_size * tile_count;
314 let spaces_count = tile_count - 1.0;
315 tile_spacing = total_space / spaces_count;
316 } else {
317 repeat = Repeat::NoRepeat
318 }
319 }
320 match repeat {
321 Repeat::Repeat | Repeat::Round | Repeat::Space => {
322 let tile_stride = *tile_size + tile_spacing;
337 let offset = position - painting_area_origin;
338 let bounds_origin = position - tile_stride * (offset / tile_stride).ceil();
339 let bounds_end = painting_area_origin + painting_area_size;
340 let bounds_size = bounds_end - bounds_origin;
341 Layout1DResult {
342 repeat: true,
343 bounds_origin,
344 bounds_size,
345 tile_spacing,
346 }
347 },
348 Repeat::NoRepeat => {
349 Layout1DResult {
354 repeat: false,
355 bounds_origin: position,
356 bounds_size: *tile_size,
357 tile_spacing: 0.0,
358 }
359 },
360 }
361}