From 8e13d6bcdfb4636fab8a0a7f713c35ea83920592 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 24 Jan 2022 16:33:01 +0800 Subject: [PATCH] add drawlists to the GAM and gfx abstractions this helps to prevent race conditions and improves performance --- apps/ball/src/ball.rs | 8 +++-- services/gam/src/api.rs | 25 ++++++++++++++++ services/gam/src/lib.rs | 5 ++++ services/gam/src/main.rs | 44 ++++++++++++++++++++++++++++ services/graphics-server/src/api.rs | 26 ++++++++++++++++ services/graphics-server/src/lib.rs | 11 ++++++- services/graphics-server/src/main.rs | 29 ++++++++++++++++++ services/status/src/main.rs | 9 ++++-- 8 files changed, 151 insertions(+), 6 deletions(-) diff --git a/apps/ball/src/ball.rs b/apps/ball/src/ball.rs index 9cd40597f..039fbadf8 100644 --- a/apps/ball/src/ball.rs +++ b/apps/ball/src/ball.rs @@ -75,9 +75,12 @@ impl Ball { } } pub(crate) fn update(&mut self) { + // send a list of objects to draw to the GAM, to avoid race conditions in between operations + let mut draw_list = GamObjectList::new(self.gid); + // clear the previous location of the ball self.ball.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 1); - self.gam.draw_circle(self.gid, self.ball).expect("couldn't erase ball's previous position"); + draw_list.push(GamObjectType::Circ(self.ball)).unwrap(); if self.mode == BallMode::Tilt { let (x, y, _z, _id) = self.com.gyro_read_blocking().unwrap(); let ix = x as i16; @@ -137,7 +140,8 @@ impl Ball { // draw the new location for the ball self.ball.style = DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1); - self.gam.draw_circle(self.gid, self.ball).expect("couldn't erase ball's previous position"); + draw_list.push(GamObjectType::Circ(self.ball)).unwrap(); + self.gam.draw_list(draw_list).expect("couldn't execute draw list"); self.gam.redraw().unwrap(); } pub(crate) fn focus(&mut self) { diff --git a/services/gam/src/api.rs b/services/gam/src/api.rs index e3ea63c04..e434162c8 100644 --- a/services/gam/src/api.rs +++ b/services/gam/src/api.rs @@ -16,6 +16,30 @@ pub struct GamObject { pub canvas: Gid, pub obj: GamObjectType, } +#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub struct GamObjectList { + pub canvas: Gid, + pub list: [Option; 32], + free: usize, +} +impl GamObjectList { + pub fn new(canvas: Gid) -> GamObjectList { + GamObjectList { + canvas, + list: Default::default(), + free: 0, + } + } + pub fn push(&mut self, item: GamObjectType) -> Result<(), GamObjectType> { + if self.free < self.list.len() { + self.list[self.free] = Some(item); + self.free += 1; + Ok(()) + } else { + Err(item) + } + } +} #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone, Eq, PartialEq)] pub enum TokenType { @@ -91,6 +115,7 @@ pub(crate) enum Opcode { // draws an object RenderObject, //(GamObject), + RenderObjectList, // renders a TextView RenderTextView, //(TextView), diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index 1250f7589..691d87579 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -194,6 +194,11 @@ impl Gam { let buf = Buffer::into_buf(go).or(Err(xous::Error::InternalError))?; buf.lend(self.conn, Opcode::RenderObject.to_u32().unwrap()).map(|_|()) } + pub fn draw_list(&self, list: GamObjectList) -> Result<(), xous::Error> { + let buf = Buffer::into_buf(list).or(Err(xous::Error::InternalError))?; + buf.lend(self.conn, Opcode::RenderObjectList.to_u32().unwrap()) + .map(|_| ()) + } pub fn get_canvas_bounds(&self, gid: Gid) -> Result { log::trace!("GAM_API: get_canvas_bounds"); diff --git a/services/gam/src/main.rs b/services/gam/src/main.rs index f952baffb..d2e6b8230 100644 --- a/services/gam/src/main.rs +++ b/services/gam/src/main.rs @@ -335,6 +335,50 @@ fn xmain() -> ! { } log::trace!("leaving RenderObject"); } + Some(Opcode::RenderObjectList) => { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let obj_ipc = buffer.to_original::().unwrap(); + if let Some(canvas) = canvases.get_mut(&obj_ipc.canvas) { + // first, figure out if we should even be drawing to this canvas. + if canvas.is_drawable() { + let mut obj_list = ClipObjectList::default(); + for item in obj_ipc.list.iter() { + if let Some(obj) = item { + match obj { + GamObjectType::Line(mut line) => { + line.translate(canvas.clip_rect().tl); + line.translate(canvas.pan_offset()); + obj_list.push(ClipObjectType::Line(line), canvas.clip_rect()).unwrap(); + }, + GamObjectType::Circ(mut circ) => { + circ.translate(canvas.clip_rect().tl); + circ.translate(canvas.pan_offset()); + obj_list.push(ClipObjectType::Circ(circ), canvas.clip_rect()).unwrap(); + }, + GamObjectType::Rect(mut rect) => { + rect.translate(canvas.clip_rect().tl); + rect.translate(canvas.pan_offset()); + obj_list.push(ClipObjectType::Rect(rect), canvas.clip_rect()).unwrap(); + }, + GamObjectType::RoundRect(mut rr) => { + rr.translate(canvas.clip_rect().tl); + rr.translate(canvas.pan_offset()); + obj_list.push(ClipObjectType::RoundRect(rr), canvas.clip_rect()).unwrap(); + } + } + } else { + break; + } + } + gfx.draw_object_list_clipped(obj_list).expect("couldn't draw object list"); + canvas.do_drawn().expect("couldn't set canvas to drawn"); + } else { + log::debug!("attempt to draw Object on non-drawable canvas. Not fatal, but request ignored: {:?}", obj_ipc); + } + } else { + info!("bogus GID in Object, not doing anything in response to draw request."); + } + } Some(Opcode::ClaimToken) => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut tokenclaim = buffer.to_original::().unwrap(); diff --git a/services/graphics-server/src/api.rs b/services/graphics-server/src/api.rs index 43bc5c374..2bee41ed2 100644 --- a/services/graphics-server/src/api.rs +++ b/services/graphics-server/src/api.rs @@ -88,6 +88,7 @@ pub(crate) enum Opcode { /// draws an object that requires clipping DrawClipObject, //(ClipObject), + DrawClipObjectList, /// draws the sleep screen; assumes requests are vetted by GAM/xous-names DrawSleepScreen, @@ -126,6 +127,31 @@ pub struct ClipObject { pub obj: ClipObjectType, } +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] +pub struct ClipObjectList { + // ClipObject is 28 bytes, so 32 of these takes 896 bytes, which is less than a 4k page (the minimum amount that gets remapped) + // we limit the length to 32 so we can use the Default initializer to set the None's on the array, otherwise it gets a bit painful. + pub list: [Option::; 32], + free: usize, +} +impl ClipObjectList { + pub fn default() -> ClipObjectList { + ClipObjectList { + list: Default::default(), + free: 0, + } + } + pub fn push(&mut self, item: ClipObjectType, clip: Rectangle) -> Result<(), ClipObjectType> { + if self.free < self.list.len() { + self.list[self.free] = Some(ClipObject {clip, obj: item}); + self.free += 1; + Ok(()) + } else { + Err(item) + } + } +} + #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] pub struct TokenClaim { pub token: Option<[u32; 4]>, diff --git a/services/graphics-server/src/lib.rs b/services/graphics-server/src/lib.rs index 7657fa64f..3628adedb 100644 --- a/services/graphics-server/src/lib.rs +++ b/services/graphics-server/src/lib.rs @@ -4,7 +4,7 @@ pub mod api; pub use api::{ Circle, ClipObject, ClipObjectType, DrawStyle, Gid, Line, PixelColor, Point, Rectangle, - RoundedRectangle, TextBounds, TextOp, TextView, TokenClaim, ClipRect, Cursor, GlyphStyle + RoundedRectangle, TextBounds, TextOp, TextView, TokenClaim, ClipRect, Cursor, GlyphStyle, ClipObjectList }; pub mod op; @@ -208,6 +208,15 @@ impl Gfx { .map(|_| ()) } + pub fn draw_object_list_clipped( + &self, + list: ClipObjectList, + ) -> Result<(), xous::Error> { + let buf = Buffer::into_buf(list).or(Err(xous::Error::InternalError))?; + buf.lend(self.conn, Opcode::DrawClipObjectList.to_u32().unwrap()) + .map(|_| ()) + } + pub fn draw_sleep_note(&self, flag: bool) -> Result<(), xous::Error> { let arg = if flag { 1 } else { 0 }; send_message( diff --git a/services/graphics-server/src/main.rs b/services/graphics-server/src/main.rs index b0b4865d8..5b4f426f2 100644 --- a/services/graphics-server/src/main.rs +++ b/services/graphics-server/src/main.rs @@ -184,6 +184,35 @@ fn xmain() -> ! { } } } + Some(Opcode::DrawClipObjectList) => { + let buffer = + unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let list_ipc = buffer.to_original::().unwrap(); + for maybe_item in list_ipc.list.iter() { + if let Some(obj) = maybe_item { + match obj.obj { + ClipObjectType::Line(line) => { + op::line(display.native_buffer(), line, Some(obj.clip), false); + } + ClipObjectType::XorLine(line) => { + op::line(display.native_buffer(), line, Some(obj.clip), true); + } + ClipObjectType::Circ(circ) => { + op::circle(display.native_buffer(), circ, Some(obj.clip)); + } + ClipObjectType::Rect(rect) => { + op::rectangle(display.native_buffer(), rect, Some(obj.clip)); + } + ClipObjectType::RoundRect(rr) => { + op::rounded_rectangle(display.native_buffer(), rr, Some(obj.clip)); + } + } + } else { + // stop at the first None entry -- if the sender packed the list with a hole in it, that's their bad + break; + } + } + } Some(Opcode::DrawTextView) => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) diff --git a/services/status/src/main.rs b/services/status/src/main.rs index 039f3b237..106dd6da1 100644 --- a/services/status/src/main.rs +++ b/services/status/src/main.rs @@ -14,6 +14,7 @@ use graphics_server::*; use graphics_server::api::GlyphStyle; use locales::t; use gam::modal::*; +use gam::{GamObjectList, GamObjectType}; use llio::Weekday; use std::collections::HashMap; @@ -513,19 +514,21 @@ fn xmain() -> ! { .expect("|status: can't draw uptime"); } { // update the CPU load bar - gam.draw_rectangle(status_gid, cpuload_rect).ok(); + let mut draw_list = GamObjectList::new(status_gid); + draw_list.push(GamObjectType::Rect(cpuload_rect)).unwrap(); let (latest_activity, period) = llio .activity_instantaneous() .expect("couldn't get CPU activity"); let activity_to_width = ((latest_activity as f32) / (period as f32)) * (cpuload_rect.width() - 4) as f32; - gam.draw_rectangle(status_gid, + draw_list.push(GamObjectType::Rect( Rectangle::new_coords_with_style( cpuload_rect.tl().x + 2, cpuload_rect.tl().y + 2, cpuload_rect.tl().x + 2 + activity_to_width as i16, cpuload_rect.br().y - 2, DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 0)) - ).ok(); + )).unwrap(); + gam.draw_list(draw_list).expect("couldn't draw object list"); } gam.redraw().expect("|status: couldn't redraw");