aboutsummaryrefslogtreecommitdiffstats
path: root/components/layout/context.rs
diff options
context:
space:
mode:
authorMartin Robinson <mrobinson@igalia.com>2024-05-02 12:34:10 +0200
committerGitHub <noreply@github.com>2024-05-02 10:34:10 +0000
commit556bfb7dff48f64e9e02872dba29fbdabc8c6ad0 (patch)
tree0c9e1e80582fee2a64aa5904df3230e8a3d2befd /components/layout/context.rs
parent8ec5344f70dd1d556cacd72d778924048b0b1154 (diff)
downloadservo-556bfb7dff48f64e9e02872dba29fbdabc8c6ad0.tar.gz
servo-556bfb7dff48f64e9e02872dba29fbdabc8c6ad0.zip
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>
Diffstat (limited to 'components/layout/context.rs')
-rw-r--r--components/layout/context.rs32
1 files changed, 2 insertions, 30 deletions
diff --git a/components/layout/context.rs b/components/layout/context.rs
index d43210c141b..5cedf5d38aa 100644
--- a/components/layout/context.rs
+++ b/components/layout/context.rs
@@ -4,7 +4,6 @@
//! Data needed by layout.
-use std::cell::{RefCell, RefMut};
use std::collections::HashMap;
use std::hash::BuildHasherDefault;
use std::sync::{Arc, Mutex};
@@ -13,7 +12,6 @@ use std::thread;
use fnv::FnvHasher;
use gfx::font_cache_thread::FontCacheThread;
use gfx::font_context::FontContext;
-use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use msg::constellation_msg::PipelineId;
use net_traits::image_cache::{
ImageCache, ImageCacheResult, ImageOrMetadataAvailable, UsePlaceholder,
@@ -29,32 +27,6 @@ use crate::display_list::items::{OpaqueNode, WebRenderImageInfo};
pub type LayoutFontContext = FontContext<FontCacheThread>;
-thread_local!(static FONT_CONTEXT_KEY: RefCell<Option<LayoutFontContext>> = RefCell::new(None));
-
-pub fn with_thread_local_font_context<F, R>(layout_context: &LayoutContext, f: F) -> R
-where
- F: FnOnce(&mut LayoutFontContext) -> R,
-{
- FONT_CONTEXT_KEY.with(|k| {
- let mut font_context = k.borrow_mut();
- if font_context.is_none() {
- let font_cache_thread = layout_context.font_cache_thread.lock().unwrap().clone();
- *font_context = Some(FontContext::new(font_cache_thread));
- }
- f(&mut RefMut::map(font_context, |x| x.as_mut().unwrap()))
- })
-}
-
-pub fn malloc_size_of_persistent_local_context(ops: &mut MallocSizeOfOps) -> usize {
- FONT_CONTEXT_KEY.with(|r| {
- if let Some(ref context) = *r.borrow() {
- context.size_of(ops)
- } else {
- 0
- }
- })
-}
-
type WebrenderImageCache =
HashMap<(ServoUrl, UsePlaceholder), WebRenderImageInfo, BuildHasherDefault<FnvHasher>>;
@@ -72,8 +44,8 @@ pub struct LayoutContext<'a> {
/// Reference to the script thread image cache.
pub image_cache: Arc<dyn ImageCache>,
- /// Interface to the font cache thread.
- pub font_cache_thread: Mutex<FontCacheThread>,
+ /// A FontContext to be used during layout.
+ pub font_context: Arc<FontContext<FontCacheThread>>,
/// A cache of WebRender image info.
pub webrender_image_cache: Arc<RwLock<WebrenderImageCache>>,