Skip to contentSkip to conversation

ui/cocoa: Add cursor composition

PATCH Superseded Akihiko Odaki 2 participants 4 messages Mar 18, 2024 – Jun 25, 2024 Arm/Amlogic Meson SoC support

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover ui/cocoa: Add cursor composition

Add accelerated cursor composition to ui/cocoa. This does not only improve performance for display devices that exposes the capability to the guest according to dpy_cursor_define_supported(), but fixes the cursor display for devices that unconditionally expects the availability of the capability (e.g., virtio-gpu).

The common pattern to implement accelerated cursor composition is to replace the cursor and warp it so that the replaced cursor is shown at the correct position on the guest display. Unfortunately, ui/cocoa cannot do the same because warping the cursor position interfers with the mouse input so it uses CALayer instead; although it is not specialized for cursor composition, it still can compose images with hardware acceleration.

ui/cocoa was the only non-headless graphical display lacking cursor composition so dpy_cursor_define_supported() is no longer needed and removed.

---
Akihiko Odaki (2):
ui/cocoa: Add cursor composition ui/console: Remove dpy_cursor_define_supported()

 meson.build             |  3 +-
 include/ui/console.h    |  1 -
 hw/display/qxl-render.c |  4 --
 hw/display/vmware_vga.c |  6 +--
 ui/console.c            | 13 -------
 ui/cocoa.m              | 97 +++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 101 insertions(+), 23 deletions(-)

---
base-commit: ba49d760eb04630e7b15f423ebecf6c871b8f77b
change-id: 20240318-cursor-3491b1806582

Best regards,

Signature · 1 line
Akihiko Odaki <[email protected]>
#1 lore raw

PATCH 1/2 ui/cocoa: Add cursor composition

Add accelerated cursor composition to ui/cocoa. This does not only improve performance for display devices that exposes the capability to the guest according to dpy_cursor_define_supported(), but fixes the cursor display for devices that unconditionally expects the availability of the capability (e.g., virtio-gpu).

The common pattern to implement accelerated cursor composition is to replace the cursor and warp it so that the replaced cursor is shown at the correct position on the guest display. Unfortunately, ui/cocoa cannot do the same because warping the cursor position interfers with the mouse input so it uses CALayer instead; although it is not specialized for cursor composition, it still can compose images with hardware acceleration.

---
 meson.build |  3 +-
 ui/cocoa.m  | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+), 1 deletion(-)
meson.build +2 -1
index b375248a7614..b198ca2972ed 100644
@@ -1044,7 +1044,8 @@ if get_option('attr').allowed()
   endif
 endif
 
-cocoa = dependency('appleframeworks', modules: ['Cocoa', 'CoreVideo'],
+cocoa = dependency('appleframeworks',
+                   modules: ['Cocoa', 'CoreVideo', 'QuartzCore'],
                    required: get_option('cocoa'))
 
 vmnet = dependency('appleframeworks', modules: 'vmnet', required: get_option('vmnet'))
ui/cocoa.m +97 -0

139 lines not on this page · raw message

Signature · 1 line
2.44.0
#2 lore raw
Quoted 7 lines · Akihiko Odaki

The common pattern to implement accelerated cursor composition is to replace the cursor and warp it so that the replaced cursor is shown at the correct position on the guest display. Unfortunately, ui/cocoa cannot do the same because warping the cursor position interfers with the mouse input so it uses CALayer instead; although it is not specialized for cursor composition, it still can compose images with hardware acceleration.

As discussed in another thread, this limitation doesn't apply when using absolute pointer input. I much prefer "native" cursor rendering, especially in the absolute pointer case, but this is definitely an improvement over no cursor support at all.

Perhaps some others can chip in with preferences on the way forward.

ui/cocoa.m @interface QemuCocoaView : NSView · line 322

Quoted 6 lines
ui/cocoa.m +2 -0
@@ -313,6 +318,11 @@ @interface QemuCocoaView : NSView
     BOOL isMouseGrabbed;
     BOOL isAbsoluteEnabled;
     CFMachPortRef eventsTap;
+    CALayer *cursorLayer;
+    QEMUCursor *cursor;

As far as I can see, these can leak on dealloc.

ui/cocoa.m @interface QemuCocoaView : NSView · line 325

Quoted 3 lines
ui/cocoa.m +3 -0
+    int mouseX;
+    int mouseY;
+    int mouseOn;

Any reason not to use a boolean type (bool or BOOL) for this?

ui/cocoa.m - (void) unhideCursor · line 513

Quoted 10 lines
ui/cocoa.m +9 -0
+- (void)setCursor:(QEMUCursor *)given_cursor
+{
[…]
+    image = CGImageCreate(
+        cursor->width, //width
+        cursor->height, //height
+        8, //bitsPerComponent
+        32, //bitsPerPixel
+        cursor->width * 4, //bytesPerRow
+        CGColorSpaceCreateWithName(kCGColorSpaceSRGB), //colorspace

This leaks the colour space object; CGImageCreate unfortunately does not consume the reference.

Quoted 5 lines
ui/cocoa.m +5 -0
+        kCGBitmapByteOrder32Little | kCGImageAlphaFirst, //bitmapInfo
+        provider, //provider
+        NULL, //decode
+        0, //interpolate
+        kCGRenderingIntentDefault //intent

ui/cocoa.m - (void) unhideCursor · line 528

Quoted 10 lines
ui/cocoa.m +10 -0
+    );
+
+    CGDataProviderRelease(provider);
+    [CATransaction begin];
+    [CATransaction setDisableActions:YES];
+    [cursorLayer setBounds:bounds];
+    [cursorLayer setContents:(id)image];
+    [CATransaction commit];
+    CGImageRelease(image);
+}

This method also runs on for quite a bit. I'd prefer some parts broken out into a helper function or two.

#3 lore raw

PATCH 2/2 ui/console: Remove dpy_cursor_define_supported()

In reply to Akihiko Odaki

Remove dpy_cursor_define_supported() as it brings no benefit today and it has a few inherent problems.

All graphical displays except egl-headless support cursor composition without DMA-BUF, and egl-headless is meant to be used in conjunction with another graphical display, so dpy_cursor_define_supported() always returns true and meaningless.

Even if we add a new display without cursor composition in the future, dpy_cursor_define_supported() will be problematic as a cursor display fix for it because some display devices like virtio-gpu cannot tell the lack of cursor composition capability to the guest and are unable to utilize the value the function returns. Therefore, all non-headless graphical displays must actually implement cursor composition for correct cursor display.

Another problem with dpy_cursor_define_supported() is that it returns true even if only some of the display listeners support cursor composition, which is wrong unless all display listeners that lack cursor composition is headless.

---
 include/ui/console.h    |  1 -
 hw/display/qxl-render.c |  4 ----
 hw/display/vmware_vga.c |  6 ++----
 ui/console.c            | 13 -------------
 4 files changed, 2 insertions(+), 22 deletions(-)
include/ui/console.h +0 -1
index a4a49ffc640c..7566282be986 100644
@@ -342,7 +342,6 @@ void dpy_text_update(QemuConsole *con, int x, int y, int w, int h);
 void dpy_text_resize(QemuConsole *con, int w, int h);
 void dpy_mouse_set(QemuConsole *con, int x, int y, int on);
 void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor);
-bool dpy_cursor_define_supported(QemuConsole *con);
 bool dpy_gfx_check_format(QemuConsole *con,
                           pixman_format_code_t format);
 
hw/display/qxl-render.c +0 -4
index ec99ec887a6e..837d2446cd52 100644
@@ -307,10 +307,6 @@ int qxl_render_cursor(PCIQXLDevice *qxl, QXLCommandExt *ext)
         return 1;
     }
 
-    if (!dpy_cursor_define_supported(qxl->vga.con)) {
-        return 0;
-    }
-
     if (qxl->debug > 1 && cmd->type != QXL_CURSOR_MOVE) {
         fprintf(stderr, "%s", __func__);
         qxl_log_cmd_cursor(qxl, cmd, ext->group_id);
hw/display/vmware_vga.c +2 -4
index 1c0f9d9a991d..fadfefc4c67c 100644
@@ -904,10 +904,8 @@ static uint32_t vmsvga_value_read(void *opaque, uint32_t address)
         caps |= SVGA_CAP_RECT_FILL;
 #endif
 #ifdef HW_MOUSE_ACCEL
-        if (dpy_cursor_define_supported(s->vga.con)) {
-            caps |= SVGA_CAP_CURSOR | SVGA_CAP_CURSOR_BYPASS_2 |
-                    SVGA_CAP_CURSOR_BYPASS;
-        }
+        caps |= SVGA_CAP_CURSOR | SVGA_CAP_CURSOR_BYPASS_2 |
+                SVGA_CAP_CURSOR_BYPASS;
 #endif
         ret = caps;
         break;
ui/console.c +0 -13
index 832055675c50..6b6f57441c53 100644
@@ -1058,19 +1058,6 @@ void dpy_cursor_define(QemuConsole *c, QEMUCursor *cursor)
     }
 }
 
-bool dpy_cursor_define_supported(QemuConsole *con)
-{
-    DisplayState *s = con->ds;
-    DisplayChangeListener *dcl;
-
-    QLIST_FOREACH(dcl, &s->listeners, next) {
-        if (dcl->ops->dpy_cursor_define) {
-            return true;
-        }
-    }
-    return false;
-}
-
 QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
                                 struct QEMUGLParams *qparams)
 {
Signature · 1 line
2.44.0