1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Managed texture loading (#3297)

* add types from proposal

* add load methods on `egui::Context`

* implement loaders from proposal in `egui_extras`

* impl `From<Vec2>` for `SizeHint`

* re-export `SizeHint` from `egui` root

* rework `svg` example to use new managed `Image`

* split loaders into separate files + add logging

* add `log_trace`

* clean up `RetainedImage` from `svg` example

* refactor ehttp loader response to bytes mapping

* remove spammy trace

* load images even without extension

* fix lints

* remove unused imports

* use `Image2` in `download_image`

* use `visuals.error_fg_color` in `Image2` error state

* update lockfile

* use `Arc<ColorImage>` in `ImageData` + add `forget` API

* add `ui.image2`

* add byte size query api

* use iterators to sum loader byte sizes

* add static image loading

* use static image in `svg` example

* small refactor of `Image2::ui` texture loading code

* add `ImageFit` to size images properly

* remove println calls

* add bad image load to `download_image` example

* add loader file extension support tests

* fix lint errors in `loaders`

* remove unused `poll-promise` dependency

* add some docs to `Image2`

* add some docs to `egui_extras::loaders::install`

* explain `loaders::install` in examples

* fix lint

* upgrade `ehttp` to `0.3` for some crates

* Remove some unused dependencies

* Remove unnecessary context clone

* Turn on the `log` create feature of egui_extras in all examples

* rename `forget` and document it

* derive `Debug` on `SizeHint`

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>

* round when converting SizeHint from vec2

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>

* add `load` module docs

* docstring `add_loader` methods

* expose + document `load_include_bytes`

* cache texture handles in `DefaultTextureLoader`

* add `image2` doctest + further document `Image2`

* use `Default` for default `Image2` options

* update `image2` doc comment

* mention immediate-mode safety

* more fit calculation into inherent impl

* add hover text on spinner

* add `all-loaders` feature

* clarify `egui_extras::loaders::install` behavior

* explain how to enable image formats

* properly format `uri`

* use `thread::Builder` instead of `spawn`

* use eq op instead of `matches`

* inline `From<Arc<ColorImage>>` for `ImageData`

* allow non-`'static` bytes + `forget` in `DefaultTextureLoader`

* sort features

* change `ehttp` feature to `http`

* update `Image2` docs

* refactor loader cache type

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
Jan Procházka
2023-09-06 10:51:51 +02:00
committed by GitHub
parent 82704bebbf
commit ec671e754f
24 changed files with 1343 additions and 79 deletions

View File

@@ -0,0 +1,92 @@
use egui::{
ahash::HashMap,
load::{BytesPoll, ImageLoadResult, ImageLoader, ImagePoll, LoadError, SizeHint},
mutex::Mutex,
ColorImage,
};
use std::{mem::size_of, path::Path, sync::Arc};
type Entry = Result<Arc<ColorImage>, String>;
#[derive(Default)]
pub struct SvgLoader {
cache: Mutex<HashMap<(String, SizeHint), Entry>>,
}
fn is_supported(uri: &str) -> bool {
let Some(ext) = Path::new(uri).extension().and_then(|ext| ext.to_str()) else { return false };
ext == "svg"
}
impl ImageLoader for SvgLoader {
fn load(&self, ctx: &egui::Context, uri: &str, size_hint: SizeHint) -> ImageLoadResult {
if !is_supported(uri) {
return Err(LoadError::NotSupported);
}
let uri = uri.to_owned();
let mut cache = self.cache.lock();
// We can't avoid the `uri` clone here without unsafe code.
if let Some(entry) = cache.get(&(uri.clone(), size_hint)).cloned() {
match entry {
Ok(image) => Ok(ImagePoll::Ready { image }),
Err(err) => Err(LoadError::Custom(err)),
}
} else {
match ctx.try_load_bytes(&uri) {
Ok(BytesPoll::Ready { bytes, .. }) => {
crate::log_trace!("started loading {uri:?}");
let fit_to = match size_hint {
SizeHint::Original => usvg::FitTo::Original,
SizeHint::Width(w) => usvg::FitTo::Width(w),
SizeHint::Height(h) => usvg::FitTo::Height(h),
SizeHint::Size(w, h) => usvg::FitTo::Size(w, h),
};
let result =
crate::image::load_svg_bytes_with_size(&bytes, fit_to).map(Arc::new);
crate::log_trace!("finished loading {uri:?}");
cache.insert((uri, size_hint), result.clone());
match result {
Ok(image) => Ok(ImagePoll::Ready { image }),
Err(err) => Err(LoadError::Custom(err)),
}
}
Ok(BytesPoll::Pending { size }) => Ok(ImagePoll::Pending { size }),
Err(err) => Err(err),
}
}
}
fn forget(&self, uri: &str) {
self.cache.lock().retain(|(u, _), _| u != uri);
}
fn byte_size(&self) -> usize {
self.cache
.lock()
.values()
.map(|result| match result {
Ok(image) => image.pixels.len() * size_of::<egui::Color32>(),
Err(err) => err.len(),
})
.sum()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_support() {
// inverse of same test in `image_loader.rs`
assert!(!is_supported("https://test.png"));
assert!(!is_supported("test.jpeg"));
assert!(!is_supported("http://test.gif"));
assert!(!is_supported("test.webp"));
assert!(!is_supported("file://test"));
assert!(is_supported("test.svg"));
}
}