Owned pixel buffer for no-copy presentation

This is based on the API that will be used for no-copy presentation. But
wraps it in `set_buffer`.

This also fixes the Wayland buffer code to set `self.width` and
`self.height` on resize, and set the length of the shared memory file
when the buffer is created.

Co-authored-by: jtnunley <jtnunley01@gmail.com>
This commit is contained in:
Ian Douglas Scott 2023-04-06 00:30:59 -07:00 committed by GitHub
parent e5d546ff9e
commit a09e4cf679
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 1176 additions and 438 deletions

View file

@ -2,33 +2,134 @@
//!
//! This module converts the input buffer into a bitmap and then stretches it to the window.
use crate::SoftBufferError;
use crate::{util, SoftBufferError};
use raw_window_handle::Win32WindowHandle;
use std::io;
use std::mem;
use std::os::raw::c_int;
use std::num::{NonZeroI32, NonZeroU32};
use std::ptr::{self, NonNull};
use std::slice;
use windows_sys::Win32::Foundation::HWND;
use windows_sys::Win32::Graphics::Gdi::{
GetDC, StretchDIBits, ValidateRect, BITMAPINFOHEADER, BI_BITFIELDS, DIB_RGB_COLORS, HDC,
RGBQUAD, SRCCOPY,
use windows_sys::Win32::Graphics::Gdi;
const ZERO_QUAD: Gdi::RGBQUAD = Gdi::RGBQUAD {
rgbBlue: 0,
rgbGreen: 0,
rgbRed: 0,
rgbReserved: 0,
};
struct Buffer {
dc: Gdi::HDC,
bitmap: Gdi::HBITMAP,
pixels: NonNull<u32>,
width: NonZeroI32,
height: NonZeroI32,
}
impl Drop for Buffer {
fn drop(&mut self) {
unsafe {
Gdi::DeleteDC(self.dc);
Gdi::DeleteObject(self.bitmap);
}
}
}
impl Buffer {
fn new(window_dc: Gdi::HDC, width: NonZeroI32, height: NonZeroI32) -> Self {
let dc = unsafe { Gdi::CreateCompatibleDC(window_dc) };
assert!(dc != 0);
// Create a new bitmap info struct.
let bitmap_info = BitmapInfo {
bmi_header: Gdi::BITMAPINFOHEADER {
biSize: mem::size_of::<Gdi::BITMAPINFOHEADER>() as u32,
biWidth: width.get(),
biHeight: -height.get(),
biPlanes: 1,
biBitCount: 32,
biCompression: Gdi::BI_BITFIELDS,
biSizeImage: 0,
biXPelsPerMeter: 0,
biYPelsPerMeter: 0,
biClrUsed: 0,
biClrImportant: 0,
},
bmi_colors: [
Gdi::RGBQUAD {
rgbRed: 0xff,
..ZERO_QUAD
},
Gdi::RGBQUAD {
rgbGreen: 0xff,
..ZERO_QUAD
},
Gdi::RGBQUAD {
rgbBlue: 0xff,
..ZERO_QUAD
},
],
};
// XXX alignment?
// XXX better to use CreateFileMapping, and pass hSection?
// XXX test return value?
let mut pixels: *mut u32 = ptr::null_mut();
let bitmap = unsafe {
Gdi::CreateDIBSection(
dc,
&bitmap_info as *const BitmapInfo as *const _,
Gdi::DIB_RGB_COLORS,
&mut pixels as *mut *mut u32 as _,
0,
0,
)
};
assert!(bitmap != 0);
let pixels = NonNull::new(pixels).unwrap();
unsafe {
Gdi::SelectObject(dc, bitmap);
}
Self {
dc,
bitmap,
width,
height,
pixels,
}
}
fn pixels_mut(&mut self) -> &mut [u32] {
unsafe {
slice::from_raw_parts_mut(
self.pixels.as_ptr(),
i32::from(self.width) as usize * i32::from(self.height) as usize,
)
}
}
}
/// The handle to a window for software buffering.
pub struct Win32Impl {
/// The window handle.
window: HWND,
/// The device context for the window.
dc: HDC,
dc: Gdi::HDC,
buffer: Option<Buffer>,
}
/// The Win32-compatible bitmap information.
#[repr(C)]
struct BitmapInfo {
pub bmi_header: BITMAPINFOHEADER,
pub bmi_colors: [RGBQUAD; 3],
bmi_header: Gdi::BITMAPINFOHEADER,
bmi_colors: [Gdi::RGBQUAD; 3],
}
impl Win32Impl {
@ -46,7 +147,7 @@ impl Win32Impl {
// 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 = unsafe { GetDC(hwnd) };
let dc = unsafe { Gdi::GetDC(hwnd) };
// GetDC returns null if there is a platform error.
if dc == 0 {
@ -56,72 +157,76 @@ impl Win32Impl {
));
}
Ok(Self { dc, window: hwnd })
Ok(Self {
dc,
window: hwnd,
buffer: None,
})
}
pub(crate) unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
// 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,
};
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
let (width, height) = (|| {
let width = NonZeroI32::new(i32::try_from(width.get()).ok()?)?;
let height = NonZeroI32::new(i32::try_from(height.get()).ok()?)?;
Some((width, height))
})()
.ok_or(SoftBufferError::SizeOutOfRange { width, height })?;
// Stretch the bitmap onto the window.
// SAFETY:
// - The bitmap information is valid.
// - The buffer is a valid pointer to image data.
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,
)
};
if let Some(buffer) = self.buffer.as_ref() {
if buffer.width == width && buffer.height == height {
return Ok(());
}
}
// Validate the window.
unsafe { ValidateRect(self.window, std::ptr::null_mut()) };
self.buffer = Some(Buffer::new(self.dc, width, height));
Ok(())
}
pub fn buffer_mut(&mut self) -> Result<BufferImpl, SoftBufferError> {
Ok(BufferImpl(util::BorrowStack::new(self, |surface| {
Ok(surface
.buffer
.as_mut()
.expect("Must set size of surface before calling `buffer_mut()`")
.pixels_mut())
})?))
}
}
pub struct BufferImpl<'a>(util::BorrowStack<'a, Win32Impl, [u32]>);
impl<'a> BufferImpl<'a> {
#[inline]
pub fn pixels(&self) -> &[u32] {
self.0.member()
}
#[inline]
pub fn pixels_mut(&mut self) -> &mut [u32] {
self.0.member_mut()
}
pub fn present(self) -> Result<(), SoftBufferError> {
let imp = self.0.into_container();
let buffer = imp.buffer.as_ref().unwrap();
unsafe {
Gdi::BitBlt(
imp.dc,
0,
0,
buffer.width.get(),
buffer.height.get(),
buffer.dc,
0,
0,
Gdi::SRCCOPY,
);
// Validate the window.
Gdi::ValidateRect(imp.window, ptr::null_mut());
}
Ok(())
}
}