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/>.
20 #include "qemu-common.h"
21 #include "qemu-spice.h"
22 #include "qemu-timer.h"
23 #include "qemu-queue.h"
28 #include "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);
66 void qemu_spice_update_area(SimpleSpiceDisplay *ssd, uint32_t surface_id,
67 struct QXLRect *area, struct QXLRect *dirty_rects,
68 uint32_t num_dirty_rects,
69 uint32_t clear_dirty_region)
71 ssd->worker->update_area(ssd->worker, surface_id, area, dirty_rects,
72 num_dirty_rects, clear_dirty_region);
75 void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot)
77 ssd->worker->add_memslot(ssd->worker, memslot);
80 void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
82 ssd->worker->del_memslot(ssd->worker, gid, sid);
85 void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
86 QXLDevSurfaceCreate *surface)
88 ssd->worker->create_primary_surface(ssd->worker, id, surface);
91 void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id)
93 ssd->worker->destroy_primary_surface(ssd->worker, id);
96 void qemu_spice_destroy_surface_wait(SimpleSpiceDisplay *ssd, uint32_t id)
98 ssd->worker->destroy_surface_wait(ssd->worker, id);
101 void qemu_spice_loadvm_commands(SimpleSpiceDisplay *ssd,
102 struct QXLCommandExt *ext, uint32_t count)
104 ssd->worker->loadvm_commands(ssd->worker, ext, count);
107 void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
109 ssd->worker->wakeup(ssd->worker);
112 void qemu_spice_oom(SimpleSpiceDisplay *ssd)
114 ssd->worker->oom(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 void qemu_spice_reset_memslots(SimpleSpiceDisplay *ssd)
129 ssd->worker->reset_memslots(ssd->worker);
132 void qemu_spice_destroy_surfaces(SimpleSpiceDisplay *ssd)
134 ssd->worker->destroy_surfaces(ssd->worker);
137 void qemu_spice_reset_image_cache(SimpleSpiceDisplay *ssd)
139 ssd->worker->reset_image_cache(ssd->worker);
142 void qemu_spice_reset_cursor(SimpleSpiceDisplay *ssd)
144 ssd->worker->reset_cursor(ssd->worker);
148 static SimpleSpiceUpdate *qemu_spice_create_update(SimpleSpiceDisplay *ssd)
150 SimpleSpiceUpdate *update;
151 QXLDrawable *drawable;
156 struct timespec time_space;
158 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
162 dprint(2, "%s: lr %d -> %d, tb -> %d -> %d\n", __FUNCTION__,
163 ssd->dirty.left, ssd->dirty.right,
164 ssd->dirty.top, ssd->dirty.bottom);
166 update = qemu_mallocz(sizeof(*update));
167 drawable = &update->drawable;
168 image = &update->image;
169 cmd = &update->ext.cmd;
171 bw = ssd->dirty.right - ssd->dirty.left;
172 bh = ssd->dirty.bottom - ssd->dirty.top;
173 update->bitmap = qemu_malloc(bw * bh * 4);
175 drawable->bbox = ssd->dirty;
176 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
177 drawable->effect = QXL_EFFECT_OPAQUE;
178 drawable->release_info.id = (intptr_t)update;
179 drawable->type = QXL_DRAW_COPY;
180 drawable->surfaces_dest[0] = -1;
181 drawable->surfaces_dest[1] = -1;
182 drawable->surfaces_dest[2] = -1;
183 clock_gettime(CLOCK_MONOTONIC, &time_space);
184 /* time in milliseconds from epoch. */
185 drawable->mm_time = time_space.tv_sec * 1000
186 + time_space.tv_nsec / 1000 / 1000;
188 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
189 drawable->u.copy.src_bitmap = (intptr_t)image;
190 drawable->u.copy.src_area.right = bw;
191 drawable->u.copy.src_area.bottom = bh;
193 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
194 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
195 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
196 image->bitmap.stride = bw * 4;
197 image->descriptor.width = image->bitmap.x = bw;
198 image->descriptor.height = image->bitmap.y = bh;
199 image->bitmap.data = (intptr_t)(update->bitmap);
200 image->bitmap.palette = 0;
201 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
203 if (ssd->conv == NULL) {
204 PixelFormat dst = qemu_default_pixelformat(32);
205 ssd->conv = qemu_pf_conv_get(&dst, &ssd->ds->surface->pf);
209 src = ds_get_data(ssd->ds) +
210 ssd->dirty.top * ds_get_linesize(ssd->ds) +
211 ssd->dirty.left * ds_get_bytes_per_pixel(ssd->ds);
212 dst = update->bitmap;
213 for (by = 0; by < bh; by++) {
214 qemu_pf_conv_run(ssd->conv, dst, src, bw);
215 src += ds_get_linesize(ssd->ds);
216 dst += image->bitmap.stride;
219 cmd->type = QXL_CMD_DRAW;
220 cmd->data = (intptr_t)drawable;
222 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
227 * Called from spice server thread context (via interface_release_ressource)
228 * We do *not* hold the global qemu mutex here, so extra care is needed
229 * when calling qemu functions. Qemu interfaces used:
230 * - qemu_free (underlying glibc free is re-entrant).
232 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
234 qemu_free(update->bitmap);
238 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
240 QXLDevMemSlot memslot;
242 dprint(1, "%s:\n", __FUNCTION__);
244 memset(&memslot, 0, sizeof(memslot));
245 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
246 memslot.virt_end = ~0;
247 qemu_spice_add_memslot(ssd, &memslot);
250 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
252 QXLDevSurfaceCreate surface;
254 dprint(1, "%s: %dx%d\n", __FUNCTION__,
255 ds_get_width(ssd->ds), ds_get_height(ssd->ds));
257 surface.format = SPICE_SURFACE_FMT_32_xRGB;
258 surface.width = ds_get_width(ssd->ds);
259 surface.height = ds_get_height(ssd->ds);
260 surface.stride = -surface.width * 4;
261 surface.mouse_mode = true;
264 surface.mem = (intptr_t)ssd->buf;
265 surface.group_id = MEMSLOT_GROUP_HOST;
267 qemu_spice_create_primary_surface(ssd, 0, &surface);
270 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
272 dprint(1, "%s:\n", __FUNCTION__);
274 qemu_spice_destroy_primary_surface(ssd, 0);
277 void qemu_spice_vm_change_state_handler(void *opaque, int running, int reason)
279 SimpleSpiceDisplay *ssd = opaque;
282 qemu_spice_start(ssd);
284 qemu_spice_stop(ssd);
286 ssd->running = running;
289 /* display listener callbacks */
291 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
292 int x, int y, int w, int h)
296 dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__, x, y, w, h);
297 update_area.left = x,
298 update_area.right = x + w;
300 update_area.bottom = y + h;
302 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
305 qemu_spice_rect_union(&ssd->dirty, &update_area);
308 void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
310 dprint(1, "%s:\n", __FUNCTION__);
312 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
313 qemu_pf_conv_put(ssd->conv);
316 qemu_mutex_lock(&ssd->lock);
317 if (ssd->update != NULL) {
318 qemu_spice_destroy_update(ssd, ssd->update);
321 qemu_mutex_unlock(&ssd->lock);
322 qemu_spice_destroy_host_primary(ssd);
323 qemu_spice_create_host_primary(ssd);
325 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
329 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
331 dprint(3, "%s:\n", __FUNCTION__);
334 qemu_mutex_lock(&ssd->lock);
335 if (ssd->update == NULL) {
336 ssd->update = qemu_spice_create_update(ssd);
340 ssd->ds->cursor_define(ssd->cursor);
341 cursor_put(ssd->cursor);
344 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
345 ssd->ds->mouse_set(ssd->mouse_x, ssd->mouse_y, 1);
349 qemu_mutex_unlock(&ssd->lock);
353 qemu_spice_wakeup(ssd);
354 dprint(2, "%s: notify\n", __FUNCTION__);
358 /* spice display interface callbacks */
360 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
362 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
364 dprint(1, "%s:\n", __FUNCTION__);
365 ssd->worker = qxl_worker;
368 static void interface_set_compression_level(QXLInstance *sin, int level)
370 dprint(1, "%s:\n", __FUNCTION__);
374 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
376 dprint(3, "%s:\n", __FUNCTION__);
380 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
382 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
384 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
385 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
386 info->num_memslots = NUM_MEMSLOTS;
387 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
388 info->internal_groupslot_id = 0;
389 info->qxl_ram_size = ssd->bufsize;
390 info->n_surfaces = NUM_SURFACES;
393 static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
395 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
396 SimpleSpiceUpdate *update;
399 dprint(3, "%s:\n", __FUNCTION__);
401 qemu_mutex_lock(&ssd->lock);
402 if (ssd->update != NULL) {
403 update = ssd->update;
408 qemu_mutex_unlock(&ssd->lock);
413 static int interface_req_cmd_notification(QXLInstance *sin)
415 dprint(1, "%s:\n", __FUNCTION__);
419 static void interface_release_resource(QXLInstance *sin,
420 struct QXLReleaseInfoExt ext)
422 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
425 dprint(2, "%s:\n", __FUNCTION__);
427 qemu_spice_destroy_update(ssd, (void*)id);
430 static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
432 dprint(3, "%s:\n", __FUNCTION__);
436 static int interface_req_cursor_notification(QXLInstance *sin)
438 dprint(1, "%s:\n", __FUNCTION__);
442 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
444 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
448 static int interface_flush_resources(QXLInstance *sin)
450 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
455 static const QXLInterface dpy_interface = {
456 .base.type = SPICE_INTERFACE_QXL,
457 .base.description = "qemu simple display",
458 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
459 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
461 .attache_worker = interface_attach_worker,
462 .set_compression_level = interface_set_compression_level,
463 .set_mm_time = interface_set_mm_time,
464 .get_init_info = interface_get_init_info,
466 /* the callbacks below are called from spice server thread context */
467 .get_command = interface_get_command,
468 .req_cmd_notification = interface_req_cmd_notification,
469 .release_resource = interface_release_resource,
470 .get_cursor_command = interface_get_cursor_command,
471 .req_cursor_notification = interface_req_cursor_notification,
472 .notify_update = interface_notify_update,
473 .flush_resources = interface_flush_resources,
476 static SimpleSpiceDisplay sdpy;
478 static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
480 qemu_spice_display_update(&sdpy, x, y, w, h);
483 static void display_resize(struct DisplayState *ds)
485 qemu_spice_display_resize(&sdpy);
488 static void display_refresh(struct DisplayState *ds)
490 qemu_spice_display_refresh(&sdpy);
493 static DisplayChangeListener display_listener = {
494 .dpy_update = display_update,
495 .dpy_resize = display_resize,
496 .dpy_refresh = display_refresh,
499 void qemu_spice_display_init(DisplayState *ds)
501 assert(sdpy.ds == NULL);
503 qemu_mutex_init(&sdpy.lock);
506 sdpy.bufsize = (16 * 1024 * 1024);
507 sdpy.buf = qemu_malloc(sdpy.bufsize);
508 register_displaychangelistener(ds, &display_listener);
510 sdpy.qxl.base.sif = &dpy_interface.base;
511 qemu_spice_add_interface(&sdpy.qxl.base);
514 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler, &sdpy);
515 qemu_spice_create_host_memslot(&sdpy);
516 qemu_spice_create_host_primary(&sdpy);