summaryrefslogtreecommitdiff
path: root/platform/android/src/snapshotter/map_snapshotter.cpp
blob: d64218d11a8dea65c0b7619cb08f3cb0eb2b9416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "map_snapshotter.hpp"

#include <mbgl/renderer/renderer.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/util/shared_thread_pool.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/actor/scheduler.hpp>

#include "../attach_env.hpp"
#include "../bitmap.hpp"

namespace mbgl {
namespace android {

MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env,
                               jni::Object<MapSnapshotter> _obj,
                               jni::Object<FileSource> jFileSource,
                               jni::jfloat _pixelRatio,
                               jni::jint width,
                               jni::jint height,
                               jni::String styleURL,
                               jni::Object<LatLngBounds> region,
                               jni::Object<CameraPosition> position,
                               jni::String _programCacheDir)
        : javaPeer(SeizeGenericWeakRef(_env, jni::Object<MapSnapshotter>(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<uint32_t>(width), static_cast<uint32_t>(height) };
    auto cameraOptions = position ? CameraPosition::getCameraOptions(_env, position) : CameraOptions();
    optional<mbgl::LatLngBounds> bounds;
    if (region) {
        bounds = LatLngBounds::getLatLngBounds(_env, region);
    }

    // Create the core snapshotter
    snapshotter = std::make_unique<mbgl::MapSnapshotter>(fileSource,
                                                         *threadPool,
                                                         jni::Make<std::string>(_env, styleURL),
                                                         size,
                                                         pixelRatio,
                                                         cameraOptions,
                                                         bounds,
                                                         jni::Make<std::string>(_env, _programCacheDir));

}

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) {
        MBGL_VERIFY_THREAD(tid);
        android::UniqueEnv _env = android::AttachEnv();

        if (err) {
            // error handler callback
            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));

            // invoke callback
            static auto onSnapshotReady = javaClass.GetMethod<void (jni::Object<Bitmap>)>(*_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> MapSnapshotter::javaClass;

void MapSnapshotter::registerNative(jni::JNIEnv& env) {
    // Lookup the class
    MapSnapshotter::javaClass = *jni::Class<MapSnapshotter>::Find(env).NewGlobalRef(env).release();

#define METHOD(MethodPtr, name) jni::MakeNativePeerMethod<decltype(MethodPtr), (MethodPtr)>(name)

    // Register the peer
    jni::RegisterNativePeer<MapSnapshotter>(env, MapSnapshotter::javaClass, "nativePtr",
                                            std::make_unique<MapSnapshotter, JNIEnv&, jni::Object<MapSnapshotter>, jni::Object<FileSource>, jni::jfloat, jni::jint, jni::jint, jni::String, jni::Object<LatLngBounds>, jni::Object<CameraPosition>, jni::String>,
                                           "nativeInitialize",
                                           "finalize",
                                            METHOD(&MapSnapshotter::start, "nativeStart"),
                                            METHOD(&MapSnapshotter::cancel, "nativeCancel")
    );
}

} // namespace android
} // namespace mbgl