Skip to content

Commit

Permalink
pixman: Store strong reference to WlBuffer in PixmanImage
Browse files Browse the repository at this point in the history
If a weak `WlBuffer` is used, it isn't possible to read the contents of
a destroyed shm buffer. But the Wayland protocol specifies that
destroying an attached buffer shouldn't change the contents of the
surface.

A `wayland_server::Weak` will not upgrade to a strong reference for a
destroyed object even if other strong references exist, like the strong
reference to the `WlBuffer` in `WaylandSurfaceRenderElement`.

This fixes an error when a buffer is destroyed by libdecor, which
crashed a version of Anvil patched to use pixman.
  • Loading branch information
ids1024 committed Dec 6, 2024
1 parent a8c64b4 commit 4e66ba0
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/backend/renderer/pixman/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
wayland::{compositor::SurfaceData, shm},
};
#[cfg(feature = "wayland_frontend")]
use wayland_server::{protocol::wl_buffer, Resource, Weak};
use wayland_server::protocol::wl_buffer;

#[cfg(all(
feature = "wayland_frontend",
Expand Down Expand Up @@ -88,7 +88,7 @@ struct PixmanDmabufMapping {
#[derive(Debug)]
struct PixmanImageInner {
#[cfg(feature = "wayland_frontend")]
buffer: Option<Weak<wl_buffer::WlBuffer>>,
buffer: Option<wl_buffer::WlBuffer>,
dmabuf: Option<PixmanDmabufMapping>,
image: Mutex<Image<'static, 'static>>,
_flipped: bool, /* TODO: What about flipped textures? */
Expand All @@ -102,7 +102,7 @@ impl PixmanImage {
fn accessor<'l>(&'l self) -> Result<TextureAccessor<'l>, PixmanError> {
#[cfg(feature = "wayland_frontend")]
let buffer = if let Some(buffer) = self.0.buffer.as_ref() {
Some(buffer.upgrade().map_err(|_| PixmanError::BufferDestroyed)?)
Some(buffer)
} else {
None
};
Expand All @@ -116,7 +116,7 @@ impl PixmanImage {

Ok(TextureAccessor {
#[cfg(feature = "wayland_frontend")]
buffer,
buffer: buffer.cloned(),
image: &self.0.image,
_guard: guard,
})
Expand Down Expand Up @@ -1124,7 +1124,7 @@ impl ImportMemWl for PixmanRenderer {
std::result::Result::<_, PixmanError>::Ok(image)
})??;
Ok(PixmanTexture(PixmanImage(Arc::new(PixmanImageInner {
buffer: Some(buffer.downgrade()),
buffer: Some(buffer.clone()),
dmabuf: None,
image: Mutex::new(image),
_flipped: false,
Expand Down

0 comments on commit 4e66ba0

Please sign in to comment.