exprim 0.4.1

A concurrent, memory-bounded PDF reader and renderer: parse, extract text, and rasterize pages on an immutable document snapshot
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
//! The backend-neutral rendering API (roadmap §6).
//!
//! Rules:
//! - No `wgpu` (or any backend) types anywhere in this crate — ever.
//! - Backends are *job-based* (`submit` → [`RenderTicket`]); synchronous
//!   callers use [`render_blocking`].
//! - `HostPage` is the stable result type. GPU-resident surfaces arrive
//!   later as an opaque `ResidentPage` without changing this API's shape.

use std::sync::Arc;
use std::sync::mpsc;

use crate::page_ir::{CompiledPage, DeviceRect, DeviceSize, Matrix, PageFeatures};

pub mod contract;

/// Identifies a backend implementation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BackendId {
    Cpu,
    Wgpu,
    /// Test/diagnostic backends.
    Other(u32),
}

/// Frozen output surface contract (roadmap §7 Phase 6): channel order,
/// premultiplication, and transfer function are part of the *format*, so
/// CPU and GPU cannot silently diverge.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum OutputFormat {
    /// 8-bit RGBA, premultiplied alpha, sRGB transfer.
    Rgba8PremultipliedSrgb,
    /// 8-bit single-channel gray, sRGB transfer.
    Gray8,
}

impl OutputFormat {
    pub fn bytes_per_pixel(self) -> usize {
        match self {
            OutputFormat::Rgba8PremultipliedSrgb => 4,
            OutputFormat::Gray8 => 1,
        }
    }
}

/// Page background handling.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Background {
    /// Composite over opaque white (the common rasterization default).
    White,
    /// Leave unpainted areas transparent.
    Transparent,
    /// Composite over a solid color.
    Solid(crate::page_ir::Color),
}

/// Static-annotation handling (interactive features are out of scope by
/// project boundary).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AnnotationMode {
    None,
    /// Render existing appearance streams as page content.
    StaticAppearances,
}

/// Annotations render by default (PDFium `FPDF_ANNOT` display parity):
/// static appearance streams are part of what a viewer shows, so opting
/// *out* is the explicit choice, not opting in.
impl Default for AnnotationMode {
    fn default() -> Self {
        AnnotationMode::StaticAppearances
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RenderQuality {
    /// Fast preview: reduced AA, nearest-neighbor images allowed.
    Draft,
    /// Full quality per the frozen surface contract.
    Normal,
}

/// Render-time resource limits (blueprint §4.4) — enforced per job.
#[derive(Debug, Clone)]
pub struct RenderLimits {
    /// Peak intermediate bytes one page job may hold.
    pub max_page_bytes: u64,
    /// Transparency-group nesting depth.
    pub max_group_depth: u32,
    /// Wall-clock hint; backends check between operations.
    pub cancellation: Option<CancellationToken>,
}

impl Default for RenderLimits {
    fn default() -> Self {
        Self {
            max_page_bytes: 2 << 30,
            max_group_depth: 64,
            cancellation: None,
        }
    }
}

/// Cooperative cancellation. Cheap to clone, checked at operation
/// boundaries.
#[derive(Debug, Clone, Default)]
pub struct CancellationToken {
    cancelled: Arc<std::sync::atomic::AtomicBool>,
    linked: Arc<[Arc<std::sync::atomic::AtomicBool>]>,
}

impl CancellationToken {
    pub fn new() -> Self {
        Self::default()
    }

    /// Build a renderer token over a caller-owned shared flag. This is the
    /// zero-adapter cancellation seam used by tightly integrated schedulers:
    /// the viewer conductor and renderer observe the same atomic state.
    pub fn from_shared(cancelled: Arc<std::sync::atomic::AtomicBool>) -> Self {
        Self {
            cancelled,
            linked: Arc::default(),
        }
    }

    /// Share this token's own flag. For a linked token, observe
    /// [`Self::is_cancelled`] to include cancellation from its parents.
    pub fn shared_flag(&self) -> Arc<std::sync::atomic::AtomicBool> {
        self.cancelled.clone()
    }

    /// A token cancelled when either input is cancelled. Cancelling the
    /// returned token does not cancel either input. Links are flattened and
    /// deduplicated so composing scheduler/request tokens stays cheap to poll.
    pub fn linked_with(&self, other: &Self) -> Self {
        let mut linked = Vec::new();
        for flag in std::iter::once(&self.cancelled)
            .chain(self.linked.iter())
            .chain(std::iter::once(&other.cancelled))
            .chain(other.linked.iter())
        {
            if !linked.iter().any(|seen| Arc::ptr_eq(seen, flag)) {
                linked.push(Arc::clone(flag));
            }
        }
        Self {
            cancelled: Arc::default(),
            linked: linked.into(),
        }
    }

    pub fn cancel(&self) {
        self.cancelled
            .store(true, std::sync::atomic::Ordering::Release);
    }
    pub fn is_cancelled(&self) -> bool {
        self.cancelled.load(std::sync::atomic::Ordering::Acquire)
            || self
                .linked
                .iter()
                .any(|flag| flag.load(std::sync::atomic::Ordering::Acquire))
    }
}

/// Where the caller wants the result to live (roadmap §6.1). During the
/// CPU-only phases both modes return host data.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputResidency {
    /// The caller requires host memory (`HostPage`).
    HostRequired,
    /// The backend may keep the surface resident (GPU) and return an opaque
    /// handle; falls back to host data where residency is unsupported.
    BackendPreferred,
}

/// Mapping from IR page space to the output surface.
#[derive(Debug, Clone, Copy)]
pub struct PageTransform {
    /// User-space → device-space transform (already includes scale, flip,
    /// and rotation).
    pub matrix: Matrix,
}

/// Renderer-owned recoloring policy. Vector paint, text, shadings, stencil
/// images, and the page background are transformed before rasterization;
/// ordinary photographic images retain their source colors.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RenderColorPolicy {
    #[default]
    Original,
    Night {
        paper_rgb: [u8; 3],
        text_rgb: [u8; 3],
    },
    WarmPaper {
        paper_rgb: [u8; 3],
    },
}

impl RenderColorPolicy {
    pub fn paper_rgb(self) -> [u8; 3] {
        match self {
            Self::Original => [255, 255, 255],
            Self::Night { paper_rgb, .. } | Self::WarmPaper { paper_rgb } => paper_rgb,
        }
    }
}

/// A complete, backend-neutral render request (roadmap §6.1).
#[derive(Debug, Clone)]
pub struct RenderRequest {
    pub page: Arc<CompiledPage>,
    pub transform: PageTransform,
    /// Optional device-space crop of the output.
    pub crop: Option<DeviceRect>,
    pub output_size: DeviceSize,
    pub output_format: OutputFormat,
    pub background: Background,
    pub color_policy: RenderColorPolicy,
    pub annotations: AnnotationMode,
    pub quality: RenderQuality,
    pub limits: RenderLimits,
    pub residency: OutputResidency,
}

/// A rendered page in host memory — the stable result type.
#[derive(Debug, Clone)]
pub struct HostPage {
    pub width: u32,
    pub height: u32,
    /// Row stride in bytes (>= width * bytes_per_pixel).
    pub stride: usize,
    pub format: OutputFormat,
    pub pixels: Arc<[u8]>,
}

/// Opaque backend-resident surface (Stage B, roadmap §10). The owning
/// backend interprets `handle`; nothing else may.
#[derive(Debug, Clone)]
pub struct ResidentPage {
    pub backend: BackendId,
    pub handle: u64,
    pub size: DeviceSize,
    pub format: OutputFormat,
}

#[derive(Debug, Clone)]
pub enum RenderedPage {
    Host(HostPage),
    Resident(ResidentPage),
}

impl RenderedPage {
    pub fn as_host(&self) -> Option<&HostPage> {
        match self {
            RenderedPage::Host(h) => Some(h),
            RenderedPage::Resident(_) => None,
        }
    }
}

/// Why a backend declines a page (preflight, roadmap §2.4).
#[derive(Debug, Clone)]
pub struct UnsupportedFeature {
    /// Features the page needs that the backend lacks.
    pub missing: PageFeatures,
    pub detail: &'static str,
}

#[derive(Debug, Clone)]
pub enum SupportLevel {
    /// Backend implements every feature the page requires.
    Native,
    /// Backend cannot render this page; route elsewhere.
    Unsupported(UnsupportedFeature),
}

/// Post-processing operations that can be executed after rasterization.
///
/// This vocabulary deliberately lives in `exprim::render_api` rather than
/// `exprim::postprocess`: the latter consumes the frozen render API, so putting
/// capability negotiation there would create a dependency cycle.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PostprocessOperations(u16);

impl PostprocessOperations {
    pub const CROP: Self = Self(1 << 0);
    pub const RESIZE: Self = Self(1 << 1);
    pub const CONVERT_TO_GRAY: Self = Self(1 << 2);
    pub const APPLY_TONE_CURVE: Self = Self(1 << 3);
    pub const OTSU: Self = Self(1 << 4);
    pub const SAUVOLA: Self = Self(1 << 5);
    pub const FUSE_THRESHOLDS: Self = Self(1 << 6);
    pub const DITHER: Self = Self(1 << 7);
    pub const PACK_MONOCHROME: Self = Self(1 << 8);

    const ALL_BITS: u16 = (1 << 9) - 1;

    pub const fn empty() -> Self {
        Self(0)
    }

    pub const fn all() -> Self {
        Self(Self::ALL_BITS)
    }

    pub const fn contains(self, required: Self) -> bool {
        self.0 & required.0 == required.0
    }
}

impl std::ops::BitOr for PostprocessOperations {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        Self(self.0 | rhs.0)
    }
}

/// Post-processing support associated with one render backend.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PostprocessCapabilities {
    pub operations: PostprocessOperations,
    /// Operations can consume and produce this backend's resident surfaces
    /// without an intervening host readback.
    pub resident_execution: bool,
}

impl PostprocessCapabilities {
    pub const NONE: Self = Self {
        operations: PostprocessOperations::empty(),
        resident_execution: false,
    };

    /// The complete host-memory CPU executor shipped with the renderer.
    pub const HOST_ALL: Self = Self {
        operations: PostprocessOperations::all(),
        resident_execution: false,
    };

    pub fn supports(self, required: PostprocessOperations) -> bool {
        self.operations.contains(required)
    }
}

impl Default for PostprocessCapabilities {
    fn default() -> Self {
        Self::NONE
    }
}

/// What a backend can do — consulted by the scheduler's routing policy.
#[derive(Debug, Clone)]
pub struct BackendCapabilities {
    pub formats: Vec<OutputFormat>,
    pub max_surface: DeviceSize,
    pub features: PageFeatures,
    pub resident_surfaces: bool,
    pub postprocess: PostprocessCapabilities,
}

/// Render failure taxonomy (roadmap §13). GPU variants exist now so
/// fallback policy code can be written and tested before the GPU does.
#[derive(Debug, Clone, thiserror::Error)]
pub enum RenderError {
    #[error("page requires unsupported features: {0:?}")]
    Unsupported(PageFeatures),
    #[error("render limit exceeded: {0}")]
    LimitExceeded(&'static str),
    #[error("render cancelled")]
    Cancelled,
    #[error("backend failure: {0}")]
    Backend(String),
    #[error("GPU unavailable: {0}")]
    GpuUnavailable(String),
    #[error("GPU device lost")]
    GpuDeviceLost,
    #[error("GPU out of memory")]
    GpuOutOfMemory,
    #[error("readback failure: {0}")]
    Readback(String),
    #[error("result channel disconnected (backend dropped the job)")]
    Disconnected,
    /// The backend panicked while compiling/rendering this page. The panic
    /// was caught at the API boundary (see [`render_blocking`] /
    /// [`submit_caught`]): it fails this page only, never the process or the
    /// worker pool.
    #[error("backend panicked: {message}")]
    Panic { message: String },
}

/// Handle to an in-flight render job.
#[derive(Debug)]
pub struct RenderTicket {
    pub job_id: u64,
    state: std::cell::RefCell<TicketState>,
}

#[derive(Debug)]
enum TicketState {
    Pending(mpsc::Receiver<Result<RenderedPage, RenderError>>),
    Ready(Result<RenderedPage, RenderError>),
}

impl RenderTicket {
    pub fn new(job_id: u64) -> (Self, mpsc::Sender<Result<RenderedPage, RenderError>>) {
        let (tx, rx) = mpsc::channel();
        (
            Self {
                job_id,
                state: std::cell::RefCell::new(TicketState::Pending(rx)),
            },
            tx,
        )
    }

    /// Block until the job completes.
    pub fn wait(self) -> Result<RenderedPage, RenderError> {
        match self.state.into_inner() {
            TicketState::Pending(receiver) => {
                receiver.recv().unwrap_or(Err(RenderError::Disconnected))
            }
            TicketState::Ready(result) => result,
        }
    }

    /// Non-blocking poll; `None` while still in flight. Once ready, the same
    /// result remains observable on later polls and through [`Self::wait`].
    /// Host pixel buffers are shared by Arc, not copied when polling.
    pub fn try_wait(&self) -> Option<Result<RenderedPage, RenderError>> {
        let mut state = self.state.borrow_mut();
        match &*state {
            TicketState::Ready(result) => return Some(result.clone()),
            TicketState::Pending(receiver) => {
                let result = match receiver.try_recv() {
                    Ok(result) => result,
                    Err(mpsc::TryRecvError::Empty) => return None,
                    Err(mpsc::TryRecvError::Disconnected) => Err(RenderError::Disconnected),
                };
                *state = TicketState::Ready(result.clone());
                Some(result)
            }
        }
    }
}

#[derive(Debug, Clone, thiserror::Error)]
pub enum SubmitError {
    #[error("backend queue is full")]
    QueueFull,
    #[error("backend is shutting down")]
    ShuttingDown,
    #[error("request invalid: {0}")]
    InvalidRequest(&'static str),
}

/// The backend contract (roadmap §6.2). Object-safe; `Arc<dyn
/// RenderBackend>` is the ubiquitous handle.
pub trait RenderBackend: Send + Sync + std::fmt::Debug {
    fn id(&self) -> BackendId;
    fn capabilities(&self) -> BackendCapabilities;
    fn supports(&self, page: &CompiledPage, request: &RenderRequest) -> SupportLevel;
    fn submit(&self, request: RenderRequest) -> Result<RenderTicket, SubmitError>;

    /// Bytes of the premultiplied RGBA plane this backend paints `request`
    /// on, which the scheduler budgets for beside the output and caps its
    /// offscreen estimate by. The whole page, four bytes per pixel, unless
    /// the backend renders the page in pieces.
    fn working_surface_bytes(&self, request: &RenderRequest) -> u64 {
        u64::from(request.output_size.width)
            .saturating_mul(u64::from(request.output_size.height))
            .saturating_mul(4)
    }
}

/// Best-effort extraction of a human-readable message from a panic payload.
/// Public so other panic boundaries (e.g. the scheduler's render workers)
/// produce the same [`RenderError::Panic`] taxonomy.
pub fn panic_message(payload: Box<dyn std::any::Any + Send>) -> String {
    if let Some(s) = payload.downcast_ref::<&'static str>() {
        (*s).to_owned()
    } else if let Some(s) = payload.downcast_ref::<String>() {
        s.clone()
    } else {
        "non-string panic payload".to_owned()
    }
}

/// Submit a job with a panic boundary: a panic escaping the backend's
/// `submit` (the CPU backend fulfills synchronously inside `submit`) is
/// caught and converted into a ticket that resolves to
/// [`RenderError::Panic`]. Worker pools driving `submit` directly should use
/// this so one poisoned page can never take down a worker thread.
///
/// `AssertUnwindSafe` is justified: the request's `CompiledPage` is behind an
/// immutable `Arc` snapshot (backends never mutate it), and all backend
/// scratch state is per-call and discarded on unwind, so no observable
/// broken invariants survive the catch.
pub fn submit_caught(
    backend: &dyn RenderBackend,
    request: RenderRequest,
) -> Result<RenderTicket, SubmitError> {
    match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| backend.submit(request))) {
        Ok(outcome) => outcome,
        Err(payload) => {
            // Fabricate a resolved ticket carrying the typed panic error so
            // callers observe a normal per-page failure.
            let (ticket, tx) = RenderTicket::new(u64::MAX);
            let _ = tx.send(Err(RenderError::Panic {
                message: panic_message(payload),
            }));
            Ok(ticket)
        }
    }
}

/// Blocking convenience wrapper — the API most simple embedders use.
///
/// This is a panic boundary: a panic anywhere in the backend's per-page
/// compile/render path becomes [`RenderError::Panic`] for *that page only* —
/// never a process abort or a dead worker. `AssertUnwindSafe` is sound here
/// because the document snapshot behind the request is immutable and every
/// piece of backend scratch state is per-call and discarded on unwind.
pub fn render_blocking(
    backend: &dyn RenderBackend,
    request: RenderRequest,
) -> Result<RenderedPage, RenderError> {
    let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        match backend.submit(request) {
            Ok(ticket) => ticket.wait(),
            Err(e) => Err(RenderError::Backend(e.to_string())),
        }
    }));
    match outcome {
        Ok(result) => result,
        Err(payload) => Err(RenderError::Panic {
            message: panic_message(payload),
        }),
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
    use super::*;

    const fn assert_send_sync<T: Send + Sync>() {}
    const _: () = assert_send_sync::<RenderRequest>();
    const _: () = assert_send_sync::<HostPage>();

    #[test]
    fn ticket_roundtrip() {
        let (ticket, tx) = RenderTicket::new(7);
        assert!(ticket.try_wait().is_none());
        let page = HostPage {
            width: 1,
            height: 1,
            stride: 4,
            format: OutputFormat::Rgba8PremultipliedSrgb,
            pixels: Arc::from([0u8; 4]),
        };
        tx.send(Ok(RenderedPage::Host(page))).unwrap();
        assert!(matches!(ticket.wait(), Ok(RenderedPage::Host(_))));
    }

    #[test]
    fn polling_completion_keeps_the_result_ready_until_wait() {
        let (ticket, tx) = RenderTicket::new(9);
        assert!(ticket.try_wait().is_none());
        tx.send(Err(RenderError::Cancelled)).unwrap();
        drop(tx);
        for _ in 0..3 {
            assert!(matches!(
                ticket.try_wait(),
                Some(Err(RenderError::Cancelled))
            ));
        }
        assert!(matches!(ticket.wait(), Err(RenderError::Cancelled)));
    }

    #[test]
    fn linked_cancellation_observes_both_sources_without_cancelling_parents() {
        for cancel_first in [false, true] {
            let first = CancellationToken::new();
            let second = CancellationToken::new();
            let linked = first.linked_with(&second);
            assert!(!linked.is_cancelled());
            if cancel_first {
                first.cancel();
            } else {
                second.cancel();
            }
            assert!(linked.is_cancelled());
        }
        let first = CancellationToken::new();
        let second = CancellationToken::new();
        first.linked_with(&second).cancel();
        assert!(!first.is_cancelled());
        assert!(!second.is_cancelled());
        let third = CancellationToken::new();
        let linked = first.linked_with(&second).linked_with(&third);
        second.cancel();
        assert!(linked.is_cancelled());
    }

    #[test]
    fn dropped_sender_is_disconnected() {
        let (ticket, tx) = RenderTicket::new(1);
        drop(tx);
        assert!(matches!(ticket.wait(), Err(RenderError::Disconnected)));
    }

    /// Test-only backend that panics inside `submit`, standing in for any
    /// bug in the per-page compile/render path.
    #[derive(Debug)]
    struct PanickingBackend;

    impl RenderBackend for PanickingBackend {
        fn id(&self) -> BackendId {
            BackendId::Other(0xDEAD)
        }
        fn capabilities(&self) -> BackendCapabilities {
            BackendCapabilities {
                formats: vec![OutputFormat::Rgba8PremultipliedSrgb],
                max_surface: DeviceSize {
                    width: 1,
                    height: 1,
                },
                features: PageFeatures::empty(),
                resident_surfaces: false,
                postprocess: PostprocessCapabilities::NONE,
            }
        }
        fn supports(&self, _page: &CompiledPage, _request: &RenderRequest) -> SupportLevel {
            SupportLevel::Native
        }
        fn submit(&self, _request: RenderRequest) -> Result<RenderTicket, SubmitError> {
            panic!("injected test panic: page 0 poisoned");
        }
    }

    fn dummy_request() -> RenderRequest {
        let page = CompiledPage::empty(crate::page_ir::PageBounds {
            crop: crate::page_ir::Rect {
                x0: 0.0,
                y0: 0.0,
                x1: 10.0,
                y1: 10.0,
            },
            rotate: 0,
        });
        RenderRequest {
            page: Arc::new(page),
            transform: PageTransform {
                matrix: Matrix::IDENTITY,
            },
            crop: None,
            output_size: DeviceSize {
                width: 4,
                height: 4,
            },
            output_format: OutputFormat::Rgba8PremultipliedSrgb,
            background: Background::White,
            annotations: AnnotationMode::None,
            color_policy: RenderColorPolicy::Original,
            quality: RenderQuality::Normal,
            limits: RenderLimits::default(),
            residency: OutputResidency::HostRequired,
        }
    }

    #[test]
    fn injected_panic_becomes_typed_error_in_render_blocking() {
        // Silence the default panic hook's backtrace spew for this test; the
        // hook is process-global, so restore it afterwards.
        let prev = std::panic::take_hook();
        std::panic::set_hook(Box::new(|_| {}));
        let result = render_blocking(&PanickingBackend, dummy_request());
        std::panic::set_hook(prev);
        match result {
            Err(RenderError::Panic { message }) => {
                assert!(
                    message.contains("injected test panic"),
                    "message: {message}"
                );
            }
            other => panic!("expected RenderError::Panic, got {other:?}"),
        }
    }

    #[test]
    fn injected_panic_becomes_typed_error_in_submit_caught() {
        let prev = std::panic::take_hook();
        std::panic::set_hook(Box::new(|_| {}));
        let submitted = submit_caught(&PanickingBackend, dummy_request());
        std::panic::set_hook(prev);
        let ticket = submitted.expect("panic must yield a resolved ticket, not a SubmitError");
        match ticket.wait() {
            Err(RenderError::Panic { message }) => {
                assert!(
                    message.contains("injected test panic"),
                    "message: {message}"
                );
            }
            other => panic!("expected RenderError::Panic, got {other:?}"),
        }
    }
}