Merge remote-tracking branch 'qemu-kvm-tmp/memory/core' into staging
[qemu.git] / ui / spice-display.c
blobe38536114b7ef838ea28c52c6c52e225a3577d8a
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 <pthread.h>
20 #include "qemu-common.h"
21 #include "qemu-spice.h"
22 #include "qemu-timer.h"
23 #include "qemu-queue.h"
24 #include "monitor.h"
25 #include "console.h"
26 #include "sysemu.h"
28 #include "spice-display.h"
30 static int debug = 0;
32 static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
34 va_list args;
36 if (level <= debug) {
37 va_start(args, fmt);
38 vfprintf(stderr, fmt, args);
39 va_end(args);
43 int qemu_spice_rect_is_empty(const QXLRect* r)
45 return r->top == r->bottom || r->left == r->right;
48 void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
50 if (qemu_spice_rect_is_empty(r)) {
51 return;
54 if (qemu_spice_rect_is_empty(dest)) {
55 *dest = *r;
56 return;
59 dest->top = MIN(dest->top, r->top);
60 dest->left = MIN(dest->left, r->left);
61 dest->bottom = MAX(dest->bottom, r->bottom);
62 dest->right = MAX(dest->right, r->right);
65 void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
66 qxl_async_io async)
68 if (async != QXL_SYNC) {
69 #if SPICE_INTERFACE_QXL_MINOR >= 1
70 spice_qxl_add_memslot_async(&ssd->qxl, memslot, 0);
71 #else
72 abort();
73 #endif
74 } else {
75 ssd->worker->add_memslot(ssd->worker, memslot);
79 void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
81 ssd->worker->del_memslot(ssd->worker, gid, sid);
84 void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
85 QXLDevSurfaceCreate *surface,
86 qxl_async_io async)
88 if (async != QXL_SYNC) {
89 #if SPICE_INTERFACE_QXL_MINOR >= 1
90 spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface, 0);
91 #else
92 abort();
93 #endif
94 } else {
95 ssd->worker->create_primary_surface(ssd->worker, id, surface);
100 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
101 uint32_t id, qxl_async_io async)
103 if (async != QXL_SYNC) {
104 #if SPICE_INTERFACE_QXL_MINOR >= 1
105 spice_qxl_destroy_primary_surface_async(&ssd->qxl, id, 0);
106 #else
107 abort();
108 #endif
109 } else {
110 ssd->worker->destroy_primary_surface(ssd->worker, id);
114 void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
116 ssd->worker->wakeup(ssd->worker);
119 void qemu_spice_start(SimpleSpiceDisplay *ssd)
121 ssd->worker->start(ssd->worker);
124 void qemu_spice_stop(SimpleSpiceDisplay *ssd)
126 ssd->worker->stop(ssd->worker);
129 static SimpleSpiceUpdate *qemu_spice_create_update(SimpleSpiceDisplay *ssd)
131 SimpleSpiceUpdate *update;
132 QXLDrawable *drawable;
133 QXLImage *image;
134 QXLCommand *cmd;
135 uint8_t *src, *dst;
136 int by, bw, bh;
137 struct timespec time_space;
139 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
140 return NULL;
143 dprint(2, "%s: lr %d -> %d, tb -> %d -> %d\n", __FUNCTION__,
144 ssd->dirty.left, ssd->dirty.right,
145 ssd->dirty.top, ssd->dirty.bottom);
147 update = g_malloc0(sizeof(*update));
148 drawable = &update->drawable;
149 image = &update->image;
150 cmd = &update->ext.cmd;
152 bw = ssd->dirty.right - ssd->dirty.left;
153 bh = ssd->dirty.bottom - ssd->dirty.top;
154 update->bitmap = g_malloc(bw * bh * 4);
156 drawable->bbox = ssd->dirty;
157 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
158 drawable->effect = QXL_EFFECT_OPAQUE;
159 drawable->release_info.id = (intptr_t)update;
160 drawable->type = QXL_DRAW_COPY;
161 drawable->surfaces_dest[0] = -1;
162 drawable->surfaces_dest[1] = -1;
163 drawable->surfaces_dest[2] = -1;
164 clock_gettime(CLOCK_MONOTONIC, &time_space);
165 /* time in milliseconds from epoch. */
166 drawable->mm_time = time_space.tv_sec * 1000
167 + time_space.tv_nsec / 1000 / 1000;
169 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
170 drawable->u.copy.src_bitmap = (intptr_t)image;
171 drawable->u.copy.src_area.right = bw;
172 drawable->u.copy.src_area.bottom = bh;
174 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
175 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
176 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
177 image->bitmap.stride = bw * 4;
178 image->descriptor.width = image->bitmap.x = bw;
179 image->descriptor.height = image->bitmap.y = bh;
180 image->bitmap.data = (intptr_t)(update->bitmap);
181 image->bitmap.palette = 0;
182 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
184 if (ssd->conv == NULL) {
185 PixelFormat dst = qemu_default_pixelformat(32);
186 ssd->conv = qemu_pf_conv_get(&dst, &ssd->ds->surface->pf);
187 assert(ssd->conv);
190 src = ds_get_data(ssd->ds) +
191 ssd->dirty.top * ds_get_linesize(ssd->ds) +
192 ssd->dirty.left * ds_get_bytes_per_pixel(ssd->ds);
193 dst = update->bitmap;
194 for (by = 0; by < bh; by++) {
195 qemu_pf_conv_run(ssd->conv, dst, src, bw);
196 src += ds_get_linesize(ssd->ds);
197 dst += image->bitmap.stride;
200 cmd->type = QXL_CMD_DRAW;
201 cmd->data = (intptr_t)drawable;
203 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
204 return update;
208 * Called from spice server thread context (via interface_release_ressource)
209 * We do *not* hold the global qemu mutex here, so extra care is needed
210 * when calling qemu functions. Qemu interfaces used:
211 * - g_free (underlying glibc free is re-entrant).
213 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
215 g_free(update->bitmap);
216 g_free(update);
219 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
221 QXLDevMemSlot memslot;
223 dprint(1, "%s:\n", __FUNCTION__);
225 memset(&memslot, 0, sizeof(memslot));
226 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
227 memslot.virt_end = ~0;
228 qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
231 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
233 QXLDevSurfaceCreate surface;
235 dprint(1, "%s: %dx%d\n", __FUNCTION__,
236 ds_get_width(ssd->ds), ds_get_height(ssd->ds));
238 surface.format = SPICE_SURFACE_FMT_32_xRGB;
239 surface.width = ds_get_width(ssd->ds);
240 surface.height = ds_get_height(ssd->ds);
241 surface.stride = -surface.width * 4;
242 surface.mouse_mode = true;
243 surface.flags = 0;
244 surface.type = 0;
245 surface.mem = (intptr_t)ssd->buf;
246 surface.group_id = MEMSLOT_GROUP_HOST;
248 qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
251 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
253 dprint(1, "%s:\n", __FUNCTION__);
255 qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
258 void qemu_spice_vm_change_state_handler(void *opaque, int running, int reason)
260 SimpleSpiceDisplay *ssd = opaque;
262 if (running) {
263 ssd->running = true;
264 qemu_spice_start(ssd);
265 } else {
266 qemu_spice_stop(ssd);
267 ssd->running = false;
271 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd, DisplayState *ds)
273 ssd->ds = ds;
274 qemu_mutex_init(&ssd->lock);
275 ssd->mouse_x = -1;
276 ssd->mouse_y = -1;
277 ssd->bufsize = (16 * 1024 * 1024);
278 ssd->buf = g_malloc(ssd->bufsize);
281 /* display listener callbacks */
283 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
284 int x, int y, int w, int h)
286 QXLRect update_area;
288 dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__, x, y, w, h);
289 update_area.left = x,
290 update_area.right = x + w;
291 update_area.top = y;
292 update_area.bottom = y + h;
294 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
295 ssd->notify++;
297 qemu_spice_rect_union(&ssd->dirty, &update_area);
300 void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
302 dprint(1, "%s:\n", __FUNCTION__);
304 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
305 qemu_pf_conv_put(ssd->conv);
306 ssd->conv = NULL;
308 qemu_mutex_lock(&ssd->lock);
309 if (ssd->update != NULL) {
310 qemu_spice_destroy_update(ssd, ssd->update);
311 ssd->update = NULL;
313 qemu_mutex_unlock(&ssd->lock);
314 qemu_spice_destroy_host_primary(ssd);
315 qemu_spice_create_host_primary(ssd);
317 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
318 ssd->notify++;
321 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
323 dprint(3, "%s:\n", __FUNCTION__);
324 vga_hw_update();
326 qemu_mutex_lock(&ssd->lock);
327 if (ssd->update == NULL) {
328 ssd->update = qemu_spice_create_update(ssd);
329 ssd->notify++;
331 if (ssd->cursor) {
332 ssd->ds->cursor_define(ssd->cursor);
333 cursor_put(ssd->cursor);
334 ssd->cursor = NULL;
336 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
337 ssd->ds->mouse_set(ssd->mouse_x, ssd->mouse_y, 1);
338 ssd->mouse_x = -1;
339 ssd->mouse_y = -1;
341 qemu_mutex_unlock(&ssd->lock);
343 if (ssd->notify) {
344 ssd->notify = 0;
345 qemu_spice_wakeup(ssd);
346 dprint(2, "%s: notify\n", __FUNCTION__);
350 /* spice display interface callbacks */
352 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
354 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
356 dprint(1, "%s:\n", __FUNCTION__);
357 ssd->worker = qxl_worker;
360 static void interface_set_compression_level(QXLInstance *sin, int level)
362 dprint(1, "%s:\n", __FUNCTION__);
363 /* nothing to do */
366 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
368 dprint(3, "%s:\n", __FUNCTION__);
369 /* nothing to do */
372 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
374 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
376 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
377 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
378 info->num_memslots = NUM_MEMSLOTS;
379 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
380 info->internal_groupslot_id = 0;
381 info->qxl_ram_size = ssd->bufsize;
382 info->n_surfaces = NUM_SURFACES;
385 static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
387 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
388 SimpleSpiceUpdate *update;
389 int ret = false;
391 dprint(3, "%s:\n", __FUNCTION__);
393 qemu_mutex_lock(&ssd->lock);
394 if (ssd->update != NULL) {
395 update = ssd->update;
396 ssd->update = NULL;
397 *ext = update->ext;
398 ret = true;
400 qemu_mutex_unlock(&ssd->lock);
402 return ret;
405 static int interface_req_cmd_notification(QXLInstance *sin)
407 dprint(1, "%s:\n", __FUNCTION__);
408 return 1;
411 static void interface_release_resource(QXLInstance *sin,
412 struct QXLReleaseInfoExt ext)
414 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
415 uintptr_t id;
417 dprint(2, "%s:\n", __FUNCTION__);
418 id = ext.info->id;
419 qemu_spice_destroy_update(ssd, (void*)id);
422 static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
424 dprint(3, "%s:\n", __FUNCTION__);
425 return false;
428 static int interface_req_cursor_notification(QXLInstance *sin)
430 dprint(1, "%s:\n", __FUNCTION__);
431 return 1;
434 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
436 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
437 abort();
440 static int interface_flush_resources(QXLInstance *sin)
442 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
443 abort();
444 return 0;
447 static const QXLInterface dpy_interface = {
448 .base.type = SPICE_INTERFACE_QXL,
449 .base.description = "qemu simple display",
450 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
451 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
453 .attache_worker = interface_attach_worker,
454 .set_compression_level = interface_set_compression_level,
455 .set_mm_time = interface_set_mm_time,
456 .get_init_info = interface_get_init_info,
458 /* the callbacks below are called from spice server thread context */
459 .get_command = interface_get_command,
460 .req_cmd_notification = interface_req_cmd_notification,
461 .release_resource = interface_release_resource,
462 .get_cursor_command = interface_get_cursor_command,
463 .req_cursor_notification = interface_req_cursor_notification,
464 .notify_update = interface_notify_update,
465 .flush_resources = interface_flush_resources,
468 static SimpleSpiceDisplay sdpy;
470 static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
472 qemu_spice_display_update(&sdpy, x, y, w, h);
475 static void display_resize(struct DisplayState *ds)
477 qemu_spice_display_resize(&sdpy);
480 static void display_refresh(struct DisplayState *ds)
482 qemu_spice_display_refresh(&sdpy);
485 static DisplayChangeListener display_listener = {
486 .dpy_update = display_update,
487 .dpy_resize = display_resize,
488 .dpy_refresh = display_refresh,
491 void qemu_spice_display_init(DisplayState *ds)
493 assert(sdpy.ds == NULL);
494 qemu_spice_display_init_common(&sdpy, ds);
495 register_displaychangelistener(ds, &display_listener);
497 sdpy.qxl.base.sif = &dpy_interface.base;
498 qemu_spice_add_interface(&sdpy.qxl.base);
499 assert(sdpy.worker);
501 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler, &sdpy);
502 qemu_spice_create_host_memslot(&sdpy);
503 qemu_spice_create_host_primary(&sdpy);