refactor source information into SourceInfo object

This commit is contained in:
Konstantin Käfer
2014-07-17 15:37:18 -07:00
parent d3aeed6cf2
commit 3141b8b1f7
5 changed files with 38 additions and 44 deletions

View File

@@ -23,10 +23,7 @@ class Texturepool;
class Source : public std::enable_shared_from_this<Source>, private util::noncopyable {
public:
Source(StyleSource style_source, const std::string &access_token = "");
Source(SourceType type = SourceType::Vector, const std::string &url = "",
uint32_t tile_size = 512, uint32_t min_zoom = 0, uint32_t max_zoom = 22,
const std::string &access_token = "");
Source(SourceInfo info, const std::string &access_token = "");
bool update(Map &map);
void updateMatrices(const mat4 &projMatrix, const TransformState &transform);
@@ -41,11 +38,7 @@ public:
static std::string normalizeSourceURL(const std::string &url, const std::string &access_token);
public:
const SourceType type;
const std::string url;
const uint32_t tile_size;
const int32_t min_zoom;
const int32_t max_zoom;
const SourceInfo info;
private:
bool findLoadedChildren(const Tile::ID& id, int32_t maxCoveringZoom, std::forward_list<Tile::ID>& retain);

View File

@@ -9,7 +9,7 @@ namespace mbgl {
class Source;
class StyleSource {
class SourceInfo {
public:
const SourceType type;
const std::string url;
@@ -17,13 +17,21 @@ public:
const int32_t min_zoom;
const int32_t max_zoom;
SourceInfo(SourceType type = SourceType::Vector, const std::string &url = "",
uint32_t tile_size = 512, int32_t min_zoom = 0, int32_t max_zoom = 22)
: type(type), url(url), tile_size(tile_size), min_zoom(min_zoom), max_zoom(max_zoom) {}
};
class StyleSource {
public:
const SourceInfo info;
bool enabled = false;
std::shared_ptr<Source> source;
StyleSource(SourceType type = SourceType::Vector, const std::string &url = "",
uint32_t tile_size = 512, uint32_t min_zoom = 0, uint32_t max_zoom = 22)
: type(type), url(url), tile_size(tile_size), min_zoom(min_zoom), max_zoom(max_zoom) {}
};
StyleSource(const SourceInfo &info) : info(info) {}
};
}
#endif

View File

@@ -421,13 +421,13 @@ void Map::updateSources() {
updateSources(style->layers);
// Then, construct or destroy the actual source object, depending on enabled state.
for (const std::shared_ptr<StyleSource> &source : activeSources) {
if (source->enabled) {
if (!source->source) {
source->source = std::make_shared<Source>(*source, getAccessToken());
for (const std::shared_ptr<StyleSource> &style_source : activeSources) {
if (style_source->enabled) {
if (!style_source->source) {
style_source->source = std::make_shared<Source>(style_source->info, getAccessToken());
}
} else {
source->source.reset();
style_source->source.reset();
}
}

View File

@@ -18,21 +18,14 @@
namespace mbgl {
Source::Source(StyleSource style_source, const std::string &access_token)
: type(style_source.type),
url(normalizeSourceURL(style_source.url, access_token)),
tile_size(style_source.tile_size),
min_zoom(style_source.min_zoom),
max_zoom(style_source.max_zoom) {}
Source::Source(SourceType type, const std::string &url,
uint32_t tile_size, uint32_t min_zoom, uint32_t max_zoom,
const std::string &access_token)
: type(type),
url(normalizeSourceURL(url, access_token)),
tile_size(tile_size),
min_zoom(min_zoom),
max_zoom(max_zoom) {}
Source::Source(SourceInfo info, const std::string &access_token)
: info(
info.type,
normalizeSourceURL(info.url, access_token),
info.tile_size,
info.min_zoom,
info.max_zoom
) {}
std::string Source::normalizeSourceURL(const std::string &url, const std::string &access_token) {
const std::string t = "mapbox://";
@@ -153,7 +146,7 @@ TileData::State Source::addTile(Map &map, const Tile::ID& id) {
if (!new_tile.data) {
// If we don't find working tile data, we're just going to load it.
const std::string formed_url = util::replaceTokens(url, [&](const std::string &token) -> std::string {
const std::string formed_url = util::replaceTokens(info.url, [&](const std::string &token) -> std::string {
if (token == "z") return std::to_string(normalized_id.z);
if (token == "x") return std::to_string(normalized_id.x);
if (token == "y") return std::to_string(normalized_id.y);
@@ -161,9 +154,9 @@ TileData::State Source::addTile(Map &map, const Tile::ID& id) {
return "";
});
if (type == SourceType::Vector) {
if (info.type == SourceType::Vector) {
new_tile.data = std::make_shared<VectorTileData>(normalized_id, map, formed_url);
} else if (type == SourceType::Raster) {
} else if (info.type == SourceType::Raster) {
new_tile.data = std::make_shared<RasterTileData>(normalized_id, map, formed_url);
} else {
throw std::runtime_error("source type not implemented");
@@ -232,14 +225,14 @@ bool Source::updateTiles(Map &map) {
// Figure out what tiles we need to load
int32_t clamped_zoom = map.getState().getIntegerZoom();
if (clamped_zoom > max_zoom) clamped_zoom = max_zoom;
if (clamped_zoom < min_zoom) clamped_zoom = min_zoom;
if (clamped_zoom > info.max_zoom) clamped_zoom = info.max_zoom;
if (clamped_zoom < info.min_zoom) clamped_zoom = info.min_zoom;
int32_t max_covering_zoom = clamped_zoom + 1;
if (max_covering_zoom > max_zoom) max_covering_zoom = max_zoom;
if (max_covering_zoom > info.max_zoom) max_covering_zoom = info.max_zoom;
int32_t min_covering_zoom = clamped_zoom - 10;
if (min_covering_zoom < min_zoom) min_covering_zoom = min_zoom;
if (min_covering_zoom < info.min_zoom) min_covering_zoom = info.min_zoom;
// Map four viewport corners to pixel coordinates
box box = map.getState().cornersToBox(clamped_zoom);
@@ -379,7 +372,7 @@ void _scanTriangle(const mbgl::vec2<double> a, const mbgl::vec2<double> b, const
}
double Source::getZoom(const TransformState &state) const {
double offset = log(util::tileSize / tile_size) / log(2);
double offset = log(util::tileSize / info.tile_size) / log(2);
offset += (state.getPixelRatio() > 1.0 ? 1 :0);
return state.getZoom() + offset;
}
@@ -387,7 +380,7 @@ double Source::getZoom(const TransformState &state) const {
std::forward_list<mbgl::Tile::ID> Source::covering_tiles(const TransformState &state, int32_t clamped_zoom, const box& points) {
int32_t dim = std::pow(2, clamped_zoom);
std::forward_list<mbgl::Tile::ID> tiles;
bool is_raster = (type == SourceType::Raster);
bool is_raster = (info.type == SourceType::Raster);
double search_zoom = getZoom(state);
auto scanLine = [&tiles, clamped_zoom, is_raster, search_zoom](int32_t x0, int32_t x1, int32_t y, int32_t ymax) {

View File

@@ -175,7 +175,7 @@ void StyleParser::parseSources(JSVal value) {
parseRenderProperty(itr->value, min_zoom, "minZoom");
parseRenderProperty(itr->value, max_zoom, "maxZoom");
sources.emplace(std::move(name), std::make_shared<StyleSource>(type, url, tile_size, min_zoom, max_zoom));
sources.emplace(std::move(name), std::make_shared<StyleSource>(SourceInfo { type, url, tile_size, min_zoom, max_zoom }));
}
} else {
throw Style::exception("sources must be an object");