~ubuntu-branches/debian/sid/xscreensaver/sid

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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* fps, Copyright (c) 2001-2007 Jamie Zawinski <jwz@jwz.org>
 * Utility function to draw a frames-per-second display.
 *
 * 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 "xlockmoreI.h"

#ifdef HAVE_COCOA
# undef usleep /* conflicts with 10.5 headers... */
# include <OpenGL/gl.h>
# include <OpenGL/glu.h>
# include <AGL/agl.h>
#else /* !HAVE_COCOA -- real Xlib */
# include <GL/gl.h>
# include <GL/glu.h>
# include <GL/glx.h>
#endif /* !HAVE_COCOA */

#undef DEBUG  /* Defining this causes check_gl_error() to be called inside
                 time-critical sections, which could slow things down (since
                 it might result in a round-trip, and stall of the pipeline.)
               */

extern void clear_gl_error (void);
extern void check_gl_error (const char *type);

extern GLfloat fps_1 (ModeInfo *mi);
extern void    fps_2 (ModeInfo *mi);
extern void    do_fps (ModeInfo *mi);
extern void    fps_free (ModeInfo *mi);

struct fps_state {
  int x, y;
  int ascent, descent;
  GLuint font_dlist;
  Bool clear_p;
  char string[1024];

  int last_ifps;
  GLfloat last_fps;
  int frame_count;
  struct timeval prev, now;
};


static void
fps_init (ModeInfo *mi)
{
  struct fps_state *st = mi->fps_state;

  if (st) free (st);
  st = (struct fps_state *) calloc (1, sizeof(*st));
  mi->fps_state = st;

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

# ifndef HAVE_COCOA /* Xlib version */
  {
    const char *font;
    XFontStruct *f;
    Font id;
    int first, last;

    font = get_string_resource (mi->dpy, "fpsFont", "Font");

    if (!font) font = "-*-courier-bold-r-normal-*-180-*";
    f = XLoadQueryFont (mi->dpy, font);
    if (!f) f = XLoadQueryFont (mi->dpy, "fixed");

    id = f->fid;
    first = f->min_char_or_byte2;
    last = f->max_char_or_byte2;
  
    clear_gl_error ();
    st->font_dlist = glGenLists ((GLuint) last+1);
    check_gl_error ("glGenLists");

    st->ascent = f->ascent;
    st->descent = f->descent;

    glXUseXFont (id, first, last-first+1, st->font_dlist + first);
    check_gl_error ("glXUseXFont");
  }

# else  /* HAVE_COCOA */

  {
    AGLContext ctx = aglGetCurrentContext();
    GLint id    = 0;   /* 0 = system font; 1 = application font */
    Style face  = 1;   /* 0 = plain; 1=B; 2=I; 3=BI; 4=U; 5=UB; etc. */
    GLint size  = 24;
    GLint first = 32;
    GLint last  = 255;

    st->ascent  = size * 0.9;
    st->descent = size - st->ascent;

    clear_gl_error ();
    st->font_dlist = glGenLists ((GLuint) last+1);
    check_gl_error ("glGenLists");

    if (! aglUseFont (ctx, id, face, size, 
                      first, last-first+1, st->font_dlist + first)) {
      check_gl_error ("aglUseFont");
      abort();
    }
  }

# endif  /* HAVE_COCOA */

  st->x = 10;
  st->y = 10;
  if (get_boolean_resource (mi->dpy, "fpsTop", "FPSTop"))
    st->y = - (st->ascent + 10);
}

void
fps_free (ModeInfo *mi)
{
  if (mi->fps_state)
    free (mi->fps_state);
  mi->fps_state = 0;
}

static void
fps_print_string (ModeInfo *mi, GLfloat x, GLfloat y, const char *string)
{
  struct fps_state *st = mi->fps_state;
  const char *L2 = strchr (string, '\n');

  if (y < 0)
    {
      y = mi->xgwa.height + y;
      if (L2)
        y -= (st->ascent + st->descent);
    }

# ifdef DEBUG
  clear_gl_error ();
# endif

  /* Sadly, this causes a stall of the graphics pipeline (as would the
     equivalent calls to glGet*.)  But there's no way around this, short
     of having each caller set up the specific display matrix we need
     here, which would kind of defeat the purpose of centralizing this
     code in one file.
   */
  glPushAttrib (GL_TRANSFORM_BIT |  /* for matrix contents */
                GL_ENABLE_BIT |     /* for various glDisable calls */
                GL_CURRENT_BIT |    /* for glColor3f() */
                GL_LIST_BIT);       /* for glListBase() */
  {
# ifdef DEBUG
    check_gl_error ("glPushAttrib");
# endif

    /* disable lighting and texturing when drawing bitmaps!
       (glPopAttrib() restores these.)
     */
    glDisable (GL_TEXTURE_2D);
    glDisable (GL_LIGHTING);
    glDisable (GL_BLEND);
    glDisable (GL_DEPTH_TEST);
    glDisable (GL_CULL_FACE);

    /* glPopAttrib() does not restore matrix changes, so we must
       push/pop the matrix stacks to be non-intrusive there.
     */
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    {
# ifdef DEBUG
      check_gl_error ("glPushMatrix");
# endif
      glLoadIdentity();

      /* Each matrix mode has its own stack, so we need to push/pop
         them separately. */
      glMatrixMode(GL_MODELVIEW);
      glPushMatrix();
      {
# ifdef DEBUG
        check_gl_error ("glPushMatrix");
# endif
        glLoadIdentity();

        gluOrtho2D(0, mi->xgwa.width, 0, mi->xgwa.height);
# ifdef DEBUG
        check_gl_error ("gluOrtho2D");
# endif

        /* clear the background */
        if (st->clear_p)
          {
            int lines = L2 ? 2 : 1;
            glColor3f (0, 0, 0);
            glRecti (x / 2, y - st->descent,
                     mi->xgwa.width - x,
                     y + lines * (st->ascent + st->descent));
          }

        /* draw the text */
        glColor3f (1, 1, 1);
        glRasterPos2f (x, y);
        glListBase (st->font_dlist);

        if (L2)
          {
            L2++;
            glCallLists (strlen(L2), GL_UNSIGNED_BYTE, L2);
            glRasterPos2f (x, y + (st->ascent + st->descent));
            glCallLists (L2 - string - 1, GL_UNSIGNED_BYTE, string);
          }
        else
          {
            glCallLists (strlen(string), GL_UNSIGNED_BYTE, string);
          }

# ifdef DEBUG
        check_gl_error ("fps_print_string");
# endif
      }
      glPopMatrix();
    }
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();

  }
  /* clean up after our state changes */
  glPopAttrib();
# ifdef DEBUG
  check_gl_error ("glPopAttrib");
# endif
}


GLfloat
fps_1 (ModeInfo *mi)
{
  struct fps_state *st = mi->fps_state;
  if (!st)
    {
      fps_init (mi);
      st = mi->fps_state;
      strcpy (st->string, "FPS: (accumulating...)");
    }

  /* 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->now, &tzp);
# else
      gettimeofday(&st->now);
# endif

      if (st->prev.tv_sec == 0)
        st->prev = st->now;
    }

  /* If we've probed the wall-clock time, regenerate the string.
   */
  if (st->now.tv_sec != st->prev.tv_sec)
    {
      double uprev = st->prev.tv_sec + ((double) st->prev.tv_usec * 0.000001);
      double unow  =  st->now.tv_sec + ((double)  st->now.tv_usec * 0.000001);
      double fps   = st->frame_count / (unow - uprev);

      st->prev = st->now;
      st->frame_count = 0;
      st->last_ifps = fps;
      st->last_fps  = fps;

      sprintf (st->string, "FPS: %.02f", fps);

#ifndef HAVE_COCOA
      /* since there's no "-delay 0" in the Cocoa version,
         don't bother mentioning the inter-frame delay. */
      if (mi->pause != 0)
        {
          char buf[40];
          sprintf(buf, "%f", mi->pause / 1000000.0); /* FTSO C */
          while(*buf && buf[strlen(buf)-1] == '0')
            buf[strlen(buf)-1] = 0;
          if (buf[strlen(buf)-1] == '.')
            buf[strlen(buf)-1] = 0;
          sprintf(st->string + strlen(st->string),
                  " (including %s sec/frame delay)",
                  buf);
        }
#endif /* HAVE_COCOA */

      if (mi->polygon_count > 0)
        {
          unsigned long p = mi->polygon_count;
          const char *s = "";
# if 0
          if      (p >= (1024 * 1024)) p >>= 20, s = "M";
          else if (p >= 2048)          p >>= 10, s = "K";
# endif

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

  return st->last_fps;
}

void
fps_2 (ModeInfo *mi)
{
  struct fps_state *st = mi->fps_state;
  fps_print_string (mi, st->x, st->y, st->string);
}


void
do_fps (ModeInfo *mi)
{
  fps_1 (mi);   /* Lazily compute current FPS value, about once a second. */
  fps_2 (mi);   /* Print the string every frame (else nothing shows up.) */
}