spice-display.c 38 KB

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