-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.rs
More file actions
172 lines (148 loc) · 4.46 KB
/
Copy pathprotocol.rs
File metadata and controls
172 lines (148 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! Damage rect primitives for partial surface updates.
/// Axis-aligned rectangle (x, y, w, h) in pixels.
///
/// Used for damage regions in SURFACE_COMMIT events.
/// All coordinates are relative to the surface origin (top-left).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Rect {
pub x: u16,
pub y: u16,
pub w: u16,
pub h: u16,
}
impl Rect {
pub fn new(x: u16, y: u16, w: u16, h: u16) -> Self {
Self { x, y, w, h }
}
pub fn zero() -> Self {
Self {
x: 0,
y: 0,
w: 0,
h: 0,
}
}
pub fn is_zero(&self) -> bool {
self.w == 0 || self.h == 0
}
}
/// Clamp damage rect to surface bounds.
///
/// Zero-sized damage means full surface (Wayland convention).
/// Returns `None` if the clamped rect has zero area (completely outside bounds).
pub fn clamp_damage(damage: Rect, surface_w: u16, surface_h: u16) -> Option<Rect> {
if damage.is_zero() {
return Some(Rect::new(0, 0, surface_w, surface_h));
}
let x = damage.x.min(surface_w);
let y = damage.y.min(surface_h);
let x2 = (damage.x.saturating_add(damage.w)).min(surface_w);
let y2 = (damage.y.saturating_add(damage.h)).min(surface_h);
let w = x2.saturating_sub(x);
let h = y2.saturating_sub(y);
if w == 0 || h == 0 {
None
} else {
Some(Rect::new(x, y, w, h))
}
}
/// Merge two damage rects into a single bounding rect (union).
pub fn merge_damage(d1: Rect, d2: Rect) -> Rect {
if d1.is_zero() {
return d2;
}
if d2.is_zero() {
return d1;
}
let x1 = d1.x.min(d2.x);
let y1 = d1.y.min(d2.y);
let x2 = (d1.x.saturating_add(d1.w)).max(d2.x.saturating_add(d2.w));
let y2 = (d1.y.saturating_add(d1.h)).max(d2.y.saturating_add(d2.h));
Rect::new(x1, y1, x2.saturating_sub(x1), y2.saturating_sub(y1))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn damage_inside_bounds_passes_through() {
let damage = Rect::new(10, 20, 30, 40);
assert_eq!(
clamp_damage(damage, 100, 100),
Some(Rect::new(10, 20, 30, 40))
);
}
#[test]
fn damage_partially_outside_is_clamped() {
let damage = Rect::new(80, 80, 50, 50);
assert_eq!(
clamp_damage(damage, 100, 100),
Some(Rect::new(80, 80, 20, 20))
);
}
#[test]
fn damage_completely_outside_returns_none() {
let damage = Rect::new(200, 200, 10, 10);
assert_eq!(clamp_damage(damage, 100, 100), None);
}
#[test]
fn damage_zero_size_means_full_surface() {
let damage = Rect::new(0, 0, 0, 0);
assert_eq!(
clamp_damage(damage, 100, 100),
Some(Rect::new(0, 0, 100, 100))
);
}
#[test]
fn damage_exactly_at_surface_edge_kept() {
let damage = Rect::new(90, 90, 10, 10);
assert_eq!(
clamp_damage(damage, 100, 100),
Some(Rect::new(90, 90, 10, 10))
);
}
#[test]
fn damage_with_overflow_xy_clamps() {
let damage = Rect::new(65535, 65535, 100, 100);
assert_eq!(clamp_damage(damage, 100, 100), None);
}
#[test]
fn damage_zero_width_means_full_surface() {
let damage = Rect::new(0, 0, 0, 50);
assert_eq!(
clamp_damage(damage, 100, 100),
Some(Rect::new(0, 0, 100, 100))
);
}
#[test]
fn damage_zero_height_means_full_surface() {
let damage = Rect::new(0, 0, 50, 0);
assert_eq!(
clamp_damage(damage, 100, 100),
Some(Rect::new(0, 0, 100, 100))
);
}
#[test]
fn merge_disjoint_rects_returns_enclosing() {
let d1 = Rect::new(0, 0, 10, 10);
let d2 = Rect::new(20, 20, 10, 10);
assert_eq!(merge_damage(d1, d2), Rect::new(0, 0, 30, 30));
}
#[test]
fn merge_overlapping_rects_returns_enclosing() {
let d1 = Rect::new(0, 0, 20, 20);
let d2 = Rect::new(10, 10, 20, 20);
assert_eq!(merge_damage(d1, d2), Rect::new(0, 0, 30, 30));
}
#[test]
fn merge_same_rects_returns_same() {
let d1 = Rect::new(5, 5, 10, 10);
let d2 = Rect::new(5, 5, 10, 10);
assert_eq!(merge_damage(d1, d2), Rect::new(5, 5, 10, 10));
}
#[test]
fn merge_with_empty_returns_other() {
let d1 = Rect::new(0, 0, 0, 0);
let d2 = Rect::new(10, 10, 20, 20);
assert_eq!(merge_damage(d1, d2), Rect::new(10, 10, 20, 20));
}
}