glium/backend/
mod.rs

1/*!
2
3The `backend` module allows one to link between glium and the OpenGL context..
4
5There are three concepts in play:
6
7 - The `Backend` trait describes the glue between glium and the OpenGL context provider like
8   glutin, SDL, GLFW, etc.
9 - The `Context` struct is the main brick of glium. It manages everything that glium needs to
10   execute OpenGL commands. Creating a `Context` requires a `Backend`.
11 - The `Facade` trait. Calling functions like `VertexBuffer::new` requires passing an object
12   that implements this trait. It is implemented on `Rc<Context>`.
13
14*/
15use std::ops::Deref;
16use std::os::raw::c_void;
17use std::rc::Rc;
18
19use crate::CapabilitiesSource;
20use crate::SwapBuffersError;
21
22use crate::version::Version;
23
24pub use crate::context::Capabilities;
25pub use crate::context::Context;
26pub use crate::context::ExtensionsList;
27pub use crate::context::ReleaseBehavior;
28
29#[cfg(feature = "glutin")]
30pub mod glutin;
31
32use ::glutin::surface::SwapInterval;
33#[cfg(feature = "simple_window_builder")]
34pub use winit;
35
36/// Trait for types that can be used as a backend for a glium context.
37///
38/// This trait is unsafe, as you can get undefined behaviors or crashes if you don't implement
39/// the methods correctly.
40pub unsafe trait Backend {
41    /// Swaps buffers at the end of a frame.
42    fn swap_buffers(&self) -> Result<(), SwapBuffersError>;
43
44    /// Returns the address of an OpenGL function.
45    ///
46    /// Supposes that the context has been made current before this function is called.
47    unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void;
48
49    /// Returns the dimensions of the window, or screen, etc.
50    fn get_framebuffer_dimensions(&self) -> (u32, u32);
51
52    /// Resizes the underlying surface, should be called when the window's size has changed for example.
53    fn resize(&self, new_size: (u32, u32));
54
55    /// Set swap interval for the surface.
56    fn set_swap_interval(&self, interval: SwapInterval);
57
58    /// Returns true if the OpenGL context is the current one in the thread.
59    fn is_current(&self) -> bool;
60
61    /// Makes the OpenGL context the current context in the current thread.
62    unsafe fn make_current(&self);
63}
64
65unsafe impl<T> Backend for Rc<T>
66where
67    T: Backend,
68{
69    fn swap_buffers(&self) -> Result<(), SwapBuffersError> {
70        self.deref().swap_buffers()
71    }
72
73    unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void {
74        self.deref().get_proc_address(symbol)
75    }
76
77    fn get_framebuffer_dimensions(&self) -> (u32, u32) {
78        self.deref().get_framebuffer_dimensions()
79    }
80
81    fn resize(&self, new_size: (u32, u32)) {
82        self.deref().resize(new_size);
83    }
84
85    fn set_swap_interval(&self, interval: SwapInterval) {
86        self.deref().set_swap_interval(interval);
87    }
88
89    fn is_current(&self) -> bool {
90        self.deref().is_current()
91    }
92
93    unsafe fn make_current(&self) {
94        self.deref().make_current();
95    }
96}
97
98/// Trait for types that provide a safe access for glium functions.
99pub trait Facade {
100    /// Returns an opaque type that contains the OpenGL state, extensions, version, etc.
101    fn get_context(&self) -> &Rc<Context>;
102}
103
104impl<T: ?Sized> CapabilitiesSource for T
105where
106    T: Facade,
107{
108    fn get_version(&self) -> &Version {
109        self.get_context().deref().get_opengl_version()
110    }
111
112    fn get_extensions(&self) -> &ExtensionsList {
113        self.get_context().deref().get_extensions()
114    }
115
116    fn get_capabilities(&self) -> &Capabilities {
117        self.get_context().deref().get_capabilities()
118    }
119}
120
121impl Facade for Rc<Context> {
122    #[inline]
123    fn get_context(&self) -> &Rc<Context> {
124        self
125    }
126}