From d57a02f3a9274d331ddaa7a82d062e36fdc09251 Mon Sep 17 00:00:00 2001 From: Leith Bade Date: Tue, 28 Jul 2015 14:34:16 +1000 Subject: First pass at updating JNI bindings Hook up collisionDebug, isFullyLoaded, onLowMemory in Android Rename class to styleClass in Java to match iOS Fixed some formatting Fixes #1905 Fixes #1946 Fixes #1263 --- android/cpp/jni.cpp | 68 ++++++++++++++++++++++++++++++----------- android/cpp/native_map_view.cpp | 15 ++++++++- 2 files changed, 65 insertions(+), 18 deletions(-) (limited to 'android/cpp') diff --git a/android/cpp/jni.cpp b/android/cpp/jni.cpp index 0521e07ad0..85022dbc0d 100644 --- a/android/cpp/jni.cpp +++ b/android/cpp/jni.cpp @@ -272,6 +272,7 @@ jlongArray std_vector_uint_to_jobject(JNIEnv *env, std::vector vector) return jarray; } + } } @@ -279,13 +280,12 @@ namespace { using namespace mbgl::android; -jlong JNICALL -nativeCreate(JNIEnv *env, jobject obj, jstring cachePath_, jstring dataPath_, jstring apkPath_, jfloat pixelRatio) { +jlong JNICALL nativeCreate(JNIEnv *env, jobject obj, jstring cachePath_, jstring dataPath_, jstring apkPath_, jfloat pixelRatio, jint availableProcessors, jlong totalMemory) { mbgl::Log::Debug(mbgl::Event::JNI, "nativeCreate"); cachePath = std_string_from_jstring(env, cachePath_); dataPath = std_string_from_jstring(env, dataPath_); apkPath = std_string_from_jstring(env, apkPath_); - NativeMapView *nativeMapView = new NativeMapView(env, obj, pixelRatio); + NativeMapView *nativeMapView = new NativeMapView(env, obj, pixelRatio, availableProcessors, totalMemory); jlong mapViewPtr = reinterpret_cast(nativeMapView); return mapViewPtr; } @@ -337,8 +337,7 @@ void JNICALL nativeTerminateContext(JNIEnv *env, jobject obj, jlong nativeMapVie nativeMapView->terminateContext(); } -void JNICALL -nativeCreateSurface(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject surface) { +void JNICALL nativeCreateSurface(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject surface) { mbgl::Log::Debug(mbgl::Event::JNI, "nativeCreateSurface"); assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast(nativeMapViewPtr); @@ -428,8 +427,7 @@ void JNICALL nativeAddClass(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, js nativeMapView->getMap().addClass(std_string_from_jstring(env, clazz)); } -void JNICALL -nativeSetClasses(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject classes) { +void JNICALL nativeSetClasses(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject classes) { mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetClasses"); assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast(nativeMapViewPtr); @@ -482,8 +480,7 @@ jstring JNICALL nativeGetStyleJSON(JNIEnv *env, jobject obj, jlong nativeMapView return std_string_to_jstring(env, nativeMapView->getMap().getStyleJSON()); } -void JNICALL -nativeSetAccessToken(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jstring accessToken) { +void JNICALL nativeSetAccessToken(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jstring accessToken) { mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetAccessToken"); assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast(nativeMapViewPtr); @@ -691,7 +688,6 @@ std::pair readPolygon(JNIEnv *en return std::make_pair(segment, shapeProperties); } - jlong JNICALL nativeAddPolygon(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject polygon) { mbgl::Log::Debug(mbgl::Event::JNI, "nativeAddPolygon"); assert(nativeMapViewPtr != 0); @@ -760,7 +756,6 @@ jlongArray JNICALL nativeAddPolygons(JNIEnv *env, jobject obj, jlong nativeMapVi return std_vector_uint_to_jobject(env, shapeAnnotationIDs); } - void JNICALL nativeRemoveAnnotation(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jlong annotationId) { mbgl::Log::Debug(mbgl::Event::JNI, "nativeRemoveAnnotation"); assert(nativeMapViewPtr != 0); @@ -845,8 +840,7 @@ jdouble JNICALL nativeGetScale(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) return nativeMapView->getMap().getScale(); } -void JNICALL -nativeSetZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jdouble zoom, jlong duration) { +void JNICALL nativeSetZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jdouble zoom, jlong duration) { mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetZoom"); assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast(nativeMapViewPtr); @@ -962,6 +956,13 @@ void JNICALL nativeResetNorth(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) nativeMapView->getMap().resetNorth(); } +void JNICALL nativeOnLowMemory(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeOnLowMemory"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast(nativeMapViewPtr); + nativeMapView->getMap().onLowMemory(); +} + void JNICALL nativeSetDebug(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jboolean debug) { mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetDebug"); assert(nativeMapViewPtr != 0); @@ -985,8 +986,35 @@ jboolean JNICALL nativeGetDebug(JNIEnv *env, jobject obj, jlong nativeMapViewPtr return nativeMapView->getMap().getDebug(); } -void JNICALL -nativeSetReachability(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jboolean status) { +void JNICALL nativeSetCollisionDebug(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jboolean debug) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetCollisionDebug"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast(nativeMapViewPtr); + nativeMapView->getMap().setCollisionDebug(debug); +} + +void JNICALL nativeToggleCollisionDebug(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeToggleCollisionDebug"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast(nativeMapViewPtr); + nativeMapView->getMap().toggleCollisionDebug(); +} + +jboolean JNICALL nativeGetCollisionDebug(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetCollisionDebug"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast(nativeMapViewPtr); + return nativeMapView->getMap().getCollisionDebug(); +} + +jboolean JNICALL nativeIsFullyLoaded(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeIsFullyLoaded"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast(nativeMapViewPtr); + return nativeMapView->getMap().isFullyLoaded(); +} + +void JNICALL nativeSetReachability(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jboolean status) { mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetReachability"); assert(nativeMapViewPtr != 0); if (status) { @@ -1112,6 +1140,7 @@ jobject JNICALL nativeLatLngForPixel(JNIEnv *env, jobject obj, jlong nativeMapVi return ret; } + } extern "C" { @@ -1167,7 +1196,7 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { env->ExceptionDescribe(); return JNI_ERR; } - + markerSpriteId = env->GetFieldID(markerClass, "sprite", "Ljava/lang/String;"); if (markerSpriteId == nullptr) { env->ExceptionDescribe(); @@ -1415,7 +1444,7 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { } const std::vector methods = { - {"nativeCreate", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;F)J", + {"nativeCreate", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;FIJ)J", reinterpret_cast(&nativeCreate)}, {"nativeDestroy", "(J)V", reinterpret_cast(&nativeDestroy)}, {"nativeInitializeDisplay", "(J)V", reinterpret_cast(&nativeInitializeDisplay)}, @@ -1500,9 +1529,14 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { &nativeSetBearing))}, {"nativeGetBearing", "(J)D", reinterpret_cast(&nativeGetBearing)}, {"nativeResetNorth", "(J)V", reinterpret_cast(&nativeResetNorth)}, + {"nativeOnLowMemory", "(J)V", reinterpret_cast(&nativeOnLowMemory)}, {"nativeSetDebug", "(JZ)V", reinterpret_cast(&nativeSetDebug)}, {"nativeToggleDebug", "(J)V", reinterpret_cast(&nativeToggleDebug)}, {"nativeGetDebug", "(J)Z", reinterpret_cast(&nativeGetDebug)}, + {"nativeSetCollisionDebug", "(JZ)V", reinterpret_cast(&nativeSetCollisionDebug)}, + {"nativeToggleCollisionDebug", "(J)V", reinterpret_cast(&nativeToggleCollisionDebug)}, + {"nativeGetCollisionDebug", "(J)Z", reinterpret_cast(&nativeGetCollisionDebug)}, + {"nativeIsFullyLoaded", "(J)Z", reinterpret_cast(&nativeIsFullyLoaded)}, {"nativeSetReachability", "(JZ)V", reinterpret_cast(&nativeSetReachability)}, //{"nativeGetWorldBoundsMeters", "(J)V", reinterpret_cast(&nativeGetWorldBoundsMeters)}, //{"nativeGetWorldBoundsLatLng", "(J)V", reinterpret_cast(&nativeGetWorldBoundsLatLng)}, diff --git a/android/cpp/native_map_view.cpp b/android/cpp/native_map_view.cpp index 9c7a3efde7..351833a07d 100644 --- a/android/cpp/native_map_view.cpp +++ b/android/cpp/native_map_view.cpp @@ -15,6 +15,7 @@ #include #include #include +#include namespace mbgl { namespace android { @@ -52,9 +53,11 @@ void log_gl_string(GLenum name, const char *label) { } } -NativeMapView::NativeMapView(JNIEnv *env, jobject obj_, float pixelRatio_) +NativeMapView::NativeMapView(JNIEnv *env, jobject obj_, float pixelRatio_, int availableProcessors_, size_t totalMemory_) : mbgl::View(*this), pixelRatio(pixelRatio_), + availableProcessors(availableProcessors_), + totalMemory(totalMemory_), fileCache(mbgl::android::cachePath + "/mbgl-cache.db"), fileSource(&fileCache), map(*this, fileSource, MapMode::Continuous) { @@ -763,6 +766,16 @@ void NativeMapView::onInvalidate(bool inProgress) { const bool dirty = !clean.test_and_set(); if (dirty) { + 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); + + size_t cacheSize = zoomFactor * cpuFactor * memoryFactor * sizeFactor * 0.5f; + + map.setSourceTileCacheSize(cacheSize); + const bool needsRerender = map.renderSync(); if (!inProgress) { map.nudgeTransitions(needsRerender); -- cgit v1.2.1