add stash/pop of FB; fix suspend/resume buffering

The `ManagedMem` thing for the suspend/resume crate is simply
a disaster. It tries to be fancy and Rusty and really it just fails
in non-obvious ways. This is one of the places where it simply
does not pay to make things seem so simple.

Why? because in reality, memory is complicated. It comes in pages.
There's initialization. There's caching. There's volatility.
I haven't encountered a single situation where the assumptions
of page-alignment and memory layout have really lined up well enough
for this primitive to make sense. The "idea" was hey, since
you allocate a peripheral MemoryRegion, why not just use its attributes
as a marker to a suspend/resume thing that automatically handled
stashing that MemoryRegion to a backing store inside RAM? Seems
simple right? wrong. Every time I've tried to use it, it's failed
in non-obvious ways.

In this most recent incarnation, it turns out that for whatever
reason, it seems to be just stashing about the first 4-8k of the
framebuffer to the SR backup region, and then somehow grabbing
a future(?) copy of the memory? or maybe just a wrong copy? I don't
know. The code seems simple enough but it goes through some
traits and some type transformations and then there's some unsafe
code to make it volatile and it doesn't work.

Instead, we just use a damn [u32; FB_SIZE] slice to store the
frame buffer. This is so obvious and easy. The only price is
you have an "ugly" `fb[..FB_SIZE].copy_from_slice(&self.srfb)`
line instead of an "elegant" `srfb.suspend()` invocation.
Stashing into it is a bit uglier, because we have to cast the
slice to an unsafe pointer in order to access the `write_volatile()`
method. But, it works! It. Just. Works. And you can see what's going
on. Really, I should stop trying to be fancy and just be readable.

OK anyways, that was that rant. This commit also adds some
calls that allow us to re-use the suspend/resume frame buffer during
"normal" times as a stash/pop for the application image. This is
nice because the memory is normally idle/overhead when not
in suspend, so now we're repurposing some already-allocated memory
to accelerate some drawing operations, and it also makes, crucially,
a multi-step blocking modal dialog interaction less cosmetically
jarring, because the application thread can be fully blocked and
not responding to redraw commands but the stash/pop mechanism
creates the illusion of "everything is moving along".
This commit is contained in:
bunnie 2022-07-05 02:44:13 +08:00
parent 3ad1ae7325
commit 147ec8594c
5 changed files with 90 additions and 9 deletions

View file

@ -108,6 +108,11 @@ pub(crate) enum Opcode {
BulkReadFonts,
RestartBulkRead,
/// sling the framebuffer into and out of the suspend/resume area, abusing this
/// to help accelerate redraws between modal swaps.
Stash,
Pop,
/// generates a test pattern
TestPattern,

View file

@ -1,6 +1,6 @@
use crate::api::Point;
use crate::api::{LINES, WIDTH};
use susres::{ManagedMem, RegManager, RegOrField, SuspendResume};
use susres::{RegManager, RegOrField, SuspendResume};
use utralib::generated::*;
use xous::MemoryRange;
@ -13,7 +13,7 @@ const CONFIG_CLOCK_FREQUENCY: u32 = 100_000_000;
pub struct XousDisplay {
fb: MemoryRange,
hwfb: MemoryRange,
srfb: ManagedMem<{ utralib::generated::HW_MEMLCD_MEM_LEN / core::mem::size_of::<u32>() }>,
srfb: [u32; FB_SIZE],
csr: utralib::CSR<u32>,
susres: RegManager<{ utra::memlcd::MEMLCD_NUMREGS }>,
}
@ -61,7 +61,7 @@ impl XousDisplay {
hwfb,
csr: CSR::new(control.as_mut_ptr() as *mut u32),
susres: RegManager::new(control.as_mut_ptr() as *mut u32),
srfb: ManagedMem::new(fb),
srfb: [0u32; FB_SIZE],
};
display.set_clock(CONFIG_CLOCK_FREQUENCY);
@ -101,18 +101,36 @@ impl XousDisplay {
)
}
pub fn stash(&mut self) {
let fb = unsafe{core::slice::from_raw_parts_mut(self.fb.as_mut_ptr() as *mut u32, FB_SIZE)};
for lines in 0..FB_LINES {
// set the dirty bits prior to stashing the frame buffer
fb[lines * FB_WIDTH_WORDS + (FB_WIDTH_WORDS - 1)] |= 0x1_0000;
}
let srfb_ptr = self.srfb.as_mut_ptr() as *mut u32;
for (index, &src) in fb.iter().enumerate() {
unsafe{srfb_ptr.add(index).write_volatile(src)};
}
}
pub fn pop(&mut self) {
let fb: &mut [u32] = self.fb.as_slice_mut();
fb[..FB_SIZE].copy_from_slice(&self.srfb);
self.redraw();
}
pub fn suspend(&mut self) {
while self.busy() {
// just wait until any pending FB operations are done
}
let fb: *mut [u32; FB_SIZE] = self.fb.as_mut_ptr() as *mut [u32; FB_SIZE];
let fb = unsafe{core::slice::from_raw_parts_mut(self.fb.as_mut_ptr() as *mut u32, FB_SIZE)};
for lines in 0..FB_LINES {
// set the dirty bits prior to stashing the frame buffer
unsafe {
(*fb)[lines * FB_WIDTH_WORDS + (FB_WIDTH_WORDS - 1)] |= 0x1_0000;
}
fb[lines * FB_WIDTH_WORDS + (FB_WIDTH_WORDS - 1)] |= 0x1_0000;
}
let srfb_ptr = self.srfb.as_mut_ptr() as *mut u32;
for (index, &src) in fb.iter().enumerate() {
unsafe{srfb_ptr.add(index).write_volatile(src)};
}
self.srfb.suspend();
self.susres.suspend();
let note = crate::sleep_note::LOGO_MAP;
@ -154,7 +172,8 @@ impl XousDisplay {
}
pub fn resume(&mut self) {
self.susres.resume();
self.srfb.resume();
let fb: &mut [u32] = self.fb.as_slice_mut();
fb[..FB_SIZE].copy_from_slice(&self.srfb);
self.redraw();
}

View file

@ -20,6 +20,7 @@ const LIGHT_COLOUR: u32 = 0x1B1B19;
pub struct XousDisplay {
native_buffer: Vec<u32>, //[u32; WIDTH * HEIGHT],
emulated_buffer: [u32; FB_SIZE],
srfb: [u32; FB_SIZE],
window: Window,
devboot: bool,
}
@ -70,6 +71,7 @@ impl XousDisplay {
native_buffer,
window,
emulated_buffer: [0u32; FB_SIZE],
srfb: [0u32; FB_SIZE],
devboot: true,
}
}
@ -82,6 +84,15 @@ impl XousDisplay {
pub fn suspend(&self) {}
pub fn resume(&self) {}
pub fn stash(&mut self) {
self.srfb.copy_from_slice(&self.emulated_buffer);
}
pub fn pop(&mut self) {
self.emulated_buffer.copy_from_slice(&self.srfb);
self.redraw();
self.update();
}
pub fn screen_size(&self) -> Point {
Point::new(WIDTH as i16, HEIGHT as i16)
}

View file

@ -268,6 +268,38 @@ impl Gfx {
)
.expect("couldn't self test");
}
pub fn stash(&self, blocking: bool) {
if blocking {
send_message(
self.conn,
Message::new_blocking_scalar(Opcode::Stash.to_usize().unwrap(), 0, 0, 0, 0)
)
.expect("couldn't stash");
} else {
send_message(
self.conn,
Message::new_scalar(Opcode::Stash.to_usize().unwrap(), 0, 0, 0, 0)
)
.expect("couldn't stash");
}
}
pub fn pop(&self, blocking: bool) {
if blocking {
send_message(
self.conn,
Message::new_blocking_scalar(Opcode::Pop.to_usize().unwrap(), 0, 0, 0, 0)
)
.expect("couldn't stash");
} else {
send_message(
self.conn,
Message::new_scalar(Opcode::Pop.to_usize().unwrap(), 0, 0, 0, 0)
)
.expect("couldn't stash");
}
}
}
use core::sync::atomic::{AtomicU32, Ordering};

View file

@ -606,6 +606,20 @@ fn wrapped_main() -> ! {
xous::return_scalar(msg.sender, duration).expect("couldn't ack test pattern");
}),
Some(Opcode::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(),
_ => ()
}
}
Some(Opcode::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(),
_ => ()
}
}
Some(Opcode::Quit) => break,
None => {
log::error!("received opcode scalar that is not handled");