]>
Commit | Line | Data |
---|---|---|
a3e22260 GH |
1 | /* |
2 | * Copyright (C) 2010 Red Hat, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License as | |
6 | * published by the Free Software Foundation; either version 2 or | |
7 | * (at your option) version 3 of the License. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
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 | 29 | bool spice_opengl; |
a3e22260 | 30 | |
a3e22260 GH |
31 | int qemu_spice_rect_is_empty(const QXLRect* r) |
32 | { | |
33 | return r->top == r->bottom || r->left == r->right; | |
34 | } | |
35 | ||
36 | void 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 |
53 | QXLCookie *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 |
63 | void 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 | ||
79 | void 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 | ||
85 | void 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 |
99 | void 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 |
112 | void 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 |
118 | static 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 |
186 | static 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 |
251 | static SimpleSpiceCursor* |
252 | qemu_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 | */ |
303 | void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update) | |
304 | { | |
7267c094 AL |
305 | g_free(update->bitmap); |
306 | g_free(update); | |
a3e22260 GH |
307 | } |
308 | ||
309 | void 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 | ||
319 | void 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 | ||
349 | void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd) | |
350 | { | |
5ff4e36c | 351 | qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC); |
a3e22260 GH |
352 | } |
353 | ||
c78f7137 | 354 | void 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 | ||
367 | void 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 |
384 | void 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 | ||
0b2824e5 | 453 | static void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd) |
a3e22260 | 454 | { |
07536094 | 455 | if (ssd->cursor) { |
284d1c6b GH |
456 | assert(ssd->dcl.con); |
457 | dpy_cursor_define(ssd->dcl.con, ssd->cursor); | |
07536094 GH |
458 | } |
459 | if (ssd->mouse_x != -1 && ssd->mouse_y != -1) { | |
284d1c6b GH |
460 | assert(ssd->dcl.con); |
461 | dpy_mouse_set(ssd->dcl.con, ssd->mouse_x, ssd->mouse_y, 1); | |
07536094 GH |
462 | ssd->mouse_x = -1; |
463 | ssd->mouse_y = -1; | |
464 | } | |
bb5a8cd5 AL |
465 | } |
466 | ||
0b2824e5 GH |
467 | void qemu_spice_cursor_refresh_bh(void *opaque) |
468 | { | |
469 | SimpleSpiceDisplay *ssd = opaque; | |
470 | ||
471 | qemu_mutex_lock(&ssd->lock); | |
472 | qemu_spice_cursor_refresh_unlocked(ssd); | |
473 | qemu_mutex_unlock(&ssd->lock); | |
474 | } | |
475 | ||
bb5a8cd5 AL |
476 | void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd) |
477 | { | |
284d1c6b | 478 | graphic_hw_update(ssd->dcl.con); |
bb5a8cd5 AL |
479 | |
480 | qemu_mutex_lock(&ssd->lock); | |
71874c17 | 481 | if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) { |
b1af98ba | 482 | qemu_spice_create_update(ssd); |
bb5a8cd5 AL |
483 | ssd->notify++; |
484 | } | |
e0c64d08 GH |
485 | qemu_mutex_unlock(&ssd->lock); |
486 | ||
e181f8bb | 487 | trace_qemu_spice_display_refresh(ssd->qxl.id, ssd->notify); |
a3e22260 GH |
488 | if (ssd->notify) { |
489 | ssd->notify = 0; | |
5c59d118 | 490 | qemu_spice_wakeup(ssd); |
a3e22260 GH |
491 | } |
492 | } | |
493 | ||
494 | /* spice display interface callbacks */ | |
495 | ||
496 | static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker) | |
497 | { | |
e181f8bb | 498 | /* nothing to do */ |
a3e22260 GH |
499 | } |
500 | ||
501 | static void interface_set_compression_level(QXLInstance *sin, int level) | |
502 | { | |
a3e22260 GH |
503 | /* nothing to do */ |
504 | } | |
505 | ||
015e02f8 | 506 | #if SPICE_NEEDS_SET_MM_TIME |
a3e22260 GH |
507 | static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time) |
508 | { | |
a3e22260 GH |
509 | /* nothing to do */ |
510 | } | |
015e02f8 | 511 | #endif |
a3e22260 GH |
512 | |
513 | static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info) | |
514 | { | |
515 | SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl); | |
516 | ||
517 | info->memslot_gen_bits = MEMSLOT_GENERATION_BITS; | |
518 | info->memslot_id_bits = MEMSLOT_SLOT_BITS; | |
519 | info->num_memslots = NUM_MEMSLOTS; | |
520 | info->num_memslots_groups = NUM_MEMSLOTS_GROUPS; | |
521 | info->internal_groupslot_id = 0; | |
ab9509cc | 522 | info->qxl_ram_size = 16 * 1024 * 1024; |
ddd8fdc7 | 523 | info->n_surfaces = ssd->num_surfaces; |
a3e22260 GH |
524 | } |
525 | ||
c9f88ce3 | 526 | static int interface_get_command(QXLInstance *sin, QXLCommandExt *ext) |
a3e22260 GH |
527 | { |
528 | SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl); | |
529 | SimpleSpiceUpdate *update; | |
e0c64d08 | 530 | int ret = false; |
a3e22260 | 531 | |
e0c64d08 | 532 | qemu_mutex_lock(&ssd->lock); |
b1af98ba GH |
533 | update = QTAILQ_FIRST(&ssd->updates); |
534 | if (update != NULL) { | |
535 | QTAILQ_REMOVE(&ssd->updates, update, next); | |
e0c64d08 GH |
536 | *ext = update->ext; |
537 | ret = true; | |
a3e22260 | 538 | } |
e0c64d08 GH |
539 | qemu_mutex_unlock(&ssd->lock); |
540 | ||
541 | return ret; | |
a3e22260 GH |
542 | } |
543 | ||
544 | static int interface_req_cmd_notification(QXLInstance *sin) | |
545 | { | |
a3e22260 GH |
546 | return 1; |
547 | } | |
548 | ||
549 | static void interface_release_resource(QXLInstance *sin, | |
c9f88ce3 | 550 | QXLReleaseInfoExt rext) |
a3e22260 GH |
551 | { |
552 | SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl); | |
5643fc01 GH |
553 | SimpleSpiceUpdate *update; |
554 | SimpleSpiceCursor *cursor; | |
555 | QXLCommandExt *ext; | |
a3e22260 | 556 | |
e8e23b7d | 557 | ext = (void *)(intptr_t)(rext.info->id); |
5643fc01 GH |
558 | switch (ext->cmd.type) { |
559 | case QXL_CMD_DRAW: | |
560 | update = container_of(ext, SimpleSpiceUpdate, ext); | |
561 | qemu_spice_destroy_update(ssd, update); | |
562 | break; | |
563 | case QXL_CMD_CURSOR: | |
564 | cursor = container_of(ext, SimpleSpiceCursor, ext); | |
565 | g_free(cursor); | |
566 | break; | |
567 | default: | |
568 | g_assert_not_reached(); | |
569 | } | |
a3e22260 GH |
570 | } |
571 | ||
c9f88ce3 | 572 | static int interface_get_cursor_command(QXLInstance *sin, QXLCommandExt *ext) |
a3e22260 | 573 | { |
5643fc01 GH |
574 | SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl); |
575 | int ret; | |
576 | ||
5643fc01 GH |
577 | qemu_mutex_lock(&ssd->lock); |
578 | if (ssd->ptr_define) { | |
579 | *ext = ssd->ptr_define->ext; | |
580 | ssd->ptr_define = NULL; | |
581 | ret = true; | |
582 | } else if (ssd->ptr_move) { | |
583 | *ext = ssd->ptr_move->ext; | |
584 | ssd->ptr_move = NULL; | |
585 | ret = true; | |
586 | } else { | |
587 | ret = false; | |
588 | } | |
589 | qemu_mutex_unlock(&ssd->lock); | |
590 | return ret; | |
a3e22260 GH |
591 | } |
592 | ||
593 | static int interface_req_cursor_notification(QXLInstance *sin) | |
594 | { | |
a3e22260 GH |
595 | return 1; |
596 | } | |
597 | ||
598 | static void interface_notify_update(QXLInstance *sin, uint32_t update_id) | |
599 | { | |
a89f364a | 600 | fprintf(stderr, "%s: abort()\n", __func__); |
a3e22260 GH |
601 | abort(); |
602 | } | |
603 | ||
604 | static int interface_flush_resources(QXLInstance *sin) | |
605 | { | |
a89f364a | 606 | fprintf(stderr, "%s: abort()\n", __func__); |
a3e22260 GH |
607 | abort(); |
608 | return 0; | |
609 | } | |
610 | ||
21a50d0b GH |
611 | static void interface_update_area_complete(QXLInstance *sin, |
612 | uint32_t surface_id, | |
613 | QXLRect *dirty, uint32_t num_updated_rects) | |
614 | { | |
615 | /* should never be called, used in qxl native mode only */ | |
616 | fprintf(stderr, "%s: abort()\n", __func__); | |
617 | abort(); | |
618 | } | |
619 | ||
620 | /* called from spice server thread context only */ | |
621 | static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token) | |
622 | { | |
474114b7 GH |
623 | QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token; |
624 | ||
625 | switch (cookie->type) { | |
626 | #ifdef HAVE_SPICE_GL | |
627 | case QXL_COOKIE_TYPE_GL_DRAW_DONE: | |
628 | { | |
629 | SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl); | |
630 | qemu_bh_schedule(ssd->gl_unblock_bh); | |
631 | break; | |
632 | } | |
39414ef4 GH |
633 | case QXL_COOKIE_TYPE_IO: |
634 | if (cookie->io == QXL_IO_MONITORS_CONFIG_ASYNC) { | |
635 | g_free(cookie->u.data); | |
636 | } | |
637 | break; | |
474114b7 GH |
638 | #endif |
639 | default: | |
640 | /* should never be called, used in qxl native mode only */ | |
641 | fprintf(stderr, "%s: abort()\n", __func__); | |
642 | abort(); | |
643 | } | |
644 | g_free(cookie); | |
21a50d0b GH |
645 | } |
646 | ||
647 | static void interface_set_client_capabilities(QXLInstance *sin, | |
648 | uint8_t client_present, | |
649 | uint8_t caps[58]) | |
650 | { | |
e181f8bb | 651 | /* nothing to do */ |
21a50d0b GH |
652 | } |
653 | ||
654 | static int interface_client_monitors_config(QXLInstance *sin, | |
9b74d0d5 | 655 | VDAgentMonitorsConfig *mc) |
21a50d0b | 656 | { |
9b74d0d5 GH |
657 | SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl); |
658 | QemuUIInfo info; | |
c61d8126 | 659 | int head; |
5a9259a0 GH |
660 | |
661 | if (!dpy_ui_info_supported(ssd->dcl.con)) { | |
662 | return 0; /* == not supported by guest */ | |
663 | } | |
9b74d0d5 | 664 | |
dc491cfc GH |
665 | if (!mc) { |
666 | return 1; | |
667 | } | |
668 | ||
9b74d0d5 | 669 | memset(&info, 0, sizeof(info)); |
c61d8126 MAL |
670 | |
671 | head = qemu_console_get_head(ssd->dcl.con); | |
672 | if (mc->num_of_monitors > head) { | |
673 | info.width = mc->monitors[head].width; | |
674 | info.height = mc->monitors[head].height; | |
9b74d0d5 | 675 | } |
e181f8bb GH |
676 | |
677 | trace_qemu_spice_ui_info(ssd->qxl.id, info.width, info.height); | |
5a9259a0 | 678 | dpy_set_ui_info(ssd->dcl.con, &info); |
5a9259a0 | 679 | return 1; |
21a50d0b GH |
680 | } |
681 | ||
a3e22260 GH |
682 | static const QXLInterface dpy_interface = { |
683 | .base.type = SPICE_INTERFACE_QXL, | |
684 | .base.description = "qemu simple display", | |
685 | .base.major_version = SPICE_INTERFACE_QXL_MAJOR, | |
686 | .base.minor_version = SPICE_INTERFACE_QXL_MINOR, | |
687 | ||
688 | .attache_worker = interface_attach_worker, | |
689 | .set_compression_level = interface_set_compression_level, | |
015e02f8 | 690 | #if SPICE_NEEDS_SET_MM_TIME |
a3e22260 | 691 | .set_mm_time = interface_set_mm_time, |
015e02f8 | 692 | #endif |
a3e22260 GH |
693 | .get_init_info = interface_get_init_info, |
694 | ||
695 | /* the callbacks below are called from spice server thread context */ | |
696 | .get_command = interface_get_command, | |
697 | .req_cmd_notification = interface_req_cmd_notification, | |
698 | .release_resource = interface_release_resource, | |
699 | .get_cursor_command = interface_get_cursor_command, | |
700 | .req_cursor_notification = interface_req_cursor_notification, | |
701 | .notify_update = interface_notify_update, | |
702 | .flush_resources = interface_flush_resources, | |
21a50d0b GH |
703 | .async_complete = interface_async_complete, |
704 | .update_area_complete = interface_update_area_complete, | |
705 | .set_client_capabilities = interface_set_client_capabilities, | |
706 | .client_monitors_config = interface_client_monitors_config, | |
a3e22260 GH |
707 | }; |
708 | ||
7c20b4a3 | 709 | static void display_update(DisplayChangeListener *dcl, |
7c20b4a3 | 710 | int x, int y, int w, int h) |
a3e22260 | 711 | { |
9c80a315 GH |
712 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); |
713 | qemu_spice_display_update(ssd, x, y, w, h); | |
a3e22260 GH |
714 | } |
715 | ||
c12aeb86 | 716 | static void display_switch(DisplayChangeListener *dcl, |
9425c004 | 717 | DisplaySurface *surface) |
a3e22260 | 718 | { |
9c80a315 | 719 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); |
c12aeb86 | 720 | qemu_spice_display_switch(ssd, surface); |
a3e22260 GH |
721 | } |
722 | ||
bc2ed970 | 723 | static void display_refresh(DisplayChangeListener *dcl) |
a3e22260 | 724 | { |
9c80a315 GH |
725 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); |
726 | qemu_spice_display_refresh(ssd); | |
a3e22260 GH |
727 | } |
728 | ||
5643fc01 GH |
729 | static void display_mouse_set(DisplayChangeListener *dcl, |
730 | int x, int y, int on) | |
731 | { | |
732 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); | |
733 | ||
734 | qemu_mutex_lock(&ssd->lock); | |
735 | ssd->ptr_x = x; | |
d0df04a1 | 736 | ssd->ptr_y = y; |
ef1e1e07 | 737 | g_free(ssd->ptr_move); |
700cd855 | 738 | ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL, on); |
5643fc01 | 739 | qemu_mutex_unlock(&ssd->lock); |
51e0b654 | 740 | qemu_spice_wakeup(ssd); |
5643fc01 GH |
741 | } |
742 | ||
743 | static void display_mouse_define(DisplayChangeListener *dcl, | |
744 | QEMUCursor *c) | |
745 | { | |
746 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); | |
747 | ||
748 | qemu_mutex_lock(&ssd->lock); | |
28f4a708 | 749 | cursor_get(c); |
58c7b618 MAL |
750 | cursor_put(ssd->cursor); |
751 | ssd->cursor = c; | |
dc8dceee MAL |
752 | ssd->hot_x = c->hot_x; |
753 | ssd->hot_y = c->hot_y; | |
ef1e1e07 DB |
754 | g_free(ssd->ptr_move); |
755 | ssd->ptr_move = NULL; | |
756 | g_free(ssd->ptr_define); | |
700cd855 | 757 | ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c, 0); |
5643fc01 | 758 | qemu_mutex_unlock(&ssd->lock); |
51e0b654 | 759 | qemu_spice_wakeup(ssd); |
5643fc01 GH |
760 | } |
761 | ||
7c20b4a3 | 762 | static const DisplayChangeListenerOps display_listener_ops = { |
0002a518 GH |
763 | .dpy_name = "spice", |
764 | .dpy_gfx_update = display_update, | |
765 | .dpy_gfx_switch = display_switch, | |
766 | .dpy_gfx_check_format = qemu_pixman_check_format, | |
767 | .dpy_refresh = display_refresh, | |
768 | .dpy_mouse_set = display_mouse_set, | |
769 | .dpy_cursor_define = display_mouse_define, | |
a3e22260 GH |
770 | }; |
771 | ||
474114b7 GH |
772 | #ifdef HAVE_SPICE_GL |
773 | ||
39414ef4 GH |
774 | static void qemu_spice_gl_monitor_config(SimpleSpiceDisplay *ssd, |
775 | int x, int y, int w, int h) | |
776 | { | |
777 | QXLMonitorsConfig *config; | |
778 | QXLCookie *cookie; | |
779 | ||
780 | config = g_malloc0(sizeof(QXLMonitorsConfig) + sizeof(QXLHead)); | |
781 | config->count = 1; | |
782 | config->max_allowed = 1; | |
783 | config->heads[0].x = x; | |
784 | config->heads[0].y = y; | |
785 | config->heads[0].width = w; | |
786 | config->heads[0].height = h; | |
787 | cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO, | |
788 | QXL_IO_MONITORS_CONFIG_ASYNC); | |
789 | cookie->u.data = config; | |
790 | ||
791 | spice_qxl_monitors_config_async(&ssd->qxl, | |
792 | (uintptr_t)config, | |
793 | MEMSLOT_GROUP_HOST, | |
794 | (uintptr_t)cookie); | |
795 | } | |
796 | ||
474114b7 GH |
797 | static void qemu_spice_gl_block(SimpleSpiceDisplay *ssd, bool block) |
798 | { | |
8e388e90 GH |
799 | uint64_t timeout; |
800 | ||
801 | if (block) { | |
802 | timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); | |
803 | timeout += 1000; /* one sec */ | |
804 | timer_mod(ssd->gl_unblock_timer, timeout); | |
805 | } else { | |
806 | timer_del(ssd->gl_unblock_timer); | |
807 | } | |
474114b7 GH |
808 | graphic_hw_gl_block(ssd->dcl.con, block); |
809 | } | |
810 | ||
811 | static void qemu_spice_gl_unblock_bh(void *opaque) | |
812 | { | |
813 | SimpleSpiceDisplay *ssd = opaque; | |
814 | ||
815 | qemu_spice_gl_block(ssd, false); | |
816 | } | |
817 | ||
8e388e90 GH |
818 | static void qemu_spice_gl_block_timer(void *opaque) |
819 | { | |
2ab4b135 | 820 | warn_report("spice: no gl-draw-done within one second"); |
8e388e90 GH |
821 | } |
822 | ||
44231843 GH |
823 | static void spice_gl_refresh(DisplayChangeListener *dcl) |
824 | { | |
825 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); | |
826 | uint64_t cookie; | |
827 | ||
828 | if (!ssd->ds || qemu_console_is_gl_blocked(ssd->dcl.con)) { | |
829 | return; | |
830 | } | |
831 | ||
832 | graphic_hw_update(dcl->con); | |
833 | if (ssd->gl_updates && ssd->have_surface) { | |
834 | qemu_spice_gl_block(ssd, true); | |
835 | cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0); | |
836 | spice_qxl_gl_draw_async(&ssd->qxl, 0, 0, | |
837 | surface_width(ssd->ds), | |
838 | surface_height(ssd->ds), | |
839 | cookie); | |
840 | ssd->gl_updates = 0; | |
841 | } | |
842 | } | |
843 | ||
844 | static void spice_gl_update(DisplayChangeListener *dcl, | |
845 | int x, int y, int w, int h) | |
846 | { | |
847 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); | |
848 | ||
849 | surface_gl_update_texture(ssd->gls, ssd->ds, x, y, w, h); | |
850 | ssd->gl_updates++; | |
851 | } | |
852 | ||
853 | static void spice_gl_switch(DisplayChangeListener *dcl, | |
854 | struct DisplaySurface *new_surface) | |
855 | { | |
856 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); | |
857 | EGLint stride, fourcc; | |
858 | int fd; | |
859 | ||
860 | if (ssd->ds) { | |
861 | surface_gl_destroy_texture(ssd->gls, ssd->ds); | |
862 | } | |
863 | ssd->ds = new_surface; | |
864 | if (ssd->ds) { | |
865 | surface_gl_create_texture(ssd->gls, ssd->ds); | |
866 | fd = egl_get_fd_for_texture(ssd->ds->texture, | |
867 | &stride, &fourcc); | |
868 | if (fd < 0) { | |
869 | surface_gl_destroy_texture(ssd->gls, ssd->ds); | |
870 | return; | |
871 | } | |
872 | ||
e181f8bb GH |
873 | trace_qemu_spice_gl_surface(ssd->qxl.id, |
874 | surface_width(ssd->ds), | |
875 | surface_height(ssd->ds), | |
876 | fourcc); | |
44231843 GH |
877 | |
878 | /* note: spice server will close the fd */ | |
879 | spice_qxl_gl_scanout(&ssd->qxl, fd, | |
880 | surface_width(ssd->ds), | |
881 | surface_height(ssd->ds), | |
882 | stride, fourcc, false); | |
883 | ssd->have_surface = true; | |
884 | ssd->have_scanout = false; | |
885 | ||
886 | qemu_spice_gl_monitor_config(ssd, 0, 0, | |
887 | surface_width(ssd->ds), | |
888 | surface_height(ssd->ds)); | |
889 | } | |
890 | } | |
891 | ||
474114b7 GH |
892 | static QEMUGLContext qemu_spice_gl_create_context(DisplayChangeListener *dcl, |
893 | QEMUGLParams *params) | |
894 | { | |
895 | eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, | |
896 | qemu_egl_rn_ctx); | |
897 | return qemu_egl_create_context(dcl, params); | |
898 | } | |
899 | ||
46ffd0c0 GH |
900 | static void qemu_spice_gl_scanout_disable(DisplayChangeListener *dcl) |
901 | { | |
902 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); | |
903 | ||
e181f8bb | 904 | trace_qemu_spice_gl_scanout_disable(ssd->qxl.id); |
46ffd0c0 GH |
905 | spice_qxl_gl_scanout(&ssd->qxl, -1, 0, 0, 0, 0, false); |
906 | qemu_spice_gl_monitor_config(ssd, 0, 0, 0, 0); | |
907 | ssd->have_surface = false; | |
908 | ssd->have_scanout = false; | |
909 | } | |
910 | ||
f4c36bda GH |
911 | static void qemu_spice_gl_scanout_texture(DisplayChangeListener *dcl, |
912 | uint32_t tex_id, | |
913 | bool y_0_top, | |
914 | uint32_t backing_width, | |
915 | uint32_t backing_height, | |
916 | uint32_t x, uint32_t y, | |
917 | uint32_t w, uint32_t h) | |
474114b7 GH |
918 | { |
919 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); | |
920 | EGLint stride = 0, fourcc = 0; | |
921 | int fd = -1; | |
922 | ||
46ffd0c0 GH |
923 | assert(tex_id); |
924 | fd = egl_get_fd_for_texture(tex_id, &stride, &fourcc); | |
925 | if (fd < 0) { | |
926 | fprintf(stderr, "%s: failed to get fd for texture\n", __func__); | |
927 | return; | |
474114b7 | 928 | } |
e181f8bb | 929 | trace_qemu_spice_gl_scanout_texture(ssd->qxl.id, w, h, fourcc); |
474114b7 GH |
930 | |
931 | /* note: spice server will close the fd */ | |
9d8256eb | 932 | spice_qxl_gl_scanout(&ssd->qxl, fd, backing_width, backing_height, |
474114b7 | 933 | stride, fourcc, y_0_top); |
39414ef4 | 934 | qemu_spice_gl_monitor_config(ssd, x, y, w, h); |
46ffd0c0 GH |
935 | ssd->have_surface = false; |
936 | ssd->have_scanout = true; | |
474114b7 GH |
937 | } |
938 | ||
cd2452b2 GH |
939 | static void qemu_spice_gl_scanout_dmabuf(DisplayChangeListener *dcl, |
940 | QemuDmaBuf *dmabuf) | |
941 | { | |
942 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); | |
943 | ||
b153f901 GH |
944 | ssd->guest_dmabuf = dmabuf; |
945 | ssd->guest_dmabuf_refresh = true; | |
946 | ||
cd2452b2 GH |
947 | ssd->have_surface = false; |
948 | ssd->have_scanout = true; | |
949 | } | |
950 | ||
b153f901 GH |
951 | static void qemu_spice_gl_cursor_dmabuf(DisplayChangeListener *dcl, |
952 | QemuDmaBuf *dmabuf, bool have_hot, | |
953 | uint32_t hot_x, uint32_t hot_y) | |
954 | { | |
955 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); | |
956 | ||
957 | ssd->have_hot = have_hot; | |
958 | ssd->hot_x = hot_x; | |
959 | ssd->hot_y = hot_y; | |
960 | ||
961 | trace_qemu_spice_gl_cursor(ssd->qxl.id, dmabuf != NULL, have_hot); | |
962 | if (dmabuf) { | |
963 | egl_dmabuf_import_texture(dmabuf); | |
964 | if (!dmabuf->texture) { | |
965 | return; | |
966 | } | |
967 | egl_fb_setup_for_tex(&ssd->cursor_fb, dmabuf->width, dmabuf->height, | |
968 | dmabuf->texture, false); | |
969 | } else { | |
970 | egl_fb_destroy(&ssd->cursor_fb); | |
971 | } | |
972 | } | |
973 | ||
974 | static void qemu_spice_gl_cursor_position(DisplayChangeListener *dcl, | |
975 | uint32_t pos_x, uint32_t pos_y) | |
976 | { | |
977 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); | |
978 | ||
979 | ssd->ptr_x = pos_x; | |
980 | ssd->ptr_y = pos_y; | |
981 | } | |
982 | ||
983 | static void qemu_spice_gl_release_dmabuf(DisplayChangeListener *dcl, | |
984 | QemuDmaBuf *dmabuf) | |
985 | { | |
986 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); | |
987 | ||
988 | if (ssd->guest_dmabuf == dmabuf) { | |
989 | ssd->guest_dmabuf = NULL; | |
990 | ssd->guest_dmabuf_refresh = false; | |
991 | } | |
992 | egl_dmabuf_release_texture(dmabuf); | |
993 | } | |
994 | ||
474114b7 GH |
995 | static void qemu_spice_gl_update(DisplayChangeListener *dcl, |
996 | uint32_t x, uint32_t y, uint32_t w, uint32_t h) | |
997 | { | |
998 | SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl); | |
b153f901 GH |
999 | EGLint stride = 0, fourcc = 0; |
1000 | bool render_cursor = false; | |
1001 | bool y_0_top = false; /* FIXME */ | |
474114b7 | 1002 | uint64_t cookie; |
b153f901 | 1003 | int fd; |
474114b7 | 1004 | |
44231843 GH |
1005 | if (!ssd->have_scanout) { |
1006 | return; | |
1007 | } | |
1008 | ||
b153f901 GH |
1009 | if (ssd->cursor_fb.texture) { |
1010 | render_cursor = true; | |
1011 | } | |
1012 | if (ssd->render_cursor != render_cursor) { | |
1013 | ssd->render_cursor = render_cursor; | |
1014 | ssd->guest_dmabuf_refresh = true; | |
1015 | egl_fb_destroy(&ssd->blit_fb); | |
1016 | } | |
1017 | ||
1018 | if (ssd->guest_dmabuf_refresh) { | |
1019 | QemuDmaBuf *dmabuf = ssd->guest_dmabuf; | |
1020 | if (render_cursor) { | |
1021 | egl_dmabuf_import_texture(dmabuf); | |
1022 | if (!dmabuf->texture) { | |
1023 | return; | |
1024 | } | |
1025 | ||
1026 | /* source framebuffer */ | |
1027 | egl_fb_setup_for_tex(&ssd->guest_fb, | |
1028 | dmabuf->width, dmabuf->height, | |
1029 | dmabuf->texture, false); | |
1030 | ||
1031 | /* dest framebuffer */ | |
1032 | if (ssd->blit_fb.width != dmabuf->width || | |
1033 | ssd->blit_fb.height != dmabuf->height) { | |
1034 | trace_qemu_spice_gl_render_dmabuf(ssd->qxl.id, dmabuf->width, | |
1035 | dmabuf->height); | |
1036 | egl_fb_destroy(&ssd->blit_fb); | |
1037 | egl_fb_setup_new_tex(&ssd->blit_fb, | |
1038 | dmabuf->width, dmabuf->height); | |
1039 | fd = egl_get_fd_for_texture(ssd->blit_fb.texture, | |
1040 | &stride, &fourcc); | |
1041 | spice_qxl_gl_scanout(&ssd->qxl, fd, | |
1042 | dmabuf->width, dmabuf->height, | |
1043 | stride, fourcc, false); | |
1044 | } | |
1045 | } else { | |
1046 | trace_qemu_spice_gl_forward_dmabuf(ssd->qxl.id, | |
1047 | dmabuf->width, dmabuf->height); | |
1048 | /* note: spice server will close the fd, so hand over a dup */ | |
1049 | spice_qxl_gl_scanout(&ssd->qxl, dup(dmabuf->fd), | |
1050 | dmabuf->width, dmabuf->height, | |
1051 | dmabuf->stride, dmabuf->fourcc, false); | |
1052 | } | |
1053 | qemu_spice_gl_monitor_config(ssd, 0, 0, dmabuf->width, dmabuf->height); | |
1054 | ssd->guest_dmabuf_refresh = false; | |
1055 | } | |
1056 | ||
1057 | if (render_cursor) { | |
1058 | egl_texture_blit(ssd->gls, &ssd->blit_fb, &ssd->guest_fb, | |
1059 | !y_0_top); | |
1060 | egl_texture_blend(ssd->gls, &ssd->blit_fb, &ssd->cursor_fb, | |
1061 | !y_0_top, ssd->ptr_x, ssd->ptr_y); | |
1062 | glFlush(); | |
1063 | } | |
e181f8bb GH |
1064 | |
1065 | trace_qemu_spice_gl_update(ssd->qxl.id, w, h, x, y); | |
474114b7 GH |
1066 | qemu_spice_gl_block(ssd, true); |
1067 | cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0); | |
1068 | spice_qxl_gl_draw_async(&ssd->qxl, x, y, w, h, cookie); | |
1069 | } | |
1070 | ||
1071 | static const DisplayChangeListenerOps display_listener_gl_ops = { | |
44231843 GH |
1072 | .dpy_name = "spice-egl", |
1073 | .dpy_gfx_update = spice_gl_update, | |
1074 | .dpy_gfx_switch = spice_gl_switch, | |
1075 | .dpy_gfx_check_format = console_gl_check_format, | |
1076 | .dpy_refresh = spice_gl_refresh, | |
1077 | .dpy_mouse_set = display_mouse_set, | |
1078 | .dpy_cursor_define = display_mouse_define, | |
474114b7 GH |
1079 | |
1080 | .dpy_gl_ctx_create = qemu_spice_gl_create_context, | |
1081 | .dpy_gl_ctx_destroy = qemu_egl_destroy_context, | |
1082 | .dpy_gl_ctx_make_current = qemu_egl_make_context_current, | |
1083 | .dpy_gl_ctx_get_current = qemu_egl_get_current_context, | |
1084 | ||
46ffd0c0 | 1085 | .dpy_gl_scanout_disable = qemu_spice_gl_scanout_disable, |
f4c36bda | 1086 | .dpy_gl_scanout_texture = qemu_spice_gl_scanout_texture, |
cd2452b2 | 1087 | .dpy_gl_scanout_dmabuf = qemu_spice_gl_scanout_dmabuf, |
b153f901 GH |
1088 | .dpy_gl_cursor_dmabuf = qemu_spice_gl_cursor_dmabuf, |
1089 | .dpy_gl_cursor_position = qemu_spice_gl_cursor_position, | |
1090 | .dpy_gl_release_dmabuf = qemu_spice_gl_release_dmabuf, | |
474114b7 GH |
1091 | .dpy_gl_update = qemu_spice_gl_update, |
1092 | }; | |
1093 | ||
1094 | #endif /* HAVE_SPICE_GL */ | |
1095 | ||
9fa03286 | 1096 | static void qemu_spice_display_init_one(QemuConsole *con) |
a3e22260 | 1097 | { |
9c80a315 GH |
1098 | SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1); |
1099 | ||
c78f7137 | 1100 | qemu_spice_display_init_common(ssd); |
a3e22260 | 1101 | |
b5e751b5 | 1102 | ssd->dcl.ops = &display_listener_ops; |
474114b7 | 1103 | #ifdef HAVE_SPICE_GL |
fe5c44f9 | 1104 | if (spice_opengl) { |
474114b7 | 1105 | ssd->dcl.ops = &display_listener_gl_ops; |
474114b7 | 1106 | ssd->gl_unblock_bh = qemu_bh_new(qemu_spice_gl_unblock_bh, ssd); |
8e388e90 GH |
1107 | ssd->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME, |
1108 | qemu_spice_gl_block_timer, ssd); | |
46e19e14 | 1109 | ssd->gls = qemu_gl_init_shader(); |
44231843 GH |
1110 | ssd->have_surface = false; |
1111 | ssd->have_scanout = false; | |
474114b7 GH |
1112 | } |
1113 | #endif | |
b5e751b5 GH |
1114 | ssd->dcl.con = con; |
1115 | ||
9c80a315 | 1116 | ssd->qxl.base.sif = &dpy_interface.base; |
9fa03286 | 1117 | qemu_spice_add_display_interface(&ssd->qxl, con); |
9c80a315 | 1118 | qemu_spice_create_host_memslot(ssd); |
7c20b4a3 | 1119 | |
5209089f | 1120 | register_displaychangelistener(&ssd->dcl); |
a3e22260 | 1121 | } |
9fa03286 GH |
1122 | |
1123 | void qemu_spice_display_init(void) | |
1124 | { | |
8bf69b49 GH |
1125 | QemuOptsList *olist = qemu_find_opts("spice"); |
1126 | QemuOpts *opts = QTAILQ_FIRST(&olist->head); | |
1127 | QemuConsole *spice_con, *con; | |
1128 | const char *str; | |
9fa03286 GH |
1129 | int i; |
1130 | ||
8bf69b49 GH |
1131 | str = qemu_opt_get(opts, "display"); |
1132 | if (str) { | |
1133 | int head = qemu_opt_get_number(opts, "head", 0); | |
1134 | Error *err = NULL; | |
1135 | ||
1136 | spice_con = qemu_console_lookup_by_device_name(str, head, &err); | |
1137 | if (err) { | |
1138 | error_report("Failed to lookup display/head"); | |
1139 | exit(1); | |
1140 | } | |
1141 | } else { | |
1142 | spice_con = NULL; | |
1143 | } | |
1144 | ||
9fa03286 GH |
1145 | for (i = 0;; i++) { |
1146 | con = qemu_console_lookup_by_index(i); | |
1147 | if (!con || !qemu_console_is_graphic(con)) { | |
1148 | break; | |
1149 | } | |
1150 | if (qemu_spice_have_display_interface(con)) { | |
1151 | continue; | |
1152 | } | |
8bf69b49 GH |
1153 | if (spice_con != NULL && spice_con != con) { |
1154 | continue; | |
1155 | } | |
9fa03286 GH |
1156 | qemu_spice_display_init_one(con); |
1157 | } | |
1158 | } |