[core] More efficient layer cloning
This commit is contained in:
@@ -4,10 +4,7 @@
|
||||
namespace mbgl {
|
||||
|
||||
std::unique_ptr<StyleLayer> BackgroundLayer::clone() const {
|
||||
std::unique_ptr<BackgroundLayer> result = std::make_unique<BackgroundLayer>();
|
||||
result->copy(*this);
|
||||
result->paint = paint;
|
||||
return std::move(result);
|
||||
return std::make_unique<BackgroundLayer>(*this);
|
||||
}
|
||||
|
||||
void BackgroundLayer::parsePaints(const JSVal& layer) {
|
||||
|
||||
@@ -5,10 +5,7 @@
|
||||
namespace mbgl {
|
||||
|
||||
std::unique_ptr<StyleLayer> CircleLayer::clone() const {
|
||||
std::unique_ptr<CircleLayer> result = std::make_unique<CircleLayer>();
|
||||
result->copy(*this);
|
||||
result->paint = paint;
|
||||
return std::move(result);
|
||||
return std::make_unique<CircleLayer>(*this);
|
||||
}
|
||||
|
||||
void CircleLayer::parsePaints(const JSVal& layer) {
|
||||
|
||||
@@ -5,10 +5,7 @@
|
||||
namespace mbgl {
|
||||
|
||||
std::unique_ptr<StyleLayer> FillLayer::clone() const {
|
||||
std::unique_ptr<FillLayer> result = std::make_unique<FillLayer>();
|
||||
result->copy(*this);
|
||||
result->paint = paint;
|
||||
return std::move(result);
|
||||
return std::make_unique<FillLayer>(*this);
|
||||
}
|
||||
|
||||
void FillLayer::parsePaints(const JSVal& layer) {
|
||||
|
||||
@@ -6,11 +6,7 @@
|
||||
namespace mbgl {
|
||||
|
||||
std::unique_ptr<StyleLayer> LineLayer::clone() const {
|
||||
std::unique_ptr<LineLayer> result = std::make_unique<LineLayer>();
|
||||
result->copy(*this);
|
||||
result->layout = layout;
|
||||
result->paint = paint;
|
||||
return std::move(result);
|
||||
return std::make_unique<LineLayer>(*this);
|
||||
}
|
||||
|
||||
void LineLayer::parseLayout(const JSVal& value) {
|
||||
|
||||
@@ -4,10 +4,7 @@
|
||||
namespace mbgl {
|
||||
|
||||
std::unique_ptr<StyleLayer> RasterLayer::clone() const {
|
||||
std::unique_ptr<RasterLayer> result = std::make_unique<RasterLayer>();
|
||||
result->copy(*this);
|
||||
result->paint = paint;
|
||||
return std::move(result);
|
||||
return std::make_unique<RasterLayer>(*this);
|
||||
}
|
||||
|
||||
void RasterLayer::parsePaints(const JSVal& layer) {
|
||||
|
||||
@@ -6,12 +6,7 @@
|
||||
namespace mbgl {
|
||||
|
||||
std::unique_ptr<StyleLayer> SymbolLayer::clone() const {
|
||||
std::unique_ptr<SymbolLayer> result = std::make_unique<SymbolLayer>();
|
||||
result->copy(*this);
|
||||
result->layout = layout;
|
||||
result->paint = paint;
|
||||
result->spriteAtlas = spriteAtlas;
|
||||
return std::move(result);
|
||||
return std::make_unique<SymbolLayer>(*this);
|
||||
}
|
||||
|
||||
void SymbolLayer::parseLayout(const JSVal& value) {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <mbgl/style/style_cascade_parameters.hpp>
|
||||
#include <mbgl/style/style_calculation_parameters.hpp>
|
||||
#include <mbgl/util/interpolate.hpp>
|
||||
#include <mbgl/util/std.hpp>
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
|
||||
@@ -23,9 +24,8 @@ class PaintProperty {
|
||||
public:
|
||||
using Fn = Function<Result>;
|
||||
|
||||
PaintProperty(T fallbackValue_)
|
||||
: fallbackValue(fallbackValue_),
|
||||
value(fallbackValue_) {
|
||||
PaintProperty(T fallbackValue)
|
||||
: value(fallbackValue) {
|
||||
values.emplace(ClassID::Fallback, Fn(fallbackValue));
|
||||
}
|
||||
|
||||
@@ -34,14 +34,8 @@ public:
|
||||
transitions(other.transitions) {
|
||||
}
|
||||
|
||||
void operator=(const PaintProperty& other) {
|
||||
values = other.values;
|
||||
transitions = other.transitions;
|
||||
}
|
||||
|
||||
void parse(const char* name, const JSVal& layer) {
|
||||
values.clear();
|
||||
values.emplace(ClassID::Fallback, Fn(fallbackValue));
|
||||
mbgl::util::erase_if(values, [] (const auto& p) { return p.first != ClassID::Fallback; });
|
||||
|
||||
std::string transitionName = { name };
|
||||
transitionName += "-transition";
|
||||
@@ -104,9 +98,9 @@ public:
|
||||
return cascaded->prior.operator bool();
|
||||
}
|
||||
|
||||
void operator=(const T& v) { values.emplace(ClassID::Default, Fn(v)); }
|
||||
operator T() const { return value; }
|
||||
|
||||
T fallbackValue;
|
||||
std::map<ClassID, Fn> values;
|
||||
std::map<ClassID, PropertyTransition> transitions;
|
||||
|
||||
|
||||
@@ -35,16 +35,4 @@ bool StyleLayer::hasRenderPass(RenderPass pass) const {
|
||||
return bool(passes & pass);
|
||||
}
|
||||
|
||||
void StyleLayer::copy(const StyleLayer& src) {
|
||||
id = src.id;
|
||||
ref = src.ref;
|
||||
type = src.type;
|
||||
source = src.source;
|
||||
sourceLayer = src.sourceLayer;
|
||||
filter = src.filter;
|
||||
minZoom = src.minZoom;
|
||||
maxZoom = src.maxZoom;
|
||||
visibility = src.visibility;
|
||||
}
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -21,7 +21,7 @@ class Bucket;
|
||||
|
||||
using JSVal = rapidjson::Value;
|
||||
|
||||
class StyleLayer : public util::noncopyable {
|
||||
class StyleLayer {
|
||||
public:
|
||||
static std::unique_ptr<StyleLayer> create(StyleLayerType);
|
||||
virtual std::unique_ptr<StyleLayer> clone() const = 0;
|
||||
@@ -58,11 +58,13 @@ public:
|
||||
VisibilityType visibility = VisibilityType::Visible;
|
||||
|
||||
protected:
|
||||
StyleLayer() = default;
|
||||
StyleLayer(const StyleLayer&) = default;
|
||||
StyleLayer& operator=(const StyleLayer&) = delete;
|
||||
|
||||
// Stores what render passes this layer is currently enabled for. This depends on the
|
||||
// evaluated StyleProperties object and is updated accordingly.
|
||||
RenderPass passes = RenderPass::None;
|
||||
|
||||
void copy(const StyleLayer& source);
|
||||
};
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
24
test/style/style_layer.cpp
Normal file
24
test/style/style_layer.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "../fixtures/util.hpp"
|
||||
|
||||
#include <mbgl/style/style_layer.hpp>
|
||||
#include <mbgl/layer/background_layer.hpp>
|
||||
|
||||
using namespace mbgl;
|
||||
|
||||
TEST(StyleLayer, Create) {
|
||||
std::unique_ptr<StyleLayer> layer = StyleLayer::create(StyleLayerType::Background);
|
||||
EXPECT_TRUE(reinterpret_cast<BackgroundLayer*>(layer.get()));
|
||||
}
|
||||
|
||||
TEST(StyleLayer, Clone) {
|
||||
std::unique_ptr<StyleLayer> layer = StyleLayer::create(StyleLayerType::Background);
|
||||
std::unique_ptr<StyleLayer> clone = layer->clone();
|
||||
EXPECT_NE(layer.get(), clone.get());
|
||||
EXPECT_TRUE(reinterpret_cast<BackgroundLayer*>(layer.get()));
|
||||
}
|
||||
|
||||
TEST(StyleLayer, CloneCopiesBaseProperties) {
|
||||
std::unique_ptr<BackgroundLayer> layer = std::make_unique<BackgroundLayer>();
|
||||
layer->id = "test";
|
||||
EXPECT_EQ("test", layer->clone()->id);
|
||||
}
|
||||
@@ -91,6 +91,7 @@
|
||||
'style/glyph_store.cpp',
|
||||
'style/pending_resources.cpp',
|
||||
'style/resource_loading.cpp',
|
||||
'style/style_layer.cpp',
|
||||
|
||||
'sprite/sprite_atlas.cpp',
|
||||
'sprite/sprite_image.cpp',
|
||||
|
||||
Reference in New Issue
Block a user