From 39815cc596ba3199c6905419e9cca905e94f8043 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Fri, 17 Jun 2022 21:16:25 +1000 Subject: [PATCH 1/3] fix gam::Tile render RHS --- services/graphics-server/src/op.rs | 73 ++++++++++++++++-------------- services/modals/src/lib.rs | 14 +++--- services/modals/src/tests.rs | 4 +- 3 files changed, 48 insertions(+), 43 deletions(-) diff --git a/services/graphics-server/src/op.rs b/services/graphics-server/src/op.rs index 84b3cc14c..5b7e26f8b 100644 --- a/services/graphics-server/src/op.rs +++ b/services/graphics-server/src/op.rs @@ -1,6 +1,4 @@ -use crate::api::{ - Circle, DrawStyle, Line, Pixel, PixelColor, Point, Rectangle, RoundedRectangle, -}; +use crate::api::{Circle, DrawStyle, Line, Pixel, PixelColor, Point, Rectangle, RoundedRectangle}; /// LCD Frame buffer bounds pub const LCD_WORDS_PER_LINE: usize = 11; @@ -589,50 +587,51 @@ pub fn rounded_rectangle(fb: &mut LcdFB, rr: RoundedRectangle, clip: Option) { - use std::cmp::min; - // prepare the clip boundries: min (LCD, clip, tile) - let bound = tile.bound(); - let tl_x = bound.tl.x as usize; - let tl_y = bound.tl.y as usize; - log::info!("bound: {:?}, clip: {:?}", bound, clip); - let (mut br_x, mut br_y) = match clip { - Some(clip) => { - let x = min(bound.br.x, clip.br.x) as usize; - let y = min(bound.br.y, clip.br.y) as usize; - (x, y) - } - None => (bound.br.x as usize, bound.br.y as usize), + use std::cmp::{max, min}; + let fb_size: Point = Point::new((LCD_PX_PER_LINE - 1) as i16, (LCD_LINES - 1) as i16); + const FB_WORDS_PER_LINE: usize = LCD_WORDS_PER_LINE; + let clip = match clip { + // alter clip to work around an odd use (bug?) of the clip origin + // clip.tl appears to relate to the the usable screen area as origin + // clip.br appears to relate to the clip.tl as origin + Some(clip) => Rectangle::new(Point::new(0, 0), clip.br), + // default to the LcdFB dimensions + None => Rectangle::new(Point::new(0, 0), fb_size), }; - br_x = min(br_x, LCD_PX_PER_LINE - 1); - br_y = min(br_y, LCD_LINES - 1); + // determine the sub-region of the tile to transfer to the fb (in Tile coords) + let (tl_x, tl_y, br_x, br_y): (usize, usize, usize, usize) = ( + max(tile.bound().tl.x, clip.tl.x) as usize, + max(tile.bound().tl.y, clip.tl.y) as usize, + min(tile.bound().br.x, clip.br.x) as usize, + min(tile.bound().br.y, clip.br.y) as usize, + ); // prepare to cut and mask misaligned words let r_cut = tl_x % 32; let l_cut = 32 - r_cut; let mask = (1 << r_cut) - 1; - // prepare to clip a word spanning br_y + // prepare to clip a word spanning br_x let c_clip = br_x % 32; let c_mask = (1 << c_clip) - 1; - let first_word = tl_x / 32; - let last_word = LCD_WORDS_PER_LINE - 1; - log::info!("tl_y {}, br_y {}, clip: {:?}", tl_y, br_y, clip); + let first_wd = tl_x / 32; + let clip_wd = br_x / 32; + let last_wd = min(clip_wd, FB_WORDS_PER_LINE - 1); for tile_ln in tl_y..=br_y { - let fb_ln = tile_ln * LCD_WORDS_PER_LINE; + let fb_ln = tile_ln * FB_WORDS_PER_LINE; let tile_line = tile.get_line(Point::new(tl_x as i16, tile_ln as i16)); - // log::info!("tile_line: {:x?}", tile_line); - let mut fb_wd = first_word; + let mut fb_wd = first_wd; let mut tile_wd = 0; let mut fb_word = fb[fb_ln + fb_wd]; let mut left_part = fb_word & mask; let mut right_part; let mut tile_word: u32; - while (fb_wd <= last_word) & (tile_wd < tile_line.len()) { + while (fb_wd <= last_wd) & (tile_wd < tile_line.len()) { fb_word = fb[fb_ln + fb_wd]; tile_word = tile_line[tile_wd]; fb[fb_ln + fb_wd] = match r_cut { @@ -648,12 +647,20 @@ pub fn tile(fb: &mut LcdFB, tile: Tile, clip: Option) { fb_wd += 1; tile_wd += 1; } - // clip the last word in the line (ie word containing br_x) - left_part = fb[fb_ln + last_word] & c_mask; - right_part = fb_word & !c_mask; - fb[fb_ln + last_word] = left_part | right_part; + // complete the right side of the tile + if fb_wd > last_wd { + // encountered clip before end of tile + left_part = fb[fb_ln + fb_wd - 1] & c_mask; + right_part = fb_word & !c_mask; + fb[fb_ln + fb_wd - 1] = left_part | right_part; + } else { + // encountered end of tile before clip + left_part = left_part & c_mask; + right_part = fb[fb_ln + fb_wd] & !c_mask; + fb[fb_ln + fb_wd] = left_part | right_part; + } // set the dirty bit on the line - fb[fb_ln + (LCD_WORDS_PER_LINE - 1)] |= 0x1_0000; + fb[fb_ln + (FB_WORDS_PER_LINE - 1)] |= 0x1_0000; } } diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 8a59660c5..c46b2f161 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -195,16 +195,15 @@ impl Modals { } #[cfg(feature = "ditherpunk")] - const MODAL_WIDTH: u32 = 300; + const MODAL_WIDTH: u32 = 296; #[allow(dead_code)] #[cfg(feature = "ditherpunk")] - const MODAL_HEIGHT: u32 = 370; + const MODAL_HEIGHT: u32 = 300; /// this blocks until the image has been dismissed. #[cfg(feature = "ditherpunk")] pub fn show_image(&self, img: &Img) -> Result<(), xous::Error> { self.lock(); // resize and/or rotate - /* let (modal_width, modal_height) = (Modals::MODAL_WIDTH as f32, Modals::MODAL_HEIGHT as f32); let (w, h, _) = img.size(); let (img_width, img_height) = (w as f32, h as f32); @@ -229,23 +228,22 @@ impl Modals { bm = if rotate { bm.rotate90() } else { bm }; let (bm_width, bm_height) = bm.size(); let (bm_width, bm_height) = (bm_width as u32, bm_height as u32); - */ + let bm = Bitmap::from(img.clone()); // center image in modal - /* let center = Point::new( ((Modals::MODAL_WIDTH - bm_width) / 2).try_into().unwrap(), ((Modals::MODAL_HEIGHT - bm_height) / 2).try_into().unwrap(), - );*/ + ); let mut tiles: [Option; 6] = [None; 6]; for (t, tile) in bm.iter().enumerate() { if t >= tiles.len() { continue; } - let copy = tile.clone(); - //copy.translate(center); + let mut copy = tile.clone(); + copy.translate(center); tiles[t] = Some(copy); } diff --git a/services/modals/src/tests.rs b/services/modals/src/tests.rs index 4d30ca31a..3bb10811e 100644 --- a/services/modals/src/tests.rs +++ b/services/modals/src/tests.rs @@ -173,9 +173,9 @@ pub fn spawn_test() { // https://sequelaencollection.home.blog/2d-chaotic-attractors/ #[cfg(feature = "ditherpunk")] fn clifford() -> Img { - const SIZE: u32 = Modals::MODAL_WIDTH; + const SIZE: u32 = Modals::MODAL_WIDTH - 10; const CENTER: f32 = (SIZE / 2) as f32; - const SCALE: f32 = 70.0; + const SCALE: f32 = 60.0; let mut buf = vec![255u8; (SIZE * SIZE).try_into().unwrap()]; let (a, b, c, d) = (-2.0, -2.4, 1.1, -0.9); let (mut x, mut y): (f32, f32) = (0.0, 0.0); From 148e0fdd9972bf90c133a11a0e12eb9a85839907 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Fri, 17 Jun 2022 23:06:39 +1000 Subject: [PATCH 2/3] remove unnecessary Img clone --- services/gam/src/bitmap.rs | 8 ++++---- services/modals/src/lib.rs | 24 +++++++++--------------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/services/gam/src/bitmap.rs b/services/gam/src/bitmap.rs index 46923cc2f..960989744 100644 --- a/services/gam/src/bitmap.rs +++ b/services/gam/src/bitmap.rs @@ -235,8 +235,8 @@ impl From<[Option; 6]> for Bitmap { } } -impl From for Bitmap { - fn from(image: Img) -> Self { +impl From<&Img> for Bitmap { + fn from(image: &Img) -> Self { let (bm_width, bm_height, _) = image.size(); let bm_bottom = bm_height - 1; let bm_right = bm_width - 1; @@ -251,7 +251,7 @@ impl From for Bitmap { }; let mut mosaic: Vec = Vec::new(); - let pixels = Dither::new(BURKES.to_vec()).dither(image); + let pixels = Dither::new(BURKES.to_vec()).dither(&image); let mut px_index = 0; let bits_per_word: i16 = BITS_PER_WORD.try_into().unwrap(); for t in 0..tile_count { @@ -456,7 +456,7 @@ impl Dither { PixelColor::Light } } - pub fn dither(&mut self, image: Img) -> Vec { + pub fn dither(&mut self, image: &Img) -> Vec { let (width, height, length) = image.size(); self.provision(width.try_into().unwrap()); let mut pixels: Vec = Vec::with_capacity(length); diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index c46b2f161..51afbf40e 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -210,27 +210,21 @@ impl Modals { let portrait_scale = (modal_width / img_width).min(modal_height / img_height); let landscape_scale = (modal_width / img_height).min(modal_height / img_width); - let mut rotate = false; - let modal_img; - if portrait_scale >= 1.0 { - modal_img = img.clone(); - } else if landscape_scale >= 1.0 { - modal_img = img.clone(); - rotate = true; + let bm = if portrait_scale >= 1.0 { + Bitmap::from(img) + } else if landscape_scale >= 1.0 { + Bitmap::from(img).rotate90() } else if portrait_scale >= landscape_scale { - modal_img = Modals::resize_image(img.clone(), portrait_scale) + let resized = Modals::resize_image(img, portrait_scale); + Bitmap::from(&resized) } else { - rotate = true; - modal_img = Modals::resize_image(img.clone(), landscape_scale) + let resized = Modals::resize_image(img, landscape_scale); + Bitmap::from(&resized).rotate90() }; - let mut bm = Bitmap::from(modal_img); - bm = if rotate { bm.rotate90() } else { bm }; let (bm_width, bm_height) = bm.size(); let (bm_width, bm_height) = (bm_width as u32, bm_height as u32); - let bm = Bitmap::from(img.clone()); - // center image in modal let center = Point::new( ((Modals::MODAL_WIDTH - bm_width) / 2).try_into().unwrap(), @@ -260,7 +254,7 @@ impl Modals { #[allow(dead_code)] #[cfg(feature = "ditherpunk")] - fn resize_image(img: Img, scale: f32) -> Img { + fn resize_image(img: &Img, scale: f32) -> Img { let (w, h, _) = img.size(); let (img_width, img_height) = (w as f32, h as f32); let height: u32 = (scale * img_height).floor() as u32; From d75c33388815e018d7ea7bf22f861abda8c6d43b Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Fri, 17 Jun 2022 23:22:29 +1000 Subject: [PATCH 3/3] remove unnecessary Tile clone --- services/modals/src/lib.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 51afbf40e..cb0edf1bd 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -210,7 +210,7 @@ impl Modals { let portrait_scale = (modal_width / img_width).min(modal_height / img_height); let landscape_scale = (modal_width / img_height).min(modal_height / img_width); - let bm = if portrait_scale >= 1.0 { + let mut bm = if portrait_scale >= 1.0 { Bitmap::from(img) } else if landscape_scale >= 1.0 { Bitmap::from(img).rotate90() @@ -229,16 +229,15 @@ impl Modals { let center = Point::new( ((Modals::MODAL_WIDTH - bm_width) / 2).try_into().unwrap(), ((Modals::MODAL_HEIGHT - bm_height) / 2).try_into().unwrap(), - ); + ); + bm.translate(center); let mut tiles: [Option; 6] = [None; 6]; for (t, tile) in bm.iter().enumerate() { if t >= tiles.len() { continue; } - let mut copy = tile.clone(); - copy.translate(center); - tiles[t] = Some(copy); + tiles[t] = Some(*tile); } let spec = ManagedImage {