Skip to content

Commit

Permalink
Add a GlContext trait
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaka committed Apr 30, 2015
1 parent a08388b commit aa58f41
Show file tree
Hide file tree
Showing 12 changed files with 332 additions and 190 deletions.
72 changes: 40 additions & 32 deletions src/api/caca/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!()
}
Expand All @@ -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!();
}
Expand All @@ -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 {
Expand Down
18 changes: 14 additions & 4 deletions src/api/cocoa/headless.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use CreationError;
use CreationError::OsError;
use BuilderAttribs;
use GlContext;
use libc;
use std::ptr;

Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand All @@ -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 {}
Expand Down
85 changes: 44 additions & 41 deletions src/api/cocoa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use libc;

use Api;
use BuilderAttribs;
use GlContext;
use GlRequest;
use PixelFormat;
use native_monitor::NativeMonitorId;
Expand Down Expand Up @@ -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!()
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 13 additions & 10 deletions src/api/egl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use BuilderAttribs;
use CreationError;
use GlContext;
use GlRequest;
use PixelFormat;
use Api;
Expand Down Expand Up @@ -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)
};
Expand All @@ -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)
};
Expand All @@ -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 {}
Expand Down
22 changes: 15 additions & 7 deletions src/api/glx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

use BuilderAttribs;
use CreationError;
use GlContext;
use GlRequest;
use Api;
use PixelFormat;

use libc;
use std::ffi::CString;
Expand Down Expand Up @@ -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 {}
Expand Down
Loading

0 comments on commit aa58f41

Please sign in to comment.