summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeith Bade <leith@mapbox.com>2014-12-09 03:41:05 +1100
committerLeith Bade <leith@mapbox.com>2014-12-09 21:47:30 +1100
commitd2792e29870629faeaebd0eea91b5819fc58e719 (patch)
tree8ecbc7e31222bdd2c515135f05b27ae50c26f29f
parent318ef5d44314814f4dd7da2ba8c532b0119bdb35 (diff)
downloadqtlocation-mapboxgl-d2792e29870629faeaebd0eea91b5819fc58e719.tar.gz
Tidy up HeadlessView
-rw-r--r--include/mbgl/platform/default/headless_display.hpp4
-rw-r--r--include/mbgl/platform/default/headless_view.hpp14
-rw-r--r--platform/default/glfw_view.cpp1
-rw-r--r--platform/default/headless_display.cpp36
-rw-r--r--platform/default/headless_view.cpp151
5 files changed, 110 insertions, 96 deletions
diff --git a/include/mbgl/platform/default/headless_display.hpp b/include/mbgl/platform/default/headless_display.hpp
index 08c423c587..bf67e3c3b3 100644
--- a/include/mbgl/platform/default/headless_display.hpp
+++ b/include/mbgl/platform/default/headless_display.hpp
@@ -15,8 +15,8 @@ public:
#endif
#if MBGL_USE_GLX
- Display *x_display = nullptr;
- GLXFBConfig *fb_configs = nullptr;
+ Display *xDisplay = nullptr;
+ GLXFBConfig *fbConfigs = nullptr;
#endif
};
diff --git a/include/mbgl/platform/default/headless_view.hpp b/include/mbgl/platform/default/headless_view.hpp
index 9d8acf70f7..a521cff547 100644
--- a/include/mbgl/platform/default/headless_view.hpp
+++ b/include/mbgl/platform/default/headless_view.hpp
@@ -46,19 +46,19 @@ private:
float pixelRatio_;
#if MBGL_USE_CGL
- CGLContextObj gl_context;
+ CGLContextObj glContext;
#endif
#if MBGL_USE_GLX
- Display *x_display = nullptr;
- GLXFBConfig *fb_configs = nullptr;
- GLXContext gl_context = 0;
- GLXPbuffer glx_pbuffer = 0;
+ Display *xDisplay = nullptr;
+ GLXFBConfig *fbConfigs = nullptr;
+ GLXContext glContext = 0;
+ GLXPbuffer glxPbuffer = 0;
#endif
GLuint fbo = 0;
- GLuint fbo_depth_stencil = 0;
- GLuint fbo_color = 0;
+ GLuint fboDepthStencil = 0;
+ GLuint fboColor = 0;
};
}
diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp
index 32b7e44ece..7cfb132133 100644
--- a/platform/default/glfw_view.cpp
+++ b/platform/default/glfw_view.cpp
@@ -65,7 +65,6 @@ void GLFWView::initialize(mbgl::Map *map_) {
glfwSetScrollCallback(window, scroll);
glfwSetKeyCallback(window, key);
-
const std::string extensions = (char *)glGetString(GL_EXTENSIONS);
{
using namespace mbgl;
diff --git a/platform/default/headless_display.cpp b/platform/default/headless_display.cpp
index ada48b8648..40cc0640a9 100644
--- a/platform/default/headless_display.cpp
+++ b/platform/default/headless_display.cpp
@@ -18,31 +18,35 @@ HeadlessDisplay::HeadlessDisplay() {
GLint num;
CGLError error = CGLChoosePixelFormat(attributes, &pixelFormat, &num);
- if (error) {
- fprintf(stderr, "Error pixel format: %s\n", CGLErrorString(error));
+ if (error != kCGLNoError) {
+ throw std::runtime_error(std::string("Error choosing pixel format:") + CGLErrorString(error) + "\n");
+ return;
+ }
+ if (num <= 0) {
+ throw std::runtime_error("No pixel formats found.");
return;
}
#endif
#if MBGL_USE_GLX
if (!XInitThreads()) {
- throw std::runtime_error("Failed to XInitThreads");
+ throw std::runtime_error("Failed to XInitThreads.");
}
- x_display = XOpenDisplay(nullptr);
- if (x_display == nullptr) {
- throw std::runtime_error("Failed to open X display");
+ xDisplay = XOpenDisplay(nullptr);
+ if (xDisplay == nullptr) {
+ throw std::runtime_error("Failed to open X display.");
}
- const char *extensions = (char *)glXQueryServerString(x_display, DefaultScreen(x_display), GLX_EXTENSIONS);
+ const char *extensions = (char *)glXQueryServerString(xDisplay, DefaultScreen(xDisplay), GLX_EXTENSIONS);
if (!extensions) {
- throw std::runtime_error("Cannot read GLX extensions");
+ throw std::runtime_error("Cannot read GLX extensions.");
}
if (!strstr(extensions,"GLX_SGIX_fbconfig")) {
- throw std::runtime_error("Extension GLX_SGIX_fbconfig was not found");
+ throw std::runtime_error("Extension GLX_SGIX_fbconfig was not found.");
}
if (!strstr(extensions, "GLX_SGIX_pbuffer")) {
- throw std::runtime_error("Cannot find glXCreateContextAttribsARB");
+ throw std::runtime_error("Cannot find glXCreateContextAttribsARB.");
}
// We're creating a dummy pbuffer anyway that we're not using.
@@ -52,9 +56,12 @@ HeadlessDisplay::HeadlessDisplay() {
};
int configs = 0;
- fb_configs = glXChooseFBConfig(x_display, DefaultScreen(x_display), pixelFormat, &configs);
+ fbConfigs = glXChooseFBConfig(xDisplay, DefaultScreen(xDisplay), pixelFormat, &configs);
+ if (fbConfigs == nullptr) {
+ throw std::runtime_error("Failed to glXChooseFBConfig.");
+ }
if (configs <= 0) {
- throw std::runtime_error("No Framebuffer configurations");
+ throw std::runtime_error("No Framebuffer configurations.");
}
#endif
}
@@ -65,10 +72,9 @@ HeadlessDisplay::~HeadlessDisplay() {
#endif
#if MBGL_USE_GLX
- XFree(fb_configs);
- XCloseDisplay(x_display);
+ XFree(fbConfigs);
+ XCloseDisplay(xDisplay);
#endif
}
}
-
diff --git a/platform/default/headless_view.cpp b/platform/default/headless_view.cpp
index 5b95c76026..c84651c2e9 100644
--- a/platform/default/headless_view.cpp
+++ b/platform/default/headless_view.cpp
@@ -7,6 +7,7 @@
#include <sstream>
#include <string>
#include <cstring>
+#include <cassert>
#if MBGL_USE_GLX
#ifdef GLX_ARB_create_context
@@ -23,7 +24,7 @@ CGLProc CGLGetProcAddress(const char *proc) {
if (!framework) {
framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
if (!framework) {
- throw std::runtime_error("Failed to load OpenGL.framework");
+ throw std::runtime_error("Failed to load OpenGL framework.");
}
}
@@ -59,16 +60,25 @@ void HeadlessView::loadExtensions() {
gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)CGLGetProcAddress("glDeleteVertexArraysAPPLE");
gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)CGLGetProcAddress("glGenVertexArraysAPPLE");
gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)CGLGetProcAddress("glIsVertexArrayAPPLE");
+ assert(gl::BindVertexArray != nullptr);
+ assert(gl::DeleteVertexArrays != nullptr);
+ assert(gl::GenVertexArrays != nullptr);
+ assert(gl::IsVertexArray != nullptr);
}
#endif
#ifdef MBGL_USE_GLX
if (extensions.find("GL_ARB_vertex_array_object") != std::string::npos) {
- gl::BindVertexArray = (gl::PFNGLBINDVERTEXARRAYPROC)glXGetProcAddress((const GLubyte *)"glBindVertexArrayARB");
- gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)glXGetProcAddress((const GLubyte *)"glDeleteVertexArraysARB");
- gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)glXGetProcAddress((const GLubyte *)"glGenVertexArraysARB");
- gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)glXGetProcAddress((const GLubyte *)"glIsVertexArrayARB");
+ gl::BindVertexArray = (gl::PFNGLBINDVERTEXARRAYPROC)glXGetProcAddress((const GLubyte *)"glBindVertexArray");
+ gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)glXGetProcAddress((const GLubyte *)"glDeleteVertexArrays");
+ gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)glXGetProcAddress((const GLubyte *)"glGenVertexArrays");
+ gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)glXGetProcAddress((const GLubyte *)"glIsVertexArray");
+ assert(gl::BindVertexArray != nullptr);
+ assert(gl::DeleteVertexArrays != nullptr);
+ assert(gl::GenVertexArrays != nullptr);
+ assert(gl::IsVertexArray != nullptr);
}
#endif
+
make_inactive();
}
@@ -78,7 +88,7 @@ void HeadlessView::loadExtensions() {
// These are all of the OpenGL Core profile version that we know about.
struct core_profile_version { int major, minor; };
-static const core_profile_version core_profile_versions[] = {
+static const core_profile_version coreProfileVersions[] = {
{4, 5},
{4, 4},
{4, 3},
@@ -93,33 +103,33 @@ static const core_profile_version core_profile_versions[] = {
};
GLXContext createCoreProfile(Display *dpy, GLXFBConfig fbconfig) {
- static bool context_creation_failed = false;
+ static bool contextCreationFailed = false;
GLXContext ctx = 0;
// Set the Error Handler to avoid crashing the program when the context creation fails.
// It is expected that some context creation attempts fail, e.g. because the OpenGL
// implementation does not support the version we're requesting.
- int (*previous_error_handler)(Display *, XErrorEvent *) = XSetErrorHandler([](Display *, XErrorEvent *) {
- context_creation_failed = true;
+ int (*previousErrorHandler)(Display *, XErrorEvent *) = XSetErrorHandler([](Display *, XErrorEvent *) {
+ contextCreationFailed = true;
return 0;
});
// Try to create core profiles from the highest known version on down.
- for (int i = 0; !ctx && core_profile_versions[i].major; i++) {
- context_creation_failed = false;
- const int context_flags[] = {
- GLX_CONTEXT_MAJOR_VERSION_ARB, core_profile_versions[i].major,
- GLX_CONTEXT_MINOR_VERSION_ARB, core_profile_versions[i].minor,
+ for (int i = 0; !ctx && coreProfileVersions[i].major; i++) {
+ contextCreationFailed = false;
+ const int contextFlags[] = {
+ GLX_CONTEXT_MAJOR_VERSION_ARB, coreProfileVersions[i].major,
+ GLX_CONTEXT_MINOR_VERSION_ARB, coreProfileVersions[i].minor,
0
};
- ctx = glXCreateContextAttribsARB(dpy, fbconfig, 0, True, context_flags);
- if (context_creation_failed) {
+ ctx = glXCreateContextAttribsARB(dpy, fbconfig, 0, True, contextFlags);
+ if (contextCreationFailed) {
ctx = 0;
}
}
// Restore the old error handler.
- XSetErrorHandler(previous_error_handler);
+ XSetErrorHandler(previousErrorHandler);
return ctx;
}
#endif
@@ -127,14 +137,14 @@ GLXContext createCoreProfile(Display *dpy, GLXFBConfig fbconfig) {
void HeadlessView::createContext() {
#if MBGL_USE_CGL
- CGLError error = CGLCreateContext(display_->pixelFormat, NULL, &gl_context);
- if (error) {
- throw std::runtime_error("Error creating GL context object\n");
+ CGLError error = CGLCreateContext(display_->pixelFormat, NULL, &glContext);
+ if (error != kCGLNoError) {
+ throw std::runtime_error(std::string("Error creating GL context object:") + CGLErrorString(error) + "\n");
}
- error = CGLEnable(gl_context, kCGLCEMPEngine);
- if (error != kCGLNoError ) {
- throw std::runtime_error("Error enabling OpenGL multithreading\n");
+ error = CGLEnable(glContext, kCGLCEMPEngine);
+ if (error != kCGLNoError) {
+ throw std::runtime_error(std::string("Error enabling OpenGL multithreading:") + CGLErrorString(error) + "\n");
}
#endif
@@ -145,39 +155,39 @@ void HeadlessView::createContext() {
}
#endif
- x_display = display_->x_display;
- fb_configs = display_->fb_configs;
+ xDisplay = display_->xDisplay;
+ fbConfigs = display_->fbConfigs;
#ifdef GLX_ARB_create_context
if (glXCreateContextAttribsARB) {
// Try to create a core profile context.
- gl_context = createCoreProfile(x_display, fb_configs[0]);
+ glContext = createCoreProfile(xDisplay, fbConfigs[0]);
}
#endif
- if (!gl_context) {
+ if (!glContext) {
// Try to create a legacy context
- gl_context = glXCreateNewContext(x_display, fb_configs[0], GLX_RGBA_TYPE, 0, True);
- if (gl_context) {
- if (!glXIsDirect(x_display, gl_context)) {
- glXDestroyContext(x_display, gl_context);
- gl_context = 0;
+ glContext = glXCreateNewContext(xDisplay, fbConfigs[0], GLX_RGBA_TYPE, 0, True);
+ if (glContext) {
+ if (!glXIsDirect(xDisplay, glContext)) {
+ glXDestroyContext(xDisplay, glContext);
+ glContext = 0;
}
}
}
- if (gl_context == 0) {
- throw std::runtime_error("Error creating GL context object");
+ if (glContext == 0) {
+ throw std::runtime_error("Error creating GL context object.");
}
// Create a dummy pbuffer. We will render to framebuffers anyway, but we need a pbuffer to
// activate the context.
- int pbuffer_attributes[] = {
+ int pbufferAttributes[] = {
GLX_PBUFFER_WIDTH, 8,
GLX_PBUFFER_HEIGHT, 8,
0
};
- glx_pbuffer = glXCreatePbuffer(x_display, fb_configs[0], pbuffer_attributes);
+ glxPbuffer = glXCreatePbuffer(xDisplay, fbConfigs[0], pbufferAttributes);
#endif
}
@@ -194,35 +204,35 @@ void HeadlessView::resize(uint16_t width, uint16_t height, float pixelRatio) {
make_active();
// Create depth/stencil buffer
- glGenRenderbuffersEXT(1, &fbo_depth_stencil);
- glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo_depth_stencil);
+ glGenRenderbuffersEXT(1, &fboDepthStencil);
+ glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fboDepthStencil);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8_EXT, w, h);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
- glGenRenderbuffersEXT(1, &fbo_color);
- glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo_color);
+ glGenRenderbuffersEXT(1, &fboColor);
+ glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fboColor);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, w, h);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
glGenFramebuffersEXT(1, &fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
- glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, fbo_color);
- glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER_EXT, fbo_depth_stencil);
+ glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, fboColor);
+ glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER_EXT, fboDepthStencil);
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
std::stringstream error("Couldn't create framebuffer: ");
switch (status) {
- case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: (error << "incomplete attachment\n"); break;
- case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: error << "incomplete missing attachment\n"; break;
- case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: error << "incomplete dimensions\n"; break;
- case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: error << "incomplete formats\n"; break;
- case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: error << "incomplete draw buffer\n"; break;
- case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: error << "incomplete read buffer\n"; break;
- case GL_FRAMEBUFFER_UNSUPPORTED: error << "unsupported\n"; break;
- default: error << "other\n"; break;
+ case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: (error << "incomplete attachment.\n"); break;
+ case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: error << "incomplete missing attachment.\n"; break;
+ case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: error << "incomplete dimensions.\n"; break;
+ case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: error << "incomplete formats.\n"; break;
+ case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: error << "incomplete draw buffer.\n"; break;
+ case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: error << "incomplete read buffer.\n"; break;
+ case GL_FRAMEBUFFER_UNSUPPORTED: error << "unsupported.\n"; break;
+ default: error << "othe.r\n"; break;
}
throw std::runtime_error(error.str());
}
@@ -262,14 +272,14 @@ void HeadlessView::clear_buffers() {
fbo = 0;
}
- if (fbo_color) {
- glDeleteTextures(1, &fbo_color);
- fbo_color = 0;
+ if (fboColor) {
+ glDeleteTextures(1, &fboColor);
+ fboColor = 0;
}
- if (fbo_depth_stencil) {
- glDeleteRenderbuffersEXT(1, &fbo_depth_stencil);
- fbo_depth_stencil = 0;
+ if (fboDepthStencil) {
+ glDeleteRenderbuffersEXT(1, &fboDepthStencil);
+ fboDepthStencil = 0;
}
make_inactive();
@@ -279,16 +289,16 @@ HeadlessView::~HeadlessView() {
clear_buffers();
#if MBGL_USE_CGL
- CGLDestroyContext(gl_context);
+ CGLDestroyContext(glContext);
#endif
#if MBGL_USE_GLX
- if (glx_pbuffer) {
- glXDestroyPbuffer(x_display, glx_pbuffer);
- glx_pbuffer = 0;
+ if (glxPbuffer) {
+ glXDestroyPbuffer(xDisplay, glxPbuffer);
+ glxPbuffer = 0;
}
- glXDestroyContext(x_display, gl_context);
+ glXDestroyContext(xDisplay, glContext);
#endif
}
@@ -302,15 +312,15 @@ void HeadlessView::notify_map_change(mbgl::MapChange /*change*/, mbgl::timestamp
void HeadlessView::make_active() {
#if MBGL_USE_CGL
- CGLError error = CGLSetCurrentContext(gl_context);
- if (error) {
- throw std::runtime_error("Switching OpenGL context failed\n");
+ CGLError error = CGLSetCurrentContext(glContext);
+ if (error != kCGLNoError) {
+ throw std::runtime_error(std::string("Switching OpenGL context failed:") + CGLErrorString(error) + "\n");
}
#endif
#if MBGL_USE_GLX
- if (!glXMakeContextCurrent(x_display, glx_pbuffer, glx_pbuffer, gl_context)) {
- throw std::runtime_error("Switching OpenGL context failed\n");
+ if (!glXMakeContextCurrent(xDisplay, glxPbuffer, glxPbuffer, glContext)) {
+ throw std::runtime_error("Switching OpenGL context failed.\n");
}
#endif
}
@@ -318,14 +328,14 @@ void HeadlessView::make_active() {
void HeadlessView::make_inactive() {
#if MBGL_USE_CGL
CGLError error = CGLSetCurrentContext(nullptr);
- if (error) {
- throw std::runtime_error("Removing OpenGL context failed\n");
+ if (error != kCGLNoError) {
+ throw std::runtime_error(std::string("Removing OpenGL context failed:") + CGLErrorString(error) + "\n");
}
#endif
#if MBGL_USE_GLX
- if (!glXMakeContextCurrent(x_display, 0, 0, nullptr)) {
- throw std::runtime_error("Removing OpenGL context failed\n");
+ if (!glXMakeContextCurrent(xDisplay, 0, 0, nullptr)) {
+ throw std::runtime_error("Removing OpenGL context failed.\n");
}
#endif
}
@@ -333,4 +343,3 @@ void HeadlessView::make_inactive() {
void HeadlessView::swap() {}
}
-