Merge remote-tracking branch 'jb/dither' into bunnie-dev

This commit is contained in:
bunnie 2022-05-27 16:44:17 +08:00
commit c8165be0fb
19 changed files with 822 additions and 18 deletions

View file

@ -34,6 +34,11 @@ tts-frontend = {path="../tts"}
qrcode = { version = "0.12", default-features = false }
#dither = "1.3.10" pending https://gitlab.com/efronlicht/dither/-/merge_requests/28
dither = { git = "https://gitlab.com/nworbnhoj/dither", default-features = false }
# note: image version is matched to version used by dither
image = { version = "0.23", default-features = false, features = ["jpeg", "png"] }
[target.'cfg(not(any(windows,unix)))'.dependencies]
utralib = {path = "../../utralib"}

View file

@ -1,4 +1,4 @@
use graphics_server::api::{Rectangle, TextView, Line, RoundedRectangle, Circle, Point, Gid};
use graphics_server::api::{Rectangle, TextView, Line, RoundedRectangle, Tile, Circle, Point, Gid};
use xous_ipc::String;
pub(crate) const SERVER_NAME_GAM: &str = "_Graphical Abstraction Manager_";
@ -9,6 +9,7 @@ pub enum GamObjectType {
Circ(Circle),
Rect(Rectangle),
RoundRect(RoundedRectangle),
Tile(Tile),
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
@ -271,4 +272,4 @@ impl CanvasType {
pub struct GidRecord {
pub gid: Gid,
pub canvas_type: CanvasType,
}
}

280
services/gam/src/bitmap.rs Normal file
View file

@ -0,0 +1,280 @@
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 std::convert::TryInto;
use std::ops::Deref;
/*
* The basic idea is to define a Bitmap as a variable mosaic of Sized Tiles.
*
* Each Tile contains an Array of u32 Words, a bounding Rectangle, and a Word width.
* This is arranged to come in just under 4096 bytes, allowing for the rkyv overhead.
* Each line of bits across the Tile is packed into an Integer number of u32 Words.
*
* The Bitmap contains a bounding Rectangle and a Vec of Tiles. The current implmentation
* has a very simple tiling strategy - a single vertical strip of full-width tiles.
* All tiles are the same width and same maximum height - except the last Tile which may
* have some unused Words at the end of the Array. More space efficient tiling strategies
* are possible - but likely with a processing and code complexity overhead.
*
* author: nworbnhoj
*/
#[derive(Debug, Clone)]
pub struct Bitmap {
pub bound: Rectangle,
tile_size: Point,
mosaic: Vec<Tile>,
}
impl Bitmap {
pub fn new(size: Point) -> Self {
let bound = Rectangle::new(Point::new(0, 0), size);
log::trace!("new Bitmap {:?}", bound);
let (tile_size, tile_width_words) = Bitmap::tile_spec(size);
let tile_height = tile_size.y as usize;
let bm_height = (size.y + 1) as usize;
let tile_count = match bm_height % tile_height {
0 => bm_height / tile_height,
_ => bm_height / tile_height + 1,
};
let mut mosaic: Vec<Tile> = Vec::new();
for y in 0..tile_count {
let tl = Point::new(0, (y * tile_height) as i16);
let mut br = Point::new(tile_size.x - 1, ((y + 1) * tile_height - 1) as i16);
if br.y > size.y {
br.y = size.y;
}
let tile = Tile::new(Rectangle::new(tl, br), tile_width_words as u16);
mosaic.push(tile);
}
Self {
bound,
tile_size,
mosaic,
}
}
fn tile_spec(bm_size: Point) -> (Point, i16) {
let bm_width_bits = 1 + bm_size.x as usize;
let mut tile_width_bits = bm_width_bits;
let tile_width_words = if bm_width_bits > shapes::BITS_PER_TILE {
log::warn!("Bitmap max width exceeded");
tile_width_bits = shapes::WORDS_PER_TILE * shapes::BITS_PER_WORD;
shapes::WORDS_PER_TILE
} else {
match bm_width_bits % shapes::BITS_PER_WORD {
0 => bm_width_bits / shapes::BITS_PER_WORD,
_ => bm_width_bits / shapes::BITS_PER_WORD + 1,
}
};
let tile_height_bits = shapes::WORDS_PER_TILE / tile_width_words;
let tile_size = Point::new(tile_width_bits as i16, tile_height_bits as i16);
(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)
}
fn get_tile_index(&self, point: Point) -> usize {
if self.bound.intersects_point(point) {
let x = point.x as usize;
let y = point.y as usize;
let tile_width = self.tile_size.x as usize;
let tile_height = self.tile_size.y as usize;
let tile_size_bits = tile_width * tile_height;
(x + y * tile_width) / tile_size_bits
} else {
log::warn!("Out of bounds {:?}", point);
0
}
}
fn hull(mosaic: &Vec<Tile>) -> 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]
}
fn get_mut_tile(&mut self, point: Point) -> &mut Tile {
let tile = self.get_tile_index(point);
&mut self.mosaic.as_mut_slice()[tile]
}
pub fn get_line(&self, point: Point) -> Vec<shapes::Word> {
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)
}
pub fn get_pixel(&self, point: Point) -> PixelColor {
self.get_tile(point).get_pixel(point)
}
pub fn set_pixel(&mut self, point: Point, color: PixelColor) {
self.get_mut_tile(point).set_pixel(point, color)
}
pub fn translate(&mut self, offset: Point) {
for tile in self.mosaic.as_mut_slice() {
tile.translate(offset);
}
}
}
impl Deref for Bitmap {
type Target = Vec<Tile>;
fn deref(&self) -> &Self::Target {
&self.mosaic
}
}
impl From<[Option<Tile>; 6]> for Bitmap {
fn from(tiles: [Option<Tile>; 6]) -> Self {
let mut mosaic: Vec<Tile> = 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<&image::RgbImage> for Bitmap {
fn from(image: &image::RgbImage) -> Self {
let pixels: Vec<RGB<u8>> = image.pixels().map(|p| RGB::from(p.0)).collect();
let img = Img::new(pixels, image.width()).expect("failed to create dither Img");
let img = img.convert_with(|rgb: RGB<u8>| rgb.convert_with(f64::from));
let bit_depth = 1;
let quantize = dither::create_quantize_n_bits_func(bit_depth).unwrap();
let bw_img = img.convert_with(|rgb| rgb.to_chroma_corrected_black_and_white());
let ditherer = dither::ditherer::FLOYD_STEINBERG;
let output_img = ditherer
.dither(bw_img, quantize)
.convert_with(RGB::from_chroma_corrected_black_and_white);
let bm_width: usize = output_img.width().try_into().unwrap();
let img_vec = output_img.into_vec();
/*
let bw_vec = Vec::<PixelColor>::new();
for pixel in img_vec {
let color = match pixel.to_hex() {
0 => PixelColor::Light,
_ => PixelColor::Dark,
};
bw_vec.push(color);
}
*/
let bm_height = img_vec.len() / bm_width;
let bm_bottom = (bm_height - 1) as i16;
let bm_right = (bm_width - 1) as i16;
let bm_br = Point::new(bm_right, bm_bottom);
let bound = Rectangle::new(Point::new(0, 0), bm_br);
let (tile_size, tile_width_words) = Bitmap::tile_spec(bm_br);
let tile_height = tile_size.y as usize;
let tile_count = match bm_height % tile_height {
0 => bm_height / tile_height,
_ => bm_height / tile_height + 1,
};
let mut mosaic: Vec<Tile> = Vec::new();
let mut img_vec_index = 0;
for t in 0..tile_count {
let t_top = (t * tile_height) as i16;
let t_left = 0;
let t_bottom = min(bm_bottom, ((t + 1) * tile_height - 1) as i16);
let t_right = tile_size.x - 1;
let t_tl = Point::new(t_left, t_top);
let t_br = Point::new(t_right, t_bottom);
let t_bound = Rectangle::new(t_tl, t_br);
let mut tile = Tile::new(t_bound, tile_width_words as u16);
for y in t_top..=t_bottom {
// TODO performance gain here by utilizing Tile.set_line()
for x in t_left..=t_right {
let pixel = img_vec[img_vec_index];
let color = match pixel.to_hex() {
0 => PixelColor::Light,
_ => PixelColor::Dark,
};
tile.set_pixel(Point::new(x, y), color);
img_vec_index += 1;
}
}
mosaic.push(tile);
}
Self {
bound,
tile_size,
mosaic,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bitmap_test() {
let x_size = 100;
let y_size = 10;
let bm = Bitmap::new(Point::new(x_size, y_size));
assert_equal!(bm.size.x, x_size);
assert_equal!(bm.size.y, y_size);
assert_equal!(bm.get(5, 5), PixelColor::Light);
bm.set(5, 5, PixelColor::Dark);
assert_equal!(bm.get(5, 5), PixelColor::Dark);
}
}

View file

@ -10,9 +10,12 @@ pub mod menu;
pub use menu::*;
pub mod apps;
pub use apps::*;
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
@ -188,6 +191,15 @@ 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> {
let mut list = GamObjectList::new(gid);
for (_i, tile) in bm.iter().enumerate(){
list.push(GamObjectType::Tile(*tile)).unwrap();
};
let buf = Buffer::into_buf(list).or(Err(xous::Error::InternalError))?;
buf.lend(self.conn, Opcode::RenderObjectList.to_u32().unwrap())
.map(|_| ())
}
pub fn draw_circle(&self, gid: Gid, circ: Circle) -> Result<(), xous::Error> {
let go = GamObject {
canvas: gid,
@ -454,4 +466,4 @@ fn forwarding_thread(addr: usize, size: usize, offset: usize) {
}
log::trace!("modal forwarding server exiting");
xous::destroy_server(xous::SID::from_array(forwarding_config.public_sid)).expect("can't destroy my server on exit!");
}
}

View file

@ -337,6 +337,14 @@ fn xmain() -> ! {
canvas.clip_rect(),
).expect("couldn't draw rounded rectangle");
}
GamObjectType::Tile(mut tile) => {
tile.translate(canvas.clip_rect().tl);
tile.translate(canvas.pan_offset());
gfx.draw_tile_clipped(
tile,
canvas.clip_rect(),
).expect("couldn't draw bitmap");
}
}
canvas.do_drawn().expect("couldn't set canvas to drawn");
} else {
@ -376,6 +384,11 @@ fn xmain() -> ! {
rr.translate(canvas.clip_rect().tl);
rr.translate(canvas.pan_offset());
obj_list.push(ClipObjectType::RoundRect(rr), canvas.clip_rect()).unwrap();
},
GamObjectType::Tile(mut tile) => {
tile.translate(canvas.clip_rect().tl);
tile.translate(canvas.pan_offset());
obj_list.push(ClipObjectType::Tile(tile), canvas.clip_rect()).unwrap();
}
}
} else {

View file

@ -8,6 +8,8 @@ mod checkboxes;
pub use checkboxes::*;
mod notification;
pub use notification::*;
mod image;
pub use self::image::*;
mod slider;
pub use slider::*;
mod progressbar;
@ -37,6 +39,7 @@ pub enum ActionType {
CheckBoxes,
Slider,
Notification,
Image,
ConsoleInput
}

View file

@ -0,0 +1,62 @@
use crate::*;
#[derive(Debug)]
pub struct Image {
pub action_conn: xous::CID,
pub action_opcode: u32,
pub bitmap: Option<Bitmap>,
}
impl Image {
pub fn new(action_conn: xous::CID, action_opcode: u32) -> Self {
Image {
action_conn,
action_opcode,
bitmap: None,
}
}
pub fn set_bitmap(&mut self, setting: Option<Bitmap>) {
self.bitmap = setting;
}
}
impl ActionApi for Image {
fn set_action_opcode(&mut self, op: u32) {
self.action_opcode = op
}
fn height(&self, _glyph_height: i16, margin: i16) -> i16 {
let bm_height = match &self.bitmap {
Some(bm) => bm.bound.br.y - bm.bound.tl.y,
None => 0,
};
log::info!("bitmap height {:?} : margin {}", bm_height, margin);
margin * 2 + bm_height
}
fn redraw(&self, _at_height: i16, modal: &Modal) {
if self.bitmap.is_some() {
//bm.translate(Point::new(0, at_height));
modal
.gam
.draw_bitmap(modal.canvas, self.bitmap.as_ref().unwrap())
.expect("couldn't draw bitmap");
}
}
fn key_action(&mut self, k: char) -> (Option<ValidatorErr>, bool) {
log::trace!("key_action: {}", k);
match k {
'\u{0}' => {
// ignore null messages
}
_ => {
send_message(
self.action_conn,
xous::Message::new_scalar(self.action_opcode as usize, 0, 0, 0, 0),
)
.expect("couldn't pass on dismissal");
return (None, true);
}
}
(None, false)
}
}

View file

@ -73,6 +73,9 @@ pub(crate) enum Opcode {
/// Draw a rounded rectangle
RoundedRectangle, //(RoundedRectangle),
/// Paint a Bitmap Tile
Tile,
/// Draw a circle with a specified radius
Circle, //(Circle),
@ -116,6 +119,7 @@ pub enum ClipObjectType {
Rect(Rectangle),
RoundRect(RoundedRectangle),
XorLine(Line),
Tile(Tile),
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]

View file

@ -1,6 +1,8 @@
use crate::api::{DrawStyle, Point, ClipRect};
use crate::api::{ClipRect, DrawStyle, PixelColor, Point};
use crate::op::{HEIGHT, WIDTH};
use core::cmp::{max, min};
use std::convert::TryInto;
#[derive(Debug, Clone, Copy, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub struct Rectangle {
@ -25,6 +27,7 @@ impl Rectangle {
style: DrawStyle::default(),
}
}
pub fn new_coords(x0: i16, y0: i16, x1: i16, y1: i16) -> Rectangle {
Rectangle {
tl: Point::new(min(x0, x1), min(y0, y1)),
@ -347,6 +350,127 @@ impl RoundedRectangle {
}
}
//////////////////////// Tile -------- author: nworbnhoj
/*
* A Tile contains an Array of u32 Words, a bounding Rectangle, and a Word width.
* This is arranged to come in just under 4096 bytes, allowing for the rkyv overhead.
* Each line of bits across the Tile is packed into an Integer number of u32 Words.
* Hence, there may be unused bits in the right-most Word in each line, and a few
* unused Words at the end of the Array. This arrangement is similar to the structure
* of the frame-buffer in the graphics-server to facilitate efficient transfer.
*/
pub type Word = u32;
pub const BITS_PER_WORD: usize = Word::BITS as usize;
pub(crate) const META_WORDS_PER_TILE: usize = 4;
pub const WORDS_PER_TILE: usize = (4096 * 8 / BITS_PER_WORD) - META_WORDS_PER_TILE;
pub const BITS_PER_TILE: usize = WORDS_PER_TILE * BITS_PER_WORD;
pub(crate) const OUT_OF_BOUNDS: usize = usize::MAX;
#[derive(Debug, Clone, Copy, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub struct Tile {
bound: Rectangle,
width_words: u16,
words: [Word; WORDS_PER_TILE],
}
impl Tile {
pub fn new(bound: Rectangle, width_words: u16) -> Self {
log::trace!("new Tile {:?}", bound);
let words = [0u32; WORDS_PER_TILE];
Self {
bound,
width_words,
words,
}
}
pub fn bound(&self) -> Rectangle {
self.bound
}
pub fn area(&self) -> u32 {
let size = self.size();
(size.x * size.y).try_into().unwrap()
}
pub fn size(&self) -> Point {
let pos = self.bound;
Point::new(pos.br.x - pos.tl.x, pos.br.y - pos.tl.y)
}
fn word_index(&self, point: Point) -> usize {
if self.bound.intersects_point(point) {
let tile_line: u16 = (point.y - self.bound.tl.y).try_into().unwrap();
let first_word_in_line:usize = (tile_line * self.width_words).try_into().unwrap();
let bit_index: usize = (point.x - self.bound.tl.x).try_into().unwrap();
first_word_in_line + (bit_index / BITS_PER_WORD)
} else {
log::warn!("point {:?} out of bounds {:?}", point, self.bound);
OUT_OF_BOUNDS
}
}
pub fn get_word(&self, point: Point) -> Word {
self.words[self.word_index(point)]
}
pub fn set_word(&mut self, point: Point, word: Word) {
self.words[self.word_index(point)] = word;
}
pub fn get_line(&self, point: Point) -> Vec<Word> {
let first_pixel_in_line = Point::new(self.bound.tl.x, point.y);
let first_word = self.word_index(first_pixel_in_line);
let width: usize = self.width_words.try_into().unwrap();
match first_word == OUT_OF_BOUNDS {
true => Vec::new(),
false => {
let last_word = first_word + width;
self.words[first_word..last_word].to_vec()
}
}
}
pub fn set_line(&self, _point: Point, _pixels: Vec<PixelColor>){
log::warn!("not implemented");
}
pub fn get_pixel(&self, point: Point) -> PixelColor {
let word: usize = self.get_word(point).try_into().unwrap();
let bpw: i16 = BITS_PER_WORD.try_into().unwrap();
let bit = point.x % bpw;
PixelColor::from((word >> bit) & 1)
}
pub fn set_pixel(&mut self, point: Point, color: PixelColor) {
let word_index = self.word_index(point);
let bpw: i16 = BITS_PER_WORD.try_into().unwrap();
match word_index == OUT_OF_BOUNDS {
true => {}
false => {
let word = self.words[word_index];
let bit = point.x % bpw;
match color {
PixelColor::Dark => self.words[word_index] = word | 1 << bit,
PixelColor::Light => self.words[word_index] = word & !(1 << bit),
}
}
};
}
pub fn translate(&mut self, offset: Point) {
self.bound.tl.x += offset.x;
self.bound.tl.y += offset.y;
self.bound.br.x += offset.x;
self.bound.br.y += offset.y;
}
}
#[cfg(test)]
mod tests {
use super::*;
@ -391,4 +515,5 @@ mod tests {
// to the right of
assert!(!a.intersects(Rectangle::new(Point::new(101, 0), Point::new(150, 150),)));
}
}

View file

@ -4,7 +4,7 @@
pub mod api;
pub use api::{
Circle, ClipObject, ClipObjectType, DrawStyle, Gid, Line, PixelColor, Point, Rectangle,
RoundedRectangle, TextBounds, TextOp, TextView, TokenClaim, ClipRect, Cursor, GlyphStyle, ClipObjectList
RoundedRectangle, Tile, TextBounds, TextOp, TextView, TokenClaim, ClipRect, Cursor, GlyphStyle, ClipObjectList
};
pub mod op;
@ -208,6 +208,20 @@ impl Gfx {
.map(|_| ())
}
pub fn draw_tile_clipped(
&self,
tile: Tile,
clip: Rectangle,
) -> Result<(), xous::Error> {
let co = ClipObject {
clip,
obj: ClipObjectType::Tile(tile),
};
let buf = Buffer::into_buf(co).or(Err(xous::Error::InternalError))?;
buf.lend(self.conn, Opcode::DrawClipObject.to_u32().unwrap())
.map(|_| ())
}
pub fn draw_object_list_clipped(
&self,
list: ClipObjectList,

View file

@ -172,6 +172,9 @@ fn xmain() -> ! {
ClipObjectType::RoundRect(rr) => {
op::rounded_rectangle(display.native_buffer(), rr, Some(obj.clip));
}
ClipObjectType::Tile(tile) => {
op::tile(display.native_buffer(), tile, Some(obj.clip));
}
}
}
Some(Opcode::DrawClipObjectList) => {
@ -196,6 +199,9 @@ fn xmain() -> ! {
ClipObjectType::RoundRect(rr) => {
op::rounded_rectangle(display.native_buffer(), rr, Some(obj.clip));
}
ClipObjectType::Tile(tile) => {
op::tile(display.native_buffer(), tile, Some(obj.clip));
}
}
} else {
// stop at the first None entry -- if the sender packed the list with a hole in it, that's their bad
@ -373,6 +379,12 @@ fn xmain() -> ! {
);
op::rounded_rectangle(display.native_buffer(), rr, screen_clip.into());
}),
Some(Opcode::Tile) => {
let buffer =
unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let bm = buffer.to_original::<Tile, _>().unwrap();
op::tile(display.native_buffer(), bm, screen_clip.into());
},
Some(Opcode::Circle) => msg_scalar_unpack!(msg, center, radius, style, _, {
let c = Circle::new_with_style(
Point::from(center),

View file

@ -1,4 +1,7 @@
use crate::api::{Circle, DrawStyle, Line, Pixel, PixelColor, Point, Rectangle, RoundedRectangle};
use crate::api::{
Circle, DrawStyle, Line, Pixel, PixelColor, Point, Rectangle, RoundedRectangle, Tile,
};
use std::cmp::min;
/// LCD Frame buffer bounds
pub const LCD_WORDS_PER_LINE: usize = 11;
@ -564,3 +567,88 @@ pub fn rounded_rectangle(fb: &mut LcdFB, rr: RoundedRectangle, clip: Option<Rect
clip,
);
}
/*
* Each line of the LCD Frame Buffer and the Tile is represented by a whole
* number of u32 words, packed with bits, from the left. The right most word
* may have unused bits on the right. The right-most bit in the frame-buffer
* is used as a dirty-line bit flag.
*
* A brute force approach would copy each pixel one-by-one from Tile to fb.
*
* We can do better by copying a word at a time, but there are a few challenges
* In general (unfortunately) a line of Words in the Tile is not aligned with
* the Words in the Frame Buffer. And, even if it were, the right side of the
* Tile will not generally align with a Word boundry.
*
* So the approach for each line is to:
* - start with the first word in the fb impacted by the tile
* - take the left-part of the fb Word and shift the tile Word into the right-part
* - replace the next fb Words with the aligned parts of adjacent parts from the tile Words
* - align the last tile Word, clip it and join with the right-most fb word beyond the clip
* - finally set the dirty-line bit flag on the right most fb Word.
*
* author: nworbnhoj
*/
pub fn tile(fb: &mut LcdFB, tile: Tile, clip: Option<Rectangle>) {
// 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;
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),
};
br_x = min(br_x, LCD_PX_PER_LINE - 1);
br_y = min(br_y, LCD_LINES - 1);
// 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
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;
for tile_ln in tl_y..=br_y {
let fb_ln = tile_ln * LCD_WORDS_PER_LINE;
let tile_line = tile.get_line(Point::new(tl_x as i16, tile_ln as i16));
let mut fb_wd = first_word;
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()) {
fb_word = fb[fb_ln + fb_wd];
tile_word = tile_line[tile_wd];
fb[fb_ln + fb_wd] = match r_cut {
0 => tile_word,
_ => {
// create an aligned word from parts of adjacent tile words
right_part = tile_word << r_cut;
let word = left_part | right_part;
left_part = tile_word >> l_cut;
word
}
};
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;
// set the dirty bit on the line
fb[fb_ln + (LCD_WORDS_PER_LINE - 1)] |= 0x1_0000;
}
}

View file

@ -22,10 +22,15 @@ 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", default-features = false }
# note: image version is matched to version used by dither
image = { version = "0.23", default-features = false, features = ["jpeg", "png"] }
fast_image_resize = "0.9.2"
[target.'cfg(not(any(windows,unix)))'.dependencies]
utralib = { path = "../../utralib"}
[features]
ux_tests = []
tts = []
default = []

View file

@ -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<xous_ipc::String<4096>>,
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)]
pub struct ManagedImage {
pub token: [u32; 4],
pub tiles: [Option<Tile>; 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,

View file

@ -2,11 +2,16 @@
pub mod api;
use api::*;
pub mod tests;
use bit_field::BitField;
use core::cell::Cell;
use fast_image_resize as fir;
use gam::*;
use image::{imageops, RgbImage};
use num_traits::*;
use std::convert::TryInto;
use std::num::NonZeroU32;
use xous::{send_message, Message, CID};
use xous_ipc::Buffer;
@ -189,6 +194,96 @@ impl Modals {
Ok(())
}
const MODAL_WIDTH: u32 = 300;
const MODAL_HEIGHT: u32 = 370;
/// this blocks until the image has been dismissed.
pub fn show_image(&self, img: &RgbImage) -> 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 (img_w, img_h) = img.dimensions();
let (img_width, img_height) = (img_w as f32, img_h as f32);
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 modal_img: RgbImage = img.clone();
if portrait_scale >= 1.0 {
//noop
} else if landscape_scale >= 1.0 {
modal_img = imageops::rotate90(&modal_img);
} else if portrait_scale >= landscape_scale {
modal_img = Modals::resize_image(modal_img, portrait_scale);
} else {
modal_img = Modals::resize_image(modal_img, landscape_scale);
modal_img = imageops::rotate90(&modal_img);
}
// center image in modal
let center = Point::new(
((Modals::MODAL_WIDTH - modal_img.width()) / 2)
.try_into()
.unwrap(),
((Modals::MODAL_HEIGHT - modal_img.height()) / 2)
.try_into()
.unwrap(),
);
let bm = Bitmap::from(&modal_img);
let mut tiles: [Option<Tile>; 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);
}
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(())
}
fn resize_image(img: RgbImage, scale: f32) -> RgbImage {
let width: u32 = (img.width() as f32 * scale).floor() as u32;
let height: u32 = (img.height() as f32 * scale).floor() as u32;
let fir_img = fir::Image::from_vec_u8(
NonZeroU32::new(img.width()).unwrap(),
NonZeroU32::new(img.height()).unwrap(),
img.into_vec(),
fir::PixelType::U8x3,
)
.unwrap();
// Create container for data of destination image
let mut resized = fir::Image::new(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
fir_img.pixel_type(),
);
let mut rsz_view = resized.view_mut();
// resize image with fastest available algorithm
let mut resizer = fir::Resizer::new(fir::ResizeAlg::Nearest);
resizer.resize(&fir_img.view(), &mut rsz_view).unwrap();
// convert back into an RgbImage
RgbImage::from_vec(
resized.width().try_into().unwrap(),
resized.height().try_into().unwrap(),
resized.into_vec(),
)
.expect("failed to convert to RgbImage")
}
pub fn start_progress(
&self,
title: &str,

View file

@ -27,7 +27,7 @@
/// a `TextResponseValid` message which pumps the work queue.
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 +54,7 @@ enum RendererState {
RunText(ManagedPromptWithTextResponse),
RunProgress(ManagedProgress),
RunNotification(ManagedNotification),
RunImage(ManagedImage),
RunDynamicNotification(DynamicNotification),
}
@ -124,11 +125,6 @@ fn xmain() -> ! {
let mut list_hash = HashMap::<String, usize>::new();
let mut list_selected = 0u32;
if cfg!(feature = "ux_tests") {
tt.sleep_ms(1000).unwrap();
tests::spawn_test();
}
let mut token_lock: Option<[u32; 4]> = None;
let trng = trng::Trng::new(&xns).unwrap();
// this is a random number that serves as a "default" that cannot be guessed
@ -241,6 +237,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::<ManagedImage, _>().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 +440,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 +671,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");

View file

@ -1,8 +1,9 @@
#![allow(dead_code)]
use gam::*;
use std::thread;
use xous_names::XousNames;
use image::io::{Reader};
use super::*;
const RADIO_TEST: [&'static str; 4] = ["zebra", "cow", "horse", "cat"];
@ -15,12 +16,12 @@ const CHECKBOX_TEST: [&'static str; 5] = ["happy", "😃", "安", "peaceful", ".
///
/// Each thread will create a series of Modal primitives, including progess bars, notifications,
/// check boxes and radio boxes.
pub(crate) fn spawn_test() {
pub fn spawn_test() {
// spawn two threads that compete for modal resources, to test the interlocking mechanisms
thread::spawn({
move || {
let xns = XousNames::new().unwrap();
let modals = modals::Modals::new(&xns).unwrap();
let modals = Modals::new(&xns).unwrap();
let tt = ticktimer_server::Ticktimer::new().unwrap();
// 0. multi-modal test
@ -84,7 +85,7 @@ pub(crate) fn spawn_test() {
thread::spawn({
move || {
let xns = XousNames::new().unwrap();
let modals = modals::Modals::new(&xns).unwrap();
let modals = Modals::new(&xns).unwrap();
// 1. test radio box
for item in RADIO_TEST {
@ -133,6 +134,25 @@ pub(crate) fn spawn_test() {
)
.expect("qrcode failed");
log::info!("qrcode test done");
// 5. test image
log::info!("testing image");
let reader = match Reader::open("../services/modals/src/tests/bunny.png") {
Err(err) => {
log::error!("cannot load image, {}", err);
return
}
Ok(r) => r,
};
let img = match reader.decode() {
Err(e) => {
log::error!("failed to decode image, {}", e);
return
}
Ok(img) => img.into_rgb8(),
};
modals.show_image(&img).expect("image modal failed");
log::info!("image test done");
}
});
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 KiB

View file

@ -10,6 +10,7 @@ use core::fmt::Write;
use core::sync::atomic::{AtomicBool, Ordering, AtomicU32};
use std::sync::Arc;
use num_traits::*;
use modals;
use std::time::Instant;
@ -608,6 +609,9 @@ impl<'a> ShellCmdApi<'a> for Test {
write!(ret, "Ship mode request denied").unwrap();
}
}
"modals" => {
modals::tests::spawn_test();
}
_ => {
() // do nothing
}