Allow to load an image from memory
New `image::Handle` opaque type uniquely identifying some `image::Data`, allowing reliable caching.
This commit is contained in:
parent
811d8b90d7
commit
505588d585
5 changed files with 133 additions and 33 deletions
|
|
@ -1,11 +1,14 @@
|
|||
use crate::Transformation;
|
||||
use iced_native::Rectangle;
|
||||
use iced_native::{
|
||||
image::{Data, Handle},
|
||||
Rectangle,
|
||||
};
|
||||
|
||||
use std::{cell::RefCell, collections::HashMap, mem, rc::Rc};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Pipeline {
|
||||
cache: RefCell<HashMap<String, Memory>>,
|
||||
cache: RefCell<HashMap<u64, Memory>>,
|
||||
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
uniforms: wgpu::Buffer,
|
||||
|
|
@ -197,23 +200,36 @@ impl Pipeline {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn dimensions(&self, path: &str) -> (u32, u32) {
|
||||
self.load(path);
|
||||
pub fn dimensions(&self, handle: &Handle) -> (u32, u32) {
|
||||
self.load(handle);
|
||||
|
||||
self.cache.borrow().get(path).unwrap().dimensions()
|
||||
self.cache.borrow().get(&handle.id()).unwrap().dimensions()
|
||||
}
|
||||
|
||||
fn load(&self, path: &str) {
|
||||
if !self.cache.borrow().contains_key(path) {
|
||||
let memory = if let Ok(image) = image::open(path) {
|
||||
Memory::Host {
|
||||
image: image.to_bgra(),
|
||||
fn load(&self, handle: &Handle) {
|
||||
if !self.cache.borrow().contains_key(&handle.id()) {
|
||||
let memory = match handle.data() {
|
||||
Data::Path(path) => {
|
||||
if let Ok(image) = image::open(path) {
|
||||
Memory::Host {
|
||||
image: image.to_bgra(),
|
||||
}
|
||||
} else {
|
||||
Memory::NotFound
|
||||
}
|
||||
}
|
||||
Data::Bytes(bytes) => {
|
||||
if let Ok(image) = image::load_from_memory(&bytes) {
|
||||
Memory::Host {
|
||||
image: image.to_bgra(),
|
||||
}
|
||||
} else {
|
||||
Memory::Invalid
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Memory::NotFound
|
||||
};
|
||||
|
||||
let _ = self.cache.borrow_mut().insert(path.to_string(), memory);
|
||||
let _ = self.cache.borrow_mut().insert(handle.id(), memory);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -245,12 +261,12 @@ impl Pipeline {
|
|||
//
|
||||
// [1]: https://github.com/nical/guillotiere
|
||||
for image in instances {
|
||||
self.load(&image.path);
|
||||
self.load(&image.handle);
|
||||
|
||||
if let Some(texture) = self
|
||||
.cache
|
||||
.borrow_mut()
|
||||
.get_mut(&image.path)
|
||||
.get_mut(&image.handle.id())
|
||||
.unwrap()
|
||||
.upload(device, encoder, &self.texture_layout)
|
||||
{
|
||||
|
|
@ -327,6 +343,7 @@ enum Memory {
|
|||
height: u32,
|
||||
},
|
||||
NotFound,
|
||||
Invalid,
|
||||
}
|
||||
|
||||
impl Memory {
|
||||
|
|
@ -335,6 +352,7 @@ impl Memory {
|
|||
Memory::Host { image } => image.dimensions(),
|
||||
Memory::Device { width, height, .. } => (*width, *height),
|
||||
Memory::NotFound => (1, 1),
|
||||
Memory::Invalid => (1, 1),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -417,12 +435,13 @@ impl Memory {
|
|||
}
|
||||
Memory::Device { bind_group, .. } => Some(bind_group.clone()),
|
||||
Memory::NotFound => None,
|
||||
Memory::Invalid => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Image {
|
||||
pub path: String,
|
||||
pub handle: Handle,
|
||||
pub position: [f32; 2],
|
||||
pub scale: [f32; 2],
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue