hw/arm/smmuv3: Support nested SMMUs in smmuv3_notify_iova()
[qemu/ar7.git] / ui / dbus-listener.c
bloba54123acea7ddea22381ac4faa60f016125ca1bb
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 v_data = g_variant_new_from_data(
640 G_VARIANT_TYPE("ay"),
641 pixman_image_get_data(img),
642 pixman_image_get_stride(img) * h,
643 TRUE,
644 (GDestroyNotify)pixman_image_unref,
645 img);
646 qemu_dbus_display1_listener_call_update(ddl->proxy,
647 x, y, w, h, pixman_image_get_stride(img), pixman_image_get_format(img),
648 v_data,
649 G_DBUS_CALL_FLAGS_NONE,
650 DBUS_DEFAULT_TIMEOUT, NULL, NULL, NULL);
653 static void ddl_scanout(DBusDisplayListener *ddl)
655 GVariant *v_data;
657 v_data = g_variant_new_from_data(
658 G_VARIANT_TYPE("ay"), surface_data(ddl->ds),
659 surface_stride(ddl->ds) * surface_height(ddl->ds), TRUE,
660 (GDestroyNotify)pixman_image_unref, pixman_image_ref(ddl->ds->image));
662 ddl_discard_pending_messages(ddl);
664 qemu_dbus_display1_listener_call_scanout(
665 ddl->proxy, surface_width(ddl->ds), surface_height(ddl->ds),
666 surface_stride(ddl->ds), surface_format(ddl->ds), v_data,
667 G_DBUS_CALL_FLAGS_NONE, DBUS_DEFAULT_TIMEOUT, NULL, NULL,
668 g_object_ref(ddl));
671 static void dbus_gfx_update(DisplayChangeListener *dcl,
672 int x, int y, int w, int h)
674 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
676 assert(ddl->ds);
678 trace_dbus_update(x, y, w, h);
680 #ifdef WIN32
681 if (dbus_scanout_map(ddl)) {
682 qemu_dbus_display1_listener_win32_map_call_update_map(
683 ddl->map_proxy,
684 x, y, w, h,
685 G_DBUS_CALL_FLAGS_NONE,
686 DBUS_DEFAULT_TIMEOUT, NULL, NULL, NULL);
687 return;
689 #endif
691 if (x == 0 && y == 0 && w == surface_width(ddl->ds) && h == surface_height(ddl->ds)) {
692 return ddl_scanout(ddl);
695 dbus_gfx_update_sub(ddl, x, y, w, h);
698 #ifdef CONFIG_OPENGL
699 static void dbus_gl_gfx_switch(DisplayChangeListener *dcl,
700 struct DisplaySurface *new_surface)
702 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
704 trace_dbus_gl_gfx_switch(new_surface);
706 ddl->ds = new_surface;
707 ddl->ds_share = SHARE_KIND_NONE;
708 if (ddl->ds) {
709 int width = surface_width(ddl->ds);
710 int height = surface_height(ddl->ds);
712 /* TODO: lazy send dmabuf (there are unnecessary sent otherwise) */
713 dbus_scanout_texture(&ddl->dcl, ddl->ds->texture, false,
714 width, height, 0, 0, width, height, NULL);
717 #endif
719 static void dbus_gfx_switch(DisplayChangeListener *dcl,
720 struct DisplaySurface *new_surface)
722 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
724 ddl->ds = new_surface;
725 ddl->ds_share = SHARE_KIND_NONE;
728 static void dbus_mouse_set(DisplayChangeListener *dcl,
729 int x, int y, bool on)
731 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
733 qemu_dbus_display1_listener_call_mouse_set(
734 ddl->proxy, x, y, on, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
737 static void dbus_cursor_define(DisplayChangeListener *dcl,
738 QEMUCursor *c)
740 DBusDisplayListener *ddl = container_of(dcl, DBusDisplayListener, dcl);
741 GVariant *v_data = NULL;
743 v_data = g_variant_new_from_data(
744 G_VARIANT_TYPE("ay"),
745 c->data,
746 c->width * c->height * 4,
747 TRUE,
748 (GDestroyNotify)cursor_unref,
749 cursor_ref(c));
751 qemu_dbus_display1_listener_call_cursor_define(
752 ddl->proxy,
753 c->width,
754 c->height,
755 c->hot_x,
756 c->hot_y,
757 v_data,
758 G_DBUS_CALL_FLAGS_NONE,
760 NULL,
761 NULL,
762 NULL);
765 #ifdef CONFIG_OPENGL
766 const DisplayChangeListenerOps dbus_gl_dcl_ops = {
767 .dpy_name = "dbus-gl",
768 .dpy_gfx_update = dbus_gl_gfx_update,
769 .dpy_gfx_switch = dbus_gl_gfx_switch,
770 .dpy_gfx_check_format = console_gl_check_format,
771 .dpy_refresh = dbus_gl_refresh,
772 .dpy_mouse_set = dbus_mouse_set,
773 .dpy_cursor_define = dbus_cursor_define,
775 .dpy_gl_scanout_disable = dbus_scanout_disable,
776 .dpy_gl_scanout_texture = dbus_scanout_texture,
777 #ifdef CONFIG_GBM
778 .dpy_gl_scanout_dmabuf = dbus_scanout_dmabuf,
779 .dpy_gl_cursor_dmabuf = dbus_cursor_dmabuf,
780 .dpy_gl_release_dmabuf = dbus_release_dmabuf,
781 #endif
782 .dpy_gl_cursor_position = dbus_gl_cursor_position,
783 .dpy_gl_update = dbus_scanout_update,
785 #endif
787 const DisplayChangeListenerOps dbus_dcl_ops = {
788 .dpy_name = "dbus",
789 .dpy_gfx_update = dbus_gfx_update,
790 .dpy_gfx_switch = dbus_gfx_switch,
791 .dpy_refresh = dbus_refresh,
792 .dpy_mouse_set = dbus_mouse_set,
793 .dpy_cursor_define = dbus_cursor_define,
796 static void
797 dbus_display_listener_dispose(GObject *object)
799 DBusDisplayListener *ddl = DBUS_DISPLAY_LISTENER(object);
801 unregister_displaychangelistener(&ddl->dcl);
802 g_clear_object(&ddl->conn);
803 g_clear_pointer(&ddl->bus_name, g_free);
804 g_clear_object(&ddl->proxy);
805 #ifdef WIN32
806 g_clear_object(&ddl->map_proxy);
807 g_clear_object(&ddl->d3d11_proxy);
808 g_clear_pointer(&ddl->peer_process, CloseHandle);
809 #ifdef CONFIG_PIXMAN
810 pixman_region32_fini(&ddl->gl_damage);
811 #endif
812 #ifdef CONFIG_OPENGL
813 egl_fb_destroy(&ddl->fb);
814 #endif
815 #endif
817 G_OBJECT_CLASS(dbus_display_listener_parent_class)->dispose(object);
820 static void
821 dbus_display_listener_constructed(GObject *object)
823 DBusDisplayListener *ddl = DBUS_DISPLAY_LISTENER(object);
825 ddl->dcl.ops = &dbus_dcl_ops;
826 #ifdef CONFIG_OPENGL
827 if (display_opengl) {
828 ddl->dcl.ops = &dbus_gl_dcl_ops;
830 #endif
832 G_OBJECT_CLASS(dbus_display_listener_parent_class)->constructed(object);
835 static void
836 dbus_display_listener_class_init(DBusDisplayListenerClass *klass)
838 GObjectClass *object_class = G_OBJECT_CLASS(klass);
840 object_class->dispose = dbus_display_listener_dispose;
841 object_class->constructed = dbus_display_listener_constructed;
844 static void
845 dbus_display_listener_init(DBusDisplayListener *ddl)
847 #ifdef CONFIG_PIXMAN
848 pixman_region32_init(&ddl->gl_damage);
849 #endif
852 const char *
853 dbus_display_listener_get_bus_name(DBusDisplayListener *ddl)
855 return ddl->bus_name ?: "p2p";
858 DBusDisplayConsole *
859 dbus_display_listener_get_console(DBusDisplayListener *ddl)
861 return ddl->console;
864 #ifdef WIN32
865 static bool
866 dbus_display_listener_implements(DBusDisplayListener *ddl, const char *iface)
868 QemuDBusDisplay1Listener *l = QEMU_DBUS_DISPLAY1_LISTENER(ddl->proxy);
869 bool implements;
871 implements = g_strv_contains(qemu_dbus_display1_listener_get_interfaces(l), iface);
872 if (!implements) {
873 g_debug("Display listener does not implement: `%s`", iface);
876 return implements;
879 static bool
880 dbus_display_listener_setup_peer_process(DBusDisplayListener *ddl)
882 g_autoptr(GError) err = NULL;
883 GDBusConnection *conn;
884 GIOStream *stream;
885 GSocket *sock;
886 g_autoptr(GCredentials) creds = NULL;
887 DWORD *pid;
889 if (ddl->peer_process) {
890 return true;
893 conn = g_dbus_proxy_get_connection(G_DBUS_PROXY(ddl->proxy));
894 stream = g_dbus_connection_get_stream(conn);
896 if (!G_IS_UNIX_CONNECTION(stream)) {
897 return false;
900 sock = g_socket_connection_get_socket(G_SOCKET_CONNECTION(stream));
901 creds = g_socket_get_credentials(sock, &err);
903 if (!creds) {
904 g_debug("Failed to get peer credentials: %s", err->message);
905 return false;
908 pid = g_credentials_get_native(creds, G_CREDENTIALS_TYPE_WIN32_PID);
910 if (pid == NULL) {
911 g_debug("Failed to get peer PID");
912 return false;
915 ddl->peer_process = OpenProcess(
916 PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION,
917 false, *pid);
919 if (!ddl->peer_process) {
920 g_autofree char *msg = g_win32_error_message(GetLastError());
921 g_debug("Failed to OpenProcess: %s", msg);
922 return false;
925 return true;
927 #endif
929 static void
930 dbus_display_listener_setup_d3d11(DBusDisplayListener *ddl)
932 #ifdef WIN32
933 g_autoptr(GError) err = NULL;
935 if (!dbus_display_listener_implements(ddl,
936 "org.qemu.Display1.Listener.Win32.D3d11")) {
937 return;
940 if (!dbus_display_listener_setup_peer_process(ddl)) {
941 return;
944 ddl->d3d11_proxy =
945 qemu_dbus_display1_listener_win32_d3d11_proxy_new_sync(ddl->conn,
946 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
947 NULL,
948 "/org/qemu/Display1/Listener",
949 NULL,
950 &err);
951 if (!ddl->d3d11_proxy) {
952 g_debug("Failed to setup win32 d3d11 proxy: %s", err->message);
953 return;
955 #endif
958 static void
959 dbus_display_listener_setup_shared_map(DBusDisplayListener *ddl)
961 #ifdef WIN32
962 g_autoptr(GError) err = NULL;
964 if (!dbus_display_listener_implements(ddl, "org.qemu.Display1.Listener.Win32.Map")) {
965 return;
968 if (!dbus_display_listener_setup_peer_process(ddl)) {
969 return;
972 ddl->map_proxy =
973 qemu_dbus_display1_listener_win32_map_proxy_new_sync(ddl->conn,
974 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
975 NULL,
976 "/org/qemu/Display1/Listener",
977 NULL,
978 &err);
979 if (!ddl->map_proxy) {
980 g_debug("Failed to setup win32 map proxy: %s", err->message);
981 return;
984 ddl->can_share_map = true;
985 #endif
988 static GDBusMessage *
989 dbus_filter(GDBusConnection *connection,
990 GDBusMessage *message,
991 gboolean incoming,
992 gpointer user_data)
994 DBusDisplayListener *ddl = DBUS_DISPLAY_LISTENER(user_data);
995 guint32 serial;
997 if (incoming) {
998 return message;
1001 serial = g_dbus_message_get_serial(message);
1002 if (serial <= ddl->out_serial_to_discard) {
1003 trace_dbus_filter(serial, ddl->out_serial_to_discard);
1004 return NULL;
1007 return message;
1010 DBusDisplayListener *
1011 dbus_display_listener_new(const char *bus_name,
1012 GDBusConnection *conn,
1013 DBusDisplayConsole *console)
1015 DBusDisplayListener *ddl;
1016 QemuConsole *con;
1017 g_autoptr(GError) err = NULL;
1019 ddl = g_object_new(DBUS_DISPLAY_TYPE_LISTENER, NULL);
1020 ddl->proxy =
1021 qemu_dbus_display1_listener_proxy_new_sync(conn,
1022 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
1023 NULL,
1024 "/org/qemu/Display1/Listener",
1025 NULL,
1026 &err);
1027 if (!ddl->proxy) {
1028 error_report("Failed to setup proxy: %s", err->message);
1029 g_object_unref(conn);
1030 g_object_unref(ddl);
1031 return NULL;
1034 ddl->dbus_filter = g_dbus_connection_add_filter(conn, dbus_filter, g_object_ref(ddl), g_object_unref);
1035 ddl->bus_name = g_strdup(bus_name);
1036 ddl->conn = conn;
1037 ddl->console = console;
1039 dbus_display_listener_setup_shared_map(ddl);
1040 dbus_display_listener_setup_d3d11(ddl);
1042 con = qemu_console_lookup_by_index(dbus_display_console_get_index(console));
1043 assert(con);
1044 ddl->dcl.con = con;
1045 register_displaychangelistener(&ddl->dcl);
1047 return ddl;