Reuse entries in text::Cache in iced_wgpu

This commit is contained in:
Héctor Ramón Jiménez 2023-06-28 00:35:37 +02:00
parent af62ec1c87
commit 78ad365db2
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
10 changed files with 69 additions and 46 deletions

View file

@ -64,8 +64,8 @@ impl text::Renderer for Null {
_font: Font, _font: Font,
_bounds: Size, _bounds: Size,
_shaping: text::Shaping, _shaping: text::Shaping,
) -> (f32, f32) { ) -> Size {
(0.0, 20.0) Size::new(0.0, 20.0)
} }
fn hit_test( fn hit_test(

View file

@ -163,7 +163,7 @@ pub trait Renderer: crate::Renderer {
font: Self::Font, font: Self::Font,
bounds: Size, bounds: Size,
shaping: Shaping, shaping: Shaping,
) -> (f32, f32); ) -> Size;
/// Measures the width of the text as if it were laid out in a single line. /// Measures the width of the text as if it were laid out in a single line.
fn measure_width( fn measure_width(
@ -173,7 +173,7 @@ pub trait Renderer: crate::Renderer {
font: Self::Font, font: Self::Font,
shaping: Shaping, shaping: Shaping,
) -> f32 { ) -> f32 {
let (width, _) = self.measure( let bounds = self.measure(
content, content,
size, size,
LineHeight::Absolute(Pixels(size)), LineHeight::Absolute(Pixels(size)),
@ -182,7 +182,7 @@ pub trait Renderer: crate::Renderer {
shaping, shaping,
); );
width bounds.width
} }
/// Tests whether the provided point is within the boundaries of text /// Tests whether the provided point is within the boundaries of text

View file

@ -5,7 +5,7 @@ use crate::mouse;
use crate::renderer; use crate::renderer;
use crate::text; use crate::text;
use crate::widget::Tree; use crate::widget::Tree;
use crate::{Color, Element, Layout, Length, Pixels, Rectangle, Size, Widget}; use crate::{Color, Element, Layout, Length, Pixels, Rectangle, Widget};
use std::borrow::Cow; use std::borrow::Cow;
@ -139,18 +139,16 @@ where
let size = self.size.unwrap_or_else(|| renderer.default_size()); let size = self.size.unwrap_or_else(|| renderer.default_size());
let bounds = limits.max(); let bounds = renderer.measure(
let (width, height) = renderer.measure(
&self.content, &self.content,
size, size,
self.line_height, self.line_height,
self.font.unwrap_or_else(|| renderer.default_font()), self.font.unwrap_or_else(|| renderer.default_font()),
bounds, limits.max(),
self.shaping, self.shaping,
); );
let size = limits.resolve(Size::new(width, height)); let size = limits.resolve(bounds);
layout::Node::new(size) layout::Node::new(size)
} }

View file

@ -38,7 +38,7 @@ pub trait Text {
font: Font, font: Font,
bounds: Size, bounds: Size,
shaping: text::Shaping, shaping: text::Shaping,
) -> (f32, f32); ) -> Size;
/// Tests whether the provided point is within the boundaries of [`Text`] /// Tests whether the provided point is within the boundaries of [`Text`]
/// laid out with the given parameters, returning information about /// laid out with the given parameters, returning information about

View file

@ -133,7 +133,7 @@ where
font: Font, font: Font,
bounds: Size, bounds: Size,
shaping: text::Shaping, shaping: text::Shaping,
) -> (f32, f32) { ) -> Size {
self.backend().measure( self.backend().measure(
content, content,
size, size,

View file

@ -42,7 +42,7 @@ impl backend::Text for Backend {
font: Font, font: Font,
bounds: Size, bounds: Size,
shaping: text::Shaping, shaping: text::Shaping,
) -> (f32, f32) { ) -> Size {
delegate!( delegate!(
self, self,
backend, backend,

View file

@ -787,7 +787,7 @@ impl backend::Text for Backend {
font: Font, font: Font,
bounds: Size, bounds: Size,
shaping: text::Shaping, shaping: text::Shaping,
) -> (f32, f32) { ) -> Size {
self.text_pipeline.measure( self.text_pipeline.measure(
contents, contents,
size, size,

View file

@ -138,7 +138,7 @@ impl Pipeline {
font: Font, font: Font,
bounds: Size, bounds: Size,
shaping: Shaping, shaping: Shaping,
) -> (f32, f32) { ) -> Size {
let mut measurement_cache = self.cache.borrow_mut(); let mut measurement_cache = self.cache.borrow_mut();
let line_height = f32::from(line_height.to_absolute(Pixels(size))); let line_height = f32::from(line_height.to_absolute(Pixels(size)));
@ -162,7 +162,7 @@ impl Pipeline {
(i + 1, buffer.line_w.max(max)) (i + 1, buffer.line_w.max(max))
}); });
(max_width, line_height * total_lines as f32) Size::new(max_width, line_height * total_lines as f32)
} }
pub fn hit_test( pub fn hit_test(

View file

@ -355,7 +355,7 @@ impl backend::Text for Backend {
font: Font, font: Font,
bounds: Size, bounds: Size,
shaping: core::text::Shaping, shaping: core::text::Shaping,
) -> (f32, f32) { ) -> Size {
self.text_pipeline.measure( self.text_pipeline.measure(
contents, contents,
size, size,

View file

@ -113,15 +113,13 @@ impl Pipeline {
.iter() .iter()
.zip(keys.iter()) .zip(keys.iter())
.filter_map(|(section, key)| { .filter_map(|(section, key)| {
let buffer = cache.get(key).expect("Get cached buffer"); let entry = cache.get(key).expect("Get cached buffer");
let x = section.bounds.x * scale_factor; let x = section.bounds.x * scale_factor;
let y = section.bounds.y * scale_factor; let y = section.bounds.y * scale_factor;
let (max_width, total_height) = measure(buffer); let max_width = entry.bounds.width * scale_factor;
let total_height = entry.bounds.height * scale_factor;
let max_width = max_width * scale_factor;
let total_height = total_height * scale_factor;
let left = match section.horizontal_alignment { let left = match section.horizontal_alignment {
alignment::Horizontal::Left => x, alignment::Horizontal::Left => x,
@ -145,7 +143,7 @@ impl Pipeline {
let clip_bounds = bounds.intersection(&section_bounds)?; let clip_bounds = bounds.intersection(&section_bounds)?;
Some(glyphon::TextArea { Some(glyphon::TextArea {
buffer, buffer: &entry.buffer,
left, left,
top, top,
scale: scale_factor, scale: scale_factor,
@ -239,12 +237,12 @@ impl Pipeline {
font: Font, font: Font,
bounds: Size, bounds: Size,
shaping: Shaping, shaping: Shaping,
) -> (f32, f32) { ) -> Size {
let mut measurement_cache = self.cache.borrow_mut(); let mut measurement_cache = self.cache.borrow_mut();
let line_height = f32::from(line_height.to_absolute(Pixels(size))); let line_height = f32::from(line_height.to_absolute(Pixels(size)));
let (_, paragraph) = measurement_cache.allocate( let (_, entry) = measurement_cache.allocate(
&mut self.font_system.borrow_mut(), &mut self.font_system.borrow_mut(),
Key { Key {
content, content,
@ -256,7 +254,7 @@ impl Pipeline {
}, },
); );
measure(paragraph) entry.bounds
} }
pub fn hit_test( pub fn hit_test(
@ -274,7 +272,7 @@ impl Pipeline {
let line_height = f32::from(line_height.to_absolute(Pixels(size))); let line_height = f32::from(line_height.to_absolute(Pixels(size)));
let (_, paragraph) = measurement_cache.allocate( let (_, entry) = measurement_cache.allocate(
&mut self.font_system.borrow_mut(), &mut self.font_system.borrow_mut(),
Key { Key {
content, content,
@ -286,20 +284,20 @@ impl Pipeline {
}, },
); );
let cursor = paragraph.hit(point.x, point.y)?; let cursor = entry.buffer.hit(point.x, point.y)?;
Some(Hit::CharOffset(cursor.index)) Some(Hit::CharOffset(cursor.index))
} }
} }
fn measure(buffer: &glyphon::Buffer) -> (f32, f32) { fn measure(buffer: &glyphon::Buffer) -> Size {
let (width, total_lines) = buffer let (width, total_lines) = buffer
.layout_runs() .layout_runs()
.fold((0.0, 0usize), |(width, total_lines), run| { .fold((0.0, 0usize), |(width, total_lines), run| {
(run.line_w.max(width), total_lines + 1) (run.line_w.max(width), total_lines + 1)
}); });
(width, total_lines as f32 * buffer.metrics().line_height) Size::new(width, total_lines as f32 * buffer.metrics().line_height)
} }
fn to_family(family: font::Family) -> glyphon::Family<'static> { fn to_family(family: font::Family) -> glyphon::Family<'static> {
@ -349,11 +347,17 @@ fn to_shaping(shaping: Shaping) -> glyphon::Shaping {
} }
struct Cache { struct Cache {
entries: FxHashMap<KeyHash, glyphon::Buffer>, entries: FxHashMap<KeyHash, Entry>,
bound_entries: FxHashMap<KeyHash, KeyHash>,
recently_used: FxHashSet<KeyHash>, recently_used: FxHashSet<KeyHash>,
hasher: HashBuilder, hasher: HashBuilder,
} }
struct Entry {
buffer: glyphon::Buffer,
bounds: Size,
}
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
type HashBuilder = twox_hash::RandomXxHashBuilder64; type HashBuilder = twox_hash::RandomXxHashBuilder64;
@ -364,12 +368,13 @@ impl Cache {
fn new() -> Self { fn new() -> Self {
Self { Self {
entries: FxHashMap::default(), entries: FxHashMap::default(),
bound_entries: FxHashMap::default(),
recently_used: FxHashSet::default(), recently_used: FxHashSet::default(),
hasher: HashBuilder::default(), hasher: HashBuilder::default(),
} }
} }
fn get(&self, key: &KeyHash) -> Option<&glyphon::Buffer> { fn get(&self, key: &KeyHash) -> Option<&Entry> {
self.entries.get(key) self.entries.get(key)
} }
@ -377,20 +382,15 @@ impl Cache {
&mut self, &mut self,
font_system: &mut glyphon::FontSystem, font_system: &mut glyphon::FontSystem,
key: Key<'_>, key: Key<'_>,
) -> (KeyHash, &mut glyphon::Buffer) { ) -> (KeyHash, &mut Entry) {
let hash = { let hash = key.hash(self.hasher.build_hasher());
let mut hasher = self.hasher.build_hasher();
key.content.hash(&mut hasher); if let Some(bound_hash) = self.bound_entries.get(&hash) {
key.size.to_bits().hash(&mut hasher); let _ = self.recently_used.insert(hash);
key.line_height.to_bits().hash(&mut hasher); let _ = self.recently_used.insert(*bound_hash);
key.font.hash(&mut hasher);
key.bounds.width.to_bits().hash(&mut hasher);
key.bounds.height.to_bits().hash(&mut hasher);
key.shaping.hash(&mut hasher);
hasher.finish() return (*bound_hash, self.entries.get_mut(&bound_hash).unwrap());
}; }
if let hash_map::Entry::Vacant(entry) = self.entries.entry(hash) { if let hash_map::Entry::Vacant(entry) = self.entries.entry(hash) {
let metrics = glyphon::Metrics::new(key.size, key.line_height); let metrics = glyphon::Metrics::new(key.size, key.line_height);
@ -411,7 +411,16 @@ impl Cache {
to_shaping(key.shaping), to_shaping(key.shaping),
); );
let _ = entry.insert(buffer); let bounds = measure(&buffer);
let _ = entry.insert(Entry { buffer, bounds });
if key.bounds != bounds {
let _ = self.bound_entries.insert(
Key { bounds, ..key }.hash(self.hasher.build_hasher()),
hash,
);
}
} }
let _ = self.recently_used.insert(hash); let _ = self.recently_used.insert(hash);
@ -422,6 +431,8 @@ impl Cache {
fn trim(&mut self) { fn trim(&mut self) {
self.entries self.entries
.retain(|key, _| self.recently_used.contains(key)); .retain(|key, _| self.recently_used.contains(key));
self.bound_entries
.retain(|key, _| self.recently_used.contains(key));
self.recently_used.clear(); self.recently_used.clear();
} }
@ -437,4 +448,18 @@ struct Key<'a> {
shaping: Shaping, shaping: Shaping,
} }
impl Key<'_> {
fn hash<H: Hasher>(self, mut hasher: H) -> KeyHash {
self.content.hash(&mut hasher);
self.size.to_bits().hash(&mut hasher);
self.line_height.to_bits().hash(&mut hasher);
self.font.hash(&mut hasher);
self.bounds.width.to_bits().hash(&mut hasher);
self.bounds.height.to_bits().hash(&mut hasher);
self.shaping.hash(&mut hasher);
hasher.finish()
}
}
type KeyHash = u64; type KeyHash = u64;