diff options
Diffstat (limited to 'platform/darwin/src/headless_backend_eagl.mm')
-rw-r--r-- | platform/darwin/src/headless_backend_eagl.mm | 61 |
1 files changed, 29 insertions, 32 deletions
diff --git a/platform/darwin/src/headless_backend_eagl.mm b/platform/darwin/src/headless_backend_eagl.mm index 1daaeaf54c..050fa62c78 100644 --- a/platform/darwin/src/headless_backend_eagl.mm +++ b/platform/darwin/src/headless_backend_eagl.mm @@ -6,51 +6,48 @@ namespace mbgl { -struct EAGLImpl : public HeadlessBackend::Impl { - EAGLImpl(EAGLContext* glContext_) : glContext(glContext_) { - [reinterpret_cast<EAGLContext*>(glContext) retain]; - reinterpret_cast<EAGLContext*>(glContext).multiThreaded = YES; +class EAGLBackendImpl : public HeadlessBackend::Impl { +public: + EAGLBackendImpl() { + glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; + if (glContext == nil) { + throw std::runtime_error("Error creating GL context object"); + } + glContext.multiThreaded = YES; } - ~EAGLImpl() { - [glContext release]; + // Required for ARC to deallocate correctly. + ~EAGLBackendImpl() final = default; + + gl::ProcAddress getExtensionFunctionPointer(const char* name) final { + static CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengles")); + if (!framework) { + throw std::runtime_error("Failed to load OpenGL framework."); + } + + CFStringRef str = + CFStringCreateWithCString(kCFAllocatorDefault, name, kCFStringEncodingASCII); + void* symbol = CFBundleGetFunctionPointerForName(framework, str); + CFRelease(str); + + return reinterpret_cast<gl::ProcAddress>(symbol); } - void activateContext() { + void activateContext() final { [EAGLContext setCurrentContext:glContext]; } - void deactivateContext() { + void deactivateContext() final { [EAGLContext setCurrentContext:nil]; } +private: EAGLContext* glContext = nullptr; }; -gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) { - static CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengles")); - if (!framework) { - throw std::runtime_error("Failed to load OpenGL framework."); - } - - CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, name, kCFStringEncodingASCII); - void* symbol = CFBundleGetFunctionPointerForName(framework, str); - CFRelease(str); - - return reinterpret_cast<gl::ProcAddress>(symbol); -} - -bool HeadlessBackend::hasDisplay() { - return true; -} - -void HeadlessBackend::createContext() { - EAGLContext* glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; - if (glContext == nil) { - throw std::runtime_error("Error creating GL context object"); - } - - impl.reset(new EAGLImpl(glContext)); +void HeadlessBackend::createImpl() { + assert(!impl); + impl = std::make_unique<EAGLBackendImpl>(); } } // namespace mbgl |