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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
use {
std::{
io,
io::prelude::*,
io::BufReader,
},
crate::{
makepad_live_id::*,
makepad_math::*,
makepad_micro_serde::*,
event::Event,
window::CxWindowPool,
event::WindowGeom,
CxOsApi,
texture::{Texture, TextureFormat},
thread::SignalToUI,
os::{
win32_app::{Win32Time},
d3d11::D3d11Cx,
cx_stdin::{HostToStdin, PresentableDraw, StdinToHost, Swapchain},
},
pass::{CxPassParent},
cx_api::CxOsOp,
cx::Cx,
windows::Win32::Foundation::HANDLE,
}
};
#[derive(Default)]
pub(crate) struct StdinWindow{
swapchain: Option<Swapchain<Texture>>,
present_index: usize,
new_frame_being_rendered: Option<PresentableDraw>
}
impl Cx {
pub (crate) fn stdin_handle_repaint(
&mut self,
d3d11_cx: &mut D3d11Cx,
windows: &mut Vec<StdinWindow>,
time: &Win32Time,
) {
let mut passes_todo = Vec::new();
self.compute_pass_repaint_order(&mut passes_todo);
self.repaint_id += 1;
let time_now = time.time_now();
for &pass_id in &passes_todo {
self.passes[pass_id].set_time(time_now as f32);
match self.passes[pass_id].parent.clone() {
CxPassParent::Xr => {}
CxPassParent::Window(window_id) => {
// only render to swapchain if swapchain exists
let window = &mut windows[window_id.id()];
if let Some(swapchain) = &window.swapchain {
// and if GPU is not already rendering something else
if window.new_frame_being_rendered.is_none() {
let current_image = &swapchain.presentable_images[window.present_index];
window.present_index = (window.present_index + 1) % swapchain.presentable_images.len();
// render to swapchain
self.draw_pass_to_texture(pass_id, d3d11_cx, Some(current_image.image.texture_id()));
let dpi_factor = self.passes[pass_id].dpi_factor.unwrap();
let pass_rect = self.get_pass_rect(pass_id, dpi_factor).unwrap();
let future_presentable_draw = PresentableDraw {
window_id: window_id.id(),
target_id: current_image.id,
width: (pass_rect.size.x * dpi_factor) as u32,
height: (pass_rect.size.y * dpi_factor) as u32,
};
// start GPU event query
d3d11_cx.start_querying();
// and inform event_loop to go poll GPU readiness
window.new_frame_being_rendered = Some(future_presentable_draw);
}
}
}
CxPassParent::Pass(_) => {
//let dpi_factor = self.get_delegated_dpi_factor(parent_pass_id);
self.draw_pass_to_texture(pass_id, d3d11_cx, None);
},
CxPassParent::None => {
self.draw_pass_to_texture(pass_id, d3d11_cx, None);
}
}
}
}
pub fn stdin_event_loop(&mut self, d3d11_cx: &mut D3d11Cx) {
let (json_msg_tx, json_msg_rx) = std::sync::mpsc::channel();
{
std::thread::spawn(move || {
let mut reader = BufReader::new(std::io::stdin().lock());
let mut line = String::new();
loop {
line.clear();
if let Ok(0) | Err(_) = reader.read_line(&mut line) {
break;
}
// alright lets put the line in a json parser
match HostToStdin::deserialize_json(&line) {
Ok(msg) => {
if json_msg_tx.send(msg).is_err() {
break;
}
}
Err(err) => {
// we should output a log string
crate::error!("Cant parse stdin-JSON {} {:?}", line, err)
}
}
}
});
}
let _ = io::stdout().write_all(StdinToHost::ReadyToStart.to_json().as_bytes());
let mut stdin_windows:Vec<StdinWindow> = Vec::new();
let time = Win32Time::new();
self.call_event_handler(&Event::Startup);
//let mut previous_tick_time_s: Option<f64> = None;
//let mut previous_elapsed_s = 0f64;
//let mut allow_rendering = true;
while let Ok(msg) = json_msg_rx.recv() {
match msg {
HostToStdin::KeyDown(e) => {
self.call_event_handler(&Event::KeyDown(e));
}
HostToStdin::KeyUp(e) => {
self.call_event_handler(&Event::KeyUp(e));
}
HostToStdin::TextInput(e) => {
self.call_event_handler(&Event::TextInput(e));
}
HostToStdin::MouseDown(e) => {
self.fingers.process_tap_count(
dvec2(e.x, e.y),
e.time
);
let (window_id, pos) = self.windows.window_id_contains(dvec2(e.x, e.y));
let mouse_down_event = e.into_event(window_id, pos);
self.fingers.mouse_down(mouse_down_event.button, window_id);
self.call_event_handler(&Event::MouseDown(mouse_down_event));
}
HostToStdin::MouseMove(e) => {
let (window_id, pos) = if let Some((_, window_id)) = self.fingers.first_mouse_button{
(window_id, self.windows[window_id].window_geom.position)
}
else{
self.windows.window_id_contains(dvec2(e.x, e.y))
};
self.call_event_handler(&Event::MouseMove(e.into_event(window_id, pos)));
self.fingers.cycle_hover_area(live_id!(mouse).into());
self.fingers.switch_captures();
}
HostToStdin::MouseUp(e) => {
let (window_id, pos) = if let Some((_, window_id)) = self.fingers.first_mouse_button{
(window_id, self.windows[window_id].window_geom.position)
}
else{
self.windows.window_id_contains(dvec2(e.x, e.y))
};
let mouse_up_event = e.into_event(window_id, pos);
let button = mouse_up_event.button;
self.call_event_handler(&Event::MouseUp(mouse_up_event));
self.fingers.mouse_up(button);
self.fingers.cycle_hover_area(live_id!(mouse).into());
}
HostToStdin::Scroll(e) => {
let (window_id,pos) = self.windows.window_id_contains(dvec2(e.x, e.y));
self.call_event_handler(&Event::Scroll(e.into_event(window_id, pos)));
}
HostToStdin::WindowGeomChange { dpi_factor, left, top, width, height, window_id } => {
self.windows[CxWindowPool::from_usize(window_id)].window_geom = WindowGeom {
dpi_factor,
position: dvec2(left, top),
inner_size: dvec2(width, height),
..Default::default()
};
self.redraw_all();
}
HostToStdin::Swapchain(new_swapchain) => {
let new_swapchain = new_swapchain.images_map(|pi| {
let handle = HANDLE(pi.image as isize);
let format = TextureFormat::SharedBGRAu8 {
id: pi.id,
width: new_swapchain.alloc_width as usize,
height: new_swapchain.alloc_height as usize,
initial: true,
};
let texture = Texture::new_with_format(self, format);
self.textures[texture.texture_id()].update_from_shared_handle(d3d11_cx, handle);
texture
});
let window_id = new_swapchain.window_id;
let stdin_window = &mut stdin_windows[window_id];
stdin_window.swapchain = Some(new_swapchain);
stdin_window.present_index = 0;
self.redraw_all();
self.stdin_handle_platform_ops(&mut stdin_windows);
}
HostToStdin::Tick => {
// probe current time
//let start_time = ::std::time::SystemTime::now();
// poll the service for updates
// check signals
if SignalToUI::check_and_clear_ui_signal() {
self.handle_media_signals();
self.call_event_handler(&Event::Signal);
}
if SignalToUI::check_and_clear_action_signal() {
self.handle_action_receiver();
}
if self.handle_live_edit() {
self.call_event_handler(&Event::LiveEdit);
self.redraw_all();
}
self.handle_networking_events();
// we should poll our runloop
self.stdin_handle_platform_ops(&mut stdin_windows);
// alright a tick.
// we should now run all the stuff.
if self.new_next_frames.len() != 0 {
self.call_next_frame_event(self.seconds_since_app_start());
}
if self.need_redrawing() {
self.call_draw_event();
self.hlsl_compile_shaders(d3d11_cx);
}
// repaint
self.stdin_handle_repaint(d3d11_cx, &mut stdin_windows, &time);
// only allow rendering if it didn't take too much time last time
//if allow_rendering {
// check if GPU is ready to flip frames
for window in &mut stdin_windows{
if let Some(presentable_draw) = window.new_frame_being_rendered {
while !d3d11_cx.is_gpu_done() {
std::thread::sleep(std::time::Duration::from_millis(3));
}
let _ = io::stdout().write_all(StdinToHost::DrawCompleteAndFlip(presentable_draw).to_json().as_bytes());
window.new_frame_being_rendered = None;
}
}
//}
// probe how long this took
/*
let elapsed_s = (start_time.elapsed().unwrap().as_nanos() as f64) / 1000000000.0;
if let Some(previous_tick_time_s) = previous_tick_time_s {
// calculate time difference as dictated by the ticks
let previous_dtick_s = time - previous_tick_time_s;
// if this time difference is smaller than the elapsed time, disallow rendering
allow_rendering = previous_dtick_s > previous_elapsed_s;
}
// store current tick time and elapsed time
previous_tick_time_s = Some(time);
previous_elapsed_s = elapsed_s;*/
}
}
}
}
fn stdin_handle_platform_ops(
&mut self,
stdin_windows:&mut Vec<StdinWindow>,
) {
while let Some(op) = self.platform_ops.pop() {
match op {
CxOsOp::CreateWindow(window_id) => {
while window_id.id() >= stdin_windows.len(){
stdin_windows.push(StdinWindow::default());
}
//let stdin_window = &mut stdin_windows[window_id.id()];
let window = &mut self.windows[window_id];
window.is_created = true;
let _ = io::stdout().write_all(StdinToHost::CreateWindow{window_id:window_id.id(),kind_id:window.kind_id}.to_json().as_bytes());
// lets set up our render pass target
/* let pass = &mut self.passes[window.main_pass_id.unwrap()];
if let Some(swapchain) = swapchain {
pass.color_textures = vec![CxPassColorTexture {
clear_color: PassClearColor::ClearWith(vec4(1.0, 1.0, 0.0, 1.0)),
//clear_color: PassClearColor::ClearWith(pass.clear_color),
texture: swapchain.presentable_images[present_index].image.clone(),
}];
}*/
},
CxOsOp::SetCursor(cursor) => {
let _ = io::stdout().write_all(StdinToHost::SetCursor(cursor).to_json().as_bytes());
},
_ => ()
/*
CxOsOp::CloseWindow(_window_id) => {},
CxOsOp::MinimizeWindow(_window_id) => {},
CxOsOp::MaximizeWindow(_window_id) => {},
CxOsOp::RestoreWindow(_window_id) => {},
CxOsOp::FullscreenWindow(_window_id) => {},
CxOsOp::NormalizeWindow(_window_id) => {}
CxOsOp::SetTopmost(_window_id, _is_topmost) => {}
CxOsOp::XrStartPresenting(_) => {},
CxOsOp::XrStopPresenting(_) => {},
CxOsOp::ShowTextIME(_area, _pos) => {},
CxOsOp::HideTextIME => {},
CxOsOp::SetCursor(_cursor) => {},
CxOsOp::StartTimer {timer_id, interval, repeats} => {},
CxOsOp::StopTimer(timer_id) => {},
CxOsOp::StartDragging(dragged_item) => {}
CxOsOp::UpdateMenu(menu) => {}*/
}
}
}
}