2
0

spice-display.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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/osdep.h"
  18. #include "qemu-common.h"
  19. #include "ui/qemu-spice.h"
  20. #include "qemu/timer.h"
  21. #include "qemu/queue.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_LE_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 = DIV_ROUND_UP(surface_width(ssd->ds), blksize);
  173. int dirty_top[blocks];
  174. int y, yoff1, yoff2, 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. for (blk = 0; blk < blocks; blk++) {
  181. dirty_top[blk] = -1;
  182. }
  183. guest = surface_data(ssd->ds);
  184. mirror = (void *)pixman_image_get_data(ssd->mirror);
  185. for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
  186. yoff1 = y * surface_stride(ssd->ds);
  187. yoff2 = y * pixman_image_get_stride(ssd->mirror);
  188. for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
  189. xoff = x * bpp;
  190. blk = x / blksize;
  191. bw = MIN(blksize, ssd->dirty.right - x);
  192. if (memcmp(guest + yoff1 + xoff,
  193. mirror + yoff2 + xoff,
  194. bw * bpp) == 0) {
  195. if (dirty_top[blk] != -1) {
  196. QXLRect update = {
  197. .top = dirty_top[blk],
  198. .bottom = y,
  199. .left = x,
  200. .right = x + bw,
  201. };
  202. qemu_spice_create_one_update(ssd, &update);
  203. dirty_top[blk] = -1;
  204. }
  205. } else {
  206. if (dirty_top[blk] == -1) {
  207. dirty_top[blk] = y;
  208. }
  209. }
  210. }
  211. }
  212. for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
  213. blk = x / blksize;
  214. bw = MIN(blksize, ssd->dirty.right - x);
  215. if (dirty_top[blk] != -1) {
  216. QXLRect update = {
  217. .top = dirty_top[blk],
  218. .bottom = ssd->dirty.bottom,
  219. .left = x,
  220. .right = x + bw,
  221. };
  222. qemu_spice_create_one_update(ssd, &update);
  223. dirty_top[blk] = -1;
  224. }
  225. }
  226. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  227. }
  228. static SimpleSpiceCursor*
  229. qemu_spice_create_cursor_update(SimpleSpiceDisplay *ssd,
  230. QEMUCursor *c,
  231. int on)
  232. {
  233. size_t size = c ? c->width * c->height * 4 : 0;
  234. SimpleSpiceCursor *update;
  235. QXLCursorCmd *ccmd;
  236. QXLCursor *cursor;
  237. QXLCommand *cmd;
  238. update = g_malloc0(sizeof(*update) + size);
  239. ccmd = &update->cmd;
  240. cursor = &update->cursor;
  241. cmd = &update->ext.cmd;
  242. if (c) {
  243. ccmd->type = QXL_CURSOR_SET;
  244. ccmd->u.set.position.x = ssd->ptr_x + ssd->hot_x;
  245. ccmd->u.set.position.y = ssd->ptr_y + ssd->hot_y;
  246. ccmd->u.set.visible = true;
  247. ccmd->u.set.shape = (uintptr_t)cursor;
  248. cursor->header.unique = ssd->unique++;
  249. cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
  250. cursor->header.width = c->width;
  251. cursor->header.height = c->height;
  252. cursor->header.hot_spot_x = c->hot_x;
  253. cursor->header.hot_spot_y = c->hot_y;
  254. cursor->data_size = size;
  255. cursor->chunk.data_size = size;
  256. memcpy(cursor->chunk.data, c->data, size);
  257. } else if (!on) {
  258. ccmd->type = QXL_CURSOR_HIDE;
  259. } else {
  260. ccmd->type = QXL_CURSOR_MOVE;
  261. ccmd->u.position.x = ssd->ptr_x + ssd->hot_x;
  262. ccmd->u.position.y = ssd->ptr_y + ssd->hot_y;
  263. }
  264. ccmd->release_info.id = (uintptr_t)(&update->ext);
  265. cmd->type = QXL_CMD_CURSOR;
  266. cmd->data = (uintptr_t)ccmd;
  267. return update;
  268. }
  269. /*
  270. * Called from spice server thread context (via interface_release_resource)
  271. * We do *not* hold the global qemu mutex here, so extra care is needed
  272. * when calling qemu functions. QEMU interfaces used:
  273. * - g_free (underlying glibc free is re-entrant).
  274. */
  275. void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
  276. {
  277. g_free(update->bitmap);
  278. g_free(update);
  279. }
  280. void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
  281. {
  282. QXLDevMemSlot memslot;
  283. dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
  284. memset(&memslot, 0, sizeof(memslot));
  285. memslot.slot_group_id = MEMSLOT_GROUP_HOST;
  286. memslot.virt_end = ~0;
  287. qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
  288. }
  289. void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
  290. {
  291. QXLDevSurfaceCreate surface;
  292. uint64_t surface_size;
  293. memset(&surface, 0, sizeof(surface));
  294. surface_size = (uint64_t) surface_width(ssd->ds) *
  295. surface_height(ssd->ds) * 4;
  296. assert(surface_size > 0);
  297. assert(surface_size < INT_MAX);
  298. if (ssd->bufsize < surface_size) {
  299. ssd->bufsize = surface_size;
  300. g_free(ssd->buf);
  301. ssd->buf = g_malloc(ssd->bufsize);
  302. }
  303. dprint(1, "%s/%d: %ux%u (size %" PRIu64 "/%d)\n", __func__, ssd->qxl.id,
  304. surface_width(ssd->ds), surface_height(ssd->ds),
  305. surface_size, ssd->bufsize);
  306. surface.format = SPICE_SURFACE_FMT_32_xRGB;
  307. surface.width = surface_width(ssd->ds);
  308. surface.height = surface_height(ssd->ds);
  309. surface.stride = -surface.width * 4;
  310. surface.mouse_mode = true;
  311. surface.flags = 0;
  312. surface.type = 0;
  313. surface.mem = (uintptr_t)ssd->buf;
  314. surface.group_id = MEMSLOT_GROUP_HOST;
  315. qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
  316. }
  317. void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
  318. {
  319. dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
  320. qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
  321. }
  322. void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
  323. {
  324. qemu_mutex_init(&ssd->lock);
  325. QTAILQ_INIT(&ssd->updates);
  326. ssd->mouse_x = -1;
  327. ssd->mouse_y = -1;
  328. if (ssd->num_surfaces == 0) {
  329. ssd->num_surfaces = 1024;
  330. }
  331. }
  332. /* display listener callbacks */
  333. void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
  334. int x, int y, int w, int h)
  335. {
  336. QXLRect update_area;
  337. dprint(2, "%s/%d: x %d y %d w %d h %d\n", __func__,
  338. ssd->qxl.id, x, y, w, h);
  339. update_area.left = x,
  340. update_area.right = x + w;
  341. update_area.top = y;
  342. update_area.bottom = y + h;
  343. if (qemu_spice_rect_is_empty(&ssd->dirty)) {
  344. ssd->notify++;
  345. }
  346. qemu_spice_rect_union(&ssd->dirty, &update_area);
  347. }
  348. void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
  349. DisplaySurface *surface)
  350. {
  351. SimpleSpiceUpdate *update;
  352. bool need_destroy;
  353. if (surface && ssd->surface &&
  354. surface_width(surface) == pixman_image_get_width(ssd->surface) &&
  355. surface_height(surface) == pixman_image_get_height(ssd->surface) &&
  356. surface_format(surface) == pixman_image_get_format(ssd->surface)) {
  357. /* no-resize fast path: just swap backing store */
  358. dprint(1, "%s/%d: fast (%dx%d)\n", __func__, ssd->qxl.id,
  359. surface_width(surface), surface_height(surface));
  360. qemu_mutex_lock(&ssd->lock);
  361. ssd->ds = surface;
  362. pixman_image_unref(ssd->surface);
  363. ssd->surface = pixman_image_ref(ssd->ds->image);
  364. qemu_mutex_unlock(&ssd->lock);
  365. qemu_spice_display_update(ssd, 0, 0,
  366. surface_width(surface),
  367. surface_height(surface));
  368. return;
  369. }
  370. /* full mode switch */
  371. dprint(1, "%s/%d: full (%dx%d -> %dx%d)\n", __func__, ssd->qxl.id,
  372. ssd->surface ? pixman_image_get_width(ssd->surface) : 0,
  373. ssd->surface ? pixman_image_get_height(ssd->surface) : 0,
  374. surface ? surface_width(surface) : 0,
  375. surface ? surface_height(surface) : 0);
  376. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  377. if (ssd->surface) {
  378. pixman_image_unref(ssd->surface);
  379. ssd->surface = NULL;
  380. pixman_image_unref(ssd->mirror);
  381. ssd->mirror = NULL;
  382. }
  383. qemu_mutex_lock(&ssd->lock);
  384. need_destroy = (ssd->ds != NULL);
  385. ssd->ds = surface;
  386. while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
  387. QTAILQ_REMOVE(&ssd->updates, update, next);
  388. qemu_spice_destroy_update(ssd, update);
  389. }
  390. qemu_mutex_unlock(&ssd->lock);
  391. if (need_destroy) {
  392. qemu_spice_destroy_host_primary(ssd);
  393. }
  394. if (ssd->ds) {
  395. ssd->surface = pixman_image_ref(ssd->ds->image);
  396. ssd->mirror = qemu_pixman_mirror_create(ssd->ds->format,
  397. ssd->ds->image);
  398. qemu_spice_create_host_primary(ssd);
  399. }
  400. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  401. ssd->notify++;
  402. qemu_mutex_lock(&ssd->lock);
  403. if (ssd->cursor) {
  404. g_free(ssd->ptr_define);
  405. ssd->ptr_define = qemu_spice_create_cursor_update(ssd, ssd->cursor, 0);
  406. }
  407. qemu_mutex_unlock(&ssd->lock);
  408. }
  409. static void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
  410. {
  411. if (ssd->cursor) {
  412. assert(ssd->dcl.con);
  413. dpy_cursor_define(ssd->dcl.con, ssd->cursor);
  414. }
  415. if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
  416. assert(ssd->dcl.con);
  417. dpy_mouse_set(ssd->dcl.con, ssd->mouse_x, ssd->mouse_y, 1);
  418. ssd->mouse_x = -1;
  419. ssd->mouse_y = -1;
  420. }
  421. }
  422. void qemu_spice_cursor_refresh_bh(void *opaque)
  423. {
  424. SimpleSpiceDisplay *ssd = opaque;
  425. qemu_mutex_lock(&ssd->lock);
  426. qemu_spice_cursor_refresh_unlocked(ssd);
  427. qemu_mutex_unlock(&ssd->lock);
  428. }
  429. void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
  430. {
  431. dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
  432. graphic_hw_update(ssd->dcl.con);
  433. qemu_mutex_lock(&ssd->lock);
  434. if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
  435. qemu_spice_create_update(ssd);
  436. ssd->notify++;
  437. }
  438. qemu_mutex_unlock(&ssd->lock);
  439. if (ssd->notify) {
  440. ssd->notify = 0;
  441. qemu_spice_wakeup(ssd);
  442. dprint(2, "%s/%d: notify\n", __func__, ssd->qxl.id);
  443. }
  444. }
  445. /* spice display interface callbacks */
  446. static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
  447. {
  448. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  449. dprint(1, "%s/%d:\n", __func__, ssd->qxl.id);
  450. ssd->worker = qxl_worker;
  451. }
  452. static void interface_set_compression_level(QXLInstance *sin, int level)
  453. {
  454. dprint(1, "%s/%d:\n", __func__, sin->id);
  455. /* nothing to do */
  456. }
  457. #if SPICE_NEEDS_SET_MM_TIME
  458. static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
  459. {
  460. dprint(3, "%s/%d:\n", __func__, sin->id);
  461. /* nothing to do */
  462. }
  463. #endif
  464. static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
  465. {
  466. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  467. info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
  468. info->memslot_id_bits = MEMSLOT_SLOT_BITS;
  469. info->num_memslots = NUM_MEMSLOTS;
  470. info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
  471. info->internal_groupslot_id = 0;
  472. info->qxl_ram_size = 16 * 1024 * 1024;
  473. info->n_surfaces = ssd->num_surfaces;
  474. }
  475. static int interface_get_command(QXLInstance *sin, QXLCommandExt *ext)
  476. {
  477. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  478. SimpleSpiceUpdate *update;
  479. int ret = false;
  480. dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
  481. qemu_mutex_lock(&ssd->lock);
  482. update = QTAILQ_FIRST(&ssd->updates);
  483. if (update != NULL) {
  484. QTAILQ_REMOVE(&ssd->updates, update, next);
  485. *ext = update->ext;
  486. ret = true;
  487. }
  488. qemu_mutex_unlock(&ssd->lock);
  489. return ret;
  490. }
  491. static int interface_req_cmd_notification(QXLInstance *sin)
  492. {
  493. dprint(2, "%s/%d:\n", __func__, sin->id);
  494. return 1;
  495. }
  496. static void interface_release_resource(QXLInstance *sin,
  497. QXLReleaseInfoExt rext)
  498. {
  499. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  500. SimpleSpiceUpdate *update;
  501. SimpleSpiceCursor *cursor;
  502. QXLCommandExt *ext;
  503. dprint(2, "%s/%d:\n", __func__, ssd->qxl.id);
  504. ext = (void *)(intptr_t)(rext.info->id);
  505. switch (ext->cmd.type) {
  506. case QXL_CMD_DRAW:
  507. update = container_of(ext, SimpleSpiceUpdate, ext);
  508. qemu_spice_destroy_update(ssd, update);
  509. break;
  510. case QXL_CMD_CURSOR:
  511. cursor = container_of(ext, SimpleSpiceCursor, ext);
  512. g_free(cursor);
  513. break;
  514. default:
  515. g_assert_not_reached();
  516. }
  517. }
  518. static int interface_get_cursor_command(QXLInstance *sin, QXLCommandExt *ext)
  519. {
  520. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  521. int ret;
  522. dprint(3, "%s/%d:\n", __func__, ssd->qxl.id);
  523. qemu_mutex_lock(&ssd->lock);
  524. if (ssd->ptr_define) {
  525. *ext = ssd->ptr_define->ext;
  526. ssd->ptr_define = NULL;
  527. ret = true;
  528. } else if (ssd->ptr_move) {
  529. *ext = ssd->ptr_move->ext;
  530. ssd->ptr_move = NULL;
  531. ret = true;
  532. } else {
  533. ret = false;
  534. }
  535. qemu_mutex_unlock(&ssd->lock);
  536. return ret;
  537. }
  538. static int interface_req_cursor_notification(QXLInstance *sin)
  539. {
  540. dprint(2, "%s:\n", __func__);
  541. return 1;
  542. }
  543. static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
  544. {
  545. fprintf(stderr, "%s: abort()\n", __FUNCTION__);
  546. abort();
  547. }
  548. static int interface_flush_resources(QXLInstance *sin)
  549. {
  550. fprintf(stderr, "%s: abort()\n", __FUNCTION__);
  551. abort();
  552. return 0;
  553. }
  554. static void interface_update_area_complete(QXLInstance *sin,
  555. uint32_t surface_id,
  556. QXLRect *dirty, uint32_t num_updated_rects)
  557. {
  558. /* should never be called, used in qxl native mode only */
  559. fprintf(stderr, "%s: abort()\n", __func__);
  560. abort();
  561. }
  562. /* called from spice server thread context only */
  563. static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
  564. {
  565. QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
  566. switch (cookie->type) {
  567. #ifdef HAVE_SPICE_GL
  568. case QXL_COOKIE_TYPE_GL_DRAW_DONE:
  569. {
  570. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  571. qemu_bh_schedule(ssd->gl_unblock_bh);
  572. break;
  573. }
  574. case QXL_COOKIE_TYPE_IO:
  575. if (cookie->io == QXL_IO_MONITORS_CONFIG_ASYNC) {
  576. g_free(cookie->u.data);
  577. }
  578. break;
  579. #endif
  580. default:
  581. /* should never be called, used in qxl native mode only */
  582. fprintf(stderr, "%s: abort()\n", __func__);
  583. abort();
  584. }
  585. g_free(cookie);
  586. }
  587. static void interface_set_client_capabilities(QXLInstance *sin,
  588. uint8_t client_present,
  589. uint8_t caps[58])
  590. {
  591. dprint(3, "%s:\n", __func__);
  592. }
  593. static int interface_client_monitors_config(QXLInstance *sin,
  594. VDAgentMonitorsConfig *mc)
  595. {
  596. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  597. QemuUIInfo info;
  598. int head;
  599. if (!dpy_ui_info_supported(ssd->dcl.con)) {
  600. return 0; /* == not supported by guest */
  601. }
  602. if (!mc) {
  603. return 1;
  604. }
  605. memset(&info, 0, sizeof(info));
  606. head = qemu_console_get_head(ssd->dcl.con);
  607. if (mc->num_of_monitors > head) {
  608. info.width = mc->monitors[head].width;
  609. info.height = mc->monitors[head].height;
  610. }
  611. dpy_set_ui_info(ssd->dcl.con, &info);
  612. dprint(1, "%s/%d: size %dx%d\n", __func__, ssd->qxl.id,
  613. info.width, info.height);
  614. return 1;
  615. }
  616. static const QXLInterface dpy_interface = {
  617. .base.type = SPICE_INTERFACE_QXL,
  618. .base.description = "qemu simple display",
  619. .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
  620. .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
  621. .attache_worker = interface_attach_worker,
  622. .set_compression_level = interface_set_compression_level,
  623. #if SPICE_NEEDS_SET_MM_TIME
  624. .set_mm_time = interface_set_mm_time,
  625. #endif
  626. .get_init_info = interface_get_init_info,
  627. /* the callbacks below are called from spice server thread context */
  628. .get_command = interface_get_command,
  629. .req_cmd_notification = interface_req_cmd_notification,
  630. .release_resource = interface_release_resource,
  631. .get_cursor_command = interface_get_cursor_command,
  632. .req_cursor_notification = interface_req_cursor_notification,
  633. .notify_update = interface_notify_update,
  634. .flush_resources = interface_flush_resources,
  635. .async_complete = interface_async_complete,
  636. .update_area_complete = interface_update_area_complete,
  637. .set_client_capabilities = interface_set_client_capabilities,
  638. .client_monitors_config = interface_client_monitors_config,
  639. };
  640. static void display_update(DisplayChangeListener *dcl,
  641. int x, int y, int w, int h)
  642. {
  643. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  644. qemu_spice_display_update(ssd, x, y, w, h);
  645. }
  646. static void display_switch(DisplayChangeListener *dcl,
  647. DisplaySurface *surface)
  648. {
  649. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  650. qemu_spice_display_switch(ssd, surface);
  651. }
  652. static void display_refresh(DisplayChangeListener *dcl)
  653. {
  654. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  655. qemu_spice_display_refresh(ssd);
  656. }
  657. static void display_mouse_set(DisplayChangeListener *dcl,
  658. int x, int y, int on)
  659. {
  660. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  661. qemu_mutex_lock(&ssd->lock);
  662. ssd->ptr_x = x;
  663. ssd->ptr_y = y;
  664. g_free(ssd->ptr_move);
  665. ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL, on);
  666. qemu_mutex_unlock(&ssd->lock);
  667. qemu_spice_wakeup(ssd);
  668. }
  669. static void display_mouse_define(DisplayChangeListener *dcl,
  670. QEMUCursor *c)
  671. {
  672. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  673. qemu_mutex_lock(&ssd->lock);
  674. cursor_get(c);
  675. cursor_put(ssd->cursor);
  676. ssd->cursor = c;
  677. ssd->hot_x = c->hot_x;
  678. ssd->hot_y = c->hot_y;
  679. g_free(ssd->ptr_move);
  680. ssd->ptr_move = NULL;
  681. g_free(ssd->ptr_define);
  682. ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c, 0);
  683. qemu_mutex_unlock(&ssd->lock);
  684. qemu_spice_wakeup(ssd);
  685. }
  686. static const DisplayChangeListenerOps display_listener_ops = {
  687. .dpy_name = "spice",
  688. .dpy_gfx_update = display_update,
  689. .dpy_gfx_switch = display_switch,
  690. .dpy_gfx_check_format = qemu_pixman_check_format,
  691. .dpy_refresh = display_refresh,
  692. .dpy_mouse_set = display_mouse_set,
  693. .dpy_cursor_define = display_mouse_define,
  694. };
  695. #ifdef HAVE_SPICE_GL
  696. static void qemu_spice_gl_monitor_config(SimpleSpiceDisplay *ssd,
  697. int x, int y, int w, int h)
  698. {
  699. QXLMonitorsConfig *config;
  700. QXLCookie *cookie;
  701. config = g_malloc0(sizeof(QXLMonitorsConfig) + sizeof(QXLHead));
  702. config->count = 1;
  703. config->max_allowed = 1;
  704. config->heads[0].x = x;
  705. config->heads[0].y = y;
  706. config->heads[0].width = w;
  707. config->heads[0].height = h;
  708. cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
  709. QXL_IO_MONITORS_CONFIG_ASYNC);
  710. cookie->u.data = config;
  711. spice_qxl_monitors_config_async(&ssd->qxl,
  712. (uintptr_t)config,
  713. MEMSLOT_GROUP_HOST,
  714. (uintptr_t)cookie);
  715. }
  716. static void qemu_spice_gl_block(SimpleSpiceDisplay *ssd, bool block)
  717. {
  718. uint64_t timeout;
  719. if (block) {
  720. timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  721. timeout += 1000; /* one sec */
  722. timer_mod(ssd->gl_unblock_timer, timeout);
  723. } else {
  724. timer_del(ssd->gl_unblock_timer);
  725. }
  726. graphic_hw_gl_block(ssd->dcl.con, block);
  727. }
  728. static void qemu_spice_gl_unblock_bh(void *opaque)
  729. {
  730. SimpleSpiceDisplay *ssd = opaque;
  731. qemu_spice_gl_block(ssd, false);
  732. }
  733. static void qemu_spice_gl_block_timer(void *opaque)
  734. {
  735. fprintf(stderr, "WARNING: spice: no gl-draw-done within one second\n");
  736. }
  737. static void spice_gl_refresh(DisplayChangeListener *dcl)
  738. {
  739. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  740. uint64_t cookie;
  741. if (!ssd->ds || qemu_console_is_gl_blocked(ssd->dcl.con)) {
  742. return;
  743. }
  744. graphic_hw_update(dcl->con);
  745. if (ssd->gl_updates && ssd->have_surface) {
  746. qemu_spice_gl_block(ssd, true);
  747. cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
  748. spice_qxl_gl_draw_async(&ssd->qxl, 0, 0,
  749. surface_width(ssd->ds),
  750. surface_height(ssd->ds),
  751. cookie);
  752. ssd->gl_updates = 0;
  753. }
  754. }
  755. static void spice_gl_update(DisplayChangeListener *dcl,
  756. int x, int y, int w, int h)
  757. {
  758. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  759. surface_gl_update_texture(ssd->gls, ssd->ds, x, y, w, h);
  760. ssd->gl_updates++;
  761. }
  762. static void spice_gl_switch(DisplayChangeListener *dcl,
  763. struct DisplaySurface *new_surface)
  764. {
  765. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  766. EGLint stride, fourcc;
  767. int fd;
  768. if (ssd->ds) {
  769. surface_gl_destroy_texture(ssd->gls, ssd->ds);
  770. }
  771. ssd->ds = new_surface;
  772. if (ssd->ds) {
  773. surface_gl_create_texture(ssd->gls, ssd->ds);
  774. fd = egl_get_fd_for_texture(ssd->ds->texture,
  775. &stride, &fourcc);
  776. if (fd < 0) {
  777. surface_gl_destroy_texture(ssd->gls, ssd->ds);
  778. return;
  779. }
  780. dprint(1, "%s: %dx%d (stride %d/%d, fourcc 0x%x)\n", __func__,
  781. surface_width(ssd->ds), surface_height(ssd->ds),
  782. surface_stride(ssd->ds), stride, fourcc);
  783. /* note: spice server will close the fd */
  784. spice_qxl_gl_scanout(&ssd->qxl, fd,
  785. surface_width(ssd->ds),
  786. surface_height(ssd->ds),
  787. stride, fourcc, false);
  788. ssd->have_surface = true;
  789. ssd->have_scanout = false;
  790. qemu_spice_gl_monitor_config(ssd, 0, 0,
  791. surface_width(ssd->ds),
  792. surface_height(ssd->ds));
  793. }
  794. }
  795. static QEMUGLContext qemu_spice_gl_create_context(DisplayChangeListener *dcl,
  796. QEMUGLParams *params)
  797. {
  798. eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
  799. qemu_egl_rn_ctx);
  800. return qemu_egl_create_context(dcl, params);
  801. }
  802. static void qemu_spice_gl_scanout_disable(DisplayChangeListener *dcl)
  803. {
  804. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  805. dprint(1, "%s: no framebuffer\n", __func__);
  806. spice_qxl_gl_scanout(&ssd->qxl, -1, 0, 0, 0, 0, false);
  807. qemu_spice_gl_monitor_config(ssd, 0, 0, 0, 0);
  808. ssd->have_surface = false;
  809. ssd->have_scanout = false;
  810. }
  811. static void qemu_spice_gl_scanout_texture(DisplayChangeListener *dcl,
  812. uint32_t tex_id,
  813. bool y_0_top,
  814. uint32_t backing_width,
  815. uint32_t backing_height,
  816. uint32_t x, uint32_t y,
  817. uint32_t w, uint32_t h)
  818. {
  819. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  820. EGLint stride = 0, fourcc = 0;
  821. int fd = -1;
  822. assert(tex_id);
  823. fd = egl_get_fd_for_texture(tex_id, &stride, &fourcc);
  824. if (fd < 0) {
  825. fprintf(stderr, "%s: failed to get fd for texture\n", __func__);
  826. return;
  827. }
  828. dprint(1, "%s: %dx%d (stride %d, fourcc 0x%x)\n", __func__,
  829. w, h, stride, fourcc);
  830. /* note: spice server will close the fd */
  831. spice_qxl_gl_scanout(&ssd->qxl, fd, backing_width, backing_height,
  832. stride, fourcc, y_0_top);
  833. qemu_spice_gl_monitor_config(ssd, x, y, w, h);
  834. ssd->have_surface = false;
  835. ssd->have_scanout = true;
  836. }
  837. static void qemu_spice_gl_update(DisplayChangeListener *dcl,
  838. uint32_t x, uint32_t y, uint32_t w, uint32_t h)
  839. {
  840. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  841. uint64_t cookie;
  842. if (!ssd->have_scanout) {
  843. return;
  844. }
  845. dprint(2, "%s: %dx%d+%d+%d\n", __func__, w, h, x, y);
  846. qemu_spice_gl_block(ssd, true);
  847. cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
  848. spice_qxl_gl_draw_async(&ssd->qxl, x, y, w, h, cookie);
  849. }
  850. static const DisplayChangeListenerOps display_listener_gl_ops = {
  851. .dpy_name = "spice-egl",
  852. .dpy_gfx_update = spice_gl_update,
  853. .dpy_gfx_switch = spice_gl_switch,
  854. .dpy_gfx_check_format = console_gl_check_format,
  855. .dpy_refresh = spice_gl_refresh,
  856. .dpy_mouse_set = display_mouse_set,
  857. .dpy_cursor_define = display_mouse_define,
  858. .dpy_gl_ctx_create = qemu_spice_gl_create_context,
  859. .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
  860. .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
  861. .dpy_gl_ctx_get_current = qemu_egl_get_current_context,
  862. .dpy_gl_scanout_disable = qemu_spice_gl_scanout_disable,
  863. .dpy_gl_scanout_texture = qemu_spice_gl_scanout_texture,
  864. .dpy_gl_update = qemu_spice_gl_update,
  865. };
  866. #endif /* HAVE_SPICE_GL */
  867. static void qemu_spice_display_init_one(QemuConsole *con)
  868. {
  869. SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
  870. qemu_spice_display_init_common(ssd);
  871. ssd->dcl.ops = &display_listener_ops;
  872. #ifdef HAVE_SPICE_GL
  873. if (display_opengl) {
  874. ssd->dcl.ops = &display_listener_gl_ops;
  875. ssd->gl_unblock_bh = qemu_bh_new(qemu_spice_gl_unblock_bh, ssd);
  876. ssd->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
  877. qemu_spice_gl_block_timer, ssd);
  878. ssd->gls = console_gl_init_context();
  879. ssd->have_surface = false;
  880. ssd->have_scanout = false;
  881. }
  882. #endif
  883. ssd->dcl.con = con;
  884. ssd->qxl.base.sif = &dpy_interface.base;
  885. qemu_spice_add_display_interface(&ssd->qxl, con);
  886. assert(ssd->worker);
  887. qemu_spice_create_host_memslot(ssd);
  888. register_displaychangelistener(&ssd->dcl);
  889. }
  890. void qemu_spice_display_init(void)
  891. {
  892. QemuOptsList *olist = qemu_find_opts("spice");
  893. QemuOpts *opts = QTAILQ_FIRST(&olist->head);
  894. QemuConsole *spice_con, *con;
  895. const char *str;
  896. int i;
  897. str = qemu_opt_get(opts, "display");
  898. if (str) {
  899. int head = qemu_opt_get_number(opts, "head", 0);
  900. Error *err = NULL;
  901. spice_con = qemu_console_lookup_by_device_name(str, head, &err);
  902. if (err) {
  903. error_report("Failed to lookup display/head");
  904. exit(1);
  905. }
  906. } else {
  907. spice_con = NULL;
  908. }
  909. for (i = 0;; i++) {
  910. con = qemu_console_lookup_by_index(i);
  911. if (!con || !qemu_console_is_graphic(con)) {
  912. break;
  913. }
  914. if (qemu_spice_have_display_interface(con)) {
  915. continue;
  916. }
  917. if (spice_con != NULL && spice_con != con) {
  918. continue;
  919. }
  920. qemu_spice_display_init_one(con);
  921. }
  922. }