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 } => previous.as_ref().map(|data| data.geometry.clone()),
88            cache::State::Filled { current } => {
89                if current.bounds == bounds {
90                    return Cached::load(&current.geometry);
91                }
92
93                Some(current.geometry.clone())
94            }
95        };
96
97        let mut frame = Frame::with_bounds(renderer, bounds);
98        draw_fn(&mut frame);
99
100        let geometry = frame.into_geometry().cache(self.raw.group(), previous);
101        let result = Cached::load(&geometry);
102
103        *state.borrow_mut() = cache::State::Filled {
104            current: Data { bounds, geometry },
105        };
106
107        result
108    }
109}
110
111impl<Renderer> std::fmt::Debug for Cache<Renderer>
112where
113    Renderer: geometry::Renderer,
114    <Renderer::Geometry as Cached>::Cache: std::fmt::Debug,
115{
116    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117        write!(f, "{:?}", &self.raw)
118    }
119}
120
121impl<Renderer> Default for Cache<Renderer>
122where
123    Renderer: geometry::Renderer,
124{
125    fn default() -> Self {
126        Self::new()
127    }
128}