summaryrefslogtreecommitdiff
path: root/platform/android/src/map_renderer_runnable.cpp
blob: 4dc6611c40d0862cbb65dcbf8000886df0caa0f6 (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
#include "map_renderer_runnable.hpp"

#include <mbgl/util/logging.hpp>

namespace mbgl {
namespace android {

MapRendererRunnable::MapRendererRunnable(jni::JNIEnv& env, std::weak_ptr<Mailbox> mailbox_)
    : mailbox(std::move(mailbox_)) {

    // Create the Java peer and hold on to a global reference
    // Not using a weak reference here as this might oerflow
    // the weak reference table on some devices
    jni::UniqueLocalFrame frame = jni::PushLocalFrame(env, 5);
    static auto constructor = javaClass.GetConstructor<jlong>(env);
    auto instance = javaClass.New(env, constructor, reinterpret_cast<jlong>(this));
    javaPeer = instance.NewGlobalRef(env);
}

MapRendererRunnable::~MapRendererRunnable() = default;

void MapRendererRunnable::run(jni::JNIEnv&) {
    Mailbox::maybeReceive(mailbox);
}

jni::UniqueObject<MapRendererRunnable> MapRendererRunnable::peer() {
    return std::move(javaPeer);
}

// Static methods //

jni::Class<MapRendererRunnable> MapRendererRunnable::javaClass;

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

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

    jni::RegisterNativePeer<MapRendererRunnable>(
            env,
            MapRendererRunnable::javaClass,
            "nativePtr",
            std::make_unique<MapRendererRunnable, JNIEnv&>,
            "nativeInitialize",
            "finalize",
            METHOD(&MapRendererRunnable::run, "run"));
}

} // namespace android
} // namespace mbgl