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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! Front → back buffer sync with per-cell equality filtering.
//!
//! Screens write freely into their `front_buf`; `fill_rect` and bulk
//! ops mark whole rows as touched even when the resulting content
//! matches the previous frame. [`Renderer::sync_front`] copies those
//! touched spans into [`Renderer::back_buf`] using
//! [`RenderBuffer::set_cell`], which only flips `back_buf.touched`
//! when the destination cell actually differs. The diff pass then
//! visits only rows that genuinely changed on the wire.
//!
//! Only the cells inside `front_buf.touched(y)` spans are read — no
//! full-buffer scan.
use crate::renderer::Renderer;
use crate::renderer::buffer::RenderBuffer;
use crate::buffer::Surface;
impl Renderer {
/// Sync a caller-facing buffer into the renderer staging buffer.
///
/// # Parameters
///
/// - `front`: desired cell grid whose touched spans identify writes
/// since the last sync.
///
/// # Returns
///
/// `true` when the renderer has real work to render: either the
/// staging buffer now has touched rows that differ from its previous
/// contents, or a force-clear is pending. Returns `false` for pure
/// no-op writes.
///
/// # Behavior
///
/// Copies only cells within `front`'s touched spans, skips
/// continuation cells, steps by wide-cell width, and clears
/// `front`'s touched flags before returning.
pub(crate) fn sync_front(&mut self, front: &mut RenderBuffer) -> bool {
let width = front.width();
let height = front.height();
if self.back_buf.width() != width || self.back_buf.height() != height {
self.back_buf.resize(width, height);
// `resize` touches every row as a bookkeeping side effect.
// We use `touched` to mean "differs from the wire", so
// reset it; cell-by-cell copies below will re-touch only
// rows that actually changed. The caller is expected to
// have set `force_clear` (e.g. via `request_clear` from
// [`crate::screen::Screen::resize`]) to drive a full
// repaint when dimensions change.
self.back_buf.clear_touched();
}
for y in 0..height {
let Some(span) = front.touched(y) else {
continue;
};
let mut x = span.first;
while x <= span.last {
let pos = (x, y).into();
let Some(new_cell) = front.cell(pos) else {
x += 1;
continue;
};
// Skip continuation columns. Copying the primary places
// the continuation on the far side as part of the same
// write, so the column is already carrying the value this
// loop would ask for. `Buffer::set` ignores a continuation
// offered on its own, so passing one on would be a wasted
// call rather than a wrong one, and stepping over it keeps
// the walk in step with the widths it advances by.
if new_cell.is_continuation() {
x += 1;
continue;
}
// `RenderBuffer::set_cell` does a value-equality
// check before writing, so unchanged cells pay no
// clone and no touch.
self.back_buf.set_cell(pos, new_cell);
// Step over any continuation columns owned by the cell
// we just wrote so we don't try to re-copy them.
let step = (new_cell.width() as u16).max(1);
x = x.saturating_add(step);
}
}
front.clear_touched();
self.force_clear || self.back_buf.has_changes()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cell::Cell;
use crate::renderer::buffer::RenderBuffer;
fn cell(c: &str) -> Cell {
Cell::narrow(c)
}
fn fill_row(buf: &mut RenderBuffer, y: u16, content: &str) {
for (x, ch) in content.chars().enumerate() {
buf.set_cell((x as u16, y), &cell(&ch.to_string()));
}
}
#[test]
fn sync_lazy_resizes_back_buf() {
let mut r = Renderer::new();
let mut front = RenderBuffer::new(10, 3);
r.sync_front(&mut front);
assert_eq!(r.back_buf.width(), 10);
assert_eq!(r.back_buf.height(), 3);
}
#[test]
fn sync_clears_front_touched() {
let mut r = Renderer::new();
let mut front = RenderBuffer::new(10, 3);
fill_row(&mut front, 0, "Hello");
r.sync_front(&mut front);
assert!(!front.has_changes());
}
#[test]
fn sync_propagates_real_changes_to_back_buf() {
let mut r = Renderer::new();
let mut front = RenderBuffer::new(10, 3);
fill_row(&mut front, 1, "World");
let need = r.sync_front(&mut front);
assert!(need);
assert!(r.back_buf.touched(1).is_some());
assert!(r.back_buf.touched(0).is_none());
assert_eq!(r.back_buf.cell((0, 1).into()).unwrap().content(), "W");
}
#[test]
fn sync_skips_touched_rows_with_identical_content() {
let mut r = Renderer::new();
let mut front = RenderBuffer::new(10, 3);
fill_row(&mut front, 0, "Hello");
fill_row(&mut front, 1, "World");
// First sync: back_buf gets the content + touched.
assert!(r.sync_front(&mut front));
r.back_buf.clear_touched();
// Second pass: touch every row but write identical content.
front.touch_all();
fill_row(&mut front, 0, "Hello");
fill_row(&mut front, 1, "World");
let need = r.sync_front(&mut front);
assert!(!need, "identical content must leave back_buf untouched");
assert!(!r.back_buf.has_changes());
}
#[test]
fn sync_only_touches_rows_that_actually_changed() {
let mut r = Renderer::new();
let mut front = RenderBuffer::new(10, 3);
fill_row(&mut front, 0, "Hello");
fill_row(&mut front, 1, "World");
fill_row(&mut front, 2, "!!!!");
r.sync_front(&mut front);
r.back_buf.clear_touched();
// Touch all rows but only row 1 genuinely changes.
front.touch_all();
fill_row(&mut front, 0, "Hello");
fill_row(&mut front, 1, "Xorld");
fill_row(&mut front, 2, "!!!!");
assert!(r.sync_front(&mut front));
assert!(r.back_buf.touched(0).is_none());
assert!(r.back_buf.touched(1).is_some());
assert!(r.back_buf.touched(2).is_none());
}
#[test]
fn sync_only_walks_touched_spans() {
// Writing a single cell must not propagate other rows or
// columns to back_buf — verified by leaving back_buf in a
// distinct state outside the span and confirming it survives.
let mut r = Renderer::new();
let mut front = RenderBuffer::new(10, 3);
// Pre-seed back_buf row 2 with content via a full sync.
fill_row(&mut front, 2, "preset");
r.sync_front(&mut front);
r.back_buf.clear_touched();
// Now write one cell on row 0; row 2 of front is blank-but-
// untouched, so sync must not visit row 2.
front.set_cell((3, 0), &cell("X"));
r.sync_front(&mut front);
assert!(r.back_buf.touched(0).is_some());
assert!(r.back_buf.touched(2).is_none());
assert_eq!(
r.back_buf.cell((0, 2).into()).unwrap().content(),
"p",
"row 2 in back_buf must keep its pre-existing content"
);
}
#[test]
fn sync_reports_force_clear_even_with_no_changes() {
let mut r = Renderer::new();
r.force_clear = true;
let mut front = RenderBuffer::new(10, 3);
assert!(r.sync_front(&mut front));
}
/// Regression: sync_front used to step by 1 column even on wide
/// cells, which re-visited the continuation column and overwrote
/// the wide primary in back_buf with a blank. With the width-
/// aware step the primary survives and the continuation is
/// skipped.
#[test]
fn sync_advances_by_wide_cell_width_and_skips_continuation() {
let mut r = Renderer::new();
let mut front = RenderBuffer::new(10, 1);
// Wide CJK cell at col 0; col 1 is its continuation.
let wide = Cell::wide("漢");
front.set_cell((0, 0), &wide);
front.set_cell((3, 0), &cell("a"));
r.sync_front(&mut front);
assert_eq!(r.back_buf.cell((0, 0).into()).unwrap().content(), "漢");
assert_eq!(r.back_buf.cell((0, 0).into()).unwrap().width(), 2);
assert!(
r.back_buf.cell((1, 0).into()).unwrap().is_continuation(),
"col 1 must remain the wide cell's continuation, not be blanked"
);
assert_eq!(r.back_buf.cell((3, 0).into()).unwrap().content(), "a");
}
}