[android] map snapshotter - wrap snapshot in native peer for access to the pointFor functionality

This commit is contained in:
Ivo van Dongen
2017-10-09 16:54:32 +03:00
committed by Ivo van Dongen
parent 87b735aaf7
commit dde1a92291
14 changed files with 293 additions and 28 deletions

View File

@@ -50,6 +50,7 @@
#include "style/sources/sources.hpp"
#include "style/light.hpp"
#include "snapshotter/map_snapshotter.hpp"
#include "snapshotter/map_snapshot.hpp"
namespace mbgl {
namespace android {
@@ -185,6 +186,7 @@ void registerNatives(JavaVM *vm) {
// Snapshotter
MapSnapshotter::registerNative(env);
MapSnapshot::registerNative(env);
}
} // namespace android

View File

@@ -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

View File

@@ -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

View File

@@ -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);
}
});