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"
26 #include "spice-display.h"
30 static void GCC_FMT_ATTR(2, 3) dprint(int level
, const char *fmt
, ...)
36 vfprintf(stderr
, fmt
, args
);
41 int qemu_spice_rect_is_empty(const QXLRect
* r
)
43 return r
->top
== r
->bottom
|| r
->left
== r
->right
;
46 void qemu_spice_rect_union(QXLRect
*dest
, const QXLRect
*r
)
48 if (qemu_spice_rect_is_empty(r
)) {
52 if (qemu_spice_rect_is_empty(dest
)) {
57 dest
->top
= MIN(dest
->top
, r
->top
);
58 dest
->left
= MIN(dest
->left
, r
->left
);
59 dest
->bottom
= MAX(dest
->bottom
, r
->bottom
);
60 dest
->right
= MAX(dest
->right
, r
->right
);
63 QXLCookie
*qxl_cookie_new(int type
, uint64_t io
)
67 cookie
= g_malloc0(sizeof(*cookie
));
73 void qemu_spice_add_memslot(SimpleSpiceDisplay
*ssd
, QXLDevMemSlot
*memslot
,
76 if (async
!= QXL_SYNC
) {
77 spice_qxl_add_memslot_async(&ssd
->qxl
, memslot
,
78 (uint64_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
79 QXL_IO_MEMSLOT_ADD_ASYNC
));
81 ssd
->worker
->add_memslot(ssd
->worker
, memslot
);
85 void qemu_spice_del_memslot(SimpleSpiceDisplay
*ssd
, uint32_t gid
, uint32_t sid
)
87 ssd
->worker
->del_memslot(ssd
->worker
, gid
, sid
);
90 void qemu_spice_create_primary_surface(SimpleSpiceDisplay
*ssd
, uint32_t id
,
91 QXLDevSurfaceCreate
*surface
,
94 if (async
!= QXL_SYNC
) {
95 spice_qxl_create_primary_surface_async(&ssd
->qxl
, id
, surface
,
96 (uint64_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
97 QXL_IO_CREATE_PRIMARY_ASYNC
));
99 ssd
->worker
->create_primary_surface(ssd
->worker
, id
, surface
);
104 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay
*ssd
,
105 uint32_t id
, qxl_async_io async
)
107 if (async
!= QXL_SYNC
) {
108 spice_qxl_destroy_primary_surface_async(&ssd
->qxl
, id
,
109 (uint64_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
110 QXL_IO_DESTROY_PRIMARY_ASYNC
));
112 ssd
->worker
->destroy_primary_surface(ssd
->worker
, id
);
116 void qemu_spice_wakeup(SimpleSpiceDisplay
*ssd
)
118 ssd
->worker
->wakeup(ssd
->worker
);
121 void qemu_spice_start(SimpleSpiceDisplay
*ssd
)
123 ssd
->worker
->start(ssd
->worker
);
126 void qemu_spice_stop(SimpleSpiceDisplay
*ssd
)
128 ssd
->worker
->stop(ssd
->worker
);
131 static SimpleSpiceUpdate
*qemu_spice_create_update(SimpleSpiceDisplay
*ssd
)
133 SimpleSpiceUpdate
*update
;
134 QXLDrawable
*drawable
;
139 struct timespec time_space
;
141 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
145 dprint(2, "%s: lr %d -> %d, tb -> %d -> %d\n", __FUNCTION__
,
146 ssd
->dirty
.left
, ssd
->dirty
.right
,
147 ssd
->dirty
.top
, ssd
->dirty
.bottom
);
149 update
= g_malloc0(sizeof(*update
));
150 drawable
= &update
->drawable
;
151 image
= &update
->image
;
152 cmd
= &update
->ext
.cmd
;
154 bw
= ssd
->dirty
.right
- ssd
->dirty
.left
;
155 bh
= ssd
->dirty
.bottom
- ssd
->dirty
.top
;
156 update
->bitmap
= g_malloc(bw
* bh
* 4);
158 drawable
->bbox
= ssd
->dirty
;
159 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
160 drawable
->effect
= QXL_EFFECT_OPAQUE
;
161 drawable
->release_info
.id
= (intptr_t)update
;
162 drawable
->type
= QXL_DRAW_COPY
;
163 drawable
->surfaces_dest
[0] = -1;
164 drawable
->surfaces_dest
[1] = -1;
165 drawable
->surfaces_dest
[2] = -1;
166 clock_gettime(CLOCK_MONOTONIC
, &time_space
);
167 /* time in milliseconds from epoch. */
168 drawable
->mm_time
= time_space
.tv_sec
* 1000
169 + time_space
.tv_nsec
/ 1000 / 1000;
171 drawable
->u
.copy
.rop_descriptor
= SPICE_ROPD_OP_PUT
;
172 drawable
->u
.copy
.src_bitmap
= (intptr_t)image
;
173 drawable
->u
.copy
.src_area
.right
= bw
;
174 drawable
->u
.copy
.src_area
.bottom
= bh
;
176 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, ssd
->unique
++);
177 image
->descriptor
.type
= SPICE_IMAGE_TYPE_BITMAP
;
178 image
->bitmap
.flags
= QXL_BITMAP_DIRECT
| QXL_BITMAP_TOP_DOWN
;
179 image
->bitmap
.stride
= bw
* 4;
180 image
->descriptor
.width
= image
->bitmap
.x
= bw
;
181 image
->descriptor
.height
= image
->bitmap
.y
= bh
;
182 image
->bitmap
.data
= (intptr_t)(update
->bitmap
);
183 image
->bitmap
.palette
= 0;
184 image
->bitmap
.format
= SPICE_BITMAP_FMT_32BIT
;
186 if (ssd
->conv
== NULL
) {
187 PixelFormat dst
= qemu_default_pixelformat(32);
188 ssd
->conv
= qemu_pf_conv_get(&dst
, &ssd
->ds
->surface
->pf
);
192 src
= ds_get_data(ssd
->ds
) +
193 ssd
->dirty
.top
* ds_get_linesize(ssd
->ds
) +
194 ssd
->dirty
.left
* ds_get_bytes_per_pixel(ssd
->ds
);
195 dst
= update
->bitmap
;
196 for (by
= 0; by
< bh
; by
++) {
197 qemu_pf_conv_run(ssd
->conv
, dst
, src
, bw
);
198 src
+= ds_get_linesize(ssd
->ds
);
199 dst
+= image
->bitmap
.stride
;
202 cmd
->type
= QXL_CMD_DRAW
;
203 cmd
->data
= (intptr_t)drawable
;
205 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
210 * Called from spice server thread context (via interface_release_ressource)
211 * We do *not* hold the global qemu mutex here, so extra care is needed
212 * when calling qemu functions. Qemu interfaces used:
213 * - g_free (underlying glibc free is re-entrant).
215 void qemu_spice_destroy_update(SimpleSpiceDisplay
*sdpy
, SimpleSpiceUpdate
*update
)
217 g_free(update
->bitmap
);
221 void qemu_spice_create_host_memslot(SimpleSpiceDisplay
*ssd
)
223 QXLDevMemSlot memslot
;
225 dprint(1, "%s:\n", __FUNCTION__
);
227 memset(&memslot
, 0, sizeof(memslot
));
228 memslot
.slot_group_id
= MEMSLOT_GROUP_HOST
;
229 memslot
.virt_end
= ~0;
230 qemu_spice_add_memslot(ssd
, &memslot
, QXL_SYNC
);
233 void qemu_spice_create_host_primary(SimpleSpiceDisplay
*ssd
)
235 QXLDevSurfaceCreate surface
;
237 dprint(1, "%s: %dx%d\n", __FUNCTION__
,
238 ds_get_width(ssd
->ds
), ds_get_height(ssd
->ds
));
240 surface
.format
= SPICE_SURFACE_FMT_32_xRGB
;
241 surface
.width
= ds_get_width(ssd
->ds
);
242 surface
.height
= ds_get_height(ssd
->ds
);
243 surface
.stride
= -surface
.width
* 4;
244 surface
.mouse_mode
= true;
247 surface
.mem
= (intptr_t)ssd
->buf
;
248 surface
.group_id
= MEMSLOT_GROUP_HOST
;
250 qemu_spice_create_primary_surface(ssd
, 0, &surface
, QXL_SYNC
);
253 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay
*ssd
)
255 dprint(1, "%s:\n", __FUNCTION__
);
257 qemu_spice_destroy_primary_surface(ssd
, 0, QXL_SYNC
);
260 void qemu_spice_vm_change_state_handler(void *opaque
, int running
,
263 SimpleSpiceDisplay
*ssd
= opaque
;
267 qemu_spice_start(ssd
);
269 qemu_spice_stop(ssd
);
270 ssd
->running
= false;
274 void qemu_spice_display_init_common(SimpleSpiceDisplay
*ssd
, DisplayState
*ds
)
277 qemu_mutex_init(&ssd
->lock
);
280 ssd
->bufsize
= (16 * 1024 * 1024);
281 ssd
->buf
= g_malloc(ssd
->bufsize
);
284 /* display listener callbacks */
286 void qemu_spice_display_update(SimpleSpiceDisplay
*ssd
,
287 int x
, int y
, int w
, int h
)
291 dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__
, x
, y
, w
, h
);
292 update_area
.left
= x
,
293 update_area
.right
= x
+ w
;
295 update_area
.bottom
= y
+ h
;
297 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
300 qemu_spice_rect_union(&ssd
->dirty
, &update_area
);
303 void qemu_spice_display_resize(SimpleSpiceDisplay
*ssd
)
305 dprint(1, "%s:\n", __FUNCTION__
);
307 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
308 qemu_pf_conv_put(ssd
->conv
);
311 qemu_mutex_lock(&ssd
->lock
);
312 if (ssd
->update
!= NULL
) {
313 qemu_spice_destroy_update(ssd
, ssd
->update
);
316 qemu_mutex_unlock(&ssd
->lock
);
317 qemu_spice_destroy_host_primary(ssd
);
318 qemu_spice_create_host_primary(ssd
);
320 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
324 void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay
*ssd
)
327 ssd
->ds
->cursor_define(ssd
->cursor
);
328 cursor_put(ssd
->cursor
);
331 if (ssd
->mouse_x
!= -1 && ssd
->mouse_y
!= -1) {
332 ssd
->ds
->mouse_set(ssd
->mouse_x
, ssd
->mouse_y
, 1);
338 void qemu_spice_display_refresh(SimpleSpiceDisplay
*ssd
)
340 dprint(3, "%s:\n", __func__
);
343 qemu_mutex_lock(&ssd
->lock
);
344 if (ssd
->update
== NULL
) {
345 ssd
->update
= qemu_spice_create_update(ssd
);
348 qemu_spice_cursor_refresh_unlocked(ssd
);
349 qemu_mutex_unlock(&ssd
->lock
);
353 qemu_spice_wakeup(ssd
);
354 dprint(2, "%s: notify\n", __FUNCTION__
);
358 /* spice display interface callbacks */
360 static void interface_attach_worker(QXLInstance
*sin
, QXLWorker
*qxl_worker
)
362 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
364 dprint(1, "%s:\n", __FUNCTION__
);
365 ssd
->worker
= qxl_worker
;
368 static void interface_set_compression_level(QXLInstance
*sin
, int level
)
370 dprint(1, "%s:\n", __FUNCTION__
);
374 static void interface_set_mm_time(QXLInstance
*sin
, uint32_t mm_time
)
376 dprint(3, "%s:\n", __FUNCTION__
);
380 static void interface_get_init_info(QXLInstance
*sin
, QXLDevInitInfo
*info
)
382 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
384 info
->memslot_gen_bits
= MEMSLOT_GENERATION_BITS
;
385 info
->memslot_id_bits
= MEMSLOT_SLOT_BITS
;
386 info
->num_memslots
= NUM_MEMSLOTS
;
387 info
->num_memslots_groups
= NUM_MEMSLOTS_GROUPS
;
388 info
->internal_groupslot_id
= 0;
389 info
->qxl_ram_size
= ssd
->bufsize
;
390 info
->n_surfaces
= NUM_SURFACES
;
393 static int interface_get_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
395 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
396 SimpleSpiceUpdate
*update
;
399 dprint(3, "%s:\n", __FUNCTION__
);
401 qemu_mutex_lock(&ssd
->lock
);
402 if (ssd
->update
!= NULL
) {
403 update
= ssd
->update
;
408 qemu_mutex_unlock(&ssd
->lock
);
413 static int interface_req_cmd_notification(QXLInstance
*sin
)
415 dprint(1, "%s:\n", __FUNCTION__
);
419 static void interface_release_resource(QXLInstance
*sin
,
420 struct QXLReleaseInfoExt ext
)
422 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
425 dprint(2, "%s:\n", __FUNCTION__
);
427 qemu_spice_destroy_update(ssd
, (void*)id
);
430 static int interface_get_cursor_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
432 dprint(3, "%s:\n", __FUNCTION__
);
436 static int interface_req_cursor_notification(QXLInstance
*sin
)
438 dprint(1, "%s:\n", __FUNCTION__
);
442 static void interface_notify_update(QXLInstance
*sin
, uint32_t update_id
)
444 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
448 static int interface_flush_resources(QXLInstance
*sin
)
450 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
455 static const QXLInterface dpy_interface
= {
456 .base
.type
= SPICE_INTERFACE_QXL
,
457 .base
.description
= "qemu simple display",
458 .base
.major_version
= SPICE_INTERFACE_QXL_MAJOR
,
459 .base
.minor_version
= SPICE_INTERFACE_QXL_MINOR
,
461 .attache_worker
= interface_attach_worker
,
462 .set_compression_level
= interface_set_compression_level
,
463 .set_mm_time
= interface_set_mm_time
,
464 .get_init_info
= interface_get_init_info
,
466 /* the callbacks below are called from spice server thread context */
467 .get_command
= interface_get_command
,
468 .req_cmd_notification
= interface_req_cmd_notification
,
469 .release_resource
= interface_release_resource
,
470 .get_cursor_command
= interface_get_cursor_command
,
471 .req_cursor_notification
= interface_req_cursor_notification
,
472 .notify_update
= interface_notify_update
,
473 .flush_resources
= interface_flush_resources
,
476 static SimpleSpiceDisplay sdpy
;
478 static void display_update(struct DisplayState
*ds
, int x
, int y
, int w
, int h
)
480 qemu_spice_display_update(&sdpy
, x
, y
, w
, h
);
483 static void display_resize(struct DisplayState
*ds
)
485 qemu_spice_display_resize(&sdpy
);
488 static void display_refresh(struct DisplayState
*ds
)
490 qemu_spice_display_refresh(&sdpy
);
493 static DisplayChangeListener display_listener
= {
494 .dpy_update
= display_update
,
495 .dpy_resize
= display_resize
,
496 .dpy_refresh
= display_refresh
,
499 void qemu_spice_display_init(DisplayState
*ds
)
501 assert(sdpy
.ds
== NULL
);
502 qemu_spice_display_init_common(&sdpy
, ds
);
503 register_displaychangelistener(ds
, &display_listener
);
505 sdpy
.qxl
.base
.sif
= &dpy_interface
.base
;
506 qemu_spice_add_interface(&sdpy
.qxl
.base
);
509 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler
, &sdpy
);
510 qemu_spice_create_host_memslot(&sdpy
);
511 qemu_spice_create_host_primary(&sdpy
);