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
//! Native-only integration tests for the full-stroke brush editor preview.
//!
//! Asserts the stroke engine runs end-to-end against the preview's own
//! scratch target and produces non-empty RGBA pixels where the S-curve was
//! drawn. Uses the blocking `test_utils::readback_texture` helper — native
//! only; the wasm path does async readback via the ReadbackScheduler.

use darkly::brush::{
    default_graph,
    pipeline::BrushPipelines,
    preview_renderer::{synthesize_stroke_path, BrushStrokePreviewRenderer},
};
use darkly::gpu::test_utils::{readback_texture, test_device};

#[test]
fn renders_s_curve_over_black_background() {
    let (device, queue) = test_device();
    let pipelines = BrushPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let mut renderer = BrushStrokePreviewRenderer::new();
    let graph = default_graph();

    let width: u32 = 320;
    let height: u32 = 120;
    let path = synthesize_stroke_path(width as f32, height as f32, 30, 0.0);

    let fg = [1.0, 1.0, 1.0, 1.0]; // white stroke
    let bg = [0.0, 0.0, 0.0, 1.0]; // black background

    let texture = renderer
        .render_stroke(
            &device, &queue, &pipelines, &graph, &path, fg, bg, width, height,
        )
        .expect("render_stroke should return a texture for the default graph");

    let pixels = readback_texture(
        &device,
        &queue,
        texture,
        wgpu::TextureFormat::Rgba8Unorm,
        width,
        height,
    );

    assert_eq!(pixels.len(), (width * height * 4) as usize);

    // Pixel at (x, y), RGBA.
    let get = |x: u32, y: u32| -> [u8; 4] {
        let i = ((y * width + x) * 4) as usize;
        [pixels[i], pixels[i + 1], pixels[i + 2], pixels[i + 3]]
    };

    // A corner not crossed by the stroke should still be the solid bg.
    // Top-right corner falls outside the S-curve envelope.
    let corner = get(width - 2, 1);
    assert_eq!(
        corner[3], 255,
        "bg alpha should remain opaque away from the stroke"
    );
    assert!(
        corner[0] < 40 && corner[1] < 40 && corner[2] < 40,
        "bg corner should stay near-black, got {:?}",
        corner
    );

    // The S-curve passes through the geometric center near peak pressure.
    // At least one nearby sample should be brighter than the background.
    let mut any_bright = false;
    for dy in -10i32..=10 {
        for dx in -10i32..=10 {
            let x = (width as i32 / 2 + dx) as u32;
            let y = (height as i32 / 2 + dy) as u32;
            let px = get(x, y);
            if px[0] > 64 || px[1] > 64 || px[2] > 64 {
                any_bright = true;
            }
        }
    }
    assert!(
        any_bright,
        "expected bright pixels near the center along the S-curve"
    );

    // Deliberately no wall-clock assertion here. Render time is dominated
    // by the GPU backend: ~5-20 ms on native Vulkan/Metal, several
    // hundred ms on CI's software fallback (lavapipe). Any bound loose
    // enough for CI catches only cartoonish regressions; any bound tight
    // enough to be meaningful flakes on CI. Perf tracking for this path
    // belongs in a dedicated bench on hardware, not here.
}

#[test]
fn renderer_reuses_target_across_renders_of_same_size() {
    let (device, queue) = test_device();
    let pipelines = BrushPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let mut renderer = BrushStrokePreviewRenderer::new();
    let graph = default_graph();
    let path = synthesize_stroke_path(320.0, 120.0, 20, 0.0);

    assert!(renderer.current_size().is_none());

    let _ = renderer.render_stroke(
        &device,
        &queue,
        &pipelines,
        &graph,
        &path,
        [1.0, 1.0, 1.0, 1.0],
        [0.0, 0.0, 0.0, 1.0],
        320,
        120,
    );
    assert_eq!(renderer.current_size(), Some((320, 120)));
    let first_ptr = renderer.current_texture().map(|t| t as *const _);

    let _ = renderer.render_stroke(
        &device,
        &queue,
        &pipelines,
        &graph,
        &path,
        [1.0, 0.0, 0.0, 1.0],
        [1.0, 1.0, 1.0, 1.0],
        320,
        120,
    );
    let second_ptr = renderer.current_texture().map(|t| t as *const _);

    // Same size → same underlying texture.
    assert_eq!(first_ptr, second_ptr);
}

/// Decode a `brush_stroke_preview()` PNG to raw RGBA bytes plus its
/// canonical `BRUSH_THUMBNAIL_SIZE` dimensions — same shape the frontend
/// receives via the `Blob` URL path.
fn decode_preview_png(png_bytes: &[u8]) -> (u32, u32, Vec<u8>) {
    let img = image::load_from_memory(png_bytes).expect("valid PNG bytes");
    let rgba = img.to_rgba8();
    let (w, h) = rgba.dimensions();
    (w, h, rgba.into_raw())
}

#[test]
fn engine_brush_stroke_preview_caches_after_readback() {
    use darkly::engine::DarklyEngine;
    use darkly::gpu::context::GpuContext;

    let (device, queue) = test_device();
    let gpu = GpuContext::new_headless(device, queue);
    let mut engine = DarklyEngine::new(gpu, 1024, 768);

    // First call: cache empty, kicks off a readback, returns an empty Vec
    // — the frontend uses that as a "no fresh bytes" signal so it
    // preserves whatever was last shown rather than flashing transparent.
    let first = engine.brush_stroke_preview();
    assert!(
        first.is_empty(),
        "cache miss should return empty Vec, got {} bytes",
        first.len()
    );

    // Flush the in-flight readback (native-only helper; wasm relies on the
    // event loop polling the ReadbackScheduler via the render loop).
    engine.test_flush_readbacks();

    // Second call: cache now populated with PNG bytes — same shape as
    // `brush_active_dab_preview` / `brush_thumbnail`.
    let second = engine.brush_stroke_preview();
    assert!(
        !second.is_empty(),
        "post-readback call should return cached PNG bytes"
    );
    assert_eq!(
        &second[..8],
        &[0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A],
        "cache should hold PNG-encoded bytes"
    );

    // Decode and verify the stroke deposited ink.
    let (w, h, pixels) = decode_preview_png(&second);
    assert!(w > 0 && h > 0, "decoded preview has positive dimensions");
    let non_zero_pixels = pixels
        .chunks_exact(4)
        .filter(|px| px[0] > 0 || px[1] > 0 || px[2] > 0)
        .count();
    assert!(
        non_zero_pixels > 100,
        "expected non-trivial stroke coverage in cached preview, got {non_zero_pixels} non-zero pixels"
    );
}

#[test]
fn engine_brush_stroke_preview_skips_unchanged_graph() {
    use darkly::engine::DarklyEngine;
    use darkly::gpu::context::GpuContext;

    let (device, queue) = test_device();
    let gpu = GpuContext::new_headless(device, queue);
    let mut engine = DarklyEngine::new(gpu, 1024, 768);

    // Prime the cache.
    let _ = engine.brush_stroke_preview();
    engine.test_flush_readbacks();
    let first = engine.brush_stroke_preview();

    // Without touching the graph, a second call returns the same cache
    // and does not queue another readback.
    let second = engine.brush_stroke_preview();
    assert_eq!(first, second);
}

#[test]
fn set_preview_theme_invalidates_cache() {
    use darkly::engine::DarklyEngine;
    use darkly::gpu::context::GpuContext;

    let (device, queue) = test_device();
    let gpu = GpuContext::new_headless(device, queue);
    let mut engine = DarklyEngine::new(gpu, 1024, 768);

    // Prime the cache with the default (dark) theme: white on dark.
    engine.set_preview_theme([1.0, 1.0, 1.0, 1.0], [0.02, 0.02, 0.02, 1.0]);
    let _ = engine.brush_stroke_preview();
    engine.test_flush_readbacks();
    let dark_png = engine.brush_stroke_preview();

    // Switch to the light theme: black on light. Cache should invalidate
    // and the next readback should produce distinctly different pixels.
    engine.set_preview_theme([0.0, 0.0, 0.0, 1.0], [0.9, 0.9, 0.9, 1.0]);
    let after_change = engine.brush_stroke_preview();
    // Pre-readback call returns an empty Vec (cache was invalidated).
    assert!(after_change.is_empty());

    engine.test_flush_readbacks();
    let light_png = engine.brush_stroke_preview();

    assert_ne!(
        dark_png, light_png,
        "theme change must produce new preview bytes"
    );
    // Sanity-check: the light-theme preview has bright bg pixels.
    let (_, _, light_pixels) = decode_preview_png(&light_png);
    let mut bright_bg = 0;
    for chunk in light_pixels.chunks_exact(4) {
        if chunk[0] > 200 && chunk[1] > 200 && chunk[2] > 200 {
            bright_bg += 1;
        }
    }
    assert!(
        bright_bg > 1000,
        "light theme preview should have many bright bg pixels, got {bright_bg}"
    );
}

#[test]
fn brush_save_bakes_thumbnail_asynchronously() {
    use darkly::brush::bundle::Brush;
    use darkly::engine::DarklyEngine;
    use darkly::gpu::context::GpuContext;

    let (device, queue) = test_device();
    let gpu = GpuContext::new_headless(device, queue);
    let mut engine = DarklyEngine::new(gpu, 1024, 768);

    // Save a brush — kicks off an async thumbnail readback against the
    // engine's library copy.
    engine.brush_save("TestBrush", "basic").unwrap();

    // Before the readback lands, the library entry has no thumbnail.
    let exported_before = engine.brush_export("TestBrush").expect("brush exported");
    let bundle_before = Brush::from_bytes(&exported_before).unwrap();
    assert!(
        bundle_before.thumbnail_png.is_none(),
        "thumbnail should be absent before readback completes"
    );

    // Flush the pending readback; the completion handler writes the PNG
    // back onto the library entry.
    engine.test_flush_readbacks();

    let exported_after = engine.brush_export("TestBrush").unwrap();
    let bundle_after = Brush::from_bytes(&exported_after).unwrap();
    let png = bundle_after
        .thumbnail_png
        .expect("thumbnail present after readback");
    // Valid PNG — starts with the PNG magic signature.
    assert_eq!(
        &png[..8],
        &[0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A],
        "library entry now carries a PNG-encoded thumbnail"
    );
}

/// Regression: Hard Round (no `pressure → size_input` wire) paints every
/// dab at full size, including the endpoints. The endpoint dabs must not
/// be clipped against the cache border — the leftmost and rightmost
/// columns of the framed preview must contain only background pixels.
///
/// This is the user-visible bug: with the previous size-aware inset
/// hack, the path was shrunk so endpoints landed inside the canvas,
/// but the inset clamped to half the canvas at any non-trivial size and
/// the path degenerated. Without an inset, endpoints sit on the canvas
/// edge and the framer can't recover the clipped half of the dab.
#[test]
fn airbrush_endpoint_dabs_not_clipped_against_cache_border() {
    use darkly::engine::DarklyEngine;
    use darkly::gpu::context::GpuContext;

    let (device, queue) = test_device();
    let gpu = GpuContext::new_headless(device, queue);
    let mut engine = DarklyEngine::new(gpu, 1024, 768);

    // Pin the theme so the bg pixel value is deterministic for the test
    // — black bg, white stroke.
    engine.set_preview_theme([1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 0.0, 1.0]);

    // Airbrush is a built-in: shape tip with a fixed `size_input` constant
    // (no pressure→size_input wire), so the dab radius doesn't scale with
    // the synthetic stroke's pressure ramp. Same invariant the old
    // "Hard Round" test exercised before that brush was removed.
    engine.brush_load("Airbrush").expect("Airbrush built-in");

    // Prime + flush + read.
    let _ = engine.brush_stroke_preview();
    engine.test_flush_readbacks();
    let png = engine.brush_stroke_preview();
    let (width, height, pixels) = decode_preview_png(&png);
    assert_eq!(pixels.len(), (width * height * 4) as usize);

    // bg is black; mark anything noticeably brighter as stroke.
    const TOLERANCE: u8 = 16;
    let is_stroke = |i: usize| -> bool {
        pixels[i] > TOLERANCE || pixels[i + 1] > TOLERANCE || pixels[i + 2] > TOLERANCE
    };

    // The leftmost and rightmost columns must be entirely background —
    // any stroke pixel there means an endpoint dab was clipped.
    let edge_band = 1u32;
    for x_band in [0..edge_band, (width - edge_band)..width] {
        for x in x_band {
            for y in 0..height {
                let i = ((y * width + x) * 4) as usize;
                assert!(
                    !is_stroke(i),
                    "Airbrush preview cuts off at the edge — column {x} y={y} \
                     has stroke pixel rgba={:?}",
                    [pixels[i], pixels[i + 1], pixels[i + 2]],
                );
            }
        }
    }
}

/// Regression: scrubbing a `pen_input.stabilize` setting must not
/// invalidate the editor-preview cache. The synthetic-stroke preview
/// always renders with `PassThrough`, so the rendered pixels can't
/// change in response to a user scrub. Bumping `brush_graph_version`
/// on these scrubs would trigger a wasted full-stroke re-render every
/// 100 ms while the user drags the slider (~1 GB/s of GPU work for no
/// visible effect).
///
/// The fix declares stabilize via `with_preview_value(0.0)` and routes
/// scrubs on any preview-irrelevant port through
/// `ChangeKind::PreviewIrrelevantScrub`, which skips the version bump.
/// Asserted against the public `brush_graph_version()` getter, with a
/// negative-control scrub (`stamp.rotation`, no `preview_value`) to
/// guard against the rule being over-broad.
#[test]
fn stabilize_scrub_does_not_bump_editor_preview_version() {
    use darkly::engine::DarklyEngine;
    use darkly::gpu::context::GpuContext;

    let (device, queue) = test_device();
    let gpu = GpuContext::new_headless(device, queue);
    let mut engine = DarklyEngine::new(gpu, 1024, 768);

    // Ink Pen exposes both `stabilize` (default 0.6) and `size` so we can
    // contrast a preview-irrelevant scrub against a preview-affecting
    // one in the same engine — avoids creating a second wgpu device.
    engine.brush_load("Ink Pen").expect("Ink Pen built-in");

    // Prime the editor preview cache and let the readback land so the
    // version counter is at its post-init steady state.
    let _ = engine.brush_stroke_preview();
    engine.test_flush_readbacks();
    let v_before_stabilize = engine.brush_graph_version();

    // Find the exposed `stabilize` port and scrub it through
    // `brush_set_exposed_port` — the same entry point the brush bar uses.
    let stabilize = engine
        .brush_exposed_ports()
        .into_iter()
        .find(|p| p.port_name == "stabilize")
        .expect("Ink Pen exposes a `stabilize` port");
    engine
        .brush_set_exposed_port(stabilize.node_id, "stabilize", 90.0)
        .expect("scrub set");

    assert_eq!(
        engine.brush_graph_version(),
        v_before_stabilize,
        "stabilize is preview-irrelevant (PassThrough is hardcoded for \
         the synthetic stroke); scrubbing it must not bump \
         brush_graph_version — bumping invalidates the editor preview \
         cache and triggers a wasted full-stroke re-render."
    );

    // Negative control: scrubbing a port the preview *does* read must
    // still bump the version. After the compiled-WGSL migration
    // `softness` lives on the upstream `shape` node (the
    // `paint` terminal has no softness port). It has no
    // `preview_irrelevant_scrub` flag, is read by the preview shader,
    // and is unwired — the perfect canary for "rule too broad". Find
    // its node via the exposed-port listing.
    let softness = engine
        .brush_exposed_ports()
        .into_iter()
        .find(|p| p.port_name == "softness")
        .expect("Ink Pen exposes a `softness` port (on shape after migration)");
    let v_before_softness = engine.brush_graph_version();
    engine
        .brush_set_exposed_port(softness.node_id, "softness", 0.5)
        .expect("scrub set");
    assert_ne!(
        engine.brush_graph_version(),
        v_before_softness,
        "softness is not flagged preview_irrelevant_scrub → it affects \
         the preview output → its scrub must bump brush_graph_version. \
         If this assertion fails, the preview-irrelevant rule is \
         over-broad and real preview updates would also stall."
    );
}

/// Regression: scrubbing `paint.size` (or any port flagged with
/// `preview_value`) must not invalidate the editor-preview cache.
///
/// The previous "continued charcoal debugging" attempt deleted the
/// caller-side `graph.apply_preview_overrides()` on the stroke-preview
/// path and rewrote the `ChangeKind` classifier to key off
/// `preview_irrelevant_scrub` only, with the rationale "the preview
/// must match what the user would actually paint". That made the
/// brush picker tile and the editor stroke preview inconsistent —
/// the tile stayed size-invariant via `reset_exposed_scrubs`, but the
/// stroke preview now mutated visibly on every size scrub.
///
/// Both previews share the same intent: brush identity, not momentary
/// scrub state. This test pins the restored behavior: a size scrub
/// on the active brush must not bump `brush_graph_version` (which
/// is what gates the editor preview cache) — because the renderer
/// neutralizes `preview_value` ports before rendering, so the output
/// is identical anyway.
#[test]
fn size_scrub_does_not_bump_editor_preview_version() {
    use darkly::engine::DarklyEngine;
    use darkly::gpu::context::GpuContext;

    let (device, queue) = test_device();
    let gpu = GpuContext::new_headless(device, queue);
    let mut engine = DarklyEngine::new(gpu, 1024, 768);

    engine.brush_load("Ink Pen").expect("Ink Pen built-in");
    let _ = engine.brush_stroke_preview();
    engine.test_flush_readbacks();
    let v_before = engine.brush_graph_version();

    let size = engine
        .brush_exposed_ports()
        .into_iter()
        .find(|p| p.port_name == "size")
        .expect("Ink Pen exposes a `size` port");
    engine
        .brush_set_exposed_port(size.node_id, "size", 90.0)
        .expect("scrub set");

    assert_eq!(
        engine.brush_graph_version(),
        v_before,
        "scrubbing `size` must not bump brush_graph_version. \
         `paint.size` is flagged `preview_value` (= 0.1), and the \
         stroke-preview render path applies `apply_preview_overrides` \
         to neutralize it before rendering — so the editor preview's \
         output cannot change in response to the scrub, and \
         invalidating its cache would just trigger a wasted \
         full-stroke re-render and a visible blink as the new size \
         briefly shows."
    );
}

#[test]
fn empty_path_returns_none() {
    let (device, queue) = test_device();
    let pipelines = BrushPipelines::new(
        &device,
        &queue,
        &darkly::gpu::selection::selection_mask_bgl(&device),
    );
    let mut renderer = BrushStrokePreviewRenderer::new();
    let graph = default_graph();

    let result = renderer.render_stroke(
        &device,
        &queue,
        &pipelines,
        &graph,
        &[],
        [1.0, 1.0, 1.0, 1.0],
        [0.0, 0.0, 0.0, 1.0],
        320,
        120,
    );
    assert!(result.is_none());
}