blob: ad1ceafb3f0a45be0bab95bd3ae018d2ee1ecd50 [file] [log] [blame]
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02001/*
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
Peter Maydelle16f4c82016-01-29 17:49:51 +000018#include "qemu/osdep.h"
Gerd Hoffmanna3e22262010-08-25 15:32:06 +020019#include "qemu-common.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010020#include "ui/qemu-spice.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010021#include "qemu/timer.h"
22#include "qemu/queue.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010023#include "ui/console.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010024#include "sysemu/sysemu.h"
Alon Levyc480bb72012-03-18 13:46:14 +010025#include "trace.h"
Gerd Hoffmanna3e22262010-08-25 15:32:06 +020026
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010027#include "ui/spice-display.h"
Gerd Hoffmanna3e22262010-08-25 15:32:06 +020028
29static int debug = 0;
Gerd Hoffmannfe5c44f2017-06-06 13:06:18 +020030bool spice_opengl;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +020031
Stefan Weil2c80e422010-10-13 20:54:27 +020032static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +020033{
34 va_list args;
35
36 if (level <= debug) {
37 va_start(args, fmt);
38 vfprintf(stderr, fmt, args);
39 va_end(args);
40 }
41}
42
43int qemu_spice_rect_is_empty(const QXLRect* r)
44{
45 return r->top == r->bottom || r->left == r->right;
46}
47
48void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
49{
50 if (qemu_spice_rect_is_empty(r)) {
51 return;
52 }
53
54 if (qemu_spice_rect_is_empty(dest)) {
55 *dest = *r;
56 return;
57 }
58
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);
63}
64
Alon Levy2e1a98c2012-02-24 23:19:30 +020065QXLCookie *qxl_cookie_new(int type, uint64_t io)
66{
67 QXLCookie *cookie;
68
69 cookie = g_malloc0(sizeof(*cookie));
70 cookie->type = type;
71 cookie->io = io;
72 return cookie;
73}
74
Alon Levy5ff4e362011-07-20 12:20:58 +030075void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
76 qxl_async_io async)
Gerd Hoffmann5c59d112011-07-20 12:20:50 +030077{
Alon Levyc480bb72012-03-18 13:46:14 +010078 trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
79 memslot->virt_start, memslot->virt_end,
80 async);
81
Alon Levy5ff4e362011-07-20 12:20:58 +030082 if (async != QXL_SYNC) {
Alon Levy2e1a98c2012-02-24 23:19:30 +020083 spice_qxl_add_memslot_async(&ssd->qxl, memslot,
Peter Maydell34d14c62012-03-07 13:36:48 +000084 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
85 QXL_IO_MEMSLOT_ADD_ASYNC));
Alon Levy5ff4e362011-07-20 12:20:58 +030086 } else {
Marc-André Lureau26defe82013-10-04 13:10:46 +020087 spice_qxl_add_memslot(&ssd->qxl, memslot);
Alon Levy5ff4e362011-07-20 12:20:58 +030088 }
Gerd Hoffmann5c59d112011-07-20 12:20:50 +030089}
90
91void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
92{
Alon Levyc480bb72012-03-18 13:46:14 +010093 trace_qemu_spice_del_memslot(ssd->qxl.id, gid, sid);
Marc-André Lureau26defe82013-10-04 13:10:46 +020094 spice_qxl_del_memslot(&ssd->qxl, gid, sid);
Gerd Hoffmann5c59d112011-07-20 12:20:50 +030095}
96
97void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
Alon Levy5ff4e362011-07-20 12:20:58 +030098 QXLDevSurfaceCreate *surface,
99 qxl_async_io async)
Gerd Hoffmann5c59d112011-07-20 12:20:50 +0300100{
Alon Levyc480bb72012-03-18 13:46:14 +0100101 trace_qemu_spice_create_primary_surface(ssd->qxl.id, id, surface, async);
Alon Levy5ff4e362011-07-20 12:20:58 +0300102 if (async != QXL_SYNC) {
Alon Levy2e1a98c2012-02-24 23:19:30 +0200103 spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface,
Peter Maydell34d14c62012-03-07 13:36:48 +0000104 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
105 QXL_IO_CREATE_PRIMARY_ASYNC));
Alon Levy5ff4e362011-07-20 12:20:58 +0300106 } else {
Marc-André Lureau26defe82013-10-04 13:10:46 +0200107 spice_qxl_create_primary_surface(&ssd->qxl, id, surface);
Alon Levy5ff4e362011-07-20 12:20:58 +0300108 }
Gerd Hoffmann5c59d112011-07-20 12:20:50 +0300109}
110
Alon Levy5ff4e362011-07-20 12:20:58 +0300111void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
112 uint32_t id, qxl_async_io async)
Gerd Hoffmann5c59d112011-07-20 12:20:50 +0300113{
Alon Levyc480bb72012-03-18 13:46:14 +0100114 trace_qemu_spice_destroy_primary_surface(ssd->qxl.id, id, async);
Alon Levy5ff4e362011-07-20 12:20:58 +0300115 if (async != QXL_SYNC) {
Alon Levy2e1a98c2012-02-24 23:19:30 +0200116 spice_qxl_destroy_primary_surface_async(&ssd->qxl, id,
Peter Maydell34d14c62012-03-07 13:36:48 +0000117 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
118 QXL_IO_DESTROY_PRIMARY_ASYNC));
Alon Levy5ff4e362011-07-20 12:20:58 +0300119 } else {
Marc-André Lureau26defe82013-10-04 13:10:46 +0200120 spice_qxl_destroy_primary_surface(&ssd->qxl, id);
Alon Levy5ff4e362011-07-20 12:20:58 +0300121 }
Gerd Hoffmann5c59d112011-07-20 12:20:50 +0300122}
123
Gerd Hoffmann5c59d112011-07-20 12:20:50 +0300124void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
125{
Alon Levyc480bb72012-03-18 13:46:14 +0100126 trace_qemu_spice_wakeup(ssd->qxl.id);
Marc-André Lureau26defe82013-10-04 13:10:46 +0200127 spice_qxl_wakeup(&ssd->qxl);
Gerd Hoffmann5c59d112011-07-20 12:20:50 +0300128}
129
Gerd Hoffmannc60319a2012-09-05 08:52:23 +0200130static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
131 QXLRect *rect)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200132{
133 SimpleSpiceUpdate *update;
134 QXLDrawable *drawable;
135 QXLImage *image;
136 QXLCommand *cmd;
Gerd Hoffmannd9a86562012-11-02 09:12:49 +0100137 int bw, bh;
Alon Levy22795172011-06-15 20:44:38 +0200138 struct timespec time_space;
Gerd Hoffmannd9a86562012-11-02 09:12:49 +0100139 pixman_image_t *dest;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200140
Alon Levyc480bb72012-03-18 13:46:14 +0100141 trace_qemu_spice_create_update(
Gerd Hoffmannc60319a2012-09-05 08:52:23 +0200142 rect->left, rect->right,
143 rect->top, rect->bottom);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200144
Anthony Liguori7267c092011-08-20 22:09:37 -0500145 update = g_malloc0(sizeof(*update));
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200146 drawable = &update->drawable;
147 image = &update->image;
148 cmd = &update->ext.cmd;
149
Gerd Hoffmannc60319a2012-09-05 08:52:23 +0200150 bw = rect->right - rect->left;
151 bh = rect->bottom - rect->top;
Anthony Liguori7267c092011-08-20 22:09:37 -0500152 update->bitmap = g_malloc(bw * bh * 4);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200153
Gerd Hoffmannc60319a2012-09-05 08:52:23 +0200154 drawable->bbox = *rect;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200155 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
156 drawable->effect = QXL_EFFECT_OPAQUE;
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200157 drawable->release_info.id = (uintptr_t)(&update->ext);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200158 drawable->type = QXL_DRAW_COPY;
159 drawable->surfaces_dest[0] = -1;
160 drawable->surfaces_dest[1] = -1;
161 drawable->surfaces_dest[2] = -1;
Alon Levy22795172011-06-15 20:44:38 +0200162 clock_gettime(CLOCK_MONOTONIC, &time_space);
163 /* time in milliseconds from epoch. */
164 drawable->mm_time = time_space.tv_sec * 1000
165 + time_space.tv_nsec / 1000 / 1000;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200166
167 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
Alon Levya13ccc92012-03-21 18:17:18 +0200168 drawable->u.copy.src_bitmap = (uintptr_t)image;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200169 drawable->u.copy.src_area.right = bw;
170 drawable->u.copy.src_area.bottom = bh;
171
172 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
173 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
174 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
175 image->bitmap.stride = bw * 4;
176 image->descriptor.width = image->bitmap.x = bw;
177 image->descriptor.height = image->bitmap.y = bh;
Alon Levya13ccc92012-03-21 18:17:18 +0200178 image->bitmap.data = (uintptr_t)(update->bitmap);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200179 image->bitmap.palette = 0;
180 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
181
Gerd Hoffmannc1d37cd2015-04-14 08:56:21 +0200182 dest = pixman_image_create_bits(PIXMAN_LE_x8r8g8b8, bw, bh,
Gerd Hoffmannd9a86562012-11-02 09:12:49 +0100183 (void *)update->bitmap, bw * 4);
184 pixman_image_composite(PIXMAN_OP_SRC, ssd->surface, NULL, ssd->mirror,
185 rect->left, rect->top, 0, 0,
186 rect->left, rect->top, bw, bh);
187 pixman_image_composite(PIXMAN_OP_SRC, ssd->mirror, NULL, dest,
188 rect->left, rect->top, 0, 0,
189 0, 0, bw, bh);
190 pixman_image_unref(dest);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200191
192 cmd->type = QXL_CMD_DRAW;
Alon Levya13ccc92012-03-21 18:17:18 +0200193 cmd->data = (uintptr_t)drawable;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200194
Gerd Hoffmannb1af98b2012-09-05 08:25:08 +0200195 QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200196}
197
Gerd Hoffmannc60319a2012-09-05 08:52:23 +0200198static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
199{
Gerd Hoffmannb021bd22012-09-05 10:41:42 +0200200 static const int blksize = 32;
Laurent Vivier5d61caf2016-05-31 18:35:58 +0200201 int blocks = DIV_ROUND_UP(surface_width(ssd->ds), blksize);
Gerd Hoffmannb021bd22012-09-05 10:41:42 +0200202 int dirty_top[blocks];
Gerd Hoffmannc6e48472015-06-09 21:08:47 +0200203 int y, yoff1, yoff2, x, xoff, blk, bw;
Gerd Hoffmann71874c12013-02-28 16:42:28 +0100204 int bpp = surface_bytes_per_pixel(ssd->ds);
Gerd Hoffmannb021bd22012-09-05 10:41:42 +0200205 uint8_t *guest, *mirror;
206
Gerd Hoffmannc60319a2012-09-05 08:52:23 +0200207 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
208 return;
209 };
Gerd Hoffmanna7310dd2012-09-05 09:35:57 +0200210
Gerd Hoffmannb021bd22012-09-05 10:41:42 +0200211 for (blk = 0; blk < blocks; blk++) {
212 dirty_top[blk] = -1;
213 }
214
Gerd Hoffmann71874c12013-02-28 16:42:28 +0100215 guest = surface_data(ssd->ds);
Gerd Hoffmannd9a86562012-11-02 09:12:49 +0100216 mirror = (void *)pixman_image_get_data(ssd->mirror);
Gerd Hoffmannb021bd22012-09-05 10:41:42 +0200217 for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
Gerd Hoffmannc6e48472015-06-09 21:08:47 +0200218 yoff1 = y * surface_stride(ssd->ds);
219 yoff2 = y * pixman_image_get_stride(ssd->mirror);
Gerd Hoffmannb021bd22012-09-05 10:41:42 +0200220 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
221 xoff = x * bpp;
222 blk = x / blksize;
223 bw = MIN(blksize, ssd->dirty.right - x);
Gerd Hoffmannc6e48472015-06-09 21:08:47 +0200224 if (memcmp(guest + yoff1 + xoff,
225 mirror + yoff2 + xoff,
Gerd Hoffmannb021bd22012-09-05 10:41:42 +0200226 bw * bpp) == 0) {
227 if (dirty_top[blk] != -1) {
228 QXLRect update = {
229 .top = dirty_top[blk],
230 .bottom = y,
231 .left = x,
232 .right = x + bw,
233 };
234 qemu_spice_create_one_update(ssd, &update);
235 dirty_top[blk] = -1;
236 }
237 } else {
238 if (dirty_top[blk] == -1) {
239 dirty_top[blk] = y;
240 }
241 }
242 }
243 }
244
245 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
246 blk = x / blksize;
247 bw = MIN(blksize, ssd->dirty.right - x);
248 if (dirty_top[blk] != -1) {
249 QXLRect update = {
250 .top = dirty_top[blk],
251 .bottom = ssd->dirty.bottom,
252 .left = x,
253 .right = x + bw,
254 };
255 qemu_spice_create_one_update(ssd, &update);
256 dirty_top[blk] = -1;
257 }
258 }
259
Gerd Hoffmannc60319a2012-09-05 08:52:23 +0200260 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
261}
262
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200263static SimpleSpiceCursor*
264qemu_spice_create_cursor_update(SimpleSpiceDisplay *ssd,
Marc-André Lureau700cd852015-03-24 17:50:13 +0100265 QEMUCursor *c,
266 int on)
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200267{
268 size_t size = c ? c->width * c->height * 4 : 0;
269 SimpleSpiceCursor *update;
270 QXLCursorCmd *ccmd;
271 QXLCursor *cursor;
272 QXLCommand *cmd;
273
274 update = g_malloc0(sizeof(*update) + size);
275 ccmd = &update->cmd;
276 cursor = &update->cursor;
277 cmd = &update->ext.cmd;
278
279 if (c) {
280 ccmd->type = QXL_CURSOR_SET;
Marc-André Lureaudc8dcee2015-03-24 17:50:12 +0100281 ccmd->u.set.position.x = ssd->ptr_x + ssd->hot_x;
282 ccmd->u.set.position.y = ssd->ptr_y + ssd->hot_y;
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200283 ccmd->u.set.visible = true;
284 ccmd->u.set.shape = (uintptr_t)cursor;
285 cursor->header.unique = ssd->unique++;
286 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
287 cursor->header.width = c->width;
288 cursor->header.height = c->height;
289 cursor->header.hot_spot_x = c->hot_x;
290 cursor->header.hot_spot_y = c->hot_y;
291 cursor->data_size = size;
292 cursor->chunk.data_size = size;
293 memcpy(cursor->chunk.data, c->data, size);
Marc-André Lureau700cd852015-03-24 17:50:13 +0100294 } else if (!on) {
295 ccmd->type = QXL_CURSOR_HIDE;
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200296 } else {
297 ccmd->type = QXL_CURSOR_MOVE;
Marc-André Lureaudc8dcee2015-03-24 17:50:12 +0100298 ccmd->u.position.x = ssd->ptr_x + ssd->hot_x;
299 ccmd->u.position.y = ssd->ptr_y + ssd->hot_y;
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200300 }
301 ccmd->release_info.id = (uintptr_t)(&update->ext);
302
303 cmd->type = QXL_CMD_CURSOR;
304 cmd->data = (uintptr_t)ccmd;
305
306 return update;
307}
308
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200309/*
Stefan Weil4580c492012-08-17 15:20:00 +0200310 * Called from spice server thread context (via interface_release_resource)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200311 * We do *not* hold the global qemu mutex here, so extra care is needed
Stefan Weil5cbdb3a2012-04-07 09:23:39 +0200312 * when calling qemu functions. QEMU interfaces used:
Anthony Liguori7267c092011-08-20 22:09:37 -0500313 * - g_free (underlying glibc free is re-entrant).
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200314 */
315void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
316{
Anthony Liguori7267c092011-08-20 22:09:37 -0500317 g_free(update->bitmap);
318 g_free(update);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200319}
320
321void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
322{
323 QXLDevMemSlot memslot;
324
Gerd Hoffmann35b21222013-10-17 12:11:43 +0200325 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200326
327 memset(&memslot, 0, sizeof(memslot));
328 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
329 memslot.virt_end = ~0;
Alon Levy5ff4e362011-07-20 12:20:58 +0300330 qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200331}
332
333void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
334{
335 QXLDevSurfaceCreate surface;
Gerd Hoffmannab9509c2014-09-03 15:50:08 +0200336 uint64_t surface_size;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200337
Alon Levy160c31f2012-05-24 12:38:11 +0300338 memset(&surface, 0, sizeof(surface));
339
Gerd Hoffmannab9509c2014-09-03 15:50:08 +0200340 surface_size = (uint64_t) surface_width(ssd->ds) *
341 surface_height(ssd->ds) * 4;
342 assert(surface_size > 0);
343 assert(surface_size < INT_MAX);
344 if (ssd->bufsize < surface_size) {
345 ssd->bufsize = surface_size;
346 g_free(ssd->buf);
347 ssd->buf = g_malloc(ssd->bufsize);
348 }
349
350 dprint(1, "%s/%d: %ux%u (size %" PRIu64 "/%d)\n", __func__, ssd->qxl.id,
351 surface_width(ssd->ds), surface_height(ssd->ds),
352 surface_size, ssd->bufsize);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200353
354 surface.format = SPICE_SURFACE_FMT_32_xRGB;
Gerd Hoffmann71874c12013-02-28 16:42:28 +0100355 surface.width = surface_width(ssd->ds);
356 surface.height = surface_height(ssd->ds);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200357 surface.stride = -surface.width * 4;
Gerd Hoffmann869564a2010-04-13 09:05:03 +0200358 surface.mouse_mode = true;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200359 surface.flags = 0;
360 surface.type = 0;
Alon Levya13ccc92012-03-21 18:17:18 +0200361 surface.mem = (uintptr_t)ssd->buf;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200362 surface.group_id = MEMSLOT_GROUP_HOST;
Gerd Hoffmann7466bc42010-10-14 16:55:01 +0200363
Alon Levy5ff4e362011-07-20 12:20:58 +0300364 qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200365}
366
367void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
368{
Gerd Hoffmann35b21222013-10-17 12:11:43 +0200369 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200370
Alon Levy5ff4e362011-07-20 12:20:58 +0300371 qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200372}
373
Gerd Hoffmannc78f7132013-03-05 15:24:14 +0100374void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
Gerd Hoffmanna963f872011-07-20 12:20:51 +0300375{
Gerd Hoffmanna963f872011-07-20 12:20:51 +0300376 qemu_mutex_init(&ssd->lock);
Gerd Hoffmannb1af98b2012-09-05 08:25:08 +0200377 QTAILQ_INIT(&ssd->updates);
Gerd Hoffmanna963f872011-07-20 12:20:51 +0300378 ssd->mouse_x = -1;
379 ssd->mouse_y = -1;
Gerd Hoffmannddd8fdc2012-09-04 11:39:41 +0200380 if (ssd->num_surfaces == 0) {
381 ssd->num_surfaces = 1024;
382 }
Gerd Hoffmanna963f872011-07-20 12:20:51 +0300383}
384
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200385/* display listener callbacks */
386
387void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
388 int x, int y, int w, int h)
389{
390 QXLRect update_area;
391
Gerd Hoffmann35b21222013-10-17 12:11:43 +0200392 dprint(2, "%s/%d: x %d y %d w %d h %d\n", __func__,
393 ssd->qxl.id, x, y, w, h);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200394 update_area.left = x,
395 update_area.right = x + w;
396 update_area.top = y;
397 update_area.bottom = y + h;
398
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200399 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
400 ssd->notify++;
401 }
402 qemu_spice_rect_union(&ssd->dirty, &update_area);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200403}
404
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +0100405void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
406 DisplaySurface *surface)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200407{
Gerd Hoffmannb1af98b2012-09-05 08:25:08 +0200408 SimpleSpiceUpdate *update;
Gerd Hoffmann4b87dc42014-01-24 10:48:58 +0100409 bool need_destroy;
Gerd Hoffmannb1af98b2012-09-05 08:25:08 +0200410
Gerd Hoffmann555e72f2014-11-08 08:56:34 +0100411 if (surface && ssd->surface &&
412 surface_width(surface) == pixman_image_get_width(ssd->surface) &&
Gerd Hoffmannb2af43c2015-09-18 12:07:18 +0200413 surface_height(surface) == pixman_image_get_height(ssd->surface) &&
414 surface_format(surface) == pixman_image_get_format(ssd->surface)) {
Gerd Hoffmann555e72f2014-11-08 08:56:34 +0100415 /* no-resize fast path: just swap backing store */
416 dprint(1, "%s/%d: fast (%dx%d)\n", __func__, ssd->qxl.id,
417 surface_width(surface), surface_height(surface));
418 qemu_mutex_lock(&ssd->lock);
419 ssd->ds = surface;
420 pixman_image_unref(ssd->surface);
421 ssd->surface = pixman_image_ref(ssd->ds->image);
422 qemu_mutex_unlock(&ssd->lock);
423 qemu_spice_display_update(ssd, 0, 0,
424 surface_width(surface),
425 surface_height(surface));
426 return;
427 }
428
429 /* full mode switch */
430 dprint(1, "%s/%d: full (%dx%d -> %dx%d)\n", __func__, ssd->qxl.id,
431 ssd->surface ? pixman_image_get_width(ssd->surface) : 0,
432 ssd->surface ? pixman_image_get_height(ssd->surface) : 0,
433 surface ? surface_width(surface) : 0,
434 surface ? surface_height(surface) : 0);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200435
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200436 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
Gerd Hoffmannd9a86562012-11-02 09:12:49 +0100437 if (ssd->surface) {
438 pixman_image_unref(ssd->surface);
439 ssd->surface = NULL;
440 pixman_image_unref(ssd->mirror);
441 ssd->mirror = NULL;
442 }
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200443
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200444 qemu_mutex_lock(&ssd->lock);
Gerd Hoffmann4b87dc42014-01-24 10:48:58 +0100445 need_destroy = (ssd->ds != NULL);
Gerd Hoffmann71874c12013-02-28 16:42:28 +0100446 ssd->ds = surface;
Gerd Hoffmannb1af98b2012-09-05 08:25:08 +0200447 while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
448 QTAILQ_REMOVE(&ssd->updates, update, next);
449 qemu_spice_destroy_update(ssd, update);
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200450 }
451 qemu_mutex_unlock(&ssd->lock);
Gerd Hoffmann4b87dc42014-01-24 10:48:58 +0100452 if (need_destroy) {
453 qemu_spice_destroy_host_primary(ssd);
454 }
455 if (ssd->ds) {
Gerd Hoffmann51a09092015-01-15 12:06:16 +0100456 ssd->surface = pixman_image_ref(ssd->ds->image);
457 ssd->mirror = qemu_pixman_mirror_create(ssd->ds->format,
458 ssd->ds->image);
Gerd Hoffmann4b87dc42014-01-24 10:48:58 +0100459 qemu_spice_create_host_primary(ssd);
460 }
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200461
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200462 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
463 ssd->notify++;
Marc-André Lureau58c7b612015-12-08 17:08:10 +0100464
465 qemu_mutex_lock(&ssd->lock);
466 if (ssd->cursor) {
467 g_free(ssd->ptr_define);
468 ssd->ptr_define = qemu_spice_create_cursor_update(ssd, ssd->cursor, 0);
469 }
470 qemu_mutex_unlock(&ssd->lock);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200471}
472
Gerd Hoffmann0b2824e2014-11-04 13:59:59 +0100473static void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200474{
Gerd Hoffmann07536092011-04-27 15:50:32 +0200475 if (ssd->cursor) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100476 assert(ssd->dcl.con);
477 dpy_cursor_define(ssd->dcl.con, ssd->cursor);
Gerd Hoffmann07536092011-04-27 15:50:32 +0200478 }
479 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100480 assert(ssd->dcl.con);
481 dpy_mouse_set(ssd->dcl.con, ssd->mouse_x, ssd->mouse_y, 1);
Gerd Hoffmann07536092011-04-27 15:50:32 +0200482 ssd->mouse_x = -1;
483 ssd->mouse_y = -1;
484 }
Alon Levybb5a8cd2012-02-24 23:19:25 +0200485}
486
Gerd Hoffmann0b2824e2014-11-04 13:59:59 +0100487void qemu_spice_cursor_refresh_bh(void *opaque)
488{
489 SimpleSpiceDisplay *ssd = opaque;
490
491 qemu_mutex_lock(&ssd->lock);
492 qemu_spice_cursor_refresh_unlocked(ssd);
493 qemu_mutex_unlock(&ssd->lock);
494}
495
Alon Levybb5a8cd2012-02-24 23:19:25 +0200496void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
497{
Gerd Hoffmann35b21222013-10-17 12:11:43 +0200498 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
Gerd Hoffmann284d1c62013-03-15 15:45:54 +0100499 graphic_hw_update(ssd->dcl.con);
Alon Levybb5a8cd2012-02-24 23:19:25 +0200500
501 qemu_mutex_lock(&ssd->lock);
Gerd Hoffmann71874c12013-02-28 16:42:28 +0100502 if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
Gerd Hoffmannb1af98b2012-09-05 08:25:08 +0200503 qemu_spice_create_update(ssd);
Alon Levybb5a8cd2012-02-24 23:19:25 +0200504 ssd->notify++;
505 }
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200506 qemu_mutex_unlock(&ssd->lock);
507
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200508 if (ssd->notify) {
509 ssd->notify = 0;
Gerd Hoffmann5c59d112011-07-20 12:20:50 +0300510 qemu_spice_wakeup(ssd);
Gerd Hoffmann35b21222013-10-17 12:11:43 +0200511 dprint(2, "%s/%d: notify\n", __func__, ssd->qxl.id);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200512 }
513}
514
515/* spice display interface callbacks */
516
517static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
518{
519 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
520
Gerd Hoffmann35b21222013-10-17 12:11:43 +0200521 dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200522 ssd->worker = qxl_worker;
523}
524
525static void interface_set_compression_level(QXLInstance *sin, int level)
526{
Gerd Hoffmann35b21222013-10-17 12:11:43 +0200527 dprint(1, "%s/%d:\n", __func__, sin->id);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200528 /* nothing to do */
529}
530
John Snow015e02f2016-06-29 18:41:35 -0400531#if SPICE_NEEDS_SET_MM_TIME
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200532static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
533{
Gerd Hoffmann35b21222013-10-17 12:11:43 +0200534 dprint(3, "%s/%d:\n", __func__, sin->id);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200535 /* nothing to do */
536}
John Snow015e02f2016-06-29 18:41:35 -0400537#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200538
539static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
540{
541 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
542
543 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
544 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
545 info->num_memslots = NUM_MEMSLOTS;
546 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
547 info->internal_groupslot_id = 0;
Gerd Hoffmannab9509c2014-09-03 15:50:08 +0200548 info->qxl_ram_size = 16 * 1024 * 1024;
Gerd Hoffmannddd8fdc2012-09-04 11:39:41 +0200549 info->n_surfaces = ssd->num_surfaces;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200550}
551
Chih-Min Chaoc9f88ce2015-04-09 02:04:14 +0800552static int interface_get_command(QXLInstance *sin, QXLCommandExt *ext)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200553{
554 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
555 SimpleSpiceUpdate *update;
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200556 int ret = false;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200557
Gerd Hoffmann35b21222013-10-17 12:11:43 +0200558 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200559
560 qemu_mutex_lock(&ssd->lock);
Gerd Hoffmannb1af98b2012-09-05 08:25:08 +0200561 update = QTAILQ_FIRST(&ssd->updates);
562 if (update != NULL) {
563 QTAILQ_REMOVE(&ssd->updates, update, next);
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200564 *ext = update->ext;
565 ret = true;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200566 }
Gerd Hoffmanne0c64d02011-04-27 15:21:51 +0200567 qemu_mutex_unlock(&ssd->lock);
568
569 return ret;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200570}
571
572static int interface_req_cmd_notification(QXLInstance *sin)
573{
Gerd Hoffmann22672a32016-02-16 11:05:18 +0100574 dprint(2, "%s/%d:\n", __func__, sin->id);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200575 return 1;
576}
577
578static void interface_release_resource(QXLInstance *sin,
Chih-Min Chaoc9f88ce2015-04-09 02:04:14 +0800579 QXLReleaseInfoExt rext)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200580{
581 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200582 SimpleSpiceUpdate *update;
583 SimpleSpiceCursor *cursor;
584 QXLCommandExt *ext;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200585
Gerd Hoffmann35b21222013-10-17 12:11:43 +0200586 dprint(2, "%s/%d:\n", __func__, ssd->qxl.id);
Gerd Hoffmanne8e23b72014-06-20 08:12:44 +0200587 ext = (void *)(intptr_t)(rext.info->id);
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200588 switch (ext->cmd.type) {
589 case QXL_CMD_DRAW:
590 update = container_of(ext, SimpleSpiceUpdate, ext);
591 qemu_spice_destroy_update(ssd, update);
592 break;
593 case QXL_CMD_CURSOR:
594 cursor = container_of(ext, SimpleSpiceCursor, ext);
595 g_free(cursor);
596 break;
597 default:
598 g_assert_not_reached();
599 }
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200600}
601
Chih-Min Chaoc9f88ce2015-04-09 02:04:14 +0800602static int interface_get_cursor_command(QXLInstance *sin, QXLCommandExt *ext)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200603{
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200604 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
605 int ret;
606
607 dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
608
609 qemu_mutex_lock(&ssd->lock);
610 if (ssd->ptr_define) {
611 *ext = ssd->ptr_define->ext;
612 ssd->ptr_define = NULL;
613 ret = true;
614 } else if (ssd->ptr_move) {
615 *ext = ssd->ptr_move->ext;
616 ssd->ptr_move = NULL;
617 ret = true;
618 } else {
619 ret = false;
620 }
621 qemu_mutex_unlock(&ssd->lock);
622 return ret;
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200623}
624
625static int interface_req_cursor_notification(QXLInstance *sin)
626{
Gerd Hoffmann22672a32016-02-16 11:05:18 +0100627 dprint(2, "%s:\n", __func__);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200628 return 1;
629}
630
631static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
632{
633 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
634 abort();
635}
636
637static int interface_flush_resources(QXLInstance *sin)
638{
639 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
640 abort();
641 return 0;
642}
643
Gerd Hoffmann21a50d02012-11-21 14:41:48 +0100644static void interface_update_area_complete(QXLInstance *sin,
645 uint32_t surface_id,
646 QXLRect *dirty, uint32_t num_updated_rects)
647{
648 /* should never be called, used in qxl native mode only */
649 fprintf(stderr, "%s: abort()\n", __func__);
650 abort();
651}
652
653/* called from spice server thread context only */
654static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
655{
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200656 QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
657
658 switch (cookie->type) {
659#ifdef HAVE_SPICE_GL
660 case QXL_COOKIE_TYPE_GL_DRAW_DONE:
661 {
662 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
663 qemu_bh_schedule(ssd->gl_unblock_bh);
664 break;
665 }
Gerd Hoffmann39414ef2016-02-03 13:55:00 +0100666 case QXL_COOKIE_TYPE_IO:
667 if (cookie->io == QXL_IO_MONITORS_CONFIG_ASYNC) {
668 g_free(cookie->u.data);
669 }
670 break;
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200671#endif
672 default:
673 /* should never be called, used in qxl native mode only */
674 fprintf(stderr, "%s: abort()\n", __func__);
675 abort();
676 }
677 g_free(cookie);
Gerd Hoffmann21a50d02012-11-21 14:41:48 +0100678}
679
680static void interface_set_client_capabilities(QXLInstance *sin,
681 uint8_t client_present,
682 uint8_t caps[58])
683{
684 dprint(3, "%s:\n", __func__);
685}
686
687static int interface_client_monitors_config(QXLInstance *sin,
Gerd Hoffmann9b74d0d2014-01-24 18:47:20 +0100688 VDAgentMonitorsConfig *mc)
Gerd Hoffmann21a50d02012-11-21 14:41:48 +0100689{
Gerd Hoffmann9b74d0d2014-01-24 18:47:20 +0100690 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
691 QemuUIInfo info;
Marc-André Lureauc61d8122016-06-14 15:44:08 +0200692 int head;
Gerd Hoffmann5a9259a2015-03-13 12:21:50 +0100693
694 if (!dpy_ui_info_supported(ssd->dcl.con)) {
695 return 0; /* == not supported by guest */
696 }
Gerd Hoffmann9b74d0d2014-01-24 18:47:20 +0100697
Gerd Hoffmanndc491cf2014-04-07 12:15:44 +0200698 if (!mc) {
699 return 1;
700 }
701
Gerd Hoffmann9b74d0d2014-01-24 18:47:20 +0100702 memset(&info, 0, sizeof(info));
Marc-André Lureauc61d8122016-06-14 15:44:08 +0200703
704 head = qemu_console_get_head(ssd->dcl.con);
705 if (mc->num_of_monitors > head) {
706 info.width = mc->monitors[head].width;
707 info.height = mc->monitors[head].height;
Gerd Hoffmann9b74d0d2014-01-24 18:47:20 +0100708 }
Gerd Hoffmann5a9259a2015-03-13 12:21:50 +0100709 dpy_set_ui_info(ssd->dcl.con, &info);
710 dprint(1, "%s/%d: size %dx%d\n", __func__, ssd->qxl.id,
711 info.width, info.height);
712 return 1;
Gerd Hoffmann21a50d02012-11-21 14:41:48 +0100713}
714
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200715static const QXLInterface dpy_interface = {
716 .base.type = SPICE_INTERFACE_QXL,
717 .base.description = "qemu simple display",
718 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
719 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
720
721 .attache_worker = interface_attach_worker,
722 .set_compression_level = interface_set_compression_level,
John Snow015e02f2016-06-29 18:41:35 -0400723#if SPICE_NEEDS_SET_MM_TIME
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200724 .set_mm_time = interface_set_mm_time,
John Snow015e02f2016-06-29 18:41:35 -0400725#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200726 .get_init_info = interface_get_init_info,
727
728 /* the callbacks below are called from spice server thread context */
729 .get_command = interface_get_command,
730 .req_cmd_notification = interface_req_cmd_notification,
731 .release_resource = interface_release_resource,
732 .get_cursor_command = interface_get_cursor_command,
733 .req_cursor_notification = interface_req_cursor_notification,
734 .notify_update = interface_notify_update,
735 .flush_resources = interface_flush_resources,
Gerd Hoffmann21a50d02012-11-21 14:41:48 +0100736 .async_complete = interface_async_complete,
737 .update_area_complete = interface_update_area_complete,
738 .set_client_capabilities = interface_set_client_capabilities,
739 .client_monitors_config = interface_client_monitors_config,
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200740};
741
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100742static void display_update(DisplayChangeListener *dcl,
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100743 int x, int y, int w, int h)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200744{
Gerd Hoffmann9c80a312013-02-28 14:47:07 +0100745 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
746 qemu_spice_display_update(ssd, x, y, w, h);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200747}
748
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +0100749static void display_switch(DisplayChangeListener *dcl,
Chih-Min Chao9425c002015-04-09 02:04:13 +0800750 DisplaySurface *surface)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200751{
Gerd Hoffmann9c80a312013-02-28 14:47:07 +0100752 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
Gerd Hoffmannc12aeb82013-02-28 15:03:04 +0100753 qemu_spice_display_switch(ssd, surface);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200754}
755
Gerd Hoffmannbc2ed972013-03-01 13:03:04 +0100756static void display_refresh(DisplayChangeListener *dcl)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200757{
Gerd Hoffmann9c80a312013-02-28 14:47:07 +0100758 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
759 qemu_spice_display_refresh(ssd);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200760}
761
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200762static void display_mouse_set(DisplayChangeListener *dcl,
763 int x, int y, int on)
764{
765 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
766
767 qemu_mutex_lock(&ssd->lock);
768 ssd->ptr_x = x;
Marc-André Lureaud0df04a2015-03-24 17:50:11 +0100769 ssd->ptr_y = y;
Daniel P. Berrangeef1e1e02015-08-26 12:17:18 +0100770 g_free(ssd->ptr_move);
Marc-André Lureau700cd852015-03-24 17:50:13 +0100771 ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL, on);
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200772 qemu_mutex_unlock(&ssd->lock);
Marc-André Lureau51e0b652017-01-30 14:45:40 +0400773 qemu_spice_wakeup(ssd);
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200774}
775
776static void display_mouse_define(DisplayChangeListener *dcl,
777 QEMUCursor *c)
778{
779 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
780
781 qemu_mutex_lock(&ssd->lock);
Gonglei28f4a702016-05-12 17:57:08 +0800782 cursor_get(c);
Marc-André Lureau58c7b612015-12-08 17:08:10 +0100783 cursor_put(ssd->cursor);
784 ssd->cursor = c;
Marc-André Lureaudc8dcee2015-03-24 17:50:12 +0100785 ssd->hot_x = c->hot_x;
786 ssd->hot_y = c->hot_y;
Daniel P. Berrangeef1e1e02015-08-26 12:17:18 +0100787 g_free(ssd->ptr_move);
788 ssd->ptr_move = NULL;
789 g_free(ssd->ptr_define);
Marc-André Lureau700cd852015-03-24 17:50:13 +0100790 ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c, 0);
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200791 qemu_mutex_unlock(&ssd->lock);
Marc-André Lureau51e0b652017-01-30 14:45:40 +0400792 qemu_spice_wakeup(ssd);
Gerd Hoffmann5643fc02014-06-07 13:03:10 +0200793}
794
Gerd Hoffmann7c20b4a2012-11-13 14:51:41 +0100795static const DisplayChangeListenerOps display_listener_ops = {
Gerd Hoffmann0002a512015-01-09 09:31:58 +0100796 .dpy_name = "spice",
797 .dpy_gfx_update = display_update,
798 .dpy_gfx_switch = display_switch,
799 .dpy_gfx_check_format = qemu_pixman_check_format,
800 .dpy_refresh = display_refresh,
801 .dpy_mouse_set = display_mouse_set,
802 .dpy_cursor_define = display_mouse_define,
Gerd Hoffmanna3e22262010-08-25 15:32:06 +0200803};
804
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200805#ifdef HAVE_SPICE_GL
806
Gerd Hoffmann39414ef2016-02-03 13:55:00 +0100807static void qemu_spice_gl_monitor_config(SimpleSpiceDisplay *ssd,
808 int x, int y, int w, int h)
809{
810 QXLMonitorsConfig *config;
811 QXLCookie *cookie;
812
813 config = g_malloc0(sizeof(QXLMonitorsConfig) + sizeof(QXLHead));
814 config->count = 1;
815 config->max_allowed = 1;
816 config->heads[0].x = x;
817 config->heads[0].y = y;
818 config->heads[0].width = w;
819 config->heads[0].height = h;
820 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
821 QXL_IO_MONITORS_CONFIG_ASYNC);
822 cookie->u.data = config;
823
824 spice_qxl_monitors_config_async(&ssd->qxl,
825 (uintptr_t)config,
826 MEMSLOT_GROUP_HOST,
827 (uintptr_t)cookie);
828}
829
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200830static void qemu_spice_gl_block(SimpleSpiceDisplay *ssd, bool block)
831{
Gerd Hoffmann8e388e92016-02-19 07:46:47 +0100832 uint64_t timeout;
833
834 if (block) {
835 timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
836 timeout += 1000; /* one sec */
837 timer_mod(ssd->gl_unblock_timer, timeout);
838 } else {
839 timer_del(ssd->gl_unblock_timer);
840 }
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200841 graphic_hw_gl_block(ssd->dcl.con, block);
842}
843
844static void qemu_spice_gl_unblock_bh(void *opaque)
845{
846 SimpleSpiceDisplay *ssd = opaque;
847
848 qemu_spice_gl_block(ssd, false);
849}
850
Gerd Hoffmann8e388e92016-02-19 07:46:47 +0100851static void qemu_spice_gl_block_timer(void *opaque)
852{
Alistair Francis2ab4b132017-09-11 12:52:50 -0700853 warn_report("spice: no gl-draw-done within one second");
Gerd Hoffmann8e388e92016-02-19 07:46:47 +0100854}
855
Gerd Hoffmann44231842016-09-23 09:50:28 +0200856static void spice_gl_refresh(DisplayChangeListener *dcl)
857{
858 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
859 uint64_t cookie;
860
861 if (!ssd->ds || qemu_console_is_gl_blocked(ssd->dcl.con)) {
862 return;
863 }
864
865 graphic_hw_update(dcl->con);
866 if (ssd->gl_updates && ssd->have_surface) {
867 qemu_spice_gl_block(ssd, true);
868 cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
869 spice_qxl_gl_draw_async(&ssd->qxl, 0, 0,
870 surface_width(ssd->ds),
871 surface_height(ssd->ds),
872 cookie);
873 ssd->gl_updates = 0;
874 }
875}
876
877static void spice_gl_update(DisplayChangeListener *dcl,
878 int x, int y, int w, int h)
879{
880 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
881
882 surface_gl_update_texture(ssd->gls, ssd->ds, x, y, w, h);
883 ssd->gl_updates++;
884}
885
886static void spice_gl_switch(DisplayChangeListener *dcl,
887 struct DisplaySurface *new_surface)
888{
889 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
890 EGLint stride, fourcc;
891 int fd;
892
893 if (ssd->ds) {
894 surface_gl_destroy_texture(ssd->gls, ssd->ds);
895 }
896 ssd->ds = new_surface;
897 if (ssd->ds) {
898 surface_gl_create_texture(ssd->gls, ssd->ds);
899 fd = egl_get_fd_for_texture(ssd->ds->texture,
900 &stride, &fourcc);
901 if (fd < 0) {
902 surface_gl_destroy_texture(ssd->gls, ssd->ds);
903 return;
904 }
905
906 dprint(1, "%s: %dx%d (stride %d/%d, fourcc 0x%x)\n", __func__,
907 surface_width(ssd->ds), surface_height(ssd->ds),
908 surface_stride(ssd->ds), stride, fourcc);
909
910 /* note: spice server will close the fd */
911 spice_qxl_gl_scanout(&ssd->qxl, fd,
912 surface_width(ssd->ds),
913 surface_height(ssd->ds),
914 stride, fourcc, false);
915 ssd->have_surface = true;
916 ssd->have_scanout = false;
917
918 qemu_spice_gl_monitor_config(ssd, 0, 0,
919 surface_width(ssd->ds),
920 surface_height(ssd->ds));
921 }
922}
923
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200924static QEMUGLContext qemu_spice_gl_create_context(DisplayChangeListener *dcl,
925 QEMUGLParams *params)
926{
927 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
928 qemu_egl_rn_ctx);
929 return qemu_egl_create_context(dcl, params);
930}
931
Gerd Hoffmann46ffd0c2017-02-21 10:37:19 +0100932static void qemu_spice_gl_scanout_disable(DisplayChangeListener *dcl)
933{
934 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
935
936 dprint(1, "%s: no framebuffer\n", __func__);
937 spice_qxl_gl_scanout(&ssd->qxl, -1, 0, 0, 0, 0, false);
938 qemu_spice_gl_monitor_config(ssd, 0, 0, 0, 0);
939 ssd->have_surface = false;
940 ssd->have_scanout = false;
941}
942
Gerd Hoffmannf4c36bd2017-02-21 10:37:16 +0100943static void qemu_spice_gl_scanout_texture(DisplayChangeListener *dcl,
944 uint32_t tex_id,
945 bool y_0_top,
946 uint32_t backing_width,
947 uint32_t backing_height,
948 uint32_t x, uint32_t y,
949 uint32_t w, uint32_t h)
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200950{
951 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
952 EGLint stride = 0, fourcc = 0;
953 int fd = -1;
954
Gerd Hoffmann46ffd0c2017-02-21 10:37:19 +0100955 assert(tex_id);
956 fd = egl_get_fd_for_texture(tex_id, &stride, &fourcc);
957 if (fd < 0) {
958 fprintf(stderr, "%s: failed to get fd for texture\n", __func__);
959 return;
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200960 }
Gerd Hoffmann46ffd0c2017-02-21 10:37:19 +0100961 dprint(1, "%s: %dx%d (stride %d, fourcc 0x%x)\n", __func__,
962 w, h, stride, fourcc);
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200963
964 /* note: spice server will close the fd */
Marc-André Lureau9d8256e2016-06-14 15:44:09 +0200965 spice_qxl_gl_scanout(&ssd->qxl, fd, backing_width, backing_height,
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200966 stride, fourcc, y_0_top);
Gerd Hoffmann39414ef2016-02-03 13:55:00 +0100967 qemu_spice_gl_monitor_config(ssd, x, y, w, h);
Gerd Hoffmann46ffd0c2017-02-21 10:37:19 +0100968 ssd->have_surface = false;
969 ssd->have_scanout = true;
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200970}
971
972static void qemu_spice_gl_update(DisplayChangeListener *dcl,
973 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
974{
975 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
976 uint64_t cookie;
977
Gerd Hoffmann44231842016-09-23 09:50:28 +0200978 if (!ssd->have_scanout) {
979 return;
980 }
981
Gerd Hoffmann22672a32016-02-16 11:05:18 +0100982 dprint(2, "%s: %dx%d+%d+%d\n", __func__, w, h, x, y);
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200983 qemu_spice_gl_block(ssd, true);
984 cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
985 spice_qxl_gl_draw_async(&ssd->qxl, x, y, w, h, cookie);
986}
987
988static const DisplayChangeListenerOps display_listener_gl_ops = {
Gerd Hoffmann44231842016-09-23 09:50:28 +0200989 .dpy_name = "spice-egl",
990 .dpy_gfx_update = spice_gl_update,
991 .dpy_gfx_switch = spice_gl_switch,
992 .dpy_gfx_check_format = console_gl_check_format,
993 .dpy_refresh = spice_gl_refresh,
994 .dpy_mouse_set = display_mouse_set,
995 .dpy_cursor_define = display_mouse_define,
Gerd Hoffmann474114b2015-10-13 15:39:34 +0200996
997 .dpy_gl_ctx_create = qemu_spice_gl_create_context,
998 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
999 .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
1000 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
1001
Gerd Hoffmann46ffd0c2017-02-21 10:37:19 +01001002 .dpy_gl_scanout_disable = qemu_spice_gl_scanout_disable,
Gerd Hoffmannf4c36bd2017-02-21 10:37:16 +01001003 .dpy_gl_scanout_texture = qemu_spice_gl_scanout_texture,
Gerd Hoffmann474114b2015-10-13 15:39:34 +02001004 .dpy_gl_update = qemu_spice_gl_update,
1005};
1006
1007#endif /* HAVE_SPICE_GL */
1008
Gerd Hoffmann9fa03282013-10-11 22:39:59 +02001009static void qemu_spice_display_init_one(QemuConsole *con)
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02001010{
Gerd Hoffmann9c80a312013-02-28 14:47:07 +01001011 SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02001012
Gerd Hoffmannc78f7132013-03-05 15:24:14 +01001013 qemu_spice_display_init_common(ssd);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02001014
Gerd Hoffmannb5e751b2016-02-19 09:23:03 +01001015 ssd->dcl.ops = &display_listener_ops;
Gerd Hoffmann474114b2015-10-13 15:39:34 +02001016#ifdef HAVE_SPICE_GL
Gerd Hoffmannfe5c44f2017-06-06 13:06:18 +02001017 if (spice_opengl) {
Gerd Hoffmann474114b2015-10-13 15:39:34 +02001018 ssd->dcl.ops = &display_listener_gl_ops;
Gerd Hoffmann474114b2015-10-13 15:39:34 +02001019 ssd->gl_unblock_bh = qemu_bh_new(qemu_spice_gl_unblock_bh, ssd);
Gerd Hoffmann8e388e92016-02-19 07:46:47 +01001020 ssd->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1021 qemu_spice_gl_block_timer, ssd);
Gerd Hoffmann46e19e12017-10-10 15:54:49 +02001022 ssd->gls = qemu_gl_init_shader();
Gerd Hoffmann44231842016-09-23 09:50:28 +02001023 ssd->have_surface = false;
1024 ssd->have_scanout = false;
Gerd Hoffmann474114b2015-10-13 15:39:34 +02001025 }
1026#endif
Gerd Hoffmannb5e751b2016-02-19 09:23:03 +01001027 ssd->dcl.con = con;
1028
Gerd Hoffmann9c80a312013-02-28 14:47:07 +01001029 ssd->qxl.base.sif = &dpy_interface.base;
Gerd Hoffmann9fa03282013-10-11 22:39:59 +02001030 qemu_spice_add_display_interface(&ssd->qxl, con);
Gerd Hoffmann9c80a312013-02-28 14:47:07 +01001031 assert(ssd->worker);
Gerd Hoffmann9c80a312013-02-28 14:47:07 +01001032 qemu_spice_create_host_memslot(ssd);
Gerd Hoffmann9c80a312013-02-28 14:47:07 +01001033
Gerd Hoffmann52090892013-04-23 15:44:31 +02001034 register_displaychangelistener(&ssd->dcl);
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02001035}
Gerd Hoffmann9fa03282013-10-11 22:39:59 +02001036
1037void qemu_spice_display_init(void)
1038{
Gerd Hoffmann8bf69b42017-02-21 08:57:37 +01001039 QemuOptsList *olist = qemu_find_opts("spice");
1040 QemuOpts *opts = QTAILQ_FIRST(&olist->head);
1041 QemuConsole *spice_con, *con;
1042 const char *str;
Gerd Hoffmann9fa03282013-10-11 22:39:59 +02001043 int i;
1044
Gerd Hoffmann8bf69b42017-02-21 08:57:37 +01001045 str = qemu_opt_get(opts, "display");
1046 if (str) {
1047 int head = qemu_opt_get_number(opts, "head", 0);
1048 Error *err = NULL;
1049
1050 spice_con = qemu_console_lookup_by_device_name(str, head, &err);
1051 if (err) {
1052 error_report("Failed to lookup display/head");
1053 exit(1);
1054 }
1055 } else {
1056 spice_con = NULL;
1057 }
1058
Gerd Hoffmann9fa03282013-10-11 22:39:59 +02001059 for (i = 0;; i++) {
1060 con = qemu_console_lookup_by_index(i);
1061 if (!con || !qemu_console_is_graphic(con)) {
1062 break;
1063 }
1064 if (qemu_spice_have_display_interface(con)) {
1065 continue;
1066 }
Gerd Hoffmann8bf69b42017-02-21 08:57:37 +01001067 if (spice_con != NULL && spice_con != con) {
1068 continue;
1069 }
Gerd Hoffmann9fa03282013-10-11 22:39:59 +02001070 qemu_spice_display_init_one(con);
1071 }
1072}