Fix regressed wl_shm format choosing, delay it until formats are available

This commit is contained in:
Gergő Sályi 2025-04-24 15:15:58 +02:00
parent 6eec049e48
commit 3a90b4b9e8
2 changed files with 26 additions and 13 deletions

View file

@ -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<wl_shm::Format>,
pub background_layers: Vec<BackgroundLayer>,
pub compositor_connection_task: ConnectionTask,
pub color_transform: ColorTransform,
@ -67,6 +67,21 @@ pub struct State {
pub gpu: Option<Gpu>,
}
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);

View file

@ -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,
);