Retain caches in iced_wgpu as long as Rc values are alive

This allows reusing a `canvas::Cache` at no cost even if it
is not presented every frame.
This commit is contained in:
Héctor Ramón Jiménez 2024-04-27 14:16:12 +02:00
parent d5bb6deb2f
commit 2dcd4f916e
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 12 additions and 14 deletions

View file

@ -4,9 +4,9 @@ use crate::graphics::color;
use crate::graphics::text::cache::{self, Cache as BufferCache}; use crate::graphics::text::cache::{self, Cache as BufferCache};
use crate::graphics::text::{font_system, to_color, Editor, Paragraph}; use crate::graphics::text::{font_system, to_color, Editor, Paragraph};
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::FxHashMap;
use std::collections::hash_map; use std::collections::hash_map;
use std::rc::Rc; use std::rc::{self, Rc};
use std::sync::atomic::{self, AtomicU64}; use std::sync::atomic::{self, AtomicU64};
use std::sync::Arc; use std::sync::Arc;
@ -69,12 +69,12 @@ struct Upload {
buffer_cache: BufferCache, buffer_cache: BufferCache,
transformation: Transformation, transformation: Transformation,
version: usize, version: usize,
text: rc::Weak<[Text]>,
} }
#[derive(Default)] #[derive(Default)]
pub struct Storage { pub struct Storage {
uploads: FxHashMap<Id, Upload>, uploads: FxHashMap<Id, Upload>,
recently_used: FxHashSet<Id>,
} }
impl Storage { impl Storage {
@ -162,6 +162,7 @@ impl Storage {
buffer_cache, buffer_cache,
transformation: new_transformation, transformation: new_transformation,
version: 0, version: 0,
text: Rc::downgrade(&cache.text),
}); });
log::info!( log::info!(
@ -171,13 +172,11 @@ impl Storage {
); );
} }
} }
let _ = self.recently_used.insert(cache.id);
} }
pub fn trim(&mut self) { pub fn trim(&mut self) {
self.uploads.retain(|id, _| self.recently_used.contains(id)); self.uploads
self.recently_used.clear(); .retain(|_id, upload| upload.text.strong_count() > 0);
} }
} }

View file

@ -6,9 +6,9 @@ use crate::graphics::mesh::{self, Mesh};
use crate::graphics::Antialiasing; use crate::graphics::Antialiasing;
use crate::Buffer; use crate::Buffer;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::FxHashMap;
use std::collections::hash_map; use std::collections::hash_map;
use std::rc::Rc; use std::rc::{self, Rc};
use std::sync::atomic::{self, AtomicU64}; use std::sync::atomic::{self, AtomicU64};
const INITIAL_INDEX_COUNT: usize = 1_000; const INITIAL_INDEX_COUNT: usize = 1_000;
@ -64,12 +64,12 @@ struct Upload {
layer: Layer, layer: Layer,
transformation: Transformation, transformation: Transformation,
version: usize, version: usize,
batch: rc::Weak<[Mesh]>,
} }
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Storage { pub struct Storage {
uploads: FxHashMap<Id, Upload>, uploads: FxHashMap<Id, Upload>,
recently_used: FxHashSet<Id>,
} }
impl Storage { impl Storage {
@ -134,6 +134,7 @@ impl Storage {
layer, layer,
transformation: new_transformation, transformation: new_transformation,
version: 0, version: 0,
batch: Rc::downgrade(&cache.batch),
}); });
log::info!( log::info!(
@ -143,13 +144,11 @@ impl Storage {
); );
} }
} }
let _ = self.recently_used.insert(cache.id);
} }
pub fn trim(&mut self) { pub fn trim(&mut self) {
self.uploads.retain(|id, _| self.recently_used.contains(id)); self.uploads
self.recently_used.clear(); .retain(|_id, upload| upload.batch.strong_count() > 0);
} }
} }