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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use super::Renderer;
use crate::graphics_renderer::GraphicRenderInfo;
use anyhow::Result;
/// A single prettifier graphic entry passed to [`Renderer::update_prettifier_graphics`]:
/// `(texture_id, rgba_data, pixel_width, pixel_height, screen_row, col)`.
pub type PrettifierGraphicRef<'a> = (u64, &'a [u8], u32, u32, isize, usize);
impl Renderer {
/// Update graphics textures (Sixel, iTerm2, Kitty)
///
/// # Arguments
/// * `graphics` - Graphics from the terminal with RGBA data
/// * `view_scroll_offset` - Current view scroll offset (0 = viewing current content)
/// * `scrollback_len` - Total lines in scrollback buffer
/// * `visible_rows` - Number of visible rows in terminal
pub fn update_graphics(
&mut self,
graphics: &[par_term_emu_core_rust::graphics::TerminalGraphic],
view_scroll_offset: usize,
scrollback_len: usize,
visible_rows: usize,
) -> Result<()> {
// Track whether we had graphics before this update (to detect removal)
let had_graphics = !self.sixel_graphics.is_empty();
// Clear old graphics list
self.sixel_graphics.clear();
// Calculate the view window in absolute terms
// total_lines = scrollback_len + visible_rows
// When scroll_offset = 0, we view lines [scrollback_len, scrollback_len + visible_rows)
// When scroll_offset > 0, we view earlier lines
let total_lines = scrollback_len + visible_rows;
let view_end = total_lines.saturating_sub(view_scroll_offset);
let view_start = view_end.saturating_sub(visible_rows);
// Process each graphic
for graphic in graphics {
// Use the unique ID from the graphic (stable across position changes)
let id = graphic.id;
let (col, row) = graphic.position;
// Convert scroll_offset_rows from the core library's cell units (graphic.cell_dimensions.1
// pixels per row, defaulting to 2) into display cell rows (self.cell_renderer.cell_height()
// pixels per row).
let core_cell_height = graphic
.cell_dimensions
.map(|(_, h)| h as f32)
.unwrap_or(2.0)
.max(1.0);
let display_cell_height = self.cell_renderer.cell_height().max(1.0);
let scroll_offset_in_display_rows = (graphic.scroll_offset_rows as f32
* core_cell_height
/ display_cell_height)
.round() as usize;
// Calculate screen row based on whether this is a scrollback graphic or current
let screen_row: isize = if let Some(sb_row) = graphic.scrollback_row {
// Scrollback graphic: sb_row is absolute index in scrollback
// Screen row = sb_row - view_start
sb_row as isize - view_start as isize
} else {
// Current graphic: position is relative to visible area
// Absolute position = scrollback_len + row - scroll_offset_in_display_rows
// This keeps the graphic at its original absolute position as scrollback grows
let absolute_row =
scrollback_len.saturating_sub(scroll_offset_in_display_rows) + row;
log::trace!(
"[RENDERER] CALC: scrollback_len={}, row={}, scroll_offset_rows={}, scroll_in_display_rows={}, absolute_row={}, view_start={}, screen_row={}",
scrollback_len,
row,
graphic.scroll_offset_rows,
scroll_offset_in_display_rows,
absolute_row,
view_start,
absolute_row as isize - view_start as isize
);
absolute_row as isize - view_start as isize
};
log::debug!(
"[RENDERER] Graphics update: id={}, protocol={:?}, pos=({},{}), screen_row={}, scrollback_row={:?}, scroll_offset_rows={}, size={}x{}, view=[{},{})",
id,
graphic.protocol,
col,
row,
screen_row,
graphic.scrollback_row,
graphic.scroll_offset_rows,
graphic.width,
graphic.height,
view_start,
view_end
);
// Create or update texture in cache
self.graphics_renderer.get_or_create_texture(
self.cell_renderer.device(),
self.cell_renderer.queue(),
id,
&graphic.pixels, // RGBA pixel data (Arc<Vec<u8>>)
graphic.width as u32,
graphic.height as u32,
)?;
// Add to render list with position and dimensions
// Calculate size in cells (rounding up to cover all affected cells)
let width_cells =
((graphic.width as f32 / self.cell_renderer.cell_width()).ceil() as usize).max(1);
let height_cells =
((graphic.height as f32 / self.cell_renderer.cell_height()).ceil() as usize).max(1);
// Calculate effective clip rows based on screen position
// If screen_row < 0, we need to clip that many rows from the top
// If screen_row >= 0, no clipping needed (we can see the full graphic)
let effective_clip_rows = if screen_row < 0 {
(-screen_row) as usize
} else {
0
};
self.sixel_graphics.push(GraphicRenderInfo {
id,
screen_row,
col,
width_cells,
height_cells,
alpha: 1.0,
scroll_offset_rows: effective_clip_rows,
});
}
// Mark dirty when graphics change (added or removed)
if !graphics.is_empty() || had_graphics {
self.dirty = true;
}
Ok(())
}
/// Compute positioned graphics list for a single pane without touching `self.sixel_graphics`.
///
/// Shares the same texture cache as the global path so textures are never duplicated.
///
/// Returns a `Vec` of [`GraphicRenderInfo`] ready to pass to
/// [`GraphicsRenderer::render_for_pane`].
pub fn update_pane_graphics(
&mut self,
graphics: &[par_term_emu_core_rust::graphics::TerminalGraphic],
view_scroll_offset: usize,
scrollback_len: usize,
visible_rows: usize,
) -> Result<Vec<GraphicRenderInfo>> {
let total_lines = scrollback_len + visible_rows;
let view_end = total_lines.saturating_sub(view_scroll_offset);
let view_start = view_end.saturating_sub(visible_rows);
log::debug!(
"[PANE_GRAPHICS] update_pane_graphics: scrollback_len={}, visible_rows={}, view_scroll_offset={}, total_lines={}, view_start={}, view_end={}, graphics_count={}",
scrollback_len,
visible_rows,
view_scroll_offset,
total_lines,
view_start,
view_end,
graphics.len()
);
let mut positioned = Vec::new();
for graphic in graphics {
let id = graphic.id;
let (col, row) = graphic.position;
// Convert scroll_offset_rows from the core library's cell units (graphic.cell_dimensions.1
// pixels per row, defaulting to 2) into display cell rows (self.cell_renderer.cell_height()
// pixels per row). Without this conversion, the absolute-row formula is wrong whenever
// the graphic was created before set_cell_dimensions() was called on the pane terminal.
let core_cell_height = graphic
.cell_dimensions
.map(|(_, h)| h as f32)
.unwrap_or(2.0)
.max(1.0);
let display_cell_height = self.cell_renderer.cell_height().max(1.0);
let scroll_offset_in_display_rows = (graphic.scroll_offset_rows as f32
* core_cell_height
/ display_cell_height)
.round() as usize;
let screen_row: isize = if let Some(sb_row) = graphic.scrollback_row {
let sr = sb_row as isize - view_start as isize;
log::debug!(
"[PANE_GRAPHICS] scrollback graphic id={}: sb_row={}, view_start={}, screen_row={}",
id,
sb_row,
view_start,
sr
);
sr
} else {
let absolute_row =
scrollback_len.saturating_sub(scroll_offset_in_display_rows) + row;
let sr = absolute_row as isize - view_start as isize;
log::debug!(
"[PANE_GRAPHICS] current graphic id={}: scrollback_len={}, scroll_offset_rows={}, core_cell_h={}, disp_cell_h={}, scroll_in_display_rows={}, row={}, absolute_row={}, view_start={}, screen_row={}",
id,
scrollback_len,
graphic.scroll_offset_rows,
core_cell_height,
display_cell_height,
scroll_offset_in_display_rows,
row,
absolute_row,
view_start,
sr
);
sr
};
// Upload / refresh texture in the shared cache
self.graphics_renderer.get_or_create_texture(
self.cell_renderer.device(),
self.cell_renderer.queue(),
id,
&graphic.pixels,
graphic.width as u32,
graphic.height as u32,
)?;
let width_cells =
((graphic.width as f32 / self.cell_renderer.cell_width()).ceil() as usize).max(1);
let height_cells =
((graphic.height as f32 / self.cell_renderer.cell_height()).ceil() as usize).max(1);
let effective_clip_rows = if screen_row < 0 {
(-screen_row) as usize
} else {
0
};
positioned.push(GraphicRenderInfo {
id,
screen_row,
col,
width_cells,
height_cells,
alpha: 1.0,
scroll_offset_rows: effective_clip_rows,
});
}
Ok(positioned)
}
/// Render inline graphics (Sixel/iTerm2/Kitty) for a single split pane.
///
/// Uses the same `surface_view` as the cell render pass (with `LoadOp::Load`) so
/// graphics are composited on top of already-rendered cells. A scissor rect derived
/// from `viewport` clips output to the pane's bounds.
pub(crate) fn render_pane_sixel_graphics(
&mut self,
surface_view: &wgpu::TextureView,
viewport: &crate::cell_renderer::PaneViewport,
graphics: &[par_term_emu_core_rust::graphics::TerminalGraphic],
scroll_offset: usize,
scrollback_len: usize,
visible_rows: usize,
) -> Result<()> {
let positioned =
self.update_pane_graphics(graphics, scroll_offset, scrollback_len, visible_rows)?;
if positioned.is_empty() {
return Ok(());
}
let mut encoder =
self.cell_renderer
.device()
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("pane sixel encoder"),
});
{
let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some("pane sixel render pass"),
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: surface_view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
},
depth_slice: None,
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});
// Clip to pane bounds
let (sx, sy, sw, sh) = viewport.to_scissor_rect();
render_pass.set_scissor_rect(sx, sy, sw, sh);
let (ox, oy) = viewport.content_origin();
log::debug!(
"[PANE_GRAPHICS] render_pane_sixel_graphics: scissor=({},{},{},{}), origin=({},{}), window={}x{}, positioned_count={}",
sx,
sy,
sw,
sh,
ox,
oy,
self.size.width,
self.size.height,
positioned.len()
);
for g in &positioned {
log::debug!(
"[PANE_GRAPHICS] positioned: id={}, screen_row={}, col={}, width_cells={}, height_cells={}, clip_rows={}",
g.id,
g.screen_row,
g.col,
g.width_cells,
g.height_cells,
g.scroll_offset_rows
);
}
self.graphics_renderer.render_for_pane(
self.cell_renderer.device(),
self.cell_renderer.queue(),
&mut render_pass,
&positioned,
crate::graphics_renderer::PaneRenderGeometry {
window_width: self.size.width as f32,
window_height: self.size.height as f32,
pane_origin_x: ox,
pane_origin_y: oy,
},
)?;
}
self.cell_renderer
.queue()
.submit(std::iter::once(encoder.finish()));
Ok(())
}
/// Upload prettifier diagram graphics and append them to the sixel render list.
///
/// Each graphic is specified by:
/// - `id`: Unique texture ID (should not collide with terminal graphic IDs)
/// - `rgba_data`: Pre-decoded RGBA pixel data
/// - `pixel_width`, `pixel_height`: Image dimensions in pixels
/// - `screen_row`: Row on screen where the graphic starts (0-based)
/// - `col`: Column on screen where the graphic starts
///
/// Graphics are composited on top of terminal cells in the same pass as
/// sixel/iTerm2/Kitty graphics.
pub fn update_prettifier_graphics(
&mut self,
graphics: &[PrettifierGraphicRef<'_>],
) -> Result<()> {
for &(id, rgba_data, pixel_width, pixel_height, screen_row, col) in graphics {
if rgba_data.is_empty() || pixel_width == 0 || pixel_height == 0 {
continue;
}
// Upload / refresh texture in the shared cache
self.graphics_renderer.get_or_create_texture(
self.cell_renderer.device(),
self.cell_renderer.queue(),
id,
rgba_data,
pixel_width,
pixel_height,
)?;
// Calculate size in cells
let width_cells =
((pixel_width as f32 / self.cell_renderer.cell_width()).ceil() as usize).max(1);
let height_cells =
((pixel_height as f32 / self.cell_renderer.cell_height()).ceil() as usize).max(1);
// Clip rows if graphic extends above the viewport
let effective_clip_rows = if screen_row < 0 {
(-screen_row) as usize
} else {
0
};
self.sixel_graphics.push(GraphicRenderInfo {
id,
screen_row,
col,
width_cells,
height_cells,
alpha: 1.0,
scroll_offset_rows: effective_clip_rows,
});
}
if !graphics.is_empty() {
self.dirty = true;
}
Ok(())
}
/// Clear all cached sixel textures
pub fn clear_sixel_cache(&mut self) {
self.graphics_renderer.clear_cache();
self.sixel_graphics.clear();
self.dirty = true;
}
/// Get the number of cached sixel textures
pub fn sixel_cache_size(&self) -> usize {
self.graphics_renderer.cache_size()
}
/// Remove a specific sixel texture from cache
pub fn remove_sixel_texture(&mut self, id: u64) {
self.graphics_renderer.remove_texture(id);
self.sixel_graphics.retain(|g| g.id != id);
self.dirty = true;
}
}