mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
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.
This commit is contained in:
parent
e9591ce62f
commit
efa0d45757
16 changed files with 152 additions and 247 deletions
|
@ -15,7 +15,6 @@ use style::values::computed::font::FontWeight;
|
|||
use crate::font::FontHandleMethods;
|
||||
use crate::font_cache_thread::FontIdentifier;
|
||||
use crate::platform::font::FontHandle;
|
||||
use crate::platform::font_context::FontContextHandle;
|
||||
use crate::platform::font_template::FontTemplateData;
|
||||
|
||||
/// Describes how to select a font from a given family. This is very basic at the moment and needs
|
||||
|
@ -130,10 +129,7 @@ impl FontTemplate {
|
|||
}
|
||||
|
||||
/// Get the descriptor. Returns `None` when instantiating the data fails.
|
||||
pub fn descriptor(
|
||||
&mut self,
|
||||
font_context: &FontContextHandle,
|
||||
) -> Option<FontTemplateDescriptor> {
|
||||
pub fn descriptor(&mut self) -> Option<FontTemplateDescriptor> {
|
||||
// The font template data can be unloaded when nothing is referencing
|
||||
// it (via the Weak reference to the Arc above). However, if we have
|
||||
// already loaded a font, store the style information about it separately,
|
||||
|
@ -141,7 +137,7 @@ impl FontTemplate {
|
|||
// without having to reload the font (unless it is an actual match).
|
||||
|
||||
self.descriptor.or_else(|| {
|
||||
if self.instantiate(font_context).is_err() {
|
||||
if self.instantiate().is_err() {
|
||||
return None;
|
||||
};
|
||||
|
||||
|
@ -155,10 +151,9 @@ impl FontTemplate {
|
|||
/// Get the data for creating a font if it matches a given descriptor.
|
||||
pub fn data_for_descriptor(
|
||||
&mut self,
|
||||
fctx: &FontContextHandle,
|
||||
requested_desc: &FontTemplateDescriptor,
|
||||
) -> Option<Arc<FontTemplateData>> {
|
||||
self.descriptor(fctx).and_then(|descriptor| {
|
||||
self.descriptor().and_then(|descriptor| {
|
||||
if *requested_desc == descriptor {
|
||||
self.data().ok()
|
||||
} else {
|
||||
|
@ -171,23 +166,22 @@ impl FontTemplate {
|
|||
/// descriptor, if the font can be loaded.
|
||||
pub fn data_for_approximate_descriptor(
|
||||
&mut self,
|
||||
font_context: &FontContextHandle,
|
||||
requested_descriptor: &FontTemplateDescriptor,
|
||||
) -> Option<(Arc<FontTemplateData>, f32)> {
|
||||
self.descriptor(font_context).and_then(|descriptor| {
|
||||
self.descriptor().and_then(|descriptor| {
|
||||
self.data()
|
||||
.ok()
|
||||
.map(|data| (data, descriptor.distance_from(requested_descriptor)))
|
||||
})
|
||||
}
|
||||
|
||||
fn instantiate(&mut self, font_context: &FontContextHandle) -> Result<(), &'static str> {
|
||||
fn instantiate(&mut self) -> Result<(), &'static str> {
|
||||
if !self.is_valid {
|
||||
return Err("Invalid font template");
|
||||
}
|
||||
|
||||
let data = self.data().map_err(|_| "Could not get FontTemplate data")?;
|
||||
let handle = FontHandleMethods::new_from_template(font_context, data, None);
|
||||
let handle = FontHandleMethods::new_from_template(data, None);
|
||||
self.is_valid = handle.is_ok();
|
||||
let handle: FontHandle = handle?;
|
||||
self.descriptor = Some(FontTemplateDescriptor::new(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue