From cb0686803e9732ca7366a3dc4ee3eeaa4cb610b1 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 6 Mar 2021 02:45:47 +0800 Subject: [PATCH] split out the background clear/redraw from the core loop --- services/gam/src/lib.rs | 40 ++++++++++++++-- services/ime-frontend/src/main.rs | 79 +++++++++++++++++++++---------- 2 files changed, 90 insertions(+), 29 deletions(-) diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index 0ae7eac60..feb0596ce 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -7,7 +7,7 @@ use rkyv::Write; use rkyv::Unarchive; use graphics_server::api::{TextOp, TextView}; -use graphics_server::api::{Point, Gid, Line}; +use graphics_server::api::{Point, Gid, Line, Rectangle, Circle, RoundedRectangle}; use log::info; /// this "posts" a textview -- it's not a "draw" as the update is neither guaranteed nor instantaneous @@ -47,9 +47,43 @@ pub fn draw_line(gam_cid: xous::CID, gid: Gid, line: Line) -> Result<(), xous::E let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096)); let pos = writer.archive(&rkyv_tv).expect("GAM_API: couldn't archive GamObject"); let xous_buffer = writer.into_inner(); - xous_buffer.lend(gam_cid, pos as u32).expect("GAM_API: GamObject operation failure"); - + Ok(()) +} +pub fn draw_rectangle(gam_cid: xous::CID, gid: Gid, rect: Rectangle) -> Result<(), xous::Error> { + let rkyv_tv = api::Opcode::RenderObject( + GamObject { + canvas: gid, + obj: GamObjectType::Rect(rect), + }); + let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096)); + let pos = writer.archive(&rkyv_tv).expect("GAM_API: couldn't archive GamObject"); + let xous_buffer = writer.into_inner(); + xous_buffer.lend(gam_cid, pos as u32).expect("GAM_API: GamObject operation failure"); + Ok(()) +} +pub fn draw_rouded_rectangle(gam_cid: xous::CID, gid: Gid, rr: RoundedRectangle) -> Result<(), xous::Error> { + let rkyv_tv = api::Opcode::RenderObject( + GamObject { + canvas: gid, + obj: GamObjectType::RoundRect(rr), + }); + let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096)); + let pos = writer.archive(&rkyv_tv).expect("GAM_API: couldn't archive GamObject"); + let xous_buffer = writer.into_inner(); + xous_buffer.lend(gam_cid, pos as u32).expect("GAM_API: GamObject operation failure"); + Ok(()) +} +pub fn draw_circle(gam_cid: xous::CID, gid: Gid, circ: Circle) -> Result<(), xous::Error> { + let rkyv_tv = api::Opcode::RenderObject( + GamObject { + canvas: gid, + obj: GamObjectType::Circ(circ), + }); + let mut writer = rkyv::ArchiveBuffer::new(xous::XousBuffer::new(4096)); + let pos = writer.archive(&rkyv_tv).expect("GAM_API: couldn't archive GamObject"); + let xous_buffer = writer.into_inner(); + xous_buffer.lend(gam_cid, pos as u32).expect("GAM_API: GamObject operation failure"); Ok(()) } diff --git a/services/ime-frontend/src/main.rs b/services/ime-frontend/src/main.rs index c41c5f79d..b1d14b843 100644 --- a/services/ime-frontend/src/main.rs +++ b/services/ime-frontend/src/main.rs @@ -71,6 +71,57 @@ impl InputTracker { self.input_canvas.is_some() && self.pred_canvas.is_some() && self.predictor.is_some() } + pub fn clear_area(&mut self) -> Result<(), xous::Error> { + if let Some(pc) = self.pred_canvas { + let pc_bounds: Point = gam::get_canvas_bounds(self.gam_conn, pc).expect("IMEF: Couldn't get prediction canvas bounds"); + gam::draw_rectangle(self.gam_conn, pc, + Rectangle::new_with_style(Point::new(0, 0), pc_bounds, + DrawStyle { + fill_color: Some(PixelColor::Light), + stroke_color: None, + stroke_width: 0 + } + )).expect("IMEF: can't clear prediction area"); + // add the border line on top + gam::draw_line(self.gam_conn, pc, + Line::new_with_style( + Point::new(0,0), + Point::new(pc_bounds.x, 0), + DrawStyle { + fill_color: None, + stroke_color: Some(PixelColor::Dark), + stroke_width: 1, + }) + ).expect("IMEF: can't draw prediction top border"); + } + + if let Some(ic) = self.input_canvas { + let mut ic_bounds: Point = gam::get_canvas_bounds(self.gam_conn, ic).expect("IMEF: Couldn't get input canvas bounds"); + gam::draw_rectangle(self.gam_conn, ic, + Rectangle::new_with_style(Point::new(0, 0), ic_bounds, + DrawStyle { + fill_color: Some(PixelColor::Light), + stroke_color: None, + stroke_width: 0 + } + )).expect("IMEF: can't clear input area"); + + // add the border line on top + gam::draw_line(self.gam_conn, ic, + Line::new_with_style( + Point::new(0,0), + Point::new(ic_bounds.x, 0), + DrawStyle { + fill_color: None, + stroke_color: Some(PixelColor::Dark), + stroke_width: 1, + })) + .expect("IMEF: can't draw input top line border"); + } + + Ok(()) + } + pub fn update(&mut self, newkeys: [char; 4]) -> Result<(), xous::Error> { let debug1= false; // just draw a rectangle for the prediction area for now @@ -86,18 +137,6 @@ impl InputTracker { if debug1{info!("IMEF: posting textview {:?}", starting_tv);} gam::post_textview(self.gam_conn, &mut starting_tv).expect("IMEF: can't draw prediction TextView"); - - // add the border line on top - gam::draw_line(self.gam_conn, pc, - Line::new_with_style( - Point::new(0,0), - Point::new(pc_bounds.x, 0), - DrawStyle { - fill_color: None, - stroke_color: Some(PixelColor::Dark), - stroke_width: 1, - }) - ).expect("IMEF: can't draw prediction top border"); } if let Some(ic) = self.input_canvas { @@ -144,18 +183,6 @@ impl InputTracker { Line::new(ins_pt, ins_pt + Point::new(0, self.insertion.line_height as i16 - input_tv.margin.y) )) .expect("IMEF: can't draw insertion point"); - - // add the border line on top - gam::draw_line(self.gam_conn, ic, - Line::new_with_style( - Point::new(0,0), - Point::new(ic_bounds.x, 0), - DrawStyle { - fill_color: None, - stroke_color: Some(PixelColor::Dark), - stroke_width: 1, - })) - .expect("IMEF: can't draw input top line border"); } /* what else do we need: @@ -221,9 +248,9 @@ fn xmain() -> ! { } } - // force a redraw of the UI with no keys + // force a redraw of the UI if debug1{info!("IMEF: forcing initial UI redraw");} - tracker.update(['\u{0000}'; 4]).expect("IMEF: couldn't redraw initial UI"); + tracker.clear_area().expect("IMEF: can't initially clear areas"); let mut key_queue: Vec = Vec::new(); info!("IMEF: entering main loop");