summaryrefslogtreecommitdiffstats
path: root/hacks/fps.c
blob: a24f62315f98879d8d7b792290be78c830446bd6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* fps, Copyright (c) 2001-2018 Jamie Zawinski <jwz@jwz.org>
 * Draw a frames-per-second display (Xlib and OpenGL).
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */

#include <time.h>
#include "screenhackI.h"
#include "fpsI.h"

fps_state *
fps_init (Display *dpy, Window window)
{
  fps_state *st;
  const char *font;
  XFontStruct *f;
  Bool top_p;
  XWindowAttributes xgwa;

  if (! get_boolean_resource (dpy, "doFPS", "DoFPS"))
    return 0;

  if (!strcasecmp (progname, "BSOD")) return 0;  /* Never worked right */

  top_p = get_boolean_resource (dpy, "fpsTop", "FPSTop");

  st = (fps_state *) calloc (1, sizeof(*st));

  st->dpy = dpy;
  st->window = window;
  st->clear_p = get_boolean_resource (dpy, "fpsSolid", "FPSSolid");

  font = get_string_resource (dpy, "fpsFont", "Font");

  if (!font)
    font = "-*-courier-bold-r-normal-*-*-180-*-*-*-*-*-*"; /* also texfont.c */
  f = load_font_retry (dpy, font);
  if (!f) abort();

  {
    XGCValues gcv;
    XGetWindowAttributes (dpy, window, &xgwa);
    gcv.font = f->fid;
    gcv.foreground = 
      get_pixel_resource (st->dpy, xgwa.colormap, "foreground", "Foreground");
    st->draw_gc = XCreateGC (dpy, window, GCFont|GCForeground, &gcv);
    gcv.foreground =
      get_pixel_resource (st->dpy, xgwa.colormap, "background", "Background");
    st->erase_gc = XCreateGC (dpy, window, GCFont|GCForeground, &gcv);
  }

  st->font = f;
  st->x = 10;
  st->y = 10;
  if (top_p)
    st->y = - (st->font->ascent + st->font->descent + 10);

# ifdef USE_IPHONE
  /* Don't hide the FPS display under the iPhone X bezel.
     #### This is the worst of all possible ways to do this!  But how else?
   */
  if (xgwa.width == 2436 || xgwa.height == 2436)
    {
      st->x += 48;
      st->y += 48 * (top_p ? -1 : 1);
    }
# endif

  strcpy (st->string, "FPS: ... ");

  return st;
}

void
fps_free (fps_state *st)
{
  if (st->draw_gc)  XFreeGC (st->dpy, st->draw_gc);
  if (st->erase_gc) XFreeGC (st->dpy, st->erase_gc);
  if (st->font) XFreeFont (st->dpy, st->font);
  free (st);
}


void
fps_slept (fps_state *st, unsigned long usecs)
{
  st->slept += usecs;
}


double
fps_compute (fps_state *st, unsigned long polys, double depth)
{
  if (! st) return 0;  /* too early? */

  /* Every N frames (where N is approximately one second's worth of frames)
     check the wall clock.  We do this because checking the wall clock is
     a slow operation.
   */
  if (st->frame_count++ >= st->last_ifps)
    {
# ifdef GETTIMEOFDAY_TWO_ARGS
      struct timezone tzp;
      gettimeofday(&st->this_frame_end, &tzp);
# else
      gettimeofday(&st->this_frame_end);
# endif

      if (st->prev_frame_end.tv_sec == 0)
        st->prev_frame_end = st->this_frame_end;
    }

  /* If we've probed the wall-clock time, regenerate the string.
   */
  if (st->this_frame_end.tv_sec != st->prev_frame_end.tv_sec)
    {
      double uprev_frame_end = (st->prev_frame_end.tv_sec +
                                ((double) st->prev_frame_end.tv_usec
                                 * 0.000001));
      double uthis_frame_end = (st->this_frame_end.tv_sec +
                                ((double) st->this_frame_end.tv_usec
                                 * 0.000001));
      double fps = st->frame_count / (uthis_frame_end - uprev_frame_end);
      double idle = (((double) st->slept * 0.000001) /
                     (uthis_frame_end - uprev_frame_end));
      double load = 100 * (1 - idle);

      if (load < 0) load = 0;  /* well that's obviously nonsense... */

      st->prev_frame_end = st->this_frame_end;
      st->frame_count = 0;
      st->slept       = 0;
      st->last_ifps   = fps;
      st->last_fps    = fps;

      sprintf (st->string, (polys 
                            ? "FPS:   %.1f \nLoad:  %.1f%% "
                            : "FPS:  %.1f \nLoad: %.1f%% "),
               fps, load);

      if (polys > 0)
        {
          const char *s = "";
# if 0
          if      (polys >= (1024 * 1024)) polys >>= 20, s = "M";
          else if (polys >= 2048)          polys >>= 10, s = "K";
# endif

          strcat (st->string, "\nPolys: ");
          if (polys >= 1000000)
            sprintf (st->string + strlen(st->string), "%lu,%03lu,%03lu%s ",
                     (polys / 1000000), ((polys / 1000) % 1000),
                     (polys % 1000), s);
          else if (polys >= 1000)
            sprintf (st->string + strlen(st->string), "%lu,%03lu%s ",
                     (polys / 1000), (polys % 1000), s);
          else
            sprintf (st->string + strlen(st->string), "%lu%s ", polys, s);
        }

      if (depth >= 0.0)
        {
          unsigned long L = strlen (st->string);
          char *s = st->string + L;
          strcat (s, "\nDepth: ");
          sprintf (s + strlen(s), "%.1f", depth);
          L = strlen (s);
          /* Remove trailing ".0" in case depth is not a fraction. */
          if (s[L-2] == '.' && s[L-1] == '0')
            s[L-2] = 0;
        }
    }

  return st->last_fps;
}



/* Width (and optionally height) of the string in pixels.
 */
static int
string_width (XFontStruct *f, const char *c, int *height_ret)
{
  int x = 0;
  int max_w = 0;
  int h = f->ascent + f->descent;
  while (*c)
    {
      int cc = *((unsigned char *) c);
      if (*c == '\n')
        {
          if (x > max_w) max_w = x;
          x = 0;
          h += f->ascent + f->descent;
        }
      else
        x += (f->per_char
              ? f->per_char[cc-f->min_char_or_byte2].width
              : f->min_bounds.rbearing);
      c++;
    }
  if (x > max_w) max_w = x;
  if (height_ret) *height_ret = h;

  return max_w;
}


/* This function is used only in Xlib mode.  For GL mode, see glx/fps-gl.c.
 */
void
fps_draw (fps_state *st)
{
  XWindowAttributes xgwa;
  const char *string = st->string;
  const char *s;
  int x = st->x;
  int y = st->y;
  int lines = 1;
  int lh = st->font->ascent + st->font->descent;

  XGetWindowAttributes (st->dpy, st->window, &xgwa);

  for (s = string; *s; s++) 
    if (*s == '\n') lines++;

  if (y < 0)
    y = -y + (lines-1) * lh;
  else
    y = xgwa.height - y;

  y -= lh * (lines-1) + st->font->descent;

  /* clear the background */
  if (st->clear_p)
    {
      int w, h;
      w = string_width (st->font, string, &h);
      XFillRectangle (st->dpy, st->window, st->erase_gc,
                      x - st->font->descent,
                      y - lh,
                      w + 2*st->font->descent,
                      h + 2*st->font->descent);
    }

  /* draw the text */
  while (lines)
    {
      s = strchr (string, '\n');
      if (! s) s = string + strlen(string);
      XDrawString (st->dpy, st->window, st->draw_gc,
                   x, y, string, (int) (s - string));
      string = s;
      string++;
      lines--;
      y += lh;
    }
}