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 "qemu/error-report.h"
26 #include "sysemu/sysemu.h"
28 #include <gio/gunixfdlist.h>
31 #include "ui/shader.h"
32 #include "ui/egl-helpers.h"
33 #include "ui/egl-context.h"
37 struct _DBusDisplayListener
{
41 DBusDisplayConsole
*console
;
42 GDBusConnection
*conn
;
44 QemuDBusDisplay1Listener
*proxy
;
46 DisplayChangeListener dcl
;
51 G_DEFINE_TYPE(DBusDisplayListener
, dbus_display_listener
, G_TYPE_OBJECT
)
54 static void dbus_update_gl_cb(GObject
*source_object
,
58 g_autoptr(GError
) err
= NULL
;
59 DBusDisplayListener
*ddl
= user_data
;
61 if (!qemu_dbus_display1_listener_call_update_dmabuf_finish(ddl
->proxy
,
63 error_report("Failed to call update: %s", err
->message
);
66 graphic_hw_gl_block(ddl
->dcl
.con
, false);
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);
75 qemu_dbus_display1_listener_call_update_dmabuf(ddl
->proxy
,
77 G_DBUS_CALL_FLAGS_NONE
,
78 DBUS_DEFAULT_TIMEOUT
, NULL
,
83 static void dbus_scanout_disable(DisplayChangeListener
*dcl
)
85 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
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
,
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
);
105 qemu_dbus_display1_listener_call_scanout_dmabuf(
107 g_variant_new_handle(0),
114 G_DBUS_CALL_FLAGS_NONE
,
120 static void dbus_scanout_texture(DisplayChangeListener
*dcl
,
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
,
135 dmabuf
.fd
= egl_get_fd_for_texture(
136 tex_id
, (EGLint
*)&dmabuf
.stride
,
137 (EGLint
*)&dmabuf
.fourcc
,
140 error_report("%s: failed to get fd for texture", __func__
);
144 dbus_scanout_dmabuf(dcl
, &dmabuf
);
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
);
154 GVariant
*v_data
= NULL
;
155 egl_fb cursor_fb
= EGL_FB_INIT
;
158 qemu_dbus_display1_listener_call_mouse_set(
159 ddl
->proxy
, 0, 0, false,
160 G_DBUS_CALL_FLAGS_NONE
, -1, NULL
, NULL
, NULL
);
164 egl_dmabuf_import_texture(dmabuf
);
165 if (!dmabuf
->texture
) {
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"),
176 surface_width(ds
) * surface_height(ds
) * 4,
178 (GDestroyNotify
)qemu_free_displaysurface
,
180 qemu_dbus_display1_listener_call_cursor_define(
187 G_DBUS_CALL_FLAGS_NONE
,
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
,
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
)) {
229 if (ddl
->gl_updates
) {
230 dbus_call_update_gl(ddl
, 0, 0,
231 surface_width(ddl
->ds
), surface_height(ddl
->ds
));
237 static void dbus_refresh(DisplayChangeListener
*dcl
)
239 graphic_hw_update(dcl
->con
);
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
);
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
);
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
),
271 (GDestroyNotify
)pixman_image_unref
,
272 pixman_image_ref(ddl
->ds
->image
));
273 qemu_dbus_display1_listener_call_scanout(
275 surface_width(ddl
->ds
),
276 surface_height(ddl
->ds
),
277 surface_stride(ddl
->ds
),
278 surface_format(ddl
->ds
),
280 G_DBUS_CALL_FLAGS_NONE
,
281 DBUS_DEFAULT_TIMEOUT
, NULL
, NULL
, NULL
);
285 /* make a copy, since gvariant only handles linear data */
286 img
= pixman_image_create_bits(surface_format(ddl
->ds
),
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
,
296 (GDestroyNotify
)pixman_image_unref
,
298 qemu_dbus_display1_listener_call_update(ddl
->proxy
,
299 x
, y
, w
, h
, pixman_image_get_stride(img
), pixman_image_get_format(img
),
301 G_DBUS_CALL_FLAGS_NONE
,
302 DBUS_DEFAULT_TIMEOUT
, NULL
, NULL
, NULL
);
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
;
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
);
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
;
330 /* why not call disable instead? */
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
,
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"),
353 c
->width
* c
->height
* 4,
355 (GDestroyNotify
)cursor_unref
,
358 qemu_dbus_display1_listener_call_cursor_define(
365 G_DBUS_CALL_FLAGS_NONE
,
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
,
392 const DisplayChangeListenerOps dbus_dcl_ops
= {
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
,
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
);
415 dbus_display_listener_constructed(GObject
*object
)
417 DBusDisplayListener
*ddl
= DBUS_DISPLAY_LISTENER(object
);
419 ddl
->dcl
.ops
= &dbus_dcl_ops
;
421 if (display_opengl
) {
422 ddl
->dcl
.ops
= &dbus_gl_dcl_ops
;
426 G_OBJECT_CLASS(dbus_display_listener_parent_class
)->constructed(object
);
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
;
439 dbus_display_listener_init(DBusDisplayListener
*ddl
)
444 dbus_display_listener_get_bus_name(DBusDisplayListener
*ddl
)
446 return ddl
->bus_name
?: "p2p";
450 dbus_display_listener_get_console(DBusDisplayListener
*ddl
)
455 DBusDisplayListener
*
456 dbus_display_listener_new(const char *bus_name
,
457 GDBusConnection
*conn
,
458 DBusDisplayConsole
*console
)
460 DBusDisplayListener
*ddl
;
462 g_autoptr(GError
) err
= NULL
;
464 ddl
= g_object_new(DBUS_DISPLAY_TYPE_LISTENER
, NULL
);
466 qemu_dbus_display1_listener_proxy_new_sync(conn
,
467 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START
,
469 "/org/qemu/Display1/Listener",
473 error_report("Failed to setup proxy: %s", err
->message
);
474 g_object_unref(conn
);
479 ddl
->bus_name
= g_strdup(bus_name
);
481 ddl
->console
= console
;
483 con
= qemu_console_lookup_by_index(dbus_display_console_get_index(console
));
486 register_displaychangelistener(&ddl
->dcl
);