2017-06-02 11:16:12 +00:00
|
|
|
//! Glium compatibility module
|
|
|
|
|
2018-10-04 22:37:43 +00:00
|
|
|
use backend::graphics::egl::{
|
|
|
|
error::Result as EGLResult,
|
|
|
|
wayland::{EGLDisplay, EGLWaylandExtensions},
|
|
|
|
EGLGraphicsBackend, SwapBuffersError,
|
|
|
|
};
|
|
|
|
use glium::{
|
|
|
|
backend::{Backend, Context, Facade},
|
|
|
|
debug::DebugCallbackBehavior,
|
|
|
|
Frame, SwapBuffersError as GliumSwapBuffersError,
|
|
|
|
};
|
|
|
|
use std::{
|
|
|
|
cell::{Ref, RefCell, RefMut},
|
|
|
|
os::raw::c_void,
|
|
|
|
rc::Rc,
|
|
|
|
};
|
2018-01-05 19:04:46 +00:00
|
|
|
use wayland_server::Display;
|
2017-06-02 11:16:12 +00:00
|
|
|
|
2017-03-20 13:33:27 +00:00
|
|
|
impl From<SwapBuffersError> for GliumSwapBuffersError {
|
2017-03-07 10:53:57 +00:00
|
|
|
fn from(error: SwapBuffersError) -> Self {
|
|
|
|
match error {
|
|
|
|
SwapBuffersError::ContextLost => GliumSwapBuffersError::ContextLost,
|
|
|
|
SwapBuffersError::AlreadySwapped => GliumSwapBuffersError::AlreadySwapped,
|
2017-10-01 17:21:12 +00:00
|
|
|
SwapBuffersError::Unknown(_) => GliumSwapBuffersError::ContextLost, // TODO
|
2017-03-07 10:53:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 12:56:30 +00:00
|
|
|
/// Wrapper to expose `Glium` compatibility
|
2017-06-02 11:16:12 +00:00
|
|
|
pub struct GliumGraphicsBackend<T: EGLGraphicsBackend> {
|
|
|
|
context: Rc<Context>,
|
|
|
|
backend: Rc<InternalBackend<T>>,
|
|
|
|
}
|
|
|
|
|
2018-02-26 17:29:52 +00:00
|
|
|
struct InternalBackend<T: EGLGraphicsBackend>(RefCell<T>);
|
2017-06-02 11:16:12 +00:00
|
|
|
|
|
|
|
impl<T: EGLGraphicsBackend + 'static> GliumGraphicsBackend<T> {
|
|
|
|
fn new(backend: T) -> GliumGraphicsBackend<T> {
|
2018-02-26 17:29:52 +00:00
|
|
|
let internal = Rc::new(InternalBackend(RefCell::new(backend)));
|
2017-06-02 11:16:12 +00:00
|
|
|
|
|
|
|
GliumGraphicsBackend {
|
|
|
|
// cannot fail
|
|
|
|
context: unsafe {
|
2017-09-13 20:51:35 +00:00
|
|
|
Context::new(internal.clone(), true, DebugCallbackBehavior::default()).unwrap()
|
2017-06-02 11:16:12 +00:00
|
|
|
},
|
|
|
|
backend: internal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Start drawing on the backbuffer.
|
|
|
|
///
|
|
|
|
/// This function returns a `Frame`, which can be used to draw on it. When the `Frame` is
|
|
|
|
/// destroyed, the buffers are swapped.
|
|
|
|
///
|
|
|
|
/// Note that destroying a `Frame` is immediate, even if vsync is enabled.
|
|
|
|
#[inline]
|
|
|
|
pub fn draw(&self) -> Frame {
|
2018-09-24 22:32:09 +00:00
|
|
|
Frame::new(self.context.clone(), self.backend.get_framebuffer_dimensions())
|
2017-06-02 11:16:12 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 17:29:52 +00:00
|
|
|
/// Borrow the underlying backend.
|
|
|
|
///
|
|
|
|
/// This follows the same semantics as `std::cell:RefCell`.
|
|
|
|
/// Multiple read-only borrows are possible. Borrowing the
|
|
|
|
/// backend while there is a mutable reference will panic.
|
|
|
|
pub fn borrow(&self) -> Ref<T> {
|
|
|
|
self.backend.0.borrow()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrow the underlying backend mutably.
|
|
|
|
///
|
|
|
|
/// This follows the same semantics as `std::cell:RefCell`.
|
|
|
|
/// Holding any other borrow while trying to borrow the backend
|
2018-10-30 12:56:30 +00:00
|
|
|
/// mutably will panic. Note that Glium will borrow the backend
|
2018-02-26 17:29:52 +00:00
|
|
|
/// (not mutably) during rendering.
|
2018-03-29 13:31:25 +00:00
|
|
|
pub fn borrow_mut(&self) -> RefMut<T> {
|
2018-02-26 17:29:52 +00:00
|
|
|
self.backend.0.borrow_mut()
|
2017-06-02 11:16:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-18 16:27:49 +00:00
|
|
|
|
2017-06-13 14:52:17 +00:00
|
|
|
impl<T: EGLGraphicsBackend> Facade for GliumGraphicsBackend<T> {
|
|
|
|
fn get_context(&self) -> &Rc<Context> {
|
|
|
|
&self.context
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 17:48:58 +00:00
|
|
|
impl<T: EGLGraphicsBackend + 'static> From<T> for GliumGraphicsBackend<T> {
|
|
|
|
fn from(backend: T) -> Self {
|
|
|
|
GliumGraphicsBackend::new(backend)
|
2017-03-18 16:27:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-07 21:30:38 +00:00
|
|
|
impl<T: EGLGraphicsBackend + EGLWaylandExtensions + 'static> EGLWaylandExtensions
|
2018-02-08 12:58:54 +00:00
|
|
|
for GliumGraphicsBackend<T>
|
|
|
|
{
|
2018-01-05 19:04:46 +00:00
|
|
|
fn bind_wl_display(&self, display: &Display) -> EGLResult<EGLDisplay> {
|
2018-02-26 17:29:52 +00:00
|
|
|
(*self.backend).0.borrow().bind_wl_display(display)
|
2018-01-05 19:04:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-02 11:16:12 +00:00
|
|
|
unsafe impl<T: EGLGraphicsBackend> Backend for InternalBackend<T> {
|
2017-03-18 16:27:49 +00:00
|
|
|
fn swap_buffers(&self) -> Result<(), GliumSwapBuffersError> {
|
2018-02-26 17:29:52 +00:00
|
|
|
self.0.borrow().swap_buffers().map_err(Into::into)
|
2017-03-07 10:53:57 +00:00
|
|
|
}
|
|
|
|
|
2017-03-20 13:33:27 +00:00
|
|
|
unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void {
|
2018-02-26 17:29:52 +00:00
|
|
|
self.0.borrow().get_proc_address(symbol) as *const c_void
|
2017-03-07 10:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_framebuffer_dimensions(&self) -> (u32, u32) {
|
2018-02-26 17:29:52 +00:00
|
|
|
self.0.borrow().get_framebuffer_dimensions()
|
2017-03-07 10:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_current(&self) -> bool {
|
2018-02-26 17:29:52 +00:00
|
|
|
self.0.borrow().is_current()
|
2017-03-07 10:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn make_current(&self) {
|
2018-02-26 17:29:52 +00:00
|
|
|
self.0.borrow().make_current().expect("Context was lost")
|
2017-03-07 10:53:57 +00:00
|
|
|
}
|
|
|
|
}
|