softbuffer/src/win32.rs

128 lines
3.6 KiB
Rust
Raw Normal View History

2022-12-21 17:26:09 -08:00
//! Implementation of software buffering for Windows.
2022-12-22 10:09:47 -08:00
//!
2022-12-21 17:26:09 -08:00
//! This module converts the input buffer into a bitmap and then stretches it to the window.
use crate::SoftBufferError;
use raw_window_handle::Win32WindowHandle;
2022-12-21 17:26:09 -08:00
use std::io;
2022-12-22 10:09:47 -08:00
use std::mem;
use std::os::raw::c_int;
2022-12-20 08:08:46 -08:00
use windows_sys::Win32::Foundation::HWND;
use windows_sys::Win32::Graphics::Gdi::{
2022-12-21 17:26:09 -08:00
GetDC, StretchDIBits, ValidateRect, BITMAPINFOHEADER, BI_BITFIELDS, DIB_RGB_COLORS, HDC,
RGBQUAD, SRCCOPY,
2022-12-20 08:08:46 -08:00
};
2022-12-21 17:26:09 -08:00
/// The handle to a window for software buffering.
2022-01-16 08:59:29 -06:00
pub struct Win32Impl {
2022-12-21 17:26:09 -08:00
/// The window handle.
window: HWND,
2022-12-21 17:26:09 -08:00
/// The device context for the window.
2022-01-16 08:59:29 -06:00
dc: HDC,
}
2022-12-21 17:26:09 -08:00
/// The Win32-compatible bitmap information.
#[repr(C)]
struct BitmapInfo {
pub bmi_header: BITMAPINFOHEADER,
pub bmi_colors: [RGBQUAD; 3],
}
impl Win32Impl {
2022-12-21 17:26:09 -08:00
/// Create a new `Win32Impl` from a `Win32WindowHandle`.
2022-12-22 10:09:47 -08:00
///
2022-12-21 17:26:09 -08:00
/// # Safety
2022-12-22 10:09:47 -08:00
///
2022-12-21 17:26:09 -08:00
/// The `Win32WindowHandle` must be a valid window handle.
pub unsafe fn new(handle: &Win32WindowHandle) -> Result<Self, crate::SoftBufferError> {
2022-12-21 17:26:09 -08:00
// It is valid for the window handle to be null here. Error out if it is.
if handle.hwnd.is_null() {
return Err(SoftBufferError::IncompleteWindowHandle);
2022-12-21 17:26:09 -08:00
}
// Get the handle to the device context.
// SAFETY: We have confirmed that the window handle is valid.
let hwnd = handle.hwnd as HWND;
2022-12-23 04:19:41 +01:00
let dc = unsafe { GetDC(hwnd) };
2022-01-16 08:59:29 -06:00
2022-12-21 17:26:09 -08:00
// GetDC returns null if there is a platform error.
2022-12-20 08:08:46 -08:00
if dc == 0 {
return Err(SoftBufferError::PlatformError(
2022-12-21 17:26:09 -08:00
Some("Device Context is null".into()),
Some(Box::new(io::Error::last_os_error())),
));
}
2022-12-22 10:09:47 -08:00
Ok(Self { dc, window: hwnd })
2022-01-16 08:59:29 -06:00
}
2022-12-22 10:09:47 -08:00
pub(crate) unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
2022-12-21 17:26:09 -08:00
// Create a new bitmap info struct.
let bmi_header = BITMAPINFOHEADER {
biSize: mem::size_of::<BITMAPINFOHEADER>() as u32,
biWidth: width as i32,
biHeight: -(height as i32),
biPlanes: 1,
biBitCount: 32,
biCompression: BI_BITFIELDS,
biSizeImage: 0,
biXPelsPerMeter: 0,
biYPelsPerMeter: 0,
biClrUsed: 0,
biClrImportant: 0,
};
let zero_quad = RGBQUAD {
rgbBlue: 0,
rgbGreen: 0,
rgbRed: 0,
rgbReserved: 0,
};
let bmi_colors = [
RGBQUAD {
rgbRed: 0xff,
..zero_quad
},
RGBQUAD {
rgbGreen: 0xff,
..zero_quad
},
RGBQUAD {
rgbBlue: 0xff,
..zero_quad
},
];
let bitmap_info = BitmapInfo {
bmi_header,
bmi_colors,
};
2022-12-21 17:26:09 -08:00
// Stretch the bitmap onto the window.
// SAFETY:
// - The bitmap information is valid.
// - The buffer is a valid pointer to image data.
2022-12-23 04:19:41 +01:00
unsafe {
StretchDIBits(
self.dc,
0,
0,
width as c_int,
height as c_int,
0,
0,
width as c_int,
height as c_int,
buffer.as_ptr().cast(),
&bitmap_info as *const BitmapInfo as *const _,
DIB_RGB_COLORS,
SRCCOPY,
)
};
2022-12-21 17:26:09 -08:00
// Validate the window.
2022-12-23 04:19:41 +01:00
unsafe { ValidateRect(self.window, std::ptr::null_mut()) };
}
2022-01-16 08:59:29 -06:00
}