diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2020-04-27 15:24:22 +0300 |
---|---|---|
committer | Thiago Marcos P. Santos <thiago@mapbox.com> | 2020-05-26 20:35:03 +0300 |
commit | 1d892b1feef4a4d15711d4596f36e6f558afae26 (patch) | |
tree | 270d5cd24e287bc5e3b838d0aa590a62d99ca850 | |
parent | 95c80287d280883328c614bbf0c8013d71be37d5 (diff) | |
download | qtlocation-mapboxgl-1d892b1feef4a4d15711d4596f36e6f558afae26.tar.gz |
remove SourceType usage and source downcasts
58 files changed, 931 insertions, 681 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 58796e8b16..a61cb319c1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -232,6 +232,8 @@ add_library( ${PROJECT_SOURCE_DIR}/include/mbgl/style/property_value.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/style/rotation.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/style/source.hpp + ${PROJECT_SOURCE_DIR}/include/mbgl/style/source_data.hpp + ${PROJECT_SOURCE_DIR}/include/mbgl/style/source_parameters.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/style/sources/custom_geometry_source.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/style/sources/geojson_source.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/style/sources/image_source.hpp @@ -244,6 +246,7 @@ add_library( ${PROJECT_SOURCE_DIR}/include/mbgl/style/types.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/style/undefined.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/tile/tile_id.hpp + ${PROJECT_SOURCE_DIR}/include/mbgl/tile/tile_kind.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/tile/tile_necessity.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/util/async_request.hpp ${PROJECT_SOURCE_DIR}/include/mbgl/util/async_task.hpp @@ -686,6 +689,8 @@ add_library( ${PROJECT_SOURCE_DIR}/src/mbgl/style/sources/image_source_impl.cpp ${PROJECT_SOURCE_DIR}/src/mbgl/style/sources/image_source_impl.hpp ${PROJECT_SOURCE_DIR}/src/mbgl/style/sources/raster_dem_source.cpp + ${PROJECT_SOURCE_DIR}/src/mbgl/style/sources/raster_dem_source_impl.cpp + ${PROJECT_SOURCE_DIR}/src/mbgl/style/sources/raster_dem_source_impl.hpp ${PROJECT_SOURCE_DIR}/src/mbgl/style/sources/raster_source.cpp ${PROJECT_SOURCE_DIR}/src/mbgl/style/sources/raster_source_impl.cpp ${PROJECT_SOURCE_DIR}/src/mbgl/style/sources/raster_source_impl.hpp diff --git a/include/mbgl/style/layer.hpp b/include/mbgl/style/layer.hpp index 9884ad0a35..13a2ec014f 100644 --- a/include/mbgl/style/layer.hpp +++ b/include/mbgl/style/layer.hpp @@ -61,7 +61,7 @@ struct LayerTypeInfo { /** * @brief contains the Id of the supported tile type. Used for internal checks. - * The contained values correspond to \c Tile::Kind enum. + * The contained values correspond to \c TileKind enum. */ const enum class TileKind : uint8_t { Geometry, Raster, RasterDEM, NotRequired } tileKind; }; diff --git a/include/mbgl/style/source.hpp b/include/mbgl/style/source.hpp index eada63d12d..3727d944f8 100644 --- a/include/mbgl/style/source.hpp +++ b/include/mbgl/style/source.hpp @@ -1,19 +1,23 @@ #pragma once +#include <mapbox/std/weak.hpp> +#include <mapbox/util/type_wrapper.hpp> +#include <mbgl/style/source_data.hpp> +#include <mbgl/style/source_parameters.hpp> #include <mbgl/style/types.hpp> +#include <mbgl/tile/tile_kind.hpp> #include <mbgl/util/chrono.hpp> +#include <mbgl/util/feature.hpp> #include <mbgl/util/immutable.hpp> #include <mbgl/util/optional.hpp> - -#include <mapbox/std/weak.hpp> -#include <mapbox/util/type_wrapper.hpp> - +#include <mbgl/util/variant.hpp> #include <memory> #include <string> namespace mbgl { class FileSource; +class Tileset; namespace style { @@ -25,6 +29,27 @@ class SourceObserver; struct LayerTypeInfo; /** + * @brief Holds static data for a certain source type. + */ +struct SourceTypeInfo { + /** + * @brief contains the source type as defined in the style specification; + */ + const char* type; + + /** + * @brief specifies whether the source supports tile prefetching; + */ + const bool supportsTilePrefetch; + + /** + * @brief specifies source's tile kind; + * + */ + const optional<TileKind> tileKind; +}; + +/** * The runtime representation of a [source](https://www.mapbox.com/mapbox-gl-style-spec/#sources) from the Mapbox Style * Specification. * @@ -47,22 +72,6 @@ public: virtual ~Source(); - // Check whether this source is of the given subtype. - template <class T> - bool is() const; - - // Dynamically cast this source to the given subtype. - template <class T> - T* as() { - return is<T>() ? reinterpret_cast<T*>(this) : nullptr; - } - - template <class T> - const T* as() const { - return is<T>() ? reinterpret_cast<const T*>(this) : nullptr; - } - - SourceType getType() const; std::string getID() const; optional<std::string> getAttribution() const; @@ -78,9 +87,25 @@ public: SourceObserver* observer = nullptr; virtual void loadDescription(FileSource&) = 0; + + virtual void setSourceData(SourceData){}; + + virtual SourceDataResult getSourceData() const { return {}; } + + virtual const variant<std::string, Tileset>* getURLOrTileset() const { return nullptr; } + + virtual void setSourceParameters(SourceParameters){}; + + virtual void invalidateTile(const CanonicalTileID&) {} + virtual void invalidateRegion(const LatLngBounds&) {} + void setPrefetchZoomDelta(optional<uint8_t> delta) noexcept; optional<uint8_t> getPrefetchZoomDelta() const noexcept; + int32_t getCoveringZoomLevel(double z) const noexcept; + + uint16_t getTileSize() const; + // If the given source supports loading tiles from a server, // sets the minimum tile update interval. // Update network requests that are more frequent than the @@ -103,6 +128,7 @@ public: // parent tile may be used. void setMaxOverscaleFactorForParentTiles(optional<uint8_t> overscaleFactor) noexcept; optional<uint8_t> getMaxOverscaleFactorForParentTiles() const noexcept; + void dumpDebugLogs() const; virtual bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const = 0; @@ -116,8 +142,16 @@ public: virtual mapbox::base::WeakPtr<Source> makeWeakPtr() = 0; + const SourceTypeInfo* getTypeInfo() const noexcept; + + virtual Value serialize() const; + protected: explicit Source(Immutable<Impl>); + + void serializeUrlOrTileSet(Value&, const variant<std::string, Tileset>*) const; + void serializeTileSet(Value&, const mbgl::Tileset&) const; + virtual Mutable<Impl> createMutable() const noexcept = 0; }; diff --git a/include/mbgl/style/source_data.hpp b/include/mbgl/style/source_data.hpp new file mode 100644 index 0000000000..366adff0df --- /dev/null +++ b/include/mbgl/style/source_data.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include <mbgl/util/geojson.hpp> +#include <mbgl/util/image.hpp> +#include <mbgl/util/optional.hpp> +#include <memory> +#include <string> + +namespace mbgl { +namespace style { + +class GeoJSONData; + +struct SourceData { + SourceData() = default; + explicit SourceData(std::string url_) : url(std::move(url_)) {} + explicit SourceData(PremultipliedImage image_) : image(std::move(image_)) {} + explicit SourceData(GeoJSON geoJSON_) : geoJSON(std::move(geoJSON_)) {} + explicit SourceData(std::shared_ptr<GeoJSONData> data) : geoJSONData(std::move(data)) {} + + optional<std::string> url; + optional<PremultipliedImage> image; + optional<GeoJSON> geoJSON; + std::shared_ptr<GeoJSONData> geoJSONData; +}; + +struct SourceDataResult { + SourceDataResult() = default; + explicit SourceDataResult(const std::string& url_) : url(&url_) {} + explicit SourceDataResult(std::weak_ptr<PremultipliedImage>& image_) : image(std::move(image_)) {} + explicit SourceDataResult(std::weak_ptr<GeoJSONData> data) : geoJSONData(std::move(data)) {} + + const std::string* url = nullptr; + std::weak_ptr<const PremultipliedImage> image; + std::weak_ptr<const GeoJSONData> geoJSONData; +}; + +} // namespace style +} // namespace mbgl diff --git a/include/mbgl/style/source_parameters.hpp b/include/mbgl/style/source_parameters.hpp new file mode 100644 index 0000000000..0d7891f193 --- /dev/null +++ b/include/mbgl/style/source_parameters.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include <mbgl/util/geo.hpp> +#include <mbgl/util/optional.hpp> + +namespace mbgl { +namespace style { + +struct SourceParameters { + explicit SourceParameters(std::array<LatLng, 4> bounds_) : bounds(bounds_) {} + optional<std::array<LatLng, 4>> bounds; +}; + +} // namespace style +} // namespace mbgl diff --git a/include/mbgl/style/sources/custom_geometry_source.hpp b/include/mbgl/style/sources/custom_geometry_source.hpp index 250886c209..6687f0f148 100644 --- a/include/mbgl/style/sources/custom_geometry_source.hpp +++ b/include/mbgl/style/sources/custom_geometry_source.hpp @@ -9,7 +9,6 @@ namespace mbgl { class OverscaledTileID; -class CanonicalTileID; template <class T> class Actor; class ThreadPool; @@ -41,8 +40,8 @@ public: ~CustomGeometrySource() final; void loadDescription(FileSource&) final; void setTileData(const CanonicalTileID&, const GeoJSON&); - void invalidateTile(const CanonicalTileID&); - void invalidateRegion(const LatLngBounds&); + void invalidateTile(const CanonicalTileID&) override; + void invalidateRegion(const LatLngBounds&) override; // Private implementation class Impl; const Impl& impl() const; @@ -60,10 +59,5 @@ private: mapbox::base::WeakPtrFactory<Source> weakFactory {this}; }; -template <> -inline bool Source::is<CustomGeometrySource>() const { - return getType() == SourceType::CustomVector; -} - } // namespace style } // namespace mbgl diff --git a/include/mbgl/style/sources/geojson_source.hpp b/include/mbgl/style/sources/geojson_source.hpp index b728b340ea..7d8cc8a013 100644 --- a/include/mbgl/style/sources/geojson_source.hpp +++ b/include/mbgl/style/sources/geojson_source.hpp @@ -1,13 +1,12 @@ #pragma once +#include <map> #include <mbgl/style/expression/expression.hpp> #include <mbgl/style/source.hpp> #include <mbgl/tile/tile_id.hpp> #include <mbgl/util/constants.hpp> #include <mbgl/util/geojson.hpp> #include <mbgl/util/optional.hpp> - -#include <map> #include <memory> #include <utility> @@ -37,6 +36,7 @@ struct GeoJSONOptions { static Immutable<GeoJSONOptions> defaultOptions(); }; + class GeoJSONData { public: using TileFeatures = mapbox::feature::feature_collection<int16_t>; @@ -49,9 +49,9 @@ public: virtual void getTile(const CanonicalTileID&, const std::function<void(TileFeatures)>&) = 0; // SuperclusterData - virtual Features getChildren(std::uint32_t) = 0; - virtual Features getLeaves(std::uint32_t, std::uint32_t limit, std::uint32_t offset) = 0; - virtual std::uint8_t getClusterExpansionZoom(std::uint32_t) = 0; + virtual Features getChildren(std::uint32_t) const = 0; + virtual Features getLeaves(std::uint32_t, std::uint32_t limit, std::uint32_t offset) const = 0; + virtual std::uint8_t getClusterExpansionZoom(std::uint32_t) const = 0; virtual std::shared_ptr<Scheduler> getScheduler() { return nullptr; } }; @@ -64,7 +64,9 @@ public: void setURL(const std::string& url); void setGeoJSON(const GeoJSON&); void setGeoJSONData(std::shared_ptr<GeoJSONData>); + void setSourceData(SourceData data) override; + SourceDataResult getSourceData() const override; optional<std::string> getURL() const; const GeoJSONOptions& getOptions() const; @@ -75,9 +77,9 @@ public: bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const override; - mapbox::base::WeakPtr<Source> makeWeakPtr() override { - return weakFactory.makeWeakPtr(); - } + mapbox::base::WeakPtr<Source> makeWeakPtr() override { return weakFactory.makeWeakPtr(); } + + Value serialize() const override; protected: Mutable<Source::Impl> createMutable() const noexcept final; @@ -86,13 +88,8 @@ private: optional<std::string> url; std::unique_ptr<AsyncRequest> req; std::shared_ptr<Scheduler> threadPool; - mapbox::base::WeakPtrFactory<Source> weakFactory {this}; + mapbox::base::WeakPtrFactory<Source> weakFactory{this}; }; -template <> -inline bool Source::is<GeoJSONSource>() const { - return getType() == SourceType::GeoJSON; -} - } // namespace style } // namespace mbgl diff --git a/include/mbgl/style/sources/image_source.hpp b/include/mbgl/style/sources/image_source.hpp index 32971ef3d6..5d2e72027f 100644 --- a/include/mbgl/style/sources/image_source.hpp +++ b/include/mbgl/style/sources/image_source.hpp @@ -16,9 +16,11 @@ public: ~ImageSource() override; optional<std::string> getURL() const; - void setURL(const std::string& url); + void setURL(const std::string& url); void setImage(PremultipliedImage&&); + void setSourceData(SourceData) override; + SourceDataResult getSourceData() const override; void setCoordinates(const std::array<LatLng, 4>&); std::array<LatLng, 4> getCoordinates() const; @@ -30,9 +32,9 @@ public: bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const override; - mapbox::base::WeakPtr<Source> makeWeakPtr() override { - return weakFactory.makeWeakPtr(); - } + mapbox::base::WeakPtr<Source> makeWeakPtr() override { return weakFactory.makeWeakPtr(); } + + Value serialize() const override; protected: Mutable<Source::Impl> createMutable() const noexcept final; @@ -40,13 +42,8 @@ protected: private: optional<std::string> url; std::unique_ptr<AsyncRequest> req; - mapbox::base::WeakPtrFactory<Source> weakFactory {this}; + mapbox::base::WeakPtrFactory<Source> weakFactory{this}; }; -template <> -inline bool Source::is<ImageSource>() const { - return getType() == SourceType::Image; -} - } // namespace style } // namespace mbgl diff --git a/include/mbgl/style/sources/raster_dem_source.hpp b/include/mbgl/style/sources/raster_dem_source.hpp index 42e27cd078..14eed64545 100644 --- a/include/mbgl/style/sources/raster_dem_source.hpp +++ b/include/mbgl/style/sources/raster_dem_source.hpp @@ -14,12 +14,16 @@ class RasterDEMSource : public RasterSource { public: RasterDEMSource(std::string id, variant<std::string, Tileset> urlOrTileset, uint16_t tileSize); bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const override; -}; -template <> -inline bool Source::is<RasterDEMSource>() const { - return getType() == SourceType::RasterDEM; -} + class Impl; + const Impl& impl() const; + + Value serialize() const override; + +protected: + Mutable<Source::Impl> createMutable() const noexcept final; + Mutable<Source::Impl> createMutable(Tileset tileset) const noexcept final; +}; } // namespace style } // namespace mbgl diff --git a/include/mbgl/style/sources/raster_source.hpp b/include/mbgl/style/sources/raster_source.hpp index e06c3404e9..d84a9576c1 100644 --- a/include/mbgl/style/sources/raster_source.hpp +++ b/include/mbgl/style/sources/raster_source.hpp @@ -12,14 +12,12 @@ namespace style { class RasterSource : public Source { public: - RasterSource(std::string id, variant<std::string, Tileset> urlOrTileset, uint16_t tileSize, SourceType sourceType = SourceType::Raster); + RasterSource(std::string id, variant<std::string, Tileset> urlOrTileset, uint16_t tileSize); ~RasterSource() override; - const variant<std::string, Tileset>& getURLOrTileset() const; + const variant<std::string, Tileset>* getURLOrTileset() const final; optional<std::string> getURL() const; - uint16_t getTileSize() const; - class Impl; const Impl& impl() const; @@ -31,8 +29,13 @@ public: return weakFactory.makeWeakPtr(); } + Value serialize() const override; + protected: - Mutable<Source::Impl> createMutable() const noexcept final; + RasterSource(Immutable<Impl>&&, variant<std::string, Tileset> urlOrTileset_); + + Mutable<Source::Impl> createMutable() const noexcept override; + virtual Mutable<Source::Impl> createMutable(Tileset tileset) const noexcept; private: const variant<std::string, Tileset> urlOrTileset; @@ -40,10 +43,5 @@ private: mapbox::base::WeakPtrFactory<Source> weakFactory {this}; }; -template <> -inline bool Source::is<RasterSource>() const { - return getType() == SourceType::Raster; -} - } // namespace style } // namespace mbgl diff --git a/include/mbgl/style/sources/vector_source.hpp b/include/mbgl/style/sources/vector_source.hpp index 83fcae95d6..27fd78cce5 100644 --- a/include/mbgl/style/sources/vector_source.hpp +++ b/include/mbgl/style/sources/vector_source.hpp @@ -16,7 +16,7 @@ public: optional<float> minZoom = nullopt); ~VectorSource() final; - const variant<std::string, Tileset>& getURLOrTileset() const; + const variant<std::string, Tileset>* getURLOrTileset() const final; optional<std::string> getURL() const; class Impl; @@ -30,6 +30,8 @@ public: return weakFactory.makeWeakPtr(); } + Value serialize() const override; + protected: Mutable<Source::Impl> createMutable() const noexcept final; @@ -41,10 +43,5 @@ private: optional<float> minZoom; }; -template <> -inline bool Source::is<VectorSource>() const { - return getType() == SourceType::Vector; -} - } // namespace style } // namespace mbgl diff --git a/include/mbgl/style/types.hpp b/include/mbgl/style/types.hpp index 5d88dafb33..7c3657e7ee 100644 --- a/include/mbgl/style/types.hpp +++ b/include/mbgl/style/types.hpp @@ -6,18 +6,6 @@ namespace mbgl { namespace style { -// TODO: should be in public source.hpp header -enum class SourceType : uint8_t { - Vector, - Raster, - RasterDEM, - GeoJSON, - Video, - Annotations, - Image, - CustomVector -}; - enum class VisibilityType : bool { Visible, None, diff --git a/include/mbgl/tile/tile_kind.hpp b/include/mbgl/tile/tile_kind.hpp new file mode 100644 index 0000000000..16e50b802c --- /dev/null +++ b/include/mbgl/tile/tile_kind.hpp @@ -0,0 +1,7 @@ +#pragma once + +namespace mbgl { + +enum class TileKind : uint8_t { Geometry, Raster, RasterDEM }; + +} // namespace mbgl
\ No newline at end of file diff --git a/platform/default/include/mbgl/storage/offline_download.hpp b/platform/default/include/mbgl/storage/offline_download.hpp index 973fa826fa..3c9c5db879 100644 --- a/platform/default/include/mbgl/storage/offline_download.hpp +++ b/platform/default/include/mbgl/storage/offline_download.hpp @@ -19,6 +19,7 @@ class Tileset; namespace style { class Parser; +class Source; } // namespace style /** @@ -65,7 +66,7 @@ private: std::list<std::tuple<Resource, Response>> buffer; void queueResource(Resource&&); - void queueTiles(style::SourceType, uint16_t tileSize, const Tileset&); + void queueTiles(const style::Source&, const Tileset&); void markPendingUsedResources(); }; diff --git a/platform/default/src/mbgl/storage/offline_download.cpp b/platform/default/src/mbgl/storage/offline_download.cpp index c502e12b0b..a144bf1900 100644 --- a/platform/default/src/mbgl/storage/offline_download.cpp +++ b/platform/default/src/mbgl/storage/offline_download.cpp @@ -1,23 +1,21 @@ +#include <mbgl/storage/http_file_source.hpp> #include <mbgl/storage/offline_database.hpp> #include <mbgl/storage/offline_download.hpp> #include <mbgl/storage/resource.hpp> #include <mbgl/storage/response.hpp> -#include <mbgl/storage/http_file_source.hpp> +#include <mbgl/style/conversion/json.hpp> +#include <mbgl/style/conversion/tileset.hpp> #include <mbgl/style/parser.hpp> -#include <mbgl/style/sources/vector_source.hpp> -#include <mbgl/style/sources/raster_source.hpp> -#include <mbgl/style/sources/raster_dem_source.hpp> #include <mbgl/style/sources/geojson_source.hpp> +#include <mbgl/style/sources/geojson_source_impl.hpp> #include <mbgl/style/sources/image_source.hpp> -#include <mbgl/style/conversion/json.hpp> -#include <mbgl/style/conversion/tileset.hpp> +#include <mbgl/style/sources/image_source_impl.hpp> #include <mbgl/text/glyph.hpp> #include <mbgl/util/i18n.hpp> #include <mbgl/util/mapbox.hpp> #include <mbgl/util/run_loop.hpp> #include <mbgl/util/tile_cover.hpp> #include <mbgl/util/tileset.hpp> - #include <set> namespace { @@ -35,15 +33,16 @@ using namespace style; template <class RegionDefinition> Range<uint8_t> coveringZoomRange(const RegionDefinition& definition, - style::SourceType type, uint16_t tileSize, const Range<uint8_t>& zoomRange) { - double minZ = std::max<double>(util::coveringZoomLevel(definition.minZoom, type, tileSize), zoomRange.min); - double maxZ = std::min<double>(util::coveringZoomLevel(definition.maxZoom, type, tileSize), zoomRange.max); + const style::Source& source, + const Range<uint8_t>& zoomRange) { + double minZ = std::max<double>(source.getCoveringZoomLevel(definition.minZoom), zoomRange.min); + double maxZ = std::min<double>(source.getCoveringZoomLevel(definition.maxZoom), zoomRange.max); assert(minZ >= 0); assert(maxZ >= 0); assert(minZ < std::numeric_limits<uint8_t>::max()); assert(maxZ < std::numeric_limits<uint8_t>::max()); - return { static_cast<uint8_t>(minZ), static_cast<uint8_t>(maxZ) }; + return {static_cast<uint8_t>(minZ), static_cast<uint8_t>(maxZ)}; } template <class Geometry, class Fn> @@ -54,33 +53,31 @@ void tileCover(const Geometry& geometry, uint8_t z, Fn&& fn) { } } - template <class Fn> -void tileCover(const OfflineRegionDefinition& definition, style::SourceType type, - uint16_t tileSize, const Range<uint8_t>& zoomRange, Fn&& fn) { +void tileCover(const OfflineRegionDefinition& definition, + const style::Source& source, + const Range<uint8_t>& zoomRange, + Fn&& fn) { const Range<uint8_t> clampedZoomRange = - definition.match([&](auto& reg) { return coveringZoomRange(reg, type, tileSize, zoomRange); }); + definition.match([&](auto& reg) { return coveringZoomRange(reg, source, zoomRange); }); for (uint8_t z = clampedZoomRange.min; z <= clampedZoomRange.max; z++) { - definition.match( - [&](const OfflineTilePyramidRegionDefinition& reg){ tileCover(reg.bounds, z, fn); }, - [&](const OfflineGeometryRegionDefinition& reg){ tileCover(reg.geometry, z, fn); } - ); + definition.match([&](const OfflineTilePyramidRegionDefinition& reg) { tileCover(reg.bounds, z, fn); }, + [&](const OfflineGeometryRegionDefinition& reg) { tileCover(reg.geometry, z, fn); }); } } -uint64_t tileCount(const OfflineRegionDefinition& definition, style::SourceType type, - uint16_t tileSize, const Range<uint8_t>& zoomRange) { - +uint64_t tileCount(const OfflineRegionDefinition& definition, + const style::Source& source, + const Range<uint8_t>& zoomRange) { const Range<uint8_t> clampedZoomRange = - definition.match([&](auto& reg) { return coveringZoomRange(reg, type, tileSize, zoomRange); }); + definition.match([&](auto& reg) { return coveringZoomRange(reg, source, zoomRange); }); - unsigned long result = 0;; + unsigned long result = 0; for (uint8_t z = clampedZoomRange.min; z <= clampedZoomRange.max; z++) { result += definition.match( - [&](const OfflineTilePyramidRegionDefinition& reg){ return util::tileCount(reg.bounds, z); }, - [&](const OfflineGeometryRegionDefinition& reg){ return util::tileCount(reg.geometry, z); } - ); + [&](const OfflineTilePyramidRegionDefinition& reg) { return util::tileCount(reg.bounds, z); }, + [&](const OfflineGeometryRegionDefinition& reg) { return util::tileCount(reg.geometry, z); }); } return result; @@ -135,7 +132,7 @@ OfflineRegionStatus OfflineDownload::getStatus() const { result->requiredResourceCount++; optional<Response> styleResponse = - offlineDatabase.get(Resource::style(definition.match([](auto& reg){ return reg.styleURL; }))); + offlineDatabase.get(Resource::style(definition.match([](auto& reg) { return reg.styleURL; }))); if (!styleResponse) { return *result; } @@ -146,11 +143,9 @@ OfflineRegionStatus OfflineDownload::getStatus() const { result->requiredResourceCountIsPrecise = true; for (const auto& source : parser.sources) { - SourceType type = source->getType(); - - auto handleTiledSource = [&] (const variant<std::string, Tileset>& urlOrTileset, const uint16_t tileSize) { + auto handleTiledSource = [&](const variant<std::string, Tileset>& urlOrTileset) { if (urlOrTileset.is<Tileset>()) { - uint64_t tileSourceCount = tileCount(definition, type, tileSize, urlOrTileset.get<Tileset>().zoomRange); + uint64_t tileSourceCount = tileCount(definition, *source, urlOrTileset.get<Tileset>().zoomRange); result->requiredTileCount += tileSourceCount; result->requiredResourceCount += tileSourceCount; } else { @@ -161,7 +156,7 @@ OfflineRegionStatus OfflineDownload::getStatus() const { style::conversion::Error error; optional<Tileset> tileset = style::conversion::convertJSON<Tileset>(*sourceResponse->data, error); if (tileset) { - uint64_t tileSourceCount = tileCount(definition, type, tileSize, (*tileset).zoomRange); + uint64_t tileSourceCount = tileCount(definition, *source, (*tileset).zoomRange); result->requiredTileCount += tileSourceCount; result->requiredResourceCount += tileSourceCount; } @@ -171,53 +166,19 @@ OfflineRegionStatus OfflineDownload::getStatus() const { } }; - switch (type) { - case SourceType::Vector: { - const auto& vectorSource = *source->as<VectorSource>(); - handleTiledSource(vectorSource.getURLOrTileset(), util::tileSize); - break; - } - - case SourceType::Raster: { - const auto& rasterSource = *source->as<RasterSource>(); - handleTiledSource(rasterSource.getURLOrTileset(), rasterSource.getTileSize()); - break; - } - - case SourceType::RasterDEM: { - const auto& rasterDEMSource = *source->as<RasterDEMSource>(); - handleTiledSource(rasterDEMSource.getURLOrTileset(), rasterDEMSource.getTileSize()); - break; - } - - case SourceType::GeoJSON: { - const auto& geojsonSource = *source->as<GeoJSONSource>(); - if (geojsonSource.getURL()) { - result->requiredResourceCount += 1; - } - break; - } - - case SourceType::Image: { - const auto& imageSource = *source->as<ImageSource>(); - if (imageSource.getURL()) { - result->requiredResourceCount += 1; - } - break; - } - - case SourceType::Video: - case SourceType::Annotations: - case SourceType::CustomVector: - break; + auto urlOrTileSet = source->getURLOrTileset(); + if (urlOrTileSet) { + handleTiledSource(*urlOrTileSet); + } else if (source->getSourceData().url) { + result->requiredResourceCount += 1; } } if (!parser.glyphURL.empty()) { - result->requiredResourceCount += parser.fontStacks().size() * - (definition.match([](auto& reg){ return reg.includeIdeographs; }) ? - GLYPH_RANGES_PER_FONT_STACK : - NON_IDEOGRAPH_GLYPH_RANGES_PER_FONT_STACK); + result->requiredResourceCount += + parser.fontStacks().size() * (definition.match([](auto& reg) { return reg.includeIdeographs; }) + ? GLYPH_RANGES_PER_FONT_STACK + : NON_IDEOGRAPH_GLYPH_RANGES_PER_FONT_STACK); } if (!parser.spriteURL.empty()) { @@ -232,7 +193,7 @@ void OfflineDownload::activateDownload() { status.downloadState = OfflineRegionDownloadState::Active; status.requiredResourceCount++; - auto styleResource = Resource::style(definition.match([](auto& reg){ return reg.styleURL; })); + auto styleResource = Resource::style(definition.match([](auto& reg) { return reg.styleURL; })); styleResource.setPriority(Resource::Priority::Low); styleResource.setUsage(Resource::Usage::Offline); @@ -242,89 +203,62 @@ void OfflineDownload::activateDownload() { style::Parser parser; parser.parse(*styleResponse.data); - for (const auto& source : parser.sources) { - SourceType type = source->getType(); - - auto handleTiledSource = [&] (const variant<std::string, Tileset>& urlOrTileset, const uint16_t tileSize) { - if (urlOrTileset.is<Tileset>()) { - queueTiles(type, tileSize, urlOrTileset.get<Tileset>()); - } else { - const auto& url = urlOrTileset.get<std::string>(); - status.requiredResourceCountIsPrecise = false; - status.requiredResourceCount++; - requiredSourceURLs.insert(url); - - auto sourceResource = Resource::source(url); - sourceResource.setPriority(Resource::Priority::Low); - sourceResource.setUsage(Resource::Usage::Offline); - - ensureResource(std::move(sourceResource), [=](const Response& sourceResponse) { - style::conversion::Error error; - optional<Tileset> tileset = style::conversion::convertJSON<Tileset>(*sourceResponse.data, error); - if (tileset) { - util::mapbox::canonicalizeTileset(*tileset, url, type, tileSize); - queueTiles(type, tileSize, *tileset); - - requiredSourceURLs.erase(url); - if (requiredSourceURLs.empty()) { - status.requiredResourceCountIsPrecise = true; - } - } - }); - } - }; - - switch (type) { - case SourceType::Vector: { - const auto& vectorSource = *source->as<VectorSource>(); - handleTiledSource(vectorSource.getURLOrTileset(), util::tileSize); - break; - } - - case SourceType::Raster: { - const auto& rasterSource = *source->as<RasterSource>(); - handleTiledSource(rasterSource.getURLOrTileset(), rasterSource.getTileSize()); - break; - } - - case SourceType::RasterDEM: { - const auto& rasterDEMSource = *source->as<RasterDEMSource>(); - handleTiledSource(rasterDEMSource.getURLOrTileset(), rasterDEMSource.getTileSize()); - break; - } - - case SourceType::GeoJSON: { - const auto& geojsonSource = *source->as<GeoJSONSource>(); - if (geojsonSource.getURL()) { - queueResource(Resource::source(*geojsonSource.getURL())); - } - break; - } + auto handleTiledSource = [this](std::unique_ptr<Source> source, + const variant<std::string, Tileset>& urlOrTileset) mutable { + if (urlOrTileset.is<Tileset>()) { + queueTiles(*source, urlOrTileset.get<Tileset>()); + } else { + const auto& url = urlOrTileset.get<std::string>(); + status.requiredResourceCountIsPrecise = false; + status.requiredResourceCount++; + requiredSourceURLs.insert(url); + + auto sourceResource = Resource::source(url); + sourceResource.setPriority(Resource::Priority::Low); + sourceResource.setUsage(Resource::Usage::Offline); + + ensureResource(std::move(sourceResource), + [=, source = std::make_shared<std::unique_ptr<Source>>(std::move(source))]( + const Response& sourceResponse) { + style::conversion::Error error; + optional<Tileset> tileset = + style::conversion::convertJSON<Tileset>(*sourceResponse.data, error); + if (tileset) { + util::mapbox::canonicalizeTileset(*tileset, url, **source); + queueTiles(**source, *tileset); + + requiredSourceURLs.erase(url); + if (requiredSourceURLs.empty()) { + status.requiredResourceCountIsPrecise = true; + } + } + }); + } + }; - case SourceType::Image: { - const auto& imageSource = *source->as<ImageSource>(); - auto imageUrl = imageSource.getURL(); - if (imageUrl && !imageUrl->empty()) { - queueResource(Resource::image(*imageUrl)); - } - break; + for (auto& source : parser.sources) { + auto urlOrTileSet = source->getURLOrTileset(); + if (urlOrTileSet) { + handleTiledSource(std::move(source), *urlOrTileSet); + } else { + auto sourceData = source->getSourceData(); + if (sourceData.url && source->getTypeInfo() == ImageSource::Impl::staticTypeInfo()) { + queueResource(Resource::image(*sourceData.url)); + } else if (sourceData.url && source->getTypeInfo() == GeoJSONSource::Impl::staticTypeInfo()) { + queueResource(Resource::source(*sourceData.url)); } - - case SourceType::Video: - case SourceType::Annotations: - case SourceType::CustomVector: - break; } } if (!parser.glyphURL.empty()) { - const bool includeIdeographs = definition.match([](auto& reg){ return reg.includeIdeographs; }); + const bool includeIdeographs = definition.match([](auto& reg) { return reg.includeIdeographs; }); for (const auto& fontStack : parser.fontStacks()) { for (char16_t i = 0; i < GLYPH_RANGES_PER_FONT_STACK; i++) { // Assumes that if a glyph range starts with fixed width/ideographic characters, the entire // range will be fixed width. if (includeIdeographs || !util::i18n::allowsFixedWidthGlyphGeneration(i * GLYPHS_PER_GLYPH_RANGE)) { - queueResource(Resource::glyphs(parser.glyphURL, fontStack, getGlyphRange(i * GLYPHS_PER_GLYPH_RANGE))); + queueResource( + Resource::glyphs(parser.glyphURL, fontStack, getGlyphRange(i * GLYPHS_PER_GLYPH_RANGE))); } } } @@ -414,14 +348,17 @@ void OfflineDownload::queueResource(Resource&& resource) { resourcesRemaining.push_front(std::move(resource)); } -void OfflineDownload::queueTiles(SourceType type, uint16_t tileSize, const Tileset& tileset) { - tileCover(definition, type, tileSize, tileset.zoomRange, [&](const auto& tile) { +void OfflineDownload::queueTiles(const Source& source, const Tileset& tileset) { + tileCover(definition, source, tileset.zoomRange, [&](const auto& tile) { status.requiredResourceCount++; status.requiredTileCount++; - auto tileResource = Resource::tile( - tileset.tiles[0], definition.match([](auto& def) { return def.pixelRatio; }), - tile.x, tile.y, tile.z, tileset.scheme); + auto tileResource = Resource::tile(tileset.tiles[0], + definition.match([](auto& def) { return def.pixelRatio; }), + tile.x, + tile.y, + tile.z, + tileset.scheme); tileResource.setPriority(Resource::Priority::Low); tileResource.setUsage(Resource::Usage::Offline); @@ -435,8 +372,7 @@ void OfflineDownload::markPendingUsedResources() { resourcesToBeMarkedAsUsed.clear(); } -void OfflineDownload::ensureResource(Resource&& resource, - std::function<void(Response)> callback) { +void OfflineDownload::ensureResource(Resource&& resource, std::function<void(Response)> callback) { assert(resource.priority == Resource::Priority::Low); assert(resource.usage == Resource::Usage::Offline); @@ -444,7 +380,7 @@ void OfflineDownload::ensureResource(Resource&& resource, *workRequestsIt = util::RunLoop::Get()->invokeCancellable([=]() { requests.erase(workRequestsIt); const auto resourceKind = resource.kind; - auto getResourceSizeInDatabase = [&] () -> optional<int64_t> { + auto getResourceSizeInDatabase = [&]() -> optional<int64_t> { optional<int64_t> result; if (!callback) { result = offlineDatabase.hasRegionResource(resource); diff --git a/src/mbgl/annotation/annotation_source.cpp b/src/mbgl/annotation/annotation_source.cpp index 318241a914..712ad73651 100644 --- a/src/mbgl/annotation/annotation_source.cpp +++ b/src/mbgl/annotation/annotation_source.cpp @@ -10,9 +10,7 @@ AnnotationSource::AnnotationSource() : Source(makeMutable<Impl>()) { } -AnnotationSource::Impl::Impl() - : Source::Impl(SourceType::Annotations, AnnotationManager::SourceID) { -} +AnnotationSource::Impl::Impl() : Source::Impl(AnnotationManager::SourceID) {} const AnnotationSource::Impl& AnnotationSource::impl() const { return static_cast<const Impl&>(*baseImpl); @@ -34,4 +32,9 @@ Mutable<Source::Impl> AnnotationSource::createMutable() const noexcept { return staticMutableCast<Source::Impl>(makeMutable<Impl>(impl())); } +const SourceTypeInfo* AnnotationSource::Impl::staticTypeInfo() noexcept { + const static SourceTypeInfo typeInfo{"annotations", false, nullopt}; + return &typeInfo; +} + } // namespace mbgl diff --git a/src/mbgl/annotation/annotation_source.hpp b/src/mbgl/annotation/annotation_source.hpp index de432202a0..7503c4b066 100644 --- a/src/mbgl/annotation/annotation_source.hpp +++ b/src/mbgl/annotation/annotation_source.hpp @@ -18,10 +18,8 @@ protected: private: void loadDescription(FileSource&) final; bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const override; - mapbox::base::WeakPtr<Source> makeWeakPtr() override { - return weakFactory.makeWeakPtr(); - } - mapbox::base::WeakPtrFactory<Source> weakFactory {this}; + mapbox::base::WeakPtr<Source> makeWeakPtr() override { return weakFactory.makeWeakPtr(); } + mapbox::base::WeakPtrFactory<Source> weakFactory{this}; }; class AnnotationSource::Impl : public style::Source::Impl { @@ -29,6 +27,8 @@ public: Impl(); optional<std::string> getAttribution() const final; + + DECLARE_SOURCE_TYPE_INFO; }; } // namespace mbgl diff --git a/src/mbgl/renderer/layers/render_symbol_layer.cpp b/src/mbgl/renderer/layers/render_symbol_layer.cpp index 1d8c0a70a8..169e9d22ab 100644 --- a/src/mbgl/renderer/layers/render_symbol_layer.cpp +++ b/src/mbgl/renderer/layers/render_symbol_layer.cpp @@ -582,7 +582,7 @@ void RenderSymbolLayer::prepare(const LayerPrepareParameters& params) { // Only place this layer if it's the "group leader" for the bucket const Tile* tile = params.source->getRenderedTile(renderTile.id); assert(tile); - assert(tile->kind == Tile::Kind::Geometry); + assert(tile->kind == TileKind::Geometry); auto featureIndex = static_cast<const GeometryTile*>(tile)->getFeatureIndex(); diff --git a/src/mbgl/renderer/render_source.cpp b/src/mbgl/renderer/render_source.cpp index f39d3089d7..dd1959e2c7 100644 --- a/src/mbgl/renderer/render_source.cpp +++ b/src/mbgl/renderer/render_source.cpp @@ -1,16 +1,15 @@ +#include <mbgl/annotation/render_annotation_source.hpp> +#include <mbgl/layermanager/layer_manager.hpp> #include <mbgl/renderer/render_source.hpp> #include <mbgl/renderer/render_source_observer.hpp> +#include <mbgl/renderer/sources/render_custom_geometry_source.hpp> #include <mbgl/renderer/sources/render_geojson_source.hpp> -#include <mbgl/renderer/sources/render_raster_source.hpp> +#include <mbgl/renderer/sources/render_image_source.hpp> #include <mbgl/renderer/sources/render_raster_dem_source.hpp> +#include <mbgl/renderer/sources/render_raster_source.hpp> #include <mbgl/renderer/sources/render_vector_source.hpp> #include <mbgl/renderer/tile_parameters.hpp> -#include <mbgl/annotation/render_annotation_source.hpp> -#include <mbgl/renderer/sources/render_image_source.hpp> -#include <mbgl/renderer/sources/render_custom_geometry_source.hpp> #include <mbgl/tile/tile.hpp> - -#include <mbgl/layermanager/layer_manager.hpp> #include <mbgl/util/constants.hpp> #include <utility> @@ -19,42 +18,38 @@ namespace mbgl { using namespace style; std::unique_ptr<RenderSource> RenderSource::create(const Immutable<Source::Impl>& impl) { - switch (impl->type) { - case SourceType::Vector: + std::string sourceType{impl->getTypeInfo()->type}; + if (sourceType == VectorSource::Impl::staticTypeInfo()->type) { return std::make_unique<RenderVectorSource>(staticImmutableCast<VectorSource::Impl>(impl)); - case SourceType::Raster: + } else if (sourceType == RasterSource::Impl::staticTypeInfo()->type) { return std::make_unique<RenderRasterSource>(staticImmutableCast<RasterSource::Impl>(impl)); - case SourceType::RasterDEM: - return std::make_unique<RenderRasterDEMSource>(staticImmutableCast<RasterSource::Impl>(impl)); - case SourceType::GeoJSON: + } else if (sourceType == RasterDEMSource::Impl::staticTypeInfo()->type) { + return std::make_unique<RenderRasterDEMSource>(staticImmutableCast<RasterDEMSource::Impl>(impl)); + } else if (sourceType == GeoJSONSource::Impl::staticTypeInfo()->type) { return std::make_unique<RenderGeoJSONSource>(staticImmutableCast<GeoJSONSource::Impl>(impl)); - case SourceType::Video: + } else if (sourceType == "video") { assert(false); return nullptr; - case SourceType::Annotations: + } else if (sourceType == AnnotationSource::Impl::staticTypeInfo()->type) { if (LayerManager::annotationsEnabled) { return std::make_unique<RenderAnnotationSource>(staticImmutableCast<AnnotationSource::Impl>(impl)); } else { assert(false); return nullptr; } - case SourceType::Image: + } else if (sourceType == ImageSource::Impl::staticTypeInfo()->type) { return std::make_unique<RenderImageSource>(staticImmutableCast<ImageSource::Impl>(impl)); - case SourceType::CustomVector: + } else if (sourceType == CustomGeometrySource::Impl::staticTypeInfo()->type) { return std::make_unique<RenderCustomGeometrySource>(staticImmutableCast<CustomGeometrySource::Impl>(impl)); } - // Not reachable, but placate GCC. assert(false); return nullptr; } static RenderSourceObserver nullObserver; -RenderSource::RenderSource(Immutable<style::Source::Impl> impl) - : baseImpl(std::move(impl)), - observer(&nullObserver) { -} +RenderSource::RenderSource(Immutable<style::Source::Impl> impl) : baseImpl(std::move(impl)), observer(&nullObserver) {} RenderSource::~RenderSource() = default; @@ -74,7 +69,7 @@ bool RenderSource::isEnabled() const { return enabled; } -uint8_t RenderSource::getMaxZoom() const { +uint8_t RenderSource::getMaxZoom() const { assert(false); return util::TERRAIN_RGB_MAXZOOM; } diff --git a/src/mbgl/renderer/sources/render_raster_dem_source.hpp b/src/mbgl/renderer/sources/render_raster_dem_source.hpp index 72a3779e99..5768a058a6 100644 --- a/src/mbgl/renderer/sources/render_raster_dem_source.hpp +++ b/src/mbgl/renderer/sources/render_raster_dem_source.hpp @@ -1,7 +1,7 @@ #pragma once #include <mbgl/renderer/sources/render_tile_source.hpp> -#include <mbgl/style/sources/raster_source_impl.hpp> +#include <mbgl/style/sources/raster_dem_source_impl.hpp> namespace mbgl { diff --git a/src/mbgl/renderer/style_diff.cpp b/src/mbgl/renderer/style_diff.cpp index 270c4483cd..d925aaa9d7 100644 --- a/src/mbgl/renderer/style_diff.cpp +++ b/src/mbgl/renderer/style_diff.cpp @@ -53,15 +53,14 @@ ImageDifference diffImages(const Immutable<std::vector<ImmutableImage>>& a, SourceDifference diffSources(const Immutable<std::vector<ImmutableSource>>& a, const Immutable<std::vector<ImmutableSource>>& b) { - return diff(a, b, [] (const ImmutableSource& lhs, const ImmutableSource& rhs) { - return std::tie(lhs->id, lhs->type) - == std::tie(rhs->id, rhs->type); + return diff(a, b, [](const ImmutableSource& lhs, const ImmutableSource& rhs) { + return (lhs->id == rhs->id) && (lhs->getTypeInfo() == rhs->getTypeInfo()); }); } LayerDifference diffLayers(const Immutable<std::vector<ImmutableLayer>>& a, const Immutable<std::vector<ImmutableLayer>>& b) { - return diff(a, b, [] (const ImmutableLayer& lhs, const ImmutableLayer& rhs) { + return diff(a, b, [](const ImmutableLayer& lhs, const ImmutableLayer& rhs) { return (lhs->id == rhs->id) && (lhs->getTypeInfo() == rhs->getTypeInfo()); }); } diff --git a/src/mbgl/renderer/tile_pyramid.cpp b/src/mbgl/renderer/tile_pyramid.cpp index 39a9c59283..60aabafd58 100644 --- a/src/mbgl/renderer/tile_pyramid.cpp +++ b/src/mbgl/renderer/tile_pyramid.cpp @@ -1,21 +1,18 @@ -#include <mbgl/renderer/tile_pyramid.hpp> +#include <algorithm> +#include <cmath> +#include <mapbox/geometry/envelope.hpp> +#include <mbgl/algorithm/update_renderables.hpp> +#include <mbgl/map/transform.hpp> +#include <mbgl/math/clamp.hpp> #include <mbgl/renderer/paint_parameters.hpp> +#include <mbgl/renderer/query.hpp> #include <mbgl/renderer/render_source.hpp> #include <mbgl/renderer/tile_parameters.hpp> -#include <mbgl/renderer/query.hpp> -#include <mbgl/map/transform.hpp> -#include <mbgl/math/clamp.hpp> -#include <mbgl/util/tile_cover.hpp> -#include <mbgl/util/tile_range.hpp> +#include <mbgl/renderer/tile_pyramid.hpp> #include <mbgl/util/enum.hpp> #include <mbgl/util/logging.hpp> - -#include <mbgl/algorithm/update_renderables.hpp> - -#include <mapbox/geometry/envelope.hpp> - -#include <cmath> -#include <algorithm> +#include <mbgl/util/tile_cover.hpp> +#include <mbgl/util/tile_range.hpp> namespace mbgl { @@ -23,9 +20,7 @@ using namespace style; static TileObserver nullObserver; -TilePyramid::TilePyramid() - : observer(&nullObserver) { -} +TilePyramid::TilePyramid() : observer(&nullObserver) {} TilePyramid::~TilePyramid() = default; @@ -84,9 +79,8 @@ void TilePyramid::update(const std::vector<Immutable<style::LayerProperties>>& l handleWrapJump(parameters.transformState.getLatLng().longitude()); - const auto type = sourceImpl.type; // Determine the overzooming/underzooming amounts and required tiles. - int32_t overscaledZoom = util::coveringZoomLevel(parameters.transformState.getZoom(), type, tileSize); + int32_t overscaledZoom = sourceImpl.getCoveringZoomLevel(parameters.transformState.getZoom()); int32_t tileZoom = overscaledZoom; int32_t panZoom = zoomRange.max; @@ -101,14 +95,13 @@ void TilePyramid::update(const std::vector<Immutable<style::LayerProperties>>& l if (overscaledZoom >= zoomRange.min) { int32_t idealZoom = std::min<int32_t>(zoomRange.max, overscaledZoom); - // Make sure we're not reparsing overzoomed raster tiles. - if (type == SourceType::Raster) { + if (sourceImpl.getTypeInfo()->tileKind == TileKind::Raster) { tileZoom = idealZoom; } // Only attempt prefetching in continuous mode. - if (parameters.mode == MapMode::Continuous && type != style::SourceType::GeoJSON && type != style::SourceType::Annotations) { + if (parameters.mode == MapMode::Continuous && sourceImpl.getTypeInfo()->supportsTilePrefetch) { // Request lower zoom level tiles (if configured to do so) in an attempt // to show something on the screen faster at the cost of a little of bandwidth. const uint8_t prefetchZoomDelta = @@ -123,8 +116,8 @@ void TilePyramid::update(const std::vector<Immutable<style::LayerProperties>>& l } idealTiles = util::tileCover(parameters.transformState, idealZoom, tileZoom); - if (parameters.mode == MapMode::Tile && type != SourceType::Raster && type != SourceType::RasterDEM && - idealTiles.size() > 1) { + if (parameters.mode == MapMode::Tile && sourceImpl.getTypeInfo()->tileKind != TileKind::Raster && + sourceImpl.getTypeInfo()->tileKind != TileKind::RasterDEM && idealTiles.size() > 1) { mbgl::Log::Warning(mbgl::Event::General, "Provided camera options returned %zu tiles, only %s is taken in Tile mode.", idealTiles.size(), @@ -212,7 +205,8 @@ void TilePyramid::update(const std::vector<Immutable<style::LayerProperties>>& l } } - if (type != SourceType::Annotations) { + // TODO: can this be more generic than != "annotations"? + if (strcmp(sourceImpl.getTypeInfo()->type, "annotations") != 0) { size_t conservativeCacheSize = std::max(static_cast<float>(parameters.transformState.getSize().width) / tileSize, 1.0f) * std::max(static_cast<float>(parameters.transformState.getSize().height) / tileSize, 1.0f) * @@ -303,9 +297,12 @@ void TilePyramid::handleWrapJump(float lng) { } std::unordered_map<std::string, std::vector<Feature>> TilePyramid::queryRenderedFeatures( - const ScreenLineString& geometry, const TransformState& transformState, - const std::unordered_map<std::string, const RenderLayer*>& layers, const RenderedQueryOptions& options, - const mat4& projMatrix, const SourceFeatureState& featureState) const { + const ScreenLineString& geometry, + const TransformState& transformState, + const std::unordered_map<std::string, const RenderLayer*>& layers, + const RenderedQueryOptions& options, + const mat4& projMatrix, + const SourceFeatureState& featureState) const { std::unordered_map<std::string, std::vector<Feature>> result; if (renderedTiles.empty() || geometry.empty()) { return result; @@ -315,18 +312,19 @@ std::unordered_map<std::string, std::vector<Feature>> TilePyramid::queryRendered queryGeometry.reserve(geometry.size()); for (const auto& p : geometry) { - queryGeometry.push_back(TileCoordinate::fromScreenCoordinate( - transformState, 0, { p.x, transformState.getSize().height - p.y }).p); + queryGeometry.push_back( + TileCoordinate::fromScreenCoordinate(transformState, 0, {p.x, transformState.getSize().height - p.y}).p); } mapbox::geometry::box<double> box = mapbox::geometry::envelope(queryGeometry); auto cmp = [](const UnwrappedTileID& a, const UnwrappedTileID& b) { return std::tie(a.canonical.z, a.canonical.y, a.wrap, a.canonical.x) < - std::tie(b.canonical.z, b.canonical.y, b.wrap, b.canonical.x); + std::tie(b.canonical.z, b.canonical.y, b.wrap, b.canonical.x); }; - std::map<UnwrappedTileID, std::reference_wrapper<Tile>, decltype(cmp)> sortedTiles{renderedTiles.begin(), renderedTiles.end(), cmp}; + std::map<UnwrappedTileID, std::reference_wrapper<Tile>, decltype(cmp)> sortedTiles{ + renderedTiles.begin(), renderedTiles.end(), cmp}; auto maxPitchScaleFactor = transformState.maxPitchScaleFactor(); @@ -334,11 +332,14 @@ std::unordered_map<std::string, std::vector<Feature>> TilePyramid::queryRendered const UnwrappedTileID& id = entry.first; Tile& tile = entry.second; - const float scale = transformState.getScale() / (1 << id.canonical.z); // equivalent to std::pow(2, transformState.getZoom() - id.canonical.z); + const float scale = + transformState.getScale() / + (1 << id.canonical.z); // equivalent to std::pow(2, transformState.getZoom() - id.canonical.z); auto queryPadding = maxPitchScaleFactor * tile.getQueryPadding(layers) * util::EXTENT / util::tileSize / scale; GeometryCoordinate tileSpaceBoundsMin = TileCoordinate::toGeometryCoordinate(id, box.min); - if (tileSpaceBoundsMin.x - queryPadding >= util::EXTENT || tileSpaceBoundsMin.y - queryPadding >= util::EXTENT) { + if (tileSpaceBoundsMin.x - queryPadding >= util::EXTENT || + tileSpaceBoundsMin.y - queryPadding >= util::EXTENT) { continue; } @@ -353,8 +354,8 @@ std::unordered_map<std::string, std::vector<Feature>> TilePyramid::queryRendered tileSpaceQueryGeometry.push_back(TileCoordinate::toGeometryCoordinate(id, c)); } - tile.queryRenderedFeatures(result, tileSpaceQueryGeometry, transformState, layers, options, projMatrix, - featureState); + tile.queryRenderedFeatures( + result, tileSpaceQueryGeometry, transformState, layers, options, projMatrix, featureState); } return result; diff --git a/src/mbgl/style/layer.cpp b/src/mbgl/style/layer.cpp index 428bcc9a5a..b1c14191be 100644 --- a/src/mbgl/style/layer.cpp +++ b/src/mbgl/style/layer.cpp @@ -12,11 +12,11 @@ namespace mbgl { namespace style { -static_assert(mbgl::underlying_type(Tile::Kind::Geometry) == mbgl::underlying_type(LayerTypeInfo::TileKind::Geometry), +static_assert(mbgl::underlying_type(TileKind::Geometry) == mbgl::underlying_type(LayerTypeInfo::TileKind::Geometry), "tile kind error"); -static_assert(mbgl::underlying_type(Tile::Kind::Raster) == mbgl::underlying_type(LayerTypeInfo::TileKind::Raster), +static_assert(mbgl::underlying_type(TileKind::Raster) == mbgl::underlying_type(LayerTypeInfo::TileKind::Raster), "tile kind error"); -static_assert(mbgl::underlying_type(Tile::Kind::RasterDEM) == mbgl::underlying_type(LayerTypeInfo::TileKind::RasterDEM), +static_assert(mbgl::underlying_type(TileKind::RasterDEM) == mbgl::underlying_type(LayerTypeInfo::TileKind::RasterDEM), "tile kind error"); static LayerObserver nullObserver; diff --git a/src/mbgl/style/source.cpp b/src/mbgl/style/source.cpp index b2a9473fe6..db195867c0 100644 --- a/src/mbgl/style/source.cpp +++ b/src/mbgl/style/source.cpp @@ -2,23 +2,17 @@ #include <mbgl/style/source_impl.hpp> #include <mbgl/style/source_observer.hpp> #include <mbgl/util/logging.hpp> +#include <mbgl/util/tileset.hpp> namespace mbgl { namespace style { static SourceObserver nullObserver; -Source::Source(Immutable<Impl> impl) - : baseImpl(std::move(impl)), - observer(&nullObserver) { -} +Source::Source(Immutable<Impl> impl) : baseImpl(std::move(impl)), observer(&nullObserver) {} Source::~Source() = default; -SourceType Source::getType() const { - return baseImpl->type; -} - std::string Source::getID() const { return baseImpl->id; } @@ -79,10 +73,73 @@ optional<uint8_t> Source::getMaxOverscaleFactorForParentTiles() const noexcept { return baseImpl->getMaxOverscaleFactorForParentTiles(); } +int32_t Source::getCoveringZoomLevel(double z) const noexcept { + return baseImpl->getCoveringZoomLevel(z); +} + void Source::dumpDebugLogs() const { Log::Info(Event::General, "Source::id: %s", getID().c_str()); Log::Info(Event::General, "Source::loaded: %d", loaded); } +const SourceTypeInfo* Source::getTypeInfo() const noexcept { + return baseImpl->getTypeInfo(); +} + +Value Source::serialize() const { + mapbox::base::ValueObject result; + result.insert(std::make_pair("id", getID())); + result.insert(std::make_pair("type", getTypeInfo()->type)); + return result; +} + +std::string tilesetSchemeToString(mbgl::Tileset::Scheme scheme) { + switch (scheme) { + case mbgl::Tileset::Scheme::XYZ: + return "xyz"; + case mbgl::Tileset::Scheme::TMS: + return "tms"; + } + + return "xyz"; +} + +void Source::serializeTileSet(Value& value, const mbgl::Tileset& tileset) const { + assert(value.getObject()); + const auto json = value.getObject(); + std::vector<mapbox::base::Value> tiles; + tiles.reserve(tileset.tiles.size()); + for (const auto& tile : tileset.tiles) tiles.emplace_back(tile); + + json->insert({"tiles", tiles}); + json->insert({"minzoom", tileset.zoomRange.min}); + json->insert({"maxzoom", tileset.zoomRange.max}); + json->insert({"scheme", tilesetSchemeToString(tileset.scheme)}); + + if (tileset.bounds.has_value()) { + json->insert({"bounds", + std::vector<mapbox::base::Value>{tileset.bounds->southwest().longitude(), + tileset.bounds->southwest().latitude(), + tileset.bounds->northeast().longitude(), + tileset.bounds->northeast().latitude()}}); + } +} + +void Source::serializeUrlOrTileSet(Value& value, const mbgl::variant<std::string, mbgl::Tileset>* urlOrTileSet) const { + assert(value.getObject()); + + if (!urlOrTileSet) return; + + urlOrTileSet->match( + [&](const std::string& url) { + value.getObject()->insert({"url", url}); + }, + [&](const mbgl::Tileset& tileset) { serializeTileSet(value, tileset); }); +} + +uint16_t Source::getTileSize() const { + return baseImpl->getTileSize(); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/source_impl.cpp b/src/mbgl/style/source_impl.cpp index 98433a8c86..b4d3292884 100644 --- a/src/mbgl/style/source_impl.cpp +++ b/src/mbgl/style/source_impl.cpp @@ -1,3 +1,4 @@ +#include <mbgl/math/log2.hpp> #include <mbgl/style/source_impl.hpp> #include <mbgl/util/constants.hpp> #include <mbgl/util/logging.hpp> @@ -14,10 +15,7 @@ void WarnIfOverscaleFactorCapsPrefetchDelta(const optional<uint8_t>& overscale, } } // namespace -Source::Impl::Impl(SourceType type_, std::string id_) - : type(type_), - id(std::move(id_)) { -} +Source::Impl::Impl(std::string id_) : id(std::move(id_)) {} void Source::Impl::setPrefetchZoomDelta(optional<uint8_t> delta) noexcept { prefetchZoomDelta = std::move(delta); @@ -37,5 +35,14 @@ optional<uint8_t> Source::Impl::getMaxOverscaleFactorForParentTiles() const noex return maxOverscaleFactor; } +int32_t Source::Impl::getCoveringZoomLevel(double zoom) const noexcept { + zoom += util::log2(util::tileSize / getTileSize()); + if (getTypeInfo()->tileKind == TileKind::Raster) { + return ::round(zoom); + } else { + return std::floor(zoom); + } +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/source_impl.hpp b/src/mbgl/style/source_impl.hpp index 87a30dcf95..57e0ea5710 100644 --- a/src/mbgl/style/source_impl.hpp +++ b/src/mbgl/style/source_impl.hpp @@ -1,7 +1,6 @@ #pragma once #include <mbgl/style/source.hpp> - #include <string> namespace mbgl { @@ -18,9 +17,13 @@ public: Impl& operator=(const Impl&) = delete; + virtual uint16_t getTileSize() const { return util::tileSize; }; + virtual optional<std::string> getAttribution() const = 0; + void setPrefetchZoomDelta(optional<uint8_t> delta) noexcept; optional<uint8_t> getPrefetchZoomDelta() const noexcept; + void setMinimumTileUpdateInterval(Duration interval) { minimumTileUpdateInterval = interval; } Duration getMinimumTileUpdateInterval() const { return minimumTileUpdateInterval; } void setMaxOverscaleFactorForParentTiles(optional<uint8_t> overscaleFactor) noexcept; @@ -28,7 +31,12 @@ public: bool isVolatile() const { return volatileFlag; } void setVolatile(bool set) { volatileFlag = set; } - const SourceType type; + + // Returns pointer to the statically allocated source type info structure. + virtual const SourceTypeInfo* getTypeInfo() const noexcept = 0; + + int32_t getCoveringZoomLevel(double zoom) const noexcept; + const std::string id; protected: @@ -37,9 +45,14 @@ protected: Duration minimumTileUpdateInterval{Duration::zero()}; bool volatileFlag = false; - Impl(SourceType, std::string); + explicit Impl(std::string); Impl(const Impl&) = default; }; +// To be used in the inherited classes. +#define DECLARE_SOURCE_TYPE_INFO \ + const mbgl::style::SourceTypeInfo* getTypeInfo() const noexcept override { return staticTypeInfo(); } \ + static const mbgl::style::SourceTypeInfo* staticTypeInfo() noexcept + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/custom_geometry_source.cpp b/src/mbgl/style/sources/custom_geometry_source.cpp index 9d3a58c151..278d4d5a72 100644 --- a/src/mbgl/style/sources/custom_geometry_source.cpp +++ b/src/mbgl/style/sources/custom_geometry_source.cpp @@ -32,7 +32,7 @@ void CustomGeometrySource::loadDescription(FileSource&) { } bool CustomGeometrySource::supportsLayerType(const mbgl::style::LayerTypeInfo* info) const { - return mbgl::underlying_type(Tile::Kind::Geometry) == mbgl::underlying_type(info->tileKind); + return mbgl::underlying_type(TileKind::Geometry) == mbgl::underlying_type(info->tileKind); } void CustomGeometrySource::setTileData(const CanonicalTileID& tileID, diff --git a/src/mbgl/style/sources/custom_geometry_source_impl.cpp b/src/mbgl/style/sources/custom_geometry_source_impl.cpp index 184718fd76..59f25a3d2d 100644 --- a/src/mbgl/style/sources/custom_geometry_source_impl.cpp +++ b/src/mbgl/style/sources/custom_geometry_source_impl.cpp @@ -5,7 +5,7 @@ namespace mbgl { namespace style { CustomGeometrySource::Impl::Impl(std::string id_, const CustomGeometrySource::Options& options) - : Source::Impl(SourceType::CustomVector, std::move(id_)), + : Source::Impl(std::move(id_)), tileOptions(makeMutable<CustomGeometrySource::TileOptions>(options.tileOptions)), zoomRange(options.zoomRange), loaderRef({}) {} @@ -33,5 +33,10 @@ optional<ActorRef<CustomTileLoader>> CustomGeometrySource::Impl::getTileLoader() return loaderRef; } +const SourceTypeInfo* CustomGeometrySource::Impl::staticTypeInfo() noexcept { + const static SourceTypeInfo typeInfo{"customvector", true, nullopt}; + return &typeInfo; +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/custom_geometry_source_impl.hpp b/src/mbgl/style/sources/custom_geometry_source_impl.hpp index a441b38f69..efacdbedda 100644 --- a/src/mbgl/style/sources/custom_geometry_source_impl.hpp +++ b/src/mbgl/style/sources/custom_geometry_source_impl.hpp @@ -20,6 +20,8 @@ public: optional<ActorRef<CustomTileLoader>> getTileLoader() const; bool operator!=(const Impl&) const noexcept; + DECLARE_SOURCE_TYPE_INFO; + private: Immutable<CustomGeometrySource::TileOptions> tileOptions; Range<uint8_t> zoomRange; diff --git a/src/mbgl/style/sources/geojson_source.cpp b/src/mbgl/style/sources/geojson_source.cpp index d750d6b1d2..8bca19af35 100644 --- a/src/mbgl/style/sources/geojson_source.cpp +++ b/src/mbgl/style/sources/geojson_source.cpp @@ -61,6 +61,24 @@ void GeoJSONSource::setGeoJSONData(std::shared_ptr<GeoJSONData> geoJSONData) { observer->onSourceChanged(*this); } +void GeoJSONSource::setSourceData(SourceData data) { + if (data.url) { + setURL(*data.url); + } else if (data.geoJSON) { + setGeoJSON(*data.geoJSON); + } else if (data.geoJSONData) { + setGeoJSONData(std::move(data.geoJSONData)); + } +} + +SourceDataResult GeoJSONSource::getSourceData() const { + SourceDataResult result{impl().getData()}; + if (url) { + result.url = &*url; + } + return result; +} + optional<std::string> GeoJSONSource::getURL() const { return url; } @@ -81,13 +99,11 @@ void GeoJSONSource::loadDescription(FileSource& fileSource) { req = fileSource.request(Resource::source(*url), [this](const Response& res) { if (res.error) { - observer->onSourceError( - *this, std::make_exception_ptr(std::runtime_error(res.error->message))); + observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(res.error->message))); } else if (res.notModified) { return; } else if (res.noContent) { - observer->onSourceError( - *this, std::make_exception_ptr(std::runtime_error("unexpectedly empty GeoJSON"))); + observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error("unexpectedly empty GeoJSON"))); } else { auto makeImplInBackground = [currentImpl = baseImpl, data = res.data]() -> Immutable<Source::Impl> { assert(data); @@ -117,12 +133,23 @@ void GeoJSONSource::loadDescription(FileSource& fileSource) { } bool GeoJSONSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info) const { - return mbgl::underlying_type(Tile::Kind::Geometry) == mbgl::underlying_type(info->tileKind); + return mbgl::underlying_type(TileKind::Geometry) == mbgl::underlying_type(info->tileKind); } Mutable<Source::Impl> GeoJSONSource::createMutable() const noexcept { return staticMutableCast<Source::Impl>(makeMutable<Impl>(impl())); } +Value GeoJSONSource::serialize() const { + auto value = Source::serialize(); + assert(value.getObject()); + + if (url.has_value()) { + value.getObject()->insert({"data", url.value()}); + } + + return value; +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/geojson_source_impl.cpp b/src/mbgl/style/sources/geojson_source_impl.cpp index fb33eee8f9..7193af0f1b 100644 --- a/src/mbgl/style/sources/geojson_source_impl.cpp +++ b/src/mbgl/style/sources/geojson_source_impl.cpp @@ -20,13 +20,11 @@ class GeoJSONVTData final : public GeoJSONData { [id, impl = this->impl]() -> TileFeatures { return impl->getTile(id.z, id.x, id.y).features; }, fn); } - Features getChildren(const std::uint32_t) final { return {}; } + Features getChildren(const std::uint32_t) const final { return {}; } - Features getLeaves(const std::uint32_t, const std::uint32_t, const std::uint32_t) final { return {}; } + Features getLeaves(const std::uint32_t, const std::uint32_t, const std::uint32_t) const final { return {}; } - std::uint8_t getClusterExpansionZoom(std::uint32_t) final { - return 0; - } + std::uint8_t getClusterExpansionZoom(std::uint32_t) const final { return 0; } std::shared_ptr<Scheduler> getScheduler() final { return scheduler; } @@ -48,13 +46,15 @@ class SuperclusterData final : public GeoJSONData { fn(impl.getTile(id.z, id.x, id.y)); } - Features getChildren(const std::uint32_t cluster_id) final { return impl.getChildren(cluster_id); } + Features getChildren(const std::uint32_t cluster_id) const final { return impl.getChildren(cluster_id); } - Features getLeaves(const std::uint32_t cluster_id, const std::uint32_t limit, const std::uint32_t offset) final { + Features getLeaves(const std::uint32_t cluster_id, + const std::uint32_t limit, + const std::uint32_t offset) const final { return impl.getLeaves(cluster_id, limit, offset); } - std::uint8_t getClusterExpansionZoom(std::uint32_t cluster_id) final { + std::uint8_t getClusterExpansionZoom(std::uint32_t cluster_id) const final { return impl.getClusterExpansionZoom(cluster_id); } @@ -122,7 +122,7 @@ std::shared_ptr<GeoJSONData> GeoJSONData::create(const GeoJSON& geoJSON, } GeoJSONSource::Impl::Impl(std::string id_, Immutable<GeoJSONOptions> options_) - : Source::Impl(SourceType::GeoJSON, std::move(id_)), options(std::move(options_)) {} + : Source::Impl(std::move(id_)), options(std::move(options_)) {} GeoJSONSource::Impl::Impl(const GeoJSONSource::Impl& other, std::shared_ptr<GeoJSONData> data_) : Source::Impl(other), options(other.options), data(std::move(data_)) {} @@ -141,5 +141,10 @@ optional<std::string> GeoJSONSource::Impl::getAttribution() const { return {}; } +const SourceTypeInfo* GeoJSONSource::Impl::staticTypeInfo() noexcept { + const static SourceTypeInfo typeInfo{"geojson", false, TileKind::Geometry}; + return &typeInfo; +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/geojson_source_impl.hpp b/src/mbgl/style/sources/geojson_source_impl.hpp index fd23ebdbe1..67b419bee9 100644 --- a/src/mbgl/style/sources/geojson_source_impl.hpp +++ b/src/mbgl/style/sources/geojson_source_impl.hpp @@ -23,6 +23,8 @@ public: optional<std::string> getAttribution() const final; + DECLARE_SOURCE_TYPE_INFO; + private: Immutable<GeoJSONOptions> options; std::shared_ptr<GeoJSONData> data; diff --git a/src/mbgl/style/sources/image_source.cpp b/src/mbgl/style/sources/image_source.cpp index 144d007cc5..a1bc719c0c 100644 --- a/src/mbgl/style/sources/image_source.cpp +++ b/src/mbgl/style/sources/image_source.cpp @@ -50,6 +50,28 @@ void ImageSource::setImage(PremultipliedImage&& image_) { observer->onSourceChanged(*this); } +void ImageSource::setSourceData(SourceData data) { + if (data.url) { + setURL(*data.url); + } else if (data.image) { + setImage(std::move(*data.image)); + } +} + +SourceDataResult ImageSource::getSourceData() const { + SourceDataResult result{}; + if (url) { + result.url = &*url; + } + + auto image = impl().getImage(); + if (image) { + result.image = image; + } + + return result; +} + optional<std::string> ImageSource::getURL() const { return url; } @@ -84,12 +106,31 @@ void ImageSource::loadDescription(FileSource& fileSource) { } bool ImageSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info) const { - return mbgl::underlying_type(Tile::Kind::Raster) == mbgl::underlying_type(info->tileKind); + return mbgl::underlying_type(TileKind::Raster) == mbgl::underlying_type(info->tileKind); } Mutable<Source::Impl> ImageSource::createMutable() const noexcept { return staticMutableCast<Source::Impl>(makeMutable<Impl>(impl())); } +Value ImageSource::serialize() const { + auto value = Source::serialize(); + assert(value.getObject()); + auto object = value.getObject(); + + if (url) object->insert({"url", url.value()}); + + const auto source_coordinates = getCoordinates(); + std::vector<mapbox::base::Value> coordinates; + coordinates.reserve(source_coordinates.size()); + + for (const auto& c : source_coordinates) { + coordinates.emplace_back(std::vector<mapbox::base::Value>{c.longitude(), c.latitude()}); + } + object->insert({"coordinates", coordinates}); + + return value; +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/image_source_impl.cpp b/src/mbgl/style/sources/image_source_impl.cpp index 7a4f5ac01f..41255b3bb0 100644 --- a/src/mbgl/style/sources/image_source_impl.cpp +++ b/src/mbgl/style/sources/image_source_impl.cpp @@ -5,16 +5,13 @@ namespace mbgl { namespace style { ImageSource::Impl::Impl(std::string id_, std::array<LatLng, 4> coords_) - : Source::Impl(SourceType::Image, std::move(id_)), coords(coords_) {} + : Source::Impl(std::move(id_)), coords(coords_) {} ImageSource::Impl::Impl(const Impl& other, std::array<LatLng, 4> coords_) : Source::Impl(other), coords(coords_), image(other.image) {} ImageSource::Impl::Impl(const Impl& rhs, PremultipliedImage&& image_) - : Source::Impl(rhs), - coords(rhs.coords), - image(std::make_shared<PremultipliedImage>(std::move(image_))) { -} + : Source::Impl(rhs), coords(rhs.coords), image(std::make_shared<PremultipliedImage>(std::move(image_))) {} ImageSource::Impl::~Impl() = default; std::shared_ptr<PremultipliedImage> ImageSource::Impl::getImage() const { @@ -29,5 +26,10 @@ optional<std::string> ImageSource::Impl::getAttribution() const { return {}; } +const SourceTypeInfo* ImageSource::Impl::staticTypeInfo() noexcept { + const static SourceTypeInfo typeInfo{"image", true, nullopt}; + return &typeInfo; +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/image_source_impl.hpp b/src/mbgl/style/sources/image_source_impl.hpp index 16ddcfda71..d35891e27b 100644 --- a/src/mbgl/style/sources/image_source_impl.hpp +++ b/src/mbgl/style/sources/image_source_impl.hpp @@ -21,6 +21,9 @@ public: std::array<LatLng, 4> getCoordinates() const; optional<std::string> getAttribution() const final; + + DECLARE_SOURCE_TYPE_INFO; + private: std::array<LatLng, 4> coords; std::shared_ptr<PremultipliedImage> image; diff --git a/src/mbgl/style/sources/raster_dem_source.cpp b/src/mbgl/style/sources/raster_dem_source.cpp index 98a83ee870..a7efc2ba69 100644 --- a/src/mbgl/style/sources/raster_dem_source.cpp +++ b/src/mbgl/style/sources/raster_dem_source.cpp @@ -3,19 +3,49 @@ #include <mbgl/style/layer.hpp> #include <mbgl/style/source_observer.hpp> #include <mbgl/style/sources/raster_dem_source.hpp> -#include <mbgl/style/sources/raster_source_impl.hpp> +#include <mbgl/style/sources/raster_dem_source_impl.hpp> #include <mbgl/tile/tile.hpp> -#include <mbgl/util/mapbox.hpp> #include <utility> namespace mbgl { namespace style { RasterDEMSource::RasterDEMSource(std::string id, variant<std::string, Tileset> urlOrTileset_, uint16_t tileSize) - : RasterSource(std::move(id), std::move(urlOrTileset_), tileSize, SourceType::RasterDEM) {} + : RasterSource(makeMutable<Impl>(std::move(id), tileSize), std::move(urlOrTileset_)) {} bool RasterDEMSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info) const { - return mbgl::underlying_type(Tile::Kind::RasterDEM) == mbgl::underlying_type(info->tileKind); + return mbgl::underlying_type(TileKind::RasterDEM) == mbgl::underlying_type(info->tileKind); +} + +static std::string tilesetDEMEncodingToString(mbgl::Tileset::DEMEncoding encoding) { + switch (encoding) { + case mbgl::Tileset::DEMEncoding::Mapbox: + return "mapbox"; + case mbgl::Tileset::DEMEncoding::Terrarium: + return "terrarium"; + } + return "terrarium"; +} + +Value RasterDEMSource::serialize() const { + auto value = RasterSource::serialize(); + getURLOrTileset()->match([](const std::string&) {}, + [&](const mbgl::Tileset& tileset) { + value.getObject()->insert({"encoding", tilesetDEMEncodingToString(tileset.encoding)}); + }); + return value; +} + +const RasterDEMSource::Impl& RasterDEMSource::impl() const { + return static_cast<const Impl&>(*baseImpl); +} + +Mutable<Source::Impl> RasterDEMSource::createMutable() const noexcept { + return staticMutableCast<Source::Impl>(makeMutable<Impl>(impl())); +} + +Mutable<Source::Impl> RasterDEMSource::createMutable(Tileset tileset) const noexcept { + return staticMutableCast<Source::Impl>(makeMutable<Impl>(impl(), std::move(tileset))); } } // namespace style diff --git a/src/mbgl/style/sources/raster_dem_source_impl.cpp b/src/mbgl/style/sources/raster_dem_source_impl.cpp new file mode 100644 index 0000000000..487c451d83 --- /dev/null +++ b/src/mbgl/style/sources/raster_dem_source_impl.cpp @@ -0,0 +1,16 @@ +#include <mbgl/style/sources/raster_dem_source_impl.hpp> + +namespace mbgl { +namespace style { + +RasterDEMSource::Impl::Impl(std::string id_, uint16_t tileSize_) : RasterSource::Impl(std::move(id_), tileSize_) {} + +RasterDEMSource::Impl::Impl(const Impl& other, Tileset tileset_) : RasterSource::Impl(other, std::move(tileset_)) {} + +const SourceTypeInfo* RasterDEMSource::Impl::staticTypeInfo() noexcept { + const static SourceTypeInfo typeInfo{"raster-dem", true, TileKind::RasterDEM}; + return &typeInfo; +} + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/sources/raster_dem_source_impl.hpp b/src/mbgl/style/sources/raster_dem_source_impl.hpp new file mode 100644 index 0000000000..dc8568766e --- /dev/null +++ b/src/mbgl/style/sources/raster_dem_source_impl.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include <mbgl/style/sources/raster_dem_source.hpp> +#include <mbgl/style/sources/raster_source_impl.hpp> + +namespace mbgl { +namespace style { + +class RasterDEMSource::Impl : public RasterSource::Impl { +public: + Impl(std::string id, uint16_t tileSize); + Impl(const Impl&, Tileset); + + DECLARE_SOURCE_TYPE_INFO; +}; + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/sources/raster_source.cpp b/src/mbgl/style/sources/raster_source.cpp index aa56f9570b..6684b8a52e 100644 --- a/src/mbgl/style/sources/raster_source.cpp +++ b/src/mbgl/style/sources/raster_source.cpp @@ -13,10 +13,11 @@ namespace mbgl { namespace style { -RasterSource::RasterSource(std::string id, variant<std::string, Tileset> urlOrTileset_, uint16_t tileSize, SourceType sourceType) - : Source(makeMutable<Impl>(sourceType, std::move(id), tileSize)), - urlOrTileset(std::move(urlOrTileset_)) { -} +RasterSource::RasterSource(std::string id, variant<std::string, Tileset> urlOrTileset_, uint16_t tileSize) + : Source(makeMutable<Impl>(std::move(id), tileSize)), urlOrTileset(std::move(urlOrTileset_)) {} + +RasterSource::RasterSource(Immutable<Impl>&& impl, variant<std::string, Tileset> urlOrTileset_) + : Source(std::move(impl)), urlOrTileset(std::move(urlOrTileset_)) {} RasterSource::~RasterSource() = default; @@ -24,8 +25,8 @@ const RasterSource::Impl& RasterSource::impl() const { return static_cast<const Impl&>(*baseImpl); } -const variant<std::string, Tileset>& RasterSource::getURLOrTileset() const { - return urlOrTileset; +const variant<std::string, Tileset>* RasterSource::getURLOrTileset() const { + return &urlOrTileset; } optional<std::string> RasterSource::getURL() const { @@ -36,13 +37,9 @@ optional<std::string> RasterSource::getURL() const { return urlOrTileset.get<std::string>(); } -uint16_t RasterSource::getTileSize() const { - return impl().getTileSize(); -} - void RasterSource::loadDescription(FileSource& fileSource) { if (urlOrTileset.is<Tileset>()) { - baseImpl = makeMutable<Impl>(impl(), urlOrTileset.get<Tileset>()); + baseImpl = createMutable(urlOrTileset.get<Tileset>()); loaded = true; observer->onSourceLoaded(*this); return; @@ -68,10 +65,10 @@ void RasterSource::loadDescription(FileSource& fileSource) { return; } - util::mapbox::canonicalizeTileset(*tileset, url, getType(), getTileSize()); + util::mapbox::canonicalizeTileset(*tileset, url, *this); bool changed = impl().tileset != *tileset; - baseImpl = makeMutable<Impl>(impl(), *tileset); + baseImpl = createMutable(*tileset); loaded = true; observer->onSourceLoaded(*this); @@ -84,12 +81,22 @@ void RasterSource::loadDescription(FileSource& fileSource) { } bool RasterSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info) const { - return mbgl::underlying_type(Tile::Kind::Raster) == mbgl::underlying_type(info->tileKind); + return mbgl::underlying_type(TileKind::Raster) == mbgl::underlying_type(info->tileKind); } Mutable<Source::Impl> RasterSource::createMutable() const noexcept { return staticMutableCast<Source::Impl>(makeMutable<Impl>(impl())); } +Mutable<Source::Impl> RasterSource::createMutable(Tileset tileset) const noexcept { + return staticMutableCast<Source::Impl>(makeMutable<Impl>(impl(), std::move(tileset))); +} + +Value RasterSource::serialize() const { + auto value = Source::serialize(); + serializeUrlOrTileSet(value, getURLOrTileset()); + return value; +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/raster_source_impl.cpp b/src/mbgl/style/sources/raster_source_impl.cpp index 4201fd0578..b18a34412b 100644 --- a/src/mbgl/style/sources/raster_source_impl.cpp +++ b/src/mbgl/style/sources/raster_source_impl.cpp @@ -3,16 +3,10 @@ namespace mbgl { namespace style { -RasterSource::Impl::Impl(SourceType sourceType, std::string id_, uint16_t tileSize_) - : Source::Impl(sourceType, std::move(id_)), - tileSize(tileSize_) { -} +RasterSource::Impl::Impl(std::string id_, uint16_t tileSize_) : Source::Impl(std::move(id_)), tileSize(tileSize_) {} RasterSource::Impl::Impl(const Impl& other, Tileset tileset_) - : Source::Impl(other), - tileset(std::move(tileset_)), - tileSize(other.tileSize) { -} + : Source::Impl(other), tileset(std::move(tileset_)), tileSize(other.tileSize) {} uint16_t RasterSource::Impl::getTileSize() const { return tileSize; @@ -25,5 +19,10 @@ optional<std::string> RasterSource::Impl::getAttribution() const { return tileset->attribution; } +const SourceTypeInfo* RasterSource::Impl::staticTypeInfo() noexcept { + const static SourceTypeInfo typeInfo{"raster", true, TileKind::Raster}; + return &typeInfo; +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/raster_source_impl.hpp b/src/mbgl/style/sources/raster_source_impl.hpp index bb58455140..716d044f6f 100644 --- a/src/mbgl/style/sources/raster_source_impl.hpp +++ b/src/mbgl/style/sources/raster_source_impl.hpp @@ -1,22 +1,24 @@ #pragma once -#include <mbgl/style/sources/raster_source.hpp> #include <mbgl/style/source_impl.hpp> +#include <mbgl/style/sources/raster_source.hpp> namespace mbgl { namespace style { class RasterSource::Impl : public Source::Impl { public: - Impl(SourceType sourceType, std::string id, uint16_t tileSize); + Impl(std::string id, uint16_t tileSize); Impl(const Impl&, Tileset); - uint16_t getTileSize() const; + uint16_t getTileSize() const override; optional<std::string> getAttribution() const final; const optional<Tileset> tileset; + DECLARE_SOURCE_TYPE_INFO; + private: uint16_t tileSize; }; diff --git a/src/mbgl/style/sources/vector_source.cpp b/src/mbgl/style/sources/vector_source.cpp index b482449fbf..c08be53e6f 100644 --- a/src/mbgl/style/sources/vector_source.cpp +++ b/src/mbgl/style/sources/vector_source.cpp @@ -10,11 +10,14 @@ #include <mbgl/util/constants.hpp> #include <mbgl/util/exception.hpp> #include <mbgl/util/mapbox.hpp> +#include <mbgl/util/util.hpp> namespace mbgl { namespace style { -VectorSource::VectorSource(std::string id, variant<std::string, Tileset> urlOrTileset_, optional<float> maxZoom_, +VectorSource::VectorSource(std::string id, + variant<std::string, Tileset> urlOrTileset_, + optional<float> maxZoom_, optional<float> minZoom_) : Source(makeMutable<Impl>(std::move(id))), urlOrTileset(std::move(urlOrTileset_)), @@ -27,8 +30,8 @@ const VectorSource::Impl& VectorSource::impl() const { return static_cast<const Impl&>(*baseImpl); } -const variant<std::string, Tileset>& VectorSource::getURLOrTileset() const { - return urlOrTileset; +const variant<std::string, Tileset>* VectorSource::getURLOrTileset() const { + return &urlOrTileset; } optional<std::string> VectorSource::getURL() const { @@ -72,7 +75,7 @@ void VectorSource::loadDescription(FileSource& fileSource) { if (minZoom) { tileset->zoomRange.min = *minZoom; } - util::mapbox::canonicalizeTileset(*tileset, url, getType(), util::tileSize); + util::mapbox::canonicalizeTileset(*tileset, url, *this); bool changed = impl().tileset != *tileset; baseImpl = makeMutable<Impl>(impl(), *tileset); @@ -88,12 +91,18 @@ void VectorSource::loadDescription(FileSource& fileSource) { } bool VectorSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info) const { - return mbgl::underlying_type(Tile::Kind::Geometry) == mbgl::underlying_type(info->tileKind); + return mbgl::underlying_type(TileKind::Geometry) == mbgl::underlying_type(info->tileKind); } Mutable<Source::Impl> VectorSource::createMutable() const noexcept { return staticMutableCast<Source::Impl>(makeMutable<Impl>(impl())); } +Value VectorSource::serialize() const { + auto result = Source::serialize(); + serializeUrlOrTileSet(result, getURLOrTileset()); + return result; +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/vector_source_impl.cpp b/src/mbgl/style/sources/vector_source_impl.cpp index 8f85a41ddb..172673be8a 100644 --- a/src/mbgl/style/sources/vector_source_impl.cpp +++ b/src/mbgl/style/sources/vector_source_impl.cpp @@ -3,14 +3,9 @@ namespace mbgl { namespace style { -VectorSource::Impl::Impl(std::string id_) - : Source::Impl(SourceType::Vector, std::move(id_)) { -} +VectorSource::Impl::Impl(std::string id_) : Source::Impl(std::move(id_)) {} -VectorSource::Impl::Impl(const Impl& other, Tileset tileset_) - : Source::Impl(other), - tileset(std::move(tileset_)) { -} +VectorSource::Impl::Impl(const Impl& other, Tileset tileset_) : Source::Impl(other), tileset(std::move(tileset_)) {} optional<std::string> VectorSource::Impl::getAttribution() const { if (!tileset) { @@ -19,5 +14,10 @@ optional<std::string> VectorSource::Impl::getAttribution() const { return tileset->attribution; } +const SourceTypeInfo* VectorSource::Impl::staticTypeInfo() noexcept { + const static SourceTypeInfo typeInfo{"vector", true, TileKind::Geometry}; + return &typeInfo; +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/vector_source_impl.hpp b/src/mbgl/style/sources/vector_source_impl.hpp index 4526fbe356..c616fb30a6 100644 --- a/src/mbgl/style/sources/vector_source_impl.hpp +++ b/src/mbgl/style/sources/vector_source_impl.hpp @@ -1,7 +1,7 @@ #pragma once -#include <mbgl/style/sources/vector_source.hpp> #include <mbgl/style/source_impl.hpp> +#include <mbgl/style/sources/vector_source.hpp> namespace mbgl { namespace style { @@ -14,6 +14,8 @@ public: optional<std::string> getAttribution() const final; const optional<Tileset> tileset; + + DECLARE_SOURCE_TYPE_INFO; }; } // namespace style diff --git a/src/mbgl/style/types.cpp b/src/mbgl/style/types.cpp index c4a7b76b66..dda72e11fe 100644 --- a/src/mbgl/style/types.cpp +++ b/src/mbgl/style/types.cpp @@ -5,118 +5,114 @@ namespace mbgl { using namespace style; -MBGL_DEFINE_ENUM(SourceType, { - { SourceType::Vector, "vector" }, - { SourceType::Raster, "raster" }, - { SourceType::GeoJSON, "geojson" }, - { SourceType::Video, "video" }, - { SourceType::Annotations, "annotations" }, - { SourceType::Image, "image" }, - { SourceType::CustomVector, "customvector" } -}); - -MBGL_DEFINE_ENUM(VisibilityType, { - { VisibilityType::Visible, "visible" }, - { VisibilityType::None, "none" }, -}); - -MBGL_DEFINE_ENUM(TranslateAnchorType, { - { TranslateAnchorType::Map, "map" }, - { TranslateAnchorType::Viewport, "viewport" }, -}); - -MBGL_DEFINE_ENUM(RasterResamplingType, { - { RasterResamplingType::Linear, "linear" }, - { RasterResamplingType::Nearest, "nearest" }, -}); - -MBGL_DEFINE_ENUM(HillshadeIlluminationAnchorType, { - { HillshadeIlluminationAnchorType::Map, "map" }, - { HillshadeIlluminationAnchorType::Viewport, "viewport" }, -}); - -MBGL_DEFINE_ENUM(RotateAnchorType, { - { RotateAnchorType::Map, "map" }, - { RotateAnchorType::Viewport, "viewport" }, -}); - -MBGL_DEFINE_ENUM(CirclePitchScaleType, { - { CirclePitchScaleType::Map, "map" }, - { CirclePitchScaleType::Viewport, "viewport" }, -}); - -MBGL_DEFINE_ENUM(LineCapType, { - { LineCapType::Round, "round" }, - { LineCapType::Butt, "butt" }, - { LineCapType::Square, "square" }, -}); - -MBGL_DEFINE_ENUM(LineJoinType, { - { LineJoinType::Miter, "miter" }, - { LineJoinType::Bevel, "bevel" }, - { LineJoinType::Round, "round" }, - { LineJoinType::FakeRound, "fakeround" }, - { LineJoinType::FlipBevel, "flipbevel" }, -}); - -MBGL_DEFINE_ENUM(SymbolPlacementType, { - { SymbolPlacementType::Point, "point" }, - { SymbolPlacementType::Line, "line" }, - { SymbolPlacementType::LineCenter, "line-center" }, -}); - -MBGL_DEFINE_ENUM(SymbolAnchorType, { - { SymbolAnchorType::Center, "center" }, - { SymbolAnchorType::Left, "left" }, - { SymbolAnchorType::Right, "right" }, - { SymbolAnchorType::Top, "top" }, - { SymbolAnchorType::Bottom, "bottom" }, - { SymbolAnchorType::TopLeft, "top-left" }, - { SymbolAnchorType::TopRight, "top-right" }, - { SymbolAnchorType::BottomLeft, "bottom-left" }, - { SymbolAnchorType::BottomRight, "bottom-right" } -}); - -MBGL_DEFINE_ENUM(SymbolZOrderType, { - { SymbolZOrderType::Auto, "auto" }, - { SymbolZOrderType::ViewportY, "viewport-y" }, - { SymbolZOrderType::Source, "source" } -}); - -MBGL_DEFINE_ENUM(TextJustifyType, { - { TextJustifyType::Auto, "auto" }, - { TextJustifyType::Center, "center" }, - { TextJustifyType::Left, "left" }, - { TextJustifyType::Right, "right" }, -}); - -MBGL_DEFINE_ENUM(TextTransformType, { - { TextTransformType::None, "none" }, - { TextTransformType::Uppercase, "uppercase" }, - { TextTransformType::Lowercase, "lowercase" }, -}); - -MBGL_DEFINE_ENUM(TextWritingModeType, { - { TextWritingModeType::Horizontal, "horizontal" }, - { TextWritingModeType::Vertical, "vertical" } -}); - -MBGL_DEFINE_ENUM(AlignmentType, { - { AlignmentType::Map, "map" }, - { AlignmentType::Viewport, "viewport" }, - { AlignmentType::Auto, "auto" }, -}); - -MBGL_DEFINE_ENUM(IconTextFitType, { - { IconTextFitType::None, "none" }, - { IconTextFitType::Both, "both" }, - { IconTextFitType::Width, "width" }, - { IconTextFitType::Height, "height" }, -}); - -MBGL_DEFINE_ENUM(LightAnchorType, { - { LightAnchorType::Map, "map" }, - { LightAnchorType::Viewport, "viewport" } -}); +MBGL_DEFINE_ENUM(VisibilityType, + { + {VisibilityType::Visible, "visible"}, + {VisibilityType::None, "none"}, + }); + +MBGL_DEFINE_ENUM(TranslateAnchorType, + { + {TranslateAnchorType::Map, "map"}, + {TranslateAnchorType::Viewport, "viewport"}, + }); + +MBGL_DEFINE_ENUM(RasterResamplingType, + { + {RasterResamplingType::Linear, "linear"}, + {RasterResamplingType::Nearest, "nearest"}, + }); + +MBGL_DEFINE_ENUM(HillshadeIlluminationAnchorType, + { + {HillshadeIlluminationAnchorType::Map, "map"}, + {HillshadeIlluminationAnchorType::Viewport, "viewport"}, + }); + +MBGL_DEFINE_ENUM(RotateAnchorType, + { + {RotateAnchorType::Map, "map"}, + {RotateAnchorType::Viewport, "viewport"}, + }); + +MBGL_DEFINE_ENUM(CirclePitchScaleType, + { + {CirclePitchScaleType::Map, "map"}, + {CirclePitchScaleType::Viewport, "viewport"}, + }); + +MBGL_DEFINE_ENUM(LineCapType, + { + {LineCapType::Round, "round"}, + {LineCapType::Butt, "butt"}, + {LineCapType::Square, "square"}, + }); + +MBGL_DEFINE_ENUM(LineJoinType, + { + {LineJoinType::Miter, "miter"}, + {LineJoinType::Bevel, "bevel"}, + {LineJoinType::Round, "round"}, + {LineJoinType::FakeRound, "fakeround"}, + {LineJoinType::FlipBevel, "flipbevel"}, + }); + +MBGL_DEFINE_ENUM(SymbolPlacementType, + { + {SymbolPlacementType::Point, "point"}, + {SymbolPlacementType::Line, "line"}, + {SymbolPlacementType::LineCenter, "line-center"}, + }); + +MBGL_DEFINE_ENUM(SymbolAnchorType, + {{SymbolAnchorType::Center, "center"}, + {SymbolAnchorType::Left, "left"}, + {SymbolAnchorType::Right, "right"}, + {SymbolAnchorType::Top, "top"}, + {SymbolAnchorType::Bottom, "bottom"}, + {SymbolAnchorType::TopLeft, "top-left"}, + {SymbolAnchorType::TopRight, "top-right"}, + {SymbolAnchorType::BottomLeft, "bottom-left"}, + {SymbolAnchorType::BottomRight, "bottom-right"}}); + +MBGL_DEFINE_ENUM(SymbolZOrderType, + {{SymbolZOrderType::Auto, "auto"}, + {SymbolZOrderType::ViewportY, "viewport-y"}, + {SymbolZOrderType::Source, "source"}}); + +MBGL_DEFINE_ENUM(TextJustifyType, + { + {TextJustifyType::Auto, "auto"}, + {TextJustifyType::Center, "center"}, + {TextJustifyType::Left, "left"}, + {TextJustifyType::Right, "right"}, + }); + +MBGL_DEFINE_ENUM(TextTransformType, + { + {TextTransformType::None, "none"}, + {TextTransformType::Uppercase, "uppercase"}, + {TextTransformType::Lowercase, "lowercase"}, + }); + +MBGL_DEFINE_ENUM(TextWritingModeType, + {{TextWritingModeType::Horizontal, "horizontal"}, {TextWritingModeType::Vertical, "vertical"}}); + +MBGL_DEFINE_ENUM(AlignmentType, + { + {AlignmentType::Map, "map"}, + {AlignmentType::Viewport, "viewport"}, + {AlignmentType::Auto, "auto"}, + }); + +MBGL_DEFINE_ENUM(IconTextFitType, + { + {IconTextFitType::None, "none"}, + {IconTextFitType::Both, "both"}, + {IconTextFitType::Width, "width"}, + {IconTextFitType::Height, "height"}, + }); + +MBGL_DEFINE_ENUM(LightAnchorType, {{LightAnchorType::Map, "map"}, {LightAnchorType::Viewport, "viewport"}}); } // namespace mbgl diff --git a/src/mbgl/tile/geometry_tile.cpp b/src/mbgl/tile/geometry_tile.cpp index ee08121693..a33863352a 100644 --- a/src/mbgl/tile/geometry_tile.cpp +++ b/src/mbgl/tile/geometry_tile.cpp @@ -140,10 +140,8 @@ const LayerRenderData* GeometryTileRenderData::getLayerRenderData(const style::L that could flag the tile as non-pending too early. */ -GeometryTile::GeometryTile(const OverscaledTileID& id_, - std::string sourceID_, - const TileParameters& parameters) - : Tile(Kind::Geometry, id_), +GeometryTile::GeometryTile(const OverscaledTileID& id_, std::string sourceID_, const TileParameters& parameters) + : Tile(TileKind::Geometry, id_), ImageRequestor(parameters.imageManager), sourceID(std::move(sourceID_)), mailbox(std::make_shared<Mailbox>(*Scheduler::GetCurrent())), @@ -159,8 +157,7 @@ GeometryTile::GeometryTile(const OverscaledTileID& id_, glyphManager(parameters.glyphManager), imageManager(parameters.imageManager), mode(parameters.mode), - showCollisionBoxes(parameters.debugOptions & MapDebugOptions::Collision) { -} + showCollisionBoxes(parameters.debugOptions & MapDebugOptions::Collision) {} GeometryTile::~GeometryTile() { glyphManager.removeRequestor(*this); diff --git a/src/mbgl/tile/raster_dem_tile.cpp b/src/mbgl/tile/raster_dem_tile.cpp index bdd890bbbf..b3443dac34 100644 --- a/src/mbgl/tile/raster_dem_tile.cpp +++ b/src/mbgl/tile/raster_dem_tile.cpp @@ -14,15 +14,11 @@ namespace mbgl { -RasterDEMTile::RasterDEMTile(const OverscaledTileID& id_, - const TileParameters& parameters, - const Tileset& tileset) - : Tile(Kind::RasterDEM, id_), +RasterDEMTile::RasterDEMTile(const OverscaledTileID& id_, const TileParameters& parameters, const Tileset& tileset) + : Tile(TileKind::RasterDEM, id_), loader(*this, id_, parameters, tileset), mailbox(std::make_shared<Mailbox>(*Scheduler::GetCurrent())), - worker(Scheduler::GetBackground(), - ActorRef<RasterDEMTile>(*this, mailbox)) { - + worker(Scheduler::GetBackground(), ActorRef<RasterDEMTile>(*this, mailbox)) { encoding = tileset.encoding; if ( id.canonical.y == 0 ){ // this tile doesn't have upper neighboring tiles so marked those as backfilled diff --git a/src/mbgl/tile/raster_tile.cpp b/src/mbgl/tile/raster_tile.cpp index 2ba1dfcc07..6cc8046d4c 100644 --- a/src/mbgl/tile/raster_tile.cpp +++ b/src/mbgl/tile/raster_tile.cpp @@ -14,15 +14,11 @@ namespace mbgl { -RasterTile::RasterTile(const OverscaledTileID& id_, - const TileParameters& parameters, - const Tileset& tileset) - : Tile(Kind::Raster, id_), +RasterTile::RasterTile(const OverscaledTileID& id_, const TileParameters& parameters, const Tileset& tileset) + : Tile(TileKind::Raster, id_), loader(*this, id_, parameters, tileset), mailbox(std::make_shared<Mailbox>(*Scheduler::GetCurrent())), - worker(Scheduler::GetBackground(), - ActorRef<RasterTile>(*this, mailbox)) { -} + worker(Scheduler::GetBackground(), ActorRef<RasterTile>(*this, mailbox)) {} RasterTile::~RasterTile() = default; diff --git a/src/mbgl/tile/tile.cpp b/src/mbgl/tile/tile.cpp index 441f0424aa..ea397318f6 100644 --- a/src/mbgl/tile/tile.cpp +++ b/src/mbgl/tile/tile.cpp @@ -8,7 +8,7 @@ namespace mbgl { static TileObserver nullObserver; -Tile::Tile(Kind kind_, OverscaledTileID id_) : kind(kind_), id(id_), observer(&nullObserver) {} +Tile::Tile(TileKind kind_, OverscaledTileID id_) : kind(kind_), id(id_), observer(&nullObserver) {} Tile::~Tile() = default; @@ -27,12 +27,20 @@ void Tile::setTriedCache() { void Tile::dumpDebugLogs() const { std::string kindString; switch (kind) { - case Kind::Geometry: kindString = "Geometry"; break; - case Kind::Raster: kindString = "Raster"; break; - case Kind::RasterDEM: kindString = "RasterDEM"; break; - default: kindString = "Unknown"; break; + case TileKind::Geometry: + kindString = "Geometry"; + break; + case TileKind::Raster: + kindString = "Raster"; + break; + case TileKind::RasterDEM: + kindString = "RasterDEM"; + break; + default: + kindString = "Unknown"; + break; } - Log::Info(Event::General, "Tile::Kind: %s", kindString.c_str()); + Log::Info(Event::General, "TileKind: %s", kindString.c_str()); Log::Info(Event::General, "Tile::id: %s", util::toString(id).c_str()); Log::Info(Event::General, "Tile::renderable: %s", isRenderable() ? "yes" : "no"); Log::Info(Event::General, "Tile::complete: %s", isComplete() ? "yes" : "no"); diff --git a/src/mbgl/tile/tile.hpp b/src/mbgl/tile/tile.hpp index 573b3a1c50..ded23263a1 100644 --- a/src/mbgl/tile/tile.hpp +++ b/src/mbgl/tile/tile.hpp @@ -1,17 +1,18 @@ #pragma once -#include <mbgl/util/noncopyable.hpp> -#include <mbgl/util/chrono.hpp> -#include <mbgl/util/optional.hpp> -#include <mbgl/util/feature.hpp> -#include <mbgl/util/tile_coordinate.hpp> -#include <mbgl/tile/tile_id.hpp> -#include <mbgl/tile/tile_necessity.hpp> -#include <mbgl/renderer/tile_mask.hpp> #include <mbgl/renderer/bucket.hpp> -#include <mbgl/tile/geometry_tile_data.hpp> +#include <mbgl/renderer/tile_mask.hpp> #include <mbgl/storage/resource.hpp> #include <mbgl/style/layer_properties.hpp> +#include <mbgl/tile/geometry_tile_data.hpp> +#include <mbgl/tile/tile_id.hpp> +#include <mbgl/tile/tile_kind.hpp> +#include <mbgl/tile/tile_necessity.hpp> +#include <mbgl/util/chrono.hpp> +#include <mbgl/util/feature.hpp> +#include <mbgl/util/noncopyable.hpp> +#include <mbgl/util/optional.hpp> +#include <mbgl/util/tile_coordinate.hpp> #include <string> #include <memory> @@ -48,15 +49,10 @@ inline bool operator!=(const TileUpdateParameters& a, const TileUpdateParameters } class Tile { public: - enum class Kind : uint8_t { - Geometry, - Raster, - RasterDEM - }; Tile(const Tile&) = delete; Tile& operator=(const Tile&) = delete; - Tile(Kind, OverscaledTileID); + Tile(TileKind, OverscaledTileID); virtual ~Tile(); virtual std::unique_ptr<TileRenderData> createRenderData() = 0; @@ -74,7 +70,7 @@ public: // // Tile implementation should update the contained layer // render data with the given properties. - // + // // Returns `true` if the corresponding render layer data is present in this tile (and i.e. it // was succesfully updated); returns `false` otherwise. virtual bool layerPropertiesUpdated(const Immutable<style::LayerProperties>& layerProperties) = 0; @@ -124,7 +120,7 @@ public: bool isComplete() const { return loaded && !pending; } - + // "holdForFade" is used to keep tiles in the render tree after they're no longer // ideal tiles in order to allow symbols to fade out virtual bool holdForFade() const { @@ -143,7 +139,7 @@ public: void dumpDebugLogs() const; - const Kind kind; + const TileKind kind; OverscaledTileID id; optional<Timestamp> modified; optional<Timestamp> expires; diff --git a/src/mbgl/util/mapbox.cpp b/src/mbgl/util/mapbox.cpp index 6c36c152cd..ff24680296 100644 --- a/src/mbgl/util/mapbox.cpp +++ b/src/mbgl/util/mapbox.cpp @@ -1,8 +1,9 @@ -#include <mbgl/util/mapbox.hpp> +#include <mbgl/style/source.hpp> #include <mbgl/util/constants.hpp> #include <mbgl/util/logging.hpp> -#include <mbgl/util/url.hpp> +#include <mbgl/util/mapbox.hpp> #include <mbgl/util/tileset.hpp> +#include <mbgl/util/url.hpp> #include <stdexcept> #include <vector> @@ -112,8 +113,7 @@ std::string normalizeTileURL(const std::string& baseURL, return transformURL(tpl, str, url); } -std::string -canonicalizeTileURL(const std::string& str, const style::SourceType type, const uint16_t tileSize) { +std::string canonicalizeTileURL(const std::string& str, const style::Source& source) { const char* version = "/v4/"; const size_t versionLen = strlen(version); @@ -132,8 +132,8 @@ canonicalizeTileURL(const std::string& str, const style::SourceType type, const std::string result = "mapbox://tiles/"; result.append(str, path.directory.first + versionLen, path.directory.second - versionLen); result.append(str, path.filename.first, path.filename.second); - if (type == style::SourceType::Raster || type == style::SourceType::RasterDEM) { - result += tileSize == util::tileSize ? "@2x" : "{ratio}"; + if (source.getTypeInfo()->tileKind == TileKind::Raster) { + result += source.getTileSize() == util::tileSize ? "@2x" : "{ratio}"; } result.append(str, path.extension.first, path.extension.second); @@ -158,11 +158,11 @@ canonicalizeTileURL(const std::string& str, const style::SourceType type, const return result; } -void canonicalizeTileset(Tileset& tileset, const std::string& sourceURL, style::SourceType type, uint16_t tileSize) { +void canonicalizeTileset(Tileset& tileset, const std::string& sourceURL, const style::Source& source) { // TODO: Remove this hack by delivering proper URLs in the TileJSON to begin with. if (isMapboxURL(sourceURL)) { for (auto& url : tileset.tiles) { - url = canonicalizeTileURL(url, type, tileSize); + url = canonicalizeTileURL(url, source); } } } diff --git a/src/mbgl/util/mapbox.hpp b/src/mbgl/util/mapbox.hpp index 815b338cab..1bedb17014 100644 --- a/src/mbgl/util/mapbox.hpp +++ b/src/mbgl/util/mapbox.hpp @@ -1,12 +1,16 @@ #pragma once -#include <string> #include <mbgl/style/types.hpp> +#include <string> namespace mbgl { class Tileset; +namespace style { +class Source; +} + namespace util { namespace mapbox { @@ -19,10 +23,10 @@ std::string normalizeGlyphsURL(const std::string& baseURL, const std::string& st std::string normalizeTileURL(const std::string& baseURL, const std::string& str, const std::string& accessToken); // Return a "mapbox://tiles/..." URL (suitable for normalizeTileURL) for the given Mapbox tile URL. -std::string canonicalizeTileURL(const std::string& str, style::SourceType, uint16_t tileSize); +std::string canonicalizeTileURL(const std::string& str, const style::Source&); // Replace URL templates with "mapbox://tiles/..." URLs (suitable for normalizeTileURL). -void canonicalizeTileset(Tileset&, const std::string& url, style::SourceType, uint16_t tileSize); +void canonicalizeTileset(Tileset&, const std::string& url, const style::Source&); extern const uint64_t DEFAULT_OFFLINE_TILE_COUNT_LIMIT; diff --git a/src/mbgl/util/tile_cover.cpp b/src/mbgl/util/tile_cover.cpp index fb4579b9ab..56a1ae7f69 100644 --- a/src/mbgl/util/tile_cover.cpp +++ b/src/mbgl/util/tile_cover.cpp @@ -138,15 +138,6 @@ std::vector<UnwrappedTileID> tileCover(const Point<double>& tl, } // namespace -int32_t coveringZoomLevel(double zoom, style::SourceType type, uint16_t size) { - zoom += util::log2(util::tileSize / size); - if (type == style::SourceType::Raster || type == style::SourceType::Video) { - return ::round(zoom); - } else { - return std::floor(zoom); - } -} - std::vector<OverscaledTileID> tileCover(const TransformState& state, uint8_t z, const optional<uint8_t>& overscaledZ) { struct Node { AABB aabb; diff --git a/src/mbgl/util/tile_cover.hpp b/src/mbgl/util/tile_cover.hpp index 5e09d8a671..c5d0bd40c5 100644 --- a/src/mbgl/util/tile_cover.hpp +++ b/src/mbgl/util/tile_cover.hpp @@ -31,8 +31,6 @@ private: std::unique_ptr<Impl> impl; }; -int32_t coveringZoomLevel(double z, style::SourceType type, uint16_t tileSize); - std::vector<OverscaledTileID> tileCover(const TransformState&, uint8_t z, const optional<uint8_t>& overscaledZ = nullopt); diff --git a/test/storage/offline.test.cpp b/test/storage/offline.test.cpp index 46e7d68c3f..8335e43db1 100644 --- a/test/storage/offline.test.cpp +++ b/test/storage/offline.test.cpp @@ -4,7 +4,6 @@ #include <gtest/gtest.h> using namespace mbgl; -using SourceType = mbgl::style::SourceType; TEST(OfflineTilePyramidRegionDefinition, EncodeDecode) { diff --git a/test/style/source.test.cpp b/test/style/source.test.cpp index c8f1e3781d..db9d90c5f2 100644 --- a/test/style/source.test.cpp +++ b/test/style/source.test.cpp @@ -50,7 +50,6 @@ #include <gmock/gmock.h> using namespace mbgl; -using SourceType = mbgl::style::SourceType; class SourceTest { public: @@ -272,12 +271,13 @@ TEST(Source, RasterTileFail) { RasterSource source("source", tileset, 512); source.loadDescription(*test.fileSource); - test.renderSourceObserver.tileError = [&] (RenderSource& source_, const OverscaledTileID& tileID, std::exception_ptr error) { - EXPECT_EQ(SourceType::Raster, source_.baseImpl->type); - EXPECT_EQ(OverscaledTileID(0, 0, 0), tileID); - EXPECT_EQ("Failed by the test case", util::toString(error)); - test.end(); - }; + test.renderSourceObserver.tileError = + [&](RenderSource& source_, const OverscaledTileID& tileID, std::exception_ptr error) { + EXPECT_EQ(RasterSource::Impl::staticTypeInfo(), source_.baseImpl->getTypeInfo()); + EXPECT_EQ(OverscaledTileID(0, 0, 0), tileID); + EXPECT_EQ("Failed by the test case", util::toString(error)); + test.end(); + }; auto renderSource = RenderSource::create(source.baseImpl); renderSource->setObserver(&test.renderSourceObserver); @@ -307,12 +307,13 @@ TEST(Source, RasterDEMTileFail) { RasterDEMSource source("source", tileset, 512); source.loadDescription(*test.fileSource); - test.renderSourceObserver.tileError = [&] (RenderSource& source_, const OverscaledTileID& tileID, std::exception_ptr error) { - EXPECT_EQ(SourceType::RasterDEM, source_.baseImpl->type); - EXPECT_EQ(OverscaledTileID(0, 0, 0), tileID); - EXPECT_EQ("Failed by the test case", util::toString(error)); - test.end(); - }; + test.renderSourceObserver.tileError = + [&](RenderSource& source_, const OverscaledTileID& tileID, std::exception_ptr error) { + EXPECT_EQ(RasterDEMSource::Impl::staticTypeInfo(), source_.baseImpl->getTypeInfo()); + EXPECT_EQ(OverscaledTileID(0, 0, 0), tileID); + EXPECT_EQ("Failed by the test case", util::toString(error)); + test.end(); + }; auto renderSource = RenderSource::create(source.baseImpl); renderSource->setObserver(&test.renderSourceObserver); @@ -344,12 +345,13 @@ TEST(Source, VectorTileFail) { VectorSource source("source", tileset); source.loadDescription(*test.fileSource); - test.renderSourceObserver.tileError = [&] (RenderSource& source_, const OverscaledTileID& tileID, std::exception_ptr error) { - EXPECT_EQ(SourceType::Vector, source_.baseImpl->type); - EXPECT_EQ(OverscaledTileID(0, 0, 0), tileID); - EXPECT_EQ("Failed by the test case", util::toString(error)); - test.end(); - }; + test.renderSourceObserver.tileError = + [&](RenderSource& source_, const OverscaledTileID& tileID, std::exception_ptr error) { + EXPECT_EQ(VectorSource::Impl::staticTypeInfo(), source_.baseImpl->getTypeInfo()); + EXPECT_EQ(OverscaledTileID(0, 0, 0), tileID); + EXPECT_EQ("Failed by the test case", util::toString(error)); + test.end(); + }; auto renderSource = RenderSource::create(source.baseImpl); renderSource->setObserver(&test.renderSourceObserver); @@ -377,13 +379,14 @@ TEST(Source, RasterTileCorrupt) { RasterSource source("source", tileset, 512); source.loadDescription(*test.fileSource); - test.renderSourceObserver.tileError = [&] (RenderSource& source_, const OverscaledTileID& tileID, std::exception_ptr error) { - EXPECT_EQ(source_.baseImpl->type, SourceType::Raster); - EXPECT_EQ(OverscaledTileID(0, 0, 0), tileID); - EXPECT_TRUE(bool(error)); - // Not asserting on platform-specific error text. - test.end(); - }; + test.renderSourceObserver.tileError = + [&](RenderSource& source_, const OverscaledTileID& tileID, std::exception_ptr error) { + EXPECT_EQ(RasterSource::Impl::staticTypeInfo(), source_.baseImpl->getTypeInfo()); + EXPECT_EQ(OverscaledTileID(0, 0, 0), tileID); + EXPECT_TRUE(bool(error)); + // Not asserting on platform-specific error text. + test.end(); + }; auto renderSource = RenderSource::create(source.baseImpl); renderSource->setObserver(&test.renderSourceObserver); @@ -411,13 +414,14 @@ TEST(Source, RasterDEMTileCorrupt) { RasterDEMSource source("source", tileset, 512); source.loadDescription(*test.fileSource); - test.renderSourceObserver.tileError = [&] (RenderSource& source_, const OverscaledTileID& tileID, std::exception_ptr error) { - EXPECT_EQ(source_.baseImpl->type, SourceType::RasterDEM); - EXPECT_EQ(OverscaledTileID(0, 0, 0), tileID); - EXPECT_TRUE(bool(error)); - // Not asserting on platform-specific error text. - test.end(); - }; + test.renderSourceObserver.tileError = + [&](RenderSource& source_, const OverscaledTileID& tileID, std::exception_ptr error) { + EXPECT_EQ(RasterDEMSource::Impl::staticTypeInfo(), source_.baseImpl->getTypeInfo()); + EXPECT_EQ(OverscaledTileID(0, 0, 0), tileID); + EXPECT_TRUE(bool(error)); + // Not asserting on platform-specific error text. + test.end(); + }; auto renderSource = RenderSource::create(source.baseImpl); renderSource->setObserver(&test.renderSourceObserver); @@ -447,12 +451,13 @@ TEST(Source, VectorTileCorrupt) { VectorSource source("source", tileset); source.loadDescription(*test.fileSource); - test.renderSourceObserver.tileError = [&] (RenderSource& source_, const OverscaledTileID& tileID, std::exception_ptr error) { - EXPECT_EQ(source_.baseImpl->type, SourceType::Vector); - EXPECT_EQ(OverscaledTileID(0, 0, 0), tileID); - EXPECT_EQ(util::toString(error), "unknown pbf field type exception"); - test.end(); - }; + test.renderSourceObserver.tileError = + [&](RenderSource& source_, const OverscaledTileID& tileID, std::exception_ptr error) { + EXPECT_EQ(VectorSource::Impl::staticTypeInfo(), source_.baseImpl->getTypeInfo()); + EXPECT_EQ(OverscaledTileID(0, 0, 0), tileID); + EXPECT_EQ(util::toString(error), "unknown pbf field type exception"); + test.end(); + }; auto renderSource = RenderSource::create(source.baseImpl); renderSource->setObserver(&test.renderSourceObserver); @@ -745,7 +750,7 @@ class FakeTileSource; class FakeTile : public Tile { public: FakeTile(FakeTileSource& source_, const OverscaledTileID& tileID) - : Tile(Tile::Kind::Geometry, tileID), source(source_) { + : Tile(TileKind::Geometry, tileID), source(source_) { renderable = true; } void setNecessity(TileNecessity necessity) override; diff --git a/test/tile/geojson_tile.test.cpp b/test/tile/geojson_tile.test.cpp index bffd99999e..ae16d9b4bf 100644 --- a/test/tile/geojson_tile.test.cpp +++ b/test/tile/geojson_tile.test.cpp @@ -53,11 +53,11 @@ public: fn(features); } - Features getChildren(const std::uint32_t) final { return {}; } + Features getChildren(const std::uint32_t) const final { return {}; } - Features getLeaves(const std::uint32_t, const std::uint32_t, const std::uint32_t) final { return {}; } + Features getLeaves(const std::uint32_t, const std::uint32_t, const std::uint32_t) const final { return {}; } - std::uint8_t getClusterExpansionZoom(std::uint32_t) final { return 0; } + std::uint8_t getClusterExpansionZoom(std::uint32_t) const final { return 0; } private: TileFeatures features; diff --git a/test/util/mapbox.test.cpp b/test/util/mapbox.test.cpp index 20af31b822..d1959201a9 100644 --- a/test/util/mapbox.test.cpp +++ b/test/util/mapbox.test.cpp @@ -1,13 +1,15 @@ #include <mbgl/test/util.hpp> +#include <mbgl/style/sources/raster_source.hpp> +#include <mbgl/style/sources/vector_source.hpp> +#include <mbgl/util/constants.hpp> #include <mbgl/util/logging.hpp> #include <mbgl/util/mapbox.hpp> -#include <mbgl/util/constants.hpp> #include <mbgl/util/tileset.hpp> + #include <stdexcept> using namespace mbgl; -using SourceType = mbgl::style::SourceType; TEST(Mapbox, SourceURL) { EXPECT_EQ( @@ -127,74 +129,78 @@ TEST(Mapbox, TileURL) { } TEST(Mapbox, CanonicalURL) { + EXPECT_EQ("mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf", + mbgl::util::mapbox::canonicalizeTileURL("http://a.tiles.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf", + style::VectorSource{"id", "url"})); + EXPECT_EQ("mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf", + mbgl::util::mapbox::canonicalizeTileURL("http://b.tiles.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf", + style::VectorSource{"id", "url"})); + EXPECT_EQ("mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf", + mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf", + style::VectorSource{"id", "url"})); EXPECT_EQ( "mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf", - mbgl::util::mapbox::canonicalizeTileURL("http://a.tiles.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf", SourceType::Vector, 512)); - EXPECT_EQ( - "mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf", - mbgl::util::mapbox::canonicalizeTileURL("http://b.tiles.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf", SourceType::Vector, 512)); - EXPECT_EQ( - "mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf", - mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf", SourceType::Vector, 512)); + mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf?access_token=key", + style::VectorSource{"id", "url"})); EXPECT_EQ( "mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf", - mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf?access_token=key", SourceType::Vector, 512)); - EXPECT_EQ( - "mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf", - mbgl::util::mapbox::canonicalizeTileURL("https://api.mapbox.cn/v4/a.b/{z}/{x}/{y}.vector.pbf?access_token=key", SourceType::Vector, 512)); - EXPECT_EQ( - "mapbox://tiles/a.b,c.d/{z}/{x}/{y}.vector.pbf", - mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b,c.d/{z}/{x}/{y}.vector.pbf?access_token=key", SourceType::Vector, 512)); - EXPECT_EQ( - "mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf?custom=parameter", - mbgl::util::mapbox::canonicalizeTileURL("http://a.tiles.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf?access_token=key&custom=parameter", SourceType::Vector, 512)); - EXPECT_EQ( - "mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf?custom=parameter", - mbgl::util::mapbox::canonicalizeTileURL("http://a.tiles.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf?custom=parameter&access_token=key", SourceType::Vector, 512)); + mbgl::util::mapbox::canonicalizeTileURL("https://api.mapbox.cn/v4/a.b/{z}/{x}/{y}.vector.pbf?access_token=key", + style::VectorSource{"id", "url"})); + EXPECT_EQ("mapbox://tiles/a.b,c.d/{z}/{x}/{y}.vector.pbf", + mbgl::util::mapbox::canonicalizeTileURL( + "http://api.mapbox.com/v4/a.b,c.d/{z}/{x}/{y}.vector.pbf?access_token=key", + style::VectorSource{"id", "url"})); + EXPECT_EQ("mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf?custom=parameter", + mbgl::util::mapbox::canonicalizeTileURL( + "http://a.tiles.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf?access_token=key&custom=parameter", + style::VectorSource{"id", "url"})); + EXPECT_EQ("mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf?custom=parameter", + mbgl::util::mapbox::canonicalizeTileURL( + "http://a.tiles.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf?custom=parameter&access_token=key", + style::VectorSource{"id", "url"})); EXPECT_EQ( "mapbox://tiles/a.b/{z}/{x}/{y}.vector.pbf?custom=parameter&second=param", - mbgl::util::mapbox::canonicalizeTileURL("http://a.tiles.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf?custom=parameter&access_token=key&second=param", SourceType::Vector, 512)); - EXPECT_EQ( - "mapbox://tiles/a.b/{z}/{x}/{y}{ratio}.jpg", - mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.jpg?access_token=key", SourceType::Raster, 256)); - EXPECT_EQ( - "mapbox://tiles/a.b/{z}/{x}/{y}{ratio}.jpg70", - mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.jpg70?access_token=key", SourceType::Raster, 256)); - EXPECT_EQ( - "mapbox://tiles/a.b/{z}/{x}/{y}@2x.jpg", - mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.jpg?access_token=key", SourceType::Raster, 512)); - EXPECT_EQ( - "mapbox://tiles/a.b/{z}/{x}/{y}@2x.jpg70", - mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.jpg70?access_token=key", SourceType::Raster, 512)); - EXPECT_EQ( - "mapbox://tiles/a.b/{z}/{x}/{y}{ratio}.png", - mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png", SourceType::Raster, 256)); - EXPECT_EQ( - "mapbox://tiles/a.b/{z}/{x}/{y}{ratio}.png", - mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png?access_token=key", SourceType::Raster, 256)); - EXPECT_EQ( - "mapbox://tiles/a.b/{z}/{x}/{y}@2x.png", - mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png", SourceType::Raster, 512)); - EXPECT_EQ( - "mapbox://tiles/a.b/{z}/{x}/{y}@2x.png", - mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png?access_token=key", SourceType::Raster, 512)); + mbgl::util::mapbox::canonicalizeTileURL( + "http://a.tiles.mapbox.com/v4/a.b/{z}/{x}/{y}.vector.pbf?custom=parameter&access_token=key&second=param", + style::VectorSource{"id", "url"})); + EXPECT_EQ("mapbox://tiles/a.b/{z}/{x}/{y}{ratio}.jpg", + mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.jpg?access_token=key", + style::RasterSource{"id", "url", 256})); + EXPECT_EQ("mapbox://tiles/a.b/{z}/{x}/{y}{ratio}.jpg70", + mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.jpg70?access_token=key", + style::RasterSource{"id", "url", 256})); + EXPECT_EQ("mapbox://tiles/a.b/{z}/{x}/{y}@2x.jpg", + mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.jpg?access_token=key", + style::RasterSource{"id", "url", 512})); + EXPECT_EQ("mapbox://tiles/a.b/{z}/{x}/{y}@2x.jpg70", + mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.jpg70?access_token=key", + style::RasterSource{"id", "url", 512})); + EXPECT_EQ("mapbox://tiles/a.b/{z}/{x}/{y}{ratio}.png", + mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png", + style::RasterSource{"id", "url", 256})); + EXPECT_EQ("mapbox://tiles/a.b/{z}/{x}/{y}{ratio}.png", + mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png?access_token=key", + style::RasterSource{"id", "url", 256})); + EXPECT_EQ("mapbox://tiles/a.b/{z}/{x}/{y}@2x.png", + mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png", + style::RasterSource{"id", "url", 512})); + EXPECT_EQ("mapbox://tiles/a.b/{z}/{x}/{y}@2x.png", + mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.png?access_token=key", + style::RasterSource{"id", "url", 512})); // We don't ever expect to see these inputs, but be safe anyway. - EXPECT_EQ( - "", - mbgl::util::mapbox::canonicalizeTileURL("", SourceType::Raster, 256)); - EXPECT_EQ( - "http://path", - mbgl::util::mapbox::canonicalizeTileURL("http://path", SourceType::Raster, 256)); + EXPECT_EQ("", mbgl::util::mapbox::canonicalizeTileURL("", style::RasterSource{"id", "url", 256})); + EXPECT_EQ("http://path", + mbgl::util::mapbox::canonicalizeTileURL("http://path", style::RasterSource{"id", "url", 256})); EXPECT_EQ( "http://api.mapbox.com/v4/", - mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/", SourceType::Raster, 256)); - EXPECT_EQ( - "http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.", - mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.", SourceType::Raster, 256)); - EXPECT_EQ( - "http://api.mapbox.com/v4/a.b/{z}/{x}/{y}/.", - mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}/.", SourceType::Raster, 256)); + mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/", style::RasterSource{"id", "url", 256})); + EXPECT_EQ("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.", + mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}.", + style::RasterSource{"id", "url", 256})); + EXPECT_EQ("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}/.", + mbgl::util::mapbox::canonicalizeTileURL("http://api.mapbox.com/v4/a.b/{z}/{x}/{y}/.", + style::RasterSource{"id", "url", 256})); } TEST(Mapbox, CanonicalizeRasterTileset) { @@ -203,7 +209,8 @@ TEST(Mapbox, CanonicalizeRasterTileset) { "http://a.tiles.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}.png?access_token=key" }; - mbgl::util::mapbox::canonicalizeTileset(tileset, "mapbox://mapbox.satellite", SourceType::Raster, 256); + mbgl::util::mapbox::canonicalizeTileset( + tileset, "mapbox://mapbox.satellite", style::RasterSource{"id", "url", 256}); EXPECT_EQ("mapbox://tiles/mapbox.satellite/{z}/{x}/{y}{ratio}.png", tileset.tiles[0]); } @@ -214,7 +221,7 @@ TEST(Mapbox, CanonicalizeVectorTileset) { "http://a.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.vector.pbf?access_token=key" }; - mbgl::util::mapbox::canonicalizeTileset(tileset, "mapbox://mapbox.streets", SourceType::Vector, 512); + mbgl::util::mapbox::canonicalizeTileset(tileset, "mapbox://mapbox.streets", style::VectorSource{"id", "url"}); EXPECT_EQ("mapbox://tiles/mapbox.streets/{z}/{x}/{y}.vector.pbf", tileset.tiles[0]); } |