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>
30 #include "ui/shader.h"
31 #include "ui/egl-helpers.h"
32 #include "ui/egl-context.h"
35 struct _DBusDisplayListener
{
39 DBusDisplayConsole
*console
;
40 GDBusConnection
*conn
;
42 QemuDBusDisplay1Listener
*proxy
;
44 DisplayChangeListener dcl
;
49 G_DEFINE_TYPE(DBusDisplayListener
, dbus_display_listener
, G_TYPE_OBJECT
)
51 static void dbus_update_gl_cb(GObject
*source_object
,
55 g_autoptr(GError
) err
= NULL
;
56 DBusDisplayListener
*ddl
= user_data
;
58 if (!qemu_dbus_display1_listener_call_update_dmabuf_finish(ddl
->proxy
,
60 error_report("Failed to call update: %s", err
->message
);
63 graphic_hw_gl_block(ddl
->dcl
.con
, false);
67 static void dbus_call_update_gl(DBusDisplayListener
*ddl
,
68 int x
, int y
, int w
, int h
)
70 graphic_hw_gl_block(ddl
->dcl
.con
, true);
72 qemu_dbus_display1_listener_call_update_dmabuf(ddl
->proxy
,
74 G_DBUS_CALL_FLAGS_NONE
,
75 DBUS_DEFAULT_TIMEOUT
, NULL
,
80 static void dbus_scanout_disable(DisplayChangeListener
*dcl
)
82 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
85 qemu_dbus_display1_listener_call_disable(
86 ddl
->proxy
, G_DBUS_CALL_FLAGS_NONE
, -1, NULL
, NULL
, NULL
);
89 static void dbus_scanout_dmabuf(DisplayChangeListener
*dcl
,
92 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
93 g_autoptr(GError
) err
= NULL
;
94 g_autoptr(GUnixFDList
) fd_list
= NULL
;
96 fd_list
= g_unix_fd_list_new();
97 if (g_unix_fd_list_append(fd_list
, dmabuf
->fd
, &err
) != 0) {
98 error_report("Failed to setup dmabuf fdlist: %s", err
->message
);
102 qemu_dbus_display1_listener_call_scanout_dmabuf(
104 g_variant_new_handle(0),
111 G_DBUS_CALL_FLAGS_NONE
,
117 static void dbus_scanout_texture(DisplayChangeListener
*dcl
,
119 bool backing_y_0_top
,
120 uint32_t backing_width
,
121 uint32_t backing_height
,
122 uint32_t x
, uint32_t y
,
123 uint32_t w
, uint32_t h
)
125 QemuDmaBuf dmabuf
= {
126 .width
= backing_width
,
127 .height
= backing_height
,
128 .y0_top
= backing_y_0_top
,
132 dmabuf
.fd
= egl_get_fd_for_texture(
133 tex_id
, (EGLint
*)&dmabuf
.stride
,
134 (EGLint
*)&dmabuf
.fourcc
,
137 error_report("%s: failed to get fd for texture", __func__
);
141 dbus_scanout_dmabuf(dcl
, &dmabuf
);
145 static void dbus_cursor_dmabuf(DisplayChangeListener
*dcl
,
146 QemuDmaBuf
*dmabuf
, bool have_hot
,
147 uint32_t hot_x
, uint32_t hot_y
)
149 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
151 GVariant
*v_data
= NULL
;
155 qemu_dbus_display1_listener_call_mouse_set(
156 ddl
->proxy
, 0, 0, false,
157 G_DBUS_CALL_FLAGS_NONE
, -1, NULL
, NULL
, NULL
);
161 egl_dmabuf_import_texture(dmabuf
);
162 if (!dmabuf
->texture
) {
165 egl_fb_setup_for_tex(&cursor_fb
, dmabuf
->width
, dmabuf
->height
,
166 dmabuf
->texture
, false);
167 ds
= qemu_create_displaysurface(dmabuf
->width
, dmabuf
->height
);
168 egl_fb_read(ds
, &cursor_fb
);
170 v_data
= g_variant_new_from_data(
171 G_VARIANT_TYPE("ay"),
173 surface_width(ds
) * surface_height(ds
) * 4,
175 (GDestroyNotify
)qemu_free_displaysurface
,
177 qemu_dbus_display1_listener_call_cursor_define(
184 G_DBUS_CALL_FLAGS_NONE
,
191 static void dbus_cursor_position(DisplayChangeListener
*dcl
,
192 uint32_t pos_x
, uint32_t pos_y
)
194 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
196 qemu_dbus_display1_listener_call_mouse_set(
197 ddl
->proxy
, pos_x
, pos_y
, true,
198 G_DBUS_CALL_FLAGS_NONE
, -1, NULL
, NULL
, NULL
);
201 static void dbus_release_dmabuf(DisplayChangeListener
*dcl
,
204 dbus_scanout_disable(dcl
);
207 static void dbus_scanout_update(DisplayChangeListener
*dcl
,
208 uint32_t x
, uint32_t y
,
209 uint32_t w
, uint32_t h
)
211 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
213 dbus_call_update_gl(ddl
, x
, y
, w
, h
);
216 static void dbus_gl_refresh(DisplayChangeListener
*dcl
)
218 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
220 graphic_hw_update(dcl
->con
);
222 if (!ddl
->ds
|| qemu_console_is_gl_blocked(ddl
->dcl
.con
)) {
226 if (ddl
->gl_updates
) {
227 dbus_call_update_gl(ddl
, 0, 0,
228 surface_width(ddl
->ds
), surface_height(ddl
->ds
));
233 static void dbus_refresh(DisplayChangeListener
*dcl
)
235 graphic_hw_update(dcl
->con
);
238 static void dbus_gl_gfx_update(DisplayChangeListener
*dcl
,
239 int x
, int y
, int w
, int h
)
241 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
246 static void dbus_gfx_update(DisplayChangeListener
*dcl
,
247 int x
, int y
, int w
, int h
)
249 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
255 stride
= w
* DIV_ROUND_UP(PIXMAN_FORMAT_BPP(surface_format(ddl
->ds
)), 8);
257 trace_dbus_update(x
, y
, w
, h
);
259 if (x
== 0 && y
== 0 && w
== surface_width(ddl
->ds
) && h
== surface_height(ddl
->ds
)) {
260 v_data
= g_variant_new_from_data(
261 G_VARIANT_TYPE("ay"),
262 surface_data(ddl
->ds
),
263 surface_stride(ddl
->ds
) * surface_height(ddl
->ds
),
265 (GDestroyNotify
)pixman_image_unref
,
266 pixman_image_ref(ddl
->ds
->image
));
267 qemu_dbus_display1_listener_call_scanout(
269 surface_width(ddl
->ds
),
270 surface_height(ddl
->ds
),
271 surface_stride(ddl
->ds
),
272 surface_format(ddl
->ds
),
274 G_DBUS_CALL_FLAGS_NONE
,
275 DBUS_DEFAULT_TIMEOUT
, NULL
, NULL
, NULL
);
279 /* make a copy, since gvariant only handles linear data */
280 img
= pixman_image_create_bits(surface_format(ddl
->ds
),
282 pixman_image_composite(PIXMAN_OP_SRC
, ddl
->ds
->image
, NULL
, img
,
283 x
, y
, 0, 0, 0, 0, w
, h
);
285 v_data
= g_variant_new_from_data(
286 G_VARIANT_TYPE("ay"),
287 pixman_image_get_data(img
),
288 pixman_image_get_stride(img
) * h
,
290 (GDestroyNotify
)pixman_image_unref
,
292 qemu_dbus_display1_listener_call_update(ddl
->proxy
,
293 x
, y
, w
, h
, pixman_image_get_stride(img
), pixman_image_get_format(img
),
295 G_DBUS_CALL_FLAGS_NONE
,
296 DBUS_DEFAULT_TIMEOUT
, NULL
, NULL
, NULL
);
299 static void dbus_gl_gfx_switch(DisplayChangeListener
*dcl
,
300 struct DisplaySurface
*new_surface
)
302 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
304 ddl
->ds
= new_surface
;
306 int width
= surface_width(ddl
->ds
);
307 int height
= surface_height(ddl
->ds
);
309 /* TODO: lazy send dmabuf (there are unnecessary sent otherwise) */
310 dbus_scanout_texture(&ddl
->dcl
, ddl
->ds
->texture
, false,
311 width
, height
, 0, 0, width
, height
);
315 static void dbus_gfx_switch(DisplayChangeListener
*dcl
,
316 struct DisplaySurface
*new_surface
)
318 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
320 ddl
->ds
= new_surface
;
322 /* why not call disable instead? */
327 static void dbus_mouse_set(DisplayChangeListener
*dcl
,
328 int x
, int y
, int on
)
330 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
332 qemu_dbus_display1_listener_call_mouse_set(
333 ddl
->proxy
, x
, y
, on
, G_DBUS_CALL_FLAGS_NONE
, -1, NULL
, NULL
, NULL
);
336 static void dbus_cursor_define(DisplayChangeListener
*dcl
,
339 DBusDisplayListener
*ddl
= container_of(dcl
, DBusDisplayListener
, dcl
);
340 GVariant
*v_data
= NULL
;
343 v_data
= g_variant_new_from_data(
344 G_VARIANT_TYPE("ay"),
346 c
->width
* c
->height
* 4,
348 (GDestroyNotify
)cursor_put
,
351 qemu_dbus_display1_listener_call_cursor_define(
358 G_DBUS_CALL_FLAGS_NONE
,
365 const DisplayChangeListenerOps dbus_gl_dcl_ops
= {
366 .dpy_name
= "dbus-gl",
367 .dpy_gfx_update
= dbus_gl_gfx_update
,
368 .dpy_gfx_switch
= dbus_gl_gfx_switch
,
369 .dpy_gfx_check_format
= console_gl_check_format
,
370 .dpy_refresh
= dbus_gl_refresh
,
371 .dpy_mouse_set
= dbus_mouse_set
,
372 .dpy_cursor_define
= dbus_cursor_define
,
374 .dpy_gl_scanout_disable
= dbus_scanout_disable
,
375 .dpy_gl_scanout_texture
= dbus_scanout_texture
,
376 .dpy_gl_scanout_dmabuf
= dbus_scanout_dmabuf
,
377 .dpy_gl_cursor_dmabuf
= dbus_cursor_dmabuf
,
378 .dpy_gl_cursor_position
= dbus_cursor_position
,
379 .dpy_gl_release_dmabuf
= dbus_release_dmabuf
,
380 .dpy_gl_update
= dbus_scanout_update
,
383 const DisplayChangeListenerOps dbus_dcl_ops
= {
385 .dpy_gfx_update
= dbus_gfx_update
,
386 .dpy_gfx_switch
= dbus_gfx_switch
,
387 .dpy_refresh
= dbus_refresh
,
388 .dpy_mouse_set
= dbus_mouse_set
,
389 .dpy_cursor_define
= dbus_cursor_define
,
393 dbus_display_listener_dispose(GObject
*object
)
395 DBusDisplayListener
*ddl
= DBUS_DISPLAY_LISTENER(object
);
397 unregister_displaychangelistener(&ddl
->dcl
);
398 g_clear_object(&ddl
->conn
);
399 g_clear_pointer(&ddl
->bus_name
, g_free
);
400 g_clear_object(&ddl
->proxy
);
402 G_OBJECT_CLASS(dbus_display_listener_parent_class
)->dispose(object
);
406 dbus_display_listener_constructed(GObject
*object
)
408 DBusDisplayListener
*ddl
= DBUS_DISPLAY_LISTENER(object
);
410 if (display_opengl
) {
411 ddl
->dcl
.ops
= &dbus_gl_dcl_ops
;
413 ddl
->dcl
.ops
= &dbus_dcl_ops
;
416 G_OBJECT_CLASS(dbus_display_listener_parent_class
)->constructed(object
);
420 dbus_display_listener_class_init(DBusDisplayListenerClass
*klass
)
422 GObjectClass
*object_class
= G_OBJECT_CLASS(klass
);
424 object_class
->dispose
= dbus_display_listener_dispose
;
425 object_class
->constructed
= dbus_display_listener_constructed
;
429 dbus_display_listener_init(DBusDisplayListener
*ddl
)
434 dbus_display_listener_get_bus_name(DBusDisplayListener
*ddl
)
436 return ddl
->bus_name
?: "p2p";
440 dbus_display_listener_get_console(DBusDisplayListener
*ddl
)
445 DBusDisplayListener
*
446 dbus_display_listener_new(const char *bus_name
,
447 GDBusConnection
*conn
,
448 DBusDisplayConsole
*console
)
450 DBusDisplayListener
*ddl
;
452 g_autoptr(GError
) err
= NULL
;
454 ddl
= g_object_new(DBUS_DISPLAY_TYPE_LISTENER
, NULL
);
456 qemu_dbus_display1_listener_proxy_new_sync(conn
,
457 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START
,
459 "/org/qemu/Display1/Listener",
463 error_report("Failed to setup proxy: %s", err
->message
);
464 g_object_unref(conn
);
469 ddl
->bus_name
= g_strdup(bus_name
);
471 ddl
->console
= console
;
473 con
= qemu_console_lookup_by_index(dbus_display_console_get_index(console
));
476 register_displaychangelistener(&ddl
->dcl
);