#include "map_snapshotter.hpp" #include #include #include #include #include #include #include "../attach_env.hpp" #include "../bitmap.hpp" namespace mbgl { namespace android { MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, jni::Object _obj, jni::Object jFileSource, jni::jfloat _pixelRatio, jni::jint width, jni::jint height, jni::String styleURL, jni::Object region, jni::Object position, jni::String _programCacheDir) : javaPeer(SeizeGenericWeakRef(_env, jni::Object(jni::NewWeakGlobalRef(_env, _obj.Get()).release()))) , pixelRatio(_pixelRatio) , threadPool(sharedThreadPool()) { // Get a reference to the JavaVM for callbacks if (_env.GetJavaVM(&vm) < 0) { _env.ExceptionDescribe(); return; } auto& fileSource = mbgl::android::FileSource::getDefaultFileSource(_env, jFileSource); auto size = mbgl::Size { static_cast(width), static_cast(height) }; auto cameraOptions = position ? CameraPosition::getCameraOptions(_env, position) : CameraOptions(); optional bounds; if (region) { bounds = LatLngBounds::getLatLngBounds(_env, region); } // Create the core snapshotter snapshotter = std::make_unique(fileSource, *threadPool, jni::Make(_env, styleURL), size, pixelRatio, cameraOptions, bounds, jni::Make(_env, _programCacheDir)); } MapSnapshotter::~MapSnapshotter() = default; void MapSnapshotter::start(JNIEnv&) { MBGL_VERIFY_THREAD(tid); snapshotCallback = std::make_unique>(*Scheduler::GetCurrent(), [this](std::exception_ptr err, PremultipliedImage image) { MBGL_VERIFY_THREAD(tid); android::UniqueEnv _env = android::AttachEnv(); if (err) { // error handler callback static auto onSnapshotFailed = javaClass.GetMethod(*_env, "onSnapshotFailed"); javaPeer->Call(*_env, onSnapshotFailed, jni::Make(*_env, util::toString(err))); } else { // Create the bitmap auto bitmap = Bitmap::CreateBitmap(*_env, std::move(image)); // invoke callback static auto onSnapshotReady = javaClass.GetMethod)>(*_env, "onSnapshotReady"); javaPeer->Call(*_env, onSnapshotReady, bitmap); } }); snapshotter->snapshot(snapshotCallback->self()); } void MapSnapshotter::cancel(JNIEnv&) { MBGL_VERIFY_THREAD(tid); snapshotCallback.reset(); snapshotter.reset(); } // Static methods // jni::Class MapSnapshotter::javaClass; void MapSnapshotter::registerNative(jni::JNIEnv& env) { // Lookup the class MapSnapshotter::javaClass = *jni::Class::Find(env).NewGlobalRef(env).release(); #define METHOD(MethodPtr, name) jni::MakeNativePeerMethod(name) // Register the peer jni::RegisterNativePeer(env, MapSnapshotter::javaClass, "nativePtr", std::make_unique, jni::Object, jni::jfloat, jni::jint, jni::jint, jni::String, jni::Object, jni::Object, jni::String>, "nativeInitialize", "finalize", METHOD(&MapSnapshotter::start, "nativeStart"), METHOD(&MapSnapshotter::cancel, "nativeCancel") ); } } // namespace android } // namespace mbgl