From 306aeab6dfb6673dcedae18a3bb26f0f9a5311de Mon Sep 17 00:00:00 2001 From: Arman Uguray Date: Thu, 16 Mar 2023 00:30:51 -0700 Subject: [PATCH] [frame_stats] Key binding to clear min/max frame time; address review comments --- examples/with_winit/README.md | 1 + examples/with_winit/src/lib.rs | 9 ++++++--- examples/with_winit/src/stats.rs | 13 +++++++------ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/examples/with_winit/README.md b/examples/with_winit/README.md index 49527b5..8dc1a8e 100644 --- a/examples/with_winit/README.md +++ b/examples/with_winit/README.md @@ -19,4 +19,5 @@ $ cargo run -p with_winit --release -- [SVG FILES] - Arrow keys switch between SVG images in the current set. - Space resets the position and zoom of the image. - S toggles the frame statistics layer +- C resets the min/max frame time tracked by statistics - Escape exits the program. diff --git a/examples/with_winit/src/lib.rs b/examples/with_winit/src/lib.rs index 7943d6f..760ec99 100644 --- a/examples/with_winit/src/lib.rs +++ b/examples/with_winit/src/lib.rs @@ -99,7 +99,7 @@ fn run( let mut simple_text = SimpleText::new(); let mut images = ImageCache::new(); let mut stats = stats::Stats::new(); - let mut stats_toggle = true; + let mut stats_shown = true; let start = Instant::now(); let mut touch_state = multi_touch::TouchState::new(); @@ -146,7 +146,10 @@ fn run( transform = Affine::IDENTITY; } Some(VirtualKeyCode::S) => { - stats_toggle = !stats_toggle; + stats_shown = !stats_shown; + } + Some(VirtualKeyCode::C) => { + stats.clear_min_and_max(); } Some(VirtualKeyCode::Escape) => { *control_flow = ControlFlow::Exit; @@ -304,7 +307,7 @@ fn run( transform = transform * Affine::scale(scale_factor); } builder.append(&fragment, Some(transform)); - if stats_toggle { + if stats_shown { snapshot.draw_layer( &mut builder, &mut scene_params.text, diff --git a/examples/with_winit/src/stats.rs b/examples/with_winit/src/stats.rs index e0b0b28..0e68e63 100644 --- a/examples/with_winit/src/stats.rs +++ b/examples/with_winit/src/stats.rs @@ -168,6 +168,11 @@ impl Stats { } } + pub fn clear_min_and_max(&mut self) { + self.min = u64::MAX; + self.max = u64::MIN; + } + pub fn add_sample(&mut self, sample: Sample) { let oldest = if self.count < SLIDING_WINDOW_SIZE { self.count += 1; @@ -181,11 +186,7 @@ impl Stats { if let Some(oldest) = oldest { self.sum -= oldest; } - if micros < self.min { - self.min = micros; - } - if micros > self.max { - self.max = micros; - } + self.min = self.min.min(micros); + self.max = self.max.max(micros); } }