From 70db04a697b1bf456e8dce0122719f8ddc1ff116 Mon Sep 17 00:00:00 2001 From: Tobrun Date: Tue, 10 Apr 2018 11:10:14 +0200 Subject: [PATCH] [android] - add paused state to map renderer, don't render snapshot when paused (#11358) --- .../mapboxsdk/maps/renderer/MapRenderer.java | 8 ++++++-- .../imagegenerator/SnapshotActivity.java | 8 ++++++++ platform/android/src/map_renderer.cpp | 16 ++++++++++++++-- platform/android/src/map_renderer.hpp | 5 +++++ platform/android/src/native_map_view.hpp | 1 - 5 files changed, 33 insertions(+), 5 deletions(-) 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 fcee5bd1793..f1c70325a09 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 @@ -37,11 +37,11 @@ public void onStart() { } public void onPause() { - // Implement if needed + nativeOnPause(); } public void onResume() { - // Implement if needed + nativeOnResume(); } public void onStop() { @@ -124,6 +124,10 @@ private native void nativeInitialize(MapRenderer self, private native void nativeRender(); + private native void nativeOnResume(); + + private native void nativeOnPause(); + private long frames; private long timeElapsed; diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/imagegenerator/SnapshotActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/imagegenerator/SnapshotActivity.java index 655012f799b..09100458853 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/imagegenerator/SnapshotActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/imagegenerator/SnapshotActivity.java @@ -17,6 +17,8 @@ import java.util.Locale; +import timber.log.Timber; + /** * Test activity showcasing the Snapshot API to create and display a bitmap of the current shown Map. */ @@ -79,6 +81,12 @@ protected void onResume() { @Override protected void onPause() { super.onPause(); + mapboxMap.snapshot(new MapboxMap.SnapshotReadyCallback() { + @Override + public void onSnapshotReady(Bitmap snapshot) { + Timber.e("Regression test for https://github.com/mapbox/mapbox-gl-native/pull/11358"); + } + }); mapView.onPause(); } diff --git a/platform/android/src/map_renderer.cpp b/platform/android/src/map_renderer.cpp index 2440ac93ef2..f7e16b70919 100644 --- a/platform/android/src/map_renderer.cpp +++ b/platform/android/src/map_renderer.cpp @@ -136,7 +136,7 @@ void MapRenderer::render(JNIEnv&) { renderer->render(*params); // Deliver the snapshot if requested - if (snapshotCallback) { + if (snapshotCallback && !paused) { snapshotCallback->operator()(backend->readFramebuffer()); snapshotCallback.reset(); } @@ -174,6 +174,14 @@ void MapRenderer::onSurfaceChanged(JNIEnv&, jint width, jint height) { requestRender(); } +void MapRenderer::onResume(JNIEnv&) { + paused = false; +} + +void MapRenderer::onPause(JNIEnv&) { + paused = true; +} + // Static methods // jni::Class MapRenderer::javaClass; @@ -192,7 +200,11 @@ void MapRenderer::registerNative(jni::JNIEnv& env) { METHOD(&MapRenderer::onSurfaceCreated, "nativeOnSurfaceCreated"), METHOD(&MapRenderer::onSurfaceChanged, - "nativeOnSurfaceChanged")); + "nativeOnSurfaceChanged"), + METHOD(&MapRenderer::onResume, + "nativeOnResume"), + METHOD(&MapRenderer::onPause, + "nativeOnPause")); } MapRenderer& MapRenderer::getNativePeer(JNIEnv& env, jni::Object jObject) { diff --git a/platform/android/src/map_renderer.hpp b/platform/android/src/map_renderer.hpp index c36357af7a2..5fb5ef1a618 100644 --- a/platform/android/src/map_renderer.hpp +++ b/platform/android/src/map_renderer.hpp @@ -98,6 +98,10 @@ class MapRenderer : public Scheduler { void onSurfaceChanged(JNIEnv&, jint width, jint height); + void onResume(JNIEnv&); + + void onPause(JNIEnv&); + private: GenericUniqueWeakObject javaPeer; @@ -120,6 +124,7 @@ class MapRenderer : public Scheduler { std::mutex updateMutex; bool framebufferSizeChanged = false; + std::atomic paused {false}; std::unique_ptr snapshotCallback; }; diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp index d7e3b17b998..1b6371d726e 100755 --- a/platform/android/src/native_map_view.hpp +++ b/platform/android/src/native_map_view.hpp @@ -257,7 +257,6 @@ class NativeMapView : public MapObserver { MapRenderer& mapRenderer; std::string styleUrl; - std::string apiKey; float pixelRatio;