smithay/src/backend/graphics/glium.rs

97 lines
2.8 KiB
Rust
Raw Normal View History

2017-06-02 11:16:12 +00:00
//! Glium compatibility module
2017-05-18 20:28:02 +00:00
use backend::graphics::egl::{EGLGraphicsBackend, SwapBuffersError};
2017-06-02 11:16:12 +00:00
use glium::Frame;
2017-03-07 10:53:57 +00:00
use glium::SwapBuffersError as GliumSwapBuffersError;
2017-06-02 11:16:12 +00:00
use glium::backend::{Backend, Context};
use glium::debug::DebugCallbackBehavior;
use std::ops::Deref;
2017-03-18 16:27:49 +00:00
use std::os::raw::c_void;
2017-06-02 11:16:12 +00:00
use std::rc::Rc;
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-06-02 11:16:12 +00:00
/// Wrapper to expose `glium` compatibility
pub struct GliumGraphicsBackend<T: EGLGraphicsBackend> {
context: Rc<Context>,
backend: Rc<InternalBackend<T>>,
}
struct InternalBackend<T: EGLGraphicsBackend>(T);
impl<T: EGLGraphicsBackend + 'static> GliumGraphicsBackend<T> {
fn new(backend: T) -> GliumGraphicsBackend<T> {
let internal = Rc::new(InternalBackend(backend));
GliumGraphicsBackend {
// cannot fail
context: unsafe {
Context::new::<_, ()>(internal.clone(), false, DebugCallbackBehavior::default()).unwrap()
},
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 {
Frame::new(self.context.clone(),
self.backend.get_framebuffer_dimensions())
}
}
impl<T: EGLGraphicsBackend> Deref for GliumGraphicsBackend<T> {
type Target = Context;
fn deref(&self) -> &Context {
&self.context
}
}
2017-03-18 16:27:49 +00:00
2017-06-02 11:16:12 +00:00
/// Converter trait to expose `glium` compatibility for all `EGLGraphicsBackend`s
2017-05-18 20:28:02 +00:00
pub trait IntoGlium: EGLGraphicsBackend + Sized {
2017-06-02 11:16:12 +00:00
/// Wrap the given `EGLGraphicsBackend` to a `GliumGraphicBackend`
fn into_glium(self) -> GliumGraphicsBackend<Self>;
2017-03-18 16:27:49 +00:00
}
2017-06-02 11:16:12 +00:00
impl<T: EGLGraphicsBackend + 'static> IntoGlium for T {
fn into_glium(self) -> GliumGraphicsBackend<Self> {
GliumGraphicsBackend::new(self)
2017-03-18 16:27:49 +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> {
self.0.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 {
2017-03-18 16:27:49 +00:00
self.0.get_proc_address(symbol) as *const c_void
2017-03-07 10:53:57 +00:00
}
fn get_framebuffer_dimensions(&self) -> (u32, u32) {
2017-03-18 16:27:49 +00:00
self.0.get_framebuffer_dimensions()
2017-03-07 10:53:57 +00:00
}
fn is_current(&self) -> bool {
2017-03-18 16:27:49 +00:00
self.0.is_current()
2017-03-07 10:53:57 +00:00
}
unsafe fn make_current(&self) {
2017-05-18 20:28:02 +00:00
self.0.make_current().expect("Context was lost")
2017-03-07 10:53:57 +00:00
}
}