diff options
author | 2024-01-18 10:35:27 +0100 | |
---|---|---|
committer | 2024-01-18 10:35:27 +0100 | |
commit | 4b7744b9806397c9891b1fc179df8a61eaa3670d (patch) | |
tree | 3f4d5799f32b7e55566e40fb99e0093a610c65e9 /tiny_skia | |
parent | 5fc49edc55a0e64c4c46ca55eddafe9d4e8232e1 (diff) | |
download | iced-4b7744b9806397c9891b1fc179df8a61eaa3670d.tar.gz iced-4b7744b9806397c9891b1fc179df8a61eaa3670d.tar.bz2 iced-4b7744b9806397c9891b1fc179df8a61eaa3670d.zip |
Support out-of-order `Buffer` ages in `iced_tiny_skia`
Diffstat (limited to 'tiny_skia')
-rw-r--r-- | tiny_skia/src/window/compositor.rs | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/tiny_skia/src/window/compositor.rs b/tiny_skia/src/window/compositor.rs index 86400aa0..c0aabdb6 100644 --- a/tiny_skia/src/window/compositor.rs +++ b/tiny_skia/src/window/compositor.rs @@ -20,9 +20,9 @@ pub struct Surface { Box<dyn compositor::Window>, >, clip_mask: tiny_skia::Mask, - // Primitives of existing buffers, by decreasing age - primitives: VecDeque<Vec<Primitive>>, + primitive_stack: VecDeque<Vec<Primitive>>, background_color: Color, + max_age: u8, } impl<Theme> crate::graphics::Compositor for Compositor<Theme> { @@ -61,8 +61,9 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> { window, clip_mask: tiny_skia::Mask::new(width, height) .expect("Create clip mask"), - primitives: VecDeque::new(), + primitive_stack: VecDeque::new(), background_color: Color::BLACK, + max_age: 0, } } @@ -74,7 +75,7 @@ impl<Theme> crate::graphics::Compositor for Compositor<Theme> { ) { surface.clip_mask = tiny_skia::Mask::new(width, height).expect("Create clip mask"); - surface.primitives.clear(); + surface.primitive_stack.clear(); } fn fetch_information(&self) -> Information { @@ -159,7 +160,6 @@ pub fn present<T: AsRef<str>>( ) .unwrap(); - // TODO Add variants to `SurfaceError`? let mut buffer = surface .window .buffer_mut() @@ -167,27 +167,25 @@ pub fn present<T: AsRef<str>>( let age = buffer.age(); - // Forget primatives for back buffers older than `age` - // Or if this is a new buffer, keep at most two. - let max = if age == 0 { 2 } else { age }; - while surface.primitives.len() as u8 > max { - let _ = surface.primitives.pop_front(); - } + let last_primitives = { + surface.max_age = surface.max_age.max(age); + surface.primitive_stack.truncate(surface.max_age as usize); - let last_primitives = if surface.primitives.len() as u8 == age { - surface.primitives.pop_front() - } else { - None + if age > 0 { + surface.primitive_stack.get(age as usize - 1) + } else { + None + } }; let damage = last_primitives .and_then(|last_primitives| { (surface.background_color == background_color) - .then(|| damage::list(&last_primitives, primitives)) + .then(|| damage::list(last_primitives, primitives)) }) .unwrap_or_else(|| vec![Rectangle::with_size(viewport.logical_size())]); - surface.primitives.push_back(primitives.to_vec()); + surface.primitive_stack.push_front(primitives.to_vec()); surface.background_color = background_color; if !damage.is_empty() { |