Don't crash when failing to render a cursor
Browse files at this commit475b3df2b5bd660f9137cf28b6de571d4dad90e6Parent: 15418352 changed files +25 -15
- src/cursor.rs +12 -5
- src/niri.rs +13 -10
src/cursor.rsmodified+12 -5
| @@ -224,7 +224,7 @@ pub enum RenderCursor { | |||
| 224 | }, | 224 | }, |
| 225 | } | 225 | } |
| 226 | 226 | ||
| 227 | -type TextureCache = HashMap<(CursorIcon, i32), Vec<TextureBuffer<GlesTexture>>>; | 227 | +type TextureCache = HashMap<(CursorIcon, i32), Vec<Option<TextureBuffer<GlesTexture>>>>; |
| 228 | 228 | ||
| 229 | #[derive(Default)] | 229 | #[derive(Default)] |
| 230 | pub struct CursorTextureCache { | 230 | pub struct CursorTextureCache { |
| @@ -243,7 +243,7 @@ impl CursorTextureCache { | |||
| 243 | scale: i32, | 243 | scale: i32, |
| 244 | cursor: &XCursor, | 244 | cursor: &XCursor, |
| 245 | idx: usize, | 245 | idx: usize, |
| 246 | - ) -> TextureBuffer<GlesTexture> { | 246 | + ) -> Option<TextureBuffer<GlesTexture>> { |
| 247 | self.cache | 247 | self.cache |
| 248 | .borrow_mut() | 248 | .borrow_mut() |
| 249 | .entry((icon, scale)) | 249 | .entry((icon, scale)) |
| @@ -254,7 +254,7 @@ impl CursorTextureCache { | |||
| 254 | .map(|frame| { | 254 | .map(|frame| { |
| 255 | let _span = tracy_client::span!("create TextureBuffer"); | 255 | let _span = tracy_client::span!("create TextureBuffer"); |
| 256 | 256 | ||
| 257 | - TextureBuffer::from_memory( | 257 | + let buffer = TextureBuffer::from_memory( |
| 258 | renderer, | 258 | renderer, |
| 259 | &frame.pixels_rgba, | 259 | &frame.pixels_rgba, |
| 260 | Fourcc::Abgr8888, | 260 | Fourcc::Abgr8888, |
| @@ -263,8 +263,15 @@ impl CursorTextureCache { | |||
| 263 | scale, | 263 | scale, |
| 264 | Transform::Normal, | 264 | Transform::Normal, |
| 265 | None, | 265 | None, |
| 266 | - ) | 266 | + ); |
| 267 | - .unwrap() | 267 | + |
| 268 | + match buffer { | ||
| 269 | + Ok(x) => Some(x), | ||
| 270 | + Err(err) => { | ||
| 271 | + warn!("error creating a cursor texture: {err:?}"); | ||
| 272 | + None | ||
| 273 | + } | ||
| 274 | + } | ||
| 268 | }) | 275 | }) |
| 269 | .collect() | 276 | .collect() |
| 270 | })[idx] | 277 | })[idx] |
src/niri.rsmodified+13 -10
| @@ -1660,16 +1660,19 @@ impl Niri { | |||
| 1660 | idx, | 1660 | idx, |
| 1661 | ); | 1661 | ); |
| 1662 | 1662 | ||
| 1663 | - let pointer_elements = vec![OutputRenderElements::NamedPointer( | 1663 | + let mut pointer_elements = vec![]; |
| 1664 | - PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( | 1664 | + if let Some(texture) = texture { |
| 1665 | - pointer_pos.to_f64(), | 1665 | + pointer_elements.push(OutputRenderElements::NamedPointer( |
| 1666 | - &texture, | 1666 | + PrimaryGpuTextureRenderElement(TextureRenderElement::from_texture_buffer( |
| 1667 | - None, | 1667 | + pointer_pos.to_f64(), |
| 1668 | - None, | 1668 | + &texture, |
| 1669 | - None, | 1669 | + None, |
| 1670 | - Kind::Cursor, | 1670 | + None, |
| 1671 | - )), | 1671 | + None, |
| 1672 | - )]; | 1672 | + Kind::Cursor, |
| 1673 | + )), | ||
| 1674 | + )); | ||
| 1675 | + } | ||
| 1673 | 1676 | ||
| 1674 | (pointer_elements, pointer_pos) | 1677 | (pointer_elements, pointer_pos) |
| 1675 | } | 1678 | } |