refactor graphics server to use reply_and_receive_next()

This new api allows for scalar-5 returns as well as more
efficient use of the scheduler's quantum by not simply yielding
time when a message is returned; it will fetch the next message
and process it until its time is up.
This commit is contained in:
bunnie 2025-02-26 14:35:33 +08:00
parent 79f803ef58
commit 111d8f8d3d

View file

@ -19,8 +19,6 @@ use poweron_bt as poweron;
mod sleep_note; mod sleep_note;
use blitstr2::fontmap; use blitstr2::fontmap;
use num_traits::FromPrimitive;
use xous::{MemoryRange, msg_blocking_scalar_unpack, msg_scalar_unpack};
use xous_ipc::Buffer; use xous_ipc::Buffer;
#[cfg(any(feature = "precursor", feature = "renode", feature = "cramium-soc"))] #[cfg(any(feature = "precursor", feature = "renode", feature = "cramium-soc"))]
@ -122,7 +120,7 @@ fn map_fonts() -> MemoryRange {
} }
#[cfg(not(target_os = "xous"))] #[cfg(not(target_os = "xous"))]
fn map_fonts() -> MemoryRange { fn map_fonts() -> xous::MemoryRange {
// does nothing // does nothing
let fontlen: u32 = ((fontmap::FONT_TOTAL_LEN as u32 + 8) & 0xFFFF_F000) + 0x1000; 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) let fontregion = xous::syscall::map_memory(None, None, fontlen as usize, xous::MemoryFlags::R)
@ -213,82 +211,108 @@ fn wrapped_main(main_thread_token: backend::MainThreadToken) -> ! {
#[cfg(feature = "gfx-testing")] #[cfg(feature = "gfx-testing")]
testing::tests(); testing::tests();
let mut msg_opt = None;
loop { loop {
if !is_panic.load(Ordering::Relaxed) { if !is_panic.load(Ordering::Relaxed) {
// non-panic graphics operations if we are in a panic situation xous::reply_and_receive_next(sid, &mut msg_opt).unwrap();
let mut msg = xous::receive_message(sid).unwrap(); let msg = msg_opt.as_mut().unwrap();
let op = FromPrimitive::from_usize(msg.body.id()); let op = num_traits::FromPrimitive::from_usize(msg.body.id()).unwrap_or(GfxOpcode::InvalidCall);
log::trace!("{:?}", op); log::debug!("{:?}", op);
match op { match op {
#[cfg(not(feature = "cramium-soc"))] #[cfg(not(feature = "cramium-soc"))]
Some(GfxOpcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, { GfxOpcode::SuspendResume => {
display.suspend(); if let Some(scalar) = msg.body.scalar_message() {
susres.suspend_until_resume(token).expect("couldn't execute suspend/resume"); let token = scalar.arg1;
display.resume(); display.suspend();
}), susres.suspend_until_resume(token).expect("couldn't execute suspend/resume");
Some(GfxOpcode::DrawClipObject) => { display.resume();
minigfx::handlers::draw_clip_object(&mut display, &mut msg); } else {
panic!("Incorrect message type");
}
} }
Some(GfxOpcode::DrawClipObjectList) => { GfxOpcode::DrawClipObject => {
minigfx::handlers::draw_clip_object_list(&mut display, &mut msg); minigfx::handlers::draw_clip_object(&mut display, msg);
} }
Some(GfxOpcode::DrawTextView) => { GfxOpcode::DrawClipObjectList => {
minigfx::handlers::draw_text_view(&mut display, &mut msg); minigfx::handlers::draw_clip_object_list(&mut display, msg);
} }
Some(GfxOpcode::Flush) => { GfxOpcode::DrawTextView => {
minigfx::handlers::draw_text_view(&mut display, msg);
}
GfxOpcode::Flush => {
log::trace!("***gfx flush*** redraw##"); log::trace!("***gfx flush*** redraw##");
display.redraw(); display.redraw();
} }
Some(GfxOpcode::Clear) => { GfxOpcode::Clear => {
let mut r = Rectangle::full_screen(); let mut r = Rectangle::full_screen();
r.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 0); r.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 0);
op::rectangle(&mut display, r, screen_clip.into(), false) op::rectangle(&mut display, r, screen_clip.into(), false)
} }
Some(GfxOpcode::Line) => { GfxOpcode::Line => {
minigfx::handlers::line(&mut display, screen_clip.into(), &mut msg); minigfx::handlers::line(&mut display, screen_clip.into(), msg);
} }
Some(GfxOpcode::Rectangle) => { GfxOpcode::Rectangle => {
minigfx::handlers::rectangle(&mut display, screen_clip.into(), &mut msg); minigfx::handlers::rectangle(&mut display, screen_clip.into(), msg);
} }
Some(GfxOpcode::RoundedRectangle) => { GfxOpcode::RoundedRectangle => {
minigfx::handlers::rounded_rectangle(&mut display, screen_clip.into(), &mut msg); minigfx::handlers::rounded_rectangle(&mut display, screen_clip.into(), msg);
} }
#[cfg(feature = "ditherpunk")] #[cfg(feature = "ditherpunk")]
Some(GfxOpcode::Tile) => { GfxOpcode::Tile => {
minigfx::handlers::tile(&mut display, screen_clip.into(), &mut msg); minigfx::handlers::tile(&mut display, screen_clip.into(), msg);
} }
Some(GfxOpcode::Circle) => { GfxOpcode::Circle => {
minigfx::handlers::circle(&mut display, screen_clip.into(), &mut msg); minigfx::handlers::circle(&mut display, screen_clip.into(), msg);
} }
Some(GfxOpcode::ScreenSize) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { GfxOpcode::ScreenSize => {
let pt = display.screen_size(); if let Some(scalar) = msg.body.scalar_message_mut() {
xous::return_scalar2(msg.sender, pt.x as usize, pt.y as usize) let pt = display.screen_size();
.expect("couldn't return ScreenSize request"); scalar.arg1 = pt.x as usize;
}), scalar.arg2 = pt.y as usize;
Some(GfxOpcode::QueryGlyphProps) => {
minigfx::handlers::query_glyph_props(&mut msg);
}
Some(GfxOpcode::DrawSleepScreen) => msg_scalar_unpack!(msg, _, _, _, _, {
display.blit_screen(&logo::LOGO_MAP);
display.redraw();
}),
Some(GfxOpcode::DrawBootLogo) => msg_scalar_unpack!(msg, _, _, _, _, {
display.blit_screen(&poweron::LOGO_MAP);
display.redraw();
}),
Some(GfxOpcode::Devboot) => msg_scalar_unpack!(msg, ena, _, _, _, {
if ena != 0 {
display.set_devboot(true);
} else { } else {
display.set_devboot(false); panic!("Incorrect message type");
} }
}), }
Some(GfxOpcode::RestartBulkRead) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { GfxOpcode::QueryGlyphProps => {
bulkread.from_offset = 0; minigfx::handlers::query_glyph_props(msg);
xous::return_scalar(msg.sender, 0) }
.expect("couldn't ack that bulk read pointer was reset"); GfxOpcode::DrawSleepScreen => {
}), if let Some(_scalar) = msg.body.scalar_message() {
Some(GfxOpcode::BulkReadFonts) => { display.blit_screen(&logo::LOGO_MAP);
display.redraw();
} else {
panic!("Incorrect message type");
}
}
GfxOpcode::DrawBootLogo => {
if let Some(_scalar) = msg.body.scalar_message() {
display.blit_screen(&poweron::LOGO_MAP);
display.redraw();
} else {
panic!("Incorrect message type");
}
}
GfxOpcode::Devboot => {
if let Some(scalar) = msg.body.scalar_message() {
let ena = scalar.arg1;
if ena != 0 {
display.set_devboot(true);
} else {
display.set_devboot(false);
}
} else {
panic!("Incorrect message type");
}
}
GfxOpcode::RestartBulkRead => {
if let Some(scalar) = msg.body.scalar_message_mut() {
bulkread.from_offset = 0;
scalar.arg1 = 0;
} else {
panic!("Incorrect message type");
}
}
GfxOpcode::BulkReadFonts => {
// this also needs to reflect in root-keys/src/implementation.rs @ sign_loader() // this also needs to reflect in root-keys/src/implementation.rs @ sign_loader()
let fontlen = fontmap::FONT_TOTAL_LEN as u32 let fontlen = fontmap::FONT_TOTAL_LEN as u32
+ 16 // minver + 16 // minver
@ -323,127 +347,132 @@ fn wrapped_main(main_thread_token: backend::MainThreadToken) -> ! {
bulkread.from_offset += readlen as u32; bulkread.from_offset += readlen as u32;
buf.replace(bulkread).unwrap(); buf.replace(bulkread).unwrap();
} }
Some(GfxOpcode::TestPattern) => msg_blocking_scalar_unpack!(msg, duration, _, _, _, { GfxOpcode::TestPattern => {
let mut stashmem = xous::syscall::map_memory( if let Some(scalar) = msg.body.scalar_message_mut() {
None, let duration = scalar.arg1;
None, let mut stashmem = xous::syscall::map_memory(
((FB_SIZE * 4) + 4096) & !4095, None,
xous::MemoryFlags::R | xous::MemoryFlags::W, None,
) ((FB_SIZE * 4) + 4096) & !4095,
.expect("couldn't map stash frame buffer"); xous::MemoryFlags::R | xous::MemoryFlags::W,
// Safety: `u8` contains no undefined values )
let stash = unsafe { &mut stashmem.as_slice_mut()[..FB_SIZE] }; .expect("couldn't map stash frame buffer");
for (&src, dst) in display.as_slice().iter().zip(stash.iter_mut()) { // Safety: `u8` contains no undefined values
*dst = src; let stash = unsafe { &mut stashmem.as_slice_mut()[..FB_SIZE] };
} for (&src, dst) in display.as_slice().iter().zip(stash.iter_mut()) {
for lines in 0..FB_LINES { *dst = src;
// mark all lines dirty
stash[lines * FB_WIDTH_WORDS + (FB_WIDTH_WORDS - 1)] |= 0x1_0000;
}
let start_time = ticktimer.elapsed_ms();
let mut testmem = xous::syscall::map_memory(
None,
None,
((FB_SIZE * 4) + 4096) & !4095,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map stash frame buffer");
// Safety: `u8` contains no undefined values
let testpat = unsafe { &mut testmem.as_slice_mut()[..FB_SIZE] };
const DWELL: usize = 1000;
while ticktimer.elapsed_ms() - start_time < duration as u64 {
// all black
for w in testpat.iter_mut() {
*w = 0;
} }
for lines in 0..FB_LINES { for lines in 0..FB_LINES {
// mark dirty bits // mark all lines dirty
testpat[lines * FB_WIDTH_WORDS + (FB_WIDTH_WORDS - 1)] |= 0x1_0000; stash[lines * FB_WIDTH_WORDS + (FB_WIDTH_WORDS - 1)] |= 0x1_0000;
} }
display.blit_screen(testpat);
display.redraw();
ticktimer.sleep_ms(DWELL).unwrap();
// all white let start_time = ticktimer.elapsed_ms();
for w in testpat.iter_mut() { let mut testmem = xous::syscall::map_memory(
*w = 0xFFFF_FFFF; None,
} None,
// dirty bits already set ((FB_SIZE * 4) + 4096) & !4095,
display.blit_screen(testpat); xous::MemoryFlags::R | xous::MemoryFlags::W,
display.redraw(); )
ticktimer.sleep_ms(DWELL).unwrap(); .expect("couldn't map stash frame buffer");
// Safety: `u8` contains no undefined values
// vertical bars let testpat = unsafe { &mut testmem.as_slice_mut()[..FB_SIZE] };
for lines in 0..FB_LINES { const DWELL: usize = 1000;
for words in 0..FB_WIDTH_WORDS { while ticktimer.elapsed_ms() - start_time < duration as u64 {
testpat[lines * FB_WIDTH_WORDS + words] = 0xaaaa_aaaa; // all black
for w in testpat.iter_mut() {
*w = 0;
} }
} for lines in 0..FB_LINES {
display.blit_screen(testpat); // mark dirty bits
display.redraw(); testpat[lines * FB_WIDTH_WORDS + (FB_WIDTH_WORDS - 1)] |= 0x1_0000;
ticktimer.sleep_ms(DWELL).unwrap();
for lines in 0..FB_LINES {
for words in 0..FB_WIDTH_WORDS {
testpat[lines * FB_WIDTH_WORDS + words] = 0x5555_5555;
} }
} display.blit_screen(testpat);
display.blit_screen(testpat); display.redraw();
display.redraw(); ticktimer.sleep_ms(DWELL).unwrap();
ticktimer.sleep_ms(DWELL).unwrap();
// horiz bars // all white
for lines in 0..FB_LINES { for w in testpat.iter_mut() {
for words in 0..FB_WIDTH_WORDS { *w = 0xFFFF_FFFF;
if lines % 2 == 0 { }
testpat[lines * FB_WIDTH_WORDS + words] = 0x0; // dirty bits already set
} else { display.blit_screen(testpat);
testpat[lines * FB_WIDTH_WORDS + words] = 0xffff_ffff; display.redraw();
ticktimer.sleep_ms(DWELL).unwrap();
// vertical bars
for lines in 0..FB_LINES {
for words in 0..FB_WIDTH_WORDS {
testpat[lines * FB_WIDTH_WORDS + words] = 0xaaaa_aaaa;
} }
} }
testpat[lines * FB_WIDTH_WORDS + (FB_WIDTH_WORDS - 1)] |= 0x1_0000; display.blit_screen(testpat);
} display.redraw();
display.blit_screen(testpat); ticktimer.sleep_ms(DWELL).unwrap();
display.redraw();
ticktimer.sleep_ms(DWELL).unwrap();
for lines in 0..FB_LINES { for lines in 0..FB_LINES {
for words in 0..FB_WIDTH_WORDS { for words in 0..FB_WIDTH_WORDS {
if lines % 2 == 1 { testpat[lines * FB_WIDTH_WORDS + words] = 0x5555_5555;
testpat[lines * FB_WIDTH_WORDS + words] = 0x0;
} else {
testpat[lines * FB_WIDTH_WORDS + words] = 0xffff_ffff;
} }
} }
testpat[lines * FB_WIDTH_WORDS + (FB_WIDTH_WORDS - 1)] |= 0x1_0000; display.blit_screen(testpat);
} display.redraw();
display.blit_screen(testpat); ticktimer.sleep_ms(DWELL).unwrap();
display.redraw();
ticktimer.sleep_ms(DWELL).unwrap();
}
display.blit_screen(stash);
xous::return_scalar(msg.sender, duration).expect("couldn't ack test pattern"); // horiz bars
}), for lines in 0..FB_LINES {
Some(GfxOpcode::Stash) => { for words in 0..FB_WIDTH_WORDS {
if lines % 2 == 0 {
testpat[lines * FB_WIDTH_WORDS + words] = 0x0;
} else {
testpat[lines * FB_WIDTH_WORDS + words] = 0xffff_ffff;
}
}
testpat[lines * FB_WIDTH_WORDS + (FB_WIDTH_WORDS - 1)] |= 0x1_0000;
}
display.blit_screen(testpat);
display.redraw();
ticktimer.sleep_ms(DWELL).unwrap();
for lines in 0..FB_LINES {
for words in 0..FB_WIDTH_WORDS {
if lines % 2 == 1 {
testpat[lines * FB_WIDTH_WORDS + words] = 0x0;
} else {
testpat[lines * FB_WIDTH_WORDS + words] = 0xffff_ffff;
}
}
testpat[lines * FB_WIDTH_WORDS + (FB_WIDTH_WORDS - 1)] |= 0x1_0000;
}
display.blit_screen(testpat);
display.redraw();
ticktimer.sleep_ms(DWELL).unwrap();
}
display.blit_screen(stash);
scalar.arg1 = duration;
} else {
panic!("Incorrect message type");
}
}
GfxOpcode::Stash => {
display.stash(); display.stash();
match msg.body { if let Some(scalar) = msg.body.scalar_message_mut() {
// ack the message if it's a blocking scalar // ack the message if it's a blocking scalar
xous::Message::BlockingScalar(_) => xous::return_scalar(msg.sender, 1).unwrap(), scalar.arg1 = 1;
_ => (),
} }
// no failure if it's not
} }
Some(GfxOpcode::Pop) => { GfxOpcode::Pop => {
display.pop(); display.pop();
match msg.body { if let Some(scalar) = msg.body.scalar_message_mut() {
// ack the message if it's a blocking scalar // ack the message if it's a blocking scalar
xous::Message::BlockingScalar(_) => xous::return_scalar(msg.sender, 1).unwrap(), scalar.arg1 = 1;
_ => (),
} }
// no failure if it's not
} }
Some(GfxOpcode::Quit) => break, GfxOpcode::Quit => break,
None => { GfxOpcode::InvalidCall => {
log::error!("Received invalid GfxOpcode. Ignoring."); log::error!("Received invalid GfxOpcode. Ignoring.");
} }
_ => { _ => {