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