defacement first-pass seems working

This commit is contained in:
bunnie 2021-06-18 17:40:00 +08:00
parent 0f937037e4
commit 65cc8dd90c
3 changed files with 65 additions and 14 deletions

View file

@ -145,7 +145,7 @@ impl PartialEq for Canvas {
impl Eq for Canvas {}
pub fn deface(gfx: &graphics_server::Gfx, tng: &trng::Trng, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> Result<(), xous::Error> {
pub fn deface(gfx: &graphics_server::Gfx, trng: &trng::Trng, canvases: &mut FnvIndexMap<Gid, Canvas, 32>) -> bool {
// first check if any need defacing, if not, then we're done
let mut needs_defacing = false;
for (_, c) in canvases.iter() {
@ -154,7 +154,10 @@ pub fn deface(gfx: &graphics_server::Gfx, tng: &trng::Trng, canvases: &mut FnvIn
}
}
if needs_defacing {
error!("GAM: haven't implemented canvas defacing yet");
log::debug!("doing a deface computation!");
let screensize = gfx.screen_size().unwrap();
let screen_rect = Rectangle::new(Point::new(0, 0,), screensize);
/*
This routine will need to do something similar to recompute_canvases, where it extracts
a sorted order and draws the defacement upon the canvas that requires defacing.
@ -163,19 +166,63 @@ pub fn deface(gfx: &graphics_server::Gfx, tng: &trng::Trng, canvases: &mut FnvIn
in between states, so in the worst case we are drawing defacement with a rectangular clip
area open in the middle of a canvas...
*/
// temporarirly clear the error so we can develop other features
for (_, c) in canvases.iter_mut() {
if c.needs_defacing() {
let clip_rect = c.clip_rect();
if clip_rect.intersects(screen_rect) {
let width = clip_rect.br().x - clip_rect.tl().x;
let height = clip_rect.br().y - clip_rect.tl().y;
// roughly scale the number of lines hatched by the size of the clipping area to deface
let mut num_lines = (width + height) / 24;
if num_lines < 8 {
num_lines = 8;
}
if num_lines > 40 {
num_lines = 40;
}
// log::debug!("deface width {} height {}, numlines {}, cliprect {:?}", width, height, num_lines, clip_rect);
// draw 32 lines, of random orientation and lengths, across the clip area.
for _ in 0..num_lines {
// do the actual defacing
// get 64 bits of entropy, and express it geometrically
let mut rand = trng.get_u64().unwrap();
let mut x = (rand & 0xFFF) as i16;
rand >>= 12;
let mut y = (rand & 0xFFF) as i16;
rand >>= 12;
let mut delta_x = (rand & 0x3F) as i16 + 64;
rand >>= 6;
if (rand & 1) == 1 {
delta_x = -delta_x;
}
rand >>= 1;
let mut delta_y = (rand & 0x3F) as i16 + 64;
rand >>= 6;
if (rand & 1) == 1 {
delta_y = -delta_y;
}
// rand >>= 1;
x = x % width;
y = y % height;
gfx.draw_line_clipped(
Line::new_with_style(
Point::new(x, y),
Point::new(x + delta_x, y + delta_y),
DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1)),
clip_rect).unwrap();
}
}
// indicate that the defacement has happened to the canvas state machine
c.do_defaced();
c.do_defaced().expect("couldn't update defacement state");
}
}
Ok(())
} else {
Ok(())
}
needs_defacing
}
// we use the "screen" parameter to determine when we can turn off drawing to canvases that are off-screen

View file

@ -377,8 +377,8 @@ impl ContextManager {
let last_token = context.app_token;
self.last_context = self.focused_context;
self.focused_context = Some(last_token);
self.redraw().expect("couldn't redraw the currently focused app");
}
self.redraw().expect("couldn't redraw the currently focused app");
}
}
pub(crate) fn revert_focus(&mut self,
@ -606,12 +606,15 @@ fn xmain() -> ! {
if elapsed_time - last_time > 33 { // rate limit updates, no point in going faster than the eye can see
last_time = elapsed_time;
deface(&gfx, &trng, &mut canvases);
if deface(&gfx, &trng, &mut canvases) {
// redraw the trusted foreground apps after a defacement
context_mgr.redraw().expect("couldn't redraw after defacement");
}
log::trace!("flushing...");
gfx.flush().expect("couldn't flush buffer to screen");
for (_, c) in canvases.iter_mut() {
c.do_flushed();
c.do_flushed().expect("couldn't update flushed state");
}
}
})
@ -679,10 +682,11 @@ fn xmain() -> ! {
let mut cb = buffer.to_original::<SetCanvasBoundsRequest, _>().unwrap();
log::debug!("SetCanvasBoundsRequest {:?}", cb);
let granted = context_mgr.set_canvas_height(&gfx, cb.token, cb.requested.y, &&status_canvas, &mut canvases);
let granted = context_mgr.set_canvas_height(&gfx, cb.token, cb.requested.y, &status_canvas, &mut canvases);
if granted.is_some() {
// recompute the canvas orders based on the new layout
canvases = recompute_canvases(&canvases, Rectangle::new(Point::new(0, 0), screensize));
let mut recomp_canvases = recompute_canvases(&canvases, Rectangle::new(Point::new(0, 0), screensize));
canvases = recomp_canvases;
context_mgr.redraw().expect("can't redraw after new canvas bounds");
}
cb.granted = granted;

View file

@ -85,7 +85,7 @@ impl Menu {
self.gam.draw_rounded_rectangle(self.canvas,
RoundedRectangle::new(
Rectangle::new_with_style(Point::new(0, 0), canvas_size,
DrawStyle::new(PixelColor::Dark, PixelColor::Dark, 1)
DrawStyle::new(PixelColor::Light, PixelColor::Dark, 3)
), 5
)).unwrap();
}