]> git.proxmox.com Git - mirror_qemu.git/blame - ui/spice-display.c
spice: reduce refresh rate in native mode
[mirror_qemu.git] / ui / spice-display.c
CommitLineData
a3e22260
GH
1/*
2 * Copyright (C) 2010 Red Hat, Inc.
3 *
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.
8 *
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.
13 *
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/>.
16 */
17
a3e22260 18#include "qemu-common.h"
28ecbaee 19#include "ui/qemu-spice.h"
1de7afc9
PB
20#include "qemu/timer.h"
21#include "qemu/queue.h"
83c9089e 22#include "monitor/monitor.h"
28ecbaee 23#include "ui/console.h"
9c17d615 24#include "sysemu/sysemu.h"
c480bb7d 25#include "trace.h"
a3e22260 26
28ecbaee 27#include "ui/spice-display.h"
a3e22260
GH
28
29static int debug = 0;
30
2c80e423 31static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
a3e22260
GH
32{
33 va_list args;
34
35 if (level <= debug) {
36 va_start(args, fmt);
37 vfprintf(stderr, fmt, args);
38 va_end(args);
39 }
40}
41
42int qemu_spice_rect_is_empty(const QXLRect* r)
43{
44 return r->top == r->bottom || r->left == r->right;
45}
46
47void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
48{
49 if (qemu_spice_rect_is_empty(r)) {
50 return;
51 }
52
53 if (qemu_spice_rect_is_empty(dest)) {
54 *dest = *r;
55 return;
56 }
57
58 dest->top = MIN(dest->top, r->top);
59 dest->left = MIN(dest->left, r->left);
60 dest->bottom = MAX(dest->bottom, r->bottom);
61 dest->right = MAX(dest->right, r->right);
62}
63
2e1a98c9
AL
64QXLCookie *qxl_cookie_new(int type, uint64_t io)
65{
66 QXLCookie *cookie;
67
68 cookie = g_malloc0(sizeof(*cookie));
69 cookie->type = type;
70 cookie->io = io;
71 return cookie;
72}
73
5ff4e36c
AL
74void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
75 qxl_async_io async)
5c59d118 76{
c480bb7d
AL
77 trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
78 memslot->virt_start, memslot->virt_end,
79 async);
80
5ff4e36c 81 if (async != QXL_SYNC) {
2e1a98c9 82 spice_qxl_add_memslot_async(&ssd->qxl, memslot,
34d14c6d
PM
83 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
84 QXL_IO_MEMSLOT_ADD_ASYNC));
5ff4e36c 85 } else {
26defe81 86 spice_qxl_add_memslot(&ssd->qxl, memslot);
5ff4e36c 87 }
5c59d118
GH
88}
89
90void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
91{
c480bb7d 92 trace_qemu_spice_del_memslot(ssd->qxl.id, gid, sid);
26defe81 93 spice_qxl_del_memslot(&ssd->qxl, gid, sid);
5c59d118
GH
94}
95
96void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
5ff4e36c
AL
97 QXLDevSurfaceCreate *surface,
98 qxl_async_io async)
5c59d118 99{
c480bb7d 100 trace_qemu_spice_create_primary_surface(ssd->qxl.id, id, surface, async);
5ff4e36c 101 if (async != QXL_SYNC) {
2e1a98c9 102 spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface,
34d14c6d
PM
103 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
104 QXL_IO_CREATE_PRIMARY_ASYNC));
5ff4e36c 105 } else {
26defe81 106 spice_qxl_create_primary_surface(&ssd->qxl, id, surface);
5ff4e36c 107 }
5c59d118
GH
108}
109
5ff4e36c
AL
110void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
111 uint32_t id, qxl_async_io async)
5c59d118 112{
c480bb7d 113 trace_qemu_spice_destroy_primary_surface(ssd->qxl.id, id, async);
5ff4e36c 114 if (async != QXL_SYNC) {
2e1a98c9 115 spice_qxl_destroy_primary_surface_async(&ssd->qxl, id,
34d14c6d
PM
116 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
117 QXL_IO_DESTROY_PRIMARY_ASYNC));
5ff4e36c 118 } else {
26defe81 119 spice_qxl_destroy_primary_surface(&ssd->qxl, id);
5ff4e36c 120 }
5c59d118
GH
121}
122
5c59d118
GH
123void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
124{
c480bb7d 125 trace_qemu_spice_wakeup(ssd->qxl.id);
26defe81 126 spice_qxl_wakeup(&ssd->qxl);
5c59d118
GH
127}
128
c60319a3
GH
129static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
130 QXLRect *rect)
a3e22260
GH
131{
132 SimpleSpiceUpdate *update;
133 QXLDrawable *drawable;
134 QXLImage *image;
135 QXLCommand *cmd;
d9a86569 136 int bw, bh;
22795174 137 struct timespec time_space;
d9a86569 138 pixman_image_t *dest;
a3e22260 139
c480bb7d 140 trace_qemu_spice_create_update(
c60319a3
GH
141 rect->left, rect->right,
142 rect->top, rect->bottom);
a3e22260 143
7267c094 144 update = g_malloc0(sizeof(*update));
a3e22260
GH
145 drawable = &update->drawable;
146 image = &update->image;
147 cmd = &update->ext.cmd;
148
c60319a3
GH
149 bw = rect->right - rect->left;
150 bh = rect->bottom - rect->top;
7267c094 151 update->bitmap = g_malloc(bw * bh * 4);
a3e22260 152
c60319a3 153 drawable->bbox = *rect;
a3e22260
GH
154 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
155 drawable->effect = QXL_EFFECT_OPAQUE;
5643fc01 156 drawable->release_info.id = (uintptr_t)(&update->ext);
a3e22260
GH
157 drawable->type = QXL_DRAW_COPY;
158 drawable->surfaces_dest[0] = -1;
159 drawable->surfaces_dest[1] = -1;
160 drawable->surfaces_dest[2] = -1;
22795174
AL
161 clock_gettime(CLOCK_MONOTONIC, &time_space);
162 /* time in milliseconds from epoch. */
163 drawable->mm_time = time_space.tv_sec * 1000
164 + time_space.tv_nsec / 1000 / 1000;
a3e22260
GH
165
166 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
a13ccc99 167 drawable->u.copy.src_bitmap = (uintptr_t)image;
a3e22260
GH
168 drawable->u.copy.src_area.right = bw;
169 drawable->u.copy.src_area.bottom = bh;
170
171 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
172 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
173 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
174 image->bitmap.stride = bw * 4;
175 image->descriptor.width = image->bitmap.x = bw;
176 image->descriptor.height = image->bitmap.y = bh;
a13ccc99 177 image->bitmap.data = (uintptr_t)(update->bitmap);
a3e22260
GH
178 image->bitmap.palette = 0;
179 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
180
d9a86569
GH
181 dest = pixman_image_create_bits(PIXMAN_x8r8g8b8, bw, bh,
182 (void *)update->bitmap, bw * 4);
183 pixman_image_composite(PIXMAN_OP_SRC, ssd->surface, NULL, ssd->mirror,
184 rect->left, rect->top, 0, 0,
185 rect->left, rect->top, bw, bh);
186 pixman_image_composite(PIXMAN_OP_SRC, ssd->mirror, NULL, dest,
187 rect->left, rect->top, 0, 0,
188 0, 0, bw, bh);
189 pixman_image_unref(dest);
a3e22260
GH
190
191 cmd->type = QXL_CMD_DRAW;
a13ccc99 192 cmd->data = (uintptr_t)drawable;
a3e22260 193
b1af98ba 194 QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
a3e22260
GH
195}
196
c60319a3
GH
197static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
198{
b021bd29 199 static const int blksize = 32;
71874c17 200 int blocks = (surface_width(ssd->ds) + blksize - 1) / blksize;
b021bd29
GH
201 int dirty_top[blocks];
202 int y, yoff, x, xoff, blk, bw;
71874c17 203 int bpp = surface_bytes_per_pixel(ssd->ds);
b021bd29
GH
204 uint8_t *guest, *mirror;
205
c60319a3
GH
206 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
207 return;
208 };
a7310dd3 209
d9a86569 210 if (ssd->surface == NULL) {
71874c17
GH
211 ssd->surface = pixman_image_ref(ssd->ds->image);
212 ssd->mirror = qemu_pixman_mirror_create(ssd->ds->format,
213 ssd->ds->image);
a7310dd3
GH
214 }
215
b021bd29
GH
216 for (blk = 0; blk < blocks; blk++) {
217 dirty_top[blk] = -1;
218 }
219
71874c17 220 guest = surface_data(ssd->ds);
d9a86569 221 mirror = (void *)pixman_image_get_data(ssd->mirror);
b021bd29 222 for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
71874c17 223 yoff = y * surface_stride(ssd->ds);
b021bd29
GH
224 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
225 xoff = x * bpp;
226 blk = x / blksize;
227 bw = MIN(blksize, ssd->dirty.right - x);
228 if (memcmp(guest + yoff + xoff,
229 mirror + yoff + xoff,
230 bw * bpp) == 0) {
231 if (dirty_top[blk] != -1) {
232 QXLRect update = {
233 .top = dirty_top[blk],
234 .bottom = y,
235 .left = x,
236 .right = x + bw,
237 };
238 qemu_spice_create_one_update(ssd, &update);
239 dirty_top[blk] = -1;
240 }
241 } else {
242 if (dirty_top[blk] == -1) {
243 dirty_top[blk] = y;
244 }
245 }
246 }
247 }
248
249 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
250 blk = x / blksize;
251 bw = MIN(blksize, ssd->dirty.right - x);
252 if (dirty_top[blk] != -1) {
253 QXLRect update = {
254 .top = dirty_top[blk],
255 .bottom = ssd->dirty.bottom,
256 .left = x,
257 .right = x + bw,
258 };
259 qemu_spice_create_one_update(ssd, &update);
260 dirty_top[blk] = -1;
261 }
262 }
263
c60319a3
GH
264 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
265}
266
5643fc01
GH
267static SimpleSpiceCursor*
268qemu_spice_create_cursor_update(SimpleSpiceDisplay *ssd,
269 QEMUCursor *c)
270{
271 size_t size = c ? c->width * c->height * 4 : 0;
272 SimpleSpiceCursor *update;
273 QXLCursorCmd *ccmd;
274 QXLCursor *cursor;
275 QXLCommand *cmd;
276
277 update = g_malloc0(sizeof(*update) + size);
278 ccmd = &update->cmd;
279 cursor = &update->cursor;
280 cmd = &update->ext.cmd;
281
282 if (c) {
283 ccmd->type = QXL_CURSOR_SET;
284 ccmd->u.set.position.x = ssd->ptr_x;
285 ccmd->u.set.position.y = ssd->ptr_y;
286 ccmd->u.set.visible = true;
287 ccmd->u.set.shape = (uintptr_t)cursor;
288 cursor->header.unique = ssd->unique++;
289 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
290 cursor->header.width = c->width;
291 cursor->header.height = c->height;
292 cursor->header.hot_spot_x = c->hot_x;
293 cursor->header.hot_spot_y = c->hot_y;
294 cursor->data_size = size;
295 cursor->chunk.data_size = size;
296 memcpy(cursor->chunk.data, c->data, size);
297 } else {
298 ccmd->type = QXL_CURSOR_MOVE;
299 ccmd->u.position.x = ssd->ptr_x;
300 ccmd->u.position.y = ssd->ptr_y;
301 }
302 ccmd->release_info.id = (uintptr_t)(&update->ext);
303
304 cmd->type = QXL_CMD_CURSOR;
305 cmd->data = (uintptr_t)ccmd;
306
307 return update;
308}
309
a3e22260 310/*
4580c490 311 * Called from spice server thread context (via interface_release_resource)
a3e22260 312 * We do *not* hold the global qemu mutex here, so extra care is needed
5cbdb3a3 313 * when calling qemu functions. QEMU interfaces used:
7267c094 314 * - g_free (underlying glibc free is re-entrant).
a3e22260
GH
315 */
316void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
317{
7267c094
AL
318 g_free(update->bitmap);
319 g_free(update);
a3e22260
GH
320}
321
322void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
323{
324 QXLDevMemSlot memslot;
325
35b2122d 326 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
a3e22260
GH
327
328 memset(&memslot, 0, sizeof(memslot));
329 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
330 memslot.virt_end = ~0;
5ff4e36c 331 qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
a3e22260
GH
332}
333
334void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
335{
336 QXLDevSurfaceCreate surface;
ab9509cc 337 uint64_t surface_size;
a3e22260 338
160c31f7
AL
339 memset(&surface, 0, sizeof(surface));
340
ab9509cc
GH
341 surface_size = (uint64_t) surface_width(ssd->ds) *
342 surface_height(ssd->ds) * 4;
343 assert(surface_size > 0);
344 assert(surface_size < INT_MAX);
345 if (ssd->bufsize < surface_size) {
346 ssd->bufsize = surface_size;
347 g_free(ssd->buf);
348 ssd->buf = g_malloc(ssd->bufsize);
349 }
350
351 dprint(1, "%s/%d: %ux%u (size %" PRIu64 "/%d)\n", __func__, ssd->qxl.id,
352 surface_width(ssd->ds), surface_height(ssd->ds),
353 surface_size, ssd->bufsize);
a3e22260
GH
354
355 surface.format = SPICE_SURFACE_FMT_32_xRGB;
71874c17
GH
356 surface.width = surface_width(ssd->ds);
357 surface.height = surface_height(ssd->ds);
a3e22260 358 surface.stride = -surface.width * 4;
869564a9 359 surface.mouse_mode = true;
a3e22260
GH
360 surface.flags = 0;
361 surface.type = 0;
a13ccc99 362 surface.mem = (uintptr_t)ssd->buf;
a3e22260 363 surface.group_id = MEMSLOT_GROUP_HOST;
7466bc49 364
5ff4e36c 365 qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
a3e22260
GH
366}
367
368void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
369{
35b2122d 370 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
a3e22260 371
5ff4e36c 372 qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
a3e22260
GH
373}
374
c78f7137 375void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
a963f876 376{
a963f876 377 qemu_mutex_init(&ssd->lock);
b1af98ba 378 QTAILQ_INIT(&ssd->updates);
a963f876
GH
379 ssd->mouse_x = -1;
380 ssd->mouse_y = -1;
ddd8fdc7
GH
381 if (ssd->num_surfaces == 0) {
382 ssd->num_surfaces = 1024;
383 }
a963f876
GH
384}
385
a3e22260
GH
386/* display listener callbacks */
387
388void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
389 int x, int y, int w, int h)
390{
391 QXLRect update_area;
392
35b2122d
GH
393 dprint(2, "%s/%d: x %d y %d w %d h %d\n", __func__,
394 ssd->qxl.id, x, y, w, h);
a3e22260
GH
395 update_area.left = x,
396 update_area.right = x + w;
397 update_area.top = y;
398 update_area.bottom = y + h;
399
a3e22260
GH
400 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
401 ssd->notify++;
402 }
403 qemu_spice_rect_union(&ssd->dirty, &update_area);
a3e22260
GH
404}
405
c12aeb86
GH
406void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
407 DisplaySurface *surface)
a3e22260 408{
b1af98ba 409 SimpleSpiceUpdate *update;
4b87dc4c 410 bool need_destroy;
b1af98ba 411
35b2122d 412 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
a3e22260 413
a3e22260 414 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
d9a86569
GH
415 if (ssd->surface) {
416 pixman_image_unref(ssd->surface);
417 ssd->surface = NULL;
418 pixman_image_unref(ssd->mirror);
419 ssd->mirror = NULL;
420 }
a3e22260 421
e0c64d08 422 qemu_mutex_lock(&ssd->lock);
4b87dc4c 423 need_destroy = (ssd->ds != NULL);
71874c17 424 ssd->ds = surface;
b1af98ba
GH
425 while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
426 QTAILQ_REMOVE(&ssd->updates, update, next);
427 qemu_spice_destroy_update(ssd, update);
e0c64d08
GH
428 }
429 qemu_mutex_unlock(&ssd->lock);
4b87dc4c
GH
430 if (need_destroy) {
431 qemu_spice_destroy_host_primary(ssd);
432 }
433 if (ssd->ds) {
434 qemu_spice_create_host_primary(ssd);
435 }
a3e22260 436
a3e22260
GH
437 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
438 ssd->notify++;
a3e22260
GH
439}
440
0b2824e5 441static void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
a3e22260 442{
07536094 443 if (ssd->cursor) {
284d1c6b
GH
444 assert(ssd->dcl.con);
445 dpy_cursor_define(ssd->dcl.con, ssd->cursor);
07536094
GH
446 cursor_put(ssd->cursor);
447 ssd->cursor = NULL;
448 }
449 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
284d1c6b
GH
450 assert(ssd->dcl.con);
451 dpy_mouse_set(ssd->dcl.con, ssd->mouse_x, ssd->mouse_y, 1);
07536094
GH
452 ssd->mouse_x = -1;
453 ssd->mouse_y = -1;
454 }
bb5a8cd5
AL
455}
456
0b2824e5
GH
457void qemu_spice_cursor_refresh_bh(void *opaque)
458{
459 SimpleSpiceDisplay *ssd = opaque;
460
461 qemu_mutex_lock(&ssd->lock);
462 qemu_spice_cursor_refresh_unlocked(ssd);
463 qemu_mutex_unlock(&ssd->lock);
464}
465
bb5a8cd5
AL
466void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
467{
35b2122d 468 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
284d1c6b 469 graphic_hw_update(ssd->dcl.con);
bb5a8cd5
AL
470
471 qemu_mutex_lock(&ssd->lock);
71874c17 472 if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
b1af98ba 473 qemu_spice_create_update(ssd);
bb5a8cd5
AL
474 ssd->notify++;
475 }
e0c64d08
GH
476 qemu_mutex_unlock(&ssd->lock);
477
a3e22260
GH
478 if (ssd->notify) {
479 ssd->notify = 0;
5c59d118 480 qemu_spice_wakeup(ssd);
35b2122d 481 dprint(2, "%s/%d: notify\n", __func__, ssd->qxl.id);
a3e22260
GH
482 }
483}
484
485/* spice display interface callbacks */
486
487static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
488{
489 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
490
35b2122d 491 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
a3e22260
GH
492 ssd->worker = qxl_worker;
493}
494
495static void interface_set_compression_level(QXLInstance *sin, int level)
496{
35b2122d 497 dprint(1, "%s/%d:\n", __func__, sin->id);
a3e22260
GH
498 /* nothing to do */
499}
500
501static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
502{
35b2122d 503 dprint(3, "%s/%d:\n", __func__, sin->id);
a3e22260
GH
504 /* nothing to do */
505}
506
507static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
508{
509 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
510
511 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
512 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
513 info->num_memslots = NUM_MEMSLOTS;
514 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
515 info->internal_groupslot_id = 0;
ab9509cc 516 info->qxl_ram_size = 16 * 1024 * 1024;
ddd8fdc7 517 info->n_surfaces = ssd->num_surfaces;
a3e22260
GH
518}
519
520static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
521{
522 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
523 SimpleSpiceUpdate *update;
e0c64d08 524 int ret = false;
a3e22260 525
35b2122d 526 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
e0c64d08
GH
527
528 qemu_mutex_lock(&ssd->lock);
b1af98ba
GH
529 update = QTAILQ_FIRST(&ssd->updates);
530 if (update != NULL) {
531 QTAILQ_REMOVE(&ssd->updates, update, next);
e0c64d08
GH
532 *ext = update->ext;
533 ret = true;
a3e22260 534 }
e0c64d08
GH
535 qemu_mutex_unlock(&ssd->lock);
536
537 return ret;
a3e22260
GH
538}
539
540static int interface_req_cmd_notification(QXLInstance *sin)
541{
35b2122d 542 dprint(1, "%s/%d:\n", __func__, sin->id);
a3e22260
GH
543 return 1;
544}
545
546static void interface_release_resource(QXLInstance *sin,
5643fc01 547 struct QXLReleaseInfoExt rext)
a3e22260
GH
548{
549 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
5643fc01
GH
550 SimpleSpiceUpdate *update;
551 SimpleSpiceCursor *cursor;
552 QXLCommandExt *ext;
a3e22260 553
35b2122d 554 dprint(2, "%s/%d:\n", __func__, ssd->qxl.id);
e8e23b7d 555 ext = (void *)(intptr_t)(rext.info->id);
5643fc01
GH
556 switch (ext->cmd.type) {
557 case QXL_CMD_DRAW:
558 update = container_of(ext, SimpleSpiceUpdate, ext);
559 qemu_spice_destroy_update(ssd, update);
560 break;
561 case QXL_CMD_CURSOR:
562 cursor = container_of(ext, SimpleSpiceCursor, ext);
563 g_free(cursor);
564 break;
565 default:
566 g_assert_not_reached();
567 }
a3e22260
GH
568}
569
570static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
571{
5643fc01
GH
572 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
573 int ret;
574
575 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
576
577 qemu_mutex_lock(&ssd->lock);
578 if (ssd->ptr_define) {
579 *ext = ssd->ptr_define->ext;
580 ssd->ptr_define = NULL;
581 ret = true;
582 } else if (ssd->ptr_move) {
583 *ext = ssd->ptr_move->ext;
584 ssd->ptr_move = NULL;
585 ret = true;
586 } else {
587 ret = false;
588 }
589 qemu_mutex_unlock(&ssd->lock);
590 return ret;
a3e22260
GH
591}
592
593static int interface_req_cursor_notification(QXLInstance *sin)
594{
595 dprint(1, "%s:\n", __FUNCTION__);
596 return 1;
597}
598
599static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
600{
601 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
602 abort();
603}
604
605static int interface_flush_resources(QXLInstance *sin)
606{
607 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
608 abort();
609 return 0;
610}
611
21a50d0b
GH
612static void interface_update_area_complete(QXLInstance *sin,
613 uint32_t surface_id,
614 QXLRect *dirty, uint32_t num_updated_rects)
615{
616 /* should never be called, used in qxl native mode only */
617 fprintf(stderr, "%s: abort()\n", __func__);
618 abort();
619}
620
621/* called from spice server thread context only */
622static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
623{
624 /* should never be called, used in qxl native mode only */
625 fprintf(stderr, "%s: abort()\n", __func__);
626 abort();
627}
628
629static void interface_set_client_capabilities(QXLInstance *sin,
630 uint8_t client_present,
631 uint8_t caps[58])
632{
633 dprint(3, "%s:\n", __func__);
634}
635
636static int interface_client_monitors_config(QXLInstance *sin,
9b74d0d5 637 VDAgentMonitorsConfig *mc)
21a50d0b 638{
9b74d0d5
GH
639 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
640 QemuUIInfo info;
641 int rc;
642
dc491cfc
GH
643 if (!mc) {
644 return 1;
645 }
646
9b74d0d5
GH
647 /*
648 * FIXME: multihead is tricky due to the way
649 * spice has multihead implemented.
650 */
651 memset(&info, 0, sizeof(info));
652 if (mc->num_of_monitors > 0) {
653 info.width = mc->monitors[0].width;
654 info.height = mc->monitors[0].height;
655 }
656 rc = dpy_set_ui_info(ssd->dcl.con, &info);
657 dprint(1, "%s/%d: size %dx%d, rc %d <--- ==========================\n",
658 __func__, ssd->qxl.id, info.width, info.height, rc);
659 if (rc != 0) {
660 return 0; /* == not supported by guest */
661 } else {
662 return 1;
663 }
21a50d0b
GH
664}
665
a3e22260
GH
666static const QXLInterface dpy_interface = {
667 .base.type = SPICE_INTERFACE_QXL,
668 .base.description = "qemu simple display",
669 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
670 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
671
672 .attache_worker = interface_attach_worker,
673 .set_compression_level = interface_set_compression_level,
674 .set_mm_time = interface_set_mm_time,
675 .get_init_info = interface_get_init_info,
676
677 /* the callbacks below are called from spice server thread context */
678 .get_command = interface_get_command,
679 .req_cmd_notification = interface_req_cmd_notification,
680 .release_resource = interface_release_resource,
681 .get_cursor_command = interface_get_cursor_command,
682 .req_cursor_notification = interface_req_cursor_notification,
683 .notify_update = interface_notify_update,
684 .flush_resources = interface_flush_resources,
21a50d0b
GH
685 .async_complete = interface_async_complete,
686 .update_area_complete = interface_update_area_complete,
687 .set_client_capabilities = interface_set_client_capabilities,
688 .client_monitors_config = interface_client_monitors_config,
a3e22260
GH
689};
690
7c20b4a3 691static void display_update(DisplayChangeListener *dcl,
7c20b4a3 692 int x, int y, int w, int h)
a3e22260 693{
9c80a315
GH
694 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
695 qemu_spice_display_update(ssd, x, y, w, h);
a3e22260
GH
696}
697
c12aeb86 698static void display_switch(DisplayChangeListener *dcl,
c12aeb86 699 struct DisplaySurface *surface)
a3e22260 700{
9c80a315 701 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
c12aeb86 702 qemu_spice_display_switch(ssd, surface);
a3e22260
GH
703}
704
bc2ed970 705static void display_refresh(DisplayChangeListener *dcl)
a3e22260 706{
9c80a315
GH
707 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
708 qemu_spice_display_refresh(ssd);
a3e22260
GH
709}
710
5643fc01
GH
711static void display_mouse_set(DisplayChangeListener *dcl,
712 int x, int y, int on)
713{
714 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
715
716 qemu_mutex_lock(&ssd->lock);
717 ssd->ptr_x = x;
718 ssd->ptr_y = x;
719 if (ssd->ptr_move) {
720 g_free(ssd->ptr_move);
721 }
722 ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL);
723 qemu_mutex_unlock(&ssd->lock);
724}
725
726static void display_mouse_define(DisplayChangeListener *dcl,
727 QEMUCursor *c)
728{
729 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
730
731 qemu_mutex_lock(&ssd->lock);
732 if (ssd->ptr_move) {
733 g_free(ssd->ptr_move);
734 ssd->ptr_move = NULL;
735 }
736 if (ssd->ptr_define) {
737 g_free(ssd->ptr_define);
738 }
739 ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c);
740 qemu_mutex_unlock(&ssd->lock);
741}
742
7c20b4a3 743static const DisplayChangeListenerOps display_listener_ops = {
5643fc01
GH
744 .dpy_name = "spice",
745 .dpy_gfx_update = display_update,
746 .dpy_gfx_switch = display_switch,
747 .dpy_refresh = display_refresh,
748 .dpy_mouse_set = display_mouse_set,
749 .dpy_cursor_define = display_mouse_define,
a3e22260
GH
750};
751
9fa03286 752static void qemu_spice_display_init_one(QemuConsole *con)
a3e22260 753{
9c80a315
GH
754 SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
755
c78f7137 756 qemu_spice_display_init_common(ssd);
a3e22260 757
9c80a315 758 ssd->qxl.base.sif = &dpy_interface.base;
9fa03286 759 qemu_spice_add_display_interface(&ssd->qxl, con);
9c80a315 760 assert(ssd->worker);
a3e22260 761
9c80a315 762 qemu_spice_create_host_memslot(ssd);
7c20b4a3 763
9c80a315 764 ssd->dcl.ops = &display_listener_ops;
9fa03286 765 ssd->dcl.con = con;
5209089f 766 register_displaychangelistener(&ssd->dcl);
a3e22260 767}
9fa03286
GH
768
769void qemu_spice_display_init(void)
770{
771 QemuConsole *con;
772 int i;
773
774 for (i = 0;; i++) {
775 con = qemu_console_lookup_by_index(i);
776 if (!con || !qemu_console_is_graphic(con)) {
777 break;
778 }
779 if (qemu_spice_have_display_interface(con)) {
780 continue;
781 }
782 qemu_spice_display_init_one(con);
783 }
784}