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 /test | |
parent | 39a732d7ae3cb1b927d94c4b1154b42d9565356a (diff) | |
download | qtlocation-mapboxgl-3832f8d0d8194b81ea34a045e19b0d5bc7a89e25.tar.gz |
[core] renderer interface
Diffstat (limited to 'test')
-rw-r--r-- | test/api/annotations.test.cpp | 17 | ||||
-rw-r--r-- | test/api/api_misuse.test.cpp | 11 | ||||
-rw-r--r-- | test/api/custom_layer.test.cpp | 9 | ||||
-rw-r--r-- | test/api/query.test.cpp | 40 | ||||
-rw-r--r-- | test/api/render_missing.test.cpp | 10 | ||||
-rw-r--r-- | test/api/repeated_render.test.cpp | 21 | ||||
-rw-r--r-- | test/map/map.test.cpp | 344 | ||||
-rw-r--r-- | test/map/prefetch.test.cpp | 6 | ||||
-rw-r--r-- | test/src/mbgl/test/stub_renderer_frontend.cpp | 52 | ||||
-rw-r--r-- | test/src/mbgl/test/stub_renderer_frontend.hpp | 44 | ||||
-rw-r--r-- | test/src/mbgl/test/util.cpp | 2 | ||||
-rw-r--r-- | test/tile/annotation_tile.test.cpp | 3 | ||||
-rw-r--r-- | test/tile/vector_tile.test.cpp | 2 | ||||
-rw-r--r-- | test/util/memory.test.cpp | 48 |
14 files changed, 372 insertions, 237 deletions
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); |