diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2015-04-23 11:46:22 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-04-28 14:32:24 -0400 |
commit | a625ba8a0976ef8417a2609700187c5633be2c58 (patch) | |
tree | ee61c1e8b6bde556b2c93f4730ea806547f6090a /src | |
parent | c84e8862226e6f89538b34fafd592182caf9008b (diff) | |
download | qtlocation-mapboxgl-a625ba8a0976ef8417a2609700187c5633be2c58.tar.gz |
Remove Map::start/stop
Both Android and iOS now use pause/resume. The map thread is
always running, and be able to receive messages. When paused,
it will just refuse to render.
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/map/map.cpp | 70 | ||||
-rw-r--r-- | src/mbgl/map/map_context.cpp | 1 | ||||
-rw-r--r-- | src/mbgl/map/map_data.hpp | 4 | ||||
-rw-r--r-- | src/mbgl/util/thread.hpp | 14 |
4 files changed, 11 insertions, 78 deletions
diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 0bf4f11a67..c82c2c7c77 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -33,60 +33,22 @@ namespace mbgl { -Map::Map(View& view_, FileSource& fileSource_) - : env(util::make_unique<Environment>(fileSource_)), +Map::Map(View& view, FileSource& fileSource, MapMode mode, bool startPaused) + : env(util::make_unique<Environment>(fileSource)), scope(util::make_unique<EnvironmentScope>(*env, ThreadType::Main, "Main")), - view(view_), - data(util::make_unique<MapData>(view_)) + data(util::make_unique<MapData>(view, mode)), + context(util::make_unique<util::Thread<MapContext>>("Map", *env, view, *data, startPaused)) { view.initialize(this); } Map::~Map() { - if (data->mode != MapMode::None) { - stop(); - } -} - -void Map::start(bool startPaused, MapMode renderMode) { - assert(Environment::currentlyOn(ThreadType::Main)); - assert(data->mode == MapMode::None); - - // When starting map rendering in another thread, we perform async/continuously - // updated rendering. Only in these cases, we attach the async handlers. - data->mode = renderMode; - - context = util::make_unique<util::Thread<MapContext>>("Map", *env, view, *data, startPaused); - triggerUpdate(); -} - -void Map::stop(std::function<void ()> cb) { - assert(Environment::currentlyOn(ThreadType::Main)); - assert(data->mode != MapMode::None); - resume(); - - if (cb) { - // Wait until the render thread stopped. We are using this construct instead of plainly - // relying on the thread_join because the system might need to run things in the current - // thread that is required for the render thread to terminate correctly. This is for example - // 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. - context->pumpingStop(cb); - } - - // If a callback function was provided, this should return immediately because the thread has - // already finished executing. - context.reset(); - - data->mode = MapMode::None; } void Map::pause(bool waitForPause) { assert(Environment::currentlyOn(ThreadType::Main)); assert(data->mode == MapMode::Continuous); - assert(context); std::unique_lock<std::mutex> lockPause(data->mutexPause); context->invoke(&MapContext::pause); @@ -98,15 +60,12 @@ void Map::pause(bool waitForPause) { void Map::resume() { assert(Environment::currentlyOn(ThreadType::Main)); - assert(data->mode != MapMode::None); - assert(context); data->condResume.notify_all(); } void Map::renderStill(StillImageCallback fn) { assert(Environment::currentlyOn(ThreadType::Main)); - assert(context); if (data->mode != MapMode::Still) { throw util::Exception("Map is not in still image render mode"); @@ -122,20 +81,17 @@ void Map::renderStill(StillImageCallback fn) { void Map::renderSync() { assert(Environment::currentlyOn(ThreadType::Main)); - assert(context); context->invokeSync(&MapContext::render); } void Map::renderAsync() { assert(Environment::currentlyOn(ThreadType::Main)); - assert(context); context->invoke(&MapContext::render); } void Map::update() { - assert(context); triggerUpdate(); } @@ -346,7 +302,6 @@ void Map::setDefaultPointAnnotationSymbol(const std::string& symbol) { double Map::getTopOffsetPixelsForAnnotationSymbol(const std::string& symbol) { assert(Environment::currentlyOn(ThreadType::Main)); - assert(context); return context->invokeSync<double>(&MapContext::getTopOffsetPixelsForAnnotationSymbol, symbol); } @@ -357,9 +312,7 @@ uint32_t Map::addPointAnnotation(const LatLng& point, const std::string& symbol) std::vector<uint32_t> Map::addPointAnnotations(const std::vector<LatLng>& points, const std::vector<std::string>& symbols) { assert(Environment::currentlyOn(ThreadType::Main)); auto result = data->annotationManager.addPointAnnotations(points, symbols, *data); - if (context) { - context->invoke(&MapContext::updateAnnotationTiles, result.first); - } + context->invoke(&MapContext::updateAnnotationTiles, result.first); return result.second; } @@ -371,9 +324,7 @@ void Map::removeAnnotation(uint32_t annotation) { void Map::removeAnnotations(const std::vector<uint32_t>& annotations) { assert(Environment::currentlyOn(ThreadType::Main)); auto result = data->annotationManager.removeAnnotations(annotations, *data); - if (context) { - context->invoke(&MapContext::updateAnnotationTiles, result); - } + context->invoke(&MapContext::updateAnnotationTiles, result); } std::vector<uint32_t> Map::getAnnotationsInBounds(const LatLngBounds& bounds) { @@ -441,22 +392,17 @@ Duration Map::getDefaultTransitionDuration() { } void Map::setSourceTileCacheSize(size_t size) { - assert(context); context->invoke(&MapContext::setSourceTileCacheSize, size); } void Map::onLowMemory() { - if (context) { - context->invoke(&MapContext::onLowMemory); - } + context->invoke(&MapContext::onLowMemory); } #pragma mark - Private void Map::triggerUpdate(Update update_) { - if (context) { - context->invoke(&MapContext::triggerUpdate, update_); - } + context->invoke(&MapContext::triggerUpdate, update_); } } diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index 012fae2d06..981191140b 100644 --- a/src/mbgl/map/map_context.cpp +++ b/src/mbgl/map/map_context.cpp @@ -45,7 +45,6 @@ MapContext::MapContext(uv_loop_t* loop, Environment& env_, View& view_, MapData& painter(util::make_unique<Painter>(*spriteAtlas, *glyphAtlas, *lineAtlas)) { assert(Environment::currentlyOn(ThreadType::Map)); - assert(data.mode != MapMode::None); asyncUpdate->unref(); diff --git a/src/mbgl/map/map_data.hpp b/src/mbgl/map/map_data.hpp index 089eb3b9f2..248614edf4 100644 --- a/src/mbgl/map/map_data.hpp +++ b/src/mbgl/map/map_data.hpp @@ -30,7 +30,7 @@ class MapData { using Lock = std::lock_guard<std::mutex>; public: - inline MapData(View& view) : transform(view) { + inline MapData(View& view, MapMode mode_) : transform(view), mode(mode_) { setAnimationTime(TimePoint::min()); setDefaultTransitionDuration(Duration::zero()); } @@ -100,7 +100,7 @@ public: public: Transform transform; AnnotationManager annotationManager; - MapMode mode = MapMode::None; + const MapMode mode; private: mutable std::mutex mtx; diff --git a/src/mbgl/util/thread.hpp b/src/mbgl/util/thread.hpp index 9a1a7b0137..2150f5bc3b 100644 --- a/src/mbgl/util/thread.hpp +++ b/src/mbgl/util/thread.hpp @@ -89,7 +89,6 @@ private: std::promise<void> joinable; std::thread thread; - std::atomic_bool joined; Object* object; RunLoop* loop; @@ -97,8 +96,7 @@ private: template <class Object> template <class... Args> -Thread<Object>::Thread(const std::string& name, Args&&... args) - : joined(false) { +Thread<Object>::Thread(const std::string& name, Args&&... args) { // Note: We're using std::tuple<> to store the arguments because GCC 4.9 has a bug // when expanding parameters packs captured in lambdas. std::tuple<Args...> params = std::forward_as_tuple(::std::forward<Args>(args)...); @@ -137,7 +135,6 @@ void Thread<Object>::run(P&& params, index_sequence<I...>) { loop_.run(); joinable.get_future().get(); - joined = true; } template <class Object> @@ -147,15 +144,6 @@ Thread<Object>::~Thread() { thread.join(); } -template <class Object> -void Thread<Object>::pumpingStop(std::function<void ()> cb) { - loop->stop(); - joinable.set_value(); - while (!joined) { - cb(); - } -} - } } |