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 "ui/qemu-spice.h"
20 #include "qemu/error-report.h"
21 #include "qemu/timer.h"
22 #include "qemu/lockable.h"
23 #include "qemu/main-loop.h"
24 #include "qemu/option.h"
25 #include "qemu/queue.h"
26 #include "ui/console.h"
29 #include "ui/spice-display.h"
33 int qemu_spice_rect_is_empty(const QXLRect
* r
)
35 return r
->top
== r
->bottom
|| r
->left
== r
->right
;
38 void qemu_spice_rect_union(QXLRect
*dest
, const QXLRect
*r
)
40 if (qemu_spice_rect_is_empty(r
)) {
44 if (qemu_spice_rect_is_empty(dest
)) {
49 dest
->top
= MIN(dest
->top
, r
->top
);
50 dest
->left
= MIN(dest
->left
, r
->left
);
51 dest
->bottom
= MAX(dest
->bottom
, r
->bottom
);
52 dest
->right
= MAX(dest
->right
, r
->right
);
55 QXLCookie
*qxl_cookie_new(int type
, uint64_t io
)
59 cookie
= g_malloc0(sizeof(*cookie
));
65 void qemu_spice_add_memslot(SimpleSpiceDisplay
*ssd
, QXLDevMemSlot
*memslot
,
68 trace_qemu_spice_add_memslot(ssd
->qxl
.id
, memslot
->slot_id
,
69 memslot
->virt_start
, memslot
->virt_end
,
72 if (async
!= QXL_SYNC
) {
73 spice_qxl_add_memslot_async(&ssd
->qxl
, memslot
,
74 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
75 QXL_IO_MEMSLOT_ADD_ASYNC
));
77 spice_qxl_add_memslot(&ssd
->qxl
, memslot
);
81 void qemu_spice_del_memslot(SimpleSpiceDisplay
*ssd
, uint32_t gid
, uint32_t sid
)
83 trace_qemu_spice_del_memslot(ssd
->qxl
.id
, gid
, sid
);
84 spice_qxl_del_memslot(&ssd
->qxl
, gid
, sid
);
87 void qemu_spice_create_primary_surface(SimpleSpiceDisplay
*ssd
, uint32_t id
,
88 QXLDevSurfaceCreate
*surface
,
91 trace_qemu_spice_create_primary_surface(ssd
->qxl
.id
, id
, surface
, async
);
92 if (async
!= QXL_SYNC
) {
93 spice_qxl_create_primary_surface_async(&ssd
->qxl
, id
, surface
,
94 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
95 QXL_IO_CREATE_PRIMARY_ASYNC
));
97 spice_qxl_create_primary_surface(&ssd
->qxl
, id
, surface
);
101 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay
*ssd
,
102 uint32_t id
, qxl_async_io async
)
104 trace_qemu_spice_destroy_primary_surface(ssd
->qxl
.id
, id
, async
);
105 if (async
!= QXL_SYNC
) {
106 spice_qxl_destroy_primary_surface_async(&ssd
->qxl
, id
,
107 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
108 QXL_IO_DESTROY_PRIMARY_ASYNC
));
110 spice_qxl_destroy_primary_surface(&ssd
->qxl
, id
);
114 void qemu_spice_wakeup(SimpleSpiceDisplay
*ssd
)
116 trace_qemu_spice_wakeup(ssd
->qxl
.id
);
117 spice_qxl_wakeup(&ssd
->qxl
);
120 static void qemu_spice_create_one_update(SimpleSpiceDisplay
*ssd
,
123 SimpleSpiceUpdate
*update
;
124 QXLDrawable
*drawable
;
128 struct timespec time_space
;
129 pixman_image_t
*dest
;
131 trace_qemu_spice_create_update(
132 rect
->left
, rect
->right
,
133 rect
->top
, rect
->bottom
);
135 update
= g_malloc0(sizeof(*update
));
136 drawable
= &update
->drawable
;
137 image
= &update
->image
;
138 cmd
= &update
->ext
.cmd
;
140 bw
= rect
->right
- rect
->left
;
141 bh
= rect
->bottom
- rect
->top
;
142 update
->bitmap
= g_malloc(bw
* bh
* 4);
144 drawable
->bbox
= *rect
;
145 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
146 drawable
->effect
= QXL_EFFECT_OPAQUE
;
147 drawable
->release_info
.id
= (uintptr_t)(&update
->ext
);
148 drawable
->type
= QXL_DRAW_COPY
;
149 drawable
->surfaces_dest
[0] = -1;
150 drawable
->surfaces_dest
[1] = -1;
151 drawable
->surfaces_dest
[2] = -1;
152 clock_gettime(CLOCK_MONOTONIC
, &time_space
);
153 /* time in milliseconds from epoch. */
154 drawable
->mm_time
= time_space
.tv_sec
* 1000
155 + time_space
.tv_nsec
/ 1000 / 1000;
157 drawable
->u
.copy
.rop_descriptor
= SPICE_ROPD_OP_PUT
;
158 drawable
->u
.copy
.src_bitmap
= (uintptr_t)image
;
159 drawable
->u
.copy
.src_area
.right
= bw
;
160 drawable
->u
.copy
.src_area
.bottom
= bh
;
162 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, ssd
->unique
++);
163 image
->descriptor
.type
= SPICE_IMAGE_TYPE_BITMAP
;
164 image
->bitmap
.flags
= QXL_BITMAP_DIRECT
| QXL_BITMAP_TOP_DOWN
;
165 image
->bitmap
.stride
= bw
* 4;
166 image
->descriptor
.width
= image
->bitmap
.x
= bw
;
167 image
->descriptor
.height
= image
->bitmap
.y
= bh
;
168 image
->bitmap
.data
= (uintptr_t)(update
->bitmap
);
169 image
->bitmap
.palette
= 0;
170 image
->bitmap
.format
= SPICE_BITMAP_FMT_32BIT
;
172 dest
= pixman_image_create_bits(PIXMAN_LE_x8r8g8b8
, bw
, bh
,
173 (void *)update
->bitmap
, bw
* 4);
174 pixman_image_composite(PIXMAN_OP_SRC
, ssd
->surface
, NULL
, ssd
->mirror
,
175 rect
->left
, rect
->top
, 0, 0,
176 rect
->left
, rect
->top
, bw
, bh
);
177 pixman_image_composite(PIXMAN_OP_SRC
, ssd
->mirror
, NULL
, dest
,
178 rect
->left
, rect
->top
, 0, 0,
180 pixman_image_unref(dest
);
182 cmd
->type
= QXL_CMD_DRAW
;
183 cmd
->data
= (uintptr_t)drawable
;
185 QTAILQ_INSERT_TAIL(&ssd
->updates
, update
, next
);
188 static void qemu_spice_create_update(SimpleSpiceDisplay
*ssd
)
190 static const int blksize
= 32;
191 int blocks
= DIV_ROUND_UP(surface_width(ssd
->ds
), blksize
);
192 int dirty_top
[blocks
];
193 int y
, yoff1
, yoff2
, x
, xoff
, blk
, bw
;
194 int bpp
= surface_bytes_per_pixel(ssd
->ds
);
195 uint8_t *guest
, *mirror
;
197 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
201 for (blk
= 0; blk
< blocks
; blk
++) {
205 guest
= surface_data(ssd
->ds
);
206 mirror
= (void *)pixman_image_get_data(ssd
->mirror
);
207 for (y
= ssd
->dirty
.top
; y
< ssd
->dirty
.bottom
; y
++) {
208 yoff1
= y
* surface_stride(ssd
->ds
);
209 yoff2
= y
* pixman_image_get_stride(ssd
->mirror
);
210 for (x
= ssd
->dirty
.left
; x
< ssd
->dirty
.right
; x
+= blksize
) {
213 bw
= MIN(blksize
, ssd
->dirty
.right
- x
);
214 if (memcmp(guest
+ yoff1
+ xoff
,
215 mirror
+ yoff2
+ xoff
,
217 if (dirty_top
[blk
] != -1) {
219 .top
= dirty_top
[blk
],
224 qemu_spice_create_one_update(ssd
, &update
);
228 if (dirty_top
[blk
] == -1) {
235 for (x
= ssd
->dirty
.left
; x
< ssd
->dirty
.right
; x
+= blksize
) {
237 bw
= MIN(blksize
, ssd
->dirty
.right
- x
);
238 if (dirty_top
[blk
] != -1) {
240 .top
= dirty_top
[blk
],
241 .bottom
= ssd
->dirty
.bottom
,
245 qemu_spice_create_one_update(ssd
, &update
);
250 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
253 static SimpleSpiceCursor
*
254 qemu_spice_create_cursor_update(SimpleSpiceDisplay
*ssd
,
258 size_t size
= c
? c
->width
* c
->height
* 4 : 0;
259 SimpleSpiceCursor
*update
;
264 update
= g_malloc0(sizeof(*update
) + size
);
266 cursor
= &update
->cursor
;
267 cmd
= &update
->ext
.cmd
;
270 ccmd
->type
= QXL_CURSOR_SET
;
271 ccmd
->u
.set
.position
.x
= ssd
->ptr_x
+ ssd
->hot_x
;
272 ccmd
->u
.set
.position
.y
= ssd
->ptr_y
+ ssd
->hot_y
;
273 ccmd
->u
.set
.visible
= true;
274 ccmd
->u
.set
.shape
= (uintptr_t)cursor
;
275 cursor
->header
.unique
= ssd
->unique
++;
276 cursor
->header
.type
= SPICE_CURSOR_TYPE_ALPHA
;
277 cursor
->header
.width
= c
->width
;
278 cursor
->header
.height
= c
->height
;
279 cursor
->header
.hot_spot_x
= c
->hot_x
;
280 cursor
->header
.hot_spot_y
= c
->hot_y
;
281 cursor
->data_size
= size
;
282 cursor
->chunk
.data_size
= size
;
283 memcpy(cursor
->chunk
.data
, c
->data
, size
);
285 ccmd
->type
= QXL_CURSOR_HIDE
;
287 ccmd
->type
= QXL_CURSOR_MOVE
;
288 ccmd
->u
.position
.x
= ssd
->ptr_x
+ ssd
->hot_x
;
289 ccmd
->u
.position
.y
= ssd
->ptr_y
+ ssd
->hot_y
;
291 ccmd
->release_info
.id
= (uintptr_t)(&update
->ext
);
293 cmd
->type
= QXL_CMD_CURSOR
;
294 cmd
->data
= (uintptr_t)ccmd
;
300 * Called from spice server thread context (via interface_release_resource)
301 * We do *not* hold the global qemu mutex here, so extra care is needed
302 * when calling qemu functions. QEMU interfaces used:
303 * - g_free (underlying glibc free is re-entrant).
305 void qemu_spice_destroy_update(SimpleSpiceDisplay
*sdpy
, SimpleSpiceUpdate
*update
)
307 g_free(update
->bitmap
);
311 void qemu_spice_create_host_memslot(SimpleSpiceDisplay
*ssd
)
313 QXLDevMemSlot memslot
;
315 memset(&memslot
, 0, sizeof(memslot
));
316 memslot
.slot_group_id
= MEMSLOT_GROUP_HOST
;
317 memslot
.virt_end
= ~0;
318 qemu_spice_add_memslot(ssd
, &memslot
, QXL_SYNC
);
321 void qemu_spice_create_host_primary(SimpleSpiceDisplay
*ssd
)
323 QXLDevSurfaceCreate surface
;
324 uint64_t surface_size
;
326 memset(&surface
, 0, sizeof(surface
));
328 surface_size
= (uint64_t) surface_width(ssd
->ds
) *
329 surface_height(ssd
->ds
) * 4;
330 assert(surface_size
> 0);
331 assert(surface_size
< INT_MAX
);
332 if (ssd
->bufsize
< surface_size
) {
333 ssd
->bufsize
= surface_size
;
335 ssd
->buf
= g_malloc(ssd
->bufsize
);
338 surface
.format
= SPICE_SURFACE_FMT_32_xRGB
;
339 surface
.width
= surface_width(ssd
->ds
);
340 surface
.height
= surface_height(ssd
->ds
);
341 surface
.stride
= -surface
.width
* 4;
342 surface
.mouse_mode
= true;
345 surface
.mem
= (uintptr_t)ssd
->buf
;
346 surface
.group_id
= MEMSLOT_GROUP_HOST
;
348 qemu_spice_create_primary_surface(ssd
, 0, &surface
, QXL_SYNC
);
351 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay
*ssd
)
353 qemu_spice_destroy_primary_surface(ssd
, 0, QXL_SYNC
);
356 void qemu_spice_display_init_common(SimpleSpiceDisplay
*ssd
)
358 qemu_mutex_init(&ssd
->lock
);
359 QTAILQ_INIT(&ssd
->updates
);
362 if (ssd
->num_surfaces
== 0) {
363 ssd
->num_surfaces
= 1024;
367 /* display listener callbacks */
369 void qemu_spice_display_update(SimpleSpiceDisplay
*ssd
,
370 int x
, int y
, int w
, int h
)
374 trace_qemu_spice_display_update(ssd
->qxl
.id
, x
, y
, w
, h
);
375 update_area
.left
= x
,
376 update_area
.right
= x
+ w
;
378 update_area
.bottom
= y
+ h
;
380 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
383 qemu_spice_rect_union(&ssd
->dirty
, &update_area
);
386 void qemu_spice_display_switch(SimpleSpiceDisplay
*ssd
,
387 DisplaySurface
*surface
)
389 SimpleSpiceUpdate
*update
;
393 surface_width(surface
) == pixman_image_get_width(ssd
->surface
) &&
394 surface_height(surface
) == pixman_image_get_height(ssd
->surface
) &&
395 surface_format(surface
) == pixman_image_get_format(ssd
->surface
)) {
396 /* no-resize fast path: just swap backing store */
397 trace_qemu_spice_display_surface(ssd
->qxl
.id
,
398 surface_width(surface
),
399 surface_height(surface
),
401 qemu_mutex_lock(&ssd
->lock
);
403 pixman_image_unref(ssd
->surface
);
404 ssd
->surface
= pixman_image_ref(ssd
->ds
->image
);
405 qemu_mutex_unlock(&ssd
->lock
);
406 qemu_spice_display_update(ssd
, 0, 0,
407 surface_width(surface
),
408 surface_height(surface
));
412 /* full mode switch */
413 trace_qemu_spice_display_surface(ssd
->qxl
.id
,
414 surface_width(surface
),
415 surface_height(surface
),
418 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
420 pixman_image_unref(ssd
->surface
);
422 pixman_image_unref(ssd
->mirror
);
426 qemu_mutex_lock(&ssd
->lock
);
427 need_destroy
= (ssd
->ds
!= NULL
);
429 while ((update
= QTAILQ_FIRST(&ssd
->updates
)) != NULL
) {
430 QTAILQ_REMOVE(&ssd
->updates
, update
, next
);
431 qemu_spice_destroy_update(ssd
, update
);
433 qemu_mutex_unlock(&ssd
->lock
);
435 qemu_spice_destroy_host_primary(ssd
);
438 ssd
->surface
= pixman_image_ref(ssd
->ds
->image
);
439 ssd
->mirror
= qemu_pixman_mirror_create(ssd
->ds
->format
,
441 qemu_spice_create_host_primary(ssd
);
444 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
447 qemu_mutex_lock(&ssd
->lock
);
449 g_free(ssd
->ptr_define
);
450 ssd
->ptr_define
= qemu_spice_create_cursor_update(ssd
, ssd
->cursor
, 0);
452 qemu_mutex_unlock(&ssd
->lock
);
455 void qemu_spice_cursor_refresh_bh(void *opaque
)
457 SimpleSpiceDisplay
*ssd
= opaque
;
459 qemu_mutex_lock(&ssd
->lock
);
461 QEMUCursor
*c
= ssd
->cursor
;
462 assert(ssd
->dcl
.con
);
464 qemu_mutex_unlock(&ssd
->lock
);
465 dpy_cursor_define(ssd
->dcl
.con
, c
);
466 qemu_mutex_lock(&ssd
->lock
);
470 if (ssd
->mouse_x
!= -1 && ssd
->mouse_y
!= -1) {
472 assert(ssd
->dcl
.con
);
477 qemu_mutex_unlock(&ssd
->lock
);
478 dpy_mouse_set(ssd
->dcl
.con
, x
, y
, 1);
480 qemu_mutex_unlock(&ssd
->lock
);
484 void qemu_spice_display_refresh(SimpleSpiceDisplay
*ssd
)
486 graphic_hw_update(ssd
->dcl
.con
);
488 WITH_QEMU_LOCK_GUARD(&ssd
->lock
) {
489 if (QTAILQ_EMPTY(&ssd
->updates
) && ssd
->ds
) {
490 qemu_spice_create_update(ssd
);
495 trace_qemu_spice_display_refresh(ssd
->qxl
.id
, ssd
->notify
);
498 qemu_spice_wakeup(ssd
);
502 /* spice display interface callbacks */
504 #if SPICE_HAS_ATTACHED_WORKER
505 static void interface_attached_worker(QXLInstance
*sin
)
510 static void interface_attach_worker(QXLInstance
*sin
, QXLWorker
*qxl_worker
)
516 static void interface_set_compression_level(QXLInstance
*sin
, int level
)
521 static void interface_get_init_info(QXLInstance
*sin
, QXLDevInitInfo
*info
)
523 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
525 info
->memslot_gen_bits
= MEMSLOT_GENERATION_BITS
;
526 info
->memslot_id_bits
= MEMSLOT_SLOT_BITS
;
527 info
->num_memslots
= NUM_MEMSLOTS
;
528 info
->num_memslots_groups
= NUM_MEMSLOTS_GROUPS
;
529 info
->internal_groupslot_id
= 0;
530 info
->qxl_ram_size
= 16 * 1024 * 1024;
531 info
->n_surfaces
= ssd
->num_surfaces
;
534 static int interface_get_command(QXLInstance
*sin
, QXLCommandExt
*ext
)
536 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
537 SimpleSpiceUpdate
*update
;
540 qemu_mutex_lock(&ssd
->lock
);
541 update
= QTAILQ_FIRST(&ssd
->updates
);
542 if (update
!= NULL
) {
543 QTAILQ_REMOVE(&ssd
->updates
, update
, next
);
547 qemu_mutex_unlock(&ssd
->lock
);
552 static int interface_req_cmd_notification(QXLInstance
*sin
)
557 static void interface_release_resource(QXLInstance
*sin
,
558 QXLReleaseInfoExt rext
)
560 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
561 SimpleSpiceUpdate
*update
;
562 SimpleSpiceCursor
*cursor
;
569 ext
= (void *)(intptr_t)(rext
.info
->id
);
570 switch (ext
->cmd
.type
) {
572 update
= container_of(ext
, SimpleSpiceUpdate
, ext
);
573 qemu_spice_destroy_update(ssd
, update
);
576 cursor
= container_of(ext
, SimpleSpiceCursor
, ext
);
580 g_assert_not_reached();
584 static int interface_get_cursor_command(QXLInstance
*sin
, QXLCommandExt
*ext
)
586 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
589 QEMU_LOCK_GUARD(&ssd
->lock
);
590 if (ssd
->ptr_define
) {
591 *ext
= ssd
->ptr_define
->ext
;
592 ssd
->ptr_define
= NULL
;
594 } else if (ssd
->ptr_move
) {
595 *ext
= ssd
->ptr_move
->ext
;
596 ssd
->ptr_move
= NULL
;
604 static int interface_req_cursor_notification(QXLInstance
*sin
)
609 static void interface_notify_update(QXLInstance
*sin
, uint32_t update_id
)
611 fprintf(stderr
, "%s: abort()\n", __func__
);
615 static int interface_flush_resources(QXLInstance
*sin
)
617 fprintf(stderr
, "%s: abort()\n", __func__
);
622 static void interface_update_area_complete(QXLInstance
*sin
,
624 QXLRect
*dirty
, uint32_t num_updated_rects
)
626 /* should never be called, used in qxl native mode only */
627 fprintf(stderr
, "%s: abort()\n", __func__
);
631 /* called from spice server thread context only */
632 static void interface_async_complete(QXLInstance
*sin
, uint64_t cookie_token
)
634 QXLCookie
*cookie
= (QXLCookie
*)(uintptr_t)cookie_token
;
636 switch (cookie
->type
) {
638 case QXL_COOKIE_TYPE_GL_DRAW_DONE
:
640 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
641 qemu_bh_schedule(ssd
->gl_unblock_bh
);
644 case QXL_COOKIE_TYPE_IO
:
645 if (cookie
->io
== QXL_IO_MONITORS_CONFIG_ASYNC
) {
646 g_free(cookie
->u
.data
);
651 /* should never be called, used in qxl native mode only */
652 fprintf(stderr
, "%s: abort()\n", __func__
);
658 static void interface_set_client_capabilities(QXLInstance
*sin
,
659 uint8_t client_present
,
665 static int interface_client_monitors_config(QXLInstance
*sin
,
666 VDAgentMonitorsConfig
*mc
)
668 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
672 if (!dpy_ui_info_supported(ssd
->dcl
.con
)) {
673 return 0; /* == not supported by guest */
680 info
= *dpy_get_ui_info(ssd
->dcl
.con
);
682 head
= qemu_console_get_index(ssd
->dcl
.con
);
683 if (mc
->num_of_monitors
> head
) {
684 info
.width
= mc
->monitors
[head
].width
;
685 info
.height
= mc
->monitors
[head
].height
;
686 #if SPICE_SERVER_VERSION >= 0x000e04 /* release 0.14.4 */
687 if (mc
->flags
& VD_AGENT_CONFIG_MONITORS_FLAG_PHYSICAL_SIZE
) {
688 VDAgentMonitorMM
*mm
= (void *)&mc
->monitors
[mc
->num_of_monitors
];
689 info
.width_mm
= mm
[head
].width
;
690 info
.height_mm
= mm
[head
].height
;
695 trace_qemu_spice_ui_info(ssd
->qxl
.id
, info
.width
, info
.height
);
696 dpy_set_ui_info(ssd
->dcl
.con
, &info
, false);
700 static const QXLInterface dpy_interface
= {
701 .base
.type
= SPICE_INTERFACE_QXL
,
702 .base
.description
= "qemu simple display",
703 .base
.major_version
= SPICE_INTERFACE_QXL_MAJOR
,
704 .base
.minor_version
= SPICE_INTERFACE_QXL_MINOR
,
706 #if SPICE_HAS_ATTACHED_WORKER
707 .attached_worker
= interface_attached_worker
,
709 .attache_worker
= interface_attach_worker
,
711 .set_compression_level
= interface_set_compression_level
,
712 .get_init_info
= interface_get_init_info
,
714 /* the callbacks below are called from spice server thread context */
715 .get_command
= interface_get_command
,
716 .req_cmd_notification
= interface_req_cmd_notification
,
717 .release_resource
= interface_release_resource
,
718 .get_cursor_command
= interface_get_cursor_command
,
719 .req_cursor_notification
= interface_req_cursor_notification
,
720 .notify_update
= interface_notify_update
,
721 .flush_resources
= interface_flush_resources
,
722 .async_complete
= interface_async_complete
,
723 .update_area_complete
= interface_update_area_complete
,
724 .set_client_capabilities
= interface_set_client_capabilities
,
725 .client_monitors_config
= interface_client_monitors_config
,
728 static void display_update(DisplayChangeListener
*dcl
,
729 int x
, int y
, int w
, int h
)
731 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
732 qemu_spice_display_update(ssd
, x
, y
, w
, h
);
735 static void display_switch(DisplayChangeListener
*dcl
,
736 DisplaySurface
*surface
)
738 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
739 qemu_spice_display_switch(ssd
, surface
);
742 static void display_refresh(DisplayChangeListener
*dcl
)
744 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
745 qemu_spice_display_refresh(ssd
);
748 static void display_mouse_set(DisplayChangeListener
*dcl
,
749 int x
, int y
, int on
)
751 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
753 qemu_mutex_lock(&ssd
->lock
);
756 g_free(ssd
->ptr_move
);
757 ssd
->ptr_move
= qemu_spice_create_cursor_update(ssd
, NULL
, on
);
758 qemu_mutex_unlock(&ssd
->lock
);
759 qemu_spice_wakeup(ssd
);
762 static void display_mouse_define(DisplayChangeListener
*dcl
,
765 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
767 qemu_mutex_lock(&ssd
->lock
);
769 cursor_unref(ssd
->cursor
);
771 ssd
->hot_x
= c
->hot_x
;
772 ssd
->hot_y
= c
->hot_y
;
773 g_free(ssd
->ptr_move
);
774 ssd
->ptr_move
= NULL
;
775 g_free(ssd
->ptr_define
);
776 ssd
->ptr_define
= qemu_spice_create_cursor_update(ssd
, c
, 0);
777 qemu_mutex_unlock(&ssd
->lock
);
778 qemu_spice_wakeup(ssd
);
781 static const DisplayChangeListenerOps display_listener_ops
= {
783 .dpy_gfx_update
= display_update
,
784 .dpy_gfx_switch
= display_switch
,
785 .dpy_gfx_check_format
= qemu_pixman_check_format
,
786 .dpy_refresh
= display_refresh
,
787 .dpy_mouse_set
= display_mouse_set
,
788 .dpy_cursor_define
= display_mouse_define
,
793 static void qemu_spice_gl_monitor_config(SimpleSpiceDisplay
*ssd
,
794 int x
, int y
, int w
, int h
)
796 QXLMonitorsConfig
*config
;
799 config
= g_malloc0(sizeof(QXLMonitorsConfig
) + sizeof(QXLHead
));
801 config
->max_allowed
= 1;
802 config
->heads
[0].x
= x
;
803 config
->heads
[0].y
= y
;
804 config
->heads
[0].width
= w
;
805 config
->heads
[0].height
= h
;
806 cookie
= qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
807 QXL_IO_MONITORS_CONFIG_ASYNC
);
808 cookie
->u
.data
= config
;
810 spice_qxl_monitors_config_async(&ssd
->qxl
,
816 static void qemu_spice_gl_block(SimpleSpiceDisplay
*ssd
, bool block
)
821 timeout
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
822 timeout
+= 1000; /* one sec */
823 timer_mod(ssd
->gl_unblock_timer
, timeout
);
825 timer_del(ssd
->gl_unblock_timer
);
827 graphic_hw_gl_block(ssd
->dcl
.con
, block
);
830 static void qemu_spice_gl_unblock_bh(void *opaque
)
832 SimpleSpiceDisplay
*ssd
= opaque
;
834 qemu_spice_gl_block(ssd
, false);
837 static void qemu_spice_gl_block_timer(void *opaque
)
839 warn_report("spice: no gl-draw-done within one second");
842 static void spice_gl_refresh(DisplayChangeListener
*dcl
)
844 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
847 if (!ssd
->ds
|| qemu_console_is_gl_blocked(ssd
->dcl
.con
)) {
851 graphic_hw_update(dcl
->con
);
852 if (ssd
->gl_updates
&& ssd
->have_surface
) {
853 qemu_spice_gl_block(ssd
, true);
855 cookie
= (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE
, 0);
856 spice_qxl_gl_draw_async(&ssd
->qxl
, 0, 0,
857 surface_width(ssd
->ds
),
858 surface_height(ssd
->ds
),
864 static void spice_gl_update(DisplayChangeListener
*dcl
,
865 int x
, int y
, int w
, int h
)
867 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
869 surface_gl_update_texture(ssd
->gls
, ssd
->ds
, x
, y
, w
, h
);
873 static void spice_gl_switch(DisplayChangeListener
*dcl
,
874 struct DisplaySurface
*new_surface
)
876 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
877 EGLint stride
, fourcc
;
881 surface_gl_destroy_texture(ssd
->gls
, ssd
->ds
);
883 ssd
->ds
= new_surface
;
885 surface_gl_create_texture(ssd
->gls
, ssd
->ds
);
886 fd
= egl_get_fd_for_texture(ssd
->ds
->texture
,
890 surface_gl_destroy_texture(ssd
->gls
, ssd
->ds
);
894 trace_qemu_spice_gl_surface(ssd
->qxl
.id
,
895 surface_width(ssd
->ds
),
896 surface_height(ssd
->ds
),
899 /* note: spice server will close the fd */
900 spice_qxl_gl_scanout(&ssd
->qxl
, fd
,
901 surface_width(ssd
->ds
),
902 surface_height(ssd
->ds
),
903 stride
, fourcc
, false);
904 ssd
->have_surface
= true;
905 ssd
->have_scanout
= false;
907 qemu_spice_gl_monitor_config(ssd
, 0, 0,
908 surface_width(ssd
->ds
),
909 surface_height(ssd
->ds
));
913 static QEMUGLContext
qemu_spice_gl_create_context(DisplayGLCtx
*dgc
,
914 QEMUGLParams
*params
)
916 eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
, EGL_NO_SURFACE
,
918 return qemu_egl_create_context(dgc
, params
);
921 static void qemu_spice_gl_scanout_disable(DisplayChangeListener
*dcl
)
923 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
925 trace_qemu_spice_gl_scanout_disable(ssd
->qxl
.id
);
926 spice_qxl_gl_scanout(&ssd
->qxl
, -1, 0, 0, 0, 0, false);
927 qemu_spice_gl_monitor_config(ssd
, 0, 0, 0, 0);
928 ssd
->have_surface
= false;
929 ssd
->have_scanout
= false;
932 static void qemu_spice_gl_scanout_texture(DisplayChangeListener
*dcl
,
935 uint32_t backing_width
,
936 uint32_t backing_height
,
937 uint32_t x
, uint32_t y
,
938 uint32_t w
, uint32_t h
,
941 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
942 EGLint stride
= 0, fourcc
= 0;
946 fd
= egl_get_fd_for_texture(tex_id
, &stride
, &fourcc
, NULL
);
948 fprintf(stderr
, "%s: failed to get fd for texture\n", __func__
);
951 trace_qemu_spice_gl_scanout_texture(ssd
->qxl
.id
, w
, h
, fourcc
);
953 /* note: spice server will close the fd */
954 spice_qxl_gl_scanout(&ssd
->qxl
, fd
, backing_width
, backing_height
,
955 stride
, fourcc
, y_0_top
);
956 qemu_spice_gl_monitor_config(ssd
, x
, y
, w
, h
);
957 ssd
->have_surface
= false;
958 ssd
->have_scanout
= true;
961 static void qemu_spice_gl_scanout_dmabuf(DisplayChangeListener
*dcl
,
964 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
966 ssd
->guest_dmabuf
= dmabuf
;
967 ssd
->guest_dmabuf_refresh
= true;
969 ssd
->have_surface
= false;
970 ssd
->have_scanout
= true;
973 static void qemu_spice_gl_cursor_dmabuf(DisplayChangeListener
*dcl
,
974 QemuDmaBuf
*dmabuf
, bool have_hot
,
975 uint32_t hot_x
, uint32_t hot_y
)
977 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
979 ssd
->have_hot
= have_hot
;
983 trace_qemu_spice_gl_cursor(ssd
->qxl
.id
, dmabuf
!= NULL
, have_hot
);
985 egl_dmabuf_import_texture(dmabuf
);
986 if (!dmabuf
->texture
) {
989 egl_fb_setup_for_tex(&ssd
->cursor_fb
, dmabuf
->width
, dmabuf
->height
,
990 dmabuf
->texture
, false);
992 egl_fb_destroy(&ssd
->cursor_fb
);
996 static void qemu_spice_gl_cursor_position(DisplayChangeListener
*dcl
,
997 uint32_t pos_x
, uint32_t pos_y
)
999 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
1001 qemu_mutex_lock(&ssd
->lock
);
1004 qemu_mutex_unlock(&ssd
->lock
);
1007 static void qemu_spice_gl_release_dmabuf(DisplayChangeListener
*dcl
,
1010 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
1012 if (ssd
->guest_dmabuf
== dmabuf
) {
1013 ssd
->guest_dmabuf
= NULL
;
1014 ssd
->guest_dmabuf_refresh
= false;
1016 egl_dmabuf_release_texture(dmabuf
);
1019 static void qemu_spice_gl_update(DisplayChangeListener
*dcl
,
1020 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
1022 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
1023 EGLint stride
= 0, fourcc
= 0;
1024 bool render_cursor
= false;
1025 bool y_0_top
= false; /* FIXME */
1029 if (!ssd
->have_scanout
) {
1033 if (ssd
->cursor_fb
.texture
) {
1034 render_cursor
= true;
1036 if (ssd
->render_cursor
!= render_cursor
) {
1037 ssd
->render_cursor
= render_cursor
;
1038 ssd
->guest_dmabuf_refresh
= true;
1039 egl_fb_destroy(&ssd
->blit_fb
);
1042 if (ssd
->guest_dmabuf_refresh
) {
1043 QemuDmaBuf
*dmabuf
= ssd
->guest_dmabuf
;
1044 if (render_cursor
) {
1045 egl_dmabuf_import_texture(dmabuf
);
1046 if (!dmabuf
->texture
) {
1050 /* source framebuffer */
1051 egl_fb_setup_for_tex(&ssd
->guest_fb
,
1052 dmabuf
->width
, dmabuf
->height
,
1053 dmabuf
->texture
, false);
1055 /* dest framebuffer */
1056 if (ssd
->blit_fb
.width
!= dmabuf
->width
||
1057 ssd
->blit_fb
.height
!= dmabuf
->height
) {
1058 trace_qemu_spice_gl_render_dmabuf(ssd
->qxl
.id
, dmabuf
->width
,
1060 egl_fb_destroy(&ssd
->blit_fb
);
1061 egl_fb_setup_new_tex(&ssd
->blit_fb
,
1062 dmabuf
->width
, dmabuf
->height
);
1063 fd
= egl_get_fd_for_texture(ssd
->blit_fb
.texture
,
1064 &stride
, &fourcc
, NULL
);
1065 spice_qxl_gl_scanout(&ssd
->qxl
, fd
,
1066 dmabuf
->width
, dmabuf
->height
,
1067 stride
, fourcc
, false);
1070 trace_qemu_spice_gl_forward_dmabuf(ssd
->qxl
.id
,
1071 dmabuf
->width
, dmabuf
->height
);
1072 /* note: spice server will close the fd, so hand over a dup */
1073 spice_qxl_gl_scanout(&ssd
->qxl
, dup(dmabuf
->fd
),
1074 dmabuf
->width
, dmabuf
->height
,
1075 dmabuf
->stride
, dmabuf
->fourcc
,
1078 qemu_spice_gl_monitor_config(ssd
, 0, 0, dmabuf
->width
, dmabuf
->height
);
1079 ssd
->guest_dmabuf_refresh
= false;
1082 if (render_cursor
) {
1084 qemu_mutex_lock(&ssd
->lock
);
1087 qemu_mutex_unlock(&ssd
->lock
);
1088 egl_texture_blit(ssd
->gls
, &ssd
->blit_fb
, &ssd
->guest_fb
,
1090 egl_texture_blend(ssd
->gls
, &ssd
->blit_fb
, &ssd
->cursor_fb
,
1091 !y_0_top
, x
, y
, 1.0, 1.0);
1095 trace_qemu_spice_gl_update(ssd
->qxl
.id
, w
, h
, x
, y
);
1096 qemu_spice_gl_block(ssd
, true);
1098 cookie
= (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE
, 0);
1099 spice_qxl_gl_draw_async(&ssd
->qxl
, x
, y
, w
, h
, cookie
);
1102 static const DisplayChangeListenerOps display_listener_gl_ops
= {
1103 .dpy_name
= "spice-egl",
1104 .dpy_gfx_update
= spice_gl_update
,
1105 .dpy_gfx_switch
= spice_gl_switch
,
1106 .dpy_gfx_check_format
= console_gl_check_format
,
1107 .dpy_refresh
= spice_gl_refresh
,
1108 .dpy_mouse_set
= display_mouse_set
,
1109 .dpy_cursor_define
= display_mouse_define
,
1111 .dpy_gl_scanout_disable
= qemu_spice_gl_scanout_disable
,
1112 .dpy_gl_scanout_texture
= qemu_spice_gl_scanout_texture
,
1113 .dpy_gl_scanout_dmabuf
= qemu_spice_gl_scanout_dmabuf
,
1114 .dpy_gl_cursor_dmabuf
= qemu_spice_gl_cursor_dmabuf
,
1115 .dpy_gl_cursor_position
= qemu_spice_gl_cursor_position
,
1116 .dpy_gl_release_dmabuf
= qemu_spice_gl_release_dmabuf
,
1117 .dpy_gl_update
= qemu_spice_gl_update
,
1121 qemu_spice_is_compatible_dcl(DisplayGLCtx
*dgc
,
1122 DisplayChangeListener
*dcl
)
1124 return dcl
->ops
== &display_listener_gl_ops
;
1127 static const DisplayGLCtxOps gl_ctx_ops
= {
1128 .dpy_gl_ctx_is_compatible_dcl
= qemu_spice_is_compatible_dcl
,
1129 .dpy_gl_ctx_create
= qemu_spice_gl_create_context
,
1130 .dpy_gl_ctx_destroy
= qemu_egl_destroy_context
,
1131 .dpy_gl_ctx_make_current
= qemu_egl_make_context_current
,
1134 #endif /* HAVE_SPICE_GL */
1136 static void qemu_spice_display_init_one(QemuConsole
*con
)
1138 SimpleSpiceDisplay
*ssd
= g_new0(SimpleSpiceDisplay
, 1);
1140 qemu_spice_display_init_common(ssd
);
1142 ssd
->dcl
.ops
= &display_listener_ops
;
1143 #ifdef HAVE_SPICE_GL
1145 ssd
->dcl
.ops
= &display_listener_gl_ops
;
1146 ssd
->dgc
.ops
= &gl_ctx_ops
;
1147 ssd
->gl_unblock_bh
= qemu_bh_new(qemu_spice_gl_unblock_bh
, ssd
);
1148 ssd
->gl_unblock_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1149 qemu_spice_gl_block_timer
, ssd
);
1150 ssd
->gls
= qemu_gl_init_shader();
1151 ssd
->have_surface
= false;
1152 ssd
->have_scanout
= false;
1157 ssd
->qxl
.base
.sif
= &dpy_interface
.base
;
1158 qemu_spice_add_display_interface(&ssd
->qxl
, con
);
1160 #if SPICE_SERVER_VERSION >= 0x000e02 /* release 0.14.2 */
1162 char device_address
[256] = "";
1163 if (qemu_console_fill_device_address(con
, device_address
, 256, &err
)) {
1164 spice_qxl_set_device_info(&ssd
->qxl
,
1166 qemu_console_get_head(con
),
1169 error_report_err(err
);
1173 qemu_spice_create_host_memslot(ssd
);
1176 qemu_console_set_display_gl_ctx(con
, &ssd
->dgc
);
1178 register_displaychangelistener(&ssd
->dcl
);
1181 void qemu_spice_display_init(void)
1183 QemuOptsList
*olist
= qemu_find_opts("spice");
1184 QemuOpts
*opts
= QTAILQ_FIRST(&olist
->head
);
1185 QemuConsole
*spice_con
, *con
;
1189 str
= qemu_opt_get(opts
, "display");
1191 int head
= qemu_opt_get_number(opts
, "head", 0);
1194 spice_con
= qemu_console_lookup_by_device_name(str
, head
, &err
);
1196 error_report("Failed to lookup display/head");
1204 con
= qemu_console_lookup_by_index(i
);
1205 if (!con
|| !qemu_console_is_graphic(con
)) {
1208 if (qemu_spice_have_display_interface(con
)) {
1211 if (spice_con
!= NULL
&& spice_con
!= con
) {
1214 qemu_spice_display_init_one(con
);
1217 qemu_spice_display_init_done();