* [PATCH 1/2] ui/cocoa: Add cursor composition
2024-03-18 7:58 [PATCH 0/2] ui/cocoa: Add cursor composition Akihiko Odaki
@ 2024-03-18 7:58 ` Akihiko Odaki
2024-06-25 14:51 ` Phil Dennis-Jordan
2024-03-18 7:58 ` [PATCH 2/2] ui/console: Remove dpy_cursor_define_supported() Akihiko Odaki
1 sibling, 1 reply; 4+ messages in thread
From: Akihiko Odaki @ 2024-03-18 7:58 UTC (permalink / raw)
To: Paolo Bonzini, Marc-André Lureau, Daniel P. Berrangé,
Thomas Huth, Philippe Mathieu-Daudé, Peter Maydell,
Gerd Hoffmann, Dmitry Fleytman
Cc: qemu-devel, Akihiko Odaki
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.
Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
meson.build | 3 +-
ui/cocoa.m | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 99 insertions(+), 1 deletion(-)
diff --git a/meson.build b/meson.build
index b375248a7614..b198ca2972ed 100644
--- a/meson.build
+++ b/meson.build
@@ -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'))
diff --git a/ui/cocoa.m b/ui/cocoa.m
index fa879d7dcd4b..c13c2a01e947 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -25,6 +25,7 @@
#include "qemu/osdep.h"
#import <Cocoa/Cocoa.h>
+#import <QuartzCore/QuartzCore.h>
#include <crt_externs.h>
#include "qemu/help-texts.h"
@@ -92,12 +93,16 @@ static void cocoa_switch(DisplayChangeListener *dcl,
DisplaySurface *surface);
static void cocoa_refresh(DisplayChangeListener *dcl);
+static void cocoa_mouse_set(DisplayChangeListener *dcl, int x, int y, int on);
+static void cocoa_cursor_define(DisplayChangeListener *dcl, QEMUCursor *cursor);
static const DisplayChangeListenerOps dcl_ops = {
.dpy_name = "cocoa",
.dpy_gfx_update = cocoa_update,
.dpy_gfx_switch = cocoa_switch,
.dpy_refresh = cocoa_refresh,
+ .dpy_mouse_set = cocoa_mouse_set,
+ .dpy_cursor_define = cocoa_cursor_define,
};
static DisplayChangeListener dcl = {
.ops = &dcl_ops,
@@ -313,6 +318,11 @@ @interface QemuCocoaView : NSView
BOOL isMouseGrabbed;
BOOL isAbsoluteEnabled;
CFMachPortRef eventsTap;
+ CALayer *cursorLayer;
+ QEMUCursor *cursor;
+ int mouseX;
+ int mouseY;
+ int mouseOn;
}
- (void) switchSurface:(pixman_image_t *)image;
- (void) grabMouse;
@@ -365,6 +375,12 @@ - (id)initWithFrame:(NSRect)frameRect
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_14_0
[self setClipsToBounds:YES];
#endif
+ [self setWantsLayer:YES];
+ cursorLayer = [[CALayer alloc] init];
+ [cursorLayer setAnchorPoint:CGPointMake(0, 1)];
+ [cursorLayer setAutoresizingMask:kCALayerMaxXMargin |
+ kCALayerMinYMargin];
+ [[self layer] addSublayer:cursorLayer];
}
return self;
@@ -445,6 +461,72 @@ - (void) unhideCursor
[NSCursor unhide];
}
+- (void)setMouseX:(int)x y:(int)y on:(int)on
+{
+ CGPoint position;
+
+ mouseX = x;
+ mouseY = y;
+ mouseOn = on;
+
+ position.x = mouseX;
+ position.y = screen.height - mouseY;
+
+ [CATransaction begin];
+ [CATransaction setDisableActions:YES];
+ [cursorLayer setPosition:position];
+ [cursorLayer setHidden:!mouseOn];
+ [CATransaction commit];
+}
+
+- (void)setCursor:(QEMUCursor *)given_cursor
+{
+ CGDataProviderRef provider;
+ CGImageRef image;
+ CGRect bounds = CGRectZero;
+
+ cursor_unref(cursor);
+ cursor = given_cursor;
+
+ if (!cursor) {
+ return;
+ }
+
+ cursor_ref(cursor);
+
+ bounds.size.width = cursor->width;
+ bounds.size.height = cursor->height;
+
+ provider = CGDataProviderCreateWithData(
+ NULL,
+ cursor->data,
+ cursor->width * cursor->height * 4,
+ NULL
+ );
+
+ image = CGImageCreate(
+ cursor->width, //width
+ cursor->height, //height
+ 8, //bitsPerComponent
+ 32, //bitsPerPixel
+ cursor->width * 4, //bytesPerRow
+ CGColorSpaceCreateWithName(kCGColorSpaceSRGB), //colorspace
+ kCGBitmapByteOrder32Little | kCGImageAlphaFirst, //bitmapInfo
+ provider, //provider
+ NULL, //decode
+ 0, //interpolate
+ kCGRenderingIntentDefault //intent
+ );
+
+ CGDataProviderRelease(provider);
+ [CATransaction begin];
+ [CATransaction setDisableActions:YES];
+ [cursorLayer setBounds:bounds];
+ [cursorLayer setContents:(id)image];
+ [CATransaction commit];
+ CGImageRelease(image);
+}
+
- (void) drawRect:(NSRect) rect
{
COCOA_DEBUG("QemuCocoaView: drawRect\n");
@@ -1982,6 +2064,21 @@ static void cocoa_refresh(DisplayChangeListener *dcl)
[pool release];
}
+static void cocoa_mouse_set(DisplayChangeListener *dcl, int x, int y, int on)
+{
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [cocoaView setMouseX:x y:y on:on];
+ });
+}
+
+static void cocoa_cursor_define(DisplayChangeListener *dcl, QEMUCursor *cursor)
+{
+ dispatch_async(dispatch_get_main_queue(), ^{
+ BQL_LOCK_GUARD();
+ [cocoaView setCursor:qemu_console_get_cursor(dcl->con)];
+ });
+}
+
static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
--
2.44.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/2] ui/console: Remove dpy_cursor_define_supported()
2024-03-18 7:58 [PATCH 0/2] ui/cocoa: Add cursor composition Akihiko Odaki
2024-03-18 7:58 ` [PATCH 1/2] " Akihiko Odaki
@ 2024-03-18 7:58 ` Akihiko Odaki
1 sibling, 0 replies; 4+ messages in thread
From: Akihiko Odaki @ 2024-03-18 7:58 UTC (permalink / raw)
To: Paolo Bonzini, Marc-André Lureau, Daniel P. Berrangé,
Thomas Huth, Philippe Mathieu-Daudé, Peter Maydell,
Gerd Hoffmann, Dmitry Fleytman
Cc: qemu-devel, 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.
Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
---
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(-)
diff --git a/include/ui/console.h b/include/ui/console.h
index a4a49ffc640c..7566282be986 100644
--- a/include/ui/console.h
+++ b/include/ui/console.h
@@ -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);
diff --git a/hw/display/qxl-render.c b/hw/display/qxl-render.c
index ec99ec887a6e..837d2446cd52 100644
--- a/hw/display/qxl-render.c
+++ b/hw/display/qxl-render.c
@@ -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);
diff --git a/hw/display/vmware_vga.c b/hw/display/vmware_vga.c
index 1c0f9d9a991d..fadfefc4c67c 100644
--- a/hw/display/vmware_vga.c
+++ b/hw/display/vmware_vga.c
@@ -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;
diff --git a/ui/console.c b/ui/console.c
index 832055675c50..6b6f57441c53 100644
--- a/ui/console.c
+++ b/ui/console.c
@@ -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)
{
--
2.44.0
^ permalink raw reply related [flat|nested] 4+ messages in thread