use std::sync::Arc;
use super::SceneCache;
#[derive(Clone, Copy, Debug)]
#[repr(usize)]
pub enum CachePartition {
Coverage,
Paragraph,
Glyph,
Gradient,
Formula,
Ink,
Scene,
}
#[derive(Clone, Debug)]
pub struct RenderCache {
partitions: [Arc<SceneCache>; 7],
}
impl Default for RenderCache {
fn default() -> Self {
Self {
partitions: std::array::from_fn(|index| {
let (max_entries, max_bytes) = match index {
0 => (2048, 16 * 1024 * 1024),
1 => (256, 8 * 1024 * 1024),
2 => (2048, 16 * 1024 * 1024),
3 => (64, 8 * 1024 * 1024),
5 => (64, 32 * 1024 * 1024),
6 => (256, 8 * 1024 * 1024),
_ => (32, 32 * 1024 * 1024),
};
Arc::new(SceneCache::with_limits(CacheLimits {
max_entries,
max_bytes,
}))
}),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CacheLimits {
pub max_entries: usize,
pub max_bytes: usize,
}
impl RenderCache {
pub fn with_limits(limits: CacheLimits) -> Self {
Self {
partitions: std::array::from_fn(|_| Arc::new(SceneCache::with_limits(limits))),
}
}
pub fn partition(&self, partition: CachePartition) -> &Arc<SceneCache> {
&self.partitions[partition as usize]
}
pub fn fork_partition(&self, partition: CachePartition) -> Self {
let mut cache = self.clone();
let source = self.partition(partition);
cache.partitions[partition as usize] = Arc::new(SceneCache::with_limits(CacheLimits {
max_entries: source.capacity(),
max_bytes: source.max_bytes(),
}));
cache
}
pub fn clear(&self) {
for partition in &self.partitions {
partition.clear();
}
}
}