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

« back to all changes in this revision

Viewing changes to hacks/glx/fps.c

  • Committer: Bazaar Package Importer
  • Author(s): Jose Luis Rivas, Tormod Volden, Jose Luis Rivas
  • Date: 2008-07-15 14:48:48 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080715144848-c6c6mhyxij0dk2p7
Tags: 5.05-3
[ Tormod Volden ]
* debian/patches/10_jwz-xscreensaver-randr-patch-3.patch:
  from upstream, addresses issues with xrandr/xinerama
  (Closes: #482385, #428797, #471920, #453708, #473681, #479715, #480231)
* fixed typo "screen < real_nscreens" in driver/lock:1527 from above patch
* drop 61_DualHead-nVidia_bug471920.patch (obsolete)
* drop 67_XineRama-mode_bug473681.patch (obsolete)
* fix m6502.o typo in hacks/Makefile.in
* refresh 53_XScreenSaver.ad.in.patch
* refresh (disabled) 60_add-ant-hack.patch

[ Jose Luis Rivas ]
* add xscreensaver-demo desktop file, thanks to Daniel Dickinson
  (Closes: #480592)
* update package descriptions (thanks jwz)
* fix categories in xscreensaver.menu
* change build-deps from xlibmesa-gl-dev to libgl1-mesa-dev,
  xutils to xutils-dev, x-dev to x11proto-core-dev.
* bump Standards-Version to 3.8.0
* add Vcs fields and Homepage to debian/control
* Flurry is not installed until the bug get fixed (Closes: #484112)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* tube, Copyright (c) 2001 Jamie Zawinski <jwz@jwz.org>
 
1
/* fps, Copyright (c) 2001-2007 Jamie Zawinski <jwz@jwz.org>
2
2
 * Utility function to draw a frames-per-second display.
3
3
 *
4
4
 * Permission to use, copy, modify, distribute, and sell this software and its
10
10
 * implied warranty.
11
11
 */
12
12
 
13
 
#include "config.h"
14
 
#include <stdlib.h>
15
 
#include <stdio.h>
 
13
#ifdef HAVE_CONFIG_H
 
14
# include "config.h"
 
15
#endif /* HAVE_CONFIG_H */
16
16
 
17
 
#include "screenhack.h"
18
17
#include "xlockmoreI.h"
19
18
 
20
 
#include <GL/gl.h>
21
 
#include <GL/glu.h>
22
 
#include <GL/glx.h>
 
19
#ifdef HAVE_COCOA
 
20
# undef usleep /* conflicts with 10.5 headers... */
 
21
# include <OpenGL/gl.h>
 
22
# include <OpenGL/glu.h>
 
23
# include <AGL/agl.h>
 
24
#else /* !HAVE_COCOA -- real Xlib */
 
25
# include <GL/gl.h>
 
26
# include <GL/glu.h>
 
27
# include <GL/glx.h>
 
28
#endif /* !HAVE_COCOA */
23
29
 
24
30
#undef DEBUG  /* Defining this causes check_gl_error() to be called inside
25
31
                 time-critical sections, which could slow things down (since
29
35
extern void clear_gl_error (void);
30
36
extern void check_gl_error (const char *type);
31
37
 
32
 
static int fps_text_x = 10;
33
 
static int fps_text_y = 10;
34
 
static int fps_ascent, fps_descent;
35
 
static GLuint font_dlist;
36
 
static Bool fps_clear_p = False;
37
 
static char fps_string[1024];
 
38
extern GLfloat fps_1 (ModeInfo *mi);
 
39
extern void    fps_2 (ModeInfo *mi);
 
40
extern void    do_fps (ModeInfo *mi);
 
41
extern void    fps_free (ModeInfo *mi);
 
42
 
 
43
struct fps_state {
 
44
  int x, y;
 
45
  int ascent, descent;
 
46
  GLuint font_dlist;
 
47
  Bool clear_p;
 
48
  char string[1024];
 
49
 
 
50
  int last_ifps;
 
51
  GLfloat last_fps;
 
52
  int frame_count;
 
53
  struct timeval prev, now;
 
54
};
 
55
 
38
56
 
39
57
static void
40
58
fps_init (ModeInfo *mi)
41
59
{
42
 
  const char *font = get_string_resource ("fpsFont", "Font");
43
 
  XFontStruct *f;
44
 
  Font id;
45
 
  int first, last;
46
 
 
47
 
  fps_clear_p = get_boolean_resource ("fpsSolid", "FPSSolid");
48
 
 
49
 
  if (!font) font = "-*-courier-bold-r-normal-*-180-*";
50
 
  f = XLoadQueryFont(mi->dpy, font);
51
 
  if (!f) f = XLoadQueryFont(mi->dpy, "fixed");
52
 
 
53
 
  id = f->fid;
54
 
  first = f->min_char_or_byte2;
55
 
  last = f->max_char_or_byte2;
 
60
  struct fps_state *st = mi->fps_state;
 
61
 
 
62
  if (st) free (st);
 
63
  st = (struct fps_state *) calloc (1, sizeof(*st));
 
64
  mi->fps_state = st;
 
65
 
 
66
  st->clear_p = get_boolean_resource (mi->dpy, "fpsSolid", "FPSSolid");
 
67
 
 
68
# ifndef HAVE_COCOA /* Xlib version */
 
69
  {
 
70
    const char *font;
 
71
    XFontStruct *f;
 
72
    Font id;
 
73
    int first, last;
 
74
 
 
75
    font = get_string_resource (mi->dpy, "fpsFont", "Font");
 
76
 
 
77
    if (!font) font = "-*-courier-bold-r-normal-*-180-*";
 
78
    f = XLoadQueryFont (mi->dpy, font);
 
79
    if (!f) f = XLoadQueryFont (mi->dpy, "fixed");
 
80
 
 
81
    id = f->fid;
 
82
    first = f->min_char_or_byte2;
 
83
    last = f->max_char_or_byte2;
56
84
  
57
 
  clear_gl_error ();
58
 
  font_dlist = glGenLists ((GLuint) last+1);
59
 
  check_gl_error ("glGenLists");
60
 
 
61
 
  fps_ascent = f->ascent;
62
 
  fps_descent = f->descent;
63
 
 
64
 
  if (get_boolean_resource ("fpsTop", "FPSTop"))
65
 
    fps_text_y = - (f->ascent + 10);
66
 
 
67
 
  glXUseXFont(id, first, last-first+1, font_dlist + first);
68
 
  check_gl_error ("glXUseXFont");
69
 
}
70
 
 
 
85
    clear_gl_error ();
 
86
    st->font_dlist = glGenLists ((GLuint) last+1);
 
87
    check_gl_error ("glGenLists");
 
88
 
 
89
    st->ascent = f->ascent;
 
90
    st->descent = f->descent;
 
91
 
 
92
    glXUseXFont (id, first, last-first+1, st->font_dlist + first);
 
93
    check_gl_error ("glXUseXFont");
 
94
  }
 
95
 
 
96
# else  /* HAVE_COCOA */
 
97
 
 
98
  {
 
99
    AGLContext ctx = aglGetCurrentContext();
 
100
    GLint id    = 0;   /* 0 = system font; 1 = application font */
 
101
    Style face  = 1;   /* 0 = plain; 1=B; 2=I; 3=BI; 4=U; 5=UB; etc. */
 
102
    GLint size  = 24;
 
103
    GLint first = 32;
 
104
    GLint last  = 255;
 
105
 
 
106
    st->ascent  = size * 0.9;
 
107
    st->descent = size - st->ascent;
 
108
 
 
109
    clear_gl_error ();
 
110
    st->font_dlist = glGenLists ((GLuint) last+1);
 
111
    check_gl_error ("glGenLists");
 
112
 
 
113
    if (! aglUseFont (ctx, id, face, size, 
 
114
                      first, last-first+1, st->font_dlist + first)) {
 
115
      check_gl_error ("aglUseFont");
 
116
      abort();
 
117
    }
 
118
  }
 
119
 
 
120
# endif  /* HAVE_COCOA */
 
121
 
 
122
  st->x = 10;
 
123
  st->y = 10;
 
124
  if (get_boolean_resource (mi->dpy, "fpsTop", "FPSTop"))
 
125
    st->y = - (st->ascent + 10);
 
126
}
 
127
 
 
128
void
 
129
fps_free (ModeInfo *mi)
 
130
{
 
131
  if (mi->fps_state)
 
132
    free (mi->fps_state);
 
133
  mi->fps_state = 0;
 
134
}
71
135
 
72
136
static void
73
137
fps_print_string (ModeInfo *mi, GLfloat x, GLfloat y, const char *string)
74
138
{
 
139
  struct fps_state *st = mi->fps_state;
75
140
  const char *L2 = strchr (string, '\n');
76
141
 
77
142
  if (y < 0)
78
143
    {
79
144
      y = mi->xgwa.height + y;
80
145
      if (L2)
81
 
        y -= (fps_ascent + fps_descent);
 
146
        y -= (st->ascent + st->descent);
82
147
    }
83
148
 
84
149
# ifdef DEBUG
136
201
# endif
137
202
 
138
203
        /* clear the background */
139
 
        if (fps_clear_p)
 
204
        if (st->clear_p)
140
205
          {
141
206
            int lines = L2 ? 2 : 1;
142
207
            glColor3f (0, 0, 0);
143
 
            glRecti (x / 2, y - fps_descent,
 
208
            glRecti (x / 2, y - st->descent,
144
209
                     mi->xgwa.width - x,
145
 
                     y + lines * (fps_ascent + fps_descent));
 
210
                     y + lines * (st->ascent + st->descent));
146
211
          }
147
212
 
148
213
        /* draw the text */
149
214
        glColor3f (1, 1, 1);
150
215
        glRasterPos2f (x, y);
151
 
        glListBase (font_dlist);
 
216
        glListBase (st->font_dlist);
152
217
 
153
218
        if (L2)
154
219
          {
155
220
            L2++;
156
221
            glCallLists (strlen(L2), GL_UNSIGNED_BYTE, L2);
157
 
            glRasterPos2f (x, y + (fps_ascent + fps_descent));
 
222
            glRasterPos2f (x, y + (st->ascent + st->descent));
158
223
            glCallLists (L2 - string - 1, GL_UNSIGNED_BYTE, string);
159
224
          }
160
225
        else
183
248
GLfloat
184
249
fps_1 (ModeInfo *mi)
185
250
{
186
 
  static Bool initted_p = False;
187
 
  static int last_ifps = -1;
188
 
  static GLfloat last_fps = -1;
189
 
  static int frame_count = 0;
190
 
  static struct timeval prev = { 0, 0 };
191
 
  static struct timeval now  = { 0, 0 };
192
 
 
193
 
  if (!initted_p)
 
251
  struct fps_state *st = mi->fps_state;
 
252
  if (!st)
194
253
    {
195
 
      initted_p = True;
196
254
      fps_init (mi);
197
 
      strcpy (fps_string, "FPS: (accumulating...)");
 
255
      st = mi->fps_state;
 
256
      strcpy (st->string, "FPS: (accumulating...)");
198
257
    }
199
258
 
200
259
  /* Every N frames (where N is approximately one second's worth of frames)
201
260
     check the wall clock.  We do this because checking the wall clock is
202
261
     a slow operation.
203
262
   */
204
 
  if (frame_count++ >= last_ifps)
 
263
  if (st->frame_count++ >= st->last_ifps)
205
264
    {
206
265
# ifdef GETTIMEOFDAY_TWO_ARGS
207
266
      struct timezone tzp;
208
 
      gettimeofday(&now, &tzp);
 
267
      gettimeofday(&st->now, &tzp);
209
268
# else
210
 
      gettimeofday(&now);
 
269
      gettimeofday(&st->now);
211
270
# endif
212
271
 
213
 
      if (prev.tv_sec == 0)
214
 
        prev = now;
 
272
      if (st->prev.tv_sec == 0)
 
273
        st->prev = st->now;
215
274
    }
216
275
 
217
276
  /* If we've probed the wall-clock time, regenerate the string.
218
277
   */
219
 
  if (now.tv_sec != prev.tv_sec)
 
278
  if (st->now.tv_sec != st->prev.tv_sec)
220
279
    {
221
 
      double uprev = prev.tv_sec + ((double) prev.tv_usec * 0.000001);
222
 
      double unow  =  now.tv_sec + ((double)  now.tv_usec * 0.000001);
223
 
      double fps   = frame_count / (unow - uprev);
224
 
 
225
 
      prev = now;
226
 
      frame_count = 0;
227
 
      last_ifps = fps;
228
 
      last_fps  = fps;
229
 
 
230
 
      sprintf (fps_string, "FPS: %.02f", fps);
231
 
 
 
280
      double uprev = st->prev.tv_sec + ((double) st->prev.tv_usec * 0.000001);
 
281
      double unow  =  st->now.tv_sec + ((double)  st->now.tv_usec * 0.000001);
 
282
      double fps   = st->frame_count / (unow - uprev);
 
283
 
 
284
      st->prev = st->now;
 
285
      st->frame_count = 0;
 
286
      st->last_ifps = fps;
 
287
      st->last_fps  = fps;
 
288
 
 
289
      sprintf (st->string, "FPS: %.02f", fps);
 
290
 
 
291
#ifndef HAVE_COCOA
 
292
      /* since there's no "-delay 0" in the Cocoa version,
 
293
         don't bother mentioning the inter-frame delay. */
232
294
      if (mi->pause != 0)
233
295
        {
234
296
          char buf[40];
237
299
            buf[strlen(buf)-1] = 0;
238
300
          if (buf[strlen(buf)-1] == '.')
239
301
            buf[strlen(buf)-1] = 0;
240
 
          sprintf(fps_string + strlen(fps_string),
 
302
          sprintf(st->string + strlen(st->string),
241
303
                  " (including %s sec/frame delay)",
242
304
                  buf);
243
305
        }
 
306
#endif /* HAVE_COCOA */
244
307
 
245
308
      if (mi->polygon_count > 0)
246
309
        {
251
314
          else if (p >= 2048)          p >>= 10, s = "K";
252
315
# endif
253
316
 
254
 
          strcat (fps_string, "\nPolys: ");
 
317
          strcat (st->string, "\nPolys: ");
255
318
          if (p >= 1000000)
256
 
            sprintf (fps_string + strlen(fps_string), "%lu,%03lu,%03lu%s",
 
319
            sprintf (st->string + strlen(st->string), "%lu,%03lu,%03lu%s",
257
320
                     (p / 1000000), ((p / 1000) % 1000), (p % 1000), s);
258
321
          else if (p >= 1000)
259
 
            sprintf (fps_string + strlen(fps_string), "%lu,%03lu%s",
 
322
            sprintf (st->string + strlen(st->string), "%lu,%03lu%s",
260
323
                     (p / 1000), (p % 1000), s);
261
324
          else
262
 
            sprintf (fps_string + strlen(fps_string), "%lu%s", p, s);
 
325
            sprintf (st->string + strlen(st->string), "%lu%s", p, s);
263
326
        }
264
327
    }
265
328
 
266
 
  return last_fps;
 
329
  return st->last_fps;
267
330
}
268
331
 
269
332
void
270
333
fps_2 (ModeInfo *mi)
271
334
{
272
 
  fps_print_string (mi, fps_text_x, fps_text_y, fps_string);
 
335
  struct fps_state *st = mi->fps_state;
 
336
  fps_print_string (mi, st->x, st->y, st->string);
273
337
}
274
338
 
275
339