rhwp 0.9.1

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
//! Cross-thread presentation inputs and CPU frame leases.
//!
//! No session, Skia renderer/context, or GPU pointer lives in this registry.
//! Expensive replay happens outside locks. A lease pins immutable bytes after
//! its producer handle is released; stale sources reject new acquisition.

use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};

use serde::{Deserialize, Serialize};

use super::render_snapshot::{NativeRenderSnapshot, NativeRenderSnapshotUsage};
use super::runtime::{NativeRequestId, NativeRuntimeObserver, NativeRuntimeTarget};
use super::NativeHostError;
use crate::renderer::raster_pixels::RasterRegion;
use rhwp_codec_resource::atomic::try_update_u64;

const MAX_OBJECTS: usize = 64;
const MAX_BYTES: usize = 128 * 1024 * 1024;
static NEXT_ID: AtomicU64 = AtomicU64::new(1);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct NativePresentationId(u64);

impl NativePresentationId {
    pub fn from_raw(value: u64) -> Self {
        Self(value)
    }

    pub fn as_u64(self) -> u64 {
        self.0
    }
}

fn next_id() -> Result<NativePresentationId, NativeHostError> {
    try_update_u64(&NEXT_ID, Ordering::Relaxed, Ordering::Relaxed, |id| {
        id.checked_add(1)
    })
    .map(NativePresentationId)
    .map_err(|_| NativeHostError::LimitExceeded("presentation handle space exhausted".into()))
}

fn invalid(message: &str) -> NativeHostError {
    NativeHostError::InvalidArgument(message.into())
}

fn limit(message: &str) -> NativeHostError {
    NativeHostError::LimitExceeded(message.into())
}

fn lock<T>(value: &Mutex<T>) -> Result<MutexGuard<'_, T>, NativeHostError> {
    value
        .lock()
        .map_err(|_| NativeHostError::Unsupported("presentation registry is poisoned".into()))
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct NativePresentationStamp {
    pub document_id: u64,
    pub revision: u64,
    pub font_generation: u64,
    pub view_generation: u64,
    pub device_generation: u64,
    pub device_scale: f64,
}

impl NativePresentationStamp {
    pub fn validate(self) -> Result<(), NativeHostError> {
        if self.document_id == 0
            || self.view_generation == 0
            || self.device_generation == 0
            || !self.device_scale.is_finite()
            || self.device_scale <= 0.0
            || self.device_scale > 16.0
        {
            return Err(invalid(
                "invalid presentation identity, generation, or device scale",
            ));
        }
        Ok(())
    }

    fn target(self) -> NativeRuntimeTarget {
        NativeRuntimeTarget {
            document_id: NativeRequestId::from_raw(self.document_id),
            revision: self.revision,
        }
    }
}

const MAX_COORDINATE: f64 = 16_777_216.0;

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct NativePagePlacement {
    pub snapshot: NativePresentationId,
    pub scale: f64,
    pub origin_x: f64,
    pub origin_y: f64,
    pub background_color: Option<u32>,
}

impl NativePagePlacement {
    pub fn validate(self, snapshot: &NativeRenderSnapshot) -> Result<(), NativeHostError> {
        let tree = snapshot.tree();
        let values = [
            self.scale,
            self.origin_x,
            self.origin_y,
            tree.page_width,
            tree.page_height,
            self.origin_x + tree.page_width * self.scale,
            self.origin_y + tree.page_height * self.scale,
        ];
        if self.scale <= 0.0
            || self.scale > 16.0
            || self.scale as f32 <= 0.0
            || tree.page_width <= 0.0
            || tree.page_height <= 0.0
            || values
                .into_iter()
                .any(|value| !value.is_finite() || value.abs() > MAX_COORDINATE)
        {
            return Err(invalid("invalid page placement"));
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Copy)]
pub struct NativePresentationConfig {
    pub max_sources: usize,
    pub max_snapshots: usize,
    pub max_snapshot_bytes: usize,
    pub max_frames: usize,
    pub max_frame_bytes: usize,
    pub max_leases: usize,
    pub max_in_flight_replays: usize,
}

/// Physical presentation pixels, independent of any GL/Linux target type.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct NativePresentationViewport {
    pub width: i32,
    pub height: i32,
}

impl NativePresentationViewport {
    /// Checked `(stride, byte_length)` for an RGBA8 target.
    pub fn rgba_layout(
        self,
        max_dimension: i32,
        max_pixels: usize,
    ) -> Result<(usize, usize), NativeHostError> {
        if self.width <= 0
            || self.height <= 0
            || self.width > max_dimension
            || self.height > max_dimension
        {
            return Err(invalid("invalid presentation viewport size"));
        }
        let pixels = (self.width as usize)
            .checked_mul(self.height as usize)
            .filter(|pixels| *pixels <= max_pixels)
            .ok_or_else(|| limit("presentation viewport pixel quota exceeded"))?;
        let stride = (self.width as usize)
            .checked_mul(4)
            .ok_or_else(|| limit("presentation stride overflow"))?;
        let bytes = pixels
            .checked_mul(4)
            .ok_or_else(|| limit("presentation byte length overflow"))?;
        Ok((stride, bytes))
    }
}

impl Default for NativePresentationConfig {
    fn default() -> Self {
        Self {
            max_sources: 16,
            max_snapshots: 32,
            max_snapshot_bytes: MAX_BYTES,
            max_frames: 32,
            max_frame_bytes: MAX_BYTES,
            max_leases: 64,
            max_in_flight_replays: 4,
        }
    }
}

impl NativePresentationConfig {
    fn validate(self) -> Result<(), NativeHostError> {
        if [
            self.max_sources,
            self.max_snapshots,
            self.max_frames,
            self.max_leases,
        ]
        .into_iter()
        .any(|value| value == 0 || value > MAX_OBJECTS)
            || !(1..=MAX_BYTES).contains(&self.max_snapshot_bytes)
            || !(1..=MAX_BYTES).contains(&self.max_frame_bytes)
            || !(1..=4).contains(&self.max_in_flight_replays)
        {
            return Err(invalid("invalid presentation registry limits"));
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NativePresentationSnapshotInfo {
    pub snapshot: NativePresentationId,
    pub source: NativePresentationId,
    pub document_id: u64,
    pub revision: u64,
    pub font_generation: u64,
    pub page: u32,
    pub profile: &'static str,
    pub font_fingerprint: String,
    pub usage: NativeRenderSnapshotUsage,
    pub current: bool,
}

/// Only the CPU variant is implemented. Future GPU descriptors must define
/// backend-specific ownership and fences before a GPU capability is advertised.
#[derive(Debug, Clone, Serialize)]
#[serde(
    tag = "backend",
    rename_all = "camelCase",
    rename_all_fields = "camelCase"
)]
pub enum NativePresentationDescriptor {
    Cpu {
        width: i32,
        height: i32,
        stride: usize,
        byte_length: usize,
        pixel_format: &'static str,
        color_space: &'static str,
        alpha: &'static str,
        origin: &'static str,
        origin_x: i32,
        origin_y: i32,
        raster_scale: f64,
        device_scale: f64,
    },
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NativePresentationFrameInfo {
    pub frame: NativePresentationId,
    pub source: NativePresentationId,
    pub stamp: NativePresentationStamp,
    pub descriptor: NativePresentationDescriptor,
    pub current: bool,
    pub page_count: usize,
    pub coordinate_space: &'static str,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NativePresentationLeaseInfo {
    pub lease: NativePresentationId,
    pub frame: NativePresentationFrameInfo,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NativePresentationStats {
    pub sources: usize,
    pub snapshot_handles: usize,
    pub retained_snapshots: usize,
    pub snapshot_estimated_bytes: usize,
    pub frame_handles: usize,
    pub retained_frames: usize,
    pub frame_bytes: usize,
    pub leases: usize,
    pub in_flight_replays: usize,
}

struct Source {
    id: NativePresentationId,
    observer: NativeRuntimeObserver,
    active: AtomicBool,
    stamp: Mutex<NativePresentationStamp>,
}

impl Source {
    fn document_current(&self, document_id: u64, revision: u64, generation: u64) -> bool {
        self.active.load(Ordering::Acquire)
            && self.observer.is_current(
                NativeRuntimeTarget {
                    document_id: NativeRequestId::from_raw(document_id),
                    revision,
                },
                generation,
            )
    }

    fn current(&self, stamp: NativePresentationStamp) -> Result<bool, NativeHostError> {
        let configured = *lock(&self.stamp)?;
        Ok(configured == stamp
            && self.document_current(stamp.document_id, stamp.revision, stamp.font_generation))
    }
}

#[derive(Default)]
struct Retention {
    snapshots: usize,
    snapshot_bytes: usize,
    frames: usize,
    frame_bytes: usize,
    replays: usize,
}

struct Budget {
    config: NativePresentationConfig,
    retained: Mutex<Retention>,
}

enum AllocationKind {
    Snapshot,
    #[cfg(feature = "native-skia")]
    Frame,
}

/// The owning Arc record keeps this reservation after removal of a handle.
/// Pixel/snapshot fields are declared before it, so payloads drop first.
struct Allocation {
    budget: Arc<Budget>,
    bytes: usize,
    kind: AllocationKind,
    #[cfg(feature = "native-skia")]
    replaying: bool,
}

impl Allocation {
    fn snapshot(budget: &Arc<Budget>, bytes: usize) -> Result<Self, NativeHostError> {
        let mut retained = lock(&budget.retained)?;
        let total = retained
            .snapshot_bytes
            .checked_add(bytes)
            .filter(|total| *total <= budget.config.max_snapshot_bytes)
            .ok_or_else(|| limit("presentation snapshot byte quota exceeded"))?;
        if retained.snapshots >= budget.config.max_snapshots {
            return Err(limit("presentation snapshot count quota exceeded"));
        }
        retained.snapshots += 1;
        retained.snapshot_bytes = total;
        Ok(Self {
            budget: Arc::clone(budget),
            bytes,
            kind: AllocationKind::Snapshot,
            #[cfg(feature = "native-skia")]
            replaying: false,
        })
    }

    #[cfg(feature = "native-skia")]
    fn frame(budget: &Arc<Budget>, bytes: usize) -> Result<Self, NativeHostError> {
        let mut retained = lock(&budget.retained)?;
        let total = retained
            .frame_bytes
            .checked_add(bytes)
            .filter(|total| *total <= budget.config.max_frame_bytes)
            .ok_or_else(|| limit("presentation CPU frame byte quota exceeded"))?;
        if retained.frames >= budget.config.max_frames
            || retained.replays >= budget.config.max_in_flight_replays
        {
            return Err(limit(
                "presentation CPU frame or replay count quota exceeded",
            ));
        }
        retained.frames += 1;
        retained.frame_bytes = total;
        retained.replays += 1;
        Ok(Self {
            budget: Arc::clone(budget),
            bytes,
            kind: AllocationKind::Frame,
            replaying: true,
        })
    }

    #[cfg(feature = "native-skia")]
    fn replay_finished(&mut self) -> Result<(), NativeHostError> {
        let mut retained = lock(&self.budget.retained)?;
        if self.replaying {
            retained.replays -= 1;
            self.replaying = false;
        }
        Ok(())
    }
}

impl Drop for Allocation {
    fn drop(&mut self) {
        // No caller code runs under this lock. On poisoning, retain the credits
        // and fail closed rather than make unverified state reusable.
        if let Ok(mut retained) = self.budget.retained.lock() {
            match self.kind {
                AllocationKind::Snapshot => {
                    retained.snapshots -= 1;
                    retained.snapshot_bytes -= self.bytes;
                }
                #[cfg(feature = "native-skia")]
                AllocationKind::Frame => {
                    retained.frames -= 1;
                    retained.frame_bytes -= self.bytes;
                    if self.replaying {
                        retained.replays -= 1;
                    }
                }
            }
        }
    }
}

struct Snapshot {
    input: Arc<NativeRenderSnapshot>,
    document_id: u64,
    source: Arc<Source>,
    _allocation: Allocation,
}

/// Pins the registry's snapshot reservation through an independent replay.
/// This never owns a session or renderer. Page input identity is independent
/// of the view/device stamp, which must be checked again before publication.
pub struct NativePresentationSnapshotBorrow {
    entry: Arc<Snapshot>,
}

#[derive(Debug)]
pub enum NativePresentationBorrowError {
    InvalidHandle,
    Stale,
    Host(NativeHostError),
}

impl From<NativePresentationBorrowError> for NativeHostError {
    fn from(error: NativePresentationBorrowError) -> Self {
        match error {
            NativePresentationBorrowError::InvalidHandle => {
                invalid("unknown presentation snapshot")
            }
            NativePresentationBorrowError::Stale => invalid("stale presentation render stamp"),
            NativePresentationBorrowError::Host(error) => error,
        }
    }
}

impl std::fmt::Display for NativePresentationBorrowError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidHandle => f.write_str("unknown presentation snapshot"),
            Self::Stale => f.write_str("stale presentation render stamp"),
            Self::Host(error) => error.fmt(f),
        }
    }
}
impl std::error::Error for NativePresentationBorrowError {}

impl NativePresentationSnapshotBorrow {
    pub fn snapshot(&self) -> &NativeRenderSnapshot {
        &self.entry.input
    }

    pub fn source(&self) -> NativePresentationId {
        self.entry.source.id
    }

    pub fn validate_current(&self, stamp: NativePresentationStamp) -> Result<(), NativeHostError> {
        self.validate_current_checked(stamp).map_err(Into::into)
    }

    pub fn validate_current_checked(
        &self,
        stamp: NativePresentationStamp,
    ) -> Result<(), NativePresentationBorrowError> {
        stamp
            .validate()
            .map_err(NativePresentationBorrowError::Host)?;
        if stamp.document_id != self.entry.document_id
            || stamp.revision != self.entry.input.revision()
            || stamp.font_generation != self.entry.input.font_generation()
            || !self
                .entry
                .source
                .current(stamp)
                .map_err(NativePresentationBorrowError::Host)?
        {
            return Err(NativePresentationBorrowError::Stale);
        }
        Ok(())
    }
}

struct Frame {
    bytes: Box<[u8]>,
    id: NativePresentationId,
    source: Arc<Source>,
    stamp: NativePresentationStamp,
    descriptor: NativePresentationDescriptor,
    page_count: usize,
    coordinate_space: &'static str,
    _allocation: Allocation,
}

impl Frame {
    fn info(&self) -> Result<NativePresentationFrameInfo, NativeHostError> {
        Ok(NativePresentationFrameInfo {
            frame: self.id,
            source: self.source.id,
            stamp: self.stamp,
            descriptor: self.descriptor.clone(),
            current: self.source.current(self.stamp)?,
            page_count: self.page_count,
            coordinate_space: self.coordinate_space,
        })
    }
}

#[derive(Default)]
struct Records {
    sources: HashMap<NativePresentationId, Arc<Source>>,
    snapshots: HashMap<NativePresentationId, Arc<Snapshot>>,
    frames: HashMap<NativePresentationId, Arc<Frame>>,
    leases: HashMap<NativePresentationId, Arc<Frame>>,
}

pub struct NativePresentationRegistry {
    config: NativePresentationConfig,
    records: Mutex<Records>,
    budget: Arc<Budget>,
}

impl NativePresentationRegistry {
    pub fn new(config: NativePresentationConfig) -> Result<Self, NativeHostError> {
        config.validate()?;
        Ok(Self {
            config,
            records: Mutex::new(Records::default()),
            budget: Arc::new(Budget {
                config,
                retained: Mutex::new(Retention::default()),
            }),
        })
    }

    pub fn create_source(
        &self,
        observer: NativeRuntimeObserver,
        stamp: NativePresentationStamp,
    ) -> Result<NativePresentationId, NativeHostError> {
        stamp.validate()?;
        if !observer.is_current(stamp.target(), stamp.font_generation) {
            return Err(invalid("stale presentation source document"));
        }
        let mut records = lock(&self.records)?;
        if records.sources.len() >= self.config.max_sources {
            return Err(limit("presentation source count quota exceeded"));
        }
        let id = next_id()?;
        records.sources.insert(
            id,
            Arc::new(Source {
                id,
                observer,
                active: AtomicBool::new(true),
                stamp: Mutex::new(stamp),
            }),
        );
        Ok(id)
    }

    pub fn configure_source(
        &self,
        source: NativePresentationId,
        stamp: NativePresentationStamp,
    ) -> Result<(), NativeHostError> {
        stamp.validate()?;
        let records = lock(&self.records)?;
        let source = records
            .sources
            .get(&source)
            .ok_or_else(|| invalid("unknown presentation source"))?;
        if !source.document_current(stamp.document_id, stamp.revision, stamp.font_generation) {
            return Err(invalid("stale presentation source document"));
        }
        let mut current = lock(&source.stamp)?;
        let previous = *current;
        if stamp.view_generation < previous.view_generation
            || stamp.device_generation < previous.device_generation
            || (stamp != previous
                && stamp.view_generation == previous.view_generation
                && stamp.device_generation == previous.device_generation)
        {
            return Err(invalid(
                "presentation generations must advance before changing inputs",
            ));
        }
        *current = stamp;
        Ok(())
    }

    pub fn close_source(&self, source: NativePresentationId) -> Result<(), NativeHostError> {
        let mut records = lock(&self.records)?;
        let source = records
            .sources
            .remove(&source)
            .ok_or_else(|| invalid("unknown presentation source"))?;
        source.active.store(false, Ordering::Release);
        Ok(())
    }

    pub fn retain_snapshot(
        &self,
        source: NativePresentationId,
        document_id: u64,
        snapshot: Arc<NativeRenderSnapshot>,
    ) -> Result<NativePresentationId, NativeHostError> {
        let mut records = lock(&self.records)?;
        let source = Arc::clone(
            records
                .sources
                .get(&source)
                .ok_or_else(|| invalid("unknown presentation source"))?,
        );
        if !source.document_current(document_id, snapshot.revision(), snapshot.font_generation()) {
            return Err(invalid("stale render snapshot document"));
        }
        let allocation = Allocation::snapshot(&self.budget, snapshot.usage().estimated_bytes)?;
        let id = next_id()?;
        records.snapshots.insert(
            id,
            Arc::new(Snapshot {
                input: snapshot,
                document_id,
                source,
                _allocation: allocation,
            }),
        );
        Ok(id)
    }

    pub fn snapshot_info(
        &self,
        snapshot: NativePresentationId,
    ) -> Result<NativePresentationSnapshotInfo, NativeHostError> {
        let records = lock(&self.records)?;
        let entry = records
            .snapshots
            .get(&snapshot)
            .ok_or_else(|| invalid("unknown presentation snapshot"))?;
        Ok(NativePresentationSnapshotInfo {
            snapshot,
            source: entry.source.id,
            document_id: entry.document_id,
            revision: entry.input.revision(),
            font_generation: entry.input.font_generation(),
            page: entry.input.page(),
            profile: super::render_snapshot::profile_name(entry.input.profile()),
            font_fingerprint: entry.input.font_state().fingerprint.clone(),
            usage: entry.input.usage(),
            current: entry.source.document_current(
                entry.document_id,
                entry.input.revision(),
                entry.input.font_generation(),
            ),
        })
    }

    /// Metadata-only full-stamp query. A stale valid handle returns false;
    /// malformed stamps/unknown handles remain errors. No Core/replay runs.
    pub fn snapshot_check(
        &self,
        snapshot: NativePresentationId,
        stamp: NativePresentationStamp,
    ) -> Result<bool, NativeHostError> {
        match self.borrow_snapshot_checked(snapshot, stamp) {
            Ok(_) => Ok(true),
            Err(NativePresentationBorrowError::Stale) => Ok(false),
            Err(error) => Err(error.into()),
        }
    }

    pub fn release_snapshot(&self, snapshot: NativePresentationId) -> Result<(), NativeHostError> {
        lock(&self.records)?
            .snapshots
            .remove(&snapshot)
            .ok_or_else(|| invalid("unknown presentation snapshot"))?;
        Ok(())
    }

    pub fn borrow_snapshot(
        &self,
        snapshot: NativePresentationId,
        stamp: NativePresentationStamp,
    ) -> Result<NativePresentationSnapshotBorrow, NativeHostError> {
        self.borrow_snapshot_checked(snapshot, stamp)
            .map_err(Into::into)
    }

    pub fn borrow_snapshot_checked(
        &self,
        snapshot: NativePresentationId,
        stamp: NativePresentationStamp,
    ) -> Result<NativePresentationSnapshotBorrow, NativePresentationBorrowError> {
        let records = lock(&self.records).map_err(NativePresentationBorrowError::Host)?;
        let borrowed = NativePresentationSnapshotBorrow {
            entry: Arc::clone(
                records
                    .snapshots
                    .get(&snapshot)
                    .ok_or(NativePresentationBorrowError::InvalidHandle)?,
            ),
        };
        borrowed.validate_current_checked(stamp)?;
        Ok(borrowed)
    }

    pub fn render_cpu(
        &self,
        snapshot: NativePresentationId,
        stamp: NativePresentationStamp,
        region: RasterRegion,
        scale: f64,
    ) -> Result<NativePresentationFrameInfo, NativeHostError> {
        #[cfg(feature = "native-skia")]
        {
            use crate::renderer::layer_renderer::RasterRenderOptions;
            stamp.validate()?;
            super::NativeDocumentSession::check_scale(scale)?;
            let options = RasterRenderOptions {
                scale,
                max_pixels: super::MAX_FRAME_PIXELS,
                transparent: false,
                ..Default::default()
            };
            let window =
                region.pixel_window(scale, options.max_dimension, super::MAX_FRAME_PIXELS)?;
            let (entry, mut allocation) = {
                let records = lock(&self.records)?;
                let entry = Arc::clone(
                    records
                        .snapshots
                        .get(&snapshot)
                        .ok_or_else(|| invalid("unknown presentation snapshot"))?,
                );
                if stamp.document_id != entry.document_id
                    || stamp.revision != entry.input.revision()
                    || stamp.font_generation != entry.input.font_generation()
                    || !entry.source.current(stamp)?
                {
                    return Err(invalid("stale presentation render stamp"));
                }
                let allocation = Allocation::frame(&self.budget, window.output_bytes)?;
                (entry, allocation)
            };
            // Only immutable input/observer Arcs cross this boundary. Native
            // objects are created, used, and dropped on this calling thread.
            let rendered = entry
                .input
                .create_renderer()?
                .render_region_pixels_with_options(entry.input.tree(), region, options)?;
            if rendered.pixels.bytes.len() != window.output_bytes
                || rendered.pixels.width != window.width
                || rendered.pixels.height != window.height
                || rendered.pixels.stride != window.stride
                || rendered.pixels.format
                    != crate::renderer::raster_pixels::RasterPixelFormat::Rgba8Premultiplied
                || rendered.pixels.color_space
                    != crate::renderer::layer_renderer::RasterColorSpace::Srgb
                || rendered.origin_x != window.origin_x
                || rendered.origin_y != window.origin_y
            {
                return Err(invalid(
                    "CPU renderer output disagrees with its reserved geometry",
                ));
            }
            let bytes = rendered.pixels.bytes.into_boxed_slice();
            allocation.replay_finished()?;
            let mut records = lock(&self.records)?;
            if !entry.source.current(stamp)? {
                return Err(invalid("presentation source changed during replay"));
            }
            let id = next_id()?;
            let frame = Arc::new(Frame {
                bytes,
                id,
                source: Arc::clone(&entry.source),
                stamp,
                page_count: 1,
                coordinate_space: "pageRaster",
                descriptor: NativePresentationDescriptor::Cpu {
                    width: window.width,
                    height: window.height,
                    stride: window.stride,
                    byte_length: window.output_bytes,
                    pixel_format: "rgba8Premultiplied",
                    color_space: "srgb",
                    alpha: "premultiplied",
                    origin: "topLeft",
                    origin_x: window.origin_x,
                    origin_y: window.origin_y,
                    raster_scale: scale,
                    device_scale: stamp.device_scale,
                },
                _allocation: allocation,
            });
            let info = frame.info()?;
            if !info.current {
                return Err(invalid("presentation source changed during replay"));
            }
            records.frames.insert(id, frame);
            Ok(info)
        }
        #[cfg(not(feature = "native-skia"))]
        {
            let _ = (snapshot, stamp, region, scale);
            Err(NativeHostError::FeatureDisabled)
        }
    }

    /// Compose owned immutable pages into one physical CPU viewport. This is
    /// synchronous work for a caller-owned render thread, with no Core/layout
    /// query and no renderer transfer. Every output uses the existing leases.
    pub fn render_cpu_batch(
        &self,
        stamp: NativePresentationStamp,
        viewport: NativePresentationViewport,
        placements: &[NativePagePlacement],
        clear_color: u32,
    ) -> Result<NativePresentationFrameInfo, NativeHostError> {
        #[cfg(feature = "native-skia")]
        {
            use crate::renderer::skia::SkiaSurfaceRenderOptions;
            use skia_safe::{surfaces, AlphaType, Color, ColorSpace, ColorType, ImageInfo};
            stamp.validate()?;
            let (stride, byte_length) =
                viewport.rgba_layout(4096, super::MAX_FRAME_PIXELS as usize)?;
            if placements.is_empty() || placements.len() > 32 {
                return Err(limit("CPU batch page count is outside its quota"));
            }
            let inputs: Vec<_> = placements
                .iter()
                .map(|placement| {
                    let input = self
                        .borrow_snapshot_checked(placement.snapshot, stamp)
                        .map_err(NativeHostError::from)?;
                    placement.validate(input.snapshot())?;
                    Ok(input)
                })
                .collect::<Result<_, NativeHostError>>()?;
            if inputs
                .iter()
                .any(|input| input.source() != inputs[0].source())
            {
                return Err(invalid(
                    "CPU batch pages must share one presentation source",
                ));
            }
            let mut allocation = Allocation::frame(&self.budget, byte_length)?;
            let mut bytes = vec![0u8; byte_length];
            let image_info = ImageInfo::new(
                (viewport.width, viewport.height),
                ColorType::RGBA8888,
                AlphaType::Premul,
                ColorSpace::new_srgb(),
            );
            {
                let mut surface = surfaces::wrap_pixels(&image_info, &mut bytes, stride, None)
                    .ok_or_else(|| {
                        NativeHostError::Unsupported(
                            "could not borrow the CPU presentation pixels".into(),
                        )
                    })?;
                surface.canvas().clear(Color::from(clear_color));
                for (placement, input) in placements.iter().zip(&inputs) {
                    // At most one native renderer/font copy is live here. Its
                    // three caches together retain at most the 8 MiB estimate.
                    let renderer = input.snapshot().create_renderer()?.with_cache_limits(
                        super::MAX_RENDER_CACHE_ENTRIES,
                        super::MAX_RENDER_CACHE_BYTES / 3,
                    );
                    renderer.render_to_canvas_strict(
                        surface.canvas(),
                        input.snapshot().tree(),
                        SkiaSurfaceRenderOptions {
                            scale: placement.scale,
                            origin_x: placement.origin_x,
                            origin_y: placement.origin_y,
                            background_color: placement.background_color,
                        },
                    )?;
                }
            }
            allocation.replay_finished()?;
            let mut records = lock(&self.records)?;
            for input in &inputs {
                input.validate_current(stamp)?;
            }
            let id = next_id()?;
            let frame = Arc::new(Frame {
                bytes: bytes.into_boxed_slice(),
                id,
                source: Arc::clone(&inputs[0].entry.source),
                stamp,
                page_count: placements.len(),
                coordinate_space: "viewportPixels",
                descriptor: NativePresentationDescriptor::Cpu {
                    width: viewport.width,
                    height: viewport.height,
                    stride,
                    byte_length,
                    pixel_format: "rgba8Premultiplied",
                    color_space: "srgb",
                    alpha: "premultiplied",
                    origin: "topLeft",
                    origin_x: 0,
                    origin_y: 0,
                    raster_scale: 1.0,
                    device_scale: stamp.device_scale,
                },
                _allocation: allocation,
            });
            let info = frame.info()?;
            if !info.current {
                return Err(invalid("presentation source changed during replay"));
            }
            records.frames.insert(id, frame);
            Ok(info)
        }
        #[cfg(not(feature = "native-skia"))]
        {
            let _ = (stamp, viewport, placements, clear_color);
            Err(NativeHostError::FeatureDisabled)
        }
    }

    pub fn frame_info(
        &self,
        frame: NativePresentationId,
    ) -> Result<NativePresentationFrameInfo, NativeHostError> {
        lock(&self.records)?
            .frames
            .get(&frame)
            .ok_or_else(|| invalid("unknown presentation frame"))?
            .info()
    }

    pub fn acquire_frame(
        &self,
        frame: NativePresentationId,
        stamp: NativePresentationStamp,
    ) -> Result<NativePresentationLeaseInfo, NativeHostError> {
        stamp.validate()?;
        let mut records = lock(&self.records)?;
        let frame = Arc::clone(
            records
                .frames
                .get(&frame)
                .ok_or_else(|| invalid("unknown presentation frame"))?,
        );
        if stamp != frame.stamp || !frame.source.current(stamp)? {
            return Err(invalid("stale presentation frame"));
        }
        if records.leases.len() >= self.config.max_leases {
            return Err(limit("presentation consumer lease count quota exceeded"));
        }
        let id = next_id()?;
        let info = frame.info()?;
        if !info.current {
            return Err(invalid("stale presentation frame"));
        }
        let result = NativePresentationLeaseInfo {
            lease: id,
            frame: info,
        };
        records.leases.insert(id, frame);
        Ok(result)
    }

    pub fn release_frame(&self, frame: NativePresentationId) -> Result<(), NativeHostError> {
        lock(&self.records)?
            .frames
            .remove(&frame)
            .ok_or_else(|| invalid("unknown presentation frame"))?;
        Ok(())
    }

    pub fn lease_info(
        &self,
        lease: NativePresentationId,
    ) -> Result<NativePresentationLeaseInfo, NativeHostError> {
        let records = lock(&self.records)?;
        let frame = records
            .leases
            .get(&lease)
            .ok_or_else(|| invalid("unknown presentation lease"))?;
        Ok(NativePresentationLeaseInfo {
            lease,
            frame: frame.info()?,
        })
    }

    /// The returned CPU address is immutable and valid until this lease is
    /// released or this registry is dropped. The caller must keep the registry
    /// alive and coordinate pointer use versus lease release and library unload.
    /// Source staleness does not revoke memory from an existing lease.
    pub fn lease_data(
        &self,
        lease: NativePresentationId,
    ) -> Result<(*const u8, usize), NativeHostError> {
        let records = lock(&self.records)?;
        let frame = records
            .leases
            .get(&lease)
            .ok_or_else(|| invalid("unknown presentation lease"))?;
        Ok((frame.bytes.as_ptr(), frame.bytes.len()))
    }

    pub fn release_lease(&self, lease: NativePresentationId) -> Result<(), NativeHostError> {
        lock(&self.records)?
            .leases
            .remove(&lease)
            .ok_or_else(|| invalid("unknown presentation lease"))?;
        Ok(())
    }

    pub fn stats(&self) -> Result<NativePresentationStats, NativeHostError> {
        let records = lock(&self.records)?;
        let retained = lock(&self.budget.retained)?;
        Ok(NativePresentationStats {
            sources: records.sources.len(),
            snapshot_handles: records.snapshots.len(),
            retained_snapshots: retained.snapshots,
            snapshot_estimated_bytes: retained.snapshot_bytes,
            frame_handles: records.frames.len(),
            retained_frames: retained.frames,
            frame_bytes: retained.frame_bytes,
            leases: records.leases.len(),
            in_flight_replays: retained.replays,
        })
    }
}