[core] reparse tiles when new data arrives

We're now reparsing tiles when they expire. We're also swapping out buckets atomically to avoid flickering data; i.e. we're displaying the old data as long as we don't have a new parsed bucket for that layer yet. The parsed buckets now live in the *TileData objects rather than in the TileWorker; only partially parsed == pending buckets will remain in the TileWorker. Once they're parsed, they're moved to the *TileData object.
This commit is contained in:
Konstantin Käfer
2015-10-20 12:19:11 +02:00
parent 5173bf1bb8
commit c0c554e36f
27 changed files with 467 additions and 289 deletions

View File

@@ -3,6 +3,7 @@
#include <mbgl/style/style.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/style/property_evaluator.hpp>
#include <mbgl/geometry/sprite_atlas.hpp>
#include <mbgl/geometry/glyph_atlas.hpp>
#include <mbgl/renderer/fill_bucket.hpp>
#include <mbgl/renderer/line_bucket.hpp>
@@ -10,6 +11,7 @@
#include <mbgl/renderer/symbol_bucket.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/exception.hpp>
using namespace mbgl;
@@ -33,34 +35,66 @@ TileWorker::~TileWorker() {
style.glyphAtlas->removeGlyphs(reinterpret_cast<uintptr_t>(this));
}
Bucket* TileWorker::getBucket(const StyleLayer& layer) const {
std::lock_guard<std::mutex> lock(bucketsMutex);
TileParseResult TileWorker::parseAllLayers(const GeometryTile& geometryTile) {
// We're doing a fresh parse of the tile, because the underlying data has changed.
pending.clear();
const auto it = buckets.find(layer.bucket->name);
if (it == buckets.end()) {
return nullptr;
}
assert(it->second);
return it->second.get();
}
TileParseResult TileWorker::parse(const GeometryTile& geometryTile) {
partialParse = false;
// 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;
for (auto i = layers.rbegin(); i != layers.rend(); i++) {
parseLayer(**i, geometryTile);
const StyleLayer& layer = **i;
if (layer.bucket && parsed.find(layer.bucket.get()) == parsed.end()) {
parsed.emplace(layer.bucket.get());
parseLayer(layer, geometryTile);
}
}
return partialParse ? TileData::State::partial : TileData::State::parsed;
result.state = pending.empty() ? TileData::State::parsed : TileData::State::partial;
return std::move(result);
}
void TileWorker::redoPlacement(float angle, float pitch, bool collisionDebug) {
TileParseResult TileWorker::parsePendingLayers() {
// Try parsing the remaining layers that we couldn't parse in the first step due to missing
// dependencies.
for (auto it = pending.begin(); it != pending.end();) {
auto& styleBucket = it->first;
auto& bucket = it->second;
assert(bucket);
if (styleBucket.type == StyleLayerType::Symbol) {
auto symbolBucket = dynamic_cast<SymbolBucket*>(bucket.get());
if (!symbolBucket->needsDependencies(*style.glyphStore, *style.sprite)) {
symbolBucket->addFeatures(reinterpret_cast<uintptr_t>(this), *style.spriteAtlas,
*style.glyphAtlas, *style.glyphStore, *collisionTile);
insertBucket(styleBucket.name, std::move(bucket));
pending.erase(it++);
continue;
}
}
// Advance the iterator here; we're skipping this when erasing an element from this list.
++it;
}
result.state = pending.empty() ? TileData::State::parsed : TileData::State::partial;
return std::move(result);
}
void TileWorker::redoPlacement(
const std::unordered_map<std::string, std::unique_ptr<Bucket>>* buckets,
float angle,
float pitch,
bool collisionDebug) {
// Reset the collision tile so we have a clean slate; we're placing all features anyway.
collisionTile = std::make_unique<CollisionTile>(angle, pitch, collisionDebug);
for (auto i = layers.rbegin(); i != layers.rend(); i++) {
auto bucket = getBucket(**i);
if (bucket) {
bucket->placeFeatures(*collisionTile);
const auto it = buckets->find((*i)->id);
if (it != buckets->end()) {
it->second->placeFeatures(*collisionTile);
}
}
}
@@ -88,21 +122,15 @@ void TileWorker::parseLayer(const StyleLayer& layer, const GeometryTile& geometr
return;
}
// This is a singular layer. Check if this bucket already exists.
if (getBucket(layer))
return;
const StyleBucket& styleBucket = *layer.bucket;
// Skip this bucket if we are to not render this
if (styleBucket.source != sourceID)
return;
if (id.z < std::floor(styleBucket.min_zoom))
return;
if (id.z >= std::ceil(styleBucket.max_zoom))
return;
if (styleBucket.visibility == mbgl::VisibilityType::None)
if ((styleBucket.source != sourceID) ||
(id.z < std::floor(styleBucket.min_zoom)) ||
(id.z >= std::ceil(styleBucket.max_zoom)) ||
(styleBucket.visibility == VisibilityType::None)) {
return;
}
auto geometryLayer = geometryTile.getLayer(styleBucket.source_layer);
if (!geometryLayer) {
@@ -114,35 +142,25 @@ void TileWorker::parseLayer(const StyleLayer& layer, const GeometryTile& geometr
return;
}
std::unique_ptr<Bucket> bucket;
switch (styleBucket.type) {
case StyleLayerType::Fill:
bucket = createFillBucket(*geometryLayer, styleBucket);
createFillBucket(*geometryLayer, styleBucket);
break;
case StyleLayerType::Line:
bucket = createLineBucket(*geometryLayer, styleBucket);
createLineBucket(*geometryLayer, styleBucket);
break;
case StyleLayerType::Circle:
bucket = createCircleBucket(*geometryLayer, styleBucket);
createCircleBucket(*geometryLayer, styleBucket);
break;
case StyleLayerType::Symbol:
bucket = createSymbolBucket(*geometryLayer, styleBucket);
createSymbolBucket(*geometryLayer, styleBucket);
break;
case StyleLayerType::Raster:
return;
break;
default:
Log::Warning(Event::ParseTile, "unknown bucket render type for layer '%s' (source layer '%s')",
styleBucket.name.c_str(), styleBucket.source_layer.c_str());
}
// Bucket creation might fail because the data tile may not
// contain any data that falls into this bucket.
if (!bucket)
return;
std::lock_guard<std::mutex> lock(bucketsMutex);
buckets[styleBucket.name] = std::move(bucket);
}
template <class Bucket>
@@ -161,101 +179,108 @@ void TileWorker::addBucketGeometries(Bucket& bucket, const GeometryTileLayer& la
}
}
std::unique_ptr<Bucket> TileWorker::createFillBucket(const GeometryTileLayer& layer,
const StyleBucket& bucket_desc) {
void TileWorker::createFillBucket(const GeometryTileLayer& layer,
const StyleBucket& styleBucket) {
auto bucket = std::make_unique<FillBucket>();
addBucketGeometries(bucket, layer, bucket_desc.filter);
return bucket->hasData() ? std::move(bucket) : nullptr;
// Fill does not have layout properties to apply.
addBucketGeometries(bucket, layer, styleBucket.filter);
insertBucket(styleBucket.name, std::move(bucket));
}
std::unique_ptr<Bucket> TileWorker::createLineBucket(const GeometryTileLayer& layer,
const StyleBucket& bucket_desc) {
void TileWorker::createLineBucket(const GeometryTileLayer& layer,
const StyleBucket& styleBucket) {
auto bucket = std::make_unique<LineBucket>();
const float z = id.z;
auto& layout = bucket->layout;
applyLayoutProperty(PropertyKey::LineCap, bucket_desc.layout, layout.cap, z);
applyLayoutProperty(PropertyKey::LineJoin, bucket_desc.layout, layout.join, z);
applyLayoutProperty(PropertyKey::LineMiterLimit, bucket_desc.layout, layout.miter_limit, z);
applyLayoutProperty(PropertyKey::LineRoundLimit, bucket_desc.layout, layout.round_limit, z);
applyLayoutProperty(PropertyKey::LineCap, styleBucket.layout, layout.cap, z);
applyLayoutProperty(PropertyKey::LineJoin, styleBucket.layout, layout.join, z);
applyLayoutProperty(PropertyKey::LineMiterLimit, styleBucket.layout, layout.miter_limit, z);
applyLayoutProperty(PropertyKey::LineRoundLimit, styleBucket.layout, layout.round_limit, z);
addBucketGeometries(bucket, layer, bucket_desc.filter);
return bucket->hasData() ? std::move(bucket) : nullptr;
addBucketGeometries(bucket, layer, styleBucket.filter);
insertBucket(styleBucket.name, std::move(bucket));
}
std::unique_ptr<Bucket> TileWorker::createCircleBucket(const GeometryTileLayer& layer,
const StyleBucket& bucket_desc) {
void TileWorker::createCircleBucket(const GeometryTileLayer& layer,
const StyleBucket& styleBucket) {
auto bucket = std::make_unique<CircleBucket>();
// Circle does not have layout properties to apply.
addBucketGeometries(bucket, layer, bucket_desc.filter);
return bucket->hasData() ? std::move(bucket) : nullptr;
addBucketGeometries(bucket, layer, styleBucket.filter);
insertBucket(styleBucket.name, std::move(bucket));
}
std::unique_ptr<Bucket> TileWorker::createSymbolBucket(const GeometryTileLayer& layer,
const StyleBucket& bucket_desc) {
void TileWorker::createSymbolBucket(const GeometryTileLayer& layer,
const StyleBucket& styleBucket) {
auto bucket = std::make_unique<SymbolBucket>(id.overscaling, id.z);
const float z = id.z;
auto& layout = bucket->layout;
applyLayoutProperty(PropertyKey::SymbolPlacement, bucket_desc.layout, layout.placement, z);
applyLayoutProperty(PropertyKey::SymbolPlacement, styleBucket.layout, layout.placement, z);
if (layout.placement == PlacementType::Line) {
layout.icon.rotation_alignment = RotationAlignmentType::Map;
layout.text.rotation_alignment = RotationAlignmentType::Map;
};
applyLayoutProperty(PropertyKey::SymbolSpacing, bucket_desc.layout, layout.spacing, z);
applyLayoutProperty(PropertyKey::SymbolAvoidEdges, bucket_desc.layout, layout.avoid_edges, z);
applyLayoutProperty(PropertyKey::SymbolSpacing, styleBucket.layout, layout.spacing, z);
applyLayoutProperty(PropertyKey::SymbolAvoidEdges, styleBucket.layout, layout.avoid_edges, z);
applyLayoutProperty(PropertyKey::IconAllowOverlap, bucket_desc.layout, layout.icon.allow_overlap, z);
applyLayoutProperty(PropertyKey::IconIgnorePlacement, bucket_desc.layout, layout.icon.ignore_placement, z);
applyLayoutProperty(PropertyKey::IconOptional, bucket_desc.layout, layout.icon.optional, z);
applyLayoutProperty(PropertyKey::IconRotationAlignment, bucket_desc.layout, layout.icon.rotation_alignment, z);
applyLayoutProperty(PropertyKey::IconImage, bucket_desc.layout, layout.icon.image, z);
applyLayoutProperty(PropertyKey::IconPadding, bucket_desc.layout, layout.icon.padding, z);
applyLayoutProperty(PropertyKey::IconRotate, bucket_desc.layout, layout.icon.rotate, z);
applyLayoutProperty(PropertyKey::IconKeepUpright, bucket_desc.layout, layout.icon.keep_upright, z);
applyLayoutProperty(PropertyKey::IconOffset, bucket_desc.layout, layout.icon.offset, z);
applyLayoutProperty(PropertyKey::IconAllowOverlap, styleBucket.layout, layout.icon.allow_overlap, z);
applyLayoutProperty(PropertyKey::IconIgnorePlacement, styleBucket.layout, layout.icon.ignore_placement, z);
applyLayoutProperty(PropertyKey::IconOptional, styleBucket.layout, layout.icon.optional, z);
applyLayoutProperty(PropertyKey::IconRotationAlignment, styleBucket.layout, layout.icon.rotation_alignment, z);
applyLayoutProperty(PropertyKey::IconImage, styleBucket.layout, layout.icon.image, z);
applyLayoutProperty(PropertyKey::IconPadding, styleBucket.layout, layout.icon.padding, z);
applyLayoutProperty(PropertyKey::IconRotate, styleBucket.layout, layout.icon.rotate, z);
applyLayoutProperty(PropertyKey::IconKeepUpright, styleBucket.layout, layout.icon.keep_upright, z);
applyLayoutProperty(PropertyKey::IconOffset, styleBucket.layout, layout.icon.offset, z);
applyLayoutProperty(PropertyKey::TextRotationAlignment, bucket_desc.layout, layout.text.rotation_alignment, z);
applyLayoutProperty(PropertyKey::TextField, bucket_desc.layout, layout.text.field, z);
applyLayoutProperty(PropertyKey::TextFont, bucket_desc.layout, layout.text.font, z);
applyLayoutProperty(PropertyKey::TextMaxWidth, bucket_desc.layout, layout.text.max_width, z);
applyLayoutProperty(PropertyKey::TextLineHeight, bucket_desc.layout, layout.text.line_height, z);
applyLayoutProperty(PropertyKey::TextLetterSpacing, bucket_desc.layout, layout.text.letter_spacing, z);
applyLayoutProperty(PropertyKey::TextMaxAngle, bucket_desc.layout, layout.text.max_angle, z);
applyLayoutProperty(PropertyKey::TextRotate, bucket_desc.layout, layout.text.rotate, z);
applyLayoutProperty(PropertyKey::TextPadding, bucket_desc.layout, layout.text.padding, z);
applyLayoutProperty(PropertyKey::TextIgnorePlacement, bucket_desc.layout, layout.text.ignore_placement, z);
applyLayoutProperty(PropertyKey::TextOptional, bucket_desc.layout, layout.text.optional, z);
applyLayoutProperty(PropertyKey::TextJustify, bucket_desc.layout, layout.text.justify, z);
applyLayoutProperty(PropertyKey::TextAnchor, bucket_desc.layout, layout.text.anchor, z);
applyLayoutProperty(PropertyKey::TextKeepUpright, bucket_desc.layout, layout.text.keep_upright, z);
applyLayoutProperty(PropertyKey::TextTransform, bucket_desc.layout, layout.text.transform, z);
applyLayoutProperty(PropertyKey::TextOffset, bucket_desc.layout, layout.text.offset, z);
applyLayoutProperty(PropertyKey::TextAllowOverlap, bucket_desc.layout, layout.text.allow_overlap, z);
applyLayoutProperty(PropertyKey::TextRotationAlignment, styleBucket.layout, layout.text.rotation_alignment, z);
applyLayoutProperty(PropertyKey::TextField, styleBucket.layout, layout.text.field, z);
applyLayoutProperty(PropertyKey::TextFont, styleBucket.layout, layout.text.font, z);
applyLayoutProperty(PropertyKey::TextMaxWidth, styleBucket.layout, layout.text.max_width, z);
applyLayoutProperty(PropertyKey::TextLineHeight, styleBucket.layout, layout.text.line_height, z);
applyLayoutProperty(PropertyKey::TextLetterSpacing, styleBucket.layout, layout.text.letter_spacing, z);
applyLayoutProperty(PropertyKey::TextMaxAngle, styleBucket.layout, layout.text.max_angle, z);
applyLayoutProperty(PropertyKey::TextRotate, styleBucket.layout, layout.text.rotate, z);
applyLayoutProperty(PropertyKey::TextPadding, styleBucket.layout, layout.text.padding, z);
applyLayoutProperty(PropertyKey::TextIgnorePlacement, styleBucket.layout, layout.text.ignore_placement, z);
applyLayoutProperty(PropertyKey::TextOptional, styleBucket.layout, layout.text.optional, z);
applyLayoutProperty(PropertyKey::TextJustify, styleBucket.layout, layout.text.justify, z);
applyLayoutProperty(PropertyKey::TextAnchor, styleBucket.layout, layout.text.anchor, z);
applyLayoutProperty(PropertyKey::TextKeepUpright, styleBucket.layout, layout.text.keep_upright, z);
applyLayoutProperty(PropertyKey::TextTransform, styleBucket.layout, layout.text.transform, z);
applyLayoutProperty(PropertyKey::TextOffset, styleBucket.layout, layout.text.offset, z);
applyLayoutProperty(PropertyKey::TextAllowOverlap, styleBucket.layout, layout.text.allow_overlap, z);
applyLayoutProperty(PropertyKey::IconSize, bucket_desc.layout, layout.icon.size, z + 1);
applyLayoutProperty(PropertyKey::IconSize, bucket_desc.layout, layout.icon.max_size, 18);
applyLayoutProperty(PropertyKey::TextSize, bucket_desc.layout, layout.text.size, z + 1);
applyLayoutProperty(PropertyKey::TextSize, bucket_desc.layout, layout.text.max_size, 18);
applyLayoutProperty(PropertyKey::IconSize, styleBucket.layout, layout.icon.size, z + 1);
applyLayoutProperty(PropertyKey::IconSize, styleBucket.layout, layout.icon.max_size, 18);
applyLayoutProperty(PropertyKey::TextSize, styleBucket.layout, layout.text.size, z + 1);
applyLayoutProperty(PropertyKey::TextSize, styleBucket.layout, layout.text.max_size, 18);
if (bucket->needsDependencies(layer, bucket_desc.filter, *style.glyphStore, *style.sprite)) {
partialParse = true;
bucket->parseFeatures(layer, styleBucket.filter);
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 {
bucket->addFeatures(reinterpret_cast<uintptr_t>(this), *style.spriteAtlas,
*style.glyphAtlas, *style.glyphStore, *collisionTile);
insertBucket(styleBucket.name, std::move(bucket));
}
}
void TileWorker::insertBucket(const std::string& name, std::unique_ptr<Bucket> bucket) {
if (bucket->hasData()) {
result.buckets.emplace_back(name, std::move(bucket));
}
// We do not proceed if the parser is in a "partial" state because
// the layer ordering needs to be respected when calculating text
// collisions. Although, at this point, we requested all the resources
// needed by this tile.
if (partialParse) {
return nullptr;
}
bucket->addFeatures(reinterpret_cast<uintptr_t>(this), *style.spriteAtlas, *style.glyphAtlas,
*style.glyphStore, *collisionTile);
return bucket->hasData() ? std::move(bucket) : nullptr;
}