lurq 0.65.1

A Rust UI toolkit with typed components, retained state, reactive signals, GPU rendering, and DevTools.
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
//! Persistent 2D drawing through an existing element ref.
//!
//! Enable `canvas`, attach a [`crate::core::ElementRef`] to a
//! [`crate::components::Canvas`], and call `as_canvas()` after layout. Contexts
//! are owned handles: clones share state and drawing does not rebuild the UI.
//! Drawing is queued into persistent GPU textures. Presentation is coalesced
//! through the host event-loop waker. Readbacks are explicit and asynchronous.

mod handle;
use handle::{ResolvedPaint, SoftwareLayer};
mod presentation;
mod presentation_budget;
pub use presentation::MAX_PRESENTATION_BYTES;
pub(crate) use presentation::PresentationEvent;
pub(crate) use presentation_budget::{TargetCharge, replacement_admitted};
mod software;
pub use software::CanvasColor;
use software::rescale_clip;
pub(crate) use software::transform;
mod asset_cache;
mod blend;
mod context;
mod effect;
#[cfg(any(test, feature = "wgpu", all(feature = "dx12", target_os = "windows")))]
mod frame_boundary;
mod frame_cache;
pub(crate) mod gpu;
mod items;
#[cfg(any(feature = "wgpu", all(feature = "dx12", target_os = "windows")))]
pub(crate) use asset_cache::AssetCache;
pub use asset_cache::CanvasAssetBudget;
pub use blend::BlendMode;
pub use effect::{Filter, MAX_BLUR_RADIUS, MAX_EFFECT_PIXELS, MAX_SHADOW_BLUR, MAX_SHADOW_SPREAD, Shadow};
#[cfg(any(feature = "wgpu", all(feature = "dx12", target_os = "windows")))]
pub(crate) use frame_boundary::FrameBoundary;
pub use gpu::{CanvasReadback, MAX_LAYER_DEPTH};
pub use items::{CanvasItem, CanvasItemShape};

mod paint;
mod path;
#[cfg(all(test, any(feature = "wgpu", all(feature = "dx12", target_os = "windows"))))]
pub(crate) mod test_pages;
mod text;
use std::{
  fmt,
  sync::{
    Arc, Weak,
    atomic::{AtomicU64, Ordering},
  },
};

pub use paint::{CanvasPaint, Gradient, GradientKind, MAX_GRADIENT_STOPS, MIN_GRADIENT_STOPS, Paint, RAMP_TEXELS};
use parking_lot::Mutex;
pub use path::{ArcDirection, Path2D};
pub(crate) use text::CanvasTextEngine;
pub use text::{CanvasFont, TextAlign, TextBaseline, TextMetrics};
pub use tiny_skia::{LineCap, LineJoin};
use tiny_skia::{Mask, Paint as SkiaPaint, Path, Pixmap, PixmapPaint, Point, Stroke, StrokeDash};

use crate::{
  app::window::Window,
  images::{ImageData, ImagePixelFormat, NativeImageBackend, NativeImageData, StreamingImage},
  layout::Size,
  node::{color::Color, transform::Transform2D},
};

const MAX_PIXELS: u64 = 16_777_216;
const MAX_SAVE_DEPTH: usize = 128;
const MAX_CLIP_BYTES: usize = 64 * 1024 * 1024;
static NEXT_CANVAS_ID: AtomicU64 = AtomicU64::new(1);

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum FillRule {
  #[default]
  NonZero,
  EvenOdd,
}
impl FillRule {
  fn skia(self) -> tiny_skia::FillRule {
    match self {
      Self::NonZero => tiny_skia::FillRule::Winding,
      Self::EvenOdd => tiny_skia::FillRule::EvenOdd,
    }
  }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct CanvasId(u64);

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CanvasError {
  Detached,
  QueueFull,
  UnsupportedBackend,
  RendererLost,
  InvalidGeometry,
  InvalidImage,
  UnsupportedImage,
  SurfaceTooLarge,
  PresentationBusy,
  StateLimit,
  TextUnavailable,
  TextTooLarge,
  UnsupportedPaint,
  UnbalancedLayer,
}
impl fmt::Display for CanvasError {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.write_str(match self {
      Self::Detached => "canvas is detached",
      Self::QueueFull => "canvas pending-work budget exceeded",
      Self::UnsupportedBackend => "renderer does not support GPU canvas; select software explicitly",
      Self::RendererLost => "canvas renderer or readback was released",
      Self::InvalidGeometry => "invalid canvas geometry",
      Self::InvalidImage => "invalid or empty image source",
      Self::UnsupportedImage => "canvas accepts immutable RGBA images only",
      Self::PresentationBusy => "canvas presentation targets are still in flight",
      Self::SurfaceTooLarge => "canvas backing storage exceeds its allocation limit",
      Self::StateLimit => "canvas path, clip, or saved-state limit exceeded",
      Self::TextUnavailable => "canvas text service is not attached yet",
      Self::TextTooLarge => "canvas text exceeds its rasterization limit",
      Self::UnsupportedPaint => "this canvas operation accepts a solid colour only",
      Self::UnbalancedLayer => "canvas layer was ended without being begun",
    })
  }
}
impl std::error::Error for CanvasError {}

/// What a painter draws from, as the engine keeps it between drawings: the value a moving drawing goes to, and
/// the one it comes from.
pub(crate) type MotionValue = std::sync::Arc<dyn std::any::Any + Send + Sync>;

/// Where a drawing is as a painter draws it: what it moves from (none when it enters or is at rest) and to, and how
/// far along the way, eased, 1 at rest.
pub(crate) struct PaintFrame<'a> {
  pub(crate) from: Option<&'a MotionValue>,
  pub(crate) to: Option<&'a MotionValue>,
  pub(crate) progress: f32,
}

pub(crate) type PaintFn = std::sync::Arc<dyn Fn(&CanvasHandle, &PaintFrame<'_>) + Send + Sync>;

/// What draws a canvas from what it is given: called with the canvas when it is first laid out, when its size or
/// its scale changes, and when its key changes, and not otherwise; and, for a moving one, at each frame while it
/// moves. See `Canvas::paint` and `Canvas::paint_moving`.
#[derive(Clone)]
pub struct CanvasPainter {
  pub(crate) key: u64,
  pub(crate) paint: PaintFn,
  pub(crate) motion: Option<PainterMotion>,
}

/// What a moving painter draws toward: `value`, which `of` (its hash) tells apart from what it drew before.
#[derive(Clone)]
pub(crate) struct PainterMotion {
  pub(crate) of: u64,
  pub(crate) value: MotionValue,
  pub(crate) motion: CanvasMotion,
}

impl CanvasPainter {
  pub fn new(key: u64, paint: impl Fn(&CanvasHandle) + Send + Sync + 'static) -> Self {
    Self {
      key,
      paint: std::sync::Arc::new(move |canvas, _| paint(canvas)),
      motion: None,
    }
  }

  /// A painter whose drawing moves to `value` when `of` changes, and enters when it is first drawn: see
  /// `Canvas::paint_moving`.
  pub fn moving<T: Send + Sync + 'static>(
    key: u64,
    motion: CanvasMotion,
    of: u64,
    value: std::sync::Arc<T>,
    paint: impl Fn(&CanvasHandle, CanvasMove<'_, T>) + Send + Sync + 'static,
  ) -> Self {
    let fallback = value.clone();
    let paint = move |canvas: &CanvasHandle, frame: &PaintFrame<'_>| {
      let to = frame
        .to
        .and_then(|value| value.downcast_ref::<T>())
        .unwrap_or(&*fallback);
      let from = frame.from.and_then(|value| value.downcast_ref::<T>());
      paint(
        canvas,
        CanvasMove {
          from,
          to,
          progress: frame.progress,
        },
      )
    };
    Self {
      key,
      paint: std::sync::Arc::new(paint),
      motion: Some(PainterMotion { of, value, motion }),
    }
  }
}

impl std::fmt::Debug for CanvasPainter {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("CanvasPainter")
      .field("key", &self.key)
      .field("moving", &self.motion.is_some())
      .finish()
  }
}

/// How a drawing that follows from a key moves to what it is given next: how long it takes, and how it is eased.
/// See `Canvas::paint_moving`. A short ease out by default, of 300 ms.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CanvasMotion {
  pub duration: std::time::Duration,
  pub easing: crate::animation::Easing,
}

impl CanvasMotion {
  pub const fn new(duration: std::time::Duration) -> Self {
    Self {
      duration,
      easing: crate::animation::Easing::EASE_OUT,
    }
  }

  pub const fn easing(mut self, easing: crate::animation::Easing) -> Self {
    self.easing = easing;
    self
  }
}

impl Default for CanvasMotion {
  fn default() -> Self {
    Self::new(std::time::Duration::from_millis(300))
  }
}

/// Where a moving drawing is, as its painter is given it: `to` is what it is drawn from now, `from` what it was
/// drawn from before (none as it enters, and at rest), and `progress` how far along the way it is, eased, from 0
/// to 1. At rest `progress` is 1 and `from` none: the painter draws `to` as it is.
#[derive(Clone, Copy, Debug)]
pub struct CanvasMove<'a, T> {
  pub from: Option<&'a T>,
  pub to: &'a T,
  pub progress: f32,
}

impl<T> CanvasMove<'_, T> {
  /// Whether the drawing is at rest.
  pub fn at_rest(&self) -> bool {
    self.progress >= 1.0
  }

  /// Whether the drawing is entering: first shown, and still on its way in.
  pub fn entering(&self) -> bool {
    self.from.is_none() && !self.at_rest()
  }
}

/// A drawing on its way to what it was given last: what it comes from, when it started, and how it moves.
pub(crate) struct Run {
  pub(crate) from: Option<MotionValue>,
  pub(crate) started: crate::time::Instant,
  pub(crate) motion: CanvasMotion,
  pub(crate) paint: PaintFn,
}

impl Run {
  /// How far along the way it is at `now`, eased: 1 at its end.
  pub(crate) fn progress(&self, now: crate::time::Instant) -> f32 {
    let duration = self.motion.duration.as_secs_f64();
    if duration <= 0.0 {
      return 1.0;
    }
    let t = (now.saturating_duration_since(self.started).as_secs_f64() / duration).clamp(0.0, 1.0);
    if t >= 1.0 {
      return 1.0;
    }
    (self.motion.easing.evaluate(t) as f32).clamp(0.0, 1.0)
  }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CanvasMetrics {
  pub size: Size,
  pub pixel_width: u32,
  pub pixel_height: u32,
  pub scale_factor: f32,
  /// Changes on first readiness, logical resize, or display-scale change.
  pub revision: u64,
}

#[derive(Clone, Debug, PartialEq)]
pub struct CanvasStatus {
  pub attached: bool,
  pub metrics: CanvasMetrics,
  pub content_revision: u64,
  pub error: Option<CanvasError>,
  /// Conservative memory charge for queued and encoding work, including retained sources.
  pub pending_bytes: usize,
  /// Persistent GPU backing bytes; excludes the renderer's shared tile scratch.
  pub gpu_bytes: usize,
  pub software: bool,
  pub gpu: CanvasGpuStats,
}

/// Cumulative work submitted for this surface. Source uploads exclude explicit
/// readbacks; drawing solid paths never uploads a canvas bitmap.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct CanvasGpuStats {
  /// Backend submission evidence, not physical scanout acknowledgement.
  pub presentation_workspace_bytes: usize,
  pub presentation_commits: u64,
  pub presentation_token: u64,
  pub fallback_quads: u64,
  pub presentation_allocations: u64,
  pub presentation_reuses: u64,
  pub presentation_refusals: u64,

  pub batches: u64,
  pub vertices: u64,
  pub tiles: u64,
  pub uploaded_bytes: u64,
  /// Cumulative CPU mesh-cache work while preparing this canvas, including
  /// attempted batches that later fail to submit.
  pub mesh_cache_hits: u64,
  pub mesh_cache_misses: u64,
  pub mesh_cache_evictions: u64,
  /// Shared renderer cache occupancy at this canvas's last preparation.
  /// Bytes include source geometry, triangle positions and estimated metadata.
  pub mesh_cache_entries: usize,
  pub mesh_cache_bytes: usize,
}

/// Straight-alpha sRGB RGBA8 pixels returned by an explicit readback.
#[derive(Clone, Debug)]
pub struct CanvasSnapshot {
  pub width: u32,
  pub height: u32,
  pub rgba: Vec<u8>,
  pub revision: u64,
}

type MetricsCallback = dyn Fn(CanvasMetrics) + Send + Sync;

/// Keep this value alive to receive metrics changes. Dropping it unsubscribes.
/// The surface holds only a weak callback, so capturing the canvas is safe.
pub struct CanvasObserver {
  _callback: Arc<MetricsCallback>,
}

#[derive(Clone)]
pub struct CanvasHandle {
  inner: Arc<Mutex<Surface>>,
}

#[derive(Clone)]
pub(crate) struct CanvasWeak {
  inner: Weak<Mutex<Surface>>,
}

impl CanvasWeak {
  pub(crate) fn upgrade(&self) -> Option<CanvasHandle> {
    self.inner.upgrade().map(|inner| CanvasHandle { inner })
  }
}

#[derive(Clone)]
pub struct Context2D {
  canvas: CanvasHandle,
}

#[derive(Clone)]
struct DrawingState {
  fill: Paint,
  stroke_paint: Paint,
  alpha: f32,
  blend: BlendMode,
  shadow: Option<Shadow>,
  filter: Filter,
  transform: Transform2D,
  stroke: Stroke,
  dash: Vec<f32>,
  dash_offset: f32,
  clip: Option<Arc<Mask>>,
  gpu_clip: Option<Arc<gpu::Clip>>,
  font: CanvasFont,
  align: TextAlign,
  baseline: TextBaseline,
  smoothing: bool,
}

impl Default for DrawingState {
  fn default() -> Self {
    Self {
      fill: Paint::Solid(Color::new(0, 0, 0, 255)),
      stroke_paint: Paint::Solid(Color::new(0, 0, 0, 255)),
      alpha: 1.0,
      blend: BlendMode::Normal,
      shadow: None,
      filter: Filter::None,
      transform: Transform2D::IDENTITY,
      stroke: Stroke {
        miter_limit: 10.0,
        ..Stroke::default()
      },
      dash: Vec::new(),
      dash_offset: 0.0,
      clip: None,
      gpu_clip: None,
      font: CanvasFont::default(),
      align: TextAlign::Left,
      baseline: TextBaseline::Alphabetic,
      smoothing: true,
    }
  }
}

struct Surface {
  id: CanvasId,
  metrics: CanvasMetrics,
  pixels: Option<Pixmap>,
  software: bool,
  commands: Vec<gpu::Command>,
  command_bytes: usize,
  /// Work held in open isolated layers, charged against the same budget.
  layers: Vec<gpu::LayerFrame>,
  layer_bytes: usize,
  layer_commands: usize,
  /// Software backend only: one pixmap per open layer, innermost last.
  software_layers: Vec<SoftwareLayer>,
  presentation_pixels: Option<Pixmap>,
  artwork_pixels: Option<Pixmap>,
  next_artwork_pixels: Option<Pixmap>,
  presentation_revision: u64,
  visible_revision: u64,
  presentation_serial: u64,
  presentation_refused: bool,
  presentation: Option<u64>,
  inflight_bytes: usize,
  gpu_bytes: usize,
  gpu: CanvasGpuStats,
  native: Option<NativeImageData>,
  readbacks: Arc<std::sync::atomic::AtomicUsize>,
  state: DrawingState,
  defaults: DrawingState,
  stack: Vec<DrawingState>,
  path: Path2D,
  revision: u64,
  exported_revision: u64,
  pending_paint: bool,
  image: Option<StreamingImage>,
  attached: bool,
  window: Option<Window>,
  to_window: Transform2D,
  observers: Vec<Weak<MetricsCallback>>,
  text: Option<Arc<Mutex<CanvasTextEngine>>>,
  error: Option<CanvasError>,
  /// Semantic items registered by the app, replaced as a whole.
  items: Arc<[CanvasItem]>,
  /// Fingerprint of the duplicated ids last warned about, so a redraw that
  /// repeats the same duplicates every frame warns once.
  warned_duplicates: Option<u64>,
  /// The key a painter last drew this surface for.
  painted: Option<u64>,
  /// What a moving painter last drew toward, and its hash: what the next motion comes from.
  shown: Option<(u64, MotionValue)>,
  /// The motion on its way, while the drawing moves.
  run: Option<Run>,
}

impl fmt::Debug for CanvasHandle {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("CanvasHandle")
      .field("id", &self.surface_id())
      .field("status", &self.status())
      .finish()
  }
}
impl fmt::Debug for Context2D {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_tuple("Context2D").field(&self.canvas).finish()
  }
}

impl ResolvedPaint {
  fn identity(&self) -> u64 {
    match self {
      Self::Solid(color) => {
        (u64::from(color.r()) << 24) | (u64::from(color.g()) << 16) | (u64::from(color.b()) << 8) | u64::from(color.a())
      }
      Self::Gradient {
        gradient,
        frame_from_model,
        ..
      } => {
        gradient.ramp().id
          ^ (u64::from(frame_from_model.a.to_bits()) << 8)
          ^ (u64::from(frame_from_model.d.to_bits()) << 16)
          ^ (u64::from(frame_from_model.tx.to_bits()) << 24)
          ^ (u64::from(frame_from_model.ty.to_bits()) << 32)
      }
    }
  }
}

impl CanvasColor for Color {
  fn canvas_color(&self) -> Option<Color> {
    Some(*self)
  }
}
impl CanvasColor for &str {
  fn canvas_color(&self) -> Option<Color> {
    parse_color(self)
  }
}
impl CanvasColor for String {
  fn canvas_color(&self) -> Option<Color> {
    parse_color(self)
  }
}

fn parse_color(value: &str) -> Option<Color> {
  let value = value.trim();
  let hex = value.strip_prefix('#')?;
  if !hex.is_ascii() {
    return None;
  }
  let byte = |a: usize, b: usize| u8::from_str_radix(hex.get(a..b)?, 16).ok();
  match hex.len() {
    3 | 4 => Some(Color::new(
      byte(0, 1)? * 17,
      byte(1, 2)? * 17,
      byte(2, 3)? * 17,
      if hex.len() == 4 { byte(3, 4)? * 17 } else { 255 },
    )),
    6 | 8 => Some(Color::new(
      byte(0, 2)?,
      byte(2, 4)?,
      byte(4, 6)?,
      if hex.len() == 8 { byte(6, 8)? } else { 255 },
    )),
    _ => None,
  }
}

/// One scene exercising every paint and effect this module adds, shared by the
/// software suite, the wgpu suite and the `canvas_capture_check` example that
/// covers dx12, so a difference between them is a difference in the backend
/// rather than in the fixture. Not part of the drawing API.
#[doc(hidden)]
pub fn effects_scene(d: &Context2D) {
  d.reset();
  d.set_fill_style("#101820");
  d.fill_rect(0., 0., 512., 512.);
  // Gradient fills: linear, radial and angular, each over its own box.
  d.set_fill_style(
    Gradient::linear()
      .stop(0., "#2563eb")
      .stop(1., "#f43f5e")
      .rotation(0.6)
      .in_box(20., 20., 130., 100.),
  );
  d.fill_rect(20., 20., 130., 100.);
  d.set_fill_style(
    Gradient::radial()
      .stop(0., "#fde68a")
      .stop(1., "#7c3aed")
      .in_box(170., 20., 130., 100.),
  );
  d.fill_rect(170., 20., 130., 100.);
  d.set_fill_style(
    Gradient::angular()
      .stop(0., "#22d3ee")
      .stop(0.5, "#0f172a")
      .stop(1., "#22d3ee")
      .in_box(320., 20., 130., 100.),
  );
  d.fill_rect(320., 20., 130., 100.);
  // A gradient stroke, and a shadow cast by a rounded rectangle.
  d.set_line_width(6.);
  d.set_stroke_style(
    Gradient::linear()
      .stop(0., "#34d399")
      .stop(1., "#f59e0b")
      .in_box(20., 150., 200., 80.),
  );
  d.stroke_rect(20., 150., 200., 80.);
  d.set_shadow(Some(
    Shadow::new(Color::new(0, 0, 0, 200))
      .offset(8., 10.)
      .blur(14.)
      .spread(2.),
  ));
  d.set_fill_style("#e2e8f0");
  d.round_rect(260., 150., 180., 80., 16.).unwrap();
  d.fill();
  d.set_shadow(None);
  // An inner shadow, and a layer blur.
  d.set_shadow(Some(
    Shadow::new(Color::new(0, 0, 0, 220))
      .offset(6., 6.)
      .blur(10.)
      .inset(true),
  ));
  d.set_fill_style("#94a3b8");
  d.fill_rect(20., 260., 140., 110.);
  d.set_shadow(None);
  d.set_filter(Filter::Blur(10.));
  d.set_fill_style("#f97316");
  d.fill_rect(190., 270., 90., 90.);
  d.set_filter(Filter::None);
  // Every blend mode over the same backdrop, as a row of swatches.
  d.set_fill_style("#334155");
  d.fill_rect(20., 400., 468., 40.);
  for (index, mode) in BlendMode::ALL.iter().enumerate() {
    d.set_global_composite_operation(*mode);
    d.set_fill_style("#9ae6b4");
    d.fill_rect(20. + index as f32 * 26., 400., 26., 40.);
  }
  d.set_global_composite_operation(BlendMode::Normal);
  // An isolated group: two overlapping shapes fade together, and a nested
  // layer composites with a blend mode of its own.
  d.begin_layer(0.5, BlendMode::Normal).unwrap();
  d.set_fill_style("#ef4444");
  d.fill_rect(280., 280., 80., 80.);
  d.begin_layer(1., BlendMode::Multiply).unwrap();
  d.set_fill_style("#60a5fa");
  d.fill_rect(320., 320., 80., 80.);
  d.end_layer().unwrap();
  d.end_layer().unwrap();
}

/// Surfaces for the GPU renderers' canvas tests, which use them without a tree.
#[cfg(all(test, any(feature = "wgpu", all(feature = "dx12", target_os = "windows"))))]
impl CanvasHandle {
  pub(crate) fn test_surface(width: u32, height: u32, scale: f32, software: bool) -> Self {
    let canvas = Self::new();
    {
      let mut s = canvas.inner.lock();
      s.attached = true;
      s.software = software;
      s.metrics = CanvasMetrics {
        size: Size::new(width as f32 / scale, height as f32 / scale),
        pixel_width: width,
        pixel_height: height,
        scale_factor: scale,
        revision: 1,
      };
      s.text = Some(Arc::new(Mutex::new(CanvasTextEngine::new(
        cosmic_text::FontSystem::new(),
        Default::default(),
      ))));
      if software {
        s.pixels = Pixmap::new(width, height);
      } else {
        s.enqueue(gpu::Command::Resize {
          width,
          height,
          preserve: false,
        });
      }
    }
    canvas
  }
  pub(crate) fn test_resize(&self, width: u32, height: u32, preserve: bool) {
    let mut s = self.inner.lock();
    s.metrics.pixel_width = width;
    s.metrics.pixel_height = height;
    s.enqueue(gpu::Command::Resize {
      width,
      height,
      preserve,
    });
  }
}