Skip to content
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
50 changes: 32 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ log = "*"
git = "https://github.com/servo/rust-glx"

[dependencies.x11]
version = "1.0.0"
version = "1.1.0"
features = ["xlib"]

[dependencies.cgl]
Expand Down
5 changes: 5 additions & 0 deletions src/gl_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ pub struct GLContext {
}

impl GLContext {
#[inline(always)]
pub fn get_proc_address(addr: &str) -> *const () {
NativeGLContext::get_proc_address(addr)
}

pub fn create_headless() -> Result<GLContext, &'static str> {
let native_context = try!(NativeGLContext::create_headless());

Expand Down
3 changes: 3 additions & 0 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
use layers::platform::surface::NativeGraphicsMetadata;

pub trait NativeGLContextMethods {
fn get_proc_address(&str) -> *const ();

fn create_headless() -> Result<Self, &'static str>;
fn is_current(&self) -> bool;
fn make_current(&self) -> Result<(), &'static str>;

#[cfg(feature="texture_surface")]
fn get_metadata(&self) -> NativeGraphicsMetadata;

Expand Down
4 changes: 4 additions & 0 deletions src/platform/not_implemented/native_gl_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ pub struct NativeGLContext;
use layers::platform::surface::NativeGraphicsMetadata;

impl NativeGLContextMethods for NativeGLContext {
fn get_proc_address(addr: &str) -> *const () {
0 as *const ()
}

fn create_headless() -> Result<NativeGLContext, &'static str> {
Err("Not implemented (yet)")
}
Expand Down
17 changes: 17 additions & 0 deletions src/platform/with_cgl/native_gl_context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use cgl::*;
use std::mem;

use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName};
use core_foundation::base::TCFType;
use core_foundation::string::CFString;
use std::str::FromStr;

use platform::NativeGLContextMethods;

#[cfg(feature="texture_surface")]
Expand Down Expand Up @@ -56,6 +61,18 @@ impl Drop for NativeGLContext {
}

impl NativeGLContextMethods for NativeGLContext {
fn get_proc_address(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 ()
}

fn create_headless() -> Result<NativeGLContext, &'static str> {
let mut attributes = [
0
Expand Down
9 changes: 9 additions & 0 deletions src/platform/with_egl/native_gl_context.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ffi::CString;
use geom::Size2D;
use NativeGLContextMethods;
use platform::with_egl::utils::{create_pixel_buffer_backed_offscreen_context};
Expand Down Expand Up @@ -52,6 +53,14 @@ impl NativeGLContext {


impl NativeGLContextMethods for NativeGLContext {
fn get_proc_address(addr: &str) -> *const () {
let addr = CString::new(s.as_bytes()).unwrap();
let addr = addr.as_ptr();
unsafe {
egl::GetProcAddress(addr as *const _) as *const ()
}
}

fn create_headless() -> Result<NativeGLContext, &'static str> {
// We create a context with a dummy size, we can't rely on a
// default framebuffer
Expand Down
10 changes: 10 additions & 0 deletions src/platform/with_glx/native_gl_context.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ffi::CString;

use glx;
use x11::xlib::*;
use libc::*;
Expand Down Expand Up @@ -65,6 +67,14 @@ impl Drop for NativeGLContext {
}

impl NativeGLContextMethods for NativeGLContext {
fn get_proc_address(addr: &str) -> *const () {
let addr = CString::new(addr.as_bytes()).unwrap();
let addr = addr.as_ptr();
unsafe {
glx::GetProcAddress(addr as *const _) as *const ()
}
}

fn create_headless() -> Result<NativeGLContext, &'static str> {
// We create a context with a dummy size since in other platforms
// a default framebuffer is not bound
Expand Down
49 changes: 1 addition & 48 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,6 @@ use GLContext;
use GLContextAttributes;
use ColorAttachmentType;

use std::ffi::CString;

#[cfg(target_os = "linux")]
use glx;
#[cfg(target_os = "android")]
use egl;

#[cfg(target_os = "macos")]
use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName};
#[cfg(target_os = "macos")]
use core_foundation::base::TCFType;
#[cfg(target_os = "macos")]
use core_foundation::string::CFString;

use std::str::FromStr;

#[cfg(feature="texture_surface")]
use layers::texturegl::Texture;

Expand All @@ -38,45 +22,14 @@ extern {}
// This is probably a time bomb
static mut GL_LOADED : bool = false;

// Shamelessly stolen from glutin
#[cfg(target_os = "linux")]
fn get_proc_address(addr: &str) -> *const () {
let addr = CString::new(addr.as_bytes()).unwrap();
let addr = addr.as_ptr();
unsafe {
glx::GetProcAddress(addr as *const _) as *const ()
}
}

#[cfg(target_os = "android")]
fn get_proc_address(addr: &str) -> *const () {
let addr = CString::new(s.as_bytes()).unwrap();
let addr = addr.as_ptr();
unsafe {
egl::GetProcAddress(addr as *const _) as *const ()
}
}

#[cfg(target_os = "macos")]
fn get_proc_address(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 ()
}

fn load_gl() {
unsafe {
if GL_LOADED {
return;
}

gl::load_with(|s| get_proc_address(s) as *const _);
gl::load_with(|s| GLContext::get_proc_address(s) as *const _);
GL_LOADED = true;
}
}
Expand Down