Skip to main content

ass_renderer/utils/
regions.rs

1//! Dirty region tracking for incremental rendering
2
3/// Dirty region for incremental updates
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub struct DirtyRegion {
6    /// Left coordinate
7    pub x: u32,
8    /// Top coordinate
9    pub y: u32,
10    /// Width
11    pub width: u32,
12    /// Height
13    pub height: u32,
14}
15
16impl DirtyRegion {
17    /// Create a new dirty region
18    pub fn new(x: u32, y: u32, width: u32, height: u32) -> Self {
19        Self {
20            x,
21            y,
22            width,
23            height,
24        }
25    }
26
27    /// Create a full screen dirty region
28    pub fn full_screen() -> Self {
29        Self {
30            x: 0,
31            y: 0,
32            width: u32::MAX,
33            height: u32::MAX,
34        }
35    }
36
37    /// Check if this region intersects with bounds
38    pub fn intersects(&self, bounds: (u32, u32, u32, u32)) -> bool {
39        let (bx1, by1, bx2, by2) = bounds;
40        let rx1 = self.x;
41        let ry1 = self.y;
42        let rx2 = self.x.saturating_add(self.width);
43        let ry2 = self.y.saturating_add(self.height);
44
45        !(rx2 < bx1 || rx1 > bx2 || ry2 < by1 || ry1 > by2)
46    }
47
48    /// Merge with another region
49    pub fn merge(&self, other: &Self) -> Self {
50        let x1 = self.x.min(other.x);
51        let y1 = self.y.min(other.y);
52        let x2 = (self.x + self.width).max(other.x + other.width);
53        let y2 = (self.y + self.height).max(other.y + other.height);
54
55        Self {
56            x: x1,
57            y: y1,
58            width: x2 - x1,
59            height: y2 - y1,
60        }
61    }
62
63    /// Check if region contains point
64    pub fn contains_point(&self, x: u32, y: u32) -> bool {
65        x >= self.x && x < self.x + self.width && y >= self.y && y < self.y + self.height
66    }
67
68    /// Get area of region
69    pub fn area(&self) -> u64 {
70        self.width as u64 * self.height as u64
71    }
72}