peterdelevoryas / rpms / qemu

Forked from rpms/qemu 2 years ago
Clone

Blame 0418-qxl-make-qxl_render_update-async.patch

56753f
From c26805e29312fee136008a57c70a2f5f140ba706 Mon Sep 17 00:00:00 2001
56753f
From: Alon Levy <alevy@redhat.com>
56753f
Date: Fri, 24 Feb 2012 23:19:31 +0200
56753f
Subject: [PATCH 418/434] qxl: make qxl_render_update async
56753f
56753f
RHBZ# 747011
56753f
56753f
Removes the last user of QXL_SYNC when using update drivers that use the
56753f
_ASYNC io ports.
56753f
56753f
The last user is qxl_render_update, it is called both by qxl_hw_update
56753f
which is the vga_hw_update_ptr passed to graphic_console_init, and by
56753f
qxl_hw_screen_dump.
56753f
56753f
At the same time the QXLRect area being passed to the red_worker thread
56753f
is passed as a copy, as part of the QXLCookie.
56753f
56753f
The implementation uses interface_update_area_complete with a bh to make
56753f
sure dpy_update and qxl_flip are called from the io thread, otherwise
56753f
the vga->ds->surface.data can change under our feet.
56753f
56753f
With this patch sdl+spice works fine. But spice by itself doesn't
56753f
produce the expected screendumps unless repeated a few times, due to
56753f
ppm_save being called before update_area (rendering done in spice server
56753f
thread) having a chance to complete. Fixed by next patch, but see commit
56753f
message for problem introduced by it.
56753f
56753f
Signed-off-by: Alon Levy <alevy@redhat.com>
56753f
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
56753f
---
56753f
 hw/qxl-render.c    |   96 +++++++++++++++++++++++++++++++++++++---------------
56753f
 hw/qxl.c           |   69 ++++++++++++++++++++++++++++++++++---
56753f
 hw/qxl.h           |   10 ++++++
56753f
 ui/spice-display.h |    6 ++++
56753f
 4 files changed, 150 insertions(+), 31 deletions(-)
56753f
56753f
diff --git a/hw/qxl-render.c b/hw/qxl-render.c
56753f
index 4c22166..4857838 100644
56753f
--- a/hw/qxl-render.c
56753f
+++ b/hw/qxl-render.c
56753f
@@ -82,17 +82,25 @@ void qxl_render_resize(PCIQXLDevice *qxl)
56753f
     }
56753f
 }
56753f
 
56753f
-void qxl_render_update(PCIQXLDevice *qxl)
56753f
+static void qxl_set_rect_to_surface(PCIQXLDevice *qxl, QXLRect *area)
56753f
+{
56753f
+    area->left   = 0;
56753f
+    area->right  = qxl->guest_primary.surface.width;
56753f
+    area->top    = 0;
56753f
+    area->bottom = qxl->guest_primary.surface.height;
56753f
+}
56753f
+
56753f
+static void qxl_render_update_area_unlocked(PCIQXLDevice *qxl)
56753f
 {
56753f
     VGACommonState *vga = &qxl->vga;
56753f
-    QXLRect dirty[32], update;
56753f
-    int i, redraw = 0;
56753f
+    int i;
56753f
     DisplaySurface *surface = vga->ds->surface;
56753f
 
56753f
     if (qxl->guest_primary.resized) {
56753f
         qxl->guest_primary.resized = 0;
56753f
-
56753f
         qxl->guest_primary.data = memory_region_get_ram_ptr(&qxl->vga.vram);
56753f
+        qxl_set_rect_to_surface(qxl, &qxl->dirty[0]);
56753f
+        qxl->num_dirty_rects = 1;
56753f
         dprint(qxl, 1, "%s: %dx%d, stride %d, bpp %d, depth %d\n",
56753f
                __FUNCTION__,
56753f
                qxl->guest_primary.surface.width,
56753f
@@ -103,9 +111,9 @@ void qxl_render_update(PCIQXLDevice *qxl)
56753f
     }
56753f
     if (surface->width != qxl->guest_primary.surface.width ||
56753f
         surface->height != qxl->guest_primary.surface.height) {
56753f
-        dprint(qxl, 1, "%s: resizing displaysurface to guest_primary\n",
56753f
-               __func__);
56753f
         if (qxl->guest_primary.qxl_stride > 0) {
56753f
+            dprint(qxl, 1, "%s: using guest_primary for displaysurface\n",
56753f
+                   __func__);
56753f
             qemu_free_displaysurface(vga->ds);
56753f
             qemu_create_displaysurface_from(qxl->guest_primary.surface.width,
56753f
                                             qxl->guest_primary.surface.height,
56753f
@@ -113,36 +121,70 @@ void qxl_render_update(PCIQXLDevice *qxl)
56753f
                                             qxl->guest_primary.abs_stride,
56753f
                                             qxl->guest_primary.data);
56753f
         } else {
56753f
+            dprint(qxl, 1, "%s: resizing displaysurface to guest_primary\n",
56753f
+                   __func__);
56753f
             qemu_resize_displaysurface(vga->ds,
56753f
                     qxl->guest_primary.surface.width,
56753f
                     qxl->guest_primary.surface.height);
56753f
         }
56753f
     }
56753f
-    update.left   = 0;
56753f
-    update.right  = qxl->guest_primary.surface.width;
56753f
-    update.top    = 0;
56753f
-    update.bottom = qxl->guest_primary.surface.height;
56753f
-
56753f
-    memset(dirty, 0, sizeof(dirty));
56753f
-    if (runstate_is_running() && qxl->guest_primary.commands) {
56753f
-        qxl->guest_primary.commands = 0;
56753f
-        qxl_spice_update_area(qxl, 0, &update,
56753f
-                              dirty, ARRAY_SIZE(dirty), 1, QXL_SYNC, NULL);
56753f
-    }
56753f
-    if (redraw) {
56753f
-        memset(dirty, 0, sizeof(dirty));
56753f
-        dirty[0] = update;
56753f
-    }
56753f
-    for (i = 0; i < ARRAY_SIZE(dirty); i++) {
56753f
-        if (qemu_spice_rect_is_empty(dirty+i)) {
56753f
+    for (i = 0; i < qxl->num_dirty_rects; i++) {
56753f
+        if (qemu_spice_rect_is_empty(qxl->dirty+i)) {
56753f
             break;
56753f
         }
56753f
-        qxl_flip(qxl, dirty+i);
56753f
+        qxl_flip(qxl, qxl->dirty+i);
56753f
         dpy_update(vga->ds,
56753f
-                   dirty[i].left, dirty[i].top,
56753f
-                   dirty[i].right - dirty[i].left,
56753f
-                   dirty[i].bottom - dirty[i].top);
56753f
+                   qxl->dirty[i].left, qxl->dirty[i].top,
56753f
+                   qxl->dirty[i].right - qxl->dirty[i].left,
56753f
+                   qxl->dirty[i].bottom - qxl->dirty[i].top);
56753f
+    }
56753f
+    qxl->num_dirty_rects = 0;
56753f
+}
56753f
+
56753f
+/*
56753f
+ * use ssd.lock to protect render_update_cookie_num.
56753f
+ * qxl_render_update is called by io thread or vcpu thread, and the completion
56753f
+ * callbacks are called by spice_server thread, defering to bh called from the
56753f
+ * io thread.
56753f
+ */
56753f
+void qxl_render_update(PCIQXLDevice *qxl)
56753f
+{
56753f
+    QXLCookie *cookie;
56753f
+
56753f
+    qemu_mutex_lock(&qxl->ssd.lock);
56753f
+
56753f
+    if (!runstate_is_running() || !qxl->guest_primary.commands) {
56753f
+        qxl_render_update_area_unlocked(qxl);
56753f
+        qemu_mutex_unlock(&qxl->ssd.lock);
56753f
+        return;
56753f
     }
56753f
+
56753f
+    qxl->guest_primary.commands = 0;
56753f
+    qxl->render_update_cookie_num++;
56753f
+    qemu_mutex_unlock(&qxl->ssd.lock);
56753f
+    cookie = qxl_cookie_new(QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
56753f
+                            0);
56753f
+    qxl_set_rect_to_surface(qxl, &cookie->u.render.area);
56753f
+    qxl_spice_update_area(qxl, 0, &cookie->u.render.area, NULL,
56753f
+                          0, 1 /* clear_dirty_region */, QXL_ASYNC, cookie);
56753f
+}
56753f
+
56753f
+void qxl_render_update_area_bh(void *opaque)
56753f
+{
56753f
+    PCIQXLDevice *qxl = opaque;
56753f
+
56753f
+    qemu_mutex_lock(&qxl->ssd.lock);
56753f
+    qxl_render_update_area_unlocked(qxl);
56753f
+    qemu_mutex_unlock(&qxl->ssd.lock);
56753f
+}
56753f
+
56753f
+void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie)
56753f
+{
56753f
+    qemu_mutex_lock(&qxl->ssd.lock);
56753f
+    qemu_bh_schedule(qxl->update_area_bh);
56753f
+    qxl->render_update_cookie_num--;
56753f
+    qemu_mutex_unlock(&qxl->ssd.lock);
56753f
+    g_free(cookie);
56753f
 }
56753f
 
56753f
 static QEMUCursor *qxl_cursor(PCIQXLDevice *qxl, QXLCursor *cursor)
56753f
diff --git a/hw/qxl.c b/hw/qxl.c
56753f
index e6e65d9..73be115 100644
56753f
--- a/hw/qxl.c
56753f
+++ b/hw/qxl.c
56753f
@@ -750,6 +750,11 @@ static void interface_async_complete_io(PCIQXLDevice *qxl, QXLCookie *cookie)
56753f
                 __func__, current_async, cookie->io);
56753f
     }
56753f
     switch (current_async) {
56753f
+    case QXL_IO_MEMSLOT_ADD_ASYNC:
56753f
+    case QXL_IO_DESTROY_PRIMARY_ASYNC:
56753f
+    case QXL_IO_UPDATE_AREA_ASYNC:
56753f
+    case QXL_IO_FLUSH_SURFACES_ASYNC:
56753f
+        break;
56753f
     case QXL_IO_CREATE_PRIMARY_ASYNC:
56753f
         qxl_create_guest_primary_complete(qxl);
56753f
         break;
56753f
@@ -759,11 +764,54 @@ static void interface_async_complete_io(PCIQXLDevice *qxl, QXLCookie *cookie)
56753f
     case QXL_IO_DESTROY_SURFACE_ASYNC:
56753f
         qxl_spice_destroy_surface_wait_complete(qxl, cookie->u.surface_id);
56753f
         break;
56753f
+    default:
56753f
+        fprintf(stderr, "qxl: %s: unexpected current_async %d\n", __func__,
56753f
+                current_async);
56753f
     }
56753f
     qxl_send_events(qxl, QXL_INTERRUPT_IO_CMD);
56753f
 }
56753f
 
56753f
 /* called from spice server thread context only */
56753f
+static void interface_update_area_complete(QXLInstance *sin,
56753f
+        uint32_t surface_id,
56753f
+        QXLRect *dirty, uint32_t num_updated_rects)
56753f
+{
56753f
+    PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
56753f
+    int i;
56753f
+    int qxl_i;
56753f
+
56753f
+    qemu_mutex_lock(&qxl->ssd.lock);
56753f
+    if (surface_id != 0 || !qxl->render_update_cookie_num) {
56753f
+        qemu_mutex_unlock(&qxl->ssd.lock);
56753f
+        return;
56753f
+    }
56753f
+    if (qxl->num_dirty_rects + num_updated_rects > QXL_NUM_DIRTY_RECTS) {
56753f
+        /*
56753f
+         * overflow - treat this as a full update. Not expected to be common.
56753f
+         */
56753f
+        dprint(qxl, 1, "%s: overflow of dirty rects\n", __func__);
56753f
+        qxl->guest_primary.resized = 1;
56753f
+    }
56753f
+    if (qxl->guest_primary.resized) {
56753f
+        /*
56753f
+         * Don't bother copying or scheduling the bh since we will flip
56753f
+         * the whole area anyway on completion of the update_area async call
56753f
+         */
56753f
+        qemu_mutex_unlock(&qxl->ssd.lock);
56753f
+        return;
56753f
+    }
56753f
+    qxl_i = qxl->num_dirty_rects;
56753f
+    for (i = 0; i < num_updated_rects; i++) {
56753f
+        qxl->dirty[qxl_i++] = dirty[i];
56753f
+    }
56753f
+    qxl->num_dirty_rects += num_updated_rects;
56753f
+    dprint(qxl, 1, "%s: scheduling update_area_bh, #dirty %d\n",
56753f
+           __func__, qxl->num_dirty_rects);
56753f
+    qemu_bh_schedule(qxl->update_area_bh);
56753f
+    qemu_mutex_unlock(&qxl->ssd.lock);
56753f
+}
56753f
+
56753f
+/* called from spice server thread context only */
56753f
 static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
56753f
 {
56753f
     PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
56753f
@@ -772,12 +820,16 @@ static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
56753f
     switch (cookie->type) {
56753f
     case QXL_COOKIE_TYPE_IO:
56753f
         interface_async_complete_io(qxl, cookie);
56753f
+        g_free(cookie);
56753f
+        break;
56753f
+    case QXL_COOKIE_TYPE_RENDER_UPDATE_AREA:
56753f
+        qxl_render_update_area_done(qxl, cookie);
56753f
         break;
56753f
     default:
56753f
         fprintf(stderr, "qxl: %s: unexpected cookie type %d\n",
56753f
                 __func__, cookie->type);
56753f
+        g_free(cookie);
56753f
     }
56753f
-    g_free(cookie);
56753f
 }
56753f
 
56753f
 static const QXLInterface qxl_interface = {
56753f
@@ -800,6 +852,7 @@ static const QXLInterface qxl_interface = {
56753f
     .notify_update           = interface_notify_update,
56753f
     .flush_resources         = interface_flush_resources,
56753f
     .async_complete          = interface_async_complete,
56753f
+    .update_area_complete    = interface_update_area_complete,
56753f
 };
56753f
 
56753f
 static void qxl_enter_vga_mode(PCIQXLDevice *d)
56753f
@@ -1216,11 +1269,17 @@ async_common:
56753f
     switch (io_port) {
56753f
     case QXL_IO_UPDATE_AREA:
56753f
     {
56753f
+        QXLCookie *cookie = NULL;
56753f
         QXLRect update = d->ram->update_area;
56753f
+
56753f
+        if (async == QXL_ASYNC) {
56753f
+            cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
56753f
+                                    QXL_IO_UPDATE_AREA_ASYNC);
56753f
+            cookie->u.area = update;
56753f
+        }
56753f
         qxl_spice_update_area(d, d->ram->update_surface,
56753f
-                              &update, NULL, 0, 0, async,
56753f
-                              qxl_cookie_new(QXL_COOKIE_TYPE_IO,
56753f
-                                             QXL_IO_UPDATE_AREA_ASYNC));
56753f
+                              cookie ? &cookie->u.area : &update,
56753f
+                              NULL, 0, 0, async, cookie);
56753f
         break;
56753f
     }
56753f
     case QXL_IO_NOTIFY_CMD:
56753f
@@ -1652,6 +1711,8 @@ static int qxl_init_common(PCIQXLDevice *qxl)
56753f
     init_pipe_signaling(qxl);
56753f
     qxl_reset_state(qxl);
56753f
 
56753f
+    qxl->update_area_bh = qemu_bh_new(qxl_render_update_area_bh, qxl);
56753f
+
56753f
     return 0;
56753f
 }
56753f
 
56753f
diff --git a/hw/qxl.h b/hw/qxl.h
56753f
index 1443925..86e415b 100644
56753f
--- a/hw/qxl.h
56753f
+++ b/hw/qxl.h
56753f
@@ -18,6 +18,8 @@ enum qxl_mode {
56753f
 
56753f
 #define QXL_UNDEFINED_IO UINT32_MAX
56753f
 
56753f
+#define QXL_NUM_DIRTY_RECTS 64
56753f
+
56753f
 typedef struct PCIQXLDevice {
56753f
     PCIDevice          pci;
56753f
     SimpleSpiceDisplay ssd;
56753f
@@ -93,6 +95,12 @@ typedef struct PCIQXLDevice {
56753f
     /* user-friendly properties (in megabytes) */
56753f
     uint32_t          ram_size_mb;
56753f
     uint32_t          vram_size_mb;
56753f
+
56753f
+    /* qxl_render_update state */
56753f
+    int                render_update_cookie_num;
56753f
+    int                num_dirty_rects;
56753f
+    QXLRect            dirty[QXL_NUM_DIRTY_RECTS];
56753f
+    QEMUBH            *update_area_bh;
56753f
 } PCIQXLDevice;
56753f
 
56753f
 #define PANIC_ON(x) if ((x)) {                         \
56753f
@@ -134,3 +142,5 @@ void qxl_log_command(PCIQXLDevice *qxl, const char *ring, QXLCommandExt *ext);
56753f
 void qxl_render_resize(PCIQXLDevice *qxl);
56753f
 void qxl_render_update(PCIQXLDevice *qxl);
56753f
 void qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext);
56753f
+void qxl_render_update_area_done(PCIQXLDevice *qxl, QXLCookie *cookie);
56753f
+void qxl_render_update_area_bh(void *opaque);
56753f
diff --git a/ui/spice-display.h b/ui/spice-display.h
56753f
index 8a010cb..12e50b6 100644
56753f
--- a/ui/spice-display.h
56753f
+++ b/ui/spice-display.h
56753f
@@ -50,6 +50,7 @@ typedef enum qxl_async_io {
56753f
 
56753f
 enum {
56753f
     QXL_COOKIE_TYPE_IO,
56753f
+    QXL_COOKIE_TYPE_RENDER_UPDATE_AREA,
56753f
 };
56753f
 
56753f
 typedef struct QXLCookie {
56753f
@@ -57,6 +58,11 @@ typedef struct QXLCookie {
56753f
     uint64_t io;
56753f
     union {
56753f
         uint32_t surface_id;
56753f
+        QXLRect area;
56753f
+        struct {
56753f
+            QXLRect area;
56753f
+            int redraw;
56753f
+        } render;
56753f
     } u;
56753f
 } QXLCookie;
56753f
 
56753f
-- 
56753f
1.7.10
56753f