Add TaggedScheduler, couple lifetime of tasks and orchestrator (#2398)

This commit is contained in:
mwilsnd
2024-06-28 17:20:03 -04:00
committed by GitHub
parent b2fe5021b8
commit c4c75452b6
99 changed files with 759 additions and 489 deletions

View File

@@ -28,7 +28,7 @@ private:
AndroidRendererBackend& backend;
};
AndroidRendererBackend::AndroidRendererBackend()
AndroidRendererBackend::AndroidRendererBackend(const TaggedScheduler& threadPool)
: gl::RendererBackend(gfx::ContextMode::Unique),
mbgl::gfx::Renderable({64, 64}, std::make_unique<AndroidGLRenderableResource>(*this)) {}

View File

@@ -8,7 +8,7 @@ namespace android {
class AndroidRendererBackend : public gl::RendererBackend, public mbgl::gfx::Renderable {
public:
AndroidRendererBackend();
AndroidRendererBackend(const TaggedScheduler& threadPool);
~AndroidRendererBackend() override;
void updateViewPort();

View File

@@ -82,6 +82,10 @@ void AndroidRendererFrontend::update(std::shared_ptr<UpdateParameters> params) {
updateAsyncTask->send();
}
const TaggedScheduler& AndroidRendererFrontend::getThreadPool() const {
return mapRenderer.getThreadPool();
}
void AndroidRendererFrontend::reduceMemoryUse() {
mapRenderer.actor().invoke(&Renderer::reduceMemoryUse);
}

View File

@@ -37,6 +37,8 @@ public:
void update(std::shared_ptr<UpdateParameters>) override;
const TaggedScheduler& getThreadPool() const override;
// Feature querying
std::vector<Feature> queryRenderedFeatures(const ScreenCoordinate&, const RenderedQueryOptions&) const;
std::vector<Feature> queryRenderedFeatures(const ScreenBox&, const RenderedQueryOptions&) const;

View File

@@ -22,6 +22,7 @@ MapRenderer::MapRenderer(jni::JNIEnv& _env,
pixelRatio(pixelRatio_),
localIdeographFontFamily(localIdeographFontFamily_ ? jni::Make<std::string>(_env, localIdeographFontFamily_)
: std::optional<std::string>{}),
threadPool(Scheduler::GetBackground(), {}),
mailboxData(this) {}
MapRenderer::MailboxData::MailboxData(Scheduler* scheduler_)
@@ -84,20 +85,17 @@ void MapRenderer::schedule(std::function<void()>&& scheduled) {
}
}
std::size_t MapRenderer::waitForEmpty(Milliseconds timeout) {
void MapRenderer::waitForEmpty([[maybe_unused]] const util::SimpleIdentity tag) {
try {
android::UniqueEnv _env = android::AttachEnv();
static auto& javaClass = jni::Class<MapRenderer>::Singleton(*_env);
static auto waitForEmpty = javaClass.GetMethod<jni::jint(jni::jlong)>(*_env, "waitForEmpty");
static auto waitForEmpty = javaClass.GetMethod<void()>(*_env, "waitForEmpty");
if (auto weakReference = javaPeer.get(*_env)) {
return weakReference.Call(*_env, waitForEmpty, static_cast<int64_t>(timeout.count()));
return weakReference.Call(*_env, waitForEmpty);
}
// If the peer is already cleaned up, there's nothing to wait for
return 0;
} catch (...) {
Log::Error(Event::Android, "MapRenderer::waitForEmpty failed");
jni::ThrowJavaError(*android::AttachEnv(), std::current_exception());
return 0;
}
}
@@ -221,7 +219,7 @@ void MapRenderer::onSurfaceCreated(JNIEnv&) {
backend.reset();
// Create the new backend and renderer
backend = std::make_unique<AndroidRendererBackend>();
backend = std::make_unique<AndroidRendererBackend>(threadPool);
renderer = std::make_unique<Renderer>(*backend, pixelRatio, localIdeographFontFamily);
rendererRef = std::make_unique<ActorRef<Renderer>>(*renderer, mailboxData.getMailbox());

View File

@@ -67,11 +67,12 @@ public:
// From Scheduler. Schedules by using callbacks to the
// JVM to process the mailbox on the right thread.
void schedule(std::function<void()>&& scheduled) override;
void schedule(const util::SimpleIdentity, std::function<void()>&& fn) override { schedule(std::move(fn)); };
mapbox::base::WeakPtr<Scheduler> makeWeakPtr() override { return weakFactory.makeWeakPtr(); }
// Wait for the queue to be empty
// A timeout of zero results in an unbounded wait
std::size_t waitForEmpty(Milliseconds timeout) override;
void waitForEmpty(const util::SimpleIdentity tag) override;
void requestRender();
@@ -79,6 +80,9 @@ public:
using SnapshotCallback = std::function<void(PremultipliedImage)>;
void requestSnapshot(SnapshotCallback);
AndroidRendererBackend& getRendererBackend() const { return *backend; }
const TaggedScheduler& getThreadPool() const { return threadPool; }
protected:
// Called from the GL Thread //
@@ -119,7 +123,7 @@ private:
float pixelRatio;
std::optional<std::string> localIdeographFontFamily;
std::shared_ptr<ThreadPool> threadPool;
TaggedScheduler threadPool;
const MailboxData mailboxData;
std::mutex initialisationMutex;