Add fps mode for #646
This commit is contained in:
@@ -30,6 +30,7 @@ std::string data_path;
|
||||
std::string apk_path;
|
||||
|
||||
jmethodID on_map_changed_id = nullptr;
|
||||
jmethodID on_fps_changed_id = nullptr;
|
||||
|
||||
jclass lon_lat_class = nullptr;
|
||||
jmethodID lon_lat_constructor_id = nullptr;
|
||||
@@ -665,14 +666,14 @@ void JNICALL nativeSetDebug(JNIEnv* env, jobject obj, jlong native_map_view_ptr,
|
||||
mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetDebug");
|
||||
assert(native_map_view_ptr != 0);
|
||||
NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
|
||||
COFFEE_TRY_JNI(env, native_map_view->getMap().setDebug(debug));
|
||||
COFFEE_TRY_JNI(env, native_map_view->getMap().setDebug(debug); native_map_view->enableFps(debug));
|
||||
}
|
||||
|
||||
void JNICALL nativeToggleDebug(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
|
||||
mbgl::Log::Debug(mbgl::Event::JNI, "nativeToggleDebug");
|
||||
assert(native_map_view_ptr != 0);
|
||||
NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
|
||||
COFFEE_TRY_JNI(env, native_map_view->getMap().toggleDebug());
|
||||
COFFEE_TRY_JNI(env, native_map_view->getMap().toggleDebug(); native_map_view->enableFps(native_map_view->getMap().getDebug()));
|
||||
}
|
||||
|
||||
jboolean JNICALL nativeGetDebug(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
|
||||
@@ -773,6 +774,12 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
on_fps_changed_id = env->GetMethodID(native_map_view_class, "onFpsChanged", "(D)V");
|
||||
if (on_fps_changed_id == nullptr) {
|
||||
env->ExceptionDescribe();
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
runtime_exception_class = env->FindClass("java/lang/RuntimeException");
|
||||
if (runtime_exception_class == nullptr) {
|
||||
env->ExceptionDescribe();
|
||||
@@ -951,6 +958,7 @@ extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
|
||||
lon_lat_zoom_zoom_id = nullptr;
|
||||
|
||||
on_map_changed_id = nullptr;
|
||||
on_fps_changed_id = nullptr;
|
||||
|
||||
env->DeleteGlobalRef(runtime_exception_class);
|
||||
runtime_exception_class = nullptr;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <memory>
|
||||
#include <list>
|
||||
#include <tuple>
|
||||
@@ -72,6 +73,7 @@ void MBGLView::swap() {
|
||||
mbgl::Log::Error(mbgl::Event::OpenGL, "eglSwapBuffers() returned error %d", eglGetError());
|
||||
}
|
||||
map->swapped();
|
||||
nativeView.updateFps();
|
||||
} else {
|
||||
mbgl::Log::Info(mbgl::Event::Android, "Not swapping as we are not ready");
|
||||
}
|
||||
@@ -664,5 +666,74 @@ void NativeMapView::notifyMapChange() {
|
||||
env = nullptr;
|
||||
}
|
||||
|
||||
void NativeMapView::enableFps(bool enable) {
|
||||
mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::enableFps()");
|
||||
|
||||
fps_enabled = enable;
|
||||
}
|
||||
|
||||
void NativeMapView::updateFps() {
|
||||
mbgl::Log::Debug(mbgl::Event::Android, "NativeMapView::updateFps()");
|
||||
|
||||
if (!fps_enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
static int frames = 0;
|
||||
static int64_t time_elapsed = 0LL;
|
||||
|
||||
frames++;
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
int64_t current_time = now.tv_sec*1000000000LL + now.tv_nsec;
|
||||
|
||||
if (current_time - time_elapsed >= 1) {
|
||||
fps = frames / ((current_time - time_elapsed) / 1E9);
|
||||
mbgl::Log::Debug(mbgl::Event::Render, "FPS: %4.2f", fps);
|
||||
time_elapsed = current_time;
|
||||
frames = 0;
|
||||
}
|
||||
|
||||
assert(vm != nullptr);
|
||||
assert(obj != nullptr);
|
||||
|
||||
JavaVMAttachArgs args = {
|
||||
JNI_VERSION_1_2,
|
||||
"NativeMapView::updateFps()",
|
||||
NULL
|
||||
};
|
||||
|
||||
jint ret;
|
||||
JNIEnv* env = nullptr;
|
||||
bool detach = false;
|
||||
ret = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
|
||||
if (ret != JNI_OK) {
|
||||
if (ret != JNI_EDETACHED) {
|
||||
mbgl::Log::Error(mbgl::Event::JNI, "GetEnv() failed with %i", ret);
|
||||
return;
|
||||
} else {
|
||||
ret = vm->AttachCurrentThread(&env, &args);
|
||||
if (ret != JNI_OK) {
|
||||
mbgl::Log::Error(mbgl::Event::JNI, "AttachCurrentThread() failed with %i", ret);
|
||||
return;
|
||||
}
|
||||
detach = true;
|
||||
}
|
||||
}
|
||||
|
||||
env->CallVoidMethod(obj, on_fps_changed_id, fps);
|
||||
if (env->ExceptionCheck()) {
|
||||
env->ExceptionDescribe();
|
||||
}
|
||||
|
||||
if (detach) {
|
||||
if ((ret = vm->DetachCurrentThread()) != JNI_OK) {
|
||||
mbgl::Log::Error(mbgl::Event::JNI, "DetachCurrentThread() failed with %i", ret);
|
||||
return;
|
||||
}
|
||||
}
|
||||
env = nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user