mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 22:30:03 -04:00
Remove 64 bit atomics in egui_extras (#8263)
This is a bit like my previous pr (<https://github.com/emilk/egui/pull/8037>), but addresses the final bit of 64 bit atomic dependent code, the svg loader in the `egui_extras` crate. As with the previous pr, this improves egui's compatibility on platforms without 64 bit atomics. * Closes <https://github.com/emilk/egui/issues/7692> * [x] I have followed the instructions in the PR template
This commit is contained in:
@@ -1,10 +1,4 @@
|
|||||||
use std::{
|
use std::{mem::size_of, sync::Arc};
|
||||||
mem::size_of,
|
|
||||||
sync::{
|
|
||||||
Arc,
|
|
||||||
atomic::{AtomicU64, Ordering::Relaxed},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
use ahash::HashMap;
|
use ahash::HashMap;
|
||||||
|
|
||||||
@@ -15,16 +9,20 @@ use egui::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct Entry {
|
struct Entry {
|
||||||
last_used: AtomicU64,
|
last_used: u64,
|
||||||
result: Result<Arc<ColorImage>, String>,
|
result: Result<Arc<ColorImage>, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SvgLoader {
|
struct State {
|
||||||
pass_index: AtomicU64,
|
pass_index: u64,
|
||||||
cache: Mutex<HashMap<String, HashMap<SizeHint, Entry>>>,
|
cache: HashMap<String, HashMap<SizeHint, Entry>>,
|
||||||
options: resvg::usvg::Options<'static>,
|
options: resvg::usvg::Options<'static>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct SvgLoader {
|
||||||
|
state: Mutex<State>,
|
||||||
|
}
|
||||||
|
|
||||||
impl SvgLoader {
|
impl SvgLoader {
|
||||||
pub const ID: &'static str = egui::generate_loader_id!(SvgLoader);
|
pub const ID: &'static str = egui::generate_loader_id!(SvgLoader);
|
||||||
}
|
}
|
||||||
@@ -43,9 +41,11 @@ impl Default for SvgLoader {
|
|||||||
options.fontdb_mut().load_system_fonts();
|
options.fontdb_mut().load_system_fonts();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
pass_index: AtomicU64::new(0),
|
state: Mutex::new(State {
|
||||||
cache: Mutex::new(HashMap::default()),
|
pass_index: 0,
|
||||||
options,
|
cache: HashMap::default(),
|
||||||
|
options,
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,13 +60,18 @@ impl ImageLoader for SvgLoader {
|
|||||||
return Err(LoadError::NotSupported);
|
return Err(LoadError::NotSupported);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut cache = self.cache.lock();
|
let mut state = self.state.lock();
|
||||||
|
let State {
|
||||||
|
pass_index,
|
||||||
|
cache,
|
||||||
|
options,
|
||||||
|
} = &mut *state;
|
||||||
|
|
||||||
let bucket = cache.entry(uri.to_owned()).or_default();
|
let bucket = cache.entry(uri.to_owned()).or_default();
|
||||||
|
|
||||||
if let Some(entry) = bucket.get(&size_hint) {
|
if let Some(entry) = bucket.get_mut(&size_hint) {
|
||||||
entry
|
entry.last_used = *pass_index;
|
||||||
.last_used
|
|
||||||
.store(self.pass_index.load(Relaxed), Relaxed);
|
|
||||||
match entry.result.clone() {
|
match entry.result.clone() {
|
||||||
Ok(image) => Ok(ImagePoll::Ready { image }),
|
Ok(image) => Ok(ImagePoll::Ready { image }),
|
||||||
Err(err) => Err(LoadError::Loading(err)),
|
Err(err) => Err(LoadError::Loading(err)),
|
||||||
@@ -75,15 +80,14 @@ impl ImageLoader for SvgLoader {
|
|||||||
match ctx.try_load_bytes(uri) {
|
match ctx.try_load_bytes(uri) {
|
||||||
Ok(BytesPoll::Ready { bytes, .. }) => {
|
Ok(BytesPoll::Ready { bytes, .. }) => {
|
||||||
log::trace!("Started loading {uri:?}");
|
log::trace!("Started loading {uri:?}");
|
||||||
let result =
|
let result = crate::image::load_svg_bytes_with_size(&bytes, size_hint, options)
|
||||||
crate::image::load_svg_bytes_with_size(&bytes, size_hint, &self.options)
|
.map(Arc::new);
|
||||||
.map(Arc::new);
|
|
||||||
|
|
||||||
log::trace!("Finished loading {uri:?}");
|
log::trace!("Finished loading {uri:?}");
|
||||||
bucket.insert(
|
bucket.insert(
|
||||||
size_hint,
|
size_hint,
|
||||||
Entry {
|
Entry {
|
||||||
last_used: AtomicU64::new(self.pass_index.load(Relaxed)),
|
last_used: *pass_index,
|
||||||
result: result.clone(),
|
result: result.clone(),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -99,16 +103,17 @@ impl ImageLoader for SvgLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn forget(&self, uri: &str) {
|
fn forget(&self, uri: &str) {
|
||||||
self.cache.lock().retain(|key, _| key != uri);
|
self.state.lock().cache.retain(|key, _| key != uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn forget_all(&self) {
|
fn forget_all(&self) {
|
||||||
self.cache.lock().clear();
|
self.state.lock().cache.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn byte_size(&self) -> usize {
|
fn byte_size(&self) -> usize {
|
||||||
self.cache
|
self.state
|
||||||
.lock()
|
.lock()
|
||||||
|
.cache
|
||||||
.values()
|
.values()
|
||||||
.flat_map(|bucket| bucket.values())
|
.flat_map(|bucket| bucket.values())
|
||||||
.map(|entry| match &entry.result {
|
.map(|entry| match &entry.result {
|
||||||
@@ -119,15 +124,17 @@ impl ImageLoader for SvgLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn end_pass(&self, pass_index: u64) {
|
fn end_pass(&self, pass_index: u64) {
|
||||||
self.pass_index.store(pass_index, Relaxed);
|
let mut state = self.state.lock();
|
||||||
let mut cache = self.cache.lock();
|
|
||||||
cache.retain(|_key, bucket| {
|
state.pass_index = pass_index;
|
||||||
|
|
||||||
|
state.cache.retain(|_key, bucket| {
|
||||||
if 2 <= bucket.len() {
|
if 2 <= bucket.len() {
|
||||||
// There are multiple images of the same URI (e.g. SVGs of different scales).
|
// There are multiple images of the same URI (e.g. SVGs of different scales).
|
||||||
// This could be because someone has an SVG in a resizable container,
|
// This could be because someone has an SVG in a resizable container,
|
||||||
// and so we get a lot of different sizes of it.
|
// and so we get a lot of different sizes of it.
|
||||||
// This could wast RAM, so we remove the ones that are not used in this frame.
|
// This could wast RAM, so we remove the ones that are not used in this frame.
|
||||||
bucket.retain(|_, texture| pass_index <= texture.last_used.load(Relaxed) + 1);
|
bucket.retain(|_, texture| pass_index <= texture.last_used + 1);
|
||||||
}
|
}
|
||||||
!bucket.is_empty()
|
!bucket.is_empty()
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user