dbus-listener.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 "sysemu/sysemu.h"
  26. #include "dbus.h"
  27. #include <gio/gunixfdlist.h>
  28. #include "ui/shader.h"
  29. #include "ui/egl-helpers.h"
  30. #include "ui/egl-context.h"
  31. #include "trace.h"
  32. struct _DBusDisplayListener {
  33. GObject parent;
  34. char *bus_name;
  35. DBusDisplayConsole *console;
  36. GDBusConnection *conn;
  37. QemuDBusDisplay1Listener *proxy;
  38. DisplayChangeListener dcl;
  39. DisplaySurface *ds;
  40. QemuGLShader *gls;
  41. int gl_updates;
  42. };
  43. G_DEFINE_TYPE(DBusDisplayListener, dbus_display_listener, G_TYPE_OBJECT)
  44. static void dbus_update_gl_cb(GObject *source_object,
  45. GAsyncResult *res,
  46. gpointer user_data)
  47. {
  48. g_autoptr(GError) err = NULL;
  49. DBusDisplayListener *ddl = user_data;
  50. if (!qemu_dbus_display1_listener_call_update_dmabuf_finish(ddl->proxy,
  51. res, &err)) {
  52. error_report("Failed to call update: %s", err->message);
  53. }
  54. graphic_hw_gl_block(ddl->dcl.con, false);
  55. g_object_unref(ddl);
  56. }
  57. static void dbus_call_update_gl(DBusDisplayListener *ddl,
  58. int x, int y, int w, int h)
  59. {
  60. graphic_hw_gl_block(ddl->dcl.con, true);
  61. glFlush();
  62. qemu_dbus_display1_listener_call_update_dmabuf(ddl->proxy,
  63. x, y, w, h,
  64. G_DBUS_CALL_FLAGS_NONE,
  65. DBUS_DEFAULT_TIMEOUT, NULL,
  66. dbus_update_gl_cb,
  67. g_object_ref(ddl));
  68. }
  69. static void dbus_scanout_disable(DisplayChangeListener *dcl)
  70. {
  71. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  72. ddl->ds = NULL;
  73. qemu_dbus_display1_listener_call_disable(
  74. ddl->proxy, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
  75. }
  76. static void dbus_scanout_dmabuf(DisplayChangeListener *dcl,
  77. QemuDmaBuf *dmabuf)
  78. {
  79. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  80. g_autoptr(GError) err = NULL;
  81. g_autoptr(GUnixFDList) fd_list = NULL;
  82. fd_list = g_unix_fd_list_new();
  83. if (g_unix_fd_list_append(fd_list, dmabuf->fd, &err) != 0) {
  84. error_report("Failed to setup dmabuf fdlist: %s", err->message);
  85. return;
  86. }
  87. qemu_dbus_display1_listener_call_scanout_dmabuf(
  88. ddl->proxy,
  89. g_variant_new_handle(0),
  90. dmabuf->width,
  91. dmabuf->height,
  92. dmabuf->stride,
  93. dmabuf->fourcc,
  94. dmabuf->modifier,
  95. dmabuf->y0_top,
  96. G_DBUS_CALL_FLAGS_NONE,
  97. -1,
  98. fd_list,
  99. NULL, NULL, NULL);
  100. }
  101. static void dbus_scanout_texture(DisplayChangeListener *dcl,
  102. uint32_t tex_id,
  103. bool backing_y_0_top,
  104. uint32_t backing_width,
  105. uint32_t backing_height,
  106. uint32_t x, uint32_t y,
  107. uint32_t w, uint32_t h)
  108. {
  109. QemuDmaBuf dmabuf = {
  110. .width = backing_width,
  111. .height = backing_height,
  112. .y0_top = backing_y_0_top,
  113. };
  114. assert(tex_id);
  115. dmabuf.fd = egl_get_fd_for_texture(
  116. tex_id, (EGLint *)&dmabuf.stride,
  117. (EGLint *)&dmabuf.fourcc,
  118. &dmabuf.modifier);
  119. if (dmabuf.fd < 0) {
  120. error_report("%s: failed to get fd for texture", __func__);
  121. return;
  122. }
  123. dbus_scanout_dmabuf(dcl, &dmabuf);
  124. close(dmabuf.fd);
  125. }
  126. static void dbus_cursor_dmabuf(DisplayChangeListener *dcl,
  127. QemuDmaBuf *dmabuf, bool have_hot,
  128. uint32_t hot_x, uint32_t hot_y)
  129. {
  130. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  131. DisplaySurface *ds;
  132. GVariant *v_data = NULL;
  133. egl_fb cursor_fb;
  134. if (!dmabuf) {
  135. qemu_dbus_display1_listener_call_mouse_set(
  136. ddl->proxy, 0, 0, false,
  137. G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
  138. return;
  139. }
  140. egl_dmabuf_import_texture(dmabuf);
  141. if (!dmabuf->texture) {
  142. return;
  143. }
  144. egl_fb_setup_for_tex(&cursor_fb, dmabuf->width, dmabuf->height,
  145. dmabuf->texture, false);
  146. ds = qemu_create_displaysurface(dmabuf->width, dmabuf->height);
  147. egl_fb_read(ds, &cursor_fb);
  148. v_data = g_variant_new_from_data(
  149. G_VARIANT_TYPE("ay"),
  150. surface_data(ds),
  151. surface_width(ds) * surface_height(ds) * 4,
  152. TRUE,
  153. (GDestroyNotify)qemu_free_displaysurface,
  154. ds);
  155. qemu_dbus_display1_listener_call_cursor_define(
  156. ddl->proxy,
  157. surface_width(ds),
  158. surface_height(ds),
  159. hot_x,
  160. hot_y,
  161. v_data,
  162. G_DBUS_CALL_FLAGS_NONE,
  163. -1,
  164. NULL,
  165. NULL,
  166. NULL);
  167. }
  168. static void dbus_cursor_position(DisplayChangeListener *dcl,
  169. uint32_t pos_x, uint32_t pos_y)
  170. {
  171. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  172. qemu_dbus_display1_listener_call_mouse_set(
  173. ddl->proxy, pos_x, pos_y, true,
  174. G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
  175. }
  176. static void dbus_release_dmabuf(DisplayChangeListener *dcl,
  177. QemuDmaBuf *dmabuf)
  178. {
  179. dbus_scanout_disable(dcl);
  180. }
  181. static void dbus_scanout_update(DisplayChangeListener *dcl,
  182. uint32_t x, uint32_t y,
  183. uint32_t w, uint32_t h)
  184. {
  185. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  186. dbus_call_update_gl(ddl, x, y, w, h);
  187. }
  188. static void dbus_gl_refresh(DisplayChangeListener *dcl)
  189. {
  190. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  191. graphic_hw_update(dcl->con);
  192. if (!ddl->ds || qemu_console_is_gl_blocked(ddl->dcl.con)) {
  193. return;
  194. }
  195. if (ddl->gl_updates) {
  196. dbus_call_update_gl(ddl, 0, 0,
  197. surface_width(ddl->ds), surface_height(ddl->ds));
  198. ddl->gl_updates = 0;
  199. }
  200. }
  201. static void dbus_refresh(DisplayChangeListener *dcl)
  202. {
  203. graphic_hw_update(dcl->con);
  204. }
  205. static void dbus_gl_gfx_update(DisplayChangeListener *dcl,
  206. int x, int y, int w, int h)
  207. {
  208. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  209. if (ddl->ds) {
  210. surface_gl_update_texture(ddl->gls, ddl->ds, x, y, w, h);
  211. }
  212. ddl->gl_updates++;
  213. }
  214. static void dbus_gfx_update(DisplayChangeListener *dcl,
  215. int x, int y, int w, int h)
  216. {
  217. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  218. pixman_image_t *img;
  219. GVariant *v_data;
  220. size_t stride;
  221. assert(ddl->ds);
  222. stride = w * DIV_ROUND_UP(PIXMAN_FORMAT_BPP(surface_format(ddl->ds)), 8);
  223. trace_dbus_update(x, y, w, h);
  224. /* make a copy, since gvariant only handles linear data */
  225. img = pixman_image_create_bits(surface_format(ddl->ds),
  226. w, h, NULL, stride);
  227. pixman_image_composite(PIXMAN_OP_SRC, ddl->ds->image, NULL, img,
  228. x, y, 0, 0, 0, 0, w, h);
  229. v_data = g_variant_new_from_data(
  230. G_VARIANT_TYPE("ay"),
  231. pixman_image_get_data(img),
  232. pixman_image_get_stride(img) * h,
  233. TRUE,
  234. (GDestroyNotify)pixman_image_unref,
  235. img);
  236. qemu_dbus_display1_listener_call_update(ddl->proxy,
  237. x, y, w, h, pixman_image_get_stride(img), pixman_image_get_format(img),
  238. v_data,
  239. G_DBUS_CALL_FLAGS_NONE,
  240. DBUS_DEFAULT_TIMEOUT, NULL, NULL, NULL);
  241. }
  242. static void dbus_gl_gfx_switch(DisplayChangeListener *dcl,
  243. struct DisplaySurface *new_surface)
  244. {
  245. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  246. if (ddl->ds) {
  247. surface_gl_destroy_texture(ddl->gls, ddl->ds);
  248. }
  249. ddl->ds = new_surface;
  250. if (ddl->ds) {
  251. int width = surface_width(ddl->ds);
  252. int height = surface_height(ddl->ds);
  253. surface_gl_create_texture(ddl->gls, ddl->ds);
  254. /* TODO: lazy send dmabuf (there are unnecessary sent otherwise) */
  255. dbus_scanout_texture(&ddl->dcl, ddl->ds->texture, false,
  256. width, height, 0, 0, width, height);
  257. }
  258. }
  259. static void dbus_gfx_switch(DisplayChangeListener *dcl,
  260. struct DisplaySurface *new_surface)
  261. {
  262. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  263. GVariant *v_data = NULL;
  264. ddl->ds = new_surface;
  265. if (!ddl->ds) {
  266. /* why not call disable instead? */
  267. return;
  268. }
  269. v_data = g_variant_new_from_data(
  270. G_VARIANT_TYPE("ay"),
  271. surface_data(ddl->ds),
  272. surface_stride(ddl->ds) * surface_height(ddl->ds),
  273. TRUE,
  274. (GDestroyNotify)pixman_image_unref,
  275. pixman_image_ref(ddl->ds->image));
  276. qemu_dbus_display1_listener_call_scanout(ddl->proxy,
  277. surface_width(ddl->ds),
  278. surface_height(ddl->ds),
  279. surface_stride(ddl->ds),
  280. surface_format(ddl->ds),
  281. v_data,
  282. G_DBUS_CALL_FLAGS_NONE,
  283. DBUS_DEFAULT_TIMEOUT, NULL, NULL, NULL);
  284. }
  285. static void dbus_mouse_set(DisplayChangeListener *dcl,
  286. int x, int y, int on)
  287. {
  288. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  289. qemu_dbus_display1_listener_call_mouse_set(
  290. ddl->proxy, x, y, on, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
  291. }
  292. static void dbus_cursor_define(DisplayChangeListener *dcl,
  293. QEMUCursor *c)
  294. {
  295. DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
  296. GVariant *v_data = NULL;
  297. cursor_get(c);
  298. v_data = g_variant_new_from_data(
  299. G_VARIANT_TYPE("ay"),
  300. c->data,
  301. c->width * c->height * 4,
  302. TRUE,
  303. (GDestroyNotify)cursor_put,
  304. c);
  305. qemu_dbus_display1_listener_call_cursor_define(
  306. ddl->proxy,
  307. c->width,
  308. c->height,
  309. c->hot_x,
  310. c->hot_y,
  311. v_data,
  312. G_DBUS_CALL_FLAGS_NONE,
  313. -1,
  314. NULL,
  315. NULL,
  316. NULL);
  317. }
  318. const DisplayChangeListenerOps dbus_gl_dcl_ops = {
  319. .dpy_name = "dbus-gl",
  320. .dpy_gfx_update = dbus_gl_gfx_update,
  321. .dpy_gfx_switch = dbus_gl_gfx_switch,
  322. .dpy_gfx_check_format = console_gl_check_format,
  323. .dpy_refresh = dbus_gl_refresh,
  324. .dpy_mouse_set = dbus_mouse_set,
  325. .dpy_cursor_define = dbus_cursor_define,
  326. .dpy_gl_scanout_disable = dbus_scanout_disable,
  327. .dpy_gl_scanout_texture = dbus_scanout_texture,
  328. .dpy_gl_scanout_dmabuf = dbus_scanout_dmabuf,
  329. .dpy_gl_cursor_dmabuf = dbus_cursor_dmabuf,
  330. .dpy_gl_cursor_position = dbus_cursor_position,
  331. .dpy_gl_release_dmabuf = dbus_release_dmabuf,
  332. .dpy_gl_update = dbus_scanout_update,
  333. };
  334. const DisplayChangeListenerOps dbus_dcl_ops = {
  335. .dpy_name = "dbus",
  336. .dpy_gfx_update = dbus_gfx_update,
  337. .dpy_gfx_switch = dbus_gfx_switch,
  338. .dpy_refresh = dbus_refresh,
  339. .dpy_mouse_set = dbus_mouse_set,
  340. .dpy_cursor_define = dbus_cursor_define,
  341. };
  342. static void
  343. dbus_display_listener_dispose(GObject *object)
  344. {
  345. DBusDisplayListener *ddl = DBUS_DISPLAY_LISTENER(object);
  346. unregister_displaychangelistener(&ddl->dcl);
  347. g_clear_object(&ddl->conn);
  348. g_clear_pointer(&ddl->bus_name, g_free);
  349. g_clear_object(&ddl->proxy);
  350. g_clear_pointer(&ddl->gls, qemu_gl_fini_shader);
  351. G_OBJECT_CLASS(dbus_display_listener_parent_class)->dispose(object);
  352. }
  353. static void
  354. dbus_display_listener_constructed(GObject *object)
  355. {
  356. DBusDisplayListener *ddl = DBUS_DISPLAY_LISTENER(object);
  357. if (display_opengl) {
  358. ddl->gls = qemu_gl_init_shader();
  359. ddl->dcl.ops = &dbus_gl_dcl_ops;
  360. } else {
  361. ddl->dcl.ops = &dbus_dcl_ops;
  362. }
  363. G_OBJECT_CLASS(dbus_display_listener_parent_class)->constructed(object);
  364. }
  365. static void
  366. dbus_display_listener_class_init(DBusDisplayListenerClass *klass)
  367. {
  368. GObjectClass *object_class = G_OBJECT_CLASS(klass);
  369. object_class->dispose = dbus_display_listener_dispose;
  370. object_class->constructed = dbus_display_listener_constructed;
  371. }
  372. static void
  373. dbus_display_listener_init(DBusDisplayListener *ddl)
  374. {
  375. }
  376. const char *
  377. dbus_display_listener_get_bus_name(DBusDisplayListener *ddl)
  378. {
  379. return ddl->bus_name;
  380. }
  381. DBusDisplayConsole *
  382. dbus_display_listener_get_console(DBusDisplayListener *ddl)
  383. {
  384. return ddl->console;
  385. }
  386. DBusDisplayListener *
  387. dbus_display_listener_new(const char *bus_name,
  388. GDBusConnection *conn,
  389. DBusDisplayConsole *console)
  390. {
  391. DBusDisplayListener *ddl;
  392. QemuConsole *con;
  393. g_autoptr(GError) err = NULL;
  394. ddl = g_object_new(DBUS_DISPLAY_TYPE_LISTENER, NULL);
  395. ddl->proxy =
  396. qemu_dbus_display1_listener_proxy_new_sync(conn,
  397. G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
  398. NULL,
  399. "/org/qemu/Display1/Listener",
  400. NULL,
  401. &err);
  402. if (!ddl->proxy) {
  403. error_report("Failed to setup proxy: %s", err->message);
  404. g_object_unref(conn);
  405. g_object_unref(ddl);
  406. return NULL;
  407. }
  408. ddl->bus_name = g_strdup(bus_name);
  409. ddl->conn = conn;
  410. ddl->console = console;
  411. con = qemu_console_lookup_by_index(dbus_display_console_get_index(console));
  412. assert(con);
  413. ddl->dcl.con = con;
  414. register_displaychangelistener(&ddl->dcl);
  415. return ddl;
  416. }