Skip to content

Commit

Permalink
Fix remaining clippy warnings by hand
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed May 28, 2022
1 parent 09e50a2 commit e7e9923
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 175 deletions.
7 changes: 2 additions & 5 deletions glutin/src/api/dlloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ impl<T: SymTrait> SymWrapper<T> {
#[cfg(not(target_os = "windows"))]
let lib = unsafe { Library::new(path) };

if lib.is_ok() {
return Ok(SymWrapper {
inner: T::load_with(lib.as_ref().unwrap()),
_lib: Arc::new(lib.unwrap()),
});
if let Ok(lib) = lib {
return Ok(SymWrapper { inner: T::load_with(&lib), _lib: Arc::new(lib) });
}
}

Expand Down
161 changes: 78 additions & 83 deletions glutin/src/api/egl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,91 +7,85 @@
target_os = "netbsd",
target_os = "openbsd",
))]
#![allow(unused_variables)]

mod egl {
use super::ffi;
use crate::api::dlloader::{SymTrait, SymWrapper};
use libloading;
use std::sync::{Arc, Mutex};
use crate::api::dlloader::{SymTrait, SymWrapper};
use libloading;
use std::sync::{Arc, Mutex};

#[cfg(unix)]
use libloading::os::unix as libloading_os;
#[cfg(windows)]
use libloading::os::windows as libloading_os;
#[cfg(unix)]
use libloading::os::unix as libloading_os;
#[cfg(windows)]
use libloading::os::windows as libloading_os;

#[derive(Clone)]
pub struct Egl(pub SymWrapper<ffi::egl::Egl>);
#[derive(Clone)]
pub struct Egl(pub SymWrapper<ffi::egl::Egl>);

/// Because `*const raw::c_void` doesn't implement `Sync`.
unsafe impl Sync for Egl {}
/// Because `*const raw::c_void` doesn't implement `Sync`.
unsafe impl Sync for Egl {}

type EglGetProcAddressType = libloading_os::Symbol<
unsafe extern "C" fn(*const std::os::raw::c_void) -> *const std::os::raw::c_void,
>;
type EglGetProcAddressType = libloading_os::Symbol<
unsafe extern "C" fn(*const std::os::raw::c_void) -> *const std::os::raw::c_void,
>;

lazy_static! {
static ref EGL_GET_PROC_ADDRESS: Arc<Mutex<Option<EglGetProcAddressType>>> =
Arc::new(Mutex::new(None));
}
lazy_static! {
static ref EGL_GET_PROC_ADDRESS: Arc<Mutex<Option<EglGetProcAddressType>>> =
Arc::new(Mutex::new(None));
}

impl SymTrait for ffi::egl::Egl {
fn load_with(lib: &libloading::Library) -> Self {
let f = move |s: &'static str| -> *const std::os::raw::c_void {
// Check if the symbol is available in the library directly. If
// it is, just return it.
match unsafe {
lib.get(std::ffi::CString::new(s.as_bytes()).unwrap().as_bytes_with_nul())
} {
Ok(sym) => return *sym,
Err(_) => (),
};

let mut egl_get_proc_address = (*EGL_GET_PROC_ADDRESS).lock().unwrap();
if egl_get_proc_address.is_none() {
unsafe {
let sym: libloading::Symbol<
unsafe extern "C" fn(
*const std::os::raw::c_void,
)
-> *const std::os::raw::c_void,
> = lib.get(b"eglGetProcAddress").unwrap();
*egl_get_proc_address = Some(sym.into_raw());
}
}
impl SymTrait for ffi::egl::Egl {
fn load_with(lib: &libloading::Library) -> Self {
let f = move |s: &'static str| -> *const std::os::raw::c_void {
// Check if the symbol is available in the library directly. If
// it is, just return it.
if let Ok(sym) = unsafe {
lib.get(std::ffi::CString::new(s.as_bytes()).unwrap().as_bytes_with_nul())
} {
return *sym;
}

// The symbol was not available in the library, so ask
// eglGetProcAddress for it. Note that eglGetProcAddress was
// only able to look up extension functions prior to EGL 1.5,
// hence this two-part dance.
let mut egl_get_proc_address = (*EGL_GET_PROC_ADDRESS).lock().unwrap();
if egl_get_proc_address.is_none() {
unsafe {
(egl_get_proc_address.as_ref().unwrap())(
std::ffi::CString::new(s.as_bytes()).unwrap().as_bytes_with_nul().as_ptr()
as *const std::os::raw::c_void,
)
let sym: libloading::Symbol<
unsafe extern "C" fn(
*const std::os::raw::c_void,
)
-> *const std::os::raw::c_void,
> = lib.get(b"eglGetProcAddress").unwrap();
*egl_get_proc_address = Some(sym.into_raw());
}
};
}

Self::load_with(f)
}
// The symbol was not available in the library, so ask
// eglGetProcAddress for it. Note that eglGetProcAddress was
// only able to look up extension functions prior to EGL 1.5,
// hence this two-part dance.
unsafe {
(egl_get_proc_address.as_ref().unwrap())(
std::ffi::CString::new(s.as_bytes()).unwrap().as_bytes_with_nul().as_ptr()
as *const std::os::raw::c_void,
)
}
};

Self::load_with(f)
}
}

impl Egl {
pub fn new() -> Result<Self, ()> {
#[cfg(target_os = "windows")]
let paths = vec!["libEGL.dll", "atioglxx.dll"];
impl Egl {
pub fn new() -> Result<Self, ()> {
#[cfg(target_os = "windows")]
let paths = vec!["libEGL.dll", "atioglxx.dll"];

#[cfg(not(target_os = "windows"))]
let paths = vec!["libEGL.so.1", "libEGL.so"];
#[cfg(not(target_os = "windows"))]
let paths = vec!["libEGL.so.1", "libEGL.so"];

SymWrapper::new(paths).map(Egl)
}
SymWrapper::new(paths).map(Egl)
}
}

mod make_current_guard;

pub use self::egl::Egl;
use self::make_current_guard::MakeCurrentGuard;
#[cfg(not(target_os = "windows"))]
use crate::Rect;
Expand All @@ -101,7 +95,6 @@ use crate::{
};

use glutin_egl_sys as ffi;
use parking_lot::Mutex;
#[cfg(any(
target_os = "android",
target_os = "windows",
Expand Down Expand Up @@ -158,7 +151,7 @@ pub enum NativeDisplay {
pub struct Context {
display: ffi::egl::types::EGLDisplay,
context: ffi::egl::types::EGLContext,
surface: Option<Mutex<ffi::egl::types::EGLSurface>>,
surface: Option<parking_lot::Mutex<ffi::egl::types::EGLSurface>>,
api: Api,
pixel_format: PixelFormat,
#[cfg(target_os = "android")]
Expand Down Expand Up @@ -251,7 +244,7 @@ fn get_native_display(native_display: &NativeDisplay) -> *const raw::c_void {
}
};

let has_dp_extension = |e: &str| dp_extensions.iter().any(|s| &s == &e);
let has_dp_extension = |e: &str| dp_extensions.iter().any(|s| s == e);

match *native_display {
// Note: Some EGL implementations are missing the
Expand Down Expand Up @@ -378,8 +371,8 @@ impl Context {
///
/// This function initializes some things and chooses the pixel format.
///
/// To finish the process, you must call `.finish(window)` on the
/// `ContextPrototype`.
/// To finish the process, you must call [`ContextPrototype::finish()`].
#[allow(clippy::new_ret_no_self)]
pub fn new<'a, F>(
pf_reqs: &PixelFormatRequirements,
opengl: &'a GlAttributes<&'a Context>,
Expand Down Expand Up @@ -683,7 +676,7 @@ impl Drop for Context {
guard.if_any_same_then_invalidate(surface, surface, self.context);

let gl_finish_fn = self.get_proc_address("glFinish");
assert!(gl_finish_fn != std::ptr::null());
assert!(!gl_finish_fn.is_null());
let gl_finish_fn = std::mem::transmute::<_, extern "system" fn()>(gl_finish_fn);
gl_finish_fn();

Expand Down Expand Up @@ -831,7 +824,7 @@ impl<'a> ContextPrototype<'a> {
pub fn finish_surfaceless(self) -> Result<Context, CreationError> {
// FIXME: Also check for the GL_OES_surfaceless_context *CONTEXT*
// extension
if !self.extensions.iter().any(|s| &s == &"EGL_KHR_surfaceless_context") {
if !self.extensions.iter().any(|s| s == "EGL_KHR_surfaceless_context") {
Err(CreationError::NotSupported("EGL surfaceless not supported".to_string()))
} else {
self.finish_impl(None)
Expand All @@ -851,11 +844,6 @@ impl<'a> ContextPrototype<'a> {
let size: (u32, u32) = size.into();

let egl = EGL.as_ref().unwrap();
let tex_fmt = if self.pixel_format.alpha_bits > 0 {
ffi::egl::TEXTURE_RGBA
} else {
ffi::egl::TEXTURE_RGB
};
let attrs = &[
ffi::egl::WIDTH as raw::c_int,
size.0 as raw::c_int,
Expand Down Expand Up @@ -984,7 +972,7 @@ impl<'a> ContextPrototype<'a> {
Ok(Context {
display: self.display,
context,
surface: surface.map(Mutex::new),
surface: surface.map(parking_lot::Mutex::new),
api: self.api,
pixel_format: self.pixel_format,
#[cfg(target_os = "android")]
Expand Down Expand Up @@ -1163,7 +1151,7 @@ where
.into_iter()
.filter(|&config| {
let mut min_swap_interval = 0;
let res = egl.GetConfigAttrib(
let _res = egl.GetConfigAttrib(
display,
config,
ffi::egl::MIN_SWAP_INTERVAL as ffi::egl::types::EGLint,
Expand All @@ -1175,7 +1163,7 @@ where
}

let mut max_swap_interval = 0;
let res = egl.GetConfigAttrib(
let _res = egl.GetConfigAttrib(
display,
config,
ffi::egl::MAX_SWAP_INTERVAL as ffi::egl::types::EGLint,
Expand Down Expand Up @@ -1251,21 +1239,21 @@ unsafe fn create_context(
let mut context_attributes = Vec::with_capacity(10);
let mut flags = 0;

if egl_version >= &(1, 5) || extensions.iter().any(|s| &s == &"EGL_KHR_create_context") {
if egl_version >= &(1, 5) || extensions.iter().any(|s| s == "EGL_KHR_create_context") {
context_attributes.push(ffi::egl::CONTEXT_MAJOR_VERSION as i32);
context_attributes.push(version.0 as i32);
context_attributes.push(ffi::egl::CONTEXT_MINOR_VERSION as i32);
context_attributes.push(version.1 as i32);

// handling robustness
let supports_robustness = egl_version >= &(1, 5)
|| extensions.iter().any(|s| &s == &"EGL_EXT_create_context_robustness");
|| extensions.iter().any(|s| s == "EGL_EXT_create_context_robustness");

match gl_robustness {
Robustness::NotRobust => (),

Robustness::NoError => {
if extensions.iter().any(|s| &s == &"EGL_KHR_create_context_no_error") {
if extensions.iter().any(|s| s == "EGL_KHR_create_context_no_error") {
context_attributes.push(ffi::egl::CONTEXT_OPENGL_NO_ERROR_KHR as raw::c_int);
context_attributes.push(1);
}
Expand Down Expand Up @@ -1315,6 +1303,13 @@ unsafe fn create_context(
if gl_debug && egl_version >= &(1, 5) {
context_attributes.push(ffi::egl::CONTEXT_OPENGL_DEBUG as i32);
context_attributes.push(ffi::egl::TRUE as i32);

// TODO: using this flag sometimes generates an error
// there was a change in the specs that added this flag, so it
// may not be supported everywhere ; however it is
// not possible to know whether it is supported or
// not flags = flags |
// ffi::egl::CONTEXT_OPENGL_DEBUG_BIT_KHR as i32;
}

// In at least some configurations, the Android emulator’s GL
Expand Down
5 changes: 1 addition & 4 deletions glutin/src/api/glx/make_current_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ impl Drop for MakeCurrentGuard {
None => (0, std::ptr::null()),
};

let display = match self.old_display {
old_display if old_display == std::ptr::null_mut() => self.display,
old_display => old_display,
};
let display = if self.old_display.is_null() { self.display } else { self.old_display };

let res = unsafe { glx.MakeCurrent(display as *mut _, drawable, context) };

Expand Down
Loading

0 comments on commit e7e9923

Please sign in to comment.