dbus-listener.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * QEMU DBus display console
  3. *
  4. * Copyright (c) 2021 Marc-André Lureau <marcandre.lureau@redhat.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/error-report.h"
  26. #include "sysemu/sysemu.h"
  27. #include "dbus.h"
  28. #include <gio/gunixfdlist.h>
  29. #ifdef CONFIG_OPENGL
  30. #include "ui/shader.h"
  31. #include "ui/egl-helpers.h"
  32. #include "ui/egl-context.h"
  33. #endif
  34. #include "trace.h"
  35. struct _DBusDisplayListener {
  36. GObject parent;
  37. char *bus_name;
  38. DBusDisplayConsole *console;
  39. GDBusConnection *conn;
  40. QemuDBusDisplay1Listener *proxy;
  41. DisplayChangeListener dcl;
  42. DisplaySurface *ds;
  43. int gl_updates;
  44. };
  45. G_DEFINE_TYPE(DBusDisplayListener, dbus_display_listener, G_TYPE_OBJECT)
  46. #ifdef CONFIG_GBM
  47. static void dbus_update_gl_cb(GObject *source_object,
  48. GAsyncResult *res,
  49. gpointer user_data)
  50. {
  51. g_autoptr(GError) err = NULL;
  52. DBusDisplayListener *ddl = user_data;
  53. if (!qemu_dbus_display1_listener_call_update_dmabuf_finish(ddl->proxy,
  54. res, &err)) {
  55. error_report("Failed to call update: %s", err->message);
  56. }
  57. graphic_hw_gl_block(ddl->dcl.con, false);
  58. g_object_unref(ddl);
  59. }
  60. static void dbus_call_update_gl(DBusDisplayListener *ddl,
  61. int x, int y, int w, int h)
  62. {
  63. graphic_hw_gl_block(ddl->dcl.con, true);
  64. glFlush();
  65. qemu_dbus_display1_listener_call_update_dmabuf(ddl->proxy,
  66. x, y, w, h,
  67. G_DBUS_CALL_FLAGS_NONE,
  68. DBUS_DEFAULT_TIMEOUT, NULL,
  69. dbus_update_gl_cb,
  70. g_object_ref(ddl));
  71. }
  72. static void dbus_scanout_disable(DisplayChangeListener *dcl)
  73. {
  74. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  75. ddl->ds = NULL;
  76. qemu_dbus_display1_listener_call_disable(
  77. ddl->proxy, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
  78. }
  79. static void dbus_scanout_dmabuf(DisplayChangeListener *dcl,
  80. QemuDmaBuf *dmabuf)
  81. {
  82. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  83. g_autoptr(GError) err = NULL;
  84. g_autoptr(GUnixFDList) fd_list = NULL;
  85. fd_list = g_unix_fd_list_new();
  86. if (g_unix_fd_list_append(fd_list, dmabuf->fd, &err) != 0) {
  87. error_report("Failed to setup dmabuf fdlist: %s", err->message);
  88. return;
  89. }
  90. qemu_dbus_display1_listener_call_scanout_dmabuf(
  91. ddl->proxy,
  92. g_variant_new_handle(0),
  93. dmabuf->width,
  94. dmabuf->height,
  95. dmabuf->stride,
  96. dmabuf->fourcc,
  97. dmabuf->modifier,
  98. dmabuf->y0_top,
  99. G_DBUS_CALL_FLAGS_NONE,
  100. -1,
  101. fd_list,
  102. NULL, NULL, NULL);
  103. }
  104. static void dbus_scanout_borrowed_texture(DisplayChangeListener *dcl,
  105. uint32_t tex_id,
  106. bool backing_y_0_top,
  107. uint32_t backing_width,
  108. uint32_t backing_height)
  109. {
  110. QemuDmaBuf dmabuf = {
  111. .width = backing_width,
  112. .height = backing_height,
  113. .y0_top = backing_y_0_top,
  114. };
  115. assert(tex_id);
  116. dmabuf.fd = egl_get_fd_for_texture(
  117. tex_id, (EGLint *)&dmabuf.stride,
  118. (EGLint *)&dmabuf.fourcc,
  119. &dmabuf.modifier);
  120. if (dmabuf.fd < 0) {
  121. error_report("%s: failed to get fd for texture", __func__);
  122. return;
  123. }
  124. dbus_scanout_dmabuf(dcl, &dmabuf);
  125. close(dmabuf.fd);
  126. }
  127. static void dbus_scanout_texture(DisplayChangeListener *dcl,
  128. uint32_t backing_id,
  129. DisplayGLTextureBorrower backing_borrow,
  130. uint32_t x, uint32_t y,
  131. uint32_t w, uint32_t h)
  132. {
  133. bool backing_y_0_top;
  134. uint32_t backing_width;
  135. uint32_t backing_height;
  136. uint32_t tex_id = backing_borrow(backing_id, &backing_y_0_top,
  137. &backing_width, &backing_height);
  138. dbus_scanout_borrowed_texture(dcl, tex_id, backing_y_0_top,
  139. backing_width, backing_height);
  140. }
  141. static void dbus_cursor_dmabuf(DisplayChangeListener *dcl,
  142. QemuDmaBuf *dmabuf, bool have_hot,
  143. uint32_t hot_x, uint32_t hot_y)
  144. {
  145. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  146. DisplaySurface *ds;
  147. GVariant *v_data = NULL;
  148. egl_fb cursor_fb = EGL_FB_INIT;
  149. if (!dmabuf) {
  150. qemu_dbus_display1_listener_call_mouse_set(
  151. ddl->proxy, 0, 0, false,
  152. G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
  153. return;
  154. }
  155. egl_dmabuf_import_texture(dmabuf);
  156. if (!dmabuf->texture) {
  157. return;
  158. }
  159. egl_fb_setup_for_tex(&cursor_fb, dmabuf->width, dmabuf->height,
  160. dmabuf->texture, false);
  161. ds = qemu_create_displaysurface(dmabuf->width, dmabuf->height);
  162. egl_fb_read(ds, &cursor_fb);
  163. v_data = g_variant_new_from_data(
  164. G_VARIANT_TYPE("ay"),
  165. surface_data(ds),
  166. surface_width(ds) * surface_height(ds) * 4,
  167. TRUE,
  168. (GDestroyNotify)qemu_free_displaysurface,
  169. ds);
  170. qemu_dbus_display1_listener_call_cursor_define(
  171. ddl->proxy,
  172. surface_width(ds),
  173. surface_height(ds),
  174. hot_x,
  175. hot_y,
  176. v_data,
  177. G_DBUS_CALL_FLAGS_NONE,
  178. -1,
  179. NULL,
  180. NULL,
  181. NULL);
  182. }
  183. static void dbus_cursor_position(DisplayChangeListener *dcl,
  184. uint32_t pos_x, uint32_t pos_y)
  185. {
  186. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  187. qemu_dbus_display1_listener_call_mouse_set(
  188. ddl->proxy, pos_x, pos_y, true,
  189. G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
  190. }
  191. static void dbus_release_dmabuf(DisplayChangeListener *dcl,
  192. QemuDmaBuf *dmabuf)
  193. {
  194. dbus_scanout_disable(dcl);
  195. }
  196. static void dbus_scanout_update(DisplayChangeListener *dcl,
  197. uint32_t x, uint32_t y,
  198. uint32_t w, uint32_t h)
  199. {
  200. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  201. dbus_call_update_gl(ddl, x, y, w, h);
  202. }
  203. static void dbus_gl_refresh(DisplayChangeListener *dcl)
  204. {
  205. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  206. graphic_hw_update(dcl->con);
  207. if (!ddl->ds || qemu_console_is_gl_blocked(ddl->dcl.con)) {
  208. return;
  209. }
  210. if (ddl->gl_updates) {
  211. dbus_call_update_gl(ddl, 0, 0,
  212. surface_width(ddl->ds), surface_height(ddl->ds));
  213. ddl->gl_updates = 0;
  214. }
  215. }
  216. #endif
  217. static void dbus_refresh(DisplayChangeListener *dcl)
  218. {
  219. graphic_hw_update(dcl->con);
  220. }
  221. #ifdef CONFIG_GBM
  222. static void dbus_gl_gfx_update(DisplayChangeListener *dcl,
  223. int x, int y, int w, int h)
  224. {
  225. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  226. ddl->gl_updates++;
  227. }
  228. #endif
  229. static void dbus_gfx_update(DisplayChangeListener *dcl,
  230. int x, int y, int w, int h)
  231. {
  232. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  233. pixman_image_t *img;
  234. GVariant *v_data;
  235. size_t stride;
  236. assert(ddl->ds);
  237. stride = w * DIV_ROUND_UP(PIXMAN_FORMAT_BPP(surface_format(ddl->ds)), 8);
  238. trace_dbus_update(x, y, w, h);
  239. if (x == 0 && y == 0 && w == surface_width(ddl->ds) && h == surface_height(ddl->ds)) {
  240. v_data = g_variant_new_from_data(
  241. G_VARIANT_TYPE("ay"),
  242. surface_data(ddl->ds),
  243. surface_stride(ddl->ds) * surface_height(ddl->ds),
  244. TRUE,
  245. (GDestroyNotify)pixman_image_unref,
  246. pixman_image_ref(ddl->ds->image));
  247. qemu_dbus_display1_listener_call_scanout(
  248. ddl->proxy,
  249. surface_width(ddl->ds),
  250. surface_height(ddl->ds),
  251. surface_stride(ddl->ds),
  252. surface_format(ddl->ds),
  253. v_data,
  254. G_DBUS_CALL_FLAGS_NONE,
  255. DBUS_DEFAULT_TIMEOUT, NULL, NULL, NULL);
  256. return;
  257. }
  258. /* make a copy, since gvariant only handles linear data */
  259. img = pixman_image_create_bits(surface_format(ddl->ds),
  260. w, h, NULL, stride);
  261. pixman_image_composite(PIXMAN_OP_SRC, ddl->ds->image, NULL, img,
  262. x, y, 0, 0, 0, 0, w, h);
  263. v_data = g_variant_new_from_data(
  264. G_VARIANT_TYPE("ay"),
  265. pixman_image_get_data(img),
  266. pixman_image_get_stride(img) * h,
  267. TRUE,
  268. (GDestroyNotify)pixman_image_unref,
  269. img);
  270. qemu_dbus_display1_listener_call_update(ddl->proxy,
  271. x, y, w, h, pixman_image_get_stride(img), pixman_image_get_format(img),
  272. v_data,
  273. G_DBUS_CALL_FLAGS_NONE,
  274. DBUS_DEFAULT_TIMEOUT, NULL, NULL, NULL);
  275. }
  276. #ifdef CONFIG_GBM
  277. static void dbus_gl_gfx_switch(DisplayChangeListener *dcl,
  278. struct DisplaySurface *new_surface)
  279. {
  280. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  281. ddl->ds = new_surface;
  282. if (ddl->ds) {
  283. int width = surface_width(ddl->ds);
  284. int height = surface_height(ddl->ds);
  285. /* TODO: lazy send dmabuf (there are unnecessary sent otherwise) */
  286. dbus_scanout_borrowed_texture(&ddl->dcl, ddl->ds->texture, false,
  287. width, height);
  288. }
  289. }
  290. #endif
  291. static void dbus_gfx_switch(DisplayChangeListener *dcl,
  292. struct DisplaySurface *new_surface)
  293. {
  294. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  295. ddl->ds = new_surface;
  296. if (!ddl->ds) {
  297. /* why not call disable instead? */
  298. return;
  299. }
  300. }
  301. static void dbus_mouse_set(DisplayChangeListener *dcl,
  302. int x, int y, int on)
  303. {
  304. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  305. qemu_dbus_display1_listener_call_mouse_set(
  306. ddl->proxy, x, y, on, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
  307. }
  308. static void dbus_cursor_define(DisplayChangeListener *dcl,
  309. QEMUCursor *c)
  310. {
  311. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  312. GVariant *v_data = NULL;
  313. v_data = g_variant_new_from_data(
  314. G_VARIANT_TYPE("ay"),
  315. c->data,
  316. c->width * c->height * 4,
  317. TRUE,
  318. (GDestroyNotify)cursor_unref,
  319. cursor_ref(c));
  320. qemu_dbus_display1_listener_call_cursor_define(
  321. ddl->proxy,
  322. c->width,
  323. c->height,
  324. c->hot_x,
  325. c->hot_y,
  326. v_data,
  327. G_DBUS_CALL_FLAGS_NONE,
  328. -1,
  329. NULL,
  330. NULL,
  331. NULL);
  332. }
  333. #ifdef CONFIG_GBM
  334. const DisplayChangeListenerOps dbus_gl_dcl_ops = {
  335. .dpy_name = "dbus-gl",
  336. .dpy_gfx_update = dbus_gl_gfx_update,
  337. .dpy_gfx_switch = dbus_gl_gfx_switch,
  338. .dpy_gfx_check_format = console_gl_check_format,
  339. .dpy_refresh = dbus_gl_refresh,
  340. .dpy_mouse_set = dbus_mouse_set,
  341. .dpy_cursor_define = dbus_cursor_define,
  342. .dpy_gl_scanout_disable = dbus_scanout_disable,
  343. .dpy_gl_scanout_texture = dbus_scanout_texture,
  344. .dpy_gl_scanout_dmabuf = dbus_scanout_dmabuf,
  345. .dpy_gl_cursor_dmabuf = dbus_cursor_dmabuf,
  346. .dpy_gl_cursor_position = dbus_cursor_position,
  347. .dpy_gl_release_dmabuf = dbus_release_dmabuf,
  348. .dpy_gl_update = dbus_scanout_update,
  349. };
  350. #endif
  351. const DisplayChangeListenerOps dbus_dcl_ops = {
  352. .dpy_name = "dbus",
  353. .dpy_gfx_update = dbus_gfx_update,
  354. .dpy_gfx_switch = dbus_gfx_switch,
  355. .dpy_refresh = dbus_refresh,
  356. .dpy_mouse_set = dbus_mouse_set,
  357. .dpy_cursor_define = dbus_cursor_define,
  358. };
  359. static void
  360. dbus_display_listener_dispose(GObject *object)
  361. {
  362. DBusDisplayListener *ddl = DBUS_DISPLAY_LISTENER(object);
  363. unregister_displaychangelistener(&ddl->dcl);
  364. g_clear_object(&ddl->conn);
  365. g_clear_pointer(&ddl->bus_name, g_free);
  366. g_clear_object(&ddl->proxy);
  367. G_OBJECT_CLASS(dbus_display_listener_parent_class)->dispose(object);
  368. }
  369. static void
  370. dbus_display_listener_constructed(GObject *object)
  371. {
  372. DBusDisplayListener *ddl = DBUS_DISPLAY_LISTENER(object);
  373. ddl->dcl.ops = &dbus_dcl_ops;
  374. #ifdef CONFIG_GBM
  375. if (display_opengl) {
  376. ddl->dcl.ops = &dbus_gl_dcl_ops;
  377. }
  378. #endif
  379. G_OBJECT_CLASS(dbus_display_listener_parent_class)->constructed(object);
  380. }
  381. static void
  382. dbus_display_listener_class_init(DBusDisplayListenerClass *klass)
  383. {
  384. GObjectClass *object_class = G_OBJECT_CLASS(klass);
  385. object_class->dispose = dbus_display_listener_dispose;
  386. object_class->constructed = dbus_display_listener_constructed;
  387. }
  388. static void
  389. dbus_display_listener_init(DBusDisplayListener *ddl)
  390. {
  391. }
  392. const char *
  393. dbus_display_listener_get_bus_name(DBusDisplayListener *ddl)
  394. {
  395. return ddl->bus_name ?: "p2p";
  396. }
  397. DBusDisplayConsole *
  398. dbus_display_listener_get_console(DBusDisplayListener *ddl)
  399. {
  400. return ddl->console;
  401. }
  402. DBusDisplayListener *
  403. dbus_display_listener_new(const char *bus_name,
  404. GDBusConnection *conn,
  405. DBusDisplayConsole *console)
  406. {
  407. DBusDisplayListener *ddl;
  408. QemuConsole *con;
  409. g_autoptr(GError) err = NULL;
  410. ddl = g_object_new(DBUS_DISPLAY_TYPE_LISTENER, NULL);
  411. ddl->proxy =
  412. qemu_dbus_display1_listener_proxy_new_sync(conn,
  413. G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
  414. NULL,
  415. "/org/qemu/Display1/Listener",
  416. NULL,
  417. &err);
  418. if (!ddl->proxy) {
  419. error_report("Failed to setup proxy: %s", err->message);
  420. g_object_unref(conn);
  421. g_object_unref(ddl);
  422. return NULL;
  423. }
  424. ddl->bus_name = g_strdup(bus_name);
  425. ddl->conn = conn;
  426. ddl->console = console;
  427. con = qemu_console_lookup_by_index(dbus_display_console_get_index(console));
  428. assert(con);
  429. ddl->dcl.con = con;
  430. register_displaychangelistener(&ddl->dcl);
  431. return ddl;
  432. }