Skip to content

Commit 16d8fe7

Browse files
committed
use RWLock to font library instead
1 parent 2c0dc19 commit 16d8fe7

File tree

7 files changed

+69
-57
lines changed

7 files changed

+69
-57
lines changed

frontends/rioterm/src/application.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ impl ApplicationHandler<EventPayload> for Application<'_> {
11931193
route.window.screen.set_modifiers(*modifiers);
11941194

11951195
// Process the key event
1196-
route.window.screen.process_key_event(&key);
1196+
route.window.screen.process_key_event(key);
11971197

11981198
// Restore the original modifiers
11991199
route.window.screen.set_modifiers(original_modifiers);

frontends/rioterm/src/renderer/font_cache.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ impl FontCache {
105105
),
106106
];
107107

108-
if let Some(mut font_ctx) = font_context.inner.try_lock() {
108+
if let Some(font_ctx) = font_context.inner.try_read() {
109109
for &ch in &common_chars {
110110
for &attrs in &common_attrs {
111111
let key = (ch, attrs);
112-
if self.get(&mut key.clone()).is_none() {
112+
if self.get(&key).is_none() {
113113
let style = rio_backend::sugarloaf::FragmentStyle {
114114
font_attrs: attrs,
115115
..Default::default()

frontends/rioterm/src/renderer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ impl Renderer {
371371

372372
// Batch font lookups with a single lock acquisition
373373
if !font_lookups.is_empty() {
374-
let mut font_ctx = self.font_context.inner.lock();
374+
let font_ctx = self.font_context.inner.read();
375375
for (style_index, square_content, font_attrs) in font_lookups {
376376
let mut width = square_content.width().unwrap_or(1) as f32;
377377
let style = &mut styles_and_chars[style_index].0;
@@ -721,7 +721,7 @@ impl Renderer {
721721

722722
// Batch font lookups with a single lock acquisition
723723
if !font_lookups.is_empty() {
724-
let mut font_ctx = self.font_context.inner.lock();
724+
let font_ctx = self.font_context.inner.read();
725725
for (style_index, character) in font_lookups {
726726
let mut width = character.width().unwrap_or(1) as f32;
727727
let char_style = &mut char_styles[style_index].0;

sugarloaf/src/components/filters/runtime/error.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ pub enum FilterChainError {
1818
#[error("shader preprocess error")]
1919
ShaderPreprocessError(#[from] PreprocessError),
2020
#[error("shader compile error")]
21-
ShaderCompileError(#[from] ShaderCompileError),
21+
ShaderCompileError(#[source] Box<ShaderCompileError>),
2222
#[error("shader reflect error")]
23-
ShaderReflectError(#[from] ShaderReflectError),
23+
ShaderReflectError(#[source] Box<ShaderReflectError>),
2424
#[error("lut loading error")]
2525
LutLoadError(#[from] ImageError),
2626
#[error("unreachable")]
@@ -29,3 +29,15 @@ pub enum FilterChainError {
2929

3030
/// Result type for wgpu filter chains.
3131
pub type Result<T> = std::result::Result<T, FilterChainError>;
32+
33+
impl From<ShaderCompileError> for FilterChainError {
34+
fn from(error: ShaderCompileError) -> Self {
35+
FilterChainError::ShaderCompileError(Box::new(error))
36+
}
37+
}
38+
39+
impl From<ShaderReflectError> for FilterChainError {
40+
fn from(error: ShaderReflectError) -> Self {
41+
FilterChainError::ShaderReflectError(Box::new(error))
42+
}
43+
}

sugarloaf/src/components/rich_text/image_cache/glyph.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use super::cache::ImageCache;
22
use super::{AddImage, ImageData, ImageId, ImageLocation};
33
use crate::font::FontLibrary;
4-
use crate::font_introspector::scale::{
5-
image::{Content, Image as GlyphImage},
6-
*,
7-
};
84
use crate::font_introspector::zeno::Format;
5+
use crate::font_introspector::{
6+
scale::{
7+
image::{Content, Image as GlyphImage},
8+
*,
9+
},
10+
FontRef,
11+
};
912
use core::borrow::Borrow;
1013
use core::hash::{Hash, Hasher};
1114
use rustc_hash::FxHashMap;
@@ -120,16 +123,23 @@ impl GlyphCacheSession<'_> {
120123
}
121124

122125
self.scaled_image.data.clear();
123-
let mut font_library_data = self.font_library.inner.lock();
126+
let font_library_data = self.font_library.inner.read();
124127
let enable_hint = font_library_data.hinting;
125128
let font_data = font_library_data.get(&self.font);
126129
let should_embolden = font_data.should_embolden;
127130
let should_italicize = font_data.should_italicize;
128131

129-
if let Some(data) = font_library_data.get_data(&self.font) {
132+
if let Some((shared_data, offset, cache_key)) =
133+
font_library_data.get_data(&self.font)
134+
{
135+
let font_ref = FontRef {
136+
data: shared_data.as_ref(),
137+
offset,
138+
key: cache_key,
139+
};
130140
let mut scaler = self
131141
.scale_context
132-
.builder(data)
142+
.builder(font_ref)
133143
// With the advent of high-DPI displays (displays with >300 pixels per inch),
134144
// font hinting has become less relevant, as aliasing effects become
135145
// un-noticeable to the human eye.

sugarloaf/src/font/mod.rs

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ use crate::font_introspector::text::Script;
1616
use crate::font_introspector::{CacheKey, FontRef, Synthesis};
1717
use crate::layout::FragmentStyle;
1818
use crate::SugarloafErrors;
19-
use lru::LruCache;
20-
use parking_lot::FairMutex;
19+
use parking_lot::RwLock;
2120
use rustc_hash::FxHashMap;
22-
use std::num::NonZeroUsize;
2321
use std::ops::Range;
2422
use std::path::PathBuf;
2523
use std::sync::Arc;
@@ -29,7 +27,7 @@ pub use crate::font_introspector::{Style, Weight};
2927
pub fn lookup_for_font_match(
3028
cluster: &mut CharCluster,
3129
synth: &mut Synthesis,
32-
library: &mut FontLibraryData,
30+
library: &FontLibraryData,
3331
spec_font_attr_opt: Option<&(crate::font_introspector::Style, bool)>,
3432
) -> Option<(usize, bool)> {
3533
let mut search_result = None;
@@ -62,8 +60,13 @@ pub fn lookup_for_font_match(
6260
}
6361
}
6462

65-
if let Some(data) = library.get_data(&font_id) {
66-
let charmap = data.charmap();
63+
if let Some((shared_data, offset, key)) = library.get_data(&font_id) {
64+
let font_ref = FontRef {
65+
data: shared_data.as_ref(),
66+
offset,
67+
key,
68+
};
69+
let charmap = font_ref.charmap();
6770
let status = cluster.map(|ch| charmap.map(ch));
6871
if status != Status::Discard {
6972
*synth = font_synth;
@@ -84,7 +87,7 @@ pub fn lookup_for_font_match(
8487

8588
#[derive(Clone)]
8689
pub struct FontLibrary {
87-
pub inner: Arc<FairMutex<FontLibraryData>>,
90+
pub inner: Arc<RwLock<FontLibraryData>>,
8891
}
8992

9093
impl FontLibrary {
@@ -100,7 +103,7 @@ impl FontLibrary {
100103

101104
(
102105
Self {
103-
inner: Arc::new(FairMutex::new(font_library)),
106+
inner: Arc::new(RwLock::new(font_library)),
104107
},
105108
sugarloaf_errors,
106109
)
@@ -113,7 +116,7 @@ impl Default for FontLibrary {
113116
let _fonts_not_found = font_library.load(SugarloafFonts::default());
114117

115118
Self {
116-
inner: Arc::new(FairMutex::new(font_library)),
119+
inner: Arc::new(RwLock::new(font_library)),
117120
}
118121
}
119122
}
@@ -127,15 +130,13 @@ pub struct FontLibraryData {
127130
// Standard is fallback for everything, it is also the inner number 0
128131
pub inner: FxHashMap<usize, FontData>,
129132
pub symbol_maps: Option<Vec<SymbolMap>>,
130-
pub stash: LruCache<usize, SharedData>,
131133
pub hinting: bool,
132134
}
133135

134136
impl Default for FontLibraryData {
135137
fn default() -> Self {
136138
Self {
137139
inner: FxHashMap::default(),
138-
stash: LruCache::new(NonZeroUsize::new(2).unwrap()),
139140
hinting: true,
140141
symbol_maps: None,
141142
}
@@ -145,7 +146,7 @@ impl Default for FontLibraryData {
145146
impl FontLibraryData {
146147
#[inline]
147148
pub fn find_best_font_match(
148-
&mut self,
149+
&self,
149150
ch: char,
150151
fragment_style: &FragmentStyle,
151152
) -> Option<(usize, bool)> {
@@ -205,39 +206,22 @@ impl FontLibraryData {
205206
}
206207

207208
#[inline]
208-
pub fn get(&mut self, font_id: &usize) -> &FontData {
209+
pub fn get(&self, font_id: &usize) -> &FontData {
209210
&self.inner[font_id]
210211
}
211212

212-
pub fn get_data<'a>(&'a mut self, font_id: &usize) -> Option<FontRef<'a>> {
213+
pub fn get_data(&self, font_id: &usize) -> Option<(SharedData, u32, CacheKey)> {
213214
if let Some(font) = self.inner.get(font_id) {
214-
match &font.data {
215-
Some(data) => {
216-
return Some(FontRef {
217-
data: data.as_ref(),
218-
offset: font.offset,
219-
key: font.key,
220-
})
221-
}
222-
None => {
223-
if !self.stash.contains(font_id) {
224-
if let Some(path) = &font.path {
225-
if let Some(raw_data) = load_from_font_source(path) {
226-
self.stash.put(*font_id, SharedData::new(raw_data));
227-
}
228-
}
229-
}
215+
if let Some(data) = &font.data {
216+
return Some((data.clone(), font.offset, font.key));
217+
} else if let Some(path) = &font.path {
218+
// Load font data directly from file when needed
219+
if let Some(raw_data) = load_from_font_source(path) {
220+
let shared_data = SharedData::new(raw_data);
221+
return Some((shared_data, font.offset, font.key));
230222
}
231223
}
232-
233-
if let Some(data) = self.stash.get(font_id) {
234-
return Some(FontRef {
235-
data: data.as_ref(),
236-
offset: font.offset,
237-
key: font.key,
238-
});
239-
}
240-
};
224+
}
241225

242226
None
243227
}

sugarloaf/src/layout/content.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
use crate::components::rich_text::RichTextBrush;
77
use crate::font::FontLibrary;
8-
use crate::font_introspector::shape::cluster::GlyphCluster;
98
use crate::font_introspector::shape::cluster::OwnedGlyphCluster;
109
use crate::font_introspector::shape::ShapeContext;
1110
use crate::font_introspector::text::Script;
1211
use crate::font_introspector::Metrics;
12+
use crate::font_introspector::{shape::cluster::GlyphCluster, FontRef};
1313
use crate::layout::render_data::RenderData;
1414
use crate::layout::RichTextLayout;
1515
use crate::Graphics;
@@ -605,11 +605,17 @@ impl Content {
605605

606606
// Process the font data directly without cloning FontRef
607607
{
608-
let font_library = &mut self.fonts.inner.lock();
609-
if let Some(data) = font_library.get_data(&font_id) {
608+
let font_library = &self.fonts.inner.read();
609+
if let Some((shared_data, offset, key)) = font_library.get_data(&font_id)
610+
{
611+
let font_ref = FontRef {
612+
data: shared_data.as_ref(),
613+
offset,
614+
key,
615+
};
610616
let mut shaper = self
611617
.scx
612-
.builder(data) // Use reference directly without cloning
618+
.builder(font_ref) // Use reference directly without cloning
613619
.script(script)
614620
.size(scaled_font_size)
615621
.features(features.iter().copied())

0 commit comments

Comments
 (0)