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

build(deps): update windows-sys requirement from 0.52.0 to 0.59.0 #232

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ wayland-sys = { version = "0.31.0", optional = true }
x11rb = { version = "0.13.0", features = ["allow-unsafe-code", "shm"], optional = true }

[target.'cfg(target_os = "windows")'.dependencies.windows-sys]
version = "0.52.0"
version = "0.59.0"
features = ["Win32_Graphics_Gdi", "Win32_UI_Shell", "Win32_UI_WindowsAndMessaging", "Win32_Foundation"]

[target.'cfg(target_os = "macos")'.dependencies]
Expand Down
45 changes: 33 additions & 12 deletions src/backends/win32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Drop for Buffer {
impl Buffer {
fn new(window_dc: Gdi::HDC, width: NonZeroI32, height: NonZeroI32) -> Self {
let dc = Allocator::get().allocate(window_dc);
assert!(dc != 0);
assert!(!dc.is_null());

// Create a new bitmap info struct.
let bitmap_info = BitmapInfo {
Expand Down Expand Up @@ -92,11 +92,11 @@ impl Buffer {
&bitmap_info as *const BitmapInfo as *const _,
Gdi::DIB_RGB_COLORS,
&mut pixels as *mut *mut u32 as _,
0,
ptr::null_mut(),
0,
)
};
assert!(bitmap != 0);
assert!(!bitmap.is_null());
let pixels = NonNull::new(pixels).unwrap();

unsafe {
Expand Down Expand Up @@ -137,10 +137,10 @@ impl Buffer {
/// The handle to a window for software buffering.
pub struct Win32Impl<D: ?Sized, W> {
/// The window handle.
window: HWND,
window: OnlyUsedFromOrigin<HWND>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't HWND and HDC thread-safe (so they could be used from any origin), as you've also realized with unsafe impl Send for Command {}?


/// The device context for the window.
dc: Gdi::HDC,
dc: OnlyUsedFromOrigin<Gdi::HDC>,

/// The buffer used to hold the image.
buffer: Option<Buffer>,
Expand All @@ -159,7 +159,7 @@ pub struct Win32Impl<D: ?Sized, W> {
impl<D: ?Sized, W> Drop for Win32Impl<D, W> {
fn drop(&mut self) {
// Release our resources.
Allocator::get().release(self.window, self.dc);
Allocator::get().release(self.window.0, self.dc.0);
}
}

Expand All @@ -184,11 +184,21 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> Win32Impl<D, W> {
))
})()
.ok_or(SoftBufferError::DamageOutOfRange { rect })?;
Gdi::BitBlt(self.dc, x, y, width, height, buffer.dc, x, y, Gdi::SRCCOPY);
Gdi::BitBlt(
self.dc.0,
x,
y,
width,
height,
buffer.dc,
x,
y,
Gdi::SRCCOPY,
);
}

// Validate the window.
Gdi::ValidateRect(self.window, ptr::null_mut());
Gdi::ValidateRect(self.window.0, ptr::null_mut());
}
buffer.presented = true;

Expand All @@ -214,7 +224,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Win32Im
let dc = Allocator::get().get_dc(hwnd);

// GetDC returns null if there is a platform error.
if dc == 0 {
if dc.is_null() {
return Err(SoftBufferError::PlatformError(
Some("Device Context is null".into()),
Some(Box::new(io::Error::last_os_error())),
Expand All @@ -223,8 +233,8 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Win32Im
}

Ok(Self {
dc,
window: hwnd,
dc: dc.into(),
window: hwnd.into(),
buffer: None,
handle: window,
_display: PhantomData,
Expand All @@ -250,7 +260,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for Win32Im
}
}

self.buffer = Some(Buffer::new(self.dc, width, height));
self.buffer = Some(Buffer::new(self.dc.0, width, height));

Ok(())
}
Expand Down Expand Up @@ -411,6 +421,8 @@ enum Command {
},
}

unsafe impl Send for Command {}

impl Command {
/// Handle this command.
///
Expand Down Expand Up @@ -445,3 +457,12 @@ impl Command {
}
}
}

struct OnlyUsedFromOrigin<T>(T);
unsafe impl<T> Send for OnlyUsedFromOrigin<T> {}

impl<T> From<T> for OnlyUsedFromOrigin<T> {
fn from(t: T) -> Self {
Self(t)
}
}