diff --git a/libs/ux-api/src/minigfx/op.rs b/libs/ux-api/src/minigfx/op.rs index e05c38468..0e3a618a8 100644 --- a/libs/ux-api/src/minigfx/op.rs +++ b/libs/ux-api/src/minigfx/op.rs @@ -1,4 +1,5 @@ use super::*; +use crate::minigfx::FrameBuffer; /// LCD Frame buffer bounds pub const LCD_WORDS_PER_LINE: usize = crate::platform::FB_WIDTH_WORDS; @@ -9,51 +10,10 @@ pub const LCD_FRAME_BUF_SIZE: usize = LCD_WORDS_PER_LINE * LCD_LINES; pub const WIDTH: isize = crate::platform::WIDTH as isize; pub const HEIGHT: isize = crate::platform::LINES as isize; -/// For passing frame buffer references -pub type LcdFB = [u32]; - /// Set the expected rectangle length for the busy animation pub const BUSY_ANIMATION_RECT_WIDTH: isize = 32; // golden ratio off of a height of 20 -fn put_pixel(fb: &mut LcdFB, x: isize, y: isize, color: ColorNative) { - let mut clip_y: usize = y as usize; - if clip_y >= LCD_LINES { - clip_y = LCD_LINES - 1; - } - - let mut clip_x: usize = x as usize; - if clip_x >= LCD_PX_PER_LINE { - clip_x = LCD_PX_PER_LINE - 1; - } - - let pc = PixelColor::from(color.0); - - if pc == PixelColor::Light { - fb[(clip_x + clip_y * LCD_WORDS_PER_LINE * 32) / 32] |= 1 << (clip_x % 32) - } else { - fb[(clip_x + clip_y * LCD_WORDS_PER_LINE * 32) / 32] &= !(1 << (clip_x % 32)) - } - // set the dirty bit on the line that contains the pixel - fb[clip_y * LCD_WORDS_PER_LINE + (LCD_WORDS_PER_LINE - 1)] |= 0x1_0000; -} - -fn xor_pixel(fb: &mut LcdFB, x: isize, y: isize) { - let mut clip_y: usize = y as usize; - if clip_y >= LCD_LINES { - clip_y = LCD_LINES - 1; - } - - let mut clip_x: usize = x as usize; - if clip_x >= LCD_PX_PER_LINE { - clip_x = LCD_PX_PER_LINE - 1; - } - - fb[(clip_x + clip_y * LCD_WORDS_PER_LINE * 32) / 32] ^= 1 << (clip_x % 32); - // set the dirty bit on the line that contains the pixel - fb[clip_y * LCD_WORDS_PER_LINE + (LCD_WORDS_PER_LINE - 1)] |= 0x1_0000; -} - -pub fn line(fb: &mut LcdFB, l: Line, clip: Option, xor: bool) { +pub fn line(fb: &mut T, l: Line, clip: Option, xor: bool) { let color: ColorNative; if l.style.stroke_color.is_some() { color = ColorNative::from(l.style.stroke_color.unwrap()); @@ -75,9 +35,9 @@ pub fn line(fb: &mut LcdFB, l: Line, clip: Option, xor: bool) { if x0 >= 0 && y0 >= 0 && x0 < (WIDTH as _) && y0 < (HEIGHT as _) { if clip.is_none() || (clip.unwrap().intersects_point(Point::new(x0, y0))) { if !xor { - put_pixel(fb, x0 as _, y0 as _, color); + fb.put_pixel(Point::new(x0, y0), color); } else { - xor_pixel(fb, x0 as _, y0 as _); + fb.xor_pixel(Point::new(x0, y0)); } } } @@ -171,7 +131,7 @@ impl Iterator for CircleIterator { } } -pub fn circle(fb: &mut LcdFB, circle: Circle, clip: Option) { +pub fn circle(fb: &mut T, circle: Circle, clip: Option) { let radius = circle.radius.abs() as u16; let c = CircleIterator { center: circle.center, @@ -182,7 +142,7 @@ pub fn circle(fb: &mut LcdFB, circle: Circle, clip: Option) { }; for pixel in c { - put_pixel(fb, pixel.0.x, pixel.0.y, pixel.1); + fb.put_pixel(pixel.0, pixel.1); } } @@ -256,13 +216,13 @@ impl Iterator for RectangleIterator { } } -pub fn rectangle(fb: &mut LcdFB, rect: Rectangle, clip: Option, xor: bool) { +pub fn rectangle(fb: &mut T, rect: Rectangle, clip: Option, xor: bool) { let r = RectangleIterator { top_left: rect.tl, bottom_right: rect.br, style: rect.style, p: rect.tl, clip }; for pixel in r { if !xor { - put_pixel(fb, pixel.0.x, pixel.0.y, pixel.1); + fb.put_pixel(pixel.0, pixel.1); } else { if rect.width() <= BUSY_ANIMATION_RECT_WIDTH as u32 { // only allow xor of a rectangle if it is within the expected width for the busy animation @@ -270,14 +230,14 @@ pub fn rectangle(fb: &mut LcdFB, rect: Rectangle, clip: Option, xor: // text on an otherwise untrusted dialog box (I suspect it's not impossible, but introduces // a likelihood of drawing artifacts especially across translations and font size selections). if pixel.0.y % 25 != 5 { - xor_pixel(fb, pixel.0.x, pixel.0.y); + fb.xor_pixel(pixel.0); } else { // don't allow XOR on some pixels to avoid this being used as a primitive to synthesize // secure boxes } } else { log::warn!("invalid xor rect width"); - put_pixel(fb, pixel.0.x, pixel.0.y, pixel.1); + fb.put_pixel(pixel.0, pixel.1); } } } @@ -405,7 +365,7 @@ impl Iterator for QuadrantIterator { } } -pub fn quadrant(fb: &mut LcdFB, circle: Circle, quad: Quadrant, clip: Option) { +pub fn quadrant(fb: &mut T, circle: Circle, quad: Quadrant, clip: Option) { let starting_pixel = match quad { Quadrant::TopLeft => Point::new(-(circle.radius as isize), -(circle.radius as isize)), Quadrant::TopRight => Point::new(0, -(circle.radius as isize)), @@ -422,7 +382,7 @@ pub fn quadrant(fb: &mut LcdFB, circle: Circle, quad: Quadrant, clip: Option) { +pub fn rounded_rectangle(fb: &mut T, rr: RoundedRectangle, clip: Option) { // compute the four quadrants // call the rr iterator on the rectangle // then call it on one each of the four circle quadrants @@ -534,7 +494,7 @@ pub fn rounded_rectangle(fb: &mut LcdFB, rr: RoundedRectangle, clip: Option) { +pub fn tile(fb_api: &mut T, tile: Tile, clip: Option) { + let fb = unsafe { fb_api.raw_mut() }; use std::cmp::{max, min}; let fb_size: Point = Point::new((LCD_PX_PER_LINE - 1) as isize, (LCD_LINES - 1) as isize); const FB_WORDS_PER_LINE: usize = LCD_WORDS_PER_LINE;