[core] always reparse with the freshest possible placement config

Fixes an issue where updates to stale tiles would remove labels altogether until the map was rotated.
This commit is contained in:
Konstantin Käfer
2015-10-26 15:45:32 +01:00
parent c0c554e36f
commit 75dec6ffac
15 changed files with 231 additions and 130 deletions

View File

@@ -14,17 +14,12 @@ using namespace mbgl;
LiveTileData::LiveTileData(const TileID& id_,
std::unique_ptr<AnnotationTile> tile_,
Style& style_,
const SourceInfo& source_,
Style& style,
const SourceInfo& source,
std::function<void()> callback)
: TileData(id_),
worker(style_.workers),
tileWorker(id_,
source_.source_id,
style_,
style_.layers,
state,
std::make_unique<CollisionTile>(0, 0, false)),
worker(style.workers),
tileWorker(id, source.source_id, style, style.layers, state),
tile(std::move(tile_)) {
state = State::loaded;
@@ -41,25 +36,41 @@ bool LiveTileData::parsePending(std::function<void()> callback) {
return false;
}
workRequest = worker.parseLiveTile(tileWorker, *tile, [this, callback] (TileParseResult result) {
workRequest.reset();
workRequest = worker.parseLiveTile(tileWorker, *tile, targetConfig, [this, callback, config = targetConfig] (TileParseResult result) {
workRequest.reset();
if (result.is<TileParseResultBuckets>()) {
auto& resultBuckets = result.get<TileParseResultBuckets>();
state = resultBuckets.state;
// Persist the configuration we just placed so that we can later check whether we need
// to place again in case the configuration has changed.
placedConfig = config;
// Move over all buckets we received in this parse request, potentially overwriting
// existing buckets in case we got a refresh parse.
for (auto& bucket : resultBuckets.buckets) {
buckets[bucket.first] = std::move(bucket.second);
}
// The target configuration could have changed since we started placement. In this case,
// we're starting another placement run.
if (placedConfig != targetConfig) {
redoPlacement();
}
} else {
error = result.get<std::string>();
state = State::obsolete;
}
callback();
// The target configuration could have changed since we started placement. In this case,
// we're starting another placement run.
if (!workRequest && placedConfig != targetConfig) {
redoPlacement();
}
});
return true;
@@ -87,3 +98,36 @@ void LiveTileData::cancel() {
state = State::obsolete;
workRequest.reset();
}
void LiveTileData::redoPlacement(const PlacementConfig newConfig) {
if (newConfig != placedConfig) {
targetConfig = newConfig;
if (!workRequest) {
// Don't start a new placement request when the current one hasn't completed yet, or
// when we are parsing buckets.
redoPlacement();
}
}
}
void LiveTileData::redoPlacement() {
workRequest.reset();
workRequest = worker.redoPlacement(tileWorker, buckets, targetConfig, [this, config = targetConfig] {
workRequest.reset();
// Persist the configuration we just placed so that we can later check whether we need to
// place again in case the configuration has changed.
placedConfig = config;
for (auto& bucket : buckets) {
bucket.second->swapRenderData();
}
// The target configuration could have changed since we started placement. In this case,
// we're starting another placement run.
if (placedConfig != targetConfig) {
redoPlacement();
}
});
}

View File

@@ -22,6 +22,9 @@ public:
bool parsePending(std::function<void ()> callback) override;
void redoPlacement(PlacementConfig config) override;
void redoPlacement();
void cancel() override;
Bucket* getBucket(const StyleLayer&) override;
@@ -34,6 +37,13 @@ private:
// Contains all the Bucket objects for the tile. Buckets are render
// objects and they get added by tile parsing operations.
std::unordered_map<std::string, std::unique_ptr<Bucket>> buckets;
// Stores the placement configuration of the text that is currently placed on the screen.
PlacementConfig placedConfig;
// Stores the placement configuration of how the text should be placed. This isn't necessarily
// the one that is being displayed.
PlacementConfig targetConfig;
};
}

View File

@@ -286,8 +286,7 @@ TileData::State Source::addTile(MapData& data,
// If we don't find working tile data, we're just going to load it.
if (info.type == SourceType::Vector) {
auto tileData = std::make_shared<VectorTileData>(normalized_id, style, info,
transformState.getAngle(), transformState.getPitch(), data.getCollisionDebug());
auto tileData = std::make_shared<VectorTileData>(normalized_id, style, info);
tileData->request(data.pixelRatio, callback);
new_tile.data = tileData;
} else if (info.type == SourceType::Raster) {
@@ -296,7 +295,7 @@ TileData::State Source::addTile(MapData& data,
new_tile.data = tileData;
} else if (info.type == SourceType::Annotations) {
new_tile.data = std::make_shared<LiveTileData>(normalized_id,
data.getAnnotationManager()->getTile(normalized_id), style, info, callback);
data.getAnnotationManager()->getTile(normalized_id), style, info, callback);
} else {
throw std::runtime_error("source type not implemented");
}
@@ -510,7 +509,8 @@ bool Source::update(MapData& data,
updateTilePtrs();
for (auto& tilePtr : tilePtrs) {
tilePtr->data->redoPlacement(transformState.getAngle(), transformState.getPitch(), data.getCollisionDebug());
tilePtr->data->redoPlacement(
{ transformState.getAngle(), transformState.getPitch(), data.getCollisionDebug() });
}
updated = data.getAnimationTime();
@@ -560,7 +560,7 @@ void Source::tileLoadingCompleteCallback(const TileID& normalized_id, const Tran
return;
}
data->redoPlacement(transformState.getAngle(), transformState.getPitch(), collisionDebug);
data->redoPlacement({ transformState.getAngle(), transformState.getPitch(), collisionDebug });
emitTileLoaded(true);
}

View File

@@ -4,6 +4,7 @@
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/map/tile_id.hpp>
#include <mbgl/renderer/bucket.hpp>
#include <mbgl/text/placement_config.hpp>
#include <atomic>
#include <string>
@@ -76,7 +77,7 @@ public:
virtual Bucket* getBucket(const StyleLayer&) = 0;
virtual bool parsePending(std::function<void ()>) { return true; }
virtual void redoPlacement(float, float, bool) {}
virtual void redoPlacement(PlacementConfig) {}
bool isReady() const {
return isReadyState(state);

View File

@@ -19,15 +19,13 @@ TileWorker::TileWorker(TileID id_,
std::string sourceID_,
Style& style_,
std::vector<util::ptr<StyleLayer>> layers_,
const std::atomic<TileData::State>& state_,
std::unique_ptr<CollisionTile> collision_)
const std::atomic<TileData::State>& state_)
: layers(std::move(layers_)),
id(id_),
sourceID(sourceID_),
parameters(id.z),
style(style_),
state(state_),
collisionTile(std::move(collision_)) {
state(state_) {
assert(style.sprite);
}
@@ -35,10 +33,14 @@ TileWorker::~TileWorker() {
style.glyphAtlas->removeGlyphs(reinterpret_cast<uintptr_t>(this));
}
TileParseResult TileWorker::parseAllLayers(const GeometryTile& geometryTile) {
TileParseResult TileWorker::parseAllLayers(const GeometryTile& geometryTile,
PlacementConfig config) {
// We're doing a fresh parse of the tile, because the underlying data has changed.
pending.clear();
// Reset the collision tile so we have a clean slate; we're placing all features anyway.
collisionTile = std::make_unique<CollisionTile>(config);
// We're storing a list of buckets we've parsed to avoid parsing a bucket twice that is
// referenced from more than one layer
std::set<StyleBucket*> parsed;
@@ -84,12 +86,10 @@ TileParseResult TileWorker::parsePendingLayers() {
void TileWorker::redoPlacement(
const std::unordered_map<std::string, std::unique_ptr<Bucket>>* buckets,
float angle,
float pitch,
bool collisionDebug) {
PlacementConfig config) {
// Reset the collision tile so we have a clean slate; we're placing all features anyway.
collisionTile = std::make_unique<CollisionTile>(angle, pitch, collisionDebug);
collisionTile = std::make_unique<CollisionTile>(config);
for (auto i = layers.rbegin(); i != layers.rend(); i++) {
const auto it = buckets->find((*i)->id);
@@ -268,11 +268,17 @@ void TileWorker::createSymbolBucket(const GeometryTileLayer& layer,
bucket->parseFeatures(layer, styleBucket.filter);
assert(style.glyphStore);
assert(style.sprite);
const bool needsDependencies = bucket->needsDependencies(*style.glyphStore, *style.sprite);
if (needsDependencies) {
// We cannot parse this bucket yet. Instead, we're saving it for later.
pending.emplace_back(styleBucket, std::move(bucket));
} else {
assert(style.spriteAtlas);
assert(style.glyphAtlas);
assert(style.glyphStore);
assert(collisionTile);
bucket->addFeatures(reinterpret_cast<uintptr_t>(this), *style.spriteAtlas,
*style.glyphAtlas, *style.glyphStore, *collisionTile);
insertBucket(styleBucket.name, std::move(bucket));

View File

@@ -8,6 +8,7 @@
#include <mbgl/util/ptr.hpp>
#include <mbgl/style/filter_expression.hpp>
#include <mbgl/style/style_calculation_parameters.hpp>
#include <mbgl/text/placement_config.hpp>
#include <string>
#include <memory>
@@ -42,16 +43,13 @@ public:
std::string sourceID,
Style&,
std::vector<util::ptr<StyleLayer>>,
const std::atomic<TileData::State>&,
std::unique_ptr<CollisionTile>);
const std::atomic<TileData::State>&);
~TileWorker();
TileParseResult parseAllLayers(const GeometryTile&);
TileParseResult parseAllLayers(const GeometryTile&, PlacementConfig);
TileParseResult parsePendingLayers();
void redoPlacement(const std::unordered_map<std::string, std::unique_ptr<Bucket>>*,
float angle,
float pitch,
bool collisionDebug);
PlacementConfig);
std::vector<util::ptr<StyleLayer>> layers;

View File

@@ -14,23 +14,15 @@ using namespace mbgl;
VectorTileData::VectorTileData(const TileID& id_,
Style& style_,
const SourceInfo& source_,
float angle,
float pitch,
bool collisionDebug)
const SourceInfo& source_)
: TileData(id_),
worker(style_.workers),
tileWorker(id_,
source_.source_id,
style_,
style_.layers,
state,
std::make_unique<CollisionTile>(angle, pitch, collisionDebug)),
source(source_),
lastAngle(angle),
currentAngle(angle),
currentPitch(pitch),
currentCollisionDebug(collisionDebug) {
state),
source(source_) {
}
VectorTileData::~VectorTileData() {
@@ -81,7 +73,7 @@ void VectorTileData::parse(std::function<void ()> callback) {
// when tile data changed. Replacing the workdRequest will cancel a pending work
// request in case there is one.
workRequest.reset();
workRequest = worker.parseVectorTile(tileWorker, data, [this, callback] (TileParseResult result) {
workRequest = worker.parseVectorTile(tileWorker, data, targetConfig, [this, callback, config = targetConfig] (TileParseResult result) {
workRequest.reset();
if (state == State::obsolete) {
return;
@@ -91,11 +83,21 @@ void VectorTileData::parse(std::function<void ()> callback) {
auto& resultBuckets = result.get<TileParseResultBuckets>();
state = resultBuckets.state;
// Persist the configuration we just placed so that we can later check whether we need to
// place again in case the configuration has changed.
placedConfig = config;
// Move over all buckets we received in this parse request, potentially overwriting
// existing buckets in case we got a refresh parse.
for (auto& bucket : resultBuckets.buckets) {
buckets[bucket.first] = std::move(bucket.second);
}
// The target configuration could have changed since we started placement. In this case,
// we're starting another placement run.
if (placedConfig != targetConfig) {
redoPlacement();
}
} else {
std::stringstream message;
message << "Failed to parse [" << std::string(id) << "]: " << result.get<std::string>();
@@ -129,6 +131,12 @@ bool VectorTileData::parsePending(std::function<void()> callback) {
for (auto& bucket : resultBuckets.buckets) {
buckets[bucket.first] = std::move(bucket.second);
}
// The target configuration could have changed since we started placement. In this case,
// we're starting another placement run.
if (placedConfig != targetConfig) {
redoPlacement();
}
} else {
std::stringstream message;
message << "Failed to parse [" << std::string(id) << "]: " << result.get<std::string>();
@@ -156,34 +164,36 @@ Bucket* VectorTileData::getBucket(const StyleLayer& layer) {
return it->second.get();
}
void VectorTileData::redoPlacement(float angle, float pitch, bool collisionDebug) {
if (angle == currentAngle && pitch == currentPitch && collisionDebug == currentCollisionDebug)
return;
void VectorTileData::redoPlacement(const PlacementConfig newConfig) {
if (newConfig != placedConfig) {
targetConfig = newConfig;
lastAngle = angle;
lastPitch = pitch;
lastCollisionDebug = collisionDebug;
if (workRequest) {
// Don't start a new placement request when the current one hasn't completed yet, or when
// we are parsing buckets.
return;
}
currentAngle = angle;
currentPitch = pitch;
currentCollisionDebug = collisionDebug;
workRequest.reset();
workRequest = worker.redoPlacement(tileWorker, buckets, angle, pitch, collisionDebug, [this] {
workRequest.reset();
for (const auto& layer : tileWorker.layers) {
auto bucket = getBucket(*layer);
if (bucket) {
bucket->swapRenderData();
}
if (!workRequest) {
// Don't start a new placement request when the current one hasn't completed yet, or when
// we are parsing buckets.
redoPlacement();
}
}
}
void VectorTileData::redoPlacement() {
workRequest.reset();
workRequest = worker.redoPlacement(tileWorker, buckets, targetConfig, [this, config = targetConfig] {
workRequest.reset();
// Persist the configuration we just placed so that we can later check whether we need to
// place again in case the configuration has changed.
placedConfig = config;
for (auto& bucket : buckets) {
bucket.second->swapRenderData();
}
// The target configuration could have changed since we started placement. In this case,
// we're starting another placement run.
if (placedConfig != targetConfig) {
redoPlacement();
}
redoPlacement(lastAngle, lastPitch, lastCollisionDebug);
});
}

View File

@@ -4,6 +4,7 @@
#include <mbgl/map/tile_data.hpp>
#include <mbgl/map/tile_worker.hpp>
#include <mbgl/storage/request_holder.hpp>
#include <mbgl/text/placement_config.hpp>
#include <atomic>
@@ -17,7 +18,7 @@ class Request;
class VectorTileData : public TileData {
public:
VectorTileData(
const TileID&, Style&, const SourceInfo&, float angle_, float pitch_, bool collisionDebug_);
const TileID&, Style&, const SourceInfo&);
~VectorTileData();
Bucket* getBucket(const StyleLayer&) override;
@@ -27,7 +28,8 @@ public:
void parse(std::function<void()> callback);
bool parsePending(std::function<void()> callback) override;
void redoPlacement(float angle, float pitch, bool collisionDebug) override;
void redoPlacement(PlacementConfig config) override;
void redoPlacement();
void cancel() override;
@@ -43,12 +45,13 @@ private:
const SourceInfo& source;
RequestHolder req;
std::shared_ptr<const std::string> data;
float lastAngle = 0;
float currentAngle;
float lastPitch = 0;
float currentPitch;
bool lastCollisionDebug = 0;
bool currentCollisionDebug = 0;
// Stores the placement configuration of the text that is currently placed on the screen.
PlacementConfig placedConfig;
// Stores the placement configuration of how the text should be placed. This isn't necessarily
// the one that is being displayed.
PlacementConfig targetConfig;
};
} // namespace mbgl