156 lines
5.3 KiB
Rust
156 lines
5.3 KiB
Rust
//! The portraits of a card grid: decoded only for the cards it paints, and freed, in memory and
|
|
//! on the GPU, once it paints them no more (rule #1: memory in proportion to what is shown).
|
|
//!
|
|
//! Each grid owns one `CardImages`, which its cards' `img` elements read through (`provide`),
|
|
//! never through GPUI's app-wide asset cache, which keeps every image it ever loaded. A grid may
|
|
//! be a cached view: while it is not rendered again, its last paint is replayed with the images
|
|
//! it used. So the cache only changes in the grid's own render: each one starts with
|
|
//! `start_render`, which drops what the previous render did not paint, as no frame shows it.
|
|
|
|
use std::collections::HashMap;
|
|
use std::sync::Arc;
|
|
|
|
use gpui_kit::*;
|
|
|
|
struct Entry {
|
|
/// The image, or its load while it runs, or why it failed.
|
|
item: ImageCacheItem,
|
|
/// The last render that painted it.
|
|
painted: u64,
|
|
}
|
|
|
|
pub struct CardImages {
|
|
entries: HashMap<Resource, Entry>,
|
|
/// The grid's render being drawn.
|
|
render: u64,
|
|
/// The grid is painting: `uniform_list` also lays rows out only to measure them, and a row
|
|
/// that is not painted is not shown, so its portraits are neither loaded nor kept.
|
|
painting: bool,
|
|
/// Loads started, to guard against retrying a failed one every frame.
|
|
#[cfg(test)]
|
|
pub(crate) loads: usize,
|
|
}
|
|
|
|
impl CardImages {
|
|
pub fn new(cx: &mut App) -> Entity<CardImages> {
|
|
let images = cx.new(|_| CardImages {
|
|
entries: HashMap::new(),
|
|
render: 0,
|
|
painting: false,
|
|
#[cfg(test)]
|
|
loads: 0,
|
|
});
|
|
// The grid is gone: so are its portraits, from every window's atlas too.
|
|
cx.observe_release(&images, |images, cx| {
|
|
for (_, entry) in images.entries.drain() {
|
|
if let Some(Ok(image)) = entry.item.get() {
|
|
cx.drop_image(image, None);
|
|
}
|
|
}
|
|
})
|
|
.detach();
|
|
images
|
|
}
|
|
|
|
/// Drops every portrait, in memory and in the window's atlas, for a grid that is no longer
|
|
/// shown: they load again when it is drawn. A load still running is cancelled. True when
|
|
/// something was held.
|
|
pub fn release(&mut self, window: &mut Window, cx: &mut App) -> bool {
|
|
let held = !self.entries.is_empty();
|
|
for (_, entry) in self.entries.drain() {
|
|
if let Some(Ok(image)) = entry.item.get() {
|
|
cx.drop_image(image, Some(window));
|
|
}
|
|
}
|
|
held
|
|
}
|
|
|
|
/// Called first in each render of the grid. The frame on screen shows what the previous
|
|
/// render painted, and the frame being drawn shows only what this one will paint: anything
|
|
/// else is dropped, in memory and in the window's atlas. A load still running is cancelled.
|
|
pub fn start_render(&mut self, window: &mut Window, cx: &mut App) {
|
|
let previous = self.render;
|
|
self.entries.retain(|_, entry| {
|
|
if entry.painted == previous {
|
|
return true;
|
|
}
|
|
if let Some(Ok(image)) = entry.item.get() {
|
|
cx.drop_image(image, Some(window));
|
|
}
|
|
false
|
|
});
|
|
self.render += 1;
|
|
self.painting = false;
|
|
}
|
|
}
|
|
|
|
impl ImageCache for CardImages {
|
|
/// While the grid lays out, an image already there is given back and nothing is loaded.
|
|
/// While it paints, a missing image starts loading off the UI thread, and its end draws the
|
|
/// grid again. A failed load is kept, so it is not tried again while the card stays shown.
|
|
fn load(
|
|
&mut self,
|
|
resource: &Resource,
|
|
window: &mut Window,
|
|
cx: &mut App,
|
|
) -> Option<Result<Arc<RenderImage>, ImageCacheError>> {
|
|
if !self.painting {
|
|
return self
|
|
.entries
|
|
.get(resource)
|
|
.and_then(|entry| entry.item.get());
|
|
}
|
|
let render = self.render;
|
|
let entry = self.entries.entry(resource.clone()).or_insert_with(|| {
|
|
#[cfg(test)]
|
|
{
|
|
self.loads += 1;
|
|
}
|
|
Entry {
|
|
item: ImageCacheItem::new(resource, cx),
|
|
painted: render,
|
|
}
|
|
});
|
|
entry.painted = render;
|
|
entry.item.use_image(window)
|
|
}
|
|
}
|
|
|
|
/// `root`, whose `img` elements read through `images`. `root` gets its children after this:
|
|
/// a mark painted before them tells `images` that the grid paints.
|
|
pub fn provide(root: Div, images: Entity<CardImages>) -> Div {
|
|
let painting = images.clone();
|
|
root.image_cache(images).child(
|
|
canvas(
|
|
|_, _, _| {},
|
|
move |_, _, _, cx| painting.update(cx, |images, _| images.painting = true),
|
|
)
|
|
.absolute(),
|
|
)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
impl CardImages {
|
|
/// The portraits held, loaded or not, sorted.
|
|
pub(crate) fn held(&self) -> Vec<std::path::PathBuf> {
|
|
let mut paths: Vec<_> = (self.entries.keys())
|
|
.filter_map(|resource| match resource {
|
|
Resource::Path(path) => Some(path.to_path_buf()),
|
|
_ => None,
|
|
})
|
|
.collect();
|
|
paths.sort();
|
|
paths
|
|
}
|
|
|
|
/// The images decoded.
|
|
pub(crate) fn loaded(&self) -> Vec<Arc<RenderImage>> {
|
|
(self.entries.values())
|
|
.filter_map(|entry| entry.item.get()?.ok())
|
|
.collect()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
pub(crate) mod tests;
|