|
| 1 | +//! Opaque render state cached per footprint. |
| 2 | +//! |
| 3 | +//! ```ignore |
| 4 | +//! let state: SomeState = cache.take(ctx.footprint()).unwrap_or_default(); |
| 5 | +//! // ...render, freely mutating the state |
| 6 | +//! cache.store(ctx.footprint(), state); |
| 7 | +//! ``` |
| 8 | +
|
| 9 | +use core_types::transform::Footprint; |
| 10 | +use glam::DMat2; |
| 11 | +use std::sync::{Arc, Mutex}; |
| 12 | + |
| 13 | +const STALE_EPOCHS: u64 = 2; |
| 14 | +const MAX_VIEWS: usize = 3; |
| 15 | + |
| 16 | +#[derive(Clone)] |
| 17 | +pub struct BrushCache { |
| 18 | + state: Arc<Mutex<State>>, |
| 19 | + nonce: u64, // Avoid deduplication of cache entries across different brush nodes. |
| 20 | +} |
| 21 | + |
| 22 | +impl Default for BrushCache { |
| 23 | + fn default() -> Self { |
| 24 | + Self { |
| 25 | + state: Default::default(), |
| 26 | + nonce: core_types::uuid::generate_uuid(), |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl BrushCache { |
| 32 | + pub fn take<S: std::any::Any + Send + Sync>(&self, footprint: &Footprint) -> Option<S> { |
| 33 | + let mut guard = self.state.lock().unwrap(); |
| 34 | + let state = guard.take(footprint)?; |
| 35 | + match state.downcast() { |
| 36 | + Ok(state) => Some(*state), |
| 37 | + Err(state) => { |
| 38 | + guard.store(footprint, state); |
| 39 | + None |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + pub fn store<S: std::any::Any + Send + Sync>(&self, footprint: &Footprint, state: S) { |
| 45 | + self.state.lock().unwrap().store(footprint, Box::new(state)); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl PartialEq for BrushCache { |
| 50 | + fn eq(&self, _: &Self) -> bool { |
| 51 | + true |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl std::fmt::Debug for BrushCache { |
| 56 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 57 | + f.debug_struct("BrushCache").field("slots", &self.state.lock().unwrap().slots.len()).finish() |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl core_types::CacheHash for BrushCache { |
| 62 | + fn cache_hash<H: core::hash::Hasher>(&self, state: &mut H) { |
| 63 | + state.write_u64(self.nonce); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +unsafe impl dyn_any::StaticType for BrushCache { |
| 68 | + type Static = BrushCache; |
| 69 | +} |
| 70 | + |
| 71 | +#[cfg(feature = "serde")] |
| 72 | +impl serde::Serialize for BrushCache { |
| 73 | + fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { |
| 74 | + serializer.serialize_unit() |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[cfg(feature = "serde")] |
| 79 | +impl<'de> serde::Deserialize<'de> for BrushCache { |
| 80 | + fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
| 81 | + serde::de::IgnoredAny::deserialize(deserializer)?; |
| 82 | + Ok(Self::default()) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +type BoxedData = Box<dyn std::any::Any + Send + Sync>; |
| 87 | + |
| 88 | +#[derive(Default)] |
| 89 | +struct State { |
| 90 | + epoch: u64, |
| 91 | + slots: Vec<Slot>, |
| 92 | +} |
| 93 | + |
| 94 | +struct Slot { |
| 95 | + footprint: Footprint, |
| 96 | + epoch: u64, |
| 97 | + data: BoxedData, |
| 98 | +} |
| 99 | + |
| 100 | +impl Slot { |
| 101 | + fn view(&self) -> DMat2 { |
| 102 | + self.footprint.transform.matrix2 |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl State { |
| 107 | + fn take(&mut self, footprint: &Footprint) -> Option<BoxedData> { |
| 108 | + self.touch(footprint.transform.matrix2); |
| 109 | + let index = self.slots.iter().position(|slot| slot.footprint == *footprint); |
| 110 | + let hit = index.map(|index| { |
| 111 | + let slot = self.slots.remove(index); |
| 112 | + if slot.epoch == self.epoch { |
| 113 | + self.epoch += 1; |
| 114 | + } |
| 115 | + slot.data |
| 116 | + }); |
| 117 | + self.retire(); |
| 118 | + hit |
| 119 | + } |
| 120 | + |
| 121 | + fn store(&mut self, footprint: &Footprint, data: BoxedData) { |
| 122 | + self.touch(footprint.transform.matrix2); |
| 123 | + self.slots.retain(|slot| slot.footprint != *footprint); |
| 124 | + self.slots.push(Slot { |
| 125 | + footprint: *footprint, |
| 126 | + epoch: self.epoch, |
| 127 | + data, |
| 128 | + }); |
| 129 | + self.retire(); |
| 130 | + } |
| 131 | + |
| 132 | + fn touch(&mut self, view: DMat2) { |
| 133 | + self.slots.sort_by_key(|slot| slot.view() == view); |
| 134 | + } |
| 135 | + |
| 136 | + fn retire(&mut self) { |
| 137 | + let epoch = self.epoch; |
| 138 | + self.slots.retain(|slot| epoch - slot.epoch < STALE_EPOCHS); |
| 139 | + while self.slots.chunk_by(|a, b| a.view() == b.view()).count() > MAX_VIEWS { |
| 140 | + let front = self.slots[0].view(); |
| 141 | + let group = self.slots.iter().take_while(|slot| slot.view() == front).count(); |
| 142 | + self.slots.drain(..group.max(1)); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +#[cfg(test)] |
| 148 | +mod tests { |
| 149 | + use super::*; |
| 150 | + use core_types::transform::RenderQuality; |
| 151 | + use glam::{DAffine2, DVec2, UVec2}; |
| 152 | + |
| 153 | + struct Dummy; |
| 154 | + |
| 155 | + fn view(zoom: f64, rotation: f64, pan: DVec2) -> Footprint { |
| 156 | + Footprint { |
| 157 | + transform: DAffine2::from_scale_angle_translation(DVec2::splat(zoom), rotation, pan), |
| 158 | + resolution: UVec2::new(1920, 1080), |
| 159 | + quality: RenderQuality::Full, |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + fn thumbnail(zoom: f64) -> Footprint { |
| 164 | + Footprint { |
| 165 | + resolution: UVec2::new(150, 150), |
| 166 | + ..view(zoom, 0., DVec2::ZERO) |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + fn live(cache: &BrushCache) -> usize { |
| 171 | + cache.state.lock().unwrap().slots.len() |
| 172 | + } |
| 173 | + |
| 174 | + fn render(cache: &BrushCache, footprint: &Footprint) -> bool { |
| 175 | + let hit = cache.take::<Dummy>(footprint).is_some(); |
| 176 | + cache.store(footprint, Dummy); |
| 177 | + hit |
| 178 | + } |
| 179 | + |
| 180 | + #[test] |
| 181 | + fn continuous_zoom_is_bounded_by_views() { |
| 182 | + let cache = BrushCache::default(); |
| 183 | + for step in 0..100 { |
| 184 | + render(&cache, &view(1. + step as f64 * 0.01, 0., DVec2::ZERO)); |
| 185 | + } |
| 186 | + assert!(live(&cache) <= MAX_VIEWS); |
| 187 | + } |
| 188 | + |
| 189 | + #[test] |
| 190 | + fn continuous_rotation_is_bounded_by_views() { |
| 191 | + let cache = BrushCache::default(); |
| 192 | + for step in 0..100 { |
| 193 | + render(&cache, &view(2., step as f64 * 0.01, DVec2::ZERO)); |
| 194 | + } |
| 195 | + assert!(live(&cache) <= MAX_VIEWS); |
| 196 | + } |
| 197 | + |
| 198 | + #[test] |
| 199 | + fn zooming_reclaims_pan_slots() { |
| 200 | + let cache = BrushCache::default(); |
| 201 | + for step in 0..30 { |
| 202 | + render(&cache, &view(1., 0., DVec2::splat(step as f64 * 100.))); |
| 203 | + } |
| 204 | + for step in 1..=3 { |
| 205 | + render(&cache, &view(1. + step as f64, 0., DVec2::ZERO)); |
| 206 | + } |
| 207 | + assert_eq!(live(&cache), 3); |
| 208 | + } |
| 209 | + |
| 210 | + #[test] |
| 211 | + fn frames_may_hold_many_footprints_per_view() { |
| 212 | + let cache = BrushCache::default(); |
| 213 | + let footprints: Vec<_> = (0..5).map(|step| view(1., 0., DVec2::splat(step as f64 * 100.))).collect(); |
| 214 | + for frame in 0..10 { |
| 215 | + for footprint in &footprints { |
| 216 | + assert_eq!(render(&cache, footprint), frame > 0, "footprint evicted while its frame still renders it"); |
| 217 | + } |
| 218 | + } |
| 219 | + assert_eq!(live(&cache), 5); |
| 220 | + } |
| 221 | + |
| 222 | + #[test] |
| 223 | + fn thumbnail_drift_is_bounded_and_keeps_the_view() { |
| 224 | + let cache = BrushCache::default(); |
| 225 | + for step in 0..100 { |
| 226 | + render(&cache, &thumbnail(1. + step as f64 * 0.001)); |
| 227 | + } |
| 228 | + assert!(live(&cache) <= MAX_VIEWS); |
| 229 | + |
| 230 | + let viewport = view(2., 0., DVec2::ZERO); |
| 231 | + render(&cache, &viewport); |
| 232 | + for step in 0..50 { |
| 233 | + render(&cache, &thumbnail(2. + step as f64 * 0.001)); |
| 234 | + assert!(render(&cache, &viewport), "thumbnail churn evicted the viewport slot"); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + #[test] |
| 239 | + fn settled_view_retires_stale_slots() { |
| 240 | + let cache = BrushCache::default(); |
| 241 | + for step in 0..3 { |
| 242 | + render(&cache, &view(1. + step as f64, 0., DVec2::ZERO)); |
| 243 | + } |
| 244 | + assert_eq!(live(&cache), 3); |
| 245 | + for _ in 0..STALE_EPOCHS { |
| 246 | + render(&cache, &view(1., 0., DVec2::ZERO)); |
| 247 | + } |
| 248 | + assert_eq!(live(&cache), 1); |
| 249 | + } |
| 250 | +} |
0 commit comments