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