space: Handle multiple overlapping rects when merging damage

This commit is contained in:
Victor Brekenfeld 2022-01-14 18:51:44 +01:00 committed by Victoria Brekenfeld
parent 8c126eeb80
commit a53d64053f
1 changed files with 8 additions and 5 deletions

View File

@ -557,12 +557,15 @@ impl Space {
damage.retain(|rect| rect.overlaps(output_geo));
damage.retain(|rect| rect.size.h > 0 && rect.size.w > 0);
// merge overlapping rectangles
damage = damage.into_iter().fold(Vec::new(), |mut new_damage, rect| {
if let Some(existing) = new_damage.iter_mut().find(|other| rect.overlaps(**other)) {
*existing = existing.merge(rect);
} else {
new_damage.push(rect);
damage = damage.into_iter().fold(Vec::new(), |new_damage, mut rect| {
// replace with drain_filter, when that becomes stable to reuse the original Vec's memory
let (overlapping, mut new_damage): (Vec<_>, Vec<_>) =
new_damage.into_iter().partition(|other| other.overlaps(rect));
for overlap in overlapping {
rect = rect.merge(overlap);
}
new_damage.push(rect);
new_damage
});