Skip to main content

motion_core/authoring/context/
partition.rs

1//! Rendering-cache partitions and the entry and byte limits shared by cache constructors.
2
3use std::sync::Arc;
4
5use super::SceneCache;
6
7/// Independently bounded pure rendering-cache domains.
8#[derive(Clone, Copy, Debug)]
9#[repr(usize)]
10pub enum CachePartition {
11    /// Device-space coverage keyed by geometry, transform and tolerance.
12    Coverage,
13    /// Shaped paragraph layouts keyed by text and font environment.
14    Paragraph,
15    /// Font-unit outlines keyed by immutable font identity and glyph.
16    Glyph,
17    /// Linear color lookup tables keyed by gradient content.
18    Gradient,
19    /// Compiled formula content keyed by source and compiler configuration.
20    Formula,
21    /// Full-path preview ink atlases keyed by material parameters and device density.
22    Ink,
23    /// Explicit pure author computations shared by render workers and setting changes.
24    Scene,
25}
26
27/// Cloneable rendering cache shared by sessions and export workers.
28/// Each partition has its own entry and logical-byte allowance; external Arc pins
29/// and allocator overhead are not included. Computation runs outside cache locks.
30#[derive(Clone, Debug)]
31pub struct RenderCache {
32    partitions: [Arc<SceneCache>; 7],
33}
34
35impl Default for RenderCache {
36    fn default() -> Self {
37        Self {
38            partitions: std::array::from_fn(|index| {
39                let (entries, bytes) = match index {
40                    0 => (2048, 16 * 1024 * 1024),
41                    1 => (256, 8 * 1024 * 1024),
42                    2 => (2048, 16 * 1024 * 1024),
43                    3 => (64, 8 * 1024 * 1024),
44                    5 => (64, 32 * 1024 * 1024),
45                    6 => (256, 8 * 1024 * 1024),
46                    _ => (32, 32 * 1024 * 1024),
47                };
48                Arc::new(SceneCache::with_limits(CacheLimits { entries, bytes }))
49            }),
50        }
51    }
52}
53
54/// Entry-count and payload-byte allowance shared by the cache constructors.
55#[derive(Clone, Copy, Debug, PartialEq, Eq)]
56pub struct CacheLimits {
57    /// Maximum retained entries; zero disables caching where the cache allows it.
58    pub entries: usize,
59    /// Caller-declared retained payload bytes.
60    pub bytes: usize,
61}
62
63impl RenderCache {
64    /// Creates independent LRU partitions with the supplied per-partition limits.
65    pub fn with_limits(limits: CacheLimits) -> Self {
66        Self {
67            partitions: std::array::from_fn(|_| Arc::new(SceneCache::with_limits(limits))),
68        }
69    }
70
71    /// Shared storage for one domain. Costed entries must include owned key payloads.
72    pub fn partition(&self, partition: CachePartition) -> &Arc<SceneCache> {
73        &self.partitions[partition as usize]
74    }
75
76    /// Isolates one domain while sharing the other partitions and preserving limits.
77    pub fn fork_partition(&self, partition: CachePartition) -> Self {
78        let mut cache = self.clone();
79        let source = self.partition(partition);
80        cache.partitions[partition as usize] = Arc::new(SceneCache::with_limits(CacheLimits {
81            entries: source.capacity(),
82            bytes: source.max_bytes(),
83        }));
84        cache
85    }
86
87    /// Releases all retained entries without invalidating outstanding value handles.
88    pub fn clear(&self) {
89        for partition in &self.partitions {
90            partition.clear();
91        }
92    }
93}