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

Commit

Permalink
[Android] Add setPreferredFramesPerSecond for MapView (#13498)
Browse files Browse the repository at this point in the history
* [Android] Add setPreferredFramesPerSecond for MapView
  • Loading branch information
Kevin Li committed Dec 12, 2018
1 parent d812bab commit 2d527c2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,18 @@ public void onDestroy() {
}
}

/**
* The maximum frame rate at which the map view is rendered,
* but it can't excess the ability of device hardware.
*
* @param maximumFps Can be set to arbitrary integer values.
*/
public void setMaximumFps(int maximumFps) {
if (mapRenderer != null) {
mapRenderer.setMaximumFps(maximumFps);
}
}

/**
* Returns if the map has been destroyed.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public abstract class MapRenderer implements MapRendererScheduler {
// Holds the pointer to the native peer after initialisation
private long nativePtr = 0;

private double expectedRenderTime = 0;
private MapboxMap.OnFpsChangedListener onFpsChangedListener;

public MapRenderer(@NonNull Context context, String localIdeographFontFamily) {
Expand Down Expand Up @@ -75,11 +76,20 @@ protected void onSurfaceChanged(@NonNull GL10 gl, int width, int height) {

@CallSuper
protected void onDrawFrame(GL10 gl) {
long startTime = System.nanoTime();
try {
nativeRender();
} catch (java.lang.Error error) {
Logger.e(TAG, error.getMessage());
}
long renderTime = System.nanoTime() - startTime;
if (renderTime < expectedRenderTime) {
try {
Thread.sleep((long) ((expectedRenderTime - renderTime) / 1E6));
} catch (InterruptedException ex) {
Logger.e(TAG, ex.getMessage());
}
}
if (onFpsChangedListener != null) {
updateFps();
}
Expand Down Expand Up @@ -115,18 +125,26 @@ private native void nativeInitialize(MapRenderer self,

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;
double fps = 1E9 / ((currentTime - timeElapsed));
onFpsChangedListener.onFpsChanged(fps);
timeElapsed = currentTime;
}

/**
* The max frame rate at which this render is rendered,
* but it can't excess the ability of device hardware.
*
* @param maximumFps Can be set to arbitrary integer values.
*/
public void setMaximumFps(int maximumFps) {
if (maximumFps <= 0) {
// Not valid, just return
return;
}
expectedRenderTime = 1E9 / maximumFps;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected void onCreate(Bundle savedInstanceState) {
setupMapView(savedInstanceState);
setupDebugChangeView();
setupStyleChangeView();
setupFpsChangeView();
}

private void setupToolbar() {
Expand Down Expand Up @@ -172,6 +173,15 @@ private void setupStyleChangeView() {
});
}

private void setupFpsChangeView() {
findViewById(R.id.fps_30).setOnClickListener(view -> {
mapView.setMaximumFps(30);
});
findViewById(R.id.fps_60).setOnClickListener(view -> {
mapView.setMaximumFps(60);
});
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
return actionBarDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@
app:layout_anchor="@id/bottom_sheet"
app:layout_anchorGravity="bottom|end"/>

<Button
android:id="@+id/fps_60"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|start"
app:backgroundTint="@color/primary"
android:layout_margin="@dimen/fab_margin"
android:text="@string/fps60"
app:layout_anchor="@id/bottom_sheet"
app:layout_anchorGravity="top|end"/>

<Button
android:id="@+id/fps_30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:layout_marginBottom="82dp"
app:backgroundTint="@color/primary"
android:layout_marginEnd="@dimen/fab_margin"
android:layout_marginRight="@dimen/fab_margin"
android:text="@string/fps30"
app:layout_anchor="@id/fps_60"
app:layout_anchorGravity="top"/>
</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Mapbox Android SDK TestApp</string>
<string name="fps30">fps30</string>
<string name="fps60">fps60</string>
</resources>

0 comments on commit 2d527c2

Please sign in to comment.