From 0f1d33f5ee29adf9b62c912339935fdde44b0b98 Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Fri, 3 Oct 2014 18:31:38 -0400 Subject: break out HeadlessDisplay from HeadlessView, ref #478 --- common/headless_display.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++ common/headless_display.hpp | 32 ++++++++++++++++++++++ common/headless_view.cpp | 60 +++++++++-------------------------------- common/headless_view.hpp | 24 ++++++++--------- 4 files changed, 121 insertions(+), 60 deletions(-) create mode 100644 common/headless_display.cpp create mode 100644 common/headless_display.hpp (limited to 'common') diff --git a/common/headless_display.cpp b/common/headless_display.cpp new file mode 100644 index 0000000000..580d3ef587 --- /dev/null +++ b/common/headless_display.cpp @@ -0,0 +1,65 @@ +#include "headless_view.hpp" + +#include + +namespace mbgl { + +HeadlessDisplay::HeadlessDisplay() { +#if MBGL_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[] = { + kCGLPFAOpenGLProfile, + (CGLPixelFormatAttribute) kCGLOGLPVersion_Legacy, + kCGLPFAAccelerated, + (CGLPixelFormatAttribute) 0 + }; + + GLint num; + CGLError error = CGLChoosePixelFormat(attributes, &pixelFormat, &num); + if (error) { + fprintf(stderr, "Error pixel format: %s\n", CGLErrorString(error)); + return; + } +#endif + +#if MBGL_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, + 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); + + if (x_info == nullptr) { + throw std::runtime_error("Error pixel format"); + } +#endif +} + +HeadlessDisplay::~HeadlessDisplay() { +#if MBGL_USE_CGL + CGLDestroyPixelFormat(pixelFormat); +#endif + +#if MBGL_USE_GLX + XFree(x_info); + XCloseDisplay(x_display); +#endif +} + +} + diff --git a/common/headless_display.hpp b/common/headless_display.hpp new file mode 100644 index 0000000000..4fc03da083 --- /dev/null +++ b/common/headless_display.hpp @@ -0,0 +1,32 @@ +#ifndef MBGL_COMMON_HEADLESS_DISPLAY +#define MBGL_COMMON_HEADLESS_DISPLAY + +#ifdef __APPLE__ +#define MBGL_USE_CGL 1 +#else +#include +#define MBGL_USE_GLX 1 +#endif + +#include + +namespace mbgl { + +class HeadlessDisplay { +public: + HeadlessDisplay(); + ~HeadlessDisplay(); + +#if MBGL_USE_CGL + CGLPixelFormatObj pixelFormat; +#endif + +#if MBGL_USE_GLX + Display *x_display = nullptr; + XVisualInfo *x_info = nullptr; +#endif +}; + +} + +#endif diff --git a/common/headless_view.cpp b/common/headless_view.cpp index 10888f7a76..47cf3bd661 100644 --- a/common/headless_view.cpp +++ b/common/headless_view.cpp @@ -1,31 +1,21 @@ #include "headless_view.hpp" -#include #include namespace mbgl { -HeadlessView::HeadlessView() { -#if MBGL_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[] = { - kCGLPFAOpenGLProfile, - (CGLPixelFormatAttribute) kCGLOGLPVersion_Legacy, - kCGLPFAAccelerated, - (CGLPixelFormatAttribute) 0 - }; - - CGLPixelFormatObj pixelFormat; - GLint num; - CGLError error = CGLChoosePixelFormat(attributes, &pixelFormat, &num); - if (error) { - fprintf(stderr, "Error pixel format: %s\n", CGLErrorString(error)); - return; - } +HeadlessView::HeadlessView() : display_(new HeadlessDisplay()) { + createContext(); +} + +HeadlessView::HeadlessView(HeadlessDisplay *display) + : display_(display) { + createContext(); +} - error = CGLCreateContext(pixelFormat, NULL, &gl_context); - CGLDestroyPixelFormat(pixelFormat); +void HeadlessView::createContext() { +#if MBGL_USE_CGL + CGLError error = CGLCreateContext(display_->pixelFormat, NULL, &gl_context); if (error) { fprintf(stderr, "Error creating GL context object\n"); return; @@ -39,29 +29,8 @@ HeadlessView::HeadlessView() { #endif #if MBGL_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, - 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); - - if (x_info == nullptr) { - throw std::runtime_error("Error pixel format"); - } + x_display = display_->x_display; + x_info = display_->x_info; gl_context = glXCreateContext(x_display, x_info, 0, GL_TRUE); if (gl_context == nullptr) { @@ -70,7 +39,6 @@ HeadlessView::HeadlessView() { #endif } - void HeadlessView::resize(uint16_t width, uint16_t height, float pixelRatio) { clear_buffers(); @@ -171,8 +139,6 @@ HeadlessView::~HeadlessView() { #if MBGL_USE_GLX glXDestroyContext(x_display, gl_context); - XFree(x_info); - XCloseDisplay(x_display); #endif } diff --git a/common/headless_view.hpp b/common/headless_view.hpp index c8475a2516..d1b09ce700 100644 --- a/common/headless_view.hpp +++ b/common/headless_view.hpp @@ -1,24 +1,20 @@ -#ifndef MBGL_COMMON_HEADLESS_CGL -#define MBGL_COMMON_HEADLESS_CGL - -#ifdef __APPLE__ -#define MBGL_USE_CGL 1 -#else -#include -#define MBGL_USE_GLX 1 -#endif +#ifndef MBGL_COMMON_HEADLESS_VIEW +#define MBGL_COMMON_HEADLESS_VIEW + +#include "headless_display.hpp" #include -#include -#include namespace mbgl { class HeadlessView : public View { public: HeadlessView(); + HeadlessView(HeadlessDisplay *display); ~HeadlessView(); + void createContext(); + void resize(uint16_t width, uint16_t height, float pixelRatio); void notify_map_change(MapChange change, timestamp delay = 0); @@ -32,6 +28,8 @@ private: private: + HeadlessDisplay *display_; + #if MBGL_USE_CGL CGLContextObj gl_context; GLuint fbo = 0; @@ -40,9 +38,9 @@ private: #endif #if MBGL_USE_GLX - GLXContext gl_context = nullptr; - XVisualInfo *x_info = nullptr; Display *x_display = nullptr; + XVisualInfo *x_info = nullptr; + GLXContext gl_context = nullptr; Pixmap x_pixmap = 0; GLXPixmap glx_pixmap = 0; #endif -- cgit v1.2.1