1 /******************************************************************************
2 * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Texas Instruments Incorporated nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 *****************************************************************************/
29 #pragma once
31 #include <vector>
32 #include <chrono>
34 #define MAX_WINDOW_SIZE 64
35 #define INIT_FRAME_TIME 0.001
37 // Compute average FPS across a sliding window of frames
38 class AvgFPSWindow
39 {
40 public:
41 AvgFPSWindow(uint32_t window_size) :
42 window_size_m(window_size), circ_idx_m(0), total_time_m(0.0)
43 {
44 if (window_size_m == 0 || window_size_m > MAX_WINDOW_SIZE)
45 window_size_m = MAX_WINDOW_SIZE;
46 history_times_m.assign(window_size_m, INIT_FRAME_TIME);
47 frame_time_m = INIT_FRAME_TIME;
48 total_time_m = window_size_m * INIT_FRAME_TIME;
49 t0_m = std::chrono::steady_clock::now();
50 }
52 // Invoked per loop iteration to capture frame time
53 void Tick()
54 {
55 t1_m = std::chrono::steady_clock::now();
56 std::chrono::duration<double> elapsed = t1_m - t0_m;
57 frame_time_m = elapsed.count(); // in seconds
58 t0_m = t1_m;
59 }
61 // Update the frame_time_m into circular array for history timing
62 // Reading will only become valid after window_size_m frames
63 // Return updated average FPS
64 double UpdateAvgFPS()
65 {
66 total_time_m += frame_time_m - history_times_m[circ_idx_m];
67 history_times_m[circ_idx_m] = frame_time_m;
68 circ_idx_m = (circ_idx_m + 1) % window_size_m;
69 return (1.0 * window_size_m) / total_time_m;
70 }
72 // Return average FPS
73 double GetAvgFPS()
74 {
75 return (1.0 * window_size_m) / total_time_m;
76 }
78 AvgFPSWindow() =delete;
79 AvgFPSWindow(const AvgFPSWindow&) =delete;
80 AvgFPSWindow& operator=(const AvgFPSWindow&) =delete;
82 private:
83 uint32_t window_size_m;
84 uint32_t circ_idx_m;
85 double total_time_m;
86 std::vector<double> history_times_m;
87 std::chrono::time_point<std::chrono::steady_clock> t0_m, t1_m;
88 double frame_time_m;
89 };