From 3a90b4b9e8d80b07c400f8dc1e176aacab3582cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20S=C3=A1lyi?= Date: Thu, 24 Apr 2025 15:15:58 +0200 Subject: [PATCH] Fix regressed wl_shm format choosing, delay it until formats are available --- src/main.rs | 32 ++++++++++++++++++++++---------- src/wayland.rs | 7 ++++--- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index b9cac6d..da40566 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,7 +59,7 @@ pub struct State { pub layer_shell: LayerShell, pub viewporter: WpViewporter, pub wallpaper_dir: PathBuf, - pub shm_format: wl_shm::Format, + pub shm_format: Option, pub background_layers: Vec, pub compositor_connection_task: ConnectionTask, pub color_transform: ColorTransform, @@ -67,6 +67,21 @@ pub struct State { pub gpu: Option, } +impl State { + fn shm_format(&mut self) -> wl_shm::Format { + *self.shm_format.get_or_insert_with(|| { + let mut format = wl_shm::Format::Xrgb8888; + // Consume less gpu memory by using Bgr888 if available, + // fall back to the always supported Xrgb8888 otherwise + if self.shm.formats().contains(&wl_shm::Format::Bgr888) { + format = wl_shm::Format::Bgr888 + } + debug!("Using shm format: {format:?}"); + format + }) + } +} + fn main() -> Result<(), ()> { run().map_err(|e| { error!("{e:#}"); }) } @@ -104,15 +119,12 @@ fn run() -> anyhow::Result<()> { let compositor_state = CompositorState::bind(&globals, &qh).unwrap(); let layer_shell = LayerShell::bind(&globals, &qh).unwrap(); let shm = Shm::bind(&globals, &qh).unwrap(); - let mut shm_format = wl_shm::Format::Xrgb8888; - if cli.pixelformat != Some(PixelFormat::Baseline) { - // Consume less gpu memory by using Bgr888 if available, - // fall back to the always supported Xrgb8888 otherwise - if shm.formats().contains(&wl_shm::Format::Bgr888) { - shm_format = wl_shm::Format::Bgr888; - } - } - debug!("Using shm format: {shm_format:?}"); + let shm_format = if cli.pixelformat == Some(PixelFormat::Baseline) { + debug!("Using shm format: {:?}", wl_shm::Format::Xrgb8888); + Some(wl_shm::Format::Xrgb8888) + } else { + None + }; let registry_state = RegistryState::new(&globals); diff --git a/src/wayland.rs b/src/wayland.rs index 8e05163..0414de6 100644 --- a/src/wayland.rs +++ b/src/wayland.rs @@ -923,7 +923,8 @@ fn load_wallpapers( return } }; - let shm_stride = match state.shm_format { + let shm_format = state.shm_format(); + let shm_stride = match shm_format { wl_shm::Format::Xrgb8888 => width as usize * 4, wl_shm::Format::Bgr888 => { // Align buffer stride to both 4 and pixel format @@ -1044,7 +1045,7 @@ fn load_wallpapers( width as u32, height as u32, shm_stride, - state.shm_format, + shm_format, state.color_transform, &mut resizer, ) { @@ -1057,7 +1058,7 @@ fn load_wallpapers( width, height, shm_stride.try_into().unwrap(), - state.shm_format, + shm_format, (), qh, );