Skip to contentSkip to conversation

virtio-gpu-virgl: introduce Venus support for macOS

RFC PATCH Superseded Joelle van Dyne 4 participants 35 messages Dec 3, 2025 – Jan 5 Arm/Amlogic Meson SoC support

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover virtio-gpu-virgl: introduce Venus support for macOS

This brings Vulkan guest paravirtualization to macOS hosts. The bulk of the work is being submitted to the virglrenderer project so this is a RFC for now.

In addition to Vulkan, we also introduce Metal texture scanout for GLES guests as well. We also added Metal texture rendering in the Cocoa backend but that builds upon the GLES work by Akihiko Odaki which is not currently upstream.

Fork with all dependent changes:
https://github.com/utmapp/qemu/tree/submit/macos-venus
Instructions for building and testing with all related projects:
https://gist.github.com/osy/a8f705050eed1c8421ad1a0855a8faa9

Joelle van Dyne (7):
egl-helpers: store handle to native device
virtio-gpu-virgl: check page alignment of blob mapping
console: rename d3d_tex2d to native
virtio-gpu-virgl: update virglrenderer defines
virtio-gpu-virgl: support scanout of Metal textures
console: add cleanup callback for ScanoutTexture
virtio-gpu-virgl: add support for native blob scanout

 meson.build                   |   9 +++
 include/ui/console.h          |  31 ++++++++-
 include/ui/egl-helpers.h      |   2 +-
 include/ui/gtk.h              |   4 +-
 include/ui/sdl2.h             |   2 +-
 hw/display/virtio-gpu-virgl.c | 126 +++++++++++++++++++++++++++++++---
 hw/display/virtio-gpu.c       |   2 +
 ui/console.c                  |  60 +++++++++++++---
 ui/dbus-console.c             |   2 +-
 ui/dbus-listener.c            |   8 +--
 ui/egl-headless.c             |   2 +-
 ui/egl-helpers.c              |   4 +-
 ui/gtk-egl.c                  |   2 +-
 ui/gtk-gl-area.c              |   2 +-
 ui/sdl2-gl.c                  |   2 +-
 ui/spice-display.c            |   2 +-
 16 files changed, 225 insertions(+), 35 deletions(-)
Signature · 1 line
2.41.0
#30 lore raw

RFC PATCH 5/7 virtio-gpu-virgl: support scanout of Metal textures

When supported, virglrenderer will return a MTLTexture handle that can be directly used for scanout.

---
 meson.build                   |  4 ++++
 include/ui/console.h          |  2 ++
 hw/display/virtio-gpu-virgl.c | 12 +++++++++++-
 hw/display/virtio-gpu.c       |  2 ++
 4 files changed, 19 insertions(+), 1 deletion(-)
meson.build +4 -0
index d9293294d8..05bad66376 100644
@@ -832,6 +832,7 @@ version_res = []
 coref = []
 iokit = []
 pvg = not_found
+metal = not_found
 emulator_link_args = []
 midl = not_found
 widl = not_found
@@ -859,6 +860,7 @@ elif host_os == 'darwin'
   host_dsosuf = '.dylib'
   pvg = dependency('appleframeworks', modules: ['ParavirtualizedGraphics', 'Metal'],
                    required: get_option('pvg'))
+  metal = dependency('appleframeworks', modules: 'Metal', required: false)
 elif host_os == 'sunos'
   socket = [cc.find_library('socket'),
             cc.find_library('nsl'),
@@ -2591,6 +2593,7 @@ if xen.found()
     ('0' + xen_version[2]).substring(-2)
   config_host_data.set('CONFIG_XEN_CTRL_INTERFACE_VERSION', xen_ctrl_version)
 endif
+config_host_data.set('CONFIG_METAL', metal.found())
 config_host_data.set('QEMU_VERSION', '"@0@"'.format(meson.project_version()))
 config_host_data.set('QEMU_VERSION_MAJOR', meson.project_version().split('.')[0])
 config_host_data.set('QEMU_VERSION_MINOR', meson.project_version().split('.')[1])
@@ -4874,6 +4877,7 @@ summary(summary_info, bool_yn: true, section: 'Crypto')
 summary_info = {}
 if host_os == 'darwin'
   summary_info += {'Cocoa support':           cocoa}
+  summary_info += {'Metal support':           metal}
 endif
 summary_info += {'D-Bus display':     dbus_display}
 summary_info += {'SDL support':       sdl}
include/ui/console.h +2 -0
index 25e45295d4..a45b524c57 100644
@@ -134,12 +134,14 @@ struct QemuConsoleClass {
 typedef enum ScanoutTextureNativeType {
     SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
     SCANOUT_TEXTURE_NATIVE_TYPE_D3D,
+    SCANOUT_TEXTURE_NATIVE_TYPE_METAL,
 } ScanoutTextureNativeType;
 
 typedef struct ScanoutTextureNative {
     ScanoutTextureNativeType type;
     union {
         void *d3d_tex2d;
+        void *metal_texture;
     } u;
 } ScanoutTextureNative;
 
hw/display/virtio-gpu-virgl.c +11 -1
index 36c670f988..e091eb0c76 100644
@@ -452,6 +452,13 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
 #if VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION >= SUPPORTED_VIRGL_INFO_EXT_VERSION
         if (ext.version >= VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION) {
             switch (ext.native_type) {
+#ifdef CONFIG_METAL
+            case VIRGL_NATIVE_HANDLE_METAL_TEXTURE: {
+                native.type = SCANOUT_TEXTURE_NATIVE_TYPE_METAL;
+                native.u.metal_texture = ext.native_handle;
+                break;
+            }
+#endif
             case VIRGL_NATIVE_HANDLE_NONE:
             case VIRGL_NATIVE_HANDLE_D3D_TEX2D: {
                 /* already handled above */
@@ -1198,7 +1205,10 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
     }
 #if VIRGL_VERSION_MAJOR >= 1
     if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
-        flags |= VIRGL_RENDERER_VENUS | VIRGL_RENDERER_RENDER_SERVER;
+        flags |= VIRGL_RENDERER_VENUS;
+#ifndef CONFIG_METAL /* Metal does not support render server */
+        flags |= VIRGL_RENDERER_RENDER_SERVER;
+#endif
     }
 #endif
 
hw/display/virtio-gpu.c +2 -0
index 43e88a4daf..9cf2c15a43 100644
@@ -1485,12 +1485,14 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
     VirtIOGPU *g = VIRTIO_GPU(qdev);
 
     if (virtio_gpu_blob_enabled(g->parent_obj.conf)) {
+#ifndef CONFIG_METAL
         if (!virtio_gpu_rutabaga_enabled(g->parent_obj.conf) &&
             !virtio_gpu_virgl_enabled(g->parent_obj.conf) &&
             !virtio_gpu_have_udmabuf()) {
             error_setg(errp, "need rutabaga or udmabuf for blob resources");
             return;
         }
+#endif
 
 #ifdef VIRGL_VERSION_MAJOR
     #if VIRGL_VERSION_MAJOR < 1
Signature · 1 line
2.41.0
#13 lore raw

RFC PATCH 2/7 virtio-gpu-virgl: check page alignment of blob mapping

In reply to Joelle van Dyne

Currently if a mapping is not page aligned, it will sliently fail and the guest, assuming it is mapped, will attempt to access the memory and fail. This is particularly an issue on macOS when the host page size is 16KiB and the guest page size is 4KiB.

---
 hw/display/virtio-gpu-virgl.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
hw/display/virtio-gpu-virgl.c +14 -0
index 20c856c04e..adf02ac22b 100644
@@ -116,6 +116,20 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
         return ret;
     }
 
+    if (!QEMU_IS_ALIGNED((uintptr_t)data, qemu_real_host_page_size())) {
+        virgl_renderer_resource_unmap(res->base.resource_id);
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: address %p is not aligned to page size\n",
+                      __func__, data);
+        return -ENOMEM;
+    }
+
+    if (!QEMU_IS_ALIGNED(size, qemu_real_host_page_size())) {
+        virgl_renderer_resource_unmap(res->base.resource_id);
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: size 0x%llx is not aligned to page size\n",
+                      __func__, size);
+        return -ENOMEM;
+    }
+
     vmr = g_new0(struct virtio_gpu_virgl_hostmem_region, 1);
     vmr->g = g;
 
Signature · 1 line
2.41.0
#32 lore raw

RFC PATCH 6/7 console: add cleanup callback for ScanoutTexture

In reply to Joelle van Dyne

Before we introduce changes that allow for QemuConsole to take ownership of a texture handle, we need scaffolding that will allow us to callback into a cleanup function any time the ScanoutTexture becomes invalid, which is whenever the scanout.kind or scanout.texture gets updated.

The ordering is important: we need to first update the DisplayScanout, then we need to notify all the listeners, and once all the listeners have had the chance to finish using the previous native texture, we are safe to call the cleanup function. This means we need to hold on to the previous scanout native handle locally until all listeners are notified.

---
 include/ui/console.h          |  9 +++++-
 hw/display/virtio-gpu-virgl.c |  2 +-
 ui/console.c                  | 56 +++++++++++++++++++++++++++++++----
 3 files changed, 59 insertions(+), 8 deletions(-)
include/ui/console.h +8 -1
index a45b524c57..df9c083a16 100644
@@ -149,6 +149,11 @@ typedef struct ScanoutTextureNative {
     .type = SCANOUT_TEXTURE_NATIVE_TYPE_NONE \
 })
 
+/**
+ * Cleanup callback function when ScanoutTexture is about to be destroyed
+ */
+typedef void (*dpy_cleanup_texture)(ScanoutTextureNative *native);
+
 typedef struct ScanoutTexture {
     uint32_t backing_id;
     bool backing_y_0_top;
@@ -159,6 +164,7 @@ typedef struct ScanoutTexture {
     uint32_t width;
     uint32_t height;
     ScanoutTextureNative native;
+    dpy_cleanup_texture cb_cleanup;
 } ScanoutTexture;
 
 typedef struct QemuUIInfo {
@@ -347,7 +353,8 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                             uint32_t backing_id, bool backing_y_0_top,
                             uint32_t backing_width, uint32_t backing_height,
                             uint32_t x, uint32_t y, uint32_t w, uint32_t h,
-                            ScanoutTextureNative native);
+                            ScanoutTextureNative native,
+                            dpy_cleanup_texture cb_cleanup);
 void dpy_gl_scanout_dmabuf(QemuConsole *con,
                            QemuDmaBuf *dmabuf);
 void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
hw/display/virtio-gpu-virgl.c +1 -1
index e091eb0c76..b7bc095676 100644
@@ -492,7 +492,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
             info.flags & VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP,
             info.width, info.height,
             ss.r.x, ss.r.y, ss.r.width, ss.r.height,
-            native);
+            native, NULL);
     } else {
         dpy_gfx_replace_surface(
             g->parent_obj.scanout[ss.scanout_id].con, NULL);
ui/console.c +50 -6

125 lines not on this page · raw message

Signature · 1 line
2.41.0
#25 lore raw

RFC PATCH 4/7 virtio-gpu-virgl: update virglrenderer defines

In reply to Joelle van Dyne

In order to support additional native texture types, we need to update the defines in virglrenderer. The changes are backwards compatible and so builds should work with either the new version or the old version.

---
 hw/display/virtio-gpu-virgl.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-virgl.c +25 -3
index d0e6ad4b17..36c670f988 100644
@@ -24,6 +24,8 @@
 
 #include <virglrenderer.h>
 
+#define SUPPORTED_VIRGL_INFO_EXT_VERSION (1)
+
 struct virtio_gpu_virgl_resource {
     struct virtio_gpu_simple_resource base;
     MemoryRegion *mr;
@@ -441,12 +443,30 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
         memset(&ext, 0, sizeof(ext));
         ret = virgl_renderer_resource_get_info_ext(ss.resource_id, &ext);
         info = ext.base;
+        /* fallback to older version */
         native = (ScanoutTextureNative){
             .type = ext.d3d_tex2d ? SCANOUT_TEXTURE_NATIVE_TYPE_D3D :
                                     SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
             .u.d3d_tex2d = ext.d3d_tex2d,
         };
-#else
+#if VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION >= SUPPORTED_VIRGL_INFO_EXT_VERSION
+        if (ext.version >= VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION) {
+            switch (ext.native_type) {
+            case VIRGL_NATIVE_HANDLE_NONE:
+            case VIRGL_NATIVE_HANDLE_D3D_TEX2D: {
+                /* already handled above */
+                break;
+            }
+            default: {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                            "%s: unsupported native texture type %d\n",
+                            __func__, ext.native_type);
+                break;
+            }
+            }
+        }
+#endif
+#else /* VIRGL_VERSION_MAJOR < 1 */
         memset(&info, 0, sizeof(info));
         ret = virgl_renderer_resource_get_info(ss.resource_id, &info);
 #endif
@@ -1169,11 +1189,13 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
         virtio_gpu_3d_cbs.get_egl_display = virgl_get_egl_display;
     }
 #endif
-#ifdef VIRGL_RENDERER_D3D11_SHARE_TEXTURE
     if (qemu_egl_angle_native_device) {
+#if defined(VIRGL_RENDERER_NATIVE_SHARE_TEXTURE)
+        flags |= VIRGL_RENDERER_NATIVE_SHARE_TEXTURE;
+#elif defined(VIRGL_RENDERER_D3D11_SHARE_TEXTURE) && defined(WIN32)
         flags |= VIRGL_RENDERER_D3D11_SHARE_TEXTURE;
-    }
 #endif
+    }
 #if VIRGL_VERSION_MAJOR >= 1
     if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
         flags |= VIRGL_RENDERER_VENUS | VIRGL_RENDERER_RENDER_SERVER;
Signature · 1 line
2.41.0
#18 lore raw

RFC PATCH 3/7 console: rename `d3d_tex2d` to `native`

In reply to Joelle van Dyne

In order to support native texture scanout beyond D3D, we make this more generic allowing for multiple native texture handle types.

---
 include/ui/console.h          | 22 +++++++++++++++++++---
 include/ui/gtk.h              |  4 ++--
 include/ui/sdl2.h             |  2 +-
 hw/display/virtio-gpu-virgl.c | 10 +++++++---
 ui/console.c                  |  8 ++++----
 ui/dbus-console.c             |  2 +-
 ui/dbus-listener.c            |  8 ++++----
 ui/egl-headless.c             |  2 +-
 ui/gtk-egl.c                  |  2 +-
 ui/gtk-gl-area.c              |  2 +-
 ui/sdl2-gl.c                  |  2 +-
 ui/spice-display.c            |  2 +-
 12 files changed, 43 insertions(+), 23 deletions(-)
include/ui/console.h +19 -3
index 98feaa58bd..25e45295d4 100644
@@ -131,6 +131,22 @@ struct QemuConsoleClass {
     ObjectClass parent_class;
 };
 
+typedef enum ScanoutTextureNativeType {
+    SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
+    SCANOUT_TEXTURE_NATIVE_TYPE_D3D,
+} ScanoutTextureNativeType;
+
+typedef struct ScanoutTextureNative {
+    ScanoutTextureNativeType type;
+    union {
+        void *d3d_tex2d;
+    } u;
+} ScanoutTextureNative;
+
+#define NO_NATIVE_TEXTURE ((ScanoutTextureNative){ \
+    .type = SCANOUT_TEXTURE_NATIVE_TYPE_NONE \
+})
+
 typedef struct ScanoutTexture {
     uint32_t backing_id;
     bool backing_y_0_top;
@@ -140,7 +156,7 @@ typedef struct ScanoutTexture {
     uint32_t y;
     uint32_t width;
     uint32_t height;
-    void *d3d_tex2d;
+    ScanoutTextureNative native;
 } ScanoutTexture;
 
 typedef struct QemuUIInfo {
@@ -245,7 +261,7 @@ typedef struct DisplayChangeListenerOps {
                                    uint32_t backing_height,
                                    uint32_t x, uint32_t y,
                                    uint32_t w, uint32_t h,
-                                   void *d3d_tex2d);
+                                   ScanoutTextureNative native);
     /* optional (default to true if has dpy_gl_scanout_dmabuf) */
     bool (*dpy_has_dmabuf)(DisplayChangeListener *dcl);
     /* optional */
@@ -329,7 +345,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                             uint32_t backing_id, bool backing_y_0_top,
                             uint32_t backing_width, uint32_t backing_height,
                             uint32_t x, uint32_t y, uint32_t w, uint32_t h,
-                            void *d3d_tex2d);
+                            ScanoutTextureNative native);
 void dpy_gl_scanout_dmabuf(QemuConsole *con,
                            QemuDmaBuf *dmabuf);
 void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
include/ui/gtk.h +2 -2
index 3e6ce3cb48..f88e2b281d 100644
@@ -178,7 +178,7 @@ void gd_egl_scanout_texture(DisplayChangeListener *dcl,
                             uint32_t backing_height,
                             uint32_t x, uint32_t y,
                             uint32_t w, uint32_t h,
-                            void *d3d_tex2d);
+                            ScanoutTextureNative native);
 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
                            QemuDmaBuf *dmabuf);
 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
@@ -215,7 +215,7 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                 uint32_t backing_height,
                                 uint32_t x, uint32_t y,
                                 uint32_t w, uint32_t h,
-                                void *d3d_tex2d);
+                                ScanoutTextureNative native);
 void gd_gl_area_scanout_disable(DisplayChangeListener *dcl);
 void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
                               uint32_t x, uint32_t y, uint32_t w, uint32_t h);
include/ui/sdl2.h +1 -1
index dbe6e3d973..fdefb88229 100644
@@ -93,7 +93,7 @@ void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
                              uint32_t backing_height,
                              uint32_t x, uint32_t y,
                              uint32_t w, uint32_t h,
-                             void *d3d_tex2d);
+                             ScanoutTextureNative native);
 void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
                            uint32_t x, uint32_t y, uint32_t w, uint32_t h);
 
hw/display/virtio-gpu-virgl.c +7 -3
index adf02ac22b..d0e6ad4b17 100644
@@ -434,14 +434,18 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
 
     if (ss.resource_id && ss.r.width && ss.r.height) {
         struct virgl_renderer_resource_info info;
-        void *d3d_tex2d = NULL;
+        ScanoutTextureNative native = { .type = SCANOUT_TEXTURE_NATIVE_TYPE_NONE };
 
 #if VIRGL_VERSION_MAJOR >= 1
         struct virgl_renderer_resource_info_ext ext;
         memset(&ext, 0, sizeof(ext));
         ret = virgl_renderer_resource_get_info_ext(ss.resource_id, &ext);
         info = ext.base;
-        d3d_tex2d = ext.d3d_tex2d;
+        native = (ScanoutTextureNative){
+            .type = ext.d3d_tex2d ? SCANOUT_TEXTURE_NATIVE_TYPE_D3D :
+                                    SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
+            .u.d3d_tex2d = ext.d3d_tex2d,
+        };
 #else
         memset(&info, 0, sizeof(info));
         ret = virgl_renderer_resource_get_info(ss.resource_id, &info);
@@ -461,7 +465,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
             info.flags & VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP,
             info.width, info.height,
             ss.r.x, ss.r.y, ss.r.width, ss.r.height,
-            d3d_tex2d);
+            native);
     } else {
         dpy_gfx_replace_surface(
             g->parent_obj.scanout[ss.scanout_id].con, NULL);
ui/console.c +4 -4
index 2d00828c53..9378afd53d 100644
@@ -295,7 +295,7 @@ static void displaychangelistener_display_console(DisplayChangeListener *dcl,
                                          con->scanout.texture.y,
                                          con->scanout.texture.width,
                                          con->scanout.texture.height,
-                                         con->scanout.texture.d3d_tex2d);
+                                         con->scanout.texture.native);
     }
 }
 
@@ -1023,7 +1023,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                             uint32_t backing_height,
                             uint32_t x, uint32_t y,
                             uint32_t width, uint32_t height,
-                            void *d3d_tex2d)
+                            ScanoutTextureNative native)
 {
     DisplayState *s = con->ds;
     DisplayChangeListener *dcl;
@@ -1031,7 +1031,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
     con->scanout.kind = SCANOUT_TEXTURE;
     con->scanout.texture = (ScanoutTexture) {
         backing_id, backing_y_0_top, backing_width, backing_height,
-        x, y, width, height, d3d_tex2d,
+        x, y, width, height, native,
     };
     QLIST_FOREACH(dcl, &s->listeners, next) {
         if (con != dcl->con) {
@@ -1042,7 +1042,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                                              backing_y_0_top,
                                              backing_width, backing_height,
                                              x, y, width, height,
-                                             d3d_tex2d);
+                                             native);
         }
     }
 }
ui/dbus-console.c +1 -1
index 85e215ef23..651f0daeaf 100644
@@ -99,7 +99,7 @@ dbus_gl_scanout_texture(DisplayChangeListener *dcl,
                         uint32_t backing_height,
                         uint32_t x, uint32_t y,
                         uint32_t w, uint32_t h,
-                        void *d3d_tex2d)
+                        ScanoutTextureNative native)
 {
     DBusDisplayConsole *ddc = container_of(dcl, DBusDisplayConsole, dcl);
 
ui/dbus-listener.c +4 -4
index 52e041edb0..a737752996 100644
@@ -587,7 +587,7 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
                                  uint32_t backing_height,
                                  uint32_t x, uint32_t y,
                                  uint32_t w, uint32_t h,
-                                 void *d3d_tex2d)
+                                 ScanoutTextureNative native)
 {
     trace_dbus_scanout_texture(tex_id, backing_y_0_top,
                                backing_width, backing_height, x, y, w, h);
@@ -618,8 +618,8 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
     assert(surface_width(ddl->ds) == w);
     assert(surface_height(ddl->ds) == h);
 
-    if (d3d_tex2d) {
-        dbus_scanout_share_d3d_texture(ddl, d3d_tex2d, backing_y_0_top,
+    if (native.type == SCANOUT_TEXTURE_NATIVE_TYPE_D3d) {
+        dbus_scanout_share_d3d_texture(ddl, native.u.d3d_tex2d, backing_y_0_top,
                                        backing_width, backing_height, x, y, w, h);
     } else {
         dbus_scanout_map(ddl);
@@ -868,7 +868,7 @@ static void dbus_gl_gfx_switch(DisplayChangeListener *dcl,
 
         /* TODO: lazy send dmabuf (there are unnecessary sent otherwise) */
         dbus_scanout_texture(&ddl->dcl, ddl->ds->texture, false,
-                             width, height, 0, 0, width, height, NULL);
+                             width, height, 0, 0, width, height, NO_NATIVE_TEXTURE);
     }
 }
 #endif
ui/egl-headless.c +1 -1
index 1f6b845500..e36778bd48 100644
@@ -62,7 +62,7 @@ static void egl_scanout_texture(DisplayChangeListener *dcl,
                                 uint32_t backing_height,
                                 uint32_t x, uint32_t y,
                                 uint32_t w, uint32_t h,
-                                void *d3d_tex2d)
+                                ScanoutTextureNative native)
 {
     egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
ui/gtk-egl.c +1 -1
index ae9239999c..7933237d45 100644
@@ -239,7 +239,7 @@ void gd_egl_scanout_texture(DisplayChangeListener *dcl,
                             uint32_t backing_width, uint32_t backing_height,
                             uint32_t x, uint32_t y,
                             uint32_t w, uint32_t h,
-                            void *d3d_tex2d)
+                            ScanoutTextureNative native)
 {
     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 
ui/gtk-gl-area.c +1 -1
index cd86022d26..f2ed543d6a 100644
@@ -306,7 +306,7 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                 uint32_t backing_height,
                                 uint32_t x, uint32_t y,
                                 uint32_t w, uint32_t h,
-                                void *d3d_tex2d)
+                                ScanoutTextureNative native)
 {
     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 
ui/sdl2-gl.c +1 -1
index 3be17d1079..b15db86f24 100644
@@ -206,7 +206,7 @@ void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
                              uint32_t backing_height,
                              uint32_t x, uint32_t y,
                              uint32_t w, uint32_t h,
-                             void *d3d_tex2d)
+                             ScanoutTextureNative native)
 {
     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
ui/spice-display.c +1 -1
index db71e866f8..2a2285cb1f 100644
@@ -1084,7 +1084,7 @@ static void qemu_spice_gl_scanout_texture(DisplayChangeListener *dcl,
                                           uint32_t backing_height,
                                           uint32_t x, uint32_t y,
                                           uint32_t w, uint32_t h,
-                                          void *d3d_tex2d)
+                                          ScanoutTextureNative native)
 {
     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
     EGLint offset[DMABUF_MAX_PLANES], stride[DMABUF_MAX_PLANES], fourcc = 0;
Signature · 1 line
2.41.0
#1 lore raw

RFC PATCH 1/7 egl-helpers: store handle to native device

In reply to Joelle van Dyne

Make way for other platforms by making the variable more general. Also we will be using the device in the future so let's save the pointer in the global instead of just a boolean flag.

---
 include/ui/egl-helpers.h      | 2 +-
 hw/display/virtio-gpu-virgl.c | 2 +-
 ui/egl-helpers.c              | 4 ++--
 3 files changed, 4 insertions(+), 4 deletions(-)
include/ui/egl-helpers.h +1 -1
index acf993fcf5..c239d32317 100644
@@ -12,7 +12,7 @@
 extern EGLDisplay *qemu_egl_display;
 extern EGLConfig qemu_egl_config;
 extern DisplayGLMode qemu_egl_mode;
-extern bool qemu_egl_angle_d3d;
+extern void *qemu_egl_angle_native_device;
 
 typedef struct egl_fb {
     int width;
hw/display/virtio-gpu-virgl.c +1 -1
index 07f6355ad6..20c856c04e 100644
@@ -1152,7 +1152,7 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
     }
 #endif
 #ifdef VIRGL_RENDERER_D3D11_SHARE_TEXTURE
-    if (qemu_egl_angle_d3d) {
+    if (qemu_egl_angle_native_device) {
         flags |= VIRGL_RENDERER_D3D11_SHARE_TEXTURE;
     }
 #endif
ui/egl-helpers.c +2 -2
index e3f2872cc1..57bcf99498 100644
@@ -28,7 +28,7 @@
 EGLDisplay *qemu_egl_display;
 EGLConfig qemu_egl_config;
 DisplayGLMode qemu_egl_mode;
-bool qemu_egl_angle_d3d;
+void *qemu_egl_angle_native_device;
 
 /* ------------------------------------------------------------------ */
 
@@ -651,7 +651,7 @@ int qemu_egl_init_dpy_win32(EGLNativeDisplayType dpy, DisplayGLMode mode)
         }
 
         trace_egl_init_d3d11_device(device);
-        qemu_egl_angle_d3d = device != NULL;
+        qemu_egl_angle_native_device = d3d11_device;
     }
 #endif
 
Signature · 1 line
2.41.0
#34 lore raw

RFC PATCH 7/7 virtio-gpu-virgl: add support for native blob scanout

In reply to Joelle van Dyne

On macOS we do not have dmabuf and so we use MTLTexture as our scanout source. For blob scanout, the buffer is untyped and so we cannot get a MTLTexture until we pass more information to virglrenderer (surface size, pixel format, etc). The new API to do this is currently unstable so we need to define VIRGL_RENDERER_UNSTABLE_APIS. This should be removed after the ABI becomes stable.

---
 meson.build                   |  5 +++
 hw/display/virtio-gpu-virgl.c | 60 +++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)
meson.build +5 -0
index 05bad66376..8917aff044 100644
@@ -2549,6 +2549,11 @@ config_host_data.set('CONFIG_VNC', vnc.found())
 config_host_data.set('CONFIG_VNC_JPEG', jpeg.found())
 config_host_data.set('CONFIG_VNC_SASL', sasl.found())
 if virgl.found()
+  config_host_data.set('HAVE_VIRGL_RENDERER_NATIVE_SCANOUT',
+                        cc.has_function('virgl_renderer_create_handle_for_scanout',
+                                        args: '-DVIRGL_RENDERER_UNSTABLE_APIS',
+                                        prefix: '#include <virglrenderer.h>',
+                                        dependencies: virgl))
   config_host_data.set('VIRGL_VERSION_MAJOR', virgl.version().split('.')[0])
 endif
 config_host_data.set('CONFIG_VIRTFS', have_virtfs)
hw/display/virtio-gpu-virgl.c +60 -0

78 lines not on this page · raw message

Signature · 1 line
2.41.0
#14 lore raw

Re: [PATCH RFC 2/7] virtio-gpu-virgl: check page alignment of blob mapping

In reply to Joelle van Dyne
Quoted 4 lines · Joelle van Dyne

Currently if a mapping is not page aligned, it will sliently fail and the guest, assuming it is mapped, will attempt to access the memory and fail. This is particularly an issue on macOS when the host page size is 16KiB and the guest page size is 4KiB.

It should work. If I understand correctly, tcg doesn't care the host page size. hvf will not call hv_vm_map() for misaligned regions and that causes Data Abort, but it is handled by QEMU to perform memory access. I think it needs more debugging.

Regards,
Akihiko Odaki

Quoted 31 lines
---
  hw/display/virtio-gpu-virgl.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
hw/display/virtio-gpu-virgl.c +14 -0
index 20c856c04e..adf02ac22b 100644
@@ -116,6 +116,20 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
          return ret;
      }
 
+    if (!QEMU_IS_ALIGNED((uintptr_t)data, qemu_real_host_page_size())) {
+        virgl_renderer_resource_unmap(res->base.resource_id);
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: address %p is not aligned to page size\n",
+                      __func__, data);
+        return -ENOMEM;
+    }
+
+    if (!QEMU_IS_ALIGNED(size, qemu_real_host_page_size())) {
+        virgl_renderer_resource_unmap(res->base.resource_id);
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: size 0x%llx is not aligned to page size\n",
+                      __func__, size);
+        return -ENOMEM;
+    }
+
      vmr = g_new0(struct virtio_gpu_virgl_hostmem_region, 1);
      vmr->g = g;
 
#31 lore raw

Re: [PATCH RFC 5/7] virtio-gpu-virgl: support scanout of Metal textures

In reply to Joelle van Dyne
Quoted 102 lines · Joelle van Dyne

When supported, virglrenderer will return a MTLTexture handle that can be directly used for scanout.

---
  meson.build                   |  4 ++++
  include/ui/console.h          |  2 ++
  hw/display/virtio-gpu-virgl.c | 12 +++++++++++-
  hw/display/virtio-gpu.c       |  2 ++
  4 files changed, 19 insertions(+), 1 deletion(-)
meson.build +4 -0
index d9293294d8..05bad66376 100644
@@ -832,6 +832,7 @@ version_res = []
  coref = []
  iokit = []
  pvg = not_found
+metal = not_found
  emulator_link_args = []
  midl = not_found
  widl = not_found
@@ -859,6 +860,7 @@ elif host_os == 'darwin'
    host_dsosuf = '.dylib'
    pvg = dependency('appleframeworks', modules: ['ParavirtualizedGraphics', 'Metal'],
                     required: get_option('pvg'))
+  metal = dependency('appleframeworks', modules: 'Metal', required: false)
  elif host_os == 'sunos'
    socket = [cc.find_library('socket'),
              cc.find_library('nsl'),
@@ -2591,6 +2593,7 @@ if xen.found()
      ('0' + xen_version[2]).substring(-2)
    config_host_data.set('CONFIG_XEN_CTRL_INTERFACE_VERSION', xen_ctrl_version)
  endif
+config_host_data.set('CONFIG_METAL', metal.found())
  config_host_data.set('QEMU_VERSION', '"@0@"'.format(meson.project_version()))
  config_host_data.set('QEMU_VERSION_MAJOR', meson.project_version().split('.')[0])
  config_host_data.set('QEMU_VERSION_MINOR', meson.project_version().split('.')[1])
@@ -4874,6 +4877,7 @@ summary(summary_info, bool_yn: true, section: 'Crypto')
  summary_info = {}
  if host_os == 'darwin'
    summary_info += {'Cocoa support':           cocoa}
+  summary_info += {'Metal support':           metal}
  endif
  summary_info += {'D-Bus display':     dbus_display}
  summary_info += {'SDL support':       sdl}
include/ui/console.h +2 -0
index 25e45295d4..a45b524c57 100644
@@ -134,12 +134,14 @@ struct QemuConsoleClass {
  typedef enum ScanoutTextureNativeType {
      SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
      SCANOUT_TEXTURE_NATIVE_TYPE_D3D,
+    SCANOUT_TEXTURE_NATIVE_TYPE_METAL,
  } ScanoutTextureNativeType;
 
  typedef struct ScanoutTextureNative {
      ScanoutTextureNativeType type;
      union {
          void *d3d_tex2d;
+        void *metal_texture;
      } u;
  } ScanoutTextureNative;
 
hw/display/virtio-gpu-virgl.c +11 -1
index 36c670f988..e091eb0c76 100644
@@ -452,6 +452,13 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
  #if VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION >= SUPPORTED_VIRGL_INFO_EXT_VERSION
          if (ext.version >= VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION) {
              switch (ext.native_type) {
+#ifdef CONFIG_METAL
+            case VIRGL_NATIVE_HANDLE_METAL_TEXTURE: {
+                native.type = SCANOUT_TEXTURE_NATIVE_TYPE_METAL;
+                native.u.metal_texture = ext.native_handle;
+                break;
+            }
+#endif
              case VIRGL_NATIVE_HANDLE_NONE:
              case VIRGL_NATIVE_HANDLE_D3D_TEX2D: {
                  /* already handled above */
@@ -1198,7 +1205,10 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
      }
  #if VIRGL_VERSION_MAJOR >= 1
      if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
-        flags |= VIRGL_RENDERER_VENUS | VIRGL_RENDERER_RENDER_SERVER;
+        flags |= VIRGL_RENDERER_VENUS;
+#ifndef CONFIG_METAL /* Metal does not support render server */
+        flags |= VIRGL_RENDERER_RENDER_SERVER;
+#endif
      }
  #endif
 
hw/display/virtio-gpu.c +0 -0
index 43e88a4daf..9cf2c15a43 100644
@@ -1485,12 +1485,14 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
      VirtIOGPU *g = VIRTIO_GPU(qdev);

hw/display/virtio-gpu.c void virtio_gpu_device_realize(DeviceState *qdev, Error **errp) · line 1495

Quoted 10 lines
hw/display/virtio-gpu.c +2 -0
 
      if (virtio_gpu_blob_enabled(g->parent_obj.conf)) {
+#ifndef CONFIG_METAL
          if (!virtio_gpu_rutabaga_enabled(g->parent_obj.conf) &&
              !virtio_gpu_virgl_enabled(g->parent_obj.conf) &&
              !virtio_gpu_have_udmabuf()) {
              error_setg(errp, "need rutabaga or udmabuf for blob resources");
              return;
          }
+#endif

This check should be kept enabled. Blob resources cannot be created if none of Rutabaga, virgl or udmabuf is enabled.

Regards,
Akihiko Odaki

#15 lore raw

Re: [PATCH RFC 2/7] virtio-gpu-virgl: check page alignment of blob mapping

In reply to Akihiko Odaki

The problem specifically with how Mesa uses it is that virtgpu_ioctl_map calls the IOCTL DRM_IOCTL_VIRTGPU_MAP which performs the VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB with an unaligned offset. virtio_gpu_virgl_map_resource_blob does not error and hv_vm_map returns silently without doing any maps. Once that makes it to Mesa, it will try to mmap() and then use the region with vector instructions that are not supported by HVF because HVF does not emulate instructions when ISV=0 (this was a topic of discussion many times for other features like TPM emulation).

Quoted 47 lines · Akihiko Odaki
Quoted 4 lines · Joelle van Dyne

Currently if a mapping is not page aligned, it will sliently fail and the guest, assuming it is mapped, will attempt to access the memory and fail. This is particularly an issue on macOS when the host page size is 16KiB and the guest page size is 4KiB.

It should work. If I understand correctly, tcg doesn't care the host page size. hvf will not call hv_vm_map() for misaligned regions and that causes Data Abort, but it is handled by QEMU to perform memory access. I think it needs more debugging.

Regards,
Akihiko Odaki

Quoted 31 lines
---
  hw/display/virtio-gpu-virgl.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
hw/display/virtio-gpu-virgl.c +14 -0
index 20c856c04e..adf02ac22b 100644
@@ -116,6 +116,20 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
          return ret;
      }
 
+    if (!QEMU_IS_ALIGNED((uintptr_t)data, qemu_real_host_page_size())) {
+        virgl_renderer_resource_unmap(res->base.resource_id);
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: address %p is not aligned to page size\n",
+                      __func__, data);
+        return -ENOMEM;
+    }
+
+    if (!QEMU_IS_ALIGNED(size, qemu_real_host_page_size())) {
+        virgl_renderer_resource_unmap(res->base.resource_id);
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: size 0x%llx is not aligned to page size\n",
+                      __func__, size);
+        return -ENOMEM;
+    }
+
      vmr = g_new0(struct virtio_gpu_virgl_hostmem_region, 1);
      vmr->g = g;
 
#16 lore raw

Re: [PATCH RFC 2/7] virtio-gpu-virgl: check page alignment of blob mapping

Quoted 3 lines · Joelle van Dyne

The problem specifically with how Mesa uses it is that virtgpu_ioctl_map calls the IOCTL DRM_IOCTL_VIRTGPU_MAP which performs the VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB with an unaligned offset.

The kernel is the one who determines the offset, not Mesa. And I cannot blame the kernel either because the virtio specification says nothing about the alignment requirement.

Quoted 6 lines

virtio_gpu_virgl_map_resource_blob does not error and hv_vm_map returns silently without doing any maps. Once that makes it to Mesa, it will try to mmap() and then use the region with vector instructions that are not supported by HVF because HVF does not emulate instructions when ISV=0 (this was a topic of discussion many times for other features like TPM emulation).

This explains the situation well. Please add a comment with a similar explanation and change this to perform the check only if !IS_ENABLED(HOST_AARCH64) && tcg_enabled() (apparently kvm and whpx[1] have the same issue) so that it won't break other configurations.

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Regards,
Akihiko Odaki

Quoted 50 lines
Quoted 47 lines · Akihiko Odaki
Quoted 4 lines · Joelle van Dyne

Currently if a mapping is not page aligned, it will sliently fail and the guest, assuming it is mapped, will attempt to access the memory and fail. This is particularly an issue on macOS when the host page size is 16KiB and the guest page size is 4KiB.

It should work. If I understand correctly, tcg doesn't care the host page size. hvf will not call hv_vm_map() for misaligned regions and that causes Data Abort, but it is handled by QEMU to perform memory access. I think it needs more debugging.

Regards,
Akihiko Odaki

Quoted 31 lines
---
   hw/display/virtio-gpu-virgl.c | 14 ++++++++++++++
   1 file changed, 14 insertions(+)
hw/display/virtio-gpu-virgl.c +14 -0
index 20c856c04e..adf02ac22b 100644
@@ -116,6 +116,20 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
           return ret;
       }
 
+    if (!QEMU_IS_ALIGNED((uintptr_t)data, qemu_real_host_page_size())) {
+        virgl_renderer_resource_unmap(res->base.resource_id);
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: address %p is not aligned to page size\n",
+                      __func__, data);
+        return -ENOMEM;
+    }
+
+    if (!QEMU_IS_ALIGNED(size, qemu_real_host_page_size())) {
+        virgl_renderer_resource_unmap(res->base.resource_id);
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: size 0x%llx is not aligned to page size\n",
+                      __func__, size);
+        return -ENOMEM;
+    }
+
       vmr = g_new0(struct virtio_gpu_virgl_hostmem_region, 1);
       vmr->g = g;
 
#33 lore raw

Re: [PATCH RFC 6/7] console: add cleanup callback for ScanoutTexture

In reply to Joelle van Dyne
Quoted 23 lines · Joelle van Dyne

Before we introduce changes that allow for QemuConsole to take ownership of a texture handle, we need scaffolding that will allow us to callback into a cleanup function any time the ScanoutTexture becomes invalid, which is whenever the scanout.kind or scanout.texture gets updated.

The ordering is important: we need to first update the DisplayScanout, then we need to notify all the listeners, and once all the listeners have had the chance to finish using the previous native texture, we are safe to call the cleanup function. This means we need to hold on to the previous scanout native handle locally until all listeners are notified.

---
  include/ui/console.h          |  9 +++++-
  hw/display/virtio-gpu-virgl.c |  2 +-
  ui/console.c                  | 56 +++++++++++++++++++++++++++++++----
  3 files changed, 59 insertions(+), 8 deletions(-)
include/ui/console.h +0 -0
index a45b524c57..df9c083a16 100644

include/ui/console.h typedef struct ScanoutTextureNative { · line 155

Quoted 7 lines
include/ui/console.h +4 -0
@@ -149,6 +149,11 @@ typedef struct ScanoutTextureNative {
      .type = SCANOUT_TEXTURE_NATIVE_TYPE_NONE \
  })
 
+/**
+ * Cleanup callback function when ScanoutTexture is about to be destroyed
+ */
+typedef void (*dpy_cleanup_texture)(ScanoutTextureNative *native);

Please name this typedef CamelCase as like others.

Quoted 40 lines
include/ui/console.h +4 -1
+
  typedef struct ScanoutTexture {
      uint32_t backing_id;
      bool backing_y_0_top;
@@ -159,6 +164,7 @@ typedef struct ScanoutTexture {
      uint32_t width;
      uint32_t height;
      ScanoutTextureNative native;
+    dpy_cleanup_texture cb_cleanup;
  } ScanoutTexture;
 
  typedef struct QemuUIInfo {
@@ -347,7 +353,8 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                              uint32_t backing_id, bool backing_y_0_top,
                              uint32_t backing_width, uint32_t backing_height,
                              uint32_t x, uint32_t y, uint32_t w, uint32_t h,
-                            ScanoutTextureNative native);
+                            ScanoutTextureNative native,
+                            dpy_cleanup_texture cb_cleanup);
  void dpy_gl_scanout_dmabuf(QemuConsole *con,
                             QemuDmaBuf *dmabuf);
  void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
hw/display/virtio-gpu-virgl.c +1 -1
index e091eb0c76..b7bc095676 100644
@@ -492,7 +492,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
              info.flags & VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP,
              info.width, info.height,
              ss.r.x, ss.r.y, ss.r.width, ss.r.height,
-            native);
+            native, NULL);
      } else {
          dpy_gfx_replace_surface(
              g->parent_obj.scanout[ss.scanout_id].con, NULL);
ui/console.c +0 -0
index 9378afd53d..8271c36586 100644

ui/console.c void dpy_gfx_update_full(QemuConsole *con) · line 814

Quoted 7 lines
ui/console.c +4 -0
@@ -808,6 +808,41 @@ void dpy_gfx_update_full(QemuConsole *con)
      dpy_gfx_update(con, 0, 0, w, h);
  }
 
+struct scanout_change {
+    ScanoutTextureNative native;
+    dpy_cleanup_texture cb_cleanup;
+};

Struct should have typedef according to: docs/devel/style.rst

Regards,
Akihiko Odaki

Quoted 127 lines
ui/console.c +46 -6
+
+#define SCANOUT_CHANGE_NONE ((struct scanout_change){ NO_NATIVE_TEXTURE })
+
+static struct scanout_change dpy_change_scanout_kind(DisplayScanout *scanout,
+                                                     enum display_scanout kind)
+{
+    struct scanout_change change = SCANOUT_CHANGE_NONE;
+
+    /**
+     * We cannot cleanup until the resource is no longer in use, so we record it
+     * You MUST call dpy_complete_scanout_change after all listeners are updated
+     */
+    if (scanout->kind == SCANOUT_TEXTURE && scanout->texture.cb_cleanup) {
+        change.native = scanout->texture.native;
+        change.cb_cleanup = scanout->texture.cb_cleanup;
+    }
+    scanout->kind = kind;
+
+    return change;
+}
+
+static void dpy_complete_scanout_change(struct scanout_change *change)
+{
+    /**
+     * If we previously have a texture and cleanup is required, we call it now
+     */
+    if (change->native.type != SCANOUT_TEXTURE_NATIVE_TYPE_NONE && change->cb_cleanup) {
+        change->cb_cleanup(&change->native);
+    }
+}
+
  void dpy_gfx_replace_surface(QemuConsole *con,
                               DisplaySurface *surface)
  {
@@ -818,6 +853,7 @@ void dpy_gfx_replace_surface(QemuConsole *con,
      DisplayChangeListener *dcl;
      int width;
      int height;
+    struct scanout_change change = SCANOUT_CHANGE_NONE;
 
      if (!surface) {
          if (old_surface) {
@@ -833,7 +869,7 @@ void dpy_gfx_replace_surface(QemuConsole *con,
 
      assert(old_surface != new_surface);
 
-    con->scanout.kind = SCANOUT_SURFACE;
+    change = dpy_change_scanout_kind(&con->scanout, SCANOUT_SURFACE);
      con->surface = new_surface;
      dpy_gfx_create_texture(con, new_surface);
      QLIST_FOREACH(dcl, &s->listeners, next) {
@@ -844,6 +880,7 @@ void dpy_gfx_replace_surface(QemuConsole *con,
      }
      dpy_gfx_destroy_texture(con, old_surface);
      qemu_free_displaysurface(old_surface);
+    dpy_complete_scanout_change(&change);
  }
 
  bool dpy_gfx_check_format(QemuConsole *con,
@@ -1002,9 +1039,10 @@ void dpy_gl_scanout_disable(QemuConsole *con)
  {
      DisplayState *s = con->ds;
      DisplayChangeListener *dcl;
+    struct scanout_change change = SCANOUT_CHANGE_NONE;
 
      if (con->scanout.kind != SCANOUT_SURFACE) {
-        con->scanout.kind = SCANOUT_NONE;
+        change = dpy_change_scanout_kind(&con->scanout, SCANOUT_NONE);
      }
      QLIST_FOREACH(dcl, &s->listeners, next) {
          if (con != dcl->con) {
@@ -1014,6 +1052,7 @@ void dpy_gl_scanout_disable(QemuConsole *con)
              dcl->ops->dpy_gl_scanout_disable(dcl);
          }
      }
+    dpy_complete_scanout_change(&change);
  }
 
  void dpy_gl_scanout_texture(QemuConsole *con,
@@ -1023,15 +1062,17 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                              uint32_t backing_height,
                              uint32_t x, uint32_t y,
                              uint32_t width, uint32_t height,
-                            ScanoutTextureNative native)
+                            ScanoutTextureNative native,
+                            dpy_cleanup_texture cb_cleanup)
  {
      DisplayState *s = con->ds;
      DisplayChangeListener *dcl;
+    struct scanout_change change = SCANOUT_CHANGE_NONE;
 
-    con->scanout.kind = SCANOUT_TEXTURE;
+    change = dpy_change_scanout_kind(&con->scanout, SCANOUT_TEXTURE);
      con->scanout.texture = (ScanoutTexture) {
          backing_id, backing_y_0_top, backing_width, backing_height,
-        x, y, width, height, native,
+        x, y, width, height, native, cb_cleanup
      };
      QLIST_FOREACH(dcl, &s->listeners, next) {
          if (con != dcl->con) {
@@ -1045,6 +1086,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                                               native);
          }
      }
+    dpy_complete_scanout_change(&change);
  }
 
  void dpy_gl_scanout_dmabuf(QemuConsole *con,
@@ -1052,8 +1094,9 @@ void dpy_gl_scanout_dmabuf(QemuConsole *con,
  {
      DisplayState *s = con->ds;
      DisplayChangeListener *dcl;
+    struct scanout_change change = SCANOUT_CHANGE_NONE;
 
-    con->scanout.kind = SCANOUT_DMABUF;
+    change = dpy_change_scanout_kind(&con->scanout, SCANOUT_DMABUF);
      con->scanout.dmabuf = dmabuf;
      QLIST_FOREACH(dcl, &s->listeners, next) {
          if (con != dcl->con) {
@@ -1063,6 +1106,7 @@ void dpy_gl_scanout_dmabuf(QemuConsole *con,
              dcl->ops->dpy_gl_scanout_dmabuf(dcl, dmabuf);
          }
      }
+    dpy_complete_scanout_change(&change);
  }
 
  void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
#2 lore raw

Re: [PATCH RFC 1/7] egl-helpers: store handle to native device

In reply to Joelle van Dyne
Quoted 16 lines · Joelle van Dyne

Make way for other platforms by making the variable more general. Also we will be using the device in the future so let's save the pointer in the global instead of just a boolean flag.

---
  include/ui/egl-helpers.h      | 2 +-
  hw/display/virtio-gpu-virgl.c | 2 +-
  ui/egl-helpers.c              | 4 ++--
  3 files changed, 4 insertions(+), 4 deletions(-)
include/ui/egl-helpers.h +0 -0
index acf993fcf5..c239d32317 100644

include/ui/egl-helpers.h line 15

Quoted 5 lines
include/ui/egl-helpers.h +1 -1
@@ -12,7 +12,7 @@
  extern EGLDisplay *qemu_egl_display;
  extern EGLConfig qemu_egl_config;
  extern DisplayGLMode qemu_egl_mode;
-extern bool qemu_egl_angle_d3d;
+extern void *qemu_egl_angle_native_device;

I guess ANGLE will not be relevant for Metal and leaving it will be misleading.

Regards,
Akihiko Odaki

Quoted 38 lines
include/ui/egl-helpers.h +0 -0
  typedef struct egl_fb {
      int width;
hw/display/virtio-gpu-virgl.c +1 -1
index 07f6355ad6..20c856c04e 100644
@@ -1152,7 +1152,7 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
      }
  #endif
  #ifdef VIRGL_RENDERER_D3D11_SHARE_TEXTURE
-    if (qemu_egl_angle_d3d) {
+    if (qemu_egl_angle_native_device) {
          flags |= VIRGL_RENDERER_D3D11_SHARE_TEXTURE;
      }
  #endif
ui/egl-helpers.c +2 -2
index e3f2872cc1..57bcf99498 100644
@@ -28,7 +28,7 @@
  EGLDisplay *qemu_egl_display;
  EGLConfig qemu_egl_config;
  DisplayGLMode qemu_egl_mode;
-bool qemu_egl_angle_d3d;
+void *qemu_egl_angle_native_device;
 
  /* ------------------------------------------------------------------ */
 
@@ -651,7 +651,7 @@ int qemu_egl_init_dpy_win32(EGLNativeDisplayType dpy, DisplayGLMode mode)
          }
 
          trace_egl_init_d3d11_device(device);
-        qemu_egl_angle_d3d = device != NULL;
+        qemu_egl_angle_native_device = d3d11_device;
      }
  #endif
 
#19 lore raw

Re: [PATCH RFC 3/7] console: rename `d3d_tex2d` to `native`

In reply to Joelle van Dyne
Quoted 27 lines · Joelle van Dyne

In order to support native texture scanout beyond D3D, we make this more generic allowing for multiple native texture handle types.

---
  include/ui/console.h          | 22 +++++++++++++++++++---
  include/ui/gtk.h              |  4 ++--
  include/ui/sdl2.h             |  2 +-
  hw/display/virtio-gpu-virgl.c | 10 +++++++---
  ui/console.c                  |  8 ++++----
  ui/dbus-console.c             |  2 +-
  ui/dbus-listener.c            |  8 ++++----
  ui/egl-headless.c             |  2 +-
  ui/gtk-egl.c                  |  2 +-
  ui/gtk-gl-area.c              |  2 +-
  ui/sdl2-gl.c                  |  2 +-
  ui/spice-display.c            |  2 +-
  12 files changed, 43 insertions(+), 23 deletions(-)
include/ui/console.h +0 -0
index 98feaa58bd..25e45295d4 100644
@@ -131,6 +131,22 @@ struct QemuConsoleClass {
      ObjectClass parent_class;
  };
 

include/ui/console.h struct QemuConsoleClass { · line 143

Quoted 10 lines
include/ui/console.h +10 -0
+typedef enum ScanoutTextureNativeType {
+    SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
+    SCANOUT_TEXTURE_NATIVE_TYPE_D3D,
+} ScanoutTextureNativeType;
+
+typedef struct ScanoutTextureNative {
+    ScanoutTextureNativeType type;
+    union {
+        void *d3d_tex2d;
+    } u;> +} ScanoutTextureNative;

Instead, I suggest:

typedef struct ScanoutTextureNative {
    ScanoutTextureNativeType type;
    void *handle;
} ScanoutTextureNative;

...to align with the definition of struct virgl_renderer_resource_info_ext and virgl_renderer_create_handle_for_scanout(), which do not add a field for each type (except the one for the backward compatibility).

Regards,
Akihiko Odaki

Quoted 171 lines
include/ui/console.h +8 -3
+
+#define NO_NATIVE_TEXTURE ((ScanoutTextureNative){ \
+    .type = SCANOUT_TEXTURE_NATIVE_TYPE_NONE \
+})
+
  typedef struct ScanoutTexture {
      uint32_t backing_id;
      bool backing_y_0_top;
@@ -140,7 +156,7 @@ typedef struct ScanoutTexture {
      uint32_t y;
      uint32_t width;
      uint32_t height;
-    void *d3d_tex2d;
+    ScanoutTextureNative native;
  } ScanoutTexture;
 
  typedef struct QemuUIInfo {
@@ -245,7 +261,7 @@ typedef struct DisplayChangeListenerOps {
                                     uint32_t backing_height,
                                     uint32_t x, uint32_t y,
                                     uint32_t w, uint32_t h,
-                                   void *d3d_tex2d);
+                                   ScanoutTextureNative native);
      /* optional (default to true if has dpy_gl_scanout_dmabuf) */
      bool (*dpy_has_dmabuf)(DisplayChangeListener *dcl);
      /* optional */
@@ -329,7 +345,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                              uint32_t backing_id, bool backing_y_0_top,
                              uint32_t backing_width, uint32_t backing_height,
                              uint32_t x, uint32_t y, uint32_t w, uint32_t h,
-                            void *d3d_tex2d);
+                            ScanoutTextureNative native);
  void dpy_gl_scanout_dmabuf(QemuConsole *con,
                             QemuDmaBuf *dmabuf);
  void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
include/ui/gtk.h +2 -2
index 3e6ce3cb48..f88e2b281d 100644
@@ -178,7 +178,7 @@ void gd_egl_scanout_texture(DisplayChangeListener *dcl,
                              uint32_t backing_height,
                              uint32_t x, uint32_t y,
                              uint32_t w, uint32_t h,
-                            void *d3d_tex2d);
+                            ScanoutTextureNative native);
  void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
                             QemuDmaBuf *dmabuf);
  void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
@@ -215,7 +215,7 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                  uint32_t backing_height,
                                  uint32_t x, uint32_t y,
                                  uint32_t w, uint32_t h,
-                                void *d3d_tex2d);
+                                ScanoutTextureNative native);
  void gd_gl_area_scanout_disable(DisplayChangeListener *dcl);
  void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
                                uint32_t x, uint32_t y, uint32_t w, uint32_t h);
include/ui/sdl2.h +1 -1
index dbe6e3d973..fdefb88229 100644
@@ -93,7 +93,7 @@ void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
                               uint32_t backing_height,
                               uint32_t x, uint32_t y,
                               uint32_t w, uint32_t h,
-                             void *d3d_tex2d);
+                             ScanoutTextureNative native);
  void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
                             uint32_t x, uint32_t y, uint32_t w, uint32_t h);
 
hw/display/virtio-gpu-virgl.c +7 -3
index adf02ac22b..d0e6ad4b17 100644
@@ -434,14 +434,18 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
 
      if (ss.resource_id && ss.r.width && ss.r.height) {
          struct virgl_renderer_resource_info info;
-        void *d3d_tex2d = NULL;
+        ScanoutTextureNative native = { .type = SCANOUT_TEXTURE_NATIVE_TYPE_NONE };
 
  #if VIRGL_VERSION_MAJOR >= 1
          struct virgl_renderer_resource_info_ext ext;
          memset(&ext, 0, sizeof(ext));
          ret = virgl_renderer_resource_get_info_ext(ss.resource_id, &ext);
          info = ext.base;
-        d3d_tex2d = ext.d3d_tex2d;
+        native = (ScanoutTextureNative){
+            .type = ext.d3d_tex2d ? SCANOUT_TEXTURE_NATIVE_TYPE_D3D :
+                                    SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
+            .u.d3d_tex2d = ext.d3d_tex2d,
+        };
  #else
          memset(&info, 0, sizeof(info));
          ret = virgl_renderer_resource_get_info(ss.resource_id, &info);
@@ -461,7 +465,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
              info.flags & VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP,
              info.width, info.height,
              ss.r.x, ss.r.y, ss.r.width, ss.r.height,
-            d3d_tex2d);
+            native);
      } else {
          dpy_gfx_replace_surface(
              g->parent_obj.scanout[ss.scanout_id].con, NULL);
ui/console.c +4 -4
index 2d00828c53..9378afd53d 100644
@@ -295,7 +295,7 @@ static void displaychangelistener_display_console(DisplayChangeListener *dcl,
                                           con->scanout.texture.y,
                                           con->scanout.texture.width,
                                           con->scanout.texture.height,
-                                         con->scanout.texture.d3d_tex2d);
+                                         con->scanout.texture.native);
      }
  }
 
@@ -1023,7 +1023,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                              uint32_t backing_height,
                              uint32_t x, uint32_t y,
                              uint32_t width, uint32_t height,
-                            void *d3d_tex2d)
+                            ScanoutTextureNative native)
  {
      DisplayState *s = con->ds;
      DisplayChangeListener *dcl;
@@ -1031,7 +1031,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
      con->scanout.kind = SCANOUT_TEXTURE;
      con->scanout.texture = (ScanoutTexture) {
          backing_id, backing_y_0_top, backing_width, backing_height,
-        x, y, width, height, d3d_tex2d,
+        x, y, width, height, native,
      };
      QLIST_FOREACH(dcl, &s->listeners, next) {
          if (con != dcl->con) {
@@ -1042,7 +1042,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                                               backing_y_0_top,
                                               backing_width, backing_height,
                                               x, y, width, height,
-                                             d3d_tex2d);
+                                             native);
          }
      }
  }
ui/dbus-console.c +1 -1
index 85e215ef23..651f0daeaf 100644
@@ -99,7 +99,7 @@ dbus_gl_scanout_texture(DisplayChangeListener *dcl,
                          uint32_t backing_height,
                          uint32_t x, uint32_t y,
                          uint32_t w, uint32_t h,
-                        void *d3d_tex2d)
+                        ScanoutTextureNative native)
  {
      DBusDisplayConsole *ddc = container_of(dcl, DBusDisplayConsole, dcl);
 
ui/dbus-listener.c +1 -1
index 52e041edb0..a737752996 100644
@@ -587,7 +587,7 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
                                   uint32_t backing_height,
                                   uint32_t x, uint32_t y,
                                   uint32_t w, uint32_t h,
-                                 void *d3d_tex2d)
+                                 ScanoutTextureNative native)
  {
      trace_dbus_scanout_texture(tex_id, backing_y_0_top,
                                 backing_width, backing_height, x, y, w, h);

ui/dbus-listener.c static void dbus_scanout_texture(DisplayChangeListener *dcl, · line 621

Quoted 6 lines
ui/dbus-listener.c +1 -2
@@ -618,8 +618,8 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
      assert(surface_width(ddl->ds) == w);
      assert(surface_height(ddl->ds) == h);
 
-    if (d3d_tex2d) {
-        dbus_scanout_share_d3d_texture(ddl, d3d_tex2d, backing_y_0_top,
+    if (native.type == SCANOUT_TEXTURE_NATIVE_TYPE_D3d) {

s/SCANOUT_TEXTURE_NATIVE_TYPE_D3d/SCANOUT_TEXTURE_NATIVE_TYPE_D3D/

Regards,
Akihiko Odaki

Quoted 78 lines
ui/dbus-listener.c +2 -1
+        dbus_scanout_share_d3d_texture(ddl, native.u.d3d_tex2d, backing_y_0_top,
                                         backing_width, backing_height, x, y, w, h);
      } else {
          dbus_scanout_map(ddl);
@@ -868,7 +868,7 @@ static void dbus_gl_gfx_switch(DisplayChangeListener *dcl,
 
          /* TODO: lazy send dmabuf (there are unnecessary sent otherwise) */
          dbus_scanout_texture(&ddl->dcl, ddl->ds->texture, false,
-                             width, height, 0, 0, width, height, NULL);
+                             width, height, 0, 0, width, height, NO_NATIVE_TEXTURE);
      }
  }
  #endif
ui/egl-headless.c +1 -1
index 1f6b845500..e36778bd48 100644
@@ -62,7 +62,7 @@ static void egl_scanout_texture(DisplayChangeListener *dcl,
                                  uint32_t backing_height,
                                  uint32_t x, uint32_t y,
                                  uint32_t w, uint32_t h,
-                                void *d3d_tex2d)
+                                ScanoutTextureNative native)
  {
      egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
ui/gtk-egl.c +1 -1
index ae9239999c..7933237d45 100644
@@ -239,7 +239,7 @@ void gd_egl_scanout_texture(DisplayChangeListener *dcl,
                              uint32_t backing_width, uint32_t backing_height,
                              uint32_t x, uint32_t y,
                              uint32_t w, uint32_t h,
-                            void *d3d_tex2d)
+                            ScanoutTextureNative native)
  {
      VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 
ui/gtk-gl-area.c +1 -1
index cd86022d26..f2ed543d6a 100644
@@ -306,7 +306,7 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                  uint32_t backing_height,
                                  uint32_t x, uint32_t y,
                                  uint32_t w, uint32_t h,
-                                void *d3d_tex2d)
+                                ScanoutTextureNative native)
  {
      VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 
ui/sdl2-gl.c +1 -1
index 3be17d1079..b15db86f24 100644
@@ -206,7 +206,7 @@ void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
                               uint32_t backing_height,
                               uint32_t x, uint32_t y,
                               uint32_t w, uint32_t h,
-                             void *d3d_tex2d)
+                             ScanoutTextureNative native)
  {
      struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
ui/spice-display.c +1 -1
index db71e866f8..2a2285cb1f 100644
@@ -1084,7 +1084,7 @@ static void qemu_spice_gl_scanout_texture(DisplayChangeListener *dcl,
                                            uint32_t backing_height,
                                            uint32_t x, uint32_t y,
                                            uint32_t w, uint32_t h,
-                                          void *d3d_tex2d)
+                                          ScanoutTextureNative native)
  {
      SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
      EGLint offset[DMABUF_MAX_PLANES], stride[DMABUF_MAX_PLANES], fourcc = 0;
#26 lore raw

Re: [PATCH RFC 4/7] virtio-gpu-virgl: update virglrenderer defines

In reply to Joelle van Dyne
Quoted 14 lines · Joelle van Dyne

In order to support additional native texture types, we need to update the defines in virglrenderer. The changes are backwards compatible and so builds should work with either the new version or the old version.

---
  hw/display/virtio-gpu-virgl.c | 28 +++++++++++++++++++++++++---
  1 file changed, 25 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-virgl.c +0 -0
index d0e6ad4b17..36c670f988 100644

hw/display/virtio-gpu-virgl.c line 27

Quoted 4 lines
hw/display/virtio-gpu-virgl.c +1 -0
@@ -24,6 +24,8 @@
 
  #include <virglrenderer.h>
 
+#define SUPPORTED_VIRGL_INFO_EXT_VERSION (1)

This naming is a bit confusing because version 0 is also supported.

Quoted 17 lines
hw/display/virtio-gpu-virgl.c +4 -1
+
  struct virtio_gpu_virgl_resource {
      struct virtio_gpu_simple_resource base;
      MemoryRegion *mr;
@@ -441,12 +443,30 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
          memset(&ext, 0, sizeof(ext));
          ret = virgl_renderer_resource_get_info_ext(ss.resource_id, &ext);
          info = ext.base;
+        /* fallback to older version */
          native = (ScanoutTextureNative){
              .type = ext.d3d_tex2d ? SCANOUT_TEXTURE_NATIVE_TYPE_D3D :
                                      SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
              .u.d3d_tex2d = ext.d3d_tex2d,
          };
-#else
+#if VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION >= SUPPORTED_VIRGL_INFO_EXT_VERSION
+        if (ext.version >= VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION) {

hw/display/virtio-gpu-virgl.c static void virgl_cmd_set_scanout(VirtIOGPU *g, · line 463

Quoted 10 lines
hw/display/virtio-gpu-virgl.c +10 -0
+            switch (ext.native_type) {
+            case VIRGL_NATIVE_HANDLE_NONE:
+            case VIRGL_NATIVE_HANDLE_D3D_TEX2D: {
+                /* already handled above */
+                break;
+            }
+            default: {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                            "%s: unsupported native texture type %d\n",
+                            __func__, ext.native_type);

It is not an error condition; the d3d_tex2d field is an optional hint that allows zero-copy inter-process sharing and the tex_id field is used if it is missing or inter-process sharing is unnecessary. It should be fine to dismiss the native handle unless the semantic changes.

Regards,
Akihiko Odaki

Quoted 25 lines
hw/display/virtio-gpu-virgl.c +10 -2
+                break;
+            }
+            }
+        }
+#endif
+#else /* VIRGL_VERSION_MAJOR < 1 */
          memset(&info, 0, sizeof(info));
          ret = virgl_renderer_resource_get_info(ss.resource_id, &info);
  #endif
@@ -1169,11 +1189,13 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
          virtio_gpu_3d_cbs.get_egl_display = virgl_get_egl_display;
      }
  #endif
-#ifdef VIRGL_RENDERER_D3D11_SHARE_TEXTURE
      if (qemu_egl_angle_native_device) {
+#if defined(VIRGL_RENDERER_NATIVE_SHARE_TEXTURE)
+        flags |= VIRGL_RENDERER_NATIVE_SHARE_TEXTURE;
+#elif defined(VIRGL_RENDERER_D3D11_SHARE_TEXTURE) && defined(WIN32)
          flags |= VIRGL_RENDERER_D3D11_SHARE_TEXTURE;
-    }
  #endif
+    }
  #if VIRGL_VERSION_MAJOR >= 1
      if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
          flags |= VIRGL_RENDERER_VENUS | VIRGL_RENDERER_RENDER_SERVER;
#24 lore raw

Re: [PATCH RFC 3/7] console: rename `d3d_tex2d` to `native`

In reply to Joelle van Dyne

Hi

Quoted 5 lines · Joelle van Dyne

In order to support native texture scanout beyond D3D, we make this more generic allowing for multiple native texture handle types.

../ui/egl-headless.c:100:40: error: incompatible type for argument 10 of ‘egl_scanout_texture’
../ui/gtk-gl-area.c:377:53: error: incompatible type for argument 10 of ‘gd_gl_area_scanout_texture’
../ui/gtk-egl.c:293:49: error: incompatible type for argument 10 of ‘gd_egl_scanout_texture’

all like:
note: expected ‘ScanoutTextureNative’ but argument is of type ‘void *’

Quoted 293 lines
---
 include/ui/console.h          | 22 +++++++++++++++++++---
 include/ui/gtk.h              |  4 ++--
 include/ui/sdl2.h             |  2 +-
 hw/display/virtio-gpu-virgl.c | 10 +++++++---
 ui/console.c                  |  8 ++++----
 ui/dbus-console.c             |  2 +-
 ui/dbus-listener.c            |  8 ++++----
 ui/egl-headless.c             |  2 +-
 ui/gtk-egl.c                  |  2 +-
 ui/gtk-gl-area.c              |  2 +-
 ui/sdl2-gl.c                  |  2 +-
 ui/spice-display.c            |  2 +-
 12 files changed, 43 insertions(+), 23 deletions(-)
include/ui/console.h +19 -3
index 98feaa58bd..25e45295d4 100644
@@ -131,6 +131,22 @@ struct QemuConsoleClass {
     ObjectClass parent_class;
 };
 
+typedef enum ScanoutTextureNativeType {
+    SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
+    SCANOUT_TEXTURE_NATIVE_TYPE_D3D,
+} ScanoutTextureNativeType;
+
+typedef struct ScanoutTextureNative {
+    ScanoutTextureNativeType type;
+    union {
+        void *d3d_tex2d;
+    } u;
+} ScanoutTextureNative;
+
+#define NO_NATIVE_TEXTURE ((ScanoutTextureNative){ \
+    .type = SCANOUT_TEXTURE_NATIVE_TYPE_NONE \
+})
+
 typedef struct ScanoutTexture {
     uint32_t backing_id;
     bool backing_y_0_top;
@@ -140,7 +156,7 @@ typedef struct ScanoutTexture {
     uint32_t y;
     uint32_t width;
     uint32_t height;
-    void *d3d_tex2d;
+    ScanoutTextureNative native;
 } ScanoutTexture;
 
 typedef struct QemuUIInfo {
@@ -245,7 +261,7 @@ typedef struct DisplayChangeListenerOps {
                                    uint32_t backing_height,
                                    uint32_t x, uint32_t y,
                                    uint32_t w, uint32_t h,
-                                   void *d3d_tex2d);
+                                   ScanoutTextureNative native);
     /* optional (default to true if has dpy_gl_scanout_dmabuf) */
     bool (*dpy_has_dmabuf)(DisplayChangeListener *dcl);
     /* optional */
@@ -329,7 +345,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                             uint32_t backing_id, bool backing_y_0_top,
                             uint32_t backing_width, uint32_t backing_height,
                             uint32_t x, uint32_t y, uint32_t w, uint32_t h,
-                            void *d3d_tex2d);
+                            ScanoutTextureNative native);
 void dpy_gl_scanout_dmabuf(QemuConsole *con,
                            QemuDmaBuf *dmabuf);
 void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
include/ui/gtk.h +2 -2
index 3e6ce3cb48..f88e2b281d 100644
@@ -178,7 +178,7 @@ void gd_egl_scanout_texture(DisplayChangeListener *dcl,
                             uint32_t backing_height,
                             uint32_t x, uint32_t y,
                             uint32_t w, uint32_t h,
-                            void *d3d_tex2d);
+                            ScanoutTextureNative native);
 void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
                            QemuDmaBuf *dmabuf);
 void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
@@ -215,7 +215,7 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                 uint32_t backing_height,
                                 uint32_t x, uint32_t y,
                                 uint32_t w, uint32_t h,
-                                void *d3d_tex2d);
+                                ScanoutTextureNative native);
 void gd_gl_area_scanout_disable(DisplayChangeListener *dcl);
 void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
                               uint32_t x, uint32_t y, uint32_t w, uint32_t h);
include/ui/sdl2.h +1 -1
index dbe6e3d973..fdefb88229 100644
@@ -93,7 +93,7 @@ void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
                              uint32_t backing_height,
                              uint32_t x, uint32_t y,
                              uint32_t w, uint32_t h,
-                             void *d3d_tex2d);
+                             ScanoutTextureNative native);
 void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
                            uint32_t x, uint32_t y, uint32_t w, uint32_t h);
 
hw/display/virtio-gpu-virgl.c +7 -3
index adf02ac22b..d0e6ad4b17 100644
@@ -434,14 +434,18 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
 
     if (ss.resource_id && ss.r.width && ss.r.height) {
         struct virgl_renderer_resource_info info;
-        void *d3d_tex2d = NULL;
+        ScanoutTextureNative native = { .type = SCANOUT_TEXTURE_NATIVE_TYPE_NONE };
 
 #if VIRGL_VERSION_MAJOR >= 1
         struct virgl_renderer_resource_info_ext ext;
         memset(&ext, 0, sizeof(ext));
         ret = virgl_renderer_resource_get_info_ext(ss.resource_id, &ext);
         info = ext.base;
-        d3d_tex2d = ext.d3d_tex2d;
+        native = (ScanoutTextureNative){
+            .type = ext.d3d_tex2d ? SCANOUT_TEXTURE_NATIVE_TYPE_D3D :
+                                    SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
+            .u.d3d_tex2d = ext.d3d_tex2d,
+        };
 #else
         memset(&info, 0, sizeof(info));
         ret = virgl_renderer_resource_get_info(ss.resource_id, &info);
@@ -461,7 +465,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
             info.flags & VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP,
             info.width, info.height,
             ss.r.x, ss.r.y, ss.r.width, ss.r.height,
-            d3d_tex2d);
+            native);
     } else {
         dpy_gfx_replace_surface(
             g->parent_obj.scanout[ss.scanout_id].con, NULL);
ui/console.c +4 -4
index 2d00828c53..9378afd53d 100644
@@ -295,7 +295,7 @@ static void displaychangelistener_display_console(DisplayChangeListener *dcl,
                                          con->scanout.texture.y,
                                          con->scanout.texture.width,
                                          con->scanout.texture.height,
-                                         con->scanout.texture.d3d_tex2d);
+                                         con->scanout.texture.native);
     }
 }
 
@@ -1023,7 +1023,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                             uint32_t backing_height,
                             uint32_t x, uint32_t y,
                             uint32_t width, uint32_t height,
-                            void *d3d_tex2d)
+                            ScanoutTextureNative native)
 {
     DisplayState *s = con->ds;
     DisplayChangeListener *dcl;
@@ -1031,7 +1031,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
     con->scanout.kind = SCANOUT_TEXTURE;
     con->scanout.texture = (ScanoutTexture) {
         backing_id, backing_y_0_top, backing_width, backing_height,
-        x, y, width, height, d3d_tex2d,
+        x, y, width, height, native,
     };
     QLIST_FOREACH(dcl, &s->listeners, next) {
         if (con != dcl->con) {
@@ -1042,7 +1042,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                                              backing_y_0_top,
                                              backing_width, backing_height,
                                              x, y, width, height,
-                                             d3d_tex2d);
+                                             native);
         }
     }
 }
ui/dbus-console.c +1 -1
index 85e215ef23..651f0daeaf 100644
@@ -99,7 +99,7 @@ dbus_gl_scanout_texture(DisplayChangeListener *dcl,
                         uint32_t backing_height,
                         uint32_t x, uint32_t y,
                         uint32_t w, uint32_t h,
-                        void *d3d_tex2d)
+                        ScanoutTextureNative native)
 {
     DBusDisplayConsole *ddc = container_of(dcl, DBusDisplayConsole, dcl);
 
ui/dbus-listener.c +4 -4
index 52e041edb0..a737752996 100644
@@ -587,7 +587,7 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
                                  uint32_t backing_height,
                                  uint32_t x, uint32_t y,
                                  uint32_t w, uint32_t h,
-                                 void *d3d_tex2d)
+                                 ScanoutTextureNative native)
 {
     trace_dbus_scanout_texture(tex_id, backing_y_0_top,
                                backing_width, backing_height, x, y, w, h);
@@ -618,8 +618,8 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
     assert(surface_width(ddl->ds) == w);
     assert(surface_height(ddl->ds) == h);
 
-    if (d3d_tex2d) {
-        dbus_scanout_share_d3d_texture(ddl, d3d_tex2d, backing_y_0_top,
+    if (native.type == SCANOUT_TEXTURE_NATIVE_TYPE_D3d) {
+        dbus_scanout_share_d3d_texture(ddl, native.u.d3d_tex2d, backing_y_0_top,
                                        backing_width, backing_height, x, y, w, h);
     } else {
         dbus_scanout_map(ddl);
@@ -868,7 +868,7 @@ static void dbus_gl_gfx_switch(DisplayChangeListener *dcl,
 
         /* TODO: lazy send dmabuf (there are unnecessary sent otherwise) */
         dbus_scanout_texture(&ddl->dcl, ddl->ds->texture, false,
-                             width, height, 0, 0, width, height, NULL);
+                             width, height, 0, 0, width, height, NO_NATIVE_TEXTURE);
     }
 }
 #endif
ui/egl-headless.c +1 -1
index 1f6b845500..e36778bd48 100644
@@ -62,7 +62,7 @@ static void egl_scanout_texture(DisplayChangeListener *dcl,
                                 uint32_t backing_height,
                                 uint32_t x, uint32_t y,
                                 uint32_t w, uint32_t h,
-                                void *d3d_tex2d)
+                                ScanoutTextureNative native)
 {
     egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
ui/gtk-egl.c +1 -1
index ae9239999c..7933237d45 100644
@@ -239,7 +239,7 @@ void gd_egl_scanout_texture(DisplayChangeListener *dcl,
                             uint32_t backing_width, uint32_t backing_height,
                             uint32_t x, uint32_t y,
                             uint32_t w, uint32_t h,
-                            void *d3d_tex2d)
+                            ScanoutTextureNative native)
 {
     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 
ui/gtk-gl-area.c +1 -1
index cd86022d26..f2ed543d6a 100644
@@ -306,7 +306,7 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                 uint32_t backing_height,
                                 uint32_t x, uint32_t y,
                                 uint32_t w, uint32_t h,
-                                void *d3d_tex2d)
+                                ScanoutTextureNative native)
 {
     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 
ui/sdl2-gl.c +1 -1
index 3be17d1079..b15db86f24 100644
@@ -206,7 +206,7 @@ void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
                              uint32_t backing_height,
                              uint32_t x, uint32_t y,
                              uint32_t w, uint32_t h,
-                             void *d3d_tex2d)
+                             ScanoutTextureNative native)
 {
     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
ui/spice-display.c +1 -2
index db71e866f8..2a2285cb1f 100644
@@ -1084,7 +1084,7 @@ static void qemu_spice_gl_scanout_texture(DisplayChangeListener *dcl,
                                           uint32_t backing_height,
                                           uint32_t x, uint32_t y,
                                           uint32_t w, uint32_t h,
-                                          void *d3d_tex2d)
+                                          ScanoutTextureNative native)
 {
     SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
     EGLint offset[DMABUF_MAX_PLANES], stride[DMABUF_MAX_PLANES], fourcc = 0;
--

2.41.0

Signature · 1 line
Marc-André Lureau
#29 lore raw

Re: [PATCH RFC 4/7] virtio-gpu-virgl: update virglrenderer defines

In reply to Joelle van Dyne

Hi

Quoted 6 lines · Joelle van Dyne

In order to support additional native texture types, we need to update the defines in virglrenderer. The changes are backwards compatible and so builds should work with either the new version or the old version.

Please add a link to the pending MR.

Quoted 69 lines
---
 hw/display/virtio-gpu-virgl.c | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-virgl.c +25 -4
index d0e6ad4b17..36c670f988 100644
@@ -24,6 +24,8 @@
 
 #include <virglrenderer.h>
 
+#define SUPPORTED_VIRGL_INFO_EXT_VERSION (1)
+
 struct virtio_gpu_virgl_resource {
     struct virtio_gpu_simple_resource base;
     MemoryRegion *mr;
@@ -441,12 +443,30 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
         memset(&ext, 0, sizeof(ext));
         ret = virgl_renderer_resource_get_info_ext(ss.resource_id, &ext);
         info = ext.base;
+        /* fallback to older version */
         native = (ScanoutTextureNative){
             .type = ext.d3d_tex2d ? SCANOUT_TEXTURE_NATIVE_TYPE_D3D :
                                     SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
             .u.d3d_tex2d = ext.d3d_tex2d,
         };
-#else
+#if VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION >= SUPPORTED_VIRGL_INFO_EXT_VERSION
+        if (ext.version >= VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION) {
+            switch (ext.native_type) {
+            case VIRGL_NATIVE_HANDLE_NONE:
+            case VIRGL_NATIVE_HANDLE_D3D_TEX2D: {
+                /* already handled above */
+                break;
+            }
+            default: {
+                qemu_log_mask(LOG_GUEST_ERROR,
+                            "%s: unsupported native texture type %d\n",
+                            __func__, ext.native_type);
+                break;
+            }
+            }
+        }
+#endif
+#else /* VIRGL_VERSION_MAJOR < 1 */
         memset(&info, 0, sizeof(info));
         ret = virgl_renderer_resource_get_info(ss.resource_id, &info);
 #endif
@@ -1169,11 +1189,13 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
         virtio_gpu_3d_cbs.get_egl_display = virgl_get_egl_display;
     }
 #endif
-#ifdef VIRGL_RENDERER_D3D11_SHARE_TEXTURE
     if (qemu_egl_angle_native_device) {
+#if defined(VIRGL_RENDERER_NATIVE_SHARE_TEXTURE)
+        flags |= VIRGL_RENDERER_NATIVE_SHARE_TEXTURE;
+#elif defined(VIRGL_RENDERER_D3D11_SHARE_TEXTURE) && defined(WIN32)
         flags |= VIRGL_RENDERER_D3D11_SHARE_TEXTURE;
-    }
 #endif
+    }
 #if VIRGL_VERSION_MAJOR >= 1
     if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
         flags |= VIRGL_RENDERER_VENUS | VIRGL_RENDERER_RENDER_SERVER;
--

2.41.0

Signature · 1 line
Marc-André Lureau
#17 lore raw

Re: [PATCH RFC 2/7] virtio-gpu-virgl: check page alignment of blob mapping

In reply to Joelle van Dyne
Quoted 43 lines · Joelle van Dyne

Currently if a mapping is not page aligned, it will sliently fail and the guest, assuming it is mapped, will attempt to access the memory and fail. This is particularly an issue on macOS when the host page size is 16KiB and the guest page size is 4KiB.

---
hw/display/virtio-gpu-virgl.c | 14 ++++++++++++++

1 file changed, 14 insertions(+)
hw/display/virtio-gpu-virgl.c +14 -0
index 20c856c04e..adf02ac22b 100644
@@ -116,6 +116,20 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
        return ret;
    }
 
+    if (!QEMU_IS_ALIGNED((uintptr_t)data, qemu_real_host_page_size())) {
+        virgl_renderer_resource_unmap(res->base.resource_id);
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: address %p is not aligned to page size\n",
+                      __func__, data);
+        return -ENOMEM;
+    }
+
+    if (!QEMU_IS_ALIGNED(size, qemu_real_host_page_size())) {
+        virgl_renderer_resource_unmap(res->base.resource_id);
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: size 0x%llx is not aligned to page size\n",
+                      __func__, size);
+        return -ENOMEM;
+    }
+
    vmr = g_new0(struct virtio_gpu_virgl_hostmem_region, 1);
    vmr->g = g;
 
Signature · 1 line
2.41.0

Hello,

There’s a better way to handle this without guest breakage.

Please use hv_vm_config_set_ipa_granule, it’ll allow to have 4KB granule allocations as the IPA.

Note: This might have some impact on performance so we might to want to enable it only when virtio-gpu is used.

Thank you,

#3 lore raw

Re: [PATCH RFC 1/7] egl-helpers: store handle to native device

In reply to Akihiko Odaki
Quoted 13 lines · Akihiko Odaki
Quoted 11 lines · Joelle van Dyne

Make way for other platforms by making the variable more general. Also we will be using the device in the future so let's save the pointer in the global instead of just a boolean flag.

---
  include/ui/egl-helpers.h      | 2 +-
  hw/display/virtio-gpu-virgl.c | 2 +-
  ui/egl-helpers.c              | 4 ++--
  3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 13 lines

include/ui/egl-helpers.h line 15

Quoted 10 lines
include/ui/egl-helpers.h +1 -1
index acf993fcf5..c239d32317 100644
@@ -12,7 +12,7 @@
  extern EGLDisplay *qemu_egl_display;
  extern EGLConfig qemu_egl_config;
  extern DisplayGLMode qemu_egl_mode;
-extern bool qemu_egl_angle_d3d;
+extern void *qemu_egl_angle_native_device;

I guess ANGLE will not be relevant for Metal and leaving it will be misleading.

What is your suggestion? This is just to remove "D3D" from the variable name. If you want to remove "ANGLE" from the variable name as well, it may be misleading because this variable is only used with an ANGLE EGL backend.

Quoted 43 lines

Regards,
Akihiko Odaki

Quoted 38 lines
include/ui/egl-helpers.h +0 -0
  typedef struct egl_fb {
      int width;
hw/display/virtio-gpu-virgl.c +1 -1
index 07f6355ad6..20c856c04e 100644
@@ -1152,7 +1152,7 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
      }
  #endif
  #ifdef VIRGL_RENDERER_D3D11_SHARE_TEXTURE
-    if (qemu_egl_angle_d3d) {
+    if (qemu_egl_angle_native_device) {
          flags |= VIRGL_RENDERER_D3D11_SHARE_TEXTURE;
      }
  #endif
ui/egl-helpers.c +2 -2
index e3f2872cc1..57bcf99498 100644
@@ -28,7 +28,7 @@
  EGLDisplay *qemu_egl_display;
  EGLConfig qemu_egl_config;
  DisplayGLMode qemu_egl_mode;
-bool qemu_egl_angle_d3d;
+void *qemu_egl_angle_native_device;
 
  /* ------------------------------------------------------------------ */
 
@@ -651,7 +651,7 @@ int qemu_egl_init_dpy_win32(EGLNativeDisplayType dpy, DisplayGLMode mode)
          }
 
          trace_egl_init_d3d11_device(device);
-        qemu_egl_angle_d3d = device != NULL;
+        qemu_egl_angle_native_device = d3d11_device;
      }
  #endif
 
#20 lore raw

Re: [PATCH RFC 3/7] console: rename `d3d_tex2d` to `native`

In reply to Akihiko Odaki
Quoted 47 lines · Akihiko Odaki
Quoted 27 lines · Joelle van Dyne

In order to support native texture scanout beyond D3D, we make this more generic allowing for multiple native texture handle types.

---
  include/ui/console.h          | 22 +++++++++++++++++++---
  include/ui/gtk.h              |  4 ++--
  include/ui/sdl2.h             |  2 +-
  hw/display/virtio-gpu-virgl.c | 10 +++++++---
  ui/console.c                  |  8 ++++----
  ui/dbus-console.c             |  2 +-
  ui/dbus-listener.c            |  8 ++++----
  ui/egl-headless.c             |  2 +-
  ui/gtk-egl.c                  |  2 +-
  ui/gtk-gl-area.c              |  2 +-
  ui/sdl2-gl.c                  |  2 +-
  ui/spice-display.c            |  2 +-
  12 files changed, 43 insertions(+), 23 deletions(-)
include/ui/console.h +0 -0
index 98feaa58bd..25e45295d4 100644
@@ -131,6 +131,22 @@ struct QemuConsoleClass {
      ObjectClass parent_class;
  };
 

include/ui/console.h struct QemuConsoleClass { · line 143

Quoted 10 lines
include/ui/console.h +10 -0
+typedef enum ScanoutTextureNativeType {
+    SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
+    SCANOUT_TEXTURE_NATIVE_TYPE_D3D,
+} ScanoutTextureNativeType;
+
+typedef struct ScanoutTextureNative {
+    ScanoutTextureNativeType type;
+    union {
+        void *d3d_tex2d;
+    } u;> +} ScanoutTextureNative;

Instead, I suggest:

typedef struct ScanoutTextureNative {
     ScanoutTextureNativeType type;
     void *handle;
} ScanoutTextureNative;
Quoted 4 lines

...to align with the definition of struct virgl_renderer_resource_info_ext and virgl_renderer_create_handle_for_scanout(), which do not add a field for each type (except the one for the backward compatibility).

I've updated virglrenderer to use a union as well. Since we are passing ScanoutTextureNative around byval and it can grow with additional platform support, I think it would be better to keep it as a union.

Quoted 266 lines

Regards,
Akihiko Odaki

Quoted 171 lines
include/ui/console.h +8 -3
+
+#define NO_NATIVE_TEXTURE ((ScanoutTextureNative){ \
+    .type = SCANOUT_TEXTURE_NATIVE_TYPE_NONE \
+})
+
  typedef struct ScanoutTexture {
      uint32_t backing_id;
      bool backing_y_0_top;
@@ -140,7 +156,7 @@ typedef struct ScanoutTexture {
      uint32_t y;
      uint32_t width;
      uint32_t height;
-    void *d3d_tex2d;
+    ScanoutTextureNative native;
  } ScanoutTexture;
 
  typedef struct QemuUIInfo {
@@ -245,7 +261,7 @@ typedef struct DisplayChangeListenerOps {
                                     uint32_t backing_height,
                                     uint32_t x, uint32_t y,
                                     uint32_t w, uint32_t h,
-                                   void *d3d_tex2d);
+                                   ScanoutTextureNative native);
      /* optional (default to true if has dpy_gl_scanout_dmabuf) */
      bool (*dpy_has_dmabuf)(DisplayChangeListener *dcl);
      /* optional */
@@ -329,7 +345,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                              uint32_t backing_id, bool backing_y_0_top,
                              uint32_t backing_width, uint32_t backing_height,
                              uint32_t x, uint32_t y, uint32_t w, uint32_t h,
-                            void *d3d_tex2d);
+                            ScanoutTextureNative native);
  void dpy_gl_scanout_dmabuf(QemuConsole *con,
                             QemuDmaBuf *dmabuf);
  void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
include/ui/gtk.h +2 -2
index 3e6ce3cb48..f88e2b281d 100644
@@ -178,7 +178,7 @@ void gd_egl_scanout_texture(DisplayChangeListener *dcl,
                              uint32_t backing_height,
                              uint32_t x, uint32_t y,
                              uint32_t w, uint32_t h,
-                            void *d3d_tex2d);
+                            ScanoutTextureNative native);
  void gd_egl_scanout_dmabuf(DisplayChangeListener *dcl,
                             QemuDmaBuf *dmabuf);
  void gd_egl_cursor_dmabuf(DisplayChangeListener *dcl,
@@ -215,7 +215,7 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                  uint32_t backing_height,
                                  uint32_t x, uint32_t y,
                                  uint32_t w, uint32_t h,
-                                void *d3d_tex2d);
+                                ScanoutTextureNative native);
  void gd_gl_area_scanout_disable(DisplayChangeListener *dcl);
  void gd_gl_area_scanout_flush(DisplayChangeListener *dcl,
                                uint32_t x, uint32_t y, uint32_t w, uint32_t h);
include/ui/sdl2.h +1 -1
index dbe6e3d973..fdefb88229 100644
@@ -93,7 +93,7 @@ void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
                               uint32_t backing_height,
                               uint32_t x, uint32_t y,
                               uint32_t w, uint32_t h,
-                             void *d3d_tex2d);
+                             ScanoutTextureNative native);
  void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
                             uint32_t x, uint32_t y, uint32_t w, uint32_t h);
 
hw/display/virtio-gpu-virgl.c +7 -3
index adf02ac22b..d0e6ad4b17 100644
@@ -434,14 +434,18 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
 
      if (ss.resource_id && ss.r.width && ss.r.height) {
          struct virgl_renderer_resource_info info;
-        void *d3d_tex2d = NULL;
+        ScanoutTextureNative native = { .type = SCANOUT_TEXTURE_NATIVE_TYPE_NONE };
 
  #if VIRGL_VERSION_MAJOR >= 1
          struct virgl_renderer_resource_info_ext ext;
          memset(&ext, 0, sizeof(ext));
          ret = virgl_renderer_resource_get_info_ext(ss.resource_id, &ext);
          info = ext.base;
-        d3d_tex2d = ext.d3d_tex2d;
+        native = (ScanoutTextureNative){
+            .type = ext.d3d_tex2d ? SCANOUT_TEXTURE_NATIVE_TYPE_D3D :
+                                    SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
+            .u.d3d_tex2d = ext.d3d_tex2d,
+        };
  #else
          memset(&info, 0, sizeof(info));
          ret = virgl_renderer_resource_get_info(ss.resource_id, &info);
@@ -461,7 +465,7 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
              info.flags & VIRTIO_GPU_RESOURCE_FLAG_Y_0_TOP,
              info.width, info.height,
              ss.r.x, ss.r.y, ss.r.width, ss.r.height,
-            d3d_tex2d);
+            native);
      } else {
          dpy_gfx_replace_surface(
              g->parent_obj.scanout[ss.scanout_id].con, NULL);
ui/console.c +4 -4
index 2d00828c53..9378afd53d 100644
@@ -295,7 +295,7 @@ static void displaychangelistener_display_console(DisplayChangeListener *dcl,
                                           con->scanout.texture.y,
                                           con->scanout.texture.width,
                                           con->scanout.texture.height,
-                                         con->scanout.texture.d3d_tex2d);
+                                         con->scanout.texture.native);
      }
  }
 
@@ -1023,7 +1023,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                              uint32_t backing_height,
                              uint32_t x, uint32_t y,
                              uint32_t width, uint32_t height,
-                            void *d3d_tex2d)
+                            ScanoutTextureNative native)
  {
      DisplayState *s = con->ds;
      DisplayChangeListener *dcl;
@@ -1031,7 +1031,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
      con->scanout.kind = SCANOUT_TEXTURE;
      con->scanout.texture = (ScanoutTexture) {
          backing_id, backing_y_0_top, backing_width, backing_height,
-        x, y, width, height, d3d_tex2d,
+        x, y, width, height, native,
      };
      QLIST_FOREACH(dcl, &s->listeners, next) {
          if (con != dcl->con) {
@@ -1042,7 +1042,7 @@ void dpy_gl_scanout_texture(QemuConsole *con,
                                               backing_y_0_top,
                                               backing_width, backing_height,
                                               x, y, width, height,
-                                             d3d_tex2d);
+                                             native);
          }
      }
  }
ui/dbus-console.c +1 -1
index 85e215ef23..651f0daeaf 100644
@@ -99,7 +99,7 @@ dbus_gl_scanout_texture(DisplayChangeListener *dcl,
                          uint32_t backing_height,
                          uint32_t x, uint32_t y,
                          uint32_t w, uint32_t h,
-                        void *d3d_tex2d)
+                        ScanoutTextureNative native)
  {
      DBusDisplayConsole *ddc = container_of(dcl, DBusDisplayConsole, dcl);
 
ui/dbus-listener.c +1 -1
index 52e041edb0..a737752996 100644
@@ -587,7 +587,7 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
                                   uint32_t backing_height,
                                   uint32_t x, uint32_t y,
                                   uint32_t w, uint32_t h,
-                                 void *d3d_tex2d)
+                                 ScanoutTextureNative native)
  {
      trace_dbus_scanout_texture(tex_id, backing_y_0_top,
                                 backing_width, backing_height, x, y, w, h);

ui/dbus-listener.c static void dbus_scanout_texture(DisplayChangeListener *dcl, · line 621

Quoted 6 lines
ui/dbus-listener.c +1 -2
@@ -618,8 +618,8 @@ static void dbus_scanout_texture(DisplayChangeListener *dcl,
      assert(surface_width(ddl->ds) == w);
      assert(surface_height(ddl->ds) == h);
 
-    if (d3d_tex2d) {
-        dbus_scanout_share_d3d_texture(ddl, d3d_tex2d, backing_y_0_top,
+    if (native.type == SCANOUT_TEXTURE_NATIVE_TYPE_D3d) {

s/SCANOUT_TEXTURE_NATIVE_TYPE_D3d/SCANOUT_TEXTURE_NATIVE_TYPE_D3D/

Regards,
Akihiko Odaki

Quoted 78 lines
ui/dbus-listener.c +2 -1
+        dbus_scanout_share_d3d_texture(ddl, native.u.d3d_tex2d, backing_y_0_top,
                                         backing_width, backing_height, x, y, w, h);
      } else {
          dbus_scanout_map(ddl);
@@ -868,7 +868,7 @@ static void dbus_gl_gfx_switch(DisplayChangeListener *dcl,
 
          /* TODO: lazy send dmabuf (there are unnecessary sent otherwise) */
          dbus_scanout_texture(&ddl->dcl, ddl->ds->texture, false,
-                             width, height, 0, 0, width, height, NULL);
+                             width, height, 0, 0, width, height, NO_NATIVE_TEXTURE);
      }
  }
  #endif
ui/egl-headless.c +1 -1
index 1f6b845500..e36778bd48 100644
@@ -62,7 +62,7 @@ static void egl_scanout_texture(DisplayChangeListener *dcl,
                                  uint32_t backing_height,
                                  uint32_t x, uint32_t y,
                                  uint32_t w, uint32_t h,
-                                void *d3d_tex2d)
+                                ScanoutTextureNative native)
  {
      egl_dpy *edpy = container_of(dcl, egl_dpy, dcl);
 
ui/gtk-egl.c +1 -1
index ae9239999c..7933237d45 100644
@@ -239,7 +239,7 @@ void gd_egl_scanout_texture(DisplayChangeListener *dcl,
                              uint32_t backing_width, uint32_t backing_height,
                              uint32_t x, uint32_t y,
                              uint32_t w, uint32_t h,
-                            void *d3d_tex2d)
+                            ScanoutTextureNative native)
  {
      VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 
ui/gtk-gl-area.c +1 -1
index cd86022d26..f2ed543d6a 100644
@@ -306,7 +306,7 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                  uint32_t backing_height,
                                  uint32_t x, uint32_t y,
                                  uint32_t w, uint32_t h,
-                                void *d3d_tex2d)
+                                ScanoutTextureNative native)
  {
      VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
 
ui/sdl2-gl.c +1 -1
index 3be17d1079..b15db86f24 100644
@@ -206,7 +206,7 @@ void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
                               uint32_t backing_height,
                               uint32_t x, uint32_t y,
                               uint32_t w, uint32_t h,
-                             void *d3d_tex2d)
+                             ScanoutTextureNative native)
  {
      struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
ui/spice-display.c +1 -1
index db71e866f8..2a2285cb1f 100644
@@ -1084,7 +1084,7 @@ static void qemu_spice_gl_scanout_texture(DisplayChangeListener *dcl,
                                            uint32_t backing_height,
                                            uint32_t x, uint32_t y,
                                            uint32_t w, uint32_t h,
-                                          void *d3d_tex2d)
+                                          ScanoutTextureNative native)
  {
      SimpleSpiceDisplay *ssd = container_of(dcl, SimpleSpiceDisplay, dcl);
      EGLint offset[DMABUF_MAX_PLANES], stride[DMABUF_MAX_PLANES], fourcc = 0;
#27 lore raw

Re: [PATCH RFC 4/7] virtio-gpu-virgl: update virglrenderer defines

In reply to Akihiko Odaki
Quoted 47 lines · Akihiko Odaki
Quoted 14 lines · Joelle van Dyne

In order to support additional native texture types, we need to update the defines in virglrenderer. The changes are backwards compatible and so builds should work with either the new version or the old version.

---
  hw/display/virtio-gpu-virgl.c | 28 +++++++++++++++++++++++++---
  1 file changed, 25 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-virgl.c +0 -0
index d0e6ad4b17..36c670f988 100644

hw/display/virtio-gpu-virgl.c line 27

Quoted 4 lines
hw/display/virtio-gpu-virgl.c +1 -0
@@ -24,6 +24,8 @@
 
  #include <virglrenderer.h>
 
+#define SUPPORTED_VIRGL_INFO_EXT_VERSION (1)

This naming is a bit confusing because version 0 is also supported.

Quoted 24 lines
hw/display/virtio-gpu-virgl.c +11 -1
+
  struct virtio_gpu_virgl_resource {
      struct virtio_gpu_simple_resource base;
      MemoryRegion *mr;
@@ -441,12 +443,30 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
          memset(&ext, 0, sizeof(ext));
          ret = virgl_renderer_resource_get_info_ext(ss.resource_id, &ext);
          info = ext.base;
+        /* fallback to older version */
          native = (ScanoutTextureNative){
              .type = ext.d3d_tex2d ? SCANOUT_TEXTURE_NATIVE_TYPE_D3D :
                                      SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
              .u.d3d_tex2d = ext.d3d_tex2d,
          };
-#else
+#if VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION >= SUPPORTED_VIRGL_INFO_EXT_VERSION
+        if (ext.version >= VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION) {
+            switch (ext.native_type) {
+            case VIRGL_NATIVE_HANDLE_NONE:
+            case VIRGL_NATIVE_HANDLE_D3D_TEX2D: {
+                /* already handled above */
+                break;
+            }
+            default: {
Quoted 8 lines

hw/display/virtio-gpu-virgl.c static void virgl_cmd_set_scanout(VirtIOGPU *g, · line 463

Quoted 3 lines
hw/display/virtio-gpu-virgl.c +3 -0
+                qemu_log_mask(LOG_GUEST_ERROR,
+                            "%s: unsupported native texture type %d\n",
+                            __func__, ext.native_type);

It is not an error condition; the d3d_tex2d field is an optional hint that allows zero-copy inter-process sharing and the tex_id field is used if it is missing or inter-process sharing is unnecessary. It should be fine to dismiss the native handle unless the semantic changes.

When VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION >= 1, the semantic changed to always have a ext.native_type. In case of missing d3d_tex2d, ext.native_type will be VIRGL_NATIVE_HANDLE_NONE. The default case is if virglrenderer introduces a new native type that is unknown at the time QEMU is built. This is why the struct version is bumped.

Quoted 30 lines

Regards,
Akihiko Odaki

Quoted 25 lines
hw/display/virtio-gpu-virgl.c +10 -2
+                break;
+            }
+            }
+        }
+#endif
+#else /* VIRGL_VERSION_MAJOR < 1 */
          memset(&info, 0, sizeof(info));
          ret = virgl_renderer_resource_get_info(ss.resource_id, &info);
  #endif
@@ -1169,11 +1189,13 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
          virtio_gpu_3d_cbs.get_egl_display = virgl_get_egl_display;
      }
  #endif
-#ifdef VIRGL_RENDERER_D3D11_SHARE_TEXTURE
      if (qemu_egl_angle_native_device) {
+#if defined(VIRGL_RENDERER_NATIVE_SHARE_TEXTURE)
+        flags |= VIRGL_RENDERER_NATIVE_SHARE_TEXTURE;
+#elif defined(VIRGL_RENDERER_D3D11_SHARE_TEXTURE) && defined(WIN32)
          flags |= VIRGL_RENDERER_D3D11_SHARE_TEXTURE;
-    }
  #endif
+    }
  #if VIRGL_VERSION_MAJOR >= 1
      if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
          flags |= VIRGL_RENDERER_VENUS | VIRGL_RENDERER_RENDER_SERVER;
#4 lore raw

Re: [PATCH RFC 1/7] egl-helpers: store handle to native device

In reply to Joelle van Dyne
Quoted 28 lines · Joelle van Dyne
Quoted 13 lines · Akihiko Odaki
Quoted 11 lines · Joelle van Dyne

Make way for other platforms by making the variable more general. Also we will be using the device in the future so let's save the pointer in the global instead of just a boolean flag.

---
   include/ui/egl-helpers.h      | 2 +-
   hw/display/virtio-gpu-virgl.c | 2 +-
   ui/egl-helpers.c              | 4 ++--
   3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 13 lines

include/ui/egl-helpers.h line 15

Quoted 10 lines
include/ui/egl-helpers.h +1 -1
index acf993fcf5..c239d32317 100644
@@ -12,7 +12,7 @@
   extern EGLDisplay *qemu_egl_display;
   extern EGLConfig qemu_egl_config;
   extern DisplayGLMode qemu_egl_mode;
-extern bool qemu_egl_angle_d3d;
+extern void *qemu_egl_angle_native_device;

I guess ANGLE will not be relevant for Metal and leaving it will be misleading.

Quoted 4 lines

What is your suggestion? This is just to remove "D3D" from the variable name. If you want to remove "ANGLE" from the variable name as well, it may be misleading because this variable is only used with an ANGLE EGL backend.

Looking the usage of the variable in your tree, I realized this variable may not be necessary for this at all.

There are two use cases:

1. To determine if VIRGL_RENDERER_NATIVE_SHARE_TEXTURE needs to be set.
2. To pass the device used by ANGLE to Cocoa.

Regarding 1, virglrenderer can simply pass MTLTexture whenever the EGL context is backed with Metal and Venus is in use.

Although your code is modeled after the code dealing with Direct3D handles, the functionality of your code is quite a different from it.

Direct3D handles are used to "share" texture with other processes in a zero-copy manner. It is an optional hint; dbus can fall back to use OpenGL textures if they are missing, and the other displays just don't care.

On the other hand, MTLTexture plays an essential role in your scenario. There are no corresponding OpenGL texture so no fallback happens.

The difference shows that the VIRGL_RENDERER_NATIVE_SHARE_TEXTURE flag is useful when dealing with Direct3D handles but not with MTLTexture. The absence of flag tells virglrenderer that we do not need Direct3D handle (because the dbus display is not used), and allows it to skip some code to convert OpenGL textures to Direct3D handles).

On the other hand, not passing MTLTexture in your scenario does not make sense because it prevents the scanout at all. virglrenderer does not have any code that can be skipped when the flag is absent. virglrenderer can simply pass MTLTexture in your scenario.

2 implicitly lets ANGLE choose a device, but this can be suboptimal. Ideally, Cocoa should instead choose a device because it knows what device displays the window. Cocoa should pass the device it chose to ANGLE.

To demonstrate the point, I updated my tree to use ANGLE's Metal renderer for OpenGL and to pass the device from Cocoa to ANGLE:
https://github.com/akihikodaki/v/commit/8f191a7d2c225fa601c22a783c24c81a3fda47fa

By the way, I had to make a few modifications to ANGLE, Epoxy, and virglrenderer to fix glitches when hosting glmark2 and Firefox on GNOME. You can also find them with the above URL if you are interested.

Regards,
Akihiko Odaki

#21 lore raw

Re: [PATCH RFC 3/7] console: rename `d3d_tex2d` to `native`

In reply to Joelle van Dyne
Quoted 49 lines · Joelle van Dyne
Quoted 47 lines · Akihiko Odaki
Quoted 27 lines · Joelle van Dyne

In order to support native texture scanout beyond D3D, we make this more generic allowing for multiple native texture handle types.

---
   include/ui/console.h          | 22 +++++++++++++++++++---
   include/ui/gtk.h              |  4 ++--
   include/ui/sdl2.h             |  2 +-
   hw/display/virtio-gpu-virgl.c | 10 +++++++---
   ui/console.c                  |  8 ++++----
   ui/dbus-console.c             |  2 +-
   ui/dbus-listener.c            |  8 ++++----
   ui/egl-headless.c             |  2 +-
   ui/gtk-egl.c                  |  2 +-
   ui/gtk-gl-area.c              |  2 +-
   ui/sdl2-gl.c                  |  2 +-
   ui/spice-display.c            |  2 +-
   12 files changed, 43 insertions(+), 23 deletions(-)
include/ui/console.h +0 -0
index 98feaa58bd..25e45295d4 100644
@@ -131,6 +131,22 @@ struct QemuConsoleClass {
       ObjectClass parent_class;
   };
 

include/ui/console.h struct QemuConsoleClass { · line 143

Quoted 10 lines
include/ui/console.h +10 -0
+typedef enum ScanoutTextureNativeType {
+    SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
+    SCANOUT_TEXTURE_NATIVE_TYPE_D3D,
+} ScanoutTextureNativeType;
+
+typedef struct ScanoutTextureNative {
+    ScanoutTextureNativeType type;
+    union {
+        void *d3d_tex2d;
+    } u;> +} ScanoutTextureNative;

Instead, I suggest:

typedef struct ScanoutTextureNative {
      ScanoutTextureNativeType type;
      void *handle;
} ScanoutTextureNative;
Quoted 8 lines
Quoted 4 lines

...to align with the definition of struct virgl_renderer_resource_info_ext and virgl_renderer_create_handle_for_scanout(), which do not add a field for each type (except the one for the backward compatibility).

I've updated virglrenderer to use a union as well. Since we are passing ScanoutTextureNative around byval and it can grow with additional platform support, I think it would be better to keep it as a union.

I don't think using a union for virglrenderer is a good idea because adding another member to the union can change its size and cause an ABI breakage. Sticking to void * is the safest choice. We rely on void * to represent platform-specific types anyway so there is practically no type safetey here, unfortunately.

Regards,
Akihiko Odaki

#28 lore raw

Re: [PATCH RFC 4/7] virtio-gpu-virgl: update virglrenderer defines

In reply to Joelle van Dyne
Quoted 57 lines · Joelle van Dyne
Quoted 47 lines · Akihiko Odaki
Quoted 14 lines · Joelle van Dyne

In order to support additional native texture types, we need to update the defines in virglrenderer. The changes are backwards compatible and so builds should work with either the new version or the old version.

---
   hw/display/virtio-gpu-virgl.c | 28 +++++++++++++++++++++++++---
   1 file changed, 25 insertions(+), 3 deletions(-)
hw/display/virtio-gpu-virgl.c +0 -0
index d0e6ad4b17..36c670f988 100644

hw/display/virtio-gpu-virgl.c line 27

Quoted 4 lines
hw/display/virtio-gpu-virgl.c +1 -0
@@ -24,6 +24,8 @@
 
   #include <virglrenderer.h>
 
+#define SUPPORTED_VIRGL_INFO_EXT_VERSION (1)

This naming is a bit confusing because version 0 is also supported.

Quoted 24 lines
hw/display/virtio-gpu-virgl.c +11 -1
+
   struct virtio_gpu_virgl_resource {
       struct virtio_gpu_simple_resource base;
       MemoryRegion *mr;
@@ -441,12 +443,30 @@ static void virgl_cmd_set_scanout(VirtIOGPU *g,
           memset(&ext, 0, sizeof(ext));
           ret = virgl_renderer_resource_get_info_ext(ss.resource_id, &ext);
           info = ext.base;
+        /* fallback to older version */
           native = (ScanoutTextureNative){
               .type = ext.d3d_tex2d ? SCANOUT_TEXTURE_NATIVE_TYPE_D3D :
                                       SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
               .u.d3d_tex2d = ext.d3d_tex2d,
           };
-#else
+#if VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION >= SUPPORTED_VIRGL_INFO_EXT_VERSION
+        if (ext.version >= VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION) {
+            switch (ext.native_type) {
+            case VIRGL_NATIVE_HANDLE_NONE:
+            case VIRGL_NATIVE_HANDLE_D3D_TEX2D: {
+                /* already handled above */
+                break;
+            }
+            default: {
Quoted 8 lines

hw/display/virtio-gpu-virgl.c static void virgl_cmd_set_scanout(VirtIOGPU *g, · line 463

Quoted 3 lines
hw/display/virtio-gpu-virgl.c +3 -0
+                qemu_log_mask(LOG_GUEST_ERROR,
+                            "%s: unsupported native texture type %d\n",
+                            __func__, ext.native_type);

It is not an error condition; the d3d_tex2d field is an optional hint that allows zero-copy inter-process sharing and the tex_id field is used if it is missing or inter-process sharing is unnecessary. It should be fine to dismiss the native handle unless the semantic changes.

Quoted 6 lines

When VIRGL_RENDERER_RESOURCE_INFO_EXT_VERSION >= 1, the semantic changed to always have a ext.native_type. In case of missing d3d_tex2d, ext.native_type will be VIRGL_NATIVE_HANDLE_NONE. The default case is if virglrenderer introduces a new native type that is unknown at the time QEMU is built. This is why the struct version is bumped.

What I am saying is that having a new native type that is unknown is fine and hence no need to log an error. QEMU will simply use the OpenGL texture in such a case.

Regards,
Akihiko Odaki

#5 lore raw

Re: [PATCH RFC 1/7] egl-helpers: store handle to native device

In reply to Akihiko Odaki
Quoted 48 lines · Akihiko Odaki
Quoted 28 lines · Joelle van Dyne
Quoted 13 lines · Akihiko Odaki
Quoted 11 lines · Joelle van Dyne

Make way for other platforms by making the variable more general. Also we will be using the device in the future so let's save the pointer in the global instead of just a boolean flag.

---
   include/ui/egl-helpers.h      | 2 +-
   hw/display/virtio-gpu-virgl.c | 2 +-
   ui/egl-helpers.c              | 4 ++--
   3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 13 lines

include/ui/egl-helpers.h line 15

Quoted 10 lines
include/ui/egl-helpers.h +1 -1
index acf993fcf5..c239d32317 100644
@@ -12,7 +12,7 @@
   extern EGLDisplay *qemu_egl_display;
   extern EGLConfig qemu_egl_config;
   extern DisplayGLMode qemu_egl_mode;
-extern bool qemu_egl_angle_d3d;
+extern void *qemu_egl_angle_native_device;

I guess ANGLE will not be relevant for Metal and leaving it will be misleading.

Quoted 4 lines

What is your suggestion? This is just to remove "D3D" from the variable name. If you want to remove "ANGLE" from the variable name as well, it may be misleading because this variable is only used with an ANGLE EGL backend.

Looking the usage of the variable in your tree, I realized this variable may not be necessary for this at all.

There are two use cases:

1. To determine if VIRGL_RENDERER_NATIVE_SHARE_TEXTURE needs to be set.
2. To pass the device used by ANGLE to Cocoa.

Regarding 1, virglrenderer can simply pass MTLTexture whenever the EGL context is backed with Metal and Venus is in use.

Although your code is modeled after the code dealing with Direct3D handles, the functionality of your code is quite a different from it.

Quoted 6 lines

Direct3D handles are used to "share" texture with other processes in a zero-copy manner. It is an optional hint; dbus can fall back to use OpenGL textures if they are missing, and the other displays just don't care.

On the other hand, MTLTexture plays an essential role in your scenario. There are no corresponding OpenGL texture so no fallback happens.

That's a good point, but there's two uses of MTLTexture: one as a direct parallel to D3D handles (vrend allocate a texture backed by MTLTexture; returns it in info_ext along with a tex_id) and one returned as part of the new API (virgl_renderer_create_handle_for_scanout) which does not have an associated tex_id.

Quoted 16 lines

The difference shows that the VIRGL_RENDERER_NATIVE_SHARE_TEXTURE flag is useful when dealing with Direct3D handles but not with MTLTexture. The absence of flag tells virglrenderer that we do not need Direct3D handle (because the dbus display is not used), and allows it to skip some code to convert OpenGL textures to Direct3D handles).

On the other hand, not passing MTLTexture in your scenario does not make sense because it prevents the scanout at all. virglrenderer does not have any code that can be skipped when the flag is absent. virglrenderer can simply pass MTLTexture in your scenario.

2 implicitly lets ANGLE choose a device, but this can be suboptimal. Ideally, Cocoa should instead choose a device because it knows what device displays the window. Cocoa should pass the device it chose to ANGLE.

Quoted 7 lines

To demonstrate the point, I updated my tree to use ANGLE's Metal renderer for OpenGL and to pass the device from Cocoa to ANGLE:
https://github.com/akihikodaki/v/commit/8f191a7d2c225fa601c22a783c24c81a3fda47fa

By the way, I had to make a few modifications to ANGLE, Epoxy, and virglrenderer to fix glitches when hosting glmark2 and Firefox on GNOME. You can also find them with the above URL if you are interested.

Thank you, we picked up a couple of your changes in the UTM branch already. Do you have plans to upstream those changes? And also, what are your plans for upstreaming the Cocoa GL backend changes for QEMU?

Quoted 3 lines

Regards,
Akihiko Odaki

#22 lore raw

Re: [PATCH RFC 3/7] console: rename `d3d_tex2d` to `native`

In reply to Akihiko Odaki
Quoted 60 lines · Akihiko Odaki
Quoted 49 lines · Joelle van Dyne
Quoted 47 lines · Akihiko Odaki
Quoted 27 lines · Joelle van Dyne

In order to support native texture scanout beyond D3D, we make this more generic allowing for multiple native texture handle types.

---
   include/ui/console.h          | 22 +++++++++++++++++++---
   include/ui/gtk.h              |  4 ++--
   include/ui/sdl2.h             |  2 +-
   hw/display/virtio-gpu-virgl.c | 10 +++++++---
   ui/console.c                  |  8 ++++----
   ui/dbus-console.c             |  2 +-
   ui/dbus-listener.c            |  8 ++++----
   ui/egl-headless.c             |  2 +-
   ui/gtk-egl.c                  |  2 +-
   ui/gtk-gl-area.c              |  2 +-
   ui/sdl2-gl.c                  |  2 +-
   ui/spice-display.c            |  2 +-
   12 files changed, 43 insertions(+), 23 deletions(-)
include/ui/console.h +0 -0
index 98feaa58bd..25e45295d4 100644
@@ -131,6 +131,22 @@ struct QemuConsoleClass {
       ObjectClass parent_class;
   };
 

include/ui/console.h struct QemuConsoleClass { · line 143

Quoted 10 lines
include/ui/console.h +10 -0
+typedef enum ScanoutTextureNativeType {
+    SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
+    SCANOUT_TEXTURE_NATIVE_TYPE_D3D,
+} ScanoutTextureNativeType;
+
+typedef struct ScanoutTextureNative {
+    ScanoutTextureNativeType type;
+    union {
+        void *d3d_tex2d;
+    } u;> +} ScanoutTextureNative;

Instead, I suggest:

typedef struct ScanoutTextureNative {
      ScanoutTextureNativeType type;
      void *handle;
} ScanoutTextureNative;
Quoted 8 lines
Quoted 4 lines

...to align with the definition of struct virgl_renderer_resource_info_ext and virgl_renderer_create_handle_for_scanout(), which do not add a field for each type (except the one for the backward compatibility).

I've updated virglrenderer to use a union as well. Since we are passing ScanoutTextureNative around byval and it can grow with additional platform support, I think it would be better to keep it as a union.

Quoted 5 lines

I don't think using a union for virglrenderer is a good idea because adding another member to the union can change its size and cause an ABI breakage. Sticking to void * is the safest choice. We rely on void * to represent platform-specific types anyway so there is practically no type safetey here, unfortunately.

That's fair, we can use a struct in virglrenderer. However, in QEMU if we are passing byval and a struct (once increased in size) could lead to needless copying.

Quoted 3 lines

Regards,
Akihiko Odaki

#6 lore raw

Re: [PATCH RFC 1/7] egl-helpers: store handle to native device

In reply to Joelle van Dyne
Quoted 54 lines · Joelle van Dyne
Quoted 52 lines · Akihiko Odaki
Quoted 28 lines · Joelle van Dyne
Quoted 13 lines · Akihiko Odaki
Quoted 11 lines · Joelle van Dyne

Make way for other platforms by making the variable more general. Also we will be using the device in the future so let's save the pointer in the global instead of just a boolean flag.

---
    include/ui/egl-helpers.h      | 2 +-
    hw/display/virtio-gpu-virgl.c | 2 +-
    ui/egl-helpers.c              | 4 ++--
    3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 13 lines

include/ui/egl-helpers.h line 15

Quoted 10 lines
include/ui/egl-helpers.h +1 -1
index acf993fcf5..c239d32317 100644
@@ -12,7 +12,7 @@
    extern EGLDisplay *qemu_egl_display;
    extern EGLConfig qemu_egl_config;
    extern DisplayGLMode qemu_egl_mode;
-extern bool qemu_egl_angle_d3d;
+extern void *qemu_egl_angle_native_device;

I guess ANGLE will not be relevant for Metal and leaving it will be misleading.

Quoted 4 lines

What is your suggestion? This is just to remove "D3D" from the variable name. If you want to remove "ANGLE" from the variable name as well, it may be misleading because this variable is only used with an ANGLE EGL backend.

Looking the usage of the variable in your tree, I realized this variable may not be necessary for this at all.

There are two use cases:

1. To determine if VIRGL_RENDERER_NATIVE_SHARE_TEXTURE needs to be set.
2. To pass the device used by ANGLE to Cocoa.

Regarding 1, virglrenderer can simply pass MTLTexture whenever the EGL context is backed with Metal and Venus is in use.

Although your code is modeled after the code dealing with Direct3D handles, the functionality of your code is quite a different from it.

Direct3D handles are used to "share" texture with other processes in a zero-copy manner. It is an optional hint; dbus can fall back to use OpenGL textures if they are missing, and the other displays just don't care.

Quoted 8 lines
Quoted 2 lines

On the other hand, MTLTexture plays an essential role in your scenario. There are no corresponding OpenGL texture so no fallback happens.

That's a good point, but there's two uses of MTLTexture: one as a direct parallel to D3D handles (vrend allocate a texture backed by MTLTexture; returns it in info_ext along with a tex_id) and one returned as part of the new API (virgl_renderer_create_handle_for_scanout) which does not have an associated tex_id.

Thanks for clarification. I think the point I mentioned below still holds; virglrenderer does not have any code that can be skipped for MTLTexture with the absence of VIRGL_RENDERER_NATIVE_SHARE_TEXTURE.

Quoted 21 lines
Quoted 21 lines

The difference shows that the VIRGL_RENDERER_NATIVE_SHARE_TEXTURE flag is useful when dealing with Direct3D handles but not with MTLTexture. The absence of flag tells virglrenderer that we do not need Direct3D handle (because the dbus display is not used), and allows it to skip some code to convert OpenGL textures to Direct3D handles).

On the other hand, not passing MTLTexture in your scenario does not make sense because it prevents the scanout at all. virglrenderer does not have any code that can be skipped when the flag is absent. virglrenderer can simply pass MTLTexture in your scenario.

2 implicitly lets ANGLE choose a device, but this can be suboptimal. Ideally, Cocoa should instead choose a device because it knows what device displays the window. Cocoa should pass the device it chose to ANGLE.

To demonstrate the point, I updated my tree to use ANGLE's Metal renderer for OpenGL and to pass the device from Cocoa to ANGLE:
https://github.com/akihikodaki/v/commit/8f191a7d2c225fa601c22a783c24c81a3fda47fa

Quoted 6 lines
Quoted 3 lines

By the way, I had to make a few modifications to ANGLE, Epoxy, and virglrenderer to fix glitches when hosting glmark2 and Firefox on GNOME. You can also find them with the above URL if you are interested.

Thank you, we picked up a couple of your changes in the UTM branch already. Do you have plans to upstream those changes? And also, what are your plans for upstreaming the Cocoa GL backend changes for QEMU?

It depends on changes.

I intend to upstream the Cocoa change and the following virlgrenderer changes once the libepoxy change to enable ANGLE is merged, which is blocking forever... They are useless without the libepoxy change:
https://github.com/akihikodaki/virglrenderer/commit/6dfbc7e61a72347485c09a171c8c6c0908e12a2a
https://github.com/akihikodaki/virglrenderer/commit/b12bda2fd272b0ffd0a9b95a021e4a13e947749b

I intend to upstream all two changes for ANGLE:
https://github.com/akihikodaki/angle/commits/v/

The other changes are already submitted to the upstream if I don't miss anything.

Regards,
Akihiko Odaki

#23 lore raw

Re: [PATCH RFC 3/7] console: rename `d3d_tex2d` to `native`

In reply to Joelle van Dyne
Quoted 62 lines · Joelle van Dyne
Quoted 60 lines · Akihiko Odaki
Quoted 57 lines · Joelle van Dyne
Quoted 47 lines · Akihiko Odaki
Quoted 27 lines · Joelle van Dyne

In order to support native texture scanout beyond D3D, we make this more generic allowing for multiple native texture handle types.

---
    include/ui/console.h          | 22 +++++++++++++++++++---
    include/ui/gtk.h              |  4 ++--
    include/ui/sdl2.h             |  2 +-
    hw/display/virtio-gpu-virgl.c | 10 +++++++---
    ui/console.c                  |  8 ++++----
    ui/dbus-console.c             |  2 +-
    ui/dbus-listener.c            |  8 ++++----
    ui/egl-headless.c             |  2 +-
    ui/gtk-egl.c                  |  2 +-
    ui/gtk-gl-area.c              |  2 +-
    ui/sdl2-gl.c                  |  2 +-
    ui/spice-display.c            |  2 +-
    12 files changed, 43 insertions(+), 23 deletions(-)
include/ui/console.h +0 -0
index 98feaa58bd..25e45295d4 100644
@@ -131,6 +131,22 @@ struct QemuConsoleClass {
        ObjectClass parent_class;
    };
 

include/ui/console.h struct QemuConsoleClass { · line 143

Quoted 10 lines
include/ui/console.h +10 -0
+typedef enum ScanoutTextureNativeType {
+    SCANOUT_TEXTURE_NATIVE_TYPE_NONE,
+    SCANOUT_TEXTURE_NATIVE_TYPE_D3D,
+} ScanoutTextureNativeType;
+
+typedef struct ScanoutTextureNative {
+    ScanoutTextureNativeType type;
+    union {
+        void *d3d_tex2d;
+    } u;> +} ScanoutTextureNative;

Instead, I suggest:

typedef struct ScanoutTextureNative {
       ScanoutTextureNativeType type;
       void *handle;
} ScanoutTextureNative;
Quoted 4 lines

...to align with the definition of struct virgl_renderer_resource_info_ext and virgl_renderer_create_handle_for_scanout(), which do not add a field for each type (except the one for the backward compatibility).

I've updated virglrenderer to use a union as well. Since we are passing ScanoutTextureNative around byval and it can grow with additional platform support, I think it would be better to keep it as a union.

Quoted 8 lines
Quoted 5 lines

I don't think using a union for virglrenderer is a good idea because adding another member to the union can change its size and cause an ABI breakage. Sticking to void * is the safest choice. We rely on void * to represent platform-specific types anyway so there is practically no type safetey here, unfortunately.

That's fair, we can use a struct in virglrenderer. However, in QEMU if we are passing byval and a struct (once increased in size) could lead to needless copying.

What I suggest for virlgrenderer is basically as folllows:

struct virgl_renderer_resource_info_ext {
int version;
struct virgl_renderer_resource_info base;
bool has_dmabuf_export;
int planes;
uint64_t modifiers;
void *d3d_tex2d;

   union {
      /* this is for backwards compatibility */
      void *d3d_tex2d;
      void *native_handle;

};
enum virgl_renderer_native_handle_type native_type;
};

For QEMU:

typedef struct ScanoutTextureNative {
    enum virgl_renderer_native_handle_type type;
    void *handle;
} ScanoutTextureNative;

... and you'll get the stable ABI for virglrenderer and zero-copy with handle.

Regards,
Akihiko Odaki

#7 lore raw

Re: [PATCH RFC 1/7] egl-helpers: store handle to native device

In reply to Akihiko Odaki
Quoted 65 lines · Akihiko Odaki
Quoted 54 lines · Joelle van Dyne
Quoted 52 lines · Akihiko Odaki
Quoted 28 lines · Joelle van Dyne
Quoted 13 lines · Akihiko Odaki
Quoted 11 lines · Joelle van Dyne

Make way for other platforms by making the variable more general. Also we will be using the device in the future so let's save the pointer in the global instead of just a boolean flag.

---
    include/ui/egl-helpers.h      | 2 +-
    hw/display/virtio-gpu-virgl.c | 2 +-
    ui/egl-helpers.c              | 4 ++--
    3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 13 lines

include/ui/egl-helpers.h line 15

Quoted 10 lines
include/ui/egl-helpers.h +1 -1
index acf993fcf5..c239d32317 100644
@@ -12,7 +12,7 @@
    extern EGLDisplay *qemu_egl_display;
    extern EGLConfig qemu_egl_config;
    extern DisplayGLMode qemu_egl_mode;
-extern bool qemu_egl_angle_d3d;
+extern void *qemu_egl_angle_native_device;

I guess ANGLE will not be relevant for Metal and leaving it will be misleading.

Quoted 4 lines

What is your suggestion? This is just to remove "D3D" from the variable name. If you want to remove "ANGLE" from the variable name as well, it may be misleading because this variable is only used with an ANGLE EGL backend.

Looking the usage of the variable in your tree, I realized this variable may not be necessary for this at all.

There are two use cases:

1. To determine if VIRGL_RENDERER_NATIVE_SHARE_TEXTURE needs to be set.
2. To pass the device used by ANGLE to Cocoa.

Regarding 1, virglrenderer can simply pass MTLTexture whenever the EGL context is backed with Metal and Venus is in use.

Although your code is modeled after the code dealing with Direct3D handles, the functionality of your code is quite a different from it.

Direct3D handles are used to "share" texture with other processes in a zero-copy manner. It is an optional hint; dbus can fall back to use OpenGL textures if they are missing, and the other displays just don't care.

Quoted 8 lines
Quoted 2 lines

On the other hand, MTLTexture plays an essential role in your scenario. There are no corresponding OpenGL texture so no fallback happens.

That's a good point, but there's two uses of MTLTexture: one as a direct parallel to D3D handles (vrend allocate a texture backed by MTLTexture; returns it in info_ext along with a tex_id) and one returned as part of the new API (virgl_renderer_create_handle_for_scanout) which does not have an associated tex_id.

Quoted 3 lines

Thanks for clarification. I think the point I mentioned below still holds; virglrenderer does not have any code that can be skipped for MTLTexture with the absence of VIRGL_RENDERER_NATIVE_SHARE_TEXTURE.

We support the same "hint" use case in vrend for MTLTexture. So, without that flag set, there will be no attempt to allocate a MTLTexture. This works regardless if Venus is used or not. However, in the Venus use case, the hint does not matter at all because as long as the new API virgl_renderer_create_handle_for_scanout is used, a MTLTexture is returned. In this sense, I think it makes sense to rename *D3D* to *NATIVE*.

I agree with point 2 though, it is better to pass the native device handle from the Cocoa backend. I will wait for those changes to be upstreamed.

Quoted 45 lines
Quoted 22 lines
Quoted 22 lines

The difference shows that the VIRGL_RENDERER_NATIVE_SHARE_TEXTURE flag is useful when dealing with Direct3D handles but not with MTLTexture. The absence of flag tells virglrenderer that we do not need Direct3D handle (because the dbus display is not used), and allows it to skip some code to convert OpenGL textures to Direct3D handles).

On the other hand, not passing MTLTexture in your scenario does not make sense because it prevents the scanout at all. virglrenderer does not have any code that can be skipped when the flag is absent. virglrenderer can simply pass MTLTexture in your scenario.

2 implicitly lets ANGLE choose a device, but this can be suboptimal. Ideally, Cocoa should instead choose a device because it knows what device displays the window. Cocoa should pass the device it chose to ANGLE.

To demonstrate the point, I updated my tree to use ANGLE's Metal renderer for OpenGL and to pass the device from Cocoa to ANGLE:
https://github.com/akihikodaki/v/commit/8f191a7d2c225fa601c22a783c24c81a3fda47fa

Quoted 6 lines
Quoted 3 lines

By the way, I had to make a few modifications to ANGLE, Epoxy, and virglrenderer to fix glitches when hosting glmark2 and Firefox on GNOME. You can also find them with the above URL if you are interested.

Thank you, we picked up a couple of your changes in the UTM branch already. Do you have plans to upstream those changes? And also, what are your plans for upstreaming the Cocoa GL backend changes for QEMU?

It depends on changes.

I intend to upstream the Cocoa change and the following virlgrenderer changes once the libepoxy change to enable ANGLE is merged, which is blocking forever... They are useless without the libepoxy change:
https://github.com/akihikodaki/virglrenderer/commit/6dfbc7e61a72347485c09a171c8c6c0908e12a2a
https://github.com/akihikodaki/virglrenderer/commit/b12bda2fd272b0ffd0a9b95a021e4a13e947749b

I intend to upstream all two changes for ANGLE:
https://github.com/akihikodaki/angle/commits/v/

The other changes are already submitted to the upstream if I don't miss anything.

Regards,
Akihiko Odaki

#8 lore raw

Re: [PATCH RFC 1/7] egl-helpers: store handle to native device

Quoted 70 lines · Joelle van Dyne
Quoted 65 lines · Akihiko Odaki
Quoted 54 lines · Joelle van Dyne
Quoted 52 lines · Akihiko Odaki
Quoted 28 lines · Joelle van Dyne
Quoted 13 lines · Akihiko Odaki
Quoted 11 lines · Joelle van Dyne

Make way for other platforms by making the variable more general. Also we will be using the device in the future so let's save the pointer in the global instead of just a boolean flag.

---
     include/ui/egl-helpers.h      | 2 +-
     hw/display/virtio-gpu-virgl.c | 2 +-
     ui/egl-helpers.c              | 4 ++--
     3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 13 lines

include/ui/egl-helpers.h line 15

Quoted 10 lines
include/ui/egl-helpers.h +1 -1
index acf993fcf5..c239d32317 100644
@@ -12,7 +12,7 @@
     extern EGLDisplay *qemu_egl_display;
     extern EGLConfig qemu_egl_config;
     extern DisplayGLMode qemu_egl_mode;
-extern bool qemu_egl_angle_d3d;
+extern void *qemu_egl_angle_native_device;

I guess ANGLE will not be relevant for Metal and leaving it will be misleading.

Quoted 4 lines

What is your suggestion? This is just to remove "D3D" from the variable name. If you want to remove "ANGLE" from the variable name as well, it may be misleading because this variable is only used with an ANGLE EGL backend.

Looking the usage of the variable in your tree, I realized this variable may not be necessary for this at all.

There are two use cases:

1. To determine if VIRGL_RENDERER_NATIVE_SHARE_TEXTURE needs to be set.
2. To pass the device used by ANGLE to Cocoa.

Regarding 1, virglrenderer can simply pass MTLTexture whenever the EGL context is backed with Metal and Venus is in use.

Although your code is modeled after the code dealing with Direct3D handles, the functionality of your code is quite a different from it.

Direct3D handles are used to "share" texture with other processes in a zero-copy manner. It is an optional hint; dbus can fall back to use OpenGL textures if they are missing, and the other displays just don't care.

Quoted 8 lines
Quoted 2 lines

On the other hand, MTLTexture plays an essential role in your scenario. There are no corresponding OpenGL texture so no fallback happens.

That's a good point, but there's two uses of MTLTexture: one as a direct parallel to D3D handles (vrend allocate a texture backed by MTLTexture; returns it in info_ext along with a tex_id) and one returned as part of the new API (virgl_renderer_create_handle_for_scanout) which does not have an associated tex_id.

Quoted 3 lines

Thanks for clarification. I think the point I mentioned below still holds; virglrenderer does not have any code that can be skipped for MTLTexture with the absence of VIRGL_RENDERER_NATIVE_SHARE_TEXTURE.

Quoted 7 lines

We support the same "hint" use case in vrend for MTLTexture. So, without that flag set, there will be no attempt to allocate a MTLTexture. This works regardless if Venus is used or not. However, in the Venus use case, the hint does not matter at all because as long as the new API virgl_renderer_create_handle_for_scanout is used, a MTLTexture is returned. In this sense, I think it makes sense to rename *D3D* to *NATIVE*.

MTLTexture is always allocated; If virglrenderer doesn't, ANGLE does that internally. There should be no practical difference whether MTLTexture is exposed to QEMU or not.

So I think the only thing matters here is code complexity. MTLTexture should be exposed to QEMU if it makes the code simpler. If it complicates the code, it shouldn't be done. No flag is needed either way.

Regards,
Akihiko Odaki

#9 lore raw

Re: [PATCH RFC 1/7] egl-helpers: store handle to native device

Quoted 80 lines · Akihiko Odaki
Quoted 70 lines · Joelle van Dyne
Quoted 65 lines · Akihiko Odaki
Quoted 54 lines · Joelle van Dyne
Quoted 52 lines · Akihiko Odaki
Quoted 28 lines · Joelle van Dyne
Quoted 13 lines · Akihiko Odaki
Quoted 11 lines · Joelle van Dyne

Make way for other platforms by making the variable more general. Also we will be using the device in the future so let's save the pointer in the global instead of just a boolean flag.

---
     include/ui/egl-helpers.h      | 2 +-
     hw/display/virtio-gpu-virgl.c | 2 +-
     ui/egl-helpers.c              | 4 ++--
     3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 13 lines

include/ui/egl-helpers.h line 15

Quoted 10 lines
include/ui/egl-helpers.h +1 -1
index acf993fcf5..c239d32317 100644
@@ -12,7 +12,7 @@
     extern EGLDisplay *qemu_egl_display;
     extern EGLConfig qemu_egl_config;
     extern DisplayGLMode qemu_egl_mode;
-extern bool qemu_egl_angle_d3d;
+extern void *qemu_egl_angle_native_device;

I guess ANGLE will not be relevant for Metal and leaving it will be misleading.

Quoted 4 lines

What is your suggestion? This is just to remove "D3D" from the variable name. If you want to remove "ANGLE" from the variable name as well, it may be misleading because this variable is only used with an ANGLE EGL backend.

Looking the usage of the variable in your tree, I realized this variable may not be necessary for this at all.

There are two use cases:

1. To determine if VIRGL_RENDERER_NATIVE_SHARE_TEXTURE needs to be set.
2. To pass the device used by ANGLE to Cocoa.

Regarding 1, virglrenderer can simply pass MTLTexture whenever the EGL context is backed with Metal and Venus is in use.

Although your code is modeled after the code dealing with Direct3D handles, the functionality of your code is quite a different from it.

Direct3D handles are used to "share" texture with other processes in a zero-copy manner. It is an optional hint; dbus can fall back to use OpenGL textures if they are missing, and the other displays just don't care.

Quoted 8 lines
Quoted 2 lines

On the other hand, MTLTexture plays an essential role in your scenario. There are no corresponding OpenGL texture so no fallback happens.

That's a good point, but there's two uses of MTLTexture: one as a direct parallel to D3D handles (vrend allocate a texture backed by MTLTexture; returns it in info_ext along with a tex_id) and one returned as part of the new API (virgl_renderer_create_handle_for_scanout) which does not have an associated tex_id.

Quoted 3 lines

Thanks for clarification. I think the point I mentioned below still holds; virglrenderer does not have any code that can be skipped for MTLTexture with the absence of VIRGL_RENDERER_NATIVE_SHARE_TEXTURE.

Quoted 7 lines

We support the same "hint" use case in vrend for MTLTexture. So, without that flag set, there will be no attempt to allocate a MTLTexture. This works regardless if Venus is used or not. However, in the Venus use case, the hint does not matter at all because as long as the new API virgl_renderer_create_handle_for_scanout is used, a MTLTexture is returned. In this sense, I think it makes sense to rename *D3D* to *NATIVE*.

Quoted 3 lines

MTLTexture is always allocated; If virglrenderer doesn't, ANGLE does that internally. There should be no practical difference whether MTLTexture is exposed to QEMU or not.

The difference is that if virglrenderer creates it and exposes it to QEMU, then the UI can use a faster blit path. When the flag is set, virglrenderer creates the MTLTexture, passes it to ANGLE, and holds a reference for QEMU to retrieve. When the flag is not set, there is no way of getting the MTLTexture from ANGLE.

Quoted 7 lines

So I think the only thing matters here is code complexity. MTLTexture should be exposed to QEMU if it makes the code simpler. If it complicates the code, it shouldn't be done. No flag is needed either way.

Regards,
Akihiko Odaki

#10 lore raw

Re: [PATCH RFC 1/7] egl-helpers: store handle to native device

Quoted 82 lines · Joelle van Dyne
Quoted 80 lines · Akihiko Odaki
Quoted 77 lines · Joelle van Dyne
Quoted 65 lines · Akihiko Odaki
Quoted 54 lines · Joelle van Dyne
Quoted 52 lines · Akihiko Odaki
Quoted 28 lines · Joelle van Dyne
Quoted 13 lines · Akihiko Odaki
Quoted 11 lines · Joelle van Dyne

Make way for other platforms by making the variable more general. Also we will be using the device in the future so let's save the pointer in the global instead of just a boolean flag.

---
      include/ui/egl-helpers.h      | 2 +-
      hw/display/virtio-gpu-virgl.c | 2 +-
      ui/egl-helpers.c              | 4 ++--
      3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 13 lines

include/ui/egl-helpers.h line 15

Quoted 10 lines
include/ui/egl-helpers.h +1 -1
index acf993fcf5..c239d32317 100644
@@ -12,7 +12,7 @@
      extern EGLDisplay *qemu_egl_display;
      extern EGLConfig qemu_egl_config;
      extern DisplayGLMode qemu_egl_mode;
-extern bool qemu_egl_angle_d3d;
+extern void *qemu_egl_angle_native_device;

I guess ANGLE will not be relevant for Metal and leaving it will be misleading.

Quoted 4 lines

What is your suggestion? This is just to remove "D3D" from the variable name. If you want to remove "ANGLE" from the variable name as well, it may be misleading because this variable is only used with an ANGLE EGL backend.

Looking the usage of the variable in your tree, I realized this variable may not be necessary for this at all.

There are two use cases:

1. To determine if VIRGL_RENDERER_NATIVE_SHARE_TEXTURE needs to be set.
2. To pass the device used by ANGLE to Cocoa.

Regarding 1, virglrenderer can simply pass MTLTexture whenever the EGL context is backed with Metal and Venus is in use.

Although your code is modeled after the code dealing with Direct3D handles, the functionality of your code is quite a different from it.

Direct3D handles are used to "share" texture with other processes in a zero-copy manner. It is an optional hint; dbus can fall back to use OpenGL textures if they are missing, and the other displays just don't care.

Quoted 8 lines
Quoted 2 lines

On the other hand, MTLTexture plays an essential role in your scenario. There are no corresponding OpenGL texture so no fallback happens.

That's a good point, but there's two uses of MTLTexture: one as a direct parallel to D3D handles (vrend allocate a texture backed by MTLTexture; returns it in info_ext along with a tex_id) and one returned as part of the new API (virgl_renderer_create_handle_for_scanout) which does not have an associated tex_id.

Quoted 3 lines

Thanks for clarification. I think the point I mentioned below still holds; virglrenderer does not have any code that can be skipped for MTLTexture with the absence of VIRGL_RENDERER_NATIVE_SHARE_TEXTURE.

We support the same "hint" use case in vrend for MTLTexture. So, without that flag set, there will be no attempt to allocate a MTLTexture. This works regardless if Venus is used or not. However, in the Venus use case, the hint does not matter at all because as long as the new API virgl_renderer_create_handle_for_scanout is used, a MTLTexture is returned. In this sense, I think it makes sense to rename *D3D* to *NATIVE*.

Quoted 8 lines
Quoted 3 lines

MTLTexture is always allocated; If virglrenderer doesn't, ANGLE does that internally. There should be no practical difference whether MTLTexture is exposed to QEMU or not.

The difference is that if virglrenderer creates it and exposes it to QEMU, then the UI can use a faster blit path. When the flag is set, virglrenderer creates the MTLTexture, passes it to ANGLE, and holds a reference for QEMU to retrieve. When the flag is not set, there is no way of getting the MTLTexture from ANGLE.

I suspect it is a premature optimization. There is only one blit per frame and it is a simple blit, so I don't think it can create a perceivable performance uplift.

Besides, if the optimization turns out useful, you can still unconditionally let virglrenderer create the MTLTexture, so the flag is unnecessary.

Regards,
Akihiko Odaki

#11 lore raw

Re: [PATCH RFC 1/7] egl-helpers: store handle to native device

Quoted 96 lines · Akihiko Odaki
Quoted 82 lines · Joelle van Dyne
Quoted 80 lines · Akihiko Odaki
Quoted 77 lines · Joelle van Dyne
Quoted 65 lines · Akihiko Odaki
Quoted 54 lines · Joelle van Dyne
Quoted 52 lines · Akihiko Odaki
Quoted 28 lines · Joelle van Dyne
Quoted 13 lines · Akihiko Odaki
Quoted 11 lines · Joelle van Dyne

Make way for other platforms by making the variable more general. Also we will be using the device in the future so let's save the pointer in the global instead of just a boolean flag.

---
    include/ui/egl-helpers.h      | 2 +-
    hw/display/virtio-gpu-virgl.c | 2 +-
    ui/egl-helpers.c              | 4 ++--
    3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 13 lines

include/ui/egl-helpers.h line 15

Quoted 10 lines
include/ui/egl-helpers.h +1 -1
index acf993fcf5..c239d32317 100644
@@ -12,7 +12,7 @@
    extern EGLDisplay *qemu_egl_display;
    extern EGLConfig qemu_egl_config;
    extern DisplayGLMode qemu_egl_mode;
-extern bool qemu_egl_angle_d3d;
+extern void *qemu_egl_angle_native_device;

I guess ANGLE will not be relevant for Metal and leaving it will be misleading.

Quoted 4 lines

What is your suggestion? This is just to remove "D3D" from the variable name. If you want to remove "ANGLE" from the variable name as well, it may be misleading because this variable is only used with an ANGLE EGL backend.

Looking the usage of the variable in your tree, I realized this variable may not be necessary for this at all.

There are two use cases:

1. To determine if VIRGL_RENDERER_NATIVE_SHARE_TEXTURE needs to be set.
2. To pass the device used by ANGLE to Cocoa.

Regarding 1, virglrenderer can simply pass MTLTexture whenever the EGL context is backed with Metal and Venus is in use.

Although your code is modeled after the code dealing with Direct3D handles, the functionality of your code is quite a different from it.

Direct3D handles are used to "share" texture with other processes in a zero-copy manner. It is an optional hint; dbus can fall back to use OpenGL textures if they are missing, and the other displays just don't care.

Quoted 8 lines
Quoted 2 lines

On the other hand, MTLTexture plays an essential role in your scenario. There are no corresponding OpenGL texture so no fallback happens.

That's a good point, but there's two uses of MTLTexture: one as a direct parallel to D3D handles (vrend allocate a texture backed by MTLTexture; returns it in info_ext along with a tex_id) and one returned as part of the new API (virgl_renderer_create_handle_for_scanout) which does not have an associated tex_id.

Quoted 3 lines

Thanks for clarification. I think the point I mentioned below still holds; virglrenderer does not have any code that can be skipped for MTLTexture with the absence of VIRGL_RENDERER_NATIVE_SHARE_TEXTURE.

We support the same "hint" use case in vrend for MTLTexture. So, without that flag set, there will be no attempt to allocate a MTLTexture. This works regardless if Venus is used or not. However, in the Venus use case, the hint does not matter at all because as long as the new API virgl_renderer_create_handle_for_scanout is used, a MTLTexture is returned. In this sense, I think it makes sense to rename *D3D* to *NATIVE*.

Quoted 8 lines
Quoted 3 lines

MTLTexture is always allocated; If virglrenderer doesn't, ANGLE does that internally. There should be no practical difference whether MTLTexture is exposed to QEMU or not.

The difference is that if virglrenderer creates it and exposes it to QEMU, then the UI can use a faster blit path. When the flag is set, virglrenderer creates the MTLTexture, passes it to ANGLE, and holds a reference for QEMU to retrieve. When the flag is not set, there is no way of getting the MTLTexture from ANGLE.

Quoted 7 lines

I suspect it is a premature optimization. There is only one blit per frame and it is a simple blit, so I don't think it can create a perceivable performance uplift.

Besides, if the optimization turns out useful, you can still unconditionally let virglrenderer create the MTLTexture, so the flag is unnecessary.

Regards,
Akihiko Odaki

Hello,

It might be worthwhile having a way to do partial damage from the get-go - which entails not doing full blits each frame. It has its importance for battery life.

Although there are more aspects around that still look in flux within virglrenderer… and ANGLE (at least on Vulkan) has a stub implementation of EGL_KHR_partial_update that doesn’t actually implement partial damage...

#12 lore raw

Re: [PATCH RFC 1/7] egl-helpers: store handle to native device

Quoted 104 lines · Mohamed Mediouni
Quoted 96 lines · Akihiko Odaki
Quoted 82 lines · Joelle van Dyne
Quoted 80 lines · Akihiko Odaki
Quoted 77 lines · Joelle van Dyne
Quoted 65 lines · Akihiko Odaki
Quoted 54 lines · Joelle van Dyne
Quoted 52 lines · Akihiko Odaki
Quoted 28 lines · Joelle van Dyne
Quoted 13 lines · Akihiko Odaki
Quoted 11 lines · Joelle van Dyne

Make way for other platforms by making the variable more general. Also we will be using the device in the future so let's save the pointer in the global instead of just a boolean flag.

---
     include/ui/egl-helpers.h      | 2 +-
     hw/display/virtio-gpu-virgl.c | 2 +-
     ui/egl-helpers.c              | 4 ++--
     3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 13 lines

include/ui/egl-helpers.h line 15

Quoted 10 lines
include/ui/egl-helpers.h +1 -1
index acf993fcf5..c239d32317 100644
@@ -12,7 +12,7 @@
     extern EGLDisplay *qemu_egl_display;
     extern EGLConfig qemu_egl_config;
     extern DisplayGLMode qemu_egl_mode;
-extern bool qemu_egl_angle_d3d;
+extern void *qemu_egl_angle_native_device;

I guess ANGLE will not be relevant for Metal and leaving it will be misleading.

Quoted 4 lines

What is your suggestion? This is just to remove "D3D" from the variable name. If you want to remove "ANGLE" from the variable name as well, it may be misleading because this variable is only used with an ANGLE EGL backend.

Looking the usage of the variable in your tree, I realized this variable may not be necessary for this at all.

There are two use cases:

1. To determine if VIRGL_RENDERER_NATIVE_SHARE_TEXTURE needs to be set.
2. To pass the device used by ANGLE to Cocoa.

Regarding 1, virglrenderer can simply pass MTLTexture whenever the EGL context is backed with Metal and Venus is in use.

Although your code is modeled after the code dealing with Direct3D handles, the functionality of your code is quite a different from it.

Direct3D handles are used to "share" texture with other processes in a zero-copy manner. It is an optional hint; dbus can fall back to use OpenGL textures if they are missing, and the other displays just don't care.

Quoted 8 lines
Quoted 2 lines

On the other hand, MTLTexture plays an essential role in your scenario. There are no corresponding OpenGL texture so no fallback happens.

That's a good point, but there's two uses of MTLTexture: one as a direct parallel to D3D handles (vrend allocate a texture backed by MTLTexture; returns it in info_ext along with a tex_id) and one returned as part of the new API (virgl_renderer_create_handle_for_scanout) which does not have an associated tex_id.

Quoted 3 lines

Thanks for clarification. I think the point I mentioned below still holds; virglrenderer does not have any code that can be skipped for MTLTexture with the absence of VIRGL_RENDERER_NATIVE_SHARE_TEXTURE.

We support the same "hint" use case in vrend for MTLTexture. So, without that flag set, there will be no attempt to allocate a MTLTexture. This works regardless if Venus is used or not. However, in the Venus use case, the hint does not matter at all because as long as the new API virgl_renderer_create_handle_for_scanout is used, a MTLTexture is returned. In this sense, I think it makes sense to rename *D3D* to *NATIVE*.

Quoted 8 lines
Quoted 3 lines

MTLTexture is always allocated; If virglrenderer doesn't, ANGLE does that internally. There should be no practical difference whether MTLTexture is exposed to QEMU or not.

The difference is that if virglrenderer creates it and exposes it to QEMU, then the UI can use a faster blit path. When the flag is set, virglrenderer creates the MTLTexture, passes it to ANGLE, and holds a reference for QEMU to retrieve. When the flag is not set, there is no way of getting the MTLTexture from ANGLE.

Quoted 7 lines

I suspect it is a premature optimization. There is only one blit per frame and it is a simple blit, so I don't think it can create a perceivable performance uplift.

Besides, if the optimization turns out useful, you can still unconditionally let virglrenderer create the MTLTexture, so the flag is unnecessary.

Regards,
Akihiko Odaki

Quoted 6 lines

Hello,

It might be worthwhile having a way to do partial damage from the get-go - which entails not doing full blits each frame. It has its importance for battery life.

Although there are more aspects around that still look in flux within virglrenderer… and ANGLE (at least on Vulkan) has a stub implementation of EGL_KHR_partial_update that doesn’t actually implement partial damage...

It is better to look at the Metal API instead; after all, we use ANGLE's Metal renderer so ANGLE can only provide features Metal allows. If necessary, we can use the underlying MTLTexture as this series and the corresponding virglrenderer change does.

Searching the Web, I found partial damage is feasible with Metal, but there is a complication, CAMetalLayer implements double or triple buffering, which complicates damage calculation. Flutter's issue for partial damage and its corresponding pull request details the problem and their solution:
https://github.com/flutter/flutter/issues/124526

The issue also shows the CPU usage is dramatically reduced and the optimization is worthwhile (though I wonder why; it is mostly GPU work and the CPU shouldn't be affected. Perhaps the memory bandwidth shared between CPU and GPU may affect the accounting).

In conclusion, partial damage is worthwhile to explore, but it should be done with a follow-up patch as it is not so simple that we can implement it by just passing the damaged area to Cocoa and Metal but it requires an additional logic. Keeping the initial patch small is essential for upstreaming.

And any optimization patch should come with some benchmark. Something simple is sufficient; e.g., the Flutter issue tells the CPU usage is dropped from 40% to 16% for "the blinking cursor in flutter gallery".

Regards,
Akihiko Odaki