From 2b67ce73b6d313b518f55fc612fd647d9c540a4e Mon Sep 17 00:00:00 2001 From: Alexander Shalamov Date: Wed, 25 Mar 2020 10:38:03 +0200 Subject: [PATCH] [android] Expose getLayer, getSource and Observer interface for snapshotter So that users can modify properties of an existing layer / source objects --- .../src/snapshotter/map_snapshotter.cpp | 27 ++++++++++++ .../src/snapshotter/map_snapshotter.hpp | 2 + .../style/sources/custom_geometry_source.cpp | 5 +-- .../style/sources/custom_geometry_source.hpp | 2 +- .../src/style/sources/geojson_source.cpp | 2 +- .../src/style/sources/geojson_source.hpp | 2 +- .../src/style/sources/image_source.cpp | 7 +-- .../src/style/sources/image_source.hpp | 2 +- .../src/style/sources/raster_dem_source.cpp | 7 ++- .../src/style/sources/raster_dem_source.hpp | 2 +- .../src/style/sources/raster_source.cpp | 7 +-- .../src/style/sources/raster_source.hpp | 2 +- platform/android/src/style/sources/source.cpp | 43 +++++++++++-------- platform/android/src/style/sources/source.hpp | 3 +- .../src/style/sources/unknown_source.cpp | 15 +++---- .../src/style/sources/unknown_source.hpp | 2 +- .../src/style/sources/vector_source.cpp | 7 +-- .../src/style/sources/vector_source.hpp | 2 +- 18 files changed, 82 insertions(+), 57 deletions(-) diff --git a/platform/android/src/snapshotter/map_snapshotter.cpp b/platform/android/src/snapshotter/map_snapshotter.cpp index 5bccbd1ba8b..a02c9e5c937 100644 --- a/platform/android/src/snapshotter/map_snapshotter.cpp +++ b/platform/android/src/snapshotter/map_snapshotter.cpp @@ -7,6 +7,7 @@ #include #include "../attach_env.hpp" +#include "../style/layers/layer_manager.hpp" #include "map_snapshot.hpp" namespace mbgl { @@ -282,6 +283,30 @@ void MapSnapshotter::addImages(JNIEnv& env, const jni::Array> MapSnapshotter::getLayer(JNIEnv& env, const jni::String& layerId) { + // Find the layer + mbgl::style::Layer* coreLayer = snapshotter->getStyle().getLayer(jni::Make(env, layerId)); + if (!coreLayer) { + mbgl::Log::Debug(mbgl::Event::JNI, "No layer found"); + return jni::Local>(); + } + + // Create and return the layer's native peer + return LayerManagerAndroid::get()->createJavaLayerPeer(env, *coreLayer); +} + +jni::Local> MapSnapshotter::getSource(JNIEnv& env, const jni::String& sourceId) { + // Find the source + mbgl::style::Source* coreSource = snapshotter->getStyle().getSource(jni::Make(env, sourceId)); + if (!coreSource) { + mbgl::Log::Debug(mbgl::Event::JNI, "No source found"); + return jni::Local>(); + } + + // Create and return the source's native peer + return jni::NewLocal(env, Source::peerForCoreSource(env, *coreSource)); +} + // Static methods // void MapSnapshotter::registerNative(jni::JNIEnv& env) { @@ -314,6 +339,8 @@ void MapSnapshotter::registerNative(jni::JNIEnv& env) { METHOD(&MapSnapshotter::addLayerAbove, "nativeAddLayerAbove"), METHOD(&MapSnapshotter::addSource, "nativeAddSource"), METHOD(&MapSnapshotter::addImages, "nativeAddImages"), + METHOD(&MapSnapshotter::getLayer, "nativeGetLayer"), + METHOD(&MapSnapshotter::getSource, "nativeGetSource"), METHOD(&MapSnapshotter::setStyleJson, "setStyleJson"), METHOD(&MapSnapshotter::setSize, "setSize"), METHOD(&MapSnapshotter::setCameraPosition, "setCameraPosition"), diff --git a/platform/android/src/snapshotter/map_snapshotter.hpp b/platform/android/src/snapshotter/map_snapshotter.hpp index c7a9ce14ad7..4d0186e7f8b 100644 --- a/platform/android/src/snapshotter/map_snapshotter.hpp +++ b/platform/android/src/snapshotter/map_snapshotter.hpp @@ -58,6 +58,8 @@ class MapSnapshotter final : public mbgl::MapSnapshotterObserver { void addLayerAbove(JNIEnv&, jlong, const jni::String&); void addSource(JNIEnv&, const jni::Object&, jlong nativePtr); void addImages(JNIEnv&, const jni::Array>&); + jni::Local> getLayer(JNIEnv&, const jni::String&); + jni::Local> getSource(JNIEnv&, const jni::String&); // MapSnapshotterObserver overrides void onDidFailLoadingStyle(const std::string&) override; diff --git a/platform/android/src/style/sources/custom_geometry_source.cpp b/platform/android/src/style/sources/custom_geometry_source.cpp index dccca4cf460..0d96a099165 100644 --- a/platform/android/src/style/sources/custom_geometry_source.cpp +++ b/platform/android/src/style/sources/custom_geometry_source.cpp @@ -51,9 +51,8 @@ namespace android { CustomGeometrySource::CustomGeometrySource(jni::JNIEnv& env, mbgl::style::Source& coreSource, - AndroidRendererFrontend& frontend) - : Source(env, coreSource, createJavaPeer(env), frontend) { - } + AndroidRendererFrontend* frontend) + : Source(env, coreSource, createJavaPeer(env), frontend) {} CustomGeometrySource::~CustomGeometrySource() { releaseThreads(); diff --git a/platform/android/src/style/sources/custom_geometry_source.hpp b/platform/android/src/style/sources/custom_geometry_source.hpp index 52e4027dd86..6979ecc25d4 100644 --- a/platform/android/src/style/sources/custom_geometry_source.hpp +++ b/platform/android/src/style/sources/custom_geometry_source.hpp @@ -21,7 +21,7 @@ class CustomGeometrySource : public Source { static void registerNative(jni::JNIEnv&); CustomGeometrySource(jni::JNIEnv&, const jni::String&, const jni::Object<>&); - CustomGeometrySource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&); + CustomGeometrySource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend*); ~CustomGeometrySource(); bool removeFromMap(JNIEnv&, const jni::Object&, mbgl::Map&) override; diff --git a/platform/android/src/style/sources/geojson_source.cpp b/platform/android/src/style/sources/geojson_source.cpp index f68c9d8f425..0a287eb6da3 100644 --- a/platform/android/src/style/sources/geojson_source.cpp +++ b/platform/android/src/style/sources/geojson_source.cpp @@ -50,7 +50,7 @@ GeoJSONSource::GeoJSONSource(jni::JNIEnv& env, const jni::String& sourceId, cons converter(std::make_unique>(Scheduler::GetBackground(), source.as()->impl().getOptions())) {} -GeoJSONSource::GeoJSONSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend& frontend) +GeoJSONSource::GeoJSONSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend* frontend) : Source(env, coreSource, createJavaPeer(env), frontend), converter(std::make_unique>(Scheduler::GetBackground(), source.as()->impl().getOptions())) {} diff --git a/platform/android/src/style/sources/geojson_source.hpp b/platform/android/src/style/sources/geojson_source.hpp index 668e944e1cf..c7ef6e54dea 100644 --- a/platform/android/src/style/sources/geojson_source.hpp +++ b/platform/android/src/style/sources/geojson_source.hpp @@ -43,7 +43,7 @@ class GeoJSONSource : public Source { static void registerNative(jni::JNIEnv&); GeoJSONSource(jni::JNIEnv&, const jni::String&, const jni::Object<>&); - GeoJSONSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&); + GeoJSONSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend*); ~GeoJSONSource(); private: diff --git a/platform/android/src/style/sources/image_source.cpp b/platform/android/src/style/sources/image_source.cpp index b42e0e5a51d..448ecd21608 100644 --- a/platform/android/src/style/sources/image_source.cpp +++ b/platform/android/src/style/sources/image_source.cpp @@ -23,11 +23,8 @@ namespace android { ) { } - ImageSource::ImageSource(jni::JNIEnv& env, - mbgl::style::Source& coreSource, - AndroidRendererFrontend& frontend) - : Source(env, coreSource, createJavaPeer(env), frontend) { - } + ImageSource::ImageSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend* frontend) + : Source(env, coreSource, createJavaPeer(env), frontend) {} ImageSource::~ImageSource() = default; diff --git a/platform/android/src/style/sources/image_source.hpp b/platform/android/src/style/sources/image_source.hpp index 6c359bf0ee1..da88feac742 100644 --- a/platform/android/src/style/sources/image_source.hpp +++ b/platform/android/src/style/sources/image_source.hpp @@ -19,7 +19,7 @@ class ImageSource : public Source { ImageSource(jni::JNIEnv&, const jni::String&, const jni::Object&); - ImageSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&); + ImageSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend*); ~ImageSource(); diff --git a/platform/android/src/style/sources/raster_dem_source.cpp b/platform/android/src/style/sources/raster_dem_source.cpp index f60526a9b75..a33e002598c 100644 --- a/platform/android/src/style/sources/raster_dem_source.cpp +++ b/platform/android/src/style/sources/raster_dem_source.cpp @@ -24,10 +24,9 @@ namespace android { } RasterDEMSource::RasterDEMSource(jni::JNIEnv& env, - mbgl::style::Source& coreSource, - AndroidRendererFrontend& frontend) - : Source(env, coreSource, createJavaPeer(env), frontend) { - } + mbgl::style::Source& coreSource, + AndroidRendererFrontend* frontend) + : Source(env, coreSource, createJavaPeer(env), frontend) {} RasterDEMSource::~RasterDEMSource() = default; diff --git a/platform/android/src/style/sources/raster_dem_source.hpp b/platform/android/src/style/sources/raster_dem_source.hpp index 0b31c4ecd3b..581e8f8bc67 100644 --- a/platform/android/src/style/sources/raster_dem_source.hpp +++ b/platform/android/src/style/sources/raster_dem_source.hpp @@ -15,7 +15,7 @@ class RasterDEMSource : public Source { static void registerNative(jni::JNIEnv&); RasterDEMSource(jni::JNIEnv&, const jni::String&, const jni::Object<>&, jni::jint); - RasterDEMSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&); + RasterDEMSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend*); ~RasterDEMSource(); jni::Local getURL(jni::JNIEnv&); diff --git a/platform/android/src/style/sources/raster_source.cpp b/platform/android/src/style/sources/raster_source.cpp index 535f899c99b..67603e74cdb 100644 --- a/platform/android/src/style/sources/raster_source.cpp +++ b/platform/android/src/style/sources/raster_source.cpp @@ -22,11 +22,8 @@ namespace android { ) { } - RasterSource::RasterSource(jni::JNIEnv& env, - mbgl::style::Source& coreSource, - AndroidRendererFrontend& frontend) - : Source(env, coreSource, createJavaPeer(env), frontend) { - } + RasterSource::RasterSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend* frontend) + : Source(env, coreSource, createJavaPeer(env), frontend) {} RasterSource::~RasterSource() = default; diff --git a/platform/android/src/style/sources/raster_source.hpp b/platform/android/src/style/sources/raster_source.hpp index 08648f79aad..e1bc5ca7aeb 100644 --- a/platform/android/src/style/sources/raster_source.hpp +++ b/platform/android/src/style/sources/raster_source.hpp @@ -15,7 +15,7 @@ class RasterSource : public Source { static void registerNative(jni::JNIEnv&); RasterSource(jni::JNIEnv&, const jni::String&, const jni::Object<>&, jni::jint); - RasterSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&); + RasterSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend*); ~RasterSource(); jni::Local getURL(jni::JNIEnv&); diff --git a/platform/android/src/style/sources/source.cpp b/platform/android/src/style/sources/source.cpp index f4040a57181..5c43c2ae7ca 100644 --- a/platform/android/src/style/sources/source.cpp +++ b/platform/android/src/style/sources/source.cpp @@ -34,33 +34,42 @@ namespace mbgl { namespace android { - static std::unique_ptr createSourcePeer(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend& frontend) { - if (coreSource.is()) { - return std::make_unique(env, *coreSource.as(), frontend); - } else if (coreSource.is()) { - return std::make_unique(env, *coreSource.as(), frontend); - } else if (coreSource.is()) { - return std::make_unique(env, *coreSource.as(), frontend); - } else if (coreSource.is()) { - return std::make_unique(env, *coreSource.as(), frontend); - } else { - return std::make_unique(env, coreSource, frontend); - } +static std::unique_ptr createSourcePeer(jni::JNIEnv& env, + mbgl::style::Source& coreSource, + AndroidRendererFrontend* frontend) { + if (coreSource.is()) { + return std::make_unique(env, *coreSource.as(), frontend); + } else if (coreSource.is()) { + return std::make_unique(env, *coreSource.as(), frontend); + } else if (coreSource.is()) { + return std::make_unique(env, *coreSource.as(), frontend); + } else if (coreSource.is()) { + return std::make_unique(env, *coreSource.as(), frontend); + } else { + return std::make_unique(env, coreSource, frontend); } +} const jni::Object& Source::peerForCoreSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend& frontend) { if (!coreSource.peer.has_value()) { - coreSource.peer = createSourcePeer(env, coreSource, frontend); + coreSource.peer = createSourcePeer(env, coreSource, &frontend); } return coreSource.peer.get>()->javaPeer; } - Source::Source(jni::JNIEnv& env, mbgl::style::Source& coreSource, const jni::Object& obj, AndroidRendererFrontend& frontend) - : source(coreSource) - , javaPeer(jni::NewGlobal(env, obj)) - , rendererFrontend(&frontend) { + const jni::Object& Source::peerForCoreSource(jni::JNIEnv& env, mbgl::style::Source& coreSource) { + if (!coreSource.peer.has_value()) { + coreSource.peer = createSourcePeer(env, coreSource, nullptr); + } + return coreSource.peer.get>()->javaPeer; } + Source::Source(jni::JNIEnv& env, + mbgl::style::Source& coreSource, + const jni::Object& obj, + AndroidRendererFrontend* frontend) + : source(coreSource), javaPeer(jni::NewGlobal(env, obj)), rendererFrontend(frontend) {} + Source::Source(jni::JNIEnv&, std::unique_ptr coreSource) : ownedSource(std::move(coreSource)) , source(*ownedSource) { diff --git a/platform/android/src/style/sources/source.hpp b/platform/android/src/style/sources/source.hpp index a87972ce792..e0f2b2229f8 100644 --- a/platform/android/src/style/sources/source.hpp +++ b/platform/android/src/style/sources/source.hpp @@ -19,12 +19,13 @@ class Source : private mbgl::util::noncopyable { static void registerNative(jni::JNIEnv&); + static const jni::Object& peerForCoreSource(jni::JNIEnv&, mbgl::style::Source&); static const jni::Object& peerForCoreSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&); /* * Called when a Java object is created for a core source that belongs to a map. */ - Source(jni::JNIEnv&, mbgl::style::Source&, const jni::Object&, AndroidRendererFrontend&); + Source(jni::JNIEnv&, mbgl::style::Source&, const jni::Object&, AndroidRendererFrontend*); /* * Called when a Java object is created for a new core source that does not belong to a map. diff --git a/platform/android/src/style/sources/unknown_source.cpp b/platform/android/src/style/sources/unknown_source.cpp index b5285a5f9f6..1a0e3d4f821 100644 --- a/platform/android/src/style/sources/unknown_source.cpp +++ b/platform/android/src/style/sources/unknown_source.cpp @@ -12,16 +12,13 @@ namespace { namespace mbgl { namespace android { - UnknownSource::UnknownSource(jni::JNIEnv& env, - mbgl::style::Source& coreSource, - AndroidRendererFrontend& frontend) - : Source(env, coreSource, createJavaPeer(env), frontend) { - } +UnknownSource::UnknownSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend* frontend) + : Source(env, coreSource, createJavaPeer(env), frontend) {} - jni::Local> UnknownSource::createJavaPeer(jni::JNIEnv& env) { - static auto& javaClass = jni::Class::Singleton(env); - static auto constructor = javaClass.GetConstructor(env); - return javaClass.New(env, constructor, reinterpret_cast(this)); +jni::Local> UnknownSource::createJavaPeer(jni::JNIEnv& env) { + static auto& javaClass = jni::Class::Singleton(env); + static auto constructor = javaClass.GetConstructor(env); + return javaClass.New(env, constructor, reinterpret_cast(this)); } void UnknownSource::registerNative(jni::JNIEnv& env) { diff --git a/platform/android/src/style/sources/unknown_source.hpp b/platform/android/src/style/sources/unknown_source.hpp index f042e2042c4..ab5a2da82f5 100644 --- a/platform/android/src/style/sources/unknown_source.hpp +++ b/platform/android/src/style/sources/unknown_source.hpp @@ -14,7 +14,7 @@ class UnknownSource : public Source { static void registerNative(jni::JNIEnv&); - UnknownSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&); + UnknownSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend*); ~UnknownSource() = default; diff --git a/platform/android/src/style/sources/vector_source.cpp b/platform/android/src/style/sources/vector_source.cpp index e46fc1a94ec..2be1a78119a 100644 --- a/platform/android/src/style/sources/vector_source.cpp +++ b/platform/android/src/style/sources/vector_source.cpp @@ -30,11 +30,8 @@ namespace android { ) { } - VectorSource::VectorSource(jni::JNIEnv& env, - mbgl::style::Source& coreSource, - AndroidRendererFrontend& frontend) - : Source(env, coreSource, createJavaPeer(env), frontend) { - } + VectorSource::VectorSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend* frontend) + : Source(env, coreSource, createJavaPeer(env), frontend) {} VectorSource::~VectorSource() = default; diff --git a/platform/android/src/style/sources/vector_source.hpp b/platform/android/src/style/sources/vector_source.hpp index 4cb46c6c620..7ae2e8c9e74 100644 --- a/platform/android/src/style/sources/vector_source.hpp +++ b/platform/android/src/style/sources/vector_source.hpp @@ -16,7 +16,7 @@ class VectorSource : public Source { static void registerNative(jni::JNIEnv&); VectorSource(jni::JNIEnv&, const jni::String&, const jni::Object<>&); - VectorSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend&); + VectorSource(jni::JNIEnv&, mbgl::style::Source&, AndroidRendererFrontend*); ~VectorSource(); private: