crabshell-render 0.1.0

Draws Crabshell windows into pixmaps: shapes, text, icons and damage tracking
Documentation
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
//! Repainting only what changed gives the same pixels as drawing the whole
//! window, in a buffer that kept up and in buffers that skipped frames.

use crabshell_reactive::Signal;
use crabshell_ui::{Align, Color, Icon, Row, Stack, Style, Text, Window};

use super::{Buffer, Scene, fixture};
use crate::{BufferAge, Canvas, DamageHistory, PixelFormat, PixelRect};

/// What the changing window's bindings read.
#[derive(Clone, Copy)]
struct Knobs {
  red: Signal<bool>,
  label: Signal<String>,
  big: Signal<bool>,
  faded: Signal<bool>,
  border: Signal<f32>,
}

/// A window whose every kind of property can change: a paint change, text
/// that moves its neighbors, an icon's size, opacity and a border. Its
/// background is translucent, so cleared areas show if they are wrong.
fn changing_window(scene: &mut Scene) -> (Window, Knobs) {
  let knobs = Knobs {
    red: scene.cx.signal(false),
    label: scene.cx.signal(String::from("12:34")),
    big: scene.cx.signal(false),
    faded: scene.cx.signal(false),
    border: scene.cx.signal(1.0),
  };
  let Knobs {
    red,
    label,
    big,
    faded,
    border,
  } = knobs;
  let panel = scene
    .panel()
    .style(
      Style::new()
        .background(Color::rgba(30, 30, 46, 200))
        .color(Color::hex("#cdd6f4")),
    )
    .child(
      Row::new()
        .padding(4, 6)
        .gap(6)
        .align(Align::Center)
        .child(Row::new().style(move || {
          let color = if red.get() { "#f38ba8" } else { "#a6e3a1" };
          Style::new()
            .background(Color::hex(color))
            .radius(4.0)
            .padding(6, 8)
        }))
        .child(Text::new(move || label.get()))
        .child(
          Icon::new(fixture("shapes.svg").as_path()).size(move || if big.get() { 24 } else { 16 }),
        )
        .child(
          Stack::new()
            .style(move || Style::new().opacity(if faded.get() { 0.4 } else { 1.0 }))
            .child(
              Row::new().style(
                Style::new()
                  .background(Color::hex("#89b4fa"))
                  .padding(6, 10),
              ),
            )
            .child(Text::new("ab")),
        )
        .child(Row::new().style(move || {
          Style::new()
            .border(border.get(), Color::hex("#fab387"))
            .radius(3.0)
            .padding(6, 6)
        }))
        .child(Text::new("right side")),
    );
  (scene.window(panel, 260.0, 32.0), knobs)
}

/// Each step changes something.
fn steps(knobs: Knobs) -> Vec<Box<dyn Fn()>> {
  vec![
    Box::new(move || knobs.red.set(true)),
    Box::new(move || knobs.label.set(String::from("12:345678"))),
    Box::new(move || knobs.big.set(true)),
    Box::new(move || knobs.faded.set(true)),
    Box::new(move || knobs.border.set(3.0)),
    Box::new(move || knobs.label.set(String::from("9"))),
    Box::new(move || {
      knobs.big.set(false);
      knobs.red.set(false);
    }),
    Box::new(move || knobs.faded.set(false)),
  ]
}

#[test]
fn repainting_the_damage_matches_drawing_everything() {
  for scale in [1.0, 1.5, 2.0] {
    let mut scene = Scene::new();
    let (window, knobs) = changing_window(&mut scene);
    let mut history = DamageHistory::new();
    let mut buffer = Buffer::new(&window, scale);
    scene.draw(&window, &mut history, &mut buffer);
    for (number, step) in steps(knobs).iter().enumerate() {
      step();
      scene.frame(&window);
      let report = scene.draw(&window, &mut history, &mut buffer);
      assert!(
        !report.is_empty(),
        "step {number} at {scale} reported nothing"
      );
      let full = scene.full(&window, scale);
      assert!(
        buffer.pixels == full.pixels,
        "step {number} at scale {scale}: the repaint differs from a full draw"
      );
    }
  }
}

#[test]
fn buffers_that_skip_frames_catch_up() {
  for scale in [1.0, 1.5] {
    let mut scene = Scene::new();
    let (window, knobs) = changing_window(&mut scene);
    let mut history = DamageHistory::new();
    let mut buffers = vec![Buffer::new(&window, scale); 3];
    // Buffer 0 draws the first frame; then the order skips around, so
    // buffers fall one, two or more frames behind.
    scene.draw(&window, &mut history, &mut buffers[0]);
    let order = [1, 2, 0, 0, 1, 2, 2, 0];
    for ((number, step), &index) in steps(knobs).iter().enumerate().zip(&order) {
      step();
      scene.frame(&window);
      scene.draw(&window, &mut history, &mut buffers[index]);
      let full = scene.full(&window, scale);
      assert!(
        buffers[index].pixels == full.pixels,
        "step {number} into buffer {index} at scale {scale} differs from a full draw"
      );
    }
  }
}

#[test]
fn the_report_is_what_changed_since_the_last_frame() {
  let mut scene = Scene::new();
  let (window, knobs) = changing_window(&mut scene);
  let mut history = DamageHistory::new();
  let mut buffer = Buffer::new(&window, 1.0);
  let first = scene.draw(&window, &mut history, &mut buffer);
  assert_eq!(first, vec![PixelRect::new(0, 0, 260, 32)]);

  knobs.red.set(true);
  scene.frame(&window);
  let report = scene.draw(&window, &mut history, &mut buffer);
  // The 16 by 12 box at 6, 10, plus a pixel around it.
  assert_eq!(report, vec![PixelRect::new(5, 9, 18, 14)]);

  // Nothing changed: nothing to repaint or report.
  assert!(scene.draw(&window, &mut history, &mut buffer).is_empty());
}

#[test]
fn argb_buffers_get_red_and_blue_swapped() {
  let mut scene = Scene::new();
  let (window, _) = changing_window(&mut scene);
  let rgba = scene.full(&window, 1.5);
  let mut bgra = Buffer::new(&window, 1.5);
  scene
    .renderer
    .draw(
      &window.view(),
      &[],
      &mut scene.text,
      &mut DamageHistory::new(),
      Canvas {
        pixels: &mut bgra.pixels,
        width: bgra.width,
        height: bgra.height,
        scale: 1.5,
        format: PixelFormat::Argb8888,
        age: &mut bgra.age,
      },
    )
    .expect("the canvas fits");
  let swapped: Vec<u8> = bgra
    .pixels
    .as_chunks::<4>()
    .0
    .iter()
    .flat_map(|&[b, g, r, a]| [r, g, b, a])
    .collect();
  assert!(swapped == rgba.pixels);
  assert!(bgra.pixels != rgba.pixels, "the scene has colors to swap");
}

#[test]
fn text_taller_than_its_box_is_drawn_whole() {
  let mut scene = Scene::new();
  // 9 pixels of padding in a 30-pixel window leave 12 for a 16-pixel line.
  let panel = scene
    .panel()
    .style(
      Style::new()
        .background(Color::rgb(0, 0, 0))
        .color(Color::rgb(255, 255, 255)),
    )
    .child(Row::new().padding(9, 4).child(Text::new("Hgjy")));
  let window = scene.window(panel, 100.0, 30.0);
  let buffer = scene.full(&window, 1.0);
  let ink = |rows: std::ops::Range<u32>| {
    rows
      .flat_map(|y| (0..100).map(move |x| (x, y)))
      .any(|(x, y)| buffer.pixel(x, y)[0] > 64)
  };
  // The descenders reach below the box, which ends at row 21.
  assert!(ink(21..30), "no ink below the text's box");
  assert!(ink(9..21));
}

#[test]
fn a_canvas_of_the_wrong_size_draws_nothing() {
  let mut scene = Scene::new();
  let (window, _) = changing_window(&mut scene);
  let mut pixels = vec![7; 100];
  let mut age = BufferAge::default();
  let mut history = DamageHistory::new();
  let result = scene.renderer.draw(
    &window.view(),
    &[],
    &mut scene.text,
    &mut history,
    Canvas {
      pixels: &mut pixels,
      width: 260,
      height: 32,
      scale: 1.0,
      format: PixelFormat::Abgr8888,
      age: &mut age,
    },
  );
  let error = result.expect_err("a canvas of the wrong size is an error");
  assert!(error.to_string().contains("260x32"), "{error}");
  assert!(pixels.iter().all(|&byte| byte == 7));
  // The failed draw is no frame: the next one, on a good buffer, is the
  // history's first and repaints everything.
  let mut buffer = Buffer::new(&window, 1.0);
  let report = scene.draw(&window, &mut history, &mut buffer);
  assert_eq!(report, vec![PixelRect::new(0, 0, 260, 32)]);
}

/// Pixels that differ by more than 2/255, and their bounding box.
fn stale(drawn: &Buffer, full: &Buffer) -> (usize, Option<(u32, u32, u32, u32)>) {
  let mut count = 0;
  let mut area: Option<(u32, u32, u32, u32)> = None;
  for y in 0..drawn.height {
    for x in 0..drawn.width {
      let (a, b) = (drawn.pixel(x, y), full.pixel(x, y));
      if a.iter().zip(b).any(|(a, b)| a.abs_diff(b) > 2) {
        count += 1;
        area = Some(match area {
          None => (x, y, x, y),
          Some((x0, y0, x1, y1)) => (x0.min(x), y0.min(y), x1.max(x), y1.max(y)),
        });
      }
    }
  }
  (count, area)
}

#[test]
fn an_icon_bigger_than_its_box_leaves_nothing_behind() {
  use crabshell_ui::IconSource;

  for scale in [1.0, 1.25, 1.5, 2.0] {
    let mut scene = Scene::new();
    let first = scene.cx.signal(true);
    let (svg, png) = (fixture("shapes.svg"), fixture("disc.png"));
    // 10 pixels of padding in a 36-pixel bar leave 16 for a 24-pixel
    // icon, which overflows its box.
    let panel = scene
      .panel()
      .style(Style::new().background(Color::rgb(0, 0, 0)))
      .child(
        Row::new().padding(10, 4).child(
          Icon::new(move || {
            IconSource::File(if first.get() {
              svg.clone()
            } else {
              png.clone()
            })
          })
          .size(24),
        ),
      );
    let window = scene.window(panel, 60.0, 36.0);
    let mut history = DamageHistory::new();
    let mut buffer = Buffer::new(&window, scale);
    scene.draw(&window, &mut history, &mut buffer);
    first.set(false);
    scene.frame(&window);
    scene.draw(&window, &mut history, &mut buffer);
    let (count, area) = stale(&buffer, &scene.full(&window, scale));
    assert_eq!(count, 0, "at {scale}: stale pixels in {area:?}");
  }
}

#[test]
fn glyph_ink_outside_the_line_leaves_nothing_behind() {
  for (from, to) in [("\u{1ED6}", "O"), ("\u{1EB6}", "A"), ("\u{303}x", "x")] {
    for scale in [1.0, 1.25, 1.5, 1.75, 2.0] {
      let mut scene = Scene::new();
      let label = scene.cx.signal(String::from(from));
      let panel = scene
        .panel()
        .style(
          Style::new()
            .background(Color::rgb(0, 0, 0))
            .color(Color::rgb(255, 255, 255)),
        )
        .child(
          Row::new()
            .padding(8, 8)
            .align(Align::Center)
            .child(Text::new(move || label.get())),
        );
      let window = scene.window(panel, 60.0, 40.0);
      let mut history = DamageHistory::new();
      let mut buffer = Buffer::new(&window, scale);
      scene.draw(&window, &mut history, &mut buffer);
      label.set(String::from(to));
      scene.frame(&window);
      scene.draw(&window, &mut history, &mut buffer);
      let (count, area) = stale(&buffer, &scene.full(&window, scale));
      assert_eq!(
        count, 0,
        "{from:?} to {to:?} at {scale}: stale pixels in {area:?}"
      );
    }
  }
}

/// A small deterministic random number generator.
struct Random(u64);

impl Random {
  fn below(&mut self, n: u64) -> u64 {
    self.0 = self
      .0
      .wrapping_mul(6_364_136_223_846_793_005)
      .wrapping_add(1_442_695_040_888_963_407);
    (self.0 >> 33) % n
  }
}

#[test]
fn random_changes_and_buffer_orders_never_leave_stale_pixels() {
  let labels = ["12:34", "9", "12:345678", "a much longer label here", ""];
  let scales = [1.0_f32, 1.25, 1.5, 1.75, 2.0];
  for seed in 0..12_u64 {
    for count in [2_usize, 3] {
      let mut random = Random(seed * 7 + count as u64);
      let mut scene = Scene::new();
      let (window, knobs) = changing_window(&mut scene);
      let mut scale = scales[random.below(5) as usize];
      let mut history = DamageHistory::new();
      let mut buffers = vec![Buffer::new(&window, scale); count];
      for step in 0..60 {
        match random.below(8) {
          0 => knobs.red.set(random.below(2) == 0),
          1 => knobs
            .label
            .set(String::from(labels[random.below(5) as usize])),
          2 => knobs.big.set(random.below(2) == 0),
          3 => knobs.faded.set(random.below(2) == 0),
          4 => knobs
            .border
            .set([0.0, 0.5, 1.0, 3.0][random.below(4) as usize]),
          5 if random.below(6) == 0 => {
            scale = scales[random.below(5) as usize];
            buffers = vec![Buffer::new(&window, scale); count];
          }
          6 if random.below(6) == 0 => {
            let width = [200.0, 260.0, 333.0][random.below(3) as usize];
            window.set_size(crabshell_ui::Size::new(width, 32.0));
            scene.frame(&window);
            buffers = vec![Buffer::new(&window, scale); count];
          }
          _ => {}
        }
        scene.frame(&window);
        // Buffers come back in any order; the first one often, so
        // the others fall far behind.
        let index = if random.below(4) == 0 {
          0
        } else {
          random.below(count as u64) as usize
        };
        scene.draw(&window, &mut history, &mut buffers[index]);
        let (stale, area) = stale(&buffers[index], &scene.full(&window, scale));
        assert_eq!(
          stale, 0,
          "seed {seed}, {count} buffers, step {step}, scale {scale}: stale pixels in {area:?}"
        );
      }
    }
  }
}

#[test]
fn argb_repaints_match_a_full_argb_draw() {
  for scale in [1.0_f32, 1.25, 1.75] {
    let mut scene = Scene::new();
    let (window, knobs) = changing_window(&mut scene);
    let draw = |scene: &mut Scene,
                history: &mut DamageHistory,
                buffer: &mut Buffer,
                damage: &[crabshell_ui::Rect]| {
      scene
        .renderer
        .draw(
          &window.view(),
          damage,
          &mut scene.text,
          history,
          Canvas {
            pixels: &mut buffer.pixels,
            width: buffer.width,
            height: buffer.height,
            scale,
            format: PixelFormat::Argb8888,
            age: &mut buffer.age,
          },
        )
        .expect("the canvas fits");
    };
    let mut history = DamageHistory::new();
    let mut buffer = Buffer::new(&window, scale);
    draw(&mut scene, &mut history, &mut buffer, &window.take_damage());
    knobs.red.set(true);
    knobs.border.set(3.0);
    scene.frame(&window);
    draw(&mut scene, &mut history, &mut buffer, &window.take_damage());
    let mut full = Buffer::new(&window, scale);
    draw(&mut scene, &mut DamageHistory::new(), &mut full, &[]);
    assert!(buffer.pixels == full.pixels, "at {scale}");
  }
}

/// Draws `damage` into `buffer`, which belongs to `history`.
fn repaint(
  scene: &mut Scene,
  window: &Window,
  damage: &[crabshell_ui::Rect],
  history: &mut DamageHistory,
  buffer: &mut Buffer,
) {
  scene
    .renderer
    .draw(
      &window.view(),
      damage,
      &mut scene.text,
      history,
      Canvas {
        pixels: &mut buffer.pixels,
        width: buffer.width,
        height: buffer.height,
        scale: buffer.scale,
        format: PixelFormat::Abgr8888,
        age: &mut buffer.age,
      },
    )
    .expect("the test's canvas fits");
}

#[test]
fn a_repaint_whose_edge_cuts_a_rounded_corner_matches_drawing_everything() {
  let mut scene = Scene::new();
  let panel = scene
    .panel()
    .style(Style::new().background(Color::rgb(30, 30, 46)))
    .child(
      Row::new().padding(5, 10).align(Align::Start).child(
        Row::new().style(
          Style::new()
            .background(Color::hex("#89b4fa"))
            .border(1.0, Color::hex("#fab387"))
            .radius(6.0)
            .padding(10, 20),
        ),
      ),
    );
  let window = scene.window(panel, 100.0, 40.0);
  for scale in [1.0, 1.25, 1.5, 1.75, 2.0] {
    let full = scene.full(&window, scale);
    // The box's left corners span x 10 to 16; sweep the damage's right
    // edge through them.
    for right in 11..=16 {
      let mut history = DamageHistory::new();
      let mut buffer = Buffer::new(&window, scale);
      repaint(&mut scene, &window, &[], &mut history, &mut buffer);
      let damage = crabshell_ui::Rect::new(0.0, 0.0, right as f32, 40.0);
      repaint(&mut scene, &window, &[damage], &mut history, &mut buffer);
      assert!(
        buffer.pixels == full.pixels,
        "a repaint to x {right} at scale {scale} differs from a full draw"
      );
    }
  }
}