add drawlists to the GAM and gfx abstractions

this helps to prevent race conditions and improves performance
This commit is contained in:
bunnie 2022-01-24 16:33:01 +08:00
parent 6089672960
commit 8e13d6bcdf
8 changed files with 151 additions and 6 deletions

View file

@ -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) {

View file

@ -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<GamObjectType>; 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),

View file

@ -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<Point, xous::Error> {
log::trace!("GAM_API: get_canvas_bounds");

View file

@ -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::<GamObjectList, _>().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::<TokenClaim, _>().unwrap();

View file

@ -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::<ClipObject>; 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]>,

View file

@ -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(

View file

@ -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::<ClipObjectList, _>().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())

View file

@ -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");