]> git.proxmox.com Git - qemu.git/blame - ui/spice-display.c
main-loop: disable fd_set-based glib integration under w32
[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
GH
18#include "qemu-common.h"
19#include "qemu-spice.h"
20#include "qemu-timer.h"
21#include "qemu-queue.h"
22#include "monitor.h"
23#include "console.h"
24#include "sysemu.h"
c480bb7d 25#include "trace.h"
a3e22260
GH
26
27#include "spice-display.h"
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
AL
85 } else {
86 ssd->worker->add_memslot(ssd->worker, memslot);
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);
5c59d118
GH
93 ssd->worker->del_memslot(ssd->worker, gid, sid);
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
AL
105 } else {
106 ssd->worker->create_primary_surface(ssd->worker, id, surface);
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
AL
118 } else {
119 ssd->worker->destroy_primary_surface(ssd->worker, id);
120 }
5c59d118
GH
121}
122
5c59d118
GH
123void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
124{
c480bb7d 125 trace_qemu_spice_wakeup(ssd->qxl.id);
5c59d118
GH
126 ssd->worker->wakeup(ssd->worker);
127}
128
5c59d118
GH
129void qemu_spice_start(SimpleSpiceDisplay *ssd)
130{
c480bb7d 131 trace_qemu_spice_start(ssd->qxl.id);
5c59d118
GH
132 ssd->worker->start(ssd->worker);
133}
134
135void qemu_spice_stop(SimpleSpiceDisplay *ssd)
136{
c480bb7d 137 trace_qemu_spice_stop(ssd->qxl.id);
5c59d118
GH
138 ssd->worker->stop(ssd->worker);
139}
140
e0c64d08 141static SimpleSpiceUpdate *qemu_spice_create_update(SimpleSpiceDisplay *ssd)
a3e22260
GH
142{
143 SimpleSpiceUpdate *update;
144 QXLDrawable *drawable;
145 QXLImage *image;
146 QXLCommand *cmd;
147 uint8_t *src, *dst;
148 int by, bw, bh;
22795174 149 struct timespec time_space;
a3e22260
GH
150
151 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
152 return NULL;
153 };
154
c480bb7d 155 trace_qemu_spice_create_update(
a3e22260
GH
156 ssd->dirty.left, ssd->dirty.right,
157 ssd->dirty.top, ssd->dirty.bottom);
158
7267c094 159 update = g_malloc0(sizeof(*update));
a3e22260
GH
160 drawable = &update->drawable;
161 image = &update->image;
162 cmd = &update->ext.cmd;
163
164 bw = ssd->dirty.right - ssd->dirty.left;
165 bh = ssd->dirty.bottom - ssd->dirty.top;
7267c094 166 update->bitmap = g_malloc(bw * bh * 4);
a3e22260
GH
167
168 drawable->bbox = ssd->dirty;
169 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
170 drawable->effect = QXL_EFFECT_OPAQUE;
a13ccc99 171 drawable->release_info.id = (uintptr_t)update;
a3e22260
GH
172 drawable->type = QXL_DRAW_COPY;
173 drawable->surfaces_dest[0] = -1;
174 drawable->surfaces_dest[1] = -1;
175 drawable->surfaces_dest[2] = -1;
22795174
AL
176 clock_gettime(CLOCK_MONOTONIC, &time_space);
177 /* time in milliseconds from epoch. */
178 drawable->mm_time = time_space.tv_sec * 1000
179 + time_space.tv_nsec / 1000 / 1000;
a3e22260
GH
180
181 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
a13ccc99 182 drawable->u.copy.src_bitmap = (uintptr_t)image;
a3e22260
GH
183 drawable->u.copy.src_area.right = bw;
184 drawable->u.copy.src_area.bottom = bh;
185
186 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
187 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
188 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
189 image->bitmap.stride = bw * 4;
190 image->descriptor.width = image->bitmap.x = bw;
191 image->descriptor.height = image->bitmap.y = bh;
a13ccc99 192 image->bitmap.data = (uintptr_t)(update->bitmap);
a3e22260
GH
193 image->bitmap.palette = 0;
194 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
195
196 if (ssd->conv == NULL) {
197 PixelFormat dst = qemu_default_pixelformat(32);
198 ssd->conv = qemu_pf_conv_get(&dst, &ssd->ds->surface->pf);
199 assert(ssd->conv);
200 }
201
202 src = ds_get_data(ssd->ds) +
203 ssd->dirty.top * ds_get_linesize(ssd->ds) +
204 ssd->dirty.left * ds_get_bytes_per_pixel(ssd->ds);
205 dst = update->bitmap;
206 for (by = 0; by < bh; by++) {
207 qemu_pf_conv_run(ssd->conv, dst, src, bw);
208 src += ds_get_linesize(ssd->ds);
209 dst += image->bitmap.stride;
210 }
211
212 cmd->type = QXL_CMD_DRAW;
a13ccc99 213 cmd->data = (uintptr_t)drawable;
a3e22260
GH
214
215 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
a3e22260
GH
216 return update;
217}
218
219/*
220 * Called from spice server thread context (via interface_release_ressource)
221 * We do *not* hold the global qemu mutex here, so extra care is needed
222 * when calling qemu functions. Qemu interfaces used:
7267c094 223 * - g_free (underlying glibc free is re-entrant).
a3e22260
GH
224 */
225void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
226{
7267c094
AL
227 g_free(update->bitmap);
228 g_free(update);
a3e22260
GH
229}
230
231void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
232{
233 QXLDevMemSlot memslot;
234
235 dprint(1, "%s:\n", __FUNCTION__);
236
237 memset(&memslot, 0, sizeof(memslot));
238 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
239 memslot.virt_end = ~0;
5ff4e36c 240 qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
a3e22260
GH
241}
242
243void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
244{
245 QXLDevSurfaceCreate surface;
246
247 dprint(1, "%s: %dx%d\n", __FUNCTION__,
248 ds_get_width(ssd->ds), ds_get_height(ssd->ds));
249
250 surface.format = SPICE_SURFACE_FMT_32_xRGB;
251 surface.width = ds_get_width(ssd->ds);
252 surface.height = ds_get_height(ssd->ds);
253 surface.stride = -surface.width * 4;
869564a9 254 surface.mouse_mode = true;
a3e22260
GH
255 surface.flags = 0;
256 surface.type = 0;
a13ccc99 257 surface.mem = (uintptr_t)ssd->buf;
a3e22260 258 surface.group_id = MEMSLOT_GROUP_HOST;
7466bc49 259
5ff4e36c 260 qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
a3e22260
GH
261}
262
263void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
264{
265 dprint(1, "%s:\n", __FUNCTION__);
266
5ff4e36c 267 qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
a3e22260
GH
268}
269
1dfb4dd9
LC
270void qemu_spice_vm_change_state_handler(void *opaque, int running,
271 RunState state)
a3e22260
GH
272{
273 SimpleSpiceDisplay *ssd = opaque;
274
275 if (running) {
7e79cf40 276 ssd->running = true;
5c59d118 277 qemu_spice_start(ssd);
a3e22260 278 } else {
5c59d118 279 qemu_spice_stop(ssd);
7e79cf40 280 ssd->running = false;
a3e22260 281 }
a3e22260
GH
282}
283
a963f876
GH
284void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd, DisplayState *ds)
285{
286 ssd->ds = ds;
287 qemu_mutex_init(&ssd->lock);
288 ssd->mouse_x = -1;
289 ssd->mouse_y = -1;
290 ssd->bufsize = (16 * 1024 * 1024);
7267c094 291 ssd->buf = g_malloc(ssd->bufsize);
a963f876
GH
292}
293
a3e22260
GH
294/* display listener callbacks */
295
296void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
297 int x, int y, int w, int h)
298{
299 QXLRect update_area;
300
301 dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__, x, y, w, h);
302 update_area.left = x,
303 update_area.right = x + w;
304 update_area.top = y;
305 update_area.bottom = y + h;
306
a3e22260
GH
307 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
308 ssd->notify++;
309 }
310 qemu_spice_rect_union(&ssd->dirty, &update_area);
a3e22260
GH
311}
312
313void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
314{
315 dprint(1, "%s:\n", __FUNCTION__);
316
a3e22260
GH
317 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
318 qemu_pf_conv_put(ssd->conv);
319 ssd->conv = NULL;
a3e22260 320
e0c64d08
GH
321 qemu_mutex_lock(&ssd->lock);
322 if (ssd->update != NULL) {
323 qemu_spice_destroy_update(ssd, ssd->update);
324 ssd->update = NULL;
325 }
326 qemu_mutex_unlock(&ssd->lock);
a3e22260
GH
327 qemu_spice_destroy_host_primary(ssd);
328 qemu_spice_create_host_primary(ssd);
329
a3e22260
GH
330 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
331 ssd->notify++;
a3e22260
GH
332}
333
bb5a8cd5 334void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
a3e22260 335{
07536094
GH
336 if (ssd->cursor) {
337 ssd->ds->cursor_define(ssd->cursor);
338 cursor_put(ssd->cursor);
339 ssd->cursor = NULL;
340 }
341 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
342 ssd->ds->mouse_set(ssd->mouse_x, ssd->mouse_y, 1);
343 ssd->mouse_x = -1;
344 ssd->mouse_y = -1;
345 }
bb5a8cd5
AL
346}
347
348void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
349{
350 dprint(3, "%s:\n", __func__);
351 vga_hw_update();
352
353 qemu_mutex_lock(&ssd->lock);
354 if (ssd->update == NULL) {
355 ssd->update = qemu_spice_create_update(ssd);
356 ssd->notify++;
357 }
358 qemu_spice_cursor_refresh_unlocked(ssd);
e0c64d08
GH
359 qemu_mutex_unlock(&ssd->lock);
360
a3e22260
GH
361 if (ssd->notify) {
362 ssd->notify = 0;
5c59d118 363 qemu_spice_wakeup(ssd);
a3e22260
GH
364 dprint(2, "%s: notify\n", __FUNCTION__);
365 }
366}
367
368/* spice display interface callbacks */
369
370static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
371{
372 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
373
374 dprint(1, "%s:\n", __FUNCTION__);
375 ssd->worker = qxl_worker;
376}
377
378static void interface_set_compression_level(QXLInstance *sin, int level)
379{
380 dprint(1, "%s:\n", __FUNCTION__);
381 /* nothing to do */
382}
383
384static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
385{
386 dprint(3, "%s:\n", __FUNCTION__);
387 /* nothing to do */
388}
389
390static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
391{
392 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
393
394 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
395 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
396 info->num_memslots = NUM_MEMSLOTS;
397 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
398 info->internal_groupslot_id = 0;
399 info->qxl_ram_size = ssd->bufsize;
400 info->n_surfaces = NUM_SURFACES;
401}
402
403static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
404{
405 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
406 SimpleSpiceUpdate *update;
e0c64d08 407 int ret = false;
a3e22260
GH
408
409 dprint(3, "%s:\n", __FUNCTION__);
e0c64d08
GH
410
411 qemu_mutex_lock(&ssd->lock);
412 if (ssd->update != NULL) {
413 update = ssd->update;
414 ssd->update = NULL;
415 *ext = update->ext;
416 ret = true;
a3e22260 417 }
e0c64d08
GH
418 qemu_mutex_unlock(&ssd->lock);
419
420 return ret;
a3e22260
GH
421}
422
423static int interface_req_cmd_notification(QXLInstance *sin)
424{
425 dprint(1, "%s:\n", __FUNCTION__);
426 return 1;
427}
428
429static void interface_release_resource(QXLInstance *sin,
430 struct QXLReleaseInfoExt ext)
431{
432 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
433 uintptr_t id;
434
435 dprint(2, "%s:\n", __FUNCTION__);
436 id = ext.info->id;
437 qemu_spice_destroy_update(ssd, (void*)id);
438}
439
440static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
441{
442 dprint(3, "%s:\n", __FUNCTION__);
443 return false;
444}
445
446static int interface_req_cursor_notification(QXLInstance *sin)
447{
448 dprint(1, "%s:\n", __FUNCTION__);
449 return 1;
450}
451
452static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
453{
454 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
455 abort();
456}
457
458static int interface_flush_resources(QXLInstance *sin)
459{
460 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
461 abort();
462 return 0;
463}
464
465static const QXLInterface dpy_interface = {
466 .base.type = SPICE_INTERFACE_QXL,
467 .base.description = "qemu simple display",
468 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
469 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
470
471 .attache_worker = interface_attach_worker,
472 .set_compression_level = interface_set_compression_level,
473 .set_mm_time = interface_set_mm_time,
474 .get_init_info = interface_get_init_info,
475
476 /* the callbacks below are called from spice server thread context */
477 .get_command = interface_get_command,
478 .req_cmd_notification = interface_req_cmd_notification,
479 .release_resource = interface_release_resource,
480 .get_cursor_command = interface_get_cursor_command,
481 .req_cursor_notification = interface_req_cursor_notification,
482 .notify_update = interface_notify_update,
483 .flush_resources = interface_flush_resources,
484};
485
486static SimpleSpiceDisplay sdpy;
487
488static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
489{
490 qemu_spice_display_update(&sdpy, x, y, w, h);
491}
492
493static void display_resize(struct DisplayState *ds)
494{
495 qemu_spice_display_resize(&sdpy);
496}
497
498static void display_refresh(struct DisplayState *ds)
499{
500 qemu_spice_display_refresh(&sdpy);
501}
502
503static DisplayChangeListener display_listener = {
504 .dpy_update = display_update,
505 .dpy_resize = display_resize,
506 .dpy_refresh = display_refresh,
507};
508
509void qemu_spice_display_init(DisplayState *ds)
510{
511 assert(sdpy.ds == NULL);
a963f876 512 qemu_spice_display_init_common(&sdpy, ds);
a3e22260
GH
513 register_displaychangelistener(ds, &display_listener);
514
515 sdpy.qxl.base.sif = &dpy_interface.base;
516 qemu_spice_add_interface(&sdpy.qxl.base);
517 assert(sdpy.worker);
518
519 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler, &sdpy);
520 qemu_spice_create_host_memslot(&sdpy);
521 qemu_spice_create_host_primary(&sdpy);
522}