wayland: Block dispatching if back buffer isn't released

https://github.com/rust-windowing/softbuffer/issues/41

If `set_buffer` can just be called in a loop without waiting for buffers
to be released or a frame callback, but can also be called in other
ways, I don't know if there's a better solution than blocking.

Should fix https://github.com/rust-windowing/softbuffer/issues/48. The
animation example could probably be implemented better, but this is at
least better.

I guess it should be documented that `set_buffer` may block? I don't
know how this compares to other backends.
This commit is contained in:
Ian Douglas Scott 2022-12-27 15:27:36 -08:00
parent a800ca457d
commit aad40343bc

View file

@ -1,6 +1,5 @@
use crate::{error::unwrap, SoftBufferError}; use crate::{error::unwrap, SoftBufferError};
use raw_window_handle::{WaylandDisplayHandle, WaylandWindowHandle}; use raw_window_handle::{WaylandDisplayHandle, WaylandWindowHandle};
use std::collections::VecDeque;
use wayland_client::{ use wayland_client::{
backend::{Backend, ObjectId}, backend::{Backend, ObjectId},
globals::{registry_queue_init, GlobalListContents}, globals::{registry_queue_init, GlobalListContents},
@ -18,8 +17,7 @@ pub struct WaylandImpl {
qh: QueueHandle<State>, qh: QueueHandle<State>,
surface: wl_surface::WlSurface, surface: wl_surface::WlSurface,
shm: wl_shm::WlShm, shm: wl_shm::WlShm,
// 0-2 buffers buffers: Option<(WaylandBuffer, WaylandBuffer)>,
buffers: VecDeque<WaylandBuffer>,
} }
impl WaylandImpl { impl WaylandImpl {
@ -61,24 +59,22 @@ impl WaylandImpl {
}) })
} }
// Allocate or reuse a buffer of the given size
fn buffer(&mut self, width: i32, height: i32) -> &WaylandBuffer { fn buffer(&mut self, width: i32, height: i32) -> &WaylandBuffer {
let buffer = if let Some(mut buffer) = self.buffers.pop_front() { self.buffers = Some(if let Some((front, mut back)) = self.buffers.take() {
if buffer.released() { // Swap buffers; block if back buffer not released yet
buffer.resize(width, height); while !back.released() {
buffer self.event_queue.blocking_dispatch(&mut State).unwrap();
} else {
// If we have more than 1 unreleased buffer, destroy it
if self.buffers.is_empty() {
self.buffers.push_back(buffer);
}
WaylandBuffer::new(&self.shm, width, height, &self.qh)
} }
back.resize(width, height);
(back, front)
} else { } else {
WaylandBuffer::new(&self.shm, width, height, &self.qh) // Allocate front and back buffer
}; (
self.buffers.push_back(buffer); WaylandBuffer::new(&self.shm, width, height, &self.qh),
self.buffers.back().unwrap() WaylandBuffer::new(&self.shm, width, height, &self.qh),
)
});
&self.buffers.as_ref().unwrap().0
} }
pub(super) unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) { pub(super) unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {