spice-display.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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. #include "qemu-common.h"
  18. #include "ui/qemu-spice.h"
  19. #include "qemu/timer.h"
  20. #include "qemu/queue.h"
  21. #include "monitor/monitor.h"
  22. #include "ui/console.h"
  23. #include "sysemu/sysemu.h"
  24. #include "trace.h"
  25. #include "ui/spice-display.h"
  26. static int debug = 0;
  27. static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
  28. {
  29. va_list args;
  30. if (level <= debug) {
  31. va_start(args, fmt);
  32. vfprintf(stderr, fmt, args);
  33. va_end(args);
  34. }
  35. }
  36. int qemu_spice_rect_is_empty(const QXLRect* r)
  37. {
  38. return r->top == r->bottom || r->left == r->right;
  39. }
  40. void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
  41. {
  42. if (qemu_spice_rect_is_empty(r)) {
  43. return;
  44. }
  45. if (qemu_spice_rect_is_empty(dest)) {
  46. *dest = *r;
  47. return;
  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. QXLCookie *qxl_cookie_new(int type, uint64_t io)
  55. {
  56. QXLCookie *cookie;
  57. cookie = g_malloc0(sizeof(*cookie));
  58. cookie->type = type;
  59. cookie->io = io;
  60. return cookie;
  61. }
  62. void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
  63. qxl_async_io async)
  64. {
  65. trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
  66. memslot->virt_start, memslot->virt_end,
  67. async);
  68. if (async != QXL_SYNC) {
  69. spice_qxl_add_memslot_async(&ssd->qxl, memslot,
  70. (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
  71. QXL_IO_MEMSLOT_ADD_ASYNC));
  72. } else {
  73. spice_qxl_add_memslot(&ssd->qxl, memslot);
  74. }
  75. }
  76. void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
  77. {
  78. trace_qemu_spice_del_memslot(ssd->qxl.id, gid, sid);
  79. spice_qxl_del_memslot(&ssd->qxl, gid, sid);
  80. }
  81. void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
  82. QXLDevSurfaceCreate *surface,
  83. qxl_async_io async)
  84. {
  85. trace_qemu_spice_create_primary_surface(ssd->qxl.id, id, surface, async);
  86. if (async != QXL_SYNC) {
  87. spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface,
  88. (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
  89. QXL_IO_CREATE_PRIMARY_ASYNC));
  90. } else {
  91. spice_qxl_create_primary_surface(&ssd->qxl, id, surface);
  92. }
  93. }
  94. void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
  95. uint32_t id, qxl_async_io async)
  96. {
  97. trace_qemu_spice_destroy_primary_surface(ssd->qxl.id, id, async);
  98. if (async != QXL_SYNC) {
  99. spice_qxl_destroy_primary_surface_async(&ssd->qxl, id,
  100. (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
  101. QXL_IO_DESTROY_PRIMARY_ASYNC));
  102. } else {
  103. spice_qxl_destroy_primary_surface(&ssd->qxl, id);
  104. }
  105. }
  106. void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
  107. {
  108. trace_qemu_spice_wakeup(ssd->qxl.id);
  109. spice_qxl_wakeup(&ssd->qxl);
  110. }
  111. static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
  112. QXLRect *rect)
  113. {
  114. SimpleSpiceUpdate *update;
  115. QXLDrawable *drawable;
  116. QXLImage *image;
  117. QXLCommand *cmd;
  118. int bw, bh;
  119. struct timespec time_space;
  120. pixman_image_t *dest;
  121. trace_qemu_spice_create_update(
  122. rect->left, rect->right,
  123. rect->top, rect->bottom);
  124. update = g_malloc0(sizeof(*update));
  125. drawable = &update->drawable;
  126. image = &update->image;
  127. cmd = &update->ext.cmd;
  128. bw = rect->right - rect->left;
  129. bh = rect->bottom - rect->top;
  130. update->bitmap = g_malloc(bw * bh * 4);
  131. drawable->bbox = *rect;
  132. drawable->clip.type = SPICE_CLIP_TYPE_NONE;
  133. drawable->effect = QXL_EFFECT_OPAQUE;
  134. drawable->release_info.id = (uintptr_t)(&update->ext);
  135. drawable->type = QXL_DRAW_COPY;
  136. drawable->surfaces_dest[0] = -1;
  137. drawable->surfaces_dest[1] = -1;
  138. drawable->surfaces_dest[2] = -1;
  139. clock_gettime(CLOCK_MONOTONIC, &time_space);
  140. /* time in milliseconds from epoch. */
  141. drawable->mm_time = time_space.tv_sec * 1000
  142. + time_space.tv_nsec / 1000 / 1000;
  143. drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
  144. drawable->u.copy.src_bitmap = (uintptr_t)image;
  145. drawable->u.copy.src_area.right = bw;
  146. drawable->u.copy.src_area.bottom = bh;
  147. QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
  148. image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
  149. image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
  150. image->bitmap.stride = bw * 4;
  151. image->descriptor.width = image->bitmap.x = bw;
  152. image->descriptor.height = image->bitmap.y = bh;
  153. image->bitmap.data = (uintptr_t)(update->bitmap);
  154. image->bitmap.palette = 0;
  155. image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
  156. dest = pixman_image_create_bits(PIXMAN_x8r8g8b8, bw, bh,
  157. (void *)update->bitmap, bw * 4);
  158. pixman_image_composite(PIXMAN_OP_SRC, ssd->surface, NULL, ssd->mirror,
  159. rect->left, rect->top, 0, 0,
  160. rect->left, rect->top, bw, bh);
  161. pixman_image_composite(PIXMAN_OP_SRC, ssd->mirror, NULL, dest,
  162. rect->left, rect->top, 0, 0,
  163. 0, 0, bw, bh);
  164. pixman_image_unref(dest);
  165. cmd->type = QXL_CMD_DRAW;
  166. cmd->data = (uintptr_t)drawable;
  167. QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
  168. }
  169. static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
  170. {
  171. static const int blksize = 32;
  172. int blocks = (surface_width(ssd->ds) + blksize - 1) / blksize;
  173. int dirty_top[blocks];
  174. int y, yoff, x, xoff, blk, bw;
  175. int bpp = surface_bytes_per_pixel(ssd->ds);
  176. uint8_t *guest, *mirror;
  177. if (qemu_spice_rect_is_empty(&ssd->dirty)) {
  178. return;
  179. };
  180. if (ssd->surface == NULL) {
  181. ssd->surface = pixman_image_ref(ssd->ds->image);
  182. ssd->mirror = qemu_pixman_mirror_create(ssd->ds->format,
  183. ssd->ds->image);
  184. }
  185. for (blk = 0; blk < blocks; blk++) {
  186. dirty_top[blk] = -1;
  187. }
  188. guest = surface_data(ssd->ds);
  189. mirror = (void *)pixman_image_get_data(ssd->mirror);
  190. for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
  191. yoff = y * surface_stride(ssd->ds);
  192. for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
  193. xoff = x * bpp;
  194. blk = x / blksize;
  195. bw = MIN(blksize, ssd->dirty.right - x);
  196. if (memcmp(guest + yoff + xoff,
  197. mirror + yoff + xoff,
  198. bw * bpp) == 0) {
  199. if (dirty_top[blk] != -1) {
  200. QXLRect update = {
  201. .top = dirty_top[blk],
  202. .bottom = y,
  203. .left = x,
  204. .right = x + bw,
  205. };
  206. qemu_spice_create_one_update(ssd, &update);
  207. dirty_top[blk] = -1;
  208. }
  209. } else {
  210. if (dirty_top[blk] == -1) {
  211. dirty_top[blk] = y;
  212. }
  213. }
  214. }
  215. }
  216. for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
  217. blk = x / blksize;
  218. bw = MIN(blksize, ssd->dirty.right - x);
  219. if (dirty_top[blk] != -1) {
  220. QXLRect update = {
  221. .top = dirty_top[blk],
  222. .bottom = ssd->dirty.bottom,
  223. .left = x,
  224. .right = x + bw,
  225. };
  226. qemu_spice_create_one_update(ssd, &update);
  227. dirty_top[blk] = -1;
  228. }
  229. }
  230. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  231. }
  232. static SimpleSpiceCursor*
  233. qemu_spice_create_cursor_update(SimpleSpiceDisplay *ssd,
  234. QEMUCursor *c)
  235. {
  236. size_t size = c ? c->width * c->height * 4 : 0;
  237. SimpleSpiceCursor *update;
  238. QXLCursorCmd *ccmd;
  239. QXLCursor *cursor;
  240. QXLCommand *cmd;
  241. update = g_malloc0(sizeof(*update) + size);
  242. ccmd = &update->cmd;
  243. cursor = &update->cursor;
  244. cmd = &update->ext.cmd;
  245. if (c) {
  246. ccmd->type = QXL_CURSOR_SET;
  247. ccmd->u.set.position.x = ssd->ptr_x;
  248. ccmd->u.set.position.y = ssd->ptr_y;
  249. ccmd->u.set.visible = true;
  250. ccmd->u.set.shape = (uintptr_t)cursor;
  251. cursor->header.unique = ssd->unique++;
  252. cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
  253. cursor->header.width = c->width;
  254. cursor->header.height = c->height;
  255. cursor->header.hot_spot_x = c->hot_x;
  256. cursor->header.hot_spot_y = c->hot_y;
  257. cursor->data_size = size;
  258. cursor->chunk.data_size = size;
  259. memcpy(cursor->chunk.data, c->data, size);
  260. } else {
  261. ccmd->type = QXL_CURSOR_MOVE;
  262. ccmd->u.position.x = ssd->ptr_x;
  263. ccmd->u.position.y = ssd->ptr_y;
  264. }
  265. ccmd->release_info.id = (uintptr_t)(&update->ext);
  266. cmd->type = QXL_CMD_CURSOR;
  267. cmd->data = (uintptr_t)ccmd;
  268. return update;
  269. }
  270. /*
  271. * Called from spice server thread context (via interface_release_resource)
  272. * We do *not* hold the global qemu mutex here, so extra care is needed
  273. * when calling qemu functions. QEMU interfaces used:
  274. * - g_free (underlying glibc free is re-entrant).
  275. */
  276. void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
  277. {
  278. g_free(update->bitmap);
  279. g_free(update);
  280. }
  281. void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
  282. {
  283. QXLDevMemSlot memslot;
  284. dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
  285. memset(&memslot, 0, sizeof(memslot));
  286. memslot.slot_group_id = MEMSLOT_GROUP_HOST;
  287. memslot.virt_end = ~0;
  288. qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
  289. }
  290. void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
  291. {
  292. QXLDevSurfaceCreate surface;
  293. uint64_t surface_size;
  294. memset(&surface, 0, sizeof(surface));
  295. surface_size = (uint64_t) surface_width(ssd->ds) *
  296. surface_height(ssd->ds) * 4;
  297. assert(surface_size > 0);
  298. assert(surface_size < INT_MAX);
  299. if (ssd->bufsize < surface_size) {
  300. ssd->bufsize = surface_size;
  301. g_free(ssd->buf);
  302. ssd->buf = g_malloc(ssd->bufsize);
  303. }
  304. dprint(1, "%s/%d: %ux%u (size %" PRIu64 "/%d)\n", __func__, ssd->qxl.id,
  305. surface_width(ssd->ds), surface_height(ssd->ds),
  306. surface_size, ssd->bufsize);
  307. surface.format = SPICE_SURFACE_FMT_32_xRGB;
  308. surface.width = surface_width(ssd->ds);
  309. surface.height = surface_height(ssd->ds);
  310. surface.stride = -surface.width * 4;
  311. surface.mouse_mode = true;
  312. surface.flags = 0;
  313. surface.type = 0;
  314. surface.mem = (uintptr_t)ssd->buf;
  315. surface.group_id = MEMSLOT_GROUP_HOST;
  316. qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
  317. }
  318. void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
  319. {
  320. dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
  321. qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
  322. }
  323. void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
  324. {
  325. qemu_mutex_init(&ssd->lock);
  326. QTAILQ_INIT(&ssd->updates);
  327. ssd->mouse_x = -1;
  328. ssd->mouse_y = -1;
  329. if (ssd->num_surfaces == 0) {
  330. ssd->num_surfaces = 1024;
  331. }
  332. }
  333. /* display listener callbacks */
  334. void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
  335. int x, int y, int w, int h)
  336. {
  337. QXLRect update_area;
  338. dprint(2, "%s/%d: x %d y %d w %d h %d\n", __func__,
  339. ssd->qxl.id, x, y, w, h);
  340. update_area.left = x,
  341. update_area.right = x + w;
  342. update_area.top = y;
  343. update_area.bottom = y + h;
  344. if (qemu_spice_rect_is_empty(&ssd->dirty)) {
  345. ssd->notify++;
  346. }
  347. qemu_spice_rect_union(&ssd->dirty, &update_area);
  348. }
  349. void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
  350. DisplaySurface *surface)
  351. {
  352. SimpleSpiceUpdate *update;
  353. bool need_destroy;
  354. dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
  355. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  356. if (ssd->surface) {
  357. pixman_image_unref(ssd->surface);
  358. ssd->surface = NULL;
  359. pixman_image_unref(ssd->mirror);
  360. ssd->mirror = NULL;
  361. }
  362. qemu_mutex_lock(&ssd->lock);
  363. need_destroy = (ssd->ds != NULL);
  364. ssd->ds = surface;
  365. while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
  366. QTAILQ_REMOVE(&ssd->updates, update, next);
  367. qemu_spice_destroy_update(ssd, update);
  368. }
  369. qemu_mutex_unlock(&ssd->lock);
  370. if (need_destroy) {
  371. qemu_spice_destroy_host_primary(ssd);
  372. }
  373. if (ssd->ds) {
  374. qemu_spice_create_host_primary(ssd);
  375. }
  376. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  377. ssd->notify++;
  378. }
  379. void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
  380. {
  381. if (ssd->cursor) {
  382. assert(ssd->dcl.con);
  383. dpy_cursor_define(ssd->dcl.con, ssd->cursor);
  384. cursor_put(ssd->cursor);
  385. ssd->cursor = NULL;
  386. }
  387. if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
  388. assert(ssd->dcl.con);
  389. dpy_mouse_set(ssd->dcl.con, ssd->mouse_x, ssd->mouse_y, 1);
  390. ssd->mouse_x = -1;
  391. ssd->mouse_y = -1;
  392. }
  393. }
  394. void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
  395. {
  396. dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
  397. graphic_hw_update(ssd->dcl.con);
  398. qemu_mutex_lock(&ssd->lock);
  399. if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
  400. qemu_spice_create_update(ssd);
  401. ssd->notify++;
  402. }
  403. qemu_spice_cursor_refresh_unlocked(ssd);
  404. qemu_mutex_unlock(&ssd->lock);
  405. if (ssd->notify) {
  406. ssd->notify = 0;
  407. qemu_spice_wakeup(ssd);
  408. dprint(2, "%s/%d: notify\n", __func__, ssd->qxl.id);
  409. }
  410. }
  411. /* spice display interface callbacks */
  412. static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
  413. {
  414. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  415. dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
  416. ssd->worker = qxl_worker;
  417. }
  418. static void interface_set_compression_level(QXLInstance *sin, int level)
  419. {
  420. dprint(1, "%s/%d:\n", __func__, sin->id);
  421. /* nothing to do */
  422. }
  423. static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
  424. {
  425. dprint(3, "%s/%d:\n", __func__, sin->id);
  426. /* nothing to do */
  427. }
  428. static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
  429. {
  430. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  431. info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
  432. info->memslot_id_bits = MEMSLOT_SLOT_BITS;
  433. info->num_memslots = NUM_MEMSLOTS;
  434. info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
  435. info->internal_groupslot_id = 0;
  436. info->qxl_ram_size = 16 * 1024 * 1024;
  437. info->n_surfaces = ssd->num_surfaces;
  438. }
  439. static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
  440. {
  441. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  442. SimpleSpiceUpdate *update;
  443. int ret = false;
  444. dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
  445. qemu_mutex_lock(&ssd->lock);
  446. update = QTAILQ_FIRST(&ssd->updates);
  447. if (update != NULL) {
  448. QTAILQ_REMOVE(&ssd->updates, update, next);
  449. *ext = update->ext;
  450. ret = true;
  451. }
  452. qemu_mutex_unlock(&ssd->lock);
  453. return ret;
  454. }
  455. static int interface_req_cmd_notification(QXLInstance *sin)
  456. {
  457. dprint(1, "%s/%d:\n", __func__, sin->id);
  458. return 1;
  459. }
  460. static void interface_release_resource(QXLInstance *sin,
  461. struct QXLReleaseInfoExt rext)
  462. {
  463. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  464. SimpleSpiceUpdate *update;
  465. SimpleSpiceCursor *cursor;
  466. QXLCommandExt *ext;
  467. dprint(2, "%s/%d:\n", __func__, ssd->qxl.id);
  468. ext = (void *)(intptr_t)(rext.info->id);
  469. switch (ext->cmd.type) {
  470. case QXL_CMD_DRAW:
  471. update = container_of(ext, SimpleSpiceUpdate, ext);
  472. qemu_spice_destroy_update(ssd, update);
  473. break;
  474. case QXL_CMD_CURSOR:
  475. cursor = container_of(ext, SimpleSpiceCursor, ext);
  476. g_free(cursor);
  477. break;
  478. default:
  479. g_assert_not_reached();
  480. }
  481. }
  482. static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
  483. {
  484. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  485. int ret;
  486. dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
  487. qemu_mutex_lock(&ssd->lock);
  488. if (ssd->ptr_define) {
  489. *ext = ssd->ptr_define->ext;
  490. ssd->ptr_define = NULL;
  491. ret = true;
  492. } else if (ssd->ptr_move) {
  493. *ext = ssd->ptr_move->ext;
  494. ssd->ptr_move = NULL;
  495. ret = true;
  496. } else {
  497. ret = false;
  498. }
  499. qemu_mutex_unlock(&ssd->lock);
  500. return ret;
  501. }
  502. static int interface_req_cursor_notification(QXLInstance *sin)
  503. {
  504. dprint(1, "%s:\n", __FUNCTION__);
  505. return 1;
  506. }
  507. static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
  508. {
  509. fprintf(stderr, "%s: abort()\n", __FUNCTION__);
  510. abort();
  511. }
  512. static int interface_flush_resources(QXLInstance *sin)
  513. {
  514. fprintf(stderr, "%s: abort()\n", __FUNCTION__);
  515. abort();
  516. return 0;
  517. }
  518. static void interface_update_area_complete(QXLInstance *sin,
  519. uint32_t surface_id,
  520. QXLRect *dirty, uint32_t num_updated_rects)
  521. {
  522. /* should never be called, used in qxl native mode only */
  523. fprintf(stderr, "%s: abort()\n", __func__);
  524. abort();
  525. }
  526. /* called from spice server thread context only */
  527. static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
  528. {
  529. /* should never be called, used in qxl native mode only */
  530. fprintf(stderr, "%s: abort()\n", __func__);
  531. abort();
  532. }
  533. static void interface_set_client_capabilities(QXLInstance *sin,
  534. uint8_t client_present,
  535. uint8_t caps[58])
  536. {
  537. dprint(3, "%s:\n", __func__);
  538. }
  539. static int interface_client_monitors_config(QXLInstance *sin,
  540. VDAgentMonitorsConfig *mc)
  541. {
  542. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  543. QemuUIInfo info;
  544. int rc;
  545. if (!mc) {
  546. return 1;
  547. }
  548. /*
  549. * FIXME: multihead is tricky due to the way
  550. * spice has multihead implemented.
  551. */
  552. memset(&info, 0, sizeof(info));
  553. if (mc->num_of_monitors > 0) {
  554. info.width = mc->monitors[0].width;
  555. info.height = mc->monitors[0].height;
  556. }
  557. rc = dpy_set_ui_info(ssd->dcl.con, &info);
  558. dprint(1, "%s/%d: size %dx%d, rc %d <--- ==========================\n",
  559. __func__, ssd->qxl.id, info.width, info.height, rc);
  560. if (rc != 0) {
  561. return 0; /* == not supported by guest */
  562. } else {
  563. return 1;
  564. }
  565. }
  566. static const QXLInterface dpy_interface = {
  567. .base.type = SPICE_INTERFACE_QXL,
  568. .base.description = "qemu simple display",
  569. .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
  570. .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
  571. .attache_worker = interface_attach_worker,
  572. .set_compression_level = interface_set_compression_level,
  573. .set_mm_time = interface_set_mm_time,
  574. .get_init_info = interface_get_init_info,
  575. /* the callbacks below are called from spice server thread context */
  576. .get_command = interface_get_command,
  577. .req_cmd_notification = interface_req_cmd_notification,
  578. .release_resource = interface_release_resource,
  579. .get_cursor_command = interface_get_cursor_command,
  580. .req_cursor_notification = interface_req_cursor_notification,
  581. .notify_update = interface_notify_update,
  582. .flush_resources = interface_flush_resources,
  583. .async_complete = interface_async_complete,
  584. .update_area_complete = interface_update_area_complete,
  585. .set_client_capabilities = interface_set_client_capabilities,
  586. .client_monitors_config = interface_client_monitors_config,
  587. };
  588. static void display_update(DisplayChangeListener *dcl,
  589. int x, int y, int w, int h)
  590. {
  591. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  592. qemu_spice_display_update(ssd, x, y, w, h);
  593. }
  594. static void display_switch(DisplayChangeListener *dcl,
  595. struct DisplaySurface *surface)
  596. {
  597. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  598. qemu_spice_display_switch(ssd, surface);
  599. }
  600. static void display_refresh(DisplayChangeListener *dcl)
  601. {
  602. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  603. qemu_spice_display_refresh(ssd);
  604. }
  605. static void display_mouse_set(DisplayChangeListener *dcl,
  606. int x, int y, int on)
  607. {
  608. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  609. qemu_mutex_lock(&ssd->lock);
  610. ssd->ptr_x = x;
  611. ssd->ptr_y = x;
  612. if (ssd->ptr_move) {
  613. g_free(ssd->ptr_move);
  614. }
  615. ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL);
  616. qemu_mutex_unlock(&ssd->lock);
  617. }
  618. static void display_mouse_define(DisplayChangeListener *dcl,
  619. QEMUCursor *c)
  620. {
  621. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  622. qemu_mutex_lock(&ssd->lock);
  623. if (ssd->ptr_move) {
  624. g_free(ssd->ptr_move);
  625. ssd->ptr_move = NULL;
  626. }
  627. if (ssd->ptr_define) {
  628. g_free(ssd->ptr_define);
  629. }
  630. ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c);
  631. qemu_mutex_unlock(&ssd->lock);
  632. }
  633. static const DisplayChangeListenerOps display_listener_ops = {
  634. .dpy_name = "spice",
  635. .dpy_gfx_update = display_update,
  636. .dpy_gfx_switch = display_switch,
  637. .dpy_refresh = display_refresh,
  638. .dpy_mouse_set = display_mouse_set,
  639. .dpy_cursor_define = display_mouse_define,
  640. };
  641. static void qemu_spice_display_init_one(QemuConsole *con)
  642. {
  643. SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
  644. qemu_spice_display_init_common(ssd);
  645. ssd->qxl.base.sif = &dpy_interface.base;
  646. qemu_spice_add_display_interface(&ssd->qxl, con);
  647. assert(ssd->worker);
  648. qemu_spice_create_host_memslot(ssd);
  649. ssd->dcl.ops = &display_listener_ops;
  650. ssd->dcl.con = con;
  651. register_displaychangelistener(&ssd->dcl);
  652. }
  653. void qemu_spice_display_init(void)
  654. {
  655. QemuConsole *con;
  656. int i;
  657. for (i = 0;; i++) {
  658. con = qemu_console_lookup_by_index(i);
  659. if (!con || !qemu_console_is_graphic(con)) {
  660. break;
  661. }
  662. if (qemu_spice_have_display_interface(con)) {
  663. continue;
  664. }
  665. qemu_spice_display_init_one(con);
  666. }
  667. }