1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
//! Paints the loading frame into the window that will host the Vulkan surface.
use raw_window_handle::{HasWindowHandle, RawWindowHandle};
use std::{io, mem};
use windows_sys::Win32::{
Foundation::{HWND, RECT},
Graphics::Gdi::{
GetDC, ReleaseDC, StretchDIBits, BITMAPINFO, BITMAPINFOHEADER, BI_RGB, DIB_RGB_COLORS,
GDI_ERROR, SRCCOPY,
},
UI::WindowsAndMessaging::GetClientRect,
};
use winit::window::Window;
/// Paints a top-down 0x00RRGGBB bitmap into the current client area.
///
/// Call from the window thread before starting Vulkan presentation.
pub fn paint(window: &Window, extent: [u32; 2], pixels: &[u32]) -> io::Result<()> {
let [width, height] = extent;
if width == 0
|| height == 0
|| width > i32::MAX as u32
|| height > i32::MAX as u32
|| (width as usize).checked_mul(height as usize) != Some(pixels.len())
{
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"invalid startup bitmap dimensions",
));
}
let handle = window.window_handle().map_err(io::Error::other)?;
let RawWindowHandle::Win32(raw) = handle.as_raw() else {
return Err(io::Error::other("startup drawing requires a Win32 window"));
};
let hwnd = raw.hwnd.get() as HWND;
// SAFETY: The borrowed window owns the HWND. The DC is acquired and released
// on this thread, and the validated bitmap stays alive while GDI reads it.
unsafe {
let mut rect = RECT::default();
if GetClientRect(hwnd, &mut rect) == 0 {
return Err(io::Error::last_os_error());
}
if rect.right == 0 || rect.bottom == 0 {
return Ok(());
}
let dc = GetDC(hwnd);
if dc.is_null() {
return Err(io::Error::last_os_error());
}
let mut info: BITMAPINFO = mem::zeroed();
info.bmiHeader = BITMAPINFOHEADER {
biSize: mem::size_of::<BITMAPINFOHEADER>() as u32,
biWidth: width as i32,
biHeight: -(height as i32),
biPlanes: 1,
biBitCount: 32,
biCompression: BI_RGB,
..mem::zeroed()
};
let result = StretchDIBits(
dc,
0,
0,
rect.right,
rect.bottom,
0,
0,
width as i32,
height as i32,
pixels.as_ptr().cast(),
&info,
DIB_RGB_COLORS,
SRCCOPY,
);
ReleaseDC(hwnd, dc);
if result == GDI_ERROR {
return Err(io::Error::other("GDI could not present the startup bitmap"));
}
}
Ok(())
}
|