|
2 | 2 | //! provides a window and a render context which can be used to render |
3 | 3 | //! both 2D and 3D graphics to the screen. |
4 | 4 |
|
5 | | -use std::time::Instant; |
| 5 | +use std::time::{ |
| 6 | + Duration, |
| 7 | + Instant, |
| 8 | +}; |
6 | 9 |
|
7 | 10 | use lambda_platform::winit::{ |
8 | 11 | winit_exports::{ |
@@ -191,6 +194,18 @@ impl Runtime<(), String> for ApplicationRuntime { |
191 | 194 | fn run(self) -> Result<(), String> { |
192 | 195 | let name = self.name; |
193 | 196 | let event_loop_policy = self.event_loop_policy; |
| 197 | + let frame_warn_threshold: Option<Duration> = match event_loop_policy { |
| 198 | + EventLoopPolicy::Poll => Some(Duration::from_millis(32)), |
| 199 | + EventLoopPolicy::Wait => None, |
| 200 | + EventLoopPolicy::WaitUntil { target_fps } if target_fps > 0 => { |
| 201 | + // Compute an expected frame interval (1 / FPS) and warn only if the |
| 202 | + // observed frame time exceeds it by a slack factor (25%) to avoid |
| 203 | + // spamming on small scheduling jitter. |
| 204 | + let expected_secs = 1.0 / target_fps as f64; |
| 205 | + Some(Duration::from_secs_f64(expected_secs * 1.25)) |
| 206 | + } |
| 207 | + EventLoopPolicy::WaitUntil { .. } => None, |
| 208 | + }; |
194 | 209 | let mut event_loop = LoopBuilder::new().build(); |
195 | 210 | let window = self.window_builder.build(&mut event_loop); |
196 | 211 | let mut component_stack = self.component_stack; |
@@ -378,14 +393,19 @@ impl Runtime<(), String> for ApplicationRuntime { |
378 | 393 | active_render_context.render(commands); |
379 | 394 | } |
380 | 395 |
|
381 | | - // Warn if frames dropped below 32 ms (30 fps). |
382 | | - if event_loop_policy != EventLoopPolicy::Wait |
383 | | - && duration.as_millis() > 32 |
384 | | - { |
385 | | - logging::warn!( |
386 | | - "Frame took too long to render: {:?} ms", |
387 | | - duration.as_millis() |
388 | | - ); |
| 396 | + // Warn if the time between frames significantly exceeds the expected |
| 397 | + // interval for the selected event loop policy. |
| 398 | + // |
| 399 | + // - Poll: uses a fixed 32 ms threshold (~30 fps). |
| 400 | + // - WaitUntil: uses a threshold derived from the target FPS. |
| 401 | + // - Wait: disabled (duration includes idle sleep time). |
| 402 | + if let Some(threshold) = frame_warn_threshold { |
| 403 | + if *duration > threshold { |
| 404 | + logging::warn!( |
| 405 | + "Frame took too long to render: {:?} ms", |
| 406 | + duration.as_millis() |
| 407 | + ); |
| 408 | + } |
389 | 409 | } |
390 | 410 |
|
391 | 411 | None |
|
0 commit comments