diff options
Diffstat (limited to 'platform')
5 files changed, 73 insertions, 8 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 83948efe4c..cc1ffbbd3d 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -422,6 +422,18 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback { } /** + * 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> * This method can be used to determine if the result of an asynchronous operation should be set. diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/MapRenderer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/MapRenderer.java index 9617f94fce..a9bea97836 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/MapRenderer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/renderer/MapRenderer.java @@ -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) { @@ -75,11 +76,20 @@ public abstract class MapRenderer implements MapRendererScheduler { @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(); } @@ -115,18 +125,26 @@ public abstract class MapRenderer implements 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; + 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; } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java index 88ab62a2ee..796560bb8b 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/DebugModeActivity.java @@ -63,6 +63,7 @@ public class DebugModeActivity extends AppCompatActivity implements OnMapReadyCa setupMapView(savedInstanceState); setupDebugChangeView(); setupStyleChangeView(); + setupFpsChangeView(); } private void setupToolbar() { @@ -172,6 +173,15 @@ public class DebugModeActivity extends AppCompatActivity implements OnMapReadyCa }); } + 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); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_debug_mode.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_debug_mode.xml index 2779ad7a08..1b185ab9c8 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_debug_mode.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_debug_mode.xml @@ -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 diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml index 15a916fac9..45048f8b07 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml @@ -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> |