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() 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) 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); 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 /// Finds a compatible [`EGLConfig`] for a given set of requirements
pub unsafe fn choose_config( pub fn choose_config(
&self, &self,
version: (u8, u8), version: (u8, u8),
reqs: PixelFormatRequirements, reqs: PixelFormatRequirements,
@ -257,19 +257,21 @@ impl<B: native::Backend, N: native::NativeDisplay<B>> EGLDisplay<B, N> {
// calling `eglChooseConfig` // calling `eglChooseConfig`
let mut config_id = MaybeUninit::uninit(); let mut config_id = MaybeUninit::uninit();
let mut num_configs = MaybeUninit::uninit(); let mut num_configs = MaybeUninit::uninit();
if ffi::egl::ChooseConfig( if unsafe {
ffi::egl::ChooseConfig(
*self.display, *self.display,
descriptor.as_ptr(), descriptor.as_ptr(),
config_id.as_mut_ptr(), config_id.as_mut_ptr(),
1, 1,
num_configs.as_mut_ptr(), num_configs.as_mut_ptr(),
) == 0 )
} == 0
{ {
return Err(Error::ConfigFailed); return Err(Error::ConfigFailed);
} }
let config_id = config_id.assume_init(); let config_id = unsafe { config_id.assume_init() };
let num_configs = num_configs.assume_init(); let num_configs = unsafe { num_configs.assume_init() };
if num_configs == 0 { if num_configs == 0 {
error!(self.logger, "No matching color format found"); error!(self.logger, "No matching color format found");
@ -295,7 +297,8 @@ impl<B: native::Backend, N: native::NativeDisplay<B>> EGLDisplay<B, N> {
}}; }};
}; };
let desc = PixelFormat { let desc = unsafe {
PixelFormat {
hardware_accelerated: attrib!(self.display, config_id, ffi::egl::CONFIG_CAVEAT) hardware_accelerated: attrib!(self.display, config_id, ffi::egl::CONFIG_CAVEAT)
!= ffi::egl::SLOW_CONFIG as i32, != ffi::egl::SLOW_CONFIG as i32,
color_bits: attrib!(self.display, config_id, ffi::egl::RED_SIZE) as u8 color_bits: attrib!(self.display, config_id, ffi::egl::RED_SIZE) as u8
@ -310,6 +313,7 @@ impl<B: native::Backend, N: native::NativeDisplay<B>> EGLDisplay<B, N> {
a => Some(a as u16), a => Some(a as u16),
}, },
srgb: false, // TODO: use EGL_KHR_gl_colorspace to know that srgb: false, // TODO: use EGL_KHR_gl_colorspace to know that
}
}; };
info!(self.logger, "Selected color format: {:?}", desc); info!(self.logger, "Selected color format: {:?}", desc);
@ -443,7 +447,7 @@ impl WaylandEGLDisplay {
extensions: &Vec<String>, extensions: &Vec<String>,
) -> Self { ) -> Self {
#[cfg(feature = "renderer_gl")] #[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 { Self {
display, display,

View File

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

View File

@ -18,11 +18,7 @@ pub trait GLGraphicsBackend {
fn swap_buffers(&self) -> Result<(), SwapBuffersError>; fn swap_buffers(&self) -> Result<(), SwapBuffersError>;
/// Returns the address of an OpenGL function. /// Returns the address of an OpenGL function.
/// fn get_proc_address(&self, symbol: &str) -> *const c_void;
/// # Safety
///
/// The context must have been made current before this function is called.
unsafe fn get_proc_address(&self, symbol: &str) -> *const c_void;
/// Returns the dimensions of the window, or screen, etc in points. /// 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 /// 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. /// other gl context or after the backend was dropped *may* cause undefined behavior.
pub fn load_raw_gl<B: GLGraphicsBackend>(backend: &B) -> Gles2 { 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); trace!(self.logger, "Getting symbol for {:?}", symbol);
get_proc_address(symbol) get_proc_address(symbol)
} }