diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2015-12-08 14:30:42 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-12-09 13:51:28 -0800 |
commit | bde07c8f0edcc890efd7fd6993f5309d6881c402 (patch) | |
tree | 38464b4f291551d0cece8505941c3763e70bc3ab /src | |
parent | 0346cfd865b7f26e92561efdc3fca458c88800e5 (diff) | |
download | qtlocation-mapboxgl-bde07c8f0edcc890efd7fd6993f5309d6881c402.tar.gz |
[core] Custom layers
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/layer/custom_layer.cpp | 19 | ||||
-rw-r--r-- | src/mbgl/layer/custom_layer.hpp | 49 | ||||
-rw-r--r-- | src/mbgl/map/map.cpp | 11 | ||||
-rw-r--r-- | src/mbgl/map/map_context.cpp | 5 | ||||
-rw-r--r-- | src/mbgl/map/map_context.hpp | 7 | ||||
-rw-r--r-- | src/mbgl/map/tile_worker.cpp | 5 | ||||
-rw-r--r-- | src/mbgl/renderer/painter.cpp | 22 | ||||
-rw-r--r-- | src/mbgl/style/style.cpp | 16 | ||||
-rw-r--r-- | src/mbgl/style/style.hpp | 6 | ||||
-rw-r--r-- | src/mbgl/style/style_render_parameters.hpp | 18 |
10 files changed, 142 insertions, 16 deletions
diff --git a/src/mbgl/layer/custom_layer.cpp b/src/mbgl/layer/custom_layer.cpp new file mode 100644 index 0000000000..1a47193525 --- /dev/null +++ b/src/mbgl/layer/custom_layer.cpp @@ -0,0 +1,19 @@ +#include <mbgl/layer/custom_layer.hpp> +#include <mbgl/renderer/bucket.hpp> + +namespace mbgl { + +CustomLayer::CustomLayer(const std::string& id_) { + id = id_; +} + +bool CustomLayer::recalculate(const StyleCalculationParameters&) { + passes = RenderPass::Translucent; + return false; +} + +std::unique_ptr<Bucket> CustomLayer::createBucket(StyleBucketParameters&) const { + return nullptr; +} + +} // namespace mbgl diff --git a/src/mbgl/layer/custom_layer.hpp b/src/mbgl/layer/custom_layer.hpp new file mode 100644 index 0000000000..172eed3051 --- /dev/null +++ b/src/mbgl/layer/custom_layer.hpp @@ -0,0 +1,49 @@ +#ifndef MBGL_CUSTOM_LAYER +#define MBGL_CUSTOM_LAYER + +#include <mbgl/style/style_layer.hpp> + +namespace mbgl { + +class StyleRenderParameters; + +class CustomLayer : public StyleLayer { +public: + CustomLayer(const std::string& id); + CustomLayer(const CustomLayer&) = default; + + /** + * Initialize any GL state needed by the custom layer. This method is called once, from the + * rendering thread, at a point when the GL context is active but before rendering for the + * first time. + * + * Resources that are acquired in this method must be released in the subclass's destructor. + * However, note that in some situations the destructor may be called without initialize() + * having been called. The destructor should be prepared for this case, e.g. by checking if + * the resources were actually acquired before releasing them. + */ + virtual void initialize() = 0; + + /** + * Render the layer. This method is called once per frame. The implementation should not make + * any assumptions about the GL state (other than that the correct context is active). It may + * make changes to the state, and is not required to reset values such as the depth mask, stencil + * mask, and corresponding test flags to their original values. + * + * The input StyleRenderParameters object has a `TransformState state` member that can be queried + * for information about the current map view. + */ + virtual void render(const StyleRenderParameters&) const = 0; + + void parseLayout(const JSVal&) final {} + void parsePaints(const JSVal&) final {} + + void cascade(const StyleCascadeParameters&) final {} + bool recalculate(const StyleCalculationParameters&) final; + + std::unique_ptr<Bucket> createBucket(StyleBucketParameters&) const final; +}; + +} // namespace mbgl + +#endif diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 6d03dfc2a9..0d6bdf9c7e 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -7,6 +7,7 @@ #include <mbgl/map/map_data.hpp> #include <mbgl/annotation/point_annotation.hpp> #include <mbgl/annotation/shape_annotation.hpp> +#include <mbgl/style/style_layer.hpp> #include <mbgl/util/projection.hpp> #include <mbgl/util/thread.hpp> @@ -415,6 +416,16 @@ AnnotationIDs Map::getPointAnnotationsInBounds(const LatLngBounds& bounds) { LatLngBounds Map::getBoundsForAnnotations(const AnnotationIDs& annotations) { return data->getAnnotationManager()->getBoundsForAnnotations(annotations); } + +#pragma mark - Style API + +void Map::addLayer(std::unique_ptr<StyleLayer> layer) { + context->invoke(&MapContext::addLayer, std::move(layer), mapbox::util::optional<std::string>()); +} + +void Map::addLayer(std::unique_ptr<StyleLayer> layer, const std::string& before) { + context->invoke(&MapContext::addLayer, std::move(layer), before); +} #pragma mark - Toggles diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index d14033c9c7..c5fcc7ff0e 100644 --- a/src/mbgl/map/map_context.cpp +++ b/src/mbgl/map/map_context.cpp @@ -11,6 +11,7 @@ #include <mbgl/storage/response.hpp> #include <mbgl/style/style.hpp> +#include <mbgl/style/style_layer.hpp> #include <mbgl/sprite/sprite_atlas.hpp> #include <mbgl/sprite/sprite_store.hpp> @@ -277,6 +278,10 @@ double MapContext::getTopOffsetPixelsForAnnotationIcon(const std::string& name) return data.getAnnotationManager()->getTopOffsetPixelsForIcon(name); } +void MapContext::addLayer(std::unique_ptr<StyleLayer> layer, mapbox::util::optional<std::string> after) { + style->addLayer(std::move(layer), after); +} + void MapContext::setSourceTileCacheSize(size_t size) { assert(util::ThreadContext::currentlyOn(util::ThreadType::Map)); if (size != sourceCacheSize) { diff --git a/src/mbgl/map/map_context.hpp b/src/mbgl/map/map_context.hpp index ec3d5aa58b..8e0dbd80c0 100644 --- a/src/mbgl/map/map_context.hpp +++ b/src/mbgl/map/map_context.hpp @@ -11,6 +11,8 @@ #include <mbgl/util/gl_object_store.hpp> #include <mbgl/util/ptr.hpp> +#include <mapbox/optional.hpp> + #include <vector> namespace mbgl { @@ -48,9 +50,14 @@ public: bool isLoaded() const; + // Annotations void addAnnotationIcon(const std::string&, std::shared_ptr<const SpriteImage>); double getTopOffsetPixelsForAnnotationIcon(const std::string&); void updateAnnotations(); + + // Style API + void addLayer(std::unique_ptr<StyleLayer>, + const mapbox::util::optional<std::string> before); void setSourceTileCacheSize(size_t size); void onLowMemory(); diff --git a/src/mbgl/map/tile_worker.cpp b/src/mbgl/map/tile_worker.cpp index 2fdd8bb3ad..c35b3d8b3c 100644 --- a/src/mbgl/map/tile_worker.cpp +++ b/src/mbgl/map/tile_worker.cpp @@ -4,6 +4,7 @@ #include <mbgl/style/style_layer.hpp> #include <mbgl/style/style_bucket_parameters.hpp> #include <mbgl/layer/background_layer.hpp> +#include <mbgl/layer/custom_layer.hpp> #include <mbgl/layer/symbol_layer.hpp> #include <mbgl/sprite/sprite_atlas.hpp> #include <mbgl/geometry/glyph_atlas.hpp> @@ -109,8 +110,8 @@ void TileWorker::parseLayer(const StyleLayer* layer, const GeometryTile& geometr if (state == TileData::State::obsolete) return; - // Background is a special case. - if (layer->is<BackgroundLayer>()) + // Background and custom layers are special cases. + if (layer->is<BackgroundLayer>() || layer->is<CustomLayer>()) return; // Skip this bucket if we are to not render this diff --git a/src/mbgl/renderer/painter.cpp b/src/mbgl/renderer/painter.cpp index 819338091f..fb74d5b8e6 100644 --- a/src/mbgl/renderer/painter.cpp +++ b/src/mbgl/renderer/painter.cpp @@ -10,8 +10,10 @@ #include <mbgl/style/style.hpp> #include <mbgl/style/style_layer.hpp> +#include <mbgl/style/style_render_parameters.hpp> #include <mbgl/layer/background_layer.hpp> +#include <mbgl/layer/custom_layer.hpp> #include <mbgl/sprite/sprite_atlas.hpp> #include <mbgl/geometry/line_atlas.hpp> @@ -217,9 +219,11 @@ void Painter::renderPass(RenderPass pass_, for (; it != end; ++it, i += increment) { currentLayer = i; + const auto& item = *it; + const StyleLayer& layer = item.layer; - if (!item.layer.hasRenderPass(pass)) + if (!layer.hasRenderPass(pass)) continue; if (pass == RenderPass::Translucent) { @@ -233,13 +237,17 @@ void Painter::renderPass(RenderPass pass_, config.stencilMask = 0x0; config.viewport = { 0, 0, frame.framebufferSize[0], frame.framebufferSize[1] }; - if (item.bucket && item.tile) { - MBGL_DEBUG_GROUP(item.layer.id + " - " + std::string(item.tile->id)); - prepareTile(*item.tile); - item.bucket->render(*this, item.layer, item.tile->id, item.tile->matrix); - } else { + if (layer.is<BackgroundLayer>()) { MBGL_DEBUG_GROUP("background"); - renderBackground(*item.layer.template as<BackgroundLayer>()); + renderBackground(*layer.as<BackgroundLayer>()); + } else if (layer.is<CustomLayer>()) { + MBGL_DEBUG_GROUP(layer.id + " - custom"); + layer.as<CustomLayer>()->render(StyleRenderParameters(state)); + config.setDirty(); + } else { + MBGL_DEBUG_GROUP(layer.id + " - " + std::string(item.tile->id)); + prepareTile(*item.tile); + item.bucket->render(*this, layer, item.tile->id, item.tile->matrix); } } diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index 5fb4edcf92..10e5c02d82 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -4,6 +4,7 @@ #include <mbgl/map/tile.hpp> #include <mbgl/map/transform_state.hpp> #include <mbgl/layer/symbol_layer.hpp> +#include <mbgl/layer/custom_layer.hpp> #include <mbgl/sprite/sprite_store.hpp> #include <mbgl/sprite/sprite_atlas.hpp> #include <mbgl/style/style_layer.hpp> @@ -100,18 +101,18 @@ StyleLayer* Style::getLayer(const std::string& id) const { return it != layers.end() ? it->get() : nullptr; } -void Style::addLayer(std::unique_ptr<StyleLayer> layer) { +void Style::addLayer(std::unique_ptr<StyleLayer> layer, mapbox::util::optional<std::string> before) { if (SymbolLayer* symbolLayer = layer->as<SymbolLayer>()) { if (!symbolLayer->spriteAtlas) { symbolLayer->spriteAtlas = spriteAtlas.get(); } } - layers.emplace_back(std::move(layer)); -} + if (CustomLayer* customLayer = layer->as<CustomLayer>()) { + customLayer->initialize(); + } -void Style::addLayer(std::unique_ptr<StyleLayer> layer, const std::string& before) { - layers.emplace(findLayer(before), std::move(layer)); + layers.emplace(before ? findLayer(*before) : layers.end(), std::move(layer)); } void Style::removeLayer(const std::string& id) { @@ -249,6 +250,11 @@ RenderData Style::getRenderData() const { continue; } + if (layer->is<CustomLayer>()) { + result.order.emplace_back(*layer); + continue; + } + Source* source = getSource(layer->source); if (!source) { Log::Warning(Event::Render, "can't find source for layer '%s'", layer->id.c_str()); diff --git a/src/mbgl/style/style.hpp b/src/mbgl/style/style.hpp index 5966866679..ed5df04256 100644 --- a/src/mbgl/style/style.hpp +++ b/src/mbgl/style/style.hpp @@ -11,6 +11,8 @@ #include <mbgl/util/chrono.hpp> #include <mbgl/util/worker.hpp> +#include <mapbox/optional.hpp> + #include <cstdint> #include <string> #include <vector> @@ -88,8 +90,8 @@ public: std::vector<std::unique_ptr<StyleLayer>> getLayers() const; StyleLayer* getLayer(const std::string& id) const; - void addLayer(std::unique_ptr<StyleLayer>); - void addLayer(std::unique_ptr<StyleLayer>, const std::string& beforeLayerID); + void addLayer(std::unique_ptr<StyleLayer>, + mapbox::util::optional<std::string> beforeLayerID = {}); void removeLayer(const std::string& layerID); RenderData getRenderData() const; diff --git a/src/mbgl/style/style_render_parameters.hpp b/src/mbgl/style/style_render_parameters.hpp new file mode 100644 index 0000000000..16ce99177a --- /dev/null +++ b/src/mbgl/style/style_render_parameters.hpp @@ -0,0 +1,18 @@ +#ifndef STYLE_RENDER_PARAMETERS +#define STYLE_RENDER_PARAMETERS + +namespace mbgl { + +class TransformState; + +class StyleRenderParameters { +public: + StyleRenderParameters(const TransformState& state_) + : state(state_) {} + + const TransformState& state; +}; + +} + +#endif |