All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] DisplayState interface change
@ 2008-11-17 12:44 Stefano Stabellini
  2008-11-17 14:37 ` Anthony Liguori
  0 siblings, 1 reply; 3+ messages in thread
From: Stefano Stabellini @ 2008-11-17 12:44 UTC (permalink / raw)
  To: qemu-devel

Hi all,
this patch changes the DisplayState interface adding support for
multiple frontends at the same time (sdl and vnc) and implements most of
the benefit of the shared_buf patch without the added complexity.

Currently DisplayState is managed by sdl (or vnc) and sdl (or vnc) is
also responsible for allocating the data and setting the depth.
Vga.c (or another backend) will do any necessary conversion.

The idea is to change it so that is vga.c (or another backend) that
fully manages the DisplayState interface allocating data and setting the
depth (either 16 or 32 bit, if the guest uses a different resolution or
is in text mode, vga.c (or another backend) is in charge of doing the
conversion seamlessly).

The other idea is that DisplayState supports *multiple* frontends like
sdl and vnc; each of them can register some callbacks to be called when
a display event occurs.

The shared_buf flag is still there to advertise that the buffer is
currently not allocated but points directly to video ram.
It would be nice to get rid of it but it is still necessary due to
multiple consoles that can share the same DisplayState.
It is probably possible to further simplify this patch making different
assumptions in console.c, any suggestion is welcome.

I hope you think this patch is more elegant and functional than the
previous shared_buf patch, enough to be merged soon :)

I am very open to any suggestion and (constructive) critics.

Cheers,

Stefano Stabellini

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>

---

diff -r 5c78dd111aae console.c
--- a/console.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/console.c	Mon Nov 17 12:15:40 2008 +0000
@@ -126,6 +126,8 @@
     void *hw;
 
     int g_width, g_height;
+    int g_depth;
+    int g_linesize;
     int width;
     int height;
     int total_height;
@@ -1046,10 +1048,18 @@
         return;
     s = consoles[index];
     if (s) {
+        DisplayState *ds = s->ds;
         active_console = s;
-        if (s->console_type != TEXT_CONSOLE && s->g_width && s->g_height
-            && (s->g_width != s->ds->width || s->g_height != s->ds->height))
-            dpy_resize(s->ds, s->g_width, s->g_height);
+        ds->depth = s->g_depth;
+        ds->linesize = s->g_linesize;
+        ds->width = s->g_width;
+        ds->height = s->g_height;
+        if (ds->shared_buf) {
+            ds->shared_buf = 0;
+            ds->data = 0;
+        }
+        ds->data_resize(ds);
+        dpy_resize(s->ds);
         vga_hw_invalidate();
     }
 }
@@ -1158,15 +1168,6 @@
 {
     TextConsole *s = (TextConsole *) opaque;
 
-    if (s->g_width != s->ds->width || s->g_height != s->ds->height) {
-        if (s->console_type == TEXT_CONSOLE_FIXED_SIZE)
-            dpy_resize(s->ds, s->g_width, s->g_height);
-        else {
-            s->g_width = s->ds->width;
-            s->g_height = s->ds->height;
-            text_console_resize(s);
-        }
-    }
     console_refresh(s);
 }
 
@@ -1247,6 +1248,11 @@
     return s;
 }
 
+int is_active_console(QEMUConsole *s)
+{
+    return (active_console == s);
+}
+
 int is_graphic_console(void)
 {
     return active_console && active_console->console_type == GRAPHIC_CONSOLE;
@@ -1321,6 +1327,8 @@
     }
     s->g_width = width;
     s->g_height = height;
+    s->g_linesize = 4 * width;
+    s->g_depth = 32;
 
     s->hw_invalidate = text_console_invalidate;
     s->hw_text_update = text_console_update;
@@ -1344,15 +1352,33 @@
     return chr;
 }
 
-void qemu_console_resize(QEMUConsole *console, int width, int height)
+void qemu_console_resize(QEMUConsole *console, int width, int height,
+                         int depth, int linesize, uint8_t *data)
 {
-    if (console->g_width != width || console->g_height != height
-        || !console->ds->data) {
-        console->g_width = width;
-        console->g_height = height;
-        if (active_console == console) {
-            dpy_resize(console->ds, width, height);
+    console->g_width = width;
+    console->g_height = height;
+    console->g_depth = depth;
+    console->g_linesize = linesize;
+    if (active_console == console) {
+        DisplayState *ds = console->ds;
+        ds->depth = depth;
+        ds->linesize = linesize;
+        ds->width = width;
+        ds->height = height;
+        if (data != NULL) {
+            if (!ds->shared_buf) {
+                qemu_free(ds->data);
+                ds->shared_buf = 1;
+            }
+            ds->data = data;
+        } else {
+            if (ds->shared_buf) {
+                ds->data = 0;
+                ds->shared_buf = 0;
+            }
+            ds->data_resize(ds);
         }
+        dpy_resize(console->ds);
     }
 }
 
@@ -1360,12 +1386,6 @@
                 int dst_x, int dst_y, int w, int h)
 {
     if (active_console == console) {
-        if (console->ds->dpy_copy)
-            console->ds->dpy_copy(console->ds,
-                            src_x, src_y, dst_x, dst_y, w, h);
-        else {
-            /* TODO */
-            console->ds->dpy_update(console->ds, dst_x, dst_y, w, h);
-        }
+            dpy_copy(console->ds, src_x, src_y, dst_x, dst_y, w, h);
     }
 }
diff -r 5c78dd111aae console.h
--- a/console.h	Tue Nov 04 09:04:41 2008 +0000
+++ b/console.h	Mon Nov 17 12:15:40 2008 +0000
@@ -73,45 +73,111 @@
 
 /* consoles */
 
-struct DisplayState {
-    uint8_t *data;
-    int linesize;
-    int depth;
-    int bgr; /* BGR color order instead of RGB. Only valid for depth == 32 */
-    int width;
-    int height;
-    void *opaque;
-    struct QEMUTimer *gui_timer;
+struct DisplayChangeListener {
+    int idle;
     uint64_t gui_timer_interval;
-    int idle; /* there is nothing to update (window invisible), set by vnc/sdl */
 
     void (*dpy_update)(struct DisplayState *s, int x, int y, int w, int h);
-    void (*dpy_resize)(struct DisplayState *s, int w, int h);
+    void (*dpy_resize)(struct DisplayState *s);
+    void (*dpy_setdata)(struct DisplayState *s);
     void (*dpy_refresh)(struct DisplayState *s);
     void (*dpy_copy)(struct DisplayState *s, int src_x, int src_y,
                      int dst_x, int dst_y, int w, int h);
     void (*dpy_fill)(struct DisplayState *s, int x, int y,
                      int w, int h, uint32_t c);
     void (*dpy_text_cursor)(struct DisplayState *s, int x, int y);
+
+    struct DisplayChangeListener *next;
+};
+
+struct DisplayState {
+    uint8_t *data;
+    int shared_buf;
+    int linesize;
+    int depth;
+    int width;
+    int height;
+    void *opaque;
+    struct QEMUTimer *gui_timer;
+
+    struct DisplayChangeListener* listeners;
+
     void (*mouse_set)(int x, int y, int on);
     void (*cursor_define)(int width, int height, int bpp, int hot_x, int hot_y,
                           uint8_t *image, uint8_t *mask);
+
+    void (*data_resize)(struct DisplayState *s);
 };
+
+static inline void register_displaychangelistener(DisplayState *ds, DisplayChangeListener *dcl)
+{
+    dcl->next = ds->listeners;
+    ds->listeners = dcl;
+}
 
 static inline void dpy_update(DisplayState *s, int x, int y, int w, int h)
 {
-    s->dpy_update(s, x, y, w, h);
+    struct DisplayChangeListener *dcl = s->listeners;
+    while (dcl != NULL) {
+        dcl->dpy_update(s, x, y, w, h);
+        dcl = dcl->next;
+    }
 }
 
-static inline void dpy_resize(DisplayState *s, int w, int h)
+static inline void dpy_resize(DisplayState *s)
 {
-    s->dpy_resize(s, w, h);
+    struct DisplayChangeListener *dcl = s->listeners;
+    while (dcl != NULL) {
+        dcl->dpy_resize(s);
+        dcl = dcl->next;
+    }
 }
 
-static inline void dpy_cursor(DisplayState *s, int x, int y)
+static inline void dpy_setdata(DisplayState *s)
 {
-    if (s->dpy_text_cursor)
-        s->dpy_text_cursor(s, x, y);
+    struct DisplayChangeListener *dcl = s->listeners;
+    while (dcl != NULL) {
+        if (dcl->dpy_setdata) dcl->dpy_setdata(s);
+        dcl = dcl->next;
+    }
+}
+
+static inline void dpy_refresh(DisplayState *s)
+{
+    struct DisplayChangeListener *dcl = s->listeners;
+    while (dcl != NULL) {
+        if (dcl->dpy_refresh) dcl->dpy_refresh(s);
+        dcl = dcl->next;
+    }
+}
+
+static inline void dpy_copy(struct DisplayState *s, int src_x, int src_y,
+                             int dst_x, int dst_y, int w, int h) {
+    struct DisplayChangeListener *dcl = s->listeners;
+    while (dcl != NULL) {
+        if (dcl->dpy_copy)
+            dcl->dpy_copy(s, src_x, src_y, dst_x, dst_y, w, h);
+        else /* TODO */
+            dcl->dpy_update(s, dst_x, dst_y, w, h);
+        dcl = dcl->next;
+    }
+}
+
+static inline void dpy_fill(struct DisplayState *s, int x, int y,
+                             int w, int h, uint32_t c) {
+    struct DisplayChangeListener *dcl = s->listeners;
+    while (dcl != NULL) {
+        if (dcl->dpy_fill) dcl->dpy_fill(s, x, y, w, h, c);
+        dcl = dcl->next;
+    }
+}
+
+static inline void dpy_cursor(struct DisplayState *s, int x, int y) {
+    struct DisplayChangeListener *dcl = s->listeners;
+    while (dcl != NULL) {
+        if (dcl->dpy_text_cursor) dcl->dpy_text_cursor(s, x, y);
+        dcl = dcl->next;
+    }
 }
 
 typedef unsigned long console_ch_t;
@@ -136,11 +202,13 @@
 void vga_hw_text_update(console_ch_t *chardata);
 
 int is_graphic_console(void);
+int is_active_console(QEMUConsole *s);
 int is_fixedsize_console(void);
 CharDriverState *text_console_init(DisplayState *ds, const char *p);
 void console_select(unsigned int index);
 void console_color_init(DisplayState *ds);
-void qemu_console_resize(QEMUConsole *console, int width, int height);
+void qemu_console_resize(QEMUConsole *console, int width, int height,
+                         int depth, int linesize, uint8_t *data);
 void qemu_console_copy(QEMUConsole *console, int src_x, int src_y,
                 int dst_x, int dst_y, int w, int h);
 
diff -r 5c78dd111aae curses.c
--- a/curses.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/curses.c	Mon Nov 17 12:15:40 2008 +0000
@@ -97,13 +97,13 @@
     }
 }
 
-static void curses_resize(DisplayState *ds, int w, int h)
+static void curses_resize(DisplayState *ds)
 {
-    if (w == gwidth && h == gheight)
+    if (ds->width == gwidth && ds->height == gheight)
         return;
 
-    gwidth = w;
-    gheight = h;
+    gwidth = ds->width;
+    gheight = ds->height;
 
     curses_calc_pad();
 }
@@ -336,8 +336,14 @@
     }
 }
 
+static void data_resize(DisplayState *ds)
+{
+    ds->data = (void *) screen;
+}
+
 void curses_display_init(DisplayState *ds, int full_screen)
 {
+    DisplayChangeListener *dcl;
 #ifndef _WIN32
     if (!isatty(1)) {
         fprintf(stderr, "We need a terminal output\n");
@@ -357,18 +363,19 @@
 #endif
 #endif
 
-    ds->data = (void *) screen;
-    ds->linesize = 0;
-    ds->depth = 0;
-    ds->width = 640;
-    ds->height = 400;
-    ds->dpy_update = curses_update;
-    ds->dpy_resize = curses_resize;
-    ds->dpy_refresh = curses_refresh;
-    ds->dpy_text_cursor = curses_cursor_position;
+    dcl = (DisplayChangeListener *) qemu_mallocz(sizeof(DisplayChangeListener));
+    if (!dcl)
+        exit(1);
+    dcl->dpy_update = curses_update;
+    dcl->dpy_resize = curses_resize;
+    dcl->dpy_refresh = curses_refresh;
+    dcl->dpy_text_cursor = curses_cursor_position;
+    register_displaychangelistener(ds, dcl);
+    ds->data_resize = data_resize;
+    data_resize(ds);
 
     invalidate = 1;
 
     /* Standard VGA initial text mode dimensions */
-    curses_resize(ds, 80, 25);
+    curses_resize(ds);
 }
diff -r 5c78dd111aae hw/blizzard.c
--- a/hw/blizzard.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/blizzard.c	Mon Nov 17 12:15:40 2008 +0000
@@ -897,7 +897,7 @@
 
     if (s->x != s->state->width || s->y != s->state->height) {
         s->invalidate = 1;
-        qemu_console_resize(s->console, s->x, s->y);
+        qemu_console_resize(s->console, s->x, s->y, 32, 4 * s->x, NULL);
     }
 
     if (s->invalidate) {
diff -r 5c78dd111aae hw/cirrus_vga.c
--- a/hw/cirrus_vga.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/cirrus_vga.c	Mon Nov 17 12:15:40 2008 +0000
@@ -788,22 +788,16 @@
     if (BLTUNSAFE(s))
         return 0;
 
-    if (s->ds->dpy_copy) {
-	cirrus_do_copy(s, s->cirrus_blt_dstaddr - s->start_addr,
-		       s->cirrus_blt_srcaddr - s->start_addr,
-		       s->cirrus_blt_width, s->cirrus_blt_height);
-    } else {
-	(*s->cirrus_rop) (s, s->vram_ptr +
-                (s->cirrus_blt_dstaddr & s->cirrus_addr_mask),
-			  s->vram_ptr +
-                (s->cirrus_blt_srcaddr & s->cirrus_addr_mask),
-			  s->cirrus_blt_dstpitch, s->cirrus_blt_srcpitch,
-			  s->cirrus_blt_width, s->cirrus_blt_height);
+    (*s->cirrus_rop) (s, s->vram_ptr +
+            (s->cirrus_blt_dstaddr & s->cirrus_addr_mask),
+            s->vram_ptr +
+            (s->cirrus_blt_srcaddr & s->cirrus_addr_mask),
+            s->cirrus_blt_dstpitch, s->cirrus_blt_srcpitch,
+            s->cirrus_blt_width, s->cirrus_blt_height);
 
-	cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
-				 s->cirrus_blt_dstpitch, s->cirrus_blt_width,
-				 s->cirrus_blt_height);
-    }
+    cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
+            s->cirrus_blt_dstpitch, s->cirrus_blt_width,
+            s->cirrus_blt_height);
 
     return 1;
 }
diff -r 5c78dd111aae hw/g364fb.c
--- a/hw/g364fb.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/g364fb.c	Mon Nov 17 12:15:40 2008 +0000
@@ -132,7 +132,7 @@
         full_update = 1;
     }
     if (s->scr_width != s->ds->width || s->scr_height != s->ds->height) {
-        qemu_console_resize(s->console, s->scr_width, s->scr_height);
+        qemu_console_resize(s->console, s->scr_width, s->scr_height, 32, 4 * s->scr_width, NULL);
         full_update = 1;
     }
     switch(graphic_mode) {
diff -r 5c78dd111aae hw/jazz_led.c
--- a/hw/jazz_led.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/jazz_led.c	Mon Nov 17 12:15:40 2008 +0000
@@ -292,7 +292,7 @@
     char buf[2];
 
     dpy_cursor(s->ds, -1, -1);
-    qemu_console_resize(s->console, 2, 1);
+    qemu_console_resize(s->console, 2, 1, 32, 4 * 2, NULL);
 
     /* TODO: draw the segments */
     snprintf(buf, 2, "%02hhx\n", s->segments);
@@ -322,5 +322,5 @@
                                      jazz_led_invalidate_display,
                                      jazz_led_screen_dump,
                                      jazz_led_text_update, s);
-    qemu_console_resize(s->console, 60, 80);
+    qemu_console_resize(s->console, 60, 80, 32, 4 * 60, NULL);
 }
diff -r 5c78dd111aae hw/musicpal.c
--- a/hw/musicpal.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/musicpal.c	Mon Nov 17 12:15:40 2008 +0000
@@ -930,7 +930,7 @@
 
     s->console = graphic_console_init(ds, lcd_refresh, lcd_invalidate,
                                       NULL, NULL, s);
-    qemu_console_resize(s->console, 128*3, 64*3);
+    qemu_console_resize(s->console, 128*3, 64*3, 32, 4*128*3, NULL);
 }
 
 /* PIC register offsets */
diff -r 5c78dd111aae hw/omap_lcdc.c
--- a/hw/omap_lcdc.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/omap_lcdc.c	Mon Nov 17 12:15:40 2008 +0000
@@ -177,7 +177,7 @@
     if (width != omap_lcd->state->width ||
             omap_lcd->height != omap_lcd->state->height) {
         qemu_console_resize(omap_lcd->console,
-                            omap_lcd->width, omap_lcd->height);
+                            omap_lcd->width, omap_lcd->height, 32, 4 * omap_lcd->width, NULL);
         omap_lcd->invalidate = 1;
     }
 
diff -r 5c78dd111aae hw/pl110.c
--- a/hw/pl110.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/pl110.c	Mon Nov 17 12:15:40 2008 +0000
@@ -272,7 +272,7 @@
 {
     if (width != s->cols || height != s->rows) {
         if (pl110_enabled(s)) {
-            qemu_console_resize(s->console, width, height);
+            qemu_console_resize(s->console, width, height, 32, 4 * width, NULL);
         }
     }
     s->cols = width;
@@ -389,7 +389,7 @@
         s->cr = val;
         s->bpp = (val >> 1) & 7;
         if (pl110_enabled(s)) {
-            qemu_console_resize(s->console, s->cols, s->rows);
+            qemu_console_resize(s->console, s->cols, s->rows, 32, 4 * s->cols, NULL);
         }
         break;
     case 10: /* LCDICR */
diff -r 5c78dd111aae hw/pxa2xx_lcd.c
--- a/hw/pxa2xx_lcd.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/pxa2xx_lcd.c	Mon Nov 17 12:15:40 2008 +0000
@@ -795,9 +795,9 @@
 
     if (width != s->xres || height != s->yres) {
         if (s->orientation)
-            qemu_console_resize(s->console, height, width);
+            qemu_console_resize(s->console, height, width, 32, 4 * height, NULL);
         else
-            qemu_console_resize(s->console, width, height);
+            qemu_console_resize(s->console, width, height, 32, 4 * width, NULL);
         s->invalidated = 1;
         s->xres = width;
         s->yres = height;
diff -r 5c78dd111aae hw/ssd0303.c
--- a/hw/ssd0303.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/ssd0303.c	Mon Nov 17 12:15:40 2008 +0000
@@ -318,6 +318,6 @@
     s->console = graphic_console_init(ds, ssd0303_update_display,
                                       ssd0303_invalidate_display,
                                       NULL, NULL, s);
-    qemu_console_resize(s->console, 96 * MAGNIFY, 16 * MAGNIFY);
+    qemu_console_resize(s->console, 96 * MAGNIFY, 16 * MAGNIFY, 32, 4 * 96 * MAGNIFY, NULL);
     register_savevm("ssd0303_oled", -1, 1, ssd0303_save, ssd0303_load, s);
 }
diff -r 5c78dd111aae hw/ssd0323.c
--- a/hw/ssd0323.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/ssd0323.c	Mon Nov 17 12:15:40 2008 +0000
@@ -334,7 +334,7 @@
     s->console = graphic_console_init(ds, ssd0323_update_display,
                                       ssd0323_invalidate_display,
                                       NULL, NULL, s);
-    qemu_console_resize(s->console, 128 * MAGNIFY, 64 * MAGNIFY);
+    qemu_console_resize(s->console, 128 * MAGNIFY, 64 * MAGNIFY, 32, 4 * 128 * MAGNIFY, NULL);
 
     cmd = qemu_allocate_irqs(ssd0323_cd, s, 1);
     *cmd_p = *cmd;
diff -r 5c78dd111aae hw/tc6393xb.c
--- a/hw/tc6393xb.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/tc6393xb.c	Mon Nov 17 12:15:40 2008 +0000
@@ -486,7 +486,7 @@
         full_update = 1;
     }
     if (s->scr_width != s->ds->width || s->scr_height != s->ds->height) {
-        qemu_console_resize(s->console, s->scr_width, s->scr_height);
+        qemu_console_resize(s->console, s->scr_width, s->scr_height, 32, 4 * s->scr_width, NULL);
         full_update = 1;
     }
     if (s->blanked)
diff -r 5c78dd111aae hw/tcx.c
--- a/hw/tcx.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/tcx.c	Mon Nov 17 12:15:40 2008 +0000
@@ -570,7 +570,7 @@
     register_savevm("tcx", addr, 4, tcx_save, tcx_load, s);
     qemu_register_reset(tcx_reset, s);
     tcx_reset(s);
-    qemu_console_resize(s->console, width, height);
+    qemu_console_resize(s->console, width, height, 32, 4 * width, NULL);
 }
 
 static void tcx_screen_dump(void *opaque, const char *filename)
diff -r 5c78dd111aae hw/vga.c
--- a/hw/vga.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/vga.c	Mon Nov 17 12:15:40 2008 +0000
@@ -1156,19 +1156,10 @@
     case 8:
         return 0;
     case 15:
-        if (s->bgr)
-            return 5;
-        else
             return 1;
     case 16:
-        if (s->bgr)
-            return 6;
-        else
             return 2;
     case 32:
-        if (s->bgr)
-            return 4;
-        else
             return 3;
     }
 }
@@ -1221,6 +1212,10 @@
     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
 };
+
+typedef unsigned int rgb_to_pixel_dup_func(unsigned int r, unsigned int g, unsigned b);
+
+static rgb_to_pixel_dup_func *rgb_to_pixel_dup_table[NB_DEPTHS];
 
 /*
  * Text mode update
@@ -1242,9 +1237,6 @@
     uint32_t *ch_attr_ptr;
     vga_draw_glyph8_func *vga_draw_glyph8;
     vga_draw_glyph9_func *vga_draw_glyph9;
-
-    full_update |= update_palette16(s);
-    palette = s->last_palette;
 
     /* compute font data address (in plane 2) */
     v = s->sr[3];
@@ -1279,7 +1271,6 @@
         cw = 9;
     if (s->sr[1] & 0x08)
         cw = 16; /* NOTE: no 18 pixel wide */
-    x_incr = cw * ((s->ds->depth + 7) >> 3);
     width = (s->cr[0x01] + 1);
     if (s->cr[0x06] == 100) {
         /* ugly hack for CGA 160x100x16 - explain me the logic */
@@ -1296,16 +1287,24 @@
     }
 
     if (width != s->last_width || height != s->last_height ||
-        cw != s->last_cw || cheight != s->last_ch) {
+        cw != s->last_cw || cheight != s->last_ch || s->last_depth) {
         s->last_scr_width = width * cw;
         s->last_scr_height = height * cheight;
-        qemu_console_resize(s->console, s->last_scr_width, s->last_scr_height);
+        qemu_console_resize(s->console, s->last_scr_width, s->last_scr_height,
+                            32, s->last_scr_width * 4, NULL);
+        s->last_depth = 0;
         s->last_width = width;
         s->last_height = height;
         s->last_ch = cheight;
         s->last_cw = cw;
         full_update = 1;
     }
+    s->rgb_to_pixel =
+        rgb_to_pixel_dup_table[get_depth_index(s->ds)];
+    full_update |= update_palette16(s);
+    palette = s->last_palette;
+    x_incr = cw * ((s->ds->depth + 7) >> 3);
+
     cursor_offset = ((s->cr[0x0e] << 8) | s->cr[0x0f]) - s->start_addr;
     if (cursor_offset != s->cursor_offset ||
         s->cr[0xa] != s->cursor_start ||
@@ -1497,8 +1496,6 @@
     vga_draw_line32_16bgr,
 };
 
-typedef unsigned int rgb_to_pixel_dup_func(unsigned int r, unsigned int g, unsigned b);
-
 static rgb_to_pixel_dup_func *rgb_to_pixel_dup_table[NB_DEPTHS] = {
     rgb_to_pixel8_dup,
     rgb_to_pixel15_dup,
@@ -1561,7 +1558,7 @@
  */
 static void vga_draw_graphic(VGAState *s, int full_update)
 {
-    int y1, y, update, page_min, page_max, linesize, y_start, double_scan, mask;
+    int y1, y, update, page_min, page_max, linesize, y_start, double_scan, mask, depth;
     int width, height, shift_control, line_offset, page0, page1, bwidth, bits;
     int disp_width, multi_scan, multi_run;
     uint8_t *d;
@@ -1641,16 +1638,33 @@
     }
     vga_draw_line = vga_draw_line_table[v * NB_DEPTHS + get_depth_index(s->ds)];
 
-    if (disp_width != s->last_width ||
-        height != s->last_height) {
-        qemu_console_resize(s->console, disp_width, height);
+    depth = s->get_bpp(s);
+    if (s->line_offset != s->last_line_offset ||
+        disp_width != s->last_width ||
+        height != s->last_height ||
+        s->last_depth != depth) {
+        if (depth == 16 || depth == 32)
+            qemu_console_resize(s->console, disp_width, height, depth,
+                                s->line_offset, s->vram_ptr + (s->start_addr * 4)); 
+        else
+            qemu_console_resize(s->console, disp_width, height, 32, 4 * width, NULL); 
         s->last_scr_width = disp_width;
         s->last_scr_height = height;
         s->last_width = disp_width;
         s->last_height = height;
+        s->last_line_offset = s->line_offset;
+        s->last_depth = depth;
         full_update = 1;
+    } else if (is_active_console(s->console) && s->ds->shared_buf &&
+               (full_update || s->ds->data != s->vram_ptr + (s->start_addr * 4))) {
+        s->ds->data = s->vram_ptr + (s->start_addr * 4);
+        dpy_setdata(s->ds);
     }
-    if (s->cursor_invalidate)
+
+    s->rgb_to_pixel =
+        rgb_to_pixel_dup_table[get_depth_index(s->ds)];
+
+    if (!s->ds->shared_buf && s->cursor_invalidate)
         s->cursor_invalidate(s);
 
     line_offset = s->line_offset;
@@ -1696,9 +1710,11 @@
                 page_min = page0;
             if (page1 > page_max)
                 page_max = page1;
-            vga_draw_line(s, d, s->vram_ptr + addr, width);
-            if (s->cursor_draw_line)
-                s->cursor_draw_line(s, d, y);
+            if (!s->ds->shared_buf) {
+                vga_draw_line(s, d, s->vram_ptr + addr, width);
+                if (s->cursor_draw_line)
+                    s->cursor_draw_line(s, d, y);
+            }
         } else {
             if (y_start >= 0) {
                 /* flush to display */
@@ -1743,6 +1759,8 @@
         return;
     if (s->last_scr_width <= 0 || s->last_scr_height <= 0)
         return;
+    s->rgb_to_pixel =
+        rgb_to_pixel_dup_table[get_depth_index(s->ds)];
     if (s->ds->depth == 8)
         val = s->rgb_to_pixel(0, 0, 0);
     else
@@ -1769,9 +1787,6 @@
     if (s->ds->depth == 0) {
         /* nothing to do */
     } else {
-        s->rgb_to_pixel =
-            rgb_to_pixel_dup_table[get_depth_index(s->ds)];
-
         full_update = 0;
         if (!(s->ar_index & 0x20)) {
             graphic_mode = GMODE_BLANK;
@@ -1879,7 +1894,7 @@
             cw != s->last_cw || cheight != s->last_ch) {
             s->last_scr_width = width * cw;
             s->last_scr_height = height * cheight;
-            qemu_console_resize(s->console, width, height);
+            qemu_console_resize(s->console, width, height, 32, 4 * width, NULL);
             s->last_width = width;
             s->last_height = height;
             s->last_ch = cheight;
@@ -1960,7 +1975,7 @@
     s->last_width = 60;
     s->last_height = height = 3;
     dpy_cursor(s->ds, -1, -1);
-    qemu_console_resize(s->console, s->last_width, height);
+    qemu_console_resize(s->console, s->last_width, height, 32, s->last_width * 4, NULL);
 
     for (dst = chardata, i = 0; i < s->last_width * height; i ++)
         console_write_ch(dst ++, ' ');
@@ -2393,12 +2408,8 @@
 {
 }
 
-static void vga_save_dpy_resize(DisplayState *s, int w, int h)
+static void vga_save_dpy_resize(DisplayState *s)
 {
-    s->linesize = w * 4;
-    s->data = qemu_malloc(h * s->linesize);
-    vga_save_w = w;
-    vga_save_h = h;
 }
 
 static void vga_save_dpy_refresh(DisplayState *s)
@@ -2440,16 +2451,19 @@
 {
     VGAState *s = (VGAState *)opaque;
     DisplayState *saved_ds, ds1, *ds = &ds1;
+    DisplayChangeListener dcl;
 
     /* XXX: this is a little hackish */
     vga_invalidate_display(s);
     saved_ds = s->ds;
 
     memset(ds, 0, sizeof(DisplayState));
-    ds->dpy_update = vga_save_dpy_update;
-    ds->dpy_resize = vga_save_dpy_resize;
-    ds->dpy_refresh = vga_save_dpy_refresh;
+    memset(&dcl, 0, sizeof(DisplayChangeListener));
+    dcl.dpy_update = vga_save_dpy_update;
+    dcl.dpy_resize = vga_save_dpy_resize;
+    dcl.dpy_refresh = vga_save_dpy_refresh;
     ds->depth = 32;
+    register_displaychangelistener(ds, &dcl);
 
     s->ds = ds;
     s->graphic_mode = -1;
@@ -2460,5 +2474,6 @@
                  s->ds->linesize);
         qemu_free(ds->data);
     }
+    ds->listeners = dcl.next;
     s->ds = saved_ds;
 }
diff -r 5c78dd111aae hw/vga_int.h
--- a/hw/vga_int.h	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/vga_int.h	Mon Nov 17 12:15:40 2008 +0000
@@ -149,9 +149,11 @@
     uint32_t line_compare;                                              \
     uint32_t start_addr;                                                \
     uint32_t plane_updated;                                             \
+    uint32_t last_line_offset;                                          \
     uint8_t last_cw, last_ch;                                           \
     uint32_t last_width, last_height; /* in chars or pixels */          \
     uint32_t last_scr_width, last_scr_height; /* in pixels */           \
+    uint32_t last_depth; /* in bits */                                  \
     uint8_t cursor_start, cursor_end;                                   \
     uint32_t cursor_offset;                                             \
     unsigned int (*rgb_to_pixel)(unsigned int r,                        \
diff -r 5c78dd111aae hw/vmware_vga.c
--- a/hw/vmware_vga.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/hw/vmware_vga.c	Mon Nov 17 12:15:40 2008 +0000
@@ -877,7 +877,7 @@
     if (s->new_width != s->width || s->new_height != s->height) {
         s->width = s->new_width;
         s->height = s->new_height;
-        qemu_console_resize(s->console, s->width, s->height);
+        qemu_console_resize(s->console, s->width, s->height, 32, 4 * s->width, NULL);
         s->invalidated = 1;
     }
 }
diff -r 5c78dd111aae qemu-common.h
--- a/qemu-common.h	Tue Nov 04 09:04:41 2008 +0000
+++ b/qemu-common.h	Mon Nov 17 12:15:40 2008 +0000
@@ -96,6 +96,7 @@
 
 void *qemu_malloc(size_t size);
 void *qemu_realloc(void *ptr, size_t size);
+void *qemu_reallocz(void *ptr, size_t size);
 void *qemu_mallocz(size_t size);
 void qemu_free(void *ptr);
 char *qemu_strdup(const char *str);
@@ -128,6 +129,7 @@
 typedef struct AudioState AudioState;
 typedef struct BlockDriverState BlockDriverState;
 typedef struct DisplayState DisplayState;
+typedef struct DisplayChangeListener DisplayChangeListener;
 typedef struct TextConsole TextConsole;
 typedef TextConsole QEMUConsole;
 typedef struct CharDriverState CharDriverState;
diff -r 5c78dd111aae qemu-malloc.c
--- a/qemu-malloc.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/qemu-malloc.c	Mon Nov 17 12:15:40 2008 +0000
@@ -43,6 +43,16 @@
     return realloc(ptr, size);
 }
 
+void *qemu_reallocz(void *ptr, size_t size)
+{
+    void *res;
+    res = realloc(ptr, size);
+    if (!res)
+        return NULL;
+    memset(res, 0, size);
+    return res;
+}
+
 void *qemu_mallocz(size_t size)
 {
     void *ptr;
diff -r 5c78dd111aae sdl.c
--- a/sdl.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/sdl.c	Mon Nov 17 12:15:40 2008 +0000
@@ -31,7 +31,9 @@
 #include <signal.h>
 #endif
 
-static SDL_Surface *screen;
+static DisplayChangeListener *dcl;
+static SDL_Surface *real_screen;
+static SDL_Surface *guest_screen = NULL;
 static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
 static int last_vm_running;
 static int gui_saved_grab;
@@ -45,6 +47,7 @@
 static int width, height;
 static SDL_Cursor *sdl_cursor_normal;
 static SDL_Cursor *sdl_cursor_hidden;
+static int bgr = 0;
 static int absolute_enabled = 0;
 static int guest_cursor = 0;
 static int guest_x, guest_y;
@@ -52,11 +55,58 @@
 
 static void sdl_update(DisplayState *ds, int x, int y, int w, int h)
 {
+    SDL_Rect rec;
+    rec.x = x;
+    rec.y = y;
+    rec.w = w;
+    rec.h = h;
     //    printf("updating x=%d y=%d w=%d h=%d\n", x, y, w, h);
-    SDL_UpdateRect(screen, x, y, w, h);
+
+    SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
+    SDL_Flip(real_screen);
 }
 
-static void sdl_resize(DisplayState *ds, int w, int h)
+static void sdl_setdata(DisplayState *ds)
+{
+    uint32_t rmask, gmask, bmask, amask = 0;
+    SDL_Rect rec;
+    rec.x = 0;
+    rec.y = 0;
+    rec.w = real_screen->w;
+    rec.h = real_screen->h;
+
+    if (guest_screen != NULL) SDL_FreeSurface(guest_screen);
+
+    switch (ds->depth) {
+        case 8:
+            rmask = 0x000000E0;
+            gmask = 0x0000001C;
+            bmask = 0x00000003;
+            break;
+        case 16:
+            rmask = 0x0000F800;
+            gmask = 0x000007E0;
+            bmask = 0x0000001F;
+            break;
+        case 24:
+            rmask = 0x00FF0000;
+            gmask = 0x0000FF00;
+            bmask = 0x000000FF;
+            break;
+        case 32:
+            rmask = 0x00FF0000;
+            gmask = 0x0000FF00;
+            bmask = 0x000000FF;
+            break;
+        default:
+            return;
+    }
+    guest_screen = SDL_CreateRGBSurfaceFrom(ds->data, ds->width, ds->height,
+                                            ds->depth, ds->linesize,
+                                            rmask , gmask, bmask, amask);
+}
+
+static void sdl_resize(DisplayState *ds)
 {
     int flags;
 
@@ -68,45 +118,28 @@
     if (gui_noframe)
         flags |= SDL_NOFRAME;
 
-    width = w;
-    height = h;
-
  again:
-    screen = SDL_SetVideoMode(w, h, 0, flags);
-    if (!screen) {
+    real_screen = SDL_SetVideoMode(ds->width, ds->height, 0, flags);
+    if (!real_screen) {
         fprintf(stderr, "Could not open SDL display\n");
         exit(1);
     }
-    if (!screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
+    if (!real_screen->pixels && (flags & SDL_HWSURFACE) && (flags & SDL_FULLSCREEN)) {
         flags &= ~SDL_HWSURFACE;
         goto again;
     }
 
-    if (!screen->pixels) {
+    if (!real_screen->pixels) {
         fprintf(stderr, "Could not open SDL display\n");
         exit(1);
     }
-    ds->data = screen->pixels;
-    ds->linesize = screen->pitch;
-    ds->depth = screen->format->BitsPerPixel;
-    /* SDL BitsPerPixel never indicates any values other than
-       multiples of 8, so we need to check for strange depths. */
-    if (ds->depth == 16) {
-        uint32_t mask;
+    if (real_screen->format->Bshift > real_screen->format->Rshift) {
+        bgr = 1;
+    } else {
+        bgr = 0;
+    }
 
-        mask = screen->format->Rmask;
-        mask |= screen->format->Gmask;
-        mask |= screen->format->Bmask;
-        if ((mask & 0x8000) == 0)
-            ds->depth = 15;
-    }
-    if (ds->depth == 32 && screen->format->Rshift == 0) {
-        ds->bgr = 1;
-    } else {
-        ds->bgr = 0;
-    }
-    ds->width = w;
-    ds->height = h;
+    sdl_setdata(ds);
 }
 
 /* generic keyboard conversion */
@@ -339,7 +372,7 @@
 static void toggle_full_screen(DisplayState *ds)
 {
     gui_fullscreen = !gui_fullscreen;
-    sdl_resize(ds, screen->w, screen->h);
+    sdl_resize(ds);
     if (gui_fullscreen) {
         gui_saved_grab = gui_grab;
         sdl_grab_start();
@@ -368,7 +401,7 @@
     while (SDL_PollEvent(ev)) {
         switch (ev->type) {
         case SDL_VIDEOEXPOSE:
-            sdl_update(ds, 0, 0, screen->w, screen->h);
+            sdl_update(ds, 0, 0, real_screen->w, real_screen->h);
             break;
         case SDL_KEYDOWN:
         case SDL_KEYUP:
@@ -523,12 +556,12 @@
             if (ev->active.state & SDL_APPACTIVE) {
                 if (ev->active.gain) {
                     /* Back to default interval */
-                    ds->gui_timer_interval = 0;
-                    ds->idle = 0;
+                    dcl->gui_timer_interval = 0;
+                    dcl->idle = 0;
                 } else {
                     /* Sleeping interval */
-                    ds->gui_timer_interval = 500;
-                    ds->idle = 1;
+                    dcl->gui_timer_interval = 500;
+                    dcl->idle = 1;
                 }
             }
             break;
@@ -541,7 +574,7 @@
 static void sdl_fill(DisplayState *ds, int x, int y, int w, int h, uint32_t c)
 {
     SDL_Rect dst = { x, y, w, h };
-    SDL_FillRect(screen, &dst, c);
+    SDL_FillRect(real_screen, &dst, c);
 }
 
 static void sdl_mouse_warp(int x, int y, int on)
@@ -637,14 +670,18 @@
         exit(1);
     }
 
-    ds->dpy_update = sdl_update;
-    ds->dpy_resize = sdl_resize;
-    ds->dpy_refresh = sdl_refresh;
-    ds->dpy_fill = sdl_fill;
+    dcl = qemu_mallocz(sizeof(DisplayChangeListener));
+    if (!dcl)
+        exit(1);
+    dcl->dpy_update = sdl_update;
+    dcl->dpy_resize = sdl_resize;
+    dcl->dpy_refresh = sdl_refresh;
+    dcl->dpy_setdata = sdl_setdata;
+    dcl->dpy_fill = sdl_fill;
     ds->mouse_set = sdl_mouse_warp;
     ds->cursor_define = sdl_mouse_define;
+    register_displaychangelistener(ds, dcl);
 
-    sdl_resize(ds, 640, 400);
     sdl_update_caption();
     SDL_EnableKeyRepeat(250, 50);
     gui_grab = 0;
diff -r 5c78dd111aae vl.c
--- a/vl.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/vl.c	Mon Nov 17 12:15:40 2008 +0000
@@ -182,6 +182,7 @@
 DisplayState display_state;
 int nographic;
 static int curses;
+static int sdl;
 const char* keyboard_layout = NULL;
 int64_t ticks_per_sec;
 ram_addr_t ram_size;
@@ -2506,20 +2507,32 @@
 {
 }
 
-static void dumb_resize(DisplayState *ds, int w, int h)
-{
+static void dumb_resize(DisplayState *ds)
+{
+}
+
+static void default_data_resize(struct DisplayState *ds)
+{
+    if (ds->shared_buf)
+        return;
+    ds->data = (uint8_t *) qemu_realloc(ds->data, ds->linesize * ds->height);
+    if (!ds->data) {
+        fprintf(stderr, "ds->data resize to %d bytes, failed\n", ds->linesize * ds->height);
+        exit(1);
+    }
 }
 
 static void dumb_display_init(DisplayState *ds)
 {
-    ds->data = NULL;
-    ds->linesize = 0;
-    ds->depth = 0;
-    ds->dpy_update = dumb_update;
-    ds->dpy_resize = dumb_resize;
-    ds->dpy_refresh = NULL;
-    ds->gui_timer_interval = 0;
-    ds->idle = 1;
+    DisplayChangeListener *dcl = qemu_mallocz(sizeof(DisplayChangeListener));
+    if (!dcl)
+        exit(1);
+    dcl->dpy_update = dumb_update;
+    dcl->dpy_resize = dumb_resize;
+    dcl->dpy_refresh = NULL;
+    dcl->idle = 1;
+    dcl->gui_timer_interval = 500;
+    register_displaychangelistener(ds, dcl);
 }
 
 /***********************************************************/
@@ -4192,13 +4205,19 @@
 
 static void gui_update(void *opaque)
 {
+    uint64_t interval = GUI_REFRESH_INTERVAL;
     DisplayState *ds = opaque;
-    ds->dpy_refresh(ds);
-    qemu_mod_timer(ds->gui_timer,
-        (ds->gui_timer_interval ?
-	    ds->gui_timer_interval :
-	    GUI_REFRESH_INTERVAL)
-	+ qemu_get_clock(rt_clock));
+    DisplayChangeListener *dcl = ds->listeners;
+
+    dpy_refresh(ds);
+
+    while (dcl != NULL) {
+        if (dcl->gui_timer_interval &&
+            dcl->gui_timer_interval < interval)
+            interval = dcl->gui_timer_interval;
+        dcl = dcl->next;
+    }
+    qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
 }
 
 struct vm_change_state_entry {
@@ -4685,6 +4704,7 @@
            "-no-frame       open SDL window without a frame and window decorations\n"
            "-alt-grab       use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
            "-no-quit        disable SDL window close capability\n"
+           "-sdl            enable SDL\n"
 #endif
 #ifdef TARGET_I386
            "-no-fd-bootchk  disable boot signature checking for floppy disks\n"
@@ -4884,6 +4904,7 @@
     QEMU_OPTION_no_frame,
     QEMU_OPTION_alt_grab,
     QEMU_OPTION_no_quit,
+    QEMU_OPTION_sdl,
     QEMU_OPTION_pidfile,
     QEMU_OPTION_no_kqemu,
     QEMU_OPTION_kernel_kqemu,
@@ -4988,6 +5009,7 @@
     { "no-frame", 0, QEMU_OPTION_no_frame },
     { "alt-grab", 0, QEMU_OPTION_alt_grab },
     { "no-quit", 0, QEMU_OPTION_no_quit },
+    { "sdl", 0, QEMU_OPTION_sdl },
 #endif
     { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
     { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
@@ -5292,6 +5314,7 @@
     const char *kernel_filename, *kernel_cmdline;
     const char *boot_devices = "";
     DisplayState *ds = &display_state;
+    DisplayChangeListener *dcl;
     int cyls, heads, secs, translation;
     const char *net_clients[MAX_NET_CLIENTS];
     int nb_net_clients;
@@ -5776,6 +5799,9 @@
                 break;
             case QEMU_OPTION_no_quit:
                 no_quit = 1;
+                break;
+            case QEMU_OPTION_sdl:
+                sdl = 1;
                 break;
 #endif
             case QEMU_OPTION_pidfile:
@@ -6142,6 +6168,11 @@
 
     /* terminal init */
     memset(&display_state, 0, sizeof(display_state));
+    ds->width = 640;
+    ds->height = 480;
+    ds->depth = 32;
+    ds->linesize = 4 * ds->width;
+    ds->data_resize = default_data_resize;
     if (nographic) {
         if (curses) {
             fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
@@ -6149,26 +6180,31 @@
         }
         /* nearly nothing to do */
         dumb_display_init(ds);
-    } else if (vnc_display != NULL) {
-        vnc_display_init(ds);
-        if (vnc_display_open(ds, vnc_display) < 0)
-            exit(1);
-    } else
+    } else { 
 #if defined(CONFIG_CURSES)
-    if (curses) {
-        curses_display_init(ds, full_screen);
-    } else
-#endif
-    {
+            if (curses) {
+                /* At the moment curses cannot be used with other displays */
+                curses_display_init(ds, full_screen);
+            } else
+#endif
+            {
+                if (vnc_display != NULL) {
+                    vnc_display_init(ds);
+                    if (vnc_display_open(ds, vnc_display) < 0)
+                        exit(1);
+                }
+                if (sdl || !vnc_display)
 #if defined(CONFIG_SDL)
-        sdl_display_init(ds, full_screen, no_frame);
+                    sdl_display_init(ds, full_screen, no_frame);
 #elif defined(CONFIG_COCOA)
-        cocoa_display_init(ds, full_screen);
-#else
-        dumb_display_init(ds);
-#endif
-    }
-
+                    cocoa_display_init(ds, full_screen);
+#else
+                    dumb_display_init(ds);
+#endif
+            }
+    }
+    ds->data_resize(ds);
+    dpy_resize(ds);
 #ifndef _WIN32
     /* must be after terminal init, SDL library changes signal handlers */
     termsig_setup();
@@ -6242,11 +6278,14 @@
         }
     }
 
-    if (display_state.dpy_refresh) {
-        display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state);
-        qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
-    }
-
+    dcl = ds->listeners;
+    while (dcl != NULL) {
+        if (dcl->dpy_refresh != NULL) {
+            display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state);
+            qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
+        }
+        dcl = dcl->next;
+    }
 #ifdef CONFIG_GDBSTUB
     if (use_gdbstub) {
         /* XXX: use standard host:port notation and modify options
diff -r 5c78dd111aae vnc.c
--- a/vnc.c	Tue Nov 04 09:04:41 2008 +0000
+++ b/vnc.c	Mon Nov 17 12:15:40 2008 +0000
@@ -176,6 +176,7 @@
 };
 
 static VncState *vnc_state; /* needed for info vnc */
+static DisplayChangeListener *dcl;
 
 void do_info_vnc(void)
 {
@@ -209,7 +210,7 @@
 static void vnc_update_client(void *opaque);
 static void vnc_client_read(void *opaque);
 
-static void vnc_colordepth(DisplayState *ds, int depth);
+static void vnc_colordepth(DisplayState *ds);
 
 static inline void vnc_set_bit(uint32_t *d, int k)
 {
@@ -287,27 +288,22 @@
     vnc_write_s32(vs, encoding);
 }
 
-static void vnc_dpy_resize(DisplayState *ds, int w, int h)
+static void vnc_dpy_resize(DisplayState *ds)
 {
     int size_changed;
     VncState *vs = ds->opaque;
 
-    ds->data = qemu_realloc(ds->data, w * h * vs->depth);
-    vs->old_data = qemu_realloc(vs->old_data, w * h * vs->depth);
+    vs->old_data = qemu_realloc(vs->old_data, ds->linesize * ds->height);
 
-    if (ds->data == NULL || vs->old_data == NULL) {
+    if (vs->old_data == NULL) {
 	fprintf(stderr, "vnc: memory allocation failed\n");
 	exit(1);
     }
 
-    if (ds->depth != vs->depth * 8) {
-        ds->depth = vs->depth * 8;
+    if (ds->depth != vs->depth * 8)
         console_color_init(ds);
-    }
-    size_changed = ds->width != w || ds->height != h;
-    ds->width = w;
-    ds->height = h;
-    ds->linesize = w * vs->depth;
+    vnc_colordepth(ds);
+    size_changed = ds->width != vs->width || ds->height != vs->height;
     if (size_changed) {
         vs->width = ds->width;
         vs->height = ds->height;
@@ -321,7 +317,7 @@
     }
 
     memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
-    memset(vs->old_data, 42, vs->ds->linesize * vs->ds->height);
+    memset(vs->old_data, 42, ds->linesize * ds->height);
 }
 
 /* fastest code */
@@ -490,10 +486,6 @@
 
 static void vnc_copy(DisplayState *ds, int src_x, int src_y, int dst_x, int dst_y, int w, int h)
 {
-    int src, dst;
-    uint8_t *src_row;
-    uint8_t *dst_row;
-    char *old_row;
     int y = 0;
     int pitch = ds->linesize;
     VncState *vs = ds->opaque;
@@ -503,21 +495,6 @@
     if (dst_y > src_y) {
 	y = h - 1;
 	pitch = -pitch;
-    }
-
-    src = (ds->linesize * (src_y + y) + vs->depth * src_x);
-    dst = (ds->linesize * (dst_y + y) + vs->depth * dst_x);
-
-    src_row = ds->data + src;
-    dst_row = ds->data + dst;
-    old_row = vs->old_data + dst;
-
-    for (y = 0; y < h; y++) {
-	memmove(old_row, src_row, w * vs->depth);
-	memmove(dst_row, src_row, w * vs->depth);
-	src_row += pitch;
-	dst_row += pitch;
-	old_row += pitch;
     }
 
     vnc_write_u8(vs, 0);  /* msg id */
@@ -701,7 +678,7 @@
 	qemu_set_fd_handler2(vs->csock, NULL, NULL, NULL, NULL);
 	closesocket(vs->csock);
 	vs->csock = -1;
-	vs->ds->idle = 1;
+	dcl->idle = 1;
 	buffer_reset(&vs->input);
 	buffer_reset(&vs->output);
 	vs->need_update = 0;
@@ -1147,7 +1124,7 @@
     vs->has_pointer_type_change = 0;
     vs->has_WMVi = 0;
     vs->absolute = -1;
-    vs->ds->dpy_copy = NULL;
+    dcl->dpy_copy = NULL;
 
     for (i = n_encodings - 1; i >= 0; i--) {
 	switch (encodings[i]) {
@@ -1155,7 +1132,7 @@
 	    vs->has_hextile = 0;
 	    break;
 	case 1: /* CopyRect */
-	    vs->ds->dpy_copy = vnc_copy;
+	    dcl->dpy_copy = vnc_copy;
 	    break;
 	case 5: /* Hextile */
 	    vs->has_hextile = 1;
@@ -1305,23 +1282,15 @@
     vnc_write(vs, pad, 3);           /* padding */
 }
 
-static void vnc_colordepth(DisplayState *ds, int depth)
+static void vnc_dpy_setdata(DisplayState *ds)
+{
+    /* We don't have to do anything */
+}
+
+static void vnc_colordepth(DisplayState *ds)
 {
     int host_big_endian_flag;
     struct VncState *vs = ds->opaque;
-
-    switch (depth) {
-        case 24:
-            if (ds->depth == 32) return;
-            depth = 32;
-            break;
-        case 15:
-        case 8:
-        case 0:
-            return;
-        default:
-            break;
-    }
 
 #ifdef WORDS_BIGENDIAN
     host_big_endian_flag = 1;
@@ -1329,9 +1298,9 @@
     host_big_endian_flag = 0;
 #endif   
     
-    switch (depth) {
+    switch (ds->depth) {
         case 8:
-            vs->depth = depth / 8;
+            vs->depth = 1;
             vs->server_red_max = 7;
             vs->server_green_max = 7;
             vs->server_blue_max = 3;
@@ -1340,7 +1309,7 @@
             vs->server_blue_shift = 0;
             break;
         case 16:
-            vs->depth = depth / 8;
+            vs->depth = 2;
             vs->server_red_max = 31;
             vs->server_green_max = 63;
             vs->server_blue_max = 31;
@@ -2110,7 +2079,7 @@
 static void vnc_connect(VncState *vs)
 {
     VNC_DEBUG("New client on socket %d\n", vs->csock);
-    vs->ds->idle = 0;
+    dcl->idle = 0;
     socket_set_nonblock(vs->csock);
     qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
     vnc_write(vs, "RFB 003.008\n", 12);
@@ -2120,7 +2089,7 @@
     memset(vs->dirty_row, 0xFF, sizeof(vs->dirty_row));
     vs->has_resize = 0;
     vs->has_hextile = 0;
-    vs->ds->dpy_copy = NULL;
+    dcl->dpy_copy = NULL;
     vnc_update_client(vs);
 }
 
@@ -2146,11 +2115,12 @@
     VncState *vs;
 
     vs = qemu_mallocz(sizeof(VncState));
-    if (!vs)
+    dcl = qemu_mallocz(sizeof(DisplayChangeListener));
+    if (!vs || !dcl)
 	exit(1);
 
     ds->opaque = vs;
-    ds->idle = 1;
+    dcl->idle = 1;
     vnc_state = vs;
     vs->display = NULL;
     vs->password = NULL;
@@ -2173,12 +2143,11 @@
     vs->timer = qemu_new_timer(rt_clock, vnc_update_client, vs);
 
     vs->ds->data = NULL;
-    vs->ds->dpy_update = vnc_dpy_update;
-    vs->ds->dpy_resize = vnc_dpy_resize;
-    vs->ds->dpy_refresh = NULL;
-
-    vnc_colordepth(vs->ds, 32);
-    vnc_dpy_resize(vs->ds, 640, 400);
+    dcl->dpy_update = vnc_dpy_update;
+    dcl->dpy_resize = vnc_dpy_resize;
+    dcl->dpy_setdata = vnc_dpy_setdata;
+    dcl->dpy_refresh = NULL;
+    register_displaychangelistener(ds, dcl);
 }
 
 #ifdef CONFIG_VNC_TLS

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Qemu-devel] [PATCH] DisplayState interface change
  2008-11-17 12:44 [Qemu-devel] [PATCH] DisplayState interface change Stefano Stabellini
@ 2008-11-17 14:37 ` Anthony Liguori
  2008-11-17 16:07   ` Stefano Stabellini
  0 siblings, 1 reply; 3+ messages in thread
From: Anthony Liguori @ 2008-11-17 14:37 UTC (permalink / raw)
  To: qemu-devel

Stefano Stabellini wrote:
> diff -r 5c78dd111aae console.h
> --- a/console.h	Tue Nov 04 09:04:41 2008 +0000
> +++ b/console.h	Mon Nov 17 12:15:40 2008 +0000
> @@ -73,45 +73,111 @@
>  
>  /* consoles */
>  
> -struct DisplayState {
> -    uint8_t *data;
> -    int linesize;
> -    int depth;
> -    int bgr; /* BGR color order instead of RGB. Only valid for depth == 32 */
> -    int width;
> -    int height;
> -    void *opaque;
> -    struct QEMUTimer *gui_timer;
> +struct DisplayChangeListener {
> +    int idle;
>      uint64_t gui_timer_interval;
> -    int idle; /* there is nothing to update (window invisible), set by vnc/sdl */
>  
>      void (*dpy_update)(struct DisplayState *s, int x, int y, int w, int h);
> -    void (*dpy_resize)(struct DisplayState *s, int w, int h);
> +    void (*dpy_resize)(struct DisplayState *s);
> +    void (*dpy_setdata)(struct DisplayState *s);
>      void (*dpy_refresh)(struct DisplayState *s);
>      void (*dpy_copy)(struct DisplayState *s, int src_x, int src_y,
>                       int dst_x, int dst_y, int w, int h);
>      void (*dpy_fill)(struct DisplayState *s, int x, int y,
>                       int w, int h, uint32_t c);
>      void (*dpy_text_cursor)(struct DisplayState *s, int x, int y);
> +
> +    struct DisplayChangeListener *next;
> +};
> +
> +struct DisplayState {
> +    uint8_t *data;
> +    int shared_buf;
> +    int linesize;
> +    int depth;
> +    int width;
> +    int height;
> +    void *opaque;
> +    struct QEMUTimer *gui_timer;
> +
> +    struct DisplayChangeListener* listeners;
> +
>      void (*mouse_set)(int x, int y, int on);
>      void (*cursor_define)(int width, int height, int bpp, int hot_x, int hot_y,
>                            uint8_t *image, uint8_t *mask);
> +
> +    void (*data_resize)(struct DisplayState *s);
>  }

If we're going to change DisplayState, and I think it's long over due 
that we do, then I think we should try to make sure we get it right.

What I would like to see is the buffer information extracted into a 
separate structure.  That buffer should also describe the pixel format 
in a much more thorough way.  There should be, in the very least, an 
endianness flag, information about the per-color shift and per-color 
mask, the bits per pixel, the bytes per pixel, the width and the height.

We should also have generic conversion functions to convert from one 
buffer to another.  This would allow us to simplify a lot of things 
(like screen shot).  It also ties in better to other display tool kits.  
Right now we have too many assumptions about what the pixel layout is 
from the depth.

The other thing to consider is that we do need some level of 
bidirectional communication.  vmware VGA supports the ability to tell 
the guest what resolution/depth it ought to be using.  You really want 
to get that information from SDL (at least the depth bits) so that we 
can avoid blitting.  With KVM, we can pretty trivially directly map the 
SDL buffer directly into the guest's physical memory provided it uses 
the right format.

Regards,

Anthony Liguori

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Qemu-devel] [PATCH] DisplayState interface change
  2008-11-17 14:37 ` Anthony Liguori
@ 2008-11-17 16:07   ` Stefano Stabellini
  0 siblings, 0 replies; 3+ messages in thread
From: Stefano Stabellini @ 2008-11-17 16:07 UTC (permalink / raw)
  To: qemu-devel

Anthony Liguori wrote:

> If we're going to change DisplayState, and I think it's long over due
> that we do, then I think we should try to make sure we get it right.
> 
> What I would like to see is the buffer information extracted into a
> separate structure.  That buffer should also describe the pixel format
> in a much more thorough way.  There should be, in the very least, an
> endianness flag, information about the per-color shift and per-color
> mask, the bits per pixel, the bytes per pixel, the width and the height.


That should be pretty easy to do, since we already have all those
informations.


> We should also have generic conversion functions to convert from one
> buffer to another.  This would allow us to simplify a lot of things
> (like screen shot).  It also ties in better to other display tool kits. 
> Right now we have too many assumptions about what the pixel layout is
> from the depth.


Those functions right now are in vga_template.h I tried not to change it
in this patch.

> The other thing to consider is that we do need some level of
> bidirectional communication.  vmware VGA supports the ability to tell
> the guest what resolution/depth it ought to be using.  You really want
> to get that information from SDL (at least the depth bits) so that we
> can avoid blitting.  With KVM, we can pretty trivially directly map the
> SDL buffer directly into the guest's physical memory provided it uses
> the right format.
> 


I agree that it be nice, we could probably do something like that on
xen too.

Please don't ask me to make these big changes (generic functions,
bidirectional communication) on top of this patch in order for it to get
applied.
This patch is already too long, it would be difficult to
accomplish all the things you suggested in a single shot without
breaking anything.
I think it is best to do this incrementally and work together on the
features suggested after this patch gets applied.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-11-17 16:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-17 12:44 [Qemu-devel] [PATCH] DisplayState interface change Stefano Stabellini
2008-11-17 14:37 ` Anthony Liguori
2008-11-17 16:07   ` Stefano Stabellini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.