add implemnetation for most graphics primitives

this is mostly a copy of what's in graphics-server. Perhaps
we can find a way to share the code between the two, but I
am going to leave it duplicated for now because it's unclear
if I can use this implementation as-is, or if it needs lots
of modifications.

If it turns out to basically work without mods, then, we can
wrap a bunch of the opcode dispatch into mega-function perhaps
that takes a display and opcode as parameters and wraps a lot
of the boilerplate into a re-usable routine.
This commit is contained in:
bunnie 2025-02-23 12:05:00 +08:00
parent 9f890d3dbd
commit 8c9abf6c5b
4 changed files with 818 additions and 297 deletions

View file

@ -19,6 +19,12 @@ cram-hal-service = { path = "../cram-hal-service", optional = true }
ux-api = { path = "../../libs/ux-api" } ux-api = { path = "../../libs/ux-api" }
cramium-emu = { path = "../cramium-emu", optional = true } cramium-emu = { path = "../cramium-emu", optional = true }
cramium-api = { path = "../../libs/cramium-api" } cramium-api = { path = "../../libs/cramium-api" }
blitstr2 = { path = "../../libs/blitstr2" }
xous-ipc = "0.10.4"
rkyv = { version = "0.8.8", default-features = false, features = [
"std",
"alloc",
] }
# QR decoding # QR decoding
libm = { version = "0.2.8" } libm = { version = "0.2.8" }
@ -28,12 +34,17 @@ rqrr = "0.8.0"
locales = { path = "../../locales" } locales = { path = "../../locales" }
[features] [features]
ditherpunk = []
# This crate is baosec specific; these flags switch between hosted mode emulation # This crate is baosec specific; these flags switch between hosted mode emulation
# (for UI development) and actual hardware targets. In UI development all camera data # (for UI development) and actual hardware targets. In UI development all camera data
# is "faked" # is "faked"
board-baosec = ["cram-hal-service", "cramium-hal", "ux-api/board-baosec"] board-baosec = ["cram-hal-service", "cramium-hal", "ux-api/board-baosec"]
hosted-baosec = ["cramium-emu", "ux-api/hosted-baosec"] hosted-baosec = ["cramium-emu", "ux-api/hosted-baosec"]
# temporary feature - remove after we have properly created a camera control API
autotest = []
cramium-soc = ["utralib/cramium-soc"] cramium-soc = ["utralib/cramium-soc"]
decongest-udma = [] decongest-udma = []
default = [ default = [

View file

@ -1,7 +0,0 @@
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub enum Opcode {
CamIrq,
InvalidCall,
}
pub const SERVER_NAME_GFX: &str = "_Graphics_";

View file

@ -1,2 +1,2 @@
mod api; pub use ux_api::service::api::*;
pub use api::*; pub use ux_api::service::gfx::Gfx;

View file

@ -1,11 +1,18 @@
use ux_api::minigfx::*; use ux_api::minigfx::*;
mod api;
mod gfx; mod gfx;
mod homography; mod homography;
mod modules; mod modules;
#[cfg(feature = "board-baosec")]
mod panic;
mod qr; mod qr;
use api::*; use core::ops::Add;
use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
use blitstr2::{GlyphStyle, fontmap, glyph_to_height_hint};
#[cfg(feature = "board-baosec")] #[cfg(feature = "board-baosec")]
use cram_hal_service::{I2c, UdmaGlobal}; use cram_hal_service::{I2c, UdmaGlobal};
use cramium_api::*; use cramium_api::*;
@ -24,6 +31,11 @@ use cramium_hal::{
use num_traits::*; use num_traits::*;
#[cfg(not(feature = "hosted-baosec"))] #[cfg(not(feature = "hosted-baosec"))]
use utralib::utra; use utralib::utra;
use ux_api::minigfx::FrameBuffer;
use ux_api::service::api::*;
use ux_api::wordwrap::*;
use xous::{MemoryRange, msg_blocking_scalar_unpack, msg_scalar_unpack};
use xous_ipc::Buffer;
// Scope of this crate: *No calls to modals* this can create dependency lockups. // Scope of this crate: *No calls to modals* this can create dependency lockups.
// //
@ -55,22 +67,22 @@ pub const BW_THRESH: u8 = 128;
// luck of the draw if the interpolation hits exactly right, or if we're roughly a module // luck of the draw if the interpolation hits exactly right, or if we're roughly a module
// off from ideal, which causes the data around that point to be interpreted incorrectly. // off from ideal, which causes the data around that point to be interpreted incorrectly.
pub fn blit_to_display(sh1107: &mut Oled128x128, frame: &[u8], display_cleared: bool) { pub fn blit_to_display(display: &mut Oled128x128, frame: &[u8], display_cleared: bool) {
for (y, row) in frame.chunks(IMAGE_WIDTH).enumerate() { for (y, row) in frame.chunks(IMAGE_WIDTH).enumerate() {
if y & 1 == 0 { if y & 1 == 0 {
for (x, &pixval) in row.iter().enumerate() { for (x, &pixval) in row.iter().enumerate() {
if x & 1 == 0 { if x & 1 == 0 {
if x < sh1107.dimensions().x as usize * 2 if x < display.dimensions().x as usize * 2
&& y < sh1107.dimensions().y as usize * 2 - (gfx::CHAR_HEIGHT as usize + 1) * 2 && y < display.dimensions().y as usize * 2 - (gfx::CHAR_HEIGHT as usize + 1) * 2
{ {
let luminance = pixval & 0xff; let luminance = pixval & 0xff;
if luminance > BW_THRESH { if luminance > BW_THRESH {
sh1107.put_pixel(Point::new(x as isize / 2, y as isize / 2), Mono::White.into()); display.put_pixel(Point::new(x as isize / 2, y as isize / 2), Mono::White.into());
} else { } else {
// optimization to avoid some computation if we're blitting to an already-black // optimization to avoid some computation if we're blitting to an already-black
// buffer // buffer
if !display_cleared { if !display_cleared {
sh1107.put_pixel( display.put_pixel(
Point::new(x as isize / 2, y as isize / 2), Point::new(x as isize / 2, y as isize / 2),
Mono::Black.into(), Mono::Black.into(),
); );
@ -102,11 +114,61 @@ fn handle_irq(_irq_no: usize, arg: *mut usize) {
// activate the handler // activate the handler
xous::try_send_message( xous::try_send_message(
cam_irq.cid, cam_irq.cid,
xous::Message::new_scalar(Opcode::CamIrq.to_usize().unwrap(), pending as usize, 0, 0, 0), xous::Message::new_scalar(GfxOpcode::CamIrq.to_usize().unwrap(), pending as usize, 0, 0, 0),
) )
.ok(); .ok();
} }
#[cfg(any(feature = "cramium-soc"))]
fn map_fonts() -> MemoryRange {
log::trace!("mapping fonts");
// this maps an extra page if the total length happens to fall on a 4096-byte boundary, but this is ok
// because the reserved area is much larger
let fontlen: u32 = ((fontmap::FONT_TOTAL_LEN as u32 + 8) & 0xFFFF_F000) + 0x1000;
log::trace!("requesting map of length 0x{:08x} at 0x{:08x}", fontlen, fontmap::FONT_BASE);
let fontregion = xous::syscall::map_memory(
xous::MemoryAddress::new(fontmap::FONT_BASE),
None,
fontlen as usize,
xous::MemoryFlags::R,
)
.expect("couldn't map fonts");
log::info!(
"font base at virtual 0x{:08x}, len of 0x{:08x}",
fontregion.as_ptr() as usize,
usize::from(fontregion.len())
);
log::trace!(
"mapping tall font to 0x{:08x}",
fontregion.as_ptr() as usize + fontmap::TALL_OFFSET as usize
);
blitstr2::fonts::bold::GLYPH_LOCATION
.store((fontregion.as_ptr() as usize + fontmap::BOLD_OFFSET as usize) as u32, Ordering::SeqCst);
blitstr2::fonts::emoji::GLYPH_LOCATION
.store((fontregion.as_ptr() as usize + fontmap::EMOJI_OFFSET as usize) as u32, Ordering::SeqCst);
blitstr2::fonts::mono::GLYPH_LOCATION
.store((fontregion.as_ptr() as usize + fontmap::MONO_OFFSET as usize) as u32, Ordering::SeqCst);
blitstr2::fonts::tall::GLYPH_LOCATION
.store((fontregion.as_ptr() as usize + fontmap::TALL_OFFSET as usize) as u32, Ordering::SeqCst);
blitstr2::fonts::regular::GLYPH_LOCATION
.store((fontregion.as_ptr() as usize + fontmap::REGULAR_OFFSET as usize) as u32, Ordering::SeqCst);
blitstr2::fonts::small::GLYPH_LOCATION
.store((fontregion.as_ptr() as usize + fontmap::SMALL_OFFSET as usize) as u32, Ordering::SeqCst);
fontregion
}
#[cfg(not(target_os = "xous"))]
fn map_fonts() -> MemoryRange {
// does nothing
let fontlen: u32 = ((fontmap::FONT_TOTAL_LEN as u32 + 8) & 0xFFFF_F000) + 0x1000;
let fontregion = xous::syscall::map_memory(None, None, fontlen as usize, xous::MemoryFlags::R)
.expect("couldn't map dummy memory for fonts");
fontregion
}
fn main() -> ! { fn main() -> ! {
let stack_size = 1 * 1024 * 1024; let stack_size = 1 * 1024 * 1024;
claim_main_thread(move |main_thread_token| { claim_main_thread(move |main_thread_token| {
@ -123,21 +185,38 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
log_server::init_wait().unwrap(); log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Info); log::set_max_level(log::LevelFilter::Info);
// ---- Xous setup
let xns = xous_names::XousNames::new().unwrap(); let xns = xous_names::XousNames::new().unwrap();
let sid = xns.register_name(crate::api::SERVER_NAME_GFX, None).expect("can't register server"); let sid = xns.register_name(SERVER_NAME_GFX, None).expect("can't register server");
let tt = ticktimer::Ticktimer::new().unwrap(); let tt = ticktimer::Ticktimer::new().unwrap();
// ---- basic hardware setup
let iox = IoxHal::new(); let iox = IoxHal::new();
let udma_global = UdmaGlobal::new(); let udma_global = UdmaGlobal::new();
let mut i2c = I2c::new(); let mut i2c = I2c::new();
let mut sh1107 = Oled128x128::new(main_thread_token, cramium_api::PERCLK, &iox, &udma_global); let mut display = Oled128x128::new(main_thread_token, cramium_api::PERCLK, &iox, &udma_global);
sh1107.init(); display.init();
gfx::msg(&mut sh1107, "hello world", Point::new(0, 64), Mono::White.into(), Mono::Black.into()); display.buffer_swap();
sh1107.buffer_swap(); display.draw();
sh1107.draw();
let fontregion = map_fonts();
// ---- panic handler - set up early so we can see panics quickly
// install the graphical panic handler. It won't catch really early panics, or panics in this crate,
// but it'll do the job 90% of the time and it's way better than having none at all.
let is_panic = Arc::new(AtomicBool::new(false));
// This is safe because the SPIM is finished with initialization, and the handler is
// Mutex-protected.
#[cfg(feature = "board-baosec")]
{
let hw_if = unsafe { display.hw_regs() };
panic::panic_handler_thread(is_panic.clone(), hw_if);
}
// ---- camera initialization
#[cfg(not(feature = "hosted-baosec"))] #[cfg(not(feature = "hosted-baosec"))]
{ {
// setup camera pins // setup camera pins
@ -167,7 +246,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
log::info!("Decongest udma option enabled."); log::info!("Decongest udma option enabled.");
let cid = xous::connect(sid).unwrap(); // self-connection always succeeds let cid = xous::connect(sid).unwrap(); // self-connection always succeeds
// register interrupt handler
// ---- register interrupt handler
#[cfg(not(feature = "hosted-baosec"))] #[cfg(not(feature = "hosted-baosec"))]
let cam_irq; // this binding has to out-live the temporaries below let cam_irq; // this binding has to out-live the temporaries below
#[cfg(not(feature = "hosted-baosec"))] #[cfg(not(feature = "hosted-baosec"))]
@ -191,22 +271,37 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
irq_csr.wfo(utra::irqarray8::EV_ENABLE_CAM_RX, 1); irq_csr.wfo(utra::irqarray8::EV_ENABLE_CAM_RX, 1);
} }
// ---- boot logo
display.blit_screen(&ux_api::bitmaps::baochip128x128::BITMAP);
display.redraw();
// ---- main loop variables
let screen_clip = Rectangle::new(Point::new(0, 0), display.screen_size());
let mut bulkread = BulkRead::default(); // holding buffer for bulk reads; wastes ~8k when not in use, but saves a lot of copy/init for each iteration of the read
// this will kick the hardware into the QR code scanning routine automatically. Eventually
// this needs to be turned into a call that can invoke and abort the QR code scanning.
#[cfg(feature = "autotest")]
{
// kick off the first acquisition synchronous to the frame sync. With NTO/DAR-704 fix we // kick off the first acquisition synchronous to the frame sync. With NTO/DAR-704 fix we
// could turn on the sync_sof field and skip this step. // could turn on the sync_sof field and skip this step.
while iox.get_gpio_pin_value(IoxPort::PB, 9) == IoxValue::High {} while iox.get_gpio_pin_value(IoxPort::PB, 9) == IoxValue::High {}
cam.capture_async(); cam.capture_async();
}
let mut frames = 0; let mut frames = 0;
let mut frame = [0u8; IMAGE_WIDTH * IMAGE_HEIGHT]; let mut frame = [0u8; IMAGE_WIDTH * IMAGE_HEIGHT];
let mut decode_success; let mut decode_success;
let mut msg_opt = None; let mut msg_opt = None;
loop { loop {
if !is_panic.load(Ordering::Relaxed) {
xous::reply_and_receive_next(sid, &mut msg_opt).unwrap(); xous::reply_and_receive_next(sid, &mut msg_opt).unwrap();
let msg = msg_opt.as_mut().unwrap(); let msg = msg_opt.as_mut().unwrap();
let opcode = num_traits::FromPrimitive::from_usize(msg.body.id()).unwrap_or(Opcode::InvalidCall); let opcode =
num_traits::FromPrimitive::from_usize(msg.body.id()).unwrap_or(GfxOpcode::InvalidCall);
log::debug!("{:?}", opcode); log::debug!("{:?}", opcode);
match opcode { match opcode {
Opcode::CamIrq => { GfxOpcode::CamIrq => {
#[cfg(not(feature = "decongest-udma"))] #[cfg(not(feature = "decongest-udma"))]
cam.capture_async(); cam.capture_async();
@ -224,7 +319,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
// vertically. // vertically.
for (y_src, line) in fb.chunks(IMAGE_WIDTH / 2).enumerate() { for (y_src, line) in fb.chunks(IMAGE_WIDTH / 2).enumerate() {
for (x_src, &u32src) in line.iter().enumerate() { for (x_src, &u32src) in line.iter().enumerate() {
frame[(IMAGE_HEIGHT - y_src - 1) * IMAGE_WIDTH + 2 * x_src] = (u32src & 0xff) as u8; frame[(IMAGE_HEIGHT - y_src - 1) * IMAGE_WIDTH + 2 * x_src] =
(u32src & 0xff) as u8;
frame[(IMAGE_HEIGHT - y_src - 1) * IMAGE_WIDTH + 2 * x_src + 1] = frame[(IMAGE_HEIGHT - y_src - 1) * IMAGE_WIDTH + 2 * x_src + 1] =
((u32src >> 16) & 0xff) as u8; ((u32src >> 16) & 0xff) as u8;
} }
@ -234,7 +330,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
let mut candidates = Vec::<Point>::new(); let mut candidates = Vec::<Point>::new();
decode_success = false; decode_success = false;
log::info!("------------- SEARCH {} -----------", frames); log::info!("------------- SEARCH {} -----------", frames);
let finder_width = qr::find_finders(&mut candidates, &frame, BW_THRESH, IMAGE_WIDTH) as isize; let finder_width =
qr::find_finders(&mut candidates, &frame, BW_THRESH, IMAGE_WIDTH) as isize;
if candidates.len() == 3 { if candidates.len() == 3 {
let candidates_orig = candidates.clone(); let candidates_orig = candidates.clone();
let mut x_candidates: [Point; 3] = [Point::new(0, 0); 3]; let mut x_candidates: [Point; 3] = [Point::new(0, 0); 3];
@ -289,12 +386,15 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
&& (y_src as i32 >= 0) && (y_src as i32 >= 0)
&& ((y_src as i32) < dims.y as i32) && ((y_src as i32) < dims.y as i32)
{ {
// println!("{},{} -> {},{}", x_src as i32, y_src as i32, x, // println!("{},{} -> {},{}", x_src as i32, y_src as i32,
// x,
// y); // y);
aligned[qr_corners.qr_pixels() * y as usize + x as usize] = aligned
[qr_corners.qr_pixels() * y as usize + x as usize] =
frame[IMAGE_WIDTH * y_src as usize + x_src as usize]; frame[IMAGE_WIDTH * y_src as usize + x_src as usize];
} else { } else {
aligned[qr_corners.qr_pixels() * y as usize + x as usize] = aligned
[qr_corners.qr_pixels() * y as usize + x as usize] =
255; 255;
} }
} }
@ -322,8 +422,8 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
frame.chunks_mut(IMAGE_WIDTH).zip(aligned.chunks(qr_width)) frame.chunks_mut(IMAGE_WIDTH).zip(aligned.chunks(qr_width))
{ {
// "center up" the QR in the middle by the estimated margin // "center up" the QR in the middle by the estimated margin
// this is just a perceptual trick to prevent users from shifting the position of // this is just a perceptual trick to prevent users from shifting the position
// the camera // of the camera
for (dst, &src) in dst_line[qr::HOMOGRAPHY_MARGIN.abs() as usize..] for (dst, &src) in dst_line[qr::HOMOGRAPHY_MARGIN.abs() as usize..]
.iter_mut() .iter_mut()
.zip(src_line.iter()) .zip(src_line.iter())
@ -331,7 +431,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
*dst = src; *dst = src;
} }
} }
blit_to_display(&mut sh1107, &frame, true); blit_to_display(&mut display, &frame, true);
// we now have a QR code in "canonical" orientation, with a // we now have a QR code in "canonical" orientation, with a
// known width in pixels // known width in pixels
@ -405,14 +505,14 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
log::info!("************ {} ***********", content); log::info!("************ {} ***********", content);
decode_success = true; decode_success = true;
gfx::msg( gfx::msg(
&mut sh1107, &mut display,
&format!("{:?}", meta), &format!("{:?}", meta),
Point::new(0, 0), Point::new(0, 0),
Mono::White.into(), Mono::White.into(),
Mono::Black.into(), Mono::Black.into(),
); );
gfx::msg( gfx::msg(
&mut sh1107, &mut display,
&format!("{:?}", content), &format!("{:?}", content),
Point::new(0, 64), Point::new(0, 64),
Mono::White.into(), Mono::White.into(),
@ -422,7 +522,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
Err(e) => { Err(e) => {
log::info!("{:?}", e); log::info!("{:?}", e);
gfx::msg( gfx::msg(
&mut sh1107, &mut display,
&format!("{:?}", e), &format!("{:?}", e),
Point::new(0, 0), Point::new(0, 0),
Mono::White.into(), Mono::White.into(),
@ -433,7 +533,7 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
} else { } else {
log::info!("Transformed image did not survive sanity check!"); log::info!("Transformed image did not survive sanity check!");
gfx::msg( gfx::msg(
&mut sh1107, &mut display,
"Hold device steady...", "Hold device steady...",
Point::new(0, 0), Point::new(0, 0),
Mono::White.into(), Mono::White.into(),
@ -441,17 +541,17 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
); );
} }
} else { } else {
blit_to_display(&mut sh1107, &frame, true); blit_to_display(&mut display, &frame, true);
for c in candidates_orig.iter() { for c in candidates_orig.iter() {
log::debug!("****** candidate: {}, {} ******", c.x, c.y); log::debug!("****** candidate: {}, {} ******", c.x, c.y);
// remap image to screen coordinates (it's 2:1) // remap image to screen coordinates (it's 2:1)
let c_screen = *c / 2; let c_screen = *c / 2;
// flip coordinates to match the camera data // flip coordinates to match the camera data
// c_screen = Point::new(c_screen.x, sh1107.dimensions().y - 1 - c_screen.y); // c_screen = Point::new(c_screen.x, display.dimensions().y - 1 - c_screen.y);
qr::draw_crosshair(&mut sh1107, c_screen); qr::draw_crosshair(&mut display, c_screen);
} }
gfx::msg( gfx::msg(
&mut sh1107, &mut display,
"Align the QR code...", "Align the QR code...",
Point::new(0, 0), Point::new(0, 0),
Mono::White.into(), Mono::White.into(),
@ -459,10 +559,10 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
); );
} }
} else { } else {
// blit raw camera fb to sh1107 // blit raw camera fb to display
blit_to_display(&mut sh1107, &frame, true); blit_to_display(&mut display, &frame, true);
gfx::msg( gfx::msg(
&mut sh1107, &mut display,
"Scan QR code...", "Scan QR code...",
Point::new(0, 0), Point::new(0, 0),
Mono::White.into(), Mono::White.into(),
@ -471,14 +571,14 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
} }
// swap the double buffer and update to the display // swap the double buffer and update to the display
sh1107.buffer_swap(); display.buffer_swap();
sh1107.draw(); display.draw();
if decode_success { if decode_success {
tt.sleep_ms(2000).ok(); tt.sleep_ms(2000).ok();
} }
// clear the front buffer // clear the front buffer
sh1107.clear(); display.clear();
// re-initiate the capture. This is done at the bottom of the loop because UDMA // re-initiate the capture. This is done at the bottom of the loop because UDMA
// congestion leads to system instability. When this problem is solved, we would // congestion leads to system instability. When this problem is solved, we would
@ -522,9 +622,426 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
} }
*/ */
} }
Opcode::InvalidCall => { GfxOpcode::InvalidCall => {
log::error!("Invalid call to bao video server: {:?}", msg); log::error!("Invalid call to bao video server: {:?}", msg);
} }
// ---- "regular" graphics API (almost a mirror of what is in graphics-server - maybe we could
// turn this even into a shared function?)
GfxOpcode::DrawClipObject => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let obj = buffer.to_original::<ClipObject, _>().unwrap();
log::trace!("DrawClipObject {:?}", obj);
match obj.obj {
ClipObjectType::Line(line) => {
op::line(&mut display, line, Some(obj.clip), false);
}
ClipObjectType::XorLine(line) => {
op::line(&mut display, line, Some(obj.clip), true);
}
ClipObjectType::Circ(circ) => {
op::circle(&mut display, circ, Some(obj.clip));
}
ClipObjectType::Rect(rect) => {
op::rectangle(&mut display, rect, Some(obj.clip), false);
}
ClipObjectType::RoundRect(rr) => {
op::rounded_rectangle(&mut display, rr, Some(obj.clip));
}
#[cfg(feature = "ditherpunk")]
ClipObjectType::Tile(tile) => {
op::tile(&mut display, tile, Some(obj.clip));
} }
} }
} }
GfxOpcode::DrawClipObjectList => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let list_ipc = buffer.to_original::<ClipObjectList, _>().unwrap();
for maybe_item in list_ipc.list.iter() {
if let Some(obj) = maybe_item {
match obj.obj {
ClipObjectType::Line(line) => {
op::line(&mut display, line, Some(obj.clip), false);
}
ClipObjectType::XorLine(line) => {
op::line(&mut display, line, Some(obj.clip), true);
}
ClipObjectType::Circ(circ) => {
op::circle(&mut display, circ, Some(obj.clip));
}
ClipObjectType::Rect(rect) => {
op::rectangle(&mut display, rect, Some(obj.clip), false);
}
ClipObjectType::RoundRect(rr) => {
op::rounded_rectangle(&mut display, rr, Some(obj.clip));
}
#[cfg(feature = "ditherpunk")]
ClipObjectType::Tile(tile) => {
op::tile(&mut display, 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
break;
}
}
}
GfxOpcode::DrawTextView => {
let mut buffer =
unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut tv = buffer.to_original::<TextView, _>().unwrap();
if tv.clip_rect.is_none() {
continue;
} // if no clipping rectangle is specified, nothing to draw
// this is the clipping rectangle of the canvas in screen coordinates
let clip_rect = tv.clip_rect.unwrap();
// this is the translation vector to and from screen space
let screen_offset: Point = tv.clip_rect.unwrap().tl;
let typeset_extent = match tv.bounds_hint {
TextBounds::BoundingBox(r) => Point::new(
r.br().x - r.tl().x - tv.margin.x * 2,
r.br().y - r.tl().y - tv.margin.y * 2,
),
TextBounds::GrowableFromBr(br, width) => {
Point::new(width as isize - tv.margin.x * 2, br.y - tv.margin.y * 2)
}
TextBounds::GrowableFromBl(bl, width) => {
Point::new(width as isize - tv.margin.x * 2, bl.y - tv.margin.y * 2)
}
TextBounds::GrowableFromTl(tl, width) => Point::new(
width as isize - tv.margin.x * 2,
(clip_rect.br().y - clip_rect.tl().y - tl.y) - tv.margin.y * 2,
),
TextBounds::GrowableFromTr(tr, width) => Point::new(
width as isize - tv.margin.x * 2,
(clip_rect.br().y - clip_rect.tl().y - tr.y) - tv.margin.y * 2,
),
TextBounds::CenteredTop(r) => Point::new(
r.br().x - r.tl().x - tv.margin.x * 2,
r.br().y - r.tl().y - tv.margin.y * 2,
),
TextBounds::CenteredBot(r) => Point::new(
r.br().x - r.tl().x - tv.margin.x * 2,
r.br().y - r.tl().y - tv.margin.y * 2,
),
};
let mut typesetter = Typesetter::setup(
tv.to_str(),
&typeset_extent,
&tv.style,
if let Some(i) = tv.insertion { Some(i as usize) } else { None },
);
let composition = typesetter.typeset(if tv.ellipsis {
OverflowStrategy::Ellipsis
} else {
OverflowStrategy::Abort
});
let composition_top_left = match tv.bounds_hint {
TextBounds::BoundingBox(r) => r.tl().add(tv.margin),
TextBounds::GrowableFromBr(br, _width) => Point::new(
br.x - (composition.bb_width() as isize + tv.margin.x),
br.y - (composition.bb_height() as isize + tv.margin.y),
),
TextBounds::GrowableFromBl(bl, _width) => Point::new(
bl.x + tv.margin.x,
bl.y - (composition.bb_height() as isize + tv.margin.y),
),
TextBounds::GrowableFromTl(tl, _width) => tl.add(tv.margin),
TextBounds::GrowableFromTr(tr, _width) => Point::new(
tr.x - (composition.bb_width() as isize + tv.margin.x),
tr.y + tv.margin.y,
),
TextBounds::CenteredTop(r) => {
if r.width() as isize > composition.bb_width() {
r.tl().add(Point::new((r.width() as isize - composition.bb_width()) / 2, 0))
} else {
r.tl().add(tv.margin)
}
}
TextBounds::CenteredBot(r) => {
if r.width() as isize > composition.bb_width() {
r.tl().add(Point::new(
(r.width() as isize - composition.bb_width()) / 2,
if (r.height() as isize) > (composition.bb_height() + tv.margin.y) {
(r.height() as isize) - (composition.bb_height() + tv.margin.y)
} else {
0
},
))
} else {
r.tl().add(tv.margin)
}
}
}
.add(screen_offset);
// compute the clear rectangle -- the border is already in screen coordinates, just add
// the margin around it
let mut clear_rect = match tv.bounds_hint {
TextBounds::BoundingBox(mut r) => {
r.translate(screen_offset);
r
}
_ => {
if tv.busy_animation_state.is_some() {
// we want to clear the entire potentially drawable region, not just the dirty
// box if we're doing a busy animation.
let r = Rectangle::new(
Point::new(screen_offset.x, composition_top_left.y),
Point::new(screen_offset.x, composition_top_left.y)
.add(Point::new(typeset_extent.x, composition.bb_height())),
);
r
} else {
// composition_top_left already had a screen_offset added when it was
// computed. just margin it out
let mut r = Rectangle::new(
composition_top_left,
composition_top_left.add(Point::new(
composition.bb_width() as _,
composition.bb_height() as _,
)),
);
r.margin_out(tv.margin);
r
}
}
};
log::trace!("clip_rect: {:?}", clip_rect);
log::trace!("composition_top_left: {:?}", composition_top_left);
log::trace!("clear_rect: {:?}", clear_rect);
// draw the bubble/border and/or clear the background area
let bordercolor = if tv.draw_border { Some(PixelColor::Dark) } else { None };
let borderwidth: isize = if tv.draw_border { tv.border_width as isize } else { 0 };
let fillcolor = if tv.clear_area || tv.invert {
if tv.invert { Some(PixelColor::Dark) } else { Some(PixelColor::Light) }
} else {
None
};
clear_rect.style = DrawStyle {
fill_color: fillcolor,
stroke_color: bordercolor,
stroke_width: borderwidth,
};
if !tv.dry_run() {
if tv.rounded_border.is_some() {
op::rounded_rectangle(
&mut display,
RoundedRectangle::new(clear_rect, tv.rounded_border.unwrap() as _),
tv.clip_rect,
);
} else {
op::rectangle(&mut display, clear_rect, tv.clip_rect, false);
}
}
// for now, if we're in braille mode, emit all text to the debug log so we can see it
//if cfg!(feature = "braille") {
// log::info!("{}", tv);
//}
if !tv.dry_run() {
// note: make the clip rect `tv.clip_rect.unwrap()` if you want to debug wordwrapping
// artifacts; otherwise smallest_rect masks some problems
let smallest_rect = clear_rect
.clip_with(tv.clip_rect.unwrap())
.unwrap_or(Rectangle::new(Point::new(0, 0), Point::new(0, 0)));
composition.render(&mut display, composition_top_left, tv.invert, smallest_rect);
}
// run the busy animation
if let Some(state) = tv.busy_animation_state {
let total_width = typeset_extent.x as isize;
if total_width > op::BUSY_ANIMATION_RECT_WIDTH * 2 {
let step = state as isize % (op::BUSY_ANIMATION_RECT_WIDTH * 2);
for offset in (0..(total_width + op::BUSY_ANIMATION_RECT_WIDTH * 2))
.step_by((op::BUSY_ANIMATION_RECT_WIDTH * 2) as usize)
{
let left_x = offset + step + composition_top_left.x;
if offset == 0
&& (step >= op::BUSY_ANIMATION_RECT_WIDTH)
&& (step < op::BUSY_ANIMATION_RECT_WIDTH * 2)
{
// handle the truncated "left" rectangle
let mut trunc_rect = Rectangle::new(
Point::new(composition_top_left.x as isize, clear_rect.tl().y),
Point::new(
(step + composition_top_left.x - op::BUSY_ANIMATION_RECT_WIDTH)
as isize,
clear_rect.br().y,
),
);
trunc_rect.style = DrawStyle {
fill_color: Some(PixelColor::Light),
stroke_color: None,
stroke_width: 0,
};
op::rectangle(&mut display, trunc_rect, tv.clip_rect, true);
} // the "right" rectangle is handled by the clipping mask
let mut xor_rect = Rectangle::new(
Point::new(left_x as isize, clear_rect.tl().y),
Point::new(
(left_x + op::BUSY_ANIMATION_RECT_WIDTH) as isize,
clear_rect.br().y,
),
);
xor_rect.style = DrawStyle {
fill_color: Some(PixelColor::Light),
stroke_color: None,
stroke_width: 0,
};
op::rectangle(&mut display, xor_rect, tv.clip_rect, true);
}
} else {
// don't do the animation, this could be abused to create inverted text
}
tv.busy_animation_state = Some(state + 2);
}
// type mismatch for now, replace this with a simple equals once we sort that out
tv.cursor.pt.x = composition.final_cursor().pt.x;
tv.cursor.pt.y = composition.final_cursor().pt.y;
tv.cursor.line_height = composition.final_cursor().line_height;
tv.overflow = Some(composition.final_overflow());
tv.bounds_computed = Some(clear_rect);
log::trace!("cursor ret {:?}, bounds ret {:?}", tv.cursor, tv.bounds_computed);
// pack our data back into the buffer to return
buffer.replace(tv).unwrap();
}
GfxOpcode::Flush => {
log::trace!("***gfx flush*** redraw##");
display.redraw();
}
GfxOpcode::Clear => {
let mut r = Rectangle::full_screen();
r.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 0);
op::rectangle(&mut display, r, screen_clip.into(), false)
}
GfxOpcode::Line => msg_scalar_unpack!(msg, p1, p2, style, _, {
let l = Line::new_with_style(Point::from(p1), Point::from(p2), DrawStyle::from(style));
op::line(&mut display, l, screen_clip.into(), false);
}),
GfxOpcode::Rectangle => msg_scalar_unpack!(msg, tl, br, style, _, {
let r =
Rectangle::new_with_style(Point::from(tl), Point::from(br), DrawStyle::from(style));
op::rectangle(&mut display, r, screen_clip.into(), false);
}),
GfxOpcode::RoundedRectangle => msg_scalar_unpack!(msg, tl, br, style, r, {
let rr = RoundedRectangle::new(
Rectangle::new_with_style(Point::from(tl), Point::from(br), DrawStyle::from(style)),
r as _,
);
op::rounded_rectangle(&mut display, rr, screen_clip.into());
}),
GfxOpcode::Circle => msg_scalar_unpack!(msg, center, radius, style, _, {
let c = Circle::new_with_style(Point::from(center), radius as _, DrawStyle::from(style));
op::circle(&mut display, c, screen_clip.into());
}),
GfxOpcode::ScreenSize => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
let pt = display.screen_size();
xous::return_scalar2(msg.sender, pt.x as usize, pt.y as usize)
.expect("couldn't return ScreenSize request");
}),
GfxOpcode::QueryGlyphProps => msg_blocking_scalar_unpack!(msg, style, _, _, _, {
let glyph = GlyphStyle::from(style);
xous::return_scalar2(msg.sender, glyph.into(), glyph_to_height_hint(glyph))
.expect("could not return QueryGlyphProps request");
}),
GfxOpcode::DrawSleepScreen => msg_scalar_unpack!(msg, _, _, _, _, {
display.blit_screen(&ux_api::bitmaps::baochip128x128::BITMAP);
display.redraw();
}),
GfxOpcode::DrawBootLogo => msg_scalar_unpack!(msg, _, _, _, _, {
display.blit_screen(&ux_api::bitmaps::baochip128x128::BITMAP);
display.redraw();
}),
GfxOpcode::Devboot => msg_scalar_unpack!(msg, ena, _, _, _, {
if ena != 0 {
display.set_devboot(true);
} else {
display.set_devboot(false);
}
}),
GfxOpcode::RestartBulkRead => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
bulkread.from_offset = 0;
xous::return_scalar(msg.sender, 0)
.expect("couldn't ack that bulk read pointer was reset");
}),
GfxOpcode::BulkReadFonts => {
// this also needs to reflect in root-keys/src/implementation.rs @ sign_loader()
let fontlen = fontmap::FONT_TOTAL_LEN as u32
+ 16 // minver
+ 16 // current ver
+ 8; // sig ver + len
let mut buf =
unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
//let mut bulkread = buf.as_flat::<BulkRead, _>().unwrap(); // try to skip the copy/init
// step by using a persistent structure Safety: `u8` contains no
// undefined values
let fontslice = unsafe { fontregion.as_slice::<u8>() };
assert!(fontlen <= fontslice.len() as u32);
if bulkread.from_offset >= fontlen {
log::error!("BulkReadFonts attempt to read out of bound on the font area; ignoring!");
continue;
}
let readlen = if bulkread.from_offset + bulkread.buf.len() as u32 > fontlen {
// returns what is readable of the last bit; anything longer than the fontlen is
// undefined/invalid
fontlen as usize - bulkread.from_offset as usize
} else {
bulkread.buf.len()
};
for (&src, dst) in fontslice
[bulkread.from_offset as usize..bulkread.from_offset as usize + readlen]
.iter()
.zip(bulkread.buf.iter_mut())
{
*dst = src;
}
bulkread.len = readlen as u32;
bulkread.from_offset += readlen as u32;
buf.replace(bulkread).unwrap();
}
GfxOpcode::TestPattern => msg_blocking_scalar_unpack!(msg, duration, _, _, _, {
todo!("Need to write this for factory testing");
}),
GfxOpcode::Stash => {
display.stash();
match msg.body {
// ack the message if it's a blocking scalar
xous::Message::BlockingScalar(_) => xous::return_scalar(msg.sender, 1).unwrap(),
_ => (),
}
}
GfxOpcode::Pop => {
display.pop();
match msg.body {
// ack the message if it's a blocking scalar
xous::Message::BlockingScalar(_) => xous::return_scalar(msg.sender, 1).unwrap(),
_ => (),
}
}
GfxOpcode::Quit => break,
_ => {
// This is perfectly normal because not all opcodes are handled by all platforms.
log::debug!("Invalid or unhandled opcode: {:?}", opcode);
}
}
} else {
// just idle while the panic handler does its thing
tt.sleep_ms(10_000).unwrap();
}
}
log::trace!("main loop exit, destroying servers");
xns.unregister_server(sid).unwrap();
xous::destroy_server(sid).unwrap();
log::trace!("quitting");
xous::terminate_process(0)
}