From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Li8tt-00086X-Tk for qemu-devel@nongnu.org; Fri, 13 Mar 2009 11:02:17 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Li8ts-00084d-Ei for qemu-devel@nongnu.org; Fri, 13 Mar 2009 11:02:17 -0400 Received: from [199.232.76.173] (port=40763 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Li8ts-00084F-5f for qemu-devel@nongnu.org; Fri, 13 Mar 2009 11:02:16 -0400 Received: from savannah.gnu.org ([199.232.41.3]:32998 helo=sv.gnu.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1Li8tr-0007lO-GK for qemu-devel@nongnu.org; Fri, 13 Mar 2009 11:02:15 -0400 Received: from cvs.savannah.gnu.org ([199.232.41.69]) by sv.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1Li8tq-00088h-Sf for qemu-devel@nongnu.org; Fri, 13 Mar 2009 15:02:14 +0000 Received: from aliguori by cvs.savannah.gnu.org with local (Exim 4.69) (envelope-from ) id 1Li8tq-00088d-BF for qemu-devel@nongnu.org; Fri, 13 Mar 2009 15:02:14 +0000 MIME-Version: 1.0 Errors-To: aliguori Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Anthony Liguori Message-Id: Date: Fri, 13 Mar 2009 15:02:14 +0000 Subject: [Qemu-devel] [6839] DisplayAllocator interface (Stefano Stabellini) Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Revision: 6839 http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=6839 Author: aliguori Date: 2009-03-13 15:02:13 +0000 (Fri, 13 Mar 2009) Log Message: ----------- DisplayAllocator interface (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. 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 Signed-off-by: Anthony Liguori Modified Paths: -------------- trunk/console.c trunk/console.h trunk/curses.c trunk/hw/musicpal.c trunk/hw/nseries.c trunk/hw/palm.c trunk/hw/sm501.c trunk/hw/tcx.c trunk/hw/vga.c trunk/qemu-common.h trunk/sdl.c trunk/vl.c Modified: trunk/console.c =================================================================== --- trunk/console.c 2009-03-13 09:35:55 UTC (rev 6838) +++ trunk/console.c 2009-03-13 15:02:13 UTC (rev 6839) @@ -1068,8 +1068,7 @@ 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 *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 @@ 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 @@ 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 @@ 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 @@ -1607,7 +1607,7 @@ return surface; } -void qemu_free_displaysurface(DisplaySurface *surface) +void defaultallocator_free_displaysurface(DisplaySurface *surface) { if (surface == NULL) return; Modified: trunk/console.h =================================================================== --- trunk/console.h 2009-03-13 09:35:55 UTC (rev 6838) +++ trunk/console.h 2009-03-13 15:02:13 UTC (rev 6839) @@ -113,11 +113,18 @@ 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 @@ 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)); Modified: trunk/curses.c =================================================================== --- trunk/curses.c 2009-03-13 09:35:55 UTC (rev 6838) +++ trunk/curses.c 2009-03-13 15:02:13 UTC (rev 6839) @@ -364,7 +364,7 @@ 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; Modified: trunk/hw/musicpal.c =================================================================== --- trunk/hw/musicpal.c 2009-03-13 09:35:55 UTC (rev 6838) +++ trunk/hw/musicpal.c 2009-03-13 15:02:13 UTC (rev 6839) @@ -831,7 +831,7 @@ 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)); Modified: trunk/hw/nseries.c =================================================================== --- trunk/hw/nseries.c 2009-03-13 09:35:55 UTC (rev 6838) +++ trunk/hw/nseries.c 2009-03-13 15:02:13 UTC (rev 6839) @@ -1362,7 +1362,7 @@ 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); } Modified: trunk/hw/palm.c =================================================================== --- trunk/hw/palm.c 2009-03-13 09:35:55 UTC (rev 6838) +++ trunk/hw/palm.c 2009-03-13 15:02:13 UTC (rev 6839) @@ -278,7 +278,7 @@ /* 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); } Modified: trunk/hw/sm501.c =================================================================== --- trunk/hw/sm501.c 2009-03-13 09:35:55 UTC (rev 6838) +++ trunk/hw/sm501.c 2009-03-13 15:02:13 UTC (rev 6839) @@ -948,7 +948,10 @@ case 16: return 2; case 32: - return 3; + if (is_surface_bgr(s->surface)) + return 4; + else + return 3; } } Modified: trunk/hw/tcx.c =================================================================== --- trunk/hw/tcx.c 2009-03-13 09:35:55 UTC (rev 6838) +++ trunk/hw/tcx.c 2009-03-13 15:02:13 UTC (rev 6839) @@ -66,7 +66,10 @@ 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 @@ 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 @@ 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]; Modified: trunk/hw/vga.c =================================================================== --- trunk/hw/vga.c 2009-03-13 09:35:55 UTC (rev 6838) +++ trunk/hw/vga.c 2009-03-13 15:02:13 UTC (rev 6839) @@ -1161,7 +1161,10 @@ case 16: return 2; case 32: - return 3; + if (is_surface_bgr(s->surface)) + return 4; + else + return 3; } } @@ -1627,7 +1630,7 @@ 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 @@ 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 @@ ppm_save(filename, ds->surface); - qemu_free_displaysurface(ds->surface); + qemu_free_displaysurface(ds); s->ds = saved_ds; } Modified: trunk/qemu-common.h =================================================================== --- trunk/qemu-common.h 2009-03-13 09:35:55 UTC (rev 6838) +++ trunk/qemu-common.h 2009-03-13 15:02:13 UTC (rev 6839) @@ -162,6 +162,7 @@ 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; Modified: trunk/sdl.c =================================================================== --- trunk/sdl.c 2009-03-13 09:35:55 UTC (rev 6838) +++ trunk/sdl.c 2009-03-13 15:02:13 UTC (rev 6839) @@ -53,17 +53,20 @@ static int guest_cursor = 0; static int guest_x, guest_y; static SDL_Cursor *guest_sprite = 0; +static uint8_t allocator; +static uint8_t hostbpp; 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_BlitSurface(guest_screen, &rec, real_screen, &rec); + if (guest_screen) { + SDL_Rect rec; + rec.x = x; + rec.y = y; + rec.w = w; + rec.h = h; + SDL_BlitSurface(guest_screen, &rec, real_screen, &rec); + } SDL_UpdateRect(real_screen, x, y, w, h); } @@ -83,7 +86,7 @@ ds->surface->pf.bmask, ds->surface->pf.amask); } -static void sdl_resize(DisplayState *ds) +static void do_sdl_resize(int width, int height, int bpp) { int flags; @@ -95,17 +98,103 @@ if (gui_noframe) flags |= SDL_NOFRAME; - width = ds_get_width(ds); - height = ds_get_height(ds); - real_screen = SDL_SetVideoMode(width, height, 0, flags); + real_screen = SDL_SetVideoMode(width, height, bpp, flags); if (!real_screen) { fprintf(stderr, "Could not open SDL display\n"); exit(1); } +} - sdl_setdata(ds); +static void sdl_resize(DisplayState *ds) +{ + if (!allocator) { + do_sdl_resize(ds_get_width(ds), ds_get_height(ds), 0); + sdl_setdata(ds); + } else { + if (guest_screen != NULL) { + SDL_FreeSurface(guest_screen); + guest_screen = NULL; + } + } } +static PixelFormat sdl_to_qemu_pixelformat(SDL_PixelFormat *sdl_pf) +{ + PixelFormat qemu_pf; + + memset(&qemu_pf, 0x00, sizeof(PixelFormat)); + + qemu_pf.bits_per_pixel = sdl_pf->BitsPerPixel; + qemu_pf.bytes_per_pixel = sdl_pf->BytesPerPixel; + qemu_pf.depth = (qemu_pf.bits_per_pixel) == 32 ? 24 : (qemu_pf.bits_per_pixel); + + qemu_pf.rmask = sdl_pf->Rmask; + qemu_pf.gmask = sdl_pf->Gmask; + qemu_pf.bmask = sdl_pf->Bmask; + qemu_pf.amask = sdl_pf->Amask; + + qemu_pf.rshift = sdl_pf->Rshift; + qemu_pf.gshift = sdl_pf->Gshift; + qemu_pf.bshift = sdl_pf->Bshift; + qemu_pf.ashift = sdl_pf->Ashift; + + qemu_pf.rbits = 8 - sdl_pf->Rloss; + qemu_pf.gbits = 8 - sdl_pf->Gloss; + qemu_pf.bbits = 8 - sdl_pf->Bloss; + qemu_pf.abits = 8 - sdl_pf->Aloss; + + qemu_pf.rmax = ((1 << qemu_pf.rbits) - 1); + qemu_pf.gmax = ((1 << qemu_pf.gbits) - 1); + qemu_pf.bmax = ((1 << qemu_pf.bbits) - 1); + qemu_pf.amax = ((1 << qemu_pf.abits) - 1); + + return qemu_pf; +} + +static DisplaySurface* sdl_create_displaysurface(int width, int height) +{ + DisplaySurface *surface = (DisplaySurface*) qemu_mallocz(sizeof(DisplaySurface)); + if (surface == NULL) { + fprintf(stderr, "sdl_create_displaysurface: malloc failed\n"); + exit(1); + } + + surface->width = width; + surface->height = height; + + if (hostbpp == 16) + do_sdl_resize(width, height, 16); + else + do_sdl_resize(width, height, 32); + + surface->pf = sdl_to_qemu_pixelformat(real_screen->format); + surface->linesize = real_screen->pitch; + surface->data = real_screen->pixels; + +#ifdef WORDS_BIGENDIAN + surface->flags = QEMU_ALLOCATED_FLAG | QEMU_BIG_ENDIAN_FLAG; +#else + surface->flags = QEMU_ALLOCATED_FLAG; +#endif + allocator = 1; + + return surface; +} + +static void sdl_free_displaysurface(DisplaySurface *surface) +{ + allocator = 0; + if (surface == NULL) + return; + qemu_free(surface); +} + +static DisplaySurface* sdl_resize_displaysurface(DisplaySurface *surface, int width, int height) +{ + sdl_free_displaysurface(surface); + return sdl_create_displaysurface(width, height); +} + /* generic keyboard conversion */ #include "sdl_keysym.h" @@ -391,7 +480,7 @@ static void toggle_full_screen(DisplayState *ds) { gui_fullscreen = !gui_fullscreen; - sdl_resize(ds); + do_sdl_resize(real_screen->w, real_screen->h, real_screen->format->BitsPerPixel); if (gui_fullscreen) { gui_saved_grab = gui_grab; sdl_grab_start(); @@ -669,6 +758,8 @@ { int flags; uint8_t data = 0; + DisplayAllocator *da; + const SDL_VideoInfo *vi; #if defined(__APPLE__) /* always use generic keymaps */ @@ -689,6 +780,8 @@ fprintf(stderr, "Could not initialize SDL - exiting\n"); exit(1); } + vi = SDL_GetVideoInfo(); + hostbpp = vi->vfmt->BitsPerPixel; dcl = qemu_mallocz(sizeof(DisplayChangeListener)); dcl->dpy_update = sdl_update; @@ -700,6 +793,18 @@ ds->cursor_define = sdl_mouse_define; register_displaychangelistener(ds, dcl); + da = qemu_mallocz(sizeof(DisplayAllocator)); + da->create_displaysurface = sdl_create_displaysurface; + da->resize_displaysurface = sdl_resize_displaysurface; + da->free_displaysurface = sdl_free_displaysurface; + if (register_displayallocator(ds, da) == da) { + DisplaySurface *surf; + surf = sdl_create_displaysurface(ds_get_width(ds), ds_get_height(ds)); + defaultallocator_free_displaysurface(ds->surface); + ds->surface = surf; + dpy_resize(ds); + } + sdl_update_caption(); SDL_EnableKeyRepeat(250, 50); gui_grab = 0; Modified: trunk/vl.c =================================================================== --- trunk/vl.c 2009-03-13 09:35:55 UTC (rev 6838) +++ trunk/vl.c 2009-03-13 15:02:13 UTC (rev 6839) @@ -2874,6 +2874,12 @@ /***********************************************************/ /* register display */ +struct DisplayAllocator default_allocator = { + defaultallocator_create_displaysurface, + defaultallocator_resize_displaysurface, + defaultallocator_free_displaysurface +}; + void register_displaystate(DisplayState *ds) { DisplayState **s; @@ -2889,12 +2895,19 @@ 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); }