From 42042ec50210573ecd9a447c30d13aacf1cf6f0c Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 6 Jul 2025 20:42:21 +0200 Subject: [PATCH 1/5] cache rendered chars --- Cargo.lock | 4 +--- Cargo.toml | 8 +++++--- src/command_executor.rs | 31 +++++++++++++++++++------------ src/font_renderer.rs | 28 +++++++++++++++++++--------- src/gui.rs | 3 ++- 5 files changed, 46 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f3bdf1e..3ab7490 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1521,9 +1521,7 @@ dependencies = [ [[package]] name = "servicepoint" -version = "0.15.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d1e88713031e003dc3ee708dbb282e36714eee466a12d311d0e2e24c61c7118" +version = "0.16.0" dependencies = [ "bitvec", "bzip2", diff --git a/Cargo.toml b/Cargo.toml index 9dff976..bb37e66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,9 +19,6 @@ env_logger = "0.11" clap = { version = "4.5", features = ["derive"] } thiserror = "2.0" -# package parsing -servicepoint = { features = ["all_compressions"], version = "0.15.2" } - # font rendering font-kit = "0.14.2" # I should not need this as a direct dependency, but then I cannot spell the types needed to use font-kit... @@ -32,6 +29,11 @@ winit = "0.30" # for drawing pixels onto the surface of the window softbuffer = "0.4.6" +[dependencies.servicepoint] +version = "0.16.0" +features = ["all_compressions"] +path = "../servicepoint-uniffi3" + [profile.release] lto = true # Enable link-time optimization codegen-units = 1 # Reduce number of codegen units to increase optimizations diff --git a/src/command_executor.rs b/src/command_executor.rs index f24a5fe..69ae30c 100644 --- a/src/command_executor.rs +++ b/src/command_executor.rs @@ -5,10 +5,11 @@ use crate::{ }; use log::{debug, error, info, trace, warn}; use servicepoint::{ - BinaryOperation, BitVecCommand, Bitmap, BitmapCommand, GlobalBrightnessCommand, - BrightnessGrid, BrightnessGridCommand, CharGridCommand, ClearCommand, - CompressionCode, Cp437GridCommand, FadeOutCommand, Grid, HardResetCommand, - Origin, TypedCommand, PIXEL_COUNT, PIXEL_WIDTH, TILE_SIZE, + BinaryOperation, BitVecCommand, Bitmap, BitmapCommand, BrightnessGrid, + BrightnessGridCommand, CharGridCommand, ClearCommand, CompressionCode, + Cp437GridCommand, FadeOutCommand, GlobalBrightnessCommand, + HardResetCommand, Origin, TypedCommand, PIXEL_COUNT, PIXEL_WIDTH, + TILE_SIZE, }; use std::{ ops::{BitAnd, BitOr, BitXor}, @@ -46,11 +47,11 @@ impl CommandExecute for BitmapCommand { fn execute(&self, context: &CommandExecutionContext) -> ExecutionResult { let Self { origin: - Origin { - x: offset_x, - y: offset_y, - .. - }, + Origin { + x: offset_x, + y: offset_y, + .. + }, bitmap: pixels, .. } = self; @@ -143,7 +144,7 @@ impl CommandExecute for Cp437GridCommand { bitmap: context.cp437_font[char_code].clone(), compression: CompressionCode::default(), } - .execute(context); + .execute(context); match execute_result { Success => {} Failure => { @@ -199,8 +200,14 @@ impl CommandExecute for CharGridCommand { if let Err(e) = context.font_renderer.render( char, - &mut display, - Origin::new(tile_x * TILE_SIZE, tile_y * TILE_SIZE), + &mut display + .window_mut( + tile_x * TILE_SIZE, + tile_y * TILE_SIZE, + TILE_SIZE, + TILE_SIZE, + ) + .unwrap(), ) { error!( "stopping drawing text because char draw failed: {e}" diff --git a/src/font_renderer.rs b/src/font_renderer.rs index d9f5fd7..efe243f 100644 --- a/src/font_renderer.rs +++ b/src/font_renderer.rs @@ -12,8 +12,11 @@ use pathfinder_geometry::{ transform2d::Transform2F, vector::{vec2f, vec2i}, }; -use servicepoint::{Bitmap, Grid, Origin, Pixels, TILE_SIZE}; -use std::sync::{Mutex, MutexGuard}; +use servicepoint::{Bitmap, GridMut, WindowMut, TILE_SIZE}; +use std::{ + collections::HashMap, + sync::{Mutex, MutexGuard}, +}; #[derive(Debug)] struct SendFont(Font); @@ -32,6 +35,7 @@ pub struct FontRenderer8x8 { font: SendFont, canvas: Mutex, fallback_char: Option, + cache: Mutex>, } #[derive(Debug, thiserror::Error)] @@ -56,6 +60,7 @@ impl FontRenderer8x8 { font: SendFont(font), fallback_char, canvas: Mutex::new(canvas), + cache: Mutex::new(HashMap::new()), } } @@ -74,9 +79,13 @@ impl FontRenderer8x8 { pub fn render( &self, char: char, - bitmap: &mut Bitmap, - offset: Origin, + target: &mut WindowMut, ) -> Result<(), RenderError> { + let cache = &mut *self.cache.lock().unwrap(); + if let Some(drawn_char) = cache.get(&char) { + target.deref_assign(drawn_char); + } + let glyph_id = self.get_glyph(char)?; let mut canvas = self.canvas.lock().unwrap(); @@ -91,20 +100,21 @@ impl FontRenderer8x8 { RasterizationOptions::Bilevel, )?; - Self::copy_to_bitmap(canvas, bitmap, offset) + let mut bitmap = Bitmap::new(TILE_SIZE, TILE_SIZE).unwrap(); + Self::copy_to_bitmap(canvas, &mut bitmap)?; + target.deref_assign(&bitmap); + cache.insert(char, bitmap); + Ok(()) } fn copy_to_bitmap( canvas: MutexGuard, bitmap: &mut Bitmap, - offset: Origin, ) -> Result<(), RenderError> { for y in 0..TILE_SIZE { for x in 0..TILE_SIZE { let canvas_val = canvas.pixels[x + y * TILE_SIZE] != 0; - let bitmap_x = offset.x + x; - let bitmap_y = offset.y + y; - if !bitmap.set_optional(bitmap_x, bitmap_y, canvas_val) { + if !bitmap.set_optional(x, y, canvas_val) { return Err(OutOfBounds(x, y)); } } diff --git a/src/gui.rs b/src/gui.rs index e5f08d4..ad3fa26 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -24,6 +24,7 @@ const PIXEL_HEIGHT_WITH_SPACERS: usize = PIXEL_HEIGHT + NUM_SPACERS * SPACER_HEIGHT; const OFF_COLOR: u32 = u32::from_ne_bytes([0u8, 0, 0, 0]); +const SPACER_COLOR: u32 = u32::from_ne_bytes([100u8, 100, 100, 0]); #[derive(Debug)] pub enum AppEvents { @@ -61,7 +62,7 @@ impl<'t> Gui<'t> { if self.options.spacers && tile_y != 0 { // cannot just frame.skip(PIXEL_WIDTH as usize * SPACER_HEIGHT as usize) because of typing for _ in 0..PIXEL_WIDTH * SPACER_HEIGHT { - frame.next().unwrap(); + *frame.next().unwrap() = SPACER_COLOR; } } From c3b9ecf4028f957068bf5bae1c35e7bb3709d9f2 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 12 Jul 2025 13:33:58 +0200 Subject: [PATCH 2/5] update to released version --- Cargo.lock | 14 ++------------ Cargo.toml | 1 - flake.nix | 1 + src/command_executor.rs | 12 +++++------- 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3ab7490..86521f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -760,17 +760,6 @@ dependencies = [ "hashbrown", ] -[[package]] -name = "inherent" -version = "1.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c38228f24186d9cc68c729accb4d413be9eaed6ad07ff79e0270d9e56f3de13" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -1522,11 +1511,12 @@ dependencies = [ [[package]] name = "servicepoint" version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b04582e916474f1bc1605cad3773262c425d9062b487e49a0df59662f2cca8d" dependencies = [ "bitvec", "bzip2", "flate2", - "inherent", "log", "once_cell", "rust-lzma", diff --git a/Cargo.toml b/Cargo.toml index bb37e66..7f923f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,6 @@ softbuffer = "0.4.6" [dependencies.servicepoint] version = "0.16.0" features = ["all_compressions"] -path = "../servicepoint-uniffi3" [profile.release] lto = true # Enable link-time optimization diff --git a/flake.nix b/flake.nix index b5f5964..ffe1341 100644 --- a/flake.nix +++ b/flake.nix @@ -75,6 +75,7 @@ NIX_LD_LIBRARY_PATH = LD_LIBRARY_PATH; NIX_LD = pkgs.stdenv.cc.bintools.dynamicLinker; RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; + RUST_BACKTRACE = "1"; }; } ); diff --git a/src/command_executor.rs b/src/command_executor.rs index 69ae30c..4ea676e 100644 --- a/src/command_executor.rs +++ b/src/command_executor.rs @@ -7,7 +7,7 @@ use log::{debug, error, info, trace, warn}; use servicepoint::{ BinaryOperation, BitVecCommand, Bitmap, BitmapCommand, BrightnessGrid, BrightnessGridCommand, CharGridCommand, ClearCommand, CompressionCode, - Cp437GridCommand, FadeOutCommand, GlobalBrightnessCommand, + Cp437GridCommand, FadeOutCommand, GlobalBrightnessCommand, Grid, GridMut, HardResetCommand, Origin, TypedCommand, PIXEL_COUNT, PIXEL_WIDTH, TILE_SIZE, }; @@ -195,17 +195,15 @@ impl CommandExecute for CharGridCommand { let char = grid.get(char_x, char_y); trace!("drawing {char}"); - let tile_x = char_x + x; - let tile_y = char_y + y; + let pixel_x = (char_x + x) * TILE_SIZE; + let pixel_y = (char_y + y) * TILE_SIZE; if let Err(e) = context.font_renderer.render( char, &mut display .window_mut( - tile_x * TILE_SIZE, - tile_y * TILE_SIZE, - TILE_SIZE, - TILE_SIZE, + pixel_x..pixel_x + TILE_SIZE, + pixel_y..pixel_y + TILE_SIZE, ) .unwrap(), ) { From 5dcdd0f311e6ea15205ef8319ad6f266dd809816 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 12 Jul 2025 13:57:19 +0200 Subject: [PATCH 3/5] add experimental null char handling --- src/cli.rs | 5 +++++ src/command_executor.rs | 33 +++++++++++++++++++++++---------- src/main.rs | 7 ++++++- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 5d7adf0..eb0497c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -22,6 +22,11 @@ pub struct Cli { help = "Set default log level lower. You can also change this via the RUST_LOG environment variable." )] pub verbose: bool, + #[arg( + long, + help = "When receiving a null byte as a char in the CharGridCommand, do not overwrite any pixels instead of clearing all pixels." + )] + pub experimental_null_char_handling: bool, } #[derive(Parser, Debug)] diff --git a/src/command_executor.rs b/src/command_executor.rs index 4ea676e..0fe276f 100644 --- a/src/command_executor.rs +++ b/src/command_executor.rs @@ -22,6 +22,7 @@ pub struct CommandExecutionContext<'t> { luma: &'t RwLock, cp437_font: Cp437Font, font_renderer: FontRenderer8x8, + experimental_null_char_handling: bool, } #[must_use] @@ -193,20 +194,30 @@ impl CommandExecute for CharGridCommand { for char_y in 0usize..grid.height() { for char_x in 0usize..grid.width() { let char = grid.get(char_x, char_y); - trace!("drawing {char}"); - - let pixel_x = (char_x + x) * TILE_SIZE; - let pixel_y = (char_y + y) * TILE_SIZE; - - if let Err(e) = context.font_renderer.render( - char, - &mut display + let mut bitmap_window = { + let pixel_x = (char_x + x) * TILE_SIZE; + let pixel_y = (char_y + y) * TILE_SIZE; + display .window_mut( pixel_x..pixel_x + TILE_SIZE, pixel_y..pixel_y + TILE_SIZE, ) - .unwrap(), - ) { + .unwrap() + }; + + if char == '\0' { + if context.experimental_null_char_handling { + trace!("skipping {char:?}"); + } else { + bitmap_window.fill(false); + } + continue; + } + + trace!("drawing {char}"); + if let Err(e) = + context.font_renderer.render(char, &mut bitmap_window) + { error!( "stopping drawing text because char draw failed: {e}" ); @@ -256,12 +267,14 @@ impl<'t> CommandExecutionContext<'t> { display: &'t RwLock, luma: &'t RwLock, font_renderer: FontRenderer8x8, + experimental_null_char_handling: bool, ) -> Self { CommandExecutionContext { display, luma, font_renderer, cp437_font: Cp437Font::default(), + experimental_null_char_handling, } } } diff --git a/src/main.rs b/src/main.rs index 06e0b28..0648b2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,7 +39,12 @@ fn main() { .font .map(FontRenderer8x8::from_name) .unwrap_or_else(FontRenderer8x8::default); - let context = CommandExecutionContext::new(&display, &luma, font_renderer); + let context = CommandExecutionContext::new( + &display, + &luma, + font_renderer, + cli.experimental_null_char_handling, + ); let mut udp_server = UdpServer::new( cli.bind, stop_udp_rx, From 8a018a484665a6c350b8a4dd62a95ceff56045c3 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 12 Jul 2025 13:59:38 +0200 Subject: [PATCH 4/5] bump version, update readme --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 26 ++++++++++++++++++-------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 86521f0..abb6812 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1526,7 +1526,7 @@ dependencies = [ [[package]] name = "servicepoint-simulator" -version = "0.2.3" +version = "0.2.4" dependencies = [ "clap", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 7f923f2..a179ced 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "servicepoint-simulator" -version = "0.2.3" +version = "0.2.4" edition = "2021" publish = true license = "GPL-3.0-or-later" diff --git a/README.md b/README.md index 17d5be5..2610a25 100644 --- a/README.md +++ b/README.md @@ -42,14 +42,24 @@ Make sure to run a release build, because a debug build _way_ slower. Usage: servicepoint-simulator [OPTIONS] Options: - --bind address and port to bind to [default: 0.0.0.0:2342] - -f, --font The name of the font family to use. This defaults to the system monospace font. - -s, --spacers add spacers between tile rows to simulate gaps in real display - -r, --red Use the red color channel - -g, --green Use the green color channel - -b, --blue Use the blue color channel - -v, --verbose Set default log level lower. You can also change this via the RUST_LOG environment variable. - -h, --help Print help + --bind + address and port to bind to [default: 0.0.0.0:2342] + -f, --font + The name of the font family to use. This defaults to the system monospace font. + -s, --spacers + add spacers between tile rows to simulate gaps in real display + -r, --red + Use the red color channel + -g, --green + Use the green color channel + -b, --blue + Use the blue color channel + -v, --verbose + Set default log level lower. You can also change this via the RUST_LOG environment variable. + --experimental-null-char-handling + When receiving a null byte as a char in the CharGridCommand, do not overwrite any pixels instead of clearing all pixels. + -h, --help + Print help ``` See [env_logger](https://docs.rs/env_logger/latest/env_logger/) to configure logging. From 80beef4f3cb21daab161119bebd9b17699b0203a Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 12 Jul 2025 14:16:30 +0200 Subject: [PATCH 5/5] update dependencies --- Cargo.lock | 28 ++++++++++++++-------------- flake.lock | 12 ++++++------ 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index abb6812..21e987a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "ab_glyph" -version = "0.2.29" +version = "0.2.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec3672c180e71eeaaac3a541fbbc5f5ad4def8b747c595ad30d674e43049f7b0" +checksum = "1e0f4f6fbdc5ee39f2ede9f5f3ec79477271a6d6a2baff22310d51736bda6cea" dependencies = [ "ab_glyph_rasterizer", "owned_ttf_parser", @@ -14,9 +14,9 @@ dependencies = [ [[package]] name = "ab_glyph_rasterizer" -version = "0.1.8" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c71b1793ee61086797f5c80b6efa2b8ffa6d5dd703f118545808a7f2e27f7046" +checksum = "b2187590a23ab1e3df8681afdf0987c48504d80291f002fcdb651f0ef5e25169" [[package]] name = "adler2" @@ -265,9 +265,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.27" +version = "1.2.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d487aa071b5f64da6f19a3e848e3578944b726ee5a4854b82172f02aa876bfdc" +checksum = "5c1599538de2394445747c8cf7935946e3cc27e9625f889d979bfb2aaf569362" dependencies = [ "jobserver", "libc", @@ -294,9 +294,9 @@ checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" [[package]] name = "clap" -version = "4.5.40" +version = "4.5.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40b6887a1d8685cebccf115538db5c0efe625ccac9696ad45c409d96566e910f" +checksum = "be92d32e80243a54711e5d7ce823c35c41c9d929dc4ab58e1276f625841aadf9" dependencies = [ "clap_builder", "clap_derive", @@ -304,9 +304,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.40" +version = "4.5.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0c66c08ce9f0c698cbce5c0279d0bb6ac936d8674174fe48f736533b964f59e" +checksum = "707eab41e9622f9139419d573eca0900137718000c517d47da73045f54331c3d" dependencies = [ "anstream", "anstyle", @@ -316,9 +316,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.5.40" +version = "4.5.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2c7947ae4cc3d851207c1adb5b5e260ff0cca11446b1d6d1423788e442257ce" +checksum = "ef4f52386a59ca4c860f7393bcf8abd8dfd91ecccc0f774635ff68e92eeef491" dependencies = [ "heck", "proc-macro2", @@ -2387,9 +2387,9 @@ dependencies = [ [[package]] name = "winnow" -version = "0.7.11" +version = "0.7.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74c7b26e3480b707944fc872477815d29a8e429d2f93a1ce000f5fa84a15cbcd" +checksum = "f3edebf492c8125044983378ecb5766203ad3b4c2f7a922bd7dd207f6d443e95" dependencies = [ "memchr", ] diff --git a/flake.lock b/flake.lock index e507dfb..4ecfe6e 100644 --- a/flake.lock +++ b/flake.lock @@ -7,11 +7,11 @@ ] }, "locked": { - "lastModified": 1745925850, - "narHash": "sha256-cyAAMal0aPrlb1NgzMxZqeN1mAJ2pJseDhm2m6Um8T0=", + "lastModified": 1752249768, + "narHash": "sha256-wKqMvhTqMgTKM/CdTH/ihq9eLZM95qpU0FG7cvTBFJg=", "owner": "nix-community", "repo": "naersk", - "rev": "38bc60bbc157ae266d4a0c96671c6c742ee17a5f", + "rev": "35aa63738857c40f98ecb04db52887d664836e74", "type": "github" }, "original": { @@ -37,11 +37,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1750969886, - "narHash": "sha256-zW/OFnotiz/ndPFdebpo3X0CrbVNf22n4DjN2vxlb58=", + "lastModified": 1752162966, + "narHash": "sha256-3MxxkU8ZXMHXcbFz7UE4M6qnIPTYGcE/7EMqlZNnVDE=", "owner": "nixos", "repo": "nixpkgs", - "rev": "a676066377a2fe7457369dd37c31fd2263b662f4", + "rev": "10e687235226880ed5e9f33f1ffa71fe60f2638a", "type": "github" }, "original": {