From 387c7be60094530ceffc3411ba4ecb0eb5446f47 Mon Sep 17 00:00:00 2001 From: Wenzhuo Liu Date: Mon, 4 Mar 2024 16:50:23 +0800 Subject: [PATCH] Fix negative duration panic on macos (#475) * Fix negative duration panic on macos * apply suggestions from code review * Add a comment explaining condition --------- Co-authored-by: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> --- examples/with_winit/src/stats.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/examples/with_winit/src/stats.rs b/examples/with_winit/src/stats.rs index 72b433cf8..2b788e3ce 100644 --- a/examples/with_winit/src/stats.rs +++ b/examples/with_winit/src/stats.rs @@ -384,11 +384,24 @@ pub fn draw_gpu_profiling( let text_size = (text_height * 0.9) as f32; // Text is specified by the baseline, but the y positions all refer to the top of the text cur_text_y = text_y + text_height; - let label = format!( - "{:.2?} - {:.30}", - Duration::from_secs_f64(this_time), - profile.label - ); + let label = { + // Sometimes, the duration turns out to be negative + // We have not yet debugged this, but display the absolute value in that case + // see https://github.com/linebender/vello/pull/475 for more + if this_time < 0.0 { + format!( + "-{:.2?}(!!) - {:.30}", + Duration::from_secs_f64(this_time.abs()), + profile.label + ) + } else { + format!( + "{:.2?} - {:.30}", + Duration::from_secs_f64(this_time), + profile.label + ) + } + }; scene.fill( Fill::NonZero, offset,