[Renderer] Convert CV stuff to callback
Use callback instead of handler because the latter is apparently broken? Fix a few issues I found by trying to build a simple app with it.
This commit is contained in:
parent
7f27ca7bc6
commit
2abf63e6fc
1 changed files with 33 additions and 14 deletions
|
@ -18,6 +18,15 @@ public class OpenGLView: NSOpenGLView {
|
||||||
// TODO: Should this be weak?
|
// TODO: Should this be weak?
|
||||||
public var renderer: FrameRenderer? = nil
|
public var renderer: FrameRenderer? = nil
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
stopRenderingLoop()
|
||||||
|
}
|
||||||
|
|
||||||
|
override public func awakeFromNib() {
|
||||||
|
setupOpenGL()
|
||||||
|
startRenderingLoop()
|
||||||
|
}
|
||||||
|
|
||||||
override public func viewWillMoveToSuperview(newSuperview: NSView?) {
|
override public func viewWillMoveToSuperview(newSuperview: NSView?) {
|
||||||
if newSuperview != nil {
|
if newSuperview != nil {
|
||||||
setupOpenGL()
|
setupOpenGL()
|
||||||
|
@ -28,7 +37,7 @@ public class OpenGLView: NSOpenGLView {
|
||||||
}
|
}
|
||||||
|
|
||||||
private func setupOpenGL() {
|
private func setupOpenGL() {
|
||||||
guard didSetupOpenGL else { return }
|
guard !didSetupOpenGL else { return }
|
||||||
defer { didSetupOpenGL = true }
|
defer { didSetupOpenGL = true }
|
||||||
|
|
||||||
let attrs = [NSOpenGLPFAAccelerated,
|
let attrs = [NSOpenGLPFAAccelerated,
|
||||||
|
@ -65,18 +74,8 @@ public class OpenGLView: NSOpenGLView {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
success = CVDisplayLinkSetOutputHandler(displayLink!) {
|
let unsafeSelf = UnsafeMutablePointer<Void>(unsafeAddressOf(self))
|
||||||
(displayLink: CVDisplayLink,
|
success = CVDisplayLinkSetOutputCallback(displayLink!, displayLinkCallback, unsafeSelf)
|
||||||
currentTime: UnsafePointer<CVTimeStamp>,
|
|
||||||
displayTime: UnsafePointer<CVTimeStamp>,
|
|
||||||
optionsIn: CVOptionFlags,
|
|
||||||
optionsOut: UnsafeMutablePointer<CVOptionFlags>) -> CVReturn in
|
|
||||||
var result = kCVReturnSuccess
|
|
||||||
autoreleasepool {
|
|
||||||
result = self.renderAtTime(currentTime.memory)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
guard success == kCVReturnSuccess else {
|
guard success == kCVReturnSuccess else {
|
||||||
// TODO: Throw an error?
|
// TODO: Throw an error?
|
||||||
return
|
return
|
||||||
|
@ -89,7 +88,11 @@ public class OpenGLView: NSOpenGLView {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
CVDisplayLinkStart(displayLink!)
|
success = CVDisplayLinkStart(displayLink!)
|
||||||
|
guard success == kCVReturnSuccess else {
|
||||||
|
// TODO: Throw an error?
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func stopRenderingLoop() {
|
private func stopRenderingLoop() {
|
||||||
|
@ -122,4 +125,20 @@ public class OpenGLView: NSOpenGLView {
|
||||||
|
|
||||||
return kCVReturnSuccess
|
return kCVReturnSuccess
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func displayLinkCallback(
|
||||||
|
displayLink: CVDisplayLink,
|
||||||
|
currentTime: UnsafePointer<CVTimeStamp>,
|
||||||
|
outputTime: UnsafePointer<CVTimeStamp>,
|
||||||
|
flags: CVOptionFlags,
|
||||||
|
flagsOut: UnsafeMutablePointer<CVOptionFlags>,
|
||||||
|
context: UnsafeMutablePointer<Void>) -> CVReturn
|
||||||
|
{
|
||||||
|
var result = kCVReturnSuccess
|
||||||
|
autoreleasepool {
|
||||||
|
let glView = unsafeBitCast(context, OpenGLView.self)
|
||||||
|
result = glView.renderAtTime(currentTime.memory)
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue