[android] Cherry picks to agua (#10442)

* [android] fix map snapshotter marker example

The reference to the MapSnapshotter needs to be held for the duration of the snapshot, otherwise it might be GC'd

* [android] MapRendererRunnable - avoid weak reference table overflow

Apparently on some devices the weak reference table is limited (numbers around 52000). Even though we don't use that many weak references, when GC is not called for a while they can stack up and a crash will occur before the GC has had the time to clear the references. The C++ peer now holds on to a global ref (strong) which can be obtained to queue the java peer and then release automatically so that the GC can take over after the runnable has been executed.
This commit is contained in:
Pablo Guardiola
2017-11-10 12:56:06 +01:00
committed by GitHub
parent 91dabd01df
commit b5140cb342
4 changed files with 19 additions and 12 deletions

View File

@@ -25,6 +25,8 @@ import timber.log.Timber;
*/
public class MapSnapshotterMarkerActivity extends AppCompatActivity implements MapSnapshotter.SnapshotReadyCallback {
private MapSnapshotter mapSnapshotter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -40,7 +42,7 @@ public class MapSnapshotterMarkerActivity extends AppCompatActivity implements M
Timber.i("Starting snapshot");
MapSnapshotter mapSnapshotter = new MapSnapshotter(
mapSnapshotter = new MapSnapshotter(
getApplicationContext(),
new MapSnapshotter
.Options(Math.min(container.getMeasuredWidth(), 1024), Math.min(container.getMeasuredHeight(), 1024))

View File

@@ -41,15 +41,19 @@ ActorRef<Renderer> MapRenderer::actor() const {
}
void MapRenderer::schedule(std::weak_ptr<Mailbox> scheduled) {
// Create a runnable and schedule it on the gl thread
// Create a runnable
android::UniqueEnv _env = android::AttachEnv();
auto runnable = std::make_unique<MapRendererRunnable>(*_env, std::move(scheduled));
// Obtain ownership of the peer (gets transferred to the MapRenderer on the JVM for later GC)
auto peer = runnable->peer();
// Queue the event on the Java Peer
static auto queueEvent = javaClass.GetMethod<void(
jni::Object<MapRendererRunnable>)>(*_env, "queueEvent");
javaPeer->Call(*_env, queueEvent, runnable->getPeer());
javaPeer->Call(*_env, queueEvent, *peer);
// Release the object as it will be destroyed on GC of the Java Peer
// Release the c++ peer as it will be destroyed on GC of the Java Peer
runnable.release();
}

View File

@@ -8,11 +8,13 @@ namespace android {
MapRendererRunnable::MapRendererRunnable(jni::JNIEnv& env, std::weak_ptr<Mailbox> mailbox_)
: mailbox(std::move(mailbox_)) {
// Create the Java peer
// Create the Java peer and hold on to a global reference
// Not using a weak reference here as this might oerflow
// the weak reference table on some devices
jni::UniqueLocalFrame frame = jni::PushLocalFrame(env, 5);
static auto constructor = javaClass.GetConstructor<jlong>(env);
auto instance = javaClass.New(env, constructor, reinterpret_cast<jlong>(this));
javaPeer = SeizeGenericWeakRef(env, jni::Object<MapRendererRunnable>(jni::NewWeakGlobalRef(env, instance.Get()).release()));
javaPeer = instance.NewGlobalRef(env);
}
MapRendererRunnable::~MapRendererRunnable() = default;
@@ -21,8 +23,8 @@ void MapRendererRunnable::run(jni::JNIEnv&) {
Mailbox::maybeReceive(mailbox);
}
jni::Object<MapRendererRunnable> MapRendererRunnable::getPeer() {
return *javaPeer;
jni::UniqueObject<MapRendererRunnable> MapRendererRunnable::peer() {
return std::move(javaPeer);
}
// Static methods //

View File

@@ -8,8 +8,6 @@
#include <jni/jni.hpp>
#include "jni/generic_global_ref_deleter.hpp"
namespace mbgl {
namespace android {
@@ -39,10 +37,11 @@ public:
void run(jni::JNIEnv&);
jni::Object<MapRendererRunnable> getPeer();
// Transfers ownership of the Peer object to the caller
jni::UniqueObject<MapRendererRunnable> peer();
private:
GenericUniqueWeakObject<MapRendererRunnable> javaPeer;
jni::UniqueObject<MapRendererRunnable> javaPeer;
std::weak_ptr<Mailbox> mailbox;
};