From 174ab733ac3284c5be19c9f89c4be3a85b1cc2ef Mon Sep 17 00:00:00 2001 From: cepetr Date: Wed, 20 Mar 2024 15:38:54 +0100 Subject: [PATCH] WIP - drawlib - new geometry --- core/embed/rust/src/trezorhal/dma2d_new.rs | 4 ++-- core/embed/rust/src/ui/shape/canvas/common.rs | 16 ++++++++++++++++ core/embed/rust/src/ui/shape/canvas/rgb565.rs | 4 +--- core/embed/rust/src/ui/shape/canvas/viewport.rs | 8 ++++---- core/embed/rust/src/ui/shape/toif.rs | 4 ++-- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/core/embed/rust/src/trezorhal/dma2d_new.rs b/core/embed/rust/src/trezorhal/dma2d_new.rs index 4230b9533..3c4e10f63 100644 --- a/core/embed/rust/src/trezorhal/dma2d_new.rs +++ b/core/embed/rust/src/trezorhal/dma2d_new.rs @@ -30,7 +30,7 @@ impl Default for Dma2d { impl Dma2d { pub fn new_fill(r: Rect, clip: Rect, color: Color, alpha: u8) -> Option { - let r = r.intersect(clip); + let r = r.clamp(clip); if !r.is_empty() { Some( Self::default() @@ -59,7 +59,7 @@ impl Dma2d { } // Clip with the canvas viewport - let mut r = r_dst.intersect(clip); + let mut r = r_dst.clamp(clip); // Clip with the bitmap top-left if r.x0 > r_dst.x0 { diff --git a/core/embed/rust/src/ui/shape/canvas/common.rs b/core/embed/rust/src/ui/shape/canvas/common.rs index 405eedaea..2ff72129c 100644 --- a/core/embed/rust/src/ui/shape/canvas/common.rs +++ b/core/embed/rust/src/ui/shape/canvas/common.rs @@ -826,6 +826,22 @@ fn fill_octant( } } +impl Rect { + /// Normalizes the rectangle coordinates. + /// + /// Returns a new `Rect` with potentially swapped left/right, + /// top/bottom coordinates, ensuring that `x0`, `y0` represents + /// the top-left corner and `x1`, `y1` represents the bottom-right corner. + pub fn normalize(&self) -> Self { + Rect { + x0: core::cmp::min(self.x0, self.x1), + y0: core::cmp::min(self.y0, self.y1), + x1: core::cmp::max(self.x0, self.x1), + y1: core::cmp::max(self.y0, self.y1), + } + } +} + impl Point { fn onleft(self) -> Self { Self { diff --git a/core/embed/rust/src/ui/shape/canvas/rgb565.rs b/core/embed/rust/src/ui/shape/canvas/rgb565.rs index 54e8e235f..49c2fa4c6 100644 --- a/core/embed/rust/src/ui/shape/canvas/rgb565.rs +++ b/core/embed/rust/src/ui/shape/canvas/rgb565.rs @@ -94,9 +94,7 @@ impl<'a> Canvas for Rgb565Canvas<'a> { #[cfg(feature = "ui_blurring")] fn blur_rect(&mut self, r: Rect, radius: usize, cache: &DrawingCache) { - let clip = r - .translate(self.viewport.origin) - .intersect(self.viewport.clip); + let clip = r.translate(self.viewport.origin).clamp(self.viewport.clip); let ofs = radius as i16; diff --git a/core/embed/rust/src/ui/shape/canvas/viewport.rs b/core/embed/rust/src/ui/shape/canvas/viewport.rs index 99db7ca17..aca7f0b65 100644 --- a/core/embed/rust/src/ui/shape/canvas/viewport.rs +++ b/core/embed/rust/src/ui/shape/canvas/viewport.rs @@ -56,7 +56,7 @@ impl Viewport { /// Checks if the viewport intersects with the specified rectangle /// given in relative coordinates. pub fn contains(&self, r: Rect) -> bool { - r.translate(self.origin).has_intersection(self.clip) + !r.translate(self.origin).clamp(self.clip).is_empty() } pub fn translate(self, offset: Offset) -> Self { @@ -77,7 +77,7 @@ impl Viewport { /// remains unchanged. pub fn absolute_clip(self, r: Rect) -> Self { Self { - clip: r.intersect(self.clip), + clip: r.clamp(self.clip), ..self } } @@ -87,7 +87,7 @@ impl Viewport { /// remains unchanged. pub fn relative_clip(self, r: Rect) -> Self { Self { - clip: r.translate(self.origin).intersect(self.clip), + clip: r.translate(self.origin).clamp(self.clip), ..self } } @@ -96,7 +96,7 @@ impl Viewport { /// given in relative coordinates. The origin of the new viewport /// is set to the top-left corner of the rectangle. pub fn relative_window(&self, r: Rect) -> Self { - let clip = r.translate(self.origin).intersect(self.clip); + let clip = r.translate(self.origin).clamp(self.clip); let origin = self.origin + (clip.top_left() - self.clip.top_left()); Self { clip, origin } } diff --git a/core/embed/rust/src/ui/shape/toif.rs b/core/embed/rust/src/ui/shape/toif.rs index 7830e4ece..f01dde5cf 100644 --- a/core/embed/rust/src/ui/shape/toif.rs +++ b/core/embed/rust/src/ui/shape/toif.rs @@ -57,7 +57,7 @@ impl<'a> ToifImage<'a> { let viewport = canvas.viewport(); let mut clip = self .bounds(cache) - .intersect(viewport.clip.translate(-viewport.origin)) + .clamp(viewport.clip.translate(-viewport.origin)) .translate((-bounds.top_left()).into()); let buff = &mut unwrap!(cache.image_buff(), "No image buffer"); @@ -105,7 +105,7 @@ impl<'a> ToifImage<'a> { let viewport = canvas.viewport(); let mut clip = self .bounds(cache) - .intersect(viewport.clip.translate(-viewport.origin)) + .clamp(viewport.clip.translate(-viewport.origin)) .translate((-bounds.top_left()).into()); let buff = &mut unwrap!(cache.image_buff(), "No image buffer");