From 90b188bffa3d7f54c1a4540876475846ccee13bf Mon Sep 17 00:00:00 2001 From: Eyal Kalderon Date: Tue, 3 Oct 2017 18:51:39 -0400 Subject: [PATCH 1/5] Implement GlContextExt for getting raw context handles (fixes #935) --- src/api/android/mod.rs | 25 ++++++++++++++++++++++--- src/api/caca/mod.rs | 1 - src/api/egl/mod.rs | 5 +++++ src/api/glx/mod.rs | 5 +++++ src/api/ios/mod.rs | 8 ++------ src/api/osmesa/mod.rs | 9 +++++++++ src/api/wgl/mod.rs | 8 ++++++-- src/headless.rs | 9 +++++++++ src/lib.rs | 8 ++++++++ src/os/android.rs | 2 ++ src/os/macos.rs | 1 + src/os/mod.rs | 13 ++++++++++++- src/os/unix.rs | 11 +++++++++++ src/os/windows.rs | 10 ++++++++++ src/platform/android/mod.rs | 1 + src/platform/emscripten/mod.rs | 21 +++++++++++++++++++++ src/platform/ios/mod.rs | 24 ++++++++++++++++++++++-- src/platform/linux/mod.rs | 28 ++++++++++++++++++++++++---- src/platform/linux/wayland.rs | 9 +++++++-- src/platform/linux/x11.rs | 10 ++++++++++ src/platform/macos/headless.rs | 10 ++++++++++ src/platform/macos/mod.rs | 17 ++++++++++++++--- src/platform/windows/context.rs | 11 +++++++++-- src/platform/windows/mod.rs | 21 +++++++++++++++++++++ 24 files changed, 241 insertions(+), 26 deletions(-) mode change 100755 => 100644 src/platform/macos/mod.rs diff --git a/src/api/android/mod.rs b/src/api/android/mod.rs index 03bf49e390..b67dd31b14 100644 --- a/src/api/android/mod.rs +++ b/src/api/android/mod.rs @@ -16,6 +16,7 @@ use PixelFormatRequirements; use api::egl; use api::egl::Context as EglContext; +use os::GlContextExt; mod ffi; @@ -23,9 +24,6 @@ pub struct Context { egl_context: EglContext, } -#[derive(Clone, Default)] -pub struct PlatformSpecificHeadlessBuilderAttributes; - impl Context { pub fn new( window_builder: winit::WindowBuilder, @@ -83,6 +81,18 @@ impl Context { } } +impl GlContextExt for Context { + type Handle = egl::ffi::egl::types::EGLContext; + + #[inline] + unsafe fn as_mut_ptr(&self) -> Self::Handle { + self.egl_context.as_mut_ptr() + } +} + +#[derive(Clone, Default)] +pub struct PlatformSpecificHeadlessBuilderAttributes; + pub struct HeadlessContext(EglContext); unsafe impl Send for HeadlessContext {} @@ -136,3 +146,12 @@ impl HeadlessContext { self.0.get_pixel_format() } } + +impl GlContextExt for HeadlessContext { + type Handle = egl::ffi::egl::types::EGLContext; + + #[inline] + unsafe fn as_mut_ptr(&self) -> Self::Handle { + self.0.as_mut_ptr() + } +} diff --git a/src/api/caca/mod.rs b/src/api/caca/mod.rs index 6f2842c1ff..ff56454f7c 100644 --- a/src/api/caca/mod.rs +++ b/src/api/caca/mod.rs @@ -111,7 +111,6 @@ impl Context { pub fn get_pixel_format(&self) -> PixelFormat { self.opengl.get_pixel_format() } - } impl Drop for Context { diff --git a/src/api/egl/mod.rs b/src/api/egl/mod.rs index d0c92e8c1d..535dfd411d 100644 --- a/src/api/egl/mod.rs +++ b/src/api/egl/mod.rs @@ -320,6 +320,11 @@ impl Context { pub fn get_pixel_format(&self) -> PixelFormat { self.pixel_format.clone() } + + #[inline] + pub unsafe fn as_mut_ptr(&self) -> ffi::egl::types::EGLContext { + self.context + } } unsafe impl Send for Context {} diff --git a/src/api/glx/mod.rs b/src/api/glx/mod.rs index 36b70f3811..59c2e1937d 100644 --- a/src/api/glx/mod.rs +++ b/src/api/glx/mod.rs @@ -143,6 +143,11 @@ impl Context { pub fn get_pixel_format(&self) -> PixelFormat { self.pixel_format.clone() } + + #[inline] + pub unsafe fn as_mut_ptr(&self) -> ffi::GLXContext { + self.context + } } unsafe impl Send for Context {} diff --git a/src/api/ios/mod.rs b/src/api/ios/mod.rs index 8bee5134ee..8a906bf3c8 100644 --- a/src/api/ios/mod.rs +++ b/src/api/ios/mod.rs @@ -343,12 +343,10 @@ impl Window { } #[inline] - pub fn set_window_resize_callback(&mut self, _: Option) { - } + pub fn set_window_resize_callback(&mut self, _: Option) {} #[inline] - pub fn set_cursor(&self, _: MouseCursor) { - } + pub fn set_cursor(&self, _: MouseCursor) {} #[inline] pub fn set_cursor_state(&self, _: CursorState) -> Result<(), String> { @@ -369,7 +367,6 @@ impl Window { pub fn create_window_proxy(&self) -> WindowProxy { WindowProxy } - } impl GlContext for Window { @@ -427,7 +424,6 @@ impl WindowProxy { } } - impl<'a> Iterator for WaitEventsIterator<'a> { type Item = Event; diff --git a/src/api/osmesa/mod.rs b/src/api/osmesa/mod.rs index ee3a82d8f4..0e2f8d989a 100644 --- a/src/api/osmesa/mod.rs +++ b/src/api/osmesa/mod.rs @@ -18,6 +18,10 @@ use PixelFormatRequirements; use Robustness; use libc; +pub mod ffi { + pub use super::osmesa_sys::OSMesaContext; +} + pub struct OsMesaContext { context: osmesa_sys::OSMesaContext, buffer: Vec, @@ -186,6 +190,11 @@ impl OsMesaContext { pub fn get_pixel_format(&self) -> PixelFormat { unimplemented!(); } + + #[inline] + pub unsafe fn as_mut_ptr(&self) -> osmesa_sys::OSMesaContext { + self.context + } } impl Drop for OsMesaContext { diff --git a/src/api/wgl/mod.rs b/src/api/wgl/mod.rs index 9a224d7302..ad23a88a10 100644 --- a/src/api/wgl/mod.rs +++ b/src/api/wgl/mod.rs @@ -16,8 +16,7 @@ use self::make_current_guard::CurrentContextGuard; use std::ffi::{CStr, CString, OsStr}; use std::os::raw::{c_void, c_int}; use std::os::windows::ffi::OsStrExt; -use std::{mem, ptr}; -use std::io; +use std::{io, mem, ptr}; use winapi; use kernel32; @@ -198,6 +197,11 @@ impl Context { pub fn get_pixel_format(&self) -> PixelFormat { self.pixel_format.clone() } + + #[inline] + pub unsafe fn as_mut_ptr(&self) -> winapi::HDC { + self.hdc + } } unsafe impl Send for Context {} diff --git a/src/headless.rs b/src/headless.rs index fc842f67ba..63898b48be 100644 --- a/src/headless.rs +++ b/src/headless.rs @@ -9,6 +9,7 @@ use PixelFormat; use PixelFormatRequirements; use Robustness; +use os::GlContextExt; use platform; /// Object that allows you to build headless contexts. @@ -143,3 +144,11 @@ impl GlContext for HeadlessContext { } } +impl GlContextExt for HeadlessContext { + type Handle = ::Handle; + + #[inline] + unsafe fn as_mut_ptr(&self) -> Self::Handle { + self.context.as_mut_ptr() + } +} diff --git a/src/lib.rs b/src/lib.rs index fb2e19a819..8a34f04d9e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -367,6 +367,14 @@ impl GlContext for Context { } } +impl os::GlContextExt for Context { + type Handle = ::Handle; + + unsafe fn as_mut_ptr(&self) -> Self::Handle { + self.context.as_mut_ptr() + } +} + impl GlContext for GlWindow { unsafe fn make_current(&self) -> Result<(), ContextError> { self.context.make_current() diff --git a/src/os/android.rs b/src/os/android.rs index f579232645..f93c2251a5 100644 --- a/src/os/android.rs +++ b/src/os/android.rs @@ -1,3 +1,5 @@ #![cfg(any(target_os = "android"))] pub use winit::os::android::{WindowBuilderExt, WindowExt}; + +pub use api::egl::ffi::egl::types::EGLContext; diff --git a/src/os/macos.rs b/src/os/macos.rs index 81cd90f0b7..e4c3f29e2e 100644 --- a/src/os/macos.rs +++ b/src/os/macos.rs @@ -1,5 +1,6 @@ #![cfg(target_os = "macos")] +pub use cocoa::base::id; pub use winit::os::macos::ActivationPolicy; pub use winit::os::macos::MonitorIdExt; pub use winit::os::macos::WindowBuilderExt; diff --git a/src/os/mod.rs b/src/os/mod.rs index f769c1e991..115bc66217 100644 --- a/src/os/mod.rs +++ b/src/os/mod.rs @@ -1,12 +1,23 @@ //! Contains traits with platform-specific methods in them. //! -//! Contains the follow modules: +//! Contains the following modules: //! +//! - `android` //! - `macos` //! - `unix` //! - `windows` //! + pub mod android; pub mod macos; pub mod unix; pub mod windows; + +/// Platform-specific extensions for OpenGL contexts. +pub trait GlContextExt { + /// Raw context handle. + type Handle; + + /// Returns the raw context handle. + unsafe fn as_mut_ptr(&self) -> Self::Handle; +} diff --git a/src/os/unix.rs b/src/os/unix.rs index 344e2e05bc..f49d9a934c 100644 --- a/src/os/unix.rs +++ b/src/os/unix.rs @@ -5,3 +5,14 @@ pub use winit::os::unix::EventsLoopExt; pub use winit::os::unix::MonitorIdExt; pub use winit::os::unix::WindowBuilderExt; pub use winit::os::unix::WindowExt; + +pub use api::egl::ffi::egl::types::EGLContext; +pub use api::glx::ffi::GLXContext; +pub use api::osmesa::ffi::OSMesaContext; + +/// Context types available on Unix-like platforms. +#[derive(Clone, Debug)] +pub enum Context { + Glx(GLXContext), + Egl(EGLContext), +} diff --git a/src/os/windows.rs b/src/os/windows.rs index a8a04a7c1a..3198cfab75 100644 --- a/src/os/windows.rs +++ b/src/os/windows.rs @@ -1,3 +1,13 @@ #![cfg(target_os = "windows")] +pub use winapi::HDC; pub use winit::os::windows::{WindowBuilderExt, WindowExt, MonitorIdExt}; + +pub use api::egl::ffi::egl::types::EGLContext; + +/// Context types available on Windows. +#[derive(Clone, Debug)] +pub enum Context { + Egl(EGLContext), + Wgl(HDC), +} diff --git a/src/platform/android/mod.rs b/src/platform/android/mod.rs index 87cfd9a687..8617ca47f7 100644 --- a/src/platform/android/mod.rs +++ b/src/platform/android/mod.rs @@ -3,3 +3,4 @@ pub use winit::EventsLoop; pub use api::android::*; +pub use api::egl::ffi::egl::types::EGLContext; diff --git a/src/platform/emscripten/mod.rs b/src/platform/emscripten/mod.rs index d035d3156d..d53c7bbe29 100644 --- a/src/platform/emscripten/mod.rs +++ b/src/platform/emscripten/mod.rs @@ -1,7 +1,10 @@ #![cfg(target_os = "emscripten")] use {Api, ContextError, CreationError, GlAttributes, PixelFormat, PixelFormatRequirements}; +use os::GlContextExt; + use winit; + use std::ffi::CString; mod ffi; @@ -116,6 +119,15 @@ impl Context { } } +impl GlContextExt for Context { + type Handle = ffi::EMSCRIPTEN_WEBGL_CONTEXT_HANDLE; + + #[inline] + unsafe fn as_mut_ptr(&self) -> Self::Handle { + self.context + } +} + impl Drop for Context { fn drop(&mut self) { unsafe { @@ -180,6 +192,15 @@ impl HeadlessContext { } } +impl GlContextExt for HeadlessContext { + type Handle = ffi::EMSCRIPTEN_WEBGL_CONTEXT_HANDLE; + + #[inline] + unsafe fn as_mut_ptr(&self) -> Self::Handle { + self.context + } +} + fn error_to_str(code: ffi::EMSCRIPTEN_RESULT) -> &'static str { match code { ffi::EMSCRIPTEN_RESULT_SUCCESS | ffi::EMSCRIPTEN_RESULT_DEFERRED diff --git a/src/platform/ios/mod.rs b/src/platform/ios/mod.rs index 0b1a550930..f99b5510fc 100644 --- a/src/platform/ios/mod.rs +++ b/src/platform/ios/mod.rs @@ -1,12 +1,24 @@ #![cfg(target_os = "ios")] +pub use api::ios::*; + +pub use cocoa::base::id; + use GlAttributes; use CreationError; use PixelFormat; use PixelFormatRequirements; use ContextError; +use os::GlContextExt; -pub use api::ios::*; +impl GlContextExt for Context { + type Handle = id; + + #[inline] + unsafe fn as_mut_ptr(&self) -> Self::Handle { + *self.eagl_context.deref() + } +} #[derive(Clone, Default)] pub struct PlatformSpecificHeadlessBuilderAttributes; @@ -46,9 +58,17 @@ impl HeadlessContext { } pub fn get_pixel_format(&self) -> PixelFormat { - unimplemented!(); + unimplemented!() } } unsafe impl Send for HeadlessContext {} unsafe impl Sync for HeadlessContext {} + +impl GlContextExt for HeadlessContext { + type Handle = i32; + + unsafe fn as_mut_ptr(&self) -> Self::Handle { + unimplemented!() + } +} diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index 308d057926..bacc7a3e12 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -2,8 +2,9 @@ use {Api, ContextError, CreationError, GlAttributes, PixelFormat, PixelFormatRequirements}; -use api::osmesa::OsMesaContext; -use wayland_client; +use api::osmesa::{self, OsMesaContext}; +use os::GlContextExt; +use os::unix::Context as OsContext; use winit; use winit::os::unix::EventsLoopExt; @@ -35,8 +36,7 @@ impl Context { }); wayland::Context::new(window_builder, events_loop, pf_reqs, &gl_attr) .map(|(window, context)| (window, Context::Wayland(context))) - } - else { + } else { if let Some(&Context::Wayland(_)) = gl_attr.sharing { let msg = "Cannot share a X11 context with an wayland context"; return Err(CreationError::PlatformSpecific(msg.into())); @@ -106,6 +106,18 @@ impl Context { } } +impl GlContextExt for Context { + type Handle = OsContext; + + #[inline] + unsafe fn as_mut_ptr(&self) -> Self::Handle { + match *self { + Context::X(ref ctxt) => ctxt.as_mut_ptr(), + Context::Wayland(ref ctxt) => OsContext::Egl(ctxt.as_mut_ptr()) + } + } +} + #[derive(Clone, Default)] pub struct PlatformSpecificHeadlessBuilderAttributes; @@ -158,3 +170,11 @@ impl HeadlessContext { self.0.get_pixel_format() } } + +impl GlContextExt for HeadlessContext { + type Handle = osmesa::ffi::OSMesaContext; + + unsafe fn as_mut_ptr(&self) -> Self::Handle { + self.0.as_mut_ptr() + } +} diff --git a/src/platform/linux/wayland.rs b/src/platform/linux/wayland.rs index e9a31e83c9..faad6ae8dc 100644 --- a/src/platform/linux/wayland.rs +++ b/src/platform/linux/wayland.rs @@ -4,8 +4,8 @@ use winit; use winit::os::unix::WindowExt; use {ContextError, CreationError, GlAttributes, PixelFormat, PixelFormatRequirements}; use api::dlopen; -use api::egl; -use api::egl::Context as EglContext; +use api::egl::{self, Context as EglContext}; +use api::egl::ffi; use wayland_client::egl as wegl; pub struct Context { @@ -79,4 +79,9 @@ impl Context { pub fn get_pixel_format(&self) -> PixelFormat { self.context.get_pixel_format().clone() } + + #[inline] + pub unsafe fn as_mut_ptr(&self) -> ffi::egl::types::EGLContext { + self.context.as_mut_ptr() + } } diff --git a/src/platform/linux/x11.rs b/src/platform/linux/x11.rs index 0b7060e431..1a93aa3de6 100644 --- a/src/platform/linux/x11.rs +++ b/src/platform/linux/x11.rs @@ -15,6 +15,7 @@ use api::{dlopen, egl}; use api::egl::Context as EglContext; use api::glx::ffi::glx::Glx; use api::egl::ffi::egl::Egl; +use os::unix::Context as OsContext; #[derive(Debug)] struct NoX11Connection; @@ -296,4 +297,13 @@ impl Context { GlContext::None => panic!() } } + + #[inline] + pub unsafe fn as_mut_ptr(&self) -> OsContext { + match self.context { + GlContext::Glx(ref ctxt) => OsContext::Glx(ctxt.as_mut_ptr()), + GlContext::Egl(ref ctxt) => OsContext::Egl(ctxt.as_mut_ptr()), + GlContext::None => panic!() + } + } } diff --git a/src/platform/macos/headless.rs b/src/platform/macos/headless.rs index 9d7d744082..de6135fd42 100644 --- a/src/platform/macos/headless.rs +++ b/src/platform/macos/headless.rs @@ -12,6 +12,8 @@ use cocoa::appkit::*; use PixelFormat; use super::helpers; +use os::GlContextExt; + #[derive(Clone, Default)] pub struct PlatformSpecificHeadlessBuilderAttributes; @@ -89,3 +91,11 @@ impl HeadlessContext { unsafe impl Send for HeadlessContext {} unsafe impl Sync for HeadlessContext {} + +impl GlContextExt for HeadlessContext { + type Handle = id; + + unsafe fn as_mut_ptr(&self) -> Self::Handle { + self.context + } +} diff --git a/src/platform/macos/mod.rs b/src/platform/macos/mod.rs old mode 100755 new mode 100644 index 5ae21c8bf6..2ee90b41d5 --- a/src/platform/macos/mod.rs +++ b/src/platform/macos/mod.rs @@ -1,11 +1,16 @@ #![cfg(target_os = "macos")] +pub use self::headless::HeadlessContext; +pub use self::headless::PlatformSpecificHeadlessBuilderAttributes; +pub use winit::{MonitorId}; + use CreationError; use ContextError; use GlAttributes; use PixelFormat; use PixelFormatRequirements; use Robustness; +use os::GlContextExt; use objc::runtime::{BOOL, NO}; @@ -24,9 +29,6 @@ use std::ops::Deref; use winit; use winit::os::macos::WindowExt; -pub use winit::{MonitorId}; -pub use self::headless::HeadlessContext; -pub use self::headless::PlatformSpecificHeadlessBuilderAttributes; mod headless; mod helpers; @@ -184,6 +186,15 @@ impl Context { } } +impl GlContextExt for Context { + type Handle = id; + + #[inline] + unsafe fn as_mut_ptr(&self) -> Self::Handle { + *self.gl.deref() + } +} + struct IdRef(id); impl IdRef { diff --git a/src/platform/windows/context.rs b/src/platform/windows/context.rs index 3e5d616228..d8fbdec948 100644 --- a/src/platform/windows/context.rs +++ b/src/platform/windows/context.rs @@ -18,6 +18,7 @@ use api::wgl::Context as WglContext; use api::egl::Context as EglContext; use api::egl::ffi::egl::Egl; use api::egl; +use os::windows::Context as OsContext; unsafe impl Send for Context {} unsafe impl Sync for Context {} @@ -27,9 +28,7 @@ pub enum Context { Wgl(WglContext), } - impl Context { - /// See the docs in the crate root file. pub fn new( window_builder: winit::WindowBuilder, @@ -126,4 +125,12 @@ impl Context { Context::Egl(ref c) => c.get_pixel_format(), } } + + #[inline] + pub unsafe fn as_mut_ptr(&self) -> OsContext { + match *self { + Context::Wgl(ref c) => OsContext::Wgl(c.as_mut_ptr()), + Context::Egl(ref c) => OsContext::Egl(c.as_mut_ptr()), + } + } } diff --git a/src/platform/windows/mod.rs b/src/platform/windows/mod.rs index 44bb114d40..1ce289951f 100644 --- a/src/platform/windows/mod.rs +++ b/src/platform/windows/mod.rs @@ -12,6 +12,8 @@ use winit; use api::egl::ffi::egl::Egl; use api::egl; use api::egl::Context as EglContext; +use os::GlContextExt; +use os::windows::Context as OsContext; use std::ffi::CString; use std::ops::{Deref, DerefMut}; @@ -92,6 +94,14 @@ impl DerefMut for Context { } } +impl GlContextExt for Context { + type Handle = OsContext; + + unsafe fn as_mut_ptr(&self) -> Self::Handle { + self.0.as_mut_ptr() + } +} + /// pub enum HeadlessContext { /// A regular window, but invisible. @@ -176,3 +186,14 @@ impl HeadlessContext { } } } + +impl GlContextExt for HeadlessContext { + type Handle = OsContext; + + unsafe fn as_mut_ptr(&self) -> Self::Handle { + match *self { + HeadlessContext::HiddenWindow(_, _, ref ctxt) => ctxt.as_mut_ptr(), + HeadlessContext::EglPbuffer(ref ctxt) => OsContext::Egl(ctxt.as_mut_ptr()), + } + } +} From e36f67817d3ee93cec65928033521120a7131e81 Mon Sep 17 00:00:00 2001 From: Eyal Kalderon Date: Wed, 4 Oct 2017 13:34:41 -0400 Subject: [PATCH 2/5] Rename all methods to raw_handle(), move trait impls to os submodules --- src/api/android/mod.rs | 17 +++--------- src/api/egl/ffi.rs | 6 +++-- src/api/egl/mod.rs | 2 +- src/api/glx/mod.rs | 2 +- src/api/osmesa/mod.rs | 2 +- src/api/wgl/mod.rs | 2 +- src/headless.rs | 12 +-------- src/lib.rs | 8 ------ src/os/android.rs | 23 ++++++++++++++++- src/os/macos.rs | 21 +++++++++++++++ src/os/mod.rs | 2 +- src/os/unix.rs | 27 ++++++++++++++----- src/os/windows.rs | 27 ++++++++++++++----- src/platform/android/mod.rs | 1 - src/platform/emscripten/mod.rs | 17 +++--------- src/platform/linux/mod.rs | 41 ++++++++++++++++------------- src/platform/linux/wayland.rs | 7 +++-- src/platform/linux/x11.rs | 11 +++----- src/platform/macos/headless.rs | 15 ++++------- src/platform/macos/mod.rs | 7 +---- src/platform/windows/context.rs | 11 ++++---- src/platform/windows/mod.rs | 46 ++++++++++++++------------------- 22 files changed, 163 insertions(+), 144 deletions(-) diff --git a/src/api/android/mod.rs b/src/api/android/mod.rs index b67dd31b14..829887bfce 100644 --- a/src/api/android/mod.rs +++ b/src/api/android/mod.rs @@ -16,7 +16,6 @@ use PixelFormatRequirements; use api::egl; use api::egl::Context as EglContext; -use os::GlContextExt; mod ffi; @@ -79,14 +78,10 @@ impl Context { pub fn get_pixel_format(&self) -> PixelFormat { self.egl_context.get_pixel_format() } -} - -impl GlContextExt for Context { - type Handle = egl::ffi::egl::types::EGLContext; #[inline] - unsafe fn as_mut_ptr(&self) -> Self::Handle { - self.egl_context.as_mut_ptr() + pub unsafe fn raw_handle(&self) -> egl::ffi::EGLContext { + self.egl_context.raw_handle() } } @@ -145,13 +140,9 @@ impl HeadlessContext { pub fn get_pixel_format(&self) -> PixelFormat { self.0.get_pixel_format() } -} - -impl GlContextExt for HeadlessContext { - type Handle = egl::ffi::egl::types::EGLContext; #[inline] - unsafe fn as_mut_ptr(&self) -> Self::Handle { - self.0.as_mut_ptr() + pub unsafe fn raw_handle(&self) -> egl::ffi::EGLContext { + self.0.raw_handle() } } diff --git a/src/api/egl/ffi.rs b/src/api/egl/ffi.rs index 9ed32c82d3..43d5f3adbe 100644 --- a/src/api/egl/ffi.rs +++ b/src/api/egl/ffi.rs @@ -1,10 +1,12 @@ #![allow(non_camel_case_types)] -use libc; - #[cfg(target_os = "windows")] extern crate winapi; +pub use self::egl::types::EGLContext; + +use libc; + pub mod egl { pub type khronos_utime_nanoseconds_t = super::khronos_utime_nanoseconds_t; pub type khronos_uint64_t = super::khronos_uint64_t; diff --git a/src/api/egl/mod.rs b/src/api/egl/mod.rs index 535dfd411d..596fa3546b 100644 --- a/src/api/egl/mod.rs +++ b/src/api/egl/mod.rs @@ -322,7 +322,7 @@ impl Context { } #[inline] - pub unsafe fn as_mut_ptr(&self) -> ffi::egl::types::EGLContext { + pub unsafe fn raw_handle(&self) -> ffi::egl::types::EGLContext { self.context } } diff --git a/src/api/glx/mod.rs b/src/api/glx/mod.rs index 59c2e1937d..05871255dd 100644 --- a/src/api/glx/mod.rs +++ b/src/api/glx/mod.rs @@ -145,7 +145,7 @@ impl Context { } #[inline] - pub unsafe fn as_mut_ptr(&self) -> ffi::GLXContext { + pub unsafe fn raw_handle(&self) -> ffi::GLXContext { self.context } } diff --git a/src/api/osmesa/mod.rs b/src/api/osmesa/mod.rs index 0e2f8d989a..e8832248f8 100644 --- a/src/api/osmesa/mod.rs +++ b/src/api/osmesa/mod.rs @@ -192,7 +192,7 @@ impl OsMesaContext { } #[inline] - pub unsafe fn as_mut_ptr(&self) -> osmesa_sys::OSMesaContext { + pub unsafe fn raw_handle(&self) -> osmesa_sys::OSMesaContext { self.context } } diff --git a/src/api/wgl/mod.rs b/src/api/wgl/mod.rs index ad23a88a10..dd8bd459ed 100644 --- a/src/api/wgl/mod.rs +++ b/src/api/wgl/mod.rs @@ -199,7 +199,7 @@ impl Context { } #[inline] - pub unsafe fn as_mut_ptr(&self) -> winapi::HDC { + pub unsafe fn raw_handle(&self) -> winapi::HDC { self.hdc } } diff --git a/src/headless.rs b/src/headless.rs index 63898b48be..a1e24a10a4 100644 --- a/src/headless.rs +++ b/src/headless.rs @@ -9,7 +9,6 @@ use PixelFormat; use PixelFormatRequirements; use Robustness; -use os::GlContextExt; use platform; /// Object that allows you to build headless contexts. @@ -94,7 +93,7 @@ impl<'a> HeadlessRendererBuilder<'a> { /// Represents a headless OpenGL context. pub struct HeadlessContext { - context: platform::HeadlessContext, + pub(crate) context: platform::HeadlessContext, } impl GlContext for HeadlessContext { @@ -143,12 +142,3 @@ impl GlContext for HeadlessContext { unimplemented!() } } - -impl GlContextExt for HeadlessContext { - type Handle = ::Handle; - - #[inline] - unsafe fn as_mut_ptr(&self) -> Self::Handle { - self.context.as_mut_ptr() - } -} diff --git a/src/lib.rs b/src/lib.rs index 8a34f04d9e..fb2e19a819 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -367,14 +367,6 @@ impl GlContext for Context { } } -impl os::GlContextExt for Context { - type Handle = ::Handle; - - unsafe fn as_mut_ptr(&self) -> Self::Handle { - self.context.as_mut_ptr() - } -} - impl GlContext for GlWindow { unsafe fn make_current(&self) -> Result<(), ContextError> { self.context.make_current() diff --git a/src/os/android.rs b/src/os/android.rs index f93c2251a5..f834720dd0 100644 --- a/src/os/android.rs +++ b/src/os/android.rs @@ -2,4 +2,25 @@ pub use winit::os::android::{WindowBuilderExt, WindowExt}; -pub use api::egl::ffi::egl::types::EGLContext; +pub use api::egl::ffi::EGLContext; + +use {Context, HeadlessContext}; +use os::GlContextExt; + +impl GlContextExt for Context { + type Handle = EGLContext; + + #[inline] + unsafe fn raw_handle(&self) -> Self::Handle { + self.context.raw_handle() + } +} + +impl GlContextExt for HeadlessContext { + type Handle = EGLContext; + + #[inline] + unsafe fn raw_handle(&self) -> Self::Handle { + self.context.raw_handle() + } +} diff --git a/src/os/macos.rs b/src/os/macos.rs index e4c3f29e2e..bc951fa9e6 100644 --- a/src/os/macos.rs +++ b/src/os/macos.rs @@ -5,3 +5,24 @@ pub use winit::os::macos::ActivationPolicy; pub use winit::os::macos::MonitorIdExt; pub use winit::os::macos::WindowBuilderExt; pub use winit::os::macos::WindowExt; + +use {Context, HeadlessContext}; +use os::GlContextExt; + +impl GlContextExt for Context { + type Handle = id; + + #[inline] + unsafe fn raw_handle(&self) -> Self::Handle { + self.context.raw_handle() + } +} + +impl GlContextExt for HeadlessContext { + type Handle = id; + + #[inline] + unsafe fn raw_handle(&self) -> Self::Handle { + self.context.raw_handle() + } +} diff --git a/src/os/mod.rs b/src/os/mod.rs index 115bc66217..755f2a1c84 100644 --- a/src/os/mod.rs +++ b/src/os/mod.rs @@ -19,5 +19,5 @@ pub trait GlContextExt { type Handle; /// Returns the raw context handle. - unsafe fn as_mut_ptr(&self) -> Self::Handle; + unsafe fn raw_handle(&self) -> Self::Handle; } diff --git a/src/os/unix.rs b/src/os/unix.rs index f49d9a934c..cd38c1f110 100644 --- a/src/os/unix.rs +++ b/src/os/unix.rs @@ -6,13 +6,28 @@ pub use winit::os::unix::MonitorIdExt; pub use winit::os::unix::WindowBuilderExt; pub use winit::os::unix::WindowExt; -pub use api::egl::ffi::egl::types::EGLContext; +pub use api::egl::ffi::EGLContext; pub use api::glx::ffi::GLXContext; pub use api::osmesa::ffi::OSMesaContext; +pub use platform::RawHandle; -/// Context types available on Unix-like platforms. -#[derive(Clone, Debug)] -pub enum Context { - Glx(GLXContext), - Egl(EGLContext), +use {Context, HeadlessContext}; +use os::GlContextExt; + +impl GlContextExt for Context { + type Handle = RawHandle; + + #[inline] + unsafe fn raw_handle(&self) -> Self::Handle { + self.context.raw_handle() + } +} + +impl GlContextExt for HeadlessContext { + type Handle = OSMesaContext; + + #[inline] + unsafe fn raw_handle(&self) -> Self::Handle { + self.context.raw_handle() + } } diff --git a/src/os/windows.rs b/src/os/windows.rs index 3198cfab75..254c328b2e 100644 --- a/src/os/windows.rs +++ b/src/os/windows.rs @@ -3,11 +3,26 @@ pub use winapi::HDC; pub use winit::os::windows::{WindowBuilderExt, WindowExt, MonitorIdExt}; -pub use api::egl::ffi::egl::types::EGLContext; +pub use api::egl::ffi::EGLContext; +pub use platform::RawHandle; -/// Context types available on Windows. -#[derive(Clone, Debug)] -pub enum Context { - Egl(EGLContext), - Wgl(HDC), +use {Context, HeadlessContext}; +use os::GlContextExt; + +impl GlContextExt for Context { + type Handle = RawHandle; + + #[inline] + unsafe fn raw_handle(&self) -> Self::Handle { + self.context.raw_handle() + } +} + +impl GlContextExt for HeadlessContext { + type Handle = RawHandle; + + #[inline] + unsafe fn raw_handle(&self) -> Self::Handle { + self.context.raw_handle() + } } diff --git a/src/platform/android/mod.rs b/src/platform/android/mod.rs index 8617ca47f7..87cfd9a687 100644 --- a/src/platform/android/mod.rs +++ b/src/platform/android/mod.rs @@ -3,4 +3,3 @@ pub use winit::EventsLoop; pub use api::android::*; -pub use api::egl::ffi::egl::types::EGLContext; diff --git a/src/platform/emscripten/mod.rs b/src/platform/emscripten/mod.rs index d53c7bbe29..830e776194 100644 --- a/src/platform/emscripten/mod.rs +++ b/src/platform/emscripten/mod.rs @@ -1,12 +1,11 @@ #![cfg(target_os = "emscripten")] +use std::ffi::CString; + use {Api, ContextError, CreationError, GlAttributes, PixelFormat, PixelFormatRequirements}; -use os::GlContextExt; use winit; -use std::ffi::CString; - mod ffi; pub struct Context { @@ -117,13 +116,9 @@ impl Context { srgb: true, } } -} - -impl GlContextExt for Context { - type Handle = ffi::EMSCRIPTEN_WEBGL_CONTEXT_HANDLE; #[inline] - unsafe fn as_mut_ptr(&self) -> Self::Handle { + pub unsafe fn raw_handle(&self) -> ffi::EMSCRIPTEN_WEBGL_CONTEXT_HANDLE { self.context } } @@ -190,13 +185,9 @@ impl HeadlessContext { pub fn get_pixel_format(&self) -> PixelFormat { unimplemented!() } -} - -impl GlContextExt for HeadlessContext { - type Handle = ffi::EMSCRIPTEN_WEBGL_CONTEXT_HANDLE; #[inline] - unsafe fn as_mut_ptr(&self) -> Self::Handle { + pub unsafe fn raw_handle(&self) -> ffi::EMSCRIPTEN_WEBGL_CONTEXT_HANDLE { self.context } } diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index bacc7a3e12..e6a03ab647 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -1,16 +1,24 @@ #![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))] -use {Api, ContextError, CreationError, GlAttributes, PixelFormat, PixelFormatRequirements}; - -use api::osmesa::{self, OsMesaContext}; -use os::GlContextExt; -use os::unix::Context as OsContext; use winit; use winit::os::unix::EventsLoopExt; +use {Api, ContextError, CreationError, GlAttributes, PixelFormat, PixelFormatRequirements}; +use api::egl; +use api::glx; +use api::osmesa::{self, OsMesaContext}; +use self::x11::GlContext; + mod wayland; mod x11; +/// Context handles available on Unix-like platforms. +#[derive(Clone, Debug)] +pub enum RawHandle { + Glx(glx::ffi::GLXContext), + Egl(egl::ffi::EGLContext), +} + pub enum Context { X(x11::Context), Wayland(wayland::Context) @@ -104,16 +112,16 @@ impl Context { Context::Wayland(ref ctxt) => ctxt.get_pixel_format() } } -} - -impl GlContextExt for Context { - type Handle = OsContext; #[inline] - unsafe fn as_mut_ptr(&self) -> Self::Handle { + pub unsafe fn raw_handle(&self) -> RawHandle { match *self { - Context::X(ref ctxt) => ctxt.as_mut_ptr(), - Context::Wayland(ref ctxt) => OsContext::Egl(ctxt.as_mut_ptr()) + Context::X(ref ctxt) => match *ctxt.raw_handle() { + GlContext::Glx(ref ctxt) => RawHandle::Glx(ctxt.raw_handle()), + GlContext::Egl(ref ctxt) => RawHandle::Egl(ctxt.raw_handle()), + GlContext::None => panic!() + } + Context::Wayland(ref ctxt) => RawHandle::Egl(ctxt.raw_handle()) } } } @@ -169,12 +177,9 @@ impl HeadlessContext { pub fn get_pixel_format(&self) -> PixelFormat { self.0.get_pixel_format() } -} - -impl GlContextExt for HeadlessContext { - type Handle = osmesa::ffi::OSMesaContext; - unsafe fn as_mut_ptr(&self) -> Self::Handle { - self.0.as_mut_ptr() + #[inline] + pub unsafe fn raw_handle(&self) -> osmesa::ffi::OSMesaContext { + self.0.raw_handle() } } diff --git a/src/platform/linux/wayland.rs b/src/platform/linux/wayland.rs index faad6ae8dc..68ee7383d3 100644 --- a/src/platform/linux/wayland.rs +++ b/src/platform/linux/wayland.rs @@ -4,8 +4,7 @@ use winit; use winit::os::unix::WindowExt; use {ContextError, CreationError, GlAttributes, PixelFormat, PixelFormatRequirements}; use api::dlopen; -use api::egl::{self, Context as EglContext}; -use api::egl::ffi; +use api::egl::{self, ffi, Context as EglContext}; use wayland_client::egl as wegl; pub struct Context { @@ -81,7 +80,7 @@ impl Context { } #[inline] - pub unsafe fn as_mut_ptr(&self) -> ffi::egl::types::EGLContext { - self.context.as_mut_ptr() + pub unsafe fn raw_handle(&self) -> ffi::EGLContext { + self.context.raw_handle() } } diff --git a/src/platform/linux/x11.rs b/src/platform/linux/x11.rs index 1a93aa3de6..3cd4ff0f12 100644 --- a/src/platform/linux/x11.rs +++ b/src/platform/linux/x11.rs @@ -15,7 +15,6 @@ use api::{dlopen, egl}; use api::egl::Context as EglContext; use api::glx::ffi::glx::Glx; use api::egl::ffi::egl::Egl; -use os::unix::Context as OsContext; #[derive(Debug)] struct NoX11Connection; @@ -84,7 +83,7 @@ impl GlxOrEgl { } } -enum GlContext { +pub enum GlContext { Glx(GlxContext), Egl(EglContext), None, @@ -299,11 +298,7 @@ impl Context { } #[inline] - pub unsafe fn as_mut_ptr(&self) -> OsContext { - match self.context { - GlContext::Glx(ref ctxt) => OsContext::Glx(ctxt.as_mut_ptr()), - GlContext::Egl(ref ctxt) => OsContext::Egl(ctxt.as_mut_ptr()), - GlContext::None => panic!() - } + pub unsafe fn raw_handle(&self) -> &GlContext { + &self.context } } diff --git a/src/platform/macos/headless.rs b/src/platform/macos/headless.rs index de6135fd42..9481e92b08 100644 --- a/src/platform/macos/headless.rs +++ b/src/platform/macos/headless.rs @@ -12,8 +12,6 @@ use cocoa::appkit::*; use PixelFormat; use super::helpers; -use os::GlContextExt; - #[derive(Clone, Default)] pub struct PlatformSpecificHeadlessBuilderAttributes; @@ -87,15 +85,12 @@ impl HeadlessContext { pub fn get_pixel_format(&self) -> PixelFormat { unimplemented!(); } -} - -unsafe impl Send for HeadlessContext {} -unsafe impl Sync for HeadlessContext {} -impl GlContextExt for HeadlessContext { - type Handle = id; - - unsafe fn as_mut_ptr(&self) -> Self::Handle { + #[inline] + pub unsafe fn raw_handle(&self) -> id { self.context } } + +unsafe impl Send for HeadlessContext {} +unsafe impl Sync for HeadlessContext {} diff --git a/src/platform/macos/mod.rs b/src/platform/macos/mod.rs index 2ee90b41d5..f3e0be911c 100644 --- a/src/platform/macos/mod.rs +++ b/src/platform/macos/mod.rs @@ -10,7 +10,6 @@ use GlAttributes; use PixelFormat; use PixelFormatRequirements; use Robustness; -use os::GlContextExt; use objc::runtime::{BOOL, NO}; @@ -184,13 +183,9 @@ impl Context { pub fn get_pixel_format(&self) -> PixelFormat { self.pixel_format.clone() } -} - -impl GlContextExt for Context { - type Handle = id; #[inline] - unsafe fn as_mut_ptr(&self) -> Self::Handle { + pub unsafe fn raw_handle(&self) -> id { *self.gl.deref() } } diff --git a/src/platform/windows/context.rs b/src/platform/windows/context.rs index d8fbdec948..b8621ded0e 100644 --- a/src/platform/windows/context.rs +++ b/src/platform/windows/context.rs @@ -2,6 +2,7 @@ use std::ptr; +use winapi; use winit; use ContextError; @@ -12,13 +13,11 @@ use Api; use PixelFormat; use PixelFormatRequirements; -use winapi; - use api::wgl::Context as WglContext; use api::egl::Context as EglContext; use api::egl::ffi::egl::Egl; use api::egl; -use os::windows::Context as OsContext; +use platform::RawHandle; unsafe impl Send for Context {} unsafe impl Sync for Context {} @@ -127,10 +126,10 @@ impl Context { } #[inline] - pub unsafe fn as_mut_ptr(&self) -> OsContext { + pub unsafe fn raw_handle(&self) -> RawHandle { match *self { - Context::Wgl(ref c) => OsContext::Wgl(c.as_mut_ptr()), - Context::Egl(ref c) => OsContext::Egl(c.as_mut_ptr()), + Context::Wgl(ref c) => RawHandle::Wgl(c.raw_handle()), + Context::Egl(ref c) => RawHandle::Egl(c.raw_handle()), } } } diff --git a/src/platform/windows/mod.rs b/src/platform/windows/mod.rs index 1ce289951f..ae3132eb08 100644 --- a/src/platform/windows/mod.rs +++ b/src/platform/windows/mod.rs @@ -1,5 +1,12 @@ #![cfg(target_os = "windows")] +use std::ffi::CString; +use std::ops::{Deref, DerefMut}; + +use kernel32; +use winapi; +use winit; + use Api; use ContextError; use CreationError; @@ -7,20 +14,19 @@ use PixelFormat; use PixelFormatRequirements; use GlAttributes; -use winit; - use api::egl::ffi::egl::Egl; use api::egl; use api::egl::Context as EglContext; -use os::GlContextExt; -use os::windows::Context as OsContext; - -use std::ffi::CString; -use std::ops::{Deref, DerefMut}; -use kernel32; mod context; +/// Context handles available on Windows. +#[derive(Clone, Debug)] +pub enum RawHandle { + Egl(egl::ffi::EGLContext), + Wgl(winapi::HDC), +} + /// Stupid wrapper because `*const libc::c_void` doesn't implement `Sync`. struct EglWrapper(Egl); unsafe impl Sync for EglWrapper {} @@ -53,9 +59,6 @@ lazy_static! { }; } -#[derive(Clone, Default)] -pub struct PlatformSpecificHeadlessBuilderAttributes; - /// The Win32 implementation of the main `Context` object. pub struct Context(context::Context); @@ -94,15 +97,9 @@ impl DerefMut for Context { } } -impl GlContextExt for Context { - type Handle = OsContext; - - unsafe fn as_mut_ptr(&self) -> Self::Handle { - self.0.as_mut_ptr() - } -} +#[derive(Clone, Default)] +pub struct PlatformSpecificHeadlessBuilderAttributes; -/// pub enum HeadlessContext { /// A regular window, but invisible. HiddenWindow(winit::EventsLoop, winit::Window, context::Context), @@ -185,15 +182,12 @@ impl HeadlessContext { &HeadlessContext::EglPbuffer(ref ctxt) => ctxt.get_pixel_format(), } } -} - -impl GlContextExt for HeadlessContext { - type Handle = OsContext; - unsafe fn as_mut_ptr(&self) -> Self::Handle { + #[inline] + pub unsafe fn raw_handle(&self) -> RawHandle { match *self { - HeadlessContext::HiddenWindow(_, _, ref ctxt) => ctxt.as_mut_ptr(), - HeadlessContext::EglPbuffer(ref ctxt) => OsContext::Egl(ctxt.as_mut_ptr()), + HeadlessContext::HiddenWindow(_, _, ref ctxt) => ctxt.raw_handle(), + HeadlessContext::EglPbuffer(ref ctxt) => RawHandle::Egl(ctxt.raw_handle()), } } } From d2bc54fee9375a1609090973f9a45c90518771f7 Mon Sep 17 00:00:00 2001 From: Eyal Kalderon Date: Wed, 4 Oct 2017 13:39:59 -0400 Subject: [PATCH 3/5] Add missing comma in match statement --- src/platform/linux/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index e6a03ab647..049044f79c 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -120,7 +120,7 @@ impl Context { GlContext::Glx(ref ctxt) => RawHandle::Glx(ctxt.raw_handle()), GlContext::Egl(ref ctxt) => RawHandle::Egl(ctxt.raw_handle()), GlContext::None => panic!() - } + }, Context::Wayland(ref ctxt) => RawHandle::Egl(ctxt.raw_handle()) } } From f62bd0b10fa584077eb57a80b4dae0d47afdee23 Mon Sep 17 00:00:00 2001 From: Eyal Kalderon Date: Wed, 4 Oct 2017 14:27:44 -0400 Subject: [PATCH 4/5] Return an HGLRC instead of HDC for WGL contexts --- src/api/wgl/mod.rs | 5 ----- src/os/windows.rs | 2 +- src/platform/windows/context.rs | 2 +- src/platform/windows/mod.rs | 2 +- 4 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/api/wgl/mod.rs b/src/api/wgl/mod.rs index dd8bd459ed..d25c9667c3 100644 --- a/src/api/wgl/mod.rs +++ b/src/api/wgl/mod.rs @@ -197,11 +197,6 @@ impl Context { pub fn get_pixel_format(&self) -> PixelFormat { self.pixel_format.clone() } - - #[inline] - pub unsafe fn raw_handle(&self) -> winapi::HDC { - self.hdc - } } unsafe impl Send for Context {} diff --git a/src/os/windows.rs b/src/os/windows.rs index 254c328b2e..a71876bef3 100644 --- a/src/os/windows.rs +++ b/src/os/windows.rs @@ -1,6 +1,6 @@ #![cfg(target_os = "windows")] -pub use winapi::HDC; +pub use winapi::HGLRC; pub use winit::os::windows::{WindowBuilderExt, WindowExt, MonitorIdExt}; pub use api::egl::ffi::EGLContext; diff --git a/src/platform/windows/context.rs b/src/platform/windows/context.rs index b8621ded0e..f8f5eb2620 100644 --- a/src/platform/windows/context.rs +++ b/src/platform/windows/context.rs @@ -128,7 +128,7 @@ impl Context { #[inline] pub unsafe fn raw_handle(&self) -> RawHandle { match *self { - Context::Wgl(ref c) => RawHandle::Wgl(c.raw_handle()), + Context::Wgl(ref c) => RawHandle::Wgl(c.get_hglrc()), Context::Egl(ref c) => RawHandle::Egl(c.raw_handle()), } } diff --git a/src/platform/windows/mod.rs b/src/platform/windows/mod.rs index ae3132eb08..528dac2e2d 100644 --- a/src/platform/windows/mod.rs +++ b/src/platform/windows/mod.rs @@ -24,7 +24,7 @@ mod context; #[derive(Clone, Debug)] pub enum RawHandle { Egl(egl::ffi::EGLContext), - Wgl(winapi::HDC), + Wgl(winapi::HGLRC), } /// Stupid wrapper because `*const libc::c_void` doesn't implement `Sync`. From 6b196fba1c6f197eb9def4f5dfe8d8027c7808c1 Mon Sep 17 00:00:00 2001 From: Eyal Kalderon Date: Wed, 4 Oct 2017 14:50:05 -0400 Subject: [PATCH 5/5] Cast certain handles as void pointer, minor reformatting --- src/api/osmesa/mod.rs | 15 ++++++++------- src/os/macos.rs | 7 ++++--- src/os/unix.rs | 13 +++++++------ src/platform/linux/mod.rs | 12 +++++++----- src/platform/macos/headless.rs | 9 +++++---- src/platform/macos/mod.rs | 19 ++++++++----------- 6 files changed, 39 insertions(+), 36 deletions(-) diff --git a/src/api/osmesa/mod.rs b/src/api/osmesa/mod.rs index e8832248f8..7547c43345 100644 --- a/src/api/osmesa/mod.rs +++ b/src/api/osmesa/mod.rs @@ -2,11 +2,6 @@ extern crate osmesa_sys; -use std::error::Error; -use std::ffi::CString; -use std::fmt::{Debug, Display, Error as FormatError, Formatter}; -use std::{mem, ptr}; - use Api; use ContextError; use CreationError; @@ -18,6 +13,12 @@ use PixelFormatRequirements; use Robustness; use libc; +use std::error::Error; +use std::ffi::CString; +use std::fmt::{Debug, Display, Error as FormatError, Formatter}; +use std::{mem, ptr}; +use std::os::raw::c_void; + pub mod ffi { pub use super::osmesa_sys::OSMesaContext; } @@ -192,8 +193,8 @@ impl OsMesaContext { } #[inline] - pub unsafe fn raw_handle(&self) -> osmesa_sys::OSMesaContext { - self.context + pub unsafe fn raw_handle(&self) -> *mut c_void { + self.context as *mut _ } } diff --git a/src/os/macos.rs b/src/os/macos.rs index bc951fa9e6..65099866a6 100644 --- a/src/os/macos.rs +++ b/src/os/macos.rs @@ -1,6 +1,5 @@ #![cfg(target_os = "macos")] -pub use cocoa::base::id; pub use winit::os::macos::ActivationPolicy; pub use winit::os::macos::MonitorIdExt; pub use winit::os::macos::WindowBuilderExt; @@ -9,8 +8,10 @@ pub use winit::os::macos::WindowExt; use {Context, HeadlessContext}; use os::GlContextExt; +use std::os::raw::c_void; + impl GlContextExt for Context { - type Handle = id; + type Handle = *mut c_void; #[inline] unsafe fn raw_handle(&self) -> Self::Handle { @@ -19,7 +20,7 @@ impl GlContextExt for Context { } impl GlContextExt for HeadlessContext { - type Handle = id; + type Handle = *mut c_void; #[inline] unsafe fn raw_handle(&self) -> Self::Handle { diff --git a/src/os/unix.rs b/src/os/unix.rs index cd38c1f110..ac2712cb6e 100644 --- a/src/os/unix.rs +++ b/src/os/unix.rs @@ -1,19 +1,20 @@ #![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))] +pub use api::egl::ffi::EGLContext; +pub use api::glx::ffi::GLXContext; +pub use platform::RawHandle; + pub use winit::os::unix::XNotSupported; pub use winit::os::unix::EventsLoopExt; pub use winit::os::unix::MonitorIdExt; pub use winit::os::unix::WindowBuilderExt; pub use winit::os::unix::WindowExt; -pub use api::egl::ffi::EGLContext; -pub use api::glx::ffi::GLXContext; -pub use api::osmesa::ffi::OSMesaContext; -pub use platform::RawHandle; - use {Context, HeadlessContext}; use os::GlContextExt; +use std::os::raw::c_void; + impl GlContextExt for Context { type Handle = RawHandle; @@ -24,7 +25,7 @@ impl GlContextExt for Context { } impl GlContextExt for HeadlessContext { - type Handle = OSMesaContext; + type Handle = *mut c_void; #[inline] unsafe fn raw_handle(&self) -> Self::Handle { diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index 049044f79c..78d108715b 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -1,14 +1,16 @@ #![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))] -use winit; -use winit::os::unix::EventsLoopExt; - use {Api, ContextError, CreationError, GlAttributes, PixelFormat, PixelFormatRequirements}; use api::egl; use api::glx; -use api::osmesa::{self, OsMesaContext}; +use api::osmesa::OsMesaContext; use self::x11::GlContext; +use winit; +use winit::os::unix::EventsLoopExt; + +use std::os::raw::c_void; + mod wayland; mod x11; @@ -179,7 +181,7 @@ impl HeadlessContext { } #[inline] - pub unsafe fn raw_handle(&self) -> osmesa::ffi::OSMesaContext { + pub unsafe fn raw_handle(&self) -> *mut c_void { self.0.raw_handle() } } diff --git a/src/platform/macos/headless.rs b/src/platform/macos/headless.rs index 9481e92b08..31030e71da 100644 --- a/src/platform/macos/headless.rs +++ b/src/platform/macos/headless.rs @@ -2,15 +2,16 @@ use ContextError; use CreationError; use CreationError::OsError; use GlAttributes; +use PixelFormat; use PixelFormatRequirements; +use super::helpers; use core_foundation::base::TCFType; use core_foundation::string::CFString; use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName}; use cocoa::base::{id, nil}; use cocoa::appkit::*; -use PixelFormat; -use super::helpers; +use std::os::raw::c_void; #[derive(Clone, Default)] pub struct PlatformSpecificHeadlessBuilderAttributes; @@ -87,8 +88,8 @@ impl HeadlessContext { } #[inline] - pub unsafe fn raw_handle(&self) -> id { - self.context + pub unsafe fn raw_handle(&self) -> *mut c_void { + self.context as *mut _ } } diff --git a/src/platform/macos/mod.rs b/src/platform/macos/mod.rs index f3e0be911c..eb2f8df8a9 100644 --- a/src/platform/macos/mod.rs +++ b/src/platform/macos/mod.rs @@ -2,7 +2,8 @@ pub use self::headless::HeadlessContext; pub use self::headless::PlatformSpecificHeadlessBuilderAttributes; -pub use winit::{MonitorId}; + +pub use winit::MonitorId; use CreationError; use ContextError; @@ -11,23 +12,20 @@ use PixelFormat; use PixelFormatRequirements; use Robustness; -use objc::runtime::{BOOL, NO}; - use cgl::{CGLEnable, kCGLCECrashOnRemovedFunctions, CGLSetParameter, kCGLCPSurfaceOpacity}; - use cocoa::base::{id, nil}; use cocoa::foundation::NSAutoreleasePool; use cocoa::appkit::{self, NSOpenGLContext, NSOpenGLPixelFormat}; - use core_foundation::base::TCFType; use core_foundation::string::CFString; use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName}; +use objc::runtime::{BOOL, NO}; +use winit; +use winit::os::macos::WindowExt; use std::str::FromStr; use std::ops::Deref; - -use winit; -use winit::os::macos::WindowExt; +use std::os::raw::c_void; mod headless; mod helpers; @@ -39,7 +37,6 @@ pub struct Context { } impl Context { - pub fn new( window_builder: winit::WindowBuilder, events_loop: &winit::EventsLoop, @@ -185,8 +182,8 @@ impl Context { } #[inline] - pub unsafe fn raw_handle(&self) -> id { - *self.gl.deref() + pub unsafe fn raw_handle(&self) -> *mut c_void { + *self.gl.deref() as *mut _ } }