]> git.proxmox.com Git - mirror_qemu.git/blame - ui/spice-display.c
migration: equation is more proper than and to check LOADVM_QUIT
[mirror_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
e16f4c87 18#include "qemu/osdep.h"
28ecbaee 19#include "ui/qemu-spice.h"
1de7afc9 20#include "qemu/timer.h"
922a01a0 21#include "qemu/option.h"
1de7afc9 22#include "qemu/queue.h"
28ecbaee 23#include "ui/console.h"
9c17d615 24#include "sysemu/sysemu.h"
c480bb7d 25#include "trace.h"
a3e22260 26
28ecbaee 27#include "ui/spice-display.h"
a3e22260 28
fe5c44f9 29bool spice_opengl;
a3e22260 30
a3e22260
GH
31int qemu_spice_rect_is_empty(const QXLRect* r)
32{
33 return r->top == r->bottom || r->left == r->right;
34}
35
36void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
37{
38 if (qemu_spice_rect_is_empty(r)) {
39 return;
40 }
41
42 if (qemu_spice_rect_is_empty(dest)) {
43 *dest = *r;
44 return;
45 }
46
47 dest->top = MIN(dest->top, r->top);
48 dest->left = MIN(dest->left, r->left);
49 dest->bottom = MAX(dest->bottom, r->bottom);
50 dest->right = MAX(dest->right, r->right);
51}
52
2e1a98c9
AL
53QXLCookie *qxl_cookie_new(int type, uint64_t io)
54{
55 QXLCookie *cookie;
56
57 cookie = g_malloc0(sizeof(*cookie));
58 cookie->type = type;
59 cookie->io = io;
60 return cookie;
61}
62
5ff4e36c
AL
63void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
64 qxl_async_io async)
5c59d118 65{
c480bb7d
AL
66 trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
67 memslot->virt_start, memslot->virt_end,
68 async);
69
5ff4e36c 70 if (async != QXL_SYNC) {
2e1a98c9 71 spice_qxl_add_memslot_async(&ssd->qxl, memslot,
34d14c6d
PM
72 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
73 QXL_IO_MEMSLOT_ADD_ASYNC));
5ff4e36c 74 } else {
26defe81 75 spice_qxl_add_memslot(&ssd->qxl, memslot);
5ff4e36c 76 }
5c59d118
GH
77}
78
79void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
80{
c480bb7d 81 trace_qemu_spice_del_memslot(ssd->qxl.id, gid, sid);
26defe81 82 spice_qxl_del_memslot(&ssd->qxl, gid, sid);
5c59d118
GH
83}
84
85void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
5ff4e36c
AL
86 QXLDevSurfaceCreate *surface,
87 qxl_async_io async)
5c59d118 88{
c480bb7d 89 trace_qemu_spice_create_primary_surface(ssd->qxl.id, id, surface, async);
5ff4e36c 90 if (async != QXL_SYNC) {
2e1a98c9 91 spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface,
34d14c6d
PM
92 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
93 QXL_IO_CREATE_PRIMARY_ASYNC));
5ff4e36c 94 } else {
26defe81 95 spice_qxl_create_primary_surface(&ssd->qxl, id, surface);
5ff4e36c 96 }
5c59d118
GH
97}
98
5ff4e36c
AL
99void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
100 uint32_t id, qxl_async_io async)
5c59d118 101{
c480bb7d 102 trace_qemu_spice_destroy_primary_surface(ssd->qxl.id, id, async);
5ff4e36c 103 if (async != QXL_SYNC) {
2e1a98c9 104 spice_qxl_destroy_primary_surface_async(&ssd->qxl, id,
34d14c6d
PM
105 (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
106 QXL_IO_DESTROY_PRIMARY_ASYNC));
5ff4e36c 107 } else {
26defe81 108 spice_qxl_destroy_primary_surface(&ssd->qxl, id);
5ff4e36c 109 }
5c59d118
GH
110}
111
5c59d118
GH
112void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
113{
c480bb7d 114 trace_qemu_spice_wakeup(ssd->qxl.id);
26defe81 115 spice_qxl_wakeup(&ssd->qxl);
5c59d118
GH
116}
117
c60319a3
GH
118static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
119 QXLRect *rect)
a3e22260
GH
120{
121 SimpleSpiceUpdate *update;
122 QXLDrawable *drawable;
123 QXLImage *image;
124 QXLCommand *cmd;
d9a86569 125 int bw, bh;
22795174 126 struct timespec time_space;
d9a86569 127 pixman_image_t *dest;
a3e22260 128
c480bb7d 129 trace_qemu_spice_create_update(
c60319a3
GH
130 rect->left, rect->right,
131 rect->top, rect->bottom);
a3e22260 132
7267c094 133 update = g_malloc0(sizeof(*update));
a3e22260
GH
134 drawable = &update->drawable;
135 image = &update->image;
136 cmd = &update->ext.cmd;
137
c60319a3
GH
138 bw = rect->right - rect->left;
139 bh = rect->bottom - rect->top;
7267c094 140 update->bitmap = g_malloc(bw * bh * 4);
a3e22260 141
c60319a3 142 drawable->bbox = *rect;
a3e22260
GH
143 drawable->clip.type = SPICE_CLIP_TYPE_NONE;
144 drawable->effect = QXL_EFFECT_OPAQUE;
5643fc01 145 drawable->release_info.id = (uintptr_t)(&update->ext);
a3e22260
GH
146 drawable->type = QXL_DRAW_COPY;
147 drawable->surfaces_dest[0] = -1;
148 drawable->surfaces_dest[1] = -1;
149 drawable->surfaces_dest[2] = -1;
22795174
AL
150 clock_gettime(CLOCK_MONOTONIC, &time_space);
151 /* time in milliseconds from epoch. */
152 drawable->mm_time = time_space.tv_sec * 1000
153 + time_space.tv_nsec / 1000 / 1000;
a3e22260
GH
154
155 drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
a13ccc99 156 drawable->u.copy.src_bitmap = (uintptr_t)image;
a3e22260
GH
157 drawable->u.copy.src_area.right = bw;
158 drawable->u.copy.src_area.bottom = bh;
159
160 QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
161 image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
162 image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
163 image->bitmap.stride = bw * 4;
164 image->descriptor.width = image->bitmap.x = bw;
165 image->descriptor.height = image->bitmap.y = bh;
a13ccc99 166 image->bitmap.data = (uintptr_t)(update->bitmap);
a3e22260
GH
167 image->bitmap.palette = 0;
168 image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
169
c1d37cd3 170 dest = pixman_image_create_bits(PIXMAN_LE_x8r8g8b8, bw, bh,
d9a86569
GH
171 (void *)update->bitmap, bw * 4);
172 pixman_image_composite(PIXMAN_OP_SRC, ssd->surface, NULL, ssd->mirror,
173 rect->left, rect->top, 0, 0,
174 rect->left, rect->top, bw, bh);
175 pixman_image_composite(PIXMAN_OP_SRC, ssd->mirror, NULL, dest,
176 rect->left, rect->top, 0, 0,
177 0, 0, bw, bh);
178 pixman_image_unref(dest);
a3e22260
GH
179
180 cmd->type = QXL_CMD_DRAW;
a13ccc99 181 cmd->data = (uintptr_t)drawable;
a3e22260 182
b1af98ba 183 QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
a3e22260
GH
184}
185
c60319a3
GH
186static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
187{
b021bd29 188 static const int blksize = 32;
5d61cafd 189 int blocks = DIV_ROUND_UP(surface_width(ssd->ds), blksize);
b021bd29 190 int dirty_top[blocks];
c6e48470 191 int y, yoff1, yoff2, x, xoff, blk, bw;
71874c17 192 int bpp = surface_bytes_per_pixel(ssd->ds);
b021bd29
GH
193 uint8_t *guest, *mirror;
194
c60319a3
GH
195 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
196 return;
197 };
a7310dd3 198
b021bd29
GH
199 for (blk = 0; blk < blocks; blk++) {
200 dirty_top[blk] = -1;
201 }
202
71874c17 203 guest = surface_data(ssd->ds);
d9a86569 204 mirror = (void *)pixman_image_get_data(ssd->mirror);
b021bd29 205 for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
c6e48470
GH
206 yoff1 = y * surface_stride(ssd->ds);
207 yoff2 = y * pixman_image_get_stride(ssd->mirror);
b021bd29
GH
208 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
209 xoff = x * bpp;
210 blk = x / blksize;
211 bw = MIN(blksize, ssd->dirty.right - x);
c6e48470
GH
212 if (memcmp(guest + yoff1 + xoff,
213 mirror + yoff2 + xoff,
b021bd29
GH
214 bw * bpp) == 0) {
215 if (dirty_top[blk] != -1) {
216 QXLRect update = {
217 .top = dirty_top[blk],
218 .bottom = y,
219 .left = x,
220 .right = x + bw,
221 };
222 qemu_spice_create_one_update(ssd, &update);
223 dirty_top[blk] = -1;
224 }
225 } else {
226 if (dirty_top[blk] == -1) {
227 dirty_top[blk] = y;
228 }
229 }
230 }
231 }
232
233 for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
234 blk = x / blksize;
235 bw = MIN(blksize, ssd->dirty.right - x);
236 if (dirty_top[blk] != -1) {
237 QXLRect update = {
238 .top = dirty_top[blk],
239 .bottom = ssd->dirty.bottom,
240 .left = x,
241 .right = x + bw,
242 };
243 qemu_spice_create_one_update(ssd, &update);
244 dirty_top[blk] = -1;
245 }
246 }
247
c60319a3
GH
248 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
249}
250
5643fc01
GH
251static SimpleSpiceCursor*
252qemu_spice_create_cursor_update(SimpleSpiceDisplay *ssd,
700cd855
MAL
253 QEMUCursor *c,
254 int on)
5643fc01
GH
255{
256 size_t size = c ? c->width * c->height * 4 : 0;
257 SimpleSpiceCursor *update;
258 QXLCursorCmd *ccmd;
259 QXLCursor *cursor;
260 QXLCommand *cmd;
261
262 update = g_malloc0(sizeof(*update) + size);
263 ccmd = &update->cmd;
264 cursor = &update->cursor;
265 cmd = &update->ext.cmd;
266
267 if (c) {
268 ccmd->type = QXL_CURSOR_SET;
dc8dceee
MAL
269 ccmd->u.set.position.x = ssd->ptr_x + ssd->hot_x;
270 ccmd->u.set.position.y = ssd->ptr_y + ssd->hot_y;
5643fc01
GH
271 ccmd->u.set.visible = true;
272 ccmd->u.set.shape = (uintptr_t)cursor;
273 cursor->header.unique = ssd->unique++;
274 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
275 cursor->header.width = c->width;
276 cursor->header.height = c->height;
277 cursor->header.hot_spot_x = c->hot_x;
278 cursor->header.hot_spot_y = c->hot_y;
279 cursor->data_size = size;
280 cursor->chunk.data_size = size;
281 memcpy(cursor->chunk.data, c->data, size);
700cd855
MAL
282 } else if (!on) {
283 ccmd->type = QXL_CURSOR_HIDE;
5643fc01
GH
284 } else {
285 ccmd->type = QXL_CURSOR_MOVE;
dc8dceee
MAL
286 ccmd->u.position.x = ssd->ptr_x + ssd->hot_x;
287 ccmd->u.position.y = ssd->ptr_y + ssd->hot_y;
5643fc01
GH
288 }
289 ccmd->release_info.id = (uintptr_t)(&update->ext);
290
291 cmd->type = QXL_CMD_CURSOR;
292 cmd->data = (uintptr_t)ccmd;
293
294 return update;
295}
296
a3e22260 297/*
4580c490 298 * Called from spice server thread context (via interface_release_resource)
a3e22260 299 * We do *not* hold the global qemu mutex here, so extra care is needed
5cbdb3a3 300 * when calling qemu functions. QEMU interfaces used:
7267c094 301 * - g_free (underlying glibc free is re-entrant).
a3e22260
GH
302 */
303void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
304{
7267c094
AL
305 g_free(update->bitmap);
306 g_free(update);
a3e22260
GH
307}
308
309void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
310{
311 QXLDevMemSlot memslot;
312
a3e22260
GH
313 memset(&memslot, 0, sizeof(memslot));
314 memslot.slot_group_id = MEMSLOT_GROUP_HOST;
315 memslot.virt_end = ~0;
5ff4e36c 316 qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
a3e22260
GH
317}
318
319void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
320{
321 QXLDevSurfaceCreate surface;
ab9509cc 322 uint64_t surface_size;
a3e22260 323
160c31f7
AL
324 memset(&surface, 0, sizeof(surface));
325
ab9509cc
GH
326 surface_size = (uint64_t) surface_width(ssd->ds) *
327 surface_height(ssd->ds) * 4;
328 assert(surface_size > 0);
329 assert(surface_size < INT_MAX);
330 if (ssd->bufsize < surface_size) {
331 ssd->bufsize = surface_size;
332 g_free(ssd->buf);
333 ssd->buf = g_malloc(ssd->bufsize);
334 }
335
a3e22260 336 surface.format = SPICE_SURFACE_FMT_32_xRGB;
71874c17
GH
337 surface.width = surface_width(ssd->ds);
338 surface.height = surface_height(ssd->ds);
a3e22260 339 surface.stride = -surface.width * 4;
869564a9 340 surface.mouse_mode = true;
a3e22260
GH
341 surface.flags = 0;
342 surface.type = 0;
a13ccc99 343 surface.mem = (uintptr_t)ssd->buf;
a3e22260 344 surface.group_id = MEMSLOT_GROUP_HOST;
7466bc49 345
5ff4e36c 346 qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
a3e22260
GH
347}
348
349void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
350{
5ff4e36c 351 qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
a3e22260
GH
352}
353
c78f7137 354void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
a963f876 355{
a963f876 356 qemu_mutex_init(&ssd->lock);
b1af98ba 357 QTAILQ_INIT(&ssd->updates);
a963f876
GH
358 ssd->mouse_x = -1;
359 ssd->mouse_y = -1;
ddd8fdc7
GH
360 if (ssd->num_surfaces == 0) {
361 ssd->num_surfaces = 1024;
362 }
a963f876
GH
363}
364
a3e22260
GH
365/* display listener callbacks */
366
367void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
368 int x, int y, int w, int h)
369{
370 QXLRect update_area;
371
e181f8bb 372 trace_qemu_spice_display_update(ssd->qxl.id, x, y, w, h);
a3e22260
GH
373 update_area.left = x,
374 update_area.right = x + w;
375 update_area.top = y;
376 update_area.bottom = y + h;
377
a3e22260
GH
378 if (qemu_spice_rect_is_empty(&ssd->dirty)) {
379 ssd->notify++;
380 }
381 qemu_spice_rect_union(&ssd->dirty, &update_area);
a3e22260
GH
382}
383
c12aeb86
GH
384void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
385 DisplaySurface *surface)
a3e22260 386{
b1af98ba 387 SimpleSpiceUpdate *update;
4b87dc4c 388 bool need_destroy;
b1af98ba 389
555e72f2
GH
390 if (surface && ssd->surface &&
391 surface_width(surface) == pixman_image_get_width(ssd->surface) &&
b2af43cc
GH
392 surface_height(surface) == pixman_image_get_height(ssd->surface) &&
393 surface_format(surface) == pixman_image_get_format(ssd->surface)) {
555e72f2 394 /* no-resize fast path: just swap backing store */
e181f8bb
GH
395 trace_qemu_spice_display_surface(ssd->qxl.id,
396 surface_width(surface),
397 surface_height(surface),
398 true);
555e72f2
GH
399 qemu_mutex_lock(&ssd->lock);
400 ssd->ds = surface;
401 pixman_image_unref(ssd->surface);
402 ssd->surface = pixman_image_ref(ssd->ds->image);
403 qemu_mutex_unlock(&ssd->lock);
404 qemu_spice_display_update(ssd, 0, 0,
405 surface_width(surface),
406 surface_height(surface));
407 return;
408 }
409
410 /* full mode switch */
e181f8bb
GH
411 trace_qemu_spice_display_surface(ssd->qxl.id,
412 surface ? surface_width(surface) : 0,
413 surface ? surface_height(surface) : 0,
414 false);
a3e22260 415
a3e22260 416 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
d9a86569
GH
417 if (ssd->surface) {
418 pixman_image_unref(ssd->surface);
419 ssd->surface = NULL;
420 pixman_image_unref(ssd->mirror);
421 ssd->mirror = NULL;
422 }
a3e22260 423
e0c64d08 424 qemu_mutex_lock(&ssd->lock);
4b87dc4c 425 need_destroy = (ssd->ds != NULL);
71874c17 426 ssd->ds = surface;
b1af98ba
GH
427 while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
428 QTAILQ_REMOVE(&ssd->updates, update, next);
429 qemu_spice_destroy_update(ssd, update);
e0c64d08
GH
430 }
431 qemu_mutex_unlock(&ssd->lock);
4b87dc4c
GH
432 if (need_destroy) {
433 qemu_spice_destroy_host_primary(ssd);
434 }
435 if (ssd->ds) {
51a09099
GH
436 ssd->surface = pixman_image_ref(ssd->ds->image);
437 ssd->mirror = qemu_pixman_mirror_create(ssd->ds->format,
438 ssd->ds->image);
4b87dc4c
GH
439 qemu_spice_create_host_primary(ssd);
440 }
a3e22260 441
a3e22260
GH
442 memset(&ssd->dirty, 0, sizeof(ssd->dirty));
443 ssd->notify++;
58c7b618
MAL
444
445 qemu_mutex_lock(&ssd->lock);
446 if (ssd->cursor) {
447 g_free(ssd->ptr_define);
448 ssd->ptr_define = qemu_spice_create_cursor_update(ssd, ssd->cursor, 0);
449 }
450 qemu_mutex_unlock(&ssd->lock);
a3e22260
GH
451}
452
006bf464 453void qemu_spice_cursor_refresh_bh(void *opaque)
a3e22260 454{
006bf464
PB
455 SimpleSpiceDisplay *ssd = opaque;
456
457 qemu_mutex_lock(&ssd->lock);
07536094 458 if (ssd->cursor) {
006bf464 459 QEMUCursor *c = ssd->cursor;
284d1c6b 460 assert(ssd->dcl.con);
006bf464
PB
461 cursor_get(c);
462 qemu_mutex_unlock(&ssd->lock);
463 dpy_cursor_define(ssd->dcl.con, c);
464 qemu_mutex_lock(&ssd->lock);
465 cursor_put(c);
07536094 466 }
006bf464 467
07536094 468 if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
006bf464 469 int x, y;
284d1c6b 470 assert(ssd->dcl.con);
006bf464
PB
471 x = ssd->mouse_x;
472 y = ssd->mouse_y;
07536094
GH
473 ssd->mouse_x = -1;
474 ssd->mouse_y = -1;
006bf464
PB
475 qemu_mutex_unlock(&ssd->lock);
476 dpy_mouse_set(ssd->dcl.con, x, y, 1);
477 } else {
478 qemu_mutex_unlock(&ssd->lock);
07536094 479 }
bb5a8cd5
AL
480}
481
482void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
483{
284d1c6b 484 graphic_hw_update(ssd->dcl.con);
bb5a8cd5
AL
485
486 qemu_mutex_lock(&ssd->lock);
71874c17 487 if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
b1af98ba 488 qemu_spice_create_update(ssd);
bb5a8cd5
AL
489 ssd->notify++;
490 }
e0c64d08
GH
491 qemu_mutex_unlock(&ssd->lock);
492
e181f8bb 493 trace_qemu_spice_display_refresh(ssd->qxl.id, ssd->notify);
a3e22260
GH
494 if (ssd->notify) {
495 ssd->notify = 0;
5c59d118 496 qemu_spice_wakeup(ssd);
a3e22260
GH
497 }
498}
499
500/* spice display interface callbacks */
501
502static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
503{
e181f8bb 504 /* nothing to do */
a3e22260
GH
505}
506
507static void interface_set_compression_level(QXLInstance *sin, int level)
508{
a3e22260
GH
509 /* nothing to do */
510}
511
015e02f8 512#if SPICE_NEEDS_SET_MM_TIME
a3e22260
GH
513static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
514{
a3e22260
GH
515 /* nothing to do */
516}
015e02f8 517#endif
a3e22260
GH
518
519static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
520{
521 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
522
523 info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
524 info->memslot_id_bits = MEMSLOT_SLOT_BITS;
525 info->num_memslots = NUM_MEMSLOTS;
526 info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
527 info->internal_groupslot_id = 0;
ab9509cc 528 info->qxl_ram_size = 16 * 1024 * 1024;
ddd8fdc7 529 info->n_surfaces = ssd->num_surfaces;
a3e22260
GH
530}
531
c9f88ce3 532static int interface_get_command(QXLInstance *sin, QXLCommandExt *ext)
a3e22260
GH
533{
534 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
535 SimpleSpiceUpdate *update;
e0c64d08 536 int ret = false;
a3e22260 537
e0c64d08 538 qemu_mutex_lock(&ssd->lock);
b1af98ba
GH
539 update = QTAILQ_FIRST(&ssd->updates);
540 if (update != NULL) {
541 QTAILQ_REMOVE(&ssd->updates, update, next);
e0c64d08
GH
542 *ext = update->ext;
543 ret = true;
a3e22260 544 }
e0c64d08
GH
545 qemu_mutex_unlock(&ssd->lock);
546
547 return ret;
a3e22260
GH
548}
549
550static int interface_req_cmd_notification(QXLInstance *sin)
551{
a3e22260
GH
552 return 1;
553}
554
555static void interface_release_resource(QXLInstance *sin,
c9f88ce3 556 QXLReleaseInfoExt rext)
a3e22260
GH
557{
558 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
5643fc01
GH
559 SimpleSpiceUpdate *update;
560 SimpleSpiceCursor *cursor;
561 QXLCommandExt *ext;
a3e22260 562
e8e23b7d 563 ext = (void *)(intptr_t)(rext.info->id);
5643fc01
GH
564 switch (ext->cmd.type) {
565 case QXL_CMD_DRAW:
566 update = container_of(ext, SimpleSpiceUpdate, ext);
567 qemu_spice_destroy_update(ssd, update);
568 break;
569 case QXL_CMD_CURSOR:
570 cursor = container_of(ext, SimpleSpiceCursor, ext);
571 g_free(cursor);
572 break;
573 default:
574 g_assert_not_reached();
575 }
a3e22260
GH
576}
577
c9f88ce3 578static int interface_get_cursor_command(QXLInstance *sin, QXLCommandExt *ext)
a3e22260 579{
5643fc01
GH
580 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
581 int ret;
582
5643fc01
GH
583 qemu_mutex_lock(&ssd->lock);
584 if (ssd->ptr_define) {
585 *ext = ssd->ptr_define->ext;
586 ssd->ptr_define = NULL;
587 ret = true;
588 } else if (ssd->ptr_move) {
589 *ext = ssd->ptr_move->ext;
590 ssd->ptr_move = NULL;
591 ret = true;
592 } else {
593 ret = false;
594 }
595 qemu_mutex_unlock(&ssd->lock);
596 return ret;
a3e22260
GH
597}
598
599static int interface_req_cursor_notification(QXLInstance *sin)
600{
a3e22260
GH
601 return 1;
602}
603
604static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
605{
a89f364a 606 fprintf(stderr, "%s: abort()\n", __func__);
a3e22260
GH
607 abort();
608}
609
610static int interface_flush_resources(QXLInstance *sin)
611{
a89f364a 612 fprintf(stderr, "%s: abort()\n", __func__);
a3e22260
GH
613 abort();
614 return 0;
615}
616
21a50d0b
GH
617static void interface_update_area_complete(QXLInstance *sin,
618 uint32_t surface_id,
619 QXLRect *dirty, uint32_t num_updated_rects)
620{
621 /* should never be called, used in qxl native mode only */
622 fprintf(stderr, "%s: abort()\n", __func__);
623 abort();
624}
625
626/* called from spice server thread context only */
627static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
628{
474114b7
GH
629 QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
630
631 switch (cookie->type) {
632#ifdef HAVE_SPICE_GL
633 case QXL_COOKIE_TYPE_GL_DRAW_DONE:
634 {
635 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
636 qemu_bh_schedule(ssd->gl_unblock_bh);
637 break;
638 }
39414ef4
GH
639 case QXL_COOKIE_TYPE_IO:
640 if (cookie->io == QXL_IO_MONITORS_CONFIG_ASYNC) {
641 g_free(cookie->u.data);
642 }
643 break;
474114b7
GH
644#endif
645 default:
646 /* should never be called, used in qxl native mode only */
647 fprintf(stderr, "%s: abort()\n", __func__);
648 abort();
649 }
650 g_free(cookie);
21a50d0b
GH
651}
652
653static void interface_set_client_capabilities(QXLInstance *sin,
654 uint8_t client_present,
655 uint8_t caps[58])
656{
e181f8bb 657 /* nothing to do */
21a50d0b
GH
658}
659
660static int interface_client_monitors_config(QXLInstance *sin,
9b74d0d5 661 VDAgentMonitorsConfig *mc)
21a50d0b 662{
9b74d0d5
GH
663 SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
664 QemuUIInfo info;
c61d8126 665 int head;
5a9259a0
GH
666
667 if (!dpy_ui_info_supported(ssd->dcl.con)) {
668 return 0; /* == not supported by guest */
669 }
9b74d0d5 670
dc491cfc
GH
671 if (!mc) {
672 return 1;
673 }
674
9b74d0d5 675 memset(&info, 0, sizeof(info));
c61d8126 676
9c956e64
GH
677 if (mc->num_of_monitors == 1) {
678 /*
679 * New spice-server version which filters the list of monitors
680 * to only include those that belong to our display channel.
681 *
682 * single-head configuration (where filtering doesn't matter)
683 * takes this code path too.
684 */
685 info.width = mc->monitors[0].width;
686 info.height = mc->monitors[0].height;
687 } else {
688 /*
689 * Old spice-server which gives us all monitors, so we have to
690 * figure ourself which entry we need. Array index is the
691 * channel_id, which is the qemu console index, see
692 * qemu_spice_add_display_interface().
693 */
694 head = qemu_console_get_index(ssd->dcl.con);
695 if (mc->num_of_monitors > head) {
696 info.width = mc->monitors[head].width;
697 info.height = mc->monitors[head].height;
698 }
9b74d0d5 699 }
e181f8bb
GH
700
701 trace_qemu_spice_ui_info(ssd->qxl.id, info.width, info.height);
5a9259a0 702 dpy_set_ui_info(ssd->dcl.con, &info);
5a9259a0 703 return 1;
21a50d0b
GH
704}
705
a3e22260
GH
706static const QXLInterface dpy_interface = {
707 .base.type = SPICE_INTERFACE_QXL,
708 .base.description = "qemu simple display",
709 .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
710 .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
711
712 .attache_worker = interface_attach_worker,
713 .set_compression_level = interface_set_compression_level,
015e02f8 714#if SPICE_NEEDS_SET_MM_TIME
a3e22260 715 .set_mm_time = interface_set_mm_time,
015e02f8 716#endif
a3e22260
GH
717 .get_init_info = interface_get_init_info,
718
719 /* the callbacks below are called from spice server thread context */
720 .get_command = interface_get_command,
721 .req_cmd_notification = interface_req_cmd_notification,
722 .release_resource = interface_release_resource,
723 .get_cursor_command = interface_get_cursor_command,
724 .req_cursor_notification = interface_req_cursor_notification,
725 .notify_update = interface_notify_update,
726 .flush_resources = interface_flush_resources,
21a50d0b
GH
727 .async_complete = interface_async_complete,
728 .update_area_complete = interface_update_area_complete,
729 .set_client_capabilities = interface_set_client_capabilities,
730 .client_monitors_config = interface_client_monitors_config,
a3e22260
GH
731};
732
7c20b4a3 733static void display_update(DisplayChangeListener *dcl,
7c20b4a3 734 int x, int y, int w, int h)
a3e22260 735{
9c80a315
GH
736 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
737 qemu_spice_display_update(ssd, x, y, w, h);
a3e22260
GH
738}
739
c12aeb86 740static void display_switch(DisplayChangeListener *dcl,
9425c004 741 DisplaySurface *surface)
a3e22260 742{
9c80a315 743 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
c12aeb86 744 qemu_spice_display_switch(ssd, surface);
a3e22260
GH
745}
746
bc2ed970 747static void display_refresh(DisplayChangeListener *dcl)
a3e22260 748{
9c80a315
GH
749 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
750 qemu_spice_display_refresh(ssd);
a3e22260
GH
751}
752
5643fc01
GH
753static void display_mouse_set(DisplayChangeListener *dcl,
754 int x, int y, int on)
755{
756 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
757
758 qemu_mutex_lock(&ssd->lock);
759 ssd->ptr_x = x;
d0df04a1 760 ssd->ptr_y = y;
ef1e1e07 761 g_free(ssd->ptr_move);
700cd855 762 ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL, on);
5643fc01 763 qemu_mutex_unlock(&ssd->lock);
51e0b654 764 qemu_spice_wakeup(ssd);
5643fc01
GH
765}
766
767static void display_mouse_define(DisplayChangeListener *dcl,
768 QEMUCursor *c)
769{
770 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
771
772 qemu_mutex_lock(&ssd->lock);
28f4a708 773 cursor_get(c);
58c7b618
MAL
774 cursor_put(ssd->cursor);
775 ssd->cursor = c;
dc8dceee
MAL
776 ssd->hot_x = c->hot_x;
777 ssd->hot_y = c->hot_y;
ef1e1e07
DB
778 g_free(ssd->ptr_move);
779 ssd->ptr_move = NULL;
780 g_free(ssd->ptr_define);
700cd855 781 ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c, 0);
5643fc01 782 qemu_mutex_unlock(&ssd->lock);
51e0b654 783 qemu_spice_wakeup(ssd);
5643fc01
GH
784}
785
7c20b4a3 786static const DisplayChangeListenerOps display_listener_ops = {
0002a518
GH
787 .dpy_name = "spice",
788 .dpy_gfx_update = display_update,
789 .dpy_gfx_switch = display_switch,
790 .dpy_gfx_check_format = qemu_pixman_check_format,
791 .dpy_refresh = display_refresh,
792 .dpy_mouse_set = display_mouse_set,
793 .dpy_cursor_define = display_mouse_define,
a3e22260
GH
794};
795
474114b7
GH
796#ifdef HAVE_SPICE_GL
797
39414ef4
GH
798static void qemu_spice_gl_monitor_config(SimpleSpiceDisplay *ssd,
799 int x, int y, int w, int h)
800{
801 QXLMonitorsConfig *config;
802 QXLCookie *cookie;
803
804 config = g_malloc0(sizeof(QXLMonitorsConfig) + sizeof(QXLHead));
805 config->count = 1;
806 config->max_allowed = 1;
807 config->heads[0].x = x;
808 config->heads[0].y = y;
809 config->heads[0].width = w;
810 config->heads[0].height = h;
811 cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
812 QXL_IO_MONITORS_CONFIG_ASYNC);
813 cookie->u.data = config;
814
815 spice_qxl_monitors_config_async(&ssd->qxl,
816 (uintptr_t)config,
817 MEMSLOT_GROUP_HOST,
818 (uintptr_t)cookie);
819}
820
474114b7
GH
821static void qemu_spice_gl_block(SimpleSpiceDisplay *ssd, bool block)
822{
8e388e90
GH
823 uint64_t timeout;
824
825 if (block) {
826 timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
827 timeout += 1000; /* one sec */
828 timer_mod(ssd->gl_unblock_timer, timeout);
829 } else {
830 timer_del(ssd->gl_unblock_timer);
831 }
474114b7
GH
832 graphic_hw_gl_block(ssd->dcl.con, block);
833}
834
835static void qemu_spice_gl_unblock_bh(void *opaque)
836{
837 SimpleSpiceDisplay *ssd = opaque;
838
839 qemu_spice_gl_block(ssd, false);
840}
841
8e388e90
GH
842static void qemu_spice_gl_block_timer(void *opaque)
843{
2ab4b135 844 warn_report("spice: no gl-draw-done within one second");
8e388e90
GH
845}
846
44231843
GH
847static void spice_gl_refresh(DisplayChangeListener *dcl)
848{
849 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
850 uint64_t cookie;
851
852 if (!ssd->ds || qemu_console_is_gl_blocked(ssd->dcl.con)) {
853 return;
854 }
855
856 graphic_hw_update(dcl->con);
857 if (ssd->gl_updates && ssd->have_surface) {
858 qemu_spice_gl_block(ssd, true);
859 cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
860 spice_qxl_gl_draw_async(&ssd->qxl, 0, 0,
861 surface_width(ssd->ds),
862 surface_height(ssd->ds),
863 cookie);
864 ssd->gl_updates = 0;
865 }
866}
867
868static void spice_gl_update(DisplayChangeListener *dcl,
869 int x, int y, int w, int h)
870{
871 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
872
873 surface_gl_update_texture(ssd->gls, ssd->ds, x, y, w, h);
874 ssd->gl_updates++;
875}
876
877static void spice_gl_switch(DisplayChangeListener *dcl,
878 struct DisplaySurface *new_surface)
879{
880 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
881 EGLint stride, fourcc;
882 int fd;
883
884 if (ssd->ds) {
885 surface_gl_destroy_texture(ssd->gls, ssd->ds);
886 }
887 ssd->ds = new_surface;
888 if (ssd->ds) {
889 surface_gl_create_texture(ssd->gls, ssd->ds);
890 fd = egl_get_fd_for_texture(ssd->ds->texture,
5fc1fb62
GH
891 &stride, &fourcc,
892 NULL);
44231843
GH
893 if (fd < 0) {
894 surface_gl_destroy_texture(ssd->gls, ssd->ds);
895 return;
896 }
897
e181f8bb
GH
898 trace_qemu_spice_gl_surface(ssd->qxl.id,
899 surface_width(ssd->ds),
900 surface_height(ssd->ds),
901 fourcc);
44231843
GH
902
903 /* note: spice server will close the fd */
904 spice_qxl_gl_scanout(&ssd->qxl, fd,
905 surface_width(ssd->ds),
906 surface_height(ssd->ds),
907 stride, fourcc, false);
908 ssd->have_surface = true;
909 ssd->have_scanout = false;
910
911 qemu_spice_gl_monitor_config(ssd, 0, 0,
912 surface_width(ssd->ds),
913 surface_height(ssd->ds));
914 }
915}
916
474114b7
GH
917static QEMUGLContext qemu_spice_gl_create_context(DisplayChangeListener *dcl,
918 QEMUGLParams *params)
919{
920 eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
921 qemu_egl_rn_ctx);
922 return qemu_egl_create_context(dcl, params);
923}
924
46ffd0c0
GH
925static void qemu_spice_gl_scanout_disable(DisplayChangeListener *dcl)
926{
927 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
928
e181f8bb 929 trace_qemu_spice_gl_scanout_disable(ssd->qxl.id);
46ffd0c0
GH
930 spice_qxl_gl_scanout(&ssd->qxl, -1, 0, 0, 0, 0, false);
931 qemu_spice_gl_monitor_config(ssd, 0, 0, 0, 0);
932 ssd->have_surface = false;
933 ssd->have_scanout = false;
934}
935
f4c36bda
GH
936static void qemu_spice_gl_scanout_texture(DisplayChangeListener *dcl,
937 uint32_t tex_id,
938 bool y_0_top,
939 uint32_t backing_width,
940 uint32_t backing_height,
941 uint32_t x, uint32_t y,
942 uint32_t w, uint32_t h)
474114b7
GH
943{
944 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
945 EGLint stride = 0, fourcc = 0;
946 int fd = -1;
947
46ffd0c0 948 assert(tex_id);
5fc1fb62 949 fd = egl_get_fd_for_texture(tex_id, &stride, &fourcc, NULL);
46ffd0c0
GH
950 if (fd < 0) {
951 fprintf(stderr, "%s: failed to get fd for texture\n", __func__);
952 return;
474114b7 953 }
e181f8bb 954 trace_qemu_spice_gl_scanout_texture(ssd->qxl.id, w, h, fourcc);
474114b7
GH
955
956 /* note: spice server will close the fd */
9d8256eb 957 spice_qxl_gl_scanout(&ssd->qxl, fd, backing_width, backing_height,
474114b7 958 stride, fourcc, y_0_top);
39414ef4 959 qemu_spice_gl_monitor_config(ssd, x, y, w, h);
46ffd0c0
GH
960 ssd->have_surface = false;
961 ssd->have_scanout = true;
474114b7
GH
962}
963
cd2452b2
GH
964static void qemu_spice_gl_scanout_dmabuf(DisplayChangeListener *dcl,
965 QemuDmaBuf *dmabuf)
966{
967 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
968
b153f901
GH
969 ssd->guest_dmabuf = dmabuf;
970 ssd->guest_dmabuf_refresh = true;
971
cd2452b2
GH
972 ssd->have_surface = false;
973 ssd->have_scanout = true;
974}
975
b153f901
GH
976static void qemu_spice_gl_cursor_dmabuf(DisplayChangeListener *dcl,
977 QemuDmaBuf *dmabuf, bool have_hot,
978 uint32_t hot_x, uint32_t hot_y)
979{
980 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
981
982 ssd->have_hot = have_hot;
983 ssd->hot_x = hot_x;
984 ssd->hot_y = hot_y;
985
986 trace_qemu_spice_gl_cursor(ssd->qxl.id, dmabuf != NULL, have_hot);
987 if (dmabuf) {
988 egl_dmabuf_import_texture(dmabuf);
989 if (!dmabuf->texture) {
990 return;
991 }
992 egl_fb_setup_for_tex(&ssd->cursor_fb, dmabuf->width, dmabuf->height,
993 dmabuf->texture, false);
994 } else {
995 egl_fb_destroy(&ssd->cursor_fb);
996 }
997}
998
999static void qemu_spice_gl_cursor_position(DisplayChangeListener *dcl,
1000 uint32_t pos_x, uint32_t pos_y)
1001{
1002 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
1003
2480f41e 1004 qemu_mutex_lock(&ssd->lock);
b153f901
GH
1005 ssd->ptr_x = pos_x;
1006 ssd->ptr_y = pos_y;
2480f41e 1007 qemu_mutex_unlock(&ssd->lock);
b153f901
GH
1008}
1009
1010static void qemu_spice_gl_release_dmabuf(DisplayChangeListener *dcl,
1011 QemuDmaBuf *dmabuf)
1012{
1013 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
1014
1015 if (ssd->guest_dmabuf == dmabuf) {
1016 ssd->guest_dmabuf = NULL;
1017 ssd->guest_dmabuf_refresh = false;
1018 }
1019 egl_dmabuf_release_texture(dmabuf);
1020}
1021
474114b7
GH
1022static void qemu_spice_gl_update(DisplayChangeListener *dcl,
1023 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
1024{
1025 SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
b153f901
GH
1026 EGLint stride = 0, fourcc = 0;
1027 bool render_cursor = false;
1028 bool y_0_top = false; /* FIXME */
474114b7 1029 uint64_t cookie;
b153f901 1030 int fd;
474114b7 1031
44231843
GH
1032 if (!ssd->have_scanout) {
1033 return;
1034 }
1035
b153f901
GH
1036 if (ssd->cursor_fb.texture) {
1037 render_cursor = true;
1038 }
1039 if (ssd->render_cursor != render_cursor) {
1040 ssd->render_cursor = render_cursor;
1041 ssd->guest_dmabuf_refresh = true;
1042 egl_fb_destroy(&ssd->blit_fb);
1043 }
1044
1045 if (ssd->guest_dmabuf_refresh) {
1046 QemuDmaBuf *dmabuf = ssd->guest_dmabuf;
1047 if (render_cursor) {
1048 egl_dmabuf_import_texture(dmabuf);
1049 if (!dmabuf->texture) {
1050 return;
1051 }
1052
1053 /* source framebuffer */
1054 egl_fb_setup_for_tex(&ssd->guest_fb,
1055 dmabuf->width, dmabuf->height,
1056 dmabuf->texture, false);
1057
1058 /* dest framebuffer */
1059 if (ssd->blit_fb.width != dmabuf->width ||
1060 ssd->blit_fb.height != dmabuf->height) {
1061 trace_qemu_spice_gl_render_dmabuf(ssd->qxl.id, dmabuf->width,
1062 dmabuf->height);
1063 egl_fb_destroy(&ssd->blit_fb);
1064 egl_fb_setup_new_tex(&ssd->blit_fb,
1065 dmabuf->width, dmabuf->height);
1066 fd = egl_get_fd_for_texture(ssd->blit_fb.texture,
5fc1fb62 1067 &stride, &fourcc, NULL);
b153f901
GH
1068 spice_qxl_gl_scanout(&ssd->qxl, fd,
1069 dmabuf->width, dmabuf->height,
1070 stride, fourcc, false);
1071 }
1072 } else {
1073 trace_qemu_spice_gl_forward_dmabuf(ssd->qxl.id,
1074 dmabuf->width, dmabuf->height);
1075 /* note: spice server will close the fd, so hand over a dup */
1076 spice_qxl_gl_scanout(&ssd->qxl, dup(dmabuf->fd),
1077 dmabuf->width, dmabuf->height,
242d0133
MAL
1078 dmabuf->stride, dmabuf->fourcc,
1079 dmabuf->y0_top);
b153f901
GH
1080 }
1081 qemu_spice_gl_monitor_config(ssd, 0, 0, dmabuf->width, dmabuf->height);
1082 ssd->guest_dmabuf_refresh = false;
1083 }
1084
1085 if (render_cursor) {
2480f41e
PB
1086 int x, y;
1087 qemu_mutex_lock(&ssd->lock);
1088 x = ssd->ptr_x;
1089 y = ssd->ptr_y;
1090 qemu_mutex_unlock(&ssd->lock);
b153f901
GH
1091 egl_texture_blit(ssd->gls, &ssd->blit_fb, &ssd->guest_fb,
1092 !y_0_top);
1093 egl_texture_blend(ssd->gls, &ssd->blit_fb, &ssd->cursor_fb,
051a0cde 1094 !y_0_top, x, y, 1.0, 1.0);
b153f901
GH
1095 glFlush();
1096 }
e181f8bb
GH
1097
1098 trace_qemu_spice_gl_update(ssd->qxl.id, w, h, x, y);
474114b7
GH
1099 qemu_spice_gl_block(ssd, true);
1100 cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
1101 spice_qxl_gl_draw_async(&ssd->qxl, x, y, w, h, cookie);
1102}
1103
1104static const DisplayChangeListenerOps display_listener_gl_ops = {
44231843
GH
1105 .dpy_name = "spice-egl",
1106 .dpy_gfx_update = spice_gl_update,
1107 .dpy_gfx_switch = spice_gl_switch,
1108 .dpy_gfx_check_format = console_gl_check_format,
1109 .dpy_refresh = spice_gl_refresh,
1110 .dpy_mouse_set = display_mouse_set,
1111 .dpy_cursor_define = display_mouse_define,
474114b7
GH
1112
1113 .dpy_gl_ctx_create = qemu_spice_gl_create_context,
1114 .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
1115 .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
1116 .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
1117
46ffd0c0 1118 .dpy_gl_scanout_disable = qemu_spice_gl_scanout_disable,
f4c36bda 1119 .dpy_gl_scanout_texture = qemu_spice_gl_scanout_texture,
cd2452b2 1120 .dpy_gl_scanout_dmabuf = qemu_spice_gl_scanout_dmabuf,
b153f901
GH
1121 .dpy_gl_cursor_dmabuf = qemu_spice_gl_cursor_dmabuf,
1122 .dpy_gl_cursor_position = qemu_spice_gl_cursor_position,
1123 .dpy_gl_release_dmabuf = qemu_spice_gl_release_dmabuf,
474114b7
GH
1124 .dpy_gl_update = qemu_spice_gl_update,
1125};
1126
1127#endif /* HAVE_SPICE_GL */
1128
9fa03286 1129static void qemu_spice_display_init_one(QemuConsole *con)
a3e22260 1130{
9c80a315
GH
1131 SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
1132
c78f7137 1133 qemu_spice_display_init_common(ssd);
a3e22260 1134
b5e751b5 1135 ssd->dcl.ops = &display_listener_ops;
474114b7 1136#ifdef HAVE_SPICE_GL
fe5c44f9 1137 if (spice_opengl) {
474114b7 1138 ssd->dcl.ops = &display_listener_gl_ops;
474114b7 1139 ssd->gl_unblock_bh = qemu_bh_new(qemu_spice_gl_unblock_bh, ssd);
8e388e90
GH
1140 ssd->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
1141 qemu_spice_gl_block_timer, ssd);
46e19e14 1142 ssd->gls = qemu_gl_init_shader();
44231843
GH
1143 ssd->have_surface = false;
1144 ssd->have_scanout = false;
474114b7
GH
1145 }
1146#endif
b5e751b5
GH
1147 ssd->dcl.con = con;
1148
9c80a315 1149 ssd->qxl.base.sif = &dpy_interface.base;
9fa03286 1150 qemu_spice_add_display_interface(&ssd->qxl, con);
be812c0a
LH
1151
1152#if SPICE_SERVER_VERSION >= 0x000e02 /* release 0.14.2 */
1153 char device_address[256] = "";
1154 if (qemu_spice_fill_device_address(con, device_address, 256)) {
1155 spice_qxl_set_device_info(&ssd->qxl,
1156 device_address,
1157 qemu_console_get_head(con),
1158 1);
1159 }
1160#endif
1161
9c80a315 1162 qemu_spice_create_host_memslot(ssd);
7c20b4a3 1163
5209089f 1164 register_displaychangelistener(&ssd->dcl);
a3e22260 1165}
9fa03286
GH
1166
1167void qemu_spice_display_init(void)
1168{
8bf69b49
GH
1169 QemuOptsList *olist = qemu_find_opts("spice");
1170 QemuOpts *opts = QTAILQ_FIRST(&olist->head);
1171 QemuConsole *spice_con, *con;
1172 const char *str;
9fa03286
GH
1173 int i;
1174
8bf69b49
GH
1175 str = qemu_opt_get(opts, "display");
1176 if (str) {
1177 int head = qemu_opt_get_number(opts, "head", 0);
1178 Error *err = NULL;
1179
1180 spice_con = qemu_console_lookup_by_device_name(str, head, &err);
1181 if (err) {
1182 error_report("Failed to lookup display/head");
1183 exit(1);
1184 }
1185 } else {
1186 spice_con = NULL;
1187 }
1188
9fa03286
GH
1189 for (i = 0;; i++) {
1190 con = qemu_console_lookup_by_index(i);
1191 if (!con || !qemu_console_is_graphic(con)) {
1192 break;
1193 }
1194 if (qemu_spice_have_display_interface(con)) {
1195 continue;
1196 }
8bf69b49
GH
1197 if (spice_con != NULL && spice_con != con) {
1198 continue;
1199 }
9fa03286
GH
1200 qemu_spice_display_init_one(con);
1201 }
1202}