From 234384ece9c70f2a803ed2b1d1eb55b248ec43d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Mon, 27 Feb 2017 18:33:16 +0100 Subject: [core] Move OpenGL extension initialization to Backend --- platform/android/src/native_map_view.cpp | 9 ++++---- platform/android/src/native_map_view.hpp | 1 + platform/darwin/src/headless_backend_cgl.cpp | 4 ++-- platform/darwin/src/headless_backend_eagl.mm | 6 ++---- platform/default/headless_backend_osmesa.cpp | 2 +- platform/default/mbgl/gl/headless_backend.cpp | 5 ----- platform/default/mbgl/gl/headless_backend.hpp | 5 +---- platform/glfw/glfw_view.cpp | 7 ++++--- platform/glfw/glfw_view.hpp | 6 +++++- platform/ios/src/MGLMapView.mm | 30 +++++++++++++-------------- platform/linux/src/headless_backend_egl.cpp | 2 +- platform/linux/src/headless_backend_glx.cpp | 2 +- platform/macos/src/MGLMapView.mm | 27 ++++++++++++------------ platform/qt/app/mapwindow.cpp | 2 -- platform/qt/include/qmapbox.hpp | 2 -- platform/qt/src/qmapbox.cpp | 23 -------------------- platform/qt/src/qmapboxgl.cpp | 14 +++++++++++++ platform/qt/src/qmapboxgl_p.hpp | 3 +++ platform/qt/test/headless_backend_qt.cpp | 4 ++-- platform/qt/test/qmapboxgl.cpp | 2 -- 20 files changed, 68 insertions(+), 88 deletions(-) (limited to 'platform') diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index c65c241458..eeb4620c2e 100755 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -15,7 +15,6 @@ #include #include -#include #include #include #include @@ -108,6 +107,10 @@ void NativeMapView::bind() { /** * From mbgl::Backend. */ +gl::ProcAddress NativeMapView::initializeExtension(const char* name) { + return eglGetProcAddress(name); +} + void NativeMapView::activate() { if (active++) { return; @@ -1394,10 +1397,6 @@ void NativeMapView::_createSurface(ANativeWindow *window_) { eglGetError()); throw std::runtime_error("eglMakeCurrent() failed"); } - - mbgl::gl::InitializeExtensions([] (const char * name) { - return reinterpret_cast(eglGetProcAddress(name)); - }); } } diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp index 9a490ddbae..af8c7dd660 100755 --- a/platform/android/src/native_map_view.hpp +++ b/platform/android/src/native_map_view.hpp @@ -262,6 +262,7 @@ public: protected: // mbgl::Backend // + gl::ProcAddress initializeExtension(const char*) override; void activate() override; void deactivate() override; diff --git a/platform/darwin/src/headless_backend_cgl.cpp b/platform/darwin/src/headless_backend_cgl.cpp index 7069738fb1..6ad98f4326 100644 --- a/platform/darwin/src/headless_backend_cgl.cpp +++ b/platform/darwin/src/headless_backend_cgl.cpp @@ -36,7 +36,7 @@ struct CGLImpl : public HeadlessBackend::Impl { CGLContextObj glContext = nullptr; }; -gl::glProc HeadlessBackend::initializeExtension(const char* name) { +gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) { static CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl")); if (!framework) { throw std::runtime_error("Failed to load OpenGL framework."); @@ -46,7 +46,7 @@ gl::glProc HeadlessBackend::initializeExtension(const char* name) { void* symbol = CFBundleGetFunctionPointerForName(framework, str); CFRelease(str); - return reinterpret_cast(symbol); + return reinterpret_cast(symbol); } bool HeadlessBackend::hasDisplay() { diff --git a/platform/darwin/src/headless_backend_eagl.mm b/platform/darwin/src/headless_backend_eagl.mm index bd4a202ec5..1daaeaf54c 100644 --- a/platform/darwin/src/headless_backend_eagl.mm +++ b/platform/darwin/src/headless_backend_eagl.mm @@ -1,7 +1,5 @@ #include -#include - #include #include @@ -29,7 +27,7 @@ struct EAGLImpl : public HeadlessBackend::Impl { EAGLContext* glContext = nullptr; }; -gl::glProc HeadlessBackend::initializeExtension(const char* name) { +gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) { static CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengles")); if (!framework) { throw std::runtime_error("Failed to load OpenGL framework."); @@ -39,7 +37,7 @@ gl::glProc HeadlessBackend::initializeExtension(const char* name) { void* symbol = CFBundleGetFunctionPointerForName(framework, str); CFRelease(str); - return reinterpret_cast(symbol); + return reinterpret_cast(symbol); } bool HeadlessBackend::hasDisplay() { diff --git a/platform/default/headless_backend_osmesa.cpp b/platform/default/headless_backend_osmesa.cpp index 8ec6079bd0..5042f5ed10 100644 --- a/platform/default/headless_backend_osmesa.cpp +++ b/platform/default/headless_backend_osmesa.cpp @@ -25,7 +25,7 @@ struct OSMesaImpl : public HeadlessBackend::Impl { GLubyte fakeBuffer = 0; }; -gl::glProc HeadlessBackend::initializeExtension(const char* name) { +gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) { return OSMesaGetProcAddress(name); } diff --git a/platform/default/mbgl/gl/headless_backend.cpp b/platform/default/mbgl/gl/headless_backend.cpp index 6ae19356fb..798c8fe680 100644 --- a/platform/default/mbgl/gl/headless_backend.cpp +++ b/platform/default/mbgl/gl/headless_backend.cpp @@ -33,11 +33,6 @@ void HeadlessBackend::activate() { assert(hasContext()); impl->activateContext(); - - if (!extensionsLoaded) { - gl::InitializeExtensions(initializeExtension); - extensionsLoaded = true; - } } void HeadlessBackend::deactivate() { diff --git a/platform/default/mbgl/gl/headless_backend.hpp b/platform/default/mbgl/gl/headless_backend.hpp index 63e3cec9ee..7e1749a253 100644 --- a/platform/default/mbgl/gl/headless_backend.hpp +++ b/platform/default/mbgl/gl/headless_backend.hpp @@ -1,7 +1,5 @@ #pragma once -#include - #include #include @@ -27,7 +25,7 @@ public: private: // Implementation specific functions - static gl::glProc initializeExtension(const char*); + gl::ProcAddress initializeExtension(const char*) override; void activate() override; void deactivate() override; @@ -40,7 +38,6 @@ private: std::unique_ptr impl; std::shared_ptr display; - bool extensionsLoaded = false; bool active = false; }; diff --git a/platform/glfw/glfw_view.cpp b/platform/glfw/glfw_view.cpp index d73b147deb..812b0e32af 100644 --- a/platform/glfw/glfw_view.cpp +++ b/platform/glfw/glfw_view.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -86,8 +85,6 @@ GLFWView::GLFWView(bool fullscreen_, bool benchmark_) glfwSetScrollCallback(window, onScroll); glfwSetKeyCallback(window, onKey); - mbgl::gl::InitializeExtensions(glfwGetProcAddress); - glfwGetWindowSize(window, &width, &height); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); pixelRatio = static_cast(fbWidth) / width; @@ -499,6 +496,10 @@ mbgl::Size GLFWView::getFramebufferSize() const { return { static_cast(fbWidth), static_cast(fbHeight) }; } +mbgl::gl::ProcAddress GLFWView::initializeExtension(const char* name) { + return glfwGetProcAddress(name); +} + void GLFWView::activate() { glfwMakeContextCurrent(window); } diff --git a/platform/glfw/glfw_view.hpp b/platform/glfw/glfw_view.hpp index bfabf6cc68..55f795cd34 100644 --- a/platform/glfw/glfw_view.hpp +++ b/platform/glfw/glfw_view.hpp @@ -44,9 +44,13 @@ public: mbgl::Size getFramebufferSize() const; // mbgl::Backend implementation + void invalidate() override; + +protected: + // mbgl::Backend implementation + mbgl::gl::ProcAddress initializeExtension(const char*) override; void activate() override; void deactivate() override; - void invalidate() override; private: // Window callbacks diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 6bd3092445..e8bc1b4b2a 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -1,7 +1,6 @@ #import "MGLMapView_Private.h" #include -#include #include #import @@ -610,21 +609,6 @@ public: [_glView bindDrawable]; [self insertSubview:_glView atIndex:0]; _glView.contentMode = UIViewContentModeCenter; - - // load extensions - // - mbgl::gl::InitializeExtensions([](const char * name) { - static CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengles")); - if (!framework) { - throw std::runtime_error("Failed to load OpenGL framework."); - } - - CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, name, kCFStringEncodingASCII); - void* symbol = CFBundleGetFunctionPointerForName(framework, str); - CFRelease(str); - - return reinterpret_cast(symbol); - }); } - (UIImage *)compassImage @@ -5436,6 +5420,20 @@ public: [nativeView didFinishLoadingStyle]; } + mbgl::gl::ProcAddress initializeExtension(const char* name) override { + static CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengles")); + if (!framework) { + throw std::runtime_error("Failed to load OpenGL framework."); + } + + CFStringRef str = + CFStringCreateWithCString(kCFAllocatorDefault, name, kCFStringEncodingASCII); + void* symbol = CFBundleGetFunctionPointerForName(framework, str); + CFRelease(str); + + return reinterpret_cast(symbol); + } + void invalidate() override { [nativeView setNeedsGLDisplay]; diff --git a/platform/linux/src/headless_backend_egl.cpp b/platform/linux/src/headless_backend_egl.cpp index df0ecc8dff..696fa3065c 100644 --- a/platform/linux/src/headless_backend_egl.cpp +++ b/platform/linux/src/headless_backend_egl.cpp @@ -60,7 +60,7 @@ struct EGLImpl : public HeadlessBackend::Impl { EGLSurface glSurface = EGL_NO_SURFACE; }; -gl::glProc HeadlessBackend::initializeExtension(const char* name) { +gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) { return eglGetProcAddress(name); } diff --git a/platform/linux/src/headless_backend_glx.cpp b/platform/linux/src/headless_backend_glx.cpp index 5791f1892f..36a60ec06b 100644 --- a/platform/linux/src/headless_backend_glx.cpp +++ b/platform/linux/src/headless_backend_glx.cpp @@ -44,7 +44,7 @@ struct GLXImpl : public HeadlessBackend::Impl { GLXFBConfig* fbConfigs = nullptr; }; -gl::glProc HeadlessBackend::initializeExtension(const char* name) { +gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) { return glXGetProcAddress(reinterpret_cast(name)); } diff --git a/platform/macos/src/MGLMapView.mm b/platform/macos/src/MGLMapView.mm index ef1c0f3bc1..45ac7c5357 100644 --- a/platform/macos/src/MGLMapView.mm +++ b/platform/macos/src/MGLMapView.mm @@ -26,7 +26,6 @@ #import #import #import -#import #import #import #import @@ -768,19 +767,6 @@ public: - (void)renderSync { if (!self.dormant) { - // Enable vertex buffer objects. - mbgl::gl::InitializeExtensions([](const char *name) { - static CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl")); - if (!framework) { - throw std::runtime_error("Failed to load OpenGL framework."); - } - - CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, name, kCFStringEncodingASCII); - void *symbol = CFBundleGetFunctionPointerForName(framework, str); - CFRelease(str); - - return reinterpret_cast(symbol); - }); // The OpenGL implementation automatically enables the OpenGL context for us. mbgl::BackendScope scope { *_mbglView, mbgl::BackendScope::ScopeType::Implicit }; @@ -2846,6 +2832,19 @@ public: [nativeView sourceDidChange]; } + mbgl::gl::ProcAddress initializeExtension(const char* name) override { + static CFBundleRef framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl")); + if (!framework) { + throw std::runtime_error("Failed to load OpenGL framework."); + } + + CFStringRef str = CFStringCreateWithCString(kCFAllocatorDefault, name, kCFStringEncodingASCII); + void *symbol = CFBundleGetFunctionPointerForName(framework, str); + CFRelease(str); + + return reinterpret_cast(symbol); + } + void invalidate() override { [nativeView setNeedsGLDisplay]; } diff --git a/platform/qt/app/mapwindow.cpp b/platform/qt/app/mapwindow.cpp index 926d504904..345c5196ba 100644 --- a/platform/qt/app/mapwindow.cpp +++ b/platform/qt/app/mapwindow.cpp @@ -372,8 +372,6 @@ void MapWindow::wheelEvent(QWheelEvent *ev) void MapWindow::initializeGL() { - QMapbox::initializeGLExtensions(); - m_map.reset(new QMapboxGL(nullptr, m_settings, size(), pixelRatio())); connect(m_map.data(), SIGNAL(needsRendering()), this, SLOT(update())); diff --git a/platform/qt/include/qmapbox.hpp b/platform/qt/include/qmapbox.hpp index f2d96412a9..e69b37108d 100644 --- a/platform/qt/include/qmapbox.hpp +++ b/platform/qt/include/qmapbox.hpp @@ -97,8 +97,6 @@ typedef void (*CustomLayerInitializeFunction)(void* context) ; typedef void (*CustomLayerRenderFunction)(void* context, const CustomLayerRenderParameters&); typedef void (*CustomLayerDeinitializeFunction)(void* context); -Q_DECL_EXPORT void initializeGLExtensions(); - } // namespace QMapbox Q_DECLARE_METATYPE(QMapbox::Coordinate); diff --git a/platform/qt/src/qmapbox.cpp b/platform/qt/src/qmapbox.cpp index 126ece1efa..410e114690 100644 --- a/platform/qt/src/qmapbox.cpp +++ b/platform/qt/src/qmapbox.cpp @@ -1,6 +1,5 @@ #include "qmapbox.hpp" -#include #include #include #include @@ -255,26 +254,4 @@ Q_DECL_EXPORT QList >& defaultStyles() return styles; } -/*! - \fn void QMapbox::initializeGLExtensions() - - Initializes the OpenGL extensions such as Vertex Array Objects (VAOs), - required by Mapbox GL Native engine. - - Should be called only once, after an OpenGL context is available. - Consecutive calls are ignored. -*/ -Q_DECL_EXPORT void initializeGLExtensions() -{ - mbgl::gl::InitializeExtensions([](const char* name) { -#if QT_VERSION >= 0x050000 - QOpenGLContext* thisContext = QOpenGLContext::currentContext(); - return thisContext->getProcAddress(name); -#else - const QGLContext* thisContext = QGLContext::currentContext(); - return reinterpret_cast(thisContext->getProcAddress(name)); -#endif - }); -} - } // namespace QMapbox diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index 384bdc8ebf..5776502a5b 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -1678,6 +1678,20 @@ void QMapboxGLPrivate::onSourceChanged(mbgl::style::Source&) emit mapChanged(QMapboxGL::MapChangeSourceDidChange); } +/*! + Initializes an OpenGL extension function such as Vertex Array Objects (VAOs), + required by Mapbox GL Native engine. +*/ +mbgl::gl::ProcAddress QMapboxGLPrivate::initializeExtension(const char* name) { +#if QT_VERSION >= 0x050000 + QOpenGLContext* thisContext = QOpenGLContext::currentContext(); + return thisContext->getProcAddress(name); +#else + const QGLContext* thisContext = QGLContext::currentContext(); + return reinterpret_cast(thisContext->getProcAddress(name)); +#endif +} + void QMapboxGLPrivate::connectionEstablished() { mbgl::NetworkStatus::Reachable(); diff --git a/platform/qt/src/qmapboxgl_p.hpp b/platform/qt/src/qmapboxgl_p.hpp index 4112542431..464b66bd73 100644 --- a/platform/qt/src/qmapboxgl_p.hpp +++ b/platform/qt/src/qmapboxgl_p.hpp @@ -58,6 +58,9 @@ public: bool dirty { false }; +private: + mbgl::gl::ProcAddress initializeExtension(const char*) override; + public slots: void connectionEstablished(); diff --git a/platform/qt/test/headless_backend_qt.cpp b/platform/qt/test/headless_backend_qt.cpp index 401ce55a7f..5f95b2f96a 100644 --- a/platform/qt/test/headless_backend_qt.cpp +++ b/platform/qt/test/headless_backend_qt.cpp @@ -24,13 +24,13 @@ struct QtImpl : public HeadlessBackend::Impl { QGLWidget widget; }; -gl::glProc HeadlessBackend::initializeExtension(const char* name) { +gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) { #if QT_VERSION >= 0x050000 QOpenGLContext* thisContext = QOpenGLContext::currentContext(); return thisContext->getProcAddress(name); #else const QGLContext* thisContext = QGLContext::currentContext(); - return reinterpret_cast(thisContext->getProcAddress(name)); + return reinterpret_cast(thisContext->getProcAddress(name)); #endif } diff --git a/platform/qt/test/qmapboxgl.cpp b/platform/qt/test/qmapboxgl.cpp index 650c635b99..453604076e 100644 --- a/platform/qt/test/qmapboxgl.cpp +++ b/platform/qt/test/qmapboxgl.cpp @@ -20,8 +20,6 @@ public: this, SLOT(onMapChanged(QMapboxGL::MapChange))); connect(&map, SIGNAL(needsRendering()), this, SLOT(onNeedsRendering())); - QMapbox::initializeGLExtensions(); - map.resize(fbo.size(), fbo.size()); map.setFramebufferObject(fbo.handle()); map.setCoordinateZoom(QMapbox::Coordinate(60.170448, 24.942046), 14); -- cgit v1.2.1