2 * Copyright (C) 2010 Red Hat, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 or
7 * (at your option) version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "qemu-common.h"
19 #include "ui/qemu-spice.h"
20 #include "qemu/timer.h"
21 #include "qemu/queue.h"
22 #include "monitor/monitor.h"
23 #include "ui/console.h"
24 #include "sysemu/sysemu.h"
27 #include "ui/spice-display.h"
35 static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
41 vfprintf(stderr, fmt, args);
46 int qemu_spice_rect_is_empty(const QXLRect* r)
48 return r->top == r->bottom || r->left == r->right;
51 void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
53 if (qemu_spice_rect_is_empty(r)) {
57 if (qemu_spice_rect_is_empty(dest)) {
62 dest->top = MIN(dest->top, r->top);
63 dest->left = MIN(dest->left, r->left);
64 dest->bottom = MAX(dest->bottom, r->bottom);
65 dest->right = MAX(dest->right, r->right);
68 QXLCookie *qxl_cookie_new(int type, uint64_t io)
72 cookie = g_malloc0(sizeof(*cookie));
78 void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
81 trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
82 memslot->virt_start, memslot->virt_end,
85 if (async != QXL_SYNC) {
86 spice_qxl_add_memslot_async(&ssd->qxl, memslot,
87 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
88 QXL_IO_MEMSLOT_ADD_ASYNC));
90 spice_qxl_add_memslot(&ssd->qxl, memslot);
94 void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
96 trace_qemu_spice_del_memslot(ssd->qxl.id, gid, sid);
97 spice_qxl_del_memslot(&ssd->qxl, gid, sid);
100 void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
101 QXLDevSurfaceCreate *surface,
104 trace_qemu_spice_create_primary_surface(ssd->qxl.id, id, surface, async);
105 if (async != QXL_SYNC) {
106 spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface,
107 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
108 QXL_IO_CREATE_PRIMARY_ASYNC));
110 spice_qxl_create_primary_surface(&ssd->qxl, id, surface);
114 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
115 uint32_t id, qxl_async_io async)
117 trace_qemu_spice_destroy_primary_surface(ssd->qxl.id, id, async);
118 if (async != QXL_SYNC) {
119 spice_qxl_destroy_primary_surface_async(&ssd->qxl, id,
120 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
121 QXL_IO_DESTROY_PRIMARY_ASYNC));
123 spice_qxl_destroy_primary_surface(&ssd->qxl, id);
127 void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
129 trace_qemu_spice_wakeup(ssd->qxl.id);
130 spice_qxl_wakeup(&ssd->qxl);
133 static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
136 SimpleSpiceUpdate *update;
137 QXLDrawable *drawable;
141 struct timespec time_space;
142 pixman_image_t *dest;
144 trace_qemu_spice_create_update(
145 rect->left, rect->right,
146 rect->top, rect->bottom);
148 update = g_malloc0(sizeof(*update));
149 drawable = &update->drawable;
150 image = &update->image;
151 cmd = &update->ext.cmd;
153 bw = rect->right - rect->left;
154 bh = rect->bottom - rect->top;
155 update->bitmap = g_malloc(bw * bh * 4);
157 drawable->bbox = *rect;
158 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
159 drawable->effect = QXL_EFFECT_OPAQUE;
160 drawable->release_info.id = (uintptr_t)update;
161 drawable->type = QXL_DRAW_COPY;
162 drawable->surfaces_dest[0] = -1;
163 drawable->surfaces_dest[1] = -1;
164 drawable->surfaces_dest[2] = -1;
165 clock_gettime(CLOCK_MONOTONIC, &time_space);
166 /* time in milliseconds from epoch. */
167 drawable->mm_time = time_space.tv_sec * 1000
168 + time_space.tv_nsec / 1000 / 1000;
170 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
171 drawable->u.copy.src_bitmap = (uintptr_t)image;
172 drawable->u.copy.src_area.right = bw;
173 drawable->u.copy.src_area.bottom = bh;
175 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
176 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
177 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
178 image->bitmap.stride = bw * 4;
179 image->descriptor.width = image->bitmap.x = bw;
180 image->descriptor.height = image->bitmap.y = bh;
181 image->bitmap.data = (uintptr_t)(update->bitmap);
182 image->bitmap.palette = 0;
183 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
185 dest = pixman_image_create_bits(PIXMAN_x8r8g8b8, bw, bh,
186 (void *)update->bitmap, bw * 4);
187 pixman_image_composite(PIXMAN_OP_SRC, ssd->surface, NULL, ssd->mirror,
188 rect->left, rect->top, 0, 0,
189 rect->left, rect->top, bw, bh);
190 pixman_image_composite(PIXMAN_OP_SRC, ssd->mirror, NULL, dest,
191 rect->left, rect->top, 0, 0,
193 pixman_image_unref(dest);
195 cmd->type = QXL_CMD_DRAW;
196 cmd->data = (uintptr_t)drawable;
198 QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
201 static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
204 static const int blksize = 32;
205 int blocks = (surface_width(ssd->ds) + blksize - 1) / blksize;
206 int dirty_top[blocks];
207 int y, yoff, x, xoff, blk, bw;
208 int bpp = surface_bytes_per_pixel(ssd->ds);
209 uint8_t *guest, *mirror;
212 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
216 if (ssd->surface == NULL) {
217 ssd->surface = pixman_image_ref(ssd->ds->image);
218 ssd->mirror = qemu_pixman_mirror_create(ssd->ds->format,
223 for (blk = 0; blk < blocks; blk++) {
227 guest = surface_data(ssd->ds);
228 mirror = (void *)pixman_image_get_data(ssd->mirror);
229 for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
230 yoff = y * surface_stride(ssd->ds);
231 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
234 bw = MIN(blksize, ssd->dirty.right - x);
235 if (memcmp(guest + yoff + xoff,
236 mirror + yoff + xoff,
238 if (dirty_top[blk] != -1) {
240 .top = dirty_top[blk],
245 qemu_spice_create_one_update(ssd, &update);
249 if (dirty_top[blk] == -1) {
256 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
258 bw = MIN(blksize, ssd->dirty.right - x);
259 if (dirty_top[blk] != -1) {
261 .top = dirty_top[blk],
262 .bottom = ssd->dirty.bottom,
266 qemu_spice_create_one_update(ssd, &update);
273 pixman_transform_t matrix;
274 struct pixman_f_transform matrix_f;
276 pixman_f_transform_init_identity(&matrix_f);
277 pixman_f_transform_scale(&matrix_f, NULL, RATIO_SCALE, RATIO_SCALE);
278 pixman_transform_from_pixman_f_transform(&matrix, &matrix_f);
279 pixman_image_set_transform(ssd->ds->image, &matrix);
280 pixman_image_set_filter(ssd->ds->image, PIXMAN_FILTER_BEST, NULL, 0);
284 .bottom = surface_height(ssd->ds),
286 .right = surface_width(ssd->ds),
289 qemu_spice_create_one_update(ssd, &update);
292 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
296 * Called from spice server thread context (via interface_release_resource)
297 * We do *not* hold the global qemu mutex here, so extra care is needed
298 * when calling qemu functions. QEMU interfaces used:
299 * - g_free (underlying glibc free is re-entrant).
301 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
303 g_free(update->bitmap);
307 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
309 QXLDevMemSlot memslot;
311 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
313 memset(&memslot, 0, sizeof(memslot));
314 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
315 memslot.virt_end = ~0;
316 qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
319 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
321 QXLDevSurfaceCreate surface;
323 memset(&surface, 0, sizeof(surface));
325 dprint(1, "%s/%d: %dx%d\n", __func__, ssd->qxl.id,
326 surface_width(ssd->ds), surface_height(ssd->ds));
328 surface.format = SPICE_SURFACE_FMT_32_xRGB;
329 surface.width = surface_width(ssd->ds);
330 surface.height = surface_height(ssd->ds);
331 surface.stride = -surface.width * 4;
332 surface.mouse_mode = true;
335 surface.mem = (uintptr_t)ssd->buf;
336 surface.group_id = MEMSLOT_GROUP_HOST;
339 surface.width /= RATIO_SCALE;
340 surface.height /= RATIO_SCALE;
343 qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
346 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
348 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
350 qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
353 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
355 qemu_mutex_init(&ssd->lock);
356 QTAILQ_INIT(&ssd->updates);
359 if (ssd->num_surfaces == 0) {
360 ssd->num_surfaces = 1024;
362 ssd->bufsize = (16 * 1024 * 1024);
363 ssd->buf = g_malloc(ssd->bufsize);
366 /* display listener callbacks */
368 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
369 int x, int y, int w, int h)
373 dprint(2, "%s/%d: x %d y %d w %d h %d\n", __func__,
374 ssd->qxl.id, x, y, w, h);
375 update_area.left = x,
376 update_area.right = x + w;
378 update_area.bottom = y + h;
380 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
383 qemu_spice_rect_union(&ssd->dirty, &update_area);
386 void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
387 DisplaySurface *surface)
389 SimpleSpiceUpdate *update;
392 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
394 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
396 pixman_image_unref(ssd->surface);
398 pixman_image_unref(ssd->mirror);
402 qemu_mutex_lock(&ssd->lock);
403 need_destroy = (ssd->ds != NULL);
405 while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
406 QTAILQ_REMOVE(&ssd->updates, update, next);
407 qemu_spice_destroy_update(ssd, update);
409 qemu_mutex_unlock(&ssd->lock);
411 qemu_spice_destroy_host_primary(ssd);
414 qemu_spice_create_host_primary(ssd);
417 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
421 void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
424 assert(ssd->dcl.con);
425 dpy_cursor_define(ssd->dcl.con, ssd->cursor);
426 cursor_put(ssd->cursor);
429 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
430 assert(ssd->dcl.con);
431 dpy_mouse_set(ssd->dcl.con, ssd->mouse_x, ssd->mouse_y, 1);
437 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
439 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
440 graphic_hw_update(ssd->dcl.con);
442 qemu_mutex_lock(&ssd->lock);
443 if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
444 qemu_spice_create_update(ssd);
447 qemu_spice_cursor_refresh_unlocked(ssd);
448 qemu_mutex_unlock(&ssd->lock);
452 qemu_spice_wakeup(ssd);
453 dprint(2, "%s/%d: notify\n", __func__, ssd->qxl.id);
457 /* spice display interface callbacks */
459 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
461 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
463 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
464 ssd->worker = qxl_worker;
467 static void interface_set_compression_level(QXLInstance *sin, int level)
469 dprint(1, "%s/%d:\n", __func__, sin->id);
473 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
475 dprint(3, "%s/%d:\n", __func__, sin->id);
479 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
481 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
483 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
484 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
485 info->num_memslots = NUM_MEMSLOTS;
486 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
487 info->internal_groupslot_id = 0;
488 info->qxl_ram_size = ssd->bufsize;
489 info->n_surfaces = ssd->num_surfaces;
492 static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
494 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
495 SimpleSpiceUpdate *update;
498 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
500 qemu_mutex_lock(&ssd->lock);
501 update = QTAILQ_FIRST(&ssd->updates);
502 if (update != NULL) {
503 QTAILQ_REMOVE(&ssd->updates, update, next);
507 qemu_mutex_unlock(&ssd->lock);
512 static int interface_req_cmd_notification(QXLInstance *sin)
514 dprint(1, "%s/%d:\n", __func__, sin->id);
518 static void interface_release_resource(QXLInstance *sin,
519 struct QXLReleaseInfoExt ext)
521 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
524 dprint(2, "%s/%d:\n", __func__, ssd->qxl.id);
526 qemu_spice_destroy_update(ssd, (void*)id);
529 static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
531 dprint(3, "%s:\n", __FUNCTION__);
535 static int interface_req_cursor_notification(QXLInstance *sin)
537 dprint(1, "%s:\n", __FUNCTION__);
541 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
543 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
547 static int interface_flush_resources(QXLInstance *sin)
549 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
554 static void interface_update_area_complete(QXLInstance *sin,
556 QXLRect *dirty, uint32_t num_updated_rects)
558 /* should never be called, used in qxl native mode only */
559 fprintf(stderr, "%s: abort()\n", __func__);
563 /* called from spice server thread context only */
564 static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
566 /* should never be called, used in qxl native mode only */
567 fprintf(stderr, "%s: abort()\n", __func__);
571 static void interface_set_client_capabilities(QXLInstance *sin,
572 uint8_t client_present,
575 dprint(3, "%s:\n", __func__);
578 static int interface_client_monitors_config(QXLInstance *sin,
579 VDAgentMonitorsConfig *mc)
581 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
590 * FIXME: multihead is tricky due to the way
591 * spice has multihead implemented.
593 memset(&info, 0, sizeof(info));
594 if (mc->num_of_monitors > 0) {
595 info.width = mc->monitors[0].width;
596 info.height = mc->monitors[0].height;
598 rc = dpy_set_ui_info(ssd->dcl.con, &info);
599 dprint(1, "%s/%d: size %dx%d, rc %d <--- ==========================\n",
600 __func__, ssd->qxl.id, info.width, info.height, rc);
602 return 0; /* == not supported by guest */
608 static const QXLInterface dpy_interface = {
609 .base.type = SPICE_INTERFACE_QXL,
610 .base.description = "qemu simple display",
611 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
612 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
614 .attache_worker = interface_attach_worker,
615 .set_compression_level = interface_set_compression_level,
616 .set_mm_time = interface_set_mm_time,
617 .get_init_info = interface_get_init_info,
619 /* the callbacks below are called from spice server thread context */
620 .get_command = interface_get_command,
621 .req_cmd_notification = interface_req_cmd_notification,
622 .release_resource = interface_release_resource,
623 .get_cursor_command = interface_get_cursor_command,
624 .req_cursor_notification = interface_req_cursor_notification,
625 .notify_update = interface_notify_update,
626 .flush_resources = interface_flush_resources,
627 .async_complete = interface_async_complete,
628 .update_area_complete = interface_update_area_complete,
629 .set_client_capabilities = interface_set_client_capabilities,
630 .client_monitors_config = interface_client_monitors_config,
633 static void display_update(DisplayChangeListener *dcl,
634 int x, int y, int w, int h)
636 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
637 qemu_spice_display_update(ssd, x, y, w, h);
640 static void display_switch(DisplayChangeListener *dcl,
641 struct DisplaySurface *surface)
643 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
644 qemu_spice_display_switch(ssd, surface);
647 static void display_refresh(DisplayChangeListener *dcl)
649 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
650 qemu_spice_display_refresh(ssd);
653 static const DisplayChangeListenerOps display_listener_ops = {
655 .dpy_gfx_update = display_update,
656 .dpy_gfx_switch = display_switch,
657 .dpy_refresh = display_refresh,
660 static void qemu_spice_display_init_one(QemuConsole *con)
662 SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
664 qemu_spice_display_init_common(ssd);
666 ssd->qxl.base.sif = &dpy_interface.base;
667 qemu_spice_add_display_interface(&ssd->qxl, con);
670 qemu_spice_create_host_memslot(ssd);
672 ssd->dcl.ops = &display_listener_ops;
674 register_displaychangelistener(&ssd->dcl);
677 void qemu_spice_display_init(void)
683 con = qemu_console_lookup_by_index(i);
684 if (!con || !qemu_console_is_graphic(con)) {
687 if (qemu_spice_have_display_interface(con)) {
690 qemu_spice_display_init_one(con);