From 1b5e22be23993400f4f30395bc94c4c7063d7c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Wed, 2 Apr 2025 01:01:04 +0200 Subject: [PATCH] macOS: Remove drawRect and use plain CALayer for raster windows The drawRect callback was used when we were supporting both surface- backed and layer-backed drawing, but nowadays we only support layer- backed drawing. However we couldn't just remove the implementation, as the default backing layer of an NSView is an NSViewBackingLayer, which in its override of [CALayer display] checks whether the view implements drawRect, and if not bails out, which meant we never reached our displayLayer callback from [CALayer display]. But, we don't need NSViewBackingLayer, as we handle layer updates and draw callbacks in a more generic way, to support CAMetalLayer, so instead we now use a plain CALayer as our backing layer for raster windows. Change-Id: I5b878ff51ca92dafcb80bf5a686ced13088cfa28 Reviewed-by: Timur Pocheptsov --- .../platforms/cocoa/qnsview_drawing.mm | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/plugins/platforms/cocoa/qnsview_drawing.mm b/src/plugins/platforms/cocoa/qnsview_drawing.mm index 51e6a19c488..7037368eeb7 100644 --- a/src/plugins/platforms/cocoa/qnsview_drawing.mm +++ b/src/plugins/platforms/cocoa/qnsview_drawing.mm @@ -85,7 +85,12 @@ } } - return [super makeBackingLayer]; + // We handle drawing via displayLayer instead of drawRect or updateLayer, + // as the latter two do not work for CAMetalLayer. And we handle content + // scale manually for the same reason. Which means we don't really need + // NSViewBackingLayer. In fact it just gets in the way, by assuming that + // if we don't have a drawRect function we "draw nothing". + return [CALayer layer]; } /* @@ -200,21 +205,9 @@ // ----------------------- Draw callbacks ----------------------- /* - This method is called by AppKit for the non-layer case, where we are - drawing into the NSWindow's surface. -*/ -- (void)drawRect:(NSRect)dirtyBoundingRect -{ - Q_UNUSED(dirtyBoundingRect); - // As we are layer backed we shouldn't really end up here, but AppKit will - // in some cases call this method just because we implement it. - // FIXME: Remove drawRect and switch from displayLayer to updateLayer - qCWarning(lcQpaDrawing) << "[QNSView drawRect] called for layer backed view"; -} - -/* - This method is called by AppKit when we are layer-backed, where - we are drawing into the layer. + We set our view up as the layer's delegate, which means we get + first dibs on displaying the layer, without needing to go through + updateLayer or drawRect. */ - (void)displayLayer:(CALayer *)layer {