Merge branch 'master' into libuv010

Conflicts:
	setup-libraries.sh
This commit is contained in:
Mike Morris
2014-08-26 16:55:08 -04:00
29 changed files with 440 additions and 572 deletions

View File

@@ -511,18 +511,12 @@ void Map::updateTiles() {
}
void Map::updateRenderState() {
std::forward_list<Tile::ID> ids;
// Update all clipping IDs.
ClipIDGenerator generator;
for (const std::shared_ptr<StyleSource> &source : getActiveSources()) {
ids.splice_after(ids.before_begin(), source->source->getIDs());
generator.update(source->source->getLoadedTiles());
source->source->updateMatrices(painter.projMatrix, state);
}
const std::map<Tile::ID, ClipID> clipIDs = computeClipIDs(ids);
for (const std::shared_ptr<StyleSource> &source : getActiveSources()) {
source->source->updateClipIDs(clipIDs);
}
}
void Map::prepare() {

View File

@@ -121,7 +121,6 @@ void Source::finishRender(Painter &painter) {
}
}
std::forward_list<Tile::ID> Source::getIDs() const {
std::forward_list<Tile::ID> ptrs;
@@ -132,6 +131,18 @@ std::forward_list<Tile::ID> Source::getIDs() const {
return ptrs;
}
std::forward_list<Tile *> Source::getLoadedTiles() const {
std::forward_list<Tile *> ptrs;
auto it = ptrs.before_begin();
for (const auto &pair : tiles) {
if (pair.second->data->ready()) {
it = ptrs.insert_after(it, pair.second.get());
}
}
return ptrs;
}
TileData::State Source::hasTile(const Tile::ID& id) {
auto it = tiles.find(id);
if (it != tiles.end()) {

View File

@@ -169,9 +169,9 @@ void Painter::setStrata(float value) {
}
void Painter::prepareTile(const Tile& tile) {
GLint id = (GLint)tile.clip.mask.to_ulong();
GLuint mask = clipMask[tile.clip.length];
glStencilFunc(GL_EQUAL, id, mask);
const GLint ref = (GLint)tile.clip.reference.to_ulong();
const GLuint mask = (GLuint)tile.clip.mask.to_ulong();
glStencilFunc(GL_EQUAL, ref, mask);
}
void Painter::renderTileLayer(const Tile& tile, std::shared_ptr<StyleLayer> layer_desc, const mat4 &matrix) {

View File

@@ -14,7 +14,6 @@ void Painter::drawClippingMasks(const std::set<std::shared_ptr<StyleSource>> &so
depthMask(false);
glColorMask(false, false, false, false);
depthRange(1.0f, 1.0f);
glStencilMask(0xFF);
coveringPlainArray.bind(*plainShader, tileStencilBuffer, BUFFER_OFFSET(0));
@@ -31,9 +30,10 @@ void Painter::drawClippingMasks(const std::set<std::shared_ptr<StyleSource>> &so
void Painter::drawClippingMask(const mat4& matrix, const ClipID &clip) {
plainShader->setMatrix(matrix);
GLint id = static_cast<GLint>(clip.mask.to_ulong());
GLuint mask = clipMask[clip.length];
glStencilFunc(GL_ALWAYS, id, mask);
const GLint ref = (GLint)(clip.reference.to_ulong());
const GLuint mask = (GLuint)(clip.mask.to_ulong());
glStencilFunc(GL_ALWAYS, ref, mask);
glStencilMask(mask);
glDrawArrays(GL_TRIANGLES, 0, (GLsizei)tileStencilBuffer.index());
}

View File

@@ -75,11 +75,10 @@ std::vector<SymbolFeature> SymbolBucket::processFeatures(const VectorTileLayer &
if (text) {
std::string u8string = util::replaceTokens(properties.text.field, feature.properties);
auto &convert = std::use_facet<std::ctype<char>>(std::locale());
if (properties.text.transform == TextTransformType::Uppercase) {
convert.toupper(&u8string[0], &u8string[0] + u8string.size());
u8string = platform::uppercase(u8string);
} else if (properties.text.transform == TextTransformType::Lowercase) {
convert.tolower(&u8string[0], &u8string[0] + u8string.size());
u8string = platform::lowercase(u8string);
}
ft.label = ucs4conv.convert(u8string);
@@ -296,7 +295,7 @@ void SymbolBucket::addFeature(const std::vector<Coordinate> &line, const Shaping
}
// Insert final placement into collision tree and add glyphs/icons to buffers
if (glyphScale) {
if (glyphScale && std::isfinite(glyphScale)) {
if (!properties.text.ignore_placement) {
collision.insert(glyphPlacement.boxes, anchor, glyphScale, glyphRange,
horizontalText);
@@ -304,7 +303,7 @@ void SymbolBucket::addFeature(const std::vector<Coordinate> &line, const Shaping
if (inside) addSymbols(text, glyphPlacement.shapes, glyphScale, glyphRange);
}
if (iconScale) {
if (iconScale && std::isfinite(iconScale)) {
if (!properties.icon.ignore_placement) {
collision.insert(iconPlacement.boxes, anchor, iconScale, iconRange, horizontalIcon);
}

View File

@@ -166,10 +166,10 @@ float Collision::getPlacementScale(const GlyphBoxes &glyphs, float minPlacementS
float s4 = (ob.br.y - nb.tl.y + padding) /
(na.y - oa.y); // scale at which new box is to the bottom of old box
if (isnan(s1) || isnan(s2)) {
if (std::isnan(s1) || std::isnan(s2)) {
s1 = s2 = 1;
}
if (isnan(s3) || isnan(s4)) {
if (std::isnan(s3) || std::isnan(s4)) {
s3 = s4 = 1;
}

View File

@@ -95,7 +95,7 @@ rotatingRotatingCollisions(const CollisionRect &a, const CollisionRect &b,
std::vector<float> f;
for (size_t i = 0; i < c.size(); i++) {
// Check if they are close enough to collide
if (!isnan(c[i]) && d_sq <= e[i]) {
if (!std::isnan(c[i]) && d_sq <= e[i]) {
// So far, angles have been calulated as relative to the vector
// between anchors.
// Convert the angles to angles from north.

View File

@@ -12,187 +12,85 @@
namespace mbgl {
struct TileHierarchy {
TileHierarchy(Tile::ID id, std::list<TileHierarchy> &&children)
: id(id), children(std::move(children)) {}
ClipIDGenerator::Leaf::Leaf(Tile &tile_) : tile(tile_) {}
const Tile::ID id;
ClipID clip;
std::list<TileHierarchy> children;
};
std::list<TileHierarchy> partition(std::forward_list<Tile::ID> &&array) {
if (array.empty()) {
// We don't have to update the clipping mask because there are no tiles
// anyway.
return {};
}
int8_t minZ = array.begin()->z;
std::list<TileHierarchy> result;
std::forward_list<Tile::ID> remainder;
auto remainder_it = remainder.before_begin();
while (!array.empty()) {
const Tile::ID id = array.front();
array.pop_front();
if (id.z == minZ) {
std::forward_list<Tile::ID> children;
auto children_it = children.before_begin();
array.remove_if([&id, &children, &children_it](const Tile::ID &child) {
if (child.isChildOf(id)) {
children_it = children.insert_after(children_it, child);
return true;
} else {
return false;
}
});
result.emplace_back(id, partition(std::move(children)));
} else {
remainder_it = remainder.insert_after(remainder_it, id);
void ClipIDGenerator::Leaf::add(const Tile::ID &p) {
if (p.isChildOf(tile.id)) {
// Ensure that no already present child is a parent of the new p.
for (const Tile::ID &child : children) {
if (p.isChildOf(child))
return;
}
children.push_front(p);
}
// Concatenate the remainder.
if (!remainder.empty()) {
result.splice(result.begin(), partition(std::move(remainder)));
}
return result;
}
uint8_t prefix(std::list<TileHierarchy> &array, TileHierarchy *parent = nullptr) {
if (array.empty()) {
return 0;
bool ClipIDGenerator::Leaf::operator==(const Leaf &other) const {
return tile.id == other.tile.id && children == other.children;
}
bool ClipIDGenerator::reuseExisting(Leaf &leaf) {
for (const std::vector<Leaf> &pool : pools) {
auto existing = std::find(pool.begin(), pool.end(), leaf);
if (existing != pool.end()) {
leaf.tile.clip = existing->tile.clip;
return true;
}
}
return false;
}
bool all_children_are_immediate = true;
uint8_t max_child_prefix_length = 0;
void ClipIDGenerator::update(std::forward_list<Tile *> tiles) {
Pool pool;
struct Huffman {
explicit Huffman(int prefix_length, TileHierarchy *item)
: prefix_length(prefix_length), children(1, item) {}
uint8_t prefix_length;
std::vector<TileHierarchy *> children;
};
// Create a temporary structure that we use for sorting the prefix tree.
std::list<Huffman> huffman;
std::transform(array.begin(), array.end(), std::back_inserter(huffman), [parent, &all_children_are_immediate, &max_child_prefix_length](TileHierarchy &item) {
uint8_t prefix_length = prefix(item.children, &item);
if (prefix_length > max_child_prefix_length) {
max_child_prefix_length = prefix_length;
}
if (!parent || item.id.z != parent->id.z + 1) {
all_children_are_immediate = false;
}
return Huffman { prefix_length + 1, &item };
tiles.sort([](const Tile *a, const Tile *b) {
return a->id < b->id;
});
while (huffman.size() > 1) {
huffman.sort([](const Huffman &a, const Huffman &b) {
return a.prefix_length < b.prefix_length;
});
Huffman &first = *huffman.begin();
Huffman &second = *(++huffman.begin());
assert(&first != &second);
// Prefix with 0
std::for_each(first.children.begin(), first.children.end(), [](TileHierarchy *child) {
child->clip.mask >>= 1;
child->clip.mask.set(7, false); // noop
child->clip.length++;
});
first.prefix_length++;
// Prefix with 1
std::for_each(second.children.begin(), second.children.end(), [](TileHierarchy *child) {
child->clip.mask >>= 1;
child->clip.mask.set(7, true);
child->clip.length++;
});
second.prefix_length++;
second.children.insert(second.children.end(), first.children.begin(), first.children.end());
second.prefix_length = first.prefix_length + second.prefix_length;
// Remove the first child as we've just merged it into the second version.
huffman.erase(huffman.begin());
}
uint8_t prefix_length = 0;
// Filter out all-zero bits
bool filter_zero = !all_children_are_immediate || array.size() != 4;
for (TileHierarchy &item : array) {
if (filter_zero && !item.clip.mask.any()) {
// Make sure we don't have a prefix that is all zeros.
// item.clip.mask |= (0x80 >> item.length);
item.clip.mask.set(7 - item.clip.length);
item.clip.length++;
const auto end = tiles.end();
for (auto it = tiles.begin(); it != end; it++) {
if (!*it) {
// Handle null pointers.
continue;
}
if (item.clip.length > prefix_length) {
prefix_length = item.clip.length;
Tile &tile = **it;
Leaf clip { tile };
// Try to add all remaining ids as children. We sorted the tile list
// by z earlier, so all preceding items cannot be children of the current
// tile.
for (auto child_it = std::next(it); child_it != end; child_it++) {
clip.add((*child_it)->id);
}
clip.children.sort();
// Loop through all existing pools and try to find a matching ClipID.
if (!reuseExisting(clip)) {
// We haven't found an existing clip ID
pool.push_back(std::move(clip));
}
}
return max_child_prefix_length + prefix_length;
}
if (pool.size()) {
const uint32_t bit_count = util::ceil_log2(pool.size() + 1);
const std::bitset<8> mask = uint64_t(((1 << bit_count) - 1) << bit_offset);
void propagate(std::map<Tile::ID, ClipID> &mapping, std::list<TileHierarchy> &array, const ClipID &parent = ClipID{}) {
for (auto &item : array) {
item.clip.mask >>= parent.length;
item.clip.mask |= parent.mask;
item.clip.length += parent.length;
#if defined(DEBUG)
auto result = mapping.emplace(item.id, item.clip);
assert("Tried to insert a duplicate item" && result.second == true);
#else
mapping.emplace(item.id, item.clip);
#endif
propagate(mapping, item.children, const_cast<const ClipID &>(item.clip));
};
}
void updateClipIDs(const std::list<Tile *> &array) {
std::forward_list<Tile::ID> ids;
std::transform(array.begin(), array.end(), std::front_inserter(ids), [](Tile *item) {
return item->id;
});
const std::map<Tile::ID, ClipID> mapping = computeClipIDs(ids);
std::for_each(array.begin(), array.end(), [&mapping](Tile *item) {
auto it = mapping.find(item->id);
if (it != mapping.end()) {
item->clip = it->second;
} else {
item->clip = ClipID {};
// We are starting our count with 1 since we need at least 1 bit set to distinguish between
// areas without any tiles whatsoever and the current area.
uint8_t count = 1;
for (Leaf &leaf : pool) {
leaf.tile.clip.mask = mask;
leaf.tile.clip.reference = count++ << bit_offset;
}
});
}
std::map<Tile::ID, ClipID> computeClipIDs(std::forward_list<Tile::ID> array) {
// Sort by zoom level and make sure that we don't have duplicate elements.
array.sort();
array.unique();
bit_offset += bit_count;
pools.push_front(std::move(pool));
}
std::list<TileHierarchy> hierarchy = partition(std::move(array));
prefix(hierarchy);
std::map<Tile::ID, ClipID> mapping;
propagate(mapping, hierarchy);
return mapping;
if (bit_offset > 8) {
fprintf(stderr, "stencil mask overflow\n");
}
}
}

25
src/util/math.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include <mbgl/util/math.hpp>
namespace mbgl {
namespace util {
// From http://stackoverflow.com/questions/3272424/compute-fast-log-base-2-ceiling
uint32_t ceil_log2(uint64_t x) {
static const uint64_t t[6] = {0xFFFFFFFF00000000, 0x00000000FFFF0000,
0x000000000000FF00, 0x00000000000000F0,
0x000000000000000C, 0x0000000000000002};
uint32_t y = (((x & (x - 1)) == 0) ? 0 : 1);
uint32_t j = 32;
for (int32_t i = 0; i < 6; i++) {
const uint32_t k = (((x & t[i]) == 0) ? 0 : j);
y += k;
x >>= k;
j >>= 1;
}
return y;
}
}
}

View File

@@ -1,54 +0,0 @@
#include <mbgl/util/threadpool.hpp>
#include <mbgl/util/std.hpp>
#include <thread>
#include <memory>
using namespace mbgl::util;
std::unique_ptr<Threadpool> mbgl::util::threadpool = std::make_unique<Threadpool>(std::thread::hardware_concurrency());
Threadpool::Threadpool(int max_workers)
: max_workers(max_workers) {
}
void Threadpool::add(Callback callback, void *data) {
if (worker_count < max_workers) {
worker_count++;
workers.emplace_front(*this);
}
pthread_mutex_lock(&mutex);
tasks.push(std::make_pair(callback, data));
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&condition);
}
Threadpool::Worker::Worker(Threadpool& pool)
: pool(pool) {
pthread_create(&thread, nullptr, loop, (void *)this);
}
Threadpool::Worker::~Worker() {
pthread_cancel(thread);
}
void *Threadpool::Worker::loop(void *ptr) {
Worker *worker = static_cast<Worker *>(ptr);
Threadpool& pool = worker->pool;
pthread_mutex_lock(&pool.mutex);
while (true) {
if (pool.tasks.size()) {
Threadpool::Task task = pool.tasks.front();
pool.tasks.pop();
pthread_mutex_unlock(&pool.mutex);
task.first(task.second);
pthread_mutex_lock(&pool.mutex);
} else {
pthread_cond_wait(&pool.condition, &pool.mutex);
}
}
return nullptr;
}