Merge branch 'tizen_3.0_qemu_2.5' 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         surface_format(surface) == pixman_image_get_format(ssd->surface)) {
452         /* no-resize fast path: just swap backing store */
453         dprint(1, "%s/%d: fast (%dx%d)\n", __func__, ssd->qxl.id,
454                surface_width(surface), surface_height(surface));
455         qemu_mutex_lock(&ssd->lock);
456         ssd->ds = surface;
457         pixman_image_unref(ssd->surface);
458         ssd->surface = pixman_image_ref(ssd->ds->image);
459         qemu_mutex_unlock(&ssd->lock);
460         qemu_spice_display_update(ssd, 0, 0,
461                                   surface_width(surface),
462                                   surface_height(surface));
463         return;
464     }
465
466     /* full mode switch */
467     dprint(1, "%s/%d: full (%dx%d -> %dx%d)\n", __func__, ssd->qxl.id,
468            ssd->surface ? pixman_image_get_width(ssd->surface)  : 0,
469            ssd->surface ? pixman_image_get_height(ssd->surface) : 0,
470            surface ? surface_width(surface)  : 0,
471            surface ? surface_height(surface) : 0);
472
473     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
474     if (ssd->surface) {
475         pixman_image_unref(ssd->surface);
476         ssd->surface = NULL;
477         pixman_image_unref(ssd->mirror);
478         ssd->mirror = NULL;
479     }
480
481     qemu_mutex_lock(&ssd->lock);
482     need_destroy = (ssd->ds != NULL);
483     ssd->ds = surface;
484     while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
485         QTAILQ_REMOVE(&ssd->updates, update, next);
486         qemu_spice_destroy_update(ssd, update);
487     }
488     qemu_mutex_unlock(&ssd->lock);
489     if (need_destroy) {
490         qemu_spice_destroy_host_primary(ssd);
491     }
492     if (ssd->ds) {
493         ssd->surface = pixman_image_ref(ssd->ds->image);
494         ssd->mirror  = qemu_pixman_mirror_create(ssd->ds->format,
495                                                  ssd->ds->image);
496         qemu_spice_create_host_primary(ssd);
497     }
498
499     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
500     ssd->notify++;
501 }
502
503 static void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
504 {
505     if (ssd->cursor) {
506         assert(ssd->dcl.con);
507         dpy_cursor_define(ssd->dcl.con, ssd->cursor);
508         cursor_put(ssd->cursor);
509         ssd->cursor = NULL;
510     }
511     if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
512         assert(ssd->dcl.con);
513         dpy_mouse_set(ssd->dcl.con, ssd->mouse_x, ssd->mouse_y, 1);
514         ssd->mouse_x = -1;
515         ssd->mouse_y = -1;
516     }
517 }
518
519 void qemu_spice_cursor_refresh_bh(void *opaque)
520 {
521     SimpleSpiceDisplay *ssd = opaque;
522
523     qemu_mutex_lock(&ssd->lock);
524     qemu_spice_cursor_refresh_unlocked(ssd);
525     qemu_mutex_unlock(&ssd->lock);
526 }
527
528 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
529 {
530     dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
531     graphic_hw_update(ssd->dcl.con);
532
533     qemu_mutex_lock(&ssd->lock);
534     if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
535         qemu_spice_create_update(ssd);
536         ssd->notify++;
537     }
538     qemu_mutex_unlock(&ssd->lock);
539
540     if (ssd->notify) {
541         ssd->notify = 0;
542         qemu_spice_wakeup(ssd);
543         dprint(2, "%s/%d: notify\n", __func__, ssd->qxl.id);
544     }
545 }
546
547 /* spice display interface callbacks */
548
549 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
550 {
551     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
552
553     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
554     ssd->worker = qxl_worker;
555 }
556
557 static void interface_set_compression_level(QXLInstance *sin, int level)
558 {
559     dprint(1, "%s/%d:\n", __func__, sin->id);
560     /* nothing to do */
561 }
562
563 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
564 {
565     dprint(3, "%s/%d:\n", __func__, sin->id);
566     /* nothing to do */
567 }
568
569 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
570 {
571     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
572
573     info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
574     info->memslot_id_bits  = MEMSLOT_SLOT_BITS;
575     info->num_memslots = NUM_MEMSLOTS;
576     info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
577     info->internal_groupslot_id = 0;
578     info->qxl_ram_size = 16 * 1024 * 1024;
579     info->n_surfaces = ssd->num_surfaces;
580 }
581
582 static int interface_get_command(QXLInstance *sin, QXLCommandExt *ext)
583 {
584     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
585     SimpleSpiceUpdate *update;
586     int ret = false;
587
588     dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
589
590     qemu_mutex_lock(&ssd->lock);
591     update = QTAILQ_FIRST(&ssd->updates);
592     if (update != NULL) {
593         QTAILQ_REMOVE(&ssd->updates, update, next);
594         *ext = update->ext;
595         ret = true;
596     }
597     qemu_mutex_unlock(&ssd->lock);
598
599     return ret;
600 }
601
602 static int interface_req_cmd_notification(QXLInstance *sin)
603 {
604     dprint(1, "%s/%d:\n", __func__, sin->id);
605     return 1;
606 }
607
608 static void interface_release_resource(QXLInstance *sin,
609                                        QXLReleaseInfoExt rext)
610 {
611     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
612     SimpleSpiceUpdate *update;
613     SimpleSpiceCursor *cursor;
614     QXLCommandExt *ext;
615
616     dprint(2, "%s/%d:\n", __func__, ssd->qxl.id);
617     ext = (void *)(intptr_t)(rext.info->id);
618     switch (ext->cmd.type) {
619     case QXL_CMD_DRAW:
620         update = container_of(ext, SimpleSpiceUpdate, ext);
621         qemu_spice_destroy_update(ssd, update);
622         break;
623     case QXL_CMD_CURSOR:
624         cursor = container_of(ext, SimpleSpiceCursor, ext);
625         g_free(cursor);
626         break;
627     default:
628         g_assert_not_reached();
629     }
630 }
631
632 static int interface_get_cursor_command(QXLInstance *sin, QXLCommandExt *ext)
633 {
634     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
635     int ret;
636
637     dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
638
639     qemu_mutex_lock(&ssd->lock);
640     if (ssd->ptr_define) {
641         *ext = ssd->ptr_define->ext;
642         ssd->ptr_define = NULL;
643         ret = true;
644     } else if (ssd->ptr_move) {
645         *ext = ssd->ptr_move->ext;
646         ssd->ptr_move = NULL;
647         ret = true;
648     } else {
649         ret = false;
650     }
651     qemu_mutex_unlock(&ssd->lock);
652     return ret;
653 }
654
655 static int interface_req_cursor_notification(QXLInstance *sin)
656 {
657     dprint(1, "%s:\n", __FUNCTION__);
658     return 1;
659 }
660
661 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
662 {
663     fprintf(stderr, "%s: abort()\n", __FUNCTION__);
664     abort();
665 }
666
667 static int interface_flush_resources(QXLInstance *sin)
668 {
669     fprintf(stderr, "%s: abort()\n", __FUNCTION__);
670     abort();
671     return 0;
672 }
673
674 static void interface_update_area_complete(QXLInstance *sin,
675         uint32_t surface_id,
676         QXLRect *dirty, uint32_t num_updated_rects)
677 {
678     /* should never be called, used in qxl native mode only */
679     fprintf(stderr, "%s: abort()\n", __func__);
680     abort();
681 }
682
683 /* called from spice server thread context only */
684 static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
685 {
686     /* should never be called, used in qxl native mode only */
687     fprintf(stderr, "%s: abort()\n", __func__);
688     abort();
689 }
690
691 static void interface_set_client_capabilities(QXLInstance *sin,
692                                               uint8_t client_present,
693                                               uint8_t caps[58])
694 {
695     dprint(3, "%s:\n", __func__);
696 }
697
698 static int interface_client_monitors_config(QXLInstance *sin,
699                                             VDAgentMonitorsConfig *mc)
700 {
701     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
702     QemuUIInfo info;
703
704     if (!dpy_ui_info_supported(ssd->dcl.con)) {
705         return 0; /* == not supported by guest */
706     }
707
708     if (!mc) {
709         return 1;
710     }
711
712     /*
713      * FIXME: multihead is tricky due to the way
714      * spice has multihead implemented.
715      */
716     memset(&info, 0, sizeof(info));
717     if (mc->num_of_monitors > 0) {
718         info.width  = mc->monitors[0].width;
719         info.height = mc->monitors[0].height;
720     }
721     dpy_set_ui_info(ssd->dcl.con, &info);
722     dprint(1, "%s/%d: size %dx%d\n", __func__, ssd->qxl.id,
723            info.width, info.height);
724     return 1;
725 }
726
727 static const QXLInterface dpy_interface = {
728     .base.type               = SPICE_INTERFACE_QXL,
729     .base.description        = "qemu simple display",
730     .base.major_version      = SPICE_INTERFACE_QXL_MAJOR,
731     .base.minor_version      = SPICE_INTERFACE_QXL_MINOR,
732
733     .attache_worker          = interface_attach_worker,
734     .set_compression_level   = interface_set_compression_level,
735     .set_mm_time             = interface_set_mm_time,
736     .get_init_info           = interface_get_init_info,
737
738     /* the callbacks below are called from spice server thread context */
739     .get_command             = interface_get_command,
740     .req_cmd_notification    = interface_req_cmd_notification,
741     .release_resource        = interface_release_resource,
742     .get_cursor_command      = interface_get_cursor_command,
743     .req_cursor_notification = interface_req_cursor_notification,
744     .notify_update           = interface_notify_update,
745     .flush_resources         = interface_flush_resources,
746     .async_complete          = interface_async_complete,
747     .update_area_complete    = interface_update_area_complete,
748     .set_client_capabilities = interface_set_client_capabilities,
749     .client_monitors_config  = interface_client_monitors_config,
750 };
751
752 static void display_update(DisplayChangeListener *dcl,
753                            int x, int y, int w, int h)
754 {
755     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
756     qemu_spice_display_update(ssd, x, y, w, h);
757 }
758
759 static void display_switch(DisplayChangeListener *dcl,
760                            DisplaySurface *surface)
761 {
762     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
763     qemu_spice_display_switch(ssd, surface);
764 }
765
766 static void display_refresh(DisplayChangeListener *dcl)
767 {
768     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
769     qemu_spice_display_refresh(ssd);
770 }
771
772 static void display_mouse_set(DisplayChangeListener *dcl,
773                               int x, int y, int on)
774 {
775     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
776
777     qemu_mutex_lock(&ssd->lock);
778     ssd->ptr_x = x;
779     ssd->ptr_y = y;
780     g_free(ssd->ptr_move);
781     ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL, on);
782     qemu_mutex_unlock(&ssd->lock);
783 }
784
785 static void display_mouse_define(DisplayChangeListener *dcl,
786                                  QEMUCursor *c)
787 {
788     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
789
790     qemu_mutex_lock(&ssd->lock);
791     ssd->hot_x = c->hot_x;
792     ssd->hot_y = c->hot_y;
793     g_free(ssd->ptr_move);
794     ssd->ptr_move = NULL;
795     g_free(ssd->ptr_define);
796     ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c, 0);
797     qemu_mutex_unlock(&ssd->lock);
798 }
799
800 static const DisplayChangeListenerOps display_listener_ops = {
801     .dpy_name             = "spice",
802     .dpy_gfx_update       = display_update,
803     .dpy_gfx_switch       = display_switch,
804     .dpy_gfx_check_format = qemu_pixman_check_format,
805     .dpy_refresh          = display_refresh,
806     .dpy_mouse_set        = display_mouse_set,
807     .dpy_cursor_define    = display_mouse_define,
808 };
809
810 static void qemu_spice_display_init_one(QemuConsole *con)
811 {
812     SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
813
814     qemu_spice_display_init_common(ssd);
815
816     ssd->qxl.base.sif = &dpy_interface.base;
817     qemu_spice_add_display_interface(&ssd->qxl, con);
818     assert(ssd->worker);
819
820     qemu_spice_create_host_memslot(ssd);
821
822     ssd->dcl.ops = &display_listener_ops;
823     ssd->dcl.con = con;
824     register_displaychangelistener(&ssd->dcl);
825 }
826
827 void qemu_spice_display_init(void)
828 {
829     QemuConsole *con;
830     int i;
831
832     for (i = 0;; i++) {
833         con = qemu_console_lookup_by_index(i);
834         if (!con || !qemu_console_is_graphic(con)) {
835             break;
836         }
837         if (qemu_spice_have_display_interface(con)) {
838             continue;
839         }
840         qemu_spice_display_init_one(con);
841     }
842 }