Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement access of raw context handles #939

Merged
merged 5 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/api/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ pub struct Context {
egl_context: EglContext,
}

#[derive(Clone, Default)]
pub struct PlatformSpecificHeadlessBuilderAttributes;

impl Context {
pub fn new(
window_builder: winit::WindowBuilder,
Expand Down Expand Up @@ -81,8 +78,16 @@ impl Context {
pub fn get_pixel_format(&self) -> PixelFormat {
self.egl_context.get_pixel_format()
}

#[inline]
pub unsafe fn raw_handle(&self) -> egl::ffi::EGLContext {
self.egl_context.raw_handle()
}
}

#[derive(Clone, Default)]
pub struct PlatformSpecificHeadlessBuilderAttributes;

pub struct HeadlessContext(EglContext);

unsafe impl Send for HeadlessContext {}
Expand Down Expand Up @@ -135,4 +140,9 @@ impl HeadlessContext {
pub fn get_pixel_format(&self) -> PixelFormat {
self.0.get_pixel_format()
}

#[inline]
pub unsafe fn raw_handle(&self) -> egl::ffi::EGLContext {
self.0.raw_handle()
}
}
1 change: 0 additions & 1 deletion src/api/caca/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ impl Context {
pub fn get_pixel_format(&self) -> PixelFormat {
self.opengl.get_pixel_format()
}

}

impl Drop for Context {
Expand Down
6 changes: 4 additions & 2 deletions src/api/egl/ffi.rs
Original file line number Diff line number Diff line change
@@ -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;
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 @@ -320,6 +320,11 @@ impl Context {
pub fn get_pixel_format(&self) -> PixelFormat {
self.pixel_format.clone()
}

#[inline]
pub unsafe fn raw_handle(&self) -> ffi::egl::types::EGLContext {
self.context
}
}

unsafe impl Send for Context {}
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 @@ -143,6 +143,11 @@ impl Context {
pub fn get_pixel_format(&self) -> PixelFormat {
self.pixel_format.clone()
}

#[inline]
pub unsafe fn raw_handle(&self) -> ffi::GLXContext {
self.context
}
}

unsafe impl Send for Context {}
Expand Down
8 changes: 2 additions & 6 deletions src/api/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,10 @@ impl Window {
}

#[inline]
pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) {
}
pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) {}

#[inline]
pub fn set_cursor(&self, _: MouseCursor) {
}
pub fn set_cursor(&self, _: MouseCursor) {}

#[inline]
pub fn set_cursor_state(&self, _: CursorState) -> Result<(), String> {
Expand All @@ -369,7 +367,6 @@ impl Window {
pub fn create_window_proxy(&self) -> WindowProxy {
WindowProxy
}

}

impl GlContext for Window {
Expand Down Expand Up @@ -427,7 +424,6 @@ impl WindowProxy {
}
}


impl<'a> Iterator for WaitEventsIterator<'a> {
type Item = Event;

Expand Down
20 changes: 15 additions & 5 deletions src/api/osmesa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +13,16 @@ 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;
}

pub struct OsMesaContext {
context: osmesa_sys::OSMesaContext,
buffer: Vec<u32>,
Expand Down Expand Up @@ -186,6 +191,11 @@ impl OsMesaContext {
pub fn get_pixel_format(&self) -> PixelFormat {
unimplemented!();
}

#[inline]
pub unsafe fn raw_handle(&self) -> *mut c_void {
self.context as *mut _
}
}

impl Drop for OsMesaContext {
Expand Down
3 changes: 1 addition & 2 deletions src/api/wgl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,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 {
Expand Down Expand Up @@ -142,4 +142,3 @@ impl GlContext for HeadlessContext {
unimplemented!()
}
}

23 changes: 23 additions & 0 deletions src/os/android.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
#![cfg(any(target_os = "android"))]

pub use winit::os::android::{WindowBuilderExt, WindowExt};

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()
}
}
23 changes: 23 additions & 0 deletions src/os/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,26 @@ 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;

use std::os::raw::c_void;

impl GlContextExt for Context {
type Handle = *mut c_void;

#[inline]
unsafe fn raw_handle(&self) -> Self::Handle {
self.context.raw_handle()
}
}

impl GlContextExt for HeadlessContext {
type Handle = *mut c_void;

#[inline]
unsafe fn raw_handle(&self) -> Self::Handle {
self.context.raw_handle()
}
}
13 changes: 12 additions & 1 deletion src/os/mod.rs
Original file line number Diff line number Diff line change
@@ -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 raw_handle(&self) -> Self::Handle;
}
27 changes: 27 additions & 0 deletions src/os/unix.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
#![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;

use {Context, HeadlessContext};
use os::GlContextExt;

use std::os::raw::c_void;

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 = *mut c_void;

#[inline]
unsafe fn raw_handle(&self) -> Self::Handle {
self.context.raw_handle()
}
}
25 changes: 25 additions & 0 deletions src/os/windows.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
#![cfg(target_os = "windows")]

pub use winapi::HGLRC;
pub use winit::os::windows::{WindowBuilderExt, WindowExt, MonitorIdExt};

pub use api::egl::ffi::EGLContext;
pub use platform::RawHandle;

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()
}
}
14 changes: 13 additions & 1 deletion src/platform/emscripten/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#![cfg(target_os = "emscripten")]

use std::ffi::CString;

use {Api, ContextError, CreationError, GlAttributes, PixelFormat, PixelFormatRequirements};

use winit;
use std::ffi::CString;

mod ffi;

Expand Down Expand Up @@ -114,6 +116,11 @@ impl Context {
srgb: true,
}
}

#[inline]
pub unsafe fn raw_handle(&self) -> ffi::EMSCRIPTEN_WEBGL_CONTEXT_HANDLE {
self.context
}
}

impl Drop for Context {
Expand Down Expand Up @@ -178,6 +185,11 @@ impl HeadlessContext {
pub fn get_pixel_format(&self) -> PixelFormat {
unimplemented!()
}

#[inline]
pub unsafe fn raw_handle(&self) -> ffi::EMSCRIPTEN_WEBGL_CONTEXT_HANDLE {
self.context
}
}

fn error_to_str(code: ffi::EMSCRIPTEN_RESULT) -> &'static str {
Expand Down
Loading