darkly 0.5.0

A GPU-native paint engine on wgpu: brushes, layers, blend modes, masks, selections, and undo.
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
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
//! Brush stroke GPU integration tests: stroke workflows, undo/redo.
//!
//! Tests the end-to-end GPU brush flow using raw components (RegionStore,
//! GpuPaintTarget, GpuRegionAction) without a full DarklyEngine.
//! Run with: `cargo test -p darkly --test stroke`

use darkly::coord::CanvasRect;
use darkly::gpu::atlas::CanvasFrame;
use darkly::gpu::diff_rect::DiffRectPass;
use darkly::gpu::paint_target::{GpuPaintTarget, PaintPipelines};
use darkly::gpu::region_store::RegionScratch;
use darkly::gpu::test_utils::*;

fn cr(x: i32, y: i32, w: u32, h: u32) -> CanvasRect {
    CanvasRect::from_xywh(x, y, w, h)
}

/// Build a CanvasFrame for a test texture sized `(w, h)` at canvas origin (0, 0).
fn frame<'a>(tex: &'a wgpu::Texture, w: u32, h: u32) -> CanvasFrame<'a> {
    CanvasFrame {
        texture: tex,
        canvas_extent: cr(0, 0, w, h),
    }
}

fn encoder(device: &wgpu::Device) -> wgpu::CommandEncoder {
    device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
        label: Some("test"),
    })
}

fn submit(queue: &wgpu::Queue, encoder: wgpu::CommandEncoder) {
    queue.submit([encoder.finish()]);
}

fn pixel_at(pixels: &[u8], w: u32, x: u32, y: u32, bpp: u32) -> &[u8] {
    let offset = ((y * w + x) * bpp) as usize;
    &pixels[offset..offset + bpp as usize]
}

// ============================================================================
// End-to-end GPU brush stroke
// ============================================================================

/// Simulate: begin_stroke → paint two circles → end_stroke → undo → redo.
#[test]
fn gpu_stroke_paint_undo_redo() {
    let (device, queue) = test_device();
    let (w, h) = (128, 128);
    let fmt = wgpu::TextureFormat::Rgba8Unorm;

    // Create a transparent layer texture.
    let (tex, view) = create_test_texture(&device, &queue, w, h, &vec![0u8; (w * h * 4) as usize]);
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let mut store = RegionScratch::new(&device, w, h);

    // --- begin_stroke: save full canvas ---
    let mut enc = encoder(&device);
    let snap = store.save_region(&device, &mut enc, &frame(&tex, w, h), fmt, cr(0, 0, w, h));
    submit(&queue, enc);

    // --- stroke_to: two circles ---
    let target = GpuPaintTarget::from_canvas_texture(
        &tex,
        &view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(0, 0, w, h),
    );

    let mut enc = encoder(&device);
    target.composite_circle(
        &mut enc,
        &pipelines,
        &queue,
        50.0,
        50.0,
        5.0,
        [255, 0, 0, 255],
        1.0,
    );
    submit(&queue, enc);

    let mut enc = encoder(&device);
    target.composite_circle(
        &mut enc,
        &pipelines,
        &queue,
        60.0,
        50.0,
        5.0,
        [255, 0, 0, 255],
        1.0,
    );
    submit(&queue, enc);

    // --- end_stroke: commit the stroke rect ---
    // Bounding rect: x=45..65, y=45..55 (approx) — use conservative rect.
    let stroke_rect = cr(43, 43, 24, 14);
    let mut enc = encoder(&device);
    let (entry, _req) = store.commit_region(
        &mut enc,
        &device,
        darkly::layer::LayerId::from_ffi(1),
        &frame(&tex, w, h),
        &snap,
        stroke_rect,
    );
    submit(&queue, enc);

    // Verify paint landed.
    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    let center1 = pixel_at(&pixels, w, 50, 50, 4);
    assert_eq!(
        center1[0], 255,
        "circle 1 center should be red, R={}",
        center1[0]
    );
    assert_eq!(
        center1[3], 255,
        "circle 1 center should be opaque, A={}",
        center1[3]
    );

    let center2 = pixel_at(&pixels, w, 60, 50, 4);
    assert!(
        center2[3] > 0,
        "circle 2 center should be visible, A={}",
        center2[3]
    );

    let painted_snapshot = pixels.clone();

    // --- undo ---
    let mut enc = encoder(&device);
    let (forward_entry, _req) = store.restore_region(&mut enc, &device, &entry, &frame(&tex, w, h));
    submit(&queue, enc);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    let center1 = pixel_at(&pixels, w, 50, 50, 4);
    assert_eq!(
        center1[3], 0,
        "after undo, circle 1 should be gone, A={}",
        center1[3]
    );

    let corner = pixel_at(&pixels, w, 0, 0, 4);
    assert_eq!(corner[3], 0, "corner should still be transparent");

    // --- redo ---
    let mut enc = encoder(&device);
    let (_backward_entry, _req) =
        store.restore_region(&mut enc, &device, &forward_entry, &frame(&tex, w, h));
    submit(&queue, enc);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    // The stroke rect area should be pixel-identical to the painted snapshot.
    let (sx, sy, sw, sh) = (
        stroke_rect.x0() as u32,
        stroke_rect.y0() as u32,
        stroke_rect.width,
        stroke_rect.height,
    );
    for y in sy..sy + sh {
        for x in sx..sx + sw {
            let p = pixel_at(&pixels, w, x, y, 4);
            let expected = pixel_at(&painted_snapshot, w, x, y, 4);
            assert_eq!(
                p, expected,
                "redo mismatch at ({x},{y}): got {:?}, expected {:?}",
                p, expected
            );
        }
    }
}

// ============================================================================
// Stroke rect tracking
// ============================================================================

#[test]
fn stroke_rect_tracking() {
    // Test the bounding rect expansion logic (no GPU needed).
    // Simulated GpuStrokeState expand logic (mirrors engine.rs).
    fn expand_rect(
        rect: Option<[u32; 4]>,
        cx: f32,
        cy: f32,
        radius: f32,
        canvas_w: u32,
        canvas_h: u32,
    ) -> [u32; 4] {
        let pad = 2.0;
        let x0 = (cx - radius - pad).max(0.0) as u32;
        let y0 = (cy - radius - pad).max(0.0) as u32;
        let x1 = ((cx + radius + pad).ceil() as u32).min(canvas_w);
        let y1 = ((cy + radius + pad).ceil() as u32).min(canvas_h);

        match rect {
            None => [x0, y0, x1 - x0, y1 - y0],
            Some([sx, sy, sw, sh]) => {
                let nx = sx.min(x0);
                let ny = sy.min(y0);
                let nx1 = (sx + sw).max(x1);
                let ny1 = (sy + sh).max(y1);
                [nx, ny, nx1 - nx, ny1 - ny]
            }
        }
    }

    let canvas = (256, 256);

    // Single circle at (100, 100, r=10).
    let rect = expand_rect(None, 100.0, 100.0, 10.0, canvas.0, canvas.1);
    assert!(rect[0] <= 88, "x0 should be ≤ 88, got {}", rect[0]);
    assert!(rect[1] <= 88, "y0 should be ≤ 88, got {}", rect[1]);
    assert!(
        rect[0] + rect[2] >= 112,
        "x1 should be ≥ 112, got {}",
        rect[0] + rect[2]
    );
    assert!(
        rect[1] + rect[3] >= 112,
        "y1 should be ≥ 112, got {}",
        rect[1] + rect[3]
    );

    // Second circle at (200, 200, r=10) — rect should expand.
    let rect = expand_rect(Some(rect), 200.0, 200.0, 10.0, canvas.0, canvas.1);
    assert!(rect[0] <= 88, "expanded x0 should still cover first circle");
    assert!(
        rect[0] + rect[2] >= 212,
        "expanded x1 should cover second circle"
    );
    assert!(
        rect[1] + rect[3] >= 212,
        "expanded y1 should cover second circle"
    );

    // Circle near edge (0, 0, r=5) — clamped to canvas bounds.
    let rect = expand_rect(None, 0.0, 0.0, 5.0, canvas.0, canvas.1);
    assert_eq!(rect[0], 0, "clamped x0 should be 0");
    assert_eq!(rect[1], 0, "clamped y0 should be 0");

    // Small stroke rect should be << canvas size.
    let small = expand_rect(None, 128.0, 128.0, 3.0, canvas.0, canvas.1);
    assert!(
        small[2] < 20,
        "small stroke width should be < 20, got {}",
        small[2]
    );
    assert!(
        small[3] < 20,
        "small stroke height should be < 20, got {}",
        small[3]
    );
}

// ============================================================================
// GPU stroke on mask (R8)
// ============================================================================

/// Paint black on a fully-revealed mask → undo → verify mask is restored.
#[test]
fn gpu_stroke_on_mask_undo() {
    let (device, queue) = test_device();
    let (w, h) = (128, 128);
    let fmt = wgpu::TextureFormat::R8Unorm;

    // Fully-revealed mask (255).
    let white = vec![255u8; (w * h) as usize];
    let (tex, view) = create_test_texture_with_format(&device, &queue, w, h, &white, fmt);
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let mut store = RegionScratch::new(&device, w, h);

    // begin_stroke: save full canvas.
    let mut enc = encoder(&device);
    let snap = store.save_region(&device, &mut enc, &frame(&tex, w, h), fmt, cr(0, 0, w, h));
    submit(&queue, enc);

    // stroke_to: paint black circles → mask toward 0.
    let target = GpuPaintTarget::from_canvas_texture(
        &tex,
        &view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(0, 0, w, h),
    );

    let mut enc = encoder(&device);
    target.composite_circle(
        &mut enc,
        &pipelines,
        &queue,
        64.0,
        64.0,
        10.0,
        [0, 0, 0, 255],
        1.0,
    );
    submit(&queue, enc);

    // end_stroke: commit.
    let mut enc = encoder(&device);
    let (entry, _req) = store.commit_region(
        &mut enc,
        &device,
        darkly::layer::LayerId::from_ffi(1),
        &frame(&tex, w, h),
        &snap,
        cr(52, 52, 24, 24),
    );
    submit(&queue, enc);

    // Verify mask is painted.
    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    let center = (64 * w + 64) as usize;
    assert!(
        pixels[center] < 10,
        "mask center should be near 0, got {}",
        pixels[center]
    );
    assert_eq!(pixels[0], 255, "mask corner should be 255");

    // Undo: restore mask to 255.
    let mut enc = encoder(&device);
    let (_forward, _req) = store.restore_region(&mut enc, &device, &entry, &frame(&tex, w, h));
    submit(&queue, enc);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    assert_eq!(
        pixels[center], 255,
        "after undo, mask center should be 255, got {}",
        pixels[center]
    );
}

// ============================================================================
// Multiple strokes with undo
// ============================================================================

/// Two separate strokes, undo one at a time.
#[test]
fn gpu_two_strokes_sequential_undo() {
    let (device, queue) = test_device();
    let (w, h) = (128, 128);
    let fmt = wgpu::TextureFormat::Rgba8Unorm;

    let (tex, view) = create_test_texture(&device, &queue, w, h, &vec![0u8; (w * h * 4) as usize]);
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let mut store = RegionScratch::new(&device, w, h);

    // --- Stroke 1: red circle at (30, 30) ---
    let mut enc = encoder(&device);
    let snap1 = store.save_region(&device, &mut enc, &frame(&tex, w, h), fmt, cr(0, 0, w, h));
    submit(&queue, enc);

    let target = GpuPaintTarget::from_canvas_texture(
        &tex,
        &view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(0, 0, w, h),
    );
    let mut enc = encoder(&device);
    target.composite_circle(
        &mut enc,
        &pipelines,
        &queue,
        30.0,
        30.0,
        5.0,
        [255, 0, 0, 255],
        1.0,
    );
    submit(&queue, enc);

    let mut enc = encoder(&device);
    let (entry1, _req) = store.commit_region(
        &mut enc,
        &device,
        darkly::layer::LayerId::from_ffi(1),
        &frame(&tex, w, h),
        &snap1,
        cr(23, 23, 14, 14),
    );
    submit(&queue, enc);

    let after_stroke1 = readback_texture(&device, &queue, &tex, fmt, w, h);

    // --- Stroke 2: blue circle at (90, 90) ---
    let mut enc = encoder(&device);
    let snap2 = store.save_region(&device, &mut enc, &frame(&tex, w, h), fmt, cr(0, 0, w, h));
    submit(&queue, enc);

    let target = GpuPaintTarget::from_canvas_texture(
        &tex,
        &view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(0, 0, w, h),
    );
    let mut enc = encoder(&device);
    target.composite_circle(
        &mut enc,
        &pipelines,
        &queue,
        90.0,
        90.0,
        5.0,
        [0, 0, 255, 255],
        1.0,
    );
    submit(&queue, enc);

    let mut enc = encoder(&device);
    let (entry2, _req) = store.commit_region(
        &mut enc,
        &device,
        darkly::layer::LayerId::from_ffi(1),
        &frame(&tex, w, h),
        &snap2,
        cr(83, 83, 14, 14),
    );
    submit(&queue, enc);

    // Verify both painted.
    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    assert_eq!(
        pixel_at(&pixels, w, 30, 30, 4)[0],
        255,
        "red circle should be present"
    );
    assert_eq!(
        pixel_at(&pixels, w, 90, 90, 4)[2],
        255,
        "blue circle should be present"
    );

    // --- Undo stroke 2 → blue gone, red remains ---
    let mut enc = encoder(&device);
    let (forward2, _req) = store.restore_region(&mut enc, &device, &entry2, &frame(&tex, w, h));
    submit(&queue, enc);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    assert_eq!(
        pixel_at(&pixels, w, 30, 30, 4)[0],
        255,
        "red should remain after undo stroke 2"
    );
    assert_eq!(
        pixel_at(&pixels, w, 90, 90, 4)[3],
        0,
        "blue should be gone after undo stroke 2"
    );

    // Compare with snapshot after stroke 1.
    let [sx, sy, sw, sh] = [23, 23, 14, 14];
    for y in sy..sy + sh {
        for x in sx..sx + sw {
            assert_eq!(
                pixel_at(&pixels, w, x, y, 4),
                pixel_at(&after_stroke1, w, x, y, 4),
                "stroke 1 region should match at ({x},{y})"
            );
        }
    }

    // --- Undo stroke 1 → both gone ---
    let mut enc = encoder(&device);
    let (forward1, _req) = store.restore_region(&mut enc, &device, &entry1, &frame(&tex, w, h));
    submit(&queue, enc);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    assert_eq!(
        pixel_at(&pixels, w, 30, 30, 4)[3],
        0,
        "red should be gone after undo stroke 1"
    );
    assert_eq!(pixel_at(&pixels, w, 90, 90, 4)[3], 0, "blue still gone");

    // --- Redo stroke 1 → red back ---
    let mut enc = encoder(&device);
    let (_backward1, _req) = store.restore_region(&mut enc, &device, &forward1, &frame(&tex, w, h));
    submit(&queue, enc);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    assert_eq!(
        pixel_at(&pixels, w, 30, 30, 4)[0],
        255,
        "red should be back after redo stroke 1"
    );
    assert_eq!(
        pixel_at(&pixels, w, 90, 90, 4)[3],
        0,
        "blue still gone (only redid stroke 1)"
    );

    // --- Redo stroke 2 → blue back ---
    let mut enc = encoder(&device);
    let (_backward2, _req) = store.restore_region(&mut enc, &device, &forward2, &frame(&tex, w, h));
    submit(&queue, enc);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    assert_eq!(
        pixel_at(&pixels, w, 30, 30, 4)[0],
        255,
        "red should still be present"
    );
    assert_eq!(
        pixel_at(&pixels, w, 90, 90, 4)[2],
        255,
        "blue should be back after redo stroke 2"
    );
}

// ============================================================================
// GpuRegionAction + UndoStack integration
// ============================================================================

/// Verify GpuRegionAction integrates with UndoStack (push/pop/complete).
#[test]
fn gpu_region_action_undo_stack() {
    use darkly::document::Document;
    use darkly::undo::{GpuRegionAction, UndoStack};

    let (device, queue) = test_device();
    let (w, h) = (64, 64);
    let fmt = wgpu::TextureFormat::Rgba8Unorm;

    let red: Vec<u8> = (0..w * h).flat_map(|_| [255u8, 0, 0, 255]).collect();
    let (tex, view) = create_test_texture(&device, &queue, w, h, &red);
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let mut store = RegionScratch::new(&device, w, h);
    let mut undo_stack = UndoStack::new(50);
    let mut doc = Document::new(w, h);

    // save → paint → commit → push.
    let mut enc = encoder(&device);
    let snap = store.save_region(&device, &mut enc, &frame(&tex, w, h), fmt, cr(0, 0, w, h));
    submit(&queue, enc);

    let target = GpuPaintTarget::from_canvas_texture(
        &tex,
        &view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(0, 0, w, h),
    );
    let mut enc = encoder(&device);
    target.composite_circle(
        &mut enc,
        &pipelines,
        &queue,
        32.0,
        32.0,
        8.0,
        [0, 255, 0, 255],
        1.0,
    );
    submit(&queue, enc);

    let mut enc = encoder(&device);
    let (entry, _req) = store.commit_region(
        &mut enc,
        &device,
        darkly::layer::LayerId::from_ffi(1),
        &frame(&tex, w, h),
        &snap,
        cr(22, 22, 20, 20),
    );
    submit(&queue, enc);
    let _ = undo_stack.push(&mut doc, Box::new(GpuRegionAction::new(entry)));

    assert!(undo_stack.can_undo());
    assert!(!undo_stack.can_redo());

    // Pop for undo, execute GPU restore, complete.
    let mut action = undo_stack.pop_for_undo().unwrap();
    let affected = action.undo(&mut doc);
    assert!(affected.is_empty(), "GPU action returns empty affected map");

    if let Some(entry) = action.gpu_region_entry_mut() {
        let mut enc = encoder(&device);
        let (forward, _req) = store.restore_region(&mut enc, &device, entry, &frame(&tex, w, h));
        submit(&queue, enc);
        *entry = forward;
    }
    undo_stack.complete_undo(action);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    let center = pixel_at(&pixels, w, 32, 32, 4);
    assert_eq!(
        center,
        &[255, 0, 0, 255],
        "after undo, should be red, got {:?}",
        center
    );

    assert!(!undo_stack.can_undo());
    assert!(undo_stack.can_redo());

    // Pop for redo, execute GPU restore, complete.
    let mut action = undo_stack.pop_for_redo().unwrap();
    let affected = action.redo(&mut doc);
    assert!(affected.is_empty());

    if let Some(entry) = action.gpu_region_entry_mut() {
        let mut enc = encoder(&device);
        let (backward, _req) = store.restore_region(&mut enc, &device, entry, &frame(&tex, w, h));
        submit(&queue, enc);
        *entry = backward;
    }
    undo_stack.complete_redo(action);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    let center = pixel_at(&pixels, w, 32, 32, 4);
    assert!(
        center[1] > 0,
        "after redo, green should be visible, got G={}",
        center[1]
    );

    assert!(undo_stack.can_undo());
    assert!(!undo_stack.can_redo());
}

// ============================================================================
// Mixed GPU + CPU undo coexistence
// ============================================================================

/// Push a GPU action, then a CPU PropertyAction, undo both.
#[test]
fn gpu_cpu_undo_interleaved() {
    use darkly::document::Document;
    use darkly::layer::Layer;
    use darkly::undo::property::Property;
    use darkly::undo::{GpuRegionAction, PropertyAction, UndoStack};

    let (device, queue) = test_device();
    let (w, h) = (64, 64);
    let fmt = wgpu::TextureFormat::Rgba8Unorm;

    let (tex, view) = create_test_texture(&device, &queue, w, h, &vec![0u8; (w * h * 4) as usize]);
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let mut store = RegionScratch::new(&device, w, h);
    let mut undo_stack = UndoStack::new(50);
    let mut doc = Document::new(w, h);
    let layer_id = doc.add_raster_layer(None);

    // Step 1: GPU paint stroke.
    let mut enc = encoder(&device);
    let snap = store.save_region(&device, &mut enc, &frame(&tex, w, h), fmt, cr(0, 0, w, h));
    submit(&queue, enc);

    let target = GpuPaintTarget::from_canvas_texture(
        &tex,
        &view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(0, 0, w, h),
    );
    let mut enc = encoder(&device);
    target.composite_circle(
        &mut enc,
        &pipelines,
        &queue,
        32.0,
        32.0,
        5.0,
        [255, 0, 0, 255],
        1.0,
    );
    submit(&queue, enc);

    let mut enc = encoder(&device);
    let (entry, _req) = store.commit_region(
        &mut enc,
        &device,
        layer_id,
        &frame(&tex, w, h),
        &snap,
        cr(25, 25, 14, 14),
    );
    submit(&queue, enc);
    let _ = undo_stack.push(&mut doc, Box::new(GpuRegionAction::new(entry)));

    // Step 2: CPU property change (opacity).
    let _ = undo_stack.push(
        &mut doc,
        Box::new(PropertyAction::new(
            layer_id,
            Property::Opacity(1.0),
            Property::Opacity(0.5),
        )),
    );
    if let Some(Layer::Raster(r)) = doc.layer_mut(layer_id) {
        r.blend.opacity = 0.5;
    }

    // Undo #1: property change (CPU — no GPU work).
    let mut action = undo_stack.pop_for_undo().unwrap();
    let _affected = action.undo(&mut doc);
    assert!(
        action.gpu_region_entry_mut().is_none(),
        "property action should not be GPU"
    );
    undo_stack.complete_undo(action);

    if let Some(Layer::Raster(r)) = doc.layer(layer_id) {
        assert!(
            (r.blend.opacity - 1.0).abs() < f32::EPSILON,
            "opacity should be restored to 1.0"
        );
    }

    // Undo #2: GPU paint stroke.
    let mut action = undo_stack.pop_for_undo().unwrap();
    let _affected = action.undo(&mut doc);
    if let Some(entry) = action.gpu_region_entry_mut() {
        let mut enc = encoder(&device);
        let (forward, _req) = store.restore_region(&mut enc, &device, entry, &frame(&tex, w, h));
        submit(&queue, enc);
        *entry = forward;
    }
    undo_stack.complete_undo(action);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    assert_eq!(
        pixel_at(&pixels, w, 32, 32, 4)[3],
        0,
        "paint should be undone"
    );

    // Redo both.
    let mut action = undo_stack.pop_for_redo().unwrap();
    let _affected = action.redo(&mut doc);
    if let Some(entry) = action.gpu_region_entry_mut() {
        let mut enc = encoder(&device);
        let (backward, _req) = store.restore_region(&mut enc, &device, entry, &frame(&tex, w, h));
        submit(&queue, enc);
        *entry = backward;
    }
    undo_stack.complete_redo(action);

    let pixels = readback_texture(&device, &queue, &tex, fmt, w, h);
    assert!(
        pixel_at(&pixels, w, 32, 32, 4)[0] > 0,
        "paint should be redone"
    );

    let mut action = undo_stack.pop_for_redo().unwrap();
    let _affected = action.redo(&mut doc);
    undo_stack.complete_redo(action);

    if let Some(Layer::Raster(r)) = doc.layer(layer_id) {
        assert!(
            (r.blend.opacity - 0.5).abs() < f32::EPSILON,
            "opacity should be 0.5 after redo"
        );
    }
}

// ============================================================================
// DiffRectPass: GPU diff-based undo region
// ============================================================================

/// Paint a circle far from the origin, use DiffRectPass to find the changed
/// region, and verify the diff rect covers the painted pixels. This is the
/// mechanism that fixes scatter brush undo — the diff finds the actual changed
/// pixels regardless of where the stroke engine thought they were.
#[test]
fn diff_rect_finds_painted_region() {
    let (device, queue) = test_device();
    let (w, h) = (128, 128);
    let fmt = wgpu::TextureFormat::Rgba8Unorm;

    // Two identical transparent textures.
    let blank = vec![0u8; (w * h * 4) as usize];
    let (_scratch_tex, scratch_view) = create_test_texture(&device, &queue, w, h, &blank);
    let (canvas_tex, canvas_view) = create_test_texture(&device, &queue, w, h, &blank);

    // Paint a circle at (100, 100) on the canvas only — simulating a
    // scattered dab that landed far from where the stroke engine tracked.
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let target = GpuPaintTarget::from_canvas_texture(
        &canvas_tex,
        &canvas_view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(0, 0, w, h),
    );
    let mut enc = encoder(&device);
    target.composite_circle(
        &mut enc,
        &pipelines,
        &queue,
        100.0,
        100.0,
        8.0,
        [255, 0, 0, 255],
        1.0,
    );
    submit(&queue, enc);

    // Dispatch the diff.
    let mut diff = DiffRectPass::new(&device);
    diff.request(&device, &queue, &scratch_view, &canvas_view, cr(0, 0, w, h));

    // Poll until ready (native/test — blocking poll is fine).
    let rect = loop {
        if let Some(result) = diff.poll(&device) {
            break result;
        }
        let _ = device.poll(wgpu::PollType::Wait {
            submission_index: None,
            timeout: None,
        });
    };

    let rect = rect.expect("diff should find changed pixels");
    let (rx, ry, rw, rh) = (rect.x0(), rect.y0(), rect.width, rect.height);

    // The circle is at (100, 100) with radius 8. The diff rect should
    // contain the circle — center must be inside the rect.
    assert!(
        rx <= 100 && 100 < rx + rw as i32,
        "diff rect x range [{}, {}) should contain 100",
        rx,
        rx + rw as i32
    );
    assert!(
        ry <= 100 && 100 < ry + rh as i32,
        "diff rect y range [{}, {}) should contain 100",
        ry,
        ry + rh as i32
    );

    // Rect should be reasonably tight (not the full canvas).
    assert!(rw < 30, "diff rect width should be tight, got {rw}");
    assert!(rh < 30, "diff rect height should be tight, got {rh}");
}

/// Verify that undo fully restores the canvas when the diff rect is used
/// instead of a hand-tracked stroke rect — the key regression test for
/// the scatter brush undo bug.
#[test]
fn diff_rect_undo_restores_offset_paint() {
    let (device, queue) = test_device();
    let (w, h) = (128, 128);
    let fmt = wgpu::TextureFormat::Rgba8Unorm;

    let blank = vec![0u8; (w * h * 4) as usize];
    let (tex, view) = create_test_texture(&device, &queue, w, h, &blank);
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let mut store = RegionScratch::new(&device, w, h);

    // begin_stroke: save full canvas to scratch.
    let mut enc = encoder(&device);
    let snap = store.save_region(&device, &mut enc, &frame(&tex, w, h), fmt, cr(0, 0, w, h));
    submit(&queue, enc);

    // Paint a circle at (100, 100) — far from origin, simulating scatter.
    let target = GpuPaintTarget::from_canvas_texture(
        &tex,
        &view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(0, 0, w, h),
    );
    let mut enc = encoder(&device);
    target.composite_circle(
        &mut enc,
        &pipelines,
        &queue,
        100.0,
        100.0,
        8.0,
        [255, 0, 0, 255],
        1.0,
    );
    submit(&queue, enc);

    // Verify paint landed.
    let painted = readback_texture(&device, &queue, &tex, fmt, w, h);
    assert!(
        pixel_at(&painted, w, 100, 100, 4)[3] > 0,
        "paint should be visible at (100,100)"
    );

    // Compute diff rect via GPU (instead of hand-tracking).
    let scratch_view = store.scratch_view(fmt);
    let mut diff = DiffRectPass::new(&device);
    diff.request(&device, &queue, &scratch_view, &view, cr(0, 0, w, h));

    let rect = loop {
        if let Some(result) = diff.poll(&device) {
            break result;
        }
        let _ = device.poll(wgpu::PollType::Wait {
            submission_index: None,
            timeout: None,
        });
    };
    let rect = rect.expect("should have a diff rect");

    // Commit with the diff-derived rect.
    let mut enc = encoder(&device);
    let (entry, _req) = store.commit_region(
        &mut enc,
        &device,
        darkly::layer::LayerId::from_ffi(1),
        &frame(&tex, w, h),
        &snap,
        rect,
    );
    submit(&queue, enc);

    // Undo: restore the pre-stroke state.
    let mut enc = encoder(&device);
    let (_forward, _req) = store.restore_region(&mut enc, &device, &entry, &frame(&tex, w, h));
    submit(&queue, enc);

    // The entire canvas should be back to transparent.
    let restored = readback_texture(&device, &queue, &tex, fmt, w, h);
    assert_eq!(
        pixel_at(&restored, w, 100, 100, 4)[3],
        0,
        "after undo, scattered paint at (100,100) should be gone"
    );

    // Verify the whole canvas is clean.
    for y in 0..h {
        for x in 0..w {
            let a = pixel_at(&restored, w, x, y, 4)[3];
            assert_eq!(
                a, 0,
                "pixel ({x},{y}) should be transparent after undo, got A={a}"
            );
        }
    }
}

/// Regression for canvas-coord snapshot storage: a `Snapshot` saved on a
/// 256×256 layer at canvas (0, 0) survives a negative-direction grow that
/// shifts the layer's local-coord origin by (256, 256). Commit at canvas
/// (-100, -100) must round-trip via `canvas_to_layer_rect` to layer-local
/// (156, 156) in the new frame, and undo must restore the correct pre-stroke
/// pixels at the original canvas position.
#[test]
fn negative_direction_grow_crosses_zero() {
    use wgpu::TextureUsages;
    let (device, queue) = test_device();
    let fmt = wgpu::TextureFormat::Rgba8Unorm;

    // Initial layer: 256×256 at canvas (0, 0), filled red.
    let (init_w, init_h) = (256u32, 256u32);
    let red: Vec<u8> = (0..init_w * init_h)
        .flat_map(|_| [255u8, 0, 0, 255])
        .collect();
    let (initial_tex, _v) = create_test_texture(&device, &queue, init_w, init_h, &red);
    let initial_frame = CanvasFrame {
        texture: &initial_tex,
        canvas_extent: cr(0, 0, init_w, init_h),
    };

    let mut store = RegionScratch::new(&device, init_w, init_h);

    // Save the full 256×256 layer (canvas (0, 0) → (256, 256)) as the
    // pre-stroke snapshot.
    let mut enc = encoder(&device);
    let mut snap = store.save_region(
        &device,
        &mut enc,
        &initial_frame,
        fmt,
        cr(0, 0, init_w, init_h),
    );
    submit(&queue, enc);

    // Simulate a negative-direction grow: new 512×512 layer at canvas
    // (-256, -256). Old contents land at layer-local (256, 256).
    let (new_w, new_h) = (512u32, 512u32);
    let new_tex = device.create_texture(&wgpu::TextureDescriptor {
        label: Some("grown-layer"),
        size: wgpu::Extent3d {
            width: new_w,
            height: new_h,
            depth_or_array_layers: 1,
        },
        mip_level_count: 1,
        sample_count: 1,
        dimension: wgpu::TextureDimension::D2,
        format: fmt,
        usage: TextureUsages::TEXTURE_BINDING
            | TextureUsages::COPY_SRC
            | TextureUsages::COPY_DST
            | TextureUsages::RENDER_ATTACHMENT,
        view_formats: &[],
    });
    let mut enc = encoder(&device);
    enc.copy_texture_to_texture(
        wgpu::TexelCopyTextureInfo {
            texture: &initial_tex,
            mip_level: 0,
            origin: wgpu::Origin3d::ZERO,
            aspect: wgpu::TextureAspect::All,
        },
        wgpu::TexelCopyTextureInfo {
            texture: &new_tex,
            mip_level: 0,
            origin: wgpu::Origin3d {
                x: 256,
                y: 256,
                z: 0,
            },
            aspect: wgpu::TextureAspect::All,
        },
        wgpu::Extent3d {
            width: init_w,
            height: init_h,
            depth_or_array_layers: 1,
        },
    );
    // Rebase the region_store scratch alongside the layer.
    store.grow_scratch_preserving(&device, &mut enc, new_w, new_h, 256, 256);
    submit(&queue, enc);

    // After grow the engine widens snap.saved to the new canvas extent so
    // commits that spill into the newly-grown area are still contained.
    snap.saved = cr(-256, -256, new_w, new_h);

    // Frame describing the post-grow layer at canvas (-256, -256).
    let new_frame = CanvasFrame {
        texture: &new_tex,
        canvas_extent: cr(-256, -256, new_w, new_h),
    };

    // Paint blue on top of the (previously red) pre-grow pixels at canvas
    // (50, 50) — translated to new layer-local (306, 306). Then commit a
    // sub-rect that *crosses zero*: canvas (-100, -100, 200, 200) covers
    // both new (transparent) area and the original canvas region.
    let pipelines = PaintPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let new_view = new_tex.create_view(&wgpu::TextureViewDescriptor::default());
    let target = GpuPaintTarget::from_extent(
        &new_tex,
        &new_view,
        fmt,
        darkly::coord::CanvasRect::from_xywh(-256, -256, new_w, new_h),
        darkly::coord::CanvasRect::from_xywh(0, 0, new_w, new_h),
    );
    let mut enc = encoder(&device);
    target.composite_circle(
        &mut enc,
        &pipelines,
        &queue,
        50.0,
        50.0,
        20.0,
        [0, 0, 255, 255],
        1.0,
    );
    submit(&queue, enc);

    // Commit a canvas rect that spans (-100, -100) → (100, 100). This is
    // the regression: pre-fix, this rect would not be representable in the
    // pre-grow saved frame and would either panic or write garbage; post-
    // fix, the canvas-coord rect translates cleanly to the new layer's
    // local frame at (156, 156, 200, 200).
    let mut enc = encoder(&device);
    let commit_rect = cr(-100, -100, 200, 200);
    let (entry, _req) = store.commit_region(
        &mut enc,
        &device,
        darkly::layer::LayerId::from_ffi(1),
        &new_frame,
        &snap,
        commit_rect,
    );
    submit(&queue, enc);

    // Undo: restore the pre-stroke pixels at canvas (-100, -100, 200, 200).
    let mut enc = encoder(&device);
    let (_forward, _req) = store.restore_region(&mut enc, &device, &entry, &new_frame);
    submit(&queue, enc);

    // After undo, canvas (50, 50) should be back to red (the pre-stroke
    // state), and canvas (-100, -100) (which was zeroed pre-grow) should
    // be transparent — both correctly restored.
    let pixels = readback_texture(&device, &queue, &new_tex, fmt, new_w, new_h);
    // canvas (50, 50) → layer-local (306, 306).
    let p = pixel_at(&pixels, new_w, 306, 306, 4);
    assert_eq!(
        p,
        &[255, 0, 0, 255],
        "canvas (50, 50) should be the pre-stroke red after undo, got {:?}",
        p
    );
    // canvas (-100, -100) → layer-local (156, 156). Should be transparent.
    let q = pixel_at(&pixels, new_w, 156, 156, 4);
    assert_eq!(
        q[3], 0,
        "canvas (-100, -100) should be transparent after undo, got A={}",
        q[3]
    );
}