if no client is connected there is no need to keep the server
surface. Throw it away to save memory.

Signed-off-by: Peter Lieven <p...@kamp.de>
---
v1->v2: don't create a dummy surface just set vd->server = NULL [Gerd]

 ui/vnc.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)

diff --git a/ui/vnc.c b/ui/vnc.c
index eba3fba..8c12ff5 100644
--- a/ui/vnc.c
+++ b/ui/vnc.c
@@ -630,8 +630,14 @@ static void vnc_dpy_update(DisplayChangeListener *dcl,
 {
     VncDisplay *vd = container_of(dcl, VncDisplay, dcl);
     struct VncSurface *s = &vd->guest;
-    int width = pixman_image_get_width(vd->server);
-    int height = pixman_image_get_height(vd->server);
+    int width, height;
+
+    if (!vd->server) {
+        return;
+    }
+
+    width = pixman_image_get_width(vd->server);
+    height = pixman_image_get_height(vd->server);
 
     vnc_set_area_dirty(s->dirty, width, height, x, y, w, h);
 }
@@ -712,8 +718,17 @@ static void vnc_dpy_switch(DisplayChangeListener *dcl,
 
     vnc_abort_display_jobs(vd);
 
-    /* server surface */
     qemu_pixman_image_unref(vd->server);
+    vd->server = NULL;
+    qemu_pixman_image_unref(vd->guest.fb);
+    vd->guest.fb = NULL;
+
+    /* if no client is connected we don't need a server surface */
+    if (QTAILQ_EMPTY(&vd->clients)) {
+        return;
+    }
+
+    /* server surface */
     vd->ds = surface;
     width = MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds),
                                         VNC_DIRTY_PIXELS_PER_BIT));
@@ -726,7 +741,6 @@ static void vnc_dpy_switch(DisplayChangeListener *dcl,
     if (ds_get_bytes_per_pixel(ds) != vd->guest.ds->pf.bytes_per_pixel)
         console_color_init(ds);
 #endif
-    qemu_pixman_image_unref(vd->guest.fb);
     vd->guest.fb = pixman_image_ref(surface->image);
     vd->guest.format = surface->format;
     memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty));
@@ -900,6 +914,10 @@ static void vnc_dpy_copy(DisplayChangeListener *dcl,
     int i, x, y, pitch, inc, w_lim, s;
     int cmp_bytes;
 
+    if (!vd->server) {
+        return;
+    }
+
     vnc_refresh_server_surface(vd);
     QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) {
         if (vnc_has_feature(vs, VNC_FEATURE_COPYRECT)) {
@@ -1213,6 +1231,9 @@ void vnc_disconnect_finish(VncState *vs)
 
     if (vs->initialized) {
         QTAILQ_REMOVE(&vs->vd->clients, vs, next);
+        if (QTAILQ_EMPTY(&vs->vd->clients)) {
+            vnc_dpy_switch(&vs->vd->dcl, NULL);
+        }
         qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier);
     }
 
@@ -3049,6 +3070,7 @@ void vnc_init_state(VncState *vs)
 {
     vs->initialized = true;
     VncDisplay *vd = vs->vd;
+    bool first_client;
 
     vs->last_x = -1;
     vs->last_y = -1;
@@ -3061,9 +3083,15 @@ void vnc_init_state(VncState *vs)
     qemu_mutex_init(&vs->output_mutex);
     vs->bh = qemu_bh_new(vnc_jobs_bh, vs);
 
+    first_client = QTAILQ_EMPTY(&vd->clients);
     QTAILQ_INSERT_TAIL(&vd->clients, vs, next);
 
-    graphic_hw_update(vd->dcl.con);
+    if (first_client) {
+        /* set/restore the correct surface in the VNC server */
+        console_select(0);
+    } else {
+        graphic_hw_update(vd->dcl.con);
+    }
 
     vnc_write(vs, "RFB 003.008\n", 12);
     vnc_flush(vs);
-- 
1.9.1


Reply via email to