v2
This commit is contained in:
74
trekstor/src/framebuffer/ffi.rs
Normal file
74
trekstor/src/framebuffer/ffi.rs
Normal file
@@ -0,0 +1,74 @@
|
||||
//! Kernel ABI: framebuffer + Kindle EPDC structs and ioctl numbers.
|
||||
|
||||
// Standard Linux framebuffer ioctl numbers (see <linux/fb.h>).
|
||||
// Typed as c_ulong (not libc::Ioctl) so the crate still type-checks on
|
||||
// non-Linux dev hosts where libc::Ioctl isn't defined, like macos
|
||||
pub(super) const FBIOGET_VSCREENINFO: libc::c_ulong = 0x4600;
|
||||
pub(super) const FBIOGET_FSCREENINFO: libc::c_ulong = 0x4602;
|
||||
|
||||
// These structs mirror the kernel's `fb_var_screeninfo` and `fb_fix_screeninfo`.
|
||||
// We only read from them, fields we care about are `xres`, `yres` (visible
|
||||
// resolution) and `line_length` (stride in bytes per row, which may be larger
|
||||
// than xres due to alignment padding).
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Default)]
|
||||
pub(super) struct FbBitfield {
|
||||
pub(super) offset: u32,
|
||||
pub(super) length: u32,
|
||||
pub(super) msb_right: u32,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Default)]
|
||||
pub(super) struct FbVarScreeninfo {
|
||||
pub(super) xres: u32,
|
||||
pub(super) yres: u32,
|
||||
pub(super) xres_virtual: u32,
|
||||
pub(super) yres_virtual: u32,
|
||||
pub(super) xoffset: u32,
|
||||
pub(super) yoffset: u32,
|
||||
pub(super) bits_per_pixel: u32,
|
||||
pub(super) grayscale: u32,
|
||||
pub(super) red: FbBitfield,
|
||||
pub(super) green: FbBitfield,
|
||||
pub(super) blue: FbBitfield,
|
||||
pub(super) transp: FbBitfield,
|
||||
pub(super) nonstd: u32,
|
||||
pub(super) activate: u32,
|
||||
pub(super) height: u32,
|
||||
pub(super) width: u32,
|
||||
pub(super) accel_flags: u32,
|
||||
pub(super) pixclock: u32,
|
||||
pub(super) left_margin: u32,
|
||||
pub(super) right_margin: u32,
|
||||
pub(super) upper_margin: u32,
|
||||
pub(super) lower_margin: u32,
|
||||
pub(super) hsync_len: u32,
|
||||
pub(super) vsync_len: u32,
|
||||
pub(super) sync: u32,
|
||||
pub(super) vmode: u32,
|
||||
pub(super) rotate: u32,
|
||||
pub(super) colorspace: u32,
|
||||
pub(super) reserved: [u32; 4],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Default)]
|
||||
pub(super) struct FbFixScreeninfo {
|
||||
pub(super) id: [u8; 16],
|
||||
pub(super) smem_start: libc::c_ulong,
|
||||
pub(super) smem_len: u32,
|
||||
pub(super) type_: u32,
|
||||
pub(super) type_aux: u32,
|
||||
pub(super) visual: u32,
|
||||
pub(super) xpanstep: u16,
|
||||
pub(super) ypanstep: u16,
|
||||
pub(super) ywrapstep: u16,
|
||||
pub(super) line_length: u32,
|
||||
pub(super) mmio_start: libc::c_ulong,
|
||||
pub(super) mmio_len: u32,
|
||||
pub(super) accel: u32,
|
||||
pub(super) capabilities: u16,
|
||||
pub(super) reserved: [u16; 2],
|
||||
}
|
||||
135
trekstor/src/framebuffer/mod.rs
Normal file
135
trekstor/src/framebuffer/mod.rs
Normal file
@@ -0,0 +1,135 @@
|
||||
mod ffi;
|
||||
|
||||
use std::os::fd::AsRawFd;
|
||||
|
||||
use ffi::{FBIOGET_FSCREENINFO, FBIOGET_VSCREENINFO, FbFixScreeninfo, FbVarScreeninfo};
|
||||
|
||||
/// Memory-mapped handle to the Kindle's e-ink framebuffer.
|
||||
///
|
||||
/// Pixel format is 8-bit grayscale (one byte per pixel). The `stride` may be
|
||||
/// wider than `width` due to hardware alignment requirements.
|
||||
pub(crate) struct Framebuffer {
|
||||
map: *mut u8,
|
||||
len: usize,
|
||||
pub(crate) width: u32,
|
||||
pub(crate) height: u32,
|
||||
}
|
||||
|
||||
// SAFETY: The mmap is process-wide and we only access it from the event loop thread.
|
||||
unsafe impl Send for Framebuffer {}
|
||||
|
||||
impl Framebuffer {
|
||||
/// Open the framebuffer device and query its geometry from the kernel.
|
||||
///
|
||||
/// This works on any Kindle model - the resolution and stride are read at
|
||||
/// runtime rather than being hardcoded.
|
||||
pub(crate) fn open() -> std::io::Result<Self> {
|
||||
let file = std::fs::OpenOptions::new()
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open("/dev/fb0")?;
|
||||
|
||||
let fd = file.as_raw_fd();
|
||||
|
||||
let mut vinfo = FbVarScreeninfo::default();
|
||||
if unsafe {
|
||||
libc::ioctl(
|
||||
fd,
|
||||
FBIOGET_VSCREENINFO as _,
|
||||
&mut vinfo as *mut _ as *mut libc::c_void,
|
||||
)
|
||||
} == -1
|
||||
{
|
||||
return Err(std::io::Error::last_os_error());
|
||||
}
|
||||
|
||||
let mut finfo = FbFixScreeninfo::default();
|
||||
if unsafe {
|
||||
libc::ioctl(
|
||||
fd,
|
||||
FBIOGET_FSCREENINFO as _,
|
||||
&mut finfo as *mut _ as *mut libc::c_void,
|
||||
)
|
||||
} == -1
|
||||
{
|
||||
return Err(std::io::Error::last_os_error());
|
||||
}
|
||||
|
||||
let width = vinfo.xres;
|
||||
let height = vinfo.yres;
|
||||
let stride = finfo.line_length as usize;
|
||||
|
||||
// The whole render path treats the mmap as four bytes per pixel. A
|
||||
// different depth would silently produce garbled output, so reject it
|
||||
// with a clear error instead.
|
||||
if vinfo.bits_per_pixel != 32 {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
format!(
|
||||
"unsupported framebuffer depth: {} bpp (expected 8-bit grayscale)",
|
||||
vinfo.bits_per_pixel
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
if width == 0 || height == 0 || stride < width as usize {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
format!("invalid framebuffer geometry: {width}x{height}, stride={stride}"),
|
||||
));
|
||||
}
|
||||
|
||||
let len = stride * height as usize;
|
||||
|
||||
let map = unsafe {
|
||||
libc::mmap(
|
||||
std::ptr::null_mut(),
|
||||
len,
|
||||
libc::PROT_READ | libc::PROT_WRITE,
|
||||
libc::MAP_SHARED,
|
||||
fd,
|
||||
0,
|
||||
)
|
||||
};
|
||||
|
||||
if map == libc::MAP_FAILED {
|
||||
return Err(std::io::Error::last_os_error());
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
map: map as *mut u8,
|
||||
len,
|
||||
width,
|
||||
height,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn write_buffer(
|
||||
&mut self,
|
||||
pixels: &[slint::platform::software_renderer::Rgb565Pixel],
|
||||
) {
|
||||
let dst = unsafe { std::slice::from_raw_parts_mut(self.map as *mut u32, pixels.len()) };
|
||||
|
||||
for (dst, src) in dst.iter_mut().zip(pixels) {
|
||||
let c = src.0; // assuming Rgb565Pixel(pub u16)
|
||||
|
||||
let r5 = ((c >> 11) & 0x1f) as u8;
|
||||
let g6 = ((c >> 5) & 0x3f) as u8;
|
||||
let b5 = (c & 0x1f) as u8;
|
||||
|
||||
// Expand to 8 bits.
|
||||
let r = (r5 << 3) | (r5 >> 2);
|
||||
let g = (g6 << 2) | (g6 >> 4);
|
||||
let b = (b5 << 3) | (b5 >> 2);
|
||||
|
||||
// BGRA8888
|
||||
*dst = u32::from_le_bytes([b, g, r, 0xff]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Framebuffer {
|
||||
fn drop(&mut self) {
|
||||
unsafe { libc::munmap(self.map.cast::<libc::c_void>(), self.len) };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user