diff --git a/src/backend/drm/egl/session.rs b/src/backend/drm/egl/session.rs index e6e4dc2..5a50247 100644 --- a/src/backend/drm/egl/session.rs +++ b/src/backend/drm/egl/session.rs @@ -11,7 +11,7 @@ use std::rc::{Rc, Weak}; use super::{EglDevice, EglSurfaceInternal}; use crate::backend::drm::{Device, Surface}; -use crate::backend::egl::native::{Backend, NativeDisplay, NativeSurface}; +use crate::backend::egl::{ffi, native::{Backend, NativeDisplay, NativeSurface}}; use crate::backend::session::{AsSessionObserver, SessionObserver}; /// [`SessionObserver`](SessionObserver) @@ -50,5 +50,15 @@ impl SessionObserver f fn activate(&mut self, devnum: Option<(u32, u32, Option)>) { self.observer.activate(devnum); + if let Some(backends) = self.backends.upgrade() { + for (_crtc, backend) in backends.borrow().iter() { + if let Some(backend) = backend.upgrade() { + let old_surface = backend.surface.surface.replace(std::ptr::null()); + if !old_surface.is_null() { + unsafe { ffi::egl::DestroySurface(**backend.surface.display, old_surface as *const _); } + } + } + } + } } } diff --git a/src/backend/egl/surface.rs b/src/backend/egl/surface.rs index 4e175d2..7d911fc 100644 --- a/src/backend/egl/surface.rs +++ b/src/backend/egl/surface.rs @@ -12,7 +12,7 @@ use std::{ /// EGL surface of a given EGL context for rendering pub struct EGLSurface { - display: Arc, + pub(crate) display: Arc, native: N, pub(crate) surface: Cell, config_id: ffi::egl::types::EGLConfig, @@ -90,9 +90,7 @@ impl EGLSurface { wrap_egl_call(|| unsafe { ffi::egl::SwapBuffers(**self.display, surface as *const _) }) .map_err(SwapBuffersError::EGLSwapBuffers) .and_then(|_| self.native.swap_buffers().map_err(SwapBuffersError::Underlying)) - } else { - Ok(()) - }; + } else { Err(SwapBuffersError::EGLSwapBuffers(EGLError::BadSurface)) }; // workaround for missing `PartialEq` impl let is_bad_surface = if let Err(SwapBuffersError::EGLSwapBuffers(EGLError::BadSurface)) = result { @@ -117,9 +115,11 @@ impl EGLSurface { }) .map_err(SwapBuffersError::EGLCreateWindowSurface)? }); - } - result + result.map_err(|_| SwapBuffersError::EGLSwapBuffers(EGLError::BadSurface)) + } else { + result + } } /// Returns true if the OpenGL surface is the current one in the thread.