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