I'm working on an app which needs to draw with OpengGL at a refresh rate at least equal to the refresh rate of the monitor. And I need to perform the drawing in a separate thread so that drawing is never locked by intense UI actions.
Actually I'm using a NSOpenGLView
in combination with CVDisplayLink
and I'm able to achive 60-80FPS without any problem.
Since I need also to display some cocoa controls on top of this view I tried to subclass NSOpenGLView
and make it layer-backed, following LayerBackedOpenGLView Apple example.
The result isn't satisfactory and I get a lot of artifacts.
Therefore I've solved the problem using a separate NSWindow
to host the cocoa controls and adding this window as a child window of the main window containing the NSOpenGLView
.
It works fine and I'm able to get quite the same FPS as the initial implementation.
Since I consider this solution quite like a dirty hack, I'm looking for an alternative and more clean way of achiving what I need.
Few days ago I came across NSOpenGLLayer
and I thought that it could be used as a viable solution for my problem.
So finally, after all this preamble, here comes my question:
is it possible to draw to a NSOpenGLLayer
from a separate thread using CVDisplayLink
callback?.
So far I've tried to implement this but I'm not able to draw from the CVDisplayLink
callback. I can only -setNeedsDisplay:TRUE
on the NSOpenGLLayer
from the CVDisplayLink
callback and then perform the drawing in -drawInOpenGLContext:pixelFormat:forLayerTime:displayTime:
when it gets automatically called by cocoa. But I suppose that this way I'm drawing from the main thread, isn't it?
After googling for this I've even found this post in which the user claims that under Lion drawing can occur only inside -drawInOpenGLContext:pixelFormat:forLayerTime:displayTime:
.
I'm on Snow Leopard at the moment but the app should run flawlessly even on Lion.
Am I missing something?