Skip to contentSkip to conversation

smoother sdl display & mouse

PATCH In review Jindrich Makovicka 2 participants 3 messages Jul 24, 2007 – Jul 29, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 smoother sdl display & mouse

Hi,

the attached patch modifies the SDL display refresh code so the mouse and keyboard events are processed each 10 ms instead of 30. This causes much better mouse behavior due to lower latency.

By default, display is still refreshed only each 30 ms to prevent the increase of the cpu load, but it is possible to tune the refresh rate via the -sdl-decimate option.

Please cc:, I am not a subscriber.

Regards,

Signature · 1 line
Jindrich Makovicka
qemu/sdl.c +6 -1
diff -ur kvm-27.orig/qemu/sdl.c kvm-27/qemu/sdl.c
@@ -43,6 +43,7 @@
 static SDL_Cursor *sdl_cursor_normal;
 static SDL_Cursor *sdl_cursor_hidden;
 static int absolute_enabled = 0;
+static int decimate_counter = 0;
 
 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
 {
@@ -321,7 +322,10 @@
         sdl_update_caption();
     }
 
-    vga_hw_update();
+    if (decimate_counter == 0)
+	vga_hw_update();
+
+    decimate_counter = (decimate_counter + 1) % sdl_decimate;
 
     while (SDL_PollEvent(ev)) {
         switch (ev->type) {
@@ -504,6 +508,7 @@
     ds->dpy_update = sdl_update;
     ds->dpy_resize = sdl_resize;
     ds->dpy_refresh = sdl_refresh;
+    ds->refresh_interval = 10;
 
     sdl_resize(ds, 640, 400);
     sdl_update_caption();
qemu/vl.c +14 -2
diff -ur kvm-27.orig/qemu/vl.c kvm-27/qemu/vl.c
@@ -110,7 +110,7 @@
 #define DEFAULT_RAM_SIZE 128
 #endif
 /* in ms */
-#define GUI_REFRESH_INTERVAL 30
+#define DEFAULT_GUI_REFRESH_INTERVAL 30
 
 /* Max number of USB devices that can be specified on the commandline.  */
 #define MAX_USB_CMDLINE 8
@@ -133,6 +133,7 @@
 static DisplayState display_state;
 int nographic;
 const char* keyboard_layout = NULL;
+int sdl_decimate = 3;
 int64_t ticks_per_sec;
 int boot_device = 'c';
 int ram_size;
@@ -4137,6 +4138,7 @@
     ds->dpy_update = dumb_update;
     ds->dpy_resize = dumb_resize;
     ds->dpy_refresh = dumb_refresh;
+    ds->refresh_interval = DEFAULT_GUI_REFRESH_INTERVAL;
 }
 
 /***********************************************************/
@@ -5989,7 +5991,7 @@
 void gui_update(void *opaque)
 {
     display_state.dpy_refresh(&display_state);
-    qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
+    qemu_mod_timer(gui_timer, display_state.refresh_interval + qemu_get_clock(rt_clock));
 }
 
 struct vm_change_state_entry {
@@ -6342,6 +6344,7 @@
            "-snapshot       write to temporary files instead of disk image files\n"
 #ifdef CONFIG_SDL
            "-no-quit        disable SDL window close capability\n"
+           "-sdl-decimate n refresh display every n*10 milliseconds (default n=3)\n"
 #endif
 #ifdef TARGET_I386
            "-no-fd-bootchk  disable boot signature checking for floppy disks\n"
@@ -6541,6 +6544,9 @@
     QEMU_OPTION_no_rtc,
 #endif
     QEMU_OPTION_cpu_vendor,
+#ifdef CONFIG_SDL
+    QEMU_OPTION_sdl_decimate,
+#endif
 };
 
 typedef struct QEMUOption {
@@ -6616,6 +6622,7 @@
     { "full-screen", 0, QEMU_OPTION_full_screen },
 #ifdef CONFIG_SDL
     { "no-quit", 0, QEMU_OPTION_no_quit },
+    { "sdl-decimate", HAS_ARG, QEMU_OPTION_sdl_decimate },
 #endif
     { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
     { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
@@ -7189,6 +7196,11 @@
 	    case QEMU_OPTION_k:
 		keyboard_layout = optarg;
 		break;
+	    case QEMU_OPTION_sdl_decimate:
+		sdl_decimate = atoi(optarg);
+		if (sdl_decimate <= 0 || sdl_decimate > 5)
+		    help();
+		break;
             case QEMU_OPTION_localtime:
                 rtc_utc = 0;
                 break;
qemu/vl.h +2 -0
diff -ur kvm-27.orig/qemu/vl.h kvm-27/qemu/vl.h
@@ -156,6 +156,7 @@
 extern int graphic_height;
 extern int graphic_depth;
 extern const char *keyboard_layout;
+extern int sdl_decimate;
 extern int kqemu_allowed;
 extern int kvm_allowed;
 extern int win2k_install_hack;
@@ -901,6 +902,7 @@
     int width;
     int height;
     void *opaque;
+    int refresh_interval;
 
     void (*dpy_update)(struct DisplayState *s, int x, int y, int w, int h);
     void (*dpy_resize)(struct DisplayState *s, int w, int h);
#1 lore raw
Quoted 9 lines · Jindrich Makovicka

Hi,

the attached patch modifies the SDL display refresh code so the mouse and keyboard events are processed each 10 ms instead of 30. This causes much better mouse behavior due to lower latency.

By default, display is still refreshed only each 30 ms to prevent the increase of the cpu load, but it is possible to tune the refresh rate via the -sdl-decimate option.

Using a command line argument to change the refresh rate doesn't seem like a particularly optional solution. It think it would be better to have QEMU automatically adapt the refresh rate. eg, it would start off refreshing SDL at 30ms intervals. If it sees a keyboard or mouse event, it would increase the refresh rate to 10ms. If, say, 100ms passes without any further events, then it could decrease it to 30ms again.

That way everyone would always get the low CPU load, unless they were actaully interacting with the mouse/keyboard in which case it would tune itself for interactive response.

Regards,
Dan.

Signature · 4 lines
|=- Red Hat, Engineering, Emerging Technologies, Boston.  +1 978 392 2496 -=|
|=-           Perl modules: http://search.cpan.org/~danberr/              -=|
|=-               Projects: http://freshmeat.net/~danielpb/               -=|
|=-  GnuPG: 7D3B9505   F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505  -=| 
#2 lore raw
Quoted 11 lines · On Tue, 24 Jul 2007 19:44:12 +0100 "Daniel P. Berrange" <[email protected]>…

Using a command line argument to change the refresh rate doesn't seem like a particularly optional solution. It think it would be better to have QEMU automatically adapt the refresh rate. eg, it would start off refreshing SDL at 30ms intervals. If it sees a keyboard or mouse event, it would increase the refresh rate to 10ms. If, say, 100ms passes without any further events, then it could decrease it to 30ms again.

That way everyone would always get the low CPU load, unless they were actaully interacting with the mouse/keyboard in which case it would tune itself for interactive response.

Sounds reasonable. Updated patch attached.

Signature · 1 line
Jindrich Makovicka
qemu/sdl.c +21 -1
diff -ur kvm-33.orig/qemu/sdl.c kvm-33/qemu/sdl.c
@@ -43,6 +43,8 @@
 static SDL_Cursor *sdl_cursor_normal;
 static SDL_Cursor *sdl_cursor_hidden;
 static int absolute_enabled = 0;
+static int decimate_counter = 0;
+static int idle_counter = 0;
 
 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
 {
@@ -311,17 +313,24 @@
     vga_hw_update();
 }
 
+#define SDL_IDLE_THRESHOLD 10
+#define SDL_DECIMATE 3
+
 static void sdl_refresh(DisplayState *ds)
 {
     SDL_Event ev1, *ev = &ev1;
     int mod_state;
+    int was_idle = 1;
                      
     if (last_vm_running != vm_running) {
         last_vm_running = vm_running;
         sdl_update_caption();
     }
 
-    vga_hw_update();
+    if (idle_counter < SDL_IDLE_THRESHOLD || decimate_counter == 0)
+	vga_hw_update();
+
+    decimate_counter = (decimate_counter + 1) % SDL_DECIMATE;
 
     while (SDL_PollEvent(ev)) {
         switch (ev->type) {
@@ -330,6 +339,7 @@
             break;
         case SDL_KEYDOWN:
         case SDL_KEYUP:
+	    was_idle = 0;
             if (ev->type == SDL_KEYDOWN) {
                 mod_state = (SDL_GetModState() & gui_grab_code) ==
                     gui_grab_code;
@@ -428,6 +438,7 @@
             }
             break;
         case SDL_MOUSEMOTION:
+	    was_idle = 0;
             if (gui_grab || kbd_mouse_is_absolute() ||
                 absolute_enabled) {
                 sdl_send_mouse_event(0);
@@ -435,6 +446,7 @@
             break;
         case SDL_MOUSEBUTTONDOWN:
         case SDL_MOUSEBUTTONUP:
+	    was_idle = 0;
             {
                 SDL_MouseButtonEvent *bev = &ev->button;
                 if (!gui_grab && !kbd_mouse_is_absolute()) {
@@ -467,6 +479,13 @@
             break;
         }
     }
+
+    if (was_idle) {
+	if (idle_counter < SDL_IDLE_THRESHOLD)
+	    idle_counter++;
+    } else {
+	idle_counter = 0;
+    }
 }
 
 static void sdl_cleanup(void) 
@@ -504,6 +523,7 @@
     ds->dpy_update = sdl_update;
     ds->dpy_resize = sdl_resize;
     ds->dpy_refresh = sdl_refresh;
+    ds->refresh_interval = 10;
 
     sdl_resize(ds, 640, 400);
     sdl_update_caption();
qemu/vl.c +3 -2
diff -ur kvm-33.orig/qemu/vl.c kvm-33/qemu/vl.c
@@ -110,7 +110,7 @@
 #define DEFAULT_RAM_SIZE 128
 #endif
 /* in ms */
-#define GUI_REFRESH_INTERVAL 30
+#define DEFAULT_GUI_REFRESH_INTERVAL 30
 
 /* Max number of USB devices that can be specified on the commandline.  */
 #define MAX_USB_CMDLINE 8
@@ -4137,6 +4137,7 @@
     ds->dpy_update = dumb_update;
     ds->dpy_resize = dumb_resize;
     ds->dpy_refresh = dumb_refresh;
+    ds->refresh_interval = DEFAULT_GUI_REFRESH_INTERVAL;
 }
 
 /***********************************************************/
@@ -5995,7 +5996,7 @@
 void gui_update(void *opaque)
 {
     display_state.dpy_refresh(&display_state);
-    qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
+    qemu_mod_timer(gui_timer, display_state.refresh_interval + qemu_get_clock(rt_clock));
 }
 
 struct vm_change_state_entry {

Only in kvm-33/qemu: vl.c~

qemu/vl.h +1 -0
diff -ur kvm-33.orig/qemu/vl.h kvm-33/qemu/vl.h
@@ -907,6 +907,7 @@
     int width;
     int height;
     void *opaque;
+    int refresh_interval;
 
     void (*dpy_update)(struct DisplayState *s, int x, int y, int w, int h);
     void (*dpy_resize)(struct DisplayState *s, int w, int h);