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