Skip to content

Commit c374c9e

Browse files
authored
Merge pull request #55 from ids1024/wayland-buffer
wayland: Block dispatching if back buffer isn't released
2 parents afe6da2 + aad4034 commit c374c9e

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/wayland/mod.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::{error::unwrap, SoftBufferError};
22
use raw_window_handle::{WaylandDisplayHandle, WaylandWindowHandle};
3-
use std::collections::VecDeque;
43
use wayland_client::{
54
backend::{Backend, ObjectId},
65
globals::{registry_queue_init, GlobalListContents},
@@ -18,8 +17,7 @@ pub struct WaylandImpl {
1817
qh: QueueHandle<State>,
1918
surface: wl_surface::WlSurface,
2019
shm: wl_shm::WlShm,
21-
// 0-2 buffers
22-
buffers: VecDeque<WaylandBuffer>,
20+
buffers: Option<(WaylandBuffer, WaylandBuffer)>,
2321
}
2422

2523
impl WaylandImpl {
@@ -61,24 +59,22 @@ impl WaylandImpl {
6159
})
6260
}
6361

64-
// Allocate or reuse a buffer of the given size
6562
fn buffer(&mut self, width: i32, height: i32) -> &WaylandBuffer {
66-
let buffer = if let Some(mut buffer) = self.buffers.pop_front() {
67-
if buffer.released() {
68-
buffer.resize(width, height);
69-
buffer
70-
} else {
71-
// If we have more than 1 unreleased buffer, destroy it
72-
if self.buffers.is_empty() {
73-
self.buffers.push_back(buffer);
74-
}
75-
WaylandBuffer::new(&self.shm, width, height, &self.qh)
63+
self.buffers = Some(if let Some((front, mut back)) = self.buffers.take() {
64+
// Swap buffers; block if back buffer not released yet
65+
while !back.released() {
66+
self.event_queue.blocking_dispatch(&mut State).unwrap();
7667
}
68+
back.resize(width, height);
69+
(back, front)
7770
} else {
78-
WaylandBuffer::new(&self.shm, width, height, &self.qh)
79-
};
80-
self.buffers.push_back(buffer);
81-
self.buffers.back().unwrap()
71+
// Allocate front and back buffer
72+
(
73+
WaylandBuffer::new(&self.shm, width, height, &self.qh),
74+
WaylandBuffer::new(&self.shm, width, height, &self.qh),
75+
)
76+
});
77+
&self.buffers.as_ref().unwrap().0
8278
}
8379

8480
pub(super) unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {

0 commit comments

Comments
 (0)