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"
31 static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
37 vfprintf(stderr, fmt, args);
42 int qemu_spice_rect_is_empty(const QXLRect* r)
44 return r->top == r->bottom || r->left == r->right;
47 void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
49 if (qemu_spice_rect_is_empty(r)) {
53 if (qemu_spice_rect_is_empty(dest)) {
58 dest->top = MIN(dest->top, r->top);
59 dest->left = MIN(dest->left, r->left);
60 dest->bottom = MAX(dest->bottom, r->bottom);
61 dest->right = MAX(dest->right, r->right);
64 QXLCookie *qxl_cookie_new(int type, uint64_t io)
68 cookie = g_malloc0(sizeof(*cookie));
74 void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
77 trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
78 memslot->virt_start, memslot->virt_end,
81 if (async != QXL_SYNC) {
82 spice_qxl_add_memslot_async(&ssd->qxl, memslot,
83 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
84 QXL_IO_MEMSLOT_ADD_ASYNC));
86 spice_qxl_add_memslot(&ssd->qxl, memslot);
90 void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
92 trace_qemu_spice_del_memslot(ssd->qxl.id, gid, sid);
93 spice_qxl_del_memslot(&ssd->qxl, gid, sid);
96 void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
97 QXLDevSurfaceCreate *surface,
100 trace_qemu_spice_create_primary_surface(ssd->qxl.id, id, surface, async);
101 if (async != QXL_SYNC) {
102 spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface,
103 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
104 QXL_IO_CREATE_PRIMARY_ASYNC));
106 spice_qxl_create_primary_surface(&ssd->qxl, id, surface);
110 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
111 uint32_t id, qxl_async_io async)
113 trace_qemu_spice_destroy_primary_surface(ssd->qxl.id, id, async);
114 if (async != QXL_SYNC) {
115 spice_qxl_destroy_primary_surface_async(&ssd->qxl, id,
116 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
117 QXL_IO_DESTROY_PRIMARY_ASYNC));
119 spice_qxl_destroy_primary_surface(&ssd->qxl, id);
123 void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
125 trace_qemu_spice_wakeup(ssd->qxl.id);
126 spice_qxl_wakeup(&ssd->qxl);
129 static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
132 SimpleSpiceUpdate *update;
133 QXLDrawable *drawable;
137 struct timespec time_space;
138 pixman_image_t *dest;
140 trace_qemu_spice_create_update(
141 rect->left, rect->right,
142 rect->top, rect->bottom);
144 update = g_malloc0(sizeof(*update));
145 drawable = &update->drawable;
146 image = &update->image;
147 cmd = &update->ext.cmd;
149 bw = rect->right - rect->left;
150 bh = rect->bottom - rect->top;
151 update->bitmap = g_malloc(bw * bh * 4);
153 drawable->bbox = *rect;
154 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
155 drawable->effect = QXL_EFFECT_OPAQUE;
156 drawable->release_info.id = (uintptr_t)(&update->ext);
157 drawable->type = QXL_DRAW_COPY;
158 drawable->surfaces_dest[0] = -1;
159 drawable->surfaces_dest[1] = -1;
160 drawable->surfaces_dest[2] = -1;
161 clock_gettime(CLOCK_MONOTONIC, &time_space);
162 /* time in milliseconds from epoch. */
163 drawable->mm_time = time_space.tv_sec * 1000
164 + time_space.tv_nsec / 1000 / 1000;
166 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
167 drawable->u.copy.src_bitmap = (uintptr_t)image;
168 drawable->u.copy.src_area.right = bw;
169 drawable->u.copy.src_area.bottom = bh;
171 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
172 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
173 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
174 image->bitmap.stride = bw * 4;
175 image->descriptor.width = image->bitmap.x = bw;
176 image->descriptor.height = image->bitmap.y = bh;
177 image->bitmap.data = (uintptr_t)(update->bitmap);
178 image->bitmap.palette = 0;
179 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
181 dest = pixman_image_create_bits(PIXMAN_x8r8g8b8, bw, bh,
182 (void *)update->bitmap, bw * 4);
183 pixman_image_composite(PIXMAN_OP_SRC, ssd->surface, NULL, ssd->mirror,
184 rect->left, rect->top, 0, 0,
185 rect->left, rect->top, bw, bh);
186 pixman_image_composite(PIXMAN_OP_SRC, ssd->mirror, NULL, dest,
187 rect->left, rect->top, 0, 0,
189 pixman_image_unref(dest);
191 cmd->type = QXL_CMD_DRAW;
192 cmd->data = (uintptr_t)drawable;
194 QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
197 static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
199 static const int blksize = 32;
200 int blocks = (surface_width(ssd->ds) + blksize - 1) / blksize;
201 int dirty_top[blocks];
202 int y, yoff, x, xoff, blk, bw;
203 int bpp = surface_bytes_per_pixel(ssd->ds);
204 uint8_t *guest, *mirror;
206 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
210 if (ssd->surface == NULL) {
211 ssd->surface = pixman_image_ref(ssd->ds->image);
212 ssd->mirror = qemu_pixman_mirror_create(ssd->ds->format,
216 for (blk = 0; blk < blocks; blk++) {
220 guest = surface_data(ssd->ds);
221 mirror = (void *)pixman_image_get_data(ssd->mirror);
222 for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
223 yoff = y * surface_stride(ssd->ds);
224 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
227 bw = MIN(blksize, ssd->dirty.right - x);
228 if (memcmp(guest + yoff + xoff,
229 mirror + yoff + xoff,
231 if (dirty_top[blk] != -1) {
233 .top = dirty_top[blk],
238 qemu_spice_create_one_update(ssd, &update);
242 if (dirty_top[blk] == -1) {
249 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
251 bw = MIN(blksize, ssd->dirty.right - x);
252 if (dirty_top[blk] != -1) {
254 .top = dirty_top[blk],
255 .bottom = ssd->dirty.bottom,
259 qemu_spice_create_one_update(ssd, &update);
264 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
267 static SimpleSpiceCursor*
268 qemu_spice_create_cursor_update(SimpleSpiceDisplay *ssd,
271 size_t size = c ? c->width * c->height * 4 : 0;
272 SimpleSpiceCursor *update;
277 update = g_malloc0(sizeof(*update) + size);
279 cursor = &update->cursor;
280 cmd = &update->ext.cmd;
283 ccmd->type = QXL_CURSOR_SET;
284 ccmd->u.set.position.x = ssd->ptr_x;
285 ccmd->u.set.position.y = ssd->ptr_y;
286 ccmd->u.set.visible = true;
287 ccmd->u.set.shape = (uintptr_t)cursor;
288 cursor->header.unique = ssd->unique++;
289 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
290 cursor->header.width = c->width;
291 cursor->header.height = c->height;
292 cursor->header.hot_spot_x = c->hot_x;
293 cursor->header.hot_spot_y = c->hot_y;
294 cursor->data_size = size;
295 cursor->chunk.data_size = size;
296 memcpy(cursor->chunk.data, c->data, size);
298 ccmd->type = QXL_CURSOR_MOVE;
299 ccmd->u.position.x = ssd->ptr_x;
300 ccmd->u.position.y = ssd->ptr_y;
302 ccmd->release_info.id = (uintptr_t)(&update->ext);
304 cmd->type = QXL_CMD_CURSOR;
305 cmd->data = (uintptr_t)ccmd;
311 * Called from spice server thread context (via interface_release_resource)
312 * We do *not* hold the global qemu mutex here, so extra care is needed
313 * when calling qemu functions. QEMU interfaces used:
314 * - g_free (underlying glibc free is re-entrant).
316 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
318 g_free(update->bitmap);
322 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
324 QXLDevMemSlot memslot;
326 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
328 memset(&memslot, 0, sizeof(memslot));
329 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
330 memslot.virt_end = ~0;
331 qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
334 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
336 QXLDevSurfaceCreate surface;
338 memset(&surface, 0, sizeof(surface));
340 dprint(1, "%s/%d: %dx%d\n", __func__, ssd->qxl.id,
341 surface_width(ssd->ds), surface_height(ssd->ds));
343 surface.format = SPICE_SURFACE_FMT_32_xRGB;
344 surface.width = surface_width(ssd->ds);
345 surface.height = surface_height(ssd->ds);
346 surface.stride = -surface.width * 4;
347 surface.mouse_mode = true;
350 surface.mem = (uintptr_t)ssd->buf;
351 surface.group_id = MEMSLOT_GROUP_HOST;
353 qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
356 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
358 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
360 qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
363 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
365 qemu_mutex_init(&ssd->lock);
366 QTAILQ_INIT(&ssd->updates);
369 if (ssd->num_surfaces == 0) {
370 ssd->num_surfaces = 1024;
372 ssd->bufsize = (16 * 1024 * 1024);
373 ssd->buf = g_malloc(ssd->bufsize);
376 /* display listener callbacks */
378 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
379 int x, int y, int w, int h)
383 dprint(2, "%s/%d: x %d y %d w %d h %d\n", __func__,
384 ssd->qxl.id, x, y, w, h);
385 update_area.left = x,
386 update_area.right = x + w;
388 update_area.bottom = y + h;
390 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
393 qemu_spice_rect_union(&ssd->dirty, &update_area);
396 void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
397 DisplaySurface *surface)
399 SimpleSpiceUpdate *update;
402 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
404 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
406 pixman_image_unref(ssd->surface);
408 pixman_image_unref(ssd->mirror);
412 qemu_mutex_lock(&ssd->lock);
413 need_destroy = (ssd->ds != NULL);
415 while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
416 QTAILQ_REMOVE(&ssd->updates, update, next);
417 qemu_spice_destroy_update(ssd, update);
419 qemu_mutex_unlock(&ssd->lock);
421 qemu_spice_destroy_host_primary(ssd);
424 qemu_spice_create_host_primary(ssd);
427 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
431 void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
434 assert(ssd->dcl.con);
435 dpy_cursor_define(ssd->dcl.con, ssd->cursor);
436 cursor_put(ssd->cursor);
439 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
440 assert(ssd->dcl.con);
441 dpy_mouse_set(ssd->dcl.con, ssd->mouse_x, ssd->mouse_y, 1);
447 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
449 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
450 graphic_hw_update(ssd->dcl.con);
452 qemu_mutex_lock(&ssd->lock);
453 if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
454 qemu_spice_create_update(ssd);
457 qemu_spice_cursor_refresh_unlocked(ssd);
458 qemu_mutex_unlock(&ssd->lock);
462 qemu_spice_wakeup(ssd);
463 dprint(2, "%s/%d: notify\n", __func__, ssd->qxl.id);
467 /* spice display interface callbacks */
469 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
471 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
473 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
474 ssd->worker = qxl_worker;
477 static void interface_set_compression_level(QXLInstance *sin, int level)
479 dprint(1, "%s/%d:\n", __func__, sin->id);
483 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
485 dprint(3, "%s/%d:\n", __func__, sin->id);
489 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
491 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
493 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
494 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
495 info->num_memslots = NUM_MEMSLOTS;
496 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
497 info->internal_groupslot_id = 0;
498 info->qxl_ram_size = ssd->bufsize;
499 info->n_surfaces = ssd->num_surfaces;
502 static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
504 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
505 SimpleSpiceUpdate *update;
508 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
510 qemu_mutex_lock(&ssd->lock);
511 update = QTAILQ_FIRST(&ssd->updates);
512 if (update != NULL) {
513 QTAILQ_REMOVE(&ssd->updates, update, next);
517 qemu_mutex_unlock(&ssd->lock);
522 static int interface_req_cmd_notification(QXLInstance *sin)
524 dprint(1, "%s/%d:\n", __func__, sin->id);
528 static void interface_release_resource(QXLInstance *sin,
529 struct QXLReleaseInfoExt rext)
531 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
532 SimpleSpiceUpdate *update;
533 SimpleSpiceCursor *cursor;
536 dprint(2, "%s/%d:\n", __func__, ssd->qxl.id);
537 ext = (void *)(rext.info->id);
538 switch (ext->cmd.type) {
540 update = container_of(ext, SimpleSpiceUpdate, ext);
541 qemu_spice_destroy_update(ssd, update);
544 cursor = container_of(ext, SimpleSpiceCursor, ext);
548 g_assert_not_reached();
552 static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
554 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
557 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
559 qemu_mutex_lock(&ssd->lock);
560 if (ssd->ptr_define) {
561 *ext = ssd->ptr_define->ext;
562 ssd->ptr_define = NULL;
564 } else if (ssd->ptr_move) {
565 *ext = ssd->ptr_move->ext;
566 ssd->ptr_move = NULL;
571 qemu_mutex_unlock(&ssd->lock);
575 static int interface_req_cursor_notification(QXLInstance *sin)
577 dprint(1, "%s:\n", __FUNCTION__);
581 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
583 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
587 static int interface_flush_resources(QXLInstance *sin)
589 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
594 static void interface_update_area_complete(QXLInstance *sin,
596 QXLRect *dirty, uint32_t num_updated_rects)
598 /* should never be called, used in qxl native mode only */
599 fprintf(stderr, "%s: abort()\n", __func__);
603 /* called from spice server thread context only */
604 static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
606 /* should never be called, used in qxl native mode only */
607 fprintf(stderr, "%s: abort()\n", __func__);
611 static void interface_set_client_capabilities(QXLInstance *sin,
612 uint8_t client_present,
615 dprint(3, "%s:\n", __func__);
618 static int interface_client_monitors_config(QXLInstance *sin,
619 VDAgentMonitorsConfig *mc)
621 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
630 * FIXME: multihead is tricky due to the way
631 * spice has multihead implemented.
633 memset(&info, 0, sizeof(info));
634 if (mc->num_of_monitors > 0) {
635 info.width = mc->monitors[0].width;
636 info.height = mc->monitors[0].height;
638 rc = dpy_set_ui_info(ssd->dcl.con, &info);
639 dprint(1, "%s/%d: size %dx%d, rc %d <--- ==========================\n",
640 __func__, ssd->qxl.id, info.width, info.height, rc);
642 return 0; /* == not supported by guest */
648 static const QXLInterface dpy_interface = {
649 .base.type = SPICE_INTERFACE_QXL,
650 .base.description = "qemu simple display",
651 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
652 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
654 .attache_worker = interface_attach_worker,
655 .set_compression_level = interface_set_compression_level,
656 .set_mm_time = interface_set_mm_time,
657 .get_init_info = interface_get_init_info,
659 /* the callbacks below are called from spice server thread context */
660 .get_command = interface_get_command,
661 .req_cmd_notification = interface_req_cmd_notification,
662 .release_resource = interface_release_resource,
663 .get_cursor_command = interface_get_cursor_command,
664 .req_cursor_notification = interface_req_cursor_notification,
665 .notify_update = interface_notify_update,
666 .flush_resources = interface_flush_resources,
667 .async_complete = interface_async_complete,
668 .update_area_complete = interface_update_area_complete,
669 .set_client_capabilities = interface_set_client_capabilities,
670 .client_monitors_config = interface_client_monitors_config,
673 static void display_update(DisplayChangeListener *dcl,
674 int x, int y, int w, int h)
676 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
677 qemu_spice_display_update(ssd, x, y, w, h);
680 static void display_switch(DisplayChangeListener *dcl,
681 struct DisplaySurface *surface)
683 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
684 qemu_spice_display_switch(ssd, surface);
687 static void display_refresh(DisplayChangeListener *dcl)
689 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
690 qemu_spice_display_refresh(ssd);
693 static void display_mouse_set(DisplayChangeListener *dcl,
694 int x, int y, int on)
696 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
698 qemu_mutex_lock(&ssd->lock);
702 g_free(ssd->ptr_move);
704 ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL);
705 qemu_mutex_unlock(&ssd->lock);
708 static void display_mouse_define(DisplayChangeListener *dcl,
711 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
713 qemu_mutex_lock(&ssd->lock);
715 g_free(ssd->ptr_move);
716 ssd->ptr_move = NULL;
718 if (ssd->ptr_define) {
719 g_free(ssd->ptr_define);
721 ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c);
722 qemu_mutex_unlock(&ssd->lock);
725 static const DisplayChangeListenerOps display_listener_ops = {
727 .dpy_gfx_update = display_update,
728 .dpy_gfx_switch = display_switch,
729 .dpy_refresh = display_refresh,
730 .dpy_mouse_set = display_mouse_set,
731 .dpy_cursor_define = display_mouse_define,
734 static void qemu_spice_display_init_one(QemuConsole *con)
736 SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
738 qemu_spice_display_init_common(ssd);
740 ssd->qxl.base.sif = &dpy_interface.base;
741 qemu_spice_add_display_interface(&ssd->qxl, con);
744 qemu_spice_create_host_memslot(ssd);
746 ssd->dcl.ops = &display_listener_ops;
748 register_displaychangelistener(&ssd->dcl);
751 void qemu_spice_display_init(void)
757 con = qemu_console_lookup_by_index(i);
758 if (!con || !qemu_console_is_graphic(con)) {
761 if (qemu_spice_have_display_interface(con)) {
764 qemu_spice_display_init_one(con);