implement graphics-server Tile

This commit is contained in:
nworbnhoj 2022-05-18 11:00:06 +10:00
parent 867b0efdf2
commit 61d6a4b853
5 changed files with 237 additions and 3 deletions

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,4 +1,4 @@
use crate::api::{DrawStyle, Point, ClipRect};
use crate::api::{ClipRect, DrawStyle, PixelColor, Point};
use crate::op::{HEIGHT, WIDTH};
use core::cmp::{max, min};
@ -25,6 +25,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 +348,119 @@ 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 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 = (point.y - self.bound.tl.y) as usize;
let first_word_in_line = tile_line * self.width_words as usize;
let bit_index = (point.x - self.bound.tl.x) as usize;
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);
match first_word == OUT_OF_BOUNDS {
true => Vec::new(),
false => {
let last_word = first_word + self.width_words as usize;
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 = self.get_word(point);
let bit = point.x as usize % BITS_PER_WORD;
PixelColor::from((word as usize >> bit) & 1)
}
pub fn set_pixel(&mut self, point: Point, color: PixelColor) {
let word_index = self.word_index(point);
match word_index == OUT_OF_BOUNDS {
true => {}
false => {
let word = self.words[word_index];
let bit = point.x as usize % BITS_PER_WORD;
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 +505,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,89 @@ 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 = 0;
let mut word = fb_word;
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;
}
}