diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-11-06 20:39:13 +0200 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2016-11-08 18:02:38 +0200 |
commit | d0ac12309b470eb59690ece3f15773df73a127e1 (patch) | |
tree | cfb7939f6d3ac7f1656966142b8b6f548ab6389f /platform/linux | |
parent | 297c303e88416274d3d2c14b5f6abf1c850c3cf8 (diff) | |
download | qtlocation-mapboxgl-d0ac12309b470eb59690ece3f15773df73a127e1.tar.gz |
[core] Refactor HeadlessBackend
Diffstat (limited to 'platform/linux')
-rw-r--r-- | platform/linux/src/headless_backend_glx.cpp | 92 |
1 files changed, 56 insertions, 36 deletions
diff --git a/platform/linux/src/headless_backend_glx.cpp b/platform/linux/src/headless_backend_glx.cpp index 929eb73f5a..e1ea9cef15 100644 --- a/platform/linux/src/headless_backend_glx.cpp +++ b/platform/linux/src/headless_backend_glx.cpp @@ -3,32 +3,71 @@ #include <mbgl/platform/log.hpp> -// #include <cassert> +#include <cassert> #include <GL/glx.h> namespace mbgl { +struct GLXImpl : public HeadlessBackend::Impl { + GLXImpl(GLXContext glContext_, GLXPbuffer glxPbuffer_, Display* xDisplay_, GLXFBConfig* fbConfigs_) + : glContext(glContext_), + glxPbuffer(glxPbuffer_), + xDisplay(xDisplay_), + fbConfigs(fbConfigs_) { + } + + ~GLXImpl() { + if (glxPbuffer) { + glXDestroyPbuffer(xDisplay, glxPbuffer); + } + glXDestroyContext(xDisplay, glContext); + } + + void activateContext() final { + if (!glXMakeContextCurrent(xDisplay, glxPbuffer, glxPbuffer, glContext)) { + throw std::runtime_error("Switching OpenGL context failed.\n"); + } + } + + void deactivateContext() final { + if (!glXMakeContextCurrent(xDisplay, 0, 0, nullptr)) { + throw std::runtime_error("Removing OpenGL context failed.\n"); + } + } + + GLXContext glContext = nullptr; + GLXPbuffer glxPbuffer = 0; + + // Needed for ImplDeleter. + Display* xDisplay = nullptr; + GLXFBConfig* fbConfigs = nullptr; +}; + gl::glProc HeadlessBackend::initializeExtension(const char* name) { return glXGetProcAddress(reinterpret_cast<const GLubyte*>(name)); } -void HeadlessBackend::createContext() { - xDisplay = display->attribute<Display*>(); - fbConfigs = display->attribute<GLXFBConfig*>(); - - if (!glContext) { - // Try to create a legacy context - glContext = glXCreateNewContext(xDisplay, fbConfigs[0], GLX_RGBA_TYPE, None, True); - if (glContext) { - if (!glXIsDirect(xDisplay, glContext)) { - Log::Error(Event::OpenGL, "failed to create direct OpenGL Legacy context"); - glXDestroyContext(xDisplay, glContext); - glContext = nullptr; - } - } +bool HeadlessBackend::hasDisplay() { + if (!display) { + display.reset(new HeadlessDisplay); } + return bool(display); +}; + +void HeadlessBackend::createContext() { + assert(!hasContext()); + + Display* xDisplay = display->attribute<Display*>(); + GLXFBConfig* 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."); } @@ -40,28 +79,9 @@ void HeadlessBackend::createContext() { GLX_PBUFFER_HEIGHT, 8, None }; - glxPbuffer = glXCreatePbuffer(xDisplay, fbConfigs[0], pbufferAttributes); -} - -void HeadlessBackend::destroyContext() { - if (glxPbuffer) { - glXDestroyPbuffer(xDisplay, glxPbuffer); - glxPbuffer = 0; - } - - glXDestroyContext(xDisplay, glContext); -} + GLXPbuffer glxPbuffer = glXCreatePbuffer(xDisplay, fbConfigs[0], pbufferAttributes); -void HeadlessBackend::activateContext() { - if (!glXMakeContextCurrent(xDisplay, glxPbuffer, glxPbuffer, glContext)) { - throw std::runtime_error("Switching OpenGL context failed.\n"); - } -} - -void HeadlessBackend::deactivateContext() { - if (!glXMakeContextCurrent(xDisplay, 0, 0, nullptr)) { - throw std::runtime_error("Removing OpenGL context failed.\n"); - } + impl.reset(new GLXImpl(glContext, glxPbuffer, xDisplay, fbConfigs)); } } // namespace mbgl |