This commit is contained in:
Drakulix 2017-06-10 23:29:09 +02:00
parent 2d255fd48d
commit eef617258e
2 changed files with 57 additions and 42 deletions

View File

@ -17,8 +17,8 @@ use std::ffi::{CStr, CString};
use std::fmt; use std::fmt;
use std::io; use std::io;
use std::mem; use std::mem;
use std::ptr;
use std::ops::Deref; use std::ops::Deref;
use std::ptr;
#[allow(non_camel_case_types, dead_code)] #[allow(non_camel_case_types, dead_code)]
mod ffi { mod ffi {
@ -155,8 +155,8 @@ impl EGLContext {
/// ///
/// This method is marked unsafe, because the contents of `NativeDisplay` cannot be verified and msy /// This method is marked unsafe, because the contents of `NativeDisplay` cannot be verified and msy
/// contain dangeling pointers are similar unsafe content /// contain dangeling pointers are similar unsafe content
pub unsafe fn new<L>(native: NativeDisplay, mut attributes: GlAttributes, reqs: PixelFormatRequirements, pub unsafe fn new<L>(native: NativeDisplay, mut attributes: GlAttributes,
logger: L) reqs: PixelFormatRequirements, logger: L)
-> Result<EGLContext, CreationError> -> Result<EGLContext, CreationError>
where L: Into<Option<::slog::Logger>> where L: Into<Option<::slog::Logger>>
{ {
@ -545,14 +545,21 @@ impl EGLContext {
/// ///
/// This method is marked unsafe, because the contents of `NativeSurface` cannot be verified and msy /// This method is marked unsafe, because the contents of `NativeSurface` cannot be verified and msy
/// contain dangeling pointers are similar unsafe content /// contain dangeling pointers are similar unsafe content
pub unsafe fn create_surface<'a>(&'a self, native: NativeSurface) -> Result<EGLSurface<'a>, CreationError> { pub unsafe fn create_surface<'a>(&'a self, native: NativeSurface)
-> Result<EGLSurface<'a>, CreationError> {
trace!(self.logger, "Creating EGL window surface..."); trace!(self.logger, "Creating EGL window surface...");
let surface = { let surface = {
let surface = match native { let surface = match native {
NativeSurface::X11(window) | NativeSurface::X11(window) |
NativeSurface::Wayland(window) | NativeSurface::Wayland(window) |
NativeSurface::Gbm(window) => self.egl.CreateWindowSurface(self.display, self.config_id, window, self.surface_attributes.as_ptr()), NativeSurface::Gbm(window) => {
self.egl
.CreateWindowSurface(self.display,
self.config_id,
window,
self.surface_attributes.as_ptr())
}
}; };
if surface.is_null() { if surface.is_null() {
@ -594,7 +601,8 @@ impl<'a> EGLSurface<'a> {
/// Swaps buffers at the end of a frame. /// Swaps buffers at the end of a frame.
pub fn swap_buffers(&self) -> Result<(), SwapBuffersError> { pub fn swap_buffers(&self) -> Result<(), SwapBuffersError> {
let ret = unsafe { let ret = unsafe {
self.context.egl self.context
.egl
.SwapBuffers(self.context.display as *const _, self.surface as *const _) .SwapBuffers(self.context.display as *const _, self.surface as *const _)
}; };
@ -615,7 +623,8 @@ impl<'a> EGLSurface<'a> {
/// This function is marked unsafe, because the context cannot be made current /// This function is marked unsafe, because the context cannot be made current
/// on multiple threads. /// on multiple threads.
pub unsafe fn make_current(&self) -> Result<(), SwapBuffersError> { pub unsafe fn make_current(&self) -> Result<(), SwapBuffersError> {
let ret = self.context.egl let ret = self.context
.egl
.MakeCurrent(self.context.display as *const _, .MakeCurrent(self.context.display as *const _,
self.surface as *const _, self.surface as *const _,
self.surface as *const _, self.surface as *const _,
@ -652,7 +661,8 @@ impl Drop for EGLContext {
impl<'a> Drop for EGLSurface<'a> { impl<'a> Drop for EGLSurface<'a> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
self.context.egl self.context
.egl
.DestroySurface(self.context.display as *const _, self.surface as *const _); .DestroySurface(self.context.display as *const _, self.surface as *const _);
} }
} }

View File

@ -2,8 +2,8 @@
use backend::{SeatInternal, TouchSlotInternal}; use backend::{SeatInternal, TouchSlotInternal};
use backend::graphics::GraphicsBackend; use backend::graphics::GraphicsBackend;
use backend::graphics::egl::{CreationError, EGLContext, EGLGraphicsBackend, GlAttributes, NativeDisplay, NativeSurface, use backend::graphics::egl::{CreationError, EGLContext, EGLGraphicsBackend, GlAttributes, NativeDisplay,
PixelFormat, PixelFormatRequirements, SwapBuffersError}; NativeSurface, PixelFormat, PixelFormatRequirements, SwapBuffersError};
use backend::input::{Axis, AxisSource, Event as BackendEvent, InputBackend, InputHandler, KeyState, use backend::input::{Axis, AxisSource, Event as BackendEvent, InputBackend, InputHandler, KeyState,
KeyboardKeyEvent, MouseButton, MouseButtonState, PointerAxisEvent, PointerButtonEvent, KeyboardKeyEvent, MouseButton, MouseButtonState, PointerAxisEvent, PointerButtonEvent,
PointerMotionAbsoluteEvent, Seat, SeatCapabilities, TouchCancelEvent, TouchDownEvent, PointerMotionAbsoluteEvent, Seat, SeatCapabilities, TouchCancelEvent, TouchDownEvent,
@ -103,18 +103,21 @@ pub fn init_from_builder_with_gl_attr<L>
let window = Rc::new(builder.build(&events_loop)?); let window = Rc::new(builder.build(&events_loop)?);
debug!(log, "Window created"); debug!(log, "Window created");
let (native_display, native_surface, surface) = let (native_display, native_surface, surface) = if let (Some(conn), Some(window)) =
if let (Some(conn), Some(window)) = (get_x11_xconnection(), window.get_xlib_window()) { (get_x11_xconnection(), window.get_xlib_window()) {
debug!(log, "Window is backed by X11"); debug!(log, "Window is backed by X11");
(NativeDisplay::X11(conn.display as *const _), NativeSurface::X11(window), None) (NativeDisplay::X11(conn.display as *const _), NativeSurface::X11(window), None)
} else if let (Some(display), Some(surface)) = (window.get_wayland_display(), window.get_wayland_client_surface()) { } else if let (Some(display), Some(surface)) =
(window.get_wayland_display(), window.get_wayland_client_surface()) {
debug!(log, "Window is backed by Wayland"); debug!(log, "Window is backed by Wayland");
let (w, h) = window.get_inner_size().unwrap(); let (w, h) = window.get_inner_size().unwrap();
let egl_surface = wegl::WlEglSurface::new(surface, w as i32, h as i32); let egl_surface = wegl::WlEglSurface::new(surface, w as i32, h as i32);
(NativeDisplay::Wayland(display), NativeSurface::Wayland(egl_surface.ptr() as *const _), Some(egl_surface)) (NativeDisplay::Wayland(display),
NativeSurface::Wayland(egl_surface.ptr() as *const _),
Some(egl_surface))
} else { } else {
error!(log, "Window is backed by an unsupported graphics framework"); error!(log, "Window is backed by an unsupported graphics framework");
return Err(CreationError::NotSupported) return Err(CreationError::NotSupported);
}; };
let context = unsafe { let context = unsafe {
@ -137,7 +140,9 @@ pub fn init_from_builder_with_gl_attr<L>
Ok((WinitGraphicsBackend { Ok((WinitGraphicsBackend {
window: window.clone(), window: window.clone(),
context: match egl::RentEGL::try_new(Box::new(context), move |context| unsafe { context.create_surface(native_surface) }) { context: match egl::RentEGL::try_new(Box::new(context), move |context| unsafe {
context.create_surface(native_surface)
}) {
Ok(x) => x, Ok(x) => x,
Err(::rental::TryNewError(err, _)) => return Err(err), Err(::rental::TryNewError(err, _)) => return Err(err),
}, },