Skip to main content

zest_core/
dirty.rs

1//! Dirty-region tracking and platform rendering capabilities.
2
3use alloc::{vec, vec::Vec};
4use embedded_graphics::primitives::Rectangle;
5
6/// Dirty regions to redraw on the next frame.
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub enum DirtyRegion {
9    /// Nothing changed; drawing can be skipped.
10    None,
11    /// The whole surface should be redrawn.
12    Full,
13    /// Redraw the listed rectangles.
14    Rects(Vec<Rectangle>),
15}
16
17impl DirtyRegion {
18    /// Nothing is dirty.
19    #[must_use]
20    pub const fn none() -> Self {
21        Self::None
22    }
23
24    /// The whole surface is dirty.
25    #[must_use]
26    pub const fn full() -> Self {
27        Self::Full
28    }
29
30    /// A single dirty rectangle.
31    #[must_use]
32    pub fn rect(rect: Rectangle) -> Self {
33        Self::Rects(vec![rect])
34    }
35
36    /// True if no redraw is needed.
37    #[must_use]
38    pub const fn is_none(&self) -> bool {
39        matches!(self, Self::None)
40    }
41
42    /// Borrow the dirty rectangles when the region is rectangular.
43    #[must_use]
44    pub fn rects(&self) -> Option<&[Rectangle]> {
45        match self {
46            Self::Rects(rects) => Some(rects.as_slice()),
47            _ => None,
48        }
49    }
50
51    /// Add a rectangle to the dirty set.
52    pub fn add_rect(&mut self, rect: Rectangle) {
53        match self {
54            Self::None => *self = Self::Rects(vec![rect]),
55            Self::Full => {}
56            Self::Rects(rects) => rects.push(rect),
57        }
58    }
59}
60
61/// Renderer/backend capabilities relevant to the runtime.
62#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
63pub struct PlatformCapabilities {
64    /// The renderer can clip draw calls to a sub-rectangle.
65    pub supports_clip: bool,
66    /// The backend can flush only dirty rectangles efficiently.
67    pub supports_partial_flush: bool,
68    /// The backend accepts semantic input actions directly.
69    pub supports_semantic_input: bool,
70    /// The backend is happier redrawing the whole frame.
71    pub prefers_full_redraw: bool,
72}