]> git.proxmox.com Git - qemu.git/blob - ui/spice-display.c
misc: move include files to include/qemu/
[qemu.git] / ui / spice-display.c
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
18 #include "qemu-common.h"
19 #include "ui/qemu-spice.h"
20 #include "qemu/timer.h"
21 #include "qemu/queue.h"
22 #include "monitor/monitor.h"
23 #include "ui/console.h"
24 #include "sysemu.h"
25 #include "trace.h"
26
27 #include "ui/spice-display.h"
28
29 static int debug = 0;
30
31 static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
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
42 int qemu_spice_rect_is_empty(const QXLRect* r)
43 {
44 return r->top == r->bottom || r->left == r->right;
45 }
46
47 void 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
64 QXLCookie *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
74 void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
75 qxl_async_io async)
76 {
77 trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
78 memslot->virt_start, memslot->virt_end,
79 async);
80
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));
85 } else {
86 ssd->worker->add_memslot(ssd->worker, memslot);
87 }
88 }
89
90 void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
91 {
92 trace_qemu_spice_del_memslot(ssd->qxl.id, gid, sid);
93 ssd->worker->del_memslot(ssd->worker, gid, sid);
94 }
95
96 void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
97 QXLDevSurfaceCreate *surface,
98 qxl_async_io async)
99 {
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));
105 } else {
106 ssd->worker->create_primary_surface(ssd->worker, id, surface);
107 }
108 }
109
110 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
111 uint32_t id, qxl_async_io async)
112 {
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));
118 } else {
119 ssd->worker->destroy_primary_surface(ssd->worker, id);
120 }
121 }
122
123 void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
124 {
125 trace_qemu_spice_wakeup(ssd->qxl.id);
126 ssd->worker->wakeup(ssd->worker);
127 }
128
129 static int spice_display_is_running;
130
131 void qemu_spice_display_start(void)
132 {
133 spice_display_is_running = true;
134 }
135
136 void qemu_spice_display_stop(void)
137 {
138 spice_display_is_running = false;
139 }
140
141 int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
142 {
143 return spice_display_is_running;
144 }
145
146 static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
147 QXLRect *rect)
148 {
149 SimpleSpiceUpdate *update;
150 QXLDrawable *drawable;
151 QXLImage *image;
152 QXLCommand *cmd;
153 int bw, bh;
154 struct timespec time_space;
155 pixman_image_t *dest;
156
157 trace_qemu_spice_create_update(
158 rect->left, rect->right,
159 rect->top, rect->bottom);
160
161 update = g_malloc0(sizeof(*update));
162 drawable = &update->drawable;
163 image = &update->image;
164 cmd = &update->ext.cmd;
165
166 bw = rect->right - rect->left;
167 bh = rect->bottom - rect->top;
168 update->bitmap = g_malloc(bw * bh * 4);
169
170 drawable->bbox = *rect;
171 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
172 drawable->effect = QXL_EFFECT_OPAQUE;
173 drawable->release_info.id = (uintptr_t)update;
174 drawable->type = QXL_DRAW_COPY;
175 drawable->surfaces_dest[0] = -1;
176 drawable->surfaces_dest[1] = -1;
177 drawable->surfaces_dest[2] = -1;
178 clock_gettime(CLOCK_MONOTONIC, &time_space);
179 /* time in milliseconds from epoch. */
180 drawable->mm_time = time_space.tv_sec * 1000
181 + time_space.tv_nsec / 1000 / 1000;
182
183 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
184 drawable->u.copy.src_bitmap = (uintptr_t)image;
185 drawable->u.copy.src_area.right = bw;
186 drawable->u.copy.src_area.bottom = bh;
187
188 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
189 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
190 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
191 image->bitmap.stride = bw * 4;
192 image->descriptor.width = image->bitmap.x = bw;
193 image->descriptor.height = image->bitmap.y = bh;
194 image->bitmap.data = (uintptr_t)(update->bitmap);
195 image->bitmap.palette = 0;
196 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
197
198 dest = pixman_image_create_bits(PIXMAN_x8r8g8b8, bw, bh,
199 (void *)update->bitmap, bw * 4);
200 pixman_image_composite(PIXMAN_OP_SRC, ssd->surface, NULL, ssd->mirror,
201 rect->left, rect->top, 0, 0,
202 rect->left, rect->top, bw, bh);
203 pixman_image_composite(PIXMAN_OP_SRC, ssd->mirror, NULL, dest,
204 rect->left, rect->top, 0, 0,
205 0, 0, bw, bh);
206 pixman_image_unref(dest);
207
208 cmd->type = QXL_CMD_DRAW;
209 cmd->data = (uintptr_t)drawable;
210
211 QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
212 }
213
214 static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
215 {
216 static const int blksize = 32;
217 int blocks = (ds_get_width(ssd->ds) + blksize - 1) / blksize;
218 int dirty_top[blocks];
219 int y, yoff, x, xoff, blk, bw;
220 int bpp = ds_get_bytes_per_pixel(ssd->ds);
221 uint8_t *guest, *mirror;
222
223 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
224 return;
225 };
226
227 if (ssd->surface == NULL) {
228 ssd->surface = pixman_image_ref(ds_get_image(ssd->ds));
229 ssd->mirror = qemu_pixman_mirror_create(ds_get_format(ssd->ds),
230 ds_get_image(ssd->ds));
231 }
232
233 for (blk = 0; blk < blocks; blk++) {
234 dirty_top[blk] = -1;
235 }
236
237 guest = ds_get_data(ssd->ds);
238 mirror = (void *)pixman_image_get_data(ssd->mirror);
239 for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
240 yoff = y * ds_get_linesize(ssd->ds);
241 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
242 xoff = x * bpp;
243 blk = x / blksize;
244 bw = MIN(blksize, ssd->dirty.right - x);
245 if (memcmp(guest + yoff + xoff,
246 mirror + yoff + xoff,
247 bw * bpp) == 0) {
248 if (dirty_top[blk] != -1) {
249 QXLRect update = {
250 .top = dirty_top[blk],
251 .bottom = y,
252 .left = x,
253 .right = x + bw,
254 };
255 qemu_spice_create_one_update(ssd, &update);
256 dirty_top[blk] = -1;
257 }
258 } else {
259 if (dirty_top[blk] == -1) {
260 dirty_top[blk] = y;
261 }
262 }
263 }
264 }
265
266 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
267 blk = x / blksize;
268 bw = MIN(blksize, ssd->dirty.right - x);
269 if (dirty_top[blk] != -1) {
270 QXLRect update = {
271 .top = dirty_top[blk],
272 .bottom = ssd->dirty.bottom,
273 .left = x,
274 .right = x + bw,
275 };
276 qemu_spice_create_one_update(ssd, &update);
277 dirty_top[blk] = -1;
278 }
279 }
280
281 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
282 }
283
284 /*
285 * Called from spice server thread context (via interface_release_resource)
286 * We do *not* hold the global qemu mutex here, so extra care is needed
287 * when calling qemu functions. QEMU interfaces used:
288 * - g_free (underlying glibc free is re-entrant).
289 */
290 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
291 {
292 g_free(update->bitmap);
293 g_free(update);
294 }
295
296 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
297 {
298 QXLDevMemSlot memslot;
299
300 dprint(1, "%s:\n", __FUNCTION__);
301
302 memset(&memslot, 0, sizeof(memslot));
303 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
304 memslot.virt_end = ~0;
305 qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
306 }
307
308 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
309 {
310 QXLDevSurfaceCreate surface;
311
312 memset(&surface, 0, sizeof(surface));
313
314 dprint(1, "%s: %dx%d\n", __FUNCTION__,
315 ds_get_width(ssd->ds), ds_get_height(ssd->ds));
316
317 surface.format = SPICE_SURFACE_FMT_32_xRGB;
318 surface.width = ds_get_width(ssd->ds);
319 surface.height = ds_get_height(ssd->ds);
320 surface.stride = -surface.width * 4;
321 surface.mouse_mode = true;
322 surface.flags = 0;
323 surface.type = 0;
324 surface.mem = (uintptr_t)ssd->buf;
325 surface.group_id = MEMSLOT_GROUP_HOST;
326
327 qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
328 }
329
330 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
331 {
332 dprint(1, "%s:\n", __FUNCTION__);
333
334 qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
335 }
336
337 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd, DisplayState *ds)
338 {
339 ssd->ds = ds;
340 qemu_mutex_init(&ssd->lock);
341 QTAILQ_INIT(&ssd->updates);
342 ssd->mouse_x = -1;
343 ssd->mouse_y = -1;
344 if (ssd->num_surfaces == 0) {
345 ssd->num_surfaces = 1024;
346 }
347 ssd->bufsize = (16 * 1024 * 1024);
348 ssd->buf = g_malloc(ssd->bufsize);
349 }
350
351 /* display listener callbacks */
352
353 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
354 int x, int y, int w, int h)
355 {
356 QXLRect update_area;
357
358 dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__, x, y, w, h);
359 update_area.left = x,
360 update_area.right = x + w;
361 update_area.top = y;
362 update_area.bottom = y + h;
363
364 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
365 ssd->notify++;
366 }
367 qemu_spice_rect_union(&ssd->dirty, &update_area);
368 }
369
370 void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
371 {
372 SimpleSpiceUpdate *update;
373
374 dprint(1, "%s:\n", __FUNCTION__);
375
376 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
377 if (ssd->surface) {
378 pixman_image_unref(ssd->surface);
379 ssd->surface = NULL;
380 pixman_image_unref(ssd->mirror);
381 ssd->mirror = NULL;
382 }
383
384 qemu_mutex_lock(&ssd->lock);
385 while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
386 QTAILQ_REMOVE(&ssd->updates, update, next);
387 qemu_spice_destroy_update(ssd, update);
388 }
389 qemu_mutex_unlock(&ssd->lock);
390 qemu_spice_destroy_host_primary(ssd);
391 qemu_spice_create_host_primary(ssd);
392
393 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
394 ssd->notify++;
395 }
396
397 void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
398 {
399 if (ssd->cursor) {
400 dpy_cursor_define(ssd->ds, ssd->cursor);
401 cursor_put(ssd->cursor);
402 ssd->cursor = NULL;
403 }
404 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
405 dpy_mouse_set(ssd->ds, ssd->mouse_x, ssd->mouse_y, 1);
406 ssd->mouse_x = -1;
407 ssd->mouse_y = -1;
408 }
409 }
410
411 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
412 {
413 dprint(3, "%s:\n", __func__);
414 vga_hw_update();
415
416 qemu_mutex_lock(&ssd->lock);
417 if (QTAILQ_EMPTY(&ssd->updates)) {
418 qemu_spice_create_update(ssd);
419 ssd->notify++;
420 }
421 qemu_spice_cursor_refresh_unlocked(ssd);
422 qemu_mutex_unlock(&ssd->lock);
423
424 if (ssd->notify) {
425 ssd->notify = 0;
426 qemu_spice_wakeup(ssd);
427 dprint(2, "%s: notify\n", __FUNCTION__);
428 }
429 }
430
431 /* spice display interface callbacks */
432
433 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
434 {
435 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
436
437 dprint(1, "%s:\n", __FUNCTION__);
438 ssd->worker = qxl_worker;
439 }
440
441 static void interface_set_compression_level(QXLInstance *sin, int level)
442 {
443 dprint(1, "%s:\n", __FUNCTION__);
444 /* nothing to do */
445 }
446
447 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
448 {
449 dprint(3, "%s:\n", __FUNCTION__);
450 /* nothing to do */
451 }
452
453 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
454 {
455 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
456
457 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
458 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
459 info->num_memslots = NUM_MEMSLOTS;
460 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
461 info->internal_groupslot_id = 0;
462 info->qxl_ram_size = ssd->bufsize;
463 info->n_surfaces = ssd->num_surfaces;
464 }
465
466 static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
467 {
468 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
469 SimpleSpiceUpdate *update;
470 int ret = false;
471
472 dprint(3, "%s:\n", __FUNCTION__);
473
474 qemu_mutex_lock(&ssd->lock);
475 update = QTAILQ_FIRST(&ssd->updates);
476 if (update != NULL) {
477 QTAILQ_REMOVE(&ssd->updates, update, next);
478 *ext = update->ext;
479 ret = true;
480 }
481 qemu_mutex_unlock(&ssd->lock);
482
483 return ret;
484 }
485
486 static int interface_req_cmd_notification(QXLInstance *sin)
487 {
488 dprint(1, "%s:\n", __FUNCTION__);
489 return 1;
490 }
491
492 static void interface_release_resource(QXLInstance *sin,
493 struct QXLReleaseInfoExt ext)
494 {
495 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
496 uintptr_t id;
497
498 dprint(2, "%s:\n", __FUNCTION__);
499 id = ext.info->id;
500 qemu_spice_destroy_update(ssd, (void*)id);
501 }
502
503 static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
504 {
505 dprint(3, "%s:\n", __FUNCTION__);
506 return false;
507 }
508
509 static int interface_req_cursor_notification(QXLInstance *sin)
510 {
511 dprint(1, "%s:\n", __FUNCTION__);
512 return 1;
513 }
514
515 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
516 {
517 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
518 abort();
519 }
520
521 static int interface_flush_resources(QXLInstance *sin)
522 {
523 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
524 abort();
525 return 0;
526 }
527
528 static void interface_update_area_complete(QXLInstance *sin,
529 uint32_t surface_id,
530 QXLRect *dirty, uint32_t num_updated_rects)
531 {
532 /* should never be called, used in qxl native mode only */
533 fprintf(stderr, "%s: abort()\n", __func__);
534 abort();
535 }
536
537 /* called from spice server thread context only */
538 static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
539 {
540 /* should never be called, used in qxl native mode only */
541 fprintf(stderr, "%s: abort()\n", __func__);
542 abort();
543 }
544
545 static void interface_set_client_capabilities(QXLInstance *sin,
546 uint8_t client_present,
547 uint8_t caps[58])
548 {
549 dprint(3, "%s:\n", __func__);
550 }
551
552 static int interface_client_monitors_config(QXLInstance *sin,
553 VDAgentMonitorsConfig *monitors_config)
554 {
555 dprint(3, "%s:\n", __func__);
556 return 0; /* == not supported by guest */
557 }
558
559 static const QXLInterface dpy_interface = {
560 .base.type = SPICE_INTERFACE_QXL,
561 .base.description = "qemu simple display",
562 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
563 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
564
565 .attache_worker = interface_attach_worker,
566 .set_compression_level = interface_set_compression_level,
567 .set_mm_time = interface_set_mm_time,
568 .get_init_info = interface_get_init_info,
569
570 /* the callbacks below are called from spice server thread context */
571 .get_command = interface_get_command,
572 .req_cmd_notification = interface_req_cmd_notification,
573 .release_resource = interface_release_resource,
574 .get_cursor_command = interface_get_cursor_command,
575 .req_cursor_notification = interface_req_cursor_notification,
576 .notify_update = interface_notify_update,
577 .flush_resources = interface_flush_resources,
578 .async_complete = interface_async_complete,
579 .update_area_complete = interface_update_area_complete,
580 .set_client_capabilities = interface_set_client_capabilities,
581 .client_monitors_config = interface_client_monitors_config,
582 };
583
584 static SimpleSpiceDisplay sdpy;
585
586 static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
587 {
588 qemu_spice_display_update(&sdpy, x, y, w, h);
589 }
590
591 static void display_resize(struct DisplayState *ds)
592 {
593 qemu_spice_display_resize(&sdpy);
594 }
595
596 static void display_refresh(struct DisplayState *ds)
597 {
598 qemu_spice_display_refresh(&sdpy);
599 }
600
601 static DisplayChangeListener display_listener = {
602 .dpy_gfx_update = display_update,
603 .dpy_gfx_resize = display_resize,
604 .dpy_refresh = display_refresh,
605 };
606
607 void qemu_spice_display_init(DisplayState *ds)
608 {
609 assert(sdpy.ds == NULL);
610 qemu_spice_display_init_common(&sdpy, ds);
611
612 sdpy.qxl.base.sif = &dpy_interface.base;
613 qemu_spice_add_interface(&sdpy.qxl.base);
614 assert(sdpy.worker);
615
616 qemu_spice_create_host_memslot(&sdpy);
617 qemu_spice_create_host_primary(&sdpy);
618 register_displaychangelistener(ds, &display_listener);
619 }