Revert "yagl: fix the display broken issue"
[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/osdep.h"
19 #include "qemu-common.h"
20 #include "ui/qemu-spice.h"
21 #include "qemu/timer.h"
22 #include "qemu/queue.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->ext);
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_LE_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, yoff1, yoff2, 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         yoff1 = y * surface_stride(ssd->ds);
231         yoff2 = y * pixman_image_get_stride(ssd->mirror);
232         for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
233             xoff = x * bpp;
234             blk = x / blksize;
235             bw = MIN(blksize, ssd->dirty.right - x);
236             if (memcmp(guest + yoff1 + xoff,
237                        mirror + yoff2 + xoff,
238                        bw * bpp) == 0) {
239                 if (dirty_top[blk] != -1) {
240                     QXLRect update = {
241                         .top    = dirty_top[blk],
242                         .bottom = y,
243                         .left   = x,
244                         .right  = x + bw,
245                     };
246                     qemu_spice_create_one_update(ssd, &update);
247                     dirty_top[blk] = -1;
248                 }
249             } else {
250                 if (dirty_top[blk] == -1) {
251                     dirty_top[blk] = y;
252                 }
253             }
254         }
255     }
256
257     for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
258         blk = x / blksize;
259         bw = MIN(blksize, ssd->dirty.right - x);
260         if (dirty_top[blk] != -1) {
261             QXLRect update = {
262                 .top    = dirty_top[blk],
263                 .bottom = ssd->dirty.bottom,
264                 .left   = x,
265                 .right  = x + bw,
266             };
267             qemu_spice_create_one_update(ssd, &update);
268             dirty_top[blk] = -1;
269         }
270     }
271 #endif
272
273 #ifdef CONFIG_MARU
274     pixman_transform_t matrix;
275     struct pixman_f_transform matrix_f;
276
277     pixman_f_transform_init_identity(&matrix_f);
278     pixman_f_transform_scale(&matrix_f, NULL, RATIO_SCALE, RATIO_SCALE);
279     pixman_transform_from_pixman_f_transform(&matrix, &matrix_f);
280     pixman_image_set_transform(ssd->ds->image, &matrix);
281     pixman_image_set_filter(ssd->ds->image, PIXMAN_FILTER_BEST, NULL, 0);
282
283     QXLRect update = {
284         .top    = 0,
285         .bottom = surface_height(ssd->ds),
286         .left   = 0,
287         .right  = surface_width(ssd->ds),
288     };
289
290     qemu_spice_create_one_update(ssd, &update);
291 #endif
292
293     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
294 }
295
296 static SimpleSpiceCursor*
297 qemu_spice_create_cursor_update(SimpleSpiceDisplay *ssd,
298                                 QEMUCursor *c,
299                                 int on)
300 {
301     size_t size = c ? c->width * c->height * 4 : 0;
302     SimpleSpiceCursor *update;
303     QXLCursorCmd *ccmd;
304     QXLCursor *cursor;
305     QXLCommand *cmd;
306
307     update   = g_malloc0(sizeof(*update) + size);
308     ccmd     = &update->cmd;
309     cursor   = &update->cursor;
310     cmd      = &update->ext.cmd;
311
312     if (c) {
313         ccmd->type = QXL_CURSOR_SET;
314         ccmd->u.set.position.x = ssd->ptr_x + ssd->hot_x;
315         ccmd->u.set.position.y = ssd->ptr_y + ssd->hot_y;
316         ccmd->u.set.visible    = true;
317         ccmd->u.set.shape      = (uintptr_t)cursor;
318         cursor->header.unique     = ssd->unique++;
319         cursor->header.type       = SPICE_CURSOR_TYPE_ALPHA;
320         cursor->header.width      = c->width;
321         cursor->header.height     = c->height;
322         cursor->header.hot_spot_x = c->hot_x;
323         cursor->header.hot_spot_y = c->hot_y;
324         cursor->data_size         = size;
325         cursor->chunk.data_size   = size;
326         memcpy(cursor->chunk.data, c->data, size);
327     } else if (!on) {
328         ccmd->type = QXL_CURSOR_HIDE;
329     } else {
330         ccmd->type = QXL_CURSOR_MOVE;
331         ccmd->u.position.x = ssd->ptr_x + ssd->hot_x;
332         ccmd->u.position.y = ssd->ptr_y + ssd->hot_y;
333     }
334     ccmd->release_info.id = (uintptr_t)(&update->ext);
335
336     cmd->type = QXL_CMD_CURSOR;
337     cmd->data = (uintptr_t)ccmd;
338
339     return update;
340 }
341
342 /*
343  * Called from spice server thread context (via interface_release_resource)
344  * We do *not* hold the global qemu mutex here, so extra care is needed
345  * when calling qemu functions.  QEMU interfaces used:
346  *    - g_free (underlying glibc free is re-entrant).
347  */
348 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
349 {
350     g_free(update->bitmap);
351     g_free(update);
352 }
353
354 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
355 {
356     QXLDevMemSlot memslot;
357
358     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
359
360     memset(&memslot, 0, sizeof(memslot));
361     memslot.slot_group_id = MEMSLOT_GROUP_HOST;
362     memslot.virt_end = ~0;
363     qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
364 }
365
366 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
367 {
368     QXLDevSurfaceCreate surface;
369     uint64_t surface_size;
370
371     memset(&surface, 0, sizeof(surface));
372
373     surface_size = (uint64_t) surface_width(ssd->ds) *
374         surface_height(ssd->ds) * 4;
375     assert(surface_size > 0);
376     assert(surface_size < INT_MAX);
377     if (ssd->bufsize < surface_size) {
378         ssd->bufsize = surface_size;
379         g_free(ssd->buf);
380         ssd->buf = g_malloc(ssd->bufsize);
381     }
382
383     dprint(1, "%s/%d: %ux%u (size %" PRIu64 "/%d)\n", __func__, ssd->qxl.id,
384            surface_width(ssd->ds), surface_height(ssd->ds),
385            surface_size, ssd->bufsize);
386
387     surface.format     = SPICE_SURFACE_FMT_32_xRGB;
388     surface.width      = surface_width(ssd->ds);
389     surface.height     = surface_height(ssd->ds);
390     surface.stride     = -surface.width * 4;
391     surface.mouse_mode = true;
392     surface.flags      = 0;
393     surface.type       = 0;
394     surface.mem        = (uintptr_t)ssd->buf;
395     surface.group_id   = MEMSLOT_GROUP_HOST;
396
397 #ifdef CONFIG_MARU
398     surface.width /= RATIO_SCALE;
399     surface.height /= RATIO_SCALE;
400 #endif
401
402     qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
403 }
404
405 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
406 {
407     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
408
409     qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
410 }
411
412 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
413 {
414     qemu_mutex_init(&ssd->lock);
415     QTAILQ_INIT(&ssd->updates);
416     ssd->mouse_x = -1;
417     ssd->mouse_y = -1;
418     if (ssd->num_surfaces == 0) {
419         ssd->num_surfaces = 1024;
420     }
421 }
422
423 /* display listener callbacks */
424
425 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
426                                int x, int y, int w, int h)
427 {
428     QXLRect update_area;
429
430     dprint(2, "%s/%d: x %d y %d w %d h %d\n", __func__,
431            ssd->qxl.id, x, y, w, h);
432     update_area.left = x,
433     update_area.right = x + w;
434     update_area.top = y;
435     update_area.bottom = y + h;
436
437     if (qemu_spice_rect_is_empty(&ssd->dirty)) {
438         ssd->notify++;
439     }
440     qemu_spice_rect_union(&ssd->dirty, &update_area);
441 }
442
443 void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
444                                DisplaySurface *surface)
445 {
446     SimpleSpiceUpdate *update;
447     bool need_destroy;
448
449     if (surface && ssd->surface &&
450         surface_width(surface) == pixman_image_get_width(ssd->surface) &&
451         surface_height(surface) == pixman_image_get_height(ssd->surface) &&
452         surface_format(surface) == pixman_image_get_format(ssd->surface)) {
453         /* no-resize fast path: just swap backing store */
454         dprint(1, "%s/%d: fast (%dx%d)\n", __func__, ssd->qxl.id,
455                surface_width(surface), surface_height(surface));
456         qemu_mutex_lock(&ssd->lock);
457         ssd->ds = surface;
458         pixman_image_unref(ssd->surface);
459         ssd->surface = pixman_image_ref(ssd->ds->image);
460         qemu_mutex_unlock(&ssd->lock);
461         qemu_spice_display_update(ssd, 0, 0,
462                                   surface_width(surface),
463                                   surface_height(surface));
464         return;
465     }
466
467     /* full mode switch */
468     dprint(1, "%s/%d: full (%dx%d -> %dx%d)\n", __func__, ssd->qxl.id,
469            ssd->surface ? pixman_image_get_width(ssd->surface)  : 0,
470            ssd->surface ? pixman_image_get_height(ssd->surface) : 0,
471            surface ? surface_width(surface)  : 0,
472            surface ? surface_height(surface) : 0);
473
474     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
475     if (ssd->surface) {
476         pixman_image_unref(ssd->surface);
477         ssd->surface = NULL;
478         pixman_image_unref(ssd->mirror);
479         ssd->mirror = NULL;
480     }
481
482     qemu_mutex_lock(&ssd->lock);
483     need_destroy = (ssd->ds != NULL);
484     ssd->ds = surface;
485     while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
486         QTAILQ_REMOVE(&ssd->updates, update, next);
487         qemu_spice_destroy_update(ssd, update);
488     }
489     qemu_mutex_unlock(&ssd->lock);
490     if (need_destroy) {
491         qemu_spice_destroy_host_primary(ssd);
492     }
493     if (ssd->ds) {
494         ssd->surface = pixman_image_ref(ssd->ds->image);
495         ssd->mirror  = qemu_pixman_mirror_create(ssd->ds->format,
496                                                  ssd->ds->image);
497         qemu_spice_create_host_primary(ssd);
498     }
499
500     memset(&ssd->dirty, 0, sizeof(ssd->dirty));
501     ssd->notify++;
502
503     qemu_mutex_lock(&ssd->lock);
504     if (ssd->cursor) {
505         g_free(ssd->ptr_define);
506         ssd->ptr_define = qemu_spice_create_cursor_update(ssd, ssd->cursor, 0);
507     }
508     qemu_mutex_unlock(&ssd->lock);
509 }
510
511 static void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
512 {
513     if (ssd->cursor) {
514         assert(ssd->dcl.con);
515         dpy_cursor_define(ssd->dcl.con, ssd->cursor);
516     }
517     if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
518         assert(ssd->dcl.con);
519         dpy_mouse_set(ssd->dcl.con, ssd->mouse_x, ssd->mouse_y, 1);
520         ssd->mouse_x = -1;
521         ssd->mouse_y = -1;
522     }
523 }
524
525 void qemu_spice_cursor_refresh_bh(void *opaque)
526 {
527     SimpleSpiceDisplay *ssd = opaque;
528
529     qemu_mutex_lock(&ssd->lock);
530     qemu_spice_cursor_refresh_unlocked(ssd);
531     qemu_mutex_unlock(&ssd->lock);
532 }
533
534 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
535 {
536     dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
537     graphic_hw_update(ssd->dcl.con);
538
539     qemu_mutex_lock(&ssd->lock);
540     if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
541         qemu_spice_create_update(ssd);
542         ssd->notify++;
543     }
544     qemu_mutex_unlock(&ssd->lock);
545
546     if (ssd->notify) {
547         ssd->notify = 0;
548         qemu_spice_wakeup(ssd);
549         dprint(2, "%s/%d: notify\n", __func__, ssd->qxl.id);
550     }
551 }
552
553 /* spice display interface callbacks */
554
555 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
556 {
557     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
558
559     dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
560     ssd->worker = qxl_worker;
561 }
562
563 static void interface_set_compression_level(QXLInstance *sin, int level)
564 {
565     dprint(1, "%s/%d:\n", __func__, sin->id);
566     /* nothing to do */
567 }
568
569 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
570 {
571     dprint(3, "%s/%d:\n", __func__, sin->id);
572     /* nothing to do */
573 }
574
575 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
576 {
577     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
578
579     info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
580     info->memslot_id_bits  = MEMSLOT_SLOT_BITS;
581     info->num_memslots = NUM_MEMSLOTS;
582     info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
583     info->internal_groupslot_id = 0;
584     info->qxl_ram_size = 16 * 1024 * 1024;
585     info->n_surfaces = ssd->num_surfaces;
586 }
587
588 static int interface_get_command(QXLInstance *sin, QXLCommandExt *ext)
589 {
590     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
591     SimpleSpiceUpdate *update;
592     int ret = false;
593
594     dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
595
596     qemu_mutex_lock(&ssd->lock);
597     update = QTAILQ_FIRST(&ssd->updates);
598     if (update != NULL) {
599         QTAILQ_REMOVE(&ssd->updates, update, next);
600         *ext = update->ext;
601         ret = true;
602     }
603     qemu_mutex_unlock(&ssd->lock);
604
605     return ret;
606 }
607
608 static int interface_req_cmd_notification(QXLInstance *sin)
609 {
610     dprint(2, "%s/%d:\n", __func__, sin->id);
611     return 1;
612 }
613
614 static void interface_release_resource(QXLInstance *sin,
615                                        QXLReleaseInfoExt rext)
616 {
617     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
618     SimpleSpiceUpdate *update;
619     SimpleSpiceCursor *cursor;
620     QXLCommandExt *ext;
621
622     dprint(2, "%s/%d:\n", __func__, ssd->qxl.id);
623     ext = (void *)(intptr_t)(rext.info->id);
624     switch (ext->cmd.type) {
625     case QXL_CMD_DRAW:
626         update = container_of(ext, SimpleSpiceUpdate, ext);
627         qemu_spice_destroy_update(ssd, update);
628         break;
629     case QXL_CMD_CURSOR:
630         cursor = container_of(ext, SimpleSpiceCursor, ext);
631         g_free(cursor);
632         break;
633     default:
634         g_assert_not_reached();
635     }
636 }
637
638 static int interface_get_cursor_command(QXLInstance *sin, QXLCommandExt *ext)
639 {
640     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
641     int ret;
642
643     dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
644
645     qemu_mutex_lock(&ssd->lock);
646     if (ssd->ptr_define) {
647         *ext = ssd->ptr_define->ext;
648         ssd->ptr_define = NULL;
649         ret = true;
650     } else if (ssd->ptr_move) {
651         *ext = ssd->ptr_move->ext;
652         ssd->ptr_move = NULL;
653         ret = true;
654     } else {
655         ret = false;
656     }
657     qemu_mutex_unlock(&ssd->lock);
658     return ret;
659 }
660
661 static int interface_req_cursor_notification(QXLInstance *sin)
662 {
663     dprint(2, "%s:\n", __func__);
664     return 1;
665 }
666
667 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
668 {
669     fprintf(stderr, "%s: abort()\n", __FUNCTION__);
670     abort();
671 }
672
673 static int interface_flush_resources(QXLInstance *sin)
674 {
675     fprintf(stderr, "%s: abort()\n", __FUNCTION__);
676     abort();
677     return 0;
678 }
679
680 static void interface_update_area_complete(QXLInstance *sin,
681         uint32_t surface_id,
682         QXLRect *dirty, uint32_t num_updated_rects)
683 {
684     /* should never be called, used in qxl native mode only */
685     fprintf(stderr, "%s: abort()\n", __func__);
686     abort();
687 }
688
689 /* called from spice server thread context only */
690 static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
691 {
692     QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
693
694     switch (cookie->type) {
695 #ifdef HAVE_SPICE_GL
696     case QXL_COOKIE_TYPE_GL_DRAW_DONE:
697     {
698         SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
699         qemu_bh_schedule(ssd->gl_unblock_bh);
700         break;
701     }
702 #endif
703     default:
704         /* should never be called, used in qxl native mode only */
705         fprintf(stderr, "%s: abort()\n", __func__);
706         abort();
707     }
708     g_free(cookie);
709 }
710
711 static void interface_set_client_capabilities(QXLInstance *sin,
712                                               uint8_t client_present,
713                                               uint8_t caps[58])
714 {
715     dprint(3, "%s:\n", __func__);
716 }
717
718 static int interface_client_monitors_config(QXLInstance *sin,
719                                             VDAgentMonitorsConfig *mc)
720 {
721     SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
722     QemuUIInfo info;
723
724     if (!dpy_ui_info_supported(ssd->dcl.con)) {
725         return 0; /* == not supported by guest */
726     }
727
728     if (!mc) {
729         return 1;
730     }
731
732     /*
733      * FIXME: multihead is tricky due to the way
734      * spice has multihead implemented.
735      */
736     memset(&info, 0, sizeof(info));
737     if (mc->num_of_monitors > 0) {
738         info.width  = mc->monitors[0].width;
739         info.height = mc->monitors[0].height;
740     }
741     dpy_set_ui_info(ssd->dcl.con, &info);
742     dprint(1, "%s/%d: size %dx%d\n", __func__, ssd->qxl.id,
743            info.width, info.height);
744     return 1;
745 }
746
747 static const QXLInterface dpy_interface = {
748     .base.type               = SPICE_INTERFACE_QXL,
749     .base.description        = "qemu simple display",
750     .base.major_version      = SPICE_INTERFACE_QXL_MAJOR,
751     .base.minor_version      = SPICE_INTERFACE_QXL_MINOR,
752
753     .attache_worker          = interface_attach_worker,
754     .set_compression_level   = interface_set_compression_level,
755     .set_mm_time             = interface_set_mm_time,
756     .get_init_info           = interface_get_init_info,
757
758     /* the callbacks below are called from spice server thread context */
759     .get_command             = interface_get_command,
760     .req_cmd_notification    = interface_req_cmd_notification,
761     .release_resource        = interface_release_resource,
762     .get_cursor_command      = interface_get_cursor_command,
763     .req_cursor_notification = interface_req_cursor_notification,
764     .notify_update           = interface_notify_update,
765     .flush_resources         = interface_flush_resources,
766     .async_complete          = interface_async_complete,
767     .update_area_complete    = interface_update_area_complete,
768     .set_client_capabilities = interface_set_client_capabilities,
769     .client_monitors_config  = interface_client_monitors_config,
770 };
771
772 static void display_update(DisplayChangeListener *dcl,
773                            int x, int y, int w, int h)
774 {
775     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
776     qemu_spice_display_update(ssd, x, y, w, h);
777 }
778
779 static void display_switch(DisplayChangeListener *dcl,
780                            DisplaySurface *surface)
781 {
782     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
783     qemu_spice_display_switch(ssd, surface);
784 }
785
786 static void display_refresh(DisplayChangeListener *dcl)
787 {
788     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
789     qemu_spice_display_refresh(ssd);
790 }
791
792 static void display_mouse_set(DisplayChangeListener *dcl,
793                               int x, int y, int on)
794 {
795     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
796
797     qemu_mutex_lock(&ssd->lock);
798     ssd->ptr_x = x;
799     ssd->ptr_y = y;
800     g_free(ssd->ptr_move);
801     ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL, on);
802     qemu_mutex_unlock(&ssd->lock);
803 }
804
805 static void display_mouse_define(DisplayChangeListener *dcl,
806                                  QEMUCursor *c)
807 {
808     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
809
810     qemu_mutex_lock(&ssd->lock);
811     if (c) {
812         cursor_get(c);
813     }
814     cursor_put(ssd->cursor);
815     ssd->cursor = c;
816     ssd->hot_x = c->hot_x;
817     ssd->hot_y = c->hot_y;
818     g_free(ssd->ptr_move);
819     ssd->ptr_move = NULL;
820     g_free(ssd->ptr_define);
821     ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c, 0);
822     qemu_mutex_unlock(&ssd->lock);
823 }
824
825 static const DisplayChangeListenerOps display_listener_ops = {
826     .dpy_name             = "spice",
827     .dpy_gfx_update       = display_update,
828     .dpy_gfx_switch       = display_switch,
829     .dpy_gfx_check_format = qemu_pixman_check_format,
830     .dpy_refresh          = display_refresh,
831     .dpy_mouse_set        = display_mouse_set,
832     .dpy_cursor_define    = display_mouse_define,
833 };
834
835 #ifdef HAVE_SPICE_GL
836
837 static void qemu_spice_gl_block(SimpleSpiceDisplay *ssd, bool block)
838 {
839     uint64_t timeout;
840
841     if (block) {
842         timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
843         timeout += 1000; /* one sec */
844         timer_mod(ssd->gl_unblock_timer, timeout);
845     } else {
846         timer_del(ssd->gl_unblock_timer);
847     }
848     graphic_hw_gl_block(ssd->dcl.con, block);
849 }
850
851 static void qemu_spice_gl_unblock_bh(void *opaque)
852 {
853     SimpleSpiceDisplay *ssd = opaque;
854
855     qemu_spice_gl_block(ssd, false);
856 }
857
858 static void qemu_spice_gl_block_timer(void *opaque)
859 {
860     fprintf(stderr, "WARNING: spice: no gl-draw-done within one second\n");
861 }
862
863 static QEMUGLContext qemu_spice_gl_create_context(DisplayChangeListener *dcl,
864                                                   QEMUGLParams *params)
865 {
866     eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
867                    qemu_egl_rn_ctx);
868     return qemu_egl_create_context(dcl, params);
869 }
870
871 static void qemu_spice_gl_scanout(DisplayChangeListener *dcl,
872                                   uint32_t tex_id,
873                                   bool y_0_top,
874                                   uint32_t x, uint32_t y,
875                                   uint32_t w, uint32_t h)
876 {
877     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
878     EGLint stride = 0, fourcc = 0;
879     int fd = -1;
880
881     if (tex_id) {
882         fd = egl_get_fd_for_texture(tex_id, &stride, &fourcc);
883         if (fd < 0) {
884             fprintf(stderr, "%s: failed to get fd for texture\n", __func__);
885             return;
886         }
887         dprint(1, "%s: %dx%d (stride %d, fourcc 0x%x)\n", __func__,
888                w, h, stride, fourcc);
889     } else {
890         dprint(1, "%s: no texture (no framebuffer)\n", __func__);
891     }
892
893     assert(!tex_id || fd >= 0);
894
895     /* note: spice server will close the fd */
896     spice_qxl_gl_scanout(&ssd->qxl, fd,
897                          surface_width(ssd->ds),
898                          surface_height(ssd->ds),
899                          stride, fourcc, y_0_top);
900 }
901
902 static void qemu_spice_gl_update(DisplayChangeListener *dcl,
903                                  uint32_t x, uint32_t y, uint32_t w, uint32_t h)
904 {
905     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
906     uint64_t cookie;
907
908     dprint(2, "%s: %dx%d+%d+%d\n", __func__, w, h, x, y);
909     qemu_spice_gl_block(ssd, true);
910     cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
911     spice_qxl_gl_draw_async(&ssd->qxl, x, y, w, h, cookie);
912 }
913
914 static const DisplayChangeListenerOps display_listener_gl_ops = {
915     .dpy_name             = "spice-egl",
916     .dpy_gfx_update       = display_update,
917     .dpy_gfx_switch       = display_switch,
918     .dpy_gfx_check_format = qemu_pixman_check_format,
919     .dpy_refresh          = display_refresh,
920     .dpy_mouse_set        = display_mouse_set,
921     .dpy_cursor_define    = display_mouse_define,
922
923     .dpy_gl_ctx_create       = qemu_spice_gl_create_context,
924     .dpy_gl_ctx_destroy      = qemu_egl_destroy_context,
925     .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
926     .dpy_gl_ctx_get_current  = qemu_egl_get_current_context,
927
928     .dpy_gl_scanout          = qemu_spice_gl_scanout,
929     .dpy_gl_update           = qemu_spice_gl_update,
930 };
931
932 #endif /* HAVE_SPICE_GL */
933
934 static void qemu_spice_display_init_one(QemuConsole *con)
935 {
936     SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
937
938     qemu_spice_display_init_common(ssd);
939
940     ssd->dcl.ops = &display_listener_ops;
941 #ifdef HAVE_SPICE_GL
942     if (display_opengl) {
943         ssd->dcl.ops = &display_listener_gl_ops;
944         ssd->dmabuf_fd = -1;
945         ssd->gl_unblock_bh = qemu_bh_new(qemu_spice_gl_unblock_bh, ssd);
946         ssd->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
947                                              qemu_spice_gl_block_timer, ssd);
948     }
949 #endif
950     ssd->dcl.con = con;
951
952     ssd->qxl.base.sif = &dpy_interface.base;
953     qemu_spice_add_display_interface(&ssd->qxl, con);
954     assert(ssd->worker);
955     qemu_spice_create_host_memslot(ssd);
956
957     register_displaychangelistener(&ssd->dcl);
958 }
959
960 void qemu_spice_display_init(void)
961 {
962     QemuConsole *con;
963     int i;
964
965     for (i = 0;; i++) {
966         con = qemu_console_lookup_by_index(i);
967         if (!con || !qemu_console_is_graphic(con)) {
968             break;
969         }
970         if (qemu_spice_have_display_interface(con)) {
971             continue;
972         }
973         qemu_spice_display_init_one(con);
974     }
975 }