/
glium.rs
122 lines (105 loc) · 3.74 KB
/
glium.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Glium compatibility module
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,
};
use wayland_server::Display;
impl From<SwapBuffersError> for GliumSwapBuffersError {
fn from(error: SwapBuffersError) -> Self {
match error {
SwapBuffersError::ContextLost => GliumSwapBuffersError::ContextLost,
SwapBuffersError::AlreadySwapped => GliumSwapBuffersError::AlreadySwapped,
SwapBuffersError::Unknown(_) => GliumSwapBuffersError::ContextLost, // TODO
}
}
}
/// Wrapper to expose `glium` compatibility
pub struct GliumGraphicsBackend<T: EGLGraphicsBackend> {
context: Rc<Context>,
backend: Rc<InternalBackend<T>>,
}
struct InternalBackend<T: EGLGraphicsBackend>(RefCell<T>);
impl<T: EGLGraphicsBackend + 'static> GliumGraphicsBackend<T> {
fn new(backend: T) -> GliumGraphicsBackend<T> {
let internal = Rc::new(InternalBackend(RefCell::new(backend)));
GliumGraphicsBackend {
// cannot fail
context: unsafe {
Context::new(internal.clone(), true, 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())
}
/// 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
/// mutably will panic. Note that glium will borrow the backend
/// (not mutably) during rendering.
pub fn borrow_mut(&self) -> RefMut<T> {
self.backend.0.borrow_mut()
}
}
impl<T: EGLGraphicsBackend> Facade for GliumGraphicsBackend<T> {
fn get_context(&self) -> &Rc<Context> {
&self.context
}
}
impl<T: EGLGraphicsBackend + 'static> From<T> for GliumGraphicsBackend<T> {
fn from(backend: T) -> Self {
GliumGraphicsBackend::new(backend)
}
}
impl<T: EGLGraphicsBackend + EGLWaylandExtensions + 'static> EGLWaylandExtensions
for GliumGraphicsBackend<T>
{
fn bind_wl_display(&self, display: &Display) -> EGLResult<EGLDisplay> {
(*self.backend).0.borrow().bind_wl_display(display)
}
}
unsafe impl<T: EGLGraphicsBackend> Backend for InternalBackend<T> {
fn swap_buffers(&self) -> Result<(), GliumSwapBuffersError> {
self.0.borrow().swap_buffers().map_err(Into::into)
}
unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void {
self.0.borrow().get_proc_address(symbol) as *const c_void
}
fn get_framebuffer_dimensions(&self) -> (u32, u32) {
self.0.borrow().get_framebuffer_dimensions()
}
fn is_current(&self) -> bool {
self.0.borrow().is_current()
}
unsafe fn make_current(&self) {
self.0.borrow().make_current().expect("Context was lost")
}
}