Skip to content

Commit

Permalink
make ImageLoader use background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
bircni committed Nov 20, 2024
1 parent 83a3006 commit 10e496f
Showing 1 changed file with 34 additions and 15 deletions.
49 changes: 34 additions & 15 deletions crates/egui_extras/src/loaders/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use egui::{
ColorImage,
};
use image::ImageFormat;
use std::{mem::size_of, path::Path, sync::Arc};
use std::{mem::size_of, path::Path, sync::Arc, task::Poll, thread};

type Entry = Result<Arc<ColorImage>, String>;
type Entry = Poll<Result<Arc<ColorImage>, String>>;

#[derive(Default)]
pub struct ImageCrateLoader {
cache: Mutex<HashMap<String, Entry>>,
cache: Arc<Mutex<HashMap<String, Entry>>>,
}

impl ImageCrateLoader {
Expand Down Expand Up @@ -58,8 +58,9 @@ impl ImageLoader for ImageCrateLoader {
let mut cache = self.cache.lock();
if let Some(entry) = cache.get(uri).cloned() {
match entry {
Ok(image) => Ok(ImagePoll::Ready { image }),
Err(err) => Err(LoadError::Loading(err)),
Poll::Ready(Ok(image)) => Ok(ImagePoll::Ready { image }),
Poll::Ready(Err(err)) => Err(LoadError::Loading(err)),
Poll::Pending => Ok(ImagePoll::Pending { size: None }),
}
} else {
match ctx.try_load_bytes(uri) {
Expand All @@ -71,14 +72,31 @@ impl ImageLoader for ImageCrateLoader {
return Err(LoadError::NotSupported);
}

log::trace!("started loading {uri:?}");
let result = crate::image::load_image_bytes(&bytes).map(Arc::new);
log::trace!("finished loading {uri:?}");
cache.insert(uri.into(), result.clone());
match result {
Ok(image) => Ok(ImagePoll::Ready { image }),
Err(err) => Err(LoadError::Loading(err)),
}
let uri = uri.to_owned();
cache.insert(uri.clone(), Poll::Pending);
drop(cache);

// Do the image parsing on a bg thread
thread::Builder::new()
.name(format!("egui_extras::ImageLoader::load({uri:?})"))
.spawn({
let ctx = ctx.clone();
let cache = self.cache.clone();

let uri = uri.clone();
move || {
log::trace!("ImageLoader - started loading {uri:?}");
let result = crate::image::load_image_bytes(&bytes).map(Arc::new);
log::trace!("ImageLoader - finished loading {uri:?}");
let prev = cache.lock().insert(uri, Poll::Ready(result));
assert!(matches!(prev, Some(Poll::Pending)));

ctx.request_repaint();
}
})
.expect("failed to spawn thread");

Ok(ImagePoll::Pending { size: None })
}
Ok(BytesPoll::Pending { size }) => Ok(ImagePoll::Pending { size }),
Err(err) => Err(err),
Expand All @@ -99,8 +117,9 @@ impl ImageLoader for ImageCrateLoader {
.lock()
.values()
.map(|result| match result {
Ok(image) => image.pixels.len() * size_of::<egui::Color32>(),
Err(err) => err.len(),
Poll::Ready(Ok(image)) => image.pixels.len() * size_of::<egui::Color32>(),
Poll::Ready(Err(err)) => err.len(),
Poll::Pending => 0,
})
.sum()
}
Expand Down

0 comments on commit 10e496f

Please sign in to comment.