From 463e2ad77dc352486b7f875422bf24a82763f7b9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 26 Feb 2021 03:37:48 +0800 Subject: [PATCH] fixup GAM APIs to latest rkyv standard, fix drawable computation --- buildpush.sh | 2 +- services/com/src/main.rs | 2 +- services/gam/README.md | 23 +++++++++------ services/gam/src/api.rs | 6 ++-- services/gam/src/canvas.rs | 50 ++++++++++++++++++++++----------- services/gam/src/lib.rs | 11 +++++--- services/gam/src/main.rs | 34 +++++++++++----------- services/gam/src/status.rs | 9 ++++-- services/log-server/src/main.rs | 2 +- xtask/src/main.rs | 4 +-- 10 files changed, 86 insertions(+), 57 deletions(-) diff --git a/buildpush.sh b/buildpush.sh index 451256210..aa71eb5a1 100755 --- a/buildpush.sh +++ b/buildpush.sh @@ -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 diff --git a/services/com/src/main.rs b/services/com/src/main.rs index e596fc06c..00c0f3e33 100644 --- a/services/com/src/main.rs +++ b/services/com/src/main.rs @@ -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 { diff --git a/services/gam/README.md b/services/gam/README.md index 834a9f989..9a7f9f06f 100644 --- a/services/gam/README.md +++ b/services/gam/README.md @@ -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 diff --git a/services/gam/src/api.rs b/services/gam/src/api.rs index 56ffbf7b8..25b3fa14e 100644 --- a/services/gam/src/api.rs +++ b/services/gam/src/api.rs @@ -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), diff --git a/services/gam/src/canvas.rs b/services/gam/src/canvas.rs index 65479d24a..19a7e42f9 100644 --- a/services/gam/src/canvas.rs +++ b/services/gam/src/canvas.rs @@ -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, screen: Rectangle) -> FnvIndexMap { +pub fn recompute_canvases(canvases: FnvIndexMap, screen: Rectangle) -> FnvIndexMap { + 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 = 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, screen: R let mut higher_clipregions: BinaryHeap = 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 ®ion 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 ®ion 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 = 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(); } diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index 3d354977d..298010fa1 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -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::(xous_buffer.as_ref(), pos)}; - if let rkyv::Archived::::TextViewResult(result) = returned { - let tvr: TextViewResult = result.unarchive(); - tv.set_computed_bounds(tvr.bounds_computed); + if let rkyv::Archived::::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"); } diff --git a/services/gam/src/main.rs b/services/gam/src/main.rs index 464ae582d..4b6af3d14 100644 --- a/services/gam/src/main.rs +++ b/services/gam/src/main.rs @@ -44,19 +44,19 @@ struct ModalCanvases { fn add_modal_layout(trng_conn: xous::CID, canvases: &mut FnvIndexMap) -> Result { 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::(Pin::new(buf.as_mut()), m.id as usize) }; + use rkyv::Write; + //let mut writer: rkyv::ArchiveBuffer = rkyv::ArchiveBuffer::new(buf); + let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096)); match &*value { rkyv::Archived::::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::(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(), } diff --git a/services/gam/src/status.rs b/services/gam/src/status.rs index 7f42b2ebc..2c40866f3 100644 --- a/services/gam/src/status.rs +++ b/services/gam/src/status.rs @@ -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 { diff --git a/services/log-server/src/main.rs b/services/log-server/src/main.rs index 9b3eb4da7..d14466df0 100644 --- a/services/log-server/src/main.rs +++ b/services/log-server/src/main.rs @@ -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(); diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 4e1e709d9..e593480e8 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -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",