Flesh out glium compatibility
+//! Glium compatibility module
+
use backend::graphics::egl::{EGLGraphicsBackend, SwapBuffersError};
+use glium::Frame;
use glium::SwapBuffersError as GliumSwapBuffersError;
-use glium::backend::Backend;
-
+use glium::backend::{Backend, Context};
+use glium::debug::DebugCallbackBehavior;
+use std::ops::Deref;
use std::os::raw::c_void;
+use std::rc::Rc;
+
impl From<SwapBuffersError> for GliumSwapBuffersError {
fn from(error: SwapBuffersError) -> Self {
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> {
+ 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
+ }
+}
+/// Converter trait to expose `glium` compatibility for all `EGLGraphicsBackend`s
pub trait IntoGlium: EGLGraphicsBackend + Sized {
- fn into_glium(self) -> GliumGraphicBackend<Self>;
+ /// Wrap the given `EGLGraphicsBackend` to a `GliumGraphicBackend`
+ fn into_glium(self) -> GliumGraphicsBackend<Self>;
}
-impl<T: EGLGraphicsBackend> IntoGlium for T {
- fn into_glium(self) -> GliumGraphicBackend<Self> {
- GliumGraphicBackend(self)
+impl<T: EGLGraphicsBackend + 'static> IntoGlium for T {
+ fn into_glium(self) -> GliumGraphicsBackend<Self> {
+ GliumGraphicsBackend::new(self)
}
}
-unsafe impl<T: EGLGraphicsBackend> Backend for GliumGraphicBackend<T> {
+unsafe impl<T: EGLGraphicsBackend> Backend for InternalBackend<T> {
fn swap_buffers(&self) -> Result<(), GliumSwapBuffersError> {
self.0.swap_buffers().map_err(Into::into)
}
