From f51bd7fcf17c474f106a2c14c00d527d3f8601a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Mon, 7 Jul 2014 14:49:23 -0700 Subject: [PATCH 1/2] first draft at glx headless rendering [skip ci] --- common/headless_view.cpp | 58 ++++++++++++++++++++++++++++++++++++++++ common/headless_view.hpp | 21 ++++++++++++++- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/common/headless_view.cpp b/common/headless_view.cpp index 7fa5b8394..4fd7b42ae 100644 --- a/common/headless_view.cpp +++ b/common/headless_view.cpp @@ -13,6 +13,7 @@ void notify_map_change(MapChange change) { } HeadlessView::HeadlessView() { +#if LLMR_USE_CGL // TODO: test if OpenGL 4.1 with GL_ARB_ES2_compatibility is supported // If it is, use kCGLOGLPVersion_3_2_Core and enable that extension. CGLPixelFormatAttribute attributes[] = { @@ -36,6 +37,26 @@ HeadlessView::HeadlessView() { fprintf(stderr, "Error creating GL context object\n"); return; } +#endif + + #ifdef USE_GLX + x_display = XOpenDisplay(0); + + static int pixelFormat[] = { + GLX_RGBA, + GLX_DOUBLEBUFFER, + GLX_RED_SIZE, 8, + GLX_GREEN_SIZE, 8, + GLX_BLUE_SIZE, 8, + GLX_ALPHA_SIZE, 8, + GLX_DEPTH_SIZE, 24, + GLX_STENCIL_SIZE, 8, + None + }; + + x_info = glXChooseVisual(x_display, DefaultScreen(x_display), pixelFormat); + gl_context = glXCreateContext(display, x_info, 0, GL_TRUE); + #endif make_active(); } @@ -44,6 +65,7 @@ HeadlessView::HeadlessView() { void HeadlessView::resize(int width, int height) { clear_buffers(); +#if LLMR_USE_CGL // Create depth/stencil buffer glGenRenderbuffersEXT(1, &fbo_depth_stencil); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo_depth_stencil); @@ -74,9 +96,17 @@ void HeadlessView::resize(int width, int height) { } return; } +#endif + +#if LLMR_USE_GLX + x_pixmap = XCreatePixmap(display, DefaultRootWindow(display), width, height, 32); + glx_pixmap = glXCreateGLXPixmap(display, x_info, x_pixmap); +#endif + } void HeadlessView::clear_buffers() { +#if LLMR_USE_CGL glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); if (fbo) { @@ -93,24 +123,52 @@ void HeadlessView::clear_buffers() { glDeleteRenderbuffersEXT(1, &fbo_depth_stencil); fbo_depth_stencil = 0; } +#endif + +#if LLMR_USE_GLX + if (glx_pixmap) { + glXDestroyGLXPixmap(x_display, glx_pixmap); + glx_pixmap = nullptr; + } + + if (x_pixmap) { + XFreePixmap(x_display, x_pixmap); + x_pixmap = nullptr; + } +#endif } HeadlessView::~HeadlessView() { clear_buffers(); + +#if LLMR_USE_CGL CGLDestroyContext(gl_context); +#endif } void HeadlessView::make_active() { +#if LLMR_USE_CGL CGLError error = CGLSetCurrentContext(gl_context); if (error) { fprintf(stderr, "Switching OpenGL context failed\n"); } +#endif + +#if LLMR_USE_GLX + if (!glXMakeCurrent(display, glx_pixmap, gl_context)) { + fprintf(stderr, "Switching OpenGL context failed\n"); + } +#endif } void HeadlessView::swap() {} unsigned int HeadlessView::root_fbo() { +#if LLMR_USE_CGL return fbo; +#endif + + return 0; } } diff --git a/common/headless_view.hpp b/common/headless_view.hpp index b79b0c974..57cdec524 100644 --- a/common/headless_view.hpp +++ b/common/headless_view.hpp @@ -1,6 +1,15 @@ #ifndef LLMR_COMMON_HEADLESS_CGL #define LLMR_COMMON_HEADLESS_CGL +#ifdef __APPLE__ +#define LLMR_USE_CGL 1 +#define LLMR_CONTEXT_OBJ CGLContextObj +#else +#include +#define LLMR_USE_GLX 1 +#define LLMR_CONTEXT_OBJ GLXContext +#endif + #include #include @@ -22,10 +31,20 @@ private: private: - CGLContextObj gl_context; + LLMR_CONTEXT_OBJ gl_context; + +#ifdef LLMR_USE_CGL GLuint fbo = 0; GLuint fbo_depth_stencil = 0; GLuint fbo_color = 0; +#endif + +#ifdef LLMR_USE_GLX + XVisualInfo *x_info = nullptr; + Display *x_display = nullptr; + Pixmap x_pixmap; + GLXPixmap glx_pixmap; +#endif }; } From 501af4a3807474d6de288939266b1f866889bf60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Mon, 7 Jul 2014 16:52:23 -0700 Subject: [PATCH 2/2] fix headless rendering on travis and actually execute the headless test fixes #357 --- Makefile | 6 ++---- common/headless_view.cpp | 32 +++++++++++++++++++++++--------- common/headless_view.hpp | 10 ++++------ linux/llmr-app.gyp | 29 ++--------------------------- llmr.gyp | 25 +++++++++++++++++++++++++ scripts/setup_travis.sh | 3 +++ test/test.gyp | 10 +++++++++- 7 files changed, 68 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index 0c6d6eebb..65f781298 100644 --- a/Makefile +++ b/Makefile @@ -29,13 +29,11 @@ test: build/test/Makefile # Runs all test cases run-tests: test - @for FILE in build/$(BUILDTYPE)/test_*; do \ - $${FILE}; \ - done + (cd build/${BUILDTYPE} && exec find . -maxdepth 1 -name "test_*" -exec {} \;) test/%: build/test/Makefile $(MAKE) -C build/test BUILDTYPE=$(BUILDTYPE) V=$(V) $* - (cd build/$(BUILDTYPE) && ./test_$*) + (cd build/$(BUILDTYPE) && exec ./test_$*) ##### Makefile builds ########################################################## diff --git a/common/headless_view.cpp b/common/headless_view.cpp index 4fd7b42ae..a44c6e6ed 100644 --- a/common/headless_view.cpp +++ b/common/headless_view.cpp @@ -39,9 +39,13 @@ HeadlessView::HeadlessView() { } #endif - #ifdef USE_GLX +#if LLMR_USE_GLX x_display = XOpenDisplay(0); + if (x_display == nullptr) { + throw std::runtime_error("Failed to open X display"); + } + static int pixelFormat[] = { GLX_RGBA, GLX_DOUBLEBUFFER, @@ -55,10 +59,16 @@ HeadlessView::HeadlessView() { }; x_info = glXChooseVisual(x_display, DefaultScreen(x_display), pixelFormat); - gl_context = glXCreateContext(display, x_info, 0, GL_TRUE); - #endif - make_active(); + if (x_info == nullptr) { + throw std::runtime_error("Error pixel format"); + } + + gl_context = glXCreateContext(x_display, x_info, 0, GL_TRUE); + if (gl_context == nullptr) { + throw std::runtime_error("Error creating GL context object"); + } +#endif } @@ -66,6 +76,8 @@ void HeadlessView::resize(int width, int height) { clear_buffers(); #if LLMR_USE_CGL + make_active(); + // Create depth/stencil buffer glGenRenderbuffersEXT(1, &fbo_depth_stencil); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo_depth_stencil); @@ -99,8 +111,10 @@ void HeadlessView::resize(int width, int height) { #endif #if LLMR_USE_GLX - x_pixmap = XCreatePixmap(display, DefaultRootWindow(display), width, height, 32); - glx_pixmap = glXCreateGLXPixmap(display, x_info, x_pixmap); + x_pixmap = XCreatePixmap(x_display, DefaultRootWindow(x_display), width, height, 32); + glx_pixmap = glXCreateGLXPixmap(x_display, x_info, x_pixmap); + + make_active(); #endif } @@ -128,12 +142,12 @@ void HeadlessView::clear_buffers() { #if LLMR_USE_GLX if (glx_pixmap) { glXDestroyGLXPixmap(x_display, glx_pixmap); - glx_pixmap = nullptr; + glx_pixmap = 0; } if (x_pixmap) { XFreePixmap(x_display, x_pixmap); - x_pixmap = nullptr; + x_pixmap = 0; } #endif } @@ -155,7 +169,7 @@ void HeadlessView::make_active() { #endif #if LLMR_USE_GLX - if (!glXMakeCurrent(display, glx_pixmap, gl_context)) { + if (!glXMakeCurrent(x_display, glx_pixmap, gl_context)) { fprintf(stderr, "Switching OpenGL context failed\n"); } #endif diff --git a/common/headless_view.hpp b/common/headless_view.hpp index 57cdec524..a203d477e 100644 --- a/common/headless_view.hpp +++ b/common/headless_view.hpp @@ -3,11 +3,9 @@ #ifdef __APPLE__ #define LLMR_USE_CGL 1 -#define LLMR_CONTEXT_OBJ CGLContextObj #else #include #define LLMR_USE_GLX 1 -#define LLMR_CONTEXT_OBJ GLXContext #endif #include @@ -31,19 +29,19 @@ private: private: - LLMR_CONTEXT_OBJ gl_context; - #ifdef LLMR_USE_CGL + CGLContextObj gl_context; GLuint fbo = 0; GLuint fbo_depth_stencil = 0; GLuint fbo_color = 0; #endif #ifdef LLMR_USE_GLX + GLXContext gl_context = nullptr; XVisualInfo *x_info = nullptr; Display *x_display = nullptr; - Pixmap x_pixmap; - GLXPixmap glx_pixmap; + Pixmap x_pixmap = 0; + GLXPixmap glx_pixmap = 0; #endif }; diff --git a/linux/llmr-app.gyp b/linux/llmr-app.gyp index c6ab686cb..0a524c088 100644 --- a/linux/llmr-app.gyp +++ b/linux/llmr-app.gyp @@ -51,34 +51,9 @@ ], 'dependencies': [ '../llmr.gyp:llmr-x86', - 'default_stylesheet', - 'certificate_bundle', + '../llmr.gyp:copy_default_stylesheet', + '../llmr.gyp:copy_certificate_bundle', ], }, - { - 'target_name': 'default_stylesheet', - 'type': 'none', - 'hard_dependency': 1, - 'dependencies': [ - '../llmr.gyp:build_stylesheet' - ], - 'copies': [ - { - 'files': [ '<(SHARED_INTERMEDIATE_DIR)/bin/style.min.js' ], - 'destination': '<(PRODUCT_DIR)' - } - ] - }, - { - 'target_name': 'certificate_bundle', - 'type': 'none', - 'hard_dependency': 1, - 'copies': [ - { - 'files': [ '../common/ca-bundle.crt' ], - 'destination': '<(PRODUCT_DIR)' - } - ] - } ], } diff --git a/llmr.gyp b/llmr.gyp index 2ffb3ae63..700be136a 100644 --- a/llmr.gyp +++ b/llmr.gyp @@ -55,6 +55,31 @@ ], } }, + { + 'target_name': 'copy_default_stylesheet', + 'type': 'none', + 'hard_dependency': 1, + 'dependencies': [ + 'build_stylesheet' + ], + 'copies': [ + { + 'files': [ '<(SHARED_INTERMEDIATE_DIR)/bin/style.min.js' ], + 'destination': '<(PRODUCT_DIR)' + } + ] + }, + { + 'target_name': 'copy_certificate_bundle', + 'type': 'none', + 'hard_dependency': 1, + 'copies': [ + { + 'files': [ 'common/ca-bundle.crt' ], + 'destination': '<(PRODUCT_DIR)' + } + ] + }, { 'target_name': 'llmr-x86', 'product_name': 'llmr-x86', diff --git a/scripts/setup_travis.sh b/scripts/setup_travis.sh index 83b0d2c28..3b819ea81 100755 --- a/scripts/setup_travis.sh +++ b/scripts/setup_travis.sh @@ -22,6 +22,9 @@ if [[ ${TRAVIS_OS_NAME} == "linux" ]]; then sudo apt-get -y install git build-essential zlib1g-dev automake libtool xutils-dev make cmake pkg-config sudo apt-get -y install libxi-dev libglu1-mesa-dev x11proto-randr-dev x11proto-xext-dev libxrandr-dev x11proto-xf86vidmode-dev libxxf86vm-dev libxcursor-dev + export DISPLAY=:99.0 + sh -e /etc/init.d/xvfb start + # use g++ that supports c++11 if [[ ${CXX} == "g++" ]]; then export CXX="g++-4.8" ; diff --git a/test/test.gyp b/test/test.gyp index 98effdfa9..340a73ba3 100644 --- a/test/test.gyp +++ b/test/test.gyp @@ -16,7 +16,12 @@ ], }, }, { - # TODO: add OpenGL link settings for linux + 'link_settings': { + 'libraries': [ + '<@(glfw3_libraries)', # This is a hack since we're not actually using GLFW + '-lboost_regex', + ], + }, }], ], }, @@ -148,6 +153,8 @@ "../llmr.gyp:llmr-x86", "link_gl", "link_curl", + '../llmr.gyp:copy_default_stylesheet', + '../llmr.gyp:copy_certificate_bundle', ] }, { @@ -159,6 +166,7 @@ "variant", "tile", "functions", + "headless", ], } ]