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 int spice_display_is_running
;
131 void qemu_spice_display_start(void)
133 spice_display_is_running
= true;
136 void qemu_spice_display_stop(void)
138 spice_display_is_running
= false;
141 int qemu_spice_display_is_running(SimpleSpiceDisplay
*ssd
)
143 return spice_display_is_running
;
146 static void qemu_spice_create_one_update(SimpleSpiceDisplay
*ssd
,
149 SimpleSpiceUpdate
*update
;
150 QXLDrawable
*drawable
;
154 struct timespec time_space
;
155 pixman_image_t
*dest
;
157 trace_qemu_spice_create_update(
158 rect
->left
, rect
->right
,
159 rect
->top
, rect
->bottom
);
161 update
= g_malloc0(sizeof(*update
));
162 drawable
= &update
->drawable
;
163 image
= &update
->image
;
164 cmd
= &update
->ext
.cmd
;
166 bw
= rect
->right
- rect
->left
;
167 bh
= rect
->bottom
- rect
->top
;
168 update
->bitmap
= g_malloc(bw
* bh
* 4);
170 drawable
->bbox
= *rect
;
171 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
172 drawable
->effect
= QXL_EFFECT_OPAQUE
;
173 drawable
->release_info
.id
= (uintptr_t)update
;
174 drawable
->type
= QXL_DRAW_COPY
;
175 drawable
->surfaces_dest
[0] = -1;
176 drawable
->surfaces_dest
[1] = -1;
177 drawable
->surfaces_dest
[2] = -1;
178 clock_gettime(CLOCK_MONOTONIC
, &time_space
);
179 /* time in milliseconds from epoch. */
180 drawable
->mm_time
= time_space
.tv_sec
* 1000
181 + time_space
.tv_nsec
/ 1000 / 1000;
183 drawable
->u
.copy
.rop_descriptor
= SPICE_ROPD_OP_PUT
;
184 drawable
->u
.copy
.src_bitmap
= (uintptr_t)image
;
185 drawable
->u
.copy
.src_area
.right
= bw
;
186 drawable
->u
.copy
.src_area
.bottom
= bh
;
188 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, ssd
->unique
++);
189 image
->descriptor
.type
= SPICE_IMAGE_TYPE_BITMAP
;
190 image
->bitmap
.flags
= QXL_BITMAP_DIRECT
| QXL_BITMAP_TOP_DOWN
;
191 image
->bitmap
.stride
= bw
* 4;
192 image
->descriptor
.width
= image
->bitmap
.x
= bw
;
193 image
->descriptor
.height
= image
->bitmap
.y
= bh
;
194 image
->bitmap
.data
= (uintptr_t)(update
->bitmap
);
195 image
->bitmap
.palette
= 0;
196 image
->bitmap
.format
= SPICE_BITMAP_FMT_32BIT
;
198 dest
= pixman_image_create_bits(PIXMAN_x8r8g8b8
, bw
, bh
,
199 (void *)update
->bitmap
, bw
* 4);
200 pixman_image_composite(PIXMAN_OP_SRC
, ssd
->surface
, NULL
, ssd
->mirror
,
201 rect
->left
, rect
->top
, 0, 0,
202 rect
->left
, rect
->top
, bw
, bh
);
203 pixman_image_composite(PIXMAN_OP_SRC
, ssd
->mirror
, NULL
, dest
,
204 rect
->left
, rect
->top
, 0, 0,
206 pixman_image_unref(dest
);
208 cmd
->type
= QXL_CMD_DRAW
;
209 cmd
->data
= (uintptr_t)drawable
;
211 QTAILQ_INSERT_TAIL(&ssd
->updates
, update
, next
);
214 static void qemu_spice_create_update(SimpleSpiceDisplay
*ssd
)
216 static const int blksize
= 32;
217 int blocks
= (surface_width(ssd
->ds
) + blksize
- 1) / blksize
;
218 int dirty_top
[blocks
];
219 int y
, yoff
, x
, xoff
, blk
, bw
;
220 int bpp
= surface_bytes_per_pixel(ssd
->ds
);
221 uint8_t *guest
, *mirror
;
223 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
227 if (ssd
->surface
== NULL
) {
228 ssd
->surface
= pixman_image_ref(ssd
->ds
->image
);
229 ssd
->mirror
= qemu_pixman_mirror_create(ssd
->ds
->format
,
233 for (blk
= 0; blk
< blocks
; blk
++) {
237 guest
= surface_data(ssd
->ds
);
238 mirror
= (void *)pixman_image_get_data(ssd
->mirror
);
239 for (y
= ssd
->dirty
.top
; y
< ssd
->dirty
.bottom
; y
++) {
240 yoff
= y
* surface_stride(ssd
->ds
);
241 for (x
= ssd
->dirty
.left
; x
< ssd
->dirty
.right
; x
+= blksize
) {
244 bw
= MIN(blksize
, ssd
->dirty
.right
- x
);
245 if (memcmp(guest
+ yoff
+ xoff
,
246 mirror
+ yoff
+ xoff
,
248 if (dirty_top
[blk
] != -1) {
250 .top
= dirty_top
[blk
],
255 qemu_spice_create_one_update(ssd
, &update
);
259 if (dirty_top
[blk
] == -1) {
266 for (x
= ssd
->dirty
.left
; x
< ssd
->dirty
.right
; x
+= blksize
) {
268 bw
= MIN(blksize
, ssd
->dirty
.right
- x
);
269 if (dirty_top
[blk
] != -1) {
271 .top
= dirty_top
[blk
],
272 .bottom
= ssd
->dirty
.bottom
,
276 qemu_spice_create_one_update(ssd
, &update
);
281 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
285 * Called from spice server thread context (via interface_release_resource)
286 * We do *not* hold the global qemu mutex here, so extra care is needed
287 * when calling qemu functions. QEMU interfaces used:
288 * - g_free (underlying glibc free is re-entrant).
290 void qemu_spice_destroy_update(SimpleSpiceDisplay
*sdpy
, SimpleSpiceUpdate
*update
)
292 g_free(update
->bitmap
);
296 void qemu_spice_create_host_memslot(SimpleSpiceDisplay
*ssd
)
298 QXLDevMemSlot memslot
;
300 dprint(1, "%s/%d:\n", __func__
, ssd
->qxl
.id
);
302 memset(&memslot
, 0, sizeof(memslot
));
303 memslot
.slot_group_id
= MEMSLOT_GROUP_HOST
;
304 memslot
.virt_end
= ~0;
305 qemu_spice_add_memslot(ssd
, &memslot
, QXL_SYNC
);
308 void qemu_spice_create_host_primary(SimpleSpiceDisplay
*ssd
)
310 QXLDevSurfaceCreate surface
;
312 memset(&surface
, 0, sizeof(surface
));
314 dprint(1, "%s/%d: %dx%d\n", __func__
, ssd
->qxl
.id
,
315 surface_width(ssd
->ds
), surface_height(ssd
->ds
));
317 surface
.format
= SPICE_SURFACE_FMT_32_xRGB
;
318 surface
.width
= surface_width(ssd
->ds
);
319 surface
.height
= surface_height(ssd
->ds
);
320 surface
.stride
= -surface
.width
* 4;
321 surface
.mouse_mode
= true;
324 surface
.mem
= (uintptr_t)ssd
->buf
;
325 surface
.group_id
= MEMSLOT_GROUP_HOST
;
327 qemu_spice_create_primary_surface(ssd
, 0, &surface
, QXL_SYNC
);
330 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay
*ssd
)
332 dprint(1, "%s/%d:\n", __func__
, ssd
->qxl
.id
);
334 qemu_spice_destroy_primary_surface(ssd
, 0, QXL_SYNC
);
337 void qemu_spice_display_init_common(SimpleSpiceDisplay
*ssd
)
339 qemu_mutex_init(&ssd
->lock
);
340 QTAILQ_INIT(&ssd
->updates
);
343 if (ssd
->num_surfaces
== 0) {
344 ssd
->num_surfaces
= 1024;
346 ssd
->bufsize
= (16 * 1024 * 1024);
347 ssd
->buf
= g_malloc(ssd
->bufsize
);
350 /* display listener callbacks */
352 void qemu_spice_display_update(SimpleSpiceDisplay
*ssd
,
353 int x
, int y
, int w
, int h
)
357 dprint(2, "%s/%d: x %d y %d w %d h %d\n", __func__
,
358 ssd
->qxl
.id
, x
, y
, w
, h
);
359 update_area
.left
= x
,
360 update_area
.right
= x
+ w
;
362 update_area
.bottom
= y
+ h
;
364 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
367 qemu_spice_rect_union(&ssd
->dirty
, &update_area
);
370 void qemu_spice_display_switch(SimpleSpiceDisplay
*ssd
,
371 DisplaySurface
*surface
)
373 SimpleSpiceUpdate
*update
;
375 dprint(1, "%s/%d:\n", __func__
, ssd
->qxl
.id
);
377 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
379 pixman_image_unref(ssd
->surface
);
381 pixman_image_unref(ssd
->mirror
);
385 qemu_mutex_lock(&ssd
->lock
);
387 while ((update
= QTAILQ_FIRST(&ssd
->updates
)) != NULL
) {
388 QTAILQ_REMOVE(&ssd
->updates
, update
, next
);
389 qemu_spice_destroy_update(ssd
, update
);
391 qemu_mutex_unlock(&ssd
->lock
);
392 qemu_spice_destroy_host_primary(ssd
);
393 qemu_spice_create_host_primary(ssd
);
395 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
399 void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay
*ssd
)
402 assert(ssd
->dcl
.con
);
403 dpy_cursor_define(ssd
->dcl
.con
, ssd
->cursor
);
404 cursor_put(ssd
->cursor
);
407 if (ssd
->mouse_x
!= -1 && ssd
->mouse_y
!= -1) {
408 assert(ssd
->dcl
.con
);
409 dpy_mouse_set(ssd
->dcl
.con
, ssd
->mouse_x
, ssd
->mouse_y
, 1);
415 void qemu_spice_display_refresh(SimpleSpiceDisplay
*ssd
)
417 dprint(3, "%s/%d:\n", __func__
, ssd
->qxl
.id
);
418 graphic_hw_update(ssd
->dcl
.con
);
420 qemu_mutex_lock(&ssd
->lock
);
421 if (QTAILQ_EMPTY(&ssd
->updates
) && ssd
->ds
) {
422 qemu_spice_create_update(ssd
);
425 qemu_spice_cursor_refresh_unlocked(ssd
);
426 qemu_mutex_unlock(&ssd
->lock
);
430 qemu_spice_wakeup(ssd
);
431 dprint(2, "%s/%d: notify\n", __func__
, ssd
->qxl
.id
);
435 /* spice display interface callbacks */
437 static void interface_attach_worker(QXLInstance
*sin
, QXLWorker
*qxl_worker
)
439 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
441 dprint(1, "%s/%d:\n", __func__
, ssd
->qxl
.id
);
442 ssd
->worker
= qxl_worker
;
445 static void interface_set_compression_level(QXLInstance
*sin
, int level
)
447 dprint(1, "%s/%d:\n", __func__
, sin
->id
);
451 static void interface_set_mm_time(QXLInstance
*sin
, uint32_t mm_time
)
453 dprint(3, "%s/%d:\n", __func__
, sin
->id
);
457 static void interface_get_init_info(QXLInstance
*sin
, QXLDevInitInfo
*info
)
459 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
461 info
->memslot_gen_bits
= MEMSLOT_GENERATION_BITS
;
462 info
->memslot_id_bits
= MEMSLOT_SLOT_BITS
;
463 info
->num_memslots
= NUM_MEMSLOTS
;
464 info
->num_memslots_groups
= NUM_MEMSLOTS_GROUPS
;
465 info
->internal_groupslot_id
= 0;
466 info
->qxl_ram_size
= ssd
->bufsize
;
467 info
->n_surfaces
= ssd
->num_surfaces
;
470 static int interface_get_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
472 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
473 SimpleSpiceUpdate
*update
;
476 dprint(3, "%s/%d:\n", __func__
, ssd
->qxl
.id
);
478 qemu_mutex_lock(&ssd
->lock
);
479 update
= QTAILQ_FIRST(&ssd
->updates
);
480 if (update
!= NULL
) {
481 QTAILQ_REMOVE(&ssd
->updates
, update
, next
);
485 qemu_mutex_unlock(&ssd
->lock
);
490 static int interface_req_cmd_notification(QXLInstance
*sin
)
492 dprint(1, "%s/%d:\n", __func__
, sin
->id
);
496 static void interface_release_resource(QXLInstance
*sin
,
497 struct QXLReleaseInfoExt ext
)
499 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
502 dprint(2, "%s/%d:\n", __func__
, ssd
->qxl
.id
);
504 qemu_spice_destroy_update(ssd
, (void*)id
);
507 static int interface_get_cursor_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
509 dprint(3, "%s:\n", __FUNCTION__
);
513 static int interface_req_cursor_notification(QXLInstance
*sin
)
515 dprint(1, "%s:\n", __FUNCTION__
);
519 static void interface_notify_update(QXLInstance
*sin
, uint32_t update_id
)
521 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
525 static int interface_flush_resources(QXLInstance
*sin
)
527 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
532 static void interface_update_area_complete(QXLInstance
*sin
,
534 QXLRect
*dirty
, uint32_t num_updated_rects
)
536 /* should never be called, used in qxl native mode only */
537 fprintf(stderr
, "%s: abort()\n", __func__
);
541 /* called from spice server thread context only */
542 static void interface_async_complete(QXLInstance
*sin
, uint64_t cookie_token
)
544 /* should never be called, used in qxl native mode only */
545 fprintf(stderr
, "%s: abort()\n", __func__
);
549 static void interface_set_client_capabilities(QXLInstance
*sin
,
550 uint8_t client_present
,
553 dprint(3, "%s:\n", __func__
);
556 static int interface_client_monitors_config(QXLInstance
*sin
,
557 VDAgentMonitorsConfig
*monitors_config
)
559 dprint(3, "%s:\n", __func__
);
560 return 0; /* == not supported by guest */
563 static const QXLInterface dpy_interface
= {
564 .base
.type
= SPICE_INTERFACE_QXL
,
565 .base
.description
= "qemu simple display",
566 .base
.major_version
= SPICE_INTERFACE_QXL_MAJOR
,
567 .base
.minor_version
= SPICE_INTERFACE_QXL_MINOR
,
569 .attache_worker
= interface_attach_worker
,
570 .set_compression_level
= interface_set_compression_level
,
571 .set_mm_time
= interface_set_mm_time
,
572 .get_init_info
= interface_get_init_info
,
574 /* the callbacks below are called from spice server thread context */
575 .get_command
= interface_get_command
,
576 .req_cmd_notification
= interface_req_cmd_notification
,
577 .release_resource
= interface_release_resource
,
578 .get_cursor_command
= interface_get_cursor_command
,
579 .req_cursor_notification
= interface_req_cursor_notification
,
580 .notify_update
= interface_notify_update
,
581 .flush_resources
= interface_flush_resources
,
582 .async_complete
= interface_async_complete
,
583 .update_area_complete
= interface_update_area_complete
,
584 .set_client_capabilities
= interface_set_client_capabilities
,
585 .client_monitors_config
= interface_client_monitors_config
,
588 static void display_update(DisplayChangeListener
*dcl
,
589 int x
, int y
, int w
, int h
)
591 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
592 qemu_spice_display_update(ssd
, x
, y
, w
, h
);
595 static void display_switch(DisplayChangeListener
*dcl
,
596 struct DisplaySurface
*surface
)
598 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
599 qemu_spice_display_switch(ssd
, surface
);
602 static void display_refresh(DisplayChangeListener
*dcl
)
604 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
605 qemu_spice_display_refresh(ssd
);
608 static const DisplayChangeListenerOps display_listener_ops
= {
610 .dpy_gfx_update
= display_update
,
611 .dpy_gfx_switch
= display_switch
,
612 .dpy_refresh
= display_refresh
,
615 static void qemu_spice_display_init_one(QemuConsole
*con
)
617 SimpleSpiceDisplay
*ssd
= g_new0(SimpleSpiceDisplay
, 1);
619 qemu_spice_display_init_common(ssd
);
621 ssd
->qxl
.base
.sif
= &dpy_interface
.base
;
622 qemu_spice_add_display_interface(&ssd
->qxl
, con
);
625 qemu_spice_create_host_memslot(ssd
);
627 ssd
->dcl
.ops
= &display_listener_ops
;
629 register_displaychangelistener(&ssd
->dcl
);
631 qemu_spice_create_host_primary(ssd
);
634 void qemu_spice_display_init(void)
640 con
= qemu_console_lookup_by_index(i
);
641 if (!con
|| !qemu_console_is_graphic(con
)) {
644 if (qemu_spice_have_display_interface(con
)) {
647 qemu_spice_display_init_one(con
);