xref: /openbmc/qemu/ui/spice-display.c (revision d7478d42)
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/osdep.h"
19 #include "ui/qemu-spice.h"
20 #include "qemu/timer.h"
21 #include "qemu/lockable.h"
22 #include "qemu/main-loop.h"
23 #include "qemu/option.h"
24 #include "qemu/queue.h"
25 #include "ui/console.h"
26 #include "trace.h"
27 
28 #include "ui/spice-display.h"
29 
30 bool spice_opengl;
31 
32 int qemu_spice_rect_is_empty(const QXLRect* r)
33 {
34     return r->top == r->bottom || r->left == r->right;
35 }
36 
37 void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
38 {
39     if (qemu_spice_rect_is_empty(r)) {
40         return;
41     }
42 
43     if (qemu_spice_rect_is_empty(dest)) {
44         *dest = *r;
45         return;
46     }
47 
48     dest->top = MIN(dest->top, r->top);
49     dest->left = MIN(dest->left, r->left);
50     dest->bottom = MAX(dest->bottom, r->bottom);
51     dest->right = MAX(dest->right, r->right);
52 }
53 
54 QXLCookie *qxl_cookie_new(int type, uint64_t io)
55 {
56     QXLCookie *cookie;
57 
58     cookie = g_malloc0(sizeof(*cookie));
59     cookie->type = type;
60     cookie->io = io;
61     return cookie;
62 }
63 
64 void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
65                             qxl_async_io async)
66 {
67     trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
68                                 memslot->virt_start, memslot->virt_end,
69                                 async);
70 
71     if (async != QXL_SYNC) {
72         spice_qxl_add_memslot_async(&ssd->qxl, memslot,
73                 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
74                                           QXL_IO_MEMSLOT_ADD_ASYNC));
75     } else {
76         spice_qxl_add_memslot(&ssd->qxl, memslot);
77     }
78 }
79 
80 void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
81 {
82     trace_qemu_spice_del_memslot(ssd->qxl.id, gid, sid);
83     spice_qxl_del_memslot(&ssd->qxl, gid, sid);
84 }
85 
86 void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
87                                        QXLDevSurfaceCreate *surface,
88                                        qxl_async_io async)
89 {
90     trace_qemu_spice_create_primary_surface(ssd->qxl.id, id, surface, async);
91     if (async != QXL_SYNC) {
92         spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface,
93                 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
94                                           QXL_IO_CREATE_PRIMARY_ASYNC));
95     } else {
96         spice_qxl_create_primary_surface(&ssd->qxl, id, surface);
97     }
98 }
99 
100 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
101                                         uint32_t id, qxl_async_io async)
102 {
103     trace_qemu_spice_destroy_primary_surface(ssd->qxl.id, id, async);
104     if (async != QXL_SYNC) {
105         spice_qxl_destroy_primary_surface_async(&ssd->qxl, id,
106                 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
107                                           QXL_IO_DESTROY_PRIMARY_ASYNC));
108     } else {
109         spice_qxl_destroy_primary_surface(&ssd->qxl, id);
110     }
111 }
112 
113 void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
114 {
115     trace_qemu_spice_wakeup(ssd->qxl.id);
116     spice_qxl_wakeup(&ssd->qxl);
117 }
118 
119 static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
120                                          QXLRect *rect)
121 {
122     SimpleSpiceUpdate *update;
123     QXLDrawable *drawable;
124     QXLImage *image;
125     QXLCommand *cmd;
126     int bw, bh;
127     struct timespec time_space;
128     pixman_image_t *dest;
129 
130     trace_qemu_spice_create_update(
131            rect->left, rect->right,
132            rect->top, rect->bottom);
133 
134     update   = g_malloc0(sizeof(*update));
135     drawable = &update->drawable;
136     image    = &update->image;
137     cmd      = &update->ext.cmd;
138 
139     bw       = rect->right - rect->left;
140     bh       = rect->bottom - rect->top;
141     update->bitmap = g_malloc(bw * bh * 4);
142 
143     drawable->bbox            = *rect;
144     drawable->clip.type       = SPICE_CLIP_TYPE_NONE;
145     drawable->effect          = QXL_EFFECT_OPAQUE;
146     drawable->release_info.id = (uintptr_t)(&update->ext);
147     drawable->type            = QXL_DRAW_COPY;
148     drawable->surfaces_dest[0] = -1;
149     drawable->surfaces_dest[1] = -1;
150     drawable->surfaces_dest[2] = -1;
151     clock_gettime(CLOCK_MONOTONIC, &time_space);
152     /* time in milliseconds from epoch. */
153     drawable->mm_time = time_space.tv_sec * 1000
154                       + time_space.tv_nsec / 1000 / 1000;
155 
156     drawable->u.copy.rop_descriptor  = SPICE_ROPD_OP_PUT;
157     drawable->u.copy.src_bitmap      = (uintptr_t)image;
158     drawable->u.copy.src_area.right  = bw;
159     drawable->u.copy.src_area.bottom = bh;
160 
161     QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
162     image->descriptor.type   = SPICE_IMAGE_TYPE_BITMAP;
163     image->bitmap.flags      = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
164     image->bitmap.stride     = bw * 4;
165     image->descriptor.width  = image->bitmap.x = bw;
166     image->descriptor.height = image->bitmap.y = bh;
167     image->bitmap.data = (uintptr_t)(update->bitmap);
168     image->bitmap.palette = 0;
169     image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
170 
171     dest = pixman_image_create_bits(PIXMAN_LE_x8r8g8b8, bw, bh,
172                                     (void *)update->bitmap, bw * 4);
173     pixman_image_composite(PIXMAN_OP_SRC, ssd->surface, NULL, ssd->mirror,
174                            rect->left, rect->top, 0, 0,
175                            rect->left, rect->top, bw, bh);
176     pixman_image_composite(PIXMAN_OP_SRC, ssd->mirror, NULL, dest,
177                            rect->left, rect->top, 0, 0,
178                            0, 0, bw, bh);
179     pixman_image_unref(dest);
180 
181     cmd->type = QXL_CMD_DRAW;
182     cmd->data = (uintptr_t)drawable;
183 
184     QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
185 }
186 
187 static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
188 {
189     static const int blksize = 32;
190     int blocks = DIV_ROUND_UP(surface_width(ssd->ds), blksize);
191     int dirty_top[blocks];
192     int y, yoff1, yoff2, x, xoff, blk, bw;
193     int bpp = surface_bytes_per_pixel(ssd->ds);
194     uint8_t *guest, *mirror;
195 
196     if (qemu_spice_rect_is_empty(&ssd->dirty)) {
197         return;
198     };
199 
200     for (blk = 0; blk < blocks; blk++) {
201         dirty_top[blk] = -1;
202     }
203 
204     guest = surface_data(ssd->ds);
205     mirror = (void *)pixman_image_get_data(ssd->mirror);
206     for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
207         yoff1 = y * surface_stride(ssd->ds);
208         yoff2 = y * pixman_image_get_stride(ssd->mirror);
209         for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
210             xoff = x * bpp;
211             blk = x / blksize;
212             bw = MIN(blksize, ssd->dirty.right - x);
213             if (memcmp(guest + yoff1 + xoff,
214                        mirror + yoff2 + xoff,
215                        bw * bpp) == 0) {
216                 if (dirty_top[blk] != -1) {
217                     QXLRect update = {
218                         .top    = dirty_top[blk],
219                         .bottom = y,
220                         .left   = x,
221                         .right  = x + bw,
222                     };
223                     qemu_spice_create_one_update(ssd, &update);
224                     dirty_top[blk] = -1;
225                 }
226             } else {
227                 if (dirty_top[blk] == -1) {
228                     dirty_top[blk] = y;
229                 }
230             }
231         }
232     }
233 
234     for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
235         blk = x / blksize;
236         bw = MIN(blksize, ssd->dirty.right - x);
237         if (dirty_top[blk] != -1) {
238             QXLRect update = {
239                 .top    = dirty_top[blk],
240                 .bottom = ssd->dirty.bottom,
241                 .left   = x,
242                 .right  = x + bw,
243             };
244             qemu_spice_create_one_update(ssd, &update);
245             dirty_top[blk] = -1;
246         }
247     }
248 
249     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
250 }
251 
252 static SimpleSpiceCursor*
253 qemu_spice_create_cursor_update(SimpleSpiceDisplay *ssd,
254                                 QEMUCursor *c,
255                                 int on)
256 {
257     size_t size = c ? c->width * c->height * 4 : 0;
258     SimpleSpiceCursor *update;
259     QXLCursorCmd *ccmd;
260     QXLCursor *cursor;
261     QXLCommand *cmd;
262 
263     update   = g_malloc0(sizeof(*update) + size);
264     ccmd     = &update->cmd;
265     cursor   = &update->cursor;
266     cmd      = &update->ext.cmd;
267 
268     if (c) {
269         ccmd->type = QXL_CURSOR_SET;
270         ccmd->u.set.position.x = ssd->ptr_x + ssd->hot_x;
271         ccmd->u.set.position.y = ssd->ptr_y + ssd->hot_y;
272         ccmd->u.set.visible    = true;
273         ccmd->u.set.shape      = (uintptr_t)cursor;
274         cursor->header.unique     = ssd->unique++;
275         cursor->header.type       = SPICE_CURSOR_TYPE_ALPHA;
276         cursor->header.width      = c->width;
277         cursor->header.height     = c->height;
278         cursor->header.hot_spot_x = c->hot_x;
279         cursor->header.hot_spot_y = c->hot_y;
280         cursor->data_size         = size;
281         cursor->chunk.data_size   = size;
282         memcpy(cursor->chunk.data, c->data, size);
283     } else if (!on) {
284         ccmd->type = QXL_CURSOR_HIDE;
285     } else {
286         ccmd->type = QXL_CURSOR_MOVE;
287         ccmd->u.position.x = ssd->ptr_x + ssd->hot_x;
288         ccmd->u.position.y = ssd->ptr_y + ssd->hot_y;
289     }
290     ccmd->release_info.id = (uintptr_t)(&update->ext);
291 
292     cmd->type = QXL_CMD_CURSOR;
293     cmd->data = (uintptr_t)ccmd;
294 
295     return update;
296 }
297 
298 /*
299  * Called from spice server thread context (via interface_release_resource)
300  * We do *not* hold the global qemu mutex here, so extra care is needed
301  * when calling qemu functions.  QEMU interfaces used:
302  *    - g_free (underlying glibc free is re-entrant).
303  */
304 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
305 {
306     g_free(update->bitmap);
307     g_free(update);
308 }
309 
310 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
311 {
312     QXLDevMemSlot memslot;
313 
314     memset(&memslot, 0, sizeof(memslot));
315     memslot.slot_group_id = MEMSLOT_GROUP_HOST;
316     memslot.virt_end = ~0;
317     qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
318 }
319 
320 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
321 {
322     QXLDevSurfaceCreate surface;
323     uint64_t surface_size;
324 
325     memset(&surface, 0, sizeof(surface));
326 
327     surface_size = (uint64_t) surface_width(ssd->ds) *
328         surface_height(ssd->ds) * 4;
329     assert(surface_size > 0);
330     assert(surface_size < INT_MAX);
331     if (ssd->bufsize < surface_size) {
332         ssd->bufsize = surface_size;
333         g_free(ssd->buf);
334         ssd->buf = g_malloc(ssd->bufsize);
335     }
336 
337     surface.format     = SPICE_SURFACE_FMT_32_xRGB;
338     surface.width      = surface_width(ssd->ds);
339     surface.height     = surface_height(ssd->ds);
340     surface.stride     = -surface.width * 4;
341     surface.mouse_mode = true;
342     surface.flags      = 0;
343     surface.type       = 0;
344     surface.mem        = (uintptr_t)ssd->buf;
345     surface.group_id   = MEMSLOT_GROUP_HOST;
346 
347     qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
348 }
349 
350 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
351 {
352     qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
353 }
354 
355 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
356 {
357     qemu_mutex_init(&ssd->lock);
358     QTAILQ_INIT(&ssd->updates);
359     ssd->mouse_x = -1;
360     ssd->mouse_y = -1;
361     if (ssd->num_surfaces == 0) {
362         ssd->num_surfaces = 1024;
363     }
364 }
365 
366 /* display listener callbacks */
367 
368 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
369                                int x, int y, int w, int h)
370 {
371     QXLRect update_area;
372 
373     trace_qemu_spice_display_update(ssd->qxl.id, x, y, w, h);
374     update_area.left = x,
375     update_area.right = x + w;
376     update_area.top = y;
377     update_area.bottom = y + h;
378 
379     if (qemu_spice_rect_is_empty(&ssd->dirty)) {
380         ssd->notify++;
381     }
382     qemu_spice_rect_union(&ssd->dirty, &update_area);
383 }
384 
385 void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
386                                DisplaySurface *surface)
387 {
388     SimpleSpiceUpdate *update;
389     bool need_destroy;
390 
391     if (ssd->surface &&
392         surface_width(surface) == pixman_image_get_width(ssd->surface) &&
393         surface_height(surface) == pixman_image_get_height(ssd->surface) &&
394         surface_format(surface) == pixman_image_get_format(ssd->surface)) {
395         /* no-resize fast path: just swap backing store */
396         trace_qemu_spice_display_surface(ssd->qxl.id,
397                                          surface_width(surface),
398                                          surface_height(surface),
399                                          true);
400         qemu_mutex_lock(&ssd->lock);
401         ssd->ds = surface;
402         pixman_image_unref(ssd->surface);
403         ssd->surface = pixman_image_ref(ssd->ds->image);
404         qemu_mutex_unlock(&ssd->lock);
405         qemu_spice_display_update(ssd, 0, 0,
406                                   surface_width(surface),
407                                   surface_height(surface));
408         return;
409     }
410 
411     /* full mode switch */
412     trace_qemu_spice_display_surface(ssd->qxl.id,
413                                      surface_width(surface),
414                                      surface_height(surface),
415                                      false);
416 
417     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
418     if (ssd->surface) {
419         pixman_image_unref(ssd->surface);
420         ssd->surface = NULL;
421         pixman_image_unref(ssd->mirror);
422         ssd->mirror = NULL;
423     }
424 
425     qemu_mutex_lock(&ssd->lock);
426     need_destroy = (ssd->ds != NULL);
427     ssd->ds = surface;
428     while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
429         QTAILQ_REMOVE(&ssd->updates, update, next);
430         qemu_spice_destroy_update(ssd, update);
431     }
432     qemu_mutex_unlock(&ssd->lock);
433     if (need_destroy) {
434         qemu_spice_destroy_host_primary(ssd);
435     }
436     if (ssd->ds) {
437         ssd->surface = pixman_image_ref(ssd->ds->image);
438         ssd->mirror  = qemu_pixman_mirror_create(ssd->ds->format,
439                                                  ssd->ds->image);
440         qemu_spice_create_host_primary(ssd);
441     }
442 
443     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
444     ssd->notify++;
445 
446     qemu_mutex_lock(&ssd->lock);
447     if (ssd->cursor) {
448         g_free(ssd->ptr_define);
449         ssd->ptr_define = qemu_spice_create_cursor_update(ssd, ssd->cursor, 0);
450     }
451     qemu_mutex_unlock(&ssd->lock);
452 }
453 
454 void qemu_spice_cursor_refresh_bh(void *opaque)
455 {
456     SimpleSpiceDisplay *ssd = opaque;
457 
458     qemu_mutex_lock(&ssd->lock);
459     if (ssd->cursor) {
460         QEMUCursor *c = ssd->cursor;
461         assert(ssd->dcl.con);
462         cursor_get(c);
463         qemu_mutex_unlock(&ssd->lock);
464         dpy_cursor_define(ssd->dcl.con, c);
465         qemu_mutex_lock(&ssd->lock);
466         cursor_put(c);
467     }
468 
469     if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
470         int x, y;
471         assert(ssd->dcl.con);
472         x = ssd->mouse_x;
473         y = ssd->mouse_y;
474         ssd->mouse_x = -1;
475         ssd->mouse_y = -1;
476         qemu_mutex_unlock(&ssd->lock);
477         dpy_mouse_set(ssd->dcl.con, x, y, 1);
478     } else {
479         qemu_mutex_unlock(&ssd->lock);
480     }
481 }
482 
483 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
484 {
485     graphic_hw_update(ssd->dcl.con);
486 
487     WITH_QEMU_LOCK_GUARD(&ssd->lock) {
488         if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
489             qemu_spice_create_update(ssd);
490             ssd->notify++;
491         }
492     }
493 
494     trace_qemu_spice_display_refresh(ssd->qxl.id, ssd->notify);
495     if (ssd->notify) {
496         ssd->notify = 0;
497         qemu_spice_wakeup(ssd);
498     }
499 }
500 
501 /* spice display interface callbacks */
502 
503 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
504 {
505     /* nothing to do */
506 }
507 
508 static void interface_set_compression_level(QXLInstance *sin, int level)
509 {
510     /* nothing to do */
511 }
512 
513 #if SPICE_NEEDS_SET_MM_TIME
514 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
515 {
516     /* nothing to do */
517 }
518 #endif
519 
520 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
521 {
522     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
523 
524     info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
525     info->memslot_id_bits  = MEMSLOT_SLOT_BITS;
526     info->num_memslots = NUM_MEMSLOTS;
527     info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
528     info->internal_groupslot_id = 0;
529     info->qxl_ram_size = 16 * 1024 * 1024;
530     info->n_surfaces = ssd->num_surfaces;
531 }
532 
533 static int interface_get_command(QXLInstance *sin, QXLCommandExt *ext)
534 {
535     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
536     SimpleSpiceUpdate *update;
537     int ret = false;
538 
539     qemu_mutex_lock(&ssd->lock);
540     update = QTAILQ_FIRST(&ssd->updates);
541     if (update != NULL) {
542         QTAILQ_REMOVE(&ssd->updates, update, next);
543         *ext = update->ext;
544         ret = true;
545     }
546     qemu_mutex_unlock(&ssd->lock);
547 
548     return ret;
549 }
550 
551 static int interface_req_cmd_notification(QXLInstance *sin)
552 {
553     return 1;
554 }
555 
556 static void interface_release_resource(QXLInstance *sin,
557                                        QXLReleaseInfoExt rext)
558 {
559     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
560     SimpleSpiceUpdate *update;
561     SimpleSpiceCursor *cursor;
562     QXLCommandExt *ext;
563 
564     if (!rext.info) {
565         return;
566     }
567 
568     ext = (void *)(intptr_t)(rext.info->id);
569     switch (ext->cmd.type) {
570     case QXL_CMD_DRAW:
571         update = container_of(ext, SimpleSpiceUpdate, ext);
572         qemu_spice_destroy_update(ssd, update);
573         break;
574     case QXL_CMD_CURSOR:
575         cursor = container_of(ext, SimpleSpiceCursor, ext);
576         g_free(cursor);
577         break;
578     default:
579         g_assert_not_reached();
580     }
581 }
582 
583 static int interface_get_cursor_command(QXLInstance *sin, QXLCommandExt *ext)
584 {
585     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
586     int ret;
587 
588     QEMU_LOCK_GUARD(&ssd->lock);
589     if (ssd->ptr_define) {
590         *ext = ssd->ptr_define->ext;
591         ssd->ptr_define = NULL;
592         ret = true;
593     } else if (ssd->ptr_move) {
594         *ext = ssd->ptr_move->ext;
595         ssd->ptr_move = NULL;
596         ret = true;
597     } else {
598         ret = false;
599     }
600     return ret;
601 }
602 
603 static int interface_req_cursor_notification(QXLInstance *sin)
604 {
605     return 1;
606 }
607 
608 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
609 {
610     fprintf(stderr, "%s: abort()\n", __func__);
611     abort();
612 }
613 
614 static int interface_flush_resources(QXLInstance *sin)
615 {
616     fprintf(stderr, "%s: abort()\n", __func__);
617     abort();
618     return 0;
619 }
620 
621 static void interface_update_area_complete(QXLInstance *sin,
622         uint32_t surface_id,
623         QXLRect *dirty, uint32_t num_updated_rects)
624 {
625     /* should never be called, used in qxl native mode only */
626     fprintf(stderr, "%s: abort()\n", __func__);
627     abort();
628 }
629 
630 /* called from spice server thread context only */
631 static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
632 {
633     QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
634 
635     switch (cookie->type) {
636 #ifdef HAVE_SPICE_GL
637     case QXL_COOKIE_TYPE_GL_DRAW_DONE:
638     {
639         SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
640         qemu_bh_schedule(ssd->gl_unblock_bh);
641         break;
642     }
643     case QXL_COOKIE_TYPE_IO:
644         if (cookie->io == QXL_IO_MONITORS_CONFIG_ASYNC) {
645             g_free(cookie->u.data);
646         }
647         break;
648 #endif
649     default:
650         /* should never be called, used in qxl native mode only */
651         fprintf(stderr, "%s: abort()\n", __func__);
652         abort();
653     }
654     g_free(cookie);
655 }
656 
657 static void interface_set_client_capabilities(QXLInstance *sin,
658                                               uint8_t client_present,
659                                               uint8_t caps[58])
660 {
661     /* nothing to do */
662 }
663 
664 static int interface_client_monitors_config(QXLInstance *sin,
665                                             VDAgentMonitorsConfig *mc)
666 {
667     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
668     QemuUIInfo info;
669     int head;
670 
671     if (!dpy_ui_info_supported(ssd->dcl.con)) {
672         return 0; /* == not supported by guest */
673     }
674 
675     if (!mc) {
676         return 1;
677     }
678 
679     info = *dpy_get_ui_info(ssd->dcl.con);
680 
681     head = qemu_console_get_index(ssd->dcl.con);
682     if (mc->num_of_monitors > head) {
683         info.width  = mc->monitors[head].width;
684         info.height = mc->monitors[head].height;
685 #if SPICE_SERVER_VERSION >= 0x000e04 /* release 0.14.4 */
686         if (mc->flags & VD_AGENT_CONFIG_MONITORS_FLAG_PHYSICAL_SIZE) {
687             VDAgentMonitorMM *mm = (void *)&mc->monitors[mc->num_of_monitors];
688             info.width_mm = mm[head].width;
689             info.height_mm = mm[head].height;
690         }
691 #endif
692     }
693 
694     trace_qemu_spice_ui_info(ssd->qxl.id, info.width, info.height);
695     dpy_set_ui_info(ssd->dcl.con, &info, false);
696     return 1;
697 }
698 
699 static const QXLInterface dpy_interface = {
700     .base.type               = SPICE_INTERFACE_QXL,
701     .base.description        = "qemu simple display",
702     .base.major_version      = SPICE_INTERFACE_QXL_MAJOR,
703     .base.minor_version      = SPICE_INTERFACE_QXL_MINOR,
704 
705     .attache_worker          = interface_attach_worker,
706     .set_compression_level   = interface_set_compression_level,
707 #if SPICE_NEEDS_SET_MM_TIME
708     .set_mm_time             = interface_set_mm_time,
709 #endif
710     .get_init_info           = interface_get_init_info,
711 
712     /* the callbacks below are called from spice server thread context */
713     .get_command             = interface_get_command,
714     .req_cmd_notification    = interface_req_cmd_notification,
715     .release_resource        = interface_release_resource,
716     .get_cursor_command      = interface_get_cursor_command,
717     .req_cursor_notification = interface_req_cursor_notification,
718     .notify_update           = interface_notify_update,
719     .flush_resources         = interface_flush_resources,
720     .async_complete          = interface_async_complete,
721     .update_area_complete    = interface_update_area_complete,
722     .set_client_capabilities = interface_set_client_capabilities,
723     .client_monitors_config  = interface_client_monitors_config,
724 };
725 
726 static void display_update(DisplayChangeListener *dcl,
727                            int x, int y, int w, int h)
728 {
729     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
730     qemu_spice_display_update(ssd, x, y, w, h);
731 }
732 
733 static void display_switch(DisplayChangeListener *dcl,
734                            DisplaySurface *surface)
735 {
736     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
737     qemu_spice_display_switch(ssd, surface);
738 }
739 
740 static void display_refresh(DisplayChangeListener *dcl)
741 {
742     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
743     qemu_spice_display_refresh(ssd);
744 }
745 
746 static void display_mouse_set(DisplayChangeListener *dcl,
747                               int x, int y, int on)
748 {
749     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
750 
751     qemu_mutex_lock(&ssd->lock);
752     ssd->ptr_x = x;
753     ssd->ptr_y = y;
754     g_free(ssd->ptr_move);
755     ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL, on);
756     qemu_mutex_unlock(&ssd->lock);
757     qemu_spice_wakeup(ssd);
758 }
759 
760 static void display_mouse_define(DisplayChangeListener *dcl,
761                                  QEMUCursor *c)
762 {
763     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
764 
765     qemu_mutex_lock(&ssd->lock);
766     cursor_get(c);
767     cursor_put(ssd->cursor);
768     ssd->cursor = c;
769     ssd->hot_x = c->hot_x;
770     ssd->hot_y = c->hot_y;
771     g_free(ssd->ptr_move);
772     ssd->ptr_move = NULL;
773     g_free(ssd->ptr_define);
774     ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c, 0);
775     qemu_mutex_unlock(&ssd->lock);
776     qemu_spice_wakeup(ssd);
777 }
778 
779 static const DisplayChangeListenerOps display_listener_ops = {
780     .dpy_name             = "spice",
781     .dpy_gfx_update       = display_update,
782     .dpy_gfx_switch       = display_switch,
783     .dpy_gfx_check_format = qemu_pixman_check_format,
784     .dpy_refresh          = display_refresh,
785     .dpy_mouse_set        = display_mouse_set,
786     .dpy_cursor_define    = display_mouse_define,
787 };
788 
789 #ifdef HAVE_SPICE_GL
790 
791 static void qemu_spice_gl_monitor_config(SimpleSpiceDisplay *ssd,
792                                          int x, int y, int w, int h)
793 {
794     QXLMonitorsConfig *config;
795     QXLCookie *cookie;
796 
797     config = g_malloc0(sizeof(QXLMonitorsConfig) + sizeof(QXLHead));
798     config->count = 1;
799     config->max_allowed = 1;
800     config->heads[0].x = x;
801     config->heads[0].y = y;
802     config->heads[0].width = w;
803     config->heads[0].height = h;
804     cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
805                             QXL_IO_MONITORS_CONFIG_ASYNC);
806     cookie->u.data = config;
807 
808     spice_qxl_monitors_config_async(&ssd->qxl,
809                                     (uintptr_t)config,
810                                     MEMSLOT_GROUP_HOST,
811                                     (uintptr_t)cookie);
812 }
813 
814 static void qemu_spice_gl_block(SimpleSpiceDisplay *ssd, bool block)
815 {
816     uint64_t timeout;
817 
818     if (block) {
819         timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
820         timeout += 1000; /* one sec */
821         timer_mod(ssd->gl_unblock_timer, timeout);
822     } else {
823         timer_del(ssd->gl_unblock_timer);
824     }
825     graphic_hw_gl_block(ssd->dcl.con, block);
826 }
827 
828 static void qemu_spice_gl_unblock_bh(void *opaque)
829 {
830     SimpleSpiceDisplay *ssd = opaque;
831 
832     qemu_spice_gl_block(ssd, false);
833 }
834 
835 static void qemu_spice_gl_block_timer(void *opaque)
836 {
837     warn_report("spice: no gl-draw-done within one second");
838 }
839 
840 static void spice_gl_refresh(DisplayChangeListener *dcl)
841 {
842     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
843     uint64_t cookie;
844 
845     if (!ssd->ds || qemu_console_is_gl_blocked(ssd->dcl.con)) {
846         return;
847     }
848 
849     graphic_hw_update(dcl->con);
850     if (ssd->gl_updates && ssd->have_surface) {
851         qemu_spice_gl_block(ssd, true);
852         glFlush();
853         cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
854         spice_qxl_gl_draw_async(&ssd->qxl, 0, 0,
855                                 surface_width(ssd->ds),
856                                 surface_height(ssd->ds),
857                                 cookie);
858         ssd->gl_updates = 0;
859     }
860 }
861 
862 static void spice_gl_update(DisplayChangeListener *dcl,
863                             int x, int y, int w, int h)
864 {
865     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
866 
867     surface_gl_update_texture(ssd->gls, ssd->ds, x, y, w, h);
868     ssd->gl_updates++;
869 }
870 
871 static void spice_gl_switch(DisplayChangeListener *dcl,
872                             struct DisplaySurface *new_surface)
873 {
874     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
875     EGLint stride, fourcc;
876     int fd;
877 
878     if (ssd->ds) {
879         surface_gl_destroy_texture(ssd->gls, ssd->ds);
880     }
881     ssd->ds = new_surface;
882     if (ssd->ds) {
883         surface_gl_create_texture(ssd->gls, ssd->ds);
884         fd = egl_get_fd_for_texture(ssd->ds->texture,
885                                     &stride, &fourcc,
886                                     NULL);
887         if (fd < 0) {
888             surface_gl_destroy_texture(ssd->gls, ssd->ds);
889             return;
890         }
891 
892         trace_qemu_spice_gl_surface(ssd->qxl.id,
893                                     surface_width(ssd->ds),
894                                     surface_height(ssd->ds),
895                                     fourcc);
896 
897         /* note: spice server will close the fd */
898         spice_qxl_gl_scanout(&ssd->qxl, fd,
899                              surface_width(ssd->ds),
900                              surface_height(ssd->ds),
901                              stride, fourcc, false);
902         ssd->have_surface = true;
903         ssd->have_scanout = false;
904 
905         qemu_spice_gl_monitor_config(ssd, 0, 0,
906                                      surface_width(ssd->ds),
907                                      surface_height(ssd->ds));
908     }
909 }
910 
911 static QEMUGLContext qemu_spice_gl_create_context(DisplayGLCtx *dgc,
912                                                   QEMUGLParams *params)
913 {
914     eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
915                    qemu_egl_rn_ctx);
916     return qemu_egl_create_context(dgc, params);
917 }
918 
919 static void qemu_spice_gl_scanout_disable(DisplayChangeListener *dcl)
920 {
921     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
922 
923     trace_qemu_spice_gl_scanout_disable(ssd->qxl.id);
924     spice_qxl_gl_scanout(&ssd->qxl, -1, 0, 0, 0, 0, false);
925     qemu_spice_gl_monitor_config(ssd, 0, 0, 0, 0);
926     ssd->have_surface = false;
927     ssd->have_scanout = false;
928 }
929 
930 static void qemu_spice_gl_scanout_texture(DisplayChangeListener *dcl,
931                                           uint32_t tex_id,
932                                           bool y_0_top,
933                                           uint32_t backing_width,
934                                           uint32_t backing_height,
935                                           uint32_t x, uint32_t y,
936                                           uint32_t w, uint32_t h)
937 {
938     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
939     EGLint stride = 0, fourcc = 0;
940     int fd = -1;
941 
942     assert(tex_id);
943     fd = egl_get_fd_for_texture(tex_id, &stride, &fourcc, NULL);
944     if (fd < 0) {
945         fprintf(stderr, "%s: failed to get fd for texture\n", __func__);
946         return;
947     }
948     trace_qemu_spice_gl_scanout_texture(ssd->qxl.id, w, h, fourcc);
949 
950     /* note: spice server will close the fd */
951     spice_qxl_gl_scanout(&ssd->qxl, fd, backing_width, backing_height,
952                          stride, fourcc, y_0_top);
953     qemu_spice_gl_monitor_config(ssd, x, y, w, h);
954     ssd->have_surface = false;
955     ssd->have_scanout = true;
956 }
957 
958 static void qemu_spice_gl_scanout_dmabuf(DisplayChangeListener *dcl,
959                                          QemuDmaBuf *dmabuf)
960 {
961     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
962 
963     ssd->guest_dmabuf = dmabuf;
964     ssd->guest_dmabuf_refresh = true;
965 
966     ssd->have_surface = false;
967     ssd->have_scanout = true;
968 }
969 
970 static void qemu_spice_gl_cursor_dmabuf(DisplayChangeListener *dcl,
971                                         QemuDmaBuf *dmabuf, bool have_hot,
972                                         uint32_t hot_x, uint32_t hot_y)
973 {
974     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
975 
976     ssd->have_hot = have_hot;
977     ssd->hot_x = hot_x;
978     ssd->hot_y = hot_y;
979 
980     trace_qemu_spice_gl_cursor(ssd->qxl.id, dmabuf != NULL, have_hot);
981     if (dmabuf) {
982         egl_dmabuf_import_texture(dmabuf);
983         if (!dmabuf->texture) {
984             return;
985         }
986         egl_fb_setup_for_tex(&ssd->cursor_fb, dmabuf->width, dmabuf->height,
987                              dmabuf->texture, false);
988     } else {
989         egl_fb_destroy(&ssd->cursor_fb);
990     }
991 }
992 
993 static void qemu_spice_gl_cursor_position(DisplayChangeListener *dcl,
994                                           uint32_t pos_x, uint32_t pos_y)
995 {
996     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
997 
998     qemu_mutex_lock(&ssd->lock);
999     ssd->ptr_x = pos_x;
1000     ssd->ptr_y = pos_y;
1001     qemu_mutex_unlock(&ssd->lock);
1002 }
1003 
1004 static void qemu_spice_gl_release_dmabuf(DisplayChangeListener *dcl,
1005                                          QemuDmaBuf *dmabuf)
1006 {
1007     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
1008 
1009     if (ssd->guest_dmabuf == dmabuf) {
1010         ssd->guest_dmabuf = NULL;
1011         ssd->guest_dmabuf_refresh = false;
1012     }
1013     egl_dmabuf_release_texture(dmabuf);
1014 }
1015 
1016 static void qemu_spice_gl_update(DisplayChangeListener *dcl,
1017                                  uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1018 {
1019     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
1020     EGLint stride = 0, fourcc = 0;
1021     bool render_cursor = false;
1022     bool y_0_top = false; /* FIXME */
1023     uint64_t cookie;
1024     int fd;
1025 
1026     if (!ssd->have_scanout) {
1027         return;
1028     }
1029 
1030     if (ssd->cursor_fb.texture) {
1031         render_cursor = true;
1032     }
1033     if (ssd->render_cursor != render_cursor) {
1034         ssd->render_cursor = render_cursor;
1035         ssd->guest_dmabuf_refresh = true;
1036         egl_fb_destroy(&ssd->blit_fb);
1037     }
1038 
1039     if (ssd->guest_dmabuf_refresh) {
1040         QemuDmaBuf *dmabuf = ssd->guest_dmabuf;
1041         if (render_cursor) {
1042             egl_dmabuf_import_texture(dmabuf);
1043             if (!dmabuf->texture) {
1044                 return;
1045             }
1046 
1047             /* source framebuffer */
1048             egl_fb_setup_for_tex(&ssd->guest_fb,
1049                                  dmabuf->width, dmabuf->height,
1050                                  dmabuf->texture, false);
1051 
1052             /* dest framebuffer */
1053             if (ssd->blit_fb.width  != dmabuf->width ||
1054                 ssd->blit_fb.height != dmabuf->height) {
1055                 trace_qemu_spice_gl_render_dmabuf(ssd->qxl.id, dmabuf->width,
1056                                                   dmabuf->height);
1057                 egl_fb_destroy(&ssd->blit_fb);
1058                 egl_fb_setup_new_tex(&ssd->blit_fb,
1059                                      dmabuf->width, dmabuf->height);
1060                 fd = egl_get_fd_for_texture(ssd->blit_fb.texture,
1061                                             &stride, &fourcc, NULL);
1062                 spice_qxl_gl_scanout(&ssd->qxl, fd,
1063                                      dmabuf->width, dmabuf->height,
1064                                      stride, fourcc, false);
1065             }
1066         } else {
1067             trace_qemu_spice_gl_forward_dmabuf(ssd->qxl.id,
1068                                                dmabuf->width, dmabuf->height);
1069             /* note: spice server will close the fd, so hand over a dup */
1070             spice_qxl_gl_scanout(&ssd->qxl, dup(dmabuf->fd),
1071                                  dmabuf->width, dmabuf->height,
1072                                  dmabuf->stride, dmabuf->fourcc,
1073                                  dmabuf->y0_top);
1074         }
1075         qemu_spice_gl_monitor_config(ssd, 0, 0, dmabuf->width, dmabuf->height);
1076         ssd->guest_dmabuf_refresh = false;
1077     }
1078 
1079     if (render_cursor) {
1080         int x, y;
1081         qemu_mutex_lock(&ssd->lock);
1082         x = ssd->ptr_x;
1083         y = ssd->ptr_y;
1084         qemu_mutex_unlock(&ssd->lock);
1085         egl_texture_blit(ssd->gls, &ssd->blit_fb, &ssd->guest_fb,
1086                          !y_0_top);
1087         egl_texture_blend(ssd->gls, &ssd->blit_fb, &ssd->cursor_fb,
1088                           !y_0_top, x, y, 1.0, 1.0);
1089         glFlush();
1090     }
1091 
1092     trace_qemu_spice_gl_update(ssd->qxl.id, w, h, x, y);
1093     qemu_spice_gl_block(ssd, true);
1094     glFlush();
1095     cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
1096     spice_qxl_gl_draw_async(&ssd->qxl, x, y, w, h, cookie);
1097 }
1098 
1099 static const DisplayChangeListenerOps display_listener_gl_ops = {
1100     .dpy_name                = "spice-egl",
1101     .dpy_gfx_update          = spice_gl_update,
1102     .dpy_gfx_switch          = spice_gl_switch,
1103     .dpy_gfx_check_format    = console_gl_check_format,
1104     .dpy_refresh             = spice_gl_refresh,
1105     .dpy_mouse_set           = display_mouse_set,
1106     .dpy_cursor_define       = display_mouse_define,
1107 
1108     .dpy_gl_scanout_disable  = qemu_spice_gl_scanout_disable,
1109     .dpy_gl_scanout_texture  = qemu_spice_gl_scanout_texture,
1110     .dpy_gl_scanout_dmabuf   = qemu_spice_gl_scanout_dmabuf,
1111     .dpy_gl_cursor_dmabuf    = qemu_spice_gl_cursor_dmabuf,
1112     .dpy_gl_cursor_position  = qemu_spice_gl_cursor_position,
1113     .dpy_gl_release_dmabuf   = qemu_spice_gl_release_dmabuf,
1114     .dpy_gl_update           = qemu_spice_gl_update,
1115 };
1116 
1117 static const DisplayGLCtxOps gl_ctx_ops = {
1118     .compatible_dcl          = &display_listener_gl_ops,
1119     .dpy_gl_ctx_create       = qemu_spice_gl_create_context,
1120     .dpy_gl_ctx_destroy      = qemu_egl_destroy_context,
1121     .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
1122 };
1123 
1124 #endif /* HAVE_SPICE_GL */
1125 
1126 static void qemu_spice_display_init_one(QemuConsole *con)
1127 {
1128     SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
1129 
1130     qemu_spice_display_init_common(ssd);
1131 
1132     ssd->dcl.ops = &display_listener_ops;
1133 #ifdef HAVE_SPICE_GL
1134     if (spice_opengl) {
1135         ssd->dcl.ops = &display_listener_gl_ops;
1136         ssd->dgc.ops = &gl_ctx_ops;
1137         ssd->gl_unblock_bh = qemu_bh_new(qemu_spice_gl_unblock_bh, ssd);
1138         ssd->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1139                                              qemu_spice_gl_block_timer, ssd);
1140         ssd->gls = qemu_gl_init_shader();
1141         ssd->have_surface = false;
1142         ssd->have_scanout = false;
1143     }
1144 #endif
1145     ssd->dcl.con = con;
1146 
1147     ssd->qxl.base.sif = &dpy_interface.base;
1148     qemu_spice_add_display_interface(&ssd->qxl, con);
1149 
1150 #if SPICE_SERVER_VERSION >= 0x000e02 /* release 0.14.2 */
1151     Error *err = NULL;
1152     char device_address[256] = "";
1153     if (qemu_console_fill_device_address(con, device_address, 256, &err)) {
1154         spice_qxl_set_device_info(&ssd->qxl,
1155                                   device_address,
1156                                   qemu_console_get_head(con),
1157                                   1);
1158     } else {
1159         error_report_err(err);
1160     }
1161 #endif
1162 
1163     qemu_spice_create_host_memslot(ssd);
1164 
1165     if (spice_opengl) {
1166         qemu_console_set_display_gl_ctx(con, &ssd->dgc);
1167     }
1168     register_displaychangelistener(&ssd->dcl);
1169 }
1170 
1171 void qemu_spice_display_init(void)
1172 {
1173     QemuOptsList *olist = qemu_find_opts("spice");
1174     QemuOpts *opts = QTAILQ_FIRST(&olist->head);
1175     QemuConsole *spice_con, *con;
1176     const char *str;
1177     int i;
1178 
1179     str = qemu_opt_get(opts, "display");
1180     if (str) {
1181         int head = qemu_opt_get_number(opts, "head", 0);
1182         Error *err = NULL;
1183 
1184         spice_con = qemu_console_lookup_by_device_name(str, head, &err);
1185         if (err) {
1186             error_report("Failed to lookup display/head");
1187             exit(1);
1188         }
1189     } else {
1190         spice_con = NULL;
1191     }
1192 
1193     for (i = 0;; i++) {
1194         con = qemu_console_lookup_by_index(i);
1195         if (!con || !qemu_console_is_graphic(con)) {
1196             break;
1197         }
1198         if (qemu_spice_have_display_interface(con)) {
1199             continue;
1200         }
1201         if (spice_con != NULL && spice_con != con) {
1202             continue;
1203         }
1204         qemu_spice_display_init_one(con);
1205     }
1206 
1207     qemu_spice_display_init_done();
1208 }
1209