// // NSGLWrapper.mm // XcodeOpenCL // // Created by Joshua Moerman on 13/04/14. // // #include "NSGLWrapper.hpp" #include #include #import #import static void check_cgl(CGLError e) { if(e != kCGLNoError){ throw std::runtime_error(CGLErrorString(e)); } } GLContext::GLContext() { GLint npix{}; const CGLPixelFormatAttribute attributes[]{ kCGLPFADoubleBuffer, kCGLPFADepthSize, static_cast(24), kCGLPFAOpenGLProfile, static_cast(kCGLOGLPVersion_3_2_Core), static_cast(0) }; check_cgl(CGLChoosePixelFormat(attributes, &pix, &npix)); check_cgl(CGLCreateContext(pix, 0, &ctx)); GLint swap_interval = 1; check_cgl(CGLSetParameter(ctx, kCGLCPSwapInterval, &swap_interval)); set_as_current_context(); } GLContext::~GLContext() { CGLReleaseContext(ctx); CGLReleasePixelFormat(pix); } void GLContext::set_as_current_context() const { check_cgl(CGLSetCurrentContext(ctx)); } void GLContext::lock() { check_cgl(CGLLockContext(ctx)); } void GLContext::unlock() { check_cgl(CGLUnlockContext(ctx)); } static void check_cv(CVReturn r) { if(r != kCVReturnSuccess){ throw std::runtime_error("Some CV display link issue"); } } static CVReturn CVDisplayLinkyInvokeCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext) { if(!displayLinkContext){ throw std::runtime_error("There is no context in the display link"); } auto& foo = *(CVDisplayLinky*)displayLinkContext; if(foo.callback){ foo.callback(); } return kCVReturnSuccess; } CVDisplayLinky::CVDisplayLinky(GLContext const & c) { check_cv(CVDisplayLinkCreateWithActiveCGDisplays(&displayLink)); check_cv(CVDisplayLinkSetOutputCallback(displayLink, &CVDisplayLinkyInvokeCallback, this)); check_cv(CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext(displayLink, c.ctx, c.pix)); } CVDisplayLinky::~CVDisplayLinky() { stop(); CVDisplayLinkRelease(displayLink); } void CVDisplayLinky::start() { check_cv(CVDisplayLinkStart(displayLink)); } void CVDisplayLinky::stop() { check_cv(CVDisplayLinkStop(displayLink)); }