From ac0dc42e9ef1de172c47678d5359cdef466decd8 Mon Sep 17 00:00:00 2001 From: Victor Brekenfeld Date: Wed, 21 Nov 2018 09:21:12 +0100 Subject: [PATCH] Add seperate GL module - Move parts of glium & egl module into own module - Add raw GL loader as an alternative --- src/backend/graphics/gl.rs | 76 +++++++++++++++++++++++++++++++++++ src/backend/graphics/glium.rs | 28 +++++-------- src/backend/graphics/mod.rs | 3 +- src/backend/mod.rs | 2 + 4 files changed, 89 insertions(+), 20 deletions(-) create mode 100644 src/backend/graphics/gl.rs diff --git a/src/backend/graphics/gl.rs b/src/backend/graphics/gl.rs new file mode 100644 index 0000000..5f120b5 --- /dev/null +++ b/src/backend/graphics/gl.rs @@ -0,0 +1,76 @@ +use nix::libc::c_void; + +use super::SwapBuffersError; + +#[cfg_attr(feature = "cargo-clippy", allow(clippy))] +#[allow(missing_docs)] +pub(crate) mod ffi { + include!(concat!(env!("OUT_DIR"), "/gl_bindings.rs")); +} + +pub use self::ffi::Gles2; + +/// Describes the pixel format of the main framebuffer +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct PixelFormat { + /// is the format hardware accelerated + pub hardware_accelerated: bool, + /// number of bits used for colors + pub color_bits: u8, + /// number of bits used for alpha channel + pub alpha_bits: u8, + /// number of bits used for depth channel + pub depth_bits: u8, + /// number of bits used for stencil buffer + pub stencil_bits: u8, + /// is stereoscopy enabled + pub stereoscopy: bool, + /// is double buffering enabled + pub double_buffer: bool, + /// number of samples used for multisampling if enabled + pub multisampling: Option, + /// is srgb enabled + pub srgb: bool, +} + +/// Trait that describes objects that have an OpenGL context +/// and can be used to render upon +pub trait GLGraphicsBackend { + /// Swaps buffers at the end of a frame. + fn swap_buffers(&self) -> Result<(), SwapBuffersError>; + + /// Returns the address of an OpenGL function. + /// + /// Supposes that the context has been made current before this function is called. + unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void; + + /// Returns the dimensions of the window, or screen, etc in points. + /// + /// That are the scaled pixels of the underlying graphics backend. + /// For nested compositors this will respect the scaling of the root compositor. + /// For drawing directly onto hardware this unit will be equal to actual pixels. + fn get_framebuffer_dimensions(&self) -> (u32, u32); + + /// Returns true if the OpenGL context is the current one in the thread. + fn is_current(&self) -> bool; + + /// Makes the OpenGL context the current context in the current thread. + /// + /// # Unsafety + /// + /// This function is marked unsafe, because the context cannot be made current + /// on multiple threads. + unsafe fn make_current(&self) -> Result<(), SwapBuffersError>; + + /// Returns the pixel format of the main framebuffer of the context. + fn get_pixel_format(&self) -> PixelFormat; +} + +/// Loads a Raw GLES Interface for a given `GLGraphicsBackend` +/// +/// This remains valid as long as the underlying `GLGraphicsBackend` is alive +/// and may only be used in combination with the backend. Using this with any +/// other gl context may cause undefined behavior. +pub fn load_raw_gl(backend: &B) -> Gles2 { + Gles2::load_with(|s| unsafe { backend.get_proc_address(s) as *const _ }) +} \ No newline at end of file diff --git a/src/backend/graphics/glium.rs b/src/backend/graphics/glium.rs index 4c242fe..caf1b1f 100644 --- a/src/backend/graphics/glium.rs +++ b/src/backend/graphics/glium.rs @@ -1,9 +1,8 @@ //! Glium compatibility module -use backend::graphics::egl::{ - error::Result as EGLResult, - wayland::{EGLDisplay, EGLWaylandExtensions}, - EGLGraphicsBackend, SwapBuffersError, +use backend::graphics::{ + gl::GLGraphicsBackend, + SwapBuffersError, }; use glium::{ backend::{Backend, Context, Facade}, @@ -15,7 +14,6 @@ use std::{ os::raw::c_void, rc::Rc, }; -use wayland_server::Display; impl From for GliumSwapBuffersError { fn from(error: SwapBuffersError) -> Self { @@ -28,14 +26,14 @@ impl From for GliumSwapBuffersError { } /// Wrapper to expose `Glium` compatibility -pub struct GliumGraphicsBackend { +pub struct GliumGraphicsBackend { context: Rc, backend: Rc>, } -struct InternalBackend(RefCell); +struct InternalBackend(RefCell); -impl GliumGraphicsBackend { +impl GliumGraphicsBackend { fn new(backend: T) -> GliumGraphicsBackend { let internal = Rc::new(InternalBackend(RefCell::new(backend))); @@ -79,27 +77,19 @@ impl GliumGraphicsBackend { } } -impl Facade for GliumGraphicsBackend { +impl Facade for GliumGraphicsBackend { fn get_context(&self) -> &Rc { &self.context } } -impl From for GliumGraphicsBackend { +impl From for GliumGraphicsBackend { fn from(backend: T) -> Self { GliumGraphicsBackend::new(backend) } } -impl EGLWaylandExtensions - for GliumGraphicsBackend -{ - fn bind_wl_display(&self, display: &Display) -> EGLResult { - (*self.backend).0.borrow().bind_wl_display(display) - } -} - -unsafe impl Backend for InternalBackend { +unsafe impl Backend for InternalBackend { fn swap_buffers(&self) -> Result<(), GliumSwapBuffersError> { self.0.borrow().swap_buffers().map_err(Into::into) } diff --git a/src/backend/graphics/mod.rs b/src/backend/graphics/mod.rs index e9470e8..323cc39 100644 --- a/src/backend/graphics/mod.rs +++ b/src/backend/graphics/mod.rs @@ -35,7 +35,8 @@ pub trait GraphicsBackend { ) -> Result<(), Self::Error>; } -pub mod egl; +#[cfg(feature = "renderer_gl")] +pub mod gl; #[cfg(feature = "renderer_glium")] pub mod glium; pub mod software; diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 328f605..ba94a50 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -19,6 +19,8 @@ pub mod input; #[cfg(feature = "backend_drm")] pub mod drm; +#[cfg(feature = "backend_egl")] +pub mod egl; #[cfg(feature = "backend_libinput")] pub mod libinput; #[cfg(feature = "backend_session")]