[Renderer] Move around OpenGLView setup to allow calling start() and stop()
This commit is contained in:
parent
c14a4fcd24
commit
cb22821fc5
2 changed files with 27 additions and 18 deletions
|
@ -11,7 +11,7 @@ import Cocoa
|
||||||
import GLKit
|
import GLKit
|
||||||
import CoreVideo
|
import CoreVideo
|
||||||
|
|
||||||
public class OpenGLView: NSOpenGLView, RenderingSurface {
|
public class OpenGLView: NSOpenGLView {
|
||||||
private var didSetupOpenGL = false
|
private var didSetupOpenGL = false
|
||||||
private var displayLink: CVDisplayLink? = nil
|
private var displayLink: CVDisplayLink? = nil
|
||||||
|
|
||||||
|
@ -26,21 +26,16 @@ public class OpenGLView: NSOpenGLView, RenderingSurface {
|
||||||
startRenderingLoop()
|
startRenderingLoop()
|
||||||
}
|
}
|
||||||
|
|
||||||
override public func viewWillMoveToSuperview(newSuperview: NSView?) {
|
|
||||||
if newSuperview != nil {
|
|
||||||
setupOpenGL()
|
|
||||||
startRenderingLoop()
|
|
||||||
} else {
|
|
||||||
stopRenderingLoop()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override public func prepareOpenGL() {
|
override public func prepareOpenGL() {
|
||||||
super.prepareOpenGL()
|
super.prepareOpenGL()
|
||||||
|
|
||||||
|
setupOpenGL()
|
||||||
|
|
||||||
glClearColor(0.16, 0.17, 0.21, 1.0)
|
glClearColor(0.16, 0.17, 0.21, 1.0)
|
||||||
glEnable(GLenum(GL_DEPTH_TEST))
|
glEnable(GLenum(GL_DEPTH_TEST))
|
||||||
glDepthFunc(GLenum(GL_LESS))
|
glDepthFunc(GLenum(GL_LESS))
|
||||||
|
|
||||||
|
setupDisplayLink()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func setupOpenGL() {
|
private func setupOpenGL() {
|
||||||
|
@ -72,7 +67,7 @@ public class OpenGLView: NSOpenGLView, RenderingSurface {
|
||||||
wantsBestResolutionOpenGLSurface = true
|
wantsBestResolutionOpenGLSurface = true
|
||||||
}
|
}
|
||||||
|
|
||||||
private func startRenderingLoop() {
|
private func setupDisplayLink() {
|
||||||
var success = kCVReturnSuccess
|
var success = kCVReturnSuccess
|
||||||
|
|
||||||
success = CVDisplayLinkCreateWithActiveCGDisplays(&displayLink)
|
success = CVDisplayLinkCreateWithActiveCGDisplays(&displayLink)
|
||||||
|
@ -94,27 +89,25 @@ public class OpenGLView: NSOpenGLView, RenderingSurface {
|
||||||
// TODO: Throw an error?
|
// TODO: Throw an error?
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
success = CVDisplayLinkStart(displayLink!)
|
private func startRenderingLoop() {
|
||||||
guard success == kCVReturnSuccess else {
|
guard displayLink != nil else { return }
|
||||||
// TODO: Throw an error?
|
CVDisplayLinkStart(displayLink!)
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func stopRenderingLoop() {
|
private func stopRenderingLoop() {
|
||||||
guard displayLink != nil else { return }
|
guard displayLink != nil else { return }
|
||||||
CVDisplayLinkStop(displayLink!)
|
CVDisplayLinkStop(displayLink!)
|
||||||
displayLink = nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func renderAtTime(time: CVTimeStamp) -> CVReturn {
|
private func renderAtTime(time: CVTimeStamp) -> CVReturn {
|
||||||
let context: NSOpenGLContext! = self.openGLContext
|
let context: NSOpenGLContext! = self.openGLContext
|
||||||
|
|
||||||
guard context != nil else {
|
guard context != nil else {
|
||||||
// TODO: Throw an error? Log something?
|
// TODO: Throw an error? Log something?
|
||||||
return kCVReturnError
|
return kCVReturnError
|
||||||
}
|
}
|
||||||
|
|
||||||
guard renderer != nil else {
|
guard renderer != nil else {
|
||||||
// This is actually okay, we just don't render anything.
|
// This is actually okay, we just don't render anything.
|
||||||
return kCVReturnSuccess
|
return kCVReturnSuccess
|
||||||
|
@ -134,6 +127,16 @@ public class OpenGLView: NSOpenGLView, RenderingSurface {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension OpenGLView: RenderingSurface {
|
||||||
|
public func start() {
|
||||||
|
startRenderingLoop()
|
||||||
|
}
|
||||||
|
|
||||||
|
public func stop() {
|
||||||
|
stopRenderingLoop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private func displayLinkCallback(
|
private func displayLinkCallback(
|
||||||
displayLink: CVDisplayLink,
|
displayLink: CVDisplayLink,
|
||||||
currentTime: UnsafePointer<CVTimeStamp>,
|
currentTime: UnsafePointer<CVTimeStamp>,
|
||||||
|
|
|
@ -11,7 +11,13 @@ import CoreVideo
|
||||||
public typealias FrameTimeStamp = CVTimeStamp
|
public typealias FrameTimeStamp = CVTimeStamp
|
||||||
|
|
||||||
public protocol RenderingSurface {
|
public protocol RenderingSurface {
|
||||||
|
/// Bounds in pixels of this surface.
|
||||||
var bounds: CGRect { get }
|
var bounds: CGRect { get }
|
||||||
|
|
||||||
|
/// Start the rendering loop for this surface.
|
||||||
|
func start()
|
||||||
|
/// Stop the rendering loop for this surface.
|
||||||
|
func stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol FrameRenderer {
|
public protocol FrameRenderer {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue