remove pixels, add softbuffer for drawing to window

This commit is contained in:
Vinzenz Schroeter 2025-01-28 20:54:06 +01:00
parent 205cd566cb
commit 0aa66e3ebd
7 changed files with 268 additions and 747 deletions

843
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -10,19 +10,17 @@ license = "GPL-3.0-or-later"
log = "0.4" log = "0.4"
env_logger = "0.11" env_logger = "0.11"
clap = { version = "4.5", features = ["derive"] } clap = { version = "4.5", features = ["derive"] }
# for drawing pixels onto the surface of the window
pixels = "0.15"
# I should not need this as a direct dependency, but then I cannot spell the types needed to use font-kit...
pathfinder_geometry = "0.5.1"
font-kit = "0.14.2"
thiserror = "2.0" thiserror = "2.0"
[dependencies.servicepoint] # package parsing
version = "0.13.0" servicepoint = { version = "0.13.0", features = ["all_compressions"] }
features = ["all_compressions"]
[dependencies.winit] # font rendering
version = "0.30" font-kit = "0.14.2"
features = ["rwh_05"] # I should not need this as a direct dependency, but then I cannot spell the types needed to use font-kit...
default-features = true pathfinder_geometry = "0.5.1"
# for opening a window
winit = "0.30.8"
# for drawing pixels onto the surface of the window
softbuffer = "0.4.6"

View file

@ -52,12 +52,11 @@ impl FontRenderer8x8 {
assert_eq!(canvas.pixels.len(), TILE_SIZE * TILE_SIZE); assert_eq!(canvas.pixels.len(), TILE_SIZE * TILE_SIZE);
assert_eq!(canvas.stride, TILE_SIZE); assert_eq!(canvas.stride, TILE_SIZE);
let fallback_char = font.glyph_for_char(Self::FALLBACK_CHAR); let fallback_char = font.glyph_for_char(Self::FALLBACK_CHAR);
let result = Self { Self {
font: SendFont(font), font: SendFont(font),
fallback_char, fallback_char,
canvas: Mutex::new(canvas), canvas: Mutex::new(canvas),
}; }
result
} }
pub fn from_name(family_name: String) -> Self { pub fn from_name(family_name: String) -> Self {
@ -118,7 +117,7 @@ impl FontRenderer8x8 {
.as_ref() .as_ref()
.glyph_for_char(char) .glyph_for_char(char)
.or(self.fallback_char) .or(self.fallback_char)
.ok_or_else(|| GlyphNotFound(char)) .ok_or(GlyphNotFound(char))
} }
} }

View file

@ -1,26 +1,22 @@
use std::slice::ChunksExactMut; use std::{sync::mpsc::Sender, sync::RwLock};
use std::sync::mpsc::Sender;
use std::sync::RwLock;
use log::{info, warn}; use log::{info, warn};
use pixels::{Pixels, SurfaceTexture};
use servicepoint::*; use servicepoint::*;
use winit::application::ApplicationHandler; use winit::{
use winit::dpi::LogicalSize; application::ApplicationHandler, dpi::LogicalSize, event::WindowEvent,
use winit::event::WindowEvent; event_loop::ActiveEventLoop, keyboard::KeyCode::KeyC, window::WindowId,
use winit::event_loop::ActiveEventLoop; };
use winit::keyboard::KeyCode::KeyC;
use winit::window::{Window, WindowId};
use crate::cli::GuiOptions; use crate::cli::GuiOptions;
use crate::gui_window::GuiWindow;
pub struct Gui<'t> { pub struct Gui<'t> {
display: &'t RwLock<Bitmap>, display: &'t RwLock<Bitmap>,
luma: &'t RwLock<BrightnessGrid>, luma: &'t RwLock<BrightnessGrid>,
window: Option<Window>,
stop_udp_tx: Sender<()>, stop_udp_tx: Sender<()>,
options: GuiOptions, options: GuiOptions,
logical_size: LogicalSize<u16>, logical_size: LogicalSize<u16>,
window: Option<GuiWindow>,
} }
const SPACER_HEIGHT: usize = 4; const SPACER_HEIGHT: usize = 4;
@ -28,7 +24,7 @@ const NUM_SPACERS: usize = (PIXEL_HEIGHT / TILE_SIZE) - 1;
const PIXEL_HEIGHT_WITH_SPACERS: usize = const PIXEL_HEIGHT_WITH_SPACERS: usize =
PIXEL_HEIGHT + NUM_SPACERS * SPACER_HEIGHT; PIXEL_HEIGHT + NUM_SPACERS * SPACER_HEIGHT;
const OFF_COLOR: [u8; 4] = [0u8, 0, 0, 255]; const OFF_COLOR: u32 = u32::from_ne_bytes([0u8, 0, 0, 0]);
#[derive(Debug)] #[derive(Debug)]
pub enum AppEvents { pub enum AppEvents {
@ -43,7 +39,7 @@ impl<'t> Gui<'t> {
stop_udp_tx: Sender<()>, stop_udp_tx: Sender<()>,
options: GuiOptions, options: GuiOptions,
) -> Self { ) -> Self {
Gui { Self {
window: None, window: None,
logical_size: Self::get_logical_size(options.spacers), logical_size: Self::get_logical_size(options.spacers),
display, display,
@ -54,31 +50,14 @@ impl<'t> Gui<'t> {
} }
fn draw(&mut self) { fn draw(&mut self) {
let window = self.window.as_ref().unwrap();
let window_size = window.inner_size();
let surface_texture =
SurfaceTexture::new(window_size.width, window_size.height, &window);
// TODO: fix pixels: creating a new instance per draw crashes after some time on macOS,
// but keeping one instance for the lifetime of the Gui SIGSEGVs on Wayland when entering a background state.
let mut pixels = Pixels::new(
self.logical_size.width as u32,
self.logical_size.height as u32,
surface_texture,
)
.unwrap();
let mut frame = pixels.frame_mut().chunks_exact_mut(4);
self.draw_frame(&mut frame);
pixels.render().expect("could not render");
}
fn draw_frame(&self, frame: &mut ChunksExactMut<u8>) {
let display = self.display.read().unwrap(); let display = self.display.read().unwrap();
let luma = self.luma.read().unwrap(); let luma = self.luma.read().unwrap();
let brightness_scale = let brightness_scale =
(u8::MAX as f32) / (u8::from(Brightness::MAX) as f32); (u8::MAX as f32) / (u8::from(Brightness::MAX) as f32);
let mut buffer = self.window.as_mut().unwrap().get_buffer();
let mut frame = buffer.iter_mut();
for tile_y in 0..TILE_HEIGHT { for tile_y in 0..TILE_HEIGHT {
if self.options.spacers && tile_y != 0 { if self.options.spacers && tile_y != 0 {
// cannot just frame.skip(PIXEL_WIDTH as usize * SPACER_HEIGHT as usize) because of typing // cannot just frame.skip(PIXEL_WIDTH as usize * SPACER_HEIGHT as usize) because of typing
@ -93,7 +72,8 @@ impl<'t> Gui<'t> {
let brightness = u8::from(luma.get(tile_x, tile_y)); let brightness = u8::from(luma.get(tile_x, tile_y));
let brightness = let brightness =
(brightness_scale * brightness as f32) as u8; (brightness_scale * brightness as f32) as u8;
let on_color = self.get_on_color(brightness); let on_color =
Self::get_on_color(&self.options, brightness);
let start_x = tile_x * TILE_SIZE; let start_x = tile_x * TILE_SIZE;
for x in start_x..start_x + TILE_SIZE { for x in start_x..start_x + TILE_SIZE {
let color = if display.get(x, y) { let color = if display.get(x, y) {
@ -101,21 +81,22 @@ impl<'t> Gui<'t> {
} else { } else {
OFF_COLOR OFF_COLOR
}; };
let pixel = frame.next().unwrap(); *frame.next().unwrap() = color;
pixel.copy_from_slice(&color);
} }
} }
} }
} }
buffer.present().unwrap();
} }
fn get_on_color(&self, brightness: u8) -> [u8; 4] { fn get_on_color(options: &GuiOptions, brightness: u8) -> u32 {
[ u32::from_ne_bytes([
if self.options.red { brightness } else { 0u8 }, if options.blue { brightness } else { 0u8 },
if self.options.green { brightness } else { 0u8 }, if options.green { brightness } else { 0u8 },
if self.options.blue { brightness } else { 0u8 }, if options.red { brightness } else { 0u8 },
255, 0,
] ])
} }
fn get_logical_size(spacers: bool) -> LogicalSize<u16> { fn get_logical_size(spacers: bool) -> LogicalSize<u16> {
@ -128,15 +109,9 @@ impl<'t> Gui<'t> {
} }
} }
impl ApplicationHandler<AppEvents> for Gui<'_> { impl<'t> ApplicationHandler<AppEvents> for Gui<'t> {
fn resumed(&mut self, event_loop: &ActiveEventLoop) { fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let attributes = Window::default_attributes() self.window = Some(GuiWindow::new(event_loop, self.logical_size));
.with_title("servicepoint-simulator")
.with_inner_size(self.logical_size)
.with_transparent(false);
let window = event_loop.create_window(attributes).unwrap();
self.window = Some(window);
} }
fn user_event(&mut self, event_loop: &ActiveEventLoop, event: AppEvents) { fn user_event(&mut self, event_loop: &ActiveEventLoop, event: AppEvents) {
@ -162,7 +137,6 @@ impl ApplicationHandler<AppEvents> for Gui<'_> {
match event { match event {
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
warn!("window event close requested"); warn!("window event close requested");
self.window = None;
let _ = self.stop_udp_tx.send(()); // try to stop udp thread let _ = self.stop_udp_tx.send(()); // try to stop udp thread
event_loop.exit(); event_loop.exit();
} }
@ -179,4 +153,8 @@ impl ApplicationHandler<AppEvents> for Gui<'_> {
_ => {} _ => {}
} }
} }
fn suspended(&mut self, _: &ActiveEventLoop) {
self.window = None;
}
} }

46
src/gui_window.rs Normal file
View file

@ -0,0 +1,46 @@
use softbuffer::Buffer;
use std::{num::NonZero, rc::Rc};
use winit::{dpi::LogicalSize, event_loop::ActiveEventLoop, window::Window};
type Context = softbuffer::Context<Rc<Window>>;
type Surface = softbuffer::Surface<Rc<Window>, Rc<Window>>;
pub struct GuiWindow {
winit_window: Rc<Window>,
surface: Surface,
}
impl GuiWindow {
pub fn new(
event_loop: &ActiveEventLoop,
logical_size: LogicalSize<u16>,
) -> GuiWindow {
let attributes = Window::default_attributes()
.with_title("servicepoint-simulator")
.with_min_inner_size(logical_size)
.with_inner_size(logical_size)
.with_transparent(false);
let winit_window =
Rc::new(event_loop.create_window(attributes).unwrap());
let context = Context::new(winit_window.clone()).unwrap();
let mut surface = Surface::new(&context, winit_window.clone()).unwrap();
surface
.resize(
NonZero::new(logical_size.width as u32).unwrap(),
NonZero::new(logical_size.height as u32).unwrap(),
)
.unwrap();
Self {
winit_window,
surface,
}
}
pub fn get_buffer(&mut self) -> Buffer<Rc<Window>, Rc<Window>> {
self.surface.buffer_mut().unwrap()
}
pub(crate) fn request_redraw(&self) {
self.winit_window.request_redraw();
}
}

View file

@ -15,6 +15,7 @@ mod command_executor;
mod cp437_font; mod cp437_font;
mod font_renderer; mod font_renderer;
mod gui; mod gui;
mod gui_window;
mod udp_server; mod udp_server;
fn main() { fn main() {
@ -36,8 +37,8 @@ fn main() {
let (stop_udp_tx, stop_udp_rx) = mpsc::channel(); let (stop_udp_tx, stop_udp_rx) = mpsc::channel();
let font_renderer = cli let font_renderer = cli
.font .font
.map(move |font| FontRenderer8x8::from_name(font)) .map(FontRenderer8x8::from_name)
.unwrap_or_else(move || FontRenderer8x8::default()); .unwrap_or_else(FontRenderer8x8::default);
let command_executor = CommandExecutor::new(&display, &luma, font_renderer); let command_executor = CommandExecutor::new(&display, &luma, font_renderer);
let mut udp_server = UdpServer::new( let mut udp_server = UdpServer::new(
cli.bind, cli.bind,

View file

@ -42,11 +42,11 @@ impl<'t> UdpServer<'t> {
pub(crate) fn run(&mut self) { pub(crate) fn run(&mut self) {
while self.stop_rx.try_recv().is_err() { while self.stop_rx.try_recv().is_err() {
self.receive_into_buf() if let Some(cmd) = self.receive_into_buf().and_then(|amount| {
.and_then(|amount| { Self::command_from_slice(&self.buf[..amount])
Self::command_from_slice(&self.buf[..amount]) }) {
}) self.handle_command(cmd);
.map(|cmd| self.handle_command(cmd)); }
} }
} }