]> git.proxmox.com Git - qemu.git/blame - ui/spice-display.c
spice: catch spice server initialization failures.
[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
18#include <pthread.h>
19
20#include "qemu-common.h"
21#include "qemu-spice.h"
22#include "qemu-timer.h"
23#include "qemu-queue.h"
24#include "monitor.h"
25#include "console.h"
26#include "sysemu.h"
27
28#include "spice-display.h"
29
30static int debug = 0;
31
2c80e423 32static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
a3e22260
GH
33{
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
e0c64d08 65static SimpleSpiceUpdate *qemu_spice_create_update(SimpleSpiceDisplay *ssd)
a3e22260
GH
66{
67 SimpleSpiceUpdate *update;
68 QXLDrawable *drawable;
69 QXLImage *image;
70 QXLCommand *cmd;
71 uint8_t *src, *dst;
72 int by, bw, bh;
73
74 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
75 return NULL;
76 };
77
a3e22260
GH
78 dprint(2, "%s: lr %d -> %d, tb -> %d -> %d\n", __FUNCTION__,
79 ssd->dirty.left, ssd->dirty.right,
80 ssd->dirty.top, ssd->dirty.bottom);
81
82 update = qemu_mallocz(sizeof(*update));
83 drawable = &update->drawable;
84 image = &update->image;
85 cmd = &update->ext.cmd;
86
87 bw = ssd->dirty.right - ssd->dirty.left;
88 bh = ssd->dirty.bottom - ssd->dirty.top;
89 update->bitmap = qemu_malloc(bw * bh * 4);
90
91 drawable->bbox = ssd->dirty;
92 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
93 drawable->effect = QXL_EFFECT_OPAQUE;
94 drawable->release_info.id = (intptr_t)update;
95 drawable->type = QXL_DRAW_COPY;
96 drawable->surfaces_dest[0] = -1;
97 drawable->surfaces_dest[1] = -1;
98 drawable->surfaces_dest[2] = -1;
99
100 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
101 drawable->u.copy.src_bitmap = (intptr_t)image;
102 drawable->u.copy.src_area.right = bw;
103 drawable->u.copy.src_area.bottom = bh;
104
105 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
106 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
107 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
108 image->bitmap.stride = bw * 4;
109 image->descriptor.width = image->bitmap.x = bw;
110 image->descriptor.height = image->bitmap.y = bh;
111 image->bitmap.data = (intptr_t)(update->bitmap);
112 image->bitmap.palette = 0;
113 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
114
115 if (ssd->conv == NULL) {
116 PixelFormat dst = qemu_default_pixelformat(32);
117 ssd->conv = qemu_pf_conv_get(&dst, &ssd->ds->surface->pf);
118 assert(ssd->conv);
119 }
120
121 src = ds_get_data(ssd->ds) +
122 ssd->dirty.top * ds_get_linesize(ssd->ds) +
123 ssd->dirty.left * ds_get_bytes_per_pixel(ssd->ds);
124 dst = update->bitmap;
125 for (by = 0; by < bh; by++) {
126 qemu_pf_conv_run(ssd->conv, dst, src, bw);
127 src += ds_get_linesize(ssd->ds);
128 dst += image->bitmap.stride;
129 }
130
131 cmd->type = QXL_CMD_DRAW;
132 cmd->data = (intptr_t)drawable;
133
134 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
a3e22260
GH
135 return update;
136}
137
138/*
139 * Called from spice server thread context (via interface_release_ressource)
140 * We do *not* hold the global qemu mutex here, so extra care is needed
141 * when calling qemu functions. Qemu interfaces used:
142 * - qemu_free (underlying glibc free is re-entrant).
143 */
144void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
145{
146 qemu_free(update->bitmap);
147 qemu_free(update);
148}
149
150void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
151{
152 QXLDevMemSlot memslot;
153
154 dprint(1, "%s:\n", __FUNCTION__);
155
156 memset(&memslot, 0, sizeof(memslot));
157 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
158 memslot.virt_end = ~0;
159 ssd->worker->add_memslot(ssd->worker, &memslot);
160}
161
162void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
163{
164 QXLDevSurfaceCreate surface;
165
166 dprint(1, "%s: %dx%d\n", __FUNCTION__,
167 ds_get_width(ssd->ds), ds_get_height(ssd->ds));
168
169 surface.format = SPICE_SURFACE_FMT_32_xRGB;
170 surface.width = ds_get_width(ssd->ds);
171 surface.height = ds_get_height(ssd->ds);
172 surface.stride = -surface.width * 4;
869564a9 173 surface.mouse_mode = true;
a3e22260
GH
174 surface.flags = 0;
175 surface.type = 0;
176 surface.mem = (intptr_t)ssd->buf;
177 surface.group_id = MEMSLOT_GROUP_HOST;
7466bc49 178
a3e22260
GH
179 ssd->worker->create_primary_surface(ssd->worker, 0, &surface);
180}
181
182void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
183{
184 dprint(1, "%s:\n", __FUNCTION__);
185
186 ssd->worker->destroy_primary_surface(ssd->worker, 0);
187}
188
189void qemu_spice_vm_change_state_handler(void *opaque, int running, int reason)
190{
191 SimpleSpiceDisplay *ssd = opaque;
192
193 if (running) {
194 ssd->worker->start(ssd->worker);
195 } else {
196 ssd->worker->stop(ssd->worker);
197 }
198 ssd->running = running;
199}
200
201/* display listener callbacks */
202
203void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
204 int x, int y, int w, int h)
205{
206 QXLRect update_area;
207
208 dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__, x, y, w, h);
209 update_area.left = x,
210 update_area.right = x + w;
211 update_area.top = y;
212 update_area.bottom = y + h;
213
a3e22260
GH
214 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
215 ssd->notify++;
216 }
217 qemu_spice_rect_union(&ssd->dirty, &update_area);
a3e22260
GH
218}
219
220void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
221{
222 dprint(1, "%s:\n", __FUNCTION__);
223
a3e22260
GH
224 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
225 qemu_pf_conv_put(ssd->conv);
226 ssd->conv = NULL;
a3e22260 227
e0c64d08
GH
228 qemu_mutex_lock(&ssd->lock);
229 if (ssd->update != NULL) {
230 qemu_spice_destroy_update(ssd, ssd->update);
231 ssd->update = NULL;
232 }
233 qemu_mutex_unlock(&ssd->lock);
a3e22260
GH
234 qemu_spice_destroy_host_primary(ssd);
235 qemu_spice_create_host_primary(ssd);
236
a3e22260
GH
237 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
238 ssd->notify++;
a3e22260
GH
239}
240
241void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
242{
243 dprint(3, "%s:\n", __FUNCTION__);
244 vga_hw_update();
e0c64d08
GH
245
246 qemu_mutex_lock(&ssd->lock);
247 if (ssd->update == NULL) {
248 ssd->update = qemu_spice_create_update(ssd);
249 ssd->notify++;
250 }
07536094
GH
251 if (ssd->cursor) {
252 ssd->ds->cursor_define(ssd->cursor);
253 cursor_put(ssd->cursor);
254 ssd->cursor = NULL;
255 }
256 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
257 ssd->ds->mouse_set(ssd->mouse_x, ssd->mouse_y, 1);
258 ssd->mouse_x = -1;
259 ssd->mouse_y = -1;
260 }
e0c64d08
GH
261 qemu_mutex_unlock(&ssd->lock);
262
a3e22260
GH
263 if (ssd->notify) {
264 ssd->notify = 0;
265 ssd->worker->wakeup(ssd->worker);
266 dprint(2, "%s: notify\n", __FUNCTION__);
267 }
268}
269
270/* spice display interface callbacks */
271
272static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
273{
274 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
275
276 dprint(1, "%s:\n", __FUNCTION__);
277 ssd->worker = qxl_worker;
278}
279
280static void interface_set_compression_level(QXLInstance *sin, int level)
281{
282 dprint(1, "%s:\n", __FUNCTION__);
283 /* nothing to do */
284}
285
286static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
287{
288 dprint(3, "%s:\n", __FUNCTION__);
289 /* nothing to do */
290}
291
292static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
293{
294 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
295
296 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
297 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
298 info->num_memslots = NUM_MEMSLOTS;
299 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
300 info->internal_groupslot_id = 0;
301 info->qxl_ram_size = ssd->bufsize;
302 info->n_surfaces = NUM_SURFACES;
303}
304
305static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
306{
307 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
308 SimpleSpiceUpdate *update;
e0c64d08 309 int ret = false;
a3e22260
GH
310
311 dprint(3, "%s:\n", __FUNCTION__);
e0c64d08
GH
312
313 qemu_mutex_lock(&ssd->lock);
314 if (ssd->update != NULL) {
315 update = ssd->update;
316 ssd->update = NULL;
317 *ext = update->ext;
318 ret = true;
a3e22260 319 }
e0c64d08
GH
320 qemu_mutex_unlock(&ssd->lock);
321
322 return ret;
a3e22260
GH
323}
324
325static int interface_req_cmd_notification(QXLInstance *sin)
326{
327 dprint(1, "%s:\n", __FUNCTION__);
328 return 1;
329}
330
331static void interface_release_resource(QXLInstance *sin,
332 struct QXLReleaseInfoExt ext)
333{
334 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
335 uintptr_t id;
336
337 dprint(2, "%s:\n", __FUNCTION__);
338 id = ext.info->id;
339 qemu_spice_destroy_update(ssd, (void*)id);
340}
341
342static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
343{
344 dprint(3, "%s:\n", __FUNCTION__);
345 return false;
346}
347
348static int interface_req_cursor_notification(QXLInstance *sin)
349{
350 dprint(1, "%s:\n", __FUNCTION__);
351 return 1;
352}
353
354static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
355{
356 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
357 abort();
358}
359
360static int interface_flush_resources(QXLInstance *sin)
361{
362 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
363 abort();
364 return 0;
365}
366
367static const QXLInterface dpy_interface = {
368 .base.type = SPICE_INTERFACE_QXL,
369 .base.description = "qemu simple display",
370 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
371 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
372
373 .attache_worker = interface_attach_worker,
374 .set_compression_level = interface_set_compression_level,
375 .set_mm_time = interface_set_mm_time,
376 .get_init_info = interface_get_init_info,
377
378 /* the callbacks below are called from spice server thread context */
379 .get_command = interface_get_command,
380 .req_cmd_notification = interface_req_cmd_notification,
381 .release_resource = interface_release_resource,
382 .get_cursor_command = interface_get_cursor_command,
383 .req_cursor_notification = interface_req_cursor_notification,
384 .notify_update = interface_notify_update,
385 .flush_resources = interface_flush_resources,
386};
387
388static SimpleSpiceDisplay sdpy;
389
390static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
391{
392 qemu_spice_display_update(&sdpy, x, y, w, h);
393}
394
395static void display_resize(struct DisplayState *ds)
396{
397 qemu_spice_display_resize(&sdpy);
398}
399
400static void display_refresh(struct DisplayState *ds)
401{
402 qemu_spice_display_refresh(&sdpy);
403}
404
405static DisplayChangeListener display_listener = {
406 .dpy_update = display_update,
407 .dpy_resize = display_resize,
408 .dpy_refresh = display_refresh,
409};
410
411void qemu_spice_display_init(DisplayState *ds)
412{
413 assert(sdpy.ds == NULL);
414 sdpy.ds = ds;
e0c64d08 415 qemu_mutex_init(&sdpy.lock);
07536094
GH
416 sdpy.mouse_x = -1;
417 sdpy.mouse_y = -1;
a3e22260
GH
418 sdpy.bufsize = (16 * 1024 * 1024);
419 sdpy.buf = qemu_malloc(sdpy.bufsize);
a3e22260
GH
420 register_displaychangelistener(ds, &display_listener);
421
422 sdpy.qxl.base.sif = &dpy_interface.base;
423 qemu_spice_add_interface(&sdpy.qxl.base);
424 assert(sdpy.worker);
425
426 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler, &sdpy);
427 qemu_spice_create_host_memslot(&sdpy);
428 qemu_spice_create_host_primary(&sdpy);
429}