2 * Copyright (C) 2010 Red Hat, Inc.
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.
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.
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/>.
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"
27 #include "ui/spice-display.h"
35 static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
41 vfprintf(stderr, fmt, args);
46 int qemu_spice_rect_is_empty(const QXLRect* r)
48 return r->top == r->bottom || r->left == r->right;
51 void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
53 if (qemu_spice_rect_is_empty(r)) {
57 if (qemu_spice_rect_is_empty(dest)) {
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);
68 QXLCookie *qxl_cookie_new(int type, uint64_t io)
72 cookie = g_malloc0(sizeof(*cookie));
78 void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
81 trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
82 memslot->virt_start, memslot->virt_end,
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));
90 spice_qxl_add_memslot(&ssd->qxl, memslot);
94 void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
96 trace_qemu_spice_del_memslot(ssd->qxl.id, gid, sid);
97 spice_qxl_del_memslot(&ssd->qxl, gid, sid);
100 void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
101 QXLDevSurfaceCreate *surface,
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));
110 spice_qxl_create_primary_surface(&ssd->qxl, id, surface);
114 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
115 uint32_t id, qxl_async_io async)
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));
123 spice_qxl_destroy_primary_surface(&ssd->qxl, id);
127 void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
129 trace_qemu_spice_wakeup(ssd->qxl.id);
130 spice_qxl_wakeup(&ssd->qxl);
133 static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
136 SimpleSpiceUpdate *update;
137 QXLDrawable *drawable;
141 struct timespec time_space;
142 pixman_image_t *dest;
144 trace_qemu_spice_create_update(
145 rect->left, rect->right,
146 rect->top, rect->bottom);
148 update = g_malloc0(sizeof(*update));
149 drawable = &update->drawable;
150 image = &update->image;
151 cmd = &update->ext.cmd;
153 bw = rect->right - rect->left;
154 bh = rect->bottom - rect->top;
155 update->bitmap = g_malloc(bw * bh * 4);
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;
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;
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;
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,
193 pixman_image_unref(dest);
195 cmd->type = QXL_CMD_DRAW;
196 cmd->data = (uintptr_t)drawable;
198 QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
201 static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
204 static const int blksize = 32;
205 int blocks = DIV_ROUND_UP(surface_width(ssd->ds), 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;
212 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
216 if (ssd->surface == NULL) {
217 ssd->surface = pixman_image_ref(ssd->ds->image);
218 ssd->mirror = qemu_pixman_mirror_create(ssd->ds->format,
223 for (blk = 0; blk < blocks; blk++) {
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) {
235 bw = MIN(blksize, ssd->dirty.right - x);
236 if (memcmp(guest + yoff1 + xoff,
237 mirror + yoff2 + xoff,
239 if (dirty_top[blk] != -1) {
241 .top = dirty_top[blk],
246 qemu_spice_create_one_update(ssd, &update);
250 if (dirty_top[blk] == -1) {
257 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
259 bw = MIN(blksize, ssd->dirty.right - x);
260 if (dirty_top[blk] != -1) {
262 .top = dirty_top[blk],
263 .bottom = ssd->dirty.bottom,
267 qemu_spice_create_one_update(ssd, &update);
274 pixman_transform_t matrix;
275 struct pixman_f_transform matrix_f;
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);
285 .bottom = surface_height(ssd->ds),
287 .right = surface_width(ssd->ds),
290 qemu_spice_create_one_update(ssd, &update);
293 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
296 static SimpleSpiceCursor*
297 qemu_spice_create_cursor_update(SimpleSpiceDisplay *ssd,
301 size_t size = c ? c->width * c->height * 4 : 0;
302 SimpleSpiceCursor *update;
307 update = g_malloc0(sizeof(*update) + size);
309 cursor = &update->cursor;
310 cmd = &update->ext.cmd;
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);
328 ccmd->type = QXL_CURSOR_HIDE;
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;
334 ccmd->release_info.id = (uintptr_t)(&update->ext);
336 cmd->type = QXL_CMD_CURSOR;
337 cmd->data = (uintptr_t)ccmd;
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).
348 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
350 g_free(update->bitmap);
354 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
356 QXLDevMemSlot memslot;
358 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
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);
366 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
368 QXLDevSurfaceCreate surface;
369 uint64_t surface_size;
371 memset(&surface, 0, sizeof(surface));
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;
380 ssd->buf = g_malloc(ssd->bufsize);
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);
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;
394 surface.mem = (uintptr_t)ssd->buf;
395 surface.group_id = MEMSLOT_GROUP_HOST;
398 surface.width /= RATIO_SCALE;
399 surface.height /= RATIO_SCALE;
402 qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
405 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
407 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
409 qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
412 void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
414 qemu_mutex_init(&ssd->lock);
415 QTAILQ_INIT(&ssd->updates);
418 if (ssd->num_surfaces == 0) {
419 ssd->num_surfaces = 1024;
423 /* display listener callbacks */
425 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
426 int x, int y, int w, int h)
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;
435 update_area.bottom = y + h;
437 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
440 qemu_spice_rect_union(&ssd->dirty, &update_area);
443 void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
444 DisplaySurface *surface)
446 SimpleSpiceUpdate *update;
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);
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));
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);
474 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
476 pixman_image_unref(ssd->surface);
478 pixman_image_unref(ssd->mirror);
482 qemu_mutex_lock(&ssd->lock);
483 need_destroy = (ssd->ds != NULL);
485 while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
486 QTAILQ_REMOVE(&ssd->updates, update, next);
487 qemu_spice_destroy_update(ssd, update);
489 qemu_mutex_unlock(&ssd->lock);
491 qemu_spice_destroy_host_primary(ssd);
494 ssd->surface = pixman_image_ref(ssd->ds->image);
495 ssd->mirror = qemu_pixman_mirror_create(ssd->ds->format,
497 qemu_spice_create_host_primary(ssd);
500 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
503 qemu_mutex_lock(&ssd->lock);
505 g_free(ssd->ptr_define);
506 ssd->ptr_define = qemu_spice_create_cursor_update(ssd, ssd->cursor, 0);
508 qemu_mutex_unlock(&ssd->lock);
511 static void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
514 assert(ssd->dcl.con);
515 dpy_cursor_define(ssd->dcl.con, ssd->cursor);
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);
525 void qemu_spice_cursor_refresh_bh(void *opaque)
527 SimpleSpiceDisplay *ssd = opaque;
529 qemu_mutex_lock(&ssd->lock);
530 qemu_spice_cursor_refresh_unlocked(ssd);
531 qemu_mutex_unlock(&ssd->lock);
534 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
536 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
537 graphic_hw_update(ssd->dcl.con);
539 qemu_mutex_lock(&ssd->lock);
540 if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
541 qemu_spice_create_update(ssd);
544 qemu_mutex_unlock(&ssd->lock);
548 qemu_spice_wakeup(ssd);
549 dprint(2, "%s/%d: notify\n", __func__, ssd->qxl.id);
553 /* spice display interface callbacks */
555 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
557 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
559 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
560 ssd->worker = qxl_worker;
563 static void interface_set_compression_level(QXLInstance *sin, int level)
565 dprint(1, "%s/%d:\n", __func__, sin->id);
569 #if SPICE_NEEDS_SET_MM_TIME
570 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
572 dprint(3, "%s/%d:\n", __func__, sin->id);
577 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
579 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
581 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
582 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
583 info->num_memslots = NUM_MEMSLOTS;
584 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
585 info->internal_groupslot_id = 0;
586 info->qxl_ram_size = 16 * 1024 * 1024;
587 info->n_surfaces = ssd->num_surfaces;
590 static int interface_get_command(QXLInstance *sin, QXLCommandExt *ext)
592 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
593 SimpleSpiceUpdate *update;
596 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
598 qemu_mutex_lock(&ssd->lock);
599 update = QTAILQ_FIRST(&ssd->updates);
600 if (update != NULL) {
601 QTAILQ_REMOVE(&ssd->updates, update, next);
605 qemu_mutex_unlock(&ssd->lock);
610 static int interface_req_cmd_notification(QXLInstance *sin)
612 dprint(2, "%s/%d:\n", __func__, sin->id);
616 static void interface_release_resource(QXLInstance *sin,
617 QXLReleaseInfoExt rext)
619 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
620 SimpleSpiceUpdate *update;
621 SimpleSpiceCursor *cursor;
624 dprint(2, "%s/%d:\n", __func__, ssd->qxl.id);
625 ext = (void *)(intptr_t)(rext.info->id);
626 switch (ext->cmd.type) {
628 update = container_of(ext, SimpleSpiceUpdate, ext);
629 qemu_spice_destroy_update(ssd, update);
632 cursor = container_of(ext, SimpleSpiceCursor, ext);
636 g_assert_not_reached();
640 static int interface_get_cursor_command(QXLInstance *sin, QXLCommandExt *ext)
642 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
645 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
647 qemu_mutex_lock(&ssd->lock);
648 if (ssd->ptr_define) {
649 *ext = ssd->ptr_define->ext;
650 ssd->ptr_define = NULL;
652 } else if (ssd->ptr_move) {
653 *ext = ssd->ptr_move->ext;
654 ssd->ptr_move = NULL;
659 qemu_mutex_unlock(&ssd->lock);
663 static int interface_req_cursor_notification(QXLInstance *sin)
665 dprint(2, "%s:\n", __func__);
669 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
671 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
675 static int interface_flush_resources(QXLInstance *sin)
677 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
682 static void interface_update_area_complete(QXLInstance *sin,
684 QXLRect *dirty, uint32_t num_updated_rects)
686 /* should never be called, used in qxl native mode only */
687 fprintf(stderr, "%s: abort()\n", __func__);
691 /* called from spice server thread context only */
692 static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
694 QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
696 switch (cookie->type) {
698 case QXL_COOKIE_TYPE_GL_DRAW_DONE:
700 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
701 qemu_bh_schedule(ssd->gl_unblock_bh);
704 case QXL_COOKIE_TYPE_IO:
705 if (cookie->io == QXL_IO_MONITORS_CONFIG_ASYNC) {
706 g_free(cookie->u.data);
711 /* should never be called, used in qxl native mode only */
712 fprintf(stderr, "%s: abort()\n", __func__);
718 static void interface_set_client_capabilities(QXLInstance *sin,
719 uint8_t client_present,
722 dprint(3, "%s:\n", __func__);
725 static int interface_client_monitors_config(QXLInstance *sin,
726 VDAgentMonitorsConfig *mc)
728 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
732 if (!dpy_ui_info_supported(ssd->dcl.con)) {
733 return 0; /* == not supported by guest */
740 memset(&info, 0, sizeof(info));
742 head = qemu_console_get_head(ssd->dcl.con);
743 if (mc->num_of_monitors > head) {
744 info.width = mc->monitors[head].width;
745 info.height = mc->monitors[head].height;
747 dpy_set_ui_info(ssd->dcl.con, &info);
748 dprint(1, "%s/%d: size %dx%d\n", __func__, ssd->qxl.id,
749 info.width, info.height);
753 static const QXLInterface dpy_interface = {
754 .base.type = SPICE_INTERFACE_QXL,
755 .base.description = "qemu simple display",
756 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
757 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
759 .attache_worker = interface_attach_worker,
760 .set_compression_level = interface_set_compression_level,
761 #if SPICE_NEEDS_SET_MM_TIME
762 .set_mm_time = interface_set_mm_time,
764 .get_init_info = interface_get_init_info,
766 /* the callbacks below are called from spice server thread context */
767 .get_command = interface_get_command,
768 .req_cmd_notification = interface_req_cmd_notification,
769 .release_resource = interface_release_resource,
770 .get_cursor_command = interface_get_cursor_command,
771 .req_cursor_notification = interface_req_cursor_notification,
772 .notify_update = interface_notify_update,
773 .flush_resources = interface_flush_resources,
774 .async_complete = interface_async_complete,
775 .update_area_complete = interface_update_area_complete,
776 .set_client_capabilities = interface_set_client_capabilities,
777 .client_monitors_config = interface_client_monitors_config,
780 static void display_update(DisplayChangeListener *dcl,
781 int x, int y, int w, int h)
783 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
784 qemu_spice_display_update(ssd, x, y, w, h);
787 static void display_switch(DisplayChangeListener *dcl,
788 DisplaySurface *surface)
790 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
791 qemu_spice_display_switch(ssd, surface);
794 static void display_refresh(DisplayChangeListener *dcl)
796 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
797 qemu_spice_display_refresh(ssd);
800 static void display_mouse_set(DisplayChangeListener *dcl,
801 int x, int y, int on)
803 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
805 qemu_mutex_lock(&ssd->lock);
808 g_free(ssd->ptr_move);
809 ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL, on);
810 qemu_mutex_unlock(&ssd->lock);
813 static void display_mouse_define(DisplayChangeListener *dcl,
816 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
818 qemu_mutex_lock(&ssd->lock);
820 cursor_put(ssd->cursor);
822 ssd->hot_x = c->hot_x;
823 ssd->hot_y = c->hot_y;
824 g_free(ssd->ptr_move);
825 ssd->ptr_move = NULL;
826 g_free(ssd->ptr_define);
827 ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c, 0);
828 qemu_mutex_unlock(&ssd->lock);
831 static const DisplayChangeListenerOps display_listener_ops = {
833 .dpy_gfx_update = display_update,
834 .dpy_gfx_switch = display_switch,
835 .dpy_gfx_check_format = qemu_pixman_check_format,
836 .dpy_refresh = display_refresh,
837 .dpy_mouse_set = display_mouse_set,
838 .dpy_cursor_define = display_mouse_define,
843 static void qemu_spice_gl_monitor_config(SimpleSpiceDisplay *ssd,
844 int x, int y, int w, int h)
846 QXLMonitorsConfig *config;
849 config = g_malloc0(sizeof(QXLMonitorsConfig) + sizeof(QXLHead));
851 config->max_allowed = 1;
852 config->heads[0].x = x;
853 config->heads[0].y = y;
854 config->heads[0].width = w;
855 config->heads[0].height = h;
856 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
857 QXL_IO_MONITORS_CONFIG_ASYNC);
858 cookie->u.data = config;
860 spice_qxl_monitors_config_async(&ssd->qxl,
866 static void qemu_spice_gl_block(SimpleSpiceDisplay *ssd, bool block)
871 timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
872 timeout += 1000; /* one sec */
873 timer_mod(ssd->gl_unblock_timer, timeout);
875 timer_del(ssd->gl_unblock_timer);
877 graphic_hw_gl_block(ssd->dcl.con, block);
880 static void qemu_spice_gl_unblock_bh(void *opaque)
882 SimpleSpiceDisplay *ssd = opaque;
884 qemu_spice_gl_block(ssd, false);
887 static void qemu_spice_gl_block_timer(void *opaque)
889 fprintf(stderr, "WARNING: spice: no gl-draw-done within one second\n");
892 static void spice_gl_refresh(DisplayChangeListener *dcl)
894 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
897 if (!ssd->ds || qemu_console_is_gl_blocked(ssd->dcl.con)) {
901 graphic_hw_update(dcl->con);
902 if (ssd->gl_updates && ssd->have_surface) {
903 qemu_spice_gl_block(ssd, true);
904 cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
905 spice_qxl_gl_draw_async(&ssd->qxl, 0, 0,
906 surface_width(ssd->ds),
907 surface_height(ssd->ds),
913 static void spice_gl_update(DisplayChangeListener *dcl,
914 int x, int y, int w, int h)
916 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
918 surface_gl_update_texture(ssd->gls, ssd->ds, x, y, w, h);
922 static void spice_gl_switch(DisplayChangeListener *dcl,
923 struct DisplaySurface *new_surface)
925 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
926 EGLint stride, fourcc;
930 surface_gl_destroy_texture(ssd->gls, ssd->ds);
932 ssd->ds = new_surface;
934 surface_gl_create_texture(ssd->gls, ssd->ds);
935 fd = egl_get_fd_for_texture(ssd->ds->texture,
938 surface_gl_destroy_texture(ssd->gls, ssd->ds);
942 dprint(1, "%s: %dx%d (stride %d/%d, fourcc 0x%x)\n", __func__,
943 surface_width(ssd->ds), surface_height(ssd->ds),
944 surface_stride(ssd->ds), stride, fourcc);
946 /* note: spice server will close the fd */
947 spice_qxl_gl_scanout(&ssd->qxl, fd,
948 surface_width(ssd->ds),
949 surface_height(ssd->ds),
950 stride, fourcc, false);
951 ssd->have_surface = true;
952 ssd->have_scanout = false;
954 qemu_spice_gl_monitor_config(ssd, 0, 0,
955 surface_width(ssd->ds),
956 surface_height(ssd->ds));
960 static QEMUGLContext qemu_spice_gl_create_context(DisplayChangeListener *dcl,
961 QEMUGLParams *params)
963 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
965 return qemu_egl_create_context(dcl, params);
968 static void qemu_spice_gl_scanout(DisplayChangeListener *dcl,
971 uint32_t backing_width,
972 uint32_t backing_height,
973 uint32_t x, uint32_t y,
974 uint32_t w, uint32_t h)
976 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
977 EGLint stride = 0, fourcc = 0;
981 fd = egl_get_fd_for_texture(tex_id, &stride, &fourcc);
983 fprintf(stderr, "%s: failed to get fd for texture\n", __func__);
986 dprint(1, "%s: %dx%d (stride %d, fourcc 0x%x)\n", __func__,
987 w, h, stride, fourcc);
989 dprint(1, "%s: no texture (no framebuffer)\n", __func__);
992 assert(!tex_id || fd >= 0);
994 /* note: spice server will close the fd */
995 spice_qxl_gl_scanout(&ssd->qxl, fd, backing_width, backing_height,
996 stride, fourcc, y_0_top);
997 ssd->have_surface = false;
998 ssd->have_scanout = (tex_id != 0);
1000 qemu_spice_gl_monitor_config(ssd, x, y, w, h);
1003 static void qemu_spice_gl_update(DisplayChangeListener *dcl,
1004 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1006 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
1009 if (!ssd->have_scanout) {
1013 dprint(2, "%s: %dx%d+%d+%d\n", __func__, w, h, x, y);
1014 qemu_spice_gl_block(ssd, true);
1015 cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
1016 spice_qxl_gl_draw_async(&ssd->qxl, x, y, w, h, cookie);
1019 static const DisplayChangeListenerOps display_listener_gl_ops = {
1020 .dpy_name = "spice-egl",
1021 .dpy_gfx_update = spice_gl_update,
1022 .dpy_gfx_switch = spice_gl_switch,
1023 .dpy_gfx_check_format = console_gl_check_format,
1024 .dpy_refresh = spice_gl_refresh,
1025 .dpy_mouse_set = display_mouse_set,
1026 .dpy_cursor_define = display_mouse_define,
1028 .dpy_gl_ctx_create = qemu_spice_gl_create_context,
1029 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
1030 .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
1031 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
1033 .dpy_gl_scanout = qemu_spice_gl_scanout,
1034 .dpy_gl_update = qemu_spice_gl_update,
1037 #endif /* HAVE_SPICE_GL */
1039 static void qemu_spice_display_init_one(QemuConsole *con)
1041 SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
1043 qemu_spice_display_init_common(ssd);
1045 ssd->dcl.ops = &display_listener_ops;
1046 #ifdef HAVE_SPICE_GL
1047 if (display_opengl) {
1048 ssd->dcl.ops = &display_listener_gl_ops;
1049 ssd->gl_unblock_bh = qemu_bh_new(qemu_spice_gl_unblock_bh, ssd);
1050 ssd->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1051 qemu_spice_gl_block_timer, ssd);
1052 ssd->gls = console_gl_init_context();
1053 ssd->have_surface = false;
1054 ssd->have_scanout = false;
1059 ssd->qxl.base.sif = &dpy_interface.base;
1060 qemu_spice_add_display_interface(&ssd->qxl, con);
1061 assert(ssd->worker);
1062 qemu_spice_create_host_memslot(ssd);
1064 register_displaychangelistener(&ssd->dcl);
1067 void qemu_spice_display_init(void)
1073 con = qemu_console_lookup_by_index(i);
1074 if (!con || !qemu_console_is_graphic(con)) {
1077 if (qemu_spice_have_display_interface(con)) {
1080 qemu_spice_display_init_one(con);