modify graphics-server to use shared handlers

This commit is contained in:
bunnie 2025-02-23 18:07:16 +08:00
parent 55cbe7a962
commit e0462f1603

View file

@ -3,8 +3,8 @@
mod backend;
use backend::XousDisplay;
use ux_api::minigfx::op;
use ux_api::minigfx::*;
use ux_api::minigfx::{self, op};
use ux_api::platform::{FB_LINES, FB_SIZE, FB_WIDTH_WORDS};
use ux_api::service::api;
use ux_api::service::api::*;
@ -18,9 +18,8 @@ mod poweron_bt;
use poweron_bt as poweron;
mod sleep_note;
use blitstr2::{GlyphStyle, fontmap, glyph_to_height_hint};
use blitstr2::fontmap;
use num_traits::FromPrimitive;
use ux_api::wordwrap::*;
use xous::{MemoryRange, msg_blocking_scalar_unpack, msg_scalar_unpack};
use xous_ipc::Buffer;
@ -28,7 +27,6 @@ use xous_ipc::Buffer;
// only install for hardware targets; hosted mode uses host's panic handler
mod panic;
use core::ops::Add;
use core::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
@ -229,291 +227,13 @@ fn wrapped_main(main_thread_token: backend::MainThreadToken) -> ! {
display.resume();
}),
Some(GfxOpcode::DrawClipObject) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let obj = buffer.to_original::<ClipObject, _>().unwrap();
log::trace!("DrawClipObject {:?}", obj);
match obj.obj {
ClipObjectType::Line(line) => {
op::line(&mut display, line, Some(obj.clip), false);
}
ClipObjectType::XorLine(line) => {
op::line(&mut display, line, Some(obj.clip), true);
}
ClipObjectType::Circ(circ) => {
op::circle(&mut display, circ, Some(obj.clip));
}
ClipObjectType::Rect(rect) => {
op::rectangle(&mut display, rect, Some(obj.clip), false);
}
ClipObjectType::RoundRect(rr) => {
op::rounded_rectangle(&mut display, rr, Some(obj.clip));
}
#[cfg(feature = "ditherpunk")]
ClipObjectType::Tile(tile) => {
op::tile(&mut display, tile, Some(obj.clip));
}
}
minigfx::handlers::draw_clip_object(&mut display, &mut msg);
}
Some(GfxOpcode::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(&mut display, line, Some(obj.clip), false);
}
ClipObjectType::XorLine(line) => {
op::line(&mut display, line, Some(obj.clip), true);
}
ClipObjectType::Circ(circ) => {
op::circle(&mut display, circ, Some(obj.clip));
}
ClipObjectType::Rect(rect) => {
op::rectangle(&mut display, rect, Some(obj.clip), false);
}
ClipObjectType::RoundRect(rr) => {
op::rounded_rectangle(&mut display, rr, Some(obj.clip));
}
#[cfg(feature = "ditherpunk")]
ClipObjectType::Tile(tile) => {
op::tile(&mut display, tile, 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;
}
}
minigfx::handlers::draw_clip_object_list(&mut display, &mut msg);
}
Some(GfxOpcode::DrawTextView) => {
let mut buffer =
unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut tv = buffer.to_original::<TextView, _>().unwrap();
if tv.clip_rect.is_none() {
continue;
} // if no clipping rectangle is specified, nothing to draw
// this is the clipping rectangle of the canvas in screen coordinates
let clip_rect = tv.clip_rect.unwrap();
// this is the translation vector to and from screen space
let screen_offset: Point = tv.clip_rect.unwrap().tl;
let typeset_extent = match tv.bounds_hint {
TextBounds::BoundingBox(r) => Point::new(
r.br().x - r.tl().x - tv.margin.x * 2,
r.br().y - r.tl().y - tv.margin.y * 2,
),
TextBounds::GrowableFromBr(br, width) => {
Point::new(width as isize - tv.margin.x * 2, br.y - tv.margin.y * 2)
}
TextBounds::GrowableFromBl(bl, width) => {
Point::new(width as isize - tv.margin.x * 2, bl.y - tv.margin.y * 2)
}
TextBounds::GrowableFromTl(tl, width) => Point::new(
width as isize - tv.margin.x * 2,
(clip_rect.br().y - clip_rect.tl().y - tl.y) - tv.margin.y * 2,
),
TextBounds::GrowableFromTr(tr, width) => Point::new(
width as isize - tv.margin.x * 2,
(clip_rect.br().y - clip_rect.tl().y - tr.y) - tv.margin.y * 2,
),
TextBounds::CenteredTop(r) => Point::new(
r.br().x - r.tl().x - tv.margin.x * 2,
r.br().y - r.tl().y - tv.margin.y * 2,
),
TextBounds::CenteredBot(r) => Point::new(
r.br().x - r.tl().x - tv.margin.x * 2,
r.br().y - r.tl().y - tv.margin.y * 2,
),
};
let mut typesetter = Typesetter::setup(
tv.to_str(),
&typeset_extent,
&tv.style,
if let Some(i) = tv.insertion { Some(i as usize) } else { None },
);
let composition = typesetter.typeset(if tv.ellipsis {
OverflowStrategy::Ellipsis
} else {
OverflowStrategy::Abort
});
let composition_top_left = match tv.bounds_hint {
TextBounds::BoundingBox(r) => r.tl().add(tv.margin),
TextBounds::GrowableFromBr(br, _width) => Point::new(
br.x - (composition.bb_width() as isize + tv.margin.x),
br.y - (composition.bb_height() as isize + tv.margin.y),
),
TextBounds::GrowableFromBl(bl, _width) => Point::new(
bl.x + tv.margin.x,
bl.y - (composition.bb_height() as isize + tv.margin.y),
),
TextBounds::GrowableFromTl(tl, _width) => tl.add(tv.margin),
TextBounds::GrowableFromTr(tr, _width) => Point::new(
tr.x - (composition.bb_width() as isize + tv.margin.x),
tr.y + tv.margin.y,
),
TextBounds::CenteredTop(r) => {
if r.width() as isize > composition.bb_width() {
r.tl().add(Point::new((r.width() as isize - composition.bb_width()) / 2, 0))
} else {
r.tl().add(tv.margin)
}
}
TextBounds::CenteredBot(r) => {
if r.width() as isize > composition.bb_width() {
r.tl().add(Point::new(
(r.width() as isize - composition.bb_width()) / 2,
if (r.height() as isize) > (composition.bb_height() + tv.margin.y) {
(r.height() as isize) - (composition.bb_height() + tv.margin.y)
} else {
0
},
))
} else {
r.tl().add(tv.margin)
}
}
}
.add(screen_offset);
// compute the clear rectangle -- the border is already in screen coordinates, just add
// the margin around it
let mut clear_rect = match tv.bounds_hint {
TextBounds::BoundingBox(mut r) => {
r.translate(screen_offset);
r
}
_ => {
if tv.busy_animation_state.is_some() {
// we want to clear the entire potentially drawable region, not just the dirty
// box if we're doing a busy animation.
let r = Rectangle::new(
Point::new(screen_offset.x, composition_top_left.y),
Point::new(screen_offset.x, composition_top_left.y)
.add(Point::new(typeset_extent.x, composition.bb_height())),
);
r
} else {
// composition_top_left already had a screen_offset added when it was
// computed. just margin it out
let mut r = Rectangle::new(
composition_top_left,
composition_top_left.add(Point::new(
composition.bb_width() as _,
composition.bb_height() as _,
)),
);
r.margin_out(tv.margin);
r
}
}
};
log::trace!("clip_rect: {:?}", clip_rect);
log::trace!("composition_top_left: {:?}", composition_top_left);
log::trace!("clear_rect: {:?}", clear_rect);
// draw the bubble/border and/or clear the background area
let bordercolor = if tv.draw_border { Some(PixelColor::Dark) } else { None };
let borderwidth: isize = if tv.draw_border { tv.border_width as isize } else { 0 };
let fillcolor = if tv.clear_area || tv.invert {
if tv.invert { Some(PixelColor::Dark) } else { Some(PixelColor::Light) }
} else {
None
};
clear_rect.style = DrawStyle {
fill_color: fillcolor,
stroke_color: bordercolor,
stroke_width: borderwidth,
};
if !tv.dry_run() {
if tv.rounded_border.is_some() {
op::rounded_rectangle(
&mut display,
RoundedRectangle::new(clear_rect, tv.rounded_border.unwrap() as _),
tv.clip_rect,
);
} else {
op::rectangle(&mut display, clear_rect, tv.clip_rect, false);
}
}
// for now, if we're in braille mode, emit all text to the debug log so we can see it
//if cfg!(feature = "braille") {
// log::info!("{}", tv);
//}
if !tv.dry_run() {
// note: make the clip rect `tv.clip_rect.unwrap()` if you want to debug wordwrapping
// artifacts; otherwise smallest_rect masks some problems
let smallest_rect = clear_rect
.clip_with(tv.clip_rect.unwrap())
.unwrap_or(Rectangle::new(Point::new(0, 0), Point::new(0, 0)));
composition.render(&mut display, composition_top_left, tv.invert, smallest_rect);
}
// run the busy animation
if let Some(state) = tv.busy_animation_state {
let total_width = typeset_extent.x as isize;
if total_width > op::BUSY_ANIMATION_RECT_WIDTH * 2 {
let step = state as isize % (op::BUSY_ANIMATION_RECT_WIDTH * 2);
for offset in (0..(total_width + op::BUSY_ANIMATION_RECT_WIDTH * 2))
.step_by((op::BUSY_ANIMATION_RECT_WIDTH * 2) as usize)
{
let left_x = offset + step + composition_top_left.x;
if offset == 0
&& (step >= op::BUSY_ANIMATION_RECT_WIDTH)
&& (step < op::BUSY_ANIMATION_RECT_WIDTH * 2)
{
// handle the truncated "left" rectangle
let mut trunc_rect = Rectangle::new(
Point::new(composition_top_left.x as isize, clear_rect.tl().y),
Point::new(
(step + composition_top_left.x - op::BUSY_ANIMATION_RECT_WIDTH)
as isize,
clear_rect.br().y,
),
);
trunc_rect.style = DrawStyle {
fill_color: Some(PixelColor::Light),
stroke_color: None,
stroke_width: 0,
};
op::rectangle(&mut display, trunc_rect, tv.clip_rect, true);
} // the "right" rectangle is handled by the clipping mask
let mut xor_rect = Rectangle::new(
Point::new(left_x as isize, clear_rect.tl().y),
Point::new(
(left_x + op::BUSY_ANIMATION_RECT_WIDTH) as isize,
clear_rect.br().y,
),
);
xor_rect.style = DrawStyle {
fill_color: Some(PixelColor::Light),
stroke_color: None,
stroke_width: 0,
};
op::rectangle(&mut display, xor_rect, tv.clip_rect, true);
}
} else {
// don't do the animation, this could be abused to create inverted text
}
tv.busy_animation_state = Some(state + 2);
}
// type mismatch for now, replace this with a simple equals once we sort that out
tv.cursor.pt.x = composition.final_cursor().pt.x;
tv.cursor.pt.y = composition.final_cursor().pt.y;
tv.cursor.line_height = composition.final_cursor().line_height;
tv.overflow = Some(composition.final_overflow());
tv.bounds_computed = Some(clear_rect);
log::trace!("cursor ret {:?}, bounds ret {:?}", tv.cursor, tv.bounds_computed);
// pack our data back into the buffer to return
buffer.replace(tv).unwrap();
minigfx::handlers::draw_text_view(&mut display, &mut msg);
}
Some(GfxOpcode::Flush) => {
log::trace!("***gfx flush*** redraw##");
@ -524,42 +244,30 @@ fn wrapped_main(main_thread_token: backend::MainThreadToken) -> ! {
r.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 0);
op::rectangle(&mut display, r, screen_clip.into(), false)
}
Some(GfxOpcode::Line) => msg_scalar_unpack!(msg, p1, p2, style, _, {
let l = Line::new_with_style(Point::from(p1), Point::from(p2), DrawStyle::from(style));
op::line(&mut display, l, screen_clip.into(), false);
}),
Some(GfxOpcode::Rectangle) => msg_scalar_unpack!(msg, tl, br, style, _, {
let r =
Rectangle::new_with_style(Point::from(tl), Point::from(br), DrawStyle::from(style));
op::rectangle(&mut display, r, screen_clip.into(), false);
}),
Some(GfxOpcode::RoundedRectangle) => msg_scalar_unpack!(msg, tl, br, style, r, {
let rr = RoundedRectangle::new(
Rectangle::new_with_style(Point::from(tl), Point::from(br), DrawStyle::from(style)),
r as _,
);
op::rounded_rectangle(&mut display, rr, screen_clip.into());
}),
Some(GfxOpcode::Line) => {
minigfx::handlers::line(&mut display, screen_clip.into(), &mut msg);
}
Some(GfxOpcode::Rectangle) => {
minigfx::handlers::rectangle(&mut display, screen_clip.into(), &mut msg);
}
Some(GfxOpcode::RoundedRectangle) => {
minigfx::handlers::rounded_rectangle(&mut display, screen_clip.into(), &mut msg);
}
#[cfg(feature = "ditherpunk")]
Some(GfxOpcode::Tile) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let bm = buffer.to_original::<Tile, _>().unwrap();
op::tile(&mut display, bm, screen_clip.into());
minigfx::handlers::tile(&mut display, screen_clip.into(), &mut msg);
}
Some(GfxOpcode::Circle) => {
minigfx::handlers::circle(&mut display, screen_clip.into(), &mut msg);
}
Some(GfxOpcode::Circle) => msg_scalar_unpack!(msg, center, radius, style, _, {
let c = Circle::new_with_style(Point::from(center), radius as _, DrawStyle::from(style));
op::circle(&mut display, c, screen_clip.into());
}),
Some(GfxOpcode::ScreenSize) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
let pt = display.screen_size();
xous::return_scalar2(msg.sender, pt.x as usize, pt.y as usize)
.expect("couldn't return ScreenSize request");
}),
Some(GfxOpcode::QueryGlyphProps) => msg_blocking_scalar_unpack!(msg, style, _, _, _, {
let glyph = GlyphStyle::from(style);
xous::return_scalar2(msg.sender, glyph.into(), glyph_to_height_hint(glyph))
.expect("could not return QueryGlyphProps request");
}),
Some(GfxOpcode::QueryGlyphProps) => {
minigfx::handlers::query_glyph_props(&mut msg);
}
Some(GfxOpcode::DrawSleepScreen) => msg_scalar_unpack!(msg, _, _, _, _, {
display.blit_screen(&logo::LOGO_MAP);
display.redraw();