1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2013 Marek Olšák <maraeo@gmail.com>
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice (including the
15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
16848b8605Smrg * of the Software.
17848b8605Smrg *
18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21848b8605Smrg * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25848b8605Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg
28848b8605Smrg/* This file contains code for calculating framerate for displaying on the HUD.
29848b8605Smrg */
30848b8605Smrg
31848b8605Smrg#include "hud/hud_private.h"
32b8e80941Smrg#include "util/os_time.h"
33848b8605Smrg#include "util/u_memory.h"
34848b8605Smrg
35848b8605Smrgstruct fps_info {
36b8e80941Smrg   boolean frametime;
37848b8605Smrg   int frames;
38848b8605Smrg   uint64_t last_time;
39848b8605Smrg};
40848b8605Smrg
41848b8605Smrgstatic void
42b8e80941Smrgquery_fps(struct hud_graph *gr, struct pipe_context *pipe)
43848b8605Smrg{
44848b8605Smrg   struct fps_info *info = gr->query_data;
45848b8605Smrg   uint64_t now = os_time_get();
46848b8605Smrg
47848b8605Smrg   info->frames++;
48848b8605Smrg
49848b8605Smrg   if (info->last_time) {
50b8e80941Smrg      if (info->frametime) {
51b8e80941Smrg         double frametime = ((double)now - (double)info->last_time) / 1000.0;
52b8e80941Smrg         hud_graph_add_value(gr, frametime);
53b8e80941Smrg         info->last_time = now;
54b8e80941Smrg      }
55b8e80941Smrg      else if (info->last_time + gr->pane->period <= now) {
56b8e80941Smrg         double fps = ((uint64_t)info->frames) * 1000000 /
57848b8605Smrg                      (double)(now - info->last_time);
58848b8605Smrg         info->frames = 0;
59848b8605Smrg         info->last_time = now;
60848b8605Smrg
61b8e80941Smrg         hud_graph_add_value(gr, fps);
62848b8605Smrg      }
63848b8605Smrg   }
64848b8605Smrg   else {
65848b8605Smrg      info->last_time = now;
66848b8605Smrg   }
67848b8605Smrg}
68848b8605Smrg
69848b8605Smrgstatic void
70b8e80941Smrgfree_query_data(void *p, struct pipe_context *pipe)
71848b8605Smrg{
72848b8605Smrg   FREE(p);
73848b8605Smrg}
74848b8605Smrg
75848b8605Smrgvoid
76848b8605Smrghud_fps_graph_install(struct hud_pane *pane)
77848b8605Smrg{
78848b8605Smrg   struct hud_graph *gr = CALLOC_STRUCT(hud_graph);
79848b8605Smrg
80848b8605Smrg   if (!gr)
81848b8605Smrg      return;
82848b8605Smrg
83848b8605Smrg   strcpy(gr->name, "fps");
84848b8605Smrg   gr->query_data = CALLOC_STRUCT(fps_info);
85848b8605Smrg   if (!gr->query_data) {
86848b8605Smrg      FREE(gr);
87848b8605Smrg      return;
88848b8605Smrg   }
89b8e80941Smrg   struct fps_info *info = gr->query_data;
90b8e80941Smrg   info->frametime = false;
91848b8605Smrg
92848b8605Smrg   gr->query_new_value = query_fps;
93848b8605Smrg
94848b8605Smrg   /* Don't use free() as our callback as that messes up Gallium's
95848b8605Smrg    * memory debugger.  Use simple free_query_data() wrapper.
96848b8605Smrg    */
97848b8605Smrg   gr->free_query_data = free_query_data;
98848b8605Smrg
99848b8605Smrg   hud_pane_add_graph(pane, gr);
100848b8605Smrg}
101b8e80941Smrg
102b8e80941Smrgvoid
103b8e80941Smrghud_frametime_graph_install(struct hud_pane *pane)
104b8e80941Smrg{
105b8e80941Smrg   struct hud_graph *gr = CALLOC_STRUCT(hud_graph);
106b8e80941Smrg
107b8e80941Smrg   if (!gr)
108b8e80941Smrg      return;
109b8e80941Smrg
110b8e80941Smrg   strcpy(gr->name, "frametime (ms)");
111b8e80941Smrg   gr->query_data = CALLOC_STRUCT(fps_info);
112b8e80941Smrg   if (!gr->query_data) {
113b8e80941Smrg      FREE(gr);
114b8e80941Smrg      return;
115b8e80941Smrg   }
116b8e80941Smrg   struct fps_info *info = gr->query_data;
117b8e80941Smrg   info->frametime = true;
118b8e80941Smrg
119b8e80941Smrg   gr->query_new_value = query_fps;
120b8e80941Smrg
121b8e80941Smrg   gr->free_query_data = free_query_data;
122b8e80941Smrg
123b8e80941Smrg   hud_pane_add_graph(pane, gr);
124b8e80941Smrg}
125