From a5a0558bde5d67617b6f305179063cd4e0ac329e Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Wed, 5 Jul 2017 18:04:15 +0300 Subject: [test] allow only a single shared display to avoid egl errors --- platform/darwin/src/headless_backend_cgl.cpp | 2 +- platform/default/mbgl/gl/headless_backend.cpp | 4 ---- platform/default/mbgl/gl/headless_backend.hpp | 1 - platform/default/mbgl/gl/headless_display.hpp | 16 +++++++++++++++- platform/linux/src/headless_backend_egl.cpp | 2 +- platform/linux/src/headless_backend_glx.cpp | 2 +- platform/node/src/node_map.cpp | 12 +----------- test/api/annotations.test.cpp | 2 +- test/api/api_misuse.test.cpp | 2 +- test/api/custom_layer.test.cpp | 2 +- test/api/query.test.cpp | 2 +- test/api/render_missing.test.cpp | 2 +- test/api/repeated_render.test.cpp | 4 ++-- test/gl/object.test.cpp | 2 +- test/map/map.test.cpp | 5 +++-- test/map/prefetch.test.cpp | 2 +- test/src/mbgl/test/util.cpp | 6 ------ test/src/mbgl/test/util.hpp | 2 -- test/tile/annotation_tile.test.cpp | 2 +- test/util/memory.test.cpp | 2 +- test/util/offscreen_texture.test.cpp | 4 ++-- 21 files changed, 35 insertions(+), 43 deletions(-) diff --git a/platform/darwin/src/headless_backend_cgl.cpp b/platform/darwin/src/headless_backend_cgl.cpp index 6ad98f4326..3b0c3aaf35 100644 --- a/platform/darwin/src/headless_backend_cgl.cpp +++ b/platform/darwin/src/headless_backend_cgl.cpp @@ -51,7 +51,7 @@ gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) { bool HeadlessBackend::hasDisplay() { if (!display) { - display.reset(new HeadlessDisplay); + display = HeadlessDisplay::create(); } return bool(display); } diff --git a/platform/default/mbgl/gl/headless_backend.cpp b/platform/default/mbgl/gl/headless_backend.cpp index e17e8f5c11..9076be4cbb 100644 --- a/platform/default/mbgl/gl/headless_backend.cpp +++ b/platform/default/mbgl/gl/headless_backend.cpp @@ -11,10 +11,6 @@ namespace mbgl { HeadlessBackend::HeadlessBackend() = default; -HeadlessBackend::HeadlessBackend(std::shared_ptr display_) - : display(std::move(display_)) { -} - HeadlessBackend::~HeadlessBackend() { BackendScope guard { *this, getScopeType() }; context.reset(); diff --git a/platform/default/mbgl/gl/headless_backend.hpp b/platform/default/mbgl/gl/headless_backend.hpp index 133c2096d9..5f39eb9f00 100644 --- a/platform/default/mbgl/gl/headless_backend.hpp +++ b/platform/default/mbgl/gl/headless_backend.hpp @@ -12,7 +12,6 @@ class HeadlessDisplay; class HeadlessBackend : public RendererBackend { public: HeadlessBackend(); - HeadlessBackend(std::shared_ptr); ~HeadlessBackend() override; void updateAssumedState() override; diff --git a/platform/default/mbgl/gl/headless_display.hpp b/platform/default/mbgl/gl/headless_display.hpp index a5c95085b8..8c294655e5 100644 --- a/platform/default/mbgl/gl/headless_display.hpp +++ b/platform/default/mbgl/gl/headless_display.hpp @@ -6,13 +6,27 @@ namespace mbgl { class HeadlessDisplay { public: - HeadlessDisplay(); + static std::shared_ptr create() { + static std::weak_ptr instance; + + auto shared = instance.lock(); + + if (!shared) { + instance = shared = std::shared_ptr(new HeadlessDisplay()); + } + + return shared; + } + + ~HeadlessDisplay(); template DisplayAttribute attribute() const; private: + HeadlessDisplay(); + class Impl; std::unique_ptr impl; }; diff --git a/platform/linux/src/headless_backend_egl.cpp b/platform/linux/src/headless_backend_egl.cpp index d98b2edc03..0784173af7 100644 --- a/platform/linux/src/headless_backend_egl.cpp +++ b/platform/linux/src/headless_backend_egl.cpp @@ -67,7 +67,7 @@ gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) { bool HeadlessBackend::hasDisplay() { if (!display) { - display.reset(new HeadlessDisplay); + display = HeadlessDisplay::create(); } return bool(display); }; diff --git a/platform/linux/src/headless_backend_glx.cpp b/platform/linux/src/headless_backend_glx.cpp index eec0e7656f..0ba7f08630 100644 --- a/platform/linux/src/headless_backend_glx.cpp +++ b/platform/linux/src/headless_backend_glx.cpp @@ -50,7 +50,7 @@ gl::ProcAddress HeadlessBackend::initializeExtension(const char* name) { bool HeadlessBackend::hasDisplay() { if (!display) { - display.reset(new HeadlessDisplay); + display = HeadlessDisplay::create(); } return bool(display); }; diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index b4e93455ea..079112b4c8 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -5,7 +5,6 @@ #include "node_geojson.hpp" #include "node_renderer_frontend.hpp" -#include #include #include #include @@ -35,17 +34,11 @@ struct NodeMap::RenderOptions { Nan::Persistent NodeMap::constructor; -static std::shared_ptr sharedDisplay() { - static auto display = std::make_shared(); - return display; -} - static const char* releasedMessage() { return "Map resources have already been released"; } -NodeBackend::NodeBackend() - : HeadlessBackend(sharedDisplay()) {} +NodeBackend::NodeBackend(): HeadlessBackend() {} void NodeMapObserver::onDidFailLoadingMap(std::exception_ptr error) { std::rethrow_exception(error); @@ -81,9 +74,6 @@ void NodeMap::Init(v8::Local target) { constructor.Reset(tpl->GetFunction()); Nan::Set(target, Nan::New("Map").ToLocalChecked(), tpl->GetFunction()); - - // Initialize display connection on module load. - sharedDisplay(); } /** diff --git a/test/api/annotations.test.cpp b/test/api/annotations.test.cpp index bc1ad54129..878ef90831 100644 --- a/test/api/annotations.test.cpp +++ b/test/api/annotations.test.cpp @@ -30,7 +30,7 @@ std::unique_ptr namedMarker(const std::string& name) { class AnnotationTest { public: util::RunLoop loop; - HeadlessBackend backend { test::sharedDisplay() }; + HeadlessBackend backend; BackendScope scope { backend }; OffscreenView view { backend.getContext() }; StubFileSource fileSource; diff --git a/test/api/api_misuse.test.cpp b/test/api/api_misuse.test.cpp index ed5cc41a49..9f53b763be 100644 --- a/test/api/api_misuse.test.cpp +++ b/test/api/api_misuse.test.cpp @@ -24,7 +24,7 @@ TEST(API, RenderWithoutCallback) { util::RunLoop loop; - HeadlessBackend backend { test::sharedDisplay() }; + HeadlessBackend backend; BackendScope scope { backend }; OffscreenView view { backend.getContext(), { 128, 512 } }; StubFileSource fileSource; diff --git a/test/api/custom_layer.test.cpp b/test/api/custom_layer.test.cpp index da5b535a86..972485338f 100644 --- a/test/api/custom_layer.test.cpp +++ b/test/api/custom_layer.test.cpp @@ -89,7 +89,7 @@ public: TEST(CustomLayer, Basic) { util::RunLoop loop; - HeadlessBackend backend { test::sharedDisplay() }; + HeadlessBackend backend; BackendScope scope { backend }; OffscreenView view { backend.getContext() }; DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets"); diff --git a/test/api/query.test.cpp b/test/api/query.test.cpp index 5135b51e8b..a5224222cb 100644 --- a/test/api/query.test.cpp +++ b/test/api/query.test.cpp @@ -30,7 +30,7 @@ public: } util::RunLoop loop; - HeadlessBackend backend { test::sharedDisplay() }; + HeadlessBackend backend; BackendScope scope { backend }; OffscreenView view { backend.getContext() }; StubFileSource fileSource; diff --git a/test/api/render_missing.test.cpp b/test/api/render_missing.test.cpp index 01a0e9293b..02c19d359b 100644 --- a/test/api/render_missing.test.cpp +++ b/test/api/render_missing.test.cpp @@ -30,7 +30,7 @@ TEST(API, TEST_REQUIRES_SERVER(RenderMissingTile)) { const auto style = util::read_file("test/fixtures/api/water_missing_tiles.json"); - HeadlessBackend backend { test::sharedDisplay() }; + HeadlessBackend backend; BackendScope scope { backend }; OffscreenView view { backend.getContext(), { 256, 512 } }; float pixelRatio { 1 }; diff --git a/test/api/repeated_render.test.cpp b/test/api/repeated_render.test.cpp index 7d61992769..0e8a2715b7 100644 --- a/test/api/repeated_render.test.cpp +++ b/test/api/repeated_render.test.cpp @@ -27,7 +27,7 @@ TEST(API, RepeatedRender) { const auto style = util::read_file("test/fixtures/api/water.json"); - HeadlessBackend backend { test::sharedDisplay() }; + HeadlessBackend backend; BackendScope scope { backend }; OffscreenView view { backend.getContext(), { 512, 512 } }; float pixelRatio { 1 }; @@ -84,7 +84,7 @@ TEST(API, ZoomHistory) { const auto style = util::read_file("test/fixtures/api/empty.json"); - HeadlessBackend backend { test::sharedDisplay() }; + HeadlessBackend backend; BackendScope scope { backend }; OffscreenView view { backend.getContext(), { 512, 512 } }; DefaultFileSource fileSource(":memory:", "."); diff --git a/test/gl/object.test.cpp b/test/gl/object.test.cpp index cf887dab40..30699718b3 100644 --- a/test/gl/object.test.cpp +++ b/test/gl/object.test.cpp @@ -47,7 +47,7 @@ TEST(GLObject, Value) { } TEST(GLObject, Store) { - HeadlessBackend backend { test::sharedDisplay() }; + HeadlessBackend backend; BackendScope scope { backend }; OffscreenView view(backend.getContext()); diff --git a/test/map/map.test.cpp b/test/map/map.test.cpp index 93890aa9f3..3e8f8696e1 100644 --- a/test/map/map.test.cpp +++ b/test/map/map.test.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -539,8 +540,8 @@ TEST(Map, DontLoadUnneededTiles) { TEST(Map, TEST_DISABLED_ON_CI(ContinuousRendering)) { util::RunLoop runLoop; - HeadlessBackend backend { test::sharedDisplay() }; - BackendScope scope(backend); + HeadlessBackend backend; + BackendScope scope { backend }; OffscreenView view { backend.getContext() }; ThreadPool threadPool { 4 }; DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets"); diff --git a/test/map/prefetch.test.cpp b/test/map/prefetch.test.cpp index c3ff04acfc..436bdc3b63 100644 --- a/test/map/prefetch.test.cpp +++ b/test/map/prefetch.test.cpp @@ -24,7 +24,7 @@ using namespace std::literals::string_literals; TEST(Map, PrefetchTiles) { util::RunLoop runLoop; - HeadlessBackend backend(test::sharedDisplay()); + HeadlessBackend backend; BackendScope scope(backend); OffscreenView view(backend.getContext(), { 512, 512 }); ThreadPool threadPool(4); diff --git a/test/src/mbgl/test/util.cpp b/test/src/mbgl/test/util.cpp index f3b0bbc96f..0b1034e315 100644 --- a/test/src/mbgl/test/util.cpp +++ b/test/src/mbgl/test/util.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -99,11 +98,6 @@ Server::~Server() { } } -std::shared_ptr sharedDisplay() { - static auto display = std::make_shared(); - return display; -} - PremultipliedImage render(Map& map, OffscreenView& view) { PremultipliedImage result; map.renderStill([&](std::exception_ptr) { diff --git a/test/src/mbgl/test/util.hpp b/test/src/mbgl/test/util.hpp index 8673155fe4..9c015f1641 100644 --- a/test/src/mbgl/test/util.hpp +++ b/test/src/mbgl/test/util.hpp @@ -70,8 +70,6 @@ private: int fd = -1; }; -std::shared_ptr sharedDisplay(); - PremultipliedImage render(Map&, OffscreenView&); void checkImage(const std::string& base, diff --git a/test/tile/annotation_tile.test.cpp b/test/tile/annotation_tile.test.cpp index 50f1facc10..c58b980ecd 100644 --- a/test/tile/annotation_tile.test.cpp +++ b/test/tile/annotation_tile.test.cpp @@ -29,7 +29,7 @@ public: ThreadPool threadPool { 1 }; style::Style style { loop, fileSource, 1 }; AnnotationManager annotationManager { style }; - HeadlessBackend backend { test::sharedDisplay() }; + HeadlessBackend backend; BackendScope scope { backend }; RenderStyle renderStyle { threadPool, fileSource }; ImageManager imageManager; diff --git a/test/util/memory.test.cpp b/test/util/memory.test.cpp index cb333129d8..ad7982ce57 100644 --- a/test/util/memory.test.cpp +++ b/test/util/memory.test.cpp @@ -38,7 +38,7 @@ public: } util::RunLoop runLoop; - HeadlessBackend backend { test::sharedDisplay() }; + HeadlessBackend backend; BackendScope scope { backend }; OffscreenView view { backend.getContext(), { 512, 512 } }; StubFileSource fileSource; diff --git a/test/util/offscreen_texture.test.cpp b/test/util/offscreen_texture.test.cpp index d8a409de36..d4efd75689 100644 --- a/test/util/offscreen_texture.test.cpp +++ b/test/util/offscreen_texture.test.cpp @@ -11,7 +11,7 @@ using namespace mbgl; TEST(OffscreenTexture, EmptyRed) { - HeadlessBackend backend { test::sharedDisplay() }; + HeadlessBackend backend; BackendScope scope { backend }; OffscreenView view(backend.getContext(), { 512, 256 }); @@ -74,7 +74,7 @@ struct Buffer { TEST(OffscreenTexture, RenderToTexture) { - HeadlessBackend backend { test::sharedDisplay() }; + HeadlessBackend backend; BackendScope scope { backend }; auto& context = backend.getContext(); -- cgit v1.2.1