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