From 2c4a9f6a3be8945559f127adb8695a11d833cd89 Mon Sep 17 00:00:00 2001 From: Leith Bade Date: Thu, 24 Sep 2015 12:18:59 +1000 Subject: Refactor NativeMapView to hold std::unique_ptr to Map and DefaultFileSource Add missing asserts to attach_jni_thread/detach_jni_thread Fixes #2406 --- android/cpp/jni.cpp | 6 ++++++ android/cpp/native_map_view.cpp | 41 +++++++++++++++++++++++------------------ 2 files changed, 29 insertions(+), 18 deletions(-) (limited to 'android') 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(fileCache.get()); + map = std::make_unique(*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(&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(totalMemory) / 1000.0f / 1000.0f / 1000.0f; - float sizeFactor = (static_cast(map.getWidth()) / mbgl::util::tileSize) * - (static_cast(map.getHeight()) / mbgl::util::tileSize); + float sizeFactor = (static_cast(map->getWidth()) / mbgl::util::tileSize) * + (static_cast(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); } } -- cgit v1.2.1