DisplayAllocator interface
PATCH 1/1 DisplayAllocator interface
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.
---
console.c +13 -13
@@ -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;
@@ -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 ; }
@@ -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); } }
@@ -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
@@ -1570,13 +1570,13 @@ DisplaySurface* qemu_create_displaysurface(int width, int height, int bpp, int lreturn 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
@@ -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 ;
console.h +36 -4
@@ -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);
@@ -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 ));
curses.c +1 -1
@@ -366,7 +366,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 ;
hw/musicpal.c +1 -1
@@ -822,7 +822,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));
hw/nseries.c +1 -1
@@ -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); }
hw/palm.c +1 -1
@@ -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); }
hw/sm501.c +4 -1
@@ -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 ; } }
hw/tcx.c +10 -3
@@ -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 ; } }
@@ -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
@@ -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];
hw/vga.c +7 -4
@@ -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 ; } }
@@ -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 ));
@@ -2619,7 +2622,7 @@ 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 ;
@@ -2627,7 +2630,7 @@ static void vga_screen_dump_common(VGAState *s, const char *filename,ppm_save (filename, ds->surface); -qemu_free_displaysurface (ds->surface); +qemu_free_displaysurface (ds); s->ds = saved_ds; }
qemu-common.h +1 -0
@@ -168,6 +168,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 ;
sdl.c +118 -13
182 lines not on this page · raw message
vl.c +14 -1
@@ -2843,6 +2843,12 @@ void pcmcia_info(void)/***********************************************************/ /* register display */ +struct DisplayAllocator default_allocator = { + defaultallocator_create_displaysurface, + defaultallocator_resize_displaysurface, + defaultallocator_free_displaysurface +}; +void register_displaystate (DisplayState *ds) {DisplayState **s;
@@ -2858,12 +2864,19 @@ DisplayState *get_displaystate(void)return display_state; } +DisplayAllocator *register_displayallocator (DisplayState *ds,DisplayAllocator *da) +{ +if (ds->allocator == &default_allocator) ds->allocator = da; +return ds->allocator; +} +/* dumb display */ static void dumb_display_init (void ) {DisplayState *ds =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 );register_displaystate (ds); }
Anthony Liguori
AppliedQuoted 5 lines · Stefano Stabellini
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.
Quoted 14 lines
Whenever a graphical backend cannot use
qemu_create_displaysurface_frombecause 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 usingqemu_create_displaysurfaceand 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 ofqemu_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.
Applied. Thanks.
Regards,
Anthony Liguori