首页 > 解决方案 > NSOpenGLView 子类不在 Mojave 上呈现

问题描述

在针对 Mojave 框架使用 Xcode 10 进行编译时,我遇到了一个非常奇怪的错误。

事实证明,OpenGL 停止显示。看起来它实际上在某个地方(支持层覆盖它?)在快速更新窗口的可见区域(!)或最小化时可见(精灵效果使用的快照显示它!)

最奇怪的是,我在另一个项目中使用了几乎相同的类,并且在为 Mojave 构建时它工作正常!

我看到了为使用单独上下文的人发送更新到 OpenGLContext 的解决方案,但这是 NSOpenGLView 的一个简单子类。在这种情况下,Cocos2D 的 SpriteBuilder 的一个分支。

我尝试更新底层上下文,将视图设置为支持高分辨率并尝试防止创建支持层。到目前为止没有任何效果。

编码:

@implementation CCGLView {
    NSMutableArray *_fences;
}

- (id) initWithFrame:(NSRect)frameRect
{
    self = [self initWithFrame:frameRect shareContext:nil];
    return self;
}

- (id) initWithFrame:(NSRect)frameRect shareContext:(NSOpenGLContext*)context
{
    NSOpenGLPixelFormatAttribute attribs[] =
    {
        NSOpenGLPFADoubleBuffer,
        NSOpenGLPFADepthSize, 24,
        0
    };

    NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];

    if (!pixelFormat)
        CCLOG(@"No OpenGL pixel format");

    if ((self = [super initWithFrame:frameRect pixelFormat:pixelFormat])) {

        if (context) [self setOpenGLContext:context];

    }

    return self;
}

- (void) prepareOpenGL
{
    [super prepareOpenGL];

    // Make this openGL context current to the thread
    // (i.e. all openGL on this thread calls will go to this context)
    [[self openGLContext] makeCurrentContext];

    // Synchronize buffer swaps with vertical refresh rate
    GLint swapInt = 1;
    [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];  

}

- (NSUInteger) depthFormat
{
    return 24;
}

- (void) reshape
{
    [self lockOpenGLContext];

    NSRect rect = [self convertRectToBacking:self.bounds];

    CCDirector *director = [CCDirector sharedDirector];
    [director reshapeProjection: NSSizeToCGSize(rect.size) ];

    if (director.runningScene) { 
       [director drawScene];
    }
    [self unlockOpenGLContext];
}


- (void)lockOpenGLContext
{
    NSOpenGLContext *glContext = [self openGLContext];
    NSAssert( glContext, @"FATAL: could not get openGL context");

    [glContext makeCurrentContext];
    CGLLockContext([glContext CGLContextObj]);  
}

-(void) unlockOpenGLContext
{
    NSOpenGLContext *glContext = [self openGLContext];
    NSAssert( glContext, @"FATAL: could not get openGL context");

    CGLUnlockContext([glContext CGLContextObj]);
}

// Find or make a fence that is ready to use.
-(CCGLViewFence *)getReadyFence
{
    // First checkf oldest (first in the array) fence is ready again.
    CCGLViewFence *fence = _fences.firstObject;;
    if(fence.isReady){
        // Remove the fence so it can be inserted at the end of the queue again.
        [_fences removeObjectAtIndex:0];
        return fence;
    } else {
        // No existing fences ready. Make a new one.
        return [[CCGLViewFence alloc] init];
    }
}

-(void)addFrameCompletionHandler:(dispatch_block_t)handler
{
    if(_fences == nil){
        _fences = [NSMutableArray arrayWithObject:[[CCGLViewFence alloc] init]];
    }

    CCGLViewFence *fence = _fences.lastObject;
    if(!fence.isReady){
        fence = [self getReadyFence];
        [_fences addObject:fence];
    }

    [fence.handlers addObject:handler];
}

-(void)beginFrame
{
    [self lockOpenGLContext];
}

-(void)presentFrame
{
    {
        CCGLViewFence *fence = _fences.lastObject;
        if(fence.isReady){
            // If the fence is ready to be added, insert a sync point for it.
            [fence insertFence];
        }
    }

    [self.openGLContext flushBuffer];

    // Check the fences for completion.
    for(CCGLViewFence *fence in _fences){
        if(fence.isComplete){
            for(dispatch_block_t handler in fence.handlers) handler();
            [fence.handlers removeAllObjects];
        } else {
            break;
        }
    }

    [self unlockOpenGLContext];
}

-(GLuint)fbo
{
    return 0;
}
@end    

标签: openglxcode10macos-mojavensopenglview

解决方案


推荐阅读