aboutsummaryrefslogtreecommitdiffstats
path: root/components/gfx/tests
diff options
context:
space:
mode:
authorMartin Robinson <mrobinson@igalia.com>2024-05-02 12:34:10 +0200
committerGitHub <noreply@github.com>2024-05-02 10:34:10 +0000
commit556bfb7dff48f64e9e02872dba29fbdabc8c6ad0 (patch)
tree0c9e1e80582fee2a64aa5904df3230e8a3d2befd /components/gfx/tests
parent8ec5344f70dd1d556cacd72d778924048b0b1154 (diff)
downloadservo-556bfb7dff48f64e9e02872dba29fbdabc8c6ad0.tar.gz
servo-556bfb7dff48f64e9e02872dba29fbdabc8c6ad0.zip
fonts: Make `FontContext` thread-safe and share it per-Layout (#32205)
This allows sharing font templates, fonts, and platform fonts across layout threads. It's the first step toward storing web fonts in the layout versus the shared `FontCacheThread`. Now fonts and font groups have some locking (especially on FreeType), which will probably affect performance. On the other hand, we measured memory usage and this saves roughly 40 megabytes of memory when loading servo.org based on data from the memory profiler. Signed-off-by: Martin Robinson <mrobinson@igalia.com> Co-authored-by: Mukilan Thiyagarajan <mukilan@igalia.com>
Diffstat (limited to 'components/gfx/tests')
-rw-r--r--components/gfx/tests/font_context.rs58
1 files changed, 23 insertions, 35 deletions
diff --git a/components/gfx/tests/font_context.rs b/components/gfx/tests/font_context.rs
index f502b7d97fe..54babf3ef83 100644
--- a/components/gfx/tests/font_context.rs
+++ b/components/gfx/tests/font_context.rs
@@ -150,22 +150,26 @@ fn font_family(names: Vec<&str>) -> FontFamily {
#[test]
fn test_font_group_is_cached_by_style() {
let source = TestFontSource::new();
- let mut context = FontContext::new(source);
+ let context = FontContext::new(source);
let style1 = style();
let mut style2 = style();
style2.set_font_style(FontStyle::ITALIC);
- assert_eq!(
- context.font_group(Arc::new(style1.clone())).as_ptr(),
- context.font_group(Arc::new(style1.clone())).as_ptr(),
+ assert!(
+ std::ptr::eq(
+ &*context.font_group(Arc::new(style1.clone())).read(),
+ &*context.font_group(Arc::new(style1.clone())).read()
+ ),
"the same font group should be returned for two styles with the same hash"
);
- assert_ne!(
- context.font_group(Arc::new(style1.clone())).as_ptr(),
- context.font_group(Arc::new(style2.clone())).as_ptr(),
+ assert!(
+ !std::ptr::eq(
+ &*context.font_group(Arc::new(style1.clone())).read(),
+ &*context.font_group(Arc::new(style2.clone())).read()
+ ),
"different font groups should be returned for two styles with different hashes"
)
}
@@ -181,12 +185,9 @@ fn test_font_group_find_by_codepoint() {
let group = context.font_group(Arc::new(style));
- let font = group
- .borrow_mut()
- .find_by_codepoint(&mut context, 'a')
- .unwrap();
+ let font = group.write().find_by_codepoint(&mut context, 'a').unwrap();
assert_eq!(
- font.borrow().identifier(),
+ font.identifier(),
TestFontSource::identifier_for_font_name("csstest-ascii")
);
assert_eq!(
@@ -195,12 +196,9 @@ fn test_font_group_find_by_codepoint() {
"only the first font in the list should have been loaded"
);
- let font = group
- .borrow_mut()
- .find_by_codepoint(&mut context, 'a')
- .unwrap();
+ let font = group.write().find_by_codepoint(&mut context, 'a').unwrap();
assert_eq!(
- font.borrow().identifier(),
+ font.identifier(),
TestFontSource::identifier_for_font_name("csstest-ascii")
);
assert_eq!(
@@ -209,12 +207,9 @@ fn test_font_group_find_by_codepoint() {
"we shouldn't load the same font a second time"
);
- let font = group
- .borrow_mut()
- .find_by_codepoint(&mut context, 'á')
- .unwrap();
+ let font = group.write().find_by_codepoint(&mut context, 'á').unwrap();
assert_eq!(
- font.borrow().identifier(),
+ font.identifier(),
TestFontSource::identifier_for_font_name("csstest-basic-regular")
);
assert_eq!(count.get(), 2, "both fonts should now have been loaded");
@@ -230,22 +225,16 @@ fn test_font_fallback() {
let group = context.font_group(Arc::new(style));
- let font = group
- .borrow_mut()
- .find_by_codepoint(&mut context, 'a')
- .unwrap();
+ let font = group.write().find_by_codepoint(&mut context, 'a').unwrap();
assert_eq!(
- font.borrow().identifier(),
+ font.identifier(),
TestFontSource::identifier_for_font_name("csstest-ascii"),
"a family in the group should be used if there is a matching glyph"
);
- let font = group
- .borrow_mut()
- .find_by_codepoint(&mut context, 'á')
- .unwrap();
+ let font = group.write().find_by_codepoint(&mut context, 'á').unwrap();
assert_eq!(
- font.borrow().identifier(),
+ font.identifier(),
TestFontSource::identifier_for_font_name("csstest-basic-regular"),
"a fallback font should be used if there is no matching glyph in the group"
);
@@ -255,7 +244,7 @@ fn test_font_fallback() {
fn test_font_template_is_cached() {
let source = TestFontSource::new();
let count = source.find_font_count.clone();
- let mut context = FontContext::new(source);
+ let context = FontContext::new(source);
let mut font_descriptor = FontDescriptor {
weight: FontWeight::normal(),
@@ -280,8 +269,7 @@ fn test_font_template_is_cached() {
.unwrap();
assert_ne!(
- font1.borrow().descriptor.pt_size,
- font2.borrow().descriptor.pt_size,
+ font1.descriptor.pt_size, font2.descriptor.pt_size,
"the same font should not have been returned"
);