From 627b0d93388da023ef416070c856dabe223e037e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Mon, 30 Apr 2018 18:32:37 +0200 Subject: [PATCH] [android] - checking is renderer is not destroyed before delivering the snapshot --- .../mapboxsdk/maps/renderer/MapRenderer.java | 8 ++----- platform/android/src/map_renderer.cpp | 23 ++++++------------- platform/android/src/map_renderer.hpp | 6 +---- 3 files changed, 10 insertions(+), 27 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 f1c70325a09..fcee5bd1793 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() { - nativeOnPause(); + // Implement if needed } public void onResume() { - nativeOnResume(); + // Implement if needed } public void onStop() { @@ -124,10 +124,6 @@ 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/src/map_renderer.cpp b/platform/android/src/map_renderer.cpp index f7e16b70919..ba6fdc63b00 100644 --- a/platform/android/src/map_renderer.cpp +++ b/platform/android/src/map_renderer.cpp @@ -29,6 +29,7 @@ MapRenderer::MapRenderer(jni::JNIEnv& _env, jni::Object obj, MapRenderer::~MapRenderer() = default; void MapRenderer::reset() { + destroyed = true; // Make sure to destroy the renderer on the GL Thread auto self = ActorRef(*this, mailbox); self.ask(&MapRenderer::resetRenderer).wait(); @@ -88,8 +89,10 @@ void MapRenderer::requestSnapshot(SnapshotCallback callback) { self.invoke( &MapRenderer::scheduleSnapshot, std::make_unique([&, callback=std::move(callback), runloop=util::RunLoop::Get()](PremultipliedImage image) { - runloop->invoke([callback=std::move(callback), image=std::move(image)]() mutable { - callback(std::move(image)); + runloop->invoke([callback=std::move(callback), image=std::move(image), renderer=std::move(this)]() mutable { + if (renderer && !renderer->destroyed) { + callback(std::move(image)); + } }); snapshotCallback.reset(); }) @@ -136,7 +139,7 @@ void MapRenderer::render(JNIEnv&) { renderer->render(*params); // Deliver the snapshot if requested - if (snapshotCallback && !paused) { + if (snapshotCallback) { snapshotCallback->operator()(backend->readFramebuffer()); snapshotCallback.reset(); } @@ -174,14 +177,6 @@ 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; @@ -200,11 +195,7 @@ void MapRenderer::registerNative(jni::JNIEnv& env) { METHOD(&MapRenderer::onSurfaceCreated, "nativeOnSurfaceCreated"), METHOD(&MapRenderer::onSurfaceChanged, - "nativeOnSurfaceChanged"), - METHOD(&MapRenderer::onResume, - "nativeOnResume"), - METHOD(&MapRenderer::onPause, - "nativeOnPause")); + "nativeOnSurfaceChanged")); } 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 5fb5ef1a618..97d2db4a910 100644 --- a/platform/android/src/map_renderer.hpp +++ b/platform/android/src/map_renderer.hpp @@ -98,10 +98,6 @@ class MapRenderer : public Scheduler { void onSurfaceChanged(JNIEnv&, jint width, jint height); - void onResume(JNIEnv&); - - void onPause(JNIEnv&); - private: GenericUniqueWeakObject javaPeer; @@ -124,7 +120,7 @@ class MapRenderer : public Scheduler { std::mutex updateMutex; bool framebufferSizeChanged = false; - std::atomic paused {false}; + std::atomic destroyed {false}; std::unique_ptr snapshotCallback; };