-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathwinit_multithread.rs
More file actions
146 lines (129 loc) · 5.34 KB
/
winit_multithread.rs
File metadata and controls
146 lines (129 loc) · 5.34 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//! `Surface` implements `Send`. This makes sure that multithreading can work here.
#[cfg(not(target_family = "wasm"))]
#[path = "utils/winit_app.rs"]
mod winit_app;
#[cfg(not(target_family = "wasm"))]
pub mod ex {
use std::num::NonZeroU32;
use std::sync::{mpsc, Arc, Mutex};
use winit::event::{Event, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
use winit::window::Window;
use super::winit_app;
type Surface = softbuffer::Surface<Arc<Window>, Arc<Window>>;
fn render_thread(
window: Arc<Window>,
do_render: mpsc::Receiver<Arc<Mutex<Surface>>>,
done: mpsc::Sender<()>,
) {
loop {
println!("waiting for render...");
let Ok(surface) = do_render.recv() else {
println!("main thread destroyed");
break;
};
// Perform the rendering.
let mut surface = surface.lock().unwrap();
if let (Some(width), Some(height)) = {
let size = window.inner_size();
println!("got size: {size:?}");
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
} {
println!("resizing...");
surface.resize(width, height).unwrap();
let mut buffer = surface.buffer_mut().unwrap();
for y in 0..buffer.height() {
for x in 0..buffer.width() {
let red = x as u32 % 255;
let green = y as u32 % 255;
let blue = (x as u32 * y as u32) % 255;
let index = y * buffer.width() + x;
buffer[index] = blue | (green << 8) | (red << 16);
}
}
println!("presenting...");
buffer.present().unwrap();
}
// We're done, tell the main thread to keep going.
done.send(()).ok();
}
}
pub fn entry(event_loop: EventLoop<()>) {
let app = winit_app::WinitAppBuilder::with_init(
|elwt| {
let attributes = Window::default_attributes();
#[cfg(target_arch = "wasm32")]
let attributes =
winit::platform::web::WindowAttributesExtWebSys::with_append(attributes, true);
let window = Arc::new(elwt.create_window(attributes).unwrap());
let context = softbuffer::Context::new(window.clone()).unwrap();
// Spawn a thread to handle rendering for this specific surface. The channels will
// be closed and the thread will be stopped whenever this surface (the returned
// context below) is dropped, so that it can all be recreated again (on Android)
// when a new surface is created.
let (start_render, do_render) = mpsc::channel();
let (render_done, finish_render) = mpsc::channel();
println!("starting thread...");
std::thread::spawn({
let window = window.clone();
move || render_thread(window, do_render, render_done)
});
(window, context, start_render, finish_render)
},
|_elwt, (window, context, _start_render, _finish_render)| {
println!("making surface...");
Arc::new(Mutex::new(
softbuffer::Surface::new(context, window.clone()).unwrap(),
))
},
)
.with_event_handler(|state, surface, event, elwt| {
let (window, _context, start_render, finish_render) = state;
elwt.set_control_flow(ControlFlow::Wait);
match event {
Event::WindowEvent {
window_id,
event: WindowEvent::RedrawRequested,
} if window_id == window.id() => {
let Some(surface) = surface else {
eprintln!("RedrawRequested fired before Resumed or after Suspended");
return;
};
// Start the render and then finish it.
start_render.send(surface.clone()).unwrap();
finish_render.recv().unwrap();
}
Event::WindowEvent {
event:
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
},
window_id,
} if window_id == window.id() => {
elwt.exit();
}
_ => {}
}
});
winit_app::run_app(event_loop, app);
}
}
#[cfg(target_family = "wasm")]
mod ex {
use winit::event_loop::EventLoop;
pub(crate) fn entry(_event_loop: EventLoop<()>) {
eprintln!("winit_multithreaded doesn't work on WASM");
}
}
#[cfg(not(target_os = "android"))]
fn main() {
use winit::event_loop::EventLoop;
ex::entry(EventLoop::new().unwrap())
}