drawRect 没有被调用以使用 NSView 进行图层支持绘图

drawRect not getting called for layer backed drawing with NSView

我正在尝试将我的 Opengl 应用程序迁移到 Metal,并且我正在使用 CAMetalLayer 作为 Metal 实现。 我在继承的 NSView class 对象上设置了以下属性:

self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;
[self setWantsLayer:YES];

我已经检查了关于 SO 的其他答案,例如 NSView subclass - drawRect: not called

所以,我尝试按照上面的答案中的建议分配委托,就像这样

[self.layer setDelegate:self];

我得到以下编译错误:

Cannot initialize a parameter of type 'id<CALayerDelegate> _Nullable' with an lvalue of type 'MyNSView *'

此外,我还在 https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/SettingUpLayerObjects/SettingUpLayerObjects.html 看到了设置图层对象的文档,其中指出:

For layer-backed views with custom content, you should continue to override the view’s methods to do your drawing. A layer-backed view automatically makes itself the delegate of its layer and implements the needed delegate methods, and you should not change that configuration. Instead, you should implement your view’s drawRect: method to draw your content.

因此,据此,我什至不必在我的案例中设置委托,我已经在 MyNSView 中实现了 drawRect 函数。仍然没有被调用。

还有什么需要做的吗?

谢谢!

编辑: 因此,我通过让我的层遵循协议 CALayerDelegate 来修复编译错误,但我仍然没有收到任何对 drawLayerdrawRect.

的调用

制作背景图层

- (CALayer*)makeBackingLayer
{
    id <MTLDevice> device = MTLCreateSystemDefaultDevice();
    CAMetalLayer *backingLayer   = [CAMetalLayer layer];
    backingLayer.opaque          = YES;
    backingLayer.device          = device;
    backingLayer.pixelFormat     = MTLPixelFormatBGRA8Unorm;
    backingLayer.framebufferOnly = YES;

//    [backingLayer setDelegate:self]; //Tried setting the delegate 
                                       //here as well

    return backingLayer;
}

初始化函数

self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;
[self setWantsLayer:YES];
[self.layer setDelegate:self];

绘制函数

- (void)displayLayer:(CALayer *)layer
{
    [self drawRect:self.bounds];
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    [self drawRect:self.bounds];
}

-(void) drawRect: (NSRect) dirtyRect
{
    [super drawRect:dirtyRect];
}

我已经实现了所有绘制函数以检查是否有任何函数被击中,但我没有收到任何这些函数的调用。

为了将内容绘制到 NSView,我绘制到屏幕外资源并从 NSView 的 CALayer 获取 drawable 并在提交 commandBuffer 之前调用 presentDrawable

由于这里没有太多我可以调试以找到引入错误的地方,所以我开始研究指令的执行顺序。事实证明,您调用的顺序:

[self setWantsLayer:YES];
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;

导致了这个问题。交换这两个语句后,我开始收到对 displayLayer 函数的调用。我仍然无法弄清楚为什么 drawRect 没有被层支持的 NSView 调用。

张贴这个以防万一这个交换也解决了其他人的问题。