Skip to content

Commit

Permalink
api: move get_proc_address to GlDisplay
Browse files Browse the repository at this point in the history
This should made it clear that you should call this only once
and not on each context creation.
  • Loading branch information
kchibisov committed Sep 8, 2022
1 parent 74eae78 commit 8b3688e
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 57 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- `Config` doesn't force OpenGL `Api` by default.
- `Display::create_context` now uses the most recent available `Api` from the `Config` when `ContextApi` is not specified in `ContextAttributes`.
- `PossiblyCurrentGlContext::get_proc_address` method was moved to `GlDisplay::get_proc_address`.

# Version 0.30.0-beta.2 (2022-09-03)

Expand Down
14 changes: 1 addition & 13 deletions glutin/src/api/cgl/context.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
//! Everything related to `NSOpenGLContext`.

use std::ffi::{self, CStr};
use std::fmt;
use std::marker::PhantomData;
use std::ops::Deref;

use cgl::CGLSetParameter;
use cocoa::appkit::{NSOpenGLContext, NSOpenGLContextParameter};
use cocoa::base::{id, nil};
use core_foundation::base::TCFType;
use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName};
use core_foundation::string::CFString;

use objc::rc::autoreleasepool;
use objc::runtime::{BOOL, NO};

Expand Down Expand Up @@ -158,15 +155,6 @@ impl PossiblyCurrentGlContext for PossiblyCurrentContext {
}
})
}

fn get_proc_address(&self, addr: &CStr) -> *const ffi::c_void {
let symbol_name = CFString::new(addr.to_str().unwrap());
let framework_name = CFString::new("com.apple.opengl");
unsafe {
let framework = CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef());
CFBundleGetFunctionPointerForName(framework, symbol_name.as_concrete_TypeRef()).cast()
}
}
}

impl<T: SurfaceTypeTrait> PossiblyCurrentContextGlSurfaceAccessor<T> for PossiblyCurrentContext {
Expand Down
13 changes: 13 additions & 0 deletions glutin/src/api/cgl/display.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//! A CGL display.

use std::ffi::{self, CStr};
use std::marker::PhantomData;

use core_foundation::base::TCFType;
use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName};
use core_foundation::string::CFString;
use raw_window_handle::RawDisplayHandle;

use crate::config::ConfigTemplate;
Expand Down Expand Up @@ -81,6 +85,15 @@ impl GlDisplay for Display {
) -> Result<Self::PixmapSurface> {
unsafe { Self::create_pixmap_surface(self, config, surface_attributes) }
}

fn get_proc_address(&self, addr: &CStr) -> *const ffi::c_void {
let symbol_name = CFString::new(addr.to_str().unwrap());
let framework_name = CFString::new("com.apple.opengl");
unsafe {
let framework = CFBundleGetBundleWithIdentifier(framework_name.as_concrete_TypeRef());
CFBundleGetFunctionPointerForName(framework, symbol_name.as_concrete_TypeRef()).cast()
}
}
}

impl AsRawDisplay for Display {
Expand Down
5 changes: 0 additions & 5 deletions glutin/src/api/egl/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Everything related to `EGLContext` management.

use std::ffi::{self, CStr};
use std::fmt;
use std::marker::PhantomData;
use std::ops::Deref;
Expand Down Expand Up @@ -228,10 +227,6 @@ impl PossiblyCurrentGlContext for PossiblyCurrentContext {
self.inner.display.inner.egl.GetCurrentContext() == *self.inner.raw
}
}

fn get_proc_address(&self, addr: &CStr) -> *const ffi::c_void {
unsafe { self.inner.display.inner.egl.GetProcAddress(addr.as_ptr()) as *const _ }
}
}

impl<T: SurfaceTypeTrait> PossiblyCurrentContextGlSurfaceAccessor<T> for PossiblyCurrentContext {
Expand Down
6 changes: 5 additions & 1 deletion glutin/src/api/egl/display.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Everything related to `EGLDisplay`.

use std::collections::HashSet;
use std::ffi::CStr;
use std::ffi::{self, CStr};
use std::fmt;
use std::ops::Deref;
use std::sync::Arc;
Expand Down Expand Up @@ -271,6 +271,10 @@ impl GlDisplay for Display {
) -> Result<Self::PixmapSurface> {
unsafe { Self::create_pixmap_surface(self, config, surface_attributes) }
}

fn get_proc_address(&self, addr: &CStr) -> *const ffi::c_void {
unsafe { self.inner.egl.GetProcAddress(addr.as_ptr()) as *const _ }
}
}

impl AsRawDisplay for Display {
Expand Down
7 changes: 0 additions & 7 deletions glutin/src/api/glx/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Everything related to `GLXContext`.

use std::ffi::{self, CStr};
use std::fmt;
use std::marker::PhantomData;
use std::ops::Deref;
Expand Down Expand Up @@ -296,12 +295,6 @@ impl PossiblyCurrentGlContext for PossiblyCurrentContext {
fn is_current(&self) -> bool {
unsafe { self.inner.display.inner.glx.GetCurrentContext() == *self.inner.raw }
}

fn get_proc_address(&self, addr: &CStr) -> *const ffi::c_void {
unsafe {
self.inner.display.inner.glx.GetProcAddress(addr.as_ptr() as *const _) as *const _
}
}
}

impl<T: SurfaceTypeTrait> PossiblyCurrentContextGlSurfaceAccessor<T> for PossiblyCurrentContext {
Expand Down
6 changes: 5 additions & 1 deletion glutin/src/api/glx/display.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! GLX object creation.

use std::collections::HashSet;
use std::ffi::CStr;
use std::ffi::{self, CStr};
use std::fmt;
use std::ops::Deref;
use std::sync::atomic::Ordering;
Expand Down Expand Up @@ -153,6 +153,10 @@ impl GlDisplay for Display {
) -> Result<Self::PixmapSurface> {
unsafe { Self::create_pixmap_surface(self, config, surface_attributes) }
}

fn get_proc_address(&self, addr: &CStr) -> *const ffi::c_void {
unsafe { self.inner.glx.GetProcAddress(addr.as_ptr() as *const _) as *const _ }
}
}

impl AsRawDisplay for Display {
Expand Down
15 changes: 0 additions & 15 deletions glutin/src/api/wgl/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! WGL context handling.

use std::ffi::{self, CStr};
use std::fmt;
use std::io::Error as IoError;
use std::marker::PhantomData;
Expand All @@ -11,7 +10,6 @@ use glutin_wgl_sys::wgl::types::HGLRC;
use glutin_wgl_sys::{wgl, wgl_extra};
use raw_window_handle::RawWindowHandle;
use windows_sys::Win32::Graphics::Gdi::{self as gdi, HDC};
use windows_sys::Win32::System::LibraryLoader as dll_loader;

use crate::config::GetGlConfig;
use crate::context::{
Expand Down Expand Up @@ -293,19 +291,6 @@ impl PossiblyCurrentGlContext for PossiblyCurrentContext {
fn is_current(&self) -> bool {
unsafe { wgl::GetCurrentContext() == *self.inner.raw }
}

fn get_proc_address(&self, addr: &CStr) -> *const ffi::c_void {
unsafe {
let addr = addr.as_ptr();
let fn_ptr = wgl::GetProcAddress(addr);
if !fn_ptr.is_null() {
fn_ptr.cast()
} else {
dll_loader::GetProcAddress(self.inner.display.inner.lib_opengl32, addr.cast())
.map_or(std::ptr::null(), |fn_ptr| fn_ptr as *const _)
}
}
}
}

impl NotCurrentGlContext for NotCurrentContext {
Expand Down
16 changes: 15 additions & 1 deletion glutin/src/api/wgl/display.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! WGL display initialization and extension loading.

use std::collections::HashSet;
use std::ffi::{CStr, OsStr};
use std::ffi::{self, CStr, OsStr};
use std::fmt;
use std::os::windows::ffi::OsStrExt;
use std::sync::Arc;

use glutin_wgl_sys::wgl;
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
use windows_sys::Win32::Foundation::HINSTANCE;
use windows_sys::Win32::Graphics::Gdi::HDC;
Expand Down Expand Up @@ -117,6 +118,19 @@ impl GlDisplay for Display {
) -> Result<Self::PixmapSurface> {
unsafe { Self::create_pixmap_surface(self, config, surface_attributes) }
}

fn get_proc_address(&self, addr: &CStr) -> *const ffi::c_void {
unsafe {
let addr = addr.as_ptr();
let fn_ptr = wgl::GetProcAddress(addr);
if !fn_ptr.is_null() {
fn_ptr.cast()
} else {
dll_loader::GetProcAddress(self.inner.lib_opengl32, addr.cast())
.map_or(std::ptr::null(), |fn_ptr| fn_ptr as *const _)
}
}
}
}

impl AsRawDisplay for Display {
Expand Down
10 changes: 1 addition & 9 deletions glutin/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! OpenGL context creation and initialization.

#![allow(unreachable_patterns)]
use std::ffi::{self, CStr};
use std::ffi::{self};

use raw_window_handle::RawWindowHandle;

Expand Down Expand Up @@ -77,10 +77,6 @@ pub trait PossiblyCurrentGlContext: Sealed {
/// [`Self::NotCurrentContext`] to indicate that the context is a not
/// current to allow sending it to the different thread.
fn make_not_current(self) -> Result<Self::NotCurrentContext>;

/// Returns the address of an OpenGL function. The context must be current
/// when doing so.
fn get_proc_address(&self, addr: &CStr) -> *const ffi::c_void;
}

/// A trait that splits the methods accessing [`crate::surface::Surface`].
Expand Down Expand Up @@ -506,10 +502,6 @@ impl PossiblyCurrentGlContext for PossiblyCurrentContext {
gl_api_dispatch!(self; Self(context) => context.make_not_current()?; as NotCurrentContext),
)
}

fn get_proc_address(&self, addr: &CStr) -> *const ffi::c_void {
gl_api_dispatch!(self; Self(context) => context.get_proc_address(addr))
}
}

impl<T: SurfaceTypeTrait> PossiblyCurrentContextGlSurfaceAccessor<T> for PossiblyCurrentContext {
Expand Down
15 changes: 15 additions & 0 deletions glutin/src/display.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! The OpenGL platform display selection and creation.
#![allow(unreachable_patterns)]

use std::ffi::{self, CStr};
use std::fmt;

use raw_window_handle::RawDisplayHandle;
Expand Down Expand Up @@ -102,6 +103,16 @@ pub trait GlDisplay: Sealed {
config: &Self::Config,
surface_attributes: &SurfaceAttributes<PixmapSurface>,
) -> Result<Self::PixmapSurface>;

/// Returns the address of an OpenGL function.
///
/// # Api-specific
///
/// **WGL:** - The display should be created with [`RawWindowHandle`] or
/// some context should be made current at least once.
///
/// [`RawWindowHandle`]: raw_window_handle::RawWindowHandle
fn get_proc_address(&self, addr: &CStr) -> *const ffi::c_void;
}

/// Get the [`Display`].
Expand Down Expand Up @@ -350,6 +361,10 @@ impl GlDisplay for Display {
_ => unreachable!(),
}
}

fn get_proc_address(&self, addr: &CStr) -> *const ffi::c_void {
gl_api_dispatch!(self; Self(display) => display.get_proc_address(addr))
}
}

impl AsRawDisplay for Display {
Expand Down
10 changes: 5 additions & 5 deletions glutin_examples/examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ fn main() {
// underlying GL platform. See support module on how it's being done.
let gl_display = create_display(raw_display, raw_window_handle);

gl::load_with(|symbol| {
let symbol = CString::new(symbol).unwrap();
gl_display.get_proc_address(symbol.as_c_str()) as *const _
});

// Create the config we'll be used for window. We'll use the native window
// raw-window-handle for it to get the right visual and use proper hdc. Note
// that you can likely use it for other windows using the same config.
Expand All @@ -48,11 +53,6 @@ fn main() {
// Make it current and load symbols.
let gl_context = gl_context.make_current(&gl_window.surface).unwrap();

gl::load_with(|symbol| {
let symbol = CString::new(symbol).unwrap();
gl_context.get_proc_address(symbol.as_c_str()) as *const _
});

// Try setting vsync.
if let Err(res) = gl_window
.surface
Expand Down

0 comments on commit 8b3688e

Please sign in to comment.