Skip to content

Commit

Permalink
Asynchronous GeometryTile deletion (#2051)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Sylvester <[email protected]>
  • Loading branch information
mwilsnd and TimSylvester authored Feb 28, 2024
1 parent e3cbdb6 commit 4fff8a5
Show file tree
Hide file tree
Showing 80 changed files with 1,180 additions and 334 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ if(MLN_DRAWABLE_RENDERER)
${PROJECT_SOURCE_DIR}/include/mbgl/renderer/layer_tweaker.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/renderer/render_target.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/shader_program_base.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/identity.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/suppress_copies.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/shader_info.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/shaders/gl/shader_program_gl.hpp
Expand Down Expand Up @@ -216,7 +215,6 @@ if(MLN_DRAWABLE_RENDERER)
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/collision_layer_tweaker.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/collision_layer_tweaker.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/shader_program_base.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/identity.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/shaders/gl/shader_program_gl.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/buffer_allocator.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/drawable_gl.cpp
Expand Down Expand Up @@ -403,6 +401,7 @@ list(APPEND INCLUDE_FILES
${PROJECT_SOURCE_DIR}/include/mbgl/util/geo.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/geojson.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/geometry.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/identity.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/ignore.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/image.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/immutable.hpp
Expand All @@ -415,6 +414,7 @@ list(APPEND INCLUDE_FILES
${PROJECT_SOURCE_DIR}/include/mbgl/util/projection.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/range.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/run_loop.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/scoped.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/size.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/string.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/string_indexer.hpp
Expand Down Expand Up @@ -939,6 +939,7 @@ list(APPEND SRC_FILES
${PROJECT_SOURCE_DIR}/src/mbgl/util/http_timeout.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/i18n.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/i18n.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/identity.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/interpolate.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/intersection_tests.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/intersection_tests.hpp
Expand Down Expand Up @@ -1444,4 +1445,4 @@ endif()

add_subdirectory(${PROJECT_SOURCE_DIR}/test)
add_subdirectory(${PROJECT_SOURCE_DIR}/benchmark)
add_subdirectory(${PROJECT_SOURCE_DIR}/render-test)
add_subdirectory(${PROJECT_SOURCE_DIR}/render-test)
16 changes: 8 additions & 8 deletions bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,18 @@ MLN_CORE_SOURCE = [
"src/mbgl/geometry/line_atlas.hpp",
"src/mbgl/gfx/attribute.cpp",
"src/mbgl/gfx/attribute.hpp",
"include/mbgl/gfx/backend.hpp",
"src/mbgl/gfx/color_mode.hpp",
"src/mbgl/gfx/command_encoder.hpp",
"src/mbgl/gfx/cull_face_mode.hpp",
"src/mbgl/gfx/debug_group.hpp",
"src/mbgl/gfx/depth_mode.hpp",
"src/mbgl/gfx/draw_mode.hpp",
"src/mbgl/gfx/draw_scope.hpp",
"src/mbgl/gfx/fill_generator.cpp",
"src/mbgl/gfx/index_buffer.hpp",
"src/mbgl/gfx/index_vector.hpp",
"src/mbgl/gfx/offscreen_texture.hpp",
"src/mbgl/gfx/polyline_generator.cpp",
"src/mbgl/gfx/program.hpp",
"src/mbgl/gfx/render_pass.hpp",
"src/mbgl/gfx/renderbuffer.hpp",
Expand Down Expand Up @@ -598,6 +599,7 @@ MLN_CORE_SOURCE = [
"src/mbgl/util/http_timeout.hpp",
"src/mbgl/util/i18n.cpp",
"src/mbgl/util/i18n.hpp",
"src/mbgl/util/identity.cpp",
"src/mbgl/util/interpolate.cpp",
"src/mbgl/util/intersection_tests.cpp",
"src/mbgl/util/intersection_tests.hpp",
Expand All @@ -618,8 +620,6 @@ MLN_CORE_SOURCE = [
"src/mbgl/util/premultiply.cpp",
"src/mbgl/util/quaternion.cpp",
"src/mbgl/util/quaternion.hpp",
"src/mbgl/gfx/polyline_generator.cpp",
"src/mbgl/gfx/fill_generator.cpp",
"src/mbgl/util/rapidjson.cpp",
"src/mbgl/util/rapidjson.hpp",
"src/mbgl/util/rect.hpp",
Expand Down Expand Up @@ -661,8 +661,11 @@ MLN_CORE_HEADERS = [
"include/mbgl/actor/message.hpp",
"include/mbgl/actor/scheduler.hpp",
"include/mbgl/annotation/annotation.hpp",
"include/mbgl/gfx/backend.hpp",
"include/mbgl/gfx/backend_scope.hpp",
"include/mbgl/gfx/fill_generator.hpp",
"include/mbgl/gfx/gfx_types.hpp",
"include/mbgl/gfx/polyline_generator.hpp",
"include/mbgl/gfx/renderable.hpp",
"include/mbgl/gfx/renderer_backend.hpp",
"include/mbgl/gfx/rendering_stats.hpp",
Expand Down Expand Up @@ -820,6 +823,7 @@ MLN_CORE_HEADERS = [
"include/mbgl/util/geo.hpp",
"include/mbgl/util/geojson.hpp",
"include/mbgl/util/geometry.hpp",
"include/mbgl/util/identity.hpp",
"include/mbgl/util/ignore.hpp",
"include/mbgl/util/image.hpp",
"include/mbgl/util/immutable.hpp",
Expand All @@ -829,12 +833,11 @@ MLN_CORE_HEADERS = [
"include/mbgl/util/monotonic_timer.hpp",
"include/mbgl/util/noncopyable.hpp",
"include/mbgl/util/platform.hpp",
"include/mbgl/gfx/polyline_generator.hpp",
"include/mbgl/gfx/fill_generator.hpp",
"include/mbgl/util/premultiply.hpp",
"include/mbgl/util/projection.hpp",
"include/mbgl/util/range.hpp",
"include/mbgl/util/run_loop.hpp",
"include/mbgl/util/scoped.hpp",
"include/mbgl/util/size.hpp",
"include/mbgl/util/string.hpp",
"include/mbgl/util/string_indexer.hpp",
Expand Down Expand Up @@ -905,7 +908,6 @@ MLN_OPENGL_SOURCE = [
]

MLN_OPENGL_HEADERS = [
"include/mbgl/gfx/backend.hpp",
"include/mbgl/gl/renderable_resource.hpp",
"include/mbgl/gl/renderer_backend.hpp",
"include/mbgl/layermanager/location_indicator_layer_factory.hpp",
Expand Down Expand Up @@ -957,7 +959,6 @@ MLN_DRAWABLES_SOURCE = [
"src/mbgl/renderer/layers/collision_layer_tweaker.cpp",
"src/mbgl/renderer/layers/collision_layer_tweaker.hpp",
"src/mbgl/shaders/shader_program_base.cpp",
"src/mbgl/util/identity.cpp",
"src/mbgl/style/layers/custom_drawable_layer.cpp",
"src/mbgl/layermanager/custom_drawable_layer_factory.cpp",
"src/mbgl/style/layers/custom_drawable_layer_impl.cpp",
Expand Down Expand Up @@ -999,7 +1000,6 @@ MLN_DRAWABLES_HEADERS = [
"include/mbgl/shaders/shader_defines.hpp",
"include/mbgl/shaders/shader_program_base.hpp",
"include/mbgl/shaders/symbol_layer_ubo.hpp",
"include/mbgl/util/identity.hpp",
"include/mbgl/util/suppress_copies.hpp",
"include/mbgl/style/layers/custom_drawable_layer.hpp",
"include/mbgl/layermanager/custom_drawable_layer_factory.hpp",
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/actor/actor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Actor {
ActorRef<std::decay_t<Object>> self() { return parent.self(); }

private:
std::shared_ptr<Scheduler> retainer;
const std::shared_ptr<Scheduler> retainer;
AspiringActor<Object> parent;
EstablishedActor<Object> target;
};
Expand Down
12 changes: 11 additions & 1 deletion include/mbgl/actor/mailbox.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once

#include <atomic>
#include <functional>
#include <memory>
#include <mutex>
Expand Down Expand Up @@ -28,6 +28,9 @@ class Mailbox : public std::enable_shared_from_this<Mailbox> {
void open(Scheduler& scheduler_);
void close();

// Indicate this mailbox will no longer be checked for messages
void abandon();

bool isOpen() const;

void push(std::unique_ptr<Message>);
Expand All @@ -37,11 +40,18 @@ class Mailbox : public std::enable_shared_from_this<Mailbox> {
static std::function<void()> makeClosure(std::weak_ptr<Mailbox>);

private:
enum class State : uint32_t {
Idle = 0,
Processing,
Abandoned
};

mapbox::base::WeakPtr<Scheduler> weakScheduler;

std::recursive_mutex receivingMutex;
std::mutex pushingMutex;

std::atomic<State> state{State::Idle};
bool closed{false};

std::mutex queueMutex;
Expand Down
18 changes: 16 additions & 2 deletions include/mbgl/actor/scheduler.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <mbgl/util/chrono.hpp>

#include <mapbox/std/weak.hpp>

#include <functional>
Expand Down Expand Up @@ -36,9 +38,12 @@ class Scheduler {
virtual ~Scheduler() = default;

/// Enqueues a function for execution.
virtual void schedule(std::function<void()>) = 0;
virtual void schedule(std::function<void()>&&) = 0;
/// Makes a weak pointer to this Scheduler.
virtual mapbox::base::WeakPtr<Scheduler> makeWeakPtr() = 0;
/// Enqueues a function for execution on the render thread.
virtual void runOnRenderThread(std::function<void()>&&){};
virtual void runRenderJobs() {}

/// Returns a closure wrapping the given one.
///
Expand All @@ -62,6 +67,11 @@ class Scheduler {
scheduleAndReplyValue(task, reply, GetCurrent()->makeWeakPtr());
}

/// Wait until there's nothing pending or in process
/// Must not be called from a task provided to this scheduler.
/// @param timeout Time to wait, or zero to wait forever.
virtual std::size_t waitForEmpty(Milliseconds timeout = Milliseconds{0}) = 0;

/// Set/Get the current Scheduler for this thread
static Scheduler* GetCurrent();
static void SetCurrent(Scheduler*);
Expand All @@ -84,6 +94,9 @@ class Scheduler {
/// on the same thread-unsafe object.
[[nodiscard]] static std::shared_ptr<Scheduler> GetSequenced();

/// Set a function to be called when an exception occurs on a thread controlled by the scheduler
void setExceptionHandler(std::function<void(const std::exception_ptr)> handler_) { handler = std::move(handler_); }

protected:
template <typename TaskFn, typename ReplyFn>
void scheduleAndReplyValue(const TaskFn& task,
Expand All @@ -97,9 +110,10 @@ class Scheduler {
};
replyScheduler->schedule(std::move(scheduledReply));
};

schedule(std::move(scheduled));
}

std::function<void(const std::exception_ptr)> handler;
};

} // namespace mbgl
6 changes: 5 additions & 1 deletion include/mbgl/gfx/context.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <mbgl/actor/scheduler.hpp>
#include <mbgl/gfx/backend.hpp>
#include <mbgl/gfx/command_encoder.hpp>
#include <mbgl/gfx/draw_scope.hpp>
Expand Down Expand Up @@ -53,7 +54,8 @@ using VertexAttributeArrayPtr = std::shared_ptr<VertexAttributeArray>;
class Context {
protected:
Context(uint32_t maximumVertexBindingCount_)
: maximumVertexBindingCount(maximumVertexBindingCount_) {}
: maximumVertexBindingCount(maximumVertexBindingCount_),
backgroundScheduler(Scheduler::GetBackground()) {}

public:
static constexpr const uint32_t minimumRequiredVertexBindingCount = 8;
Expand Down Expand Up @@ -161,6 +163,8 @@ class Context {
virtual std::unique_ptr<DrawScopeResource> createDrawScopeResource() = 0;

gfx::RenderingStats stats;

std::shared_ptr<Scheduler> backgroundScheduler;
};

} // namespace gfx
Expand Down
4 changes: 3 additions & 1 deletion include/mbgl/util/run_loop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ class RunLoop : public Scheduler, private util::noncopyable {
return std::make_unique<WorkRequest>(task);
}

void schedule(std::function<void()> fn) override { invoke(std::move(fn)); }
void schedule(std::function<void()>&& fn) override { invoke(std::move(fn)); }
::mapbox::base::WeakPtr<Scheduler> makeWeakPtr() override { return weakFactory.makeWeakPtr(); }

std::size_t waitForEmpty(Milliseconds timeout) override;

class Impl;

private:
Expand Down
21 changes: 21 additions & 0 deletions include/mbgl/util/scoped.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

namespace mbgl {

/// Run a lambda on scope exit
template <typename Func>
struct Scoped {
Scoped(Func&& fn)
: cb(std::move(fn)){};
~Scoped() { cb(); }

Scoped(const Scoped&) = delete;
Scoped(Scoped&&) noexcept = delete;
Scoped& operator=(const Scoped&) = delete;
Scoped& operator=(Scoped&&) noexcept = delete;

private:
Func cb;
};

} // namespace mbgl
10 changes: 8 additions & 2 deletions include/mbgl/util/string.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once

#include <string>
#include <cstdint>
#include <cstdlib>
#include <type_traits>
#include <exception>
#include <sstream>
#include <string>
#include <thread>
#include <type_traits>

// Polyfill needed by Qt when building for Android with GCC
#if defined(__ANDROID__) && defined(__GLIBCXX__)
Expand Down Expand Up @@ -76,6 +78,10 @@ inline std::string toString(long double t, bool decimal = false) {
return toString(static_cast<double>(t), decimal);
}

inline std::string toString(std::thread::id threadId) {
return (std::ostringstream() << threadId).str();
}

std::string toString(const std::exception_ptr &);

template <class T>
Expand Down
19 changes: 18 additions & 1 deletion platform/android/MapboxGLAndroidSDK/src/cpp/map_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ActorRef<Renderer> MapRenderer::actor() const {
return *rendererRef;
}

void MapRenderer::schedule(std::function<void()> scheduled) {
void MapRenderer::schedule(std::function<void()>&& scheduled) {
try {
// Create a runnable
android::UniqueEnv _env = android::AttachEnv();
Expand All @@ -84,6 +84,23 @@ void MapRenderer::schedule(std::function<void()> scheduled) {
}
}

std::size_t MapRenderer::waitForEmpty(Milliseconds timeout) {
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");
if (auto weakReference = javaPeer.get(*_env)) {
return weakReference.Call(*_env, waitForEmpty, static_cast<int64_t>(timeout.count()));
}
// 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;
}
}

void MapRenderer::requestRender() {
try {
android::UniqueEnv _env = android::AttachEnv();
Expand Down
6 changes: 5 additions & 1 deletion platform/android/MapboxGLAndroidSDK/src/cpp/map_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ class MapRenderer : public Scheduler {

// 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(std::function<void()>&& scheduled) override;
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 requestRender();

// Snapshot - requires a RunLoop on the calling thread
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ void queueEvent(MapRendererRunnable runnable) {
this.queueEvent((Runnable) runnable);
}

/// Wait indefinitely for the queue to become empty
public void waitForEmpty() {
waitForEmpty(0);
}

private native void nativeInitialize(MapRenderer self,
float pixelRatio,
String localIdeographFontFamily);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public interface MapRendererScheduler {
@Keep
void queueEvent(Runnable runnable);

@Keep
void waitForEmpty();

@Keep
long waitForEmpty(long timeoutMillis);
}
Loading

0 comments on commit 4fff8a5

Please sign in to comment.