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 void qemu_spice_start(SimpleSpiceDisplay
*ssd
)
131 trace_qemu_spice_start(ssd
->qxl
.id
);
132 ssd
->worker
->start(ssd
->worker
);
135 void qemu_spice_stop(SimpleSpiceDisplay
*ssd
)
137 trace_qemu_spice_stop(ssd
->qxl
.id
);
138 ssd
->worker
->stop(ssd
->worker
);
141 static SimpleSpiceUpdate
*qemu_spice_create_update(SimpleSpiceDisplay
*ssd
)
143 SimpleSpiceUpdate
*update
;
144 QXLDrawable
*drawable
;
149 struct timespec time_space
;
151 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
155 trace_qemu_spice_create_update(
156 ssd
->dirty
.left
, ssd
->dirty
.right
,
157 ssd
->dirty
.top
, ssd
->dirty
.bottom
);
159 update
= g_malloc0(sizeof(*update
));
160 drawable
= &update
->drawable
;
161 image
= &update
->image
;
162 cmd
= &update
->ext
.cmd
;
164 bw
= ssd
->dirty
.right
- ssd
->dirty
.left
;
165 bh
= ssd
->dirty
.bottom
- ssd
->dirty
.top
;
166 update
->bitmap
= g_malloc(bw
* bh
* 4);
168 drawable
->bbox
= ssd
->dirty
;
169 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
170 drawable
->effect
= QXL_EFFECT_OPAQUE
;
171 drawable
->release_info
.id
= (uintptr_t)update
;
172 drawable
->type
= QXL_DRAW_COPY
;
173 drawable
->surfaces_dest
[0] = -1;
174 drawable
->surfaces_dest
[1] = -1;
175 drawable
->surfaces_dest
[2] = -1;
176 clock_gettime(CLOCK_MONOTONIC
, &time_space
);
177 /* time in milliseconds from epoch. */
178 drawable
->mm_time
= time_space
.tv_sec
* 1000
179 + time_space
.tv_nsec
/ 1000 / 1000;
181 drawable
->u
.copy
.rop_descriptor
= SPICE_ROPD_OP_PUT
;
182 drawable
->u
.copy
.src_bitmap
= (uintptr_t)image
;
183 drawable
->u
.copy
.src_area
.right
= bw
;
184 drawable
->u
.copy
.src_area
.bottom
= bh
;
186 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, ssd
->unique
++);
187 image
->descriptor
.type
= SPICE_IMAGE_TYPE_BITMAP
;
188 image
->bitmap
.flags
= QXL_BITMAP_DIRECT
| QXL_BITMAP_TOP_DOWN
;
189 image
->bitmap
.stride
= bw
* 4;
190 image
->descriptor
.width
= image
->bitmap
.x
= bw
;
191 image
->descriptor
.height
= image
->bitmap
.y
= bh
;
192 image
->bitmap
.data
= (uintptr_t)(update
->bitmap
);
193 image
->bitmap
.palette
= 0;
194 image
->bitmap
.format
= SPICE_BITMAP_FMT_32BIT
;
196 if (ssd
->conv
== NULL
) {
197 PixelFormat dst
= qemu_default_pixelformat(32);
198 ssd
->conv
= qemu_pf_conv_get(&dst
, &ssd
->ds
->surface
->pf
);
202 src
= ds_get_data(ssd
->ds
) +
203 ssd
->dirty
.top
* ds_get_linesize(ssd
->ds
) +
204 ssd
->dirty
.left
* ds_get_bytes_per_pixel(ssd
->ds
);
205 dst
= update
->bitmap
;
206 for (by
= 0; by
< bh
; by
++) {
207 qemu_pf_conv_run(ssd
->conv
, dst
, src
, bw
);
208 src
+= ds_get_linesize(ssd
->ds
);
209 dst
+= image
->bitmap
.stride
;
212 cmd
->type
= QXL_CMD_DRAW
;
213 cmd
->data
= (uintptr_t)drawable
;
215 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
220 * Called from spice server thread context (via interface_release_ressource)
221 * We do *not* hold the global qemu mutex here, so extra care is needed
222 * when calling qemu functions. QEMU interfaces used:
223 * - g_free (underlying glibc free is re-entrant).
225 void qemu_spice_destroy_update(SimpleSpiceDisplay
*sdpy
, SimpleSpiceUpdate
*update
)
227 g_free(update
->bitmap
);
231 void qemu_spice_create_host_memslot(SimpleSpiceDisplay
*ssd
)
233 QXLDevMemSlot memslot
;
235 dprint(1, "%s:\n", __FUNCTION__
);
237 memset(&memslot
, 0, sizeof(memslot
));
238 memslot
.slot_group_id
= MEMSLOT_GROUP_HOST
;
239 memslot
.virt_end
= ~0;
240 qemu_spice_add_memslot(ssd
, &memslot
, QXL_SYNC
);
243 void qemu_spice_create_host_primary(SimpleSpiceDisplay
*ssd
)
245 QXLDevSurfaceCreate surface
;
247 memset(&surface
, 0, sizeof(surface
));
249 dprint(1, "%s: %dx%d\n", __FUNCTION__
,
250 ds_get_width(ssd
->ds
), ds_get_height(ssd
->ds
));
252 surface
.format
= SPICE_SURFACE_FMT_32_xRGB
;
253 surface
.width
= ds_get_width(ssd
->ds
);
254 surface
.height
= ds_get_height(ssd
->ds
);
255 surface
.stride
= -surface
.width
* 4;
256 surface
.mouse_mode
= true;
259 surface
.mem
= (uintptr_t)ssd
->buf
;
260 surface
.group_id
= MEMSLOT_GROUP_HOST
;
262 qemu_spice_create_primary_surface(ssd
, 0, &surface
, QXL_SYNC
);
265 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay
*ssd
)
267 dprint(1, "%s:\n", __FUNCTION__
);
269 qemu_spice_destroy_primary_surface(ssd
, 0, QXL_SYNC
);
272 void qemu_spice_vm_change_state_handler(void *opaque
, int running
,
275 SimpleSpiceDisplay
*ssd
= opaque
;
279 qemu_spice_start(ssd
);
281 qemu_spice_stop(ssd
);
282 ssd
->running
= false;
286 void qemu_spice_display_init_common(SimpleSpiceDisplay
*ssd
, DisplayState
*ds
)
289 qemu_mutex_init(&ssd
->lock
);
292 ssd
->bufsize
= (16 * 1024 * 1024);
293 ssd
->buf
= g_malloc(ssd
->bufsize
);
296 /* display listener callbacks */
298 void qemu_spice_display_update(SimpleSpiceDisplay
*ssd
,
299 int x
, int y
, int w
, int h
)
303 dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__
, x
, y
, w
, h
);
304 update_area
.left
= x
,
305 update_area
.right
= x
+ w
;
307 update_area
.bottom
= y
+ h
;
309 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
312 qemu_spice_rect_union(&ssd
->dirty
, &update_area
);
315 void qemu_spice_display_resize(SimpleSpiceDisplay
*ssd
)
317 dprint(1, "%s:\n", __FUNCTION__
);
319 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
320 qemu_pf_conv_put(ssd
->conv
);
323 qemu_mutex_lock(&ssd
->lock
);
324 if (ssd
->update
!= NULL
) {
325 qemu_spice_destroy_update(ssd
, ssd
->update
);
328 qemu_mutex_unlock(&ssd
->lock
);
329 qemu_spice_destroy_host_primary(ssd
);
330 qemu_spice_create_host_primary(ssd
);
332 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
336 void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay
*ssd
)
339 ssd
->ds
->cursor_define(ssd
->cursor
);
340 cursor_put(ssd
->cursor
);
343 if (ssd
->mouse_x
!= -1 && ssd
->mouse_y
!= -1) {
344 ssd
->ds
->mouse_set(ssd
->mouse_x
, ssd
->mouse_y
, 1);
350 void qemu_spice_display_refresh(SimpleSpiceDisplay
*ssd
)
352 dprint(3, "%s:\n", __func__
);
355 qemu_mutex_lock(&ssd
->lock
);
356 if (ssd
->update
== NULL
) {
357 ssd
->update
= qemu_spice_create_update(ssd
);
360 qemu_spice_cursor_refresh_unlocked(ssd
);
361 qemu_mutex_unlock(&ssd
->lock
);
365 qemu_spice_wakeup(ssd
);
366 dprint(2, "%s: notify\n", __FUNCTION__
);
370 /* spice display interface callbacks */
372 static void interface_attach_worker(QXLInstance
*sin
, QXLWorker
*qxl_worker
)
374 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
376 dprint(1, "%s:\n", __FUNCTION__
);
377 ssd
->worker
= qxl_worker
;
380 static void interface_set_compression_level(QXLInstance
*sin
, int level
)
382 dprint(1, "%s:\n", __FUNCTION__
);
386 static void interface_set_mm_time(QXLInstance
*sin
, uint32_t mm_time
)
388 dprint(3, "%s:\n", __FUNCTION__
);
392 static void interface_get_init_info(QXLInstance
*sin
, QXLDevInitInfo
*info
)
394 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
396 info
->memslot_gen_bits
= MEMSLOT_GENERATION_BITS
;
397 info
->memslot_id_bits
= MEMSLOT_SLOT_BITS
;
398 info
->num_memslots
= NUM_MEMSLOTS
;
399 info
->num_memslots_groups
= NUM_MEMSLOTS_GROUPS
;
400 info
->internal_groupslot_id
= 0;
401 info
->qxl_ram_size
= ssd
->bufsize
;
402 info
->n_surfaces
= NUM_SURFACES
;
405 static int interface_get_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
407 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
408 SimpleSpiceUpdate
*update
;
411 dprint(3, "%s:\n", __FUNCTION__
);
413 qemu_mutex_lock(&ssd
->lock
);
414 if (ssd
->update
!= NULL
) {
415 update
= ssd
->update
;
420 qemu_mutex_unlock(&ssd
->lock
);
425 static int interface_req_cmd_notification(QXLInstance
*sin
)
427 dprint(1, "%s:\n", __FUNCTION__
);
431 static void interface_release_resource(QXLInstance
*sin
,
432 struct QXLReleaseInfoExt ext
)
434 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
437 dprint(2, "%s:\n", __FUNCTION__
);
439 qemu_spice_destroy_update(ssd
, (void*)id
);
442 static int interface_get_cursor_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
444 dprint(3, "%s:\n", __FUNCTION__
);
448 static int interface_req_cursor_notification(QXLInstance
*sin
)
450 dprint(1, "%s:\n", __FUNCTION__
);
454 static void interface_notify_update(QXLInstance
*sin
, uint32_t update_id
)
456 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
460 static int interface_flush_resources(QXLInstance
*sin
)
462 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
467 static const QXLInterface dpy_interface
= {
468 .base
.type
= SPICE_INTERFACE_QXL
,
469 .base
.description
= "qemu simple display",
470 .base
.major_version
= SPICE_INTERFACE_QXL_MAJOR
,
471 .base
.minor_version
= SPICE_INTERFACE_QXL_MINOR
,
473 .attache_worker
= interface_attach_worker
,
474 .set_compression_level
= interface_set_compression_level
,
475 .set_mm_time
= interface_set_mm_time
,
476 .get_init_info
= interface_get_init_info
,
478 /* the callbacks below are called from spice server thread context */
479 .get_command
= interface_get_command
,
480 .req_cmd_notification
= interface_req_cmd_notification
,
481 .release_resource
= interface_release_resource
,
482 .get_cursor_command
= interface_get_cursor_command
,
483 .req_cursor_notification
= interface_req_cursor_notification
,
484 .notify_update
= interface_notify_update
,
485 .flush_resources
= interface_flush_resources
,
488 static SimpleSpiceDisplay sdpy
;
490 static void display_update(struct DisplayState
*ds
, int x
, int y
, int w
, int h
)
492 qemu_spice_display_update(&sdpy
, x
, y
, w
, h
);
495 static void display_resize(struct DisplayState
*ds
)
497 qemu_spice_display_resize(&sdpy
);
500 static void display_refresh(struct DisplayState
*ds
)
502 qemu_spice_display_refresh(&sdpy
);
505 static DisplayChangeListener display_listener
= {
506 .dpy_update
= display_update
,
507 .dpy_resize
= display_resize
,
508 .dpy_refresh
= display_refresh
,
511 void qemu_spice_display_init(DisplayState
*ds
)
513 assert(sdpy
.ds
== NULL
);
514 qemu_spice_display_init_common(&sdpy
, ds
);
515 register_displaychangelistener(ds
, &display_listener
);
517 sdpy
.qxl
.base
.sif
= &dpy_interface
.base
;
518 qemu_spice_add_interface(&sdpy
.qxl
.base
);
521 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler
, &sdpy
);
522 qemu_spice_create_host_memslot(&sdpy
);
523 qemu_spice_create_host_primary(&sdpy
);