[core] fold HeadlessDisplay into the headless RenderBackend implementation

This commit is contained in:
Konstantin Käfer
2017-11-27 16:34:52 +01:00
parent 772b909062
commit 335f04f7e1
17 changed files with 288 additions and 445 deletions

View File

@@ -1,5 +1,5 @@
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/headless_display.hpp>
#include <mbgl/util/logging.hpp>
#include <OpenGL/OpenGL.h>
#include <CoreFoundation/CoreFoundation.h>
@@ -9,11 +9,78 @@
namespace mbgl {
struct CGLImpl : public HeadlessBackend::Impl {
CGLImpl(CGLContextObj glContext_) : glContext(glContext_) {
// This class provides a singleton that contains information about the pixel format used for
// instantiating new headless rendering contexts.
class CGLDisplayConfig {
private:
// Key for singleton construction.
struct Key { explicit Key() = default; };
public:
CGLDisplayConfig(Key) {
// 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, static_cast<CGLPixelFormatAttribute>(kCGLOGLPVersion_Legacy),
// Not adding kCGLPFAAccelerated, as this will make headless rendering impossible when running in VMs.
kCGLPFAClosestPolicy,
kCGLPFAAccumSize, static_cast<CGLPixelFormatAttribute>(32),
kCGLPFAColorSize, static_cast<CGLPixelFormatAttribute>(24),
kCGLPFAAlphaSize, static_cast<CGLPixelFormatAttribute>(8),
kCGLPFADepthSize, static_cast<CGLPixelFormatAttribute>(16),
kCGLPFAStencilSize, static_cast<CGLPixelFormatAttribute>(8),
kCGLPFASupportsAutomaticGraphicsSwitching,
kCGLPFAAllowOfflineRenderers, // Allows using the integrated GPU
static_cast<CGLPixelFormatAttribute>(0)
};
GLint num;
// TODO: obtain all configurations and choose the best one.
const CGLError error = CGLChoosePixelFormat(attributes, &pixelFormat, &num);
if (error != kCGLNoError) {
throw std::runtime_error(std::string("Error choosing pixel format:") + CGLErrorString(error) + "\n");
}
if (num <= 0) {
throw std::runtime_error("No pixel formats found.");
}
}
~CGLImpl() {
~CGLDisplayConfig() {
const CGLError error = CGLDestroyPixelFormat(pixelFormat);
if (error != kCGLNoError) {
Log::Error(Event::OpenGL, std::string("Error destroying pixel format:") + CGLErrorString(error));
}
}
static std::shared_ptr<const CGLDisplayConfig> create() {
static std::weak_ptr<const CGLDisplayConfig> instance;
auto shared = instance.lock();
if (!shared) {
instance = shared = std::make_shared<CGLDisplayConfig>(Key{});
}
return shared;
}
public:
CGLPixelFormatObj pixelFormat = nullptr;
};
struct CGLImpl : public HeadlessBackend::Impl {
CGLImpl() {
CGLError error = CGLCreateContext(cglDisplay->pixelFormat, nullptr, &glContext);
if (error != kCGLNoError) {
throw std::runtime_error(std::string("Error creating GL context object:") +
CGLErrorString(error) + "\n");
}
error = CGLEnable(glContext, kCGLCEMPEngine);
if (error != kCGLNoError) {
throw std::runtime_error(std::string("Error enabling OpenGL multithreading:") +
CGLErrorString(error) + "\n");
}
}
~CGLImpl() final {
CGLDestroyContext(glContext);
}
@@ -33,10 +100,13 @@ struct CGLImpl : public HeadlessBackend::Impl {
}
}
private:
const std::shared_ptr<const CGLDisplayConfig> cglDisplay = CGLDisplayConfig::create();
CGLContextObj glContext = nullptr;
};
gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) {
assert(hasContext());
static CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
if (!framework) {
throw std::runtime_error("Failed to load OpenGL framework.");
@@ -49,30 +119,9 @@ gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) {
return reinterpret_cast<gl::ProcAddress>(symbol);
}
bool HeadlessBackend::hasDisplay() {
if (!display) {
display = HeadlessDisplay::create();
}
return bool(display);
}
void HeadlessBackend::createContext() {
assert(!hasContext());
CGLContextObj glContext = nullptr;
CGLError error = CGLCreateContext(display->attribute<CGLPixelFormatObj>(), nullptr, &glContext);
if (error != kCGLNoError) {
throw std::runtime_error(std::string("Error creating GL context object:") +
CGLErrorString(error) + "\n");
}
error = CGLEnable(glContext, kCGLCEMPEngine);
if (error != kCGLNoError) {
throw std::runtime_error(std::string("Error enabling OpenGL multithreading:") +
CGLErrorString(error) + "\n");
}
impl.reset(new CGLImpl(glContext));
impl = std::make_unique<CGLImpl>();
}
} // namespace mbgl

View File

@@ -7,23 +7,26 @@
namespace mbgl {
struct EAGLImpl : public HeadlessBackend::Impl {
EAGLImpl(EAGLContext* glContext_) : glContext(glContext_) {
[reinterpret_cast<EAGLContext*>(glContext) retain];
reinterpret_cast<EAGLContext*>(glContext).multiThreaded = YES;
EAGLImpl() {
glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (glContext == nil) {
throw std::runtime_error("Error creating GL context object");
}
glContext.multiThreaded = YES;
}
~EAGLImpl() {
[glContext release];
}
// Required for ARC to deallocate correctly.
~EAGLImpl() final = default;
void activateContext() {
void activateContext() final {
[EAGLContext setCurrentContext:glContext];
}
void deactivateContext() {
void deactivateContext() final {
[EAGLContext setCurrentContext:nil];
}
private:
EAGLContext* glContext = nullptr;
};
@@ -40,17 +43,9 @@ gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) {
return reinterpret_cast<gl::ProcAddress>(symbol);
}
bool HeadlessBackend::hasDisplay() {
return true;
}
void HeadlessBackend::createContext() {
EAGLContext* glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (glContext == nil) {
throw std::runtime_error("Error creating GL context object");
}
impl.reset(new EAGLImpl(glContext));
assert(!hasContext());
impl = std::make_unique<EAGLImpl>();
}
} // namespace mbgl

View File

@@ -1,60 +0,0 @@
#include <mbgl/gl/headless_display.hpp>
#include <OpenGL/OpenGL.h>
#include <stdexcept>
#include <string>
namespace mbgl {
class HeadlessDisplay::Impl {
public:
Impl();
~Impl();
CGLPixelFormatObj pixelFormat = nullptr;
};
HeadlessDisplay::Impl::Impl() {
// 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, static_cast<CGLPixelFormatAttribute>(kCGLOGLPVersion_Legacy),
// Not adding kCGLPFAAccelerated, as this will make headless rendering impossible when running in VMs.
kCGLPFAClosestPolicy,
kCGLPFAAccumSize, static_cast<CGLPixelFormatAttribute>(32),
kCGLPFAColorSize, static_cast<CGLPixelFormatAttribute>(24),
kCGLPFAAlphaSize, static_cast<CGLPixelFormatAttribute>(8),
kCGLPFADepthSize, static_cast<CGLPixelFormatAttribute>(16),
kCGLPFAStencilSize, static_cast<CGLPixelFormatAttribute>(8),
kCGLPFASupportsAutomaticGraphicsSwitching,
kCGLPFAAllowOfflineRenderers, // Allows using the integrated GPU
static_cast<CGLPixelFormatAttribute>(0)
};
GLint num;
CGLError error = CGLChoosePixelFormat(attributes, &pixelFormat, &num);
if (error != kCGLNoError) {
throw std::runtime_error(std::string("Error choosing pixel format:") + CGLErrorString(error) + "\n");
}
if (num <= 0) {
throw std::runtime_error("No pixel formats found.");
}
}
HeadlessDisplay::Impl::~Impl() {
CGLDestroyPixelFormat(pixelFormat);
}
template <>
CGLPixelFormatObj HeadlessDisplay::attribute() const {
return impl->pixelFormat;
}
HeadlessDisplay::HeadlessDisplay()
: impl(std::make_unique<Impl>()) {
}
HeadlessDisplay::~HeadlessDisplay() {
}
} // namespace mbgl

View File

@@ -8,10 +8,18 @@
namespace mbgl {
struct OSMesaImpl : public HeadlessBackend::Impl {
OSMesaImpl(OSMesaContext glContext_) : glContext(glContext_) {
OSMesaImpl() {
#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
glContext = OSMesaCreateContextExt(OSMESA_RGBA, 16, 0, 0, nullptr);
#else
glContext = OSMesaCreateContext(OSMESA_RGBA, nullptr);
#endif
if (glContext == nullptr) {
throw std::runtime_error("Error creating GL context object.");
}
}
~OSMesaImpl() {
~OSMesaImpl() final {
OSMesaDestroyContext(glContext);
}
@@ -21,6 +29,7 @@ struct OSMesaImpl : public HeadlessBackend::Impl {
}
}
private:
OSMesaContext glContext = nullptr;
GLubyte fakeBuffer = 0;
};
@@ -29,23 +38,9 @@ gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) {
return OSMesaGetProcAddress(name);
}
bool HeadlessBackend::hasDisplay() {
return true;
};
void HeadlessBackend::createContext() {
assert(!hasContext());
#if OSMESA_MAJOR_VERSION * 100 + OSMESA_MINOR_VERSION >= 305
OSMesaContext glContext = OSMesaCreateContextExt(OSMESA_RGBA, 16, 0, 0, nullptr);
#else
OSMesaContext glContext = OSMesaCreateContext(OSMESA_RGBA, nullptr);
#endif
if (glContext == nullptr) {
throw std::runtime_error("Error creating GL context object.");
}
impl.reset(new OSMesaImpl(glContext));
impl = std::make_unique<OSMesaImpl>();
}
} // namespace mbgl

View File

@@ -1,5 +1,4 @@
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/headless_display.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/renderer/backend_scope.hpp>
@@ -36,9 +35,6 @@ void HeadlessBackend::activate() {
active = true;
if (!hasContext()) {
if (!hasDisplay()) {
throw std::runtime_error("Display is not set");
}
createContext();
}

View File

@@ -7,8 +7,6 @@
namespace mbgl {
class HeadlessDisplay;
class HeadlessBackend : public RendererBackend {
public:
HeadlessBackend(Size = { 256, 256 });
@@ -35,11 +33,9 @@ private:
void deactivate() override;
bool hasContext() const { return bool(impl); }
bool hasDisplay();
void createContext();
std::shared_ptr<HeadlessDisplay> display;
std::unique_ptr<Impl> impl;
Size size;

View File

@@ -1,15 +0,0 @@
#include <mbgl/gl/headless_display.hpp>
namespace mbgl {
class HeadlessDisplay::Impl {};
HeadlessDisplay::HeadlessDisplay() {
// no-op
}
HeadlessDisplay::~HeadlessDisplay() {
// no-op
}
} // namespace mbgl

View File

@@ -1,34 +0,0 @@
#pragma once
#include <memory>
namespace mbgl {
class HeadlessDisplay {
public:
static std::shared_ptr<HeadlessDisplay> create() {
static std::weak_ptr<HeadlessDisplay> instance;
auto shared = instance.lock();
if (!shared) {
instance = shared = std::shared_ptr<HeadlessDisplay>(new HeadlessDisplay());
}
return shared;
}
~HeadlessDisplay();
template <typename DisplayAttribute>
DisplayAttribute attribute() const;
private:
HeadlessDisplay();
class Impl;
std::unique_ptr<Impl> impl;
};
} // namespace mbgl

View File

@@ -46,8 +46,6 @@ macro(mbgl_platform_core)
PRIVATE platform/default/mbgl/gl/headless_backend.cpp
PRIVATE platform/default/mbgl/gl/headless_backend.hpp
PRIVATE platform/darwin/src/headless_backend_eagl.mm
PRIVATE platform/default/mbgl/gl/headless_display.cpp
PRIVATE platform/default/mbgl/gl/headless_display.hpp
# Snapshotting
PRIVATE platform/default/mbgl/map/map_snapshotter.cpp
@@ -68,13 +66,6 @@ macro(mbgl_platform_core)
PRIVATE -fvisibility=hidden
)
# TODO: Remove this by converting to ARC
set_source_files_properties(
platform/darwin/src/headless_backend_eagl.mm
PROPERTIES
COMPILE_FLAGS -fno-objc-arc
)
target_include_directories(mbgl-core
PUBLIC platform/darwin
PUBLIC platform/default

View File

@@ -19,7 +19,6 @@ macro(mbgl_platform_core)
if(WITH_OSMESA)
target_sources(mbgl-core
PRIVATE platform/default/headless_backend_osmesa.cpp
PRIVATE platform/default/mbgl/gl/headless_display.cpp
)
target_link_libraries(mbgl-core
PUBLIC -lOSMesa
@@ -27,7 +26,6 @@ macro(mbgl_platform_core)
elseif(WITH_EGL)
target_sources(mbgl-core
PRIVATE platform/linux/src/headless_backend_egl.cpp
PRIVATE platform/linux/src/headless_display_egl.cpp
)
target_link_libraries(mbgl-core
PUBLIC -lGLESv2
@@ -37,7 +35,6 @@ macro(mbgl_platform_core)
else()
target_sources(mbgl-core
PRIVATE platform/linux/src/headless_backend_glx.cpp
PRIVATE platform/linux/src/headless_display_glx.cpp
)
target_link_libraries(mbgl-core
PUBLIC -lGL
@@ -66,7 +63,6 @@ macro(mbgl_platform_core)
PRIVATE platform/default/mbgl/gl/headless_frontend.hpp
PRIVATE platform/default/mbgl/gl/headless_backend.cpp
PRIVATE platform/default/mbgl/gl/headless_backend.hpp
PRIVATE platform/default/mbgl/gl/headless_display.hpp
# Thread pool
PRIVATE platform/default/mbgl/util/default_thread_pool.cpp

View File

@@ -1,5 +1,4 @@
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/headless_display.hpp>
#include <mbgl/util/logging.hpp>
@@ -9,11 +8,82 @@
namespace mbgl {
// This class provides a singleton that contains information about the configuration used for
// instantiating new headless rendering contexts.
class EGLDisplayConfig {
private:
// Key for singleton construction.
struct Key { explicit Key() = default; };
public:
EGLDisplayConfig(Key) {
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (display == EGL_NO_DISPLAY) {
throw std::runtime_error("Failed to obtain a valid EGL display.\n");
}
EGLint major, minor, numConfigs;
if (!eglInitialize(display, &major, &minor)) {
throw std::runtime_error("eglInitialize() failed.\n");
}
if (!eglBindAPI(EGL_OPENGL_ES_API)) {
mbgl::Log::Error(mbgl::Event::OpenGL, "eglBindAPI(EGL_OPENGL_ES_API) returned error %d",
eglGetError());
throw std::runtime_error("eglBindAPI() failed");
}
const EGLint attribs[] = {
#if MBGL_USE_GLES2
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
#endif
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_NONE
};
// Note: we're choosing an arbitrary pixel format, since we're not using the default surface
// anyway; all rendering will be directed to framebuffers which have their own configuration.
if (!eglChooseConfig(display, attribs, &config, 1, &numConfigs) || numConfigs != 1) {
throw std::runtime_error("Failed to choose ARGB config.\n");
}
}
~EGLDisplayConfig() {
eglTerminate(display);
}
static std::shared_ptr<const EGLDisplayConfig> create() {
static std::weak_ptr<const EGLDisplayConfig> instance;
auto shared = instance.lock();
if (!shared) {
instance = shared = std::make_shared<EGLDisplayConfig>(Key{});
}
return shared;
}
public:
EGLDisplay display = EGL_NO_DISPLAY;
EGLConfig config = 0;
};
struct EGLImpl : public HeadlessBackend::Impl {
EGLImpl(EGLContext glContext_, EGLDisplay display_, EGLConfig config_)
: glContext(glContext_),
display(display_),
config(config_) {
EGLImpl() {
// EGL initializes the context client version to 1 by default. We want to
// use OpenGL ES 2.0 which has the ability to create shader and program
// objects and also to write vertex and fragment shaders in the OpenGL ES
// Shading Language.
const EGLint attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
eglContext = eglCreateContext(eglDisplay->display, eglDisplay->config, EGL_NO_CONTEXT, attribs);
if (eglContext == EGL_NO_CONTEXT) {
mbgl::Log::Error(mbgl::Event::OpenGL, "eglCreateContext() returned error 0x%04x",
eglGetError());
throw std::runtime_error("Error creating the EGL context object.\n");
}
// Create a dummy pbuffer. We will render to framebuffers anyway, but we need a pbuffer to
// activate the context.
// Note that to be able to create pbuffer surfaces, we need to choose a config that
@@ -25,77 +95,49 @@ struct EGLImpl : public HeadlessBackend::Impl {
EGL_NONE
};
glSurface = eglCreatePbufferSurface(display, config, surfAttribs);
if (glSurface == EGL_NO_SURFACE) {
eglSurface = eglCreatePbufferSurface(eglDisplay->display, eglDisplay->config, surfAttribs);
if (eglSurface == EGL_NO_SURFACE) {
throw std::runtime_error("Could not create surface: " + std::to_string(eglGetError()));
}
}
~EGLImpl() {
if (glSurface != EGL_NO_SURFACE) {
if (!eglDestroySurface(display, glSurface)) {
throw std::runtime_error("Failed to destroy EGL surface.\n");
~EGLImpl() final {
if (eglSurface != EGL_NO_SURFACE) {
if (!eglDestroySurface(eglDisplay->display, eglSurface)) {
Log::Error(Event::OpenGL, "Failed to destroy EGL surface.");
}
glSurface = EGL_NO_SURFACE;
eglSurface = EGL_NO_SURFACE;
}
if (!eglDestroyContext(display, glContext)) {
throw std::runtime_error("Failed to destroy EGL context.\n");
if (!eglDestroyContext(eglDisplay->display, eglContext)) {
Log::Error(Event::OpenGL, "Failed to destroy EGL context.");
}
}
void activateContext() final {
if (!eglMakeCurrent(display, glSurface, glSurface, glContext)) {
if (!eglMakeCurrent(eglDisplay->display, eglSurface, eglSurface, eglContext)) {
throw std::runtime_error("Switching OpenGL context failed.\n");
}
}
void deactivateContext() final {
if (!eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
if (!eglMakeCurrent(eglDisplay->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
throw std::runtime_error("Removing OpenGL context failed.\n");
}
}
EGLContext glContext = EGL_NO_CONTEXT;
EGLDisplay display = EGL_NO_DISPLAY;
EGLConfig config = 0;
EGLSurface glSurface = EGL_NO_SURFACE;
private:
const std::shared_ptr<const EGLDisplayConfig> eglDisplay = EGLDisplayConfig::create();
EGLContext eglContext = EGL_NO_CONTEXT;
EGLSurface eglSurface = EGL_NO_SURFACE;
};
gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) {
return eglGetProcAddress(name);
}
bool HeadlessBackend::hasDisplay() {
if (!display) {
display = HeadlessDisplay::create();
}
return bool(display);
};
void HeadlessBackend::createContext() {
assert(!hasContext());
assert(hasDisplay());
EGLDisplay display_ = display->attribute<EGLDisplay>();
EGLConfig& config = display->attribute<EGLConfig&>();
// EGL initializes the context client version to 1 by default. We want to
// use OpenGL ES 2.0 which has the ability to create shader and program
// objects and also to write vertex and fragment shaders in the OpenGL ES
// Shading Language.
const EGLint attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
EGLContext glContext = eglCreateContext(display_, config, EGL_NO_CONTEXT, attribs);
if (glContext == EGL_NO_CONTEXT) {
mbgl::Log::Error(mbgl::Event::OpenGL, "eglCreateContext() returned error 0x%04x",
eglGetError());
throw std::runtime_error("Error creating the EGL context object.\n");
}
impl.reset(new EGLImpl(glContext, display_, config));
impl = std::make_unique<EGLImpl>();
}
} // namespace mbgl

View File

@@ -1,5 +1,4 @@
#include <mbgl/gl/headless_backend.hpp>
#include <mbgl/gl/headless_display.hpp>
#include <mbgl/util/logging.hpp>
@@ -9,79 +8,122 @@
namespace mbgl {
struct GLXImpl : public HeadlessBackend::Impl {
GLXImpl(GLXContext glContext_, GLXPbuffer glxPbuffer_, Display* xDisplay_, GLXFBConfig* fbConfigs_)
: glContext(glContext_),
glxPbuffer(glxPbuffer_),
xDisplay(xDisplay_),
fbConfigs(fbConfigs_) {
// This class provides a singleton that contains information about the configuration used for
// instantiating new headless rendering contexts.
class GLXDisplayConfig {
private:
// Key for singleton construction.
struct Key { explicit Key() = default; };
public:
GLXDisplayConfig(Key) {
if (!XInitThreads()) {
throw std::runtime_error("Failed to XInitThreads.");
}
xDisplay = XOpenDisplay(nullptr);
if (xDisplay == nullptr) {
throw std::runtime_error("Failed to open X display.");
}
const auto* extensions = reinterpret_cast<const char*>(
glXQueryServerString(xDisplay, DefaultScreen(xDisplay), GLX_EXTENSIONS));
if (!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.");
}
if (!strstr(extensions, "GLX_SGIX_pbuffer")) {
throw std::runtime_error("Cannot find glXCreateContextAttribsARB.");
}
// We're creating a dummy pbuffer anyway that we're not using.
static int pixelFormat[] = { GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT, None };
int configs = 0;
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.");
}
}
~GLXImpl() override {
if (glxPbuffer) {
glXDestroyPbuffer(xDisplay, glxPbuffer);
~GLXDisplayConfig() {
XFree(fbConfigs);
XCloseDisplay(xDisplay);
}
static std::shared_ptr<const GLXDisplayConfig> create() {
static std::weak_ptr<const GLXDisplayConfig> instance;
auto shared = instance.lock();
if (!shared) {
instance = shared = std::make_shared<GLXDisplayConfig>(Key{});
}
glXDestroyContext(xDisplay, glContext);
return shared;
}
public:
Display* xDisplay = nullptr;
GLXFBConfig* fbConfigs = nullptr;
};
struct GLXImpl : public HeadlessBackend::Impl {
GLXImpl() {
// Try to create a legacy context.
glContext = glXCreateNewContext(glxDisplay->xDisplay, glxDisplay->fbConfigs[0],
GLX_RGBA_TYPE, None, True);
if (glContext && !glXIsDirect(glxDisplay->xDisplay, glContext)) {
Log::Error(Event::OpenGL, "failed to create direct OpenGL Legacy context");
glXDestroyContext(glxDisplay->xDisplay, glContext);
glContext = nullptr;
}
if (glContext == nullptr) {
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 pbufferAttributes[] = { GLX_PBUFFER_WIDTH, 8, GLX_PBUFFER_HEIGHT, 8, None };
glxPbuffer =
glXCreatePbuffer(glxDisplay->xDisplay, glxDisplay->fbConfigs[0], pbufferAttributes);
}
~GLXImpl() final {
if (glxPbuffer) {
glXDestroyPbuffer(glxDisplay->xDisplay, glxPbuffer);
}
glXDestroyContext(glxDisplay->xDisplay, glContext);
}
void activateContext() final {
if (!glXMakeContextCurrent(xDisplay, glxPbuffer, glxPbuffer, glContext)) {
if (!glXMakeContextCurrent(glxDisplay->xDisplay, glxPbuffer, glxPbuffer, glContext)) {
throw std::runtime_error("Switching OpenGL context failed.\n");
}
}
void deactivateContext() final {
if (!glXMakeContextCurrent(xDisplay, 0, 0, nullptr)) {
if (!glXMakeContextCurrent(glxDisplay->xDisplay, 0, 0, nullptr)) {
throw std::runtime_error("Removing OpenGL context failed.\n");
}
}
private:
const std::shared_ptr<const GLXDisplayConfig> glxDisplay = GLXDisplayConfig::create();
GLXContext glContext = nullptr;
GLXPbuffer glxPbuffer = 0;
// Needed for ImplDeleter.
Display* xDisplay = nullptr;
GLXFBConfig* fbConfigs = nullptr;
};
gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) {
return glXGetProcAddress(reinterpret_cast<const GLubyte*>(name));
}
bool HeadlessBackend::hasDisplay() {
if (!display) {
display = HeadlessDisplay::create();
}
return bool(display);
};
void HeadlessBackend::createContext() {
assert(!hasContext());
auto* xDisplay = display->attribute<Display*>();
auto* fbConfigs = display->attribute<GLXFBConfig*>();
// Try to create a legacy context.
GLXContext glContext = glXCreateNewContext(xDisplay, fbConfigs[0], GLX_RGBA_TYPE, None, True);
if (glContext && !glXIsDirect(xDisplay, glContext)) {
Log::Error(Event::OpenGL, "failed to create direct OpenGL Legacy context");
glXDestroyContext(xDisplay, glContext);
glContext = nullptr;
}
if (glContext == nullptr) {
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 pbufferAttributes[] = {
GLX_PBUFFER_WIDTH, 8,
GLX_PBUFFER_HEIGHT, 8,
None
};
GLXPbuffer glxPbuffer = glXCreatePbuffer(xDisplay, fbConfigs[0], pbufferAttributes);
impl = std::make_unique<mbgl::GLXImpl>(glContext, glxPbuffer, xDisplay, fbConfigs);
impl = std::make_unique<GLXImpl>();
}
} // namespace mbgl

View File

@@ -1,68 +0,0 @@
#include <mbgl/gl/headless_display.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/util/string.hpp>
#include <EGL/egl.h>
namespace mbgl {
class HeadlessDisplay::Impl {
public:
Impl();
~Impl();
EGLDisplay display = EGL_NO_DISPLAY;
EGLConfig config = 0;
};
HeadlessDisplay::Impl::Impl() {
display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (display == EGL_NO_DISPLAY) {
throw std::runtime_error("Failed to obtain a valid EGL display.\n");
}
EGLint major, minor, numConfigs;
if (!eglInitialize(display, &major, &minor)) {
throw std::runtime_error("eglInitialize() failed.\n");
}
if (!eglBindAPI(EGL_OPENGL_ES_API)) {
mbgl::Log::Error(mbgl::Event::OpenGL, "eglBindAPI(EGL_OPENGL_ES_API) returned error %d", eglGetError());
throw std::runtime_error("eglBindAPI() failed");
}
const EGLint attribs[] = {
#if MBGL_USE_GLES2
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
#endif
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_NONE
};
if (!eglChooseConfig(display, attribs, &config, 1, &numConfigs) || numConfigs != 1) {
throw std::runtime_error("Failed to choose ARGB config.\n");
}
}
HeadlessDisplay::Impl::~Impl() {
eglTerminate(display);
}
template <>
EGLDisplay HeadlessDisplay::attribute() const {
return impl->display;
}
template <>
EGLConfig& HeadlessDisplay::attribute() const {
return impl->config;
}
HeadlessDisplay::HeadlessDisplay()
: impl(std::make_unique<Impl>()) {
}
HeadlessDisplay::~HeadlessDisplay() {
}
} // namespace mbgl

View File

@@ -1,78 +0,0 @@
#include <mbgl/gl/headless_display.hpp>
#include <GL/glx.h>
#include <cstring>
#include <stdexcept>
#include <string>
namespace mbgl {
class HeadlessDisplay::Impl {
public:
Impl();
~Impl();
Display* xDisplay = nullptr;
GLXFBConfig* fbConfigs = nullptr;
};
HeadlessDisplay::Impl::Impl() {
if (!XInitThreads()) {
throw std::runtime_error("Failed to XInitThreads.");
}
xDisplay = XOpenDisplay(nullptr);
if (xDisplay == nullptr) {
throw std::runtime_error("Failed to open X display.");
}
const auto *extensions = reinterpret_cast<const char *>(glXQueryServerString(xDisplay, DefaultScreen(xDisplay), GLX_EXTENSIONS));
if (!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.");
}
if (!strstr(extensions, "GLX_SGIX_pbuffer")) {
throw std::runtime_error("Cannot find glXCreateContextAttribsARB.");
}
// We're creating a dummy pbuffer anyway that we're not using.
static int pixelFormat[] = {
GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
None
};
int configs = 0;
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.");
}
}
HeadlessDisplay::Impl::~Impl() {
XFree(fbConfigs);
XCloseDisplay(xDisplay);
}
template <>
Display* HeadlessDisplay::attribute() const {
return impl->xDisplay;
}
template <>
GLXFBConfig* HeadlessDisplay::attribute() const {
return impl->fbConfigs;
}
HeadlessDisplay::HeadlessDisplay()
: impl(std::make_unique<Impl>()) {
}
HeadlessDisplay::~HeadlessDisplay() = default;
} // namespace mbgl

View File

@@ -32,8 +32,6 @@ macro(mbgl_platform_core)
PRIVATE platform/default/mbgl/gl/headless_backend.cpp
PRIVATE platform/default/mbgl/gl/headless_backend.hpp
PRIVATE platform/darwin/src/headless_backend_cgl.cpp
PRIVATE platform/default/mbgl/gl/headless_display.hpp
PRIVATE platform/darwin/src/headless_display_cgl.cpp
# Snapshotting
PRIVATE platform/default/mbgl/map/map_snapshotter.cpp

View File

@@ -17,8 +17,6 @@ set(MBGL_QT_CORE_FILES
PRIVATE platform/default/mbgl/gl/headless_frontend.hpp
PRIVATE platform/default/mbgl/gl/headless_backend.cpp
PRIVATE platform/default/mbgl/gl/headless_backend.hpp
PRIVATE platform/default/mbgl/gl/headless_display.cpp
PRIVATE platform/default/mbgl/gl/headless_display.hpp
PRIVATE platform/qt/src/headless_backend_qt.cpp
# Thread pool

View File

@@ -12,7 +12,10 @@
namespace mbgl {
struct QtImpl : public HeadlessBackend::Impl {
class QtImpl : public HeadlessBackend::Impl {
public:
~QtImpl() final = default;
void activateContext() final {
widget.makeCurrent();
}
@@ -21,6 +24,7 @@ struct QtImpl : public HeadlessBackend::Impl {
widget.doneCurrent();
}
private:
QGLWidget widget;
};
@@ -34,13 +38,9 @@ gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) {
#endif
}
bool HeadlessBackend::hasDisplay() {
return true;
};
void HeadlessBackend::createContext() {
assert(!hasContext());
impl.reset(new QtImpl);
impl = std::make_unique<QtImpl>();
}
} // namespace mbgl