From fce4129435ddc5f14abdd1e2ed6a74bf84e1c496 Mon Sep 17 00:00:00 2001 From: Alexander Shalamov Date: Thu, 6 Feb 2020 09:44:49 +0200 Subject: [PATCH] [core] Add runtime API for setting tile prefetch delta for Source New setPrefetchZoomDelta(optional delta) method allow overriding default tile prefetch setting that is defined by the Map instance. The method can be moved to generic style specification if found to be useful for gl-js engine. --- include/mbgl/style/source.hpp | 5 +++++ .../mbgl/style/sources/custom_geometry_source.hpp | 4 ++++ include/mbgl/style/sources/geojson_source.hpp | 3 +++ include/mbgl/style/sources/image_source.hpp | 4 ++++ include/mbgl/style/sources/raster_source.hpp | 3 +++ include/mbgl/style/sources/vector_source.hpp | 3 +++ src/mbgl/annotation/annotation_source.cpp | 8 ++++++++ src/mbgl/annotation/annotation_source.hpp | 4 +++- src/mbgl/style/source.cpp | 12 ++++++++++++ src/mbgl/style/source_impl.cpp | 8 ++++++++ src/mbgl/style/source_impl.hpp | 3 +++ src/mbgl/style/sources/custom_geometry_source.cpp | 6 +++++- src/mbgl/style/sources/geojson_source.cpp | 4 ++++ src/mbgl/style/sources/image_source.cpp | 4 ++++ src/mbgl/style/sources/raster_source.cpp | 4 ++++ src/mbgl/style/sources/vector_source.cpp | 4 ++++ 16 files changed, 77 insertions(+), 2 deletions(-) diff --git a/include/mbgl/style/source.hpp b/include/mbgl/style/source.hpp index c3c0609a9..59afd6916 100644 --- a/include/mbgl/style/source.hpp +++ b/include/mbgl/style/source.hpp @@ -73,6 +73,8 @@ public: SourceObserver* observer = nullptr; virtual void loadDescription(FileSource&) = 0; + void setPrefetchZoomDelta(optional delta) noexcept; + optional getPrefetchZoomDelta() const noexcept; void dumpDebugLogs() const; virtual bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const = 0; @@ -85,6 +87,9 @@ public: mapbox::base::TypeWrapper peer; virtual mapbox::base::WeakPtr makeWeakPtr() = 0; + +protected: + virtual Mutable createMutable() const noexcept = 0; }; } // namespace style diff --git a/include/mbgl/style/sources/custom_geometry_source.hpp b/include/mbgl/style/sources/custom_geometry_source.hpp index ff0450569..504ec42ea 100644 --- a/include/mbgl/style/sources/custom_geometry_source.hpp +++ b/include/mbgl/style/sources/custom_geometry_source.hpp @@ -50,6 +50,10 @@ public: mapbox::base::WeakPtr makeWeakPtr() override { return weakFactory.makeWeakPtr(); } + +protected: + Mutable createMutable() const noexcept final; + private: std::shared_ptr threadPool; std::unique_ptr> loader; diff --git a/include/mbgl/style/sources/geojson_source.hpp b/include/mbgl/style/sources/geojson_source.hpp index 750d29627..4bd250b89 100644 --- a/include/mbgl/style/sources/geojson_source.hpp +++ b/include/mbgl/style/sources/geojson_source.hpp @@ -81,6 +81,9 @@ public: return weakFactory.makeWeakPtr(); } +protected: + Mutable createMutable() const noexcept final; + private: optional url; std::unique_ptr req; diff --git a/include/mbgl/style/sources/image_source.hpp b/include/mbgl/style/sources/image_source.hpp index 699a3c649..d2b7c37bd 100644 --- a/include/mbgl/style/sources/image_source.hpp +++ b/include/mbgl/style/sources/image_source.hpp @@ -33,6 +33,10 @@ public: mapbox::base::WeakPtr makeWeakPtr() override { return weakFactory.makeWeakPtr(); } + +protected: + Mutable createMutable() const noexcept final; + private: optional url; std::unique_ptr req; diff --git a/include/mbgl/style/sources/raster_source.hpp b/include/mbgl/style/sources/raster_source.hpp index 00a3b788c..e06c3404e 100644 --- a/include/mbgl/style/sources/raster_source.hpp +++ b/include/mbgl/style/sources/raster_source.hpp @@ -31,6 +31,9 @@ public: return weakFactory.makeWeakPtr(); } +protected: + Mutable createMutable() const noexcept final; + private: const variant urlOrTileset; std::unique_ptr req; diff --git a/include/mbgl/style/sources/vector_source.hpp b/include/mbgl/style/sources/vector_source.hpp index 4165af0a6..83fcae95d 100644 --- a/include/mbgl/style/sources/vector_source.hpp +++ b/include/mbgl/style/sources/vector_source.hpp @@ -30,6 +30,9 @@ public: return weakFactory.makeWeakPtr(); } +protected: + Mutable createMutable() const noexcept final; + private: const variant urlOrTileset; std::unique_ptr req; diff --git a/src/mbgl/annotation/annotation_source.cpp b/src/mbgl/annotation/annotation_source.cpp index 7a137f188..318241a91 100644 --- a/src/mbgl/annotation/annotation_source.cpp +++ b/src/mbgl/annotation/annotation_source.cpp @@ -14,6 +14,10 @@ AnnotationSource::Impl::Impl() : Source::Impl(SourceType::Annotations, AnnotationManager::SourceID) { } +const AnnotationSource::Impl& AnnotationSource::impl() const { + return static_cast(*baseImpl); +} + void AnnotationSource::loadDescription(FileSource&) { loaded = true; } @@ -26,4 +30,8 @@ bool AnnotationSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info) return !std::strcmp(info->type, "line") || !std::strcmp(info->type, "symbol") || !std::strcmp(info->type, "fill"); } +Mutable AnnotationSource::createMutable() const noexcept { + return staticMutableCast(makeMutable(impl())); +} + } // namespace mbgl diff --git a/src/mbgl/annotation/annotation_source.hpp b/src/mbgl/annotation/annotation_source.hpp index 0379426b3..de432202a 100644 --- a/src/mbgl/annotation/annotation_source.hpp +++ b/src/mbgl/annotation/annotation_source.hpp @@ -12,13 +12,15 @@ public: class Impl; const Impl& impl() const; +protected: + Mutable createMutable() const noexcept final; + private: void loadDescription(FileSource&) final; bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const override; mapbox::base::WeakPtr makeWeakPtr() override { return weakFactory.makeWeakPtr(); } - Mutable mutableImpl() const; mapbox::base::WeakPtrFactory weakFactory {this}; }; diff --git a/src/mbgl/style/source.cpp b/src/mbgl/style/source.cpp index e7701b8be..644a31dac 100644 --- a/src/mbgl/style/source.cpp +++ b/src/mbgl/style/source.cpp @@ -31,6 +31,18 @@ void Source::setObserver(SourceObserver* observer_) { observer = observer_ ? observer_ : &nullObserver; } +void Source::setPrefetchZoomDelta(optional delta) noexcept { + if (getPrefetchZoomDelta() == delta) return; + auto newImpl = createMutable(); + newImpl->setPrefetchZoomDelta(std::move(delta)); + baseImpl = std::move(newImpl); + observer->onSourceChanged(*this); +} + +optional Source::getPrefetchZoomDelta() const noexcept { + return baseImpl->getPrefetchZoomDelta(); +} + void Source::dumpDebugLogs() const { Log::Info(Event::General, "Source::id: %s", getID().c_str()); Log::Info(Event::General, "Source::loaded: %d", loaded); diff --git a/src/mbgl/style/source_impl.cpp b/src/mbgl/style/source_impl.cpp index 0683f847c..7f6f633ed 100644 --- a/src/mbgl/style/source_impl.cpp +++ b/src/mbgl/style/source_impl.cpp @@ -8,5 +8,13 @@ Source::Impl::Impl(SourceType type_, std::string id_) id(std::move(id_)) { } +void Source::Impl::setPrefetchZoomDelta(optional delta) noexcept { + prefetchZoomDelta = std::move(delta); +} + +optional Source::Impl::getPrefetchZoomDelta() const noexcept { + return prefetchZoomDelta; +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/source_impl.hpp b/src/mbgl/style/source_impl.hpp index 42da97345..d2c5d54d1 100644 --- a/src/mbgl/style/source_impl.hpp +++ b/src/mbgl/style/source_impl.hpp @@ -20,9 +20,12 @@ public: Impl& operator=(const Impl&) = delete; virtual optional getAttribution() const = 0; + void setPrefetchZoomDelta(optional delta) noexcept; + optional getPrefetchZoomDelta() const noexcept; const SourceType type; const std::string id; + optional prefetchZoomDelta; protected: Impl(SourceType, std::string); diff --git a/src/mbgl/style/sources/custom_geometry_source.cpp b/src/mbgl/style/sources/custom_geometry_source.cpp index 1076cbf41..113298dd3 100644 --- a/src/mbgl/style/sources/custom_geometry_source.cpp +++ b/src/mbgl/style/sources/custom_geometry_source.cpp @@ -27,7 +27,7 @@ const CustomGeometrySource::Impl& CustomGeometrySource::impl() const { } void CustomGeometrySource::loadDescription(FileSource&) { - baseImpl = makeMutable(impl(), loader->self()); + baseImpl = makeMutable(impl(), loader->self()); loaded = true; observer->onSourceLoaded(*this); } @@ -49,5 +49,9 @@ void CustomGeometrySource::invalidateRegion(const LatLngBounds& bounds) { loader->self().invoke(&CustomTileLoader::invalidateRegion, bounds, impl().getZoomRange()); } +Mutable CustomGeometrySource::createMutable() const noexcept { + return staticMutableCast(makeMutable(impl())); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/geojson_source.cpp b/src/mbgl/style/sources/geojson_source.cpp index 3afccf07f..2880b9b13 100644 --- a/src/mbgl/style/sources/geojson_source.cpp +++ b/src/mbgl/style/sources/geojson_source.cpp @@ -120,5 +120,9 @@ bool GeoJSONSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info) co return mbgl::underlying_type(Tile::Kind::Geometry) == mbgl::underlying_type(info->tileKind); } +Mutable GeoJSONSource::createMutable() const noexcept { + return staticMutableCast(makeMutable(impl())); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/image_source.cpp b/src/mbgl/style/sources/image_source.cpp index d55f7c9f0..ec727a33b 100644 --- a/src/mbgl/style/sources/image_source.cpp +++ b/src/mbgl/style/sources/image_source.cpp @@ -87,5 +87,9 @@ bool ImageSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info) cons return mbgl::underlying_type(Tile::Kind::Raster) == mbgl::underlying_type(info->tileKind); } +Mutable ImageSource::createMutable() const noexcept { + return staticMutableCast(makeMutable(impl())); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/raster_source.cpp b/src/mbgl/style/sources/raster_source.cpp index f90306945..8c41da3a2 100644 --- a/src/mbgl/style/sources/raster_source.cpp +++ b/src/mbgl/style/sources/raster_source.cpp @@ -87,5 +87,9 @@ bool RasterSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info) con return mbgl::underlying_type(Tile::Kind::Raster) == mbgl::underlying_type(info->tileKind); } +Mutable RasterSource::createMutable() const noexcept { + return staticMutableCast(makeMutable(impl())); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/vector_source.cpp b/src/mbgl/style/sources/vector_source.cpp index 510106adb..cc2319b30 100644 --- a/src/mbgl/style/sources/vector_source.cpp +++ b/src/mbgl/style/sources/vector_source.cpp @@ -91,5 +91,9 @@ bool VectorSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info) con return mbgl::underlying_type(Tile::Kind::Geometry) == mbgl::underlying_type(info->tileKind); } +Mutable VectorSource::createMutable() const noexcept { + return staticMutableCast(makeMutable(impl())); +} + } // namespace style } // namespace mbgl