diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index f9738debe..148509ece 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -144,8 +144,8 @@ public: inline double getMetersPerPixelAtLatitude(const double lat, const double zoom) const { return Projection::getMetersPerPixelAtLatitude(lat, zoom); } inline const ProjectedMeters projectedMetersForLatLng(const LatLng latLng) const { return Projection::projectedMetersForLatLng(latLng); } inline const LatLng latLngForProjectedMeters(const ProjectedMeters projectedMeters) const { return Projection::latLngForProjectedMeters(projectedMeters); } - inline const vec2 pixelForLatLng(const LatLng latLng) const { return state.pixelForLatLng(latLng); } - inline const LatLng latLngForPixel(const vec2 pixel) const { return state.latLngForPixel(pixel); } + inline const vec2 pixelForLatLng(const LatLng latLng) const { return transform.currentState().pixelForLatLng(latLng); } + inline const LatLng latLngForPixel(const vec2 pixel) const { return transform.currentState().latLngForPixel(pixel); } // Annotations void setDefaultPointAnnotationSymbol(const std::string&); @@ -168,8 +168,6 @@ public: void toggleDebug(); bool getDebug() const; - inline const TransformState &getState() const { return state; } - TimePoint getTime() const; inline AnnotationManager& getAnnotationManager() const { return *annotationManager; } private: @@ -245,7 +243,6 @@ private: std::atomic_bool isStopped; Transform transform; - TransformState state; FileSource& fileSource; diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index e931d09e0..74b153862 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -450,7 +450,8 @@ std::string Map::getStyleJSON() const { } util::ptr Map::getSprite() { - const float pixelRatio = state.getPixelRatio(); + assert(Environment::currentlyOn(ThreadType::Map)); + const float pixelRatio = data->getTransformState().getPixelRatio(); const std::string &sprite_url = style->getSpriteURL(); if (!sprite || !sprite->hasPixelRatio(pixelRatio)) { sprite = Sprite::Create(sprite_url, pixelRatio, *env); @@ -672,10 +673,6 @@ bool Map::getDebug() const { return data->getDebug(); } -TimePoint Map::getTime() const { - return data->getAnimationTime(); -} - void Map::addClass(const std::string& klass) { if (data->addClass(klass)) { triggerUpdate(Update::Classes); @@ -717,7 +714,7 @@ void Map::updateTiles() { assert(Environment::currentlyOn(ThreadType::Map)); if (!style) return; for (const auto& source : style->sources) { - source->update(*this, getWorker(), style, *glyphAtlas, *glyphStore, + source->update(*this, *data, getWorker(), style, *glyphAtlas, *glyphStore, *spriteAtlas, getSprite(), *texturePool, [this]() { assert(Environment::currentlyOn(ThreadType::Map)); triggerUpdate(); @@ -728,7 +725,7 @@ void Map::updateTiles() { void Map::update() { assert(Environment::currentlyOn(ThreadType::Map)); - if (state.hasSize()) { + if (data->getTransformState().hasSize()) { prepare(); } } @@ -792,7 +789,7 @@ void Map::prepare() { u |= static_cast(Update::StyleInfo); } - state = transform.currentState(); + data->setTransformState(transform.currentState()); if (u & static_cast(Update::StyleInfo)) { reloadStyle(); @@ -828,11 +825,11 @@ void Map::prepare() { if (u & static_cast(Update::StyleInfo) || u & static_cast(Update::Classes) || u & static_cast(Update::Zoom)) { - style->recalculate(state.getNormalizedZoom(), now); + style->recalculate(data->getTransformState().getNormalizedZoom(), now); } // Allow the sprite atlas to potentially pull new sprite images if needed. - spriteAtlas->resize(state.getPixelRatio()); + spriteAtlas->resize(data->getTransformState().getPixelRatio()); spriteAtlas->setSprite(getSprite()); updateTiles(); @@ -854,7 +851,7 @@ void Map::render() { assert(style); assert(painter); - painter->render(*style, state, data->getAnimationTime()); + painter->render(*style, data->getTransformState(), data->getAnimationTime()); // Schedule another rerender when we definitely need a next frame. if (transform.needsTransition() || style->hasTransitions()) { diff --git a/src/mbgl/map/map_data.hpp b/src/mbgl/map/map_data.hpp index 718c3eb0e..cbb1b5413 100644 --- a/src/mbgl/map/map_data.hpp +++ b/src/mbgl/map/map_data.hpp @@ -7,6 +7,10 @@ #include #include #include +#include + +#include +#include namespace mbgl { @@ -87,12 +91,23 @@ public: defaultTransitionDuration = duration; }; + // Make sure the state is only accessible/modifiable from the Map thread. + inline const TransformState& getTransformState() const { + assert(Environment::currentlyOn(ThreadType::Map)); + return transformState; + } + inline void setTransformState(const TransformState& state) { + assert(Environment::currentlyOn(ThreadType::Map)); + transformState = state; + } + private: mutable std::mutex mtx; StyleInfo styleInfo; std::string accessToken; std::vector classes; + TransformState transformState; std::atomic debug { false }; std::atomic animationTime; std::atomic defaultTransitionDuration; diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp index 9e318964b..aa171ee43 100644 --- a/src/mbgl/map/source.cpp +++ b/src/mbgl/map/source.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -215,11 +216,17 @@ TileData::State Source::hasTile(const TileID& id) { return TileData::State::invalid; } -TileData::State Source::addTile(Map &map, Worker &worker, - util::ptr