-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
158 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use super::*; | ||
|
||
#[derive(Default)] | ||
pub struct DefaultBytesLoader { | ||
cache: Mutex<HashMap<Cow<'static, str>, Bytes>>, | ||
} | ||
|
||
impl DefaultBytesLoader { | ||
pub fn insert(&self, uri: impl Into<Cow<'static, str>>, bytes: impl Into<Bytes>) { | ||
self.cache | ||
.lock() | ||
.entry(uri.into()) | ||
.or_insert_with_key(|uri| { | ||
let bytes: Bytes = bytes.into(); | ||
|
||
#[cfg(feature = "log")] | ||
log::trace!("loaded {} bytes for uri {uri:?}", bytes.len()); | ||
|
||
bytes | ||
}); | ||
} | ||
} | ||
|
||
impl BytesLoader for DefaultBytesLoader { | ||
fn id(&self) -> &str { | ||
generate_loader_id!(DefaultBytesLoader) | ||
} | ||
|
||
fn load(&self, _: &Context, uri: &str) -> BytesLoadResult { | ||
match self.cache.lock().get(uri).cloned() { | ||
Some(bytes) => Ok(BytesPoll::Ready { | ||
size: None, | ||
bytes, | ||
mime: None, | ||
}), | ||
None => Err(LoadError::NotSupported), | ||
} | ||
} | ||
|
||
fn forget(&self, uri: &str) { | ||
#[cfg(feature = "log")] | ||
log::trace!("forget {uri:?}"); | ||
|
||
let _ = self.cache.lock().remove(uri); | ||
} | ||
|
||
fn forget_all(&self) { | ||
#[cfg(feature = "log")] | ||
log::trace!("forget all"); | ||
|
||
self.cache.lock().clear(); | ||
} | ||
|
||
fn byte_size(&self) -> usize { | ||
self.cache.lock().values().map(|bytes| bytes.len()).sum() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use super::*; | ||
|
||
#[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); | ||
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.