diff options
author | Pierre Krieger <pierre.krieger1708@gmail.com> | 2015-04-30 13:23:37 +0200 |
---|---|---|
committer | Pierre Krieger <pierre.krieger1708@gmail.com> | 2015-04-30 16:57:07 +0200 |
commit | aa58f4149a65783b07c0dc401b5854ed80c4915e (patch) | |
tree | dd5d06aa46a1e18f0b93c2960dacc2cea9c81982 /src/api | |
parent | a08388bca1ca275fd1455ceae26ab06c02c81111 (diff) | |
download | glutin-aa58f4149a65783b07c0dc401b5854ed80c4915e.tar.gz glutin-aa58f4149a65783b07c0dc401b5854ed80c4915e.zip |
Add a GlContext trait
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/caca/mod.rs | 72 | ||||
-rw-r--r-- | src/api/cocoa/headless.rs | 18 | ||||
-rw-r--r-- | src/api/cocoa/mod.rs | 85 | ||||
-rw-r--r-- | src/api/egl/mod.rs | 23 | ||||
-rw-r--r-- | src/api/glx/mod.rs | 22 | ||||
-rw-r--r-- | src/api/osmesa/mod.rs | 26 | ||||
-rw-r--r-- | src/api/win32/mod.rs | 79 | ||||
-rw-r--r-- | src/api/x11/mod.rs | 91 |
8 files changed, 231 insertions, 185 deletions
diff --git a/src/api/caca/mod.rs b/src/api/caca/mod.rs index d8199dd..da7810a 100644 --- a/src/api/caca/mod.rs +++ b/src/api/caca/mod.rs @@ -3,9 +3,11 @@ use libc; use api::osmesa::OsMesaContext; +use Api; use BuilderAttribs; use CreationError; use Event; +use GlContext; use PixelFormat; use CursorState; use MouseCursor; @@ -169,34 +171,6 @@ impl Window { } } - pub unsafe fn make_current(&self) { - self.opengl.make_current() - } - - pub fn is_current(&self) -> bool { - self.opengl.is_current() - } - - pub fn get_proc_address(&self, addr: &str) -> *const () { - self.opengl.get_proc_address(addr) as *const _ - } - - pub fn swap_buffers(&self) { - unsafe { - let canvas = (self.libcaca.caca_get_canvas)(self.display); - let width = (self.libcaca.caca_get_canvas_width)(canvas); - let height = (self.libcaca.caca_get_canvas_height)(canvas); - - let buffer = self.opengl.get_framebuffer().chunks(self.opengl.get_dimensions().0 as usize) - .flat_map(|i| i.iter().cloned()).rev().collect::<Vec<u32>>(); - - (self.libcaca.caca_dither_bitmap)(canvas, 0, 0, width as libc::c_int, - height as libc::c_int, self.dither, - buffer.as_ptr() as *const _); - (self.libcaca.caca_refresh_display)(self.display); - }; - } - pub fn platform_display(&self) -> *mut libc::c_void { unimplemented!() } @@ -205,10 +179,6 @@ impl Window { unimplemented!() } - pub fn get_api(&self) -> ::Api { - self.opengl.get_api() - } - pub fn get_pixel_format(&self) -> PixelFormat { unimplemented!(); } @@ -232,6 +202,44 @@ impl Window { } } +impl GlContext for Window { + unsafe fn make_current(&self) { + self.opengl.make_current() + } + + fn is_current(&self) -> bool { + self.opengl.is_current() + } + + fn get_proc_address(&self, addr: &str) -> *const libc::c_void { + self.opengl.get_proc_address(addr) + } + + fn swap_buffers(&self) { + unsafe { + let canvas = (self.libcaca.caca_get_canvas)(self.display); + let width = (self.libcaca.caca_get_canvas_width)(canvas); + let height = (self.libcaca.caca_get_canvas_height)(canvas); + + let buffer = self.opengl.get_framebuffer().chunks(self.opengl.get_dimensions().0 as usize) + .flat_map(|i| i.iter().cloned()).rev().collect::<Vec<u32>>(); + + (self.libcaca.caca_dither_bitmap)(canvas, 0, 0, width as libc::c_int, + height as libc::c_int, self.dither, + buffer.as_ptr() as *const _); + (self.libcaca.caca_refresh_display)(self.display); + }; + } + + fn get_api(&self) -> Api { + self.opengl.get_api() + } + + fn get_pixel_format(&self) -> PixelFormat { + self.opengl.get_pixel_format() + } +} + impl Drop for Window { fn drop(&mut self) { unsafe { diff --git a/src/api/cocoa/headless.rs b/src/api/cocoa/headless.rs index 298027f..cc2a526 100644 --- a/src/api/cocoa/headless.rs +++ b/src/api/cocoa/headless.rs @@ -1,6 +1,7 @@ use CreationError; use CreationError::OsError; use BuilderAttribs; +use GlContext; use libc; use std::ptr; @@ -56,8 +57,10 @@ impl HeadlessContext { Ok(headless) } +} - pub unsafe fn make_current(&self) { +impl GlContext for HeadlessContext { + unsafe fn make_current(&self) { self.context.makeCurrentContext(); gl::GenFramebuffersEXT(1, &mut framebuffer); @@ -76,11 +79,11 @@ impl HeadlessContext { } } - pub fn is_current(&self) -> bool { + fn is_current(&self) -> bool { unimplemented!() } - pub fn get_proc_address(&self, _addr: &str) -> *const () { + fn get_proc_address(&self, _addr: &str) -> *const () { let symbol_name: CFString = _addr.parse().unwrap(); let framework_name: CFString = "com.apple.opengl".parse().unwrap(); let framework = unsafe { @@ -92,9 +95,16 @@ impl HeadlessContext { symbol as *const () } - pub fn get_api(&self) -> ::Api { + fn swap_buffers(&self) { + } + + fn get_api(&self) -> ::Api { ::Api::OpenGl } + + fn get_pixel_format(&self) -> PixelFormat { + unimplemented!(); + } } unsafe impl Send for HeadlessContext {} diff --git a/src/api/cocoa/mod.rs b/src/api/cocoa/mod.rs index 28d5ecc..101cfc0 100644 --- a/src/api/cocoa/mod.rs +++ b/src/api/cocoa/mod.rs @@ -9,6 +9,7 @@ use libc; use Api; use BuilderAttribs; +use GlContext; use GlRequest; use PixelFormat; use native_monitor::NativeMonitorId; @@ -672,39 +673,6 @@ impl Window { return None; } - pub unsafe fn make_current(&self) { - let _: () = msg_send![*self.context, update]; - self.context.makeCurrentContext(); - } - - pub fn is_current(&self) -> bool { - unsafe { - let current = NSOpenGLContext::currentContext(nil); - if current != nil { - let is_equal: BOOL = msg_send![current, isEqual:*self.context]; - is_equal != NO - } else { - false - } - } - } - - pub fn get_proc_address(&self, _addr: &str) -> *const () { - let symbol_name: CFString = FromStr::from_str(_addr).unwrap(); - let framework_name: CFString = FromStr::from_str("com.apple.opengl").unwrap(); - let framework = unsafe { - CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef()) - }; - let symbol = unsafe { - CFBundleGetFunctionPointerForName(framework, symbol_name.as_concrete_TypeRef()) - }; - symbol as *const () - } - - pub fn swap_buffers(&self) { - unsafe { self.context.flushBuffer(); } - } - pub fn platform_display(&self) -> *mut libc::c_void { unimplemented!() } @@ -713,14 +681,6 @@ impl Window { unimplemented!() } - pub fn get_api(&self) -> ::Api { - ::Api::OpenGl - } - - pub fn get_pixel_format(&self) -> PixelFormat { - self.pixel_format.clone() - } - pub fn set_window_resize_callback(&mut self, callback: Option<fn(u32, u32)>) { self.delegate.state.resize_handler = callback; } @@ -791,6 +751,49 @@ impl Window { } } +impl GlContext for Window { + unsafe fn make_current(&self) { + let _: () = msg_send![*self.context, update]; + self.context.makeCurrentContext(); + } + + fn is_current(&self) -> bool { + unsafe { + let current = NSOpenGLContext::currentContext(nil); + if current != nil { + let is_equal: BOOL = msg_send![current, isEqual:*self.context]; + is_equal != NO + } else { + false + } + } + } + + fn get_proc_address(&self, addr: &str) -> *const libc::c_void { + let symbol_name: CFString = FromStr::from_str(_addr).unwrap(); + let framework_name: CFString = FromStr::from_str("com.apple.opengl").unwrap(); + let framework = unsafe { + CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef()) + }; + let symbol = unsafe { + CFBundleGetFunctionPointerForName(framework, symbol_name.as_concrete_TypeRef()) + }; + symbol as *const _ + } + + fn swap_buffers(&self) { + unsafe { self.context.flushBuffer(); } + } + + fn get_api(&self) -> ::Api { + ::Api::OpenGl + } + + fn get_pixel_format(&self) -> PixelFormat { + self.pixel_format.clone() + } +} + struct IdRef(id); impl IdRef { diff --git a/src/api/egl/mod.rs b/src/api/egl/mod.rs index 9c8db62..a6b6bcc 100644 --- a/src/api/egl/mod.rs +++ b/src/api/egl/mod.rs @@ -2,6 +2,7 @@ use BuilderAttribs; use CreationError; +use GlContext; use GlRequest; use PixelFormat; use Api; @@ -156,8 +157,10 @@ impl Context { pixel_format: pixel_format, }) } +} - pub fn make_current(&self) { +impl GlContext for Context { + unsafe fn make_current(&self) { let ret = unsafe { self.egl.MakeCurrent(self.display, self.surface, self.surface, self.context) }; @@ -167,23 +170,19 @@ impl Context { } } - pub fn get_pixel_format(&self) -> &PixelFormat { - &self.pixel_format - } - - pub fn is_current(&self) -> bool { + fn is_current(&self) -> bool { unsafe { self.egl.GetCurrentContext() == self.context } } - pub fn get_proc_address(&self, addr: &str) -> *const () { + fn get_proc_address(&self, addr: &str) -> *const libc::c_void { let addr = CString::new(addr.as_bytes()).unwrap(); let addr = addr.as_ptr(); unsafe { - self.egl.GetProcAddress(addr) as *const () + self.egl.GetProcAddress(addr) as *const _ } } - pub fn swap_buffers(&self) { + fn swap_buffers(&self) { let ret = unsafe { self.egl.SwapBuffers(self.display, self.surface) }; @@ -193,9 +192,13 @@ impl Context { } } - pub fn get_api(&self) -> Api { + fn get_api(&self) -> Api { self.api } + + fn get_pixel_format(&self) -> PixelFormat { + self.pixel_format.clone() + } } unsafe impl Send for Context {} diff --git a/src/api/glx/mod.rs b/src/api/glx/mod.rs index fb86dfd..7aa77e7 100644 --- a/src/api/glx/mod.rs +++ b/src/api/glx/mod.rs @@ -2,8 +2,10 @@ use BuilderAttribs; use CreationError; +use GlContext; use GlRequest; use Api; +use PixelFormat; use libc; use std::ffi::CString; @@ -140,35 +142,41 @@ impl Context { context: context, }) } +} - pub fn make_current(&self) { - let res = unsafe { ffi::glx::MakeCurrent(self.display as *mut _, self.window, self.context) }; +impl GlContext for Context { + unsafe fn make_current(&self) { + let res = ffi::glx::MakeCurrent(self.display as *mut _, self.window, self.context); if res == 0 { panic!("glx::MakeCurrent failed"); } } - pub fn is_current(&self) -> bool { + fn is_current(&self) -> bool { unsafe { ffi::glx::GetCurrentContext() == self.context } } - pub fn get_proc_address(&self, addr: &str) -> *const () { + fn get_proc_address(&self, addr: &str) -> *const libc::c_void { let addr = CString::new(addr.as_bytes()).unwrap(); let addr = addr.as_ptr(); unsafe { - ffi::glx::GetProcAddress(addr as *const _) as *const () + ffi::glx::GetProcAddress(addr as *const _) as *const _ } } - pub fn swap_buffers(&self) { + fn swap_buffers(&self) { unsafe { ffi::glx::SwapBuffers(self.display as *mut _, self.window) } } - pub fn get_api(&self) -> ::Api { + fn get_api(&self) -> ::Api { ::Api::OpenGl } + + fn get_pixel_format(&self) -> PixelFormat { + unimplemented!(); + } } unsafe impl Send for Context {} diff --git a/src/api/osmesa/mod.rs b/src/api/osmesa/mod.rs index ea90583..61eebc1 100644 --- a/src/api/osmesa/mod.rs +++ b/src/api/osmesa/mod.rs @@ -2,9 +2,12 @@ extern crate osmesa_sys; +use Api; use BuilderAttribs; use CreationError; use CreationError::OsError; +use GlContext; +use PixelFormat; use libc; use std::{mem, ptr}; use std::ffi::CString; @@ -43,7 +46,13 @@ impl OsMesaContext { (self.width, self.height) } - pub unsafe fn make_current(&self) { + // TODO: can we remove this without causing havoc? + pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) { + } +} + +impl GlContext for OsMesaContext { + unsafe fn make_current(&self) { let ret = osmesa_sys::OSMesaMakeCurrent(self.context, self.buffer.as_ptr() as *mut libc::c_void, 0x1401, self.width as libc::c_int, self.height as libc::c_int); @@ -53,23 +62,26 @@ impl OsMesaContext { } } - pub fn is_current(&self) -> bool { + fn is_current(&self) -> bool { unsafe { osmesa_sys::OSMesaGetCurrentContext() == self.context } } - pub fn get_proc_address(&self, addr: &str) -> *const () { + fn get_proc_address(&self, addr: &str) -> *const libc::c_void { unsafe { let c_str = CString::new(addr.as_bytes().to_vec()).unwrap(); mem::transmute(osmesa_sys::OSMesaGetProcAddress(mem::transmute(c_str.as_ptr()))) } } - /// See the docs in the crate root file. - pub fn get_api(&self) -> ::Api { - ::Api::OpenGl + fn swap_buffers(&self) { } - pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) { + fn get_api(&self) -> Api { + Api::OpenGl + } + + fn get_pixel_format(&self) -> PixelFormat { + unimplemented!(); } } diff --git a/src/api/win32/mod.rs b/src/api/win32/mod.rs index dd3e685..e6f5cb8 100644 --- a/src/api/win32/mod.rs +++ b/src/api/win32/mod.rs @@ -14,7 +14,9 @@ use std::sync::mpsc::Receiver; use libc; use {CreationError, Event, MouseCursor}; use CursorState; +use GlContext; +use Api; use PixelFormat; use BuilderAttribs; @@ -218,37 +220,6 @@ impl Window { } } - /// See the docs in the crate root file. - pub unsafe fn make_current(&self) { - // TODO: check return value - gl::wgl::MakeCurrent(self.window.1 as *const libc::c_void, - self.context.0 as *const libc::c_void); - } - - /// See the docs in the crate root file. - pub fn is_current(&self) -> bool { - unsafe { gl::wgl::GetCurrentContext() == self.context.0 as *const libc::c_void } - } - - /// See the docs in the crate root file. - pub fn get_proc_address(&self, addr: &str) -> *const () { - let addr = CString::new(addr.as_bytes()).unwrap(); - let addr = addr.as_ptr(); - - unsafe { - let p = gl::wgl::GetProcAddress(addr) as *const (); - if !p.is_null() { return p; } - kernel32::GetProcAddress(self.gl_library, addr) as *const () - } - } - - /// See the docs in the crate root file. - pub fn swap_buffers(&self) { - unsafe { - gdi32::SwapBuffers(self.window.1); - } - } - pub fn platform_display(&self) -> *mut libc::c_void { unimplemented!() } @@ -257,15 +228,6 @@ impl Window { self.window.0 as *mut libc::c_void } - /// See the docs in the crate root file. - pub fn get_api(&self) -> ::Api { - ::Api::OpenGl - } - - pub fn get_pixel_format(&self) -> PixelFormat { - self.pixel_format.clone() - } - pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) { } @@ -362,6 +324,43 @@ impl Window { } } +impl GlContext for Window { + unsafe fn make_current(&self) { + // TODO: check return value + gl::wgl::MakeCurrent(self.window.1 as *const libc::c_void, + self.context.0 as *const libc::c_void); + } + + fn is_current(&self) -> bool { + unsafe { gl::wgl::GetCurrentContext() == self.context.0 as *const libc::c_void } + } + + fn get_proc_address(&self, addr: &str) -> *const libc::c_void { + let addr = CString::new(addr.as_bytes()).unwrap(); + let addr = addr.as_ptr(); + + unsafe { + let p = gl::wgl::GetProcAddress(addr) as *const _; + if !p.is_null() { return p; } + kernel32::GetProcAddress(self.gl_library, addr) as *const _ + } + } + + fn swap_buffers(&self) { + unsafe { + gdi32::SwapBuffers(self.window.1); + } + } + + fn get_api(&self) -> Api { + Api::OpenGl + } + + fn get_pixel_format(&self) -> PixelFormat { + self.pixel_format.clone() + } +} + pub struct PollEventsIterator<'a> { window: &'a Window, } diff --git a/src/api/x11/mod.rs b/src/api/x11/mod.rs index 6b528ab..9946243 100644 --- a/src/api/x11/mod.rs +++ b/src/api/x11/mod.rs @@ -13,6 +13,7 @@ use std::sync::{Arc, Mutex, Once, ONCE_INIT}; use Api; use CursorState; +use GlContext; use GlRequest; use PixelFormat; @@ -674,38 +675,6 @@ impl Window { } } - pub unsafe fn make_current(&self) { - match self.x.context { - Context::Glx(ref ctxt) => ctxt.make_current(), - Context::Egl(ref ctxt) => ctxt.make_current(), - Context::None => {} - } - } - - pub fn is_current(&self) -> bool { - match self.x.context { - Context::Glx(ref ctxt) => ctxt.is_current(), - Context::Egl(ref ctxt) => ctxt.is_current(), - Context::None => panic!() - } - } - - pub fn get_proc_address(&self, addr: &str) -> *const () { - match self.x.context { - Context::Glx(ref ctxt) => ctxt.get_proc_address(addr), - Context::Egl(ref ctxt) => ctxt.get_proc_address(addr), - Context::None => ptr::null() - } - } - - pub fn swap_buffers(&self) { - match self.x.context { - Context::Glx(ref ctxt) => ctxt.swap_buffers(), - Context::Egl(ref ctxt) => ctxt.swap_buffers(), - Context::None => {} - } - } - pub fn platform_display(&self) -> *mut libc::c_void { self.x.display as *mut libc::c_void } @@ -714,18 +683,6 @@ impl Window { unimplemented!() } - /// See the docs in the crate root file. - pub fn get_api(&self) -> ::Api { - match self.x.context { - Context::Glx(ref ctxt) => ctxt.get_api(), - Context::Egl(ref ctxt) => ctxt.get_api(), - Context::None => panic!() - } - } - - pub fn get_pixel_format(&self) -> PixelFormat { - self.pixel_format.clone() - } pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) { } @@ -828,3 +785,49 @@ impl Window { Ok(()) } } + +impl GlContext for Window { + unsafe fn make_current(&self) { + match self.x.context { + Context::Glx(ref ctxt) => ctxt.make_current(), + Context::Egl(ref ctxt) => ctxt.make_current(), + Context::None => {} + } + } + + fn is_current(&self) -> bool { + match self.x.context { + Context::Glx(ref ctxt) => ctxt.is_current(), + Context::Egl(ref ctxt) => ctxt.is_current(), + Context::None => panic!() + } + } + + fn get_proc_address(&self, addr: &str) -> *const libc::c_void { + match self.x.context { + Context::Glx(ref ctxt) => ctxt.get_proc_address(addr), + Context::Egl(ref ctxt) => ctxt.get_proc_address(addr), + Context::None => ptr::null() + } + } + + fn swap_buffers(&self) { + match self.x.context { + Context::Glx(ref ctxt) => ctxt.swap_buffers(), + Context::Egl(ref ctxt) => ctxt.swap_buffers(), + Context::None => {} + } + } + + fn get_api(&self) -> Api { + match self.x.context { + Context::Glx(ref ctxt) => ctxt.get_api(), + Context::Egl(ref ctxt) => ctxt.get_api(), + Context::None => panic!() + } + } + + fn get_pixel_format(&self) -> PixelFormat { + self.pixel_format.clone() + } +} |