Skip to content

Commit fa9f48e

Browse files
committed
[update] how frame warning thresholds are computed.
1 parent ca9fb91 commit fa9f48e

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

crates/lambda-rs/src/runtimes/application.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
//! provides a window and a render context which can be used to render
33
//! both 2D and 3D graphics to the screen.
44
5-
use std::time::Instant;
5+
use std::time::{
6+
Duration,
7+
Instant,
8+
};
69

710
use lambda_platform::winit::{
811
winit_exports::{
@@ -191,6 +194,18 @@ impl Runtime<(), String> for ApplicationRuntime {
191194
fn run(self) -> Result<(), String> {
192195
let name = self.name;
193196
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+
};
194209
let mut event_loop = LoopBuilder::new().build();
195210
let window = self.window_builder.build(&mut event_loop);
196211
let mut component_stack = self.component_stack;
@@ -378,14 +393,19 @@ impl Runtime<(), String> for ApplicationRuntime {
378393
active_render_context.render(commands);
379394
}
380395

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+
}
389409
}
390410

391411
None

0 commit comments

Comments
 (0)