1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! Dirty region computation: diffs two framebuffers to find changed areas for incremental rendering.
//!
//! This module provides [`DirtyDiff`] and [`DirtyRegion`] for computing minimal
//! repaint areas between frames, enabling efficient terminal output.
use crate::framebuffer::FrameBuffer;
/// A rectangular region of the screen that needs repainting.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DirtyRegion {
/// Left edge (column).
pub x: u16,
/// Top edge (row).
pub y: u16,
/// Width in cells.
pub width: u16,
/// Height in cells.
pub height: u16,
}
impl DirtyRegion {
/// Creates a new dirty region at `(x, y)` with the given dimensions.
pub fn new(x: u16, y: u16, width: u16, height: u16) -> Self {
Self { x, y, width, height }
}
/// Returns the right edge (exclusive).
pub fn right(&self) -> u16 {
self.x.saturating_add(self.width)
}
/// Returns the bottom edge (exclusive).
pub fn bottom(&self) -> u16 {
self.y.saturating_add(self.height)
}
/// Returns the area in cells.
pub fn area(&self) -> u32 {
(self.width as u32) * (self.height as u32)
}
/// Returns `true` if `(x, y)` is inside this region.
pub fn contains(&self, x: u16, y: u16) -> bool {
x >= self.x && x < self.right() && y >= self.y && y < self.bottom()
}
/// Returns `true` if this region overlaps `other`.
pub fn intersects(&self, other: &DirtyRegion) -> bool {
self.x < other.right() && self.right() > other.x && self.y < other.bottom() && self.bottom() > other.y
}
/// Returns the smallest region that contains both `self` and `other`.
pub fn merge(&self, other: &DirtyRegion) -> DirtyRegion {
let x = self.x.min(other.x);
let y = self.y.min(other.y);
let right = self.right().max(other.right());
let bottom = self.bottom().max(other.bottom());
DirtyRegion::new(x, y, right - x, bottom - y)
}
/// Returns `true` if `self` and `other` can be merged horizontally
/// (same row span, adjacent columns).
pub fn can_merge_horizontal(&self, other: &DirtyRegion) -> bool {
self.y == other.y && self.height == other.height && (self.right() == other.x || other.right() == self.x)
}
/// Returns `true` if `self` and `other` can be merged vertically
/// (same column span, adjacent rows).
pub fn can_merge_vertical(&self, other: &DirtyRegion) -> bool {
self.x == other.x && self.width == other.width && (self.bottom() == other.y || other.bottom() == self.y)
}
}
/// Computes dirty regions between two framebuffers for incremental rendering.
///
/// Maintains a generation counter for caching: calling [`compute`](Self::compute)
/// with the same generation returns cached results without recomputation.
#[derive(Debug)]
pub struct DirtyDiff {
regions: Vec<DirtyRegion>,
last_generation: u64,
}
impl Default for DirtyDiff {
fn default() -> Self {
Self::new()
}
}
impl DirtyDiff {
/// Creates a new DirtyDiff with no regions.
pub fn new() -> Self {
Self { regions: Vec::new(), last_generation: 0 }
}
/// Computes dirty regions by diffing `current` against `previous`.
///
/// Returns a slice of [`DirtyRegion`]s representing the changed areas.
/// Results are cached per generation: repeated calls with the same generation
/// return the cached result.
pub fn compute(&mut self, current: &FrameBuffer, previous: &FrameBuffer, generation: u64) -> &[DirtyRegion] {
if generation == self.last_generation && !self.regions.is_empty() {
return &self.regions;
}
self.last_generation = generation;
self.regions.clear();
let w = current.width().min(previous.width());
let h = current.height().min(previous.height());
Self::compute_dirty_regions(current, previous, w, h, &mut self.regions);
Self::merge_adjacent_regions(&mut self.regions);
&self.regions
}
/// Sets a full repaint region covering the entire screen.
pub fn compute_full_repaint(&mut self, width: u16, height: u16) -> &[DirtyRegion] {
self.regions.clear();
if width > 0 && height > 0 {
self.regions.push(DirtyRegion::new(0, 0, width, height));
}
&self.regions
}
/// Returns the computed dirty regions.
pub fn regions(&self) -> &[DirtyRegion] {
&self.regions
}
/// Returns `true` if there are no dirty regions.
pub fn is_empty(&self) -> bool {
self.regions.is_empty()
}
/// Returns the total area of all dirty regions combined.
pub fn total_area(&self) -> u32 {
self.regions.iter().map(|r| r.area()).sum()
}
fn compute_dirty_regions(
current: &FrameBuffer,
previous: &FrameBuffer,
w: u16,
h: u16,
regions: &mut Vec<DirtyRegion>,
) {
for y in 0..h {
let mut x = 0;
while x < w {
if current.get(x, y) != previous.get(x, y) {
let start_x = x;
while x + 1 < w && current.get(x + 1, y) != previous.get(x + 1, y) {
x += 1;
}
let span_width = x - start_x + 1;
let mut merged = false;
for r in regions.iter_mut().rev() {
if r.y + r.height == y && start_x >= r.x && start_x + span_width <= r.x + r.width {
r.height += 1;
merged = true;
break;
}
}
if !merged {
regions.push(DirtyRegion::new(start_x, y, span_width, 1));
}
}
x += 1;
}
}
}
/// Merges adjacent regions horizontally or vertically.
#[doc(hidden)]
pub fn merge_adjacent_regions(regions: &mut Vec<DirtyRegion>) {
if regions.len() < 2 {
return;
}
let mut changed = true;
while changed {
changed = false;
let mut j = 0;
while j + 1 < regions.len() {
let (left, right) = (regions[j], regions[j + 1]);
if left.can_merge_horizontal(&right) || left.can_merge_vertical(&right) {
let merged = left.merge(&right);
regions[j] = merged;
regions.swap_remove(j + 1);
changed = true;
} else {
j += 1;
}
}
}
}
}