2
0

spice-display.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 "qemu-spice.h"
  19. #include "qemu-timer.h"
  20. #include "qemu-queue.h"
  21. #include "monitor.h"
  22. #include "console.h"
  23. #include "sysemu.h"
  24. #include "trace.h"
  25. #include "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. ssd->worker->add_memslot(ssd->worker, 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. ssd->worker->del_memslot(ssd->worker, 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. ssd->worker->create_primary_surface(ssd->worker, 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. ssd->worker->destroy_primary_surface(ssd->worker, id);
  104. }
  105. }
  106. void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
  107. {
  108. trace_qemu_spice_wakeup(ssd->qxl.id);
  109. ssd->worker->wakeup(ssd->worker);
  110. }
  111. #if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
  112. static void qemu_spice_start(SimpleSpiceDisplay *ssd)
  113. {
  114. trace_qemu_spice_start(ssd->qxl.id);
  115. ssd->worker->start(ssd->worker);
  116. }
  117. static void qemu_spice_stop(SimpleSpiceDisplay *ssd)
  118. {
  119. trace_qemu_spice_stop(ssd->qxl.id);
  120. ssd->worker->stop(ssd->worker);
  121. }
  122. #else
  123. static int spice_display_is_running;
  124. void qemu_spice_display_start(void)
  125. {
  126. spice_display_is_running = true;
  127. }
  128. void qemu_spice_display_stop(void)
  129. {
  130. spice_display_is_running = false;
  131. }
  132. #endif
  133. int qemu_spice_display_is_running(SimpleSpiceDisplay *ssd)
  134. {
  135. #if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
  136. return ssd->running;
  137. #else
  138. return spice_display_is_running;
  139. #endif
  140. }
  141. static SimpleSpiceUpdate *qemu_spice_create_update(SimpleSpiceDisplay *ssd)
  142. {
  143. SimpleSpiceUpdate *update;
  144. QXLDrawable *drawable;
  145. QXLImage *image;
  146. QXLCommand *cmd;
  147. uint8_t *src, *dst;
  148. int by, bw, bh;
  149. struct timespec time_space;
  150. if (qemu_spice_rect_is_empty(&ssd->dirty)) {
  151. return NULL;
  152. };
  153. trace_qemu_spice_create_update(
  154. ssd->dirty.left, ssd->dirty.right,
  155. ssd->dirty.top, ssd->dirty.bottom);
  156. update = g_malloc0(sizeof(*update));
  157. drawable = &update->drawable;
  158. image = &update->image;
  159. cmd = &update->ext.cmd;
  160. bw = ssd->dirty.right - ssd->dirty.left;
  161. bh = ssd->dirty.bottom - ssd->dirty.top;
  162. update->bitmap = g_malloc(bw * bh * 4);
  163. drawable->bbox = ssd->dirty;
  164. drawable->clip.type = SPICE_CLIP_TYPE_NONE;
  165. drawable->effect = QXL_EFFECT_OPAQUE;
  166. drawable->release_info.id = (uintptr_t)update;
  167. drawable->type = QXL_DRAW_COPY;
  168. drawable->surfaces_dest[0] = -1;
  169. drawable->surfaces_dest[1] = -1;
  170. drawable->surfaces_dest[2] = -1;
  171. clock_gettime(CLOCK_MONOTONIC, &time_space);
  172. /* time in milliseconds from epoch. */
  173. drawable->mm_time = time_space.tv_sec * 1000
  174. + time_space.tv_nsec / 1000 / 1000;
  175. drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
  176. drawable->u.copy.src_bitmap = (uintptr_t)image;
  177. drawable->u.copy.src_area.right = bw;
  178. drawable->u.copy.src_area.bottom = bh;
  179. QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
  180. image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
  181. image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
  182. image->bitmap.stride = bw * 4;
  183. image->descriptor.width = image->bitmap.x = bw;
  184. image->descriptor.height = image->bitmap.y = bh;
  185. image->bitmap.data = (uintptr_t)(update->bitmap);
  186. image->bitmap.palette = 0;
  187. image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
  188. if (ssd->conv == NULL) {
  189. PixelFormat dst = qemu_default_pixelformat(32);
  190. ssd->conv = qemu_pf_conv_get(&dst, &ssd->ds->surface->pf);
  191. assert(ssd->conv);
  192. }
  193. src = ds_get_data(ssd->ds) +
  194. ssd->dirty.top * ds_get_linesize(ssd->ds) +
  195. ssd->dirty.left * ds_get_bytes_per_pixel(ssd->ds);
  196. dst = update->bitmap;
  197. for (by = 0; by < bh; by++) {
  198. qemu_pf_conv_run(ssd->conv, dst, src, bw);
  199. src += ds_get_linesize(ssd->ds);
  200. dst += image->bitmap.stride;
  201. }
  202. cmd->type = QXL_CMD_DRAW;
  203. cmd->data = (uintptr_t)drawable;
  204. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  205. return update;
  206. }
  207. /*
  208. * Called from spice server thread context (via interface_release_ressource)
  209. * We do *not* hold the global qemu mutex here, so extra care is needed
  210. * when calling qemu functions. QEMU interfaces used:
  211. * - g_free (underlying glibc free is re-entrant).
  212. */
  213. void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
  214. {
  215. g_free(update->bitmap);
  216. g_free(update);
  217. }
  218. void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
  219. {
  220. QXLDevMemSlot memslot;
  221. dprint(1, "%s:\n", __FUNCTION__);
  222. memset(&memslot, 0, sizeof(memslot));
  223. memslot.slot_group_id = MEMSLOT_GROUP_HOST;
  224. memslot.virt_end = ~0;
  225. qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
  226. }
  227. void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
  228. {
  229. QXLDevSurfaceCreate surface;
  230. memset(&surface, 0, sizeof(surface));
  231. dprint(1, "%s: %dx%d\n", __FUNCTION__,
  232. ds_get_width(ssd->ds), ds_get_height(ssd->ds));
  233. surface.format = SPICE_SURFACE_FMT_32_xRGB;
  234. surface.width = ds_get_width(ssd->ds);
  235. surface.height = ds_get_height(ssd->ds);
  236. surface.stride = -surface.width * 4;
  237. surface.mouse_mode = true;
  238. surface.flags = 0;
  239. surface.type = 0;
  240. surface.mem = (uintptr_t)ssd->buf;
  241. surface.group_id = MEMSLOT_GROUP_HOST;
  242. qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
  243. }
  244. void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
  245. {
  246. dprint(1, "%s:\n", __FUNCTION__);
  247. qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
  248. }
  249. void qemu_spice_vm_change_state_handler(void *opaque, int running,
  250. RunState state)
  251. {
  252. #if SPICE_SERVER_VERSION < 0x000b02 /* before 0.11.2 */
  253. SimpleSpiceDisplay *ssd = opaque;
  254. if (running) {
  255. ssd->running = true;
  256. qemu_spice_start(ssd);
  257. } else {
  258. qemu_spice_stop(ssd);
  259. ssd->running = false;
  260. }
  261. #endif
  262. }
  263. void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd, DisplayState *ds)
  264. {
  265. ssd->ds = ds;
  266. qemu_mutex_init(&ssd->lock);
  267. ssd->mouse_x = -1;
  268. ssd->mouse_y = -1;
  269. ssd->bufsize = (16 * 1024 * 1024);
  270. ssd->buf = g_malloc(ssd->bufsize);
  271. }
  272. /* display listener callbacks */
  273. void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
  274. int x, int y, int w, int h)
  275. {
  276. QXLRect update_area;
  277. dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__, x, y, w, h);
  278. update_area.left = x,
  279. update_area.right = x + w;
  280. update_area.top = y;
  281. update_area.bottom = y + h;
  282. if (qemu_spice_rect_is_empty(&ssd->dirty)) {
  283. ssd->notify++;
  284. }
  285. qemu_spice_rect_union(&ssd->dirty, &update_area);
  286. }
  287. void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
  288. {
  289. dprint(1, "%s:\n", __FUNCTION__);
  290. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  291. qemu_pf_conv_put(ssd->conv);
  292. ssd->conv = NULL;
  293. qemu_mutex_lock(&ssd->lock);
  294. if (ssd->update != NULL) {
  295. qemu_spice_destroy_update(ssd, ssd->update);
  296. ssd->update = NULL;
  297. }
  298. qemu_mutex_unlock(&ssd->lock);
  299. qemu_spice_destroy_host_primary(ssd);
  300. qemu_spice_create_host_primary(ssd);
  301. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  302. ssd->notify++;
  303. }
  304. void qemu_spice_cursor_refresh_unlocked(SimpleSpiceDisplay *ssd)
  305. {
  306. if (ssd->cursor) {
  307. ssd->ds->cursor_define(ssd->cursor);
  308. cursor_put(ssd->cursor);
  309. ssd->cursor = NULL;
  310. }
  311. if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
  312. ssd->ds->mouse_set(ssd->mouse_x, ssd->mouse_y, 1);
  313. ssd->mouse_x = -1;
  314. ssd->mouse_y = -1;
  315. }
  316. }
  317. void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
  318. {
  319. dprint(3, "%s:\n", __func__);
  320. vga_hw_update();
  321. qemu_mutex_lock(&ssd->lock);
  322. if (ssd->update == NULL) {
  323. ssd->update = qemu_spice_create_update(ssd);
  324. ssd->notify++;
  325. }
  326. qemu_spice_cursor_refresh_unlocked(ssd);
  327. qemu_mutex_unlock(&ssd->lock);
  328. if (ssd->notify) {
  329. ssd->notify = 0;
  330. qemu_spice_wakeup(ssd);
  331. dprint(2, "%s: notify\n", __FUNCTION__);
  332. }
  333. }
  334. /* spice display interface callbacks */
  335. static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
  336. {
  337. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  338. dprint(1, "%s:\n", __FUNCTION__);
  339. ssd->worker = qxl_worker;
  340. }
  341. static void interface_set_compression_level(QXLInstance *sin, int level)
  342. {
  343. dprint(1, "%s:\n", __FUNCTION__);
  344. /* nothing to do */
  345. }
  346. static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
  347. {
  348. dprint(3, "%s:\n", __FUNCTION__);
  349. /* nothing to do */
  350. }
  351. static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
  352. {
  353. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  354. info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
  355. info->memslot_id_bits = MEMSLOT_SLOT_BITS;
  356. info->num_memslots = NUM_MEMSLOTS;
  357. info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
  358. info->internal_groupslot_id = 0;
  359. info->qxl_ram_size = ssd->bufsize;
  360. info->n_surfaces = NUM_SURFACES;
  361. }
  362. static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
  363. {
  364. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  365. SimpleSpiceUpdate *update;
  366. int ret = false;
  367. dprint(3, "%s:\n", __FUNCTION__);
  368. qemu_mutex_lock(&ssd->lock);
  369. if (ssd->update != NULL) {
  370. update = ssd->update;
  371. ssd->update = NULL;
  372. *ext = update->ext;
  373. ret = true;
  374. }
  375. qemu_mutex_unlock(&ssd->lock);
  376. return ret;
  377. }
  378. static int interface_req_cmd_notification(QXLInstance *sin)
  379. {
  380. dprint(1, "%s:\n", __FUNCTION__);
  381. return 1;
  382. }
  383. static void interface_release_resource(QXLInstance *sin,
  384. struct QXLReleaseInfoExt ext)
  385. {
  386. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  387. uintptr_t id;
  388. dprint(2, "%s:\n", __FUNCTION__);
  389. id = ext.info->id;
  390. qemu_spice_destroy_update(ssd, (void*)id);
  391. }
  392. static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
  393. {
  394. dprint(3, "%s:\n", __FUNCTION__);
  395. return false;
  396. }
  397. static int interface_req_cursor_notification(QXLInstance *sin)
  398. {
  399. dprint(1, "%s:\n", __FUNCTION__);
  400. return 1;
  401. }
  402. static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
  403. {
  404. fprintf(stderr, "%s: abort()\n", __FUNCTION__);
  405. abort();
  406. }
  407. static int interface_flush_resources(QXLInstance *sin)
  408. {
  409. fprintf(stderr, "%s: abort()\n", __FUNCTION__);
  410. abort();
  411. return 0;
  412. }
  413. static const QXLInterface dpy_interface = {
  414. .base.type = SPICE_INTERFACE_QXL,
  415. .base.description = "qemu simple display",
  416. .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
  417. .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
  418. .attache_worker = interface_attach_worker,
  419. .set_compression_level = interface_set_compression_level,
  420. .set_mm_time = interface_set_mm_time,
  421. .get_init_info = interface_get_init_info,
  422. /* the callbacks below are called from spice server thread context */
  423. .get_command = interface_get_command,
  424. .req_cmd_notification = interface_req_cmd_notification,
  425. .release_resource = interface_release_resource,
  426. .get_cursor_command = interface_get_cursor_command,
  427. .req_cursor_notification = interface_req_cursor_notification,
  428. .notify_update = interface_notify_update,
  429. .flush_resources = interface_flush_resources,
  430. };
  431. static SimpleSpiceDisplay sdpy;
  432. static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
  433. {
  434. qemu_spice_display_update(&sdpy, x, y, w, h);
  435. }
  436. static void display_resize(struct DisplayState *ds)
  437. {
  438. qemu_spice_display_resize(&sdpy);
  439. }
  440. static void display_refresh(struct DisplayState *ds)
  441. {
  442. qemu_spice_display_refresh(&sdpy);
  443. }
  444. static DisplayChangeListener display_listener = {
  445. .dpy_update = display_update,
  446. .dpy_resize = display_resize,
  447. .dpy_refresh = display_refresh,
  448. };
  449. void qemu_spice_display_init(DisplayState *ds)
  450. {
  451. assert(sdpy.ds == NULL);
  452. qemu_spice_display_init_common(&sdpy, ds);
  453. register_displaychangelistener(ds, &display_listener);
  454. sdpy.qxl.base.sif = &dpy_interface.base;
  455. qemu_spice_add_interface(&sdpy.qxl.base);
  456. assert(sdpy.worker);
  457. qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler, &sdpy);
  458. qemu_spice_create_host_memslot(&sdpy);
  459. qemu_spice_create_host_primary(&sdpy);
  460. }