fix cloning

This commit is contained in:
Alex Janka 2023-05-04 13:04:21 +10:00
parent f02d45f563
commit 48785ffb7a
2 changed files with 18 additions and 19 deletions

View file

@ -23,17 +23,17 @@ use winit_input_helper::WinitInputHelper;
pub struct WindowInfo { pub struct WindowInfo {
id: WindowId, id: WindowId,
data: Arc<Mutex<WindowData>>, data: Arc<WindowData>,
} }
pub struct WindowData { pub struct WindowData {
scaled_buf: Vec<[u8; 4]>, scaled_buf: Mutex<Vec<[u8; 4]>>,
pixels: Pixels, pixels: Mutex<Pixels>,
} }
pub struct WindowManager { pub struct WindowManager {
event_loop: EventLoop<()>, event_loop: EventLoop<()>,
windows: HashMap<WindowId, Arc<Mutex<WindowData>>>, windows: HashMap<WindowId, Arc<WindowData>>,
input: Arc<Mutex<WinitInputHelper>>, input: Arc<Mutex<WinitInputHelper>>,
} }
@ -76,14 +76,13 @@ impl WindowManager {
} }
Event::RedrawRequested(window_id) => { Event::RedrawRequested(window_id) => {
if let Some(w) = self.windows.get(&window_id) { if let Some(w) = self.windows.get(&window_id) {
if let Ok(mut w) = w.lock() { if let Ok(scaled_buf) = w.scaled_buf.lock() && let Ok(mut pixels) = w.pixels.lock() {
let scaled = w.scaled_buf.clone();
for (pixel, source) in for (pixel, source) in
w.pixels.frame_mut().chunks_exact_mut(4).zip(&scaled) pixels.frame_mut().chunks_exact_mut(4).zip(scaled_buf.iter())
{ {
pixel.copy_from_slice(source); pixel.copy_from_slice(source);
} }
w.pixels.render().unwrap(); pixels.render().unwrap();
} }
} }
} }
@ -96,7 +95,7 @@ impl WindowManager {
pub struct WindowRenderer { pub struct WindowRenderer {
window: Window, window: Window,
input: Arc<Mutex<WinitInputHelper>>, input: Arc<Mutex<WinitInputHelper>>,
data: Arc<Mutex<WindowData>>, data: Arc<WindowData>,
width: usize, width: usize,
height: usize, height: usize,
factor: usize, factor: usize,
@ -133,10 +132,10 @@ impl WindowRenderer {
.unwrap() .unwrap()
}; };
let data = Arc::new(Mutex::new(WindowData { let data = Arc::new(WindowData {
scaled_buf: Vec::new(), scaled_buf: Mutex::new(Vec::new()),
pixels, pixels: Mutex::new(pixels),
})); });
let info = WindowInfo { let info = WindowInfo {
id: window.id(), id: window.id(),
data: data.clone(), data: data.clone(),
@ -170,24 +169,24 @@ impl Renderer<[u8; 4]> for WindowRenderer {
let h = (height * self.real_factor) as u32; let h = (height * self.real_factor) as u32;
self.window.set_inner_size(PhysicalSize::new(w, h)); self.window.set_inner_size(PhysicalSize::new(w, h));
if let Ok(mut data) = self.data.lock() { if let Ok(mut scaled_buf) = self.data.scaled_buf.lock() && let Ok(mut pixels) = self.data.pixels.lock() {
data.pixels = { *pixels = {
let window_size = self.window.inner_size(); let window_size = self.window.inner_size();
let surface_texture = let surface_texture =
SurfaceTexture::new(window_size.width, window_size.height, &self.window); SurfaceTexture::new(window_size.width, window_size.height, &self.window);
Pixels::new(window_size.width, window_size.height, surface_texture).unwrap() Pixels::new(window_size.width, window_size.height, surface_texture).unwrap()
}; };
data.scaled_buf.resize((w * h) as usize, [0; 4]); scaled_buf.resize((w * h) as usize, [0; 4]);
} }
self.window.request_redraw(); self.window.request_redraw();
} }
fn display(&mut self, buffer: &[[u8; 4]]) { fn display(&mut self, buffer: &[[u8; 4]]) {
if let Ok(mut data) = self.data.lock() { if let Ok(mut scaled_buf) = self.data.scaled_buf.lock() {
scale_buffer_in_place( scale_buffer_in_place(
buffer, buffer,
&mut data.scaled_buf, &mut scaled_buf,
self.width, self.width,
self.height, self.height,
self.real_factor, self.real_factor,

View file

@ -1,4 +1,4 @@
#![feature(exclusive_range_pattern, let_chains, bigint_helper_methods, step_trait)] #![feature(exclusive_range_pattern, let_chains, bigint_helper_methods)]
use crate::{ use crate::{
processor::{memory::Memory, Flags}, processor::{memory::Memory, Flags},