use crate::widgets::CanvasRect;
impl CanvasRect {
pub fn bounding_union(&self, other: &CanvasRect) -> CanvasRect {
if self.is_empty() {
return *other;
}
if other.is_empty() {
return *self;
}
let min_x = self.x.min(other.x);
let min_y = self.y.min(other.y);
let max_x = (self.x + self.width).max(other.x + other.width);
let max_y = (self.y + self.height).max(other.y + other.height);
CanvasRect {
x: min_x,
y: min_y,
width: max_x - min_x,
height: max_y - min_y,
}
}
pub fn is_empty(&self) -> bool {
self.width <= 0 || self.height <= 0
}
pub fn area(&self) -> u64 {
if self.is_empty() {
0
} else {
(self.width as u64) * (self.height as u64)
}
}
pub fn contains_rect(&self, other: &CanvasRect) -> bool {
if other.is_empty() {
return true;
}
if self.is_empty() {
return false;
}
self.x <= other.x
&& self.y <= other.y
&& (self.x + self.width) >= (other.x + other.width)
&& (self.y + self.height) >= (other.y + other.height)
}
pub fn intersects(&self, other: &CanvasRect) -> bool {
if self.is_empty() || other.is_empty() {
return false;
}
self.x < (other.x + other.width)
&& (self.x + self.width) > other.x
&& self.y < (other.y + other.height)
&& (self.y + self.height) > other.y
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CanvasDamage {
Full,
Rect(CanvasRect),
}
impl CanvasDamage {
pub fn is_full(&self) -> bool {
matches!(self, CanvasDamage::Full)
}
pub fn is_empty(&self) -> bool {
match self {
CanvasDamage::Full => false,
CanvasDamage::Rect(r) => r.is_empty(),
}
}
pub fn rect(&self) -> Option<CanvasRect> {
match self {
CanvasDamage::Full => None,
CanvasDamage::Rect(r) => Some(*r),
}
}
pub fn union(&self, other: &CanvasDamage) -> CanvasDamage {
match (self, other) {
(CanvasDamage::Full, _) | (_, CanvasDamage::Full) => CanvasDamage::Full,
(CanvasDamage::Rect(r1), CanvasDamage::Rect(r2)) => {
CanvasDamage::Rect(r1.bounding_union(r2))
}
}
}
pub fn union_rect(&self, rect: CanvasRect) -> CanvasDamage {
self.union(&CanvasDamage::Rect(rect))
}
pub fn area(&self) -> Option<u64> {
match self {
CanvasDamage::Full => None,
CanvasDamage::Rect(r) => Some(r.area()),
}
}
pub fn should_promote_to_full(
&self,
canvas_width: u32,
canvas_height: u32,
threshold_ratio: f64,
) -> bool {
match self {
CanvasDamage::Full => true,
CanvasDamage::Rect(r) => {
if canvas_width == 0 || canvas_height == 0 {
return true;
}
let total_area = (canvas_width as f64) * (canvas_height as f64);
let damage_area = r.area() as f64;
(damage_area / total_area) >= threshold_ratio
}
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct CanvasDamageAccumulator {
pending_damage: Option<CanvasDamage>,
events_coalesced: u64,
refreshes_emitted: u64,
}
impl CanvasDamageAccumulator {
pub fn new() -> Self {
Self::default()
}
pub fn record_damage(&mut self, damage: CanvasDamage) {
self.events_coalesced = self.events_coalesced.saturating_add(1);
self.pending_damage = match self.pending_damage {
None => Some(damage),
Some(prev) => Some(prev.union(&damage)),
};
}
pub fn record_full_damage(&mut self) {
self.record_damage(CanvasDamage::Full);
}
pub fn record_rect_damage(&mut self, rect: CanvasRect) {
self.record_damage(CanvasDamage::Rect(rect));
}
pub fn is_dirty(&self) -> bool {
self.pending_damage.is_some()
}
pub fn pending_damage(&self) -> Option<CanvasDamage> {
self.pending_damage
}
pub fn promote_if_exceeds(
&mut self,
canvas_width: u32,
canvas_height: u32,
threshold_ratio: f64,
) {
if let Some(damage) = self.pending_damage {
if damage.should_promote_to_full(canvas_width, canvas_height, threshold_ratio) {
self.pending_damage = Some(CanvasDamage::Full);
}
}
}
pub fn take_damage(&mut self) -> Option<CanvasDamage> {
let damage = self.pending_damage.take();
if damage.is_some() {
self.refreshes_emitted = self.refreshes_emitted.saturating_add(1);
}
damage
}
pub fn events_coalesced(&self) -> u64 {
self.events_coalesced
}
pub fn refreshes_emitted(&self) -> u64 {
self.refreshes_emitted
}
pub fn reset(&mut self) {
self.pending_damage = None;
self.events_coalesced = 0;
self.refreshes_emitted = 0;
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CanvasStrokeCoalescingPolicy {
pub promotion_threshold: f64,
pub target_refresh_rate_hz: u32,
}
impl Default for CanvasStrokeCoalescingPolicy {
fn default() -> Self {
Self {
promotion_threshold: 0.60,
target_refresh_rate_hz: 60,
}
}
}
impl CanvasStrokeCoalescingPolicy {
pub fn new(promotion_threshold: f64, target_refresh_rate_hz: u32) -> Self {
Self {
promotion_threshold: promotion_threshold.clamp(0.01, 1.0),
target_refresh_rate_hz: target_refresh_rate_hz.max(1),
}
}
pub fn frame_interval(&self) -> std::time::Duration {
std::time::Duration::from_nanos((1_000_000_000u64) / (self.target_refresh_rate_hz as u64))
}
}
pub struct CanvasBufferCost;
impl CanvasBufferCost {
pub const fn buffer_bytes(width: u32, height: u32) -> usize {
(width as usize) * (height as usize) * 4
}
pub fn bandwidth_bytes_per_sec(width: u32, height: u32, rate_hz: u32) -> u64 {
(Self::buffer_bytes(width, height) as u64) * (rate_hz as u64)
}
pub fn bandwidth_mb_per_sec(width: u32, height: u32, rate_hz: u32) -> f64 {
(Self::bandwidth_bytes_per_sec(width, height, rate_hz) as f64) / 1_000_000.0
}
pub fn bandwidth_mib_per_sec(width: u32, height: u32, rate_hz: u32) -> f64 {
(Self::bandwidth_bytes_per_sec(width, height, rate_hz) as f64) / (1024.0 * 1024.0)
}
pub fn estimated_copy_duration_ms(width: u32, height: u32, throughput_gib_per_sec: f64) -> f64 {
if throughput_gib_per_sec <= 0.0 {
return f64::INFINITY;
}
let bytes = Self::buffer_bytes(width, height) as f64;
let bytes_per_sec = throughput_gib_per_sec * 1024.0 * 1024.0 * 1024.0;
(bytes / bytes_per_sec) * 1000.0
}
pub fn damage_buffer_bytes(rect: &CanvasRect) -> usize {
(rect.area() as usize) * 4
}
pub fn damage_area_ratio(canvas_width: u32, canvas_height: u32, rect: &CanvasRect) -> f64 {
if canvas_width == 0 || canvas_height == 0 {
return 1.0;
}
let total = (canvas_width as f64) * (canvas_height as f64);
(rect.area() as f64 / total).clamp(0.0, 1.0)
}
pub fn copy_reduction_factor(canvas_width: u32, canvas_height: u32, rect: &CanvasRect) -> f64 {
let ratio = Self::damage_area_ratio(canvas_width, canvas_height, rect);
if ratio <= 0.0 {
f64::INFINITY
} else {
1.0 / ratio
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_canvas_rect_bounding_union() {
let r1 = CanvasRect {
x: 10,
y: 10,
width: 20,
height: 20,
};
let r2 = CanvasRect {
x: 20,
y: 20,
width: 30,
height: 30,
};
let u = r1.bounding_union(&r2);
assert_eq!(
u,
CanvasRect {
x: 10,
y: 10,
width: 40,
height: 40,
}
);
assert_eq!(u.area(), 1600);
let empty = CanvasRect {
x: 0,
y: 0,
width: 0,
height: 0,
};
assert_eq!(r1.bounding_union(&empty), r1);
assert_eq!(empty.bounding_union(&r2), r2);
}
#[test]
fn test_canvas_rect_contains_and_intersects() {
let outer = CanvasRect {
x: 0,
y: 0,
width: 100,
height: 100,
};
let inner = CanvasRect {
x: 10,
y: 10,
width: 20,
height: 20,
};
let disjoint = CanvasRect {
x: 200,
y: 200,
width: 50,
height: 50,
};
let overlapping = CanvasRect {
x: 90,
y: 90,
width: 50,
height: 50,
};
assert!(outer.contains_rect(&inner));
assert!(!inner.contains_rect(&outer));
assert!(!outer.contains_rect(&disjoint));
assert!(outer.intersects(&overlapping));
assert!(overlapping.intersects(&outer));
assert!(!outer.intersects(&disjoint));
}
#[test]
fn test_canvas_damage_union() {
let d_full = CanvasDamage::Full;
let d1 = CanvasDamage::Rect(CanvasRect {
x: 10,
y: 10,
width: 20,
height: 20,
});
let d2 = CanvasDamage::Rect(CanvasRect {
x: 20,
y: 20,
width: 30,
height: 30,
});
assert_eq!(d_full.union(&d1), CanvasDamage::Full);
assert_eq!(d1.union(&d_full), CanvasDamage::Full);
assert_eq!(d_full.union(&d_full), CanvasDamage::Full);
let merged = d1.union(&d2);
assert_eq!(
merged,
CanvasDamage::Rect(CanvasRect {
x: 10,
y: 10,
width: 40,
height: 40,
})
);
assert_eq!(merged.area(), Some(1600));
assert!(!merged.is_full());
assert!(!merged.is_empty());
}
#[test]
fn test_canvas_damage_promotion_threshold() {
let small_rect = CanvasDamage::Rect(CanvasRect {
x: 100,
y: 100,
width: 200,
height: 200,
}); assert!(!small_rect.should_promote_to_full(1000, 1000, 0.60));
let large_rect = CanvasDamage::Rect(CanvasRect {
x: 0,
y: 0,
width: 800,
height: 800,
}); assert!(large_rect.should_promote_to_full(1000, 1000, 0.60));
assert!(CanvasDamage::Full.should_promote_to_full(1000, 1000, 0.60));
}
#[test]
fn test_accumulator_lifecycle_and_coalescing() {
let mut accum = CanvasDamageAccumulator::new();
assert!(!accum.is_dirty());
assert_eq!(accum.events_coalesced(), 0);
assert_eq!(accum.refreshes_emitted(), 0);
for i in 0..5 {
accum.record_rect_damage(CanvasRect {
x: i * 10,
y: i * 10,
width: 20,
height: 20,
});
}
assert!(accum.is_dirty());
assert_eq!(accum.events_coalesced(), 5);
assert_eq!(accum.refreshes_emitted(), 0);
let coalesced = accum.take_damage().expect("damage should be present");
assert_eq!(
coalesced,
CanvasDamage::Rect(CanvasRect {
x: 0,
y: 0,
width: 60,
height: 60,
})
);
assert_eq!(accum.refreshes_emitted(), 1);
assert!(!accum.is_dirty());
assert_eq!(accum.take_damage(), None);
}
#[test]
fn test_accumulator_full_damage_subsumption_and_promotion() {
let mut accum = CanvasDamageAccumulator::new();
accum.record_rect_damage(CanvasRect {
x: 10,
y: 10,
width: 20,
height: 20,
});
accum.record_full_damage();
accum.record_rect_damage(CanvasRect {
x: 30,
y: 30,
width: 20,
height: 20,
});
assert_eq!(accum.events_coalesced(), 3);
assert_eq!(accum.take_damage(), Some(CanvasDamage::Full));
accum.record_rect_damage(CanvasRect {
x: 0,
y: 0,
width: 800,
height: 800,
});
accum.promote_if_exceeds(1000, 1000, 0.60);
assert_eq!(accum.take_damage(), Some(CanvasDamage::Full));
}
#[test]
fn test_canvas_buffer_cost_measurement_12mp() {
let bytes = CanvasBufferCost::buffer_bytes(4000, 3000);
assert_eq!(bytes, 48_000_000);
let bw_60hz = CanvasBufferCost::bandwidth_bytes_per_sec(4000, 3000, 60);
assert_eq!(bw_60hz, 2_880_000_000); assert!((CanvasBufferCost::bandwidth_mb_per_sec(4000, 3000, 60) - 2880.0).abs() < 1e-4);
let bw_120hz = CanvasBufferCost::bandwidth_bytes_per_sec(4000, 3000, 120);
assert_eq!(bw_120hz, 5_760_000_000);
let bw_1000hz = CanvasBufferCost::bandwidth_bytes_per_sec(4000, 3000, 1000);
assert_eq!(bw_1000hz, 48_000_000_000);
let latency_ms = CanvasBufferCost::estimated_copy_duration_ms(4000, 3000, 25.0);
assert!(latency_ms > 1.5 && latency_ms < 2.0);
let stroke_damage = CanvasRect {
x: 500,
y: 500,
width: 40,
height: 40,
};
let stroke_bytes = CanvasBufferCost::damage_buffer_bytes(&stroke_damage);
assert_eq!(stroke_bytes, 6_400);
let factor = CanvasBufferCost::copy_reduction_factor(4000, 3000, &stroke_damage);
assert_eq!(factor, 7500.0); }
#[test]
fn test_canvas_buffer_cost_resolutions_scaling() {
assert_eq!(
CanvasBufferCost::buffer_bytes(800, 600),
1_920_000 );
assert_eq!(
CanvasBufferCost::buffer_bytes(1920, 1080),
8_294_400 );
assert_eq!(
CanvasBufferCost::buffer_bytes(3840, 2160),
33_177_600 );
assert_eq!(
CanvasBufferCost::buffer_bytes(6000, 4000),
96_000_000 );
}
}