Don't use the borrowing trick under Windows (#89)

This commit is contained in:
John Nunley 2023-04-07 17:31:21 -07:00 committed by GitHub
parent 885c76bf02
commit 92e8eb7d22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,7 +2,7 @@
//! //!
//! This module converts the input buffer into a bitmap and then stretches it to the window. //! This module converts the input buffer into a bitmap and then stretches it to the window.
use crate::{util, SoftBufferError}; use crate::SoftBufferError;
use raw_window_handle::Win32WindowHandle; use raw_window_handle::Win32WindowHandle;
use std::io; use std::io;
@ -104,6 +104,17 @@ impl Buffer {
} }
} }
#[inline]
fn pixels(&self) -> &[u32] {
unsafe {
slice::from_raw_parts(
self.pixels.as_ptr(),
i32::from(self.width) as usize * i32::from(self.height) as usize,
)
}
}
#[inline]
fn pixels_mut(&mut self) -> &mut [u32] { fn pixels_mut(&mut self) -> &mut [u32] {
unsafe { unsafe {
slice::from_raw_parts_mut( slice::from_raw_parts_mut(
@ -122,6 +133,7 @@ pub struct Win32Impl {
/// The device context for the window. /// The device context for the window.
dc: Gdi::HDC, dc: Gdi::HDC,
/// The buffer used to hold the image.
buffer: Option<Buffer>, buffer: Option<Buffer>,
} }
@ -184,31 +196,29 @@ impl Win32Impl {
} }
pub fn buffer_mut(&mut self) -> Result<BufferImpl, SoftBufferError> { pub fn buffer_mut(&mut self) -> Result<BufferImpl, SoftBufferError> {
Ok(BufferImpl(util::BorrowStack::new(self, |surface| { if self.buffer.is_none() {
Ok(surface panic!("Must set size of surface before calling `buffer_mut()`");
.buffer }
.as_mut()
.expect("Must set size of surface before calling `buffer_mut()`") Ok(BufferImpl(self))
.pixels_mut())
})?))
} }
} }
pub struct BufferImpl<'a>(util::BorrowStack<'a, Win32Impl, [u32]>); pub struct BufferImpl<'a>(&'a mut Win32Impl);
impl<'a> BufferImpl<'a> { impl<'a> BufferImpl<'a> {
#[inline] #[inline]
pub fn pixels(&self) -> &[u32] { pub fn pixels(&self) -> &[u32] {
self.0.member() self.0.buffer.as_ref().unwrap().pixels()
} }
#[inline] #[inline]
pub fn pixels_mut(&mut self) -> &mut [u32] { pub fn pixels_mut(&mut self) -> &mut [u32] {
self.0.member_mut() self.0.buffer.as_mut().unwrap().pixels_mut()
} }
pub fn present(self) -> Result<(), SoftBufferError> { pub fn present(self) -> Result<(), SoftBufferError> {
let imp = self.0.into_container(); let imp = self.0;
let buffer = imp.buffer.as_ref().unwrap(); let buffer = imp.buffer.as_ref().unwrap();
unsafe { unsafe {
Gdi::BitBlt( Gdi::BitBlt(