]> git.proxmox.com Git - mirror_qemu.git/blame - ui/spice-display.c
spice: fix mouse cursor position
[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
c1d37cd3 181 dest = pixman_image_create_bits(PIXMAN_LE_x8r8g8b8, bw, bh,
d9a86569
GH
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
b021bd29
GH
210 for (blk = 0; blk < blocks; blk++) {
211 dirty_top[blk] = -1;
212 }
213
71874c17 214 guest = surface_data(ssd->ds);
d9a86569 215 mirror = (void *)pixman_image_get_data(ssd->mirror);
b021bd29 216 for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
71874c17 217 yoff = y * surface_stride(ssd->ds);
b021bd29
GH
218 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
219 xoff = x * bpp;
220 blk = x / blksize;
221 bw = MIN(blksize, ssd->dirty.right - x);
222 if (memcmp(guest + yoff + xoff,
223 mirror + yoff + xoff,
224 bw * bpp) == 0) {
225 if (dirty_top[blk] != -1) {
226 QXLRect update = {
227 .top = dirty_top[blk],
228 .bottom = y,
229 .left = x,
230 .right = x + bw,
231 };
232 qemu_spice_create_one_update(ssd, &update);
233 dirty_top[blk] = -1;
234 }
235 } else {
236 if (dirty_top[blk] == -1) {
237 dirty_top[blk] = y;
238 }
239 }
240 }
241 }
242
243 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
244 blk = x / blksize;
245 bw = MIN(blksize, ssd->dirty.right - x);
246 if (dirty_top[blk] != -1) {
247 QXLRect update = {
248 .top = dirty_top[blk],
249 .bottom = ssd->dirty.bottom,
250 .left = x,
251 .right = x + bw,
252 };
253 qemu_spice_create_one_update(ssd, &update);
254 dirty_top[blk] = -1;
255 }
256 }
257
c60319a3
GH
258 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
259}
260
5643fc01
GH
261static SimpleSpiceCursor*
262qemu_spice_create_cursor_update(SimpleSpiceDisplay *ssd,
263 QEMUCursor *c)
264{
265 size_t size = c ? c->width * c->height * 4 : 0;
266 SimpleSpiceCursor *update;
267 QXLCursorCmd *ccmd;
268 QXLCursor *cursor;
269 QXLCommand *cmd;
270
271 update = g_malloc0(sizeof(*update) + size);
272 ccmd = &update->cmd;
273 cursor = &update->cursor;
274 cmd = &update->ext.cmd;
275
276 if (c) {
277 ccmd->type = QXL_CURSOR_SET;
278 ccmd->u.set.position.x = ssd->ptr_x;
279 ccmd->u.set.position.y = ssd->ptr_y;
280 ccmd->u.set.visible = true;
281 ccmd->u.set.shape = (uintptr_t)cursor;
282 cursor->header.unique = ssd->unique++;
283 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
284 cursor->header.width = c->width;
285 cursor->header.height = c->height;
286 cursor->header.hot_spot_x = c->hot_x;
287 cursor->header.hot_spot_y = c->hot_y;
288 cursor->data_size = size;
289 cursor->chunk.data_size = size;
290 memcpy(cursor->chunk.data, c->data, size);
291 } else {
292 ccmd->type = QXL_CURSOR_MOVE;
293 ccmd->u.position.x = ssd->ptr_x;
294 ccmd->u.position.y = ssd->ptr_y;
295 }
296 ccmd->release_info.id = (uintptr_t)(&update->ext);
297
298 cmd->type = QXL_CMD_CURSOR;
299 cmd->data = (uintptr_t)ccmd;
300
301 return update;
302}
303
a3e22260 304/*
4580c490 305 * Called from spice server thread context (via interface_release_resource)
a3e22260 306 * We do *not* hold the global qemu mutex here, so extra care is needed
5cbdb3a3 307 * when calling qemu functions. QEMU interfaces used:
7267c094 308 * - g_free (underlying glibc free is re-entrant).
a3e22260
GH
309 */
310void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
311{
7267c094
AL
312 g_free(update->bitmap);
313 g_free(update);
a3e22260
GH
314}
315
316void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
317{
318 QXLDevMemSlot memslot;
319
35b2122d 320 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
a3e22260
GH
321
322 memset(&memslot, 0, sizeof(memslot));
323 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
324 memslot.virt_end = ~0;
5ff4e36c 325 qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
a3e22260
GH
326}
327
328void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
329{
330 QXLDevSurfaceCreate surface;
ab9509cc 331 uint64_t surface_size;
a3e22260 332
160c31f7
AL
333 memset(&surface, 0, sizeof(surface));
334
ab9509cc
GH
335 surface_size = (uint64_t) surface_width(ssd->ds) *
336 surface_height(ssd->ds) * 4;
337 assert(surface_size > 0);
338 assert(surface_size < INT_MAX);
339 if (ssd->bufsize < surface_size) {
340 ssd->bufsize = surface_size;
341 g_free(ssd->buf);
342 ssd->buf = g_malloc(ssd->bufsize);
343 }
344
345 dprint(1, "%s/%d: %ux%u (size %" PRIu64 "/%d)\n", __func__, ssd->qxl.id,
346 surface_width(ssd->ds), surface_height(ssd->ds),
347 surface_size, ssd->bufsize);
a3e22260
GH
348
349 surface.format = SPICE_SURFACE_FMT_32_xRGB;
71874c17
GH
350 surface.width = surface_width(ssd->ds);
351 surface.height = surface_height(ssd->ds);
a3e22260 352 surface.stride = -surface.width * 4;
869564a9 353 surface.mouse_mode = true;
a3e22260
GH
354 surface.flags = 0;
355 surface.type = 0;
a13ccc99 356 surface.mem = (uintptr_t)ssd->buf;
a3e22260 357 surface.group_id = MEMSLOT_GROUP_HOST;
7466bc49 358
5ff4e36c 359 qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
a3e22260
GH
360}
361
362void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
363{
35b2122d 364 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
a3e22260 365
5ff4e36c 366 qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
a3e22260
GH
367}
368
c78f7137 369void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
a963f876 370{
a963f876 371 qemu_mutex_init(&ssd->lock);
b1af98ba 372 QTAILQ_INIT(&ssd->updates);
a963f876
GH
373 ssd->mouse_x = -1;
374 ssd->mouse_y = -1;
ddd8fdc7
GH
375 if (ssd->num_surfaces == 0) {
376 ssd->num_surfaces = 1024;
377 }
a963f876
GH
378}
379
a3e22260
GH
380/* display listener callbacks */
381
382void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
383 int x, int y, int w, int h)
384{
385 QXLRect update_area;
386
35b2122d
GH
387 dprint(2, "%s/%d: x %d y %d w %d h %d\n", __func__,
388 ssd->qxl.id, x, y, w, h);
a3e22260
GH
389 update_area.left = x,
390 update_area.right = x + w;
391 update_area.top = y;
392 update_area.bottom = y + h;
393
a3e22260
GH
394 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
395 ssd->notify++;
396 }
397 qemu_spice_rect_union(&ssd->dirty, &update_area);
a3e22260
GH
398}
399
c12aeb86
GH
400void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
401 DisplaySurface *surface)
a3e22260 402{
b1af98ba 403 SimpleSpiceUpdate *update;
4b87dc4c 404 bool need_destroy;
b1af98ba 405
555e72f2
GH
406 if (surface && ssd->surface &&
407 surface_width(surface) == pixman_image_get_width(ssd->surface) &&
408 surface_height(surface) == pixman_image_get_height(ssd->surface)) {
409 /* no-resize fast path: just swap backing store */
410 dprint(1, "%s/%d: fast (%dx%d)\n", __func__, ssd->qxl.id,
411 surface_width(surface), surface_height(surface));
412 qemu_mutex_lock(&ssd->lock);
413 ssd->ds = surface;
414 pixman_image_unref(ssd->surface);
415 ssd->surface = pixman_image_ref(ssd->ds->image);
416 qemu_mutex_unlock(&ssd->lock);
417 qemu_spice_display_update(ssd, 0, 0,
418 surface_width(surface),
419 surface_height(surface));
420 return;
421 }
422
423 /* full mode switch */
424 dprint(1, "%s/%d: full (%dx%d -> %dx%d)\n", __func__, ssd->qxl.id,
425 ssd->surface ? pixman_image_get_width(ssd->surface) : 0,
426 ssd->surface ? pixman_image_get_height(ssd->surface) : 0,
427 surface ? surface_width(surface) : 0,
428 surface ? surface_height(surface) : 0);
a3e22260 429
a3e22260 430 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
d9a86569
GH
431 if (ssd->surface) {
432 pixman_image_unref(ssd->surface);
433 ssd->surface = NULL;
434 pixman_image_unref(ssd->mirror);
435 ssd->mirror = NULL;
436 }
a3e22260 437
e0c64d08 438 qemu_mutex_lock(&ssd->lock);
4b87dc4c 439 need_destroy = (ssd->ds != NULL);
71874c17 440 ssd->ds = surface;
b1af98ba
GH
441 while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
442 QTAILQ_REMOVE(&ssd->updates, update, next);
443 qemu_spice_destroy_update(ssd, update);
e0c64d08
GH
444 }
445 qemu_mutex_unlock(&ssd->lock);
4b87dc4c
GH
446 if (need_destroy) {
447 qemu_spice_destroy_host_primary(ssd);
448 }
449 if (ssd->ds) {
51a09099
GH
450 ssd->surface = pixman_image_ref(ssd->ds->image);
451 ssd->mirror = qemu_pixman_mirror_create(ssd->ds->format,
452 ssd->ds->image);
4b87dc4c
GH
453 qemu_spice_create_host_primary(ssd);
454 }
a3e22260 455
a3e22260
GH
456 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
457 ssd->notify++;
a3e22260
GH
458}
459
0b2824e5 460static void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
a3e22260 461{
07536094 462 if (ssd->cursor) {
284d1c6b
GH
463 assert(ssd->dcl.con);
464 dpy_cursor_define(ssd->dcl.con, ssd->cursor);
07536094
GH
465 cursor_put(ssd->cursor);
466 ssd->cursor = NULL;
467 }
468 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
284d1c6b
GH
469 assert(ssd->dcl.con);
470 dpy_mouse_set(ssd->dcl.con, ssd->mouse_x, ssd->mouse_y, 1);
07536094
GH
471 ssd->mouse_x = -1;
472 ssd->mouse_y = -1;
473 }
bb5a8cd5
AL
474}
475
0b2824e5
GH
476void qemu_spice_cursor_refresh_bh(void *opaque)
477{
478 SimpleSpiceDisplay *ssd = opaque;
479
480 qemu_mutex_lock(&ssd->lock);
481 qemu_spice_cursor_refresh_unlocked(ssd);
482 qemu_mutex_unlock(&ssd->lock);
483}
484
bb5a8cd5
AL
485void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
486{
35b2122d 487 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
284d1c6b 488 graphic_hw_update(ssd->dcl.con);
bb5a8cd5
AL
489
490 qemu_mutex_lock(&ssd->lock);
71874c17 491 if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
b1af98ba 492 qemu_spice_create_update(ssd);
bb5a8cd5
AL
493 ssd->notify++;
494 }
e0c64d08
GH
495 qemu_mutex_unlock(&ssd->lock);
496
a3e22260
GH
497 if (ssd->notify) {
498 ssd->notify = 0;
5c59d118 499 qemu_spice_wakeup(ssd);
35b2122d 500 dprint(2, "%s/%d: notify\n", __func__, ssd->qxl.id);
a3e22260
GH
501 }
502}
503
504/* spice display interface callbacks */
505
506static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
507{
508 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
509
35b2122d 510 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
a3e22260
GH
511 ssd->worker = qxl_worker;
512}
513
514static void interface_set_compression_level(QXLInstance *sin, int level)
515{
35b2122d 516 dprint(1, "%s/%d:\n", __func__, sin->id);
a3e22260
GH
517 /* nothing to do */
518}
519
520static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
521{
35b2122d 522 dprint(3, "%s/%d:\n", __func__, sin->id);
a3e22260
GH
523 /* nothing to do */
524}
525
526static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
527{
528 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
529
530 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
531 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
532 info->num_memslots = NUM_MEMSLOTS;
533 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
534 info->internal_groupslot_id = 0;
ab9509cc 535 info->qxl_ram_size = 16 * 1024 * 1024;
ddd8fdc7 536 info->n_surfaces = ssd->num_surfaces;
a3e22260
GH
537}
538
539static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
540{
541 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
542 SimpleSpiceUpdate *update;
e0c64d08 543 int ret = false;
a3e22260 544
35b2122d 545 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
e0c64d08
GH
546
547 qemu_mutex_lock(&ssd->lock);
b1af98ba
GH
548 update = QTAILQ_FIRST(&ssd->updates);
549 if (update != NULL) {
550 QTAILQ_REMOVE(&ssd->updates, update, next);
e0c64d08
GH
551 *ext = update->ext;
552 ret = true;
a3e22260 553 }
e0c64d08
GH
554 qemu_mutex_unlock(&ssd->lock);
555
556 return ret;
a3e22260
GH
557}
558
559static int interface_req_cmd_notification(QXLInstance *sin)
560{
35b2122d 561 dprint(1, "%s/%d:\n", __func__, sin->id);
a3e22260
GH
562 return 1;
563}
564
565static void interface_release_resource(QXLInstance *sin,
5643fc01 566 struct QXLReleaseInfoExt rext)
a3e22260
GH
567{
568 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
5643fc01
GH
569 SimpleSpiceUpdate *update;
570 SimpleSpiceCursor *cursor;
571 QXLCommandExt *ext;
a3e22260 572
35b2122d 573 dprint(2, "%s/%d:\n", __func__, ssd->qxl.id);
e8e23b7d 574 ext = (void *)(intptr_t)(rext.info->id);
5643fc01
GH
575 switch (ext->cmd.type) {
576 case QXL_CMD_DRAW:
577 update = container_of(ext, SimpleSpiceUpdate, ext);
578 qemu_spice_destroy_update(ssd, update);
579 break;
580 case QXL_CMD_CURSOR:
581 cursor = container_of(ext, SimpleSpiceCursor, ext);
582 g_free(cursor);
583 break;
584 default:
585 g_assert_not_reached();
586 }
a3e22260
GH
587}
588
589static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
590{
5643fc01
GH
591 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
592 int ret;
593
594 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
595
596 qemu_mutex_lock(&ssd->lock);
597 if (ssd->ptr_define) {
598 *ext = ssd->ptr_define->ext;
599 ssd->ptr_define = NULL;
600 ret = true;
601 } else if (ssd->ptr_move) {
602 *ext = ssd->ptr_move->ext;
603 ssd->ptr_move = NULL;
604 ret = true;
605 } else {
606 ret = false;
607 }
608 qemu_mutex_unlock(&ssd->lock);
609 return ret;
a3e22260
GH
610}
611
612static int interface_req_cursor_notification(QXLInstance *sin)
613{
614 dprint(1, "%s:\n", __FUNCTION__);
615 return 1;
616}
617
618static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
619{
620 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
621 abort();
622}
623
624static int interface_flush_resources(QXLInstance *sin)
625{
626 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
627 abort();
628 return 0;
629}
630
21a50d0b
GH
631static void interface_update_area_complete(QXLInstance *sin,
632 uint32_t surface_id,
633 QXLRect *dirty, uint32_t num_updated_rects)
634{
635 /* should never be called, used in qxl native mode only */
636 fprintf(stderr, "%s: abort()\n", __func__);
637 abort();
638}
639
640/* called from spice server thread context only */
641static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
642{
643 /* should never be called, used in qxl native mode only */
644 fprintf(stderr, "%s: abort()\n", __func__);
645 abort();
646}
647
648static void interface_set_client_capabilities(QXLInstance *sin,
649 uint8_t client_present,
650 uint8_t caps[58])
651{
652 dprint(3, "%s:\n", __func__);
653}
654
655static int interface_client_monitors_config(QXLInstance *sin,
9b74d0d5 656 VDAgentMonitorsConfig *mc)
21a50d0b 657{
9b74d0d5
GH
658 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
659 QemuUIInfo info;
660 int rc;
661
dc491cfc
GH
662 if (!mc) {
663 return 1;
664 }
665
9b74d0d5
GH
666 /*
667 * FIXME: multihead is tricky due to the way
668 * spice has multihead implemented.
669 */
670 memset(&info, 0, sizeof(info));
671 if (mc->num_of_monitors > 0) {
672 info.width = mc->monitors[0].width;
673 info.height = mc->monitors[0].height;
674 }
675 rc = dpy_set_ui_info(ssd->dcl.con, &info);
676 dprint(1, "%s/%d: size %dx%d, rc %d <--- ==========================\n",
677 __func__, ssd->qxl.id, info.width, info.height, rc);
678 if (rc != 0) {
679 return 0; /* == not supported by guest */
680 } else {
681 return 1;
682 }
21a50d0b
GH
683}
684
a3e22260
GH
685static const QXLInterface dpy_interface = {
686 .base.type = SPICE_INTERFACE_QXL,
687 .base.description = "qemu simple display",
688 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
689 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
690
691 .attache_worker = interface_attach_worker,
692 .set_compression_level = interface_set_compression_level,
693 .set_mm_time = interface_set_mm_time,
694 .get_init_info = interface_get_init_info,
695
696 /* the callbacks below are called from spice server thread context */
697 .get_command = interface_get_command,
698 .req_cmd_notification = interface_req_cmd_notification,
699 .release_resource = interface_release_resource,
700 .get_cursor_command = interface_get_cursor_command,
701 .req_cursor_notification = interface_req_cursor_notification,
702 .notify_update = interface_notify_update,
703 .flush_resources = interface_flush_resources,
21a50d0b
GH
704 .async_complete = interface_async_complete,
705 .update_area_complete = interface_update_area_complete,
706 .set_client_capabilities = interface_set_client_capabilities,
707 .client_monitors_config = interface_client_monitors_config,
a3e22260
GH
708};
709
7c20b4a3 710static void display_update(DisplayChangeListener *dcl,
7c20b4a3 711 int x, int y, int w, int h)
a3e22260 712{
9c80a315
GH
713 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
714 qemu_spice_display_update(ssd, x, y, w, h);
a3e22260
GH
715}
716
c12aeb86 717static void display_switch(DisplayChangeListener *dcl,
c12aeb86 718 struct DisplaySurface *surface)
a3e22260 719{
9c80a315 720 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
c12aeb86 721 qemu_spice_display_switch(ssd, surface);
a3e22260
GH
722}
723
bc2ed970 724static void display_refresh(DisplayChangeListener *dcl)
a3e22260 725{
9c80a315
GH
726 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
727 qemu_spice_display_refresh(ssd);
a3e22260
GH
728}
729
5643fc01
GH
730static void display_mouse_set(DisplayChangeListener *dcl,
731 int x, int y, int on)
732{
733 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
734
735 qemu_mutex_lock(&ssd->lock);
736 ssd->ptr_x = x;
d0df04a1 737 ssd->ptr_y = y;
5643fc01
GH
738 if (ssd->ptr_move) {
739 g_free(ssd->ptr_move);
740 }
741 ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL);
742 qemu_mutex_unlock(&ssd->lock);
743}
744
745static void display_mouse_define(DisplayChangeListener *dcl,
746 QEMUCursor *c)
747{
748 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
749
750 qemu_mutex_lock(&ssd->lock);
751 if (ssd->ptr_move) {
752 g_free(ssd->ptr_move);
753 ssd->ptr_move = NULL;
754 }
755 if (ssd->ptr_define) {
756 g_free(ssd->ptr_define);
757 }
758 ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c);
759 qemu_mutex_unlock(&ssd->lock);
760}
761
7c20b4a3 762static const DisplayChangeListenerOps display_listener_ops = {
0002a518
GH
763 .dpy_name = "spice",
764 .dpy_gfx_update = display_update,
765 .dpy_gfx_switch = display_switch,
766 .dpy_gfx_check_format = qemu_pixman_check_format,
767 .dpy_refresh = display_refresh,
768 .dpy_mouse_set = display_mouse_set,
769 .dpy_cursor_define = display_mouse_define,
a3e22260
GH
770};
771
9fa03286 772static void qemu_spice_display_init_one(QemuConsole *con)
a3e22260 773{
9c80a315
GH
774 SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
775
c78f7137 776 qemu_spice_display_init_common(ssd);
a3e22260 777
9c80a315 778 ssd->qxl.base.sif = &dpy_interface.base;
9fa03286 779 qemu_spice_add_display_interface(&ssd->qxl, con);
9c80a315 780 assert(ssd->worker);
a3e22260 781
9c80a315 782 qemu_spice_create_host_memslot(ssd);
7c20b4a3 783
9c80a315 784 ssd->dcl.ops = &display_listener_ops;
9fa03286 785 ssd->dcl.con = con;
5209089f 786 register_displaychangelistener(&ssd->dcl);
a3e22260 787}
9fa03286
GH
788
789void qemu_spice_display_init(void)
790{
791 QemuConsole *con;
792 int i;
793
794 for (i = 0;; i++) {
795 con = qemu_console_lookup_by_index(i);
796 if (!con || !qemu_console_is_graphic(con)) {
797 break;
798 }
799 if (qemu_spice_have_display_interface(con)) {
800 continue;
801 }
802 qemu_spice_display_init_one(con);
803 }
804}