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
24 #include "qemu/osdep.h"
25 #include "sysemu/sysemu.h"
27 #include <gio/gunixfdlist.h>
29 #include "ui/shader.h"
30 #include "ui/egl-helpers.h"
31 #include "ui/egl-context.h"
34 struct _DBusDisplayListener
{
38 DBusDisplayConsole
*console
;
39 GDBusConnection
*conn
;
41 QemuDBusDisplay1Listener
*proxy
;
43 DisplayChangeListener dcl
;
48 G_DEFINE_TYPE(DBusDisplayListener
, dbus_display_listener
, G_TYPE_OBJECT
)
50 static void dbus_update_gl_cb(GObject
*source_object
,
54 g_autoptr(GError
) err
= NULL
;
55 DBusDisplayListener
*ddl
= user_data
;
57 if (!qemu_dbus_display1_listener_call_update_dmabuf_finish(ddl
->proxy
,
59 error_report("Failed to call update: %s", err
->message
);
62 graphic_hw_gl_block(ddl
->dcl
.con
, false);
66 static void dbus_call_update_gl(DBusDisplayListener
*ddl
,
67 int x
, int y
, int w
, int h
)
69 graphic_hw_gl_block(ddl
->dcl
.con
, true);
71 qemu_dbus_display1_listener_call_update_dmabuf(ddl
->proxy
,
73 G_DBUS_CALL_FLAGS_NONE
,
74 DBUS_DEFAULT_TIMEOUT
, NULL
,
79 static void dbus_scanout_disable(DisplayChangeListener
*dcl
)
81 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
84 qemu_dbus_display1_listener_call_disable(
85 ddl
->proxy
, G_DBUS_CALL_FLAGS_NONE
, -1, NULL
, NULL
, NULL
);
88 static void dbus_scanout_dmabuf(DisplayChangeListener
*dcl
,
91 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
92 g_autoptr(GError
) err
= NULL
;
93 g_autoptr(GUnixFDList
) fd_list
= NULL
;
95 fd_list
= g_unix_fd_list_new();
96 if (g_unix_fd_list_append(fd_list
, dmabuf
->fd
, &err
) != 0) {
97 error_report("Failed to setup dmabuf fdlist: %s", err
->message
);
101 qemu_dbus_display1_listener_call_scanout_dmabuf(
103 g_variant_new_handle(0),
110 G_DBUS_CALL_FLAGS_NONE
,
116 static void dbus_scanout_texture(DisplayChangeListener
*dcl
,
118 bool backing_y_0_top
,
119 uint32_t backing_width
,
120 uint32_t backing_height
,
121 uint32_t x
, uint32_t y
,
122 uint32_t w
, uint32_t h
)
124 QemuDmaBuf dmabuf
= {
125 .width
= backing_width
,
126 .height
= backing_height
,
127 .y0_top
= backing_y_0_top
,
131 dmabuf
.fd
= egl_get_fd_for_texture(
132 tex_id
, (EGLint
*)&dmabuf
.stride
,
133 (EGLint
*)&dmabuf
.fourcc
,
136 error_report("%s: failed to get fd for texture", __func__
);
140 dbus_scanout_dmabuf(dcl
, &dmabuf
);
144 static void dbus_cursor_dmabuf(DisplayChangeListener
*dcl
,
145 QemuDmaBuf
*dmabuf
, bool have_hot
,
146 uint32_t hot_x
, uint32_t hot_y
)
148 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
150 GVariant
*v_data
= NULL
;
154 qemu_dbus_display1_listener_call_mouse_set(
155 ddl
->proxy
, 0, 0, false,
156 G_DBUS_CALL_FLAGS_NONE
, -1, NULL
, NULL
, NULL
);
160 egl_dmabuf_import_texture(dmabuf
);
161 if (!dmabuf
->texture
) {
164 egl_fb_setup_for_tex(&cursor_fb
, dmabuf
->width
, dmabuf
->height
,
165 dmabuf
->texture
, false);
166 ds
= qemu_create_displaysurface(dmabuf
->width
, dmabuf
->height
);
167 egl_fb_read(ds
, &cursor_fb
);
169 v_data
= g_variant_new_from_data(
170 G_VARIANT_TYPE("ay"),
172 surface_width(ds
) * surface_height(ds
) * 4,
174 (GDestroyNotify
)qemu_free_displaysurface
,
176 qemu_dbus_display1_listener_call_cursor_define(
183 G_DBUS_CALL_FLAGS_NONE
,
190 static void dbus_cursor_position(DisplayChangeListener
*dcl
,
191 uint32_t pos_x
, uint32_t pos_y
)
193 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
195 qemu_dbus_display1_listener_call_mouse_set(
196 ddl
->proxy
, pos_x
, pos_y
, true,
197 G_DBUS_CALL_FLAGS_NONE
, -1, NULL
, NULL
, NULL
);
200 static void dbus_release_dmabuf(DisplayChangeListener
*dcl
,
203 dbus_scanout_disable(dcl
);
206 static void dbus_scanout_update(DisplayChangeListener
*dcl
,
207 uint32_t x
, uint32_t y
,
208 uint32_t w
, uint32_t h
)
210 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
212 dbus_call_update_gl(ddl
, x
, y
, w
, h
);
215 static void dbus_gl_refresh(DisplayChangeListener
*dcl
)
217 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
219 graphic_hw_update(dcl
->con
);
221 if (!ddl
->ds
|| qemu_console_is_gl_blocked(ddl
->dcl
.con
)) {
225 if (ddl
->gl_updates
) {
226 dbus_call_update_gl(ddl
, 0, 0,
227 surface_width(ddl
->ds
), surface_height(ddl
->ds
));
232 static void dbus_refresh(DisplayChangeListener
*dcl
)
234 graphic_hw_update(dcl
->con
);
237 static void dbus_gl_gfx_update(DisplayChangeListener
*dcl
,
238 int x
, int y
, int w
, int h
)
240 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
245 static void dbus_gfx_update(DisplayChangeListener
*dcl
,
246 int x
, int y
, int w
, int h
)
248 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
254 stride
= w
* DIV_ROUND_UP(PIXMAN_FORMAT_BPP(surface_format(ddl
->ds
)), 8);
256 trace_dbus_update(x
, y
, w
, h
);
258 if (x
== 0 && y
== 0 && w
== surface_width(ddl
->ds
) && h
== surface_height(ddl
->ds
)) {
259 v_data
= g_variant_new_from_data(
260 G_VARIANT_TYPE("ay"),
261 surface_data(ddl
->ds
),
262 surface_stride(ddl
->ds
) * surface_height(ddl
->ds
),
264 (GDestroyNotify
)pixman_image_unref
,
265 pixman_image_ref(ddl
->ds
->image
));
266 qemu_dbus_display1_listener_call_scanout(
268 surface_width(ddl
->ds
),
269 surface_height(ddl
->ds
),
270 surface_stride(ddl
->ds
),
271 surface_format(ddl
->ds
),
273 G_DBUS_CALL_FLAGS_NONE
,
274 DBUS_DEFAULT_TIMEOUT
, NULL
, NULL
, NULL
);
278 /* make a copy, since gvariant only handles linear data */
279 img
= pixman_image_create_bits(surface_format(ddl
->ds
),
281 pixman_image_composite(PIXMAN_OP_SRC
, ddl
->ds
->image
, NULL
, img
,
282 x
, y
, 0, 0, 0, 0, w
, h
);
284 v_data
= g_variant_new_from_data(
285 G_VARIANT_TYPE("ay"),
286 pixman_image_get_data(img
),
287 pixman_image_get_stride(img
) * h
,
289 (GDestroyNotify
)pixman_image_unref
,
291 qemu_dbus_display1_listener_call_update(ddl
->proxy
,
292 x
, y
, w
, h
, pixman_image_get_stride(img
), pixman_image_get_format(img
),
294 G_DBUS_CALL_FLAGS_NONE
,
295 DBUS_DEFAULT_TIMEOUT
, NULL
, NULL
, NULL
);
298 static void dbus_gl_gfx_switch(DisplayChangeListener
*dcl
,
299 struct DisplaySurface
*new_surface
)
301 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
303 ddl
->ds
= new_surface
;
305 int width
= surface_width(ddl
->ds
);
306 int height
= surface_height(ddl
->ds
);
308 /* TODO: lazy send dmabuf (there are unnecessary sent otherwise) */
309 dbus_scanout_texture(&ddl
->dcl
, ddl
->ds
->texture
, false,
310 width
, height
, 0, 0, width
, height
);
314 static void dbus_gfx_switch(DisplayChangeListener
*dcl
,
315 struct DisplaySurface
*new_surface
)
317 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
319 ddl
->ds
= new_surface
;
321 /* why not call disable instead? */
326 static void dbus_mouse_set(DisplayChangeListener
*dcl
,
327 int x
, int y
, int on
)
329 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
331 qemu_dbus_display1_listener_call_mouse_set(
332 ddl
->proxy
, x
, y
, on
, G_DBUS_CALL_FLAGS_NONE
, -1, NULL
, NULL
, NULL
);
335 static void dbus_cursor_define(DisplayChangeListener
*dcl
,
338 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
339 GVariant
*v_data
= NULL
;
342 v_data
= g_variant_new_from_data(
343 G_VARIANT_TYPE("ay"),
345 c
->width
* c
->height
* 4,
347 (GDestroyNotify
)cursor_put
,
350 qemu_dbus_display1_listener_call_cursor_define(
357 G_DBUS_CALL_FLAGS_NONE
,
364 const DisplayChangeListenerOps dbus_gl_dcl_ops
= {
365 .dpy_name
= "dbus-gl",
366 .dpy_gfx_update
= dbus_gl_gfx_update
,
367 .dpy_gfx_switch
= dbus_gl_gfx_switch
,
368 .dpy_gfx_check_format
= console_gl_check_format
,
369 .dpy_refresh
= dbus_gl_refresh
,
370 .dpy_mouse_set
= dbus_mouse_set
,
371 .dpy_cursor_define
= dbus_cursor_define
,
373 .dpy_gl_scanout_disable
= dbus_scanout_disable
,
374 .dpy_gl_scanout_texture
= dbus_scanout_texture
,
375 .dpy_gl_scanout_dmabuf
= dbus_scanout_dmabuf
,
376 .dpy_gl_cursor_dmabuf
= dbus_cursor_dmabuf
,
377 .dpy_gl_cursor_position
= dbus_cursor_position
,
378 .dpy_gl_release_dmabuf
= dbus_release_dmabuf
,
379 .dpy_gl_update
= dbus_scanout_update
,
382 const DisplayChangeListenerOps dbus_dcl_ops
= {
384 .dpy_gfx_update
= dbus_gfx_update
,
385 .dpy_gfx_switch
= dbus_gfx_switch
,
386 .dpy_refresh
= dbus_refresh
,
387 .dpy_mouse_set
= dbus_mouse_set
,
388 .dpy_cursor_define
= dbus_cursor_define
,
392 dbus_display_listener_dispose(GObject
*object
)
394 DBusDisplayListener
*ddl
= DBUS_DISPLAY_LISTENER(object
);
396 unregister_displaychangelistener(&ddl
->dcl
);
397 g_clear_object(&ddl
->conn
);
398 g_clear_pointer(&ddl
->bus_name
, g_free
);
399 g_clear_object(&ddl
->proxy
);
401 G_OBJECT_CLASS(dbus_display_listener_parent_class
)->dispose(object
);
405 dbus_display_listener_constructed(GObject
*object
)
407 DBusDisplayListener
*ddl
= DBUS_DISPLAY_LISTENER(object
);
409 if (display_opengl
) {
410 ddl
->dcl
.ops
= &dbus_gl_dcl_ops
;
412 ddl
->dcl
.ops
= &dbus_dcl_ops
;
415 G_OBJECT_CLASS(dbus_display_listener_parent_class
)->constructed(object
);
419 dbus_display_listener_class_init(DBusDisplayListenerClass
*klass
)
421 GObjectClass
*object_class
= G_OBJECT_CLASS(klass
);
423 object_class
->dispose
= dbus_display_listener_dispose
;
424 object_class
->constructed
= dbus_display_listener_constructed
;
428 dbus_display_listener_init(DBusDisplayListener
*ddl
)
433 dbus_display_listener_get_bus_name(DBusDisplayListener
*ddl
)
435 return ddl
->bus_name
?: "p2p";
439 dbus_display_listener_get_console(DBusDisplayListener
*ddl
)
444 DBusDisplayListener
*
445 dbus_display_listener_new(const char *bus_name
,
446 GDBusConnection
*conn
,
447 DBusDisplayConsole
*console
)
449 DBusDisplayListener
*ddl
;
451 g_autoptr(GError
) err
= NULL
;
453 ddl
= g_object_new(DBUS_DISPLAY_TYPE_LISTENER
, NULL
);
455 qemu_dbus_display1_listener_proxy_new_sync(conn
,
456 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START
,
458 "/org/qemu/Display1/Listener",
462 error_report("Failed to setup proxy: %s", err
->message
);
463 g_object_unref(conn
);
468 ddl
->bus_name
= g_strdup(bus_name
);
470 ddl
->console
= console
;
472 con
= qemu_console_lookup_by_index(dbus_display_console_get_index(console
));
475 register_displaychangelistener(&ddl
->dcl
);