Split out a header file so other projects can re-use the code
[experiments/fps-meter.git] / fps-meter.h
1 /*
2  * fps-meter - Example program about how to measure frames per seconds
3  *
4  * Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef FPS_METER_H
21 #define FPS_METER_H
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <time.h>
26
27 #ifdef DEBUG
28 #define dbg(...)                     \
29         do {                         \
30                 printf(__VA_ARGS__); \
31                 printf("\n");        \
32                 fflush(stdout);      \
33         } while(0)
34 #else
35 #define dbg(...) do {} while(0)
36 #endif
37
38 #define NSEC_PER_SEC 1000000000
39
40 #define timespecsub(a, b, result)                                \
41         do {                                                     \
42                 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;    \
43                 (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
44                 if ((result)->tv_nsec < 0) {                     \
45                         --(result)->tv_sec;                      \
46                         (result)->tv_nsec += 1000000000;         \
47                 }                                                \
48         } while(0)
49
50 struct fps_meter_stats {
51         struct timespec time_start;
52         struct timespec time_end;
53         struct timespec elapsed;
54
55         unsigned int frames;
56         double nsecs;
57 };
58
59 static void fps_meter_init(struct fps_meter_stats *stats)
60 {
61         memset(stats, 0, sizeof(*stats));
62
63         clock_gettime(CLOCK_MONOTONIC, &stats->time_start);
64         dbg("Init time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
65 }
66
67 static void fps_meter_update(struct fps_meter_stats *stats)
68 {
69         dbg("Start time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
70
71         clock_gettime(CLOCK_MONOTONIC, &stats->time_end);
72         dbg("End time: s: %ld, ns: %ld", stats->time_end.tv_sec, stats->time_end.tv_nsec);
73
74         timespecsub(&stats->time_end, &stats->time_start, &stats->elapsed);
75         dbg("Elapsed s: %ld ns: %ld", stats->elapsed.tv_sec, stats->elapsed.tv_nsec);
76
77         stats->frames++;
78         stats->nsecs += (stats->elapsed.tv_sec * NSEC_PER_SEC + stats->elapsed.tv_nsec);
79         if (stats->nsecs >= NSEC_PER_SEC) {
80                 /* 
81                  * if we were garanteed that each frame took less than
82                  * a second, then just printing 'frames' would be enough here,
83                  * but if we want to cover the case when a frame may take more
84                  * than a second, some calculations have to be done.
85                  */
86                 float fps = stats->frames / (stats->nsecs / NSEC_PER_SEC);
87                 printf("(frames: %d, nsecs: %f) FPS: %.2f\n", stats->frames, stats->nsecs, fps);
88                 stats->nsecs = 0;
89                 stats->frames = 0;
90         }
91
92         /* update the stats for the next iteration */
93         clock_gettime(CLOCK_MONOTONIC, &stats->time_start);
94 }
95 #endif /* FPS_METER_H */