Skip to content

Commit

Permalink
wgl: reduce calls to GetDC
Browse files Browse the repository at this point in the history
Frequent calls to `GetDC` slows down `swap_buffers` over time. 
This commit creates HDC for the `Surface` and uses that in
all operations with a latter release on `Surface` drop.
  • Loading branch information
baehny authored Oct 29, 2022
1 parent befb764 commit 2a2a972
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- On WGL, fixed that `Surface::swap_buffers` takes longer with every call caused by frequent calls of the win32 function `HDC GetDC(HWND hWnd)`.

# Version 0.30.0

- **This version of `glutin` has been rewritten from the ground and no longer depends on `winit`, the `raw-window-handle` is now used instead of it.**
Expand Down
3 changes: 1 addition & 2 deletions glutin/src/api/wgl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ impl ContextInner {

fn make_current<T: SurfaceTypeTrait>(&self, surface: &Surface<T>) -> Result<()> {
unsafe {
let hdc = gdi::GetDC(surface.hwnd);
if wgl::MakeCurrent(hdc as _, self.raw.cast()) == 0 {
if wgl::MakeCurrent(surface.hdc as _, self.raw.cast()) == 0 {
Err(IoError::last_os_error().into())
} else {
Ok(())
Expand Down
14 changes: 10 additions & 4 deletions glutin/src/api/wgl/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::num::NonZeroU32;

use raw_window_handle::RawWindowHandle;
use windows_sys::Win32::Foundation::HWND;
use windows_sys::Win32::Graphics::Gdi::HDC;
use windows_sys::Win32::Graphics::{Gdi as gdi, OpenGL as gl};

use crate::config::GetGlConfig;
Expand Down Expand Up @@ -57,8 +58,10 @@ impl Display {
},
};

let hdc = unsafe { gdi::GetDC(hwnd) };

let surface =
Surface { display: self.clone(), config: config.clone(), hwnd, _ty: PhantomData };
Surface { display: self.clone(), config: config.clone(), hwnd, hdc, _ty: PhantomData };

Ok(surface)
}
Expand All @@ -69,12 +72,15 @@ pub struct Surface<T: SurfaceTypeTrait> {
display: Display,
config: Config,
pub(crate) hwnd: HWND,
pub(crate) hdc: HDC,
_ty: PhantomData<T>,
}

impl<T: SurfaceTypeTrait> Drop for Surface<T> {
fn drop(&mut self) {
// This line intentionally left blank.
unsafe {
gdi::ReleaseDC(self.hwnd, self.hdc);
}
}
}

Expand All @@ -100,8 +106,7 @@ impl<T: SurfaceTypeTrait> GlSurface<T> for Surface<T> {

fn swap_buffers(&self, _context: &Self::Context) -> Result<()> {
unsafe {
let hdc = gdi::GetDC(self.hwnd);
if gl::SwapBuffers(hdc) == 0 {
if gl::SwapBuffers(self.hdc) == 0 {
Err(IoError::last_os_error().into())
} else {
Ok(())
Expand Down Expand Up @@ -155,6 +160,7 @@ impl<T: SurfaceTypeTrait> fmt::Debug for Surface<T> {
f.debug_struct("Surface")
.field("config", &self.config.inner.pixel_format_index)
.field("hwnd", &self.hwnd)
.field("hdc", &self.hdc)
.finish()
}
}
Expand Down

0 comments on commit 2a2a972

Please sign in to comment.