为什么我的CAOpenGLLayer更新比我以前的NSOpenGLView更慢?

时间:2011-05-24 16:48:43

标签: performance macos opengl calayer

我有一个在Mac OS X上呈现OpenGL内容的应用程序。最初它是渲染到NSOpenGLView,然后我将其更改为渲染到CAOpenGLLayer子类。

当我这样做时,我看到了巨大的性能损失:帧速率减半,鼠标响应度降低,口吃(不时停止,最多一秒钟,在此期间,探测器活动报告等待互斥锁以便在GPU ram上加载数据) ,并将CPU使用率提高一倍。

我正在调查此问题,并提出了一些问题:

  • 其他人是否看到过类似的表现?
  • 我的CAOpenGLLayer设置有问题吗?
  • CAOpenGLLayer和核心动画框架是如何实现的,即我的OpenGL内容从我的glDrawElements调用到我的屏幕的路径,以及如何通过这样的设置来优化性能?

这是我的CAOpenGLLayer设置代码:

// my application's entry point (can't be easily changed):
void AppUpdateLogic(); //update application logic. Will load textures
void AppRender(); //executes drawing
void AppEventSink(NSEvent* ev); //handle mouse and keyboard events.
                                //Will do pick renderings

@interface MyCAOpenGLLayer: CAOpenGLLayer
{
    CGLPixelFormatObj   pixelFormat;
    CGLContextObj       glContext;
}
@end

@implementation MyCAOpenGLLayer

- (id)init {
    self = [super init];

    CGLPixelFormatAttribute attributes[] =
    {
        kCGLPFAAccelerated,
        kCGLPFAColorSize, (CGLPixelFormatAttribute)24,
        kCGLPFAAlphaSize, (CGLPixelFormatAttribute)8,
        kCGLPFADepthSize, (CGLPixelFormatAttribute)16,
        (CGLPixelFormatAttribute)0
    };

    GLint numPixelFormats = 0;
    CGLChoosePixelFormat(attributes, &pixelFormat, &numPixelFormats);

    glContext = [super copyCGLContextForPixelFormat:mPixelFormat];

    return self;
}

- (void)drawInCGLContext:(CGLContextObj)inGlContext
             pixelFormat:(CGLPixelFormatObj)inPixelFormat
            forLayerTime:(CFTimeInterval)timeInterval
             displayTime:(const CVTimeStamp *)timeStamp 
{
    AppRender();
    [super drawInCGLContext:inGlContext
                pixelFormat:inPixelFormat
               forLayerTime:timeInterval
                displayTime:timeStamp ]
}

- (void)releaseCGLPixelFormat:(CGLPixelFormatObj)pixelFormat {
    [self release];
}

- (CGLPixelFormatObj)copyCGLPixelFormatForDisplayMask:(uint32_t)mask {
    [self retain];
    return pixelFormat;
}

- (CGLContextObj)copyCGLContextForPixelFormat:(CGLPixelFormatObj)pixelFormat {
    [self retain];
    return glContext;
}

- (void)releaseCGLContext:(CGLContextObj)glContext {
    [self release];
}

@end

@interface MyMainViewController: NSViewController {
    CGLContextObj   glContext;
    CALayer*        myOpenGLLayer;
}

-(void)timerTriggered:(NSTimer*)timer;
@end


@implementation MyMainViewController

-(void)viewDidLoad:(NSView*)view {
    myOpenGLLayer = [[MyCAOpenGLLayer alloc] init];
    [view setLayer:myOpenGLLayer];
    [view setWantsLayer:YES];

    glContext = [myOpenGLLayer copyCGLContextForPixelFormat:nil];

    [NSTimer scheduledTimerWithTimeInterval:1/30.0
                                     target:self
                                   selector:@selector(timerTriggered:)
                                   userInfo:nil
                                    repeats:YES ];
}

- (void)timerTriggered:(NSTimer*)timer {
    CGLContextObj oldContext = CGLContextGetCurrent();
    CGLContextSetCurrent(glContext);
    CGLContextLock(glContext);

    AppUpdateLogic();
    [myOpenGLLayer  setNeedsDisplay:YES];

    CGLContextUnlock(glContext);
    CGLContextSetCurrent(oldContext);
}

- (void)mouseDown:(NSEvent*)event {
    CGLContextObj oldContext = CGLContextGetCurrent();
    CGLContextSetCurrent(glContext);
    CGLContextLock(glContext);

    AppEventSink(event);

    CGLContextUnlock(glContext);
    CGLContextSetCurrent(oldContext);
}
@end

了解我的视频卡功能不是很强大(具有64 MB共享内存的Intel GMA)可能很有用。

1 个答案:

答案 0 :(得分:3)

在我的一个应用程序中,我从NSOpenGLView切换到CAOpenGLLayer,然后由于后者的更新机制的一些问题而最终返回。但是,这与您在此处报告的性能问题不同。

在您的情况下,我相信您执行图层内容更新的方式可能是罪魁祸首。首先,使用NSTimer触发重绘并不能保证更新事件与显示器的刷新率完全一致。相反,我建议将CAOpenGLLayer的asynchronous属性设置为YES,并使用–canDrawInCGLContext:pixelFormat:forLayerTime:displayTime:来管理更新频率。这将导致OpenGL层与显示同步更新,并且它将避免您正在进行的上下文锁定。

这方面的缺点(这也是NSTimer方法的问题)是在主线程上触发了CAOpenGLLayer委托回调。如果您有阻止主线程的东西,您的显示将冻结。同样,如果您的OpenGL框架更新需要一段时间,则可能会导致您的UI响应性降低。

这就是我使用CVDisplayLink在后台线程上生成我的OpenGL内容的触发更新的原因。不幸的是,我在用这个更新我的CAOpenGLLayer时看到了一些渲染工件,所以我最终切换回NSOpenGLView。从那时起,我遇到了一种可能避免这些工件的方法,但NSOpenGLView已经很好地满足了我们的需求,所以我没有再次切换回来。

相关问题