Mark get_proc_address as safe

This commit is contained in:
Chandler Newman 2020-04-15 12:16:02 +01:00
parent a684f5d8d6
commit b8df85e744
6 changed files with 41 additions and 39 deletions

View File

@ -94,7 +94,7 @@ where
self.surface.swap_buffers()
}
unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void {
fn get_proc_address(&self, symbol: &str) -> *const c_void {
get_proc_address(symbol)
}

View File

@ -64,7 +64,7 @@ impl EGLContext {
}
};
let (pixel_format, config_id) = unsafe { display.choose_config(version, reqs)? };
let (pixel_format, config_id) = display.choose_config(version, reqs)?;
let mut context_attributes = Vec::with_capacity(10);

View File

@ -135,7 +135,7 @@ impl<B: native::Backend, N: native::NativeDisplay<B>> EGLDisplay<B, N> {
}
/// Finds a compatible [`EGLConfig`] for a given set of requirements
pub unsafe fn choose_config(
pub fn choose_config(
&self,
version: (u8, u8),
reqs: PixelFormatRequirements,
@ -257,19 +257,21 @@ impl<B: native::Backend, N: native::NativeDisplay<B>> EGLDisplay<B, N> {
// calling `eglChooseConfig`
let mut config_id = MaybeUninit::uninit();
let mut num_configs = MaybeUninit::uninit();
if ffi::egl::ChooseConfig(
*self.display,
descriptor.as_ptr(),
config_id.as_mut_ptr(),
1,
num_configs.as_mut_ptr(),
) == 0
if unsafe {
ffi::egl::ChooseConfig(
*self.display,
descriptor.as_ptr(),
config_id.as_mut_ptr(),
1,
num_configs.as_mut_ptr(),
)
} == 0
{
return Err(Error::ConfigFailed);
}
let config_id = config_id.assume_init();
let num_configs = num_configs.assume_init();
let config_id = unsafe { config_id.assume_init() };
let num_configs = unsafe { num_configs.assume_init() };
if num_configs == 0 {
error!(self.logger, "No matching color format found");
@ -295,21 +297,23 @@ impl<B: native::Backend, N: native::NativeDisplay<B>> EGLDisplay<B, N> {
}};
};
let desc = PixelFormat {
hardware_accelerated: attrib!(self.display, config_id, ffi::egl::CONFIG_CAVEAT)
!= ffi::egl::SLOW_CONFIG as i32,
color_bits: attrib!(self.display, config_id, ffi::egl::RED_SIZE) as u8
+ attrib!(self.display, config_id, ffi::egl::BLUE_SIZE) as u8
+ attrib!(self.display, config_id, ffi::egl::GREEN_SIZE) as u8,
alpha_bits: attrib!(self.display, config_id, ffi::egl::ALPHA_SIZE) as u8,
depth_bits: attrib!(self.display, config_id, ffi::egl::DEPTH_SIZE) as u8,
stencil_bits: attrib!(self.display, config_id, ffi::egl::STENCIL_SIZE) as u8,
stereoscopy: false,
multisampling: match attrib!(self.display, config_id, ffi::egl::SAMPLES) {
0 | 1 => None,
a => Some(a as u16),
},
srgb: false, // TODO: use EGL_KHR_gl_colorspace to know that
let desc = unsafe {
PixelFormat {
hardware_accelerated: attrib!(self.display, config_id, ffi::egl::CONFIG_CAVEAT)
!= ffi::egl::SLOW_CONFIG as i32,
color_bits: attrib!(self.display, config_id, ffi::egl::RED_SIZE) as u8
+ attrib!(self.display, config_id, ffi::egl::BLUE_SIZE) as u8
+ attrib!(self.display, config_id, ffi::egl::GREEN_SIZE) as u8,
alpha_bits: attrib!(self.display, config_id, ffi::egl::ALPHA_SIZE) as u8,
depth_bits: attrib!(self.display, config_id, ffi::egl::DEPTH_SIZE) as u8,
stencil_bits: attrib!(self.display, config_id, ffi::egl::STENCIL_SIZE) as u8,
stereoscopy: false,
multisampling: match attrib!(self.display, config_id, ffi::egl::SAMPLES) {
0 | 1 => None,
a => Some(a as u16),
},
srgb: false, // TODO: use EGL_KHR_gl_colorspace to know that
}
};
info!(self.logger, "Selected color format: {:?}", desc);
@ -443,7 +447,7 @@ impl WaylandEGLDisplay {
extensions: &Vec<String>,
) -> Self {
#[cfg(feature = "renderer_gl")]
let gl = gl_ffi::Gles2::load_with(|s| unsafe { get_proc_address(s) as *const _ });
let gl = gl_ffi::Gles2::load_with(|s| get_proc_address(s) as *const _);
Self {
display,

View File

@ -65,10 +65,12 @@ impl ::std::error::Error for EglExtensionNotSupportedError {}
/// Returns the address of an OpenGL function.
///
/// Result is independent of displays and does not guarantee an extension is actually supported at runtime.
pub unsafe fn get_proc_address(symbol: &str) -> *const c_void {
let addr = CString::new(symbol.as_bytes()).unwrap();
let addr = addr.as_ptr();
ffi::egl::GetProcAddress(addr) as *const _
pub fn get_proc_address(symbol: &str) -> *const c_void {
unsafe {
let addr = CString::new(symbol.as_bytes()).unwrap();
let addr = addr.as_ptr();
ffi::egl::GetProcAddress(addr) as *const _
}
}
/// Error that can occur when accessing an EGL buffer

View File

@ -18,11 +18,7 @@ pub trait GLGraphicsBackend {
fn swap_buffers(&self) -> Result<(), SwapBuffersError>;
/// Returns the address of an OpenGL function.
///
/// # Safety
///
/// The context must have been made current before this function is called.
unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void;
fn get_proc_address(&self, symbol: &str) -> *const c_void;
/// Returns the dimensions of the window, or screen, etc in points.
///
@ -51,5 +47,5 @@ pub trait GLGraphicsBackend {
/// and may only be used in combination with the backend. Using this with any
/// other gl context or after the backend was dropped *may* cause undefined behavior.
pub fn load_raw_gl<B: GLGraphicsBackend>(backend: &B) -> Gles2 {
Gles2::load_with(|s| unsafe { backend.get_proc_address(s) as *const _ })
Gles2::load_with(|s| { backend.get_proc_address(s) as *const _ })
}

View File

@ -285,7 +285,7 @@ impl GLGraphicsBackend for WinitGraphicsBackend {
}
}
unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void {
fn get_proc_address(&self, symbol: &str) -> *const c_void {
trace!(self.logger, "Getting symbol for {:?}", symbol);
get_proc_address(symbol)
}