break out HeadlessDisplay from HeadlessView, ref #478

This commit is contained in:
Mike Morris
2014-10-03 18:31:38 -04:00
parent c4c4b3318a
commit 0f1d33f5ee
5 changed files with 122 additions and 59 deletions

View File

@@ -0,0 +1,65 @@
#include "headless_view.hpp"
#include <stdexcept>
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
}
}

View File

@@ -0,0 +1,32 @@
#ifndef MBGL_COMMON_HEADLESS_DISPLAY
#define MBGL_COMMON_HEADLESS_DISPLAY
#ifdef __APPLE__
#define MBGL_USE_CGL 1
#else
#include <GL/glx.h>
#define MBGL_USE_GLX 1
#endif
#include <mbgl/platform/gl.hpp>
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

View File

@@ -1,31 +1,21 @@
#include "headless_view.hpp"
#include <mbgl/util/timer.hpp>
#include <stdexcept>
namespace mbgl {
HeadlessView::HeadlessView() {
HeadlessView::HeadlessView() : display_(new HeadlessDisplay()) {
createContext();
}
HeadlessView::HeadlessView(HeadlessDisplay *display)
: display_(display) {
createContext();
}
void HeadlessView::createContext() {
#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;
}
error = CGLCreateContext(pixelFormat, NULL, &gl_context);
CGLDestroyPixelFormat(pixelFormat);
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
}

View File

@@ -1,24 +1,20 @@
#ifndef MBGL_COMMON_HEADLESS_CGL
#define MBGL_COMMON_HEADLESS_CGL
#ifndef MBGL_COMMON_HEADLESS_VIEW
#define MBGL_COMMON_HEADLESS_VIEW
#ifdef __APPLE__
#define MBGL_USE_CGL 1
#else
#include <GL/glx.h>
#define MBGL_USE_GLX 1
#endif
#include "headless_display.hpp"
#include <mbgl/map/view.hpp>
#include <mbgl/platform/gl.hpp>
#include <mbgl/util/time.hpp>
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

View File

@@ -201,6 +201,8 @@
"./headless.cpp",
"../common/headless_view.hpp",
"../common/headless_view.cpp",
"../common/headless_display.hpp",
"../common/headless_display.cpp",
"../common/platform_default.cpp",
"./fixtures/fixture_request.cpp",
"./fixtures/fixture_log.hpp",