Re: [AD] Triple Buffering under X?

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


> Does anyone think it could be useful to pursue this further? I get 
roughly 
> 50-60 FPS from the exupdate example using triple buffering, compared to 
> about 90 FPS using plain page flipping. It's still better than plain 
> double buffering though, which gives me around 20 FPS.

Unless when I actually compare things in the same resolution. Triple 
buffering gives the same FPS as page flipping for me now. Patch attached 
for those who are curious and want to take a look at it.

Evert
diff -ru allegro/src/x/xgfxdrv.c allegro-x-triple/src/x/xgfxdrv.c
--- allegro/src/x/xgfxdrv.c	2004-09-03 11:30:44.000000000 +0200
+++ allegro-x-triple/src/x/xgfxdrv.c	2004-09-03 19:36:26.000000000 +0200
@@ -36,7 +36,13 @@
    _xwin_scroll_screen,
    _xwin_vsync,
    _xwin_set_palette_range,
-   NULL, NULL, NULL,
+#ifdef ALLEGRO_MULTITHREADED
+   _xwin_request_scroll,
+   _xwin_poll_scroll,
+   NULL,
+#else
+   NULL, NULL, NULL, 
+#endif
    NULL, NULL, NULL, NULL,
    NULL, NULL,
 #ifdef ALLEGRO_XWINDOWS_WITH_XCURSOR
diff -ru allegro/src/x/xwin.c allegro-x-triple/src/x/xwin.c
--- allegro/src/x/xwin.c	2004-09-03 11:30:45.000000000 +0200
+++ allegro-x-triple/src/x/xwin.c	2004-09-03 19:48:41.000000000 +0200
@@ -45,6 +45,9 @@
 #include <X11/Xcursor/Xcursor.h>
 #endif
 
+#ifdef ALLEGRO_MULTITHREADED
+#include <pthread.h>
+#endif
 
 #ifdef ALLEGRO_XWINDOWS_WITH_XPM
 #include <X11/xpm.h>
@@ -147,6 +150,17 @@
 static COLORCONV_BLITTER_FUNC *blitter_func = NULL;
 static int use_bgr_palette_hack = FALSE; /* use BGR hack for color conversion palette? */
 
+#ifdef ALLEGRO_MULTITHREADED
+static volatile int _scroll_requested = 0;
+static volatile int _scrolled = 0;
+static volatile int _sx, _sy;
+static volatile int run_triple_emulator = 1;
+
+static pthread_t req_scroll_thread = 0;
+static pthread_cond_t req_scroll_cond = PTHREAD_COND_INITIALIZER;
+static pthread_mutex_t scroll_lock = PTHREAD_MUTEX_INITIALIZER;
+#endif
+
 #ifndef ALLEGRO_MULTITHREADED
 int _xwin_missed_input;
 #endif
@@ -269,6 +283,9 @@
 void _xwin_unwrite_line_asm (BITMAP *bmp);
 #endif
 
+#ifdef ALLEGRO_MULTITHREADED
+static void *_xwin_private_request_scroll(void *args);
+#endif
 
 
 /* _xwin_open_display:
@@ -302,7 +319,7 @@
 void _xwin_close_display(void)
 {
    Display *dpy;
-
+   
    if (!_unix_bg_man->multi_threaded) {
       XLOCK();
    }
@@ -426,6 +443,12 @@
 #else
    _xwin.hw_cursor_ok = 0;
 #endif
+
+#ifdef ALLEGRO_MULTITHREADED
+   /* Enable triple buffering */
+   if (!req_scroll_thread)
+      pthread_create(&req_scroll_thread, NULL, _xwin_private_request_scroll, NULL);
+#endif
    
    return 0;
 }
@@ -844,7 +867,7 @@
       _release_colorconv_blitter(blitter_func);
       blitter_func = NULL;
    }
-
+   
    (*_xwin_window_defaultor)();
 }
 
@@ -3150,6 +3173,66 @@
 
 
 
+/* Triple buffering emulation */
+#ifdef ALLEGRO_MULTITHREADED
+
+/* _xwin_private_request_scroll:
+ *  Handle the actual scroll from the background thread.
+ */
+static void *_xwin_private_request_scroll(void *args)
+{
+   while (run_triple_emulator) {
+      pthread_mutex_lock(&scroll_lock);
+      _scroll_requested = 0;
+      _scrolled = 0;
+      pthread_mutex_unlock(&scroll_lock);
+      
+      pthread_mutex_lock(&scroll_lock);
+         while(!_scroll_requested) {
+            pthread_cond_wait(&req_scroll_cond, &scroll_lock);
+         }
+      pthread_mutex_unlock(&scroll_lock);
+
+      _xwin_private_scroll_screen(_sx, _sy);
+   }
+   
+   return NULL;
+}
+
+
+
+/* _xwin_request_scroll:
+ *  Scroll screen, but return immediately and delegate to background thread
+ */
+int _xwin_request_scroll(int x, int y)
+{
+
+   _sx = x;
+   _sy = y;
+
+   pthread_mutex_lock(&scroll_lock);
+   _scroll_requested = 1;
+   _scrolled = 1;
+   pthread_mutex_unlock(&scroll_lock);
+   
+   pthread_cond_broadcast(&req_scroll_cond);
+   
+   return 0;
+}
+
+
+
+/* _xwin_poll_scroll:
+ *  Tell wether the last scroll  request was processed
+ */
+int _xwin_poll_scroll(void)
+{
+   return _scrolled;
+}
+#endif
+
+
+
 /* Hook functions */
 int (*_xwin_window_creator)(void) = &_xwin_private_create_window;
 void (*_xwin_window_defaultor)(void) = &_xwin_private_set_window_defaults;
diff -ru allegro/src/x/xwin.h allegro-x-triple/src/x/xwin.h
--- allegro/src/x/xwin.h	2004-08-30 18:29:32.000000000 +0200
+++ allegro-x-triple/src/x/xwin.h	2004-09-03 19:55:42.000000000 +0200
@@ -51,6 +51,11 @@
 AL_FUNC(int, _xwin_get_pointer_mapping, (unsigned char map[], int nmap));
 AL_FUNC(void, _xwin_init_keyboard_tables, (void));
 
+#ifdef ALLEGRO_MULTITHREADED
+AL_FUNC(int, _xwin_request_scroll, (int x, int y));
+AL_FUNC(int, _xwin_poll_scroll, (void));
+#endif
+
 #ifdef ALLEGRO_XWINDOWS_WITH_XCURSOR
 AL_FUNC(int, _xwin_set_mouse_sprite, (struct BITMAP *sprite, int x, int y));
 AL_FUNC(int, _xwin_show_mouse, (struct BITMAP *bmp, int x, int y));


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