Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug-fix(font-cache): Eliminated unwanted cloning of cosmic_text::fontdb::Database by passing it as a reference #71

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ macro_rules! msg {

/// Passed to [`Component#render`][Component#method.render], with context required for rendering.
#[derive(Clone)]
pub struct RenderContext {
pub struct RenderContext<'a> {
/// The `AABB` that contains the given [`Component`] instance.
pub aabb: AABB,
/// For scrollable Components (Components that return a `Some` value for [`#scroll_position`][Component#method.scroll_position]), this is the size of the child Nodes.
pub inner_scale: Option<Scale>,
/// The caches used by the renderer.
pub caches: Caches,
pub caches: Caches<'a>,
/// The value previously returned by [`Component#render`][Component#method.render] of the given instance.
pub prev_state: Option<Vec<Renderable>>,
/// The scale factor of the current monitor. Renderables should be scaled by this value.
Expand Down
8 changes: 4 additions & 4 deletions core/src/renderer/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn load_assets_to_canvas(
}

pub struct CanvasRenderer {
fonts: cosmic_text::fontdb::Database,
fonts_cache: FontCache,
text_renderer: TextRenderer,
assets: HashMap<String, ImageId>,
svgs: HashMap<String, SvgData>,
Expand Down Expand Up @@ -123,7 +123,7 @@ impl super::Renderer for CanvasRenderer {
let loaded_svgs = load_svg_paths(svgs, fonts.clone());

Self {
fonts: fonts.clone(),
fonts_cache: FontCache::new(fonts.clone()),
text_renderer,
assets: HashMap::new(),
svgs: loaded_svgs,
Expand Down Expand Up @@ -197,10 +197,10 @@ impl super::Renderer for CanvasRenderer {
}

/// This default is provided for tests, it should be overridden
fn caches(&self) -> Caches {
fn caches(&mut self) -> Caches {
// println!("caches()");
Caches {
font: Arc::new(RwLock::new(FontCache::new(self.fonts.clone()))),
font: Arc::new(RwLock::new(&mut self.fonts_cache)),
}
}
}
6 changes: 3 additions & 3 deletions core/src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use std::{

/// The caches used by the Renderer. Passed to [`Component#render`][crate::Component#method.render] in a [`RenderContext`][crate::RenderContext].
#[derive(Clone)]
pub struct Caches {
pub struct Caches<'a> {
/// Font cache
pub font: Arc<RwLock<FontCache>>,
pub font: Arc<RwLock<&'a mut FontCache>>,
}

pub trait RendererContext {}
Expand All @@ -37,5 +37,5 @@ pub(crate) trait Renderer: fmt::Debug + std::marker::Sized + Send + Sync {
fn resize(&mut self, width: u32, height: u32) {}
// use this method to clear any saved references or caches
fn clear(&mut self) {}
fn caches(&self) -> Caches;
fn caches(&mut self) -> Caches;
}
6 changes: 4 additions & 2 deletions core/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,17 @@ impl<
new.view(Some(&mut old), &mut new_registrations);
*registrations.write().unwrap() = new_registrations;

let renderer = renderer.read().unwrap();
let mut renderer = renderer.write().unwrap();

if renderer.is_none() {
*node_dirty.write().unwrap() = true;
return;
}

let caches: crate::renderer::Caches = renderer.as_ref().unwrap().caches();
let caches: crate::renderer::Caches = renderer.as_mut().unwrap().caches();
namana-mecha marked this conversation as resolved.
Show resolved Hide resolved

// A mutable reference to caches to cache the properties that are
// dynamically allocated by the nodes or componenets.
new.layout(&old, &mut caches.font.write().unwrap(), scale_factor);

do_render = new.render(caches, Some(&mut old), scale_factor);
Expand Down