//! 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::() 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(()) }