]> git.proxmox.com Git - qemu.git/blame - ui/spice-display.c
spice: make number of surfaces runtime-configurable.
[qemu.git] / ui / spice-display.c
CommitLineData
a3e22260
GH
1/*
2 * Copyright (C) 2010 Red Hat, Inc.
3 *
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.
8 *
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.
13 *
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/>.
16 */
17
a3e22260
GH
18#include "qemu-common.h"
19#include "qemu-spice.h"
20#include "qemu-timer.h"
21#include "qemu-queue.h"
22#include "monitor.h"
23#include "console.h"
24#include "sysemu.h"
c480bb7d 25#include "trace.h"
a3e22260
GH
26
27#include "spice-display.h"
28
29static int debug = 0;
30
2c80e423 31static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
a3e22260
GH
32{
33 va_list args;
34
35 if (level <= debug) {
36 va_start(args, fmt);
37 vfprintf(stderr, fmt, args);
38 va_end(args);
39 }
40}
41
42int qemu_spice_rect_is_empty(const QXLRect* r)
43{
44 return r->top == r->bottom || r->left == r->right;
45}
46
47void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
48{
49 if (qemu_spice_rect_is_empty(r)) {
50 return;
51 }
52
53 if (qemu_spice_rect_is_empty(dest)) {
54 *dest = *r;
55 return;
56 }
57
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);
62}
63
2e1a98c9
AL
64QXLCookie *qxl_cookie_new(int type, uint64_t io)
65{
66 QXLCookie *cookie;
67
68 cookie = g_malloc0(sizeof(*cookie));
69 cookie->type = type;
70 cookie->io = io;
71 return cookie;
72}
73
5ff4e36c
AL
74void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
75 qxl_async_io async)
5c59d118 76{
c480bb7d
AL
77 trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
78 memslot->virt_start, memslot->virt_end,
79 async);
80
5ff4e36c 81 if (async != QXL_SYNC) {
2e1a98c9 82 spice_qxl_add_memslot_async(&ssd->qxl, memslot,
34d14c6d
PM
83 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
84 QXL_IO_MEMSLOT_ADD_ASYNC));
5ff4e36c
AL
85 } else {
86 ssd->worker->add_memslot(ssd->worker, memslot);
87 }
5c59d118
GH
88}
89
90void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
91{
c480bb7d 92 trace_qemu_spice_del_memslot(ssd->qxl.id, gid, sid);
5c59d118
GH
93 ssd->worker->del_memslot(ssd->worker, gid, sid);
94}
95
96void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
5ff4e36c
AL
97 QXLDevSurfaceCreate *surface,
98 qxl_async_io async)
5c59d118 99{
c480bb7d 100 trace_qemu_spice_create_primary_surface(ssd->qxl.id, id, surface, async);
5ff4e36c 101 if (async != QXL_SYNC) {
2e1a98c9 102 spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface,
34d14c6d
PM
103 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
104 QXL_IO_CREATE_PRIMARY_ASYNC));
5ff4e36c
AL
105 } else {
106 ssd->worker->create_primary_surface(ssd->worker, id, surface);
107 }
5c59d118
GH
108}
109
5ff4e36c
AL
110void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
111 uint32_t id, qxl_async_io async)
5c59d118 112{
c480bb7d 113 trace_qemu_spice_destroy_primary_surface(ssd->qxl.id, id, async);
5ff4e36c 114 if (async != QXL_SYNC) {
2e1a98c9 115 spice_qxl_destroy_primary_surface_async(&ssd->qxl, id,
34d14c6d
PM
116 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
117 QXL_IO_DESTROY_PRIMARY_ASYNC));
5ff4e36c
AL
118 } else {
119 ssd->worker->destroy_primary_surface(ssd->worker, id);
120 }
5c59d118
GH
121}
122
5c59d118
GH
123void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
124{
c480bb7d 125 trace_qemu_spice_wakeup(ssd->qxl.id);
5c59d118
GH
126 ssd->worker->wakeup(ssd->worker);
127}
128
71d388d4
YH
129#if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
130static void qemu_spice_start(SimpleSpiceDisplay *ssd)
5c59d118 131{
c480bb7d 132 trace_qemu_spice_start(ssd->qxl.id);
5c59d118
GH
133 ssd->worker->start(ssd->worker);
134}
135
71d388d4 136static void qemu_spice_stop(SimpleSpiceDisplay *ssd)
5c59d118 137{
c480bb7d 138 trace_qemu_spice_stop(ssd->qxl.id);
5c59d118
GH
139 ssd->worker->stop(ssd->worker);
140}
141
71d388d4
YH
142#else
143
144static int spice_display_is_running;
145
146void qemu_spice_display_start(void)
147{
148 spice_display_is_running = true;
149}
150
151void qemu_spice_display_stop(void)
152{
153 spice_display_is_running = false;
154}
155
156#endif
157
158int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
159{
160#if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
161 return ssd->running;
162#else
163 return spice_display_is_running;
164#endif
165}
166
e0c64d08 167static SimpleSpiceUpdate *qemu_spice_create_update(SimpleSpiceDisplay *ssd)
a3e22260
GH
168{
169 SimpleSpiceUpdate *update;
170 QXLDrawable *drawable;
171 QXLImage *image;
172 QXLCommand *cmd;
173 uint8_t *src, *dst;
174 int by, bw, bh;
22795174 175 struct timespec time_space;
a3e22260
GH
176
177 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
178 return NULL;
179 };
180
c480bb7d 181 trace_qemu_spice_create_update(
a3e22260
GH
182 ssd->dirty.left, ssd->dirty.right,
183 ssd->dirty.top, ssd->dirty.bottom);
184
7267c094 185 update = g_malloc0(sizeof(*update));
a3e22260
GH
186 drawable = &update->drawable;
187 image = &update->image;
188 cmd = &update->ext.cmd;
189
190 bw = ssd->dirty.right - ssd->dirty.left;
191 bh = ssd->dirty.bottom - ssd->dirty.top;
7267c094 192 update->bitmap = g_malloc(bw * bh * 4);
a3e22260
GH
193
194 drawable->bbox = ssd->dirty;
195 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
196 drawable->effect = QXL_EFFECT_OPAQUE;
a13ccc99 197 drawable->release_info.id = (uintptr_t)update;
a3e22260
GH
198 drawable->type = QXL_DRAW_COPY;
199 drawable->surfaces_dest[0] = -1;
200 drawable->surfaces_dest[1] = -1;
201 drawable->surfaces_dest[2] = -1;
22795174
AL
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;
a3e22260
GH
206
207 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
a13ccc99 208 drawable->u.copy.src_bitmap = (uintptr_t)image;
a3e22260
GH
209 drawable->u.copy.src_area.right = bw;
210 drawable->u.copy.src_area.bottom = bh;
211
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;
a13ccc99 218 image->bitmap.data = (uintptr_t)(update->bitmap);
a3e22260
GH
219 image->bitmap.palette = 0;
220 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
221
222 if (ssd->conv == NULL) {
223 PixelFormat dst = qemu_default_pixelformat(32);
224 ssd->conv = qemu_pf_conv_get(&dst, &ssd->ds->surface->pf);
225 assert(ssd->conv);
226 }
227
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;
236 }
237
238 cmd->type = QXL_CMD_DRAW;
a13ccc99 239 cmd->data = (uintptr_t)drawable;
a3e22260
GH
240
241 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
a3e22260
GH
242 return update;
243}
244
245/*
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
5cbdb3a3 248 * when calling qemu functions. QEMU interfaces used:
7267c094 249 * - g_free (underlying glibc free is re-entrant).
a3e22260
GH
250 */
251void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
252{
7267c094
AL
253 g_free(update->bitmap);
254 g_free(update);
a3e22260
GH
255}
256
257void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
258{
259 QXLDevMemSlot memslot;
260
261 dprint(1, "%s:\n", __FUNCTION__);
262
263 memset(&memslot, 0, sizeof(memslot));
264 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
265 memslot.virt_end = ~0;
5ff4e36c 266 qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
a3e22260
GH
267}
268
269void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
270{
271 QXLDevSurfaceCreate surface;
272
160c31f7
AL
273 memset(&surface, 0, sizeof(surface));
274
a3e22260
GH
275 dprint(1, "%s: %dx%d\n", __FUNCTION__,
276 ds_get_width(ssd->ds), ds_get_height(ssd->ds));
277
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;
869564a9 282 surface.mouse_mode = true;
a3e22260
GH
283 surface.flags = 0;
284 surface.type = 0;
a13ccc99 285 surface.mem = (uintptr_t)ssd->buf;
a3e22260 286 surface.group_id = MEMSLOT_GROUP_HOST;
7466bc49 287
5ff4e36c 288 qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
a3e22260
GH
289}
290
291void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
292{
293 dprint(1, "%s:\n", __FUNCTION__);
294
5ff4e36c 295 qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
a3e22260
GH
296}
297
1dfb4dd9
LC
298void qemu_spice_vm_change_state_handler(void *opaque, int running,
299 RunState state)
a3e22260 300{
71d388d4 301#if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
a3e22260
GH
302 SimpleSpiceDisplay *ssd = opaque;
303
304 if (running) {
7e79cf40 305 ssd->running = true;
5c59d118 306 qemu_spice_start(ssd);
a3e22260 307 } else {
5c59d118 308 qemu_spice_stop(ssd);
7e79cf40 309 ssd->running = false;
a3e22260 310 }
71d388d4 311#endif
a3e22260
GH
312}
313
a963f876
GH
314void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd, DisplayState *ds)
315{
316 ssd->ds = ds;
317 qemu_mutex_init(&ssd->lock);
318 ssd->mouse_x = -1;
319 ssd->mouse_y = -1;
ddd8fdc7
GH
320 if (ssd->num_surfaces == 0) {
321 ssd->num_surfaces = 1024;
322 }
a963f876 323 ssd->bufsize = (16 * 1024 * 1024);
7267c094 324 ssd->buf = g_malloc(ssd->bufsize);
a963f876
GH
325}
326
a3e22260
GH
327/* display listener callbacks */
328
329void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
330 int x, int y, int w, int h)
331{
332 QXLRect update_area;
333
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;
337 update_area.top = y;
338 update_area.bottom = y + h;
339
a3e22260
GH
340 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
341 ssd->notify++;
342 }
343 qemu_spice_rect_union(&ssd->dirty, &update_area);
a3e22260
GH
344}
345
346void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
347{
348 dprint(1, "%s:\n", __FUNCTION__);
349
a3e22260
GH
350 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
351 qemu_pf_conv_put(ssd->conv);
352 ssd->conv = NULL;
a3e22260 353
e0c64d08
GH
354 qemu_mutex_lock(&ssd->lock);
355 if (ssd->update != NULL) {
356 qemu_spice_destroy_update(ssd, ssd->update);
357 ssd->update = NULL;
358 }
359 qemu_mutex_unlock(&ssd->lock);
a3e22260
GH
360 qemu_spice_destroy_host_primary(ssd);
361 qemu_spice_create_host_primary(ssd);
362
a3e22260
GH
363 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
364 ssd->notify++;
a3e22260
GH
365}
366
bb5a8cd5 367void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
a3e22260 368{
07536094
GH
369 if (ssd->cursor) {
370 ssd->ds->cursor_define(ssd->cursor);
371 cursor_put(ssd->cursor);
372 ssd->cursor = NULL;
373 }
374 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
375 ssd->ds->mouse_set(ssd->mouse_x, ssd->mouse_y, 1);
376 ssd->mouse_x = -1;
377 ssd->mouse_y = -1;
378 }
bb5a8cd5
AL
379}
380
381void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
382{
383 dprint(3, "%s:\n", __func__);
384 vga_hw_update();
385
386 qemu_mutex_lock(&ssd->lock);
387 if (ssd->update == NULL) {
388 ssd->update = qemu_spice_create_update(ssd);
389 ssd->notify++;
390 }
391 qemu_spice_cursor_refresh_unlocked(ssd);
e0c64d08
GH
392 qemu_mutex_unlock(&ssd->lock);
393
a3e22260
GH
394 if (ssd->notify) {
395 ssd->notify = 0;
5c59d118 396 qemu_spice_wakeup(ssd);
a3e22260
GH
397 dprint(2, "%s: notify\n", __FUNCTION__);
398 }
399}
400
401/* spice display interface callbacks */
402
403static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
404{
405 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
406
407 dprint(1, "%s:\n", __FUNCTION__);
408 ssd->worker = qxl_worker;
409}
410
411static void interface_set_compression_level(QXLInstance *sin, int level)
412{
413 dprint(1, "%s:\n", __FUNCTION__);
414 /* nothing to do */
415}
416
417static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
418{
419 dprint(3, "%s:\n", __FUNCTION__);
420 /* nothing to do */
421}
422
423static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
424{
425 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
426
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;
ddd8fdc7 433 info->n_surfaces = ssd->num_surfaces;
a3e22260
GH
434}
435
436static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
437{
438 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
439 SimpleSpiceUpdate *update;
e0c64d08 440 int ret = false;
a3e22260
GH
441
442 dprint(3, "%s:\n", __FUNCTION__);
e0c64d08
GH
443
444 qemu_mutex_lock(&ssd->lock);
445 if (ssd->update != NULL) {
446 update = ssd->update;
447 ssd->update = NULL;
448 *ext = update->ext;
449 ret = true;
a3e22260 450 }
e0c64d08
GH
451 qemu_mutex_unlock(&ssd->lock);
452
453 return ret;
a3e22260
GH
454}
455
456static int interface_req_cmd_notification(QXLInstance *sin)
457{
458 dprint(1, "%s:\n", __FUNCTION__);
459 return 1;
460}
461
462static void interface_release_resource(QXLInstance *sin,
463 struct QXLReleaseInfoExt ext)
464{
465 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
466 uintptr_t id;
467
468 dprint(2, "%s:\n", __FUNCTION__);
469 id = ext.info->id;
470 qemu_spice_destroy_update(ssd, (void*)id);
471}
472
473static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
474{
475 dprint(3, "%s:\n", __FUNCTION__);
476 return false;
477}
478
479static int interface_req_cursor_notification(QXLInstance *sin)
480{
481 dprint(1, "%s:\n", __FUNCTION__);
482 return 1;
483}
484
485static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
486{
487 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
488 abort();
489}
490
491static int interface_flush_resources(QXLInstance *sin)
492{
493 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
494 abort();
495 return 0;
496}
497
498static 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,
503
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,
508
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,
517};
518
519static SimpleSpiceDisplay sdpy;
520
521static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
522{
523 qemu_spice_display_update(&sdpy, x, y, w, h);
524}
525
526static void display_resize(struct DisplayState *ds)
527{
528 qemu_spice_display_resize(&sdpy);
529}
530
531static void display_refresh(struct DisplayState *ds)
532{
533 qemu_spice_display_refresh(&sdpy);
534}
535
536static DisplayChangeListener display_listener = {
537 .dpy_update = display_update,
538 .dpy_resize = display_resize,
539 .dpy_refresh = display_refresh,
540};
541
542void qemu_spice_display_init(DisplayState *ds)
543{
544 assert(sdpy.ds == NULL);
a963f876 545 qemu_spice_display_init_common(&sdpy, ds);
a3e22260
GH
546 register_displaychangelistener(ds, &display_listener);
547
548 sdpy.qxl.base.sif = &dpy_interface.base;
549 qemu_spice_add_interface(&sdpy.qxl.base);
550 assert(sdpy.worker);
551
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);
555}