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