Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
First pass at updating JNI bindings
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Leith Bade committed Jul 29, 2015
1 parent 3f2f9d2 commit d57a02f
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 66 deletions.
68 changes: 51 additions & 17 deletions android/cpp/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +272,20 @@ jlongArray std_vector_uint_to_jobject(JNIEnv *env, std::vector<uint32_t> vector)

return jarray;
}

}
}

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<jlong>(nativeMapView);
return mapViewPtr;
}
Expand Down Expand Up @@ -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<NativeMapView *>(nativeMapViewPtr);
Expand Down Expand Up @@ -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<NativeMapView *>(nativeMapViewPtr);
Expand Down Expand Up @@ -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<NativeMapView *>(nativeMapViewPtr);
Expand Down Expand Up @@ -691,7 +688,6 @@ std::pair<mbgl::AnnotationSegment, mbgl::StyleProperties> 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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<NativeMapView *>(nativeMapViewPtr);
Expand Down Expand Up @@ -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<NativeMapView *>(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);
Expand All @@ -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<NativeMapView *>(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<NativeMapView *>(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<NativeMapView *>(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<NativeMapView *>(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) {
Expand Down Expand Up @@ -1112,6 +1140,7 @@ jobject JNICALL nativeLatLngForPixel(JNIEnv *env, jobject obj, jlong nativeMapVi

return ret;
}

}

extern "C" {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -1415,7 +1444,7 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
}

const std::vector<JNINativeMethod> 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<void *>(&nativeCreate)},
{"nativeDestroy", "(J)V", reinterpret_cast<void *>(&nativeDestroy)},
{"nativeInitializeDisplay", "(J)V", reinterpret_cast<void *>(&nativeInitializeDisplay)},
Expand Down Expand Up @@ -1500,9 +1529,14 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
&nativeSetBearing))},
{"nativeGetBearing", "(J)D", reinterpret_cast<void *>(&nativeGetBearing)},
{"nativeResetNorth", "(J)V", reinterpret_cast<void *>(&nativeResetNorth)},
{"nativeOnLowMemory", "(J)V", reinterpret_cast<void *>(&nativeOnLowMemory)},
{"nativeSetDebug", "(JZ)V", reinterpret_cast<void *>(&nativeSetDebug)},
{"nativeToggleDebug", "(J)V", reinterpret_cast<void *>(&nativeToggleDebug)},
{"nativeGetDebug", "(J)Z", reinterpret_cast<void *>(&nativeGetDebug)},
{"nativeSetCollisionDebug", "(JZ)V", reinterpret_cast<void *>(&nativeSetCollisionDebug)},
{"nativeToggleCollisionDebug", "(J)V", reinterpret_cast<void *>(&nativeToggleCollisionDebug)},
{"nativeGetCollisionDebug", "(J)Z", reinterpret_cast<void *>(&nativeGetCollisionDebug)},
{"nativeIsFullyLoaded", "(J)Z", reinterpret_cast<void *>(&nativeIsFullyLoaded)},
{"nativeSetReachability", "(JZ)V", reinterpret_cast<void *>(&nativeSetReachability)},
//{"nativeGetWorldBoundsMeters", "(J)V", reinterpret_cast<void *>(&nativeGetWorldBoundsMeters)},
//{"nativeGetWorldBoundsLatLng", "(J)V", reinterpret_cast<void *>(&nativeGetWorldBoundsLatLng)},
Expand Down
15 changes: 14 additions & 1 deletion android/cpp/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <mbgl/platform/event.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/platform/gl.hpp>
#include <mbgl/util/constants.hpp>

namespace mbgl {
namespace android {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<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);

size_t cacheSize = zoomFactor * cpuFactor * memoryFactor * sizeFactor * 0.5f;

map.setSourceTileCacheSize(cacheSize);

const bool needsRerender = map.renderSync();
if (!inProgress) {
map.nudgeTransitions(needsRerender);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.mapboxgl.views;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -66,7 +67,7 @@ public class MapView extends SurfaceView {
private static final String STATE_DEBUG_ACTIVE = "debugActive";
private static final String STATE_STYLE_URL = "styleUrl";
private static final String STATE_ACCESS_TOKEN = "accessToken";
private static final String STATE_CLASSES = "classes";
private static final String STATE_STYLE_CLASSES = "styleClasses";
private static final String STATE_DEFAULT_TRANSITION_DURATION = "defaultTransitionDuration";

/**
Expand Down Expand Up @@ -156,7 +157,12 @@ private void initialize(Context context, AttributeSet attrs) {
String apkPath = context.getPackageCodePath();

// Create the NativeMapView
mNativeMapView = new NativeMapView(this, cachePath, dataPath, apkPath, mScreenDensity);
int availableProcessors = Runtime.getRuntime().availableProcessors();
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.getMemoryInfo(memoryInfo);
long totalMemory = memoryInfo.totalMem;
mNativeMapView = new NativeMapView(this, cachePath, dataPath, apkPath, mScreenDensity,availableProcessors, totalMemory);

// Load the attributes
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MapView, 0, 0);
Expand Down Expand Up @@ -227,7 +233,7 @@ public void setSprite(String symbol, float scale, Bitmap bitmap) {
if(bitmap.getConfig() != Bitmap.Config.ARGB_8888) {
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, false);
}
ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
ByteBuffer buffer = ByteBuffer.allocate(bitmap.getRowBytes() * bitmap.getHeight());
bitmap.copyPixelsToBuffer(buffer);

mNativeMapView.setSprite(symbol, bitmap.getWidth(), bitmap.getHeight(), scale, buffer.array());
Expand Down Expand Up @@ -398,15 +404,21 @@ public void setRotateEnabled(boolean rotateEnabled) {
}

public boolean isDebugActive() {
return mNativeMapView.getDebug();
return mNativeMapView.getDebug() || mNativeMapView.getCollisionDebug();
}

public void setDebugActive(boolean debugActive) {
mNativeMapView.setDebug(debugActive);
mNativeMapView.setCollisionDebug(debugActive);
}

public void toggleDebug() {
mNativeMapView.toggleDebug();
mNativeMapView.toggleCollisionDebug();
}

public boolean isFullyLoaded() {
return mNativeMapView.isFullyLoaded();
}

private void validateStyleUrl(String url) {
Expand Down Expand Up @@ -442,39 +454,39 @@ public String getAccessToken() {
return mNativeMapView.getAccessToken();
}

public List<String> getClasses() {
public List<String> getStyleClasses() {
return mNativeMapView.getClasses();
}

public void setClasses(List<String> classes) {
setClasses(classes, 0);
public void setStyleClasses(List<String> styleClasses) {
setStyleClasses(styleClasses, 0);
}

public void setClasses(List<String> classes, long transitionDuration) {
public void setStyleClasses(List<String> styleClasses, long transitionDuration) {
mNativeMapView.setDefaultTransitionDuration(transitionDuration);
mNativeMapView.setClasses(classes);
mNativeMapView.setClasses(styleClasses);
}

public void addClass(String clazz) {
mNativeMapView.addClass(clazz);
public void addStyleClass(String styleClass) {
mNativeMapView.addClass(styleClass);
}

public void removeClass(String clazz) {
mNativeMapView.removeClass(clazz);
public void removeStyleClass(String styleClass) {
mNativeMapView.removeClass(styleClass);
}

public boolean hasClass(String clazz) {
return mNativeMapView.hasClass(clazz);
public boolean hasStyleClass(String styleClass) {
return mNativeMapView.hasClass(styleClass);
}

public void removeAllClasses() {
removeAllClasses(0);
public void removeAllStyleClasses() {
removeAllStyleClasses(0);
}

public void removeAllClasses(long transitionDuration) {
public void removeAllStyleClasses(long transitionDuration) {
mNativeMapView.setDefaultTransitionDuration(transitionDuration);
ArrayList<String> classes = new ArrayList<>(0);
setClasses(classes);
ArrayList<String> styleClasses = new ArrayList<>(0);
setStyleClasses(styleClasses);
}

public LatLng fromScreenLocation(PointF point) {
Expand Down Expand Up @@ -503,9 +515,9 @@ public void onCreate(Bundle savedInstanceState) {
setDebugActive(savedInstanceState.getBoolean(STATE_DEBUG_ACTIVE));
setStyleUrl(savedInstanceState.getString(STATE_STYLE_URL));
setAccessToken(savedInstanceState.getString(STATE_ACCESS_TOKEN));
List<String> appliedClasses = savedInstanceState.getStringArrayList(STATE_CLASSES);
if (!appliedClasses.isEmpty()) {
setClasses(appliedClasses);
List<String> appliedStyleClasses = savedInstanceState.getStringArrayList(STATE_STYLE_CLASSES);
if (!appliedStyleClasses.isEmpty()) {
setStyleClasses(appliedStyleClasses);
}
mNativeMapView.setDefaultTransitionDuration(savedInstanceState.getLong(STATE_DEFAULT_TRANSITION_DURATION));
}
Expand All @@ -526,7 +538,7 @@ public void onSaveInstanceState(Bundle outState) {
outState.putBoolean(STATE_DEBUG_ACTIVE, isDebugActive());
outState.putString(STATE_STYLE_URL, mStyleUrl);
outState.putString(STATE_ACCESS_TOKEN, getAccessToken());
outState.putStringArrayList(STATE_CLASSES, new ArrayList<>(getClasses()));
outState.putStringArrayList(STATE_STYLE_CLASSES, new ArrayList<>(getStyleClasses()));
outState.putLong(STATE_DEFAULT_TRANSITION_DURATION, mNativeMapView.getDefaultTransitionDuration());
}

Expand Down Expand Up @@ -634,6 +646,12 @@ protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
}
}

// Called when the system is running low on memory
// Must be called from Activity onLowMemory
public void onLowMemory() {
mNativeMapView.onLowMemory();
}

//
// Draw events
//
Expand Down
Loading

0 comments on commit d57a02f

Please sign in to comment.