From 1e8554bf02f366b18b31b9ea1dfb150f7935ca06 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 Apr 2024 20:23:07 +0200 Subject: Sort damage by distance from origin in `iced_graphics::damage` --- graphics/src/damage.rs | 7 ++++--- tiny_skia/src/layer.rs | 18 ++++++++---------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/graphics/src/damage.rs b/graphics/src/damage.rs index ff8edaf5..17d60451 100644 --- a/graphics/src/damage.rs +++ b/graphics/src/damage.rs @@ -1,5 +1,5 @@ //! Compute the damage between frames. -use crate::core::Rectangle; +use crate::core::{Point, Rectangle}; /// Diffs the damage regions given some previous and current primitives. pub fn diff( @@ -50,9 +50,10 @@ pub fn group(mut damage: Vec, bounds: Rectangle) -> Vec { const AREA_THRESHOLD: f32 = 20_000.0; damage.sort_by(|a, b| { - a.x.partial_cmp(&b.x) + a.center() + .distance(Point::ORIGIN) + .partial_cmp(&b.center().distance(Point::ORIGIN)) .unwrap_or(Ordering::Equal) - .then_with(|| a.y.partial_cmp(&b.y).unwrap_or(Ordering::Equal)) }); let mut output = Vec::new(); diff --git a/tiny_skia/src/layer.rs b/tiny_skia/src/layer.rs index d3d8b988..ec87c2bf 100644 --- a/tiny_skia/src/layer.rs +++ b/tiny_skia/src/layer.rs @@ -213,16 +213,14 @@ impl Layer { ¤t.primitives, |item| match item { Item::Live(primitive) => vec![primitive.visible_bounds()], - Item::Group(primitives, bounds, transformation) => { - damage::group( - primitives - .as_slice() - .iter() - .map(Primitive::visible_bounds) - .map(|bounds| bounds * *transformation) - .collect(), - *bounds, - ) + Item::Group(primitives, group_bounds, transformation) => { + primitives + .as_slice() + .iter() + .map(Primitive::visible_bounds) + .map(|bounds| bounds * *transformation) + .filter_map(|bounds| bounds.intersection(group_bounds)) + .collect() } Item::Cached(_, bounds, _) => { vec![*bounds] -- cgit