diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/renderer/sources/render_custom_vector_source.cpp | 43 | ||||
-rw-r--r-- | src/mbgl/renderer/sources/render_custom_vector_source.hpp | 5 | ||||
-rw-r--r-- | src/mbgl/style/sources/custom_vector_source.cpp | 80 | ||||
-rw-r--r-- | src/mbgl/style/sources/custom_vector_source_impl.cpp | 16 | ||||
-rw-r--r-- | src/mbgl/style/sources/custom_vector_source_impl.hpp | 10 | ||||
-rw-r--r-- | src/mbgl/tile/custom_tile.cpp | 33 | ||||
-rw-r--r-- | src/mbgl/tile/custom_tile.hpp | 15 |
7 files changed, 133 insertions, 69 deletions
diff --git a/src/mbgl/renderer/sources/render_custom_vector_source.cpp b/src/mbgl/renderer/sources/render_custom_vector_source.cpp index b6c27bb00c..76cc649834 100644 --- a/src/mbgl/renderer/sources/render_custom_vector_source.cpp +++ b/src/mbgl/renderer/sources/render_custom_vector_source.cpp @@ -1,14 +1,11 @@ #include <mbgl/renderer/sources/render_custom_vector_source.hpp> #include <mbgl/renderer/render_tile.hpp> #include <mbgl/renderer/paint_parameters.hpp> -#include <mbgl/tile/geojson_tile.hpp> -#include <mbgl/util/logging.hpp> +#include <mbgl/tile/custom_tile.hpp> #include <mbgl/algorithm/generate_clip_ids.hpp> #include <mbgl/algorithm/generate_clip_ids_impl.hpp> -#include <mapbox/geojsonvt.hpp> - namespace mbgl { using namespace style; @@ -26,35 +23,6 @@ bool RenderCustomVectorSource::isLoaded() const { return tilePyramid.isLoaded(); } -void RenderCustomVectorSource::setTileData(const CanonicalTileID& tileID, - const style::FetchTileResult& result) { - if (result.is<style::Error>()) { - Log::Error(Event::Render, "FetchTile (%d, %d, %d) error: %s", tileID.z, tileID.x, tileID.y, result.get<style::Error>().message.c_str()); - return; - } - - auto geoJSON = result.get<mapbox::geojson::geojson>(); - auto data = mapbox::geometry::feature_collection<int16_t>(); - if (geoJSON.is<FeatureCollection>() && !geoJSON.get<FeatureCollection>().empty()) { - const GeoJSONOptions options = impl().getOptions(); - - const double scale = util::EXTENT / options.tileSize; - - mapbox::geojsonvt::Options vtOptions; - vtOptions.maxZoom = options.maxzoom; - vtOptions.extent = util::EXTENT; - vtOptions.buffer = std::round(scale * options.buffer); - vtOptions.tolerance = scale * options.tolerance; - auto geojsonVt = std::make_unique<mapbox::geojsonvt::GeoJSONVT>(geoJSON, vtOptions); - data = geojsonVt->getTile(tileID.z, tileID.x, tileID.y).features; - } - for (auto const& item : tilePyramid.tiles) { - if (item.first.canonical == tileID) { - static_cast<GeoJSONTile*>(item.second.get())->updateData(data); - } - } -} - void RenderCustomVectorSource::update(Immutable<style::Source::Impl> baseImpl_, const std::vector<Immutable<Layer::Impl>>& layers, const bool needsRendering, @@ -64,9 +32,11 @@ void RenderCustomVectorSource::update(Immutable<style::Source::Impl> baseImpl_, enabled = needsRendering; - auto fetchTile = impl().getFetchTileFunction(); + auto tileLoader = impl().getTileLoader(); - FetchTileCallback fetchTileCallback = std::bind(&RenderCustomVectorSource::setTileData, this, std::placeholders::_1, std::placeholders::_2); + if (!tileLoader) { + return; + } const GeoJSONOptions options = impl().getOptions(); tilePyramid.update(layers, @@ -77,8 +47,7 @@ void RenderCustomVectorSource::update(Immutable<style::Source::Impl> baseImpl_, util::tileSize, { options.minzoom, options.maxzoom }, [&] (const OverscaledTileID& tileID) { - fetchTile(tileID.canonical, fetchTileCallback); - return std::make_unique<GeoJSONTile>(tileID, impl().id, parameters, mapbox::geometry::feature_collection<int16_t>()); + return std::make_unique<CustomTile>(tileID, impl().id, parameters, impl().getOptions(), *tileLoader); }); } diff --git a/src/mbgl/renderer/sources/render_custom_vector_source.hpp b/src/mbgl/renderer/sources/render_custom_vector_source.hpp index 43e5094cc1..b52fa196d2 100644 --- a/src/mbgl/renderer/sources/render_custom_vector_source.hpp +++ b/src/mbgl/renderer/sources/render_custom_vector_source.hpp @@ -3,6 +3,7 @@ #include <mbgl/renderer/render_source.hpp> #include <mbgl/renderer/tile_pyramid.hpp> #include <mbgl/style/sources/custom_vector_source_impl.hpp> +#include <mbgl/actor/actor.hpp> namespace mbgl { @@ -34,9 +35,7 @@ public: void onLowMemory() final; void dumpDebugLogs() const final; - - void setTileData(const CanonicalTileID& tileID, const style::FetchTileResult& result); - + private: const style::CustomVectorSource::Impl& impl() const; diff --git a/src/mbgl/style/sources/custom_vector_source.cpp b/src/mbgl/style/sources/custom_vector_source.cpp index e0bbd85af7..38e708a7a8 100644 --- a/src/mbgl/style/sources/custom_vector_source.cpp +++ b/src/mbgl/style/sources/custom_vector_source.cpp @@ -1,18 +1,94 @@ #include <mbgl/style/sources/custom_vector_source.hpp> #include <mbgl/style/sources/custom_vector_source_impl.hpp> +#include <mbgl/actor/scheduler.hpp> namespace mbgl { namespace style { +class CustomTileLoader::Impl { +public: + Impl(TileFunction&& fetchTileFn, TileFunction&& cancelTileFn) { + fetchTileFunction = std::move(fetchTileFn); + cancelTileFunction = std::move(cancelTileFn); + } + + void fetchTile(const CanonicalTileID& tileID, ActorRef<SetTileDataFunction> callbackRef) { + fetchTileFunction(tileID); + auto insertResult = tileCallbackMap.insert({tileID, callbackRef}); + if (insertResult.second == false) { + insertResult.first->second = callbackRef; + } + } + + void cancelTile(const CanonicalTileID& tileID) { + if(tileCallbackMap.find(tileID) != tileCallbackMap.end()) + cancelTileFunction(tileID); + } + + void removeTile(const CanonicalTileID& tileID) { + tileCallbackMap.erase(tileID); + } + + void setTileData(const CanonicalTileID& tileID, const mapbox::geojson::geojson& data) { + auto iter = tileCallbackMap.find(tileID); + if (iter == tileCallbackMap.end()) return; + iter->second.invoke(&SetTileDataFunction::operator(), tileID, data); + } + +private: + TileFunction fetchTileFunction; + TileFunction cancelTileFunction; + std::unordered_map<CanonicalTileID, ActorRef<SetTileDataFunction>> tileCallbackMap; +}; + + +CustomTileLoader::CustomTileLoader(TileFunction&& fetchTileFn, TileFunction&& cancelTileFn) + : impl(new CustomTileLoader::Impl(std::move(fetchTileFn), std::move(cancelTileFn))) { + +} + +CustomTileLoader::~CustomTileLoader() { + delete impl; + impl = nullptr; +} + +void CustomTileLoader::fetchTile(const CanonicalTileID& tileID, ActorRef<SetTileDataFunction> callbackRef) { + impl->fetchTile(tileID, callbackRef); +} + +void CustomTileLoader::cancelTile(const CanonicalTileID& tileID) { + impl->cancelTile(tileID); +} + +void CustomTileLoader::setTileData(const CanonicalTileID& tileID, const mapbox::geojson::geojson& data) { + impl->setTileData(tileID, data); +} + +void CustomTileLoader::removeTile(const CanonicalTileID& tileID) { + impl->removeTile(tileID); +} + CustomVectorSource::CustomVectorSource(std::string id, const GeoJSONOptions options, - FetchTileFunction fetchTileFn) - : Source(makeMutable<CustomVectorSource::Impl>(std::move(id), options, fetchTileFn)) { + TileFunction fetchTileFn, + TileFunction cancelTileFn) + : Source(makeMutable<CustomVectorSource::Impl>(std::move(id), options)), + mailbox(std::make_shared<Mailbox>(*Scheduler::GetCurrent())), + loader(std::move(fetchTileFn), std::move(cancelTileFn)) { } +const CustomVectorSource::Impl& CustomVectorSource::impl() const { + return static_cast<const CustomVectorSource::Impl&>(*baseImpl); +} void CustomVectorSource::loadDescription(FileSource&) { + baseImpl = makeMutable<CustomVectorSource::Impl>(impl(), ActorRef<CustomTileLoader>(loader, mailbox)); loaded = true; } +void CustomVectorSource::setTileData(const CanonicalTileID& tileID, + const mapbox::geojson::geojson& data) { + loader.setTileData(tileID, data); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/custom_vector_source_impl.cpp b/src/mbgl/style/sources/custom_vector_source_impl.cpp index 1b5ee4e5d1..d1f20045cb 100644 --- a/src/mbgl/style/sources/custom_vector_source_impl.cpp +++ b/src/mbgl/style/sources/custom_vector_source_impl.cpp @@ -9,11 +9,17 @@ namespace mbgl { namespace style { CustomVectorSource::Impl::Impl(std::string id_, - const GeoJSONOptions options_, - FetchTileFunction fetchTileFn_) + const GeoJSONOptions options_) : Source::Impl(SourceType::CustomVector, std::move(id_)), options(options_), - fetchTileFn(fetchTileFn_) { + loaderRef({}) { +} + +CustomVectorSource::Impl::Impl(const Impl& impl, ActorRef<CustomTileLoader> loaderRef_) + : Source::Impl(impl), + options(impl.options), + loaderRef(loaderRef_){ + } optional<std::string> CustomVectorSource::Impl::getAttribution() const { @@ -24,8 +30,8 @@ GeoJSONOptions CustomVectorSource::Impl::getOptions() const { return options; } -FetchTileFunction CustomVectorSource::Impl::getFetchTileFunction() const { - return fetchTileFn; +optional<ActorRef<CustomTileLoader>> CustomVectorSource::Impl::getTileLoader() const { + return loaderRef; } } // namespace style diff --git a/src/mbgl/style/sources/custom_vector_source_impl.hpp b/src/mbgl/style/sources/custom_vector_source_impl.hpp index aa07bc800d..af103f4c7e 100644 --- a/src/mbgl/style/sources/custom_vector_source_impl.hpp +++ b/src/mbgl/style/sources/custom_vector_source_impl.hpp @@ -3,24 +3,24 @@ #include <mbgl/style/source_impl.hpp> #include <mbgl/style/sources/custom_vector_source.hpp> #include <mbgl/tile/tile_id.hpp> +#include <mbgl/actor/actor_ref.hpp> namespace mbgl { namespace style { class CustomVectorSource::Impl : public Source::Impl { public: - Impl(std::string id, - GeoJSONOptions options, - FetchTileFunction fetchTileFn); + Impl(std::string id, GeoJSONOptions options); + Impl(const Impl&, ActorRef<CustomTileLoader>); optional<std::string> getAttribution() const final; GeoJSONOptions getOptions() const; - FetchTileFunction getFetchTileFunction() const; + optional<ActorRef<CustomTileLoader>> getTileLoader() const; private: GeoJSONOptions options; - FetchTileFunction fetchTileFn; + optional<ActorRef<CustomTileLoader>> loaderRef; }; } // namespace style diff --git a/src/mbgl/tile/custom_tile.cpp b/src/mbgl/tile/custom_tile.cpp index b7057d447e..c462e95d0f 100644 --- a/src/mbgl/tile/custom_tile.cpp +++ b/src/mbgl/tile/custom_tile.cpp @@ -1,4 +1,5 @@ #include <mbgl/tile/custom_tile.hpp> +#include <mbgl/tile/geojson_tile_data.hpp> #include <mbgl/tile/geometry_tile_data.hpp> #include <mbgl/renderer/query.hpp> #include <mbgl/renderer/tile_parameters.hpp> @@ -9,24 +10,26 @@ #include <mapbox/geojsonvt.hpp> namespace mbgl { +namespace style { CustomTile::CustomTile(const OverscaledTileID& overscaledTileID, std::string sourceID_, const TileParameters& parameters, - const style::GeoJSONOptions options_) + const style::GeoJSONOptions options_, + ActorRef<style::CustomTileLoader> loader_) : GeometryTile(overscaledTileID, sourceID_, parameters), + necessity(Resource::Optional), options(options_), + loader(loader_), actor(*Scheduler::GetCurrent(), std::bind(&CustomTile::setTileData, this, std::placeholders::_1, std::placeholders::_2)) { +} +CustomTile::~CustomTile() { + loader.invoke(&style::CustomTileLoader::removeTile, id.canonical); } -void CustomTile::setTileData(const CanonicalTileID&, const style::FetchTileResult& result) { - if (result.is<style::Error>()) { - Log::Error(Event::Render, "FetchTile (%d, %d, %d) error: %s", id.canonical.z, id.canonical.x, id.canonical.y, result.get<style::Error>().message.c_str()); - return; - } +void CustomTile::setTileData(const CanonicalTileID&, const mapbox::geojson::geojson& geoJSON) { - auto geoJSON = result.get<mapbox::geojson::geojson>(); auto data = mapbox::geometry::feature_collection<int16_t>(); if (geoJSON.is<FeatureCollection>() && !geoJSON.get<FeatureCollection>().empty()) { const double scale = util::EXTENT / options.tileSize; @@ -42,7 +45,16 @@ void CustomTile::setTileData(const CanonicalTileID&, const style::FetchTileResul setData(std::make_unique<GeoJSONTileData>(std::move(data))); } -void CustomTile::setNecessity(Necessity) {} +void CustomTile::setNecessity(Necessity newNecessity) { + if (newNecessity != necessity) { + necessity = newNecessity; + if (necessity == Necessity::Required) { + loader.invoke(&style::CustomTileLoader::fetchTile, id.canonical, actor.self()); + } else if(!isRenderable()) { + loader.invoke(&style::CustomTileLoader::cancelTile, id.canonical); + } + } +} void CustomTile::querySourceFeatures( std::vector<Feature>& result, @@ -66,8 +78,5 @@ void CustomTile::querySourceFeatures( } } -ActorRef<style::FetchTileCallback> CustomTile::fetchTileCallback() { - return actor.self(); -} - +} // namespace style } // namespace mbgl diff --git a/src/mbgl/tile/custom_tile.hpp b/src/mbgl/tile/custom_tile.hpp index 66606d2224..734ea2bac1 100644 --- a/src/mbgl/tile/custom_tile.hpp +++ b/src/mbgl/tile/custom_tile.hpp @@ -9,14 +9,17 @@ namespace mbgl { class TileParameters; +namespace style { + class CustomTile: public GeometryTile { public: CustomTile(const OverscaledTileID&, std::string sourceID, const TileParameters&, - const style::GeoJSONOptions); - - void setTileData(const CanonicalTileID& tileID, const style::FetchTileResult& result); + const style::GeoJSONOptions, + ActorRef<style::CustomTileLoader> loader); + ~CustomTile() override; + void setTileData(const CanonicalTileID& tileID, const mapbox::geojson::geojson& data); void setNecessity(Necessity) final; @@ -24,10 +27,12 @@ public: std::vector<Feature>& result, const SourceQueryOptions&) override; - ActorRef<style::FetchTileCallback> fetchTileCallback(); private: + Necessity necessity; const style::GeoJSONOptions options; - Actor<style::FetchTileCallback> actor; + ActorRef<style::CustomTileLoader> loader; + Actor<style::SetTileDataFunction> actor; }; +} // namespace style } // namespace mbgl |