fonts: Clean up WebRender web fonts when they are no longer used (#32545)

This is the first part of cleaning up unused WebRender resources.
Currently this only cleans up web font resources, but a more
full-featured implementation in the future could also clean up unused
system fonts.

Fixes #32345.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Mukilan Thiyagarajan <mukilan@igalia.com>
This commit is contained in:
Martin Robinson 2024-06-18 16:02:27 +02:00 committed by GitHub
parent bd15a4fbd8
commit fef1337da0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 181 additions and 7 deletions

View file

@ -191,16 +191,18 @@ pub trait WebRenderFontApi {
flags: FontInstanceFlags,
) -> FontInstanceKey;
fn add_font(&self, data: Arc<Vec<u8>>, index: u32) -> FontKey;
/// Forward an already prepared `AddFont` message, sending it on to the compositor. This is used
/// to get WebRender [`FontKey`]s for web fonts in the per-layout `FontContext`.
fn add_system_font(&self, handle: NativeFontHandle) -> FontKey;
/// Forward a `AddFont` message, sending it on to the compositor. This is used to get WebRender
/// [`FontKey`]s for web fonts in the per-layout `FontContext`.
fn forward_add_font_message(
&self,
bytes_receiver: IpcBytesReceiver,
font_index: u32,
result_sender: IpcSender<FontKey>,
);
/// Forward an already prepared `AddFontInstance` message, sending it on to the compositor. This
/// is used to get WebRender [`FontInstanceKey`]s for web fonts in the per-layout `FontContext`.
/// Forward a `AddFontInstance` message, sending it on to the compositor. This is used to get
/// WebRender [`FontInstanceKey`]s for web fonts in the per-layout `FontContext`.
fn forward_add_font_instance_message(
&self,
font_key: FontKey,
@ -208,7 +210,6 @@ pub trait WebRenderFontApi {
flags: FontInstanceFlags,
result_receiver: IpcSender<FontInstanceKey>,
);
fn add_system_font(&self, handle: NativeFontHandle) -> FontKey;
}
pub enum CanvasToCompositorMsg {
@ -257,6 +258,8 @@ pub enum ScriptToCompositorMsg {
GenerateImageKey(IpcSender<ImageKey>),
/// Perform a resource update operation.
UpdateImages(Vec<SerializedImageUpdate>),
/// Remove the given font resources from our WebRender instance.
RemoveFonts(Vec<FontKey>, Vec<FontInstanceKey>),
}
/// A mechanism to send messages from networking to the WebRender instance.
@ -420,6 +423,19 @@ impl WebRenderScriptApi {
}
});
}
pub fn remove_unused_font_resources(
&self,
keys: Vec<FontKey>,
instance_keys: Vec<FontInstanceKey>,
) {
if keys.is_empty() && instance_keys.is_empty() {
return;
}
let _ = self
.0
.send(ScriptToCompositorMsg::RemoveFonts(keys, instance_keys));
}
}
#[derive(Deserialize, Serialize)]