Flesh out glium compatibility
This commit is contained in:
parent
115eb4d200
commit
2c9f6a7479
|
@ -1,9 +1,15 @@
|
||||||
use backend::graphics::egl::{EGLGraphicsBackend, SwapBuffersError};
|
//! Glium compatibility module
|
||||||
use glium::SwapBuffersError as GliumSwapBuffersError;
|
|
||||||
use glium::backend::Backend;
|
|
||||||
|
|
||||||
|
use backend::graphics::egl::{EGLGraphicsBackend, SwapBuffersError};
|
||||||
|
use glium::Frame;
|
||||||
|
use glium::SwapBuffersError as GliumSwapBuffersError;
|
||||||
|
use glium::backend::{Backend, Context};
|
||||||
|
use glium::debug::DebugCallbackBehavior;
|
||||||
|
use std::ops::Deref;
|
||||||
use std::os::raw::c_void;
|
use std::os::raw::c_void;
|
||||||
|
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
impl From<SwapBuffersError> for GliumSwapBuffersError {
|
impl From<SwapBuffersError> for GliumSwapBuffersError {
|
||||||
fn from(error: SwapBuffersError) -> Self {
|
fn from(error: SwapBuffersError) -> Self {
|
||||||
match error {
|
match error {
|
||||||
|
@ -13,19 +19,61 @@ impl From<SwapBuffersError> for GliumSwapBuffersError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct GliumGraphicBackend<T: EGLGraphicsBackend>(T);
|
/// Wrapper to expose `glium` compatibility
|
||||||
|
pub struct GliumGraphicsBackend<T: EGLGraphicsBackend> {
|
||||||
pub trait IntoGlium: EGLGraphicsBackend + Sized {
|
context: Rc<Context>,
|
||||||
fn into_glium(self) -> GliumGraphicBackend<Self>;
|
backend: Rc<InternalBackend<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: EGLGraphicsBackend> IntoGlium for T {
|
struct InternalBackend<T: EGLGraphicsBackend>(T);
|
||||||
fn into_glium(self) -> GliumGraphicBackend<Self> {
|
|
||||||
GliumGraphicBackend(self)
|
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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<T: EGLGraphicsBackend> Backend for GliumGraphicBackend<T> {
|
impl<T: EGLGraphicsBackend> Deref for GliumGraphicsBackend<T> {
|
||||||
|
type Target = Context;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Context {
|
||||||
|
&self.context
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Converter trait to expose `glium` compatibility for all `EGLGraphicsBackend`s
|
||||||
|
pub trait IntoGlium: EGLGraphicsBackend + Sized {
|
||||||
|
/// Wrap the given `EGLGraphicsBackend` to a `GliumGraphicBackend`
|
||||||
|
fn into_glium(self) -> GliumGraphicsBackend<Self>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: EGLGraphicsBackend + 'static> IntoGlium for T {
|
||||||
|
fn into_glium(self) -> GliumGraphicsBackend<Self> {
|
||||||
|
GliumGraphicsBackend::new(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe impl<T: EGLGraphicsBackend> Backend for InternalBackend<T> {
|
||||||
fn swap_buffers(&self) -> Result<(), GliumSwapBuffersError> {
|
fn swap_buffers(&self) -> Result<(), GliumSwapBuffersError> {
|
||||||
self.0.swap_buffers().map_err(Into::into)
|
self.0.swap_buffers().map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue