]> git.proxmox.com Git - mirror_qemu.git/blame - ui/spice-display.c
spice: split qemu_spice_create_update
[mirror_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
c60319a3
GH
167static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
168 QXLRect *rect)
a3e22260
GH
169{
170 SimpleSpiceUpdate *update;
171 QXLDrawable *drawable;
172 QXLImage *image;
173 QXLCommand *cmd;
174 uint8_t *src, *dst;
175 int by, bw, bh;
22795174 176 struct timespec time_space;
a3e22260 177
c480bb7d 178 trace_qemu_spice_create_update(
c60319a3
GH
179 rect->left, rect->right,
180 rect->top, rect->bottom);
a3e22260 181
7267c094 182 update = g_malloc0(sizeof(*update));
a3e22260
GH
183 drawable = &update->drawable;
184 image = &update->image;
185 cmd = &update->ext.cmd;
186
c60319a3
GH
187 bw = rect->right - rect->left;
188 bh = rect->bottom - rect->top;
7267c094 189 update->bitmap = g_malloc(bw * bh * 4);
a3e22260 190
c60319a3 191 drawable->bbox = *rect;
a3e22260
GH
192 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
193 drawable->effect = QXL_EFFECT_OPAQUE;
a13ccc99 194 drawable->release_info.id = (uintptr_t)update;
a3e22260
GH
195 drawable->type = QXL_DRAW_COPY;
196 drawable->surfaces_dest[0] = -1;
197 drawable->surfaces_dest[1] = -1;
198 drawable->surfaces_dest[2] = -1;
22795174
AL
199 clock_gettime(CLOCK_MONOTONIC, &time_space);
200 /* time in milliseconds from epoch. */
201 drawable->mm_time = time_space.tv_sec * 1000
202 + time_space.tv_nsec / 1000 / 1000;
a3e22260
GH
203
204 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
a13ccc99 205 drawable->u.copy.src_bitmap = (uintptr_t)image;
a3e22260
GH
206 drawable->u.copy.src_area.right = bw;
207 drawable->u.copy.src_area.bottom = bh;
208
209 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
210 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
211 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
212 image->bitmap.stride = bw * 4;
213 image->descriptor.width = image->bitmap.x = bw;
214 image->descriptor.height = image->bitmap.y = bh;
a13ccc99 215 image->bitmap.data = (uintptr_t)(update->bitmap);
a3e22260
GH
216 image->bitmap.palette = 0;
217 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
218
219 if (ssd->conv == NULL) {
220 PixelFormat dst = qemu_default_pixelformat(32);
221 ssd->conv = qemu_pf_conv_get(&dst, &ssd->ds->surface->pf);
222 assert(ssd->conv);
223 }
224
225 src = ds_get_data(ssd->ds) +
c60319a3
GH
226 rect->top * ds_get_linesize(ssd->ds) +
227 rect->left * ds_get_bytes_per_pixel(ssd->ds);
a3e22260
GH
228 dst = update->bitmap;
229 for (by = 0; by < bh; by++) {
230 qemu_pf_conv_run(ssd->conv, dst, src, bw);
231 src += ds_get_linesize(ssd->ds);
232 dst += image->bitmap.stride;
233 }
234
235 cmd->type = QXL_CMD_DRAW;
a13ccc99 236 cmd->data = (uintptr_t)drawable;
a3e22260 237
b1af98ba 238 QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
a3e22260
GH
239}
240
c60319a3
GH
241static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
242{
243 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
244 return;
245 };
246 qemu_spice_create_one_update(ssd, &ssd->dirty);
247 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
248}
249
a3e22260
GH
250/*
251 * Called from spice server thread context (via interface_release_ressource)
252 * We do *not* hold the global qemu mutex here, so extra care is needed
5cbdb3a3 253 * when calling qemu functions. QEMU interfaces used:
7267c094 254 * - g_free (underlying glibc free is re-entrant).
a3e22260
GH
255 */
256void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
257{
7267c094
AL
258 g_free(update->bitmap);
259 g_free(update);
a3e22260
GH
260}
261
262void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
263{
264 QXLDevMemSlot memslot;
265
266 dprint(1, "%s:\n", __FUNCTION__);
267
268 memset(&memslot, 0, sizeof(memslot));
269 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
270 memslot.virt_end = ~0;
5ff4e36c 271 qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
a3e22260
GH
272}
273
274void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
275{
276 QXLDevSurfaceCreate surface;
277
160c31f7
AL
278 memset(&surface, 0, sizeof(surface));
279
a3e22260
GH
280 dprint(1, "%s: %dx%d\n", __FUNCTION__,
281 ds_get_width(ssd->ds), ds_get_height(ssd->ds));
282
283 surface.format = SPICE_SURFACE_FMT_32_xRGB;
284 surface.width = ds_get_width(ssd->ds);
285 surface.height = ds_get_height(ssd->ds);
286 surface.stride = -surface.width * 4;
869564a9 287 surface.mouse_mode = true;
a3e22260
GH
288 surface.flags = 0;
289 surface.type = 0;
a13ccc99 290 surface.mem = (uintptr_t)ssd->buf;
a3e22260 291 surface.group_id = MEMSLOT_GROUP_HOST;
7466bc49 292
5ff4e36c 293 qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
a3e22260
GH
294}
295
296void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
297{
298 dprint(1, "%s:\n", __FUNCTION__);
299
5ff4e36c 300 qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
a3e22260
GH
301}
302
1dfb4dd9
LC
303void qemu_spice_vm_change_state_handler(void *opaque, int running,
304 RunState state)
a3e22260 305{
71d388d4 306#if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
a3e22260
GH
307 SimpleSpiceDisplay *ssd = opaque;
308
309 if (running) {
7e79cf40 310 ssd->running = true;
5c59d118 311 qemu_spice_start(ssd);
a3e22260 312 } else {
5c59d118 313 qemu_spice_stop(ssd);
7e79cf40 314 ssd->running = false;
a3e22260 315 }
71d388d4 316#endif
a3e22260
GH
317}
318
a963f876
GH
319void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd, DisplayState *ds)
320{
321 ssd->ds = ds;
322 qemu_mutex_init(&ssd->lock);
b1af98ba 323 QTAILQ_INIT(&ssd->updates);
a963f876
GH
324 ssd->mouse_x = -1;
325 ssd->mouse_y = -1;
ddd8fdc7
GH
326 if (ssd->num_surfaces == 0) {
327 ssd->num_surfaces = 1024;
328 }
a963f876 329 ssd->bufsize = (16 * 1024 * 1024);
7267c094 330 ssd->buf = g_malloc(ssd->bufsize);
a963f876
GH
331}
332
a3e22260
GH
333/* display listener callbacks */
334
335void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
336 int x, int y, int w, int h)
337{
338 QXLRect update_area;
339
340 dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__, x, y, w, h);
341 update_area.left = x,
342 update_area.right = x + w;
343 update_area.top = y;
344 update_area.bottom = y + h;
345
a3e22260
GH
346 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
347 ssd->notify++;
348 }
349 qemu_spice_rect_union(&ssd->dirty, &update_area);
a3e22260
GH
350}
351
352void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
353{
b1af98ba
GH
354 SimpleSpiceUpdate *update;
355
a3e22260
GH
356 dprint(1, "%s:\n", __FUNCTION__);
357
a3e22260
GH
358 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
359 qemu_pf_conv_put(ssd->conv);
360 ssd->conv = NULL;
a3e22260 361
e0c64d08 362 qemu_mutex_lock(&ssd->lock);
b1af98ba
GH
363 while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
364 QTAILQ_REMOVE(&ssd->updates, update, next);
365 qemu_spice_destroy_update(ssd, update);
e0c64d08
GH
366 }
367 qemu_mutex_unlock(&ssd->lock);
a3e22260
GH
368 qemu_spice_destroy_host_primary(ssd);
369 qemu_spice_create_host_primary(ssd);
370
a3e22260
GH
371 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
372 ssd->notify++;
a3e22260
GH
373}
374
bb5a8cd5 375void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
a3e22260 376{
07536094
GH
377 if (ssd->cursor) {
378 ssd->ds->cursor_define(ssd->cursor);
379 cursor_put(ssd->cursor);
380 ssd->cursor = NULL;
381 }
382 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
383 ssd->ds->mouse_set(ssd->mouse_x, ssd->mouse_y, 1);
384 ssd->mouse_x = -1;
385 ssd->mouse_y = -1;
386 }
bb5a8cd5
AL
387}
388
389void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
390{
391 dprint(3, "%s:\n", __func__);
392 vga_hw_update();
393
394 qemu_mutex_lock(&ssd->lock);
b1af98ba
GH
395 if (QTAILQ_EMPTY(&ssd->updates)) {
396 qemu_spice_create_update(ssd);
bb5a8cd5
AL
397 ssd->notify++;
398 }
399 qemu_spice_cursor_refresh_unlocked(ssd);
e0c64d08
GH
400 qemu_mutex_unlock(&ssd->lock);
401
a3e22260
GH
402 if (ssd->notify) {
403 ssd->notify = 0;
5c59d118 404 qemu_spice_wakeup(ssd);
a3e22260
GH
405 dprint(2, "%s: notify\n", __FUNCTION__);
406 }
407}
408
409/* spice display interface callbacks */
410
411static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
412{
413 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
414
415 dprint(1, "%s:\n", __FUNCTION__);
416 ssd->worker = qxl_worker;
417}
418
419static void interface_set_compression_level(QXLInstance *sin, int level)
420{
421 dprint(1, "%s:\n", __FUNCTION__);
422 /* nothing to do */
423}
424
425static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
426{
427 dprint(3, "%s:\n", __FUNCTION__);
428 /* nothing to do */
429}
430
431static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
432{
433 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
434
435 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
436 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
437 info->num_memslots = NUM_MEMSLOTS;
438 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
439 info->internal_groupslot_id = 0;
440 info->qxl_ram_size = ssd->bufsize;
ddd8fdc7 441 info->n_surfaces = ssd->num_surfaces;
a3e22260
GH
442}
443
444static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
445{
446 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
447 SimpleSpiceUpdate *update;
e0c64d08 448 int ret = false;
a3e22260
GH
449
450 dprint(3, "%s:\n", __FUNCTION__);
e0c64d08
GH
451
452 qemu_mutex_lock(&ssd->lock);
b1af98ba
GH
453 update = QTAILQ_FIRST(&ssd->updates);
454 if (update != NULL) {
455 QTAILQ_REMOVE(&ssd->updates, update, next);
e0c64d08
GH
456 *ext = update->ext;
457 ret = true;
a3e22260 458 }
e0c64d08
GH
459 qemu_mutex_unlock(&ssd->lock);
460
461 return ret;
a3e22260
GH
462}
463
464static int interface_req_cmd_notification(QXLInstance *sin)
465{
466 dprint(1, "%s:\n", __FUNCTION__);
467 return 1;
468}
469
470static void interface_release_resource(QXLInstance *sin,
471 struct QXLReleaseInfoExt ext)
472{
473 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
474 uintptr_t id;
475
476 dprint(2, "%s:\n", __FUNCTION__);
477 id = ext.info->id;
478 qemu_spice_destroy_update(ssd, (void*)id);
479}
480
481static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
482{
483 dprint(3, "%s:\n", __FUNCTION__);
484 return false;
485}
486
487static int interface_req_cursor_notification(QXLInstance *sin)
488{
489 dprint(1, "%s:\n", __FUNCTION__);
490 return 1;
491}
492
493static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
494{
495 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
496 abort();
497}
498
499static int interface_flush_resources(QXLInstance *sin)
500{
501 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
502 abort();
503 return 0;
504}
505
506static const QXLInterface dpy_interface = {
507 .base.type = SPICE_INTERFACE_QXL,
508 .base.description = "qemu simple display",
509 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
510 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
511
512 .attache_worker = interface_attach_worker,
513 .set_compression_level = interface_set_compression_level,
514 .set_mm_time = interface_set_mm_time,
515 .get_init_info = interface_get_init_info,
516
517 /* the callbacks below are called from spice server thread context */
518 .get_command = interface_get_command,
519 .req_cmd_notification = interface_req_cmd_notification,
520 .release_resource = interface_release_resource,
521 .get_cursor_command = interface_get_cursor_command,
522 .req_cursor_notification = interface_req_cursor_notification,
523 .notify_update = interface_notify_update,
524 .flush_resources = interface_flush_resources,
525};
526
527static SimpleSpiceDisplay sdpy;
528
529static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
530{
531 qemu_spice_display_update(&sdpy, x, y, w, h);
532}
533
534static void display_resize(struct DisplayState *ds)
535{
536 qemu_spice_display_resize(&sdpy);
537}
538
539static void display_refresh(struct DisplayState *ds)
540{
541 qemu_spice_display_refresh(&sdpy);
542}
543
544static DisplayChangeListener display_listener = {
545 .dpy_update = display_update,
546 .dpy_resize = display_resize,
547 .dpy_refresh = display_refresh,
548};
549
550void qemu_spice_display_init(DisplayState *ds)
551{
552 assert(sdpy.ds == NULL);
a963f876 553 qemu_spice_display_init_common(&sdpy, ds);
a3e22260
GH
554 register_displaychangelistener(ds, &display_listener);
555
556 sdpy.qxl.base.sif = &dpy_interface.base;
557 qemu_spice_add_interface(&sdpy.qxl.base);
558 assert(sdpy.worker);
559
560 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler, &sdpy);
561 qemu_spice_create_host_memslot(&sdpy);
562 qemu_spice_create_host_primary(&sdpy);
563}