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

@ -1638,10 +1638,9 @@ impl InlineFormattingContext {
// It's unfortunate that it isn't possible to get this during IFC text processing, but in
// that situation the style of the containing block is unknown.
let default_font_metrics = layout_context.with_font_context(|font_context| {
get_font_for_first_font_for_style(style, font_context)
.map(|font| font.borrow().metrics.clone())
});
let default_font_metrics =
get_font_for_first_font_for_style(style, &layout_context.font_context)
.map(|font| font.metrics.clone());
let style_text = containing_block.style.get_inherited_text();
let mut ifc = InlineFormattingContextState {
@ -1768,34 +1767,30 @@ impl InlineFormattingContext {
// For the purposes of `text-transform: capitalize` the start of the IFC is a word boundary.
let mut on_word_boundary = true;
layout_context.with_font_context(|font_context| {
let mut linebreaker = None;
self.foreach(|iter_item| match iter_item {
InlineFormattingContextIterItem::Item(InlineLevelBox::TextRun(
ref mut text_run,
)) => {
text_run.break_and_shape(
font_context,
&mut linebreaker,
&mut ifc_fonts,
&mut last_inline_box_ended_with_white_space,
&mut on_word_boundary,
);
},
InlineFormattingContextIterItem::Item(InlineLevelBox::InlineBox(inline_box)) => {
if let Some(font) =
get_font_for_first_font_for_style(&inline_box.style, font_context)
{
inline_box.default_font_index =
Some(add_or_get_font(&font, &mut ifc_fonts));
}
},
InlineFormattingContextIterItem::Item(InlineLevelBox::Atomic(_)) => {
last_inline_box_ended_with_white_space = false;
on_word_boundary = true;
},
_ => {},
});
let mut linebreaker = None;
self.foreach(|iter_item| match iter_item {
InlineFormattingContextIterItem::Item(InlineLevelBox::TextRun(ref mut text_run)) => {
text_run.break_and_shape(
&layout_context.font_context,
&mut linebreaker,
&mut ifc_fonts,
&mut last_inline_box_ended_with_white_space,
&mut on_word_boundary,
);
},
InlineFormattingContextIterItem::Item(InlineLevelBox::InlineBox(inline_box)) => {
if let Some(font) = get_font_for_first_font_for_style(
&inline_box.style,
&layout_context.font_context,
) {
inline_box.default_font_index = Some(add_or_get_font(&font, &mut ifc_fonts));
}
},
InlineFormattingContextIterItem::Item(InlineLevelBox::Atomic(_)) => {
last_inline_box_ended_with_white_space = false;
on_word_boundary = true;
},
_ => {},
});
self.font_metrics = ifc_fonts;