summaryrefslogtreecommitdiff
path: root/platform/android/src/style/sources/source.cpp
blob: 3b89b25d7dd84cae3a65fe6704e1f4179986084c (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "source.hpp"
#include "../android_conversion.hpp"

#include <jni/jni.hpp>

#include <mbgl/style/style.hpp>
#include <mbgl/util/logging.hpp>

// Java -> C++ conversion
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/source.hpp>

// C++ -> Java conversion
#include "../conversion/property_value.hpp"

#include <string>

// Core Sources
#include <mbgl/style/sources/geojson_source.hpp>
#include <mbgl/style/sources/image_source.hpp>
#include <mbgl/style/sources/raster_source.hpp>
#include <mbgl/style/sources/vector_source.hpp>

// Android Source peers
#include "geojson_source.hpp"
#include "image_source.hpp"
#include "raster_source.hpp"
#include "unknown_source.hpp"
#include "vector_source.hpp"
#include "custom_geometry_source.hpp"

namespace mbgl {
namespace android {

    static std::unique_ptr<Source> createSourcePeer(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend& frontend) {
        if (coreSource.is<mbgl::style::VectorSource>()) {
            return std::make_unique<VectorSource>(env, *coreSource.as<mbgl::style::VectorSource>(), frontend);
        } else if (coreSource.is<mbgl::style::RasterSource>()) {
            return std::make_unique<RasterSource>(env, *coreSource.as<mbgl::style::RasterSource>(), frontend);
        } else if (coreSource.is<mbgl::style::GeoJSONSource>()) {
            return std::make_unique<GeoJSONSource>(env, *coreSource.as<mbgl::style::GeoJSONSource>(), frontend);
        } else if (coreSource.is<mbgl::style::ImageSource>()) {
            return std::make_unique<ImageSource>(env, *coreSource.as<mbgl::style::ImageSource>(), frontend);
        } else {
            return std::make_unique<UnknownSource>(env, coreSource, frontend);
        }
    }

    jni::Object<Source> Source::peerForCoreSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend& frontend) {
        if (!coreSource.peer.has_value()) {
            coreSource.peer = createSourcePeer(env, coreSource, frontend);
        }
        return *mbgl::util::any_cast<std::unique_ptr<Source>>(&coreSource.peer)->get()->javaPeer;
    }

    Source::Source(jni::JNIEnv& env, mbgl::style::Source& coreSource, jni::Object<Source> obj, AndroidRendererFrontend& frontend)
        : source(coreSource)
        , javaPeer(obj.NewGlobalRef(env))
        , rendererFrontend(&frontend) {
    }

    Source::Source(jni::JNIEnv&, std::unique_ptr<mbgl::style::Source> coreSource)
        : ownedSource(std::move(coreSource))
        , source(*ownedSource) {
    }

    Source::~Source() {
        // Before being added to a map, the Java peer owns this C++ peer and cleans
        //  up after itself correctly through the jni native peer bindings.
        // After being added to the map, the ownership is flipped and the C++ peer has a strong reference
        //  to it's Java peer, preventing the Java peer from being GC'ed.
        //  In this case, the core source initiates the destruction, which requires releasing the Java peer,
        //  while also resetting it's nativePtr to 0 to prevent the subsequent GC of the Java peer from
        //  re-entering this dtor.
        if (ownedSource.get() == nullptr && javaPeer.get() != nullptr) {
            // Manually clear the java peer
            android::UniqueEnv env = android::AttachEnv();
            static auto nativePtrField = javaClass.GetField<jlong>(*env, "nativePtr");
            javaPeer->Set(*env, nativePtrField, (jlong) 0);
            javaPeer.reset();
        }
    }

    jni::String Source::getId(jni::JNIEnv& env) {
        return jni::Make<jni::String>(env, source.getID());
    }

    jni::String Source::getAttribution(jni::JNIEnv& env) {
        auto attribution = source.getAttribution();
        return attribution ? jni::Make<jni::String>(env, attribution.value()) : jni::Make<jni::String>(env,"");
    }

    void Source::addToMap(JNIEnv& env, jni::Object<Source> obj, mbgl::Map& map, AndroidRendererFrontend& frontend) {
        // Check to see if we own the source first
        if (!ownedSource) {
            throw std::runtime_error("Cannot add source twice");
        }

        // Add source to map and release ownership
        map.getStyle().addSource(std::move(ownedSource));

        // Add peer to core source
        source.peer = std::unique_ptr<Source>(this);

        // Add strong reference to java source
        javaPeer = obj.NewGlobalRef(env);

        rendererFrontend = &frontend;
    }

    void Source::removeFromMap(JNIEnv&, jni::Object<Source>, mbgl::Map& map) {
        // Cannot remove if not attached yet
        if (ownedSource) {
            throw std::runtime_error("Cannot remove detached source");
        }

        // Remove the source from the map and take ownership
        ownedSource = map.getStyle().removeSource(source.getID());

        // The source may not be removed if any layers still reference it
        if (!ownedSource) {
            return;
        }

        // Release the peer relationships. These will be re-established when the source is added to a map
        assert(ownedSource->peer.has_value());
        util::any_cast<std::unique_ptr<Source>>(&(ownedSource->peer))->release();
        ownedSource->peer.reset();

        // Release the strong reference to the java peer
        assert(javaPeer);
        javaPeer.release();

        rendererFrontend = nullptr;
    }

    jni::Class<Source> Source::javaClass;

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

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

        // Register the peer
        jni::RegisterNativePeer<Source>(env, Source::javaClass, "nativePtr",
            METHOD(&Source::getId, "nativeGetId"),
            METHOD(&Source::getAttribution, "nativeGetAttribution")
        );

        // Register subclasses
        GeoJSONSource::registerNative(env);
        ImageSource::registerNative(env);
        RasterSource::registerNative(env);
        UnknownSource::registerNative(env);
        VectorSource::registerNative(env);
        CustomGeometrySource::registerNative(env);
    }

} // namespace android
} // namespace mbgl