pseries: Small cleanup to H_CEDE implementation
[qemu/opensuse.git] / ui / spice-display.c
blob50fbefb067471562996d5e65dbced085a62265c4
1 /*
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"
22 #include "monitor.h"
23 #include "console.h"
24 #include "sysemu.h"
25 #include "trace.h"
27 #include "spice-display.h"
29 static int debug = 0;
31 static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
33 va_list args;
35 if (level <= debug) {
36 va_start(args, fmt);
37 vfprintf(stderr, fmt, args);
38 va_end(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)) {
50 return;
53 if (qemu_spice_rect_is_empty(dest)) {
54 *dest = *r;
55 return;
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)
66 QXLCookie *cookie;
68 cookie = g_malloc0(sizeof(*cookie));
69 cookie->type = type;
70 cookie->io = io;
71 return cookie;
74 void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
75 qxl_async_io async)
77 trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
78 memslot->virt_start, memslot->virt_end,
79 async);
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);
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,
98 qxl_async_io async)
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);
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));
118 } else {
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 #if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
130 static void qemu_spice_start(SimpleSpiceDisplay *ssd)
132 trace_qemu_spice_start(ssd->qxl.id);
133 ssd->worker->start(ssd->worker);
136 static void qemu_spice_stop(SimpleSpiceDisplay *ssd)
138 trace_qemu_spice_stop(ssd->qxl.id);
139 ssd->worker->stop(ssd->worker);
142 #else
144 static int spice_display_is_running;
146 void qemu_spice_display_start(void)
148 spice_display_is_running = true;
151 void qemu_spice_display_stop(void)
153 spice_display_is_running = false;
156 #endif
158 int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
160 #if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
161 return ssd->running;
162 #else
163 return spice_display_is_running;
164 #endif
167 static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
168 QXLRect *rect)
170 SimpleSpiceUpdate *update;
171 QXLDrawable *drawable;
172 QXLImage *image;
173 QXLCommand *cmd;
174 uint8_t *src, *mirror, *dst;
175 int by, bw, bh, offset, bytes;
176 struct timespec time_space;
178 trace_qemu_spice_create_update(
179 rect->left, rect->right,
180 rect->top, rect->bottom);
182 update = g_malloc0(sizeof(*update));
183 drawable = &update->drawable;
184 image = &update->image;
185 cmd = &update->ext.cmd;
187 bw = rect->right - rect->left;
188 bh = rect->bottom - rect->top;
189 update->bitmap = g_malloc(bw * bh * 4);
191 drawable->bbox = *rect;
192 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
193 drawable->effect = QXL_EFFECT_OPAQUE;
194 drawable->release_info.id = (uintptr_t)update;
195 drawable->type = QXL_DRAW_COPY;
196 drawable->surfaces_dest[0] = -1;
197 drawable->surfaces_dest[1] = -1;
198 drawable->surfaces_dest[2] = -1;
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;
204 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
205 drawable->u.copy.src_bitmap = (uintptr_t)image;
206 drawable->u.copy.src_area.right = bw;
207 drawable->u.copy.src_area.bottom = bh;
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;
215 image->bitmap.data = (uintptr_t)(update->bitmap);
216 image->bitmap.palette = 0;
217 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
219 offset =
220 rect->top * ds_get_linesize(ssd->ds) +
221 rect->left * ds_get_bytes_per_pixel(ssd->ds);
222 bytes = ds_get_bytes_per_pixel(ssd->ds) * bw;
223 src = ds_get_data(ssd->ds) + offset;
224 mirror = ssd->ds_mirror + offset;
225 dst = update->bitmap;
226 for (by = 0; by < bh; by++) {
227 memcpy(mirror, src, bytes);
228 qemu_pf_conv_run(ssd->conv, dst, mirror, bw);
229 src += ds_get_linesize(ssd->ds);
230 mirror += ds_get_linesize(ssd->ds);
231 dst += image->bitmap.stride;
234 cmd->type = QXL_CMD_DRAW;
235 cmd->data = (uintptr_t)drawable;
237 QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
240 static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
242 static const int blksize = 32;
243 int blocks = (ds_get_width(ssd->ds) + blksize - 1) / blksize;
244 int dirty_top[blocks];
245 int y, yoff, x, xoff, blk, bw;
246 int bpp = ds_get_bytes_per_pixel(ssd->ds);
247 uint8_t *guest, *mirror;
249 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
250 return;
253 if (ssd->conv == NULL) {
254 PixelFormat dst = qemu_default_pixelformat(32);
255 ssd->conv = qemu_pf_conv_get(&dst, &ssd->ds->surface->pf);
256 assert(ssd->conv);
258 if (ssd->ds_mirror == NULL) {
259 int size = ds_get_height(ssd->ds) * ds_get_linesize(ssd->ds);
260 ssd->ds_mirror = g_malloc0(size);
263 for (blk = 0; blk < blocks; blk++) {
264 dirty_top[blk] = -1;
267 guest = ds_get_data(ssd->ds);
268 mirror = ssd->ds_mirror;
269 for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
270 yoff = y * ds_get_linesize(ssd->ds);
271 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
272 xoff = x * bpp;
273 blk = x / blksize;
274 bw = MIN(blksize, ssd->dirty.right - x);
275 if (memcmp(guest + yoff + xoff,
276 mirror + yoff + xoff,
277 bw * bpp) == 0) {
278 if (dirty_top[blk] != -1) {
279 QXLRect update = {
280 .top = dirty_top[blk],
281 .bottom = y,
282 .left = x,
283 .right = x + bw,
285 qemu_spice_create_one_update(ssd, &update);
286 dirty_top[blk] = -1;
288 } else {
289 if (dirty_top[blk] == -1) {
290 dirty_top[blk] = y;
296 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
297 blk = x / blksize;
298 bw = MIN(blksize, ssd->dirty.right - x);
299 if (dirty_top[blk] != -1) {
300 QXLRect update = {
301 .top = dirty_top[blk],
302 .bottom = ssd->dirty.bottom,
303 .left = x,
304 .right = x + bw,
306 qemu_spice_create_one_update(ssd, &update);
307 dirty_top[blk] = -1;
311 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
315 * Called from spice server thread context (via interface_release_resource)
316 * We do *not* hold the global qemu mutex here, so extra care is needed
317 * when calling qemu functions. QEMU interfaces used:
318 * - g_free (underlying glibc free is re-entrant).
320 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
322 g_free(update->bitmap);
323 g_free(update);
326 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
328 QXLDevMemSlot memslot;
330 dprint(1, "%s:\n", __FUNCTION__);
332 memset(&memslot, 0, sizeof(memslot));
333 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
334 memslot.virt_end = ~0;
335 qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
338 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
340 QXLDevSurfaceCreate surface;
342 memset(&surface, 0, sizeof(surface));
344 dprint(1, "%s: %dx%d\n", __FUNCTION__,
345 ds_get_width(ssd->ds), ds_get_height(ssd->ds));
347 surface.format = SPICE_SURFACE_FMT_32_xRGB;
348 surface.width = ds_get_width(ssd->ds);
349 surface.height = ds_get_height(ssd->ds);
350 surface.stride = -surface.width * 4;
351 surface.mouse_mode = true;
352 surface.flags = 0;
353 surface.type = 0;
354 surface.mem = (uintptr_t)ssd->buf;
355 surface.group_id = MEMSLOT_GROUP_HOST;
357 qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
360 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
362 dprint(1, "%s:\n", __FUNCTION__);
364 qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
367 void qemu_spice_vm_change_state_handler(void *opaque, int running,
368 RunState state)
370 #if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
371 SimpleSpiceDisplay *ssd = opaque;
373 if (running) {
374 ssd->running = true;
375 qemu_spice_start(ssd);
376 } else {
377 qemu_spice_stop(ssd);
378 ssd->running = false;
380 #endif
383 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd, DisplayState *ds)
385 ssd->ds = ds;
386 qemu_mutex_init(&ssd->lock);
387 QTAILQ_INIT(&ssd->updates);
388 ssd->mouse_x = -1;
389 ssd->mouse_y = -1;
390 if (ssd->num_surfaces == 0) {
391 ssd->num_surfaces = 1024;
393 ssd->bufsize = (16 * 1024 * 1024);
394 ssd->buf = g_malloc(ssd->bufsize);
397 /* display listener callbacks */
399 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
400 int x, int y, int w, int h)
402 QXLRect update_area;
404 dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__, x, y, w, h);
405 update_area.left = x,
406 update_area.right = x + w;
407 update_area.top = y;
408 update_area.bottom = y + h;
410 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
411 ssd->notify++;
413 qemu_spice_rect_union(&ssd->dirty, &update_area);
416 void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
418 SimpleSpiceUpdate *update;
420 dprint(1, "%s:\n", __FUNCTION__);
422 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
423 qemu_pf_conv_put(ssd->conv);
424 ssd->conv = NULL;
425 g_free(ssd->ds_mirror);
426 ssd->ds_mirror = NULL;
428 qemu_mutex_lock(&ssd->lock);
429 while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
430 QTAILQ_REMOVE(&ssd->updates, update, next);
431 qemu_spice_destroy_update(ssd, update);
433 qemu_mutex_unlock(&ssd->lock);
434 qemu_spice_destroy_host_primary(ssd);
435 qemu_spice_create_host_primary(ssd);
437 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
438 ssd->notify++;
441 void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
443 if (ssd->cursor) {
444 ssd->ds->cursor_define(ssd->cursor);
445 cursor_put(ssd->cursor);
446 ssd->cursor = NULL;
448 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
449 ssd->ds->mouse_set(ssd->mouse_x, ssd->mouse_y, 1);
450 ssd->mouse_x = -1;
451 ssd->mouse_y = -1;
455 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
457 dprint(3, "%s:\n", __func__);
458 vga_hw_update();
460 qemu_mutex_lock(&ssd->lock);
461 if (QTAILQ_EMPTY(&ssd->updates)) {
462 qemu_spice_create_update(ssd);
463 ssd->notify++;
465 qemu_spice_cursor_refresh_unlocked(ssd);
466 qemu_mutex_unlock(&ssd->lock);
468 if (ssd->notify) {
469 ssd->notify = 0;
470 qemu_spice_wakeup(ssd);
471 dprint(2, "%s: notify\n", __FUNCTION__);
475 /* spice display interface callbacks */
477 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
479 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
481 dprint(1, "%s:\n", __FUNCTION__);
482 ssd->worker = qxl_worker;
485 static void interface_set_compression_level(QXLInstance *sin, int level)
487 dprint(1, "%s:\n", __FUNCTION__);
488 /* nothing to do */
491 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
493 dprint(3, "%s:\n", __FUNCTION__);
494 /* nothing to do */
497 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
499 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
501 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
502 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
503 info->num_memslots = NUM_MEMSLOTS;
504 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
505 info->internal_groupslot_id = 0;
506 info->qxl_ram_size = ssd->bufsize;
507 info->n_surfaces = ssd->num_surfaces;
510 static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
512 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
513 SimpleSpiceUpdate *update;
514 int ret = false;
516 dprint(3, "%s:\n", __FUNCTION__);
518 qemu_mutex_lock(&ssd->lock);
519 update = QTAILQ_FIRST(&ssd->updates);
520 if (update != NULL) {
521 QTAILQ_REMOVE(&ssd->updates, update, next);
522 *ext = update->ext;
523 ret = true;
525 qemu_mutex_unlock(&ssd->lock);
527 return ret;
530 static int interface_req_cmd_notification(QXLInstance *sin)
532 dprint(1, "%s:\n", __FUNCTION__);
533 return 1;
536 static void interface_release_resource(QXLInstance *sin,
537 struct QXLReleaseInfoExt ext)
539 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
540 uintptr_t id;
542 dprint(2, "%s:\n", __FUNCTION__);
543 id = ext.info->id;
544 qemu_spice_destroy_update(ssd, (void*)id);
547 static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
549 dprint(3, "%s:\n", __FUNCTION__);
550 return false;
553 static int interface_req_cursor_notification(QXLInstance *sin)
555 dprint(1, "%s:\n", __FUNCTION__);
556 return 1;
559 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
561 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
562 abort();
565 static int interface_flush_resources(QXLInstance *sin)
567 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
568 abort();
569 return 0;
572 static const QXLInterface dpy_interface = {
573 .base.type = SPICE_INTERFACE_QXL,
574 .base.description = "qemu simple display",
575 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
576 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
578 .attache_worker = interface_attach_worker,
579 .set_compression_level = interface_set_compression_level,
580 .set_mm_time = interface_set_mm_time,
581 .get_init_info = interface_get_init_info,
583 /* the callbacks below are called from spice server thread context */
584 .get_command = interface_get_command,
585 .req_cmd_notification = interface_req_cmd_notification,
586 .release_resource = interface_release_resource,
587 .get_cursor_command = interface_get_cursor_command,
588 .req_cursor_notification = interface_req_cursor_notification,
589 .notify_update = interface_notify_update,
590 .flush_resources = interface_flush_resources,
593 static SimpleSpiceDisplay sdpy;
595 static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
597 qemu_spice_display_update(&sdpy, x, y, w, h);
600 static void display_resize(struct DisplayState *ds)
602 qemu_spice_display_resize(&sdpy);
605 static void display_refresh(struct DisplayState *ds)
607 qemu_spice_display_refresh(&sdpy);
610 static DisplayChangeListener display_listener = {
611 .dpy_update = display_update,
612 .dpy_resize = display_resize,
613 .dpy_refresh = display_refresh,
616 void qemu_spice_display_init(DisplayState *ds)
618 assert(sdpy.ds == NULL);
619 qemu_spice_display_init_common(&sdpy, ds);
620 register_displaychangelistener(ds, &display_listener);
622 sdpy.qxl.base.sif = &dpy_interface.base;
623 qemu_spice_add_interface(&sdpy.qxl.base);
624 assert(sdpy.worker);
626 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler, &sdpy);
627 qemu_spice_create_host_memslot(&sdpy);
628 qemu_spice_create_host_primary(&sdpy);