diff options
author | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2020-01-24 12:38:08 +0200 |
---|---|---|
committer | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2020-01-24 14:15:37 +0200 |
commit | 61ab64906e6ebca9fe27463d9ea12a747314386c (patch) | |
tree | ca59b4331d0a50336a1becdc17fd603db49836a7 | |
parent | 9e1bcc4fdc6cb3bb1710a6e2cefd7e642159b2f3 (diff) | |
download | qtlocation-mapboxgl-61ab64906e6ebca9fe27463d9ea12a747314386c.tar.gz |
GeoJSONSource is re-using the existing scheduler
Thus avoiding spawing new threads on `setGeoJSONData()` calls.
-rw-r--r-- | include/mbgl/style/sources/geojson_source.hpp | 5 | ||||
-rw-r--r-- | src/mbgl/style/sources/geojson_source.cpp | 16 | ||||
-rw-r--r-- | src/mbgl/style/sources/geojson_source_impl.cpp | 26 |
3 files changed, 33 insertions, 14 deletions
diff --git a/include/mbgl/style/sources/geojson_source.hpp b/include/mbgl/style/sources/geojson_source.hpp index df2f9d2daf..750d29627d 100644 --- a/include/mbgl/style/sources/geojson_source.hpp +++ b/include/mbgl/style/sources/geojson_source.hpp @@ -42,7 +42,8 @@ public: using TileFeatures = mapbox::feature::feature_collection<int16_t>; using Features = mapbox::feature::feature_collection<double>; static std::shared_ptr<GeoJSONData> create(const GeoJSON&, - Immutable<GeoJSONOptions> = GeoJSONOptions::defaultOptions()); + Immutable<GeoJSONOptions> = GeoJSONOptions::defaultOptions(), + std::shared_ptr<Scheduler> scheduler = nullptr); virtual ~GeoJSONData() = default; virtual void getTile(const CanonicalTileID&, const std::function<void(TileFeatures)>&) = 0; @@ -53,6 +54,8 @@ public: const std::uint32_t limit = 10u, const std::uint32_t offset = 0u) = 0; virtual std::uint8_t getClusterExpansionZoom(std::uint32_t) = 0; + + virtual std::shared_ptr<Scheduler> getScheduler() { return nullptr; } }; class GeoJSONSource final : public Source { diff --git a/src/mbgl/style/sources/geojson_source.cpp b/src/mbgl/style/sources/geojson_source.cpp index d8414c1f84..3afccf07f2 100644 --- a/src/mbgl/style/sources/geojson_source.cpp +++ b/src/mbgl/style/sources/geojson_source.cpp @@ -39,8 +39,20 @@ void GeoJSONSource::setURL(const std::string& url_) { } } +namespace { + +inline std::shared_ptr<GeoJSONData> createGeoJSONData(const mapbox::geojson::geojson& geoJSON, + const GeoJSONSource::Impl& impl) { + if (auto data = impl.getData().lock()) { + return GeoJSONData::create(geoJSON, impl.getOptions(), data->getScheduler()); + } + return GeoJSONData::create(geoJSON, impl.getOptions()); +} + +} // namespace + void GeoJSONSource::setGeoJSON(const mapbox::geojson::geojson& geoJSON) { - setGeoJSONData(GeoJSONData::create(geoJSON, impl().getOptions())); + setGeoJSONData(createGeoJSONData(geoJSON, impl())); } void GeoJSONSource::setGeoJSONData(std::shared_ptr<GeoJSONData> geoJSONData) { @@ -83,7 +95,7 @@ void GeoJSONSource::loadDescription(FileSource& fileSource) { conversion::Error error; std::shared_ptr<GeoJSONData> geoJSONData; if (optional<GeoJSON> geoJSON = conversion::convertJSON<GeoJSON>(*data, error)) { - geoJSONData = GeoJSONData::create(*geoJSON, current.getOptions()); + geoJSONData = createGeoJSONData(*geoJSON, current); } else { // Create an empty GeoJSON VT object to make sure we're not infinitely waiting for tiles to load. Log::Error(Event::ParseStyle, "Failed to parse GeoJSON data: %s", error.message.c_str()); diff --git a/src/mbgl/style/sources/geojson_source_impl.cpp b/src/mbgl/style/sources/geojson_source_impl.cpp index e73876b429..bb5445f02b 100644 --- a/src/mbgl/style/sources/geojson_source_impl.cpp +++ b/src/mbgl/style/sources/geojson_source_impl.cpp @@ -13,8 +13,7 @@ namespace mbgl { namespace style { -class GeoJSONVTData : public GeoJSONData { -public: +class GeoJSONVTData final : public GeoJSONData { void getTile(const CanonicalTileID& id, const std::function<void(TileFeatures)>& fn) final { assert(fn); scheduler->scheduleAndReplyValue( @@ -29,18 +28,21 @@ public: return 0; } -private: + std::shared_ptr<Scheduler> getScheduler() final { return scheduler; } + friend GeoJSONData; - GeoJSONVTData(const GeoJSON& geoJSON, const mapbox::geojsonvt::Options& options) - : impl(std::make_shared<mapbox::geojsonvt::GeoJSONVT>(geoJSON, options)), - scheduler(Scheduler::GetSequenced()) {} + GeoJSONVTData(const GeoJSON& geoJSON, + const mapbox::geojsonvt::Options& options, + std::shared_ptr<Scheduler> scheduler_) + : impl(std::make_shared<mapbox::geojsonvt::GeoJSONVT>(geoJSON, options)), scheduler(std::move(scheduler_)) { + assert(scheduler); + } std::shared_ptr<mapbox::geojsonvt::GeoJSONVT> impl; // Accessed on worker thread. std::shared_ptr<Scheduler> scheduler; }; -class SuperclusterData : public GeoJSONData { -public: +class SuperclusterData final : public GeoJSONData { void getTile(const CanonicalTileID& id, const std::function<void(TileFeatures)>& fn) final { assert(fn); fn(impl.getTile(id.z, id.x, id.y)); @@ -56,7 +58,6 @@ public: return impl.getClusterExpansionZoom(cluster_id); } -private: friend GeoJSONData; SuperclusterData(const Features& features, const mapbox::supercluster::Options& options) : impl(features, options) {} @@ -78,7 +79,9 @@ T evaluateFeature(const mapbox::feature::feature<double>& f, } // static -std::shared_ptr<GeoJSONData> GeoJSONData::create(const GeoJSON& geoJSON, Immutable<GeoJSONOptions> options) { +std::shared_ptr<GeoJSONData> GeoJSONData::create(const GeoJSON& geoJSON, + Immutable<GeoJSONOptions> options, + std::shared_ptr<Scheduler> scheduler) { constexpr double scale = util::EXTENT / util::tileSize; if (options->cluster && geoJSON.is<Features>() && !geoJSON.get<Features>().empty()) { mapbox::supercluster::Options clusterOptions; @@ -114,7 +117,8 @@ std::shared_ptr<GeoJSONData> GeoJSONData::create(const GeoJSON& geoJSON, Immutab vtOptions.buffer = ::round(scale * options->buffer); vtOptions.tolerance = scale * options->tolerance; vtOptions.lineMetrics = options->lineMetrics; - return std::shared_ptr<GeoJSONData>(new GeoJSONVTData(geoJSON, vtOptions)); + if (!scheduler) scheduler = Scheduler::GetSequenced(); + return std::shared_ptr<GeoJSONData>(new GeoJSONVTData(geoJSON, vtOptions, std::move(scheduler))); } GeoJSONSource::Impl::Impl(std::string id_, Immutable<GeoJSONOptions> options_) |