From b817e0f58d6b62a6d8fc445be0e1bfaf1ef29d03 Mon Sep 17 00:00:00 2001 From: Alexander Shalamov Date: Thu, 19 Mar 2020 23:15:46 +0200 Subject: Use custom deleter for snapshotter --- .../android/src/snapshotter/map_snapshotter.cpp | 46 ++++++++++++---------- .../android/src/snapshotter/map_snapshotter.hpp | 14 +++++-- 2 files changed, 35 insertions(+), 25 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 #include +#include + #include "../attach_env.hpp" #include "map_snapshot.hpp" namespace mbgl { namespace android { +MapSnapshotter::DeleteOnThread::DeleteOnThread() = default; +MapSnapshotter::DeleteOnThread::DeleteOnThread(mapbox::base::WeakPtr 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(p, DeleteOnThread(weakScheduler))] { + (void)ptr; + }); + } else { + delete p; + } +} + MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, const jni::Object& _obj, const jni::Object& _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(width), static_cast(height) }; showLogo = _showLogo; + // Create the core snapshotter - snapshotter = std::make_unique( - size, - pixelRatio, - mbgl::android::FileSource::getSharedResourceOptions(_env, _jFileSource), - *this, - _localIdeographFontFamily ? jni::Make(_env, _localIdeographFontFamily) : optional{}); + snapshotter = std::unique_ptr(new mbgl::MapSnapshotter( + size, + pixelRatio, + mbgl::android::FileSource::getSharedResourceOptions(_env, _jFileSource), + *this, + _localIdeographFontFamily ? jni::Make(_env, _localIdeographFontFamily) : optional{}), + 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 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); diff --git a/platform/android/src/snapshotter/map_snapshotter.hpp b/platform/android/src/snapshotter/map_snapshotter.hpp index 1da4f8275b..e6f543649f 100644 --- a/platform/android/src/snapshotter/map_snapshotter.hpp +++ b/platform/android/src/snapshotter/map_snapshotter.hpp @@ -3,9 +3,7 @@ #include #include #include - #include -#include #include "../file_source.hpp" #include "../geometry/lat_lng_bounds.hpp" @@ -64,6 +62,15 @@ public: void onDidFinishLoadingStyle() override; void onStyleImageMissing(const std::string&) override; +private: + struct DeleteOnThread { + DeleteOnThread(); + explicit DeleteOnThread(mapbox::base::WeakPtr); + void operator()(mbgl::MapSnapshotter* p) const; + private: + mapbox::base::WeakPtr weakScheduler; + }; + private: MBGL_STORE_THREAD(tid); @@ -77,8 +84,7 @@ private: void activateFilesource(JNIEnv&); void deactivateFilesource(JNIEnv&); bool activatedFilesource = false; - std::unique_ptr snapshotter; - mapbox::base::WeakPtr weakScheduler; + std::unique_ptr snapshotter; }; } // namespace android -- cgit v1.2.1