mirror of
https://github.com/emilk/egui.git
synced 2026-06-26 22:53:14 -04:00
<!-- Please read the "Making a PR" section of [`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md) before opening a Pull Request! * Keep your PR:s small and focused. * The PR title is what ends up in the changelog, so make it descriptive! * If applicable, add a screenshot or gif. * If it is a non-trivial addition, consider adding a demo for it to `egui_demo_lib`, or a new example. * Do NOT open PR:s from your `master` branch, as that makes it hard for maintainers to test and add commits to your PR. * Remember to run `cargo fmt` and `cargo clippy`. * Open the PR as a draft until you have self-reviewed it and run `./scripts/check.sh`. * When you have addressed a PR comment, mark it as resolved. Please be patient! I will review your PR, but my time is limited! --> I removed (I hope so) all wildcard imports I found. For me on my pc this improved the build time: - for egui -5s - for eframe -12s * [x] I have followed the instructions in the PR template
75 lines
2.4 KiB
Rust
75 lines
2.4 KiB
Rust
use super::{
|
|
BytesLoader, Context, HashMap, ImagePoll, Mutex, SizeHint, SizedTexture, TextureHandle,
|
|
TextureLoadResult, TextureLoader, TextureOptions, TexturePoll,
|
|
};
|
|
|
|
#[derive(Default)]
|
|
pub struct DefaultTextureLoader {
|
|
cache: Mutex<HashMap<(String, TextureOptions), TextureHandle>>,
|
|
}
|
|
|
|
impl TextureLoader for DefaultTextureLoader {
|
|
fn id(&self) -> &str {
|
|
crate::generate_loader_id!(DefaultTextureLoader)
|
|
}
|
|
|
|
fn load(
|
|
&self,
|
|
ctx: &Context,
|
|
uri: &str,
|
|
texture_options: TextureOptions,
|
|
size_hint: SizeHint,
|
|
) -> TextureLoadResult {
|
|
let mut cache = self.cache.lock();
|
|
if let Some(handle) = cache.get(&(uri.into(), texture_options)) {
|
|
let texture = SizedTexture::from_handle(handle);
|
|
Ok(TexturePoll::Ready { texture })
|
|
} else {
|
|
match ctx.try_load_image(uri, size_hint)? {
|
|
ImagePoll::Pending { size } => Ok(TexturePoll::Pending { size }),
|
|
ImagePoll::Ready { image } => {
|
|
let handle = ctx.load_texture(uri, image, texture_options);
|
|
let texture = SizedTexture::from_handle(&handle);
|
|
cache.insert((uri.into(), texture_options), handle);
|
|
let reduce_texture_memory = ctx.options(|o| o.reduce_texture_memory);
|
|
if reduce_texture_memory {
|
|
let loaders = ctx.loaders();
|
|
loaders.include.forget(uri);
|
|
for loader in loaders.bytes.lock().iter().rev() {
|
|
loader.forget(uri);
|
|
}
|
|
for loader in loaders.image.lock().iter().rev() {
|
|
loader.forget(uri);
|
|
}
|
|
}
|
|
Ok(TexturePoll::Ready { texture })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn forget(&self, uri: &str) {
|
|
#[cfg(feature = "log")]
|
|
log::trace!("forget {uri:?}");
|
|
|
|
self.cache.lock().retain(|(u, _), _| u != uri);
|
|
}
|
|
|
|
fn forget_all(&self) {
|
|
#[cfg(feature = "log")]
|
|
log::trace!("forget all");
|
|
|
|
self.cache.lock().clear();
|
|
}
|
|
|
|
fn end_frame(&self, _: usize) {}
|
|
|
|
fn byte_size(&self) -> usize {
|
|
self.cache
|
|
.lock()
|
|
.values()
|
|
.map(|texture| texture.byte_size())
|
|
.sum()
|
|
}
|
|
}
|