Merge remote-tracking branch 'qemu-project/master'
[qemu/ar7.git] / ui / dbus-listener.c
bloba5629e978c4fb92da24a60a22285484e94a15a05
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 "qapi/error.h"
27 #include "sysemu/sysemu.h"
28 #include "dbus.h"
29 #ifdef G_OS_UNIX
30 #include <gio/gunixfdlist.h>
31 #endif
32 #ifdef WIN32
33 #include <d3d11.h>
34 #include <dxgi1_2.h>
35 #endif
37 #ifdef CONFIG_OPENGL
38 #include "ui/shader.h"
39 #include "ui/egl-helpers.h"
40 #include "ui/egl-context.h"
41 #include "ui/qemu-pixman.h"
42 #endif
43 #include "trace.h"
45 static void dbus_gfx_switch(DisplayChangeListener *dcl,
46 struct DisplaySurface *new_surface);
48 enum share_kind {
49 SHARE_KIND_NONE,
50 SHARE_KIND_MAPPED,
51 SHARE_KIND_D3DTEX,
54 struct _DBusDisplayListener {
55 GObject parent;
57 char *bus_name;
58 DBusDisplayConsole *console;
59 GDBusConnection *conn;
61 QemuDBusDisplay1Listener *proxy;
63 #ifdef CONFIG_PIXMAN
64 /* Keep track of the damage region */
65 pixman_region32_t gl_damage;
66 #else
67 int gl_damage;
68 #endif
70 DisplayChangeListener dcl;
71 DisplaySurface *ds;
72 enum share_kind ds_share;
74 bool ds_mapped;
75 bool can_share_map;
77 #ifdef WIN32
78 QemuDBusDisplay1ListenerWin32Map *map_proxy;
79 QemuDBusDisplay1ListenerWin32D3d11 *d3d11_proxy;
80 HANDLE peer_process;
81 ID3D11Texture2D *d3d_texture;
82 #ifdef CONFIG_OPENGL
83 egl_fb fb;
84 #endif
85 #endif
87 guint dbus_filter;
88 guint32 out_serial_to_discard;
91 G_DEFINE_TYPE(DBusDisplayListener, dbus_display_listener, G_TYPE_OBJECT)
93 static void dbus_gfx_update(DisplayChangeListener *dcl,
94 int x, int y, int w, int h);
96 static void ddl_discard_pending_messages(DBusDisplayListener *ddl)
98 ddl->out_serial_to_discard = g_dbus_connection_get_last_serial(
99 g_dbus_proxy_get_connection(G_DBUS_PROXY(ddl->proxy)));
102 #ifdef CONFIG_OPENGL
103 static void dbus_scanout_disable(DisplayChangeListener *dcl)
105 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
107 qemu_dbus_display1_listener_call_disable(
108 ddl->proxy, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
111 #ifdef WIN32
112 static bool d3d_texture2d_share(ID3D11Texture2D *d3d_texture,
113 HANDLE *handle, Error **errp)
115 IDXGIResource1 *dxgiResource = NULL;
116 HRESULT hr;
118 hr = d3d_texture->lpVtbl->QueryInterface(d3d_texture,
119 &IID_IDXGIResource1,
120 (void **)&dxgiResource);
121 if (FAILED(hr)) {
122 goto fail;
125 hr = dxgiResource->lpVtbl->CreateSharedHandle(
126 dxgiResource,
127 NULL,
128 DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE,
129 NULL,
130 handle
133 dxgiResource->lpVtbl->Release(dxgiResource);
135 if (SUCCEEDED(hr)) {
136 return true;
139 fail:
140 error_setg_win32(errp, GetLastError(), "failed to create shared handle");
141 return false;
144 static bool d3d_texture2d_acquire0(ID3D11Texture2D *d3d_texture, Error **errp)
146 IDXGIKeyedMutex *dxgiMutex = NULL;
147 HRESULT hr;
149 hr = d3d_texture->lpVtbl->QueryInterface(d3d_texture,
150 &IID_IDXGIKeyedMutex,
151 (void **)&dxgiMutex);
152 if (FAILED(hr)) {
153 goto fail;
156 hr = dxgiMutex->lpVtbl->AcquireSync(dxgiMutex, 0, INFINITE);
158 dxgiMutex->lpVtbl->Release(dxgiMutex);
160 if (SUCCEEDED(hr)) {
161 return true;
164 fail:
165 error_setg_win32(errp, GetLastError(), "failed to acquire texture mutex");
166 return false;
169 static bool d3d_texture2d_release0(ID3D11Texture2D *d3d_texture, Error **errp)
171 IDXGIKeyedMutex *dxgiMutex = NULL;
172 HRESULT hr;
174 hr = d3d_texture->lpVtbl->QueryInterface(d3d_texture,
175 &IID_IDXGIKeyedMutex,
176 (void **)&dxgiMutex);
177 if (FAILED(hr)) {
178 goto fail;
181 hr = dxgiMutex->lpVtbl->ReleaseSync(dxgiMutex, 0);
183 dxgiMutex->lpVtbl->Release(dxgiMutex);
185 if (SUCCEEDED(hr)) {
186 return true;
189 fail:
190 error_setg_win32(errp, GetLastError(), "failed to release texture mutex");
191 return false;
193 #endif /* WIN32 */
195 #if defined(CONFIG_GBM) || defined(WIN32)
196 static void dbus_update_gl_cb(GObject *source_object,
197 GAsyncResult *res,
198 gpointer user_data)
200 g_autoptr(GError) err = NULL;
201 DBusDisplayListener *ddl = user_data;
202 bool success;
204 #ifdef CONFIG_GBM
205 success = qemu_dbus_display1_listener_call_update_dmabuf_finish(
206 ddl->proxy, res, &err);
207 #endif
209 #ifdef WIN32
210 success = qemu_dbus_display1_listener_win32_d3d11_call_update_texture2d_finish(
211 ddl->d3d11_proxy, res, &err);
212 d3d_texture2d_acquire0(ddl->d3d_texture, &error_warn);
213 #endif
215 if (!success) {
216 error_report("Failed to call update: %s", err->message);
219 graphic_hw_gl_block(ddl->dcl.con, false);
220 g_object_unref(ddl);
222 #endif
224 static void dbus_call_update_gl(DisplayChangeListener *dcl,
225 int x, int y, int w, int h)
227 #if defined(CONFIG_GBM) || defined(WIN32)
228 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
229 #endif
231 trace_dbus_update_gl(x, y, w, h);
233 glFlush();
234 #ifdef CONFIG_GBM
235 graphic_hw_gl_block(ddl->dcl.con, true);
236 qemu_dbus_display1_listener_call_update_dmabuf(ddl->proxy,
237 x, y, w, h,
238 G_DBUS_CALL_FLAGS_NONE,
239 DBUS_DEFAULT_TIMEOUT, NULL,
240 dbus_update_gl_cb,
241 g_object_ref(ddl));
242 #endif
244 #ifdef WIN32
245 switch (ddl->ds_share) {
246 case SHARE_KIND_MAPPED:
247 egl_fb_read_rect(ddl->ds, &ddl->fb, x, y, w, h);
248 dbus_gfx_update(dcl, x, y, w, h);
249 break;
250 case SHARE_KIND_D3DTEX: {
251 Error *err = NULL;
252 assert(ddl->d3d_texture);
254 graphic_hw_gl_block(ddl->dcl.con, true);
255 if (!d3d_texture2d_release0(ddl->d3d_texture, &err)) {
256 error_report_err(err);
257 return;
259 qemu_dbus_display1_listener_win32_d3d11_call_update_texture2d(
260 ddl->d3d11_proxy,
261 x, y, w, h,
262 G_DBUS_CALL_FLAGS_NONE,
263 DBUS_DEFAULT_TIMEOUT, NULL,
264 dbus_update_gl_cb,
265 g_object_ref(ddl));
266 break;
268 default:
269 g_warn_if_reached();
271 #endif
274 #ifdef CONFIG_GBM
275 static void dbus_scanout_dmabuf(DisplayChangeListener *dcl,
276 QemuDmaBuf *dmabuf)
278 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
279 g_autoptr(GError) err = NULL;
280 g_autoptr(GUnixFDList) fd_list = NULL;
281 int fd;
282 uint32_t width, height, stride, fourcc;
283 uint64_t modifier;
284 bool y0_top;
286 fd = qemu_dmabuf_get_fd(dmabuf);
287 fd_list = g_unix_fd_list_new();
288 if (g_unix_fd_list_append(fd_list, fd, &err) != 0) {
289 error_report("Failed to setup dmabuf fdlist: %s", err->message);
290 return;
293 ddl_discard_pending_messages(ddl);
295 width = qemu_dmabuf_get_width(dmabuf);
296 height = qemu_dmabuf_get_height(dmabuf);
297 stride = qemu_dmabuf_get_stride(dmabuf);
298 fourcc = qemu_dmabuf_get_fourcc(dmabuf);
299 modifier = qemu_dmabuf_get_modifier(dmabuf);
300 y0_top = qemu_dmabuf_get_y0_top(dmabuf);
302 /* FIXME: add missing x/y/w/h support */
303 qemu_dbus_display1_listener_call_scanout_dmabuf(
304 ddl->proxy, g_variant_new_handle(0),
305 width, height, stride, fourcc, modifier,
306 y0_top, G_DBUS_CALL_FLAGS_NONE,
307 -1, fd_list, NULL, NULL, NULL);
309 #endif /* GBM */
310 #endif /* OPENGL */
312 #ifdef WIN32
313 static bool dbus_scanout_map(DBusDisplayListener *ddl)
315 g_autoptr(GError) err = NULL;
316 BOOL success;
317 HANDLE target_handle;
319 if (ddl->ds_share == SHARE_KIND_MAPPED) {
320 return true;
323 if (!ddl->can_share_map || !ddl->ds->handle) {
324 return false;
327 success = DuplicateHandle(
328 GetCurrentProcess(),
329 ddl->ds->handle,
330 ddl->peer_process,
331 &target_handle,
332 FILE_MAP_READ | SECTION_QUERY,
333 FALSE, 0);
334 if (!success) {
335 g_autofree char *msg = g_win32_error_message(GetLastError());
336 g_debug("Failed to DuplicateHandle: %s", msg);
337 ddl->can_share_map = false;
338 return false;
341 ddl_discard_pending_messages(ddl);
343 if (!qemu_dbus_display1_listener_win32_map_call_scanout_map_sync(
344 ddl->map_proxy,
345 GPOINTER_TO_UINT(target_handle),
346 ddl->ds->handle_offset,
347 surface_width(ddl->ds),
348 surface_height(ddl->ds),
349 surface_stride(ddl->ds),
350 surface_format(ddl->ds),
351 G_DBUS_CALL_FLAGS_NONE,
352 DBUS_DEFAULT_TIMEOUT,
353 NULL,
354 &err)) {
355 g_debug("Failed to call ScanoutMap: %s", err->message);
356 ddl->can_share_map = false;
357 return false;
360 ddl->ds_share = SHARE_KIND_MAPPED;
362 return true;
365 #ifdef CONFIG_OPENGL
366 static bool
367 dbus_scanout_share_d3d_texture(
368 DBusDisplayListener *ddl,
369 ID3D11Texture2D *tex,
370 bool backing_y_0_top,
371 uint32_t backing_width,
372 uint32_t backing_height,
373 uint32_t x, uint32_t y,
374 uint32_t w, uint32_t h)
376 Error *err = NULL;
377 BOOL success;
378 HANDLE share_handle, target_handle;
380 if (!d3d_texture2d_release0(tex, &err)) {
381 error_report_err(err);
382 return false;
385 if (!d3d_texture2d_share(tex, &share_handle, &err)) {
386 error_report_err(err);
387 return false;
390 success = DuplicateHandle(
391 GetCurrentProcess(),
392 share_handle,
393 ddl->peer_process,
394 &target_handle,
396 FALSE, DUPLICATE_SAME_ACCESS);
397 if (!success) {
398 g_autofree char *msg = g_win32_error_message(GetLastError());
399 g_debug("Failed to DuplicateHandle: %s", msg);
400 CloseHandle(share_handle);
401 return false;
404 ddl_discard_pending_messages(ddl);
406 qemu_dbus_display1_listener_win32_d3d11_call_scanout_texture2d(
407 ddl->d3d11_proxy,
408 GPOINTER_TO_INT(target_handle),
409 backing_width,
410 backing_height,
411 backing_y_0_top,
412 x, y, w, h,
413 G_DBUS_CALL_FLAGS_NONE,
415 NULL, NULL, NULL);
417 CloseHandle(share_handle);
419 if (!d3d_texture2d_acquire0(tex, &err)) {
420 error_report_err(err);
421 return false;
424 ddl->d3d_texture = tex;
425 ddl->ds_share = SHARE_KIND_D3DTEX;
427 return true;
429 #endif /* CONFIG_OPENGL */
430 #endif /* WIN32 */
432 #ifdef CONFIG_OPENGL
433 static void dbus_scanout_texture(DisplayChangeListener *dcl,
434 uint32_t tex_id,
435 bool backing_y_0_top,
436 uint32_t backing_width,
437 uint32_t backing_height,
438 uint32_t x, uint32_t y,
439 uint32_t w, uint32_t h,
440 void *d3d_tex2d)
442 trace_dbus_scanout_texture(tex_id, backing_y_0_top,
443 backing_width, backing_height, x, y, w, h);
444 #ifdef CONFIG_GBM
445 g_autoptr(QemuDmaBuf) dmabuf = NULL;
446 int fd;
447 uint32_t stride, fourcc;
448 uint64_t modifier;
450 assert(tex_id);
451 fd = egl_get_fd_for_texture(tex_id, (EGLint *)&stride, (EGLint *)&fourcc,
452 &modifier);
453 if (fd < 0) {
454 error_report("%s: failed to get fd for texture", __func__);
455 return;
457 dmabuf = qemu_dmabuf_new(w, h, stride, x, y, backing_width,
458 backing_height, fourcc, modifier, fd,
459 false, backing_y_0_top);
461 dbus_scanout_dmabuf(dcl, dmabuf);
462 qemu_dmabuf_close(dmabuf);
463 #endif
465 #ifdef WIN32
466 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
468 /* there must be a matching gfx_switch before */
469 assert(surface_width(ddl->ds) == w);
470 assert(surface_height(ddl->ds) == h);
472 if (d3d_tex2d) {
473 dbus_scanout_share_d3d_texture(ddl, d3d_tex2d, backing_y_0_top,
474 backing_width, backing_height, x, y, w, h);
475 } else {
476 dbus_scanout_map(ddl);
477 egl_fb_setup_for_tex(&ddl->fb, backing_width, backing_height, tex_id, false);
479 #endif
482 #ifdef CONFIG_GBM
483 static void dbus_cursor_dmabuf(DisplayChangeListener *dcl,
484 QemuDmaBuf *dmabuf, bool have_hot,
485 uint32_t hot_x, uint32_t hot_y)
487 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
488 DisplaySurface *ds;
489 GVariant *v_data = NULL;
490 egl_fb cursor_fb = EGL_FB_INIT;
491 uint32_t width, height, texture;
493 if (!dmabuf) {
494 qemu_dbus_display1_listener_call_mouse_set(
495 ddl->proxy, 0, 0, false,
496 G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
497 return;
500 egl_dmabuf_import_texture(dmabuf);
501 texture = qemu_dmabuf_get_texture(dmabuf);
502 if (!texture) {
503 return;
506 width = qemu_dmabuf_get_width(dmabuf);
507 height = qemu_dmabuf_get_height(dmabuf);
509 egl_fb_setup_for_tex(&cursor_fb, width, height, texture, false);
510 ds = qemu_create_displaysurface(width, height);
511 egl_fb_read(ds, &cursor_fb);
513 v_data = g_variant_new_from_data(
514 G_VARIANT_TYPE("ay"),
515 surface_data(ds),
516 surface_width(ds) * surface_height(ds) * 4,
517 TRUE,
518 (GDestroyNotify)qemu_free_displaysurface,
519 ds);
520 qemu_dbus_display1_listener_call_cursor_define(
521 ddl->proxy,
522 surface_width(ds),
523 surface_height(ds),
524 hot_x,
525 hot_y,
526 v_data,
527 G_DBUS_CALL_FLAGS_NONE,
529 NULL,
530 NULL,
531 NULL);
534 static void dbus_release_dmabuf(DisplayChangeListener *dcl,
535 QemuDmaBuf *dmabuf)
537 dbus_scanout_disable(dcl);
539 #endif /* GBM */
541 static void dbus_gl_cursor_position(DisplayChangeListener *dcl,
542 uint32_t pos_x, uint32_t pos_y)
544 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
546 qemu_dbus_display1_listener_call_mouse_set(
547 ddl->proxy, pos_x, pos_y, true,
548 G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
551 static void dbus_scanout_update(DisplayChangeListener *dcl,
552 uint32_t x, uint32_t y,
553 uint32_t w, uint32_t h)
555 dbus_call_update_gl(dcl, x, y, w, h);
558 static void dbus_gl_refresh(DisplayChangeListener *dcl)
560 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
562 graphic_hw_update(dcl->con);
564 if (!ddl->ds || qemu_console_is_gl_blocked(ddl->dcl.con)) {
565 return;
568 #ifdef CONFIG_PIXMAN
569 int n_rects = pixman_region32_n_rects(&ddl->gl_damage);
571 for (int i = 0; i < n_rects; i++) {
572 pixman_box32_t *box;
573 box = pixman_region32_rectangles(&ddl->gl_damage, NULL) + i;
574 /* TODO: Add a UpdateList call to send multiple updates at once */
575 dbus_call_update_gl(dcl, box->x1, box->y1,
576 box->x2 - box->x1, box->y2 - box->y1);
578 pixman_region32_clear(&ddl->gl_damage);
579 #else
580 if (ddl->gl_damage) {
581 dbus_call_update_gl(dcl, 0, 0,
582 surface_width(ddl->ds), surface_height(ddl->ds));
583 ddl->gl_damage = 0;
585 #endif
587 #endif /* OPENGL */
589 static void dbus_refresh(DisplayChangeListener *dcl)
591 graphic_hw_update(dcl->con);
594 #ifdef CONFIG_OPENGL
595 static void dbus_gl_gfx_update(DisplayChangeListener *dcl,
596 int x, int y, int w, int h)
598 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
600 #ifdef CONFIG_PIXMAN
601 pixman_region32_t rect_region;
602 pixman_region32_init_rect(&rect_region, x, y, w, h);
603 pixman_region32_union(&ddl->gl_damage, &ddl->gl_damage, &rect_region);
604 pixman_region32_fini(&rect_region);
605 #else
606 ddl->gl_damage++;
607 #endif
609 #endif
611 static void dbus_gfx_update_sub(DBusDisplayListener *ddl,
612 int x, int y, int w, int h)
614 pixman_image_t *img;
615 size_t stride;
616 GVariant *v_data;
618 /* make a copy, since gvariant only handles linear data */
619 stride = w * DIV_ROUND_UP(PIXMAN_FORMAT_BPP(surface_format(ddl->ds)), 8);
620 img = pixman_image_create_bits(surface_format(ddl->ds),
621 w, h, NULL, stride);
622 #ifdef CONFIG_PIXMAN
623 pixman_image_composite(PIXMAN_OP_SRC, ddl->ds->image, NULL, img,
624 x, y, 0, 0, 0, 0, w, h);
625 #else
627 uint8_t *src = (uint8_t *)pixman_image_get_data(ddl->ds->image);
628 uint8_t *dst = (uint8_t *)pixman_image_get_data(img);
629 int bp = PIXMAN_FORMAT_BPP(surface_format(ddl->ds)) / 8;
630 int hh;
632 for (hh = 0; hh < h; hh++) {
633 memcpy(&dst[stride * hh],
634 &src[surface_stride(ddl->ds) * (hh + y) + x * bp],
635 stride);
638 #endif
639 #pragma GCC diagnostic ignored "-Wcast-function-type"
640 v_data = g_variant_new_from_data(
641 G_VARIANT_TYPE("ay"),
642 pixman_image_get_data(img),
643 pixman_image_get_stride(img) * h,
644 TRUE,
645 (GDestroyNotify)pixman_image_unref,
646 img);
647 qemu_dbus_display1_listener_call_update(ddl->proxy,
648 x, y, w, h, pixman_image_get_stride(img), pixman_image_get_format(img),
649 v_data,
650 G_DBUS_CALL_FLAGS_NONE,
651 DBUS_DEFAULT_TIMEOUT, NULL, NULL, NULL);
654 static void ddl_scanout(DBusDisplayListener *ddl)
656 GVariant *v_data;
658 v_data = g_variant_new_from_data(
659 G_VARIANT_TYPE("ay"), surface_data(ddl->ds),
660 surface_stride(ddl->ds) * surface_height(ddl->ds), TRUE,
661 (GDestroyNotify)pixman_image_unref, pixman_image_ref(ddl->ds->image));
663 ddl_discard_pending_messages(ddl);
665 qemu_dbus_display1_listener_call_scanout(
666 ddl->proxy, surface_width(ddl->ds), surface_height(ddl->ds),
667 surface_stride(ddl->ds), surface_format(ddl->ds), v_data,
668 G_DBUS_CALL_FLAGS_NONE, DBUS_DEFAULT_TIMEOUT, NULL, NULL,
669 g_object_ref(ddl));
672 static void dbus_gfx_update(DisplayChangeListener *dcl,
673 int x, int y, int w, int h)
675 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
677 assert(ddl->ds);
679 trace_dbus_update(x, y, w, h);
681 #ifdef WIN32
682 if (dbus_scanout_map(ddl)) {
683 qemu_dbus_display1_listener_win32_map_call_update_map(
684 ddl->map_proxy,
685 x, y, w, h,
686 G_DBUS_CALL_FLAGS_NONE,
687 DBUS_DEFAULT_TIMEOUT, NULL, NULL, NULL);
688 return;
690 #endif
692 if (x == 0 && y == 0 && w == surface_width(ddl->ds) && h == surface_height(ddl->ds)) {
693 return ddl_scanout(ddl);
696 dbus_gfx_update_sub(ddl, x, y, w, h);
699 #ifdef CONFIG_OPENGL
700 static void dbus_gl_gfx_switch(DisplayChangeListener *dcl,
701 struct DisplaySurface *new_surface)
703 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
705 trace_dbus_gl_gfx_switch(new_surface);
707 ddl->ds = new_surface;
708 ddl->ds_share = SHARE_KIND_NONE;
709 if (ddl->ds) {
710 int width = surface_width(ddl->ds);
711 int height = surface_height(ddl->ds);
713 /* TODO: lazy send dmabuf (there are unnecessary sent otherwise) */
714 dbus_scanout_texture(&ddl->dcl, ddl->ds->texture, false,
715 width, height, 0, 0, width, height, NULL);
718 #endif
720 static void dbus_gfx_switch(DisplayChangeListener *dcl,
721 struct DisplaySurface *new_surface)
723 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
725 ddl->ds = new_surface;
726 ddl->ds_share = SHARE_KIND_NONE;
729 static void dbus_mouse_set(DisplayChangeListener *dcl,
730 int x, int y, bool on)
732 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
734 qemu_dbus_display1_listener_call_mouse_set(
735 ddl->proxy, x, y, on, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
738 static void dbus_cursor_define(DisplayChangeListener *dcl,
739 QEMUCursor *c)
741 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
742 GVariant *v_data = NULL;
744 v_data = g_variant_new_from_data(
745 G_VARIANT_TYPE("ay"),
746 c->data,
747 c->width * c->height * 4,
748 TRUE,
749 (GDestroyNotify)cursor_unref,
750 cursor_ref(c));
752 qemu_dbus_display1_listener_call_cursor_define(
753 ddl->proxy,
754 c->width,
755 c->height,
756 c->hot_x,
757 c->hot_y,
758 v_data,
759 G_DBUS_CALL_FLAGS_NONE,
761 NULL,
762 NULL,
763 NULL);
766 #ifdef CONFIG_OPENGL
767 const DisplayChangeListenerOps dbus_gl_dcl_ops = {
768 .dpy_name = "dbus-gl",
769 .dpy_gfx_update = dbus_gl_gfx_update,
770 .dpy_gfx_switch = dbus_gl_gfx_switch,
771 .dpy_gfx_check_format = console_gl_check_format,
772 .dpy_refresh = dbus_gl_refresh,
773 .dpy_mouse_set = dbus_mouse_set,
774 .dpy_cursor_define = dbus_cursor_define,
776 .dpy_gl_scanout_disable = dbus_scanout_disable,
777 .dpy_gl_scanout_texture = dbus_scanout_texture,
778 #ifdef CONFIG_GBM
779 .dpy_gl_scanout_dmabuf = dbus_scanout_dmabuf,
780 .dpy_gl_cursor_dmabuf = dbus_cursor_dmabuf,
781 .dpy_gl_release_dmabuf = dbus_release_dmabuf,
782 #endif
783 .dpy_gl_cursor_position = dbus_gl_cursor_position,
784 .dpy_gl_update = dbus_scanout_update,
786 #endif
788 const DisplayChangeListenerOps dbus_dcl_ops = {
789 .dpy_name = "dbus",
790 .dpy_gfx_update = dbus_gfx_update,
791 .dpy_gfx_switch = dbus_gfx_switch,
792 .dpy_refresh = dbus_refresh,
793 .dpy_mouse_set = dbus_mouse_set,
794 .dpy_cursor_define = dbus_cursor_define,
797 static void
798 dbus_display_listener_dispose(GObject *object)
800 DBusDisplayListener *ddl = DBUS_DISPLAY_LISTENER(object);
802 unregister_displaychangelistener(&ddl->dcl);
803 g_clear_object(&ddl->conn);
804 g_clear_pointer(&ddl->bus_name, g_free);
805 g_clear_object(&ddl->proxy);
806 #ifdef WIN32
807 g_clear_object(&ddl->map_proxy);
808 g_clear_object(&ddl->d3d11_proxy);
809 g_clear_pointer(&ddl->peer_process, CloseHandle);
810 #ifdef CONFIG_PIXMAN
811 pixman_region32_fini(&ddl->gl_damage);
812 #endif
813 #ifdef CONFIG_OPENGL
814 egl_fb_destroy(&ddl->fb);
815 #endif
816 #endif
818 G_OBJECT_CLASS(dbus_display_listener_parent_class)->dispose(object);
821 static void
822 dbus_display_listener_constructed(GObject *object)
824 DBusDisplayListener *ddl = DBUS_DISPLAY_LISTENER(object);
826 ddl->dcl.ops = &dbus_dcl_ops;
827 #ifdef CONFIG_OPENGL
828 if (display_opengl) {
829 ddl->dcl.ops = &dbus_gl_dcl_ops;
831 #endif
833 G_OBJECT_CLASS(dbus_display_listener_parent_class)->constructed(object);
836 static void
837 dbus_display_listener_class_init(DBusDisplayListenerClass *klass)
839 GObjectClass *object_class = G_OBJECT_CLASS(klass);
841 object_class->dispose = dbus_display_listener_dispose;
842 object_class->constructed = dbus_display_listener_constructed;
845 static void
846 dbus_display_listener_init(DBusDisplayListener *ddl)
848 #ifdef CONFIG_PIXMAN
849 pixman_region32_init(&ddl->gl_damage);
850 #endif
853 const char *
854 dbus_display_listener_get_bus_name(DBusDisplayListener *ddl)
856 return ddl->bus_name ?: "p2p";
859 DBusDisplayConsole *
860 dbus_display_listener_get_console(DBusDisplayListener *ddl)
862 return ddl->console;
865 #ifdef WIN32
866 static bool
867 dbus_display_listener_implements(DBusDisplayListener *ddl, const char *iface)
869 QemuDBusDisplay1Listener *l = QEMU_DBUS_DISPLAY1_LISTENER(ddl->proxy);
870 bool implements;
872 implements = g_strv_contains(qemu_dbus_display1_listener_get_interfaces(l), iface);
873 if (!implements) {
874 g_debug("Display listener does not implement: `%s`", iface);
877 return implements;
880 static bool
881 dbus_display_listener_setup_peer_process(DBusDisplayListener *ddl)
883 g_autoptr(GError) err = NULL;
884 GDBusConnection *conn;
885 GIOStream *stream;
886 GSocket *sock;
887 g_autoptr(GCredentials) creds = NULL;
888 DWORD *pid;
890 if (ddl->peer_process) {
891 return true;
894 conn = g_dbus_proxy_get_connection(G_DBUS_PROXY(ddl->proxy));
895 stream = g_dbus_connection_get_stream(conn);
897 if (!G_IS_UNIX_CONNECTION(stream)) {
898 return false;
901 sock = g_socket_connection_get_socket(G_SOCKET_CONNECTION(stream));
902 creds = g_socket_get_credentials(sock, &err);
904 if (!creds) {
905 g_debug("Failed to get peer credentials: %s", err->message);
906 return false;
909 pid = g_credentials_get_native(creds, G_CREDENTIALS_TYPE_WIN32_PID);
911 if (pid == NULL) {
912 g_debug("Failed to get peer PID");
913 return false;
916 ddl->peer_process = OpenProcess(
917 PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION,
918 false, *pid);
920 if (!ddl->peer_process) {
921 g_autofree char *msg = g_win32_error_message(GetLastError());
922 g_debug("Failed to OpenProcess: %s", msg);
923 return false;
926 return true;
928 #endif
930 static void
931 dbus_display_listener_setup_d3d11(DBusDisplayListener *ddl)
933 #ifdef WIN32
934 g_autoptr(GError) err = NULL;
936 if (!dbus_display_listener_implements(ddl,
937 "org.qemu.Display1.Listener.Win32.D3d11")) {
938 return;
941 if (!dbus_display_listener_setup_peer_process(ddl)) {
942 return;
945 ddl->d3d11_proxy =
946 qemu_dbus_display1_listener_win32_d3d11_proxy_new_sync(ddl->conn,
947 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
948 NULL,
949 "/org/qemu/Display1/Listener",
950 NULL,
951 &err);
952 if (!ddl->d3d11_proxy) {
953 g_debug("Failed to setup win32 d3d11 proxy: %s", err->message);
954 return;
956 #endif
959 static void
960 dbus_display_listener_setup_shared_map(DBusDisplayListener *ddl)
962 #ifdef WIN32
963 g_autoptr(GError) err = NULL;
965 if (!dbus_display_listener_implements(ddl, "org.qemu.Display1.Listener.Win32.Map")) {
966 return;
969 if (!dbus_display_listener_setup_peer_process(ddl)) {
970 return;
973 ddl->map_proxy =
974 qemu_dbus_display1_listener_win32_map_proxy_new_sync(ddl->conn,
975 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
976 NULL,
977 "/org/qemu/Display1/Listener",
978 NULL,
979 &err);
980 if (!ddl->map_proxy) {
981 g_debug("Failed to setup win32 map proxy: %s", err->message);
982 return;
985 ddl->can_share_map = true;
986 #endif
989 static GDBusMessage *
990 dbus_filter(GDBusConnection *connection,
991 GDBusMessage *message,
992 gboolean incoming,
993 gpointer user_data)
995 DBusDisplayListener *ddl = DBUS_DISPLAY_LISTENER(user_data);
996 guint32 serial;
998 if (incoming) {
999 return message;
1002 serial = g_dbus_message_get_serial(message);
1003 if (serial <= ddl->out_serial_to_discard) {
1004 trace_dbus_filter(serial, ddl->out_serial_to_discard);
1005 return NULL;
1008 return message;
1011 DBusDisplayListener *
1012 dbus_display_listener_new(const char *bus_name,
1013 GDBusConnection *conn,
1014 DBusDisplayConsole *console)
1016 DBusDisplayListener *ddl;
1017 QemuConsole *con;
1018 g_autoptr(GError) err = NULL;
1020 ddl = g_object_new(DBUS_DISPLAY_TYPE_LISTENER, NULL);
1021 ddl->proxy =
1022 qemu_dbus_display1_listener_proxy_new_sync(conn,
1023 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
1024 NULL,
1025 "/org/qemu/Display1/Listener",
1026 NULL,
1027 &err);
1028 if (!ddl->proxy) {
1029 error_report("Failed to setup proxy: %s", err->message);
1030 g_object_unref(conn);
1031 g_object_unref(ddl);
1032 return NULL;
1035 ddl->dbus_filter = g_dbus_connection_add_filter(conn, dbus_filter, g_object_ref(ddl), g_object_unref);
1036 ddl->bus_name = g_strdup(bus_name);
1037 ddl->conn = conn;
1038 ddl->console = console;
1040 dbus_display_listener_setup_shared_map(ddl);
1041 dbus_display_listener_setup_d3d11(ddl);
1043 con = qemu_console_lookup_by_index(dbus_display_console_get_index(console));
1044 assert(con);
1045 ddl->dcl.con = con;
1046 register_displaychangelistener(&ddl->dcl);
1048 return ddl;