From d0ac12309b470eb59690ece3f15773df73a127e1 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Sun, 6 Nov 2016 20:39:13 +0200 Subject: [core] Refactor HeadlessBackend --- platform/darwin/src/headless_backend_cgl.cpp | 58 +++++++++++++++++++--------- 1 file changed, 39 insertions(+), 19 deletions(-) (limited to 'platform/darwin/src/headless_backend_cgl.cpp') diff --git a/platform/darwin/src/headless_backend_cgl.cpp b/platform/darwin/src/headless_backend_cgl.cpp index 081156e8aa..dd062dd6af 100644 --- a/platform/darwin/src/headless_backend_cgl.cpp +++ b/platform/darwin/src/headless_backend_cgl.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -8,6 +9,33 @@ namespace mbgl { +struct CGLImpl : public HeadlessBackend::Impl { + CGLImpl(CGLContextObj glContext_) : glContext(glContext_) { + } + + ~CGLImpl() { + CGLDestroyContext(glContext); + } + + void activateContext() final { + CGLError error = CGLSetCurrentContext(glContext); + if (error != kCGLNoError) { + throw std::runtime_error(std::string("Switching OpenGL context failed:") + + CGLErrorString(error) + "\n"); + } + } + + void deactivateContext() final { + CGLError error = CGLSetCurrentContext(nullptr); + if (error != kCGLNoError) { + throw std::runtime_error(std::string("Removing OpenGL context failed:") + + CGLErrorString(error) + "\n"); + } + } + + CGLContextObj glContext = nullptr; +}; + gl::glProc HeadlessBackend::initializeExtension(const char* name) { static CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl")); if (!framework) { @@ -21,7 +49,17 @@ gl::glProc HeadlessBackend::initializeExtension(const char* name) { return reinterpret_cast(symbol); } +bool HeadlessBackend::hasDisplay() { + if (!display) { + display.reset(new HeadlessDisplay); + } + return bool(display); +} + void HeadlessBackend::createContext() { + assert(!hasContext()); + + CGLContextObj glContext = nullptr; CGLError error = CGLCreateContext(display->attribute(), nullptr, &glContext); if (error != kCGLNoError) { throw std::runtime_error(std::string("Error creating GL context object:") + @@ -33,26 +71,8 @@ void HeadlessBackend::createContext() { throw std::runtime_error(std::string("Error enabling OpenGL multithreading:") + CGLErrorString(error) + "\n"); } -} - -void HeadlessBackend::destroyContext() { - CGLDestroyContext(glContext); -} - -void HeadlessBackend::activateContext() { - CGLError error = CGLSetCurrentContext(glContext); - if (error != kCGLNoError) { - throw std::runtime_error(std::string("Switching OpenGL context failed:") + - CGLErrorString(error) + "\n"); - } -} -void HeadlessBackend::deactivateContext() { - CGLError error = CGLSetCurrentContext(nullptr); - if (error != kCGLNoError) { - throw std::runtime_error(std::string("Removing OpenGL context failed:") + - CGLErrorString(error) + "\n"); - } + impl.reset(new CGLImpl(glContext)); } } // namespace mbgl -- cgit v1.2.1