From 1ff317c1cdebc2bb34bb759dea957f8dd9660eb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 5 Jan 2016 15:59:10 +0100 Subject: [PATCH] [core] remove SourceInfo dependency from *TileData --- src/mbgl/map/raster_tile_data.cpp | 5 +---- src/mbgl/map/raster_tile_data.hpp | 6 ++---- src/mbgl/map/source.cpp | 6 +++--- src/mbgl/map/source_info.cpp | 33 ------------------------------- src/mbgl/map/source_info.hpp | 2 -- src/mbgl/map/vector_tile.cpp | 6 ++++-- src/mbgl/map/vector_tile.hpp | 7 ++++--- src/mbgl/util/url.cpp | 24 ++++++++++++++++++++++ src/mbgl/util/url.hpp | 5 +++++ 9 files changed, 43 insertions(+), 51 deletions(-) delete mode 100644 src/mbgl/map/source_info.cpp diff --git a/src/mbgl/map/raster_tile_data.cpp b/src/mbgl/map/raster_tile_data.cpp index 5c85cbd03..a462b86be 100644 --- a/src/mbgl/map/raster_tile_data.cpp +++ b/src/mbgl/map/raster_tile_data.cpp @@ -10,11 +10,9 @@ using namespace mbgl; RasterTileData::RasterTileData(const TileID& id_, TexturePool &texturePool_, - const SourceInfo &source_, Worker& worker_) : TileData(id_), texturePool(texturePool_), - source(source_), worker(worker_) { } @@ -22,9 +20,8 @@ RasterTileData::~RasterTileData() { cancel(); } -void RasterTileData::request(float pixelRatio, +void RasterTileData::request(const std::string& url, const RasterTileData::Callback& callback) { - std::string url = source.tileURL(id, pixelRatio); state = State::loading; FileSource* fs = util::ThreadContext::getFileSource(); diff --git a/src/mbgl/map/raster_tile_data.hpp b/src/mbgl/map/raster_tile_data.hpp index 54330b73a..34a79cad4 100644 --- a/src/mbgl/map/raster_tile_data.hpp +++ b/src/mbgl/map/raster_tile_data.hpp @@ -6,7 +6,6 @@ namespace mbgl { -class SourceInfo; class FileRequest; class StyleLayer; class TexturePool; @@ -14,12 +13,12 @@ class WorkRequest; class RasterTileData : public TileData { public: - RasterTileData(const TileID&, TexturePool&, const SourceInfo&, Worker&); + RasterTileData(const TileID&, TexturePool&, Worker&); ~RasterTileData(); using Callback = std::function; - void request(float pixelRatio, + void request(const std::string& url, const Callback& callback); void cancel() override; @@ -28,7 +27,6 @@ public: private: TexturePool& texturePool; - const SourceInfo& source; Worker& worker; std::unique_ptr req; diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp index 107c405ef..a14e0badd 100644 --- a/src/mbgl/map/source.cpp +++ b/src/mbgl/map/source.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -307,16 +308,15 @@ TileData::State Source::addTile(const TileID& id, const StyleUpdateParameters& p if (info.type == SourceType::Raster) { auto tileData = std::make_shared(normalized_id, parameters.texturePool, - info, parameters.worker); - tileData->request(parameters.pixelRatio, callback); + tileData->request(util::templateTileURL(info.tiles.at(0), normalized_id, parameters.pixelRatio), callback); newTile->data = tileData; } else { std::unique_ptr monitor; if (info.type == SourceType::Vector) { - monitor = std::make_unique(info, normalized_id, parameters.pixelRatio); + monitor = std::make_unique(normalized_id, info.tiles.at(0)); } else if (info.type == SourceType::Annotations) { monitor = std::make_unique(normalized_id, parameters.data); } else if (info.type == SourceType::GeoJSON) { diff --git a/src/mbgl/map/source_info.cpp b/src/mbgl/map/source_info.cpp deleted file mode 100644 index 2980e3ec4..000000000 --- a/src/mbgl/map/source_info.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include -#include -#include -#include - -namespace mbgl { - -std::string SourceInfo::tileURL(const TileID& id, float pixelRatio) const { - std::string result = tiles.at(0); - result = util::mapbox::normalizeTileURL(result, url, type); - result = util::replaceTokens(result, [&](const std::string& token) -> std::string { - if (token == "z") { - return util::toString(std::min(id.z, static_cast(max_zoom))); - } else if (token == "x") { - return util::toString(id.x); - } else if (token == "y") { - return util::toString(id.y); - } else if (token == "prefix") { - std::string prefix{ 2 }; - prefix[0] = "0123456789abcdef"[id.x % 16]; - prefix[1] = "0123456789abcdef"[id.y % 16]; - return prefix; - } else if (token == "ratio") { - return pixelRatio > 1.0 ? "@2x" : ""; - } else { - return ""; - } - }); - return result; -} - -} // namespace mbgl diff --git a/src/mbgl/map/source_info.hpp b/src/mbgl/map/source_info.hpp index 6e52dc8c0..0a72380d6 100644 --- a/src/mbgl/map/source_info.hpp +++ b/src/mbgl/map/source_info.hpp @@ -25,8 +25,6 @@ public: std::array center = { { 0, 0, 0 } }; std::array bounds = { { -180, -90, 180, 90 } }; std::string source_id = ""; - - std::string tileURL(const TileID&, float pixelRatio) const; }; } // namespace mbgl diff --git a/src/mbgl/map/vector_tile.cpp b/src/mbgl/map/vector_tile.cpp index 39e31da0d..c53559d9c 100644 --- a/src/mbgl/map/vector_tile.cpp +++ b/src/mbgl/map/vector_tile.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -177,11 +178,12 @@ util::ptr VectorTileLayer::getFeature(std::size_t i) return std::make_shared(features.at(i), *this); } -VectorTileMonitor::VectorTileMonitor(const SourceInfo& source, const TileID& id, float pixelRatio) - : url(source.tileURL(id, pixelRatio)) { +VectorTileMonitor::VectorTileMonitor(const TileID& tileID_, const std::string& urlTemplate_) + : tileID(tileID_), urlTemplate(urlTemplate_) { } std::unique_ptr VectorTileMonitor::monitorTile(const GeometryTileMonitor::Callback& callback) { + const std::string url = util::templateTileURL(urlTemplate, tileID); return util::ThreadContext::getFileSource()->request({ Resource::Kind::Tile, url }, [callback, this](Response res) { if (res.data && data == res.data) { // We got the same data again. Abort early. diff --git a/src/mbgl/map/vector_tile.hpp b/src/mbgl/map/vector_tile.hpp index 61b97bc9a..fb2007aa4 100644 --- a/src/mbgl/map/vector_tile.hpp +++ b/src/mbgl/map/vector_tile.hpp @@ -2,6 +2,7 @@ #define MBGL_MAP_VECTOR_TILE #include +#include #include #include @@ -56,17 +57,17 @@ private: mutable std::map> layers; }; -class SourceInfo; class TileID; class VectorTileMonitor : public GeometryTileMonitor { public: - VectorTileMonitor(const SourceInfo&, const TileID&, float pixelRatio); + VectorTileMonitor(const TileID&, const std::string& urlTemplate); std::unique_ptr monitorTile(const GeometryTileMonitor::Callback&) override; private: - std::string url; + TileID tileID; + std::string urlTemplate; std::shared_ptr data; }; diff --git a/src/mbgl/util/url.cpp b/src/mbgl/util/url.cpp index 07a226414..1bd9f410f 100644 --- a/src/mbgl/util/url.cpp +++ b/src/mbgl/util/url.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include #include #include @@ -47,6 +50,27 @@ std::string percentDecode(const std::string& input) { return decoded; } +std::string templateTileURL(const std::string& url, const TileID& id, float pixelRatio) { + return util::replaceTokens(url, [&](const std::string& token) -> std::string { + if (token == "z") { + return util::toString(id.sourceZ); + } else if (token == "x") { + return util::toString(id.x); + } else if (token == "y") { + return util::toString(id.y); + } else if (token == "prefix") { + std::string prefix{ 2 }; + prefix[0] = "0123456789abcdef"[id.x % 16]; + prefix[1] = "0123456789abcdef"[id.y % 16]; + return prefix; + } else if (token == "ratio") { + return pixelRatio > 1.0 ? "@2x" : ""; + } else { + return ""; + } + }); +} + namespace { const std::string assetProtocol = "asset://"; } diff --git a/src/mbgl/util/url.hpp b/src/mbgl/util/url.hpp index 583e04c30..da4cfb267 100644 --- a/src/mbgl/util/url.hpp +++ b/src/mbgl/util/url.hpp @@ -4,11 +4,16 @@ #include namespace mbgl { + +class TileID; + namespace util { std::string percentEncode(const std::string&); std::string percentDecode(const std::string&); +std::string templateTileURL(const std::string& url, const TileID& id, float pixelRatio = 1); + bool isAssetURL(const std::string&); } // namespace util