spice-display.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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/error-report.h"
  20. #include "qemu/timer.h"
  21. #include "qemu/lockable.h"
  22. #include "qemu/main-loop.h"
  23. #include "qemu/option.h"
  24. #include "qemu/queue.h"
  25. #include "ui/console.h"
  26. #include "trace.h"
  27. #ifdef CONFIG_IOSURFACE
  28. #include <TargetConditionals.h>
  29. #endif
  30. #ifdef CONFIG_ANGLE
  31. #include <GLES2/gl2.h>
  32. #include <GLES2/gl2ext.h>
  33. #endif
  34. #include "ui/spice-display.h"
  35. bool spice_opengl;
  36. #ifdef CONFIG_ANGLE
  37. EGLContext spice_gl_ctx;
  38. #endif
  39. int qemu_spice_rect_is_empty(const QXLRect* r)
  40. {
  41. return r->top == r->bottom || r->left == r->right;
  42. }
  43. void qemu_spice_rect_union(QXLRect *dest, const QXLRect *r)
  44. {
  45. if (qemu_spice_rect_is_empty(r)) {
  46. return;
  47. }
  48. if (qemu_spice_rect_is_empty(dest)) {
  49. *dest = *r;
  50. return;
  51. }
  52. dest->top = MIN(dest->top, r->top);
  53. dest->left = MIN(dest->left, r->left);
  54. dest->bottom = MAX(dest->bottom, r->bottom);
  55. dest->right = MAX(dest->right, r->right);
  56. }
  57. QXLCookie *qxl_cookie_new(int type, uint64_t io)
  58. {
  59. QXLCookie *cookie;
  60. cookie = g_malloc0(sizeof(*cookie));
  61. cookie->type = type;
  62. cookie->io = io;
  63. return cookie;
  64. }
  65. void qemu_spice_add_memslot(SimpleSpiceDisplay *ssd, QXLDevMemSlot *memslot,
  66. qxl_async_io async)
  67. {
  68. trace_qemu_spice_add_memslot(ssd->qxl.id, memslot->slot_id,
  69. memslot->virt_start, memslot->virt_end,
  70. async);
  71. if (async != QXL_SYNC) {
  72. spice_qxl_add_memslot_async(&ssd->qxl, memslot,
  73. (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
  74. QXL_IO_MEMSLOT_ADD_ASYNC));
  75. } else {
  76. spice_qxl_add_memslot(&ssd->qxl, memslot);
  77. }
  78. }
  79. void qemu_spice_del_memslot(SimpleSpiceDisplay *ssd, uint32_t gid, uint32_t sid)
  80. {
  81. trace_qemu_spice_del_memslot(ssd->qxl.id, gid, sid);
  82. spice_qxl_del_memslot(&ssd->qxl, gid, sid);
  83. }
  84. void qemu_spice_create_primary_surface(SimpleSpiceDisplay *ssd, uint32_t id,
  85. QXLDevSurfaceCreate *surface,
  86. qxl_async_io async)
  87. {
  88. trace_qemu_spice_create_primary_surface(ssd->qxl.id, id, surface, async);
  89. if (async != QXL_SYNC) {
  90. spice_qxl_create_primary_surface_async(&ssd->qxl, id, surface,
  91. (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
  92. QXL_IO_CREATE_PRIMARY_ASYNC));
  93. } else {
  94. spice_qxl_create_primary_surface(&ssd->qxl, id, surface);
  95. }
  96. }
  97. void qemu_spice_destroy_primary_surface(SimpleSpiceDisplay *ssd,
  98. uint32_t id, qxl_async_io async)
  99. {
  100. trace_qemu_spice_destroy_primary_surface(ssd->qxl.id, id, async);
  101. if (async != QXL_SYNC) {
  102. spice_qxl_destroy_primary_surface_async(&ssd->qxl, id,
  103. (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_IO,
  104. QXL_IO_DESTROY_PRIMARY_ASYNC));
  105. } else {
  106. spice_qxl_destroy_primary_surface(&ssd->qxl, id);
  107. }
  108. }
  109. void qemu_spice_wakeup(SimpleSpiceDisplay *ssd)
  110. {
  111. trace_qemu_spice_wakeup(ssd->qxl.id);
  112. spice_qxl_wakeup(&ssd->qxl);
  113. }
  114. static void qemu_spice_create_one_update(SimpleSpiceDisplay *ssd,
  115. QXLRect *rect)
  116. {
  117. SimpleSpiceUpdate *update;
  118. QXLDrawable *drawable;
  119. QXLImage *image;
  120. QXLCommand *cmd;
  121. int bw, bh;
  122. struct timespec time_space;
  123. pixman_image_t *dest;
  124. trace_qemu_spice_create_update(
  125. rect->left, rect->right,
  126. rect->top, rect->bottom);
  127. update = g_malloc0(sizeof(*update));
  128. drawable = &update->drawable;
  129. image = &update->image;
  130. cmd = &update->ext.cmd;
  131. bw = rect->right - rect->left;
  132. bh = rect->bottom - rect->top;
  133. update->bitmap = g_malloc(bw * bh * 4);
  134. drawable->bbox = *rect;
  135. drawable->clip.type = SPICE_CLIP_TYPE_NONE;
  136. drawable->effect = QXL_EFFECT_OPAQUE;
  137. drawable->release_info.id = (uintptr_t)(&update->ext);
  138. drawable->type = QXL_DRAW_COPY;
  139. drawable->surfaces_dest[0] = -1;
  140. drawable->surfaces_dest[1] = -1;
  141. drawable->surfaces_dest[2] = -1;
  142. clock_gettime(CLOCK_MONOTONIC, &time_space);
  143. /* time in milliseconds from epoch. */
  144. drawable->mm_time = time_space.tv_sec * 1000
  145. + time_space.tv_nsec / 1000 / 1000;
  146. drawable->u.copy.rop_descriptor = SPICE_ROPD_OP_PUT;
  147. drawable->u.copy.src_bitmap = (uintptr_t)image;
  148. drawable->u.copy.src_area.right = bw;
  149. drawable->u.copy.src_area.bottom = bh;
  150. QXL_SET_IMAGE_ID(image, QXL_IMAGE_GROUP_DEVICE, ssd->unique++);
  151. image->descriptor.type = SPICE_IMAGE_TYPE_BITMAP;
  152. image->bitmap.flags = QXL_BITMAP_DIRECT | QXL_BITMAP_TOP_DOWN;
  153. image->bitmap.stride = bw * 4;
  154. image->descriptor.width = image->bitmap.x = bw;
  155. image->descriptor.height = image->bitmap.y = bh;
  156. image->bitmap.data = (uintptr_t)(update->bitmap);
  157. image->bitmap.palette = 0;
  158. image->bitmap.format = SPICE_BITMAP_FMT_32BIT;
  159. dest = pixman_image_create_bits(PIXMAN_LE_x8r8g8b8, bw, bh,
  160. (void *)update->bitmap, bw * 4);
  161. pixman_image_composite(PIXMAN_OP_SRC, ssd->surface, NULL, ssd->mirror,
  162. rect->left, rect->top, 0, 0,
  163. rect->left, rect->top, bw, bh);
  164. pixman_image_composite(PIXMAN_OP_SRC, ssd->mirror, NULL, dest,
  165. rect->left, rect->top, 0, 0,
  166. 0, 0, bw, bh);
  167. pixman_image_unref(dest);
  168. cmd->type = QXL_CMD_DRAW;
  169. cmd->data = (uintptr_t)drawable;
  170. QTAILQ_INSERT_TAIL(&ssd->updates, update, next);
  171. }
  172. static void qemu_spice_create_update(SimpleSpiceDisplay *ssd)
  173. {
  174. static const int blksize = 32;
  175. int blocks = DIV_ROUND_UP(surface_width(ssd->ds), blksize);
  176. g_autofree int *dirty_top = NULL;
  177. int y, yoff1, yoff2, x, xoff, blk, bw;
  178. int bpp = surface_bytes_per_pixel(ssd->ds);
  179. uint8_t *guest, *mirror;
  180. if (qemu_spice_rect_is_empty(&ssd->dirty)) {
  181. return;
  182. };
  183. dirty_top = g_new(int, blocks);
  184. for (blk = 0; blk < blocks; blk++) {
  185. dirty_top[blk] = -1;
  186. }
  187. guest = surface_data(ssd->ds);
  188. mirror = (void *)pixman_image_get_data(ssd->mirror);
  189. for (y = ssd->dirty.top; y < ssd->dirty.bottom; y++) {
  190. yoff1 = y * surface_stride(ssd->ds);
  191. yoff2 = y * pixman_image_get_stride(ssd->mirror);
  192. for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
  193. xoff = x * bpp;
  194. blk = x / blksize;
  195. bw = MIN(blksize, ssd->dirty.right - x);
  196. if (memcmp(guest + yoff1 + xoff,
  197. mirror + yoff2 + xoff,
  198. bw * bpp) == 0) {
  199. if (dirty_top[blk] != -1) {
  200. QXLRect update = {
  201. .top = dirty_top[blk],
  202. .bottom = y,
  203. .left = x,
  204. .right = x + bw,
  205. };
  206. qemu_spice_create_one_update(ssd, &update);
  207. dirty_top[blk] = -1;
  208. }
  209. } else {
  210. if (dirty_top[blk] == -1) {
  211. dirty_top[blk] = y;
  212. }
  213. }
  214. }
  215. }
  216. for (x = ssd->dirty.left; x < ssd->dirty.right; x += blksize) {
  217. blk = x / blksize;
  218. bw = MIN(blksize, ssd->dirty.right - x);
  219. if (dirty_top[blk] != -1) {
  220. QXLRect update = {
  221. .top = dirty_top[blk],
  222. .bottom = ssd->dirty.bottom,
  223. .left = x,
  224. .right = x + bw,
  225. };
  226. qemu_spice_create_one_update(ssd, &update);
  227. dirty_top[blk] = -1;
  228. }
  229. }
  230. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  231. }
  232. static SimpleSpiceCursor*
  233. qemu_spice_create_cursor_update(SimpleSpiceDisplay *ssd,
  234. QEMUCursor *c,
  235. bool on)
  236. {
  237. size_t size = c ? c->width * c->height * 4 : 0;
  238. SimpleSpiceCursor *update;
  239. QXLCursorCmd *ccmd;
  240. QXLCursor *cursor;
  241. QXLCommand *cmd;
  242. update = g_malloc0(sizeof(*update) + size);
  243. ccmd = &update->cmd;
  244. cursor = &update->cursor;
  245. cmd = &update->ext.cmd;
  246. if (c) {
  247. ccmd->type = QXL_CURSOR_SET;
  248. ccmd->u.set.position.x = ssd->ptr_x + ssd->hot_x;
  249. ccmd->u.set.position.y = ssd->ptr_y + ssd->hot_y;
  250. ccmd->u.set.visible = true;
  251. ccmd->u.set.shape = (uintptr_t)cursor;
  252. cursor->header.unique = ssd->unique++;
  253. cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
  254. cursor->header.width = c->width;
  255. cursor->header.height = c->height;
  256. cursor->header.hot_spot_x = c->hot_x;
  257. cursor->header.hot_spot_y = c->hot_y;
  258. cursor->data_size = size;
  259. cursor->chunk.data_size = size;
  260. memcpy(cursor->chunk.data, c->data, size);
  261. } else if (!on) {
  262. ccmd->type = QXL_CURSOR_HIDE;
  263. } else {
  264. ccmd->type = QXL_CURSOR_MOVE;
  265. ccmd->u.position.x = ssd->ptr_x + ssd->hot_x;
  266. ccmd->u.position.y = ssd->ptr_y + ssd->hot_y;
  267. }
  268. ccmd->release_info.id = (uintptr_t)(&update->ext);
  269. cmd->type = QXL_CMD_CURSOR;
  270. cmd->data = (uintptr_t)ccmd;
  271. return update;
  272. }
  273. /*
  274. * Called from spice server thread context (via interface_release_resource)
  275. * We do *not* hold the global qemu mutex here, so extra care is needed
  276. * when calling qemu functions. QEMU interfaces used:
  277. * - g_free (underlying glibc free is re-entrant).
  278. */
  279. void qemu_spice_destroy_update(SimpleSpiceDisplay *sdpy, SimpleSpiceUpdate *update)
  280. {
  281. g_free(update->bitmap);
  282. g_free(update);
  283. }
  284. void qemu_spice_create_host_memslot(SimpleSpiceDisplay *ssd)
  285. {
  286. QXLDevMemSlot memslot;
  287. memset(&memslot, 0, sizeof(memslot));
  288. memslot.slot_group_id = MEMSLOT_GROUP_HOST;
  289. memslot.virt_end = ~0;
  290. qemu_spice_add_memslot(ssd, &memslot, QXL_SYNC);
  291. }
  292. void qemu_spice_create_host_primary(SimpleSpiceDisplay *ssd)
  293. {
  294. QXLDevSurfaceCreate surface;
  295. uint64_t surface_size;
  296. memset(&surface, 0, sizeof(surface));
  297. surface_size = (uint64_t) surface_width(ssd->ds) *
  298. surface_height(ssd->ds) * 4;
  299. assert(surface_size > 0);
  300. assert(surface_size < INT_MAX);
  301. if (ssd->bufsize < surface_size) {
  302. ssd->bufsize = surface_size;
  303. g_free(ssd->buf);
  304. ssd->buf = g_malloc(ssd->bufsize);
  305. }
  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. qemu_spice_destroy_primary_surface(ssd, 0, QXL_SYNC);
  320. }
  321. void qemu_spice_display_init_common(SimpleSpiceDisplay *ssd)
  322. {
  323. qemu_mutex_init(&ssd->lock);
  324. QTAILQ_INIT(&ssd->updates);
  325. ssd->mouse_x = -1;
  326. ssd->mouse_y = -1;
  327. if (ssd->num_surfaces == 0) {
  328. ssd->num_surfaces = 1024;
  329. }
  330. }
  331. /* display listener callbacks */
  332. void qemu_spice_display_update(SimpleSpiceDisplay *ssd,
  333. int x, int y, int w, int h)
  334. {
  335. QXLRect update_area;
  336. trace_qemu_spice_display_update(ssd->qxl.id, x, y, w, h);
  337. update_area.left = x,
  338. update_area.right = x + w;
  339. update_area.top = y;
  340. update_area.bottom = y + h;
  341. if (qemu_spice_rect_is_empty(&ssd->dirty)) {
  342. ssd->notify++;
  343. }
  344. qemu_spice_rect_union(&ssd->dirty, &update_area);
  345. }
  346. void qemu_spice_display_switch(SimpleSpiceDisplay *ssd,
  347. DisplaySurface *surface)
  348. {
  349. SimpleSpiceUpdate *update;
  350. bool need_destroy;
  351. if (ssd->surface &&
  352. surface_width(surface) == pixman_image_get_width(ssd->surface) &&
  353. surface_height(surface) == pixman_image_get_height(ssd->surface) &&
  354. surface_format(surface) == pixman_image_get_format(ssd->surface)) {
  355. /* no-resize fast path: just swap backing store */
  356. trace_qemu_spice_display_surface(ssd->qxl.id,
  357. surface_width(surface),
  358. surface_height(surface),
  359. true);
  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. trace_qemu_spice_display_surface(ssd->qxl.id,
  372. surface_width(surface),
  373. surface_height(surface),
  374. false);
  375. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  376. if (ssd->surface) {
  377. pixman_image_unref(ssd->surface);
  378. ssd->surface = NULL;
  379. pixman_image_unref(ssd->mirror);
  380. ssd->mirror = NULL;
  381. }
  382. qemu_mutex_lock(&ssd->lock);
  383. need_destroy = (ssd->ds != NULL);
  384. ssd->ds = surface;
  385. while ((update = QTAILQ_FIRST(&ssd->updates)) != NULL) {
  386. QTAILQ_REMOVE(&ssd->updates, update, next);
  387. qemu_spice_destroy_update(ssd, update);
  388. }
  389. qemu_mutex_unlock(&ssd->lock);
  390. if (need_destroy) {
  391. qemu_spice_destroy_host_primary(ssd);
  392. }
  393. if (ssd->ds) {
  394. ssd->surface = pixman_image_ref(ssd->ds->image);
  395. ssd->mirror = qemu_pixman_mirror_create(surface_format(ssd->ds),
  396. ssd->ds->image);
  397. qemu_spice_create_host_primary(ssd);
  398. }
  399. memset(&ssd->dirty, 0, sizeof(ssd->dirty));
  400. ssd->notify++;
  401. qemu_mutex_lock(&ssd->lock);
  402. if (ssd->cursor) {
  403. g_free(ssd->ptr_define);
  404. ssd->ptr_define =
  405. qemu_spice_create_cursor_update(ssd, ssd->cursor, false);
  406. }
  407. qemu_mutex_unlock(&ssd->lock);
  408. }
  409. void qemu_spice_cursor_refresh_bh(void *opaque)
  410. {
  411. SimpleSpiceDisplay *ssd = opaque;
  412. qemu_mutex_lock(&ssd->lock);
  413. if (ssd->cursor) {
  414. QEMUCursor *c = ssd->cursor;
  415. assert(ssd->dcl.con);
  416. cursor_ref(c);
  417. qemu_mutex_unlock(&ssd->lock);
  418. dpy_cursor_define(ssd->dcl.con, c);
  419. qemu_mutex_lock(&ssd->lock);
  420. cursor_unref(c);
  421. }
  422. if (ssd->mouse_x != -1 && ssd->mouse_y != -1) {
  423. int x, y;
  424. assert(ssd->dcl.con);
  425. x = ssd->mouse_x;
  426. y = ssd->mouse_y;
  427. ssd->mouse_x = -1;
  428. ssd->mouse_y = -1;
  429. qemu_mutex_unlock(&ssd->lock);
  430. dpy_mouse_set(ssd->dcl.con, x, y, true);
  431. } else {
  432. qemu_mutex_unlock(&ssd->lock);
  433. }
  434. }
  435. void qemu_spice_display_refresh(SimpleSpiceDisplay *ssd)
  436. {
  437. graphic_hw_update(ssd->dcl.con);
  438. WITH_QEMU_LOCK_GUARD(&ssd->lock) {
  439. if (QTAILQ_EMPTY(&ssd->updates) && ssd->ds) {
  440. qemu_spice_create_update(ssd);
  441. ssd->notify++;
  442. }
  443. }
  444. trace_qemu_spice_display_refresh(ssd->qxl.id, ssd->notify);
  445. if (ssd->notify) {
  446. ssd->notify = 0;
  447. qemu_spice_wakeup(ssd);
  448. }
  449. }
  450. /* spice display interface callbacks */
  451. #if SPICE_HAS_ATTACHED_WORKER
  452. static void interface_attached_worker(QXLInstance *sin)
  453. {
  454. /* nothing to do */
  455. }
  456. #else
  457. static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
  458. {
  459. /* nothing to do */
  460. }
  461. #endif
  462. static void interface_set_compression_level(QXLInstance *sin, int level)
  463. {
  464. /* nothing to do */
  465. }
  466. static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
  467. {
  468. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  469. info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
  470. info->memslot_id_bits = MEMSLOT_SLOT_BITS;
  471. info->num_memslots = NUM_MEMSLOTS;
  472. info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
  473. info->internal_groupslot_id = 0;
  474. info->qxl_ram_size = 16 * 1024 * 1024;
  475. info->n_surfaces = ssd->num_surfaces;
  476. }
  477. static int interface_get_command(QXLInstance *sin, QXLCommandExt *ext)
  478. {
  479. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  480. SimpleSpiceUpdate *update;
  481. int ret = false;
  482. qemu_mutex_lock(&ssd->lock);
  483. update = QTAILQ_FIRST(&ssd->updates);
  484. if (update != NULL) {
  485. QTAILQ_REMOVE(&ssd->updates, update, next);
  486. *ext = update->ext;
  487. ret = true;
  488. }
  489. qemu_mutex_unlock(&ssd->lock);
  490. return ret;
  491. }
  492. static int interface_req_cmd_notification(QXLInstance *sin)
  493. {
  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. if (!rext.info) {
  504. return;
  505. }
  506. ext = (void *)(intptr_t)(rext.info->id);
  507. switch (ext->cmd.type) {
  508. case QXL_CMD_DRAW:
  509. update = container_of(ext, SimpleSpiceUpdate, ext);
  510. qemu_spice_destroy_update(ssd, update);
  511. break;
  512. case QXL_CMD_CURSOR:
  513. cursor = container_of(ext, SimpleSpiceCursor, ext);
  514. g_free(cursor);
  515. break;
  516. default:
  517. g_assert_not_reached();
  518. }
  519. }
  520. static int interface_get_cursor_command(QXLInstance *sin, QXLCommandExt *ext)
  521. {
  522. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  523. int ret;
  524. QEMU_LOCK_GUARD(&ssd->lock);
  525. if (ssd->ptr_define) {
  526. *ext = ssd->ptr_define->ext;
  527. ssd->ptr_define = NULL;
  528. ret = true;
  529. } else if (ssd->ptr_move) {
  530. *ext = ssd->ptr_move->ext;
  531. ssd->ptr_move = NULL;
  532. ret = true;
  533. } else {
  534. ret = false;
  535. }
  536. return ret;
  537. }
  538. static int interface_req_cursor_notification(QXLInstance *sin)
  539. {
  540. return 1;
  541. }
  542. static void interface_notify_update(QXLInstance *sin, uint32_t update_id)
  543. {
  544. fprintf(stderr, "%s: abort()\n", __func__);
  545. abort();
  546. }
  547. static int interface_flush_resources(QXLInstance *sin)
  548. {
  549. fprintf(stderr, "%s: abort()\n", __func__);
  550. abort();
  551. return 0;
  552. }
  553. static void interface_update_area_complete(QXLInstance *sin,
  554. uint32_t surface_id,
  555. QXLRect *dirty, uint32_t num_updated_rects)
  556. {
  557. /* should never be called, used in qxl native mode only */
  558. fprintf(stderr, "%s: abort()\n", __func__);
  559. abort();
  560. }
  561. /* called from spice server thread context only */
  562. static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
  563. {
  564. QXLCookie *cookie = (QXLCookie *)(uintptr_t)cookie_token;
  565. switch (cookie->type) {
  566. #ifdef HAVE_SPICE_GL
  567. case QXL_COOKIE_TYPE_GL_DRAW_DONE:
  568. {
  569. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  570. qemu_bh_schedule(ssd->gl_unblock_bh);
  571. break;
  572. }
  573. case QXL_COOKIE_TYPE_IO:
  574. if (cookie->io == QXL_IO_MONITORS_CONFIG_ASYNC) {
  575. g_free(cookie->u.data);
  576. }
  577. break;
  578. #endif
  579. default:
  580. /* should never be called, used in qxl native mode only */
  581. fprintf(stderr, "%s: abort()\n", __func__);
  582. abort();
  583. }
  584. g_free(cookie);
  585. }
  586. static void interface_set_client_capabilities(QXLInstance *sin,
  587. uint8_t client_present,
  588. uint8_t caps[58])
  589. {
  590. /* nothing to do */
  591. }
  592. static int interface_client_monitors_config(QXLInstance *sin,
  593. VDAgentMonitorsConfig *mc)
  594. {
  595. SimpleSpiceDisplay *ssd = container_of(sin, SimpleSpiceDisplay, qxl);
  596. QemuUIInfo info;
  597. int head;
  598. if (!dpy_ui_info_supported(ssd->dcl.con)) {
  599. return 0; /* == not supported by guest */
  600. }
  601. if (!mc) {
  602. return 1;
  603. }
  604. info = *dpy_get_ui_info(ssd->dcl.con);
  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. #if SPICE_SERVER_VERSION >= 0x000e04 /* release 0.14.4 */
  610. if (mc->flags & VD_AGENT_CONFIG_MONITORS_FLAG_PHYSICAL_SIZE) {
  611. VDAgentMonitorMM *mm = (void *)&mc->monitors[mc->num_of_monitors];
  612. info.width_mm = mm[head].width;
  613. info.height_mm = mm[head].height;
  614. }
  615. #endif
  616. }
  617. trace_qemu_spice_ui_info(ssd->qxl.id, info.width, info.height);
  618. dpy_set_ui_info(ssd->dcl.con, &info, false);
  619. return 1;
  620. }
  621. static const QXLInterface dpy_interface = {
  622. .base.type = SPICE_INTERFACE_QXL,
  623. .base.description = "qemu simple display",
  624. .base.major_version = SPICE_INTERFACE_QXL_MAJOR,
  625. .base.minor_version = SPICE_INTERFACE_QXL_MINOR,
  626. #if SPICE_HAS_ATTACHED_WORKER
  627. .attached_worker = interface_attached_worker,
  628. #else
  629. .attache_worker = interface_attach_worker,
  630. #endif
  631. .set_compression_level = interface_set_compression_level,
  632. .get_init_info = interface_get_init_info,
  633. /* the callbacks below are called from spice server thread context */
  634. .get_command = interface_get_command,
  635. .req_cmd_notification = interface_req_cmd_notification,
  636. .release_resource = interface_release_resource,
  637. .get_cursor_command = interface_get_cursor_command,
  638. .req_cursor_notification = interface_req_cursor_notification,
  639. .notify_update = interface_notify_update,
  640. .flush_resources = interface_flush_resources,
  641. .async_complete = interface_async_complete,
  642. .update_area_complete = interface_update_area_complete,
  643. .set_client_capabilities = interface_set_client_capabilities,
  644. .client_monitors_config = interface_client_monitors_config,
  645. };
  646. static void display_update(DisplayChangeListener *dcl,
  647. int x, int y, int w, int h)
  648. {
  649. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  650. qemu_spice_display_update(ssd, x, y, w, h);
  651. }
  652. static void display_switch(DisplayChangeListener *dcl,
  653. DisplaySurface *surface)
  654. {
  655. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  656. qemu_spice_display_switch(ssd, surface);
  657. }
  658. static void display_refresh(DisplayChangeListener *dcl)
  659. {
  660. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  661. qemu_spice_display_refresh(ssd);
  662. }
  663. static void display_mouse_set(DisplayChangeListener *dcl,
  664. int x, int y, bool on)
  665. {
  666. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  667. qemu_mutex_lock(&ssd->lock);
  668. ssd->ptr_x = x;
  669. ssd->ptr_y = y;
  670. g_free(ssd->ptr_move);
  671. ssd->ptr_move = qemu_spice_create_cursor_update(ssd, NULL, on);
  672. qemu_mutex_unlock(&ssd->lock);
  673. qemu_spice_wakeup(ssd);
  674. }
  675. static void display_mouse_define(DisplayChangeListener *dcl,
  676. QEMUCursor *c)
  677. {
  678. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  679. qemu_mutex_lock(&ssd->lock);
  680. cursor_ref(c);
  681. cursor_unref(ssd->cursor);
  682. ssd->cursor = c;
  683. ssd->hot_x = c->hot_x;
  684. ssd->hot_y = c->hot_y;
  685. g_free(ssd->ptr_move);
  686. ssd->ptr_move = NULL;
  687. g_free(ssd->ptr_define);
  688. ssd->ptr_define = qemu_spice_create_cursor_update(ssd, c, false);
  689. qemu_mutex_unlock(&ssd->lock);
  690. qemu_spice_wakeup(ssd);
  691. }
  692. static const DisplayChangeListenerOps display_listener_ops = {
  693. .dpy_name = "spice",
  694. .dpy_gfx_update = display_update,
  695. .dpy_gfx_switch = display_switch,
  696. .dpy_gfx_check_format = qemu_pixman_check_format,
  697. .dpy_refresh = display_refresh,
  698. .dpy_mouse_set = display_mouse_set,
  699. .dpy_cursor_define = display_mouse_define,
  700. };
  701. #ifdef HAVE_SPICE_GL
  702. #if defined(CONFIG_IOSURFACE)
  703. static void AddIntegerValue(CFMutableDictionaryRef dictionary, const CFStringRef key, int32_t value)
  704. {
  705. CFNumberRef number = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &value);
  706. CFDictionaryAddValue(dictionary, key, number);
  707. CFRelease(number);
  708. }
  709. static int spice_iosurface_create(SimpleSpiceDisplay *ssd, int width, int height)
  710. {
  711. CFMutableDictionaryRef dict = CFDictionaryCreateMutable(
  712. kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  713. AddIntegerValue(dict, kIOSurfaceWidth, width);
  714. AddIntegerValue(dict, kIOSurfaceHeight, height);
  715. AddIntegerValue(dict, kIOSurfacePixelFormat, 'BGRA');
  716. AddIntegerValue(dict, kIOSurfaceBytesPerElement, 4);
  717. #if TARGET_OS_OSX
  718. CFDictionaryAddValue(dict, kIOSurfaceIsGlobal, kCFBooleanTrue);
  719. #endif
  720. ssd->iosurface = IOSurfaceCreate(dict);
  721. CFRelease(dict);
  722. if (!ssd->iosurface) {
  723. error_report("spice_iosurface_create: IOSurfaceCreate failed");
  724. return 0;
  725. }
  726. #if defined(CONFIG_ANGLE)
  727. EGLint target = 0;
  728. GLenum tex_target = 0;
  729. if (eglGetConfigAttrib(qemu_egl_display,
  730. qemu_egl_config,
  731. EGL_BIND_TO_TEXTURE_TARGET_ANGLE,
  732. &target) != EGL_TRUE) {
  733. error_report("spice_iosurface_create: eglGetConfigAttrib failed");
  734. goto gl_error;
  735. }
  736. if (target == EGL_TEXTURE_2D) {
  737. tex_target = GL_TEXTURE_2D;
  738. } else if (target == EGL_TEXTURE_RECTANGLE_ANGLE) {
  739. tex_target = GL_TEXTURE_RECTANGLE_ANGLE;
  740. } else {
  741. error_report("spice_iosurface_create: unsupported texture target");
  742. goto gl_error;
  743. }
  744. const EGLint attribs[] = {
  745. EGL_WIDTH, width,
  746. EGL_HEIGHT, height,
  747. EGL_IOSURFACE_PLANE_ANGLE, 0,
  748. EGL_TEXTURE_TARGET, target,
  749. EGL_TEXTURE_INTERNAL_FORMAT_ANGLE, GL_BGRA_EXT,
  750. EGL_TEXTURE_FORMAT, EGL_TEXTURE_RGBA,
  751. EGL_TEXTURE_TYPE_ANGLE, GL_UNSIGNED_BYTE,
  752. EGL_IOSURFACE_USAGE_HINT_ANGLE, EGL_IOSURFACE_WRITE_HINT_ANGLE,
  753. EGL_NONE, EGL_NONE,
  754. };
  755. ssd->esurface = qemu_egl_init_buffer_surface(spice_gl_ctx,
  756. EGL_IOSURFACE_ANGLE,
  757. ssd->iosurface,
  758. attribs);
  759. if (ssd->esurface == NULL) {
  760. goto gl_error;
  761. }
  762. egl_fb_setup_new_tex_target(&ssd->iosurface_fb, width, height, tex_target);
  763. eglBindTexImage(qemu_egl_display, ssd->esurface, EGL_BACK_BUFFER);
  764. return 1;
  765. gl_error:
  766. CFRelease(ssd->iosurface);
  767. ssd->iosurface = NULL;
  768. return 0;
  769. #else
  770. error_report("spice_iosurface_create: ANGLE not found");
  771. return 0;
  772. #endif
  773. }
  774. static void spice_iosurface_destroy(SimpleSpiceDisplay *ssd)
  775. {
  776. if (!ssd->iosurface) {
  777. return;
  778. }
  779. #if defined(CONFIG_ANGLE)
  780. eglMakeCurrent(qemu_egl_display, ssd->esurface, ssd->esurface, spice_gl_ctx);
  781. eglReleaseTexImage(qemu_egl_display, ssd->esurface, EGL_BACK_BUFFER);
  782. egl_fb_destroy(&ssd->iosurface_fb);
  783. qemu_egl_destroy_surface(ssd->esurface);
  784. ssd->esurface = EGL_NO_SURFACE;
  785. #endif
  786. if (ssd->surface_send_fd > -1) {
  787. // this sends POLLHUP and indicates that any unread data is stale
  788. // and should not be used
  789. close(ssd->surface_send_fd);
  790. ssd->surface_send_fd = -1;
  791. }
  792. // FIXME: still a tiny race with the close() above
  793. CFRelease(ssd->iosurface);
  794. ssd->iosurface = NULL;
  795. }
  796. static int spice_iosurface_resize(SimpleSpiceDisplay *ssd, int width, int height)
  797. {
  798. if (ssd->iosurface) {
  799. if (IOSurfaceGetHeight(ssd->iosurface) != width ||
  800. IOSurfaceGetWidth(ssd->iosurface) != height) {
  801. spice_iosurface_destroy(ssd);
  802. return spice_iosurface_create(ssd, width, height);
  803. } else {
  804. return 1;
  805. }
  806. } else {
  807. return spice_iosurface_create(ssd, width, height);
  808. }
  809. }
  810. /* FIXME: use mach ports instead of this hack */
  811. static int spice_iosurface_create_fd(SimpleSpiceDisplay *ssd, int *fourcc)
  812. {
  813. int fds[2];
  814. IOSurfaceID surfaceid;
  815. if (!ssd->iosurface) {
  816. return -1;
  817. }
  818. if (pipe(fds) < 0) {
  819. error_report("spice_iosurface_create_fd: failed to create pipe");
  820. return -1;
  821. }
  822. if (ssd->surface_send_fd > -1) {
  823. close(ssd->surface_send_fd);
  824. }
  825. // we keep the write end of the pipe open for the lifetime of this surface
  826. // when we close it, POLLHUP will be seen by the other side and know that
  827. // the surface ID is stale and should not be used
  828. ssd->surface_send_fd = fds[1];
  829. *fourcc = 'BGRA';
  830. surfaceid = IOSurfaceGetID(ssd->iosurface);
  831. write(ssd->surface_send_fd, &surfaceid, sizeof(surfaceid));
  832. return fds[0];
  833. }
  834. static void spice_iosurface_blit(SimpleSpiceDisplay *ssd, GLuint src_texture, bool flip, bool swap)
  835. {
  836. egl_fb tmp_fb = { .texture = src_texture, .texture_target = GL_TEXTURE_2D };
  837. if (!ssd->iosurface) {
  838. return;
  839. }
  840. #if defined(CONFIG_ANGLE)
  841. eglMakeCurrent(qemu_egl_display, ssd->esurface, ssd->esurface, spice_gl_ctx);
  842. egl_texture_blit(ssd->gls, &ssd->iosurface_fb, &tmp_fb, flip, swap);
  843. #endif
  844. }
  845. #endif
  846. static void qemu_spice_gl_monitor_config(SimpleSpiceDisplay *ssd,
  847. int x, int y, int w, int h)
  848. {
  849. QXLMonitorsConfig *config;
  850. QXLCookie *cookie;
  851. config = g_malloc0(sizeof(QXLMonitorsConfig) + sizeof(QXLHead));
  852. config->count = 1;
  853. config->max_allowed = 1;
  854. config->heads[0].x = x;
  855. config->heads[0].y = y;
  856. config->heads[0].width = w;
  857. config->heads[0].height = h;
  858. cookie = qxl_cookie_new(QXL_COOKIE_TYPE_IO,
  859. QXL_IO_MONITORS_CONFIG_ASYNC);
  860. cookie->u.data = config;
  861. spice_qxl_monitors_config_async(&ssd->qxl,
  862. (uintptr_t)config,
  863. MEMSLOT_GROUP_HOST,
  864. (uintptr_t)cookie);
  865. }
  866. static void qemu_spice_gl_block(SimpleSpiceDisplay *ssd, bool block)
  867. {
  868. uint64_t timeout;
  869. if (block) {
  870. timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  871. timeout += 1000; /* one sec */
  872. timer_mod(ssd->gl_unblock_timer, timeout);
  873. } else {
  874. timer_del(ssd->gl_unblock_timer);
  875. }
  876. graphic_hw_gl_block(ssd->dcl.con, block);
  877. }
  878. static void qemu_spice_gl_unblock_bh(void *opaque)
  879. {
  880. SimpleSpiceDisplay *ssd = opaque;
  881. qemu_spice_gl_block(ssd, false);
  882. }
  883. static void qemu_spice_gl_block_timer(void *opaque)
  884. {
  885. warn_report("spice: no gl-draw-done within one second");
  886. }
  887. static void spice_gl_refresh(DisplayChangeListener *dcl)
  888. {
  889. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  890. uint64_t cookie;
  891. if (!ssd->ds || qemu_console_is_gl_blocked(ssd->dcl.con)) {
  892. return;
  893. }
  894. graphic_hw_update(dcl->con);
  895. if (ssd->gl_updates && ssd->have_surface) {
  896. qemu_spice_gl_block(ssd, true);
  897. glFlush();
  898. cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
  899. spice_qxl_gl_draw_async(&ssd->qxl, 0, 0,
  900. surface_width(ssd->ds),
  901. surface_height(ssd->ds),
  902. cookie);
  903. ssd->gl_updates = 0;
  904. }
  905. }
  906. static void spice_gl_update(DisplayChangeListener *dcl,
  907. int x, int y, int w, int h)
  908. {
  909. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  910. surface_gl_update_texture(ssd->gls, ssd->ds, x, y, w, h);
  911. #if defined(CONFIG_IOSURFACE)
  912. if (!qemu_console_is_gl_blocked(ssd->dcl.con)) {
  913. spice_iosurface_blit(ssd, ssd->ds->texture, true, ssd->ds->glswapped);
  914. }
  915. #endif
  916. ssd->gl_updates++;
  917. }
  918. static void spice_gl_switch(DisplayChangeListener *dcl,
  919. struct DisplaySurface *new_surface)
  920. {
  921. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  922. EGLint stride = 0, fourcc = 0;
  923. int fd = -1;
  924. int width = 0, height = 0;
  925. if (ssd->ds) {
  926. surface_gl_destroy_texture(ssd->gls, ssd->ds);
  927. }
  928. ssd->ds = new_surface;
  929. width = surface_width(new_surface);
  930. height = surface_height(new_surface);
  931. if (ssd->ds) {
  932. surface_gl_create_texture(ssd->gls, ssd->ds);
  933. #if defined(CONFIG_GBM)
  934. fd = egl_get_fd_for_texture(ssd->ds->texture,
  935. &stride, &fourcc,
  936. NULL);
  937. if (fd < 0) {
  938. surface_gl_destroy_texture(ssd->gls, ssd->ds);
  939. return;
  940. }
  941. #elif defined(CONFIG_IOSURFACE)
  942. if (spice_iosurface_resize(ssd, width, height)) {
  943. fd = spice_iosurface_create_fd(ssd, &fourcc);
  944. if (fd < 0) {
  945. error_report("spice_gl_switch: failed to create fd");
  946. return;
  947. }
  948. } else {
  949. error_report("spice_gl_switch: failed to create IOSurface");
  950. return;
  951. }
  952. #endif
  953. trace_qemu_spice_gl_surface(ssd->qxl.id,
  954. width,
  955. height,
  956. fourcc);
  957. /* note: spice server will close the fd */
  958. spice_qxl_gl_scanout(&ssd->qxl, fd,
  959. width,
  960. height,
  961. stride, fourcc, false);
  962. ssd->have_surface = true;
  963. ssd->have_scanout = false;
  964. qemu_spice_gl_monitor_config(ssd, 0, 0,
  965. width,
  966. height);
  967. }
  968. }
  969. static QEMUGLContext qemu_spice_gl_create_context(DisplayGLCtx *dgc,
  970. QEMUGLParams *params)
  971. {
  972. #if defined(CONFIG_GBM)
  973. eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
  974. qemu_egl_rn_ctx);
  975. #elif defined(CONFIG_ANGLE)
  976. eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
  977. spice_gl_ctx);
  978. #endif
  979. return qemu_egl_create_context(dgc, params);
  980. }
  981. static void qemu_spice_gl_scanout_disable(DisplayChangeListener *dcl)
  982. {
  983. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  984. trace_qemu_spice_gl_scanout_disable(ssd->qxl.id);
  985. spice_qxl_gl_scanout(&ssd->qxl, -1, 0, 0, 0, 0, false);
  986. qemu_spice_gl_monitor_config(ssd, 0, 0, 0, 0);
  987. ssd->have_surface = false;
  988. ssd->have_scanout = false;
  989. #if defined(CONFIG_IOSURFACE)
  990. spice_iosurface_destroy(ssd);
  991. #endif
  992. #if defined(CONFIG_ANGLE)
  993. ssd->backing_borrow = NULL;
  994. ssd->backing_id = -1;
  995. #endif
  996. }
  997. static void qemu_spice_gl_scanout_texture(DisplayChangeListener *dcl,
  998. uint32_t backing_id,
  999. DisplayGLTextureBorrower backing_borrow,
  1000. uint32_t x, uint32_t y,
  1001. uint32_t w, uint32_t h)
  1002. {
  1003. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  1004. EGLint stride = 0, fourcc = 0;
  1005. int fd = -1;
  1006. bool y_0_top;
  1007. uint32_t backing_width;
  1008. uint32_t backing_height;
  1009. void *d3d_tex2d;
  1010. GLuint tex_id = backing_borrow(backing_id, &y_0_top,
  1011. &backing_width, &backing_height,
  1012. &d3d_tex2d);
  1013. assert(tex_id);
  1014. #if defined(CONFIG_GBM)
  1015. fd = egl_get_fd_for_texture(tex_id, &stride, &fourcc, NULL);
  1016. #elif defined(CONFIG_IOSURFACE)
  1017. if (spice_iosurface_resize(ssd, backing_width, backing_height)) {
  1018. #if defined(CONFIG_ANGLE)
  1019. ssd->backing_borrow = backing_borrow;
  1020. ssd->backing_id = backing_id;
  1021. #endif
  1022. fd = spice_iosurface_create_fd(ssd, &fourcc);
  1023. } else {
  1024. fd = -1;
  1025. }
  1026. #endif
  1027. if (fd < 0) {
  1028. fprintf(stderr, "%s: failed to get fd for texture\n", __func__);
  1029. return;
  1030. }
  1031. trace_qemu_spice_gl_scanout_texture(ssd->qxl.id, w, h, fourcc);
  1032. /* note: spice server will close the fd */
  1033. spice_qxl_gl_scanout(&ssd->qxl, fd, backing_width, backing_height,
  1034. stride, fourcc, y_0_top);
  1035. qemu_spice_gl_monitor_config(ssd, x, y, w, h);
  1036. ssd->have_surface = false;
  1037. ssd->have_scanout = true;
  1038. }
  1039. #ifdef CONFIG_GBM
  1040. static void qemu_spice_gl_scanout_dmabuf(DisplayChangeListener *dcl,
  1041. QemuDmaBuf *dmabuf)
  1042. {
  1043. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  1044. ssd->guest_dmabuf = dmabuf;
  1045. ssd->guest_dmabuf_refresh = true;
  1046. ssd->have_surface = false;
  1047. ssd->have_scanout = true;
  1048. }
  1049. static void qemu_spice_gl_cursor_dmabuf(DisplayChangeListener *dcl,
  1050. QemuDmaBuf *dmabuf, bool have_hot,
  1051. uint32_t hot_x, uint32_t hot_y)
  1052. {
  1053. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  1054. uint32_t width, height, texture;
  1055. ssd->have_hot = have_hot;
  1056. ssd->hot_x = hot_x;
  1057. ssd->hot_y = hot_y;
  1058. trace_qemu_spice_gl_cursor(ssd->qxl.id, dmabuf != NULL, have_hot);
  1059. if (dmabuf) {
  1060. egl_dmabuf_import_texture(dmabuf);
  1061. texture = qemu_dmabuf_get_texture(dmabuf);
  1062. if (!texture) {
  1063. return;
  1064. }
  1065. width = qemu_dmabuf_get_width(dmabuf);
  1066. height = qemu_dmabuf_get_height(dmabuf);
  1067. egl_fb_setup_for_tex(&ssd->cursor_fb, width, height, texture, false);
  1068. } else {
  1069. egl_fb_destroy(&ssd->cursor_fb);
  1070. }
  1071. }
  1072. static void qemu_spice_gl_cursor_position(DisplayChangeListener *dcl,
  1073. uint32_t pos_x, uint32_t pos_y)
  1074. {
  1075. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  1076. qemu_mutex_lock(&ssd->lock);
  1077. ssd->ptr_x = pos_x;
  1078. ssd->ptr_y = pos_y;
  1079. qemu_mutex_unlock(&ssd->lock);
  1080. }
  1081. static void qemu_spice_gl_release_dmabuf(DisplayChangeListener *dcl,
  1082. QemuDmaBuf *dmabuf)
  1083. {
  1084. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  1085. if (ssd->guest_dmabuf == dmabuf) {
  1086. ssd->guest_dmabuf = NULL;
  1087. ssd->guest_dmabuf_refresh = false;
  1088. }
  1089. egl_dmabuf_release_texture(dmabuf);
  1090. }
  1091. #endif // CONFIG_GBM
  1092. static void qemu_spice_gl_update(DisplayChangeListener *dcl,
  1093. uint32_t x, uint32_t y, uint32_t w, uint32_t h)
  1094. {
  1095. SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
  1096. #ifdef CONFIG_GBM
  1097. EGLint stride = 0, fourcc = 0;
  1098. int fd;
  1099. bool render_cursor = false;
  1100. #endif
  1101. bool y_0_top = false; /* FIXME */
  1102. uint64_t cookie;
  1103. int fd;
  1104. uint32_t width, height, texture;
  1105. if (!ssd->have_scanout) {
  1106. return;
  1107. }
  1108. #ifdef CONFIG_GBM
  1109. if (ssd->cursor_fb.texture) {
  1110. render_cursor = true;
  1111. }
  1112. if (ssd->render_cursor != render_cursor) {
  1113. ssd->render_cursor = render_cursor;
  1114. ssd->guest_dmabuf_refresh = true;
  1115. egl_fb_destroy(&ssd->blit_fb);
  1116. }
  1117. if (ssd->guest_dmabuf_refresh) {
  1118. QemuDmaBuf *dmabuf = ssd->guest_dmabuf;
  1119. width = qemu_dmabuf_get_width(dmabuf);
  1120. height = qemu_dmabuf_get_height(dmabuf);
  1121. if (render_cursor) {
  1122. egl_dmabuf_import_texture(dmabuf);
  1123. texture = qemu_dmabuf_get_texture(dmabuf);
  1124. if (!texture) {
  1125. return;
  1126. }
  1127. /* source framebuffer */
  1128. egl_fb_setup_for_tex(&ssd->guest_fb, width, height,
  1129. texture, false);
  1130. /* dest framebuffer */
  1131. if (ssd->blit_fb.width != width ||
  1132. ssd->blit_fb.height != height) {
  1133. trace_qemu_spice_gl_render_dmabuf(ssd->qxl.id, width,
  1134. height);
  1135. egl_fb_destroy(&ssd->blit_fb);
  1136. egl_fb_setup_new_tex(&ssd->blit_fb,
  1137. width, height);
  1138. fd = egl_get_fd_for_texture(ssd->blit_fb.texture,
  1139. &stride, &fourcc, NULL);
  1140. spice_qxl_gl_scanout(&ssd->qxl, fd, width, height,
  1141. stride, fourcc, false);
  1142. }
  1143. } else {
  1144. stride = qemu_dmabuf_get_stride(dmabuf);
  1145. fourcc = qemu_dmabuf_get_fourcc(dmabuf);
  1146. y_0_top = qemu_dmabuf_get_y0_top(dmabuf);
  1147. fd = qemu_dmabuf_dup_fd(dmabuf);
  1148. trace_qemu_spice_gl_forward_dmabuf(ssd->qxl.id, width, height);
  1149. /* note: spice server will close the fd, so hand over a dup */
  1150. spice_qxl_gl_scanout(&ssd->qxl, fd, width, height,
  1151. stride, fourcc, y_0_top);
  1152. }
  1153. qemu_spice_gl_monitor_config(ssd, 0, 0, width, height);
  1154. ssd->guest_dmabuf_refresh = false;
  1155. }
  1156. if (render_cursor) {
  1157. int ptr_x, ptr_y;
  1158. qemu_mutex_lock(&ssd->lock);
  1159. ptr_x = ssd->ptr_x;
  1160. ptr_y = ssd->ptr_y;
  1161. qemu_mutex_unlock(&ssd->lock);
  1162. egl_texture_blit(ssd->gls, &ssd->blit_fb, &ssd->guest_fb,
  1163. !y_0_top, false);
  1164. egl_texture_blend(ssd->gls, &ssd->blit_fb, &ssd->cursor_fb,
  1165. !y_0_top, false, ptr_x, ptr_y, 1.0, 1.0);
  1166. glFlush();
  1167. }
  1168. #elif defined(CONFIG_ANGLE) && defined(CONFIG_IOSURFACE)
  1169. GLuint tex_id = ssd->backing_borrow(ssd->backing_id, &y_0_top,
  1170. NULL, NULL, NULL);
  1171. spice_iosurface_blit(ssd, tex_id, !y_0_top, false);
  1172. //TODO: cursor stuff
  1173. #endif
  1174. trace_qemu_spice_gl_update(ssd->qxl.id, w, h, x, y);
  1175. qemu_spice_gl_block(ssd, true);
  1176. glFlush();
  1177. cookie = (uintptr_t)qxl_cookie_new(QXL_COOKIE_TYPE_GL_DRAW_DONE, 0);
  1178. spice_qxl_gl_draw_async(&ssd->qxl, x, y, w, h, cookie);
  1179. }
  1180. static const DisplayChangeListenerOps display_listener_gl_ops = {
  1181. .dpy_name = "spice-egl",
  1182. .dpy_gfx_update = spice_gl_update,
  1183. .dpy_gfx_switch = spice_gl_switch,
  1184. .dpy_gfx_check_format = console_gl_check_format,
  1185. .dpy_refresh = spice_gl_refresh,
  1186. .dpy_mouse_set = display_mouse_set,
  1187. .dpy_cursor_define = display_mouse_define,
  1188. .dpy_gl_scanout_disable = qemu_spice_gl_scanout_disable,
  1189. .dpy_gl_scanout_texture = qemu_spice_gl_scanout_texture,
  1190. #ifdef CONFIG_GBM
  1191. .dpy_gl_scanout_dmabuf = qemu_spice_gl_scanout_dmabuf,
  1192. .dpy_gl_cursor_dmabuf = qemu_spice_gl_cursor_dmabuf,
  1193. .dpy_gl_cursor_position = qemu_spice_gl_cursor_position,
  1194. .dpy_gl_release_dmabuf = qemu_spice_gl_release_dmabuf,
  1195. #endif
  1196. .dpy_gl_update = qemu_spice_gl_update,
  1197. };
  1198. static bool
  1199. qemu_spice_is_compatible_dcl(DisplayGLCtx *dgc,
  1200. DisplayChangeListener *dcl)
  1201. {
  1202. return dcl->ops == &display_listener_gl_ops;
  1203. }
  1204. static const DisplayGLCtxOps gl_ctx_ops = {
  1205. .dpy_gl_ctx_is_compatible_dcl = qemu_spice_is_compatible_dcl,
  1206. .dpy_gl_ctx_create = qemu_spice_gl_create_context,
  1207. .dpy_gl_ctx_destroy = qemu_egl_destroy_context,
  1208. .dpy_gl_ctx_make_current = qemu_egl_make_context_current,
  1209. };
  1210. #endif /* HAVE_SPICE_GL */
  1211. static void qemu_spice_display_init_one(QemuConsole *con)
  1212. {
  1213. SimpleSpiceDisplay *ssd = g_new0(SimpleSpiceDisplay, 1);
  1214. qemu_spice_display_init_common(ssd);
  1215. ssd->dcl.ops = &display_listener_ops;
  1216. #ifdef HAVE_SPICE_GL
  1217. if (spice_opengl) {
  1218. ssd->dcl.ops = &display_listener_gl_ops;
  1219. ssd->dgc.ops = &gl_ctx_ops;
  1220. ssd->gl_unblock_bh = qemu_bh_new(qemu_spice_gl_unblock_bh, ssd);
  1221. ssd->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
  1222. qemu_spice_gl_block_timer, ssd);
  1223. ssd->gls = qemu_gl_init_shader();
  1224. ssd->have_surface = false;
  1225. ssd->have_scanout = false;
  1226. #if defined(CONFIG_IOSURFACE)
  1227. ssd->iosurface = NULL;
  1228. ssd->surface_send_fd = -1;
  1229. #endif
  1230. #if defined(CONFIG_ANGLE)
  1231. ssd->esurface = EGL_NO_SURFACE;
  1232. ssd->backing_borrow = NULL;
  1233. ssd->backing_id = -1;
  1234. #endif
  1235. }
  1236. #endif
  1237. ssd->dcl.con = con;
  1238. ssd->qxl.base.sif = &dpy_interface.base;
  1239. qemu_spice_add_display_interface(&ssd->qxl, con);
  1240. #if SPICE_SERVER_VERSION >= 0x000e02 /* release 0.14.2 */
  1241. Error *err = NULL;
  1242. char device_address[256] = "";
  1243. if (qemu_console_fill_device_address(con, device_address, 256, &err)) {
  1244. spice_qxl_set_device_info(&ssd->qxl,
  1245. device_address,
  1246. qemu_console_get_head(con),
  1247. 1);
  1248. } else {
  1249. error_report_err(err);
  1250. }
  1251. #endif
  1252. qemu_spice_create_host_memslot(ssd);
  1253. if (spice_opengl) {
  1254. qemu_console_set_display_gl_ctx(con, &ssd->dgc);
  1255. }
  1256. register_displaychangelistener(&ssd->dcl);
  1257. }
  1258. void qemu_spice_display_init(void)
  1259. {
  1260. QemuOptsList *olist = qemu_find_opts("spice");
  1261. QemuOpts *opts = QTAILQ_FIRST(&olist->head);
  1262. QemuConsole *spice_con, *con;
  1263. const char *str;
  1264. int i;
  1265. str = qemu_opt_get(opts, "display");
  1266. if (str) {
  1267. int head = qemu_opt_get_number(opts, "head", 0);
  1268. Error *err = NULL;
  1269. spice_con = qemu_console_lookup_by_device_name(str, head, &err);
  1270. if (err) {
  1271. error_report("Failed to lookup display/head");
  1272. exit(1);
  1273. }
  1274. } else {
  1275. spice_con = NULL;
  1276. }
  1277. for (i = 0;; i++) {
  1278. con = qemu_console_lookup_by_index(i);
  1279. if (!con || !qemu_console_is_graphic(con)) {
  1280. break;
  1281. }
  1282. if (qemu_spice_have_display_interface(con)) {
  1283. continue;
  1284. }
  1285. if (spice_con != NULL && spice_con != con) {
  1286. continue;
  1287. }
  1288. qemu_spice_display_init_one(con);
  1289. }
  1290. qemu_spice_display_init_done();
  1291. }