target/arm: Move 64-bit TCG CPUs into tcg/
[qemu/ar7.git] / ui / dbus-listener.c
blob911acdc52935bff0576e3861c4540c22db1b1921
1 /*
2 * QEMU DBus display console
4 * Copyright (c) 2021 Marc-André Lureau <marcandre.lureau@redhat.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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>
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"
37 struct _DBusDisplayListener {
38 GObject parent;
40 char *bus_name;
41 DBusDisplayConsole *console;
42 GDBusConnection *conn;
44 QemuDBusDisplay1Listener *proxy;
46 DisplayChangeListener dcl;
47 DisplaySurface *ds;
48 int gl_updates;
51 G_DEFINE_TYPE(DBusDisplayListener, dbus_display_listener, G_TYPE_OBJECT)
53 #ifdef CONFIG_GBM
54 static void dbus_update_gl_cb(GObject *source_object,
55 GAsyncResult *res,
56 gpointer user_data)
58 g_autoptr(GError) err = NULL;
59 DBusDisplayListener *ddl = user_data;
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);
66 graphic_hw_gl_block(ddl->dcl.con, false);
67 g_object_unref(ddl);
70 static void dbus_call_update_gl(DBusDisplayListener *ddl,
71 int x, int y, int w, int h)
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));
83 static void dbus_scanout_disable(DisplayChangeListener *dcl)
85 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
87 ddl->ds = NULL;
88 qemu_dbus_display1_listener_call_disable(
89 ddl->proxy, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
92 static void dbus_scanout_dmabuf(DisplayChangeListener *dcl,
93 QemuDmaBuf *dmabuf)
95 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
96 g_autoptr(GError) err = NULL;
97 g_autoptr(GUnixFDList) fd_list = NULL;
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;
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,
116 fd_list,
117 NULL, NULL, NULL);
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)
128 QemuDmaBuf dmabuf = {
129 .width = backing_width,
130 .height = backing_height,
131 .y0_top = backing_y_0_top,
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;
144 dbus_scanout_dmabuf(dcl, &dmabuf);
145 close(dmabuf.fd);
148 static void dbus_cursor_dmabuf(DisplayChangeListener *dcl,
149 QemuDmaBuf *dmabuf, bool have_hot,
150 uint32_t hot_x, uint32_t hot_y)
152 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
153 DisplaySurface *ds;
154 GVariant *v_data = NULL;
155 egl_fb cursor_fb = EGL_FB_INIT;
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;
164 egl_dmabuf_import_texture(dmabuf);
165 if (!dmabuf->texture) {
166 return;
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);
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,
189 NULL,
190 NULL,
191 NULL);
194 static void dbus_cursor_position(DisplayChangeListener *dcl,
195 uint32_t pos_x, uint32_t pos_y)
197 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
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);
204 static void dbus_release_dmabuf(DisplayChangeListener *dcl,
205 QemuDmaBuf *dmabuf)
207 dbus_scanout_disable(dcl);
210 static void dbus_scanout_update(DisplayChangeListener *dcl,
211 uint32_t x, uint32_t y,
212 uint32_t w, uint32_t h)
214 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
216 dbus_call_update_gl(ddl, x, y, w, h);
219 static void dbus_gl_refresh(DisplayChangeListener *dcl)
221 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
223 graphic_hw_update(dcl->con);
225 if (!ddl->ds || qemu_console_is_gl_blocked(ddl->dcl.con)) {
226 return;
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;
235 #endif
237 static void dbus_refresh(DisplayChangeListener *dcl)
239 graphic_hw_update(dcl->con);
242 #ifdef CONFIG_GBM
243 static void dbus_gl_gfx_update(DisplayChangeListener *dcl,
244 int x, int y, int w, int h)
246 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
248 ddl->gl_updates++;
250 #endif
252 static void dbus_gfx_update(DisplayChangeListener *dcl,
253 int x, int y, int w, int h)
255 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
256 pixman_image_t *img;
257 GVariant *v_data;
258 size_t stride;
260 assert(ddl->ds);
261 stride = w * DIV_ROUND_UP(PIXMAN_FORMAT_BPP(surface_format(ddl->ds)), 8);
263 trace_dbus_update(x, y, w, h);
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;
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);
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);
305 #ifdef CONFIG_GBM
306 static void dbus_gl_gfx_switch(DisplayChangeListener *dcl,
307 struct DisplaySurface *new_surface)
309 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
311 ddl->ds = new_surface;
312 if (ddl->ds) {
313 int width = surface_width(ddl->ds);
314 int height = surface_height(ddl->ds);
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);
321 #endif
323 static void dbus_gfx_switch(DisplayChangeListener *dcl,
324 struct DisplaySurface *new_surface)
326 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
328 ddl->ds = new_surface;
329 if (!ddl->ds) {
330 /* why not call disable instead? */
331 return;
335 static void dbus_mouse_set(DisplayChangeListener *dcl,
336 int x, int y, int on)
338 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
340 qemu_dbus_display1_listener_call_mouse_set(
341 ddl->proxy, x, y, on, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
344 static void dbus_cursor_define(DisplayChangeListener *dcl,
345 QEMUCursor *c)
347 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
348 GVariant *v_data = NULL;
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));
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,
367 NULL,
368 NULL,
369 NULL);
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,
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,
390 #endif
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,
401 static void
402 dbus_display_listener_dispose(GObject *object)
404 DBusDisplayListener *ddl = DBUS_DISPLAY_LISTENER(object);
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);
411 G_OBJECT_CLASS(dbus_display_listener_parent_class)->dispose(object);
414 static void
415 dbus_display_listener_constructed(GObject *object)
417 DBusDisplayListener *ddl = DBUS_DISPLAY_LISTENER(object);
419 ddl->dcl.ops = &dbus_dcl_ops;
420 #ifdef CONFIG_GBM
421 if (display_opengl) {
422 ddl->dcl.ops = &dbus_gl_dcl_ops;
424 #endif
426 G_OBJECT_CLASS(dbus_display_listener_parent_class)->constructed(object);
429 static void
430 dbus_display_listener_class_init(DBusDisplayListenerClass *klass)
432 GObjectClass *object_class = G_OBJECT_CLASS(klass);
434 object_class->dispose = dbus_display_listener_dispose;
435 object_class->constructed = dbus_display_listener_constructed;
438 static void
439 dbus_display_listener_init(DBusDisplayListener *ddl)
443 const char *
444 dbus_display_listener_get_bus_name(DBusDisplayListener *ddl)
446 return ddl->bus_name ?: "p2p";
449 DBusDisplayConsole *
450 dbus_display_listener_get_console(DBusDisplayListener *ddl)
452 return ddl->console;
455 DBusDisplayListener *
456 dbus_display_listener_new(const char *bus_name,
457 GDBusConnection *conn,
458 DBusDisplayConsole *console)
460 DBusDisplayListener *ddl;
461 QemuConsole *con;
462 g_autoptr(GError) err = NULL;
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;
479 ddl->bus_name = g_strdup(bus_name);
480 ddl->conn = conn;
481 ddl->console = console;
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);
488 return ddl;