diff --git a/services/gam/src/bitmap.rs b/services/gam/src/bitmap.rs index 7046bc64b..f8b665d5b 100644 --- a/services/gam/src/bitmap.rs +++ b/services/gam/src/bitmap.rs @@ -1,9 +1,8 @@ +use core::cmp::{max, min}; +use dither::prelude::{Dither, Img, RGB}; use graphics_server::api::shapes; use graphics_server::api::{Point, Rectangle, Tile}; use graphics_server::PixelColor; - -use core::cmp::min; -use dither::prelude::{Dither, Img, RGB}; use std::convert::TryInto; use std::ops::Deref; @@ -78,6 +77,13 @@ impl Bitmap { (tile_size, tile_width_words as i16) } + #[allow(dead_code)] + fn area(&self) -> u32 { + let (x, y) = self.size(); + (x * y) as u32 + } + + #[allow(dead_code)] fn size(&self) -> (usize, usize) { (self.bound.br.x as usize, self.bound.br.y as usize) } @@ -96,6 +102,28 @@ impl Bitmap { } } + fn hull(mosaic: &Vec) -> Rectangle { + let mut hull_tl = Point::new(i16::MAX, i16::MAX); + let mut hull_br = Point::new(i16::MIN, i16::MIN); + let mut tile_area = 0; + for (_i, tile) in mosaic.iter().enumerate() { + let tile_bound = tile.bound(); + hull_tl.x = min(hull_tl.x, tile_bound.tl.x); + hull_tl.y = min(hull_tl.y, tile_bound.tl.y); + hull_br.x = max(hull_br.x, tile_bound.br.x); + hull_br.y = max(hull_br.y, tile_bound.br.y); + tile_area += + (1 + tile_bound.br.x - tile_bound.tl.x) * (1 + tile_bound.br.y - tile_bound.tl.y); + } + let hull_area = (1 + hull_br.x - hull_tl.x) * (1 + hull_br.y - hull_tl.y); + if tile_area < hull_area { + log::warn!("Bitmap Tile gaps"); + } else if tile_area > hull_area { + log::warn!("Bitmap Tile overlap"); + } + Rectangle::new(hull_tl, hull_br) + } + pub fn get_tile(&self, point: Point) -> Tile { let tile = self.get_tile_index(point); self.mosaic.as_slice()[tile] @@ -110,6 +138,7 @@ impl Bitmap { self.get_tile(point).get_line(point) } + #[allow(dead_code)] fn get_word(&self, point: Point) -> shapes::Word { self.get_tile(point).get_word(point) } @@ -137,6 +166,28 @@ impl Deref for Bitmap { } } +impl From<[Option; 6]> for Bitmap { + fn from(tiles: [Option; 6]) -> Self { + let mut mosaic: Vec = Vec::new(); + let mut tile_size = Point::new(0, 0); + for t in 0..tiles.len() { + if tiles[t].is_some() { + let tile = tiles[t].unwrap(); + mosaic.push(tile); + if tile_size.x == 0 { + tile_size = tile.size(); + } + } + } + + Self { + bound: Self::hull(&mosaic), + tile_size: tile_size, + mosaic: mosaic, + } + } +} + impl From>> for Bitmap { fn from(img: Img>) -> Self { let img = img.convert_with(|rgb| rgb.convert_with(>::from)); diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index 1e31771f0..a9e0f16c9 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -14,7 +14,8 @@ pub mod bitmap; pub use bitmap::*; use graphics_server::api::{TextOp, TextView}; -use graphics_server::api::{Point, Gid, Line, Rectangle, Circle, RoundedRectangle, TokenClaim}; +use graphics_server::api::{Gid, Line, Circle, RoundedRectangle, TokenClaim}; +pub use graphics_server::api::{Point, Tile, Rectangle}; pub use graphics_server::api::GlyphStyle; pub use graphics_server::api::PixelColor; use api::Opcode; // if you prefer to map the api into your local namespace @@ -190,7 +191,7 @@ impl Gam { let buf = Buffer::into_buf(go).or(Err(xous::Error::InternalError))?; buf.lend(self.conn, Opcode::RenderObject.to_u32().unwrap()).map(|_|()) } - pub fn draw_bitmap(&self, gid: Gid, bm: Bitmap) -> Result<(), xous::Error> { + pub fn draw_bitmap(&self, gid: Gid, bm: &Bitmap) -> Result<(), xous::Error> { let mut list = GamObjectList::new(gid); for (_i, tile) in bm.iter().enumerate(){ list.push(GamObjectType::Tile(*tile)).unwrap(); diff --git a/services/gam/src/modal.rs b/services/gam/src/modal.rs index b7a3027cf..60a15fcc0 100644 --- a/services/gam/src/modal.rs +++ b/services/gam/src/modal.rs @@ -8,6 +8,8 @@ mod checkboxes; pub use checkboxes::*; mod notification; pub use notification::*; +mod image; +pub use image::*; mod slider; pub use slider::*; mod progressbar; @@ -37,6 +39,7 @@ pub enum ActionType { CheckBoxes, Slider, Notification, + Image, ConsoleInput } diff --git a/services/graphics-server/src/api/shapes.rs b/services/graphics-server/src/api/shapes.rs index b6a120faa..7eda69a35 100644 --- a/services/graphics-server/src/api/shapes.rs +++ b/services/graphics-server/src/api/shapes.rs @@ -388,6 +388,11 @@ impl Tile { self.bound } + pub fn area(&self) -> u32 { + let size = self.size(); + (size.x * size.y) as u32 + } + pub fn size(&self) -> Point { let pos = self.bound; Point::new(pos.br.x - pos.tl.x, pos.br.y - pos.tl.y) diff --git a/services/graphics-server/src/op.rs b/services/graphics-server/src/op.rs index 8aa789456..1aae61433 100644 --- a/services/graphics-server/src/op.rs +++ b/services/graphics-server/src/op.rs @@ -626,8 +626,7 @@ pub fn tile(fb: &mut LcdFB, tile: Tile, clip: Option) { 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 = 0; - let mut word = fb_word; + let mut tile_word: u32; while (fb_wd <= last_word) & (tile_wd < tile_line.len()) { fb_word = fb[fb_ln + fb_wd]; tile_word = tile_line[tile_wd]; diff --git a/services/modals/Cargo.toml b/services/modals/Cargo.toml index 1e2e94d13..93d7a7567 100644 --- a/services/modals/Cargo.toml +++ b/services/modals/Cargo.toml @@ -22,6 +22,9 @@ tts-frontend = {path="../tts"} locales = {path = "../../locales"} bit_field = "0.9.0" +#dither = "1.3.10" pending https://gitlab.com/efronlicht/dither/-/merge_requests/28 +dither = { git = "https://gitlab.com/nworbnhoj/dither" } + [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} diff --git a/services/modals/src/api.rs b/services/modals/src/api.rs index 4289aead3..91b8e1492 100644 --- a/services/modals/src/api.rs +++ b/services/modals/src/api.rs @@ -1,4 +1,6 @@ use gam::modal::*; +use gam::Tile; + pub(crate) const SERVER_NAME_MODALS: &str = "_Modal Dialog Server_"; @@ -40,6 +42,13 @@ pub struct ManagedNotification { // A Type 40 (177x177) qrcode with Medium data correction can encode max 3391 alphanumeric characters pub qrtext: Option>, } + +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] +pub struct ManagedImage { + pub token: [u32; 4], + pub tiles: [Option; 6], +} + #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] pub struct ManagedProgress { pub token: [u32; 4], @@ -72,6 +81,8 @@ pub(crate) enum Opcode { PromptWithMultiResponse, /// simple notification Notification, + /// display an image + Image, /// dynamic notification - a simple non-interactive notification that allows its text to be dynamically updated DynamicNotification, @@ -113,6 +124,7 @@ pub(crate) enum Opcode { RadioReturn, CheckBoxReturn, NotificationReturn, + ImageReturn, DoUpdateDynamicNotification, DoCloseDynamicNotification, diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index 5cc39844f..5cffbf2be 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -9,6 +9,8 @@ use gam::*; use num_traits::*; use xous::{send_message, Message, CID}; use xous_ipc::Buffer; +use dither::prelude::{Img, RGB}; + pub type TextValidationFn = fn(TextEntryPayload) -> Option; @@ -187,8 +189,40 @@ impl Modals { .or(Err(xous::Error::InternalError))?; self.unlock(); Ok(()) + } + + /// this blocks until the image has been dismissed. + pub fn show_image( + &self, + img: Img>, + ) -> Result<(), xous::Error> { + self.lock(); + + // resize image with fast_image_resize + // rotate Img with img? + + let bm = Bitmap::from(img); + let offset = Point::new(-bm.bound.tl.x, -bm.bound.tl.y); + 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(offset); + tiles[t] = Some(copy); + }; + + let spec = ManagedImage { + token: self.token, + tiles: tiles, + }; + let buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?; + buf.lend(self.conn, Opcode::Image.to_u32().unwrap()) + .or(Err(xous::Error::InternalError))?; + self.unlock(); + Ok(()) } + pub fn start_progress( &self, title: &str, diff --git a/services/modals/src/main.rs b/services/modals/src/main.rs index 69ac782bc..3f93abf3a 100644 --- a/services/modals/src/main.rs +++ b/services/modals/src/main.rs @@ -28,6 +28,7 @@ mod api; use api::*; mod tests; +use gam::Bitmap; use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack, send_message, Message}; use xous_ipc::Buffer; @@ -54,6 +55,7 @@ enum RendererState { RunText(ManagedPromptWithTextResponse), RunProgress(ManagedProgress), RunNotification(ManagedNotification), + RunImage(ManagedImage), RunDynamicNotification(DynamicNotification), } @@ -241,6 +243,24 @@ fn xmain() -> ! { ) .expect("couldn't initiate UX op"); } + Some(Opcode::Image) => { + let spec = { + let buffer = + unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + buffer.to_original::().unwrap() + }; + if spec.token != token_lock.unwrap_or(default_nonce) { + log::warn!("Attempt to access modals without a mutex lock. Ignoring."); + continue; + } + op = RendererState::RunImage(spec); + dr = Some(msg); + send_message( + renderer_cid, + Message::new_scalar(Opcode::InitiateOp.to_usize().unwrap(), 0, 0, 0, 0), + ) + .expect("couldn't initiate UX op"); + } Some(Opcode::StartProgress) => { let spec = { let buffer = @@ -426,6 +446,22 @@ fn xmain() -> ! { ); renderer_modal.activate(); } + RendererState::RunImage(config) => { + let mut image = gam::modal::Image::new( + renderer_cid, + Opcode::ImageReturn.to_u32().unwrap(), + ); + image.set_bitmap(Some(Bitmap::from(config.tiles))); + renderer_modal.modify( + Some(ActionType::Image(image)), + None, + false, + None, + true, + None, + ); + renderer_modal.activate(); + } RendererState::RunProgress(config) => { start_work = config.start_work; end_work = config.end_work; @@ -641,6 +677,25 @@ fn xmain() -> ! { panic!("UX return opcode does not match our current operation in flight. This is a serious internal error."); } } + }, + Some(Opcode::ImageReturn) => { + match op { + RendererState::RunImage(_) => { + op = RendererState::None; + dr.take(); // unblocks the caller, but without any response data + token_lock = next_lock(&mut work_queue); + } + RendererState::None => { + log::warn!("Notification detected a fat finger event, ignoring.") + } + _ => { + log::error!( + "UX return opcode does not match our current operation in flight: {:?}", + op + ); + panic!("UX return opcode does not match our current operation in flight. This is a serious internal error."); + } + } } Some(Opcode::Gutter) => { log::info!("gutter op, doing nothing"); diff --git a/services/modals/src/tests.rs b/services/modals/src/tests.rs index 1973069fb..fbd6caccb 100644 --- a/services/modals/src/tests.rs +++ b/services/modals/src/tests.rs @@ -3,6 +3,7 @@ use gam::*; use std::thread; use xous_names::XousNames; +use dither::prelude::{Img, RGB}; const RADIO_TEST: [&'static str; 4] = ["zebra", "cow", "horse", "cat"]; @@ -79,6 +80,7 @@ pub(crate) fn spawn_test() { .expect("notification failed"); log::info!("notification test done"); } + }); thread::spawn({ @@ -133,6 +135,14 @@ pub(crate) fn spawn_test() { ) .expect("qrcode failed"); log::info!("qrcode test done"); + + // 5. test image + log::info!("testing image"); + let mut input = std::env::current_dir().unwrap(); + input.push("../services/modals/src/tests/bunny.png"); + let img = Img::>::load(&input).unwrap(); + modals.show_image(img).expect("image failed"); + log::info!("image test done"); } }); } diff --git a/services/modals/src/tests/bunny.png b/services/modals/src/tests/bunny.png new file mode 100644 index 000000000..c90dfe6db Binary files /dev/null and b/services/modals/src/tests/bunny.png differ