From 424dae0a2c9e5ebe1c0230e5468f481273c98d0b Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Mon, 20 Apr 2015 17:30:20 -0700 Subject: [PATCH] Move run() to MapContext --- include/mbgl/map/map.hpp | 22 --- .../mbgl/platform/default/headless_view.hpp | 1 + src/mbgl/map/map.cpp | 135 +++--------------- src/mbgl/map/map_context.cpp | 95 +++++++++++- src/mbgl/map/map_context.hpp | 9 +- src/mbgl/map/map_data.hpp | 20 +++ 6 files changed, 136 insertions(+), 146 deletions(-) diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index 09fe05cb2..f91a24c35 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -12,7 +12,6 @@ #include #include #include -#include #include #include #include @@ -150,33 +149,12 @@ private: void resize(uint16_t width, uint16_t height, float ratio = 1); void resize(uint16_t width, uint16_t height, float ratio, uint16_t fbWidth, uint16_t fbHeight); - // Checks if render thread needs to pause - void checkForPause(); - - // Setup - void setup(); - size_t sourceCacheSize; const std::unique_ptr env; std::unique_ptr scope; View &view; const std::unique_ptr data; std::unique_ptr context; - -private: - std::thread thread; - - bool pausing = false; - bool isPaused = false; - std::mutex mutexRun; - std::condition_variable condRun; - std::mutex mutexPause; - std::condition_variable condPause; - - // Stores whether the map thread has been stopped already. - std::atomic_bool isStopped; - - StillImageCallback callback; }; } diff --git a/include/mbgl/platform/default/headless_view.hpp b/include/mbgl/platform/default/headless_view.hpp index 44b891bf6..fd7a69f96 100644 --- a/include/mbgl/platform/default/headless_view.hpp +++ b/include/mbgl/platform/default/headless_view.hpp @@ -17,6 +17,7 @@ typedef XID GLXPbuffer; #include #include +#include namespace mbgl { diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 38d0c3871..fc2b180a8 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include @@ -73,27 +72,14 @@ void Map::start(bool startPaused, MapMode renderMode) { data->mode = renderMode; // Reset the flag. - isStopped = false; - - context->start(); + data->isStopped = false; // Do we need to pause first? if (startPaused) { pause(); } - thread = std::thread([this]() { -#ifdef __APPLE__ - pthread_setname_np("Map"); -#endif - - run(); - - // Make sure that the stop() function knows when to stop invoking the callback function. - isStopped = true; - view.notify(); - }); - + context->start(); context->triggerUpdate(); } @@ -112,14 +98,14 @@ void Map::stop(std::function cb) { // the case with Cocoa's NSURLRequest. Otherwise, we will eventually deadlock because this // thread (== main thread) is blocked. The callback function should use an efficient waiting // function to avoid a busy waiting loop. - while (!isStopped) { + while (!data->isStopped) { cb(); } } // If a callback function was provided, this should return immediately because the thread has // already finished executing. - thread.join(); + data->thread.join(); data->mode = MapMode::None; } @@ -127,17 +113,17 @@ void Map::stop(std::function cb) { void Map::pause(bool waitForPause) { assert(Environment::currentlyOn(ThreadType::Main)); assert(data->mode == MapMode::Continuous); - mutexRun.lock(); - pausing = true; - mutexRun.unlock(); + data->mutexRun.lock(); + data->pausing = true; + data->mutexRun.unlock(); uv_stop(env->loop); context->triggerUpdate(); // Needed to ensure uv_stop is seen and uv_run exits, otherwise we deadlock on wait_for_pause if (waitForPause) { - std::unique_lock lockPause (mutexPause); - while (!isPaused) { - condPause.wait(lockPause); + std::unique_lock lockPause (data->mutexPause); + while (!data->isPaused) { + data->condPause.wait(lockPause); } } } @@ -146,10 +132,10 @@ void Map::resume() { assert(Environment::currentlyOn(ThreadType::Main)); assert(data->mode != MapMode::None); - mutexRun.lock(); - pausing = false; - condRun.notify_all(); - mutexRun.unlock(); + data->mutexRun.lock(); + data->pausing = false; + data->condRun.notify_all(); + data->mutexRun.unlock(); } void Map::renderStill(StillImageCallback fn) { @@ -159,80 +145,17 @@ void Map::renderStill(StillImageCallback fn) { throw util::Exception("Map is not in still image render mode"); } - if (callback) { + if (data->callback) { throw util::Exception("Map is currently rendering an image"); } assert(data->mode == MapMode::Still); - callback = std::move(fn); + data->callback = std::move(fn); context->triggerUpdate(Update::RenderStill); } -void Map::run() { - EnvironmentScope mapScope(*env, ThreadType::Map, "Map"); - assert(Environment::currentlyOn(ThreadType::Map)); - assert(data->mode != MapMode::None); - - if (data->mode == MapMode::Continuous) { - checkForPause(); - } - - view.activate(); - view.discard(); - - context->workers = util::make_unique(env->loop, 4); - - setup(); - context->prepare(); - - if (data->mode == MapMode::Continuous) { - context->terminating = false; - while (!context->terminating) { - uv_run(env->loop, UV_RUN_DEFAULT); - checkForPause(); - } - } else if (data->mode == MapMode::Still) { - context->terminating = false; - while (!context->terminating) { - uv_run(env->loop, UV_RUN_DEFAULT); - - // After the loop terminated, these async handles may have been deleted if the terminate() - // callback was fired. In this case, we are exiting the loop. - if (context->asyncTerminate && context->asyncUpdate) { - // Otherwise, loop termination means that we have acquired and parsed all resources - // required for this map image and we can now proceed to rendering. - context->render(); - auto image = view.readStillImage(); - - // We are moving the callback out of the way and empty it in case the callback function - // starts the next map image render. - assert(callback); - StillImageCallback cb; - std::swap(cb, callback); - - // Now we can finally invoke the callback function with the map image we rendered. - cb(std::move(image)); - - // To prepare for the next event loop run, we have to make sure the async handles keep - // the loop alive. - context->asyncTerminate->ref(); - context->asyncUpdate->ref(); - context->asyncInvoke->ref(); - context->asyncRender->ref(); - } - } - } else { - abort(); - } - - // Run the event loop once more to make sure our async delete handlers are called. - uv_run(env->loop, UV_RUN_ONCE); - - view.deactivate(); -} - void Map::renderSync() { // Must be called in UI thread. assert(Environment::currentlyOn(ThreadType::Main)); @@ -242,38 +165,12 @@ void Map::renderSync() { context->rendered.wait(); } -void Map::checkForPause() { - std::unique_lock lockRun (mutexRun); - while (pausing) { - view.deactivate(); - - mutexPause.lock(); - isPaused = true; - condPause.notify_all(); - mutexPause.unlock(); - - condRun.wait(lockRun); - - view.activate(); - } - - mutexPause.lock(); - isPaused = false; - mutexPause.unlock(); -} - void Map::update() { context->triggerUpdate(); } #pragma mark - Setup -void Map::setup() { - assert(Environment::currentlyOn(ThreadType::Map)); - assert(context->painter); - context->painter->setup(); -} - std::string Map::getStyleURL() const { return data->getStyleInfo().url; } diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index cc75883e8..81bc8c645 100644 --- a/src/mbgl/map/map_context.cpp +++ b/src/mbgl/map/map_context.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -54,7 +55,7 @@ void MapContext::start() { // could dispatch to the worker pool. workers.reset(); - terminating = true; + data.terminating = true; // Closes all open handles on the loop. This means that the loop will automatically terminate. asyncRender.reset(); @@ -86,8 +87,100 @@ void MapContext::start() { // Finally, notify all listeners that we have finished rendering this frame. rendered.notify(); }); + + data.thread = std::thread([this]() { +#ifdef __APPLE__ + pthread_setname_np("Map"); +#endif + + EnvironmentScope mapScope(env, ThreadType::Map, "Map"); + assert(Environment::currentlyOn(ThreadType::Map)); + assert(data.mode != MapMode::None); + + if (data.mode == MapMode::Continuous) { + checkForPause(); + } + + view.activate(); + view.discard(); + + workers = util::make_unique(env.loop, 4); + + assert(painter); + painter->setup(); + + prepare(); + + if (data.mode == MapMode::Continuous) { + data.terminating = false; + while (!data.terminating) { + uv_run(env.loop, UV_RUN_DEFAULT); + checkForPause(); + } + } else if (data.mode == MapMode::Still) { + data.terminating = false; + while (!data.terminating) { + uv_run(env.loop, UV_RUN_DEFAULT); + + // After the loop terminated, these async handles may have been deleted if the terminate() + // callback was fired. In this case, we are exiting the loop. + if (asyncTerminate && asyncUpdate) { + // Otherwise, loop termination means that we have acquired and parsed all resources + // required for this map image and we can now proceed to rendering. + render(); + auto image = view.readStillImage(); + + // We are moving the callback out of the way and empty it in case the callback function + // starts the next map image render. + assert(data.callback); + MapData::StillImageCallback cb; + std::swap(cb, data.callback); + + // Now we can finally invoke the callback function with the map image we rendered. + cb(std::move(image)); + + // To prepare for the next event loop run, we have to make sure the async handles keep + // the loop alive. + asyncTerminate->ref(); + asyncUpdate->ref(); + asyncInvoke->ref(); + asyncRender->ref(); + } + } + } else { + abort(); + } + + // Run the event loop once more to make sure our async delete handlers are called. + uv_run(env.loop, UV_RUN_ONCE); + + view.deactivate(); + + // Make sure that the stop() function knows when to stop invoking the callback function. + data.isStopped = true; + view.notify(); + }); } +void MapContext::checkForPause() { + std::unique_lock lockRun (data.mutexRun); + while (data.pausing) { + view.deactivate(); + + data.mutexPause.lock(); + data.isPaused = true; + data.condPause.notify_all(); + data.mutexPause.unlock(); + + data.condRun.wait(lockRun); + + view.activate(); + } + + data.mutexPause.lock(); + data.isPaused = false; + data.mutexPause.unlock(); +} void MapContext::terminate() { assert(asyncTerminate); diff --git a/src/mbgl/map/map_context.hpp b/src/mbgl/map/map_context.hpp index c6ef1c24f..3f2dbe725 100644 --- a/src/mbgl/map/map_context.hpp +++ b/src/mbgl/map/map_context.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace uv { class async; @@ -33,7 +34,7 @@ class MapContext { public: MapContext(Environment&, View&, MapData&); - // Starts the async handles. + // Starts the map thread. void start(); // Terminates the map thread @@ -58,6 +59,9 @@ public: // These can only be called from the Map thread. private: + // Checks if render thread needs to pause + void checkForPause(); + Worker& getWorker(); util::ptr getSprite(); void updateTiles(); @@ -114,9 +118,6 @@ public: // Used to signal that rendering completed. public: util::Signal rendered; - - // TODO: document this - public: bool terminating = false; }; } diff --git a/src/mbgl/map/map_data.hpp b/src/mbgl/map/map_data.hpp index 78ecc9e5a..63373428d 100644 --- a/src/mbgl/map/map_data.hpp +++ b/src/mbgl/map/map_data.hpp @@ -17,6 +17,8 @@ namespace mbgl { +class StillImage; + struct StyleInfo { std::string url; std::string base; @@ -119,6 +121,24 @@ private: std::atomic debug { false }; std::atomic animationTime; std::atomic defaultTransitionDuration; + +public: + // TODO: make private + bool terminating = false; + + std::thread thread; + bool pausing = false; + bool isPaused = false; + std::mutex mutexRun; + std::condition_variable condRun; + std::mutex mutexPause; + std::condition_variable condPause; + + // Stores whether the map thread has been stopped already. + std::atomic_bool isStopped; + + using StillImageCallback = std::function)>; + StillImageCallback callback; }; }