summaryrefslogtreecommitdiff
path: root/platform/darwin/src/headless_backend_cgl.cpp
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2016-10-06 13:23:50 +0200
committerKonstantin Käfer <mail@kkaefer.com>2016-10-25 13:52:36 -0700
commit5cc390d694fc7510d445310d8eb9e32429a5e67b (patch)
tree7a24706f919ac3e8154be8b4ce33aed5bf42188d /platform/darwin/src/headless_backend_cgl.cpp
parent45f4dc0166f2d609d014d2174209fdbe1994c943 (diff)
downloadqtlocation-mapboxgl-5cc390d694fc7510d445310d8eb9e32429a5e67b.tar.gz
[core] separate Backend from View for headless rendering
Diffstat (limited to 'platform/darwin/src/headless_backend_cgl.cpp')
-rw-r--r--platform/darwin/src/headless_backend_cgl.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/platform/darwin/src/headless_backend_cgl.cpp b/platform/darwin/src/headless_backend_cgl.cpp
new file mode 100644
index 0000000000..4ca567f55c
--- /dev/null
+++ b/platform/darwin/src/headless_backend_cgl.cpp
@@ -0,0 +1,58 @@
+#include <mbgl/platform/default/headless_backend.hpp>
+#include <mbgl/platform/default/headless_display.hpp>
+
+#include <CoreFoundation/CoreFoundation.h>
+
+#include <string>
+#include <stdexcept>
+
+namespace mbgl {
+
+gl::glProc HeadlessBackend::initializeExtension(const char* name) {
+ static CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
+ 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::glProc>(symbol);
+}
+
+void HeadlessBackend::createContext() {
+ CGLError error = CGLCreateContext(display->pixelFormat, nullptr, &glContext);
+ if (error != kCGLNoError) {
+ throw std::runtime_error(std::string("Error creating GL context object:") +
+ CGLErrorString(error) + "\n");
+ }
+
+ error = CGLEnable(glContext, kCGLCEMPEngine);
+ if (error != kCGLNoError) {
+ 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");
+ }
+}
+
+} // namespace mbgl