1use super::*;
2
3impl TextShapingKey {
5 #[must_use]
15 pub fn hash_bytes_128(bytes: &[u8]) -> (u64, u64) {
16 let mut ha: u64 = SHAPING_FNV_OFFSET_A;
17 let mut hb: u64 = SHAPING_FNV_OFFSET_B;
18 for (idx, &b) in bytes.iter().enumerate() {
19 ha ^= b as u64;
20 ha = ha.wrapping_mul(SHAPING_FNV_PRIME);
21 hb ^= (b as u64).wrapping_add((idx as u64).wrapping_mul(31));
22 hb = hb.wrapping_mul(SHAPING_FNV_PRIME);
23 }
24 (ha, hb)
25 }
26
27 #[must_use]
41 pub fn from_text(
42 text: &str,
43 style: TextStyle,
44 locale: &str,
45 rtl: bool,
46 font_chain_revision: u64,
47 ) -> Self {
48 let (text_hash_a, text_hash_b): (u64, u64) = Self::hash_bytes_128(text.as_bytes());
49 let (locale_hash, _): (u64, u64) = Self::hash_bytes_128(locale.as_bytes());
50 Self {
51 text_hash_a,
52 text_hash_b,
53 text_len: text.len() as u32,
54 font_size_bits: style.get_font_size().to_bits(),
55 family: style.get_family(),
56 weight: style.get_weight(),
57 italic: style.get_italic(),
58 letter_spacing_bits: style.get_letter_spacing().to_bits(),
59 locale_hash,
60 rtl,
61 font_chain_revision,
62 }
63 }
64
65 #[must_use]
77 pub fn line_break_key(&self, max_width: Option<f32>, style: TextStyle) -> LineBreakKey {
78 let width_bits: u32 = match max_width {
79 Some(w) if w.is_finite() => ((w.max(0.0) * 10.0).round() as u32).min(u32::MAX - 1),
80 _ => u32::MAX,
81 };
82 let mut ha: u64 = self.get_text_hash_a();
83 let mut hb: u64 = self.get_text_hash_b();
84 let meta: u64 =
85 ((self.get_font_size_bits() as u64) << 32) | (self.get_letter_spacing_bits() as u64);
86 ha = (ha ^ meta).wrapping_mul(SHAPING_FNV_PRIME);
87 hb = (hb ^ self.get_locale_hash() ^ ((self.get_family() as u64) << 8))
88 .wrapping_mul(SHAPING_FNV_PRIME);
89 LineBreakKey {
90 shaping_hash_a: ha,
91 shaping_hash_b: hb,
92 max_width_bits: width_bits,
93 line_height_bits: style.get_line_height().to_bits(),
94 overflow: style.get_overflow(),
95 font_chain_revision: self.get_font_chain_revision(),
96 }
97 }
98}
99
100impl Default for TextShapingCache {
102 fn default() -> Self {
108 Self::with_budget(TEXT_SHAPING_DEFAULT_BUDGET_BYTES)
109 }
110}
111
112impl TextShapingCache {
114 #[must_use]
124 pub fn with_budget(budget_bytes: usize) -> Self {
125 Self {
126 budget_bytes: budget_bytes.max(TEXT_SHAPING_MIN_BUDGET_BYTES),
127 resident_bytes: 0,
128 font_chain_revision: 1,
129 lru_clock: 0,
130 shaped_runs: HashMap::new(),
131 line_breaks: HashMap::new(),
132 shaping_hits: 0,
133 shaping_misses: 0,
134 line_break_hits: 0,
135 line_break_misses: 0,
136 evictions: 0,
137 }
138 }
139
140 pub fn set_memory_budget(&mut self, max_bytes: usize) {
147 self.set_budget_bytes(max_bytes.max(TEXT_SHAPING_MIN_BUDGET_BYTES));
148 self.evict_if_over_budget();
149 }
150
151 pub fn invalidate_font_chain(&mut self, new_revision: u64) -> bool {
162 if self.get_font_chain_revision() == new_revision {
163 return false;
164 }
165 let Self {
166 font_chain_revision,
167 shaped_runs,
168 line_breaks,
169 resident_bytes,
170 ..
171 } = self;
172 *font_chain_revision = new_revision;
173 shaped_runs.clear();
174 line_breaks.clear();
175 *resident_bytes = 0;
176 true
177 }
178
179 pub fn get_or_shape<F>(&mut self, key: TextShapingKey, shape_fn: F) -> &ShapedGlyphRun
191 where
192 F: FnOnce() -> (Vec<f32>, f32),
193 {
194 let hit: bool = self.get_shaped_runs().contains_key(&key);
195 if hit {
196 let Self {
197 shaped_runs,
198 lru_clock,
199 shaping_hits,
200 ..
201 } = self;
202 *lru_clock = lru_clock.wrapping_add(1);
203 *shaping_hits = shaping_hits.saturating_add(1);
204 let tick: u64 = *lru_clock;
205 if let Some(entry) = shaped_runs.get_mut(&key) {
206 entry.set_last_used_tick(tick);
207 }
208 return &shaped_runs[&key];
209 }
210 let (advances, total_width): (Vec<f32>, f32) = shape_fn();
211 let cluster_count: usize = advances.len();
212 let entry_bytes: usize =
213 TEXT_SHAPING_ENTRY_OVERHEAD_BYTES.saturating_add(cluster_count.saturating_mul(4));
214 self.evict_to_make_room(entry_bytes);
215 let Self {
216 shaped_runs,
217 resident_bytes,
218 lru_clock,
219 shaping_misses,
220 ..
221 } = self;
222 *lru_clock = lru_clock.wrapping_add(1);
223 *shaping_misses = shaping_misses.saturating_add(1);
224 let run: ShapedGlyphRun = ShapedGlyphRun {
225 cluster_advances: advances,
226 total_width,
227 cluster_count,
228 byte_size: entry_bytes,
229 last_used_tick: *lru_clock,
230 };
231 let _: Option<ShapedGlyphRun> = shaped_runs.insert(key, run);
232 *resident_bytes = resident_bytes.saturating_add(entry_bytes);
233 &shaped_runs[&key]
234 }
235
236 pub fn get_or_break_lines<F>(&mut self, key: LineBreakKey, break_fn: F) -> &TextLayoutResult
248 where
249 F: FnOnce() -> TextLayoutResult,
250 {
251 let hit: bool = self.get_line_breaks().contains_key(&key);
252 if hit {
253 let Self {
254 line_breaks,
255 lru_clock,
256 line_break_hits,
257 ..
258 } = self;
259 *lru_clock = lru_clock.wrapping_add(1);
260 *line_break_hits = line_break_hits.saturating_add(1);
261 let tick: u64 = *lru_clock;
262 if let Some(entry) = line_breaks.get_mut(&key) {
263 entry.set_last_used_tick(tick);
264 }
265 return line_breaks[&key].get_layout();
266 }
267 let layout: TextLayoutResult = break_fn();
268 let mut entry_bytes: usize = TEXT_SHAPING_ENTRY_OVERHEAD_BYTES;
269 for line in layout.get_lines().iter() {
270 entry_bytes = entry_bytes
271 .saturating_add(32)
272 .saturating_add(line.get_text().len());
273 }
274 self.evict_to_make_room(entry_bytes);
275 let Self {
276 line_breaks,
277 resident_bytes,
278 lru_clock,
279 line_break_misses,
280 ..
281 } = self;
282 *lru_clock = lru_clock.wrapping_add(1);
283 *line_break_misses = line_break_misses.saturating_add(1);
284 let entry: CachedLineBreakEntry = CachedLineBreakEntry {
285 layout,
286 byte_size: entry_bytes,
287 last_used_tick: *lru_clock,
288 };
289 let _: Option<CachedLineBreakEntry> = line_breaks.insert(key, entry);
290 *resident_bytes = resident_bytes.saturating_add(entry_bytes);
291 line_breaks[&key].get_layout()
292 }
293
294 fn evict_to_make_room(&mut self, incoming_bytes: usize) {
301 let budget: usize = self.get_budget_bytes();
302 if self.get_resident_bytes().saturating_add(incoming_bytes) <= budget {
303 return;
304 }
305 let target: usize = budget
306 .saturating_mul(3)
307 .saturating_div(4)
308 .saturating_sub(incoming_bytes);
309 self.evict_lru_to_target(target);
310 }
311
312 pub fn evict_if_over_budget(&mut self) {
318 let budget: usize = self.get_budget_bytes();
319 if self.get_resident_bytes() <= budget {
320 return;
321 }
322 let target: usize = (budget.saturating_mul(3) / 4).max(TEXT_SHAPING_ENTRY_OVERHEAD_BYTES);
323 self.evict_lru_to_target(target);
324 }
325
326 fn evict_lru_to_target(&mut self, target_bytes: usize) {
333 let Self {
334 shaped_runs,
335 line_breaks,
336 resident_bytes,
337 evictions,
338 ..
339 } = self;
340 let mut victims: Vec<ShapingEvictVictim> =
341 Vec::with_capacity(shaped_runs.len() + line_breaks.len());
342 for (k, v) in shaped_runs.iter() {
343 victims.push(ShapingEvictVictim {
344 last_used_tick: v.get_last_used_tick(),
345 is_shape: true,
346 shape_key: *k,
347 line_break_key: LineBreakKey::default(),
348 byte_size: v.get_byte_size(),
349 });
350 }
351 for (k, v) in line_breaks.iter() {
352 victims.push(ShapingEvictVictim {
353 last_used_tick: v.get_last_used_tick(),
354 is_shape: false,
355 shape_key: TextShapingKey::default(),
356 line_break_key: *k,
357 byte_size: v.get_byte_size(),
358 });
359 }
360 victims.sort_unstable_by_key(|item: &ShapingEvictVictim| item.last_used_tick);
361 for victim in victims.into_iter() {
362 if *resident_bytes <= target_bytes {
363 break;
364 }
365 if victim.is_shape {
366 if shaped_runs.remove(&victim.shape_key).is_some() {
367 *resident_bytes = resident_bytes.saturating_sub(victim.byte_size);
368 *evictions = evictions.saturating_add(1);
369 }
370 } else if line_breaks.remove(&victim.line_break_key).is_some() {
371 *resident_bytes = resident_bytes.saturating_sub(victim.byte_size);
372 *evictions = evictions.saturating_add(1);
373 }
374 }
375 }
376
377 #[must_use]
387 pub fn total_entries(&self) -> usize {
388 self.get_shaped_runs()
389 .len()
390 .saturating_add(self.get_line_breaks().len())
391 }
392}