iced_graphics/geometry/
cache.rs

1use crate::cache::{self, Cached};
2use crate::core::{Rectangle, Size};
3use crate::geometry::{self, Frame};
4
5pub use cache::Group;
6
7/// A simple cache that stores generated geometry to avoid recomputation.
8///
9/// A [`Cache`] will not redraw its geometry unless the dimensions of its layer
10/// change or it is explicitly cleared.
11pub struct Cache<Renderer>
12where
13    Renderer: geometry::Renderer,
14{
15    raw: crate::Cache<Data<<Renderer::Geometry as Cached>::Cache>>,
16}
17
18#[derive(Debug, Clone)]
19struct Data<T> {
20    bounds: Rectangle,
21    geometry: T,
22}
23
24impl<Renderer> Cache<Renderer>
25where
26    Renderer: geometry::Renderer,
27{
28    /// Creates a new empty [`Cache`].
29    pub fn new() -> Self {
30        Cache {
31            raw: cache::Cache::new(),
32        }
33    }
34
35    /// Creates a new empty [`Cache`] with the given [`Group`].
36    ///
37    /// Caches within the same group may reuse internal rendering storage.
38    ///
39    /// You should generally group caches that are likely to change
40    /// together.
41    pub fn with_group(group: Group) -> Self {
42        Cache {
43            raw: crate::Cache::with_group(group),
44        }
45    }
46
47    /// Clears the [`Cache`], forcing a redraw the next time it is used.
48    pub fn clear(&self) {
49        self.raw.clear();
50    }
51
52    /// Draws geometry using the provided closure and stores it in the
53    /// [`Cache`].
54    ///
55    /// The closure will only be called when
56    /// - the size has changed since the previous draw call.
57    /// - the [`Cache`] is empty or has been explicitly cleared.
58    ///
59    /// Otherwise, the previously stored geometry will be returned. The
60    /// [`Cache`] is not cleared in this case. In other words, it will keep
61    /// returning the stored geometry if needed.
62    pub fn draw(
63        &self,
64        renderer: &Renderer,
65        size: Size,
66        draw_fn: impl FnOnce(&mut Frame<Renderer>),
67    ) -> Renderer::Geometry {
68        self.draw_with_bounds(renderer, Rectangle::with_size(size), draw_fn)
69    }
70
71    /// Draws geometry using the provided closure and stores it in the
72    /// [`Cache`].
73    ///
74    /// Analogous to [`draw`](Self::draw), but takes a clipping [`Rectangle`] instead of
75    /// a [`Size`].
76    pub fn draw_with_bounds(
77        &self,
78        renderer: &Renderer,
79        bounds: Rectangle,
80        draw_fn: impl FnOnce(&mut Frame<Renderer>),
81    ) -> Renderer::Geometry {
82        use std::ops::Deref;
83
84        let state = self.raw.state();
85
86        let previous = match state.borrow().deref() {
87            cache::State::Empty { previous } => {
88                previous.as_ref().map(|data| data.geometry.clone())
89            }
90            cache::State::Filled { current } => {
91                if current.bounds == bounds {
92                    return Cached::load(&current.geometry);
93                }
94
95                Some(current.geometry.clone())
96            }
97        };
98
99        let mut frame = Frame::with_bounds(renderer, bounds);
100        draw_fn(&mut frame);
101
102        let geometry = frame.into_geometry().cache(self.raw.group(), previous);
103        let result = Cached::load(&geometry);
104
105        *state.borrow_mut() = cache::State::Filled {
106            current: Data { bounds, geometry },
107        };
108
109        result
110    }
111}
112
113impl<Renderer> std::fmt::Debug for Cache<Renderer>
114where
115    Renderer: geometry::Renderer,
116    <Renderer::Geometry as Cached>::Cache: std::fmt::Debug,
117{
118    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119        write!(f, "{:?}", &self.raw)
120    }
121}
122
123impl<Renderer> Default for Cache<Renderer>
124where
125    Renderer: geometry::Renderer,
126{
127    fn default() -> Self {
128        Self::new()
129    }
130}