diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2017-06-13 10:50:16 +0300 |
---|---|---|
committer | Ivo van Dongen <ivovandongen@users.noreply.github.com> | 2017-07-18 10:45:12 +0200 |
commit | 3832f8d0d8194b81ea34a045e19b0d5bc7a89e25 (patch) | |
tree | bbb4b277e1e74bde457271b9b9a5b6a9ec3ad5fb | |
parent | 39a732d7ae3cb1b927d94c4b1154b42d9565356a (diff) | |
download | qtlocation-mapboxgl-3832f8d0d8194b81ea34a045e19b0d5bc7a89e25.tar.gz |
[core] renderer interface
44 files changed, 1013 insertions, 538 deletions
diff --git a/bin/render.cpp b/bin/render.cpp index 74fd6fdcc4..fedd8bbda6 100644 --- a/bin/render.cpp +++ b/bin/render.cpp @@ -8,6 +8,8 @@ #include <mbgl/util/default_thread_pool.hpp> #include <mbgl/storage/default_file_source.hpp> #include <mbgl/style/style.hpp> +#include <mbgl/renderer/renderer.hpp> +#include <mbgl/renderer/async_renderer_frontend.hpp> #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunknown-pragmas" @@ -88,7 +90,8 @@ int main(int argc, char *argv[]) { OffscreenView view(backend.getContext(), { static_cast<uint32_t>(width * pixelRatio), static_cast<uint32_t>(height * pixelRatio) }); ThreadPool threadPool(4); - Map map(backend, MapObserver::nullObserver(), mbgl::Size { width, height }, pixelRatio, fileSource, threadPool, MapMode::Still); + AsyncRendererFrontend rendererFrontend(std::make_unique<Renderer>(backend, pixelRatio, fileSource, threadPool), view); + Map map(rendererFrontend, MapObserver::nullObserver(), mbgl::Size { width, height }, pixelRatio, fileSource, threadPool, MapMode::Still); if (style_path.find("://") == std::string::npos) { style_path = std::string("file://") + style_path; @@ -103,7 +106,7 @@ int main(int argc, char *argv[]) { map.setDebug(debug ? mbgl::MapDebugOptions::TileBorders | mbgl::MapDebugOptions::ParseStatus : mbgl::MapDebugOptions::NoDebug); } - map.renderStill(view, [&](std::exception_ptr error) { + map.renderStill([&](std::exception_ptr error) { try { if (error) { std::rethrow_exception(error); diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake index 28dd25376b..d22aa58fef 100644 --- a/cmake/core-files.cmake +++ b/cmake/core-files.cmake @@ -108,7 +108,6 @@ set(MBGL_CORE_FILES include/mbgl/map/map.hpp include/mbgl/map/map_observer.hpp include/mbgl/map/mode.hpp - include/mbgl/map/query.hpp include/mbgl/map/view.hpp src/mbgl/map/backend.cpp src/mbgl/map/backend_scope.cpp @@ -160,6 +159,9 @@ set(MBGL_CORE_FILES src/mbgl/programs/uniforms.hpp # renderer + include/mbgl/renderer/query.hpp + include/mbgl/renderer/renderer.hpp + include/mbgl/renderer/renderer_frontend.hpp src/mbgl/renderer/bucket.hpp src/mbgl/renderer/bucket_parameters.cpp src/mbgl/renderer/bucket_parameters.hpp @@ -196,6 +198,10 @@ set(MBGL_CORE_FILES src/mbgl/renderer/render_style_observer.hpp src/mbgl/renderer/render_tile.cpp src/mbgl/renderer/render_tile.hpp + src/mbgl/renderer/renderer.cpp + src/mbgl/renderer/renderer_impl.cpp + src/mbgl/renderer/renderer_impl.hpp + src/mbgl/renderer/renderer_observer.hpp src/mbgl/renderer/style_diff.cpp src/mbgl/renderer/style_diff.hpp src/mbgl/renderer/tile_parameters.hpp diff --git a/cmake/render.cmake b/cmake/render.cmake index 407e1ca8ef..350350dc76 100644 --- a/cmake/render.cmake +++ b/cmake/render.cmake @@ -1,4 +1,6 @@ add_executable(mbgl-render + platform/default/mbgl/renderer/async_renderer_frontend.cpp + platform/default/mbgl/renderer/async_renderer_frontend.hpp bin/render.cpp ) diff --git a/cmake/test-files.cmake b/cmake/test-files.cmake index cdca1db280..ca7182581b 100644 --- a/cmake/test-files.cmake +++ b/cmake/test-files.cmake @@ -60,6 +60,8 @@ set(MBGL_TEST_FILES test/src/mbgl/test/stub_geometry_tile_feature.hpp test/src/mbgl/test/stub_layer_observer.hpp test/src/mbgl/test/stub_render_source_observer.hpp + test/src/mbgl/test/stub_renderer_frontend.cpp + test/src/mbgl/test/stub_renderer_frontend.hpp test/src/mbgl/test/stub_style_observer.hpp test/src/mbgl/test/stub_tile_observer.hpp test/src/mbgl/test/test.cpp diff --git a/include/mbgl/map/backend.hpp b/include/mbgl/map/backend.hpp index 434f68779c..3347086571 100644 --- a/include/mbgl/map/backend.hpp +++ b/include/mbgl/map/backend.hpp @@ -1,6 +1,7 @@ #pragma once #include <mbgl/map/view.hpp> +#include <mbgl/map/backend_scope.hpp> #include <mbgl/util/image.hpp> #include <mbgl/util/size.hpp> @@ -15,8 +16,6 @@ using ProcAddress = void (*)(); using FramebufferID = uint32_t; } // namespace gl -class BackendScope; - class Backend { public: Backend(); @@ -28,9 +27,9 @@ public: // Called prior to rendering to update the internally assumed OpenGL state. virtual void updateAssumedState() = 0; - // Called when the map needs to be rendered; the backend should call Map::render() at some point - // in the near future. (Not called for Map::renderStill() mode.) - virtual void invalidate() = 0; + inline virtual BackendScope::ScopeType getScopeType() const { + return BackendScope::ScopeType::Explicit; + }; protected: // Called with the name of an OpenGL extension that should be loaded. Backend implementations diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index 22ac100c40..14f42d7fd5 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -4,13 +4,10 @@ #include <mbgl/util/chrono.hpp> #include <mbgl/map/map_observer.hpp> #include <mbgl/map/mode.hpp> -#include <mbgl/util/geo.hpp> -#include <mbgl/util/feature.hpp> #include <mbgl/util/noncopyable.hpp> #include <mbgl/util/size.hpp> #include <mbgl/annotation/annotation.hpp> #include <mbgl/map/camera.hpp> -#include <mbgl/map/query.hpp> #include <cstdint> #include <string> @@ -24,6 +21,7 @@ class Backend; class View; class FileSource; class Scheduler; +class RendererFrontend; namespace style { class Image; @@ -32,30 +30,25 @@ class Style; class Map : private util::noncopyable { public: - explicit Map(Backend&, + explicit Map(RendererFrontend&, MapObserver&, Size size, float pixelRatio, FileSource&, Scheduler&, MapMode mapMode = MapMode::Continuous, - GLContextMode contextMode = GLContextMode::Unique, ConstrainMode constrainMode = ConstrainMode::HeightOnly, - ViewportMode viewportMode = ViewportMode::Default, - optional<std::string> programCacheDir = optional<std::string>()); + ViewportMode viewportMode = ViewportMode::Default); ~Map(); // Register a callback that will get called (on the render thread) when all resources have // been loaded and a complete render occurs. using StillImageCallback = std::function<void (std::exception_ptr)>; - void renderStill(View&, StillImageCallback callback); + void renderStill(StillImageCallback callback); // Triggers a repaint. void triggerRepaint(); - // Main render function. - void render(View&); - style::Style& getStyle(); const style::Style& getStyle() const; @@ -150,13 +143,6 @@ public: void updateAnnotation(AnnotationID, const Annotation&); void removeAnnotation(AnnotationID); - // Feature queries - std::vector<Feature> queryRenderedFeatures(const ScreenCoordinate&, const RenderedQueryOptions& options = {}); - std::vector<Feature> queryRenderedFeatures(const ScreenBox&, const RenderedQueryOptions& options = {}); - std::vector<Feature> querySourceFeatures(const std::string& sourceID, const SourceQueryOptions& options = {}); - - AnnotationIDs queryPointAnnotations(const ScreenBox&); - // Tile prefetching // // When loading a map, if `PrefetchZoomDelta` is set to any number greater than 0, the map will @@ -166,9 +152,6 @@ public: void setPrefetchZoomDelta(uint8_t delta); uint8_t getPrefetchZoomDelta() const; - // Memory - void onLowMemory(); - // Debug void setDebug(MapDebugOptions); void cycleDebugOptions(); diff --git a/include/mbgl/map/query.hpp b/include/mbgl/renderer/query.hpp index b9d5f21a44..b9d5f21a44 100644 --- a/include/mbgl/map/query.hpp +++ b/include/mbgl/renderer/query.hpp diff --git a/include/mbgl/renderer/renderer.hpp b/include/mbgl/renderer/renderer.hpp new file mode 100644 index 0000000000..6626f74d3d --- /dev/null +++ b/include/mbgl/renderer/renderer.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include <mbgl/map/mode.hpp> +#include <mbgl/renderer/query.hpp> +#include <mbgl/annotation/annotation.hpp> +#include <mbgl/util/geo.hpp> +#include <mbgl/util/geo.hpp> + +#include <functional> +#include <memory> +#include <string> +#include <vector> + +namespace mbgl { + +class Backend; +class FileSource; +class RendererObserver; +class RenderedQueryOptions; +class Scheduler; +class SourceQueryOptions; +class UpdateParameters; +class View; + +class Renderer { +public: + Renderer(Backend&, float pixelRatio_, FileSource&, Scheduler&, + GLContextMode = GLContextMode::Unique, + const optional<std::string> programCacheDir = optional<std::string>()); + ~Renderer(); + + void setObserver(RendererObserver*); + + void render(View& view, const UpdateParameters&); + + // Feature queries + std::vector<Feature> queryRenderedFeatures(const ScreenLineString&, const RenderedQueryOptions& options = {}) const; + std::vector<Feature> queryRenderedFeatures(const ScreenCoordinate& point, const RenderedQueryOptions& options = {}) const; + std::vector<Feature> queryRenderedFeatures(const ScreenBox& box, const RenderedQueryOptions& options = {}) const; + std::vector<Feature> querySourceFeatures(const std::string& sourceID, const SourceQueryOptions& options = {}) const; + AnnotationIDs queryPointAnnotations(const ScreenBox& box) const; + + // Debug + void dumpDebugLogs(); + + // Memory + void onLowMemory(); + +private: + class Impl; + std::unique_ptr<Impl> impl; +}; + +} // namespace mbgl diff --git a/include/mbgl/renderer/renderer_frontend.hpp b/include/mbgl/renderer/renderer_frontend.hpp new file mode 100644 index 0000000000..f72b0ccdde --- /dev/null +++ b/include/mbgl/renderer/renderer_frontend.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include <memory> + +namespace mbgl { + +class RendererObserver; +class UpdateParameters; + +// The RenderFrontend is the bridge between the Map and +// platform used to update and observer the Renderer +// +// It hides any threading specifics and always replies on +// the original thread. +class RendererFrontend { +public: + + virtual ~RendererFrontend() = default; + + // Must synchronously clean up the Renderer if set + virtual void reset() = 0; + + // Implementer must bind the renderer observer to the renderer in a + // appropriate manner so that the callbacks occur on the main thread + virtual void setObserver(RendererObserver&) = 0; + + // Coalescing updates is up to the implementer + virtual void update(std::shared_ptr<UpdateParameters>) = 0; +}; + +} // namespace mbgl diff --git a/platform/android/config.cmake b/platform/android/config.cmake index 5a6950a23f..9fdbfa1b45 100644 --- a/platform/android/config.cmake +++ b/platform/android/config.cmake @@ -57,6 +57,8 @@ macro(mbgl_platform_core) PRIVATE platform/default/bidi.cpp PRIVATE platform/default/thread_local.cpp PRIVATE platform/default/utf.cpp + PRIVATE platform/default/mbgl/renderer/default_render_bridge.cpp + PRIVATE platform/default/mbgl/renderer/default_render_bridge.hpp # Image handling PRIVATE platform/default/png_writer.cpp diff --git a/platform/default/mbgl/gl/headless_backend.cpp b/platform/default/mbgl/gl/headless_backend.cpp index df3a517823..82cf7b38fd 100644 --- a/platform/default/mbgl/gl/headless_backend.cpp +++ b/platform/default/mbgl/gl/headless_backend.cpp @@ -16,7 +16,7 @@ HeadlessBackend::HeadlessBackend(std::shared_ptr<HeadlessDisplay> display_) } HeadlessBackend::~HeadlessBackend() { - BackendScope scope(*this); + BackendScope guard { *this, getScopeType() }; context.reset(); } @@ -44,8 +44,4 @@ void HeadlessBackend::updateAssumedState() { // no-op } -void HeadlessBackend::invalidate() { - assert(false); -} - } // namespace mbgl diff --git a/platform/default/mbgl/gl/headless_backend.hpp b/platform/default/mbgl/gl/headless_backend.hpp index 26033acf82..7b9f7f5ad3 100644 --- a/platform/default/mbgl/gl/headless_backend.hpp +++ b/platform/default/mbgl/gl/headless_backend.hpp @@ -17,8 +17,6 @@ public: void updateAssumedState() override; - void invalidate() override; - struct Impl { virtual ~Impl() = default; virtual void activateContext() = 0; diff --git a/platform/default/mbgl/renderer/async_renderer_frontend.cpp b/platform/default/mbgl/renderer/async_renderer_frontend.cpp new file mode 100644 index 0000000000..37b4e60cf6 --- /dev/null +++ b/platform/default/mbgl/renderer/async_renderer_frontend.cpp @@ -0,0 +1,38 @@ +#include "async_renderer_frontend.hpp" +#include <mbgl/renderer/renderer.hpp> + +namespace mbgl { + +AsyncRendererFrontend::AsyncRendererFrontend(std::unique_ptr<Renderer> renderer_, View& view_) + : renderer(std::move(renderer_)) + , view(view_) + , asyncInvalidate([this] { + if (renderer && updateParameters) { + renderer->render(view, *updateParameters); + } + }) { +} + +AsyncRendererFrontend::~AsyncRendererFrontend() = default; + +void AsyncRendererFrontend::reset() { + assert(renderer); + renderer.reset(); +} + +void AsyncRendererFrontend::update(std::shared_ptr<UpdateParameters> updateParameters_) { + updateParameters = updateParameters_; + asyncInvalidate.send(); +} + +void AsyncRendererFrontend::setObserver(RendererObserver& observer_) { + assert(renderer); + renderer->setObserver(&observer_); +} + +Renderer* AsyncRendererFrontend::getRenderer() { + assert(renderer); + return renderer.get(); +} + +} // namespace mbgl diff --git a/platform/default/mbgl/renderer/async_renderer_frontend.hpp b/platform/default/mbgl/renderer/async_renderer_frontend.hpp new file mode 100644 index 0000000000..00cf3a480c --- /dev/null +++ b/platform/default/mbgl/renderer/async_renderer_frontend.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include <mbgl/map/view.hpp> +#include <mbgl/renderer/renderer_frontend.hpp> +#include <mbgl/util/async_task.hpp> + +#include <memory> +#include <vector> + +namespace mbgl { + +class Renderer; + +// Default implementation for RendererFrontend +class AsyncRendererFrontend : public mbgl::RendererFrontend { +public: + AsyncRendererFrontend(std::unique_ptr<Renderer>, View&); + ~AsyncRendererFrontend() override; + + void reset() override; + + void update(std::shared_ptr<UpdateParameters> updateParameters_) override; + + void setObserver(RendererObserver& observer_) override; + + Renderer* getRenderer(); + +private: + std::unique_ptr<Renderer> renderer; + View& view; + std::shared_ptr<UpdateParameters> updateParameters; + util::AsyncTask asyncInvalidate; +}; + +} // namespace mbgl diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index 78e98e2a28..2599996f7c 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -12,7 +12,6 @@ #include <mbgl/style/style.hpp> #include <mbgl/style/image.hpp> #include <mbgl/map/backend_scope.hpp> -#include <mbgl/map/query.hpp> #include <mbgl/util/premultiply.hpp> #include <unistd.h> diff --git a/src/mbgl/annotation/annotation_manager.cpp b/src/mbgl/annotation/annotation_manager.cpp index ec5a360a90..dbf8387ae0 100644 --- a/src/mbgl/annotation/annotation_manager.cpp +++ b/src/mbgl/annotation/annotation_manager.cpp @@ -215,7 +215,6 @@ void AnnotationManager::addImage(std::unique_ptr<style::Image> image) { images.erase(id); auto inserted = images.emplace(id, style::Image(id, image->getImage().clone(), image->getPixelRatio(), image->isSdf())); - style.get().impl->addImage(std::make_unique<style::Image>(inserted.first->second)); } diff --git a/src/mbgl/geometry/feature_index.cpp b/src/mbgl/geometry/feature_index.cpp index b1594388c4..4c4e985369 100644 --- a/src/mbgl/geometry/feature_index.cpp +++ b/src/mbgl/geometry/feature_index.cpp @@ -1,12 +1,12 @@ #include <mbgl/geometry/feature_index.hpp> #include <mbgl/renderer/render_style.hpp> #include <mbgl/renderer/render_layer.hpp> +#include <mbgl/renderer/query.hpp> #include <mbgl/renderer/layers/render_symbol_layer.hpp> #include <mbgl/text/collision_tile.hpp> #include <mbgl/util/constants.hpp> #include <mbgl/util/math.hpp> #include <mbgl/math/minmax.hpp> -#include <mbgl/map/query.hpp> #include <mbgl/style/filter.hpp> #include <mbgl/style/filter_evaluator.hpp> #include <mbgl/tile/geometry_tile.hpp> diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 172b2bf851..7d57f6863e 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -1,23 +1,19 @@ #include <mbgl/map/map.hpp> #include <mbgl/map/camera.hpp> -#include <mbgl/map/view.hpp> -#include <mbgl/map/backend.hpp> -#include <mbgl/map/backend_scope.hpp> #include <mbgl/map/transform.hpp> #include <mbgl/map/transform_state.hpp> #include <mbgl/annotation/annotation_manager.hpp> #include <mbgl/style/style_impl.hpp> #include <mbgl/style/observer.hpp> #include <mbgl/renderer/update_parameters.hpp> -#include <mbgl/renderer/painter.hpp> -#include <mbgl/renderer/render_source.hpp> -#include <mbgl/renderer/render_style.hpp> -#include <mbgl/renderer/render_style_observer.hpp> +#include <mbgl/renderer/renderer_frontend.hpp> +#include <mbgl/renderer/renderer_observer.hpp> +#include <mbgl/storage/file_source.hpp> +#include <mbgl/storage/resource.hpp> +#include <mbgl/storage/response.hpp> #include <mbgl/util/constants.hpp> -#include <mbgl/util/exception.hpp> #include <mbgl/util/math.hpp> #include <mbgl/util/exception.hpp> -#include <mbgl/util/async_task.hpp> #include <mbgl/util/mapbox.hpp> #include <mbgl/util/tile_coordinate.hpp> #include <mbgl/actor/scheduler.hpp> @@ -29,69 +25,58 @@ namespace mbgl { using namespace style; -enum class RenderState : uint8_t { - Never, - Partial, - Fully, -}; - struct StillImageRequest { - StillImageRequest(View& view_, Map::StillImageCallback&& callback_) - : view(view_), callback(std::move(callback_)) { + StillImageRequest(Map::StillImageCallback&& callback_) + : callback(std::move(callback_)) { } - View& view; Map::StillImageCallback callback; }; class Map::Impl : public style::Observer, - public RenderStyleObserver { + public RendererObserver { public: Impl(Map&, - Backend&, + RendererFrontend&, MapObserver&, float pixelRatio, FileSource&, Scheduler&, MapMode, - GLContextMode, ConstrainMode, - ViewportMode, - optional<std::string> programCacheDir); + ViewportMode); + + ~Impl(); + // StyleObserver void onSourceChanged(style::Source&) override; void onUpdate(Update) override; - void onInvalidate() override; void onStyleLoading() override; void onStyleLoaded() override; void onStyleError(std::exception_ptr) override; - void onResourceError(std::exception_ptr) override; - void render(View&); - void renderStill(); + // RendererObserver + void onInvalidate() override; + void onResourceError(std::exception_ptr) override; + void onWillStartRenderingFrame() override; + void onDidFinishRenderingFrame(RenderMode, bool) override; + void onWillStartRenderingMap() override; + void onDidFinishRenderingMap() override; Map& map; MapObserver& observer; - Backend& backend; + RendererFrontend& rendererFrontend; FileSource& fileSource; Scheduler& scheduler; - RenderState renderState = RenderState::Never; Transform transform; const MapMode mode; - const GLContextMode contextMode; const float pixelRatio; - const optional<std::string> programCacheDir; MapDebugOptions debugOptions { MapDebugOptions::NoDebug }; - Update updateFlags = Update::Nothing; - - std::unique_ptr<Painter> painter; std::unique_ptr<Style> style; - std::unique_ptr<RenderStyle> renderStyle; - AnnotationManager annotationManager; bool cameraMutated = false; @@ -99,81 +84,66 @@ public: uint8_t prefetchZoomDelta = util::DEFAULT_PREFETCH_ZOOM_DELTA; bool loading = false; - - util::AsyncTask asyncInvalidate; + bool rendererFullyLoaded; std::unique_ptr<StillImageRequest> stillImageRequest; }; -Map::Map(Backend& backend, +Map::Map(RendererFrontend& rendererFrontend, MapObserver& mapObserver, const Size size, const float pixelRatio, FileSource& fileSource, Scheduler& scheduler, MapMode mapMode, - GLContextMode contextMode, ConstrainMode constrainMode, - ViewportMode viewportMode, - optional<std::string> programCacheDir) + ViewportMode viewportMode) : impl(std::make_unique<Impl>(*this, - backend, + rendererFrontend, mapObserver, pixelRatio, fileSource, scheduler, mapMode, - contextMode, constrainMode, - viewportMode, - programCacheDir)) { + viewportMode)) { impl->transform.resize(size); } Map::Impl::Impl(Map& map_, - Backend& backend_, + RendererFrontend& frontend, MapObserver& mapObserver, float pixelRatio_, FileSource& fileSource_, Scheduler& scheduler_, MapMode mode_, - GLContextMode contextMode_, ConstrainMode constrainMode_, - ViewportMode viewportMode_, - optional<std::string> programCacheDir_) + ViewportMode viewportMode_) : map(map_), observer(mapObserver), - backend(backend_), + rendererFrontend(frontend), fileSource(fileSource_), scheduler(scheduler_), transform(observer, constrainMode_, viewportMode_), mode(mode_), - contextMode(contextMode_), pixelRatio(pixelRatio_), - programCacheDir(std::move(programCacheDir_)), style(std::make_unique<Style>(scheduler, fileSource, pixelRatio)), - annotationManager(*style), - asyncInvalidate([this] { - if (mode == MapMode::Continuous) { - backend.invalidate(); - } else { - renderStill(); - } - }) { + annotationManager(*style) { + style->impl->setObserver(this); + rendererFrontend.setObserver(*this); } -Map::~Map() { - BackendScope guard(impl->backend); +Map::Impl::~Impl() { + // Explicitly reset the RendererFrontend first to ensure it releases + // All shared resources (AnnotationManager) + rendererFrontend.reset(); +}; - // Explicit resets currently necessary because these abandon resources that need to be - // cleaned up by context.reset(); - impl->renderStyle.reset(); - impl->painter.reset(); -} +Map::~Map() = default; -void Map::renderStill(View& view, StillImageCallback callback) { +void Map::renderStill(StillImageCallback callback) { if (!callback) { Log::Error(Event::General, "StillImageCallback not set"); return; @@ -194,129 +164,53 @@ void Map::renderStill(View& view, StillImageCallback callback) { return; } - impl->stillImageRequest = std::make_unique<StillImageRequest>(view, std::move(callback)); - impl->onUpdate(Update::Repaint); -} - -void Map::Impl::renderStill() { - if (!stillImageRequest) { - return; - } + impl->stillImageRequest = std::make_unique<StillImageRequest>(std::move(callback)); - // TODO: determine whether we need activate/deactivate - BackendScope guard(backend); - render(stillImageRequest->view); + impl->onUpdate(Update::Repaint); } void Map::triggerRepaint() { - impl->backend.invalidate(); -} - -void Map::render(View& view) { - impl->render(view); + impl->onUpdate(Update::Repaint); } -void Map::Impl::render(View& view) { - TimePoint timePoint = mode == MapMode::Continuous - ? Clock::now() - : Clock::time_point::max(); - - transform.updateTransitions(timePoint); +#pragma mark - Map::Impl RendererObserver - if (updateFlags & Update::AnnotationData) { - annotationManager.updateData(); +void Map::Impl::onWillStartRenderingMap() { + if (mode == MapMode::Continuous) { + observer.onWillStartRenderingMap(); } +} - updateFlags = Update::Nothing; - - gl::Context& context = backend.getContext(); - if (!painter) { - renderStyle = std::make_unique<RenderStyle>(scheduler, fileSource); - renderStyle->setObserver(this); - painter = std::make_unique<Painter>(context, transform.getState(), pixelRatio, programCacheDir); +void Map::Impl::onWillStartRenderingFrame() { + if (mode == MapMode::Continuous) { + observer.onWillStartRenderingFrame(); } +} - renderStyle->update({ - mode, - pixelRatio, - debugOptions, - timePoint, - transform.getState(), - style->impl->getGlyphURL(), - style->impl->spriteLoaded, - style->impl->getTransitionOptions(), - style->impl->getLight()->impl, - style->impl->getImageImpls(), - style->impl->getSourceImpls(), - style->impl->getLayerImpls(), - scheduler, - fileSource, - annotationManager, - prefetchZoomDelta - }); - - bool loaded = style->impl->isLoaded() && renderStyle->isLoaded(); +void Map::Impl::onDidFinishRenderingFrame(RenderMode renderMode, bool needsRepaint) { + rendererFullyLoaded = renderMode == RenderMode::Full; if (mode == MapMode::Continuous) { - if (renderState == RenderState::Never) { - observer.onWillStartRenderingMap(); - } - - observer.onWillStartRenderingFrame(); - - FrameData frameData { timePoint, - pixelRatio, - mode, - contextMode, - debugOptions }; - - backend.updateAssumedState(); - - painter->render(*renderStyle, - frameData, - view); - - painter->cleanup(); - - observer.onDidFinishRenderingFrame(loaded - ? MapObserver::RenderMode::Full - : MapObserver::RenderMode::Partial); - - if (!loaded) { - renderState = RenderState::Partial; - } else if (renderState != RenderState::Fully) { - renderState = RenderState::Fully; - observer.onDidFinishRenderingMap(MapObserver::RenderMode::Full); - if (loading) { - loading = false; - observer.onDidFinishLoadingMap(); - } - } + observer.onDidFinishRenderingFrame(MapObserver::RenderMode(renderMode)); - // Schedule an update if we need to paint another frame due to transitions or - // animations that are still in progress - if (renderStyle->hasTransitions() || painter->needsAnimation() || transform.inTransition()) { + if (needsRepaint || transform.inTransition()) { onUpdate(Update::Repaint); } - } else if (stillImageRequest && loaded) { - FrameData frameData { timePoint, - pixelRatio, - mode, - contextMode, - debugOptions }; - - backend.updateAssumedState(); - - painter->render(*renderStyle, - frameData, - view); + } +} +void Map::Impl::onDidFinishRenderingMap() { + if (mode == MapMode::Continuous && loading) { + observer.onDidFinishRenderingMap(MapObserver::RenderMode::Full); + if (loading) { + loading = false; + observer.onDidFinishLoadingMap(); + } + } else if (stillImageRequest) { auto request = std::move(stillImageRequest); request->callback(nullptr); - - painter->cleanup(); } -} +}; #pragma mark - Style @@ -737,60 +631,6 @@ void Map::removeAnnotation(AnnotationID annotation) { impl->onUpdate(Update::AnnotationData); } -#pragma mark - Feature query api - -std::vector<Feature> Map::queryRenderedFeatures(const ScreenCoordinate& point, const RenderedQueryOptions& options) { - if (!impl->renderStyle) return {}; - - return impl->renderStyle->queryRenderedFeatures( - { point }, - impl->transform.getState(), - options - ); -} - -std::vector<Feature> Map::queryRenderedFeatures(const ScreenBox& box, const RenderedQueryOptions& options) { - if (!impl->renderStyle) return {}; - - return impl->renderStyle->queryRenderedFeatures( - { - box.min, - { box.max.x, box.min.y }, - box.max, - { box.min.x, box.max.y }, - box.min - }, - impl->transform.getState(), - options - ); -} - -std::vector<Feature> Map::querySourceFeatures(const std::string& sourceID, const SourceQueryOptions& options) { - if (!impl->renderStyle) return {}; - - const RenderSource* source = impl->renderStyle->getRenderSource(sourceID); - if (!source) return {}; - - return source->querySourceFeatures(options); -} - -AnnotationIDs Map::queryPointAnnotations(const ScreenBox& box) { - RenderedQueryOptions options; - options.layerIDs = {{ AnnotationManager::PointLayerID }}; - auto features = queryRenderedFeatures(box, options); - std::set<AnnotationID> set; - for (auto &feature : features) { - assert(feature.id); - assert(feature.id->is<uint64_t>()); - assert(feature.id->get<uint64_t>() <= std::numeric_limits<AnnotationID>::max()); - set.insert(static_cast<AnnotationID>(feature.id->get<uint64_t>())); - } - AnnotationIDs ids; - ids.reserve(set.size()); - std::move(set.begin(), set.end(), std::back_inserter(ids)); - return ids; -} - #pragma mark - Toggles void Map::setDebug(MapDebugOptions debugOptions) { @@ -826,10 +666,6 @@ MapDebugOptions Map::getDebug() const { return impl->debugOptions; } -bool Map::isFullyLoaded() const { - return impl->style->impl->isLoaded() && impl->renderStyle && impl->renderStyle->isLoaded(); -} - void Map::setPrefetchZoomDelta(uint8_t delta) { impl->prefetchZoomDelta = delta; } @@ -838,32 +674,54 @@ uint8_t Map::getPrefetchZoomDelta() const { return impl->prefetchZoomDelta; } -void Map::onLowMemory() { - if (impl->painter) { - BackendScope guard(impl->backend); - impl->painter->cleanup(); - } - if (impl->renderStyle) { - impl->renderStyle->onLowMemory(); - impl->backend.invalidate(); - } +bool Map::isFullyLoaded() const { + return impl->style->impl->isLoaded() && impl->rendererFullyLoaded; } void Map::Impl::onSourceChanged(style::Source& source) { observer.onSourceChanged(source); } -void Map::Impl::onUpdate(Update flags) { - updateFlags |= flags; - asyncInvalidate.send(); -} - void Map::Impl::onInvalidate() { onUpdate(Update::Repaint); } +void Map::Impl::onUpdate(Update flags) { + TimePoint timePoint = mode == MapMode::Continuous ? Clock::now() : Clock::time_point::max(); + + transform.updateTransitions(timePoint); + + if (flags & Update::AnnotationData) { + annotationManager.updateData(); + } + + UpdateParameters params = { + style->impl->isLoaded(), + mode, + pixelRatio, + debugOptions, + timePoint, + transform.getState(), + style->impl->getGlyphURL(), + style->impl->spriteLoaded, + style->impl->getTransitionOptions(), + style->impl->getLight()->impl, + style->impl->getImageImpls(), + style->impl->getSourceImpls(), + style->impl->getLayerImpls(), + scheduler, + fileSource, + annotationManager, + prefetchZoomDelta, + bool(stillImageRequest) + }; + + rendererFrontend.update(std::make_shared<UpdateParameters>(std::move(params))); +} + void Map::Impl::onStyleLoading() { loading = true; + rendererFullyLoaded = false; observer.onWillStartLoadingMap(); } @@ -894,9 +752,6 @@ void Map::Impl::onResourceError(std::exception_ptr error) { void Map::dumpDebugLogs() const { Log::Info(Event::General, "--------------------------------------------------------------------------------"); impl->style->impl->dumpDebugLogs(); - if (impl->renderStyle) { - impl->renderStyle->dumpDebugLogs(); - } Log::Info(Event::General, "--------------------------------------------------------------------------------"); } diff --git a/src/mbgl/renderer/painter.cpp b/src/mbgl/renderer/painter.cpp index 9ab50e83b0..8e76d6bfc4 100644 --- a/src/mbgl/renderer/painter.cpp +++ b/src/mbgl/renderer/painter.cpp @@ -88,11 +88,9 @@ static gl::VertexVector<ExtrusionTextureLayoutVertex> extrusionTextureVertices() Painter::Painter(gl::Context& context_, - const TransformState& state_, float pixelRatio, const optional<std::string>& programCacheDir) : context(context_), - state(state_), tileVertexBuffer(context.createVertexBuffer(tileVertices())), rasterVertexBuffer(context.createVertexBuffer(rasterVertices())), extrusionTextureVertexBuffer(context.createVertexBuffer(extrusionTextureVertices())), diff --git a/src/mbgl/renderer/painter.hpp b/src/mbgl/renderer/painter.hpp index 0b6ee3497e..08ca76bf16 100644 --- a/src/mbgl/renderer/painter.hpp +++ b/src/mbgl/renderer/painter.hpp @@ -70,7 +70,7 @@ struct FrameData { class Painter : private util::noncopyable { public: - Painter(gl::Context&, const TransformState&, float pixelRatio, const optional<std::string>& programCacheDir); + Painter(gl::Context&, float pixelRatio, const optional<std::string>& programCacheDir); ~Painter(); void render(RenderStyle&, @@ -125,6 +125,8 @@ public: gl::Context& context; + TransformState state; + algorithm::ClipIDGenerator clipIDGenerator; mat4 projMatrix; @@ -138,8 +140,6 @@ public: return identity; }(); - const TransformState& state; - FrameData frame; int indent = 0; diff --git a/src/mbgl/renderer/render_style.cpp b/src/mbgl/renderer/render_style.cpp index 47639a57e7..a0f1fd4049 100644 --- a/src/mbgl/renderer/render_style.cpp +++ b/src/mbgl/renderer/render_style.cpp @@ -17,6 +17,7 @@ #include <mbgl/renderer/layers/render_symbol_layer.hpp> #include <mbgl/renderer/style_diff.hpp> #include <mbgl/renderer/image_manager.hpp> +#include <mbgl/renderer/query.hpp> #include <mbgl/style/style.hpp> #include <mbgl/style/source_impl.hpp> #include <mbgl/style/transition_options.hpp> @@ -24,7 +25,6 @@ #include <mbgl/text/glyph_manager.hpp> #include <mbgl/geometry/line_atlas.hpp> #include <mbgl/map/backend_scope.hpp> -#include <mbgl/map/query.hpp> #include <mbgl/tile/tile.hpp> #include <mbgl/util/math.hpp> #include <mbgl/util/string.hpp> diff --git a/src/mbgl/renderer/renderer.cpp b/src/mbgl/renderer/renderer.cpp new file mode 100644 index 0000000000..11d7726c79 --- /dev/null +++ b/src/mbgl/renderer/renderer.cpp @@ -0,0 +1,78 @@ +#include <mbgl/renderer/renderer.hpp> +#include <mbgl/renderer/renderer_impl.hpp> +#include <mbgl/renderer/update_parameters.hpp> +#include <mbgl/annotation/annotation_manager.hpp> + +namespace mbgl { + +Renderer::Renderer(Backend& backend, + float pixelRatio_, + FileSource& fileSource_, + Scheduler& scheduler_, + GLContextMode contextMode_, + const optional<std::string> programCacheDir_) + : impl(std::make_unique<Impl>(backend, pixelRatio_, fileSource_, scheduler_, + contextMode_, std::move(programCacheDir_))) { +} + +Renderer::~Renderer() = default; + +void Renderer::setObserver(RendererObserver* observer) { + impl->setObserver(observer); +} + +void Renderer::render(View& view, const UpdateParameters& updateParameters) { + impl->render(view, updateParameters); +} + +std::vector<Feature> Renderer::queryRenderedFeatures(const ScreenLineString& geometry, const RenderedQueryOptions& options) const { + return impl->queryRenderedFeatures(geometry, options); +} + +std::vector<Feature> Renderer::queryRenderedFeatures(const ScreenCoordinate& point, const RenderedQueryOptions& options) const { + return impl->queryRenderedFeatures({ point }, options); +} + +std::vector<Feature> Renderer::queryRenderedFeatures(const ScreenBox& box, const RenderedQueryOptions& options) const { + return impl->queryRenderedFeatures( + { + box.min, + {box.max.x, box.min.y}, + box.max, + {box.min.x, box.max.y}, + box.min + }, + options + ); +} + +AnnotationIDs Renderer::queryPointAnnotations(const ScreenBox& box) const { + RenderedQueryOptions options; + options.layerIDs = {{ AnnotationManager::PointLayerID }}; + auto features = queryRenderedFeatures(box, options); + std::set<AnnotationID> set; + for (auto &feature : features) { + assert(feature.id); + assert(feature.id->is<uint64_t>()); + assert(feature.id->get<uint64_t>() <= std::numeric_limits<AnnotationID>::max()); + set.insert(static_cast<AnnotationID>(feature.id->get<uint64_t>())); + } + AnnotationIDs ids; + ids.reserve(set.size()); + std::move(set.begin(), set.end(), std::back_inserter(ids)); + return ids; +} + +std::vector<Feature> Renderer::querySourceFeatures(const std::string& sourceID, const SourceQueryOptions& options) const { + return impl->querySourceFeatures(sourceID, options); +} + +void Renderer::dumpDebugLogs() { + impl->dumDebugLogs(); +} + +void Renderer::onLowMemory() { + impl->onLowMemory(); +} + +} // namespace mbgl diff --git a/src/mbgl/renderer/renderer_impl.cpp b/src/mbgl/renderer/renderer_impl.cpp new file mode 100644 index 0000000000..accdbc1332 --- /dev/null +++ b/src/mbgl/renderer/renderer_impl.cpp @@ -0,0 +1,151 @@ +#include <mbgl/renderer/renderer_impl.hpp> +#include <mbgl/actor/scheduler.hpp> +#include <mbgl/storage/file_source.hpp> +#include <mbgl/renderer/render_style.hpp> +#include <mbgl/renderer/painter.hpp> +#include <mbgl/renderer/update_parameters.hpp> +#include <mbgl/map/transform_state.hpp> +#include <mbgl/map/backend_scope.hpp> + +namespace mbgl { + +static RendererObserver& nullObserver() { + static RendererObserver observer; + return observer; +} + +Renderer::Impl::Impl(Backend& backend_, + float pixelRatio_, + FileSource& fileSource_, + Scheduler& scheduler_, + GLContextMode contextMode_, + const optional<std::string> programCacheDir_) + : backend(backend_) + , observer(&nullObserver()) + , contextMode(contextMode_) + , pixelRatio(pixelRatio_), + programCacheDir(programCacheDir_) + , renderStyle(std::make_unique<RenderStyle>(scheduler_, fileSource_)) { + + renderStyle->setObserver(this); +} + +Renderer::Impl::~Impl() { + BackendScope guard { backend, backend.getScopeType()}; + renderStyle.reset(); +}; + +void Renderer::Impl::setObserver(RendererObserver* observer_) { + observer = observer_ ? observer_ : &nullObserver(); +} + +void Renderer::Impl::render(View& view, const UpdateParameters& updateParameters) { + // Don't load/render anyting in still mode until explicitly requested. + if (updateParameters.mode == MapMode::Still && !updateParameters.stillImageRequest) return; + + BackendScope guard { backend, backend.getScopeType() }; + + // Update render style + renderStyle->update(updateParameters); + + // Initialize painter + if (!painter) { + gl::Context& context = backend.getContext(); + painter = std::make_unique<Painter>(context, + pixelRatio, + programCacheDir); + } + + // Update transform state on painter. + painter->state = updateParameters.transformState; + + bool loaded = updateParameters.styleLoaded && renderStyle->isLoaded(); + + if (updateParameters.mode == MapMode::Continuous) { + if (renderState == RenderState::Never) { + observer->onWillStartRenderingMap(); + } + + observer->onWillStartRenderingFrame(); + + FrameData frameData { updateParameters.timePoint, + pixelRatio, + updateParameters.mode, + contextMode, + updateParameters.debugOptions }; + + backend.updateAssumedState(); + + painter->render(*renderStyle, frameData, view); + painter->cleanup(); + + observer->onDidFinishRenderingFrame( + loaded ? RendererObserver::RenderMode::Full : RendererObserver::RenderMode::Partial, + renderStyle->hasTransitions() || painter->needsAnimation() + ); + + if (!loaded) { + renderState = RenderState::Partial; + } else if (renderState != RenderState::Fully) { + renderState = RenderState::Fully; + observer->onDidFinishRenderingMap(); + } + } else if (loaded) { + // We can render the map in still mode + observer->onWillStartRenderingMap(); + observer->onWillStartRenderingFrame(); + + FrameData frameData { updateParameters.timePoint, + pixelRatio, + updateParameters.mode, + contextMode, + updateParameters.debugOptions }; + + backend.updateAssumedState(); + + painter->render(*renderStyle, frameData, view); + + observer->onDidFinishRenderingFrame(RendererObserver::RenderMode::Full, false); + observer->onDidFinishRenderingMap(); + + // Cleanup only after signaling completion + painter->cleanup(); + } +} + +std::vector<Feature> Renderer::Impl::queryRenderedFeatures(const ScreenLineString& geometry, const RenderedQueryOptions& options) const { + if (!painter) return {}; + + return renderStyle->queryRenderedFeatures(geometry, painter->state, options); +} + +std::vector<Feature> Renderer::Impl::querySourceFeatures(const std::string& sourceID, const SourceQueryOptions& options) const { + const RenderSource* source = renderStyle->getRenderSource(sourceID); + if (!source) return {}; + + return source->querySourceFeatures(options); +} + +void Renderer::Impl::onInvalidate() { + observer->onInvalidate(); +}; + +void Renderer::Impl::onResourceError(std::exception_ptr ptr) { + observer->onResourceError(ptr); +} + +void Renderer::Impl::onLowMemory() { + if (painter) { + BackendScope { backend, backend.getScopeType() }; + painter->cleanup(); + } + renderStyle->onLowMemory(); + observer->onInvalidate(); +} + +void Renderer::Impl::dumDebugLogs() { + renderStyle->dumpDebugLogs(); +}; + + +} diff --git a/src/mbgl/renderer/renderer_impl.hpp b/src/mbgl/renderer/renderer_impl.hpp new file mode 100644 index 0000000000..645b33b354 --- /dev/null +++ b/src/mbgl/renderer/renderer_impl.hpp @@ -0,0 +1,65 @@ +#pragma once +#include <mbgl/renderer/renderer.hpp> +#include <mbgl/renderer/renderer_observer.hpp> +#include <mbgl/renderer/render_style_observer.hpp> +#include <mbgl/style/style.hpp> + +#include <mbgl/map/backend.hpp> + +#include <memory> +#include <string> + +namespace mbgl { + +enum class RenderState : uint8_t { + Never, + Partial, + Fully, +}; + +class Painter; +class RenderStyle; +class TransformState; +class View; + + +class Renderer::Impl : public RenderStyleObserver { +public: + Impl(Backend&, float pixelRatio_, FileSource&, Scheduler&, GLContextMode, + const optional<std::string> programCacheDir); + ~Impl() final; + + void setObserver(RendererObserver*); + + void render(View&, const UpdateParameters&); + + std::vector<Feature> queryRenderedFeatures(const ScreenLineString&, const RenderedQueryOptions&) const; + std::vector<Feature> querySourceFeatures(const std::string& sourceID, const SourceQueryOptions&) const; + + void onLowMemory(); + + void dumDebugLogs() ; + + // RenderStyleObserver implementation + void onInvalidate()override; + void onResourceError(std::exception_ptr) override; + +private: + friend class Renderer; + + Backend& backend; + + RendererObserver* observer; + + const GLContextMode contextMode; + const float pixelRatio; + const optional<std::string> programCacheDir; + + RenderState renderState = RenderState::Never; + + std::unique_ptr<RenderStyle> renderStyle; + std::unique_ptr<Painter> painter; + +}; + +} // namespace mbgl diff --git a/src/mbgl/renderer/renderer_observer.hpp b/src/mbgl/renderer/renderer_observer.hpp new file mode 100644 index 0000000000..551b5c803e --- /dev/null +++ b/src/mbgl/renderer/renderer_observer.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include <exception> + +namespace mbgl { + +class RendererObserver { +public: + virtual ~RendererObserver() = default; + + enum class RenderMode : uint32_t { + Partial, + Full + }; + + // Signals that a repaint is required + virtual void onInvalidate() {} + + // Resource failed to download / parse + virtual void onResourceError(std::exception_ptr) {} + + // First frame + virtual void onWillStartRenderingMap() {} + + // Start of frame, initial is the first frame for this map + virtual void onWillStartRenderingFrame() {} + + // End of frame, boolean flags that a repaint is required + virtual void onDidFinishRenderingFrame(RenderMode, bool) {} + + // Final frame + virtual void onDidFinishRenderingMap() {} +}; + +} // namespace mbgl diff --git a/src/mbgl/renderer/tile_pyramid.cpp b/src/mbgl/renderer/tile_pyramid.cpp index 5b1e621743..4abe189826 100644 --- a/src/mbgl/renderer/tile_pyramid.cpp +++ b/src/mbgl/renderer/tile_pyramid.cpp @@ -3,8 +3,8 @@ #include <mbgl/renderer/painter.hpp> #include <mbgl/renderer/render_source.hpp> #include <mbgl/renderer/tile_parameters.hpp> +#include <mbgl/renderer/query.hpp> #include <mbgl/map/transform.hpp> -#include <mbgl/map/query.hpp> #include <mbgl/text/placement_config.hpp> #include <mbgl/math/clamp.hpp> #include <mbgl/util/tile_cover.hpp> diff --git a/src/mbgl/renderer/update_parameters.hpp b/src/mbgl/renderer/update_parameters.hpp index 04b59699b3..9fe8afcd89 100644 --- a/src/mbgl/renderer/update_parameters.hpp +++ b/src/mbgl/renderer/update_parameters.hpp @@ -2,11 +2,14 @@ #include <mbgl/map/mode.hpp> #include <mbgl/map/transform_state.hpp> -#include <mbgl/util/chrono.hpp> #include <mbgl/style/light.hpp> #include <mbgl/style/image.hpp> #include <mbgl/style/source.hpp> #include <mbgl/style/layer.hpp> +#include <mbgl/util/chrono.hpp> +#include <mbgl/util/immutable.hpp> + +#include <vector> namespace mbgl { @@ -16,7 +19,8 @@ class AnnotationManager; class UpdateParameters { public: - UpdateParameters(const MapMode mode_, + UpdateParameters(const bool styleLoaded_, + const MapMode mode_, const float pixelRatio_, const MapDebugOptions debugOptions_, const TimePoint timePoint_, @@ -31,8 +35,10 @@ public: Scheduler& scheduler_, FileSource& fileSource_, AnnotationManager& annotationManager_, - const uint8_t prefetchZoomDelta_) - : mode(mode_), + const uint8_t prefetchZoomDelta_, + const bool stillImageRequest_) + : styleLoaded(styleLoaded_), + mode(mode_), pixelRatio(pixelRatio_), debugOptions(debugOptions_), timePoint(std::move(timePoint_)), @@ -47,8 +53,10 @@ public: scheduler(scheduler_), fileSource(fileSource_), annotationManager(annotationManager_), - prefetchZoomDelta(prefetchZoomDelta_) {} + prefetchZoomDelta(prefetchZoomDelta_), + stillImageRequest(stillImageRequest_) {} + const bool styleLoaded; const MapMode mode; const float pixelRatio; const MapDebugOptions debugOptions; @@ -68,6 +76,9 @@ public: AnnotationManager& annotationManager; const uint8_t prefetchZoomDelta; + + // For still image requests, render requested + const bool stillImageRequest; }; } // namespace mbgl diff --git a/src/mbgl/tile/geojson_tile.cpp b/src/mbgl/tile/geojson_tile.cpp index e9865e8272..d5e2e14e54 100644 --- a/src/mbgl/tile/geojson_tile.cpp +++ b/src/mbgl/tile/geojson_tile.cpp @@ -1,6 +1,6 @@ #include <mbgl/tile/geojson_tile.hpp> #include <mbgl/tile/geometry_tile_data.hpp> -#include <mbgl/map/query.hpp> +#include <mbgl/renderer/query.hpp> #include <mbgl/renderer/tile_parameters.hpp> #include <mapbox/geojsonvt.hpp> diff --git a/src/mbgl/tile/geometry_tile.cpp b/src/mbgl/tile/geometry_tile.cpp index 4f1bc9e759..33911df9ed 100644 --- a/src/mbgl/tile/geometry_tile.cpp +++ b/src/mbgl/tile/geometry_tile.cpp @@ -10,13 +10,13 @@ #include <mbgl/renderer/layers/render_custom_layer.hpp> #include <mbgl/renderer/layers/render_symbol_layer.hpp> #include <mbgl/renderer/buckets/symbol_bucket.hpp> +#include <mbgl/renderer/query.hpp> #include <mbgl/text/glyph_atlas.hpp> #include <mbgl/renderer/image_atlas.hpp> #include <mbgl/storage/file_source.hpp> #include <mbgl/geometry/feature_index.hpp> #include <mbgl/text/collision_tile.hpp> #include <mbgl/map/transform_state.hpp> -#include <mbgl/map/query.hpp> #include <mbgl/util/run_loop.hpp> #include <mbgl/style/filter_evaluator.hpp> #include <mbgl/util/chrono.hpp> diff --git a/src/mbgl/tile/tile.cpp b/src/mbgl/tile/tile.cpp index 5080d42933..35fc31dae1 100644 --- a/src/mbgl/tile/tile.cpp +++ b/src/mbgl/tile/tile.cpp @@ -1,9 +1,9 @@ #include <mbgl/tile/tile.hpp> #include <mbgl/tile/tile_observer.hpp> #include <mbgl/renderer/buckets/debug_bucket.hpp> +#include <mbgl/renderer/query.hpp> #include <mbgl/util/string.hpp> #include <mbgl/util/logging.hpp> -#include <mbgl/map/query.hpp> namespace mbgl { diff --git a/test/api/annotations.test.cpp b/test/api/annotations.test.cpp index 42fbcc0a8b..76e66c2fa3 100644 --- a/test/api/annotations.test.cpp +++ b/test/api/annotations.test.cpp @@ -12,6 +12,8 @@ #include <mbgl/util/io.hpp> #include <mbgl/util/run_loop.hpp> #include <mbgl/util/color.hpp> +#include <mbgl/renderer/renderer.hpp> +#include <mbgl/test/stub_renderer_frontend.hpp> using namespace mbgl; @@ -33,7 +35,10 @@ public: OffscreenView view { backend.getContext() }; StubFileSource fileSource; ThreadPool threadPool { 4 }; - Map map { backend, MapObserver::nullObserver(), view.getSize(), 1, fileSource, threadPool, MapMode::Still }; + float pixelRatio { 1 }; + StubRendererFrontend rendererFrontend { std::make_unique<Renderer>(backend, pixelRatio, fileSource, threadPool), view }; + Map map { rendererFrontend, MapObserver::nullObserver(), view.getSize(), pixelRatio, fileSource, + threadPool, MapMode::Still }; void checkRendering(const char * name) { test::checkImage(std::string("test/fixtures/annotations/") + name, @@ -346,12 +351,12 @@ TEST(Annotations, QueryRenderedFeatures) { test::render(test.map, test.view); - auto features = test.map.queryRenderedFeatures(test.map.pixelForLatLng({ 0, 0 })); + auto features = test.rendererFrontend.getRenderer()->queryRenderedFeatures(test.map.pixelForLatLng({ 0, 0 })); EXPECT_EQ(features.size(), 1u); EXPECT_TRUE(!!features[0].id); EXPECT_EQ(*features[0].id, uint64_t(0)); - auto features2 = test.map.queryRenderedFeatures(test.map.pixelForLatLng({ 50, 0 })); + auto features2 = test.rendererFrontend.getRenderer()->queryRenderedFeatures(test.map.pixelForLatLng({ 50, 0 })); EXPECT_EQ(features2.size(), 1u); EXPECT_TRUE(!!features2[0].id); EXPECT_EQ(*features2[0].id, uint64_t(1)); @@ -377,7 +382,7 @@ TEST(Annotations, QueryFractionalZoomLevels) { for (uint16_t zoomSteps = 10; zoomSteps <= 20; ++zoomSteps) { test.map.setZoom(zoomSteps / 10.0); test::render(test.map, test.view); - auto features = test.map.queryRenderedFeatures(box); + auto features = test.rendererFrontend.getRenderer()->queryRenderedFeatures(box); // Filter out repeated features. // See 'edge-cases/null-island' query-test for reference. @@ -410,7 +415,7 @@ TEST(Annotations, VisibleFeatures) { test.map.setBearing(45); test::render(test.map, test.view); - auto features = test.map.queryRenderedFeatures(box, {}); + auto features = test.rendererFrontend.getRenderer()->queryRenderedFeatures(box, {}); auto sortID = [](const Feature& lhs, const Feature& rhs) { return lhs.id < rhs.id; }; auto sameID = [](const Feature& lhs, const Feature& rhs) { return lhs.id == rhs.id; }; std::sort(features.begin(), features.end(), sortID); @@ -420,7 +425,7 @@ TEST(Annotations, VisibleFeatures) { test.map.setBearing(0); test.map.setZoom(4); test::render(test.map, test.view); - features = test.map.queryRenderedFeatures(box); + features = test.rendererFrontend.getRenderer()->queryRenderedFeatures(box); std::sort(features.begin(), features.end(), sortID); features.erase(std::unique(features.begin(), features.end(), sameID), features.end()); EXPECT_EQ(features.size(), ids.size()); diff --git a/test/api/api_misuse.test.cpp b/test/api/api_misuse.test.cpp index e5498576a8..742137d73d 100644 --- a/test/api/api_misuse.test.cpp +++ b/test/api/api_misuse.test.cpp @@ -6,7 +6,9 @@ #include <mbgl/map/backend_scope.hpp> #include <mbgl/gl/headless_backend.hpp> #include <mbgl/gl/offscreen_view.hpp> +#include <mbgl/test/stub_renderer_frontend.hpp> #include <mbgl/storage/online_file_source.hpp> +#include <mbgl/renderer/renderer.hpp> #include <mbgl/util/default_thread_pool.hpp> #include <mbgl/util/exception.hpp> #include <mbgl/util/run_loop.hpp> @@ -27,10 +29,13 @@ TEST(API, RenderWithoutCallback) { OffscreenView view { backend.getContext(), { 128, 512 } }; StubFileSource fileSource; ThreadPool threadPool(4); + float pixelRatio { 1 }; + StubRendererFrontend rendererFrontend { + std::make_unique<Renderer>(backend, pixelRatio, fileSource, threadPool), view }; - std::unique_ptr<Map> map = - std::make_unique<Map>(backend, MapObserver::nullObserver(), view.getSize(), 1, fileSource, threadPool, MapMode::Still); - map->renderStill(view, nullptr); + auto map = std::make_unique<Map>(rendererFrontend, MapObserver::nullObserver(), view.getSize(), + pixelRatio, fileSource, threadPool, MapMode::Still); + map->renderStill(nullptr); // Force Map thread to join. map.reset(); diff --git a/test/api/custom_layer.test.cpp b/test/api/custom_layer.test.cpp index b5d94f2786..b3ef0719ce 100644 --- a/test/api/custom_layer.test.cpp +++ b/test/api/custom_layer.test.cpp @@ -7,9 +7,11 @@ #include <mbgl/gl/offscreen_view.hpp> #include <mbgl/util/default_thread_pool.hpp> #include <mbgl/storage/default_file_source.hpp> +#include <mbgl/renderer/renderer.hpp> #include <mbgl/style/style.hpp> #include <mbgl/style/layers/custom_layer.hpp> #include <mbgl/style/layers/fill_layer.hpp> +#include <mbgl/test/stub_renderer_frontend.hpp> #include <mbgl/util/io.hpp> #include <mbgl/util/mat4.hpp> #include <mbgl/util/run_loop.hpp> @@ -92,8 +94,10 @@ TEST(CustomLayer, Basic) { OffscreenView view { backend.getContext() }; DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets"); ThreadPool threadPool(4); - - Map map(backend, MapObserver::nullObserver(), view.getSize(), 1, fileSource, threadPool, MapMode::Still); + float pixelRatio { 1 }; + StubRendererFrontend rendererFrontend { std::make_unique<Renderer>(backend, pixelRatio, fileSource, threadPool), view }; + Map map(rendererFrontend, MapObserver::nullObserver(), view.getSize(), pixelRatio, fileSource, + threadPool, MapMode::Still); map.getStyle().loadJSON(util::read_file("test/fixtures/api/water.json")); map.setLatLngZoom({ 37.8, -122.5 }, 10); map.getStyle().addLayer(std::make_unique<CustomLayer>( @@ -114,4 +118,5 @@ TEST(CustomLayer, Basic) { map.getStyle().addLayer(std::move(layer)); test::checkImage("test/fixtures/custom_layer/basic", test::render(map, view), 0.0006, 0.1); + } diff --git a/test/api/query.test.cpp b/test/api/query.test.cpp index 0b02e47219..efb31f89c6 100644 --- a/test/api/query.test.cpp +++ b/test/api/query.test.cpp @@ -11,6 +11,8 @@ #include <mbgl/style/style.hpp> #include <mbgl/style/image.hpp> #include <mbgl/style/source.hpp> +#include <mbgl/renderer/renderer.hpp> +#include <mbgl/test/stub_renderer_frontend.hpp> using namespace mbgl; using namespace mbgl::style; @@ -33,7 +35,11 @@ public: OffscreenView view { backend.getContext() }; StubFileSource fileSource; ThreadPool threadPool { 4 }; - Map map { backend, MapObserver::nullObserver(), view.getSize(), 1, fileSource, threadPool, MapMode::Still }; + float pixelRatio { 1 }; + StubRendererFrontend rendererFrontend { + std::make_unique<Renderer>(backend, pixelRatio, fileSource, threadPool), view }; + Map map { rendererFrontend, MapObserver::nullObserver(), view.getSize(), pixelRatio, fileSource, + threadPool, MapMode::Still }; }; } // end namespace @@ -41,10 +47,10 @@ public: TEST(Query, QueryRenderedFeatures) { QueryTest test; - auto features1 = test.map.queryRenderedFeatures(test.map.pixelForLatLng({ 0, 0 })); + auto features1 = test.rendererFrontend.getRenderer()->queryRenderedFeatures(test.map.pixelForLatLng({ 0, 0 })); EXPECT_EQ(features1.size(), 4u); - auto features2 = test.map.queryRenderedFeatures(test.map.pixelForLatLng({ 9, 9 })); + auto features2 = test.rendererFrontend.getRenderer()->queryRenderedFeatures(test.map.pixelForLatLng({ 9, 9 })); EXPECT_EQ(features2.size(), 0u); } @@ -53,16 +59,16 @@ TEST(Query, QueryRenderedFeaturesFilterLayer) { auto zz = test.map.pixelForLatLng({ 0, 0 }); - auto features1 = test.map.queryRenderedFeatures(zz, {{{ "layer1"}}, {}}); + auto features1 = test.rendererFrontend.getRenderer()->queryRenderedFeatures(zz, {{{ "layer1"}}, {}}); EXPECT_EQ(features1.size(), 1u); - auto features2 = test.map.queryRenderedFeatures(zz, {{{ "layer1", "layer2" }}, {}}); + auto features2 = test.rendererFrontend.getRenderer()->queryRenderedFeatures(zz, {{{ "layer1", "layer2" }}, {}}); EXPECT_EQ(features2.size(), 2u); - auto features3 = test.map.queryRenderedFeatures(zz, {{{ "foobar" }}, {}}); + auto features3 = test.rendererFrontend.getRenderer()->queryRenderedFeatures(zz, {{{ "foobar" }}, {}}); EXPECT_EQ(features3.size(), 0u); - auto features4 = test.map.queryRenderedFeatures(zz, {{{ "foobar", "layer3" }}, {}}); + auto features4 = test.rendererFrontend.getRenderer()->queryRenderedFeatures(zz, {{{ "foobar", "layer3" }}, {}}); EXPECT_EQ(features4.size(), 1u); } @@ -72,22 +78,22 @@ TEST(Query, QueryRenderedFeaturesFilter) { auto zz = test.map.pixelForLatLng({ 0, 0 }); const EqualsFilter eqFilter = { "key1", std::string("value1") }; - auto features1 = test.map.queryRenderedFeatures(zz, {{}, { eqFilter }}); + auto features1 = test.rendererFrontend.getRenderer()->queryRenderedFeatures(zz, {{}, { eqFilter }}); EXPECT_EQ(features1.size(), 1u); const IdentifierNotEqualsFilter idNotEqFilter = { std::string("feature1") }; - auto features2 = test.map.queryRenderedFeatures(zz, {{{ "layer4" }}, { idNotEqFilter }}); + auto features2 = test.rendererFrontend.getRenderer()->queryRenderedFeatures(zz, {{{ "layer4" }}, { idNotEqFilter }}); EXPECT_EQ(features2.size(), 0u); const GreaterThanFilter gtFilter = { "key2", 1.0 }; - auto features3 = test.map.queryRenderedFeatures(zz, {{ }, { gtFilter }}); + auto features3 = test.rendererFrontend.getRenderer()->queryRenderedFeatures(zz, {{ }, { gtFilter }}); EXPECT_EQ(features3.size(), 1u); } TEST(Query, QuerySourceFeatures) { QueryTest test; - auto features1 = test.map.querySourceFeatures("source3"); + auto features1 = test.rendererFrontend.getRenderer()->querySourceFeatures("source3"); EXPECT_EQ(features1.size(), 1u); } @@ -95,15 +101,15 @@ TEST(Query, QuerySourceFeaturesOptionValidation) { QueryTest test; // GeoJSONSource, doesn't require a layer id - auto features = test.map.querySourceFeatures("source3"); + auto features = test.rendererFrontend.getRenderer()->querySourceFeatures("source3"); ASSERT_EQ(features.size(), 1u); // VectorSource, requires a layer id - features = test.map.querySourceFeatures("source5"); + features = test.rendererFrontend.getRenderer()->querySourceFeatures("source5"); ASSERT_EQ(features.size(), 0u); // RasterSource, not supported - features = test.map.querySourceFeatures("source6"); + features = test.rendererFrontend.getRenderer()->querySourceFeatures("source6"); ASSERT_EQ(features.size(), 0u); } @@ -111,15 +117,15 @@ TEST(Query, QuerySourceFeaturesFilter) { QueryTest test; const EqualsFilter eqFilter = { "key1", std::string("value1") }; - auto features1 = test.map.querySourceFeatures("source4", {{}, { eqFilter }}); + auto features1 = test.rendererFrontend.getRenderer()->querySourceFeatures("source4", {{}, { eqFilter }}); EXPECT_EQ(features1.size(), 1u); const IdentifierNotEqualsFilter idNotEqFilter = { std::string("feature1") }; - auto features2 = test.map.querySourceFeatures("source4", {{}, { idNotEqFilter }}); + auto features2 = test.rendererFrontend.getRenderer()->querySourceFeatures("source4", {{}, { idNotEqFilter }}); EXPECT_EQ(features2.size(), 0u); const GreaterThanFilter gtFilter = { "key2", 1.0 }; - auto features3 = test.map.querySourceFeatures("source4", {{}, { gtFilter }}); + auto features3 = test.rendererFrontend.getRenderer()->querySourceFeatures("source4", {{}, { gtFilter }}); EXPECT_EQ(features3.size(), 1u); } diff --git a/test/api/render_missing.test.cpp b/test/api/render_missing.test.cpp index 9cba1dcac7..bce6df6305 100644 --- a/test/api/render_missing.test.cpp +++ b/test/api/render_missing.test.cpp @@ -7,12 +7,15 @@ #include <mbgl/gl/offscreen_view.hpp> #include <mbgl/util/default_thread_pool.hpp> #include <mbgl/storage/default_file_source.hpp> +#include <mbgl/renderer/renderer.hpp> #include <mbgl/util/image.hpp> #include <mbgl/util/io.hpp> #include <mbgl/util/run_loop.hpp> #include <mbgl/style/style.hpp> +#include <mbgl/test/stub_renderer_frontend.hpp> #include <future> +#include <memory> #if TEST_HAS_SERVER #define TEST_REQUIRES_SERVER(name) name @@ -30,19 +33,22 @@ TEST(API, TEST_REQUIRES_SERVER(RenderMissingTile)) { HeadlessBackend backend { test::sharedDisplay() }; BackendScope scope { backend }; OffscreenView view { backend.getContext(), { 256, 512 } }; + float pixelRatio { 1 }; DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets"); ThreadPool threadPool(4); + StubRendererFrontend rendererFrontend { std::make_unique<Renderer>(backend, pixelRatio, fileSource, threadPool), view }; Log::setObserver(std::make_unique<FixtureLogObserver>()); - Map map(backend, MapObserver::nullObserver(), view.getSize(), 1, fileSource, threadPool, MapMode::Still); + Map map { rendererFrontend, MapObserver::nullObserver(), view.getSize(), pixelRatio, fileSource, + threadPool, MapMode::Still }; std::string message; // This host does not respond (== connection error). // Are you seeing this test fail? Make sure you don't have a server running on port 3001! map.getStyle().loadJSON(style); - map.renderStill(view, [&](std::exception_ptr err) { + map.renderStill([&](std::exception_ptr err) { ASSERT_TRUE(err.operator bool()); try { std::rethrow_exception(err); diff --git a/test/api/repeated_render.test.cpp b/test/api/repeated_render.test.cpp index 033b741ab6..d52765e2a5 100644 --- a/test/api/repeated_render.test.cpp +++ b/test/api/repeated_render.test.cpp @@ -7,10 +7,12 @@ #include <mbgl/gl/offscreen_view.hpp> #include <mbgl/util/default_thread_pool.hpp> #include <mbgl/storage/default_file_source.hpp> +#include <mbgl/renderer/renderer.hpp> #include <mbgl/util/image.hpp> #include <mbgl/util/io.hpp> #include <mbgl/util/run_loop.hpp> #include <mbgl/style/style.hpp> +#include <mbgl/test/stub_renderer_frontend.hpp> #include <mbgl/style/layers/line_layer.hpp> #include <mbgl/style/sources/geojson_source.hpp> @@ -28,17 +30,21 @@ TEST(API, RepeatedRender) { HeadlessBackend backend { test::sharedDisplay() }; BackendScope scope { backend }; OffscreenView view { backend.getContext(), { 512, 512 } }; + float pixelRatio { 1 }; DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets"); ThreadPool threadPool(4); + StubRendererFrontend rendererFrontend { + std::make_unique<Renderer>(backend, pixelRatio, fileSource, threadPool), view }; Log::setObserver(std::make_unique<FixtureLogObserver>()); - Map map(backend, MapObserver::nullObserver(), view.getSize(), 1, fileSource, threadPool, MapMode::Still); + Map map(rendererFrontend, MapObserver::nullObserver(), view.getSize(), pixelRatio, fileSource, + threadPool, MapMode::Still); { map.getStyle().loadJSON(style); PremultipliedImage result; - map.renderStill(view, [&](std::exception_ptr) { + map.renderStill([&](std::exception_ptr) { result = view.readStillImage(); }); @@ -54,7 +60,7 @@ TEST(API, RepeatedRender) { { map.getStyle().loadJSON(style); PremultipliedImage result; - map.renderStill(view, [&](std::exception_ptr) { + map.renderStill([&](std::exception_ptr) { result = view.readStillImage(); }); @@ -86,7 +92,10 @@ TEST(API, ZoomHistory) { Log::setObserver(std::make_unique<FixtureLogObserver>()); - Map map(backend, MapObserver::nullObserver(), view.getSize(), 1, fileSource, threadPool, MapMode::Still); + float pixelRatio { 1 }; + StubRendererFrontend rendererFrontend { + std::make_unique<Renderer>(backend, pixelRatio, fileSource, threadPool), view }; + Map map(rendererFrontend, MapObserver::nullObserver(), view.getSize(), pixelRatio, fileSource, threadPool, MapMode::Still); map.getStyle().loadJSON(style); auto geojson = mapbox::geojson::parse(R"t({ "type": "FeatureCollection", "features": [{ "type": "Feature", "properties": {}, "geometry": { "type": "LineString", "coordinates": [ [ -150, -75 ], [ 150, 75 ] ] } } ] })t"); @@ -101,7 +110,7 @@ TEST(API, ZoomHistory) { { PremultipliedImage result; - map.renderStill(view, [&](std::exception_ptr) { + map.renderStill([&](std::exception_ptr) { result = view.readStillImage(); }); @@ -116,7 +125,7 @@ TEST(API, ZoomHistory) { map.setZoom(1.0); PremultipliedImage result; - map.renderStill(view, [&](std::exception_ptr) { + map.renderStill([&](std::exception_ptr) { result = view.readStillImage(); }); diff --git a/test/map/map.test.cpp b/test/map/map.test.cpp index 206371613d..ef1d7c3d6f 100644 --- a/test/map/map.test.cpp +++ b/test/map/map.test.cpp @@ -12,6 +12,7 @@ #include <mbgl/storage/network_status.hpp> #include <mbgl/storage/default_file_source.hpp> #include <mbgl/storage/online_file_source.hpp> +#include <mbgl/renderer/renderer.hpp> #include <mbgl/util/image.hpp> #include <mbgl/util/io.hpp> #include <mbgl/util/run_loop.hpp> @@ -19,26 +20,13 @@ #include <mbgl/style/style.hpp> #include <mbgl/style/image.hpp> #include <mbgl/style/layers/background_layer.hpp> +#include <mbgl/test/stub_renderer_frontend.hpp> #include <mbgl/util/color.hpp> using namespace mbgl; using namespace mbgl::style; using namespace std::literals::string_literals; -class MockBackend : public HeadlessBackend { -public: - MockBackend() - : HeadlessBackend(test::sharedDisplay() ) { - } - - std::function<void()> callback; - void invalidate() override { - if (callback) { - callback(); - } - } -}; - class StubMapObserver : public MapObserver { public: void onWillStartLoadingMap() final { @@ -71,66 +59,83 @@ public: std::function<void()> didFinishLoadingStyleCallback; }; -struct MapTest { +template <class FileSource = StubFileSource> +class MapTest { +public: util::RunLoop runLoop; - MockBackend backend; - StubMapObserver observer; + HeadlessBackend backend; BackendScope scope { backend }; OffscreenView view { backend.getContext() }; - StubFileSource fileSource; + FileSource fileSource; ThreadPool threadPool { 4 }; + StubMapObserver observer; + StubRendererFrontend rendererFrontend; + Map map; + + MapTest(float pixelRatio = 1, MapMode mode = MapMode::Still) + : rendererFrontend( + std::make_unique<Renderer>(backend, pixelRatio, fileSource, threadPool), view) + , map(rendererFrontend, observer, view.getSize(), pixelRatio, fileSource, threadPool, mode) { + } + + template <typename T = FileSource> + MapTest(const std::string& cachePath, const std::string& assetRoot, + float pixelRatio = 1, MapMode mode = MapMode::Still, + typename std::enable_if<std::is_same<T, DefaultFileSource>::value>::type* = 0) + : fileSource { cachePath, assetRoot } + , rendererFrontend( + std::make_unique<Renderer>(backend, pixelRatio, fileSource, threadPool), + view) + , map(rendererFrontend, observer, view.getSize(), pixelRatio, fileSource, threadPool, mode) { + } }; TEST(Map, LatLngBehavior) { - MapTest test; - Map map(test.backend, test.observer, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still); + MapTest<> test; - map.setLatLngZoom({ 1, 1 }, 0); - auto latLng1 = map.getLatLng(); + test.map.setLatLngZoom({ 1, 1 }, 0); + auto latLng1 = test.map.getLatLng(); - map.setLatLng({ 1, 1 }); - auto latLng2 = map.getLatLng(); + test.map.setLatLng({ 1, 1 }); + auto latLng2 = test.map.getLatLng(); ASSERT_DOUBLE_EQ(latLng1.latitude(), latLng2.latitude()); ASSERT_DOUBLE_EQ(latLng1.longitude(), latLng2.longitude()); } TEST(Map, LatLngBoundsToCamera) { - MapTest test; - Map map(test.backend, test.observer, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still); + MapTest<> test; - map.setLatLngZoom({ 40.712730, -74.005953 }, 16.0); + test.map.setLatLngZoom({ 40.712730, -74.005953 }, 16.0); LatLngBounds bounds = LatLngBounds::hull({15.68169,73.499857}, {53.560711, 134.77281}); - CameraOptions virtualCamera = map.cameraForLatLngBounds(bounds, {}); + CameraOptions virtualCamera = test.map.cameraForLatLngBounds(bounds, {}); ASSERT_TRUE(bounds.contains(*virtualCamera.center)); } TEST(Map, CameraToLatLngBounds) { - MapTest test; - Map map(test.backend, test.observer, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still); + MapTest<> test; - map.setLatLngZoom({ 45, 90 }, 16); + test.map.setLatLngZoom({ 45, 90 }, 16); LatLngBounds bounds = LatLngBounds::hull( - map.latLngForPixel({}), - map.latLngForPixel({ double(map.getSize().width), double(map.getSize().height) })); + test.map.latLngForPixel({}), + test.map.latLngForPixel({ double(test.map.getSize().width), double(test.map.getSize().height) })); - CameraOptions camera = map.getCameraOptions({}); + CameraOptions camera = test.map.getCameraOptions({}); - ASSERT_EQ(bounds, map.latLngBoundsForCamera(camera)); + ASSERT_EQ(bounds, test.map.latLngBoundsForCamera(camera)); // Map::cameraForLatLngBounds only sets zoom and center. - CameraOptions virtualCamera = map.cameraForLatLngBounds(bounds, {}); + CameraOptions virtualCamera = test.map.cameraForLatLngBounds(bounds, {}); ASSERT_NEAR(*camera.zoom, *virtualCamera.zoom, 1e-7); ASSERT_NEAR(camera.center->latitude(), virtualCamera.center->latitude(), 1e-7); ASSERT_NEAR(camera.center->longitude(), virtualCamera.center->longitude(), 1e-7); } TEST(Map, Offline) { - MapTest test; - DefaultFileSource fileSource(":memory:", "."); + MapTest<DefaultFileSource> test {":memory:", "."}; auto expiredItem = [] (const std::string& path) { Response response; @@ -140,19 +145,18 @@ TEST(Map, Offline) { }; const std::string prefix = "http://127.0.0.1:3000/"; - fileSource.put(Resource::style(prefix + "style.json"), expiredItem("style.json")); - fileSource.put(Resource::source(prefix + "streets.json"), expiredItem("streets.json")); - fileSource.put(Resource::spriteJSON(prefix + "sprite", 1.0), expiredItem("sprite.json")); - fileSource.put(Resource::spriteImage(prefix + "sprite", 1.0), expiredItem("sprite.png")); - fileSource.put(Resource::tile(prefix + "{z}-{x}-{y}.vector.pbf", 1.0, 0, 0, 0, Tileset::Scheme::XYZ), expiredItem("0-0-0.vector.pbf")); - fileSource.put(Resource::glyphs(prefix + "{fontstack}/{range}.pbf", {{"Helvetica"}}, {0, 255}), expiredItem("glyph.pbf")); + test.fileSource.put(Resource::style(prefix + "style.json"), expiredItem("style.json")); + test.fileSource.put(Resource::source(prefix + "streets.json"), expiredItem("streets.json")); + test.fileSource.put(Resource::spriteJSON(prefix + "sprite", 1.0), expiredItem("sprite.json")); + test.fileSource.put(Resource::spriteImage(prefix + "sprite", 1.0), expiredItem("sprite.png")); + test.fileSource.put(Resource::tile(prefix + "{z}-{x}-{y}.vector.pbf", 1.0, 0, 0, 0, Tileset::Scheme::XYZ), expiredItem("0-0-0.vector.pbf")); + test.fileSource.put(Resource::glyphs(prefix + "{fontstack}/{range}.pbf", {{"Helvetica"}}, {0, 255}), expiredItem("glyph.pbf")); NetworkStatus::Set(NetworkStatus::Status::Offline); - Map map(test.backend, test.observer, test.view.getSize(), 1, fileSource, test.threadPool, MapMode::Still); - map.getStyle().loadURL(prefix + "style.json"); + test.map.getStyle().loadURL(prefix + "style.json"); test::checkImage("test/fixtures/map/offline", - test::render(map, test.view), + test::render(test.map, test.view), 0.0015, 0.1); @@ -160,19 +164,16 @@ TEST(Map, Offline) { } TEST(Map, SetStyleInvalidJSON) { - MapTest test; - Log::setObserver(std::make_unique<FixtureLogObserver>()); bool fail = false; - test.observer.didFailLoadingMapCallback = [&]() { - fail = true; - }; { - Map map(test.backend, test.observer, test.view.getSize(), 1, test.fileSource, test.threadPool, - MapMode::Still); - map.getStyle().loadJSON("invalid"); + MapTest<> test; + test.observer.didFailLoadingMapCallback = [&]() { + fail = true; + }; + test.map.getStyle().loadJSON("invalid"); } EXPECT_TRUE(fail); @@ -186,7 +187,7 @@ TEST(Map, SetStyleInvalidJSON) { } TEST(Map, SetStyleInvalidURL) { - MapTest test; + MapTest<> test; test.fileSource.styleResponse = [] (const Resource&) { Response response; @@ -200,36 +201,32 @@ TEST(Map, SetStyleInvalidURL) { test.runLoop.stop(); }; - Map map(test.backend, test.observer, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still); - map.getStyle().loadURL("mapbox://bar"); + test.map.getStyle().loadURL("mapbox://bar"); test.runLoop.run(); } TEST(Map, DoubleStyleLoad) { - MapTest test; + MapTest<> test; - Map map(test.backend, test.observer, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still); - map.getStyle().loadJSON(""); - map.getStyle().loadJSON(""); + test.map.getStyle().loadJSON(""); + test.map.getStyle().loadJSON(""); } TEST(Map, StyleFresh) { // The map should not revalidate fresh styles. - MapTest test; - FakeFileSource fileSource; + MapTest<FakeFileSource> test; - Map map(test.backend, test.observer, test.view.getSize(), 1, fileSource, test.threadPool, MapMode::Still); - map.getStyle().loadURL("mapbox://styles/test"); - EXPECT_EQ(1u, fileSource.requests.size()); + test.map.getStyle().loadURL("mapbox://styles/test"); + EXPECT_EQ(1u, test.fileSource.requests.size()); Response response; response.data = std::make_shared<std::string>(util::read_file("test/fixtures/api/empty.json")); response.expires = Timestamp::max(); - fileSource.respond(Resource::Style, response); - EXPECT_EQ(0u, fileSource.requests.size()); + test.fileSource.respond(Resource::Style, response); + EXPECT_EQ(0u, test.fileSource.requests.size()); } TEST(Map, StyleExpired) { @@ -237,26 +234,24 @@ TEST(Map, StyleExpired) { using namespace std::chrono_literals; - MapTest test; - FakeFileSource fileSource; + MapTest<FakeFileSource> test; - Map map(test.backend, test.observer, test.view.getSize(), 1, fileSource, test.threadPool, MapMode::Still); - map.getStyle().loadURL("mapbox://styles/test"); - EXPECT_EQ(1u, fileSource.requests.size()); + test.map.getStyle().loadURL("mapbox://styles/test"); + EXPECT_EQ(1u, test.fileSource.requests.size()); Response response; response.data = std::make_shared<std::string>(util::read_file("test/fixtures/api/empty.json")); response.expires = util::now() - 1h; - fileSource.respond(Resource::Style, response); - EXPECT_EQ(1u, fileSource.requests.size()); + test.fileSource.respond(Resource::Style, response); + EXPECT_EQ(1u, test.fileSource.requests.size()); - map.getStyle().addLayer(std::make_unique<style::BackgroundLayer>("bg")); - EXPECT_EQ(1u, fileSource.requests.size()); + test.map.getStyle().addLayer(std::make_unique<style::BackgroundLayer>("bg")); + EXPECT_EQ(1u, test.fileSource.requests.size()); - fileSource.respond(Resource::Style, response); - EXPECT_EQ(0u, fileSource.requests.size()); - EXPECT_NE(nullptr, map.getStyle().getLayer("bg")); + test.fileSource.respond(Resource::Style, response); + EXPECT_EQ(0u, test.fileSource.requests.size()); + EXPECT_NE(nullptr, test.map.getStyle().getLayer("bg")); } TEST(Map, StyleExpiredWithAnnotations) { @@ -264,25 +259,23 @@ TEST(Map, StyleExpiredWithAnnotations) { using namespace std::chrono_literals; - MapTest test; - FakeFileSource fileSource; + MapTest<FakeFileSource> test; - Map map(test.backend, test.observer, test.view.getSize(), 1, fileSource, test.threadPool, MapMode::Still); - map.getStyle().loadURL("mapbox://styles/test"); - EXPECT_EQ(1u, fileSource.requests.size()); + test.map.getStyle().loadURL("mapbox://styles/test"); + EXPECT_EQ(1u, test.fileSource.requests.size()); Response response; response.data = std::make_shared<std::string>(util::read_file("test/fixtures/api/empty.json")); response.expires = util::now() - 1h; - fileSource.respond(Resource::Style, response); - EXPECT_EQ(1u, fileSource.requests.size()); + test.fileSource.respond(Resource::Style, response); + EXPECT_EQ(1u, test.fileSource.requests.size()); - map.addAnnotation(LineAnnotation { LineString<double> {{ { 0, 0 }, { 10, 10 } }} }); - EXPECT_EQ(1u, fileSource.requests.size()); + test.map.addAnnotation(LineAnnotation { LineString<double> {{ { 0, 0 }, { 10, 10 } }} }); + EXPECT_EQ(1u, test.fileSource.requests.size()); - fileSource.respond(Resource::Style, response); - EXPECT_EQ(1u, fileSource.requests.size()); + test.fileSource.respond(Resource::Style, response); + EXPECT_EQ(1u, test.fileSource.requests.size()); } TEST(Map, StyleExpiredWithRender) { @@ -290,98 +283,85 @@ TEST(Map, StyleExpiredWithRender) { using namespace std::chrono_literals; - MapTest test; - FakeFileSource fileSource; + MapTest<FakeFileSource> test; - Map map(test.backend, test.observer, test.view.getSize(), 1, fileSource, test.threadPool, MapMode::Still); - map.getStyle().loadURL("mapbox://styles/test"); - EXPECT_EQ(1u, fileSource.requests.size()); + test.map.getStyle().loadURL("mapbox://styles/test"); + EXPECT_EQ(1u, test.fileSource.requests.size()); Response response; response.data = std::make_shared<std::string>(util::read_file("test/fixtures/api/empty.json")); response.expires = util::now() - 1h; - fileSource.respond(Resource::Style, response); - EXPECT_EQ(1u, fileSource.requests.size()); + test.fileSource.respond(Resource::Style, response); + EXPECT_EQ(1u, test.fileSource.requests.size()); - map.render(test.view); - EXPECT_EQ(1u, fileSource.requests.size()); + test::render(test.map, test.view); + EXPECT_EQ(1u, test.fileSource.requests.size()); - fileSource.respond(Resource::Style, response); - EXPECT_EQ(1u, fileSource.requests.size()); + test.fileSource.respond(Resource::Style, response); + EXPECT_EQ(1u, test.fileSource.requests.size()); } TEST(Map, StyleEarlyMutation) { // An early mutation should not prevent the initial style load. - MapTest test; - FakeFileSource fileSource; + MapTest<FakeFileSource> test; - Map map(test.backend, test.observer, test.view.getSize(), 1, fileSource, test.threadPool, MapMode::Still); - map.getStyle().loadURL("mapbox://styles/test"); - map.getStyle().addLayer(std::make_unique<style::BackgroundLayer>("bg")); + test.map.getStyle().loadURL("mapbox://styles/test"); + test.map.getStyle().addLayer(std::make_unique<style::BackgroundLayer>("bg")); Response response; response.data = std::make_shared<std::string>(util::read_file("test/fixtures/api/water.json")); - fileSource.respond(Resource::Style, response); + test.fileSource.respond(Resource::Style, response); - EXPECT_EQ(0u, fileSource.requests.size()); - EXPECT_NE(nullptr, map.getStyle().getLayer("water")); + EXPECT_EQ(0u, test.fileSource.requests.size()); + EXPECT_NE(nullptr, test.map.getStyle().getLayer("water")); } TEST(Map, MapLoadingSignal) { - MapTest test; - Map map(test.backend, test.observer, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still); + MapTest<> test; bool emitted = false; test.observer.onWillStartLoadingMapCallback = [&]() { emitted = true; }; - map.getStyle().loadJSON(util::read_file("test/fixtures/api/empty.json")); + test.map.getStyle().loadJSON(util::read_file("test/fixtures/api/empty.json")); EXPECT_TRUE(emitted); } TEST(Map, MapLoadedSignal) { - MapTest test; - Map map(test.backend, test.observer, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Continuous); + MapTest<> test { 1, MapMode::Continuous }; test.observer.onDidFinishLoadingMapCallback = [&]() { test.runLoop.stop(); }; - test.backend.callback = [&]() { - map.render(test.view); - }; - - map.getStyle().loadJSON(util::read_file("test/fixtures/api/empty.json")); + test.map.getStyle().loadJSON(util::read_file("test/fixtures/api/empty.json")); test.runLoop.run(); } TEST(Map, StyleLoadedSignal) { - MapTest test; - Map map(test.backend, test.observer, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still); + MapTest<> test; // The map should emit a signal on style loaded bool emitted = false; test.observer.didFinishLoadingStyleCallback = [&]() { emitted = true; }; - map.getStyle().loadJSON(util::read_file("test/fixtures/api/empty.json")); + test.map.getStyle().loadJSON(util::read_file("test/fixtures/api/empty.json")); EXPECT_TRUE(emitted); // But not when the style couldn't be parsed emitted = false; - map.getStyle().loadJSON("invalid"); + test.map.getStyle().loadJSON("invalid"); EXPECT_FALSE(emitted); } // Test for https://github.com/mapbox/mapbox-gl-native/issues/7902 TEST(Map, TEST_REQUIRES_SERVER(StyleNetworkErrorRetry)) { - MapTest test; - OnlineFileSource fileSource; + MapTest<OnlineFileSource> test; - Map map(test.backend, test.observer, test.view.getSize(), 1, fileSource, test.threadPool, MapMode::Still); - map.getStyle().loadURL("http://127.0.0.1:3000/style-fail-once-500"); + test.map.getStyle().loadURL("http://127.0.0.1:3000/style-fail-once-500"); test.observer.didFinishLoadingStyleCallback = [&]() { test.runLoop.stop(); @@ -391,11 +371,9 @@ TEST(Map, TEST_REQUIRES_SERVER(StyleNetworkErrorRetry)) { } TEST(Map, TEST_REQUIRES_SERVER(StyleNotFound)) { - MapTest test; - OnlineFileSource fileSource; + MapTest<OnlineFileSource> test; - Map map(test.backend, test.observer, test.view.getSize(), 1, fileSource, test.threadPool, MapMode::Still); - map.getStyle().loadURL("http://127.0.0.1:3000/style-fail-once-404"); + test.map.getStyle().loadURL("http://127.0.0.1:3000/style-fail-once-404"); using namespace std::chrono_literals; util::Timer timer; @@ -414,52 +392,46 @@ TEST(Map, TEST_REQUIRES_SERVER(StyleNotFound)) { test.runLoop.run(); // Should also not retry if the response has cache headers. - map.getStyle().loadURL("http://127.0.0.1:3000/style-fail-once-404-cache"); + test.map.getStyle().loadURL("http://127.0.0.1:3000/style-fail-once-404-cache"); test.runLoop.run(); } TEST(Map, AddLayer) { - MapTest test; + MapTest<> test; - Map map(test.backend, test.observer, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still); - map.getStyle().loadJSON(util::read_file("test/fixtures/api/empty.json")); + test.map.getStyle().loadJSON(util::read_file("test/fixtures/api/empty.json")); auto layer = std::make_unique<BackgroundLayer>("background"); layer->setBackgroundColor({ { 1, 0, 0, 1 } }); - map.getStyle().addLayer(std::move(layer)); + test.map.getStyle().addLayer(std::move(layer)); - test::checkImage("test/fixtures/map/add_layer", test::render(map, test.view)); + test::checkImage("test/fixtures/map/add_layer", test::render(test.map, test.view)); } TEST(Map, WithoutVAOExtension) { - MapTest test; - + MapTest<DefaultFileSource> test { ":memory:", "test/fixtures/api/assets" }; test.backend.getContext().disableVAOExtension = true; - DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets"); + test.map.getStyle().loadJSON(util::read_file("test/fixtures/api/water.json")); - Map map(test.backend, test.observer, test.view.getSize(), 1, fileSource, test.threadPool, MapMode::Still); - map.getStyle().loadJSON(util::read_file("test/fixtures/api/water.json")); - - test::checkImage("test/fixtures/map/no_vao", test::render(map, test.view), 0.002); + test::checkImage("test/fixtures/map/no_vao", test::render(test.map, test.view), 0.002); } TEST(Map, RemoveLayer) { - MapTest test; + MapTest<> test; - Map map(test.backend, test.observer, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still); - map.getStyle().loadJSON(util::read_file("test/fixtures/api/empty.json")); + test.map.getStyle().loadJSON(util::read_file("test/fixtures/api/empty.json")); auto layer = std::make_unique<BackgroundLayer>("background"); layer->setBackgroundColor({{ 1, 0, 0, 1 }}); - map.getStyle().addLayer(std::move(layer)); - map.getStyle().removeLayer("background"); + test.map.getStyle().addLayer(std::move(layer)); + test.map.getStyle().removeLayer("background"); - test::checkImage("test/fixtures/map/remove_layer", test::render(map, test.view)); + test::checkImage("test/fixtures/map/remove_layer", test::render(test.map, test.view)); } TEST(Map, DisabledSources) { - MapTest test; + MapTest<> test; // Always load the same image tile for raster layers. test.fileSource.response = [] (const Resource& res) -> optional<Response> { @@ -472,15 +444,14 @@ TEST(Map, DisabledSources) { return {}; }; - Map map(test.backend, test.observer, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still); - map.setZoom(1); + test.map.setZoom(1); // This stylesheet has two raster layers, one that starts at zoom 1, the other at zoom 0. // We first render a map at zoom level 1, which should show both layers (both are "visible" due // to an opacity of 0.5). Then, we are zooming back out to a zoom level of 0.5 and rerender. // The "raster1" layer should not be visible anymore since it has minzoom 1, while "raster2" // should still be there. Both layers have a distinct color through "raster-hue-rotate". - map.getStyle().loadJSON(R"STYLE( + test.map.getStyle().loadJSON(R"STYLE( { "version": 8, "name": "Test", @@ -514,16 +485,15 @@ TEST(Map, DisabledSources) { } )STYLE"); - test::checkImage("test/fixtures/map/disabled_layers/first", test::render(map, test.view)); - map.setZoom(0.5); - test::checkImage("test/fixtures/map/disabled_layers/second", test::render(map, test.view)); + test::checkImage("test/fixtures/map/disabled_layers/first", test::render(test.map, test.view)); + test.map.setZoom(0.5); + test::checkImage("test/fixtures/map/disabled_layers/second", test::render(test.map, test.view)); } TEST(Map, DontLoadUnneededTiles) { - MapTest test; + MapTest<> test; - Map map(test.backend, test.observer, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still); - map.getStyle().loadJSON(R"STYLE({ + test.map.getStyle().loadJSON(R"STYLE({ "sources": { "a": { "type": "vector", "tiles": [ "a/{z}/{x}/{y}" ] } }, @@ -561,20 +531,20 @@ TEST(Map, DontLoadUnneededTiles) { // Note: using z += 0.1 in the loop doesn't produce accurate floating point numbers. const double z = double(zoom) / 10; tiles.clear(); - map.setZoom(z); - test::render(map, test.view); + test.map.setZoom(z); + test::render(test.map, test.view); EXPECT_EQ(referenceTiles[z], tiles) << "zoom level " << z; } } TEST(Map, TEST_DISABLED_ON_CI(ContinuousRendering)) { util::RunLoop runLoop; - MockBackend backend; - BackendScope scope { backend }; + HeadlessBackend backend { test::sharedDisplay() }; + BackendScope scope(backend); OffscreenView view { backend.getContext() }; ThreadPool threadPool { 4 }; DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets"); - Map map(backend, MapObserver::nullObserver(), view.getSize(), 1, fileSource, threadPool, MapMode::Continuous); + float pixelRatio { 1 }; using namespace std::chrono_literals; @@ -585,24 +555,32 @@ TEST(Map, TEST_DISABLED_ON_CI(ContinuousRendering)) { }); util::Timer timer; - util::AsyncTask render{[&] { - if (map.isFullyLoaded()) { - // Abort the test after 1 second after the map loading fully. Note that a "fully loaded - // map" doesn't mean that we won't render anymore: we could still render fade in/fade - // out or other animations. - // If we are continuing to render indefinitely, the emergency shutoff above will trigger - // and the test will fail since the regular time will be constantly reset. - timer.start(1s, 0s, [&] { - util::RunLoop::Get()->stop(); - }); - } - BackendScope scope2(backend); - map.render(view); - }}; + std::function<bool()> isLoaded; + + StubRendererFrontend rendererFrontend { + std::make_unique<Renderer>(backend, pixelRatio, fileSource, threadPool), + [&](StubRendererFrontend& bridge) { + if (isLoaded()) { + // Abort the test after 1 second after the map loading fully. Note that a "fully loaded + // map" doesn't mean that we won't render anymore: we could still render fade in/fade + // out or other animations. + // If we are continuing to render indefinitely, the emergency shutoff above will trigger + // and the test will fail since the regular time will be constantly reset. + timer.start(1s, 0s, [&] { + util::RunLoop::Get()->stop(); + }); + } + + bridge.render(view); + } + }; + + Map map(rendererFrontend, MapObserver::nullObserver(), view.getSize(), pixelRatio, fileSource, + threadPool, MapMode::Continuous); - backend.callback = [&] { - render.send(); + isLoaded = [&] { + return map.isFullyLoaded(); }; map.getStyle().loadJSON(util::read_file("test/fixtures/api/water.json")); diff --git a/test/map/prefetch.test.cpp b/test/map/prefetch.test.cpp index 47b39f7b88..d14f5db492 100644 --- a/test/map/prefetch.test.cpp +++ b/test/map/prefetch.test.cpp @@ -5,12 +5,14 @@ #include <mbgl/gl/offscreen_view.hpp> #include <mbgl/map/backend_scope.hpp> #include <mbgl/map/map.hpp> +#include <mbgl/renderer/renderer.hpp> #include <mbgl/storage/default_file_source.hpp> #include <mbgl/style/style.hpp> #include <mbgl/util/default_thread_pool.hpp> #include <mbgl/util/image.hpp> #include <mbgl/util/io.hpp> #include <mbgl/util/run_loop.hpp> +#include <mbgl/test/stub_renderer_frontend.hpp> #include <algorithm> #include <string> @@ -27,7 +29,9 @@ TEST(Map, PrefetchTiles) { OffscreenView view(backend.getContext(), { 512, 512 }); ThreadPool threadPool(4); StubFileSource fileSource; - Map map(backend, MapObserver::nullObserver(), view.getSize(), 1, fileSource, threadPool, MapMode::Still); + StubRendererFrontend rendererFrontend { + std::make_unique<Renderer>(backend, 1, fileSource, threadPool), view }; + Map map(rendererFrontend, MapObserver::nullObserver(), view.getSize(), 1, fileSource, threadPool, MapMode::Still); std::vector<int> tiles; diff --git a/test/src/mbgl/test/stub_renderer_frontend.cpp b/test/src/mbgl/test/stub_renderer_frontend.cpp new file mode 100644 index 0000000000..7edcfd397e --- /dev/null +++ b/test/src/mbgl/test/stub_renderer_frontend.cpp @@ -0,0 +1,52 @@ +#include <mbgl/test/stub_renderer_frontend.hpp> + +#include <mbgl/map/view.hpp> +#include <mbgl/renderer/renderer.hpp> + +namespace mbgl { + +StubRendererFrontend::StubRendererFrontend(std::unique_ptr<Renderer> renderer_, InvalidateCallback invalidate) + : renderer(std::move(renderer_)) + , asyncInvalidate([this, invalidate=std::move(invalidate)]() { + invalidate(*this); + }) { +} + +StubRendererFrontend::StubRendererFrontend(std::unique_ptr<Renderer> renderer_, View& view) + : renderer(std::move(renderer_)) + , asyncInvalidate([&]() { + this->render(view); + }) { +} + +StubRendererFrontend::~StubRendererFrontend() { + reset(); +} + +void StubRendererFrontend::reset() { + if (renderer) { + renderer.reset(); + } +} + +void StubRendererFrontend::setObserver(RendererObserver& observer) { + if (!renderer) return; + renderer->setObserver(&observer); +} + +void StubRendererFrontend::update(std::shared_ptr<UpdateParameters> params) { + updateParameters = std::move(params); + asyncInvalidate.send(); +} + +void StubRendererFrontend::render(View& view) { + if (!updateParameters || !renderer) return; + renderer->render(view, *updateParameters); +} + +Renderer* StubRendererFrontend::getRenderer() { + return renderer.get(); +} + +} // namespace mbgl + diff --git a/test/src/mbgl/test/stub_renderer_frontend.hpp b/test/src/mbgl/test/stub_renderer_frontend.hpp new file mode 100644 index 0000000000..f6dd1d0472 --- /dev/null +++ b/test/src/mbgl/test/stub_renderer_frontend.hpp @@ -0,0 +1,44 @@ + +#pragma once + +#include <mbgl/renderer/renderer_frontend.hpp> +#include <mbgl/util/async_task.hpp> +#include <mbgl/util/geo.hpp> +#include <mbgl/util/feature.hpp> +#include <mbgl/renderer/query.hpp> + +#include <functional> +#include <memory> + +namespace mbgl { + +class Renderer; +class View; + +class StubRendererFrontend : public RendererFrontend { +public: + // Calls the provided callback when it's time to render + using InvalidateCallback = std::function<void (StubRendererFrontend&)>; + StubRendererFrontend(std::unique_ptr<Renderer>, InvalidateCallback); + + // Will render async + StubRendererFrontend(std::unique_ptr<Renderer>, View&); + + ~StubRendererFrontend() override; + + void reset() override; + + void setObserver(RendererObserver&) override; + + void update(std::shared_ptr<UpdateParameters>) override; + void render(View& view); + + Renderer* getRenderer(); + +private: + std::unique_ptr<Renderer> renderer; + std::shared_ptr<UpdateParameters> updateParameters; + util::AsyncTask asyncInvalidate; +}; + +} // namespace mbgl diff --git a/test/src/mbgl/test/util.cpp b/test/src/mbgl/test/util.cpp index 7ca2d72504..f3b0bbc96f 100644 --- a/test/src/mbgl/test/util.cpp +++ b/test/src/mbgl/test/util.cpp @@ -106,7 +106,7 @@ std::shared_ptr<HeadlessDisplay> sharedDisplay() { PremultipliedImage render(Map& map, OffscreenView& view) { PremultipliedImage result; - map.renderStill(view, [&](std::exception_ptr) { + map.renderStill([&](std::exception_ptr) { result = view.readStillImage(); }); diff --git a/test/tile/annotation_tile.test.cpp b/test/tile/annotation_tile.test.cpp index 739307b78a..bed0bb160e 100644 --- a/test/tile/annotation_tile.test.cpp +++ b/test/tile/annotation_tile.test.cpp @@ -4,10 +4,9 @@ #include <mbgl/util/default_thread_pool.hpp> #include <mbgl/util/run_loop.hpp> #include <mbgl/map/transform.hpp> -#include <mbgl/map/query.hpp> #include <mbgl/renderer/render_style.hpp> #include <mbgl/renderer/tile_parameters.hpp> -#include <mbgl/map/query.hpp> +#include <mbgl/renderer/query.hpp> #include <mbgl/text/collision_tile.hpp> #include <mbgl/geometry/feature_index.hpp> #include <mbgl/annotation/annotation_manager.hpp> diff --git a/test/tile/vector_tile.test.cpp b/test/tile/vector_tile.test.cpp index b0fee28edf..45eab21576 100644 --- a/test/tile/vector_tile.test.cpp +++ b/test/tile/vector_tile.test.cpp @@ -6,11 +6,11 @@ #include <mbgl/util/default_thread_pool.hpp> #include <mbgl/util/run_loop.hpp> #include <mbgl/map/transform.hpp> -#include <mbgl/map/query.hpp> #include <mbgl/style/style.hpp> #include <mbgl/style/layers/symbol_layer.hpp> #include <mbgl/renderer/tile_parameters.hpp> #include <mbgl/renderer/buckets/symbol_bucket.hpp> +#include <mbgl/renderer/query.hpp> #include <mbgl/text/collision_tile.hpp> #include <mbgl/geometry/feature_index.hpp> #include <mbgl/annotation/annotation_manager.hpp> diff --git a/test/util/memory.test.cpp b/test/util/memory.test.cpp index 8fd27f2462..fb93699d7f 100644 --- a/test/util/memory.test.cpp +++ b/test/util/memory.test.cpp @@ -10,6 +10,8 @@ #include <mbgl/util/io.hpp> #include <mbgl/util/run_loop.hpp> #include <mbgl/style/style.hpp> +#include <mbgl/renderer/renderer.hpp> +#include <mbgl/test/stub_renderer_frontend.hpp> #include <algorithm> #include <iostream> @@ -38,7 +40,7 @@ public: util::RunLoop runLoop; HeadlessBackend backend { test::sharedDisplay() }; BackendScope scope { backend }; - OffscreenView view{ backend.getContext(), { 512, 512 } }; + OffscreenView view { backend.getContext(), { 512, 512 } }; StubFileSource fileSource; ThreadPool threadPool { 4 }; @@ -73,8 +75,13 @@ private: TEST(Memory, Vector) { MemoryTest test; + float ratio { 2 }; - Map map(test.backend, MapObserver::nullObserver(), { 256, 256 }, 2, test.fileSource, test.threadPool, MapMode::Still); + StubRendererFrontend rendererFrontend { + std::make_unique<Renderer>(test.backend, ratio, test.fileSource, test.threadPool), + test.view }; + Map map(rendererFrontend, MapObserver::nullObserver(), { 256, 256 }, ratio, test.fileSource, + test.threadPool, MapMode::Still); map.setZoom(16); // more map features map.getStyle().loadURL("mapbox://streets"); @@ -83,8 +90,14 @@ TEST(Memory, Vector) { TEST(Memory, Raster) { MemoryTest test; + float ratio { 2 }; - Map map(test.backend, MapObserver::nullObserver(), { 256, 256 }, 2, test.fileSource, test.threadPool, MapMode::Still); + StubRendererFrontend rendererFrontend { + std::make_unique<Renderer>(test.backend, ratio, test.fileSource, test.threadPool), + test.view }; + + Map map(rendererFrontend, MapObserver::nullObserver(), { 256, 256 }, ratio, test.fileSource, + test.threadPool, MapMode::Still); map.getStyle().loadURL("mapbox://satellite"); test::render(map, test.view); @@ -116,6 +129,7 @@ TEST(Memory, Footprint) { } MemoryTest test; + float ratio { 2 }; auto renderMap = [&](Map& map, const char* style){ map.setZoom(16); @@ -125,7 +139,11 @@ TEST(Memory, Footprint) { // Warm up buffers and cache. for (unsigned i = 0; i < 10; ++i) { - Map map(test.backend, MapObserver::nullObserver(), { 256, 256 }, 2, test.fileSource, test.threadPool, MapMode::Still); + StubRendererFrontend rendererFrontend { + std::make_unique<Renderer>(test.backend, ratio, test.fileSource, test.threadPool), + test.view }; + Map map(rendererFrontend, MapObserver::nullObserver(), { 256, 256 }, ratio, test.fileSource, + test.threadPool, MapMode::Still); renderMap(map, "mapbox://streets"); renderMap(map, "mapbox://satellite"); }; @@ -134,27 +152,31 @@ TEST(Memory, Footprint) { // libuv runloop. test.runLoop.runOnce(); - std::vector<std::unique_ptr<Map>> maps; + std::vector<std::pair<std::unique_ptr<Map>, std::unique_ptr<RendererFrontend>>> maps; unsigned runs = 15; long vectorInitialRSS = mbgl::test::getCurrentRSS(); for (unsigned i = 0; i < runs; ++i) { - auto vector = std::make_unique<Map>(test.backend, MapObserver::nullObserver(), - Size{ 256, 256 }, 2, test.fileSource, test.threadPool, - MapMode::Still); + auto frontend = std::make_unique<StubRendererFrontend>( + std::make_unique<Renderer>(test.backend, ratio, test.fileSource, test.threadPool), test.view); + auto vector = std::make_unique<Map>(*frontend, MapObserver::nullObserver(), + Size{ 256, 256 }, ratio, test.fileSource, + test.threadPool, MapMode::Still); renderMap(*vector, "mapbox://streets"); - maps.push_back(std::move(vector)); + maps.push_back({std::move(vector), std::move(frontend)}); }; double vectorFootprint = (mbgl::test::getCurrentRSS() - vectorInitialRSS) / double(runs); long rasterInitialRSS = mbgl::test::getCurrentRSS(); for (unsigned i = 0; i < runs; ++i) { - auto raster = std::make_unique<Map>(test.backend, MapObserver::nullObserver(), - Size{ 256, 256 }, 2, test.fileSource, test.threadPool, - MapMode::Still); + auto frontend = std::make_unique<StubRendererFrontend>( + std::make_unique<Renderer>(test.backend, ratio, test.fileSource, test.threadPool), test.view); + auto raster = std::make_unique<Map>(*frontend, MapObserver::nullObserver(), + Size{ 256, 256 }, ratio, test.fileSource, + test.threadPool, MapMode::Still); renderMap(*raster, "mapbox://satellite"); - maps.push_back(std::move(raster)); + maps.push_back({std::move(raster), std::move(frontend)}); }; double rasterFootprint = (mbgl::test::getCurrentRSS() - rasterInitialRSS) / double(runs); |