[AD] X vsync emulation (timing)

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Here's a simple patch that implements the vsync-emu idea I described, i.e. incrementing a counter from the bg_man thread. With this, calling vsync() delays the program as is expected by many programs, including some of Allegro examples (e.g. exstars and exspline are back to normal). It's not the same delay that you usually expect (100Hz instead of 70Hz) but that should be fine.

As a side effect, programs that use vsync() no longer take up 100% CPU on X. It's like calling rest().

Peter
Index: src/x/xwin.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/x/xwin.c,v
retrieving revision 1.79
diff -u -r1.79 xwin.c
--- src/x/xwin.c	18 Nov 2004 03:45:10 -0000	1.79
+++ src/x/xwin.c	18 Nov 2004 04:03:15 -0000
@@ -154,6 +154,8 @@
 static COLORCONV_BLITTER_FUNC *blitter_func = NULL;
 static int use_bgr_palette_hack = FALSE; /* use BGR hack for color conversion palette? */
 
+static int vsync_emu_hack = 0;
+
 #ifndef ALLEGRO_MULTITHREADED
 int _xwin_missed_input;
 #endif
@@ -2160,15 +2162,26 @@
  */
 void _xwin_vsync(void)
 {
+   int x = vsync_emu_hack;
+
    /* This does not wait for the VBI - but it waits until X11 has synchronized,
-    * i.e. until actual changes are visible. So it has a similar effect. A
-    * better solution might be waiting a specific time to simulate a VBI (but
-    * the X11 mutex might be locked by the timer thread), or using the XSync
-    * extension.
+    * i.e. until actual changes are visible. So it has a similar effect.
     */
    XLOCK();
    XSync(_xwin.display, False);
    XUNLOCK();
+
+   /* Because so many people (including the Allegro examples) depend on vsync
+    * to slow down their program, even if they shouldn't, we emulate that
+    * aspect of vsync() too.  vsync_emu_hack is updated from the bg_man
+    * thread which runs at 100Hz.
+    */
+   while (vsync_emu_hack == x) {
+      struct timeval delay;
+      delay.tv_sec = 0;
+      delay.tv_usec = 1;
+      select(0, NULL, NULL, NULL, &delay);
+   }
 }
 
 
@@ -2398,6 +2411,9 @@
 
 void _xwin_handle_input(void)
 {
+   /* See _xwin_vsync() below */
+   vsync_emu_hack++;
+
 #ifndef ALLEGRO_MULTITHREADED
    if (_xwin.lock_count) {
       ++_xwin_missed_input;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/