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 }; }