-
Notifications
You must be signed in to change notification settings - Fork 83
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
Fix eglGetProcAddress for core OpenGL entrypoints #94
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,20 @@ use euclid::Size2D; | |
use platform::NativeGLContextMethods; | ||
use platform::with_egl::utils::{create_pixel_buffer_backed_offscreen_context}; | ||
use std::ffi::CString; | ||
use std::ops::Deref; | ||
use egl; | ||
use egl::types::{EGLint, EGLBoolean, EGLDisplay, EGLSurface, EGLConfig, EGLContext}; | ||
use libloading as lib; | ||
|
||
lazy_static! { | ||
static ref GL_LIB: Option<lib::Library> = { | ||
if cfg!(target_os = "android") { | ||
lib::Library::new("libGLESv2.so").ok() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't support run-time selection of the GL backend. E.g. one may want to run with GLES on Linux desktop. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would require a change in the API because get_proc_address trait is static and we can't know the user preference. We could work on that on a separate issue/PR, it's less critical in this moment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we should be using
|
||
} else { | ||
lib::Library::new("libGL.so").ok() | ||
} | ||
}; | ||
} | ||
pub struct NativeGLContextHandle(pub EGLDisplay, pub EGLSurface); | ||
unsafe impl Send for NativeGLContextHandle {} | ||
|
||
|
@@ -69,10 +80,21 @@ impl Drop for NativeGLContext { | |
impl NativeGLContextMethods for NativeGLContext { | ||
type Handle = NativeGLContextHandle; | ||
|
||
// According to the EGL spec <= 1.4, eglGetProcAddress should only be used to | ||
// retrieve extension functions. Some implementatios return NULL for core OpenGL functions. | ||
// Other implementations may return non-NULL values even for invalid core or extension symbols. | ||
// This is very dangerous, so we use dlsym function before calling eglGetProcAddress | ||
// in order to avoid possible garbage pointers. | ||
fn get_proc_address(addr: &str) -> *const () { | ||
unsafe { | ||
let addr = CString::new(addr.as_bytes()).unwrap().as_ptr(); | ||
egl::GetProcAddress(addr as *const _) as *const () | ||
if let Some(ref lib) = *GL_LIB { | ||
let symbol: lib::Symbol<unsafe extern fn()> = lib.get(addr.as_bytes()).unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps returning |
||
return *symbol.deref() as *const(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: space after |
||
} | ||
|
||
let addr = CString::new(addr.as_bytes()); | ||
let addr = addr.unwrap().as_ptr(); | ||
egl::GetProcAddress(addr) as *const () | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting, so we can have a dependency mentioned twice?