Skip to content

Commit

Permalink
More changes, render thread task management
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilsnd committed Jun 5, 2024
1 parent 1f2ebc5 commit a2592f1
Show file tree
Hide file tree
Showing 25 changed files with 87 additions and 62 deletions.
14 changes: 5 additions & 9 deletions include/mbgl/actor/actor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,22 @@ class Actor {
template <class... Args>
Actor(Scheduler& scheduler, Args&&... args)
: target(scheduler, parent, std::forward<Args>(args)...) {}

template <class... Args>
Actor(const TaggedScheduler& scheduler, Args&&... args)
: retainer(scheduler.get()),
target(scheduler, parent, std::forward<Args>(args)...) {}

template <class... Args>
Actor(std::shared_ptr<Scheduler> scheduler, Args&&... args)
: retainer(std::move(scheduler)),
target(*retainer, parent, std::forward<Args>(args)...) {}

template <class... Args>
Actor(TaggedScheduler& scheduler, Args&&... args)
: schedulerTag(scheduler.tag()),
retainer(scheduler.get()),
target(*retainer, parent, std::forward<Args>(args)...) {}



Actor(const Actor&) = delete;

ActorRef<std::decay_t<Object>> self() { return parent.self(); }

private:
const void* schedulerTag = nullptr;
const std::shared_ptr<Scheduler> retainer;
AspiringActor<Object> parent;
EstablishedActor<Object> target;
Expand Down
18 changes: 17 additions & 1 deletion include/mbgl/actor/established_actor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,18 @@ class EstablishedActor {
class... Args,
typename std::enable_if_t<std::is_constructible_v<U, Args...> ||
std::is_constructible_v<U, ActorRef<U>, Args...>>* = nullptr>
EstablishedActor(Scheduler& scheduler, AspiringActor<Object>& parent_, Args&&... args)
EstablishedActor(Scheduler& scheduler, AspiringActor<Object>& parent_, Args&&... args)
: parent(parent_) {
emplaceObject(std::forward<Args>(args)...);
parent.mailbox->open(scheduler);
}

// Construct the Object from a parameter pack `args` (i.e. `Object(args...)`)
template <typename U = Object,
class... Args,
typename std::enable_if_t<std::is_constructible_v<U, Args...> ||
std::is_constructible_v<U, ActorRef<U>, Args...>>* = nullptr>
EstablishedActor(const TaggedScheduler& scheduler, AspiringActor<Object>& parent_, Args&&... args)
: parent(parent_) {
emplaceObject(std::forward<Args>(args)...);
parent.mailbox->open(scheduler);
Expand All @@ -48,6 +59,11 @@ class EstablishedActor {
parent.mailbox->open(scheduler);
}

template <class ArgsTuple, std::size_t ArgCount = std::tuple_size<std::decay_t<ArgsTuple>>::value>
EstablishedActor(const TaggedScheduler& scheduler, AspiringActor<Object>& parent_, ArgsTuple&& args) {
EstablishedActor(*scheduler.get(), parent_, std::forward<ArgsTuple>(args));
}

EstablishedActor(const EstablishedActor&) = delete;

~EstablishedActor() {
Expand Down
6 changes: 5 additions & 1 deletion include/mbgl/actor/mailbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <queue>

#include <mapbox/std/weak.hpp>
#include <mbgl/actor/scheduler.hpp>

namespace mbgl {

Expand All @@ -21,11 +22,13 @@ class Mailbox : public std::enable_shared_from_this<Mailbox> {
Mailbox();

Mailbox(Scheduler&);
Mailbox(const TaggedScheduler&);

/// Attach the given scheduler to this mailbox and begin processing messages
/// sent to it. The mailbox must be a "holding" mailbox, as created by the
/// default constructor Mailbox().
void open(Scheduler& scheduler_);
void open(const TaggedScheduler& scheduler_);
void open(Scheduler&);
void close();

// Indicate this mailbox will no longer be checked for messages
Expand All @@ -46,6 +49,7 @@ class Mailbox : public std::enable_shared_from_this<Mailbox> {
Abandoned
};

const void* schedulerTag{nullptr};
mapbox::base::WeakPtr<Scheduler> weakScheduler;

std::recursive_mutex receivingMutex;
Expand Down
3 changes: 2 additions & 1 deletion include/mbgl/actor/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Scheduler {
virtual void runOnRenderThread(const void*, std::function<void()>&&) {}
/// Run render thread jobs for the given tag address
/// @param closeQueue Runs all render jobs and then removes the internal queue.
virtual void runRenderJobs(const void*, bool closeQueue = false) {}
virtual void runRenderJobs(const void*, [[maybe_unused]] bool closeQueue = false) {}
/// Returns a closure wrapping the given one.
///
/// When the returned closure is invoked for the first time, it schedules
Expand Down Expand Up @@ -127,6 +127,7 @@ class TaggedScheduler {
TaggedScheduler(std::shared_ptr<Scheduler> scheduler_, const void* tagAddr_)
: scheduler(std::move(scheduler_)),
tagAddr(tagAddr_) {}
TaggedScheduler(const TaggedScheduler&) = default;

/// @brief Get the wrapped scheduler
/// @return
Expand Down
5 changes: 1 addition & 4 deletions include/mbgl/gfx/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ using VertexAttributeArrayPtr = std::shared_ptr<VertexAttributeArray>;
class Context {
protected:
Context(uint32_t maximumVertexBindingCount_)
: maximumVertexBindingCount(maximumVertexBindingCount_),
backgroundScheduler(Scheduler::GetBackground()) {}
: maximumVertexBindingCount(maximumVertexBindingCount_) {}

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

gfx::RenderingStats stats;

std::shared_ptr<Scheduler> backgroundScheduler;
};

} // namespace gfx
Expand Down
8 changes: 6 additions & 2 deletions include/mbgl/gfx/renderer_backend.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <mbgl/actor/scheduler.hpp>
#include <mbgl/util/util.hpp>

#include <memory>
Expand All @@ -25,13 +26,16 @@ enum class ContextMode : bool {

class RendererBackend {
protected:
explicit RendererBackend(ContextMode, mbgl::Map*);
explicit RendererBackend(ContextMode);

public:
virtual ~RendererBackend();
RendererBackend(const RendererBackend&) = delete;
RendererBackend& operator=(const RendererBackend&) = delete;

// Return the background thread pool assigned to this backend
TaggedScheduler& getThreadPool() noexcept { return threadPool; }

/// Returns the device's context.
Context& getContext();

Expand Down Expand Up @@ -70,8 +74,8 @@ class RendererBackend {
protected:
std::unique_ptr<Context> context;
const ContextMode contextMode;
const mbgl::Map* owner;
std::once_flag initialized;
TaggedScheduler threadPool;

friend class BackendScope;
};
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/gl/renderer_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ using FramebufferID = uint32_t;

class RendererBackend : public gfx::RendererBackend {
public:
RendererBackend(gfx::ContextMode, mbgl::Map*);
RendererBackend(gfx::ContextMode);
~RendererBackend() override;

/// Called prior to rendering to update the internally assumed OpenGL state.
Expand Down
3 changes: 1 addition & 2 deletions include/mbgl/mtl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ using UniqueVertexBufferResource = std::unique_ptr<VertexBufferResource>;

class Context final : public gfx::Context {
public:
Context(RendererBackend&, TaggedScheduler);
Context(RendererBackend&);
~Context() noexcept override;
Context(const Context&) = delete;
Context& operator=(const Context& other) = delete;
Expand Down Expand Up @@ -159,7 +159,6 @@ class Context final : public gfx::Context {

private:
RendererBackend& backend;
TaggedScheduler& scheduler;
bool cleanupOnDestruction = true;

std::optional<BufferResource> emptyBuffer;
Expand Down
16 changes: 13 additions & 3 deletions src/mbgl/actor/mailbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ Mailbox::Mailbox() = default;
Mailbox::Mailbox(Scheduler& scheduler_)
: weakScheduler(scheduler_.makeWeakPtr()) {}

Mailbox::Mailbox(const TaggedScheduler& scheduler_)
: schedulerTag(scheduler_.tag()),
weakScheduler(scheduler_.get()->makeWeakPtr()) {}

void Mailbox::open(const TaggedScheduler& scheduler_) {
assert(!weakScheduler);
schedulerTag = scheduler_.tag();
return open(*scheduler_.get());
}

void Mailbox::open(Scheduler& scheduler_) {
assert(!weakScheduler);

Expand All @@ -28,7 +38,7 @@ void Mailbox::open(Scheduler& scheduler_) {

if (!queue.empty()) {
auto guard = weakScheduler.lock();
if (weakScheduler) weakScheduler->schedule(makeClosure(shared_from_this()));
if (weakScheduler) weakScheduler->schedule(schedulerTag, makeClosure(shared_from_this()));
}
}

Expand Down Expand Up @@ -86,7 +96,7 @@ void Mailbox::push(std::unique_ptr<Message> message) {
queue.push(std::move(message));
auto guard = weakScheduler.lock();
if (wasEmpty && weakScheduler) {
weakScheduler->schedule(makeClosure(shared_from_this()));
weakScheduler->schedule(schedulerTag, makeClosure(shared_from_this()));
}
}

Expand Down Expand Up @@ -127,7 +137,7 @@ void Mailbox::receive() {
(*message)();

if (!wasEmpty) {
weakScheduler->schedule(makeClosure(shared_from_this()));
weakScheduler->schedule(schedulerTag, makeClosure(shared_from_this()));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/gfx/renderer_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace mbgl {
namespace gfx {

RendererBackend::RendererBackend(const ContextMode contextMode_, mbgl::Map* map)
RendererBackend::RendererBackend(const ContextMode contextMode_)
: contextMode(contextMode_),
owner(map) {}
threadPool(Scheduler::GetBackground(), static_cast<const void*>(this)) {}
RendererBackend::~RendererBackend() = default;

gfx::Context& RendererBackend::getContext() {
Expand Down
9 changes: 4 additions & 5 deletions src/mbgl/gl/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,17 @@ GLint getMaxVertexAttribs() {
}
} // namespace

Context::Context(RendererBackend& backend_, TaggedScheduler scheduler_)
Context::Context(RendererBackend& backend_)
: gfx::Context(/*maximumVertexBindingCount=*/getMaxVertexAttribs()),
backend(backend_),
scheduler(std::move(scheduler_)) {
backend(backend_) {
#if MLN_DRAWABLE_RENDERER
uboAllocator = std::make_unique<gl::UniformBufferAllocator>();
#endif
}

Context::~Context() noexcept {
if (cleanupOnDestruction) {
scheduler.runRenderJobs(true /* closeQueue */);
backend.getThreadPool().runRenderJobs(true /* closeQueue */);

reset();
#if !defined(NDEBUG)
Expand All @@ -94,7 +93,7 @@ Context::~Context() noexcept {
}

void Context::beginFrame() {
scheduler.runRenderJobs();
backend.getThreadPool().runRenderJobs();

#if MLN_DRAWABLE_RENDERER
frameInFlightFence = std::make_shared<gl::Fence>();
Expand Down
3 changes: 1 addition & 2 deletions src/mbgl/gl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Debugging;

class Context final : public gfx::Context {
public:
Context(RendererBackend&, TaggedScheduler);
Context(RendererBackend&);
~Context() noexcept override;
Context(const Context&) = delete;
Context& operator=(const Context& other) = delete;
Expand Down Expand Up @@ -153,7 +153,6 @@ class Context final : public gfx::Context {

private:
RendererBackend& backend;
TaggedScheduler scheduler;
bool cleanupOnDestruction = true;

std::unique_ptr<extension::Debugging> debugging;
Expand Down
7 changes: 3 additions & 4 deletions src/mbgl/gl/renderer_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
namespace mbgl {
namespace gl {

RendererBackend::RendererBackend(const gfx::ContextMode contextMode_, mbgl::Map* map)
: gfx::RendererBackend(contextMode_, map) {}
RendererBackend::RendererBackend(const gfx::ContextMode contextMode_)
: gfx::RendererBackend(contextMode_) {}

std::unique_ptr<gfx::Context> RendererBackend::createContext() {
auto result = std::make_unique<gl::Context>(*this,
TaggedScheduler{Scheduler::GetBackground(), static_cast<const void*>(owner)});
auto result = std::make_unique<gl::Context>(*this); // Tagged background thread pool will be owned by the RendererBackend
result->enableDebugging();
result->initializeExtensions(std::bind(&RendererBackend::getExtensionFunctionPointer, this, std::placeholders::_1));
return result;
Expand Down
9 changes: 4 additions & 5 deletions src/mbgl/mtl/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ namespace mtl {
// 31 for Apple2-8, Mac2, per https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
constexpr uint32_t maximumVertexBindingCount = 31;

Context::Context(RendererBackend& backend_, TaggedScheduler scheduler_)
Context::Context(RendererBackend& backend_)
: gfx::Context(mtl::maximumVertexBindingCount),
backend(backend_),
scheduler(std::move(scheduler_)) {}
backend(backend_) {}

Context::~Context() noexcept {
if (cleanupOnDestruction) {
scheduler.runRenderJobs(true /* closeQueue */);
backend.getThreadPool().runRenderJobs(true /* closeQueue */);
performCleanup();

emptyVertexBuffer.reset();
Expand All @@ -69,7 +68,7 @@ Context::~Context() noexcept {
}

void Context::beginFrame() {
scheduler.runRenderJobs();
backend.getThreadPool().runRenderJobs();
}

void Context::endFrame() {}
Expand Down
7 changes: 4 additions & 3 deletions src/mbgl/renderer/render_orchestrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class RenderTreeImpl final : public RenderTree {

} // namespace

RenderOrchestrator::RenderOrchestrator(bool backgroundLayerAsColor_, const std::optional<std::string>& localFontFamily_)
RenderOrchestrator::RenderOrchestrator(bool backgroundLayerAsColor_, TaggedScheduler& threadPool_, const std::optional<std::string>& localFontFamily_)
: observer(&nullObserver()),
glyphManager(std::make_unique<GlyphManager>(std::make_unique<LocalGlyphRasterizer>(localFontFamily_))),
imageManager(std::make_unique<ImageManager>()),
Expand All @@ -122,7 +122,7 @@ RenderOrchestrator::RenderOrchestrator(bool backgroundLayerAsColor_, const std::
layerImpls(makeMutable<std::vector<Immutable<style::Layer::Impl>>>()),
renderLight(makeMutable<Light::Impl>()),
backgroundLayerAsColor(backgroundLayerAsColor_),
threadPool(Scheduler::GetBackground(), static_cast<const void*>(this)) {
threadPool(threadPool_) {
glyphManager->setObserver(this);
imageManager->setObserver(this);
}
Expand Down Expand Up @@ -187,7 +187,8 @@ std::unique_ptr<RenderTree> RenderOrchestrator::createRenderTree(
updateParameters->annotationManager,
imageManager,
glyphManager,
updateParameters->prefetchZoomDelta};
updateParameters->prefetchZoomDelta,
threadPool};

glyphManager->setURL(updateParameters->glyphURL);

Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/renderer/render_orchestrator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#if MLN_DRAWABLE_RENDERER
#include <mbgl/renderer/layer_group.hpp>
#endif
#include <mbgl/actor/scheduler.hpp>
#include <mbgl/renderer/renderer.hpp>
#include <mbgl/renderer/render_source_observer.hpp>
#include <mbgl/renderer/render_light.hpp>
Expand Down Expand Up @@ -55,7 +56,7 @@ using ImmutableLayer = Immutable<style::Layer::Impl>;

class RenderOrchestrator final : public GlyphManagerObserver, public ImageManagerObserver, public RenderSourceObserver {
public:
RenderOrchestrator(bool backgroundLayerAsColor_, const std::optional<std::string>& localFontFamily_);
RenderOrchestrator(bool backgroundLayerAsColor_, TaggedScheduler& threadPool_, const std::optional<std::string>& localFontFamily_);
~RenderOrchestrator() override;

void markContextLost() { contextLost = true; };
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/renderer/renderer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ RendererObserver& nullObserver() {
Renderer::Impl::Impl(gfx::RendererBackend& backend_,
float pixelRatio_,
const std::optional<std::string>& localFontFamily_)
: orchestrator(!backend_.contextIsShared(), localFontFamily_),
: orchestrator(!backend_.contextIsShared(), backend_.getThreadPool(), localFontFamily_),
backend(backend_),
observer(&nullObserver()),
pixelRatio(pixelRatio_) {}
Expand Down
Loading

0 comments on commit a2592f1

Please sign in to comment.