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-common.h"
19 #include "qemu-spice.h"
20 #include "qemu-timer.h"
21 #include "qemu-queue.h"
26 #include "spice-display.h"
30 static void GCC_FMT_ATTR(2, 3) dprint(int level
, const char *fmt
, ...)
36 vfprintf(stderr
, fmt
, args
);
41 int qemu_spice_rect_is_empty(const QXLRect
* r
)
43 return r
->top
== r
->bottom
|| r
->left
== r
->right
;
46 void qemu_spice_rect_union(QXLRect
*dest
, const QXLRect
*r
)
48 if (qemu_spice_rect_is_empty(r
)) {
52 if (qemu_spice_rect_is_empty(dest
)) {
57 dest
->top
= MIN(dest
->top
, r
->top
);
58 dest
->left
= MIN(dest
->left
, r
->left
);
59 dest
->bottom
= MAX(dest
->bottom
, r
->bottom
);
60 dest
->right
= MAX(dest
->right
, r
->right
);
63 void qemu_spice_add_memslot(SimpleSpiceDisplay
*ssd
, QXLDevMemSlot
*memslot
,
66 if (async
!= QXL_SYNC
) {
67 #if SPICE_INTERFACE_QXL_MINOR >= 1
68 spice_qxl_add_memslot_async(&ssd
->qxl
, memslot
, 0);
73 ssd
->worker
->add_memslot(ssd
->worker
, memslot
);
77 void qemu_spice_del_memslot(SimpleSpiceDisplay
*ssd
, uint32_t gid
, uint32_t sid
)
79 ssd
->worker
->del_memslot(ssd
->worker
, gid
, sid
);
82 void qemu_spice_create_primary_surface(SimpleSpiceDisplay
*ssd
, uint32_t id
,
83 QXLDevSurfaceCreate
*surface
,
86 if (async
!= QXL_SYNC
) {
87 #if SPICE_INTERFACE_QXL_MINOR >= 1
88 spice_qxl_create_primary_surface_async(&ssd
->qxl
, id
, surface
, 0);
93 ssd
->worker
->create_primary_surface(ssd
->worker
, id
, surface
);
98 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay
*ssd
,
99 uint32_t id
, qxl_async_io async
)
101 if (async
!= QXL_SYNC
) {
102 #if SPICE_INTERFACE_QXL_MINOR >= 1
103 spice_qxl_destroy_primary_surface_async(&ssd
->qxl
, id
, 0);
108 ssd
->worker
->destroy_primary_surface(ssd
->worker
, id
);
112 void qemu_spice_wakeup(SimpleSpiceDisplay
*ssd
)
114 ssd
->worker
->wakeup(ssd
->worker
);
117 void qemu_spice_start(SimpleSpiceDisplay
*ssd
)
119 ssd
->worker
->start(ssd
->worker
);
122 void qemu_spice_stop(SimpleSpiceDisplay
*ssd
)
124 ssd
->worker
->stop(ssd
->worker
);
127 static SimpleSpiceUpdate
*qemu_spice_create_update(SimpleSpiceDisplay
*ssd
)
129 SimpleSpiceUpdate
*update
;
130 QXLDrawable
*drawable
;
135 struct timespec time_space
;
137 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
141 dprint(2, "%s: lr %d -> %d, tb -> %d -> %d\n", __FUNCTION__
,
142 ssd
->dirty
.left
, ssd
->dirty
.right
,
143 ssd
->dirty
.top
, ssd
->dirty
.bottom
);
145 update
= g_malloc0(sizeof(*update
));
146 drawable
= &update
->drawable
;
147 image
= &update
->image
;
148 cmd
= &update
->ext
.cmd
;
150 bw
= ssd
->dirty
.right
- ssd
->dirty
.left
;
151 bh
= ssd
->dirty
.bottom
- ssd
->dirty
.top
;
152 update
->bitmap
= g_malloc(bw
* bh
* 4);
154 drawable
->bbox
= ssd
->dirty
;
155 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
156 drawable
->effect
= QXL_EFFECT_OPAQUE
;
157 drawable
->release_info
.id
= (intptr_t)update
;
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
= (intptr_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
= (intptr_t)(update
->bitmap
);
179 image
->bitmap
.palette
= 0;
180 image
->bitmap
.format
= SPICE_BITMAP_FMT_32BIT
;
182 if (ssd
->conv
== NULL
) {
183 PixelFormat dst
= qemu_default_pixelformat(32);
184 ssd
->conv
= qemu_pf_conv_get(&dst
, &ssd
->ds
->surface
->pf
);
188 src
= ds_get_data(ssd
->ds
) +
189 ssd
->dirty
.top
* ds_get_linesize(ssd
->ds
) +
190 ssd
->dirty
.left
* ds_get_bytes_per_pixel(ssd
->ds
);
191 dst
= update
->bitmap
;
192 for (by
= 0; by
< bh
; by
++) {
193 qemu_pf_conv_run(ssd
->conv
, dst
, src
, bw
);
194 src
+= ds_get_linesize(ssd
->ds
);
195 dst
+= image
->bitmap
.stride
;
198 cmd
->type
= QXL_CMD_DRAW
;
199 cmd
->data
= (intptr_t)drawable
;
201 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
206 * Called from spice server thread context (via interface_release_ressource)
207 * We do *not* hold the global qemu mutex here, so extra care is needed
208 * when calling qemu functions. Qemu interfaces used:
209 * - g_free (underlying glibc free is re-entrant).
211 void qemu_spice_destroy_update(SimpleSpiceDisplay
*sdpy
, SimpleSpiceUpdate
*update
)
213 g_free(update
->bitmap
);
217 void qemu_spice_create_host_memslot(SimpleSpiceDisplay
*ssd
)
219 QXLDevMemSlot memslot
;
221 dprint(1, "%s:\n", __FUNCTION__
);
223 memset(&memslot
, 0, sizeof(memslot
));
224 memslot
.slot_group_id
= MEMSLOT_GROUP_HOST
;
225 memslot
.virt_end
= ~0;
226 qemu_spice_add_memslot(ssd
, &memslot
, QXL_SYNC
);
229 void qemu_spice_create_host_primary(SimpleSpiceDisplay
*ssd
)
231 QXLDevSurfaceCreate surface
;
233 dprint(1, "%s: %dx%d\n", __FUNCTION__
,
234 ds_get_width(ssd
->ds
), ds_get_height(ssd
->ds
));
236 surface
.format
= SPICE_SURFACE_FMT_32_xRGB
;
237 surface
.width
= ds_get_width(ssd
->ds
);
238 surface
.height
= ds_get_height(ssd
->ds
);
239 surface
.stride
= -surface
.width
* 4;
240 surface
.mouse_mode
= true;
243 surface
.mem
= (intptr_t)ssd
->buf
;
244 surface
.group_id
= MEMSLOT_GROUP_HOST
;
246 qemu_spice_create_primary_surface(ssd
, 0, &surface
, QXL_SYNC
);
249 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay
*ssd
)
251 dprint(1, "%s:\n", __FUNCTION__
);
253 qemu_spice_destroy_primary_surface(ssd
, 0, QXL_SYNC
);
256 void qemu_spice_vm_change_state_handler(void *opaque
, int running
,
259 SimpleSpiceDisplay
*ssd
= opaque
;
263 qemu_spice_start(ssd
);
265 qemu_spice_stop(ssd
);
266 ssd
->running
= false;
270 void qemu_spice_display_init_common(SimpleSpiceDisplay
*ssd
, DisplayState
*ds
)
273 qemu_mutex_init(&ssd
->lock
);
276 ssd
->bufsize
= (16 * 1024 * 1024);
277 ssd
->buf
= g_malloc(ssd
->bufsize
);
280 /* display listener callbacks */
282 void qemu_spice_display_update(SimpleSpiceDisplay
*ssd
,
283 int x
, int y
, int w
, int h
)
287 dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__
, x
, y
, w
, h
);
288 update_area
.left
= x
,
289 update_area
.right
= x
+ w
;
291 update_area
.bottom
= y
+ h
;
293 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
296 qemu_spice_rect_union(&ssd
->dirty
, &update_area
);
299 void qemu_spice_display_resize(SimpleSpiceDisplay
*ssd
)
301 dprint(1, "%s:\n", __FUNCTION__
);
303 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
304 qemu_pf_conv_put(ssd
->conv
);
307 qemu_mutex_lock(&ssd
->lock
);
308 if (ssd
->update
!= NULL
) {
309 qemu_spice_destroy_update(ssd
, ssd
->update
);
312 qemu_mutex_unlock(&ssd
->lock
);
313 qemu_spice_destroy_host_primary(ssd
);
314 qemu_spice_create_host_primary(ssd
);
316 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
320 void qemu_spice_display_refresh(SimpleSpiceDisplay
*ssd
)
322 dprint(3, "%s:\n", __FUNCTION__
);
325 qemu_mutex_lock(&ssd
->lock
);
326 if (ssd
->update
== NULL
) {
327 ssd
->update
= qemu_spice_create_update(ssd
);
331 ssd
->ds
->cursor_define(ssd
->cursor
);
332 cursor_put(ssd
->cursor
);
335 if (ssd
->mouse_x
!= -1 && ssd
->mouse_y
!= -1) {
336 ssd
->ds
->mouse_set(ssd
->mouse_x
, ssd
->mouse_y
, 1);
340 qemu_mutex_unlock(&ssd
->lock
);
344 qemu_spice_wakeup(ssd
);
345 dprint(2, "%s: notify\n", __FUNCTION__
);
349 /* spice display interface callbacks */
351 static void interface_attach_worker(QXLInstance
*sin
, QXLWorker
*qxl_worker
)
353 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
355 dprint(1, "%s:\n", __FUNCTION__
);
356 ssd
->worker
= qxl_worker
;
359 static void interface_set_compression_level(QXLInstance
*sin
, int level
)
361 dprint(1, "%s:\n", __FUNCTION__
);
365 static void interface_set_mm_time(QXLInstance
*sin
, uint32_t mm_time
)
367 dprint(3, "%s:\n", __FUNCTION__
);
371 static void interface_get_init_info(QXLInstance
*sin
, QXLDevInitInfo
*info
)
373 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
375 info
->memslot_gen_bits
= MEMSLOT_GENERATION_BITS
;
376 info
->memslot_id_bits
= MEMSLOT_SLOT_BITS
;
377 info
->num_memslots
= NUM_MEMSLOTS
;
378 info
->num_memslots_groups
= NUM_MEMSLOTS_GROUPS
;
379 info
->internal_groupslot_id
= 0;
380 info
->qxl_ram_size
= ssd
->bufsize
;
381 info
->n_surfaces
= NUM_SURFACES
;
384 static int interface_get_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
386 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
387 SimpleSpiceUpdate
*update
;
390 dprint(3, "%s:\n", __FUNCTION__
);
392 qemu_mutex_lock(&ssd
->lock
);
393 if (ssd
->update
!= NULL
) {
394 update
= ssd
->update
;
399 qemu_mutex_unlock(&ssd
->lock
);
404 static int interface_req_cmd_notification(QXLInstance
*sin
)
406 dprint(1, "%s:\n", __FUNCTION__
);
410 static void interface_release_resource(QXLInstance
*sin
,
411 struct QXLReleaseInfoExt ext
)
413 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
416 dprint(2, "%s:\n", __FUNCTION__
);
418 qemu_spice_destroy_update(ssd
, (void*)id
);
421 static int interface_get_cursor_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
423 dprint(3, "%s:\n", __FUNCTION__
);
427 static int interface_req_cursor_notification(QXLInstance
*sin
)
429 dprint(1, "%s:\n", __FUNCTION__
);
433 static void interface_notify_update(QXLInstance
*sin
, uint32_t update_id
)
435 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
439 static int interface_flush_resources(QXLInstance
*sin
)
441 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
446 static const QXLInterface dpy_interface
= {
447 .base
.type
= SPICE_INTERFACE_QXL
,
448 .base
.description
= "qemu simple display",
449 .base
.major_version
= SPICE_INTERFACE_QXL_MAJOR
,
450 .base
.minor_version
= SPICE_INTERFACE_QXL_MINOR
,
452 .attache_worker
= interface_attach_worker
,
453 .set_compression_level
= interface_set_compression_level
,
454 .set_mm_time
= interface_set_mm_time
,
455 .get_init_info
= interface_get_init_info
,
457 /* the callbacks below are called from spice server thread context */
458 .get_command
= interface_get_command
,
459 .req_cmd_notification
= interface_req_cmd_notification
,
460 .release_resource
= interface_release_resource
,
461 .get_cursor_command
= interface_get_cursor_command
,
462 .req_cursor_notification
= interface_req_cursor_notification
,
463 .notify_update
= interface_notify_update
,
464 .flush_resources
= interface_flush_resources
,
467 static SimpleSpiceDisplay sdpy
;
469 static void display_update(struct DisplayState
*ds
, int x
, int y
, int w
, int h
)
471 qemu_spice_display_update(&sdpy
, x
, y
, w
, h
);
474 static void display_resize(struct DisplayState
*ds
)
476 qemu_spice_display_resize(&sdpy
);
479 static void display_refresh(struct DisplayState
*ds
)
481 qemu_spice_display_refresh(&sdpy
);
484 static DisplayChangeListener display_listener
= {
485 .dpy_update
= display_update
,
486 .dpy_resize
= display_resize
,
487 .dpy_refresh
= display_refresh
,
490 void qemu_spice_display_init(DisplayState
*ds
)
492 assert(sdpy
.ds
== NULL
);
493 qemu_spice_display_init_common(&sdpy
, ds
);
494 register_displaychangelistener(ds
, &display_listener
);
496 sdpy
.qxl
.base
.sif
= &dpy_interface
.base
;
497 qemu_spice_add_interface(&sdpy
.qxl
.base
);
500 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler
, &sdpy
);
501 qemu_spice_create_host_memslot(&sdpy
);
502 qemu_spice_create_host_primary(&sdpy
);