summaryrefslogtreecommitdiff
path: root/platform/android/src/snapshotter/map_snapshotter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'platform/android/src/snapshotter/map_snapshotter.cpp')
-rw-r--r--platform/android/src/snapshotter/map_snapshotter.cpp46
1 files changed, 25 insertions, 21 deletions
diff --git a/platform/android/src/snapshotter/map_snapshotter.cpp b/platform/android/src/snapshotter/map_snapshotter.cpp
index 115c455a94..844d6481da 100644
--- a/platform/android/src/snapshotter/map_snapshotter.cpp
+++ b/platform/android/src/snapshotter/map_snapshotter.cpp
@@ -6,12 +6,28 @@
#include <mbgl/util/string.hpp>
#include <mbgl/actor/scheduler.hpp>
+#include <mapbox/weak.hpp>
+
#include "../attach_env.hpp"
#include "map_snapshot.hpp"
namespace mbgl {
namespace android {
+MapSnapshotter::DeleteOnThread::DeleteOnThread() = default;
+MapSnapshotter::DeleteOnThread::DeleteOnThread(mapbox::base::WeakPtr<mbgl::Scheduler> weakScheduler_) : weakScheduler(weakScheduler_) {}
+
+void MapSnapshotter::DeleteOnThread::operator()(mbgl::MapSnapshotter* p) const {
+ auto guard = weakScheduler.lock();
+ if (weakScheduler && weakScheduler.get() != mbgl::Scheduler::GetCurrent()) {
+ weakScheduler->schedule([ptr = std::shared_ptr<mbgl::MapSnapshotter>(p, DeleteOnThread(weakScheduler))] {
+ (void)ptr;
+ });
+ } else {
+ delete p;
+ }
+}
+
MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env,
const jni::Object<MapSnapshotter>& _obj,
const jni::Object<FileSource>& _jFileSource,
@@ -33,19 +49,19 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env,
return;
}
- weakScheduler = mbgl::Scheduler::GetCurrent()->makeWeakPtr();
-
jFileSource = FileSource::getNativePeer(_env, _jFileSource);
auto size = mbgl::Size { static_cast<uint32_t>(width), static_cast<uint32_t>(height) };
showLogo = _showLogo;
+
// Create the core snapshotter
- snapshotter = std::make_unique<mbgl::MapSnapshotter>(
- size,
- pixelRatio,
- mbgl::android::FileSource::getSharedResourceOptions(_env, _jFileSource),
- *this,
- _localIdeographFontFamily ? jni::Make<std::string>(_env, _localIdeographFontFamily) : optional<std::string>{});
+ snapshotter = std::unique_ptr<mbgl::MapSnapshotter, DeleteOnThread>(new mbgl::MapSnapshotter(
+ size,
+ pixelRatio,
+ mbgl::android::FileSource::getSharedResourceOptions(_env, _jFileSource),
+ *this,
+ _localIdeographFontFamily ? jni::Make<std::string>(_env, _localIdeographFontFamily) : optional<std::string>{}),
+ DeleteOnThread(mbgl::Scheduler::GetCurrent()->makeWeakPtr()));
if (position) {
snapshotter->setCameraOptions(CameraPosition::getCameraOptions(_env, position, pixelRatio));
@@ -62,19 +78,7 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env,
}
}
-MapSnapshotter::~MapSnapshotter() {
- auto guard = weakScheduler.lock();
- if (weakScheduler && weakScheduler.get() != mbgl::Scheduler::GetCurrent()) {
- snapshotter->cancel();
- std::shared_ptr<mbgl::MapSnapshotter> shared = std::move(snapshotter);
- weakScheduler->schedule([s = std::move(shared)] {
- (void)s;
- });
- } else {
- snapshotter.reset();
- }
- vm = nullptr;
-}
+MapSnapshotter::~MapSnapshotter() = default;
void MapSnapshotter::start(JNIEnv& env) {
MBGL_VERIFY_THREAD(tid);