Skip to content
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

api: move get_proc_address to GlDisplay #1460

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 14 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,15 @@ pub trait GlDisplay: Sealed {
config: &Self::Config,
surface_attributes: &SurfaceAttributes<PixmapSurface>,
) -> Result<Self::PixmapSurface>;

/// Return the address of an OpenGL function.
///
/// # Api-specific
///
/// **WGL:** - To load all the funcitons you must have a current context on
kchibisov marked this conversation as resolved.
Show resolved Hide resolved
/// the calling thread, otherwise only limited set of functions will be
/// loaded.
fn get_proc_address(&self, addr: &CStr) -> *const ffi::c_void;
}

/// Get the [`Display`].
Expand Down Expand Up @@ -350,6 +360,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
5 changes: 4 additions & 1 deletion glutin_examples/examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ fn main() {
// Make it current and load symbols.
let gl_context = gl_context.make_current(&gl_window.surface).unwrap();

// WGL requires current context on the calling thread to load symbols properly,
// so the call here is for portability reasons. In case you don't target WGL
// you can call it right after display creation.
gl::load_with(|symbol| {
let symbol = CString::new(symbol).unwrap();
gl_context.get_proc_address(symbol.as_c_str()) as *const _
gl_display.get_proc_address(symbol.as_c_str()) as *const _
});

// Try setting vsync.
Expand Down