crabshell_render/
damage.rs1use std::collections::VecDeque;
11use std::sync::atomic::{AtomicU64, Ordering};
12
13use crabshell_ui::Rect;
14
15const HISTORY: usize = 4;
18
19const MARGIN: u32 = 1;
22
23#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
25pub struct PixelRect {
26 pub x: u32,
28 pub y: u32,
30 pub width: u32,
32 pub height: u32,
34}
35
36impl PixelRect {
37 pub const fn new(x: u32, y: u32, width: u32, height: u32) -> PixelRect {
39 PixelRect {
40 x,
41 y,
42 width,
43 height,
44 }
45 }
46
47 pub fn right(&self) -> u32 {
49 self.x + self.width
50 }
51
52 pub fn bottom(&self) -> u32 {
54 self.y + self.height
55 }
56
57 pub fn area(&self) -> u64 {
59 u64::from(self.width) * u64::from(self.height)
60 }
61
62 pub fn overlaps(&self, other: &PixelRect) -> bool {
64 self.x < other.right()
65 && other.x < self.right()
66 && self.y < other.bottom()
67 && other.y < self.bottom()
68 }
69
70 fn union(&self, other: &PixelRect) -> PixelRect {
72 let x = self.x.min(other.x);
73 let y = self.y.min(other.y);
74 PixelRect::new(
75 x,
76 y,
77 self.right().max(other.right()) - x,
78 self.bottom().max(other.bottom()) - y,
79 )
80 }
81}
82
83pub(crate) fn to_pixels(rect: Rect, scale: f32, width: u32, height: u32) -> Option<PixelRect> {
87 if !(rect.width > 0.0 && rect.height > 0.0) {
88 return None;
89 }
90 let left = ((rect.x * scale).floor() as u32).saturating_sub(MARGIN);
92 let top = ((rect.y * scale).floor() as u32).saturating_sub(MARGIN);
93 let right = ((rect.right() * scale).ceil() as u32)
94 .saturating_add(MARGIN)
95 .min(width);
96 let bottom = ((rect.bottom() * scale).ceil() as u32)
97 .saturating_add(MARGIN)
98 .min(height);
99 (left < right && top < bottom).then(|| PixelRect::new(left, top, right - left, bottom - top))
100}
101
102pub(crate) fn merge(mut rects: Vec<PixelRect>) -> Vec<PixelRect> {
106 rects.retain(|rect| rect.area() > 0);
107 let mut merged = true;
108 while merged {
109 merged = false;
110 'outer: for i in 0..rects.len() {
111 for j in i + 1..rects.len() {
112 if rects[i].overlaps(&rects[j]) {
113 let other = rects.swap_remove(j);
114 rects[i] = rects[i].union(&other);
115 merged = true;
116 break 'outer;
117 }
118 }
119 }
120 }
121 rects
122}
123
124#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
127pub struct BufferAge {
128 drawn: Option<(u64, u64)>,
130}
131
132static HISTORIES: AtomicU64 = AtomicU64::new(0);
135
136#[derive(Debug)]
138pub struct DamageHistory {
139 id: u64,
140 next: u64,
142 shape: Option<(u32, u32, u32)>,
145 recent: VecDeque<(u64, Vec<PixelRect>)>,
147}
148
149#[derive(Debug, PartialEq, Eq)]
151pub(crate) struct Plan {
152 pub(crate) repaint: Vec<PixelRect>,
155 pub(crate) report: Vec<PixelRect>,
158}
159
160impl Default for DamageHistory {
161 fn default() -> Self {
162 Self::new()
163 }
164}
165
166impl DamageHistory {
167 pub(crate) fn id(&self) -> u64 {
169 self.id
170 }
171
172 pub fn new() -> DamageHistory {
174 DamageHistory {
175 id: HISTORIES.fetch_add(1, Ordering::Relaxed),
176 next: 0,
177 shape: None,
178 recent: VecDeque::new(),
179 }
180 }
181
182 pub(crate) fn plan(
186 &mut self,
187 damage: &[Rect],
188 scale: f32,
189 width: u32,
190 height: u32,
191 age: &mut BufferAge,
192 ) -> Plan {
193 let full = vec![PixelRect::new(0, 0, width, height)];
194 let frame = self.next;
195 self.next += 1;
196
197 let shape = (width, height, scale.to_bits());
198 let mut changed = if self.shape == Some(shape) {
199 merge(
200 damage
201 .iter()
202 .filter_map(|rect| to_pixels(*rect, scale, width, height))
203 .collect(),
204 )
205 } else {
206 self.shape = Some(shape);
209 self.recent.clear();
210 full.clone()
211 };
212 if more_than_half(&changed, width, height) {
213 changed = full.clone();
214 }
215
216 let missed = match age.drawn {
219 Some((history, drawn))
220 if history == self.id
221 && self
222 .recent
223 .front()
224 .is_none_or(|(first, _)| *first <= drawn + 1) =>
225 {
226 Some(
227 self
228 .recent
229 .iter()
230 .filter(|(number, _)| *number > drawn)
231 .flat_map(|(_, rects)| rects.iter().copied())
232 .collect::<Vec<_>>(),
233 )
234 }
235 _ => None,
236 };
237 let repaint = match missed {
238 Some(missed) => {
239 let repaint = merge(changed.iter().copied().chain(missed).collect());
240 if more_than_half(&repaint, width, height) {
241 full
242 } else {
243 repaint
244 }
245 }
246 None => full,
247 };
248
249 self.recent.push_back((frame, changed.clone()));
250 while self.recent.len() > HISTORY {
251 self.recent.pop_front();
252 }
253 age.drawn = Some((self.id, frame));
254 Plan {
255 repaint,
256 report: changed,
257 }
258 }
259}
260
261fn more_than_half(rects: &[PixelRect], width: u32, height: u32) -> bool {
264 let covered: u64 = rects.iter().map(PixelRect::area).sum();
265 covered * 2 > u64::from(width) * u64::from(height)
266}