[android] re-implemented the fps listener api

This commit is contained in:
Ivo van Dongen
2017-09-18 19:00:34 +03:00
committed by Ivo van Dongen
parent bef58c38a8
commit b0c06c764b
8 changed files with 77 additions and 80 deletions

View File

@@ -435,21 +435,6 @@ public class MapView extends FrameLayout {
nativeMapView.onLowMemory();
}
// Called when debug mode is enabled to update a FPS counter
// Called via JNI from NativeMapView
// Forward to any listener
protected void onFpsChanged(final double fps) {
final MapboxMap.OnFpsChangedListener listener = mapboxMap.getOnFpsChangedListener();
if (listener != null) {
post(new Runnable() {
@Override
public void run() {
listener.onFpsChanged(fps);
}
});
}
}
/**
* <p>
* Loads a new map style from the specified URL.

View File

@@ -1711,6 +1711,7 @@ public final class MapboxMap {
*/
public void setOnFpsChangedListener(@Nullable OnFpsChangedListener listener) {
onFpsChangedListener = listener;
nativeMapView.setOnFpsChangedListener(listener);
}
// used by MapView

View File

@@ -20,7 +20,6 @@ import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.geometry.ProjectedMeters;
import com.mapbox.mapboxsdk.maps.renderer.MapRenderer;
import com.mapbox.mapboxsdk.maps.renderer.MapRendererScheduler;
import com.mapbox.mapboxsdk.storage.FileSource;
import com.mapbox.mapboxsdk.style.layers.CannotAddLayerException;
import com.mapbox.mapboxsdk.style.layers.Filter;
@@ -54,7 +53,7 @@ final class NativeMapView {
private final FileSource fileSource;
// Used to schedule work on the MapRenderer Thread
private MapRendererScheduler scheduler;
private MapRenderer mapRenderer;
// Device density
private final float pixelRatio;
@@ -71,7 +70,7 @@ final class NativeMapView {
//
public NativeMapView(final MapView mapView, MapRenderer mapRenderer) {
this.scheduler = mapRenderer;
this.mapRenderer = mapRenderer;
this.mapView = mapView;
Context context = mapView.getContext();
@@ -106,7 +105,7 @@ final class NativeMapView {
return;
}
scheduler.requestRender();
mapRenderer.requestRender();
}
public void resizeView(int width, int height) {
@@ -510,13 +509,6 @@ final class NativeMapView {
return nativeGetDebug();
}
public void setEnableFps(boolean enable) {
if (isDestroyedOn("setEnableFps")) {
return;
}
nativeSetEnableFps(enable);
}
public boolean isFullyLoaded() {
if (isDestroyedOn("isFullyLoaded")) {
return false;
@@ -835,13 +827,6 @@ final class NativeMapView {
}
}
protected void onFpsChanged(double fps) {
if (isDestroyedOn("OnFpsChanged")) {
return;
}
mapView.onFpsChanged(fps);
}
protected void onSnapshotReady(Bitmap mapContent) {
if (isDestroyedOn("OnSnapshotReady")) {
return;
@@ -947,8 +932,6 @@ final class NativeMapView {
private native boolean nativeGetDebug();
private native void nativeSetEnableFps(boolean enable);
private native boolean nativeIsFullyLoaded();
private native void nativeSetReachability(boolean status);
@@ -1076,4 +1059,28 @@ final class NativeMapView {
nativeTakeSnapshot();
}
public void setOnFpsChangedListener(final MapboxMap.OnFpsChangedListener listener) {
mapRenderer.queueEvent(new Runnable() {
@Override
public void run() {
mapRenderer.setOnFpsChangedListener(new MapboxMap.OnFpsChangedListener() {
@Override
public void onFpsChanged(final double fps) {
mapView.post(new Runnable() {
@Override
public void run() {
listener.onFpsChanged(fps);
}
});
}
});
}
});
}
}

View File

@@ -3,6 +3,7 @@ package com.mapbox.mapboxsdk.maps.renderer;
import android.content.Context;
import android.opengl.GLSurfaceView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.storage.FileSource;
import javax.microedition.khronos.egl.EGLConfig;
@@ -22,6 +23,8 @@ public class MapRenderer implements GLSurfaceView.Renderer, MapRendererScheduler
private final GLSurfaceView glSurfaceView;
private MapboxMap.OnFpsChangedListener onFpsChangedListener;
public MapRenderer(Context context, GLSurfaceView glSurfaceView) {
this.glSurfaceView = glSurfaceView;
@@ -33,6 +36,10 @@ public class MapRenderer implements GLSurfaceView.Renderer, MapRendererScheduler
nativeInitialize(this, fileSource, pixelRatio, programCacheDir);
}
public void setOnFpsChangedListener(MapboxMap.OnFpsChangedListener listener) {
onFpsChangedListener = listener;
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
nativeOnSurfaceCreated();
@@ -65,6 +72,10 @@ public class MapRenderer implements GLSurfaceView.Renderer, MapRendererScheduler
@Override
public void onDrawFrame(GL10 gl) {
nativeRender();
if (onFpsChangedListener != null) {
updateFps();
}
}
/**
@@ -116,4 +127,19 @@ public class MapRenderer implements GLSurfaceView.Renderer, MapRendererScheduler
private native void nativeRender();
private long frames;
private long timeElapsed;
private void updateFps() {
frames++;
long currentTime = System.nanoTime();
double fps = 0;
if (currentTime - timeElapsed >= 1) {
fps = frames / ((currentTime - timeElapsed) / 1E9);
onFpsChangedListener.onFpsChanged(fps);
timeElapsed = currentTime;
frames = 0;
}
}
}

View File

@@ -26,6 +26,7 @@ import com.mapbox.mapboxsdk.style.layers.Property;
import com.mapbox.mapboxsdk.testapp.R;
import java.util.List;
import java.util.Locale;
import timber.log.Timber;
@@ -104,6 +105,17 @@ public class DebugModeActivity extends AppCompatActivity implements OnMapReadyCa
setupNavigationView(mapboxMap.getLayers());
setupZoomView();
setFpsView();
}
private void setFpsView() {
final TextView fpsView = (TextView) findViewById(R.id.fpsView);
mapboxMap.setOnFpsChangedListener(new MapboxMap.OnFpsChangedListener() {
@Override
public void onFpsChanged(double fps) {
fpsView.setText(String.format(Locale.US,"FPS: %4.2f", fps));
}
});
}
private void setupNavigationView(List<Layer> layerList) {

View File

@@ -35,6 +35,17 @@
app:layout_anchor="@id/bottom_sheet"
app:layout_anchorGravity="bottom|start"/>
<TextView
android:id="@+id/fpsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:layout_margin="8dp"
android:textIsSelectable="true"
android:textSize="14sp"
app:layout_anchor="@id/textZoom"
app:layout_anchorGravity="top"/>
<android.support.v4.widget.NestedScrollView
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"

View File

@@ -406,10 +406,6 @@ void NativeMapView::scheduleSnapshot(jni::JNIEnv&) {
});
}
void NativeMapView::enableFps(jni::JNIEnv&, jni::jboolean enable) {
fpsEnabled = enable;
}
jni::Object<CameraPosition> NativeMapView::getCameraPosition(jni::JNIEnv& env) {
return CameraPosition::New(env, map->getCameraOptions(insets));
}
@@ -457,12 +453,10 @@ void NativeMapView::setDebug(JNIEnv&, jni::jboolean debug) {
DebugOptions debugOptions = debug ? DebugOptions::TileBorders | DebugOptions::ParseStatus | DebugOptions::Collision
: DebugOptions::NoDebug;
map->setDebug(debugOptions);
fpsEnabled = debug;
}
void NativeMapView::cycleDebugOptions(JNIEnv&) {
map->cycleDebugOptions();
fpsEnabled = map->getDebug() != DebugOptions::NoDebug;
}
jni::jboolean NativeMapView::getDebug(JNIEnv&) {
@@ -926,36 +920,6 @@ jni::jboolean NativeMapView::getPrefetchesTiles(JNIEnv&) {
return jni::jboolean(map->getPrefetchZoomDelta() > 0);
}
// Private methods //
// TODO
void NativeMapView::updateFps() {
if (!fpsEnabled) {
return;
}
static int frames = 0;
static int64_t timeElapsed = 0LL;
frames++;
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
int64_t currentTime = now.tv_sec * 1000000000LL + now.tv_nsec;
if (currentTime - timeElapsed >= 1) {
fps = frames / ((currentTime - timeElapsed) / 1E9);
mbgl::Log::Info(mbgl::Event::Render, "FPS: %4.2f", fps);
timeElapsed = currentTime;
frames = 0;
}
assert(vm != nullptr);
android::UniqueEnv _env = android::AttachEnv();
static auto onFpsChanged = javaClass.GetMethod<void (double)>(*_env, "onFpsChanged");
javaPeer->Call(*_env, onFpsChanged, fps);
}
// Static methods //
jni::Class<NativeMapView> NativeMapView::javaClass;
@@ -1004,7 +968,6 @@ void NativeMapView::registerNative(jni::JNIEnv& env) {
METHOD(&NativeMapView::setVisibleCoordinateBounds, "nativeSetVisibleCoordinateBounds"),
METHOD(&NativeMapView::setContentPadding, "nativeSetContentPadding"),
METHOD(&NativeMapView::scheduleSnapshot, "nativeTakeSnapshot"),
METHOD(&NativeMapView::enableFps, "nativeSetEnableFps"),
METHOD(&NativeMapView::getCameraPosition, "nativeGetCameraPosition"),
METHOD(&NativeMapView::updateMarker, "nativeUpdateMarker"),
METHOD(&NativeMapView::addMarkers, "nativeAddMarkers"),

View File

@@ -142,8 +142,6 @@ public:
void scheduleSnapshot(jni::JNIEnv&);
void enableFps(jni::JNIEnv&, jni::jboolean enable);
jni::Object<CameraPosition> getCameraPosition(jni::JNIEnv&);
void updateMarker(jni::JNIEnv&, jni::jlong, jni::jdouble, jni::jdouble, jni::String);
@@ -242,10 +240,6 @@ public:
jni::jboolean getPrefetchesTiles(JNIEnv&);
private:
void updateFps();
private:
std::unique_ptr<AndroidRendererFrontend> rendererFrontend;
@@ -258,8 +252,6 @@ private:
std::string apiKey;
float pixelRatio;
bool fpsEnabled = false;
double fps = 0.0;
// Minimum texture size according to OpenGL ES 2.0 specification.
int width = 64;