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

[android] Cherry picks to agua #10442

Merged
merged 2 commits into from Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -25,6 +25,8 @@
*/
public class MapSnapshotterMarkerActivity extends AppCompatActivity implements MapSnapshotter.SnapshotReadyCallback {

private MapSnapshotter mapSnapshotter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -40,7 +42,7 @@ public void onGlobalLayout() {

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))
Expand Down
10 changes: 7 additions & 3 deletions platform/android/src/map_renderer.cpp
Expand Up @@ -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();
}

Expand Down
10 changes: 6 additions & 4 deletions platform/android/src/map_renderer_runnable.cpp
Expand Up @@ -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;
Expand All @@ -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 //
Expand Down
7 changes: 3 additions & 4 deletions platform/android/src/map_renderer_runnable.hpp
Expand Up @@ -8,8 +8,6 @@

#include <jni/jni.hpp>

#include "jni/generic_global_ref_deleter.hpp"

namespace mbgl {
namespace android {

Expand Down Expand Up @@ -39,10 +37,11 @@ class MapRendererRunnable {

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;
};

Expand Down