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 "qemu-spice.h"
20 #include "qemu-timer.h"
21 #include "qemu-queue.h"
27 #include "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 ssd
->worker
->add_memslot(ssd
->worker
, 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 ssd
->worker
->del_memslot(ssd
->worker
, 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 ssd
->worker
->create_primary_surface(ssd
->worker
, 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 ssd
->worker
->destroy_primary_surface(ssd
->worker
, id
);
123 void qemu_spice_wakeup(SimpleSpiceDisplay
*ssd
)
125 trace_qemu_spice_wakeup(ssd
->qxl
.id
);
126 ssd
->worker
->wakeup(ssd
->worker
);
129 #if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
130 static void qemu_spice_start(SimpleSpiceDisplay
*ssd
)
132 trace_qemu_spice_start(ssd
->qxl
.id
);
133 ssd
->worker
->start(ssd
->worker
);
136 static void qemu_spice_stop(SimpleSpiceDisplay
*ssd
)
138 trace_qemu_spice_stop(ssd
->qxl
.id
);
139 ssd
->worker
->stop(ssd
->worker
);
144 static int spice_display_is_running
;
146 void qemu_spice_display_start(void)
148 spice_display_is_running
= true;
151 void qemu_spice_display_stop(void)
153 spice_display_is_running
= false;
158 int qemu_spice_display_is_running(SimpleSpiceDisplay
*ssd
)
160 #if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
163 return spice_display_is_running
;
167 static SimpleSpiceUpdate
*qemu_spice_create_update(SimpleSpiceDisplay
*ssd
)
169 SimpleSpiceUpdate
*update
;
170 QXLDrawable
*drawable
;
175 struct timespec time_space
;
177 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
181 trace_qemu_spice_create_update(
182 ssd
->dirty
.left
, ssd
->dirty
.right
,
183 ssd
->dirty
.top
, ssd
->dirty
.bottom
);
185 update
= g_malloc0(sizeof(*update
));
186 drawable
= &update
->drawable
;
187 image
= &update
->image
;
188 cmd
= &update
->ext
.cmd
;
190 bw
= ssd
->dirty
.right
- ssd
->dirty
.left
;
191 bh
= ssd
->dirty
.bottom
- ssd
->dirty
.top
;
192 update
->bitmap
= g_malloc(bw
* bh
* 4);
194 drawable
->bbox
= ssd
->dirty
;
195 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
196 drawable
->effect
= QXL_EFFECT_OPAQUE
;
197 drawable
->release_info
.id
= (uintptr_t)update
;
198 drawable
->type
= QXL_DRAW_COPY
;
199 drawable
->surfaces_dest
[0] = -1;
200 drawable
->surfaces_dest
[1] = -1;
201 drawable
->surfaces_dest
[2] = -1;
202 clock_gettime(CLOCK_MONOTONIC
, &time_space
);
203 /* time in milliseconds from epoch. */
204 drawable
->mm_time
= time_space
.tv_sec
* 1000
205 + time_space
.tv_nsec
/ 1000 / 1000;
207 drawable
->u
.copy
.rop_descriptor
= SPICE_ROPD_OP_PUT
;
208 drawable
->u
.copy
.src_bitmap
= (uintptr_t)image
;
209 drawable
->u
.copy
.src_area
.right
= bw
;
210 drawable
->u
.copy
.src_area
.bottom
= bh
;
212 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, ssd
->unique
++);
213 image
->descriptor
.type
= SPICE_IMAGE_TYPE_BITMAP
;
214 image
->bitmap
.flags
= QXL_BITMAP_DIRECT
| QXL_BITMAP_TOP_DOWN
;
215 image
->bitmap
.stride
= bw
* 4;
216 image
->descriptor
.width
= image
->bitmap
.x
= bw
;
217 image
->descriptor
.height
= image
->bitmap
.y
= bh
;
218 image
->bitmap
.data
= (uintptr_t)(update
->bitmap
);
219 image
->bitmap
.palette
= 0;
220 image
->bitmap
.format
= SPICE_BITMAP_FMT_32BIT
;
222 if (ssd
->conv
== NULL
) {
223 PixelFormat dst
= qemu_default_pixelformat(32);
224 ssd
->conv
= qemu_pf_conv_get(&dst
, &ssd
->ds
->surface
->pf
);
228 src
= ds_get_data(ssd
->ds
) +
229 ssd
->dirty
.top
* ds_get_linesize(ssd
->ds
) +
230 ssd
->dirty
.left
* ds_get_bytes_per_pixel(ssd
->ds
);
231 dst
= update
->bitmap
;
232 for (by
= 0; by
< bh
; by
++) {
233 qemu_pf_conv_run(ssd
->conv
, dst
, src
, bw
);
234 src
+= ds_get_linesize(ssd
->ds
);
235 dst
+= image
->bitmap
.stride
;
238 cmd
->type
= QXL_CMD_DRAW
;
239 cmd
->data
= (uintptr_t)drawable
;
241 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
246 * Called from spice server thread context (via interface_release_ressource)
247 * We do *not* hold the global qemu mutex here, so extra care is needed
248 * when calling qemu functions. QEMU interfaces used:
249 * - g_free (underlying glibc free is re-entrant).
251 void qemu_spice_destroy_update(SimpleSpiceDisplay
*sdpy
, SimpleSpiceUpdate
*update
)
253 g_free(update
->bitmap
);
257 void qemu_spice_create_host_memslot(SimpleSpiceDisplay
*ssd
)
259 QXLDevMemSlot memslot
;
261 dprint(1, "%s:\n", __FUNCTION__
);
263 memset(&memslot
, 0, sizeof(memslot
));
264 memslot
.slot_group_id
= MEMSLOT_GROUP_HOST
;
265 memslot
.virt_end
= ~0;
266 qemu_spice_add_memslot(ssd
, &memslot
, QXL_SYNC
);
269 void qemu_spice_create_host_primary(SimpleSpiceDisplay
*ssd
)
271 QXLDevSurfaceCreate surface
;
273 memset(&surface
, 0, sizeof(surface
));
275 dprint(1, "%s: %dx%d\n", __FUNCTION__
,
276 ds_get_width(ssd
->ds
), ds_get_height(ssd
->ds
));
278 surface
.format
= SPICE_SURFACE_FMT_32_xRGB
;
279 surface
.width
= ds_get_width(ssd
->ds
);
280 surface
.height
= ds_get_height(ssd
->ds
);
281 surface
.stride
= -surface
.width
* 4;
282 surface
.mouse_mode
= true;
285 surface
.mem
= (uintptr_t)ssd
->buf
;
286 surface
.group_id
= MEMSLOT_GROUP_HOST
;
288 qemu_spice_create_primary_surface(ssd
, 0, &surface
, QXL_SYNC
);
291 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay
*ssd
)
293 dprint(1, "%s:\n", __FUNCTION__
);
295 qemu_spice_destroy_primary_surface(ssd
, 0, QXL_SYNC
);
298 void qemu_spice_vm_change_state_handler(void *opaque
, int running
,
301 #if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
302 SimpleSpiceDisplay
*ssd
= opaque
;
306 qemu_spice_start(ssd
);
308 qemu_spice_stop(ssd
);
309 ssd
->running
= false;
314 void qemu_spice_display_init_common(SimpleSpiceDisplay
*ssd
, DisplayState
*ds
)
317 qemu_mutex_init(&ssd
->lock
);
320 if (ssd
->num_surfaces
== 0) {
321 ssd
->num_surfaces
= 1024;
323 ssd
->bufsize
= (16 * 1024 * 1024);
324 ssd
->buf
= g_malloc(ssd
->bufsize
);
327 /* display listener callbacks */
329 void qemu_spice_display_update(SimpleSpiceDisplay
*ssd
,
330 int x
, int y
, int w
, int h
)
334 dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__
, x
, y
, w
, h
);
335 update_area
.left
= x
,
336 update_area
.right
= x
+ w
;
338 update_area
.bottom
= y
+ h
;
340 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
343 qemu_spice_rect_union(&ssd
->dirty
, &update_area
);
346 void qemu_spice_display_resize(SimpleSpiceDisplay
*ssd
)
348 dprint(1, "%s:\n", __FUNCTION__
);
350 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
351 qemu_pf_conv_put(ssd
->conv
);
354 qemu_mutex_lock(&ssd
->lock
);
355 if (ssd
->update
!= NULL
) {
356 qemu_spice_destroy_update(ssd
, ssd
->update
);
359 qemu_mutex_unlock(&ssd
->lock
);
360 qemu_spice_destroy_host_primary(ssd
);
361 qemu_spice_create_host_primary(ssd
);
363 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
367 void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay
*ssd
)
370 ssd
->ds
->cursor_define(ssd
->cursor
);
371 cursor_put(ssd
->cursor
);
374 if (ssd
->mouse_x
!= -1 && ssd
->mouse_y
!= -1) {
375 ssd
->ds
->mouse_set(ssd
->mouse_x
, ssd
->mouse_y
, 1);
381 void qemu_spice_display_refresh(SimpleSpiceDisplay
*ssd
)
383 dprint(3, "%s:\n", __func__
);
386 qemu_mutex_lock(&ssd
->lock
);
387 if (ssd
->update
== NULL
) {
388 ssd
->update
= qemu_spice_create_update(ssd
);
391 qemu_spice_cursor_refresh_unlocked(ssd
);
392 qemu_mutex_unlock(&ssd
->lock
);
396 qemu_spice_wakeup(ssd
);
397 dprint(2, "%s: notify\n", __FUNCTION__
);
401 /* spice display interface callbacks */
403 static void interface_attach_worker(QXLInstance
*sin
, QXLWorker
*qxl_worker
)
405 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
407 dprint(1, "%s:\n", __FUNCTION__
);
408 ssd
->worker
= qxl_worker
;
411 static void interface_set_compression_level(QXLInstance
*sin
, int level
)
413 dprint(1, "%s:\n", __FUNCTION__
);
417 static void interface_set_mm_time(QXLInstance
*sin
, uint32_t mm_time
)
419 dprint(3, "%s:\n", __FUNCTION__
);
423 static void interface_get_init_info(QXLInstance
*sin
, QXLDevInitInfo
*info
)
425 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
427 info
->memslot_gen_bits
= MEMSLOT_GENERATION_BITS
;
428 info
->memslot_id_bits
= MEMSLOT_SLOT_BITS
;
429 info
->num_memslots
= NUM_MEMSLOTS
;
430 info
->num_memslots_groups
= NUM_MEMSLOTS_GROUPS
;
431 info
->internal_groupslot_id
= 0;
432 info
->qxl_ram_size
= ssd
->bufsize
;
433 info
->n_surfaces
= ssd
->num_surfaces
;
436 static int interface_get_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
438 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
439 SimpleSpiceUpdate
*update
;
442 dprint(3, "%s:\n", __FUNCTION__
);
444 qemu_mutex_lock(&ssd
->lock
);
445 if (ssd
->update
!= NULL
) {
446 update
= ssd
->update
;
451 qemu_mutex_unlock(&ssd
->lock
);
456 static int interface_req_cmd_notification(QXLInstance
*sin
)
458 dprint(1, "%s:\n", __FUNCTION__
);
462 static void interface_release_resource(QXLInstance
*sin
,
463 struct QXLReleaseInfoExt ext
)
465 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
468 dprint(2, "%s:\n", __FUNCTION__
);
470 qemu_spice_destroy_update(ssd
, (void*)id
);
473 static int interface_get_cursor_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
475 dprint(3, "%s:\n", __FUNCTION__
);
479 static int interface_req_cursor_notification(QXLInstance
*sin
)
481 dprint(1, "%s:\n", __FUNCTION__
);
485 static void interface_notify_update(QXLInstance
*sin
, uint32_t update_id
)
487 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
491 static int interface_flush_resources(QXLInstance
*sin
)
493 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
498 static const QXLInterface dpy_interface
= {
499 .base
.type
= SPICE_INTERFACE_QXL
,
500 .base
.description
= "qemu simple display",
501 .base
.major_version
= SPICE_INTERFACE_QXL_MAJOR
,
502 .base
.minor_version
= SPICE_INTERFACE_QXL_MINOR
,
504 .attache_worker
= interface_attach_worker
,
505 .set_compression_level
= interface_set_compression_level
,
506 .set_mm_time
= interface_set_mm_time
,
507 .get_init_info
= interface_get_init_info
,
509 /* the callbacks below are called from spice server thread context */
510 .get_command
= interface_get_command
,
511 .req_cmd_notification
= interface_req_cmd_notification
,
512 .release_resource
= interface_release_resource
,
513 .get_cursor_command
= interface_get_cursor_command
,
514 .req_cursor_notification
= interface_req_cursor_notification
,
515 .notify_update
= interface_notify_update
,
516 .flush_resources
= interface_flush_resources
,
519 static SimpleSpiceDisplay sdpy
;
521 static void display_update(struct DisplayState
*ds
, int x
, int y
, int w
, int h
)
523 qemu_spice_display_update(&sdpy
, x
, y
, w
, h
);
526 static void display_resize(struct DisplayState
*ds
)
528 qemu_spice_display_resize(&sdpy
);
531 static void display_refresh(struct DisplayState
*ds
)
533 qemu_spice_display_refresh(&sdpy
);
536 static DisplayChangeListener display_listener
= {
537 .dpy_update
= display_update
,
538 .dpy_resize
= display_resize
,
539 .dpy_refresh
= display_refresh
,
542 void qemu_spice_display_init(DisplayState
*ds
)
544 assert(sdpy
.ds
== NULL
);
545 qemu_spice_display_init_common(&sdpy
, ds
);
546 register_displaychangelistener(ds
, &display_listener
);
548 sdpy
.qxl
.base
.sif
= &dpy_interface
.base
;
549 qemu_spice_add_interface(&sdpy
.qxl
.base
);
552 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler
, &sdpy
);
553 qemu_spice_create_host_memslot(&sdpy
);
554 qemu_spice_create_host_primary(&sdpy
);