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 spice_qxl_add_memslot_async(&ssd
->qxl
, memslot
, 0);
69 ssd
->worker
->add_memslot(ssd
->worker
, memslot
);
73 void qemu_spice_del_memslot(SimpleSpiceDisplay
*ssd
, uint32_t gid
, uint32_t sid
)
75 ssd
->worker
->del_memslot(ssd
->worker
, gid
, sid
);
78 void qemu_spice_create_primary_surface(SimpleSpiceDisplay
*ssd
, uint32_t id
,
79 QXLDevSurfaceCreate
*surface
,
82 if (async
!= QXL_SYNC
) {
83 spice_qxl_create_primary_surface_async(&ssd
->qxl
, id
, surface
, 0);
85 ssd
->worker
->create_primary_surface(ssd
->worker
, id
, surface
);
90 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay
*ssd
,
91 uint32_t id
, qxl_async_io async
)
93 if (async
!= QXL_SYNC
) {
94 spice_qxl_destroy_primary_surface_async(&ssd
->qxl
, id
, 0);
96 ssd
->worker
->destroy_primary_surface(ssd
->worker
, id
);
100 void qemu_spice_wakeup(SimpleSpiceDisplay
*ssd
)
102 ssd
->worker
->wakeup(ssd
->worker
);
105 void qemu_spice_start(SimpleSpiceDisplay
*ssd
)
107 ssd
->worker
->start(ssd
->worker
);
110 void qemu_spice_stop(SimpleSpiceDisplay
*ssd
)
112 ssd
->worker
->stop(ssd
->worker
);
115 static SimpleSpiceUpdate
*qemu_spice_create_update(SimpleSpiceDisplay
*ssd
)
117 SimpleSpiceUpdate
*update
;
118 QXLDrawable
*drawable
;
123 struct timespec time_space
;
125 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
129 dprint(2, "%s: lr %d -> %d, tb -> %d -> %d\n", __FUNCTION__
,
130 ssd
->dirty
.left
, ssd
->dirty
.right
,
131 ssd
->dirty
.top
, ssd
->dirty
.bottom
);
133 update
= g_malloc0(sizeof(*update
));
134 drawable
= &update
->drawable
;
135 image
= &update
->image
;
136 cmd
= &update
->ext
.cmd
;
138 bw
= ssd
->dirty
.right
- ssd
->dirty
.left
;
139 bh
= ssd
->dirty
.bottom
- ssd
->dirty
.top
;
140 update
->bitmap
= g_malloc(bw
* bh
* 4);
142 drawable
->bbox
= ssd
->dirty
;
143 drawable
->clip
.type
= SPICE_CLIP_TYPE_NONE
;
144 drawable
->effect
= QXL_EFFECT_OPAQUE
;
145 drawable
->release_info
.id
= (intptr_t)update
;
146 drawable
->type
= QXL_DRAW_COPY
;
147 drawable
->surfaces_dest
[0] = -1;
148 drawable
->surfaces_dest
[1] = -1;
149 drawable
->surfaces_dest
[2] = -1;
150 clock_gettime(CLOCK_MONOTONIC
, &time_space
);
151 /* time in milliseconds from epoch. */
152 drawable
->mm_time
= time_space
.tv_sec
* 1000
153 + time_space
.tv_nsec
/ 1000 / 1000;
155 drawable
->u
.copy
.rop_descriptor
= SPICE_ROPD_OP_PUT
;
156 drawable
->u
.copy
.src_bitmap
= (intptr_t)image
;
157 drawable
->u
.copy
.src_area
.right
= bw
;
158 drawable
->u
.copy
.src_area
.bottom
= bh
;
160 QXL_SET_IMAGE_ID(image
, QXL_IMAGE_GROUP_DEVICE
, ssd
->unique
++);
161 image
->descriptor
.type
= SPICE_IMAGE_TYPE_BITMAP
;
162 image
->bitmap
.flags
= QXL_BITMAP_DIRECT
| QXL_BITMAP_TOP_DOWN
;
163 image
->bitmap
.stride
= bw
* 4;
164 image
->descriptor
.width
= image
->bitmap
.x
= bw
;
165 image
->descriptor
.height
= image
->bitmap
.y
= bh
;
166 image
->bitmap
.data
= (intptr_t)(update
->bitmap
);
167 image
->bitmap
.palette
= 0;
168 image
->bitmap
.format
= SPICE_BITMAP_FMT_32BIT
;
170 if (ssd
->conv
== NULL
) {
171 PixelFormat dst
= qemu_default_pixelformat(32);
172 ssd
->conv
= qemu_pf_conv_get(&dst
, &ssd
->ds
->surface
->pf
);
176 src
= ds_get_data(ssd
->ds
) +
177 ssd
->dirty
.top
* ds_get_linesize(ssd
->ds
) +
178 ssd
->dirty
.left
* ds_get_bytes_per_pixel(ssd
->ds
);
179 dst
= update
->bitmap
;
180 for (by
= 0; by
< bh
; by
++) {
181 qemu_pf_conv_run(ssd
->conv
, dst
, src
, bw
);
182 src
+= ds_get_linesize(ssd
->ds
);
183 dst
+= image
->bitmap
.stride
;
186 cmd
->type
= QXL_CMD_DRAW
;
187 cmd
->data
= (intptr_t)drawable
;
189 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
194 * Called from spice server thread context (via interface_release_ressource)
195 * We do *not* hold the global qemu mutex here, so extra care is needed
196 * when calling qemu functions. Qemu interfaces used:
197 * - g_free (underlying glibc free is re-entrant).
199 void qemu_spice_destroy_update(SimpleSpiceDisplay
*sdpy
, SimpleSpiceUpdate
*update
)
201 g_free(update
->bitmap
);
205 void qemu_spice_create_host_memslot(SimpleSpiceDisplay
*ssd
)
207 QXLDevMemSlot memslot
;
209 dprint(1, "%s:\n", __FUNCTION__
);
211 memset(&memslot
, 0, sizeof(memslot
));
212 memslot
.slot_group_id
= MEMSLOT_GROUP_HOST
;
213 memslot
.virt_end
= ~0;
214 qemu_spice_add_memslot(ssd
, &memslot
, QXL_SYNC
);
217 void qemu_spice_create_host_primary(SimpleSpiceDisplay
*ssd
)
219 QXLDevSurfaceCreate surface
;
221 dprint(1, "%s: %dx%d\n", __FUNCTION__
,
222 ds_get_width(ssd
->ds
), ds_get_height(ssd
->ds
));
224 surface
.format
= SPICE_SURFACE_FMT_32_xRGB
;
225 surface
.width
= ds_get_width(ssd
->ds
);
226 surface
.height
= ds_get_height(ssd
->ds
);
227 surface
.stride
= -surface
.width
* 4;
228 surface
.mouse_mode
= true;
231 surface
.mem
= (intptr_t)ssd
->buf
;
232 surface
.group_id
= MEMSLOT_GROUP_HOST
;
234 qemu_spice_create_primary_surface(ssd
, 0, &surface
, QXL_SYNC
);
237 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay
*ssd
)
239 dprint(1, "%s:\n", __FUNCTION__
);
241 qemu_spice_destroy_primary_surface(ssd
, 0, QXL_SYNC
);
244 void qemu_spice_vm_change_state_handler(void *opaque
, int running
,
247 SimpleSpiceDisplay
*ssd
= opaque
;
251 qemu_spice_start(ssd
);
253 qemu_spice_stop(ssd
);
254 ssd
->running
= false;
258 void qemu_spice_display_init_common(SimpleSpiceDisplay
*ssd
, DisplayState
*ds
)
261 qemu_mutex_init(&ssd
->lock
);
264 ssd
->bufsize
= (16 * 1024 * 1024);
265 ssd
->buf
= g_malloc(ssd
->bufsize
);
268 /* display listener callbacks */
270 void qemu_spice_display_update(SimpleSpiceDisplay
*ssd
,
271 int x
, int y
, int w
, int h
)
275 dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__
, x
, y
, w
, h
);
276 update_area
.left
= x
,
277 update_area
.right
= x
+ w
;
279 update_area
.bottom
= y
+ h
;
281 if (qemu_spice_rect_is_empty(&ssd
->dirty
)) {
284 qemu_spice_rect_union(&ssd
->dirty
, &update_area
);
287 void qemu_spice_display_resize(SimpleSpiceDisplay
*ssd
)
289 dprint(1, "%s:\n", __FUNCTION__
);
291 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
292 qemu_pf_conv_put(ssd
->conv
);
295 qemu_mutex_lock(&ssd
->lock
);
296 if (ssd
->update
!= NULL
) {
297 qemu_spice_destroy_update(ssd
, ssd
->update
);
300 qemu_mutex_unlock(&ssd
->lock
);
301 qemu_spice_destroy_host_primary(ssd
);
302 qemu_spice_create_host_primary(ssd
);
304 memset(&ssd
->dirty
, 0, sizeof(ssd
->dirty
));
308 void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay
*ssd
)
311 ssd
->ds
->cursor_define(ssd
->cursor
);
312 cursor_put(ssd
->cursor
);
315 if (ssd
->mouse_x
!= -1 && ssd
->mouse_y
!= -1) {
316 ssd
->ds
->mouse_set(ssd
->mouse_x
, ssd
->mouse_y
, 1);
322 void qemu_spice_display_refresh(SimpleSpiceDisplay
*ssd
)
324 dprint(3, "%s:\n", __func__
);
327 qemu_mutex_lock(&ssd
->lock
);
328 if (ssd
->update
== NULL
) {
329 ssd
->update
= qemu_spice_create_update(ssd
);
332 qemu_spice_cursor_refresh_unlocked(ssd
);
333 qemu_mutex_unlock(&ssd
->lock
);
337 qemu_spice_wakeup(ssd
);
338 dprint(2, "%s: notify\n", __FUNCTION__
);
342 /* spice display interface callbacks */
344 static void interface_attach_worker(QXLInstance
*sin
, QXLWorker
*qxl_worker
)
346 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
348 dprint(1, "%s:\n", __FUNCTION__
);
349 ssd
->worker
= qxl_worker
;
352 static void interface_set_compression_level(QXLInstance
*sin
, int level
)
354 dprint(1, "%s:\n", __FUNCTION__
);
358 static void interface_set_mm_time(QXLInstance
*sin
, uint32_t mm_time
)
360 dprint(3, "%s:\n", __FUNCTION__
);
364 static void interface_get_init_info(QXLInstance
*sin
, QXLDevInitInfo
*info
)
366 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
368 info
->memslot_gen_bits
= MEMSLOT_GENERATION_BITS
;
369 info
->memslot_id_bits
= MEMSLOT_SLOT_BITS
;
370 info
->num_memslots
= NUM_MEMSLOTS
;
371 info
->num_memslots_groups
= NUM_MEMSLOTS_GROUPS
;
372 info
->internal_groupslot_id
= 0;
373 info
->qxl_ram_size
= ssd
->bufsize
;
374 info
->n_surfaces
= NUM_SURFACES
;
377 static int interface_get_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
379 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
380 SimpleSpiceUpdate
*update
;
383 dprint(3, "%s:\n", __FUNCTION__
);
385 qemu_mutex_lock(&ssd
->lock
);
386 if (ssd
->update
!= NULL
) {
387 update
= ssd
->update
;
392 qemu_mutex_unlock(&ssd
->lock
);
397 static int interface_req_cmd_notification(QXLInstance
*sin
)
399 dprint(1, "%s:\n", __FUNCTION__
);
403 static void interface_release_resource(QXLInstance
*sin
,
404 struct QXLReleaseInfoExt ext
)
406 SimpleSpiceDisplay
*ssd
= container_of(sin
, SimpleSpiceDisplay
, qxl
);
409 dprint(2, "%s:\n", __FUNCTION__
);
411 qemu_spice_destroy_update(ssd
, (void*)id
);
414 static int interface_get_cursor_command(QXLInstance
*sin
, struct QXLCommandExt
*ext
)
416 dprint(3, "%s:\n", __FUNCTION__
);
420 static int interface_req_cursor_notification(QXLInstance
*sin
)
422 dprint(1, "%s:\n", __FUNCTION__
);
426 static void interface_notify_update(QXLInstance
*sin
, uint32_t update_id
)
428 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
432 static int interface_flush_resources(QXLInstance
*sin
)
434 fprintf(stderr
, "%s: abort()\n", __FUNCTION__
);
439 static const QXLInterface dpy_interface
= {
440 .base
.type
= SPICE_INTERFACE_QXL
,
441 .base
.description
= "qemu simple display",
442 .base
.major_version
= SPICE_INTERFACE_QXL_MAJOR
,
443 .base
.minor_version
= SPICE_INTERFACE_QXL_MINOR
,
445 .attache_worker
= interface_attach_worker
,
446 .set_compression_level
= interface_set_compression_level
,
447 .set_mm_time
= interface_set_mm_time
,
448 .get_init_info
= interface_get_init_info
,
450 /* the callbacks below are called from spice server thread context */
451 .get_command
= interface_get_command
,
452 .req_cmd_notification
= interface_req_cmd_notification
,
453 .release_resource
= interface_release_resource
,
454 .get_cursor_command
= interface_get_cursor_command
,
455 .req_cursor_notification
= interface_req_cursor_notification
,
456 .notify_update
= interface_notify_update
,
457 .flush_resources
= interface_flush_resources
,
460 static SimpleSpiceDisplay sdpy
;
462 static void display_update(struct DisplayState
*ds
, int x
, int y
, int w
, int h
)
464 qemu_spice_display_update(&sdpy
, x
, y
, w
, h
);
467 static void display_resize(struct DisplayState
*ds
)
469 qemu_spice_display_resize(&sdpy
);
472 static void display_refresh(struct DisplayState
*ds
)
474 qemu_spice_display_refresh(&sdpy
);
477 static DisplayChangeListener display_listener
= {
478 .dpy_update
= display_update
,
479 .dpy_resize
= display_resize
,
480 .dpy_refresh
= display_refresh
,
483 void qemu_spice_display_init(DisplayState
*ds
)
485 assert(sdpy
.ds
== NULL
);
486 qemu_spice_display_init_common(&sdpy
, ds
);
487 register_displaychangelistener(ds
, &display_listener
);
489 sdpy
.qxl
.base
.sif
= &dpy_interface
.base
;
490 qemu_spice_add_interface(&sdpy
.qxl
.base
);
493 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler
, &sdpy
);
494 qemu_spice_create_host_memslot(&sdpy
);
495 qemu_spice_create_host_primary(&sdpy
);