xref: /qemu/ui/spice-display.c (revision 6c1fdcf9)
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 "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 
28 static int debug = 0;
29 
30 static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
31 {
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 
41 int qemu_spice_rect_is_empty(const QXLRect* r)
42 {
43     return r->top == r->bottom || r->left == r->right;
44 }
45 
46 void 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 
63 QXLCookie *qxl_cookie_new(int type, uint64_t io)
64 {
65     QXLCookie *cookie;
66 
67     cookie = g_malloc0(sizeof(*cookie));
68     cookie->type = type;
69     cookie->io = io;
70     return cookie;
71 }
72 
73 void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
74                             qxl_async_io async)
75 {
76     if (async != QXL_SYNC) {
77         spice_qxl_add_memslot_async(&ssd->qxl, memslot,
78                 (uint64_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
79                                          QXL_IO_MEMSLOT_ADD_ASYNC));
80     } else {
81         ssd->worker->add_memslot(ssd->worker, memslot);
82     }
83 }
84 
85 void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
86 {
87     ssd->worker->del_memslot(ssd->worker, gid, sid);
88 }
89 
90 void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
91                                        QXLDevSurfaceCreate *surface,
92                                        qxl_async_io async)
93 {
94     if (async != QXL_SYNC) {
95         spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface,
96                 (uint64_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
97                                          QXL_IO_CREATE_PRIMARY_ASYNC));
98     } else {
99         ssd->worker->create_primary_surface(ssd->worker, id, surface);
100     }
101 }
102 
103 
104 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
105                                         uint32_t id, qxl_async_io async)
106 {
107     if (async != QXL_SYNC) {
108         spice_qxl_destroy_primary_surface_async(&ssd->qxl, id,
109                 (uint64_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
110                                          QXL_IO_DESTROY_PRIMARY_ASYNC));
111     } else {
112         ssd->worker->destroy_primary_surface(ssd->worker, id);
113     }
114 }
115 
116 void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
117 {
118     ssd->worker->wakeup(ssd->worker);
119 }
120 
121 void qemu_spice_start(SimpleSpiceDisplay *ssd)
122 {
123     ssd->worker->start(ssd->worker);
124 }
125 
126 void qemu_spice_stop(SimpleSpiceDisplay *ssd)
127 {
128     ssd->worker->stop(ssd->worker);
129 }
130 
131 static SimpleSpiceUpdate *qemu_spice_create_update(SimpleSpiceDisplay *ssd)
132 {
133     SimpleSpiceUpdate *update;
134     QXLDrawable *drawable;
135     QXLImage *image;
136     QXLCommand *cmd;
137     uint8_t *src, *dst;
138     int by, bw, bh;
139     struct timespec time_space;
140 
141     if (qemu_spice_rect_is_empty(&ssd->dirty)) {
142         return NULL;
143     };
144 
145     dprint(2, "%s: lr %d -> %d,  tb -> %d -> %d\n", __FUNCTION__,
146            ssd->dirty.left, ssd->dirty.right,
147            ssd->dirty.top, ssd->dirty.bottom);
148 
149     update   = g_malloc0(sizeof(*update));
150     drawable = &update->drawable;
151     image    = &update->image;
152     cmd      = &update->ext.cmd;
153 
154     bw       = ssd->dirty.right - ssd->dirty.left;
155     bh       = ssd->dirty.bottom - ssd->dirty.top;
156     update->bitmap = g_malloc(bw * bh * 4);
157 
158     drawable->bbox            = ssd->dirty;
159     drawable->clip.type       = SPICE_CLIP_TYPE_NONE;
160     drawable->effect          = QXL_EFFECT_OPAQUE;
161     drawable->release_info.id = (intptr_t)update;
162     drawable->type            = QXL_DRAW_COPY;
163     drawable->surfaces_dest[0] = -1;
164     drawable->surfaces_dest[1] = -1;
165     drawable->surfaces_dest[2] = -1;
166     clock_gettime(CLOCK_MONOTONIC, &time_space);
167     /* time in milliseconds from epoch. */
168     drawable->mm_time = time_space.tv_sec * 1000
169                       + time_space.tv_nsec / 1000 / 1000;
170 
171     drawable->u.copy.rop_descriptor  = SPICE_ROPD_OP_PUT;
172     drawable->u.copy.src_bitmap      = (intptr_t)image;
173     drawable->u.copy.src_area.right  = bw;
174     drawable->u.copy.src_area.bottom = bh;
175 
176     QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
177     image->descriptor.type   = SPICE_IMAGE_TYPE_BITMAP;
178     image->bitmap.flags      = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
179     image->bitmap.stride     = bw * 4;
180     image->descriptor.width  = image->bitmap.x = bw;
181     image->descriptor.height = image->bitmap.y = bh;
182     image->bitmap.data = (intptr_t)(update->bitmap);
183     image->bitmap.palette = 0;
184     image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
185 
186     if (ssd->conv == NULL) {
187         PixelFormat dst = qemu_default_pixelformat(32);
188         ssd->conv = qemu_pf_conv_get(&dst, &ssd->ds->surface->pf);
189         assert(ssd->conv);
190     }
191 
192     src = ds_get_data(ssd->ds) +
193         ssd->dirty.top * ds_get_linesize(ssd->ds) +
194         ssd->dirty.left * ds_get_bytes_per_pixel(ssd->ds);
195     dst = update->bitmap;
196     for (by = 0; by < bh; by++) {
197         qemu_pf_conv_run(ssd->conv, dst, src, bw);
198         src += ds_get_linesize(ssd->ds);
199         dst += image->bitmap.stride;
200     }
201 
202     cmd->type = QXL_CMD_DRAW;
203     cmd->data = (intptr_t)drawable;
204 
205     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
206     return update;
207 }
208 
209 /*
210  * Called from spice server thread context (via interface_release_ressource)
211  * We do *not* hold the global qemu mutex here, so extra care is needed
212  * when calling qemu functions.  Qemu interfaces used:
213  *    - g_free (underlying glibc free is re-entrant).
214  */
215 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
216 {
217     g_free(update->bitmap);
218     g_free(update);
219 }
220 
221 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
222 {
223     QXLDevMemSlot memslot;
224 
225     dprint(1, "%s:\n", __FUNCTION__);
226 
227     memset(&memslot, 0, sizeof(memslot));
228     memslot.slot_group_id = MEMSLOT_GROUP_HOST;
229     memslot.virt_end = ~0;
230     qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
231 }
232 
233 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
234 {
235     QXLDevSurfaceCreate surface;
236 
237     dprint(1, "%s: %dx%d\n", __FUNCTION__,
238            ds_get_width(ssd->ds), ds_get_height(ssd->ds));
239 
240     surface.format     = SPICE_SURFACE_FMT_32_xRGB;
241     surface.width      = ds_get_width(ssd->ds);
242     surface.height     = ds_get_height(ssd->ds);
243     surface.stride     = -surface.width * 4;
244     surface.mouse_mode = true;
245     surface.flags      = 0;
246     surface.type       = 0;
247     surface.mem        = (intptr_t)ssd->buf;
248     surface.group_id   = MEMSLOT_GROUP_HOST;
249 
250     qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
251 }
252 
253 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
254 {
255     dprint(1, "%s:\n", __FUNCTION__);
256 
257     qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
258 }
259 
260 void qemu_spice_vm_change_state_handler(void *opaque, int running,
261                                         RunState state)
262 {
263     SimpleSpiceDisplay *ssd = opaque;
264 
265     if (running) {
266         ssd->running = true;
267         qemu_spice_start(ssd);
268     } else {
269         qemu_spice_stop(ssd);
270         ssd->running = false;
271     }
272 }
273 
274 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd, DisplayState *ds)
275 {
276     ssd->ds = ds;
277     qemu_mutex_init(&ssd->lock);
278     ssd->mouse_x = -1;
279     ssd->mouse_y = -1;
280     ssd->bufsize = (16 * 1024 * 1024);
281     ssd->buf = g_malloc(ssd->bufsize);
282 }
283 
284 /* display listener callbacks */
285 
286 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
287                                int x, int y, int w, int h)
288 {
289     QXLRect update_area;
290 
291     dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__, x, y, w, h);
292     update_area.left = x,
293     update_area.right = x + w;
294     update_area.top = y;
295     update_area.bottom = y + h;
296 
297     if (qemu_spice_rect_is_empty(&ssd->dirty)) {
298         ssd->notify++;
299     }
300     qemu_spice_rect_union(&ssd->dirty, &update_area);
301 }
302 
303 void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
304 {
305     dprint(1, "%s:\n", __FUNCTION__);
306 
307     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
308     qemu_pf_conv_put(ssd->conv);
309     ssd->conv = NULL;
310 
311     qemu_mutex_lock(&ssd->lock);
312     if (ssd->update != NULL) {
313         qemu_spice_destroy_update(ssd, ssd->update);
314         ssd->update = NULL;
315     }
316     qemu_mutex_unlock(&ssd->lock);
317     qemu_spice_destroy_host_primary(ssd);
318     qemu_spice_create_host_primary(ssd);
319 
320     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
321     ssd->notify++;
322 }
323 
324 void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
325 {
326     if (ssd->cursor) {
327         ssd->ds->cursor_define(ssd->cursor);
328         cursor_put(ssd->cursor);
329         ssd->cursor = NULL;
330     }
331     if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
332         ssd->ds->mouse_set(ssd->mouse_x, ssd->mouse_y, 1);
333         ssd->mouse_x = -1;
334         ssd->mouse_y = -1;
335     }
336 }
337 
338 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
339 {
340     dprint(3, "%s:\n", __func__);
341     vga_hw_update();
342 
343     qemu_mutex_lock(&ssd->lock);
344     if (ssd->update == NULL) {
345         ssd->update = qemu_spice_create_update(ssd);
346         ssd->notify++;
347     }
348     qemu_spice_cursor_refresh_unlocked(ssd);
349     qemu_mutex_unlock(&ssd->lock);
350 
351     if (ssd->notify) {
352         ssd->notify = 0;
353         qemu_spice_wakeup(ssd);
354         dprint(2, "%s: notify\n", __FUNCTION__);
355     }
356 }
357 
358 /* spice display interface callbacks */
359 
360 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
361 {
362     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
363 
364     dprint(1, "%s:\n", __FUNCTION__);
365     ssd->worker = qxl_worker;
366 }
367 
368 static void interface_set_compression_level(QXLInstance *sin, int level)
369 {
370     dprint(1, "%s:\n", __FUNCTION__);
371     /* nothing to do */
372 }
373 
374 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
375 {
376     dprint(3, "%s:\n", __FUNCTION__);
377     /* nothing to do */
378 }
379 
380 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
381 {
382     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
383 
384     info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
385     info->memslot_id_bits  = MEMSLOT_SLOT_BITS;
386     info->num_memslots = NUM_MEMSLOTS;
387     info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
388     info->internal_groupslot_id = 0;
389     info->qxl_ram_size = ssd->bufsize;
390     info->n_surfaces = NUM_SURFACES;
391 }
392 
393 static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
394 {
395     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
396     SimpleSpiceUpdate *update;
397     int ret = false;
398 
399     dprint(3, "%s:\n", __FUNCTION__);
400 
401     qemu_mutex_lock(&ssd->lock);
402     if (ssd->update != NULL) {
403         update = ssd->update;
404         ssd->update = NULL;
405         *ext = update->ext;
406         ret = true;
407     }
408     qemu_mutex_unlock(&ssd->lock);
409 
410     return ret;
411 }
412 
413 static int interface_req_cmd_notification(QXLInstance *sin)
414 {
415     dprint(1, "%s:\n", __FUNCTION__);
416     return 1;
417 }
418 
419 static void interface_release_resource(QXLInstance *sin,
420                                        struct QXLReleaseInfoExt ext)
421 {
422     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
423     uintptr_t id;
424 
425     dprint(2, "%s:\n", __FUNCTION__);
426     id = ext.info->id;
427     qemu_spice_destroy_update(ssd, (void*)id);
428 }
429 
430 static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
431 {
432     dprint(3, "%s:\n", __FUNCTION__);
433     return false;
434 }
435 
436 static int interface_req_cursor_notification(QXLInstance *sin)
437 {
438     dprint(1, "%s:\n", __FUNCTION__);
439     return 1;
440 }
441 
442 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
443 {
444     fprintf(stderr, "%s: abort()\n", __FUNCTION__);
445     abort();
446 }
447 
448 static int interface_flush_resources(QXLInstance *sin)
449 {
450     fprintf(stderr, "%s: abort()\n", __FUNCTION__);
451     abort();
452     return 0;
453 }
454 
455 static const QXLInterface dpy_interface = {
456     .base.type               = SPICE_INTERFACE_QXL,
457     .base.description        = "qemu simple display",
458     .base.major_version      = SPICE_INTERFACE_QXL_MAJOR,
459     .base.minor_version      = SPICE_INTERFACE_QXL_MINOR,
460 
461     .attache_worker          = interface_attach_worker,
462     .set_compression_level   = interface_set_compression_level,
463     .set_mm_time             = interface_set_mm_time,
464     .get_init_info           = interface_get_init_info,
465 
466     /* the callbacks below are called from spice server thread context */
467     .get_command             = interface_get_command,
468     .req_cmd_notification    = interface_req_cmd_notification,
469     .release_resource        = interface_release_resource,
470     .get_cursor_command      = interface_get_cursor_command,
471     .req_cursor_notification = interface_req_cursor_notification,
472     .notify_update           = interface_notify_update,
473     .flush_resources         = interface_flush_resources,
474 };
475 
476 static SimpleSpiceDisplay sdpy;
477 
478 static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
479 {
480     qemu_spice_display_update(&sdpy, x, y, w, h);
481 }
482 
483 static void display_resize(struct DisplayState *ds)
484 {
485     qemu_spice_display_resize(&sdpy);
486 }
487 
488 static void display_refresh(struct DisplayState *ds)
489 {
490     qemu_spice_display_refresh(&sdpy);
491 }
492 
493 static DisplayChangeListener display_listener = {
494     .dpy_update  = display_update,
495     .dpy_resize  = display_resize,
496     .dpy_refresh = display_refresh,
497 };
498 
499 void qemu_spice_display_init(DisplayState *ds)
500 {
501     assert(sdpy.ds == NULL);
502     qemu_spice_display_init_common(&sdpy, ds);
503     register_displaychangelistener(ds, &display_listener);
504 
505     sdpy.qxl.base.sif = &dpy_interface.base;
506     qemu_spice_add_interface(&sdpy.qxl.base);
507     assert(sdpy.worker);
508 
509     qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler, &sdpy);
510     qemu_spice_create_host_memslot(&sdpy);
511     qemu_spice_create_host_primary(&sdpy);
512 }
513