Merge branch 'master' into outdoors-style

Conflicts:
	bin/style.js
	src/map/map.cpp
This commit is contained in:
Konstantin Käfer
2014-05-13 16:18:52 +02:00
64 changed files with 2047 additions and 934 deletions

View File

@@ -5,6 +5,9 @@
#include <llmr/style/sprite.hpp>
#include <llmr/util/transition.hpp>
#include <llmr/util/time.hpp>
#include <llmr/util/math.hpp>
#include <llmr/util/clip_ids.hpp>
#include <llmr/util/string.hpp>
#include <algorithm>
#include <memory>
@@ -138,7 +141,6 @@ void Map::setup() {
sources.emplace("outdoors",
std::unique_ptr<Source>(new Source(*this,
painter,
texturepool,
"http://a.gl-api-us-east-1.tilestream.net/v3/mapbox.mapbox-terrain-v1,mapbox.mapbox-streets-v42-dev/%d/%d/%d.gl.pbf",
Source::Type::vector,
{{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }},
@@ -150,7 +152,6 @@ void Map::setup() {
sources.emplace("satellite",
std::unique_ptr<Source>(new Source(*this,
painter,
texturepool,
"https://a.tiles.mapbox.com/v3/justin.hh0gkdfm/%d/%d/%d%s.png256",
Source::Type::raster,
{{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 }},
@@ -331,7 +332,11 @@ void Map::toggleRaster() {
if (satellite_source.enabled) {
satellite_source.enabled = false;
style.appliedClasses.erase(style.appliedClasses.find("satellite"));
auto style_class = style.appliedClasses.find("satellite");
if (style_class != style.appliedClasses.end()) {
style.appliedClasses.erase(style_class);
}
} else {
satellite_source.enabled = true;
style.appliedClasses.insert("satellite");
@@ -350,6 +355,28 @@ void Map::updateTiles() {
}
}
void Map::updateClippingIDs() {
std::forward_list<Tile::ID> ids;
for (auto &pair : sources) {
const std::unique_ptr<Source> &source = pair.second;
if (source->enabled) {
ids.splice_after(ids.before_begin(), source->getIDs());
}
}
const std::map<Tile::ID, ClipID> clipIDs = computeClipIDs(ids);
for (auto &pair : sources) {
const std::unique_ptr<Source> &source = pair.second;
if (source->enabled) {
source->updateClipIDs(clipIDs);
}
}
}
void Map::prepare() {
view.make_active();
@@ -366,14 +393,14 @@ void Map::prepare() {
style.sprite->load(kSpriteURL);
}
if (style.sprite && style.sprite->raster->isLoaded() && !style.sprite->raster->textured) {
style.sprite->raster->setTexturepool(&texturepool);
}
updateTiles();
updateClippingIDs();
}
void Map::render() {
std::vector<std::string> debug;
painter.clear();
painter.resize();
@@ -386,28 +413,133 @@ void Map::render() {
painter.prepareClippingMask();
bool is_baselayer = true;
size_t source_id = 0;
for (auto &pair : sources) {
Source &source = *pair.second;
source.prepare_render(state, is_baselayer);
is_baselayer = source.enabled ? false : (true && is_baselayer);
size_t count = source.prepareRender(state);
debug.emplace_back(util::sprintf<100>("source %d: %d\n", source_id, count));
source_id++;
}
painter.finishClippingMask();
// Then, render each source's tiles. TODO: handle more than one
// vector source.
// Render per layer in the stylesheet.
strata_thickness = 1.0f / (style.layerCount() + 1);
strata = 0;
// - FIRST PASS ------------------------------------------------------------
// Render everything top-to-bottom by using reverse iterators. Render opaque
// objects first.
painter.startOpaquePass();
renderLayers(style.layers, Opaque);
painter.endPass();
// - SECOND PASS -----------------------------------------------------------
// Make a second pass, rendering translucent objects. This time, we render
// bottom-to-top.
--strata;
painter.startTranslucentPass();
renderLayers(style.layers, Translucent);
painter.endPass();
// Finalize the rendering, e.g. by calling debug render calls per tile.
// This guarantees that we have at least one function per tile called.
// When only rendering layers via the stylesheet, it's possible that we don't
// ever visit a tile during rendering.
for (auto &pair : sources) {
Source &source = *pair.second;
source.render(animationTime);
const std::string &name = pair.first;
gl::group group(std::string("debug ") + name);
source.finishRender();
}
painter.renderMatte();
painter.renderDebugText(debug);
// Schedule another rerender when we definitely need a next frame.
if (transform.needsTransition()) {
update();
}
}
void Map::renderLayers(const std::vector<LayerDescription>& layers, RenderPass pass) {
if (pass == Opaque) {
typedef std::vector<LayerDescription>::const_reverse_iterator riterator;
for (riterator it = layers.rbegin(), end = layers.rend(); it != end; ++it, ++strata) {
painter.setStrata(strata * strata_thickness);
renderLayer(*it, pass);
}
} else if (pass == Translucent) {
typedef std::vector<LayerDescription>::const_iterator iterator;
for (iterator it = layers.begin(), end = layers.end(); it != end; ++it, --strata) {
painter.setStrata(strata * strata_thickness);
renderLayer(*it, pass);
}
} else {
throw std::runtime_error("unknown render pass");
}
}
template <typename Styles>
bool is_invisible(const Styles &styles, const LayerDescription &layer_desc) {
auto it = styles.find(layer_desc.name);
if (it == styles.end()) return true;
const auto &properties = it->second;
if (!properties.enabled) return true;
if (properties.opacity <= 0) return true;
return false;
}
void Map::renderLayer(const LayerDescription& layer_desc, RenderPass pass) {
if (layer_desc.child_layer.size()) {
gl::group group(std::string("group: ") + layer_desc.name);
// This is a layer group.
// TODO: create framebuffer
renderLayers(layer_desc.child_layer, pass);
// TODO: render framebuffer on previous framebuffer
} else {
// This is a singular layer. Try to find the bucket associated with
// this layer and render it.
auto bucket_it = style.buckets.find(layer_desc.bucket_name);
if (bucket_it != style.buckets.end()) {
const BucketDescription &bucket_desc = bucket_it->second;
// Abort early if we can already deduce from the bucket type that
// we're not going to render anything anyway during this pass.
switch (bucket_desc.type) {
case BucketType::Fill:
if (is_invisible(style.computed.fills, layer_desc)) return;
break;
case BucketType::Line:
if (pass == Opaque) return;
if (is_invisible(style.computed.lines, layer_desc)) return;
break;
case BucketType::Point:
if (pass == Opaque) return;
if (is_invisible(style.computed.points, layer_desc)) return;
break;
case BucketType::Text:
if (pass == Opaque) return;
if (is_invisible(style.computed.texts, layer_desc)) return;
break;
case BucketType::Raster:
if (pass == Translucent) return;
if (is_invisible(style.computed.lines, layer_desc)) return;
break;
default:
break;
}
// Find the source and render all tiles in it.
auto source_it = sources.find(bucket_desc.source_name);
if (source_it != sources.end()) {
const std::unique_ptr<Source> &source = source_it->second;
source->render(layer_desc, bucket_desc);
}
}
}
}

View File

@@ -0,0 +1,34 @@
#include <llmr/map/map.hpp>
#include <llmr/map/raster_tile_data.hpp>
#include <llmr/style/layer_description.hpp>
using namespace llmr;
RasterTileData::RasterTileData(Tile::ID id, Map &map, const std::string url)
: TileData(id, map, url),
bucket(map.getTexturepool()) {
}
RasterTileData::~RasterTileData() {
}
void RasterTileData::parse() {
if (state != State::loaded) {
return;
}
if (bucket.setImage(data)) {
state = State::parsed;
} else {
state = State::invalid;
}
}
void RasterTileData::render(Painter &painter, const LayerDescription& layer_desc) {
bucket.render(painter, layer_desc.name, id);
}
bool RasterTileData::hasData(const LayerDescription& layer_desc) const {
return bucket.hasData();
}

View File

@@ -7,17 +7,21 @@
#include <llmr/util/string.hpp>
#include <llmr/util/texturepool.hpp>
#include <llmr/util/vec.hpp>
#include <llmr/util/std.hpp>
#include <llmr/geometry/glyph_atlas.hpp>
#include <llmr/map/vector_tile_data.hpp>
#include <llmr/map/raster_tile_data.hpp>
using namespace llmr;
Source::Source(Map &map, Painter &painter, Texturepool &texturepool,
Source::Source(Map &map, Painter &painter,
const char *url, Source::Type type, std::vector<uint32_t> zooms, uint32_t tile_size,
uint32_t min_zoom, uint32_t max_zoom, bool enabled)
: enabled(enabled),
map(map),
painter(painter),
texturepool(texturepool),
type(type),
zooms(zooms),
url(url),
@@ -29,45 +33,70 @@ bool Source::update() {
return updateTiles();
}
void Source::prepare_render(const TransformState &transform, bool is_baselayer) {
void Source::updateClipIDs(const std::map<Tile::ID, ClipID> &mapping) {
std::for_each(tiles.begin(), tiles.end(), [&mapping](std::pair<const Tile::ID, std::unique_ptr<Tile>> &pair) {
Tile &tile = *pair.second;
auto it = mapping.find(tile.id);
if (it != mapping.end()) {
tile.clip = it->second;
} else {
tile.clip = ClipID {};
}
});
}
size_t Source::prepareRender(const TransformState &transform) {
if (!enabled) return 0;
size_t masks = 0;
for (std::pair<const Tile::ID, std::unique_ptr<Tile>> &pair : tiles) {
Tile &tile = *pair.second;
gl::group group(util::sprintf<32>("mask %d/%d/%d", tile.id.z, tile.id.y, tile.id.z));
transform.matrixFor(tile.matrix, tile.id);
matrix::multiply(tile.matrix, painter.projMatrix, tile.matrix);
painter.drawClippingMask(tile.matrix, tile.clip);
masks++;
}
return masks;
}
void Source::render(const LayerDescription& layer_desc, const BucketDescription &bucket_desc) {
if (!enabled) return;
uint8_t i = 1;
for (Tile& tile : tiles) {
gl::group group(std::string("layer: ") + layer_desc.name);
for (std::pair<const Tile::ID, std::unique_ptr<Tile>> &pair : tiles) {
Tile &tile = *pair.second;
if (tile.data && tile.data->state == TileData::State::parsed) {
transform.matrixFor(tile.matrix, tile.id);
matrix::multiply(tile.matrix, painter.projMatrix, tile.matrix);
tile.clip_id = i++;
if (is_baselayer) {
painter.drawClippingMask(tile.matrix, tile.clip_id);
}
painter.renderTileLayer(tile, layer_desc);
}
}
}
void Source::render(time animationTime) {
void Source::finishRender() {
if (!enabled) return;
for (const Tile& tile : tiles) {
if (tile.data && tile.data->state == TileData::State::parsed) {
if (type == Type::raster) {
std::shared_ptr<Raster> raster = tile.data->raster;
if (raster && !raster->textured) {
raster->setTexturepool(&texturepool);
raster->beginFadeInTransition();
}
if (raster && raster->needsTransition()) {
raster->updateTransitions(animationTime);
}
}
painter.render(tile);
}
for (std::pair<const Tile::ID, std::unique_ptr<Tile>> &pair : tiles) {
Tile &tile = *pair.second;
painter.renderTileDebug(tile);
}
}
std::forward_list<Tile::ID> Source::getIDs() const {
std::forward_list<Tile::ID> ptrs;
std::transform(tiles.begin(), tiles.end(), std::front_inserter(ptrs), [](const std::pair<const Tile::ID, std::unique_ptr<Tile>> &pair) {
Tile &tile = *pair.second;
return tile.id;
});
return ptrs;
}
TileData::State Source::hasTile(const Tile::ID& id) {
for (const Tile& tile : tiles) {
auto it = tiles.find(id);
if (it != tiles.end()) {
Tile &tile = *it->second;
if (tile.id == id && tile.data) {
return tile.data->state;
}
@@ -83,20 +112,17 @@ TileData::State Source::addTile(const Tile::ID& id) {
return state;
}
tiles.emplace_front(id);
Tile& new_tile = tiles.front();
auto pos = tiles.emplace(id, std::make_unique<Tile>(id));
Tile& new_tile = *pos.first->second;
// We couldn't find the tile in the list. Create a new one.
// Try to find the associated TileData object.
const Tile::ID normalized_id = id.normalized();
auto it = std::find_if(tile_data.begin(), tile_data.end(), [normalized_id](const std::weak_ptr<TileData>& tile_data) {
return !tile_data.expired() && tile_data.lock()->id == normalized_id;
});
auto it = tile_data.find(normalized_id);
if (it != tile_data.end()) {
// Create a shared_ptr handle. Note that this might be empty!
new_tile.data = it->lock();
new_tile.data = it->second.lock();
}
if (new_tile.data && new_tile.data->state == TileData::State::obsolete) {
@@ -111,13 +137,14 @@ TileData::State Source::addTile(const Tile::ID& id) {
if (type == Source::Type::vector) {
formed_url = util::sprintf<256>(url, normalized_id.z, normalized_id.x, normalized_id.y);
new_tile.data = std::make_shared<VectorTileData>(normalized_id, map, formed_url);
} else {
formed_url = util::sprintf<256>(url, normalized_id.z, normalized_id.x, normalized_id.y, (map.getState().getPixelRatio() > 1.0 ? "@2x" : ""));
new_tile.data = std::make_shared<RasterTileData>(normalized_id, map, formed_url);
}
new_tile.data = std::make_shared<TileData>(normalized_id, map, formed_url, (type == Source::Type::raster));
new_tile.data->request();
tile_data.push_front(new_tile.data);
tile_data.emplace(new_tile.data->id, new_tile.data);
}
return new_tile.data->state;
@@ -230,31 +257,26 @@ bool Source::updateTiles() {
// Remove tiles that we definitely don't need, i.e. tiles that are not on
// the required list.
std::forward_list<Tile::ID> retain_data;
tiles.remove_if([&retain, &retain_data, &changed](const Tile & tile) {
std::set<Tile::ID> retain_data;
std::erase_if(tiles, [&retain, &retain_data, &changed](std::pair<const Tile::ID, std::unique_ptr<Tile>> &pair) {
Tile &tile = *pair.second;
bool obsolete = std::find(retain.begin(), retain.end(), tile.id) == retain.end();
if (obsolete) {
changed = true;
} else {
retain_data.push_front(tile.data->id);
retain_data.insert(tile.data->id);
}
return obsolete;
});
// Sort tiles by zoom level, front to back.
// We're painting front-to-back, so we want to draw more detailed tiles first
// before filling in other parts with lower zoom levels.
tiles.sort([](const Tile & a, const Tile & b) {
return a.id.z > b.id.z;
});
// Remove all the expired pointers from the list.
tile_data.remove_if([&retain_data](const std::weak_ptr<TileData>& tile_data) {
const std::shared_ptr<TileData> tile = tile_data.lock();
// Remove all the expired pointers from the set.
std::erase_if(tile_data, [&retain_data](std::pair<const Tile::ID, std::weak_ptr<TileData>> &pair) {
const std::shared_ptr<TileData> tile = pair.second.lock();
if (!tile) {
return true;
}
bool obsolete = std::find(retain_data.begin(), retain_data.end(), tile->id) == retain_data.end();
bool obsolete = retain_data.find(tile->id) == retain_data.end();
if (obsolete) {
tile->cancel();
return true;

View File

@@ -4,6 +4,8 @@
using namespace llmr;
#include <iostream>
Tile::Tile(const ID& id)
: id(id) {
}
@@ -31,14 +33,19 @@ std::forward_list<Tile::ID> Tile::ID::children(int32_t child_z) const {
return children;
}
void Tile::ID::normalize() {
Tile::ID Tile::ID::normalized() const {
int32_t dim = pow(2, z);
while (x < 0) x += dim;
while (x >= dim) x -= dim;
int32_t nx = x, ny = y;
while (nx < 0) nx += dim;
while (nx >= dim) nx -= dim;
return ID { z, nx, ny };
}
Tile::ID Tile::ID::normalized() const {
ID pos(z, x, y);
pos.normalize();
return pos;
bool Tile::ID::isChildOf(const Tile::ID &parent) const {
if (parent.z >= z || parent.w != w) {
return false;
}
int32_t scale = pow(2, z - parent.z);
return parent.x == ((x < 0 ? x - scale + 1 : x) / scale) &&
parent.y == y / scale;
}

View File

@@ -1,33 +1,22 @@
#include <llmr/map/tile_data.hpp>
#include <llmr/map/tile_parser.hpp>
#include <llmr/map/map.hpp>
#include <llmr/geometry/fill_buffer.hpp>
#include <llmr/geometry/line_buffer.hpp>
#include <llmr/geometry/point_buffer.hpp>
#include <llmr/geometry/text_buffer.hpp>
#include <llmr/geometry/elements_buffer.hpp>
#include <llmr/util/raster.hpp>
#include <llmr/util/string.hpp>
#include <llmr/util/uv.hpp>
using namespace llmr;
TileData::TileData(Tile::ID id, Map &map, const std::string url, const bool is_raster)
TileData::TileData(Tile::ID id, Map &map, const std::string url)
: id(id),
state(State::initial),
raster(),
map(map),
url(url),
is_raster(is_raster),
map(map) {
debugBucket(debugFontBuffer) {
// Initialize tile debug coordinates
const std::string str = util::sprintf<32>("%d/%d/%d", id.z, id.x, id.y);
debugFontBuffer.addText(str.c_str(), 50, 200, 5);
}
TileData::~TileData() {
map.getGlyphAtlas().removeGlyphs((uint64_t)id);
cancel();
}
@@ -51,7 +40,7 @@ void TileData::request() {
tile->data.swap(res->body);
// Schedule tile parsing in another thread
tile->scheduleParsing();
tile->reparse();
} else {
fprintf(stderr, "[%s] tile loading failed: %d, %s\n", tile->url.c_str(), res->code, res->error_message.c_str());
}
@@ -65,7 +54,7 @@ void TileData::cancel() {
}
}
void TileData::scheduleParsing() {
void TileData::reparse() {
// We're creating a new work request. The work request deletes itself after it executed
// the after work handler
new uv::work<std::shared_ptr<TileData>>(
@@ -74,24 +63,3 @@ void TileData::scheduleParsing() {
[](std::shared_ptr<TileData> &tile) { tile->map.update(); },
shared_from_this());
}
void TileData::parse() {
if (state != State::loaded) {
return;
}
try {
// Parsing creates state that is encapsulated in TileParser. While parsing,
// the TileParser object writes results into this objects. All other state
// is going to be discarded afterwards.
TileParser parser(data, *this, map.getStyle(), map.getGlyphAtlas(), is_raster);
} catch (const std::exception& ex) {
fprintf(stderr, "[%p] exception [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what());
cancel();
return;
}
if (state != State::obsolete) {
state = State::parsed;
}
}

View File

@@ -14,14 +14,13 @@
using namespace llmr;
TileParser::TileParser(const std::string& data, TileData& tile, const Style& style, GlyphAtlas& glyphAtlas, bool is_raster)
: vector_data(is_raster ? pbf(0, 0) : pbf((const uint8_t *)data.data(), data.size())),
raster_data(is_raster ? data : ""),
TileParser::TileParser(const std::string& data, VectorTileData& tile, const Style& style, GlyphAtlas& glyphAtlas)
: vector_data(pbf((const uint8_t *)data.data(), data.size())),
tile(tile),
style(style),
glyphAtlas(glyphAtlas),
placement(is_raster ? 0 : tile.id.z) {
if (!is_raster) parseGlyphs();
placement(tile.id.z) {
parseGlyphs();
parseStyleLayers(style.layers);
}
@@ -37,7 +36,7 @@ void TileParser::parseGlyphs() {
GlyphPositions &glyphs = faces[name];
for (const VectorTileGlyph &glyph : face.glyphs) {
const Rect<uint16_t> rect =
glyphAtlas.addGlyph((uint64_t)tile.id, name, glyph);
glyphAtlas.addGlyph(tile.id.to_uint64(), name, glyph);
glyphs.emplace(glyph.id, Glyph{rect, glyph.metrics});
}
}
@@ -85,31 +84,22 @@ void TileParser::parseStyleLayers(const std::vector<LayerDescription>& layers) {
}
std::unique_ptr<Bucket> TileParser::createBucket(const BucketDescription& bucket_desc) {
if (bucket_desc.type == BucketType::Raster) {
if (raster_data.length()) {
tile.raster = std::make_shared<Raster>();
tile.raster->setData(raster_data);
tile.raster->load();
}
return createRasterBucket(bucket_desc);
} else {
auto layer_it = vector_data.layers.find(bucket_desc.source_layer);
if (layer_it != vector_data.layers.end()) {
const VectorTileLayer& layer = layer_it->second;
if (bucket_desc.type == BucketType::Fill) {
return createFillBucket(layer, bucket_desc);
} else if (bucket_desc.type == BucketType::Line) {
return createLineBucket(layer, bucket_desc);
} else if (bucket_desc.type == BucketType::Point) {
return createPointBucket(layer, bucket_desc);
} else if (bucket_desc.type == BucketType::Text) {
return createTextBucket(layer, bucket_desc);
} else {
throw std::runtime_error("unknown bucket type");
}
auto layer_it = vector_data.layers.find(bucket_desc.source_layer);
if (layer_it != vector_data.layers.end()) {
const VectorTileLayer& layer = layer_it->second;
if (bucket_desc.type == BucketType::Fill) {
return createFillBucket(layer, bucket_desc);
} else if (bucket_desc.type == BucketType::Line) {
return createLineBucket(layer, bucket_desc);
} else if (bucket_desc.type == BucketType::Point) {
return createPointBucket(layer, bucket_desc);
} else if (bucket_desc.type == BucketType::Text) {
return createTextBucket(layer, bucket_desc);
} else {
// The layer specified in the bucket does not exist. Do nothing.
throw std::runtime_error("unknown bucket type");
}
} else {
// The layer specified in the bucket does not exist. Do nothing.
}
return nullptr;
@@ -183,10 +173,3 @@ std::unique_ptr<Bucket> TileParser::createTextBucket(const VectorTileLayer& laye
return std::move(bucket);
}
std::unique_ptr<Bucket> TileParser::createRasterBucket(const BucketDescription& bucket_desc) {
std::unique_ptr<RasterBucket> bucket = std::make_unique<RasterBucket>(bucket_desc);
// Raster buckets are just empty dummies so that they behave
// similarly to vector buckets in styling configurations.
return obsolete() ? nullptr : std::move(bucket);
}

View File

@@ -0,0 +1,55 @@
#include <llmr/map/vector_tile_data.hpp>
#include <llmr/map/map.hpp>
#include <llmr/map/tile_parser.hpp>
using namespace llmr;
VectorTileData::VectorTileData(Tile::ID id, Map &map, const std::string url)
: TileData(id, map, url) {
}
VectorTileData::~VectorTileData() {
map.getGlyphAtlas().removeGlyphs(id.to_uint64());
}
void VectorTileData::parse() {
if (state != State::loaded) {
return;
}
try {
// Parsing creates state that is encapsulated in TileParser. While parsing,
// the TileParser object writes results into this objects. All other state
// is going to be discarded afterwards.
TileParser parser(data, *this, map.getStyle(), map.getGlyphAtlas());
} catch (const std::exception& ex) {
fprintf(stderr, "[%p] exception [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what());
cancel();
return;
}
if (state != State::obsolete) {
state = State::parsed;
}
}
void VectorTileData::render(Painter &painter, const LayerDescription& layer_desc) {
auto databucket_it = buckets.find(layer_desc.bucket_name);
if (databucket_it != buckets.end()) {
assert(databucket_it->second);
databucket_it->second->render(painter, layer_desc.name, id);
}
}
bool VectorTileData::hasData(const LayerDescription& layer_desc) const {
if (state != State::parsed) return false;
auto databucket_it = buckets.find(layer_desc.bucket_name);
if (databucket_it != buckets.end()) {
assert(databucket_it->second);
return databucket_it->second->hasData();
} else {
return false;
}
}