spice-display.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 "spice-display.h"
  25. static int debug = 0;
  26. static void GCC_FMT_ATTR(2, 3) dprint(int level, const char *fmt, ...)
  27. {
  28. va_list args;
  29. if (level <= debug) {
  30. va_start(args, fmt);
  31. vfprintf(stderr, fmt, args);
  32. va_end(args);
  33. }
  34. }
  35. int qemu_spice_rect_is_empty(const QXLRect* r)
  36. {
  37. return r->top == r->bottom || r->left == r->right;
  38. }
  39. void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
  40. {
  41. if (qemu_spice_rect_is_empty(r)) {
  42. return;
  43. }
  44. if (qemu_spice_rect_is_empty(dest)) {
  45. *dest = *r;
  46. return;
  47. }
  48. dest->top = MIN(dest->top, r->top);
  49. dest->left = MIN(dest->left, r->left);
  50. dest->bottom = MAX(dest->bottom, r->bottom);
  51. dest->right = MAX(dest->right, r->right);
  52. }
  53. void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
  54. qxl_async_io async)
  55. {
  56. if (async != QXL_SYNC) {
  57. #if SPICE_INTERFACE_QXL_MINOR >= 1
  58. spice_qxl_add_memslot_async(&ssd->qxl, memslot, 0);
  59. #else
  60. abort();
  61. #endif
  62. } else {
  63. ssd->worker->add_memslot(ssd->worker, memslot);
  64. }
  65. }
  66. void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
  67. {
  68. ssd->worker->del_memslot(ssd->worker, gid, sid);
  69. }
  70. void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
  71. QXLDevSurfaceCreate *surface,
  72. qxl_async_io async)
  73. {
  74. if (async != QXL_SYNC) {
  75. #if SPICE_INTERFACE_QXL_MINOR >= 1
  76. spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface, 0);
  77. #else
  78. abort();
  79. #endif
  80. } else {
  81. ssd->worker->create_primary_surface(ssd->worker, id, surface);
  82. }
  83. }
  84. void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
  85. uint32_t id, qxl_async_io async)
  86. {
  87. if (async != QXL_SYNC) {
  88. #if SPICE_INTERFACE_QXL_MINOR >= 1
  89. spice_qxl_destroy_primary_surface_async(&ssd->qxl, id, 0);
  90. #else
  91. abort();
  92. #endif
  93. } else {
  94. ssd->worker->destroy_primary_surface(ssd->worker, id);
  95. }
  96. }
  97. void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
  98. {
  99. ssd->worker->wakeup(ssd->worker);
  100. }
  101. void qemu_spice_start(SimpleSpiceDisplay *ssd)
  102. {
  103. ssd->worker->start(ssd->worker);
  104. }
  105. void qemu_spice_stop(SimpleSpiceDisplay *ssd)
  106. {
  107. ssd->worker->stop(ssd->worker);
  108. }
  109. static SimpleSpiceUpdate *qemu_spice_create_update(SimpleSpiceDisplay *ssd)
  110. {
  111. SimpleSpiceUpdate *update;
  112. QXLDrawable *drawable;
  113. QXLImage *image;
  114. QXLCommand *cmd;
  115. uint8_t *src, *dst;
  116. int by, bw, bh;
  117. struct timespec time_space;
  118. if (qemu_spice_rect_is_empty(&ssd->dirty)) {
  119. return NULL;
  120. };
  121. dprint(2, "%s: lr %d -> %d, tb -> %d -> %d\n", __FUNCTION__,
  122. ssd->dirty.left, ssd->dirty.right,
  123. ssd->dirty.top, ssd->dirty.bottom);
  124. update = g_malloc0(sizeof(*update));
  125. drawable = &update->drawable;
  126. image = &update->image;
  127. cmd = &update->ext.cmd;
  128. bw = ssd->dirty.right - ssd->dirty.left;
  129. bh = ssd->dirty.bottom - ssd->dirty.top;
  130. update->bitmap = g_malloc(bw * bh * 4);
  131. drawable->bbox = ssd->dirty;
  132. drawable->clip.type = SPICE_CLIP_TYPE_NONE;
  133. drawable->effect = QXL_EFFECT_OPAQUE;
  134. drawable->release_info.id = (intptr_t)update;
  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 = (intptr_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 = (intptr_t)(update->bitmap);
  154. image->bitmap.palette = 0;
  155. image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
  156. if (ssd->conv == NULL) {
  157. PixelFormat dst = qemu_default_pixelformat(32);
  158. ssd->conv = qemu_pf_conv_get(&dst, &ssd->ds->surface->pf);
  159. assert(ssd->conv);
  160. }
  161. src = ds_get_data(ssd->ds) +
  162. ssd->dirty.top * ds_get_linesize(ssd->ds) +
  163. ssd->dirty.left * ds_get_bytes_per_pixel(ssd->ds);
  164. dst = update->bitmap;
  165. for (by = 0; by < bh; by++) {
  166. qemu_pf_conv_run(ssd->conv, dst, src, bw);
  167. src += ds_get_linesize(ssd->ds);
  168. dst += image->bitmap.stride;
  169. }
  170. cmd->type = QXL_CMD_DRAW;
  171. cmd->data = (intptr_t)drawable;
  172. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  173. return update;
  174. }
  175. /*
  176. * Called from spice server thread context (via interface_release_ressource)
  177. * We do *not* hold the global qemu mutex here, so extra care is needed
  178. * when calling qemu functions. Qemu interfaces used:
  179. * - g_free (underlying glibc free is re-entrant).
  180. */
  181. void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
  182. {
  183. g_free(update->bitmap);
  184. g_free(update);
  185. }
  186. void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
  187. {
  188. QXLDevMemSlot memslot;
  189. dprint(1, "%s:\n", __FUNCTION__);
  190. memset(&memslot, 0, sizeof(memslot));
  191. memslot.slot_group_id = MEMSLOT_GROUP_HOST;
  192. memslot.virt_end = ~0;
  193. qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
  194. }
  195. void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
  196. {
  197. QXLDevSurfaceCreate surface;
  198. dprint(1, "%s: %dx%d\n", __FUNCTION__,
  199. ds_get_width(ssd->ds), ds_get_height(ssd->ds));
  200. surface.format = SPICE_SURFACE_FMT_32_xRGB;
  201. surface.width = ds_get_width(ssd->ds);
  202. surface.height = ds_get_height(ssd->ds);
  203. surface.stride = -surface.width * 4;
  204. surface.mouse_mode = true;
  205. surface.flags = 0;
  206. surface.type = 0;
  207. surface.mem = (intptr_t)ssd->buf;
  208. surface.group_id = MEMSLOT_GROUP_HOST;
  209. qemu_spice_create_primary_surface(ssd, 0, &surface, QXL_SYNC);
  210. }
  211. void qemu_spice_destroy_host_primary(SimpleSpiceDisplay *ssd)
  212. {
  213. dprint(1, "%s:\n", __FUNCTION__);
  214. qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
  215. }
  216. void qemu_spice_vm_change_state_handler(void *opaque, int running,
  217. RunState state)
  218. {
  219. SimpleSpiceDisplay *ssd = opaque;
  220. if (running) {
  221. ssd->running = true;
  222. qemu_spice_start(ssd);
  223. } else {
  224. qemu_spice_stop(ssd);
  225. ssd->running = false;
  226. }
  227. }
  228. void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd, DisplayState *ds)
  229. {
  230. ssd->ds = ds;
  231. qemu_mutex_init(&ssd->lock);
  232. ssd->mouse_x = -1;
  233. ssd->mouse_y = -1;
  234. ssd->bufsize = (16 * 1024 * 1024);
  235. ssd->buf = g_malloc(ssd->bufsize);
  236. }
  237. /* display listener callbacks */
  238. void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
  239. int x, int y, int w, int h)
  240. {
  241. QXLRect update_area;
  242. dprint(2, "%s: x %d y %d w %d h %d\n", __FUNCTION__, x, y, w, h);
  243. update_area.left = x,
  244. update_area.right = x + w;
  245. update_area.top = y;
  246. update_area.bottom = y + h;
  247. if (qemu_spice_rect_is_empty(&ssd->dirty)) {
  248. ssd->notify++;
  249. }
  250. qemu_spice_rect_union(&ssd->dirty, &update_area);
  251. }
  252. void qemu_spice_display_resize(SimpleSpiceDisplay *ssd)
  253. {
  254. dprint(1, "%s:\n", __FUNCTION__);
  255. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  256. qemu_pf_conv_put(ssd->conv);
  257. ssd->conv = NULL;
  258. qemu_mutex_lock(&ssd->lock);
  259. if (ssd->update != NULL) {
  260. qemu_spice_destroy_update(ssd, ssd->update);
  261. ssd->update = NULL;
  262. }
  263. qemu_mutex_unlock(&ssd->lock);
  264. qemu_spice_destroy_host_primary(ssd);
  265. qemu_spice_create_host_primary(ssd);
  266. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  267. ssd->notify++;
  268. }
  269. void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
  270. {
  271. dprint(3, "%s:\n", __FUNCTION__);
  272. vga_hw_update();
  273. qemu_mutex_lock(&ssd->lock);
  274. if (ssd->update == NULL) {
  275. ssd->update = qemu_spice_create_update(ssd);
  276. ssd->notify++;
  277. }
  278. if (ssd->cursor) {
  279. ssd->ds->cursor_define(ssd->cursor);
  280. cursor_put(ssd->cursor);
  281. ssd->cursor = NULL;
  282. }
  283. if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
  284. ssd->ds->mouse_set(ssd->mouse_x, ssd->mouse_y, 1);
  285. ssd->mouse_x = -1;
  286. ssd->mouse_y = -1;
  287. }
  288. qemu_mutex_unlock(&ssd->lock);
  289. if (ssd->notify) {
  290. ssd->notify = 0;
  291. qemu_spice_wakeup(ssd);
  292. dprint(2, "%s: notify\n", __FUNCTION__);
  293. }
  294. }
  295. /* spice display interface callbacks */
  296. static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
  297. {
  298. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  299. dprint(1, "%s:\n", __FUNCTION__);
  300. ssd->worker = qxl_worker;
  301. }
  302. static void interface_set_compression_level(QXLInstance *sin, int level)
  303. {
  304. dprint(1, "%s:\n", __FUNCTION__);
  305. /* nothing to do */
  306. }
  307. static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
  308. {
  309. dprint(3, "%s:\n", __FUNCTION__);
  310. /* nothing to do */
  311. }
  312. static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
  313. {
  314. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  315. info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
  316. info->memslot_id_bits = MEMSLOT_SLOT_BITS;
  317. info->num_memslots = NUM_MEMSLOTS;
  318. info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
  319. info->internal_groupslot_id = 0;
  320. info->qxl_ram_size = ssd->bufsize;
  321. info->n_surfaces = NUM_SURFACES;
  322. }
  323. static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
  324. {
  325. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  326. SimpleSpiceUpdate *update;
  327. int ret = false;
  328. dprint(3, "%s:\n", __FUNCTION__);
  329. qemu_mutex_lock(&ssd->lock);
  330. if (ssd->update != NULL) {
  331. update = ssd->update;
  332. ssd->update = NULL;
  333. *ext = update->ext;
  334. ret = true;
  335. }
  336. qemu_mutex_unlock(&ssd->lock);
  337. return ret;
  338. }
  339. static int interface_req_cmd_notification(QXLInstance *sin)
  340. {
  341. dprint(1, "%s:\n", __FUNCTION__);
  342. return 1;
  343. }
  344. static void interface_release_resource(QXLInstance *sin,
  345. struct QXLReleaseInfoExt ext)
  346. {
  347. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  348. uintptr_t id;
  349. dprint(2, "%s:\n", __FUNCTION__);
  350. id = ext.info->id;
  351. qemu_spice_destroy_update(ssd, (void*)id);
  352. }
  353. static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
  354. {
  355. dprint(3, "%s:\n", __FUNCTION__);
  356. return false;
  357. }
  358. static int interface_req_cursor_notification(QXLInstance *sin)
  359. {
  360. dprint(1, "%s:\n", __FUNCTION__);
  361. return 1;
  362. }
  363. static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
  364. {
  365. fprintf(stderr, "%s: abort()\n", __FUNCTION__);
  366. abort();
  367. }
  368. static int interface_flush_resources(QXLInstance *sin)
  369. {
  370. fprintf(stderr, "%s: abort()\n", __FUNCTION__);
  371. abort();
  372. return 0;
  373. }
  374. static const QXLInterface dpy_interface = {
  375. .base.type = SPICE_INTERFACE_QXL,
  376. .base.description = "qemu simple display",
  377. .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
  378. .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
  379. .attache_worker = interface_attach_worker,
  380. .set_compression_level = interface_set_compression_level,
  381. .set_mm_time = interface_set_mm_time,
  382. .get_init_info = interface_get_init_info,
  383. /* the callbacks below are called from spice server thread context */
  384. .get_command = interface_get_command,
  385. .req_cmd_notification = interface_req_cmd_notification,
  386. .release_resource = interface_release_resource,
  387. .get_cursor_command = interface_get_cursor_command,
  388. .req_cursor_notification = interface_req_cursor_notification,
  389. .notify_update = interface_notify_update,
  390. .flush_resources = interface_flush_resources,
  391. };
  392. static SimpleSpiceDisplay sdpy;
  393. static void display_update(struct DisplayState *ds, int x, int y, int w, int h)
  394. {
  395. qemu_spice_display_update(&sdpy, x, y, w, h);
  396. }
  397. static void display_resize(struct DisplayState *ds)
  398. {
  399. qemu_spice_display_resize(&sdpy);
  400. }
  401. static void display_refresh(struct DisplayState *ds)
  402. {
  403. qemu_spice_display_refresh(&sdpy);
  404. }
  405. static DisplayChangeListener display_listener = {
  406. .dpy_update = display_update,
  407. .dpy_resize = display_resize,
  408. .dpy_refresh = display_refresh,
  409. };
  410. void qemu_spice_display_init(DisplayState *ds)
  411. {
  412. assert(sdpy.ds == NULL);
  413. qemu_spice_display_init_common(&sdpy, ds);
  414. register_displaychangelistener(ds, &display_listener);
  415. sdpy.qxl.base.sif = &dpy_interface.base;
  416. qemu_spice_add_interface(&sdpy.qxl.base);
  417. assert(sdpy.worker);
  418. qemu_add_vm_change_state_handler(qemu_spice_vm_change_state_handler, &sdpy);
  419. qemu_spice_create_host_memslot(&sdpy);
  420. qemu_spice_create_host_primary(&sdpy);
  421. }