1use crate::{
7 AtlasTextureId, AtlasTextureKind, Bounds, Point, Rgba, ScaledPixels, Scene, TileId, point,
8 scene::{PaintOperation, Primitive, TransformationMatrix},
9 size,
10};
11use std::rc::Rc;
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
15pub struct LayerKey(pub u32);
16
17#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
19pub struct TileCoord {
20 pub x: i32,
22 pub y: i32,
24}
25
26pub const LAYER_TILE_TEXTURE_BASE: u32 = 0xF000_0000;
28pub(crate) const LAYER_KEY_LIMIT: u32 = 0x0100_0000;
29const TILE_COORD_BIAS: i32 = 2048;
30
31pub fn layer_tile_texture_id(layer: LayerKey) -> AtlasTextureId {
33 debug_assert!(layer.0 < LAYER_KEY_LIMIT);
34 AtlasTextureId {
35 index: LAYER_TILE_TEXTURE_BASE + layer.0,
36 kind: AtlasTextureKind::Polychrome,
37 }
38}
39
40pub fn layer_tile_id(tile: TileCoord) -> TileId {
42 debug_assert!((-TILE_COORD_BIAS..TILE_COORD_BIAS).contains(&tile.x));
43 debug_assert!((-TILE_COORD_BIAS..TILE_COORD_BIAS).contains(&tile.y));
44 let x = (tile.x + TILE_COORD_BIAS) as u32;
45 let y = (tile.y + TILE_COORD_BIAS) as u32;
46 TileId((x << 16) | y)
47}
48
49pub fn decode_layer_tile(texture: AtlasTextureId, tile: TileId) -> Option<(LayerKey, TileCoord)> {
51 if texture.kind != AtlasTextureKind::Polychrome
52 || texture.index < LAYER_TILE_TEXTURE_BASE
53 || texture.index - LAYER_TILE_TEXTURE_BASE >= LAYER_KEY_LIMIT
54 {
55 return None;
56 }
57 let layer = LayerKey(texture.index - LAYER_TILE_TEXTURE_BASE);
58 let x = (tile.0 >> 16) as i32 - TILE_COORD_BIAS;
59 let y = (tile.0 & 0xFFFF) as i32 - TILE_COORD_BIAS;
60 Some((layer, TileCoord { x, y }))
61}
62
63#[derive(Default)]
66pub struct SceneLayers {
67 pub frames: Vec<LayerFrame>,
69}
70
71impl SceneLayers {
72 pub fn clear(&mut self) {
74 self.frames.clear();
75 }
76}
77
78#[derive(Clone)]
80pub struct LayerFrame {
81 pub key: LayerKey,
83 pub generation: u64,
85 pub background: Rgba,
87 pub tile_size: u32,
89 pub content: LayerContent,
91 pub dirty_tiles: Vec<TileCoord>,
93}
94
95impl LayerFrame {
96 pub fn tile_bounds(&self, tile: TileCoord) -> Bounds<ScaledPixels> {
98 let side = self.tile_size as f32;
99 Bounds {
100 origin: point(
101 ScaledPixels(tile.x as f32 * side),
102 ScaledPixels(tile.y as f32 * side),
103 ),
104 size: size(ScaledPixels(side), ScaledPixels(side)),
105 }
106 }
107
108 pub fn tile_scene(&self, tile: TileCoord) -> Scene {
112 let bounds = self.tile_bounds(tile);
113 let delta = point(
114 ScaledPixels(-bounds.origin.x.0),
115 ScaledPixels(-bounds.origin.y.0),
116 );
117 let mut scene = Scene::default();
118 for operation in self.content.operations_over(bounds) {
119 match operation {
120 PaintOperation::Primitive(primitive) => {
121 if visible_bounds(primitive).intersects(&bounds) {
122 scene.insert_primitive(translate_primitive(primitive, delta));
123 }
124 }
125 PaintOperation::StartLayer(layer_bounds) => {
126 scene.push_layer(translate_bounds(*layer_bounds, delta))
127 }
128 PaintOperation::EndLayer => scene.pop_layer(),
129 PaintOperation::FastRoundedClip(clip) => {
130 crate::fast::rounded_clip::record(&mut scene, clip.translated(delta))
131 }
132 }
133 }
134 scene.finish();
135 scene
136 }
137}
138
139#[derive(Clone, Default)]
145pub struct LayerContent {
146 parts: Rc<[LayerPart]>,
147}
148
149#[derive(Clone)]
151pub(crate) struct LayerPart {
152 pub(crate) bounds: Option<Bounds<ScaledPixels>>,
155 pub(crate) scene: Rc<Scene>,
158}
159
160impl From<Scene> for LayerContent {
161 fn from(scene: Scene) -> Self {
162 Self::from(Rc::new(scene))
163 }
164}
165
166impl From<Rc<Scene>> for LayerContent {
167 fn from(scene: Rc<Scene>) -> Self {
168 LayerContent {
169 parts: Rc::new([LayerPart {
170 bounds: None,
171 scene,
172 }]),
173 }
174 }
175}
176
177impl LayerContent {
178 pub(crate) fn from_parts(parts: impl IntoIterator<Item = LayerPart>) -> Self {
180 LayerContent {
181 parts: parts.into_iter().collect(),
182 }
183 }
184
185 pub fn scene(&self) -> Option<&Scene> {
187 match &*self.parts {
188 [part] => Some(&part.scene),
189 _ => None,
190 }
191 }
192
193 #[cfg(test)]
195 pub(crate) fn part_scenes(&self) -> impl Iterator<Item = &Rc<Scene>> {
196 self.parts.iter().map(|part| &part.scene)
197 }
198
199 pub fn ptr_eq(&self, other: &Self) -> bool {
201 Rc::ptr_eq(&self.parts, &other.parts)
202 }
203
204 pub(crate) fn operations(&self) -> impl Iterator<Item = &PaintOperation> {
206 self.parts
207 .iter()
208 .flat_map(|part| part.scene.paint_operations.iter())
209 }
210
211 pub(crate) fn operations_over(
216 &self,
217 bounds: Bounds<ScaledPixels>,
218 ) -> impl Iterator<Item = &PaintOperation> {
219 self.parts
220 .iter()
221 .filter(move |part| part.bounds.is_none_or(|part| part.intersects(&bounds)))
222 .flat_map(|part| part.scene.paint_operations.iter())
223 }
224}
225
226fn translate_bounds(
227 bounds: Bounds<ScaledPixels>,
228 delta: Point<ScaledPixels>,
229) -> Bounds<ScaledPixels> {
230 Bounds {
231 origin: bounds.origin + delta,
232 size: bounds.size,
233 }
234}
235
236pub(crate) fn visible_bounds(primitive: &Primitive) -> Bounds<ScaledPixels> {
242 drawn_bounds(primitive).intersect(&primitive.content_mask().bounds)
243}
244
245pub(crate) fn drawn_bounds(primitive: &Primitive) -> Bounds<ScaledPixels> {
248 match primitive {
249 Primitive::Shadow(shadow) if shadow.inset != 0 => shadow.element_bounds,
250 Primitive::Shadow(shadow) => {
251 let margin = ScaledPixels(3. * shadow.blur_radius.0.max(0.));
252 Bounds {
253 origin: point(
254 shadow.bounds.origin.x - margin,
255 shadow.bounds.origin.y - margin,
256 ),
257 size: size(
258 shadow.bounds.size.width + margin * 2.,
259 shadow.bounds.size.height + margin * 2.,
260 ),
261 }
262 }
263 Primitive::MonochromeSprite(sprite) => {
264 transformed_bounds(sprite.bounds, &sprite.transformation)
265 }
266 Primitive::SubpixelSprite(sprite) => {
267 transformed_bounds(sprite.bounds, &sprite.transformation)
268 }
269 primitive => *primitive.bounds(),
270 }
271}
272
273fn transformed_bounds(
275 bounds: Bounds<ScaledPixels>,
276 matrix: &TransformationMatrix,
277) -> Bounds<ScaledPixels> {
278 if *matrix == TransformationMatrix::unit() {
279 return bounds;
280 }
281 let apply = |x: f32, y: f32| {
282 let m = &matrix.rotation_scale;
283 (
284 m[0][0] * x + m[0][1] * y + matrix.translation[0],
285 m[1][0] * x + m[1][1] * y + matrix.translation[1],
286 )
287 };
288 let (x0, y0) = (bounds.origin.x.0, bounds.origin.y.0);
289 let (x1, y1) = (x0 + bounds.size.width.0, y0 + bounds.size.height.0);
290 let corners = [apply(x0, y0), apply(x1, y0), apply(x0, y1), apply(x1, y1)];
291 let min_x = corners.iter().map(|c| c.0).fold(f32::INFINITY, f32::min);
292 let max_x = corners
293 .iter()
294 .map(|c| c.0)
295 .fold(f32::NEG_INFINITY, f32::max);
296 let min_y = corners.iter().map(|c| c.1).fold(f32::INFINITY, f32::min);
297 let max_y = corners
298 .iter()
299 .map(|c| c.1)
300 .fold(f32::NEG_INFINITY, f32::max);
301 Bounds {
302 origin: point(ScaledPixels(min_x), ScaledPixels(min_y)),
303 size: size(ScaledPixels(max_x - min_x), ScaledPixels(max_y - min_y)),
304 }
305}
306
307pub(crate) fn translate_primitive(primitive: &Primitive, delta: Point<ScaledPixels>) -> Primitive {
310 let mut primitive = primitive.clone();
311 move_primitive(&mut primitive, delta);
312 primitive
313}
314
315pub(crate) fn move_primitive(primitive: &mut Primitive, delta: Point<ScaledPixels>) {
317 let mv = |bounds: &mut Bounds<ScaledPixels>| *bounds = translate_bounds(*bounds, delta);
318 match primitive {
319 Primitive::Shadow(shadow) => {
320 mv(&mut shadow.bounds);
321 mv(&mut shadow.element_bounds);
322 mv(&mut shadow.content_mask.bounds);
323 }
324 Primitive::Quad(quad) => {
325 mv(&mut quad.bounds);
326 mv(&mut quad.content_mask.bounds);
327 }
328 Primitive::Path(path) => {
329 mv(&mut path.bounds);
330 mv(&mut path.content_mask.bounds);
331 for vertex in &mut path.vertices {
332 vertex.xy_position = vertex.xy_position + delta;
333 if !vertex.content_mask.bounds.is_empty() {
336 mv(&mut vertex.content_mask.bounds);
337 }
338 }
339 }
340 Primitive::Underline(underline) => {
341 mv(&mut underline.bounds);
342 mv(&mut underline.content_mask.bounds);
343 }
344 Primitive::MonochromeSprite(sprite) => {
345 mv(&mut sprite.bounds);
346 mv(&mut sprite.content_mask.bounds);
347 translate_transformation(&mut sprite.transformation, delta);
348 }
349 Primitive::SubpixelSprite(sprite) => {
350 mv(&mut sprite.bounds);
351 mv(&mut sprite.content_mask.bounds);
352 translate_transformation(&mut sprite.transformation, delta);
353 }
354 Primitive::PolychromeSprite(sprite) => {
355 mv(&mut sprite.bounds);
356 mv(&mut sprite.content_mask.bounds);
357 }
358 Primitive::Surface(surface) => {
359 mv(&mut surface.bounds);
360 mv(&mut surface.content_mask.bounds);
361 }
362 }
363}
364
365fn translate_transformation(matrix: &mut TransformationMatrix, delta: Point<ScaledPixels>) {
370 let r = matrix.rotation_scale;
371 let (dx, dy) = (delta.x.0, delta.y.0);
372 matrix.translation[0] += dx - (r[0][0] * dx + r[0][1] * dy);
373 matrix.translation[1] += dy - (r[1][0] * dx + r[1][1] * dy);
374}