xref: /openbmc/qemu/ui/spice-display.c (revision d8f8a860f2403533fc73f541122c65a34b21e42f)
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 #include "trace.h"
26 
27 #include "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 #if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
130 static void qemu_spice_start(SimpleSpiceDisplay *ssd)
131 {
132     trace_qemu_spice_start(ssd->qxl.id);
133     ssd->worker->start(ssd->worker);
134 }
135 
136 static void qemu_spice_stop(SimpleSpiceDisplay *ssd)
137 {
138     trace_qemu_spice_stop(ssd->qxl.id);
139     ssd->worker->stop(ssd->worker);
140 }
141 
142 #else
143 
144 static int spice_display_is_running;
145 
146 void qemu_spice_display_start(void)
147 {
148     spice_display_is_running = true;
149 }
150 
151 void qemu_spice_display_stop(void)
152 {
153     spice_display_is_running = false;
154 }
155 
156 #endif
157 
158 int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
159 {
160 #if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
161     return ssd->running;
162 #else
163     return spice_display_is_running;
164 #endif
165 }
166 
167 static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
168                                          QXLRect *rect)
169 {
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;
177 
178     trace_qemu_spice_create_update(
179            rect->left, rect->right,
180            rect->top, rect->bottom);
181 
182     update   = g_malloc0(sizeof(*update));
183     drawable = &update->drawable;
184     image    = &update->image;
185     cmd      = &update->ext.cmd;
186 
187     bw       = rect->right - rect->left;
188     bh       = rect->bottom - rect->top;
189     update->bitmap = g_malloc(bw * bh * 4);
190 
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;
203 
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;
208 
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;
218 
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;
232     }
233 
234     cmd->type = QXL_CMD_DRAW;
235     cmd->data = (uintptr_t)drawable;
236 
237     QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
238 }
239 
240 static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
241 {
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;
248 
249     if (qemu_spice_rect_is_empty(&ssd->dirty)) {
250         return;
251     };
252 
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);
257     }
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);
261     }
262 
263     for (blk = 0; blk < blocks; blk++) {
264         dirty_top[blk] = -1;
265     }
266 
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,
284                     };
285                     qemu_spice_create_one_update(ssd, &update);
286                     dirty_top[blk] = -1;
287                 }
288             } else {
289                 if (dirty_top[blk] == -1) {
290                     dirty_top[blk] = y;
291                 }
292             }
293         }
294     }
295 
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,
305             };
306             qemu_spice_create_one_update(ssd, &update);
307             dirty_top[blk] = -1;
308         }
309     }
310 
311     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
312 }
313 
314 /*
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).
319  */
320 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
321 {
322     g_free(update->bitmap);
323     g_free(update);
324 }
325 
326 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
327 {
328     QXLDevMemSlot memslot;
329 
330     dprint(1, "%s:\n", __FUNCTION__);
331 
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);
336 }
337 
338 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
339 {
340     QXLDevSurfaceCreate surface;
341 
342     memset(&surface, 0, sizeof(surface));
343 
344     dprint(1, "%s: %dx%d\n", __FUNCTION__,
345            ds_get_width(ssd->ds), ds_get_height(ssd->ds));
346 
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;
356 
357     qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
358 }
359 
360 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
361 {
362     dprint(1, "%s:\n", __FUNCTION__);
363 
364     qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
365 }
366 
367 void qemu_spice_vm_change_state_handler(void *opaque, int running,
368                                         RunState state)
369 {
370 #if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
371     SimpleSpiceDisplay *ssd = opaque;
372 
373     if (running) {
374         ssd->running = true;
375         qemu_spice_start(ssd);
376     } else {
377         qemu_spice_stop(ssd);
378         ssd->running = false;
379     }
380 #endif
381 }
382 
383 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd, DisplayState *ds)
384 {
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;
392     }
393     ssd->bufsize = (16 * 1024 * 1024);
394     ssd->buf = g_malloc(ssd->bufsize);
395 }
396 
397 /* display listener callbacks */
398 
399 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
400                                int x, int y, int w, int h)
401 {
402     QXLRect update_area;
403 
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;
409 
410     if (qemu_spice_rect_is_empty(&ssd->dirty)) {
411         ssd->notify++;
412     }
413     qemu_spice_rect_union(&ssd->dirty, &update_area);
414 }
415 
416 void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
417 {
418     SimpleSpiceUpdate *update;
419 
420     dprint(1, "%s:\n", __FUNCTION__);
421 
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;
427 
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);
432     }
433     qemu_mutex_unlock(&ssd->lock);
434     qemu_spice_destroy_host_primary(ssd);
435     qemu_spice_create_host_primary(ssd);
436 
437     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
438     ssd->notify++;
439 }
440 
441 void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
442 {
443     if (ssd->cursor) {
444         ssd->ds->cursor_define(ssd->cursor);
445         cursor_put(ssd->cursor);
446         ssd->cursor = NULL;
447     }
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;
452     }
453 }
454 
455 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
456 {
457     dprint(3, "%s:\n", __func__);
458     vga_hw_update();
459 
460     qemu_mutex_lock(&ssd->lock);
461     if (QTAILQ_EMPTY(&ssd->updates)) {
462         qemu_spice_create_update(ssd);
463         ssd->notify++;
464     }
465     qemu_spice_cursor_refresh_unlocked(ssd);
466     qemu_mutex_unlock(&ssd->lock);
467 
468     if (ssd->notify) {
469         ssd->notify = 0;
470         qemu_spice_wakeup(ssd);
471         dprint(2, "%s: notify\n", __FUNCTION__);
472     }
473 }
474 
475 /* spice display interface callbacks */
476 
477 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
478 {
479     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
480 
481     dprint(1, "%s:\n", __FUNCTION__);
482     ssd->worker = qxl_worker;
483 }
484 
485 static void interface_set_compression_level(QXLInstance *sin, int level)
486 {
487     dprint(1, "%s:\n", __FUNCTION__);
488     /* nothing to do */
489 }
490 
491 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
492 {
493     dprint(3, "%s:\n", __FUNCTION__);
494     /* nothing to do */
495 }
496 
497 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
498 {
499     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
500 
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;
508 }
509 
510 static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
511 {
512     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
513     SimpleSpiceUpdate *update;
514     int ret = false;
515 
516     dprint(3, "%s:\n", __FUNCTION__);
517 
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;
524     }
525     qemu_mutex_unlock(&ssd->lock);
526 
527     return ret;
528 }
529 
530 static int interface_req_cmd_notification(QXLInstance *sin)
531 {
532     dprint(1, "%s:\n", __FUNCTION__);
533     return 1;
534 }
535 
536 static void interface_release_resource(QXLInstance *sin,
537                                        struct QXLReleaseInfoExt ext)
538 {
539     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
540     uintptr_t id;
541 
542     dprint(2, "%s:\n", __FUNCTION__);
543     id = ext.info->id;
544     qemu_spice_destroy_update(ssd, (void*)id);
545 }
546 
547 static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
548 {
549     dprint(3, "%s:\n", __FUNCTION__);
550     return false;
551 }
552 
553 static int interface_req_cursor_notification(QXLInstance *sin)
554 {
555     dprint(1, "%s:\n", __FUNCTION__);
556     return 1;
557 }
558 
559 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
560 {
561     fprintf(stderr, "%s: abort()\n", __FUNCTION__);
562     abort();
563 }
564 
565 static int interface_flush_resources(QXLInstance *sin)
566 {
567     fprintf(stderr, "%s: abort()\n", __FUNCTION__);
568     abort();
569     return 0;
570 }
571 
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,
577 
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,
582 
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,
591 };
592 
593 static SimpleSpiceDisplay sdpy;
594 
595 static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
596 {
597     qemu_spice_display_update(&sdpy, x, y, w, h);
598 }
599 
600 static void display_resize(struct DisplayState *ds)
601 {
602     qemu_spice_display_resize(&sdpy);
603 }
604 
605 static void display_refresh(struct DisplayState *ds)
606 {
607     qemu_spice_display_refresh(&sdpy);
608 }
609 
610 static DisplayChangeListener display_listener = {
611     .dpy_update  = display_update,
612     .dpy_resize  = display_resize,
613     .dpy_refresh = display_refresh,
614 };
615 
616 void qemu_spice_display_init(DisplayState *ds)
617 {
618     assert(sdpy.ds == NULL);
619     qemu_spice_display_init_common(&sdpy, ds);
620     register_displaychangelistener(ds, &display_listener);
621 
622     sdpy.qxl.base.sif = &dpy_interface.base;
623     qemu_spice_add_interface(&sdpy.qxl.base);
624     assert(sdpy.worker);
625 
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);
629 }
630