fixup GAM APIs to latest rkyv standard, fix drawable computation

This commit is contained in:
bunnie 2021-02-26 03:37:48 +08:00
parent 5b80b3fb42
commit 463e2ad77d
10 changed files with 86 additions and 57 deletions

View file

@ -67,7 +67,7 @@ DESTDIR=code/precursors
# full rebuild, and not just an incremental rebuild. :-/
#touch services/log-server/src/main.rs # bump the build time in the log server
cargo $USE_NIGHTLY xtask fcc-agent ../betrusted-soc/build/software/soc.svd
cargo $USE_NIGHTLY xtask hw-image ../betrusted-soc/build/software/soc.svd
# only copy if changed, othrewise it seems to trigger extra build effort...
rsync -a --no-times --checksum ../betrusted-soc/build/software/soc.svd svd2utra/examples/soc.svd

View file

@ -266,7 +266,7 @@ fn xmain() -> ! {
info!("COM: starting main loop");
loop {
let envelope = xous::receive_message(com_sid).unwrap();
info!("COM: Message: {:?}", envelope);
// info!("COM: Message: {:?}", envelope);
if let xous::Message::Borrow(m) = &envelope.body {
let mut buf = unsafe { buffer::XousBuffer::from_memory_message(m) };
let value = unsafe {

View file

@ -37,17 +37,22 @@ refer to a `Canvas` at render time; this allows for easy implementation
of panning and scrolling.
A `Canvas` has a `trust_level` associated with it. Higher numbers are more
trusted; 255 is the highest level of trust. More trusted `Canvas` objects always
render on top of lower trusted object; furthermore, when a higher trusted
`Canvas` object overlaps a lower trusted object, the lower trusted object is
defaced using hatched lines with a random angle and spacing, and further updates
are disallowed. Thus, a `Canvas` should *not* be thought of like a "window", as
windows in typical UIs are allowed to freely overlap and clipping is handled
trusted; 255 is the highest level of trust. Rules for drawing are as follows:
1. More trusted `Canvas` objects always render on top of lower trusted object
2. When a higher trusted `Canvas` object overlaps a lower trusted object,
the lower trusted object is:
- defaced using hatched lines with a random angle and spacing
- further updates to the lower trusted object are disallowed.
Thus, a `Canvas` should *not* be thought of like a "window", as windows in
typical UIs are allowed to freely overlap and clipping is handled
by simply drawing over lower layers of content.
In the case that both trusted and untrusted data should be rendered on the same
screen, `Canvas` makes it strongly preferred to render them next to each other, rather
than one on top of each other. This policy is partially to help users be very
`Canvas` makes it strongly preferred to render trusted and untrusted data
side-by-side, rather than one on top of each other.
This policy is partially to help users be very
clear as to e.g. where a password box is vs. an image that looks a lot like
a password box; but the policy is also informed by the limitations of the underlying
hardware. In particular, the underlying memory LCD strongly

View file

@ -3,7 +3,8 @@ use graphics_server::api::{Point, Rectangle, TextView, TextViewResult, Gid};
use blitstr_ref as blitstr;
use blitstr::{GlyphStyle, Cursor};
#[derive(Debug, rkyv::Archive, rkyv::Unarchive)]
#[derive(Debug, rkyv::Archive, rkyv::Unarchive, Copy, Clone)]
// #[archive(derive(Copy, Clone))]
pub enum Opcode {
// clears a canvas with a given GID
ClearCanvas(Gid),
@ -11,9 +12,6 @@ pub enum Opcode {
// renders a TextView
RenderTextView(TextView),
// result codes from operations on TextViews
TextViewResult(TextViewResult),
// returns a GID to the "content" Canvas; requires an authentication token
RequestContentCanvas(Gid),

View file

@ -140,11 +140,14 @@ impl Eq for Canvas {}
// we use the "screen" parameter to determine when we can turn off drawing to canvases that are off-screen
pub fn recompute_canvases(mut canvases: FnvIndexMap<Gid, Canvas, U32>, screen: Rectangle) -> FnvIndexMap<Gid, Canvas, U32> {
pub fn recompute_canvases(canvases: FnvIndexMap<Gid, Canvas, U32>, screen: Rectangle) -> FnvIndexMap<Gid, Canvas, U32> {
let debug = false;
// first, sort canvases by trust_level. Canvas implements ord/eq based on the trust_level attribute
// so jush pushing it into a max binary heap does the trick.
if debug { info!("CANVAS: recompute canvas"); }
let mut sorted_clipregions: BinaryHeap<Canvas, U32, Max> = BinaryHeap::new();
for (_, &c) in canvases.iter() {
for (&k, &c) in canvases.iter() {
if debug { info!(" CANVAS: sorting gid {:?}, canvas {:?}", k, c);}
sorted_clipregions.push(c).unwrap(); // always succeeds because incoming type is the same size
}
@ -152,29 +155,42 @@ pub fn recompute_canvases(mut canvases: FnvIndexMap<Gid, Canvas, U32>, screen: R
let mut higher_clipregions: BinaryHeap<Canvas, U32, Max> = BinaryHeap::new();
let mut trust_level: u8 = 255;
// sorted_clipregions is a Max heap keyed on trust, so popping the elements off will return them sorted from most to least trusted
if let Some(c) = sorted_clipregions.pop() {
let mut canvas = c.clone();
info!("CANVAS: now determining which regions are drawable");
loop {
if let Some(c) = sorted_clipregions.pop() {
if debug { info!(" CANVAS: considering {:?}", c);}
let mut canvas = c.clone();
let mut drawable: bool = true;
let clip_region = canvas.clip_rect();
if trust_level < canvas.trust_level() {
trust_level = canvas.trust_level();
}
// note that this .iter() is *not* sorted by trust level, but all elements will be of greater than or equal to the current trust level
for &region in higher_clipregions.iter() {
// regions of the same trust level can draw over each other. Draw order is arbitrary.
if region.clip_rect().intersects(clip_region) && (region.trust_level() < trust_level) ||
!region.clip_rect().intersects(screen) {
drawable = false;
let mut drawable: bool = true;
let clip_region = canvas.clip_rect();
if trust_level < canvas.trust_level() {
trust_level = canvas.trust_level();
}
if !clip_region.intersects(screen) {
drawable = false;
if debug { info!(" CANVAS: not drawable, does not intersect");}
} else { // short circuit this computation if it's not drawable because it's off screen
// note that this .iter() is *not* sorted by trust level, but all elements will be of greater than or equal to the current trust level
for &region in higher_clipregions.iter() {
// regions of the same trust level can draw over each other. Draw order is arbitrary.
if region.clip_rect().intersects(clip_region) && (region.trust_level() < trust_level) {
drawable = false;
if debug { info!(" CANVAS: not drawable, lower trust intersecting with higher trust region");}
}
}
}
canvas.set_drawable(drawable);
higher_clipregions.push(canvas).unwrap();
} else {
break;
}
canvas.set_drawable(drawable);
higher_clipregions.push(canvas).unwrap();
}
// create a new index map out of the recomputed higher_clipregions
let mut map: FnvIndexMap<Gid, Canvas, U32> = FnvIndexMap::new();
if debug { info!("CANVAS: reconstituting index map");}
for &c in higher_clipregions.iter() {
if debug { info!(" CANVAS: inserting gid {:?}, canvas {:?}", c.gid(), c);}
map.insert(c.gid(), c).unwrap();
}

View file

@ -19,7 +19,7 @@ pub fn post_textview(gam_cid: xous::CID, tv: &mut TextView) -> Result<(), xous::
tv.cursor.pt.x = 37;
tv.cursor.pt.y = 5;
info!("tv before lend: {:?}", tv);
// info!("tv before lend: {:?}", tv);
let mut rkyv_tv = api::Opcode::RenderTextView(*tv);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
let pos = writer.archive(&rkyv_tv).expect("couldn't archive textview");
@ -27,12 +27,15 @@ pub fn post_textview(gam_cid: xous::CID, tv: &mut TextView) -> Result<(), xous::
xous_buffer.lend_mut(gam_cid, pos as u32).expect("RenderTextView operation failure");
// recover the mutable values and mirror the ones we care about back into our local structure
let returned = unsafe { rkyv::archived_value::<api::Opcode>(xous_buffer.as_ref(), pos)};
if let rkyv::Archived::<api::Opcode>::TextViewResult(result) = returned {
let tvr: TextViewResult = result.unarchive();
tv.set_computed_bounds(tvr.bounds_computed);
if let rkyv::Archived::<api::Opcode>::RenderTextView(result) = returned {
let tvr: TextView = result.unarchive();
tv.set_computed_bounds(tvr.get_bounds_computed());
tv.cursor = tvr.cursor;
} else {
let tvr = returned.unarchive();
info!("got {:?}", tvr);
panic!("post_textview got a return value from the server that isn't expected or handled");
}

View file

@ -44,19 +44,19 @@ struct ModalCanvases {
fn add_modal_layout(trng_conn: xous::CID, canvases: &mut FnvIndexMap<Gid, Canvas, U32>) -> Result<ModalCanvases, xous::Error> {
let password_canvas = Canvas::new(
Rectangle::new_coords(-1, -1, -1, -1),
0, trng_conn, None
255, trng_conn, None
).expect("GAM: couldn't create password canvas");
canvases.insert(password_canvas.gid(), password_canvas).expect("GAM: can't store password canvas");
let menu_canvas = Canvas::new(
Rectangle::new_coords(-1, -1, -1, -1),
0, trng_conn, None
255, trng_conn, None
).expect("GAM: couldn't create menu canvas");
canvases.insert(menu_canvas.gid(), menu_canvas).expect("GAM: can't store menu canvas");
let alert_canvas = Canvas::new(
Rectangle::new_coords(-1, -1, -1, -1),
0, trng_conn, None
255, trng_conn, None
).expect("GAM: couldn't create alert canvas");
canvases.insert(alert_canvas.gid(), alert_canvas).expect("GAM: can't store alert canvas");
@ -76,26 +76,26 @@ fn add_chat_layout(gfx_conn: xous::CID, trng_conn: xous::CID, canvases: &mut Fnv
// allocate canvases in structures, and record their GID for future reference
let status_canvas = Canvas::new(
Rectangle::new_coords(0, 0, screensize.x, small_height),
0, trng_conn, None
255, trng_conn, None
).expect("GAM: couldn't create status canvas");
canvases.insert(status_canvas.gid(), status_canvas).expect("GAM: can't store status canvus");
let predictive_canvas = Canvas::new(
Rectangle::new_coords(0, screensize.y - regular_height, screensize.x, screensize.y),
1,
254,
trng_conn, None
).expect("GAM: couldn't create predictive text canvas");
canvases.insert(predictive_canvas.gid(), predictive_canvas).expect("GAM: couldn't store predictive canvas");
let input_canvas = Canvas::new(
Rectangle::new_v_stack(predictive_canvas.clip_rect(), -regular_height),
1, trng_conn, None
254, trng_conn, None
).expect("GAM: couldn't create input text canvas");
canvases.insert(input_canvas.gid(), input_canvas).expect("GAM: couldn't store input canvas");
let content_canvas = Canvas::new(
Rectangle::new_v_span(status_canvas.clip_rect(), input_canvas.clip_rect()),
2, trng_conn, None
128, trng_conn, None
).expect("GAM: couldn't create content canvas");
canvases.insert(content_canvas.gid(), content_canvas).expect("GAM: can't store content canvas");
@ -115,6 +115,12 @@ fn tv_draw(gfx_conn: xous::CID, trng_conn: xous::CID, canvases: &mut FnvIndexMap
4. draw text
*/
/*
info!("GAM: tv_draw canvas list");
for (&k, &v) in canvases.iter() {
info!("gid: {:?}, canvas: {:?}", k, v);
}*/
if let Some(canvas) = canvases.get_mut(&tv.get_canvas_gid()) {
// first, figure out if we should even be drawing to this canvas.
if canvas.is_drawable() {
@ -163,6 +169,7 @@ fn xmain() -> ! {
canvases = recompute_canvases(canvases, Rectangle::new(Point::new(0, 0), screensize));
// make a thread to manage the status bar
// the status bar is a trusted element managed by the OS, and we are chosing to domicile this in the GAM process for now
xous::create_thread_simple(status_thread, chatlayout.status.gid()).expect("GAM: couldn't create status thread");
let mut last_time: u64 = ticktimer_server::elapsed_ms(ticktimer_conn).unwrap();
@ -195,6 +202,9 @@ fn xmain() -> ! {
let value = unsafe {
archived_value_mut::<api::Opcode>(Pin::new(buf.as_mut()), m.id as usize)
};
use rkyv::Write;
//let mut writer: rkyv::ArchiveBuffer<xous::XousBuffer> = rkyv::ArchiveBuffer::new(buf);
let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096));
match &*value {
rkyv::Archived::<api::Opcode>::RenderTextView(rtv) => {
let mut tv = rtv.unarchive();
@ -208,20 +218,12 @@ fn xmain() -> ! {
},
};
let tv_ret = graphics_server::api::text::TextViewResult {
bounds_computed: tv.get_bounds_computed(),
cursor: tv.cursor,
};
use rkyv::Write;
let retop = Opcode::TextViewResult(tv_ret);
let mut writer = rkyv::ArchiveBuffer::new(buf);
let pos = writer.archive(&retop).expect("GAM: couldn't archive TextViewResult");
unsafe{ rkyv::archived_value::<api::Opcode>(writer.into_inner().as_ref(), pos)};
}
_ => panic!("GAM: invalid mutable borrow message"),
};
}
}
// envelope implements Drop(), which includes a call to syscall::return_memory(self.sender, message.buf)
_ => xous::yield_slice(),
}

View file

@ -27,8 +27,10 @@ pub fn status_thread(canvas_gid: [u32; 4]) {
let small_height: i16 = graphics_server::glyph_height_hint(gfx_conn, blitstr::GlyphStyle::Small).expect("GAM|status: couldn't get glyph height") as i16;
info!("GAM|status: building textview objects");
// build uptime text view: left half of status bar
let mut uptime_tv = TextView::new(status_gid, 0,
TextBounds::BoundingBox(Rectangle::new(Point::new(0,0), Point::new(screensize.x / 2, small_height))));
TextBounds::BoundingBox(Rectangle::new(Point::new(0,0),
Point::new(screensize.x / 2, small_height))));
uptime_tv.untrusted = false;
uptime_tv.style = blitstr::GlyphStyle::Small;
uptime_tv.draw_border = false;
@ -37,8 +39,10 @@ pub fn status_thread(canvas_gid: [u32; 4]) {
info!("GAM|status: screensize as reported: {:?}", screensize);
info!("GAM|status: uptime initialized to '{:?}'", uptime_tv);
// build battstats text view: right half of status bar
let mut battstats_tv = TextView::new(status_gid, 0,
TextBounds::BoundingBox(Rectangle::new(Point::new(screensize.x / 2, 0), Point::new(screensize.x, small_height))));
TextBounds::BoundingBox(Rectangle::new(Point::new(screensize.x / 2, 0),
Point::new(screensize.x, small_height))));
battstats_tv.style = blitstr::GlyphStyle::Small;
battstats_tv.draw_border = false;
battstats_tv.x_margin = 0; battstats_tv.y_margin = 0;
@ -58,6 +62,7 @@ pub fn status_thread(canvas_gid: [u32; 4]) {
com::api::Opcode::BattStatsEvent(s) => {
stats = s.clone();
battstats_tv.clear_str();
// toggle between two views of the data; duration of toggle is set by the modulus and thresholds below
if stats_phase > 4 {
write!(&mut battstats_tv, "{}mV {}mA", stats.voltage, stats.current).expect("GAM|status: can't write string");
} else {

View file

@ -324,7 +324,7 @@ fn some_main() -> ! {
)
.expect("couldn't map GPIO CSR range");
let mut gpio = CSR::new(gpio_base.as_mut_ptr() as *mut u32);
gpio.wfo(utra::gpio::UARTSEL_UARTSEL, 2); // 0 = kernel, 1 = log, 2 = app_uart
gpio.wfo(utra::gpio::UARTSEL_UARTSEL, 1); // 0 = kernel, 1 = log, 2 = app_uart
}
let mut output = implementation::init();

View file

@ -201,8 +201,8 @@ fn renode_image(debug: bool, packages: &[&str]) -> Result<(), DynError> {
fn run(debug: bool) -> Result<(), DynError> {
let stream = if debug { "debug" } else { "release" };
let init = [
"shell",
//"gam",
//"shell",
"gam",
//"benchmark",
//"benchmark-target",
"log-server",