fonts: Instantiate system fonts using system font loaders (#33747)

System fonts used to be instantiated using the system font loader and
this change restores that behavior. In addition, on macOS and FreeType
platforms font data for system fonts is loaded using memory mapping. The
benefit is that system font loaders typically are able to cache fonts in
system memory (using memory mapping, for instance) and we'd like to load
them in a the way most compatible with other applications.

On my Linux system, this manages to get the overhead of loading a very
large font down from 10ms to approximately 1ms. Subsequent runs show
even less overhead. We've measured similar gains on macOS systems.

Currently, system font data must be loaded into memory manually for
canvas and this is unlikely to change even with a switch to `vello`. The
use of explicit memmory mapping should help in this case -- though it
probably won't be possible to use this properly on macOS and Windows if
we ever want to load fonts from TTCs properly.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Mukilan Thiyagarajan <mukilan@igalia.com>
This commit is contained in:
Martin Robinson 2024-10-10 16:09:51 -07:00 committed by GitHub
parent 4564ce2fcc
commit 0553789d48
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 771 additions and 810 deletions

View file

@ -28,6 +28,7 @@ use style::shared_lock::SharedRwLockReadGuard;
use style::stylesheets::{CssRule, DocumentStyleSheet, FontFaceRule, StylesheetInDocument};
use style::values::computed::font::{FamilyName, FontFamilyNameSyntax, SingleFontFamily};
use style::Atom;
use tracing::instrument;
use url::Url;
use webrender_api::{FontInstanceFlags, FontInstanceKey, FontKey};
use webrender_traits::CrossProcessCompositorApi;
@ -74,6 +75,10 @@ pub struct FontContext {
/// this [`FontContext`] controls.
webrender_font_instance_keys: RwLock<HashMap<(FontKey, Au), FontInstanceKey>>,
/// The data for each web font [`FontIdentifier`]. This data might be used by more than one
/// [`FontTemplate`] as each identifier refers to a URL.
font_data: RwLock<HashMap<FontIdentifier, FontData>>,
have_removed_web_fonts: AtomicBool,
}
@ -114,6 +119,7 @@ impl FontContext {
webrender_font_keys: RwLock::default(),
webrender_font_instance_keys: RwLock::default(),
have_removed_web_fonts: AtomicBool::new(false),
font_data: RwLock::default(),
}
}
@ -121,14 +127,11 @@ impl FontContext {
self.web_fonts.read().number_of_fonts_still_loading()
}
fn get_font_data(&self, identifier: &FontIdentifier) -> Arc<FontData> {
fn get_font_data(&self, identifier: &FontIdentifier) -> Option<FontData> {
match identifier {
FontIdentifier::Web(_) => self.web_fonts.read().get_font_data(identifier),
FontIdentifier::Local(_) | FontIdentifier::Mock(_) => {
self.system_font_service_proxy.get_font_data(identifier)
},
FontIdentifier::Web(_) => self.font_data.read().get(identifier).cloned(),
FontIdentifier::Local(_) => None,
}
.expect("Could not find font data")
}
/// Handle the situation where a web font finishes loading, specifying if the load suceeded or failed.
@ -276,6 +279,7 @@ impl FontContext {
/// Create a `Font` for use in layout calculations, from a `FontTemplateData` returned by the
/// cache thread and a `FontDescriptor` which contains the styling parameters.
#[instrument(skip_all, fields(servo_profiling = true))]
fn create_font(
&self,
font_template: FontTemplateRef,
@ -292,13 +296,11 @@ impl FontContext {
pub(crate) fn create_font_instance_key(&self, font: &Font) -> FontInstanceKey {
match font.template.identifier() {
FontIdentifier::Local(_) | FontIdentifier::Mock(_) => {
self.system_font_service_proxy.get_system_font_instance(
font.template.identifier(),
font.descriptor.pt_size,
font.webrender_font_instance_flags(),
)
},
FontIdentifier::Local(_) => self.system_font_service_proxy.get_system_font_instance(
font.template.identifier(),
font.descriptor.pt_size,
font.webrender_font_instance_flags(),
),
FontIdentifier::Web(_) => self.create_web_font_instance(
font.template.clone(),
font.descriptor.pt_size,
@ -314,7 +316,9 @@ impl FontContext {
flags: FontInstanceFlags,
) -> FontInstanceKey {
let identifier = font_template.identifier().clone();
let font_data = self.get_font_data(&identifier);
let font_data = self
.get_font_data(&identifier)
.expect("Web font should have associated font data");
let font_key = *self
.webrender_font_keys
.write()
@ -519,7 +523,8 @@ impl FontContextWebFontMethods for Arc<FontContext> {
}
// Lock everything to prevent adding new fonts while we are cleaning up the old ones.
let mut web_fonts = self.web_fonts.write();
let web_fonts = self.web_fonts.write();
let mut font_data = self.font_data.write();
let _fonts = self.fonts.write();
let _font_groups = self.resolved_font_groups.write();
let mut webrender_font_keys = self.webrender_font_keys.write();
@ -533,7 +538,7 @@ impl FontContextWebFontMethods for Arc<FontContext> {
});
}
web_fonts.remove_all_font_data_for_identifiers(&unused_identifiers);
font_data.retain(|font_identifier, _| unused_identifiers.contains(font_identifier));
self.have_removed_web_fonts.store(false, Ordering::Relaxed);
@ -581,7 +586,7 @@ impl FontContext {
RemoteWebFontDownloader::download(url_source, this, web_font_family_name, state)
},
Source::Local(ref local_family_name) => {
if let Some((new_template, font_data)) = state
if let Some(new_template) = state
.local_fonts
.get(&local_family_name.name)
.cloned()
@ -593,15 +598,13 @@ impl FontContext {
state.stylesheet.clone(),
)
.ok()?;
let font_data = self.get_font_data(&local_template.identifier());
Some((template, font_data))
Some(template)
})
{
let not_cancelled = self.web_fonts.write().handle_web_font_loaded(
&state,
new_template,
font_data,
);
let not_cancelled = self
.web_fonts
.write()
.handle_web_font_loaded(&state, new_template);
self.handle_web_font_load_finished(&state.finished_callback, not_cancelled);
} else {
this.process_next_web_font_source(state);
@ -695,7 +698,7 @@ impl RemoteWebFontDownloader {
);
let font_data = match fontsan::process(&font_data) {
Ok(bytes) => Arc::new(FontData::from_bytes(bytes)),
Ok(bytes) => FontData::from_bytes(&bytes),
Err(error) => {
debug!(
"Sanitiser rejected web font: family={} url={:?} with {error:?}",
@ -707,9 +710,7 @@ impl RemoteWebFontDownloader {
let url: ServoUrl = self.url.clone().into();
let identifier = FontIdentifier::Web(url.clone());
let Ok(handle) =
PlatformFont::new_from_data(identifier, font_data.as_arc().clone(), 0, None)
else {
let Ok(handle) = PlatformFont::new_from_data(identifier, &font_data, None) else {
return false;
};
let mut descriptor = handle.descriptor();
@ -721,11 +722,16 @@ impl RemoteWebFontDownloader {
descriptor,
Some(state.stylesheet.clone()),
);
let not_cancelled = self.font_context.web_fonts.write().handle_web_font_loaded(
state,
new_template,
font_data,
);
self.font_context
.font_data
.write()
.insert(new_template.identifier.clone(), font_data);
let not_cancelled = self
.font_context
.web_fonts
.write()
.handle_web_font_loaded(state, new_template);
self.font_context
.handle_web_font_load_finished(&state.finished_callback, not_cancelled);