diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2017-10-09 16:54:32 +0300 |
---|---|---|
committer | Ivo van Dongen <ivovandongen@users.noreply.github.com> | 2017-10-31 19:52:08 +0200 |
commit | dde1a922919770fa009c90ff0525b69488e30bb1 (patch) | |
tree | b1510eabd6a7ea99e27a462a38f463510510f479 /platform/android/src/snapshotter | |
parent | 87b735aaf71557b157a092a8025e6126ed2d8611 (diff) | |
download | qtlocation-mapboxgl-dde1a922919770fa009c90ff0525b69488e30bb1.tar.gz |
[android] map snapshotter - wrap snapshot in native peer for access to the pointFor functionality
Diffstat (limited to 'platform/android/src/snapshotter')
-rw-r--r-- | platform/android/src/snapshotter/map_snapshot.cpp | 57 | ||||
-rw-r--r-- | platform/android/src/snapshotter/map_snapshot.hpp | 41 | ||||
-rw-r--r-- | platform/android/src/snapshotter/map_snapshotter.cpp | 12 |
3 files changed, 104 insertions, 6 deletions
diff --git a/platform/android/src/snapshotter/map_snapshot.cpp b/platform/android/src/snapshotter/map_snapshot.cpp new file mode 100644 index 0000000000..09e83bbb8a --- /dev/null +++ b/platform/android/src/snapshotter/map_snapshot.cpp @@ -0,0 +1,57 @@ +#include "map_snapshot.hpp" + +#include "../bitmap.hpp" + +#include <memory> + +namespace mbgl { +namespace android { + +MapSnapshot::MapSnapshot(float pixelRatio_, MapSnapshot::PointForFn pointForFn_) + : pixelRatio(pixelRatio_) + , pointForFn(std::move(pointForFn_)) { +} + +MapSnapshot::~MapSnapshot() = default; + +jni::Object<PointF> MapSnapshot::pixelForLatLng(jni::JNIEnv& env, jni::Object<LatLng> jLatLng) { + ScreenCoordinate point = pointForFn(LatLng::getLatLng(env, jLatLng)); + return PointF::New(env, point.x * pixelRatio, point.y * pixelRatio); +} + + +// Static methods // + +jni::Object<MapSnapshot> MapSnapshot::New(JNIEnv& env, + PremultipliedImage&& image, + float pixelRatio, + mbgl::MapSnapshotter::PointForFn pointForFn) { + // Create the bitmap + auto bitmap = Bitmap::CreateBitmap(env, std::move(image)); + + // Create the Mapsnapshot peers + static auto constructor = javaClass.GetConstructor<jni::jlong, jni::Object<Bitmap>>(env); + auto nativePeer = std::make_unique<MapSnapshot>(pixelRatio, pointForFn); + return javaClass.New(env, constructor, reinterpret_cast<jlong>(nativePeer.release()), bitmap); +} + +jni::Class<MapSnapshot> MapSnapshot::javaClass; + +void MapSnapshot::registerNative(jni::JNIEnv& env) { + // Lookup the class + MapSnapshot::javaClass = *jni::Class<MapSnapshot>::Find(env).NewGlobalRef(env).release(); + +#define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name) + + // Register the peer + jni::RegisterNativePeer<MapSnapshot>(env, MapSnapshot::javaClass, + "nativePtr", + std::make_unique<MapSnapshot, JNIEnv&>, + "initialize", + "finalize", + METHOD(&MapSnapshot::pixelForLatLng, "pixelForLatLng") + ); +} + +} // namespace android +} // namespace mbgl diff --git a/platform/android/src/snapshotter/map_snapshot.hpp b/platform/android/src/snapshotter/map_snapshot.hpp new file mode 100644 index 0000000000..6d60d49728 --- /dev/null +++ b/platform/android/src/snapshotter/map_snapshot.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include <mbgl/map/map_snapshotter.hpp> + +#include <jni/jni.hpp> + +#include "../geometry/lat_lng.hpp" +#include "../graphics/pointf.hpp" + +namespace mbgl { +namespace android { + +class MapSnapshot { +public: + + using PointForFn = mbgl::MapSnapshotter::PointForFn; + + static constexpr auto Name() { return "com/mapbox/mapboxsdk/snapshotter/MapSnapshot"; }; + + static void registerNative(jni::JNIEnv&); + + static jni::Object<MapSnapshot> New(JNIEnv& env, + PremultipliedImage&& image, + float pixelRatio, + PointForFn pointForFn); + + MapSnapshot(jni::JNIEnv&) {}; + MapSnapshot(float pixelRatio, PointForFn); + ~MapSnapshot(); + + jni::Object<PointF> pixelForLatLng(jni::JNIEnv&, jni::Object<LatLng>); + +private: + static jni::Class<MapSnapshot> javaClass; + + float pixelRatio; + mbgl::MapSnapshotter::PointForFn pointForFn; +}; + +} // namespace android +} // namespace mbgl
\ No newline at end of file diff --git a/platform/android/src/snapshotter/map_snapshotter.cpp b/platform/android/src/snapshotter/map_snapshotter.cpp index 74e43c70a1..a13e91ccd3 100644 --- a/platform/android/src/snapshotter/map_snapshotter.cpp +++ b/platform/android/src/snapshotter/map_snapshotter.cpp @@ -8,7 +8,7 @@ #include <mbgl/actor/scheduler.hpp> #include "../attach_env.hpp" -#include "../bitmap.hpp" +#include "map_snapshot.hpp" namespace mbgl { namespace android { @@ -58,7 +58,7 @@ MapSnapshotter::~MapSnapshotter() = default; void MapSnapshotter::start(JNIEnv&) { MBGL_VERIFY_THREAD(tid); - snapshotCallback = std::make_unique<Actor<mbgl::MapSnapshotter::Callback>>(*Scheduler::GetCurrent(), [this](std::exception_ptr err, PremultipliedImage image) { + snapshotCallback = std::make_unique<Actor<mbgl::MapSnapshotter::Callback>>(*Scheduler::GetCurrent(), [this](std::exception_ptr err, PremultipliedImage image, mbgl::MapSnapshotter::PointForFn pointForFn) { MBGL_VERIFY_THREAD(tid); android::UniqueEnv _env = android::AttachEnv(); @@ -67,12 +67,12 @@ void MapSnapshotter::start(JNIEnv&) { static auto onSnapshotFailed = javaClass.GetMethod<void (jni::String)>(*_env, "onSnapshotFailed"); javaPeer->Call(*_env, onSnapshotFailed, jni::Make<jni::String>(*_env, util::toString(err))); } else { - // Create the bitmap - auto bitmap = Bitmap::CreateBitmap(*_env, std::move(image)); + // Create the wrapper + auto mapSnapshot = android::MapSnapshot::New(*_env, std::move(image), pixelRatio, pointForFn); // invoke callback - static auto onSnapshotReady = javaClass.GetMethod<void (jni::Object<Bitmap>)>(*_env, "onSnapshotReady"); - javaPeer->Call(*_env, onSnapshotReady, bitmap); + static auto onSnapshotReady = javaClass.GetMethod<void (jni::Object<MapSnapshot>)>(*_env, "onSnapshotReady"); + javaPeer->Call(*_env, onSnapshotReady, mapSnapshot); } }); |