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