From efa0d457574f02dfbe2403f501a4626acdcb64db Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Fri, 12 Apr 2024 12:39:32 +0200 Subject: Remove `FontContextHandle` (#32038) The `FontContextHandle` was really only used on FreeType platforms to store the `FT_Library` handle to use for creating faces. Each `FontContext` and `FontCacheThread` would create its own `FontContextHandle`. This change removes this data structure in favor of a mutex-protected shared `FontContextHandle` for an entire Servo process. The handle is initialized using a `OnceLock` to ensure that it only happens once and also that it stays alive for the entire process lifetime. In addition to greatly simplifying the code, this will make it possible for different threads to share platform-specific `FontHandle`s, avoiding multiple allocations for a single font. The only downside to all of this is that memory usage of FreeType fonts isn't measured (though the mechanism is still there). This is because the `FontCacheThread` currently doesn't do any memory measurement. Eventually this *will* happen though, during the font system redesign. In exchange, this should reduce the memory usage since there is only a single FreeType library loaded into memory now. This is part of #32033. --- components/gfx/font_context.rs | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) (limited to 'components/gfx/font_context.rs') diff --git a/components/gfx/font_context.rs b/components/gfx/font_context.rs index c7f2eaef530..d316ceb50c1 100644 --- a/components/gfx/font_context.rs +++ b/components/gfx/font_context.rs @@ -12,7 +12,6 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use app_units::Au; use fnv::FnvHasher; use log::debug; -use malloc_size_of::{MallocSizeOf, MallocSizeOfOps}; use servo_arc::Arc; use style::computed_values::font_variant_caps::T as FontVariantCaps; use style::properties::style_structs::Font as FontStyleStruct; @@ -24,7 +23,6 @@ use crate::font::{ use crate::font_cache_thread::FontTemplateInfo; use crate::font_template::FontTemplateDescriptor; use crate::platform::font::FontHandle; -pub use crate::platform::font_context::FontContextHandle; static SMALL_CAPS_SCALE_FACTOR: f32 = 0.8; // Matches FireFox (see gfxFont.h) @@ -48,7 +46,6 @@ pub trait FontSource { /// required. #[derive(Debug)] pub struct FontContext { - platform_handle: FontContextHandle, font_source: S, // TODO: The font context holds a strong ref to the cached fonts @@ -66,9 +63,7 @@ pub struct FontContext { impl FontContext { pub fn new(font_source: S) -> FontContext { #[allow(clippy::default_constructed_unit_structs)] - let handle = FontContextHandle::default(); FontContext { - platform_handle: handle, font_source, font_cache: HashMap::new(), font_template_cache: HashMap::new(), @@ -217,11 +212,7 @@ impl FontContext { descriptor: FontDescriptor, synthesized_small_caps: Option, ) -> Result { - let handle = FontHandle::new_from_template( - &self.platform_handle, - info.font_template, - Some(descriptor.pt_size), - )?; + let handle = FontHandle::new_from_template(info.font_template, Some(descriptor.pt_size))?; let font_instance_key = self .font_source @@ -235,13 +226,6 @@ impl FontContext { } } -impl MallocSizeOf for FontContext { - fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - // FIXME(njn): Measure other fields eventually. - self.platform_handle.size_of(ops) - } -} - #[derive(Debug, Eq, Hash, PartialEq)] struct FontCacheKey { font_descriptor: FontDescriptor, -- cgit v1.2.3