refactor TextView into graphics-server

This commit is contained in:
bunnie 2021-01-19 04:54:54 +08:00
parent b72d2f2bb3
commit 086a4da862
11 changed files with 185 additions and 140 deletions

4
Cargo.lock generated
View file

@ -263,8 +263,6 @@ dependencies = [
"blitstr 0.3.0",
"com",
"graphics-server",
"hash32",
"hash32-derive",
"heapless",
"keyboard",
"log",
@ -320,6 +318,8 @@ name = "graphics-server"
version = "0.1.0"
dependencies = [
"blitstr 0.3.0",
"hash32",
"hash32-derive",
"keyboard",
"log",
"log-server",

View file

@ -16,9 +16,6 @@ keyboard = { path = "../keyboard" }
trng = { path = "../trng" }
heapless = "0.5"
log = "0.4"
hash32 = "0.1.0"
hash32-derive = "0.1.0"
com = { path = "../com"}
blitstr = { path = "../../../blitstr" }

View file

@ -1,135 +1,6 @@
use xous::{Message, ScalarMessage};
use graphics_server::{Point, Rectangle};
use graphics_server::{Point, Rectangle, TextView, Gid};
use blitstr::{GlyphStyle, Cursor};
use hash32::{Hash, Hasher};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Gid {
gid: [u32; 4],
}
impl Gid {
pub fn new(id: [u32; 4]) -> Self { Gid{gid: id} }
}
impl hash32::Hash for Gid {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
Hash::hash(&self.gid[..], state);
}
}
#[derive(Debug, Copy, Clone)]
pub enum TextAlignment {
Left,
Center,
Right,
}
#[derive(Debug, Copy, Clone)]
pub enum TextBounds {
// fixed width and height in a rectangle
BoundingBox(Rectangle),
// fixed width, grows up from bottom right
GrowableFromBr(Point, u16),
// fixed width, grows down from top left
GrowableFromTl(Point, u16),
// fixed width, grows up from bottom left
GrowableFromBl(Point, u16),
}
#[derive(Debug, Copy, Clone)]
// operations that may be requested of a TextView when sent to GAM
pub enum TextOp {
Nop,
Render,
ComputeBounds,
}
impl Into<usize> for TextOp {
fn into(self) -> usize {
match self {
TextOp::Nop => 0,
TextOp::Render => 1,
TextOp::ComputeBounds => 2,
}
}
}
impl From<usize> for TextOp {
fn from(code: usize) -> Self {
match code {
1 => TextOp::Render,
2 => TextOp::ComputeBounds,
_ => TextOp::Nop,
}
}
}
#[repr(C)]
pub struct TextView<'a> {
operation: TextOp,
pub untrusted: bool, // render content with random stipples to indicate the strings within are untrusted
pub token: Option<[u32; 4]>, // optional 128-bit token which is presented to prove a field's trustability
pub invert: bool, // only trusted, token-validated TextViews will have the invert bit respected
// lower numbers are drawn last
pub draw_order: usize,
// offsets for text drawing -- exactly one of the following options should be specified
pub bounds_hint: TextBounds,
pub bounds_computed: Option<Rectangle>, // is Some(Rectangle) if bounds have been computed and text has not been modified
pub style: GlyphStyle,
pub text: xous::String<'a>,
pub alignment: TextAlignment,
pub draw_border: bool,
pub clear_area: bool,
pub border_width: u16,
pub rounded_border: bool,
pub x_margin: u16,
pub y_margin: u16,
// this field specifies the beginning and end of a "selected" region of text
pub selected: Option<[usize; 2]>,
canvas: Gid, // GID of the canvas to draw on
}
impl<'a> TextView<'a> {
pub fn new(canvas: Gid, maxlen: usize, draw_order: usize, bounds_hint: TextBounds) -> Self {
TextView {
operation: TextOp::Nop,
untrusted: true,
token: None,
invert: false,
draw_order,
bounds_hint,
bounds_computed: None,
style: GlyphStyle::Regular,
text: xous::String::new(maxlen),
alignment: TextAlignment::Left,
draw_border: true,
border_width: 1,
rounded_border: false,
x_margin: 4,
y_margin: 4,
selected: None,
canvas,
clear_area: true,
}
}
pub fn set_op(&mut self, op: TextOp) { self.operation = op; }
pub fn get_op(&self) -> TextOp { self.operation }
pub fn get_canvas_gid(&self) -> Gid { self.canvas }
}
impl<'a> core::fmt::Debug for TextView<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// this should definitely be extended to print more relevant data, but for now just render the string itself
write!(f, "{}", self.text)
}
}
#[derive(Debug)]
pub enum Opcode<'a> {
@ -169,7 +40,7 @@ impl<'a> Into<Message> for Opcode<'a> {
fn into(self) -> Message {
match self {
Opcode::ClearCanvas(gid) => Message::Scalar(ScalarMessage {
id: 0, arg1: gid.gid[0] as _, arg2: gid.gid[1] as _, arg3: gid.gid[2] as _, arg4: gid.gid[3] as _
id: 0, arg1: gid.gid()[0] as _, arg2: gid.gid()[1] as _, arg3: gid.gid()[2] as _, arg4: gid.gid()[3] as _
}),
_ => panic!("GAM api: Opcode type not handled by Into(), maybe you meant to use a helper method?"),
}

View file

@ -3,7 +3,7 @@ use heapless::binary_heap::{BinaryHeap, Max};
use heapless::FnvIndexMap;
use heapless::Vec;
use heapless::consts::*;
use graphics_server::{Rectangle, Point};
use graphics_server::*;
use xous::ipc::Sendable;
use log::{error, info};

View file

@ -5,6 +5,7 @@ use api::*;
use xous::ipc::*;
use core::fmt::Write;
use graphics_server::api::{TextOp, TextView};
pub fn draw_textview(gam_cid: xous::CID, tv: &mut TextView) -> Result<(), xous::Error> {
let mut sendable_tv = Sendable::new(tv).expect("can't create sendable textview");

View file

@ -11,7 +11,7 @@ use canvas::*;
use blitstr::{Cursor, GlyphStyle};
use com::*;
use core::fmt::Write;
use graphics_server::{Circle, DrawStyle, Line, PixelColor, Point, Rectangle};
use graphics_server::*;
use log::{error, info};
use xous::String;
@ -116,6 +116,7 @@ fn tv_draw(gfx_conn: xous::CID, trng_conn: xous::CID, canvases: &mut FnvIndexMap
// first, figure out if we should even be drawing to this canvas.
if canvas.is_drawable() {
// figure out text bounds: figure out how wide our text is, to start with.
graphics_server::set_string_clipping(gfx_conn, canvas.clip_rect().into()).expect("GAM: can't set clipping");
} else {
info!("GAM: attempt to draw TextView on non-drawable canvas. Not fatal, but request ignored.");

View file

@ -2,7 +2,7 @@ use crate::api::*;
use log::{error, info};
use com::api::BattStats;
use graphics_server::{Point, Rectangle};
use graphics_server::*;
use core::fmt::Write;
use core::convert::TryFrom;

View file

@ -11,6 +11,8 @@ log-server = { path = "../log-server" }
xous-names = { path = "../xous-names" }
log = "0.4"
keyboard = { path = "../keyboard" }
hash32 = "0.1.0"
hash32-derive = "0.1.0"
# blitstr = { git = "https://github.com/betrusted-io/blitstr.git", rev = "ea27e611d5ee165d8be894f9ca1c1130156c1931" }
blitstr = { path = "../../../blitstr" }

View file

@ -17,6 +17,7 @@ use blitstr::{ClipRect, Cursor, GlyphStyle};
use core::cmp::{max, min};
use core::ops::{Add, AddAssign, Index, Neg, Sub, SubAssign};
use xous::{Message, ScalarMessage};
use hash32::{Hash, Hasher};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum PixelColor {
@ -477,6 +478,9 @@ pub enum Opcode<'a> {
/// gets info about the current glyph to assist with layout
QueryGlyphProps(GlyphStyle),
/// draws a textview
TextView(&'a TextView<'a>),
}
impl<'a> core::convert::TryFrom<&'a Message> for Opcode<'a> {
@ -552,6 +556,15 @@ impl<'a> core::convert::TryFrom<&'a Message> for Opcode<'a> {
},
_ => Err("unrecognized opcode"),
},
Message::MutableBorrow(m) => match m.id {
0x100 => {
let tv: &mut TextView = unsafe {
&mut *(m.buf.as_mut_ptr() as *mut TextView)
};
Ok(Opcode::TextView(tv))
},
_ => Err("unrecognized opcode"),
}
_ => Err("unhandled message type"),
}
}
@ -656,6 +669,138 @@ impl<'a> Into<Message> for Opcode<'a> {
arg3: 0,
arg4: 0,
}),
_ => panic!("GFX api: Opcode type not handled by Into(), maybe you meant to use a helper method?"),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Gid {
gid: [u32; 4],
}
impl Gid {
pub fn new(id: [u32; 4]) -> Self { Gid{gid: id} }
pub fn gid(&self) -> [u32; 4] { self.gid }
}
impl hash32::Hash for Gid {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
Hash::hash(&self.gid[..], state);
}
}
#[derive(Debug, Copy, Clone)]
pub enum TextAlignment {
Left,
Center,
Right,
}
#[derive(Debug, Copy, Clone)]
pub enum TextBounds {
// fixed width and height in a rectangle
BoundingBox(Rectangle),
// fixed width, grows up from bottom right
GrowableFromBr(Point, u16),
// fixed width, grows down from top left
GrowableFromTl(Point, u16),
// fixed width, grows up from bottom left
GrowableFromBl(Point, u16),
}
#[derive(Debug, Copy, Clone)]
// operations that may be requested of a TextView when sent to GAM
pub enum TextOp {
Nop,
Render,
ComputeBounds,
}
impl Into<usize> for TextOp {
fn into(self) -> usize {
match self {
TextOp::Nop => 0,
TextOp::Render => 1,
TextOp::ComputeBounds => 2,
}
}
}
impl From<usize> for TextOp {
fn from(code: usize) -> Self {
match code {
1 => TextOp::Render,
2 => TextOp::ComputeBounds,
_ => TextOp::Nop,
}
}
}
#[repr(C)]
pub struct TextView<'a> {
operation: TextOp,
pub untrusted: bool, // render content with random stipples to indicate the strings within are untrusted
pub token: Option<[u32; 4]>, // optional 128-bit token which is presented to prove a field's trustability
pub invert: bool, // only trusted, token-validated TextViews will have the invert bit respected
// lower numbers are drawn last
pub draw_order: usize,
// offsets for text drawing -- exactly one of the following options should be specified
pub bounds_hint: TextBounds,
pub bounds_computed: Option<Rectangle>, // is Some(Rectangle) if bounds have been computed and text has not been modified
pub style: GlyphStyle,
pub text: xous::String<'a>,
pub alignment: TextAlignment,
pub cursor: blitstr::Cursor,
pub draw_border: bool,
pub clear_area: bool,
pub border_width: u16,
pub rounded_border: bool,
pub x_margin: u16,
pub y_margin: u16,
// this field specifies the beginning and end of a "selected" region of text
pub selected: Option<[usize; 2]>,
canvas: Gid, // GID of the canvas to draw on
}
impl<'a> TextView<'a> {
pub fn new(canvas: Gid, maxlen: usize, draw_order: usize, bounds_hint: TextBounds) -> Self {
TextView {
operation: TextOp::Nop,
untrusted: true,
token: None,
invert: false,
draw_order,
bounds_hint,
bounds_computed: None,
style: GlyphStyle::Regular,
text: xous::String::new(maxlen),
alignment: TextAlignment::Left,
cursor: blitstr::Cursor::new(0,0,0),
draw_border: true,
border_width: 1,
rounded_border: false,
x_margin: 4,
y_margin: 4,
selected: None,
canvas,
clear_area: true,
}
}
pub fn set_op(&mut self, op: TextOp) { self.operation = op; }
pub fn get_op(&self) -> TextOp { self.operation }
pub fn get_canvas_gid(&self) -> Gid { self.canvas }
}
impl<'a> core::fmt::Debug for TextView<'a> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// this should definitely be extended to print more relevant data, but for now just render the string itself
write!(f, "{}", self.text)
}
}

View file

@ -2,9 +2,10 @@
// pub mod size;
pub mod api;
pub use api::{Circle, DrawStyle, Line, PixelColor, Point, Rectangle};
pub use api::{Circle, DrawStyle, Line, PixelColor, Point, Rectangle, TextView, TextBounds, Gid, TextOp, TextAlignment};
pub use blitstr::{ClipRect, Cursor, GlyphStyle};
use xous::String;
use xous::ipc::*;
pub mod op;
use xous::{send_message, CID};
@ -25,14 +26,23 @@ pub fn flush(cid: CID) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::Flush.into()).map(|_| ())
}
#[deprecated(
note = "Please use draw_textview for atomic text updates"
)]
pub fn set_string_clipping(cid: CID, r: ClipRect) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::SetStringClipping(r).into()).map(|_| ())
}
#[deprecated(
note = "Please use draw_textview for atomic text updates"
)]
pub fn set_cursor(cid: CID, c: Cursor) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::SetCursor(c).into()).map(|_| ())
}
#[deprecated(
note = "Please use draw_textview for atomic text updates"
)]
pub fn get_cursor(cid: CID) -> Result<Cursor, xous::Error> {
let response = send_message(cid, api::Opcode::GetCursor.into())?;
if let xous::Result::Scalar2(pt_as_usize, h) = response {
@ -43,10 +53,16 @@ pub fn get_cursor(cid: CID) -> Result<Cursor, xous::Error> {
}
}
#[deprecated(
note = "Please use draw_textview for atomic text updates"
)]
pub fn draw_string(cid: CID, s: &String) -> Result<(), xous::Error> {
s.lend(cid, 1).map(|_| ())
}
#[deprecated(
note = "Please use draw_textview for atomic text updates"
)]
pub fn set_glyph_style(cid: CID, glyph: GlyphStyle) -> Result<(), xous::Error> {
send_message(cid, api::Opcode::SetGlyphStyle(glyph).into()).map(|_| ())
}
@ -60,6 +76,9 @@ pub fn screen_size(cid: CID) -> Result<Point, xous::Error> {
}
}
#[deprecated(
note = "Please use draw_textview for atomic text updates"
)]
pub fn query_glyph(cid: CID) -> Result<(GlyphStyle, usize), xous::Error> {
let response = send_message(cid, api::Opcode::QueryGlyphStyle.into())?;
if let xous::Result::Scalar2(glyph, h) = response {
@ -77,3 +96,9 @@ pub fn glyph_height_hint(cid: CID, glyph: GlyphStyle) -> Result<usize, xous::Err
panic!("unexpected return value: {:#?}", response);
}
}
pub fn draw_textview(cid: CID, tv: TextView) -> Result<(), xous::Error> {
let mut sendable_tv = Sendable::new(tv)
.expect("can't create sendable TextView structure");
sendable_tv.lend(cid, 0x100).map(|_| ())
}

View file

@ -131,6 +131,9 @@ fn xmain() -> ! {
)
.expect("GFX: could not return QueryGlyphProps request");
}
Opcode::TextView(tv) => {
}
}
} else {
error!("Couldn't convert opcode");