diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2017-11-27 16:34:52 +0100 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2017-11-29 15:48:51 +0100 |
commit | 335f04f7e13422ce53cbbf13cebb8283149faba8 (patch) | |
tree | 4542c71910cea2f626ac37325a32f9432f1d908b /platform/linux | |
parent | 772b9090626731101b82eccacbc3adaa71cc428c (diff) | |
download | qtlocation-mapboxgl-335f04f7e13422ce53cbbf13cebb8283149faba8.tar.gz |
[core] fold HeadlessDisplay into the headless RenderBackend implementation
Diffstat (limited to 'platform/linux')
-rw-r--r-- | platform/linux/config.cmake | 4 | ||||
-rw-r--r-- | platform/linux/src/headless_backend_egl.cpp | 140 | ||||
-rw-r--r-- | platform/linux/src/headless_backend_glx.cpp | 136 | ||||
-rw-r--r-- | platform/linux/src/headless_display_egl.cpp | 68 | ||||
-rw-r--r-- | platform/linux/src/headless_display_glx.cpp | 78 |
5 files changed, 180 insertions, 246 deletions
diff --git a/platform/linux/config.cmake b/platform/linux/config.cmake index edac8e59ac..c64e20eddf 100644 --- a/platform/linux/config.cmake +++ b/platform/linux/config.cmake @@ -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 diff --git a/platform/linux/src/headless_backend_egl.cpp b/platform/linux/src/headless_backend_egl.cpp index 0784173af7..7d3a3d1ec1 100644 --- a/platform/linux/src/headless_backend_egl.cpp +++ b/platform/linux/src/headless_backend_egl.cpp @@ -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 diff --git a/platform/linux/src/headless_backend_glx.cpp b/platform/linux/src/headless_backend_glx.cpp index 0ba7f08630..6d2939da0f 100644 --- a/platform/linux/src/headless_backend_glx.cpp +++ b/platform/linux/src/headless_backend_glx.cpp @@ -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 { +// 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."); + } + } + + ~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{}); + } + return shared; + } + +public: + Display* xDisplay = nullptr; + GLXFBConfig* fbConfigs = nullptr; +}; + struct GLXImpl : public HeadlessBackend::Impl { - GLXImpl(GLXContext glContext_, GLXPbuffer glxPbuffer_, Display* xDisplay_, GLXFBConfig* fbConfigs_) - : glContext(glContext_), - glxPbuffer(glxPbuffer_), - xDisplay(xDisplay_), - fbConfigs(fbConfigs_) { + 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() override { + ~GLXImpl() final { if (glxPbuffer) { - glXDestroyPbuffer(xDisplay, glxPbuffer); + glXDestroyPbuffer(glxDisplay->xDisplay, glxPbuffer); } - glXDestroyContext(xDisplay, glContext); + 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 diff --git a/platform/linux/src/headless_display_egl.cpp b/platform/linux/src/headless_display_egl.cpp deleted file mode 100644 index b746211924..0000000000 --- a/platform/linux/src/headless_display_egl.cpp +++ /dev/null @@ -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 diff --git a/platform/linux/src/headless_display_glx.cpp b/platform/linux/src/headless_display_glx.cpp deleted file mode 100644 index 5dc342154d..0000000000 --- a/platform/linux/src/headless_display_glx.cpp +++ /dev/null @@ -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 |