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