Skip to content

Commit

Permalink
DisplayAllocator interface (Stefano Stabellini)
Browse files Browse the repository at this point in the history
Hi all,
this patch adds a DisplayAllocator interface that allows display
frontends (sdl in particular) to provide a preallocated display buffer
for the graphical backend to use.

Whenever a graphical backend cannot use
qemu_create_displaysurface_from because its own internal pixel format
cannot be exported directly (text mode or graphical mode with color
depth 8 or 24), it creates another display buffer in memory using
qemu_create_displaysurface and does the conversion.
This new buffer needs to be blitted into the sdl surface buffer every time
we need to update portions of the screen.
We can avoid this using the DisplayAllocator interace: sdl provides its
own implementation of qemu_create_displaysurface, giving back the sdl
surface buffer directly (as we used to do before the DisplayState
changes).
Since the buffer returned by sdl could be in bgr format we need to put
back in the handlers of that case.

This approach is good if the two following conditions are true:

1) the sdl surface is a software surface that resides in main memory;

2) the host display color depth is either 16 or 32 bpp.

If first condition is false we can have bad performances using sdl
and vnc together.
If the second condition is false performances are certainly not going to
improve but they shouldn't get worse either.

The first condition is always true, at least on linux/X11 systems; but I
believe is true also on other platforms.
The second condition is true in the vast majority of the cases.

This patch should also have the good side effect of solving the sdl
2D slowness malc was reporting on MacOS, because SDL_BlitSurface is not
going to be called anymore when the guest is in text mode or 24bpp.
However the root problem is still present so I suspect we may
still see some slowness on MacOS when the guest is in 32 or 16 bpp.

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6839 c046a42c-6fe2-441c-8c8c-71466251a162
  • Loading branch information
aliguori committed Mar 13, 2009
1 parent 86dbdd4 commit 7b5d76d
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 43 deletions.
26 changes: 13 additions & 13 deletions console.c
Original file line number Diff line number Diff line change
Expand Up @@ -1068,8 +1068,7 @@ void console_select(unsigned int index)
DisplayState *ds = s->ds;
active_console = s;
if (ds_get_bits_per_pixel(s->ds)) {
ds->surface = qemu_resize_displaysurface(ds->surface, s->g_width,
s->g_height, 32, 4 * s->g_width);
ds->surface = qemu_resize_displaysurface(ds, s->g_width, s->g_height);
} else {
s->ds->surface->width = s->width;
s->ds->surface->height = s->height;
Expand Down Expand Up @@ -1277,11 +1276,12 @@ DisplayState *graphic_console_init(vga_hw_update_ptr update,
DisplayState *ds;

ds = (DisplayState *) qemu_mallocz(sizeof(DisplayState));
ds->surface = qemu_create_displaysurface(640, 480, 32, 640 * 4);
ds->allocator = &default_allocator;
ds->surface = qemu_create_displaysurface(ds, 640, 480);

s = new_console(ds, GRAPHIC_CONSOLE);
if (s == NULL) {
qemu_free_displaysurface(ds->surface);
qemu_free_displaysurface(ds);
qemu_free(ds);
return NULL;
}
Expand Down Expand Up @@ -1429,7 +1429,7 @@ void qemu_console_resize(DisplayState *ds, int width, int height)
s->g_width = width;
s->g_height = height;
if (is_graphic_console()) {
ds->surface = qemu_resize_displaysurface(ds->surface, width, height, 32, 4 * width);
ds->surface = qemu_resize_displaysurface(ds, width, height);
dpy_resize(ds);
}
}
Expand Down Expand Up @@ -1552,14 +1552,14 @@ PixelFormat qemu_default_pixelformat(int bpp)
return pf;
}

DisplaySurface* qemu_create_displaysurface(int width, int height, int bpp, int linesize)
DisplaySurface* defaultallocator_create_displaysurface(int width, int height)
{
DisplaySurface *surface = (DisplaySurface*) qemu_mallocz(sizeof(DisplaySurface));

surface->width = width;
surface->height = height;
surface->linesize = linesize;
surface->pf = qemu_default_pixelformat(bpp);
surface->linesize = width * 4;
surface->pf = qemu_default_pixelformat(32);
#ifdef WORDS_BIGENDIAN
surface->flags = QEMU_ALLOCATED_FLAG | QEMU_BIG_ENDIAN_FLAG;
#else
Expand All @@ -1570,13 +1570,13 @@ DisplaySurface* qemu_create_displaysurface(int width, int height, int bpp, int l
return surface;
}

DisplaySurface* qemu_resize_displaysurface(DisplaySurface *surface,
int width, int height, int bpp, int linesize)
DisplaySurface* defaultallocator_resize_displaysurface(DisplaySurface *surface,
int width, int height)
{
surface->width = width;
surface->height = height;
surface->linesize = linesize;
surface->pf = qemu_default_pixelformat(bpp);
surface->linesize = width * 4;
surface->pf = qemu_default_pixelformat(32);
if (surface->flags & QEMU_ALLOCATED_FLAG)
surface->data = (uint8_t*) qemu_realloc(surface->data, surface->linesize * surface->height);
else
Expand Down Expand Up @@ -1607,7 +1607,7 @@ DisplaySurface* qemu_create_displaysurface_from(int width, int height, int bpp,
return surface;
}

void qemu_free_displaysurface(DisplaySurface *surface)
void defaultallocator_free_displaysurface(DisplaySurface *surface)
{
if (surface == NULL)
return;
Expand Down
40 changes: 36 additions & 4 deletions console.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,18 @@ struct DisplayChangeListener {
struct DisplayChangeListener *next;
};

struct DisplayAllocator {
DisplaySurface* (*create_displaysurface)(int width, int height);
DisplaySurface* (*resize_displaysurface)(DisplaySurface *surface, int width, int height);
void (*free_displaysurface)(DisplaySurface *surface);
};

struct DisplayState {
struct DisplaySurface *surface;
void *opaque;
struct QEMUTimer *gui_timer;

struct DisplayAllocator* allocator;
struct DisplayChangeListener* listeners;

void (*mouse_set)(int x, int y, int on);
Expand All @@ -129,15 +136,40 @@ struct DisplayState {

void register_displaystate(DisplayState *ds);
DisplayState *get_displaystate(void);
DisplaySurface* qemu_create_displaysurface(int width, int height, int bpp, int linesize);
DisplaySurface* qemu_resize_displaysurface(DisplaySurface *surface,
int width, int height, int bpp, int linesize);
DisplaySurface* qemu_create_displaysurface_from(int width, int height, int bpp,
int linesize, uint8_t *data);
void qemu_free_displaysurface(DisplaySurface *surface);
PixelFormat qemu_different_endianness_pixelformat(int bpp);
PixelFormat qemu_default_pixelformat(int bpp);

extern struct DisplayAllocator default_allocator;
DisplayAllocator *register_displayallocator(DisplayState *ds, DisplayAllocator *da);
DisplaySurface* defaultallocator_create_displaysurface(int width, int height);
DisplaySurface* defaultallocator_resize_displaysurface(DisplaySurface *surface, int width, int height);
void defaultallocator_free_displaysurface(DisplaySurface *surface);

static inline DisplaySurface* qemu_create_displaysurface(DisplayState *ds, int width, int height)
{
return ds->allocator->create_displaysurface(width, height);
}

static inline DisplaySurface* qemu_resize_displaysurface(DisplayState *ds, int width, int height)
{
return ds->allocator->resize_displaysurface(ds->surface, width, height);
}

static inline void qemu_free_displaysurface(DisplayState *ds)
{
ds->allocator->free_displaysurface(ds->surface);
}

static inline int is_surface_bgr(DisplaySurface *surface)
{
if (surface->pf.bits_per_pixel == 32 && surface->pf.rshift == 0)
return 1;
else
return 0;
}

static inline int is_buffer_shared(DisplaySurface *surface)
{
return (!(surface->flags & QEMU_ALLOCATED_FLAG));
Expand Down
2 changes: 1 addition & 1 deletion curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ void curses_display_init(DisplayState *ds, int full_screen)
dcl->dpy_refresh = curses_refresh;
dcl->dpy_text_cursor = curses_cursor_position;
register_displaychangelistener(ds, dcl);
qemu_free_displaysurface(ds->surface);
qemu_free_displaysurface(ds);
ds->surface = qemu_create_displaysurface_from(640, 400, 0, 0, (uint8_t*) screen);

invalidate = 1;
Expand Down
2 changes: 1 addition & 1 deletion hw/musicpal.c
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ static void lcd_refresh(void *opaque)
break;
LCD_REFRESH(8, rgb_to_pixel8)
LCD_REFRESH(16, rgb_to_pixel16)
LCD_REFRESH(32, rgb_to_pixel32)
LCD_REFRESH(32, (is_surface_bgr(s->ds) ? rgb_to_pixel32bgr : rgb_to_pixel32))
default:
cpu_abort(cpu_single_env, "unsupported colour depth %i\n",
ds_get_bits_per_pixel(s->ds));
Expand Down
2 changes: 1 addition & 1 deletion hw/nseries.c
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,7 @@ static void n8x0_init(ram_addr_t ram_size, const char *boot_device,
will set the size once configured, so this just sets an initial
size until the guest activates the display. */
ds = get_displaystate();
ds->surface = qemu_resize_displaysurface(ds->surface, 800, 480, 32, 4 * 800);
ds->surface = qemu_resize_displaysurface(ds, 800, 480);
dpy_resize(ds);
}

Expand Down
2 changes: 1 addition & 1 deletion hw/palm.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static void palmte_init(ram_addr_t ram_size, int vga_ram_size,
/* FIXME: We shouldn't really be doing this here. The LCD controller
will set the size once configured, so this just sets an initial
size until the guest activates the display. */
ds->surface = qemu_resize_displaysurface(ds->surface, 320, 320, 32, 4 * 320);
ds->surface = qemu_resize_displaysurface(ds, 320, 320);
dpy_resize(ds);
}

Expand Down
5 changes: 4 additions & 1 deletion hw/sm501.c
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,10 @@ static inline int get_depth_index(DisplayState *s)
case 16:
return 2;
case 32:
return 3;
if (is_surface_bgr(s->surface))
return 4;
else
return 3;
}
}

Expand Down
13 changes: 10 additions & 3 deletions hw/tcx.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ static void update_palette_entries(TCXState *s, int start, int end)
s->palette[i] = rgb_to_pixel16(s->r[i], s->g[i], s->b[i]);
break;
case 32:
s->palette[i] = rgb_to_pixel32(s->r[i], s->g[i], s->b[i]);
if (is_surface_bgr(s->ds->surface))
s->palette[i] = rgb_to_pixel32bgr(s->r[i], s->g[i], s->b[i]);
else
s->palette[i] = rgb_to_pixel32(s->r[i], s->g[i], s->b[i]);
break;
}
}
Expand Down Expand Up @@ -124,11 +127,12 @@ static inline void tcx24_draw_line32(TCXState *s1, uint8_t *d,
const uint32_t *cplane,
const uint32_t *s24)
{
int x, r, g, b;
int x, bgr, r, g, b;
uint8_t val, *p8;
uint32_t *p = (uint32_t *)d;
uint32_t dval;

bgr = is_surface_bgr(s1->ds->surface);
for(x = 0; x < width; x++, s++, s24++) {
if ((be32_to_cpu(*cplane++) & 0xff000000) == 0x03000000) {
// 24-bit direct, BGR order
Expand All @@ -137,7 +141,10 @@ static inline void tcx24_draw_line32(TCXState *s1, uint8_t *d,
b = *p8++;
g = *p8++;
r = *p8++;
dval = rgb_to_pixel32(r, g, b);
if (bgr)
dval = rgb_to_pixel32bgr(r, g, b);
else
dval = rgb_to_pixel32(r, g, b);
} else {
val = *s;
dval = s1->palette[val];
Expand Down
11 changes: 7 additions & 4 deletions hw/vga.c
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,10 @@ static inline int get_depth_index(DisplayState *s)
case 16:
return 2;
case 32:
return 3;
if (is_surface_bgr(s->surface))
return 4;
else
return 3;
}
}

Expand Down Expand Up @@ -1627,7 +1630,7 @@ static void vga_draw_graphic(VGAState *s, int full_update)
if (depth == 32) {
#endif
if (is_graphic_console()) {
qemu_free_displaysurface(s->ds->surface);
qemu_free_displaysurface(s->ds);
s->ds->surface = qemu_create_displaysurface_from(disp_width, height, depth,
s->line_offset,
s->vram_ptr + (s->start_addr * 4));
Expand Down Expand Up @@ -2619,15 +2622,15 @@ static void vga_screen_dump_common(VGAState *s, const char *filename,
dcl.dpy_resize = vga_save_dpy_resize;
dcl.dpy_refresh = vga_save_dpy_refresh;
register_displaychangelistener(ds, &dcl);
ds->surface = qemu_create_displaysurface(w, h, 32, 4 * w);
ds->surface = qemu_create_displaysurface(ds, w, h);

s->ds = ds;
s->graphic_mode = -1;
vga_update_display(s);

ppm_save(filename, ds->surface);

qemu_free_displaysurface(ds->surface);
qemu_free_displaysurface(ds);
s->ds = saved_ds;
}

Expand Down
1 change: 1 addition & 0 deletions qemu-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ typedef struct BlockDriverState BlockDriverState;
typedef struct DisplayState DisplayState;
typedef struct DisplayChangeListener DisplayChangeListener;
typedef struct DisplaySurface DisplaySurface;
typedef struct DisplayAllocator DisplayAllocator;
typedef struct PixelFormat PixelFormat;
typedef struct TextConsole TextConsole;
typedef TextConsole QEMUConsole;
Expand Down
Loading

0 comments on commit 7b5d76d

Please sign in to comment.