Merge pull request #1174 from mapbox/style-layer-group

Style code cleanup
This commit is contained in:
John Firebaugh
2015-04-01 14:07:06 -07:00
15 changed files with 80 additions and 200 deletions

View File

@@ -29,7 +29,6 @@ class LayerDescription;
class Sprite;
class Style;
class StyleLayer;
class StyleLayerGroup;
class StyleSource;
class TexturePool;
class FileSource;
@@ -188,7 +187,6 @@ private:
void updateTiles();
void updateSources();
void updateSources(const util::ptr<StyleLayerGroup> &group);
// Triggered by triggerUpdate();
void update();

View File

@@ -18,7 +18,6 @@
#include <mbgl/text/glyph_store.hpp>
#include <mbgl/geometry/glyph_atlas.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/style/style_layer_group.hpp>
#include <mbgl/style/style_bucket.hpp>
#include <mbgl/util/texture_pool.hpp>
#include <mbgl/geometry/sprite_atlas.hpp>
@@ -723,7 +722,11 @@ void Map::updateSources() {
// Then, reenable all of those that we actually use when drawing this layer.
if (style) {
updateSources(style->layers);
for (const auto& layer : style->layers) {
if (layer->bucket && layer->bucket->style_source) {
(*activeSources.emplace(layer->bucket->style_source).first)->enabled = true;
}
}
}
// Then, construct or destroy the actual source object, depending on enabled state.
@@ -744,20 +747,6 @@ void Map::updateSources() {
});
}
void Map::updateSources(const util::ptr<StyleLayerGroup> &group) {
assert(Environment::currentlyOn(ThreadType::Map));
if (!group) {
return;
}
for (const auto& layer : group->layers) {
if (!layer) continue;
if (layer->bucket && layer->bucket->style_source) {
(*activeSources.emplace(layer->bucket->style_source).first)->enabled = true;
}
}
}
void Map::updateTiles() {
assert(Environment::currentlyOn(ThreadType::Map));
for (const auto& source : activeSources) {
@@ -807,7 +796,7 @@ void Map::loadStyleJSON(const std::string& json, const std::string& base) {
style = std::make_shared<Style>();
style->base = base;
style->loadJSON((const uint8_t *)json.c_str());
style->cascadeClasses(data->getClasses());
style->cascade(data->getClasses());
style->setDefaultTransitionDuration(data->getDefaultTransitionDuration());
const std::string glyphURL = util::mapbox::normalizeGlyphsURL(style->glyph_url, getAccessToken());
@@ -834,7 +823,7 @@ void Map::prepare() {
}
if (u & static_cast<UpdateType>(Update::Classes)) {
if (style) {
style->cascadeClasses(data->getClasses());
style->cascade(data->getClasses());
}
}
@@ -850,7 +839,7 @@ void Map::prepare() {
if (style) {
updateSources();
style->updateProperties(state.getNormalizedZoom(), animationTime);
style->recalculate(state.getNormalizedZoom(), animationTime);
// Allow the sprite atlas to potentially pull new sprite images if needed.
spriteAtlas->resize(state.getPixelRatio());

View File

@@ -3,7 +3,7 @@
#include <mbgl/platform/log.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/style/style_layer_group.hpp>
#include <mbgl/style/style_source.hpp>
#include <mbgl/renderer/fill_bucket.hpp>
#include <mbgl/renderer/line_bucket.hpp>
#include <mbgl/renderer/symbol_bucket.hpp>
@@ -51,12 +51,7 @@ TileParser::TileParser(const GeometryTile& geometryTile_,
bool TileParser::obsolete() const { return tile.state == TileData::State::obsolete; }
void TileParser::parse() {
util::ptr<const StyleLayerGroup> group = style->layers;
if (!group) {
return;
}
for (const auto& layer_desc : group->layers) {
for (const auto& layer_desc : style->layers) {
// Cancel early when parsing.
if (obsolete()) {
return;

View File

@@ -28,7 +28,6 @@ class StyleLayoutFill;
class StyleLayoutRaster;
class StyleLayoutLine;
class StyleLayoutSymbol;
class StyleLayerGroup;
class VectorTileData;
class Collision;

View File

@@ -2,7 +2,6 @@
#include <mbgl/platform/log.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/style/style_layer_group.hpp>
#include <mbgl/style/style_bucket.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/string.hpp>
@@ -237,23 +236,9 @@ void Painter::render(const Style& style, const std::set<util::ptr<StyleSource>>&
// Actually render the layers
if (debug::renderTree) { Log::Info(Event::Render, "{"); indent++; }
if (style.layers) {
renderLayers(*style.layers);
}
if (debug::renderTree) { Log::Info(Event::Render, "}"); indent--; }
// 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 (const auto& source : sources) {
source->source->finishRender(*this);
}
}
void Painter::renderLayers(const StyleLayerGroup &group) {
// TODO: Correctly compute the number of layers recursively beforehand.
float strata_thickness = 1.0f / (group.layers.size() + 1);
float strata_thickness = 1.0f / (style.layers.size() + 1);
// - FIRST PASS ------------------------------------------------------------
// Render everything top-to-bottom by using reverse iterators. Render opaque
@@ -263,7 +248,7 @@ void Painter::renderLayers(const StyleLayerGroup &group) {
Log::Info(Event::Render, "%*s%s", indent++ * 4, "", "OPAQUE {");
}
int i = 0;
for (auto it = group.layers.rbegin(), end = group.layers.rend(); it != end; ++it, ++i) {
for (auto it = style.layers.rbegin(), end = style.layers.rend(); it != end; ++it, ++i) {
setOpaque();
setStrata(i * strata_thickness);
renderLayer(**it);
@@ -279,7 +264,7 @@ void Painter::renderLayers(const StyleLayerGroup &group) {
Log::Info(Event::Render, "%*s%s", indent++ * 4, "", "TRANSLUCENT {");
}
--i;
for (auto it = group.layers.begin(), end = group.layers.end(); it != end; ++it, --i) {
for (auto it = style.layers.begin(), end = style.layers.end(); it != end; ++it, --i) {
setTranslucent();
setStrata(i * strata_thickness);
renderLayer(**it);
@@ -287,6 +272,16 @@ void Painter::renderLayers(const StyleLayerGroup &group) {
if (debug::renderTree) {
Log::Info(Event::Render, "%*s%s", --indent * 4, "", "}");
}
if (debug::renderTree) { Log::Info(Event::Render, "}"); indent--; }
// 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 (const auto& source : sources) {
source->source->finishRender(*this);
}
}
void Painter::renderLayer(const StyleLayer &layer_desc, const Tile::ID* id, const mat4* matrix) {

View File

@@ -43,7 +43,6 @@ class GlyphAtlas;
class LineAtlas;
class Source;
class StyleSource;
class StyleLayerGroup;
class FillBucket;
class LineBucket;
@@ -81,7 +80,6 @@ public:
TransformState state,
std::chrono::steady_clock::time_point time);
void renderLayers(const StyleLayerGroup &group);
void renderLayer(const StyleLayer &layer_desc, const Tile::ID* id = nullptr, const mat4* matrix = nullptr);
// Renders a particular layer from a tile.

View File

@@ -2,7 +2,6 @@
#include <mbgl/platform/gl.hpp>
#include <mbgl/renderer/raster_bucket.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/style/style_layer_group.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/map/map.hpp>

View File

@@ -1,6 +1,6 @@
#include <mbgl/style/style.hpp>
#include <mbgl/map/sprite.hpp>
#include <mbgl/style/style_layer_group.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/style/style_parser.hpp>
#include <mbgl/style/style_bucket.hpp>
#include <mbgl/util/constants.hpp>
@@ -25,19 +25,21 @@ Style::Style()
// for deleting the std::unique_ptr<uv::rwlock>.
Style::~Style() {}
void Style::updateProperties(float z, std::chrono::steady_clock::time_point now) {
void Style::cascade(const std::vector<std::string>& classes) {
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
for (const auto& layer : layers) {
layer->setClasses(classes, now, defaultTransition);
}
}
void Style::recalculate(float z, std::chrono::steady_clock::time_point now) {
uv::writelock lock(mtx);
zoomHistory.update(z, now);
if (layers) {
layers->updateProperties(z, now, zoomHistory);
}
// Apply transitions after the first time.
if (!initial_render_complete) {
initial_render_complete = true;
return;
for (const auto& layer : layers) {
layer->updateProperties(z, now, zoomHistory);
}
}
@@ -49,22 +51,15 @@ void Style::setDefaultTransitionDuration(std::chrono::steady_clock::duration dur
defaultTransition.duration = duration;
}
void Style::cascadeClasses(const std::vector<std::string>& classes) {
if (layers) {
layers->setClasses(classes, std::chrono::steady_clock::now(), defaultTransition);
}
}
bool Style::hasTransitions() const {
if (layers) {
if (layers->hasTransitions()) {
for (const auto& layer : layers) {
if (layer->hasTransitions()) {
return true;
}
}
return false;
}
void Style::loadJSON(const uint8_t *const data) {
uv::writelock lock(mtx);

View File

@@ -2,54 +2,43 @@
#define MBGL_STYLE_STYLE
#include <mbgl/style/property_transition.hpp>
#include <mbgl/style/style_source.hpp>
#include <mbgl/style/zoom_history.hpp>
#include <mbgl/util/uv.hpp>
#include <mbgl/util/ptr.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <cstdint>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>
#include <set>
#include <chrono>
namespace mbgl {
class Sprite;
class StyleLayer;
class StyleLayerGroup;
class Style : public util::noncopyable {
public:
struct exception : std::runtime_error { exception(const char *msg) : std::runtime_error(msg) {} };
Style();
~Style();
void loadJSON(const uint8_t *const data);
size_t layerCount() const;
void updateProperties(float z, std::chrono::steady_clock::time_point now);
void setDefaultTransitionDuration(std::chrono::steady_clock::duration duration = std::chrono::steady_clock::duration::zero());
void cascadeClasses(const std::vector<std::string>&);
void cascade(const std::vector<std::string>&);
void recalculate(float z, std::chrono::steady_clock::time_point now);
void setDefaultTransitionDuration(std::chrono::steady_clock::duration);
bool hasTransitions() const;
const std::string &getSpriteURL() const;
util::ptr<StyleLayerGroup> layers;
std::vector<std::string> appliedClasses;
std::vector<util::ptr<StyleLayer>> layers;
std::string glyph_url;
std::string base;
private:
std::string sprite_url;
PropertyTransition defaultTransition;
bool initial_render_complete = false;
std::unique_ptr<uv::rwlock> mtx;
ZoomHistory zoomHistory;
};

View File

@@ -1,6 +1,5 @@
#include <mbgl/style/style_layer.hpp>
#include <mbgl/style/style_bucket.hpp>
#include <mbgl/style/style_layer_group.hpp>
#include <mbgl/style/property_fallback.hpp>
#include <mbgl/util/interpolate.hpp>

View File

@@ -8,6 +8,7 @@
#include <mbgl/style/zoom_history.hpp>
#include <mbgl/util/ptr.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <vector>
#include <string>
@@ -18,9 +19,8 @@
namespace mbgl {
class StyleBucket;
class StyleLayerGroup;
class StyleLayer {
class StyleLayer : public util::noncopyable {
public:
StyleLayer(const std::string &id, std::map<ClassID, ClassProperties> &&styles);

View File

@@ -1,34 +0,0 @@
#include <mbgl/style/style_layer_group.hpp>
namespace mbgl {
void StyleLayerGroup::setClasses(const std::vector<std::string> &class_names, std::chrono::steady_clock::time_point now,
const PropertyTransition &defaultTransition) {
for (const auto& layer : layers) {
if (layer) {
layer->setClasses(class_names, now, defaultTransition);
}
}
}
void StyleLayerGroup::updateProperties(float z, std::chrono::steady_clock::time_point now, ZoomHistory &zoomHistory) {
for (const auto& layer : layers) {
if (layer) {
layer->updateProperties(z, now, zoomHistory);
}
}
}
bool StyleLayerGroup::hasTransitions() const {
for (const auto& layer : layers) {
if (layer) {
if (layer->hasTransitions()) {
return true;
}
}
}
return false;
}
}

View File

@@ -1,24 +0,0 @@
#ifndef MBGL_STYLE_STYLE_LAYER_GROUP
#define MBGL_STYLE_STYLE_LAYER_GROUP
#include <mbgl/style/style_layer.hpp>
#include <vector>
#include <chrono>
namespace mbgl {
class StyleLayerGroup {
public:
void setClasses(const std::vector<std::string> &class_names, std::chrono::steady_clock::time_point now,
const PropertyTransition &defaultTransition);
void updateProperties(float z, std::chrono::steady_clock::time_point now, ZoomHistory &zoomHistory);
bool hasTransitions() const;
public:
std::vector<util::ptr<StyleLayer>> layers;
};
}
#endif

View File

@@ -1,6 +1,6 @@
#include <mbgl/style/style_source.hpp>
#include <mbgl/style/style_parser.hpp>
#include <mbgl/style/style_layer_group.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/map/annotation.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/std.hpp>
@@ -35,8 +35,7 @@ void StyleParser::parse(JSVal document) {
}
if (document.HasMember("layers")) {
root = createLayers(document["layers"]);
parseLayers();
parseLayers(document["layers"]);
// create point annotations layer
//
@@ -45,8 +44,8 @@ void StyleParser::parse(JSVal document) {
std::map<ClassID, ClassProperties> paints;
util::ptr<StyleLayer> annotations = std::make_shared<StyleLayer>(id, std::move(paints));
annotations->type = StyleLayerType::Symbol;
layers.emplace(id, std::pair<JSVal, util::ptr<StyleLayer>> { JSVal(id), annotations });
root->layers.emplace_back(annotations);
layersMap.emplace(id, std::pair<JSVal, util::ptr<StyleLayer>> { JSVal(id), annotations });
layers.emplace_back(annotations);
util::ptr<StyleBucket> pointBucket = std::make_shared<StyleBucket>(annotations->type);
pointBucket->name = annotations->id;
@@ -682,61 +681,48 @@ template<> std::tuple<bool, PiecewiseConstantFunction<Faded<std::string>>> Style
#pragma mark - Parse Layers
std::unique_ptr<StyleLayerGroup> StyleParser::createLayers(JSVal value) {
if (value.IsArray()) {
std::unique_ptr<StyleLayerGroup> group = util::make_unique<StyleLayerGroup>();
for (rapidjson::SizeType i = 0; i < value.Size(); ++i) {
util::ptr<StyleLayer> layer = createLayer(value[i]);
if (layer) {
group->layers.emplace_back(layer);
}
}
return group;
} else {
void StyleParser::parseLayers(JSVal value) {
if (!value.IsArray()) {
Log::Warning(Event::ParseStyle, "layers must be an array");
return nullptr;
return;
}
}
util::ptr<StyleLayer> StyleParser::createLayer(JSVal value) {
if (value.IsObject()) {
if (!value.HasMember("id")) {
Log::Warning(Event::ParseStyle, "layer must have an id");
return nullptr;
for (rapidjson::SizeType i = 0; i < value.Size(); ++i) {
JSVal layerValue = value[i];
if (!layerValue.IsObject()) {
Log::Warning(Event::ParseStyle, "layer must be an object");
continue;
}
JSVal id = value["id"];
if (!layerValue.HasMember("id")) {
Log::Warning(Event::ParseStyle, "layer must have an id");
continue;
}
JSVal id = layerValue["id"];
if (!id.IsString()) {
Log::Warning(Event::ParseStyle, "layer id must be a string");
return nullptr;
continue;
}
const std::string layer_id = { id.GetString(), id.GetStringLength() };
if (layers.find(layer_id) != layers.end()) {
Log::Warning(Event::ParseStyle, "duplicate layer id %s", layer_id.c_str());
return nullptr;
const std::string layerID = { id.GetString(), id.GetStringLength() };
if (layersMap.find(layerID) != layersMap.end()) {
Log::Warning(Event::ParseStyle, "duplicate layer id %s", layerID.c_str());
continue;
}
// Parse paints already, as they can't be inherited anyway.
std::map<ClassID, ClassProperties> paints;
parsePaints(value, paints);
parsePaints(layerValue, paints);
util::ptr<StyleLayer> layer = std::make_shared<StyleLayer>(
layer_id, std::move(paints));
util::ptr<StyleLayer> layer = std::make_shared<StyleLayer>(layerID, std::move(paints));
// Store the layer ID so we can reference it later.
layers.emplace(layer_id, std::pair<JSVal, util::ptr<StyleLayer>> { value, layer });
return layer;
} else {
Log::Warning(Event::ParseStyle, "layer must be an object");
return nullptr;
layers.emplace_back(layer);
layersMap.emplace(layerID, std::pair<JSVal, util::ptr<StyleLayer>> { layerValue, layer });
}
}
void StyleParser::parseLayers() {
for (auto &pair : layers) {
for (auto& pair : layersMap) {
parseLayer(pair.second);
}
}
@@ -924,8 +910,8 @@ void StyleParser::parseReference(JSVal value, util::ptr<StyleLayer> &layer) {
return;
}
const std::string ref { value.GetString(), value.GetStringLength() };
auto it = layers.find(ref);
if (it == layers.end()) {
auto it = layersMap.find(ref);
if (it == layersMap.end()) {
Log::Warning(Event::ParseStyle, "layer '%s' references unknown layer %s", layer->id.c_str(), ref.c_str());
// We cannot parse this layer further.
return;

View File

@@ -17,7 +17,6 @@ namespace mbgl {
enum class ClassID : uint32_t;
class StyleLayer;
class StyleLayerGroup;
class StyleParser {
public:
@@ -27,8 +26,8 @@ public:
void parse(JSVal document);
util::ptr<StyleLayerGroup> getLayers() {
return root;
std::vector<util::ptr<StyleLayer>> getLayers() {
return layers;
}
std::string getSprite() const {
@@ -44,10 +43,7 @@ private:
JSVal replaceConstant(JSVal value);
void parseSources(JSVal value);
std::unique_ptr<StyleLayerGroup> createLayers(JSVal value);
util::ptr<StyleLayer> createLayer(JSVal value);
void parseLayers();
void parseLayers(JSVal value);
void parseLayer(std::pair<JSVal, util::ptr<StyleLayer>> &pair);
void parsePaints(JSVal value, std::map<ClassID, ClassProperties> &paints);
void parsePaint(JSVal, ClassProperties &properties);
@@ -97,10 +93,10 @@ private:
std::unordered_map<std::string, const util::ptr<StyleSource>> sources;
// This stores the root layer.
util::ptr<StyleLayerGroup> root;
std::vector<util::ptr<StyleLayer>> layers;
// This maps ids to Layer objects, with all items being at the root level.
std::unordered_map<std::string, std::pair<JSVal, util::ptr<StyleLayer>>> layers;
std::unordered_map<std::string, std::pair<JSVal, util::ptr<StyleLayer>>> layersMap;
// Store a stack of layers we're parsing right now. This is to prevent reference cycles.
std::forward_list<StyleLayer *> stack;