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 = (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;
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 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
571 dprint(3, "%s/%d:\n", __func__, sin->id);
575 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
577 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
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;
588 static int interface_get_command(QXLInstance *sin, QXLCommandExt *ext)
590 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
591 SimpleSpiceUpdate *update;
594 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
596 qemu_mutex_lock(&ssd->lock);
597 update = QTAILQ_FIRST(&ssd->updates);
598 if (update != NULL) {
599 QTAILQ_REMOVE(&ssd->updates, update, next);
603 qemu_mutex_unlock(&ssd->lock);
608 static int interface_req_cmd_notification(QXLInstance *sin)
610 dprint(2, "%s/%d:\n", __func__, sin->id);
614 static void interface_release_resource(QXLInstance *sin,
615 QXLReleaseInfoExt rext)
617 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
618 SimpleSpiceUpdate *update;
619 SimpleSpiceCursor *cursor;
622 dprint(2, "%s/%d:\n", __func__, ssd->qxl.id);
623 ext = (void *)(intptr_t)(rext.info->id);
624 switch (ext->cmd.type) {
626 update = container_of(ext, SimpleSpiceUpdate, ext);
627 qemu_spice_destroy_update(ssd, update);
630 cursor = container_of(ext, SimpleSpiceCursor, ext);
634 g_assert_not_reached();
638 static int interface_get_cursor_command(QXLInstance *sin, QXLCommandExt *ext)
640 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
643 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
645 qemu_mutex_lock(&ssd->lock);
646 if (ssd->ptr_define) {
647 *ext = ssd->ptr_define->ext;
648 ssd->ptr_define = NULL;
650 } else if (ssd->ptr_move) {
651 *ext = ssd->ptr_move->ext;
652 ssd->ptr_move = NULL;
657 qemu_mutex_unlock(&ssd->lock);
661 static int interface_req_cursor_notification(QXLInstance *sin)
663 dprint(2, "%s:\n", __func__);
667 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
669 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
673 static int interface_flush_resources(QXLInstance *sin)
675 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
680 static void interface_update_area_complete(QXLInstance *sin,
682 QXLRect *dirty, uint32_t num_updated_rects)
684 /* should never be called, used in qxl native mode only */
685 fprintf(stderr, "%s: abort()\n", __func__);
689 /* called from spice server thread context only */
690 static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
692 QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
694 switch (cookie->type) {
696 case QXL_COOKIE_TYPE_GL_DRAW_DONE:
698 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
699 qemu_bh_schedule(ssd->gl_unblock_bh);
704 /* should never be called, used in qxl native mode only */
705 fprintf(stderr, "%s: abort()\n", __func__);
711 static void interface_set_client_capabilities(QXLInstance *sin,
712 uint8_t client_present,
715 dprint(3, "%s:\n", __func__);
718 static int interface_client_monitors_config(QXLInstance *sin,
719 VDAgentMonitorsConfig *mc)
721 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
724 if (!dpy_ui_info_supported(ssd->dcl.con)) {
725 return 0; /* == not supported by guest */
733 * FIXME: multihead is tricky due to the way
734 * spice has multihead implemented.
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;
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);
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,
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,
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,
772 static void display_update(DisplayChangeListener *dcl,
773 int x, int y, int w, int h)
775 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
776 qemu_spice_display_update(ssd, x, y, w, h);
779 static void display_switch(DisplayChangeListener *dcl,
780 DisplaySurface *surface)
782 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
783 qemu_spice_display_switch(ssd, surface);
786 static void display_refresh(DisplayChangeListener *dcl)
788 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
789 qemu_spice_display_refresh(ssd);
792 static void display_mouse_set(DisplayChangeListener *dcl,
793 int x, int y, int on)
795 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
797 qemu_mutex_lock(&ssd->lock);
800 g_free(ssd->ptr_move);
801 ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL, on);
802 qemu_mutex_unlock(&ssd->lock);
805 static void display_mouse_define(DisplayChangeListener *dcl,
808 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
810 qemu_mutex_lock(&ssd->lock);
814 cursor_put(ssd->cursor);
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);
825 static const DisplayChangeListenerOps display_listener_ops = {
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,
837 static void qemu_spice_gl_block(SimpleSpiceDisplay *ssd, bool block)
842 timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
843 timeout += 1000; /* one sec */
844 timer_mod(ssd->gl_unblock_timer, timeout);
846 timer_del(ssd->gl_unblock_timer);
848 graphic_hw_gl_block(ssd->dcl.con, block);
851 static void qemu_spice_gl_unblock_bh(void *opaque)
853 SimpleSpiceDisplay *ssd = opaque;
855 qemu_spice_gl_block(ssd, false);
858 static void qemu_spice_gl_block_timer(void *opaque)
860 fprintf(stderr, "WARNING: spice: no gl-draw-done within one second\n");
863 static QEMUGLContext qemu_spice_gl_create_context(DisplayChangeListener *dcl,
864 QEMUGLParams *params)
866 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
868 return qemu_egl_create_context(dcl, params);
871 static void qemu_spice_gl_scanout(DisplayChangeListener *dcl,
874 uint32_t x, uint32_t y,
875 uint32_t w, uint32_t h)
877 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
878 EGLint stride = 0, fourcc = 0;
882 fd = egl_get_fd_for_texture(tex_id, &stride, &fourcc);
884 fprintf(stderr, "%s: failed to get fd for texture\n", __func__);
887 dprint(1, "%s: %dx%d (stride %d, fourcc 0x%x)\n", __func__,
888 w, h, stride, fourcc);
890 dprint(1, "%s: no texture (no framebuffer)\n", __func__);
893 assert(!tex_id || fd >= 0);
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);
902 static void qemu_spice_gl_update(DisplayChangeListener *dcl,
903 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
905 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
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);
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,
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,
928 .dpy_gl_scanout = qemu_spice_gl_scanout,
929 .dpy_gl_update = qemu_spice_gl_update,
932 #endif /* HAVE_SPICE_GL */
934 static void qemu_spice_display_init_one(QemuConsole *con)
936 SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
938 qemu_spice_display_init_common(ssd);
940 ssd->dcl.ops = &display_listener_ops;
942 if (display_opengl) {
943 ssd->dcl.ops = &display_listener_gl_ops;
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);
952 ssd->qxl.base.sif = &dpy_interface.base;
953 qemu_spice_add_display_interface(&ssd->qxl, con);
955 qemu_spice_create_host_memslot(ssd);
957 register_displaychangelistener(&ssd->dcl);
960 void qemu_spice_display_init(void)
966 con = qemu_console_lookup_by_index(i);
967 if (!con || !qemu_console_is_graphic(con)) {
970 if (qemu_spice_have_display_interface(con)) {
973 qemu_spice_display_init_one(con);