summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Shalamov <alexander.shalamov@mapbox.com>2020-02-03 14:43:19 +0200
committerAlexander Shalamov <alexander.shalamov@mapbox.com>2020-02-03 22:26:58 +0200
commit7abfd587d6793f6ca9322bc854717098eb61f940 (patch)
treee8878ef99a17b7d19a1f696af2be0132d22931ae
parent1f5c6beb64805180a4b4724bc1e5b758b9ae4494 (diff)
downloadqtlocation-mapboxgl-upstream/alexhalamov_tracing_delegate_example.tar.gz
-rw-r--r--next/platform/linux/linux.cmake2
-rw-r--r--platform/custom/file_source_manager.cpp100
2 files changed, 101 insertions, 1 deletions
diff --git a/next/platform/linux/linux.cmake b/next/platform/linux/linux.cmake
index 6f2ac12e49..1d799570a3 100644
--- a/next/platform/linux/linux.cmake
+++ b/next/platform/linux/linux.cmake
@@ -20,7 +20,7 @@ target_sources(
${MBGL_ROOT}/platform/default/src/mbgl/layermanager/layer_manager.cpp
${MBGL_ROOT}/platform/default/src/mbgl/storage/asset_file_source.cpp
${MBGL_ROOT}/platform/default/src/mbgl/storage/database_file_source.cpp
- ${MBGL_ROOT}/platform/default/src/mbgl/storage/file_source_manager.cpp
+ ${MBGL_ROOT}/platform/custom/file_source_manager.cpp
${MBGL_ROOT}/platform/default/src/mbgl/storage/file_source_request.cpp
${MBGL_ROOT}/platform/default/src/mbgl/storage/http_file_source.cpp
${MBGL_ROOT}/platform/default/src/mbgl/storage/local_file_request.cpp
diff --git a/platform/custom/file_source_manager.cpp b/platform/custom/file_source_manager.cpp
new file mode 100644
index 0000000000..65d82445cc
--- /dev/null
+++ b/platform/custom/file_source_manager.cpp
@@ -0,0 +1,100 @@
+#include <mbgl/storage/asset_file_source.hpp>
+#include <mbgl/storage/database_file_source.hpp>
+#include <mbgl/storage/file_source_manager.hpp>
+#include <mbgl/storage/local_file_source.hpp>
+#include <mbgl/storage/main_resource_loader.hpp>
+#include <mbgl/storage/online_file_source.hpp>
+#include <mbgl/storage/resource.hpp>
+#include <mbgl/storage/resource_options.hpp>
+#include <mbgl/util/async_request.hpp>
+#include <mbgl/util/logging.hpp>
+
+#define MBGL_TIMING
+#include <mbgl/util/stopwatch.hpp>
+
+namespace mbgl {
+
+std::string toString(Resource::Kind kind) {
+ switch (kind) {
+ case Resource::Unknown:
+ return "unknown";
+ case Resource::Style:
+ return "style";
+ case Resource::Source:
+ return "source";
+ case Resource::Tile:
+ return "tile";
+ case Resource::Glyphs:
+ return "glyphs";
+ case Resource::SpriteImage:
+ return "sprite-image";
+ case Resource::SpriteJSON:
+ return "sprite-json";
+ case Resource::Image:
+ return "image";
+ }
+ return "";
+}
+
+class TracingDelegate final : public FileSource {
+public:
+ TracingDelegate(std::shared_ptr<FileSource> fs_, std::string tag_) : fs(std::move(fs_)), tag(std::move(tag_)) {}
+ ~TracingDelegate() = default;
+
+ std::unique_ptr<AsyncRequest> request(const Resource& resource, Callback callback) override {
+ Log::Debug(Event::Timing,
+ "%s request for '%s' of type '%s'",
+ tag.c_str(),
+ resource.url.c_str(),
+ toString(resource.kind).c_str());
+ MBGL_TIMING_START(watch);
+ return fs->request(resource, [=](Response response) {
+ MBGL_TIMING_FINISH(watch,
+ tag << " response for '" << resource.url << "' of type '" << toString(resource.kind)
+ << "' hasContent(" << (response.noContent ? "false)" : "true)") << " size "
+ << (response.data != nullptr ? response.data->size() : 0) << "B Time");
+ callback(response);
+ });
+ }
+
+ bool canRequest(const Resource& res) const override { return fs->canRequest(res); }
+
+private:
+ std::shared_ptr<FileSource> fs;
+ std::string tag;
+};
+
+class TracingFileSourceManagerImpl final : public FileSourceManager {
+public:
+ TracingFileSourceManagerImpl() {
+ registerFileSourceFactory(FileSourceType::ResourceLoader, [](const ResourceOptions& options) {
+ return std::make_unique<TracingDelegate>(std::make_unique<MainResourceLoader>(options),
+ "MainResourceLoader");
+ });
+
+ registerFileSourceFactory(FileSourceType::Asset, [](const ResourceOptions& options) {
+ return std::make_unique<AssetFileSource>(options.assetPath());
+ });
+
+ registerFileSourceFactory(FileSourceType::Database, [](const ResourceOptions& options) {
+ return std::make_unique<TracingDelegate>(std::make_unique<DatabaseFileSource>(options), "Database");
+ });
+
+ registerFileSourceFactory(FileSourceType::FileSystem,
+ [](const ResourceOptions&) { return std::make_unique<LocalFileSource>(); });
+
+ registerFileSourceFactory(FileSourceType::Network, [](const ResourceOptions& options) {
+ auto networkSource = std::make_unique<OnlineFileSource>();
+ networkSource->setProperty(ACCESS_TOKEN_KEY, options.accessToken());
+ networkSource->setProperty(API_BASE_URL_KEY, options.baseURL());
+ return std::make_unique<TracingDelegate>(std::move(networkSource), "Network");
+ });
+ }
+};
+
+FileSourceManager* FileSourceManager::get() noexcept {
+ static TracingFileSourceManagerImpl instance;
+ return &instance;
+}
+
+} // namespace mbgl