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

Commit

Permalink
Release snapshotter pointer to deleting lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshalamov authored and kevin committed Mar 20, 2020
1 parent 12a060b commit d0d560e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
36 changes: 16 additions & 20 deletions platform/android/src/snapshotter/map_snapshotter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "map_snapshotter.hpp"

#include <mapbox/weak.hpp>
#include <mbgl/actor/scheduler.hpp>
#include <mbgl/renderer/renderer.hpp>
#include <mbgl/style/style.hpp>
Expand All @@ -13,20 +12,6 @@
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,
Expand All @@ -48,20 +33,20 @@ 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::unique_ptr<mbgl::MapSnapshotter, DeleteOnThread>(
new mbgl::MapSnapshotter(size,
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>{}),
DeleteOnThread(mbgl::Scheduler::GetCurrent()->makeWeakPtr()));
: optional<std::string>{});

if (position) {
snapshotter->setCameraOptions(CameraPosition::getCameraOptions(_env, position, pixelRatio));
Expand All @@ -78,7 +63,18 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env,
}
}

MapSnapshotter::~MapSnapshotter() = default;
MapSnapshotter::~MapSnapshotter() {
auto guard = weakScheduler.lock();
if (weakScheduler && weakScheduler.get() != mbgl::Scheduler::GetCurrent()) {
snapshotter->cancel();
weakScheduler->schedule([ptr = snapshotter.release()]() mutable {
if (ptr) {
delete ptr;
ptr = nullptr;
}
});
}
}

void MapSnapshotter::start(JNIEnv& env) {
MBGL_VERIFY_THREAD(tid);
Expand Down
15 changes: 4 additions & 11 deletions platform/android/src/snapshotter/map_snapshotter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <jni/jni.hpp>
#include <mbgl/map/map_snapshotter.hpp>
#include <mbgl/util/util.hpp>
#include <mapbox/weak.hpp>

#include <memory>

#include "../file_source.hpp"
Expand Down Expand Up @@ -62,16 +64,6 @@ class MapSnapshotter final : public mbgl::MapSnapshotterObserver {
void onDidFinishLoadingStyle() override;
void onStyleImageMissing(const std::string&) override;

private:
struct DeleteOnThread {
DeleteOnThread();
explicit DeleteOnThread(mapbox::base::WeakPtr<mbgl::Scheduler>);
void operator()(mbgl::MapSnapshotter* p) const;

private:
mapbox::base::WeakPtr<mbgl::Scheduler> weakScheduler;
};

private:
MBGL_STORE_THREAD(tid);

Expand All @@ -85,7 +77,8 @@ class MapSnapshotter final : public mbgl::MapSnapshotterObserver {
void activateFilesource(JNIEnv&);
void deactivateFilesource(JNIEnv&);
bool activatedFilesource = false;
std::unique_ptr<mbgl::MapSnapshotter, DeleteOnThread> snapshotter;
mapbox::base::WeakPtr<mbgl::Scheduler> weakScheduler;
std::unique_ptr<mbgl::MapSnapshotter> snapshotter;
};

} // namespace android
Expand Down

0 comments on commit d0d560e

Please sign in to comment.