Move
This commit is contained in:
171
server/src/main.rs
Normal file
171
server/src/main.rs
Normal file
@ -0,0 +1,171 @@
|
||||
#![feature(portable_simd)]
|
||||
#![feature(stdarch_arm_neon_intrinsics)]
|
||||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use clap::{Parser, ValueEnum};
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{BufRead, BufReader, Read, Write},
|
||||
net::{TcpListener, TcpStream},
|
||||
process::Command,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
mod reduce;
|
||||
mod rmframes;
|
||||
use rmframes::RMFrames;
|
||||
|
||||
// Parameters only for RM2, firmware version >= 3.7.0.1930
|
||||
|
||||
/// Height (in pixels) of the framebuffer.
|
||||
const HEIGHT: usize = 1404;
|
||||
/// Width (in pixels) of the framebuffer.
|
||||
const WIDTH: usize = 1872;
|
||||
/// How many bytes represent one pixel in the framebuffer.
|
||||
const BYTES_PER_PIXEL: usize = 2;
|
||||
|
||||
const IN_SIZE: usize = WIDTH * HEIGHT * BYTES_PER_PIXEL;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version)]
|
||||
pub struct Opts {
|
||||
#[arg(long, name = "port", short = 'p')]
|
||||
port: usize,
|
||||
|
||||
#[arg(long, name = "format", short = 'f')]
|
||||
format: Format,
|
||||
}
|
||||
|
||||
#[derive(ValueEnum, Debug, Clone, Copy)]
|
||||
pub enum Format {
|
||||
Mono,
|
||||
MonoSimd,
|
||||
Gray8,
|
||||
Gray8Simd,
|
||||
Gray16be,
|
||||
}
|
||||
|
||||
impl Format {
|
||||
fn run<W: Write>(&self, mut src: RMFrames, mut tgt: W) -> Result<()> {
|
||||
const IN_SIZE: usize = WIDTH * HEIGHT * BYTES_PER_PIXEL;
|
||||
|
||||
match self {
|
||||
Self::Gray16be => {
|
||||
let mut buf = [0u8; IN_SIZE];
|
||||
loop {
|
||||
src.read_exact(&mut buf)?;
|
||||
tgt.write_all(&buf[0..IN_SIZE])?;
|
||||
}
|
||||
}
|
||||
|
||||
Self::Gray8 => {
|
||||
const OUT: usize = reduce::gray8::out_size(IN_SIZE);
|
||||
let mut buf = [0u8; IN_SIZE];
|
||||
loop {
|
||||
src.read_exact(&mut buf)?;
|
||||
reduce::gray8::run(&mut buf);
|
||||
tgt.write_all(&buf[0..OUT])?;
|
||||
}
|
||||
}
|
||||
|
||||
Self::Gray8Simd => {
|
||||
const OUT: usize = reduce::gray8_simd::out_size(IN_SIZE);
|
||||
let mut buf = [0u8; IN_SIZE];
|
||||
loop {
|
||||
src.read_exact(&mut buf)?;
|
||||
reduce::gray8_simd::run(&mut buf);
|
||||
tgt.write_all(&buf[0..OUT])?;
|
||||
}
|
||||
}
|
||||
|
||||
Self::Mono => {
|
||||
const OUT: usize = reduce::mono::out_size(IN_SIZE);
|
||||
let mut buf = [0u8; IN_SIZE];
|
||||
loop {
|
||||
src.read_exact(&mut buf)
|
||||
.context("while reading framebuffer")?;
|
||||
reduce::mono::run(&mut buf);
|
||||
tgt.write_all(&buf[0..OUT]).context("while sending data")?;
|
||||
}
|
||||
}
|
||||
|
||||
Self::MonoSimd => {
|
||||
const OUT: usize = reduce::mono_simd::out_size(IN_SIZE);
|
||||
let mut buf = [0u8; IN_SIZE];
|
||||
loop {
|
||||
src.read_exact(&mut buf)?;
|
||||
reduce::mono_simd::run(&mut buf);
|
||||
tgt.write_all(&buf[0..OUT])?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let opts: Opts = Opts::parse();
|
||||
|
||||
let (file, offset) = {
|
||||
let pid = xochitl_pid()?;
|
||||
let offset = rm2_fb_offset(pid)?;
|
||||
let mem = format!("/proc/{}/mem", pid);
|
||||
(mem, offset)
|
||||
};
|
||||
|
||||
let src = RMFrames::init(&file, offset, IN_SIZE)?;
|
||||
let tgt = listen_timeout(opts.port, Duration::from_secs(60))?;
|
||||
|
||||
opts.format.run(src, tgt)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn listen_timeout(port: usize, timeout: Duration) -> Result<TcpStream> {
|
||||
let listen_addr = format!("0.0.0.0:{}", port);
|
||||
let listen = TcpListener::bind(&listen_addr)?;
|
||||
eprintln!("[rM] listening for a TCP connection on {}", listen_addr);
|
||||
|
||||
let (conn, conn_addr) = listen.accept().unwrap();
|
||||
|
||||
eprintln!("[rM] connection received from {}", conn_addr);
|
||||
conn.set_write_timeout(Some(timeout))?;
|
||||
Ok(conn)
|
||||
}
|
||||
|
||||
fn xochitl_pid() -> Result<usize> {
|
||||
let output = Command::new("/bin/pidof")
|
||||
.args(["xochitl"])
|
||||
.output()
|
||||
.context("Failed to run `/bin/pidof xochitl`")?;
|
||||
if output.status.success() {
|
||||
let pid = &output.stdout;
|
||||
let pid_str = std::str::from_utf8(pid)?.trim();
|
||||
pid_str
|
||||
.parse()
|
||||
.with_context(|| format!("Failed to parse xochitl's pid: {}", pid_str))
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"Could not find pid of xochitl, is xochitl running?"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn rm2_fb_offset(pid: usize) -> Result<usize> {
|
||||
let file = File::open(format!("/proc/{}/maps", &pid))?;
|
||||
let line = BufReader::new(file)
|
||||
.lines()
|
||||
.skip_while(|line| matches!(line, Ok(l) if !l.ends_with("/dev/fb0")))
|
||||
.nth(1)
|
||||
.with_context(|| format!("No line containing /dev/fb0 in /proc/{}/maps file", pid))?
|
||||
.with_context(|| format!("Error reading file /proc/{}/maps", pid))?;
|
||||
|
||||
let addr = line
|
||||
.split("-")
|
||||
.next()
|
||||
.with_context(|| format!("Error parsing line in /proc/{}/maps", pid))?;
|
||||
|
||||
let address = usize::from_str_radix(addr, 16).context("Error parsing framebuffer address")?;
|
||||
println!("Found framebuffer at 0x{:X}", address);
|
||||
|
||||
Ok(address + 8)
|
||||
}
|
16
server/src/reduce/gray8.rs
Normal file
16
server/src/reduce/gray8.rs
Normal file
@ -0,0 +1,16 @@
|
||||
const GROUP_BY: usize = 2;
|
||||
pub const fn out_size(in_size: usize) -> usize {
|
||||
in_size / 2
|
||||
}
|
||||
|
||||
pub fn run(buf: &mut [u8]) {
|
||||
let n_raw = buf.len();
|
||||
let mut in_cursor = 0;
|
||||
let mut out_cursor = 0;
|
||||
|
||||
while in_cursor + GROUP_BY <= n_raw {
|
||||
buf[out_cursor] = buf[in_cursor];
|
||||
out_cursor += 1;
|
||||
in_cursor += GROUP_BY;
|
||||
}
|
||||
}
|
33
server/src/reduce/gray8_simd.rs
Normal file
33
server/src/reduce/gray8_simd.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use std::{
|
||||
arch::arm::{vld1q_u8, vst1q_u8, vuzpq_u8},
|
||||
convert::TryInto,
|
||||
};
|
||||
|
||||
const GROUP_BY: usize = 32;
|
||||
pub const fn out_size(in_size: usize) -> usize {
|
||||
in_size / 2
|
||||
}
|
||||
|
||||
pub fn run(buf: &mut [u8]) {
|
||||
let n_raw = buf.len();
|
||||
let mut in_cursor = 0;
|
||||
let mut out_cursor = 0;
|
||||
|
||||
let mut res = [0u8; 16];
|
||||
|
||||
while in_cursor + GROUP_BY <= n_raw {
|
||||
let a: &[u8; 16] = buf[in_cursor..in_cursor + 16].try_into().unwrap();
|
||||
let b: &[u8; 16] = buf[in_cursor + 16..in_cursor + 32].try_into().unwrap();
|
||||
|
||||
unsafe {
|
||||
let a = vld1q_u8(a as *const u8);
|
||||
let b = vld1q_u8(b as *const u8);
|
||||
let z = vuzpq_u8(a, b);
|
||||
vst1q_u8(&mut res as *mut u8, z.0);
|
||||
}
|
||||
|
||||
buf[out_cursor..out_cursor + 16].copy_from_slice(&res);
|
||||
out_cursor += 16;
|
||||
in_cursor += GROUP_BY;
|
||||
}
|
||||
}
|
4
server/src/reduce/mod.rs
Normal file
4
server/src/reduce/mod.rs
Normal file
@ -0,0 +1,4 @@
|
||||
pub mod gray8;
|
||||
pub mod gray8_simd;
|
||||
pub mod mono;
|
||||
pub mod mono_simd;
|
47
server/src/reduce/mono.rs
Normal file
47
server/src/reduce/mono.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use std::convert::TryInto;
|
||||
|
||||
const GROUP_BY: usize = 16;
|
||||
pub const fn out_size(in_size: usize) -> usize {
|
||||
in_size / 16
|
||||
}
|
||||
|
||||
pub fn run(buf: &mut [u8]) {
|
||||
let n_raw = buf.len();
|
||||
let mut in_cursor = 0;
|
||||
let mut out_cursor = 0;
|
||||
|
||||
while in_cursor + GROUP_BY <= n_raw {
|
||||
let a: &[u8; 16] = buf[in_cursor..in_cursor + 16].try_into().unwrap();
|
||||
|
||||
let mut out = 0u8;
|
||||
|
||||
if a[0] == 0x1E {
|
||||
out |= 0b10000000;
|
||||
}
|
||||
if a[2] == 0x1E {
|
||||
out |= 0b10000000 >> 1;
|
||||
}
|
||||
if a[4] == 0x1E {
|
||||
out |= 0b10000000 >> 2;
|
||||
}
|
||||
if a[6] == 0x1E {
|
||||
out |= 0b10000000 >> 3;
|
||||
}
|
||||
if a[8] == 0x1E {
|
||||
out |= 0b10000000 >> 4;
|
||||
}
|
||||
if a[10] == 0x1E {
|
||||
out |= 0b10000000 >> 5;
|
||||
}
|
||||
if a[12] == 0x1E {
|
||||
out |= 0b10000000 >> 6;
|
||||
}
|
||||
if a[14] == 0x1E {
|
||||
out |= 0b10000000 >> 7;
|
||||
}
|
||||
|
||||
buf[out_cursor] = out;
|
||||
out_cursor += 1;
|
||||
in_cursor += GROUP_BY;
|
||||
}
|
||||
}
|
69
server/src/reduce/mono_simd.rs
Normal file
69
server/src/reduce/mono_simd.rs
Normal file
@ -0,0 +1,69 @@
|
||||
use std::{
|
||||
arch::arm::{
|
||||
vandq_u8, vgetq_lane_u64, vld1q_s8, vld1q_u8, vpaddlq_u16, vpaddlq_u32, vpaddlq_u8,
|
||||
vshlq_u8, vshrq_n_u8, vuzpq_u8,
|
||||
},
|
||||
convert::TryInto,
|
||||
};
|
||||
|
||||
const GROUP_BY: usize = 32;
|
||||
pub const fn out_size(in_size: usize) -> usize {
|
||||
in_size / 16
|
||||
}
|
||||
|
||||
pub fn run(buf: &mut [u8]) {
|
||||
let n_raw = buf.len();
|
||||
let mut in_cursor = 0;
|
||||
let mut out_cursor = 0;
|
||||
|
||||
let m = unsafe {
|
||||
let mask = &[0x01u8; 16];
|
||||
vld1q_u8(mask as *const u8)
|
||||
};
|
||||
|
||||
let h = unsafe {
|
||||
let mask = &[
|
||||
0x07i8, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02,
|
||||
0x01, 0x00,
|
||||
];
|
||||
vld1q_s8(mask as *const i8)
|
||||
};
|
||||
|
||||
while in_cursor + GROUP_BY <= n_raw {
|
||||
let a: &[u8; 16] = buf[in_cursor..in_cursor + 16].try_into().unwrap();
|
||||
let b: &[u8; 16] = buf[in_cursor + 16..in_cursor + 32].try_into().unwrap();
|
||||
|
||||
let res: (u8, u8) = unsafe {
|
||||
// Load 32 bytes
|
||||
let a = vld1q_u8(a as *const u8);
|
||||
let b = vld1q_u8(b as *const u8);
|
||||
|
||||
// Unzip and get first byte of each pair
|
||||
let a = vuzpq_u8(a, b).0;
|
||||
|
||||
// White = 0b1110, so >> 4.
|
||||
let a = vshrq_n_u8::<4>(a);
|
||||
|
||||
// and with 0x01 mask
|
||||
let a = vandq_u8(a, m);
|
||||
|
||||
// shift each bit left by an appropriate amount
|
||||
// (h is [0x07, 0x06, .., 0x00, 0x07, .., 0x00])
|
||||
let a = vshlq_u8(a, h);
|
||||
|
||||
// Sum everything
|
||||
let s = vpaddlq_u8(a);
|
||||
let s = vpaddlq_u16(s);
|
||||
let s = vpaddlq_u32(s);
|
||||
(
|
||||
vgetq_lane_u64(s, 0).try_into().unwrap(),
|
||||
vgetq_lane_u64(s, 1).try_into().unwrap(),
|
||||
)
|
||||
};
|
||||
|
||||
buf[out_cursor] = res.0;
|
||||
buf[out_cursor + 1] = res.1;
|
||||
out_cursor += 2;
|
||||
in_cursor += GROUP_BY;
|
||||
}
|
||||
}
|
54
server/src/rmframes.rs
Normal file
54
server/src/rmframes.rs
Normal file
@ -0,0 +1,54 @@
|
||||
use anyhow::Result;
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{Read, Seek, SeekFrom},
|
||||
};
|
||||
|
||||
pub struct RMFrames {
|
||||
file: File,
|
||||
start: u64,
|
||||
cursor: usize,
|
||||
framebuffer_size: usize,
|
||||
}
|
||||
|
||||
impl RMFrames {
|
||||
pub fn init(path: &str, offset: usize, framebuffer_size: usize) -> Result<Self> {
|
||||
let start = offset as u64;
|
||||
let cursor = 0;
|
||||
let file = File::open(path)?;
|
||||
|
||||
let mut streamer = RMFrames {
|
||||
file,
|
||||
start,
|
||||
cursor,
|
||||
framebuffer_size,
|
||||
};
|
||||
streamer.next_frame()?;
|
||||
Ok(streamer)
|
||||
}
|
||||
|
||||
pub fn next_frame(&mut self) -> std::io::Result<()> {
|
||||
self.file.seek(SeekFrom::Start(self.start))?;
|
||||
self.cursor = 0;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for RMFrames {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
let requested = buf.len();
|
||||
let bytes_read = if self.cursor + requested < self.framebuffer_size {
|
||||
self.file.read(buf)?
|
||||
} else {
|
||||
let rest = self.framebuffer_size - self.cursor;
|
||||
self.file.read(&mut buf[0..rest])?
|
||||
};
|
||||
|
||||
self.cursor += bytes_read;
|
||||
if self.cursor == self.framebuffer_size {
|
||||
self.next_frame()?;
|
||||
}
|
||||
|
||||
Ok(bytes_read)
|
||||
}
|
||||
}
|
70
server/src/streamlsb.rs
Normal file
70
server/src/streamlsb.rs
Normal file
@ -0,0 +1,70 @@
|
||||
use std::io::Read;
|
||||
|
||||
const BYTE_FACTOR: usize = 2;
|
||||
|
||||
/// Read every other byte from `R`.
|
||||
///
|
||||
/// If `R` is a stream of u16be, this should return the least-significant byte of each u16.
|
||||
pub struct StreamLSB<R: Read> {
|
||||
/// The source to read from
|
||||
src: R,
|
||||
|
||||
/// Number of filled bits in fragbuf
|
||||
arity: usize,
|
||||
|
||||
/// Hold a partial u16, in case we read half of one
|
||||
fragbuf: [u8; BYTE_FACTOR],
|
||||
}
|
||||
|
||||
impl<R: Read> StreamLSB<R> {
|
||||
pub fn new(src: R) -> Self {
|
||||
Self {
|
||||
src,
|
||||
arity: 0,
|
||||
fragbuf: [0; BYTE_FACTOR],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Read> Read for StreamLSB<R> {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
let n_raw = self.src.read(buf)?;
|
||||
|
||||
let mut in_cursor = 0;
|
||||
let mut out_cursor = 0;
|
||||
|
||||
if self.arity != 0 {
|
||||
// Number of bytes we need to read
|
||||
let need_read = BYTE_FACTOR - self.arity;
|
||||
let can_read = need_read.min(buf.len());
|
||||
self.fragbuf[self.arity..self.arity + can_read].copy_from_slice(&buf[0..can_read]);
|
||||
|
||||
in_cursor += can_read;
|
||||
self.arity += can_read;
|
||||
self.arity %= BYTE_FACTOR;
|
||||
|
||||
if self.arity != 0 {
|
||||
// We did not read a full set of bytes,
|
||||
// wait for more data.
|
||||
return Ok(0);
|
||||
} else {
|
||||
// Extract least-significant byte
|
||||
buf[0] = self.fragbuf[0];
|
||||
out_cursor += 1;
|
||||
}
|
||||
}
|
||||
|
||||
for _ in in_cursor..(n_raw / BYTE_FACTOR) {
|
||||
// Extract least-significant byte
|
||||
buf[out_cursor] = buf[in_cursor];
|
||||
|
||||
in_cursor += BYTE_FACTOR;
|
||||
out_cursor += 1;
|
||||
}
|
||||
|
||||
self.arity = n_raw - in_cursor;
|
||||
self.fragbuf[0..self.arity].copy_from_slice(&buf[in_cursor..n_raw]);
|
||||
|
||||
return Ok(out_cursor);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user