From cd26ac1507584979160e3731195f39be5f49dae8 Mon Sep 17 00:00:00 2001 From: Victoria Brekenfeld Date: Sat, 22 Jan 2022 23:19:28 +0100 Subject: [PATCH] egl: Handle buffer age query errors --- src/backend/egl/surface.rs | 16 +++++++++++++--- src/backend/winit/mod.rs | 11 +++++++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/backend/egl/surface.rs b/src/backend/egl/surface.rs index a4a2c24..fa4bead 100644 --- a/src/backend/egl/surface.rs +++ b/src/backend/egl/surface.rs @@ -81,18 +81,28 @@ impl EGLSurface { } /// Returns the buffer age of the underlying back buffer - pub fn buffer_age(&self) -> i32 { + pub fn buffer_age(&self) -> Option { let surface = self.surface.load(Ordering::SeqCst); let mut age = 0; - unsafe { + let ret = unsafe { ffi::egl::QuerySurface( **self.display, surface as *const _, ffi::egl::BUFFER_AGE_EXT as i32, &mut age as *mut _, + ) + }; + if ret == ffi::egl::FALSE { + slog::debug!( + self.logger, + "Failed to query buffer age value for surface {:?}: {}", + self, + EGLError::from_last_call().unwrap_err() ); + None + } else { + Some(age) } - age } /// Swaps buffers at the end of a frame. diff --git a/src/backend/winit/mod.rs b/src/backend/winit/mod.rs index 6223c90..a93f3e7 100644 --- a/src/backend/winit/mod.rs +++ b/src/backend/winit/mod.rs @@ -306,12 +306,15 @@ impl WinitGraphicsBackend { /// /// This will only return a meaningful value, if this `WinitGraphicsBackend` /// is currently bound (by previously calling [`WinitGraphicsBackend::bind`]). - /// Otherwise the contents of the return value are undefined. - pub fn buffer_age(&self) -> usize { + /// + /// Otherwise and on error this function returns `None`. + /// If you are using this value actively e.g. for damage-tracking you should + /// likely interpret an error just as if "0" was returned. + pub fn buffer_age(&self) -> Option { if self.damage_tracking { - self.egl.buffer_age() as usize + self.egl.buffer_age().map(|x| x as usize) } else { - 0 + Some(0) } }