Merge changes I31aed8e7,Ibdbd63ea,I48d413bb into tizen
[sdk/emulator/qemu.git] / ui / spice-display.c
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 "ui/qemu-spice.h"
20 #include "qemu/timer.h"
21 #include "qemu/queue.h"
22 #include "monitor/monitor.h"
23 #include "ui/console.h"
24 #include "sysemu/sysemu.h"
25 #include "trace.h"
26
27 #include "ui/spice-display.h"
28
29 #ifdef CONFIG_MARU
30 #define RATIO_SCALE 2
31 #endif
32
33 static int debug = 0;
34
35 static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
36 {
37     va_list args;
38
39     if (level <= debug) {
40         va_start(args, fmt);
41         vfprintf(stderr, fmt, args);
42         va_end(args);
43     }
44 }
45
46 int qemu_spice_rect_is_empty(const QXLRect* r)
47 {
48     return r->top == r->bottom || r->left == r->right;
49 }
50
51 void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
52 {
53     if (qemu_spice_rect_is_empty(r)) {
54         return;
55     }
56
57     if (qemu_spice_rect_is_empty(dest)) {
58         *dest = *r;
59         return;
60     }
61
62     dest->top = MIN(dest->top, r->top);
63     dest->left = MIN(dest->left, r->left);
64     dest->bottom = MAX(dest->bottom, r->bottom);
65     dest->right = MAX(dest->right, r->right);
66 }
67
68 QXLCookie *qxl_cookie_new(int type, uint64_t io)
69 {
70     QXLCookie *cookie;
71
72     cookie = g_malloc0(sizeof(*cookie));
73     cookie->type = type;
74     cookie->io = io;
75     return cookie;
76 }
77
78 void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
79                             qxl_async_io async)
80 {
81     trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
82                                 memslot->virt_start, memslot->virt_end,
83                                 async);
84
85     if (async != QXL_SYNC) {
86         spice_qxl_add_memslot_async(&ssd->qxl, memslot,
87                 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
88                                           QXL_IO_MEMSLOT_ADD_ASYNC));
89     } else {
90         spice_qxl_add_memslot(&ssd->qxl, memslot);
91     }
92 }
93
94 void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
95 {
96     trace_qemu_spice_del_memslot(ssd->qxl.id, gid, sid);
97     spice_qxl_del_memslot(&ssd->qxl, gid, sid);
98 }
99
100 void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
101                                        QXLDevSurfaceCreate *surface,
102                                        qxl_async_io async)
103 {
104     trace_qemu_spice_create_primary_surface(ssd->qxl.id, id, surface, async);
105     if (async != QXL_SYNC) {
106         spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface,
107                 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
108                                           QXL_IO_CREATE_PRIMARY_ASYNC));
109     } else {
110         spice_qxl_create_primary_surface(&ssd->qxl, id, surface);
111     }
112 }
113
114 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
115                                         uint32_t id, qxl_async_io async)
116 {
117     trace_qemu_spice_destroy_primary_surface(ssd->qxl.id, id, async);
118     if (async != QXL_SYNC) {
119         spice_qxl_destroy_primary_surface_async(&ssd->qxl, id,
120                 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
121                                           QXL_IO_DESTROY_PRIMARY_ASYNC));
122     } else {
123         spice_qxl_destroy_primary_surface(&ssd->qxl, id);
124     }
125 }
126
127 void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
128 {
129     trace_qemu_spice_wakeup(ssd->qxl.id);
130     spice_qxl_wakeup(&ssd->qxl);
131 }
132
133 static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
134                                          QXLRect *rect)
135 {
136     SimpleSpiceUpdate *update;
137     QXLDrawable *drawable;
138     QXLImage *image;
139     QXLCommand *cmd;
140     int bw, bh;
141     struct timespec time_space;
142     pixman_image_t *dest;
143
144     trace_qemu_spice_create_update(
145            rect->left, rect->right,
146            rect->top, rect->bottom);
147
148     update   = g_malloc0(sizeof(*update));
149     drawable = &update->drawable;
150     image    = &update->image;
151     cmd      = &update->ext.cmd;
152
153     bw       = rect->right - rect->left;
154     bh       = rect->bottom - rect->top;
155     update->bitmap = g_malloc(bw * bh * 4);
156
157     drawable->bbox            = *rect;
158     drawable->clip.type       = SPICE_CLIP_TYPE_NONE;
159     drawable->effect          = QXL_EFFECT_OPAQUE;
160     drawable->release_info.id = (uintptr_t)update;
161     drawable->type            = QXL_DRAW_COPY;
162     drawable->surfaces_dest[0] = -1;
163     drawable->surfaces_dest[1] = -1;
164     drawable->surfaces_dest[2] = -1;
165     clock_gettime(CLOCK_MONOTONIC, &time_space);
166     /* time in milliseconds from epoch. */
167     drawable->mm_time = time_space.tv_sec * 1000
168                       + time_space.tv_nsec / 1000 / 1000;
169
170     drawable->u.copy.rop_descriptor  = SPICE_ROPD_OP_PUT;
171     drawable->u.copy.src_bitmap      = (uintptr_t)image;
172     drawable->u.copy.src_area.right  = bw;
173     drawable->u.copy.src_area.bottom = bh;
174
175     QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
176     image->descriptor.type   = SPICE_IMAGE_TYPE_BITMAP;
177     image->bitmap.flags      = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
178     image->bitmap.stride     = bw * 4;
179     image->descriptor.width  = image->bitmap.x = bw;
180     image->descriptor.height = image->bitmap.y = bh;
181     image->bitmap.data = (uintptr_t)(update->bitmap);
182     image->bitmap.palette = 0;
183     image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
184
185     dest = pixman_image_create_bits(PIXMAN_x8r8g8b8, bw, bh,
186                                     (void *)update->bitmap, bw * 4);
187     pixman_image_composite(PIXMAN_OP_SRC, ssd->surface, NULL, ssd->mirror,
188                            rect->left, rect->top, 0, 0,
189                            rect->left, rect->top, bw, bh);
190     pixman_image_composite(PIXMAN_OP_SRC, ssd->mirror, NULL, dest,
191                            rect->left, rect->top, 0, 0,
192                            0, 0, bw, bh);
193     pixman_image_unref(dest);
194
195     cmd->type = QXL_CMD_DRAW;
196     cmd->data = (uintptr_t)drawable;
197
198     QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
199 }
200
201 static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
202 {
203 #ifndef CONFIG_MARU
204     static const int blksize = 32;
205     int blocks = (surface_width(ssd->ds) + blksize - 1) / blksize;
206     int dirty_top[blocks];
207     int y, yoff, x, xoff, blk, bw;
208     int bpp = surface_bytes_per_pixel(ssd->ds);
209     uint8_t *guest, *mirror;
210 #endif
211
212     if (qemu_spice_rect_is_empty(&ssd->dirty)) {
213         return;
214     };
215
216     if (ssd->surface == NULL) {
217         ssd->surface = pixman_image_ref(ssd->ds->image);
218         ssd->mirror  = qemu_pixman_mirror_create(ssd->ds->format,
219                                                  ssd->ds->image);
220     }
221
222 #ifndef CONFIG_MARU
223     for (blk = 0; blk < blocks; blk++) {
224         dirty_top[blk] = -1;
225     }
226
227     guest = surface_data(ssd->ds);
228     mirror = (void *)pixman_image_get_data(ssd->mirror);
229     for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
230         yoff = y * surface_stride(ssd->ds);
231         for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
232             xoff = x * bpp;
233             blk = x / blksize;
234             bw = MIN(blksize, ssd->dirty.right - x);
235             if (memcmp(guest + yoff + xoff,
236                        mirror + yoff + xoff,
237                        bw * bpp) == 0) {
238                 if (dirty_top[blk] != -1) {
239                     QXLRect update = {
240                         .top    = dirty_top[blk],
241                         .bottom = y,
242                         .left   = x,
243                         .right  = x + bw,
244                     };
245                     qemu_spice_create_one_update(ssd, &update);
246                     dirty_top[blk] = -1;
247                 }
248             } else {
249                 if (dirty_top[blk] == -1) {
250                     dirty_top[blk] = y;
251                 }
252             }
253         }
254     }
255
256     for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
257         blk = x / blksize;
258         bw = MIN(blksize, ssd->dirty.right - x);
259         if (dirty_top[blk] != -1) {
260             QXLRect update = {
261                 .top    = dirty_top[blk],
262                 .bottom = ssd->dirty.bottom,
263                 .left   = x,
264                 .right  = x + bw,
265             };
266             qemu_spice_create_one_update(ssd, &update);
267             dirty_top[blk] = -1;
268         }
269     }
270 #endif
271
272 #ifdef CONFIG_MARU
273     pixman_transform_t matrix;
274     struct pixman_f_transform matrix_f;
275
276     pixman_f_transform_init_identity(&matrix_f);
277     pixman_f_transform_scale(&matrix_f, NULL, RATIO_SCALE, RATIO_SCALE);
278     pixman_transform_from_pixman_f_transform(&matrix, &matrix_f);
279     pixman_image_set_transform(ssd->ds->image, &matrix);
280     pixman_image_set_filter(ssd->ds->image, PIXMAN_FILTER_BEST, NULL, 0);
281
282     QXLRect update = {
283         .top    = 0,
284         .bottom = surface_height(ssd->ds),
285         .left   = 0,
286         .right  = surface_width(ssd->ds),
287     };
288
289     qemu_spice_create_one_update(ssd, &update);
290 #endif
291
292     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
293 }
294
295 /*
296  * Called from spice server thread context (via interface_release_resource)
297  * We do *not* hold the global qemu mutex here, so extra care is needed
298  * when calling qemu functions.  QEMU interfaces used:
299  *    - g_free (underlying glibc free is re-entrant).
300  */
301 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
302 {
303     g_free(update->bitmap);
304     g_free(update);
305 }
306
307 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
308 {
309     QXLDevMemSlot memslot;
310
311     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
312
313     memset(&memslot, 0, sizeof(memslot));
314     memslot.slot_group_id = MEMSLOT_GROUP_HOST;
315     memslot.virt_end = ~0;
316     qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
317 }
318
319 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
320 {
321     QXLDevSurfaceCreate surface;
322
323     memset(&surface, 0, sizeof(surface));
324
325     dprint(1, "%s/%d: %dx%d\n", __func__, ssd->qxl.id,
326            surface_width(ssd->ds), surface_height(ssd->ds));
327
328     surface.format     = SPICE_SURFACE_FMT_32_xRGB;
329     surface.width      = surface_width(ssd->ds);
330     surface.height     = surface_height(ssd->ds);
331     surface.stride     = -surface.width * 4;
332     surface.mouse_mode = true;
333     surface.flags      = 0;
334     surface.type       = 0;
335     surface.mem        = (uintptr_t)ssd->buf;
336     surface.group_id   = MEMSLOT_GROUP_HOST;
337
338 #ifdef CONFIG_MARU
339     surface.width /= RATIO_SCALE;
340     surface.height /= RATIO_SCALE;
341 #endif
342
343     qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
344 }
345
346 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
347 {
348     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
349
350     qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
351 }
352
353 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
354 {
355     qemu_mutex_init(&ssd->lock);
356     QTAILQ_INIT(&ssd->updates);
357     ssd->mouse_x = -1;
358     ssd->mouse_y = -1;
359     if (ssd->num_surfaces == 0) {
360         ssd->num_surfaces = 1024;
361     }
362     ssd->bufsize = (16 * 1024 * 1024);
363     ssd->buf = g_malloc(ssd->bufsize);
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     dprint(2, "%s/%d: x %d y %d w %d h %d\n", __func__,
374            ssd->qxl.id, x, y, w, h);
375     update_area.left = x,
376     update_area.right = x + w;
377     update_area.top = y;
378     update_area.bottom = y + h;
379
380     if (qemu_spice_rect_is_empty(&ssd->dirty)) {
381         ssd->notify++;
382     }
383     qemu_spice_rect_union(&ssd->dirty, &update_area);
384 }
385
386 void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
387                                DisplaySurface *surface)
388 {
389     SimpleSpiceUpdate *update;
390     bool need_destroy;
391
392     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
393
394     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
395     if (ssd->surface) {
396         pixman_image_unref(ssd->surface);
397         ssd->surface = NULL;
398         pixman_image_unref(ssd->mirror);
399         ssd->mirror = NULL;
400     }
401
402     qemu_mutex_lock(&ssd->lock);
403     need_destroy = (ssd->ds != NULL);
404     ssd->ds = surface;
405     while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
406         QTAILQ_REMOVE(&ssd->updates, update, next);
407         qemu_spice_destroy_update(ssd, update);
408     }
409     qemu_mutex_unlock(&ssd->lock);
410     if (need_destroy) {
411         qemu_spice_destroy_host_primary(ssd);
412     }
413     if (ssd->ds) {
414         qemu_spice_create_host_primary(ssd);
415     }
416
417     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
418     ssd->notify++;
419 }
420
421 void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
422 {
423     if (ssd->cursor) {
424         assert(ssd->dcl.con);
425         dpy_cursor_define(ssd->dcl.con, ssd->cursor);
426         cursor_put(ssd->cursor);
427         ssd->cursor = NULL;
428     }
429     if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
430         assert(ssd->dcl.con);
431         dpy_mouse_set(ssd->dcl.con, ssd->mouse_x, ssd->mouse_y, 1);
432         ssd->mouse_x = -1;
433         ssd->mouse_y = -1;
434     }
435 }
436
437 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
438 {
439     dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
440     graphic_hw_update(ssd->dcl.con);
441
442     qemu_mutex_lock(&ssd->lock);
443     if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
444         qemu_spice_create_update(ssd);
445         ssd->notify++;
446     }
447     qemu_spice_cursor_refresh_unlocked(ssd);
448     qemu_mutex_unlock(&ssd->lock);
449
450     if (ssd->notify) {
451         ssd->notify = 0;
452         qemu_spice_wakeup(ssd);
453         dprint(2, "%s/%d: notify\n", __func__, ssd->qxl.id);
454     }
455 }
456
457 /* spice display interface callbacks */
458
459 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
460 {
461     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
462
463     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
464     ssd->worker = qxl_worker;
465 }
466
467 static void interface_set_compression_level(QXLInstance *sin, int level)
468 {
469     dprint(1, "%s/%d:\n", __func__, sin->id);
470     /* nothing to do */
471 }
472
473 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
474 {
475     dprint(3, "%s/%d:\n", __func__, sin->id);
476     /* nothing to do */
477 }
478
479 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
480 {
481     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
482
483     info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
484     info->memslot_id_bits  = MEMSLOT_SLOT_BITS;
485     info->num_memslots = NUM_MEMSLOTS;
486     info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
487     info->internal_groupslot_id = 0;
488     info->qxl_ram_size = ssd->bufsize;
489     info->n_surfaces = ssd->num_surfaces;
490 }
491
492 static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
493 {
494     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
495     SimpleSpiceUpdate *update;
496     int ret = false;
497
498     dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
499
500     qemu_mutex_lock(&ssd->lock);
501     update = QTAILQ_FIRST(&ssd->updates);
502     if (update != NULL) {
503         QTAILQ_REMOVE(&ssd->updates, update, next);
504         *ext = update->ext;
505         ret = true;
506     }
507     qemu_mutex_unlock(&ssd->lock);
508
509     return ret;
510 }
511
512 static int interface_req_cmd_notification(QXLInstance *sin)
513 {
514     dprint(1, "%s/%d:\n", __func__, sin->id);
515     return 1;
516 }
517
518 static void interface_release_resource(QXLInstance *sin,
519                                        struct QXLReleaseInfoExt ext)
520 {
521     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
522     uintptr_t id;
523
524     dprint(2, "%s/%d:\n", __func__, ssd->qxl.id);
525     id = ext.info->id;
526     qemu_spice_destroy_update(ssd, (void*)id);
527 }
528
529 static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
530 {
531     dprint(3, "%s:\n", __FUNCTION__);
532     return false;
533 }
534
535 static int interface_req_cursor_notification(QXLInstance *sin)
536 {
537     dprint(1, "%s:\n", __FUNCTION__);
538     return 1;
539 }
540
541 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
542 {
543     fprintf(stderr, "%s: abort()\n", __FUNCTION__);
544     abort();
545 }
546
547 static int interface_flush_resources(QXLInstance *sin)
548 {
549     fprintf(stderr, "%s: abort()\n", __FUNCTION__);
550     abort();
551     return 0;
552 }
553
554 static void interface_update_area_complete(QXLInstance *sin,
555         uint32_t surface_id,
556         QXLRect *dirty, uint32_t num_updated_rects)
557 {
558     /* should never be called, used in qxl native mode only */
559     fprintf(stderr, "%s: abort()\n", __func__);
560     abort();
561 }
562
563 /* called from spice server thread context only */
564 static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
565 {
566     /* should never be called, used in qxl native mode only */
567     fprintf(stderr, "%s: abort()\n", __func__);
568     abort();
569 }
570
571 static void interface_set_client_capabilities(QXLInstance *sin,
572                                               uint8_t client_present,
573                                               uint8_t caps[58])
574 {
575     dprint(3, "%s:\n", __func__);
576 }
577
578 static int interface_client_monitors_config(QXLInstance *sin,
579                                             VDAgentMonitorsConfig *mc)
580 {
581     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
582     QemuUIInfo info;
583     int rc;
584
585     if (!mc) {
586         return 1;
587     }
588
589     /*
590      * FIXME: multihead is tricky due to the way
591      * spice has multihead implemented.
592      */
593     memset(&info, 0, sizeof(info));
594     if (mc->num_of_monitors > 0) {
595         info.width  = mc->monitors[0].width;
596         info.height = mc->monitors[0].height;
597     }
598     rc = dpy_set_ui_info(ssd->dcl.con, &info);
599     dprint(1, "%s/%d: size %dx%d, rc %d   <---   ==========================\n",
600            __func__, ssd->qxl.id, info.width, info.height, rc);
601     if (rc != 0) {
602         return 0; /* == not supported by guest */
603     } else {
604         return 1;
605     }
606 }
607
608 static const QXLInterface dpy_interface = {
609     .base.type               = SPICE_INTERFACE_QXL,
610     .base.description        = "qemu simple display",
611     .base.major_version      = SPICE_INTERFACE_QXL_MAJOR,
612     .base.minor_version      = SPICE_INTERFACE_QXL_MINOR,
613
614     .attache_worker          = interface_attach_worker,
615     .set_compression_level   = interface_set_compression_level,
616     .set_mm_time             = interface_set_mm_time,
617     .get_init_info           = interface_get_init_info,
618
619     /* the callbacks below are called from spice server thread context */
620     .get_command             = interface_get_command,
621     .req_cmd_notification    = interface_req_cmd_notification,
622     .release_resource        = interface_release_resource,
623     .get_cursor_command      = interface_get_cursor_command,
624     .req_cursor_notification = interface_req_cursor_notification,
625     .notify_update           = interface_notify_update,
626     .flush_resources         = interface_flush_resources,
627     .async_complete          = interface_async_complete,
628     .update_area_complete    = interface_update_area_complete,
629     .set_client_capabilities = interface_set_client_capabilities,
630     .client_monitors_config  = interface_client_monitors_config,
631 };
632
633 static void display_update(DisplayChangeListener *dcl,
634                            int x, int y, int w, int h)
635 {
636     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
637     qemu_spice_display_update(ssd, x, y, w, h);
638 }
639
640 static void display_switch(DisplayChangeListener *dcl,
641                            struct DisplaySurface *surface)
642 {
643     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
644     qemu_spice_display_switch(ssd, surface);
645 }
646
647 static void display_refresh(DisplayChangeListener *dcl)
648 {
649     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
650     qemu_spice_display_refresh(ssd);
651 }
652
653 static const DisplayChangeListenerOps display_listener_ops = {
654     .dpy_name        = "spice",
655     .dpy_gfx_update  = display_update,
656     .dpy_gfx_switch  = display_switch,
657     .dpy_refresh     = display_refresh,
658 };
659
660 static void qemu_spice_display_init_one(QemuConsole *con)
661 {
662     SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
663
664     qemu_spice_display_init_common(ssd);
665
666     ssd->qxl.base.sif = &dpy_interface.base;
667     qemu_spice_add_display_interface(&ssd->qxl, con);
668     assert(ssd->worker);
669
670     qemu_spice_create_host_memslot(ssd);
671
672     ssd->dcl.ops = &display_listener_ops;
673     ssd->dcl.con = con;
674     register_displaychangelistener(&ssd->dcl);
675 }
676
677 void qemu_spice_display_init(void)
678 {
679     QemuConsole *con;
680     int i;
681
682     for (i = 0;; i++) {
683         con = qemu_console_lookup_by_index(i);
684         if (!con || !qemu_console_is_graphic(con)) {
685             break;
686         }
687         if (qemu_spice_have_display_interface(con)) {
688             continue;
689         }
690         qemu_spice_display_init_one(con);
691     }
692 }