From http://www.jwz.org/xscreensaver/xscreensaver-5.32.tar.gz
[xscreensaver] / hacks / fps.c
1 /* fps, Copyright (c) 2001-2014 Jamie Zawinski <jwz@jwz.org>
2  * Draw a frames-per-second display (Xlib and OpenGL).
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or 
10  * implied warranty.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif /* HAVE_CONFIG_H */
16
17 #include "screenhackI.h"
18 #include "fpsI.h"
19
20 fps_state *
21 fps_init (Display *dpy, Window window)
22 {
23   fps_state *st;
24   const char *font;
25   XFontStruct *f;
26
27   if (! get_boolean_resource (dpy, "doFPS", "DoFPS"))
28     return 0;
29
30   st = (fps_state *) calloc (1, sizeof(*st));
31
32   st->dpy = dpy;
33   st->window = window;
34   st->clear_p = get_boolean_resource (dpy, "fpsSolid", "FPSSolid");
35
36   font = get_string_resource (dpy, "fpsFont", "Font");
37
38   if (!font) font = "-*-courier-bold-r-normal-*-180-*";
39   f = XLoadQueryFont (dpy, font);
40   if (!f) f = XLoadQueryFont (dpy, "fixed");
41
42   {
43     XWindowAttributes xgwa;
44     XGCValues gcv;
45     XGetWindowAttributes (dpy, window, &xgwa);
46     gcv.font = f->fid;
47     gcv.foreground = 
48       get_pixel_resource (st->dpy, xgwa.colormap, "foreground", "Foreground");
49     st->draw_gc = XCreateGC (dpy, window, GCFont|GCForeground, &gcv);
50     gcv.foreground =
51       get_pixel_resource (st->dpy, xgwa.colormap, "background", "Background");
52     st->erase_gc = XCreateGC (dpy, window, GCFont|GCForeground, &gcv);
53   }
54
55   st->font = f;
56   st->x = 10;
57   st->y = 10;
58   if (get_boolean_resource (dpy, "fpsTop", "FPSTop"))
59     st->y = - (st->font->ascent + st->font->descent + 10);
60
61   strcpy (st->string, "FPS: ... ");
62
63   return st;
64 }
65
66 void
67 fps_free (fps_state *st)
68 {
69   if (st->draw_gc)  XFreeGC (st->dpy, st->draw_gc);
70   if (st->erase_gc) XFreeGC (st->dpy, st->erase_gc);
71   if (st->font) XFreeFont (st->dpy, st->font);
72   free (st);
73 }
74
75
76 void
77 fps_slept (fps_state *st, unsigned long usecs)
78 {
79   st->slept += usecs;
80 }
81
82
83 double
84 fps_compute (fps_state *st, unsigned long polys, double depth)
85 {
86   if (! st) return 0;  /* too early? */
87
88   /* Every N frames (where N is approximately one second's worth of frames)
89      check the wall clock.  We do this because checking the wall clock is
90      a slow operation.
91    */
92   if (st->frame_count++ >= st->last_ifps)
93     {
94 # ifdef GETTIMEOFDAY_TWO_ARGS
95       struct timezone tzp;
96       gettimeofday(&st->this_frame_end, &tzp);
97 # else
98       gettimeofday(&st->this_frame_end);
99 # endif
100
101       if (st->prev_frame_end.tv_sec == 0)
102         st->prev_frame_end = st->this_frame_end;
103     }
104
105   /* If we've probed the wall-clock time, regenerate the string.
106    */
107   if (st->this_frame_end.tv_sec != st->prev_frame_end.tv_sec)
108     {
109       double uprev_frame_end = (st->prev_frame_end.tv_sec +
110                                 ((double) st->prev_frame_end.tv_usec
111                                  * 0.000001));
112       double uthis_frame_end = (st->this_frame_end.tv_sec +
113                                 ((double) st->this_frame_end.tv_usec
114                                  * 0.000001));
115       double fps = st->frame_count / (uthis_frame_end - uprev_frame_end);
116       double idle = (((double) st->slept * 0.000001) /
117                      (uthis_frame_end - uprev_frame_end));
118       double load = 100 * (1 - idle);
119
120       if (load < 0) load = 0;  /* well that's obviously nonsense... */
121
122       st->prev_frame_end = st->this_frame_end;
123       st->frame_count = 0;
124       st->slept       = 0;
125       st->last_ifps   = fps;
126       st->last_fps    = fps;
127
128       sprintf (st->string, (polys 
129                             ? "FPS:   %.1f \nLoad:  %.1f%% "
130                             : "FPS:  %.1f \nLoad: %.1f%% "),
131                fps, load);
132
133       if (polys > 0)
134         {
135           const char *s = "";
136 # if 0
137           if      (polys >= (1024 * 1024)) polys >>= 20, s = "M";
138           else if (polys >= 2048)          polys >>= 10, s = "K";
139 # endif
140
141           strcat (st->string, "\nPolys: ");
142           if (polys >= 1000000)
143             sprintf (st->string + strlen(st->string), "%lu,%03lu,%03lu%s ",
144                      (polys / 1000000), ((polys / 1000) % 1000),
145                      (polys % 1000), s);
146           else if (polys >= 1000)
147             sprintf (st->string + strlen(st->string), "%lu,%03lu%s ",
148                      (polys / 1000), (polys % 1000), s);
149           else
150             sprintf (st->string + strlen(st->string), "%lu%s ", polys, s);
151         }
152
153       if (depth >= 0.0)
154         {
155           unsigned long L = strlen (st->string);
156           char *s = st->string + L;
157           strcat (s, "\nDepth: ");
158           sprintf (s + strlen(s), "%.1f", depth);
159           L = strlen (s);
160           /* Remove trailing ".0" in case depth is not a fraction. */
161           if (s[L-2] == '.' && s[L-1] == '0')
162             s[L-2] = 0;
163         }
164     }
165
166   return st->last_fps;
167 }
168
169
170
171 /* Width (and optionally height) of the string in pixels.
172  */
173 static int
174 string_width (XFontStruct *f, const char *c, int *height_ret)
175 {
176   int x = 0;
177   int max_w = 0;
178   int h = f->ascent + f->descent;
179   while (*c)
180     {
181       int cc = *((unsigned char *) c);
182       if (*c == '\n')
183         {
184           if (x > max_w) max_w = x;
185           x = 0;
186           h += f->ascent + f->descent;
187         }
188       else
189         x += (f->per_char
190               ? f->per_char[cc-f->min_char_or_byte2].width
191               : f->min_bounds.rbearing);
192       c++;
193     }
194   if (x > max_w) max_w = x;
195   if (height_ret) *height_ret = h;
196
197   return max_w;
198 }
199
200
201 /* This function is used only in Xlib mode.  For GL mode, see glx/fps-gl.c.
202  */
203 void
204 fps_draw (fps_state *st)
205 {
206   XWindowAttributes xgwa;
207   const char *string = st->string;
208   const char *s;
209   int x = st->x;
210   int y = st->y;
211   int lines = 1;
212   int lh = st->font->ascent + st->font->descent;
213
214   XGetWindowAttributes (st->dpy, st->window, &xgwa);
215
216   for (s = string; *s; s++) 
217     if (*s == '\n') lines++;
218
219   if (y < 0)
220     y = -y + (lines-1) * lh;
221   else
222     y = xgwa.height - y;
223
224   y -= lh * (lines-1) + st->font->descent;
225
226   /* clear the background */
227   if (st->clear_p)
228     {
229       int w, h;
230       w = string_width (st->font, string, &h);
231       XFillRectangle (st->dpy, st->window, st->erase_gc,
232                       x - st->font->descent,
233                       y - lh,
234                       w + 2*st->font->descent,
235                       h + 2*st->font->descent);
236     }
237
238   /* draw the text */
239   while (lines)
240     {
241       s = strchr (string, '\n');
242       if (! s) s = string + strlen(string);
243       XDrawString (st->dpy, st->window, st->draw_gc,
244                    x, y, string, (int) (s - string));
245       string = s;
246       string++;
247       lines--;
248       y += lh;
249     }
250 }