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"
32 static void GCC_FMT_ATTR(2, 3) dprint(int level
, const char *fmt
, ...)
38 vfprintf(stderr
, fmt
, args
);
43 int qemu_spice_rect_is_empty(const QXLRect
* r
)
45 return r
->top
== r
->bottom
|| r
->left
== r
->right
;
48 void qemu_spice_rect_union(QXLRect
*dest
, const QXLRect
*r
)
50 if (qemu_spice_rect_is_empty(r
)) {
54 if (qemu_spice_rect_is_empty(dest
)) {
59 dest
->top
= MIN(dest
->top
, r
->top
);
60 dest
->left
= MIN(dest
->left
, r
->left
);
61 dest
->bottom
= MAX(dest
->bottom
, r
->bottom
);
62 dest
->right
= MAX(dest
->right
, r
->right
);
65 QXLCookie
*qxl_cookie_new(int type
, uint64_t io
)
69 cookie
= g_malloc0(sizeof(*cookie
));
75 void qemu_spice_add_memslot(SimpleSpiceDisplay
*ssd
, QXLDevMemSlot
*memslot
,
78 trace_qemu_spice_add_memslot(ssd
->qxl
.id
, memslot
->slot_id
,
79 memslot
->virt_start
, memslot
->virt_end
,
82 if (async
!= QXL_SYNC
) {
83 spice_qxl_add_memslot_async(&ssd
->qxl
, memslot
,
84 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
85 QXL_IO_MEMSLOT_ADD_ASYNC
));
87 spice_qxl_add_memslot(&ssd
->qxl
, memslot
);
91 void qemu_spice_del_memslot(SimpleSpiceDisplay
*ssd
, uint32_t gid
, uint32_t sid
)
93 trace_qemu_spice_del_memslot(ssd
->qxl
.id
, gid
, sid
);
94 spice_qxl_del_memslot(&ssd
->qxl
, gid
, sid
);
97 void qemu_spice_create_primary_surface(SimpleSpiceDisplay
*ssd
, uint32_t id
,
98 QXLDevSurfaceCreate
*surface
,
101 trace_qemu_spice_create_primary_surface(ssd
->qxl
.id
, id
, surface
, async
);
102 if (async
!= QXL_SYNC
) {
103 spice_qxl_create_primary_surface_async(&ssd
->qxl
, id
, surface
,
104 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
105 QXL_IO_CREATE_PRIMARY_ASYNC
));
107 spice_qxl_create_primary_surface(&ssd
->qxl
, id
, surface
);
111 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay
*ssd
,
112 uint32_t id
, qxl_async_io async
)
114 trace_qemu_spice_destroy_primary_surface(ssd
->qxl
.id
, id
, async
);
115 if (async
!= QXL_SYNC
) {
116 spice_qxl_destroy_primary_surface_async(&ssd
->qxl
, id
,
117 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
118 QXL_IO_DESTROY_PRIMARY_ASYNC
));
120 spice_qxl_destroy_primary_surface(&ssd
->qxl
, id
);
124 void qemu_spice_wakeup(SimpleSpiceDisplay
*ssd
)
126 trace_qemu_spice_wakeup(ssd
->qxl
.id
);
127 spice_qxl_wakeup(&ssd
->qxl
);
130 static void qemu_spice_create_one_update(SimpleSpiceDisplay
*ssd
,
133 SimpleSpiceUpdate
*update
;
134 QXLDrawable
*drawable
;
138 struct timespec time_space
;
139 pixman_image_t
*dest
;
141 trace_qemu_spice_create_update(
142 rect
->left
, rect
->right
,
143 rect
->top
, rect
->bottom
);
145 update
= g_malloc0(sizeof(*update
));
146 drawable
= &update
->drawable
;
147 image
= &update
->image
;
148 cmd
= &update
->ext
.cmd
;
150 bw
= rect
->right
- rect
->left
;
151 bh
= rect
->bottom
- rect
->top
;
152 update
->bitmap
= g_malloc(bw
* bh
* 4);
154 drawable
->bbox
= *rect
;
155 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
156 drawable
->effect
= QXL_EFFECT_OPAQUE
;
157 drawable
->release_info
.id
= (uintptr_t)(&update
->ext
);
158 drawable
->type
= QXL_DRAW_COPY
;
159 drawable
->surfaces_dest
[0] = -1;
160 drawable
->surfaces_dest
[1] = -1;
161 drawable
->surfaces_dest
[2] = -1;
162 clock_gettime(CLOCK_MONOTONIC
, &time_space
);
163 /* time in milliseconds from epoch. */
164 drawable
->mm_time
= time_space
.tv_sec
* 1000
165 + time_space
.tv_nsec
/ 1000 / 1000;
167 drawable
->u
.copy
.rop_descriptor
= SPICE_ROPD_OP_PUT
;
168 drawable
->u
.copy
.src_bitmap
= (uintptr_t)image
;
169 drawable
->u
.copy
.src_area
.right
= bw
;
170 drawable
->u
.copy
.src_area
.bottom
= bh
;
172 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, ssd
->unique
++);
173 image
->descriptor
.type
= SPICE_IMAGE_TYPE_BITMAP
;
174 image
->bitmap
.flags
= QXL_BITMAP_DIRECT
| QXL_BITMAP_TOP_DOWN
;
175 image
->bitmap
.stride
= bw
* 4;
176 image
->descriptor
.width
= image
->bitmap
.x
= bw
;
177 image
->descriptor
.height
= image
->bitmap
.y
= bh
;
178 image
->bitmap
.data
= (uintptr_t)(update
->bitmap
);
179 image
->bitmap
.palette
= 0;
180 image
->bitmap
.format
= SPICE_BITMAP_FMT_32BIT
;
182 dest
= pixman_image_create_bits(PIXMAN_LE_x8r8g8b8
, bw
, bh
,
183 (void *)update
->bitmap
, bw
* 4);
184 pixman_image_composite(PIXMAN_OP_SRC
, ssd
->surface
, NULL
, ssd
->mirror
,
185 rect
->left
, rect
->top
, 0, 0,
186 rect
->left
, rect
->top
, bw
, bh
);
187 pixman_image_composite(PIXMAN_OP_SRC
, ssd
->mirror
, NULL
, dest
,
188 rect
->left
, rect
->top
, 0, 0,
190 pixman_image_unref(dest
);
192 cmd
->type
= QXL_CMD_DRAW
;
193 cmd
->data
= (uintptr_t)drawable
;
195 QTAILQ_INSERT_TAIL(&ssd
->updates
, update
, next
);
198 static void qemu_spice_create_update(SimpleSpiceDisplay
*ssd
)
200 static const int blksize
= 32;
201 int blocks
= DIV_ROUND_UP(surface_width(ssd
->ds
), blksize
);
202 int dirty_top
[blocks
];
203 int y
, yoff1
, yoff2
, x
, xoff
, blk
, bw
;
204 int bpp
= surface_bytes_per_pixel(ssd
->ds
);
205 uint8_t *guest
, *mirror
;
207 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
211 for (blk
= 0; blk
< blocks
; blk
++) {
215 guest
= surface_data(ssd
->ds
);
216 mirror
= (void *)pixman_image_get_data(ssd
->mirror
);
217 for (y
= ssd
->dirty
.top
; y
< ssd
->dirty
.bottom
; y
++) {
218 yoff1
= y
* surface_stride(ssd
->ds
);
219 yoff2
= y
* pixman_image_get_stride(ssd
->mirror
);
220 for (x
= ssd
->dirty
.left
; x
< ssd
->dirty
.right
; x
+= blksize
) {
223 bw
= MIN(blksize
, ssd
->dirty
.right
- x
);
224 if (memcmp(guest
+ yoff1
+ xoff
,
225 mirror
+ yoff2
+ xoff
,
227 if (dirty_top
[blk
] != -1) {
229 .top
= dirty_top
[blk
],
234 qemu_spice_create_one_update(ssd
, &update
);
238 if (dirty_top
[blk
] == -1) {
245 for (x
= ssd
->dirty
.left
; x
< ssd
->dirty
.right
; x
+= blksize
) {
247 bw
= MIN(blksize
, ssd
->dirty
.right
- x
);
248 if (dirty_top
[blk
] != -1) {
250 .top
= dirty_top
[blk
],
251 .bottom
= ssd
->dirty
.bottom
,
255 qemu_spice_create_one_update(ssd
, &update
);
260 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
263 static SimpleSpiceCursor
*
264 qemu_spice_create_cursor_update(SimpleSpiceDisplay
*ssd
,
268 size_t size
= c
? c
->width
* c
->height
* 4 : 0;
269 SimpleSpiceCursor
*update
;
274 update
= g_malloc0(sizeof(*update
) + size
);
276 cursor
= &update
->cursor
;
277 cmd
= &update
->ext
.cmd
;
280 ccmd
->type
= QXL_CURSOR_SET
;
281 ccmd
->u
.set
.position
.x
= ssd
->ptr_x
+ ssd
->hot_x
;
282 ccmd
->u
.set
.position
.y
= ssd
->ptr_y
+ ssd
->hot_y
;
283 ccmd
->u
.set
.visible
= true;
284 ccmd
->u
.set
.shape
= (uintptr_t)cursor
;
285 cursor
->header
.unique
= ssd
->unique
++;
286 cursor
->header
.type
= SPICE_CURSOR_TYPE_ALPHA
;
287 cursor
->header
.width
= c
->width
;
288 cursor
->header
.height
= c
->height
;
289 cursor
->header
.hot_spot_x
= c
->hot_x
;
290 cursor
->header
.hot_spot_y
= c
->hot_y
;
291 cursor
->data_size
= size
;
292 cursor
->chunk
.data_size
= size
;
293 memcpy(cursor
->chunk
.data
, c
->data
, size
);
295 ccmd
->type
= QXL_CURSOR_HIDE
;
297 ccmd
->type
= QXL_CURSOR_MOVE
;
298 ccmd
->u
.position
.x
= ssd
->ptr_x
+ ssd
->hot_x
;
299 ccmd
->u
.position
.y
= ssd
->ptr_y
+ ssd
->hot_y
;
301 ccmd
->release_info
.id
= (uintptr_t)(&update
->ext
);
303 cmd
->type
= QXL_CMD_CURSOR
;
304 cmd
->data
= (uintptr_t)ccmd
;
310 * Called from spice server thread context (via interface_release_resource)
311 * We do *not* hold the global qemu mutex here, so extra care is needed
312 * when calling qemu functions. QEMU interfaces used:
313 * - g_free (underlying glibc free is re-entrant).
315 void qemu_spice_destroy_update(SimpleSpiceDisplay
*sdpy
, SimpleSpiceUpdate
*update
)
317 g_free(update
->bitmap
);
321 void qemu_spice_create_host_memslot(SimpleSpiceDisplay
*ssd
)
323 QXLDevMemSlot memslot
;
325 dprint(1, "%s/%d:\n", __func__
, ssd
->qxl
.id
);
327 memset(&memslot
, 0, sizeof(memslot
));
328 memslot
.slot_group_id
= MEMSLOT_GROUP_HOST
;
329 memslot
.virt_end
= ~0;
330 qemu_spice_add_memslot(ssd
, &memslot
, QXL_SYNC
);
333 void qemu_spice_create_host_primary(SimpleSpiceDisplay
*ssd
)
335 QXLDevSurfaceCreate surface
;
336 uint64_t surface_size
;
338 memset(&surface
, 0, sizeof(surface
));
340 surface_size
= (uint64_t) surface_width(ssd
->ds
) *
341 surface_height(ssd
->ds
) * 4;
342 assert(surface_size
> 0);
343 assert(surface_size
< INT_MAX
);
344 if (ssd
->bufsize
< surface_size
) {
345 ssd
->bufsize
= surface_size
;
347 ssd
->buf
= g_malloc(ssd
->bufsize
);
350 dprint(1, "%s/%d: %ux%u (size %" PRIu64
"/%d)\n", __func__
, ssd
->qxl
.id
,
351 surface_width(ssd
->ds
), surface_height(ssd
->ds
),
352 surface_size
, ssd
->bufsize
);
354 surface
.format
= SPICE_SURFACE_FMT_32_xRGB
;
355 surface
.width
= surface_width(ssd
->ds
);
356 surface
.height
= surface_height(ssd
->ds
);
357 surface
.stride
= -surface
.width
* 4;
358 surface
.mouse_mode
= true;
361 surface
.mem
= (uintptr_t)ssd
->buf
;
362 surface
.group_id
= MEMSLOT_GROUP_HOST
;
364 qemu_spice_create_primary_surface(ssd
, 0, &surface
, QXL_SYNC
);
367 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay
*ssd
)
369 dprint(1, "%s/%d:\n", __func__
, ssd
->qxl
.id
);
371 qemu_spice_destroy_primary_surface(ssd
, 0, QXL_SYNC
);
374 void qemu_spice_display_init_common(SimpleSpiceDisplay
*ssd
)
376 qemu_mutex_init(&ssd
->lock
);
377 QTAILQ_INIT(&ssd
->updates
);
380 if (ssd
->num_surfaces
== 0) {
381 ssd
->num_surfaces
= 1024;
385 /* display listener callbacks */
387 void qemu_spice_display_update(SimpleSpiceDisplay
*ssd
,
388 int x
, int y
, int w
, int h
)
392 dprint(2, "%s/%d: x %d y %d w %d h %d\n", __func__
,
393 ssd
->qxl
.id
, x
, y
, w
, h
);
394 update_area
.left
= x
,
395 update_area
.right
= x
+ w
;
397 update_area
.bottom
= y
+ h
;
399 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
402 qemu_spice_rect_union(&ssd
->dirty
, &update_area
);
405 void qemu_spice_display_switch(SimpleSpiceDisplay
*ssd
,
406 DisplaySurface
*surface
)
408 SimpleSpiceUpdate
*update
;
411 if (surface
&& ssd
->surface
&&
412 surface_width(surface
) == pixman_image_get_width(ssd
->surface
) &&
413 surface_height(surface
) == pixman_image_get_height(ssd
->surface
) &&
414 surface_format(surface
) == pixman_image_get_format(ssd
->surface
)) {
415 /* no-resize fast path: just swap backing store */
416 dprint(1, "%s/%d: fast (%dx%d)\n", __func__
, ssd
->qxl
.id
,
417 surface_width(surface
), surface_height(surface
));
418 qemu_mutex_lock(&ssd
->lock
);
420 pixman_image_unref(ssd
->surface
);
421 ssd
->surface
= pixman_image_ref(ssd
->ds
->image
);
422 qemu_mutex_unlock(&ssd
->lock
);
423 qemu_spice_display_update(ssd
, 0, 0,
424 surface_width(surface
),
425 surface_height(surface
));
429 /* full mode switch */
430 dprint(1, "%s/%d: full (%dx%d -> %dx%d)\n", __func__
, ssd
->qxl
.id
,
431 ssd
->surface
? pixman_image_get_width(ssd
->surface
) : 0,
432 ssd
->surface
? pixman_image_get_height(ssd
->surface
) : 0,
433 surface
? surface_width(surface
) : 0,
434 surface
? surface_height(surface
) : 0);
436 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
438 pixman_image_unref(ssd
->surface
);
440 pixman_image_unref(ssd
->mirror
);
444 qemu_mutex_lock(&ssd
->lock
);
445 need_destroy
= (ssd
->ds
!= NULL
);
447 while ((update
= QTAILQ_FIRST(&ssd
->updates
)) != NULL
) {
448 QTAILQ_REMOVE(&ssd
->updates
, update
, next
);
449 qemu_spice_destroy_update(ssd
, update
);
451 qemu_mutex_unlock(&ssd
->lock
);
453 qemu_spice_destroy_host_primary(ssd
);
456 ssd
->surface
= pixman_image_ref(ssd
->ds
->image
);
457 ssd
->mirror
= qemu_pixman_mirror_create(ssd
->ds
->format
,
459 qemu_spice_create_host_primary(ssd
);
462 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
465 qemu_mutex_lock(&ssd
->lock
);
467 g_free(ssd
->ptr_define
);
468 ssd
->ptr_define
= qemu_spice_create_cursor_update(ssd
, ssd
->cursor
, 0);
470 qemu_mutex_unlock(&ssd
->lock
);
473 static void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay
*ssd
)
476 assert(ssd
->dcl
.con
);
477 dpy_cursor_define(ssd
->dcl
.con
, ssd
->cursor
);
479 if (ssd
->mouse_x
!= -1 && ssd
->mouse_y
!= -1) {
480 assert(ssd
->dcl
.con
);
481 dpy_mouse_set(ssd
->dcl
.con
, ssd
->mouse_x
, ssd
->mouse_y
, 1);
487 void qemu_spice_cursor_refresh_bh(void *opaque
)
489 SimpleSpiceDisplay
*ssd
= opaque
;
491 qemu_mutex_lock(&ssd
->lock
);
492 qemu_spice_cursor_refresh_unlocked(ssd
);
493 qemu_mutex_unlock(&ssd
->lock
);
496 void qemu_spice_display_refresh(SimpleSpiceDisplay
*ssd
)
498 dprint(3, "%s/%d:\n", __func__
, ssd
->qxl
.id
);
499 graphic_hw_update(ssd
->dcl
.con
);
501 qemu_mutex_lock(&ssd
->lock
);
502 if (QTAILQ_EMPTY(&ssd
->updates
) && ssd
->ds
) {
503 qemu_spice_create_update(ssd
);
506 qemu_mutex_unlock(&ssd
->lock
);
510 qemu_spice_wakeup(ssd
);
511 dprint(2, "%s/%d: notify\n", __func__
, ssd
->qxl
.id
);
515 /* spice display interface callbacks */
517 static void interface_attach_worker(QXLInstance
*sin
, QXLWorker
*qxl_worker
)
519 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
521 dprint(1, "%s/%d:\n", __func__
, ssd
->qxl
.id
);
524 static void interface_set_compression_level(QXLInstance
*sin
, int level
)
526 dprint(1, "%s/%d:\n", __func__
, sin
->id
);
530 #if SPICE_NEEDS_SET_MM_TIME
531 static void interface_set_mm_time(QXLInstance
*sin
, uint32_t mm_time
)
533 dprint(3, "%s/%d:\n", __func__
, sin
->id
);
538 static void interface_get_init_info(QXLInstance
*sin
, QXLDevInitInfo
*info
)
540 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
542 info
->memslot_gen_bits
= MEMSLOT_GENERATION_BITS
;
543 info
->memslot_id_bits
= MEMSLOT_SLOT_BITS
;
544 info
->num_memslots
= NUM_MEMSLOTS
;
545 info
->num_memslots_groups
= NUM_MEMSLOTS_GROUPS
;
546 info
->internal_groupslot_id
= 0;
547 info
->qxl_ram_size
= 16 * 1024 * 1024;
548 info
->n_surfaces
= ssd
->num_surfaces
;
551 static int interface_get_command(QXLInstance
*sin
, QXLCommandExt
*ext
)
553 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
554 SimpleSpiceUpdate
*update
;
557 dprint(3, "%s/%d:\n", __func__
, ssd
->qxl
.id
);
559 qemu_mutex_lock(&ssd
->lock
);
560 update
= QTAILQ_FIRST(&ssd
->updates
);
561 if (update
!= NULL
) {
562 QTAILQ_REMOVE(&ssd
->updates
, update
, next
);
566 qemu_mutex_unlock(&ssd
->lock
);
571 static int interface_req_cmd_notification(QXLInstance
*sin
)
573 dprint(2, "%s/%d:\n", __func__
, sin
->id
);
577 static void interface_release_resource(QXLInstance
*sin
,
578 QXLReleaseInfoExt rext
)
580 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
581 SimpleSpiceUpdate
*update
;
582 SimpleSpiceCursor
*cursor
;
585 dprint(2, "%s/%d:\n", __func__
, ssd
->qxl
.id
);
586 ext
= (void *)(intptr_t)(rext
.info
->id
);
587 switch (ext
->cmd
.type
) {
589 update
= container_of(ext
, SimpleSpiceUpdate
, ext
);
590 qemu_spice_destroy_update(ssd
, update
);
593 cursor
= container_of(ext
, SimpleSpiceCursor
, ext
);
597 g_assert_not_reached();
601 static int interface_get_cursor_command(QXLInstance
*sin
, QXLCommandExt
*ext
)
603 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
606 dprint(3, "%s/%d:\n", __func__
, ssd
->qxl
.id
);
608 qemu_mutex_lock(&ssd
->lock
);
609 if (ssd
->ptr_define
) {
610 *ext
= ssd
->ptr_define
->ext
;
611 ssd
->ptr_define
= NULL
;
613 } else if (ssd
->ptr_move
) {
614 *ext
= ssd
->ptr_move
->ext
;
615 ssd
->ptr_move
= NULL
;
620 qemu_mutex_unlock(&ssd
->lock
);
624 static int interface_req_cursor_notification(QXLInstance
*sin
)
626 dprint(2, "%s:\n", __func__
);
630 static void interface_notify_update(QXLInstance
*sin
, uint32_t update_id
)
632 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
636 static int interface_flush_resources(QXLInstance
*sin
)
638 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
643 static void interface_update_area_complete(QXLInstance
*sin
,
645 QXLRect
*dirty
, uint32_t num_updated_rects
)
647 /* should never be called, used in qxl native mode only */
648 fprintf(stderr
, "%s: abort()\n", __func__
);
652 /* called from spice server thread context only */
653 static void interface_async_complete(QXLInstance
*sin
, uint64_t cookie_token
)
655 QXLCookie
*cookie
= (QXLCookie
*)(uintptr_t)cookie_token
;
657 switch (cookie
->type
) {
659 case QXL_COOKIE_TYPE_GL_DRAW_DONE
:
661 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
662 qemu_bh_schedule(ssd
->gl_unblock_bh
);
665 case QXL_COOKIE_TYPE_IO
:
666 if (cookie
->io
== QXL_IO_MONITORS_CONFIG_ASYNC
) {
667 g_free(cookie
->u
.data
);
672 /* should never be called, used in qxl native mode only */
673 fprintf(stderr
, "%s: abort()\n", __func__
);
679 static void interface_set_client_capabilities(QXLInstance
*sin
,
680 uint8_t client_present
,
683 dprint(3, "%s:\n", __func__
);
686 static int interface_client_monitors_config(QXLInstance
*sin
,
687 VDAgentMonitorsConfig
*mc
)
689 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
693 if (!dpy_ui_info_supported(ssd
->dcl
.con
)) {
694 return 0; /* == not supported by guest */
701 memset(&info
, 0, sizeof(info
));
703 head
= qemu_console_get_head(ssd
->dcl
.con
);
704 if (mc
->num_of_monitors
> head
) {
705 info
.width
= mc
->monitors
[head
].width
;
706 info
.height
= mc
->monitors
[head
].height
;
708 dpy_set_ui_info(ssd
->dcl
.con
, &info
);
709 dprint(1, "%s/%d: size %dx%d\n", __func__
, ssd
->qxl
.id
,
710 info
.width
, info
.height
);
714 static const QXLInterface dpy_interface
= {
715 .base
.type
= SPICE_INTERFACE_QXL
,
716 .base
.description
= "qemu simple display",
717 .base
.major_version
= SPICE_INTERFACE_QXL_MAJOR
,
718 .base
.minor_version
= SPICE_INTERFACE_QXL_MINOR
,
720 .attache_worker
= interface_attach_worker
,
721 .set_compression_level
= interface_set_compression_level
,
722 #if SPICE_NEEDS_SET_MM_TIME
723 .set_mm_time
= interface_set_mm_time
,
725 .get_init_info
= interface_get_init_info
,
727 /* the callbacks below are called from spice server thread context */
728 .get_command
= interface_get_command
,
729 .req_cmd_notification
= interface_req_cmd_notification
,
730 .release_resource
= interface_release_resource
,
731 .get_cursor_command
= interface_get_cursor_command
,
732 .req_cursor_notification
= interface_req_cursor_notification
,
733 .notify_update
= interface_notify_update
,
734 .flush_resources
= interface_flush_resources
,
735 .async_complete
= interface_async_complete
,
736 .update_area_complete
= interface_update_area_complete
,
737 .set_client_capabilities
= interface_set_client_capabilities
,
738 .client_monitors_config
= interface_client_monitors_config
,
741 static void display_update(DisplayChangeListener
*dcl
,
742 int x
, int y
, int w
, int h
)
744 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
745 qemu_spice_display_update(ssd
, x
, y
, w
, h
);
748 static void display_switch(DisplayChangeListener
*dcl
,
749 DisplaySurface
*surface
)
751 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
752 qemu_spice_display_switch(ssd
, surface
);
755 static void display_refresh(DisplayChangeListener
*dcl
)
757 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
758 qemu_spice_display_refresh(ssd
);
761 static void display_mouse_set(DisplayChangeListener
*dcl
,
762 int x
, int y
, int on
)
764 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
766 qemu_mutex_lock(&ssd
->lock
);
769 g_free(ssd
->ptr_move
);
770 ssd
->ptr_move
= qemu_spice_create_cursor_update(ssd
, NULL
, on
);
771 qemu_mutex_unlock(&ssd
->lock
);
772 qemu_spice_wakeup(ssd
);
775 static void display_mouse_define(DisplayChangeListener
*dcl
,
778 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
780 qemu_mutex_lock(&ssd
->lock
);
782 cursor_put(ssd
->cursor
);
784 ssd
->hot_x
= c
->hot_x
;
785 ssd
->hot_y
= c
->hot_y
;
786 g_free(ssd
->ptr_move
);
787 ssd
->ptr_move
= NULL
;
788 g_free(ssd
->ptr_define
);
789 ssd
->ptr_define
= qemu_spice_create_cursor_update(ssd
, c
, 0);
790 qemu_mutex_unlock(&ssd
->lock
);
791 qemu_spice_wakeup(ssd
);
794 static const DisplayChangeListenerOps display_listener_ops
= {
796 .dpy_gfx_update
= display_update
,
797 .dpy_gfx_switch
= display_switch
,
798 .dpy_gfx_check_format
= qemu_pixman_check_format
,
799 .dpy_refresh
= display_refresh
,
800 .dpy_mouse_set
= display_mouse_set
,
801 .dpy_cursor_define
= display_mouse_define
,
806 static void qemu_spice_gl_monitor_config(SimpleSpiceDisplay
*ssd
,
807 int x
, int y
, int w
, int h
)
809 QXLMonitorsConfig
*config
;
812 config
= g_malloc0(sizeof(QXLMonitorsConfig
) + sizeof(QXLHead
));
814 config
->max_allowed
= 1;
815 config
->heads
[0].x
= x
;
816 config
->heads
[0].y
= y
;
817 config
->heads
[0].width
= w
;
818 config
->heads
[0].height
= h
;
819 cookie
= qxl_cookie_new(QXL_COOKIE_TYPE_IO
,
820 QXL_IO_MONITORS_CONFIG_ASYNC
);
821 cookie
->u
.data
= config
;
823 spice_qxl_monitors_config_async(&ssd
->qxl
,
829 static void qemu_spice_gl_block(SimpleSpiceDisplay
*ssd
, bool block
)
834 timeout
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
835 timeout
+= 1000; /* one sec */
836 timer_mod(ssd
->gl_unblock_timer
, timeout
);
838 timer_del(ssd
->gl_unblock_timer
);
840 graphic_hw_gl_block(ssd
->dcl
.con
, block
);
843 static void qemu_spice_gl_unblock_bh(void *opaque
)
845 SimpleSpiceDisplay
*ssd
= opaque
;
847 qemu_spice_gl_block(ssd
, false);
850 static void qemu_spice_gl_block_timer(void *opaque
)
852 warn_report("spice: no gl-draw-done within one second");
855 static void spice_gl_refresh(DisplayChangeListener
*dcl
)
857 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
860 if (!ssd
->ds
|| qemu_console_is_gl_blocked(ssd
->dcl
.con
)) {
864 graphic_hw_update(dcl
->con
);
865 if (ssd
->gl_updates
&& ssd
->have_surface
) {
866 qemu_spice_gl_block(ssd
, true);
867 cookie
= (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE
, 0);
868 spice_qxl_gl_draw_async(&ssd
->qxl
, 0, 0,
869 surface_width(ssd
->ds
),
870 surface_height(ssd
->ds
),
876 static void spice_gl_update(DisplayChangeListener
*dcl
,
877 int x
, int y
, int w
, int h
)
879 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
881 surface_gl_update_texture(ssd
->gls
, ssd
->ds
, x
, y
, w
, h
);
885 static void spice_gl_switch(DisplayChangeListener
*dcl
,
886 struct DisplaySurface
*new_surface
)
888 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
889 EGLint stride
, fourcc
;
893 surface_gl_destroy_texture(ssd
->gls
, ssd
->ds
);
895 ssd
->ds
= new_surface
;
897 surface_gl_create_texture(ssd
->gls
, ssd
->ds
);
898 fd
= egl_get_fd_for_texture(ssd
->ds
->texture
,
901 surface_gl_destroy_texture(ssd
->gls
, ssd
->ds
);
905 dprint(1, "%s: %dx%d (stride %d/%d, fourcc 0x%x)\n", __func__
,
906 surface_width(ssd
->ds
), surface_height(ssd
->ds
),
907 surface_stride(ssd
->ds
), stride
, fourcc
);
909 /* note: spice server will close the fd */
910 spice_qxl_gl_scanout(&ssd
->qxl
, fd
,
911 surface_width(ssd
->ds
),
912 surface_height(ssd
->ds
),
913 stride
, fourcc
, false);
914 ssd
->have_surface
= true;
915 ssd
->have_scanout
= false;
917 qemu_spice_gl_monitor_config(ssd
, 0, 0,
918 surface_width(ssd
->ds
),
919 surface_height(ssd
->ds
));
923 static QEMUGLContext
qemu_spice_gl_create_context(DisplayChangeListener
*dcl
,
924 QEMUGLParams
*params
)
926 eglMakeCurrent(qemu_egl_display
, EGL_NO_SURFACE
, EGL_NO_SURFACE
,
928 return qemu_egl_create_context(dcl
, params
);
931 static void qemu_spice_gl_scanout_disable(DisplayChangeListener
*dcl
)
933 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
935 dprint(1, "%s: no framebuffer\n", __func__
);
936 spice_qxl_gl_scanout(&ssd
->qxl
, -1, 0, 0, 0, 0, false);
937 qemu_spice_gl_monitor_config(ssd
, 0, 0, 0, 0);
938 ssd
->have_surface
= false;
939 ssd
->have_scanout
= false;
942 static void qemu_spice_gl_scanout_texture(DisplayChangeListener
*dcl
,
945 uint32_t backing_width
,
946 uint32_t backing_height
,
947 uint32_t x
, uint32_t y
,
948 uint32_t w
, uint32_t h
)
950 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
951 EGLint stride
= 0, fourcc
= 0;
955 fd
= egl_get_fd_for_texture(tex_id
, &stride
, &fourcc
);
957 fprintf(stderr
, "%s: failed to get fd for texture\n", __func__
);
960 dprint(1, "%s: %dx%d (stride %d, fourcc 0x%x)\n", __func__
,
961 w
, h
, stride
, fourcc
);
963 /* note: spice server will close the fd */
964 spice_qxl_gl_scanout(&ssd
->qxl
, fd
, backing_width
, backing_height
,
965 stride
, fourcc
, y_0_top
);
966 qemu_spice_gl_monitor_config(ssd
, x
, y
, w
, h
);
967 ssd
->have_surface
= false;
968 ssd
->have_scanout
= true;
971 static void qemu_spice_gl_update(DisplayChangeListener
*dcl
,
972 uint32_t x
, uint32_t y
, uint32_t w
, uint32_t h
)
974 SimpleSpiceDisplay
*ssd
= container_of(dcl
, SimpleSpiceDisplay
, dcl
);
977 if (!ssd
->have_scanout
) {
981 dprint(2, "%s: %dx%d+%d+%d\n", __func__
, w
, h
, x
, y
);
982 qemu_spice_gl_block(ssd
, true);
983 cookie
= (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE
, 0);
984 spice_qxl_gl_draw_async(&ssd
->qxl
, x
, y
, w
, h
, cookie
);
987 static const DisplayChangeListenerOps display_listener_gl_ops
= {
988 .dpy_name
= "spice-egl",
989 .dpy_gfx_update
= spice_gl_update
,
990 .dpy_gfx_switch
= spice_gl_switch
,
991 .dpy_gfx_check_format
= console_gl_check_format
,
992 .dpy_refresh
= spice_gl_refresh
,
993 .dpy_mouse_set
= display_mouse_set
,
994 .dpy_cursor_define
= display_mouse_define
,
996 .dpy_gl_ctx_create
= qemu_spice_gl_create_context
,
997 .dpy_gl_ctx_destroy
= qemu_egl_destroy_context
,
998 .dpy_gl_ctx_make_current
= qemu_egl_make_context_current
,
999 .dpy_gl_ctx_get_current
= qemu_egl_get_current_context
,
1001 .dpy_gl_scanout_disable
= qemu_spice_gl_scanout_disable
,
1002 .dpy_gl_scanout_texture
= qemu_spice_gl_scanout_texture
,
1003 .dpy_gl_update
= qemu_spice_gl_update
,
1006 #endif /* HAVE_SPICE_GL */
1008 static void qemu_spice_display_init_one(QemuConsole
*con
)
1010 SimpleSpiceDisplay
*ssd
= g_new0(SimpleSpiceDisplay
, 1);
1012 qemu_spice_display_init_common(ssd
);
1014 ssd
->dcl
.ops
= &display_listener_ops
;
1015 #ifdef HAVE_SPICE_GL
1017 ssd
->dcl
.ops
= &display_listener_gl_ops
;
1018 ssd
->gl_unblock_bh
= qemu_bh_new(qemu_spice_gl_unblock_bh
, ssd
);
1019 ssd
->gl_unblock_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
,
1020 qemu_spice_gl_block_timer
, ssd
);
1021 ssd
->gls
= qemu_gl_init_shader();
1022 ssd
->have_surface
= false;
1023 ssd
->have_scanout
= false;
1028 ssd
->qxl
.base
.sif
= &dpy_interface
.base
;
1029 qemu_spice_add_display_interface(&ssd
->qxl
, con
);
1030 qemu_spice_create_host_memslot(ssd
);
1032 register_displaychangelistener(&ssd
->dcl
);
1035 void qemu_spice_display_init(void)
1037 QemuOptsList
*olist
= qemu_find_opts("spice");
1038 QemuOpts
*opts
= QTAILQ_FIRST(&olist
->head
);
1039 QemuConsole
*spice_con
, *con
;
1043 str
= qemu_opt_get(opts
, "display");
1045 int head
= qemu_opt_get_number(opts
, "head", 0);
1048 spice_con
= qemu_console_lookup_by_device_name(str
, head
, &err
);
1050 error_report("Failed to lookup display/head");
1058 con
= qemu_console_lookup_by_index(i
);
1059 if (!con
|| !qemu_console_is_graphic(con
)) {
1062 if (qemu_spice_have_display_interface(con
)) {
1065 if (spice_con
!= NULL
&& spice_con
!= con
) {
1068 qemu_spice_display_init_one(con
);