iced-yoda/wgpu/src/widget/canvas/cache.rs

97 lines
2.3 KiB
Rust
Raw Normal View History

2020-02-13 03:45:07 +01:00
use crate::{
canvas::{Frame, Geometry},
Primitive,
2020-02-13 03:45:07 +01:00
};
use iced_native::Size;
use std::{cell::RefCell, sync::Arc};
2020-02-13 03:45:07 +01:00
2020-04-08 23:07:42 +01:00
enum State {
Empty,
Filled {
bounds: Size,
primitive: Arc<Primitive>,
},
}
impl Default for State {
fn default() -> Self {
State::Empty
}
}
/// A simple cache that stores generated geometry to avoid recomputation.
///
/// A [`Cache`] will not redraw its geometry unless the dimensions of its layer
/// change or it is explicitly cleared.
///
/// [`Layer`]: ../trait.Layer.html
2020-04-01 04:34:14 +02:00
/// [`Cache`]: struct.Cache.html
#[derive(Debug, Default)]
pub struct Cache {
state: RefCell<State>,
2020-02-13 03:45:07 +01:00
}
impl Cache {
/// Creates a new empty [`Cache`].
///
/// [`Cache`]: struct.Cache.html
2020-02-13 03:45:07 +01:00
pub fn new() -> Self {
Cache {
2020-04-08 23:07:42 +01:00
state: Default::default(),
2020-02-13 03:45:07 +01:00
}
}
/// Clears the cache, forcing a redraw the next time it is used.
///
/// [`Cached`]: struct.Cached.html
2020-02-13 03:45:07 +01:00
pub fn clear(&mut self) {
*self.state.borrow_mut() = State::Empty;
2020-02-13 03:45:07 +01:00
}
pub fn draw(
&self,
new_bounds: Size,
draw_fn: impl Fn(&mut Frame),
) -> Geometry {
2020-02-13 03:45:07 +01:00
use std::ops::Deref;
if let State::Filled { bounds, primitive } = self.state.borrow().deref()
2020-02-13 03:45:07 +01:00
{
if *bounds == new_bounds {
return Geometry::from_primitive(Primitive::Cached {
cache: primitive.clone(),
});
2020-02-13 03:45:07 +01:00
}
}
2020-04-28 03:16:18 +02:00
let mut frame = Frame::new(new_bounds);
draw_fn(&mut frame);
2020-02-13 03:45:07 +01:00
let primitive = {
let geometry = frame.into_geometry();
Arc::new(geometry.into_primitive())
};
2020-02-13 03:45:07 +01:00
*self.state.borrow_mut() = State::Filled {
bounds: new_bounds,
primitive: primitive.clone(),
2020-02-13 03:45:07 +01:00
};
Geometry::from_primitive(Primitive::Cached { cache: primitive })
}
2020-02-13 03:45:07 +01:00
}
impl std::fmt::Debug for State {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
State::Empty => write!(f, "Empty"),
State::Filled { primitive, bounds } => f
.debug_struct("Filled")
.field("primitive", primitive)
.field("bounds", bounds)
.finish(),
}
}
}