summaryrefslogtreecommitdiff
path: root/platform/android/src/map_renderer_runnable.cpp
blob: 77c3aa301d66d78f50910012e9b386e7460a4611 (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
#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& javaClass = jni::Class<MapRendererRunnable>::Singleton(env);
    static auto constructor = javaClass.GetConstructor<jlong>(env);
    auto instance = javaClass.New(env, constructor, reinterpret_cast<jlong>(this));
    javaPeer = jni::NewGlobal(env, instance);
}

MapRendererRunnable::~MapRendererRunnable() = default;

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

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

// Static methods //

void MapRendererRunnable::registerNative(jni::JNIEnv& env) {
    // Lookup the class
    static auto& javaClass = jni::Class<MapRendererRunnable>::Singleton(env);

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

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

} // namespace android
} // namespace mbgl