spice-display.c 45 KB

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