Skip to content

Commit

Permalink
Added the mbtiles file source for rendering vector tiles from file st…
Browse files Browse the repository at this point in the history
…ored locally on the device.
  • Loading branch information
petr-pokorny-1 committed Jan 6, 2021
1 parent 943e7c2 commit 2f67fd3
Show file tree
Hide file tree
Showing 14 changed files with 406 additions and 6 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@
/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/developer-config.xml
/platform/android/local.properties
/platform/android/gradle/configuration.gradle
/platform/android/arm64-v8a
/platform/android/armeabi-v7a
/platform/android/MapboxGLAndroidSDK/.cxx
/platform/android/MapboxGLAndroidSDK/build
/platform/android/MapboxGLAndroidSDKTestApp/.cxx
/platform/android/MapboxGLAndroidSDKTestApp/build
/platform/android/.gradle
/platform/android/x86
/platform/android/x86_64
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ add_library(
${PROJECT_SOURCE_DIR}/src/mbgl/sprite/sprite_parser.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/sprite/sprite_parser.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/storage/asset_file_source.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/storage/mbtiles_file_source.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/storage/file_source_manager.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/storage/http_file_source.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/storage/local_file_source.hpp
Expand Down
1 change: 1 addition & 0 deletions include/mbgl/storage/file_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum FileSourceType : uint8_t {
Database,
FileSystem,
Network,
Mbtiles,
// Resource loader acts as a proxy and has logic
// for request delegation to Asset, Cache, and other
// file sources.
Expand Down
1 change: 1 addition & 0 deletions include/mbgl/util/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ constexpr int DEFAULT_RATE_LIMIT_TIMEOUT = 5;
constexpr const char* API_BASE_URL = "https://api.mapbox.com";
constexpr const char* ASSET_PROTOCOL = "asset://";
constexpr const char* FILE_PROTOCOL = "file://";
constexpr const char* MBTILES_PROTOCOL = "mbtiles://";
constexpr uint32_t DEFAULT_MAXIMUM_CONCURRENT_REQUESTS = 20;

constexpr uint8_t TERRAIN_RGB_MAXZOOM = 15;
Expand Down
4 changes: 4 additions & 0 deletions platform/android/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Mapbox welcomes participation and contributions from everyone. Please read [`Contributing Guide`](https://github.com/mapbox/mapbox-gl-native/blob/master/CONTRIBUTING.md) to get started.

## 9.3.0 - January 6, 2021
### Features
- Added the mbtiles file source for rendering vector tiles from file stored locally on the device.

## 9.2.1 - May 15, 2020
### Improvements and bug fixes
- Added compiler and linker options to reduce binary size for release build. ([#382](https://github.com/mapbox/mapbox-gl-native-android/pull/382))
Expand Down
1 change: 1 addition & 0 deletions platform/android/android.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ target_sources(
${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/file_source_request.cpp
${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/local_file_request.cpp
${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/local_file_source.cpp
${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/mbtiles_file_source.cpp
${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/main_resource_loader.cpp
${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/offline.cpp
${PROJECT_SOURCE_DIR}/platform/default/src/mbgl/storage/offline_database.cpp
Expand Down
4 changes: 4 additions & 0 deletions platform/default/src/mbgl/storage/file_source_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <mbgl/storage/local_file_source.hpp>
#include <mbgl/storage/main_resource_loader.hpp>
#include <mbgl/storage/online_file_source.hpp>
#include <mbgl/storage/mbtiles_file_source.hpp>
#include <mbgl/storage/resource_options.hpp>

namespace mbgl {
Expand All @@ -26,6 +27,9 @@ class DefaultFileSourceManagerImpl final : public FileSourceManager {
registerFileSourceFactory(FileSourceType::FileSystem,
[](const ResourceOptions&) { return std::make_unique<LocalFileSource>(); });

registerFileSourceFactory(FileSourceType::Mbtiles,
[](const ResourceOptions&) { return std::make_unique<MaptilerFileSource>(); });

registerFileSourceFactory(FileSourceType::Network, [](const ResourceOptions& options) {
std::unique_ptr<FileSource> networkSource = std::make_unique<OnlineFileSource>();
networkSource->setProperty(ACCESS_TOKEN_KEY, options.accessToken());
Expand Down
24 changes: 18 additions & 6 deletions platform/default/src/mbgl/storage/main_resource_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ class MainResourceLoaderThread {
MainResourceLoaderThread(std::shared_ptr<FileSource> assetFileSource_,
std::shared_ptr<FileSource> databaseFileSource_,
std::shared_ptr<FileSource> localFileSource_,
std::shared_ptr<FileSource> onlineFileSource_)
std::shared_ptr<FileSource> onlineFileSource_,
std::shared_ptr<FileSource> maptilerFileSource_)
: assetFileSource(std::move(assetFileSource_)),
databaseFileSource(std::move(databaseFileSource_)),
localFileSource(std::move(localFileSource_)),
onlineFileSource(std::move(onlineFileSource_)) {}
onlineFileSource(std::move(onlineFileSource_)),
maptilerFileSource(std::move(maptilerFileSource_)) {}

void request(AsyncRequest* req, const Resource& resource, const ActorRef<FileSourceRequest>& ref) {
auto callback = [ref](const Response& res) { ref.invoke(&FileSourceRequest::setResponse, res); };
Expand Down Expand Up @@ -63,6 +65,9 @@ class MainResourceLoaderThread {
if (assetFileSource && assetFileSource->canRequest(resource)) {
// Asset request
tasks[req] = assetFileSource->request(resource, callback);
} else if (maptilerFileSource && maptilerFileSource->canRequest(resource)) {
// Local file request
tasks[req] = maptilerFileSource->request(resource, callback);
} else if (localFileSource && localFileSource->canRequest(resource)) {
// Local file request
tasks[req] = localFileSource->request(resource, callback);
Expand Down Expand Up @@ -122,6 +127,7 @@ class MainResourceLoaderThread {
const std::shared_ptr<FileSource> databaseFileSource;
const std::shared_ptr<FileSource> localFileSource;
const std::shared_ptr<FileSource> onlineFileSource;
const std::shared_ptr<FileSource> maptilerFileSource;
std::map<AsyncRequest*, std::unique_ptr<AsyncRequest>> tasks;
};

Expand All @@ -130,19 +136,22 @@ class MainResourceLoader::Impl {
Impl(std::shared_ptr<FileSource> assetFileSource_,
std::shared_ptr<FileSource> databaseFileSource_,
std::shared_ptr<FileSource> localFileSource_,
std::shared_ptr<FileSource> onlineFileSource_)
std::shared_ptr<FileSource> onlineFileSource_,
std::shared_ptr<FileSource> maptilerFileSource_)
: assetFileSource(std::move(assetFileSource_)),
databaseFileSource(std::move(databaseFileSource_)),
localFileSource(std::move(localFileSource_)),
onlineFileSource(std::move(onlineFileSource_)),
maptilerFileSource(std::move(maptilerFileSource_)),
supportsCacheOnlyRequests_(bool(databaseFileSource)),
thread(std::make_unique<util::Thread<MainResourceLoaderThread>>(
util::makeThreadPrioritySetter(platform::EXPERIMENTAL_THREAD_PRIORITY_WORKER),
"ResourceLoaderThread",
assetFileSource,
databaseFileSource,
localFileSource,
onlineFileSource)) {}
onlineFileSource,
maptilerFileSource)) {}

std::unique_ptr<AsyncRequest> request(const Resource& resource, Callback callback) {
auto req = std::make_unique<FileSourceRequest>(std::move(callback));
Expand All @@ -158,7 +167,8 @@ class MainResourceLoader::Impl {
return (assetFileSource && assetFileSource->canRequest(resource)) ||
(localFileSource && localFileSource->canRequest(resource)) ||
(databaseFileSource && databaseFileSource->canRequest(resource)) ||
(onlineFileSource && onlineFileSource->canRequest(resource));
(onlineFileSource && onlineFileSource->canRequest(resource)) ||
(maptilerFileSource && maptilerFileSource->canRequest(resource));
}

bool supportsCacheOnlyRequests() const { return supportsCacheOnlyRequests_; }
Expand All @@ -172,6 +182,7 @@ class MainResourceLoader::Impl {
const std::shared_ptr<FileSource> databaseFileSource;
const std::shared_ptr<FileSource> localFileSource;
const std::shared_ptr<FileSource> onlineFileSource;
const std::shared_ptr<FileSource> maptilerFileSource;
const bool supportsCacheOnlyRequests_;
const std::unique_ptr<util::Thread<MainResourceLoaderThread>> thread;
};
Expand All @@ -180,7 +191,8 @@ MainResourceLoader::MainResourceLoader(const ResourceOptions& options)
: impl(std::make_unique<Impl>(FileSourceManager::get()->getFileSource(FileSourceType::Asset, options),
FileSourceManager::get()->getFileSource(FileSourceType::Database, options),
FileSourceManager::get()->getFileSource(FileSourceType::FileSystem, options),
FileSourceManager::get()->getFileSource(FileSourceType::Network, options))) {}
FileSourceManager::get()->getFileSource(FileSourceType::Network, options),
FileSourceManager::get()->getFileSource(FileSourceType::Mbtiles, options))) {}

MainResourceLoader::~MainResourceLoader() = default;

Expand Down
Loading

0 comments on commit 2f67fd3

Please sign in to comment.