2022-12-21 17:26:09 -08:00
|
|
|
//! Implementation of software buffering for Windows.
|
|
|
|
|
//!
|
|
|
|
|
//! This module converts the input buffer into a bitmap and then stretches it to the window.
|
|
|
|
|
|
2022-12-20 07:10:11 -07:00
|
|
|
use crate::{GraphicsContextImpl, SwBufError};
|
2022-12-20 08:43:26 -07:00
|
|
|
use raw_window_handle::Win32WindowHandle;
|
2022-12-21 17:26:09 -08:00
|
|
|
|
|
|
|
|
use std::mem;
|
|
|
|
|
use std::io;
|
2022-01-16 08:03:20 -06:00
|
|
|
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-01-16 08:03:20 -06: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.
|
2022-01-16 08:03:20 -06:00
|
|
|
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-01-16 08:03:20 -06:00
|
|
|
}
|
|
|
|
|
|
2022-12-21 17:26:09 -08:00
|
|
|
/// The Win32-compatible bitmap information.
|
2022-01-16 08:03:20 -06:00
|
|
|
#[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`.
|
|
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// The `Win32WindowHandle` must be a valid window handle.
|
2022-12-20 08:43:26 -07:00
|
|
|
pub unsafe fn new(handle: &Win32WindowHandle) -> Result<Self, crate::SwBufError> {
|
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(SwBufError::IncompleteWindowHandle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the handle to the device context.
|
|
|
|
|
// SAFETY: We have confirmed that the window handle is valid.
|
|
|
|
|
let hwnd = handle.hwnd as HWND;
|
|
|
|
|
let dc = 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 {
|
2022-12-21 17:26:09 -08:00
|
|
|
return Err(SwBufError::PlatformError(
|
|
|
|
|
Some("Device Context is null".into()),
|
|
|
|
|
Some(Box::new(io::Error::last_os_error())),
|
|
|
|
|
));
|
2022-01-16 08:03:20 -06:00
|
|
|
}
|
|
|
|
|
|
2022-12-21 17:26:09 -08:00
|
|
|
Ok(Self {
|
|
|
|
|
dc,
|
|
|
|
|
window: hwnd,
|
|
|
|
|
})
|
2022-01-16 08:59:29 -06:00
|
|
|
}
|
2022-01-16 08:03:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl GraphicsContextImpl for Win32Impl {
|
|
|
|
|
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 mut bitmap_info: BitmapInfo =mem::zeroed();
|
2022-01-16 08:03:20 -06:00
|
|
|
|
2022-12-21 17:26:09 -08:00
|
|
|
bitmap_info.bmi_header.biSize = mem::size_of::<BITMAPINFOHEADER>() as u32;
|
2022-01-16 08:03:20 -06:00
|
|
|
bitmap_info.bmi_header.biPlanes = 1;
|
|
|
|
|
bitmap_info.bmi_header.biBitCount = 32;
|
|
|
|
|
bitmap_info.bmi_header.biCompression = BI_BITFIELDS;
|
|
|
|
|
bitmap_info.bmi_header.biWidth = width as i32;
|
|
|
|
|
bitmap_info.bmi_header.biHeight = -(height as i32);
|
|
|
|
|
bitmap_info.bmi_colors[0].rgbRed = 0xff;
|
|
|
|
|
bitmap_info.bmi_colors[1].rgbGreen = 0xff;
|
|
|
|
|
bitmap_info.bmi_colors[2].rgbBlue = 0xff;
|
|
|
|
|
|
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-01-16 08:03:20 -06:00
|
|
|
StretchDIBits(
|
|
|
|
|
self.dc,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
width as c_int,
|
|
|
|
|
height as c_int,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
width as c_int,
|
|
|
|
|
height as c_int,
|
2022-12-21 17:26:09 -08:00
|
|
|
buffer.as_ptr().cast(),
|
|
|
|
|
&bitmap_info as *const BitmapInfo as *const _,
|
2022-12-20 08:08:46 -08:00
|
|
|
DIB_RGB_COLORS,
|
|
|
|
|
SRCCOPY,
|
2022-01-16 08:03:20 -06:00
|
|
|
);
|
|
|
|
|
|
2022-12-21 17:26:09 -08:00
|
|
|
// Validate the window.
|
2022-01-16 08:03:20 -06:00
|
|
|
ValidateRect(self.window, std::ptr::null_mut());
|
|
|
|
|
}
|
2022-01-16 08:59:29 -06:00
|
|
|
}
|