diff --git a/examples/graphics-server/src/backend/betrusted.rs b/examples/graphics-server/src/backend/betrusted.rs index e36405b68..2d68009ce 100644 --- a/examples/graphics-server/src/backend/betrusted.rs +++ b/examples/graphics-server/src/backend/betrusted.rs @@ -106,31 +106,33 @@ impl DrawTarget for XousDisplay { /// Draw a `Pixel` that has a color defined as `BinaryColor`. fn draw_pixel(&mut self, pixel: Pixel) -> Result<(), Self::Error> { let Pixel(coord, color) = pixel; - let framebuffer = self.fb.as_mut_ptr() as *mut u32; - let offset = (coord.x / 32 + coord.y * FB_WIDTH_WORDS as i32) as usize; - match color { - BinaryColor::Off => { - unsafe { - framebuffer.add(offset).write_volatile( - framebuffer.add(offset).read_volatile() | (1 << (coord.x % 32)), - ) - }; + if coord.x < FB_WIDTH_PIXELS as _ && coord.y < FB_LINES as _ { + let framebuffer = self.fb.as_mut_ptr() as *mut u32; + let offset = (coord.x / 32 + coord.y * FB_WIDTH_WORDS as i32) as usize; + match color { + BinaryColor::Off => { + unsafe { + framebuffer.add(offset).write_volatile( + framebuffer.add(offset).read_volatile() | (1 << (coord.x % 32)), + ) + }; + } + BinaryColor::On => { + unsafe { + framebuffer.add(offset).write_volatile( + framebuffer.add(offset).read_volatile() & !(1 << (coord.x % 32)), + ) + }; + } } - BinaryColor::On => { - unsafe { - framebuffer.add(offset).write_volatile( - framebuffer.add(offset).read_volatile() & !(1 << (coord.x % 32)), - ) - }; - } - } - // set the dirty bit on the line - unsafe { - framebuffer - .add(offset) - .write_volatile(framebuffer.add(offset).read_volatile() | 0x1_0000) - }; + // set the dirty bit on the line + unsafe { + framebuffer + .add(offset) + .write_volatile(framebuffer.add(offset).read_volatile() | 0x1_0000) + }; + } Ok(()) } diff --git a/examples/graphics-server/src/backend/minifb.rs b/examples/graphics-server/src/backend/minifb.rs index ce78b3abe..3d1a51c9a 100644 --- a/examples/graphics-server/src/backend/minifb.rs +++ b/examples/graphics-server/src/backend/minifb.rs @@ -59,11 +59,15 @@ impl DrawTarget for XousDisplay { /// Draw a `Pixel` that has a color defined as `Gray8`. fn draw_pixel(&mut self, pixel: Pixel) -> Result<(), Self::Error> { let Pixel(point, color) = pixel; - self.buffer[(point.y * (WIDTH as i32) + point.x) as usize] = if color == BinaryColor::On { - LIGHT_COLOUR - } else { - DARK_COLOUR - }; + let w = WIDTH as _; + let h = HEIGHT as _; + if point.x < w && point.y < h && point.x >= 0 && point.y >= 0 { + self.buffer[(point.y * w + point.x) as usize] = if color == BinaryColor::On { + LIGHT_COLOUR + } else { + DARK_COLOUR + }; + } Ok(()) } diff --git a/examples/graphics-server/src/lib.rs b/examples/graphics-server/src/lib.rs index 2f03cac63..e0847cd45 100644 --- a/examples/graphics-server/src/lib.rs +++ b/examples/graphics-server/src/lib.rs @@ -3,24 +3,24 @@ pub mod api; pub use api::{Point, Color}; -use xous::{send_message, CID}; +use xous::{try_send_message, CID}; pub fn draw_line(cid: CID, start: Point, end: Point) -> Result<(), xous::Error> { - send_message(cid, api::Opcode::Line(start, end).into()) + try_send_message(cid, api::Opcode::Line(start, end).into()) } pub fn draw_circle(cid: CID, center: Point, radius: u32) -> Result<(), xous::Error> { - send_message(cid, api::Opcode::Circle(center, radius).into()) + try_send_message(cid, api::Opcode::Circle(center, radius).into()) } pub fn draw_rectangle(cid: CID, start: Point, end: Point) -> Result<(), xous::Error> { - send_message(cid, api::Opcode::Rectangle(start, end).into()) + try_send_message(cid, api::Opcode::Rectangle(start, end).into()) } pub fn set_style(cid: CID, width: u32, stroke: Color, fill: Color) -> Result<(), xous::Error> { - send_message(cid, api::Opcode::Style(width, stroke, fill).into()) + try_send_message(cid, api::Opcode::Style(width, stroke, fill).into()) } pub fn flush(cid: CID) -> Result<(), xous::Error> { - send_message(cid, api::Opcode::Flush.into()) + try_send_message(cid, api::Opcode::Flush.into()) }