Address initial review feedback

* Add comment about naughty ptr2int cast
* Change Option to anyhow::Result in test scene ImageCache
* Replace magic constant with pixel stride from selected format
This commit is contained in:
Chad Brokaw 2023-03-10 15:14:34 -05:00
parent d12b711fe1
commit 37f05b853c
3 changed files with 22 additions and 17 deletions

View file

@ -16,38 +16,36 @@ impl ImageCache {
Self::default()
}
pub fn from_file(&mut self, path: impl AsRef<Path>) -> Option<Image> {
pub fn from_file(&mut self, path: impl AsRef<Path>) -> anyhow::Result<Image> {
let path = path.as_ref();
if let Some(image) = self.files.get(path) {
Some(image.clone())
Ok(image.clone())
} else {
let data = std::fs::read(path).ok()?;
let data = std::fs::read(path)?;
let image = decode_image(&data)?;
self.files.insert(path.to_owned(), image.clone());
Some(image)
Ok(image)
}
}
pub fn from_bytes(&mut self, key: usize, bytes: &[u8]) -> Option<Image> {
pub fn from_bytes(&mut self, key: usize, bytes: &[u8]) -> anyhow::Result<Image> {
if let Some(image) = self.bytes.get(&key) {
Some(image.clone())
Ok(image.clone())
} else {
let image = decode_image(bytes)?;
self.bytes.insert(key, image.clone());
Some(image)
Ok(image)
}
}
}
fn decode_image(data: &[u8]) -> Option<Image> {
fn decode_image(data: &[u8]) -> anyhow::Result<Image> {
let image = image::io::Reader::new(std::io::Cursor::new(data))
.with_guessed_format()
.ok()?
.decode()
.ok()?;
.with_guessed_format()?
.decode()?;
let width = image.width();
let height = image.height();
let data = Arc::new(image.into_rgba8().into_vec());
let blob = Blob::new(data);
Some(Image::new(blob, Format::Rgba8, width, height))
Ok(Image::new(blob, Format::Rgba8, width, height))
}

View file

@ -101,6 +101,8 @@ fn cardioid_and_friends(sb: &mut SceneBuilder, _: &mut SceneParams) {
}
fn animated_text(sb: &mut SceneBuilder, params: &mut SceneParams) {
// Uses the static array address as a cache key for expedience. Real code
// should use a better strategy.
let piet_logo = params
.images
.from_bytes(PIET_LOGO_IMAGE.as_ptr() as usize, PIET_LOGO_IMAGE)

View file

@ -299,7 +299,7 @@ impl Engine {
base_mip_level: 0,
base_array_layer: 0,
array_layer_count: None,
format: Some(TextureFormat::Rgba8Unorm),
format: Some(format),
});
queue.write_texture(
wgpu::ImageCopyTexture {
@ -311,7 +311,9 @@ impl Engine {
bytes,
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: NonZeroU32::new(image_proxy.width * 4),
bytes_per_row: NonZeroU32::new(
image_proxy.width * format.describe().block_size as u32,
),
rows_per_image: None,
},
wgpu::Extent3d {
@ -325,6 +327,7 @@ impl Engine {
}
Command::WriteImage(proxy, [x, y, width, height], data) => {
if let Ok((texture, _)) = self.bind_map.get_or_create_image(*proxy, device) {
let format = proxy.format.to_wgpu();
queue.write_texture(
wgpu::ImageCopyTexture {
texture: &texture,
@ -335,7 +338,9 @@ impl Engine {
&data[..],
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: NonZeroU32::new(*width * 4),
bytes_per_row: NonZeroU32::new(
*width * format.describe().block_size as u32,
),
rows_per_image: None,
},
wgpu::Extent3d {
@ -778,7 +783,7 @@ impl BindMap {
base_mip_level: 0,
base_array_layer: 0,
array_layer_count: None,
format: Some(TextureFormat::Rgba8Unorm),
format: Some(proxy.format.to_wgpu()),
});
Ok(vacant.insert((texture, texture_view)))
}