fix(wayland): proper frame callback handling

This commit is contained in:
Nova
2025-09-14 00:40:07 -07:00
parent 1bfd9c95f0
commit 2632a0c5f3
3 changed files with 26 additions and 20 deletions

View File

@@ -57,7 +57,7 @@ pub struct SurfaceState {
pub geometry: Option<Geometry>,
pub min_size: Option<Vector2<u32>>,
pub max_size: Option<Vector2<u32>>,
frame_callbacks: Vec<Arc<Callback>>,
frame_callbacks: Registry<Callback>,
}
impl Default for SurfaceState {
fn default() -> Self {
@@ -67,7 +67,7 @@ impl Default for SurfaceState {
geometry: None,
min_size: None,
max_size: None,
frame_callbacks: Vec::new(),
frame_callbacks: Registry::new(),
}
}
}
@@ -90,6 +90,7 @@ pub struct Surface {
pub role: OnceLock<SurfaceRole>,
pub panel_item: Mutex<Weak<PanelItem<XdgBackend>>>,
on_commit_handlers: Mutex<Vec<OnCommitCallback>>,
frame_callbacks: Registry<Callback>,
material: OnceLock<Handle<BevyMaterial>>,
pending_material_applications: Registry<ModelPart>,
presentation_feedback: Mutex<Vec<Arc<PresentationFeedback>>>,
@@ -122,6 +123,7 @@ impl Surface {
role: OnceLock::new(),
panel_item: Mutex::new(Weak::default()),
on_commit_handlers: Mutex::new(Vec::new()),
frame_callbacks: Registry::new(),
material: OnceLock::new(),
pending_material_applications: Registry::new(),
presentation_feedback: Mutex::default(),
@@ -156,7 +158,7 @@ impl Surface {
}
#[tracing::instrument(level = "debug", skip_all)]
pub fn pending_state(&self) -> parking_lot::MutexGuard<'_, DoubleBuffer<SurfaceState>> {
pub fn state_lock(&self) -> parking_lot::MutexGuard<'_, DoubleBuffer<SurfaceState>> {
self.state.lock()
}
@@ -232,8 +234,9 @@ impl Surface {
}
#[tracing::instrument(level = "debug", skip_all)]
pub fn frame_event(&self) {
for callback in self.state.lock().current.frame_callbacks.drain(..) {
let _ = self.message_sink.send(Message::Frame(callback));
let callbacks = self.frame_callbacks.take_valid_contents();
if !callbacks.is_empty() {
let _ = self.message_sink.send(Message::Frame(callbacks));
}
}
// pub fn size(&self) -> Option<Vector2<u32>> {
@@ -366,7 +369,7 @@ impl WlSurface for Surface {
callback_id: ObjectId,
) -> Result<()> {
let callback = client.insert(callback_id, Callback(callback_id));
self.state.lock().pending.frame_callbacks.push(callback);
self.state.lock().pending.frame_callbacks.add_raw(&callback);
Ok(())
}
@@ -399,8 +402,7 @@ impl WlSurface for Surface {
async fn commit(&self, _client: &mut Client, _sender_id: ObjectId) -> Result<()> {
// we want the upload to complete before we give the image to bevy
let buffer_option = self
.state
.lock()
.state_lock()
.pending
.buffer
.as_ref()
@@ -411,7 +413,9 @@ impl WlSurface for Surface {
.unwrap();
}
self.state.lock().apply();
self.state.lock().pending.frame_callbacks.clear();
for callback in self.current_state().frame_callbacks.get_valid_contents() {
self.frame_callbacks.add_raw(&callback)
}
let current_state = self.current_state();
let mut handlers = self.on_commit_handlers.lock();
handlers.retain(|f| (f)(self, &current_state));