diff --git a/include/mbgl/style/sources/geojson_source.hpp b/include/mbgl/style/sources/geojson_source.hpp index 1759c4fb242..25e0e068b24 100644 --- a/include/mbgl/style/sources/geojson_source.hpp +++ b/include/mbgl/style/sources/geojson_source.hpp @@ -41,8 +41,8 @@ class GeoJSONData { using TileFeatures = mapbox::feature::feature_collection; using Features = mapbox::feature::feature_collection; static std::shared_ptr create(const GeoJSON&, - const Immutable& = GeoJSONOptions::defaultOptions(), - std::shared_ptr scheduler = nullptr); + std::shared_ptr scheduler, + const Immutable& = GeoJSONOptions::defaultOptions()); virtual ~GeoJSONData() = default; virtual void getTile(const CanonicalTileID&, const std::function&) = 0; @@ -51,8 +51,6 @@ class GeoJSONData { 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 std::shared_ptr getScheduler() { return nullptr; } }; class GeoJSONSource final : public Source { @@ -83,6 +81,7 @@ class GeoJSONSource final : public Source { std::optional url; std::unique_ptr req; std::shared_ptr threadPool; + std::shared_ptr sequencedScheduler; mapbox::base::WeakPtrFactory weakFactory{this}; }; diff --git a/platform/android/MapboxGLAndroidSDK/src/cpp/style/sources/geojson_source.cpp b/platform/android/MapboxGLAndroidSDK/src/cpp/style/sources/geojson_source.cpp index 0a27b3b8535..bc37a5f5c5f 100644 --- a/platform/android/MapboxGLAndroidSDK/src/cpp/style/sources/geojson_source.cpp +++ b/platform/android/MapboxGLAndroidSDK/src/cpp/style/sources/geojson_source.cpp @@ -245,7 +245,8 @@ void FeatureConverter::convertJson(std::shared_ptr json, ActorRef @@ -257,7 +258,8 @@ void FeatureConverter::convertObject( android::UniqueEnv _env = android::AttachEnv(); // Convert the jni object auto geometry = JNIType::convert(*_env, *jObject); - callback.invoke(&GeoJSONDataCallback::operator(), style::GeoJSONData::create(geometry, options)); + callback.invoke(&GeoJSONDataCallback::operator(), + style::GeoJSONData::create(geometry, sequencedScheduler, options)); } Update::Update(Converter _converterFn, std::unique_ptr> _callback) diff --git a/platform/android/MapboxGLAndroidSDK/src/cpp/style/sources/geojson_source.hpp b/platform/android/MapboxGLAndroidSDK/src/cpp/style/sources/geojson_source.hpp index e0453ecdc68..6a360484b53 100644 --- a/platform/android/MapboxGLAndroidSDK/src/cpp/style/sources/geojson_source.hpp +++ b/platform/android/MapboxGLAndroidSDK/src/cpp/style/sources/geojson_source.hpp @@ -16,7 +16,8 @@ using GeoJSONDataCallback = std::function options_) - : options(std::move(options_)) {} + : options(std::move(options_)), + sequencedScheduler(Scheduler::GetSequenced()) {} void convertJson(std::shared_ptr, ActorRef); template @@ -25,6 +26,7 @@ class FeatureConverter { private: Immutable options; + std::shared_ptr sequencedScheduler; }; struct Update { diff --git a/src/mbgl/style/sources/geojson_source.cpp b/src/mbgl/style/sources/geojson_source.cpp index 11d323a0be2..a00477d3acf 100644 --- a/src/mbgl/style/sources/geojson_source.cpp +++ b/src/mbgl/style/sources/geojson_source.cpp @@ -21,7 +21,8 @@ Immutable GeoJSONOptions::defaultOptions() { GeoJSONSource::GeoJSONSource(std::string id, Immutable options) : Source(makeMutable(std::move(id), std::move(options))), - threadPool(Scheduler::GetBackground()) {} + threadPool(Scheduler::GetBackground()), + sequencedScheduler(Scheduler::GetSequenced()) {} GeoJSONSource::~GeoJSONSource() = default; @@ -40,20 +41,8 @@ void GeoJSONSource::setURL(const std::string& url_) { } } -namespace { - -inline std::shared_ptr 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(createGeoJSONData(geoJSON, impl())); + setGeoJSONData(GeoJSONData::create(geoJSON, sequencedScheduler, impl().getOptions())); } void GeoJSONSource::setGeoJSONData(std::shared_ptr geoJSONData) { @@ -88,13 +77,15 @@ void GeoJSONSource::loadDescription(FileSource& fileSource) { } else if (res.noContent) { observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error("unexpectedly empty GeoJSON"))); } else { - auto makeImplInBackground = [currentImpl = baseImpl, data = res.data]() -> Immutable { + auto makeImplInBackground = [currentImpl = baseImpl, + data = res.data, + seqScheduler{sequencedScheduler}]() -> Immutable { assert(data); auto& current = static_cast(*currentImpl); conversion::Error error; std::shared_ptr geoJSONData; if (std::optional geoJSON = conversion::convertJSON(*data, error)) { - geoJSONData = createGeoJSONData(*geoJSON, current); + geoJSONData = GeoJSONData::create(*geoJSON, std::move(seqScheduler), current.getOptions()); } else { // Create an empty GeoJSON VT object to make sure we're not // infinitely waiting for tiles to load. diff --git a/src/mbgl/style/sources/geojson_source_impl.cpp b/src/mbgl/style/sources/geojson_source_impl.cpp index 2d733d5f6ba..84872cbfe0d 100644 --- a/src/mbgl/style/sources/geojson_source_impl.cpp +++ b/src/mbgl/style/sources/geojson_source_impl.cpp @@ -38,8 +38,6 @@ class GeoJSONVTData final : public GeoJSONData { std::uint8_t getClusterExpansionZoom(std::uint32_t) final { return 0; } - std::shared_ptr getScheduler() final { return scheduler; } - friend GeoJSONData; GeoJSONVTData(const GeoJSON& geoJSON, const mapbox::geojsonvt::Options& options, @@ -91,8 +89,8 @@ T evaluateFeature(const mapbox::feature::feature& f, // static std::shared_ptr GeoJSONData::create(const GeoJSON& geoJSON, - const Immutable& options, - std::shared_ptr scheduler) { + std::shared_ptr scheduler, + const Immutable& options) { constexpr double scale = util::EXTENT / util::tileSize_D; if (options->cluster && geoJSON.is() && !geoJSON.get().empty()) { mapbox::supercluster::Options clusterOptions; @@ -128,7 +126,6 @@ std::shared_ptr GeoJSONData::create(const GeoJSON& geoJSON, vtOptions.buffer = static_cast(::round(scale * options->buffer)); vtOptions.tolerance = scale * options->tolerance; vtOptions.lineMetrics = options->lineMetrics; - if (!scheduler) scheduler = Scheduler::GetSequenced(); return std::shared_ptr(new GeoJSONVTData(geoJSON, vtOptions, std::move(scheduler))); } diff --git a/test/style/source.test.cpp b/test/style/source.test.cpp index 0a2a52b5afd..55655d0fda3 100644 --- a/test/style/source.test.cpp +++ b/test/style/source.test.cpp @@ -969,8 +969,10 @@ TEST(Source, VectorSourceUrlSetTiles) { TEST(Source, GeoJSONSourceTilesAfterDataReset) { SourceTest test; GeoJSONSource source("source"); - auto geoJSONData = GeoJSONData::create(mapbox::geojson::parse( - R"({"geometry": {"type": "Point", "coordinates": [1.1, 1.1]}, "type": "Feature", "properties": {}})")); + auto geoJSONData = GeoJSONData::create( + mapbox::geojson::parse( + R"({"geometry": {"type": "Point", "coordinates": [1.1, 1.1]}, "type": "Feature", "properties": {}})"), + Scheduler::GetSequenced()); source.setGeoJSONData(geoJSONData); RenderGeoJSONSource renderSource{staticImmutableCast(source.baseImpl), test.threadPool};