summaryrefslogtreecommitdiff
path: root/platform/android/src/snapshotter/map_snapshotter.cpp
blob: 8e0faf5cde2b315db07e3d7708e8c9497d3608e8 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#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 "../style/android_conversion.hpp"
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/filter.hpp>

#include "../conversion/conversion.hpp"
#include "../conversion/collection.hpp"
#include "../style/conversion/filter.hpp"
#include "../geojson/conversion/feature.hpp"

#include "../attach_env.hpp"
#include "map_snapshot.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::String styleJSON,
                               jni::Object<LatLngBounds> region,
                               jni::Object<CameraPosition> position,
                               jni::jboolean _showLogo,
                               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;
    }

    jFileSource = FileSource::getNativePeer(_env, _jFileSource);
    auto& fileSource = mbgl::android::FileSource::getDefaultFileSource(_env, _jFileSource);
    auto size = mbgl::Size { static_cast<uint32_t>(width), static_cast<uint32_t>(height) };

    optional<mbgl::CameraOptions> cameraOptions;
    if (position) {
        cameraOptions = CameraPosition::getCameraOptions(_env, position);
    }

    optional<mbgl::LatLngBounds> bounds;
    if (region) {
        bounds = LatLngBounds::getLatLngBounds(_env, region);
    }

    std::pair<bool, std::string> style;
    if (styleJSON) {
        style = std::make_pair(true, jni::Make<std::string>(_env, styleJSON));
    } else {
        style = std::make_pair(false, jni::Make<std::string>(_env, styleURL));
    }
    
    showLogo = _showLogo;
    // Create the core snapshotter
    snapshotter = std::make_unique<mbgl::MapSnapshotter>(fileSource,
                                                         *threadPool,
                                                         style,
                                                         size,
                                                         pixelRatio,
                                                         cameraOptions,
                                                         bounds,
                                                         jni::Make<std::string>(_env, _programCacheDir));

}

MapSnapshotter::~MapSnapshotter() = default;

void MapSnapshotter::start(JNIEnv& env) {
    MBGL_VERIFY_THREAD(tid);
    activateFilesource(env);

    snapshotCallback = std::make_unique<Actor<mbgl::MapSnapshotter::SnapshotCallback>>(
            *Scheduler::GetCurrent(),
            [this](std::exception_ptr err, PremultipliedImage image, std::vector<std::string> attributions, mbgl::MapSnapshotter::PointForFn pointForFn) {
        MBGL_VERIFY_THREAD(tid);
        android::UniqueEnv _env = android::AttachEnv();

        if (err) {
            // error handler callback
            static auto onSnapshotFailed = javaClass.GetMethod<void (jni::String)>(*_env, "onSnapshotFailed");
            auto message = jni::Make<jni::String>(*_env, util::toString(err));
            javaPeer->Call(*_env, onSnapshotFailed, message);
            jni::DeleteLocalRef(*_env, message);
        } else {
            // Create the wrapper
            auto mapSnapshot = android::MapSnapshot::New(*_env, std::move(image), pixelRatio, attributions, showLogo, pointForFn);

            // invoke callback
            static auto onSnapshotReady = javaClass.GetMethod<void (jni::Object<MapSnapshot>)>(*_env, "onSnapshotReady");
            javaPeer->Call(*_env, onSnapshotReady, mapSnapshot);
        }

        deactivateFilesource(*_env);
    });

    snapshotter->snapshot(snapshotCallback->self());
}

void MapSnapshotter::queryFeatures(JNIEnv& env, jni::jfloat left, jni::jfloat top,
                                   jni::jfloat right, jni::jfloat bottom, jni::Array<jni::String> layerIds,
                                   jni::Array<jni::Object<>> jfilter) {
    MBGL_VERIFY_THREAD(tid);
    activateFilesource(env);

    using namespace mbgl::android::conversion;
    using namespace mbgl::android::geojson;

    queryFeaturesCallback = std::make_unique<Actor<mbgl::MapSnapshotter::QueryFeaturesCallback>>(
        *Scheduler::GetCurrent(),
        [this](std::exception_ptr err, const std::vector<mbgl::Feature> features) {
            MBGL_VERIFY_THREAD(tid);
            android::UniqueEnv _env = android::AttachEnv();

            using namespace mbgl::android::conversion;
            using namespace mbgl::android::geojson;

            // todo add error handling
            if (!err) {
                // invoke callback
                static auto onQueryFeaturesReady = javaClass.GetMethod<void (jni::Array<jni::Object<geojson::Feature>>)>(*_env, "onQueryFeaturesReady");
                jni::Array<jni::Object<geojson::Feature>> featureArray = 
                        *convert<jni::Array<jni::Object<geojson::Feature>>, std::vector<mbgl::Feature>>(*_env, features);

                javaPeer->Call(*_env,
                               onQueryFeaturesReady,
                               featureArray);
            }
        }
    );

    mbgl::optional<std::vector<std::string>> layers;
    if (layerIds != nullptr && layerIds.Length(env) > 0) {
        layers = toVector(env, layerIds);
    }
    mapbox::geometry::box<double> box = {
            mapbox::geometry::point<double>{ left, top},
            mapbox::geometry::point<double>{ right, bottom }
    };

    snapshotter->queryFeatures(queryFeaturesCallback->self(), box, { layers, toFilter(env, jfilter) });
}

void MapSnapshotter::cancel(JNIEnv& env) {
    MBGL_VERIFY_THREAD(tid);
    snapshotCallback.reset();
    deactivateFilesource(env);
}

void MapSnapshotter::setStyleUrl(JNIEnv& env, jni::String styleURL) {
    snapshotter->setStyleURL(jni::Make<std::string>(env, styleURL));
}

void MapSnapshotter::setStyleJson(JNIEnv& env, jni::String styleJSON) {
    snapshotter->setStyleJSON(jni::Make<std::string>(env, styleJSON));
}

void MapSnapshotter::setSize(JNIEnv&, jni::jint width, jni::jint height) {
    auto size = mbgl::Size { static_cast<uint32_t>(width), static_cast<uint32_t>(height) };
    snapshotter->setSize(size);
}

void MapSnapshotter::setCameraPosition(JNIEnv& env, jni::Object<CameraPosition> position) {
    auto options = CameraPosition::getCameraOptions(env, position);
    snapshotter->setCameraOptions(options);
}

void MapSnapshotter::setRegion(JNIEnv& env, jni::Object<LatLngBounds> region) {
    snapshotter->setRegion(LatLngBounds::getLatLngBounds(env, region));
}


// Private methods //

void MapSnapshotter::activateFilesource(JNIEnv& env) {
    if (!activatedFilesource) {
        activatedFilesource = true;
        jFileSource->resume(env);
    }
}

void MapSnapshotter::deactivateFilesource(JNIEnv& env) {
    if (activatedFilesource) {
        activatedFilesource = false;
        jFileSource->pause(env);
    }
}

// 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::String, jni::Object<LatLngBounds>, jni::Object<CameraPosition>, jni::jboolean, jni::String>,
                                           "nativeInitialize",
                                           "finalize",
                                            METHOD(&MapSnapshotter::setStyleUrl, "setStyleUrl"),
                                            METHOD(&MapSnapshotter::setStyleJson, "setStyleJson"),
                                            METHOD(&MapSnapshotter::setSize, "setSize"),
                                            METHOD(&MapSnapshotter::setCameraPosition, "setCameraPosition"),
                                            METHOD(&MapSnapshotter::setRegion, "setRegion"),
                                            METHOD(&MapSnapshotter::start, "nativeStart"),
                                            METHOD(&MapSnapshotter::queryFeatures, "nativeQueryFeatures"),
                                            METHOD(&MapSnapshotter::cancel, "nativeCancel")
    );
}

} // namespace android
} // namespace mbgl