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