1 /* fps, Copyright (c) 2001-2017 Jamie Zawinski <jwz@jwz.org>
2 * Draw a frames-per-second display (Xlib and OpenGL).
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
15 #endif /* HAVE_CONFIG_H */
18 #include "screenhackI.h"
22 fps_init (Display *dpy, Window window)
28 XWindowAttributes xgwa;
30 if (! get_boolean_resource (dpy, "doFPS", "DoFPS"))
33 if (!strcasecmp (progname, "BSOD")) return 0; /* Never worked right */
35 top_p = get_boolean_resource (dpy, "fpsTop", "FPSTop");
37 st = (fps_state *) calloc (1, sizeof(*st));
41 st->clear_p = get_boolean_resource (dpy, "fpsSolid", "FPSSolid");
43 font = get_string_resource (dpy, "fpsFont", "Font");
46 font = "-*-courier-bold-r-normal-*-*-180-*-*-*-*-*-*"; /* also texfont.c */
47 f = XLoadQueryFont (dpy, font);
48 if (!f) f = XLoadQueryFont (dpy, "fixed");
52 XGetWindowAttributes (dpy, window, &xgwa);
55 get_pixel_resource (st->dpy, xgwa.colormap, "foreground", "Foreground");
56 st->draw_gc = XCreateGC (dpy, window, GCFont|GCForeground, &gcv);
58 get_pixel_resource (st->dpy, xgwa.colormap, "background", "Background");
59 st->erase_gc = XCreateGC (dpy, window, GCFont|GCForeground, &gcv);
66 st->y = - (st->font->ascent + st->font->descent + 10);
69 /* Don't hide the FPS display under the iPhone X bezel.
70 #### This is the worst of all possible ways to do this! But how else?
72 if (xgwa.width == 2436 || xgwa.height == 2436)
75 st->y += 48 * (top_p ? -1 : 1);
79 strcpy (st->string, "FPS: ... ");
85 fps_free (fps_state *st)
87 if (st->draw_gc) XFreeGC (st->dpy, st->draw_gc);
88 if (st->erase_gc) XFreeGC (st->dpy, st->erase_gc);
89 if (st->font) XFreeFont (st->dpy, st->font);
95 fps_slept (fps_state *st, unsigned long usecs)
102 fps_compute (fps_state *st, unsigned long polys, double depth)
104 if (! st) return 0; /* too early? */
106 /* Every N frames (where N is approximately one second's worth of frames)
107 check the wall clock. We do this because checking the wall clock is
110 if (st->frame_count++ >= st->last_ifps)
112 # ifdef GETTIMEOFDAY_TWO_ARGS
114 gettimeofday(&st->this_frame_end, &tzp);
116 gettimeofday(&st->this_frame_end);
119 if (st->prev_frame_end.tv_sec == 0)
120 st->prev_frame_end = st->this_frame_end;
123 /* If we've probed the wall-clock time, regenerate the string.
125 if (st->this_frame_end.tv_sec != st->prev_frame_end.tv_sec)
127 double uprev_frame_end = (st->prev_frame_end.tv_sec +
128 ((double) st->prev_frame_end.tv_usec
130 double uthis_frame_end = (st->this_frame_end.tv_sec +
131 ((double) st->this_frame_end.tv_usec
133 double fps = st->frame_count / (uthis_frame_end - uprev_frame_end);
134 double idle = (((double) st->slept * 0.000001) /
135 (uthis_frame_end - uprev_frame_end));
136 double load = 100 * (1 - idle);
138 if (load < 0) load = 0; /* well that's obviously nonsense... */
140 st->prev_frame_end = st->this_frame_end;
146 sprintf (st->string, (polys
147 ? "FPS: %.1f \nLoad: %.1f%% "
148 : "FPS: %.1f \nLoad: %.1f%% "),
155 if (polys >= (1024 * 1024)) polys >>= 20, s = "M";
156 else if (polys >= 2048) polys >>= 10, s = "K";
159 strcat (st->string, "\nPolys: ");
160 if (polys >= 1000000)
161 sprintf (st->string + strlen(st->string), "%lu,%03lu,%03lu%s ",
162 (polys / 1000000), ((polys / 1000) % 1000),
164 else if (polys >= 1000)
165 sprintf (st->string + strlen(st->string), "%lu,%03lu%s ",
166 (polys / 1000), (polys % 1000), s);
168 sprintf (st->string + strlen(st->string), "%lu%s ", polys, s);
173 unsigned long L = strlen (st->string);
174 char *s = st->string + L;
175 strcat (s, "\nDepth: ");
176 sprintf (s + strlen(s), "%.1f", depth);
178 /* Remove trailing ".0" in case depth is not a fraction. */
179 if (s[L-2] == '.' && s[L-1] == '0')
189 /* Width (and optionally height) of the string in pixels.
192 string_width (XFontStruct *f, const char *c, int *height_ret)
196 int h = f->ascent + f->descent;
199 int cc = *((unsigned char *) c);
202 if (x > max_w) max_w = x;
204 h += f->ascent + f->descent;
208 ? f->per_char[cc-f->min_char_or_byte2].width
209 : f->min_bounds.rbearing);
212 if (x > max_w) max_w = x;
213 if (height_ret) *height_ret = h;
219 /* This function is used only in Xlib mode. For GL mode, see glx/fps-gl.c.
222 fps_draw (fps_state *st)
224 XWindowAttributes xgwa;
225 const char *string = st->string;
230 int lh = st->font->ascent + st->font->descent;
232 XGetWindowAttributes (st->dpy, st->window, &xgwa);
234 for (s = string; *s; s++)
235 if (*s == '\n') lines++;
238 y = -y + (lines-1) * lh;
242 y -= lh * (lines-1) + st->font->descent;
244 /* clear the background */
248 w = string_width (st->font, string, &h);
249 XFillRectangle (st->dpy, st->window, st->erase_gc,
250 x - st->font->descent,
252 w + 2*st->font->descent,
253 h + 2*st->font->descent);
259 s = strchr (string, '\n');
260 if (! s) s = string + strlen(string);
261 XDrawString (st->dpy, st->window, st->draw_gc,
262 x, y, string, (int) (s - string));