blob: c6e61d84ea641df3e31af0b9b4730ce021cd3691 [file] [log] [blame]
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02001/*
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
Gerd Hoffmanna3e22262010-08-25 15:32:06 +020018#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
26#include "spice-display.h"
27
28static int debug = 0;
29
Stefan Weil2c80e422010-10-13 20:54:27 +020030static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +020031{
32 va_list args;
33
34 if (level <= debug) {
35 va_start(args, fmt);
36 vfprintf(stderr, fmt, args);
37 va_end(args);
38 }
39}
40
41int qemu_spice_rect_is_empty(const QXLRect* r)
42{
43 return r->top == r->bottom || r->left == r->right;
44}
45
46void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
47{
48 if (qemu_spice_rect_is_empty(r)) {
49 return;
50 }
51
52 if (qemu_spice_rect_is_empty(dest)) {
53 *dest = *r;
54 return;
55 }
56
57 dest->top = MIN(dest->top, r->top);
58 dest->left = MIN(dest->left, r->left);
59 dest->bottom = MAX(dest->bottom, r->bottom);
60 dest->right = MAX(dest->right, r->right);
61}
62
Alon Levy5ff4e362011-07-20 12:20:58 +030063void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
64 qxl_async_io async)
Gerd Hoffmann5c59d112011-07-20 12:20:50 +030065{
Alon Levy5ff4e362011-07-20 12:20:58 +030066 if (async != QXL_SYNC) {
67#if SPICE_INTERFACE_QXL_MINOR >= 1
68 spice_qxl_add_memslot_async(&ssd->qxl, memslot, 0);
69#else
70 abort();
71#endif
72 } else {
73 ssd->worker->add_memslot(ssd->worker, memslot);
74 }
Gerd Hoffmann5c59d112011-07-20 12:20:50 +030075}
76
77void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
78{
79 ssd->worker->del_memslot(ssd->worker, gid, sid);
80}
81
82void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
Alon Levy5ff4e362011-07-20 12:20:58 +030083 QXLDevSurfaceCreate *surface,
84 qxl_async_io async)
Gerd Hoffmann5c59d112011-07-20 12:20:50 +030085{
Alon Levy5ff4e362011-07-20 12:20:58 +030086 if (async != QXL_SYNC) {
87#if SPICE_INTERFACE_QXL_MINOR >= 1
88 spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface, 0);
89#else
90 abort();
91#endif
92 } else {
93 ssd->worker->create_primary_surface(ssd->worker, id, surface);
94 }
Gerd Hoffmann5c59d112011-07-20 12:20:50 +030095}
96
Alon Levy5ff4e362011-07-20 12:20:58 +030097
98void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
99 uint32_t id, qxl_async_io async)
Gerd Hoffmann5c59d112011-07-20 12:20:50 +0300100{
Alon Levy5ff4e362011-07-20 12:20:58 +0300101 if (async != QXL_SYNC) {
102#if SPICE_INTERFACE_QXL_MINOR >= 1
103 spice_qxl_destroy_primary_surface_async(&ssd->qxl, id, 0);
104#else
105 abort();
106#endif
107 } else {
108 ssd->worker->destroy_primary_surface(ssd->worker, id);
109 }
Gerd Hoffmann5c59d112011-07-20 12:20:50 +0300110}
111
Gerd Hoffmann5c59d112011-07-20 12:20:50 +0300112void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
113{
114 ssd->worker->wakeup(ssd->worker);
115}
116
Gerd Hoffmann5c59d112011-07-20 12:20:50 +0300117void qemu_spice_start(SimpleSpiceDisplay *ssd)
118{
119 ssd->worker->start(ssd->worker);
120}
121
122void qemu_spice_stop(SimpleSpiceDisplay *ssd)
123{
124 ssd->worker->stop(ssd->worker);
125}
126
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200127static SimpleSpiceUpdate *qemu_spice_create_update(SimpleSpiceDisplay *ssd)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200128{
129 SimpleSpiceUpdate *update;
130 QXLDrawable *drawable;
131 QXLImage *image;
132 QXLCommand *cmd;
133 uint8_t *src, *dst;
134 int by, bw, bh;
Alon Levy22795172011-06-15 20:44:38 +0200135 struct timespec time_space;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200136
137 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
138 return NULL;
139 };
140
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200141 dprint(2, "%s: lr %d -> %d, tb -> %d -> %d\n", __FUNCTION__,
142 ssd->dirty.left, ssd->dirty.right,
143 ssd->dirty.top, ssd->dirty.bottom);
144
Anthony Liguori7267c092011-08-20 22:09:37 -0500145 update = g_malloc0(sizeof(*update));
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200146 drawable = &update->drawable;
147 image = &update->image;
148 cmd = &update->ext.cmd;
149
150 bw = ssd->dirty.right - ssd->dirty.left;
151 bh = ssd->dirty.bottom - ssd->dirty.top;
Anthony Liguori7267c092011-08-20 22:09:37 -0500152 update->bitmap = g_malloc(bw * bh * 4);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200153
154 drawable->bbox = ssd->dirty;
155 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
156 drawable->effect = QXL_EFFECT_OPAQUE;
157 drawable->release_info.id = (intptr_t)update;
158 drawable->type = QXL_DRAW_COPY;
159 drawable->surfaces_dest[0] = -1;
160 drawable->surfaces_dest[1] = -1;
161 drawable->surfaces_dest[2] = -1;
Alon Levy22795172011-06-15 20:44:38 +0200162 clock_gettime(CLOCK_MONOTONIC, &time_space);
163 /* time in milliseconds from epoch. */
164 drawable->mm_time = time_space.tv_sec * 1000
165 + time_space.tv_nsec / 1000 / 1000;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200166
167 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
168 drawable->u.copy.src_bitmap = (intptr_t)image;
169 drawable->u.copy.src_area.right = bw;
170 drawable->u.copy.src_area.bottom = bh;
171
172 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
173 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
174 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
175 image->bitmap.stride = bw * 4;
176 image->descriptor.width = image->bitmap.x = bw;
177 image->descriptor.height = image->bitmap.y = bh;
178 image->bitmap.data = (intptr_t)(update->bitmap);
179 image->bitmap.palette = 0;
180 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
181
182 if (ssd->conv == NULL) {
183 PixelFormat dst = qemu_default_pixelformat(32);
184 ssd->conv = qemu_pf_conv_get(&dst, &ssd->ds->surface->pf);
185 assert(ssd->conv);
186 }
187
188 src = ds_get_data(ssd->ds) +
189 ssd->dirty.top * ds_get_linesize(ssd->ds) +
190 ssd->dirty.left * ds_get_bytes_per_pixel(ssd->ds);
191 dst = update->bitmap;
192 for (by = 0; by < bh; by++) {
193 qemu_pf_conv_run(ssd->conv, dst, src, bw);
194 src += ds_get_linesize(ssd->ds);
195 dst += image->bitmap.stride;
196 }
197
198 cmd->type = QXL_CMD_DRAW;
199 cmd->data = (intptr_t)drawable;
200
201 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200202 return update;
203}
204
205/*
206 * Called from spice server thread context (via interface_release_ressource)
207 * We do *not* hold the global qemu mutex here, so extra care is needed
208 * when calling qemu functions. Qemu interfaces used:
Anthony Liguori7267c092011-08-20 22:09:37 -0500209 * - g_free (underlying glibc free is re-entrant).
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200210 */
211void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
212{
Anthony Liguori7267c092011-08-20 22:09:37 -0500213 g_free(update->bitmap);
214 g_free(update);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200215}
216
217void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
218{
219 QXLDevMemSlot memslot;
220
221 dprint(1, "%s:\n", __FUNCTION__);
222
223 memset(&memslot, 0, sizeof(memslot));
224 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
225 memslot.virt_end = ~0;
Alon Levy5ff4e362011-07-20 12:20:58 +0300226 qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200227}
228
229void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
230{
231 QXLDevSurfaceCreate surface;
232
233 dprint(1, "%s: %dx%d\n", __FUNCTION__,
234 ds_get_width(ssd->ds), ds_get_height(ssd->ds));
235
236 surface.format = SPICE_SURFACE_FMT_32_xRGB;
237 surface.width = ds_get_width(ssd->ds);
238 surface.height = ds_get_height(ssd->ds);
239 surface.stride = -surface.width * 4;
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200240 surface.mouse_mode = true;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200241 surface.flags = 0;
242 surface.type = 0;
243 surface.mem = (intptr_t)ssd->buf;
244 surface.group_id = MEMSLOT_GROUP_HOST;
Gerd Hoffmann7466bc42010-10-14 16:55:01 +0200245
Alon Levy5ff4e362011-07-20 12:20:58 +0300246 qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200247}
248
249void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
250{
251 dprint(1, "%s:\n", __FUNCTION__);
252
Alon Levy5ff4e362011-07-20 12:20:58 +0300253 qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200254}
255
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -0300256void qemu_spice_vm_change_state_handler(void *opaque, int running,
257 RunState state)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200258{
259 SimpleSpiceDisplay *ssd = opaque;
260
261 if (running) {
Yonit Halperin7e79cf42011-09-05 17:39:50 +0300262 ssd->running = true;
Gerd Hoffmann5c59d112011-07-20 12:20:50 +0300263 qemu_spice_start(ssd);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200264 } else {
Gerd Hoffmann5c59d112011-07-20 12:20:50 +0300265 qemu_spice_stop(ssd);
Yonit Halperin7e79cf42011-09-05 17:39:50 +0300266 ssd->running = false;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200267 }
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200268}
269
Gerd Hoffmanna963f872011-07-20 12:20:51 +0300270void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd, DisplayState *ds)
271{
272 ssd->ds = ds;
273 qemu_mutex_init(&ssd->lock);
274 ssd->mouse_x = -1;
275 ssd->mouse_y = -1;
276 ssd->bufsize = (16 * 1024 * 1024);
Anthony Liguori7267c092011-08-20 22:09:37 -0500277 ssd->buf = g_malloc(ssd->bufsize);
Gerd Hoffmanna963f872011-07-20 12:20:51 +0300278}
279
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200280/* display listener callbacks */
281
282void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
283 int x, int y, int w, int h)
284{
285 QXLRect update_area;
286
287 dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__, x, y, w, h);
288 update_area.left = x,
289 update_area.right = x + w;
290 update_area.top = y;
291 update_area.bottom = y + h;
292
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200293 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
294 ssd->notify++;
295 }
296 qemu_spice_rect_union(&ssd->dirty, &update_area);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200297}
298
299void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
300{
301 dprint(1, "%s:\n", __FUNCTION__);
302
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200303 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
304 qemu_pf_conv_put(ssd->conv);
305 ssd->conv = NULL;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200306
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200307 qemu_mutex_lock(&ssd->lock);
308 if (ssd->update != NULL) {
309 qemu_spice_destroy_update(ssd, ssd->update);
310 ssd->update = NULL;
311 }
312 qemu_mutex_unlock(&ssd->lock);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200313 qemu_spice_destroy_host_primary(ssd);
314 qemu_spice_create_host_primary(ssd);
315
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200316 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
317 ssd->notify++;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200318}
319
Alon Levybb5a8cd2012-02-24 23:19:25 +0200320void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200321{
Gerd Hoffmann07536092011-04-27 15:50:32 +0200322 if (ssd->cursor) {
323 ssd->ds->cursor_define(ssd->cursor);
324 cursor_put(ssd->cursor);
325 ssd->cursor = NULL;
326 }
327 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
328 ssd->ds->mouse_set(ssd->mouse_x, ssd->mouse_y, 1);
329 ssd->mouse_x = -1;
330 ssd->mouse_y = -1;
331 }
Alon Levybb5a8cd2012-02-24 23:19:25 +0200332}
333
334void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
335{
336 dprint(3, "%s:\n", __func__);
337 vga_hw_update();
338
339 qemu_mutex_lock(&ssd->lock);
340 if (ssd->update == NULL) {
341 ssd->update = qemu_spice_create_update(ssd);
342 ssd->notify++;
343 }
344 qemu_spice_cursor_refresh_unlocked(ssd);
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200345 qemu_mutex_unlock(&ssd->lock);
346
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200347 if (ssd->notify) {
348 ssd->notify = 0;
Gerd Hoffmann5c59d112011-07-20 12:20:50 +0300349 qemu_spice_wakeup(ssd);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200350 dprint(2, "%s: notify\n", __FUNCTION__);
351 }
352}
353
354/* spice display interface callbacks */
355
356static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
357{
358 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
359
360 dprint(1, "%s:\n", __FUNCTION__);
361 ssd->worker = qxl_worker;
362}
363
364static void interface_set_compression_level(QXLInstance *sin, int level)
365{
366 dprint(1, "%s:\n", __FUNCTION__);
367 /* nothing to do */
368}
369
370static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
371{
372 dprint(3, "%s:\n", __FUNCTION__);
373 /* nothing to do */
374}
375
376static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
377{
378 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
379
380 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
381 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
382 info->num_memslots = NUM_MEMSLOTS;
383 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
384 info->internal_groupslot_id = 0;
385 info->qxl_ram_size = ssd->bufsize;
386 info->n_surfaces = NUM_SURFACES;
387}
388
389static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
390{
391 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
392 SimpleSpiceUpdate *update;
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200393 int ret = false;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200394
395 dprint(3, "%s:\n", __FUNCTION__);
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200396
397 qemu_mutex_lock(&ssd->lock);
398 if (ssd->update != NULL) {
399 update = ssd->update;
400 ssd->update = NULL;
401 *ext = update->ext;
402 ret = true;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200403 }
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200404 qemu_mutex_unlock(&ssd->lock);
405
406 return ret;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200407}
408
409static int interface_req_cmd_notification(QXLInstance *sin)
410{
411 dprint(1, "%s:\n", __FUNCTION__);
412 return 1;
413}
414
415static void interface_release_resource(QXLInstance *sin,
416 struct QXLReleaseInfoExt ext)
417{
418 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
419 uintptr_t id;
420
421 dprint(2, "%s:\n", __FUNCTION__);
422 id = ext.info->id;
423 qemu_spice_destroy_update(ssd, (void*)id);
424}
425
426static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
427{
428 dprint(3, "%s:\n", __FUNCTION__);
429 return false;
430}
431
432static int interface_req_cursor_notification(QXLInstance *sin)
433{
434 dprint(1, "%s:\n", __FUNCTION__);
435 return 1;
436}
437
438static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
439{
440 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
441 abort();
442}
443
444static int interface_flush_resources(QXLInstance *sin)
445{
446 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
447 abort();
448 return 0;
449}
450
451static const QXLInterface dpy_interface = {
452 .base.type = SPICE_INTERFACE_QXL,
453 .base.description = "qemu simple display",
454 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
455 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
456
457 .attache_worker = interface_attach_worker,
458 .set_compression_level = interface_set_compression_level,
459 .set_mm_time = interface_set_mm_time,
460 .get_init_info = interface_get_init_info,
461
462 /* the callbacks below are called from spice server thread context */
463 .get_command = interface_get_command,
464 .req_cmd_notification = interface_req_cmd_notification,
465 .release_resource = interface_release_resource,
466 .get_cursor_command = interface_get_cursor_command,
467 .req_cursor_notification = interface_req_cursor_notification,
468 .notify_update = interface_notify_update,
469 .flush_resources = interface_flush_resources,
470};
471
472static SimpleSpiceDisplay sdpy;
473
474static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
475{
476 qemu_spice_display_update(&sdpy, x, y, w, h);
477}
478
479static void display_resize(struct DisplayState *ds)
480{
481 qemu_spice_display_resize(&sdpy);
482}
483
484static void display_refresh(struct DisplayState *ds)
485{
486 qemu_spice_display_refresh(&sdpy);
487}
488
489static DisplayChangeListener display_listener = {
490 .dpy_update = display_update,
491 .dpy_resize = display_resize,
492 .dpy_refresh = display_refresh,
493};
494
495void qemu_spice_display_init(DisplayState *ds)
496{
497 assert(sdpy.ds == NULL);
Gerd Hoffmanna963f872011-07-20 12:20:51 +0300498 qemu_spice_display_init_common(&sdpy, ds);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200499 register_displaychangelistener(ds, &display_listener);
500
501 sdpy.qxl.base.sif = &dpy_interface.base;
502 qemu_spice_add_interface(&sdpy.qxl.base);
503 assert(sdpy.worker);
504
505 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler, &sdpy);
506 qemu_spice_create_host_memslot(&sdpy);
507 qemu_spice_create_host_primary(&sdpy);
508}