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 compute cache shared by sessions and export workers; unlike a
28/// [`crate::ResourceStore`], its entries are recomputable and may be evicted.
29/// Each partition has its own entry and logical-byte allowance; external Arc pins
30/// and allocator overhead are not included. Computation runs outside cache locks.
31#[derive(Clone, Debug)]
32pub struct RenderCache {
33    partitions: [Arc<SceneCache>; 7],
34}
35
36impl Default for RenderCache {
37    fn default() -> Self {
38        Self {
39            partitions: std::array::from_fn(|index| {
40                let (max_entries, max_bytes) = match index {
41                    0 => (2048, 16 * 1024 * 1024),
42                    1 => (256, 8 * 1024 * 1024),
43                    2 => (2048, 16 * 1024 * 1024),
44                    3 => (64, 8 * 1024 * 1024),
45                    5 => (64, 32 * 1024 * 1024),
46                    6 => (256, 8 * 1024 * 1024),
47                    _ => (32, 32 * 1024 * 1024),
48                };
49                Arc::new(SceneCache::with_limits(CacheLimits {
50                    max_entries,
51                    max_bytes,
52                }))
53            }),
54        }
55    }
56}
57
58/// Entry-count and payload-byte allowance of a compute cache; resource stores use [`crate::ResourceLimits`].
59#[derive(Clone, Copy, Debug, PartialEq, Eq)]
60pub struct CacheLimits {
61    /// Maximum retained entries; zero disables caching where the cache allows it.
62    pub max_entries: usize,
63    /// Caller-declared retained payload bytes.
64    pub max_bytes: usize,
65}
66
67impl RenderCache {
68    /// Creates independent LRU partitions with the supplied per-partition limits.
69    pub fn with_limits(limits: CacheLimits) -> Self {
70        Self {
71            partitions: std::array::from_fn(|_| Arc::new(SceneCache::with_limits(limits))),
72        }
73    }
74
75    /// Shared storage for one domain. Costed entries must include owned key payloads.
76    pub fn partition(&self, partition: CachePartition) -> &Arc<SceneCache> {
77        &self.partitions[partition as usize]
78    }
79
80    /// Isolates one domain while sharing the other partitions and preserving limits.
81    pub fn fork_partition(&self, partition: CachePartition) -> Self {
82        let mut cache = self.clone();
83        let source = self.partition(partition);
84        cache.partitions[partition as usize] = Arc::new(SceneCache::with_limits(CacheLimits {
85            max_entries: source.capacity(),
86            max_bytes: source.max_bytes(),
87        }));
88        cache
89    }
90
91    /// Releases all retained entries without invalidating outstanding value handles.
92    pub fn clear(&self) {
93        for partition in &self.partitions {
94            partition.clear();
95        }
96    }
97}