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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
//! This module implements the logic to manage graphic items included in a
//! `Grid` instance.
pub mod sixel;
use std::collections::HashSet;
use std::fmt::Write;
use std::sync::{Arc, Weak};
use std::{cmp, mem};
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use parking_lot::Mutex;
use smallvec::SmallVec;
use crate::event::{Event, EventListener};
use crate::grid::Dimensions;
use crate::index::{Column, Line};
use crate::term::{Term, TermMode};
use crate::vte::Params;
use crate::vte::ansi::{Handler, Rgb};
/// Max allowed dimensions (width, height) for the graphic, in pixels.
pub const MAX_GRAPHIC_DIMENSIONS: [usize; 2] = [4096, 4096];
/// Max. number of graphics stored in a single cell.
const MAX_GRAPHICS_PER_CELL: usize = 20;
/// Unique identifier for every graphic added to a grid.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Eq, PartialEq, Clone, Debug, Copy, Hash, PartialOrd, Ord)]
pub struct GraphicId(u64);
/// Reference to a texture stored in the display.
///
/// When all references to a single texture are removed, its identifier is
/// added to the remove queue.
#[derive(Clone, Debug)]
pub struct TextureRef {
/// Graphic identifier.
pub id: GraphicId,
/// Width, in pixels, of the graphic.
pub width: u16,
/// Height, in pixels, of the graphic.
pub height: u16,
/// Height, in pixels, of the cell when the graphic was inserted.
pub cell_height: usize,
/// Queue to track removed references.
pub texture_operations: Weak<Mutex<Vec<TextureOperation>>>,
}
impl PartialEq for TextureRef {
fn eq(&self, t: &Self) -> bool {
// Ignore texture_operations.
self.id == t.id
}
}
impl Eq for TextureRef {}
impl Drop for TextureRef {
fn drop(&mut self) {
if let Some(texture_operations) = self.texture_operations.upgrade() {
texture_operations.lock().push(TextureOperation::Remove(self.id));
}
}
}
/// A list of graphics in a single cell.
pub type GraphicsCell = SmallVec<[GraphicCell; 1]>;
/// Graphic data stored in a single cell.
#[derive(Clone, Debug)]
pub struct GraphicCell {
/// Texture to draw the graphic in this cell.
pub texture: Arc<TextureRef>,
/// Offset in the x direction.
pub offset_x: u16,
/// Offset in the y direction.
pub offset_y: u16,
/// Queue to track empty subregions.
pub texture_operations: Weak<Mutex<Vec<TextureOperation>>>,
}
impl PartialEq for GraphicCell {
fn eq(&self, c: &Self) -> bool {
// Ignore texture_operations.
self.texture == c.texture && self.offset_x == c.offset_x && self.offset_y == c.offset_y
}
}
impl Eq for GraphicCell {}
impl Drop for GraphicCell {
fn drop(&mut self) {
if let Some(texture_operations) = self.texture_operations.upgrade() {
let tex_op = TextureOperation::ClearSubregion(ClearSubregion {
id: self.texture.id,
x: self.offset_x,
y: self.offset_y,
});
texture_operations.lock().push(tex_op);
}
}
}
#[cfg(feature = "serde")]
#[derive(Serialize, Deserialize)]
struct GraphicCellSerde {
texture: GraphicId,
offset_x: u16,
offset_y: u16,
tex_width: u16,
tex_height: u16,
tex_cell_height: usize,
}
#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for GraphicCell {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let data = GraphicCellSerde::deserialize(deserializer)?;
let dummy_queue = Arc::new(Mutex::new(Vec::new()));
let texture = TextureRef {
id: data.texture,
width: data.tex_width,
height: data.tex_height,
cell_height: data.tex_cell_height,
texture_operations: Arc::downgrade(&dummy_queue),
};
let graphic_cell = GraphicCell {
texture: Arc::new(texture),
offset_x: data.offset_x,
offset_y: data.offset_y,
texture_operations: Arc::downgrade(&dummy_queue),
};
Ok(graphic_cell)
}
}
#[cfg(feature = "serde")]
impl Serialize for GraphicCell {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let data = GraphicCellSerde {
texture: self.texture.id,
offset_x: self.offset_x,
offset_y: self.offset_y,
tex_width: self.texture.width,
tex_height: self.texture.height,
tex_cell_height: self.texture.cell_height,
};
data.serialize(serializer)
}
}
impl GraphicCell {
/// Graphic identifier of the texture in this cell.
#[inline]
pub fn graphic_id(&self) -> GraphicId {
self.texture.id
}
}
/// Specifies the format of the pixel data.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Eq, PartialEq, Clone, Debug, Copy)]
pub enum ColorType {
/// 3 bytes per pixel (red, green, blue).
Rgb,
/// 4 bytes per pixel (red, green, blue, alpha).
Rgba,
}
/// Defines a single graphic read from the PTY.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct GraphicData {
/// Graphics identifier.
pub id: GraphicId,
/// Width, in pixels, of the graphic.
pub width: usize,
/// Height, in pixels, of the graphic.
pub height: usize,
/// Color type of the pixels.
pub color_type: ColorType,
/// Pixels data.
pub pixels: Vec<u8>,
/// Indicate if there are no transparent pixels.
pub is_opaque: bool,
}
impl GraphicData {
/// Check if the image may contain transparent pixels. If it returns
/// `false`, it is guaranteed that there are no transparent pixels.
#[inline]
pub fn maybe_transparent(&self) -> bool {
!self.is_opaque && self.color_type == ColorType::Rgba
}
/// Check if all pixels under a region are opaque.
///
/// If the region exceeds the boundaries of the image it is considered as
/// not filled.
pub fn is_filled(&self, x: usize, y: usize, width: usize, height: usize) -> bool {
// If there are pixels outside the picture we assume that the region is
// not filled.
if x + width >= self.width || y + height >= self.height {
return false;
}
// Don't check actual pixels if the image does not contain an alpha
// channel.
if !self.maybe_transparent() {
return true;
}
debug_assert!(self.color_type == ColorType::Rgba);
for offset_y in y..y + height {
let offset = offset_y * self.width * 4;
let row = &self.pixels[offset..offset + width * 4];
if row.chunks_exact(4).any(|pixel| pixel.last() != Some(&255)) {
return false;
}
}
true
}
}
/// Operation to clear a subregion in an existing graphic.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct ClearSubregion {
/// Graphics identifier.
pub id: GraphicId,
/// X coordinate.
pub x: u16,
/// Y coordinate.
pub y: u16,
}
/// Queues to add or to remove the textures in the display.
#[derive(Default)]
pub struct UpdateQueues {
/// Graphics read from the PTY.
pub pending: Vec<GraphicData>,
/// Graphics removed from the grid.
pub remove_queue: Vec<GraphicId>,
/// Subregions in a graphic to be clear.
pub clear_subregions: Vec<ClearSubregion>,
}
/// Operations on the existing textures.
#[derive(Debug)]
pub enum TextureOperation {
/// Remove a texture from the GPU.
Remove(GraphicId),
/// Clear a subregion.
ClearSubregion(ClearSubregion),
}
/// Track changes in the grid to add or to remove graphics.
#[derive(Debug, Default)]
pub struct Graphics {
/// Last generated identifier.
pub last_id: u64,
/// New graphics, received from the PTY.
pub pending: Vec<GraphicData>,
/// Graphics removed from the grid.
pub texture_operations: Arc<Mutex<Vec<TextureOperation>>>,
/// Shared palette for Sixel graphics.
pub sixel_shared_palette: Option<Vec<Rgb>>,
/// Cell height in pixels.
pub cell_height: f32,
/// Cell width in pixels.
pub cell_width: f32,
/// Current Sixel parser.
pub sixel_parser: Option<Box<sixel::Parser>>,
}
impl Graphics {
/// Create a new instance, and initialize it with the dimensions of the
/// window.
pub fn new<S: Dimensions>(size: &S) -> Self {
let mut graphics = Graphics::default();
graphics.resize(size);
graphics
}
/// Generate a new graphic identifier.
pub fn next_id(&mut self) -> GraphicId {
self.last_id += 1;
GraphicId(self.last_id)
}
/// Get queues to update graphics in the grid.
///
/// If all queues are empty, it returns `None`.
pub fn take_queues(&mut self) -> Option<UpdateQueues> {
let texture_operations = {
let mut queue = self.texture_operations.lock();
if queue.is_empty() { Vec::new() } else { mem::take(&mut *queue) }
};
if texture_operations.is_empty() && self.pending.is_empty() {
return None;
}
let mut remove_queue = Vec::new();
let mut clear_subregions = Vec::new();
for operation in texture_operations {
match operation {
TextureOperation::Remove(id) => remove_queue.push(id),
TextureOperation::ClearSubregion(cs) => clear_subregions.push(cs),
}
}
Some(UpdateQueues { pending: mem::take(&mut self.pending), remove_queue, clear_subregions })
}
/// Update cell dimensions.
pub fn resize<S: Dimensions>(&mut self, size: &S) {
self.cell_height = size.cell_height();
self.cell_width = size.cell_width();
}
pub fn graphics_attribute<L: EventListener>(&self, event_proxy: &L, pi: u16, pa: u16) {
// From Xterm documentation:
//
// CSI ? Pi ; Pa ; Pv S
//
// Pi = 1 -> item is number of color registers.
// Pi = 2 -> item is Sixel graphics geometry (in pixels).
// Pi = 3 -> item is ReGIS graphics geometry (in pixels).
//
// Pa = 1 -> read attribute.
// Pa = 2 -> reset to default.
// Pa = 3 -> set to value in Pv.
// Pa = 4 -> r
|