Skip to content

Commit

Permalink
os: implement native_display() for GlContextExt trait
Browse files Browse the repository at this point in the history
Fixes: #1078
  • Loading branch information
ceyusa committed Nov 9, 2018
1 parent 3377691 commit 208ce71
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/api/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,9 @@ impl Context {
pub unsafe fn raw_handle(&self) -> egl::ffi::EGLContext {
self.0.egl_context.raw_handle()
}

#[inline]
pub unsafe fn native_display(&self) -> egl::ffi::EGLDisplay {
self.0.egl_context.native_display()
}
}
1 change: 1 addition & 0 deletions src/api/egl/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extern crate winapi;

pub use self::egl::types::EGLContext;
pub use self::egl::types::EGLDisplay;

use libc;

Expand Down
5 changes: 5 additions & 0 deletions src/api/egl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ impl Context {
self.context
}

#[inline]
pub unsafe fn native_display(&self) -> ffi::egl::types::EGLDisplay {
self.display
}

// Handle Android Life Cycle.
// Android has started the activity or sent it to foreground.
// Create a new surface and attach it to the recreated ANativeWindow.
Expand Down
5 changes: 5 additions & 0 deletions src/api/glx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ impl Context {
pub unsafe fn raw_handle(&self) -> ffi::GLXContext {
self.context
}

#[inline]
pub unsafe fn native_display(&self) -> *mut ffi::Display {
self.xconn.display
}
}

unsafe impl Send for Context {}
Expand Down
6 changes: 6 additions & 0 deletions src/api/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,10 @@ impl GlContextExt for Context {
unsafe fn raw_handle(&self) -> *mut c_void {
self.eagl_context as *mut c_void
}

type DisplayHandle = u8;
#[inline]
unsafe fn native_display(&self) -> Self::DisplayHandle {
0
}
}
9 changes: 8 additions & 1 deletion src/os/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
pub use winit::os::android::{WindowBuilderExt, WindowExt};

pub use api::egl::ffi::EGLContext;
pub use api::egl::ffi::EGLDisplay;

use Context;
use os::GlContextExt;
use Context;

impl GlContextExt for Context {
type Handle = EGLContext;
Expand All @@ -14,4 +15,10 @@ impl GlContextExt for Context {
unsafe fn raw_handle(&self) -> Self::Handle {
self.context.raw_handle()
}

type DisplayHandle = EGLDisplay;
#[inline]
unsafe fn native_display(&self) -> Self::DisplayHandle {
self.context.native_display()
}
}
6 changes: 6 additions & 0 deletions src/os/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ impl GlContextExt for Context {
unsafe fn raw_handle(&self) -> Self::Handle {
self.context.raw_handle()
}

type DisplayHandle = u8;
#[inline]
unsafe fn native_display(&self) -> Self::DisplayHandle {
0
}
}
6 changes: 6 additions & 0 deletions src/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ pub trait GlContextExt {

/// Returns the raw context handle.
unsafe fn raw_handle(&self) -> Self::Handle;

/// Raw native display handle.
type DisplayHandle;

/// Return the native display handle.
unsafe fn native_display(&self) -> Self::DisplayHandle;
}
10 changes: 9 additions & 1 deletion src/os/unix.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))]

pub use api::egl::ffi::EGLContext;
pub use api::egl::ffi::EGLDisplay;
pub use api::glx::ffi::Display;
pub use api::glx::ffi::GLXContext;
pub use platform::RawHandle;
pub use platform::{RawDisplayHandle, RawHandle};

pub use winit::os::unix::XNotSupported;
pub use winit::os::unix::XWindowType;
Expand All @@ -21,4 +23,10 @@ impl GlContextExt for Context {
unsafe fn raw_handle(&self) -> Self::Handle {
self.context.raw_handle()
}

type DisplayHandle = RawDisplayHandle;
#[inline]
unsafe fn native_display(&self) -> Self::DisplayHandle {
self.context.native_display()
}
}
9 changes: 8 additions & 1 deletion src/os/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ pub use winapi::shared::windef::HGLRC;
pub use winit::os::windows::{DeviceIdExt, MonitorIdExt, WindowBuilderExt, WindowExt};

pub use api::egl::ffi::EGLContext;
pub use platform::RawHandle;
pub use api::egl::ffi::EGLDisplay;
pub use platform::{RawDisplayHandle, RawHandle};

use os::GlContextExt;
use Context;
Expand All @@ -16,4 +17,10 @@ impl GlContextExt for Context {
unsafe fn raw_handle(&self) -> Self::Handle {
self.context.raw_handle()
}

type DisplayHandle = RawDisplayHandle;
#[inline]
unsafe fn native_display(&self) -> Self::DisplayHandle {
self.context.native_display()
}
}
27 changes: 26 additions & 1 deletion src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use {ContextError, CreationError, GlAttributes, PixelFormat, PixelFormatRequirements};
use api::egl;
use api::glx;
use self::x11::GlContext;
use self::x11::{GlContext, GlDisplay};

use winit;
use winit::os::unix::EventsLoopExt;
Expand All @@ -20,6 +20,14 @@ pub enum RawHandle {
Egl(egl::ffi::EGLContext),
}

/// Display handles available on Unix-like platforms.
#[derive(Clone, Debug)]
pub enum RawDisplayHandle {
Glx(*mut glx::ffi::Display),
Egl(egl::ffi::EGLDisplay),
None,
}

pub enum ContextType {
X11,
Wayland,
Expand Down Expand Up @@ -213,4 +221,21 @@ impl Context {
Context::OsMesa(ref ctxt) => RawHandle::Egl(ctxt.raw_handle()),
}
}

#[inline]
pub unsafe fn native_display(&self) -> RawDisplayHandle {
match *self {
Context::WindowedX11(ref ctxt) | Context::HeadlessX11(_, ref ctxt) => {
match ctxt.native_display() {
GlDisplay::Glx(display) => RawDisplayHandle::Glx(display),
GlDisplay::Egl(display) => RawDisplayHandle::Egl(display),
GlDisplay::None => RawDisplayHandle::None,
}
}
Context::WindowedWayland(ref ctxt) | Context::HeadlessWayland(_, ref ctxt) => {
RawDisplayHandle::Egl(ctxt.native_display())
}
Context::OsMesa(_) => RawDisplayHandle::None,
}
}
}
5 changes: 5 additions & 0 deletions src/platform/linux/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,9 @@ impl Context {
pub unsafe fn raw_handle(&self) -> ffi::EGLContext {
self.context.raw_handle()
}

#[inline]
pub unsafe fn native_display(&self) -> ffi::EGLDisplay {
self.context.native_display()
}
}
17 changes: 17 additions & 0 deletions src/platform/linux/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use {Api, ContextError, CreationError, GlAttributes, GlRequest, PixelFormat, Pix
use api::glx::{ffi, Context as GlxContext};
use api::{dlopen, egl};
use api::egl::Context as EglContext;
use api::glx::ffi::Display as GlxDisplay;
use api::glx::ffi::glx::Glx;
use api::egl::ffi::EGLDisplay as EglDisplay;
use api::egl::ffi::egl::Egl;

#[derive(Debug)]
Expand Down Expand Up @@ -88,6 +90,12 @@ pub enum GlContext {
None,
}

pub enum GlDisplay {
Glx(*mut GlxDisplay),
Egl(EglDisplay),
None,
}

pub struct Context {
xconn: Arc<XConnection>,
colormap: ffi::Colormap,
Expand Down Expand Up @@ -297,4 +305,13 @@ impl Context {
pub unsafe fn raw_handle(&self) -> &GlContext {
&self.context
}

#[inline]
pub unsafe fn native_display(&self) -> GlDisplay {
match self.context {
GlContext::Glx(ref ctxt) => GlDisplay::Glx(ctxt.native_display()),
GlContext::Egl(ref ctxt) => GlDisplay::Egl(ctxt.native_display()),
GlContext::None => GlDisplay::None,
}
}
}
12 changes: 11 additions & 1 deletion src/platform/windows/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use api::egl::ffi::egl::Egl;
use api::egl::Context as EglContext;
use api::wgl::Context as WglContext;
use os::windows::WindowExt;
use platform::RawHandle;
use platform::{RawDisplayHandle, RawHandle};

unsafe impl Send for Context {}
unsafe impl Sync for Context {}
Expand Down Expand Up @@ -187,4 +187,14 @@ impl Context {
| Context::EglPbuffer(ref c) => RawHandle::Egl(c.raw_handle()),
}
}

#[inline]
pub unsafe fn native_display(&self) -> RawDisplayHandle {
match *self {
Context::Wgl(ref c) | Context::HiddenWindowWgl(_, ref c) => RawDisplayHandle::Wgl,
Context::Egl(ref c)
| Context::HiddenWindowEgl(_, ref c)
| Context::EglPbuffer(ref c) => RawDisplayHandle::Egl(c.native_display()),
}
}
}
7 changes: 7 additions & 0 deletions src/platform/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ pub enum RawHandle {
Wgl(HGLRC),
}

/// Display handles available on Windows.
#[derive(Clone, Debug)]
pub enum RawDisplayHandle {
Egl(egl::ffi::EGLDisplay),
Wgl,
}

/// Stupid wrapper because `*const libc::c_void` doesn't implement `Sync`.
struct EglWrapper(Egl);
unsafe impl Sync for EglWrapper {}
Expand Down

0 comments on commit 208ce71

Please sign in to comment.