diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2017-01-11 15:00:54 +0100 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2017-01-11 16:22:03 +0100 |
commit | d90998c7bb5f8d1548ad5394b7fb77be33f403fb (patch) | |
tree | 8eab64c1aa76602735a0d5d4094c8d1f29a29eec | |
parent | a5e284d1ccec30cb4da0a45af9d644c6a7abf3a0 (diff) | |
download | qtlocation-mapboxgl-d90998c7bb5f8d1548ad5394b7fb77be33f403fb.tar.gz |
[core] require a Scheduler (=ThreadPool) for DefaultFileSource construction
this is a preparatory commit; the Scheduler is currently unused
23 files changed, 167 insertions, 56 deletions
diff --git a/benchmark/api/query.benchmark.cpp b/benchmark/api/query.benchmark.cpp index 456f43af7e..928386c596 100644 --- a/benchmark/api/query.benchmark.cpp +++ b/benchmark/api/query.benchmark.cpp @@ -35,8 +35,8 @@ public: util::RunLoop loop; HeadlessBackend backend; OffscreenView view{ backend.getContext(), { 1000, 1000 } }; - DefaultFileSource fileSource{ "benchmark/fixtures/api/cache.db", "." }; ThreadPool threadPool{ 4, { "Worker" } }; + DefaultFileSource fileSource{ threadPool, "benchmark/fixtures/api/cache.db", "." }; Map map{ backend, view.size, 1, fileSource, threadPool, MapMode::Still }; ScreenBox box{{ 0, 0 }, { 1000, 1000 }}; }; diff --git a/bin/offline.cpp b/bin/offline.cpp index 502561d0a1..068de9ee8d 100644 --- a/bin/offline.cpp +++ b/bin/offline.cpp @@ -1,5 +1,6 @@ #include <mbgl/util/default_styles.hpp> #include <mbgl/util/run_loop.hpp> +#include <mbgl/util/default_thread_pool.hpp> #include <mbgl/util/string.hpp> #include <mbgl/util/io.hpp> @@ -55,7 +56,8 @@ int main(int argc, char *argv[]) { using namespace mbgl; util::RunLoop loop; - DefaultFileSource fileSource(output, "."); + ThreadPool threadPool{ 2, { "Worker" } }; + DefaultFileSource fileSource(threadPool, output, "."); std::unique_ptr<OfflineRegion> region; fileSource.setAccessToken(token); diff --git a/bin/render.cpp b/bin/render.cpp index 0f6ff5f207..aa2a3408a2 100644 --- a/bin/render.cpp +++ b/bin/render.cpp @@ -69,7 +69,8 @@ int main(int argc, char *argv[]) { using namespace mbgl; util::RunLoop loop; - DefaultFileSource fileSource(cache_file, asset_root); + ThreadPool threadPool(4, { "Worker" }); + DefaultFileSource fileSource(threadPool, cache_file, asset_root); // Try to load the token from the environment. if (!token.size()) { @@ -86,7 +87,6 @@ int main(int argc, char *argv[]) { HeadlessBackend backend; OffscreenView view(backend.getContext(), { width * pixelRatio, height * pixelRatio }); - ThreadPool threadPool(4, { "Worker" }); Map map(backend, mbgl::Size { width, height }, pixelRatio, fileSource, threadPool, MapMode::Still); if (util::isURL(style_path)) { diff --git a/include/mbgl/storage/default_file_source.hpp b/include/mbgl/storage/default_file_source.hpp index b8f5e1167e..4bf1681c77 100644 --- a/include/mbgl/storage/default_file_source.hpp +++ b/include/mbgl/storage/default_file_source.hpp @@ -8,6 +8,8 @@ namespace mbgl { +class Scheduler; + namespace util { template <typename T> class Thread; } // namespace util @@ -21,7 +23,8 @@ public: * regions, we want the database to remain fairly small (order tens or low hundreds * of megabytes). */ - DefaultFileSource(const std::string& cachePath, + DefaultFileSource(Scheduler&, + const std::string& cachePath, const std::string& assetRoot, uint64_t maximumCacheSize = util::DEFAULT_MAX_CACHE_SIZE); ~DefaultFileSource() override; diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp index 5182e268f3..d3bd4e02dd 100755 --- a/platform/android/src/jni.cpp +++ b/platform/android/src/jni.cpp @@ -11,6 +11,7 @@ #include "jni.hpp" #include "java_types.hpp" #include "native_map_view.hpp" +#include "shared_thread_pool.hpp" #include "connectivity_listener.hpp" #include "style/layers/layers.hpp" #include "style/sources/sources.hpp" @@ -1236,7 +1237,8 @@ void nativeScheduleTakeSnapshot(JNIEnv *env, jni::jobject* obj, jlong nativeMapV jlong createDefaultFileSource(JNIEnv *env, jni::jobject* obj, jni::jstring* cachePath_, jni::jstring* assetRoot_, jlong maximumCacheSize) { std::string cachePath = std_string_from_jstring(env, cachePath_); std::string assetRoot = std_string_from_jstring(env, assetRoot_); - mbgl::DefaultFileSource *defaultFileSource = new mbgl::DefaultFileSource(cachePath, assetRoot, maximumCacheSize); + mbgl::DefaultFileSource* defaultFileSource = new mbgl::DefaultFileSource( + mbgl::android::sharedThreadPool(), cachePath, assetRoot, maximumCacheSize); jlong defaultFileSourcePtr = reinterpret_cast<jlong>(defaultFileSource); return defaultFileSourcePtr; } diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index d2ee1a64fe..6293aed759 100755 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -1,5 +1,6 @@ #include "native_map_view.hpp" #include "jni.hpp" +#include "shared_thread_pool.hpp" #include <cstdlib> #include <ctime> @@ -40,8 +41,7 @@ void log_egl_string(EGLDisplay display, EGLint name, const char *label) { NativeMapView::NativeMapView(JNIEnv *env_, jobject obj_, float pixelRatio, int availableProcessors_, size_t totalMemory_) : env(env_), availableProcessors(availableProcessors_), - totalMemory(totalMemory_), - threadPool(4, { "Worker" }) { + totalMemory(totalMemory_) { mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::NativeMapView"); assert(env_ != nullptr); @@ -59,12 +59,13 @@ NativeMapView::NativeMapView(JNIEnv *env_, jobject obj_, float pixelRatio, int a } fileSource = std::make_unique<mbgl::DefaultFileSource>( + sharedThreadPool(), mbgl::android::cachePath + "/mbgl-offline.db", mbgl::android::apkPath); map = std::make_unique<mbgl::Map>( *this, mbgl::Size{ static_cast<uint32_t>(width), static_cast<uint32_t>(height) }, - pixelRatio, *fileSource, threadPool, MapMode::Continuous); + pixelRatio, *fileSource, sharedThreadPool(), MapMode::Continuous); float zoomFactor = map->getMaxZoom() - map->getMinZoom() + 1; float cpuFactor = availableProcessors; diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp index e7379700a9..b7513dc572 100755 --- a/platform/android/src/native_map_view.hpp +++ b/platform/android/src/native_map_view.hpp @@ -4,7 +4,6 @@ #include <mbgl/map/view.hpp> #include <mbgl/map/backend.hpp> #include <mbgl/util/noncopyable.hpp> -#include <mbgl/util/default_thread_pool.hpp> #include <mbgl/storage/default_file_source.hpp> #include <string> @@ -97,7 +96,6 @@ private: // Ensure these are initialised last std::unique_ptr<mbgl::DefaultFileSource> fileSource; - mbgl::ThreadPool threadPool; std::unique_ptr<mbgl::Map> map; mbgl::EdgeInsets insets; diff --git a/platform/android/src/shared_thread_pool.hpp b/platform/android/src/shared_thread_pool.hpp new file mode 100644 index 0000000000..3229c03193 --- /dev/null +++ b/platform/android/src/shared_thread_pool.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include <mbgl/util/default_thread_pool.hpp> + +namespace mbgl { +namespace android { + +// May only be called from the main/UI thread. +inline ThreadPool& sharedThreadPool() { + static ThreadPool threadPool{ 4, { "Worker" } }; + return threadPool; +} + +} +} diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index 10acc58b25..81219ec820 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -5,6 +5,7 @@ #import "MGLNetworkConfiguration.h" #import "MGLOfflinePack_Private.h" #import "MGLOfflineRegion_Private.h" +#import "MGLThreadPool_Private.h" #import "MGLTilePyramidOfflineRegion.h" #import "NSValue+MGLAdditions.h" @@ -130,7 +131,9 @@ NSString * const MGLOfflinePackMaximumCountUserInfoKey = MGLOfflinePackUserInfoK [[NSFileManager defaultManager] moveItemAtPath:subdirectorylessCacheURL.path toPath:cachePath error:NULL]; } - _mbglFileSource = new mbgl::DefaultFileSource(cachePath.UTF8String, [NSBundle mainBundle].resourceURL.path.UTF8String); + _mbglFileSource = new mbgl::DefaultFileSource( + *[MGLThreadPool sharedPool].mbglThreadPool, cachePath.UTF8String, + [NSBundle mainBundle].resourceURL.path.UTF8String); // Observe for changes to the API base URL (and find out the current one). [[MGLNetworkConfiguration sharedManager] addObserver:self diff --git a/platform/darwin/src/MGLThreadPool.mm b/platform/darwin/src/MGLThreadPool.mm new file mode 100644 index 0000000000..fa57233fb8 --- /dev/null +++ b/platform/darwin/src/MGLThreadPool.mm @@ -0,0 +1,39 @@ +#import "MGLThreadPool_Private.h" + +#import "NSProcessInfo+MGLAdditions.h" + +@interface MGLThreadPool () + +@property (nonatomic) mbgl::ThreadPool *mbglThreadPool; + +@end + +@implementation MGLThreadPool + +#pragma mark - Internal + ++ (instancetype)sharedPool { + if (NSProcessInfo.processInfo.mgl_isInterfaceBuilderDesignablesAgent) { + return nil; + } + static dispatch_once_t onceToken; + static MGLThreadPool *sharedThreadPool; + dispatch_once(&onceToken, ^{ + sharedThreadPool = [[self alloc] init]; + }); + return sharedThreadPool; +} + +- (instancetype)init { + if (self = [super init]) { + _mbglThreadPool = new mbgl::ThreadPool(4, { "Worker" }); + } + return self; +} + +- (void)dealloc { + delete _mbglThreadPool; + _mbglThreadPool = nullptr; +} + +@end diff --git a/platform/darwin/src/MGLThreadPool_Private.h b/platform/darwin/src/MGLThreadPool_Private.h new file mode 100644 index 0000000000..c1420ab310 --- /dev/null +++ b/platform/darwin/src/MGLThreadPool_Private.h @@ -0,0 +1,18 @@ +#import <Foundation/Foundation.h> + +#include <mbgl/util/default_thread_pool.hpp> + +@interface MGLThreadPool : NSObject +@end + +@interface MGLThreadPool (Private) + +/// Returns the shared instance of the `MGLThreadPool` class. ++ (instancetype)sharedPool; + +/** + The shared thread pool object owned by the shared thread pool object. + */ +@property (nonatomic) mbgl::ThreadPool *mbglThreadPool; + +@end diff --git a/platform/default/default_file_source.cpp b/platform/default/default_file_source.cpp index c4222b5a12..80ddb23050 100644 --- a/platform/default/default_file_source.cpp +++ b/platform/default/default_file_source.cpp @@ -160,7 +160,8 @@ private: std::unordered_map<int64_t, std::unique_ptr<OfflineDownload>> downloads; }; -DefaultFileSource::DefaultFileSource(const std::string& cachePath, +DefaultFileSource::DefaultFileSource(Scheduler&, + const std::string& cachePath, const std::string& assetRoot, uint64_t maximumCacheSize) : thread(std::make_unique<util::Thread<Impl>>(util::ThreadContext{"DefaultFileSource", util::ThreadPriority::Low}, diff --git a/platform/glfw/main.cpp b/platform/glfw/main.cpp index f9b1ec93cc..bd7c6e7216 100644 --- a/platform/glfw/main.cpp +++ b/platform/glfw/main.cpp @@ -108,7 +108,9 @@ int main(int argc, char *argv[]) { GLFWView backend(fullscreen, benchmark); view = &backend; - mbgl::DefaultFileSource fileSource("/tmp/mbgl-cache.db", "."); + mbgl::ThreadPool threadPool(4, { "Worker" }); + + mbgl::DefaultFileSource fileSource(threadPool, "/tmp/mbgl-cache.db", "."); // Set access token if present const char *token = getenv("MAPBOX_ACCESS_TOKEN"); @@ -118,8 +120,6 @@ int main(int argc, char *argv[]) { fileSource.setAccessToken(std::string(token)); } - mbgl::ThreadPool threadPool(4, { "Worker" }); - mbgl::Map map(backend, view->getSize(), view->getPixelRatio(), fileSource, threadPool); backend.setMap(&map); diff --git a/platform/ios/ios.xcodeproj/project.pbxproj b/platform/ios/ios.xcodeproj/project.pbxproj index 6f2ee38e94..3595e917b4 100644 --- a/platform/ios/ios.xcodeproj/project.pbxproj +++ b/platform/ios/ios.xcodeproj/project.pbxproj @@ -162,6 +162,10 @@ 40F887701D7A1E58008ECB67 /* MGLShapeSource_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 40F8876F1D7A1DB8008ECB67 /* MGLShapeSource_Private.h */; }; 40F887711D7A1E59008ECB67 /* MGLShapeSource_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 40F8876F1D7A1DB8008ECB67 /* MGLShapeSource_Private.h */; }; 40FDA76B1CCAAA6800442548 /* MBXAnnotationView.m in Sources */ = {isa = PBXBuildFile; fileRef = 40FDA76A1CCAAA6800442548 /* MBXAnnotationView.m */; }; + 553B1F0B1E267D9500606163 /* MGLThreadPool_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 553B1F091E267D9500606163 /* MGLThreadPool_Private.h */; }; + 553B1F0C1E267D9500606163 /* MGLThreadPool_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 553B1F091E267D9500606163 /* MGLThreadPool_Private.h */; }; + 553B1F0D1E267D9500606163 /* MGLThreadPool.mm in Sources */ = {isa = PBXBuildFile; fileRef = 553B1F0A1E267D9500606163 /* MGLThreadPool.mm */; }; + 553B1F0E1E267D9500606163 /* MGLThreadPool.mm in Sources */ = {isa = PBXBuildFile; fileRef = 553B1F0A1E267D9500606163 /* MGLThreadPool.mm */; }; 554180421D2E97DE00012372 /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 554180411D2E97DE00012372 /* OpenGLES.framework */; }; 556660CA1E1BF3A900E2C41B /* MGLFoundation.h in Headers */ = {isa = PBXBuildFile; fileRef = 556660C91E1BF3A900E2C41B /* MGLFoundation.h */; settings = {ATTRIBUTES = (Public, ); }; }; 556660D81E1D085500E2C41B /* MGLVersionNumber.m in Sources */ = {isa = PBXBuildFile; fileRef = 556660D71E1D085500E2C41B /* MGLVersionNumber.m */; }; @@ -611,6 +615,8 @@ 40F8876F1D7A1DB8008ECB67 /* MGLShapeSource_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLShapeSource_Private.h; sourceTree = "<group>"; }; 40FDA7691CCAAA6800442548 /* MBXAnnotationView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MBXAnnotationView.h; sourceTree = "<group>"; }; 40FDA76A1CCAAA6800442548 /* MBXAnnotationView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MBXAnnotationView.m; sourceTree = "<group>"; }; + 553B1F091E267D9500606163 /* MGLThreadPool_Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLThreadPool_Private.h; sourceTree = "<group>"; }; + 553B1F0A1E267D9500606163 /* MGLThreadPool.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLThreadPool.mm; sourceTree = "<group>"; }; 554180411D2E97DE00012372 /* OpenGLES.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGLES.framework; path = System/Library/Frameworks/OpenGLES.framework; sourceTree = SDKROOT; }; 556660C91E1BF3A900E2C41B /* MGLFoundation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLFoundation.h; sourceTree = "<group>"; }; 556660D71E1D085500E2C41B /* MGLVersionNumber.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = MGLVersionNumber.m; path = ../../darwin/test/MGLVersionNumber.m; sourceTree = "<group>"; }; @@ -1149,6 +1155,8 @@ DA8847EC1CBAFA5100AB86E3 /* MGLStyle.h */, 35E0CFE51D3E501500188327 /* MGLStyle_Private.h */, DA88480F1CBAFA6200AB86E3 /* MGLStyle.mm */, + 553B1F091E267D9500606163 /* MGLThreadPool_Private.h */, + 553B1F0A1E267D9500606163 /* MGLThreadPool.mm */, DA8847EE1CBAFA5100AB86E3 /* MGLTypes.h */, DA8848111CBAFA6200AB86E3 /* MGLTypes.m */, DA8848911CBB049300AB86E3 /* reachability */, @@ -1474,6 +1482,7 @@ 3510FFF01D6D9D8C00F413B2 /* NSExpression+MGLAdditions.h in Headers */, 353AFA141D65AB17005A69F4 /* NSDate+MGLAdditions.h in Headers */, DA8848531CBAFB9800AB86E3 /* MGLCompactCalloutView.h in Headers */, + 553B1F0B1E267D9500606163 /* MGLThreadPool_Private.h in Headers */, DA8847FB1CBAFA5100AB86E3 /* MGLShape.h in Headers */, 353933F51D3FB785003F57D7 /* MGLBackgroundStyleLayer.h in Headers */, DA88485A1CBAFB9800AB86E3 /* MGLUserLocation_Private.h in Headers */, @@ -1568,6 +1577,7 @@ 35B82BF91D6C5F8400B1B721 /* NSPredicate+MGLAdditions.h in Headers */, DA35A2CA1CCAAAD200E826B2 /* NSValue+MGLAdditions.h in Headers */, 350098BC1D480108004B2AF0 /* MGLVectorSource.h in Headers */, + 553B1F0C1E267D9500606163 /* MGLThreadPool_Private.h in Headers */, 353933FC1D3FB7C0003F57D7 /* MGLRasterStyleLayer.h in Headers */, 3566C76D1D4A8DFA008152BC /* MGLRasterSource.h in Headers */, DAED38641D62D0FC00D7640F /* NSURL+MGLAdditions.h in Headers */, @@ -2003,6 +2013,7 @@ 350098BD1D480108004B2AF0 /* MGLVectorSource.mm in Sources */, 3566C76E1D4A8DFA008152BC /* MGLRasterSource.mm in Sources */, DA88488C1CBB037E00AB86E3 /* SMCalloutView.m in Sources */, + 553B1F0D1E267D9500606163 /* MGLThreadPool.mm in Sources */, 35136D4E1D4277FC00C20EFD /* MGLSource.mm in Sources */, DA35A2B81CCA9A5D00E826B2 /* MGLClockDirectionFormatter.m in Sources */, DAD1657A1CF4CDFF001FF4B9 /* MGLShapeCollection.mm in Sources */, @@ -2078,6 +2089,7 @@ 350098BE1D480108004B2AF0 /* MGLVectorSource.mm in Sources */, 3566C76F1D4A8DFA008152BC /* MGLRasterSource.mm in Sources */, DAA4E4351CBB730400178DFB /* SMCalloutView.m in Sources */, + 553B1F0E1E267D9500606163 /* MGLThreadPool.mm in Sources */, 35136D4F1D4277FC00C20EFD /* MGLSource.mm in Sources */, DA35A2B91CCA9A5D00E826B2 /* MGLClockDirectionFormatter.m in Sources */, DAD1657B1CF4CDFF001FF4B9 /* MGLShapeCollection.mm in Sources */, diff --git a/platform/macos/macos.xcodeproj/project.pbxproj b/platform/macos/macos.xcodeproj/project.pbxproj index 6f6aad2436..dc0aa5e8e2 100644 --- a/platform/macos/macos.xcodeproj/project.pbxproj +++ b/platform/macos/macos.xcodeproj/project.pbxproj @@ -57,6 +57,8 @@ 40B77E461DB11BCD003DA2FE /* NSArray+MGLAdditions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 40B77E421DB11BB0003DA2FE /* NSArray+MGLAdditions.mm */; }; 40E1601D1DF217D6005EA6D9 /* MGLStyleLayerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 40E1601B1DF216E6005EA6D9 /* MGLStyleLayerTests.m */; }; 52BECB0A1CC5A26F009CD791 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52BECB091CC5A26F009CD791 /* SystemConfiguration.framework */; }; + 553B1F051E267AD900606163 /* MGLThreadPool_Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 553B1F021E267AD900606163 /* MGLThreadPool_Private.h */; }; + 553B1F071E267AD900606163 /* MGLThreadPool.mm in Sources */ = {isa = PBXBuildFile; fileRef = 553B1F041E267AD900606163 /* MGLThreadPool.mm */; }; 5548BE781D09E718005DDE81 /* libmbgl-core.a in Frameworks */ = {isa = PBXBuildFile; fileRef = DAE6C3451CC31D1200DB3429 /* libmbgl-core.a */; }; 556660C61E1BEA0100E2C41B /* MGLFoundation.h in Headers */ = {isa = PBXBuildFile; fileRef = 556660C51E1BEA0100E2C41B /* MGLFoundation.h */; settings = {ATTRIBUTES = (Public, ); }; }; 556660D61E1D07E400E2C41B /* MGLVersionNumber.m in Sources */ = {isa = PBXBuildFile; fileRef = 556660D51E1D07E400E2C41B /* MGLVersionNumber.m */; }; @@ -305,6 +307,8 @@ 40E1601A1DF216E6005EA6D9 /* MGLStyleLayerTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLStyleLayerTests.h; sourceTree = "<group>"; }; 40E1601B1DF216E6005EA6D9 /* MGLStyleLayerTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MGLStyleLayerTests.m; sourceTree = "<group>"; }; 52BECB091CC5A26F009CD791 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; }; + 553B1F021E267AD900606163 /* MGLThreadPool_Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLThreadPool_Private.h; sourceTree = "<group>"; }; + 553B1F041E267AD900606163 /* MGLThreadPool.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MGLThreadPool.mm; sourceTree = "<group>"; }; 5548BE791D0ACBB2005DDE81 /* libmbgl-loop-darwin.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = "libmbgl-loop-darwin.a"; path = "cmake/Debug/libmbgl-loop-darwin.a"; sourceTree = "<group>"; }; 5548BE7B1D0ACBBD005DDE81 /* libmbgl-loop-darwin.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = "libmbgl-loop-darwin.a"; path = "cmake/Debug/libmbgl-loop-darwin.a"; sourceTree = "<group>"; }; 556660C51E1BEA0100E2C41B /* MGLFoundation.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MGLFoundation.h; sourceTree = "<group>"; }; @@ -916,6 +920,8 @@ DAE6C3571CC31E0400DB3429 /* MGLStyle.h */, 3537CA731D3F93A600380318 /* MGLStyle_Private.h */, DAE6C37A1CC31E2A00DB3429 /* MGLStyle.mm */, + 553B1F021E267AD900606163 /* MGLThreadPool_Private.h */, + 553B1F041E267AD900606163 /* MGLThreadPool.mm */, DAE6C3591CC31E0400DB3429 /* MGLTypes.h */, DAE6C37C1CC31E2A00DB3429 /* MGLTypes.m */, DA87A99F1DC9DC6200810D09 /* MGLValueEvaluator.h */, @@ -1009,6 +1015,7 @@ 352742811D4C243B00A1ECE6 /* MGLSource.h in Headers */, DAE6C3C21CC31F4500DB3429 /* Mapbox.h in Headers */, DAE6C3641CC31E0400DB3429 /* MGLPolygon.h in Headers */, + 553B1F051E267AD900606163 /* MGLThreadPool_Private.h in Headers */, DA35A2BF1CCA9B1A00E826B2 /* MGLClockDirectionFormatter.h in Headers */, 35602BFA1D3EA99F0050646F /* MGLFillStyleLayer.h in Headers */, DA35A2A41CC9EB1A00E826B2 /* MGLCoordinateFormatter.h in Headers */, @@ -1267,6 +1274,7 @@ DA35A2D01CCAAED300E826B2 /* NSValue+MGLAdditions.m in Sources */, 3538AA241D542685008EC33D /* MGLStyleLayer.mm in Sources */, DA35A2C01CCA9B1A00E826B2 /* MGLClockDirectionFormatter.m in Sources */, + 553B1F071E267AD900606163 /* MGLThreadPool.mm in Sources */, DAE6C3BA1CC31EF300DB3429 /* MGLOpenGLLayer.mm in Sources */, DAE6C38A1CC31E2A00DB3429 /* MGLMultiPoint.mm in Sources */, DAE6C3961CC31E2A00DB3429 /* MGLTypes.m in Sources */, diff --git a/platform/macos/src/MGLMapView.mm b/platform/macos/src/MGLMapView.mm index 5df5a1f957..a66f9666b1 100644 --- a/platform/macos/src/MGLMapView.mm +++ b/platform/macos/src/MGLMapView.mm @@ -10,6 +10,7 @@ #import "MGLGeometry_Private.h" #import "MGLMultiPoint_Private.h" #import "MGLOfflineStorage_Private.h" +#import "MGLThreadPool_Private.h" #import "MGLStyle_Private.h" #import "MGLAccountManager.h" @@ -23,7 +24,6 @@ #import <mbgl/annotation/annotation.hpp> #import <mbgl/map/camera.hpp> #import <mbgl/storage/reachability.h> -#import <mbgl/util/default_thread_pool.hpp> #import <mbgl/gl/extension.hpp> #import <mbgl/gl/context.hpp> #import <mbgl/map/backend.hpp> @@ -150,7 +150,6 @@ public: /// Cross-platform map view controller. mbgl::Map *_mbglMap; MGLMapViewImpl *_mbglView; - mbgl::ThreadPool *_mbglThreadPool; NSPanGestureRecognizer *_panGestureRecognizer; NSMagnificationGestureRecognizer *_magnificationGestureRecognizer; @@ -257,10 +256,13 @@ public: NSURL *legacyCacheURL = [cachesDirectoryURL URLByAppendingPathComponent:@"cache.db"]; [[NSFileManager defaultManager] removeItemAtURL:legacyCacheURL error:NULL]; - mbgl::DefaultFileSource* mbglFileSource = [MGLOfflineStorage sharedOfflineStorage].mbglFileSource; + mbgl::DefaultFileSource* mbglFileSource = + [MGLOfflineStorage sharedOfflineStorage].mbglFileSource; - _mbglThreadPool = new mbgl::ThreadPool(4, { "Worker" }); - _mbglMap = new mbgl::Map(*_mbglView, self.size, [NSScreen mainScreen].backingScaleFactor, *mbglFileSource, *_mbglThreadPool, mbgl::MapMode::Continuous, mbgl::GLContextMode::Unique, mbgl::ConstrainMode::None, mbgl::ViewportMode::Default); + _mbglMap = new mbgl::Map(*_mbglView, self.size, [NSScreen mainScreen].backingScaleFactor, + *mbglFileSource, *[MGLThreadPool sharedPool].mbglThreadPool, + mbgl::MapMode::Continuous, mbgl::GLContextMode::Unique, + mbgl::ConstrainMode::None, mbgl::ViewportMode::Default); [self validateTileCacheSize]; // Install the OpenGL layer. Interface Builder’s synchronous drawing means @@ -510,10 +512,6 @@ public: delete _mbglView; _mbglView = nullptr; } - if (_mbglThreadPool) { - delete _mbglThreadPool; - _mbglThreadPool = nullptr; - } } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(__unused NSDictionary *)change context:(void *)context { diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index 9d3b5dc099..87eb77554b 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -1494,11 +1494,12 @@ QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settin : QObject(q) , size(size_) , q_ptr(q) + , threadPool(4, { "Worker" }) , fileSourceObj(std::make_unique<mbgl::DefaultFileSource>( + threadPool, settings.cacheDatabasePath().toStdString(), settings.assetPath().toStdString(), settings.cacheDatabaseMaximumSize())) - , threadPool(4, { "Worker" }) , mapObj(std::make_unique<mbgl::Map>( *this, mbgl::Size{ static_cast<uint32_t>(size.width()), static_cast<uint32_t>(size.height()) }, pixelRatio, *fileSourceObj, threadPool, diff --git a/platform/qt/src/qmapboxgl_p.hpp b/platform/qt/src/qmapboxgl_p.hpp index 3d2be90273..f6c13a8e5b 100644 --- a/platform/qt/src/qmapboxgl_p.hpp +++ b/platform/qt/src/qmapboxgl_p.hpp @@ -37,8 +37,8 @@ public: QMapboxGL *q_ptr { nullptr }; - std::unique_ptr<mbgl::DefaultFileSource> fileSourceObj; mbgl::ThreadPool threadPool; + std::unique_ptr<mbgl::DefaultFileSource> fileSourceObj; std::unique_ptr<mbgl::Map> mapObj; bool dirty { false }; diff --git a/test/api/custom_layer.test.cpp b/test/api/custom_layer.test.cpp index 227523d90a..afeb87a97e 100644 --- a/test/api/custom_layer.test.cpp +++ b/test/api/custom_layer.test.cpp @@ -84,19 +84,18 @@ public: TEST(CustomLayer, Basic) { util::RunLoop loop; + ThreadPool threadPool{ 4, { "Worker" } }; HeadlessBackend backend { test::sharedDisplay() }; OffscreenView view { backend.getContext() }; #ifdef MBGL_ASSET_ZIP // Regenerate with `cd test/fixtures/api/ && zip -r assets.zip assets/` - DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets.zip"); + DefaultFileSource fileSource(threadPool, ":memory:", "test/fixtures/api/assets.zip"); #else - DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets"); + DefaultFileSource fileSource(threadPool, ":memory:", "test/fixtures/api/assets"); #endif - ThreadPool threadPool{ 4, { "Worker" } }; - Map map(backend, view.size, 1, fileSource, threadPool, MapMode::Still); map.setStyleJSON(util::read_file("test/fixtures/api/water.json")); map.setLatLngZoom({ 37.8, -122.5 }, 10); diff --git a/test/api/render_missing.test.cpp b/test/api/render_missing.test.cpp index 9c53f35102..8414acc191 100644 --- a/test/api/render_missing.test.cpp +++ b/test/api/render_missing.test.cpp @@ -22,6 +22,7 @@ TEST(API, TEST_REQUIRES_SERVER(RenderMissingTile)) { using namespace mbgl; util::RunLoop loop; + ThreadPool threadPool{ 4, { "Worker" } }; const auto style = util::read_file("test/fixtures/api/water_missing_tiles.json"); @@ -29,13 +30,11 @@ TEST(API, TEST_REQUIRES_SERVER(RenderMissingTile)) { OffscreenView view { backend.getContext(), { 256, 512 } }; #ifdef MBGL_ASSET_ZIP // Regenerate with `cd test/fixtures/api/ && zip -r assets.zip assets/` - DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets.zip"); + DefaultFileSource fileSource(threadPool, ":memory:", "test/fixtures/api/assets.zip"); #else - DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets"); + DefaultFileSource fileSource(threadPool, ":memory:", "test/fixtures/api/assets"); #endif - ThreadPool threadPool{ 4, { "Worker" } }; - Log::setObserver(std::make_unique<FixtureLogObserver>()); Map map(backend, view.size, 1, fileSource, threadPool, MapMode::Still); diff --git a/test/api/repeated_render.test.cpp b/test/api/repeated_render.test.cpp index 4ece3c3150..e0cf3d427f 100644 --- a/test/api/repeated_render.test.cpp +++ b/test/api/repeated_render.test.cpp @@ -16,8 +16,8 @@ using namespace mbgl; TEST(API, RepeatedRender) { - util::RunLoop loop; + ThreadPool threadPool{ 4, { "Worker" } }; const auto style = util::read_file("test/fixtures/api/water.json"); @@ -25,13 +25,11 @@ TEST(API, RepeatedRender) { OffscreenView view { backend.getContext(), { 256, 512 } }; #ifdef MBGL_ASSET_ZIP // Regenerate with `cd test/fixtures/api/ && zip -r assets.zip assets/` - DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets.zip"); + DefaultFileSource fileSource(threadPool, ":memory:", "test/fixtures/api/assets.zip"); #else - DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets"); + DefaultFileSource fileSource(threadPool, ":memory:", "test/fixtures/api/assets"); #endif - ThreadPool threadPool{ 4, { "Worker" } }; - Log::setObserver(std::make_unique<FixtureLogObserver>()); Map map(backend, view.size, 1, fileSource, threadPool, MapMode::Still); diff --git a/test/map/map.test.cpp b/test/map/map.test.cpp index 9ee2d45a81..6821e1444a 100644 --- a/test/map/map.test.cpp +++ b/test/map/map.test.cpp @@ -47,7 +47,7 @@ TEST(Map, LatLngBehavior) { TEST(Map, Offline) { MapTest test; - DefaultFileSource fileSource(":memory:", "."); + DefaultFileSource fileSource(test.threadPool, ":memory:", "."); auto expiredItem = [] (const std::string& path) { Response response; @@ -479,9 +479,9 @@ TEST(Map, TEST_DISABLED_ON_CI(ContinuousRendering)) { #ifdef MBGL_ASSET_ZIP // Regenerate with `cd test/fixtures/api/ && zip -r assets.zip assets/` - DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets.zip"); + DefaultFileSource fileSource(threadPool, ":memory:", "test/fixtures/api/assets.zip"); #else - DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets"); + DefaultFileSource fileSource(threadPool, ":memory:", "test/fixtures/api/assets"); #endif Map map(backend, view.size, 1, fileSource, threadPool, MapMode::Continuous); diff --git a/test/storage/default_file_source.test.cpp b/test/storage/default_file_source.test.cpp index f4c23c4c7a..3949b8271c 100644 --- a/test/storage/default_file_source.test.cpp +++ b/test/storage/default_file_source.test.cpp @@ -1,12 +1,14 @@ #include <mbgl/test/util.hpp> #include <mbgl/storage/default_file_source.hpp> +#include <mbgl/util/default_thread_pool.hpp> #include <mbgl/util/run_loop.hpp> using namespace mbgl; TEST(DefaultFileSource, TEST_REQUIRES_SERVER(CacheResponse)) { util::RunLoop loop; - DefaultFileSource fs(":memory:", "."); + ThreadPool threadPool{ 1, { "Worker" } }; + DefaultFileSource fs(threadPool, ":memory:", "."); const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/cache" }; Response response; @@ -44,7 +46,8 @@ TEST(DefaultFileSource, TEST_REQUIRES_SERVER(CacheResponse)) { TEST(DefaultFileSource, TEST_REQUIRES_SERVER(CacheRevalidateSame)) { util::RunLoop loop; - DefaultFileSource fs(":memory:", "."); + ThreadPool threadPool{ 1, { "Worker" } }; + DefaultFileSource fs(threadPool, ":memory:", "."); const Resource revalidateSame { Resource::Unknown, "http://127.0.0.1:3000/revalidate-same" }; std::unique_ptr<AsyncRequest> req1; @@ -88,7 +91,8 @@ TEST(DefaultFileSource, TEST_REQUIRES_SERVER(CacheRevalidateSame)) { TEST(DefaultFileSource, TEST_REQUIRES_SERVER(CacheRevalidateModified)) { util::RunLoop loop; - DefaultFileSource fs(":memory:", "."); + ThreadPool threadPool{ 1, { "Worker" } }; + DefaultFileSource fs(threadPool, ":memory:", "."); const Resource revalidateModified{ Resource::Unknown, "http://127.0.0.1:3000/revalidate-modified" }; @@ -132,7 +136,8 @@ TEST(DefaultFileSource, TEST_REQUIRES_SERVER(CacheRevalidateModified)) { TEST(DefaultFileSource, TEST_REQUIRES_SERVER(CacheRevalidateEtag)) { util::RunLoop loop; - DefaultFileSource fs(":memory:", "."); + ThreadPool threadPool{ 1, { "Worker" } }; + DefaultFileSource fs(threadPool, ":memory:", "."); const Resource revalidateEtag { Resource::Unknown, "http://127.0.0.1:3000/revalidate-etag" }; std::unique_ptr<AsyncRequest> req1; @@ -187,7 +192,8 @@ TEST(DefaultFileSource, TEST_REQUIRES_SERVER(CacheRevalidateEtag)) { TEST(DefaultFileSource, TEST_REQUIRES_SERVER(HTTPIssue1369)) { util::RunLoop loop; - DefaultFileSource fs(":memory:", "."); + ThreadPool threadPool{ 1, { "Worker" } }; + DefaultFileSource fs(threadPool, ":memory:", "."); const Resource resource { Resource::Unknown, "http://127.0.0.1:3000/test" }; @@ -211,7 +217,8 @@ TEST(DefaultFileSource, TEST_REQUIRES_SERVER(HTTPIssue1369)) { TEST(DefaultFileSource, OptionalNonExpired) { util::RunLoop loop; - DefaultFileSource fs(":memory:", "."); + ThreadPool threadPool{ 1, { "Worker" } }; + DefaultFileSource fs(threadPool, ":memory:", "."); const Resource optionalResource { Resource::Unknown, "http://127.0.0.1:3000/test", {}, Resource::Optional }; @@ -240,7 +247,8 @@ TEST(DefaultFileSource, OptionalNonExpired) { TEST(DefaultFileSource, OptionalExpired) { util::RunLoop loop; - DefaultFileSource fs(":memory:", "."); + ThreadPool threadPool{ 1, { "Worker" } }; + DefaultFileSource fs(threadPool, ":memory:", "."); const Resource optionalResource { Resource::Unknown, "http://127.0.0.1:3000/test", {}, Resource::Optional }; @@ -269,7 +277,8 @@ TEST(DefaultFileSource, OptionalExpired) { TEST(DefaultFileSource, OptionalNotFound) { util::RunLoop loop; - DefaultFileSource fs(":memory:", "."); + ThreadPool threadPool{ 1, { "Worker" } }; + DefaultFileSource fs(threadPool, ":memory:", "."); const Resource optionalResource { Resource::Unknown, "http://127.0.0.1:3000/test", {}, Resource::Optional }; @@ -295,7 +304,8 @@ TEST(DefaultFileSource, OptionalNotFound) { // from cache like a regular request TEST(DefaultFileSource, TEST_REQUIRES_SERVER(NoCacheRefreshEtagNotModified)) { util::RunLoop loop; - DefaultFileSource fs(":memory:", "."); + ThreadPool threadPool{ 1, { "Worker" } }; + DefaultFileSource fs(threadPool, ":memory:", "."); Resource resource { Resource::Unknown, "http://127.0.0.1:3000/revalidate-same" }; resource.priorEtag.emplace("snowfall"); @@ -329,7 +339,8 @@ TEST(DefaultFileSource, TEST_REQUIRES_SERVER(NoCacheRefreshEtagNotModified)) { // from cache like a regular request TEST(DefaultFileSource, TEST_REQUIRES_SERVER(NoCacheRefreshEtagModified)) { util::RunLoop loop; - DefaultFileSource fs(":memory:", "."); + ThreadPool threadPool{ 1, { "Worker" } }; + DefaultFileSource fs(threadPool, ":memory:", "."); Resource resource { Resource::Unknown, "http://127.0.0.1:3000/revalidate-same" }; resource.priorEtag.emplace("sunshine"); @@ -363,7 +374,8 @@ TEST(DefaultFileSource, TEST_REQUIRES_SERVER(NoCacheRefreshEtagModified)) { // from cache like a regular request. TEST(DefaultFileSource, TEST_REQUIRES_SERVER(NoCacheFull)) { util::RunLoop loop; - DefaultFileSource fs(":memory:", "."); + ThreadPool threadPool{ 1, { "Worker" } }; + DefaultFileSource fs(threadPool, ":memory:", "."); Resource resource { Resource::Unknown, "http://127.0.0.1:3000/revalidate-same" }; // Setting any prior field results in skipping the cache. @@ -398,7 +410,8 @@ TEST(DefaultFileSource, TEST_REQUIRES_SERVER(NoCacheFull)) { // from cache like a regular request TEST(DefaultFileSource, TEST_REQUIRES_SERVER(NoCacheRefreshModifiedNotModified)) { util::RunLoop loop; - DefaultFileSource fs(":memory:", "."); + ThreadPool threadPool{ 1, { "Worker" } }; + DefaultFileSource fs(threadPool, ":memory:", "."); Resource resource { Resource::Unknown, "http://127.0.0.1:3000/revalidate-modified" }; resource.priorModified.emplace(Seconds(1420070400)); // January 1, 2015 @@ -432,7 +445,8 @@ TEST(DefaultFileSource, TEST_REQUIRES_SERVER(NoCacheRefreshModifiedNotModified)) // from cache like a regular request TEST(DefaultFileSource, TEST_REQUIRES_SERVER(NoCacheRefreshModifiedModified)) { util::RunLoop loop; - DefaultFileSource fs(":memory:", "."); + ThreadPool threadPool{ 1, { "Worker" } }; + DefaultFileSource fs(threadPool, ":memory:", "."); Resource resource { Resource::Unknown, "http://127.0.0.1:3000/revalidate-modified" }; resource.priorModified.emplace(Seconds(1417392000)); // December 1, 2014 |