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>
This commit is contained in:
Martin Robinson 2024-05-02 12:34:10 +02:00 committed by GitHub
parent 8ec5344f70
commit 556bfb7dff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 437 additions and 500 deletions

View file

@ -42,7 +42,7 @@ use style::values::generics::counters::ContentItem;
use style::LocalName;
use crate::block::BlockFlow;
use crate::context::{with_thread_local_font_context, LayoutContext};
use crate::context::LayoutContext;
use crate::data::{InnerLayoutData, LayoutDataFlags};
use crate::display_list::items::OpaqueNode;
use crate::flex::FlexFlow;
@ -517,11 +517,10 @@ where
// We must scan for runs before computing minimum ascent and descent because scanning
// for runs might collapse so much whitespace away that only hypothetical fragments
// remain. In that case the inline flow will compute its ascent and descent to be zero.
let scanned_fragments =
with_thread_local_font_context(self.layout_context, |font_context| {
TextRunScanner::new()
.scan_for_runs(font_context, mem::take(&mut fragments.fragments))
});
let scanned_fragments = TextRunScanner::new().scan_for_runs(
&self.layout_context.font_context,
mem::take(&mut fragments.fragments),
);
let mut inline_flow_ref = FlowRef::new(Arc::new(InlineFlow::from_fragments(
scanned_fragments,
node.style(self.style_context()).writing_mode,
@ -550,11 +549,10 @@ where
{
// FIXME(#6503): Use Arc::get_mut().unwrap() here.
let inline_flow = FlowRef::deref_mut(&mut inline_flow_ref).as_mut_inline();
inline_flow.minimum_line_metrics =
with_thread_local_font_context(self.layout_context, |font_context| {
inline_flow
.minimum_line_metrics(font_context, &node.style(self.style_context()))
});
inline_flow.minimum_line_metrics = inline_flow.minimum_line_metrics(
&self.layout_context.font_context,
&node.style(self.style_context()),
);
}
inline_flow_ref.finish();
@ -1545,11 +1543,10 @@ where
)),
self.layout_context,
));
let marker_fragments =
with_thread_local_font_context(self.layout_context, |font_context| {
TextRunScanner::new()
.scan_for_runs(font_context, unscanned_marker_fragments)
});
let marker_fragments = TextRunScanner::new().scan_for_runs(
&self.layout_context.font_context,
unscanned_marker_fragments,
);
marker_fragments.fragments
},
ListStyleTypeContent::GeneratedContent(info) => vec![Fragment::new(