diff --git a/gyp/install.gypi b/gyp/install.gypi index 56591d27d..5338bfed1 100644 --- a/gyp/install.gypi +++ b/gyp/install.gypi @@ -16,7 +16,8 @@ { 'files': [ '<(PRODUCT_DIR)/libmbgl-core.a' ], 'destination': '<(install_prefix)/lib' }, { 'files': [ '<(PRODUCT_DIR)/libmbgl-headless.a' ], 'destination': '<(install_prefix)/lib' }, { 'files': [ '<(PRODUCT_DIR)/libmbgl-<(platform).a' ], 'destination': '<(install_prefix)/lib' }, - { 'files': [ '../include/mbgl' ], 'destination': '<(install_prefix)/include' } + { 'files': [ '../include/mbgl' ], 'destination': '<(install_prefix)/include' }, + { 'files': [ '<(SHARED_INTERMEDIATE_DIR)/include/mbgl/util/version.hpp' ], 'destination': '<(install_prefix)/include/mbgl/util' }, ], 'variables': { 'conditions': [ diff --git a/gyp/version.gypi b/gyp/version.gypi index ca68f0eb5..341d9d3a2 100644 --- a/gyp/version.gypi +++ b/gyp/version.gypi @@ -6,13 +6,12 @@ 'hard_dependency': 1, 'actions': [ { - 'action_name': 'Build Version File', + 'action_name': 'Build Version Header', 'inputs': [ '../scripts/build-version.py', ], 'outputs': [ '<(SHARED_INTERMEDIATE_DIR)/include/mbgl/util/version.hpp', - '<(SHARED_INTERMEDIATE_DIR)/src/mbgl/util/mbgl_version.cpp', ], 'action': ['<@(python)', 'scripts/build-version.py', '<(SHARED_INTERMEDIATE_DIR)', '(glGetString(GL_EXTENSIONS)); { using namespace mbgl; diff --git a/platform/default/headless_display.cpp b/platform/default/headless_display.cpp index ada48b864..40cc0640a 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 0041fb3bb..5942f2c46 100644 --- a/platform/default/headless_view.cpp +++ b/platform/default/headless_view.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #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."); } } @@ -75,14 +76,22 @@ 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 @@ -99,7 +108,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}, @@ -113,39 +122,39 @@ static const core_profile_version core_profile_versions[] = { {0, 0}, }; -GLXContext createCoreProfile(Display *dpy, GLXFBConfig fbconfig, bool& usingGl3OrNewer) { - static bool context_creation_failed = false; +GLXContext createCoreProfile(Display *dpy, GLXFBConfig fbConfig, bool& usingGl3OrNewer) { + 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. int i = 0; - for (; !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 (; !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; } } - if (core_profile_versions[i].major >= 3) { + if (coreProfileVersions[i].major >= 3) { usingGl3OrNewer = true; } // Restore the old error handler. - XSetErrorHandler(previous_error_handler); + XSetErrorHandler(previousErrorHandler); return ctx; } #endif @@ -153,14 +162,14 @@ GLXContext createCoreProfile(Display *dpy, GLXFBConfig fbconfig, bool& usingGl3O 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 @@ -171,39 +180,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], usingGl3OrNewer); + glContext = createCoreProfile(xDisplay, fbConfigs[0], usingGl3OrNewer); } #endif - if (gl_context == 0) { + if (glContext == 0) { // 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 } @@ -220,34 +229,34 @@ 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; + 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; } throw std::runtime_error(error.str()); @@ -288,14 +297,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(); @@ -305,16 +314,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 } @@ -328,15 +337,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 } @@ -344,14 +353,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 } diff --git a/scripts/build-version.py b/scripts/build-version.py index 8261aea53..b768f7f43 100644 --- a/scripts/build-version.py +++ b/scripts/build-version.py @@ -60,30 +60,3 @@ header_path = os.path.join(output_dir, 'include/mbgl/util/version.hpp') mkdir_p(os.path.dirname(header_path)) with open(header_path, 'w') as f: f.write(header) -source = """// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED. -#include - -namespace mbgl {{ -namespace version {{ - -const int major = {major}; -const int minor = {minor}; -const int patch = {patch}; -const char *revision = "{rev}"; -const char *string = "{major}.{minor}.{patch}"; -const unsigned int number = 0x{major:02x}{minor:02x}{patch:02x}; - -}} -}} -""".format( - major = tag[0], - minor = tag[1], - patch = tag[2], - rev = rev -) - -# Note: We can't use version.cpp since libuv already has a file named version.c. -# Having another one here would overwrite that file and cause missing symbols errors. -source_path = os.path.join(output_dir, 'src/mbgl/util/mbgl_version.cpp') -mkdir_p(os.path.dirname(source_path)) -with open(source_path, 'w') as f: f.write(source) diff --git a/src/mbgl/util/version_info.cpp b/src/mbgl/util/version_info.cpp new file mode 100644 index 000000000..1d649d2b6 --- /dev/null +++ b/src/mbgl/util/version_info.cpp @@ -0,0 +1,14 @@ +#include + +namespace mbgl { +namespace version { + +const int major = MBGL_VERSION_MAJOR; +const int minor = MBGL_VERSION_MINOR; +const int patch = MBGL_VERSION_PATCH; +const char *revision = MBGL_VERSION_REV; +const char *string = MBGL_VERSION_STRING; +const unsigned int number = MBGL_VERSION; + +} +} \ No newline at end of file diff --git a/utils/mbgl-config/mbgl-config.template.sh b/utils/mbgl-config/mbgl-config.template.sh index 84fce43c1..89e28f64a 100644 --- a/utils/mbgl-config/mbgl-config.template.sh +++ b/utils/mbgl-config/mbgl-config.template.sh @@ -24,6 +24,12 @@ if test $# -eq 0; then usage 1 fi +if [ ${CONFIG_MBGL_PLATFORM} == 'linux' ]; then + LIBS="-Wl,--start-group -lmbgl-core -lmbgl-${CONFIG_MBGL_PLATFORM} -Wl,--end-group -lmbgl-headless" +else + LIBS="-lmbgl-core -lmbgl-${CONFIG_MBGL_PLATFORM} -lmbgl-headless" +fi + while test $# -gt 0; do case "$1" in esac @@ -43,7 +49,7 @@ while test $# -gt 0; do ;; --libs) - echo -L${CONFIG_MBGL_PREFIX}/lib -lmbgl-core -lmbgl-${CONFIG_MBGL_PLATFORM} -lmbgl-headless ${CONFIG_MBGL_LDFLAGS} + echo -L${CONFIG_MBGL_PREFIX}/lib ${LIBS} ${CONFIG_MBGL_LDFLAGS} ;; --includedir)