[Renderer] Lots of clean up for OpenGLView.renderAtTime()
Lock and unlock and make current the view's context. Do some weak error handling.
This commit is contained in:
parent
79ddf9ee54
commit
7e55045d10
1 changed files with 28 additions and 4 deletions
|
@ -11,11 +11,14 @@ import Cocoa
|
||||||
import OpenGL
|
import OpenGL
|
||||||
import CoreVideo
|
import CoreVideo
|
||||||
|
|
||||||
class OpenGLView: NSOpenGLView {
|
public class OpenGLView: NSOpenGLView {
|
||||||
private var didSetupOpenGL = false
|
private var didSetupOpenGL = false
|
||||||
private var displayLink: CVDisplayLink? = nil
|
private var displayLink: CVDisplayLink? = nil
|
||||||
|
|
||||||
override func viewWillMoveToSuperview(newSuperview: NSView?) {
|
// TODO: Should this be weak?
|
||||||
|
public var renderer: FrameRenderer? = nil
|
||||||
|
|
||||||
|
override public func viewWillMoveToSuperview(newSuperview: NSView?) {
|
||||||
if newSuperview != nil {
|
if newSuperview != nil {
|
||||||
setupOpenGL()
|
setupOpenGL()
|
||||||
startRenderingLoop()
|
startRenderingLoop()
|
||||||
|
@ -70,7 +73,7 @@ class OpenGLView: NSOpenGLView {
|
||||||
optionsOut: UnsafeMutablePointer<CVOptionFlags>) -> CVReturn in
|
optionsOut: UnsafeMutablePointer<CVOptionFlags>) -> CVReturn in
|
||||||
var result = kCVReturnSuccess
|
var result = kCVReturnSuccess
|
||||||
autoreleasepool {
|
autoreleasepool {
|
||||||
result = self._renderAtTime(currentTime.memory)
|
result = self.renderAtTime(currentTime.memory)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
@ -95,7 +98,28 @@ class OpenGLView: NSOpenGLView {
|
||||||
displayLink = nil
|
displayLink = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
private func _renderAtTime(time: CVTimeStamp) -> CVReturn {
|
private func renderAtTime(time: CVTimeStamp) -> CVReturn {
|
||||||
|
let context: NSOpenGLContext! = self.openGLContext
|
||||||
|
|
||||||
|
guard context != nil else {
|
||||||
|
// TODO: Throw an error? Log something?
|
||||||
|
return kCVReturnError
|
||||||
|
}
|
||||||
|
guard renderer != nil else {
|
||||||
|
// This is actually okay, we just don't render anything.
|
||||||
|
return kCVReturnSuccess
|
||||||
|
}
|
||||||
|
|
||||||
|
context.makeCurrentContext()
|
||||||
|
// TODO: OpenGLContext.clearCurrentContext()?
|
||||||
|
|
||||||
|
CGLLockContext(context.CGLContextObj)
|
||||||
|
defer { CGLUnlockContext(context.CGLContextObj) }
|
||||||
|
|
||||||
|
// Render the frame.
|
||||||
|
renderer!.renderAtTime(time)
|
||||||
|
context.flushBuffer()
|
||||||
|
|
||||||
return kCVReturnSuccess
|
return kCVReturnSuccess
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue