use crate::ContextRef;
use crate::egl::EglContext;
use std::cell::RefCell;
use std::ffi::c_void;
use std::rc::Rc;
pub struct Backend{
ctx: ContextRef,
size: Rc<RefCell<(u32, u32)>>,
}
impl Backend {
fn new(ctx: ContextRef, size: (u32, u32)) -> Self {
Backend { ctx, size: Rc::new(RefCell::new(size)) }
}
pub unsafe fn get_proc_address(name: &str) -> *const c_void {
ContextRef::get_proc_address(name)
}
}
unsafe impl glium::backend::Backend for Backend {
fn swap_buffers(&self) -> Result<(), glium::SwapBuffersError> { Ok(()) }
unsafe fn get_proc_address(&self, name: &str) -> *const c_void {
Backend::get_proc_address(name)
}
fn get_framebuffer_dimensions(&self) -> (u32, u32) {
let _ = *self.size.borrow();
unimplemented!("There is no default framebuffer. Use an explicit surface.");
}
fn is_current(&self) -> bool { false }
unsafe fn make_current(&self) {
unsafe { self.ctx.force_make_current() }
}
fn resize(&self, new_size:(u32, u32)) {
*self.size.borrow_mut() = new_size;
unimplemented!("There is no default framebuffer. Use an explicit surface.");
}
}
pub struct Facade {
ctx: Rc<glium::backend::Context>,
egl: ContextRef,
}
impl glium::backend::Facade for Facade {
fn get_context(&self) -> &Rc<glium::backend::Context> { &self.ctx }
}
impl Facade {
pub fn new(ctx: ContextRef, size: (u32, u32)) -> Self {
Self {
ctx: {
let _current_lock = ctx.make_current();
unsafe {
glium::backend::Context::new(
Backend::new(ctx.clone(), size),
false,
glium::debug::DebugCallbackBehavior::PrintAll
)
}.unwrap()
},
egl: ctx,
}
}
pub fn get_egl_context(&self) -> &ContextRef {
&self.egl
}
pub fn new_unsized(ctx: ContextRef) -> Self {
Self::new(ctx, (0, 0))
}
}