Monado OpenXR Runtime
u_frame_times_widget.h
Go to the documentation of this file.
1// Copyright 2019, Collabora, Ltd.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Shared code for visualizing frametimes.
6 * @author Moses Turner <moses@collabora.com>
7 * @ingroup aux_util
8 */
9
10#pragma once
11
12#include "xrt/xrt_defines.h"
13
14#include "os/os_time.h"
15#include "util/u_var.h"
16#include "util/u_logging.h"
17#include "util/u_misc.h"
18
19#include <assert.h>
20
21#define FPS_WIDGET_NUM_FRAME_TIMES 50
22
24{
25 //! Current Index for times_ns.
26 int index;
27
28 //! Timestamps of last-pushed frames.
29 int64_t times_ns[FPS_WIDGET_NUM_FRAME_TIMES];
30
31 //! Frametimes between last-pushed frames.
32 float timings_ms[FPS_WIDGET_NUM_FRAME_TIMES];
33
34 //! Average FPS of last NUM_FRAME_TIMES pushed frames.
35 float fps;
36
37 struct u_var_timing *debug_var;
38};
39
40static inline void
41u_frame_times_widget_push_sample(struct u_frame_times_widget *widget, uint64_t new_frame_time)
42{
43 int last_index = widget->index;
44
45 widget->index++;
46 widget->index %= FPS_WIDGET_NUM_FRAME_TIMES;
47
48 // update fps only once every FPS_NUM_TIMINGS
49 if (widget->index == 0) {
50 float total_ms = 0;
51
52 // frame *timings* are durations between *times*
53 int NUM_FRAME_TIMINGS = FPS_WIDGET_NUM_FRAME_TIMES - 1;
54
55 for (int i = 0; i < NUM_FRAME_TIMINGS; i++) {
56 total_ms += widget->timings_ms[i];
57 }
58 float avg_frametime_ms = total_ms / ((float)NUM_FRAME_TIMINGS);
59 widget->fps = 1000.f / avg_frametime_ms;
60 }
61
62 widget->times_ns[widget->index] = new_frame_time;
63
64 assert(widget->index >= 0);
65 assert(widget->index < FPS_WIDGET_NUM_FRAME_TIMES);
66 assert(last_index >= 0);
67 assert(last_index < FPS_WIDGET_NUM_FRAME_TIMES);
68
69 uint64_t diff = widget->times_ns[widget->index] - widget->times_ns[last_index];
70 widget->timings_ms[widget->index] = (float)time_ns_to_ms_f(diff);
71}
72
73static inline void
74u_frame_times_widget_init(struct u_frame_times_widget *widget, float target_frame_time_ms, float range)
75{
76 uint64_t now = os_monotonic_get_ns();
77 for (int i = 0; i < FPS_WIDGET_NUM_FRAME_TIMES; i++) {
78 widget->times_ns[i] = now + i;
79 }
80
81 struct u_var_timing *ft = U_TYPED_CALLOC(struct u_var_timing);
82
83
84 ft->values.data = widget->timings_ms;
85 ft->values.length = FPS_WIDGET_NUM_FRAME_TIMES;
86 ft->values.index_ptr = &widget->index;
87
88
89 ft->reference_timing = target_frame_time_ms;
90 ft->range = range;
91 ft->unit = "ms";
92 ft->dynamic_rescale = false;
93 ft->center_reference_timing = true;
94 widget->debug_var = ft;
95 widget->index = 0;
96}
97
98// Call u_var_remove_root first!
99static inline void
100u_frame_times_widget_teardown(struct u_frame_times_widget *widget)
101{
102 free(widget->debug_var);
103}
static int64_t os_monotonic_get_ns(void)
Return a monotonic clock in nanoseconds.
Definition: os_time.h:309
static double time_ns_to_ms_f(time_duration_ns ns)
Convert nanoseconds to double float milliseconds, useful for printing.
Definition: u_time.h:114
#define U_TYPED_CALLOC(TYPE)
Allocate and zero the space required for some type, and cast the return type appropriately.
Definition: u_misc.h:47
Wrapper around OS native time functions.
Definition: u_frame_times_widget.h:24
int64_t times_ns[50]
Timestamps of last-pushed frames.
Definition: u_frame_times_widget.h:29
int index
Current Index for times_ns.
Definition: u_frame_times_widget.h:26
float timings_ms[50]
Frametimes between last-pushed frames.
Definition: u_frame_times_widget.h:32
float fps
Average FPS of last NUM_FRAME_TIMES pushed frames.
Definition: u_frame_times_widget.h:35
Used to plot a graph of timing information.
Definition: u_var.h:46
float range
How many units the graph expands by default.
Definition: u_var.h:57
bool center_reference_timing
If false, reference_timing will be the bottom of the graph.
Definition: u_var.h:54
bool dynamic_rescale
Rescale graph's value range when value exceeds range.
Definition: u_var.h:60
float reference_timing
A reference line drawn on the plot.
Definition: u_var.h:51
struct u_var_f32_arr values
Values to be plotted.
Definition: u_var.h:48
const char * unit
A string describing the unit used, not freed.
Definition: u_var.h:63
Basic logging functionality.
Very small misc utils.
Variable tracking code.
Common defines and enums for XRT.