diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2019-03-19 16:57:36 +0200 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2019-03-20 21:30:08 +0200 |
commit | d5868d3da822f2bf3297229bd879e76853108a63 (patch) | |
tree | b3d28aab92b938cf541f917f1027d2bbd06d9780 /platform | |
parent | d1762d7111b39d45430bd7bb75ea60b7a5d85bd2 (diff) | |
download | qtlocation-mapboxgl-d5868d3da822f2bf3297229bd879e76853108a63.tar.gz |
[core] Remove file source from public Map ctor
Diffstat (limited to 'platform')
30 files changed, 242 insertions, 228 deletions
diff --git a/platform/android/src/file_source.cpp b/platform/android/src/file_source.cpp index 4cfb545b84..bb1f30981b 100644 --- a/platform/android/src/file_source.cpp +++ b/platform/android/src/file_source.cpp @@ -3,6 +3,7 @@ #include <mbgl/actor/actor.hpp> #include <mbgl/actor/scheduler.hpp> +#include <mbgl/storage/resource_options.hpp> #include <mbgl/storage/resource_transform.hpp> #include <mbgl/util/logging.hpp> @@ -11,6 +12,14 @@ #include "asset_manager_file_source.hpp" namespace mbgl { + +std::shared_ptr<FileSource> FileSource::createPlatformFileSource(const ResourceOptions& options) { + auto* assetFileSource = reinterpret_cast<AssetManagerFileSource*>(options.platformContext()); + auto fileSource = std::make_shared<DefaultFileSource>(options.cachePath(), std::unique_ptr<AssetManagerFileSource>(assetFileSource)); + fileSource->setAccessToken(options.accessToken()); + return fileSource; +} + namespace android { // FileSource // @@ -22,15 +31,13 @@ FileSource::FileSource(jni::JNIEnv& _env, std::string path = jni::Make<std::string>(_env, _cachePath); mapbox::sqlite::setTempPath(path); - // Create a core default file source - fileSource = std::make_unique<mbgl::DefaultFileSource>( - path + DATABASE_FILE, - std::make_unique<AssetManagerFileSource>(_env, assetManager)); + resourceOptions = mbgl::ResourceOptions() + .withAccessToken(accessToken ? jni::Make<std::string>(_env, accessToken) : "") + .withCachePath(path + DATABASE_FILE) + .withPlatformContext(reinterpret_cast<void*>(new AssetManagerFileSource(_env, assetManager))); - // Set access token - if (accessToken) { - fileSource->setAccessToken(jni::Make<std::string>(_env, accessToken)); - } + // Create a core default file source + fileSource = std::static_pointer_cast<mbgl::DefaultFileSource>(mbgl::FileSource::getSharedFileSource(resourceOptions)); } FileSource::~FileSource() { @@ -110,10 +117,10 @@ FileSource* FileSource::getNativePeer(jni::JNIEnv& env, const jni::Object<FileSo return reinterpret_cast<FileSource *>(jFileSource.Get(env, field)); } -mbgl::DefaultFileSource& FileSource::getDefaultFileSource(jni::JNIEnv& env, const jni::Object<FileSource>& jFileSource) { +mbgl::ResourceOptions FileSource::getSharedResourceOptions(jni::JNIEnv& env, const jni::Object<FileSource>& jFileSource) { FileSource* fileSource = FileSource::getNativePeer(env, jFileSource); assert(fileSource != nullptr); - return *fileSource->fileSource; + return fileSource->resourceOptions; } void FileSource::registerNative(jni::JNIEnv& env) { diff --git a/platform/android/src/file_source.hpp b/platform/android/src/file_source.hpp index 575702120e..3001a5e0f0 100644 --- a/platform/android/src/file_source.hpp +++ b/platform/android/src/file_source.hpp @@ -1,6 +1,7 @@ #pragma once #include <mbgl/storage/default_file_source.hpp> +#include <mbgl/storage/resource_options.hpp> #include "asset_manager.hpp" @@ -49,15 +50,16 @@ public: static FileSource* getNativePeer(jni::JNIEnv&, const jni::Object<FileSource>&); - static mbgl::DefaultFileSource& getDefaultFileSource(jni::JNIEnv&, const jni::Object<FileSource>&); + static mbgl::ResourceOptions getSharedResourceOptions(jni::JNIEnv&, const jni::Object<FileSource>&); static void registerNative(jni::JNIEnv&); private: const std::string DATABASE_FILE = "/mbgl-offline.db"; optional<int> activationCounter; + mbgl::ResourceOptions resourceOptions; std::unique_ptr<Actor<ResourceTransform>> resourceTransform; - std::unique_ptr<mbgl::DefaultFileSource> fileSource; + std::shared_ptr<mbgl::DefaultFileSource> fileSource; }; diff --git a/platform/android/src/map_renderer.hpp b/platform/android/src/map_renderer.hpp index 3e5b99605e..57265cebb1 100644 --- a/platform/android/src/map_renderer.hpp +++ b/platform/android/src/map_renderer.hpp @@ -1,13 +1,15 @@ #pragma once +#include <mbgl/actor/actor_ref.hpp> #include <mbgl/actor/scheduler.hpp> #include <mbgl/util/image.hpp> +#include <mbgl/util/optional.hpp> #include <memory> +#include <mutex> #include <utility> #include <jni/jni.hpp> -#include <mbgl/storage/default_file_source.hpp> namespace mbgl { diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index 3a4e2014ba..1eb4c56728 100755 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -13,6 +13,7 @@ #include <jni/jni.hpp> +#include <mbgl/map/map.hpp> #include <mbgl/map/map_options.hpp> #include <mbgl/math/minmax.hpp> #include <mbgl/util/constants.hpp> @@ -74,9 +75,6 @@ NativeMapView::NativeMapView(jni::JNIEnv& _env, return; } - // Get native peer for file source - mbgl::FileSource& fileSource = mbgl::android::FileSource::getDefaultFileSource(_env, jFileSource); - // Create a renderer frontend rendererFrontend = std::make_unique<AndroidRendererFrontend>(mapRenderer); @@ -88,10 +86,9 @@ NativeMapView::NativeMapView(jni::JNIEnv& _env, .withCrossSourceCollisions(_crossSourceCollisions); // Create the core map - map = std::make_unique<mbgl::Map>(*rendererFrontend, *this, - mbgl::Size{ static_cast<uint32_t>(width), - static_cast<uint32_t>(height) }, pixelRatio, - fileSource, *threadPool, options); + map = std::make_unique<mbgl::Map>( + *rendererFrontend, *this, mbgl::Size{ static_cast<uint32_t>(width), static_cast<uint32_t>(height) }, + pixelRatio, *threadPool, options, mbgl::android::FileSource::getSharedResourceOptions(_env, jFileSource)); } /** diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp index d695a91ce0..903543e5d1 100755 --- a/platform/android/src/native_map_view.hpp +++ b/platform/android/src/native_map_view.hpp @@ -6,7 +6,6 @@ #include <mbgl/util/noncopyable.hpp> #include <mbgl/util/default_thread_pool.hpp> #include <mbgl/util/run_loop.hpp> -#include <mbgl/storage/default_file_source.hpp> #include <mbgl/storage/network_status.hpp> #include "annotation/marker.hpp" diff --git a/platform/android/src/offline/offline_manager.cpp b/platform/android/src/offline/offline_manager.cpp index 968c33b644..54b1142845 100644 --- a/platform/android/src/offline/offline_manager.cpp +++ b/platform/android/src/offline/offline_manager.cpp @@ -10,20 +10,19 @@ namespace android { // OfflineManager // OfflineManager::OfflineManager(jni::JNIEnv& env, const jni::Object<FileSource>& jFileSource) - : fileSource(mbgl::android::FileSource::getDefaultFileSource(env, jFileSource)) { -} + : fileSource(std::static_pointer_cast<DefaultFileSource>(mbgl::FileSource::getSharedFileSource(FileSource::getSharedResourceOptions(env, jFileSource)))) {} OfflineManager::~OfflineManager() {} void OfflineManager::setOfflineMapboxTileCountLimit(jni::JNIEnv&, jni::jlong limit) { - fileSource.setOfflineMapboxTileCountLimit(limit); + fileSource->setOfflineMapboxTileCountLimit(limit); } void OfflineManager::listOfflineRegions(jni::JNIEnv& env_, const jni::Object<FileSource>& jFileSource_, const jni::Object<ListOfflineRegionsCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); auto globalFilesource = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, jFileSource_); - fileSource.listOfflineRegions([ + fileSource->listOfflineRegions([ //Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the meanwhile callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource)) @@ -59,7 +58,7 @@ void OfflineManager::createOfflineRegion(jni::JNIEnv& env_, auto globalFilesource = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, jFileSource_); // Create region - fileSource.createOfflineRegion(definition, metadata, [ + fileSource->createOfflineRegion(definition, metadata, [ //Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the meanwhile callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource)) @@ -86,7 +85,7 @@ void OfflineManager::mergeOfflineRegions(jni::JNIEnv& env_, const jni::Object<Fi auto globalFilesource = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, jFileSource_); auto path = jni::Make<std::string>(env_, jString_); - fileSource.mergeOfflineRegions(path, [ + fileSource->mergeOfflineRegions(path, [ //Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the meanwhile callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource)) @@ -226,7 +225,7 @@ void OfflineManager::putResourceWithUrl(jni::JNIEnv& env, response.expires = Timestamp(mbgl::Seconds(expires)); } - fileSource.put(resource, response); + fileSource->put(resource, response); } } // namespace android diff --git a/platform/android/src/offline/offline_manager.hpp b/platform/android/src/offline/offline_manager.hpp index f8d57b88da..d0b637b900 100644 --- a/platform/android/src/offline/offline_manager.hpp +++ b/platform/android/src/offline/offline_manager.hpp @@ -1,7 +1,5 @@ #pragma once - -#include <mbgl/storage/default_file_source.hpp> #include <mbgl/storage/offline.hpp> #include <jni/jni.hpp> @@ -10,8 +8,12 @@ #include "offline_region_definition.hpp" #include "../java_types.hpp" +#include <memory> namespace mbgl { + +class DefaultFileSource; + namespace android { class OfflineManager { @@ -85,7 +87,7 @@ public: private: - mbgl::DefaultFileSource& fileSource; + std::shared_ptr<mbgl::DefaultFileSource> fileSource; }; } // namespace android diff --git a/platform/android/src/offline/offline_region.cpp b/platform/android/src/offline/offline_region.cpp index 1cd73a7c76..e0f28631b4 100644 --- a/platform/android/src/offline/offline_region.cpp +++ b/platform/android/src/offline/offline_region.cpp @@ -14,8 +14,8 @@ namespace android { // OfflineRegion // OfflineRegion::OfflineRegion(jni::JNIEnv& env, jni::jlong offlineRegionPtr, const jni::Object<FileSource>& jFileSource) - : region(reinterpret_cast<mbgl::OfflineRegion *>(offlineRegionPtr)), - fileSource(mbgl::android::FileSource::getDefaultFileSource(env, jFileSource)) {} + : region(reinterpret_cast<mbgl::OfflineRegion *>(offlineRegionPtr)) + , fileSource(std::static_pointer_cast<DefaultFileSource>(mbgl::FileSource::getSharedFileSource(FileSource::getSharedResourceOptions(env, jFileSource)))) {} OfflineRegion::~OfflineRegion() {} @@ -62,7 +62,7 @@ void OfflineRegion::setOfflineRegionObserver(jni::JNIEnv& env_, const jni::Objec }; // Set the observer - fileSource.setOfflineRegionObserver(*region, std::make_unique<Observer>(jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback))); + fileSource->setOfflineRegionObserver(*region, std::make_unique<Observer>(jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback))); } void OfflineRegion::setOfflineRegionDownloadState(jni::JNIEnv&, jni::jint jState) { @@ -80,13 +80,13 @@ void OfflineRegion::setOfflineRegionDownloadState(jni::JNIEnv&, jni::jint jState return; } - fileSource.setOfflineRegionDownloadState(*region, state); + fileSource->setOfflineRegionDownloadState(*region, state); } void OfflineRegion::getOfflineRegionStatus(jni::JNIEnv& env_, const jni::Object<OfflineRegionStatusCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource.getOfflineRegionStatus(*region, [ + fileSource->getOfflineRegionStatus(*region, [ //Ensure the object is not gc'd in the meanwhile callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)) ](mbgl::expected<mbgl::OfflineRegionStatus, std::exception_ptr> status) mutable { @@ -104,7 +104,7 @@ void OfflineRegion::getOfflineRegionStatus(jni::JNIEnv& env_, const jni::Object< void OfflineRegion::deleteOfflineRegion(jni::JNIEnv& env_, const jni::Object<OfflineRegionDeleteCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource.deleteOfflineRegion(std::move(*region), [ + fileSource->deleteOfflineRegion(std::move(*region), [ //Ensure the object is not gc'd in the meanwhile callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)) ](std::exception_ptr error) mutable { @@ -123,7 +123,7 @@ void OfflineRegion::updateOfflineRegionMetadata(jni::JNIEnv& env_, const jni::Ar auto metadata = OfflineRegion::metadata(env_, jMetadata); auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource.updateOfflineMetadata(region->getID(), metadata, [ + fileSource->updateOfflineMetadata(region->getID(), metadata, [ //Ensure the object is not gc'd in the meanwhile callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)) ](mbgl::expected<mbgl::OfflineRegionMetadata, std::exception_ptr> data) mutable { diff --git a/platform/android/src/offline/offline_region.hpp b/platform/android/src/offline/offline_region.hpp index 49fa0c8ff8..4618e1abbd 100644 --- a/platform/android/src/offline/offline_region.hpp +++ b/platform/android/src/offline/offline_region.hpp @@ -74,7 +74,7 @@ public: private: std::unique_ptr<mbgl::OfflineRegion> region; - mbgl::DefaultFileSource& fileSource; + std::shared_ptr<mbgl::DefaultFileSource> fileSource; }; } // namespace android diff --git a/platform/android/src/snapshotter/map_snapshotter.cpp b/platform/android/src/snapshotter/map_snapshotter.cpp index 8eb1d02605..47a2781cb5 100644 --- a/platform/android/src/snapshotter/map_snapshotter.cpp +++ b/platform/android/src/snapshotter/map_snapshotter.cpp @@ -37,7 +37,6 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, } jFileSource = FileSource::getNativePeer(_env, _jFileSource); - auto& fileSource = mbgl::android::FileSource::getDefaultFileSource(_env, _jFileSource); auto size = mbgl::Size { static_cast<uint32_t>(width), static_cast<uint32_t>(height) }; optional<mbgl::CameraOptions> cameraOptions; @@ -56,11 +55,10 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, } else { style = std::make_pair(false, jni::Make<std::string>(_env, styleURL)); } - + showLogo = _showLogo; // Create the core snapshotter - snapshotter = std::make_unique<mbgl::MapSnapshotter>(&fileSource, - threadPool, + snapshotter = std::make_unique<mbgl::MapSnapshotter>(threadPool, style, size, pixelRatio, @@ -69,8 +67,8 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, jni::Make<std::string>(_env, _programCacheDir), _localIdeographFontFamily ? jni::Make<std::string>(_env, _localIdeographFontFamily) : - optional<std::string>{}); - + optional<std::string>{}, + mbgl::android::FileSource::getSharedResourceOptions(_env, _jFileSource)); } MapSnapshotter::~MapSnapshotter() = default; diff --git a/platform/darwin/filesource-files.json b/platform/darwin/filesource-files.json index 3ee96f9b95..b2e6fbc9b4 100644 --- a/platform/darwin/filesource-files.json +++ b/platform/darwin/filesource-files.json @@ -4,6 +4,7 @@ "platform/darwin/src/MGLLoggingConfiguration.m", "platform/darwin/src/MGLNetworkConfiguration.m", "platform/darwin/src/http_file_source.mm", + "platform/default/src/mbgl/storage/file_source.cpp", "platform/default/src/mbgl/storage/sqlite3.cpp" ], "public_headers": {}, diff --git a/platform/darwin/src/MGLMapSnapshotter.mm b/platform/darwin/src/MGLMapSnapshotter.mm index 169808f774..00ec45f7ff 100644 --- a/platform/darwin/src/MGLMapSnapshotter.mm +++ b/platform/darwin/src/MGLMapSnapshotter.mm @@ -3,8 +3,10 @@ #import <mbgl/actor/actor.hpp> #import <mbgl/actor/scheduler.hpp> #import <mbgl/util/geo.hpp> +#import <mbgl/map/map_options.hpp> #import <mbgl/map/map_snapshotter.hpp> #import <mbgl/map/camera.hpp> +#import <mbgl/storage/resource_options.hpp> #import <mbgl/storage/default_file_source.hpp> #import <mbgl/util/default_thread_pool.hpp> #import <mbgl/util/string.hpp> @@ -587,7 +589,9 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; _cancelled = NO; _options = options; + auto mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; + _mbglThreadPool = mbgl::sharedThreadPool(); std::string styleURL = std::string([options.styleURL.absoluteString UTF8String]); @@ -619,9 +623,14 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; // App-global configuration MGLRendererConfiguration* config = [MGLRendererConfiguration currentConfiguration]; - + + auto resourceOptions = mbgl::ResourceOptions() + .withCachePath([[MGLOfflineStorage sharedOfflineStorage] mbglCachePath]) + .withAssetPath([NSBundle mainBundle].resourceURL.path.UTF8String); + // Create the snapshotter - _mbglMapSnapshotter = std::make_unique<mbgl::MapSnapshotter>(mbglFileSource.get(), _mbglThreadPool, style, size, pixelRatio, cameraOptions, coordinateBounds, config.cacheDir, config.localFontFamilyName); + _mbglMapSnapshotter = std::make_unique<mbgl::MapSnapshotter>( + _mbglThreadPool, style, size, pixelRatio, cameraOptions, coordinateBounds, config.cacheDir, config.localFontFamilyName, resourceOptions); } @end diff --git a/platform/darwin/src/MGLOfflinePack.mm b/platform/darwin/src/MGLOfflinePack.mm index 913e9c2233..0f2e8180fa 100644 --- a/platform/darwin/src/MGLOfflinePack.mm +++ b/platform/darwin/src/MGLOfflinePack.mm @@ -10,6 +10,7 @@ #import "NSValue+MGLAdditions.h" +#include <mbgl/map/map_options.hpp> #include <mbgl/storage/default_file_source.hpp> const MGLExceptionName MGLInvalidOfflinePackException = @"MGLInvalidOfflinePackException"; @@ -58,6 +59,7 @@ private: @implementation MGLOfflinePack { BOOL _isSuspending; + std::shared_ptr<mbgl::DefaultFileSource> _mbglFileSource; } - (instancetype)init { @@ -74,8 +76,8 @@ private: _mbglOfflineRegion = region; _state = MGLOfflinePackStateUnknown; - auto mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; - mbglFileSource->setOfflineRegionObserver(*_mbglOfflineRegion, std::make_unique<MBGLOfflineRegionObserver>(self)); + _mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; + _mbglFileSource->setOfflineRegionObserver(*_mbglOfflineRegion, std::make_unique<MBGLOfflineRegionObserver>(self)); } return self; } @@ -115,8 +117,7 @@ private: self.state = MGLOfflinePackStateActive; - auto mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; - mbglFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Active); + _mbglFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Active); } - (void)suspend { @@ -128,8 +129,7 @@ private: _isSuspending = YES; } - auto mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; - mbglFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Inactive); + _mbglFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Inactive); } - (void)invalidate { @@ -137,8 +137,7 @@ private: MGLAssert(_state != MGLOfflinePackStateInvalid, @"Cannot invalidate an already invalid offline pack."); self.state = MGLOfflinePackStateInvalid; - auto mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; - mbglFileSource->setOfflineRegionObserver(*self.mbglOfflineRegion, nullptr); + _mbglFileSource->setOfflineRegionObserver(*self.mbglOfflineRegion, nullptr); self.mbglOfflineRegion = nil; } @@ -164,9 +163,8 @@ private: MGLLogInfo(@"Requesting pack progress."); MGLAssertOfflinePackIsValid(); - auto mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; __weak MGLOfflinePack *weakSelf = self; - mbglFileSource->getOfflineRegionStatus(*_mbglOfflineRegion, [&, weakSelf](mbgl::expected<mbgl::OfflineRegionStatus, std::exception_ptr> status) { + _mbglFileSource->getOfflineRegionStatus(*_mbglOfflineRegion, [&, weakSelf](mbgl::expected<mbgl::OfflineRegionStatus, std::exception_ptr> status) { if (status) { mbgl::OfflineRegionStatus checkedStatus = *status; dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/platform/darwin/src/MGLOfflinePack_Private.h b/platform/darwin/src/MGLOfflinePack_Private.h index 8a63152dca..ea3fb2da99 100644 --- a/platform/darwin/src/MGLOfflinePack_Private.h +++ b/platform/darwin/src/MGLOfflinePack_Private.h @@ -1,6 +1,6 @@ #import "MGLOfflinePack.h" -#include <mbgl/storage/default_file_source.hpp> +#include <mbgl/storage/offline.hpp> NS_ASSUME_NONNULL_BEGIN diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index a118916f86..5f3b2c7c20 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -19,6 +19,8 @@ #include <mbgl/actor/actor.hpp> #include <mbgl/actor/scheduler.hpp> +#include <mbgl/storage/default_file_source.hpp> +#include <mbgl/storage/resource_options.hpp> #include <mbgl/storage/resource_transform.hpp> #include <mbgl/util/chrono.hpp> #include <mbgl/util/run_loop.hpp> @@ -44,6 +46,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio @property (nonatomic, strong, readwrite) NSMutableArray<MGLOfflinePack *> *packs; @property (nonatomic) std::shared_ptr<mbgl::DefaultFileSource> mbglFileSource; +@property (nonatomic) std::string mbglCachePath; @property (nonatomic, getter=isPaused) BOOL paused; @end @@ -222,7 +225,10 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio [[NSFileManager defaultManager] moveItemAtPath:subdirectorylessCacheURL.path toPath:cachePath error:NULL]; } - _mbglFileSource = std::make_shared<mbgl::DefaultFileSource>(cachePath.UTF8String, [NSBundle mainBundle].resourceURL.path.UTF8String); + _mbglCachePath = cachePath.UTF8String; + auto options = mbgl::ResourceOptions().withCachePath(_mbglCachePath) + .withAssetPath([NSBundle mainBundle].resourceURL.path.UTF8String); + _mbglFileSource = std::static_pointer_cast<mbgl::DefaultFileSource>(mbgl::FileSource::getSharedFileSource(options)); // Observe for changes to the API base URL (and find out the current one). [[MGLAccountManager sharedManager] addObserver:self diff --git a/platform/darwin/src/MGLOfflineStorage_Private.h b/platform/darwin/src/MGLOfflineStorage_Private.h index c4618684ba..5ac64ea995 100644 --- a/platform/darwin/src/MGLOfflineStorage_Private.h +++ b/platform/darwin/src/MGLOfflineStorage_Private.h @@ -15,6 +15,11 @@ NS_ASSUME_NONNULL_BEGIN */ @property (nonatomic) std::shared_ptr<mbgl::DefaultFileSource> mbglFileSource; +/** + The shared offline cache path. + */ +@property (nonatomic) std::string mbglCachePath; + @end NS_ASSUME_NONNULL_END diff --git a/platform/default/include/mbgl/map/map_snapshotter.hpp b/platform/default/include/mbgl/map/map_snapshotter.hpp index 2deb2b3cda..ccc3ee17f7 100644 --- a/platform/default/include/mbgl/map/map_snapshotter.hpp +++ b/platform/default/include/mbgl/map/map_snapshotter.hpp @@ -18,6 +18,7 @@ struct CameraOptions; class FileSource; class Size; class LatLngBounds; +class ResourceOptions; namespace style { class Style; @@ -25,15 +26,15 @@ class Style; class MapSnapshotter { public: - MapSnapshotter(FileSource* fileSource, - std::shared_ptr<Scheduler> scheduler, + MapSnapshotter(std::shared_ptr<Scheduler> scheduler, const std::pair<bool, std::string> style, const Size&, const float pixelRatio, const optional<CameraOptions> cameraOptions, const optional<LatLngBounds> region, - const optional<std::string> cacheDir = {}, - const optional<std::string> localFontFamily = {}); + const optional<std::string> cacheDir, + const optional<std::string> localFontFamily, + const ResourceOptions&); ~MapSnapshotter(); diff --git a/platform/default/src/mbgl/map/map_snapshotter.cpp b/platform/default/src/mbgl/map/map_snapshotter.cpp index 415ef7befd..cd8482e4ba 100644 --- a/platform/default/src/mbgl/map/map_snapshotter.cpp +++ b/platform/default/src/mbgl/map/map_snapshotter.cpp @@ -5,7 +5,7 @@ #include <mbgl/map/map.hpp> #include <mbgl/map/map_options.hpp> #include <mbgl/map/transform_state.hpp> -#include <mbgl/storage/file_source.hpp> +#include <mbgl/storage/resource_options.hpp> #include <mbgl/style/style.hpp> #include <mbgl/util/event.hpp> #include <mbgl/map/transform.hpp> @@ -14,15 +14,15 @@ namespace mbgl { class MapSnapshotter::Impl { public: - Impl(FileSource*, - std::shared_ptr<Scheduler>, + Impl(std::shared_ptr<Scheduler>, const std::pair<bool, std::string> style, const Size&, const float pixelRatio, const optional<CameraOptions> cameraOptions, const optional<LatLngBounds> region, const optional<std::string> programCacheDir, - const optional<std::string> localFontFamily = {}); + const optional<std::string> localFontFamily, + const ResourceOptions& resourceOptions); void setStyleURL(std::string styleURL); std::string getStyleURL() const; @@ -47,19 +47,18 @@ private: Map map; }; -MapSnapshotter::Impl::Impl(FileSource* fileSource, - std::shared_ptr<Scheduler> scheduler_, - const std::pair<bool, std::string> style, - const Size& size, - const float pixelRatio, - const optional<CameraOptions> cameraOptions, - const optional<LatLngBounds> region, - const optional<std::string> programCacheDir, - const optional<std::string> localFontFamily) - : scheduler(std::move(scheduler_)) - , frontend(size, pixelRatio, *scheduler, programCacheDir, GLContextMode::Unique, localFontFamily) - , map(frontend, MapObserver::nullObserver(), size, pixelRatio, *fileSource, *scheduler, MapOptions().withMapMode(MapMode::Static)) { - +MapSnapshotter::Impl::Impl(std::shared_ptr<Scheduler> scheduler_, + const std::pair<bool, std::string> style, + const Size& size, + const float pixelRatio, + const optional<CameraOptions> cameraOptions, + const optional<LatLngBounds> region, + const optional<std::string> programCacheDir, + const optional<std::string> localFontFamily, + const ResourceOptions& resourceOptions) + : scheduler(std::move(scheduler_)) + , frontend(size, pixelRatio, *scheduler, programCacheDir, GLContextMode::Unique, localFontFamily) + , map(frontend, MapObserver::nullObserver(), size, pixelRatio, *scheduler, MapOptions().withMapMode(MapMode::Static), resourceOptions) { if (style.first) { map.getStyle().loadJSON(style.second); } else{ @@ -164,17 +163,18 @@ LatLngBounds MapSnapshotter::Impl::getRegion() const { return map.latLngBoundsForCamera(getCameraOptions()); } -MapSnapshotter::MapSnapshotter(FileSource* fileSource, - std::shared_ptr<Scheduler> scheduler, +MapSnapshotter::MapSnapshotter(std::shared_ptr<Scheduler> scheduler, const std::pair<bool, std::string> style, const Size& size, const float pixelRatio, const optional<CameraOptions> cameraOptions, const optional<LatLngBounds> region, const optional<std::string> programCacheDir, - const optional<std::string> localFontFamily) - : impl(std::make_unique<util::Thread<MapSnapshotter::Impl>>("Map Snapshotter", fileSource, std::move(scheduler), style, size, pixelRatio, cameraOptions, region, programCacheDir, localFontFamily)) { -} + const optional<std::string> localFontFamily, + const ResourceOptions& resourceOptions) + : impl(std::make_unique<util::Thread<MapSnapshotter::Impl>>( + "Map Snapshotter", std::move(scheduler), style, size, pixelRatio, cameraOptions, + region, programCacheDir, localFontFamily, resourceOptions)) {} MapSnapshotter::~MapSnapshotter() = default; diff --git a/platform/default/src/mbgl/storage/default_file_source.cpp b/platform/default/src/mbgl/storage/default_file_source.cpp index 32eb8b3d58..4d812044cf 100644 --- a/platform/default/src/mbgl/storage/default_file_source.cpp +++ b/platform/default/src/mbgl/storage/default_file_source.cpp @@ -201,9 +201,9 @@ private: }; DefaultFileSource::DefaultFileSource(const std::string& cachePath, - const std::string& assetRoot, + const std::string& assetPath, uint64_t maximumCacheSize) - : DefaultFileSource(cachePath, std::make_unique<AssetFileSource>(assetRoot), maximumCacheSize) { + : DefaultFileSource(cachePath, std::make_unique<AssetFileSource>(assetPath), maximumCacheSize) { } DefaultFileSource::DefaultFileSource(const std::string& cachePath, diff --git a/platform/default/src/mbgl/storage/file_source.cpp b/platform/default/src/mbgl/storage/file_source.cpp new file mode 100644 index 0000000000..a7bbe82f5a --- /dev/null +++ b/platform/default/src/mbgl/storage/file_source.cpp @@ -0,0 +1,15 @@ +#include <mbgl/storage/resource_options.hpp> +#include <mbgl/storage/default_file_source.hpp> + +#include <memory> + +namespace mbgl { + +std::shared_ptr<FileSource> FileSource::createPlatformFileSource(const ResourceOptions& options) { + auto fileSource = std::make_shared<DefaultFileSource>(options.cachePath(), options.assetPath()); + fileSource->setAccessToken(options.accessToken()); + fileSource->setAPIBaseURL(options.baseURL()); + return fileSource; +} + +} // namespace mbgl diff --git a/platform/glfw/main.cpp b/platform/glfw/main.cpp index 38a41cbf18..8288a009f2 100644 --- a/platform/glfw/main.cpp +++ b/platform/glfw/main.cpp @@ -93,23 +93,22 @@ int main(int argc, char *argv[]) { GLFWView backend(fullscreen, benchmark); view = &backend; - mbgl::DefaultFileSource fileSource(cacheDB, "."); - if (!settings.online) { - fileSource.setOnlineStatus(false); - mbgl::Log::Warning(mbgl::Event::Setup, "Application is offline. Press `O` to toggle online status."); - } - // Set access token if present const char *token = getenv("MAPBOX_ACCESS_TOKEN"); if (token == nullptr) { mbgl::Log::Warning(mbgl::Event::Setup, "no access token set. mapbox.com tiles won't work."); - } else { - fileSource.setAccessToken(std::string(token)); + } + + auto resourceOptions = mbgl::ResourceOptions().withCachePath(cacheDB).withAssetPath(".").withAccessToken(std::string(token)); + auto fileSource = std::static_pointer_cast<mbgl::DefaultFileSource>(mbgl::FileSource::getSharedFileSource(resourceOptions)); + if (!settings.online) { + fileSource->setOnlineStatus(false); + mbgl::Log::Warning(mbgl::Event::Setup, "Application is offline. Press `O` to toggle online status."); } mbgl::ThreadPool threadPool(4); GLFWRendererFrontend rendererFrontend { std::make_unique<mbgl::Renderer>(backend, view->getPixelRatio(), threadPool), backend }; - mbgl::Map map(rendererFrontend, backend, view->getSize(), view->getPixelRatio(), fileSource, threadPool, mbgl::MapOptions()); + mbgl::Map map(rendererFrontend, backend, view->getSize(), view->getPixelRatio(), threadPool, mbgl::MapOptions(), resourceOptions); backend.setMap(&map); @@ -124,9 +123,9 @@ int main(int argc, char *argv[]) { .withPitch(settings.pitch)); map.setDebug(mbgl::MapDebugOptions(settings.debug)); - view->setOnlineStatusCallback([&settings, &fileSource]() { + view->setOnlineStatusCallback([&settings, fileSource]() { settings.online = !settings.online; - fileSource.setOnlineStatus(settings.online); + fileSource->setOnlineStatus(settings.online); mbgl::Log::Info(mbgl::Event::Setup, "Application is %s. Press `O` to toggle online status.", settings.online ? "online" : "offline"); }); @@ -144,13 +143,13 @@ int main(int argc, char *argv[]) { mbgl::Log::Info(mbgl::Event::Setup, "Changed style to: %s", newStyle.name); }); - view->setPauseResumeCallback([&fileSource] () { + view->setPauseResumeCallback([fileSource] () { static bool isPaused = false; if (isPaused) { - fileSource.resume(); + fileSource->resume(); } else { - fileSource.pause(); + fileSource->pause(); } isPaused = !isPaused; diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 2065f92f64..94d56dfff9 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -11,7 +11,7 @@ #include <mbgl/util/platform.hpp> #include <mbgl/storage/reachability.h> #include <mbgl/util/default_thread_pool.hpp> -#include <mbgl/storage/default_file_source.hpp> +#include <mbgl/storage/resource_options.hpp> #include <mbgl/storage/network_status.hpp> #include <mbgl/style/style.hpp> #include <mbgl/style/image.hpp> @@ -477,14 +477,18 @@ public: BOOL enableCrossSourceCollisions = !config.perSourceCollisions; _rendererFrontend = std::make_unique<MGLRenderFrontend>(std::move(renderer), self, *_mbglView); - mbgl::MapOptions mapOptions; - mapOptions.withMapMode(mbgl::MapMode::Continuous) - .withConstrainMode(mbgl::ConstrainMode::None) - .withViewportMode(mbgl::ViewportMode::Default) - .withCrossSourceCollisions(enableCrossSourceCollisions); + auto mapOptions = mbgl::MapOptions() + .withMapMode(mbgl::MapMode::Continuous) + .withConstrainMode(mbgl::ConstrainMode::None) + .withViewportMode(mbgl::ViewportMode::Default) + .withCrossSourceCollisions(enableCrossSourceCollisions); + + auto resourceOptions = mbgl::ResourceOptions() + .withCachePath([[MGLOfflineStorage sharedOfflineStorage] mbglCachePath]) + .withAssetPath([NSBundle mainBundle].resourceURL.path.UTF8String); NSAssert(!_mbglMap, @"_mbglMap should be NULL"); - _mbglMap = new mbgl::Map(*_rendererFrontend, *_mbglView, self.size, config.scaleFactor, *[[MGLOfflineStorage sharedOfflineStorage] mbglFileSource], *_mbglThreadPool, mapOptions); + _mbglMap = new mbgl::Map(*_rendererFrontend, *_mbglView, self.size, config.scaleFactor, *_mbglThreadPool, mapOptions, resourceOptions); // start paused if in IB if (_isTargetingInterfaceBuilder || background) { diff --git a/platform/linux/config.cmake b/platform/linux/config.cmake index 6d4715e451..4405e0583f 100644 --- a/platform/linux/config.cmake +++ b/platform/linux/config.cmake @@ -104,13 +104,8 @@ endmacro() macro(mbgl_filesource) - target_sources(mbgl-filesource - # File source - PRIVATE platform/default/src/mbgl/storage/http_file_source.cpp - - # Database - PRIVATE platform/default/src/mbgl/storage/sqlite3.cpp - ) + # Modify platform/linux/filesource-files.json to change the source files for this target. + target_sources_from_file(mbgl-filesource PRIVATE platform/linux/filesource-files.json) # We're not referencing any cURL symbols since we're dynamically loading it. However, we want to # link the library anyway since we're definitely going to load it on startup anyway. diff --git a/platform/linux/filesource-files.json b/platform/linux/filesource-files.json new file mode 100644 index 0000000000..448f5f8613 --- /dev/null +++ b/platform/linux/filesource-files.json @@ -0,0 +1,10 @@ +{ + "//": "This file can be edited manually and is the canonical source.", + "sources": [ + "platform/default/src/mbgl/storage/file_source.cpp", + "platform/default/src/mbgl/storage/http_file_source.cpp", + "platform/default/src/mbgl/storage/sqlite3.cpp" + ], + "public_headers": {}, + "private_headers": {} +} diff --git a/platform/macos/src/MGLMapView.mm b/platform/macos/src/MGLMapView.mm index 5b2fae90ab..74189c52f4 100644 --- a/platform/macos/src/MGLMapView.mm +++ b/platform/macos/src/MGLMapView.mm @@ -36,8 +36,8 @@ #import <mbgl/renderer/renderer.hpp> #import <mbgl/renderer/renderer_backend.hpp> #import <mbgl/renderer/backend_scope.hpp> -#import <mbgl/storage/default_file_source.hpp> #import <mbgl/storage/network_status.hpp> +#import <mbgl/storage/resource_options.hpp> #import <mbgl/math/wrap.hpp> #import <mbgl/util/constants.hpp> #import <mbgl/util/chrono.hpp> @@ -290,12 +290,17 @@ public: BOOL enableCrossSourceCollisions = !config.perSourceCollisions; _rendererFrontend = std::make_unique<MGLRenderFrontend>(std::move(renderer), self, *_mbglView, true); - mbgl::MapOptions mapOptions; - mapOptions.withMapMode(mbgl::MapMode::Continuous) - .withConstrainMode(mbgl::ConstrainMode::None) - .withViewportMode(mbgl::ViewportMode::Default) - .withCrossSourceCollisions(enableCrossSourceCollisions); - _mbglMap = new mbgl::Map(*_rendererFrontend, *_mbglView, self.size, config.scaleFactor, *[[MGLOfflineStorage sharedOfflineStorage] mbglFileSource], *_mbglThreadPool, mapOptions); + auto mapOptions = mbgl::MapOptions() + .withMapMode(mbgl::MapMode::Continuous) + .withConstrainMode(mbgl::ConstrainMode::None) + .withViewportMode(mbgl::ViewportMode::Default) + .withCrossSourceCollisions(enableCrossSourceCollisions); + + auto resourceOptions = mbgl::ResourceOptions() + .withCachePath([[MGLOfflineStorage sharedOfflineStorage] mbglCachePath]) + .withAssetPath([NSBundle mainBundle].resourceURL.path.UTF8String); + + _mbglMap = new mbgl::Map(*_rendererFrontend, *_mbglView, self.size, config.scaleFactor, *_mbglThreadPool, mapOptions, resourceOptions); // Install the OpenGL layer. Interface Builder’s synchronous drawing means // we can’t display a map, so don’t even bother to have a map layer. diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index 0d69e606c1..0b27d378a7 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -20,15 +20,25 @@ #include <mbgl/style/layers/raster_layer.hpp> #include <mbgl/style/layers/symbol_layer.hpp> +#include <mbgl/storage/resource_options.hpp> #include <mbgl/style/style.hpp> #include <mbgl/style/image.hpp> #include <mbgl/style/light.hpp> +#include <mbgl/map/map.hpp> #include <mbgl/map/map_observer.hpp> #include <mbgl/util/exception.hpp> #include <mbgl/util/premultiply.hpp> #include <unistd.h> +namespace mbgl { + +std::shared_ptr<FileSource> FileSource::createPlatformFileSource(const ResourceOptions& options) { + return std::make_shared<node_mbgl::NodeFileSource>(reinterpret_cast<node_mbgl::NodeMap*>(options.platformContext())); +} + +} // namespace mbgl + namespace node_mbgl { struct NodeMap::RenderOptions { @@ -621,13 +631,14 @@ void NodeMap::cancel() { }); frontend = std::make_unique<mbgl::HeadlessFrontend>(mbgl::Size{ 256, 256 }, pixelRatio, threadpool); - mbgl::MapOptions options; - options.withMapMode(mode) - .withConstrainMode(mbgl::ConstrainMode::HeightOnly) - .withViewportMode(mbgl::ViewportMode::Default) - .withCrossSourceCollisions(crossSourceCollisions); + auto mapOptions = mbgl::MapOptions() + .withMapMode(mode) + .withCrossSourceCollisions(crossSourceCollisions); + + auto resourceOptions = mbgl::ResourceOptions().withPlatformContext(reinterpret_cast<void*>(this)); + map = std::make_unique<mbgl::Map>(*frontend, mapObserver, frontend->getSize(), pixelRatio, - fileSource, threadpool, options); + threadpool, mapOptions, resourceOptions); // FIXME: Reload the style after recreating the map. We need to find // a better way of canceling an ongoing rendering on the core level @@ -1205,20 +1216,11 @@ NodeMap::NodeMap(v8::Local<v8::Object> options) : true; }()) , mapObserver(NodeMapObserver()) - , fileSource(this) , frontend(std::make_unique<mbgl::HeadlessFrontend>(mbgl::Size { 256, 256 }, pixelRatio, threadpool)) - , map(std::make_unique<mbgl::Map>(*frontend, - mapObserver, - frontend->getSize(), - pixelRatio, - fileSource, - threadpool, - mbgl::MapOptions().withMapMode(mode) - .withConstrainMode(mbgl::ConstrainMode::HeightOnly) - .withViewportMode(mbgl::ViewportMode::Default) - .withCrossSourceCollisions(crossSourceCollisions))), - async(new uv_async_t) { - + , map(std::make_unique<mbgl::Map>(*frontend, mapObserver, frontend->getSize(), pixelRatio, threadpool, + mbgl::MapOptions().withMapMode(mode).withCrossSourceCollisions(crossSourceCollisions), + mbgl::ResourceOptions().withPlatformContext(reinterpret_cast<void*>(this)))) + , async(new uv_async_t) { async->data = this; uv_async_init(uv_default_loop(), async, [](uv_async_t* h) { reinterpret_cast<NodeMap *>(h->data)->renderFinished(); @@ -1232,8 +1234,6 @@ NodeMap::~NodeMap() { if (map) release(); } -NodeFileSource::NodeFileSource(NodeMap* nodeMap_) : nodeMap(nodeMap_) {} - std::unique_ptr<mbgl::AsyncRequest> NodeFileSource::request(const mbgl::Resource& resource, mbgl::FileSource::Callback callback_) { assert(nodeMap); @@ -1248,15 +1248,15 @@ std::unique_ptr<mbgl::AsyncRequest> NodeFileSource::request(const mbgl::Resource Nan::New<v8::External>(&callback_) }; - auto instance = Nan::NewInstance(Nan::New(NodeRequest::constructor), 2, argv).ToLocalChecked(); + auto instance = Nan::NewInstance(Nan::New(node_mbgl::NodeRequest::constructor), 2, argv).ToLocalChecked(); Nan::Set(instance, Nan::New("url").ToLocalChecked(), Nan::New(resource.url).ToLocalChecked()); Nan::Set(instance, Nan::New("kind").ToLocalChecked(), Nan::New<v8::Integer>(resource.kind)); - auto request = Nan::ObjectWrap::Unwrap<NodeRequest>(instance); + auto request = Nan::ObjectWrap::Unwrap<node_mbgl::NodeRequest>(instance); request->Execute(); - return std::make_unique<NodeRequest::NodeAsyncRequest>(request); + return std::make_unique<node_mbgl::NodeRequest::NodeAsyncRequest>(request); } } // namespace node_mbgl diff --git a/platform/node/src/node_map.hpp b/platform/node/src/node_map.hpp index 9e3eb1ad12..65664b34bb 100644 --- a/platform/node/src/node_map.hpp +++ b/platform/node/src/node_map.hpp @@ -15,26 +15,13 @@ #pragma GCC diagnostic pop namespace mbgl { -class Map; class HeadlessFrontend; } // namespace mbgl namespace node_mbgl { -class NodeMapObserver : public mbgl::MapObserver { - void onDidFailLoadingMap(mbgl::MapLoadError, const std::string&) override; -}; - -class NodeMap; - -class NodeFileSource : public mbgl::FileSource { -public: - NodeFileSource(NodeMap*); - - std::unique_ptr<mbgl::AsyncRequest> request(const mbgl::Resource&, mbgl::FileSource::Callback) final; - -private: - NodeMap* nodeMap; +struct NodeMapObserver : public mbgl::MapObserver { + void onDidFailLoadingMap(mbgl::MapLoadError, const std::string&) final; }; class RenderRequest; @@ -94,7 +81,6 @@ public: bool crossSourceCollisions; NodeThreadPool threadpool; NodeMapObserver mapObserver; - NodeFileSource fileSource; std::unique_ptr<mbgl::HeadlessFrontend> frontend; std::unique_ptr<mbgl::Map> map; @@ -108,4 +94,10 @@ public: bool loaded = false; }; +struct NodeFileSource : public mbgl::FileSource { + NodeFileSource(NodeMap* nodeMap_) : nodeMap(nodeMap_) {} + std::unique_ptr<mbgl::AsyncRequest> request(const mbgl::Resource&, mbgl::FileSource::Callback) final; + NodeMap* nodeMap; +}; + } // namespace node_mbgl diff --git a/platform/qt/qt.cmake b/platform/qt/qt.cmake index 14f09007ac..911af25bc4 100644 --- a/platform/qt/qt.cmake +++ b/platform/qt/qt.cmake @@ -50,6 +50,7 @@ set(MBGL_QT_CORE_FILES set(MBGL_QT_FILESOURCE_FILES # File source + PRIVATE platform/default/src/mbgl/storage/file_source.cpp PRIVATE platform/qt/src/http_file_source.cpp PRIVATE platform/qt/src/http_file_source.hpp PRIVATE platform/qt/src/http_request.cpp diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index b05c82a783..1feeecdd7c 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -10,6 +10,7 @@ #include <mbgl/annotation/annotation.hpp> #include <mbgl/map/camera.hpp> #include <mbgl/map/map.hpp> +#include <mbgl/map/map_options.hpp> #include <mbgl/math/log2.hpp> #include <mbgl/math/minmax.hpp> #include <mbgl/style/style.hpp> @@ -33,7 +34,9 @@ #include <mbgl/style/image.hpp> #include <mbgl/renderer/renderer.hpp> #include <mbgl/renderer/backend_scope.hpp> +#include <mbgl/storage/default_file_source.hpp> #include <mbgl/storage/network_status.hpp> +#include <mbgl/storage/resource_options.hpp> #include <mbgl/util/color.hpp> #include <mbgl/util/constants.hpp> #include <mbgl/util/geo.hpp> @@ -91,49 +94,6 @@ namespace { QThreadStorage<std::shared_ptr<mbgl::util::RunLoop>> loop; -std::shared_ptr<mbgl::DefaultFileSource> sharedDefaultFileSource(const QMapboxGLSettings &settings) { - static std::mutex mutex; - static std::unordered_map<std::string, std::weak_ptr<mbgl::DefaultFileSource>> fileSources; - - const std::string cachePath = settings.cacheDatabasePath().toStdString(); - const std::string accessToken = settings.accessToken().toStdString(); - const std::string apiBaseUrl = settings.apiBaseUrl().toStdString(); - - std::lock_guard<std::mutex> lock(mutex); - - // Purge entries no longer in use. - for (auto it = fileSources.begin(); it != fileSources.end();) { - if (it->second.expired()) { - it = fileSources.erase(it); - } else { - ++it; - } - } - - const auto key = cachePath + "|" + accessToken + "|" + apiBaseUrl; - - // Return an existing FileSource if available. - auto sharedFileSource = fileSources.find(key); - if (sharedFileSource != fileSources.end()) { - auto lockedSharedFileSource = sharedFileSource->second.lock(); - if (lockedSharedFileSource) { - return lockedSharedFileSource; - } - } - - // New path, create a new FileSource. - auto newFileSource = std::make_shared<mbgl::DefaultFileSource>( - cachePath, settings.assetPath().toStdString(), settings.cacheDatabaseMaximumSize()); - - // Setup the FileSource - newFileSource->setAccessToken(accessToken); - newFileSource->setAPIBaseURL(apiBaseUrl); - - fileSources[key] = newFileSource; - - return newFileSource; -} - // Conversion helper functions. mbgl::Size sanitizedSize(const QSize& size) { @@ -1745,22 +1705,29 @@ void QMapboxGL::connectionEstablished() \a copyrightsHtml is a string with a HTML snippet. */ +mbgl::MapOptions mapOptionsFromQMapboxGLSettings(const QMapboxGLSettings &settings) { + return mbgl::MapOptions() + .withMapMode(static_cast<mbgl::MapMode>(settings.mapMode())) + .withConstrainMode(static_cast<mbgl::ConstrainMode>(settings.constrainMode())) + .withViewportMode(static_cast<mbgl::ViewportMode>(settings.viewportMode())); +} + +mbgl::ResourceOptions resourceOptionsFromQMapboxGLSettings(const QMapboxGLSettings &settings) { + return mbgl::ResourceOptions() + .withAccessToken(settings.accessToken().toStdString()) + .withAssetPath(settings.assetPath().toStdString()) + .withBaseURL(settings.apiBaseUrl().toStdString()) + .withCachePath(settings.cacheDatabasePath().toStdString()) + .withMaximumCacheSize(settings.cacheDatabaseMaximumSize()); +} + QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settings, const QSize &size, qreal pixelRatio_) : QObject(q) - , m_fileSourceObj(sharedDefaultFileSource(settings)) , m_threadPool(mbgl::sharedThreadPool()) , m_mode(settings.contextMode()) , m_pixelRatio(pixelRatio_) , m_localFontFamily(settings.localFontFamily()) { - if (settings.resourceTransform()) { - m_resourceTransform = std::make_unique<mbgl::Actor<mbgl::ResourceTransform>>(*mbgl::Scheduler::GetCurrent(), - [callback = settings.resourceTransform()] (mbgl::Resource::Kind, const std::string &&url_) -> std::string { - return callback(std::move(url_)); - }); - m_fileSourceObj->setResourceTransform(m_resourceTransform->self()); - } - // Setup MapObserver m_mapObserver = std::make_unique<QMapboxGLMapObserver>(this); @@ -1770,18 +1737,20 @@ QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settin connect(m_mapObserver.get(), SIGNAL(mapLoadingFailed(QMapboxGL::MapLoadingFailure,QString)), q, SIGNAL(mapLoadingFailed(QMapboxGL::MapLoadingFailure,QString))); connect(m_mapObserver.get(), SIGNAL(copyrightsChanged(QString)), q, SIGNAL(copyrightsChanged(QString))); - mbgl::MapOptions options; - options.withMapMode(static_cast<mbgl::MapMode>(settings.mapMode())) - .withConstrainMode(static_cast<mbgl::ConstrainMode>(settings.constrainMode())) - .withViewportMode(static_cast<mbgl::ViewportMode>(settings.viewportMode())); - - // Setup the Map object - mapObj = std::make_unique<mbgl::Map>( - *this, // RendererFrontend - *m_mapObserver, - sanitizedSize(size), - m_pixelRatio, *m_fileSourceObj, *m_threadPool, - options); + auto resourceOptions = resourceOptionsFromQMapboxGLSettings(settings); + + // Setup the Map object. + mapObj = std::make_unique<mbgl::Map>(*this, *m_mapObserver, sanitizedSize(size), m_pixelRatio, *m_threadPool, + mapOptionsFromQMapboxGLSettings(settings), resourceOptions); + + if (settings.resourceTransform()) { + m_resourceTransform = std::make_unique<mbgl::Actor<mbgl::ResourceTransform>>(*mbgl::Scheduler::GetCurrent(), + [callback = settings.resourceTransform()] (mbgl::Resource::Kind, const std::string &&url_) -> std::string { + return callback(std::move(url_)); + }); + auto fs = mbgl::FileSource::getSharedFileSource(resourceOptions); + std::static_pointer_cast<mbgl::DefaultFileSource>(fs)->setResourceTransform(m_resourceTransform->self()); + } // Needs to be Queued to give time to discard redundant draw calls via the `renderQueued` flag. connect(this, SIGNAL(needsRendering()), q, SIGNAL(needsRendering()), Qt::QueuedConnection); diff --git a/platform/qt/src/qmapboxgl_p.hpp b/platform/qt/src/qmapboxgl_p.hpp index 7157df0aba..6a5b5ce04f 100644 --- a/platform/qt/src/qmapboxgl_p.hpp +++ b/platform/qt/src/qmapboxgl_p.hpp @@ -7,7 +7,6 @@ #include <mbgl/actor/actor.hpp> #include <mbgl/map/map.hpp> #include <mbgl/renderer/renderer_frontend.hpp> -#include <mbgl/storage/default_file_source.hpp> #include <mbgl/storage/resource_transform.hpp> #include <mbgl/util/default_thread_pool.hpp> #include <mbgl/util/geo.hpp> @@ -57,7 +56,6 @@ private: std::shared_ptr<mbgl::UpdateParameters> m_updateParameters; std::unique_ptr<QMapboxGLMapObserver> m_mapObserver; - std::shared_ptr<mbgl::DefaultFileSource> m_fileSourceObj; std::shared_ptr<mbgl::ThreadPool> m_threadPool; std::unique_ptr<QMapboxGLMapRenderer> m_mapRenderer; std::unique_ptr<mbgl::Actor<mbgl::ResourceTransform>> m_resourceTransform; |