Merge tag 'v2.4.1' into tizen_3.0_develop
[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 "ui/console.h"
23 #include "sysemu/sysemu.h"
24 #include "trace.h"
25
26 #include "ui/spice-display.h"
27
28 #ifdef CONFIG_MARU
29 #define RATIO_SCALE 2
30 #endif
31
32 static int debug = 0;
33
34 static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
35 {
36     va_list args;
37
38     if (level <= debug) {
39         va_start(args, fmt);
40         vfprintf(stderr, fmt, args);
41         va_end(args);
42     }
43 }
44
45 int qemu_spice_rect_is_empty(const QXLRect* r)
46 {
47     return r->top == r->bottom || r->left == r->right;
48 }
49
50 void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
51 {
52     if (qemu_spice_rect_is_empty(r)) {
53         return;
54     }
55
56     if (qemu_spice_rect_is_empty(dest)) {
57         *dest = *r;
58         return;
59     }
60
61     dest->top = MIN(dest->top, r->top);
62     dest->left = MIN(dest->left, r->left);
63     dest->bottom = MAX(dest->bottom, r->bottom);
64     dest->right = MAX(dest->right, r->right);
65 }
66
67 QXLCookie *qxl_cookie_new(int type, uint64_t io)
68 {
69     QXLCookie *cookie;
70
71     cookie = g_malloc0(sizeof(*cookie));
72     cookie->type = type;
73     cookie->io = io;
74     return cookie;
75 }
76
77 void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
78                             qxl_async_io async)
79 {
80     trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
81                                 memslot->virt_start, memslot->virt_end,
82                                 async);
83
84     if (async != QXL_SYNC) {
85         spice_qxl_add_memslot_async(&ssd->qxl, memslot,
86                 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
87                                           QXL_IO_MEMSLOT_ADD_ASYNC));
88     } else {
89         spice_qxl_add_memslot(&ssd->qxl, memslot);
90     }
91 }
92
93 void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
94 {
95     trace_qemu_spice_del_memslot(ssd->qxl.id, gid, sid);
96     spice_qxl_del_memslot(&ssd->qxl, gid, sid);
97 }
98
99 void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
100                                        QXLDevSurfaceCreate *surface,
101                                        qxl_async_io async)
102 {
103     trace_qemu_spice_create_primary_surface(ssd->qxl.id, id, surface, async);
104     if (async != QXL_SYNC) {
105         spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface,
106                 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
107                                           QXL_IO_CREATE_PRIMARY_ASYNC));
108     } else {
109         spice_qxl_create_primary_surface(&ssd->qxl, id, surface);
110     }
111 }
112
113 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
114                                         uint32_t id, qxl_async_io async)
115 {
116     trace_qemu_spice_destroy_primary_surface(ssd->qxl.id, id, async);
117     if (async != QXL_SYNC) {
118         spice_qxl_destroy_primary_surface_async(&ssd->qxl, id,
119                 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
120                                           QXL_IO_DESTROY_PRIMARY_ASYNC));
121     } else {
122         spice_qxl_destroy_primary_surface(&ssd->qxl, id);
123     }
124 }
125
126 void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
127 {
128     trace_qemu_spice_wakeup(ssd->qxl.id);
129     spice_qxl_wakeup(&ssd->qxl);
130 }
131
132 static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
133                                          QXLRect *rect)
134 {
135     SimpleSpiceUpdate *update;
136     QXLDrawable *drawable;
137     QXLImage *image;
138     QXLCommand *cmd;
139     int bw, bh;
140     struct timespec time_space;
141     pixman_image_t *dest;
142
143     trace_qemu_spice_create_update(
144            rect->left, rect->right,
145            rect->top, rect->bottom);
146
147     update   = g_malloc0(sizeof(*update));
148     drawable = &update->drawable;
149     image    = &update->image;
150     cmd      = &update->ext.cmd;
151
152     bw       = rect->right - rect->left;
153     bh       = rect->bottom - rect->top;
154     update->bitmap = g_malloc(bw * bh * 4);
155
156     drawable->bbox            = *rect;
157     drawable->clip.type       = SPICE_CLIP_TYPE_NONE;
158     drawable->effect          = QXL_EFFECT_OPAQUE;
159     drawable->release_info.id = (uintptr_t)(&update->ext);
160     drawable->type            = QXL_DRAW_COPY;
161     drawable->surfaces_dest[0] = -1;
162     drawable->surfaces_dest[1] = -1;
163     drawable->surfaces_dest[2] = -1;
164     clock_gettime(CLOCK_MONOTONIC, &time_space);
165     /* time in milliseconds from epoch. */
166     drawable->mm_time = time_space.tv_sec * 1000
167                       + time_space.tv_nsec / 1000 / 1000;
168
169     drawable->u.copy.rop_descriptor  = SPICE_ROPD_OP_PUT;
170     drawable->u.copy.src_bitmap      = (uintptr_t)image;
171     drawable->u.copy.src_area.right  = bw;
172     drawable->u.copy.src_area.bottom = bh;
173
174     QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
175     image->descriptor.type   = SPICE_IMAGE_TYPE_BITMAP;
176     image->bitmap.flags      = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
177     image->bitmap.stride     = bw * 4;
178     image->descriptor.width  = image->bitmap.x = bw;
179     image->descriptor.height = image->bitmap.y = bh;
180     image->bitmap.data = (uintptr_t)(update->bitmap);
181     image->bitmap.palette = 0;
182     image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
183
184     dest = pixman_image_create_bits(PIXMAN_LE_x8r8g8b8, bw, bh,
185                                     (void *)update->bitmap, bw * 4);
186     pixman_image_composite(PIXMAN_OP_SRC, ssd->surface, NULL, ssd->mirror,
187                            rect->left, rect->top, 0, 0,
188                            rect->left, rect->top, bw, bh);
189     pixman_image_composite(PIXMAN_OP_SRC, ssd->mirror, NULL, dest,
190                            rect->left, rect->top, 0, 0,
191                            0, 0, bw, bh);
192     pixman_image_unref(dest);
193
194     cmd->type = QXL_CMD_DRAW;
195     cmd->data = (uintptr_t)drawable;
196
197     QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
198 }
199
200 static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
201 {
202 #ifndef CONFIG_MARU
203     static const int blksize = 32;
204     int blocks = (surface_width(ssd->ds) + blksize - 1) / blksize;
205     int dirty_top[blocks];
206     int y, yoff1, yoff2, x, xoff, blk, bw;
207     int bpp = surface_bytes_per_pixel(ssd->ds);
208     uint8_t *guest, *mirror;
209 #endif
210
211     if (qemu_spice_rect_is_empty(&ssd->dirty)) {
212         return;
213     };
214
215     if (ssd->surface == NULL) {
216         ssd->surface = pixman_image_ref(ssd->ds->image);
217         ssd->mirror  = qemu_pixman_mirror_create(ssd->ds->format,
218                                                  ssd->ds->image);
219     }
220
221 #ifndef CONFIG_MARU
222     for (blk = 0; blk < blocks; blk++) {
223         dirty_top[blk] = -1;
224     }
225
226     guest = surface_data(ssd->ds);
227     mirror = (void *)pixman_image_get_data(ssd->mirror);
228     for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
229         yoff1 = y * surface_stride(ssd->ds);
230         yoff2 = y * pixman_image_get_stride(ssd->mirror);
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 + yoff1 + xoff,
236                        mirror + yoff2 + 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 static SimpleSpiceCursor*
296 qemu_spice_create_cursor_update(SimpleSpiceDisplay *ssd,
297                                 QEMUCursor *c,
298                                 int on)
299 {
300     size_t size = c ? c->width * c->height * 4 : 0;
301     SimpleSpiceCursor *update;
302     QXLCursorCmd *ccmd;
303     QXLCursor *cursor;
304     QXLCommand *cmd;
305
306     update   = g_malloc0(sizeof(*update) + size);
307     ccmd     = &update->cmd;
308     cursor   = &update->cursor;
309     cmd      = &update->ext.cmd;
310
311     if (c) {
312         ccmd->type = QXL_CURSOR_SET;
313         ccmd->u.set.position.x = ssd->ptr_x + ssd->hot_x;
314         ccmd->u.set.position.y = ssd->ptr_y + ssd->hot_y;
315         ccmd->u.set.visible    = true;
316         ccmd->u.set.shape      = (uintptr_t)cursor;
317         cursor->header.unique     = ssd->unique++;
318         cursor->header.type       = SPICE_CURSOR_TYPE_ALPHA;
319         cursor->header.width      = c->width;
320         cursor->header.height     = c->height;
321         cursor->header.hot_spot_x = c->hot_x;
322         cursor->header.hot_spot_y = c->hot_y;
323         cursor->data_size         = size;
324         cursor->chunk.data_size   = size;
325         memcpy(cursor->chunk.data, c->data, size);
326     } else if (!on) {
327         ccmd->type = QXL_CURSOR_HIDE;
328     } else {
329         ccmd->type = QXL_CURSOR_MOVE;
330         ccmd->u.position.x = ssd->ptr_x + ssd->hot_x;
331         ccmd->u.position.y = ssd->ptr_y + ssd->hot_y;
332     }
333     ccmd->release_info.id = (uintptr_t)(&update->ext);
334
335     cmd->type = QXL_CMD_CURSOR;
336     cmd->data = (uintptr_t)ccmd;
337
338     return update;
339 }
340
341 /*
342  * Called from spice server thread context (via interface_release_resource)
343  * We do *not* hold the global qemu mutex here, so extra care is needed
344  * when calling qemu functions.  QEMU interfaces used:
345  *    - g_free (underlying glibc free is re-entrant).
346  */
347 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
348 {
349     g_free(update->bitmap);
350     g_free(update);
351 }
352
353 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
354 {
355     QXLDevMemSlot memslot;
356
357     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
358
359     memset(&memslot, 0, sizeof(memslot));
360     memslot.slot_group_id = MEMSLOT_GROUP_HOST;
361     memslot.virt_end = ~0;
362     qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
363 }
364
365 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
366 {
367     QXLDevSurfaceCreate surface;
368     uint64_t surface_size;
369
370     memset(&surface, 0, sizeof(surface));
371
372     surface_size = (uint64_t) surface_width(ssd->ds) *
373         surface_height(ssd->ds) * 4;
374     assert(surface_size > 0);
375     assert(surface_size < INT_MAX);
376     if (ssd->bufsize < surface_size) {
377         ssd->bufsize = surface_size;
378         g_free(ssd->buf);
379         ssd->buf = g_malloc(ssd->bufsize);
380     }
381
382     dprint(1, "%s/%d: %ux%u (size %" PRIu64 "/%d)\n", __func__, ssd->qxl.id,
383            surface_width(ssd->ds), surface_height(ssd->ds),
384            surface_size, ssd->bufsize);
385
386     surface.format     = SPICE_SURFACE_FMT_32_xRGB;
387     surface.width      = surface_width(ssd->ds);
388     surface.height     = surface_height(ssd->ds);
389     surface.stride     = -surface.width * 4;
390     surface.mouse_mode = true;
391     surface.flags      = 0;
392     surface.type       = 0;
393     surface.mem        = (uintptr_t)ssd->buf;
394     surface.group_id   = MEMSLOT_GROUP_HOST;
395
396 #ifdef CONFIG_MARU
397     surface.width /= RATIO_SCALE;
398     surface.height /= RATIO_SCALE;
399 #endif
400
401     qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
402 }
403
404 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
405 {
406     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
407
408     qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
409 }
410
411 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
412 {
413     qemu_mutex_init(&ssd->lock);
414     QTAILQ_INIT(&ssd->updates);
415     ssd->mouse_x = -1;
416     ssd->mouse_y = -1;
417     if (ssd->num_surfaces == 0) {
418         ssd->num_surfaces = 1024;
419     }
420 }
421
422 /* display listener callbacks */
423
424 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
425                                int x, int y, int w, int h)
426 {
427     QXLRect update_area;
428
429     dprint(2, "%s/%d: x %d y %d w %d h %d\n", __func__,
430            ssd->qxl.id, x, y, w, h);
431     update_area.left = x,
432     update_area.right = x + w;
433     update_area.top = y;
434     update_area.bottom = y + h;
435
436     if (qemu_spice_rect_is_empty(&ssd->dirty)) {
437         ssd->notify++;
438     }
439     qemu_spice_rect_union(&ssd->dirty, &update_area);
440 }
441
442 void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
443                                DisplaySurface *surface)
444 {
445     SimpleSpiceUpdate *update;
446     bool need_destroy;
447
448     if (surface && ssd->surface &&
449         surface_width(surface) == pixman_image_get_width(ssd->surface) &&
450         surface_height(surface) == pixman_image_get_height(ssd->surface)) {
451         /* no-resize fast path: just swap backing store */
452         dprint(1, "%s/%d: fast (%dx%d)\n", __func__, ssd->qxl.id,
453                surface_width(surface), surface_height(surface));
454         qemu_mutex_lock(&ssd->lock);
455         ssd->ds = surface;
456         pixman_image_unref(ssd->surface);
457         ssd->surface = pixman_image_ref(ssd->ds->image);
458         qemu_mutex_unlock(&ssd->lock);
459         qemu_spice_display_update(ssd, 0, 0,
460                                   surface_width(surface),
461                                   surface_height(surface));
462         return;
463     }
464
465     /* full mode switch */
466     dprint(1, "%s/%d: full (%dx%d -> %dx%d)\n", __func__, ssd->qxl.id,
467            ssd->surface ? pixman_image_get_width(ssd->surface)  : 0,
468            ssd->surface ? pixman_image_get_height(ssd->surface) : 0,
469            surface ? surface_width(surface)  : 0,
470            surface ? surface_height(surface) : 0);
471
472     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
473     if (ssd->surface) {
474         pixman_image_unref(ssd->surface);
475         ssd->surface = NULL;
476         pixman_image_unref(ssd->mirror);
477         ssd->mirror = NULL;
478     }
479
480     qemu_mutex_lock(&ssd->lock);
481     need_destroy = (ssd->ds != NULL);
482     ssd->ds = surface;
483     while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
484         QTAILQ_REMOVE(&ssd->updates, update, next);
485         qemu_spice_destroy_update(ssd, update);
486     }
487     qemu_mutex_unlock(&ssd->lock);
488     if (need_destroy) {
489         qemu_spice_destroy_host_primary(ssd);
490     }
491     if (ssd->ds) {
492         ssd->surface = pixman_image_ref(ssd->ds->image);
493         ssd->mirror  = qemu_pixman_mirror_create(ssd->ds->format,
494                                                  ssd->ds->image);
495         qemu_spice_create_host_primary(ssd);
496     }
497
498     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
499     ssd->notify++;
500 }
501
502 static void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
503 {
504     if (ssd->cursor) {
505         assert(ssd->dcl.con);
506         dpy_cursor_define(ssd->dcl.con, ssd->cursor);
507         cursor_put(ssd->cursor);
508         ssd->cursor = NULL;
509     }
510     if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
511         assert(ssd->dcl.con);
512         dpy_mouse_set(ssd->dcl.con, ssd->mouse_x, ssd->mouse_y, 1);
513         ssd->mouse_x = -1;
514         ssd->mouse_y = -1;
515     }
516 }
517
518 void qemu_spice_cursor_refresh_bh(void *opaque)
519 {
520     SimpleSpiceDisplay *ssd = opaque;
521
522     qemu_mutex_lock(&ssd->lock);
523     qemu_spice_cursor_refresh_unlocked(ssd);
524     qemu_mutex_unlock(&ssd->lock);
525 }
526
527 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
528 {
529     dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
530     graphic_hw_update(ssd->dcl.con);
531
532     qemu_mutex_lock(&ssd->lock);
533     if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
534         qemu_spice_create_update(ssd);
535         ssd->notify++;
536     }
537     qemu_mutex_unlock(&ssd->lock);
538
539     if (ssd->notify) {
540         ssd->notify = 0;
541         qemu_spice_wakeup(ssd);
542         dprint(2, "%s/%d: notify\n", __func__, ssd->qxl.id);
543     }
544 }
545
546 /* spice display interface callbacks */
547
548 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
549 {
550     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
551
552     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
553     ssd->worker = qxl_worker;
554 }
555
556 static void interface_set_compression_level(QXLInstance *sin, int level)
557 {
558     dprint(1, "%s/%d:\n", __func__, sin->id);
559     /* nothing to do */
560 }
561
562 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
563 {
564     dprint(3, "%s/%d:\n", __func__, sin->id);
565     /* nothing to do */
566 }
567
568 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
569 {
570     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
571
572     info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
573     info->memslot_id_bits  = MEMSLOT_SLOT_BITS;
574     info->num_memslots = NUM_MEMSLOTS;
575     info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
576     info->internal_groupslot_id = 0;
577     info->qxl_ram_size = 16 * 1024 * 1024;
578     info->n_surfaces = ssd->num_surfaces;
579 }
580
581 static int interface_get_command(QXLInstance *sin, QXLCommandExt *ext)
582 {
583     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
584     SimpleSpiceUpdate *update;
585     int ret = false;
586
587     dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
588
589     qemu_mutex_lock(&ssd->lock);
590     update = QTAILQ_FIRST(&ssd->updates);
591     if (update != NULL) {
592         QTAILQ_REMOVE(&ssd->updates, update, next);
593         *ext = update->ext;
594         ret = true;
595     }
596     qemu_mutex_unlock(&ssd->lock);
597
598     return ret;
599 }
600
601 static int interface_req_cmd_notification(QXLInstance *sin)
602 {
603     dprint(1, "%s/%d:\n", __func__, sin->id);
604     return 1;
605 }
606
607 static void interface_release_resource(QXLInstance *sin,
608                                        QXLReleaseInfoExt rext)
609 {
610     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
611     SimpleSpiceUpdate *update;
612     SimpleSpiceCursor *cursor;
613     QXLCommandExt *ext;
614
615     dprint(2, "%s/%d:\n", __func__, ssd->qxl.id);
616     ext = (void *)(intptr_t)(rext.info->id);
617     switch (ext->cmd.type) {
618     case QXL_CMD_DRAW:
619         update = container_of(ext, SimpleSpiceUpdate, ext);
620         qemu_spice_destroy_update(ssd, update);
621         break;
622     case QXL_CMD_CURSOR:
623         cursor = container_of(ext, SimpleSpiceCursor, ext);
624         g_free(cursor);
625         break;
626     default:
627         g_assert_not_reached();
628     }
629 }
630
631 static int interface_get_cursor_command(QXLInstance *sin, QXLCommandExt *ext)
632 {
633     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
634     int ret;
635
636     dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
637
638     qemu_mutex_lock(&ssd->lock);
639     if (ssd->ptr_define) {
640         *ext = ssd->ptr_define->ext;
641         ssd->ptr_define = NULL;
642         ret = true;
643     } else if (ssd->ptr_move) {
644         *ext = ssd->ptr_move->ext;
645         ssd->ptr_move = NULL;
646         ret = true;
647     } else {
648         ret = false;
649     }
650     qemu_mutex_unlock(&ssd->lock);
651     return ret;
652 }
653
654 static int interface_req_cursor_notification(QXLInstance *sin)
655 {
656     dprint(1, "%s:\n", __FUNCTION__);
657     return 1;
658 }
659
660 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
661 {
662     fprintf(stderr, "%s: abort()\n", __FUNCTION__);
663     abort();
664 }
665
666 static int interface_flush_resources(QXLInstance *sin)
667 {
668     fprintf(stderr, "%s: abort()\n", __FUNCTION__);
669     abort();
670     return 0;
671 }
672
673 static void interface_update_area_complete(QXLInstance *sin,
674         uint32_t surface_id,
675         QXLRect *dirty, uint32_t num_updated_rects)
676 {
677     /* should never be called, used in qxl native mode only */
678     fprintf(stderr, "%s: abort()\n", __func__);
679     abort();
680 }
681
682 /* called from spice server thread context only */
683 static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
684 {
685     /* should never be called, used in qxl native mode only */
686     fprintf(stderr, "%s: abort()\n", __func__);
687     abort();
688 }
689
690 static void interface_set_client_capabilities(QXLInstance *sin,
691                                               uint8_t client_present,
692                                               uint8_t caps[58])
693 {
694     dprint(3, "%s:\n", __func__);
695 }
696
697 static int interface_client_monitors_config(QXLInstance *sin,
698                                             VDAgentMonitorsConfig *mc)
699 {
700     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
701     QemuUIInfo info;
702
703     if (!dpy_ui_info_supported(ssd->dcl.con)) {
704         return 0; /* == not supported by guest */
705     }
706
707     if (!mc) {
708         return 1;
709     }
710
711     /*
712      * FIXME: multihead is tricky due to the way
713      * spice has multihead implemented.
714      */
715     memset(&info, 0, sizeof(info));
716     if (mc->num_of_monitors > 0) {
717         info.width  = mc->monitors[0].width;
718         info.height = mc->monitors[0].height;
719     }
720     dpy_set_ui_info(ssd->dcl.con, &info);
721     dprint(1, "%s/%d: size %dx%d\n", __func__, ssd->qxl.id,
722            info.width, info.height);
723     return 1;
724 }
725
726 static const QXLInterface dpy_interface = {
727     .base.type               = SPICE_INTERFACE_QXL,
728     .base.description        = "qemu simple display",
729     .base.major_version      = SPICE_INTERFACE_QXL_MAJOR,
730     .base.minor_version      = SPICE_INTERFACE_QXL_MINOR,
731
732     .attache_worker          = interface_attach_worker,
733     .set_compression_level   = interface_set_compression_level,
734     .set_mm_time             = interface_set_mm_time,
735     .get_init_info           = interface_get_init_info,
736
737     /* the callbacks below are called from spice server thread context */
738     .get_command             = interface_get_command,
739     .req_cmd_notification    = interface_req_cmd_notification,
740     .release_resource        = interface_release_resource,
741     .get_cursor_command      = interface_get_cursor_command,
742     .req_cursor_notification = interface_req_cursor_notification,
743     .notify_update           = interface_notify_update,
744     .flush_resources         = interface_flush_resources,
745     .async_complete          = interface_async_complete,
746     .update_area_complete    = interface_update_area_complete,
747     .set_client_capabilities = interface_set_client_capabilities,
748     .client_monitors_config  = interface_client_monitors_config,
749 };
750
751 static void display_update(DisplayChangeListener *dcl,
752                            int x, int y, int w, int h)
753 {
754     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
755     qemu_spice_display_update(ssd, x, y, w, h);
756 }
757
758 static void display_switch(DisplayChangeListener *dcl,
759                            DisplaySurface *surface)
760 {
761     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
762     qemu_spice_display_switch(ssd, surface);
763 }
764
765 static void display_refresh(DisplayChangeListener *dcl)
766 {
767     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
768     qemu_spice_display_refresh(ssd);
769 }
770
771 static void display_mouse_set(DisplayChangeListener *dcl,
772                               int x, int y, int on)
773 {
774     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
775
776     qemu_mutex_lock(&ssd->lock);
777     ssd->ptr_x = x;
778     ssd->ptr_y = y;
779     if (ssd->ptr_move) {
780         g_free(ssd->ptr_move);
781     }
782     ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL, on);
783     qemu_mutex_unlock(&ssd->lock);
784 }
785
786 static void display_mouse_define(DisplayChangeListener *dcl,
787                                  QEMUCursor *c)
788 {
789     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
790
791     qemu_mutex_lock(&ssd->lock);
792     ssd->hot_x = c->hot_x;
793     ssd->hot_y = c->hot_y;
794     if (ssd->ptr_move) {
795         g_free(ssd->ptr_move);
796         ssd->ptr_move = NULL;
797     }
798     if (ssd->ptr_define) {
799         g_free(ssd->ptr_define);
800     }
801     ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c, 0);
802     qemu_mutex_unlock(&ssd->lock);
803 }
804
805 static const DisplayChangeListenerOps display_listener_ops = {
806     .dpy_name             = "spice",
807     .dpy_gfx_update       = display_update,
808     .dpy_gfx_switch       = display_switch,
809     .dpy_gfx_check_format = qemu_pixman_check_format,
810     .dpy_refresh          = display_refresh,
811     .dpy_mouse_set        = display_mouse_set,
812     .dpy_cursor_define    = display_mouse_define,
813 };
814
815 static void qemu_spice_display_init_one(QemuConsole *con)
816 {
817     SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
818
819     qemu_spice_display_init_common(ssd);
820
821     ssd->qxl.base.sif = &dpy_interface.base;
822     qemu_spice_add_display_interface(&ssd->qxl, con);
823     assert(ssd->worker);
824
825     qemu_spice_create_host_memslot(ssd);
826
827     ssd->dcl.ops = &display_listener_ops;
828     ssd->dcl.con = con;
829     register_displaychangelistener(&ssd->dcl);
830 }
831
832 void qemu_spice_display_init(void)
833 {
834     QemuConsole *con;
835     int i;
836
837     for (i = 0;; i++) {
838         con = qemu_console_lookup_by_index(i);
839         if (!con || !qemu_console_is_graphic(con)) {
840             break;
841         }
842         if (qemu_spice_have_display_interface(con)) {
843             continue;
844         }
845         qemu_spice_display_init_one(con);
846     }
847 }