Implemented synchronous update for GeoJSON source (#3968)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Alex Cristici
2025-12-03 01:02:29 +02:00
committed by GitHub
parent f25a4581fe
commit 29d70ef0bb
32 changed files with 408 additions and 116 deletions

View File

@@ -217,6 +217,8 @@ list(APPEND INCLUDE_FILES
${PROJECT_SOURCE_DIR}/include/mbgl/actor/established_actor.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/actor/mailbox.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/actor/message.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/actor/optional_actor.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/actor/optional_actor_ref.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/actor/scheduler.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/annotation/annotation.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/backend_scope.hpp

View File

@@ -618,6 +618,8 @@ MLN_CORE_HEADERS = [
"include/mbgl/actor/established_actor.hpp",
"include/mbgl/actor/mailbox.hpp",
"include/mbgl/actor/message.hpp",
"include/mbgl/actor/optional_actor.hpp",
"include/mbgl/actor/optional_actor_ref.hpp",
"include/mbgl/actor/scheduler.hpp",
"include/mbgl/annotation/annotation.hpp",
"include/mbgl/gfx/backend.hpp",

View File

@@ -4,6 +4,7 @@
#include <mbgl/actor/mailbox.hpp>
#include <mbgl/actor/message.hpp>
#include <mbgl/actor/actor_ref.hpp>
#include <mbgl/actor/optional_actor_ref.hpp>
#include <memory>
#include <future>
@@ -31,7 +32,8 @@ public:
// Construct the Object from a parameter pack `args` (i.e. `Object(args...)`)
template <typename U = Object, class... Args>
EstablishedActor(Scheduler& scheduler, AspiringActor<Object>& parent_, Args&&... args)
requires(std::is_constructible_v<U, Args...> || std::is_constructible_v<U, ActorRef<U>, Args...>)
requires(std::is_constructible_v<U, Args...> || std::is_constructible_v<U, ActorRef<U>, Args...> ||
std::is_constructible_v<U, OptionalActorRef<U>, Args...>)
: parent(parent_) {
emplaceObject(std::forward<Args>(args)...);
parent.mailbox->open(scheduler);
@@ -40,7 +42,8 @@ public:
// Construct the Object from a parameter pack `args` (i.e. `Object(args...)`)
template <typename U = Object, class... Args>
EstablishedActor(const TaggedScheduler& scheduler, AspiringActor<Object>& parent_, Args&&... args)
requires(std::is_constructible_v<U, Args...> || std::is_constructible_v<U, ActorRef<U>, Args...>)
requires(std::is_constructible_v<U, Args...> || std::is_constructible_v<U, ActorRef<U>, Args...> ||
std::is_constructible_v<U, OptionalActorRef<U>, Args...>)
: parent(parent_) {
emplaceObject(std::forward<Args>(args)...);
parent.mailbox->open(scheduler);
@@ -68,6 +71,14 @@ public:
}
private:
// Enabled for Objects with a constructor taking OptionalActorRef<Object> as the first parameter
template <typename U = Object, class... Args>
void emplaceObject(Args&&... args_)
requires(std::is_constructible_v<U, OptionalActorRef<U>, Args...>)
{
new (&parent.objectStorage) Object(OptionalActorRef(parent.self()), std::forward<Args>(args_)...);
}
// Enabled for Objects with a constructor taking ActorRef<Object> as the first parameter
template <typename U = Object, class... Args>
void emplaceObject(Args&&... args_)

View File

@@ -0,0 +1,82 @@
#pragma once
#include <mbgl/actor/actor.hpp>
#include <mbgl/actor/optional_actor_ref.hpp>
namespace mbgl {
class TaggedScheduler;
template <class Object>
class OptionalActor {
public:
template <class... Args>
OptionalActor(bool syncObject, Scheduler& scheduler, Args&&... args) {
if (syncObject) {
object = std::make_unique<SyncObject>(std::forward<Args>(args)...);
selfRef = OptionalActorRef(object->self());
} else {
actor = std::make_unique<Actor<Object>>(scheduler, std::forward<Args>(args)...);
selfRef = OptionalActorRef(actor->self());
}
}
template <class... Args>
OptionalActor(bool syncObject, const TaggedScheduler& scheduler, Args&&... args) {
if (syncObject) {
object = std::make_unique<SyncObject>(std::forward<Args>(args)...);
selfRef = OptionalActorRef(object->self());
} else {
actor = std::make_unique<Actor<Object>>(scheduler, std::forward<Args>(args)...);
selfRef = OptionalActorRef(actor->self());
}
}
template <class... Args>
OptionalActor(bool syncObject, std::shared_ptr<Scheduler> scheduler, Args&&... args) {
if (syncObject) {
object = std::make_unique<SyncObject>(std::forward<Args>(args)...);
selfRef = OptionalActorRef(object->self());
} else {
actor = std::make_unique<Actor<Object>>(scheduler, std::forward<Args>(args)...);
selfRef = OptionalActorRef(actor->self());
}
}
OptionalActor(const OptionalActor&) = delete;
const OptionalActorRef<Object>& self() { return selfRef; }
private:
class SyncObject {
public:
template <class... Args>
SyncObject(Args&&... args)
requires(std::is_constructible_v<Object, OptionalActorRef<Object>, Args...>)
{
new (&objectStorage) Object(OptionalActorRef(self()), std::forward<Args>(args)...);
}
template <class... Args>
SyncObject(Args&&... args)
requires(std::is_constructible_v<Object, Args...>)
{
new (&objectStorage) Object(std::forward<Args>(args)...);
}
SyncObject(const SyncObject&) = delete;
~SyncObject() { self().~Object(); }
Object& self() { return reinterpret_cast<Object&>(objectStorage); }
private:
std::aligned_storage_t<sizeof(Object)> objectStorage;
};
std::unique_ptr<SyncObject> object;
std::unique_ptr<Actor<Object>> actor;
OptionalActorRef<Object> selfRef;
};
} // namespace mbgl

View File

@@ -0,0 +1,40 @@
#pragma once
#include <mbgl/actor/actor_ref.hpp>
namespace mbgl {
class TaggedScheduler;
template <class Object>
class OptionalActorRef {
public:
OptionalActorRef() = default;
OptionalActorRef(bool syncRef, Object& object, std::weak_ptr<Mailbox> weakMailbox) {
if (syncRef) {
objectRef = &object;
} else {
actorRef = std::make_optional<ActorRef<Object>>(object, weakMailbox);
}
}
explicit OptionalActorRef(Object& object) { objectRef = &object; }
explicit OptionalActorRef(ActorRef<Object> actorRef_) { actorRef = actorRef_; }
template <typename Fn, class... Args>
void invoke(Fn fn, Args&&... args) const {
if (objectRef) {
(objectRef->*fn)(std::forward<Args>(args)...);
} else if (actorRef) {
actorRef->invoke(fn, std::forward<Args>(args)...);
}
}
private:
Object* objectRef = nullptr;
std::optional<ActorRef<Object>> actorRef;
};
} // namespace mbgl

View File

@@ -35,6 +35,9 @@ struct GeoJSONOptions {
using ClusterProperties = std::map<std::string, ClusterExpression>;
ClusterProperties clusterProperties;
// Update options
bool synchronousUpdate = false;
static Immutable<GeoJSONOptions> defaultOptions();
};
class GeoJSONData {
@@ -46,7 +49,7 @@ public:
const Immutable<GeoJSONOptions>& = GeoJSONOptions::defaultOptions());
virtual ~GeoJSONData() = default;
virtual void getTile(const CanonicalTileID&, const std::function<void(TileFeatures)>&) = 0;
virtual void getTile(const CanonicalTileID&, const std::function<void(TileFeatures)>&, bool runSynchronously) = 0;
// SuperclusterData
virtual Features getChildren(std::uint32_t) = 0;
@@ -76,6 +79,8 @@ public:
mapbox::base::WeakPtr<Source> makeWeakPtr() override { return weakFactory.makeWeakPtr(); }
bool isUpdateSynchronous() const noexcept;
protected:
Mutable<Source::Impl> createMutable() const noexcept final;

View File

@@ -242,6 +242,10 @@ void GeoJSONSource::setCollectionSync(jni::JNIEnv& env, const jni::Object<JNITyp
setAsync(converterFn);
}
jboolean GeoJSONSource::isUpdateSynchronous(jni::JNIEnv&) {
return source.as<style::GeoJSONSource>()->isUpdateSynchronous();
}
void GeoJSONSource::registerNative(jni::JNIEnv& env) {
// Lookup the class
static auto& javaClass = jni::Class<GeoJSONSource>::Singleton(env);
@@ -269,7 +273,8 @@ void GeoJSONSource::registerNative(jni::JNIEnv& env) {
METHOD(&GeoJSONSource::querySourceFeatures, "querySourceFeatures"),
METHOD(&GeoJSONSource::getClusterChildren, "nativeGetClusterChildren"),
METHOD(&GeoJSONSource::getClusterLeaves, "nativeGetClusterLeaves"),
METHOD(&GeoJSONSource::getClusterExpansionZoom, "nativeGetClusterExpansionZoom"));
METHOD(&GeoJSONSource::getClusterExpansionZoom, "nativeGetClusterExpansionZoom"),
METHOD(&GeoJSONSource::isUpdateSynchronous, "nativeIsUpdateSynchronous"));
}
void FeatureConverter::convertJson(std::shared_ptr<std::string> json, ActorRef<GeoJSONDataCallback> callback) {

View File

@@ -89,6 +89,8 @@ private:
template <class JNIType>
void setCollectionSync(jni::JNIEnv&, const jni::Object<JNIType>&);
jboolean isUpdateSynchronous(jni::JNIEnv&);
}; // class GeoJSONSource
} // namespace android

View File

@@ -23,10 +23,15 @@ class LayerSourceProvider {
@NonNull
GeoJsonSource generateSource(Feature locationFeature) {
return generateSource(locationFeature, false);
}
@NonNull
GeoJsonSource generateSource(Feature locationFeature, Boolean synchronousUpdate) {
return new GeoJsonSource(
LocationComponentConstants.LOCATION_SOURCE,
locationFeature,
new GeoJsonOptions().withMaxZoom(16)
new GeoJsonOptions().withMaxZoom(16).withSynchronousUpdate(synchronousUpdate)
);
}

View File

@@ -318,7 +318,7 @@ final class SymbolLocationLayerRenderer implements LocationLayerRenderer {
}
private void addLocationSource() {
locationSource = layerSourceProvider.generateSource(locationFeature);
locationSource = layerSourceProvider.generateSource(locationFeature, true);
style.addSource(locationSource);
}

View File

@@ -131,4 +131,15 @@ class GeoJsonOptions : HashMap<String?, Any?>() {
this["clusterProperties"] = properties
return this
}
/**
* Set a flag defining whether or not the fetched tiles for the given source
* should be synchronously updated on the render thread
*
* @param value current setting for synchronous. Defaults to false.
*/
fun withSynchronousUpdate(value: Boolean): GeoJsonOptions {
this["synchronousUpdate"] = value
return this
}
}

View File

@@ -228,36 +228,49 @@ class GeoJsonSource : Source {
}
/**
* Updates the GeoJson with a single feature. The update is performed asynchronously,
* so the data won't be immediately visible or available to query when this method returns.
* Updates the GeoJson with a single feature.
* The update is performed synchronously or asynchronously, based on the source synchronous update flag.
* In the case of asynchronous updates, the data won't be immediately visible or available to query when this method returns.
*
* @param feature the GeoJSON [Feature] to set
*/
fun setGeoJson(feature: Feature?) {
if (detached) {
return
}
checkThread()
nativeSetFeature(feature)
if (nativeIsUpdateSynchronous()) {
nativeSetFeatureSync(feature)
} else {
nativeSetFeature(feature)
}
}
/**
* Updates the GeoJson with a single geometry. The update is performed asynchronously,
* so the data won't be immediately visible or available to query when this method returns.
* Updates the GeoJson with a single geometry.
* The update is performed synchronously or asynchronously, based on the source synchronous update flag.
* In the case of asynchronous updates, the data won't be immediately visible or available to query when this method returns.
*
* @param geometry the GeoJSON [Geometry] to set
*/
fun setGeoJson(geometry: Geometry?) {
if (detached) {
return
}
checkThread()
nativeSetGeometry(geometry)
if (nativeIsUpdateSynchronous()) {
nativeSetGeometrySync(geometry)
} else {
nativeSetGeometry(geometry)
}
}
/**
* Updates the GeoJson. The update is performed asynchronously,
* so the data won't be immediately visible or available to query when this method returns.
* Updates the GeoJson.
* The update is performed synchronously or asynchronously, based on the source synchronous update flag.
* In the case of asynchronous updates, the data won't be immediately visible or available to query when this method returns.
*
* @param featureCollection the GeoJSON FeatureCollection
*/
@@ -266,15 +279,37 @@ class GeoJsonSource : Source {
return
}
checkThread()
var featureCollection = featureCollection
if (featureCollection != null && featureCollection.features() != null) {
val features = featureCollection.features()
val featuresCopy: List<Feature> = ArrayList(features)
nativeSetFeatureCollection(FeatureCollection.fromFeatures(featuresCopy))
featureCollection = FeatureCollection.fromFeatures(featuresCopy)
}
if (nativeIsUpdateSynchronous()) {
nativeSetFeatureCollectionSync(featureCollection)
} else {
nativeSetFeatureCollection(featureCollection)
}
}
/**
* Updates the GeoJson.
* The update is performed synchronously or asynchronously, based on the source synchronous update flag.
* In the case of asynchronous updates, the data won't be immediately visible or available to query when this method returns.
*
* @param json the raw GeoJson FeatureCollection string
*/
fun setGeoJson(json: String) {
if (detached) {
return
}
checkThread()
if (nativeIsUpdateSynchronous()) {
nativeSetGeoJsonStringSync(json)
} else {
nativeSetGeoJsonString(json)
}
}
/**
* Updates the GeoJson with a single feature. The update is performed synchronously,
@@ -282,6 +317,7 @@ class GeoJsonSource : Source {
*
* @param feature the GeoJSON [Feature] to set
*/
@Deprecated("use {@link GeoJsonOptions#withSynchronousUpdate(Boolean)} to enable synchronous updates on construction")
fun setGeoJsonSync(feature: Feature?) {
if (detached) {
return
@@ -296,6 +332,7 @@ class GeoJsonSource : Source {
*
* @param geometry the GeoJSON [Geometry] to set
*/
@Deprecated("use {@link GeoJsonOptions#withSynchronousUpdate(Boolean)} to enable synchronous updates on construction")
fun setGeoJsonSync(geometry: Geometry?) {
if (detached) {
return
@@ -310,6 +347,7 @@ class GeoJsonSource : Source {
*
* @param featureCollection the GeoJSON FeatureCollection
*/
@Deprecated("use {@link GeoJsonOptions#withSynchronousUpdate(Boolean)} to enable synchronous updates on construction")
fun setGeoJsonSync(featureCollection: FeatureCollection?) {
if (detached) {
return
@@ -324,26 +362,13 @@ class GeoJsonSource : Source {
}
}
/**
* Updates the GeoJson. The update is performed asynchronously,
* so the data won't be immediately visible or available to query when this method returns.
*
* @param json the raw GeoJson FeatureCollection string
*/
fun setGeoJson(json: String) {
if (detached) {
return
}
checkThread()
nativeSetGeoJsonString(json)
}
/**
* Updates the GeoJson. The update is performed synchronously,
* so the data will be immediately visible and available to query when this method returns.
*
* @param json the raw GeoJson FeatureCollection string
*/
@Deprecated("use {@link GeoJsonOptions#withSynchronousUpdate(Boolean)} to enable synchronous updates on construction")
fun setGeoJsonSync(json: String) {
if (detached) {
return
@@ -567,6 +592,9 @@ class GeoJsonSource : Source {
@Keep
private external fun nativeGetClusterExpansionZoom(feature: Feature): Int
@Keep
private external fun nativeIsUpdateSynchronous(): Boolean
@Keep
@Throws(Throwable::class)
protected external fun finalize()

View File

@@ -42,7 +42,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -80,7 +81,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -119,7 +121,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -158,7 +161,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -197,7 +201,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -236,7 +241,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -274,7 +280,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -308,7 +315,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val layerSet: Set<String> = HashSet()
@@ -343,7 +351,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(
Mockito.mock(
@@ -390,7 +399,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(
Mockito.mock(
@@ -436,7 +446,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(
Mockito.mock(
@@ -478,7 +489,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(
Mockito.mock(
@@ -520,7 +532,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(
Mockito.mock(
@@ -562,7 +575,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(
Mockito.mock(
@@ -604,7 +618,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(
Mockito.mock(
@@ -678,7 +693,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(
Mockito.mock(
@@ -762,7 +778,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(
Mockito.mock(
@@ -824,7 +841,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(
Mockito.mock(
@@ -886,7 +904,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -928,7 +947,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -973,7 +993,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -1017,7 +1038,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -1062,7 +1084,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -1109,7 +1132,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -1155,7 +1179,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -1202,7 +1227,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -1247,7 +1273,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -1295,7 +1322,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -1332,7 +1360,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(locationSource)
val bitmapProvider = Mockito.mock(
@@ -1368,7 +1397,8 @@ class LocationLayerControllerTest : BaseTest() {
sourceProvider.generateSource(
ArgumentMatchers.any(
Feature::class.java
)
),
ArgumentMatchers.eq(true)
)
).thenReturn(
Mockito.mock(

View File

@@ -2,6 +2,7 @@ package org.maplibre.android.testapp.style;
import org.junit.Ignore;
import org.maplibre.android.style.sources.CannotAddSourceException;
import org.maplibre.android.style.sources.GeoJsonOptions;
import org.maplibre.geojson.Feature;
import org.maplibre.geojson.FeatureCollection;
import org.maplibre.geojson.Point;
@@ -95,15 +96,15 @@ public class GeoJsonSourceTests extends EspressoTest {
public void testUpdateCoalescingSync() {
validateTestSetup();
MapLibreMapAction.invoke(maplibreMap, (uiController, maplibreMap) -> {
GeoJsonSource source = new GeoJsonSource("source");
GeoJsonSource source = new GeoJsonSource("source", new GeoJsonOptions().withSynchronousUpdate(true));
maplibreMap.getStyle().addSource(source);
maplibreMap.getStyle().addLayer(new CircleLayer("layer", source.getId()));
source.setGeoJsonSync(Point.fromLngLat(0, 0));
source.setGeoJsonSync(Point.fromLngLat(-25, -25));
source.setGeoJsonSync(ResourceUtils.readRawResource(rule.getActivity(), R.raw.test_feature_properties));
source.setGeoJson(Point.fromLngLat(0, 0));
source.setGeoJson(Point.fromLngLat(-25, -25));
source.setGeoJson(ResourceUtils.readRawResource(rule.getActivity(), R.raw.test_feature_properties));
source.setGeoJsonSync(Point.fromLngLat(20, 55));
source.setGeoJson(Point.fromLngLat(20, 55));
TestingAsyncUtils.INSTANCE.waitForLayer(uiController, mapView);
assertEquals(1, maplibreMap.queryRenderedFeatures(
maplibreMap.getProjection().toScreenLocation(
@@ -215,12 +216,12 @@ public class GeoJsonSourceTests extends EspressoTest {
protected void testFeatureFromResourceSync(final @RawRes int resource) {
validateTestSetup();
MapLibreMapAction.invoke(maplibreMap, (uiController, maplibreMap) -> {
GeoJsonSource source = new GeoJsonSource("source");
GeoJsonSource source = new GeoJsonSource("source", new GeoJsonOptions().withSynchronousUpdate(true));
maplibreMap.getStyle().addSource(source);
Layer layer = new CircleLayer("layer", source.getId());
maplibreMap.getStyle().addLayer(layer);
source.setGeoJsonSync(Feature.fromJson(ResourceUtils.readRawResource(rule.getActivity(), resource)));
source.setGeoJson(Feature.fromJson(ResourceUtils.readRawResource(rule.getActivity(), resource)));
maplibreMap.getStyle().removeLayer(layer);
maplibreMap.getStyle().removeSource(source);
@@ -262,7 +263,7 @@ public class GeoJsonSourceTests extends EspressoTest {
String geoJsonString = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[0,0]}}";
for (int i = 0; i < 1000; i++) {
// Create a GeoJSON source with the same ID each iteration
GeoJsonSource source = new GeoJsonSource("source");
GeoJsonSource source = new GeoJsonSource("source", new GeoJsonOptions().withSynchronousUpdate(true));
try {
maplibreMap.getStyle().addSource(source);
} catch (CannotAddSourceException ex) {
@@ -270,7 +271,7 @@ public class GeoJsonSourceTests extends EspressoTest {
}
// Schedule a sync update via setGeoJson(String)
source.setGeoJsonSync(geoJsonString);
source.setGeoJson(geoJsonString);
}
});
}

View File

@@ -163,6 +163,12 @@ FOUNDATION_EXTERN MLN_EXPORT const MLNShapeSourceOption MLNShapeSourceOptionSimp
*/
FOUNDATION_EXTERN MLN_EXPORT const MLNShapeSourceOption MLNShapeSourceOptionLineDistanceMetrics;
/**
An `NSNumber` object containing a Boolean flag defining whether or not the fetched tiles for the
given source should be synchronously updated on the render thread. The default value is `NO`.
*/
FOUNDATION_EXTERN MLN_EXPORT const MLNShapeSourceOption MLNShapeSourceOptionSynchronousUpdate;
/**
``MLNShapeSource`` is a map content source that supplies vector shapes to be
shown on the map. The shapes may be instances of ``MLNShape`` or ``MLNShape``,

View File

@@ -27,6 +27,7 @@ const MLNShapeSourceOption MLNShapeSourceOptionMaximumZoomLevelForClustering = @
const MLNShapeSourceOption MLNShapeSourceOptionMinimumZoomLevel = @"MLNShapeSourceOptionMinimumZoomLevel";
const MLNShapeSourceOption MLNShapeSourceOptionSimplificationTolerance = @"MLNShapeSourceOptionSimplificationTolerance";
const MLNShapeSourceOption MLNShapeSourceOptionLineDistanceMetrics = @"MLNShapeSourceOptionLineDistanceMetrics";
const MLNShapeSourceOption MLNShapeSourceOptionSynchronousUpdate = @"MLNShapeSourceOptionSynchronousUpdate";
mbgl::Immutable<mbgl::style::GeoJSONOptions> MLNGeoJSONOptionsFromDictionary(NSDictionary<MLNShapeSourceOption, id> *options) {
auto geoJSONOptions = mbgl::makeMutable<mbgl::style::GeoJSONOptions>();
@@ -154,6 +155,14 @@ mbgl::Immutable<mbgl::style::GeoJSONOptions> MLNGeoJSONOptionsFromDictionary(NSD
geoJSONOptions->lineMetrics = value.boolValue;
}
if (NSNumber *value = options[MLNShapeSourceOptionSynchronousUpdate]) {
if (![value isKindOfClass:[NSNumber class]]) {
[NSException raise:NSInvalidArgumentException
format:@"MLNShapeSourceOptionSynchronousUpdate must be an NSNumber."];
}
geoJSONOptions->synchronousUpdate = value.boolValue;
}
return geoJSONOptions;
}

View File

@@ -24,7 +24,8 @@
MLNShapeSourceOptionMaximumZoomLevel: @99,
MLNShapeSourceOptionBuffer: @1976,
MLNShapeSourceOptionSimplificationTolerance: @0.42,
MLNShapeSourceOptionLineDistanceMetrics: @YES};
MLNShapeSourceOptionLineDistanceMetrics: @YES,
MLNShapeSourceOptionSynchronousUpdate: @YES};
auto mbglOptions = MLNGeoJSONOptionsFromDictionary(options);
XCTAssertTrue(mbglOptions->cluster);
@@ -36,6 +37,7 @@
XCTAssertEqual(mbglOptions->tolerance, 0.42);
XCTAssertTrue(mbglOptions->lineMetrics);
XCTAssertTrue(!mbglOptions->clusterProperties.empty());
XCTAssertTrue(mbglOptions->synchronousUpdate);
options = @{MLNShapeSourceOptionClustered: @"number 1"};
XCTAssertThrows(MLNGeoJSONOptionsFromDictionary(options));

View File

@@ -184,21 +184,21 @@ std::unique_ptr<RenderTree> RenderOrchestrator::createRenderTree(
PropertyEvaluationParameters evaluationParameters{zoomHistory, updateParameters->timePoint, transitionDuration};
evaluationParameters.zoomChanged = zoomChanged;
const TileParameters tileParameters{.pixelRatio = updateParameters->pixelRatio,
.debugOptions = updateParameters->debugOptions,
.transformState = updateParameters->transformState,
.fileSource = updateParameters->fileSource,
.mode = updateParameters->mode,
.annotationManager = updateParameters->annotationManager,
.imageManager = imageManager,
.glyphManager = glyphManager,
.prefetchZoomDelta = updateParameters->prefetchZoomDelta,
.threadPool = threadPool,
.tileLodMinRadius = updateParameters->tileLodMinRadius,
.tileLodScale = updateParameters->tileLodScale,
.tileLodPitchThreshold = updateParameters->tileLodPitchThreshold,
.tileLodZoomShift = updateParameters->tileLodZoomShift,
.dynamicTextureAtlas = dynamicTextureAtlas};
TileParameters tileParameters{.pixelRatio = updateParameters->pixelRatio,
.debugOptions = updateParameters->debugOptions,
.transformState = updateParameters->transformState,
.fileSource = updateParameters->fileSource,
.mode = updateParameters->mode,
.annotationManager = updateParameters->annotationManager,
.imageManager = imageManager,
.glyphManager = glyphManager,
.prefetchZoomDelta = updateParameters->prefetchZoomDelta,
.threadPool = threadPool,
.tileLodMinRadius = updateParameters->tileLodMinRadius,
.tileLodScale = updateParameters->tileLodScale,
.tileLodPitchThreshold = updateParameters->tileLodPitchThreshold,
.tileLodZoomShift = updateParameters->tileLodZoomShift,
.dynamicTextureAtlas = dynamicTextureAtlas};
glyphManager->setURL(updateParameters->glyphURL);
glyphManager->setFontFaces(updateParameters->fontFaces);
@@ -410,6 +410,7 @@ std::unique_ptr<RenderTree> RenderOrchestrator::createRenderTree(
updateList[index] = true;
}
}
tileParameters.isUpdateSynchronous = sourceImpl->isUpdateSynchronous();
source->update(sourceImpl, filteredLayersForSource, sourceNeedsRendering, sourceNeedsRelayout, tileParameters);
filteredLayersForSource.clear();

View File

@@ -94,7 +94,8 @@ void RenderGeoJSONSource::update(Immutable<style::Source::Impl> baseImpl_,
const uint8_t maxZ = impl().getZoomRange().max;
for (const auto& pair : tilePyramid.getTiles()) {
if (pair.first.canonical.z <= maxZ) {
static_cast<GeoJSONTile*>(pair.second.get())->updateData(data_, needsRelayout);
static_cast<GeoJSONTile*>(pair.second.get())
->updateData(data_, needsRelayout, parameters.isUpdateSynchronous);
}
}
}

View File

@@ -38,6 +38,7 @@ public:
double tileLodPitchThreshold = (60.0 / 180.0) * std::numbers::pi;
double tileLodZoomShift = 0;
gfx::DynamicTextureAtlasPtr dynamicTextureAtlas;
bool isUpdateSynchronous = false;
};
} // namespace mbgl

View File

@@ -101,6 +101,16 @@ std::optional<GeoJSONOptions> Converter<GeoJSONOptions>::operator()(const Conver
}
}
const auto synchronousUpdateValue = objectMember(value, "synchronousUpdate");
if (synchronousUpdateValue) {
if (toBool(*synchronousUpdateValue)) {
options.synchronousUpdate = *toBool(*synchronousUpdateValue);
} else {
error.message = "GeoJSON source synchronousUpdate value must be a boolean";
return std::nullopt;
}
}
const auto clusterProperties = objectMember(value, "clusterProperties");
if (clusterProperties) {
if (!isObject(*clusterProperties)) {

View File

@@ -31,6 +31,8 @@ public:
const SourceType type;
const std::string id;
virtual bool isUpdateSynchronous() const { return false; }
protected:
std::optional<uint8_t> prefetchZoomDelta;
std::optional<uint8_t> maxOverscaleFactor;

View File

@@ -123,5 +123,9 @@ Mutable<Source::Impl> GeoJSONSource::createMutable() const noexcept {
return staticMutableCast<Source::Impl>(makeMutable<Impl>(impl()));
}
bool GeoJSONSource::isUpdateSynchronous() const noexcept {
return baseImpl->isUpdateSynchronous();
}
} // namespace style
} // namespace mbgl

View File

@@ -24,14 +24,18 @@ namespace mbgl {
namespace style {
class GeoJSONVTData final : public GeoJSONData {
void getTile(const CanonicalTileID& id, const std::function<void(TileFeatures)>& fn) final {
void getTile(const CanonicalTileID& id, const std::function<void(TileFeatures)>& fn, bool runSynchronously) final {
assert(fn);
sequencedScheduler->scheduleAndReplyValue(
util::SimpleIdentity::Empty,
[id, geoJSONVT_impl = this->impl]() -> TileFeatures {
return geoJSONVT_impl->getTile(id.z, id.x, id.y).features;
},
fn);
if (runSynchronously) {
fn(this->impl->getTile(id.z, id.x, id.y).features);
} else {
sequencedScheduler->scheduleAndReplyValue(
util::SimpleIdentity::Empty,
[id, geoJSONVT_impl = this->impl]() -> TileFeatures {
return geoJSONVT_impl->getTile(id.z, id.x, id.y).features;
},
fn);
}
}
Features getChildren(const std::uint32_t) final { return {}; }
@@ -54,7 +58,7 @@ class GeoJSONVTData final : public GeoJSONData {
};
class SuperclusterData final : public GeoJSONData {
void getTile(const CanonicalTileID& id, const std::function<void(TileFeatures)>& fn) final {
void getTile(const CanonicalTileID& id, const std::function<void(TileFeatures)>& fn, bool) final {
assert(fn);
fn(impl.getTile(id.z, id.x, id.y));
}
@@ -156,5 +160,9 @@ std::optional<std::string> GeoJSONSource::Impl::getAttribution() const {
return {};
}
bool GeoJSONSource::Impl::isUpdateSynchronous() const {
return options->synchronousUpdate;
}
} // namespace style
} // namespace mbgl

View File

@@ -23,6 +23,8 @@ public:
std::optional<std::string> getAttribution() const final;
bool isUpdateSynchronous() const final;
private:
Immutable<GeoJSONOptions> options;
std::shared_ptr<GeoJSONData> data;

View File

@@ -16,22 +16,24 @@ GeoJSONTile::GeoJSONTile(const OverscaledTileID& overscaledTileID,
std::shared_ptr<style::GeoJSONData> data_,
TileObserver* observer_)
: GeometryTile(overscaledTileID, std::move(sourceID_), parameters, observer_) {
updateData(std::move(data_), false /*needsRelayout*/);
updateData(std::move(data_), false /*needsRelayout*/, parameters.isUpdateSynchronous);
}
void GeoJSONTile::updateData(std::shared_ptr<style::GeoJSONData> data_, bool needsRelayout) {
void GeoJSONTile::updateData(std::shared_ptr<style::GeoJSONData> data_, bool needsRelayout, bool runSynchronously) {
MLN_TRACE_FUNC();
assert(data_);
data = std::move(data_);
if (needsRelayout) reset();
data->getTile(id.canonical,
[this, self = weakFactory.makeWeakPtr(), capturedData = data.get()](TileFeatures features) {
// If the data has changed, a new request is being processed, ignore this one
if (auto guard = self.lock(); self && data.get() == capturedData) {
setData(std::make_unique<GeoJSONTileData>(std::move(features)));
}
});
data->getTile(
id.canonical,
[this, self = weakFactory.makeWeakPtr(), capturedData = data.get()](TileFeatures features) {
// If the data has changed, a new request is being processed, ignore this one
if (auto guard = self.lock(); self && data.get() == capturedData) {
setData(std::make_unique<GeoJSONTileData>(std::move(features)));
}
},
runSynchronously);
}
void GeoJSONTile::querySourceFeatures(std::vector<Feature>& result, const SourceQueryOptions& options) {

View File

@@ -19,7 +19,7 @@ public:
std::shared_ptr<style::GeoJSONData>,
TileObserver* observer = nullptr);
void updateData(std::shared_ptr<style::GeoJSONData> data, bool needsRelayout = false);
void updateData(std::shared_ptr<style::GeoJSONData> data, bool needsRelayout, bool runSynchronously);
void querySourceFeatures(std::vector<Feature>& result, const SourceQueryOptions&) override;

View File

@@ -193,8 +193,9 @@ GeometryTile::GeometryTile(const OverscaledTileID& id_,
ImageRequestor(parameters.imageManager),
threadPool(parameters.threadPool),
mailbox(std::make_shared<Mailbox>(*Scheduler::GetCurrent())),
worker(parameters.threadPool, // Scheduler reference for the Actor retainer
ActorRef<GeometryTile>(*this, mailbox),
worker(parameters.isUpdateSynchronous,
parameters.threadPool, // Scheduler reference for the Actor retainer
OptionalActorRef<GeometryTile>(parameters.isUpdateSynchronous, *this, mailbox),
parameters.threadPool,
id_,
sourceID,

View File

@@ -1,6 +1,6 @@
#pragma once
#include <mbgl/actor/actor.hpp>
#include <mbgl/actor/optional_actor.hpp>
#include <mbgl/geometry/feature_index.hpp>
#include <mbgl/gfx/dynamic_texture_atlas.hpp>
#include <mbgl/renderer/image_manager.hpp>
@@ -114,7 +114,7 @@ private:
TaggedScheduler threadPool;
const std::shared_ptr<Mailbox> mailbox;
Actor<GeometryTileWorker> worker;
OptionalActor<GeometryTileWorker> worker;
const std::shared_ptr<FileSource> fileSource;
const std::shared_ptr<GlyphManager> glyphManager;

View File

@@ -29,8 +29,8 @@ namespace mbgl {
using namespace style;
GeometryTileWorker::GeometryTileWorker(ActorRef<GeometryTileWorker> self_,
ActorRef<GeometryTile> parent_,
GeometryTileWorker::GeometryTileWorker(OptionalActorRef<GeometryTileWorker> self_,
OptionalActorRef<GeometryTile> parent_,
const TaggedScheduler& scheduler_,
OverscaledTileID id_,
std::string sourceID_,

View File

@@ -5,7 +5,7 @@
#include <mbgl/style/image_impl.hpp>
#include <mbgl/text/glyph.hpp>
#include <mbgl/text/glyph_manager.hpp>
#include <mbgl/actor/actor_ref.hpp>
#include <mbgl/actor/optional_actor_ref.hpp>
#include <mbgl/util/immutable.hpp>
#include <mbgl/style/layer_properties.hpp>
#include <mbgl/geometry/feature_index.hpp>
@@ -34,8 +34,8 @@ using DynamicTextureAtlasPtr = std::shared_ptr<gfx::DynamicTextureAtlas>;
class GeometryTileWorker {
public:
GeometryTileWorker(ActorRef<GeometryTileWorker> self,
ActorRef<GeometryTile> parent,
GeometryTileWorker(OptionalActorRef<GeometryTileWorker> self,
OptionalActorRef<GeometryTile> parent,
const TaggedScheduler& scheduler_,
OverscaledTileID,
std::string,
@@ -79,8 +79,8 @@ private:
void checkPatternLayout(std::unique_ptr<Layout> layout);
ActorRef<GeometryTileWorker> self;
ActorRef<GeometryTile> parent;
OptionalActorRef<GeometryTileWorker> self;
OptionalActorRef<GeometryTile> parent;
TaggedScheduler scheduler;
const OverscaledTileID id;

View File

@@ -58,7 +58,7 @@ public:
FakeGeoJSONData(TileFeatures features_)
: features(std::move(features_)) {}
void getTile(const CanonicalTileID&, const std::function<void(TileFeatures)>& fn) final {
void getTile(const CanonicalTileID&, const std::function<void(TileFeatures)>& fn, bool) final {
assert(fn);
fn(features);
}
@@ -75,6 +75,25 @@ private:
} // namespace
TEST(GeoJSONTile, SynchronousUpdate) {
GeoJSONTileTest test;
CircleLayer layer("circle", "source");
mapbox::feature::feature_collection<int16_t> features;
features.push_back(mapbox::feature::feature<int16_t>{mapbox::geometry::point<int16_t>(0, 0)});
auto data = std::make_shared<FakeGeoJSONData>(std::move(features));
TileParameters tileParameters = test.tileParameters;
tileParameters.isUpdateSynchronous = true;
GeoJSONTile tile(OverscaledTileID(0, 0, 0), "source", tileParameters, data);
Immutable<LayerProperties> layerProperties = makeMutable<CircleLayerProperties>(
staticImmutableCast<CircleLayer::Impl>(layer.baseImpl));
std::vector<Immutable<LayerProperties>> layers{layerProperties};
tile.setLayers(layers);
ASSERT_TRUE(tile.isComplete());
ASSERT_TRUE(tile.isRenderable());
}
TEST(GeoJSONTile, Issue7648) {
GeoJSONTileTest test;
@@ -101,7 +120,7 @@ TEST(GeoJSONTile, Issue7648) {
test.loop.runOnce();
}
tile.updateData(data);
tile.updateData(data, false, test.tileParameters.isUpdateSynchronous);
while (!tile.isComplete()) {
test.loop.runOnce();
}