summaryrefslogtreecommitdiff
path: root/android
diff options
context:
space:
mode:
authorLeith Bade <leith@mapbox.com>2015-09-24 12:18:59 +1000
committerLeith Bade <leith@mapbox.com>2015-09-24 12:22:19 +1000
commit2c4a9f6a3be8945559f127adb8695a11d833cd89 (patch)
tree442e4cac33bfd19c9fb349ca27c6e7509941caf1 /android
parent18bc490f40e997bc2a1cbaa2cf82a9ffd2fa2405 (diff)
downloadqtlocation-mapboxgl-2c4a9f6a3be8945559f127adb8695a11d833cd89.tar.gz
Refactor NativeMapView to hold std::unique_ptr to Map and DefaultFileSource
Add missing asserts to attach_jni_thread/detach_jni_thread Fixes #2406
Diffstat (limited to 'android')
-rw-r--r--android/cpp/jni.cpp6
-rw-r--r--android/cpp/native_map_view.cpp41
2 files changed, 29 insertions, 18 deletions
diff --git a/android/cpp/jni.cpp b/android/cpp/jni.cpp
index 140cc9f3ca..5ed0a730c3 100644
--- a/android/cpp/jni.cpp
+++ b/android/cpp/jni.cpp
@@ -117,6 +117,9 @@ bool throw_jni_error(JNIEnv *env, const char *msg) {
}
bool attach_jni_thread(JavaVM* vm, JNIEnv** env, std::string threadName) {
+ assert(vm != nullptr);
+ assert(env != nullptr);
+
JavaVMAttachArgs args = {JNI_VERSION_1_2, threadName.c_str(), NULL};
jint ret;
@@ -142,6 +145,9 @@ bool attach_jni_thread(JavaVM* vm, JNIEnv** env, std::string threadName) {
void detach_jni_thread(JavaVM* vm, JNIEnv** env, bool detach) {
if (detach) {
+ assert(vm != nullptr);
+ assert(env != nullptr);
+
jint ret;
if ((ret = vm->DetachCurrentThread()) != JNI_OK) {
mbgl::Log::Error(mbgl::Event::JNI, "DetachCurrentThread() failed with %i", ret);
diff --git a/android/cpp/native_map_view.cpp b/android/cpp/native_map_view.cpp
index 6c1cd87bb2..cc04f089e6 100644
--- a/android/cpp/native_map_view.cpp
+++ b/android/cpp/native_map_view.cpp
@@ -57,14 +57,9 @@ NativeMapView::NativeMapView(JNIEnv *env, jobject obj_, float pixelRatio_, int a
: mbgl::View(*this),
pixelRatio(pixelRatio_),
availableProcessors(availableProcessors_),
- totalMemory(totalMemory_),
- fileCache(mbgl::SharedSQLiteCache::get(mbgl::android::cachePath + "/mbgl-cache.db")),
- fileSource(fileCache.get()),
- map(*this, fileSource, MapMode::Continuous) {
+ totalMemory(totalMemory_) {
mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::NativeMapView");
- map.pause();
-
assert(env != nullptr);
assert(obj_ != nullptr);
@@ -78,6 +73,12 @@ NativeMapView::NativeMapView(JNIEnv *env, jobject obj_, float pixelRatio_, int a
env->ExceptionDescribe();
return;
}
+
+ fileCache = mbgl::SharedSQLiteCache::get(mbgl::android::cachePath + "/mbgl-cache.db");
+ fileSource = std::make_unique<mbgl::DefaultFileSource>(fileCache.get());
+ map = std::make_unique<mbgl::Map>(*this, *fileSource, MapMode::Continuous);
+
+ map->pause();
}
NativeMapView::~NativeMapView() {
@@ -89,6 +90,10 @@ NativeMapView::~NativeMapView() {
assert(vm != nullptr);
assert(obj != nullptr);
+ map.reset();
+ fileSource.reset();
+ fileCache.reset();
+
jint ret;
JNIEnv *env = nullptr;
ret = vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
@@ -183,9 +188,9 @@ void NativeMapView::notify() {
// noop
}
-mbgl::Map &NativeMapView::getMap() { return map; }
+mbgl::Map &NativeMapView::getMap() { return *map; }
-mbgl::DefaultFileSource &NativeMapView::getFileSource() { return fileSource; }
+mbgl::DefaultFileSource &NativeMapView::getFileSource() { return *fileSource; }
bool NativeMapView::inEmulator() {
// Detect if we are in emulator
@@ -620,7 +625,7 @@ void NativeMapView::pause() {
mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::pause");
if ((display != EGL_NO_DISPLAY) && (context != EGL_NO_CONTEXT)) {
- map.pause();
+ map->pause();
}
}
@@ -631,7 +636,7 @@ void NativeMapView::resume() {
assert(context != EGL_NO_CONTEXT);
if (surface != EGL_NO_SURFACE) {
- map.resume();
+ map->resume();
} else {
mbgl::Log::Debug(mbgl::Event::Android, "Not resuming because we are not ready");
}
@@ -699,37 +704,37 @@ void NativeMapView::updateFps() {
void NativeMapView::onInvalidate() {
mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::onInvalidate()");
- if (map.isPaused()) {
+ if (map->isPaused()) {
mbgl::Log::Debug(mbgl::Event::Android, "Not rendering as map is paused");
return;
}
const bool dirty = !clean.test_and_set();
if (dirty) {
- float zoomFactor = map.getMaxZoom() - map.getMinZoom() + 1;
+ float zoomFactor = map->getMaxZoom() - map->getMinZoom() + 1;
float cpuFactor = availableProcessors;
float memoryFactor = static_cast<float>(totalMemory) / 1000.0f / 1000.0f / 1000.0f;
- float sizeFactor = (static_cast<float>(map.getWidth()) / mbgl::util::tileSize) *
- (static_cast<float>(map.getHeight()) / mbgl::util::tileSize);
+ float sizeFactor = (static_cast<float>(map->getWidth()) / mbgl::util::tileSize) *
+ (static_cast<float>(map->getHeight()) / mbgl::util::tileSize);
size_t cacheSize = zoomFactor * cpuFactor * memoryFactor * sizeFactor * 0.5f;
- map.setSourceTileCacheSize(cacheSize);
+ map->setSourceTileCacheSize(cacheSize);
- map.renderSync();
+ map->renderSync();
}
}
void NativeMapView::resizeView(int w, int h) {
width = w;
height = h;
- map.update(mbgl::Update::Dimensions);
+ map->update(mbgl::Update::Dimensions);
}
void NativeMapView::resizeFramebuffer(int w, int h) {
fbWidth = w;
fbHeight = h;
- map.update(mbgl::Update::Repaint);
+ map->update(mbgl::Update::Repaint);
}
}