spice: add tablet support
[qemu/kraxel.git] / spice-display.c
blob2d9ebd7300151c8ce9382879a69dfb3bb2b76e19
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <stdint.h>
5 #include <string.h>
6 #include <pthread.h>
8 #include "qemu-common.h"
9 #include "qemu-spice.h"
10 #include "qemu-timer.h"
11 #include "qemu-queue.h"
12 #include "monitor.h"
13 #include "console.h"
14 #include "sysemu.h"
16 #include "spice-display.h"
18 static int debug = 1;
20 int qemu_spice_rect_is_empty(const SpiceRect* r)
22 return r->top == r->bottom || r->left == r->right;
25 void qemu_spice_rect_union(SpiceRect *dest, const SpiceRect *r)
27 if (qemu_spice_rect_is_empty(r)) {
28 return;
31 if (qemu_spice_rect_is_empty(dest)) {
32 *dest = *r;
33 return;
36 dest->top = MIN(dest->top, r->top);
37 dest->left = MIN(dest->left, r->left);
38 dest->bottom = MAX(dest->bottom, r->bottom);
39 dest->right = MAX(dest->right, r->right);
42 SimpleSpiceUpdate *qemu_spice_create_update(SimpleSpiceDisplay *ssd)
44 SimpleSpiceUpdate *update;
45 QXLDrawable *drawable;
46 QXLImage *image;
47 QXLCommand *cmd;
48 uint8_t *src, *dst;
49 int by, bw, bh;
51 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
52 return NULL;
55 pthread_mutex_lock(&ssd->lock);
56 if (debug > 1)
57 fprintf(stderr, "%s: lr %d -> %d, tb -> %d -> %d\n", __FUNCTION__,
58 ssd->dirty.left, ssd->dirty.right,
59 ssd->dirty.top, ssd->dirty.bottom);
61 update = qemu_mallocz(sizeof(*update));
62 drawable = &update->drawable;
63 image = &update->image;
64 cmd = &update->ext.cmd;
66 bw = ssd->dirty.right - ssd->dirty.left;
67 bh = ssd->dirty.bottom - ssd->dirty.top;
68 update->bitmap = qemu_malloc(bw * bh * 4);
70 drawable->bbox = ssd->dirty;
71 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
72 drawable->effect = QXL_EFFECT_OPAQUE;
73 drawable->release_info.id = (intptr_t)update;
74 drawable->type = QXL_DRAW_COPY;
76 drawable->u.copy.rop_decriptor = SPICE_ROPD_OP_PUT;
77 drawable->u.copy.src_bitmap = (intptr_t)image;
78 drawable->u.copy.src_area.right = bw;
79 drawable->u.copy.src_area.bottom = bh;
81 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
82 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
83 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
84 image->bitmap.stride = bw * 4;
85 image->descriptor.width = image->bitmap.x = bw;
86 image->descriptor.height = image->bitmap.y = bh;
87 image->bitmap.data = (intptr_t)(update->bitmap);
88 image->bitmap.palette = 0;
89 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
91 if (ssd->conv == NULL) {
92 PixelFormat dst = qemu_default_pixelformat(32);
93 ssd->conv = qemu_pf_conv_get(&dst, &ssd->ds->surface->pf);
94 assert(ssd->conv);
97 src = ds_get_data(ssd->ds) +
98 ssd->dirty.top * ds_get_linesize(ssd->ds) +
99 ssd->dirty.left * ds_get_bytes_per_pixel(ssd->ds);
100 dst = update->bitmap;
101 for (by = 0; by < bh; by++) {
102 qemu_pf_conv_run(ssd->conv, dst, src, bw);
103 src += ds_get_linesize(ssd->ds);
104 dst += image->bitmap.stride;
107 cmd->type = QXL_CMD_DRAW;
108 cmd->data = (intptr_t)drawable;
110 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
111 pthread_mutex_unlock(&ssd->lock);
112 return update;
115 void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
117 qemu_free(update->bitmap);
118 qemu_free(update);
121 void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
123 QXLDevMemSlot memslot;
125 if (debug)
126 fprintf(stderr, "%s:\n", __FUNCTION__);
128 memset(&memslot, 0, sizeof(memslot));
129 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
130 memslot.virt_end = ~0;
131 ssd->worker->add_memslot(ssd->worker, &memslot);
134 void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
136 QXLDevSurfaceCreate surface;
138 if (debug)
139 fprintf(stderr, "%s: %dx%d\n", __FUNCTION__,
140 ds_get_width(ssd->ds), ds_get_height(ssd->ds));
142 surface.format = SPICE_SURFACE_FMT_32_xRGB;
143 surface.width = ds_get_width(ssd->ds);
144 surface.height = ds_get_height(ssd->ds);
145 surface.stride = -surface.width * 4;
146 surface.mouse_mode = true;
147 surface.flags = 0;
148 surface.type = 0;
149 surface.mem = (intptr_t)ssd->buf;
150 surface.group_id = MEMSLOT_GROUP_HOST;
151 ssd->worker->create_primary_surface(ssd->worker, 0, &surface);
154 void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
156 if (debug)
157 fprintf(stderr, "%s:\n", __FUNCTION__);
159 ssd->worker->destroy_primary_surface(ssd->worker, 0);
162 void qemu_spice_vm_change_state_handler(void *opaque, int running, int reason)
164 SimpleSpiceDisplay *ssd = opaque;
166 if (running) {
167 ssd->worker->start(ssd->worker);
168 } else {
169 ssd->worker->stop(ssd->worker);
171 ssd->running = running;
174 /* display listener callbacks */
176 void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
177 int x, int y, int w, int h)
179 SpiceRect update_area;
181 if (debug > 1)
182 fprintf(stderr, "%s: x %d y %d w %d h %d\n", __FUNCTION__, x, y, w, h);
183 update_area.left = x,
184 update_area.right = x + w;
185 update_area.top = y;
186 update_area.bottom = y + h;
188 pthread_mutex_lock(&ssd->lock);
189 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
190 ssd->notify++;
192 qemu_spice_rect_union(&ssd->dirty, &update_area);
193 pthread_mutex_unlock(&ssd->lock);
196 void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
198 if (debug)
199 fprintf(stderr, "%s:\n", __FUNCTION__);
201 pthread_mutex_lock(&ssd->lock);
202 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
203 pthread_mutex_unlock(&ssd->lock);
205 qemu_spice_destroy_host_primary(ssd);
206 qemu_spice_create_host_primary(ssd);
207 qemu_pf_conv_put(ssd->conv);
208 ssd->conv = NULL;
210 pthread_mutex_lock(&ssd->lock);
211 ssd->dirty.left = 0;
212 ssd->dirty.right = ds_get_width(ssd->ds);
213 ssd->dirty.top = 0;
214 ssd->dirty.bottom = ds_get_height(ssd->ds);
215 ssd->notify++;
216 pthread_mutex_unlock(&ssd->lock);
219 void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
221 if (debug > 2)
222 fprintf(stderr, "%s:\n", __FUNCTION__);
223 vga_hw_update();
224 if (ssd->notify) {
225 ssd->notify = 0;
226 ssd->worker->wakeup(ssd->worker);
227 if (debug > 1)
228 fprintf(stderr, "%s: notify\n", __FUNCTION__);
232 /* spice display interface callbacks */
234 static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
236 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
238 if (debug)
239 fprintf(stderr, "%s:\n", __FUNCTION__);
240 ssd->worker = qxl_worker;
243 static void interface_set_compression_level(QXLInstance *sin, int level)
245 if (debug)
246 fprintf(stderr, "%s:\n", __FUNCTION__);
247 /* nothing to do */
250 static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
252 if (debug > 2)
253 fprintf(stderr, "%s:\n", __FUNCTION__);
254 /* nothing to do */
257 static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
259 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
261 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
262 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
263 info->num_memslots = NUM_MEMSLOTS;
264 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
265 info->internal_groupslot_id = 0;
266 info->qxl_ram_size = ssd->bufsize;
267 info->n_surfaces = NUM_SURFACES;
270 static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
272 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
273 SimpleSpiceUpdate *update;
275 if (debug > 2)
276 fprintf(stderr, "%s:\n", __FUNCTION__);
277 update = qemu_spice_create_update(ssd);
278 if (update == NULL) {
279 return false;
281 *ext = update->ext;
282 return true;
285 static int interface_req_cmd_notification(QXLInstance *sin)
287 if (debug)
288 fprintf(stderr, "%s:\n", __FUNCTION__);
289 return 1;
292 static int interface_has_command(QXLInstance *sin)
294 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
296 if (debug)
297 fprintf(stderr, "%s:\n", __FUNCTION__);
298 return !qemu_spice_rect_is_empty(&ssd->dirty);
301 static void interface_release_resource(QXLInstance *sin,
302 struct QXLReleaseInfoExt ext)
304 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
305 uintptr_t id;
307 if (debug > 1)
308 fprintf(stderr, "%s:\n", __FUNCTION__);
309 id = ext.info->id;
310 qemu_spice_destroy_update(ssd, (void*)id);
313 static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
315 if (debug > 2)
316 fprintf(stderr, "%s:\n", __FUNCTION__);
317 return false;
320 static int interface_req_cursor_notification(QXLInstance *sin)
322 if (debug)
323 fprintf(stderr, "%s:\n", __FUNCTION__);
324 return 1;
327 static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
329 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
330 abort();
333 static int interface_flush_resources(QXLInstance *sin)
335 fprintf(stderr, "%s: abort()\n", __FUNCTION__);
336 abort();
337 return 0;
340 static const QXLInterface dpy_interface = {
341 .base.type = SPICE_INTERFACE_QXL,
342 .base.description = "qemu simple display",
343 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
344 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
346 .pci_vendor = REDHAT_PCI_VENDOR_ID,
347 .pci_id = QXL_DEVICE_ID,
348 .pci_revision = QXL_REVISION,
350 .attache_worker = interface_attach_worker,
351 .set_compression_level = interface_set_compression_level,
352 .set_mm_time = interface_set_mm_time,
354 .get_init_info = interface_get_init_info,
355 .get_command = interface_get_command,
356 .req_cmd_notification = interface_req_cmd_notification,
357 .has_command = interface_has_command,
358 .release_resource = interface_release_resource,
359 .get_cursor_command = interface_get_cursor_command,
360 .req_cursor_notification = interface_req_cursor_notification,
361 .notify_update = interface_notify_update,
362 .flush_resources = interface_flush_resources,
365 static SimpleSpiceDisplay sdpy;
367 static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
369 qemu_spice_display_update(&sdpy, x, y, w, h);
372 static void display_resize(struct DisplayState *ds)
374 qemu_spice_display_resize(&sdpy);
377 static void display_refresh(struct DisplayState *ds)
379 qemu_spice_display_refresh(&sdpy);
382 static DisplayChangeListener display_listener = {
383 .dpy_update = display_update,
384 .dpy_resize = display_resize,
385 .dpy_refresh = display_refresh,
388 void qemu_spice_display_init(DisplayState *ds)
390 assert(sdpy.ds == NULL);
391 sdpy.ds = ds;
392 sdpy.bufsize = (16 * 1024 * 1024);
393 sdpy.buf = qemu_malloc(sdpy.bufsize);
394 pthread_mutex_init(&sdpy.lock, NULL);
395 register_displaychangelistener(ds, &display_listener);
397 sdpy.qxl.base.sif = &dpy_interface.base;
398 spice_server_add_interface(spice_server, &sdpy.qxl.base);
399 assert(sdpy.worker);
401 qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler, &sdpy);
402 qemu_spice_create_host_memslot(&sdpy);
403 qemu_spice_create_host_primary(&sdpy);