From e8389d82cdd84d470deb072d82ee9a613cd15df8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Fri, 13 Feb 2015 16:11:42 -0800 Subject: guard against concurrent OpenGL extension loading --- android/cpp/native_map_view.cpp | 17 ++++++++--------- include/mbgl/android/native_map_view.hpp | 4 ---- include/mbgl/platform/default/headless_view.hpp | 2 ++ platform/default/glfw_view.cpp | 8 +++++--- platform/default/headless_view.cpp | 18 +++++++++++++++--- platform/ios/MGLMapView.mm | 8 +++++--- 6 files changed, 35 insertions(+), 22 deletions(-) diff --git a/android/cpp/native_map_view.cpp b/android/cpp/native_map_view.cpp index 599f245989..35bb6b88a9 100644 --- a/android/cpp/native_map_view.cpp +++ b/android/cpp/native_map_view.cpp @@ -16,6 +16,9 @@ #include #include + +pthread_once_t loadGLExtensions = PTHREAD_ONCE_INIT; + namespace mbgl { namespace android { @@ -296,6 +299,8 @@ void NativeMapView::terminateContext() { context = EGL_NO_CONTEXT; } +void loadExtensions(); + void NativeMapView::createSurface(ANativeWindow *window_) { mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::createSurface"); @@ -337,7 +342,7 @@ void NativeMapView::createSurface(ANativeWindow *window_) { } log_gl_string(GL_EXTENSIONS, "Extensions"); - loadExtensions(); + pthread_once(&loadGLExtensions, loadExtensions); if (!eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) { mbgl::Log::Error(mbgl::Event::OpenGL, @@ -553,7 +558,6 @@ EGLConfig NativeMapView::chooseConfig(const EGLConfig configs[], EGLint numConfi // Sort the configs to find the best one configList.sort(); - usingDepth24 = std::get<1>(configList.front()) == Format24Depth8Stencil; bool isConformant = !std::get<2>(configList.front()); bool isCaveat = std::get<3>(configList.front()); int configNum = std::get<4>(configList.front()); @@ -588,7 +592,7 @@ void NativeMapView::start() { map.start(true); } -void NativeMapView::loadExtensions() { +void loadExtensions() { const GLubyte *str = glGetString(GL_EXTENSIONS); if (str == nullptr) { mbgl::Log::Error(mbgl::Event::OpenGL, "glGetString(GL_EXTENSIONS) returned error %d", @@ -620,12 +624,7 @@ void NativeMapView::loadExtensions() { } if (extensions.find("GL_OES_depth24") != std::string::npos) { - mbgl::Log::Info(mbgl::Event::OpenGL, "Using GL_OES_depth24."); - if (usingDepth24) { - gl::isDepth24Supported = true; - } else { - mbgl::Log::Info(mbgl::Event::OpenGL, "Preferring 16 bit depth."); - } + gl::isDepth24Supported = true; } if (extensions.find("GL_KHR_debug") != std::string::npos) { diff --git a/include/mbgl/android/native_map_view.hpp b/include/mbgl/android/native_map_view.hpp index eb5f295eaf..d02d43e58a 100644 --- a/include/mbgl/android/native_map_view.hpp +++ b/include/mbgl/android/native_map_view.hpp @@ -54,8 +54,6 @@ public: private: EGLConfig chooseConfig(const EGLConfig configs[], EGLint numConfigs); - void loadExtensions(); - bool inEmulator(); private: @@ -80,8 +78,6 @@ private: bool firstTime = false; - bool usingDepth24 = false; - bool fpsEnabled = false; double fps = 0.0; }; diff --git a/include/mbgl/platform/default/headless_view.hpp b/include/mbgl/platform/default/headless_view.hpp index ba318c2b41..5d0e55d69a 100644 --- a/include/mbgl/platform/default/headless_view.hpp +++ b/include/mbgl/platform/default/headless_view.hpp @@ -60,6 +60,8 @@ private: GLXPbuffer glxPbuffer = 0; #endif + bool extensionsLoaded = false; + GLuint fbo = 0; GLuint fboDepthStencil = 0; GLuint fboColor = 0; diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 865636aebf..6d23a51ffb 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -2,6 +2,8 @@ #include #include +pthread_once_t loadGLExtensions = PTHREAD_ONCE_INIT; + GLFWView::GLFWView(bool fullscreen_) : fullscreen(fullscreen_) { #ifdef NVIDIA glDiscardFramebufferEXT = reinterpret_cast(glfwGetProcAddress("glDiscardFramebufferEXT")); @@ -71,8 +73,8 @@ void GLFWView::initialize(mbgl::Map *map_) { glfwSetScrollCallback(window, onScroll); glfwSetKeyCallback(window, onKey); - const std::string extensions = reinterpret_cast(MBGL_CHECK_ERROR(glGetString(GL_EXTENSIONS))); - { + pthread_once(&loadGLExtensions, [] { + const std::string extensions = reinterpret_cast(MBGL_CHECK_ERROR(glGetString(GL_EXTENSIONS))); using namespace mbgl; if (extensions.find("GL_KHR_debug") != std::string::npos) { @@ -152,7 +154,7 @@ void GLFWView::initialize(mbgl::Map *map_) { // Require packed depth stencil gl::isPackedDepthStencilSupported = true; gl::isDepth24Supported = true; - } + }); glfwMakeContextCurrent(nullptr); } diff --git a/platform/default/headless_view.cpp b/platform/default/headless_view.cpp index a3128234ea..5f41994c27 100644 --- a/platform/default/headless_view.cpp +++ b/platform/default/headless_view.cpp @@ -10,6 +10,8 @@ #include #include +pthread_once_t loadGLExtensions = PTHREAD_ONCE_INIT; + #ifdef MBGL_USE_CGL #include @@ -48,10 +50,18 @@ HeadlessView::HeadlessView(std::shared_ptr display) } void HeadlessView::loadExtensions() { + if (extensionsLoaded) { + return; + } + activate(); - const char *extensionPtr = reinterpret_cast(MBGL_CHECK_ERROR(glGetString(GL_EXTENSIONS))); - if (extensionPtr) { + pthread_once(&loadGLExtensions, [] { + const char *extensionPtr = reinterpret_cast(MBGL_CHECK_ERROR(glGetString(GL_EXTENSIONS))); + + if (!extensionPtr) { + return; + } const std::string extensions = extensionPtr; #ifdef MBGL_USE_CGL @@ -78,12 +88,14 @@ void HeadlessView::loadExtensions() { assert(gl::IsVertexArray != nullptr); } #endif - } + }); // HeadlessView requires packed depth stencil gl::isPackedDepthStencilSupported = true; gl::isDepth24Supported = true; + extensionsLoaded = true; + deactivate(); } diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index b1932737b7..45c96e7c3b 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -37,6 +37,7 @@ const std::string &defaultCacheDatabase() { return path; } +static dispatch_once_t loadGLExtensions; extern NSString *const MGLStyleKeyGeneric; extern NSString *const MGLStyleKeyFill; @@ -213,8 +214,9 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr; // load extensions // - const std::string extensions = (char *)glGetString(GL_EXTENSIONS); - { + dispatch_once(&loadGLExtensions, ^{ + const std::string extensions = (char *)glGetString(GL_EXTENSIONS); + using namespace mbgl; if (extensions.find("GL_OES_vertex_array_object") != std::string::npos) { @@ -231,7 +233,7 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr; if (extensions.find("GL_OES_depth24") != std::string::npos) { gl::isDepth24Supported = YES; } - } + }); // setup mbgl map // -- cgit v1.2.1