Skip to content

Commit

Permalink
wgl: add GLUTIN_WGL_OPENGL_DLL
Browse files Browse the repository at this point in the history
Fixes #1671.
  • Loading branch information
kchibisov authored Jun 6, 2024
1 parent 6af4c60 commit 61ce695
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Bump MSRV from `1.65` to `1.70`.
- Bump `windows-sys` from `0.48.0` to `0.52.0`.
- Expose `Egl` and `Glx` raw API functions on `Egl` and `Glx` displays.
- Add `GLUTIN_WGL_OPENGL_DLL` environment variable to change OpenGL provider name with WGL.

# Version 0.31.3

Expand Down
15 changes: 12 additions & 3 deletions glutin/src/api/wgl/display.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! WGL display initialization and extension loading.
use std::borrow::Cow;
use std::collections::HashSet;
use std::ffi::{self, CStr, OsStr};
use std::fmt;
use std::os::windows::ffi::OsStrExt;
use std::sync::Arc;
use std::{env, fmt};

use glutin_wgl_sys::wgl;
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
Expand All @@ -24,6 +25,9 @@ use super::context::NotCurrentContext;
use super::surface::Surface;
use super::WglExtra;

/// The name of the OpenGL DLL to use.
pub(crate) static GLUTIN_WGL_OPENGL_DLL_ENV: &str = "GLUTIN_WGL_OPENGL_DLL";

/// A WGL display.
#[derive(Debug, Clone)]
pub struct Display {
Expand All @@ -37,6 +41,9 @@ impl Display {
/// passed the OpenGL will be limited to what it can do, though, basic
/// operations could still be performed.
///
/// You can alter OpenGL DLL name by setting `GLUTIN_WGL_OPENGL_DLL`
/// environment variable. The default one is `opengl32.dll`.
///
/// # Safety
///
/// The `native_window` must point to the valid platform window and have
Expand All @@ -49,8 +56,10 @@ impl Display {
return Err(ErrorKind::NotSupported("provided native display is not supported").into());
}

let name =
OsStr::new("opengl32.dll").encode_wide().chain(Some(0).into_iter()).collect::<Vec<_>>();
let dll_name: Cow<'_, str> = env::var(GLUTIN_WGL_OPENGL_DLL_ENV)
.map(Into::into)
.unwrap_or_else(|_| Cow::Borrowed("opengl32.dll"));
let name = OsStr::new(dll_name.as_ref()).encode_wide().chain(Some(0)).collect::<Vec<_>>();
let lib_opengl32 = unsafe { dll_loader::LoadLibraryW(name.as_ptr()) };
if lib_opengl32 == 0 {
return Err(ErrorKind::NotFound.into());
Expand Down
6 changes: 2 additions & 4 deletions glutin/src/api/wgl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ unsafe fn load_extra_functions(
class
};

let class_name =
OsStr::new("WglDummy Window").encode_wide().chain(Some(0).into_iter()).collect::<Vec<_>>();
let class_name = OsStr::new("WglDummy Window").encode_wide().chain(Some(0)).collect::<Vec<_>>();

class.cbSize = mem::size_of::<WNDCLASSEXW>() as _;
class.lpszClassName = class_name.as_ptr();
Expand All @@ -89,8 +88,7 @@ unsafe fn load_extra_functions(

// This dummy window should match the real one enough to get the same OpenGL
// driver.
let title =
OsStr::new("dummy window").encode_wide().chain(Some(0).into_iter()).collect::<Vec<_>>();
let title = OsStr::new("dummy window").encode_wide().chain(Some(0)).collect::<Vec<_>>();

let ex_style = wm::WS_EX_APPWINDOW;
let style = wm::WS_POPUP | wm::WS_CLIPSIBLINGS | wm::WS_CLIPCHILDREN;
Expand Down
4 changes: 4 additions & 0 deletions glutin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
//! The initialization starts by loading and connecting to the platform's
//! graphics Api when creating a [`display`]. This object is used to create all
//! the OpenGL objects, such as [`config`], [`context`], and [`surface`].
//!
//! ## Environment variables
//!
//! `GLUTIN_WGL_OPENGL_DLL` - change the name of the OpenGL DLL to load.
#![deny(rust_2018_idioms)]
#![deny(rustdoc::broken_intra_doc_links)]
Expand Down

0 comments on commit 61ce695

Please sign in to comment.