egl: move eglSwapBuffers call into NativeSurface

This commit is contained in:
Victor Brekenfeld 2020-05-22 23:12:49 +02:00
parent 0565e5fd79
commit 19ef1ed3c0
3 changed files with 25 additions and 10 deletions

View File

@ -7,7 +7,9 @@
use crate::backend::drm::{Device, RawDevice, Surface}; use crate::backend::drm::{Device, RawDevice, Surface};
use crate::backend::egl::native::{Backend, NativeDisplay, NativeSurface}; use crate::backend::egl::native::{Backend, NativeDisplay, NativeSurface};
use crate::backend::egl::{display::EGLDisplayHandle, ffi}; use crate::backend::egl::{display::EGLDisplayHandle, ffi};
use crate::backend::egl::{wrap_egl_call, EGLError, Error as EglBackendError, SurfaceCreationError}; use crate::backend::egl::{
wrap_egl_call, EGLError, Error as EglBackendError, SurfaceCreationError, SwapBuffersError,
};
use super::{Error, GbmDevice, GbmSurface}; use super::{Error, GbmDevice, GbmSurface};
@ -109,9 +111,15 @@ unsafe impl<D: RawDevice + 'static> NativeSurface for GbmSurface<D> {
self.needs_recreation() self.needs_recreation()
} }
fn swap_buffers(&self) -> Result<(), Self::Error> { fn swap_buffers(
&self,
display: &Arc<EGLDisplayHandle>,
surface: ffi::egl::types::EGLSurface,
) -> Result<(), SwapBuffersError<Self::Error>> {
wrap_egl_call(|| unsafe { ffi::egl::SwapBuffers(***display, surface as *const _) })
.map_err(SwapBuffersError::EGLSwapBuffers)?;
// this is safe since `eglSwapBuffers` will have been called exactly once // this is safe since `eglSwapBuffers` will have been called exactly once
// if this is used by our egl module, which is why this trait is unsafe. // if this is used by our egl module, which is why this trait is unsafe.
unsafe { self.page_flip() } unsafe { self.page_flip() }.map_err(SwapBuffersError::Underlying)
} }
} }

View File

@ -1,6 +1,8 @@
//! Type safe native types for safe context/surface creation //! Type safe native types for safe context/surface creation
use super::{display::EGLDisplayHandle, ffi, wrap_egl_call, EGLError, Error, SurfaceCreationError}; use super::{
display::EGLDisplayHandle, ffi, wrap_egl_call, EGLError, Error, SurfaceCreationError, SwapBuffersError,
};
use nix::libc::{c_int, c_void}; use nix::libc::{c_int, c_void};
#[cfg(feature = "backend_winit")] #[cfg(feature = "backend_winit")]
@ -228,8 +230,15 @@ pub unsafe trait NativeSurface {
/// [EGLSurface::swap_buffers](::backend::egl::surface::EGLSurface::swap_buffers) /// [EGLSurface::swap_buffers](::backend::egl::surface::EGLSurface::swap_buffers)
/// ///
/// Only implement if required by the backend. /// Only implement if required by the backend.
fn swap_buffers(&self) -> Result<(), Self::Error> { fn swap_buffers(
Ok(()) &self,
display: &Arc<EGLDisplayHandle>,
surface: ffi::egl::types::EGLSurface,
) -> Result<(), SwapBuffersError<Self::Error>> {
wrap_egl_call(|| unsafe {
ffi::egl::SwapBuffers(***display, surface as *const _);
})
.map_err(SwapBuffersError::EGLSwapBuffers)
} }
} }

View File

@ -1,6 +1,6 @@
//! EGL surface related structs //! EGL surface related structs
use super::{ffi, native, wrap_egl_call, EGLError, SurfaceCreationError, SwapBuffersError}; use super::{ffi, native, EGLError, SurfaceCreationError, SwapBuffersError};
use crate::backend::egl::display::EGLDisplayHandle; use crate::backend::egl::display::EGLDisplayHandle;
use crate::backend::graphics::PixelFormat; use crate::backend::graphics::PixelFormat;
use nix::libc::c_int; use nix::libc::c_int;
@ -93,9 +93,7 @@ impl<N: native::NativeSurface> EGLSurface<N> {
let surface = self.surface.get(); let surface = self.surface.get();
let result = if !surface.is_null() { let result = if !surface.is_null() {
wrap_egl_call(|| unsafe { ffi::egl::SwapBuffers(**self.display, surface as *const _) }) self.native.swap_buffers(&self.display, surface)
.map_err(SwapBuffersError::EGLSwapBuffers)
.and_then(|_| self.native.swap_buffers().map_err(SwapBuffersError::Underlying))
} else { } else {
Err(SwapBuffersError::EGLSwapBuffers(EGLError::BadSurface)) Err(SwapBuffersError::EGLSwapBuffers(EGLError::BadSurface))
}; };