From df90c478e28892537efc0009d468a521d4d7fee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Thu, 13 Feb 2020 03:45:07 +0100 Subject: [PATCH] Move `layer::Cached` to its own module --- wgpu/src/widget/canvas/layer.rs | 80 ++----------------------- wgpu/src/widget/canvas/layer/cached.rs | 82 ++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 76 deletions(-) create mode 100644 wgpu/src/widget/canvas/layer/cached.rs diff --git a/wgpu/src/widget/canvas/layer.rs b/wgpu/src/widget/canvas/layer.rs index c239a254..8c069f18 100644 --- a/wgpu/src/widget/canvas/layer.rs +++ b/wgpu/src/widget/canvas/layer.rs @@ -1,88 +1,16 @@ +mod cached; + +pub use cached::Cached; + use crate::{canvas::Frame, triangle}; use iced_native::Size; -use std::cell::RefCell; use std::sync::Arc; pub trait Layer: std::fmt::Debug { fn draw(&self, bounds: Size) -> Arc; } -use std::marker::PhantomData; - -#[derive(Debug)] -pub struct Cached { - input: PhantomData, - cache: RefCell, -} - -#[derive(Debug)] -enum Cache { - Empty, - Filled { - mesh: Arc, - bounds: Size, - }, -} - -impl Cached -where - T: Drawable + std::fmt::Debug, -{ - pub fn new() -> Self { - Cached { - input: PhantomData, - cache: RefCell::new(Cache::Empty), - } - } - - pub fn clear(&mut self) { - *self.cache.borrow_mut() = Cache::Empty; - } - - pub fn with<'a>(&'a self, input: &'a T) -> impl Layer + 'a { - Bind { - layer: self, - input: input, - } - } -} - -#[derive(Debug)] -struct Bind<'a, T: Drawable> { - layer: &'a Cached, - input: &'a T, -} - -impl<'a, T> Layer for Bind<'a, T> -where - T: Drawable + std::fmt::Debug, -{ - fn draw(&self, current_bounds: Size) -> Arc { - use std::ops::Deref; - - if let Cache::Filled { mesh, bounds } = - self.layer.cache.borrow().deref() - { - if *bounds == current_bounds { - return mesh.clone(); - } - } - - let mut frame = Frame::new(current_bounds.width, current_bounds.height); - self.input.draw(&mut frame); - - let mesh = Arc::new(frame.into_mesh()); - - *self.layer.cache.borrow_mut() = Cache::Filled { - mesh: mesh.clone(), - bounds: current_bounds, - }; - - mesh - } -} - pub trait Drawable { fn draw(&self, frame: &mut Frame); } diff --git a/wgpu/src/widget/canvas/layer/cached.rs b/wgpu/src/widget/canvas/layer/cached.rs new file mode 100644 index 00000000..c6741372 --- /dev/null +++ b/wgpu/src/widget/canvas/layer/cached.rs @@ -0,0 +1,82 @@ +use crate::{ + canvas::{layer::Drawable, Frame, Layer}, + triangle, +}; + +use iced_native::Size; +use std::cell::RefCell; +use std::marker::PhantomData; +use std::sync::Arc; + +#[derive(Debug)] +pub struct Cached { + input: PhantomData, + cache: RefCell, +} + +#[derive(Debug)] +enum Cache { + Empty, + Filled { + mesh: Arc, + bounds: Size, + }, +} + +impl Cached +where + T: Drawable + std::fmt::Debug, +{ + pub fn new() -> Self { + Cached { + input: PhantomData, + cache: RefCell::new(Cache::Empty), + } + } + + pub fn clear(&mut self) { + *self.cache.borrow_mut() = Cache::Empty; + } + + pub fn with<'a>(&'a self, input: &'a T) -> impl Layer + 'a { + Bind { + layer: self, + input: input, + } + } +} + +#[derive(Debug)] +struct Bind<'a, T: Drawable> { + layer: &'a Cached, + input: &'a T, +} + +impl<'a, T> Layer for Bind<'a, T> +where + T: Drawable + std::fmt::Debug, +{ + fn draw(&self, current_bounds: Size) -> Arc { + use std::ops::Deref; + + if let Cache::Filled { mesh, bounds } = + self.layer.cache.borrow().deref() + { + if *bounds == current_bounds { + return mesh.clone(); + } + } + + let mut frame = Frame::new(current_bounds.width, current_bounds.height); + self.input.draw(&mut frame); + + let mesh = Arc::new(frame.into_mesh()); + + *self.layer.cache.borrow_mut() = Cache::Filled { + mesh: mesh.clone(), + bounds: current_bounds, + }; + + mesh + } +}