macos - NSGraphicsContext becomes NULL after setting CAMetalLayer
问题描述
I trying to move my NSView based application to start using GPU with Metal. So, I added a CAMetalLayer
using like this:
[self setWantsLayer:YES];
self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawDuringViewResize;
And make the backing layer, like this:
- (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];
return backingLayer;
}
I write all this code in my CustomNSView
class is inherited from a CustomParentNSView
class which uses NSGraphicsContext
to draw some things on the screen. Now, since I am moving CustomNSView
to a layer backed NSView, when I try to call:
[NSGraphicsContext currentContext]
This returns NULL.
- Should this be NULL for layer backed NSViews?
I also tried investigating where in the previous implementation NSGraphicsContext
was getting generated, and it turns out, my application was setting it nowhere. NSView
was itself setting the NSGraphicsContext
from somewhere inside of displayIfNeeded
, before calling drawRect
.
- Can I still use NSGraphicsContext to render to NSView, even when it is layer backed?
Thanks!
解决方案
推荐阅读
- version-control - 如何在移动的目录上恢复 Perforce 历史记录
- python - 熊猫日期格式
- c# - 在 ASP.NET Core Web Api 中使用 JWT 授权检查 IP
- selenium-webdriver - 使用带有 Sendkeys 的动作类(“15/16”)后如何释放键
- android - 向 Okhttp 发送不同格式的 json
- amazon-web-services - 使用 root 用户(= 存储桶所有者)将存储桶策略放在 aws s3 存储桶上时访问被拒绝
- uwp - 如何在 UWP 中旋转 Textblock 后获得正确的宽度和高度
- html - 用内容扩展 div 宽度
- java - Levenshtein 算法中的缺失值和 NPE
- .net - 多个网站平台上的 GTM 和 GA 跟踪