agg-gui 0.4.0

Immediate-mode Rust GUI library with AGG rendering, Y-up layout, widgets, text, SVG, and native/WASM adapters
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
//! Paint orchestration for the widget tree.
//!
//! Owns the painting traversal: the thread-local `PAINT_CLIP_STACK` that
//! lets descendants query the active clip, [`paint_subtree`] dispatch
//! between direct paint and backbuffer-cached paint, and the GL/software
//! backbuffer variants used by widgets that opt in via
//! [`Widget::backbuffer_spec`](crate::widget::Widget::backbuffer_spec).
//!
//! # Coordinate system
//!
//! All paint coordinates are **logical Y-up**, origin at the bottom-left.
//! Each subtree paints with its `DrawCtx` translated so that (0,0) maps to
//! the widget's own bottom-left corner; child traversal applies further
//! per-child translations. Platform input coordinates are Y-down and are
//! converted at the App event boundary (see `App::flip_y`), not here.

use std::sync::Arc;

use crate::framebuffer::Framebuffer;
use crate::gfx_ctx::GfxCtx;
use crate::lcd_coverage::LcdBuffer;

use super::*;

std::thread_local! {
    static PAINT_CLIP_STACK: std::cell::RefCell<Vec<Rect>> =
        std::cell::RefCell::new(Vec::new());
}

/// Current visible paint clip in root coordinates, if painting is inside a
/// clipped subtree. Widgets can use this to avoid starting expensive work for
/// content that traversal visits but the active clip will discard.
pub fn current_paint_clip() -> Option<Rect> {
    PAINT_CLIP_STACK.with(|stack| stack.borrow().last().copied())
}

// ---------------------------------------------------------------------------
// Tree traversal helpers (free functions operating on &mut dyn Widget)
// ---------------------------------------------------------------------------

/// Paint `widget` and all its descendants. The caller must ensure `ctx` is
/// already translated so that (0,0) maps to `widget`'s bottom-left corner.
///
/// If the widget returns `Some` from [`Widget::backbuffer_cache_mut`], the
/// whole subtree (widget + children + overlay) is rendered once into a CPU
/// [`Framebuffer`] via a software [`GfxCtx`], cached as an
/// `Arc<Vec<u8>>` on the widget, and blitted through
/// [`DrawCtx::draw_image_rgba_arc`].  Subsequent frames that find
/// `cache.dirty == false` skip the re-raster entirely and just blit the
/// existing bitmap — identical fast path to MatterCAD's `DoubleBuffer`.
pub fn paint_subtree(widget: &mut dyn Widget, ctx: &mut dyn DrawCtx) {
    // Widgets that defer to the global-overlay pass (modal dialogs) paint
    // nothing here — neither their own body nor their children — so an
    // ancestor's clip can't truncate them. Their `paint_global_overlay`
    // re-enters via `paint_subtree_forced` during the clip-free overlay walk.
    if widget.is_visible() && widget.defer_paint_to_overlay() {
        return;
    }
    paint_subtree_forced(widget, ctx);
}

/// Paint `widget` and its descendants ignoring [`Widget::defer_paint_to_overlay`]
/// for `widget` itself. This is the entry point a deferred widget calls from its
/// `paint_global_overlay` to render its subtree during the clip-escaping global
/// overlay pass. Deferred *descendants* (rare) still route through
/// [`paint_subtree`] and thus keep deferring.
pub(crate) fn paint_subtree_forced(widget: &mut dyn Widget, ctx: &mut dyn DrawCtx) {
    if !widget.is_visible() {
        if paint_subtree_unified_backbuffer(widget, ctx, true) {
            return;
        }
        if ctx.supports_compositing_layers() {
            if let Some(layer) = widget.compositing_layer() {
                paint_subtree_layer(widget, ctx, true, layer);
            }
        }
        return;
    }

    // Snap CTM at paint_subtree ENTRY — see the commentary preserved
    // below inside `paint_subtree_direct` for the full rationale.  The
    // backbuffer path bypasses this because the bitmap is already at
    // integer texel positions by construction.
    if paint_subtree_unified_backbuffer(widget, ctx, true) {
        return;
    } else if widget.backbuffer_cache_mut().is_some() {
        paint_subtree_backbuffered(widget, ctx);
    } else {
        paint_subtree_direct(widget, ctx);
    }
}

fn paint_subtree_unified_backbuffer(
    widget: &mut dyn Widget,
    ctx: &mut dyn DrawCtx,
    include_overlay: bool,
) -> bool {
    let spec = widget.backbuffer_spec();
    if spec.kind == BackbufferKind::None {
        return false;
    }

    match spec.kind {
        BackbufferKind::GlFbo if ctx.supports_retained_layers() => {
            paint_subtree_gl_backbuffer(widget, ctx, include_overlay, spec);
            true
        }
        BackbufferKind::SoftwareRgba | BackbufferKind::SoftwareLcd => {
            // Existing CPU widgets still use `backbuffer_cache_mut`; the
            // unified spec provides the migration point without changing their
            // current behavior.
            if widget.backbuffer_cache_mut().is_some() {
                paint_subtree_backbuffered(widget, ctx);
                true
            } else {
                false
            }
        }
        _ => false,
    }
}

fn paint_subtree_gl_backbuffer(
    widget: &mut dyn Widget,
    ctx: &mut dyn DrawCtx,
    include_overlay: bool,
    spec: BackbufferSpec,
) {
    let b = widget.bounds();
    let layer_w = (b.width + spec.outsets.left + spec.outsets.right).max(1.0);
    let layer_h = (b.height + spec.outsets.bottom + spec.outsets.top).max(1.0);
    let subtree_needs_draw = widget.needs_draw();
    let theme_epoch = crate::theme::current_visuals_epoch();
    let typography_epoch = crate::font_settings::current_typography_epoch();
    let async_state_epoch = crate::animation::async_state_epoch();
    let (key, needs_draw) = {
        let Some(state) = widget.backbuffer_state_mut() else {
            paint_subtree_direct(widget, ctx);
            return;
        };
        let w = layer_w.ceil().max(1.0) as u32;
        let h = layer_h.ceil().max(1.0) as u32;
        let changed = state.width != w || state.height != h || state.spec_kind != spec.kind;
        let style_changed = state.theme_epoch != theme_epoch
            || state.typography_epoch != typography_epoch
            || state.async_state_epoch != async_state_epoch;
        let needs = !spec.cached || state.dirty || changed || style_changed || subtree_needs_draw;
        if changed {
            state.width = w;
            state.height = h;
            state.spec_kind = spec.kind;
        }
        (state.id(), needs)
    };

    if spec.cached && !needs_draw {
        ctx.save();
        ctx.translate(-spec.outsets.left, -spec.outsets.bottom);
        let composited = ctx.composite_retained_layer(key, layer_w, layer_h, spec.alpha);
        ctx.restore();
        if composited {
            if let Some(state) = widget.backbuffer_state_mut() {
                state.composite_count = state.composite_count.saturating_add(1);
            }
            return;
        }
    }

    ctx.save();
    ctx.translate(-spec.outsets.left, -spec.outsets.bottom);
    if spec.cached {
        ctx.push_retained_layer_with_alpha(key, layer_w, layer_h, spec.alpha);
    } else {
        ctx.push_layer_with_alpha(layer_w, layer_h, spec.alpha);
    }
    ctx.translate(spec.outsets.left, spec.outsets.bottom);
    paint_subtree_direct_inner(widget, ctx, include_overlay, false);
    ctx.pop_layer();
    ctx.restore();

    if let Some(state) = widget.backbuffer_state_mut() {
        state.dirty = false;
        state.theme_epoch = theme_epoch;
        state.typography_epoch = typography_epoch;
        state.async_state_epoch = async_state_epoch;
        state.repaint_count = state.repaint_count.saturating_add(1);
        state.composite_count = state.composite_count.saturating_add(1);
    }
}

fn paint_subtree_layer(
    widget: &mut dyn Widget,
    ctx: &mut dyn DrawCtx,
    include_overlay: bool,
    layer: crate::widget::CompositingLayer,
) {
    let b = widget.bounds();
    let layer_w = (b.width + layer.outset_left + layer.outset_right).max(1.0);
    let layer_h = (b.height + layer.outset_bottom + layer.outset_top).max(1.0);

    ctx.save();
    ctx.translate(-layer.outset_left, -layer.outset_bottom);
    ctx.push_layer_with_alpha(layer_w, layer_h, layer.alpha);
    ctx.translate(layer.outset_left, layer.outset_bottom);
    paint_subtree_direct_inner(widget, ctx, include_overlay, false);
    ctx.pop_layer();
    ctx.restore();
}

/// Paint app-level overlays after the whole tree has rendered.
///
/// Traverses in paint order while preserving each widget's normal local
/// transform. Implementors can use `ctx.root_transform()` to submit app-level
/// overlay geometry without forcing retained parents to repaint.
pub fn paint_global_overlays(widget: &mut dyn Widget, ctx: &mut dyn DrawCtx) {
    if !widget.is_visible() {
        return;
    }
    let n = widget.children().len();
    for i in 0..n {
        let child = &mut widget.children_mut()[i];
        let b = child.bounds();
        ctx.save();
        ctx.translate(b.x, b.y);
        paint_global_overlays(child.as_mut(), ctx);
        ctx.restore();
    }
    widget.paint_global_overlay(ctx);
}

/// Direct (non-cached) paint: widget and its children paint onto `ctx`
/// at the current CTM.  This is the default path for widgets that don't
/// opt into backbuffer caching via `Widget::backbuffer_cache_mut`.
fn paint_subtree_direct(widget: &mut dyn Widget, ctx: &mut dyn DrawCtx) {
    paint_subtree_direct_inner(widget, ctx, true, true);
}

/// Cache-building variant: paints body + children into the given ctx
/// WITHOUT calling `paint_overlay`.  The overlay is what `TextField` uses
/// for its blinking cursor — if we baked the overlay into the cache bitmap,
/// the drawn cursor would stay visible forever on blit while a second
/// (blinking) overlay was being drawn on top of it every frame, producing
/// two cursors.  Overlay runs only on the outer ctx in
/// `paint_subtree_backbuffered` after the cache blit.
fn paint_subtree_direct_no_overlay(widget: &mut dyn Widget, ctx: &mut dyn DrawCtx) {
    paint_subtree_direct_inner(widget, ctx, false, true);
}

fn paint_subtree_direct_inner(
    widget: &mut dyn Widget,
    ctx: &mut dyn DrawCtx,
    include_overlay: bool,
    allow_compositing_layer: bool,
) {
    if allow_compositing_layer && ctx.supports_compositing_layers() {
        if let Some(layer) = widget.compositing_layer() {
            paint_subtree_layer(widget, ctx, include_overlay, layer);
            return;
        }
    }

    let snap_this = widget.enforce_integer_bounds();
    if snap_this {
        ctx.save();
        ctx.snap_to_pixel();
    }

    widget.paint(ctx);

    let b = widget.bounds();
    let (cx, cy, cw, ch) = widget
        .clip_children_rect()
        .unwrap_or((0.0, 0.0, b.width, b.height));
    ctx.save();
    ctx.clip_rect(cx, cy, cw, ch);
    let clip = root_rect_from_local(ctx, cx, cy, cw, ch);
    PAINT_CLIP_STACK.with(|stack| {
        let mut stack = stack.borrow_mut();
        let clipped = if let Some(prev) = stack.last().copied() {
            intersect_rects(prev, clip).unwrap_or_else(|| Rect::new(0.0, 0.0, 0.0, 0.0))
        } else {
            clip
        };
        stack.push(clipped);
    });

    // Apply the widget's optional child transform (pan/zoom for a Scene) to
    // the whole child group.  It goes on AFTER the children clip — the clip
    // stays axis-aligned in this widget's screen-local space while the
    // children paint under the scaled/translated frame.  The transform is
    // popped by the `ctx.restore()` that lifts the children clip below, so
    // per-child `bounds()` offsets are interpreted inside the transform.
    if let Some(t) = widget.child_transform() {
        let mut m = ctx.transform();
        m.premultiply(&t);
        ctx.set_transform(m);
    }

    let n = widget.children().len();
    for i in 0..n {
        let child_bounds = widget.children()[i].bounds();
        let snap_to_pixel = widget.children()[i].enforce_integer_bounds();
        ctx.save();
        if snap_to_pixel {
            ctx.translate(child_bounds.x.round(), child_bounds.y.round());
        } else {
            ctx.translate(child_bounds.x, child_bounds.y);
        }
        let child = &mut widget.children_mut()[i];
        paint_subtree(child.as_mut(), ctx);
        ctx.restore();
    }

    PAINT_CLIP_STACK.with(|stack| {
        stack.borrow_mut().pop();
    });
    ctx.restore(); // lifts the children clip before paint_overlay
    if include_overlay {
        widget.paint_overlay(ctx);
    }
    widget.finish_paint(ctx);

    if snap_this {
        ctx.restore();
    }
}

fn root_rect_from_local(ctx: &dyn DrawCtx, x: f64, y: f64, w: f64, h: f64) -> Rect {
    let mut points = [(x, y), (x + w, y), (x, y + h), (x + w, y + h)];
    let transform = ctx.root_transform();
    for (px, py) in &mut points {
        transform.transform(px, py);
    }
    let min_x = points.iter().map(|(x, _)| *x).fold(f64::INFINITY, f64::min);
    let max_x = points
        .iter()
        .map(|(x, _)| *x)
        .fold(f64::NEG_INFINITY, f64::max);
    let min_y = points.iter().map(|(_, y)| *y).fold(f64::INFINITY, f64::min);
    let max_y = points
        .iter()
        .map(|(_, y)| *y)
        .fold(f64::NEG_INFINITY, f64::max);
    Rect::new(
        min_x,
        min_y,
        (max_x - min_x).max(0.0),
        (max_y - min_y).max(0.0),
    )
}

fn intersect_rects(a: Rect, b: Rect) -> Option<Rect> {
    let x0 = a.x.max(b.x);
    let y0 = a.y.max(b.y);
    let x1 = (a.x + a.width).min(b.x + b.width);
    let y1 = (a.y + a.height).min(b.y + b.height);
    (x1 >= x0 && y1 >= y0).then(|| Rect::new(x0, y0, x1 - x0, y1 - y0))
}

/// Backbuffered paint: re-raster through AGG if dirty, blit the cached
/// bitmap via `draw_image_rgba_arc` regardless.
///
/// # HiDPI
///
/// The backing bitmap is allocated at **physical pixel** dimensions
/// (`bounds × device_scale`) and the sub-ctx running the widget's paint has
/// a matching `scale(dps, dps)` applied.  This means glyph outlines are
/// rasterised at the physical grid — "true" HiDPI rendering, not pixel
/// doubling — and the outer blit then draws the physical-sized image at the
/// widget's logical rect, which the outer CTM (also scaled by dps) maps 1:1
/// back to physical pixels.  Net: logical layout, physical rasterisation,
/// zero upscale blur.
fn paint_subtree_backbuffered(widget: &mut dyn Widget, ctx: &mut dyn DrawCtx) {
    // Snap the outer CTM to the pixel grid BEFORE blitting the cached
    // bitmap.  `draw_image_rgba_arc` uses a NEAREST filter for Arc-keyed
    // textures (1:1 blit lane), so a fractional CTM translation shifts
    // every screen pixel by a sub-texel amount — reading back interpolated
    // near-black/near-white instead of the crisp AGG output.  Snapping
    // here restores the "AGG rasterised it, show it at the pixel grid"
    // contract the old pre-refactor code preserved.
    ctx.save();
    ctx.snap_to_pixel();

    // TEMPORARY env-gated per-section timing (AGG_PAINT_TIMING). See
    // `paint_timing.rs`. Cheap when disabled: every `pt::start()`/`pt::ms()`
    // reduces to a cached bool load and the accumulators stay untouched.
    use super::paint_timing as pt;
    let timing = pt::enabled();
    let mut tm = pt::PaintTiming::default();

    let b = widget.bounds();
    // Rasterise at the CURRENT CTM scale, not the bare device-pixel ratio.
    // The on-screen footprint of this widget is `bounds × ctm_scale`, where
    // `ctm_scale = device_scale × ux_scale` at the top level (and may be just
    // `device_scale` inside an offscreen layer that reset its transform).
    // Sizing the offscreen bitmap to `device_scale` only — as this code used
    // to — left the cached bitmap at `1/ux_scale` of its destination quad, so
    // on mobile (ux_scale ≈ 1.7) every CPU-backbuffered widget (the menu bar,
    // Labels) rendered shrunken inside its layout slot while sibling
    // GL-FBO widgets (Windows), which allocate their layer via
    // `layer_scale_from_transform`, scaled correctly.  Matching the CTM scale
    // here puts both paths on the same footing and gives a true 1:1 blit.
    let (sx, sy) = ctx.transform().scaling_abs();
    let dps_x = sx.max(1e-6);
    let dps_y = sy.max(1e-6);

    // Over-scan band (scrolling widgets only — see `Widget::backbuffer_band`).
    // The band raster covers the viewport plus `overscan_*` extra logical px
    // above/below, so ordinary scrolling within the band is a pure blit offset.
    // Quantize the extents and the blit offset to whole PHYSICAL pixels so the
    // LCD subpixel structure is never resampled by a fractional shift.
    let band = widget.backbuffer_band();
    let (over_top_phys, over_bottom_phys, blit_dy_phys) = match band {
        Some(bd) => (
            (bd.overscan_top.max(0.0) * dps_y).round() as u32,
            (bd.overscan_bottom.max(0.0) * dps_y).round() as u32,
            (bd.blit_dy * dps_y).round(),
        ),
        None => (0, 0, 0.0),
    };

    // Physical pixel dimensions of the offscreen render target. The band grows
    // only vertically (text scrolls vertically); width is unaffected.
    let w_phys = (b.width * dps_x).ceil().max(1.0) as u32;
    let base_h_phys = (b.height * dps_y).ceil().max(1.0) as u32;
    let h_phys = base_h_phys + over_top_phys + over_bottom_phys;
    // Logical translate that places the widget's own origin above the
    // bottom over-scan margin inside the taller buffer (physical-aligned).
    let over_bottom_logical = over_bottom_phys as f64 / dps_y;
    // Logical dimensions used as the blit destination rect.  **Must** be
    // derived from `w_phys / dps` rather than `b.width` so the quad the
    // bitmap is drawn into matches the bitmap's actual pixel extent.  If
    // `b.width` is non-integer (e.g. 19.5 for a sidebar Label), using
    // it as `dst_w` stretches a 20-pixel bitmap into a 19.5-pixel quad —
    // sub-pixel shrink that drops partial-coverage rows at the edges,
    // which reads as a faint fade along the top / bottom of the glyph.
    // Pre-HiDPI the blit used the bitmap's integer pixel size directly;
    // this restores that contract for the logical-units pipeline.
    let w_logical = w_phys as f64 / dps_x;
    let h_logical = h_phys as f64 / dps_y;

    // Decide whether to re-raster.  Size change invalidates; so does a
    // mode swap — if the cache holds `Rgba` bytes but the widget now
    // wants `LcdCoverage` (or vice versa) we must re-raster through the
    // correct pipeline.  Mode membership is recorded implicitly by
    // `cache.lcd_alpha`: `Some` means LCD cache, `None` means Rgba.
    let mode = widget.backbuffer_mode();
    let mode_is_lcd = matches!(mode, BackbufferMode::LcdCoverage);
    let theme_epoch = crate::theme::current_visuals_epoch();
    let typography_epoch = crate::font_settings::current_typography_epoch();
    let async_state_epoch = crate::animation::async_state_epoch();
    let (needs_raster, has_bitmap) = {
        let cache = widget
            .backbuffer_cache_mut()
            .expect("backbuffered widget must return Some from backbuffer_cache_mut");
        let cache_is_lcd = cache.lcd_alpha.is_some();
        let needs = cache.dirty
            || cache.pixels.is_none()
            || cache.width != w_phys
            || cache.height != h_phys
            || cache_is_lcd != mode_is_lcd
            || cache.theme_epoch != theme_epoch
            || cache.typography_epoch != typography_epoch
            || cache.async_state_epoch != async_state_epoch;
        (needs, cache.pixels.is_some())
    };

    if needs_raster {
        // Allocate a fresh render target whose format matches the
        // widget's chosen backbuffer mode, paint the subtree into it,
        // then convert to top-down RGBA for the cache (the blit lane
        // expects `(R, G, B, A)` rows top-first).
        //
        // `LcdCoverage` mode now uses an `LcdGfxCtx` over an `LcdBuffer`
        // — every primitive (fill, stroke, text, image) flows through
        // the per-channel LCD pipeline, so child widgets that paint
        // into this widget's backbuffer compose correctly with
        // LCD-treated text instead of breaking the per-channel
        // coverage at the first non-text fill (the alpha bug the
        // search-box screenshot showed before this change).
        // Each branch produces `(pixels, lcd_alpha)` top-down:
        //   - `Rgba`: `pixels` = straight-alpha RGBA8; `lcd_alpha` = None.
        //   - `LcdCoverage`: `pixels` = premultiplied colour plane (3 B/px);
        //     `lcd_alpha` = per-channel alpha plane (3 B/px).  The blit
        //     step below picks a compositor based on which is present.
        //
        // Over-scan band partial re-raster: if the retained LCD buffer is still
        // valid (same size / mode / styling epochs and a live cached bitmap),
        // reuse it so a band widget can repaint ONLY its dirty line strip and
        // keep every other row of the (up to 2×-viewport) buffer intact —
        // turning a keystroke from a full re-raster into a small strip fill +
        // one buffer-sized flip. `partial_allowed` grants the widget that
        // permission; a full repaint (first raster, resize, re-anchor, theme
        // flip) leaves it false and rebuilds the whole band. Confined to the
        // band + LCD path, so no other widget pays for the retained buffer.
        let partial_reuse = band.is_some()
            && mode_is_lcd
            && {
                let cache = widget.backbuffer_cache_mut().unwrap();
                cache.pixels.is_some()
                    && cache.theme_epoch == theme_epoch
                    && cache.typography_epoch == typography_epoch
                    && cache.async_state_epoch == async_state_epoch
                    && cache
                        .lcd_buffer
                        .as_ref()
                        .map_or(false, |b| b.width() == w_phys && b.height() == h_phys)
            };
        {
            widget.backbuffer_cache_mut().unwrap().partial_allowed = partial_reuse;
        }
        let mut reused_buf = if partial_reuse {
            widget.backbuffer_cache_mut().unwrap().lcd_buffer.take()
        } else {
            None
        };

        // Is this a *granted, confined* strip update of the retained top-down
        // planes rather than a full rebuild? Three things must line up:
        //   1. the framework granted the partial (`partial_reuse`);
        //   2. the widget planned a confined strip (`band.dirty_strip_y` Some —
        //      it repaints ONLY those rows, matching the fill+clip in its paint);
        //   3. the retained published planes are actually reusable (both Some and
        //      full-size), since we mutate them in place through `Arc::make_mut`.
        // Any miss → the full path below (fresh flipped planes + new Arcs). The
        // widget's `partial_allowed`-gated strip repaint stays in lockstep with
        // this decision because both read the same `render_dirty_lines`.
        let plane_len = (w_phys as usize) * (h_phys as usize) * 3;
        let strip_rows: Option<(f64, f64)> = if partial_reuse {
            let cache = widget.backbuffer_cache_mut().unwrap();
            let planes_ok = cache.pixels.as_ref().map_or(false, |p| p.len() == plane_len)
                && cache.lcd_alpha.as_ref().map_or(false, |a| a.len() == plane_len);
            if planes_ok {
                band.and_then(|b| b.dirty_strip_y)
            } else {
                None
            }
        } else {
            None
        };

        // Strip fast path (LCD band only): paint the edited line strip into the
        // reused buffer, then copy ONLY those rows of the flipped planes into the
        // retained Arcs in place. `Arc::make_mut` keeps the byte-buffer address
        // stable (a Vec move preserves its heap pointer even when outstanding
        // Weaks force a new Arc), so GPU backends key change detection on
        // `content_version` — bumped here — rather than pointer identity. No
        // whole-buffer flip, no fresh plane allocation: the win this step buys.
        let strip_done = if let (BackbufferMode::LcdCoverage, Some((lo, hi))) = (mode, strip_rows) {
            let mut buf = reused_buf
                .take()
                .expect("strip path requires the retained band buffer");
            {
                let t_setup = pt::start();
                let mut sub = crate::lcd_gfx_ctx::LcdGfxCtx::new(&mut buf);
                if (dps_x - 1.0).abs() > 1e-6 || (dps_y - 1.0).abs() > 1e-6 {
                    sub.scale(dps_x, dps_y);
                }
                if over_bottom_logical != 0.0 {
                    sub.translate(0.0, over_bottom_logical);
                }
                tm.lcd_ctx_setup_ms += pt::ms(&t_setup);
                let t_paint = pt::start();
                paint_subtree_direct_no_overlay(widget, &mut sub);
                tm.widget_paint_ms += pt::ms(&t_paint);
            }
            // Map the widget-local Y-up logical strip (lo, hi) to top-down
            // physical rows. The sub ctx maps logical y → Y-up physical
            // `y * dps_y + over_bottom_phys`; the top-down row of a Y-up
            // physical row `y_phys` is `h_phys - 1 - y_phys`. ±2 rows of slack
            // absorb AA bleed at the strip rect's top/bottom edges.
            let y_lo_phys = lo * dps_y + over_bottom_phys as f64;
            let y_hi_phys = hi * dps_y + over_bottom_phys as f64;
            let row_start =
                ((h_phys as f64 - y_hi_phys).floor() as i64 - 2).clamp(0, h_phys as i64) as u32;
            let row_end =
                ((h_phys as f64 - y_lo_phys).ceil() as i64 + 2).clamp(0, h_phys as i64) as u32;
            let cache = widget.backbuffer_cache_mut().unwrap();
            {
                // Disjoint field borrows: `pixels` and `lcd_alpha` are distinct
                // Arcs, both guaranteed Some + full-size by the `strip_rows`
                // preconditions above.
                let t_mkmut = pt::start();
                let dst_color = Arc::make_mut(cache.pixels.as_mut().unwrap());
                let dst_alpha = Arc::make_mut(cache.lcd_alpha.as_mut().unwrap());
                tm.make_mut_ms += pt::ms(&t_mkmut);
                let t_copy = pt::start();
                buf.copy_rows_flipped_into(row_start, row_end, dst_color, dst_alpha);
                tm.rowcopy_ms += pt::ms(&t_copy);
            }
            tm.plane_update_ms = tm.make_mut_ms + tm.rowcopy_ms;
            tm.is_strip = true;
            tm.strip_rows = Some((row_start, row_end));
            cache.width = w_phys;
            cache.height = h_phys;
            cache.dirty = false;
            cache.theme_epoch = theme_epoch;
            cache.typography_epoch = typography_epoch;
            cache.async_state_epoch = async_state_epoch;
            cache.content_version = next_content_version();
            cache.lcd_buffer = Some(buf);
            true
        } else {
            false
        };

        // Buffer to hand back to the cache after this raster (band path only, so
        // non-band widgets keep dropping their scratch buffer as before).
        let mut retained_buf: Option<LcdBuffer> = None;
        if !strip_done {
            let (pixels_bytes, lcd_alpha_bytes): (Vec<u8>, Option<Vec<u8>>) = match mode {
                BackbufferMode::Rgba => {
                    let mut fb = Framebuffer::new(w_phys, h_phys);
                    {
                        let mut sub = GfxCtx::new(&mut fb);
                        sub.set_lcd_mode(false); // RGBA mode never uses LCD text
                        if (dps_x - 1.0).abs() > 1e-6 || (dps_y - 1.0).abs() > 1e-6 {
                            // Widgets paint in logical coords — scale the sub ctx
                            // so their drawing lands on the physical pixel grid.
                            sub.scale(dps_x, dps_y);
                        }
                        // Band: reserve the bottom over-scan margin so the widget's
                        // own origin sits above it inside the taller buffer.
                        if over_bottom_logical != 0.0 {
                            sub.translate(0.0, over_bottom_logical);
                        }
                        paint_subtree_direct_no_overlay(widget, &mut sub);
                    }
                    // Two conversions to make the bitmap directly blittable:
                    //   1. Row order — Framebuffer is Y-up, blit lane is top-down.
                    //   2. Alpha format — AGG writes premultiplied; the blend
                    //      function expects straight alpha so that half-coverage
                    //      AA edges composite without the dark-fringe artifact.
                    let mut pixels = fb.pixels_flipped();
                    crate::framebuffer::unpremultiply_rgba_inplace(&mut pixels);
                    (pixels, None)
                }
                BackbufferMode::LcdCoverage => {
                    // The LCD pipeline is strictly WRITE-only.  The buffer
                    // starts at zero coverage everywhere; the widget paints
                    // opaque content covering its full bounds (the contract
                    // for this mode) into it via an `LcdGfxCtx`; then the
                    // two planes (premultiplied colour + per-channel alpha)
                    // are cached and composited onto the destination at
                    // blit time via `draw_lcd_backbuffer_arc` — which
                    // preserves LCD per-channel chroma through the cache.
                    //
                    // We deliberately do NOT read from any destination —
                    // seeding the buffer from the parent's pixels would
                    // tie the cache's validity to the widget's current
                    // screen position (stale on scroll / reparent), stall
                    // the GPU pipeline on GL (glReadPixels is sync), and
                    // break on backends that can't read their own target.
                    // Widgets that can't paint their own opaque bg should
                    // use `Rgba` mode or paint through the parent's ctx
                    // directly instead.
                    // Reuse the retained band buffer when the widget is doing a
                    // strip-only repaint; otherwise start from a fresh (zeroed)
                    // buffer for a full rebuild.
                    let t_setup = pt::start();
                    let mut buf = reused_buf.unwrap_or_else(|| LcdBuffer::new(w_phys, h_phys));
                    {
                        let mut sub = crate::lcd_gfx_ctx::LcdGfxCtx::new(&mut buf);
                        if (dps_x - 1.0).abs() > 1e-6 || (dps_y - 1.0).abs() > 1e-6 {
                            // Match the RGBA branch: widgets paint in logical
                            // coords; the sub ctx's scale transforms them into
                            // the physical-pixel LCD buffer.
                            sub.scale(dps_x, dps_y);
                        }
                        // Band: reserve the bottom over-scan margin (see RGBA branch).
                        if over_bottom_logical != 0.0 {
                            sub.translate(0.0, over_bottom_logical);
                        }
                        tm.lcd_ctx_setup_ms += pt::ms(&t_setup);
                        let t_paint = pt::start();
                        paint_subtree_direct_no_overlay(widget, &mut sub);
                        tm.widget_paint_ms += pt::ms(&t_paint);
                    }
                    let t_plane = pt::start();
                    let planes = (buf.color_plane_flipped(), Some(buf.alpha_plane_flipped()));
                    tm.plane_update_ms += pt::ms(&t_plane);
                    // Keep the buffer alive for the next partial re-raster (band
                    // path only). Non-band LCD widgets let it drop.
                    if band.is_some() {
                        retained_buf = Some(buf);
                    }
                    planes
                }
            };
            let t_arc = pt::start();
            let pixels = Arc::new(pixels_bytes);
            let lcd_alpha = lcd_alpha_bytes.map(Arc::new);
            tm.plane_update_ms += pt::ms(&t_arc);

            let cache = widget.backbuffer_cache_mut().unwrap();
            cache.pixels = Some(Arc::clone(&pixels));
            cache.lcd_alpha = lcd_alpha.as_ref().map(Arc::clone);
            cache.width = w_phys;
            cache.height = h_phys;
            cache.dirty = false;
            cache.theme_epoch = theme_epoch;
            cache.typography_epoch = typography_epoch;
            cache.async_state_epoch = async_state_epoch;
            // Full rebuild replaced the whole plane content (fresh Arcs), so
            // stamp a new revision uniformly with the strip path — backends
            // re-upload on any published-content change either way.
            cache.content_version = next_content_version();
            // Retain (band) or drop (everyone else) the scratch LCD buffer.
            cache.lcd_buffer = retained_buf;
        }
    }

    tm.did_raster = needs_raster;
    tm.h_phys = h_phys;

    // Blit the cached bitmap onto the outer ctx.  Two paths:
    //
    //   - `Rgba` cache (no `lcd_alpha`): a single RGBA8 texture via the
    //     standard image-blit lane.  Alpha-aware SrcOver at the blend
    //     stage handles transparency.
    //
    //   - `LcdCoverage` cache (`lcd_alpha` is `Some`): two 3-byte/pixel
    //     planes — premultiplied colour + per-channel alpha.  The
    //     backend's `draw_lcd_backbuffer_arc` composites them with
    //     per-channel src-over, preserving LCD chroma through the
    //     cache round-trip (grayscale AA on backends that fall back
    //     to the default trait impl).
    let cache = widget.backbuffer_cache_mut().unwrap();
    // Image is physical-sized; dst is logical.  The bitmap was rasterised at
    // the current CTM scale (`dps_x`/`dps_y`), and the outer CTM applies that
    // same scale to the logical dst rect, so logical dst × ctm_scale ==
    // physical dst == bitmap size, giving a 1:1 texel-to-pixel blit (no
    // up/downscale blur).
    let img_w = cache.width;
    let img_h = cache.height;
    // Content revision for backends keying a GPU texture on buffer identity: an
    // in-place strip edit keeps the plane pointer stable, so the version is what
    // signals "re-upload" (see `draw_lcd_backbuffer_arc`).
    let content_version = cache.content_version;
    if band.is_some() {
        // Band blit: shift the taller buffer by the (physical-quantized) scroll
        // residual minus the bottom over-scan margin, then clip to the widget's
        // bounds so the over-scan margins never paint over sibling widgets. The
        // shift goes through `ctx.translate` (not the `dst_y` arg) so it scales
        // with the CTM and lands on physical pixels on every backend. `dst_h`
        // stays the full buffer height, keeping the blit a 1:1 (unscaled) copy.
        let shift_logical = (blit_dy_phys - over_bottom_phys as f64) / dps_y;
        ctx.save();
        ctx.clip_rect(0.0, 0.0, b.width, b.height);
        ctx.translate(0.0, shift_logical);
        let t_blit = pt::start();
        match (cache.pixels.as_ref(), cache.lcd_alpha.as_ref()) {
            (Some(color), Some(alpha)) => {
                ctx.draw_lcd_backbuffer_arc(color, alpha, content_version, img_w, img_h, 0.0, 0.0, w_logical, h_logical);
            }
            (Some(bmp), None) => {
                ctx.draw_image_rgba_arc(bmp, img_w, img_h, 0.0, 0.0, w_logical, h_logical);
            }
            _ => {}
        }
        tm.blit_ms += pt::ms(&t_blit);
        ctx.restore();
    } else {
        let t_blit = pt::start();
        match (cache.pixels.as_ref(), cache.lcd_alpha.as_ref()) {
            (Some(color), Some(alpha)) => {
                ctx.draw_lcd_backbuffer_arc(color, alpha, content_version, img_w, img_h, 0.0, 0.0, w_logical, h_logical);
            }
            (Some(bmp), None) => {
                ctx.draw_image_rgba_arc(bmp, img_w, img_h, 0.0, 0.0, w_logical, h_logical);
            }
            _ => {}
        }
        tm.blit_ms += pt::ms(&t_blit);
    }
    let _ = has_bitmap;

    // Overlay paint runs AFTER the cache blit and paints directly onto
    // the outer ctx.  Widgets use this for content that changes too
    // often to be worth caching — the canonical case is `TextField`'s
    // blinking cursor, which flips twice per second and would otherwise
    // invalidate the cache 2×/s.  With overlay, cursor is drawn fresh
    // each frame onto the already-blitted bg+text; the cache only
    // invalidates when the text/focus/selection actually changes.
    //
    // `paint_subtree_direct` has the same overlay call after children
    // (see its own body); this keeps the two paint paths consistent.
    let t_overlay = pt::start();
    widget.paint_overlay(ctx);
    tm.overlay_ms += pt::ms(&t_overlay);

    if timing {
        tm.emit();
    }

    ctx.restore(); // pops the snap_to_pixel save above.
}